1 /* drm_vm.h -- Memory mapping for DRM -*- linux-c -*-
2 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
34 struct vm_operations_struct DRM(vm_ops) = {
35 nopage: DRM(vm_nopage),
40 struct vm_operations_struct DRM(vm_shm_ops) = {
41 nopage: DRM(vm_shm_nopage),
43 close: DRM(vm_shm_close),
46 struct vm_operations_struct DRM(vm_dma_ops) = {
47 nopage: DRM(vm_dma_nopage),
52 struct vm_operations_struct DRM(vm_sg_ops) = {
53 nopage: DRM(vm_sg_nopage),
58 struct page *DRM(vm_nopage)(struct vm_area_struct *vma,
59 unsigned long address,
63 drm_file_t *priv = vma->vm_file->private_data;
64 drm_device_t *dev = priv->dev;
65 drm_map_t *map = NULL;
66 drm_map_list_t *r_list;
67 struct list_head *list;
73 if(!dev->agp || !dev->agp->cant_use_aperture) goto vm_nopage_error;
75 list_for_each(list, &dev->maplist->head) {
76 r_list = (drm_map_list_t *)list;
79 if (map->offset == VM_OFFSET(vma)) break;
82 if (map && map->type == _DRM_AGP) {
83 unsigned long offset = address - vma->vm_start;
84 unsigned long baddr = VM_OFFSET(vma) + offset;
85 struct drm_agp_mem *agpmem;
90 * Adjust to a bus-relative address
92 baddr -= dev->hose->mem_space->start;
96 * It's AGP memory - find the real physical page to map
98 for(agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
99 if (agpmem->bound <= baddr &&
100 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
104 if (!agpmem) goto vm_nopage_error;
107 * Get the page, inc the use count, and return it
109 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
110 agpmem->memory->memory[offset] &= dev->agp->page_mask;
111 page = virt_to_page(__va(agpmem->memory->memory[offset]));
114 DRM_DEBUG("baddr = 0x%lx page = 0x%p, offset = 0x%lx\n",
115 baddr, __va(agpmem->memory->memory[offset]), offset);
120 #endif /* __REALLY_HAVE_AGP */
122 return NOPAGE_SIGBUS; /* Disallow mremap */
125 struct page *DRM(vm_shm_nopage)(struct vm_area_struct *vma,
126 unsigned long address,
129 drm_map_t *map = (drm_map_t *)vma->vm_private_data;
130 unsigned long offset;
134 if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
135 if (!map) return NOPAGE_OOM; /* Nothing allocated */
137 offset = address - vma->vm_start;
138 i = (unsigned long)map->handle + offset;
139 page = vmalloc_to_page((void *)i);
144 DRM_DEBUG("shm_nopage 0x%lx\n", address);
148 /* Special close routine which deletes map information if we are the last
149 * person to close a mapping and its not in the global maplist.
152 void DRM(vm_shm_close)(struct vm_area_struct *vma)
154 drm_file_t *priv = vma->vm_file->private_data;
155 drm_device_t *dev = priv->dev;
156 drm_vma_entry_t *pt, *prev, *next;
158 drm_map_list_t *r_list;
159 struct list_head *list;
162 DRM_DEBUG("0x%08lx,0x%08lx\n",
163 vma->vm_start, vma->vm_end - vma->vm_start);
164 atomic_dec(&dev->vma_count);
166 map = vma->vm_private_data;
168 down(&dev->struct_sem);
169 for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
171 if (pt->vma->vm_private_data == map) found_maps++;
172 if (pt->vma == vma) {
174 prev->next = pt->next;
176 dev->vmalist = pt->next;
178 DRM(free)(pt, sizeof(*pt), DRM_MEM_VMAS);
183 /* We were the only map that was found */
184 if(found_maps == 1 &&
185 map->flags & _DRM_REMOVABLE) {
186 /* Check to see if we are in the maplist, if we are not, then
187 * we delete this mappings information.
190 list = &dev->maplist->head;
191 list_for_each(list, &dev->maplist->head) {
192 r_list = (drm_map_list_t *) list;
193 if (r_list->map == map) found_maps++;
199 case _DRM_FRAME_BUFFER:
200 #if __REALLY_HAVE_MTRR
201 if (map->mtrr >= 0) {
203 retcode = mtrr_del(map->mtrr,
206 DRM_DEBUG("mtrr_del = %d\n", retcode);
209 DRM(ioremapfree)(map->handle, map->size, dev);
215 case _DRM_SCATTER_GATHER:
218 DRM(free)(map, sizeof(*map), DRM_MEM_MAPS);
221 up(&dev->struct_sem);
224 struct page *DRM(vm_dma_nopage)(struct vm_area_struct *vma,
225 unsigned long address,
228 drm_file_t *priv = vma->vm_file->private_data;
229 drm_device_t *dev = priv->dev;
230 drm_device_dma_t *dma = dev->dma;
231 unsigned long offset;
232 unsigned long page_nr;
235 if (!dma) return NOPAGE_SIGBUS; /* Error */
236 if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
237 if (!dma->pagelist) return NOPAGE_OOM ; /* Nothing allocated */
239 offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
240 page_nr = offset >> PAGE_SHIFT;
241 page = virt_to_page((dma->pagelist[page_nr] +
242 (offset & (~PAGE_MASK))));
246 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
250 struct page *DRM(vm_sg_nopage)(struct vm_area_struct *vma,
251 unsigned long address,
254 drm_map_t *map = (drm_map_t *)vma->vm_private_data;
255 drm_file_t *priv = vma->vm_file->private_data;
256 drm_device_t *dev = priv->dev;
257 drm_sg_mem_t *entry = dev->sg;
258 unsigned long offset;
259 unsigned long map_offset;
260 unsigned long page_offset;
263 if (!entry) return NOPAGE_SIGBUS; /* Error */
264 if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */
265 if (!entry->pagelist) return NOPAGE_OOM ; /* Nothing allocated */
268 offset = address - vma->vm_start;
269 map_offset = map->offset - dev->sg->handle;
270 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
271 page = entry->pagelist[page_offset];
277 void DRM(vm_open)(struct vm_area_struct *vma)
279 drm_file_t *priv = vma->vm_file->private_data;
280 drm_device_t *dev = priv->dev;
281 drm_vma_entry_t *vma_entry;
283 DRM_DEBUG("0x%08lx,0x%08lx\n",
284 vma->vm_start, vma->vm_end - vma->vm_start);
285 atomic_inc(&dev->vma_count);
287 vma_entry = DRM(alloc)(sizeof(*vma_entry), DRM_MEM_VMAS);
289 down(&dev->struct_sem);
290 vma_entry->vma = vma;
291 vma_entry->next = dev->vmalist;
292 vma_entry->pid = current->pid;
293 dev->vmalist = vma_entry;
294 up(&dev->struct_sem);
298 void DRM(vm_close)(struct vm_area_struct *vma)
300 drm_file_t *priv = vma->vm_file->private_data;
301 drm_device_t *dev = priv->dev;
302 drm_vma_entry_t *pt, *prev;
304 DRM_DEBUG("0x%08lx,0x%08lx\n",
305 vma->vm_start, vma->vm_end - vma->vm_start);
306 atomic_dec(&dev->vma_count);
308 down(&dev->struct_sem);
309 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
310 if (pt->vma == vma) {
312 prev->next = pt->next;
314 dev->vmalist = pt->next;
316 DRM(free)(pt, sizeof(*pt), DRM_MEM_VMAS);
320 up(&dev->struct_sem);
323 int DRM(mmap_dma)(struct file *filp, struct vm_area_struct *vma)
325 drm_file_t *priv = filp->private_data;
327 drm_device_dma_t *dma;
328 unsigned long length = vma->vm_end - vma->vm_start;
333 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
334 vma->vm_start, vma->vm_end, VM_OFFSET(vma));
336 /* Length must match exact page count */
337 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
343 vma->vm_ops = &DRM(vm_dma_ops);
344 vma->vm_flags |= VM_RESERVED; /* Don't swap */
345 vma->vm_file = filp; /* Needed for drm_vm_open() */
350 #ifndef DRIVER_GET_MAP_OFS
351 #define DRIVER_GET_MAP_OFS() (map->offset)
354 #ifndef DRIVER_GET_REG_OFS
356 #define DRIVER_GET_REG_OFS() (dev->hose->dense_mem_base - \
357 dev->hose->mem_space->start)
359 #define DRIVER_GET_REG_OFS() 0
363 int DRM(mmap)(struct file *filp, struct vm_area_struct *vma)
365 drm_file_t *priv = filp->private_data;
366 drm_device_t *dev = priv->dev;
367 drm_map_t *map = NULL;
368 drm_map_list_t *r_list;
369 unsigned long offset = 0;
370 struct list_head *list;
372 DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
373 vma->vm_start, vma->vm_end, VM_OFFSET(vma));
375 if ( !priv->authenticated ) return -EACCES;
377 if (!VM_OFFSET(vma)) return DRM(mmap_dma)(filp, vma);
379 /* A sequential search of a linked list is
380 fine here because: 1) there will only be
381 about 5-10 entries in the list and, 2) a
382 DRI client only has to do this mapping
383 once, so it doesn't have to be optimized
384 for performance, even if the list was a
386 list_for_each(list, &dev->maplist->head) {
389 r_list = (drm_map_list_t *)list;
392 off = DRIVER_GET_MAP_OFS();
393 if (off == VM_OFFSET(vma)) break;
396 if (!map || ((map->flags&_DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
399 /* Check for valid size. */
400 if (map->size != vma->vm_end - vma->vm_start) return -EINVAL;
402 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
403 vma->vm_flags &= VM_MAYWRITE;
404 #if defined(__i386__) || defined(__x86_64__)
405 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
407 /* Ye gads this is ugly. With more thought
408 we could move this up higher and use
409 `protection_map' instead. */
410 vma->vm_page_prot = __pgprot(pte_val(pte_wrprotect(
411 __pte(pgprot_val(vma->vm_page_prot)))));
417 #if defined(__alpha__)
419 * On Alpha we can't talk to bus dma address from the
420 * CPU, so for memory of type DRM_AGP, we'll deal with
421 * sorting out the real physical pages and mappings
424 vma->vm_ops = &DRM(vm_ops);
427 /* fall through to _DRM_FRAME_BUFFER... */
428 case _DRM_FRAME_BUFFER:
430 if (VM_OFFSET(vma) >= __pa(high_memory)) {
431 #if defined(__i386__) || defined(__x86_64__)
432 if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
433 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
434 pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
436 #elif defined(__ia64__)
437 if (map->type != _DRM_AGP)
439 pgprot_writecombine(vma->vm_page_prot);
440 #elif defined(__powerpc__)
441 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE | _PAGE_GUARDED;
443 vma->vm_flags |= VM_IO; /* not in core dump */
445 offset = DRIVER_GET_REG_OFS();
447 if (io_remap_page_range(DRM_RPR_ARG(vma) vma->vm_start,
448 VM_OFFSET(vma) + offset,
449 vma->vm_end - vma->vm_start,
450 vma->vm_page_prot, 0))
452 if (remap_page_range(DRM_RPR_ARG(vma) vma->vm_start,
453 VM_OFFSET(vma) + offset,
454 vma->vm_end - vma->vm_start,
458 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
461 vma->vm_start, vma->vm_end, VM_OFFSET(vma) + offset);
462 vma->vm_ops = &DRM(vm_ops);
465 vma->vm_ops = &DRM(vm_shm_ops);
466 vma->vm_private_data = (void *)map;
467 /* Don't let this area swap. Change when
468 DRM_KERNEL advisory is supported. */
470 case _DRM_SCATTER_GATHER:
471 vma->vm_ops = &DRM(vm_sg_ops);
472 vma->vm_private_data = (void *)map;
475 return -EINVAL; /* This should never happen. */
477 vma->vm_flags |= VM_RESERVED; /* Don't swap */
479 vma->vm_file = filp; /* Needed for drm_vm_open() */