1 /* basic set of prime tests between intel and nouveau */
2 
3 /* test list -
4    1. share buffer from intel -> nouveau.
5    2. share buffer from nouveau -> intel
6    3. share intel->nouveau, map on both, write intel, read nouveau
7    4. share intel->nouveau, blit intel fill, readback on nouveau
8    test 1 + map buffer, read/write, map other size.
9    do some hw actions on the buffer
10    some illegal operations -
11        close prime fd try and map
12 
13    TODO add some nouveau rendering tests
14 */
15 
16 
17 #include "igt.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #include <errno.h>
26 
27 #include "xf86drm.h"
28 #include <xf86drmMode.h>
29 
30 #include "intel_bufmgr.h"
31 
32 int intel_fd = -1, udl_fd = -1;
33 drm_intel_bufmgr *bufmgr;
34 uint32_t devid;
35 struct intel_batchbuffer *intel_batch;
36 
37 #define BO_SIZE (640*480*2)
38 
find_and_open_devices(void)39 static int find_and_open_devices(void)
40 {
41 	int i;
42 	char path[80];
43 	struct stat buf;
44 	FILE *fl;
45 	char vendor_id[8];
46 	int venid;
47 	for (i = 0; i < 9; i++) {
48 		sprintf(path, "/sys/class/drm/card%d/device/vendor", i);
49 		if (stat(path, &buf)) {
50 			/* look for usb dev */
51 			sprintf(path, "/sys/class/drm/card%d/device/idVendor", i);
52 			if (stat(path, &buf))
53 				break;
54 		}
55 
56 		fl = fopen(path, "r");
57 		if (!fl)
58 			break;
59 
60 		igt_assert(fgets(vendor_id, 8, fl) != NULL);
61 		fclose(fl);
62 
63 		venid = strtoul(vendor_id, NULL, 16);
64 		sprintf(path, "/dev/dri/card%d", i);
65 		if (venid == 0x8086) {
66 			intel_fd = open(path, O_RDWR);
67 			if (!intel_fd)
68 				return -1;
69 		} else if (venid == 0x17e9) {
70 			udl_fd = open(path, O_RDWR);
71 			if (!udl_fd)
72 				return -1;
73 		}
74 	}
75 	return 0;
76 }
77 
dumb_bo_destroy(int fd,uint32_t handle)78 static int dumb_bo_destroy(int fd, uint32_t handle)
79 {
80 
81 	struct drm_mode_destroy_dumb arg;
82 	int ret;
83 	memset(&arg, 0, sizeof(arg));
84 	arg.handle = handle;
85 	ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
86 	if (ret)
87 		return -errno;
88 	return 0;
89 
90 }
91 
92 /*
93  * simple share and import
94  */
test1(void)95 static int test1(void)
96 {
97 	drm_intel_bo *test_intel_bo;
98 	int prime_fd;
99 	int ret;
100 	uint32_t udl_handle;
101 
102 	test_intel_bo = drm_intel_bo_alloc(bufmgr, "test bo", BO_SIZE, 4096);
103 
104 	drm_intel_bo_gem_export_to_prime(test_intel_bo, &prime_fd);
105 
106 	ret = drmPrimeFDToHandle(udl_fd, prime_fd, &udl_handle);
107 
108 	dumb_bo_destroy(udl_fd, udl_handle);
109 	drm_intel_bo_unreference(test_intel_bo);
110 	return ret;
111 }
112 
test2(void)113 static int test2(void)
114 {
115 	drm_intel_bo *test_intel_bo;
116 	uint32_t fb_id;
117 	drmModeClip clip;
118 	int prime_fd;
119 	uint32_t udl_handle;
120 	int ret;
121 
122 	test_intel_bo = drm_intel_bo_alloc(bufmgr, "test bo", BO_SIZE, 4096);
123 
124 	drm_intel_bo_gem_export_to_prime(test_intel_bo, &prime_fd);
125 
126 	ret = drmPrimeFDToHandle(udl_fd, prime_fd, &udl_handle);
127 	if (ret)
128 		goto out;
129 
130 	ret = drmModeAddFB(udl_fd, 640, 480, 16, 16, 640, udl_handle, &fb_id);
131 	if (ret)
132 		goto out;
133 
134 	clip.x1 = 0;
135 	clip.y1 = 0;
136 	clip.x2 = 10;
137 	clip.y2 = 10;
138 	ret = drmModeDirtyFB(udl_fd, fb_id, &clip, 1);
139 	if (ret) {
140 		return ret;
141 	}
142 out:
143 	dumb_bo_destroy(udl_fd, udl_handle);
144 	drm_intel_bo_unreference(test_intel_bo);
145 	return ret;
146 }
147 
148 igt_simple_main
149 {
150 	igt_skip_on_simulation();
151 
152 	igt_assert(find_and_open_devices() >= 0);
153 
154 	igt_skip_on(udl_fd == -1);
155 	igt_skip_on(intel_fd == -1);
156 
157 	/* set up intel bufmgr */
158 	bufmgr = drm_intel_bufmgr_gem_init(intel_fd, 4096);
159 	drm_intel_bufmgr_gem_enable_reuse(bufmgr);
160 
161 	/* set up an intel batch buffer */
162 	devid = intel_get_drm_devid(intel_fd);
163 	intel_batch = intel_batchbuffer_alloc(bufmgr, devid);
164 
165 	/* create an object on the i915 */
166 	igt_assert(test1() == 0);
167 
168 	igt_assert(test2() == 0);
169 
170 	intel_batchbuffer_free(intel_batch);
171 
172 	drm_intel_bufmgr_destroy(bufmgr);
173 
174 	close(intel_fd);
175 	close(udl_fd);
176 }
177