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