1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6 
7 #include <drm/drm_file.h>
8 
9 #include "i915_drv.h"
10 #include "igt_mmap.h"
11 
12 unsigned long igt_mmap_offset(struct drm_i915_private *i915,
13 			      u64 offset,
14 			      unsigned long size,
15 			      unsigned long prot,
16 			      unsigned long flags)
17 {
18 	struct drm_vma_offset_node *node;
19 	struct file *file;
20 	unsigned long addr;
21 	int err;
22 
23 	/* no need to refcount, we own this object */
24 	drm_vma_offset_lock_lookup(i915->drm.vma_offset_manager);
25 	node = drm_vma_offset_exact_lookup_locked(i915->drm.vma_offset_manager,
26 						  offset / PAGE_SIZE, size / PAGE_SIZE);
27 	drm_vma_offset_unlock_lookup(i915->drm.vma_offset_manager);
28 
29 	if (GEM_WARN_ON(!node)) {
30 		pr_info("Failed to lookup %llx\n", offset);
31 		return -ENOENT;
32 	}
33 
34 	/* Pretend to open("/dev/dri/card0") */
35 	file = mock_drm_getfile(i915->drm.primary, O_RDWR);
36 	if (IS_ERR(file))
37 		return PTR_ERR(file);
38 
39 	err = drm_vma_node_allow(node, file->private_data);
40 	if (err) {
41 		addr = err;
42 		goto out_file;
43 	}
44 
45 	addr = vm_mmap(file, 0, drm_vma_node_size(node) << PAGE_SHIFT,
46 		       prot, flags, drm_vma_node_offset_addr(node));
47 
48 	drm_vma_node_revoke(node, file->private_data);
49 out_file:
50 	fput(file);
51 	return addr;
52 }
53