blob: 716e724ac7100ee71201d5300687291688236708 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
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
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
Masahiro Yamada2da83312017-04-24 13:50:20 +090033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
David Herrmann72525b32013-07-24 21:08:53 +020036#include <drm/drm_vma_manager.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020037#include <linux/mm.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080038#include <linux/pfn_t.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020039#include <linux/rbtree.h>
40#include <linux/module.h>
41#include <linux/uaccess.h>
Tom Lendacky95cf9262017-07-17 16:10:26 -050042#include <linux/mem_encrypt.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_BO_VM_NUM_PREFAULT 16
45
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070046static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070047 struct vm_fault *vmf)
48{
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070049 int ret = 0;
50
Christian König5bc73062016-06-15 13:44:01 +020051 if (likely(!bo->moving))
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070052 goto out_unlock;
53
54 /*
55 * Quick non-stalling check for idle.
56 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010057 if (dma_fence_is_signaled(bo->moving))
Christian König5bc73062016-06-15 13:44:01 +020058 goto out_clear;
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070059
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
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +010069 ttm_bo_reference(bo);
Dave Jiang11bac802017-02-24 14:56:41 -080070 up_read(&vmf->vma->vm_mm->mmap_sem);
Chris Wilsonf54d1862016-10-25 13:00:45 +010071 (void) dma_fence_wait(bo->moving, true);
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +010072 ttm_bo_unreserve(bo);
73 ttm_bo_unref(&bo);
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070074 goto out_unlock;
75 }
76
77 /*
78 * Ordinary wait.
79 */
Chris Wilsonf54d1862016-10-25 13:00:45 +010080 ret = dma_fence_wait(bo->moving, true);
Christian König5bc73062016-06-15 13:44:01 +020081 if (unlikely(ret != 0)) {
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070082 ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
83 VM_FAULT_NOPAGE;
Christian König5bc73062016-06-15 13:44:01 +020084 goto out_unlock;
85 }
86
87out_clear:
Chris Wilsonf54d1862016-10-25 13:00:45 +010088 dma_fence_put(bo->moving);
Christian König5bc73062016-06-15 13:44:01 +020089 bo->moving = NULL;
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070090
91out_unlock:
Thomas Hellstromcbe12e72013-10-09 03:18:07 -070092 return ret;
93}
94
Tan Xiaojunc67fa6e2017-12-25 11:43:23 +080095static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo,
96 unsigned long page_offset)
97{
98 struct ttm_bo_device *bdev = bo->bdev;
99
100 if (bdev->driver->io_mem_pfn)
101 return bdev->driver->io_mem_pfn(bo, page_offset);
102
Tan Xiaojune83bf4a2017-12-25 11:43:34 +0800103 return ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT)
104 + page_offset;
Tan Xiaojunc67fa6e2017-12-25 11:43:23 +0800105}
106
Dave Jiang11bac802017-02-24 14:56:41 -0800107static int ttm_bo_vm_fault(struct vm_fault *vmf)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200108{
Dave Jiang11bac802017-02-24 14:56:41 -0800109 struct vm_area_struct *vma = vmf->vma;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200110 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
111 vma->vm_private_data;
112 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200113 unsigned long page_offset;
114 unsigned long page_last;
115 unsigned long pfn;
116 struct ttm_tt *ttm = NULL;
117 struct page *page;
118 int ret;
119 int i;
Jan Kara1a29d852016-12-14 15:07:01 -0800120 unsigned long address = vmf->address;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100121 struct ttm_mem_type_manager *man =
122 &bdev->man[bo->mem.mem_type];
Thomas Hellstrom39438752013-11-06 09:32:59 -0800123 struct vm_area_struct cvma;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200124
125 /*
126 * Work around locking order reversal in fault / nopfn
127 * between mmap_sem and bo_reserve: Perform a trylock operation
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800128 * for reserve, and if it fails, retry the fault after waiting
129 * for the buffer to become unreserved.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130 */
Christian Königdfd5e502016-04-06 11:12:03 +0200131 ret = ttm_bo_reserve(bo, true, true, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200132 if (unlikely(ret != 0)) {
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800133 if (ret != -EBUSY)
134 return VM_FAULT_NOPAGE;
135
136 if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
137 if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +0100138 ttm_bo_reference(bo);
Dave Jiang11bac802017-02-24 14:56:41 -0800139 up_read(&vmf->vma->vm_mm->mmap_sem);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800140 (void) ttm_bo_wait_unreserved(bo);
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +0100141 ttm_bo_unref(&bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800142 }
143
144 return VM_FAULT_RETRY;
145 }
146
147 /*
148 * If we'd want to change locking order to
149 * mmap_sem -> bo::reserve, we'd use a blocking reserve here
150 * instead of retrying the fault...
151 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 return VM_FAULT_NOPAGE;
153 }
154
Thomas Hellstrom667a50d2014-01-03 11:17:18 +0100155 /*
156 * Refuse to fault imported pages. This should be handled
157 * (if at all) by redirecting mmap to the exporter.
158 */
159 if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500160 ret = VM_FAULT_SIGBUS;
Thomas Hellstrom667a50d2014-01-03 11:17:18 +0100161 goto out_unlock;
162 }
163
Jerome Glisse82c5da62010-04-09 14:39:23 +0200164 if (bdev->driver->fault_reserve_notify) {
165 ret = bdev->driver->fault_reserve_notify(bo);
166 switch (ret) {
167 case 0:
168 break;
169 case -EBUSY:
Jerome Glisse82c5da62010-04-09 14:39:23 +0200170 case -ERESTARTSYS:
Tom St Denisde8dfb82018-01-26 09:22:05 -0500171 ret = VM_FAULT_NOPAGE;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200172 goto out_unlock;
173 default:
Tom St Denisde8dfb82018-01-26 09:22:05 -0500174 ret = VM_FAULT_SIGBUS;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200175 goto out_unlock;
176 }
177 }
Dave Airliee024e112009-06-24 09:48:08 +1000178
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179 /*
180 * Wait for buffer data in transit, due to a pipelined
181 * move.
182 */
Dave Jiang11bac802017-02-24 14:56:41 -0800183 ret = ttm_bo_vm_fault_idle(bo, vmf);
Thomas Hellstromcbe12e72013-10-09 03:18:07 -0700184 if (unlikely(ret != 0)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500185 if (ret == VM_FAULT_RETRY &&
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +0100186 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
187 /* The BO has already been unreserved. */
Tom St Denisde8dfb82018-01-26 09:22:05 -0500188 return ret;
Nicolai Hähnle3089c1d2017-02-18 22:59:56 +0100189 }
190
Thomas Hellstromcbe12e72013-10-09 03:18:07 -0700191 goto out_unlock;
192 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200193
Thomas Hellstromeba67092010-11-11 09:41:57 +0100194 ret = ttm_mem_io_lock(man, true);
195 if (unlikely(ret != 0)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500196 ret = VM_FAULT_NOPAGE;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200197 goto out_unlock;
198 }
Thomas Hellstromeba67092010-11-11 09:41:57 +0100199 ret = ttm_mem_io_reserve_vm(bo);
200 if (unlikely(ret != 0)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500201 ret = VM_FAULT_SIGBUS;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100202 goto out_io_unlock;
203 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200204
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205 page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
Thomas Hellstromd3867352013-12-08 23:23:57 -0800206 vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
207 page_last = vma_pages(vma) + vma->vm_pgoff -
208 drm_vma_node_start(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200209
210 if (unlikely(page_offset >= bo->num_pages)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500211 ret = VM_FAULT_SIGBUS;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100212 goto out_io_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200213 }
214
215 /*
Thomas Hellstrom39438752013-11-06 09:32:59 -0800216 * Make a local vma copy to modify the page_prot member
217 * and vm_flags if necessary. The vma parameter is protected
218 * by mmap_sem in write mode.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200219 */
Thomas Hellstrom39438752013-11-06 09:32:59 -0800220 cvma = *vma;
221 cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
222
Jerome Glisse82c5da62010-04-09 14:39:23 +0200223 if (bo->mem.bus.is_iomem) {
Thomas Hellstrom39438752013-11-06 09:32:59 -0800224 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
225 cvma.vm_page_prot);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200226 } else {
Roger Hed0cef9f2017-12-21 17:42:50 +0800227 struct ttm_operation_ctx ctx = {
228 .interruptible = false,
229 .no_wait_gpu = false
230 };
231
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200232 ttm = bo->ttm;
Benjamin Herrenschmidt94318d52014-09-04 17:47:23 +1000233 cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
234 cvma.vm_page_prot);
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400235
236 /* Allocate all page at once, most common usage */
Roger Hed0cef9f2017-12-21 17:42:50 +0800237 if (ttm->bdev->driver->ttm_tt_populate(ttm, &ctx)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500238 ret = VM_FAULT_OOM;
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400239 goto out_io_unlock;
240 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200241 }
242
243 /*
244 * Speculatively prefault a number of pages. Only error on
245 * first page.
246 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247 for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
Tom Lendacky95cf9262017-07-17 16:10:26 -0500248 if (bo->mem.bus.is_iomem) {
249 /* Iomem should not be marked encrypted */
250 cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot);
Tan Xiaojunc67fa6e2017-12-25 11:43:23 +0800251 pfn = ttm_bo_io_mem_pfn(bo, page_offset);
Tom Lendacky95cf9262017-07-17 16:10:26 -0500252 } else {
Jerome Glisseb1e5f172011-11-02 23:59:28 -0400253 page = ttm->pages[page_offset];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 if (unlikely(!page && i == 0)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500255 ret = VM_FAULT_OOM;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100256 goto out_io_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200257 } else if (unlikely(!page)) {
258 break;
259 }
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100260 page->mapping = vma->vm_file->f_mapping;
261 page->index = drm_vma_node_start(&bo->vma_node) +
262 page_offset;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 pfn = page_to_pfn(page);
264 }
265
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100266 if (vma->vm_flags & VM_MIXEDMAP)
Dan Williams01c8f1c2016-01-15 16:56:40 -0800267 ret = vm_insert_mixed(&cvma, address,
268 __pfn_to_pfn_t(pfn, PFN_DEV));
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100269 else
270 ret = vm_insert_pfn(&cvma, address, pfn);
271
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200272 /*
273 * Somebody beat us to this PTE or prefaulting to
274 * an already populated PTE, or prefaulting error.
275 */
276
277 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
278 break;
279 else if (unlikely(ret != 0)) {
Tom St Denisde8dfb82018-01-26 09:22:05 -0500280 ret =
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200281 (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100282 goto out_io_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283 }
284
285 address += PAGE_SIZE;
286 if (unlikely(++page_offset >= page_last))
287 break;
288 }
Tom St Denisde8dfb82018-01-26 09:22:05 -0500289 ret = VM_FAULT_NOPAGE;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100290out_io_unlock:
291 ttm_mem_io_unlock(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292out_unlock:
293 ttm_bo_unreserve(bo);
Tom St Denisde8dfb82018-01-26 09:22:05 -0500294 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295}
296
297static void ttm_bo_vm_open(struct vm_area_struct *vma)
298{
299 struct ttm_buffer_object *bo =
300 (struct ttm_buffer_object *)vma->vm_private_data;
301
Thomas Hellstrom58aa6622014-01-03 11:47:23 +0100302 WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
303
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200304 (void)ttm_bo_reference(bo);
305}
306
307static void ttm_bo_vm_close(struct vm_area_struct *vma)
308{
Jerome Glisse82c5da62010-04-09 14:39:23 +0200309 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310
311 ttm_bo_unref(&bo);
312 vma->vm_private_data = NULL;
313}
314
Felix Kuehling09ac4fc2017-07-13 17:01:16 -0400315static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
316 unsigned long offset,
Tom St Denis95244db2018-01-26 09:32:29 -0500317 uint8_t *buf, int len, int write)
Felix Kuehling09ac4fc2017-07-13 17:01:16 -0400318{
319 unsigned long page = offset >> PAGE_SHIFT;
320 unsigned long bytes_left = len;
321 int ret;
322
323 /* Copy a page at a time, that way no extra virtual address
324 * mapping is needed
325 */
326 offset -= page << PAGE_SHIFT;
327 do {
328 unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
329 struct ttm_bo_kmap_obj map;
330 void *ptr;
331 bool is_iomem;
332
333 ret = ttm_bo_kmap(bo, page, 1, &map);
334 if (ret)
335 return ret;
336
337 ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
338 WARN_ON_ONCE(is_iomem);
339 if (write)
340 memcpy(ptr, buf, bytes);
341 else
342 memcpy(buf, ptr, bytes);
343 ttm_bo_kunmap(&map);
344
345 page++;
Tom St Denis95244db2018-01-26 09:32:29 -0500346 buf += bytes;
Felix Kuehling09ac4fc2017-07-13 17:01:16 -0400347 bytes_left -= bytes;
348 offset = 0;
349 } while (bytes_left);
350
351 return len;
352}
353
354static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
355 void *buf, int len, int write)
356{
357 unsigned long offset = (addr) - vma->vm_start;
358 struct ttm_buffer_object *bo = vma->vm_private_data;
359 int ret;
360
361 if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages)
362 return -EIO;
363
364 ret = ttm_bo_reserve(bo, true, false, NULL);
365 if (ret)
366 return ret;
367
368 switch (bo->mem.mem_type) {
369 case TTM_PL_SYSTEM:
370 if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
371 ret = ttm_tt_swapin(bo->ttm);
372 if (unlikely(ret != 0))
373 return ret;
374 }
375 /* fall through */
376 case TTM_PL_TT:
377 ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
378 break;
379 default:
380 if (bo->bdev->driver->access_memory)
381 ret = bo->bdev->driver->access_memory(
382 bo, offset, buf, len, write);
383 else
384 ret = -EIO;
385 }
386
387 ttm_bo_unreserve(bo);
388
389 return ret;
390}
391
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400392static const struct vm_operations_struct ttm_bo_vm_ops = {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200393 .fault = ttm_bo_vm_fault,
394 .open = ttm_bo_vm_open,
Felix Kuehling09ac4fc2017-07-13 17:01:16 -0400395 .close = ttm_bo_vm_close,
396 .access = ttm_bo_vm_access
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200397};
398
David Herrmann72525b32013-07-24 21:08:53 +0200399static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
400 unsigned long offset,
401 unsigned long pages)
402{
403 struct drm_vma_offset_node *node;
404 struct ttm_buffer_object *bo = NULL;
405
406 drm_vma_offset_lock_lookup(&bdev->vma_manager);
407
408 node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
409 if (likely(node)) {
410 bo = container_of(node, struct ttm_buffer_object, vma_node);
411 if (!kref_get_unless_zero(&bo->kref))
412 bo = NULL;
413 }
414
415 drm_vma_offset_unlock_lookup(&bdev->vma_manager);
416
417 if (!bo)
418 pr_err("Could not find buffer object to map\n");
419
420 return bo;
421}
422
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200423int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
424 struct ttm_bo_device *bdev)
425{
426 struct ttm_bo_driver *driver;
427 struct ttm_buffer_object *bo;
428 int ret;
429
David Herrmann72525b32013-07-24 21:08:53 +0200430 bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
431 if (unlikely(!bo))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200433
434 driver = bo->bdev->driver;
435 if (unlikely(!driver->verify_access)) {
436 ret = -EPERM;
437 goto out_unref;
438 }
439 ret = driver->verify_access(bo, filp);
440 if (unlikely(ret != 0))
441 goto out_unref;
442
443 vma->vm_ops = &ttm_bo_vm_ops;
444
445 /*
446 * Note: We're transferring the bo reference to
447 * vma->vm_private_data here.
448 */
449
450 vma->vm_private_data = bo;
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100451
452 /*
Thomas Hellstrom0e6d6ec2014-03-12 10:41:32 +0100453 * We'd like to use VM_PFNMAP on shared mappings, where
454 * (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
455 * but for some reason VM_PFNMAP + x86 PAT + write-combine is very
456 * bad for performance. Until that has been sorted out, use
457 * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100458 */
Thomas Hellstrom0e6d6ec2014-03-12 10:41:32 +0100459 vma->vm_flags |= VM_MIXEDMAP;
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100460 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200461 return 0;
462out_unref:
463 ttm_bo_unref(&bo);
464 return ret;
465}
466EXPORT_SYMBOL(ttm_bo_mmap);
467
468int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
469{
470 if (vma->vm_pgoff != 0)
471 return -EACCES;
472
473 vma->vm_ops = &ttm_bo_vm_ops;
474 vma->vm_private_data = ttm_bo_reference(bo);
Thomas Hellstrom0e6d6ec2014-03-12 10:41:32 +0100475 vma->vm_flags |= VM_MIXEDMAP;
Thomas Hellstrom7dfe8b62014-01-03 09:21:54 +0100476 vma->vm_flags |= VM_IO | VM_DONTEXPAND;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200477 return 0;
478}
479EXPORT_SYMBOL(ttm_fbdev_mmap);