via_dmablit.c 21 KB

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