1*cd64efc2Sriastradh /* $NetBSD: ttm_agp_backend.c,v 1.9 2021/12/19 01:50: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*cd64efc2Sriastradh __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.9 2021/12/19 01:50:54 riastradh Exp $");
34b81c64e1Sriastradh
35b81c64e1Sriastradh #include <sys/types.h>
36b81c64e1Sriastradh #include <sys/kmem.h>
37b81c64e1Sriastradh
38b81c64e1Sriastradh #include <dev/pci/pcireg.h>
39b81c64e1Sriastradh #include <dev/pci/pcivar.h>
40b81c64e1Sriastradh #include <dev/pci/agpvar.h>
41b81c64e1Sriastradh
42*cd64efc2Sriastradh #include <drm/drm_agpsupport.h>
43*cd64efc2Sriastradh
44b81c64e1Sriastradh #include <ttm/ttm_bo_driver.h>
45b81c64e1Sriastradh #include <ttm/ttm_page_alloc.h>
46*cd64efc2Sriastradh #include <ttm/ttm_tt.h>
472cd973ceSjmcneill
48b81c64e1Sriastradh struct ttm_agp {
49b81c64e1Sriastradh struct ttm_dma_tt ttm_dma;
50b81c64e1Sriastradh struct agp_softc *agp;
51b81c64e1Sriastradh unsigned long agp_pgno;
52b81c64e1Sriastradh bool bound;
53b81c64e1Sriastradh };
54b81c64e1Sriastradh
55b81c64e1Sriastradh static const struct ttm_backend_func ttm_agp_backend_func;
56b81c64e1Sriastradh
57b81c64e1Sriastradh struct ttm_tt *
ttm_agp_tt_create(struct ttm_buffer_object * bo,struct agp_bridge_data * bridge,uint32_t page_flags)58*cd64efc2Sriastradh ttm_agp_tt_create(struct ttm_buffer_object *bo, struct agp_bridge_data *bridge,
59*cd64efc2Sriastradh uint32_t page_flags)
60b81c64e1Sriastradh {
61b81c64e1Sriastradh struct ttm_agp *ttm_agp;
62b81c64e1Sriastradh
6376be0e79Sriastradh ttm_agp = kmem_zalloc(sizeof(*ttm_agp), KM_SLEEP);
64b81c64e1Sriastradh ttm_agp->agp = &bridge->abd_sc;
65b81c64e1Sriastradh ttm_agp->ttm_dma.ttm.func = &ttm_agp_backend_func;
66b81c64e1Sriastradh
67*cd64efc2Sriastradh if (ttm_dma_tt_init(&ttm_agp->ttm_dma, bo, page_flags) != 0)
68b81c64e1Sriastradh goto fail;
69b81c64e1Sriastradh
70b81c64e1Sriastradh /* Success! */
71b81c64e1Sriastradh return &ttm_agp->ttm_dma.ttm;
72b81c64e1Sriastradh
73b81c64e1Sriastradh fail: kmem_free(ttm_agp, sizeof(*ttm_agp));
74b81c64e1Sriastradh return NULL;
75b81c64e1Sriastradh }
76b81c64e1Sriastradh
77b81c64e1Sriastradh int
ttm_agp_tt_populate(struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)78*cd64efc2Sriastradh ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
79b81c64e1Sriastradh {
80b81c64e1Sriastradh
8106ee305eSmaya KASSERTMSG((ttm->state == tt_unpopulated),
8206ee305eSmaya "ttm_agp_tt_populate: ttm %p state is not tt_unpopulated: %d",
8306ee305eSmaya ttm, (int)ttm->state);
84b81c64e1Sriastradh return ttm_bus_dma_populate(container_of(ttm, struct ttm_dma_tt, ttm));
85b81c64e1Sriastradh }
86b81c64e1Sriastradh
87b81c64e1Sriastradh void
ttm_agp_tt_unpopulate(struct ttm_tt * ttm)88b81c64e1Sriastradh ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
89b81c64e1Sriastradh {
90b81c64e1Sriastradh
9106ee305eSmaya KASSERTMSG((ttm->state == tt_unbound),
9206ee305eSmaya "ttm_agp_tt_unpopulate: ttm %p state is not tt_unbound: %d",
9306ee305eSmaya ttm, (int)ttm->state);
94b81c64e1Sriastradh ttm_bus_dma_unpopulate(container_of(ttm, struct ttm_dma_tt, ttm));
95b81c64e1Sriastradh }
96b81c64e1Sriastradh
97b81c64e1Sriastradh static int
ttm_agp_bind(struct ttm_tt * ttm,struct ttm_mem_reg * bo_mem)98b81c64e1Sriastradh ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
99b81c64e1Sriastradh {
100b81c64e1Sriastradh struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
101b81c64e1Sriastradh ttm_dma.ttm);
102b81c64e1Sriastradh struct agp_softc *const sc = ttm_agp->agp;
103b81c64e1Sriastradh struct drm_mm_node *const node = bo_mem->mm_node;
104b81c64e1Sriastradh const unsigned long agp_pgno = node->start;
105b81c64e1Sriastradh unsigned i, j;
106b81c64e1Sriastradh int ret;
107b81c64e1Sriastradh
108b81c64e1Sriastradh KASSERT(!ttm_agp->bound);
109b81c64e1Sriastradh KASSERT(ttm_agp->ttm_dma.dma_address->dm_nsegs == ttm->num_pages);
110b81c64e1Sriastradh for (i = 0; i < ttm->num_pages; i++) {
111b81c64e1Sriastradh KASSERT(ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len ==
112b81c64e1Sriastradh AGP_PAGE_SIZE);
113b81c64e1Sriastradh /* XXX errno NetBSD->Linux */
114b81c64e1Sriastradh ret = -AGP_BIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT,
115b81c64e1Sriastradh ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len);
116b81c64e1Sriastradh if (ret)
117b81c64e1Sriastradh goto fail;
118b81c64e1Sriastradh }
119d46aeca2Sriastradh drm_agp_flush();
120b81c64e1Sriastradh AGP_FLUSH_TLB(sc);
121b81c64e1Sriastradh
122b81c64e1Sriastradh /* Success! */
123b81c64e1Sriastradh ttm_agp->agp_pgno = agp_pgno;
124b81c64e1Sriastradh ttm_agp->bound = true;
125b81c64e1Sriastradh return 0;
126b81c64e1Sriastradh
127b81c64e1Sriastradh fail: KASSERT(ret);
128b81c64e1Sriastradh for (j = 0; j < i; j++)
129b81c64e1Sriastradh (void)AGP_UNBIND_PAGE(sc, (agp_pgno + j) << AGP_PAGE_SHIFT);
130b81c64e1Sriastradh return ret;
131b81c64e1Sriastradh }
132b81c64e1Sriastradh
133b81c64e1Sriastradh static int
ttm_agp_unbind(struct ttm_tt * ttm)134b81c64e1Sriastradh ttm_agp_unbind(struct ttm_tt *ttm)
135b81c64e1Sriastradh {
136b81c64e1Sriastradh struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
137b81c64e1Sriastradh ttm_dma.ttm);
138b81c64e1Sriastradh struct agp_softc *const sc = ttm_agp->agp;
139b81c64e1Sriastradh const unsigned long agp_pgno = ttm_agp->agp_pgno;
140b81c64e1Sriastradh unsigned i;
141b81c64e1Sriastradh
142b81c64e1Sriastradh /* XXX Can this happen? */
143b81c64e1Sriastradh if (!ttm_agp->bound)
144b81c64e1Sriastradh return 0;
145b81c64e1Sriastradh
146b81c64e1Sriastradh for (i = 0; i < ttm->num_pages; i++)
147b81c64e1Sriastradh (void)AGP_UNBIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT);
148b81c64e1Sriastradh
149b81c64e1Sriastradh ttm_agp->agp_pgno = 0;
150b81c64e1Sriastradh ttm_agp->bound = false;
151b81c64e1Sriastradh return 0;
152b81c64e1Sriastradh }
153b81c64e1Sriastradh
154b81c64e1Sriastradh static void
ttm_agp_destroy(struct ttm_tt * ttm)155b81c64e1Sriastradh ttm_agp_destroy(struct ttm_tt *ttm)
156b81c64e1Sriastradh {
157b81c64e1Sriastradh struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
158b81c64e1Sriastradh ttm_dma.ttm);
159b81c64e1Sriastradh
160b81c64e1Sriastradh /* XXX Can this happen? */
161b81c64e1Sriastradh if (ttm_agp->bound)
162b81c64e1Sriastradh ttm_agp_unbind(ttm);
163b81c64e1Sriastradh ttm_tt_fini(ttm);
1648eea28f4Sriastradh kmem_free(ttm_agp, sizeof(*ttm_agp));
165b81c64e1Sriastradh }
166b81c64e1Sriastradh
167b81c64e1Sriastradh static const struct ttm_backend_func ttm_agp_backend_func = {
168b81c64e1Sriastradh .bind = &ttm_agp_bind,
169b81c64e1Sriastradh .unbind = &ttm_agp_unbind,
170b81c64e1Sriastradh .destroy = &ttm_agp_destroy,
171b81c64e1Sriastradh };
172