via_verifier.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * Copyright 2004 The Unichrome Project. All Rights Reserved.
  3. * Copyright 2005 Thomas Hellstrom. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial portions
  14. * of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHOR(S), AND/OR THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Author: Thomas Hellstrom 2004, 2005.
  25. * This code was written using docs obtained under NDA from VIA Inc.
  26. *
  27. * Don't run this code directly on an AGP buffer. Due to cache problems it will
  28. * be very slow.
  29. */
  30. #include "via_3d_reg.h"
  31. #include <drm/drmP.h>
  32. #include <drm/via_drm.h>
  33. #include <drm/drm_legacy.h>
  34. #include "via_verifier.h"
  35. #include "via_drv.h"
  36. #include <linux/kernel.h>
  37. typedef enum {
  38. state_command,
  39. state_header2,
  40. state_header1,
  41. state_vheader5,
  42. state_vheader6,
  43. state_error
  44. } verifier_state_t;
  45. typedef enum {
  46. no_check = 0,
  47. check_for_header2,
  48. check_for_header1,
  49. check_for_header2_err,
  50. check_for_header1_err,
  51. check_for_fire,
  52. check_z_buffer_addr0,
  53. check_z_buffer_addr1,
  54. check_z_buffer_addr_mode,
  55. check_destination_addr0,
  56. check_destination_addr1,
  57. check_destination_addr_mode,
  58. check_for_dummy,
  59. check_for_dd,
  60. check_texture_addr0,
  61. check_texture_addr1,
  62. check_texture_addr2,
  63. check_texture_addr3,
  64. check_texture_addr4,
  65. check_texture_addr5,
  66. check_texture_addr6,
  67. check_texture_addr7,
  68. check_texture_addr8,
  69. check_texture_addr_mode,
  70. check_for_vertex_count,
  71. check_number_texunits,
  72. forbidden_command
  73. } hazard_t;
  74. /*
  75. * Associates each hazard above with a possible multi-command
  76. * sequence. For example an address that is split over multiple
  77. * commands and that needs to be checked at the first command
  78. * that does not include any part of the address.
  79. */
  80. static drm_via_sequence_t seqs[] = {
  81. no_sequence,
  82. no_sequence,
  83. no_sequence,
  84. no_sequence,
  85. no_sequence,
  86. no_sequence,
  87. z_address,
  88. z_address,
  89. z_address,
  90. dest_address,
  91. dest_address,
  92. dest_address,
  93. no_sequence,
  94. no_sequence,
  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. tex_address,
  105. no_sequence
  106. };
  107. typedef struct {
  108. unsigned int code;
  109. hazard_t hz;
  110. } hz_init_t;
  111. static hz_init_t init_table1[] = {
  112. {0xf2, check_for_header2_err},
  113. {0xf0, check_for_header1_err},
  114. {0xee, check_for_fire},
  115. {0xcc, check_for_dummy},
  116. {0xdd, check_for_dd},
  117. {0x00, no_check},
  118. {0x10, check_z_buffer_addr0},
  119. {0x11, check_z_buffer_addr1},
  120. {0x12, check_z_buffer_addr_mode},
  121. {0x13, no_check},
  122. {0x14, no_check},
  123. {0x15, no_check},
  124. {0x23, no_check},
  125. {0x24, no_check},
  126. {0x33, no_check},
  127. {0x34, no_check},
  128. {0x35, no_check},
  129. {0x36, no_check},
  130. {0x37, no_check},
  131. {0x38, no_check},
  132. {0x39, no_check},
  133. {0x3A, no_check},
  134. {0x3B, no_check},
  135. {0x3C, no_check},
  136. {0x3D, no_check},
  137. {0x3E, no_check},
  138. {0x40, check_destination_addr0},
  139. {0x41, check_destination_addr1},
  140. {0x42, check_destination_addr_mode},
  141. {0x43, no_check},
  142. {0x44, no_check},
  143. {0x50, no_check},
  144. {0x51, no_check},
  145. {0x52, no_check},
  146. {0x53, no_check},
  147. {0x54, no_check},
  148. {0x55, no_check},
  149. {0x56, no_check},
  150. {0x57, no_check},
  151. {0x58, no_check},
  152. {0x70, no_check},
  153. {0x71, no_check},
  154. {0x78, no_check},
  155. {0x79, no_check},
  156. {0x7A, no_check},
  157. {0x7B, no_check},
  158. {0x7C, no_check},
  159. {0x7D, check_for_vertex_count}
  160. };
  161. static hz_init_t init_table2[] = {
  162. {0xf2, check_for_header2_err},
  163. {0xf0, check_for_header1_err},
  164. {0xee, check_for_fire},
  165. {0xcc, check_for_dummy},
  166. {0x00, check_texture_addr0},
  167. {0x01, check_texture_addr0},
  168. {0x02, check_texture_addr0},
  169. {0x03, check_texture_addr0},
  170. {0x04, check_texture_addr0},
  171. {0x05, check_texture_addr0},
  172. {0x06, check_texture_addr0},
  173. {0x07, check_texture_addr0},
  174. {0x08, check_texture_addr0},
  175. {0x09, check_texture_addr0},
  176. {0x20, check_texture_addr1},
  177. {0x21, check_texture_addr1},
  178. {0x22, check_texture_addr1},
  179. {0x23, check_texture_addr4},
  180. {0x2B, check_texture_addr3},
  181. {0x2C, check_texture_addr3},
  182. {0x2D, check_texture_addr3},
  183. {0x2E, check_texture_addr3},
  184. {0x2F, check_texture_addr3},
  185. {0x30, check_texture_addr3},
  186. {0x31, check_texture_addr3},
  187. {0x32, check_texture_addr3},
  188. {0x33, check_texture_addr3},
  189. {0x34, check_texture_addr3},
  190. {0x4B, check_texture_addr5},
  191. {0x4C, check_texture_addr6},
  192. {0x51, check_texture_addr7},
  193. {0x52, check_texture_addr8},
  194. {0x77, check_texture_addr2},
  195. {0x78, no_check},
  196. {0x79, no_check},
  197. {0x7A, no_check},
  198. {0x7B, check_texture_addr_mode},
  199. {0x7C, no_check},
  200. {0x7D, no_check},
  201. {0x7E, no_check},
  202. {0x7F, no_check},
  203. {0x80, no_check},
  204. {0x81, no_check},
  205. {0x82, no_check},
  206. {0x83, no_check},
  207. {0x85, no_check},
  208. {0x86, no_check},
  209. {0x87, no_check},
  210. {0x88, no_check},
  211. {0x89, no_check},
  212. {0x8A, no_check},
  213. {0x90, no_check},
  214. {0x91, no_check},
  215. {0x92, no_check},
  216. {0x93, no_check}
  217. };
  218. static hz_init_t init_table3[] = {
  219. {0xf2, check_for_header2_err},
  220. {0xf0, check_for_header1_err},
  221. {0xcc, check_for_dummy},
  222. {0x00, check_number_texunits}
  223. };
  224. static hazard_t table1[256];
  225. static hazard_t table2[256];
  226. static hazard_t table3[256];
  227. static __inline__ int
  228. eat_words(const uint32_t **buf, const uint32_t *buf_end, unsigned num_words)
  229. {
  230. if ((buf_end - *buf) >= num_words) {
  231. *buf += num_words;
  232. return 0;
  233. }
  234. DRM_ERROR("Illegal termination of DMA command buffer\n");
  235. return 1;
  236. }
  237. /*
  238. * Partially stolen from drm_memory.h
  239. */
  240. static __inline__ drm_local_map_t *via_drm_lookup_agp_map(drm_via_state_t *seq,
  241. unsigned long offset,
  242. unsigned long size,
  243. struct drm_device *dev)
  244. {
  245. struct drm_map_list *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_entry(r_list, &dev->maplist, head) {
  252. map = r_list->map;
  253. if (!map)
  254. continue;
  255. if (map->offset <= offset
  256. && (offset + size) <= (map->offset + map->size)
  257. && !(map->flags & _DRM_RESTRICTED)
  258. && (map->type == _DRM_AGP)) {
  259. seq->map_cache = map;
  260. return map;
  261. }
  262. }
  263. return NULL;
  264. }
  265. /*
  266. * Require that all AGP texture levels reside in the same AGP map which should
  267. * be mappable by the client. This is not a big restriction.
  268. * FIXME: To actually enforce this security policy strictly, drm_rmmap
  269. * would have to wait for dma quiescent before removing an AGP map.
  270. * The via_drm_lookup_agp_map call in reality seems to take
  271. * very little CPU time.
  272. */
  273. static __inline__ int finish_current_sequence(drm_via_state_t * cur_seq)
  274. {
  275. switch (cur_seq->unfinished) {
  276. case z_address:
  277. DRM_DEBUG("Z Buffer start address is 0x%x\n", cur_seq->z_addr);
  278. break;
  279. case dest_address:
  280. DRM_DEBUG("Destination start address is 0x%x\n",
  281. cur_seq->d_addr);
  282. break;
  283. case tex_address:
  284. if (cur_seq->agp_texture) {
  285. unsigned start =
  286. cur_seq->tex_level_lo[cur_seq->texture];
  287. unsigned end = cur_seq->tex_level_hi[cur_seq->texture];
  288. unsigned long lo = ~0, hi = 0, tmp;
  289. uint32_t *addr, *pitch, *height, tex;
  290. unsigned i;
  291. int npot;
  292. if (end > 9)
  293. end = 9;
  294. if (start > 9)
  295. start = 9;
  296. addr =
  297. &(cur_seq->t_addr[tex = cur_seq->texture][start]);
  298. pitch = &(cur_seq->pitch[tex][start]);
  299. height = &(cur_seq->height[tex][start]);
  300. npot = cur_seq->tex_npot[tex];
  301. for (i = start; i <= end; ++i) {
  302. tmp = *addr++;
  303. if (tmp < lo)
  304. lo = tmp;
  305. if (i == 0 && npot)
  306. tmp += (*height++ * *pitch++);
  307. else
  308. tmp += (*height++ << *pitch++);
  309. if (tmp > hi)
  310. hi = tmp;
  311. }
  312. if (!via_drm_lookup_agp_map
  313. (cur_seq, lo, hi - lo, cur_seq->dev)) {
  314. DRM_ERROR
  315. ("AGP texture is not in allowed map\n");
  316. return 2;
  317. }
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. cur_seq->unfinished = no_sequence;
  324. return 0;
  325. }
  326. static __inline__ int
  327. investigate_hazard(uint32_t cmd, hazard_t hz, drm_via_state_t *cur_seq)
  328. {
  329. register uint32_t tmp, *tmp_addr;
  330. if (cur_seq->unfinished && (cur_seq->unfinished != seqs[hz])) {
  331. int ret;
  332. if ((ret = finish_current_sequence(cur_seq)))
  333. return ret;
  334. }
  335. switch (hz) {
  336. case check_for_header2:
  337. if (cmd == HALCYON_HEADER2)
  338. return 1;
  339. return 0;
  340. case check_for_header1:
  341. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  342. return 1;
  343. return 0;
  344. case check_for_header2_err:
  345. if (cmd == HALCYON_HEADER2)
  346. return 1;
  347. DRM_ERROR("Illegal DMA HALCYON_HEADER2 command\n");
  348. break;
  349. case check_for_header1_err:
  350. if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  351. return 1;
  352. DRM_ERROR("Illegal DMA HALCYON_HEADER1 command\n");
  353. break;
  354. case check_for_fire:
  355. if ((cmd & HALCYON_FIREMASK) == HALCYON_FIRECMD)
  356. return 1;
  357. DRM_ERROR("Illegal DMA HALCYON_FIRECMD command\n");
  358. break;
  359. case check_for_dummy:
  360. if (HC_DUMMY == cmd)
  361. return 0;
  362. DRM_ERROR("Illegal DMA HC_DUMMY command\n");
  363. break;
  364. case check_for_dd:
  365. if (0xdddddddd == cmd)
  366. return 0;
  367. DRM_ERROR("Illegal DMA 0xdddddddd command\n");
  368. break;
  369. case check_z_buffer_addr0:
  370. cur_seq->unfinished = z_address;
  371. cur_seq->z_addr = (cur_seq->z_addr & 0xFF000000) |
  372. (cmd & 0x00FFFFFF);
  373. return 0;
  374. case check_z_buffer_addr1:
  375. cur_seq->unfinished = z_address;
  376. cur_seq->z_addr = (cur_seq->z_addr & 0x00FFFFFF) |
  377. ((cmd & 0xFF) << 24);
  378. return 0;
  379. case check_z_buffer_addr_mode:
  380. cur_seq->unfinished = z_address;
  381. if ((cmd & 0x0000C000) == 0)
  382. return 0;
  383. DRM_ERROR("Attempt to place Z buffer in system memory\n");
  384. return 2;
  385. case check_destination_addr0:
  386. cur_seq->unfinished = dest_address;
  387. cur_seq->d_addr = (cur_seq->d_addr & 0xFF000000) |
  388. (cmd & 0x00FFFFFF);
  389. return 0;
  390. case check_destination_addr1:
  391. cur_seq->unfinished = dest_address;
  392. cur_seq->d_addr = (cur_seq->d_addr & 0x00FFFFFF) |
  393. ((cmd & 0xFF) << 24);
  394. return 0;
  395. case check_destination_addr_mode:
  396. cur_seq->unfinished = dest_address;
  397. if ((cmd & 0x0000C000) == 0)
  398. return 0;
  399. DRM_ERROR
  400. ("Attempt to place 3D drawing buffer in system memory\n");
  401. return 2;
  402. case check_texture_addr0:
  403. cur_seq->unfinished = tex_address;
  404. tmp = (cmd >> 24);
  405. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  406. *tmp_addr = (*tmp_addr & 0xFF000000) | (cmd & 0x00FFFFFF);
  407. return 0;
  408. case check_texture_addr1:
  409. cur_seq->unfinished = tex_address;
  410. tmp = ((cmd >> 24) - 0x20);
  411. tmp += tmp << 1;
  412. tmp_addr = &cur_seq->t_addr[cur_seq->texture][tmp];
  413. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  414. tmp_addr++;
  415. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF00) << 16);
  416. tmp_addr++;
  417. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF0000) << 8);
  418. return 0;
  419. case check_texture_addr2:
  420. cur_seq->unfinished = tex_address;
  421. cur_seq->tex_level_lo[tmp = cur_seq->texture] = cmd & 0x3F;
  422. cur_seq->tex_level_hi[tmp] = (cmd & 0xFC0) >> 6;
  423. return 0;
  424. case check_texture_addr3:
  425. cur_seq->unfinished = tex_address;
  426. tmp = ((cmd >> 24) - HC_SubA_HTXnL0Pit);
  427. if (tmp == 0 &&
  428. (cmd & HC_HTXnEnPit_MASK)) {
  429. cur_seq->pitch[cur_seq->texture][tmp] =
  430. (cmd & HC_HTXnLnPit_MASK);
  431. cur_seq->tex_npot[cur_seq->texture] = 1;
  432. } else {
  433. cur_seq->pitch[cur_seq->texture][tmp] =
  434. (cmd & HC_HTXnLnPitE_MASK) >> HC_HTXnLnPitE_SHIFT;
  435. cur_seq->tex_npot[cur_seq->texture] = 0;
  436. if (cmd & 0x000FFFFF) {
  437. DRM_ERROR
  438. ("Unimplemented texture level 0 pitch mode.\n");
  439. return 2;
  440. }
  441. }
  442. return 0;
  443. case check_texture_addr4:
  444. cur_seq->unfinished = tex_address;
  445. tmp_addr = &cur_seq->t_addr[cur_seq->texture][9];
  446. *tmp_addr = (*tmp_addr & 0x00FFFFFF) | ((cmd & 0xFF) << 24);
  447. return 0;
  448. case check_texture_addr5:
  449. case check_texture_addr6:
  450. cur_seq->unfinished = tex_address;
  451. /*
  452. * Texture width. We don't care since we have the pitch.
  453. */
  454. return 0;
  455. case check_texture_addr7:
  456. cur_seq->unfinished = tex_address;
  457. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  458. tmp_addr[5] = 1 << ((cmd & 0x00F00000) >> 20);
  459. tmp_addr[4] = 1 << ((cmd & 0x000F0000) >> 16);
  460. tmp_addr[3] = 1 << ((cmd & 0x0000F000) >> 12);
  461. tmp_addr[2] = 1 << ((cmd & 0x00000F00) >> 8);
  462. tmp_addr[1] = 1 << ((cmd & 0x000000F0) >> 4);
  463. tmp_addr[0] = 1 << (cmd & 0x0000000F);
  464. return 0;
  465. case check_texture_addr8:
  466. cur_seq->unfinished = tex_address;
  467. tmp_addr = &(cur_seq->height[cur_seq->texture][0]);
  468. tmp_addr[9] = 1 << ((cmd & 0x0000F000) >> 12);
  469. tmp_addr[8] = 1 << ((cmd & 0x00000F00) >> 8);
  470. tmp_addr[7] = 1 << ((cmd & 0x000000F0) >> 4);
  471. tmp_addr[6] = 1 << (cmd & 0x0000000F);
  472. return 0;
  473. case check_texture_addr_mode:
  474. cur_seq->unfinished = tex_address;
  475. if (2 == (tmp = cmd & 0x00000003)) {
  476. DRM_ERROR
  477. ("Attempt to fetch texture from system memory.\n");
  478. return 2;
  479. }
  480. cur_seq->agp_texture = (tmp == 3);
  481. cur_seq->tex_palette_size[cur_seq->texture] =
  482. (cmd >> 16) & 0x000000007;
  483. return 0;
  484. case check_for_vertex_count:
  485. cur_seq->vertex_count = cmd & 0x0000FFFF;
  486. return 0;
  487. case check_number_texunits:
  488. cur_seq->multitex = (cmd >> 3) & 1;
  489. return 0;
  490. default:
  491. DRM_ERROR("Illegal DMA data: 0x%x\n", cmd);
  492. return 2;
  493. }
  494. return 2;
  495. }
  496. static __inline__ int
  497. via_check_prim_list(uint32_t const **buffer, const uint32_t * buf_end,
  498. drm_via_state_t *cur_seq)
  499. {
  500. drm_via_private_t *dev_priv =
  501. (drm_via_private_t *) cur_seq->dev->dev_private;
  502. uint32_t a_fire, bcmd, dw_count;
  503. int ret = 0;
  504. int have_fire;
  505. const uint32_t *buf = *buffer;
  506. while (buf < buf_end) {
  507. have_fire = 0;
  508. if ((buf_end - buf) < 2) {
  509. DRM_ERROR
  510. ("Unexpected termination of primitive list.\n");
  511. ret = 1;
  512. break;
  513. }
  514. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdB)
  515. break;
  516. bcmd = *buf++;
  517. if ((*buf & HC_ACMD_MASK) != HC_ACMD_HCmdA) {
  518. DRM_ERROR("Expected Vertex List A command, got 0x%x\n",
  519. *buf);
  520. ret = 1;
  521. break;
  522. }
  523. a_fire =
  524. *buf++ | HC_HPLEND_MASK | HC_HPMValidN_MASK |
  525. HC_HE3Fire_MASK;
  526. /*
  527. * How many dwords per vertex ?
  528. */
  529. if (cur_seq->agp && ((bcmd & (0xF << 11)) == 0)) {
  530. DRM_ERROR("Illegal B command vertex data for AGP.\n");
  531. ret = 1;
  532. break;
  533. }
  534. dw_count = 0;
  535. if (bcmd & (1 << 7))
  536. dw_count += (cur_seq->multitex) ? 2 : 1;
  537. if (bcmd & (1 << 8))
  538. dw_count += (cur_seq->multitex) ? 2 : 1;
  539. if (bcmd & (1 << 9))
  540. dw_count++;
  541. if (bcmd & (1 << 10))
  542. dw_count++;
  543. if (bcmd & (1 << 11))
  544. dw_count++;
  545. if (bcmd & (1 << 12))
  546. dw_count++;
  547. if (bcmd & (1 << 13))
  548. dw_count++;
  549. if (bcmd & (1 << 14))
  550. dw_count++;
  551. while (buf < buf_end) {
  552. if (*buf == a_fire) {
  553. if (dev_priv->num_fire_offsets >=
  554. VIA_FIRE_BUF_SIZE) {
  555. DRM_ERROR("Fire offset buffer full.\n");
  556. ret = 1;
  557. break;
  558. }
  559. dev_priv->fire_offsets[dev_priv->
  560. num_fire_offsets++] =
  561. buf;
  562. have_fire = 1;
  563. buf++;
  564. if (buf < buf_end && *buf == a_fire)
  565. buf++;
  566. break;
  567. }
  568. if ((*buf == HALCYON_HEADER2) ||
  569. ((*buf & HALCYON_FIREMASK) == HALCYON_FIRECMD)) {
  570. DRM_ERROR("Missing Vertex Fire command, "
  571. "Stray Vertex Fire command or verifier "
  572. "lost sync.\n");
  573. ret = 1;
  574. break;
  575. }
  576. if ((ret = eat_words(&buf, buf_end, dw_count)))
  577. break;
  578. }
  579. if (buf >= buf_end && !have_fire) {
  580. DRM_ERROR("Missing Vertex Fire command or verifier "
  581. "lost sync.\n");
  582. ret = 1;
  583. break;
  584. }
  585. if (cur_seq->agp && ((buf - cur_seq->buf_start) & 0x01)) {
  586. DRM_ERROR("AGP Primitive list end misaligned.\n");
  587. ret = 1;
  588. break;
  589. }
  590. }
  591. *buffer = buf;
  592. return ret;
  593. }
  594. static __inline__ verifier_state_t
  595. via_check_header2(uint32_t const **buffer, const uint32_t *buf_end,
  596. drm_via_state_t *hc_state)
  597. {
  598. uint32_t cmd;
  599. int hz_mode;
  600. hazard_t hz;
  601. const uint32_t *buf = *buffer;
  602. const hazard_t *hz_table;
  603. if ((buf_end - buf) < 2) {
  604. DRM_ERROR
  605. ("Illegal termination of DMA HALCYON_HEADER2 sequence.\n");
  606. return state_error;
  607. }
  608. buf++;
  609. cmd = (*buf++ & 0xFFFF0000) >> 16;
  610. switch (cmd) {
  611. case HC_ParaType_CmdVdata:
  612. if (via_check_prim_list(&buf, buf_end, hc_state))
  613. return state_error;
  614. *buffer = buf;
  615. return state_command;
  616. case HC_ParaType_NotTex:
  617. hz_table = table1;
  618. break;
  619. case HC_ParaType_Tex:
  620. hc_state->texture = 0;
  621. hz_table = table2;
  622. break;
  623. case (HC_ParaType_Tex | (HC_SubType_Tex1 << 8)):
  624. hc_state->texture = 1;
  625. hz_table = table2;
  626. break;
  627. case (HC_ParaType_Tex | (HC_SubType_TexGeneral << 8)):
  628. hz_table = table3;
  629. break;
  630. case HC_ParaType_Auto:
  631. if (eat_words(&buf, buf_end, 2))
  632. return state_error;
  633. *buffer = buf;
  634. return state_command;
  635. case (HC_ParaType_Palette | (HC_SubType_Stipple << 8)):
  636. if (eat_words(&buf, buf_end, 32))
  637. return state_error;
  638. *buffer = buf;
  639. return state_command;
  640. case (HC_ParaType_Palette | (HC_SubType_TexPalette0 << 8)):
  641. case (HC_ParaType_Palette | (HC_SubType_TexPalette1 << 8)):
  642. DRM_ERROR("Texture palettes are rejected because of "
  643. "lack of info how to determine their size.\n");
  644. return state_error;
  645. case (HC_ParaType_Palette | (HC_SubType_FogTable << 8)):
  646. DRM_ERROR("Fog factor palettes are rejected because of "
  647. "lack of info how to determine their size.\n");
  648. return state_error;
  649. default:
  650. /*
  651. * There are some unimplemented HC_ParaTypes here, that
  652. * need to be implemented if the Mesa driver is extended.
  653. */
  654. DRM_ERROR("Invalid or unimplemented HALCYON_HEADER2 "
  655. "DMA subcommand: 0x%x. Previous dword: 0x%x\n",
  656. cmd, *(buf - 2));
  657. *buffer = buf;
  658. return state_error;
  659. }
  660. while (buf < buf_end) {
  661. cmd = *buf++;
  662. if ((hz = hz_table[cmd >> 24])) {
  663. if ((hz_mode = investigate_hazard(cmd, hz, hc_state))) {
  664. if (hz_mode == 1) {
  665. buf--;
  666. break;
  667. }
  668. return state_error;
  669. }
  670. } else if (hc_state->unfinished &&
  671. finish_current_sequence(hc_state)) {
  672. return state_error;
  673. }
  674. }
  675. if (hc_state->unfinished && finish_current_sequence(hc_state))
  676. return state_error;
  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. if (count & 3)
  845. buf += 4 - (count & 3);
  846. *buffer = buf;
  847. return state_command;
  848. }
  849. static __inline__ verifier_state_t
  850. via_check_vheader6(uint32_t const **buffer, const uint32_t * buf_end)
  851. {
  852. uint32_t data;
  853. const uint32_t *buf = *buffer;
  854. uint32_t i;
  855. if (buf_end - buf < 4) {
  856. DRM_ERROR("Illegal termination of video header6 command\n");
  857. return state_error;
  858. }
  859. buf++;
  860. data = *buf++;
  861. if (*buf++ != 0x00F60000) {
  862. DRM_ERROR("Illegal header6 header data\n");
  863. return state_error;
  864. }
  865. if (*buf++ != 0x00000000) {
  866. DRM_ERROR("Illegal header6 header data\n");
  867. return state_error;
  868. }
  869. if ((buf_end - buf) < (data << 1)) {
  870. DRM_ERROR("Illegal termination of video header6 command\n");
  871. return state_error;
  872. }
  873. for (i = 0; i < data; ++i) {
  874. if (verify_mmio_address(*buf++))
  875. return state_error;
  876. buf++;
  877. }
  878. data <<= 1;
  879. if ((data & 3) && verify_video_tail(&buf, buf_end, 4 - (data & 3)))
  880. return state_error;
  881. *buffer = buf;
  882. return state_command;
  883. }
  884. static __inline__ verifier_state_t
  885. via_parse_vheader6(drm_via_private_t *dev_priv, uint32_t const **buffer,
  886. const uint32_t *buf_end)
  887. {
  888. uint32_t addr, count, i;
  889. const uint32_t *buf = *buffer;
  890. i = count = *++buf;
  891. buf += 3;
  892. while (i--) {
  893. addr = *buf++;
  894. VIA_WRITE(addr, *buf++);
  895. }
  896. count <<= 1;
  897. if (count & 3)
  898. buf += 4 - (count & 3);
  899. *buffer = buf;
  900. return state_command;
  901. }
  902. int
  903. via_verify_command_stream(const uint32_t * buf, unsigned int size,
  904. struct drm_device * dev, int agp)
  905. {
  906. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  907. drm_via_state_t *hc_state = &dev_priv->hc_state;
  908. drm_via_state_t saved_state = *hc_state;
  909. uint32_t cmd;
  910. const uint32_t *buf_end = buf + (size >> 2);
  911. verifier_state_t state = state_command;
  912. int cme_video;
  913. int supported_3d;
  914. cme_video = (dev_priv->chipset == VIA_PRO_GROUP_A ||
  915. dev_priv->chipset == VIA_DX9_0);
  916. supported_3d = dev_priv->chipset != VIA_DX9_0;
  917. hc_state->dev = dev;
  918. hc_state->unfinished = no_sequence;
  919. hc_state->map_cache = NULL;
  920. hc_state->agp = agp;
  921. hc_state->buf_start = buf;
  922. dev_priv->num_fire_offsets = 0;
  923. while (buf < buf_end) {
  924. switch (state) {
  925. case state_header2:
  926. state = via_check_header2(&buf, buf_end, hc_state);
  927. break;
  928. case state_header1:
  929. state = via_check_header1(&buf, buf_end);
  930. break;
  931. case state_vheader5:
  932. state = via_check_vheader5(&buf, buf_end);
  933. break;
  934. case state_vheader6:
  935. state = via_check_vheader6(&buf, buf_end);
  936. break;
  937. case state_command:
  938. if ((HALCYON_HEADER2 == (cmd = *buf)) &&
  939. supported_3d)
  940. state = state_header2;
  941. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  942. state = state_header1;
  943. else if (cme_video
  944. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  945. state = state_vheader5;
  946. else if (cme_video
  947. && (cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  948. state = state_vheader6;
  949. else if ((cmd == HALCYON_HEADER2) && !supported_3d) {
  950. DRM_ERROR("Accelerated 3D is not supported on this chipset yet.\n");
  951. state = state_error;
  952. } else {
  953. DRM_ERROR
  954. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  955. cmd);
  956. state = state_error;
  957. }
  958. break;
  959. case state_error:
  960. default:
  961. *hc_state = saved_state;
  962. return -EINVAL;
  963. }
  964. }
  965. if (state == state_error) {
  966. *hc_state = saved_state;
  967. return -EINVAL;
  968. }
  969. return 0;
  970. }
  971. int
  972. via_parse_command_stream(struct drm_device *dev, const uint32_t *buf,
  973. unsigned int size)
  974. {
  975. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  976. uint32_t cmd;
  977. const uint32_t *buf_end = buf + (size >> 2);
  978. verifier_state_t state = state_command;
  979. int fire_count = 0;
  980. while (buf < buf_end) {
  981. switch (state) {
  982. case state_header2:
  983. state =
  984. via_parse_header2(dev_priv, &buf, buf_end,
  985. &fire_count);
  986. break;
  987. case state_header1:
  988. state = via_parse_header1(dev_priv, &buf, buf_end);
  989. break;
  990. case state_vheader5:
  991. state = via_parse_vheader5(dev_priv, &buf, buf_end);
  992. break;
  993. case state_vheader6:
  994. state = via_parse_vheader6(dev_priv, &buf, buf_end);
  995. break;
  996. case state_command:
  997. if (HALCYON_HEADER2 == (cmd = *buf))
  998. state = state_header2;
  999. else if ((cmd & HALCYON_HEADER1MASK) == HALCYON_HEADER1)
  1000. state = state_header1;
  1001. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER5)
  1002. state = state_vheader5;
  1003. else if ((cmd & VIA_VIDEOMASK) == VIA_VIDEO_HEADER6)
  1004. state = state_vheader6;
  1005. else {
  1006. DRM_ERROR
  1007. ("Invalid / Unimplemented DMA HEADER command. 0x%x\n",
  1008. cmd);
  1009. state = state_error;
  1010. }
  1011. break;
  1012. case state_error:
  1013. default:
  1014. return -EINVAL;
  1015. }
  1016. }
  1017. if (state == state_error)
  1018. return -EINVAL;
  1019. return 0;
  1020. }
  1021. static void
  1022. setup_hazard_table(hz_init_t init_table[], hazard_t table[], int size)
  1023. {
  1024. int i;
  1025. for (i = 0; i < 256; ++i)
  1026. table[i] = forbidden_command;
  1027. for (i = 0; i < size; ++i)
  1028. table[init_table[i].code] = init_table[i].hz;
  1029. }
  1030. void via_init_command_verifier(void)
  1031. {
  1032. setup_hazard_table(init_table1, table1, ARRAY_SIZE(init_table1));
  1033. setup_hazard_table(init_table2, table2, ARRAY_SIZE(init_table2));
  1034. setup_hazard_table(init_table3, table3, ARRAY_SIZE(init_table3));
  1035. }