Alyssa Rosenzweig | fd18695 | 2020-03-24 13:24:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 Collabora Ltd. |
| 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 FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | * |
| 23 | * Authors (Collabora): |
| 24 | * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> |
| 25 | */ |
| 26 | |
| 27 | #include "bit.h" |
Alyssa Rosenzweig | 1c62b55 | 2020-08-05 16:16:00 -0400 | [diff] [blame] | 28 | #include "panfrost/lib/decode.h" |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 29 | #include "drm-uapi/panfrost_drm.h" |
Alyssa Rosenzweig | d62a6e7 | 2020-08-05 16:25:52 -0400 | [diff] [blame] | 30 | #include "panfrost/lib/pan_encoder.h" |
Alyssa Rosenzweig | fd18695 | 2020-03-24 13:24:03 -0400 | [diff] [blame] | 31 | |
| 32 | /* Standalone compiler tests submitting jobs directly to the hardware. Uses the |
| 33 | * `bit` prefix for `BIfrost Tests` and because bit sounds wicked cool. */ |
| 34 | |
| 35 | static struct panfrost_bo * |
| 36 | bit_bo_create(struct panfrost_device *dev, size_t size) |
| 37 | { |
| 38 | struct panfrost_bo *bo = panfrost_bo_create(dev, size, PAN_BO_EXECUTE); |
| 39 | pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL); |
| 40 | return bo; |
| 41 | } |
Alyssa Rosenzweig | bf1929e | 2020-03-24 13:48:06 -0400 | [diff] [blame] | 42 | |
| 43 | struct panfrost_device * |
| 44 | bit_initialize(void *memctx) |
| 45 | { |
| 46 | int fd = drmOpenWithType("panfrost", NULL, DRM_NODE_RENDER); |
| 47 | |
| 48 | if (fd < 0) |
| 49 | unreachable("No panfrost device found. Try chmod?"); |
| 50 | |
| 51 | struct panfrost_device *dev = rzalloc(memctx, struct panfrost_device); |
| 52 | panfrost_open_device(memctx, fd, dev); |
| 53 | |
| 54 | pandecode_initialize(true); |
| 55 | printf("%X\n", dev->gpu_id); |
| 56 | |
| 57 | return dev; |
| 58 | } |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 59 | |
| 60 | static bool |
| 61 | bit_submit(struct panfrost_device *dev, |
| 62 | enum mali_job_type T, |
| 63 | void *payload, size_t payload_size, |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 64 | struct panfrost_bo **bos, size_t bo_count, enum bit_debug debug) |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 65 | { |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 66 | struct panfrost_bo *job = bit_bo_create(dev, 4096); |
Boris Brezillon | eb92354 | 2020-09-08 07:07:41 +0200 | [diff] [blame] | 67 | pan_pack(job->cpu, JOB_HEADER, cfg) { |
| 68 | cfg.type = T; |
| 69 | cfg.index = 1; |
| 70 | } |
| 71 | memcpy(job->cpu + MALI_JOB_HEADER_LENGTH, payload, payload_size); |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 72 | |
| 73 | uint32_t *bo_handles = calloc(sizeof(uint32_t), bo_count); |
| 74 | |
| 75 | for (unsigned i = 0; i < bo_count; ++i) |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 76 | bo_handles[i] = bos[i]->gem_handle; |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 77 | |
| 78 | uint32_t syncobj = 0; |
| 79 | int ret = 0; |
| 80 | |
| 81 | ret = drmSyncobjCreate(dev->fd, DRM_SYNCOBJ_CREATE_SIGNALED, &syncobj); |
| 82 | assert(!ret); |
| 83 | |
| 84 | struct drm_panfrost_submit submit = { |
| 85 | .jc = job->gpu, |
| 86 | .bo_handles = (uintptr_t) bo_handles, |
| 87 | .bo_handle_count = bo_count, |
| 88 | .out_sync = syncobj, |
| 89 | }; |
| 90 | |
| 91 | ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit); |
| 92 | assert(!ret); |
| 93 | free(bo_handles); |
| 94 | |
| 95 | drmSyncobjWait(dev->fd, &syncobj, 1, INT64_MAX, 0, NULL); |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 96 | if (debug >= BIT_DEBUG_ALL) |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 97 | pandecode_jc(submit.jc, true, dev->gpu_id, false); |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | /* Checks that the device is alive and responding to basic jobs as a sanity |
| 102 | * check - prerequisite to running code on the device. We test this via a |
| 103 | * WRITE_VALUE job */ |
| 104 | |
| 105 | bool |
| 106 | bit_sanity_check(struct panfrost_device *dev) |
| 107 | { |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 108 | struct panfrost_bo *scratch = bit_bo_create(dev, 65536); |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 109 | ((uint32_t *) scratch->cpu)[0] = 0xAA; |
| 110 | |
Boris Brezillon | 9121e7d | 2020-09-08 10:39:23 +0200 | [diff] [blame] | 111 | struct mali_write_value_job_payload_packed payload; |
| 112 | |
| 113 | pan_pack(&payload, WRITE_VALUE_JOB_PAYLOAD, cfg) { |
| 114 | cfg.address = scratch->gpu; |
| 115 | cfg.type = MALI_WRITE_VALUE_TYPE_ZERO; |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 118 | struct panfrost_bo *bos[] = { scratch }; |
Alyssa Rosenzweig | 4b7056b | 2020-08-05 18:40:44 -0400 | [diff] [blame] | 119 | bool success = bit_submit(dev, MALI_JOB_TYPE_WRITE_VALUE, |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 120 | &payload, sizeof(payload), bos, 1, false); |
Alyssa Rosenzweig | a0d1be3 | 2020-03-24 14:08:16 -0400 | [diff] [blame] | 121 | |
| 122 | return success && (((uint8_t *) scratch->cpu)[0] == 0x0); |
| 123 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 124 | |
| 125 | /* Constructs a vertex job */ |
| 126 | |
| 127 | bool |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 128 | bit_vertex(struct panfrost_device *dev, panfrost_program prog, |
| 129 | uint32_t *iubo, size_t sz_ubo, |
| 130 | uint32_t *iattr, size_t sz_attr, |
| 131 | uint32_t *expected, size_t sz_expected, enum bit_debug debug) |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 132 | { |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 133 | struct panfrost_bo *shader = bit_bo_create(dev, prog.compiled.size); |
| 134 | struct panfrost_bo *shader_desc = bit_bo_create(dev, 4096); |
| 135 | struct panfrost_bo *ubo = bit_bo_create(dev, 4096); |
| 136 | struct panfrost_bo *var = bit_bo_create(dev, 4096); |
| 137 | struct panfrost_bo *attr = bit_bo_create(dev, 4096); |
| 138 | |
Alyssa Rosenzweig | b10f34a | 2020-09-09 16:29:41 -0400 | [diff] [blame] | 139 | pan_pack(attr->cpu, ATTRIBUTE, cfg) { |
Alyssa Rosenzweig | eb2762a | 2020-08-14 15:27:40 -0400 | [diff] [blame] | 140 | cfg.format = (MALI_RGBA32UI << 12); |
| 141 | cfg.unknown = true; |
Alyssa Rosenzweig | fa94967 | 2020-08-05 21:39:25 -0400 | [diff] [blame] | 142 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 143 | |
Alyssa Rosenzweig | b10f34a | 2020-09-09 16:29:41 -0400 | [diff] [blame] | 144 | pan_pack(var->cpu, ATTRIBUTE, cfg) { |
Alyssa Rosenzweig | eb2762a | 2020-08-14 15:27:40 -0400 | [diff] [blame] | 145 | cfg.format = (MALI_RGBA32UI << 12); |
Alyssa Rosenzweig | b10f34a | 2020-09-09 16:29:41 -0400 | [diff] [blame] | 146 | cfg.unknown = false; |
| 147 | } |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 148 | |
Alyssa Rosenzweig | eb2762a | 2020-08-14 15:27:40 -0400 | [diff] [blame] | 149 | pan_pack(var->cpu + 256, ATTRIBUTE_BUFFER, cfg) { |
| 150 | cfg.pointer = (var->gpu + 1024); |
| 151 | cfg.size = 1024; |
| 152 | } |
| 153 | |
| 154 | pan_pack(attr->cpu + 256, ATTRIBUTE_BUFFER, cfg) { |
| 155 | cfg.pointer = (attr->gpu + 1024); |
| 156 | cfg.size = 1024; |
| 157 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 158 | |
Alyssa Rosenzweig | b10f34a | 2020-09-09 16:29:41 -0400 | [diff] [blame] | 159 | pan_pack(ubo->cpu, UNIFORM_BUFFER, cfg) { |
| 160 | cfg.entries = sz_ubo / 16; |
| 161 | cfg.pointer = ubo->gpu + 1024; |
| 162 | } |
| 163 | |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 164 | if (sz_ubo) |
| 165 | memcpy(ubo->cpu + 1024, iubo, sz_ubo); |
| 166 | |
| 167 | if (sz_attr) |
| 168 | memcpy(attr->cpu + 1024, iattr, sz_attr); |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 169 | |
| 170 | struct panfrost_bo *shmem = bit_bo_create(dev, 4096); |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 171 | |
Boris Brezillon | 3a06fc3 | 2020-09-03 09:18:09 +0200 | [diff] [blame] | 172 | pan_pack(shmem->cpu, LOCAL_STORAGE, cfg) { |
| 173 | cfg.wls_instances = MALI_LOCAL_STORAGE_NO_WORKGROUP_MEM; |
| 174 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 175 | |
Boris Brezillon | f734e67 | 2020-09-29 15:47:04 +0200 | [diff] [blame] | 176 | pan_pack(shader_desc->cpu, RENDERER_STATE, cfg) { |
Alyssa Rosenzweig | a7b2317 | 2020-08-21 16:27:17 -0400 | [diff] [blame] | 177 | cfg.shader.shader = shader->gpu; |
| 178 | cfg.shader.attribute_count = cfg.shader.varying_count = 1; |
Boris Brezillon | 7bb85ea | 2020-09-15 17:03:28 +0200 | [diff] [blame] | 179 | cfg.properties.uniform_buffer_count = 1; |
Boris Brezillon | 519643b | 2020-10-13 18:32:14 +0200 | [diff] [blame^] | 180 | cfg.properties.bifrost.zs_update_operation = MALI_PIXEL_KILL_STRONG_EARLY; |
Boris Brezillon | 7bb85ea | 2020-09-15 17:03:28 +0200 | [diff] [blame] | 181 | cfg.preload.vertex_id = true; |
| 182 | cfg.preload.instance_id = true; |
Alyssa Rosenzweig | a7b2317 | 2020-08-21 16:27:17 -0400 | [diff] [blame] | 183 | cfg.preload.uniform_count = (sz_ubo / 16); |
| 184 | } |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 185 | |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 186 | memcpy(shader->cpu, prog.compiled.data, prog.compiled.size); |
| 187 | |
Boris Brezillon | d289209 | 2020-09-08 19:41:51 +0200 | [diff] [blame] | 188 | struct mali_compute_job_packed job; |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 189 | |
Boris Brezillon | d289209 | 2020-09-08 19:41:51 +0200 | [diff] [blame] | 190 | pan_section_pack(&job, COMPUTE_JOB, PARAMETERS, cfg) { |
| 191 | cfg.job_task_split = 5; |
| 192 | } |
Alyssa Rosenzweig | 02e768e | 2020-08-26 13:04:17 -0400 | [diff] [blame] | 193 | |
Boris Brezillon | d289209 | 2020-09-08 19:41:51 +0200 | [diff] [blame] | 194 | pan_section_pack(&job, COMPUTE_JOB, DRAW, cfg) { |
Boris Brezillon | d343f23 | 2020-09-29 10:45:23 +0200 | [diff] [blame] | 195 | cfg.draw_descriptor_is_64b = true; |
| 196 | cfg.thread_storage = shmem->gpu; |
Alyssa Rosenzweig | eb261a8 | 2020-08-26 17:10:37 -0400 | [diff] [blame] | 197 | cfg.state = shader_desc->gpu; |
| 198 | cfg.push_uniforms = ubo->gpu + 1024; |
| 199 | cfg.uniform_buffers = ubo->gpu; |
| 200 | cfg.attributes = attr->gpu; |
| 201 | cfg.attribute_buffers = attr->gpu + 256; |
| 202 | cfg.varyings = var->gpu; |
| 203 | cfg.varying_buffers = var->gpu + 256; |
| 204 | } |
| 205 | |
Boris Brezillon | d289209 | 2020-09-08 19:41:51 +0200 | [diff] [blame] | 206 | void *invocation = pan_section_ptr(&job, COMPUTE_JOB, INVOCATION); |
| 207 | panfrost_pack_work_groups_compute(invocation, |
| 208 | 1, 1, 1, |
| 209 | 1, 1, 1, |
| 210 | true); |
Alyssa Rosenzweig | 02e768e | 2020-08-26 13:04:17 -0400 | [diff] [blame] | 211 | |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 212 | struct panfrost_bo *bos[] = { |
Alyssa Rosenzweig | b10f34a | 2020-09-09 16:29:41 -0400 | [diff] [blame] | 213 | shmem, shader, shader_desc, ubo, var, attr |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 214 | }; |
| 215 | |
Boris Brezillon | d289209 | 2020-09-08 19:41:51 +0200 | [diff] [blame] | 216 | bool succ = bit_submit(dev, MALI_JOB_TYPE_VERTEX, |
| 217 | ((void *)&job) + MALI_JOB_HEADER_LENGTH, |
| 218 | MALI_COMPUTE_JOB_LENGTH - MALI_JOB_HEADER_LENGTH, |
| 219 | bos, ARRAY_SIZE(bos), debug); |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 220 | |
| 221 | /* Check the output varyings */ |
| 222 | |
Alyssa Rosenzweig | 1a1c557 | 2020-04-05 18:45:00 -0400 | [diff] [blame] | 223 | uint32_t *output = (uint32_t *) (var->cpu + 1024); |
| 224 | float *foutput = (float *) output; |
| 225 | float *fexpected = (float *) expected; |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 226 | |
Alyssa Rosenzweig | 1a1c557 | 2020-04-05 18:45:00 -0400 | [diff] [blame] | 227 | if (sz_expected) { |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 228 | unsigned comp = memcmp(output, expected, sz_expected); |
| 229 | succ &= (comp == 0); |
| 230 | |
| 231 | if (comp && (debug >= BIT_DEBUG_FAIL)) { |
| 232 | fprintf(stderr, "expected ["); |
| 233 | |
| 234 | for (unsigned i = 0; i < (sz_expected >> 2); ++i) |
| 235 | fprintf(stderr, "%08X /* %f */ ", expected[i], fexpected[i]); |
| 236 | |
| 237 | fprintf(stderr, "], got ["); |
| 238 | |
| 239 | for (unsigned i = 0; i < (sz_expected >> 2); ++i) |
| 240 | fprintf(stderr, "%08X /* %f */ ", output[i], foutput[i]); |
| 241 | |
| 242 | fprintf(stderr, "\n"); |
| 243 | } |
Alyssa Rosenzweig | 1a1c557 | 2020-04-05 18:45:00 -0400 | [diff] [blame] | 244 | } else if (debug == BIT_DEBUG_ALL) { |
| 245 | fprintf(stderr, "got ["); |
| 246 | |
| 247 | for (unsigned i = 0; i < 4; ++i) |
| 248 | fprintf(stderr, "%08X /* %f */ ", output[i], foutput[i]); |
| 249 | |
| 250 | fprintf(stderr, "\n"); |
Alyssa Rosenzweig | b033189 | 2020-03-31 12:20:18 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return succ; |
Alyssa Rosenzweig | 3d7166f | 2020-03-25 14:56:06 -0400 | [diff] [blame] | 254 | } |