vc4_validate.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. exec->bin_dep_seqno = max(exec->bin_dep_seqno,
  238. to_vc4_bo(&ib->base)->write_seqno);
  239. if (offset > ib->base.size ||
  240. (ib->base.size - offset) / index_size < length) {
  241. DRM_ERROR("IB access overflow (%d + %d*%d > %zd)\n",
  242. offset, length, index_size, ib->base.size);
  243. return -EINVAL;
  244. }
  245. *(uint32_t *)(validated + 5) = ib->paddr + offset;
  246. return 0;
  247. }
  248. static int
  249. validate_gl_array_primitive(VALIDATE_ARGS)
  250. {
  251. uint32_t length = *(uint32_t *)(untrusted + 1);
  252. uint32_t base_index = *(uint32_t *)(untrusted + 5);
  253. uint32_t max_index;
  254. struct vc4_shader_state *shader_state;
  255. /* Check overflow condition */
  256. if (exec->shader_state_count == 0) {
  257. DRM_ERROR("shader state must precede primitives\n");
  258. return -EINVAL;
  259. }
  260. shader_state = &exec->shader_state[exec->shader_state_count - 1];
  261. if (length + base_index < length) {
  262. DRM_ERROR("primitive vertex count overflow\n");
  263. return -EINVAL;
  264. }
  265. max_index = length + base_index - 1;
  266. if (max_index > shader_state->max_index)
  267. shader_state->max_index = max_index;
  268. return 0;
  269. }
  270. static int
  271. validate_gl_shader_state(VALIDATE_ARGS)
  272. {
  273. uint32_t i = exec->shader_state_count++;
  274. if (i >= exec->shader_state_size) {
  275. DRM_ERROR("More requests for shader states than declared\n");
  276. return -EINVAL;
  277. }
  278. exec->shader_state[i].addr = *(uint32_t *)untrusted;
  279. exec->shader_state[i].max_index = 0;
  280. if (exec->shader_state[i].addr & ~0xf) {
  281. DRM_ERROR("high bits set in GL shader rec reference\n");
  282. return -EINVAL;
  283. }
  284. *(uint32_t *)validated = (exec->shader_rec_p +
  285. exec->shader_state[i].addr);
  286. exec->shader_rec_p +=
  287. roundup(gl_shader_rec_size(exec->shader_state[i].addr), 16);
  288. return 0;
  289. }
  290. static int
  291. validate_tile_binning_config(VALIDATE_ARGS)
  292. {
  293. struct drm_device *dev = exec->exec_bo->base.dev;
  294. struct vc4_bo *tile_bo;
  295. uint8_t flags;
  296. uint32_t tile_state_size, tile_alloc_size;
  297. uint32_t tile_count;
  298. if (exec->found_tile_binning_mode_config_packet) {
  299. DRM_ERROR("Duplicate VC4_PACKET_TILE_BINNING_MODE_CONFIG\n");
  300. return -EINVAL;
  301. }
  302. exec->found_tile_binning_mode_config_packet = true;
  303. exec->bin_tiles_x = *(uint8_t *)(untrusted + 12);
  304. exec->bin_tiles_y = *(uint8_t *)(untrusted + 13);
  305. tile_count = exec->bin_tiles_x * exec->bin_tiles_y;
  306. flags = *(uint8_t *)(untrusted + 14);
  307. if (exec->bin_tiles_x == 0 ||
  308. exec->bin_tiles_y == 0) {
  309. DRM_ERROR("Tile binning config of %dx%d too small\n",
  310. exec->bin_tiles_x, exec->bin_tiles_y);
  311. return -EINVAL;
  312. }
  313. if (flags & (VC4_BIN_CONFIG_DB_NON_MS |
  314. VC4_BIN_CONFIG_TILE_BUFFER_64BIT)) {
  315. DRM_ERROR("unsupported binning config flags 0x%02x\n", flags);
  316. return -EINVAL;
  317. }
  318. /* The tile state data array is 48 bytes per tile, and we put it at
  319. * the start of a BO containing both it and the tile alloc.
  320. */
  321. tile_state_size = 48 * tile_count;
  322. /* Since the tile alloc array will follow us, align. */
  323. exec->tile_alloc_offset = roundup(tile_state_size, 4096);
  324. *(uint8_t *)(validated + 14) =
  325. ((flags & ~(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_MASK |
  326. VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_MASK)) |
  327. VC4_BIN_CONFIG_AUTO_INIT_TSDA |
  328. VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE_32,
  329. VC4_BIN_CONFIG_ALLOC_INIT_BLOCK_SIZE) |
  330. VC4_SET_FIELD(VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE_128,
  331. VC4_BIN_CONFIG_ALLOC_BLOCK_SIZE));
  332. /* Initial block size. */
  333. tile_alloc_size = 32 * tile_count;
  334. /*
  335. * The initial allocation gets rounded to the next 256 bytes before
  336. * the hardware starts fulfilling further allocations.
  337. */
  338. tile_alloc_size = roundup(tile_alloc_size, 256);
  339. /* Add space for the extra allocations. This is what gets used first,
  340. * before overflow memory. It must have at least 4096 bytes, but we
  341. * want to avoid overflow memory usage if possible.
  342. */
  343. tile_alloc_size += 1024 * 1024;
  344. tile_bo = vc4_bo_create(dev, exec->tile_alloc_offset + tile_alloc_size,
  345. true);
  346. exec->tile_bo = &tile_bo->base;
  347. if (IS_ERR(exec->tile_bo))
  348. return PTR_ERR(exec->tile_bo);
  349. list_add_tail(&tile_bo->unref_head, &exec->unref_list);
  350. /* tile alloc address. */
  351. *(uint32_t *)(validated + 0) = (exec->tile_bo->paddr +
  352. exec->tile_alloc_offset);
  353. /* tile alloc size. */
  354. *(uint32_t *)(validated + 4) = tile_alloc_size;
  355. /* tile state address. */
  356. *(uint32_t *)(validated + 8) = exec->tile_bo->paddr;
  357. return 0;
  358. }
  359. static int
  360. validate_gem_handles(VALIDATE_ARGS)
  361. {
  362. memcpy(exec->bo_index, untrusted, sizeof(exec->bo_index));
  363. return 0;
  364. }
  365. #define VC4_DEFINE_PACKET(packet, func) \
  366. [packet] = { packet ## _SIZE, #packet, func }
  367. static const struct cmd_info {
  368. uint16_t len;
  369. const char *name;
  370. int (*func)(struct vc4_exec_info *exec, void *validated,
  371. void *untrusted);
  372. } cmd_info[] = {
  373. VC4_DEFINE_PACKET(VC4_PACKET_HALT, NULL),
  374. VC4_DEFINE_PACKET(VC4_PACKET_NOP, NULL),
  375. VC4_DEFINE_PACKET(VC4_PACKET_FLUSH, validate_flush),
  376. VC4_DEFINE_PACKET(VC4_PACKET_FLUSH_ALL, NULL),
  377. VC4_DEFINE_PACKET(VC4_PACKET_START_TILE_BINNING,
  378. validate_start_tile_binning),
  379. VC4_DEFINE_PACKET(VC4_PACKET_INCREMENT_SEMAPHORE,
  380. validate_increment_semaphore),
  381. VC4_DEFINE_PACKET(VC4_PACKET_GL_INDEXED_PRIMITIVE,
  382. validate_indexed_prim_list),
  383. VC4_DEFINE_PACKET(VC4_PACKET_GL_ARRAY_PRIMITIVE,
  384. validate_gl_array_primitive),
  385. VC4_DEFINE_PACKET(VC4_PACKET_PRIMITIVE_LIST_FORMAT, NULL),
  386. VC4_DEFINE_PACKET(VC4_PACKET_GL_SHADER_STATE, validate_gl_shader_state),
  387. VC4_DEFINE_PACKET(VC4_PACKET_CONFIGURATION_BITS, NULL),
  388. VC4_DEFINE_PACKET(VC4_PACKET_FLAT_SHADE_FLAGS, NULL),
  389. VC4_DEFINE_PACKET(VC4_PACKET_POINT_SIZE, NULL),
  390. VC4_DEFINE_PACKET(VC4_PACKET_LINE_WIDTH, NULL),
  391. VC4_DEFINE_PACKET(VC4_PACKET_RHT_X_BOUNDARY, NULL),
  392. VC4_DEFINE_PACKET(VC4_PACKET_DEPTH_OFFSET, NULL),
  393. VC4_DEFINE_PACKET(VC4_PACKET_CLIP_WINDOW, NULL),
  394. VC4_DEFINE_PACKET(VC4_PACKET_VIEWPORT_OFFSET, NULL),
  395. VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_XY_SCALING, NULL),
  396. /* Note: The docs say this was also 105, but it was 106 in the
  397. * initial userland code drop.
  398. */
  399. VC4_DEFINE_PACKET(VC4_PACKET_CLIPPER_Z_SCALING, NULL),
  400. VC4_DEFINE_PACKET(VC4_PACKET_TILE_BINNING_MODE_CONFIG,
  401. validate_tile_binning_config),
  402. VC4_DEFINE_PACKET(VC4_PACKET_GEM_HANDLES, validate_gem_handles),
  403. };
  404. int
  405. vc4_validate_bin_cl(struct drm_device *dev,
  406. void *validated,
  407. void *unvalidated,
  408. struct vc4_exec_info *exec)
  409. {
  410. uint32_t len = exec->args->bin_cl_size;
  411. uint32_t dst_offset = 0;
  412. uint32_t src_offset = 0;
  413. while (src_offset < len) {
  414. void *dst_pkt = validated + dst_offset;
  415. void *src_pkt = unvalidated + src_offset;
  416. u8 cmd = *(uint8_t *)src_pkt;
  417. const struct cmd_info *info;
  418. if (cmd >= ARRAY_SIZE(cmd_info)) {
  419. DRM_ERROR("0x%08x: packet %d out of bounds\n",
  420. src_offset, cmd);
  421. return -EINVAL;
  422. }
  423. info = &cmd_info[cmd];
  424. if (!info->name) {
  425. DRM_ERROR("0x%08x: packet %d invalid\n",
  426. src_offset, cmd);
  427. return -EINVAL;
  428. }
  429. if (src_offset + info->len > len) {
  430. DRM_ERROR("0x%08x: packet %d (%s) length 0x%08x "
  431. "exceeds bounds (0x%08x)\n",
  432. src_offset, cmd, info->name, info->len,
  433. src_offset + len);
  434. return -EINVAL;
  435. }
  436. if (cmd != VC4_PACKET_GEM_HANDLES)
  437. memcpy(dst_pkt, src_pkt, info->len);
  438. if (info->func && info->func(exec,
  439. dst_pkt + 1,
  440. src_pkt + 1)) {
  441. DRM_ERROR("0x%08x: packet %d (%s) failed to validate\n",
  442. src_offset, cmd, info->name);
  443. return -EINVAL;
  444. }
  445. src_offset += info->len;
  446. /* GEM handle loading doesn't produce HW packets. */
  447. if (cmd != VC4_PACKET_GEM_HANDLES)
  448. dst_offset += info->len;
  449. /* When the CL hits halt, it'll stop reading anything else. */
  450. if (cmd == VC4_PACKET_HALT)
  451. break;
  452. }
  453. exec->ct0ea = exec->ct0ca + dst_offset;
  454. if (!exec->found_start_tile_binning_packet) {
  455. DRM_ERROR("Bin CL missing VC4_PACKET_START_TILE_BINNING\n");
  456. return -EINVAL;
  457. }
  458. /* The bin CL must be ended with INCREMENT_SEMAPHORE and FLUSH. The
  459. * semaphore is used to trigger the render CL to start up, and the
  460. * FLUSH is what caps the bin lists with
  461. * VC4_PACKET_RETURN_FROM_SUB_LIST (so they jump back to the main
  462. * render CL when they get called to) and actually triggers the queued
  463. * semaphore increment.
  464. */
  465. if (!exec->found_increment_semaphore_packet || !exec->found_flush) {
  466. DRM_ERROR("Bin CL missing VC4_PACKET_INCREMENT_SEMAPHORE + "
  467. "VC4_PACKET_FLUSH\n");
  468. return -EINVAL;
  469. }
  470. return 0;
  471. }
  472. static bool
  473. reloc_tex(struct vc4_exec_info *exec,
  474. void *uniform_data_u,
  475. struct vc4_texture_sample_info *sample,
  476. uint32_t texture_handle_index, bool is_cs)
  477. {
  478. struct drm_gem_cma_object *tex;
  479. uint32_t p0 = *(uint32_t *)(uniform_data_u + sample->p_offset[0]);
  480. uint32_t p1 = *(uint32_t *)(uniform_data_u + sample->p_offset[1]);
  481. uint32_t p2 = (sample->p_offset[2] != ~0 ?
  482. *(uint32_t *)(uniform_data_u + sample->p_offset[2]) : 0);
  483. uint32_t p3 = (sample->p_offset[3] != ~0 ?
  484. *(uint32_t *)(uniform_data_u + sample->p_offset[3]) : 0);
  485. uint32_t *validated_p0 = exec->uniforms_v + sample->p_offset[0];
  486. uint32_t offset = p0 & VC4_TEX_P0_OFFSET_MASK;
  487. uint32_t miplevels = VC4_GET_FIELD(p0, VC4_TEX_P0_MIPLVLS);
  488. uint32_t width = VC4_GET_FIELD(p1, VC4_TEX_P1_WIDTH);
  489. uint32_t height = VC4_GET_FIELD(p1, VC4_TEX_P1_HEIGHT);
  490. uint32_t cpp, tiling_format, utile_w, utile_h;
  491. uint32_t i;
  492. uint32_t cube_map_stride = 0;
  493. enum vc4_texture_data_type type;
  494. tex = vc4_use_bo(exec, texture_handle_index);
  495. if (!tex)
  496. return false;
  497. if (sample->is_direct) {
  498. uint32_t remaining_size = tex->base.size - p0;
  499. if (p0 > tex->base.size - 4) {
  500. DRM_ERROR("UBO offset greater than UBO size\n");
  501. goto fail;
  502. }
  503. if (p1 > remaining_size - 4) {
  504. DRM_ERROR("UBO clamp would allow reads "
  505. "outside of UBO\n");
  506. goto fail;
  507. }
  508. *validated_p0 = tex->paddr + p0;
  509. return true;
  510. }
  511. if (width == 0)
  512. width = 2048;
  513. if (height == 0)
  514. height = 2048;
  515. if (p0 & VC4_TEX_P0_CMMODE_MASK) {
  516. if (VC4_GET_FIELD(p2, VC4_TEX_P2_PTYPE) ==
  517. VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE)
  518. cube_map_stride = p2 & VC4_TEX_P2_CMST_MASK;
  519. if (VC4_GET_FIELD(p3, VC4_TEX_P2_PTYPE) ==
  520. VC4_TEX_P2_PTYPE_CUBE_MAP_STRIDE) {
  521. if (cube_map_stride) {
  522. DRM_ERROR("Cube map stride set twice\n");
  523. goto fail;
  524. }
  525. cube_map_stride = p3 & VC4_TEX_P2_CMST_MASK;
  526. }
  527. if (!cube_map_stride) {
  528. DRM_ERROR("Cube map stride not set\n");
  529. goto fail;
  530. }
  531. }
  532. type = (VC4_GET_FIELD(p0, VC4_TEX_P0_TYPE) |
  533. (VC4_GET_FIELD(p1, VC4_TEX_P1_TYPE4) << 4));
  534. switch (type) {
  535. case VC4_TEXTURE_TYPE_RGBA8888:
  536. case VC4_TEXTURE_TYPE_RGBX8888:
  537. case VC4_TEXTURE_TYPE_RGBA32R:
  538. cpp = 4;
  539. break;
  540. case VC4_TEXTURE_TYPE_RGBA4444:
  541. case VC4_TEXTURE_TYPE_RGBA5551:
  542. case VC4_TEXTURE_TYPE_RGB565:
  543. case VC4_TEXTURE_TYPE_LUMALPHA:
  544. case VC4_TEXTURE_TYPE_S16F:
  545. case VC4_TEXTURE_TYPE_S16:
  546. cpp = 2;
  547. break;
  548. case VC4_TEXTURE_TYPE_LUMINANCE:
  549. case VC4_TEXTURE_TYPE_ALPHA:
  550. case VC4_TEXTURE_TYPE_S8:
  551. cpp = 1;
  552. break;
  553. case VC4_TEXTURE_TYPE_ETC1:
  554. /* ETC1 is arranged as 64-bit blocks, where each block is 4x4
  555. * pixels.
  556. */
  557. cpp = 8;
  558. width = (width + 3) >> 2;
  559. height = (height + 3) >> 2;
  560. break;
  561. case VC4_TEXTURE_TYPE_BW1:
  562. case VC4_TEXTURE_TYPE_A4:
  563. case VC4_TEXTURE_TYPE_A1:
  564. case VC4_TEXTURE_TYPE_RGBA64:
  565. case VC4_TEXTURE_TYPE_YUV422R:
  566. default:
  567. DRM_ERROR("Texture format %d unsupported\n", type);
  568. goto fail;
  569. }
  570. utile_w = utile_width(cpp);
  571. utile_h = utile_height(cpp);
  572. if (type == VC4_TEXTURE_TYPE_RGBA32R) {
  573. tiling_format = VC4_TILING_FORMAT_LINEAR;
  574. } else {
  575. if (size_is_lt(width, height, cpp))
  576. tiling_format = VC4_TILING_FORMAT_LT;
  577. else
  578. tiling_format = VC4_TILING_FORMAT_T;
  579. }
  580. if (!vc4_check_tex_size(exec, tex, offset + cube_map_stride * 5,
  581. tiling_format, width, height, cpp)) {
  582. goto fail;
  583. }
  584. /* The mipmap levels are stored before the base of the texture. Make
  585. * sure there is actually space in the BO.
  586. */
  587. for (i = 1; i <= miplevels; i++) {
  588. uint32_t level_width = max(width >> i, 1u);
  589. uint32_t level_height = max(height >> i, 1u);
  590. uint32_t aligned_width, aligned_height;
  591. uint32_t level_size;
  592. /* Once the levels get small enough, they drop from T to LT. */
  593. if (tiling_format == VC4_TILING_FORMAT_T &&
  594. size_is_lt(level_width, level_height, cpp)) {
  595. tiling_format = VC4_TILING_FORMAT_LT;
  596. }
  597. switch (tiling_format) {
  598. case VC4_TILING_FORMAT_T:
  599. aligned_width = round_up(level_width, utile_w * 8);
  600. aligned_height = round_up(level_height, utile_h * 8);
  601. break;
  602. case VC4_TILING_FORMAT_LT:
  603. aligned_width = round_up(level_width, utile_w);
  604. aligned_height = round_up(level_height, utile_h);
  605. break;
  606. default:
  607. aligned_width = round_up(level_width, utile_w);
  608. aligned_height = level_height;
  609. break;
  610. }
  611. level_size = aligned_width * cpp * aligned_height;
  612. if (offset < level_size) {
  613. DRM_ERROR("Level %d (%dx%d -> %dx%d) size %db "
  614. "overflowed buffer bounds (offset %d)\n",
  615. i, level_width, level_height,
  616. aligned_width, aligned_height,
  617. level_size, offset);
  618. goto fail;
  619. }
  620. offset -= level_size;
  621. }
  622. *validated_p0 = tex->paddr + p0;
  623. if (is_cs) {
  624. exec->bin_dep_seqno = max(exec->bin_dep_seqno,
  625. to_vc4_bo(&tex->base)->write_seqno);
  626. }
  627. return true;
  628. fail:
  629. DRM_INFO("Texture p0 at %d: 0x%08x\n", sample->p_offset[0], p0);
  630. DRM_INFO("Texture p1 at %d: 0x%08x\n", sample->p_offset[1], p1);
  631. DRM_INFO("Texture p2 at %d: 0x%08x\n", sample->p_offset[2], p2);
  632. DRM_INFO("Texture p3 at %d: 0x%08x\n", sample->p_offset[3], p3);
  633. return false;
  634. }
  635. static int
  636. validate_gl_shader_rec(struct drm_device *dev,
  637. struct vc4_exec_info *exec,
  638. struct vc4_shader_state *state)
  639. {
  640. uint32_t *src_handles;
  641. void *pkt_u, *pkt_v;
  642. static const uint32_t shader_reloc_offsets[] = {
  643. 4, /* fs */
  644. 16, /* vs */
  645. 28, /* cs */
  646. };
  647. uint32_t shader_reloc_count = ARRAY_SIZE(shader_reloc_offsets);
  648. struct drm_gem_cma_object *bo[shader_reloc_count + 8];
  649. uint32_t nr_attributes, nr_relocs, packet_size;
  650. int i;
  651. nr_attributes = state->addr & 0x7;
  652. if (nr_attributes == 0)
  653. nr_attributes = 8;
  654. packet_size = gl_shader_rec_size(state->addr);
  655. nr_relocs = ARRAY_SIZE(shader_reloc_offsets) + nr_attributes;
  656. if (nr_relocs * 4 > exec->shader_rec_size) {
  657. DRM_ERROR("overflowed shader recs reading %d handles "
  658. "from %d bytes left\n",
  659. nr_relocs, exec->shader_rec_size);
  660. return -EINVAL;
  661. }
  662. src_handles = exec->shader_rec_u;
  663. exec->shader_rec_u += nr_relocs * 4;
  664. exec->shader_rec_size -= nr_relocs * 4;
  665. if (packet_size > exec->shader_rec_size) {
  666. DRM_ERROR("overflowed shader recs copying %db packet "
  667. "from %d bytes left\n",
  668. packet_size, exec->shader_rec_size);
  669. return -EINVAL;
  670. }
  671. pkt_u = exec->shader_rec_u;
  672. pkt_v = exec->shader_rec_v;
  673. memcpy(pkt_v, pkt_u, packet_size);
  674. exec->shader_rec_u += packet_size;
  675. /* Shader recs have to be aligned to 16 bytes (due to the attribute
  676. * flags being in the low bytes), so round the next validated shader
  677. * rec address up. This should be safe, since we've got so many
  678. * relocations in a shader rec packet.
  679. */
  680. BUG_ON(roundup(packet_size, 16) - packet_size > nr_relocs * 4);
  681. exec->shader_rec_v += roundup(packet_size, 16);
  682. exec->shader_rec_size -= packet_size;
  683. for (i = 0; i < shader_reloc_count; i++) {
  684. if (src_handles[i] > exec->bo_count) {
  685. DRM_ERROR("Shader handle %d too big\n", src_handles[i]);
  686. return -EINVAL;
  687. }
  688. bo[i] = exec->bo[src_handles[i]];
  689. if (!bo[i])
  690. return -EINVAL;
  691. }
  692. for (i = shader_reloc_count; i < nr_relocs; i++) {
  693. bo[i] = vc4_use_bo(exec, src_handles[i]);
  694. if (!bo[i])
  695. return -EINVAL;
  696. }
  697. if (((*(uint16_t *)pkt_u & VC4_SHADER_FLAG_FS_SINGLE_THREAD) == 0) !=
  698. to_vc4_bo(&bo[0]->base)->validated_shader->is_threaded) {
  699. DRM_ERROR("Thread mode of CL and FS do not match\n");
  700. return -EINVAL;
  701. }
  702. if (to_vc4_bo(&bo[1]->base)->validated_shader->is_threaded ||
  703. to_vc4_bo(&bo[2]->base)->validated_shader->is_threaded) {
  704. DRM_ERROR("cs and vs cannot be threaded\n");
  705. return -EINVAL;
  706. }
  707. for (i = 0; i < shader_reloc_count; i++) {
  708. struct vc4_validated_shader_info *validated_shader;
  709. uint32_t o = shader_reloc_offsets[i];
  710. uint32_t src_offset = *(uint32_t *)(pkt_u + o);
  711. uint32_t *texture_handles_u;
  712. void *uniform_data_u;
  713. uint32_t tex, uni;
  714. *(uint32_t *)(pkt_v + o) = bo[i]->paddr + src_offset;
  715. if (src_offset != 0) {
  716. DRM_ERROR("Shaders must be at offset 0 of "
  717. "the BO.\n");
  718. return -EINVAL;
  719. }
  720. validated_shader = to_vc4_bo(&bo[i]->base)->validated_shader;
  721. if (!validated_shader)
  722. return -EINVAL;
  723. if (validated_shader->uniforms_src_size >
  724. exec->uniforms_size) {
  725. DRM_ERROR("Uniforms src buffer overflow\n");
  726. return -EINVAL;
  727. }
  728. texture_handles_u = exec->uniforms_u;
  729. uniform_data_u = (texture_handles_u +
  730. validated_shader->num_texture_samples);
  731. memcpy(exec->uniforms_v, uniform_data_u,
  732. validated_shader->uniforms_size);
  733. for (tex = 0;
  734. tex < validated_shader->num_texture_samples;
  735. tex++) {
  736. if (!reloc_tex(exec,
  737. uniform_data_u,
  738. &validated_shader->texture_samples[tex],
  739. texture_handles_u[tex],
  740. i == 2)) {
  741. return -EINVAL;
  742. }
  743. }
  744. /* Fill in the uniform slots that need this shader's
  745. * start-of-uniforms address (used for resetting the uniform
  746. * stream in the presence of control flow).
  747. */
  748. for (uni = 0;
  749. uni < validated_shader->num_uniform_addr_offsets;
  750. uni++) {
  751. uint32_t o = validated_shader->uniform_addr_offsets[uni];
  752. ((uint32_t *)exec->uniforms_v)[o] = exec->uniforms_p;
  753. }
  754. *(uint32_t *)(pkt_v + o + 4) = exec->uniforms_p;
  755. exec->uniforms_u += validated_shader->uniforms_src_size;
  756. exec->uniforms_v += validated_shader->uniforms_size;
  757. exec->uniforms_p += validated_shader->uniforms_size;
  758. }
  759. for (i = 0; i < nr_attributes; i++) {
  760. struct drm_gem_cma_object *vbo =
  761. bo[ARRAY_SIZE(shader_reloc_offsets) + i];
  762. uint32_t o = 36 + i * 8;
  763. uint32_t offset = *(uint32_t *)(pkt_u + o + 0);
  764. uint32_t attr_size = *(uint8_t *)(pkt_u + o + 4) + 1;
  765. uint32_t stride = *(uint8_t *)(pkt_u + o + 5);
  766. uint32_t max_index;
  767. exec->bin_dep_seqno = max(exec->bin_dep_seqno,
  768. to_vc4_bo(&vbo->base)->write_seqno);
  769. if (state->addr & 0x8)
  770. stride |= (*(uint32_t *)(pkt_u + 100 + i * 4)) & ~0xff;
  771. if (vbo->base.size < offset ||
  772. vbo->base.size - offset < attr_size) {
  773. DRM_ERROR("BO offset overflow (%d + %d > %zu)\n",
  774. offset, attr_size, vbo->base.size);
  775. return -EINVAL;
  776. }
  777. if (stride != 0) {
  778. max_index = ((vbo->base.size - offset - attr_size) /
  779. stride);
  780. if (state->max_index > max_index) {
  781. DRM_ERROR("primitives use index %d out of "
  782. "supplied %d\n",
  783. state->max_index, max_index);
  784. return -EINVAL;
  785. }
  786. }
  787. *(uint32_t *)(pkt_v + o) = vbo->paddr + offset;
  788. }
  789. return 0;
  790. }
  791. int
  792. vc4_validate_shader_recs(struct drm_device *dev,
  793. struct vc4_exec_info *exec)
  794. {
  795. uint32_t i;
  796. int ret = 0;
  797. for (i = 0; i < exec->shader_state_count; i++) {
  798. ret = validate_gl_shader_rec(dev, exec, &exec->shader_state[i]);
  799. if (ret)
  800. return ret;
  801. }
  802. return ret;
  803. }