r128_cce.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /* r128_cce.c -- ATI Rage 128 driver -*- linux-c -*-
  2. * Created: Wed Apr 5 19:24:19 2000 by kevin@precisioninsight.com
  3. */
  4. /*
  5. * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
  6. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  7. * All Rights Reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Gareth Hughes <gareth@valinux.com>
  30. */
  31. #include <linux/firmware.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/module.h>
  35. #include "drmP.h"
  36. #include "drm.h"
  37. #include "r128_drm.h"
  38. #include "r128_drv.h"
  39. #define R128_FIFO_DEBUG 0
  40. #define FIRMWARE_NAME "r128/r128_cce.bin"
  41. MODULE_FIRMWARE(FIRMWARE_NAME);
  42. static int R128_READ_PLL(struct drm_device *dev, int addr)
  43. {
  44. drm_r128_private_t *dev_priv = dev->dev_private;
  45. R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f);
  46. return R128_READ(R128_CLOCK_CNTL_DATA);
  47. }
  48. #if R128_FIFO_DEBUG
  49. static void r128_status(drm_r128_private_t *dev_priv)
  50. {
  51. printk("GUI_STAT = 0x%08x\n",
  52. (unsigned int)R128_READ(R128_GUI_STAT));
  53. printk("PM4_STAT = 0x%08x\n",
  54. (unsigned int)R128_READ(R128_PM4_STAT));
  55. printk("PM4_BUFFER_DL_WPTR = 0x%08x\n",
  56. (unsigned int)R128_READ(R128_PM4_BUFFER_DL_WPTR));
  57. printk("PM4_BUFFER_DL_RPTR = 0x%08x\n",
  58. (unsigned int)R128_READ(R128_PM4_BUFFER_DL_RPTR));
  59. printk("PM4_MICRO_CNTL = 0x%08x\n",
  60. (unsigned int)R128_READ(R128_PM4_MICRO_CNTL));
  61. printk("PM4_BUFFER_CNTL = 0x%08x\n",
  62. (unsigned int)R128_READ(R128_PM4_BUFFER_CNTL));
  63. }
  64. #endif
  65. /* ================================================================
  66. * Engine, FIFO control
  67. */
  68. static int r128_do_pixcache_flush(drm_r128_private_t *dev_priv)
  69. {
  70. u32 tmp;
  71. int i;
  72. tmp = R128_READ(R128_PC_NGUI_CTLSTAT) | R128_PC_FLUSH_ALL;
  73. R128_WRITE(R128_PC_NGUI_CTLSTAT, tmp);
  74. for (i = 0; i < dev_priv->usec_timeout; i++) {
  75. if (!(R128_READ(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY))
  76. return 0;
  77. DRM_UDELAY(1);
  78. }
  79. #if R128_FIFO_DEBUG
  80. DRM_ERROR("failed!\n");
  81. #endif
  82. return -EBUSY;
  83. }
  84. static int r128_do_wait_for_fifo(drm_r128_private_t *dev_priv, int entries)
  85. {
  86. int i;
  87. for (i = 0; i < dev_priv->usec_timeout; i++) {
  88. int slots = R128_READ(R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK;
  89. if (slots >= entries)
  90. return 0;
  91. DRM_UDELAY(1);
  92. }
  93. #if R128_FIFO_DEBUG
  94. DRM_ERROR("failed!\n");
  95. #endif
  96. return -EBUSY;
  97. }
  98. static int r128_do_wait_for_idle(drm_r128_private_t *dev_priv)
  99. {
  100. int i, ret;
  101. ret = r128_do_wait_for_fifo(dev_priv, 64);
  102. if (ret)
  103. return ret;
  104. for (i = 0; i < dev_priv->usec_timeout; i++) {
  105. if (!(R128_READ(R128_GUI_STAT) & R128_GUI_ACTIVE)) {
  106. r128_do_pixcache_flush(dev_priv);
  107. return 0;
  108. }
  109. DRM_UDELAY(1);
  110. }
  111. #if R128_FIFO_DEBUG
  112. DRM_ERROR("failed!\n");
  113. #endif
  114. return -EBUSY;
  115. }
  116. /* ================================================================
  117. * CCE control, initialization
  118. */
  119. /* Load the microcode for the CCE */
  120. static int r128_cce_load_microcode(drm_r128_private_t *dev_priv)
  121. {
  122. struct platform_device *pdev;
  123. const struct firmware *fw;
  124. const __be32 *fw_data;
  125. int rc, i;
  126. DRM_DEBUG("\n");
  127. pdev = platform_device_register_simple("r128_cce", 0, NULL, 0);
  128. if (IS_ERR(pdev)) {
  129. printk(KERN_ERR "r128_cce: Failed to register firmware\n");
  130. return PTR_ERR(pdev);
  131. }
  132. rc = request_firmware(&fw, FIRMWARE_NAME, &pdev->dev);
  133. platform_device_unregister(pdev);
  134. if (rc) {
  135. printk(KERN_ERR "r128_cce: Failed to load firmware \"%s\"\n",
  136. FIRMWARE_NAME);
  137. return rc;
  138. }
  139. if (fw->size != 256 * 8) {
  140. printk(KERN_ERR
  141. "r128_cce: Bogus length %zu in firmware \"%s\"\n",
  142. fw->size, FIRMWARE_NAME);
  143. rc = -EINVAL;
  144. goto out_release;
  145. }
  146. r128_do_wait_for_idle(dev_priv);
  147. fw_data = (const __be32 *)fw->data;
  148. R128_WRITE(R128_PM4_MICROCODE_ADDR, 0);
  149. for (i = 0; i < 256; i++) {
  150. R128_WRITE(R128_PM4_MICROCODE_DATAH,
  151. be32_to_cpup(&fw_data[i * 2]));
  152. R128_WRITE(R128_PM4_MICROCODE_DATAL,
  153. be32_to_cpup(&fw_data[i * 2 + 1]));
  154. }
  155. out_release:
  156. release_firmware(fw);
  157. return rc;
  158. }
  159. /* Flush any pending commands to the CCE. This should only be used just
  160. * prior to a wait for idle, as it informs the engine that the command
  161. * stream is ending.
  162. */
  163. static void r128_do_cce_flush(drm_r128_private_t *dev_priv)
  164. {
  165. u32 tmp;
  166. tmp = R128_READ(R128_PM4_BUFFER_DL_WPTR) | R128_PM4_BUFFER_DL_DONE;
  167. R128_WRITE(R128_PM4_BUFFER_DL_WPTR, tmp);
  168. }
  169. /* Wait for the CCE to go idle.
  170. */
  171. int r128_do_cce_idle(drm_r128_private_t *dev_priv)
  172. {
  173. int i;
  174. for (i = 0; i < dev_priv->usec_timeout; i++) {
  175. if (GET_RING_HEAD(dev_priv) == dev_priv->ring.tail) {
  176. int pm4stat = R128_READ(R128_PM4_STAT);
  177. if (((pm4stat & R128_PM4_FIFOCNT_MASK) >=
  178. dev_priv->cce_fifo_size) &&
  179. !(pm4stat & (R128_PM4_BUSY |
  180. R128_PM4_GUI_ACTIVE))) {
  181. return r128_do_pixcache_flush(dev_priv);
  182. }
  183. }
  184. DRM_UDELAY(1);
  185. }
  186. #if R128_FIFO_DEBUG
  187. DRM_ERROR("failed!\n");
  188. r128_status(dev_priv);
  189. #endif
  190. return -EBUSY;
  191. }
  192. /* Start the Concurrent Command Engine.
  193. */
  194. static void r128_do_cce_start(drm_r128_private_t *dev_priv)
  195. {
  196. r128_do_wait_for_idle(dev_priv);
  197. R128_WRITE(R128_PM4_BUFFER_CNTL,
  198. dev_priv->cce_mode | dev_priv->ring.size_l2qw
  199. | R128_PM4_BUFFER_CNTL_NOUPDATE);
  200. R128_READ(R128_PM4_BUFFER_ADDR); /* as per the sample code */
  201. R128_WRITE(R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN);
  202. dev_priv->cce_running = 1;
  203. }
  204. /* Reset the Concurrent Command Engine. This will not flush any pending
  205. * commands, so you must wait for the CCE command stream to complete
  206. * before calling this routine.
  207. */
  208. static void r128_do_cce_reset(drm_r128_private_t *dev_priv)
  209. {
  210. R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
  211. R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
  212. dev_priv->ring.tail = 0;
  213. }
  214. /* Stop the Concurrent Command Engine. This will not flush any pending
  215. * commands, so you must flush the command stream and wait for the CCE
  216. * to go idle before calling this routine.
  217. */
  218. static void r128_do_cce_stop(drm_r128_private_t *dev_priv)
  219. {
  220. R128_WRITE(R128_PM4_MICRO_CNTL, 0);
  221. R128_WRITE(R128_PM4_BUFFER_CNTL,
  222. R128_PM4_NONPM4 | R128_PM4_BUFFER_CNTL_NOUPDATE);
  223. dev_priv->cce_running = 0;
  224. }
  225. /* Reset the engine. This will stop the CCE if it is running.
  226. */
  227. static int r128_do_engine_reset(struct drm_device *dev)
  228. {
  229. drm_r128_private_t *dev_priv = dev->dev_private;
  230. u32 clock_cntl_index, mclk_cntl, gen_reset_cntl;
  231. r128_do_pixcache_flush(dev_priv);
  232. clock_cntl_index = R128_READ(R128_CLOCK_CNTL_INDEX);
  233. mclk_cntl = R128_READ_PLL(dev, R128_MCLK_CNTL);
  234. R128_WRITE_PLL(R128_MCLK_CNTL,
  235. mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP);
  236. gen_reset_cntl = R128_READ(R128_GEN_RESET_CNTL);
  237. /* Taken from the sample code - do not change */
  238. R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl | R128_SOFT_RESET_GUI);
  239. R128_READ(R128_GEN_RESET_CNTL);
  240. R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl & ~R128_SOFT_RESET_GUI);
  241. R128_READ(R128_GEN_RESET_CNTL);
  242. R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl);
  243. R128_WRITE(R128_CLOCK_CNTL_INDEX, clock_cntl_index);
  244. R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl);
  245. /* Reset the CCE ring */
  246. r128_do_cce_reset(dev_priv);
  247. /* The CCE is no longer running after an engine reset */
  248. dev_priv->cce_running = 0;
  249. /* Reset any pending vertex, indirect buffers */
  250. r128_freelist_reset(dev);
  251. return 0;
  252. }
  253. static void r128_cce_init_ring_buffer(struct drm_device *dev,
  254. drm_r128_private_t *dev_priv)
  255. {
  256. u32 ring_start;
  257. u32 tmp;
  258. DRM_DEBUG("\n");
  259. /* The manual (p. 2) says this address is in "VM space". This
  260. * means it's an offset from the start of AGP space.
  261. */
  262. #if __OS_HAS_AGP
  263. if (!dev_priv->is_pci)
  264. ring_start = dev_priv->cce_ring->offset - dev->agp->base;
  265. else
  266. #endif
  267. ring_start = dev_priv->cce_ring->offset -
  268. (unsigned long)dev->sg->virtual;
  269. R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
  270. R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
  271. R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
  272. /* Set watermark control */
  273. R128_WRITE(R128_PM4_BUFFER_WM_CNTL,
  274. ((R128_WATERMARK_L / 4) << R128_WMA_SHIFT)
  275. | ((R128_WATERMARK_M / 4) << R128_WMB_SHIFT)
  276. | ((R128_WATERMARK_N / 4) << R128_WMC_SHIFT)
  277. | ((R128_WATERMARK_K / 64) << R128_WB_WM_SHIFT));
  278. /* Force read. Why? Because it's in the examples... */
  279. R128_READ(R128_PM4_BUFFER_ADDR);
  280. /* Turn on bus mastering */
  281. tmp = R128_READ(R128_BUS_CNTL) & ~R128_BUS_MASTER_DIS;
  282. R128_WRITE(R128_BUS_CNTL, tmp);
  283. }
  284. static int r128_do_init_cce(struct drm_device *dev, drm_r128_init_t *init)
  285. {
  286. drm_r128_private_t *dev_priv;
  287. int rc;
  288. DRM_DEBUG("\n");
  289. if (dev->dev_private) {
  290. DRM_DEBUG("called when already initialized\n");
  291. return -EINVAL;
  292. }
  293. dev_priv = kzalloc(sizeof(drm_r128_private_t), GFP_KERNEL);
  294. if (dev_priv == NULL)
  295. return -ENOMEM;
  296. dev_priv->is_pci = init->is_pci;
  297. if (dev_priv->is_pci && !dev->sg) {
  298. DRM_ERROR("PCI GART memory not allocated!\n");
  299. dev->dev_private = (void *)dev_priv;
  300. r128_do_cleanup_cce(dev);
  301. return -EINVAL;
  302. }
  303. dev_priv->usec_timeout = init->usec_timeout;
  304. if (dev_priv->usec_timeout < 1 ||
  305. dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT) {
  306. DRM_DEBUG("TIMEOUT problem!\n");
  307. dev->dev_private = (void *)dev_priv;
  308. r128_do_cleanup_cce(dev);
  309. return -EINVAL;
  310. }
  311. dev_priv->cce_mode = init->cce_mode;
  312. /* GH: Simple idle check.
  313. */
  314. atomic_set(&dev_priv->idle_count, 0);
  315. /* We don't support anything other than bus-mastering ring mode,
  316. * but the ring can be in either AGP or PCI space for the ring
  317. * read pointer.
  318. */
  319. if ((init->cce_mode != R128_PM4_192BM) &&
  320. (init->cce_mode != R128_PM4_128BM_64INDBM) &&
  321. (init->cce_mode != R128_PM4_64BM_128INDBM) &&
  322. (init->cce_mode != R128_PM4_64BM_64VCBM_64INDBM)) {
  323. DRM_DEBUG("Bad cce_mode!\n");
  324. dev->dev_private = (void *)dev_priv;
  325. r128_do_cleanup_cce(dev);
  326. return -EINVAL;
  327. }
  328. switch (init->cce_mode) {
  329. case R128_PM4_NONPM4:
  330. dev_priv->cce_fifo_size = 0;
  331. break;
  332. case R128_PM4_192PIO:
  333. case R128_PM4_192BM:
  334. dev_priv->cce_fifo_size = 192;
  335. break;
  336. case R128_PM4_128PIO_64INDBM:
  337. case R128_PM4_128BM_64INDBM:
  338. dev_priv->cce_fifo_size = 128;
  339. break;
  340. case R128_PM4_64PIO_128INDBM:
  341. case R128_PM4_64BM_128INDBM:
  342. case R128_PM4_64PIO_64VCBM_64INDBM:
  343. case R128_PM4_64BM_64VCBM_64INDBM:
  344. case R128_PM4_64PIO_64VCPIO_64INDPIO:
  345. dev_priv->cce_fifo_size = 64;
  346. break;
  347. }
  348. switch (init->fb_bpp) {
  349. case 16:
  350. dev_priv->color_fmt = R128_DATATYPE_RGB565;
  351. break;
  352. case 32:
  353. default:
  354. dev_priv->color_fmt = R128_DATATYPE_ARGB8888;
  355. break;
  356. }
  357. dev_priv->front_offset = init->front_offset;
  358. dev_priv->front_pitch = init->front_pitch;
  359. dev_priv->back_offset = init->back_offset;
  360. dev_priv->back_pitch = init->back_pitch;
  361. switch (init->depth_bpp) {
  362. case 16:
  363. dev_priv->depth_fmt = R128_DATATYPE_RGB565;
  364. break;
  365. case 24:
  366. case 32:
  367. default:
  368. dev_priv->depth_fmt = R128_DATATYPE_ARGB8888;
  369. break;
  370. }
  371. dev_priv->depth_offset = init->depth_offset;
  372. dev_priv->depth_pitch = init->depth_pitch;
  373. dev_priv->span_offset = init->span_offset;
  374. dev_priv->front_pitch_offset_c = (((dev_priv->front_pitch / 8) << 21) |
  375. (dev_priv->front_offset >> 5));
  376. dev_priv->back_pitch_offset_c = (((dev_priv->back_pitch / 8) << 21) |
  377. (dev_priv->back_offset >> 5));
  378. dev_priv->depth_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
  379. (dev_priv->depth_offset >> 5) |
  380. R128_DST_TILE);
  381. dev_priv->span_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
  382. (dev_priv->span_offset >> 5));
  383. dev_priv->sarea = drm_getsarea(dev);
  384. if (!dev_priv->sarea) {
  385. DRM_ERROR("could not find sarea!\n");
  386. dev->dev_private = (void *)dev_priv;
  387. r128_do_cleanup_cce(dev);
  388. return -EINVAL;
  389. }
  390. dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
  391. if (!dev_priv->mmio) {
  392. DRM_ERROR("could not find mmio region!\n");
  393. dev->dev_private = (void *)dev_priv;
  394. r128_do_cleanup_cce(dev);
  395. return -EINVAL;
  396. }
  397. dev_priv->cce_ring = drm_core_findmap(dev, init->ring_offset);
  398. if (!dev_priv->cce_ring) {
  399. DRM_ERROR("could not find cce ring region!\n");
  400. dev->dev_private = (void *)dev_priv;
  401. r128_do_cleanup_cce(dev);
  402. return -EINVAL;
  403. }
  404. dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
  405. if (!dev_priv->ring_rptr) {
  406. DRM_ERROR("could not find ring read pointer!\n");
  407. dev->dev_private = (void *)dev_priv;
  408. r128_do_cleanup_cce(dev);
  409. return -EINVAL;
  410. }
  411. dev->agp_buffer_token = init->buffers_offset;
  412. dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
  413. if (!dev->agp_buffer_map) {
  414. DRM_ERROR("could not find dma buffer region!\n");
  415. dev->dev_private = (void *)dev_priv;
  416. r128_do_cleanup_cce(dev);
  417. return -EINVAL;
  418. }
  419. if (!dev_priv->is_pci) {
  420. dev_priv->agp_textures =
  421. drm_core_findmap(dev, init->agp_textures_offset);
  422. if (!dev_priv->agp_textures) {
  423. DRM_ERROR("could not find agp texture region!\n");
  424. dev->dev_private = (void *)dev_priv;
  425. r128_do_cleanup_cce(dev);
  426. return -EINVAL;
  427. }
  428. }
  429. dev_priv->sarea_priv =
  430. (drm_r128_sarea_t *) ((u8 *) dev_priv->sarea->handle +
  431. init->sarea_priv_offset);
  432. #if __OS_HAS_AGP
  433. if (!dev_priv->is_pci) {
  434. drm_core_ioremap_wc(dev_priv->cce_ring, dev);
  435. drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
  436. drm_core_ioremap_wc(dev->agp_buffer_map, dev);
  437. if (!dev_priv->cce_ring->handle ||
  438. !dev_priv->ring_rptr->handle ||
  439. !dev->agp_buffer_map->handle) {
  440. DRM_ERROR("Could not ioremap agp regions!\n");
  441. dev->dev_private = (void *)dev_priv;
  442. r128_do_cleanup_cce(dev);
  443. return -ENOMEM;
  444. }
  445. } else
  446. #endif
  447. {
  448. dev_priv->cce_ring->handle =
  449. (void *)(unsigned long)dev_priv->cce_ring->offset;
  450. dev_priv->ring_rptr->handle =
  451. (void *)(unsigned long)dev_priv->ring_rptr->offset;
  452. dev->agp_buffer_map->handle =
  453. (void *)(unsigned long)dev->agp_buffer_map->offset;
  454. }
  455. #if __OS_HAS_AGP
  456. if (!dev_priv->is_pci)
  457. dev_priv->cce_buffers_offset = dev->agp->base;
  458. else
  459. #endif
  460. dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
  461. dev_priv->ring.start = (u32 *) dev_priv->cce_ring->handle;
  462. dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->handle
  463. + init->ring_size / sizeof(u32));
  464. dev_priv->ring.size = init->ring_size;
  465. dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
  466. dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
  467. dev_priv->ring.high_mark = 128;
  468. dev_priv->sarea_priv->last_frame = 0;
  469. R128_WRITE(R128_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
  470. dev_priv->sarea_priv->last_dispatch = 0;
  471. R128_WRITE(R128_LAST_DISPATCH_REG, dev_priv->sarea_priv->last_dispatch);
  472. #if __OS_HAS_AGP
  473. if (dev_priv->is_pci) {
  474. #endif
  475. dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
  476. dev_priv->gart_info.gart_table_location = DRM_ATI_GART_MAIN;
  477. dev_priv->gart_info.table_size = R128_PCIGART_TABLE_SIZE;
  478. dev_priv->gart_info.addr = NULL;
  479. dev_priv->gart_info.bus_addr = 0;
  480. dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
  481. if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
  482. DRM_ERROR("failed to init PCI GART!\n");
  483. dev->dev_private = (void *)dev_priv;
  484. r128_do_cleanup_cce(dev);
  485. return -ENOMEM;
  486. }
  487. R128_WRITE(R128_PCI_GART_PAGE, dev_priv->gart_info.bus_addr);
  488. #if __OS_HAS_AGP
  489. }
  490. #endif
  491. r128_cce_init_ring_buffer(dev, dev_priv);
  492. rc = r128_cce_load_microcode(dev_priv);
  493. dev->dev_private = (void *)dev_priv;
  494. r128_do_engine_reset(dev);
  495. if (rc) {
  496. DRM_ERROR("Failed to load firmware!\n");
  497. r128_do_cleanup_cce(dev);
  498. }
  499. return rc;
  500. }
  501. int r128_do_cleanup_cce(struct drm_device *dev)
  502. {
  503. /* Make sure interrupts are disabled here because the uninstall ioctl
  504. * may not have been called from userspace and after dev_private
  505. * is freed, it's too late.
  506. */
  507. if (dev->irq_enabled)
  508. drm_irq_uninstall(dev);
  509. if (dev->dev_private) {
  510. drm_r128_private_t *dev_priv = dev->dev_private;
  511. #if __OS_HAS_AGP
  512. if (!dev_priv->is_pci) {
  513. if (dev_priv->cce_ring != NULL)
  514. drm_core_ioremapfree(dev_priv->cce_ring, dev);
  515. if (dev_priv->ring_rptr != NULL)
  516. drm_core_ioremapfree(dev_priv->ring_rptr, dev);
  517. if (dev->agp_buffer_map != NULL) {
  518. drm_core_ioremapfree(dev->agp_buffer_map, dev);
  519. dev->agp_buffer_map = NULL;
  520. }
  521. } else
  522. #endif
  523. {
  524. if (dev_priv->gart_info.bus_addr)
  525. if (!drm_ati_pcigart_cleanup(dev,
  526. &dev_priv->gart_info))
  527. DRM_ERROR
  528. ("failed to cleanup PCI GART!\n");
  529. }
  530. kfree(dev->dev_private);
  531. dev->dev_private = NULL;
  532. }
  533. return 0;
  534. }
  535. int r128_cce_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  536. {
  537. drm_r128_init_t *init = data;
  538. DRM_DEBUG("\n");
  539. LOCK_TEST_WITH_RETURN(dev, file_priv);
  540. switch (init->func) {
  541. case R128_INIT_CCE:
  542. return r128_do_init_cce(dev, init);
  543. case R128_CLEANUP_CCE:
  544. return r128_do_cleanup_cce(dev);
  545. }
  546. return -EINVAL;
  547. }
  548. int r128_cce_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
  549. {
  550. drm_r128_private_t *dev_priv = dev->dev_private;
  551. DRM_DEBUG("\n");
  552. LOCK_TEST_WITH_RETURN(dev, file_priv);
  553. DEV_INIT_TEST_WITH_RETURN(dev_priv);
  554. if (dev_priv->cce_running || dev_priv->cce_mode == R128_PM4_NONPM4) {
  555. DRM_DEBUG("while CCE running\n");
  556. return 0;
  557. }
  558. r128_do_cce_start(dev_priv);
  559. return 0;
  560. }
  561. /* Stop the CCE. The engine must have been idled before calling this
  562. * routine.
  563. */
  564. int r128_cce_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
  565. {
  566. drm_r128_private_t *dev_priv = dev->dev_private;
  567. drm_r128_cce_stop_t *stop = data;
  568. int ret;
  569. DRM_DEBUG("\n");
  570. LOCK_TEST_WITH_RETURN(dev, file_priv);
  571. DEV_INIT_TEST_WITH_RETURN(dev_priv);
  572. /* Flush any pending CCE commands. This ensures any outstanding
  573. * commands are exectuted by the engine before we turn it off.
  574. */
  575. if (stop->flush)
  576. r128_do_cce_flush(dev_priv);
  577. /* If we fail to make the engine go idle, we return an error
  578. * code so that the DRM ioctl wrapper can try again.
  579. */
  580. if (stop->idle) {
  581. ret = r128_do_cce_idle(dev_priv);
  582. if (ret)
  583. return ret;
  584. }
  585. /* Finally, we can turn off the CCE. If the engine isn't idle,
  586. * we will get some dropped triangles as they won't be fully
  587. * rendered before the CCE is shut down.
  588. */
  589. r128_do_cce_stop(dev_priv);
  590. /* Reset the engine */
  591. r128_do_engine_reset(dev);
  592. return 0;
  593. }
  594. /* Just reset the CCE ring. Called as part of an X Server engine reset.
  595. */
  596. int r128_cce_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
  597. {
  598. drm_r128_private_t *dev_priv = dev->dev_private;
  599. DRM_DEBUG("\n");
  600. LOCK_TEST_WITH_RETURN(dev, file_priv);
  601. DEV_INIT_TEST_WITH_RETURN(dev_priv);
  602. r128_do_cce_reset(dev_priv);
  603. /* The CCE is no longer running after an engine reset */
  604. dev_priv->cce_running = 0;
  605. return 0;
  606. }
  607. int r128_cce_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
  608. {
  609. drm_r128_private_t *dev_priv = dev->dev_private;
  610. DRM_DEBUG("\n");
  611. LOCK_TEST_WITH_RETURN(dev, file_priv);
  612. DEV_INIT_TEST_WITH_RETURN(dev_priv);
  613. if (dev_priv->cce_running)
  614. r128_do_cce_flush(dev_priv);
  615. return r128_do_cce_idle(dev_priv);
  616. }
  617. int r128_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
  618. {
  619. DRM_DEBUG("\n");
  620. LOCK_TEST_WITH_RETURN(dev, file_priv);
  621. DEV_INIT_TEST_WITH_RETURN(dev->dev_private);
  622. return r128_do_engine_reset(dev);
  623. }
  624. int r128_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
  625. {
  626. return -EINVAL;
  627. }
  628. /* ================================================================
  629. * Freelist management
  630. */
  631. #define R128_BUFFER_USED 0xffffffff
  632. #define R128_BUFFER_FREE 0
  633. #if 0
  634. static int r128_freelist_init(struct drm_device *dev)
  635. {
  636. struct drm_device_dma *dma = dev->dma;
  637. drm_r128_private_t *dev_priv = dev->dev_private;
  638. struct drm_buf *buf;
  639. drm_r128_buf_priv_t *buf_priv;
  640. drm_r128_freelist_t *entry;
  641. int i;
  642. dev_priv->head = kzalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
  643. if (dev_priv->head == NULL)
  644. return -ENOMEM;
  645. dev_priv->head->age = R128_BUFFER_USED;
  646. for (i = 0; i < dma->buf_count; i++) {
  647. buf = dma->buflist[i];
  648. buf_priv = buf->dev_private;
  649. entry = kmalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
  650. if (!entry)
  651. return -ENOMEM;
  652. entry->age = R128_BUFFER_FREE;
  653. entry->buf = buf;
  654. entry->prev = dev_priv->head;
  655. entry->next = dev_priv->head->next;
  656. if (!entry->next)
  657. dev_priv->tail = entry;
  658. buf_priv->discard = 0;
  659. buf_priv->dispatched = 0;
  660. buf_priv->list_entry = entry;
  661. dev_priv->head->next = entry;
  662. if (dev_priv->head->next)
  663. dev_priv->head->next->prev = entry;
  664. }
  665. return 0;
  666. }
  667. #endif
  668. static struct drm_buf *r128_freelist_get(struct drm_device * dev)
  669. {
  670. struct drm_device_dma *dma = dev->dma;
  671. drm_r128_private_t *dev_priv = dev->dev_private;
  672. drm_r128_buf_priv_t *buf_priv;
  673. struct drm_buf *buf;
  674. int i, t;
  675. /* FIXME: Optimize -- use freelist code */
  676. for (i = 0; i < dma->buf_count; i++) {
  677. buf = dma->buflist[i];
  678. buf_priv = buf->dev_private;
  679. if (!buf->file_priv)
  680. return buf;
  681. }
  682. for (t = 0; t < dev_priv->usec_timeout; t++) {
  683. u32 done_age = R128_READ(R128_LAST_DISPATCH_REG);
  684. for (i = 0; i < dma->buf_count; i++) {
  685. buf = dma->buflist[i];
  686. buf_priv = buf->dev_private;
  687. if (buf->pending && buf_priv->age <= done_age) {
  688. /* The buffer has been processed, so it
  689. * can now be used.
  690. */
  691. buf->pending = 0;
  692. return buf;
  693. }
  694. }
  695. DRM_UDELAY(1);
  696. }
  697. DRM_DEBUG("returning NULL!\n");
  698. return NULL;
  699. }
  700. void r128_freelist_reset(struct drm_device *dev)
  701. {
  702. struct drm_device_dma *dma = dev->dma;
  703. int i;
  704. for (i = 0; i < dma->buf_count; i++) {
  705. struct drm_buf *buf = dma->buflist[i];
  706. drm_r128_buf_priv_t *buf_priv = buf->dev_private;
  707. buf_priv->age = 0;
  708. }
  709. }
  710. /* ================================================================
  711. * CCE command submission
  712. */
  713. int r128_wait_ring(drm_r128_private_t *dev_priv, int n)
  714. {
  715. drm_r128_ring_buffer_t *ring = &dev_priv->ring;
  716. int i;
  717. for (i = 0; i < dev_priv->usec_timeout; i++) {
  718. r128_update_ring_snapshot(dev_priv);
  719. if (ring->space >= n)
  720. return 0;
  721. DRM_UDELAY(1);
  722. }
  723. /* FIXME: This is being ignored... */
  724. DRM_ERROR("failed!\n");
  725. return -EBUSY;
  726. }
  727. static int r128_cce_get_buffers(struct drm_device *dev,
  728. struct drm_file *file_priv,
  729. struct drm_dma *d)
  730. {
  731. int i;
  732. struct drm_buf *buf;
  733. for (i = d->granted_count; i < d->request_count; i++) {
  734. buf = r128_freelist_get(dev);
  735. if (!buf)
  736. return -EAGAIN;
  737. buf->file_priv = file_priv;
  738. if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
  739. sizeof(buf->idx)))
  740. return -EFAULT;
  741. if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
  742. sizeof(buf->total)))
  743. return -EFAULT;
  744. d->granted_count++;
  745. }
  746. return 0;
  747. }
  748. int r128_cce_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
  749. {
  750. struct drm_device_dma *dma = dev->dma;
  751. int ret = 0;
  752. struct drm_dma *d = data;
  753. LOCK_TEST_WITH_RETURN(dev, file_priv);
  754. /* Please don't send us buffers.
  755. */
  756. if (d->send_count != 0) {
  757. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  758. DRM_CURRENTPID, d->send_count);
  759. return -EINVAL;
  760. }
  761. /* We'll send you buffers.
  762. */
  763. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  764. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  765. DRM_CURRENTPID, d->request_count, dma->buf_count);
  766. return -EINVAL;
  767. }
  768. d->granted_count = 0;
  769. if (d->request_count)
  770. ret = r128_cce_get_buffers(dev, file_priv, d);
  771. return ret;
  772. }