drm_scatter.c revision 1.2.4.2 1 1.2.4.2 rmind /* $NetBSD: drm_scatter.c,v 1.2.4.2 2014/05/18 17:46:00 rmind Exp $ */
2 1.2.4.2 rmind
3 1.2.4.2 rmind /*-
4 1.2.4.2 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.4.2 rmind * All rights reserved.
6 1.2.4.2 rmind *
7 1.2.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 rmind * by Taylor R. Campbell.
9 1.2.4.2 rmind *
10 1.2.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 rmind * modification, are permitted provided that the following conditions
12 1.2.4.2 rmind * are met:
13 1.2.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.2.4.2 rmind *
19 1.2.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.2.4.2 rmind */
31 1.2.4.2 rmind
32 1.2.4.2 rmind #include <sys/cdefs.h>
33 1.2.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: drm_scatter.c,v 1.2.4.2 2014/05/18 17:46:00 rmind Exp $");
34 1.2.4.2 rmind
35 1.2.4.2 rmind #include <sys/types.h>
36 1.2.4.2 rmind #include <sys/bus.h>
37 1.2.4.2 rmind #include <sys/errno.h>
38 1.2.4.2 rmind #include <sys/systm.h>
39 1.2.4.2 rmind
40 1.2.4.2 rmind #include <linux/slab.h>
41 1.2.4.2 rmind
42 1.2.4.2 rmind #include <drm/drmP.h>
43 1.2.4.2 rmind
44 1.2.4.2 rmind static int drm_sg_alloc_mem(struct drm_device *, size_t,
45 1.2.4.2 rmind struct drm_sg_mem **);
46 1.2.4.2 rmind
47 1.2.4.2 rmind int
48 1.2.4.2 rmind drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
49 1.2.4.2 rmind struct drm_file *file __unused)
50 1.2.4.2 rmind {
51 1.2.4.2 rmind struct drm_scatter_gather *const request = data;
52 1.2.4.2 rmind struct drm_sg_mem *sg;
53 1.2.4.2 rmind int error;
54 1.2.4.2 rmind
55 1.2.4.2 rmind /*
56 1.2.4.2 rmind * XXX Should not hold this mutex during drm_sg_alloc. For
57 1.2.4.2 rmind * now, we'll just use non-blocking allocations.
58 1.2.4.2 rmind */
59 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
60 1.2.4.2 rmind
61 1.2.4.2 rmind /*
62 1.2.4.2 rmind * Sanity-check the inputs.
63 1.2.4.2 rmind */
64 1.2.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_SG))
65 1.2.4.2 rmind return -ENODEV;
66 1.2.4.2 rmind if (dev->sg != NULL)
67 1.2.4.2 rmind return -EBUSY;
68 1.2.4.2 rmind if (request->size > 0xffffffffUL)
69 1.2.4.2 rmind return -ENOMEM;
70 1.2.4.2 rmind
71 1.2.4.2 rmind /*
72 1.2.4.2 rmind * Do the allocation.
73 1.2.4.2 rmind *
74 1.2.4.2 rmind * XXX Would it be safe to drop drm_global_mutex here to avoid
75 1.2.4.2 rmind * holding it during allocation?
76 1.2.4.2 rmind */
77 1.2.4.2 rmind error = drm_sg_alloc_mem(dev, request->size, &sg);
78 1.2.4.2 rmind if (error)
79 1.2.4.2 rmind return error;
80 1.2.4.2 rmind
81 1.2.4.2 rmind /* Set it up to be the device's scatter-gather memory. */
82 1.2.4.2 rmind dev->sg = sg;
83 1.2.4.2 rmind
84 1.2.4.2 rmind /* Success! */
85 1.2.4.2 rmind request->handle = sg->handle;
86 1.2.4.2 rmind return 0;
87 1.2.4.2 rmind }
88 1.2.4.2 rmind
89 1.2.4.2 rmind int
90 1.2.4.2 rmind drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file)
91 1.2.4.2 rmind {
92 1.2.4.2 rmind struct drm_scatter_gather *const request = data;
93 1.2.4.2 rmind struct drm_sg_mem *sg;
94 1.2.4.2 rmind
95 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
96 1.2.4.2 rmind
97 1.2.4.2 rmind /*
98 1.2.4.2 rmind * Sanity-check the inputs.
99 1.2.4.2 rmind */
100 1.2.4.2 rmind if (!drm_core_check_feature(dev, DRIVER_SG))
101 1.2.4.2 rmind return -ENODEV;
102 1.2.4.2 rmind
103 1.2.4.2 rmind sg = dev->sg;
104 1.2.4.2 rmind if (sg == NULL)
105 1.2.4.2 rmind return -ENXIO;
106 1.2.4.2 rmind if (sg->handle != request->handle)
107 1.2.4.2 rmind return -EINVAL;
108 1.2.4.2 rmind
109 1.2.4.2 rmind /* Remove dev->sg. */
110 1.2.4.2 rmind dev->sg = NULL;
111 1.2.4.2 rmind
112 1.2.4.2 rmind /* Success! */
113 1.2.4.2 rmind drm_sg_cleanup(sg);
114 1.2.4.2 rmind return 0;
115 1.2.4.2 rmind }
116 1.2.4.2 rmind
117 1.2.4.2 rmind static int
118 1.2.4.2 rmind drm_sg_alloc_mem(struct drm_device *dev, size_t size, struct drm_sg_mem **sgp)
119 1.2.4.2 rmind {
120 1.2.4.2 rmind int nsegs;
121 1.2.4.2 rmind int error;
122 1.2.4.2 rmind
123 1.2.4.2 rmind KASSERT(drm_core_check_feature(dev, DRIVER_SG));
124 1.2.4.2 rmind
125 1.2.4.2 rmind KASSERT(size <= (size_t)0xffffffffUL); /* XXX 32-bit sizes only? */
126 1.2.4.2 rmind const size_t nbytes = PAGE_ALIGN(size);
127 1.2.4.2 rmind const size_t npages = nbytes >> PAGE_SHIFT;
128 1.2.4.2 rmind KASSERT(npages <= (size_t)INT_MAX);
129 1.2.4.2 rmind
130 1.2.4.2 rmind /*
131 1.2.4.2 rmind * Allocate a drm_sg_mem record.
132 1.2.4.2 rmind */
133 1.2.4.2 rmind struct drm_sg_mem *const sg =
134 1.2.4.2 rmind kmem_zalloc(offsetof(struct drm_sg_mem, sg_segs[npages]),
135 1.2.4.2 rmind KM_NOSLEEP);
136 1.2.4.2 rmind if (sg == NULL)
137 1.2.4.2 rmind return -ENOMEM;
138 1.2.4.2 rmind sg->sg_tag = dev->dmat;
139 1.2.4.2 rmind sg->sg_nsegs_max = (unsigned int)npages;
140 1.2.4.2 rmind
141 1.2.4.2 rmind /*
142 1.2.4.2 rmind * Allocate the requested amount of DMA-safe memory.
143 1.2.4.2 rmind */
144 1.2.4.2 rmind KASSERT(sg->sg_nsegs_max <= (unsigned int)INT_MAX);
145 1.2.4.2 rmind /* XXX errno NetBSD->Linux */
146 1.2.4.2 rmind error = -bus_dmamem_alloc(sg->sg_tag, nbytes, PAGE_SIZE, 0,
147 1.2.4.2 rmind sg->sg_segs, (int)sg->sg_nsegs_max, &nsegs, BUS_DMA_NOWAIT);
148 1.2.4.2 rmind if (error)
149 1.2.4.2 rmind goto fail0;
150 1.2.4.2 rmind KASSERT(0 <= nsegs);
151 1.2.4.2 rmind sg->sg_nsegs = (unsigned int)nsegs;
152 1.2.4.2 rmind
153 1.2.4.2 rmind /*
154 1.2.4.2 rmind * XXX Old drm passed DRM_BUS_NOWAIT below but BUS_DMA_WAITOK
155 1.2.4.2 rmind * above. WTF?
156 1.2.4.2 rmind */
157 1.2.4.2 rmind
158 1.2.4.2 rmind /*
159 1.2.4.2 rmind * Map the DMA-safe memory into kernel virtual address space.
160 1.2.4.2 rmind */
161 1.2.4.2 rmind /* XXX errno NetBSD->Linux */
162 1.2.4.2 rmind error = -bus_dmamem_map(sg->sg_tag, sg->sg_segs, nsegs, nbytes,
163 1.2.4.2 rmind &sg->virtual,
164 1.2.4.2 rmind (BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE));
165 1.2.4.2 rmind if (error)
166 1.2.4.2 rmind goto fail1;
167 1.2.4.2 rmind sg->sg_size = nbytes;
168 1.2.4.2 rmind
169 1.2.4.2 rmind /*
170 1.2.4.2 rmind * Create a map for DMA transfers.
171 1.2.4.2 rmind */
172 1.2.4.2 rmind /* XXX errno NetBSD->Linux */
173 1.2.4.2 rmind error = -bus_dmamap_create(sg->sg_tag, nbytes, nsegs, nbytes, 0,
174 1.2.4.2 rmind BUS_DMA_NOWAIT, &sg->sg_map);
175 1.2.4.2 rmind if (error)
176 1.2.4.2 rmind goto fail2;
177 1.2.4.2 rmind
178 1.2.4.2 rmind /*
179 1.2.4.2 rmind * Load the kva buffer into the map for DMA transfers.
180 1.2.4.2 rmind */
181 1.2.4.2 rmind /* XXX errno NetBSD->Linux */
182 1.2.4.2 rmind error = -bus_dmamap_load(sg->sg_tag, sg->sg_map, sg->virtual, nbytes,
183 1.2.4.2 rmind NULL, (BUS_DMA_NOWAIT | BUS_DMA_NOCACHE));
184 1.2.4.2 rmind if (error)
185 1.2.4.2 rmind goto fail3;
186 1.2.4.2 rmind
187 1.2.4.2 rmind /*
188 1.2.4.2 rmind * Use the kernel virtual address as the userland handle.
189 1.2.4.2 rmind *
190 1.2.4.2 rmind * XXX This leaks some information about kernel virtual
191 1.2.4.2 rmind * addresses to userland; should we use an idr instead?
192 1.2.4.2 rmind */
193 1.2.4.2 rmind sg->handle = (unsigned long)(uintptr_t)sg->virtual;
194 1.2.4.2 rmind KASSERT(sg->virtual == (void *)(uintptr_t)sg->handle);
195 1.2.4.2 rmind
196 1.2.4.2 rmind /* Success! */
197 1.2.4.2 rmind *sgp = sg;
198 1.2.4.2 rmind return 0;
199 1.2.4.2 rmind
200 1.2.4.2 rmind fail3: bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
201 1.2.4.2 rmind fail2: bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
202 1.2.4.2 rmind fail1: KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
203 1.2.4.2 rmind bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
204 1.2.4.2 rmind fail0: sg->sg_tag = NULL; /* XXX paranoia */
205 1.2.4.2 rmind kfree(sg);
206 1.2.4.2 rmind return error;
207 1.2.4.2 rmind }
208 1.2.4.2 rmind
209 1.2.4.2 rmind void
210 1.2.4.2 rmind drm_sg_cleanup(struct drm_sg_mem *sg)
211 1.2.4.2 rmind {
212 1.2.4.2 rmind
213 1.2.4.2 rmind bus_dmamap_unload(sg->sg_tag, sg->sg_map);
214 1.2.4.2 rmind bus_dmamap_destroy(sg->sg_tag, sg->sg_map);
215 1.2.4.2 rmind bus_dmamem_unmap(sg->sg_tag, sg->virtual, sg->sg_size);
216 1.2.4.2 rmind KASSERT(sg->sg_nsegs <= (unsigned int)INT_MAX);
217 1.2.4.2 rmind bus_dmamem_free(sg->sg_tag, sg->sg_segs, (int)sg->sg_nsegs);
218 1.2.4.2 rmind sg->sg_tag = NULL; /* XXX paranoia */
219 1.2.4.2 rmind kmem_free(sg, offsetof(struct drm_sg_mem, sg_segs[sg->sg_nsegs_max]));
220 1.2.4.2 rmind }
221