etnaviv_cmd_parser.c 5.1 KB

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