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