2 * Copyright (C) 2015 Etnaviv Project
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.
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
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/>.
17 #include <linux/kernel.h>
19 #include "etnaviv_gem.h"
20 #include "etnaviv_gpu.h"
22 #include "cmdstream.xml.h"
24 #define EXTRACT(val, field) (((val) & field##__MASK) >> field##__SHIFT)
26 struct etna_validation_state {
27 struct etnaviv_gpu *gpu;
28 const struct drm_etnaviv_gem_submit_reloc *relocs;
29 unsigned int num_relocs;
36 } etnaviv_sensitive_states[] __initconst = {
37 #define ST(start, num) { (start) >> 2, (num) }
87 #define ETNAVIV_STATES_SIZE (VIV_FE_LOAD_STATE_HEADER_OFFSET__MASK + 1u)
88 static DECLARE_BITMAP(etnaviv_states, ETNAVIV_STATES_SIZE);
90 void __init etnaviv_validate_init(void)
94 for (i = 0; i < ARRAY_SIZE(etnaviv_sensitive_states); i++)
95 bitmap_set(etnaviv_states, etnaviv_sensitive_states[i].offset,
96 etnaviv_sensitive_states[i].size);
99 static void etnaviv_warn_if_non_sensitive(struct etna_validation_state *state,
100 unsigned int buf_offset, unsigned int state_addr)
102 if (state->num_relocs && state->relocs->submit_offset < buf_offset) {
103 dev_warn_once(state->gpu->dev,
104 "%s: relocation for non-sensitive state 0x%x at offset %u\n",
105 __func__, state_addr,
106 state->relocs->submit_offset);
107 while (state->num_relocs &&
108 state->relocs->submit_offset < buf_offset) {
115 static bool etnaviv_validate_load_state(struct etna_validation_state *state,
116 u32 *ptr, unsigned int state_offset, unsigned int num)
118 unsigned int size = min(ETNAVIV_STATES_SIZE, state_offset + num);
119 unsigned int st_offset = state_offset, buf_offset;
121 for_each_set_bit_from(st_offset, etnaviv_states, size) {
122 buf_offset = (ptr - state->start +
123 st_offset - state_offset) * 4;
125 etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4);
126 if (state->num_relocs &&
127 state->relocs->submit_offset == buf_offset) {
133 dev_warn_ratelimited(state->gpu->dev,
134 "%s: load state touches restricted state 0x%x at offset %u\n",
135 __func__, st_offset * 4, buf_offset);
139 if (state->num_relocs) {
140 buf_offset = (ptr - state->start + num) * 4;
141 etnaviv_warn_if_non_sensitive(state, buf_offset, st_offset * 4 +
142 state->relocs->submit_offset -
149 static uint8_t cmd_length[32] = {
150 [FE_OPCODE_DRAW_PRIMITIVES] = 4,
151 [FE_OPCODE_DRAW_INDEXED_PRIMITIVES] = 6,
152 [FE_OPCODE_DRAW_INSTANCED] = 4,
154 [FE_OPCODE_STALL] = 2,
157 bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu, u32 *stream,
159 struct drm_etnaviv_gem_submit_reloc *relocs,
160 unsigned int reloc_size)
162 struct etna_validation_state state;
164 u32 *end = buf + size;
167 state.relocs = relocs;
168 state.num_relocs = reloc_size;
169 state.start = stream;
173 unsigned int len, n, off;
174 unsigned int op = cmd >> 27;
177 case FE_OPCODE_LOAD_STATE:
178 n = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_COUNT);
179 len = ALIGN(1 + n, 2);
183 off = EXTRACT(cmd, VIV_FE_LOAD_STATE_HEADER_OFFSET);
184 if (!etnaviv_validate_load_state(&state, buf + 1,
189 case FE_OPCODE_DRAW_2D:
190 n = EXTRACT(cmd, VIV_FE_DRAW_2D_HEADER_COUNT);
197 len = cmd_length[op];
199 dev_err(gpu->dev, "%s: op %u not permitted at offset %tu\n",
200 __func__, op, buf - state.start);
210 dev_err(gpu->dev, "%s: commands overflow end of buffer: %tu > %u\n",
211 __func__, buf - state.start, size);