agp_amd.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: agp_amd.c,v 1.1.2.3 2002/01/10 19:56:25 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2000 Doug Rabson
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej *
28 1.1.2.2 thorpej * $FreeBSD: src/sys/pci/agp_amd.c,v 1.6 2001/07/05 21:28:46 jhb Exp $
29 1.1.2.2 thorpej */
30 1.1.2.2 thorpej
31 1.1.2.3 thorpej #include <sys/cdefs.h>
32 1.1.2.3 thorpej __KERNEL_RCSID(0, "$NetBSD: agp_amd.c,v 1.1.2.3 2002/01/10 19:56:25 thorpej Exp $");
33 1.1.2.3 thorpej
34 1.1.2.2 thorpej #include <sys/param.h>
35 1.1.2.2 thorpej #include <sys/systm.h>
36 1.1.2.2 thorpej #include <sys/malloc.h>
37 1.1.2.2 thorpej #include <sys/kernel.h>
38 1.1.2.2 thorpej #include <sys/lock.h>
39 1.1.2.2 thorpej #include <sys/proc.h>
40 1.1.2.2 thorpej #include <sys/conf.h>
41 1.1.2.2 thorpej #include <sys/device.h>
42 1.1.2.2 thorpej #include <sys/agpio.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej #include <uvm/uvm_extern.h>
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej #include <dev/pci/pcivar.h>
47 1.1.2.2 thorpej #include <dev/pci/pcireg.h>
48 1.1.2.2 thorpej #include <dev/pci/agpvar.h>
49 1.1.2.2 thorpej #include <dev/pci/agpreg.h>
50 1.1.2.2 thorpej
51 1.1.2.2 thorpej #include <dev/pci/pcidevs.h>
52 1.1.2.2 thorpej
53 1.1.2.2 thorpej #define READ2(off) bus_space_read_2(asc->iot, asc->ioh, off)
54 1.1.2.2 thorpej #define READ4(off) bus_space_read_4(asc->iot, asc->ioh, off)
55 1.1.2.2 thorpej #define WRITE2(off,v) bus_space_write_2(asc->iot, asc->ioh, off, v)
56 1.1.2.2 thorpej #define WRITE4(off,v) bus_space_write_4(asc->iot, asc->ioh, off, v)
57 1.1.2.2 thorpej
58 1.1.2.2 thorpej struct agp_amd_gatt {
59 1.1.2.2 thorpej bus_dmamap_t ag_dmamap;
60 1.1.2.2 thorpej bus_dma_segment_t ag_dmaseg;
61 1.1.2.2 thorpej int ag_nseg;
62 1.1.2.2 thorpej u_int32_t ag_entries;
63 1.1.2.2 thorpej u_int32_t *ag_vdir; /* virtual address of page dir */
64 1.1.2.2 thorpej bus_addr_t ag_pdir; /* bus address of page dir */
65 1.1.2.2 thorpej u_int32_t *ag_virtual; /* virtual address of gatt */
66 1.1.2.2 thorpej bus_addr_t ag_physical; /* bus address of gatt */
67 1.1.2.2 thorpej size_t ag_size;
68 1.1.2.2 thorpej };
69 1.1.2.2 thorpej
70 1.1.2.2 thorpej struct agp_amd_softc {
71 1.1.2.2 thorpej u_int32_t initial_aperture; /* aperture size at startup */
72 1.1.2.2 thorpej struct agp_amd_gatt *gatt;
73 1.1.2.2 thorpej bus_space_handle_t ioh;
74 1.1.2.2 thorpej bus_space_tag_t iot;
75 1.1.2.2 thorpej };
76 1.1.2.2 thorpej
77 1.1.2.2 thorpej static u_int32_t agp_amd_get_aperture(struct agp_softc *);
78 1.1.2.2 thorpej static int agp_amd_set_aperture(struct agp_softc *, u_int32_t);
79 1.1.2.2 thorpej static int agp_amd_bind_page(struct agp_softc *, off_t, bus_addr_t);
80 1.1.2.2 thorpej static int agp_amd_unbind_page(struct agp_softc *, off_t);
81 1.1.2.2 thorpej static void agp_amd_flush_tlb(struct agp_softc *);
82 1.1.2.2 thorpej
83 1.1.2.2 thorpej
84 1.1.2.2 thorpej struct agp_methods agp_amd_methods = {
85 1.1.2.2 thorpej agp_amd_get_aperture,
86 1.1.2.2 thorpej agp_amd_set_aperture,
87 1.1.2.2 thorpej agp_amd_bind_page,
88 1.1.2.2 thorpej agp_amd_unbind_page,
89 1.1.2.2 thorpej agp_amd_flush_tlb,
90 1.1.2.2 thorpej agp_generic_enable,
91 1.1.2.2 thorpej agp_generic_alloc_memory,
92 1.1.2.2 thorpej agp_generic_free_memory,
93 1.1.2.2 thorpej agp_generic_bind_memory,
94 1.1.2.2 thorpej agp_generic_unbind_memory,
95 1.1.2.2 thorpej };
96 1.1.2.2 thorpej
97 1.1.2.2 thorpej
98 1.1.2.2 thorpej static struct agp_amd_gatt *
99 1.1.2.2 thorpej agp_amd_alloc_gatt(struct agp_softc *sc)
100 1.1.2.2 thorpej {
101 1.1.2.2 thorpej u_int32_t apsize = AGP_GET_APERTURE(sc);
102 1.1.2.2 thorpej u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
103 1.1.2.2 thorpej struct agp_amd_gatt *gatt;
104 1.1.2.2 thorpej int i, npages;
105 1.1.2.3 thorpej caddr_t vdir;
106 1.1.2.2 thorpej
107 1.1.2.2 thorpej gatt = malloc(sizeof(struct agp_amd_gatt), M_AGP, M_NOWAIT);
108 1.1.2.2 thorpej if (!gatt)
109 1.1.2.2 thorpej return 0;
110 1.1.2.2 thorpej
111 1.1.2.2 thorpej if (agp_alloc_dmamem(sc->as_dmat,
112 1.1.2.2 thorpej AGP_PAGE_SIZE + entries * sizeof(u_int32_t), 0,
113 1.1.2.3 thorpej &gatt->ag_dmamap, &vdir, &gatt->ag_pdir,
114 1.1.2.2 thorpej &gatt->ag_dmaseg, 1, &gatt->ag_nseg) != 0) {
115 1.1.2.2 thorpej printf("failed to allocate GATT\n");
116 1.1.2.3 thorpej free(gatt, M_AGP);
117 1.1.2.2 thorpej return NULL;
118 1.1.2.2 thorpej }
119 1.1.2.2 thorpej
120 1.1.2.3 thorpej gatt->ag_vdir = (u_int32_t *)vdir;
121 1.1.2.2 thorpej gatt->ag_entries = entries;
122 1.1.2.3 thorpej gatt->ag_virtual = (u_int32_t *)(vdir + AGP_PAGE_SIZE);
123 1.1.2.2 thorpej gatt->ag_physical = gatt->ag_pdir + AGP_PAGE_SIZE;
124 1.1.2.2 thorpej gatt->ag_size = AGP_PAGE_SIZE + entries * sizeof(u_int32_t);
125 1.1.2.2 thorpej
126 1.1.2.2 thorpej memset(gatt->ag_vdir, 0, AGP_PAGE_SIZE);
127 1.1.2.2 thorpej memset(gatt->ag_virtual, 0, entries * sizeof(u_int32_t));
128 1.1.2.2 thorpej
129 1.1.2.2 thorpej /*
130 1.1.2.2 thorpej * Map the pages of the GATT into the page directory.
131 1.1.2.2 thorpej */
132 1.1.2.2 thorpej npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1)
133 1.1.2.2 thorpej >> AGP_PAGE_SHIFT);
134 1.1.2.2 thorpej
135 1.1.2.2 thorpej for (i = 0; i < npages; i++)
136 1.1.2.2 thorpej gatt->ag_vdir[i] = (gatt->ag_physical + i * AGP_PAGE_SIZE) | 1;
137 1.1.2.2 thorpej
138 1.1.2.2 thorpej /*
139 1.1.2.2 thorpej * Make sure the chipset can see everything.
140 1.1.2.2 thorpej */
141 1.1.2.2 thorpej agp_flush_cache();
142 1.1.2.2 thorpej
143 1.1.2.2 thorpej return gatt;
144 1.1.2.2 thorpej }
145 1.1.2.2 thorpej
146 1.1.2.2 thorpej #if 0
147 1.1.2.2 thorpej static void
148 1.1.2.2 thorpej agp_amd_free_gatt(struct agp_softc *sc, struct agp_amd_gatt *gatt)
149 1.1.2.2 thorpej {
150 1.1.2.2 thorpej agp_free_dmamem(sc->as_dmat, gatt->ag_size,
151 1.1.2.2 thorpej gatt->ag_dmamap, (caddr_t)gatt->ag_virtual, &gatt->ag_dmaseg,
152 1.1.2.2 thorpej gatt->ag_nseg);
153 1.1.2.2 thorpej free(gatt, M_AGP);
154 1.1.2.2 thorpej }
155 1.1.2.2 thorpej #endif
156 1.1.2.2 thorpej
157 1.1.2.2 thorpej int
158 1.1.2.3 thorpej agp_amd_match(const struct pci_attach_args *pa)
159 1.1.2.2 thorpej {
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej switch (PCI_PRODUCT(pa->pa_id)) {
162 1.1.2.2 thorpej case PCI_PRODUCT_AMD_SC751_SC:
163 1.1.2.3 thorpej case PCI_PRODUCT_AMD_SC762_NB:
164 1.1.2.2 thorpej return 1;
165 1.1.2.2 thorpej }
166 1.1.2.2 thorpej
167 1.1.2.2 thorpej return 0;
168 1.1.2.2 thorpej }
169 1.1.2.2 thorpej
170 1.1.2.2 thorpej int
171 1.1.2.2 thorpej agp_amd_attach(struct device *parent, struct device *self, void *aux)
172 1.1.2.2 thorpej {
173 1.1.2.2 thorpej struct agp_softc *sc = (void *)self;
174 1.1.2.2 thorpej struct agp_amd_softc *asc;
175 1.1.2.2 thorpej struct pci_attach_args *pa = aux;
176 1.1.2.2 thorpej struct agp_amd_gatt *gatt;
177 1.1.2.2 thorpej pcireg_t reg;
178 1.1.2.2 thorpej int error;
179 1.1.2.2 thorpej
180 1.1.2.2 thorpej asc = malloc(sizeof *asc, M_AGP, M_NOWAIT);
181 1.1.2.2 thorpej if (asc == NULL) {
182 1.1.2.2 thorpej printf(": can't allocate softc\n");
183 1.1.2.2 thorpej /* agp_generic_detach(sc) */
184 1.1.2.2 thorpej return ENOMEM;
185 1.1.2.2 thorpej }
186 1.1.2.2 thorpej memset(asc, 0, sizeof *asc);
187 1.1.2.2 thorpej
188 1.1.2.3 thorpej error = pci_mapreg_map(pa, AGP_AMD751_REGISTERS, PCI_MAPREG_TYPE_MEM, 0,
189 1.1.2.2 thorpej &asc->iot, &asc->ioh, NULL, NULL);
190 1.1.2.2 thorpej if (error != 0) {
191 1.1.2.3 thorpej printf(": can't map AGP registers\n");
192 1.1.2.2 thorpej agp_generic_detach(sc);
193 1.1.2.2 thorpej return error;
194 1.1.2.2 thorpej }
195 1.1.2.2 thorpej
196 1.1.2.2 thorpej if (agp_map_aperture(pa, sc) != 0) {
197 1.1.2.2 thorpej printf(": can't map aperture\n");
198 1.1.2.2 thorpej agp_generic_detach(sc);
199 1.1.2.2 thorpej free(asc, M_AGP);
200 1.1.2.2 thorpej return ENXIO;
201 1.1.2.2 thorpej }
202 1.1.2.2 thorpej pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
203 1.1.2.2 thorpej NULL);
204 1.1.2.2 thorpej sc->as_methods = &agp_amd_methods;
205 1.1.2.2 thorpej sc->as_chipc = asc;
206 1.1.2.2 thorpej asc->initial_aperture = AGP_GET_APERTURE(sc);
207 1.1.2.2 thorpej
208 1.1.2.2 thorpej for (;;) {
209 1.1.2.2 thorpej gatt = agp_amd_alloc_gatt(sc);
210 1.1.2.2 thorpej if (gatt)
211 1.1.2.2 thorpej break;
212 1.1.2.2 thorpej
213 1.1.2.2 thorpej /*
214 1.1.2.2 thorpej * Probably contigmalloc failure. Try reducing the
215 1.1.2.2 thorpej * aperture so that the gatt size reduces.
216 1.1.2.2 thorpej */
217 1.1.2.2 thorpej if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
218 1.1.2.2 thorpej printf(": can't set aperture\n");
219 1.1.2.2 thorpej return ENOMEM;
220 1.1.2.2 thorpej }
221 1.1.2.2 thorpej }
222 1.1.2.2 thorpej asc->gatt = gatt;
223 1.1.2.2 thorpej
224 1.1.2.2 thorpej /* Install the gatt. */
225 1.1.2.2 thorpej WRITE4(AGP_AMD751_ATTBASE, gatt->ag_physical);
226 1.1.2.2 thorpej
227 1.1.2.2 thorpej /* Enable synchronisation between host and agp. */
228 1.1.2.2 thorpej reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_AMD751_MODECTRL);
229 1.1.2.2 thorpej reg &= ~0x00ff00ff;
230 1.1.2.2 thorpej reg |= (AGP_AMD751_MODECTRL_SYNEN) | (AGP_AMD751_MODECTRL2_GPDCE << 16);
231 1.1.2.2 thorpej pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_AMD751_MODECTRL, reg);
232 1.1.2.2 thorpej /* Enable the TLB and flush */
233 1.1.2.2 thorpej WRITE2(AGP_AMD751_STATUS,
234 1.1.2.2 thorpej READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE);
235 1.1.2.2 thorpej AGP_FLUSH_TLB(sc);
236 1.1.2.2 thorpej
237 1.1.2.2 thorpej return 0;
238 1.1.2.2 thorpej }
239 1.1.2.2 thorpej
240 1.1.2.2 thorpej #if 0
241 1.1.2.2 thorpej static int
242 1.1.2.2 thorpej agp_amd_detach(struct agp_softc *sc)
243 1.1.2.2 thorpej {
244 1.1.2.2 thorpej pcireg_t reg;
245 1.1.2.2 thorpej struct agp_amd_softc *asc = sc->as_chipc;
246 1.1.2.2 thorpej
247 1.1.2.2 thorpej /* Disable the TLB.. */
248 1.1.2.2 thorpej WRITE2(AGP_AMD751_STATUS,
249 1.1.2.2 thorpej READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE);
250 1.1.2.2 thorpej
251 1.1.2.2 thorpej /* Disable host-agp sync */
252 1.1.2.2 thorpej reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_MODECTRL);
253 1.1.2.2 thorpej reg &= 0xffffff00;
254 1.1.2.2 thorpej pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD751_MODECTRL, reg);
255 1.1.2.2 thorpej
256 1.1.2.2 thorpej /* Clear the GATT base */
257 1.1.2.2 thorpej WRITE4(AGP_AMD751_ATTBASE, 0);
258 1.1.2.2 thorpej
259 1.1.2.2 thorpej /* Put the aperture back the way it started. */
260 1.1.2.2 thorpej AGP_SET_APERTURE(sc, asc->initial_aperture);
261 1.1.2.2 thorpej
262 1.1.2.2 thorpej agp_amd_free_gatt(sc, asc->gatt);
263 1.1.2.2 thorpej
264 1.1.2.2 thorpej /* XXXfvdl no pci_mapreg_unmap */
265 1.1.2.2 thorpej
266 1.1.2.2 thorpej return 0;
267 1.1.2.2 thorpej }
268 1.1.2.2 thorpej #endif
269 1.1.2.2 thorpej
270 1.1.2.2 thorpej static u_int32_t
271 1.1.2.2 thorpej agp_amd_get_aperture(struct agp_softc *sc)
272 1.1.2.2 thorpej {
273 1.1.2.2 thorpej int vas;
274 1.1.2.2 thorpej
275 1.1.2.2 thorpej vas = (pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL) & 0x06);
276 1.1.2.2 thorpej vas >>= 1;
277 1.1.2.2 thorpej /*
278 1.1.2.2 thorpej * The aperture size is equal to 32M<<vas.
279 1.1.2.2 thorpej */
280 1.1.2.2 thorpej return (32*1024*1024) << vas;
281 1.1.2.2 thorpej }
282 1.1.2.2 thorpej
283 1.1.2.2 thorpej static int
284 1.1.2.2 thorpej agp_amd_set_aperture(struct agp_softc *sc, u_int32_t aperture)
285 1.1.2.2 thorpej {
286 1.1.2.2 thorpej int vas;
287 1.1.2.2 thorpej pcireg_t reg;
288 1.1.2.2 thorpej
289 1.1.2.2 thorpej /*
290 1.1.2.2 thorpej * Check for a power of two and make sure its within the
291 1.1.2.2 thorpej * programmable range.
292 1.1.2.2 thorpej */
293 1.1.2.2 thorpej if (aperture & (aperture - 1)
294 1.1.2.2 thorpej || aperture < 32*1024*1024
295 1.1.2.2 thorpej || aperture > 2U*1024*1024*1024)
296 1.1.2.2 thorpej return EINVAL;
297 1.1.2.2 thorpej
298 1.1.2.2 thorpej vas = ffs(aperture / 32*1024*1024) - 1;
299 1.1.2.2 thorpej
300 1.1.2.2 thorpej reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL);
301 1.1.2.2 thorpej reg = (reg & ~0x06) | (vas << 1);
302 1.1.2.2 thorpej pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL, reg);
303 1.1.2.2 thorpej
304 1.1.2.2 thorpej return 0;
305 1.1.2.2 thorpej }
306 1.1.2.2 thorpej
307 1.1.2.2 thorpej static int
308 1.1.2.2 thorpej agp_amd_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
309 1.1.2.2 thorpej {
310 1.1.2.2 thorpej struct agp_amd_softc *asc = sc->as_chipc;
311 1.1.2.2 thorpej
312 1.1.2.2 thorpej if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
313 1.1.2.2 thorpej return EINVAL;
314 1.1.2.2 thorpej
315 1.1.2.2 thorpej asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
316 1.1.2.2 thorpej return 0;
317 1.1.2.2 thorpej }
318 1.1.2.2 thorpej
319 1.1.2.2 thorpej static int
320 1.1.2.2 thorpej agp_amd_unbind_page(struct agp_softc *sc, off_t offset)
321 1.1.2.2 thorpej {
322 1.1.2.2 thorpej struct agp_amd_softc *asc = sc->as_chipc;
323 1.1.2.2 thorpej
324 1.1.2.2 thorpej if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
325 1.1.2.2 thorpej return EINVAL;
326 1.1.2.2 thorpej
327 1.1.2.2 thorpej asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
328 1.1.2.2 thorpej return 0;
329 1.1.2.2 thorpej }
330 1.1.2.2 thorpej
331 1.1.2.2 thorpej static void
332 1.1.2.2 thorpej agp_amd_flush_tlb(struct agp_softc *sc)
333 1.1.2.2 thorpej {
334 1.1.2.2 thorpej struct agp_amd_softc *asc = sc->as_chipc;
335 1.1.2.2 thorpej
336 1.1.2.2 thorpej /* Set the cache invalidate bit and wait for the chipset to clear */
337 1.1.2.2 thorpej WRITE4(AGP_AMD751_TLBCTRL, 1);
338 1.1.2.2 thorpej do {
339 1.1.2.2 thorpej DELAY(1);
340 1.1.2.2 thorpej } while (READ4(AGP_AMD751_TLBCTRL));
341 1.1.2.2 thorpej }
342