ttm_bus_dma.c revision 1.1 1 1.1 riastrad /* $NetBSD: ttm_bus_dma.c,v 1.1 2014/07/16 20:59:58 riastradh 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.1 riastrad __KERNEL_RCSID(0, "$NetBSD: ttm_bus_dma.c,v 1.1 2014/07/16 20:59:58 riastradh 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.1 riastrad int
44 1.1 riastrad ttm_bus_dma_populate(struct ttm_dma_tt *ttm_dma)
45 1.1 riastrad {
46 1.1 riastrad int ret;
47 1.1 riastrad
48 1.1 riastrad /* If it's already populated, nothing to do. */
49 1.1 riastrad if (ttm_dma->ttm.state != tt_unpopulated)
50 1.1 riastrad return 0;
51 1.1 riastrad
52 1.1 riastrad /* Wire the pages, allocating them if necessary. */
53 1.1 riastrad ret = ttm_tt_swapin(&ttm_dma->ttm);
54 1.1 riastrad if (ret)
55 1.1 riastrad goto fail0;
56 1.1 riastrad
57 1.1 riastrad /* Load the DMA map. */
58 1.1 riastrad /* XXX errno NetBSD->Linux */
59 1.1 riastrad ret = -bus_dmamap_load_pglist(ttm_dma->ttm.bdev->dmat,
60 1.1 riastrad ttm_dma->dma_address, &ttm_dma->ttm.pglist,
61 1.1 riastrad (ttm_dma->ttm.num_pages << PAGE_SHIFT), BUS_DMA_NOWAIT);
62 1.1 riastrad if (ret)
63 1.1 riastrad goto fail1;
64 1.1 riastrad
65 1.1 riastrad /* Success! */
66 1.1 riastrad ttm_dma->ttm.state = tt_unbound;
67 1.1 riastrad return 0;
68 1.1 riastrad
69 1.1 riastrad fail2: __unused
70 1.1 riastrad bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
71 1.1 riastrad fail1: ttm_tt_swapout(&ttm_dma->ttm, NULL);
72 1.1 riastrad fail0: KASSERT(ret);
73 1.1 riastrad return ret;
74 1.1 riastrad }
75 1.1 riastrad
76 1.1 riastrad void
77 1.1 riastrad ttm_bus_dma_unpopulate(struct ttm_dma_tt *ttm_dma)
78 1.1 riastrad {
79 1.1 riastrad struct uvm_object *const uobj = ttm_dma->ttm.swap_storage;
80 1.1 riastrad const size_t size = (ttm_dma->ttm.num_pages << PAGE_SHIFT);
81 1.1 riastrad
82 1.1 riastrad /* Unload the DMA map. */
83 1.1 riastrad bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
84 1.1 riastrad
85 1.1 riastrad /* Unwire the pages. */
86 1.1 riastrad ttm_tt_swapout(&ttm_dma->ttm, NULL);
87 1.1 riastrad
88 1.1 riastrad /* We are using uvm_aobj, which had better have a pgo_put. */
89 1.1 riastrad KASSERT(uobj->pgops->pgo_put);
90 1.1 riastrad
91 1.1 riastrad /* Release the pages. */
92 1.1 riastrad mutex_enter(uobj->vmobjlock);
93 1.1 riastrad (void)(*uobj->pgops->pgo_put)(uobj, 0, size, PGO_CLEANIT|PGO_FREE);
94 1.1 riastrad /* pgo_put unlocks uobj->vmobjlock. */
95 1.1 riastrad }
96