1 /* 2 * Copyright © 2014 Broadcom 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 24 /** 25 * DOC: Command list validator for VC4. 26 * 27 * Since the VC4 has no IOMMU between it and system memory, a user 28 * with access to execute command lists could escalate privilege by 29 * overwriting system memory (drawing to it as a framebuffer) or 30 * reading system memory it shouldn't (reading it as a vertex buffer 31 * or index buffer) 32 * 33 * We validate binner command lists to ensure that all accesses are 34 * within the bounds of the GEM objects referenced by the submitted 35 * job. It explicitly whitelists packets, and looks at the offsets in 36 * any address fields to make sure they're contained within the BOs 37 * they reference. 38 * 39 * Note that because CL validation is already reading the 40 * user-submitted CL and writing the validated copy out to the memory 41 * that the GPU will actually read, this is also where GEM relocation 42 * processing (turning BO references into actual addresses for the GPU 43 * to use) happens. 44 */ 45 46 #include <drm/drm_print.h> 47 48 #include "vc4_drv.h" 49 #include "vc4_packet.h" 50 51 #define VALIDATE_ARGS \ 52 struct vc4_exec_info *exec, \ 53 void *validated, \ 54 void *untrusted 55 56 /** Return the width in pixels of a 64-byte microtile. */ 57 static uint32_t 58 utile_width(int cpp) 59 { 60 switch (cpp) { 61 case 1: 62 case 2: 63 return 8; 64 case 4: 65 return 4; 66 case 8: 67 return 2; 68 default: 69 pr_err("unknown cpp: %d\n", cpp); 70 return 1; 71 } 72 } 73 74 /** Return the height in pixels of a 64-byte microtile. */ 75 static uint32_t 76 utile_height(int cpp) 77 { 78 switch (cpp) { 79 case 1: 80 return 8; 81 case 2: 82 case 4: 83 case 8: 84 return 4; 85 default: 86 pr_err("unknown cpp: %d\n", cpp); 87 return 1; 88 } 89 } 90 91 /** 92 * size_is_lt() - Returns whether a miplevel of the given size will 93 * use the lineartile (LT) tiling layout rather than the normal T 94 * tiling layout. 95 * @width: Width in pixels of the miplevel 96 * @height: Height in pixels of the miplevel 97 * @cpp: Bytes per pixel of the pixel format 98 */ 99 static bool 100 size_is_lt(uint32_t width, uint32_t height, int cpp) 101 { 102 return (width <= 4 * utile_width(cpp) || 103 height <= 4 * utile_height(cpp)); 104 } 105 106 struct drm_gem_dma_object * 107 vc4_use_bo(struct vc4_exec_info *exec, uint32_t hindex) 108 { 109 struct vc4_dev *vc4 = exec->dev; 110 struct drm_gem_dma_object *obj; 111 struct vc4_bo *bo; 112 113 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 114 return NULL; 115 116 if (hindex >= exec->bo_count) { 117 DRM_DEBUG("BO index %d greater than BO count %d\n", 118 hindex, exec->bo_count); 119 return NULL; 120 } 121 obj = to_drm_gem_dma_obj(exec->bo[hindex]); 122 bo = to_vc4_bo(&obj->base); 123 124 if (bo->validated_shader) { 125 DRM_DEBUG("Trying to use shader BO as something other than " 126 "a shader\n"); 127 return NULL; 128 } 129 130 return obj; 131 } 132 133 static struct drm_gem_dma_object * 134 vc4_use_handle(struct vc4_exec_info *exec, uint32_t gem_handles_packet_index) 135 { 136 return vc4_use_bo(exec, exec->bo_index[gem_handles_packet_index]); 137 } 138 139 static bool 140 validate_bin_pos(struct vc4_exec_info *exec, void *untrusted, uint32_t pos) 141 { 142 /* Note that the untrusted pointer passed to these functions is 143 * incremented past the packet byte. 144 */ 145 return (untrusted - 1 == exec->bin_u + pos); 146 } 147 148 static uint32_t 149 gl_shader_rec_size(uint32_t pointer_bits) 150 { 151 uint32_t attribute_count = pointer_bits & 7; 152 bool extended = pointer_bits & 8; 153 154 if (attribute_count == 0) 155 attribute_count = 8; 156 157 if (extended) 158 return 100 + attribute_count * 4; 159 else 160 return 36 + attribute_count * 8; 161 } 162 163 bool 164 vc4_check_tex_size(struct vc4_exec_info *exec, struct drm_gem_dma_object *fbo, 165 uint32_t offset, uint8_t tiling_format, 166 uint32_t width, uint32_t height, uint8_t cpp) 167 { 168 struct vc4_dev *vc4 = exec->dev; 169 uint32_t aligned_width, aligned_height, stride, size; 170 uint32_t utile_w = utile_width(cpp); 171 uint32_t utile_h = utile_height(cpp); 172 173 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 174 return false; 175 176 /* The shaded vertex format stores signed 12.4 fixed point 177 * (-2048,2047) offsets from the viewport center, so we should 178 * never have a render target larger than 4096. The texture 179 * unit can only sample from 2048x2048, so it's even more 180 * restricted. This lets us avoid worrying about overflow in 181 * our math. 182 */ 183 if (width > 4096 || height > 4096) { 184 DRM_DEBUG("Surface dimensions (%d,%d) too large", 185 width, height); 186 return false; 187 } 188 189 switch (tiling_format) { 190 case VC4_TILING_FORMAT_LINEAR: 191 aligned_width = round_up(width, utile_w); 192 aligned_height = height; 193 break; 194 case VC4_TILING_FORMAT_T: 195 aligned_width = round_up(width, utile_w * 8); 196 aligned_height = round_up(height, utile_h * 8); 197 break; 198 case VC4_TILING_FORMAT_LT: 199 aligned_width = round_up(width, utile_w); 200 aligned_height = round_up(height, utile_h); 201 break; 202 default: 203 DRM_DEBUG("buffer tiling %d unsupported\n", tiling_format); 204 return false; 205 } 206 207 stride = aligned_width * cpp; 208 size = stride * aligned_height; 209 210 if (size + offset < size || 211 size + offset > fbo->base.size) { 212 DRM_DEBUG("Overflow in %dx%d (%dx%d) fbo size (%d + %d > %zd)\n", 213 width, height, 214 aligned_width, aligned_height, 215 size, offset, fbo->base.size); 216 return false; 217 } 218 219 return true; 220 } 221 222 static int 223 validate_flush(VALIDATE_ARGS) 224 { 225 if (!validate_bin_pos(exec, untrusted, exec->args->bin_cl_size - 1)) { 226 DRM_DEBUG("Bin CL must end with VC4_PACKET_FLUSH\n"); 227 return -EINVAL; 228 } 229 exec->found_flush = true; 230 231 return 0; 232 } 233 234 static int 235 validate_start_tile_binning(VALIDATE_ARGS) 236 { 237 if (exec->found_start_tile_binning_packet) { 238 DRM_DEBUG("Duplicate VC4_PACKET_START_TILE_BINNING\n"); 239 return -EINVAL; 240 } 241 exec->found_start_tile_binning_packet = true; 242 243 if (!exec->found_tile_binning_mode_config_packet) { 244 DRM_DEBUG("missing VC4_PACKET_TILE_BINNING_MODE_CONFIG\n"); 245 return -EINVAL; 246 } 247 248 return 0; 249 } 250 251 static int 252 validate_increment_semaphore(VALIDATE_ARGS) 253 { 254 if (!validate_bin_pos(exec, untrusted, exec->args->bin_cl_size - 2)) { 255 DRM_DEBUG("Bin CL must end with " 256 "VC4_PACKET_INCREMENT_SEMAPHORE\n"); 257 return -EINVAL; 258 } 259 exec->found_increment_semaphore_packet = true; 260 261 return 0; 262 } 263 264 static int 265 validate_indexed_prim_list(VALIDATE_ARGS) 266 { 267 struct drm_gem_dma_object *ib; 268 uint32_t length = *(uint32_t *)(untrusted + 1); 269 uint32_t offset = *(uint32_t *)(untrusted + 5); 270 uint32_t max_index = *(uint32_t *)(untrusted + 9); 271 uint32_t index_size = (*(uint8_t *)(untrusted + 0) >> 4) ? 2 : 1; 272 struct vc4_shader_state *shader_state; 273 274 /* Check overflow condition */ 275 if (exec->shader_state_count == 0) { 276 DRM_DEBUG("shader state must precede primitives\n"); 277 return -EINVAL; 278 } 279 shader_state = &exec->shader_state[exec->shader_state_count - 1]; 280 281 if (max_index > shader_state->max_index) 282 shader_state->max_index = max_index; 283 284 ib = vc4_use_handle(exec, 0); 285 if (!ib) 286 return -EINVAL; 287 288 if (offset > ib->base.size || 289 (ib->base.size - offset) / index_size < length) { 290 DRM_DEBUG("IB access overflow (%d + %d*%d > %zd)\n", 291 offset, length, index_size, ib->base.size); 292 return -EINVAL; 293 } 294 295 *(uint32_t *)(validated + 5) = ib->dma_addr + offset; 296 297 return 0; 298 } 299 300 static int 301 validate_gl_array_primitive(VALIDATE_ARGS) 302 { 303 uint32_t length = *(uint32_t *)(untrusted + 1); 304 uint32_t base_index = *(uint32_t *)(untrusted + 5); 305 uint32_t max_index; 306 struct vc4_shader_state *shader_state; 307 308 /* Check overflow condition */ 309 if (exec->shader_state_count == 0) { 310 DRM_DEBUG("shader state must precede primitives\n"); 311 return -EINVAL; 312 } 313 shader_state = &exec->shader_state[exec->shader_state_count - 1]; 314 315 if (length + base_index < length) { 316 DRM_DEBUG("primitive vertex count overflow\n"); 317 return -EINVAL; 318 } 319 max_index = length + base_index - 1; 320 321 if (max_index > shader_state->max_index) 322 shader_state->max_index = max_index; 323 324 return 0; 325 } 326 327 static int 328 validate_gl_shader_state(VALIDATE_ARGS) 329 { 330 uint32_t i = exec->shader_state_count++; 331 332 if (i >= exec->shader_state_size) { 333 DRM_DEBUG("More requests for shader states than declared\n"); 334 return -EINVAL; 335 } 336 337 exec->shader_state[i].addr = *(uint32_t *)untrusted; 338 exec->shader_state[i].max_index = 0; 339 340 if (exec->shader_state[i].addr & ~0xf) { 341 DRM_DEBUG("high bits set in GL shader rec reference\n"); 342 return -EINVAL; 343 } 344 345 *(uint32_t *)validated = (exec->shader_rec_p + 346 exec->shader_state[i].addr); 347 348 exec->shader_rec_p += 349 roundup(gl_shader_rec_size(exec->shader_state[i].addr), 16); 350 351 return 0; 352 } 353 354 static int 355 validate_tile_binning_config(VALIDATE_ARGS) 356 { 357 struct drm_device *dev = exec->exec_bo->base.dev; 358 struct vc4_dev *vc4 = to_vc4_dev(dev); 359 uint8_t flags; 360 uint32_t tile_state_size; 361 uint32_t tile_count, bin_addr; 362 int bin_slot; 363 364 if (exec->found_tile_binning_mode_config_packet) { 365 DRM_DEBUG("Duplicate VC4_PACKET_TILE_BINNING_MODE_CONFIG\n"); 366 return -EINVAL; 367 } 368 exec->found_tile_binning_mode_config_packet = true; 369 370 exec->bin_tiles_x = *(uint8_t *)(untrusted + 12); 371 exec->bin_tiles_y = *(uint8_t *)(untrusted + 13); 372 tile_count = exec->bin_tiles_x * exec->bin_tiles_y; 373 flags = *(uint8_t *)(untrusted + 14); 374 375 if (exec->bin_tiles_x == 0 || 376 exec->bin_tiles_y == 0) { 377 DRM_DEBUG("Tile binning config of %dx%d too small\n", 378 exec->bin_tiles_x, exec->bin_tiles_y); 379 return -EINVAL; 380 } 381 382 if (flags & (VC4_BIN_CONFIG_DB_NON_MS | 383 VC4_BIN_CONFIG_TILE_BUFFER_64BIT)) { 384 DRM_DEBUG("unsupported binning config flags 0x%02x\n", flags); 385 return -EINVAL; 386 } 387 388 bin_slot = vc4_v3d_get_bin_slot(vc4); 389 if (bin_slot < 0) { 390 if (bin_slot != -EINTR && bin_slot != -ERESTARTSYS) { 391 drm_err(dev, "Failed to allocate binner memory: %d\n", 392 bin_slot); 393 } 394 return bin_slot; 395 } 396 397 /* The slot we allocated will only be used by this job, and is 398 * free when the job completes rendering. 399 */ 400 exec->bin_slots |= BIT(bin_slot); 401 bin_addr = vc4->bin_bo->base.dma_addr + bin_slot * vc4->bin_alloc_size; 402 403 /* The tile state data array is 48 bytes per tile, and we put it at 404 * the start of a BO containing both it and the tile alloc. 405 */ 406 tile_state_size = 48 * tile_count; 407 408 /* Since the tile alloc array will follow us, align. */ 409 exec->tile_alloc_offset = bin_addr + roundup(tile_state_size, 4096); 410 411 *(uint8_t *)(validated + 14) = 412 ((flags & ~(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_MASK | 413 VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_MASK)) | 414 VC4_BIN_CONFIG_AUTO_INIT_TSDA | 415 VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_32, 416 VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE) | 417 VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_128, 418 VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE)); 419 420 /* tile alloc address. */ 421 *(uint32_t *)(validated + 0) = exec->tile_alloc_offset; 422 /* tile alloc size. */ 423 *(uint32_t *)(validated + 4) = (bin_addr + vc4->bin_alloc_size - 424 exec->tile_alloc_offset); 425 /* tile state address. */ 426 *(uint32_t *)(validated + 8) = bin_addr; 427 428 return 0; 429 } 430 431 static int 432 validate_gem_handles(VALIDATE_ARGS) 433 { 434 memcpy(exec->bo_index, untrusted, sizeof(exec->bo_index)); 435 return 0; 436 } 437 438 #define VC4_DEFINE_PACKET(packet, func) \ 439 [packet] = { packet ## _SIZE, #packet, func } 440 441 static const struct cmd_info { 442 uint16_t len; 443 const char *name; 444 int (*func)(struct vc4_exec_info *exec, void *validated, 445 void *untrusted); 446 } cmd_info[] = { 447 VC4_DEFINE_PACKET(VC4_PACKET_HALT, NULL), 448 VC4_DEFINE_PACKET(VC4_PACKET_NOP, NULL), 449 VC4_DEFINE_PACKET(VC4_PACKET_FLUSH, validate_flush), 450 VC4_DEFINE_PACKET(VC4_PACKET_FLUSH_ALL, NULL), 451 VC4_DEFINE_PACKET(VC4_PACKET_START_TILE_BINNING, 452 validate_start_tile_binning), 453 VC4_DEFINE_PACKET(VC4_PACKET_INCREMENT_SEMAPHORE, 454 validate_increment_semaphore), 455 456 VC4_DEFINE_PACKET(VC4_PACKET_GL_INDEXED_PRIMITIVE, 457 validate_indexed_prim_list), 458 VC4_DEFINE_PACKET(VC4_PACKET_GL_ARRAY_PRIMITIVE, 459 validate_gl_array_primitive), 460 461 VC4_DEFINE_PACKET(VC4_PACKET_PRIMITIVE_LIST_FORMAT, NULL), 462 463 VC4_DEFINE_PACKET(VC4_PACKET_GL_SHADER_STATE, validate_gl_shader_state), 464 465 VC4_DEFINE_PACKET(VC4_PACKET_CONFIGURATION_BITS, NULL), 466 VC4_DEFINE_PACKET(VC4_PACKET_FLAT_SHADE_FLAGS, NULL), 467 VC4_DEFINE_PACKET(VC4_PACKET_POINT_SIZE, NULL), 468 VC4_DEFINE_PACKET(VC4_PACKET_LINE_WIDTH, NULL), 469 VC4_DEFINE_PACKET(VC4_PACKET_RHT_X_BOUNDARY, NULL), 470 VC4_DEFINE_PACKET(VC4_PACKET_DEPTH_OFFSET, NULL), 471 VC4_DEFINE_PACKET(VC4_PACKET_CLIP_WINDOW, NULL), 472 VC4_DEFINE_PACKET(VC4_PACKET_VIEWPORT_OFFSET, NULL), 473 VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_XY_SCALING, NULL), 474 /* Note: The docs say this was also 105, but it was 106 in the 475 * initial userland code drop. 476 */ 477 VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_Z_SCALING, NULL), 478 479 VC4_DEFINE_PACKET(VC4_PACKET_TILE_BINNING_MODE_CONFIG, 480 validate_tile_binning_config), 481 482 VC4_DEFINE_PACKET(VC4_PACKET_GEM_HANDLES, validate_gem_handles), 483 }; 484 485 int 486 vc4_validate_bin_cl(struct drm_device *dev, 487 void *validated, 488 void *unvalidated, 489 struct vc4_exec_info *exec) 490 { 491 struct vc4_dev *vc4 = to_vc4_dev(dev); 492 uint32_t len = exec->args->bin_cl_size; 493 uint32_t dst_offset = 0; 494 uint32_t src_offset = 0; 495 496 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 497 return -ENODEV; 498 499 while (src_offset < len) { 500 void *dst_pkt = validated + dst_offset; 501 void *src_pkt = unvalidated + src_offset; 502 u8 cmd = *(uint8_t *)src_pkt; 503 const struct cmd_info *info; 504 505 if (cmd >= ARRAY_SIZE(cmd_info)) { 506 DRM_DEBUG("0x%08x: packet %d out of bounds\n", 507 src_offset, cmd); 508 return -EINVAL; 509 } 510 511 info = &cmd_info[cmd]; 512 if (!info->name) { 513 DRM_DEBUG("0x%08x: packet %d invalid\n", 514 src_offset, cmd); 515 return -EINVAL; 516 } 517 518 if (src_offset + info->len > len) { 519 DRM_DEBUG("0x%08x: packet %d (%s) length 0x%08x " 520 "exceeds bounds (0x%08x)\n", 521 src_offset, cmd, info->name, info->len, 522 src_offset + len); 523 return -EINVAL; 524 } 525 526 if (cmd != VC4_PACKET_GEM_HANDLES) 527 memcpy(dst_pkt, src_pkt, info->len); 528 529 if (info->func && info->func(exec, 530 dst_pkt + 1, 531 src_pkt + 1)) { 532 DRM_DEBUG("0x%08x: packet %d (%s) failed to validate\n", 533 src_offset, cmd, info->name); 534 return -EINVAL; 535 } 536 537 src_offset += info->len; 538 /* GEM handle loading doesn't produce HW packets. */ 539 if (cmd != VC4_PACKET_GEM_HANDLES) 540 dst_offset += info->len; 541 542 /* When the CL hits halt, it'll stop reading anything else. */ 543 if (cmd == VC4_PACKET_HALT) 544 break; 545 } 546 547 exec->ct0ea = exec->ct0ca + dst_offset; 548 549 if (!exec->found_start_tile_binning_packet) { 550 DRM_DEBUG("Bin CL missing VC4_PACKET_START_TILE_BINNING\n"); 551 return -EINVAL; 552 } 553 554 /* The bin CL must be ended with INCREMENT_SEMAPHORE and FLUSH. The 555 * semaphore is used to trigger the render CL to start up, and the 556 * FLUSH is what caps the bin lists with 557 * VC4_PACKET_RETURN_FROM_SUB_LIST (so they jump back to the main 558 * render CL when they get called to) and actually triggers the queued 559 * semaphore increment. 560 */ 561 if (!exec->found_increment_semaphore_packet || !exec->found_flush) { 562 DRM_DEBUG("Bin CL missing VC4_PACKET_INCREMENT_SEMAPHORE + " 563 "VC4_PACKET_FLUSH\n"); 564 return -EINVAL; 565 } 566 567 return 0; 568 } 569 570 static bool 571 reloc_tex(struct vc4_exec_info *exec, 572 void *uniform_data_u, 573 struct vc4_texture_sample_info *sample, 574 uint32_t texture_handle_index, bool is_cs) 575 { 576 struct drm_gem_dma_object *tex; 577 uint32_t p0 = *(uint32_t *)(uniform_data_u + sample->p_offset[0]); 578 uint32_t p1 = *(uint32_t *)(uniform_data_u + sample->p_offset[1]); 579 uint32_t p2 = (sample->p_offset[2] != ~0 ? 580 *(uint32_t *)(uniform_data_u + sample->p_offset[2]) : 0); 581 uint32_t p3 = (sample->p_offset[3] != ~0 ? 582 *(uint32_t *)(uniform_data_u + sample->p_offset[3]) : 0); 583 uint32_t *validated_p0 = exec->uniforms_v + sample->p_offset[0]; 584 uint32_t offset = p0 & VC4_TEX_P0_OFFSET_MASK; 585 uint32_t miplevels = VC4_GET_FIELD(p0, VC4_TEX_P0_MIPLVLS); 586 uint32_t width = VC4_GET_FIELD(p1, VC4_TEX_P1_WIDTH); 587 uint32_t height = VC4_GET_FIELD(p1, VC4_TEX_P1_HEIGHT); 588 uint32_t cpp, tiling_format, utile_w, utile_h; 589 uint32_t i; 590 uint32_t cube_map_stride = 0; 591 enum vc4_texture_data_type type; 592 593 tex = vc4_use_bo(exec, texture_handle_index); 594 if (!tex) 595 return false; 596 597 if (sample->is_direct) { 598 uint32_t remaining_size = tex->base.size - p0; 599 600 if (p0 > tex->base.size - 4) { 601 DRM_DEBUG("UBO offset greater than UBO size\n"); 602 goto fail; 603 } 604 if (p1 > remaining_size - 4) { 605 DRM_DEBUG("UBO clamp would allow reads " 606 "outside of UBO\n"); 607 goto fail; 608 } 609 *validated_p0 = tex->dma_addr + p0; 610 return true; 611 } 612 613 if (width == 0) 614 width = 2048; 615 if (height == 0) 616 height = 2048; 617 618 if (p0 & VC4_TEX_P0_CMMODE_MASK) { 619 if (VC4_GET_FIELD(p2, VC4_TEX_P2_PTYPE) == 620 VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE) 621 cube_map_stride = p2 & VC4_TEX_P2_CMST_MASK; 622 if (VC4_GET_FIELD(p3, VC4_TEX_P2_PTYPE) == 623 VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE) { 624 if (cube_map_stride) { 625 DRM_DEBUG("Cube map stride set twice\n"); 626 goto fail; 627 } 628 629 cube_map_stride = p3 & VC4_TEX_P2_CMST_MASK; 630 } 631 if (!cube_map_stride) { 632 DRM_DEBUG("Cube map stride not set\n"); 633 goto fail; 634 } 635 } 636 637 type = (VC4_GET_FIELD(p0, VC4_TEX_P0_TYPE) | 638 (VC4_GET_FIELD(p1, VC4_TEX_P1_TYPE4) << 4)); 639 640 switch (type) { 641 case VC4_TEXTURE_TYPE_RGBA8888: 642 case VC4_TEXTURE_TYPE_RGBX8888: 643 case VC4_TEXTURE_TYPE_RGBA32R: 644 cpp = 4; 645 break; 646 case VC4_TEXTURE_TYPE_RGBA4444: 647 case VC4_TEXTURE_TYPE_RGBA5551: 648 case VC4_TEXTURE_TYPE_RGB565: 649 case VC4_TEXTURE_TYPE_LUMALPHA: 650 case VC4_TEXTURE_TYPE_S16F: 651 case VC4_TEXTURE_TYPE_S16: 652 cpp = 2; 653 break; 654 case VC4_TEXTURE_TYPE_LUMINANCE: 655 case VC4_TEXTURE_TYPE_ALPHA: 656 case VC4_TEXTURE_TYPE_S8: 657 cpp = 1; 658 break; 659 case VC4_TEXTURE_TYPE_ETC1: 660 /* ETC1 is arranged as 64-bit blocks, where each block is 4x4 661 * pixels. 662 */ 663 cpp = 8; 664 width = (width + 3) >> 2; 665 height = (height + 3) >> 2; 666 break; 667 case VC4_TEXTURE_TYPE_BW1: 668 case VC4_TEXTURE_TYPE_A4: 669 case VC4_TEXTURE_TYPE_A1: 670 case VC4_TEXTURE_TYPE_RGBA64: 671 case VC4_TEXTURE_TYPE_YUV422R: 672 default: 673 DRM_DEBUG("Texture format %d unsupported\n", type); 674 goto fail; 675 } 676 utile_w = utile_width(cpp); 677 utile_h = utile_height(cpp); 678 679 if (type == VC4_TEXTURE_TYPE_RGBA32R) { 680 tiling_format = VC4_TILING_FORMAT_LINEAR; 681 } else { 682 if (size_is_lt(width, height, cpp)) 683 tiling_format = VC4_TILING_FORMAT_LT; 684 else 685 tiling_format = VC4_TILING_FORMAT_T; 686 } 687 688 if (!vc4_check_tex_size(exec, tex, offset + cube_map_stride * 5, 689 tiling_format, width, height, cpp)) { 690 goto fail; 691 } 692 693 /* The mipmap levels are stored before the base of the texture. Make 694 * sure there is actually space in the BO. 695 */ 696 for (i = 1; i <= miplevels; i++) { 697 uint32_t level_width = max(width >> i, 1u); 698 uint32_t level_height = max(height >> i, 1u); 699 uint32_t aligned_width, aligned_height; 700 uint32_t level_size; 701 702 /* Once the levels get small enough, they drop from T to LT. */ 703 if (tiling_format == VC4_TILING_FORMAT_T && 704 size_is_lt(level_width, level_height, cpp)) { 705 tiling_format = VC4_TILING_FORMAT_LT; 706 } 707 708 switch (tiling_format) { 709 case VC4_TILING_FORMAT_T: 710 aligned_width = round_up(level_width, utile_w * 8); 711 aligned_height = round_up(level_height, utile_h * 8); 712 break; 713 case VC4_TILING_FORMAT_LT: 714 aligned_width = round_up(level_width, utile_w); 715 aligned_height = round_up(level_height, utile_h); 716 break; 717 default: 718 aligned_width = round_up(level_width, utile_w); 719 aligned_height = level_height; 720 break; 721 } 722 723 level_size = aligned_width * cpp * aligned_height; 724 725 if (offset < level_size) { 726 DRM_DEBUG("Level %d (%dx%d -> %dx%d) size %db " 727 "overflowed buffer bounds (offset %d)\n", 728 i, level_width, level_height, 729 aligned_width, aligned_height, 730 level_size, offset); 731 goto fail; 732 } 733 734 offset -= level_size; 735 } 736 737 *validated_p0 = tex->dma_addr + p0; 738 739 return true; 740 fail: 741 DRM_INFO("Texture p0 at %d: 0x%08x\n", sample->p_offset[0], p0); 742 DRM_INFO("Texture p1 at %d: 0x%08x\n", sample->p_offset[1], p1); 743 DRM_INFO("Texture p2 at %d: 0x%08x\n", sample->p_offset[2], p2); 744 DRM_INFO("Texture p3 at %d: 0x%08x\n", sample->p_offset[3], p3); 745 return false; 746 } 747 748 static int 749 validate_gl_shader_rec(struct drm_device *dev, 750 struct vc4_exec_info *exec, 751 struct vc4_shader_state *state) 752 { 753 uint32_t *src_handles; 754 void *pkt_u, *pkt_v; 755 static const uint32_t shader_reloc_offsets[] = { 756 4, /* fs */ 757 16, /* vs */ 758 28, /* cs */ 759 }; 760 uint32_t shader_reloc_count = ARRAY_SIZE(shader_reloc_offsets); 761 struct drm_gem_dma_object *bo[ARRAY_SIZE(shader_reloc_offsets) + 8]; 762 uint32_t nr_attributes, nr_relocs, packet_size; 763 int i; 764 765 nr_attributes = state->addr & 0x7; 766 if (nr_attributes == 0) 767 nr_attributes = 8; 768 packet_size = gl_shader_rec_size(state->addr); 769 770 nr_relocs = ARRAY_SIZE(shader_reloc_offsets) + nr_attributes; 771 if (nr_relocs * 4 > exec->shader_rec_size) { 772 DRM_DEBUG("overflowed shader recs reading %d handles " 773 "from %d bytes left\n", 774 nr_relocs, exec->shader_rec_size); 775 return -EINVAL; 776 } 777 src_handles = exec->shader_rec_u; 778 exec->shader_rec_u += nr_relocs * 4; 779 exec->shader_rec_size -= nr_relocs * 4; 780 781 if (packet_size > exec->shader_rec_size) { 782 DRM_DEBUG("overflowed shader recs copying %db packet " 783 "from %d bytes left\n", 784 packet_size, exec->shader_rec_size); 785 return -EINVAL; 786 } 787 pkt_u = exec->shader_rec_u; 788 pkt_v = exec->shader_rec_v; 789 memcpy(pkt_v, pkt_u, packet_size); 790 exec->shader_rec_u += packet_size; 791 /* Shader recs have to be aligned to 16 bytes (due to the attribute 792 * flags being in the low bytes), so round the next validated shader 793 * rec address up. This should be safe, since we've got so many 794 * relocations in a shader rec packet. 795 */ 796 BUG_ON(roundup(packet_size, 16) - packet_size > nr_relocs * 4); 797 exec->shader_rec_v += roundup(packet_size, 16); 798 exec->shader_rec_size -= packet_size; 799 800 for (i = 0; i < shader_reloc_count; i++) { 801 if (src_handles[i] > exec->bo_count) { 802 DRM_DEBUG("Shader handle %d too big\n", src_handles[i]); 803 return -EINVAL; 804 } 805 806 bo[i] = to_drm_gem_dma_obj(exec->bo[src_handles[i]]); 807 if (!bo[i]) 808 return -EINVAL; 809 } 810 for (i = shader_reloc_count; i < nr_relocs; i++) { 811 bo[i] = vc4_use_bo(exec, src_handles[i]); 812 if (!bo[i]) 813 return -EINVAL; 814 } 815 816 if (((*(uint16_t *)pkt_u & VC4_SHADER_FLAG_FS_SINGLE_THREAD) == 0) != 817 to_vc4_bo(&bo[0]->base)->validated_shader->is_threaded) { 818 DRM_DEBUG("Thread mode of CL and FS do not match\n"); 819 return -EINVAL; 820 } 821 822 if (to_vc4_bo(&bo[1]->base)->validated_shader->is_threaded || 823 to_vc4_bo(&bo[2]->base)->validated_shader->is_threaded) { 824 DRM_DEBUG("cs and vs cannot be threaded\n"); 825 return -EINVAL; 826 } 827 828 for (i = 0; i < shader_reloc_count; i++) { 829 struct vc4_validated_shader_info *validated_shader; 830 uint32_t o = shader_reloc_offsets[i]; 831 uint32_t src_offset = *(uint32_t *)(pkt_u + o); 832 uint32_t *texture_handles_u; 833 void *uniform_data_u; 834 uint32_t tex, uni; 835 836 *(uint32_t *)(pkt_v + o) = bo[i]->dma_addr + src_offset; 837 838 if (src_offset != 0) { 839 DRM_DEBUG("Shaders must be at offset 0 of " 840 "the BO.\n"); 841 return -EINVAL; 842 } 843 844 validated_shader = to_vc4_bo(&bo[i]->base)->validated_shader; 845 if (!validated_shader) 846 return -EINVAL; 847 848 if (validated_shader->uniforms_src_size > 849 exec->uniforms_size) { 850 DRM_DEBUG("Uniforms src buffer overflow\n"); 851 return -EINVAL; 852 } 853 854 texture_handles_u = exec->uniforms_u; 855 uniform_data_u = (texture_handles_u + 856 validated_shader->num_texture_samples); 857 858 memcpy(exec->uniforms_v, uniform_data_u, 859 validated_shader->uniforms_size); 860 861 for (tex = 0; 862 tex < validated_shader->num_texture_samples; 863 tex++) { 864 if (!reloc_tex(exec, 865 uniform_data_u, 866 &validated_shader->texture_samples[tex], 867 texture_handles_u[tex], 868 i == 2)) { 869 return -EINVAL; 870 } 871 } 872 873 /* Fill in the uniform slots that need this shader's 874 * start-of-uniforms address (used for resetting the uniform 875 * stream in the presence of control flow). 876 */ 877 for (uni = 0; 878 uni < validated_shader->num_uniform_addr_offsets; 879 uni++) { 880 uint32_t o = validated_shader->uniform_addr_offsets[uni]; 881 ((uint32_t *)exec->uniforms_v)[o] = exec->uniforms_p; 882 } 883 884 *(uint32_t *)(pkt_v + o + 4) = exec->uniforms_p; 885 886 exec->uniforms_u += validated_shader->uniforms_src_size; 887 exec->uniforms_v += validated_shader->uniforms_size; 888 exec->uniforms_p += validated_shader->uniforms_size; 889 } 890 891 for (i = 0; i < nr_attributes; i++) { 892 struct drm_gem_dma_object *vbo = 893 bo[ARRAY_SIZE(shader_reloc_offsets) + i]; 894 uint32_t o = 36 + i * 8; 895 uint32_t offset = *(uint32_t *)(pkt_u + o + 0); 896 uint32_t attr_size = *(uint8_t *)(pkt_u + o + 4) + 1; 897 uint32_t stride = *(uint8_t *)(pkt_u + o + 5); 898 uint32_t max_index; 899 900 if (state->addr & 0x8) 901 stride |= (*(uint32_t *)(pkt_u + 100 + i * 4)) & ~0xff; 902 903 if (vbo->base.size < offset || 904 vbo->base.size - offset < attr_size) { 905 DRM_DEBUG("BO offset overflow (%d + %d > %zu)\n", 906 offset, attr_size, vbo->base.size); 907 return -EINVAL; 908 } 909 910 if (stride != 0) { 911 max_index = ((vbo->base.size - offset - attr_size) / 912 stride); 913 if (state->max_index > max_index) { 914 DRM_DEBUG("primitives use index %d out of " 915 "supplied %d\n", 916 state->max_index, max_index); 917 return -EINVAL; 918 } 919 } 920 921 *(uint32_t *)(pkt_v + o) = vbo->dma_addr + offset; 922 } 923 924 return 0; 925 } 926 927 int 928 vc4_validate_shader_recs(struct drm_device *dev, 929 struct vc4_exec_info *exec) 930 { 931 struct vc4_dev *vc4 = to_vc4_dev(dev); 932 uint32_t i; 933 int ret = 0; 934 935 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) 936 return -ENODEV; 937 938 for (i = 0; i < exec->shader_state_count; i++) { 939 ret = validate_gl_shader_rec(dev, exec, &exec->shader_state[i]); 940 if (ret) 941 return ret; 942 } 943 944 return ret; 945 } 946