lib/igt.cocci: Add stanza for for_each_pipe
[platform/upstream/intel-gpu-tools.git] / tests / kms_addfb.c
1 /*
2  * Copyright © 2013 Intel Corporation
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  * Authors:
24  *    Daniel Vetter <daniel.vetter@ffwll.ch>
25  *
26  */
27
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include "drm.h"
38 #include "ioctl_wrappers.h"
39 #include "drmtest.h"
40 #include "drm_fourcc.h"
41
42 uint32_t gem_bo;
43 uint32_t gem_bo_small;
44
45 static void pitch_tests(int fd)
46 {
47         struct drm_mode_fb_cmd2 f = {};
48         int bad_pitches[] = { 0, 32, 63, 128, 256, 256*4, 999, 64*1024 };
49         int i;
50
51         f.width = 512;
52         f.height = 512;
53         f.pixel_format = DRM_FORMAT_XRGB8888;
54         f.pitches[0] = 1024*4;
55
56         igt_fixture {
57                 gem_bo = gem_create(fd, 1024*1024*4);
58                 igt_assert(gem_bo);
59         }
60
61         igt_subtest("no-handle") {
62                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
63                            errno == EINVAL);
64         }
65
66         f.handles[0] = gem_bo;
67         igt_subtest("normal") {
68                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == 0);
69                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f.fb_id) == 0);
70                 f.fb_id = 0;
71         }
72
73         for (i = 0; i < ARRAY_SIZE(bad_pitches); i++) {
74                 igt_subtest_f("bad-pitch-%i", bad_pitches[i]) {
75                         f.pitches[0] = bad_pitches[i];
76                         igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
77                                    errno == EINVAL);
78                 }
79         }
80
81         igt_fixture
82                 gem_set_tiling(fd, gem_bo, I915_TILING_X, 1024*4);
83         f.pitches[0] = 1024*4;
84
85         igt_subtest("X-tiled") {
86                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == 0);
87                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f.fb_id) == 0);
88                 f.fb_id = 0;
89         }
90
91         igt_subtest("framebuffer-vs-set-tiling") {
92                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == 0);
93                 igt_assert(__gem_set_tiling(fd, gem_bo, I915_TILING_X, 512*4) == -EBUSY);
94                 igt_assert(__gem_set_tiling(fd, gem_bo, I915_TILING_X, 1024*4) == -EBUSY);
95                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f.fb_id) == 0);
96                 f.fb_id = 0;
97         }
98
99         f.pitches[0] = 512*4;
100         igt_subtest("tile-pitch-mismatch") {
101                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
102                            errno == EINVAL);
103         }
104
105         igt_fixture
106                 gem_set_tiling(fd, gem_bo, I915_TILING_Y, 1024*4);
107         f.pitches[0] = 1024*4;
108         igt_subtest("Y-tiled") {
109                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
110                            errno == EINVAL);
111         }
112
113         igt_fixture
114                 gem_close(fd, gem_bo);
115 }
116
117 static void size_tests(int fd)
118 {
119         struct drm_mode_fb_cmd2 f = {};
120         struct drm_mode_fb_cmd2 f_16 = {};
121         struct drm_mode_fb_cmd2 f_8 = {};
122
123         f.width = 1024;
124         f.height = 1024;
125         f.pixel_format = DRM_FORMAT_XRGB8888;
126         f.pitches[0] = 1024*4;
127
128         f_16.width = 1024;
129         f_16.height = 1024*2;
130         f_16.pixel_format = DRM_FORMAT_RGB565;
131         f_16.pitches[0] = 1024*2;
132
133         f_8.width = 1024*2;
134         f_8.height = 1024*2;
135         f_8.pixel_format = DRM_FORMAT_C8;
136         f_8.pitches[0] = 1024*2;
137
138         igt_fixture {
139                 gem_bo = gem_create(fd, 1024*1024*4);
140                 igt_assert(gem_bo);
141                 gem_bo_small = gem_create(fd, 1024*1024*4 - 4096);
142                 igt_assert(gem_bo_small);
143         }
144
145         f.handles[0] = gem_bo;
146         f_16.handles[0] = gem_bo;
147         f_8.handles[0] = gem_bo;
148
149         igt_subtest("size-max") {
150                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == 0);
151                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f.fb_id) == 0);
152                 f.fb_id = 0;
153                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_16) == 0);
154                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f_16.fb_id) == 0);
155                 f.fb_id = 0;
156                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_8) == 0);
157                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f_8.fb_id) == 0);
158                 f.fb_id = 0;
159         }
160
161         f.width++;
162         f_16.width++;
163         f_8.width++;
164         igt_subtest("too-wide") {
165                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
166                            errno == EINVAL);
167                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_16) == -1 &&
168                            errno == EINVAL);
169                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_8) == -1 &&
170                            errno == EINVAL);
171         }
172         f.width--;
173         f_16.width--;
174         f_8.width--;
175         f.height++;
176         f_16.height++;
177         f_8.height++;
178         igt_subtest("too-high") {
179                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
180                            errno == EINVAL);
181                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_16) == -1 &&
182                            errno == EINVAL);
183                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f_8) == -1 &&
184                            errno == EINVAL);
185         }
186
187         f.handles[0] = gem_bo_small;
188         igt_subtest("bo-too-small") {
189                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
190                            errno == EINVAL);
191         }
192
193         /* Just to check that the parameters would work. */
194         f.height = 1020;
195         igt_subtest("small-bo") {
196                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == 0);
197                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &f.fb_id) == 0);
198                 f.fb_id = 0;
199         }
200
201         igt_fixture
202                 gem_set_tiling(fd, gem_bo_small, I915_TILING_X, 1024*4);
203
204         igt_subtest("bo-too-small-due-to-tiling") {
205                 igt_assert(drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &f) == -1 &&
206                            errno == EINVAL);
207         }
208
209
210         igt_fixture {
211                 gem_close(fd, gem_bo);
212                 gem_close(fd, gem_bo_small);
213         }
214 }
215
216 int fd;
217
218 igt_main
219 {
220         igt_fixture
221                 fd = drm_open_any();
222
223         pitch_tests(fd);
224
225         size_tests(fd);
226
227         igt_fixture
228                 close(fd);
229 }