1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2020 NVIDIA Corporation */
8 struct tegra_drm_firewall {
9 struct tegra_drm_submit_data *submit;
10 struct tegra_drm_client *client;
17 static int fw_next(struct tegra_drm_firewall *fw, u32 *word)
19 if (fw->pos == fw->end)
22 *word = fw->data[fw->pos++];
27 static bool fw_check_addr_valid(struct tegra_drm_firewall *fw, u32 offset)
31 for (i = 0; i < fw->submit->num_used_mappings; i++) {
32 struct tegra_drm_mapping *m = fw->submit->used_mappings[i].mapping;
34 if (offset >= m->iova && offset <= m->iova_end)
41 static int fw_check_reg(struct tegra_drm_firewall *fw, u32 offset)
47 err = fw_next(fw, &word);
51 if (!fw->client->ops->is_addr_reg)
54 is_addr = fw->client->ops->is_addr_reg(fw->client->base.dev, fw->class,
60 if (!fw_check_addr_valid(fw, word))
66 static int fw_check_regs_seq(struct tegra_drm_firewall *fw, u32 offset,
71 for (i = 0; i < count; i++) {
72 if (fw_check_reg(fw, offset))
82 static int fw_check_regs_mask(struct tegra_drm_firewall *fw, u32 offset,
85 unsigned long bmask = mask;
88 for_each_set_bit(bit, &bmask, 16) {
89 if (fw_check_reg(fw, offset+bit))
96 static int fw_check_regs_imm(struct tegra_drm_firewall *fw, u32 offset)
100 if (!fw->client->ops->is_addr_reg)
103 is_addr = fw->client->ops->is_addr_reg(fw->client->base.dev, fw->class,
111 static int fw_check_class(struct tegra_drm_firewall *fw, u32 class)
113 if (!fw->client->ops->is_valid_class) {
114 if (class == fw->client->base.class)
120 if (!fw->client->ops->is_valid_class(class))
127 HOST1X_OPCODE_SETCLASS = 0x00,
128 HOST1X_OPCODE_INCR = 0x01,
129 HOST1X_OPCODE_NONINCR = 0x02,
130 HOST1X_OPCODE_MASK = 0x03,
131 HOST1X_OPCODE_IMM = 0x04,
132 HOST1X_OPCODE_RESTART = 0x05,
133 HOST1X_OPCODE_GATHER = 0x06,
134 HOST1X_OPCODE_SETSTRMID = 0x07,
135 HOST1X_OPCODE_SETAPPID = 0x08,
136 HOST1X_OPCODE_SETPYLD = 0x09,
137 HOST1X_OPCODE_INCR_W = 0x0a,
138 HOST1X_OPCODE_NONINCR_W = 0x0b,
139 HOST1X_OPCODE_GATHER_W = 0x0c,
140 HOST1X_OPCODE_RESTART_W = 0x0d,
141 HOST1X_OPCODE_EXTEND = 0x0e,
144 int tegra_drm_fw_validate(struct tegra_drm_client *client, u32 *data, u32 start,
145 u32 words, struct tegra_drm_submit_data *submit,
148 struct tegra_drm_firewall fw = {
156 bool payload_valid = false;
160 while (fw.pos != fw.end) {
161 u32 word, opcode, offset, count, mask, class;
163 err = fw_next(&fw, &word);
167 opcode = (word & 0xf0000000) >> 28;
170 case HOST1X_OPCODE_SETCLASS:
171 offset = word >> 16 & 0xfff;
173 class = (word >> 6) & 0x3ff;
174 err = fw_check_class(&fw, class);
178 err = fw_check_regs_mask(&fw, offset, mask);
180 dev_warn(client->base.dev,
181 "illegal SETCLASS(offset=0x%x, mask=0x%x, class=0x%x) at word %u",
182 offset, mask, class, fw.pos-1);
184 case HOST1X_OPCODE_INCR:
185 offset = (word >> 16) & 0xfff;
186 count = word & 0xffff;
187 err = fw_check_regs_seq(&fw, offset, count, true);
189 dev_warn(client->base.dev,
190 "illegal INCR(offset=0x%x, count=%u) in class 0x%x at word %u",
191 offset, count, fw.class, fw.pos-1);
193 case HOST1X_OPCODE_NONINCR:
194 offset = (word >> 16) & 0xfff;
195 count = word & 0xffff;
196 err = fw_check_regs_seq(&fw, offset, count, false);
198 dev_warn(client->base.dev,
199 "illegal NONINCR(offset=0x%x, count=%u) in class 0x%x at word %u",
200 offset, count, fw.class, fw.pos-1);
202 case HOST1X_OPCODE_MASK:
203 offset = (word >> 16) & 0xfff;
204 mask = word & 0xffff;
205 err = fw_check_regs_mask(&fw, offset, mask);
207 dev_warn(client->base.dev,
208 "illegal MASK(offset=0x%x, mask=0x%x) in class 0x%x at word %u",
209 offset, mask, fw.class, fw.pos-1);
211 case HOST1X_OPCODE_IMM:
212 /* IMM cannot reasonably be used to write a pointer */
213 offset = (word >> 16) & 0xfff;
214 err = fw_check_regs_imm(&fw, offset);
216 dev_warn(client->base.dev,
217 "illegal IMM(offset=0x%x) in class 0x%x at word %u",
218 offset, fw.class, fw.pos-1);
220 case HOST1X_OPCODE_SETPYLD:
221 payload = word & 0xffff;
222 payload_valid = true;
224 case HOST1X_OPCODE_INCR_W:
228 offset = word & 0x3fffff;
229 err = fw_check_regs_seq(&fw, offset, payload, true);
231 dev_warn(client->base.dev,
232 "illegal INCR_W(offset=0x%x) in class 0x%x at word %u",
233 offset, fw.class, fw.pos-1);
235 case HOST1X_OPCODE_NONINCR_W:
239 offset = word & 0x3fffff;
240 err = fw_check_regs_seq(&fw, offset, payload, false);
242 dev_warn(client->base.dev,
243 "illegal NONINCR(offset=0x%x) in class 0x%x at word %u",
244 offset, fw.class, fw.pos-1);
247 dev_warn(client->base.dev, "illegal opcode at word %u",