Home | History | Annotate | Line # | Download | only in ttm
ttm_bus_dma.c revision 1.6
      1  1.6      maya /*	$NetBSD: ttm_bus_dma.c,v 1.6 2017/03/09 08:05:21 maya Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.6      maya __KERNEL_RCSID(0, "$NetBSD: ttm_bus_dma.c,v 1.6 2017/03/09 08:05:21 maya Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/bus.h>
     36  1.1  riastrad 
     37  1.1  riastrad #include <uvm/uvm_extern.h>
     38  1.1  riastrad 
     39  1.1  riastrad #include <drm/bus_dma_hacks.h>
     40  1.1  riastrad #include <ttm/ttm_bo_driver.h>
     41  1.1  riastrad #include <ttm/ttm_page_alloc.h>
     42  1.1  riastrad 
     43  1.2  riastrad /*
     44  1.2  riastrad  * ttm_bus_dma_populate(ttm_dma)
     45  1.2  riastrad  *
     46  1.2  riastrad  *	If ttm_dma is not already populated, wire its pages and load
     47  1.2  riastrad  *	its DMA map.  The wiring and loading are stable as long as the
     48  1.2  riastrad  *	associated bo is reserved.
     49  1.2  riastrad  *
     50  1.2  riastrad  *	Transitions from tt_unpopulated or tt_unbound to tt_unbound.
     51  1.2  riastrad  *	Marks as wired, a.k.a. !swapped.
     52  1.2  riastrad  */
     53  1.1  riastrad int
     54  1.1  riastrad ttm_bus_dma_populate(struct ttm_dma_tt *ttm_dma)
     55  1.1  riastrad {
     56  1.1  riastrad 	int ret;
     57  1.1  riastrad 
     58  1.4      maya 	KASSERT(ttm_dma->ttm.state == tt_unpopulated);
     59  1.2  riastrad 
     60  1.4      maya 	/* If it's unpopulated, it can't be swapped.  */
     61  1.4      maya 	KASSERT(!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED));
     62  1.4      maya 	/* Pretend it is now, for the sake of ttm_tt_wire.  */
     63  1.4      maya 	ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
     64  1.1  riastrad 
     65  1.2  riastrad 	/* Wire the uvm pages and fill the ttm page array.  */
     66  1.2  riastrad 	ret = ttm_tt_wire(&ttm_dma->ttm);
     67  1.1  riastrad 	if (ret)
     68  1.1  riastrad 		goto fail0;
     69  1.1  riastrad 
     70  1.3      maya 	/* Mark it populated but unbound.  */
     71  1.3      maya 	ttm_dma->ttm.state = tt_unbound;
     72  1.3      maya 
     73  1.1  riastrad 	/* Load the DMA map.  */
     74  1.1  riastrad 	/* XXX errno NetBSD->Linux */
     75  1.1  riastrad 	ret = -bus_dmamap_load_pglist(ttm_dma->ttm.bdev->dmat,
     76  1.1  riastrad 	    ttm_dma->dma_address, &ttm_dma->ttm.pglist,
     77  1.1  riastrad 	    (ttm_dma->ttm.num_pages << PAGE_SHIFT), BUS_DMA_NOWAIT);
     78  1.1  riastrad 	if (ret)
     79  1.1  riastrad 		goto fail1;
     80  1.1  riastrad 
     81  1.2  riastrad 	/* Mark it wired.  */
     82  1.2  riastrad 	ttm_dma->ttm.page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
     83  1.2  riastrad 
     84  1.1  riastrad 	/* Success!  */
     85  1.1  riastrad 	return 0;
     86  1.1  riastrad 
     87  1.1  riastrad fail2: __unused
     88  1.1  riastrad 	bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
     89  1.6      maya fail1:	KASSERT(ttm_dma->ttm.state == tt_unbound);
     90  1.5      maya 	ttm_tt_unwire(&ttm_dma->ttm);
     91  1.6      maya 	ttm_dma->ttm.state = tt_unpopulated;
     92  1.1  riastrad fail0:	KASSERT(ret);
     93  1.1  riastrad 	return ret;
     94  1.1  riastrad }
     95  1.1  riastrad 
     96  1.2  riastrad static void
     97  1.2  riastrad ttm_bus_dma_put(struct ttm_dma_tt *ttm_dma, int flags)
     98  1.1  riastrad {
     99  1.1  riastrad 	struct uvm_object *const uobj = ttm_dma->ttm.swap_storage;
    100  1.1  riastrad 	const size_t size = (ttm_dma->ttm.num_pages << PAGE_SHIFT);
    101  1.1  riastrad 
    102  1.2  riastrad 	/*
    103  1.2  riastrad 	 * Can't be tt_bound -- still in use and needs to be removed
    104  1.2  riastrad 	 * from GPU page tables.  Can't be tt_unpopulated -- if it
    105  1.2  riastrad 	 * were, why are you hnadling this?  Hence tt_unbound.
    106  1.2  riastrad 	 */
    107  1.2  riastrad 	KASSERTMSG((ttm_dma->ttm.state == tt_unbound),
    108  1.2  riastrad 	    "ttm_tt %p in invalid state for unpopulate/swapout: %d",
    109  1.2  riastrad 	    &ttm_dma->ttm, (int)ttm_dma->ttm.state);
    110  1.2  riastrad 
    111  1.2  riastrad 	/* If pages are wired and loaded, unload and unwire them.  */
    112  1.2  riastrad 	if (!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED)) {
    113  1.2  riastrad 		bus_dmamap_unload(ttm_dma->ttm.bdev->dmat,
    114  1.2  riastrad 		    ttm_dma->dma_address);
    115  1.2  riastrad 		ttm_tt_unwire(&ttm_dma->ttm);
    116  1.2  riastrad 		ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
    117  1.2  riastrad 	}
    118  1.1  riastrad 
    119  1.1  riastrad 	/* We are using uvm_aobj, which had better have a pgo_put.  */
    120  1.1  riastrad 	KASSERT(uobj->pgops->pgo_put);
    121  1.1  riastrad 
    122  1.2  riastrad 	/* Release or deactivate the pages.  */
    123  1.1  riastrad 	mutex_enter(uobj->vmobjlock);
    124  1.2  riastrad 	(void)(*uobj->pgops->pgo_put)(uobj, 0, size, flags);
    125  1.1  riastrad 	/* pgo_put unlocks uobj->vmobjlock.  */
    126  1.2  riastrad 
    127  1.2  riastrad 	/* Mark it unpopulated.  */
    128  1.2  riastrad 	ttm_dma->ttm.state = tt_unpopulated;
    129  1.2  riastrad }
    130  1.2  riastrad 
    131  1.2  riastrad /*
    132  1.2  riastrad  * ttmm_bus_dma_unpopulate(ttm_dma)
    133  1.2  riastrad  *
    134  1.2  riastrad  *	Unload any DMA map, unwire any pages, and release any pages
    135  1.2  riastrad  *	associated with ttm_dma.
    136  1.2  riastrad  *
    137  1.2  riastrad  *	Transitions from tt_unbound to tt_unpopulated.  Marks as
    138  1.2  riastrad  *	unwired, a.k.a. swapped.
    139  1.2  riastrad  */
    140  1.2  riastrad void
    141  1.2  riastrad ttm_bus_dma_unpopulate(struct ttm_dma_tt *ttm_dma)
    142  1.2  riastrad {
    143  1.2  riastrad 
    144  1.2  riastrad 	ttm_bus_dma_put(ttm_dma, PGO_CLEANIT|PGO_FREE);
    145  1.2  riastrad }
    146  1.2  riastrad 
    147  1.2  riastrad /*
    148  1.2  riastrad  * ttm_bus_dma_swapout(ttm_dma)
    149  1.2  riastrad  *
    150  1.2  riastrad  *	Unload any DMA map, unwire any pages, and deactivate any pages
    151  1.2  riastrad  *	associated with ttm_dma so that they can be swapped out, but
    152  1.2  riastrad  *	don't release them.
    153  1.2  riastrad  *
    154  1.2  riastrad  *	Transitions from tt_unbound to tt_unpopulated.  Marks as
    155  1.2  riastrad  *	unwired, a.k.a. swapped.
    156  1.2  riastrad  */
    157  1.2  riastrad void
    158  1.2  riastrad ttm_bus_dma_swapout(struct ttm_dma_tt *ttm_dma)
    159  1.2  riastrad {
    160  1.2  riastrad 
    161  1.2  riastrad 	ttm_bus_dma_put(ttm_dma, PGO_DEACTIVATE);
    162  1.1  riastrad }
    163