savage_state.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* savage_state.c -- State and drawing support for Savage
  2. *
  3. * Copyright 2004 Felix Kuehling
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
  21. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/savage_drm.h>
  27. #include "savage_drv.h"
  28. void savage_emit_clip_rect_s3d(drm_savage_private_t * dev_priv,
  29. const struct drm_clip_rect * pbox)
  30. {
  31. uint32_t scstart = dev_priv->state.s3d.new_scstart;
  32. uint32_t scend = dev_priv->state.s3d.new_scend;
  33. scstart = (scstart & ~SAVAGE_SCISSOR_MASK_S3D) |
  34. ((uint32_t) pbox->x1 & 0x000007ff) |
  35. (((uint32_t) pbox->y1 << 16) & 0x07ff0000);
  36. scend = (scend & ~SAVAGE_SCISSOR_MASK_S3D) |
  37. (((uint32_t) pbox->x2 - 1) & 0x000007ff) |
  38. ((((uint32_t) pbox->y2 - 1) << 16) & 0x07ff0000);
  39. if (scstart != dev_priv->state.s3d.scstart ||
  40. scend != dev_priv->state.s3d.scend) {
  41. DMA_LOCALS;
  42. BEGIN_DMA(4);
  43. DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
  44. DMA_SET_REGISTERS(SAVAGE_SCSTART_S3D, 2);
  45. DMA_WRITE(scstart);
  46. DMA_WRITE(scend);
  47. dev_priv->state.s3d.scstart = scstart;
  48. dev_priv->state.s3d.scend = scend;
  49. dev_priv->waiting = 1;
  50. DMA_COMMIT();
  51. }
  52. }
  53. void savage_emit_clip_rect_s4(drm_savage_private_t * dev_priv,
  54. const struct drm_clip_rect * pbox)
  55. {
  56. uint32_t drawctrl0 = dev_priv->state.s4.new_drawctrl0;
  57. uint32_t drawctrl1 = dev_priv->state.s4.new_drawctrl1;
  58. drawctrl0 = (drawctrl0 & ~SAVAGE_SCISSOR_MASK_S4) |
  59. ((uint32_t) pbox->x1 & 0x000007ff) |
  60. (((uint32_t) pbox->y1 << 12) & 0x00fff000);
  61. drawctrl1 = (drawctrl1 & ~SAVAGE_SCISSOR_MASK_S4) |
  62. (((uint32_t) pbox->x2 - 1) & 0x000007ff) |
  63. ((((uint32_t) pbox->y2 - 1) << 12) & 0x00fff000);
  64. if (drawctrl0 != dev_priv->state.s4.drawctrl0 ||
  65. drawctrl1 != dev_priv->state.s4.drawctrl1) {
  66. DMA_LOCALS;
  67. BEGIN_DMA(4);
  68. DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
  69. DMA_SET_REGISTERS(SAVAGE_DRAWCTRL0_S4, 2);
  70. DMA_WRITE(drawctrl0);
  71. DMA_WRITE(drawctrl1);
  72. dev_priv->state.s4.drawctrl0 = drawctrl0;
  73. dev_priv->state.s4.drawctrl1 = drawctrl1;
  74. dev_priv->waiting = 1;
  75. DMA_COMMIT();
  76. }
  77. }
  78. static int savage_verify_texaddr(drm_savage_private_t * dev_priv, int unit,
  79. uint32_t addr)
  80. {
  81. if ((addr & 6) != 2) { /* reserved bits */
  82. DRM_ERROR("bad texAddr%d %08x (reserved bits)\n", unit, addr);
  83. return -EINVAL;
  84. }
  85. if (!(addr & 1)) { /* local */
  86. addr &= ~7;
  87. if (addr < dev_priv->texture_offset ||
  88. addr >= dev_priv->texture_offset + dev_priv->texture_size) {
  89. DRM_ERROR
  90. ("bad texAddr%d %08x (local addr out of range)\n",
  91. unit, addr);
  92. return -EINVAL;
  93. }
  94. } else { /* AGP */
  95. if (!dev_priv->agp_textures) {
  96. DRM_ERROR("bad texAddr%d %08x (AGP not available)\n",
  97. unit, addr);
  98. return -EINVAL;
  99. }
  100. addr &= ~7;
  101. if (addr < dev_priv->agp_textures->offset ||
  102. addr >= (dev_priv->agp_textures->offset +
  103. dev_priv->agp_textures->size)) {
  104. DRM_ERROR
  105. ("bad texAddr%d %08x (AGP addr out of range)\n",
  106. unit, addr);
  107. return -EINVAL;
  108. }
  109. }
  110. return 0;
  111. }
  112. #define SAVE_STATE(reg,where) \
  113. if(start <= reg && start+count > reg) \
  114. dev_priv->state.where = regs[reg - start]
  115. #define SAVE_STATE_MASK(reg,where,mask) do { \
  116. if(start <= reg && start+count > reg) { \
  117. uint32_t tmp; \
  118. tmp = regs[reg - start]; \
  119. dev_priv->state.where = (tmp & (mask)) | \
  120. (dev_priv->state.where & ~(mask)); \
  121. } \
  122. } while (0)
  123. static int savage_verify_state_s3d(drm_savage_private_t * dev_priv,
  124. unsigned int start, unsigned int count,
  125. const uint32_t *regs)
  126. {
  127. if (start < SAVAGE_TEXPALADDR_S3D ||
  128. start + count - 1 > SAVAGE_DESTTEXRWWATERMARK_S3D) {
  129. DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
  130. start, start + count - 1);
  131. return -EINVAL;
  132. }
  133. SAVE_STATE_MASK(SAVAGE_SCSTART_S3D, s3d.new_scstart,
  134. ~SAVAGE_SCISSOR_MASK_S3D);
  135. SAVE_STATE_MASK(SAVAGE_SCEND_S3D, s3d.new_scend,
  136. ~SAVAGE_SCISSOR_MASK_S3D);
  137. /* if any texture regs were changed ... */
  138. if (start <= SAVAGE_TEXCTRL_S3D &&
  139. start + count > SAVAGE_TEXPALADDR_S3D) {
  140. /* ... check texture state */
  141. SAVE_STATE(SAVAGE_TEXCTRL_S3D, s3d.texctrl);
  142. SAVE_STATE(SAVAGE_TEXADDR_S3D, s3d.texaddr);
  143. if (dev_priv->state.s3d.texctrl & SAVAGE_TEXCTRL_TEXEN_MASK)
  144. return savage_verify_texaddr(dev_priv, 0,
  145. dev_priv->state.s3d.texaddr);
  146. }
  147. return 0;
  148. }
  149. static int savage_verify_state_s4(drm_savage_private_t * dev_priv,
  150. unsigned int start, unsigned int count,
  151. const uint32_t *regs)
  152. {
  153. int ret = 0;
  154. if (start < SAVAGE_DRAWLOCALCTRL_S4 ||
  155. start + count - 1 > SAVAGE_TEXBLENDCOLOR_S4) {
  156. DRM_ERROR("invalid register range (0x%04x-0x%04x)\n",
  157. start, start + count - 1);
  158. return -EINVAL;
  159. }
  160. SAVE_STATE_MASK(SAVAGE_DRAWCTRL0_S4, s4.new_drawctrl0,
  161. ~SAVAGE_SCISSOR_MASK_S4);
  162. SAVE_STATE_MASK(SAVAGE_DRAWCTRL1_S4, s4.new_drawctrl1,
  163. ~SAVAGE_SCISSOR_MASK_S4);
  164. /* if any texture regs were changed ... */
  165. if (start <= SAVAGE_TEXDESCR_S4 &&
  166. start + count > SAVAGE_TEXPALADDR_S4) {
  167. /* ... check texture state */
  168. SAVE_STATE(SAVAGE_TEXDESCR_S4, s4.texdescr);
  169. SAVE_STATE(SAVAGE_TEXADDR0_S4, s4.texaddr0);
  170. SAVE_STATE(SAVAGE_TEXADDR1_S4, s4.texaddr1);
  171. if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX0EN_MASK)
  172. ret |= savage_verify_texaddr(dev_priv, 0,
  173. dev_priv->state.s4.texaddr0);
  174. if (dev_priv->state.s4.texdescr & SAVAGE_TEXDESCR_TEX1EN_MASK)
  175. ret |= savage_verify_texaddr(dev_priv, 1,
  176. dev_priv->state.s4.texaddr1);
  177. }
  178. return ret;
  179. }
  180. #undef SAVE_STATE
  181. #undef SAVE_STATE_MASK
  182. static int savage_dispatch_state(drm_savage_private_t * dev_priv,
  183. const drm_savage_cmd_header_t * cmd_header,
  184. const uint32_t *regs)
  185. {
  186. unsigned int count = cmd_header->state.count;
  187. unsigned int start = cmd_header->state.start;
  188. unsigned int count2 = 0;
  189. unsigned int bci_size;
  190. int ret;
  191. DMA_LOCALS;
  192. if (!count)
  193. return 0;
  194. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  195. ret = savage_verify_state_s3d(dev_priv, start, count, regs);
  196. if (ret != 0)
  197. return ret;
  198. /* scissor regs are emitted in savage_dispatch_draw */
  199. if (start < SAVAGE_SCSTART_S3D) {
  200. if (start + count > SAVAGE_SCEND_S3D + 1)
  201. count2 = count - (SAVAGE_SCEND_S3D + 1 - start);
  202. if (start + count > SAVAGE_SCSTART_S3D)
  203. count = SAVAGE_SCSTART_S3D - start;
  204. } else if (start <= SAVAGE_SCEND_S3D) {
  205. if (start + count > SAVAGE_SCEND_S3D + 1) {
  206. count -= SAVAGE_SCEND_S3D + 1 - start;
  207. start = SAVAGE_SCEND_S3D + 1;
  208. } else
  209. return 0;
  210. }
  211. } else {
  212. ret = savage_verify_state_s4(dev_priv, start, count, regs);
  213. if (ret != 0)
  214. return ret;
  215. /* scissor regs are emitted in savage_dispatch_draw */
  216. if (start < SAVAGE_DRAWCTRL0_S4) {
  217. if (start + count > SAVAGE_DRAWCTRL1_S4 + 1)
  218. count2 = count -
  219. (SAVAGE_DRAWCTRL1_S4 + 1 - start);
  220. if (start + count > SAVAGE_DRAWCTRL0_S4)
  221. count = SAVAGE_DRAWCTRL0_S4 - start;
  222. } else if (start <= SAVAGE_DRAWCTRL1_S4) {
  223. if (start + count > SAVAGE_DRAWCTRL1_S4 + 1) {
  224. count -= SAVAGE_DRAWCTRL1_S4 + 1 - start;
  225. start = SAVAGE_DRAWCTRL1_S4 + 1;
  226. } else
  227. return 0;
  228. }
  229. }
  230. bci_size = count + (count + 254) / 255 + count2 + (count2 + 254) / 255;
  231. if (cmd_header->state.global) {
  232. BEGIN_DMA(bci_size + 1);
  233. DMA_WRITE(BCI_CMD_WAIT | BCI_CMD_WAIT_3D);
  234. dev_priv->waiting = 1;
  235. } else {
  236. BEGIN_DMA(bci_size);
  237. }
  238. do {
  239. while (count > 0) {
  240. unsigned int n = count < 255 ? count : 255;
  241. DMA_SET_REGISTERS(start, n);
  242. DMA_COPY(regs, n);
  243. count -= n;
  244. start += n;
  245. regs += n;
  246. }
  247. start += 2;
  248. regs += 2;
  249. count = count2;
  250. count2 = 0;
  251. } while (count);
  252. DMA_COMMIT();
  253. return 0;
  254. }
  255. static int savage_dispatch_dma_prim(drm_savage_private_t * dev_priv,
  256. const drm_savage_cmd_header_t * cmd_header,
  257. const struct drm_buf * dmabuf)
  258. {
  259. unsigned char reorder = 0;
  260. unsigned int prim = cmd_header->prim.prim;
  261. unsigned int skip = cmd_header->prim.skip;
  262. unsigned int n = cmd_header->prim.count;
  263. unsigned int start = cmd_header->prim.start;
  264. unsigned int i;
  265. BCI_LOCALS;
  266. if (!dmabuf) {
  267. DRM_ERROR("called without dma buffers!\n");
  268. return -EINVAL;
  269. }
  270. if (!n)
  271. return 0;
  272. switch (prim) {
  273. case SAVAGE_PRIM_TRILIST_201:
  274. reorder = 1;
  275. prim = SAVAGE_PRIM_TRILIST;
  276. case SAVAGE_PRIM_TRILIST:
  277. if (n % 3 != 0) {
  278. DRM_ERROR("wrong number of vertices %u in TRILIST\n",
  279. n);
  280. return -EINVAL;
  281. }
  282. break;
  283. case SAVAGE_PRIM_TRISTRIP:
  284. case SAVAGE_PRIM_TRIFAN:
  285. if (n < 3) {
  286. DRM_ERROR
  287. ("wrong number of vertices %u in TRIFAN/STRIP\n",
  288. n);
  289. return -EINVAL;
  290. }
  291. break;
  292. default:
  293. DRM_ERROR("invalid primitive type %u\n", prim);
  294. return -EINVAL;
  295. }
  296. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  297. if (skip != 0) {
  298. DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
  299. return -EINVAL;
  300. }
  301. } else {
  302. unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
  303. (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
  304. (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
  305. if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
  306. DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
  307. return -EINVAL;
  308. }
  309. if (reorder) {
  310. DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
  311. return -EINVAL;
  312. }
  313. }
  314. if (start + n > dmabuf->total / 32) {
  315. DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
  316. start, start + n - 1, dmabuf->total / 32);
  317. return -EINVAL;
  318. }
  319. /* Vertex DMA doesn't work with command DMA at the same time,
  320. * so we use BCI_... to submit commands here. Flush buffered
  321. * faked DMA first. */
  322. DMA_FLUSH();
  323. if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
  324. BEGIN_BCI(2);
  325. BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
  326. BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
  327. dev_priv->state.common.vbaddr = dmabuf->bus_address;
  328. }
  329. if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
  330. /* Workaround for what looks like a hardware bug. If a
  331. * WAIT_3D_IDLE was emitted some time before the
  332. * indexed drawing command then the engine will lock
  333. * up. There are two known workarounds:
  334. * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
  335. BEGIN_BCI(63);
  336. for (i = 0; i < 63; ++i)
  337. BCI_WRITE(BCI_CMD_WAIT);
  338. dev_priv->waiting = 0;
  339. }
  340. prim <<= 25;
  341. while (n != 0) {
  342. /* Can emit up to 255 indices (85 triangles) at once. */
  343. unsigned int count = n > 255 ? 255 : n;
  344. if (reorder) {
  345. /* Need to reorder indices for correct flat
  346. * shading while preserving the clock sense
  347. * for correct culling. Only on Savage3D. */
  348. int reorder[3] = { -1, -1, -1 };
  349. reorder[start % 3] = 2;
  350. BEGIN_BCI((count + 1 + 1) / 2);
  351. BCI_DRAW_INDICES_S3D(count, prim, start + 2);
  352. for (i = start + 1; i + 1 < start + count; i += 2)
  353. BCI_WRITE((i + reorder[i % 3]) |
  354. ((i + 1 +
  355. reorder[(i + 1) % 3]) << 16));
  356. if (i < start + count)
  357. BCI_WRITE(i + reorder[i % 3]);
  358. } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  359. BEGIN_BCI((count + 1 + 1) / 2);
  360. BCI_DRAW_INDICES_S3D(count, prim, start);
  361. for (i = start + 1; i + 1 < start + count; i += 2)
  362. BCI_WRITE(i | ((i + 1) << 16));
  363. if (i < start + count)
  364. BCI_WRITE(i);
  365. } else {
  366. BEGIN_BCI((count + 2 + 1) / 2);
  367. BCI_DRAW_INDICES_S4(count, prim, skip);
  368. for (i = start; i + 1 < start + count; i += 2)
  369. BCI_WRITE(i | ((i + 1) << 16));
  370. if (i < start + count)
  371. BCI_WRITE(i);
  372. }
  373. start += count;
  374. n -= count;
  375. prim |= BCI_CMD_DRAW_CONT;
  376. }
  377. return 0;
  378. }
  379. static int savage_dispatch_vb_prim(drm_savage_private_t * dev_priv,
  380. const drm_savage_cmd_header_t * cmd_header,
  381. const uint32_t *vtxbuf, unsigned int vb_size,
  382. unsigned int vb_stride)
  383. {
  384. unsigned char reorder = 0;
  385. unsigned int prim = cmd_header->prim.prim;
  386. unsigned int skip = cmd_header->prim.skip;
  387. unsigned int n = cmd_header->prim.count;
  388. unsigned int start = cmd_header->prim.start;
  389. unsigned int vtx_size;
  390. unsigned int i;
  391. DMA_LOCALS;
  392. if (!n)
  393. return 0;
  394. switch (prim) {
  395. case SAVAGE_PRIM_TRILIST_201:
  396. reorder = 1;
  397. prim = SAVAGE_PRIM_TRILIST;
  398. case SAVAGE_PRIM_TRILIST:
  399. if (n % 3 != 0) {
  400. DRM_ERROR("wrong number of vertices %u in TRILIST\n",
  401. n);
  402. return -EINVAL;
  403. }
  404. break;
  405. case SAVAGE_PRIM_TRISTRIP:
  406. case SAVAGE_PRIM_TRIFAN:
  407. if (n < 3) {
  408. DRM_ERROR
  409. ("wrong number of vertices %u in TRIFAN/STRIP\n",
  410. n);
  411. return -EINVAL;
  412. }
  413. break;
  414. default:
  415. DRM_ERROR("invalid primitive type %u\n", prim);
  416. return -EINVAL;
  417. }
  418. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  419. if (skip > SAVAGE_SKIP_ALL_S3D) {
  420. DRM_ERROR("invalid skip flags 0x%04x\n", skip);
  421. return -EINVAL;
  422. }
  423. vtx_size = 8; /* full vertex */
  424. } else {
  425. if (skip > SAVAGE_SKIP_ALL_S4) {
  426. DRM_ERROR("invalid skip flags 0x%04x\n", skip);
  427. return -EINVAL;
  428. }
  429. vtx_size = 10; /* full vertex */
  430. }
  431. vtx_size -= (skip & 1) + (skip >> 1 & 1) +
  432. (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
  433. (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
  434. if (vtx_size > vb_stride) {
  435. DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
  436. vtx_size, vb_stride);
  437. return -EINVAL;
  438. }
  439. if (start + n > vb_size / (vb_stride * 4)) {
  440. DRM_ERROR("vertex indices (%u-%u) out of range (0-%u)\n",
  441. start, start + n - 1, vb_size / (vb_stride * 4));
  442. return -EINVAL;
  443. }
  444. prim <<= 25;
  445. while (n != 0) {
  446. /* Can emit up to 255 vertices (85 triangles) at once. */
  447. unsigned int count = n > 255 ? 255 : n;
  448. if (reorder) {
  449. /* Need to reorder vertices for correct flat
  450. * shading while preserving the clock sense
  451. * for correct culling. Only on Savage3D. */
  452. int reorder[3] = { -1, -1, -1 };
  453. reorder[start % 3] = 2;
  454. BEGIN_DMA(count * vtx_size + 1);
  455. DMA_DRAW_PRIMITIVE(count, prim, skip);
  456. for (i = start; i < start + count; ++i) {
  457. unsigned int j = i + reorder[i % 3];
  458. DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
  459. }
  460. DMA_COMMIT();
  461. } else {
  462. BEGIN_DMA(count * vtx_size + 1);
  463. DMA_DRAW_PRIMITIVE(count, prim, skip);
  464. if (vb_stride == vtx_size) {
  465. DMA_COPY(&vtxbuf[vb_stride * start],
  466. vtx_size * count);
  467. } else {
  468. for (i = start; i < start + count; ++i) {
  469. DMA_COPY(&vtxbuf [vb_stride * i],
  470. vtx_size);
  471. }
  472. }
  473. DMA_COMMIT();
  474. }
  475. start += count;
  476. n -= count;
  477. prim |= BCI_CMD_DRAW_CONT;
  478. }
  479. return 0;
  480. }
  481. static int savage_dispatch_dma_idx(drm_savage_private_t * dev_priv,
  482. const drm_savage_cmd_header_t * cmd_header,
  483. const uint16_t *idx,
  484. const struct drm_buf * dmabuf)
  485. {
  486. unsigned char reorder = 0;
  487. unsigned int prim = cmd_header->idx.prim;
  488. unsigned int skip = cmd_header->idx.skip;
  489. unsigned int n = cmd_header->idx.count;
  490. unsigned int i;
  491. BCI_LOCALS;
  492. if (!dmabuf) {
  493. DRM_ERROR("called without dma buffers!\n");
  494. return -EINVAL;
  495. }
  496. if (!n)
  497. return 0;
  498. switch (prim) {
  499. case SAVAGE_PRIM_TRILIST_201:
  500. reorder = 1;
  501. prim = SAVAGE_PRIM_TRILIST;
  502. case SAVAGE_PRIM_TRILIST:
  503. if (n % 3 != 0) {
  504. DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
  505. return -EINVAL;
  506. }
  507. break;
  508. case SAVAGE_PRIM_TRISTRIP:
  509. case SAVAGE_PRIM_TRIFAN:
  510. if (n < 3) {
  511. DRM_ERROR
  512. ("wrong number of indices %u in TRIFAN/STRIP\n", n);
  513. return -EINVAL;
  514. }
  515. break;
  516. default:
  517. DRM_ERROR("invalid primitive type %u\n", prim);
  518. return -EINVAL;
  519. }
  520. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  521. if (skip != 0) {
  522. DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
  523. return -EINVAL;
  524. }
  525. } else {
  526. unsigned int size = 10 - (skip & 1) - (skip >> 1 & 1) -
  527. (skip >> 2 & 1) - (skip >> 3 & 1) - (skip >> 4 & 1) -
  528. (skip >> 5 & 1) - (skip >> 6 & 1) - (skip >> 7 & 1);
  529. if (skip > SAVAGE_SKIP_ALL_S4 || size != 8) {
  530. DRM_ERROR("invalid skip flags 0x%04x for DMA\n", skip);
  531. return -EINVAL;
  532. }
  533. if (reorder) {
  534. DRM_ERROR("TRILIST_201 used on Savage4 hardware\n");
  535. return -EINVAL;
  536. }
  537. }
  538. /* Vertex DMA doesn't work with command DMA at the same time,
  539. * so we use BCI_... to submit commands here. Flush buffered
  540. * faked DMA first. */
  541. DMA_FLUSH();
  542. if (dmabuf->bus_address != dev_priv->state.common.vbaddr) {
  543. BEGIN_BCI(2);
  544. BCI_SET_REGISTERS(SAVAGE_VERTBUFADDR, 1);
  545. BCI_WRITE(dmabuf->bus_address | dev_priv->dma_type);
  546. dev_priv->state.common.vbaddr = dmabuf->bus_address;
  547. }
  548. if (S3_SAVAGE3D_SERIES(dev_priv->chipset) && dev_priv->waiting) {
  549. /* Workaround for what looks like a hardware bug. If a
  550. * WAIT_3D_IDLE was emitted some time before the
  551. * indexed drawing command then the engine will lock
  552. * up. There are two known workarounds:
  553. * WAIT_IDLE_EMPTY or emit at least 63 NOPs. */
  554. BEGIN_BCI(63);
  555. for (i = 0; i < 63; ++i)
  556. BCI_WRITE(BCI_CMD_WAIT);
  557. dev_priv->waiting = 0;
  558. }
  559. prim <<= 25;
  560. while (n != 0) {
  561. /* Can emit up to 255 indices (85 triangles) at once. */
  562. unsigned int count = n > 255 ? 255 : n;
  563. /* check indices */
  564. for (i = 0; i < count; ++i) {
  565. if (idx[i] > dmabuf->total / 32) {
  566. DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
  567. i, idx[i], dmabuf->total / 32);
  568. return -EINVAL;
  569. }
  570. }
  571. if (reorder) {
  572. /* Need to reorder indices for correct flat
  573. * shading while preserving the clock sense
  574. * for correct culling. Only on Savage3D. */
  575. int reorder[3] = { 2, -1, -1 };
  576. BEGIN_BCI((count + 1 + 1) / 2);
  577. BCI_DRAW_INDICES_S3D(count, prim, idx[2]);
  578. for (i = 1; i + 1 < count; i += 2)
  579. BCI_WRITE(idx[i + reorder[i % 3]] |
  580. (idx[i + 1 +
  581. reorder[(i + 1) % 3]] << 16));
  582. if (i < count)
  583. BCI_WRITE(idx[i + reorder[i % 3]]);
  584. } else if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  585. BEGIN_BCI((count + 1 + 1) / 2);
  586. BCI_DRAW_INDICES_S3D(count, prim, idx[0]);
  587. for (i = 1; i + 1 < count; i += 2)
  588. BCI_WRITE(idx[i] | (idx[i + 1] << 16));
  589. if (i < count)
  590. BCI_WRITE(idx[i]);
  591. } else {
  592. BEGIN_BCI((count + 2 + 1) / 2);
  593. BCI_DRAW_INDICES_S4(count, prim, skip);
  594. for (i = 0; i + 1 < count; i += 2)
  595. BCI_WRITE(idx[i] | (idx[i + 1] << 16));
  596. if (i < count)
  597. BCI_WRITE(idx[i]);
  598. }
  599. idx += count;
  600. n -= count;
  601. prim |= BCI_CMD_DRAW_CONT;
  602. }
  603. return 0;
  604. }
  605. static int savage_dispatch_vb_idx(drm_savage_private_t * dev_priv,
  606. const drm_savage_cmd_header_t * cmd_header,
  607. const uint16_t *idx,
  608. const uint32_t *vtxbuf,
  609. unsigned int vb_size, unsigned int vb_stride)
  610. {
  611. unsigned char reorder = 0;
  612. unsigned int prim = cmd_header->idx.prim;
  613. unsigned int skip = cmd_header->idx.skip;
  614. unsigned int n = cmd_header->idx.count;
  615. unsigned int vtx_size;
  616. unsigned int i;
  617. DMA_LOCALS;
  618. if (!n)
  619. return 0;
  620. switch (prim) {
  621. case SAVAGE_PRIM_TRILIST_201:
  622. reorder = 1;
  623. prim = SAVAGE_PRIM_TRILIST;
  624. case SAVAGE_PRIM_TRILIST:
  625. if (n % 3 != 0) {
  626. DRM_ERROR("wrong number of indices %u in TRILIST\n", n);
  627. return -EINVAL;
  628. }
  629. break;
  630. case SAVAGE_PRIM_TRISTRIP:
  631. case SAVAGE_PRIM_TRIFAN:
  632. if (n < 3) {
  633. DRM_ERROR
  634. ("wrong number of indices %u in TRIFAN/STRIP\n", n);
  635. return -EINVAL;
  636. }
  637. break;
  638. default:
  639. DRM_ERROR("invalid primitive type %u\n", prim);
  640. return -EINVAL;
  641. }
  642. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  643. if (skip > SAVAGE_SKIP_ALL_S3D) {
  644. DRM_ERROR("invalid skip flags 0x%04x\n", skip);
  645. return -EINVAL;
  646. }
  647. vtx_size = 8; /* full vertex */
  648. } else {
  649. if (skip > SAVAGE_SKIP_ALL_S4) {
  650. DRM_ERROR("invalid skip flags 0x%04x\n", skip);
  651. return -EINVAL;
  652. }
  653. vtx_size = 10; /* full vertex */
  654. }
  655. vtx_size -= (skip & 1) + (skip >> 1 & 1) +
  656. (skip >> 2 & 1) + (skip >> 3 & 1) + (skip >> 4 & 1) +
  657. (skip >> 5 & 1) + (skip >> 6 & 1) + (skip >> 7 & 1);
  658. if (vtx_size > vb_stride) {
  659. DRM_ERROR("vertex size greater than vb stride (%u > %u)\n",
  660. vtx_size, vb_stride);
  661. return -EINVAL;
  662. }
  663. prim <<= 25;
  664. while (n != 0) {
  665. /* Can emit up to 255 vertices (85 triangles) at once. */
  666. unsigned int count = n > 255 ? 255 : n;
  667. /* Check indices */
  668. for (i = 0; i < count; ++i) {
  669. if (idx[i] > vb_size / (vb_stride * 4)) {
  670. DRM_ERROR("idx[%u]=%u out of range (0-%u)\n",
  671. i, idx[i], vb_size / (vb_stride * 4));
  672. return -EINVAL;
  673. }
  674. }
  675. if (reorder) {
  676. /* Need to reorder vertices for correct flat
  677. * shading while preserving the clock sense
  678. * for correct culling. Only on Savage3D. */
  679. int reorder[3] = { 2, -1, -1 };
  680. BEGIN_DMA(count * vtx_size + 1);
  681. DMA_DRAW_PRIMITIVE(count, prim, skip);
  682. for (i = 0; i < count; ++i) {
  683. unsigned int j = idx[i + reorder[i % 3]];
  684. DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
  685. }
  686. DMA_COMMIT();
  687. } else {
  688. BEGIN_DMA(count * vtx_size + 1);
  689. DMA_DRAW_PRIMITIVE(count, prim, skip);
  690. for (i = 0; i < count; ++i) {
  691. unsigned int j = idx[i];
  692. DMA_COPY(&vtxbuf[vb_stride * j], vtx_size);
  693. }
  694. DMA_COMMIT();
  695. }
  696. idx += count;
  697. n -= count;
  698. prim |= BCI_CMD_DRAW_CONT;
  699. }
  700. return 0;
  701. }
  702. static int savage_dispatch_clear(drm_savage_private_t * dev_priv,
  703. const drm_savage_cmd_header_t * cmd_header,
  704. const drm_savage_cmd_header_t *data,
  705. unsigned int nbox,
  706. const struct drm_clip_rect *boxes)
  707. {
  708. unsigned int flags = cmd_header->clear0.flags;
  709. unsigned int clear_cmd;
  710. unsigned int i, nbufs;
  711. DMA_LOCALS;
  712. if (nbox == 0)
  713. return 0;
  714. clear_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
  715. BCI_CMD_SEND_COLOR | BCI_CMD_DEST_PBD_NEW;
  716. BCI_CMD_SET_ROP(clear_cmd, 0xCC);
  717. nbufs = ((flags & SAVAGE_FRONT) ? 1 : 0) +
  718. ((flags & SAVAGE_BACK) ? 1 : 0) + ((flags & SAVAGE_DEPTH) ? 1 : 0);
  719. if (nbufs == 0)
  720. return 0;
  721. if (data->clear1.mask != 0xffffffff) {
  722. /* set mask */
  723. BEGIN_DMA(2);
  724. DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
  725. DMA_WRITE(data->clear1.mask);
  726. DMA_COMMIT();
  727. }
  728. for (i = 0; i < nbox; ++i) {
  729. unsigned int x, y, w, h;
  730. unsigned int buf;
  731. x = boxes[i].x1, y = boxes[i].y1;
  732. w = boxes[i].x2 - boxes[i].x1;
  733. h = boxes[i].y2 - boxes[i].y1;
  734. BEGIN_DMA(nbufs * 6);
  735. for (buf = SAVAGE_FRONT; buf <= SAVAGE_DEPTH; buf <<= 1) {
  736. if (!(flags & buf))
  737. continue;
  738. DMA_WRITE(clear_cmd);
  739. switch (buf) {
  740. case SAVAGE_FRONT:
  741. DMA_WRITE(dev_priv->front_offset);
  742. DMA_WRITE(dev_priv->front_bd);
  743. break;
  744. case SAVAGE_BACK:
  745. DMA_WRITE(dev_priv->back_offset);
  746. DMA_WRITE(dev_priv->back_bd);
  747. break;
  748. case SAVAGE_DEPTH:
  749. DMA_WRITE(dev_priv->depth_offset);
  750. DMA_WRITE(dev_priv->depth_bd);
  751. break;
  752. }
  753. DMA_WRITE(data->clear1.value);
  754. DMA_WRITE(BCI_X_Y(x, y));
  755. DMA_WRITE(BCI_W_H(w, h));
  756. }
  757. DMA_COMMIT();
  758. }
  759. if (data->clear1.mask != 0xffffffff) {
  760. /* reset mask */
  761. BEGIN_DMA(2);
  762. DMA_SET_REGISTERS(SAVAGE_BITPLANEWTMASK, 1);
  763. DMA_WRITE(0xffffffff);
  764. DMA_COMMIT();
  765. }
  766. return 0;
  767. }
  768. static int savage_dispatch_swap(drm_savage_private_t * dev_priv,
  769. unsigned int nbox, const struct drm_clip_rect *boxes)
  770. {
  771. unsigned int swap_cmd;
  772. unsigned int i;
  773. DMA_LOCALS;
  774. if (nbox == 0)
  775. return 0;
  776. swap_cmd = BCI_CMD_RECT | BCI_CMD_RECT_XP | BCI_CMD_RECT_YP |
  777. BCI_CMD_SRC_PBD_COLOR_NEW | BCI_CMD_DEST_GBD;
  778. BCI_CMD_SET_ROP(swap_cmd, 0xCC);
  779. for (i = 0; i < nbox; ++i) {
  780. BEGIN_DMA(6);
  781. DMA_WRITE(swap_cmd);
  782. DMA_WRITE(dev_priv->back_offset);
  783. DMA_WRITE(dev_priv->back_bd);
  784. DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
  785. DMA_WRITE(BCI_X_Y(boxes[i].x1, boxes[i].y1));
  786. DMA_WRITE(BCI_W_H(boxes[i].x2 - boxes[i].x1,
  787. boxes[i].y2 - boxes[i].y1));
  788. DMA_COMMIT();
  789. }
  790. return 0;
  791. }
  792. static int savage_dispatch_draw(drm_savage_private_t * dev_priv,
  793. const drm_savage_cmd_header_t *start,
  794. const drm_savage_cmd_header_t *end,
  795. const struct drm_buf * dmabuf,
  796. const unsigned int *vtxbuf,
  797. unsigned int vb_size, unsigned int vb_stride,
  798. unsigned int nbox,
  799. const struct drm_clip_rect *boxes)
  800. {
  801. unsigned int i, j;
  802. int ret;
  803. for (i = 0; i < nbox; ++i) {
  804. const drm_savage_cmd_header_t *cmdbuf;
  805. dev_priv->emit_clip_rect(dev_priv, &boxes[i]);
  806. cmdbuf = start;
  807. while (cmdbuf < end) {
  808. drm_savage_cmd_header_t cmd_header;
  809. cmd_header = *cmdbuf;
  810. cmdbuf++;
  811. switch (cmd_header.cmd.cmd) {
  812. case SAVAGE_CMD_DMA_PRIM:
  813. ret = savage_dispatch_dma_prim(
  814. dev_priv, &cmd_header, dmabuf);
  815. break;
  816. case SAVAGE_CMD_VB_PRIM:
  817. ret = savage_dispatch_vb_prim(
  818. dev_priv, &cmd_header,
  819. vtxbuf, vb_size, vb_stride);
  820. break;
  821. case SAVAGE_CMD_DMA_IDX:
  822. j = (cmd_header.idx.count + 3) / 4;
  823. /* j was check in savage_bci_cmdbuf */
  824. ret = savage_dispatch_dma_idx(dev_priv,
  825. &cmd_header, (const uint16_t *)cmdbuf,
  826. dmabuf);
  827. cmdbuf += j;
  828. break;
  829. case SAVAGE_CMD_VB_IDX:
  830. j = (cmd_header.idx.count + 3) / 4;
  831. /* j was check in savage_bci_cmdbuf */
  832. ret = savage_dispatch_vb_idx(dev_priv,
  833. &cmd_header, (const uint16_t *)cmdbuf,
  834. (const uint32_t *)vtxbuf, vb_size,
  835. vb_stride);
  836. cmdbuf += j;
  837. break;
  838. default:
  839. /* What's the best return code? EFAULT? */
  840. DRM_ERROR("IMPLEMENTATION ERROR: "
  841. "non-drawing-command %d\n",
  842. cmd_header.cmd.cmd);
  843. return -EINVAL;
  844. }
  845. if (ret != 0)
  846. return ret;
  847. }
  848. }
  849. return 0;
  850. }
  851. int savage_bci_cmdbuf(struct drm_device *dev, void *data, struct drm_file *file_priv)
  852. {
  853. drm_savage_private_t *dev_priv = dev->dev_private;
  854. struct drm_device_dma *dma = dev->dma;
  855. struct drm_buf *dmabuf;
  856. drm_savage_cmdbuf_t *cmdbuf = data;
  857. drm_savage_cmd_header_t *kcmd_addr = NULL;
  858. drm_savage_cmd_header_t *first_draw_cmd;
  859. unsigned int *kvb_addr = NULL;
  860. struct drm_clip_rect *kbox_addr = NULL;
  861. unsigned int i, j;
  862. int ret = 0;
  863. DRM_DEBUG("\n");
  864. LOCK_TEST_WITH_RETURN(dev, file_priv);
  865. if (dma && dma->buflist) {
  866. if (cmdbuf->dma_idx > dma->buf_count) {
  867. DRM_ERROR
  868. ("vertex buffer index %u out of range (0-%u)\n",
  869. cmdbuf->dma_idx, dma->buf_count - 1);
  870. return -EINVAL;
  871. }
  872. dmabuf = dma->buflist[cmdbuf->dma_idx];
  873. } else {
  874. dmabuf = NULL;
  875. }
  876. /* Copy the user buffers into kernel temporary areas. This hasn't been
  877. * a performance loss compared to VERIFYAREA_READ/
  878. * COPY_FROM_USER_UNCHECKED when done in other drivers, and is correct
  879. * for locking on FreeBSD.
  880. */
  881. if (cmdbuf->size) {
  882. kcmd_addr = kmalloc_array(cmdbuf->size, 8, GFP_KERNEL);
  883. if (kcmd_addr == NULL)
  884. return -ENOMEM;
  885. if (copy_from_user(kcmd_addr, cmdbuf->cmd_addr,
  886. cmdbuf->size * 8))
  887. {
  888. kfree(kcmd_addr);
  889. return -EFAULT;
  890. }
  891. cmdbuf->cmd_addr = kcmd_addr;
  892. }
  893. if (cmdbuf->vb_size) {
  894. kvb_addr = kmalloc(cmdbuf->vb_size, GFP_KERNEL);
  895. if (kvb_addr == NULL) {
  896. ret = -ENOMEM;
  897. goto done;
  898. }
  899. if (copy_from_user(kvb_addr, cmdbuf->vb_addr,
  900. cmdbuf->vb_size)) {
  901. ret = -EFAULT;
  902. goto done;
  903. }
  904. cmdbuf->vb_addr = kvb_addr;
  905. }
  906. if (cmdbuf->nbox) {
  907. kbox_addr = kmalloc_array(cmdbuf->nbox, sizeof(struct drm_clip_rect),
  908. GFP_KERNEL);
  909. if (kbox_addr == NULL) {
  910. ret = -ENOMEM;
  911. goto done;
  912. }
  913. if (copy_from_user(kbox_addr, cmdbuf->box_addr,
  914. cmdbuf->nbox * sizeof(struct drm_clip_rect))) {
  915. ret = -EFAULT;
  916. goto done;
  917. }
  918. cmdbuf->box_addr = kbox_addr;
  919. }
  920. /* Make sure writes to DMA buffers are finished before sending
  921. * DMA commands to the graphics hardware. */
  922. mb();
  923. /* Coming from user space. Don't know if the Xserver has
  924. * emitted wait commands. Assuming the worst. */
  925. dev_priv->waiting = 1;
  926. i = 0;
  927. first_draw_cmd = NULL;
  928. while (i < cmdbuf->size) {
  929. drm_savage_cmd_header_t cmd_header;
  930. cmd_header = *(drm_savage_cmd_header_t *)cmdbuf->cmd_addr;
  931. cmdbuf->cmd_addr++;
  932. i++;
  933. /* Group drawing commands with same state to minimize
  934. * iterations over clip rects. */
  935. j = 0;
  936. switch (cmd_header.cmd.cmd) {
  937. case SAVAGE_CMD_DMA_IDX:
  938. case SAVAGE_CMD_VB_IDX:
  939. j = (cmd_header.idx.count + 3) / 4;
  940. if (i + j > cmdbuf->size) {
  941. DRM_ERROR("indexed drawing command extends "
  942. "beyond end of command buffer\n");
  943. DMA_FLUSH();
  944. ret = -EINVAL;
  945. goto done;
  946. }
  947. /* fall through */
  948. case SAVAGE_CMD_DMA_PRIM:
  949. case SAVAGE_CMD_VB_PRIM:
  950. if (!first_draw_cmd)
  951. first_draw_cmd = cmdbuf->cmd_addr - 1;
  952. cmdbuf->cmd_addr += j;
  953. i += j;
  954. break;
  955. default:
  956. if (first_draw_cmd) {
  957. ret = savage_dispatch_draw(
  958. dev_priv, first_draw_cmd,
  959. cmdbuf->cmd_addr - 1,
  960. dmabuf, cmdbuf->vb_addr, cmdbuf->vb_size,
  961. cmdbuf->vb_stride,
  962. cmdbuf->nbox, cmdbuf->box_addr);
  963. if (ret != 0)
  964. goto done;
  965. first_draw_cmd = NULL;
  966. }
  967. }
  968. if (first_draw_cmd)
  969. continue;
  970. switch (cmd_header.cmd.cmd) {
  971. case SAVAGE_CMD_STATE:
  972. j = (cmd_header.state.count + 1) / 2;
  973. if (i + j > cmdbuf->size) {
  974. DRM_ERROR("command SAVAGE_CMD_STATE extends "
  975. "beyond end of command buffer\n");
  976. DMA_FLUSH();
  977. ret = -EINVAL;
  978. goto done;
  979. }
  980. ret = savage_dispatch_state(dev_priv, &cmd_header,
  981. (const uint32_t *)cmdbuf->cmd_addr);
  982. cmdbuf->cmd_addr += j;
  983. i += j;
  984. break;
  985. case SAVAGE_CMD_CLEAR:
  986. if (i + 1 > cmdbuf->size) {
  987. DRM_ERROR("command SAVAGE_CMD_CLEAR extends "
  988. "beyond end of command buffer\n");
  989. DMA_FLUSH();
  990. ret = -EINVAL;
  991. goto done;
  992. }
  993. ret = savage_dispatch_clear(dev_priv, &cmd_header,
  994. cmdbuf->cmd_addr,
  995. cmdbuf->nbox,
  996. cmdbuf->box_addr);
  997. cmdbuf->cmd_addr++;
  998. i++;
  999. break;
  1000. case SAVAGE_CMD_SWAP:
  1001. ret = savage_dispatch_swap(dev_priv, cmdbuf->nbox,
  1002. cmdbuf->box_addr);
  1003. break;
  1004. default:
  1005. DRM_ERROR("invalid command 0x%x\n",
  1006. cmd_header.cmd.cmd);
  1007. DMA_FLUSH();
  1008. ret = -EINVAL;
  1009. goto done;
  1010. }
  1011. if (ret != 0) {
  1012. DMA_FLUSH();
  1013. goto done;
  1014. }
  1015. }
  1016. if (first_draw_cmd) {
  1017. ret = savage_dispatch_draw (
  1018. dev_priv, first_draw_cmd, cmdbuf->cmd_addr, dmabuf,
  1019. cmdbuf->vb_addr, cmdbuf->vb_size, cmdbuf->vb_stride,
  1020. cmdbuf->nbox, cmdbuf->box_addr);
  1021. if (ret != 0) {
  1022. DMA_FLUSH();
  1023. goto done;
  1024. }
  1025. }
  1026. DMA_FLUSH();
  1027. if (dmabuf && cmdbuf->discard) {
  1028. drm_savage_buf_priv_t *buf_priv = dmabuf->dev_private;
  1029. uint16_t event;
  1030. event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
  1031. SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
  1032. savage_freelist_put(dev, dmabuf);
  1033. }
  1034. done:
  1035. /* If we didn't need to allocate them, these'll be NULL */
  1036. kfree(kcmd_addr);
  1037. kfree(kvb_addr);
  1038. kfree(kbox_addr);
  1039. return ret;
  1040. }