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