Keep hashed user tokens, with the following changes:
[platform/upstream/libdrm.git] / linux-core / drm_vm.c
1 /**
2  * \file drm_vm.c
3  * Memory mapping for DRM
4  *
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
36 #include "drmP.h"
37 #if defined(__ia64__)
38 #include <linux/efi.h>
39 #endif
40
41 static void drm_vm_open(struct vm_area_struct *vma);
42 static void drm_vm_close(struct vm_area_struct *vma);
43
44 /**
45  * \c nopage method for AGP virtual memory.
46  *
47  * \param vma virtual memory area.
48  * \param address access address.
49  * \return pointer to the page structure.
50  *
51  * Find the right map and if it's AGP memory find the real physical page to
52  * map, get the page, increment the use count and return it.
53  */
54 #if __OS_HAS_AGP
55 static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
56                                                 unsigned long address)
57 {
58         drm_file_t *priv = vma->vm_file->private_data;
59         drm_device_t *dev = priv->head->dev;
60         drm_map_t *map = NULL;
61         drm_map_list_t *r_list;
62         drm_hash_item_t *hash;
63
64         /*
65          * Find the right map
66          */
67         if (!drm_core_has_AGP(dev))
68                 goto vm_nopage_error;
69
70         if (!dev->agp || !dev->agp->cant_use_aperture)
71                 goto vm_nopage_error;
72
73         if (drm_ht_find_item(&dev->map_hash, VM_OFFSET(vma), &hash)) 
74                 goto vm_nopage_error;
75
76         r_list = drm_hash_entry(hash, drm_map_list_t, hash);
77         map = r_list->map;
78
79         if (map && map->type == _DRM_AGP) {
80                 unsigned long offset = address - vma->vm_start;
81                 unsigned long baddr = map->offset + offset;
82                 struct drm_agp_mem *agpmem;
83                 struct page *page;
84
85 #ifdef __alpha__
86                 /*
87                  * Adjust to a bus-relative address
88                  */
89                 baddr -= dev->hose->mem_space->start;
90 #endif
91
92                 /*
93                  * It's AGP memory - find the real physical page to map
94                  */
95                 for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
96                         if (agpmem->bound <= baddr &&
97                             agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
98                                 break;
99                 }
100
101                 if (!agpmem)
102                         goto vm_nopage_error;
103
104                 /*
105                  * Get the page, inc the use count, and return it
106                  */
107                 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
108                 page = virt_to_page(__va(agpmem->memory->memory[offset]));
109                 get_page(page);
110
111 #if 0
112                 /* page_count() not defined everywhere */
113                 DRM_DEBUG
114                     ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
115                      baddr, __va(agpmem->memory->memory[offset]), offset,
116                      page_count(page));
117 #endif
118
119                 return page;
120         }
121       vm_nopage_error:
122         return NOPAGE_SIGBUS;   /* Disallow mremap */
123 }
124 #else                           /* __OS_HAS_AGP */
125 static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
126                                                 unsigned long address)
127 {
128         return NOPAGE_SIGBUS;
129 }
130 #endif                          /* __OS_HAS_AGP */
131
132 /**
133  * \c nopage method for shared virtual memory.
134  *
135  * \param vma virtual memory area.
136  * \param address access address.
137  * \return pointer to the page structure.
138  *
139  * Get the the mapping, find the real physical page to map, get the page, and
140  * return it.
141  */
142 static __inline__ struct page *drm_do_vm_shm_nopage(struct vm_area_struct *vma,
143                                                     unsigned long address)
144 {
145         drm_map_t *map = (drm_map_t *) vma->vm_private_data;
146         unsigned long offset;
147         unsigned long i;
148         struct page *page;
149
150         if (address > vma->vm_end)
151                 return NOPAGE_SIGBUS;   /* Disallow mremap */
152         if (!map)
153                 return NOPAGE_OOM;      /* Nothing allocated */
154
155         offset = address - vma->vm_start;
156         i = (unsigned long)map->handle + offset;
157         page = vmalloc_to_page((void *)i);
158         if (!page)
159                 return NOPAGE_OOM;
160         get_page(page);
161
162         DRM_DEBUG("shm_nopage 0x%lx\n", address);
163         return page;
164 }
165
166 /**
167  * \c close method for shared virtual memory.
168  *
169  * \param vma virtual memory area.
170  *
171  * Deletes map information if we are the last
172  * person to close a mapping and it's not in the global maplist.
173  */
174 static void drm_vm_shm_close(struct vm_area_struct *vma)
175 {
176         drm_file_t *priv = vma->vm_file->private_data;
177         drm_device_t *dev = priv->head->dev;
178         drm_vma_entry_t *pt, *prev, *next;
179         drm_map_t *map;
180         drm_map_list_t *r_list;
181         struct list_head *list;
182         int found_maps = 0;
183
184         DRM_DEBUG("0x%08lx,0x%08lx\n",
185                   vma->vm_start, vma->vm_end - vma->vm_start);
186         atomic_dec(&dev->vma_count);
187
188         map = vma->vm_private_data;
189
190         down(&dev->struct_sem);
191         for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
192                 next = pt->next;
193                 if (pt->vma->vm_private_data == map)
194                         found_maps++;
195                 if (pt->vma == vma) {
196                         if (prev) {
197                                 prev->next = pt->next;
198                         } else {
199                                 dev->vmalist = pt->next;
200                         }
201                         drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
202                 } else {
203                         prev = pt;
204                 }
205         }
206         /* We were the only map that was found */
207         if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
208                 /* Check to see if we are in the maplist, if we are not, then
209                  * we delete this mappings information.
210                  */
211                 found_maps = 0;
212                 list = &dev->maplist->head;
213                 list_for_each(list, &dev->maplist->head) {
214                         r_list = list_entry(list, drm_map_list_t, head);
215                         if (r_list->map == map)
216                                 found_maps++;
217                 }
218
219                 if (!found_maps) {
220                         drm_dma_handle_t dmah;
221
222                         switch (map->type) {
223                         case _DRM_REGISTERS:
224                         case _DRM_FRAME_BUFFER:
225                                 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
226                                         int retcode;
227                                         retcode = mtrr_del(map->mtrr,
228                                                            map->offset,
229                                                            map->size);
230                                         DRM_DEBUG("mtrr_del = %d\n", retcode);
231                                 }
232                                 drm_ioremapfree(map->handle, map->size, dev);
233                                 break;
234                         case _DRM_SHM:
235                                 vfree(map->handle);
236                                 break;
237                         case _DRM_AGP:
238                         case _DRM_SCATTER_GATHER:
239                                 break;
240                         case _DRM_CONSISTENT:
241                                 dmah.vaddr = map->handle;
242                                 dmah.busaddr = map->offset;
243                                 dmah.size = map->size;
244                                 __drm_pci_free(dev, &dmah);
245                                 break;
246                         }
247                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
248                 }
249         }
250         up(&dev->struct_sem);
251 }
252
253 /**
254  * \c nopage method for DMA virtual memory.
255  *
256  * \param vma virtual memory area.
257  * \param address access address.
258  * \return pointer to the page structure.
259  *
260  * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
261  */
262 static __inline__ struct page *drm_do_vm_dma_nopage(struct vm_area_struct *vma,
263                                                     unsigned long address)
264 {
265         drm_file_t *priv = vma->vm_file->private_data;
266         drm_device_t *dev = priv->head->dev;
267         drm_device_dma_t *dma = dev->dma;
268         unsigned long offset;
269         unsigned long page_nr;
270         struct page *page;
271
272         if (!dma)
273                 return NOPAGE_SIGBUS;   /* Error */
274         if (address > vma->vm_end)
275                 return NOPAGE_SIGBUS;   /* Disallow mremap */
276         if (!dma->pagelist)
277                 return NOPAGE_OOM;      /* Nothing allocated */
278
279         offset = address - vma->vm_start;       /* vm_[pg]off[set] should be 0 */
280         page_nr = offset >> PAGE_SHIFT;
281         page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
282
283         get_page(page);
284
285         DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
286         return page;
287 }
288
289 /**
290  * \c nopage method for scatter-gather virtual memory.
291  *
292  * \param vma virtual memory area.
293  * \param address access address.
294  * \return pointer to the page structure.
295  *
296  * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
297  */
298 static __inline__ struct page *drm_do_vm_sg_nopage(struct vm_area_struct *vma,
299                                                    unsigned long address)
300 {
301         drm_map_t *map = (drm_map_t *) vma->vm_private_data;
302         drm_file_t *priv = vma->vm_file->private_data;
303         drm_device_t *dev = priv->head->dev;
304         drm_sg_mem_t *entry = dev->sg;
305         unsigned long offset;
306         unsigned long map_offset;
307         unsigned long page_offset;
308         struct page *page;
309
310         DRM_DEBUG("\n");
311         if (!entry)
312                 return NOPAGE_SIGBUS;   /* Error */
313         if (address > vma->vm_end)
314                 return NOPAGE_SIGBUS;   /* Disallow mremap */
315         if (!entry->pagelist)
316                 return NOPAGE_OOM;      /* Nothing allocated */
317
318         offset = address - vma->vm_start;
319         map_offset = map->offset - (unsigned long)dev->sg->virtual;
320         page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
321         page = entry->pagelist[page_offset];
322         get_page(page);
323
324         return page;
325 }
326
327 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
328
329 static struct page *drm_vm_nopage(struct vm_area_struct *vma,
330                                   unsigned long address, int *type)
331 {
332         if (type)
333                 *type = VM_FAULT_MINOR;
334         return drm_do_vm_nopage(vma, address);
335 }
336
337 static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
338                                       unsigned long address, int *type)
339 {
340         if (type)
341                 *type = VM_FAULT_MINOR;
342         return drm_do_vm_shm_nopage(vma, address);
343 }
344
345 static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
346                                       unsigned long address, int *type)
347 {
348         if (type)
349                 *type = VM_FAULT_MINOR;
350         return drm_do_vm_dma_nopage(vma, address);
351 }
352
353 static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
354                                      unsigned long address, int *type)
355 {
356         if (type)
357                 *type = VM_FAULT_MINOR;
358         return drm_do_vm_sg_nopage(vma, address);
359 }
360
361 #else                           /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
362
363 static struct page *drm_vm_nopage(struct vm_area_struct *vma,
364                                   unsigned long address, int unused)
365 {
366         return drm_do_vm_nopage(vma, address);
367 }
368
369 static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
370                                       unsigned long address, int unused)
371 {
372         return drm_do_vm_shm_nopage(vma, address);
373 }
374
375 static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
376                                       unsigned long address, int unused)
377 {
378         return drm_do_vm_dma_nopage(vma, address);
379 }
380
381 static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
382                                      unsigned long address, int unused)
383 {
384         return drm_do_vm_sg_nopage(vma, address);
385 }
386
387 #endif
388
389 /** AGP virtual memory operations */
390 static struct vm_operations_struct drm_vm_ops = {
391         .nopage = drm_vm_nopage,
392         .open = drm_vm_open,
393         .close = drm_vm_close,
394 };
395
396 /** Shared virtual memory operations */
397 static struct vm_operations_struct drm_vm_shm_ops = {
398         .nopage = drm_vm_shm_nopage,
399         .open = drm_vm_open,
400         .close = drm_vm_shm_close,
401 };
402
403 /** DMA virtual memory operations */
404 static struct vm_operations_struct drm_vm_dma_ops = {
405         .nopage = drm_vm_dma_nopage,
406         .open = drm_vm_open,
407         .close = drm_vm_close,
408 };
409
410 /** Scatter-gather virtual memory operations */
411 static struct vm_operations_struct drm_vm_sg_ops = {
412         .nopage = drm_vm_sg_nopage,
413         .open = drm_vm_open,
414         .close = drm_vm_close,
415 };
416
417 /**
418  * \c open method for shared virtual memory.
419  *
420  * \param vma virtual memory area.
421  *
422  * Create a new drm_vma_entry structure as the \p vma private data entry and
423  * add it to drm_device::vmalist.
424  */
425 static void drm_vm_open(struct vm_area_struct *vma)
426 {
427         drm_file_t *priv = vma->vm_file->private_data;
428         drm_device_t *dev = priv->head->dev;
429         drm_vma_entry_t *vma_entry;
430
431         DRM_DEBUG("0x%08lx,0x%08lx\n",
432                   vma->vm_start, vma->vm_end - vma->vm_start);
433         atomic_inc(&dev->vma_count);
434
435         vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
436         if (vma_entry) {
437                 down(&dev->struct_sem);
438                 vma_entry->vma = vma;
439                 vma_entry->next = dev->vmalist;
440                 vma_entry->pid = current->pid;
441                 dev->vmalist = vma_entry;
442                 up(&dev->struct_sem);
443         }
444 }
445
446 /**
447  * \c close method for all virtual memory types.
448  *
449  * \param vma virtual memory area.
450  *
451  * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
452  * free it.
453  */
454 static void drm_vm_close(struct vm_area_struct *vma)
455 {
456         drm_file_t *priv = vma->vm_file->private_data;
457         drm_device_t *dev = priv->head->dev;
458         drm_vma_entry_t *pt, *prev;
459
460         DRM_DEBUG("0x%08lx,0x%08lx\n",
461                   vma->vm_start, vma->vm_end - vma->vm_start);
462         atomic_dec(&dev->vma_count);
463
464         down(&dev->struct_sem);
465         for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
466                 if (pt->vma == vma) {
467                         if (prev) {
468                                 prev->next = pt->next;
469                         } else {
470                                 dev->vmalist = pt->next;
471                         }
472                         drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
473                         break;
474                 }
475         }
476         up(&dev->struct_sem);
477 }
478
479 /**
480  * mmap DMA memory.
481  *
482  * \param filp file pointer.
483  * \param vma virtual memory area.
484  * \return zero on success or a negative number on failure.
485  *
486  * Sets the virtual memory area operations structure to vm_dma_ops, the file
487  * pointer, and calls vm_open().
488  */
489 static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
490 {
491         drm_file_t *priv = filp->private_data;
492         drm_device_t *dev;
493         drm_device_dma_t *dma;
494         unsigned long length = vma->vm_end - vma->vm_start;
495
496         lock_kernel();
497         dev = priv->head->dev;
498         dma = dev->dma;
499         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
500                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
501
502         /* Length must match exact page count */
503         if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
504                 unlock_kernel();
505                 return -EINVAL;
506         }
507         unlock_kernel();
508
509         vma->vm_ops = &drm_vm_dma_ops;
510
511 #if LINUX_VERSION_CODE <= 0x02040e      /* KERNEL_VERSION(2,4,14) */
512         vma->vm_flags |= VM_LOCKED | VM_SHM;    /* Don't swap */
513 #else
514         vma->vm_flags |= VM_RESERVED;   /* Don't swap */
515 #endif
516
517         vma->vm_file = filp;    /* Needed for drm_vm_open() */
518         drm_vm_open(vma);
519         return 0;
520 }
521
522 unsigned long drm_core_get_map_ofs(drm_map_t * map)
523 {
524         return map->offset;
525 }
526 EXPORT_SYMBOL(drm_core_get_map_ofs);
527
528 unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
529 {
530 #ifdef __alpha__
531         return dev->hose->dense_mem_base - dev->hose->mem_space->start;
532 #else
533         return 0;
534 #endif
535 }
536 EXPORT_SYMBOL(drm_core_get_reg_ofs);
537
538 /**
539  * mmap DMA memory.
540  *
541  * \param filp file pointer.
542  * \param vma virtual memory area.
543  * \return zero on success or a negative number on failure.
544  *
545  * If the virtual memory area has no offset associated with it then it's a DMA
546  * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
547  * checks that the restricted flag is not set, sets the virtual memory operations
548  * according to the mapping type and remaps the pages. Finally sets the file
549  * pointer and calls vm_open().
550  */
551 int drm_mmap(struct file *filp, struct vm_area_struct *vma)
552 {
553         drm_file_t *priv = filp->private_data;
554         drm_device_t *dev = priv->head->dev;
555         drm_map_t *map = NULL;
556         unsigned long offset = 0;
557         drm_hash_item_t *hash;
558
559         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
560                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
561
562         if (!priv->authenticated)
563                 return -EACCES;
564
565         /* We check for "dma". On Apple's UniNorth, it's valid to have
566          * the AGP mapped at physical address 0
567          * --BenH.
568          */
569         if (!VM_OFFSET(vma)
570 #if __OS_HAS_AGP
571             && (!dev->agp
572                 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
573 #endif
574             )
575                 return drm_mmap_dma(filp, vma);
576
577         if (drm_ht_find_item(&dev->map_hash, VM_OFFSET(vma), &hash)) {
578                 DRM_ERROR("Could not find map\n");
579                 return -EINVAL;
580         }
581         
582         map = drm_hash_entry(hash,drm_map_list_t, hash)->map;
583
584         if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
585                 return -EPERM;
586
587         /* Check for valid size. */
588         if (map->size < vma->vm_end - vma->vm_start)
589                 return -EINVAL;
590
591         if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
592                 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
593 #if defined(__i386__) || defined(__x86_64__)
594                 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
595 #else
596                 /* Ye gads this is ugly.  With more thought
597                    we could move this up higher and use
598                    `protection_map' instead.  */
599                 vma->vm_page_prot =
600                     __pgprot(pte_val
601                              (pte_wrprotect
602                               (__pte(pgprot_val(vma->vm_page_prot)))));
603 #endif
604         }
605
606         switch (map->type) {
607         case _DRM_AGP:
608                 if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
609                         /*
610                          * On some platforms we can't talk to bus dma address from the CPU, so for
611                          * memory of type DRM_AGP, we'll deal with sorting out the real physical
612                          * pages and mappings in nopage()
613                          */
614 #if defined(__powerpc__)
615                         pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
616 #endif
617                         vma->vm_ops = &drm_vm_ops;
618                         break;
619                 }
620                 /* fall through to _DRM_FRAME_BUFFER... */
621         case _DRM_FRAME_BUFFER:
622         case _DRM_REGISTERS:
623 #if defined(__i386__) || defined(__x86_64__)
624                 if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
625                         pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
626                         pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
627                 }
628 #elif defined(__powerpc__)
629                 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
630                 if (map->type == _DRM_REGISTERS)
631                         pgprot_val(vma->vm_page_prot) |= _PAGE_GUARDED;
632 #endif
633                 vma->vm_flags |= VM_IO; /* not in core dump */
634 #if defined(__ia64__)
635                 if (efi_range_is_wc(vma->vm_start, vma->vm_end -
636                                     vma->vm_start))
637                         vma->vm_page_prot =
638                                 pgprot_writecombine(vma->vm_page_prot);
639                 else
640                         vma->vm_page_prot =
641                                 pgprot_noncached(vma->vm_page_prot);
642 #endif
643                 offset = dev->driver->get_reg_ofs(dev);
644 #ifdef __sparc__
645                 if (io_remap_pfn_range(vma, vma->vm_start,
646                                         (map->offset + offset) >>PAGE_SHIFT,
647                                         vma->vm_end - vma->vm_start,
648                                         vma->vm_page_prot))
649 #else
650                 if (remap_pfn_range(vma, vma->vm_start,
651                                      (map->offset + offset) >> PAGE_SHIFT,
652                                      vma->vm_end - vma->vm_start,
653                                      vma->vm_page_prot))
654 #endif
655                         return -EAGAIN;
656                 DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
657                           " offset = 0x%lx\n",
658                           map->type,
659                           vma->vm_start, vma->vm_end, map->offset + offset);
660                 vma->vm_ops = &drm_vm_ops;
661                 break;
662         case _DRM_CONSISTENT:
663                 /* Consistent memory is really like shared memory. But
664                  * it's allocated in a different way, so avoid nopage */
665                 if (remap_pfn_range(vma, vma->vm_start,
666                     page_to_pfn(virt_to_page(map->handle)),
667                     vma->vm_end - vma->vm_start, vma->vm_page_prot))
668                         return -EAGAIN;
669         /* fall through to _DRM_SHM */
670         case _DRM_SHM:
671                 vma->vm_ops = &drm_vm_shm_ops;
672                 vma->vm_private_data = (void *)map;
673                 /* Don't let this area swap.  Change when
674                    DRM_KERNEL advisory is supported. */
675 #if LINUX_VERSION_CODE <= 0x02040e      /* KERNEL_VERSION(2,4,14) */
676                 vma->vm_flags |= VM_LOCKED;
677 #else
678                 vma->vm_flags |= VM_RESERVED;
679 #endif
680                 break;
681         case _DRM_SCATTER_GATHER:
682                 vma->vm_ops = &drm_vm_sg_ops;
683                 vma->vm_private_data = (void *)map;
684 #if LINUX_VERSION_CODE <= 0x02040e      /* KERNEL_VERSION(2,4,14) */
685                 vma->vm_flags |= VM_LOCKED;
686 #else
687                 vma->vm_flags |= VM_RESERVED;
688 #endif
689                 break;
690         default:
691                 return -EINVAL; /* This should never happen. */
692         }
693 #if LINUX_VERSION_CODE <= 0x02040e      /* KERNEL_VERSION(2,4,14) */
694         vma->vm_flags |= VM_LOCKED | VM_SHM;    /* Don't swap */
695 #else
696         vma->vm_flags |= VM_RESERVED;   /* Don't swap */
697 #endif
698
699         vma->vm_file = filp;    /* Needed for drm_vm_open() */
700         drm_vm_open(vma);
701         return 0;
702 }
703 EXPORT_SYMBOL(drm_mmap);