via_verifier.c 27 KB

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