via_dmablit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 <drm/drmP.h>
  35. #include <drm/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. wmb();
  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. vsg->pages = vzalloc(sizeof(struct page *) * vsg->num_pages);
  205. if (NULL == vsg->pages)
  206. return -ENOMEM;
  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) ? FOLL_WRITE : 0,
  212. 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 contiguous.
  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. 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. wake_up(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 + 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 + 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. via_dmablit_engine_off(dev, engine);
  321. }
  322. }
  323. if (from_irq)
  324. spin_unlock(&blitq->blit_lock);
  325. else
  326. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  327. }
  328. /*
  329. * Check whether this blit is still active, performing necessary locking.
  330. */
  331. static int
  332. via_dmablit_active(drm_via_blitq_t *blitq, int engine, uint32_t handle, wait_queue_head_t **queue)
  333. {
  334. unsigned long irqsave;
  335. uint32_t slot;
  336. int active;
  337. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  338. /*
  339. * Allow for handle wraparounds.
  340. */
  341. active = ((blitq->done_blit_handle - handle) > (1 << 23)) &&
  342. ((blitq->cur_blit_handle - handle) <= (1 << 23));
  343. if (queue && active) {
  344. slot = handle - blitq->done_blit_handle + blitq->cur - 1;
  345. if (slot >= VIA_NUM_BLIT_SLOTS)
  346. slot -= VIA_NUM_BLIT_SLOTS;
  347. *queue = blitq->blit_queue + slot;
  348. }
  349. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  350. return active;
  351. }
  352. /*
  353. * Sync. Wait for at least three seconds for the blit to be performed.
  354. */
  355. static int
  356. via_dmablit_sync(struct drm_device *dev, uint32_t handle, int engine)
  357. {
  358. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  359. drm_via_blitq_t *blitq = dev_priv->blit_queues + engine;
  360. wait_queue_head_t *queue;
  361. int ret = 0;
  362. if (via_dmablit_active(blitq, engine, handle, &queue)) {
  363. DRM_WAIT_ON(ret, *queue, 3 * HZ,
  364. !via_dmablit_active(blitq, engine, handle, NULL));
  365. }
  366. DRM_DEBUG("DMA blit sync handle 0x%x engine %d returned %d\n",
  367. handle, engine, ret);
  368. return ret;
  369. }
  370. /*
  371. * A timer that regularly polls the blit engine in cases where we don't have interrupts:
  372. * a) Broken hardware (typically those that don't have any video capture facility).
  373. * b) Blit abort. The hardware doesn't send an interrupt when a blit is aborted.
  374. * The timer and hardware IRQ's can and do work in parallel. If the hardware has
  375. * irqs, it will shorten the latency somewhat.
  376. */
  377. static void
  378. via_dmablit_timer(unsigned long data)
  379. {
  380. drm_via_blitq_t *blitq = (drm_via_blitq_t *) data;
  381. struct drm_device *dev = blitq->dev;
  382. int engine = (int)
  383. (blitq - ((drm_via_private_t *)dev->dev_private)->blit_queues);
  384. DRM_DEBUG("Polling timer called for engine %d, jiffies %lu\n", engine,
  385. (unsigned long) jiffies);
  386. via_dmablit_handler(dev, engine, 0);
  387. if (!timer_pending(&blitq->poll_timer)) {
  388. mod_timer(&blitq->poll_timer, jiffies + 1);
  389. /*
  390. * Rerun handler to delete timer if engines are off, and
  391. * to shorten abort latency. This is a little nasty.
  392. */
  393. via_dmablit_handler(dev, engine, 0);
  394. }
  395. }
  396. /*
  397. * Workqueue task that frees data and mappings associated with a blit.
  398. * Also wakes up waiting processes. Each of these tasks handles one
  399. * blit engine only and may not be called on each interrupt.
  400. */
  401. static void
  402. via_dmablit_workqueue(struct work_struct *work)
  403. {
  404. drm_via_blitq_t *blitq = container_of(work, drm_via_blitq_t, wq);
  405. struct drm_device *dev = blitq->dev;
  406. unsigned long irqsave;
  407. drm_via_sg_info_t *cur_sg;
  408. int cur_released;
  409. DRM_DEBUG("Workqueue task called for blit engine %ld\n", (unsigned long)
  410. (blitq - ((drm_via_private_t *)dev->dev_private)->blit_queues));
  411. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  412. while (blitq->serviced != blitq->cur) {
  413. cur_released = blitq->serviced++;
  414. DRM_DEBUG("Releasing blit slot %d\n", cur_released);
  415. if (blitq->serviced >= VIA_NUM_BLIT_SLOTS)
  416. blitq->serviced = 0;
  417. cur_sg = blitq->blits[cur_released];
  418. blitq->num_free++;
  419. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  420. wake_up(&blitq->busy_queue);
  421. via_free_sg_info(dev->pdev, cur_sg);
  422. kfree(cur_sg);
  423. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  424. }
  425. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  426. }
  427. /*
  428. * Init all blit engines. Currently we use two, but some hardware have 4.
  429. */
  430. void
  431. via_init_dmablit(struct drm_device *dev)
  432. {
  433. int i, j;
  434. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  435. drm_via_blitq_t *blitq;
  436. pci_set_master(dev->pdev);
  437. for (i = 0; i < VIA_NUM_BLIT_ENGINES; ++i) {
  438. blitq = dev_priv->blit_queues + i;
  439. blitq->dev = dev;
  440. blitq->cur_blit_handle = 0;
  441. blitq->done_blit_handle = 0;
  442. blitq->head = 0;
  443. blitq->cur = 0;
  444. blitq->serviced = 0;
  445. blitq->num_free = VIA_NUM_BLIT_SLOTS - 1;
  446. blitq->num_outstanding = 0;
  447. blitq->is_active = 0;
  448. blitq->aborting = 0;
  449. spin_lock_init(&blitq->blit_lock);
  450. for (j = 0; j < VIA_NUM_BLIT_SLOTS; ++j)
  451. init_waitqueue_head(blitq->blit_queue + j);
  452. init_waitqueue_head(&blitq->busy_queue);
  453. INIT_WORK(&blitq->wq, via_dmablit_workqueue);
  454. setup_timer(&blitq->poll_timer, via_dmablit_timer,
  455. (unsigned long)blitq);
  456. }
  457. }
  458. /*
  459. * Build all info and do all mappings required for a blit.
  460. */
  461. static int
  462. via_build_sg_info(struct drm_device *dev, drm_via_sg_info_t *vsg, drm_via_dmablit_t *xfer)
  463. {
  464. int draw = xfer->to_fb;
  465. int ret = 0;
  466. vsg->direction = (draw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  467. vsg->bounce_buffer = NULL;
  468. vsg->state = dr_via_sg_init;
  469. if (xfer->num_lines <= 0 || xfer->line_length <= 0) {
  470. DRM_ERROR("Zero size bitblt.\n");
  471. return -EINVAL;
  472. }
  473. /*
  474. * Below check is a driver limitation, not a hardware one. We
  475. * don't want to lock unused pages, and don't want to incoporate the
  476. * extra logic of avoiding them. Make sure there are no.
  477. * (Not a big limitation anyway.)
  478. */
  479. if ((xfer->mem_stride - xfer->line_length) > 2*PAGE_SIZE) {
  480. DRM_ERROR("Too large system memory stride. Stride: %d, "
  481. "Length: %d\n", xfer->mem_stride, xfer->line_length);
  482. return -EINVAL;
  483. }
  484. if ((xfer->mem_stride == xfer->line_length) &&
  485. (xfer->fb_stride == xfer->line_length)) {
  486. xfer->mem_stride *= xfer->num_lines;
  487. xfer->line_length = xfer->mem_stride;
  488. xfer->fb_stride = xfer->mem_stride;
  489. xfer->num_lines = 1;
  490. }
  491. /*
  492. * Don't lock an arbitrary large number of pages, since that causes a
  493. * DOS security hole.
  494. */
  495. if (xfer->num_lines > 2048 || (xfer->num_lines*xfer->mem_stride > (2048*2048*4))) {
  496. DRM_ERROR("Too large PCI DMA bitblt.\n");
  497. return -EINVAL;
  498. }
  499. /*
  500. * we allow a negative fb stride to allow flipping of images in
  501. * transfer.
  502. */
  503. if (xfer->mem_stride < xfer->line_length ||
  504. abs(xfer->fb_stride) < xfer->line_length) {
  505. DRM_ERROR("Invalid frame-buffer / memory stride.\n");
  506. return -EINVAL;
  507. }
  508. /*
  509. * A hardware bug seems to be worked around if system memory addresses start on
  510. * 16 byte boundaries. This seems a bit restrictive however. VIA is contacted
  511. * about this. Meanwhile, impose the following restrictions:
  512. */
  513. #ifdef VIA_BUGFREE
  514. if ((((unsigned long)xfer->mem_addr & 3) != ((unsigned long)xfer->fb_addr & 3)) ||
  515. ((xfer->num_lines > 1) && ((xfer->mem_stride & 3) != (xfer->fb_stride & 3)))) {
  516. DRM_ERROR("Invalid DRM bitblt alignment.\n");
  517. return -EINVAL;
  518. }
  519. #else
  520. if ((((unsigned long)xfer->mem_addr & 15) ||
  521. ((unsigned long)xfer->fb_addr & 3)) ||
  522. ((xfer->num_lines > 1) &&
  523. ((xfer->mem_stride & 15) || (xfer->fb_stride & 3)))) {
  524. DRM_ERROR("Invalid DRM bitblt alignment.\n");
  525. return -EINVAL;
  526. }
  527. #endif
  528. if (0 != (ret = via_lock_all_dma_pages(vsg, xfer))) {
  529. DRM_ERROR("Could not lock DMA pages.\n");
  530. via_free_sg_info(dev->pdev, vsg);
  531. return ret;
  532. }
  533. via_map_blit_for_device(dev->pdev, xfer, vsg, 0);
  534. if (0 != (ret = via_alloc_desc_pages(vsg))) {
  535. DRM_ERROR("Could not allocate DMA descriptor pages.\n");
  536. via_free_sg_info(dev->pdev, vsg);
  537. return ret;
  538. }
  539. via_map_blit_for_device(dev->pdev, xfer, vsg, 1);
  540. return 0;
  541. }
  542. /*
  543. * Reserve one free slot in the blit queue. Will wait for one second for one
  544. * to become available. Otherwise -EBUSY is returned.
  545. */
  546. static int
  547. via_dmablit_grab_slot(drm_via_blitq_t *blitq, int engine)
  548. {
  549. int ret = 0;
  550. unsigned long irqsave;
  551. DRM_DEBUG("Num free is %d\n", blitq->num_free);
  552. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  553. while (blitq->num_free == 0) {
  554. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  555. DRM_WAIT_ON(ret, blitq->busy_queue, HZ, blitq->num_free > 0);
  556. if (ret)
  557. return (-EINTR == ret) ? -EAGAIN : ret;
  558. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  559. }
  560. blitq->num_free--;
  561. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  562. return 0;
  563. }
  564. /*
  565. * Hand back a free slot if we changed our mind.
  566. */
  567. static void
  568. via_dmablit_release_slot(drm_via_blitq_t *blitq)
  569. {
  570. unsigned long irqsave;
  571. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  572. blitq->num_free++;
  573. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  574. wake_up(&blitq->busy_queue);
  575. }
  576. /*
  577. * Grab a free slot. Build blit info and queue a blit.
  578. */
  579. static int
  580. via_dmablit(struct drm_device *dev, drm_via_dmablit_t *xfer)
  581. {
  582. drm_via_private_t *dev_priv = (drm_via_private_t *)dev->dev_private;
  583. drm_via_sg_info_t *vsg;
  584. drm_via_blitq_t *blitq;
  585. int ret;
  586. int engine;
  587. unsigned long irqsave;
  588. if (dev_priv == NULL) {
  589. DRM_ERROR("Called without initialization.\n");
  590. return -EINVAL;
  591. }
  592. engine = (xfer->to_fb) ? 0 : 1;
  593. blitq = dev_priv->blit_queues + engine;
  594. if (0 != (ret = via_dmablit_grab_slot(blitq, engine)))
  595. return ret;
  596. if (NULL == (vsg = kmalloc(sizeof(*vsg), GFP_KERNEL))) {
  597. via_dmablit_release_slot(blitq);
  598. return -ENOMEM;
  599. }
  600. if (0 != (ret = via_build_sg_info(dev, vsg, xfer))) {
  601. via_dmablit_release_slot(blitq);
  602. kfree(vsg);
  603. return ret;
  604. }
  605. spin_lock_irqsave(&blitq->blit_lock, irqsave);
  606. blitq->blits[blitq->head++] = vsg;
  607. if (blitq->head >= VIA_NUM_BLIT_SLOTS)
  608. blitq->head = 0;
  609. blitq->num_outstanding++;
  610. xfer->sync.sync_handle = ++blitq->cur_blit_handle;
  611. spin_unlock_irqrestore(&blitq->blit_lock, irqsave);
  612. xfer->sync.engine = engine;
  613. via_dmablit_handler(dev, engine, 0);
  614. return 0;
  615. }
  616. /*
  617. * Sync on a previously submitted blit. Note that the X server use signals extensively, and
  618. * that there is a very big probability that this IOCTL will be interrupted by a signal. In that
  619. * case it returns with -EAGAIN for the signal to be delivered.
  620. * The caller should then reissue the IOCTL. This is similar to what is being done for drmGetLock().
  621. */
  622. int
  623. via_dma_blit_sync(struct drm_device *dev, void *data, struct drm_file *file_priv)
  624. {
  625. drm_via_blitsync_t *sync = data;
  626. int err;
  627. if (sync->engine >= VIA_NUM_BLIT_ENGINES)
  628. return -EINVAL;
  629. err = via_dmablit_sync(dev, sync->sync_handle, sync->engine);
  630. if (-EINTR == err)
  631. err = -EAGAIN;
  632. return err;
  633. }
  634. /*
  635. * Queue a blit and hand back a handle to be used for sync. This IOCTL may be interrupted by a signal
  636. * while waiting for a free slot in the blit queue. In that case it returns with -EAGAIN and should
  637. * be reissued. See the above IOCTL code.
  638. */
  639. int
  640. via_dma_blit(struct drm_device *dev, void *data, struct drm_file *file_priv)
  641. {
  642. drm_via_dmablit_t *xfer = data;
  643. int err;
  644. err = via_dmablit(dev, xfer);
  645. return err;
  646. }