xref: /freebsd/sys/dev/agp/agp_amd64.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*-
2  * Copyright (c) 2004 Jung-uk Kim
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_bus.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <pci/agppriv.h>
45 #include <pci/agpreg.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_object.h>
49 #include <vm/pmap.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/rman.h>
53 
54 /* XXX */
55 extern void pci_cfgregwrite(int, int, int, int, uint32_t, int);
56 extern uint32_t pci_cfgregread(int, int, int, int, int);
57 
58 MALLOC_DECLARE(M_AGP);
59 
60 #define	AMD64_MAX_MCTRL		8
61 
62 struct agp_amd64_softc {
63 	struct agp_softc	agp;
64 	uint32_t	initial_aperture; /* aperture size at startup */
65 	struct agp_gatt	*gatt;
66 	int		mctrl[AMD64_MAX_MCTRL];
67 	int		n_mctrl;
68 };
69 
70 static const char*
71 agp_amd64_match(device_t dev)
72 {
73 	if (pci_get_class(dev) != PCIC_BRIDGE
74 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
75 		return NULL;
76 
77 	if (agp_find_caps(dev) == 0)
78 		return NULL;
79 
80 	switch (pci_get_devid(dev)) {
81 	case 0x74541022:
82 		return ("AMD 8151 AGP graphics tunnel");
83 	case 0x10221039:
84 		return ("SiS 755 host to AGP bridge");
85 	case 0x02041106:
86 		return ("VIA 8380 host to PCI bridge");
87 	case 0x31881106:
88 		return ("VIA 8385 host to PCI bridge");
89 	};
90 
91 	return NULL;
92 }
93 
94 static int
95 agp_amd64_probe(device_t dev)
96 {
97 	const char *desc;
98 
99 	if (resource_disabled("agp", device_get_unit(dev)))
100 		return ENXIO;
101 	if ((desc = agp_amd64_match(dev))) {
102 		device_verbose(dev);
103 		device_set_desc(dev, desc);
104 		return 0;
105 	}
106 
107 	return ENXIO;
108 }
109 
110 static int
111 agp_amd64_attach(device_t dev)
112 {
113 	struct agp_amd64_softc *sc = device_get_softc(dev);
114 	struct agp_gatt *gatt;
115 	int i, n, error;
116 
117 	for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++)
118 		if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) {
119 			sc->mctrl[n] = i;
120 			n++;
121 		}
122 
123 	if (n == 0)
124 		return ENXIO;
125 
126 	sc->n_mctrl = n;
127 
128 	if (bootverbose)
129 		printf("AMD64: %d Misc. Control unit(s) found.\n", sc->n_mctrl);
130 
131 	if ((error = agp_generic_attach(dev)))
132 		return error;
133 
134 	sc->initial_aperture = AGP_GET_APERTURE(dev);
135 
136 	for (;;) {
137 		gatt = agp_alloc_gatt(dev);
138 		if (gatt)
139 			break;
140 
141 		/*
142 		 * Probably contigmalloc failure. Try reducing the
143 		 * aperture so that the gatt size reduces.
144 		 */
145 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
146 			agp_generic_detach(dev);
147 			return ENOMEM;
148 		}
149 	}
150 	sc->gatt = gatt;
151 
152 	/* Install the gatt and enable aperture. */
153 	for (i = 0; i < sc->n_mctrl; i++) {
154 		pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_ATTBASE,
155 		    (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK,
156 		    4);
157 		pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
158 		    (pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) |
159 		    AGP_AMD64_APCTRL_GARTEN) &
160 		    ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO),
161 		    4);
162 	}
163 
164 	agp_flush_cache();
165 
166 	return 0;
167 }
168 
169 static int
170 agp_amd64_detach(device_t dev)
171 {
172 	struct agp_amd64_softc *sc = device_get_softc(dev);
173 	int i, error;
174 
175 	if ((error = agp_generic_detach(dev)))
176 		return error;
177 
178 	for (i = 0; i < sc->n_mctrl; i++)
179 		pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
180 		    pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) &
181 		    ~AGP_AMD64_APCTRL_GARTEN, 4);
182 
183 	AGP_SET_APERTURE(dev, sc->initial_aperture);
184 	agp_free_gatt(sc->gatt);
185 
186 	return 0;
187 }
188 
189 static uint32_t agp_amd64_table[] = {
190 	0x02000000,	/*   32 MB */
191 	0x04000000,	/*   64 MB */
192 	0x08000000,	/*  128 MB */
193 	0x10000000,	/*  256 MB */
194 	0x20000000,	/*  512 MB */
195 	0x40000000,	/* 1024 MB */
196 	0x80000000,	/* 2048 MB */
197 };
198 
199 #define AGP_AMD64_TABLE_SIZE \
200 	(sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0]))
201 
202 static uint32_t
203 agp_amd64_get_aperture(device_t dev)
204 {
205 	struct agp_amd64_softc *sc = device_get_softc(dev);
206 	uint32_t i;
207 
208 	i = (pci_cfgregread(0, sc->mctrl[0], 3, AGP_AMD64_APCTRL, 4) &
209 		AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
210 
211 	if (i >= AGP_AMD64_TABLE_SIZE)
212 		return 0;
213 
214 	return (agp_amd64_table[i]);
215 }
216 
217 static int
218 agp_amd64_set_aperture(device_t dev, uint32_t aperture)
219 {
220 	struct agp_amd64_softc *sc = device_get_softc(dev);
221 	uint32_t i;
222 	int j;
223 
224 	for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
225 		if (agp_amd64_table[i] == aperture)
226 			break;
227 	if (i == AGP_AMD64_TABLE_SIZE)
228 		return EINVAL;
229 
230 	for (j = 0; j < sc->n_mctrl; j++)
231 		pci_cfgregwrite(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL,
232 		    (pci_cfgregread(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 4) &
233 		    ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1), 4);
234 
235 	return 0;
236 }
237 
238 static int
239 agp_amd64_bind_page(device_t dev, int offset, vm_offset_t physical)
240 {
241 	struct agp_amd64_softc *sc = device_get_softc(dev);
242 
243 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
244 		return EINVAL;
245 
246 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
247 	return 0;
248 }
249 
250 static int
251 agp_amd64_unbind_page(device_t dev, int offset)
252 {
253 	struct agp_amd64_softc *sc = device_get_softc(dev);
254 
255 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
256 		return EINVAL;
257 
258 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
259 	return 0;
260 }
261 
262 static void
263 agp_amd64_flush_tlb(device_t dev)
264 {
265 	struct agp_amd64_softc *sc = device_get_softc(dev);
266 	int i;
267 
268 	for (i = 0; i < sc->n_mctrl; i++)
269 		pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL,
270 		    pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 4) |
271 		    AGP_AMD64_CACHECTRL_INVGART, 4);
272 }
273 
274 static device_method_t agp_amd64_methods[] = {
275 	/* Device interface */
276 	DEVMETHOD(device_probe,		agp_amd64_probe),
277 	DEVMETHOD(device_attach,	agp_amd64_attach),
278 	DEVMETHOD(device_detach,	agp_amd64_detach),
279 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
280 	DEVMETHOD(device_suspend,	bus_generic_suspend),
281 	DEVMETHOD(device_resume,	bus_generic_resume),
282 
283 	/* AGP interface */
284 	DEVMETHOD(agp_get_aperture,	agp_amd64_get_aperture),
285 	DEVMETHOD(agp_set_aperture,	agp_amd64_set_aperture),
286 	DEVMETHOD(agp_bind_page,	agp_amd64_bind_page),
287 	DEVMETHOD(agp_unbind_page,	agp_amd64_unbind_page),
288 	DEVMETHOD(agp_flush_tlb,	agp_amd64_flush_tlb),
289 	DEVMETHOD(agp_enable,		agp_generic_enable),
290 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
291 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
292 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
293 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
294 
295 	{ 0, 0 }
296 };
297 
298 static driver_t agp_amd64_driver = {
299 	"agp",
300 	agp_amd64_methods,
301 	sizeof(struct agp_amd64_softc),
302 };
303 
304 static devclass_t agp_devclass;
305 
306 DRIVER_MODULE(agp_amd64, pci, agp_amd64_driver, agp_devclass, 0, 0);
307 MODULE_DEPEND(agp_amd64, agp, 1, 1, 1);
308 MODULE_DEPEND(agp_amd64, pci, 1, 1, 1);
309