1 /*
2  * Copyright © 2016 Broadcom
3  * Copyright © 2019 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include <assert.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <errno.h>
29 #include <sys/mman.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <fcntl.h>
34 
35 #include "drmtest.h"
36 #include "igt_aux.h"
37 #include "igt_core.h"
38 #include "igt_panfrost.h"
39 #include "ioctl_wrappers.h"
40 #include "intel_reg.h"
41 #include "intel_chipset.h"
42 #include "panfrost_drm.h"
43 #include "panfrost-job.h"
44 
45 /**
46  * SECTION:igt_panfrost
47  * @short_description: PANFROST support library
48  * @title: PANFROST
49  * @include: igt.h
50  *
51  * This library provides various auxiliary helper functions for writing PANFROST
52  * tests.
53  */
54 
55 struct panfrost_bo *
igt_panfrost_gem_new(int fd,size_t size)56 igt_panfrost_gem_new(int fd, size_t size)
57 {
58         struct panfrost_bo *bo = calloc(1, sizeof(*bo));
59 
60         struct drm_panfrost_create_bo create_bo = {
61                 .size = size,
62         };
63 
64         do_ioctl(fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
65 
66         bo->handle = create_bo.handle;
67         bo->offset = create_bo.offset;
68         bo->size = size;
69         return bo;
70 }
71 
72 void
igt_panfrost_free_bo(int fd,struct panfrost_bo * bo)73 igt_panfrost_free_bo(int fd, struct panfrost_bo *bo)
74 {
75         if (bo->map)
76                 munmap(bo->map, bo->size);
77         gem_close(fd, bo->handle);
78         free(bo);
79 }
80 
81 uint32_t
igt_panfrost_get_bo_offset(int fd,uint32_t handle)82 igt_panfrost_get_bo_offset(int fd, uint32_t handle)
83 {
84         struct drm_panfrost_get_bo_offset get = {
85                 .handle = handle,
86         };
87 
88         do_ioctl(fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get);
89 
90         return get.offset;
91 }
92 
93 uint32_t
igt_panfrost_get_param(int fd,int param)94 igt_panfrost_get_param(int fd, int param)
95 {
96         struct drm_panfrost_get_param get = {
97                 .param = param,
98         };
99 
100         do_ioctl(fd, DRM_IOCTL_PANFROST_GET_PARAM, &get);
101 
102         return get.value;
103 }
104 
105 void *
igt_panfrost_mmap_bo(int fd,uint32_t handle,uint32_t size,unsigned prot)106 igt_panfrost_mmap_bo(int fd, uint32_t handle, uint32_t size, unsigned prot)
107 {
108         struct drm_panfrost_mmap_bo mmap_bo = {
109                 .handle = handle,
110         };
111         void *ptr;
112 
113         mmap_bo.handle = handle;
114         do_ioctl(fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
115 
116         ptr = mmap(0, size, prot, MAP_SHARED, fd, mmap_bo.offset);
117         if (ptr == MAP_FAILED)
118                 return NULL;
119         else
120                 return ptr;
121 }
122 
igt_panfrost_bo_mmap(int fd,struct panfrost_bo * bo)123 void igt_panfrost_bo_mmap(int fd, struct panfrost_bo *bo)
124 {
125         bo->map = igt_panfrost_mmap_bo(fd, bo->handle, bo->size,
126                                   PROT_READ | PROT_WRITE);
127         igt_assert(bo->map);
128 }
129 
igt_panfrost_trivial_job(int fd,bool do_crash,int width,int height,uint32_t color)130 struct panfrost_submit *igt_panfrost_trivial_job(int fd, bool do_crash, int width, int height, uint32_t color)
131 {
132         struct panfrost_submit *submit;
133         struct mali_job_descriptor_header header = {
134                 .job_type = JOB_TYPE_FRAGMENT,
135                 .job_index = 1,
136                 .job_descriptor_size = 1,
137         };
138         struct mali_payload_fragment payload = {
139                 .min_tile_coord = MALI_COORDINATE_TO_TILE_MIN(0, 0),
140                 .max_tile_coord = MALI_COORDINATE_TO_TILE_MAX(ALIGN(width, 16), height),
141         };
142         struct bifrost_framebuffer mfbd_framebuffer = {
143             .unk0 = 0x0,
144             .unknown1 = 0x0,
145             .tiler_meta = 0xff00000000,
146             .width1 = MALI_POSITIVE(ALIGN(width, 16)),
147             .height1 = MALI_POSITIVE(height),
148             .width2 = MALI_POSITIVE(ALIGN(width, 16)),
149             .height2 = MALI_POSITIVE(height),
150             .unk1 = 0x1080,
151             .unk2 = 0x0,
152             .rt_count_1 = MALI_POSITIVE(1),
153             .rt_count_2 = 1,
154             .unk3 = 0x100,
155             .clear_stencil = 0x0,
156             .clear_depth = 0.000000,
157             .unknown2 = 0x1f,
158         };
159         struct mali_single_framebuffer sfbd_framebuffer = {
160             .unknown2 = 0x1f,
161             .width = MALI_POSITIVE(width),
162             .height = MALI_POSITIVE(height),
163             .stride = width * 4,
164             .resolution_check = ((width + height) / 3) << 4,
165             .tiler_flags = 0xfff,
166             .clear_color_1 = color,
167             .clear_color_2 = color,
168             .clear_color_3 = color,
169             .clear_color_4 = color,
170             .clear_flags = 0x101100 | MALI_CLEAR_SLOW,
171             .format = 0xb84e0281,
172         };
173         struct mali_rt_format fmt = {
174                 .unk1 = 0x4000000,
175                 .unk2 = 0x1,
176                 .nr_channels = MALI_POSITIVE(4),
177                 .flags = do_crash ? 0x444 | (1 << 8) : 0x444,
178                 .swizzle = MALI_CHANNEL_BLUE | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_RED << 6) | (MALI_CHANNEL_ONE << 9),
179                 .unk4 = 0x8,
180         };
181         struct bifrost_render_target rts = {
182                 .format = fmt,
183                 .chunknown = {
184                     .unk = 0x0,
185                     .pointer = 0x0,
186                 },
187                 .framebuffer_stride = ALIGN(width, 16) * 4 / 16,
188                 .clear_color_1 = color,
189                 .clear_color_2 = color,
190                 .clear_color_3 = color,
191                 .clear_color_4 = color,
192         };
193         int gpu_prod_id = igt_panfrost_get_param(fd, DRM_PANFROST_PARAM_GPU_PROD_ID);
194         uint32_t *known_unknown;
195         uint32_t *bos;
196 
197         submit = malloc(sizeof(*submit));
198 
199         submit->fbo = igt_panfrost_gem_new(fd, ALIGN(width, 16) * height * 4);
200         rts.framebuffer = submit->fbo->offset;
201         sfbd_framebuffer.framebuffer = submit->fbo->offset;
202 
203         submit->tiler_heap_bo = igt_panfrost_gem_new(fd, 32768 * 128);
204         mfbd_framebuffer.tiler_heap_start = submit->tiler_heap_bo->offset;
205         mfbd_framebuffer.tiler_heap_end = submit->tiler_heap_bo->offset + 32768 * 128;
206         sfbd_framebuffer.tiler_heap_free = mfbd_framebuffer.tiler_heap_start;
207         sfbd_framebuffer.tiler_heap_end = mfbd_framebuffer.tiler_heap_end;
208 
209         submit->tiler_scratch_bo = igt_panfrost_gem_new(fd, 128 * 128 * 128);
210         mfbd_framebuffer.tiler_scratch_start = submit->tiler_scratch_bo->offset;
211         mfbd_framebuffer.tiler_scratch_middle = submit->tiler_scratch_bo->offset + 0xf0000;
212         sfbd_framebuffer.unknown_address_0 = mfbd_framebuffer.tiler_scratch_start;
213 
214         submit->scratchpad_bo = igt_panfrost_gem_new(fd, 64 * 4096);
215         igt_panfrost_bo_mmap(fd, submit->scratchpad_bo);
216         mfbd_framebuffer.scratchpad = submit->scratchpad_bo->offset;
217         sfbd_framebuffer.unknown_address_1 = submit->scratchpad_bo->offset;
218         sfbd_framebuffer.unknown_address_2 = submit->scratchpad_bo->offset + 512;
219 
220         known_unknown = ((void*)submit->scratchpad_bo->map) + 512;
221         *known_unknown = 0xa0000000;
222 
223         if (gpu_prod_id >= 0x0750) {
224             submit->fb_bo = igt_panfrost_gem_new(fd, sizeof(mfbd_framebuffer) + sizeof(struct bifrost_render_target));
225             igt_panfrost_bo_mmap(fd, submit->fb_bo);
226             memcpy(submit->fb_bo->map, &mfbd_framebuffer, sizeof(mfbd_framebuffer));
227             memcpy(submit->fb_bo->map + sizeof(mfbd_framebuffer), &rts, sizeof(struct bifrost_render_target));
228             payload.framebuffer = submit->fb_bo->offset | MALI_MFBD;
229         } else {
230             // We don't know yet how to cause a hang on <=T720
231             // Should probably use an infinite loop to hang the GPU
232             igt_require(!do_crash);
233             submit->fb_bo = igt_panfrost_gem_new(fd, sizeof(sfbd_framebuffer));
234             igt_panfrost_bo_mmap(fd, submit->fb_bo);
235             memcpy(submit->fb_bo->map, &sfbd_framebuffer, sizeof(sfbd_framebuffer));
236             payload.framebuffer = submit->fb_bo->offset | MALI_SFBD;
237         }
238 
239         submit->submit_bo = igt_panfrost_gem_new(fd, sizeof(header) + sizeof(payload) + 1024000);
240         igt_panfrost_bo_mmap(fd, submit->submit_bo);
241 
242         memcpy(submit->submit_bo->map, &header, sizeof(header));
243         memcpy(submit->submit_bo->map + sizeof(header), &payload, sizeof(payload));
244 
245         submit->args = malloc(sizeof(*submit->args));
246         memset(submit->args, 0, sizeof(*submit->args));
247         submit->args->jc = submit->submit_bo->offset;
248         submit->args->requirements = PANFROST_JD_REQ_FS;
249 
250         bos = malloc(sizeof(*bos) * 6);
251         bos[0] = submit->fbo->handle;
252         bos[1] = submit->tiler_heap_bo->handle;
253         bos[2] = submit->tiler_scratch_bo->handle;
254         bos[3] = submit->scratchpad_bo->handle;
255         bos[4] = submit->fb_bo->handle;
256         bos[5] = submit->submit_bo->handle;
257 
258         submit->args->bo_handles = to_user_pointer(bos);
259         submit->args->bo_handle_count = 6;
260 
261         igt_assert_eq(drmSyncobjCreate(fd, DRM_SYNCOBJ_CREATE_SIGNALED, &submit->args->out_sync), 0);
262 
263         return submit;
264 }
265 
igt_panfrost_free_job(int fd,struct panfrost_submit * submit)266 void igt_panfrost_free_job(int fd, struct panfrost_submit *submit)
267 {
268         free(from_user_pointer(submit->args->bo_handles));
269         igt_panfrost_free_bo(fd, submit->submit_bo);
270         igt_panfrost_free_bo(fd, submit->fb_bo);
271         igt_panfrost_free_bo(fd, submit->scratchpad_bo);
272         igt_panfrost_free_bo(fd, submit->tiler_scratch_bo);
273         igt_panfrost_free_bo(fd, submit->tiler_heap_bo);
274         igt_panfrost_free_bo(fd, submit->fbo);
275         free(submit->args);
276         free(submit);
277 }
278