Commit | Line | Data |
---|---|---|
ba4e7d97 TH |
1 | /************************************************************************** |
2 | * | |
3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA | |
4 | * All Rights Reserved. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the | |
8 | * "Software"), to deal in the Software without restriction, including | |
9 | * without limitation the rights to use, copy, modify, merge, publish, | |
10 | * distribute, sub license, and/or sell copies of the Software, and to | |
11 | * permit persons to whom the Software is furnished to do so, subject to | |
12 | * the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice (including the | |
15 | * next paragraph) shall be included in all copies or substantial portions | |
16 | * of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 | * | |
26 | **************************************************************************/ | |
27 | /* | |
28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> | |
29 | */ | |
30 | ||
25d0479a JP |
31 | #define pr_fmt(fmt) "[TTM] " fmt |
32 | ||
ba4e7d97 TH |
33 | #include <ttm/ttm_module.h> |
34 | #include <ttm/ttm_bo_driver.h> | |
35 | #include <ttm/ttm_placement.h> | |
72525b3f | 36 | #include <drm/drm_vma_manager.h> |
ba4e7d97 | 37 | #include <linux/mm.h> |
ba4e7d97 TH |
38 | #include <linux/rbtree.h> |
39 | #include <linux/module.h> | |
40 | #include <linux/uaccess.h> | |
41 | ||
42 | #define TTM_BO_VM_NUM_PREFAULT 16 | |
43 | ||
ba4e7d97 TH |
44 | static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
45 | { | |
46 | struct ttm_buffer_object *bo = (struct ttm_buffer_object *) | |
47 | vma->vm_private_data; | |
48 | struct ttm_bo_device *bdev = bo->bdev; | |
ba4e7d97 TH |
49 | unsigned long page_offset; |
50 | unsigned long page_last; | |
51 | unsigned long pfn; | |
52 | struct ttm_tt *ttm = NULL; | |
53 | struct page *page; | |
54 | int ret; | |
55 | int i; | |
ba4e7d97 TH |
56 | unsigned long address = (unsigned long)vmf->virtual_address; |
57 | int retval = VM_FAULT_NOPAGE; | |
eba67093 TH |
58 | struct ttm_mem_type_manager *man = |
59 | &bdev->man[bo->mem.mem_type]; | |
ba4e7d97 TH |
60 | |
61 | /* | |
62 | * Work around locking order reversal in fault / nopfn | |
63 | * between mmap_sem and bo_reserve: Perform a trylock operation | |
64 | * for reserve, and if it fails, retry the fault after scheduling. | |
65 | */ | |
66 | ||
67 | ret = ttm_bo_reserve(bo, true, true, false, 0); | |
68 | if (unlikely(ret != 0)) { | |
69 | if (ret == -EBUSY) | |
70 | set_need_resched(); | |
71 | return VM_FAULT_NOPAGE; | |
72 | } | |
73 | ||
82c5da6b JG |
74 | if (bdev->driver->fault_reserve_notify) { |
75 | ret = bdev->driver->fault_reserve_notify(bo); | |
76 | switch (ret) { | |
77 | case 0: | |
78 | break; | |
79 | case -EBUSY: | |
80 | set_need_resched(); | |
81 | case -ERESTARTSYS: | |
82 | retval = VM_FAULT_NOPAGE; | |
83 | goto out_unlock; | |
84 | default: | |
85 | retval = VM_FAULT_SIGBUS; | |
86 | goto out_unlock; | |
87 | } | |
88 | } | |
e024e110 | 89 | |
ba4e7d97 TH |
90 | /* |
91 | * Wait for buffer data in transit, due to a pipelined | |
92 | * move. | |
93 | */ | |
94 | ||
702adba2 | 95 | spin_lock(&bdev->fence_lock); |
ba4e7d97 TH |
96 | if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) { |
97 | ret = ttm_bo_wait(bo, false, true, false); | |
702adba2 | 98 | spin_unlock(&bdev->fence_lock); |
ba4e7d97 | 99 | if (unlikely(ret != 0)) { |
98ffc415 | 100 | retval = (ret != -ERESTARTSYS) ? |
ba4e7d97 TH |
101 | VM_FAULT_SIGBUS : VM_FAULT_NOPAGE; |
102 | goto out_unlock; | |
103 | } | |
104 | } else | |
702adba2 | 105 | spin_unlock(&bdev->fence_lock); |
ba4e7d97 | 106 | |
eba67093 TH |
107 | ret = ttm_mem_io_lock(man, true); |
108 | if (unlikely(ret != 0)) { | |
109 | retval = VM_FAULT_NOPAGE; | |
ba4e7d97 TH |
110 | goto out_unlock; |
111 | } | |
eba67093 TH |
112 | ret = ttm_mem_io_reserve_vm(bo); |
113 | if (unlikely(ret != 0)) { | |
114 | retval = VM_FAULT_SIGBUS; | |
115 | goto out_io_unlock; | |
116 | } | |
ba4e7d97 | 117 | |
ba4e7d97 | 118 | page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) + |
72525b3f | 119 | drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff; |
025df775 | 120 | page_last = vma_pages(vma) + |
72525b3f | 121 | drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff; |
ba4e7d97 TH |
122 | |
123 | if (unlikely(page_offset >= bo->num_pages)) { | |
124 | retval = VM_FAULT_SIGBUS; | |
eba67093 | 125 | goto out_io_unlock; |
ba4e7d97 TH |
126 | } |
127 | ||
128 | /* | |
129 | * Strictly, we're not allowed to modify vma->vm_page_prot here, | |
130 | * since the mmap_sem is only held in read mode. However, we | |
131 | * modify only the caching bits of vma->vm_page_prot and | |
132 | * consider those bits protected by | |
133 | * the bo->mutex, as we should be the only writers. | |
134 | * There shouldn't really be any readers of these bits except | |
135 | * within vm_insert_mixed()? fork? | |
136 | * | |
137 | * TODO: Add a list of vmas to the bo, and change the | |
138 | * vma->vm_page_prot when the object changes caching policy, with | |
139 | * the correct locks held. | |
140 | */ | |
82c5da6b | 141 | if (bo->mem.bus.is_iomem) { |
ba4e7d97 TH |
142 | vma->vm_page_prot = ttm_io_prot(bo->mem.placement, |
143 | vma->vm_page_prot); | |
144 | } else { | |
145 | ttm = bo->ttm; | |
146 | vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ? | |
147 | vm_get_page_prot(vma->vm_flags) : | |
148 | ttm_io_prot(bo->mem.placement, vma->vm_page_prot); | |
b1e5f172 JG |
149 | |
150 | /* Allocate all page at once, most common usage */ | |
151 | if (ttm->bdev->driver->ttm_tt_populate(ttm)) { | |
152 | retval = VM_FAULT_OOM; | |
153 | goto out_io_unlock; | |
154 | } | |
ba4e7d97 TH |
155 | } |
156 | ||
157 | /* | |
158 | * Speculatively prefault a number of pages. Only error on | |
159 | * first page. | |
160 | */ | |
ba4e7d97 | 161 | for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) { |
82c5da6b JG |
162 | if (bo->mem.bus.is_iomem) |
163 | pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset; | |
ba4e7d97 | 164 | else { |
b1e5f172 | 165 | page = ttm->pages[page_offset]; |
ba4e7d97 TH |
166 | if (unlikely(!page && i == 0)) { |
167 | retval = VM_FAULT_OOM; | |
eba67093 | 168 | goto out_io_unlock; |
ba4e7d97 TH |
169 | } else if (unlikely(!page)) { |
170 | break; | |
171 | } | |
172 | pfn = page_to_pfn(page); | |
173 | } | |
174 | ||
175 | ret = vm_insert_mixed(vma, address, pfn); | |
176 | /* | |
177 | * Somebody beat us to this PTE or prefaulting to | |
178 | * an already populated PTE, or prefaulting error. | |
179 | */ | |
180 | ||
181 | if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0))) | |
182 | break; | |
183 | else if (unlikely(ret != 0)) { | |
184 | retval = | |
185 | (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; | |
eba67093 | 186 | goto out_io_unlock; |
ba4e7d97 TH |
187 | } |
188 | ||
189 | address += PAGE_SIZE; | |
190 | if (unlikely(++page_offset >= page_last)) | |
191 | break; | |
192 | } | |
eba67093 TH |
193 | out_io_unlock: |
194 | ttm_mem_io_unlock(man); | |
ba4e7d97 TH |
195 | out_unlock: |
196 | ttm_bo_unreserve(bo); | |
197 | return retval; | |
198 | } | |
199 | ||
200 | static void ttm_bo_vm_open(struct vm_area_struct *vma) | |
201 | { | |
202 | struct ttm_buffer_object *bo = | |
203 | (struct ttm_buffer_object *)vma->vm_private_data; | |
204 | ||
205 | (void)ttm_bo_reference(bo); | |
206 | } | |
207 | ||
208 | static void ttm_bo_vm_close(struct vm_area_struct *vma) | |
209 | { | |
82c5da6b | 210 | struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data; |
ba4e7d97 TH |
211 | |
212 | ttm_bo_unref(&bo); | |
213 | vma->vm_private_data = NULL; | |
214 | } | |
215 | ||
f0f37e2f | 216 | static const struct vm_operations_struct ttm_bo_vm_ops = { |
ba4e7d97 TH |
217 | .fault = ttm_bo_vm_fault, |
218 | .open = ttm_bo_vm_open, | |
219 | .close = ttm_bo_vm_close | |
220 | }; | |
221 | ||
72525b3f DH |
222 | static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev, |
223 | unsigned long offset, | |
224 | unsigned long pages) | |
225 | { | |
226 | struct drm_vma_offset_node *node; | |
227 | struct ttm_buffer_object *bo = NULL; | |
228 | ||
229 | drm_vma_offset_lock_lookup(&bdev->vma_manager); | |
230 | ||
231 | node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages); | |
232 | if (likely(node)) { | |
233 | bo = container_of(node, struct ttm_buffer_object, vma_node); | |
234 | if (!kref_get_unless_zero(&bo->kref)) | |
235 | bo = NULL; | |
236 | } | |
237 | ||
238 | drm_vma_offset_unlock_lookup(&bdev->vma_manager); | |
239 | ||
240 | if (!bo) | |
241 | pr_err("Could not find buffer object to map\n"); | |
242 | ||
243 | return bo; | |
244 | } | |
245 | ||
ba4e7d97 TH |
246 | int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, |
247 | struct ttm_bo_device *bdev) | |
248 | { | |
249 | struct ttm_bo_driver *driver; | |
250 | struct ttm_buffer_object *bo; | |
251 | int ret; | |
252 | ||
72525b3f DH |
253 | bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma)); |
254 | if (unlikely(!bo)) | |
ba4e7d97 | 255 | return -EINVAL; |
ba4e7d97 TH |
256 | |
257 | driver = bo->bdev->driver; | |
258 | if (unlikely(!driver->verify_access)) { | |
259 | ret = -EPERM; | |
260 | goto out_unref; | |
261 | } | |
262 | ret = driver->verify_access(bo, filp); | |
263 | if (unlikely(ret != 0)) | |
264 | goto out_unref; | |
265 | ||
266 | vma->vm_ops = &ttm_bo_vm_ops; | |
267 | ||
268 | /* | |
269 | * Note: We're transferring the bo reference to | |
270 | * vma->vm_private_data here. | |
271 | */ | |
272 | ||
273 | vma->vm_private_data = bo; | |
314e51b9 | 274 | vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP; |
ba4e7d97 TH |
275 | return 0; |
276 | out_unref: | |
277 | ttm_bo_unref(&bo); | |
278 | return ret; | |
279 | } | |
280 | EXPORT_SYMBOL(ttm_bo_mmap); | |
281 | ||
282 | int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo) | |
283 | { | |
284 | if (vma->vm_pgoff != 0) | |
285 | return -EACCES; | |
286 | ||
287 | vma->vm_ops = &ttm_bo_vm_ops; | |
288 | vma->vm_private_data = ttm_bo_reference(bo); | |
314e51b9 | 289 | vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND; |
ba4e7d97 TH |
290 | return 0; |
291 | } | |
292 | EXPORT_SYMBOL(ttm_fbdev_mmap); | |
293 | ||
294 | ||
295 | ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp, | |
296 | const char __user *wbuf, char __user *rbuf, size_t count, | |
297 | loff_t *f_pos, bool write) | |
298 | { | |
299 | struct ttm_buffer_object *bo; | |
300 | struct ttm_bo_driver *driver; | |
301 | struct ttm_bo_kmap_obj map; | |
302 | unsigned long dev_offset = (*f_pos >> PAGE_SHIFT); | |
303 | unsigned long kmap_offset; | |
304 | unsigned long kmap_end; | |
305 | unsigned long kmap_num; | |
306 | size_t io_size; | |
307 | unsigned int page_offset; | |
308 | char *virtual; | |
309 | int ret; | |
310 | bool no_wait = false; | |
311 | bool dummy; | |
312 | ||
72525b3f | 313 | bo = ttm_bo_vm_lookup(bdev, dev_offset, 1); |
ba4e7d97 TH |
314 | if (unlikely(bo == NULL)) |
315 | return -EFAULT; | |
316 | ||
317 | driver = bo->bdev->driver; | |
b8ff7357 | 318 | if (unlikely(!driver->verify_access)) { |
ba4e7d97 TH |
319 | ret = -EPERM; |
320 | goto out_unref; | |
321 | } | |
322 | ||
323 | ret = driver->verify_access(bo, filp); | |
324 | if (unlikely(ret != 0)) | |
325 | goto out_unref; | |
326 | ||
72525b3f | 327 | kmap_offset = dev_offset - drm_vma_node_start(&bo->vma_node); |
916635bf | 328 | if (unlikely(kmap_offset >= bo->num_pages)) { |
ba4e7d97 TH |
329 | ret = -EFBIG; |
330 | goto out_unref; | |
331 | } | |
332 | ||
333 | page_offset = *f_pos & ~PAGE_MASK; | |
334 | io_size = bo->num_pages - kmap_offset; | |
335 | io_size = (io_size << PAGE_SHIFT) - page_offset; | |
336 | if (count < io_size) | |
337 | io_size = count; | |
338 | ||
339 | kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT; | |
340 | kmap_num = kmap_end - kmap_offset + 1; | |
341 | ||
342 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); | |
343 | ||
344 | switch (ret) { | |
345 | case 0: | |
346 | break; | |
ba4e7d97 TH |
347 | case -EBUSY: |
348 | ret = -EAGAIN; | |
349 | goto out_unref; | |
350 | default: | |
351 | goto out_unref; | |
352 | } | |
353 | ||
354 | ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map); | |
355 | if (unlikely(ret != 0)) { | |
356 | ttm_bo_unreserve(bo); | |
357 | goto out_unref; | |
358 | } | |
359 | ||
360 | virtual = ttm_kmap_obj_virtual(&map, &dummy); | |
361 | virtual += page_offset; | |
362 | ||
363 | if (write) | |
364 | ret = copy_from_user(virtual, wbuf, io_size); | |
365 | else | |
366 | ret = copy_to_user(rbuf, virtual, io_size); | |
367 | ||
368 | ttm_bo_kunmap(&map); | |
369 | ttm_bo_unreserve(bo); | |
370 | ttm_bo_unref(&bo); | |
371 | ||
372 | if (unlikely(ret != 0)) | |
373 | return -EFBIG; | |
374 | ||
375 | *f_pos += io_size; | |
376 | ||
377 | return io_size; | |
378 | out_unref: | |
379 | ttm_bo_unref(&bo); | |
380 | return ret; | |
381 | } | |
382 | ||
383 | ssize_t ttm_bo_fbdev_io(struct ttm_buffer_object *bo, const char __user *wbuf, | |
384 | char __user *rbuf, size_t count, loff_t *f_pos, | |
385 | bool write) | |
386 | { | |
387 | struct ttm_bo_kmap_obj map; | |
388 | unsigned long kmap_offset; | |
389 | unsigned long kmap_end; | |
390 | unsigned long kmap_num; | |
391 | size_t io_size; | |
392 | unsigned int page_offset; | |
393 | char *virtual; | |
394 | int ret; | |
395 | bool no_wait = false; | |
396 | bool dummy; | |
397 | ||
398 | kmap_offset = (*f_pos >> PAGE_SHIFT); | |
916635bf | 399 | if (unlikely(kmap_offset >= bo->num_pages)) |
ba4e7d97 TH |
400 | return -EFBIG; |
401 | ||
402 | page_offset = *f_pos & ~PAGE_MASK; | |
403 | io_size = bo->num_pages - kmap_offset; | |
404 | io_size = (io_size << PAGE_SHIFT) - page_offset; | |
405 | if (count < io_size) | |
406 | io_size = count; | |
407 | ||
408 | kmap_end = (*f_pos + count - 1) >> PAGE_SHIFT; | |
409 | kmap_num = kmap_end - kmap_offset + 1; | |
410 | ||
411 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); | |
412 | ||
413 | switch (ret) { | |
414 | case 0: | |
415 | break; | |
ba4e7d97 TH |
416 | case -EBUSY: |
417 | return -EAGAIN; | |
418 | default: | |
419 | return ret; | |
420 | } | |
421 | ||
422 | ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map); | |
423 | if (unlikely(ret != 0)) { | |
424 | ttm_bo_unreserve(bo); | |
425 | return ret; | |
426 | } | |
427 | ||
428 | virtual = ttm_kmap_obj_virtual(&map, &dummy); | |
429 | virtual += page_offset; | |
430 | ||
431 | if (write) | |
432 | ret = copy_from_user(virtual, wbuf, io_size); | |
433 | else | |
434 | ret = copy_to_user(rbuf, virtual, io_size); | |
435 | ||
436 | ttm_bo_kunmap(&map); | |
437 | ttm_bo_unreserve(bo); | |
438 | ttm_bo_unref(&bo); | |
439 | ||
440 | if (unlikely(ret != 0)) | |
441 | return ret; | |
442 | ||
443 | *f_pos += io_size; | |
444 | ||
445 | return io_size; | |
446 | } |