via_dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* via_dma.c -- DMA support for the VIA Unichrome/Pro
  2. *
  3. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. *
  6. * Copyright 2004 Digeo, Inc., Palo Alto, CA, U.S.A.
  7. * All Rights Reserved.
  8. *
  9. * Copyright 2004 The Unichrome project.
  10. * All Rights Reserved.
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice (including the
  20. * next paragraph) shall be included in all copies or substantial portions
  21. * of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  26. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  27. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  28. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  29. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. *
  31. * Authors:
  32. * Tungsten Graphics,
  33. * Erdi Chen,
  34. * Thomas Hellstrom.
  35. */
  36. #include <drm/drmP.h>
  37. #include <drm/via_drm.h>
  38. #include "via_drv.h"
  39. #include "via_3d_reg.h"
  40. #define CMDBUF_ALIGNMENT_SIZE (0x100)
  41. #define CMDBUF_ALIGNMENT_MASK (0x0ff)
  42. /* defines for VIA 3D registers */
  43. #define VIA_REG_STATUS 0x400
  44. #define VIA_REG_TRANSET 0x43C
  45. #define VIA_REG_TRANSPACE 0x440
  46. /* VIA_REG_STATUS(0x400): Engine Status */
  47. #define VIA_CMD_RGTR_BUSY 0x00000080 /* Command Regulator is busy */
  48. #define VIA_2D_ENG_BUSY 0x00000001 /* 2D Engine is busy */
  49. #define VIA_3D_ENG_BUSY 0x00000002 /* 3D Engine is busy */
  50. #define VIA_VR_QUEUE_BUSY 0x00020000 /* Virtual Queue is busy */
  51. #define SetReg2DAGP(nReg, nData) { \
  52. *((uint32_t *)(vb)) = ((nReg) >> 2) | HALCYON_HEADER1; \
  53. *((uint32_t *)(vb) + 1) = (nData); \
  54. vb = ((uint32_t *)vb) + 2; \
  55. dev_priv->dma_low += 8; \
  56. }
  57. #define via_flush_write_combine() mb()
  58. #define VIA_OUT_RING_QW(w1, w2) do { \
  59. *vb++ = (w1); \
  60. *vb++ = (w2); \
  61. dev_priv->dma_low += 8; \
  62. } while (0)
  63. static void via_cmdbuf_start(drm_via_private_t *dev_priv);
  64. static void via_cmdbuf_pause(drm_via_private_t *dev_priv);
  65. static void via_cmdbuf_reset(drm_via_private_t *dev_priv);
  66. static void via_cmdbuf_rewind(drm_via_private_t *dev_priv);
  67. static int via_wait_idle(drm_via_private_t *dev_priv);
  68. static void via_pad_cache(drm_via_private_t *dev_priv, int qwords);
  69. /*
  70. * Free space in command buffer.
  71. */
  72. static uint32_t via_cmdbuf_space(drm_via_private_t *dev_priv)
  73. {
  74. uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  75. uint32_t hw_addr = *(dev_priv->hw_addr_ptr) - agp_base;
  76. return ((hw_addr <= dev_priv->dma_low) ?
  77. (dev_priv->dma_high + hw_addr - dev_priv->dma_low) :
  78. (hw_addr - dev_priv->dma_low));
  79. }
  80. /*
  81. * How much does the command regulator lag behind?
  82. */
  83. static uint32_t via_cmdbuf_lag(drm_via_private_t *dev_priv)
  84. {
  85. uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  86. uint32_t hw_addr = *(dev_priv->hw_addr_ptr) - agp_base;
  87. return ((hw_addr <= dev_priv->dma_low) ?
  88. (dev_priv->dma_low - hw_addr) :
  89. (dev_priv->dma_wrap + dev_priv->dma_low - hw_addr));
  90. }
  91. /*
  92. * Check that the given size fits in the buffer, otherwise wait.
  93. */
  94. static inline int
  95. via_cmdbuf_wait(drm_via_private_t *dev_priv, unsigned int size)
  96. {
  97. uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  98. uint32_t cur_addr, hw_addr, next_addr;
  99. volatile uint32_t *hw_addr_ptr;
  100. uint32_t count;
  101. hw_addr_ptr = dev_priv->hw_addr_ptr;
  102. cur_addr = dev_priv->dma_low;
  103. next_addr = cur_addr + size + 512 * 1024;
  104. count = 1000000;
  105. do {
  106. hw_addr = *hw_addr_ptr - agp_base;
  107. if (count-- == 0) {
  108. DRM_ERROR
  109. ("via_cmdbuf_wait timed out hw %x cur_addr %x next_addr %x\n",
  110. hw_addr, cur_addr, next_addr);
  111. return -1;
  112. }
  113. if ((cur_addr < hw_addr) && (next_addr >= hw_addr))
  114. msleep(1);
  115. } while ((cur_addr < hw_addr) && (next_addr >= hw_addr));
  116. return 0;
  117. }
  118. /*
  119. * Checks whether buffer head has reach the end. Rewind the ring buffer
  120. * when necessary.
  121. *
  122. * Returns virtual pointer to ring buffer.
  123. */
  124. static inline uint32_t *via_check_dma(drm_via_private_t * dev_priv,
  125. unsigned int size)
  126. {
  127. if ((dev_priv->dma_low + size + 4 * CMDBUF_ALIGNMENT_SIZE) >
  128. dev_priv->dma_high) {
  129. via_cmdbuf_rewind(dev_priv);
  130. }
  131. if (via_cmdbuf_wait(dev_priv, size) != 0)
  132. return NULL;
  133. return (uint32_t *) (dev_priv->dma_ptr + dev_priv->dma_low);
  134. }
  135. int via_dma_cleanup(struct drm_device *dev)
  136. {
  137. if (dev->dev_private) {
  138. drm_via_private_t *dev_priv =
  139. (drm_via_private_t *) dev->dev_private;
  140. if (dev_priv->ring.virtual_start) {
  141. via_cmdbuf_reset(dev_priv);
  142. drm_legacy_ioremapfree(&dev_priv->ring.map, dev);
  143. dev_priv->ring.virtual_start = NULL;
  144. }
  145. }
  146. return 0;
  147. }
  148. static int via_initialize(struct drm_device *dev,
  149. drm_via_private_t *dev_priv,
  150. drm_via_dma_init_t *init)
  151. {
  152. if (!dev_priv || !dev_priv->mmio) {
  153. DRM_ERROR("via_dma_init called before via_map_init\n");
  154. return -EFAULT;
  155. }
  156. if (dev_priv->ring.virtual_start != NULL) {
  157. DRM_ERROR("called again without calling cleanup\n");
  158. return -EFAULT;
  159. }
  160. if (!dev->agp || !dev->agp->base) {
  161. DRM_ERROR("called with no agp memory available\n");
  162. return -EFAULT;
  163. }
  164. if (dev_priv->chipset == VIA_DX9_0) {
  165. DRM_ERROR("AGP DMA is not supported on this chip\n");
  166. return -EINVAL;
  167. }
  168. dev_priv->ring.map.offset = dev->agp->base + init->offset;
  169. dev_priv->ring.map.size = init->size;
  170. dev_priv->ring.map.type = 0;
  171. dev_priv->ring.map.flags = 0;
  172. dev_priv->ring.map.mtrr = 0;
  173. drm_legacy_ioremap(&dev_priv->ring.map, dev);
  174. if (dev_priv->ring.map.handle == NULL) {
  175. via_dma_cleanup(dev);
  176. DRM_ERROR("can not ioremap virtual address for"
  177. " ring buffer\n");
  178. return -ENOMEM;
  179. }
  180. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  181. dev_priv->dma_ptr = dev_priv->ring.virtual_start;
  182. dev_priv->dma_low = 0;
  183. dev_priv->dma_high = init->size;
  184. dev_priv->dma_wrap = init->size;
  185. dev_priv->dma_offset = init->offset;
  186. dev_priv->last_pause_ptr = NULL;
  187. dev_priv->hw_addr_ptr =
  188. (volatile uint32_t *)((char *)dev_priv->mmio->handle +
  189. init->reg_pause_addr);
  190. via_cmdbuf_start(dev_priv);
  191. return 0;
  192. }
  193. static int via_dma_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  194. {
  195. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  196. drm_via_dma_init_t *init = data;
  197. int retcode = 0;
  198. switch (init->func) {
  199. case VIA_INIT_DMA:
  200. if (!capable(CAP_SYS_ADMIN))
  201. retcode = -EPERM;
  202. else
  203. retcode = via_initialize(dev, dev_priv, init);
  204. break;
  205. case VIA_CLEANUP_DMA:
  206. if (!capable(CAP_SYS_ADMIN))
  207. retcode = -EPERM;
  208. else
  209. retcode = via_dma_cleanup(dev);
  210. break;
  211. case VIA_DMA_INITIALIZED:
  212. retcode = (dev_priv->ring.virtual_start != NULL) ?
  213. 0 : -EFAULT;
  214. break;
  215. default:
  216. retcode = -EINVAL;
  217. break;
  218. }
  219. return retcode;
  220. }
  221. static int via_dispatch_cmdbuffer(struct drm_device *dev, drm_via_cmdbuffer_t *cmd)
  222. {
  223. drm_via_private_t *dev_priv;
  224. uint32_t *vb;
  225. int ret;
  226. dev_priv = (drm_via_private_t *) dev->dev_private;
  227. if (dev_priv->ring.virtual_start == NULL) {
  228. DRM_ERROR("called without initializing AGP ring buffer.\n");
  229. return -EFAULT;
  230. }
  231. if (cmd->size > VIA_PCI_BUF_SIZE)
  232. return -ENOMEM;
  233. if (copy_from_user(dev_priv->pci_buf, cmd->buf, cmd->size))
  234. return -EFAULT;
  235. /*
  236. * Running this function on AGP memory is dead slow. Therefore
  237. * we run it on a temporary cacheable system memory buffer and
  238. * copy it to AGP memory when ready.
  239. */
  240. if ((ret =
  241. via_verify_command_stream((uint32_t *) dev_priv->pci_buf,
  242. cmd->size, dev, 1))) {
  243. return ret;
  244. }
  245. vb = via_check_dma(dev_priv, (cmd->size < 0x100) ? 0x102 : cmd->size);
  246. if (vb == NULL)
  247. return -EAGAIN;
  248. memcpy(vb, dev_priv->pci_buf, cmd->size);
  249. dev_priv->dma_low += cmd->size;
  250. /*
  251. * Small submissions somehow stalls the CPU. (AGP cache effects?)
  252. * pad to greater size.
  253. */
  254. if (cmd->size < 0x100)
  255. via_pad_cache(dev_priv, (0x100 - cmd->size) >> 3);
  256. via_cmdbuf_pause(dev_priv);
  257. return 0;
  258. }
  259. int via_driver_dma_quiescent(struct drm_device *dev)
  260. {
  261. drm_via_private_t *dev_priv = dev->dev_private;
  262. if (!via_wait_idle(dev_priv))
  263. return -EBUSY;
  264. return 0;
  265. }
  266. static int via_flush_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv)
  267. {
  268. LOCK_TEST_WITH_RETURN(dev, file_priv);
  269. return via_driver_dma_quiescent(dev);
  270. }
  271. static int via_cmdbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv)
  272. {
  273. drm_via_cmdbuffer_t *cmdbuf = data;
  274. int ret;
  275. LOCK_TEST_WITH_RETURN(dev, file_priv);
  276. DRM_DEBUG("buf %p size %lu\n", cmdbuf->buf, cmdbuf->size);
  277. ret = via_dispatch_cmdbuffer(dev, cmdbuf);
  278. return ret;
  279. }
  280. static int via_dispatch_pci_cmdbuffer(struct drm_device *dev,
  281. drm_via_cmdbuffer_t *cmd)
  282. {
  283. drm_via_private_t *dev_priv = dev->dev_private;
  284. int ret;
  285. if (cmd->size > VIA_PCI_BUF_SIZE)
  286. return -ENOMEM;
  287. if (copy_from_user(dev_priv->pci_buf, cmd->buf, cmd->size))
  288. return -EFAULT;
  289. if ((ret =
  290. via_verify_command_stream((uint32_t *) dev_priv->pci_buf,
  291. cmd->size, dev, 0))) {
  292. return ret;
  293. }
  294. ret =
  295. via_parse_command_stream(dev, (const uint32_t *)dev_priv->pci_buf,
  296. cmd->size);
  297. return ret;
  298. }
  299. static int via_pci_cmdbuffer(struct drm_device *dev, void *data, struct drm_file *file_priv)
  300. {
  301. drm_via_cmdbuffer_t *cmdbuf = data;
  302. int ret;
  303. LOCK_TEST_WITH_RETURN(dev, file_priv);
  304. DRM_DEBUG("buf %p size %lu\n", cmdbuf->buf, cmdbuf->size);
  305. ret = via_dispatch_pci_cmdbuffer(dev, cmdbuf);
  306. return ret;
  307. }
  308. static inline uint32_t *via_align_buffer(drm_via_private_t *dev_priv,
  309. uint32_t * vb, int qw_count)
  310. {
  311. for (; qw_count > 0; --qw_count)
  312. VIA_OUT_RING_QW(HC_DUMMY, HC_DUMMY);
  313. return vb;
  314. }
  315. /*
  316. * This function is used internally by ring buffer management code.
  317. *
  318. * Returns virtual pointer to ring buffer.
  319. */
  320. static inline uint32_t *via_get_dma(drm_via_private_t *dev_priv)
  321. {
  322. return (uint32_t *) (dev_priv->dma_ptr + dev_priv->dma_low);
  323. }
  324. /*
  325. * Hooks a segment of data into the tail of the ring-buffer by
  326. * modifying the pause address stored in the buffer itself. If
  327. * the regulator has already paused, restart it.
  328. */
  329. static int via_hook_segment(drm_via_private_t *dev_priv,
  330. uint32_t pause_addr_hi, uint32_t pause_addr_lo,
  331. int no_pci_fire)
  332. {
  333. int paused, count;
  334. volatile uint32_t *paused_at = dev_priv->last_pause_ptr;
  335. uint32_t reader, ptr;
  336. uint32_t diff;
  337. paused = 0;
  338. via_flush_write_combine();
  339. (void) *(volatile uint32_t *)(via_get_dma(dev_priv) - 1);
  340. *paused_at = pause_addr_lo;
  341. via_flush_write_combine();
  342. (void) *paused_at;
  343. reader = *(dev_priv->hw_addr_ptr);
  344. ptr = ((volatile char *)paused_at - dev_priv->dma_ptr) +
  345. dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr + 4;
  346. dev_priv->last_pause_ptr = via_get_dma(dev_priv) - 1;
  347. /*
  348. * If there is a possibility that the command reader will
  349. * miss the new pause address and pause on the old one,
  350. * In that case we need to program the new start address
  351. * using PCI.
  352. */
  353. diff = (uint32_t) (ptr - reader) - dev_priv->dma_diff;
  354. count = 10000000;
  355. while (diff == 0 && count--) {
  356. paused = (VIA_READ(0x41c) & 0x80000000);
  357. if (paused)
  358. break;
  359. reader = *(dev_priv->hw_addr_ptr);
  360. diff = (uint32_t) (ptr - reader) - dev_priv->dma_diff;
  361. }
  362. paused = VIA_READ(0x41c) & 0x80000000;
  363. if (paused && !no_pci_fire) {
  364. reader = *(dev_priv->hw_addr_ptr);
  365. diff = (uint32_t) (ptr - reader) - dev_priv->dma_diff;
  366. diff &= (dev_priv->dma_high - 1);
  367. if (diff != 0 && diff < (dev_priv->dma_high >> 1)) {
  368. DRM_ERROR("Paused at incorrect address. "
  369. "0x%08x, 0x%08x 0x%08x\n",
  370. ptr, reader, dev_priv->dma_diff);
  371. } else if (diff == 0) {
  372. /*
  373. * There is a concern that these writes may stall the PCI bus
  374. * if the GPU is not idle. However, idling the GPU first
  375. * doesn't make a difference.
  376. */
  377. VIA_WRITE(VIA_REG_TRANSET, (HC_ParaType_PreCR << 16));
  378. VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_hi);
  379. VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_lo);
  380. VIA_READ(VIA_REG_TRANSPACE);
  381. }
  382. }
  383. return paused;
  384. }
  385. static int via_wait_idle(drm_via_private_t *dev_priv)
  386. {
  387. int count = 10000000;
  388. while (!(VIA_READ(VIA_REG_STATUS) & VIA_VR_QUEUE_BUSY) && --count)
  389. ;
  390. while (count && (VIA_READ(VIA_REG_STATUS) &
  391. (VIA_CMD_RGTR_BUSY | VIA_2D_ENG_BUSY |
  392. VIA_3D_ENG_BUSY)))
  393. --count;
  394. return count;
  395. }
  396. static uint32_t *via_align_cmd(drm_via_private_t *dev_priv, uint32_t cmd_type,
  397. uint32_t addr, uint32_t *cmd_addr_hi,
  398. uint32_t *cmd_addr_lo, int skip_wait)
  399. {
  400. uint32_t agp_base;
  401. uint32_t cmd_addr, addr_lo, addr_hi;
  402. uint32_t *vb;
  403. uint32_t qw_pad_count;
  404. if (!skip_wait)
  405. via_cmdbuf_wait(dev_priv, 2 * CMDBUF_ALIGNMENT_SIZE);
  406. vb = via_get_dma(dev_priv);
  407. VIA_OUT_RING_QW(HC_HEADER2 | ((VIA_REG_TRANSET >> 2) << 12) |
  408. (VIA_REG_TRANSPACE >> 2), HC_ParaType_PreCR << 16);
  409. agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  410. qw_pad_count = (CMDBUF_ALIGNMENT_SIZE >> 3) -
  411. ((dev_priv->dma_low & CMDBUF_ALIGNMENT_MASK) >> 3);
  412. cmd_addr = (addr) ? addr :
  413. agp_base + dev_priv->dma_low - 8 + (qw_pad_count << 3);
  414. addr_lo = ((HC_SubA_HAGPBpL << 24) | (cmd_type & HC_HAGPBpID_MASK) |
  415. (cmd_addr & HC_HAGPBpL_MASK));
  416. addr_hi = ((HC_SubA_HAGPBpH << 24) | (cmd_addr >> 24));
  417. vb = via_align_buffer(dev_priv, vb, qw_pad_count - 1);
  418. VIA_OUT_RING_QW(*cmd_addr_hi = addr_hi, *cmd_addr_lo = addr_lo);
  419. return vb;
  420. }
  421. static void via_cmdbuf_start(drm_via_private_t *dev_priv)
  422. {
  423. uint32_t pause_addr_lo, pause_addr_hi;
  424. uint32_t start_addr, start_addr_lo;
  425. uint32_t end_addr, end_addr_lo;
  426. uint32_t command;
  427. uint32_t agp_base;
  428. uint32_t ptr;
  429. uint32_t reader;
  430. int count;
  431. dev_priv->dma_low = 0;
  432. agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  433. start_addr = agp_base;
  434. end_addr = agp_base + dev_priv->dma_high;
  435. start_addr_lo = ((HC_SubA_HAGPBstL << 24) | (start_addr & 0xFFFFFF));
  436. end_addr_lo = ((HC_SubA_HAGPBendL << 24) | (end_addr & 0xFFFFFF));
  437. command = ((HC_SubA_HAGPCMNT << 24) | (start_addr >> 24) |
  438. ((end_addr & 0xff000000) >> 16));
  439. dev_priv->last_pause_ptr =
  440. via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0,
  441. &pause_addr_hi, &pause_addr_lo, 1) - 1;
  442. via_flush_write_combine();
  443. (void) *(volatile uint32_t *)dev_priv->last_pause_ptr;
  444. VIA_WRITE(VIA_REG_TRANSET, (HC_ParaType_PreCR << 16));
  445. VIA_WRITE(VIA_REG_TRANSPACE, command);
  446. VIA_WRITE(VIA_REG_TRANSPACE, start_addr_lo);
  447. VIA_WRITE(VIA_REG_TRANSPACE, end_addr_lo);
  448. VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_hi);
  449. VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_lo);
  450. wmb();
  451. VIA_WRITE(VIA_REG_TRANSPACE, command | HC_HAGPCMNT_MASK);
  452. VIA_READ(VIA_REG_TRANSPACE);
  453. dev_priv->dma_diff = 0;
  454. count = 10000000;
  455. while (!(VIA_READ(0x41c) & 0x80000000) && count--);
  456. reader = *(dev_priv->hw_addr_ptr);
  457. ptr = ((volatile char *)dev_priv->last_pause_ptr - dev_priv->dma_ptr) +
  458. dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr + 4;
  459. /*
  460. * This is the difference between where we tell the
  461. * command reader to pause and where it actually pauses.
  462. * This differs between hw implementation so we need to
  463. * detect it.
  464. */
  465. dev_priv->dma_diff = ptr - reader;
  466. }
  467. static void via_pad_cache(drm_via_private_t *dev_priv, int qwords)
  468. {
  469. uint32_t *vb;
  470. via_cmdbuf_wait(dev_priv, qwords + 2);
  471. vb = via_get_dma(dev_priv);
  472. VIA_OUT_RING_QW(HC_HEADER2, HC_ParaType_NotTex << 16);
  473. via_align_buffer(dev_priv, vb, qwords);
  474. }
  475. static inline void via_dummy_bitblt(drm_via_private_t *dev_priv)
  476. {
  477. uint32_t *vb = via_get_dma(dev_priv);
  478. SetReg2DAGP(0x0C, (0 | (0 << 16)));
  479. SetReg2DAGP(0x10, 0 | (0 << 16));
  480. SetReg2DAGP(0x0, 0x1 | 0x2000 | 0xAA000000);
  481. }
  482. static void via_cmdbuf_jump(drm_via_private_t *dev_priv)
  483. {
  484. uint32_t agp_base;
  485. uint32_t pause_addr_lo, pause_addr_hi;
  486. uint32_t jump_addr_lo, jump_addr_hi;
  487. volatile uint32_t *last_pause_ptr;
  488. uint32_t dma_low_save1, dma_low_save2;
  489. agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr;
  490. via_align_cmd(dev_priv, HC_HAGPBpID_JUMP, 0, &jump_addr_hi,
  491. &jump_addr_lo, 0);
  492. dev_priv->dma_wrap = dev_priv->dma_low;
  493. /*
  494. * Wrap command buffer to the beginning.
  495. */
  496. dev_priv->dma_low = 0;
  497. if (via_cmdbuf_wait(dev_priv, CMDBUF_ALIGNMENT_SIZE) != 0)
  498. DRM_ERROR("via_cmdbuf_jump failed\n");
  499. via_dummy_bitblt(dev_priv);
  500. via_dummy_bitblt(dev_priv);
  501. last_pause_ptr =
  502. via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi,
  503. &pause_addr_lo, 0) - 1;
  504. via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi,
  505. &pause_addr_lo, 0);
  506. *last_pause_ptr = pause_addr_lo;
  507. dma_low_save1 = dev_priv->dma_low;
  508. /*
  509. * Now, set a trap that will pause the regulator if it tries to rerun the old
  510. * command buffer. (Which may happen if via_hook_segment detecs a command regulator pause
  511. * and reissues the jump command over PCI, while the regulator has already taken the jump
  512. * and actually paused at the current buffer end).
  513. * There appears to be no other way to detect this condition, since the hw_addr_pointer
  514. * does not seem to get updated immediately when a jump occurs.
  515. */
  516. last_pause_ptr =
  517. via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi,
  518. &pause_addr_lo, 0) - 1;
  519. via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi,
  520. &pause_addr_lo, 0);
  521. *last_pause_ptr = pause_addr_lo;
  522. dma_low_save2 = dev_priv->dma_low;
  523. dev_priv->dma_low = dma_low_save1;
  524. via_hook_segment(dev_priv, jump_addr_hi, jump_addr_lo, 0);
  525. dev_priv->dma_low = dma_low_save2;
  526. via_hook_segment(dev_priv, pause_addr_hi, pause_addr_lo, 0);
  527. }
  528. static void via_cmdbuf_rewind(drm_via_private_t *dev_priv)
  529. {
  530. via_cmdbuf_jump(dev_priv);
  531. }
  532. static void via_cmdbuf_flush(drm_via_private_t *dev_priv, uint32_t cmd_type)
  533. {
  534. uint32_t pause_addr_lo, pause_addr_hi;
  535. via_align_cmd(dev_priv, cmd_type, 0, &pause_addr_hi, &pause_addr_lo, 0);
  536. via_hook_segment(dev_priv, pause_addr_hi, pause_addr_lo, 0);
  537. }
  538. static void via_cmdbuf_pause(drm_via_private_t *dev_priv)
  539. {
  540. via_cmdbuf_flush(dev_priv, HC_HAGPBpID_PAUSE);
  541. }
  542. static void via_cmdbuf_reset(drm_via_private_t *dev_priv)
  543. {
  544. via_cmdbuf_flush(dev_priv, HC_HAGPBpID_STOP);
  545. via_wait_idle(dev_priv);
  546. }
  547. /*
  548. * User interface to the space and lag functions.
  549. */
  550. static int via_cmdbuf_size(struct drm_device *dev, void *data, struct drm_file *file_priv)
  551. {
  552. drm_via_cmdbuf_size_t *d_siz = data;
  553. int ret = 0;
  554. uint32_t tmp_size, count;
  555. drm_via_private_t *dev_priv;
  556. DRM_DEBUG("\n");
  557. LOCK_TEST_WITH_RETURN(dev, file_priv);
  558. dev_priv = (drm_via_private_t *) dev->dev_private;
  559. if (dev_priv->ring.virtual_start == NULL) {
  560. DRM_ERROR("called without initializing AGP ring buffer.\n");
  561. return -EFAULT;
  562. }
  563. count = 1000000;
  564. tmp_size = d_siz->size;
  565. switch (d_siz->func) {
  566. case VIA_CMDBUF_SPACE:
  567. while (((tmp_size = via_cmdbuf_space(dev_priv)) < d_siz->size)
  568. && --count) {
  569. if (!d_siz->wait)
  570. break;
  571. }
  572. if (!count) {
  573. DRM_ERROR("VIA_CMDBUF_SPACE timed out.\n");
  574. ret = -EAGAIN;
  575. }
  576. break;
  577. case VIA_CMDBUF_LAG:
  578. while (((tmp_size = via_cmdbuf_lag(dev_priv)) > d_siz->size)
  579. && --count) {
  580. if (!d_siz->wait)
  581. break;
  582. }
  583. if (!count) {
  584. DRM_ERROR("VIA_CMDBUF_LAG timed out.\n");
  585. ret = -EAGAIN;
  586. }
  587. break;
  588. default:
  589. ret = -EFAULT;
  590. }
  591. d_siz->size = tmp_size;
  592. return ret;
  593. }
  594. const struct drm_ioctl_desc via_ioctls[] = {
  595. DRM_IOCTL_DEF_DRV(VIA_ALLOCMEM, via_mem_alloc, DRM_AUTH),
  596. DRM_IOCTL_DEF_DRV(VIA_FREEMEM, via_mem_free, DRM_AUTH),
  597. DRM_IOCTL_DEF_DRV(VIA_AGP_INIT, via_agp_init, DRM_AUTH|DRM_MASTER),
  598. DRM_IOCTL_DEF_DRV(VIA_FB_INIT, via_fb_init, DRM_AUTH|DRM_MASTER),
  599. DRM_IOCTL_DEF_DRV(VIA_MAP_INIT, via_map_init, DRM_AUTH|DRM_MASTER),
  600. DRM_IOCTL_DEF_DRV(VIA_DEC_FUTEX, via_decoder_futex, DRM_AUTH),
  601. DRM_IOCTL_DEF_DRV(VIA_DMA_INIT, via_dma_init, DRM_AUTH),
  602. DRM_IOCTL_DEF_DRV(VIA_CMDBUFFER, via_cmdbuffer, DRM_AUTH),
  603. DRM_IOCTL_DEF_DRV(VIA_FLUSH, via_flush_ioctl, DRM_AUTH),
  604. DRM_IOCTL_DEF_DRV(VIA_PCICMD, via_pci_cmdbuffer, DRM_AUTH),
  605. DRM_IOCTL_DEF_DRV(VIA_CMDBUF_SIZE, via_cmdbuf_size, DRM_AUTH),
  606. DRM_IOCTL_DEF_DRV(VIA_WAIT_IRQ, via_wait_irq, DRM_AUTH),
  607. DRM_IOCTL_DEF_DRV(VIA_DMA_BLIT, via_dma_blit, DRM_AUTH),
  608. DRM_IOCTL_DEF_DRV(VIA_BLIT_SYNC, via_dma_blit_sync, DRM_AUTH)
  609. };
  610. int via_max_ioctl = ARRAY_SIZE(via_ioctls);