via_verifier.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 "drmP.h"
  32. #include "drm.h"
  33. #include "via_drm.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. }
  677. *buffer = buf;
  678. return state_command;
  679. }
  680. static __inline__ verifier_state_t
  681. via_parse_header2(drm_via_private_t * dev_priv, uint32_t const **buffer,
  682. const uint32_t * buf_end, int *fire_count)
  683. {
  684. uint32_t cmd;
  685. const uint32_t *buf = *buffer;
  686. const uint32_t *next_fire;
  687. int burst = 0;
  688. next_fire = dev_priv->fire_offsets[*fire_count];
  689. buf++;
  690. cmd = (*buf & 0xFFFF0000) >> 16;
  691. VIA_WRITE(HC_REG_TRANS_SET + HC_REG_BASE, *buf++);
  692. switch (cmd) {
  693. case HC_ParaType_CmdVdata:
  694. while ((buf < buf_end) &&
  695. (*fire_count < dev_priv->num_fire_offsets) &&
  696. (*buf & HC_ACMD_MASK) == HC_ACMD_HCmdB) {
  697. while (buf <= next_fire) {
  698. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  699. (burst & 63), *buf++);
  700. burst += 4;
  701. }
  702. if ((buf < buf_end)
  703. && ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD))
  704. buf++;
  705. if (++(*fire_count) < dev_priv->num_fire_offsets)
  706. next_fire = dev_priv->fire_offsets[*fire_count];
  707. }
  708. break;
  709. default:
  710. while (buf < buf_end) {
  711. if (*buf == HC_HEADER2 ||
  712. (*buf & HALCYON_HEADER1MASK) == HALCYON_HEADER1 ||
  713. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5 ||
  714. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  715. break;
  716. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  717. (burst & 63), *buf++);
  718. burst += 4;
  719. }
  720. }
  721. *buffer = buf;
  722. return state_command;
  723. }
  724. static __inline__ int verify_mmio_address(uint32_t address)
  725. {
  726. if ((address > 0x3FF) && (address < 0xC00)) {
  727. DRM_ERROR("Invalid VIDEO DMA command. "
  728. "Attempt to access 3D- or command burst area.\n");
  729. return 1;
  730. } else if ((address > 0xCFF) && (address < 0x1300)) {
  731. DRM_ERROR("Invalid VIDEO DMA command. "
  732. "Attempt to access PCI DMA area.\n");
  733. return 1;
  734. } else if (address > 0x13FF) {
  735. DRM_ERROR("Invalid VIDEO DMA command. "
  736. "Attempt to access VGA registers.\n");
  737. return 1;
  738. }
  739. return 0;
  740. }
  741. static __inline__ int
  742. verify_video_tail(uint32_t const **buffer, const uint32_t * buf_end,
  743. uint32_t dwords)
  744. {
  745. const uint32_t *buf = *buffer;
  746. if (buf_end - buf < dwords) {
  747. DRM_ERROR("Illegal termination of video command.\n");
  748. return 1;
  749. }
  750. while (dwords--) {
  751. if (*buf++) {
  752. DRM_ERROR("Illegal video command tail.\n");
  753. return 1;
  754. }
  755. }
  756. *buffer = buf;
  757. return 0;
  758. }
  759. static __inline__ verifier_state_t
  760. via_check_header1(uint32_t const **buffer, const uint32_t * buf_end)
  761. {
  762. uint32_t cmd;
  763. const uint32_t *buf = *buffer;
  764. verifier_state_t ret = state_command;
  765. while (buf < buf_end) {
  766. cmd = *buf;
  767. if ((cmd > ((0x3FF >> 2) | HALCYON_HEADER1)) &&
  768. (cmd < ((0xC00 >> 2) | HALCYON_HEADER1))) {
  769. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  770. break;
  771. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  772. "Attempt to access 3D- or command burst area.\n");
  773. ret = state_error;
  774. break;
  775. } else if (cmd > ((0xCFF >> 2) | HALCYON_HEADER1)) {
  776. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  777. break;
  778. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  779. "Attempt to access VGA registers.\n");
  780. ret = state_error;
  781. break;
  782. } else {
  783. buf += 2;
  784. }
  785. }
  786. *buffer = buf;
  787. return ret;
  788. }
  789. static __inline__ verifier_state_t
  790. via_parse_header1(drm_via_private_t * dev_priv, uint32_t const **buffer,
  791. const uint32_t * buf_end)
  792. {
  793. register uint32_t cmd;
  794. const uint32_t *buf = *buffer;
  795. while (buf < buf_end) {
  796. cmd = *buf;
  797. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  798. break;
  799. VIA_WRITE((cmd & ~HALCYON_HEADER1MASK) << 2, *++buf);
  800. buf++;
  801. }
  802. *buffer = buf;
  803. return state_command;
  804. }
  805. static __inline__ verifier_state_t
  806. via_check_vheader5(uint32_t const **buffer, const uint32_t * buf_end)
  807. {
  808. uint32_t data;
  809. const uint32_t *buf = *buffer;
  810. if (buf_end - buf < 4) {
  811. DRM_ERROR("Illegal termination of video header5 command\n");
  812. return state_error;
  813. }
  814. data = *buf++ & ~VIA_VIDEOMASK;
  815. if (verify_mmio_address(data))
  816. return state_error;
  817. data = *buf++;
  818. if (*buf++ != 0x00F50000) {
  819. DRM_ERROR("Illegal header5 header data\n");
  820. return state_error;
  821. }
  822. if (*buf++ != 0x00000000) {
  823. DRM_ERROR("Illegal header5 header data\n");
  824. return state_error;
  825. }
  826. if (eat_words(&buf, buf_end, data))
  827. return state_error;
  828. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  829. return state_error;
  830. *buffer = buf;
  831. return state_command;
  832. }
  833. static __inline__ verifier_state_t
  834. via_parse_vheader5(drm_via_private_t * dev_priv, uint32_t const **buffer,
  835. const uint32_t * buf_end)
  836. {
  837. uint32_t addr, count, i;
  838. const uint32_t *buf = *buffer;
  839. addr = *buf++ & ~VIA_VIDEOMASK;
  840. i = count = *buf;
  841. buf += 3;
  842. while (i--) {
  843. VIA_WRITE(addr, *buf++);
  844. }
  845. if (count & 3)
  846. buf += 4 - (count & 3);
  847. *buffer = buf;
  848. return state_command;
  849. }
  850. static __inline__ verifier_state_t
  851. via_check_vheader6(uint32_t const **buffer, const uint32_t * buf_end)
  852. {
  853. uint32_t data;
  854. const uint32_t *buf = *buffer;
  855. uint32_t i;
  856. if (buf_end - buf < 4) {
  857. DRM_ERROR("Illegal termination of video header6 command\n");
  858. return state_error;
  859. }
  860. buf++;
  861. data = *buf++;
  862. if (*buf++ != 0x00F60000) {
  863. DRM_ERROR("Illegal header6 header data\n");
  864. return state_error;
  865. }
  866. if (*buf++ != 0x00000000) {
  867. DRM_ERROR("Illegal header6 header data\n");
  868. return state_error;
  869. }
  870. if ((buf_end - buf) < (data << 1)) {
  871. DRM_ERROR("Illegal termination of video header6 command\n");
  872. return state_error;
  873. }
  874. for (i = 0; i < data; ++i) {
  875. if (verify_mmio_address(*buf++))
  876. return state_error;
  877. buf++;
  878. }
  879. data <<= 1;
  880. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  881. return state_error;
  882. *buffer = buf;
  883. return state_command;
  884. }
  885. static __inline__ verifier_state_t
  886. via_parse_vheader6(drm_via_private_t * dev_priv, uint32_t const **buffer,
  887. const uint32_t * buf_end)
  888. {
  889. uint32_t addr, count, i;
  890. const uint32_t *buf = *buffer;
  891. i = count = *++buf;
  892. buf += 3;
  893. while (i--) {
  894. addr = *buf++;
  895. VIA_WRITE(addr, *buf++);
  896. }
  897. count <<= 1;
  898. if (count & 3)
  899. buf += 4 - (count & 3);
  900. *buffer = buf;
  901. return state_command;
  902. }
  903. int
  904. via_verify_command_stream(const uint32_t * buf, unsigned int size,
  905. struct drm_device * dev, int agp)
  906. {
  907. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  908. drm_via_state_t *hc_state = &dev_priv->hc_state;
  909. drm_via_state_t saved_state = *hc_state;
  910. uint32_t cmd;
  911. const uint32_t *buf_end = buf + (size >> 2);
  912. verifier_state_t state = state_command;
  913. int cme_video;
  914. int supported_3d;
  915. cme_video = (dev_priv->chipset == VIA_PRO_GROUP_A ||
  916. dev_priv->chipset == VIA_DX9_0);
  917. supported_3d = dev_priv->chipset != VIA_DX9_0;
  918. hc_state->dev = dev;
  919. hc_state->unfinished = no_sequence;
  920. hc_state->map_cache = NULL;
  921. hc_state->agp = agp;
  922. hc_state->buf_start = buf;
  923. dev_priv->num_fire_offsets = 0;
  924. while (buf < buf_end) {
  925. switch (state) {
  926. case state_header2:
  927. state = via_check_header2(&buf, buf_end, hc_state);
  928. break;
  929. case state_header1:
  930. state = via_check_header1(&buf, buf_end);
  931. break;
  932. case state_vheader5:
  933. state = via_check_vheader5(&buf, buf_end);
  934. break;
  935. case state_vheader6:
  936. state = via_check_vheader6(&buf, buf_end);
  937. break;
  938. case state_command:
  939. if ((HALCYON_HEADER2 == (cmd = *buf)) &&
  940. supported_3d)
  941. state = state_header2;
  942. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  943. state = state_header1;
  944. else if (cme_video
  945. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  946. state = state_vheader5;
  947. else if (cme_video
  948. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  949. state = state_vheader6;
  950. else if ((cmd == HALCYON_HEADER2) && !supported_3d) {
  951. DRM_ERROR("Accelerated 3D is not supported on this chipset yet.\n");
  952. state = state_error;
  953. } else {
  954. DRM_ERROR
  955. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  956. cmd);
  957. state = state_error;
  958. }
  959. break;
  960. case state_error:
  961. default:
  962. *hc_state = saved_state;
  963. return -EINVAL;
  964. }
  965. }
  966. if (state == state_error) {
  967. *hc_state = saved_state;
  968. return -EINVAL;
  969. }
  970. return 0;
  971. }
  972. int
  973. via_parse_command_stream(struct drm_device * dev, const uint32_t * buf,
  974. unsigned int size)
  975. {
  976. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  977. uint32_t cmd;
  978. const uint32_t *buf_end = buf + (size >> 2);
  979. verifier_state_t state = state_command;
  980. int fire_count = 0;
  981. while (buf < buf_end) {
  982. switch (state) {
  983. case state_header2:
  984. state =
  985. via_parse_header2(dev_priv, &buf, buf_end,
  986. &fire_count);
  987. break;
  988. case state_header1:
  989. state = via_parse_header1(dev_priv, &buf, buf_end);
  990. break;
  991. case state_vheader5:
  992. state = via_parse_vheader5(dev_priv, &buf, buf_end);
  993. break;
  994. case state_vheader6:
  995. state = via_parse_vheader6(dev_priv, &buf, buf_end);
  996. break;
  997. case state_command:
  998. if (HALCYON_HEADER2 == (cmd = *buf))
  999. state = state_header2;
  1000. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  1001. state = state_header1;
  1002. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  1003. state = state_vheader5;
  1004. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  1005. state = state_vheader6;
  1006. else {
  1007. DRM_ERROR
  1008. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  1009. cmd);
  1010. state = state_error;
  1011. }
  1012. break;
  1013. case state_error:
  1014. default:
  1015. return -EINVAL;
  1016. }
  1017. }
  1018. if (state == state_error) {
  1019. return -EINVAL;
  1020. }
  1021. return 0;
  1022. }
  1023. static void
  1024. setup_hazard_table(hz_init_t init_table[], hazard_t table[], int size)
  1025. {
  1026. int i;
  1027. for (i = 0; i < 256; ++i) {
  1028. table[i] = forbidden_command;
  1029. }
  1030. for (i = 0; i < size; ++i) {
  1031. table[init_table[i].code] = init_table[i].hz;
  1032. }
  1033. }
  1034. void via_init_command_verifier(void)
  1035. {
  1036. setup_hazard_table(init_table1, table1,
  1037. sizeof(init_table1) / sizeof(hz_init_t));
  1038. setup_hazard_table(init_table2, table2,
  1039. sizeof(init_table2) / sizeof(hz_init_t));
  1040. setup_hazard_table(init_table3, table3,
  1041. sizeof(init_table3) / sizeof(hz_init_t));
  1042. }