etnaviv_cmd_parser.c 5.0 KB

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