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 | */ | |
d9906753 | 104 | if (!dev->agp) |
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); | |
1da177e4 LT |
223 | |
224 | map = vma->vm_private_data; | |
225 | ||
30e2fb18 | 226 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 227 | list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { |
b5e89ed5 DA |
228 | if (pt->vma->vm_private_data == map) |
229 | found_maps++; | |
1da177e4 | 230 | if (pt->vma == vma) { |
bd1b331f | 231 | list_del(&pt->head); |
9a298b2a | 232 | kfree(pt); |
1da177e4 LT |
233 | } |
234 | } | |
bd1b331f | 235 | |
1da177e4 | 236 | /* We were the only map that was found */ |
b5e89ed5 | 237 | if (found_maps == 1 && map->flags & _DRM_REMOVABLE) { |
1da177e4 LT |
238 | /* Check to see if we are in the maplist, if we are not, then |
239 | * we delete this mappings information. | |
240 | */ | |
241 | found_maps = 0; | |
bd1b331f | 242 | list_for_each_entry(r_list, &dev->maplist, head) { |
b5e89ed5 DA |
243 | if (r_list->map == map) |
244 | found_maps++; | |
1da177e4 LT |
245 | } |
246 | ||
b5e89ed5 | 247 | if (!found_maps) { |
9c8da5eb DA |
248 | drm_dma_handle_t dmah; |
249 | ||
1da177e4 LT |
250 | switch (map->type) { |
251 | case _DRM_REGISTERS: | |
252 | case _DRM_FRAME_BUFFER: | |
28185647 | 253 | arch_phys_wc_del(map->mtrr); |
004a7727 | 254 | iounmap(map->handle); |
1da177e4 LT |
255 | break; |
256 | case _DRM_SHM: | |
257 | vfree(map->handle); | |
258 | break; | |
259 | case _DRM_AGP: | |
260 | case _DRM_SCATTER_GATHER: | |
261 | break; | |
2d0f9eaf | 262 | case _DRM_CONSISTENT: |
9c8da5eb DA |
263 | dmah.vaddr = map->handle; |
264 | dmah.busaddr = map->offset; | |
265 | dmah.size = map->size; | |
266 | __drm_pci_free(dev, &dmah); | |
2d0f9eaf | 267 | break; |
1da177e4 | 268 | } |
9a298b2a | 269 | kfree(map); |
1da177e4 LT |
270 | } |
271 | } | |
30e2fb18 | 272 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
273 | } |
274 | ||
275 | /** | |
ca0b07d9 | 276 | * \c fault method for DMA virtual memory. |
1da177e4 LT |
277 | * |
278 | * \param vma virtual memory area. | |
279 | * \param address access address. | |
280 | * \return pointer to the page structure. | |
b5e89ed5 | 281 | * |
1da177e4 LT |
282 | * Determine the page number from the page offset and get it from drm_device_dma::pagelist. |
283 | */ | |
ca0b07d9 | 284 | static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 285 | { |
84b1fd10 | 286 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 287 | struct drm_device *dev = priv->minor->dev; |
cdd55a29 | 288 | struct drm_device_dma *dma = dev->dma; |
b5e89ed5 DA |
289 | unsigned long offset; |
290 | unsigned long page_nr; | |
291 | struct page *page; | |
292 | ||
293 | if (!dma) | |
ca0b07d9 | 294 | return VM_FAULT_SIGBUS; /* Error */ |
b5e89ed5 | 295 | if (!dma->pagelist) |
ca0b07d9 | 296 | return VM_FAULT_SIGBUS; /* Nothing allocated */ |
b5e89ed5 | 297 | |
ca0b07d9 NP |
298 | offset = (unsigned long)vmf->virtual_address - vma->vm_start; /* vm_[pg]off[set] should be 0 */ |
299 | page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */ | |
e1e78533 | 300 | page = virt_to_page((void *)dma->pagelist[page_nr]); |
1da177e4 LT |
301 | |
302 | get_page(page); | |
ca0b07d9 | 303 | vmf->page = page; |
1da177e4 | 304 | |
ca0b07d9 NP |
305 | DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr); |
306 | return 0; | |
1da177e4 LT |
307 | } |
308 | ||
309 | /** | |
ca0b07d9 | 310 | * \c fault method for scatter-gather virtual memory. |
1da177e4 LT |
311 | * |
312 | * \param vma virtual memory area. | |
313 | * \param address access address. | |
314 | * \return pointer to the page structure. | |
b5e89ed5 | 315 | * |
1da177e4 LT |
316 | * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist. |
317 | */ | |
ca0b07d9 | 318 | static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 319 | { |
f77d390c | 320 | struct drm_local_map *map = vma->vm_private_data; |
84b1fd10 | 321 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 322 | struct drm_device *dev = priv->minor->dev; |
55910517 | 323 | struct drm_sg_mem *entry = dev->sg; |
1da177e4 LT |
324 | unsigned long offset; |
325 | unsigned long map_offset; | |
326 | unsigned long page_offset; | |
327 | struct page *page; | |
328 | ||
b5e89ed5 | 329 | if (!entry) |
ca0b07d9 | 330 | return VM_FAULT_SIGBUS; /* Error */ |
b5e89ed5 | 331 | if (!entry->pagelist) |
ca0b07d9 | 332 | return VM_FAULT_SIGBUS; /* Nothing allocated */ |
1da177e4 | 333 | |
ca0b07d9 | 334 | offset = (unsigned long)vmf->virtual_address - vma->vm_start; |
d1f2b55a | 335 | map_offset = map->offset - (unsigned long)dev->sg->virtual; |
1da177e4 LT |
336 | page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT); |
337 | page = entry->pagelist[page_offset]; | |
338 | get_page(page); | |
ca0b07d9 | 339 | vmf->page = page; |
1da177e4 | 340 | |
ca0b07d9 | 341 | return 0; |
1da177e4 LT |
342 | } |
343 | ||
ca0b07d9 | 344 | static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 345 | { |
ca0b07d9 | 346 | return drm_do_vm_fault(vma, vmf); |
1da177e4 LT |
347 | } |
348 | ||
ca0b07d9 | 349 | static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 350 | { |
ca0b07d9 | 351 | return drm_do_vm_shm_fault(vma, vmf); |
1da177e4 LT |
352 | } |
353 | ||
ca0b07d9 | 354 | static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 355 | { |
ca0b07d9 | 356 | return drm_do_vm_dma_fault(vma, vmf); |
1da177e4 LT |
357 | } |
358 | ||
ca0b07d9 | 359 | static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
b5e89ed5 | 360 | { |
ca0b07d9 | 361 | return drm_do_vm_sg_fault(vma, vmf); |
1da177e4 LT |
362 | } |
363 | ||
1da177e4 | 364 | /** AGP virtual memory operations */ |
f0f37e2f | 365 | static const struct vm_operations_struct drm_vm_ops = { |
ca0b07d9 | 366 | .fault = drm_vm_fault, |
b5e89ed5 DA |
367 | .open = drm_vm_open, |
368 | .close = drm_vm_close, | |
1da177e4 LT |
369 | }; |
370 | ||
371 | /** Shared virtual memory operations */ | |
f0f37e2f | 372 | static const struct vm_operations_struct drm_vm_shm_ops = { |
ca0b07d9 | 373 | .fault = drm_vm_shm_fault, |
b5e89ed5 DA |
374 | .open = drm_vm_open, |
375 | .close = drm_vm_shm_close, | |
1da177e4 LT |
376 | }; |
377 | ||
378 | /** DMA virtual memory operations */ | |
f0f37e2f | 379 | static const struct vm_operations_struct drm_vm_dma_ops = { |
ca0b07d9 | 380 | .fault = drm_vm_dma_fault, |
b5e89ed5 DA |
381 | .open = drm_vm_open, |
382 | .close = drm_vm_close, | |
1da177e4 LT |
383 | }; |
384 | ||
385 | /** Scatter-gather virtual memory operations */ | |
f0f37e2f | 386 | static const struct vm_operations_struct drm_vm_sg_ops = { |
ca0b07d9 | 387 | .fault = drm_vm_sg_fault, |
b5e89ed5 DA |
388 | .open = drm_vm_open, |
389 | .close = drm_vm_close, | |
1da177e4 LT |
390 | }; |
391 | ||
1da177e4 LT |
392 | /** |
393 | * \c open method for shared virtual memory. | |
b5e89ed5 | 394 | * |
1da177e4 | 395 | * \param vma virtual memory area. |
b5e89ed5 | 396 | * |
1da177e4 LT |
397 | * Create a new drm_vma_entry structure as the \p vma private data entry and |
398 | * add it to drm_device::vmalist. | |
399 | */ | |
b06d66be RC |
400 | void drm_vm_open_locked(struct drm_device *dev, |
401 | struct vm_area_struct *vma) | |
1da177e4 | 402 | { |
8fc2fdf4 | 403 | struct drm_vma_entry *vma_entry; |
1da177e4 LT |
404 | |
405 | DRM_DEBUG("0x%08lx,0x%08lx\n", | |
406 | vma->vm_start, vma->vm_end - vma->vm_start); | |
1da177e4 | 407 | |
9a298b2a | 408 | vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL); |
1da177e4 | 409 | if (vma_entry) { |
b5e89ed5 | 410 | vma_entry->vma = vma; |
b5e89ed5 | 411 | vma_entry->pid = current->pid; |
bd1b331f | 412 | list_add(&vma_entry->head, &dev->vmalist); |
1da177e4 LT |
413 | } |
414 | } | |
d5028995 | 415 | EXPORT_SYMBOL_GPL(drm_vm_open_locked); |
1da177e4 | 416 | |
d7d8aac7 TH |
417 | static void drm_vm_open(struct vm_area_struct *vma) |
418 | { | |
84b1fd10 | 419 | struct drm_file *priv = vma->vm_file->private_data; |
2c14f28b | 420 | struct drm_device *dev = priv->minor->dev; |
d7d8aac7 TH |
421 | |
422 | mutex_lock(&dev->struct_mutex); | |
b06d66be | 423 | drm_vm_open_locked(dev, vma); |
d7d8aac7 TH |
424 | mutex_unlock(&dev->struct_mutex); |
425 | } | |
426 | ||
b06d66be RC |
427 | void drm_vm_close_locked(struct drm_device *dev, |
428 | struct vm_area_struct *vma) | |
1da177e4 | 429 | { |
8fc2fdf4 | 430 | struct drm_vma_entry *pt, *temp; |
1da177e4 LT |
431 | |
432 | DRM_DEBUG("0x%08lx,0x%08lx\n", | |
433 | vma->vm_start, vma->vm_end - vma->vm_start); | |
1da177e4 | 434 | |
bd1b331f | 435 | list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { |
1da177e4 | 436 | if (pt->vma == vma) { |
bd1b331f | 437 | list_del(&pt->head); |
9a298b2a | 438 | kfree(pt); |
1da177e4 LT |
439 | break; |
440 | } | |
441 | } | |
31dfbc93 CW |
442 | } |
443 | ||
444 | /** | |
445 | * \c close method for all virtual memory types. | |
446 | * | |
447 | * \param vma virtual memory area. | |
448 | * | |
449 | * Search the \p vma private data entry in drm_device::vmalist, unlink it, and | |
450 | * free it. | |
451 | */ | |
452 | static void drm_vm_close(struct vm_area_struct *vma) | |
453 | { | |
454 | struct drm_file *priv = vma->vm_file->private_data; | |
455 | struct drm_device *dev = priv->minor->dev; | |
456 | ||
457 | mutex_lock(&dev->struct_mutex); | |
b06d66be | 458 | drm_vm_close_locked(dev, vma); |
30e2fb18 | 459 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
460 | } |
461 | ||
462 | /** | |
463 | * mmap DMA memory. | |
464 | * | |
6c340eac | 465 | * \param file_priv DRM file private. |
1da177e4 LT |
466 | * \param vma virtual memory area. |
467 | * \return zero on success or a negative number on failure. | |
b5e89ed5 | 468 | * |
1da177e4 LT |
469 | * Sets the virtual memory area operations structure to vm_dma_ops, the file |
470 | * pointer, and calls vm_open(). | |
471 | */ | |
c94f7029 | 472 | static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma) |
1da177e4 | 473 | { |
84b1fd10 DA |
474 | struct drm_file *priv = filp->private_data; |
475 | struct drm_device *dev; | |
cdd55a29 | 476 | struct drm_device_dma *dma; |
b5e89ed5 | 477 | unsigned long length = vma->vm_end - vma->vm_start; |
1da177e4 | 478 | |
2c14f28b | 479 | dev = priv->minor->dev; |
b5e89ed5 | 480 | dma = dev->dma; |
1545085a TH |
481 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", |
482 | vma->vm_start, vma->vm_end, vma->vm_pgoff); | |
1da177e4 | 483 | |
b5e89ed5 | 484 | /* Length must match exact page count */ |
1da177e4 | 485 | if (!dma || (length >> PAGE_SHIFT) != dma->page_count) { |
1da177e4 LT |
486 | return -EINVAL; |
487 | } | |
1da177e4 | 488 | |
3417f33e GS |
489 | if (!capable(CAP_SYS_ADMIN) && |
490 | (dma->flags & _DRM_DMA_USE_PCI_RO)) { | |
491 | vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); | |
492 | #if defined(__i386__) || defined(__x86_64__) | |
493 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; | |
494 | #else | |
495 | /* Ye gads this is ugly. With more thought | |
496 | we could move this up higher and use | |
497 | `protection_map' instead. */ | |
498 | vma->vm_page_prot = | |
499 | __pgprot(pte_val | |
500 | (pte_wrprotect | |
501 | (__pte(pgprot_val(vma->vm_page_prot))))); | |
502 | #endif | |
503 | } | |
504 | ||
b5e89ed5 | 505 | vma->vm_ops = &drm_vm_dma_ops; |
1da177e4 | 506 | |
314e51b9 | 507 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 | 508 | |
b06d66be | 509 | drm_vm_open_locked(dev, vma); |
1da177e4 LT |
510 | return 0; |
511 | } | |
512 | ||
cbc60ca0 | 513 | static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev) |
1da177e4 LT |
514 | { |
515 | #ifdef __alpha__ | |
82ba3fef | 516 | return dev->hose->dense_mem_base; |
1da177e4 LT |
517 | #else |
518 | return 0; | |
519 | #endif | |
520 | } | |
b5e89ed5 | 521 | |
1da177e4 LT |
522 | /** |
523 | * mmap DMA memory. | |
524 | * | |
6c340eac | 525 | * \param file_priv DRM file private. |
1da177e4 LT |
526 | * \param vma virtual memory area. |
527 | * \return zero on success or a negative number on failure. | |
b5e89ed5 | 528 | * |
1da177e4 LT |
529 | * If the virtual memory area has no offset associated with it then it's a DMA |
530 | * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist, | |
531 | * checks that the restricted flag is not set, sets the virtual memory operations | |
532 | * according to the mapping type and remaps the pages. Finally sets the file | |
533 | * pointer and calls vm_open(). | |
534 | */ | |
a2c0a97b | 535 | int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma) |
1da177e4 | 536 | { |
84b1fd10 | 537 | struct drm_file *priv = filp->private_data; |
2c14f28b | 538 | struct drm_device *dev = priv->minor->dev; |
f77d390c | 539 | struct drm_local_map *map = NULL; |
41c2e75e | 540 | resource_size_t offset = 0; |
e0be428e | 541 | struct drm_hash_item *hash; |
1da177e4 | 542 | |
1545085a TH |
543 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", |
544 | vma->vm_start, vma->vm_end, vma->vm_pgoff); | |
1da177e4 | 545 | |
b5e89ed5 DA |
546 | if (!priv->authenticated) |
547 | return -EACCES; | |
1da177e4 LT |
548 | |
549 | /* We check for "dma". On Apple's UniNorth, it's valid to have | |
550 | * the AGP mapped at physical address 0 | |
551 | * --BenH. | |
552 | */ | |
1545085a | 553 | if (!vma->vm_pgoff |
1da177e4 | 554 | #if __OS_HAS_AGP |
b5e89ed5 DA |
555 | && (!dev->agp |
556 | || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE) | |
1da177e4 LT |
557 | #endif |
558 | ) | |
559 | return drm_mmap_dma(filp, vma); | |
560 | ||
1545085a | 561 | if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) { |
8d153f71 TH |
562 | DRM_ERROR("Could not find map\n"); |
563 | return -EINVAL; | |
1da177e4 LT |
564 | } |
565 | ||
55910517 | 566 | map = drm_hash_entry(hash, struct drm_map_list, hash)->map; |
b5e89ed5 | 567 | if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) |
1da177e4 LT |
568 | return -EPERM; |
569 | ||
b5e89ed5 | 570 | /* Check for valid size. */ |
54ba2f76 | 571 | if (map->size < vma->vm_end - vma->vm_start) |
b5e89ed5 | 572 | return -EINVAL; |
1da177e4 LT |
573 | |
574 | if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) { | |
575 | vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); | |
576 | #if defined(__i386__) || defined(__x86_64__) | |
577 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; | |
578 | #else | |
b5e89ed5 DA |
579 | /* Ye gads this is ugly. With more thought |
580 | we could move this up higher and use | |
581 | `protection_map' instead. */ | |
582 | vma->vm_page_prot = | |
583 | __pgprot(pte_val | |
584 | (pte_wrprotect | |
585 | (__pte(pgprot_val(vma->vm_page_prot))))); | |
1da177e4 LT |
586 | #endif |
587 | } | |
588 | ||
589 | switch (map->type) { | |
4b7fb9b5 | 590 | #if !defined(__arm__) |
b5e89ed5 | 591 | case _DRM_AGP: |
d9906753 | 592 | if (dev->agp && dev->agp->cant_use_aperture) { |
b5e89ed5 DA |
593 | /* |
594 | * On some platforms we can't talk to bus dma address from the CPU, so for | |
595 | * memory of type DRM_AGP, we'll deal with sorting out the real physical | |
ca0b07d9 | 596 | * pages and mappings in fault() |
b5e89ed5 | 597 | */ |
1da177e4 | 598 | #if defined(__powerpc__) |
b5e89ed5 | 599 | pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE; |
1da177e4 | 600 | #endif |
b5e89ed5 DA |
601 | vma->vm_ops = &drm_vm_ops; |
602 | break; | |
603 | } | |
604 | /* fall through to _DRM_FRAME_BUFFER... */ | |
4b7fb9b5 | 605 | #endif |
1da177e4 LT |
606 | case _DRM_FRAME_BUFFER: |
607 | case _DRM_REGISTERS: | |
cbc60ca0 | 608 | offset = drm_core_get_reg_ofs(dev); |
ff47eaf2 | 609 | vma->vm_page_prot = drm_io_prot(map, vma); |
3d77461e | 610 | if (io_remap_pfn_range(vma, vma->vm_start, |
b5e89ed5 DA |
611 | (map->offset + offset) >> PAGE_SHIFT, |
612 | vma->vm_end - vma->vm_start, | |
613 | vma->vm_page_prot)) | |
b5e89ed5 | 614 | return -EAGAIN; |
1da177e4 | 615 | DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx," |
41c2e75e | 616 | " offset = 0x%llx\n", |
1da177e4 | 617 | map->type, |
41c2e75e | 618 | vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset)); |
4b7fb9b5 | 619 | |
1da177e4 LT |
620 | vma->vm_ops = &drm_vm_ops; |
621 | break; | |
2d0f9eaf | 622 | case _DRM_CONSISTENT: |
38315878 | 623 | /* Consistent memory is really like shared memory. But |
ca0b07d9 | 624 | * it's allocated in a different way, so avoid fault */ |
38315878 HD |
625 | if (remap_pfn_range(vma, vma->vm_start, |
626 | page_to_pfn(virt_to_page(map->handle)), | |
627 | vma->vm_end - vma->vm_start, vma->vm_page_prot)) | |
628 | return -EAGAIN; | |
6876b3ba | 629 | vma->vm_page_prot = drm_dma_prot(map->type, vma); |
38315878 HD |
630 | /* fall through to _DRM_SHM */ |
631 | case _DRM_SHM: | |
1da177e4 LT |
632 | vma->vm_ops = &drm_vm_shm_ops; |
633 | vma->vm_private_data = (void *)map; | |
1da177e4 LT |
634 | break; |
635 | case _DRM_SCATTER_GATHER: | |
636 | vma->vm_ops = &drm_vm_sg_ops; | |
637 | vma->vm_private_data = (void *)map; | |
5ff64611 | 638 | vma->vm_page_prot = drm_dma_prot(map->type, vma); |
b5e89ed5 | 639 | break; |
1da177e4 LT |
640 | default: |
641 | return -EINVAL; /* This should never happen. */ | |
642 | } | |
314e51b9 | 643 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
1da177e4 | 644 | |
b06d66be | 645 | drm_vm_open_locked(dev, vma); |
1da177e4 LT |
646 | return 0; |
647 | } | |
b5e89ed5 | 648 | |
d7d8aac7 TH |
649 | int drm_mmap(struct file *filp, struct vm_area_struct *vma) |
650 | { | |
84b1fd10 | 651 | struct drm_file *priv = filp->private_data; |
2c14f28b | 652 | struct drm_device *dev = priv->minor->dev; |
d7d8aac7 TH |
653 | int ret; |
654 | ||
2c07a21d DA |
655 | if (drm_device_is_unplugged(dev)) |
656 | return -ENODEV; | |
657 | ||
d7d8aac7 TH |
658 | mutex_lock(&dev->struct_mutex); |
659 | ret = drm_mmap_locked(filp, vma); | |
660 | mutex_unlock(&dev->struct_mutex); | |
661 | ||
662 | return ret; | |
663 | } | |
1da177e4 | 664 | EXPORT_SYMBOL(drm_mmap); |