via_verifier.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. drm_device_t * dev)
  243. {
  244. struct list_head *list;
  245. drm_map_list_t *r_list;
  246. drm_local_map_t *map = seq->map_cache;
  247. if (map && map->offset <= offset
  248. && (offset + size) <= (map->offset + map->size)) {
  249. return map;
  250. }
  251. list_for_each(list, &dev->maplist->head) {
  252. r_list = (drm_map_list_t *) list;
  253. map = r_list->map;
  254. if (!map)
  255. continue;
  256. if (map->offset <= offset
  257. && (offset + size) <= (map->offset + map->size)
  258. && !(map->flags & _DRM_RESTRICTED)
  259. && (map->type == _DRM_AGP)) {
  260. seq->map_cache = map;
  261. return map;
  262. }
  263. }
  264. return NULL;
  265. }
  266. /*
  267. * Require that all AGP texture levels reside in the same AGP map which should
  268. * be mappable by the client. This is not a big restriction.
  269. * FIXME: To actually enforce this security policy strictly, drm_rmmap
  270. * would have to wait for dma quiescent before removing an AGP map.
  271. * The via_drm_lookup_agp_map call in reality seems to take
  272. * very little CPU time.
  273. */
  274. static __inline__ int finish_current_sequence(drm_via_state_t * cur_seq)
  275. {
  276. switch (cur_seq->unfinished) {
  277. case z_address:
  278. DRM_DEBUG("Z Buffer start address is 0x%x\n", cur_seq->z_addr);
  279. break;
  280. case dest_address:
  281. DRM_DEBUG("Destination start address is 0x%x\n",
  282. cur_seq->d_addr);
  283. break;
  284. case tex_address:
  285. if (cur_seq->agp_texture) {
  286. unsigned start =
  287. cur_seq->tex_level_lo[cur_seq->texture];
  288. unsigned end = cur_seq->tex_level_hi[cur_seq->texture];
  289. unsigned long lo = ~0, hi = 0, tmp;
  290. uint32_t *addr, *pitch, *height, tex;
  291. unsigned i;
  292. int npot;
  293. if (end > 9)
  294. end = 9;
  295. if (start > 9)
  296. start = 9;
  297. addr =
  298. &(cur_seq->t_addr[tex = cur_seq->texture][start]);
  299. pitch = &(cur_seq->pitch[tex][start]);
  300. height = &(cur_seq->height[tex][start]);
  301. npot = cur_seq->tex_npot[tex];
  302. for (i = start; i <= end; ++i) {
  303. tmp = *addr++;
  304. if (tmp < lo)
  305. lo = tmp;
  306. if (i == 0 && npot)
  307. tmp += (*height++ * *pitch++);
  308. else
  309. tmp += (*height++ << *pitch++);
  310. if (tmp > hi)
  311. hi = tmp;
  312. }
  313. if (!via_drm_lookup_agp_map
  314. (cur_seq, lo, hi - lo, cur_seq->dev)) {
  315. DRM_ERROR
  316. ("AGP texture is not in allowed map\n");
  317. return 2;
  318. }
  319. }
  320. break;
  321. default:
  322. break;
  323. }
  324. cur_seq->unfinished = no_sequence;
  325. return 0;
  326. }
  327. static __inline__ int
  328. investigate_hazard(uint32_t cmd, hazard_t hz, drm_via_state_t * cur_seq)
  329. {
  330. register uint32_t tmp, *tmp_addr;
  331. if (cur_seq->unfinished && (cur_seq->unfinished != seqs[hz])) {
  332. int ret;
  333. if ((ret = finish_current_sequence(cur_seq)))
  334. return ret;
  335. }
  336. switch (hz) {
  337. case check_for_header2:
  338. if (cmd == HALCYON_HEADER2)
  339. return 1;
  340. return 0;
  341. case check_for_header1:
  342. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  343. return 1;
  344. return 0;
  345. case check_for_header2_err:
  346. if (cmd == HALCYON_HEADER2)
  347. return 1;
  348. DRM_ERROR("Illegal DMA HALCYON_HEADER2 command\n");
  349. break;
  350. case check_for_header1_err:
  351. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  352. return 1;
  353. DRM_ERROR("Illegal DMA HALCYON_HEADER1 command\n");
  354. break;
  355. case check_for_fire:
  356. if ((cmd & HALCYON_FIREMASK) == HALCYON_FIRECMD)
  357. return 1;
  358. DRM_ERROR("Illegal DMA HALCYON_FIRECMD command\n");
  359. break;
  360. case check_for_dummy:
  361. if (HC_DUMMY == cmd)
  362. return 0;
  363. DRM_ERROR("Illegal DMA HC_DUMMY command\n");
  364. break;
  365. case check_for_dd:
  366. if (0xdddddddd == cmd)
  367. return 0;
  368. DRM_ERROR("Illegal DMA 0xdddddddd command\n");
  369. break;
  370. case check_z_buffer_addr0:
  371. cur_seq->unfinished = z_address;
  372. cur_seq->z_addr = (cur_seq->z_addr & 0xFF000000) |
  373. (cmd & 0x00FFFFFF);
  374. return 0;
  375. case check_z_buffer_addr1:
  376. cur_seq->unfinished = z_address;
  377. cur_seq->z_addr = (cur_seq->z_addr & 0x00FFFFFF) |
  378. ((cmd & 0xFF) << 24);
  379. return 0;
  380. case check_z_buffer_addr_mode:
  381. cur_seq->unfinished = z_address;
  382. if ((cmd & 0x0000C000) == 0)
  383. return 0;
  384. DRM_ERROR("Attempt to place Z buffer in system memory\n");
  385. return 2;
  386. case check_destination_addr0:
  387. cur_seq->unfinished = dest_address;
  388. cur_seq->d_addr = (cur_seq->d_addr & 0xFF000000) |
  389. (cmd & 0x00FFFFFF);
  390. return 0;
  391. case check_destination_addr1:
  392. cur_seq->unfinished = dest_address;
  393. cur_seq->d_addr = (cur_seq->d_addr & 0x00FFFFFF) |
  394. ((cmd & 0xFF) << 24);
  395. return 0;
  396. case check_destination_addr_mode:
  397. cur_seq->unfinished = dest_address;
  398. if ((cmd & 0x0000C000) == 0)
  399. return 0;
  400. DRM_ERROR
  401. ("Attempt to place 3D drawing buffer in system memory\n");
  402. return 2;
  403. case check_texture_addr0:
  404. cur_seq->unfinished = tex_address;
  405. tmp = (cmd >> 24);
  406. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  407. *tmp_addr = (*tmp_addr & 0xFF000000) | (cmd & 0x00FFFFFF);
  408. return 0;
  409. case check_texture_addr1:
  410. cur_seq->unfinished = tex_address;
  411. tmp = ((cmd >> 24) - 0x20);
  412. tmp += tmp << 1;
  413. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  414. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  415. tmp_addr++;
  416. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF00) << 16);
  417. tmp_addr++;
  418. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF0000) << 8);
  419. return 0;
  420. case check_texture_addr2:
  421. cur_seq->unfinished = tex_address;
  422. cur_seq->tex_level_lo[tmp = cur_seq->texture] = cmd & 0x3F;
  423. cur_seq->tex_level_hi[tmp] = (cmd & 0xFC0) >> 6;
  424. return 0;
  425. case check_texture_addr3:
  426. cur_seq->unfinished = tex_address;
  427. tmp = ((cmd >> 24) - HC_SubA_HTXnL0Pit);
  428. if (tmp == 0 &&
  429. (cmd & HC_HTXnEnPit_MASK)) {
  430. cur_seq->pitch[cur_seq->texture][tmp] =
  431. (cmd & HC_HTXnLnPit_MASK);
  432. cur_seq->tex_npot[cur_seq->texture] = 1;
  433. } else {
  434. cur_seq->pitch[cur_seq->texture][tmp] =
  435. (cmd & HC_HTXnLnPitE_MASK) >> HC_HTXnLnPitE_SHIFT;
  436. cur_seq->tex_npot[cur_seq->texture] = 0;
  437. if (cmd & 0x000FFFFF) {
  438. DRM_ERROR
  439. ("Unimplemented texture level 0 pitch mode.\n");
  440. return 2;
  441. }
  442. }
  443. return 0;
  444. case check_texture_addr4:
  445. cur_seq->unfinished = tex_address;
  446. tmp_addr = &cur_seq->t_addr[cur_seq->texture][9];
  447. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  448. return 0;
  449. case check_texture_addr5:
  450. case check_texture_addr6:
  451. cur_seq->unfinished = tex_address;
  452. /*
  453. * Texture width. We don't care since we have the pitch.
  454. */
  455. return 0;
  456. case check_texture_addr7:
  457. cur_seq->unfinished = tex_address;
  458. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  459. tmp_addr[5] = 1 << ((cmd & 0x00F00000) >> 20);
  460. tmp_addr[4] = 1 << ((cmd & 0x000F0000) >> 16);
  461. tmp_addr[3] = 1 << ((cmd & 0x0000F000) >> 12);
  462. tmp_addr[2] = 1 << ((cmd & 0x00000F00) >> 8);
  463. tmp_addr[1] = 1 << ((cmd & 0x000000F0) >> 4);
  464. tmp_addr[0] = 1 << (cmd & 0x0000000F);
  465. return 0;
  466. case check_texture_addr8:
  467. cur_seq->unfinished = tex_address;
  468. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  469. tmp_addr[9] = 1 << ((cmd & 0x0000F000) >> 12);
  470. tmp_addr[8] = 1 << ((cmd & 0x00000F00) >> 8);
  471. tmp_addr[7] = 1 << ((cmd & 0x000000F0) >> 4);
  472. tmp_addr[6] = 1 << (cmd & 0x0000000F);
  473. return 0;
  474. case check_texture_addr_mode:
  475. cur_seq->unfinished = tex_address;
  476. if (2 == (tmp = cmd & 0x00000003)) {
  477. DRM_ERROR
  478. ("Attempt to fetch texture from system memory.\n");
  479. return 2;
  480. }
  481. cur_seq->agp_texture = (tmp == 3);
  482. cur_seq->tex_palette_size[cur_seq->texture] =
  483. (cmd >> 16) & 0x000000007;
  484. return 0;
  485. case check_for_vertex_count:
  486. cur_seq->vertex_count = cmd & 0x0000FFFF;
  487. return 0;
  488. case check_number_texunits:
  489. cur_seq->multitex = (cmd >> 3) & 1;
  490. return 0;
  491. default:
  492. DRM_ERROR("Illegal DMA data: 0x%x\n", cmd);
  493. return 2;
  494. }
  495. return 2;
  496. }
  497. static __inline__ int
  498. via_check_prim_list(uint32_t const **buffer, const uint32_t * buf_end,
  499. drm_via_state_t * cur_seq)
  500. {
  501. drm_via_private_t *dev_priv =
  502. (drm_via_private_t *) cur_seq->dev->dev_private;
  503. uint32_t a_fire, bcmd, dw_count;
  504. int ret = 0;
  505. int have_fire;
  506. const uint32_t *buf = *buffer;
  507. while (buf < buf_end) {
  508. have_fire = 0;
  509. if ((buf_end - buf) < 2) {
  510. DRM_ERROR
  511. ("Unexpected termination of primitive list.\n");
  512. ret = 1;
  513. break;
  514. }
  515. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdB)
  516. break;
  517. bcmd = *buf++;
  518. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdA) {
  519. DRM_ERROR("Expected Vertex List A command, got 0x%x\n",
  520. *buf);
  521. ret = 1;
  522. break;
  523. }
  524. a_fire =
  525. *buf++ | HC_HPLEND_MASK | HC_HPMValidN_MASK |
  526. HC_HE3Fire_MASK;
  527. /*
  528. * How many dwords per vertex ?
  529. */
  530. if (cur_seq->agp && ((bcmd & (0xF << 11)) == 0)) {
  531. DRM_ERROR("Illegal B command vertex data for AGP.\n");
  532. ret = 1;
  533. break;
  534. }
  535. dw_count = 0;
  536. if (bcmd & (1 << 7))
  537. dw_count += (cur_seq->multitex) ? 2 : 1;
  538. if (bcmd & (1 << 8))
  539. dw_count += (cur_seq->multitex) ? 2 : 1;
  540. if (bcmd & (1 << 9))
  541. dw_count++;
  542. if (bcmd & (1 << 10))
  543. dw_count++;
  544. if (bcmd & (1 << 11))
  545. dw_count++;
  546. if (bcmd & (1 << 12))
  547. dw_count++;
  548. if (bcmd & (1 << 13))
  549. dw_count++;
  550. if (bcmd & (1 << 14))
  551. dw_count++;
  552. while (buf < buf_end) {
  553. if (*buf == a_fire) {
  554. if (dev_priv->num_fire_offsets >=
  555. VIA_FIRE_BUF_SIZE) {
  556. DRM_ERROR("Fire offset buffer full.\n");
  557. ret = 1;
  558. break;
  559. }
  560. dev_priv->fire_offsets[dev_priv->
  561. num_fire_offsets++] =
  562. buf;
  563. have_fire = 1;
  564. buf++;
  565. if (buf < buf_end && *buf == a_fire)
  566. buf++;
  567. break;
  568. }
  569. if ((*buf == HALCYON_HEADER2) ||
  570. ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD)) {
  571. DRM_ERROR("Missing Vertex Fire command, "
  572. "Stray Vertex Fire command or verifier "
  573. "lost sync.\n");
  574. ret = 1;
  575. break;
  576. }
  577. if ((ret = eat_words(&buf, buf_end, dw_count)))
  578. break;
  579. }
  580. if (buf >= buf_end && !have_fire) {
  581. DRM_ERROR("Missing Vertex Fire command or verifier "
  582. "lost sync.\n");
  583. ret = 1;
  584. break;
  585. }
  586. if (cur_seq->agp && ((buf - cur_seq->buf_start) & 0x01)) {
  587. DRM_ERROR("AGP Primitive list end misaligned.\n");
  588. ret = 1;
  589. break;
  590. }
  591. }
  592. *buffer = buf;
  593. return ret;
  594. }
  595. static __inline__ verifier_state_t
  596. via_check_header2(uint32_t const **buffer, const uint32_t * buf_end,
  597. drm_via_state_t * hc_state)
  598. {
  599. uint32_t cmd;
  600. int hz_mode;
  601. hazard_t hz;
  602. const uint32_t *buf = *buffer;
  603. const hazard_t *hz_table;
  604. if ((buf_end - buf) < 2) {
  605. DRM_ERROR
  606. ("Illegal termination of DMA HALCYON_HEADER2 sequence.\n");
  607. return state_error;
  608. }
  609. buf++;
  610. cmd = (*buf++ & 0xFFFF0000) >> 16;
  611. switch (cmd) {
  612. case HC_ParaType_CmdVdata:
  613. if (via_check_prim_list(&buf, buf_end, hc_state))
  614. return state_error;
  615. *buffer = buf;
  616. return state_command;
  617. case HC_ParaType_NotTex:
  618. hz_table = table1;
  619. break;
  620. case HC_ParaType_Tex:
  621. hc_state->texture = 0;
  622. hz_table = table2;
  623. break;
  624. case (HC_ParaType_Tex | (HC_SubType_Tex1 << 8)):
  625. hc_state->texture = 1;
  626. hz_table = table2;
  627. break;
  628. case (HC_ParaType_Tex | (HC_SubType_TexGeneral << 8)):
  629. hz_table = table3;
  630. break;
  631. case HC_ParaType_Auto:
  632. if (eat_words(&buf, buf_end, 2))
  633. return state_error;
  634. *buffer = buf;
  635. return state_command;
  636. case (HC_ParaType_Palette | (HC_SubType_Stipple << 8)):
  637. if (eat_words(&buf, buf_end, 32))
  638. return state_error;
  639. *buffer = buf;
  640. return state_command;
  641. case (HC_ParaType_Palette | (HC_SubType_TexPalette0 << 8)):
  642. case (HC_ParaType_Palette | (HC_SubType_TexPalette1 << 8)):
  643. DRM_ERROR("Texture palettes are rejected because of "
  644. "lack of info how to determine their size.\n");
  645. return state_error;
  646. case (HC_ParaType_Palette | (HC_SubType_FogTable << 8)):
  647. DRM_ERROR("Fog factor palettes are rejected because of "
  648. "lack of info how to determine their size.\n");
  649. return state_error;
  650. default:
  651. /*
  652. * There are some unimplemented HC_ParaTypes here, that
  653. * need to be implemented if the Mesa driver is extended.
  654. */
  655. DRM_ERROR("Invalid or unimplemented HALCYON_HEADER2 "
  656. "DMA subcommand: 0x%x. Previous dword: 0x%x\n",
  657. cmd, *(buf - 2));
  658. *buffer = buf;
  659. return state_error;
  660. }
  661. while (buf < buf_end) {
  662. cmd = *buf++;
  663. if ((hz = hz_table[cmd >> 24])) {
  664. if ((hz_mode = investigate_hazard(cmd, hz, hc_state))) {
  665. if (hz_mode == 1) {
  666. buf--;
  667. break;
  668. }
  669. return state_error;
  670. }
  671. } else if (hc_state->unfinished &&
  672. finish_current_sequence(hc_state)) {
  673. return state_error;
  674. }
  675. }
  676. if (hc_state->unfinished && finish_current_sequence(hc_state)) {
  677. return state_error;
  678. }
  679. *buffer = buf;
  680. return state_command;
  681. }
  682. static __inline__ verifier_state_t
  683. via_parse_header2(drm_via_private_t * dev_priv, uint32_t const **buffer,
  684. const uint32_t * buf_end, int *fire_count)
  685. {
  686. uint32_t cmd;
  687. const uint32_t *buf = *buffer;
  688. const uint32_t *next_fire;
  689. int burst = 0;
  690. next_fire = dev_priv->fire_offsets[*fire_count];
  691. buf++;
  692. cmd = (*buf & 0xFFFF0000) >> 16;
  693. VIA_WRITE(HC_REG_TRANS_SET + HC_REG_BASE, *buf++);
  694. switch (cmd) {
  695. case HC_ParaType_CmdVdata:
  696. while ((buf < buf_end) &&
  697. (*fire_count < dev_priv->num_fire_offsets) &&
  698. (*buf & HC_ACMD_MASK) == HC_ACMD_HCmdB) {
  699. while (buf <= next_fire) {
  700. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  701. (burst & 63), *buf++);
  702. burst += 4;
  703. }
  704. if ((buf < buf_end)
  705. && ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD))
  706. buf++;
  707. if (++(*fire_count) < dev_priv->num_fire_offsets)
  708. next_fire = dev_priv->fire_offsets[*fire_count];
  709. }
  710. break;
  711. default:
  712. while (buf < buf_end) {
  713. if (*buf == HC_HEADER2 ||
  714. (*buf & HALCYON_HEADER1MASK) == HALCYON_HEADER1 ||
  715. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5 ||
  716. (*buf & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  717. break;
  718. VIA_WRITE(HC_REG_TRANS_SPACE + HC_REG_BASE +
  719. (burst & 63), *buf++);
  720. burst += 4;
  721. }
  722. }
  723. *buffer = buf;
  724. return state_command;
  725. }
  726. static __inline__ int verify_mmio_address(uint32_t address)
  727. {
  728. if ((address > 0x3FF) && (address < 0xC00)) {
  729. DRM_ERROR("Invalid VIDEO DMA command. "
  730. "Attempt to access 3D- or command burst area.\n");
  731. return 1;
  732. } else if ((address > 0xCFF) && (address < 0x1300)) {
  733. DRM_ERROR("Invalid VIDEO DMA command. "
  734. "Attempt to access PCI DMA area.\n");
  735. return 1;
  736. } else if (address > 0x13FF) {
  737. DRM_ERROR("Invalid VIDEO DMA command. "
  738. "Attempt to access VGA registers.\n");
  739. return 1;
  740. }
  741. return 0;
  742. }
  743. static __inline__ int
  744. verify_video_tail(uint32_t const **buffer, const uint32_t * buf_end,
  745. uint32_t dwords)
  746. {
  747. const uint32_t *buf = *buffer;
  748. if (buf_end - buf < dwords) {
  749. DRM_ERROR("Illegal termination of video command.\n");
  750. return 1;
  751. }
  752. while (dwords--) {
  753. if (*buf++) {
  754. DRM_ERROR("Illegal video command tail.\n");
  755. return 1;
  756. }
  757. }
  758. *buffer = buf;
  759. return 0;
  760. }
  761. static __inline__ verifier_state_t
  762. via_check_header1(uint32_t const **buffer, const uint32_t * buf_end)
  763. {
  764. uint32_t cmd;
  765. const uint32_t *buf = *buffer;
  766. verifier_state_t ret = state_command;
  767. while (buf < buf_end) {
  768. cmd = *buf;
  769. if ((cmd > ((0x3FF >> 2) | HALCYON_HEADER1)) &&
  770. (cmd < ((0xC00 >> 2) | HALCYON_HEADER1))) {
  771. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  772. break;
  773. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  774. "Attempt to access 3D- or command burst area.\n");
  775. ret = state_error;
  776. break;
  777. } else if (cmd > ((0xCFF >> 2) | HALCYON_HEADER1)) {
  778. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  779. break;
  780. DRM_ERROR("Invalid HALCYON_HEADER1 command. "
  781. "Attempt to access VGA registers.\n");
  782. ret = state_error;
  783. break;
  784. } else {
  785. buf += 2;
  786. }
  787. }
  788. *buffer = buf;
  789. return ret;
  790. }
  791. static __inline__ verifier_state_t
  792. via_parse_header1(drm_via_private_t * dev_priv, uint32_t const **buffer,
  793. const uint32_t * buf_end)
  794. {
  795. register uint32_t cmd;
  796. const uint32_t *buf = *buffer;
  797. while (buf < buf_end) {
  798. cmd = *buf;
  799. if ((cmd & HALCYON_HEADER1MASK) != HALCYON_HEADER1)
  800. break;
  801. VIA_WRITE((cmd & ~HALCYON_HEADER1MASK) << 2, *++buf);
  802. buf++;
  803. }
  804. *buffer = buf;
  805. return state_command;
  806. }
  807. static __inline__ verifier_state_t
  808. via_check_vheader5(uint32_t const **buffer, const uint32_t * buf_end)
  809. {
  810. uint32_t data;
  811. const uint32_t *buf = *buffer;
  812. if (buf_end - buf < 4) {
  813. DRM_ERROR("Illegal termination of video header5 command\n");
  814. return state_error;
  815. }
  816. data = *buf++ & ~VIA_VIDEOMASK;
  817. if (verify_mmio_address(data))
  818. return state_error;
  819. data = *buf++;
  820. if (*buf++ != 0x00F50000) {
  821. DRM_ERROR("Illegal header5 header data\n");
  822. return state_error;
  823. }
  824. if (*buf++ != 0x00000000) {
  825. DRM_ERROR("Illegal header5 header data\n");
  826. return state_error;
  827. }
  828. if (eat_words(&buf, buf_end, data))
  829. return state_error;
  830. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  831. return state_error;
  832. *buffer = buf;
  833. return state_command;
  834. }
  835. static __inline__ verifier_state_t
  836. via_parse_vheader5(drm_via_private_t * dev_priv, uint32_t const **buffer,
  837. const uint32_t * buf_end)
  838. {
  839. uint32_t addr, count, i;
  840. const uint32_t *buf = *buffer;
  841. addr = *buf++ & ~VIA_VIDEOMASK;
  842. i = count = *buf;
  843. buf += 3;
  844. while (i--) {
  845. VIA_WRITE(addr, *buf++);
  846. }
  847. if (count & 3)
  848. buf += 4 - (count & 3);
  849. *buffer = buf;
  850. return state_command;
  851. }
  852. static __inline__ verifier_state_t
  853. via_check_vheader6(uint32_t const **buffer, const uint32_t * buf_end)
  854. {
  855. uint32_t data;
  856. const uint32_t *buf = *buffer;
  857. uint32_t i;
  858. if (buf_end - buf < 4) {
  859. DRM_ERROR("Illegal termination of video header6 command\n");
  860. return state_error;
  861. }
  862. buf++;
  863. data = *buf++;
  864. if (*buf++ != 0x00F60000) {
  865. DRM_ERROR("Illegal header6 header data\n");
  866. return state_error;
  867. }
  868. if (*buf++ != 0x00000000) {
  869. DRM_ERROR("Illegal header6 header data\n");
  870. return state_error;
  871. }
  872. if ((buf_end - buf) < (data << 1)) {
  873. DRM_ERROR("Illegal termination of video header6 command\n");
  874. return state_error;
  875. }
  876. for (i = 0; i < data; ++i) {
  877. if (verify_mmio_address(*buf++))
  878. return state_error;
  879. buf++;
  880. }
  881. data <<= 1;
  882. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  883. return state_error;
  884. *buffer = buf;
  885. return state_command;
  886. }
  887. static __inline__ verifier_state_t
  888. via_parse_vheader6(drm_via_private_t * dev_priv, uint32_t const **buffer,
  889. const uint32_t * buf_end)
  890. {
  891. uint32_t addr, count, i;
  892. const uint32_t *buf = *buffer;
  893. i = count = *++buf;
  894. buf += 3;
  895. while (i--) {
  896. addr = *buf++;
  897. VIA_WRITE(addr, *buf++);
  898. }
  899. count <<= 1;
  900. if (count & 3)
  901. buf += 4 - (count & 3);
  902. *buffer = buf;
  903. return state_command;
  904. }
  905. int
  906. via_verify_command_stream(const uint32_t * buf, unsigned int size,
  907. drm_device_t * dev, int agp)
  908. {
  909. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  910. drm_via_state_t *hc_state = &dev_priv->hc_state;
  911. drm_via_state_t saved_state = *hc_state;
  912. uint32_t cmd;
  913. const uint32_t *buf_end = buf + (size >> 2);
  914. verifier_state_t state = state_command;
  915. int cme_video;
  916. int supported_3d;
  917. cme_video = (dev_priv->chipset == VIA_PRO_GROUP_A ||
  918. dev_priv->chipset == VIA_DX9_0);
  919. supported_3d = dev_priv->chipset != VIA_DX9_0;
  920. hc_state->dev = dev;
  921. hc_state->unfinished = no_sequence;
  922. hc_state->map_cache = NULL;
  923. hc_state->agp = agp;
  924. hc_state->buf_start = buf;
  925. dev_priv->num_fire_offsets = 0;
  926. while (buf < buf_end) {
  927. switch (state) {
  928. case state_header2:
  929. state = via_check_header2(&buf, buf_end, hc_state);
  930. break;
  931. case state_header1:
  932. state = via_check_header1(&buf, buf_end);
  933. break;
  934. case state_vheader5:
  935. state = via_check_vheader5(&buf, buf_end);
  936. break;
  937. case state_vheader6:
  938. state = via_check_vheader6(&buf, buf_end);
  939. break;
  940. case state_command:
  941. if ((HALCYON_HEADER2 == (cmd = *buf)) &&
  942. supported_3d)
  943. state = state_header2;
  944. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  945. state = state_header1;
  946. else if (cme_video
  947. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  948. state = state_vheader5;
  949. else if (cme_video
  950. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  951. state = state_vheader6;
  952. else if ((cmd == HALCYON_HEADER2) && !supported_3d) {
  953. DRM_ERROR("Accelerated 3D is not supported on this chipset yet.\n");
  954. state = state_error;
  955. } else {
  956. DRM_ERROR
  957. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  958. cmd);
  959. state = state_error;
  960. }
  961. break;
  962. case state_error:
  963. default:
  964. *hc_state = saved_state;
  965. return DRM_ERR(EINVAL);
  966. }
  967. }
  968. if (state == state_error) {
  969. *hc_state = saved_state;
  970. return DRM_ERR(EINVAL);
  971. }
  972. return 0;
  973. }
  974. int
  975. via_parse_command_stream(drm_device_t * dev, const uint32_t * buf,
  976. unsigned int size)
  977. {
  978. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  979. uint32_t cmd;
  980. const uint32_t *buf_end = buf + (size >> 2);
  981. verifier_state_t state = state_command;
  982. int fire_count = 0;
  983. while (buf < buf_end) {
  984. switch (state) {
  985. case state_header2:
  986. state =
  987. via_parse_header2(dev_priv, &buf, buf_end,
  988. &fire_count);
  989. break;
  990. case state_header1:
  991. state = via_parse_header1(dev_priv, &buf, buf_end);
  992. break;
  993. case state_vheader5:
  994. state = via_parse_vheader5(dev_priv, &buf, buf_end);
  995. break;
  996. case state_vheader6:
  997. state = via_parse_vheader6(dev_priv, &buf, buf_end);
  998. break;
  999. case state_command:
  1000. if (HALCYON_HEADER2 == (cmd = *buf))
  1001. state = state_header2;
  1002. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  1003. state = state_header1;
  1004. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  1005. state = state_vheader5;
  1006. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  1007. state = state_vheader6;
  1008. else {
  1009. DRM_ERROR
  1010. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  1011. cmd);
  1012. state = state_error;
  1013. }
  1014. break;
  1015. case state_error:
  1016. default:
  1017. return DRM_ERR(EINVAL);
  1018. }
  1019. }
  1020. if (state == state_error) {
  1021. return DRM_ERR(EINVAL);
  1022. }
  1023. return 0;
  1024. }
  1025. static void
  1026. setup_hazard_table(hz_init_t init_table[], hazard_t table[], int size)
  1027. {
  1028. int i;
  1029. for (i = 0; i < 256; ++i) {
  1030. table[i] = forbidden_command;
  1031. }
  1032. for (i = 0; i < size; ++i) {
  1033. table[init_table[i].code] = init_table[i].hz;
  1034. }
  1035. }
  1036. void via_init_command_verifier(void)
  1037. {
  1038. setup_hazard_table(init_table1, table1,
  1039. sizeof(init_table1) / sizeof(hz_init_t));
  1040. setup_hazard_table(init_table2, table2,
  1041. sizeof(init_table2) / sizeof(hz_init_t));
  1042. setup_hazard_table(init_table3, table3,
  1043. sizeof(init_table3) / sizeof(hz_init_t));
  1044. }