xref: /dragonfly/sys/dev/agp/agp_via.c (revision 52f9f0d9)
1 /*-
2  * Copyright (c) 2000 Doug Rabson
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  *	$FreeBSD: src/sys/dev/agp/agp_via.c,v 1.26 2007/11/12 21:51:37 jhb Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/lock.h>
35 
36 #include <bus/pci/pcivar.h>
37 #include <bus/pci/pcireg.h>
38 #include "agppriv.h"
39 #include "agpreg.h"
40 
41 #include <vm/vm.h>
42 #include <vm/vm_object.h>
43 #include <vm/pmap.h>
44 
45 #define	REG_GARTCTRL	0
46 #define	REG_APSIZE	1
47 #define	REG_ATTBASE	2
48 
49 struct agp_via_softc {
50 	struct agp_softc agp;
51 	u_int32_t	initial_aperture; /* aperture size at startup */
52 	struct agp_gatt *gatt;
53 	int		*regs;
54 };
55 
56 static int via_v2_regs[] = { AGP_VIA_GARTCTRL, AGP_VIA_APSIZE,
57     AGP_VIA_ATTBASE };
58 static int via_v3_regs[] = { AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE,
59     AGP3_VIA_ATTBASE };
60 
61 static const char*
62 agp_via_match(device_t dev)
63 {
64 	if (pci_get_class(dev) != PCIC_BRIDGE
65 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
66 		return NULL;
67 
68 	if (agp_find_caps(dev) == 0)
69 		return NULL;
70 
71 	switch (pci_get_devid(dev)) {
72 	case 0x01981106:
73 		return ("VIA 8763 (P4X600) host to PCI bridge");
74 	case 0x02591106:
75 		return ("VIA PM800/PN800/PM880/PN880 host to PCI bridge");
76 	case 0x02691106:
77 		return ("VIA KT880 host to PCI bridge");
78 	case 0x02961106:
79 		return ("VIA 3296 (P4M800) host to PCI bridge");
80 	case 0x03051106:
81 		return ("VIA 82C8363 (Apollo KT133x/KM133) host to PCI bridge");
82 	case 0x03911106:
83 		return ("VIA 8371 (Apollo KX133) host to PCI bridge");
84 	case 0x05011106:
85 		return ("VIA 8501 (Apollo MVP4) host to PCI bridge");
86 	case 0x05971106:
87 		return ("VIA 82C597 (Apollo VP3) host to PCI bridge");
88 	case 0x05981106:
89 		return ("VIA 82C598 (Apollo MVP3) host to PCI bridge");
90 	case 0x06011106:
91 		return ("VIA 8601 (Apollo ProMedia/PLE133Ta) host to PCI bridge");
92 	case 0x06051106:
93 		return ("VIA 82C694X (Apollo Pro 133A) host to PCI bridge");
94 	case 0x06911106:
95 		return ("VIA 82C691 (Apollo Pro) host to PCI bridge");
96 	case 0x30911106:
97 		return ("VIA 8633 (Pro 266) host to PCI bridge");
98 	case 0x30991106:
99 		return ("VIA 8367 (KT266/KY266x/KT333) host to PCI bridge");
100 	case 0x31011106:
101 		return ("VIA 8653 (Pro266T) host to PCI bridge");
102 	case 0x31121106:
103 		return ("VIA 8361 (KLE133) host to PCI bridge");
104 	case 0x31161106:
105 		return ("VIA XM266 (PM266/KM266) host to PCI bridge");
106 	case 0x31231106:
107 		return ("VIA 862x (CLE266) host to PCI bridge");
108 	case 0x31281106:
109 		return ("VIA 8753 (P4X266) host to PCI bridge");
110 	case 0x31481106:
111 		return ("VIA 8703 (P4M266x/P4N266) host to PCI bridge");
112 	case 0x31561106:
113 		return ("VIA XN266 (Apollo Pro266) host to PCI bridge");
114 	case 0x31681106:
115 		return ("VIA 8754 (PT800) host to PCI bridge");
116 	case 0x31891106:
117 		return ("VIA 8377 (Apollo KT400/KT400A/KT600) host to PCI bridge");
118 	case 0x32051106:
119 		return ("VIA 8235/8237 (Apollo KM400/KM400A) host to PCI bridge");
120 	case 0x32081106:
121 		return ("VIA 8783 (PT890) host to PCI bridge");
122 	case 0x32581106:
123 		return ("VIA PT880 host to PCI bridge");
124 	case 0xb1981106:
125 		return ("VIA VT83xx/VT87xx/KTxxx/Px8xx host to PCI bridge");
126 	};
127 
128 	return NULL;
129 }
130 
131 static int
132 agp_via_probe(device_t dev)
133 {
134 	const char *desc;
135 
136 	if (resource_disabled("agp", device_get_unit(dev)))
137 		return (ENXIO);
138 	desc = agp_via_match(dev);
139 	if (desc) {
140 		device_verbose(dev);
141 		device_set_desc(dev, desc);
142 		return BUS_PROBE_DEFAULT;
143 	}
144 
145 	return ENXIO;
146 }
147 
148 static int
149 agp_via_attach(device_t dev)
150 {
151 	struct agp_via_softc *sc = device_get_softc(dev);
152 	struct agp_gatt *gatt;
153 	int error;
154 	u_int32_t agpsel;
155 
156 	/* XXX: This should be keying off of whether the bridge is AGP3 capable,
157 	 * rather than a bunch of device ids for chipsets that happen to do 8x.
158 	 */
159 	switch (pci_get_devid(dev)) {
160 	case 0x01981106:
161 	case 0x02591106:
162 	case 0x02691106:
163 	case 0x02961106:
164 	case 0x31231106:
165 	case 0x31681106:
166 	case 0x31891106:
167 	case 0x32051106:
168 	case 0x32581106:
169 	case 0xb1981106:
170 		/* The newer VIA chipsets will select the AGP version based on
171 		 * what AGP versions the card supports.  We still have to
172 		 * program it using the v2 registers if it has chosen to use
173 		 * compatibility mode.
174 		 */
175 		agpsel = pci_read_config(dev, AGP_VIA_AGPSEL, 1);
176 		if ((agpsel & (1 << 1)) == 0)
177 			sc->regs = via_v3_regs;
178 		else
179 			sc->regs = via_v2_regs;
180 		break;
181 	default:
182 		sc->regs = via_v2_regs;
183 		break;
184 	}
185 
186 	error = agp_generic_attach(dev);
187 	if (error)
188 		return error;
189 
190 	sc->initial_aperture = AGP_GET_APERTURE(dev);
191 	if (sc->initial_aperture == 0) {
192 		device_printf(dev, "bad initial aperture size, disabling\n");
193 		return ENXIO;
194 	}
195 
196 	for (;;) {
197 		gatt = agp_alloc_gatt(dev);
198 		if (gatt)
199 			break;
200 
201 		/*
202 		 * Probably contigmalloc failure. Try reducing the
203 		 * aperture so that the gatt size reduces.
204 		 */
205 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
206 			agp_generic_detach(dev);
207 			return ENOMEM;
208 		}
209 	}
210 	sc->gatt = gatt;
211 
212 	if (sc->regs == via_v2_regs) {
213 		/* Install the gatt. */
214 		pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4);
215 
216 		/* Enable the aperture. */
217 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4);
218 	} else {
219 		u_int32_t gartctrl;
220 
221 		/* Install the gatt. */
222 		pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical, 4);
223 
224 		/* Enable the aperture. */
225 		gartctrl = pci_read_config(dev, sc->regs[REG_ATTBASE], 4);
226 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl | (3 << 7), 4);
227 	}
228 
229 	return 0;
230 }
231 
232 static int
233 agp_via_detach(device_t dev)
234 {
235 	struct agp_via_softc *sc = device_get_softc(dev);
236 
237 	agp_free_cdev(dev);
238 
239 	pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4);
240 	pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4);
241 	AGP_SET_APERTURE(dev, sc->initial_aperture);
242 	agp_free_gatt(sc->gatt);
243 	agp_free_res(dev);
244 
245 	return 0;
246 }
247 
248 static u_int32_t
249 agp_via_get_aperture(device_t dev)
250 {
251 	struct agp_via_softc *sc = device_get_softc(dev);
252 	u_int32_t apsize;
253 
254 	apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1) & 0x1f;
255 
256 	/*
257 	 * The size is determined by the number of low bits of
258 	 * register APBASE which are forced to zero. The low 20 bits
259 	 * are always forced to zero and each zero bit in the apsize
260 	 * field just read forces the corresponding bit in the 27:20
261 	 * to be zero. We calculate the aperture size accordingly.
262 	 */
263 	return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1;
264 }
265 
266 static int
267 agp_via_set_aperture(device_t dev, u_int32_t aperture)
268 {
269 	struct agp_via_softc *sc = device_get_softc(dev);
270 	u_int32_t apsize;
271 
272 	/*
273 	 * Reverse the magic from get_aperture.
274 	 */
275 	apsize = ((aperture - 1) >> 20) ^ 0xff;
276 
277 	/*
278 	 * Double check for sanity.
279 	 */
280 	if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
281 		return EINVAL;
282 
283 	pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1);
284 
285 	return 0;
286 }
287 
288 static int
289 agp_via_bind_page(device_t dev, int offset, vm_offset_t physical)
290 {
291 	struct agp_via_softc *sc = device_get_softc(dev);
292 
293 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
294 		return EINVAL;
295 
296 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
297 	return 0;
298 }
299 
300 static int
301 agp_via_unbind_page(device_t dev, int offset)
302 {
303 	struct agp_via_softc *sc = device_get_softc(dev);
304 
305 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
306 		return EINVAL;
307 
308 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
309 	return 0;
310 }
311 
312 static void
313 agp_via_flush_tlb(device_t dev)
314 {
315 	struct agp_via_softc *sc = device_get_softc(dev);
316 	u_int32_t gartctrl;
317 
318 	if (sc->regs == via_v2_regs) {
319 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4);
320 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4);
321 	} else {
322 		gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4);
323 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl &
324 		    ~(1 << 7), 4);
325 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl, 4);
326 	}
327 
328 }
329 
330 static device_method_t agp_via_methods[] = {
331 	/* Device interface */
332 	DEVMETHOD(device_probe,		agp_via_probe),
333 	DEVMETHOD(device_attach,	agp_via_attach),
334 	DEVMETHOD(device_detach,	agp_via_detach),
335 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
336 	DEVMETHOD(device_suspend,	bus_generic_suspend),
337 	DEVMETHOD(device_resume,	bus_generic_resume),
338 
339 	/* AGP interface */
340 	DEVMETHOD(agp_get_aperture,	agp_via_get_aperture),
341 	DEVMETHOD(agp_set_aperture,	agp_via_set_aperture),
342 	DEVMETHOD(agp_bind_page,	agp_via_bind_page),
343 	DEVMETHOD(agp_unbind_page,	agp_via_unbind_page),
344 	DEVMETHOD(agp_flush_tlb,	agp_via_flush_tlb),
345 	DEVMETHOD(agp_enable,		agp_generic_enable),
346 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
347 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
348 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
349 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
350 
351 	{ 0, 0 }
352 };
353 
354 static driver_t agp_via_driver = {
355 	"agp",
356 	agp_via_methods,
357 	sizeof(struct agp_via_softc),
358 };
359 
360 static devclass_t agp_devclass;
361 
362 DRIVER_MODULE(agp_via, pci, agp_via_driver, agp_devclass, NULL, NULL);
363 MODULE_DEPEND(agp_via, agp, 1, 1, 1);
364 MODULE_DEPEND(agp_via, pci, 1, 1, 1);
365