via_verifier.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  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. #include "via_3d_reg.h"
  31. #include <drm/drmP.h>
  32. #include <drm/via_drm.h>
  33. #include <drm/drm_legacy.h>
  34. #include "via_verifier.h"
  35. #include "via_drv.h"
  36. typedef enum {
  37. state_command,
  38. state_header2,
  39. state_header1,
  40. state_vheader5,
  41. state_vheader6,
  42. state_error
  43. } verifier_state_t;
  44. typedef enum {
  45. no_check = 0,
  46. check_for_header2,
  47. check_for_header1,
  48. check_for_header2_err,
  49. check_for_header1_err,
  50. check_for_fire,
  51. check_z_buffer_addr0,
  52. check_z_buffer_addr1,
  53. check_z_buffer_addr_mode,
  54. check_destination_addr0,
  55. check_destination_addr1,
  56. check_destination_addr_mode,
  57. check_for_dummy,
  58. check_for_dd,
  59. check_texture_addr0,
  60. check_texture_addr1,
  61. check_texture_addr2,
  62. check_texture_addr3,
  63. check_texture_addr4,
  64. check_texture_addr5,
  65. check_texture_addr6,
  66. check_texture_addr7,
  67. check_texture_addr8,
  68. check_texture_addr_mode,
  69. check_for_vertex_count,
  70. check_number_texunits,
  71. forbidden_command
  72. } hazard_t;
  73. /*
  74. * Associates each hazard above with a possible multi-command
  75. * sequence. For example an address that is split over multiple
  76. * commands and that needs to be checked at the first command
  77. * that does not include any part of the address.
  78. */
  79. static drm_via_sequence_t seqs[] = {
  80. no_sequence,
  81. no_sequence,
  82. no_sequence,
  83. no_sequence,
  84. no_sequence,
  85. no_sequence,
  86. z_address,
  87. z_address,
  88. z_address,
  89. dest_address,
  90. dest_address,
  91. dest_address,
  92. no_sequence,
  93. no_sequence,
  94. tex_address,
  95. tex_address,
  96. tex_address,
  97. tex_address,
  98. tex_address,
  99. tex_address,
  100. tex_address,
  101. tex_address,
  102. tex_address,
  103. tex_address,
  104. no_sequence
  105. };
  106. typedef struct {
  107. unsigned int code;
  108. hazard_t hz;
  109. } hz_init_t;
  110. static hz_init_t init_table1[] = {
  111. {0xf2, check_for_header2_err},
  112. {0xf0, check_for_header1_err},
  113. {0xee, check_for_fire},
  114. {0xcc, check_for_dummy},
  115. {0xdd, check_for_dd},
  116. {0x00, no_check},
  117. {0x10, check_z_buffer_addr0},
  118. {0x11, check_z_buffer_addr1},
  119. {0x12, check_z_buffer_addr_mode},
  120. {0x13, no_check},
  121. {0x14, no_check},
  122. {0x15, no_check},
  123. {0x23, no_check},
  124. {0x24, no_check},
  125. {0x33, no_check},
  126. {0x34, no_check},
  127. {0x35, no_check},
  128. {0x36, no_check},
  129. {0x37, no_check},
  130. {0x38, no_check},
  131. {0x39, no_check},
  132. {0x3A, no_check},
  133. {0x3B, no_check},
  134. {0x3C, no_check},
  135. {0x3D, no_check},
  136. {0x3E, no_check},
  137. {0x40, check_destination_addr0},
  138. {0x41, check_destination_addr1},
  139. {0x42, check_destination_addr_mode},
  140. {0x43, no_check},
  141. {0x44, no_check},
  142. {0x50, no_check},
  143. {0x51, no_check},
  144. {0x52, no_check},
  145. {0x53, no_check},
  146. {0x54, no_check},
  147. {0x55, no_check},
  148. {0x56, no_check},
  149. {0x57, no_check},
  150. {0x58, no_check},
  151. {0x70, no_check},
  152. {0x71, no_check},
  153. {0x78, no_check},
  154. {0x79, no_check},
  155. {0x7A, no_check},
  156. {0x7B, no_check},
  157. {0x7C, no_check},
  158. {0x7D, check_for_vertex_count}
  159. };
  160. static hz_init_t init_table2[] = {
  161. {0xf2, check_for_header2_err},
  162. {0xf0, check_for_header1_err},
  163. {0xee, check_for_fire},
  164. {0xcc, check_for_dummy},
  165. {0x00, check_texture_addr0},
  166. {0x01, check_texture_addr0},
  167. {0x02, check_texture_addr0},
  168. {0x03, check_texture_addr0},
  169. {0x04, check_texture_addr0},
  170. {0x05, check_texture_addr0},
  171. {0x06, check_texture_addr0},
  172. {0x07, check_texture_addr0},
  173. {0x08, check_texture_addr0},
  174. {0x09, check_texture_addr0},
  175. {0x20, check_texture_addr1},
  176. {0x21, check_texture_addr1},
  177. {0x22, check_texture_addr1},
  178. {0x23, check_texture_addr4},
  179. {0x2B, check_texture_addr3},
  180. {0x2C, check_texture_addr3},
  181. {0x2D, check_texture_addr3},
  182. {0x2E, check_texture_addr3},
  183. {0x2F, check_texture_addr3},
  184. {0x30, check_texture_addr3},
  185. {0x31, check_texture_addr3},
  186. {0x32, check_texture_addr3},
  187. {0x33, check_texture_addr3},
  188. {0x34, check_texture_addr3},
  189. {0x4B, check_texture_addr5},
  190. {0x4C, check_texture_addr6},
  191. {0x51, check_texture_addr7},
  192. {0x52, check_texture_addr8},
  193. {0x77, check_texture_addr2},
  194. {0x78, no_check},
  195. {0x79, no_check},
  196. {0x7A, no_check},
  197. {0x7B, check_texture_addr_mode},
  198. {0x7C, no_check},
  199. {0x7D, no_check},
  200. {0x7E, no_check},
  201. {0x7F, no_check},
  202. {0x80, no_check},
  203. {0x81, no_check},
  204. {0x82, no_check},
  205. {0x83, no_check},
  206. {0x85, no_check},
  207. {0x86, no_check},
  208. {0x87, no_check},
  209. {0x88, no_check},
  210. {0x89, no_check},
  211. {0x8A, no_check},
  212. {0x90, no_check},
  213. {0x91, no_check},
  214. {0x92, no_check},
  215. {0x93, no_check}
  216. };
  217. static hz_init_t init_table3[] = {
  218. {0xf2, check_for_header2_err},
  219. {0xf0, check_for_header1_err},
  220. {0xcc, check_for_dummy},
  221. {0x00, check_number_texunits}
  222. };
  223. static hazard_t table1[256];
  224. static hazard_t table2[256];
  225. static hazard_t table3[256];
  226. static __inline__ int
  227. eat_words(const uint32_t **buf, const uint32_t *buf_end, unsigned num_words)
  228. {
  229. if ((buf_end - *buf) >= num_words) {
  230. *buf += num_words;
  231. return 0;
  232. }
  233. DRM_ERROR("Illegal termination of DMA command buffer\n");
  234. return 1;
  235. }
  236. /*
  237. * Partially stolen from drm_memory.h
  238. */
  239. static __inline__ drm_local_map_t *via_drm_lookup_agp_map(drm_via_state_t *seq,
  240. unsigned long offset,
  241. unsigned long size,
  242. struct drm_device *dev)
  243. {
  244. struct drm_map_list *r_list;
  245. drm_local_map_t *map = seq->map_cache;
  246. if (map && map->offset <= offset
  247. && (offset + size) <= (map->offset + map->size)) {
  248. return map;
  249. }
  250. list_for_each_entry(r_list, &dev->maplist, head) {
  251. map = r_list->map;
  252. if (!map)
  253. continue;
  254. if (map->offset <= offset
  255. && (offset + size) <= (map->offset + map->size)
  256. && !(map->flags & _DRM_RESTRICTED)
  257. && (map->type == _DRM_AGP)) {
  258. seq->map_cache = map;
  259. return map;
  260. }
  261. }
  262. return NULL;
  263. }
  264. /*
  265. * Require that all AGP texture levels reside in the same AGP map which should
  266. * be mappable by the client. This is not a big restriction.
  267. * FIXME: To actually enforce this security policy strictly, drm_rmmap
  268. * would have to wait for dma quiescent before removing an AGP map.
  269. * The via_drm_lookup_agp_map call in reality seems to take
  270. * very little CPU time.
  271. */
  272. static __inline__ int finish_current_sequence(drm_via_state_t * cur_seq)
  273. {
  274. switch (cur_seq->unfinished) {
  275. case z_address:
  276. DRM_DEBUG("Z Buffer start address is 0x%x\n", cur_seq->z_addr);
  277. break;
  278. case dest_address:
  279. DRM_DEBUG("Destination start address is 0x%x\n",
  280. cur_seq->d_addr);
  281. break;
  282. case tex_address:
  283. if (cur_seq->agp_texture) {
  284. unsigned start =
  285. cur_seq->tex_level_lo[cur_seq->texture];
  286. unsigned end = cur_seq->tex_level_hi[cur_seq->texture];
  287. unsigned long lo = ~0, hi = 0, tmp;
  288. uint32_t *addr, *pitch, *height, tex;
  289. unsigned i;
  290. int npot;
  291. if (end > 9)
  292. end = 9;
  293. if (start > 9)
  294. start = 9;
  295. addr =
  296. &(cur_seq->t_addr[tex = cur_seq->texture][start]);
  297. pitch = &(cur_seq->pitch[tex][start]);
  298. height = &(cur_seq->height[tex][start]);
  299. npot = cur_seq->tex_npot[tex];
  300. for (i = start; i <= end; ++i) {
  301. tmp = *addr++;
  302. if (tmp < lo)
  303. lo = tmp;
  304. if (i == 0 && npot)
  305. tmp += (*height++ * *pitch++);
  306. else
  307. tmp += (*height++ << *pitch++);
  308. if (tmp > hi)
  309. hi = tmp;
  310. }
  311. if (!via_drm_lookup_agp_map
  312. (cur_seq, lo, hi - lo, cur_seq->dev)) {
  313. DRM_ERROR
  314. ("AGP texture is not in allowed map\n");
  315. return 2;
  316. }
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322. cur_seq->unfinished = no_sequence;
  323. return 0;
  324. }
  325. static __inline__ int
  326. investigate_hazard(uint32_t cmd, hazard_t hz, drm_via_state_t *cur_seq)
  327. {
  328. register uint32_t tmp, *tmp_addr;
  329. if (cur_seq->unfinished && (cur_seq->unfinished != seqs[hz])) {
  330. int ret;
  331. if ((ret = finish_current_sequence(cur_seq)))
  332. return ret;
  333. }
  334. switch (hz) {
  335. case check_for_header2:
  336. if (cmd == HALCYON_HEADER2)
  337. return 1;
  338. return 0;
  339. case check_for_header1:
  340. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  341. return 1;
  342. return 0;
  343. case check_for_header2_err:
  344. if (cmd == HALCYON_HEADER2)
  345. return 1;
  346. DRM_ERROR("Illegal DMA HALCYON_HEADER2 command\n");
  347. break;
  348. case check_for_header1_err:
  349. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  350. return 1;
  351. DRM_ERROR("Illegal DMA HALCYON_HEADER1 command\n");
  352. break;
  353. case check_for_fire:
  354. if ((cmd & HALCYON_FIREMASK) == HALCYON_FIRECMD)
  355. return 1;
  356. DRM_ERROR("Illegal DMA HALCYON_FIRECMD command\n");
  357. break;
  358. case check_for_dummy:
  359. if (HC_DUMMY == cmd)
  360. return 0;
  361. DRM_ERROR("Illegal DMA HC_DUMMY command\n");
  362. break;
  363. case check_for_dd:
  364. if (0xdddddddd == cmd)
  365. return 0;
  366. DRM_ERROR("Illegal DMA 0xdddddddd command\n");
  367. break;
  368. case check_z_buffer_addr0:
  369. cur_seq->unfinished = z_address;
  370. cur_seq->z_addr = (cur_seq->z_addr & 0xFF000000) |
  371. (cmd & 0x00FFFFFF);
  372. return 0;
  373. case check_z_buffer_addr1:
  374. cur_seq->unfinished = z_address;
  375. cur_seq->z_addr = (cur_seq->z_addr & 0x00FFFFFF) |
  376. ((cmd & 0xFF) << 24);
  377. return 0;
  378. case check_z_buffer_addr_mode:
  379. cur_seq->unfinished = z_address;
  380. if ((cmd & 0x0000C000) == 0)
  381. return 0;
  382. DRM_ERROR("Attempt to place Z buffer in system memory\n");
  383. return 2;
  384. case check_destination_addr0:
  385. cur_seq->unfinished = dest_address;
  386. cur_seq->d_addr = (cur_seq->d_addr & 0xFF000000) |
  387. (cmd & 0x00FFFFFF);
  388. return 0;
  389. case check_destination_addr1:
  390. cur_seq->unfinished = dest_address;
  391. cur_seq->d_addr = (cur_seq->d_addr & 0x00FFFFFF) |
  392. ((cmd & 0xFF) << 24);
  393. return 0;
  394. case check_destination_addr_mode:
  395. cur_seq->unfinished = dest_address;
  396. if ((cmd & 0x0000C000) == 0)
  397. return 0;
  398. DRM_ERROR
  399. ("Attempt to place 3D drawing buffer in system memory\n");
  400. return 2;
  401. case check_texture_addr0:
  402. cur_seq->unfinished = tex_address;
  403. tmp = (cmd >> 24);
  404. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  405. *tmp_addr = (*tmp_addr & 0xFF000000) | (cmd & 0x00FFFFFF);
  406. return 0;
  407. case check_texture_addr1:
  408. cur_seq->unfinished = tex_address;
  409. tmp = ((cmd >> 24) - 0x20);
  410. tmp += tmp << 1;
  411. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  412. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  413. tmp_addr++;
  414. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF00) << 16);
  415. tmp_addr++;
  416. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF0000) << 8);
  417. return 0;
  418. case check_texture_addr2:
  419. cur_seq->unfinished = tex_address;
  420. cur_seq->tex_level_lo[tmp = cur_seq->texture] = cmd & 0x3F;
  421. cur_seq->tex_level_hi[tmp] = (cmd & 0xFC0) >> 6;
  422. return 0;
  423. case check_texture_addr3:
  424. cur_seq->unfinished = tex_address;
  425. tmp = ((cmd >> 24) - HC_SubA_HTXnL0Pit);
  426. if (tmp == 0 &&
  427. (cmd & HC_HTXnEnPit_MASK)) {
  428. cur_seq->pitch[cur_seq->texture][tmp] =
  429. (cmd & HC_HTXnLnPit_MASK);
  430. cur_seq->tex_npot[cur_seq->texture] = 1;
  431. } else {
  432. cur_seq->pitch[cur_seq->texture][tmp] =
  433. (cmd & HC_HTXnLnPitE_MASK) >> HC_HTXnLnPitE_SHIFT;
  434. cur_seq->tex_npot[cur_seq->texture] = 0;
  435. if (cmd & 0x000FFFFF) {
  436. DRM_ERROR
  437. ("Unimplemented texture level 0 pitch mode.\n");
  438. return 2;
  439. }
  440. }
  441. return 0;
  442. case check_texture_addr4:
  443. cur_seq->unfinished = tex_address;
  444. tmp_addr = &cur_seq->t_addr[cur_seq->texture][9];
  445. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  446. return 0;
  447. case check_texture_addr5:
  448. case check_texture_addr6:
  449. cur_seq->unfinished = tex_address;
  450. /*
  451. * Texture width. We don't care since we have the pitch.
  452. */
  453. return 0;
  454. case check_texture_addr7:
  455. cur_seq->unfinished = tex_address;
  456. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  457. tmp_addr[5] = 1 << ((cmd & 0x00F00000) >> 20);
  458. tmp_addr[4] = 1 << ((cmd & 0x000F0000) >> 16);
  459. tmp_addr[3] = 1 << ((cmd & 0x0000F000) >> 12);
  460. tmp_addr[2] = 1 << ((cmd & 0x00000F00) >> 8);
  461. tmp_addr[1] = 1 << ((cmd & 0x000000F0) >> 4);
  462. tmp_addr[0] = 1 << (cmd & 0x0000000F);
  463. return 0;
  464. case check_texture_addr8:
  465. cur_seq->unfinished = tex_address;
  466. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  467. tmp_addr[9] = 1 << ((cmd & 0x0000F000) >> 12);
  468. tmp_addr[8] = 1 << ((cmd & 0x00000F00) >> 8);
  469. tmp_addr[7] = 1 << ((cmd & 0x000000F0) >> 4);
  470. tmp_addr[6] = 1 << (cmd & 0x0000000F);
  471. return 0;
  472. case check_texture_addr_mode:
  473. cur_seq->unfinished = tex_address;
  474. if (2 == (tmp = cmd & 0x00000003)) {
  475. DRM_ERROR
  476. ("Attempt to fetch texture from system memory.\n");
  477. return 2;
  478. }
  479. cur_seq->agp_texture = (tmp == 3);
  480. cur_seq->tex_palette_size[cur_seq->texture] =
  481. (cmd >> 16) & 0x000000007;
  482. return 0;
  483. case check_for_vertex_count:
  484. cur_seq->vertex_count = cmd & 0x0000FFFF;
  485. return 0;
  486. case check_number_texunits:
  487. cur_seq->multitex = (cmd >> 3) & 1;
  488. return 0;
  489. default:
  490. DRM_ERROR("Illegal DMA data: 0x%x\n", cmd);
  491. return 2;
  492. }
  493. return 2;
  494. }
  495. static __inline__ int
  496. via_check_prim_list(uint32_t const **buffer, const uint32_t * buf_end,
  497. drm_via_state_t *cur_seq)
  498. {
  499. drm_via_private_t *dev_priv =
  500. (drm_via_private_t *) cur_seq->dev->dev_private;
  501. uint32_t a_fire, bcmd, dw_count;
  502. int ret = 0;
  503. int have_fire;
  504. const uint32_t *buf = *buffer;
  505. while (buf < buf_end) {
  506. have_fire = 0;
  507. if ((buf_end - buf) < 2) {
  508. DRM_ERROR
  509. ("Unexpected termination of primitive list.\n");
  510. ret = 1;
  511. break;
  512. }
  513. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdB)
  514. break;
  515. bcmd = *buf++;
  516. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdA) {
  517. DRM_ERROR("Expected Vertex List A command, got 0x%x\n",
  518. *buf);
  519. ret = 1;
  520. break;
  521. }
  522. a_fire =
  523. *buf++ | HC_HPLEND_MASK | HC_HPMValidN_MASK |
  524. HC_HE3Fire_MASK;
  525. /*
  526. * How many dwords per vertex ?
  527. */
  528. if (cur_seq->agp && ((bcmd & (0xF << 11)) == 0)) {
  529. DRM_ERROR("Illegal B command vertex data for AGP.\n");
  530. ret = 1;
  531. break;
  532. }
  533. dw_count = 0;
  534. if (bcmd & (1 << 7))
  535. dw_count += (cur_seq->multitex) ? 2 : 1;
  536. if (bcmd & (1 << 8))
  537. dw_count += (cur_seq->multitex) ? 2 : 1;
  538. if (bcmd & (1 << 9))
  539. dw_count++;
  540. if (bcmd & (1 << 10))
  541. dw_count++;
  542. if (bcmd & (1 << 11))
  543. dw_count++;
  544. if (bcmd & (1 << 12))
  545. dw_count++;
  546. if (bcmd & (1 << 13))
  547. dw_count++;
  548. if (bcmd & (1 << 14))
  549. dw_count++;
  550. while (buf < buf_end) {
  551. if (*buf == a_fire) {
  552. if (dev_priv->num_fire_offsets >=
  553. VIA_FIRE_BUF_SIZE) {
  554. DRM_ERROR("Fire offset buffer full.\n");
  555. ret = 1;
  556. break;
  557. }
  558. dev_priv->fire_offsets[dev_priv->
  559. num_fire_offsets++] =
  560. buf;
  561. have_fire = 1;
  562. buf++;
  563. if (buf < buf_end && *buf == a_fire)
  564. buf++;
  565. break;
  566. }
  567. if ((*buf == HALCYON_HEADER2) ||
  568. ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD)) {
  569. DRM_ERROR("Missing Vertex Fire command, "
  570. "Stray Vertex Fire command or verifier "
  571. "lost sync.\n");
  572. ret = 1;
  573. break;
  574. }
  575. if ((ret = eat_words(&buf, buf_end, dw_count)))
  576. break;
  577. }
  578. if (buf >= buf_end && !have_fire) {
  579. DRM_ERROR("Missing Vertex Fire command or verifier "
  580. "lost sync.\n");
  581. ret = 1;
  582. break;
  583. }
  584. if (cur_seq->agp && ((buf - cur_seq->buf_start) & 0x01)) {
  585. DRM_ERROR("AGP Primitive list end misaligned.\n");
  586. ret = 1;
  587. break;
  588. }
  589. }
  590. *buffer = buf;
  591. return ret;
  592. }
  593. static __inline__ verifier_state_t
  594. via_check_header2(uint32_t const **buffer, const uint32_t *buf_end,
  595. drm_via_state_t *hc_state)
  596. {
  597. uint32_t cmd;
  598. int hz_mode;
  599. hazard_t hz;
  600. const uint32_t *buf = *buffer;
  601. const hazard_t *hz_table;
  602. if ((buf_end - buf) < 2) {
  603. DRM_ERROR
  604. ("Illegal termination of DMA HALCYON_HEADER2 sequence.\n");
  605. return state_error;
  606. }
  607. buf++;
  608. cmd = (*buf++ & 0xFFFF0000) >> 16;
  609. switch (cmd) {
  610. case HC_ParaType_CmdVdata:
  611. if (via_check_prim_list(&buf, buf_end, hc_state))
  612. return state_error;
  613. *buffer = buf;
  614. return state_command;
  615. case HC_ParaType_NotTex:
  616. hz_table = table1;
  617. break;
  618. case HC_ParaType_Tex:
  619. hc_state->texture = 0;
  620. hz_table = table2;
  621. break;
  622. case (HC_ParaType_Tex | (HC_SubType_Tex1 << 8)):
  623. hc_state->texture = 1;
  624. hz_table = table2;
  625. break;
  626. case (HC_ParaType_Tex | (HC_SubType_TexGeneral << 8)):
  627. hz_table = table3;
  628. break;
  629. case HC_ParaType_Auto:
  630. if (eat_words(&buf, buf_end, 2))
  631. return state_error;
  632. *buffer = buf;
  633. return state_command;
  634. case (HC_ParaType_Palette | (HC_SubType_Stipple << 8)):
  635. if (eat_words(&buf, buf_end, 32))
  636. return state_error;
  637. *buffer = buf;
  638. return state_command;
  639. case (HC_ParaType_Palette | (HC_SubType_TexPalette0 << 8)):
  640. case (HC_ParaType_Palette | (HC_SubType_TexPalette1 << 8)):
  641. DRM_ERROR("Texture palettes are rejected because of "
  642. "lack of info how to determine their size.\n");
  643. return state_error;
  644. case (HC_ParaType_Palette | (HC_SubType_FogTable << 8)):
  645. DRM_ERROR("Fog factor palettes are rejected because of "
  646. "lack of info how to determine their size.\n");
  647. return state_error;
  648. default:
  649. /*
  650. * There are some unimplemented HC_ParaTypes here, that
  651. * need to be implemented if the Mesa driver is extended.
  652. */
  653. DRM_ERROR("Invalid or unimplemented HALCYON_HEADER2 "
  654. "DMA subcommand: 0x%x. Previous dword: 0x%x\n",
  655. cmd, *(buf - 2));
  656. *buffer = buf;
  657. return state_error;
  658. }
  659. while (buf < buf_end) {
  660. cmd = *buf++;
  661. if ((hz = hz_table[cmd >> 24])) {
  662. if ((hz_mode = investigate_hazard(cmd, hz, hc_state))) {
  663. if (hz_mode == 1) {
  664. buf--;
  665. break;
  666. }
  667. return state_error;
  668. }
  669. } else if (hc_state->unfinished &&
  670. finish_current_sequence(hc_state)) {
  671. return state_error;
  672. }
  673. }
  674. if (hc_state->unfinished && finish_current_sequence(hc_state))
  675. return state_error;
  676. *buffer = buf;
  677. return state_command;
  678. }
  679. static __inline__ verifier_state_t
  680. via_parse_header2(drm_via_private_t *dev_priv, uint32_t const **buffer,
  681. const uint32_t *buf_end, int *fire_count)
  682. {
  683. uint32_t cmd;
  684. const uint32_t *buf = *buffer;
  685. const uint32_t *next_fire;
  686. int burst = 0;
  687. next_fire = dev_priv->fire_offsets[*fire_count];
  688. buf++;
  689. cmd = (*buf & 0xFFFF0000) >> 16;
  690. VIA_WRITE(HC_REG_TRANS_SET + HC_REG_BASE, *buf++);
  691. switch (cmd) {
  692. case HC_ParaType_CmdVdata:
  693. while ((buf < buf_end) &&
  694. (*fire_count < dev_priv->num_fire_offsets) &&
  695. (*buf & HC_ACMD_MASK) == HC_ACMD_HCmdB) {
  696. while (buf <= next_fire) {
  697. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  698. (burst & 63), *buf++);
  699. burst += 4;
  700. }
  701. if ((buf < buf_end)
  702. && ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD))
  703. buf++;
  704. if (++(*fire_count) < dev_priv->num_fire_offsets)
  705. next_fire = dev_priv->fire_offsets[*fire_count];
  706. }
  707. break;
  708. default:
  709. while (buf < buf_end) {
  710. if (*buf == HC_HEADER2 ||
  711. (*buf & HALCYON_HEADER1MASK) == HALCYON_HEADER1 ||
  712. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5 ||
  713. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  714. break;
  715. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  716. (burst & 63), *buf++);
  717. burst += 4;
  718. }
  719. }
  720. *buffer = buf;
  721. return state_command;
  722. }
  723. static __inline__ int verify_mmio_address(uint32_t address)
  724. {
  725. if ((address > 0x3FF) && (address < 0xC00)) {
  726. DRM_ERROR("Invalid VIDEO DMA command. "
  727. "Attempt to access 3D- or command burst area.\n");
  728. return 1;
  729. } else if ((address > 0xCFF) && (address < 0x1300)) {
  730. DRM_ERROR("Invalid VIDEO DMA command. "
  731. "Attempt to access PCI DMA area.\n");
  732. return 1;
  733. } else if (address > 0x13FF) {
  734. DRM_ERROR("Invalid VIDEO DMA command. "
  735. "Attempt to access VGA registers.\n");
  736. return 1;
  737. }
  738. return 0;
  739. }
  740. static __inline__ int
  741. verify_video_tail(uint32_t const **buffer, const uint32_t * buf_end,
  742. uint32_t dwords)
  743. {
  744. const uint32_t *buf = *buffer;
  745. if (buf_end - buf < dwords) {
  746. DRM_ERROR("Illegal termination of video command.\n");
  747. return 1;
  748. }
  749. while (dwords--) {
  750. if (*buf++) {
  751. DRM_ERROR("Illegal video command tail.\n");
  752. return 1;
  753. }
  754. }
  755. *buffer = buf;
  756. return 0;
  757. }
  758. static __inline__ verifier_state_t
  759. via_check_header1(uint32_t const **buffer, const uint32_t * buf_end)
  760. {
  761. uint32_t cmd;
  762. const uint32_t *buf = *buffer;
  763. verifier_state_t ret = state_command;
  764. while (buf < buf_end) {
  765. cmd = *buf;
  766. if ((cmd > ((0x3FF >> 2) | HALCYON_HEADER1)) &&
  767. (cmd < ((0xC00 >> 2) | HALCYON_HEADER1))) {
  768. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  769. break;
  770. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  771. "Attempt to access 3D- or command burst area.\n");
  772. ret = state_error;
  773. break;
  774. } else if (cmd > ((0xCFF >> 2) | HALCYON_HEADER1)) {
  775. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  776. break;
  777. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  778. "Attempt to access VGA registers.\n");
  779. ret = state_error;
  780. break;
  781. } else {
  782. buf += 2;
  783. }
  784. }
  785. *buffer = buf;
  786. return ret;
  787. }
  788. static __inline__ verifier_state_t
  789. via_parse_header1(drm_via_private_t *dev_priv, uint32_t const **buffer,
  790. const uint32_t *buf_end)
  791. {
  792. register uint32_t cmd;
  793. const uint32_t *buf = *buffer;
  794. while (buf < buf_end) {
  795. cmd = *buf;
  796. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  797. break;
  798. VIA_WRITE((cmd & ~HALCYON_HEADER1MASK) << 2, *++buf);
  799. buf++;
  800. }
  801. *buffer = buf;
  802. return state_command;
  803. }
  804. static __inline__ verifier_state_t
  805. via_check_vheader5(uint32_t const **buffer, const uint32_t *buf_end)
  806. {
  807. uint32_t data;
  808. const uint32_t *buf = *buffer;
  809. if (buf_end - buf < 4) {
  810. DRM_ERROR("Illegal termination of video header5 command\n");
  811. return state_error;
  812. }
  813. data = *buf++ & ~VIA_VIDEOMASK;
  814. if (verify_mmio_address(data))
  815. return state_error;
  816. data = *buf++;
  817. if (*buf++ != 0x00F50000) {
  818. DRM_ERROR("Illegal header5 header data\n");
  819. return state_error;
  820. }
  821. if (*buf++ != 0x00000000) {
  822. DRM_ERROR("Illegal header5 header data\n");
  823. return state_error;
  824. }
  825. if (eat_words(&buf, buf_end, data))
  826. return state_error;
  827. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  828. return state_error;
  829. *buffer = buf;
  830. return state_command;
  831. }
  832. static __inline__ verifier_state_t
  833. via_parse_vheader5(drm_via_private_t *dev_priv, uint32_t const **buffer,
  834. const uint32_t *buf_end)
  835. {
  836. uint32_t addr, count, i;
  837. const uint32_t *buf = *buffer;
  838. addr = *buf++ & ~VIA_VIDEOMASK;
  839. i = count = *buf;
  840. buf += 3;
  841. while (i--)
  842. VIA_WRITE(addr, *buf++);
  843. if (count & 3)
  844. buf += 4 - (count & 3);
  845. *buffer = buf;
  846. return state_command;
  847. }
  848. static __inline__ verifier_state_t
  849. via_check_vheader6(uint32_t const **buffer, const uint32_t * buf_end)
  850. {
  851. uint32_t data;
  852. const uint32_t *buf = *buffer;
  853. uint32_t i;
  854. if (buf_end - buf < 4) {
  855. DRM_ERROR("Illegal termination of video header6 command\n");
  856. return state_error;
  857. }
  858. buf++;
  859. data = *buf++;
  860. if (*buf++ != 0x00F60000) {
  861. DRM_ERROR("Illegal header6 header data\n");
  862. return state_error;
  863. }
  864. if (*buf++ != 0x00000000) {
  865. DRM_ERROR("Illegal header6 header data\n");
  866. return state_error;
  867. }
  868. if ((buf_end - buf) < (data << 1)) {
  869. DRM_ERROR("Illegal termination of video header6 command\n");
  870. return state_error;
  871. }
  872. for (i = 0; i < data; ++i) {
  873. if (verify_mmio_address(*buf++))
  874. return state_error;
  875. buf++;
  876. }
  877. data <<= 1;
  878. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  879. return state_error;
  880. *buffer = buf;
  881. return state_command;
  882. }
  883. static __inline__ verifier_state_t
  884. via_parse_vheader6(drm_via_private_t *dev_priv, uint32_t const **buffer,
  885. const uint32_t *buf_end)
  886. {
  887. uint32_t addr, count, i;
  888. const uint32_t *buf = *buffer;
  889. i = count = *++buf;
  890. buf += 3;
  891. while (i--) {
  892. addr = *buf++;
  893. VIA_WRITE(addr, *buf++);
  894. }
  895. count <<= 1;
  896. if (count & 3)
  897. buf += 4 - (count & 3);
  898. *buffer = buf;
  899. return state_command;
  900. }
  901. int
  902. via_verify_command_stream(const uint32_t * buf, unsigned int size,
  903. struct drm_device * dev, int agp)
  904. {
  905. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  906. drm_via_state_t *hc_state = &dev_priv->hc_state;
  907. drm_via_state_t saved_state = *hc_state;
  908. uint32_t cmd;
  909. const uint32_t *buf_end = buf + (size >> 2);
  910. verifier_state_t state = state_command;
  911. int cme_video;
  912. int supported_3d;
  913. cme_video = (dev_priv->chipset == VIA_PRO_GROUP_A ||
  914. dev_priv->chipset == VIA_DX9_0);
  915. supported_3d = dev_priv->chipset != VIA_DX9_0;
  916. hc_state->dev = dev;
  917. hc_state->unfinished = no_sequence;
  918. hc_state->map_cache = NULL;
  919. hc_state->agp = agp;
  920. hc_state->buf_start = buf;
  921. dev_priv->num_fire_offsets = 0;
  922. while (buf < buf_end) {
  923. switch (state) {
  924. case state_header2:
  925. state = via_check_header2(&buf, buf_end, hc_state);
  926. break;
  927. case state_header1:
  928. state = via_check_header1(&buf, buf_end);
  929. break;
  930. case state_vheader5:
  931. state = via_check_vheader5(&buf, buf_end);
  932. break;
  933. case state_vheader6:
  934. state = via_check_vheader6(&buf, buf_end);
  935. break;
  936. case state_command:
  937. if ((HALCYON_HEADER2 == (cmd = *buf)) &&
  938. supported_3d)
  939. state = state_header2;
  940. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  941. state = state_header1;
  942. else if (cme_video
  943. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  944. state = state_vheader5;
  945. else if (cme_video
  946. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  947. state = state_vheader6;
  948. else if ((cmd == HALCYON_HEADER2) && !supported_3d) {
  949. DRM_ERROR("Accelerated 3D is not supported on this chipset yet.\n");
  950. state = state_error;
  951. } else {
  952. DRM_ERROR
  953. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  954. cmd);
  955. state = state_error;
  956. }
  957. break;
  958. case state_error:
  959. default:
  960. *hc_state = saved_state;
  961. return -EINVAL;
  962. }
  963. }
  964. if (state == state_error) {
  965. *hc_state = saved_state;
  966. return -EINVAL;
  967. }
  968. return 0;
  969. }
  970. int
  971. via_parse_command_stream(struct drm_device *dev, const uint32_t *buf,
  972. unsigned int size)
  973. {
  974. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  975. uint32_t cmd;
  976. const uint32_t *buf_end = buf + (size >> 2);
  977. verifier_state_t state = state_command;
  978. int fire_count = 0;
  979. while (buf < buf_end) {
  980. switch (state) {
  981. case state_header2:
  982. state =
  983. via_parse_header2(dev_priv, &buf, buf_end,
  984. &fire_count);
  985. break;
  986. case state_header1:
  987. state = via_parse_header1(dev_priv, &buf, buf_end);
  988. break;
  989. case state_vheader5:
  990. state = via_parse_vheader5(dev_priv, &buf, buf_end);
  991. break;
  992. case state_vheader6:
  993. state = via_parse_vheader6(dev_priv, &buf, buf_end);
  994. break;
  995. case state_command:
  996. if (HALCYON_HEADER2 == (cmd = *buf))
  997. state = state_header2;
  998. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  999. state = state_header1;
  1000. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  1001. state = state_vheader5;
  1002. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  1003. state = state_vheader6;
  1004. else {
  1005. DRM_ERROR
  1006. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  1007. cmd);
  1008. state = state_error;
  1009. }
  1010. break;
  1011. case state_error:
  1012. default:
  1013. return -EINVAL;
  1014. }
  1015. }
  1016. if (state == state_error)
  1017. return -EINVAL;
  1018. return 0;
  1019. }
  1020. static void
  1021. setup_hazard_table(hz_init_t init_table[], hazard_t table[], int size)
  1022. {
  1023. int i;
  1024. for (i = 0; i < 256; ++i)
  1025. table[i] = forbidden_command;
  1026. for (i = 0; i < size; ++i)
  1027. table[init_table[i].code] = init_table[i].hz;
  1028. }
  1029. void via_init_command_verifier(void)
  1030. {
  1031. setup_hazard_table(init_table1, table1,
  1032. sizeof(init_table1) / sizeof(hz_init_t));
  1033. setup_hazard_table(init_table2, table2,
  1034. sizeof(init_table2) / sizeof(hz_init_t));
  1035. setup_hazard_table(init_table3, table3,
  1036. sizeof(init_table3) / sizeof(hz_init_t));
  1037. }