via_dmablit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* via_dmablit.c -- PCI DMA BitBlt support for the VIA Unichrome/Pro
  2. *
  3. * Copyright (C) 2005 Thomas Hellstrom, All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial portions
  14. * of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Thomas Hellstrom.
  26. * Partially based on code obtained from Digeo Inc.
  27. */
  28. /*
  29. * Unmaps the DMA mappings.
  30. * FIXME: Is this a NoOp on x86? Also
  31. * FIXME: What happens if this one is called and a pending blit has previously done
  32. * the same DMA mappings?
  33. */
  34. #include "drmP.h"
  35. #include "via_drm.h"
  36. #include "via_drv.h"
  37. #include "via_dmablit.h"
  38. #include <linux/pagemap.h>
  39. #include <linux/slab.h>
  40. #define VIA_PGDN(x) (((unsigned long)(x)) & PAGE_MASK)
  41. #define VIA_PGOFF(x) (((unsigned long)(x)) & ~PAGE_MASK)
  42. #define VIA_PFN(x) ((unsigned long)(x) >> PAGE_SHIFT)
  43. typedef struct _drm_via_descriptor {
  44. uint32_t mem_addr;
  45. uint32_t dev_addr;
  46. uint32_t size;
  47. uint32_t next;
  48. } drm_via_descriptor_t;
  49. /*
  50. * Unmap a DMA mapping.
  51. */
  52. static void
  53. via_unmap_blit_from_device(struct pci_dev *pdev, drm_via_sg_info_t *vsg)
  54. {
  55. int num_desc = vsg->num_desc;
  56. unsigned cur_descriptor_page = num_desc / vsg->descriptors_per_page;
  57. unsigned descriptor_this_page = num_desc % vsg->descriptors_per_page;
  58. drm_via_descriptor_t *desc_ptr = vsg->desc_pages[cur_descriptor_page] +
  59. descriptor_this_page;
  60. dma_addr_t next = vsg->chain_start;
  61. while(num_desc--) {
  62. if (descriptor_this_page-- == 0) {
  63. cur_descriptor_page--;
  64. descriptor_this_page = vsg->descriptors_per_page - 1;
  65. desc_ptr = vsg->desc_pages[cur_descriptor_page] +
  66. descriptor_this_page;
  67. }
  68. dma_unmap_single(&pdev->dev, next, sizeof(*desc_ptr), DMA_TO_DEVICE);
  69. dma_unmap_page(&pdev->dev, desc_ptr->mem_addr, desc_ptr->size, vsg->direction);
  70. next = (dma_addr_t) desc_ptr->next;
  71. desc_ptr--;
  72. }
  73. }
  74. /*
  75. * If mode = 0, count how many descriptors are needed.
  76. * If mode = 1, Map the DMA pages for the device, put together and map also the descriptors.
  77. * Descriptors are run in reverse order by the hardware because we are not allowed to update the
  78. * 'next' field without syncing calls when the descriptor is already mapped.
  79. */
  80. static void
  81. via_map_blit_for_device(struct pci_dev *pdev,
  82. const drm_via_dmablit_t *xfer,
  83. drm_via_sg_info_t *vsg,
  84. int mode)
  85. {
  86. unsigned cur_descriptor_page = 0;
  87. unsigned num_descriptors_this_page = 0;
  88. unsigned char *mem_addr = xfer->mem_addr;
  89. unsigned char *cur_mem;
  90. unsigned char *first_addr = (unsigned char *)VIA_PGDN(mem_addr);
  91. uint32_t fb_addr = xfer->fb_addr;
  92. uint32_t cur_fb;
  93. unsigned long line_len;
  94. unsigned remaining_len;
  95. int num_desc = 0;
  96. int cur_line;
  97. dma_addr_t next = 0 | VIA_DMA_DPR_EC;
  98. drm_via_descriptor_t *desc_ptr = NULL;
  99. if (mode == 1)
  100. desc_ptr = vsg->desc_pages[cur_descriptor_page];
  101. for (cur_line = 0; cur_line < xfer->num_lines; ++cur_line) {
  102. line_len = xfer->line_length;
  103. cur_fb = fb_addr;
  104. cur_mem = mem_addr;
  105. while (line_len > 0) {
  106. remaining_len = min(PAGE_SIZE-VIA_PGOFF(cur_mem), line_len);
  107. line_len -= remaining_len;
  108. if (mode == 1) {
  109. desc_ptr->mem_addr =
  110. dma_map_page(&pdev->dev,
  111. vsg->pages[VIA_PFN(cur_mem) -
  112. VIA_PFN(first_addr)],
  113. VIA_PGOFF(cur_mem), remaining_len,
  114. vsg->direction);
  115. desc_ptr->dev_addr = cur_fb;
  116. desc_ptr->size = remaining_len;
  117. desc_ptr->next = (uint32_t) next;
  118. next = dma_map_single(&pdev->dev, desc_ptr, sizeof(*desc_ptr),
  119. DMA_TO_DEVICE);
  120. desc_ptr++;
  121. if (++num_descriptors_this_page >= vsg->descriptors_per_page) {
  122. num_descriptors_this_page = 0;
  123. desc_ptr = vsg->desc_pages[++cur_descriptor_page];
  124. }
  125. }
  126. num_desc++;
  127. cur_mem += remaining_len;
  128. cur_fb += remaining_len;
  129. }
  130. mem_addr += xfer->mem_stride;
  131. fb_addr += xfer->fb_stride;
  132. }
  133. if (mode == 1) {
  134. vsg->chain_start = next;
  135. vsg->state = dr_via_device_mapped;
  136. }
  137. vsg->num_desc = num_desc;
  138. }
  139. /*
  140. * Function that frees up all resources for a blit. It is usable even if the
  141. * blit info has only been partially built as long as the status enum is consistent
  142. * with the actual status of the used resources.
  143. */
  144. static void
  145. via_free_sg_info(struct pci_dev *pdev, drm_via_sg_info_t *vsg)
  146. {
  147. struct page *page;
  148. int i;
  149. switch(vsg->state) {
  150. case dr_via_device_mapped:
  151. via_unmap_blit_from_device(pdev, vsg);
  152. case dr_via_desc_pages_alloc:
  153. for (i=0; i<vsg->num_desc_pages; ++i) {
  154. if (vsg->desc_pages[i] != NULL)
  155. free_page((unsigned long)vsg->desc_pages[i]);
  156. }
  157. kfree(vsg->desc_pages);
  158. case dr_via_pages_locked:
  159. for (i=0; i<vsg->num_pages; ++i) {
  160. if ( NULL != (page = vsg->pages[i])) {
  161. if (! PageReserved(page) && (DMA_FROM_DEVICE == vsg->direction))
  162. SetPageDirty(page);
  163. page_cache_release(page);
  164. }
  165. }
  166. case dr_via_pages_alloc:
  167. vfree(vsg->pages);
  168. default:
  169. vsg->state = dr_via_sg_init;
  170. }
  171. vfree(vsg->bounce_buffer);
  172. vsg->bounce_buffer = NULL;
  173. vsg->free_on_sequence = 0;
  174. }
  175. /*
  176. * Fire a blit engine.
  177. */
  178. static void
  179. via_fire_dmablit(struct drm_device *dev, drm_via_sg_info_t *vsg, int engine)
  180. {
  181. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  182. VIA_WRITE(VIA_PCI_DMA_MAR0 + engine*0x10, 0);
  183. VIA_WRITE(VIA_PCI_DMA_DAR0 + engine*0x10, 0);
  184. VIA_WRITE(VIA_PCI_DMA_CSR0 + engine*0x04, VIA_DMA_CSR_DD | VIA_DMA_CSR_TD |
  185. VIA_DMA_CSR_DE);
  186. VIA_WRITE(VIA_PCI_DMA_MR0 + engine*0x04, VIA_DMA_MR_CM | VIA_DMA_MR_TDIE);
  187. VIA_WRITE(VIA_PCI_DMA_BCR0 + engine*0x10, 0);
  188. VIA_WRITE(VIA_PCI_DMA_DPR0 + engine*0x10, vsg->chain_start);
  189. DRM_WRITEMEMORYBARRIER();
  190. VIA_WRITE(VIA_PCI_DMA_CSR0 + engine*0x04, VIA_DMA_CSR_DE | VIA_DMA_CSR_TS);
  191. VIA_READ(VIA_PCI_DMA_CSR0 + engine*0x04);
  192. }
  193. /*
  194. * Obtain a page pointer array and lock all pages into system memory. A segmentation violation will
  195. * occur here if the calling user does not have access to the submitted address.
  196. */
  197. static int
  198. via_lock_all_dma_pages(drm_via_sg_info_t *vsg, drm_via_dmablit_t *xfer)
  199. {
  200. int ret;
  201. unsigned long first_pfn = VIA_PFN(xfer->mem_addr);
  202. vsg->num_pages = VIA_PFN(xfer->mem_addr + (xfer->num_lines * xfer->mem_stride -1)) -
  203. first_pfn + 1;
  204. if (NULL == (vsg->pages = vmalloc(sizeof(struct page *) * vsg->num_pages)))
  205. return -ENOMEM;
  206. memset(vsg->pages, 0, sizeof(struct page *) * vsg->num_pages);
  207. down_read(&current->mm->mmap_sem);
  208. ret = get_user_pages(current, current->mm,
  209. (unsigned long)xfer->mem_addr,
  210. vsg->num_pages,
  211. (vsg->direction == DMA_FROM_DEVICE),
  212. 0, vsg->pages, NULL);
  213. up_read(&current->mm->mmap_sem);
  214. if (ret != vsg->num_pages) {
  215. if (ret < 0)
  216. return ret;
  217. vsg->state = dr_via_pages_locked;
  218. return -EINVAL;
  219. }
  220. vsg->state = dr_via_pages_locked;
  221. DRM_DEBUG("DMA pages locked\n");
  222. return 0;
  223. }
  224. /*
  225. * Allocate DMA capable memory for the blit descriptor chain, and an array that keeps track of the
  226. * pages we allocate. We don't want to use kmalloc for the descriptor chain because it may be
  227. * quite large for some blits, and pages don't need to be contingous.
  228. */
  229. static int
  230. via_alloc_desc_pages(drm_via_sg_info_t *vsg)
  231. {
  232. int i;
  233. vsg->descriptors_per_page = PAGE_SIZE / sizeof( drm_via_descriptor_t);
  234. vsg->num_desc_pages = (vsg->num_desc + vsg->descriptors_per_page - 1) /
  235. vsg->descriptors_per_page;
  236. if (NULL == (vsg->desc_pages = kcalloc(vsg->num_desc_pages, sizeof(void *), GFP_KERNEL)))
  237. return -ENOMEM;
  238. vsg->state = dr_via_desc_pages_alloc;
  239. for (i=0; i<vsg->num_desc_pages; ++i) {
  240. if (NULL == (vsg->desc_pages[i] =
  241. (drm_via_descriptor_t *) __get_free_page(GFP_KERNEL)))
  242. return -ENOMEM;
  243. }
  244. DRM_DEBUG("Allocated %d pages for %d descriptors.\n", vsg->num_desc_pages,
  245. vsg->num_desc);
  246. return 0;
  247. }
  248. static void
  249. via_abort_dmablit(struct drm_device *dev, int engine)
  250. {
  251. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  252. VIA_WRITE(VIA_PCI_DMA_CSR0 + engine*0x04, VIA_DMA_CSR_TA);
  253. }
  254. static void
  255. via_dmablit_engine_off(struct drm_device *dev, int engine)
  256. {
  257. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  258. VIA_WRITE(VIA_PCI_DMA_CSR0 + engine*0x04, VIA_DMA_CSR_TD | VIA_DMA_CSR_DD);
  259. }
  260. /*
  261. * The dmablit part of the IRQ handler. Trying to do only reasonably fast things here.
  262. * The rest, like unmapping and freeing memory for done blits is done in a separate workqueue
  263. * task. Basically the task of the interrupt handler is to submit a new blit to the engine, while
  264. * the workqueue task takes care of processing associated with the old blit.
  265. */
  266. void
  267. via_dmablit_handler(struct drm_device *dev, int engine, int from_irq)
  268. {
  269. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  270. drm_via_blitq_t *blitq = dev_priv->blit_queues + engine;
  271. int cur;
  272. int done_transfer;
  273. unsigned long irqsave=0;
  274. uint32_t status = 0;
  275. DRM_DEBUG("DMA blit handler called. engine = %d, from_irq = %d, blitq = 0x%lx\n",
  276. engine, from_irq, (unsigned long) blitq);
  277. if (from_irq) {
  278. spin_lock(&blitq->blit_lock);
  279. } else {
  280. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  281. }
  282. done_transfer = blitq->is_active &&
  283. (( status = VIA_READ(VIA_PCI_DMA_CSR0 + engine*0x04)) & VIA_DMA_CSR_TD);
  284. done_transfer = done_transfer || ( blitq->aborting && !(status & VIA_DMA_CSR_DE));
  285. cur = blitq->cur;
  286. if (done_transfer) {
  287. blitq->blits[cur]->aborted = blitq->aborting;
  288. blitq->done_blit_handle++;
  289. DRM_WAKEUP(blitq->blit_queue + cur);
  290. cur++;
  291. if (cur >= VIA_NUM_BLIT_SLOTS)
  292. cur = 0;
  293. blitq->cur = cur;
  294. /*
  295. * Clear transfer done flag.
  296. */
  297. VIA_WRITE(VIA_PCI_DMA_CSR0 + engine*0x04, VIA_DMA_CSR_TD);
  298. blitq->is_active = 0;
  299. blitq->aborting = 0;
  300. schedule_work(&blitq->wq);
  301. } else if (blitq->is_active && time_after_eq(jiffies, blitq->end)) {
  302. /*
  303. * Abort transfer after one second.
  304. */
  305. via_abort_dmablit(dev, engine);
  306. blitq->aborting = 1;
  307. blitq->end = jiffies + DRM_HZ;
  308. }
  309. if (!blitq->is_active) {
  310. if (blitq->num_outstanding) {
  311. via_fire_dmablit(dev, blitq->blits[cur], engine);
  312. blitq->is_active = 1;
  313. blitq->cur = cur;
  314. blitq->num_outstanding--;
  315. blitq->end = jiffies + DRM_HZ;
  316. if (!timer_pending(&blitq->poll_timer))
  317. mod_timer(&blitq->poll_timer, jiffies + 1);
  318. } else {
  319. if (timer_pending(&blitq->poll_timer)) {
  320. del_timer(&blitq->poll_timer);
  321. }
  322. via_dmablit_engine_off(dev, engine);
  323. }
  324. }
  325. if (from_irq) {
  326. spin_unlock(&blitq->blit_lock);
  327. } else {
  328. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  329. }
  330. }
  331. /*
  332. * Check whether this blit is still active, performing necessary locking.
  333. */
  334. static int
  335. via_dmablit_active(drm_via_blitq_t *blitq, int engine, uint32_t handle, wait_queue_head_t **queue)
  336. {
  337. unsigned long irqsave;
  338. uint32_t slot;
  339. int active;
  340. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  341. /*
  342. * Allow for handle wraparounds.
  343. */
  344. active = ((blitq->done_blit_handle - handle) > (1 << 23)) &&
  345. ((blitq->cur_blit_handle - handle) <= (1 << 23));
  346. if (queue && active) {
  347. slot = handle - blitq->done_blit_handle + blitq->cur -1;
  348. if (slot >= VIA_NUM_BLIT_SLOTS) {
  349. slot -= VIA_NUM_BLIT_SLOTS;
  350. }
  351. *queue = blitq->blit_queue + slot;
  352. }
  353. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  354. return active;
  355. }
  356. /*
  357. * Sync. Wait for at least three seconds for the blit to be performed.
  358. */
  359. static int
  360. via_dmablit_sync(struct drm_device *dev, uint32_t handle, int engine)
  361. {
  362. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  363. drm_via_blitq_t *blitq = dev_priv->blit_queues + engine;
  364. wait_queue_head_t *queue;
  365. int ret = 0;
  366. if (via_dmablit_active(blitq, engine, handle, &queue)) {
  367. DRM_WAIT_ON(ret, *queue, 3 * DRM_HZ,
  368. !via_dmablit_active(blitq, engine, handle, NULL));
  369. }
  370. DRM_DEBUG("DMA blit sync handle 0x%x engine %d returned %d\n",
  371. handle, engine, ret);
  372. return ret;
  373. }
  374. /*
  375. * A timer that regularly polls the blit engine in cases where we don't have interrupts:
  376. * a) Broken hardware (typically those that don't have any video capture facility).
  377. * b) Blit abort. The hardware doesn't send an interrupt when a blit is aborted.
  378. * The timer and hardware IRQ's can and do work in parallel. If the hardware has
  379. * irqs, it will shorten the latency somewhat.
  380. */
  381. static void
  382. via_dmablit_timer(unsigned long data)
  383. {
  384. drm_via_blitq_t *blitq = (drm_via_blitq_t *) data;
  385. struct drm_device *dev = blitq->dev;
  386. int engine = (int)
  387. (blitq - ((drm_via_private_t *)dev->dev_private)->blit_queues);
  388. DRM_DEBUG("Polling timer called for engine %d, jiffies %lu\n", engine,
  389. (unsigned long) jiffies);
  390. via_dmablit_handler(dev, engine, 0);
  391. if (!timer_pending(&blitq->poll_timer)) {
  392. mod_timer(&blitq->poll_timer, jiffies + 1);
  393. /*
  394. * Rerun handler to delete timer if engines are off, and
  395. * to shorten abort latency. This is a little nasty.
  396. */
  397. via_dmablit_handler(dev, engine, 0);
  398. }
  399. }
  400. /*
  401. * Workqueue task that frees data and mappings associated with a blit.
  402. * Also wakes up waiting processes. Each of these tasks handles one
  403. * blit engine only and may not be called on each interrupt.
  404. */
  405. static void
  406. via_dmablit_workqueue(struct work_struct *work)
  407. {
  408. drm_via_blitq_t *blitq = container_of(work, drm_via_blitq_t, wq);
  409. struct drm_device *dev = blitq->dev;
  410. unsigned long irqsave;
  411. drm_via_sg_info_t *cur_sg;
  412. int cur_released;
  413. DRM_DEBUG("Workqueue task called for blit engine %ld\n",(unsigned long)
  414. (blitq - ((drm_via_private_t *)dev->dev_private)->blit_queues));
  415. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  416. while(blitq->serviced != blitq->cur) {
  417. cur_released = blitq->serviced++;
  418. DRM_DEBUG("Releasing blit slot %d\n", cur_released);
  419. if (blitq->serviced >= VIA_NUM_BLIT_SLOTS)
  420. blitq->serviced = 0;
  421. cur_sg = blitq->blits[cur_released];
  422. blitq->num_free++;
  423. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  424. DRM_WAKEUP(&blitq->busy_queue);
  425. via_free_sg_info(dev->pdev, cur_sg);
  426. kfree(cur_sg);
  427. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  428. }
  429. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  430. }
  431. /*
  432. * Init all blit engines. Currently we use two, but some hardware have 4.
  433. */
  434. void
  435. via_init_dmablit(struct drm_device *dev)
  436. {
  437. int i,j;
  438. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  439. drm_via_blitq_t *blitq;
  440. pci_set_master(dev->pdev);
  441. for (i=0; i< VIA_NUM_BLIT_ENGINES; ++i) {
  442. blitq = dev_priv->blit_queues + i;
  443. blitq->dev = dev;
  444. blitq->cur_blit_handle = 0;
  445. blitq->done_blit_handle = 0;
  446. blitq->head = 0;
  447. blitq->cur = 0;
  448. blitq->serviced = 0;
  449. blitq->num_free = VIA_NUM_BLIT_SLOTS - 1;
  450. blitq->num_outstanding = 0;
  451. blitq->is_active = 0;
  452. blitq->aborting = 0;
  453. spin_lock_init(&blitq->blit_lock);
  454. for (j=0; j<VIA_NUM_BLIT_SLOTS; ++j) {
  455. DRM_INIT_WAITQUEUE(blitq->blit_queue + j);
  456. }
  457. DRM_INIT_WAITQUEUE(&blitq->busy_queue);
  458. INIT_WORK(&blitq->wq, via_dmablit_workqueue);
  459. setup_timer(&blitq->poll_timer, via_dmablit_timer,
  460. (unsigned long)blitq);
  461. }
  462. }
  463. /*
  464. * Build all info and do all mappings required for a blit.
  465. */
  466. static int
  467. via_build_sg_info(struct drm_device *dev, drm_via_sg_info_t *vsg, drm_via_dmablit_t *xfer)
  468. {
  469. int draw = xfer->to_fb;
  470. int ret = 0;
  471. vsg->direction = (draw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  472. vsg->bounce_buffer = NULL;
  473. vsg->state = dr_via_sg_init;
  474. if (xfer->num_lines <= 0 || xfer->line_length <= 0) {
  475. DRM_ERROR("Zero size bitblt.\n");
  476. return -EINVAL;
  477. }
  478. /*
  479. * Below check is a driver limitation, not a hardware one. We
  480. * don't want to lock unused pages, and don't want to incoporate the
  481. * extra logic of avoiding them. Make sure there are no.
  482. * (Not a big limitation anyway.)
  483. */
  484. if ((xfer->mem_stride - xfer->line_length) > 2*PAGE_SIZE) {
  485. DRM_ERROR("Too large system memory stride. Stride: %d, "
  486. "Length: %d\n", xfer->mem_stride, xfer->line_length);
  487. return -EINVAL;
  488. }
  489. if ((xfer->mem_stride == xfer->line_length) &&
  490. (xfer->fb_stride == xfer->line_length)) {
  491. xfer->mem_stride *= xfer->num_lines;
  492. xfer->line_length = xfer->mem_stride;
  493. xfer->fb_stride = xfer->mem_stride;
  494. xfer->num_lines = 1;
  495. }
  496. /*
  497. * Don't lock an arbitrary large number of pages, since that causes a
  498. * DOS security hole.
  499. */
  500. if (xfer->num_lines > 2048 || (xfer->num_lines*xfer->mem_stride > (2048*2048*4))) {
  501. DRM_ERROR("Too large PCI DMA bitblt.\n");
  502. return -EINVAL;
  503. }
  504. /*
  505. * we allow a negative fb stride to allow flipping of images in
  506. * transfer.
  507. */
  508. if (xfer->mem_stride < xfer->line_length ||
  509. abs(xfer->fb_stride) < xfer->line_length) {
  510. DRM_ERROR("Invalid frame-buffer / memory stride.\n");
  511. return -EINVAL;
  512. }
  513. /*
  514. * A hardware bug seems to be worked around if system memory addresses start on
  515. * 16 byte boundaries. This seems a bit restrictive however. VIA is contacted
  516. * about this. Meanwhile, impose the following restrictions:
  517. */
  518. #ifdef VIA_BUGFREE
  519. if ((((unsigned long)xfer->mem_addr & 3) != ((unsigned long)xfer->fb_addr & 3)) ||
  520. ((xfer->num_lines > 1) && ((xfer->mem_stride & 3) != (xfer->fb_stride & 3)))) {
  521. DRM_ERROR("Invalid DRM bitblt alignment.\n");
  522. return -EINVAL;
  523. }
  524. #else
  525. if ((((unsigned long)xfer->mem_addr & 15) ||
  526. ((unsigned long)xfer->fb_addr & 3)) ||
  527. ((xfer->num_lines > 1) &&
  528. ((xfer->mem_stride & 15) || (xfer->fb_stride & 3)))) {
  529. DRM_ERROR("Invalid DRM bitblt alignment.\n");
  530. return -EINVAL;
  531. }
  532. #endif
  533. if (0 != (ret = via_lock_all_dma_pages(vsg, xfer))) {
  534. DRM_ERROR("Could not lock DMA pages.\n");
  535. via_free_sg_info(dev->pdev, vsg);
  536. return ret;
  537. }
  538. via_map_blit_for_device(dev->pdev, xfer, vsg, 0);
  539. if (0 != (ret = via_alloc_desc_pages(vsg))) {
  540. DRM_ERROR("Could not allocate DMA descriptor pages.\n");
  541. via_free_sg_info(dev->pdev, vsg);
  542. return ret;
  543. }
  544. via_map_blit_for_device(dev->pdev, xfer, vsg, 1);
  545. return 0;
  546. }
  547. /*
  548. * Reserve one free slot in the blit queue. Will wait for one second for one
  549. * to become available. Otherwise -EBUSY is returned.
  550. */
  551. static int
  552. via_dmablit_grab_slot(drm_via_blitq_t *blitq, int engine)
  553. {
  554. int ret=0;
  555. unsigned long irqsave;
  556. DRM_DEBUG("Num free is %d\n", blitq->num_free);
  557. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  558. while(blitq->num_free == 0) {
  559. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  560. DRM_WAIT_ON(ret, blitq->busy_queue, DRM_HZ, blitq->num_free > 0);
  561. if (ret) {
  562. return (-EINTR == ret) ? -EAGAIN : ret;
  563. }
  564. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  565. }
  566. blitq->num_free--;
  567. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  568. return 0;
  569. }
  570. /*
  571. * Hand back a free slot if we changed our mind.
  572. */
  573. static void
  574. via_dmablit_release_slot(drm_via_blitq_t *blitq)
  575. {
  576. unsigned long irqsave;
  577. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  578. blitq->num_free++;
  579. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  580. DRM_WAKEUP( &blitq->busy_queue );
  581. }
  582. /*
  583. * Grab a free slot. Build blit info and queue a blit.
  584. */
  585. static int
  586. via_dmablit(struct drm_device *dev, drm_via_dmablit_t *xfer)
  587. {
  588. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  589. drm_via_sg_info_t *vsg;
  590. drm_via_blitq_t *blitq;
  591. int ret;
  592. int engine;
  593. unsigned long irqsave;
  594. if (dev_priv == NULL) {
  595. DRM_ERROR("Called without initialization.\n");
  596. return -EINVAL;
  597. }
  598. engine = (xfer->to_fb) ? 0 : 1;
  599. blitq = dev_priv->blit_queues + engine;
  600. if (0 != (ret = via_dmablit_grab_slot(blitq, engine))) {
  601. return ret;
  602. }
  603. if (NULL == (vsg = kmalloc(sizeof(*vsg), GFP_KERNEL))) {
  604. via_dmablit_release_slot(blitq);
  605. return -ENOMEM;
  606. }
  607. if (0 != (ret = via_build_sg_info(dev, vsg, xfer))) {
  608. via_dmablit_release_slot(blitq);
  609. kfree(vsg);
  610. return ret;
  611. }
  612. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  613. blitq->blits[blitq->head++] = vsg;
  614. if (blitq->head >= VIA_NUM_BLIT_SLOTS)
  615. blitq->head = 0;
  616. blitq->num_outstanding++;
  617. xfer->sync.sync_handle = ++blitq->cur_blit_handle;
  618. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  619. xfer->sync.engine = engine;
  620. via_dmablit_handler(dev, engine, 0);
  621. return 0;
  622. }
  623. /*
  624. * Sync on a previously submitted blit. Note that the X server use signals extensively, and
  625. * that there is a very big probability that this IOCTL will be interrupted by a signal. In that
  626. * case it returns with -EAGAIN for the signal to be delivered.
  627. * The caller should then reissue the IOCTL. This is similar to what is being done for drmGetLock().
  628. */
  629. int
  630. via_dma_blit_sync( struct drm_device *dev, void *data, struct drm_file *file_priv )
  631. {
  632. drm_via_blitsync_t *sync = data;
  633. int err;
  634. if (sync->engine >= VIA_NUM_BLIT_ENGINES)
  635. return -EINVAL;
  636. err = via_dmablit_sync(dev, sync->sync_handle, sync->engine);
  637. if (-EINTR == err)
  638. err = -EAGAIN;
  639. return err;
  640. }
  641. /*
  642. * Queue a blit and hand back a handle to be used for sync. This IOCTL may be interrupted by a signal
  643. * while waiting for a free slot in the blit queue. In that case it returns with -EAGAIN and should
  644. * be reissued. See the above IOCTL code.
  645. */
  646. int
  647. via_dma_blit( struct drm_device *dev, void *data, struct drm_file *file_priv )
  648. {
  649. drm_via_dmablit_t *xfer = data;
  650. int err;
  651. err = via_dmablit(dev, xfer);
  652. return err;
  653. }