etnaviv_cmd_parser.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(0x1608, 1),
  52. ST(0x1610, 1),
  53. ST(0x1658, 1),
  54. ST(0x165c, 1),
  55. ST(0x1664, 1),
  56. ST(0x1668, 1),
  57. ST(0x16a4, 1),
  58. ST(0x16c0, 8),
  59. ST(0x16e0, 8),
  60. ST(0x1740, 8),
  61. ST(0x17c0, 8),
  62. ST(0x17e0, 8),
  63. ST(0x2400, 14 * 16),
  64. ST(0x3824, 1),
  65. ST(0x10800, 32 * 16),
  66. ST(0x14600, 16),
  67. ST(0x14800, 8 * 8),
  68. #undef ST
  69. };
  70. #define ETNAVIV_STATES_SIZE (VIV_FE_LOAD_STATE_HEADER_OFFSET__MASK + 1u)
  71. static DECLARE_BITMAP(etnaviv_states, ETNAVIV_STATES_SIZE);
  72. void __init etnaviv_validate_init(void)
  73. {
  74. unsigned int i;
  75. for (i = 0; i < ARRAY_SIZE(etnaviv_sensitive_states); i++)
  76. bitmap_set(etnaviv_states, etnaviv_sensitive_states[i].offset,
  77. etnaviv_sensitive_states[i].size);
  78. }
  79. static void etnaviv_warn_if_non_sensitive(struct etna_validation_state *state,
  80. unsigned int buf_offset, unsigned int state_addr)
  81. {
  82. if (state->num_relocs && state->relocs->submit_offset < buf_offset) {
  83. dev_warn_once(state->gpu->dev,
  84. "%s: relocation for non-sensitive state 0x%x at offset %u\n",
  85. __func__, state_addr,
  86. state->relocs->submit_offset);
  87. while (state->num_relocs &&
  88. state->relocs->submit_offset < buf_offset) {
  89. state->relocs++;
  90. state->num_relocs--;
  91. }
  92. }
  93. }
  94. static bool etnaviv_validate_load_state(struct etna_validation_state *state,
  95. u32 *ptr, unsigned int state_offset, unsigned int num)
  96. {
  97. unsigned int size = min(ETNAVIV_STATES_SIZE, state_offset + num);
  98. unsigned int st_offset = state_offset, buf_offset;
  99. for_each_set_bit_from(st_offset, etnaviv_states, size) {
  100. buf_offset = (ptr - state->start +
  101. st_offset - state_offset) * 4;
  102. etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4);
  103. if (state->num_relocs &&
  104. state->relocs->submit_offset == buf_offset) {
  105. state->relocs++;
  106. state->num_relocs--;
  107. continue;
  108. }
  109. dev_warn_ratelimited(state->gpu->dev,
  110. "%s: load state touches restricted state 0x%x at offset %u\n",
  111. __func__, st_offset * 4, buf_offset);
  112. return false;
  113. }
  114. if (state->num_relocs) {
  115. buf_offset = (ptr - state->start + num) * 4;
  116. etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4 +
  117. state->relocs->submit_offset -
  118. buf_offset);
  119. }
  120. return true;
  121. }
  122. static uint8_t cmd_length[32] = {
  123. [FE_OPCODE_DRAW_PRIMITIVES] = 4,
  124. [FE_OPCODE_DRAW_INDEXED_PRIMITIVES] = 6,
  125. [FE_OPCODE_DRAW_INSTANCED] = 4,
  126. [FE_OPCODE_NOP] = 2,
  127. [FE_OPCODE_STALL] = 2,
  128. };
  129. bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu, u32 *stream,
  130. unsigned int size,
  131. struct drm_etnaviv_gem_submit_reloc *relocs,
  132. unsigned int reloc_size)
  133. {
  134. struct etna_validation_state state;
  135. u32 *buf = stream;
  136. u32 *end = buf + size;
  137. state.gpu = gpu;
  138. state.relocs = relocs;
  139. state.num_relocs = reloc_size;
  140. state.start = stream;
  141. while (buf < end) {
  142. u32 cmd = *buf;
  143. unsigned int len, n, off;
  144. unsigned int op = cmd >> 27;
  145. switch (op) {
  146. case FE_OPCODE_LOAD_STATE:
  147. n = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_COUNT);
  148. len = ALIGN(1 + n, 2);
  149. if (buf + len > end)
  150. break;
  151. off = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_OFFSET);
  152. if (!etnaviv_validate_load_state(&state, buf + 1,
  153. off, n))
  154. return false;
  155. break;
  156. case FE_OPCODE_DRAW_2D:
  157. n = EXTRACT(cmd, VIV_FE_DRAW_2D_HEADER_COUNT);
  158. if (n == 0)
  159. n = 256;
  160. len = 2 + n * 2;
  161. break;
  162. default:
  163. len = cmd_length[op];
  164. if (len == 0) {
  165. dev_err(gpu->dev, "%s: op %u not permitted at offset %tu\n",
  166. __func__, op, buf - state.start);
  167. return false;
  168. }
  169. break;
  170. }
  171. buf += len;
  172. }
  173. if (buf > end) {
  174. dev_err(gpu->dev, "%s: commands overflow end of buffer: %tu > %u\n",
  175. __func__, buf - state.start, size);
  176. return false;
  177. }
  178. return true;
  179. }