1 #include "gem_msm.h"
2 #include "drm-uapi/msm_drm.h"
3 
4 #include "drm.h"
5 
gem_msm_mmap(void ** ptr,int drm_fd,uint32_t gem_handle,size_t size)6 static int gem_msm_mmap(void **ptr, int drm_fd, uint32_t gem_handle, size_t size)
7 {
8 	struct drm_msm_gem_cpu_prep gem_prep = {
9 		.handle = gem_handle,
10 		.op = MSM_PREP_READ,
11 		.timeout = { .tv_sec = 1, .tv_nsec = 0 }
12 	};
13 
14 	if (drmIoctl(drm_fd, DRM_IOCTL_MSM_GEM_CPU_PREP, &gem_prep))
15 	{
16 		return -1;
17 	}
18 
19 	struct drm_msm_gem_info gem_info = {
20 		.handle = gem_handle,
21 		.flags = 0,
22 		.offset = 0
23 	};
24 
25 	if (drmIoctl(drm_fd, DRM_IOCTL_MSM_GEM_INFO, &gem_info))
26 	{
27 		return -1;
28 	}
29 
30 	void *const k_addr = 0;
31 	const int k_prot = PROT_READ | PROT_WRITE;
32 	const int k_flags = MAP_SHARED;
33 
34 	void *ret = mmap(k_addr, size, k_prot, k_flags, drm_fd, gem_info.offset);
35 
36 	if (ret == MAP_FAILED)
37 	{
38 		return -1;
39 	}
40 
41 	*ptr = ret;
42 
43 	return 0;
44 }
45 
gem_msm_munmap(int drm_fd,uint32_t gem_handle,void * ptr,size_t size)46 static int gem_msm_munmap(int drm_fd, uint32_t gem_handle, void *ptr, size_t size)
47 {
48 	if (munmap(ptr, size))
49 	{
50 		return -1;
51 	}
52 
53 	struct drm_msm_gem_cpu_fini gem_fini = {
54 		.handle = gem_handle
55 	};
56 
57 	if (drmIoctl(drm_fd, DRM_IOCTL_MSM_GEM_CPU_FINI, &gem_fini))
58 	{
59 		return -1;
60 	}
61 
62 	return 0;
63 }
64 
65 struct gem_driver gem_msm_driver = {
66 	.mmap = gem_msm_mmap,
67 	.munmap = gem_msm_munmap
68 };
69