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