drm_vm.c revision 1.2 1 1.2 riastrad /* $NetBSD: drm_vm.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $ */
2 1.2 riastrad
3 1.1 riastrad /**
4 1.1 riastrad * \file drm_vm.c
5 1.1 riastrad * Memory mapping for DRM
6 1.1 riastrad *
7 1.1 riastrad * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
8 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com>
9 1.1 riastrad */
10 1.1 riastrad
11 1.1 riastrad /*
12 1.1 riastrad * Created: Mon Jan 4 08:58:31 1999 by faith (at) valinux.com
13 1.1 riastrad *
14 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
15 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
16 1.1 riastrad * All Rights Reserved.
17 1.1 riastrad *
18 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
19 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
20 1.1 riastrad * to deal in the Software without restriction, including without limitation
21 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
23 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
24 1.1 riastrad *
25 1.1 riastrad * The above copyright notice and this permission notice (including the next
26 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
27 1.1 riastrad * Software.
28 1.1 riastrad *
29 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
36 1.1 riastrad */
37 1.1 riastrad
38 1.2 riastrad #include <sys/cdefs.h>
39 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
40 1.2 riastrad
41 1.1 riastrad #include <drm/drmP.h>
42 1.1 riastrad #include <linux/export.h>
43 1.2 riastrad #include <linux/seq_file.h>
44 1.1 riastrad #if defined(__ia64__)
45 1.1 riastrad #include <linux/efi.h>
46 1.1 riastrad #include <linux/slab.h>
47 1.1 riastrad #endif
48 1.2 riastrad #include <asm/pgtable.h>
49 1.2 riastrad #include "drm_internal.h"
50 1.2 riastrad #include "drm_legacy.h"
51 1.2 riastrad
52 1.2 riastrad struct drm_vma_entry {
53 1.2 riastrad struct list_head head;
54 1.2 riastrad struct vm_area_struct *vma;
55 1.2 riastrad pid_t pid;
56 1.2 riastrad };
57 1.1 riastrad
58 1.1 riastrad static void drm_vm_open(struct vm_area_struct *vma);
59 1.1 riastrad static void drm_vm_close(struct vm_area_struct *vma);
60 1.1 riastrad
61 1.2 riastrad static pgprot_t drm_io_prot(struct drm_local_map *map,
62 1.2 riastrad struct vm_area_struct *vma)
63 1.1 riastrad {
64 1.1 riastrad pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
65 1.1 riastrad
66 1.2 riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
67 1.2 riastrad if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
68 1.2 riastrad tmp = pgprot_noncached(tmp);
69 1.2 riastrad else
70 1.2 riastrad tmp = pgprot_writecombine(tmp);
71 1.1 riastrad #elif defined(__ia64__)
72 1.1 riastrad if (efi_range_is_wc(vma->vm_start, vma->vm_end -
73 1.1 riastrad vma->vm_start))
74 1.1 riastrad tmp = pgprot_writecombine(tmp);
75 1.1 riastrad else
76 1.1 riastrad tmp = pgprot_noncached(tmp);
77 1.1 riastrad #elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
78 1.1 riastrad tmp = pgprot_noncached(tmp);
79 1.1 riastrad #endif
80 1.1 riastrad return tmp;
81 1.1 riastrad }
82 1.1 riastrad
83 1.1 riastrad static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
84 1.1 riastrad {
85 1.1 riastrad pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
86 1.1 riastrad
87 1.1 riastrad #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
88 1.1 riastrad tmp |= _PAGE_NO_CACHE;
89 1.1 riastrad #endif
90 1.1 riastrad return tmp;
91 1.1 riastrad }
92 1.1 riastrad
93 1.1 riastrad /**
94 1.1 riastrad * \c fault method for AGP virtual memory.
95 1.1 riastrad *
96 1.1 riastrad * \param vma virtual memory area.
97 1.1 riastrad * \param address access address.
98 1.1 riastrad * \return pointer to the page structure.
99 1.1 riastrad *
100 1.1 riastrad * Find the right map and if it's AGP memory find the real physical page to
101 1.1 riastrad * map, get the page, increment the use count and return it.
102 1.1 riastrad */
103 1.2 riastrad #if IS_ENABLED(CONFIG_AGP)
104 1.1 riastrad static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
105 1.1 riastrad {
106 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
107 1.1 riastrad struct drm_device *dev = priv->minor->dev;
108 1.1 riastrad struct drm_local_map *map = NULL;
109 1.1 riastrad struct drm_map_list *r_list;
110 1.1 riastrad struct drm_hash_item *hash;
111 1.1 riastrad
112 1.1 riastrad /*
113 1.1 riastrad * Find the right map
114 1.1 riastrad */
115 1.2 riastrad if (!dev->agp)
116 1.1 riastrad goto vm_fault_error;
117 1.1 riastrad
118 1.1 riastrad if (!dev->agp || !dev->agp->cant_use_aperture)
119 1.1 riastrad goto vm_fault_error;
120 1.1 riastrad
121 1.1 riastrad if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
122 1.1 riastrad goto vm_fault_error;
123 1.1 riastrad
124 1.1 riastrad r_list = drm_hash_entry(hash, struct drm_map_list, hash);
125 1.1 riastrad map = r_list->map;
126 1.1 riastrad
127 1.1 riastrad if (map && map->type == _DRM_AGP) {
128 1.1 riastrad /*
129 1.1 riastrad * Using vm_pgoff as a selector forces us to use this unusual
130 1.1 riastrad * addressing scheme.
131 1.1 riastrad */
132 1.1 riastrad resource_size_t offset = (unsigned long)vmf->virtual_address -
133 1.1 riastrad vma->vm_start;
134 1.1 riastrad resource_size_t baddr = map->offset + offset;
135 1.1 riastrad struct drm_agp_mem *agpmem;
136 1.1 riastrad struct page *page;
137 1.1 riastrad
138 1.1 riastrad #ifdef __alpha__
139 1.1 riastrad /*
140 1.1 riastrad * Adjust to a bus-relative address
141 1.1 riastrad */
142 1.1 riastrad baddr -= dev->hose->mem_space->start;
143 1.1 riastrad #endif
144 1.1 riastrad
145 1.1 riastrad /*
146 1.1 riastrad * It's AGP memory - find the real physical page to map
147 1.1 riastrad */
148 1.1 riastrad list_for_each_entry(agpmem, &dev->agp->memory, head) {
149 1.1 riastrad if (agpmem->bound <= baddr &&
150 1.1 riastrad agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
151 1.1 riastrad break;
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad if (&agpmem->head == &dev->agp->memory)
155 1.1 riastrad goto vm_fault_error;
156 1.1 riastrad
157 1.1 riastrad /*
158 1.1 riastrad * Get the page, inc the use count, and return it
159 1.1 riastrad */
160 1.1 riastrad offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
161 1.1 riastrad page = agpmem->memory->pages[offset];
162 1.1 riastrad get_page(page);
163 1.1 riastrad vmf->page = page;
164 1.1 riastrad
165 1.1 riastrad DRM_DEBUG
166 1.1 riastrad ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
167 1.1 riastrad (unsigned long long)baddr,
168 1.1 riastrad agpmem->memory->pages[offset],
169 1.1 riastrad (unsigned long long)offset,
170 1.1 riastrad page_count(page));
171 1.1 riastrad return 0;
172 1.1 riastrad }
173 1.1 riastrad vm_fault_error:
174 1.1 riastrad return VM_FAULT_SIGBUS; /* Disallow mremap */
175 1.1 riastrad }
176 1.2 riastrad #else
177 1.1 riastrad static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
178 1.1 riastrad {
179 1.1 riastrad return VM_FAULT_SIGBUS;
180 1.1 riastrad }
181 1.2 riastrad #endif
182 1.1 riastrad
183 1.1 riastrad /**
184 1.1 riastrad * \c nopage method for shared virtual memory.
185 1.1 riastrad *
186 1.1 riastrad * \param vma virtual memory area.
187 1.1 riastrad * \param address access address.
188 1.1 riastrad * \return pointer to the page structure.
189 1.1 riastrad *
190 1.1 riastrad * Get the mapping, find the real physical page to map, get the page, and
191 1.1 riastrad * return it.
192 1.1 riastrad */
193 1.1 riastrad static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
194 1.1 riastrad {
195 1.1 riastrad struct drm_local_map *map = vma->vm_private_data;
196 1.1 riastrad unsigned long offset;
197 1.1 riastrad unsigned long i;
198 1.1 riastrad struct page *page;
199 1.1 riastrad
200 1.1 riastrad if (!map)
201 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */
202 1.1 riastrad
203 1.1 riastrad offset = (unsigned long)vmf->virtual_address - vma->vm_start;
204 1.1 riastrad i = (unsigned long)map->handle + offset;
205 1.1 riastrad page = vmalloc_to_page((void *)i);
206 1.1 riastrad if (!page)
207 1.1 riastrad return VM_FAULT_SIGBUS;
208 1.1 riastrad get_page(page);
209 1.1 riastrad vmf->page = page;
210 1.1 riastrad
211 1.1 riastrad DRM_DEBUG("shm_fault 0x%lx\n", offset);
212 1.1 riastrad return 0;
213 1.1 riastrad }
214 1.1 riastrad
215 1.1 riastrad /**
216 1.1 riastrad * \c close method for shared virtual memory.
217 1.1 riastrad *
218 1.1 riastrad * \param vma virtual memory area.
219 1.1 riastrad *
220 1.1 riastrad * Deletes map information if we are the last
221 1.1 riastrad * person to close a mapping and it's not in the global maplist.
222 1.1 riastrad */
223 1.1 riastrad static void drm_vm_shm_close(struct vm_area_struct *vma)
224 1.1 riastrad {
225 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
226 1.1 riastrad struct drm_device *dev = priv->minor->dev;
227 1.1 riastrad struct drm_vma_entry *pt, *temp;
228 1.1 riastrad struct drm_local_map *map;
229 1.1 riastrad struct drm_map_list *r_list;
230 1.1 riastrad int found_maps = 0;
231 1.1 riastrad
232 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n",
233 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start);
234 1.1 riastrad
235 1.1 riastrad map = vma->vm_private_data;
236 1.1 riastrad
237 1.1 riastrad mutex_lock(&dev->struct_mutex);
238 1.1 riastrad list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
239 1.1 riastrad if (pt->vma->vm_private_data == map)
240 1.1 riastrad found_maps++;
241 1.1 riastrad if (pt->vma == vma) {
242 1.1 riastrad list_del(&pt->head);
243 1.1 riastrad kfree(pt);
244 1.1 riastrad }
245 1.1 riastrad }
246 1.1 riastrad
247 1.1 riastrad /* We were the only map that was found */
248 1.1 riastrad if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
249 1.1 riastrad /* Check to see if we are in the maplist, if we are not, then
250 1.1 riastrad * we delete this mappings information.
251 1.1 riastrad */
252 1.1 riastrad found_maps = 0;
253 1.1 riastrad list_for_each_entry(r_list, &dev->maplist, head) {
254 1.1 riastrad if (r_list->map == map)
255 1.1 riastrad found_maps++;
256 1.1 riastrad }
257 1.1 riastrad
258 1.1 riastrad if (!found_maps) {
259 1.1 riastrad drm_dma_handle_t dmah;
260 1.1 riastrad
261 1.1 riastrad switch (map->type) {
262 1.1 riastrad case _DRM_REGISTERS:
263 1.1 riastrad case _DRM_FRAME_BUFFER:
264 1.2 riastrad arch_phys_wc_del(map->mtrr);
265 1.1 riastrad iounmap(map->handle);
266 1.1 riastrad break;
267 1.1 riastrad case _DRM_SHM:
268 1.1 riastrad vfree(map->handle);
269 1.1 riastrad break;
270 1.1 riastrad case _DRM_AGP:
271 1.1 riastrad case _DRM_SCATTER_GATHER:
272 1.1 riastrad break;
273 1.1 riastrad case _DRM_CONSISTENT:
274 1.1 riastrad dmah.vaddr = map->handle;
275 1.1 riastrad dmah.busaddr = map->offset;
276 1.1 riastrad dmah.size = map->size;
277 1.2 riastrad __drm_legacy_pci_free(dev, &dmah);
278 1.1 riastrad break;
279 1.1 riastrad }
280 1.1 riastrad kfree(map);
281 1.1 riastrad }
282 1.1 riastrad }
283 1.1 riastrad mutex_unlock(&dev->struct_mutex);
284 1.1 riastrad }
285 1.1 riastrad
286 1.1 riastrad /**
287 1.1 riastrad * \c fault method for DMA virtual memory.
288 1.1 riastrad *
289 1.1 riastrad * \param vma virtual memory area.
290 1.1 riastrad * \param address access address.
291 1.1 riastrad * \return pointer to the page structure.
292 1.1 riastrad *
293 1.1 riastrad * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
294 1.1 riastrad */
295 1.1 riastrad static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
296 1.1 riastrad {
297 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
298 1.1 riastrad struct drm_device *dev = priv->minor->dev;
299 1.1 riastrad struct drm_device_dma *dma = dev->dma;
300 1.1 riastrad unsigned long offset;
301 1.1 riastrad unsigned long page_nr;
302 1.1 riastrad struct page *page;
303 1.1 riastrad
304 1.1 riastrad if (!dma)
305 1.1 riastrad return VM_FAULT_SIGBUS; /* Error */
306 1.1 riastrad if (!dma->pagelist)
307 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */
308 1.1 riastrad
309 1.1 riastrad offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
310 1.1 riastrad page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
311 1.2 riastrad page = virt_to_page((void *)dma->pagelist[page_nr]);
312 1.1 riastrad
313 1.1 riastrad get_page(page);
314 1.1 riastrad vmf->page = page;
315 1.1 riastrad
316 1.1 riastrad DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
317 1.1 riastrad return 0;
318 1.1 riastrad }
319 1.1 riastrad
320 1.1 riastrad /**
321 1.1 riastrad * \c fault method for scatter-gather virtual memory.
322 1.1 riastrad *
323 1.1 riastrad * \param vma virtual memory area.
324 1.1 riastrad * \param address access address.
325 1.1 riastrad * \return pointer to the page structure.
326 1.1 riastrad *
327 1.1 riastrad * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
328 1.1 riastrad */
329 1.1 riastrad static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
330 1.1 riastrad {
331 1.1 riastrad struct drm_local_map *map = vma->vm_private_data;
332 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
333 1.1 riastrad struct drm_device *dev = priv->minor->dev;
334 1.1 riastrad struct drm_sg_mem *entry = dev->sg;
335 1.1 riastrad unsigned long offset;
336 1.1 riastrad unsigned long map_offset;
337 1.1 riastrad unsigned long page_offset;
338 1.1 riastrad struct page *page;
339 1.1 riastrad
340 1.1 riastrad if (!entry)
341 1.1 riastrad return VM_FAULT_SIGBUS; /* Error */
342 1.1 riastrad if (!entry->pagelist)
343 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */
344 1.1 riastrad
345 1.1 riastrad offset = (unsigned long)vmf->virtual_address - vma->vm_start;
346 1.1 riastrad map_offset = map->offset - (unsigned long)dev->sg->virtual;
347 1.1 riastrad page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
348 1.1 riastrad page = entry->pagelist[page_offset];
349 1.1 riastrad get_page(page);
350 1.1 riastrad vmf->page = page;
351 1.1 riastrad
352 1.1 riastrad return 0;
353 1.1 riastrad }
354 1.1 riastrad
355 1.1 riastrad static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
356 1.1 riastrad {
357 1.1 riastrad return drm_do_vm_fault(vma, vmf);
358 1.1 riastrad }
359 1.1 riastrad
360 1.1 riastrad static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
361 1.1 riastrad {
362 1.1 riastrad return drm_do_vm_shm_fault(vma, vmf);
363 1.1 riastrad }
364 1.1 riastrad
365 1.1 riastrad static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
366 1.1 riastrad {
367 1.1 riastrad return drm_do_vm_dma_fault(vma, vmf);
368 1.1 riastrad }
369 1.1 riastrad
370 1.1 riastrad static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
371 1.1 riastrad {
372 1.1 riastrad return drm_do_vm_sg_fault(vma, vmf);
373 1.1 riastrad }
374 1.1 riastrad
375 1.1 riastrad /** AGP virtual memory operations */
376 1.1 riastrad static const struct vm_operations_struct drm_vm_ops = {
377 1.1 riastrad .fault = drm_vm_fault,
378 1.1 riastrad .open = drm_vm_open,
379 1.1 riastrad .close = drm_vm_close,
380 1.1 riastrad };
381 1.1 riastrad
382 1.1 riastrad /** Shared virtual memory operations */
383 1.1 riastrad static const struct vm_operations_struct drm_vm_shm_ops = {
384 1.1 riastrad .fault = drm_vm_shm_fault,
385 1.1 riastrad .open = drm_vm_open,
386 1.1 riastrad .close = drm_vm_shm_close,
387 1.1 riastrad };
388 1.1 riastrad
389 1.1 riastrad /** DMA virtual memory operations */
390 1.1 riastrad static const struct vm_operations_struct drm_vm_dma_ops = {
391 1.1 riastrad .fault = drm_vm_dma_fault,
392 1.1 riastrad .open = drm_vm_open,
393 1.1 riastrad .close = drm_vm_close,
394 1.1 riastrad };
395 1.1 riastrad
396 1.1 riastrad /** Scatter-gather virtual memory operations */
397 1.1 riastrad static const struct vm_operations_struct drm_vm_sg_ops = {
398 1.1 riastrad .fault = drm_vm_sg_fault,
399 1.1 riastrad .open = drm_vm_open,
400 1.1 riastrad .close = drm_vm_close,
401 1.1 riastrad };
402 1.1 riastrad
403 1.1 riastrad /**
404 1.1 riastrad * \c open method for shared virtual memory.
405 1.1 riastrad *
406 1.1 riastrad * \param vma virtual memory area.
407 1.1 riastrad *
408 1.1 riastrad * Create a new drm_vma_entry structure as the \p vma private data entry and
409 1.1 riastrad * add it to drm_device::vmalist.
410 1.1 riastrad */
411 1.1 riastrad void drm_vm_open_locked(struct drm_device *dev,
412 1.1 riastrad struct vm_area_struct *vma)
413 1.1 riastrad {
414 1.1 riastrad struct drm_vma_entry *vma_entry;
415 1.1 riastrad
416 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n",
417 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start);
418 1.1 riastrad
419 1.1 riastrad vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
420 1.1 riastrad if (vma_entry) {
421 1.1 riastrad vma_entry->vma = vma;
422 1.1 riastrad vma_entry->pid = current->pid;
423 1.1 riastrad list_add(&vma_entry->head, &dev->vmalist);
424 1.1 riastrad }
425 1.1 riastrad }
426 1.1 riastrad
427 1.1 riastrad static void drm_vm_open(struct vm_area_struct *vma)
428 1.1 riastrad {
429 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
430 1.1 riastrad struct drm_device *dev = priv->minor->dev;
431 1.1 riastrad
432 1.1 riastrad mutex_lock(&dev->struct_mutex);
433 1.1 riastrad drm_vm_open_locked(dev, vma);
434 1.1 riastrad mutex_unlock(&dev->struct_mutex);
435 1.1 riastrad }
436 1.1 riastrad
437 1.1 riastrad void drm_vm_close_locked(struct drm_device *dev,
438 1.1 riastrad struct vm_area_struct *vma)
439 1.1 riastrad {
440 1.1 riastrad struct drm_vma_entry *pt, *temp;
441 1.1 riastrad
442 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n",
443 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start);
444 1.1 riastrad
445 1.1 riastrad list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
446 1.1 riastrad if (pt->vma == vma) {
447 1.1 riastrad list_del(&pt->head);
448 1.1 riastrad kfree(pt);
449 1.1 riastrad break;
450 1.1 riastrad }
451 1.1 riastrad }
452 1.1 riastrad }
453 1.1 riastrad
454 1.1 riastrad /**
455 1.1 riastrad * \c close method for all virtual memory types.
456 1.1 riastrad *
457 1.1 riastrad * \param vma virtual memory area.
458 1.1 riastrad *
459 1.1 riastrad * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
460 1.1 riastrad * free it.
461 1.1 riastrad */
462 1.1 riastrad static void drm_vm_close(struct vm_area_struct *vma)
463 1.1 riastrad {
464 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data;
465 1.1 riastrad struct drm_device *dev = priv->minor->dev;
466 1.1 riastrad
467 1.1 riastrad mutex_lock(&dev->struct_mutex);
468 1.1 riastrad drm_vm_close_locked(dev, vma);
469 1.1 riastrad mutex_unlock(&dev->struct_mutex);
470 1.1 riastrad }
471 1.1 riastrad
472 1.1 riastrad /**
473 1.1 riastrad * mmap DMA memory.
474 1.1 riastrad *
475 1.1 riastrad * \param file_priv DRM file private.
476 1.1 riastrad * \param vma virtual memory area.
477 1.1 riastrad * \return zero on success or a negative number on failure.
478 1.1 riastrad *
479 1.1 riastrad * Sets the virtual memory area operations structure to vm_dma_ops, the file
480 1.1 riastrad * pointer, and calls vm_open().
481 1.1 riastrad */
482 1.1 riastrad static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
483 1.1 riastrad {
484 1.1 riastrad struct drm_file *priv = filp->private_data;
485 1.1 riastrad struct drm_device *dev;
486 1.1 riastrad struct drm_device_dma *dma;
487 1.1 riastrad unsigned long length = vma->vm_end - vma->vm_start;
488 1.1 riastrad
489 1.1 riastrad dev = priv->minor->dev;
490 1.1 riastrad dma = dev->dma;
491 1.1 riastrad DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
492 1.1 riastrad vma->vm_start, vma->vm_end, vma->vm_pgoff);
493 1.1 riastrad
494 1.1 riastrad /* Length must match exact page count */
495 1.1 riastrad if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
496 1.1 riastrad return -EINVAL;
497 1.1 riastrad }
498 1.1 riastrad
499 1.1 riastrad if (!capable(CAP_SYS_ADMIN) &&
500 1.1 riastrad (dma->flags & _DRM_DMA_USE_PCI_RO)) {
501 1.1 riastrad vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
502 1.1 riastrad #if defined(__i386__) || defined(__x86_64__)
503 1.1 riastrad pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
504 1.1 riastrad #else
505 1.1 riastrad /* Ye gads this is ugly. With more thought
506 1.1 riastrad we could move this up higher and use
507 1.1 riastrad `protection_map' instead. */
508 1.1 riastrad vma->vm_page_prot =
509 1.1 riastrad __pgprot(pte_val
510 1.1 riastrad (pte_wrprotect
511 1.1 riastrad (__pte(pgprot_val(vma->vm_page_prot)))));
512 1.1 riastrad #endif
513 1.1 riastrad }
514 1.1 riastrad
515 1.1 riastrad vma->vm_ops = &drm_vm_dma_ops;
516 1.1 riastrad
517 1.1 riastrad vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
518 1.1 riastrad
519 1.1 riastrad drm_vm_open_locked(dev, vma);
520 1.1 riastrad return 0;
521 1.1 riastrad }
522 1.1 riastrad
523 1.1 riastrad static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
524 1.1 riastrad {
525 1.1 riastrad #ifdef __alpha__
526 1.1 riastrad return dev->hose->dense_mem_base;
527 1.1 riastrad #else
528 1.1 riastrad return 0;
529 1.1 riastrad #endif
530 1.1 riastrad }
531 1.1 riastrad
532 1.1 riastrad /**
533 1.1 riastrad * mmap DMA memory.
534 1.1 riastrad *
535 1.1 riastrad * \param file_priv DRM file private.
536 1.1 riastrad * \param vma virtual memory area.
537 1.1 riastrad * \return zero on success or a negative number on failure.
538 1.1 riastrad *
539 1.1 riastrad * If the virtual memory area has no offset associated with it then it's a DMA
540 1.1 riastrad * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
541 1.1 riastrad * checks that the restricted flag is not set, sets the virtual memory operations
542 1.1 riastrad * according to the mapping type and remaps the pages. Finally sets the file
543 1.1 riastrad * pointer and calls vm_open().
544 1.1 riastrad */
545 1.2 riastrad static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
546 1.1 riastrad {
547 1.1 riastrad struct drm_file *priv = filp->private_data;
548 1.1 riastrad struct drm_device *dev = priv->minor->dev;
549 1.1 riastrad struct drm_local_map *map = NULL;
550 1.1 riastrad resource_size_t offset = 0;
551 1.1 riastrad struct drm_hash_item *hash;
552 1.1 riastrad
553 1.1 riastrad DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
554 1.1 riastrad vma->vm_start, vma->vm_end, vma->vm_pgoff);
555 1.1 riastrad
556 1.1 riastrad if (!priv->authenticated)
557 1.1 riastrad return -EACCES;
558 1.1 riastrad
559 1.1 riastrad /* We check for "dma". On Apple's UniNorth, it's valid to have
560 1.1 riastrad * the AGP mapped at physical address 0
561 1.1 riastrad * --BenH.
562 1.1 riastrad */
563 1.1 riastrad if (!vma->vm_pgoff
564 1.2 riastrad #if IS_ENABLED(CONFIG_AGP)
565 1.1 riastrad && (!dev->agp
566 1.1 riastrad || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
567 1.1 riastrad #endif
568 1.1 riastrad )
569 1.1 riastrad return drm_mmap_dma(filp, vma);
570 1.1 riastrad
571 1.1 riastrad if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
572 1.1 riastrad DRM_ERROR("Could not find map\n");
573 1.1 riastrad return -EINVAL;
574 1.1 riastrad }
575 1.1 riastrad
576 1.1 riastrad map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
577 1.1 riastrad if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
578 1.1 riastrad return -EPERM;
579 1.1 riastrad
580 1.1 riastrad /* Check for valid size. */
581 1.1 riastrad if (map->size < vma->vm_end - vma->vm_start)
582 1.1 riastrad return -EINVAL;
583 1.1 riastrad
584 1.1 riastrad if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
585 1.1 riastrad vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
586 1.1 riastrad #if defined(__i386__) || defined(__x86_64__)
587 1.1 riastrad pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
588 1.1 riastrad #else
589 1.1 riastrad /* Ye gads this is ugly. With more thought
590 1.1 riastrad we could move this up higher and use
591 1.1 riastrad `protection_map' instead. */
592 1.1 riastrad vma->vm_page_prot =
593 1.1 riastrad __pgprot(pte_val
594 1.1 riastrad (pte_wrprotect
595 1.1 riastrad (__pte(pgprot_val(vma->vm_page_prot)))));
596 1.1 riastrad #endif
597 1.1 riastrad }
598 1.1 riastrad
599 1.1 riastrad switch (map->type) {
600 1.1 riastrad #if !defined(__arm__)
601 1.1 riastrad case _DRM_AGP:
602 1.2 riastrad if (dev->agp && dev->agp->cant_use_aperture) {
603 1.1 riastrad /*
604 1.1 riastrad * On some platforms we can't talk to bus dma address from the CPU, so for
605 1.1 riastrad * memory of type DRM_AGP, we'll deal with sorting out the real physical
606 1.1 riastrad * pages and mappings in fault()
607 1.1 riastrad */
608 1.1 riastrad #if defined(__powerpc__)
609 1.1 riastrad pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
610 1.1 riastrad #endif
611 1.1 riastrad vma->vm_ops = &drm_vm_ops;
612 1.1 riastrad break;
613 1.1 riastrad }
614 1.1 riastrad /* fall through to _DRM_FRAME_BUFFER... */
615 1.1 riastrad #endif
616 1.1 riastrad case _DRM_FRAME_BUFFER:
617 1.1 riastrad case _DRM_REGISTERS:
618 1.1 riastrad offset = drm_core_get_reg_ofs(dev);
619 1.2 riastrad vma->vm_page_prot = drm_io_prot(map, vma);
620 1.1 riastrad if (io_remap_pfn_range(vma, vma->vm_start,
621 1.1 riastrad (map->offset + offset) >> PAGE_SHIFT,
622 1.1 riastrad vma->vm_end - vma->vm_start,
623 1.1 riastrad vma->vm_page_prot))
624 1.1 riastrad return -EAGAIN;
625 1.1 riastrad DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
626 1.1 riastrad " offset = 0x%llx\n",
627 1.1 riastrad map->type,
628 1.1 riastrad vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
629 1.1 riastrad
630 1.1 riastrad vma->vm_ops = &drm_vm_ops;
631 1.1 riastrad break;
632 1.1 riastrad case _DRM_CONSISTENT:
633 1.1 riastrad /* Consistent memory is really like shared memory. But
634 1.1 riastrad * it's allocated in a different way, so avoid fault */
635 1.1 riastrad if (remap_pfn_range(vma, vma->vm_start,
636 1.1 riastrad page_to_pfn(virt_to_page(map->handle)),
637 1.1 riastrad vma->vm_end - vma->vm_start, vma->vm_page_prot))
638 1.1 riastrad return -EAGAIN;
639 1.1 riastrad vma->vm_page_prot = drm_dma_prot(map->type, vma);
640 1.1 riastrad /* fall through to _DRM_SHM */
641 1.1 riastrad case _DRM_SHM:
642 1.1 riastrad vma->vm_ops = &drm_vm_shm_ops;
643 1.1 riastrad vma->vm_private_data = (void *)map;
644 1.1 riastrad break;
645 1.1 riastrad case _DRM_SCATTER_GATHER:
646 1.1 riastrad vma->vm_ops = &drm_vm_sg_ops;
647 1.1 riastrad vma->vm_private_data = (void *)map;
648 1.1 riastrad vma->vm_page_prot = drm_dma_prot(map->type, vma);
649 1.1 riastrad break;
650 1.1 riastrad default:
651 1.1 riastrad return -EINVAL; /* This should never happen. */
652 1.1 riastrad }
653 1.1 riastrad vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
654 1.1 riastrad
655 1.1 riastrad drm_vm_open_locked(dev, vma);
656 1.1 riastrad return 0;
657 1.1 riastrad }
658 1.1 riastrad
659 1.2 riastrad int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
660 1.1 riastrad {
661 1.1 riastrad struct drm_file *priv = filp->private_data;
662 1.1 riastrad struct drm_device *dev = priv->minor->dev;
663 1.1 riastrad int ret;
664 1.1 riastrad
665 1.1 riastrad if (drm_device_is_unplugged(dev))
666 1.1 riastrad return -ENODEV;
667 1.1 riastrad
668 1.1 riastrad mutex_lock(&dev->struct_mutex);
669 1.1 riastrad ret = drm_mmap_locked(filp, vma);
670 1.1 riastrad mutex_unlock(&dev->struct_mutex);
671 1.1 riastrad
672 1.1 riastrad return ret;
673 1.1 riastrad }
674 1.2 riastrad EXPORT_SYMBOL(drm_legacy_mmap);
675 1.2 riastrad
676 1.2 riastrad void drm_legacy_vma_flush(struct drm_device *dev)
677 1.2 riastrad {
678 1.2 riastrad struct drm_vma_entry *vma, *vma_temp;
679 1.2 riastrad
680 1.2 riastrad /* Clear vma list (only needed for legacy drivers) */
681 1.2 riastrad list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
682 1.2 riastrad list_del(&vma->head);
683 1.2 riastrad kfree(vma);
684 1.2 riastrad }
685 1.2 riastrad }
686 1.2 riastrad
687 1.2 riastrad int drm_vma_info(struct seq_file *m, void *data)
688 1.2 riastrad {
689 1.2 riastrad struct drm_info_node *node = (struct drm_info_node *) m->private;
690 1.2 riastrad struct drm_device *dev = node->minor->dev;
691 1.2 riastrad struct drm_vma_entry *pt;
692 1.2 riastrad struct vm_area_struct *vma;
693 1.2 riastrad unsigned long vma_count = 0;
694 1.2 riastrad #if defined(__i386__)
695 1.2 riastrad unsigned int pgprot;
696 1.2 riastrad #endif
697 1.2 riastrad
698 1.2 riastrad mutex_lock(&dev->struct_mutex);
699 1.2 riastrad list_for_each_entry(pt, &dev->vmalist, head)
700 1.2 riastrad vma_count++;
701 1.2 riastrad
702 1.2 riastrad seq_printf(m, "vma use count: %lu, high_memory = %pK, 0x%pK\n",
703 1.2 riastrad vma_count, high_memory,
704 1.2 riastrad (void *)(unsigned long)virt_to_phys(high_memory));
705 1.2 riastrad
706 1.2 riastrad list_for_each_entry(pt, &dev->vmalist, head) {
707 1.2 riastrad vma = pt->vma;
708 1.2 riastrad if (!vma)
709 1.2 riastrad continue;
710 1.2 riastrad seq_printf(m,
711 1.2 riastrad "\n%5d 0x%pK-0x%pK %c%c%c%c%c%c 0x%08lx000",
712 1.2 riastrad pt->pid,
713 1.2 riastrad (void *)vma->vm_start, (void *)vma->vm_end,
714 1.2 riastrad vma->vm_flags & VM_READ ? 'r' : '-',
715 1.2 riastrad vma->vm_flags & VM_WRITE ? 'w' : '-',
716 1.2 riastrad vma->vm_flags & VM_EXEC ? 'x' : '-',
717 1.2 riastrad vma->vm_flags & VM_MAYSHARE ? 's' : 'p',
718 1.2 riastrad vma->vm_flags & VM_LOCKED ? 'l' : '-',
719 1.2 riastrad vma->vm_flags & VM_IO ? 'i' : '-',
720 1.2 riastrad vma->vm_pgoff);
721 1.2 riastrad
722 1.2 riastrad #if defined(__i386__)
723 1.2 riastrad pgprot = pgprot_val(vma->vm_page_prot);
724 1.2 riastrad seq_printf(m, " %c%c%c%c%c%c%c%c%c",
725 1.2 riastrad pgprot & _PAGE_PRESENT ? 'p' : '-',
726 1.2 riastrad pgprot & _PAGE_RW ? 'w' : 'r',
727 1.2 riastrad pgprot & _PAGE_USER ? 'u' : 's',
728 1.2 riastrad pgprot & _PAGE_PWT ? 't' : 'b',
729 1.2 riastrad pgprot & _PAGE_PCD ? 'u' : 'c',
730 1.2 riastrad pgprot & _PAGE_ACCESSED ? 'a' : '-',
731 1.2 riastrad pgprot & _PAGE_DIRTY ? 'd' : '-',
732 1.2 riastrad pgprot & _PAGE_PSE ? 'm' : 'k',
733 1.2 riastrad pgprot & _PAGE_GLOBAL ? 'g' : 'l');
734 1.2 riastrad #endif
735 1.2 riastrad seq_printf(m, "\n");
736 1.2 riastrad }
737 1.2 riastrad mutex_unlock(&dev->struct_mutex);
738 1.2 riastrad return 0;
739 1.2 riastrad }
740