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
31 #define pr_fmt(fmt) "[TTM] " fmt
32
33 #include <ttm/ttm_module.h>
34 #include <ttm/ttm_bo_driver.h>
35 #include <ttm/ttm_placement.h>
36 #include <drm/drm_vma_manager.h>
37 #include <linux/mm.h>
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
ttm_bo_vm_fault_idle(struct ttm_buffer_object * bo,struct vm_area_struct * vma,struct vm_fault * vmf)44 static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
45 struct vm_area_struct *vma,
46 struct vm_fault *vmf)
47 {
48 int ret = 0;
49
50 if (likely(!test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)))
51 goto out_unlock;
52
53 /*
54 * Quick non-stalling check for idle.
55 */
56 ret = ttm_bo_wait(bo, false, false, true);
57 if (likely(ret == 0))
58 goto out_unlock;
59
60 /*
61 * If possible, avoid waiting for GPU with mmap_sem
62 * held.
63 */
64 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
65 ret = VM_FAULT_RETRY;
66 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
67 goto out_unlock;
68
69 ttm_bo_reference(bo);
70 up_read(&vma->vm_mm->mmap_sem);
71 (void) ttm_bo_wait(bo, false, true, false);
72 ttm_bo_unreserve(bo);
73 ttm_bo_unref(&bo);
74 goto out_unlock;
75 }
76
77 /*
78 * Ordinary wait.
79 */
80 ret = ttm_bo_wait(bo, false, true, false);
81 if (unlikely(ret != 0))
82 ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
83 VM_FAULT_NOPAGE;
84
85 out_unlock:
86 return ret;
87 }
88
ttm_bo_vm_fault(struct vm_area_struct * vma,struct vm_fault * vmf)89 static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
90 {
91 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
92 vma->vm_private_data;
93 struct ttm_bo_device *bdev = bo->bdev;
94 unsigned long page_offset;
95 unsigned long page_last;
96 unsigned long pfn;
97 struct ttm_tt *ttm = NULL;
98 struct page *page;
99 int ret;
100 int i;
101 unsigned long address = (unsigned long)vmf->virtual_address;
102 int retval = VM_FAULT_NOPAGE;
103 struct ttm_mem_type_manager *man =
104 &bdev->man[bo->mem.mem_type];
105 struct vm_area_struct cvma;
106
107 /*
108 * Work around locking order reversal in fault / nopfn
109 * between mmap_sem and bo_reserve: Perform a trylock operation
110 * for reserve, and if it fails, retry the fault after waiting
111 * for the buffer to become unreserved.
112 */
113 ret = ttm_bo_reserve(bo, true, true, false, NULL);
114 if (unlikely(ret != 0)) {
115 if (ret != -EBUSY)
116 return VM_FAULT_NOPAGE;
117
118 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
119 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
120 ttm_bo_reference(bo);
121 up_read(&vma->vm_mm->mmap_sem);
122 (void) ttm_bo_wait_unreserved(bo);
123 ttm_bo_unref(&bo);
124 }
125
126 return VM_FAULT_RETRY;
127 }
128
129 /*
130 * If we'd want to change locking order to
131 * mmap_sem -> bo::reserve, we'd use a blocking reserve here
132 * instead of retrying the fault...
133 */
134 return VM_FAULT_NOPAGE;
135 }
136
137 /*
138 * Refuse to fault imported pages. This should be handled
139 * (if at all) by redirecting mmap to the exporter.
140 */
141 if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
142 retval = VM_FAULT_SIGBUS;
143 goto out_unlock;
144 }
145
146 if (bdev->driver->fault_reserve_notify) {
147 ret = bdev->driver->fault_reserve_notify(bo);
148 switch (ret) {
149 case 0:
150 break;
151 case -EBUSY:
152 case -ERESTARTSYS:
153 retval = VM_FAULT_NOPAGE;
154 goto out_unlock;
155 default:
156 retval = VM_FAULT_SIGBUS;
157 goto out_unlock;
158 }
159 }
160
161 /*
162 * Wait for buffer data in transit, due to a pipelined
163 * move.
164 */
165 ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
166 if (unlikely(ret != 0)) {
167 retval = ret;
168
169 if (retval == VM_FAULT_RETRY &&
170 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
171 /* The BO has already been unreserved. */
172 return retval;
173 }
174
175 goto out_unlock;
176 }
177
178 ret = ttm_mem_io_lock(man, true);
179 if (unlikely(ret != 0)) {
180 retval = VM_FAULT_NOPAGE;
181 goto out_unlock;
182 }
183 ret = ttm_mem_io_reserve_vm(bo);
184 if (unlikely(ret != 0)) {
185 retval = VM_FAULT_SIGBUS;
186 goto out_io_unlock;
187 }
188
189 page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
190 vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
191 page_last = vma_pages(vma) + vma->vm_pgoff -
192 drm_vma_node_start(&bo->vma_node);
193
194 if (unlikely(page_offset >= bo->num_pages)) {
195 retval = VM_FAULT_SIGBUS;
196 goto out_io_unlock;
197 }
198
199 /*
200 * Make a local vma copy to modify the page_prot member
201 * and vm_flags if necessary. The vma parameter is protected
202 * by mmap_sem in write mode.
203 */
204 cvma = *vma;
205 cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
206
207 if (bo->mem.bus.is_iomem) {
208 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
209 cvma.vm_page_prot);
210 } else {
211 ttm = bo->ttm;
212 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
213 cvma.vm_page_prot);
214
215 /* Allocate all page at once, most common usage */
216 if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
217 retval = VM_FAULT_OOM;
218 goto out_io_unlock;
219 }
220 }
221
222 /*
223 * Speculatively prefault a number of pages. Only error on
224 * first page.
225 */
226 for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
227 if (bo->mem.bus.is_iomem)
228 pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
229 else {
230 page = ttm->pages[page_offset];
231 if (unlikely(!page && i == 0)) {
232 retval = VM_FAULT_OOM;
233 goto out_io_unlock;
234 } else if (unlikely(!page)) {
235 break;
236 }
237 page->mapping = vma->vm_file->f_mapping;
238 page->index = drm_vma_node_start(&bo->vma_node) +
239 page_offset;
240 pfn = page_to_pfn(page);
241 }
242
243 if (vma->vm_flags & VM_MIXEDMAP)
244 ret = vm_insert_mixed(&cvma, address, pfn);
245 else
246 ret = vm_insert_pfn(&cvma, address, pfn);
247
248 /*
249 * Somebody beat us to this PTE or prefaulting to
250 * an already populated PTE, or prefaulting error.
251 */
252
253 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
254 break;
255 else if (unlikely(ret != 0)) {
256 retval =
257 (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
258 goto out_io_unlock;
259 }
260
261 address += PAGE_SIZE;
262 if (unlikely(++page_offset >= page_last))
263 break;
264 }
265 out_io_unlock:
266 ttm_mem_io_unlock(man);
267 out_unlock:
268 ttm_bo_unreserve(bo);
269 return retval;
270 }
271
ttm_bo_vm_open(struct vm_area_struct * vma)272 static void ttm_bo_vm_open(struct vm_area_struct *vma)
273 {
274 struct ttm_buffer_object *bo =
275 (struct ttm_buffer_object *)vma->vm_private_data;
276
277 WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
278
279 (void)ttm_bo_reference(bo);
280 }
281
ttm_bo_vm_close(struct vm_area_struct * vma)282 static void ttm_bo_vm_close(struct vm_area_struct *vma)
283 {
284 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
285
286 ttm_bo_unref(&bo);
287 vma->vm_private_data = NULL;
288 }
289
290 static const struct vm_operations_struct ttm_bo_vm_ops = {
291 .fault = ttm_bo_vm_fault,
292 .open = ttm_bo_vm_open,
293 .close = ttm_bo_vm_close
294 };
295
ttm_bo_vm_lookup(struct ttm_bo_device * bdev,unsigned long offset,unsigned long pages)296 static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
297 unsigned long offset,
298 unsigned long pages)
299 {
300 struct drm_vma_offset_node *node;
301 struct ttm_buffer_object *bo = NULL;
302
303 drm_vma_offset_lock_lookup(&bdev->vma_manager);
304
305 node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
306 if (likely(node)) {
307 bo = container_of(node, struct ttm_buffer_object, vma_node);
308 if (!kref_get_unless_zero(&bo->kref))
309 bo = NULL;
310 }
311
312 drm_vma_offset_unlock_lookup(&bdev->vma_manager);
313
314 if (!bo)
315 pr_err("Could not find buffer object to map\n");
316
317 return bo;
318 }
319
ttm_bo_mmap(struct file * filp,struct vm_area_struct * vma,struct ttm_bo_device * bdev)320 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
321 struct ttm_bo_device *bdev)
322 {
323 struct ttm_bo_driver *driver;
324 struct ttm_buffer_object *bo;
325 int ret;
326
327 bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
328 if (unlikely(!bo))
329 return -EINVAL;
330
331 driver = bo->bdev->driver;
332 if (unlikely(!driver->verify_access)) {
333 ret = -EPERM;
334 goto out_unref;
335 }
336 ret = driver->verify_access(bo, filp);
337 if (unlikely(ret != 0))
338 goto out_unref;
339
340 vma->vm_ops = &ttm_bo_vm_ops;
341
342 /*
343 * Note: We're transferring the bo reference to
344 * vma->vm_private_data here.
345 */
346
347 vma->vm_private_data = bo;
348
349 /*
350 * We'd like to use VM_PFNMAP on shared mappings, where
351 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
352 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very
353 * bad for performance. Until that has been sorted out, use
354 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
355 */
356 vma->vm_flags |= VM_MIXEDMAP;
357 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
358 return 0;
359 out_unref:
360 ttm_bo_unref(&bo);
361 return ret;
362 }
363 EXPORT_SYMBOL(ttm_bo_mmap);
364
ttm_fbdev_mmap(struct vm_area_struct * vma,struct ttm_buffer_object * bo)365 int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
366 {
367 if (vma->vm_pgoff != 0)
368 return -EACCES;
369
370 vma->vm_ops = &ttm_bo_vm_ops;
371 vma->vm_private_data = ttm_bo_reference(bo);
372 vma->vm_flags |= VM_MIXEDMAP;
373 vma->vm_flags |= VM_IO | VM_DONTEXPAND;
374 return 0;
375 }
376 EXPORT_SYMBOL(ttm_fbdev_mmap);
377