ttm_agp_backend.c revision 1.5 1 1.5 jmcneill /* $NetBSD: ttm_agp_backend.c,v 1.5 2015/10/17 21:05:57 jmcneill 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.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.5 2015/10/17 21:05:57 jmcneill Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/kmem.h>
37 1.1 riastrad
38 1.1 riastrad #include <dev/pci/pcireg.h>
39 1.1 riastrad #include <dev/pci/pcivar.h>
40 1.1 riastrad #include <dev/pci/agpvar.h>
41 1.1 riastrad
42 1.1 riastrad #include <ttm/ttm_bo_driver.h>
43 1.1 riastrad #include <ttm/ttm_page_alloc.h>
44 1.1 riastrad
45 1.5 jmcneill #if __OS_HAS_AGP
46 1.5 jmcneill
47 1.1 riastrad struct ttm_agp {
48 1.1 riastrad struct ttm_dma_tt ttm_dma;
49 1.1 riastrad struct agp_softc *agp;
50 1.1 riastrad unsigned long agp_pgno;
51 1.1 riastrad bool bound;
52 1.1 riastrad };
53 1.1 riastrad
54 1.1 riastrad static const struct ttm_backend_func ttm_agp_backend_func;
55 1.1 riastrad
56 1.1 riastrad struct ttm_tt *
57 1.1 riastrad ttm_agp_tt_create(struct ttm_bo_device *bdev, struct agp_bridge_data *bridge,
58 1.1 riastrad unsigned long size, uint32_t page_flags, struct page *dummy_read_page)
59 1.1 riastrad {
60 1.1 riastrad struct ttm_agp *ttm_agp;
61 1.1 riastrad
62 1.3 riastrad ttm_agp = kmem_zalloc(sizeof(*ttm_agp), KM_SLEEP);
63 1.1 riastrad ttm_agp->agp = &bridge->abd_sc;
64 1.1 riastrad ttm_agp->ttm_dma.ttm.func = &ttm_agp_backend_func;
65 1.1 riastrad
66 1.2 riastrad if (ttm_dma_tt_init(&ttm_agp->ttm_dma, bdev, size, page_flags,
67 1.1 riastrad dummy_read_page) != 0)
68 1.1 riastrad goto fail;
69 1.1 riastrad
70 1.1 riastrad /* Success! */
71 1.1 riastrad return &ttm_agp->ttm_dma.ttm;
72 1.1 riastrad
73 1.1 riastrad fail: kmem_free(ttm_agp, sizeof(*ttm_agp));
74 1.1 riastrad return NULL;
75 1.1 riastrad }
76 1.1 riastrad
77 1.1 riastrad int
78 1.1 riastrad ttm_agp_tt_populate(struct ttm_tt *ttm)
79 1.1 riastrad {
80 1.1 riastrad
81 1.1 riastrad if (ttm->state != tt_unpopulated)
82 1.1 riastrad return 0;
83 1.1 riastrad
84 1.1 riastrad return ttm_bus_dma_populate(container_of(ttm, struct ttm_dma_tt, ttm));
85 1.1 riastrad }
86 1.1 riastrad
87 1.1 riastrad void
88 1.1 riastrad ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
89 1.1 riastrad {
90 1.1 riastrad
91 1.1 riastrad ttm_bus_dma_unpopulate(container_of(ttm, struct ttm_dma_tt, ttm));
92 1.1 riastrad }
93 1.1 riastrad
94 1.1 riastrad static int
95 1.1 riastrad ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
96 1.1 riastrad {
97 1.1 riastrad struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
98 1.1 riastrad ttm_dma.ttm);
99 1.1 riastrad struct agp_softc *const sc = ttm_agp->agp;
100 1.1 riastrad struct drm_mm_node *const node = bo_mem->mm_node;
101 1.1 riastrad const unsigned long agp_pgno = node->start;
102 1.1 riastrad unsigned i, j;
103 1.1 riastrad int ret;
104 1.1 riastrad
105 1.1 riastrad KASSERT(!ttm_agp->bound);
106 1.1 riastrad KASSERT(ttm_agp->ttm_dma.dma_address->dm_nsegs == ttm->num_pages);
107 1.1 riastrad for (i = 0; i < ttm->num_pages; i++) {
108 1.1 riastrad KASSERT(ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len ==
109 1.1 riastrad AGP_PAGE_SIZE);
110 1.1 riastrad /* XXX errno NetBSD->Linux */
111 1.1 riastrad ret = -AGP_BIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT,
112 1.1 riastrad ttm_agp->ttm_dma.dma_address->dm_segs[i].ds_len);
113 1.1 riastrad if (ret)
114 1.1 riastrad goto fail;
115 1.1 riastrad }
116 1.1 riastrad agp_flush_cache();
117 1.1 riastrad AGP_FLUSH_TLB(sc);
118 1.1 riastrad
119 1.1 riastrad /* Success! */
120 1.1 riastrad ttm_agp->agp_pgno = agp_pgno;
121 1.1 riastrad ttm_agp->bound = true;
122 1.1 riastrad return 0;
123 1.1 riastrad
124 1.1 riastrad fail: KASSERT(ret);
125 1.1 riastrad for (j = 0; j < i; j++)
126 1.1 riastrad (void)AGP_UNBIND_PAGE(sc, (agp_pgno + j) << AGP_PAGE_SHIFT);
127 1.1 riastrad return ret;
128 1.1 riastrad }
129 1.1 riastrad
130 1.1 riastrad static int
131 1.1 riastrad ttm_agp_unbind(struct ttm_tt *ttm)
132 1.1 riastrad {
133 1.1 riastrad struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
134 1.1 riastrad ttm_dma.ttm);
135 1.1 riastrad struct agp_softc *const sc = ttm_agp->agp;
136 1.1 riastrad const unsigned long agp_pgno = ttm_agp->agp_pgno;
137 1.1 riastrad unsigned i;
138 1.1 riastrad
139 1.1 riastrad /* XXX Can this happen? */
140 1.1 riastrad if (!ttm_agp->bound)
141 1.1 riastrad return 0;
142 1.1 riastrad
143 1.1 riastrad for (i = 0; i < ttm->num_pages; i++)
144 1.1 riastrad (void)AGP_UNBIND_PAGE(sc, (agp_pgno + i) << AGP_PAGE_SHIFT);
145 1.1 riastrad
146 1.1 riastrad ttm_agp->agp_pgno = 0;
147 1.1 riastrad ttm_agp->bound = false;
148 1.1 riastrad return 0;
149 1.1 riastrad }
150 1.1 riastrad
151 1.1 riastrad static void
152 1.1 riastrad ttm_agp_destroy(struct ttm_tt *ttm)
153 1.1 riastrad {
154 1.1 riastrad struct ttm_agp *const ttm_agp = container_of(ttm, struct ttm_agp,
155 1.1 riastrad ttm_dma.ttm);
156 1.1 riastrad
157 1.1 riastrad /* XXX Can this happen? */
158 1.1 riastrad if (ttm_agp->bound)
159 1.1 riastrad ttm_agp_unbind(ttm);
160 1.1 riastrad ttm_tt_fini(ttm);
161 1.4 riastrad kmem_free(ttm_agp, sizeof(*ttm_agp));
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad static const struct ttm_backend_func ttm_agp_backend_func = {
165 1.1 riastrad .bind = &ttm_agp_bind,
166 1.1 riastrad .unbind = &ttm_agp_unbind,
167 1.1 riastrad .destroy = &ttm_agp_destroy,
168 1.1 riastrad };
169 1.5 jmcneill
170 1.5 jmcneill #endif
171