summaryrefslogtreecommitdiff
path: root/sys/external/bsd/drm2/linux/linux_sgt.c
blob: 3196cd9eedf2d20fed31565341bebd5da76028f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*	$NetBSD: linux_sgt.c,v 1.4 2021/12/24 15:08:31 riastradh Exp $	*/

/*-
 * Copyright (c) 2021 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_sgt.c,v 1.4 2021/12/24 15:08:31 riastradh Exp $");

#include <sys/bus.h>
#include <sys/errno.h>

#include <drm/bus_dma_hacks.h>

#include <linux/dma-mapping.h>
#include <linux/gfp.h>
#include <linux/mm_types.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>

int
sg_alloc_table(struct sg_table *sgt, unsigned npgs, gfp_t gfp)
{

	sgt->sgl->sg_pgs = kcalloc(npgs, sizeof(sgt->sgl->sg_pgs[0]), gfp);
	if (sgt->sgl->sg_pgs == NULL)
		return -ENOMEM;
	sgt->sgl->sg_npgs = sgt->nents = npgs;
	sgt->sgl->sg_dmamap = NULL;

	return 0;
}

int
__sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pgs,
    unsigned npgs, bus_size_t offset, bus_size_t size, unsigned maxseg,
    gfp_t gfp)
{
	unsigned i;
	int ret;

	KASSERT(offset == 0);
	KASSERT(size == (bus_size_t)npgs << PAGE_SHIFT);

	ret = sg_alloc_table(sgt, npgs, gfp);
	if (ret)
		return ret;

	for (i = 0; i < npgs; i++)
		sgt->sgl->sg_pgs[i] = pgs[i];

	return 0;
}

int
sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pgs,
    unsigned npgs, bus_size_t offset, bus_size_t size, gfp_t gfp)
{

	return __sg_alloc_table_from_pages(sgt, pgs, npgs, offset, size,
	    -1, gfp);
}

int
sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat,
    const bus_dma_segment_t *seg, int nseg, gfp_t gfp)
{
	int i, npgs = 0;
	int ret;

	KASSERT(nseg >= 1);

	/*
	 * Count the number of pages.  Some segments may span multiple
	 * contiguous pages.
	 */
	for (i = 0; i < nseg; i++) {
		bus_size_t len = seg[i].ds_len;
		for (; len >= PAGE_SIZE; len -= PAGE_SIZE, npgs++) {
			if (npgs == INT_MAX)
				return -ENOMEM;
		}
		KASSERTMSG(len == 0, "misaligned segment length: %ju\n",
		    (uintmax_t)seg[i].ds_len);
	}

	ret = sg_alloc_table(sgt, npgs, gfp);
	if (ret)
		return ret;

	/* XXX errno NetBSD->Linux */
	ret = -bus_dmamem_export_pages(dmat, seg, nseg, sgt->sgl->sg_pgs,
	    sgt->sgl->sg_npgs);
	if (ret)
		goto out;

	/* Success!  */
	ret = 0;

out:	if (ret)
		sg_free_table(sgt);
	return ret;
}

void
sg_free_table(struct sg_table *sgt)
{

	if (sgt->sgl->sg_dmamap) {
		KASSERT(sgt->sgl->sg_dmat);
		bus_dmamap_destroy(sgt->sgl->sg_dmat, sgt->sgl->sg_dmamap);
	}
	kfree(sgt->sgl->sg_pgs);
	sgt->sgl->sg_pgs = NULL;
	sgt->sgl->sg_npgs = 0;
}

int
dma_map_sg(bus_dma_tag_t dmat, struct scatterlist *sg, int nents, int dir)
{

	return dma_map_sg_attrs(dmat, sg, nents, dir, 0);
}

int
dma_map_sg_attrs(bus_dma_tag_t dmat, struct scatterlist *sg, int nents,
    int dir, int attrs)
{
	int flags = 0;
	bool loaded = false;
	int ret, error = 0;

	KASSERT(sg->sg_dmamap == NULL);
	KASSERT(sg->sg_npgs);
	KASSERT(nents >= 1);

	switch (dir) {
	case DMA_TO_DEVICE:
		flags |= BUS_DMA_WRITE;
		break;
	case DMA_FROM_DEVICE:
		flags |= BUS_DMA_READ;
		break;
	case DMA_BIDIRECTIONAL:
		flags |= BUS_DMA_READ|BUS_DMA_WRITE;
		break;
	case DMA_NONE:
	default:
		panic("invalid DMA direction %d", dir);
	}

	error = bus_dmamap_create(dmat, (bus_size_t)sg->sg_npgs << PAGE_SHIFT,
	    nents, PAGE_SIZE, 0, BUS_DMA_WAITOK, &sg->sg_dmamap);
	if (error)
		goto out;
	KASSERT(sg->sg_dmamap);

	error = bus_dmamap_load_pages(dmat, sg->sg_dmamap, sg->sg_pgs,
	    (bus_size_t)sg->sg_npgs << PAGE_SHIFT, BUS_DMA_WAITOK|flags);
	if (error)
		goto out;
	loaded = true;

	/* Success! */
	KASSERT(sg->sg_dmamap->dm_nsegs > 0);
	KASSERT(sg->sg_dmamap->dm_nsegs <= nents);
	sg->sg_dmat = dmat;
	ret = sg->sg_dmamap->dm_nsegs;
	error = 0;

out:	if (error) {
		if (loaded)
			bus_dmamap_unload(dmat, sg->sg_dmamap);
		loaded = false;
		if (sg->sg_dmamap)
			bus_dmamap_destroy(dmat, sg->sg_dmamap);
		sg->sg_dmamap = NULL;
		ret = 0;
	}
	return ret;
}

void
dma_unmap_sg(bus_dma_tag_t dmat, struct scatterlist *sg, int nents, int dir)
{

	dma_unmap_sg_attrs(dmat, sg, nents, dir, 0);
}

void
dma_unmap_sg_attrs(bus_dma_tag_t dmat, struct scatterlist *sg, int nents,
    int dir, int attrs)
{

	KASSERT(sg->sg_dmat == dmat);

	bus_dmamap_unload(dmat, sg->sg_dmamap);
	bus_dmamap_destroy(dmat, sg->sg_dmamap);
	sg->sg_dmamap = NULL;
}

bus_addr_t
sg_dma_address(const struct scatterlist *sg)
{

	KASSERT(sg->sg_dmamap->dm_nsegs == 1);
	return sg->sg_dmamap->dm_segs[0].ds_addr;
}

bus_size_t
sg_dma_len(const struct scatterlist *sg)
{

	KASSERT(sg->sg_dmamap->dm_nsegs == 1);
	return sg->sg_dmamap->dm_segs[0].ds_len;
}