blob: 54a632848955143df4bf30d1e49868366782375d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_vm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Memory mapping for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \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
Dave Airliec94f7022005-07-07 21:03:38 +100041static void drm_vm_open(struct vm_area_struct *vma);
42static void drm_vm_close(struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
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.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100050 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * 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
55static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100056 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 drm_file_t *priv = vma->vm_file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 drm_map_t *map = NULL;
61 drm_map_list_t *r_list;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100062 drm_hash_item_t *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 * Find the right map
66 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (!drm_core_has_AGP(dev))
68 goto vm_nopage_error;
69
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 if (!dev->agp || !dev->agp->cant_use_aperture)
71 goto vm_nopage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Hellstrom15450852007-02-08 16:14:05 +110073 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100074 goto vm_nopage_error;
75
76 r_list = drm_hash_entry(hash, drm_map_list_t, hash);
77 map = r_list->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (map && map->type == _DRM_AGP) {
80 unsigned long offset = address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +100081 unsigned long baddr = map->offset + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct drm_agp_mem *agpmem;
83 struct page *page;
84
85#ifdef __alpha__
86 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087 * Adjust to a bus-relative address
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 baddr -= dev->hose->mem_space->start;
90#endif
91
92 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 * It's AGP memory - find the real physical page to map
94 */
95 for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (agpmem->bound <= baddr &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
100
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 if (!agpmem)
102 goto vm_nopage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 * Get the page, inc the use count, and return it
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
108 page = virt_to_page(__va(agpmem->memory->memory[offset]));
109 get_page(page);
110
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 DRM_DEBUG
112 ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
113 baddr, __va(agpmem->memory->memory[offset]), offset,
114 page_count(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 return page;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 }
118 vm_nopage_error:
119 return NOPAGE_SIGBUS; /* Disallow mremap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121#else /* __OS_HAS_AGP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 return NOPAGE_SIGBUS;
126}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127#endif /* __OS_HAS_AGP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/**
130 * \c nopage method for shared virtual memory.
131 *
132 * \param vma virtual memory area.
133 * \param address access address.
134 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * Get the the mapping, find the real physical page to map, get the page, and
137 * return it.
138 */
139static __inline__ struct page *drm_do_vm_shm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142 drm_map_t *map = (drm_map_t *) vma->vm_private_data;
143 unsigned long offset;
144 unsigned long i;
145 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 if (address > vma->vm_end)
148 return NOPAGE_SIGBUS; /* Disallow mremap */
149 if (!map)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800150 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 offset = address - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 i = (unsigned long)map->handle + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 page = (map->type == _DRM_CONSISTENT) ?
Dave Airlie5fb4dc92005-10-22 15:25:01 +1000155 virt_to_page((void *)i) : vmalloc_to_page((void *)i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!page)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800157 return NOPAGE_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 get_page(page);
159
160 DRM_DEBUG("shm_nopage 0x%lx\n", address);
161 return page;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
165 * \c close method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Deletes map information if we are the last
170 * person to close a mapping and it's not in the global maplist.
171 */
Dave Airliec94f7022005-07-07 21:03:38 +1000172static void drm_vm_shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 drm_file_t *priv = vma->vm_file->private_data;
175 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 drm_vma_entry_t *pt, *prev, *next;
177 drm_map_t *map;
178 drm_map_list_t *r_list;
179 struct list_head *list;
180 int found_maps = 0;
181
182 DRM_DEBUG("0x%08lx,0x%08lx\n",
183 vma->vm_start, vma->vm_end - vma->vm_start);
184 atomic_dec(&dev->vma_count);
185
186 map = vma->vm_private_data;
187
Dave Airlie30e2fb12006-02-02 19:37:46 +1100188 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
190 next = pt->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 if (pt->vma->vm_private_data == map)
192 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (pt->vma == vma) {
194 if (prev) {
195 prev->next = pt->next;
196 } else {
197 dev->vmalist = pt->next;
198 }
199 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
200 } else {
201 prev = pt;
202 }
203 }
204 /* We were the only map that was found */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Check to see if we are in the maplist, if we are not, then
207 * we delete this mappings information.
208 */
209 found_maps = 0;
210 list = &dev->maplist->head;
211 list_for_each(list, &dev->maplist->head) {
212 r_list = list_entry(list, drm_map_list_t, head);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 if (r_list->map == map)
214 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 if (!found_maps) {
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000218 drm_dma_handle_t dmah;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 switch (map->type) {
221 case _DRM_REGISTERS:
222 case _DRM_FRAME_BUFFER:
223 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
224 int retcode;
225 retcode = mtrr_del(map->mtrr,
226 map->offset,
227 map->size);
228 DRM_DEBUG("mtrr_del = %d\n", retcode);
229 }
Christoph Hellwig004a7722007-01-08 21:56:59 +1100230 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case _DRM_SHM:
233 vfree(map->handle);
234 break;
235 case _DRM_AGP:
236 case _DRM_SCATTER_GATHER:
237 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000238 case _DRM_CONSISTENT:
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000239 dmah.vaddr = map->handle;
240 dmah.busaddr = map->offset;
241 dmah.size = map->size;
242 __drm_pci_free(dev, &dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
246 }
247 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100248 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
252 * \c nopage method for DMA virtual memory.
253 *
254 * \param vma virtual memory area.
255 * \param address access address.
256 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
259 */
260static __inline__ struct page *drm_do_vm_dma_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 drm_file_t *priv = vma->vm_file->private_data;
264 drm_device_t *dev = priv->head->dev;
265 drm_device_dma_t *dma = dev->dma;
266 unsigned long offset;
267 unsigned long page_nr;
268 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 if (!dma)
271 return NOPAGE_SIGBUS; /* Error */
272 if (address > vma->vm_end)
273 return NOPAGE_SIGBUS; /* Disallow mremap */
274 if (!dma->pagelist)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800275 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
278 page_nr = offset >> PAGE_SHIFT;
279 page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 get_page(page);
282
283 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
284 return page;
285}
286
287/**
288 * \c nopage method for scatter-gather virtual memory.
289 *
290 * \param vma virtual memory area.
291 * \param address access address.
292 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
295 */
296static __inline__ struct page *drm_do_vm_sg_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 drm_map_t *map = (drm_map_t *) vma->vm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 drm_file_t *priv = vma->vm_file->private_data;
301 drm_device_t *dev = priv->head->dev;
302 drm_sg_mem_t *entry = dev->sg;
303 unsigned long offset;
304 unsigned long map_offset;
305 unsigned long page_offset;
306 struct page *page;
307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 if (!entry)
309 return NOPAGE_SIGBUS; /* Error */
310 if (address > vma->vm_end)
311 return NOPAGE_SIGBUS; /* Disallow mremap */
312 if (!entry->pagelist)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800313 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 offset = address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +1000316 map_offset = map->offset - (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
318 page = entry->pagelist[page_offset];
319 get_page(page);
320
321 return page;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static struct page *drm_vm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 unsigned long address, int *type)
326{
327 if (type)
328 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return drm_do_vm_nopage(vma, address);
330}
331
332static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 unsigned long address, int *type)
334{
335 if (type)
336 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return drm_do_vm_shm_nopage(vma, address);
338}
339
340static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 unsigned long address, int *type)
342{
343 if (type)
344 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return drm_do_vm_dma_nopage(vma, address);
346}
347
348static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 unsigned long address, int *type)
350{
351 if (type)
352 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return drm_do_vm_sg_nopage(vma, address);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/** AGP virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357static struct vm_operations_struct drm_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .nopage = drm_vm_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 .open = drm_vm_open,
360 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
363/** Shared virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364static struct vm_operations_struct drm_vm_shm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .nopage = drm_vm_shm_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 .open = drm_vm_open,
367 .close = drm_vm_shm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370/** DMA virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371static struct vm_operations_struct drm_vm_dma_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .nopage = drm_vm_dma_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 .open = drm_vm_open,
374 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
377/** Scatter-gather virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378static struct vm_operations_struct drm_vm_sg_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .nopage = drm_vm_sg_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 .open = drm_vm_open,
381 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/**
385 * \c open method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * Create a new drm_vma_entry structure as the \p vma private data entry and
390 * add it to drm_device::vmalist.
391 */
Dave Airliec94f7022005-07-07 21:03:38 +1000392static void drm_vm_open(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 drm_file_t *priv = vma->vm_file->private_data;
395 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 drm_vma_entry_t *vma_entry;
397
398 DRM_DEBUG("0x%08lx,0x%08lx\n",
399 vma->vm_start, vma->vm_end - vma->vm_start);
400 atomic_inc(&dev->vma_count);
401
402 vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
403 if (vma_entry) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100404 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 vma_entry->vma = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 vma_entry->next = dev->vmalist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 vma_entry->pid = current->pid;
408 dev->vmalist = vma_entry;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100409 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411}
412
413/**
414 * \c close method for all virtual memory types.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000415 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
419 * free it.
420 */
Dave Airliec94f7022005-07-07 21:03:38 +1000421static void drm_vm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 drm_file_t *priv = vma->vm_file->private_data;
424 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 drm_vma_entry_t *pt, *prev;
426
427 DRM_DEBUG("0x%08lx,0x%08lx\n",
428 vma->vm_start, vma->vm_end - vma->vm_start);
429 atomic_dec(&dev->vma_count);
430
Dave Airlie30e2fb12006-02-02 19:37:46 +1100431 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
433 if (pt->vma == vma) {
434 if (prev) {
435 prev->next = pt->next;
436 } else {
437 dev->vmalist = pt->next;
438 }
439 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
440 break;
441 }
442 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100443 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/**
447 * mmap DMA memory.
448 *
449 * \param filp file pointer.
450 * \param vma virtual memory area.
451 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * Sets the virtual memory area operations structure to vm_dma_ops, the file
454 * pointer, and calls vm_open().
455 */
Dave Airliec94f7022005-07-07 21:03:38 +1000456static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 drm_file_t *priv = filp->private_data;
459 drm_device_t *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 drm_device_dma_t *dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 unsigned long length = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 lock_kernel();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 dev = priv->head->dev;
465 dma = dev->dma;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100466 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
467 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 /* Length must match exact page count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
471 unlock_kernel();
472 return -EINVAL;
473 }
474 unlock_kernel();
475
George Sapountzis3417f332006-10-24 12:03:04 -0700476 if (!capable(CAP_SYS_ADMIN) &&
477 (dma->flags & _DRM_DMA_USE_PCI_RO)) {
478 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
479#if defined(__i386__) || defined(__x86_64__)
480 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
481#else
482 /* Ye gads this is ugly. With more thought
483 we could move this up higher and use
484 `protection_map' instead. */
485 vma->vm_page_prot =
486 __pgprot(pte_val
487 (pte_wrprotect
488 (__pte(pgprot_val(vma->vm_page_prot)))));
489#endif
490 }
491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 vma->vm_ops = &drm_vm_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 vma->vm_flags |= VM_RESERVED; /* Don't swap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496 vma->vm_file = filp; /* Needed for drm_vm_open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 drm_vm_open(vma);
498 return 0;
499}
500
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501unsigned long drm_core_get_map_ofs(drm_map_t * map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 return map->offset;
504}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506EXPORT_SYMBOL(drm_core_get_map_ofs);
507
508unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
509{
510#ifdef __alpha__
511 return dev->hose->dense_mem_base - dev->hose->mem_space->start;
512#else
513 return 0;
514#endif
515}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517EXPORT_SYMBOL(drm_core_get_reg_ofs);
518
519/**
520 * mmap DMA memory.
521 *
522 * \param filp file pointer.
523 * \param vma virtual memory area.
524 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * If the virtual memory area has no offset associated with it then it's a DMA
527 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
528 * checks that the restricted flag is not set, sets the virtual memory operations
529 * according to the mapping type and remaps the pages. Finally sets the file
530 * pointer and calls vm_open().
531 */
532int drm_mmap(struct file *filp, struct vm_area_struct *vma)
533{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 drm_file_t *priv = filp->private_data;
535 drm_device_t *dev = priv->head->dev;
536 drm_map_t *map = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 unsigned long offset = 0;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000538 drm_hash_item_t *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Thomas Hellstrom15450852007-02-08 16:14:05 +1100540 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
541 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 if (!priv->authenticated)
544 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* We check for "dma". On Apple's UniNorth, it's valid to have
547 * the AGP mapped at physical address 0
548 * --BenH.
549 */
Thomas Hellstrom15450852007-02-08 16:14:05 +1100550 if (!vma->vm_pgoff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 && (!dev->agp
553 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555 )
556 return drm_mmap_dma(filp, vma);
557
Thomas Hellstrom15450852007-02-08 16:14:05 +1100558 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000559 DRM_ERROR("Could not find map\n");
560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000563 map = drm_hash_entry(hash, drm_map_list_t, hash)->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EPERM;
566
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 /* Check for valid size. */
568 if (map->size != vma->vm_end - vma->vm_start)
569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
572 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
573#if defined(__i386__) || defined(__x86_64__)
574 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
575#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 /* Ye gads this is ugly. With more thought
577 we could move this up higher and use
578 `protection_map' instead. */
579 vma->vm_page_prot =
580 __pgprot(pte_val
581 (pte_wrprotect
582 (__pte(pgprot_val(vma->vm_page_prot)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583#endif
584 }
585
586 switch (map->type) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 case _DRM_AGP:
588 if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
589 /*
590 * On some platforms we can't talk to bus dma address from the CPU, so for
591 * memory of type DRM_AGP, we'll deal with sorting out the real physical
592 * pages and mappings in nopage()
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#if defined(__powerpc__)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 vma->vm_ops = &drm_vm_ops;
598 break;
599 }
600 /* fall through to _DRM_FRAME_BUFFER... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case _DRM_FRAME_BUFFER:
602 case _DRM_REGISTERS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#if defined(__i386__) || defined(__x86_64__)
Dave Airlie88f399c2005-08-20 17:43:33 +1000604 if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) {
605 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
606 pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Dave Airlie88f399c2005-08-20 17:43:33 +1000608#elif defined(__powerpc__)
609 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
610 if (map->type == _DRM_REGISTERS)
611 pgprot_val(vma->vm_page_prot) |= _PAGE_GUARDED;
612#endif
613 vma->vm_flags |= VM_IO; /* not in core dump */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#if defined(__ia64__)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 if (efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 vma->vm_page_prot =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 pgprot_writecombine(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
621 offset = dev->driver->get_reg_ofs(dev);
622#ifdef __sparc__
David S. Miller14778d92006-03-21 02:29:39 -0800623 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Dave Airlie3d774612006-08-07 20:07:43 +1000624 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 (map->offset + offset) >> PAGE_SHIFT,
626 vma->vm_end - vma->vm_start,
627 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#else
629 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 (map->offset + offset) >> PAGE_SHIFT,
631 vma->vm_end - vma->vm_start,
632 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
636 " offset = 0x%lx\n",
637 map->type,
Dave Airlied1f2b552005-08-05 22:11:22 +1000638 vma->vm_start, vma->vm_end, map->offset + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 vma->vm_ops = &drm_vm_ops;
640 break;
641 case _DRM_SHM:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000642 case _DRM_CONSISTENT:
643 /* Consistent memory is really like shared memory. It's only
644 * allocate in a different way */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 vma->vm_ops = &drm_vm_shm_ops;
646 vma->vm_private_data = (void *)map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 /* Don't let this area swap. Change when
648 DRM_KERNEL advisory is supported. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 case _DRM_SCATTER_GATHER:
652 vma->vm_ops = &drm_vm_sg_ops;
653 vma->vm_private_data = (void *)map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 vma->vm_flags |= VM_RESERVED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 default:
657 return -EINVAL; /* This should never happen. */
658 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 vma->vm_flags |= VM_RESERVED; /* Don't swap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 vma->vm_file = filp; /* Needed for drm_vm_open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 drm_vm_open(vma);
663 return 0;
664}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666EXPORT_SYMBOL(drm_mmap);