1 /*
2  * Copyright © 2016 Broadcom
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <assert.h>
25 #include <string.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33 
34 #include "drmtest.h"
35 #include "igt_aux.h"
36 #include "igt_core.h"
37 #include "igt_v3d.h"
38 #include "ioctl_wrappers.h"
39 #include "intel_reg.h"
40 #include "intel_chipset.h"
41 #include "v3d_drm.h"
42 
43 /**
44  * SECTION:igt_v3d
45  * @short_description: V3D support library
46  * @title: V3D
47  * @include: igt.h
48  *
49  * This library provides various auxiliary helper functions for writing V3D
50  * tests.
51  */
52 
53 struct v3d_bo *
igt_v3d_create_bo(int fd,size_t size)54 igt_v3d_create_bo(int fd, size_t size)
55 {
56 	struct v3d_bo *bo = calloc(1, sizeof(*bo));
57 
58 	struct drm_v3d_create_bo create = {
59 		.size = size,
60 	};
61 
62 	do_ioctl(fd, DRM_IOCTL_V3D_CREATE_BO, &create);
63 
64 	bo->handle = create.handle;
65 	bo->offset = create.offset;
66 	bo->size = size;
67 
68 	return bo;
69 }
70 
71 void
igt_v3d_free_bo(int fd,struct v3d_bo * bo)72 igt_v3d_free_bo(int fd, struct v3d_bo *bo)
73 {
74 	if (bo->map)
75 		munmap(bo->map, bo->size);
76 	gem_close(fd, bo->handle);
77 	free(bo);
78 }
79 
80 uint32_t
igt_v3d_get_bo_offset(int fd,uint32_t handle)81 igt_v3d_get_bo_offset(int fd, uint32_t handle)
82 {
83 	struct drm_v3d_get_bo_offset get = {
84 		.handle = handle,
85 	};
86 
87 	do_ioctl(fd, DRM_IOCTL_V3D_GET_BO_OFFSET, &get);
88 
89 	return get.offset;
90 }
91 
92 uint32_t
igt_v3d_get_param(int fd,enum drm_v3d_param param)93 igt_v3d_get_param(int fd, enum drm_v3d_param param)
94 {
95 	struct drm_v3d_get_param get = {
96 		.param = param,
97 	};
98 
99 	do_ioctl(fd, DRM_IOCTL_V3D_GET_PARAM, &get);
100 
101 	return get.value;
102 }
103 
104 void *
igt_v3d_mmap_bo(int fd,uint32_t handle,uint32_t size,unsigned prot)105 igt_v3d_mmap_bo(int fd, uint32_t handle, uint32_t size, unsigned prot)
106 {
107 	struct drm_v3d_mmap_bo mmap_bo = {
108 		.handle = handle,
109 	};
110 	void *ptr;
111 
112 	do_ioctl(fd, DRM_IOCTL_V3D_MMAP_BO, &mmap_bo);
113 
114 	ptr = mmap(0, size, prot, MAP_SHARED, fd, mmap_bo.offset);
115 	if (ptr == MAP_FAILED)
116 		return NULL;
117 	else
118 		return ptr;
119 }
120 
igt_v3d_bo_mmap(int fd,struct v3d_bo * bo)121 void igt_v3d_bo_mmap(int fd, struct v3d_bo *bo)
122 {
123 	bo->map = igt_v3d_mmap_bo(fd, bo->handle, bo->size,
124 				  PROT_READ | PROT_WRITE);
125 	igt_assert(bo->map);
126 }
127