summaryrefslogtreecommitdiff
path: root/tests/prime_udl.c
blob: 81670c4d08fc9909100ab528e93441edad894872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* basic set of prime tests between intel and nouveau */

/* test list - 
   1. share buffer from intel -> nouveau.
   2. share buffer from nouveau -> intel
   3. share intel->nouveau, map on both, write intel, read nouveau
   4. share intel->nouveau, blit intel fill, readback on nouveau
   test 1 + map buffer, read/write, map other size.
   do some hw actions on the buffer
   some illegal operations -
       close prime fd try and map

   TODO add some nouveau rendering tests
*/

   
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "xf86drm.h"
#include "xf86drmMode.h"
#include "i915_drm.h"
#include "intel_bufmgr.h"
#include "intel_gpu_tools.h"
#include "intel_batchbuffer.h"

int intel_fd = -1, udl_fd = -1;
drm_intel_bufmgr *bufmgr;
uint32_t devid;
struct intel_batchbuffer *intel_batch;

#define BO_SIZE (640*480*2)

static int find_and_open_devices(void)
{
	int i;
	char path[80];
	struct stat buf;
	FILE *fl;
	char vendor_id[8];
	int venid;
	for (i = 0; i < 9; i++) {
		sprintf(path, "/sys/class/drm/card%d/device/vendor", i);
		if (stat(path, &buf)) {
			/* look for usb dev */
			sprintf(path, "/sys/class/drm/card%d/device/idVendor", i);
			if (stat(path, &buf))
				break;
		}

		fl = fopen(path, "r");
		if (!fl)
			break;

		fgets(vendor_id, 8, fl);
		fclose(fl);

		venid = strtoul(vendor_id, NULL, 16);
		sprintf(path, "/dev/dri/card%d", i);
		if (venid == 0x8086) {
			intel_fd = open(path, O_RDWR);
			if (!intel_fd)
				return -1;
		} else if (venid == 0x17e9) {
			udl_fd = open(path, O_RDWR);
			if (!udl_fd)
				return -1;
		}
	}
	return 0;
}

static int dumb_bo_destroy(int fd, uint32_t handle)
{

	struct drm_mode_destroy_dumb arg;
	int ret;
	memset(&arg, 0, sizeof(arg));
	arg.handle = handle;
	ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
	if (ret)
		return -errno;
	return 0;

}

/*
 * simple share and import
 */
static int test1(void)
{
	drm_intel_bo *test_intel_bo;
	int prime_fd;
	int ret;
	uint32_t udl_handle;

	test_intel_bo = drm_intel_bo_alloc(bufmgr, "test bo", BO_SIZE, 4096);

	drm_intel_bo_gem_export_to_prime(test_intel_bo, &prime_fd);

	ret = drmPrimeFDToHandle(udl_fd, prime_fd, &udl_handle);

	dumb_bo_destroy(udl_fd, udl_handle);
	drm_intel_bo_unreference(test_intel_bo);
	return ret;
}

static int test2(void)
{
	drm_intel_bo *test_intel_bo;
	uint32_t fb_id;
	drmModeClip clip;
	int prime_fd;
	uint32_t udl_handle;
	int ret;

	test_intel_bo = drm_intel_bo_alloc(bufmgr, "test bo", BO_SIZE, 4096);

	drm_intel_bo_gem_export_to_prime(test_intel_bo, &prime_fd);

	ret = drmPrimeFDToHandle(udl_fd, prime_fd, &udl_handle);
	if (ret)
		goto out;

	ret = drmModeAddFB(udl_fd, 640, 480, 16, 16, 640, udl_handle, &fb_id);
	if (ret)
		goto out;

	clip.x1 = 0;
	clip.y1 = 0;
	clip.x2 = 10;
	clip.y2 = 10;
	ret = drmModeDirtyFB(udl_fd, fb_id, &clip, 1);
	if (ret) {
		return ret;
	}
out:
	dumb_bo_destroy(udl_fd, udl_handle);
	drm_intel_bo_unreference(test_intel_bo);
	return ret;
}

int main(int argc, char **argv)
{
	int ret;

	ret = find_and_open_devices();
	if (ret < 0)
		return ret;

	if (udl_fd == -1 || intel_fd == -1) {
		printf("failed to find intel and udl GPU\n");
		return 77;
	}

	/* set up intel bufmgr */
	bufmgr = drm_intel_bufmgr_gem_init(intel_fd, 4096);
	drm_intel_bufmgr_gem_enable_reuse(bufmgr);

	/* set up an intel batch buffer */
	devid = intel_get_drm_devid(intel_fd);
	intel_batch = intel_batchbuffer_alloc(bufmgr, devid);

	/* create an object on the i915 */
	ret = test1();
	if (ret) {
		fprintf(stderr,"prime_test: failed test 1\n");
		return -1;
	}

	ret = test2();
	if (ret)
		fprintf(stderr,"prime_test: failed test 2 %d\n", ret);

	intel_batchbuffer_free(intel_batch);

	drm_intel_bufmgr_destroy(bufmgr);

	close(intel_fd);
	close(udl_fd);

	return ret;
}