Home | History | Annotate | Line # | Download | only in ttm
ttm_bus_dma.c revision 1.1
      1 /*	$NetBSD: ttm_bus_dma.c,v 1.1 2014/07/16 20:59:58 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ttm_bus_dma.c,v 1.1 2014/07/16 20:59:58 riastradh Exp $");
     34 
     35 #include <sys/bus.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <drm/bus_dma_hacks.h>
     40 #include <ttm/ttm_bo_driver.h>
     41 #include <ttm/ttm_page_alloc.h>
     42 
     43 int
     44 ttm_bus_dma_populate(struct ttm_dma_tt *ttm_dma)
     45 {
     46 	int ret;
     47 
     48 	/* If it's already populated, nothing to do.  */
     49 	if (ttm_dma->ttm.state != tt_unpopulated)
     50 		return 0;
     51 
     52 	/* Wire the pages, allocating them if necessary.  */
     53 	ret = ttm_tt_swapin(&ttm_dma->ttm);
     54 	if (ret)
     55 		goto fail0;
     56 
     57 	/* Load the DMA map.  */
     58 	/* XXX errno NetBSD->Linux */
     59 	ret = -bus_dmamap_load_pglist(ttm_dma->ttm.bdev->dmat,
     60 	    ttm_dma->dma_address, &ttm_dma->ttm.pglist,
     61 	    (ttm_dma->ttm.num_pages << PAGE_SHIFT), BUS_DMA_NOWAIT);
     62 	if (ret)
     63 		goto fail1;
     64 
     65 	/* Success!  */
     66 	ttm_dma->ttm.state = tt_unbound;
     67 	return 0;
     68 
     69 fail2: __unused
     70 	bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
     71 fail1:	ttm_tt_swapout(&ttm_dma->ttm, NULL);
     72 fail0:	KASSERT(ret);
     73 	return ret;
     74 }
     75 
     76 void
     77 ttm_bus_dma_unpopulate(struct ttm_dma_tt *ttm_dma)
     78 {
     79 	struct uvm_object *const uobj = ttm_dma->ttm.swap_storage;
     80 	const size_t size = (ttm_dma->ttm.num_pages << PAGE_SHIFT);
     81 
     82 	/* Unload the DMA map.  */
     83 	bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
     84 
     85 	/* Unwire the pages.  */
     86 	ttm_tt_swapout(&ttm_dma->ttm, NULL);
     87 
     88 	/* We are using uvm_aobj, which had better have a pgo_put.  */
     89 	KASSERT(uobj->pgops->pgo_put);
     90 
     91 	/* Release the pages.  */
     92 	mutex_enter(uobj->vmobjlock);
     93 	(void)(*uobj->pgops->pgo_put)(uobj, 0, size, PGO_CLEANIT|PGO_FREE);
     94 	/* pgo_put unlocks uobj->vmobjlock.  */
     95 }
     96