savage_state.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 = memdup_user(cmdbuf->vb_addr, cmdbuf->vb_size);
  895. if (IS_ERR(kvb_addr)) {
  896. ret = PTR_ERR(kvb_addr);
  897. kvb_addr = NULL;
  898. goto done;
  899. }
  900. cmdbuf->vb_addr = kvb_addr;
  901. }
  902. if (cmdbuf->nbox) {
  903. kbox_addr = kmalloc_array(cmdbuf->nbox, sizeof(struct drm_clip_rect),
  904. GFP_KERNEL);
  905. if (kbox_addr == NULL) {
  906. ret = -ENOMEM;
  907. goto done;
  908. }
  909. if (copy_from_user(kbox_addr, cmdbuf->box_addr,
  910. cmdbuf->nbox * sizeof(struct drm_clip_rect))) {
  911. ret = -EFAULT;
  912. goto done;
  913. }
  914. cmdbuf->box_addr = kbox_addr;
  915. }
  916. /* Make sure writes to DMA buffers are finished before sending
  917. * DMA commands to the graphics hardware. */
  918. mb();
  919. /* Coming from user space. Don't know if the Xserver has
  920. * emitted wait commands. Assuming the worst. */
  921. dev_priv->waiting = 1;
  922. i = 0;
  923. first_draw_cmd = NULL;
  924. while (i < cmdbuf->size) {
  925. drm_savage_cmd_header_t cmd_header;
  926. cmd_header = *(drm_savage_cmd_header_t *)cmdbuf->cmd_addr;
  927. cmdbuf->cmd_addr++;
  928. i++;
  929. /* Group drawing commands with same state to minimize
  930. * iterations over clip rects. */
  931. j = 0;
  932. switch (cmd_header.cmd.cmd) {
  933. case SAVAGE_CMD_DMA_IDX:
  934. case SAVAGE_CMD_VB_IDX:
  935. j = (cmd_header.idx.count + 3) / 4;
  936. if (i + j > cmdbuf->size) {
  937. DRM_ERROR("indexed drawing command extends "
  938. "beyond end of command buffer\n");
  939. DMA_FLUSH();
  940. ret = -EINVAL;
  941. goto done;
  942. }
  943. /* fall through */
  944. case SAVAGE_CMD_DMA_PRIM:
  945. case SAVAGE_CMD_VB_PRIM:
  946. if (!first_draw_cmd)
  947. first_draw_cmd = cmdbuf->cmd_addr - 1;
  948. cmdbuf->cmd_addr += j;
  949. i += j;
  950. break;
  951. default:
  952. if (first_draw_cmd) {
  953. ret = savage_dispatch_draw(
  954. dev_priv, first_draw_cmd,
  955. cmdbuf->cmd_addr - 1,
  956. dmabuf, cmdbuf->vb_addr, cmdbuf->vb_size,
  957. cmdbuf->vb_stride,
  958. cmdbuf->nbox, cmdbuf->box_addr);
  959. if (ret != 0)
  960. goto done;
  961. first_draw_cmd = NULL;
  962. }
  963. }
  964. if (first_draw_cmd)
  965. continue;
  966. switch (cmd_header.cmd.cmd) {
  967. case SAVAGE_CMD_STATE:
  968. j = (cmd_header.state.count + 1) / 2;
  969. if (i + j > cmdbuf->size) {
  970. DRM_ERROR("command SAVAGE_CMD_STATE extends "
  971. "beyond end of command buffer\n");
  972. DMA_FLUSH();
  973. ret = -EINVAL;
  974. goto done;
  975. }
  976. ret = savage_dispatch_state(dev_priv, &cmd_header,
  977. (const uint32_t *)cmdbuf->cmd_addr);
  978. cmdbuf->cmd_addr += j;
  979. i += j;
  980. break;
  981. case SAVAGE_CMD_CLEAR:
  982. if (i + 1 > cmdbuf->size) {
  983. DRM_ERROR("command SAVAGE_CMD_CLEAR extends "
  984. "beyond end of command buffer\n");
  985. DMA_FLUSH();
  986. ret = -EINVAL;
  987. goto done;
  988. }
  989. ret = savage_dispatch_clear(dev_priv, &cmd_header,
  990. cmdbuf->cmd_addr,
  991. cmdbuf->nbox,
  992. cmdbuf->box_addr);
  993. cmdbuf->cmd_addr++;
  994. i++;
  995. break;
  996. case SAVAGE_CMD_SWAP:
  997. ret = savage_dispatch_swap(dev_priv, cmdbuf->nbox,
  998. cmdbuf->box_addr);
  999. break;
  1000. default:
  1001. DRM_ERROR("invalid command 0x%x\n",
  1002. cmd_header.cmd.cmd);
  1003. DMA_FLUSH();
  1004. ret = -EINVAL;
  1005. goto done;
  1006. }
  1007. if (ret != 0) {
  1008. DMA_FLUSH();
  1009. goto done;
  1010. }
  1011. }
  1012. if (first_draw_cmd) {
  1013. ret = savage_dispatch_draw (
  1014. dev_priv, first_draw_cmd, cmdbuf->cmd_addr, dmabuf,
  1015. cmdbuf->vb_addr, cmdbuf->vb_size, cmdbuf->vb_stride,
  1016. cmdbuf->nbox, cmdbuf->box_addr);
  1017. if (ret != 0) {
  1018. DMA_FLUSH();
  1019. goto done;
  1020. }
  1021. }
  1022. DMA_FLUSH();
  1023. if (dmabuf && cmdbuf->discard) {
  1024. drm_savage_buf_priv_t *buf_priv = dmabuf->dev_private;
  1025. uint16_t event;
  1026. event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
  1027. SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
  1028. savage_freelist_put(dev, dmabuf);
  1029. }
  1030. done:
  1031. /* If we didn't need to allocate them, these'll be NULL */
  1032. kfree(kcmd_addr);
  1033. kfree(kvb_addr);
  1034. kfree(kbox_addr);
  1035. return ret;
  1036. }