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