vc4_validate.c 24 KB

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