675d2397def9830dbf82c7a650ab5fca1eb12bd9
[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 (map->offset == VM_OFFSET(vma)) break;
77         }
78
79         if (map && map->type == _DRM_AGP) {
80                 unsigned long offset = address - vma->vm_start;
81                 unsigned long baddr = VM_OFFSET(vma) + 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) goto vm_nopage_error;
102
103                 /*
104                  * Get the page, inc the use count, and return it
105                  */
106                 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
107                 page = virt_to_page(__va(agpmem->memory->memory[offset]));
108                 get_page(page);
109
110                 DRM_DEBUG("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
111                           baddr, __va(agpmem->memory->memory[offset]), offset,
112                           page_count(page));
113
114                 return page;
115         }
116 vm_nopage_error:
117         return NOPAGE_SIGBUS;           /* Disallow mremap */
118 }
119 #else /* __OS_HAS_AGP */
120 static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
121                                                  unsigned long address)
122 {
123         return NOPAGE_SIGBUS;
124 }
125 #endif /* __OS_HAS_AGP */
126
127 /**
128  * \c nopage method for shared virtual memory.
129  *
130  * \param vma virtual memory area.
131  * \param address access address.
132  * \return pointer to the page structure.
133  * 
134  * Get the the mapping, find the real physical page to map, get the page, and
135  * return it.
136  */
137 static __inline__ struct page *drm_do_vm_shm_nopage(struct vm_area_struct *vma,
138                                                      unsigned long address)
139 {
140         drm_map_t        *map    = (drm_map_t *)vma->vm_private_data;
141         unsigned long    offset;
142         unsigned long    i;
143         struct page      *page;
144
145         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
146         if (!map)                  return NOPAGE_OOM;  /* Nothing allocated */
147
148         offset   = address - vma->vm_start;
149         i = (unsigned long)map->handle + offset;
150         page = vmalloc_to_page((void *)i);
151         if (!page)
152                 return NOPAGE_OOM;
153         get_page(page);
154
155         DRM_DEBUG("shm_nopage 0x%lx\n", address);
156         return page;
157 }
158
159
160 /**
161  * \c close method for shared virtual memory.
162  * 
163  * \param vma virtual memory area.
164  * 
165  * Deletes map information if we are the last
166  * person to close a mapping and it's not in the global maplist.
167  */
168 static void drm_vm_shm_close(struct vm_area_struct *vma)
169 {
170         drm_file_t      *priv   = vma->vm_file->private_data;
171         drm_device_t    *dev    = priv->head->dev;
172         drm_vma_entry_t *pt, *prev, *next;
173         drm_map_t *map;
174         drm_map_list_t *r_list;
175         struct list_head *list;
176         int found_maps = 0;
177
178         DRM_DEBUG("0x%08lx,0x%08lx\n",
179                   vma->vm_start, vma->vm_end - vma->vm_start);
180         atomic_dec(&dev->vma_count);
181
182         map = vma->vm_private_data;
183
184         down(&dev->struct_sem);
185         for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
186                 next = pt->next;
187                 if (pt->vma->vm_private_data == map) found_maps++;
188                 if (pt->vma == vma) {
189                         if (prev) {
190                                 prev->next = pt->next;
191                         } else {
192                                 dev->vmalist = pt->next;
193                         }
194                         drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
195                 } else {
196                         prev = pt;
197                 }
198         }
199         /* We were the only map that was found */
200         if(found_maps == 1 &&
201            map->flags & _DRM_REMOVABLE) {
202                 /* Check to see if we are in the maplist, if we are not, then
203                  * we delete this mappings information.
204                  */
205                 found_maps = 0;
206                 list = &dev->maplist->head;
207                 list_for_each(list, &dev->maplist->head) {
208                         r_list = list_entry(list, drm_map_list_t, head);
209                         if (r_list->map == map) found_maps++;
210                 }
211
212                 if(!found_maps) {
213                         drm_dma_handle_t dmah;
214
215                         switch (map->type) {
216                         case _DRM_REGISTERS:
217                         case _DRM_FRAME_BUFFER:
218                                 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
219                                         int retcode;
220                                         retcode = mtrr_del(map->mtrr,
221                                                            map->offset,
222                                                            map->size);
223                                         DRM_DEBUG("mtrr_del = %d\n", retcode);
224                                 }
225                                 drm_ioremapfree(map->handle, map->size, dev);
226                                 break;
227                         case _DRM_SHM:
228                                 vfree(map->handle);
229                                 break;
230                         case _DRM_AGP:
231                         case _DRM_SCATTER_GATHER:
232                                 break;
233                         case _DRM_CONSISTENT:
234                                 dmah.vaddr = map->handle;
235                                 dmah.busaddr = map->offset;
236                                 dmah.size = map->size;
237                                 __drm_pci_free(dev, &dmah);
238                                 break;
239                         }
240                         drm_free(map, sizeof(*map), DRM_MEM_MAPS);
241                 }
242         }
243         up(&dev->struct_sem);
244 }
245
246 /**
247  * \c nopage method for DMA virtual memory.
248  *
249  * \param vma virtual memory area.
250  * \param address access address.
251  * \return pointer to the page structure.
252  * 
253  * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
254  */
255 static __inline__ struct page *drm_do_vm_dma_nopage(struct vm_area_struct *vma,
256                                                      unsigned long address)
257 {
258         drm_file_t       *priv   = vma->vm_file->private_data;
259         drm_device_t     *dev    = priv->head->dev;
260         drm_device_dma_t *dma    = dev->dma;
261         unsigned long    offset;
262         unsigned long    page_nr;
263         struct page      *page;
264
265         if (!dma)                  return NOPAGE_SIGBUS; /* Error */
266         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
267         if (!dma->pagelist)        return NOPAGE_OOM ; /* Nothing allocated */
268
269         offset   = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
270         page_nr  = offset >> PAGE_SHIFT;
271         page = virt_to_page((dma->pagelist[page_nr] + 
272                              (offset & (~PAGE_MASK))));
273
274         get_page(page);
275
276         DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
277         return page;
278 }
279
280 /**
281  * \c nopage method for scatter-gather virtual memory.
282  *
283  * \param vma virtual memory area.
284  * \param address access address.
285  * \return pointer to the page structure.
286  * 
287  * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
288  */
289 static __inline__ struct page *drm_do_vm_sg_nopage(struct vm_area_struct *vma,
290                                                     unsigned long address)
291 {
292         drm_map_t        *map    = (drm_map_t *)vma->vm_private_data;
293         drm_file_t *priv = vma->vm_file->private_data;
294         drm_device_t *dev = priv->head->dev;
295         drm_sg_mem_t *entry = dev->sg;
296         unsigned long offset;
297         unsigned long map_offset;
298         unsigned long page_offset;
299         struct page *page;
300
301         if (!entry)                return NOPAGE_SIGBUS; /* Error */
302         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
303         if (!entry->pagelist)      return NOPAGE_OOM ;  /* Nothing allocated */
304
305
306         offset = address - vma->vm_start;
307         map_offset = map->offset - dev->sg->handle;
308         page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
309         page = entry->pagelist[page_offset];
310         get_page(page);
311
312         return page;
313 }
314
315
316 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
317
318 static struct page *drm_vm_nopage(struct vm_area_struct *vma,
319                                    unsigned long address,
320                                    int *type) {
321         if (type) *type = VM_FAULT_MINOR;
322         return drm_do_vm_nopage(vma, address);
323 }
324
325 static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
326                                        unsigned long address,
327                                        int *type) {
328         if (type) *type = VM_FAULT_MINOR;
329         return drm_do_vm_shm_nopage(vma, address);
330 }
331
332 static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
333                                        unsigned long address,
334                                        int *type) {
335         if (type) *type = VM_FAULT_MINOR;
336         return drm_do_vm_dma_nopage(vma, address);
337 }
338
339 static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
340                                       unsigned long address,
341                                       int *type) {
342         if (type) *type = VM_FAULT_MINOR;
343         return drm_do_vm_sg_nopage(vma, address);
344 }
345
346 #else   /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
347
348 static struct page *drm_vm_nopage(struct vm_area_struct *vma,
349                                    unsigned long address,
350                                    int unused) {
351         return drm_do_vm_nopage(vma, address);
352 }
353
354 static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
355                                        unsigned long address,
356                                        int unused) {
357         return drm_do_vm_shm_nopage(vma, address);
358 }
359
360 static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
361                                        unsigned long address,
362                                        int unused) {
363         return drm_do_vm_dma_nopage(vma, address);
364 }
365
366 static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
367                                       unsigned long address,
368                                       int unused) {
369         return drm_do_vm_sg_nopage(vma, address);
370 }
371
372 #endif
373
374
375 /** AGP virtual memory operations */
376 static struct vm_operations_struct   drm_vm_ops = {
377         .nopage = drm_vm_nopage,
378         .open   = drm_vm_open,
379         .close  = drm_vm_close,
380 };
381
382 /** Shared virtual memory operations */
383 static struct vm_operations_struct   drm_vm_shm_ops = {
384         .nopage = drm_vm_shm_nopage,
385         .open   = drm_vm_open,
386         .close  = drm_vm_shm_close,
387 };
388
389 /** DMA virtual memory operations */
390 static struct vm_operations_struct   drm_vm_dma_ops = {
391         .nopage = drm_vm_dma_nopage,
392         .open   = drm_vm_open,
393         .close  = drm_vm_close,
394 };
395
396 /** Scatter-gather virtual memory operations */
397 static struct vm_operations_struct   drm_vm_sg_ops = {
398         .nopage = drm_vm_sg_nopage,
399         .open   = drm_vm_open,
400         .close  = drm_vm_close,
401 };
402
403
404 /**
405  * \c open method for shared virtual memory.
406  * 
407  * \param vma virtual memory area.
408  * 
409  * Create a new drm_vma_entry structure as the \p vma private data entry and
410  * add it to drm_device::vmalist.
411  */
412 static void drm_vm_open(struct vm_area_struct *vma)
413 {
414         drm_file_t      *priv   = vma->vm_file->private_data;
415         drm_device_t    *dev    = priv->head->dev;
416         drm_vma_entry_t *vma_entry;
417
418         DRM_DEBUG("0x%08lx,0x%08lx\n",
419                   vma->vm_start, vma->vm_end - vma->vm_start);
420         atomic_inc(&dev->vma_count);
421
422         vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
423         if (vma_entry) {
424                 down(&dev->struct_sem);
425                 vma_entry->vma  = vma;
426                 vma_entry->next = dev->vmalist;
427                 vma_entry->pid  = current->pid;
428                 dev->vmalist    = vma_entry;
429                 up(&dev->struct_sem);
430         }
431 }
432
433 /**
434  * \c close method for all virtual memory types.
435  * 
436  * \param vma virtual memory area.
437  * 
438  * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
439  * free it.
440  */
441 static void drm_vm_close(struct vm_area_struct *vma)
442 {
443         drm_file_t      *priv   = vma->vm_file->private_data;
444         drm_device_t    *dev    = priv->head->dev;
445         drm_vma_entry_t *pt, *prev;
446
447         DRM_DEBUG("0x%08lx,0x%08lx\n",
448                   vma->vm_start, vma->vm_end - vma->vm_start);
449         atomic_dec(&dev->vma_count);
450
451         down(&dev->struct_sem);
452         for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
453                 if (pt->vma == vma) {
454                         if (prev) {
455                                 prev->next = pt->next;
456                         } else {
457                                 dev->vmalist = pt->next;
458                         }
459                         drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
460                         break;
461                 }
462         }
463         up(&dev->struct_sem);
464 }
465
466 /**
467  * mmap DMA memory.
468  *
469  * \param filp file pointer.
470  * \param vma virtual memory area.
471  * \return zero on success or a negative number on failure.
472  * 
473  * Sets the virtual memory area operations structure to vm_dma_ops, the file
474  * pointer, and calls vm_open().
475  */
476 static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
477 {
478         drm_file_t       *priv   = filp->private_data;
479         drm_device_t     *dev;
480         drm_device_dma_t *dma;
481         unsigned long    length  = vma->vm_end - vma->vm_start;
482
483         lock_kernel();
484         dev      = priv->head->dev;
485         dma      = dev->dma;
486         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
487                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
488
489                                 /* Length must match exact page count */
490         if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
491                 unlock_kernel();
492                 return -EINVAL;
493         }
494         unlock_kernel();
495
496         vma->vm_ops   = &drm_vm_dma_ops;
497
498 #if LINUX_VERSION_CODE <= 0x02040e /* KERNEL_VERSION(2,4,14) */
499         vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */
500 #else
501         vma->vm_flags |= VM_RESERVED; /* Don't swap */
502 #endif
503
504         vma->vm_file  =  filp;  /* Needed for drm_vm_open() */
505         drm_vm_open(vma);
506         return 0;
507 }
508
509 unsigned long drm_core_get_map_ofs(drm_map_t *map)
510 {
511         return map->offset;
512 }
513 EXPORT_SYMBOL(drm_core_get_map_ofs);
514
515 unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
516 {
517 #ifdef __alpha__
518         return dev->hose->dense_mem_base - dev->hose->mem_space->start;
519 #else
520         return 0;
521 #endif
522 }
523 EXPORT_SYMBOL(drm_core_get_reg_ofs);
524
525 /**
526  * mmap DMA memory.
527  *
528  * \param filp file pointer.
529  * \param vma virtual memory area.
530  * \return zero on success or a negative number on failure.
531  * 
532  * If the virtual memory area has no offset associated with it then it's a DMA
533  * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
534  * checks that the restricted flag is not set, sets the virtual memory operations
535  * according to the mapping type and remaps the pages. Finally sets the file
536  * pointer and calls vm_open().
537  */
538 int drm_mmap(struct file *filp, struct vm_area_struct *vma)
539 {
540         drm_file_t      *priv   = filp->private_data;
541         drm_device_t    *dev    = priv->head->dev;
542         drm_map_t       *map    = NULL;
543         drm_map_list_t  *r_list;
544         unsigned long   offset  = 0;
545         struct list_head *list;
546
547         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
548                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
549
550         if ( !priv->authenticated ) return -EACCES;
551
552         /* We check for "dma". On Apple's UniNorth, it's valid to have
553          * the AGP mapped at physical address 0
554          * --BenH.
555          */
556         if (!VM_OFFSET(vma)
557 #if __OS_HAS_AGP
558             && (!dev->agp || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
559 #endif
560             )
561                 return drm_mmap_dma(filp, vma);
562
563                                 /* A sequential search of a linked list is
564                                    fine here because: 1) there will only be
565                                    about 5-10 entries in the list and, 2) a
566                                    DRI client only has to do this mapping
567                                    once, so it doesn't have to be optimized
568                                    for performance, even if the list was a
569                                    bit longer. */
570         list_for_each(list, &dev->maplist->head) {
571                 unsigned long off;
572
573                 r_list = list_entry(list, drm_map_list_t, head);
574                 map = r_list->map;
575                 if (!map) continue;
576                 off = dev->driver->get_map_ofs(map);
577                 if (off == VM_OFFSET(vma)) 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 (VM_OFFSET(vma) >= __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                                         (VM_OFFSET(vma) + 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                                      (VM_OFFSET(vma) + 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, VM_OFFSET(vma) + 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);