Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /** |
b5e89ed5 | 2 | * \file drm_vm.c |
1da177e4 | 3 | * Memory mapping for DRM |
b5e89ed5 | 4 | * |
1da177e4 LT |
5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
6 | * \author Gareth Hughes <gareth@valinux.com> | |
7 | */ | |
8 | ||
9 | /* | |
10 | * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com | |
11 | * | |
12 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. | |
13 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | |
14 | * All Rights Reserved. | |
15 | * | |
16 | * Permission is hereby granted, free of charge, to any person obtaining a | |
17 | * copy of this software and associated documentation files (the "Software"), | |
18 | * to deal in the Software without restriction, including without limitation | |
19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
20 | * and/or sell copies of the Software, and to permit persons to whom the | |
21 | * Software is furnished to do so, subject to the following conditions: | |
22 | * | |
23 | * The above copyright notice and this permission notice (including the next | |
24 | * paragraph) shall be included in all copies or substantial portions of the | |
25 | * Software. | |
26 | * | |
27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
30 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
31 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
32 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
33 | * OTHER DEALINGS IN THE SOFTWARE. | |
34 | */ | |
35 | ||
760285e7 | 36 | #include <drm/drmP.h> |
2d1a8a48 | 37 | #include <linux/export.h> |
1da177e4 LT |
38 | #if defined(__ia64__) |
39 | #include <linux/efi.h> | |
5a0e3ad6 | 40 | #include <linux/slab.h> |
1da177e4 LT |
41 | #endif |
42 | ||
c94f7029 DA |
43 | static void drm_vm_open(struct vm_area_struct *vma); |
44 | static void drm_vm_close(struct vm_area_struct *vma); | |
1da177e4 | 45 | |
ff47eaf2 AL |
46 | static pgprot_t drm_io_prot(struct drm_local_map *map, |
47 | struct vm_area_struct *vma) | |
5cc7f9ab DA |
48 | { |
49 | pgprot_t tmp = vm_get_page_prot(vma->vm_flags); | |
50 | ||
51 | #if defined(__i386__) || defined(__x86_64__) | |
f435046d AL |
52 | if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING)) |
53 | tmp = pgprot_noncached(tmp); | |
54 | else | |
55 | tmp = pgprot_writecombine(tmp); | |
5cc7f9ab DA |
56 | #elif defined(__powerpc__) |
57 | pgprot_val(tmp) |= _PAGE_NO_CACHE; | |
ff47eaf2 | 58 | if (map->type == _DRM_REGISTERS) |
5cc7f9ab | 59 | pgprot_val(tmp) |= _PAGE_GUARDED; |
6876b3ba | 60 | #elif defined(__ia64__) |
5cc7f9ab DA |
61 | if (efi_range_is_wc(vma->vm_start, vma->vm_end - |
62 | vma->vm_start)) | |
63 | tmp = pgprot_writecombine(tmp); | |
64 | else | |
65 | tmp = pgprot_noncached(tmp); | |
04cf55e1 | 66 | #elif defined(__sparc__) || defined(__arm__) || defined(__mips__) |
6876b3ba BH |
67 | tmp = pgprot_noncached(tmp); |
68 | #endif | |
69 | return tmp; | |
70 | } | |
71 | ||
72 | static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma) | |
73 | { | |
74 | pgprot_t tmp = vm_get_page_prot(vma->vm_flags); | |
75 | ||
76 | #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE) | |
77 | tmp |= _PAGE_NO_CACHE; | |
5cc7f9ab DA |
78 | #endif |
79 | return tmp; | |
80 | } | |
81 | ||
1da177e4 | 82 | /** |
ca0b07d9 | 83 | * \c fault method for AGP virtual memory. |
1da177e4 LT |
84 | * |
85 | * \param vma virtual memory area. | |
86 | * \param address access address. | |
87 | * \return pointer to the page structure. | |
b5e89ed5 | 88 | * |
1da177e4 LT |
89 | * Find the right map and if it's AGP memory find the real physical page to |
90 | * map, get the page, increment the use count and return it. | |
91 | */ | |
92 | #if __OS_HAS_AGP | |
ca0b07d9 | 93 | static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 94 | { |
84b1fd10 | 95 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 96 | struct drm_device *dev = priv->minor->dev; |
f77d390c | 97 | struct drm_local_map *map = NULL; |
55910517 | 98 | struct drm_map_list *r_list; |
e0be428e | 99 | struct drm_hash_item *hash; |
1da177e4 LT |
100 | |
101 | /* | |
b5e89ed5 DA |
102 | * Find the right map |
103 | */ | |
1da177e4 | 104 | if (!drm_core_has_AGP(dev)) |
ca0b07d9 | 105 | goto vm_fault_error; |
1da177e4 | 106 | |
b5e89ed5 | 107 | if (!dev->agp || !dev->agp->cant_use_aperture) |
ca0b07d9 | 108 | goto vm_fault_error; |
1da177e4 | 109 | |
1545085a | 110 | if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) |
ca0b07d9 | 111 | goto vm_fault_error; |
8d153f71 | 112 | |
55910517 | 113 | r_list = drm_hash_entry(hash, struct drm_map_list, hash); |
8d153f71 | 114 | map = r_list->map; |
1da177e4 LT |
115 | |
116 | if (map && map->type == _DRM_AGP) { | |
ca0b07d9 NP |
117 | /* |
118 | * Using vm_pgoff as a selector forces us to use this unusual | |
119 | * addressing scheme. | |
120 | */ | |
41c2e75e BH |
121 | resource_size_t offset = (unsigned long)vmf->virtual_address - |
122 | vma->vm_start; | |
123 | resource_size_t baddr = map->offset + offset; | |
1da177e4 LT |
124 | struct drm_agp_mem *agpmem; |
125 | struct page *page; | |
126 | ||
127 | #ifdef __alpha__ | |
128 | /* | |
b5e89ed5 DA |
129 | * Adjust to a bus-relative address |
130 | */ | |
1da177e4 LT |
131 | baddr -= dev->hose->mem_space->start; |
132 | #endif | |
133 | ||
134 | /* | |
b5e89ed5 DA |
135 | * It's AGP memory - find the real physical page to map |
136 | */ | |
bd1b331f | 137 | list_for_each_entry(agpmem, &dev->agp->memory, head) { |
1da177e4 | 138 | if (agpmem->bound <= baddr && |
b5e89ed5 | 139 | agpmem->bound + agpmem->pages * PAGE_SIZE > baddr) |
1da177e4 LT |
140 | break; |
141 | } | |
142 | ||
161c4810 | 143 | if (&agpmem->head == &dev->agp->memory) |
ca0b07d9 | 144 | goto vm_fault_error; |
1da177e4 LT |
145 | |
146 | /* | |
b5e89ed5 DA |
147 | * Get the page, inc the use count, and return it |
148 | */ | |
1da177e4 | 149 | offset = (baddr - agpmem->bound) >> PAGE_SHIFT; |
07613ba2 | 150 | page = agpmem->memory->pages[offset]; |
1da177e4 | 151 | get_page(page); |
ca0b07d9 | 152 | vmf->page = page; |
1da177e4 | 153 | |
b5e89ed5 | 154 | DRM_DEBUG |
41c2e75e BH |
155 | ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n", |
156 | (unsigned long long)baddr, | |
07613ba2 | 157 | agpmem->memory->pages[offset], |
41c2e75e | 158 | (unsigned long long)offset, |
b5e89ed5 | 159 | page_count(page)); |
ca0b07d9 | 160 | return 0; |
b5e89ed5 | 161 | } |
ca0b07d9 NP |
162 | vm_fault_error: |
163 | return VM_FAULT_SIGBUS; /* Disallow mremap */ | |
1da177e4 | 164 | } |
b5e89ed5 | 165 | #else /* __OS_HAS_AGP */ |
ca0b07d9 | 166 | static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 167 | { |
ca0b07d9 | 168 | return VM_FAULT_SIGBUS; |
1da177e4 | 169 | } |
b5e89ed5 | 170 | #endif /* __OS_HAS_AGP */ |
1da177e4 LT |
171 | |
172 | /** | |
173 | * \c nopage method for shared virtual memory. | |
174 | * | |
175 | * \param vma virtual memory area. | |
176 | * \param address access address. | |
177 | * \return pointer to the page structure. | |
b5e89ed5 | 178 | * |
59c51591 | 179 | * Get the mapping, find the real physical page to map, get the page, and |
1da177e4 LT |
180 | * return it. |
181 | */ | |
ca0b07d9 | 182 | static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 183 | { |
f77d390c | 184 | struct drm_local_map *map = vma->vm_private_data; |
b5e89ed5 DA |
185 | unsigned long offset; |
186 | unsigned long i; | |
187 | struct page *page; | |
1da177e4 | 188 | |
b5e89ed5 | 189 | if (!map) |
ca0b07d9 | 190 | return VM_FAULT_SIGBUS; /* Nothing allocated */ |
1da177e4 | 191 | |
ca0b07d9 | 192 | offset = (unsigned long)vmf->virtual_address - vma->vm_start; |
1da177e4 | 193 | i = (unsigned long)map->handle + offset; |
38315878 | 194 | page = vmalloc_to_page((void *)i); |
1da177e4 | 195 | if (!page) |
ca0b07d9 | 196 | return VM_FAULT_SIGBUS; |
1da177e4 | 197 | get_page(page); |
ca0b07d9 | 198 | vmf->page = page; |
1da177e4 | 199 | |
ca0b07d9 NP |
200 | DRM_DEBUG("shm_fault 0x%lx\n", offset); |
201 | return 0; | |
1da177e4 LT |
202 | } |
203 | ||
1da177e4 LT |
204 | /** |
205 | * \c close method for shared virtual memory. | |
b5e89ed5 | 206 | * |
1da177e4 | 207 | * \param vma virtual memory area. |
b5e89ed5 | 208 | * |
1da177e4 LT |
209 | * Deletes map information if we are the last |
210 | * person to close a mapping and it's not in the global maplist. | |
211 | */ | |
c94f7029 | 212 | static void drm_vm_shm_close(struct vm_area_struct *vma) |
1da177e4 | 213 | { |
84b1fd10 | 214 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 215 | struct drm_device *dev = priv->minor->dev; |
8fc2fdf4 | 216 | struct drm_vma_entry *pt, *temp; |
f77d390c | 217 | struct drm_local_map *map; |
55910517 | 218 | struct drm_map_list *r_list; |
1da177e4 LT |
219 | int found_maps = 0; |
220 | ||
221 | DRM_DEBUG("0x%08lx,0x%08lx\n", | |
222 | vma->vm_start, vma->vm_end - vma->vm_start); | |
223 | atomic_dec(&dev->vma_count); | |
224 | ||
225 | map = vma->vm_private_data; | |
226 | ||
30e2fb18 | 227 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 228 | list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { |
b5e89ed5 DA |
229 | if (pt->vma->vm_private_data == map) |
230 | found_maps++; | |
1da177e4 | 231 | if (pt->vma == vma) { |
bd1b331f | 232 | list_del(&pt->head); |
9a298b2a | 233 | kfree(pt); |
1da177e4 LT |
234 | } |
235 | } | |
bd1b331f | 236 | |
1da177e4 | 237 | /* We were the only map that was found */ |
b5e89ed5 | 238 | if (found_maps == 1 && map->flags & _DRM_REMOVABLE) { |
1da177e4 LT |
239 | /* Check to see if we are in the maplist, if we are not, then |
240 | * we delete this mappings information. | |
241 | */ | |
242 | found_maps = 0; | |
bd1b331f | 243 | list_for_each_entry(r_list, &dev->maplist, head) { |
b5e89ed5 DA |
244 | if (r_list->map == map) |
245 | found_maps++; | |
1da177e4 LT |
246 | } |
247 | ||
b5e89ed5 | 248 | if (!found_maps) { |
9c8da5eb DA |
249 | drm_dma_handle_t dmah; |
250 | ||
1da177e4 LT |
251 | switch (map->type) { |
252 | case _DRM_REGISTERS: | |
253 | case _DRM_FRAME_BUFFER: | |
28185647 | 254 | arch_phys_wc_del(map->mtrr); |
004a7727 | 255 | iounmap(map->handle); |
1da177e4 LT |
256 | break; |
257 | case _DRM_SHM: | |
258 | vfree(map->handle); | |
259 | break; | |
260 | case _DRM_AGP: | |
261 | case _DRM_SCATTER_GATHER: | |
262 | break; | |
2d0f9eaf | 263 | case _DRM_CONSISTENT: |
9c8da5eb DA |
264 | dmah.vaddr = map->handle; |
265 | dmah.busaddr = map->offset; | |
266 | dmah.size = map->size; | |
267 | __drm_pci_free(dev, &dmah); | |
2d0f9eaf | 268 | break; |
a2c0a97b JB |
269 | case _DRM_GEM: |
270 | DRM_ERROR("tried to rmmap GEM object\n"); | |
271 | break; | |
1da177e4 | 272 | } |
9a298b2a | 273 | kfree(map); |
1da177e4 LT |
274 | } |
275 | } | |
30e2fb18 | 276 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
277 | } |
278 | ||
279 | /** | |
ca0b07d9 | 280 | * \c fault method for DMA virtual memory. |
1da177e4 LT |
281 | * |
282 | * \param vma virtual memory area. | |
283 | * \param address access address. | |
284 | * \return pointer to the page structure. | |
b5e89ed5 | 285 | * |
1da177e4 LT |
286 | * Determine the page number from the page offset and get it from drm_device_dma::pagelist. |
287 | */ | |
ca0b07d9 | 288 | static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 289 | { |
84b1fd10 | 290 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 291 | struct drm_device *dev = priv->minor->dev; |
cdd55a29 | 292 | struct drm_device_dma *dma = dev->dma; |
b5e89ed5 DA |
293 | unsigned long offset; |
294 | unsigned long page_nr; | |
295 | struct page *page; | |
296 | ||
297 | if (!dma) | |
ca0b07d9 | 298 | return VM_FAULT_SIGBUS; /* Error */ |
b5e89ed5 | 299 | if (!dma->pagelist) |
ca0b07d9 | 300 | return VM_FAULT_SIGBUS; /* Nothing allocated */ |
b5e89ed5 | 301 | |
ca0b07d9 NP |
302 | offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */ |
303 | page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */ | |
b5e89ed5 | 304 | page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK)))); |
1da177e4 LT |
305 | |
306 | get_page(page); | |
ca0b07d9 | 307 | vmf->page = page; |
1da177e4 | 308 | |
ca0b07d9 NP |
309 | DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr); |
310 | return 0; | |
1da177e4 LT |
311 | } |
312 | ||
313 | /** | |
ca0b07d9 | 314 | * \c fault method for scatter-gather virtual memory. |
1da177e4 LT |
315 | * |
316 | * \param vma virtual memory area. | |
317 | * \param address access address. | |
318 | * \return pointer to the page structure. | |
b5e89ed5 | 319 | * |
1da177e4 LT |
320 | * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist. |
321 | */ | |
ca0b07d9 | 322 | static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 323 | { |
f77d390c | 324 | struct drm_local_map *map = vma->vm_private_data; |
84b1fd10 | 325 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 326 | struct drm_device *dev = priv->minor->dev; |
55910517 | 327 | struct drm_sg_mem *entry = dev->sg; |
1da177e4 LT |
328 | unsigned long offset; |
329 | unsigned long map_offset; | |
330 | unsigned long page_offset; | |
331 | struct page *page; | |
332 | ||
b5e89ed5 | 333 | if (!entry) |
ca0b07d9 | 334 | return VM_FAULT_SIGBUS; /* Error */ |
b5e89ed5 | 335 | if (!entry->pagelist) |
ca0b07d9 | 336 | return VM_FAULT_SIGBUS; /* Nothing allocated */ |
1da177e4 | 337 | |
ca0b07d9 | 338 | offset = (unsigned long)vmf->virtual_address - vma->vm_start; |
d1f2b55a | 339 | map_offset = map->offset - (unsigned long)dev->sg->virtual; |
1da177e4 LT |
340 | page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT); |
341 | page = entry->pagelist[page_offset]; | |
342 | get_page(page); | |
ca0b07d9 | 343 | vmf->page = page; |
1da177e4 | 344 | |
ca0b07d9 | 345 | return 0; |
1da177e4 LT |
346 | } |
347 | ||
ca0b07d9 | 348 | static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 349 | { |
ca0b07d9 | 350 | return drm_do_vm_fault(vma, vmf); |
1da177e4 LT |
351 | } |
352 | ||
ca0b07d9 | 353 | static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 354 | { |
ca0b07d9 | 355 | return drm_do_vm_shm_fault(vma, vmf); |
1da177e4 LT |
356 | } |
357 | ||
ca0b07d9 | 358 | static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 359 | { |
ca0b07d9 | 360 | return drm_do_vm_dma_fault(vma, vmf); |
1da177e4 LT |
361 | } |
362 | ||
ca0b07d9 | 363 | static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 364 | { |
ca0b07d9 | 365 | return drm_do_vm_sg_fault(vma, vmf); |
1da177e4 LT |
366 | } |
367 | ||
1da177e4 | 368 | /** AGP virtual memory operations */ |
f0f37e2f | 369 | static const struct vm_operations_struct drm_vm_ops = { |
ca0b07d9 | 370 | .fault = drm_vm_fault, |
b5e89ed5 DA |
371 | .open = drm_vm_open, |
372 | .close = drm_vm_close, | |
1da177e4 LT |
373 | }; |
374 | ||
375 | /** Shared virtual memory operations */ | |
f0f37e2f | 376 | static const struct vm_operations_struct drm_vm_shm_ops = { |
ca0b07d9 | 377 | .fault = drm_vm_shm_fault, |
b5e89ed5 DA |
378 | .open = drm_vm_open, |
379 | .close = drm_vm_shm_close, | |
1da177e4 LT |
380 | }; |
381 | ||
382 | /** DMA virtual memory operations */ | |
f0f37e2f | 383 | static const struct vm_operations_struct drm_vm_dma_ops = { |
ca0b07d9 | 384 | .fault = drm_vm_dma_fault, |
b5e89ed5 DA |
385 | .open = drm_vm_open, |
386 | .close = drm_vm_close, | |
1da177e4 LT |
387 | }; |
388 | ||
389 | /** Scatter-gather virtual memory operations */ | |
f0f37e2f | 390 | static const struct vm_operations_struct drm_vm_sg_ops = { |
ca0b07d9 | 391 | .fault = drm_vm_sg_fault, |
b5e89ed5 DA |
392 | .open = drm_vm_open, |
393 | .close = drm_vm_close, | |
1da177e4 LT |
394 | }; |
395 | ||
1da177e4 LT |
396 | /** |
397 | * \c open method for shared virtual memory. | |
b5e89ed5 | 398 | * |
1da177e4 | 399 | * \param vma virtual memory area. |
b5e89ed5 | 400 | * |
1da177e4 LT |
401 | * Create a new drm_vma_entry structure as the \p vma private data entry and |
402 | * add it to drm_device::vmalist. | |
403 | */ | |
b06d66be RC |
404 | void drm_vm_open_locked(struct drm_device *dev, |
405 | struct vm_area_struct *vma) | |
1da177e4 | 406 | { |
8fc2fdf4 | 407 | struct drm_vma_entry *vma_entry; |
1da177e4 LT |
408 | |
409 | DRM_DEBUG("0x%08lx,0x%08lx\n", | |
410 | vma->vm_start, vma->vm_end - vma->vm_start); | |
411 | atomic_inc(&dev->vma_count); | |
412 | ||
9a298b2a | 413 | vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL); |
1da177e4 | 414 | if (vma_entry) { |
b5e89ed5 | 415 | vma_entry->vma = vma; |
b5e89ed5 | 416 | vma_entry->pid = current->pid; |
bd1b331f | 417 | list_add(&vma_entry->head, &dev->vmalist); |
1da177e4 LT |
418 | } |
419 | } | |
d5028995 | 420 | EXPORT_SYMBOL_GPL(drm_vm_open_locked); |
1da177e4 | 421 | |
d7d8aac7 TH |
422 | static void drm_vm_open(struct vm_area_struct *vma) |
423 | { | |
84b1fd10 | 424 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 425 | struct drm_device *dev = priv->minor->dev; |
d7d8aac7 TH |
426 | |
427 | mutex_lock(&dev->struct_mutex); | |
b06d66be | 428 | drm_vm_open_locked(dev, vma); |
d7d8aac7 TH |
429 | mutex_unlock(&dev->struct_mutex); |
430 | } | |
431 | ||
b06d66be RC |
432 | void drm_vm_close_locked(struct drm_device *dev, |
433 | struct vm_area_struct *vma) | |
1da177e4 | 434 | { |
8fc2fdf4 | 435 | struct drm_vma_entry *pt, *temp; |
1da177e4 LT |
436 | |
437 | DRM_DEBUG("0x%08lx,0x%08lx\n", | |
438 | vma->vm_start, vma->vm_end - vma->vm_start); | |
439 | atomic_dec(&dev->vma_count); | |
440 | ||
bd1b331f | 441 | list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { |
1da177e4 | 442 | if (pt->vma == vma) { |
bd1b331f | 443 | list_del(&pt->head); |
9a298b2a | 444 | kfree(pt); |
1da177e4 LT |
445 | break; |
446 | } | |
447 | } | |
31dfbc93 CW |
448 | } |
449 | ||
450 | /** | |
451 | * \c close method for all virtual memory types. | |
452 | * | |
453 | * \param vma virtual memory area. | |
454 | * | |
455 | * Search the \p vma private data entry in drm_device::vmalist, unlink it, and | |
456 | * free it. | |
457 | */ | |
458 | static void drm_vm_close(struct vm_area_struct *vma) | |
459 | { | |
460 | struct drm_file *priv = vma->vm_file->private_data; | |
461 | struct drm_device *dev = priv->minor->dev; | |
462 | ||
463 | mutex_lock(&dev->struct_mutex); | |
b06d66be | 464 | drm_vm_close_locked(dev, vma); |
30e2fb18 | 465 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
466 | } |
467 | ||
468 | /** | |
469 | * mmap DMA memory. | |
470 | * | |
6c340eac | 471 | * \param file_priv DRM file private. |
1da177e4 LT |
472 | * \param vma virtual memory area. |
473 | * \return zero on success or a negative number on failure. | |
b5e89ed5 | 474 | * |
1da177e4 LT |
475 | * Sets the virtual memory area operations structure to vm_dma_ops, the file |
476 | * pointer, and calls vm_open(). | |
477 | */ | |
c94f7029 | 478 | static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma) |
1da177e4 | 479 | { |
84b1fd10 DA |
480 | struct drm_file *priv = filp->private_data; |
481 | struct drm_device *dev; | |
cdd55a29 | 482 | struct drm_device_dma *dma; |
b5e89ed5 | 483 | unsigned long length = vma->vm_end - vma->vm_start; |
1da177e4 | 484 | |
2c14f28b | 485 | dev = priv->minor->dev; |
b5e89ed5 | 486 | dma = dev->dma; |
1545085a TH |
487 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", |
488 | vma->vm_start, vma->vm_end, vma->vm_pgoff); | |
1da177e4 | 489 | |
b5e89ed5 | 490 | /* Length must match exact page count */ |
1da177e4 | 491 | if (!dma || (length >> PAGE_SHIFT) != dma->page_count) { |
1da177e4 LT |
492 | return -EINVAL; |
493 | } | |
1da177e4 | 494 | |
3417f33e GS |
495 | if (!capable(CAP_SYS_ADMIN) && |
496 | (dma->flags & _DRM_DMA_USE_PCI_RO)) { | |
497 | vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); | |
498 | #if defined(__i386__) || defined(__x86_64__) | |
499 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; | |
500 | #else | |
501 | /* Ye gads this is ugly. With more thought | |
502 | we could move this up higher and use | |
503 | `protection_map' instead. */ | |
504 | vma->vm_page_prot = | |
505 | __pgprot(pte_val | |
506 | (pte_wrprotect | |
507 | (__pte(pgprot_val(vma->vm_page_prot))))); | |
508 | #endif | |
509 | } | |
510 | ||
b5e89ed5 | 511 | vma->vm_ops = &drm_vm_dma_ops; |
1da177e4 | 512 | |
314e51b9 | 513 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 | 514 | |
b06d66be | 515 | drm_vm_open_locked(dev, vma); |
1da177e4 LT |
516 | return 0; |
517 | } | |
518 | ||
cbc60ca0 | 519 | static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev) |
1da177e4 LT |
520 | { |
521 | #ifdef __alpha__ | |
82ba3fef | 522 | return dev->hose->dense_mem_base; |
1da177e4 LT |
523 | #else |
524 | return 0; | |
525 | #endif | |
526 | } | |
b5e89ed5 | 527 | |
1da177e4 LT |
528 | /** |
529 | * mmap DMA memory. | |
530 | * | |
6c340eac | 531 | * \param file_priv DRM file private. |
1da177e4 LT |
532 | * \param vma virtual memory area. |
533 | * \return zero on success or a negative number on failure. | |
b5e89ed5 | 534 | * |
1da177e4 LT |
535 | * If the virtual memory area has no offset associated with it then it's a DMA |
536 | * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist, | |
537 | * checks that the restricted flag is not set, sets the virtual memory operations | |
538 | * according to the mapping type and remaps the pages. Finally sets the file | |
539 | * pointer and calls vm_open(). | |
540 | */ | |
a2c0a97b | 541 | int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma) |
1da177e4 | 542 | { |
84b1fd10 | 543 | struct drm_file *priv = filp->private_data; |
2c14f28b | 544 | struct drm_device *dev = priv->minor->dev; |
f77d390c | 545 | struct drm_local_map *map = NULL; |
41c2e75e | 546 | resource_size_t offset = 0; |
e0be428e | 547 | struct drm_hash_item *hash; |
1da177e4 | 548 | |
1545085a TH |
549 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", |
550 | vma->vm_start, vma->vm_end, vma->vm_pgoff); | |
1da177e4 | 551 | |
b5e89ed5 DA |
552 | if (!priv->authenticated) |
553 | return -EACCES; | |
1da177e4 LT |
554 | |
555 | /* We check for "dma". On Apple's UniNorth, it's valid to have | |
556 | * the AGP mapped at physical address 0 | |
557 | * --BenH. | |
558 | */ | |
1545085a | 559 | if (!vma->vm_pgoff |
1da177e4 | 560 | #if __OS_HAS_AGP |
b5e89ed5 DA |
561 | && (!dev->agp |
562 | || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE) | |
1da177e4 LT |
563 | #endif |
564 | ) | |
565 | return drm_mmap_dma(filp, vma); | |
566 | ||
1545085a | 567 | if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) { |
8d153f71 TH |
568 | DRM_ERROR("Could not find map\n"); |
569 | return -EINVAL; | |
1da177e4 LT |
570 | } |
571 | ||
55910517 | 572 | map = drm_hash_entry(hash, struct drm_map_list, hash)->map; |
b5e89ed5 | 573 | if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) |
1da177e4 LT |
574 | return -EPERM; |
575 | ||
b5e89ed5 | 576 | /* Check for valid size. */ |
54ba2f76 | 577 | if (map->size < vma->vm_end - vma->vm_start) |
b5e89ed5 | 578 | return -EINVAL; |
1da177e4 LT |
579 | |
580 | if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) { | |
581 | vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); | |
582 | #if defined(__i386__) || defined(__x86_64__) | |
583 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; | |
584 | #else | |
b5e89ed5 DA |
585 | /* Ye gads this is ugly. With more thought |
586 | we could move this up higher and use | |
587 | `protection_map' instead. */ | |
588 | vma->vm_page_prot = | |
589 | __pgprot(pte_val | |
590 | (pte_wrprotect | |
591 | (__pte(pgprot_val(vma->vm_page_prot))))); | |
1da177e4 LT |
592 | #endif |
593 | } | |
594 | ||
595 | switch (map->type) { | |
4b7fb9b5 | 596 | #if !defined(__arm__) |
b5e89ed5 DA |
597 | case _DRM_AGP: |
598 | if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) { | |
599 | /* | |
600 | * On some platforms we can't talk to bus dma address from the CPU, so for | |
601 | * memory of type DRM_AGP, we'll deal with sorting out the real physical | |
ca0b07d9 | 602 | * pages and mappings in fault() |
b5e89ed5 | 603 | */ |
1da177e4 | 604 | #if defined(__powerpc__) |
b5e89ed5 | 605 | pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE; |
1da177e4 | 606 | #endif |
b5e89ed5 DA |
607 | vma->vm_ops = &drm_vm_ops; |
608 | break; | |
609 | } | |
610 | /* fall through to _DRM_FRAME_BUFFER... */ | |
4b7fb9b5 | 611 | #endif |
1da177e4 LT |
612 | case _DRM_FRAME_BUFFER: |
613 | case _DRM_REGISTERS: | |
cbc60ca0 | 614 | offset = drm_core_get_reg_ofs(dev); |
ff47eaf2 | 615 | vma->vm_page_prot = drm_io_prot(map, vma); |
3d77461e | 616 | if (io_remap_pfn_range(vma, vma->vm_start, |
b5e89ed5 DA |
617 | (map->offset + offset) >> PAGE_SHIFT, |
618 | vma->vm_end - vma->vm_start, | |
619 | vma->vm_page_prot)) | |
b5e89ed5 | 620 | return -EAGAIN; |
1da177e4 | 621 | DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx," |
41c2e75e | 622 | " offset = 0x%llx\n", |
1da177e4 | 623 | map->type, |
41c2e75e | 624 | vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset)); |
4b7fb9b5 | 625 | |
1da177e4 LT |
626 | vma->vm_ops = &drm_vm_ops; |
627 | break; | |
2d0f9eaf | 628 | case _DRM_CONSISTENT: |
38315878 | 629 | /* Consistent memory is really like shared memory. But |
ca0b07d9 | 630 | * it's allocated in a different way, so avoid fault */ |
38315878 HD |
631 | if (remap_pfn_range(vma, vma->vm_start, |
632 | page_to_pfn(virt_to_page(map->handle)), | |
633 | vma->vm_end - vma->vm_start, vma->vm_page_prot)) | |
634 | return -EAGAIN; | |
6876b3ba | 635 | vma->vm_page_prot = drm_dma_prot(map->type, vma); |
38315878 HD |
636 | /* fall through to _DRM_SHM */ |
637 | case _DRM_SHM: | |
1da177e4 LT |
638 | vma->vm_ops = &drm_vm_shm_ops; |
639 | vma->vm_private_data = (void *)map; | |
1da177e4 LT |
640 | break; |
641 | case _DRM_SCATTER_GATHER: | |
642 | vma->vm_ops = &drm_vm_sg_ops; | |
643 | vma->vm_private_data = (void *)map; | |
5ff64611 | 644 | vma->vm_page_prot = drm_dma_prot(map->type, vma); |
b5e89ed5 | 645 | break; |
1da177e4 LT |
646 | default: |
647 | return -EINVAL; /* This should never happen. */ | |
648 | } | |
314e51b9 | 649 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 | 650 | |
b06d66be | 651 | drm_vm_open_locked(dev, vma); |
1da177e4 LT |
652 | return 0; |
653 | } | |
b5e89ed5 | 654 | |
d7d8aac7 TH |
655 | int drm_mmap(struct file *filp, struct vm_area_struct *vma) |
656 | { | |
84b1fd10 | 657 | struct drm_file *priv = filp->private_data; |
2c14f28b | 658 | struct drm_device *dev = priv->minor->dev; |
d7d8aac7 TH |
659 | int ret; |
660 | ||
2c07a21d DA |
661 | if (drm_device_is_unplugged(dev)) |
662 | return -ENODEV; | |
663 | ||
d7d8aac7 TH |
664 | mutex_lock(&dev->struct_mutex); |
665 | ret = drm_mmap_locked(filp, vma); | |
666 | mutex_unlock(&dev->struct_mutex); | |
667 | ||
668 | return ret; | |
669 | } | |
1da177e4 | 670 | EXPORT_SYMBOL(drm_mmap); |