summaryrefslogtreecommitdiff
path: root/sys/external/bsd/drm2/drm/drm_scatter.c
blob: 9da543fe83eb3fa8a9f2f5b42fe54f34f0150336 (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
239
240
241
242
243
244
/*	$NetBSD: drm_scatter.c,v 1.9 2021/12/19 12:30:05 riastradh Exp $	*/

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * 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: drm_scatter.c,v 1.9 2021/12/19 12:30:05 riastradh Exp $");

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

#include <linux/mm.h>
#include <linux/slab.h>

#include <drm/drm_device.h>
#include <drm/drm_drv.h>

#include "../dist/drm/drm_internal.h"
#include "../dist/drm/drm_legacy.h"

#if IS_ENABLED(CONFIG_DRM_LEGACY)
static int	drm_sg_alloc_mem(struct drm_device *, size_t,
		    struct drm_sg_mem **);
static void	drm_sg_free_mem(struct drm_device *, struct drm_sg_mem *);

int
drm_legacy_sg_alloc(struct drm_device *dev, void *data,
    struct drm_file *file __unused)
{
	struct drm_scatter_gather *const request = data;
	struct drm_sg_mem *sg = NULL;
	int error;

	/*
	 * XXX Should not hold this mutex during drm_sg_alloc.  For
	 * now, we'll just use non-blocking allocations.
	 */
	KASSERT(mutex_is_locked(&drm_global_mutex));

	/*
	 * Sanity-check the inputs.
	 */
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -ENODEV;
	if (dev->sg != NULL)
		return -EBUSY;
	if (request->size > 0xffffffffUL)
		return -ENOMEM;

	/*
	 * Do the allocation.
	 *
	 * XXX Would it be safe to drop drm_global_mutex here to avoid
	 * holding it during allocation?
	 */
	error = drm_sg_alloc_mem(dev, request->size, &sg);
	if (error)
		return error;

	/* Set it up to be the device's scatter-gather memory.  */
	dev->sg = sg;

	/* Success!  */
	request->handle = sg->handle;
	return 0;
}

int
drm_legacy_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
{
	struct drm_scatter_gather *const request = data;
	struct drm_sg_mem *sg;

	KASSERT(mutex_is_locked(&drm_global_mutex));

	/*
	 * Sanity-check the inputs.
	 */
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -ENODEV;

	sg = dev->sg;
	if (sg == NULL)
		return -ENXIO;
	if (sg->handle != request->handle)
		return -EINVAL;

	/* Remove dev->sg.  */
	dev->sg = NULL;

	/* Free it.  */
	drm_sg_free_mem(dev, sg);

	/* Success!  */
	return 0;
}

void
drm_legacy_sg_cleanup(struct drm_device *dev)
{

	if (!drm_core_check_feature(dev, DRIVER_SG))
		return;
	if (dev->sg == NULL)
		return;
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return;

	drm_sg_free_mem(dev, dev->sg);
}

static int
drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
{
	int nsegs;
	int error;

	KASSERT(drm_core_check_feature(dev, DRIVER_SG));

	KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only?  */
	const size_t nbytes = PAGE_ALIGN(size);
	const size_t npages = nbytes >> PAGE_SHIFT;
	KASSERT(npages <= (size_t)INT_MAX);

	/*
	 * Allocate a drm_sg_mem record.
	 */
	struct drm_sg_mem *const sg =
	    kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
		KM_NOSLEEP);
	if (sg == NULL)
		return -ENOMEM;
	sg->sg_tag = dev->dmat;
	sg->sg_nsegs_max = (unsigned int)npages;

	/*
	 * Allocate the requested amount of DMA-safe memory.
	 */
	KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
	/* XXX errno NetBSD->Linux */
	error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
	    sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
	if (error)
		goto fail0;
	KASSERT(0 <= nsegs);
	sg->sg_nsegs = (unsigned int)nsegs;

	/*
	 * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
	 * above.  WTF?
	 */

	/*
	 * Map the DMA-safe memory into kernel virtual address space.
	 */
	/* XXX errno NetBSD->Linux */
	error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
	    &sg->virtual,
	    (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
	if (error)
		goto fail1;
	sg->sg_size = nbytes;

	/*
	 * Create a map for DMA transfers.
	 */
	/* XXX errno NetBSD->Linux */
	error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
	    BUS_DMA_NOWAIT, &sg->sg_map);
	if (error)
		goto fail2;

	/*
	 * Load the kva buffer into the map for DMA transfers.
	 */
	/* XXX errno NetBSD->Linux */
	error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
	    NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
	if (error)
		goto fail3;

	/*
	 * Use the kernel virtual address as the userland handle.
	 *
	 * XXX This leaks some information about kernel virtual
	 * addresses to userland; should we use an idr instead?
	 */
	sg->handle = (unsigned long)(uintptr_t)sg->virtual;
	KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);

	/* Success!  */
	*sgp = sg;
	return 0;

fail3:	bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
fail2:	bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
fail1:	KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
	bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
fail0:	sg->sg_tag = NULL;	/* XXX paranoia */
	kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
	return error;
}

static void
drm_sg_free_mem(struct drm_device *dev, struct drm_sg_mem *sg)
{

	bus_dmamap_unload(sg->sg_tag, sg->sg_map);
	bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
	bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
	KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
	bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
	sg->sg_tag = NULL;	/* XXX paranoia */
	kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
}
#endif