2009-06-10 21:20:19 +08:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
/*
|
|
|
|
* Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
|
|
|
|
*/
|
|
|
|
|
2012-03-17 12:43:50 +08:00
|
|
|
#define pr_fmt(fmt) "[TTM] " fmt
|
|
|
|
|
2017-04-24 12:50:20 +08:00
|
|
|
#include <drm/ttm/ttm_module.h>
|
|
|
|
#include <drm/ttm/ttm_bo_driver.h>
|
|
|
|
#include <drm/ttm/ttm_placement.h>
|
2013-07-25 03:08:53 +08:00
|
|
|
#include <drm/drm_vma_manager.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
#include <linux/mm.h>
|
2016-01-16 08:56:40 +08:00
|
|
|
#include <linux/pfn_t.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
#include <linux/rbtree.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/uaccess.h>
|
2017-07-18 05:10:26 +08:00
|
|
|
#include <linux/mem_encrypt.h>
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
#define TTM_BO_VM_NUM_PREFAULT 16
|
|
|
|
|
2013-10-09 18:18:07 +08:00
|
|
|
static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
|
|
|
|
struct vm_fault *vmf)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-06-15 19:44:01 +08:00
|
|
|
if (likely(!bo->moving))
|
2013-10-09 18:18:07 +08:00
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quick non-stalling check for idle.
|
|
|
|
*/
|
2016-10-25 20:00:45 +08:00
|
|
|
if (dma_fence_is_signaled(bo->moving))
|
2016-06-15 19:44:01 +08:00
|
|
|
goto out_clear;
|
2013-10-09 18:18:07 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If possible, avoid waiting for GPU with mmap_sem
|
|
|
|
* held.
|
|
|
|
*/
|
|
|
|
if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
|
|
|
|
ret = VM_FAULT_RETRY;
|
|
|
|
if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
|
|
|
|
goto out_unlock;
|
|
|
|
|
2017-02-19 05:59:56 +08:00
|
|
|
ttm_bo_reference(bo);
|
2017-02-25 06:56:41 +08:00
|
|
|
up_read(&vmf->vma->vm_mm->mmap_sem);
|
2016-10-25 20:00:45 +08:00
|
|
|
(void) dma_fence_wait(bo->moving, true);
|
2017-02-19 05:59:56 +08:00
|
|
|
ttm_bo_unreserve(bo);
|
|
|
|
ttm_bo_unref(&bo);
|
2013-10-09 18:18:07 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ordinary wait.
|
|
|
|
*/
|
2016-10-25 20:00:45 +08:00
|
|
|
ret = dma_fence_wait(bo->moving, true);
|
2016-06-15 19:44:01 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
2013-10-09 18:18:07 +08:00
|
|
|
ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
|
|
|
|
VM_FAULT_NOPAGE;
|
2016-06-15 19:44:01 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_clear:
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_put(bo->moving);
|
2016-06-15 19:44:01 +08:00
|
|
|
bo->moving = NULL;
|
2013-10-09 18:18:07 +08:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-25 11:43:23 +08:00
|
|
|
static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo,
|
|
|
|
unsigned long page_offset)
|
|
|
|
{
|
|
|
|
struct ttm_bo_device *bdev = bo->bdev;
|
|
|
|
|
|
|
|
if (bdev->driver->io_mem_pfn)
|
|
|
|
return bdev->driver->io_mem_pfn(bo, page_offset);
|
|
|
|
|
2017-12-25 11:43:34 +08:00
|
|
|
return ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT)
|
|
|
|
+ page_offset;
|
2017-12-25 11:43:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 06:56:41 +08:00
|
|
|
static int ttm_bo_vm_fault(struct vm_fault *vmf)
|
2009-06-10 21:20:19 +08:00
|
|
|
{
|
2017-02-25 06:56:41 +08:00
|
|
|
struct vm_area_struct *vma = vmf->vma;
|
2009-06-10 21:20:19 +08:00
|
|
|
struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
|
|
|
|
vma->vm_private_data;
|
|
|
|
struct ttm_bo_device *bdev = bo->bdev;
|
|
|
|
unsigned long page_offset;
|
|
|
|
unsigned long page_last;
|
|
|
|
unsigned long pfn;
|
|
|
|
struct ttm_tt *ttm = NULL;
|
|
|
|
struct page *page;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2016-12-15 07:07:01 +08:00
|
|
|
unsigned long address = vmf->address;
|
2010-11-11 16:41:57 +08:00
|
|
|
struct ttm_mem_type_manager *man =
|
|
|
|
&bdev->man[bo->mem.mem_type];
|
2013-11-07 01:32:59 +08:00
|
|
|
struct vm_area_struct cvma;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Work around locking order reversal in fault / nopfn
|
|
|
|
* between mmap_sem and bo_reserve: Perform a trylock operation
|
2013-11-15 02:49:05 +08:00
|
|
|
* for reserve, and if it fails, retry the fault after waiting
|
|
|
|
* for the buffer to become unreserved.
|
2009-06-10 21:20:19 +08:00
|
|
|
*/
|
2016-04-06 17:12:03 +08:00
|
|
|
ret = ttm_bo_reserve(bo, true, true, NULL);
|
2009-06-10 21:20:19 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
2013-11-15 02:49:05 +08:00
|
|
|
if (ret != -EBUSY)
|
|
|
|
return VM_FAULT_NOPAGE;
|
|
|
|
|
|
|
|
if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
|
|
|
|
if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
|
2017-02-19 05:59:56 +08:00
|
|
|
ttm_bo_reference(bo);
|
2017-02-25 06:56:41 +08:00
|
|
|
up_read(&vmf->vma->vm_mm->mmap_sem);
|
2013-11-15 02:49:05 +08:00
|
|
|
(void) ttm_bo_wait_unreserved(bo);
|
2017-02-19 05:59:56 +08:00
|
|
|
ttm_bo_unref(&bo);
|
2013-11-15 02:49:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return VM_FAULT_RETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we'd want to change locking order to
|
|
|
|
* mmap_sem -> bo::reserve, we'd use a blocking reserve here
|
|
|
|
* instead of retrying the fault...
|
|
|
|
*/
|
2009-06-10 21:20:19 +08:00
|
|
|
return VM_FAULT_NOPAGE;
|
|
|
|
}
|
|
|
|
|
2014-01-03 18:17:18 +08:00
|
|
|
/*
|
|
|
|
* Refuse to fault imported pages. This should be handled
|
|
|
|
* (if at all) by redirecting mmap to the exporter.
|
|
|
|
*/
|
|
|
|
if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_SIGBUS;
|
2014-01-03 18:17:18 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2010-04-09 20:39:23 +08:00
|
|
|
if (bdev->driver->fault_reserve_notify) {
|
|
|
|
ret = bdev->driver->fault_reserve_notify(bo);
|
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -EBUSY:
|
|
|
|
case -ERESTARTSYS:
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_NOPAGE;
|
2010-04-09 20:39:23 +08:00
|
|
|
goto out_unlock;
|
|
|
|
default:
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_SIGBUS;
|
2010-04-09 20:39:23 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
}
|
2009-06-24 07:48:08 +08:00
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
/*
|
|
|
|
* Wait for buffer data in transit, due to a pipelined
|
|
|
|
* move.
|
|
|
|
*/
|
2017-02-25 06:56:41 +08:00
|
|
|
ret = ttm_bo_vm_fault_idle(bo, vmf);
|
2013-10-09 18:18:07 +08:00
|
|
|
if (unlikely(ret != 0)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
if (ret == VM_FAULT_RETRY &&
|
2017-02-19 05:59:56 +08:00
|
|
|
!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
|
|
|
|
/* The BO has already been unreserved. */
|
2018-01-26 22:22:05 +08:00
|
|
|
return ret;
|
2017-02-19 05:59:56 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 18:18:07 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
2010-11-11 16:41:57 +08:00
|
|
|
ret = ttm_mem_io_lock(man, true);
|
|
|
|
if (unlikely(ret != 0)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_NOPAGE;
|
2009-06-10 21:20:19 +08:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2010-11-11 16:41:57 +08:00
|
|
|
ret = ttm_mem_io_reserve_vm(bo);
|
|
|
|
if (unlikely(ret != 0)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_SIGBUS;
|
2010-11-11 16:41:57 +08:00
|
|
|
goto out_io_unlock;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
|
2013-12-09 15:23:57 +08:00
|
|
|
vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
|
|
|
|
page_last = vma_pages(vma) + vma->vm_pgoff -
|
|
|
|
drm_vma_node_start(&bo->vma_node);
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
if (unlikely(page_offset >= bo->num_pages)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_SIGBUS;
|
2010-11-11 16:41:57 +08:00
|
|
|
goto out_io_unlock;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-07 01:32:59 +08:00
|
|
|
* Make a local vma copy to modify the page_prot member
|
|
|
|
* and vm_flags if necessary. The vma parameter is protected
|
|
|
|
* by mmap_sem in write mode.
|
2009-06-10 21:20:19 +08:00
|
|
|
*/
|
2013-11-07 01:32:59 +08:00
|
|
|
cvma = *vma;
|
|
|
|
cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
|
|
|
|
|
2010-04-09 20:39:23 +08:00
|
|
|
if (bo->mem.bus.is_iomem) {
|
2013-11-07 01:32:59 +08:00
|
|
|
cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
|
|
|
|
cvma.vm_page_prot);
|
2009-06-10 21:20:19 +08:00
|
|
|
} else {
|
2017-12-21 17:42:50 +08:00
|
|
|
struct ttm_operation_ctx ctx = {
|
|
|
|
.interruptible = false,
|
|
|
|
.no_wait_gpu = false
|
|
|
|
};
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
ttm = bo->ttm;
|
2014-09-04 15:47:23 +08:00
|
|
|
cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
|
|
|
|
cvma.vm_page_prot);
|
2011-11-03 11:59:28 +08:00
|
|
|
|
|
|
|
/* Allocate all page at once, most common usage */
|
2018-02-01 21:39:29 +08:00
|
|
|
if (ttm_tt_populate(ttm, &ctx)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_OOM;
|
2011-11-03 11:59:28 +08:00
|
|
|
goto out_io_unlock;
|
|
|
|
}
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Speculatively prefault a number of pages. Only error on
|
|
|
|
* first page.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
|
2017-07-18 05:10:26 +08:00
|
|
|
if (bo->mem.bus.is_iomem) {
|
|
|
|
/* Iomem should not be marked encrypted */
|
|
|
|
cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot);
|
2017-12-25 11:43:23 +08:00
|
|
|
pfn = ttm_bo_io_mem_pfn(bo, page_offset);
|
2017-07-18 05:10:26 +08:00
|
|
|
} else {
|
2011-11-03 11:59:28 +08:00
|
|
|
page = ttm->pages[page_offset];
|
2009-06-10 21:20:19 +08:00
|
|
|
if (unlikely(!page && i == 0)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_OOM;
|
2010-11-11 16:41:57 +08:00
|
|
|
goto out_io_unlock;
|
2009-06-10 21:20:19 +08:00
|
|
|
} else if (unlikely(!page)) {
|
|
|
|
break;
|
|
|
|
}
|
2014-01-03 18:47:23 +08:00
|
|
|
page->index = drm_vma_node_start(&bo->vma_node) +
|
|
|
|
page_offset;
|
2009-06-10 21:20:19 +08:00
|
|
|
pfn = page_to_pfn(page);
|
|
|
|
}
|
|
|
|
|
2014-01-03 16:21:54 +08:00
|
|
|
if (vma->vm_flags & VM_MIXEDMAP)
|
2016-01-16 08:56:40 +08:00
|
|
|
ret = vm_insert_mixed(&cvma, address,
|
|
|
|
__pfn_to_pfn_t(pfn, PFN_DEV));
|
2014-01-03 16:21:54 +08:00
|
|
|
else
|
|
|
|
ret = vm_insert_pfn(&cvma, address, pfn);
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
/*
|
|
|
|
* Somebody beat us to this PTE or prefaulting to
|
|
|
|
* an already populated PTE, or prefaulting error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
|
|
|
|
break;
|
|
|
|
else if (unlikely(ret != 0)) {
|
2018-01-26 22:22:05 +08:00
|
|
|
ret =
|
2009-06-10 21:20:19 +08:00
|
|
|
(ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
|
2010-11-11 16:41:57 +08:00
|
|
|
goto out_io_unlock;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
address += PAGE_SIZE;
|
|
|
|
if (unlikely(++page_offset >= page_last))
|
|
|
|
break;
|
|
|
|
}
|
2018-01-26 22:22:05 +08:00
|
|
|
ret = VM_FAULT_NOPAGE;
|
2010-11-11 16:41:57 +08:00
|
|
|
out_io_unlock:
|
|
|
|
ttm_mem_io_unlock(man);
|
2009-06-10 21:20:19 +08:00
|
|
|
out_unlock:
|
|
|
|
ttm_bo_unreserve(bo);
|
2018-01-26 22:22:05 +08:00
|
|
|
return ret;
|
2009-06-10 21:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ttm_bo_vm_open(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct ttm_buffer_object *bo =
|
|
|
|
(struct ttm_buffer_object *)vma->vm_private_data;
|
|
|
|
|
2014-01-03 18:47:23 +08:00
|
|
|
WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
(void)ttm_bo_reference(bo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ttm_bo_vm_close(struct vm_area_struct *vma)
|
|
|
|
{
|
2010-04-09 20:39:23 +08:00
|
|
|
struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
|
2009-06-10 21:20:19 +08:00
|
|
|
|
|
|
|
ttm_bo_unref(&bo);
|
|
|
|
vma->vm_private_data = NULL;
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:01:16 +08:00
|
|
|
static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
|
|
|
|
unsigned long offset,
|
2018-01-26 22:32:29 +08:00
|
|
|
uint8_t *buf, int len, int write)
|
2017-07-14 05:01:16 +08:00
|
|
|
{
|
|
|
|
unsigned long page = offset >> PAGE_SHIFT;
|
|
|
|
unsigned long bytes_left = len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Copy a page at a time, that way no extra virtual address
|
|
|
|
* mapping is needed
|
|
|
|
*/
|
|
|
|
offset -= page << PAGE_SHIFT;
|
|
|
|
do {
|
|
|
|
unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
|
|
|
|
struct ttm_bo_kmap_obj map;
|
|
|
|
void *ptr;
|
|
|
|
bool is_iomem;
|
|
|
|
|
|
|
|
ret = ttm_bo_kmap(bo, page, 1, &map);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
|
|
|
|
WARN_ON_ONCE(is_iomem);
|
|
|
|
if (write)
|
|
|
|
memcpy(ptr, buf, bytes);
|
|
|
|
else
|
|
|
|
memcpy(buf, ptr, bytes);
|
|
|
|
ttm_bo_kunmap(&map);
|
|
|
|
|
|
|
|
page++;
|
2018-01-26 22:32:29 +08:00
|
|
|
buf += bytes;
|
2017-07-14 05:01:16 +08:00
|
|
|
bytes_left -= bytes;
|
|
|
|
offset = 0;
|
|
|
|
} while (bytes_left);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
|
|
|
|
void *buf, int len, int write)
|
|
|
|
{
|
|
|
|
unsigned long offset = (addr) - vma->vm_start;
|
|
|
|
struct ttm_buffer_object *bo = vma->vm_private_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
ret = ttm_bo_reserve(bo, true, false, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (bo->mem.mem_type) {
|
|
|
|
case TTM_PL_SYSTEM:
|
|
|
|
if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
|
|
|
|
ret = ttm_tt_swapin(bo->ttm);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case TTM_PL_TT:
|
|
|
|
ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (bo->bdev->driver->access_memory)
|
|
|
|
ret = bo->bdev->driver->access_memory(
|
|
|
|
bo, offset, buf, len, write);
|
|
|
|
else
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
ttm_bo_unreserve(bo);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-28 02:29:37 +08:00
|
|
|
static const struct vm_operations_struct ttm_bo_vm_ops = {
|
2009-06-10 21:20:19 +08:00
|
|
|
.fault = ttm_bo_vm_fault,
|
|
|
|
.open = ttm_bo_vm_open,
|
2017-07-14 05:01:16 +08:00
|
|
|
.close = ttm_bo_vm_close,
|
|
|
|
.access = ttm_bo_vm_access
|
2009-06-10 21:20:19 +08:00
|
|
|
};
|
|
|
|
|
2013-07-25 03:08:53 +08:00
|
|
|
static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
|
|
|
|
unsigned long offset,
|
|
|
|
unsigned long pages)
|
|
|
|
{
|
|
|
|
struct drm_vma_offset_node *node;
|
|
|
|
struct ttm_buffer_object *bo = NULL;
|
|
|
|
|
|
|
|
drm_vma_offset_lock_lookup(&bdev->vma_manager);
|
|
|
|
|
|
|
|
node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
|
|
|
|
if (likely(node)) {
|
|
|
|
bo = container_of(node, struct ttm_buffer_object, vma_node);
|
|
|
|
if (!kref_get_unless_zero(&bo->kref))
|
|
|
|
bo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_vma_offset_unlock_lookup(&bdev->vma_manager);
|
|
|
|
|
|
|
|
if (!bo)
|
|
|
|
pr_err("Could not find buffer object to map\n");
|
|
|
|
|
|
|
|
return bo;
|
|
|
|
}
|
|
|
|
|
2009-06-10 21:20:19 +08:00
|
|
|
int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
|
|
|
|
struct ttm_bo_device *bdev)
|
|
|
|
{
|
|
|
|
struct ttm_bo_driver *driver;
|
|
|
|
struct ttm_buffer_object *bo;
|
|
|
|
int ret;
|
|
|
|
|
2013-07-25 03:08:53 +08:00
|
|
|
bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
|
|
|
|
if (unlikely(!bo))
|
2009-06-10 21:20:19 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
driver = bo->bdev->driver;
|
|
|
|
if (unlikely(!driver->verify_access)) {
|
|
|
|
ret = -EPERM;
|
|
|
|
goto out_unref;
|
|
|
|
}
|
|
|
|
ret = driver->verify_access(bo, filp);
|
|
|
|
if (unlikely(ret != 0))
|
|
|
|
goto out_unref;
|
|
|
|
|
|
|
|
vma->vm_ops = &ttm_bo_vm_ops;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: We're transferring the bo reference to
|
|
|
|
* vma->vm_private_data here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vma->vm_private_data = bo;
|
2014-01-03 16:21:54 +08:00
|
|
|
|
|
|
|
/*
|
2014-03-12 17:41:32 +08:00
|
|
|
* We'd like to use VM_PFNMAP on shared mappings, where
|
|
|
|
* (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
|
|
|
|
* but for some reason VM_PFNMAP + x86 PAT + write-combine is very
|
|
|
|
* bad for performance. Until that has been sorted out, use
|
|
|
|
* VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
|
2014-01-03 16:21:54 +08:00
|
|
|
*/
|
2014-03-12 17:41:32 +08:00
|
|
|
vma->vm_flags |= VM_MIXEDMAP;
|
2014-01-03 16:21:54 +08:00
|
|
|
vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
|
2009-06-10 21:20:19 +08:00
|
|
|
return 0;
|
|
|
|
out_unref:
|
|
|
|
ttm_bo_unref(&bo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_bo_mmap);
|
|
|
|
|
|
|
|
int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
|
|
|
|
{
|
|
|
|
if (vma->vm_pgoff != 0)
|
|
|
|
return -EACCES;
|
|
|
|
|
|
|
|
vma->vm_ops = &ttm_bo_vm_ops;
|
|
|
|
vma->vm_private_data = ttm_bo_reference(bo);
|
2014-03-12 17:41:32 +08:00
|
|
|
vma->vm_flags |= VM_MIXEDMAP;
|
2014-01-03 16:21:54 +08:00
|
|
|
vma->vm_flags |= VM_IO | VM_DONTEXPAND;
|
2009-06-10 21:20:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ttm_fbdev_mmap);
|