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