ttm_bus_dma.c revision 1.4 1 /* $NetBSD: ttm_bus_dma.c,v 1.4 2017/03/09 07:50:50 maya 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.4 2017/03/09 07:50:50 maya 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 /*
44 * ttm_bus_dma_populate(ttm_dma)
45 *
46 * If ttm_dma is not already populated, wire its pages and load
47 * its DMA map. The wiring and loading are stable as long as the
48 * associated bo is reserved.
49 *
50 * Transitions from tt_unpopulated or tt_unbound to tt_unbound.
51 * Marks as wired, a.k.a. !swapped.
52 */
53 int
54 ttm_bus_dma_populate(struct ttm_dma_tt *ttm_dma)
55 {
56 int ret;
57
58 KASSERT(ttm_dma->ttm.state == tt_unpopulated);
59
60 /* If it's unpopulated, it can't be swapped. */
61 KASSERT(!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED));
62 /* Pretend it is now, for the sake of ttm_tt_wire. */
63 ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
64
65 /* Wire the uvm pages and fill the ttm page array. */
66 ret = ttm_tt_wire(&ttm_dma->ttm);
67 if (ret)
68 goto fail0;
69
70 /* Mark it populated but unbound. */
71 ttm_dma->ttm.state = tt_unbound;
72
73 /* Load the DMA map. */
74 /* XXX errno NetBSD->Linux */
75 ret = -bus_dmamap_load_pglist(ttm_dma->ttm.bdev->dmat,
76 ttm_dma->dma_address, &ttm_dma->ttm.pglist,
77 (ttm_dma->ttm.num_pages << PAGE_SHIFT), BUS_DMA_NOWAIT);
78 if (ret)
79 goto fail1;
80
81 /* Mark it wired. */
82 ttm_dma->ttm.page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
83
84 /* Success! */
85 return 0;
86
87 fail2: __unused
88 bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
89 fail1: ttm_tt_unwire(&ttm_dma->ttm);
90 fail0: KASSERT(ret);
91 return ret;
92 }
93
94 static void
95 ttm_bus_dma_put(struct ttm_dma_tt *ttm_dma, int flags)
96 {
97 struct uvm_object *const uobj = ttm_dma->ttm.swap_storage;
98 const size_t size = (ttm_dma->ttm.num_pages << PAGE_SHIFT);
99
100 /*
101 * Can't be tt_bound -- still in use and needs to be removed
102 * from GPU page tables. Can't be tt_unpopulated -- if it
103 * were, why are you hnadling this? Hence tt_unbound.
104 */
105 KASSERTMSG((ttm_dma->ttm.state == tt_unbound),
106 "ttm_tt %p in invalid state for unpopulate/swapout: %d",
107 &ttm_dma->ttm, (int)ttm_dma->ttm.state);
108
109 /* If pages are wired and loaded, unload and unwire them. */
110 if (!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED)) {
111 bus_dmamap_unload(ttm_dma->ttm.bdev->dmat,
112 ttm_dma->dma_address);
113 ttm_tt_unwire(&ttm_dma->ttm);
114 ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
115 }
116
117 /* We are using uvm_aobj, which had better have a pgo_put. */
118 KASSERT(uobj->pgops->pgo_put);
119
120 /* Release or deactivate the pages. */
121 mutex_enter(uobj->vmobjlock);
122 (void)(*uobj->pgops->pgo_put)(uobj, 0, size, flags);
123 /* pgo_put unlocks uobj->vmobjlock. */
124
125 /* Mark it unpopulated. */
126 ttm_dma->ttm.state = tt_unpopulated;
127 }
128
129 /*
130 * ttmm_bus_dma_unpopulate(ttm_dma)
131 *
132 * Unload any DMA map, unwire any pages, and release any pages
133 * associated with ttm_dma.
134 *
135 * Transitions from tt_unbound to tt_unpopulated. Marks as
136 * unwired, a.k.a. swapped.
137 */
138 void
139 ttm_bus_dma_unpopulate(struct ttm_dma_tt *ttm_dma)
140 {
141
142 ttm_bus_dma_put(ttm_dma, PGO_CLEANIT|PGO_FREE);
143 }
144
145 /*
146 * ttm_bus_dma_swapout(ttm_dma)
147 *
148 * Unload any DMA map, unwire any pages, and deactivate any pages
149 * associated with ttm_dma so that they can be swapped out, but
150 * don't release them.
151 *
152 * Transitions from tt_unbound to tt_unpopulated. Marks as
153 * unwired, a.k.a. swapped.
154 */
155 void
156 ttm_bus_dma_swapout(struct ttm_dma_tt *ttm_dma)
157 {
158
159 ttm_bus_dma_put(ttm_dma, PGO_DEACTIVATE);
160 }
161