r128_cce.c 24 KB

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