xref: /freebsd/sys/dev/agp/agp_via.c (revision 716fd348e01c5f2ba125f878a634a753436c2994)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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/agp/agppriv.h>
43 #include <dev/agp/agpreg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_object.h>
49 #include <vm/pmap.h>
50 
51 #define	REG_GARTCTRL	0
52 #define	REG_APSIZE	1
53 #define	REG_ATTBASE	2
54 
55 struct agp_via_softc {
56 	struct agp_softc agp;
57 	u_int32_t	initial_aperture; /* aperture size at startup */
58 	struct agp_gatt *gatt;
59 	int		*regs;
60 };
61 
62 static int via_v2_regs[] = { AGP_VIA_GARTCTRL, AGP_VIA_APSIZE,
63     AGP_VIA_ATTBASE };
64 static int via_v3_regs[] = { AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE,
65     AGP3_VIA_ATTBASE };
66 
67 static const char*
68 agp_via_match(device_t dev)
69 {
70 	if (pci_get_class(dev) != PCIC_BRIDGE
71 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
72 		return NULL;
73 
74 	if (agp_find_caps(dev) == 0)
75 		return NULL;
76 
77 	switch (pci_get_devid(dev)) {
78 	case 0x01981106:
79 		return ("VIA 8763 (P4X600) host to PCI bridge");
80 	case 0x02591106:
81 		return ("VIA PM800/PN800/PM880/PN880 host to PCI bridge");
82 	case 0x02691106:
83 		return ("VIA KT880 host to PCI bridge");
84 	case 0x02961106:
85 		return ("VIA 3296 (P4M800) host to PCI bridge");
86 	case 0x03051106:
87 		return ("VIA 82C8363 (Apollo KT133x/KM133) host to PCI bridge");
88 	case 0x03141106:
89 		return ("VIA 3314 (P4M800CE) host to PCI bridge");
90 	case 0x03241106:
91 		return ("VIA VT3324 (CX700) host to PCI bridge");
92 	case 0x03271106:
93 		return ("VIA 3327 (P4M890) host to PCI bridge");
94 	case 0x03641106:
95 		return ("VIA 3364 (P4M900) host to PCI bridge");
96 	case 0x03911106:
97 		return ("VIA 8371 (Apollo KX133) host to PCI bridge");
98 	case 0x05011106:
99 		return ("VIA 8501 (Apollo MVP4) host to PCI bridge");
100 	case 0x05971106:
101 		return ("VIA 82C597 (Apollo VP3) host to PCI bridge");
102 	case 0x05981106:
103 		return ("VIA 82C598 (Apollo MVP3) host to PCI bridge");
104 	case 0x06011106:
105 		return ("VIA 8601 (Apollo ProMedia/PLE133Ta) host to PCI bridge");
106 	case 0x06051106:
107 		return ("VIA 82C694X (Apollo Pro 133A) host to PCI bridge");
108 	case 0x06911106:
109 		return ("VIA 82C691 (Apollo Pro) host to PCI bridge");
110 	case 0x30911106:
111 		return ("VIA 8633 (Pro 266) host to PCI bridge");
112 	case 0x30991106:
113 		return ("VIA 8367 (KT266/KY266x/KT333) host to PCI bridge");
114 	case 0x31011106:
115 		return ("VIA 8653 (Pro266T) host to PCI bridge");
116 	case 0x31121106:
117 		return ("VIA 8361 (KLE133) host to PCI bridge");
118 	case 0x31161106:
119 		return ("VIA XM266 (PM266/KM266) host to PCI bridge");
120 	case 0x31231106:
121 		return ("VIA 862x (CLE266) host to PCI bridge");
122 	case 0x31281106:
123 		return ("VIA 8753 (P4X266) host to PCI bridge");
124 	case 0x31481106:
125 		return ("VIA 8703 (P4M266x/P4N266) host to PCI bridge");
126 	case 0x31561106:
127 		return ("VIA XN266 (Apollo Pro266) host to PCI bridge");
128 	case 0x31681106:
129 		return ("VIA 8754 (PT800) host to PCI bridge");
130 	case 0x31891106:
131 		return ("VIA 8377 (Apollo KT400/KT400A/KT600) host to PCI bridge");
132 	case 0x32051106:
133 		return ("VIA 8235/8237 (Apollo KM400/KM400A) host to PCI bridge");
134 	case 0x32081106:
135 		return ("VIA 8783 (PT890) host to PCI bridge");
136 	case 0x32581106:
137 		return ("VIA PT880 host to PCI bridge");
138 	case 0xb1981106:
139 		return ("VIA VT83xx/VT87xx/KTxxx/Px8xx host to PCI bridge");
140 	}
141 
142 	return NULL;
143 }
144 
145 static int
146 agp_via_probe(device_t dev)
147 {
148 	const char *desc;
149 
150 	if (resource_disabled("agp", device_get_unit(dev)))
151 		return (ENXIO);
152 	desc = agp_via_match(dev);
153 	if (desc) {
154 		device_set_desc(dev, desc);
155 		return BUS_PROBE_DEFAULT;
156 	}
157 
158 	return ENXIO;
159 }
160 
161 static int
162 agp_via_attach(device_t dev)
163 {
164 	struct agp_via_softc *sc = device_get_softc(dev);
165 	struct agp_gatt *gatt;
166 	int error;
167 	u_int32_t agpsel;
168 	u_int32_t capid;
169 
170 	sc->regs = via_v2_regs;
171 
172 	/* Look at the capability register to see if we handle AGP3 */
173 	capid = pci_read_config(dev, agp_find_caps(dev) + AGP_CAPID, 4);
174 	if (((capid >> 20) & 0x0f) >= 3) {
175 		agpsel = pci_read_config(dev, AGP_VIA_AGPSEL, 1);
176 		if ((agpsel & (1 << 1)) == 0)
177 			sc->regs = via_v3_regs;
178 	}
179 
180 	error = agp_generic_attach(dev);
181 	if (error)
182 		return error;
183 
184 	sc->initial_aperture = AGP_GET_APERTURE(dev);
185 
186 	for (;;) {
187 		gatt = agp_alloc_gatt(dev);
188 		if (gatt)
189 			break;
190 
191 		/*
192 		 * Probably contigmalloc failure. Try reducing the
193 		 * aperture so that the gatt size reduces.
194 		 */
195 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
196 			agp_generic_detach(dev);
197 			return ENOMEM;
198 		}
199 	}
200 	sc->gatt = gatt;
201 
202 	if (sc->regs == via_v2_regs) {
203 		/* Install the gatt. */
204 		pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4);
205 
206 		/* Enable the aperture. */
207 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4);
208 	} else {
209 		u_int32_t gartctrl;
210 
211 		/* Install the gatt. */
212 		pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical, 4);
213 
214 		/* Enable the aperture. */
215 		gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4);
216 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl | (3 << 7), 4);
217 	}
218 
219 	device_printf(dev, "aperture size is %dM\n",
220 		sc->initial_aperture / 1024 / 1024);
221 
222 	return 0;
223 }
224 
225 static int
226 agp_via_detach(device_t dev)
227 {
228 	struct agp_via_softc *sc = device_get_softc(dev);
229 
230 	agp_free_cdev(dev);
231 
232 	pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4);
233 	pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4);
234 	AGP_SET_APERTURE(dev, sc->initial_aperture);
235 	agp_free_gatt(sc->gatt);
236 	agp_free_res(dev);
237 
238 	return 0;
239 }
240 
241 static u_int32_t
242 agp_via_get_aperture(device_t dev)
243 {
244 	struct agp_via_softc *sc = device_get_softc(dev);
245 	u_int32_t apsize;
246 
247 	if (sc->regs == via_v2_regs) {
248 		apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1);
249 
250 		/*
251 		 * The size is determined by the number of low bits of
252 		 * register APBASE which are forced to zero. The low 20 bits
253 		 * are always forced to zero and each zero bit in the apsize
254 		 * field just read forces the corresponding bit in the 27:20
255 		 * to be zero. We calculate the aperture size accordingly.
256 		 */
257 		return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1;
258 	} else {
259 		apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 2) & 0xfff;
260 		switch (apsize) {
261 		case 0x800:
262 			return 0x80000000;
263 		case 0xc00:
264 			return 0x40000000;
265 		case 0xe00:
266 			return 0x20000000;
267 		case 0xf00:
268 			return 0x10000000;
269 		case 0xf20:
270 			return 0x08000000;
271 		case 0xf30:
272 			return 0x04000000;
273 		case 0xf38:
274 			return 0x02000000;
275 		case 0xf3c:
276 			return 0x01000000;
277 		case 0xf3e:
278 			return 0x00800000;
279 		case 0xf3f:
280 			return 0x00400000;
281 		default:
282 			device_printf(dev, "Invalid aperture setting 0x%x\n",
283 			    pci_read_config(dev, sc->regs[REG_APSIZE], 2));
284 			return 0;
285 		}
286 	}
287 }
288 
289 static int
290 agp_via_set_aperture(device_t dev, u_int32_t aperture)
291 {
292 	struct agp_via_softc *sc = device_get_softc(dev);
293 	u_int32_t apsize, key, val;
294 
295 	if (sc->regs == via_v2_regs) {
296 		/*
297 		 * Reverse the magic from get_aperture.
298 		 */
299 		apsize = ((aperture - 1) >> 20) ^ 0xff;
300 
301 		/*
302 	 	 * Double check for sanity.
303 	 	 */
304 		if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
305 			return EINVAL;
306 
307 		pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1);
308 	} else {
309 		switch (aperture) {
310 		case 0x80000000:
311 			key = 0x800;
312 			break;
313 		case 0x40000000:
314 			key = 0xc00;
315 			break;
316 		case 0x20000000:
317 			key = 0xe00;
318 			break;
319 		case 0x10000000:
320 			key = 0xf00;
321 			break;
322 		case 0x08000000:
323 			key = 0xf20;
324 			break;
325 		case 0x04000000:
326 			key = 0xf30;
327 			break;
328 		case 0x02000000:
329 			key = 0xf38;
330 			break;
331 		case 0x01000000:
332 			key = 0xf3c;
333 			break;
334 		case 0x00800000:
335 			key = 0xf3e;
336 			break;
337 		case 0x00400000:
338 			key = 0xf3f;
339 			break;
340 		default:
341 			device_printf(dev, "Invalid aperture size (%dMb)\n",
342 			    aperture / 1024 / 1024);
343 			return EINVAL;
344 		}
345 		val = pci_read_config(dev, sc->regs[REG_APSIZE], 2);
346 		pci_write_config(dev, sc->regs[REG_APSIZE],
347 		    ((val & ~0xfff) | key), 2);
348 	}
349 	return 0;
350 }
351 
352 static int
353 agp_via_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
354 {
355 	struct agp_via_softc *sc = device_get_softc(dev);
356 
357 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
358 		return EINVAL;
359 
360 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
361 	return 0;
362 }
363 
364 static int
365 agp_via_unbind_page(device_t dev, vm_offset_t offset)
366 {
367 	struct agp_via_softc *sc = device_get_softc(dev);
368 
369 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
370 		return EINVAL;
371 
372 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
373 	return 0;
374 }
375 
376 static void
377 agp_via_flush_tlb(device_t dev)
378 {
379 	struct agp_via_softc *sc = device_get_softc(dev);
380 	u_int32_t gartctrl;
381 
382 	if (sc->regs == via_v2_regs) {
383 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4);
384 		pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4);
385 	} else {
386 		gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4);
387 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl &
388 		    ~(1 << 7), 4);
389 		pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl, 4);
390 	}
391 
392 }
393 
394 static device_method_t agp_via_methods[] = {
395 	/* Device interface */
396 	DEVMETHOD(device_probe,		agp_via_probe),
397 	DEVMETHOD(device_attach,	agp_via_attach),
398 	DEVMETHOD(device_detach,	agp_via_detach),
399 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
400 	DEVMETHOD(device_suspend,	bus_generic_suspend),
401 	DEVMETHOD(device_resume,	bus_generic_resume),
402 
403 	/* AGP interface */
404 	DEVMETHOD(agp_get_aperture,	agp_via_get_aperture),
405 	DEVMETHOD(agp_set_aperture,	agp_via_set_aperture),
406 	DEVMETHOD(agp_bind_page,	agp_via_bind_page),
407 	DEVMETHOD(agp_unbind_page,	agp_via_unbind_page),
408 	DEVMETHOD(agp_flush_tlb,	agp_via_flush_tlb),
409 	DEVMETHOD(agp_enable,		agp_generic_enable),
410 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
411 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
412 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
413 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
414 	{ 0, 0 }
415 };
416 
417 static driver_t agp_via_driver = {
418 	"agp",
419 	agp_via_methods,
420 	sizeof(struct agp_via_softc),
421 };
422 
423 DRIVER_MODULE(agp_via, hostb, agp_via_driver, 0, 0);
424 MODULE_DEPEND(agp_via, agp, 1, 1, 1);
425 MODULE_DEPEND(agp_via, pci, 1, 1, 1);
426