etnaviv_cmd_parser.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2018 Etnaviv Project
  4. */
  5. #include <linux/kernel.h>
  6. #include "etnaviv_gem.h"
  7. #include "etnaviv_gpu.h"
  8. #include "cmdstream.xml.h"
  9. #define EXTRACT(val, field) (((val) & field##__MASK) >> field##__SHIFT)
  10. struct etna_validation_state {
  11. struct etnaviv_gpu *gpu;
  12. const struct drm_etnaviv_gem_submit_reloc *relocs;
  13. unsigned int num_relocs;
  14. u32 *start;
  15. };
  16. static const struct {
  17. u16 offset;
  18. u16 size;
  19. } etnaviv_sensitive_states[] __initconst = {
  20. #define ST(start, num) { (start) >> 2, (num) }
  21. /* 2D */
  22. ST(0x1200, 1),
  23. ST(0x1228, 1),
  24. ST(0x1238, 1),
  25. ST(0x1284, 1),
  26. ST(0x128c, 1),
  27. ST(0x1304, 1),
  28. ST(0x1310, 1),
  29. ST(0x1318, 1),
  30. ST(0x12800, 4),
  31. ST(0x128a0, 4),
  32. ST(0x128c0, 4),
  33. ST(0x12970, 4),
  34. ST(0x12a00, 8),
  35. ST(0x12b40, 8),
  36. ST(0x12b80, 8),
  37. ST(0x12ce0, 8),
  38. /* 3D */
  39. ST(0x0644, 1),
  40. ST(0x064c, 1),
  41. ST(0x0680, 8),
  42. ST(0x086c, 1),
  43. ST(0x1028, 1),
  44. ST(0x1410, 1),
  45. ST(0x1430, 1),
  46. ST(0x1458, 1),
  47. ST(0x1460, 8),
  48. ST(0x1480, 8),
  49. ST(0x1500, 8),
  50. ST(0x1520, 8),
  51. ST(0x1540, 8),
  52. ST(0x1608, 1),
  53. ST(0x1610, 1),
  54. ST(0x1658, 1),
  55. ST(0x165c, 1),
  56. ST(0x1664, 1),
  57. ST(0x1668, 1),
  58. ST(0x16a4, 1),
  59. ST(0x16c0, 8),
  60. ST(0x16e0, 8),
  61. ST(0x1740, 8),
  62. ST(0x17c0, 8),
  63. ST(0x17e0, 8),
  64. ST(0x2400, 14 * 16),
  65. ST(0x3824, 1),
  66. ST(0x10800, 32 * 16),
  67. ST(0x14600, 16),
  68. ST(0x14800, 8 * 8),
  69. #undef ST
  70. };
  71. #define ETNAVIV_STATES_SIZE (VIV_FE_LOAD_STATE_HEADER_OFFSET__MASK + 1u)
  72. static DECLARE_BITMAP(etnaviv_states, ETNAVIV_STATES_SIZE);
  73. void __init etnaviv_validate_init(void)
  74. {
  75. unsigned int i;
  76. for (i = 0; i < ARRAY_SIZE(etnaviv_sensitive_states); i++)
  77. bitmap_set(etnaviv_states, etnaviv_sensitive_states[i].offset,
  78. etnaviv_sensitive_states[i].size);
  79. }
  80. static void etnaviv_warn_if_non_sensitive(struct etna_validation_state *state,
  81. unsigned int buf_offset, unsigned int state_addr)
  82. {
  83. if (state->num_relocs && state->relocs->submit_offset < buf_offset) {
  84. dev_warn_once(state->gpu->dev,
  85. "%s: relocation for non-sensitive state 0x%x at offset %u\n",
  86. __func__, state_addr,
  87. state->relocs->submit_offset);
  88. while (state->num_relocs &&
  89. state->relocs->submit_offset < buf_offset) {
  90. state->relocs++;
  91. state->num_relocs--;
  92. }
  93. }
  94. }
  95. static bool etnaviv_validate_load_state(struct etna_validation_state *state,
  96. u32 *ptr, unsigned int state_offset, unsigned int num)
  97. {
  98. unsigned int size = min(ETNAVIV_STATES_SIZE, state_offset + num);
  99. unsigned int st_offset = state_offset, buf_offset;
  100. for_each_set_bit_from(st_offset, etnaviv_states, size) {
  101. buf_offset = (ptr - state->start +
  102. st_offset - state_offset) * 4;
  103. etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4);
  104. if (state->num_relocs &&
  105. state->relocs->submit_offset == buf_offset) {
  106. state->relocs++;
  107. state->num_relocs--;
  108. continue;
  109. }
  110. dev_warn_ratelimited(state->gpu->dev,
  111. "%s: load state touches restricted state 0x%x at offset %u\n",
  112. __func__, st_offset * 4, buf_offset);
  113. return false;
  114. }
  115. if (state->num_relocs) {
  116. buf_offset = (ptr - state->start + num) * 4;
  117. etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4 +
  118. state->relocs->submit_offset -
  119. buf_offset);
  120. }
  121. return true;
  122. }
  123. static uint8_t cmd_length[32] = {
  124. [FE_OPCODE_DRAW_PRIMITIVES] = 4,
  125. [FE_OPCODE_DRAW_INDEXED_PRIMITIVES] = 6,
  126. [FE_OPCODE_DRAW_INSTANCED] = 4,
  127. [FE_OPCODE_NOP] = 2,
  128. [FE_OPCODE_STALL] = 2,
  129. };
  130. bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu, u32 *stream,
  131. unsigned int size,
  132. struct drm_etnaviv_gem_submit_reloc *relocs,
  133. unsigned int reloc_size)
  134. {
  135. struct etna_validation_state state;
  136. u32 *buf = stream;
  137. u32 *end = buf + size;
  138. state.gpu = gpu;
  139. state.relocs = relocs;
  140. state.num_relocs = reloc_size;
  141. state.start = stream;
  142. while (buf < end) {
  143. u32 cmd = *buf;
  144. unsigned int len, n, off;
  145. unsigned int op = cmd >> 27;
  146. switch (op) {
  147. case FE_OPCODE_LOAD_STATE:
  148. n = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_COUNT);
  149. len = ALIGN(1 + n, 2);
  150. if (buf + len > end)
  151. break;
  152. off = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_OFFSET);
  153. if (!etnaviv_validate_load_state(&state, buf + 1,
  154. off, n))
  155. return false;
  156. break;
  157. case FE_OPCODE_DRAW_2D:
  158. n = EXTRACT(cmd, VIV_FE_DRAW_2D_HEADER_COUNT);
  159. if (n == 0)
  160. n = 256;
  161. len = 2 + n * 2;
  162. break;
  163. default:
  164. len = cmd_length[op];
  165. if (len == 0) {
  166. dev_err(gpu->dev, "%s: op %u not permitted at offset %tu\n",
  167. __func__, op, buf - state.start);
  168. return false;
  169. }
  170. break;
  171. }
  172. buf += len;
  173. }
  174. if (buf > end) {
  175. dev_err(gpu->dev, "%s: commands overflow end of buffer: %tu > %u\n",
  176. __func__, buf - state.start, size);
  177. return false;
  178. }
  179. return true;
  180. }