xref: /freebsd/sys/dev/agp/agp_intel.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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$
27  */
28 
29 #include "opt_bus.h"
30 #include "opt_pci.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/bus.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 
41 #include <pci/pcivar.h>
42 #include <pci/pcireg.h>
43 #include <pci/agppriv.h>
44 #include <pci/agpreg.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_object.h>
48 #include <vm/pmap.h>
49 
50 struct agp_intel_softc {
51 	struct agp_softc agp;
52 	u_int32_t	initial_aperture; /* aperture size at startup */
53 	struct agp_gatt *gatt;
54 };
55 
56 static const char*
57 agp_intel_match(device_t dev)
58 {
59 	if (pci_get_class(dev) != PCIC_BRIDGE
60 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
61 		return NULL;
62 
63 	if (agp_find_caps(dev) == 0)
64 		return NULL;
65 
66 	switch (pci_get_devid(dev)) {
67 	/* Intel -- vendor 0x8086 */
68 	case 0x71808086:
69 		return ("Intel 82443LX (440 LX) host to PCI bridge");
70 
71 	case 0x71908086:
72 		return ("Intel 82443BX (440 BX) host to PCI bridge");
73 
74  	case 0x71a08086:
75  		return ("Intel 82443GX host to PCI bridge");
76 
77  	case 0x71a18086:
78  		return ("Intel 82443GX host to AGP bridge");
79 
80 	case 0x11308086:
81 		return ("Intel 82815 (i815 GMCH) host to PCI bridge");
82 
83 	case 0x25008086:
84 		return ("Intel 82820 host to AGP bridge");
85 
86 	case 0x35758086:
87 		return ("Intel 82830 host to AGP bridge");
88 
89 	case 0x1a218086:
90 		return ("Intel 82840 host to AGP bridge");
91 
92 	case 0x1a308086:
93 		return ("Intel 82845 host to AGP bridge");
94 
95 	case 0x25308086:
96 		return ("Intel 82850 host to AGP bridge");
97 
98 	case 0x25318086:
99 		return ("Intel 82860 host to AGP bridge");
100 	};
101 
102 	if (pci_get_vendor(dev) == 0x8086)
103 		return ("Intel Generic host to PCI bridge");
104 
105 	return NULL;
106 }
107 
108 static int
109 agp_intel_probe(device_t dev)
110 {
111 	const char *desc;
112 
113 	desc = agp_intel_match(dev);
114 	if (desc) {
115 		device_verbose(dev);
116 		device_set_desc(dev, desc);
117 		return 0;
118 	}
119 
120 	return ENXIO;
121 }
122 
123 static int
124 agp_intel_attach(device_t dev)
125 {
126 	struct agp_intel_softc *sc = device_get_softc(dev);
127 	struct agp_gatt *gatt;
128 	u_int32_t type = pci_get_devid(dev);
129 	int error;
130 
131 	error = agp_generic_attach(dev);
132 	if (error)
133 		return error;
134 
135 	sc->initial_aperture = AGP_GET_APERTURE(dev);
136 
137 	for (;;) {
138 		gatt = agp_alloc_gatt(dev);
139 		if (gatt)
140 			break;
141 
142 		/*
143 		 * Probably contigmalloc failure. Try reducing the
144 		 * aperture so that the gatt size reduces.
145 		 */
146 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
147 			agp_generic_detach(dev);
148 			return ENOMEM;
149 		}
150 	}
151 	sc->gatt = gatt;
152 
153 	/* Install the gatt. */
154 	pci_write_config(dev, AGP_INTEL_ATTBASE, gatt->ag_physical, 4);
155 
156 	/* Enable things, clear errors etc. */
157 	switch (type) {
158 	case 0x1a218086: /* i840 */
159 	case 0x25308086: /* i850 */
160 	case 0x25318086: /* i860 */
161 		pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x0000, 4);
162 		pci_write_config(dev, AGP_INTEL_MCHCFG,
163 				 (pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
164 				  | (1 << 9)), 2);
165 		break;
166 
167 	case 0x25008086: /* i820 */
168 		pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x0000, 4);
169 		pci_write_config(dev, AGP_INTEL_I820_RDCR,
170 				 (pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
171 				  | (1 << 1)), 1);
172 		break;
173 
174 	case 0x1a308086: /* i845 */
175 		pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x0000, 4);
176 		pci_write_config(dev, AGP_INTEL_I845_MCHCFG,
177 				 (pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
178 				  | (1 << 1)), 1);
179 		break;
180 
181 	default: /* Intel Generic (maybe) */
182 		pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x2280, 4);
183 		pci_write_config(dev, AGP_INTEL_NBXCFG,
184 				 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
185 				  & ~(1 << 10)) | (1 << 9), 4);
186 	}
187 
188 	switch (type) {
189 	case 0x1a218086: /* i840 */
190 		pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0xc000, 2);
191 		break;
192 
193 	case 0x25008086: /* i820 */
194 	case 0x1a308086: /* i845 */
195 	case 0x25308086: /* i850 */
196 	case 0x25318086: /* i860 */
197 		pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0x001c, 2);
198 		break;
199 
200 	default: /* Intel Generic (maybe) */
201 		pci_write_config(dev, AGP_INTEL_ERRSTS + 1, 7, 1);
202 	}
203 
204 	return 0;
205 }
206 
207 static int
208 agp_intel_detach(device_t dev)
209 {
210 	struct agp_intel_softc *sc = device_get_softc(dev);
211 	u_int32_t type = pci_get_devid(dev);
212 	int error;
213 
214 	error = agp_generic_detach(dev);
215 	if (error)
216 		return error;
217 
218 	switch (type) {
219 	case 0x1a218086: /* i840 */
220 	case 0x25308086: /* i850 */
221 	case 0x25318086: /* i860 */
222 		printf("%s: set MCHCFG to %x\n", __func__, (unsigned)
223 				(pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
224 				& ~(1 << 9)));
225 		pci_write_config(dev, AGP_INTEL_MCHCFG,
226 				(pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
227 				& ~(1 << 9)), 2);
228 
229 	case 0x25008086: /* i820 */
230 		printf("%s: set RDCR to %x\n", __func__, (unsigned)
231 				(pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
232 				& ~(1 << 1)));
233 		pci_write_config(dev, AGP_INTEL_I820_RDCR,
234 				(pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
235 				& ~(1 << 1)), 1);
236 
237 	case 0x1a308086: /* i845 */
238 		printf("%s: set MCHCFG to %x\n", __func__, (unsigned)
239 				(pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
240 				& ~(1 << 1)));
241 		pci_write_config(dev, AGP_INTEL_MCHCFG,
242 				(pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
243 				& ~(1 << 1)), 1);
244 
245 	default: /* Intel Generic (maybe) */
246 		printf("%s: set NBXCFG to %x\n", __func__,
247 				 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
248 				  & ~(1 << 9)));
249 		pci_write_config(dev, AGP_INTEL_NBXCFG,
250 				 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
251 				  & ~(1 << 9)), 4);
252 	}
253 	pci_write_config(dev, AGP_INTEL_ATTBASE, 0, 4);
254 	AGP_SET_APERTURE(dev, sc->initial_aperture);
255 	agp_free_gatt(sc->gatt);
256 
257 	return 0;
258 }
259 
260 static u_int32_t
261 agp_intel_get_aperture(device_t dev)
262 {
263 	u_int32_t apsize;
264 
265 	apsize = pci_read_config(dev, AGP_INTEL_APSIZE, 1) & 0x1f;
266 
267 	/*
268 	 * The size is determined by the number of low bits of
269 	 * register APBASE which are forced to zero. The low 22 bits
270 	 * are always forced to zero and each zero bit in the apsize
271 	 * field just read forces the corresponding bit in the 27:22
272 	 * to be zero. We calculate the aperture size accordingly.
273 	 */
274 	return (((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1;
275 }
276 
277 static int
278 agp_intel_set_aperture(device_t dev, u_int32_t aperture)
279 {
280 	u_int32_t apsize;
281 
282 	/*
283 	 * Reverse the magic from get_aperture.
284 	 */
285 	apsize = ((aperture - 1) >> 22) ^ 0x1f;
286 
287 	/*
288 	 * Double check for sanity.
289 	 */
290 	if ((((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1 != aperture)
291 		return EINVAL;
292 
293 	pci_write_config(dev, AGP_INTEL_APSIZE, apsize, 1);
294 
295 	return 0;
296 }
297 
298 static int
299 agp_intel_bind_page(device_t dev, int offset, vm_offset_t physical)
300 {
301 	struct agp_intel_softc *sc = device_get_softc(dev);
302 
303 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
304 		return EINVAL;
305 
306 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
307 	return 0;
308 }
309 
310 static int
311 agp_intel_unbind_page(device_t dev, int offset)
312 {
313 	struct agp_intel_softc *sc = device_get_softc(dev);
314 
315 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
316 		return EINVAL;
317 
318 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
319 	return 0;
320 }
321 
322 static void
323 agp_intel_flush_tlb(device_t dev)
324 {
325 	pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x2200, 4);
326 	pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x2280, 4);
327 }
328 
329 static device_method_t agp_intel_methods[] = {
330 	/* Device interface */
331 	DEVMETHOD(device_probe,		agp_intel_probe),
332 	DEVMETHOD(device_attach,	agp_intel_attach),
333 	DEVMETHOD(device_detach,	agp_intel_detach),
334 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
335 	DEVMETHOD(device_suspend,	bus_generic_suspend),
336 	DEVMETHOD(device_resume,	bus_generic_resume),
337 
338 	/* AGP interface */
339 	DEVMETHOD(agp_get_aperture,	agp_intel_get_aperture),
340 	DEVMETHOD(agp_set_aperture,	agp_intel_set_aperture),
341 	DEVMETHOD(agp_bind_page,	agp_intel_bind_page),
342 	DEVMETHOD(agp_unbind_page,	agp_intel_unbind_page),
343 	DEVMETHOD(agp_flush_tlb,	agp_intel_flush_tlb),
344 	DEVMETHOD(agp_enable,		agp_generic_enable),
345 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
346 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
347 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
348 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
349 
350 	{ 0, 0 }
351 };
352 
353 static driver_t agp_intel_driver = {
354 	"agp",
355 	agp_intel_methods,
356 	sizeof(struct agp_intel_softc),
357 };
358 
359 static devclass_t agp_devclass;
360 
361 DRIVER_MODULE(agp_intel, pci, agp_intel_driver, agp_devclass, 0, 0);
362