blob: fb2609434df7c30118d95616abbe3ed4ad38aa04 [file] [log] [blame]
Dave Airlie22f579c2005-06-28 22:48:56 +10001/*
2 * Copyright 2004 The Unichrome Project. All Rights Reserved.
3 * Copyright 2005 Thomas Hellstrom. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S), AND/OR THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Author: Thomas Hellstrom 2004, 2005.
25 * This code was written using docs obtained under NDA from VIA Inc.
26 *
27 * Don't run this code directly on an AGP buffer. Due to cache problems it will
28 * be very slow.
29 */
30
Dave Airlie22f579c2005-06-28 22:48:56 +100031#include "via_3d_reg.h"
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/via_drm.h>
Daniel Vetterba8286f2014-09-11 07:43:25 +020034#include <drm/drm_legacy.h>
Dave Airlie22f579c2005-06-28 22:48:56 +100035#include "via_verifier.h"
36#include "via_drv.h"
Jérémy Lefaure2e20c9d2017-10-15 22:33:56 -040037#include <linux/kernel.h>
Dave Airlie22f579c2005-06-28 22:48:56 +100038
Dave Airlieb5e89ed2005-09-25 14:28:13 +100039typedef enum {
Dave Airlie22f579c2005-06-28 22:48:56 +100040 state_command,
41 state_header2,
42 state_header1,
43 state_vheader5,
44 state_vheader6,
45 state_error
46} verifier_state_t;
47
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048typedef enum {
Dave Airlie22f579c2005-06-28 22:48:56 +100049 no_check = 0,
50 check_for_header2,
51 check_for_header1,
52 check_for_header2_err,
53 check_for_header1_err,
54 check_for_fire,
55 check_z_buffer_addr0,
56 check_z_buffer_addr1,
57 check_z_buffer_addr_mode,
58 check_destination_addr0,
59 check_destination_addr1,
60 check_destination_addr_mode,
61 check_for_dummy,
62 check_for_dd,
63 check_texture_addr0,
64 check_texture_addr1,
65 check_texture_addr2,
66 check_texture_addr3,
67 check_texture_addr4,
68 check_texture_addr5,
69 check_texture_addr6,
70 check_texture_addr7,
71 check_texture_addr8,
72 check_texture_addr_mode,
73 check_for_vertex_count,
74 check_number_texunits,
75 forbidden_command
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076} hazard_t;
Dave Airlie22f579c2005-06-28 22:48:56 +100077
78/*
79 * Associates each hazard above with a possible multi-command
80 * sequence. For example an address that is split over multiple
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 * commands and that needs to be checked at the first command
Dave Airlie22f579c2005-06-28 22:48:56 +100082 * that does not include any part of the address.
83 */
84
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085static drm_via_sequence_t seqs[] = {
Dave Airlie22f579c2005-06-28 22:48:56 +100086 no_sequence,
87 no_sequence,
88 no_sequence,
89 no_sequence,
90 no_sequence,
91 no_sequence,
92 z_address,
93 z_address,
94 z_address,
95 dest_address,
96 dest_address,
97 dest_address,
98 no_sequence,
99 no_sequence,
100 tex_address,
101 tex_address,
102 tex_address,
103 tex_address,
104 tex_address,
105 tex_address,
106 tex_address,
107 tex_address,
108 tex_address,
109 tex_address,
110 no_sequence
111};
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112
113typedef struct {
Dave Airlie22f579c2005-06-28 22:48:56 +1000114 unsigned int code;
115 hazard_t hz;
116} hz_init_t;
117
Dave Airlie22f579c2005-06-28 22:48:56 +1000118static hz_init_t init_table1[] = {
119 {0xf2, check_for_header2_err},
120 {0xf0, check_for_header1_err},
121 {0xee, check_for_fire},
122 {0xcc, check_for_dummy},
123 {0xdd, check_for_dd},
124 {0x00, no_check},
125 {0x10, check_z_buffer_addr0},
126 {0x11, check_z_buffer_addr1},
127 {0x12, check_z_buffer_addr_mode},
128 {0x13, no_check},
129 {0x14, no_check},
130 {0x15, no_check},
131 {0x23, no_check},
132 {0x24, no_check},
133 {0x33, no_check},
134 {0x34, no_check},
135 {0x35, no_check},
136 {0x36, no_check},
137 {0x37, no_check},
138 {0x38, no_check},
139 {0x39, no_check},
140 {0x3A, no_check},
141 {0x3B, no_check},
142 {0x3C, no_check},
143 {0x3D, no_check},
144 {0x3E, no_check},
145 {0x40, check_destination_addr0},
146 {0x41, check_destination_addr1},
147 {0x42, check_destination_addr_mode},
148 {0x43, no_check},
149 {0x44, no_check},
150 {0x50, no_check},
151 {0x51, no_check},
152 {0x52, no_check},
153 {0x53, no_check},
154 {0x54, no_check},
155 {0x55, no_check},
156 {0x56, no_check},
157 {0x57, no_check},
158 {0x58, no_check},
159 {0x70, no_check},
160 {0x71, no_check},
161 {0x78, no_check},
162 {0x79, no_check},
163 {0x7A, no_check},
164 {0x7B, no_check},
165 {0x7C, no_check},
166 {0x7D, check_for_vertex_count}
167};
168
Dave Airlie22f579c2005-06-28 22:48:56 +1000169static hz_init_t init_table2[] = {
170 {0xf2, check_for_header2_err},
171 {0xf0, check_for_header1_err},
172 {0xee, check_for_fire},
173 {0xcc, check_for_dummy},
174 {0x00, check_texture_addr0},
175 {0x01, check_texture_addr0},
176 {0x02, check_texture_addr0},
177 {0x03, check_texture_addr0},
178 {0x04, check_texture_addr0},
179 {0x05, check_texture_addr0},
180 {0x06, check_texture_addr0},
181 {0x07, check_texture_addr0},
182 {0x08, check_texture_addr0},
183 {0x09, check_texture_addr0},
184 {0x20, check_texture_addr1},
185 {0x21, check_texture_addr1},
186 {0x22, check_texture_addr1},
187 {0x23, check_texture_addr4},
188 {0x2B, check_texture_addr3},
189 {0x2C, check_texture_addr3},
190 {0x2D, check_texture_addr3},
191 {0x2E, check_texture_addr3},
192 {0x2F, check_texture_addr3},
193 {0x30, check_texture_addr3},
194 {0x31, check_texture_addr3},
195 {0x32, check_texture_addr3},
196 {0x33, check_texture_addr3},
197 {0x34, check_texture_addr3},
198 {0x4B, check_texture_addr5},
199 {0x4C, check_texture_addr6},
200 {0x51, check_texture_addr7},
201 {0x52, check_texture_addr8},
202 {0x77, check_texture_addr2},
203 {0x78, no_check},
204 {0x79, no_check},
205 {0x7A, no_check},
206 {0x7B, check_texture_addr_mode},
207 {0x7C, no_check},
208 {0x7D, no_check},
209 {0x7E, no_check},
210 {0x7F, no_check},
211 {0x80, no_check},
212 {0x81, no_check},
213 {0x82, no_check},
214 {0x83, no_check},
215 {0x85, no_check},
216 {0x86, no_check},
217 {0x87, no_check},
218 {0x88, no_check},
219 {0x89, no_check},
220 {0x8A, no_check},
221 {0x90, no_check},
222 {0x91, no_check},
223 {0x92, no_check},
224 {0x93, no_check}
225};
226
227static hz_init_t init_table3[] = {
228 {0xf2, check_for_header2_err},
229 {0xf0, check_for_header1_err},
230 {0xcc, check_for_dummy},
231 {0x00, check_number_texunits}
232};
Dave Airlie22f579c2005-06-28 22:48:56 +1000233
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234static hazard_t table1[256];
235static hazard_t table2[256];
236static hazard_t table3[256];
Dave Airlie22f579c2005-06-28 22:48:56 +1000237
238static __inline__ int
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200239eat_words(const uint32_t **buf, const uint32_t *buf_end, unsigned num_words)
Dave Airlie22f579c2005-06-28 22:48:56 +1000240{
Dave Airlie92514242005-11-12 21:52:46 +1100241 if ((buf_end - *buf) >= num_words) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000242 *buf += num_words;
243 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000245 DRM_ERROR("Illegal termination of DMA command buffer\n");
246 return 1;
247}
248
Dave Airlie22f579c2005-06-28 22:48:56 +1000249/*
250 * Partially stolen from drm_memory.h
251 */
252
Dave Airlie92514242005-11-12 21:52:46 +1100253static __inline__ drm_local_map_t *via_drm_lookup_agp_map(drm_via_state_t *seq,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 unsigned long offset,
255 unsigned long size,
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200256 struct drm_device *dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000257{
Dave Airlie55910512007-07-11 16:53:40 +1000258 struct drm_map_list *r_list;
Dave Airlie92514242005-11-12 21:52:46 +1100259 drm_local_map_t *map = seq->map_cache;
Dave Airlie22f579c2005-06-28 22:48:56 +1000260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 if (map && map->offset <= offset
262 && (offset + size) <= (map->offset + map->size)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000263 return map;
264 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265
Dave Airliebd1b3312007-05-26 05:01:51 +1000266 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000267 map = r_list->map;
268 if (!map)
269 continue;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 if (map->offset <= offset
271 && (offset + size) <= (map->offset + map->size)
272 && !(map->flags & _DRM_RESTRICTED)
273 && (map->type == _DRM_AGP)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000274 seq->map_cache = map;
275 return map;
276 }
277 }
278 return NULL;
279}
280
Dave Airlie22f579c2005-06-28 22:48:56 +1000281/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 * Require that all AGP texture levels reside in the same AGP map which should
Dave Airlie22f579c2005-06-28 22:48:56 +1000283 * be mappable by the client. This is not a big restriction.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 * FIXME: To actually enforce this security policy strictly, drm_rmmap
285 * would have to wait for dma quiescent before removing an AGP map.
Dave Airlie22f579c2005-06-28 22:48:56 +1000286 * The via_drm_lookup_agp_map call in reality seems to take
287 * very little CPU time.
288 */
289
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290static __inline__ int finish_current_sequence(drm_via_state_t * cur_seq)
Dave Airlie22f579c2005-06-28 22:48:56 +1000291{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 switch (cur_seq->unfinished) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000293 case z_address:
294 DRM_DEBUG("Z Buffer start address is 0x%x\n", cur_seq->z_addr);
295 break;
296 case dest_address:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 DRM_DEBUG("Destination start address is 0x%x\n",
298 cur_seq->d_addr);
Dave Airlie22f579c2005-06-28 22:48:56 +1000299 break;
300 case tex_address:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 if (cur_seq->agp_texture) {
302 unsigned start =
303 cur_seq->tex_level_lo[cur_seq->texture];
Dave Airlie22f579c2005-06-28 22:48:56 +1000304 unsigned end = cur_seq->tex_level_hi[cur_seq->texture];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 unsigned long lo = ~0, hi = 0, tmp;
Dave Airlie22f579c2005-06-28 22:48:56 +1000306 uint32_t *addr, *pitch, *height, tex;
307 unsigned i;
Thomas Hellstrom9b8d9d02007-01-08 21:21:41 +1100308 int npot;
Dave Airlie22f579c2005-06-28 22:48:56 +1000309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 if (end > 9)
311 end = 9;
312 if (start > 9)
313 start = 9;
Dave Airlie22f579c2005-06-28 22:48:56 +1000314
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 addr =
316 &(cur_seq->t_addr[tex = cur_seq->texture][start]);
Dave Airlie22f579c2005-06-28 22:48:56 +1000317 pitch = &(cur_seq->pitch[tex][start]);
318 height = &(cur_seq->height[tex][start]);
Thomas Hellstrom9b8d9d02007-01-08 21:21:41 +1100319 npot = cur_seq->tex_npot[tex];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 for (i = start; i <= end; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000321 tmp = *addr++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 if (tmp < lo)
323 lo = tmp;
Thomas Hellstrom9b8d9d02007-01-08 21:21:41 +1100324 if (i == 0 && npot)
325 tmp += (*height++ * *pitch++);
326 else
327 tmp += (*height++ << *pitch++);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 if (tmp > hi)
329 hi = tmp;
Dave Airlie22f579c2005-06-28 22:48:56 +1000330 }
331
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 if (!via_drm_lookup_agp_map
333 (cur_seq, lo, hi - lo, cur_seq->dev)) {
334 DRM_ERROR
335 ("AGP texture is not in allowed map\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000336 return 2;
337 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000339 break;
340 default:
341 break;
342 }
343 cur_seq->unfinished = no_sequence;
344 return 0;
345}
346
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347static __inline__ int
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200348investigate_hazard(uint32_t cmd, hazard_t hz, drm_via_state_t *cur_seq)
Dave Airlie22f579c2005-06-28 22:48:56 +1000349{
350 register uint32_t tmp, *tmp_addr;
351
352 if (cur_seq->unfinished && (cur_seq->unfinished != seqs[hz])) {
353 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 if ((ret = finish_current_sequence(cur_seq)))
355 return ret;
Dave Airlie22f579c2005-06-28 22:48:56 +1000356 }
357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 switch (hz) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000359 case check_for_header2:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 if (cmd == HALCYON_HEADER2)
361 return 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000362 return 0;
363 case check_for_header1:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
365 return 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000366 return 0;
367 case check_for_header2_err:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 if (cmd == HALCYON_HEADER2)
369 return 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000370 DRM_ERROR("Illegal DMA HALCYON_HEADER2 command\n");
371 break;
372 case check_for_header1_err:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
374 return 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000375 DRM_ERROR("Illegal DMA HALCYON_HEADER1 command\n");
376 break;
377 case check_for_fire:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378 if ((cmd & HALCYON_FIREMASK) == HALCYON_FIRECMD)
379 return 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000380 DRM_ERROR("Illegal DMA HALCYON_FIRECMD command\n");
381 break;
382 case check_for_dummy:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (HC_DUMMY == cmd)
384 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000385 DRM_ERROR("Illegal DMA HC_DUMMY command\n");
386 break;
387 case check_for_dd:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 if (0xdddddddd == cmd)
389 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000390 DRM_ERROR("Illegal DMA 0xdddddddd command\n");
391 break;
392 case check_z_buffer_addr0:
393 cur_seq->unfinished = z_address;
394 cur_seq->z_addr = (cur_seq->z_addr & 0xFF000000) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 (cmd & 0x00FFFFFF);
Dave Airlie22f579c2005-06-28 22:48:56 +1000396 return 0;
397 case check_z_buffer_addr1:
398 cur_seq->unfinished = z_address;
399 cur_seq->z_addr = (cur_seq->z_addr & 0x00FFFFFF) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000400 ((cmd & 0xFF) << 24);
Dave Airlie22f579c2005-06-28 22:48:56 +1000401 return 0;
402 case check_z_buffer_addr_mode:
403 cur_seq->unfinished = z_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404 if ((cmd & 0x0000C000) == 0)
405 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000406 DRM_ERROR("Attempt to place Z buffer in system memory\n");
407 return 2;
408 case check_destination_addr0:
409 cur_seq->unfinished = dest_address;
410 cur_seq->d_addr = (cur_seq->d_addr & 0xFF000000) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 (cmd & 0x00FFFFFF);
Dave Airlie22f579c2005-06-28 22:48:56 +1000412 return 0;
413 case check_destination_addr1:
414 cur_seq->unfinished = dest_address;
415 cur_seq->d_addr = (cur_seq->d_addr & 0x00FFFFFF) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 ((cmd & 0xFF) << 24);
Dave Airlie22f579c2005-06-28 22:48:56 +1000417 return 0;
418 case check_destination_addr_mode:
419 cur_seq->unfinished = dest_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 if ((cmd & 0x0000C000) == 0)
421 return 0;
422 DRM_ERROR
423 ("Attempt to place 3D drawing buffer in system memory\n");
424 return 2;
Dave Airlie22f579c2005-06-28 22:48:56 +1000425 case check_texture_addr0:
426 cur_seq->unfinished = tex_address;
427 tmp = (cmd >> 24);
428 tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
429 *tmp_addr = (*tmp_addr & 0xFF000000) | (cmd & 0x00FFFFFF);
430 return 0;
431 case check_texture_addr1:
432 cur_seq->unfinished = tex_address;
433 tmp = ((cmd >> 24) - 0x20);
434 tmp += tmp << 1;
435 tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
436 *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
437 tmp_addr++;
438 *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF00) << 16);
439 tmp_addr++;
440 *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF0000) << 8);
441 return 0;
442 case check_texture_addr2:
443 cur_seq->unfinished = tex_address;
444 cur_seq->tex_level_lo[tmp = cur_seq->texture] = cmd & 0x3F;
445 cur_seq->tex_level_hi[tmp] = (cmd & 0xFC0) >> 6;
446 return 0;
447 case check_texture_addr3:
448 cur_seq->unfinished = tex_address;
Thomas Hellstrom9b8d9d02007-01-08 21:21:41 +1100449 tmp = ((cmd >> 24) - HC_SubA_HTXnL0Pit);
450 if (tmp == 0 &&
451 (cmd & HC_HTXnEnPit_MASK)) {
452 cur_seq->pitch[cur_seq->texture][tmp] =
453 (cmd & HC_HTXnLnPit_MASK);
454 cur_seq->tex_npot[cur_seq->texture] = 1;
455 } else {
456 cur_seq->pitch[cur_seq->texture][tmp] =
457 (cmd & HC_HTXnLnPitE_MASK) >> HC_HTXnLnPitE_SHIFT;
458 cur_seq->tex_npot[cur_seq->texture] = 0;
459 if (cmd & 0x000FFFFF) {
460 DRM_ERROR
461 ("Unimplemented texture level 0 pitch mode.\n");
462 return 2;
463 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000464 }
465 return 0;
466 case check_texture_addr4:
467 cur_seq->unfinished = tex_address;
468 tmp_addr = &cur_seq->t_addr[cur_seq->texture][9];
469 *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
470 return 0;
471 case check_texture_addr5:
472 case check_texture_addr6:
473 cur_seq->unfinished = tex_address;
474 /*
475 * Texture width. We don't care since we have the pitch.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 */
Dave Airlie22f579c2005-06-28 22:48:56 +1000477 return 0;
478 case check_texture_addr7:
479 cur_seq->unfinished = tex_address;
480 tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
481 tmp_addr[5] = 1 << ((cmd & 0x00F00000) >> 20);
482 tmp_addr[4] = 1 << ((cmd & 0x000F0000) >> 16);
483 tmp_addr[3] = 1 << ((cmd & 0x0000F000) >> 12);
484 tmp_addr[2] = 1 << ((cmd & 0x00000F00) >> 8);
485 tmp_addr[1] = 1 << ((cmd & 0x000000F0) >> 4);
486 tmp_addr[0] = 1 << (cmd & 0x0000000F);
487 return 0;
488 case check_texture_addr8:
489 cur_seq->unfinished = tex_address;
490 tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
491 tmp_addr[9] = 1 << ((cmd & 0x0000F000) >> 12);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 tmp_addr[8] = 1 << ((cmd & 0x00000F00) >> 8);
Dave Airlie22f579c2005-06-28 22:48:56 +1000493 tmp_addr[7] = 1 << ((cmd & 0x000000F0) >> 4);
494 tmp_addr[6] = 1 << (cmd & 0x0000000F);
495 return 0;
496 case check_texture_addr_mode:
497 cur_seq->unfinished = tex_address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 if (2 == (tmp = cmd & 0x00000003)) {
499 DRM_ERROR
500 ("Attempt to fetch texture from system memory.\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000501 return 2;
502 }
503 cur_seq->agp_texture = (tmp == 3);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 cur_seq->tex_palette_size[cur_seq->texture] =
505 (cmd >> 16) & 0x000000007;
Dave Airlie22f579c2005-06-28 22:48:56 +1000506 return 0;
507 case check_for_vertex_count:
508 cur_seq->vertex_count = cmd & 0x0000FFFF;
509 return 0;
510 case check_number_texunits:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 cur_seq->multitex = (cmd >> 3) & 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000512 return 0;
513 default:
514 DRM_ERROR("Illegal DMA data: 0x%x\n", cmd);
515 return 2;
516 }
517 return 2;
518}
519
Dave Airlie22f579c2005-06-28 22:48:56 +1000520static __inline__ int
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000521via_check_prim_list(uint32_t const **buffer, const uint32_t * buf_end,
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200522 drm_via_state_t *cur_seq)
Dave Airlie22f579c2005-06-28 22:48:56 +1000523{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000524 drm_via_private_t *dev_priv =
525 (drm_via_private_t *) cur_seq->dev->dev_private;
526 uint32_t a_fire, bcmd, dw_count;
Dave Airlie22f579c2005-06-28 22:48:56 +1000527 int ret = 0;
528 int have_fire;
529 const uint32_t *buf = *buffer;
530
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 while (buf < buf_end) {
532 have_fire = 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000533 if ((buf_end - buf) < 2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 DRM_ERROR
535 ("Unexpected termination of primitive list.\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000536 ret = 1;
537 break;
538 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdB)
540 break;
Dave Airlie22f579c2005-06-28 22:48:56 +1000541 bcmd = *buf++;
542 if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdA) {
543 DRM_ERROR("Expected Vertex List A command, got 0x%x\n",
544 *buf);
545 ret = 1;
546 break;
547 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 a_fire =
549 *buf++ | HC_HPLEND_MASK | HC_HPMValidN_MASK |
550 HC_HE3Fire_MASK;
551
Dave Airlie22f579c2005-06-28 22:48:56 +1000552 /*
553 * How many dwords per vertex ?
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 */
555
Dave Airlie22f579c2005-06-28 22:48:56 +1000556 if (cur_seq->agp && ((bcmd & (0xF << 11)) == 0)) {
557 DRM_ERROR("Illegal B command vertex data for AGP.\n");
558 ret = 1;
559 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000561
562 dw_count = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 if (bcmd & (1 << 7))
564 dw_count += (cur_seq->multitex) ? 2 : 1;
565 if (bcmd & (1 << 8))
566 dw_count += (cur_seq->multitex) ? 2 : 1;
567 if (bcmd & (1 << 9))
568 dw_count++;
569 if (bcmd & (1 << 10))
570 dw_count++;
571 if (bcmd & (1 << 11))
572 dw_count++;
573 if (bcmd & (1 << 12))
574 dw_count++;
575 if (bcmd & (1 << 13))
576 dw_count++;
577 if (bcmd & (1 << 14))
578 dw_count++;
Dave Airlie22f579c2005-06-28 22:48:56 +1000579
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 while (buf < buf_end) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000581 if (*buf == a_fire) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 if (dev_priv->num_fire_offsets >=
583 VIA_FIRE_BUF_SIZE) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000584 DRM_ERROR("Fire offset buffer full.\n");
585 ret = 1;
586 break;
587 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 dev_priv->fire_offsets[dev_priv->
589 num_fire_offsets++] =
590 buf;
591 have_fire = 1;
Dave Airlie22f579c2005-06-28 22:48:56 +1000592 buf++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 if (buf < buf_end && *buf == a_fire)
Dave Airlie22f579c2005-06-28 22:48:56 +1000594 buf++;
595 break;
596 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 if ((*buf == HALCYON_HEADER2) ||
Dave Airlie22f579c2005-06-28 22:48:56 +1000598 ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD)) {
599 DRM_ERROR("Missing Vertex Fire command, "
600 "Stray Vertex Fire command or verifier "
601 "lost sync.\n");
602 ret = 1;
603 break;
604 }
605 if ((ret = eat_words(&buf, buf_end, dw_count)))
606 break;
607 }
608 if (buf >= buf_end && !have_fire) {
609 DRM_ERROR("Missing Vertex Fire command or verifier "
610 "lost sync.\n");
611 ret = 1;
612 break;
613 }
614 if (cur_seq->agp && ((buf - cur_seq->buf_start) & 0x01)) {
615 DRM_ERROR("AGP Primitive list end misaligned.\n");
616 ret = 1;
617 break;
618 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000620 *buffer = buf;
621 return ret;
622}
623
Dave Airlie22f579c2005-06-28 22:48:56 +1000624static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200625via_check_header2(uint32_t const **buffer, const uint32_t *buf_end,
626 drm_via_state_t *hc_state)
Dave Airlie22f579c2005-06-28 22:48:56 +1000627{
628 uint32_t cmd;
629 int hz_mode;
630 hazard_t hz;
631 const uint32_t *buf = *buffer;
632 const hazard_t *hz_table;
633
Dave Airlie22f579c2005-06-28 22:48:56 +1000634 if ((buf_end - buf) < 2) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 DRM_ERROR
636 ("Illegal termination of DMA HALCYON_HEADER2 sequence.\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000637 return state_error;
638 }
639 buf++;
640 cmd = (*buf++ & 0xFFFF0000) >> 16;
641
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 switch (cmd) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000643 case HC_ParaType_CmdVdata:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 if (via_check_prim_list(&buf, buf_end, hc_state))
Dave Airlie22f579c2005-06-28 22:48:56 +1000645 return state_error;
646 *buffer = buf;
647 return state_command;
648 case HC_ParaType_NotTex:
649 hz_table = table1;
650 break;
651 case HC_ParaType_Tex:
652 hc_state->texture = 0;
653 hz_table = table2;
654 break;
655 case (HC_ParaType_Tex | (HC_SubType_Tex1 << 8)):
656 hc_state->texture = 1;
657 hz_table = table2;
658 break;
659 case (HC_ParaType_Tex | (HC_SubType_TexGeneral << 8)):
660 hz_table = table3;
661 break;
662 case HC_ParaType_Auto:
663 if (eat_words(&buf, buf_end, 2))
664 return state_error;
665 *buffer = buf;
666 return state_command;
667 case (HC_ParaType_Palette | (HC_SubType_Stipple << 8)):
668 if (eat_words(&buf, buf_end, 32))
669 return state_error;
670 *buffer = buf;
671 return state_command;
672 case (HC_ParaType_Palette | (HC_SubType_TexPalette0 << 8)):
673 case (HC_ParaType_Palette | (HC_SubType_TexPalette1 << 8)):
674 DRM_ERROR("Texture palettes are rejected because of "
675 "lack of info how to determine their size.\n");
676 return state_error;
677 case (HC_ParaType_Palette | (HC_SubType_FogTable << 8)):
678 DRM_ERROR("Fog factor palettes are rejected because of "
679 "lack of info how to determine their size.\n");
680 return state_error;
681 default:
682
683 /*
684 * There are some unimplemented HC_ParaTypes here, that
685 * need to be implemented if the Mesa driver is extended.
686 */
687
688 DRM_ERROR("Invalid or unimplemented HALCYON_HEADER2 "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 "DMA subcommand: 0x%x. Previous dword: 0x%x\n",
690 cmd, *(buf - 2));
Dave Airlie22f579c2005-06-28 22:48:56 +1000691 *buffer = buf;
692 return state_error;
693 }
694
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 while (buf < buf_end) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000696 cmd = *buf++;
697 if ((hz = hz_table[cmd >> 24])) {
698 if ((hz_mode = investigate_hazard(cmd, hz, hc_state))) {
699 if (hz_mode == 1) {
700 buf--;
701 break;
702 }
703 return state_error;
704 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000705 } else if (hc_state->unfinished &&
Dave Airlie22f579c2005-06-28 22:48:56 +1000706 finish_current_sequence(hc_state)) {
707 return state_error;
708 }
709 }
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200710 if (hc_state->unfinished && finish_current_sequence(hc_state))
Dave Airlie22f579c2005-06-28 22:48:56 +1000711 return state_error;
Dave Airlie22f579c2005-06-28 22:48:56 +1000712 *buffer = buf;
713 return state_command;
714}
715
716static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200717via_parse_header2(drm_via_private_t *dev_priv, uint32_t const **buffer,
718 const uint32_t *buf_end, int *fire_count)
Dave Airlie22f579c2005-06-28 22:48:56 +1000719{
720 uint32_t cmd;
721 const uint32_t *buf = *buffer;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 const uint32_t *next_fire;
Dave Airlie22f579c2005-06-28 22:48:56 +1000723 int burst = 0;
724
725 next_fire = dev_priv->fire_offsets[*fire_count];
726 buf++;
727 cmd = (*buf & 0xFFFF0000) >> 16;
728 VIA_WRITE(HC_REG_TRANS_SET + HC_REG_BASE, *buf++);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 switch (cmd) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000730 case HC_ParaType_CmdVdata:
731 while ((buf < buf_end) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 (*fire_count < dev_priv->num_fire_offsets) &&
733 (*buf & HC_ACMD_MASK) == HC_ACMD_HCmdB) {
734 while (buf <= next_fire) {
735 VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
736 (burst & 63), *buf++);
Dave Airlie22f579c2005-06-28 22:48:56 +1000737 burst += 4;
738 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000739 if ((buf < buf_end)
740 && ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD))
Dave Airlie22f579c2005-06-28 22:48:56 +1000741 buf++;
742
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 if (++(*fire_count) < dev_priv->num_fire_offsets)
Dave Airlie22f579c2005-06-28 22:48:56 +1000744 next_fire = dev_priv->fire_offsets[*fire_count];
745 }
746 break;
747 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 while (buf < buf_end) {
749
750 if (*buf == HC_HEADER2 ||
751 (*buf & HALCYON_HEADER1MASK) == HALCYON_HEADER1 ||
752 (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5 ||
753 (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
754 break;
755
756 VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
757 (burst & 63), *buf++);
758 burst += 4;
Dave Airlie22f579c2005-06-28 22:48:56 +1000759 }
760 }
761 *buffer = buf;
762 return state_command;
763}
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765static __inline__ int verify_mmio_address(uint32_t address)
Dave Airlie22f579c2005-06-28 22:48:56 +1000766{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 if ((address > 0x3FF) && (address < 0xC00)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000768 DRM_ERROR("Invalid VIDEO DMA command. "
769 "Attempt to access 3D- or command burst area.\n");
770 return 1;
771 } else if ((address > 0xCFF) && (address < 0x1300)) {
772 DRM_ERROR("Invalid VIDEO DMA command. "
773 "Attempt to access PCI DMA area.\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 return 1;
775 } else if (address > 0x13FF) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000776 DRM_ERROR("Invalid VIDEO DMA command. "
777 "Attempt to access VGA registers.\n");
778 return 1;
779 }
780 return 0;
781}
782
783static __inline__ int
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784verify_video_tail(uint32_t const **buffer, const uint32_t * buf_end,
785 uint32_t dwords)
Dave Airlie22f579c2005-06-28 22:48:56 +1000786{
787 const uint32_t *buf = *buffer;
788
789 if (buf_end - buf < dwords) {
790 DRM_ERROR("Illegal termination of video command.\n");
791 return 1;
792 }
793 while (dwords--) {
794 if (*buf++) {
795 DRM_ERROR("Illegal video command tail.\n");
796 return 1;
797 }
798 }
799 *buffer = buf;
800 return 0;
801}
Dave Airlie22f579c2005-06-28 22:48:56 +1000802
803static __inline__ verifier_state_t
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804via_check_header1(uint32_t const **buffer, const uint32_t * buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000805{
806 uint32_t cmd;
807 const uint32_t *buf = *buffer;
808 verifier_state_t ret = state_command;
809
810 while (buf < buf_end) {
811 cmd = *buf;
812 if ((cmd > ((0x3FF >> 2) | HALCYON_HEADER1)) &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 (cmd < ((0xC00 >> 2) | HALCYON_HEADER1))) {
814 if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
Dave Airlie22f579c2005-06-28 22:48:56 +1000815 break;
816 DRM_ERROR("Invalid HALCYON_HEADER1 command. "
817 "Attempt to access 3D- or command burst area.\n");
818 ret = state_error;
819 break;
820 } else if (cmd > ((0xCFF >> 2) | HALCYON_HEADER1)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
Dave Airlie22f579c2005-06-28 22:48:56 +1000822 break;
823 DRM_ERROR("Invalid HALCYON_HEADER1 command. "
824 "Attempt to access VGA registers.\n");
825 ret = state_error;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 break;
827 } else {
Dave Airlie22f579c2005-06-28 22:48:56 +1000828 buf += 2;
829 }
830 }
831 *buffer = buf;
832 return ret;
833}
834
835static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200836via_parse_header1(drm_via_private_t *dev_priv, uint32_t const **buffer,
837 const uint32_t *buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000838{
839 register uint32_t cmd;
840 const uint32_t *buf = *buffer;
841
842 while (buf < buf_end) {
843 cmd = *buf;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
845 break;
846 VIA_WRITE((cmd & ~HALCYON_HEADER1MASK) << 2, *++buf);
Dave Airlie22f579c2005-06-28 22:48:56 +1000847 buf++;
848 }
849 *buffer = buf;
850 return state_command;
851}
852
853static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200854via_check_vheader5(uint32_t const **buffer, const uint32_t *buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000855{
856 uint32_t data;
857 const uint32_t *buf = *buffer;
858
859 if (buf_end - buf < 4) {
860 DRM_ERROR("Illegal termination of video header5 command\n");
861 return state_error;
862 }
863
864 data = *buf++ & ~VIA_VIDEOMASK;
865 if (verify_mmio_address(data))
866 return state_error;
867
868 data = *buf++;
869 if (*buf++ != 0x00F50000) {
870 DRM_ERROR("Illegal header5 header data\n");
871 return state_error;
872 }
873 if (*buf++ != 0x00000000) {
874 DRM_ERROR("Illegal header5 header data\n");
875 return state_error;
876 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 if (eat_words(&buf, buf_end, data))
Dave Airlie22f579c2005-06-28 22:48:56 +1000878 return state_error;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
Dave Airlie22f579c2005-06-28 22:48:56 +1000880 return state_error;
881 *buffer = buf;
882 return state_command;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883
884}
Dave Airlie22f579c2005-06-28 22:48:56 +1000885
886static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200887via_parse_vheader5(drm_via_private_t *dev_priv, uint32_t const **buffer,
888 const uint32_t *buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000889{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 uint32_t addr, count, i;
Dave Airlie22f579c2005-06-28 22:48:56 +1000891 const uint32_t *buf = *buffer;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892
Dave Airlie22f579c2005-06-28 22:48:56 +1000893 addr = *buf++ & ~VIA_VIDEOMASK;
894 i = count = *buf;
895 buf += 3;
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200896 while (i--)
Dave Airlie22f579c2005-06-28 22:48:56 +1000897 VIA_WRITE(addr, *buf++);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 if (count & 3)
899 buf += 4 - (count & 3);
Dave Airlie22f579c2005-06-28 22:48:56 +1000900 *buffer = buf;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 return state_command;
902}
Dave Airlie22f579c2005-06-28 22:48:56 +1000903
904static __inline__ verifier_state_t
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905via_check_vheader6(uint32_t const **buffer, const uint32_t * buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000906{
907 uint32_t data;
908 const uint32_t *buf = *buffer;
909 uint32_t i;
910
Dave Airlie22f579c2005-06-28 22:48:56 +1000911 if (buf_end - buf < 4) {
912 DRM_ERROR("Illegal termination of video header6 command\n");
913 return state_error;
914 }
915 buf++;
916 data = *buf++;
917 if (*buf++ != 0x00F60000) {
918 DRM_ERROR("Illegal header6 header data\n");
919 return state_error;
920 }
921 if (*buf++ != 0x00000000) {
922 DRM_ERROR("Illegal header6 header data\n");
923 return state_error;
924 }
925 if ((buf_end - buf) < (data << 1)) {
926 DRM_ERROR("Illegal termination of video header6 command\n");
927 return state_error;
928 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 for (i = 0; i < data; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000930 if (verify_mmio_address(*buf++))
931 return state_error;
932 buf++;
933 }
934 data <<= 1;
935 if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
936 return state_error;
937 *buffer = buf;
938 return state_command;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939}
Dave Airlie22f579c2005-06-28 22:48:56 +1000940
941static __inline__ verifier_state_t
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200942via_parse_vheader6(drm_via_private_t *dev_priv, uint32_t const **buffer,
943 const uint32_t *buf_end)
Dave Airlie22f579c2005-06-28 22:48:56 +1000944{
945
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000946 uint32_t addr, count, i;
Dave Airlie22f579c2005-06-28 22:48:56 +1000947 const uint32_t *buf = *buffer;
948
949 i = count = *++buf;
950 buf += 3;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 while (i--) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000952 addr = *buf++;
953 VIA_WRITE(addr, *buf++);
954 }
955 count <<= 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000956 if (count & 3)
957 buf += 4 - (count & 3);
Dave Airlie22f579c2005-06-28 22:48:56 +1000958 *buffer = buf;
959 return state_command;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960}
Dave Airlie22f579c2005-06-28 22:48:56 +1000961
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000962int
963via_verify_command_stream(const uint32_t * buf, unsigned int size,
Dave Airlie84b1fd12007-07-11 15:53:27 +1000964 struct drm_device * dev, int agp)
Dave Airlie22f579c2005-06-28 22:48:56 +1000965{
966
967 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
968 drm_via_state_t *hc_state = &dev_priv->hc_state;
969 drm_via_state_t saved_state = *hc_state;
970 uint32_t cmd;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 const uint32_t *buf_end = buf + (size >> 2);
Dave Airlie22f579c2005-06-28 22:48:56 +1000972 verifier_state_t state = state_command;
Thomas Hellstrom689692e2007-01-08 21:19:57 +1100973 int cme_video;
974 int supported_3d;
975
976 cme_video = (dev_priv->chipset == VIA_PRO_GROUP_A ||
977 dev_priv->chipset == VIA_DX9_0);
978
979 supported_3d = dev_priv->chipset != VIA_DX9_0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000980
Dave Airlie22f579c2005-06-28 22:48:56 +1000981 hc_state->dev = dev;
982 hc_state->unfinished = no_sequence;
983 hc_state->map_cache = NULL;
984 hc_state->agp = agp;
985 hc_state->buf_start = buf;
986 dev_priv->num_fire_offsets = 0;
987
988 while (buf < buf_end) {
989
990 switch (state) {
991 case state_header2:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 state = via_check_header2(&buf, buf_end, hc_state);
Dave Airlie22f579c2005-06-28 22:48:56 +1000993 break;
994 case state_header1:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 state = via_check_header1(&buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +1000996 break;
997 case state_vheader5:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000998 state = via_check_vheader5(&buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +1000999 break;
1000 case state_vheader6:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 state = via_check_vheader6(&buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +10001002 break;
1003 case state_command:
Thomas Hellstrom689692e2007-01-08 21:19:57 +11001004 if ((HALCYON_HEADER2 == (cmd = *buf)) &&
1005 supported_3d)
Dave Airlie22f579c2005-06-28 22:48:56 +10001006 state = state_header2;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
Dave Airlie22f579c2005-06-28 22:48:56 +10001008 state = state_header1;
Thomas Hellstrom689692e2007-01-08 21:19:57 +11001009 else if (cme_video
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001010 && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
Dave Airlie22f579c2005-06-28 22:48:56 +10001011 state = state_vheader5;
Thomas Hellstrom689692e2007-01-08 21:19:57 +11001012 else if (cme_video
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001013 && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
Dave Airlie22f579c2005-06-28 22:48:56 +10001014 state = state_vheader6;
Thomas Hellstrom689692e2007-01-08 21:19:57 +11001015 else if ((cmd == HALCYON_HEADER2) && !supported_3d) {
1016 DRM_ERROR("Accelerated 3D is not supported on this chipset yet.\n");
1017 state = state_error;
1018 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 DRM_ERROR
1020 ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
1021 cmd);
Dave Airlie22f579c2005-06-28 22:48:56 +10001022 state = state_error;
1023 }
1024 break;
1025 case state_error:
1026 default:
1027 *hc_state = saved_state;
Eric Anholt20caafa2007-08-25 19:22:43 +10001028 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +10001029 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 }
Dave Airlie22f579c2005-06-28 22:48:56 +10001031 if (state == state_error) {
1032 *hc_state = saved_state;
Eric Anholt20caafa2007-08-25 19:22:43 +10001033 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +10001034 }
1035 return 0;
1036}
1037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038int
Nicolas Kaiser58c1e852010-07-11 15:32:42 +02001039via_parse_command_stream(struct drm_device *dev, const uint32_t *buf,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 unsigned int size)
Dave Airlie22f579c2005-06-28 22:48:56 +10001041{
1042
1043 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
1044 uint32_t cmd;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 const uint32_t *buf_end = buf + (size >> 2);
Dave Airlie22f579c2005-06-28 22:48:56 +10001046 verifier_state_t state = state_command;
1047 int fire_count = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048
Dave Airlie22f579c2005-06-28 22:48:56 +10001049 while (buf < buf_end) {
1050
1051 switch (state) {
1052 case state_header2:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 state =
1054 via_parse_header2(dev_priv, &buf, buf_end,
1055 &fire_count);
Dave Airlie22f579c2005-06-28 22:48:56 +10001056 break;
1057 case state_header1:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 state = via_parse_header1(dev_priv, &buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +10001059 break;
1060 case state_vheader5:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 state = via_parse_vheader5(dev_priv, &buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +10001062 break;
1063 case state_vheader6:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 state = via_parse_vheader6(dev_priv, &buf, buf_end);
Dave Airlie22f579c2005-06-28 22:48:56 +10001065 break;
1066 case state_command:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 if (HALCYON_HEADER2 == (cmd = *buf))
Dave Airlie22f579c2005-06-28 22:48:56 +10001068 state = state_header2;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
Dave Airlie22f579c2005-06-28 22:48:56 +10001070 state = state_header1;
1071 else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
1072 state = state_vheader5;
1073 else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
1074 state = state_vheader6;
1075 else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 DRM_ERROR
1077 ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
1078 cmd);
Dave Airlie22f579c2005-06-28 22:48:56 +10001079 state = state_error;
1080 }
1081 break;
1082 case state_error:
1083 default:
Eric Anholt20caafa2007-08-25 19:22:43 +10001084 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +10001085 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 }
Nicolas Kaiser58c1e852010-07-11 15:32:42 +02001087 if (state == state_error)
Eric Anholt20caafa2007-08-25 19:22:43 +10001088 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +10001089 return 0;
1090}
1091
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092static void
Dave Airlie22f579c2005-06-28 22:48:56 +10001093setup_hazard_table(hz_init_t init_table[], hazard_t table[], int size)
1094{
1095 int i;
1096
Nicolas Kaiser58c1e852010-07-11 15:32:42 +02001097 for (i = 0; i < 256; ++i)
Dave Airlie22f579c2005-06-28 22:48:56 +10001098 table[i] = forbidden_command;
Dave Airlie22f579c2005-06-28 22:48:56 +10001099
Nicolas Kaiser58c1e852010-07-11 15:32:42 +02001100 for (i = 0; i < size; ++i)
Dave Airlie22f579c2005-06-28 22:48:56 +10001101 table[init_table[i].code] = init_table[i].hz;
Dave Airlie22f579c2005-06-28 22:48:56 +10001102}
1103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104void via_init_command_verifier(void)
Dave Airlie22f579c2005-06-28 22:48:56 +10001105{
Jérémy Lefaure2e20c9d2017-10-15 22:33:56 -04001106 setup_hazard_table(init_table1, table1, ARRAY_SIZE(init_table1));
1107 setup_hazard_table(init_table2, table2, ARRAY_SIZE(init_table2));
1108 setup_hazard_table(init_table3, table3, ARRAY_SIZE(init_table3));
Dave Airlie22f579c2005-06-28 22:48:56 +10001109}