1*7c65befaSriastradh /* $NetBSD: ttm_bus_dma.c,v 1.10 2021/12/19 11:32:54 riastradh Exp $ */
2b81c64e1Sriastradh
3b81c64e1Sriastradh /*-
4b81c64e1Sriastradh * Copyright (c) 2014 The NetBSD Foundation, Inc.
5b81c64e1Sriastradh * All rights reserved.
6b81c64e1Sriastradh *
7b81c64e1Sriastradh * This code is derived from software contributed to The NetBSD Foundation
8b81c64e1Sriastradh * by Taylor R. Campbell.
9b81c64e1Sriastradh *
10b81c64e1Sriastradh * Redistribution and use in source and binary forms, with or without
11b81c64e1Sriastradh * modification, are permitted provided that the following conditions
12b81c64e1Sriastradh * are met:
13b81c64e1Sriastradh * 1. Redistributions of source code must retain the above copyright
14b81c64e1Sriastradh * notice, this list of conditions and the following disclaimer.
15b81c64e1Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
16b81c64e1Sriastradh * notice, this list of conditions and the following disclaimer in the
17b81c64e1Sriastradh * documentation and/or other materials provided with the distribution.
18b81c64e1Sriastradh *
19b81c64e1Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b81c64e1Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b81c64e1Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b81c64e1Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b81c64e1Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b81c64e1Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b81c64e1Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b81c64e1Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b81c64e1Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b81c64e1Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b81c64e1Sriastradh * POSSIBILITY OF SUCH DAMAGE.
30b81c64e1Sriastradh */
31b81c64e1Sriastradh
32b81c64e1Sriastradh #include <sys/cdefs.h>
33*7c65befaSriastradh __KERNEL_RCSID(0, "$NetBSD: ttm_bus_dma.c,v 1.10 2021/12/19 11:32:54 riastradh Exp $");
34b81c64e1Sriastradh
35b81c64e1Sriastradh #include <sys/bus.h>
36b81c64e1Sriastradh
37b81c64e1Sriastradh #include <uvm/uvm_extern.h>
38b81c64e1Sriastradh
39b81c64e1Sriastradh #include <drm/bus_dma_hacks.h>
40b81c64e1Sriastradh #include <ttm/ttm_bo_driver.h>
41b81c64e1Sriastradh #include <ttm/ttm_page_alloc.h>
42b81c64e1Sriastradh
439d42f346Sriastradh /*
449d42f346Sriastradh * ttm_bus_dma_populate(ttm_dma)
459d42f346Sriastradh *
469d42f346Sriastradh * If ttm_dma is not already populated, wire its pages and load
479d42f346Sriastradh * its DMA map. The wiring and loading are stable as long as the
489d42f346Sriastradh * associated bo is reserved.
499d42f346Sriastradh *
5006ee305eSmaya * Transitions from tt_unpopulated to tt_unbound. Marks as wired,
5106ee305eSmaya * a.k.a. !swapped.
529d42f346Sriastradh */
53b81c64e1Sriastradh int
ttm_bus_dma_populate(struct ttm_dma_tt * ttm_dma)54b81c64e1Sriastradh ttm_bus_dma_populate(struct ttm_dma_tt *ttm_dma)
55b81c64e1Sriastradh {
56b81c64e1Sriastradh int ret;
57b81c64e1Sriastradh
58c36d1d9aSmaya KASSERT(ttm_dma->ttm.state == tt_unpopulated);
59b81c64e1Sriastradh
609d42f346Sriastradh /* If it's unpopulated, it can't be swapped. */
61c36d1d9aSmaya KASSERT(!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED));
629d42f346Sriastradh /* Pretend it is now, for the sake of ttm_tt_wire. */
639d42f346Sriastradh ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
649d42f346Sriastradh
659d42f346Sriastradh /* Wire the uvm pages and fill the ttm page array. */
669d42f346Sriastradh ret = ttm_tt_wire(&ttm_dma->ttm);
67b81c64e1Sriastradh if (ret)
68b81c64e1Sriastradh goto fail0;
69b81c64e1Sriastradh
70e45033a0Smaya /* Mark it populated but unbound. */
71e45033a0Smaya ttm_dma->ttm.state = tt_unbound;
72e45033a0Smaya
737feda174Schs /* Mark it wired. */
747feda174Schs ttm_dma->ttm.page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
757feda174Schs
76b81c64e1Sriastradh /* Load the DMA map. */
77b81c64e1Sriastradh /* XXX errno NetBSD->Linux */
78*7c65befaSriastradh ret = -bus_dmamap_load_pages(ttm_dma->ttm.bdev->dmat,
79*7c65befaSriastradh ttm_dma->dma_address, ttm_dma->ttm.pages,
80b81c64e1Sriastradh (ttm_dma->ttm.num_pages << PAGE_SHIFT), BUS_DMA_NOWAIT);
81b81c64e1Sriastradh if (ret)
82b81c64e1Sriastradh goto fail1;
83b81c64e1Sriastradh
849d42f346Sriastradh /* Success! */
85b81c64e1Sriastradh return 0;
86b81c64e1Sriastradh
87b81c64e1Sriastradh fail2: __unused
88b81c64e1Sriastradh bus_dmamap_unload(ttm_dma->ttm.bdev->dmat, ttm_dma->dma_address);
8995f78b5bSmaya fail1: KASSERT(ttm_dma->ttm.state == tt_unbound);
90023a9fa3Smaya ttm_tt_unwire(&ttm_dma->ttm);
9195f78b5bSmaya ttm_dma->ttm.state = tt_unpopulated;
92b81c64e1Sriastradh fail0: KASSERT(ret);
93b81c64e1Sriastradh return ret;
94b81c64e1Sriastradh }
95b81c64e1Sriastradh
969d42f346Sriastradh static void
ttm_bus_dma_put(struct ttm_dma_tt * ttm_dma,int flags)979d42f346Sriastradh ttm_bus_dma_put(struct ttm_dma_tt *ttm_dma, int flags)
98b81c64e1Sriastradh {
99b81c64e1Sriastradh struct uvm_object *const uobj = ttm_dma->ttm.swap_storage;
100b81c64e1Sriastradh const size_t size = (ttm_dma->ttm.num_pages << PAGE_SHIFT);
101b81c64e1Sriastradh
1029d42f346Sriastradh /*
1039d42f346Sriastradh * Can't be tt_bound -- still in use and needs to be removed
1049d42f346Sriastradh * from GPU page tables. Can't be tt_unpopulated -- if it
1059d42f346Sriastradh * were, why are you hnadling this? Hence tt_unbound.
1069d42f346Sriastradh */
1079d42f346Sriastradh KASSERTMSG((ttm_dma->ttm.state == tt_unbound),
1089d42f346Sriastradh "ttm_tt %p in invalid state for unpopulate/swapout: %d",
1099d42f346Sriastradh &ttm_dma->ttm, (int)ttm_dma->ttm.state);
110b81c64e1Sriastradh
1119d42f346Sriastradh /* If pages are wired and loaded, unload and unwire them. */
1129d42f346Sriastradh if (!ISSET(ttm_dma->ttm.page_flags, TTM_PAGE_FLAG_SWAPPED)) {
1139d42f346Sriastradh bus_dmamap_unload(ttm_dma->ttm.bdev->dmat,
1149d42f346Sriastradh ttm_dma->dma_address);
1159d42f346Sriastradh ttm_tt_unwire(&ttm_dma->ttm);
1169d42f346Sriastradh ttm_dma->ttm.page_flags |= TTM_PAGE_FLAG_SWAPPED;
1179d42f346Sriastradh }
118b81c64e1Sriastradh
119b81c64e1Sriastradh /* We are using uvm_aobj, which had better have a pgo_put. */
120b81c64e1Sriastradh KASSERT(uobj->pgops->pgo_put);
121b81c64e1Sriastradh
1229d42f346Sriastradh /* Release or deactivate the pages. */
123d2a0ebb6Sad rw_enter(uobj->vmobjlock, RW_WRITER);
1249d42f346Sriastradh (void)(*uobj->pgops->pgo_put)(uobj, 0, size, flags);
125b81c64e1Sriastradh /* pgo_put unlocks uobj->vmobjlock. */
1269d42f346Sriastradh
1279d42f346Sriastradh /* Mark it unpopulated. */
1289d42f346Sriastradh ttm_dma->ttm.state = tt_unpopulated;
1299d42f346Sriastradh }
1309d42f346Sriastradh
1319d42f346Sriastradh /*
1329d42f346Sriastradh * ttmm_bus_dma_unpopulate(ttm_dma)
1339d42f346Sriastradh *
1349d42f346Sriastradh * Unload any DMA map, unwire any pages, and release any pages
1359d42f346Sriastradh * associated with ttm_dma.
1369d42f346Sriastradh *
1379d42f346Sriastradh * Transitions from tt_unbound to tt_unpopulated. Marks as
1389d42f346Sriastradh * unwired, a.k.a. swapped.
1399d42f346Sriastradh */
1409d42f346Sriastradh void
ttm_bus_dma_unpopulate(struct ttm_dma_tt * ttm_dma)1419d42f346Sriastradh ttm_bus_dma_unpopulate(struct ttm_dma_tt *ttm_dma)
1429d42f346Sriastradh {
1439d42f346Sriastradh
1449d42f346Sriastradh ttm_bus_dma_put(ttm_dma, PGO_CLEANIT|PGO_FREE);
1459d42f346Sriastradh }
1469d42f346Sriastradh
1479d42f346Sriastradh /*
1489d42f346Sriastradh * ttm_bus_dma_swapout(ttm_dma)
1499d42f346Sriastradh *
1509d42f346Sriastradh * Unload any DMA map, unwire any pages, and deactivate any pages
1519d42f346Sriastradh * associated with ttm_dma so that they can be swapped out, but
1529d42f346Sriastradh * don't release them.
1539d42f346Sriastradh *
1549d42f346Sriastradh * Transitions from tt_unbound to tt_unpopulated. Marks as
1559d42f346Sriastradh * unwired, a.k.a. swapped.
1569d42f346Sriastradh */
1579d42f346Sriastradh void
ttm_bus_dma_swapout(struct ttm_dma_tt * ttm_dma)1589d42f346Sriastradh ttm_bus_dma_swapout(struct ttm_dma_tt *ttm_dma)
1599d42f346Sriastradh {
1609d42f346Sriastradh
1619d42f346Sriastradh ttm_bus_dma_put(ttm_dma, PGO_DEACTIVATE);
162b81c64e1Sriastradh }
163