blob: 239ac33a6e1040ba760a49f6c37a0701cddf45ec [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
Adrian Bunk0bead7c2007-03-11 11:41:16 +110044static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110045{
46 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
47
48#if defined(__i386__) || defined(__x86_64__)
49 if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
50 pgprot_val(tmp) |= _PAGE_PCD;
51 pgprot_val(tmp) &= ~_PAGE_PWT;
52 }
53#elif defined(__powerpc__)
54 pgprot_val(tmp) |= _PAGE_NO_CACHE;
55 if (map_type == _DRM_REGISTERS)
56 pgprot_val(tmp) |= _PAGE_GUARDED;
57#endif
58#if defined(__ia64__)
59 if (efi_range_is_wc(vma->vm_start, vma->vm_end -
60 vma->vm_start))
61 tmp = pgprot_writecombine(tmp);
62 else
63 tmp = pgprot_noncached(tmp);
64#endif
65 return tmp;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/**
69 * \c nopage method for AGP virtual memory.
70 *
71 * \param vma virtual memory area.
72 * \param address access address.
73 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Find the right map and if it's AGP memory find the real physical page to
76 * map, get the page, increment the use count and return it.
77 */
78#if __OS_HAS_AGP
79static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100080 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 drm_file_t *priv = vma->vm_file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 drm_map_t *map = NULL;
85 drm_map_list_t *r_list;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100086 drm_hash_item_t *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +100089 * Find the right map
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (!drm_core_has_AGP(dev))
92 goto vm_nopage_error;
93
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 if (!dev->agp || !dev->agp->cant_use_aperture)
95 goto vm_nopage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Hellstrom15450852007-02-08 16:14:05 +110097 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100098 goto vm_nopage_error;
99
100 r_list = drm_hash_entry(hash, drm_map_list_t, hash);
101 map = r_list->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 if (map && map->type == _DRM_AGP) {
104 unsigned long offset = address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +1000105 unsigned long baddr = map->offset + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct drm_agp_mem *agpmem;
107 struct page *page;
108
109#ifdef __alpha__
110 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000111 * Adjust to a bus-relative address
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 baddr -= dev->hose->mem_space->start;
114#endif
115
116 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 * It's AGP memory - find the real physical page to map
118 */
119 for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (agpmem->bound <= baddr &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 break;
123 }
124
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125 if (!agpmem)
126 goto vm_nopage_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 * Get the page, inc the use count, and return it
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
132 page = virt_to_page(__va(agpmem->memory->memory[offset]));
133 get_page(page);
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 DRM_DEBUG
136 ("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
137 baddr, __va(agpmem->memory->memory[offset]), offset,
138 page_count(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return page;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 }
142 vm_nopage_error:
143 return NOPAGE_SIGBUS; /* Disallow mremap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145#else /* __OS_HAS_AGP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static __inline__ struct page *drm_do_vm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 return NOPAGE_SIGBUS;
150}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151#endif /* __OS_HAS_AGP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/**
154 * \c nopage method for shared virtual memory.
155 *
156 * \param vma virtual memory area.
157 * \param address access address.
158 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * Get the the mapping, find the real physical page to map, get the page, and
161 * return it.
162 */
163static __inline__ struct page *drm_do_vm_shm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 drm_map_t *map = (drm_map_t *) vma->vm_private_data;
167 unsigned long offset;
168 unsigned long i;
169 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 if (address > vma->vm_end)
172 return NOPAGE_SIGBUS; /* Disallow mremap */
173 if (!map)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800174 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 offset = address - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 i = (unsigned long)map->handle + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 page = (map->type == _DRM_CONSISTENT) ?
Dave Airlie5fb4dc92005-10-22 15:25:01 +1000179 virt_to_page((void *)i) : vmalloc_to_page((void *)i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!page)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800181 return NOPAGE_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 get_page(page);
183
184 DRM_DEBUG("shm_nopage 0x%lx\n", address);
185 return page;
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
189 * \c close method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Deletes map information if we are the last
194 * person to close a mapping and it's not in the global maplist.
195 */
Dave Airliec94f7022005-07-07 21:03:38 +1000196static void drm_vm_shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 drm_file_t *priv = vma->vm_file->private_data;
199 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 drm_vma_entry_t *pt, *prev, *next;
201 drm_map_t *map;
202 drm_map_list_t *r_list;
203 struct list_head *list;
204 int found_maps = 0;
205
206 DRM_DEBUG("0x%08lx,0x%08lx\n",
207 vma->vm_start, vma->vm_end - vma->vm_start);
208 atomic_dec(&dev->vma_count);
209
210 map = vma->vm_private_data;
211
Dave Airlie30e2fb12006-02-02 19:37:46 +1100212 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 for (pt = dev->vmalist, prev = NULL; pt; pt = next) {
214 next = pt->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 if (pt->vma->vm_private_data == map)
216 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (pt->vma == vma) {
218 if (prev) {
219 prev->next = pt->next;
220 } else {
221 dev->vmalist = pt->next;
222 }
223 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
224 } else {
225 prev = pt;
226 }
227 }
228 /* We were the only map that was found */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Check to see if we are in the maplist, if we are not, then
231 * we delete this mappings information.
232 */
233 found_maps = 0;
234 list = &dev->maplist->head;
235 list_for_each(list, &dev->maplist->head) {
236 r_list = list_entry(list, drm_map_list_t, head);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 if (r_list->map == map)
238 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (!found_maps) {
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000242 drm_dma_handle_t dmah;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 switch (map->type) {
245 case _DRM_REGISTERS:
246 case _DRM_FRAME_BUFFER:
247 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
248 int retcode;
249 retcode = mtrr_del(map->mtrr,
250 map->offset,
251 map->size);
252 DRM_DEBUG("mtrr_del = %d\n", retcode);
253 }
Christoph Hellwig004a7722007-01-08 21:56:59 +1100254 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 case _DRM_SHM:
257 vfree(map->handle);
258 break;
259 case _DRM_AGP:
260 case _DRM_SCATTER_GATHER:
261 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000262 case _DRM_CONSISTENT:
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000263 dmah.vaddr = map->handle;
264 dmah.busaddr = map->offset;
265 dmah.size = map->size;
266 __drm_pci_free(dev, &dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
270 }
271 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100272 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/**
276 * \c nopage method for DMA virtual memory.
277 *
278 * \param vma virtual memory area.
279 * \param address access address.
280 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
283 */
284static __inline__ struct page *drm_do_vm_dma_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000287 drm_file_t *priv = vma->vm_file->private_data;
288 drm_device_t *dev = priv->head->dev;
289 drm_device_dma_t *dma = dev->dma;
290 unsigned long offset;
291 unsigned long page_nr;
292 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 if (!dma)
295 return NOPAGE_SIGBUS; /* Error */
296 if (address > vma->vm_end)
297 return NOPAGE_SIGBUS; /* Disallow mremap */
298 if (!dma->pagelist)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800299 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */
302 page_nr = offset >> PAGE_SHIFT;
303 page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 get_page(page);
306
307 DRM_DEBUG("dma_nopage 0x%lx (page %lu)\n", address, page_nr);
308 return page;
309}
310
311/**
312 * \c nopage method for scatter-gather virtual memory.
313 *
314 * \param vma virtual memory area.
315 * \param address access address.
316 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
319 */
320static __inline__ struct page *drm_do_vm_sg_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 drm_map_t *map = (drm_map_t *) vma->vm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 drm_file_t *priv = vma->vm_file->private_data;
325 drm_device_t *dev = priv->head->dev;
326 drm_sg_mem_t *entry = dev->sg;
327 unsigned long offset;
328 unsigned long map_offset;
329 unsigned long page_offset;
330 struct page *page;
331
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 if (!entry)
333 return NOPAGE_SIGBUS; /* Error */
334 if (address > vma->vm_end)
335 return NOPAGE_SIGBUS; /* Disallow mremap */
336 if (!entry->pagelist)
Nick Piggincd54e7e2006-12-06 20:31:53 -0800337 return NOPAGE_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 offset = address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +1000340 map_offset = map->offset - (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
342 page = entry->pagelist[page_offset];
343 get_page(page);
344
345 return page;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct page *drm_vm_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_nopage(vma, address);
354}
355
356static struct page *drm_vm_shm_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 unsigned long address, int *type)
358{
359 if (type)
360 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return drm_do_vm_shm_nopage(vma, address);
362}
363
364static struct page *drm_vm_dma_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 unsigned long address, int *type)
366{
367 if (type)
368 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return drm_do_vm_dma_nopage(vma, address);
370}
371
372static struct page *drm_vm_sg_nopage(struct vm_area_struct *vma,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 unsigned long address, int *type)
374{
375 if (type)
376 *type = VM_FAULT_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return drm_do_vm_sg_nopage(vma, address);
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/** AGP virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000381static struct vm_operations_struct drm_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 .nopage = drm_vm_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 .open = drm_vm_open,
384 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
387/** Shared virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388static struct vm_operations_struct drm_vm_shm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .nopage = drm_vm_shm_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 .open = drm_vm_open,
391 .close = drm_vm_shm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};
393
394/** DMA virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395static struct vm_operations_struct drm_vm_dma_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .nopage = drm_vm_dma_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 .open = drm_vm_open,
398 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
401/** Scatter-gather virtual memory operations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402static struct vm_operations_struct drm_vm_sg_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .nopage = drm_vm_sg_nopage,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404 .open = drm_vm_open,
405 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406};
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/**
409 * \c open method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * Create a new drm_vma_entry structure as the \p vma private data entry and
414 * add it to drm_device::vmalist.
415 */
Dave Airliec94f7022005-07-07 21:03:38 +1000416static void drm_vm_open(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 drm_file_t *priv = vma->vm_file->private_data;
419 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 drm_vma_entry_t *vma_entry;
421
422 DRM_DEBUG("0x%08lx,0x%08lx\n",
423 vma->vm_start, vma->vm_end - vma->vm_start);
424 atomic_inc(&dev->vma_count);
425
426 vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
427 if (vma_entry) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100428 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 vma_entry->vma = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 vma_entry->next = dev->vmalist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 vma_entry->pid = current->pid;
432 dev->vmalist = vma_entry;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100433 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435}
436
437/**
438 * \c close method for all virtual memory types.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
443 * free it.
444 */
Dave Airliec94f7022005-07-07 21:03:38 +1000445static void drm_vm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 drm_file_t *priv = vma->vm_file->private_data;
448 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 drm_vma_entry_t *pt, *prev;
450
451 DRM_DEBUG("0x%08lx,0x%08lx\n",
452 vma->vm_start, vma->vm_end - vma->vm_start);
453 atomic_dec(&dev->vma_count);
454
Dave Airlie30e2fb12006-02-02 19:37:46 +1100455 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) {
457 if (pt->vma == vma) {
458 if (prev) {
459 prev->next = pt->next;
460 } else {
461 dev->vmalist = pt->next;
462 }
463 drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
464 break;
465 }
466 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100467 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/**
471 * mmap DMA memory.
472 *
473 * \param filp file pointer.
474 * \param vma virtual memory area.
475 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * Sets the virtual memory area operations structure to vm_dma_ops, the file
478 * pointer, and calls vm_open().
479 */
Dave Airliec94f7022005-07-07 21:03:38 +1000480static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 drm_file_t *priv = filp->private_data;
483 drm_device_t *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 drm_device_dma_t *dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 unsigned long length = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 lock_kernel();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 dev = priv->head->dev;
489 dma = dev->dma;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100490 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
491 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 /* Length must match exact page count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
495 unlock_kernel();
496 return -EINVAL;
497 }
498 unlock_kernel();
499
George Sapountzis3417f332006-10-24 12:03:04 -0700500 if (!capable(CAP_SYS_ADMIN) &&
501 (dma->flags & _DRM_DMA_USE_PCI_RO)) {
502 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
503#if defined(__i386__) || defined(__x86_64__)
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 =
510 __pgprot(pte_val
511 (pte_wrprotect
512 (__pte(pgprot_val(vma->vm_page_prot)))));
513#endif
514 }
515
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000516 vma->vm_ops = &drm_vm_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 vma->vm_flags |= VM_RESERVED; /* Don't swap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 vma->vm_file = filp; /* Needed for drm_vm_open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 drm_vm_open(vma);
522 return 0;
523}
524
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525unsigned long drm_core_get_map_ofs(drm_map_t * map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 return map->offset;
528}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530EXPORT_SYMBOL(drm_core_get_map_ofs);
531
532unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
533{
534#ifdef __alpha__
535 return dev->hose->dense_mem_base - dev->hose->mem_space->start;
536#else
537 return 0;
538#endif
539}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541EXPORT_SYMBOL(drm_core_get_reg_ofs);
542
543/**
544 * mmap DMA memory.
545 *
546 * \param filp file pointer.
547 * \param vma virtual memory area.
548 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * If the virtual memory area has no offset associated with it then it's a DMA
551 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
552 * checks that the restricted flag is not set, sets the virtual memory operations
553 * according to the mapping type and remaps the pages. Finally sets the file
554 * pointer and calls vm_open().
555 */
556int drm_mmap(struct file *filp, struct vm_area_struct *vma)
557{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 drm_file_t *priv = filp->private_data;
559 drm_device_t *dev = priv->head->dev;
560 drm_map_t *map = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561 unsigned long offset = 0;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000562 drm_hash_item_t *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Thomas Hellstrom15450852007-02-08 16:14:05 +1100564 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
565 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 if (!priv->authenticated)
568 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* We check for "dma". On Apple's UniNorth, it's valid to have
571 * the AGP mapped at physical address 0
572 * --BenH.
573 */
Thomas Hellstrom15450852007-02-08 16:14:05 +1100574 if (!vma->vm_pgoff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 && (!dev->agp
577 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579 )
580 return drm_mmap_dma(filp, vma);
581
Thomas Hellstrom15450852007-02-08 16:14:05 +1100582 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000583 DRM_ERROR("Could not find map\n");
584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000587 map = drm_hash_entry(hash, drm_map_list_t, hash)->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return -EPERM;
590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 /* Check for valid size. */
Dave Airlie54ba2f72007-02-10 12:07:47 +1100592 if (map->size < vma->vm_end - vma->vm_start)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
596 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
597#if defined(__i386__) || defined(__x86_64__)
598 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
599#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 /* Ye gads this is ugly. With more thought
601 we could move this up higher and use
602 `protection_map' instead. */
603 vma->vm_page_prot =
604 __pgprot(pte_val
605 (pte_wrprotect
606 (__pte(pgprot_val(vma->vm_page_prot)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
608 }
609
610 switch (map->type) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 case _DRM_AGP:
612 if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
613 /*
614 * On some platforms we can't talk to bus dma address from the CPU, so for
615 * memory of type DRM_AGP, we'll deal with sorting out the real physical
616 * pages and mappings in nopage()
617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#if defined(__powerpc__)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 vma->vm_ops = &drm_vm_ops;
622 break;
623 }
624 /* fall through to _DRM_FRAME_BUFFER... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 case _DRM_FRAME_BUFFER:
626 case _DRM_REGISTERS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 offset = dev->driver->get_reg_ofs(dev);
Dave Airlie5cc7f9a2007-02-10 11:53:13 +1100628 vma->vm_flags |= VM_IO; /* not in core dump */
629 vma->vm_page_prot = drm_io_prot(map->type, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#ifdef __sparc__
David S. Miller14778d92006-03-21 02:29:39 -0800631 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Dave Airlie3d774612006-08-07 20:07:43 +1000632 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 (map->offset + offset) >> PAGE_SHIFT,
634 vma->vm_end - vma->vm_start,
635 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#else
637 if (io_remap_pfn_range(vma, vma->vm_start,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 (map->offset + offset) >> PAGE_SHIFT,
639 vma->vm_end - vma->vm_start,
640 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
644 " offset = 0x%lx\n",
645 map->type,
Dave Airlied1f2b552005-08-05 22:11:22 +1000646 vma->vm_start, vma->vm_end, map->offset + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 vma->vm_ops = &drm_vm_ops;
648 break;
649 case _DRM_SHM:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000650 case _DRM_CONSISTENT:
651 /* Consistent memory is really like shared memory. It's only
652 * allocate in a different way */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 vma->vm_ops = &drm_vm_shm_ops;
654 vma->vm_private_data = (void *)map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 /* Don't let this area swap. Change when
656 DRM_KERNEL advisory is supported. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 vma->vm_flags |= VM_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case _DRM_SCATTER_GATHER:
660 vma->vm_ops = &drm_vm_sg_ops;
661 vma->vm_private_data = (void *)map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 vma->vm_flags |= VM_RESERVED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 default:
665 return -EINVAL; /* This should never happen. */
666 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 vma->vm_flags |= VM_RESERVED; /* Don't swap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 vma->vm_file = filp; /* Needed for drm_vm_open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 drm_vm_open(vma);
671 return 0;
672}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674EXPORT_SYMBOL(drm_mmap);