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