via_dmablit.c 21 KB

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