- fix mapped-pte usage - do not access it after unmapping.
[opensuse:kernel.git] / drivers / char / drm / drm_vm.h
1 /* drm_vm.h -- Memory mapping for DRM -*- linux-c -*-
2  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  */
31
32 #define __NO_VERSION__
33 #include "drmP.h"
34
35 struct vm_operations_struct   DRM(vm_ops) = {
36         nopage:  DRM(vm_nopage),
37         open:    DRM(vm_open),
38         close:   DRM(vm_close),
39 };
40
41 struct vm_operations_struct   DRM(vm_shm_ops) = {
42         nopage:  DRM(vm_shm_nopage),
43         open:    DRM(vm_open),
44         close:   DRM(vm_shm_close),
45 };
46
47 struct vm_operations_struct   DRM(vm_dma_ops) = {
48         nopage:  DRM(vm_dma_nopage),
49         open:    DRM(vm_open),
50         close:   DRM(vm_close),
51 };
52
53 struct vm_operations_struct   DRM(vm_sg_ops) = {
54         nopage:  DRM(vm_sg_nopage),
55         open:    DRM(vm_open),
56         close:   DRM(vm_close),
57 };
58
59 #if LINUX_VERSION_CODE < 0x020317
60 unsigned long DRM(vm_nopage)(struct vm_area_struct *vma,
61                              unsigned long address,
62                              int unused)
63 #else
64                                 /* Return type changed in 2.3.23 */
65 struct page *DRM(vm_nopage)(struct vm_area_struct *vma,
66                             unsigned long address,
67                             int unused)
68 #endif
69 {
70 #if __REALLY_HAVE_AGP
71         drm_file_t *priv  = vma->vm_file->private_data;
72         drm_device_t *dev = priv->dev;
73         drm_map_t *map    = NULL;
74         drm_map_list_t  *r_list;
75         struct list_head *list;
76
77         /*
78          * Find the right map
79          */
80
81         if(!dev->agp->cant_use_aperture) goto vm_nopage_error;
82
83         list_for_each(list, &dev->maplist->head) {
84                 r_list = (drm_map_list_t *)list;
85                 map = r_list->map;
86                 if (!map) continue;
87                 if (map->offset == VM_OFFSET(vma)) break;
88         }
89
90         if (map && map->type == _DRM_AGP) {
91                 unsigned long offset = address - vma->vm_start;
92                 unsigned long baddr = VM_OFFSET(vma) + offset;
93                 struct drm_agp_mem *agpmem;
94                 struct page *page;
95
96 #if __alpha__
97                 /*
98                  * Adjust to a bus-relative address
99                  */
100                 baddr -= dev->hose->mem_space->start;
101 #endif
102
103                 /*
104                  * It's AGP memory - find the real physical page to map
105                  */
106                 for(agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
107                         if (agpmem->bound <= baddr &&
108                             agpmem->bound + agpmem->pages * PAGE_SIZE > baddr) 
109                                 break;
110                 }
111
112                 if (!agpmem) goto vm_nopage_error;
113
114                 /*
115                  * Get the page, inc the use count, and return it
116                  */
117                 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
118                 agpmem->memory->memory[offset] &= dev->agp->page_mask;
119                 page = virt_to_page(__va(agpmem->memory->memory[offset]));
120                 get_page(page);
121
122                 DRM_DEBUG("baddr = 0x%lx page = 0x%p, offset = 0x%lx\n",
123                           baddr, __va(agpmem->memory->memory[offset]), offset);
124
125 #if LINUX_VERSION_CODE < 0x020317
126                 return page_address(page);
127 #else
128                 return page;
129 #endif
130         }
131 vm_nopage_error:
132 #endif /* __REALLY_HAVE_AGP */
133
134         return NOPAGE_SIGBUS;           /* Disallow mremap */
135 }
136
137 #if LINUX_VERSION_CODE < 0x020317
138 unsigned long DRM(vm_shm_nopage)(struct vm_area_struct *vma,
139                                  unsigned long address,
140                                  int unused)
141 #else
142                                 /* Return type changed in 2.3.23 */
143 struct page *DRM(vm_shm_nopage)(struct vm_area_struct *vma,
144                                 unsigned long address,
145                                 int unused)
146 #endif
147 {
148 #if LINUX_VERSION_CODE >= 0x020300
149         drm_map_t        *map    = (drm_map_t *)vma->vm_private_data;
150 #else
151         drm_map_t        *map    = (drm_map_t *)vma->vm_pte;
152 #endif
153         unsigned long    offset;
154         unsigned long    i;
155         pgd_t            *pgd;
156         pmd_t            *pmd;
157         pte_t            *pte, entry;
158         struct page      *page;
159
160         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
161         if (!map)                  return NOPAGE_OOM;  /* Nothing allocated */
162
163         offset   = address - vma->vm_start;
164         i = (unsigned long)map->handle + offset;
165         /* We have to walk page tables here because we need large SAREA's, and
166          * they need to be virtually contiguous in kernel space.
167          */
168         pgd = pgd_offset_k( i );
169         if (!pgd_present(*pgd))
170                 goto oom;
171         pmd = pmd_offset( pgd, i );
172         if (!pmd_present(*pmd))
173                 goto oom;
174
175         preempt_disable();
176         pte = pte_offset_map(pmd, i);
177         entry = *pte;
178         pte_unmap(pte);
179         preempt_enable();
180
181         if (!pte_present(entry))
182                 goto oom;
183
184         page = pte_page(entry);
185         get_page(page);
186
187         DRM_DEBUG("shm_nopage 0x%lx\n", address);
188 #if LINUX_VERSION_CODE < 0x020317
189         return page_address(page);
190 #else
191         return page;
192 #endif
193 oom:
194         return NOPAGE_OOM;
195 }
196
197 /* Special close routine which deletes map information if we are the last
198  * person to close a mapping and its not in the global maplist.
199  */
200
201 void DRM(vm_shm_close)(struct vm_area_struct *vma)
202 {
203         drm_file_t      *priv   = vma->vm_file->private_data;
204         drm_device_t    *dev    = priv->dev;
205         drm_vma_entry_t *pt, *prev, *next;
206         drm_map_t *map;
207         drm_map_list_t *r_list;
208         struct list_head *list;
209         int found_maps = 0;
210
211         DRM_DEBUG("0x%08lx,0x%08lx\n",
212                   vma->vm_start, vma->vm_end - vma->vm_start);
213 #if LINUX_VERSION_CODE < 0x020333
214         MOD_DEC_USE_COUNT; /* Needed before Linux 2.3.51 */
215 #endif
216         atomic_dec(&dev->vma_count);
217
218 #if LINUX_VERSION_CODE >= 0x020300
219         map = vma->vm_private_data;
220 #else
221         map = vma->vm_pte;
222 #endif
223
224         down(&dev->struct_sem);
225         for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
226                 next = pt->next;
227 #if LINUX_VERSION_CODE >= 0x020300
228                 if (pt->vma->vm_private_data == map) found_maps++;
229 #else
230                 if (pt->vma->vm_pte == map) found_maps++;
231 #endif
232                 if (pt->vma == vma) {
233                         if (prev) {
234                                 prev->next = pt->next;
235                         } else {
236                                 dev->vmalist = pt->next;
237                         }
238                         DRM(free)(pt, sizeof(*pt), DRM_MEM_VMAS);
239                 } else {
240                         prev = pt;
241                 }
242         }
243         /* We were the only map that was found */
244         if(found_maps == 1 &&
245            map->flags & _DRM_REMOVABLE) {
246                 /* Check to see if we are in the maplist, if we are not, then
247                  * we delete this mappings information.
248                  */
249                 found_maps = 0;
250                 list = &dev->maplist->head;
251                 list_for_each(list, &dev->maplist->head) {
252                         r_list = (drm_map_list_t *) list;
253                         if (r_list->map == map) found_maps++;
254                 }
255
256                 if(!found_maps) {
257                         switch (map->type) {
258                         case _DRM_REGISTERS:
259                         case _DRM_FRAME_BUFFER:
260 #if __REALLY_HAVE_MTRR
261                                 if (map->mtrr >= 0) {
262                                         int retcode;
263                                         retcode = mtrr_del(map->mtrr,
264                                                            map->offset,
265                                                            map->size);
266                                         DRM_DEBUG("mtrr_del = %d\n", retcode);
267                                 }
268 #endif
269                                 DRM(ioremapfree)(map->handle, map->size);
270                                 break;
271                         case _DRM_SHM:
272                                 vfree(map->handle);
273                                 break;
274                         case _DRM_AGP:
275                         case _DRM_SCATTER_GATHER:
276                                 break;
277                         }
278                         DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
279                 }
280         }
281         up(&dev->struct_sem);
282 }
283
284 #if LINUX_VERSION_CODE < 0x020317
285 unsigned long DRM(vm_dma_nopage)(struct vm_area_struct *vma,
286                                  unsigned long address,
287                                  int unused)
288 #else
289                                 /* Return type changed in 2.3.23 */
290 struct page *DRM(vm_dma_nopage)(struct vm_area_struct *vma,
291                                 unsigned long address,
292                                 int unused)
293 #endif
294 {
295         drm_file_t       *priv   = vma->vm_file->private_data;
296         drm_device_t     *dev    = priv->dev;
297         drm_device_dma_t *dma    = dev->dma;
298         unsigned long    offset;
299         unsigned long    page_nr;
300         struct page      *page;
301
302         if (!dma)                  return NOPAGE_SIGBUS; /* Error */
303         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
304         if (!dma->pagelist)        return NOPAGE_OOM ; /* Nothing allocated */
305
306         offset   = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
307         page_nr  = offset >> PAGE_SHIFT;
308         page = virt_to_page((dma->pagelist[page_nr] + 
309                              (offset & (~PAGE_MASK))));
310
311         get_page(page);
312
313         DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr); 
314 #if LINUX_VERSION_CODE < 0x020317
315         return page_address(page);
316 #else
317         return page;
318 #endif
319 }
320
321 #if LINUX_VERSION_CODE < 0x020317
322 unsigned long DRM(vm_sg_nopage)(struct vm_area_struct *vma,
323                                 unsigned long address,
324                                 int unused)
325 #else
326                                 /* Return type changed in 2.3.23 */
327 struct page *DRM(vm_sg_nopage)(struct vm_area_struct *vma,
328                                unsigned long address,
329                                int unused)
330 #endif
331 {
332 #if LINUX_VERSION_CODE >= 0x020300
333         drm_map_t        *map    = (drm_map_t *)vma->vm_private_data;
334 #else
335         drm_map_t        *map    = (drm_map_t *)vma->vm_pte;
336 #endif
337         drm_file_t *priv = vma->vm_file->private_data;
338         drm_device_t *dev = priv->dev;
339         drm_sg_mem_t *entry = dev->sg;
340         unsigned long offset;
341         unsigned long map_offset;
342         unsigned long page_offset;
343         struct page *page;
344
345         if (!entry)                return NOPAGE_SIGBUS; /* Error */
346         if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
347         if (!entry->pagelist)      return NOPAGE_OOM ;  /* Nothing allocated */
348
349
350         offset = address - vma->vm_start;
351         map_offset = map->offset - dev->sg->handle;
352         page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
353         page = entry->pagelist[page_offset];
354         get_page(page);
355
356 #if LINUX_VERSION_CODE < 0x020317
357         return page_address(page);
358 #else
359         return page;
360 #endif
361 }
362
363 void DRM(vm_open)(struct vm_area_struct *vma)
364 {
365         drm_file_t      *priv   = vma->vm_file->private_data;
366         drm_device_t    *dev    = priv->dev;
367         drm_vma_entry_t *vma_entry;
368
369         DRM_DEBUG("0x%08lx,0x%08lx\n",
370                   vma->vm_start, vma->vm_end - vma->vm_start);
371         atomic_inc(&dev->vma_count);
372 #if LINUX_VERSION_CODE < 0x020333
373                                 /* The map can exist after the fd is closed. */
374         MOD_INC_USE_COUNT; /* Needed before Linux 2.3.51 */
375 #endif
376
377         vma_entry = DRM(alloc)(sizeof(*vma_entry), DRM_MEM_VMAS);
378         if (vma_entry) {
379                 down(&dev->struct_sem);
380                 vma_entry->vma  = vma;
381                 vma_entry->next = dev->vmalist;
382                 vma_entry->pid  = current->pid;
383                 dev->vmalist    = vma_entry;
384                 up(&dev->struct_sem);
385         }
386 }
387
388 void DRM(vm_close)(struct vm_area_struct *vma)
389 {
390         drm_file_t      *priv   = vma->vm_file->private_data;
391         drm_device_t    *dev    = priv->dev;
392         drm_vma_entry_t *pt, *prev;
393
394         DRM_DEBUG("0x%08lx,0x%08lx\n",
395                   vma->vm_start, vma->vm_end - vma->vm_start);
396 #if LINUX_VERSION_CODE < 0x020333
397         MOD_DEC_USE_COUNT; /* Needed before Linux 2.3.51 */
398 #endif
399         atomic_dec(&dev->vma_count);
400
401         down(&dev->struct_sem);
402         for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
403                 if (pt->vma == vma) {
404                         if (prev) {
405                                 prev->next = pt->next;
406                         } else {
407                                 dev->vmalist = pt->next;
408                         }
409                         DRM(free)(pt, sizeof(*pt), DRM_MEM_VMAS);
410                         break;
411                 }
412         }
413         up(&dev->struct_sem);
414 }
415
416 int DRM(mmap_dma)(struct file *filp, struct vm_area_struct *vma)
417 {
418         drm_file_t       *priv   = filp->private_data;
419         drm_device_t     *dev;
420         drm_device_dma_t *dma;
421         unsigned long    length  = vma->vm_end - vma->vm_start;
422
423         lock_kernel();
424         dev      = priv->dev;
425         dma      = dev->dma;
426         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
427                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
428
429                                 /* Length must match exact page count */
430         if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
431                 unlock_kernel();
432                 return -EINVAL;
433         }
434         unlock_kernel();
435
436         vma->vm_ops   = &DRM(vm_dma_ops);
437         vma->vm_flags |= VM_RESERVED; /* Don't swap */
438
439 #if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */
440                                 /* In Linux 2.2.3 and above, this is
441                                    handled in do_mmap() in mm/mmap.c. */
442         ++filp->f_count;
443 #endif
444         vma->vm_file  =  filp;  /* Needed for drm_vm_open() */
445         DRM(vm_open)(vma);
446         return 0;
447 }
448
449 #ifndef DRIVER_GET_MAP_OFS
450 #define DRIVER_GET_MAP_OFS()    (map->offset)
451 #endif
452
453 #ifndef DRIVER_GET_REG_OFS
454 #ifdef __alpha__
455 #define DRIVER_GET_REG_OFS()    (dev->hose->dense_mem_base -    \
456                                  dev->hose->mem_space->start)
457 #else
458 #define DRIVER_GET_REG_OFS()    0
459 #endif
460 #endif
461
462 int DRM(mmap)(struct file *filp, struct vm_area_struct *vma)
463 {
464         drm_file_t      *priv   = filp->private_data;
465         drm_device_t    *dev    = priv->dev;
466         drm_map_t       *map    = NULL;
467         drm_map_list_t  *r_list;
468         unsigned long   offset  = 0;
469         struct list_head *list;
470
471         DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
472                   vma->vm_start, vma->vm_end, VM_OFFSET(vma));
473
474         if ( !priv->authenticated ) return -EACCES;
475
476         if (!VM_OFFSET(vma)) return DRM(mmap_dma)(filp, vma);
477
478                                 /* A sequential search of a linked list is
479                                    fine here because: 1) there will only be
480                                    about 5-10 entries in the list and, 2) a
481                                    DRI client only has to do this mapping
482                                    once, so it doesn't have to be optimized
483                                    for performance, even if the list was a
484                                    bit longer. */
485         list_for_each(list, &dev->maplist->head) {
486                 unsigned long off;
487
488                 r_list = (drm_map_list_t *)list;
489                 map = r_list->map;
490                 if (!map) continue;
491                 off = DRIVER_GET_MAP_OFS();
492                 if (off == VM_OFFSET(vma)) break;
493         }
494
495         if (!map || ((map->flags&_DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
496                 return -EPERM;
497
498                                 /* Check for valid size. */
499         if (map->size != vma->vm_end - vma->vm_start) return -EINVAL;
500
501         if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
502                 vma->vm_flags &= VM_MAYWRITE;
503 #if defined(__i386__)
504                 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
505 #else
506                                 /* Ye gads this is ugly.  With more thought
507                                    we could move this up higher and use
508                                    `protection_map' instead.  */
509                 vma->vm_page_prot = __pgprot(pte_val(pte_wrprotect(
510                         __pte(pgprot_val(vma->vm_page_prot)))));
511 #endif
512         }
513
514         switch (map->type) {
515         case _DRM_AGP:
516 #if defined(__alpha__)
517                 /*
518                  * On Alpha we can't talk to bus dma address from the
519                  * CPU, so for memory of type DRM_AGP, we'll deal with
520                  * sorting out the real physical pages and mappings
521                  * in nopage()
522                  */
523                 vma->vm_ops = &DRM(vm_ops);
524                 break;
525 #endif
526                 /* fall through to _DRM_FRAME_BUFFER... */        
527         case _DRM_FRAME_BUFFER:
528         case _DRM_REGISTERS:
529                 if (VM_OFFSET(vma) >= __pa(high_memory)) {
530 #if defined(__i386__)
531                         if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
532                                 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
533                                 pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
534                         }
535 #elif defined(__ia64__)
536                         if (map->type != _DRM_AGP)
537                                 vma->vm_page_prot =
538                                         pgprot_writecombine(vma->vm_page_prot);
539 #elif defined(__powerpc__)
540                         pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE | _PAGE_GUARDED;
541 #endif
542                         vma->vm_flags |= VM_IO; /* not in core dump */
543                 }
544                 offset = DRIVER_GET_REG_OFS();
545 #ifdef __sparc__
546                 if (io_remap_page_range(vma, vma->vm_start,
547                                         VM_OFFSET(vma) + offset,
548                                         vma->vm_end - vma->vm_start,
549                                         vma->vm_page_prot, 0))
550 #else
551                 if (remap_page_range(vma, vma->vm_start,
552                                      VM_OFFSET(vma) + offset,
553                                      vma->vm_end - vma->vm_start,
554                                      vma->vm_page_prot))
555 #endif
556                                 return -EAGAIN;
557                 DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
558                           " offset = 0x%lx\n",
559                           map->type,
560                           vma->vm_start, vma->vm_end, VM_OFFSET(vma) + offset);
561                 vma->vm_ops = &DRM(vm_ops);
562                 break;
563         case _DRM_SHM:
564                 vma->vm_ops = &DRM(vm_shm_ops);
565 #if LINUX_VERSION_CODE >= 0x020300
566                 vma->vm_private_data = (void *)map;
567 #else
568                 vma->vm_pte = (unsigned long)map;
569 #endif
570                                 /* Don't let this area swap.  Change when
571                                    DRM_KERNEL advisory is supported. */
572                 vma->vm_flags |= VM_RESERVED;
573                 break;
574         case _DRM_SCATTER_GATHER:
575                 vma->vm_ops = &DRM(vm_sg_ops);
576 #if LINUX_VERSION_CODE >= 0x020300
577                 vma->vm_private_data = (void *)map;
578 #else
579                 vma->vm_pte = (unsigned long)map;
580 #endif
581                 vma->vm_flags |= VM_RESERVED;
582                 break;
583         default:
584                 return -EINVAL; /* This should never happen. */
585         }
586         vma->vm_flags |= VM_RESERVED; /* Don't swap */
587
588 #if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */
589                                 /* In Linux 2.2.3 and above, this is
590                                    handled in do_mmap() in mm/mmap.c. */
591         ++filp->f_count;
592 #endif
593         vma->vm_file  =  filp;  /* Needed for drm_vm_open() */
594         DRM(vm_open)(vma);
595         return 0;
596 }