via_dmablit.c 21 KB

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