2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
39 #include <sys/mutex.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <pci/agppriv.h>
45 #include <pci/agpreg.h>
48 #include <vm/vm_object.h>
51 #define MAX_APSIZE 0x3f /* 256 MB */
53 struct agp_intel_softc {
55 u_int32_t initial_aperture; /* aperture size at startup */
56 struct agp_gatt *gatt;
61 agp_intel_match(device_t dev)
63 if (pci_get_class(dev) != PCIC_BRIDGE
64 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
67 if (agp_find_caps(dev) == 0)
70 switch (pci_get_devid(dev)) {
71 /* Intel -- vendor 0x8086 */
73 return ("Intel 82443LX (440 LX) host to PCI bridge");
76 return ("Intel 82443BX (440 BX) host to PCI bridge");
79 return ("Intel 82443GX host to PCI bridge");
82 return ("Intel 82443GX host to AGP bridge");
85 return ("Intel 82815 (i815 GMCH) host to PCI bridge");
89 return ("Intel 82820 host to AGP bridge");
92 return ("Intel 82830 host to AGP bridge");
95 return ("Intel 82840 host to AGP bridge");
98 return ("Intel 82845 host to AGP bridge");
101 return ("Intel 82850 host to AGP bridge");
104 return ("Intel 82855 host to AGP bridge");
107 return ("Intel 82860 host to AGP bridge");
110 return ("Intel 82865 host to AGP bridge");
113 return ("Intel E7205 host to AGP bridge");
116 return ("Intel E7505 host to AGP bridge");
119 return ("Intel 82875P host to AGP bridge");
122 return ("Intel 82845G host to AGP bridge");
125 return ("Intel 82855GM host to AGP bridge");
132 agp_intel_probe(device_t dev)
136 if (resource_disabled("agp", device_get_unit(dev)))
138 desc = agp_intel_match(dev);
140 device_set_desc(dev, desc);
141 return BUS_PROBE_DEFAULT;
148 agp_intel_attach(device_t dev)
150 struct agp_intel_softc *sc = device_get_softc(dev);
151 struct agp_gatt *gatt;
152 u_int32_t type = pci_get_devid(dev);
156 error = agp_generic_attach(dev);
160 /* Determine maximum supported aperture size. */
161 value = pci_read_config(dev, AGP_INTEL_APSIZE, 1);
162 pci_write_config(dev, AGP_INTEL_APSIZE, MAX_APSIZE, 1);
163 sc->aperture_mask = pci_read_config(dev, AGP_INTEL_APSIZE, 1) &
165 pci_write_config(dev, AGP_INTEL_APSIZE, value, 1);
166 sc->initial_aperture = AGP_GET_APERTURE(dev);
169 gatt = agp_alloc_gatt(dev);
174 * Probably contigmalloc failure. Try reducing the
175 * aperture so that the gatt size reduces.
177 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
178 agp_generic_detach(dev);
184 /* Install the gatt. */
185 pci_write_config(dev, AGP_INTEL_ATTBASE, gatt->ag_physical, 4);
187 /* Enable the GLTB and setup the control register. */
189 case 0x71908086: /* 440LX/EX */
190 pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x2080, 4);
192 case 0x71808086: /* 440BX */
194 * XXX: Should be 0xa080? Bit 9 is undefined, and
195 * bit 13 being on and bit 15 being clear is illegal.
197 pci_write_config(dev, AGP_INTEL_AGPCTRL, 0x2280, 4);
200 value = pci_read_config(dev, AGP_INTEL_AGPCTRL, 4);
201 pci_write_config(dev, AGP_INTEL_AGPCTRL, value | 0x80, 4);
204 /* Enable things, clear errors etc. */
206 case 0x1a218086: /* i840 */
207 case 0x25308086: /* i850 */
208 case 0x25318086: /* i860 */
209 pci_write_config(dev, AGP_INTEL_MCHCFG,
210 (pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
214 case 0x25008086: /* i820 */
215 case 0x25018086: /* i820 */
216 pci_write_config(dev, AGP_INTEL_I820_RDCR,
217 (pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
221 case 0x1a308086: /* i845 */
222 case 0x33408086: /* i855 */
223 case 0x35808086: /* i855GM */
224 case 0x255d8086: /* E7205 */
225 case 0x25508086: /* E7505 */
226 case 0x25708086: /* i865 */
227 case 0x25788086: /* i875P */
228 case 0x25608086: /* i845G */
229 pci_write_config(dev, AGP_INTEL_I845_MCHCFG,
230 (pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
234 default: /* Intel Generic (maybe) */
235 pci_write_config(dev, AGP_INTEL_NBXCFG,
236 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
237 & ~(1 << 10)) | (1 << 9), 4);
241 case 0x1a218086: /* i840 */
242 pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0xc000, 2);
245 case 0x25008086: /* i820 */
246 case 0x25018086: /* i820 */
247 case 0x1a308086: /* i845 */
248 case 0x25308086: /* i850 */
249 case 0x33408086: /* i855 */
250 case 0x255d8086: /* E7205 */
251 case 0x25508086: /* E7505 */
252 case 0x25318086: /* i860 */
253 case 0x25708086: /* i865 */
254 case 0x25788086: /* i875P */
255 case 0x25608086: /* i845G */
256 pci_write_config(dev, AGP_INTEL_I8XX_ERRSTS, 0x00ff, 2);
259 default: /* Intel Generic (maybe) */
260 pci_write_config(dev, AGP_INTEL_ERRSTS + 1, 7, 1);
267 agp_intel_detach(device_t dev)
269 struct agp_intel_softc *sc = device_get_softc(dev);
270 u_int32_t type = pci_get_devid(dev);
273 error = agp_generic_detach(dev);
278 case 0x1a218086: /* i840 */
279 case 0x25308086: /* i850 */
280 case 0x25318086: /* i860 */
281 printf("%s: set MCHCFG to %x\n", __func__, (unsigned)
282 (pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
284 pci_write_config(dev, AGP_INTEL_MCHCFG,
285 (pci_read_config(dev, AGP_INTEL_MCHCFG, 2)
288 case 0x25008086: /* i820 */
289 case 0x25018086: /* i820 */
290 printf("%s: set RDCR to %x\n", __func__, (unsigned)
291 (pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
293 pci_write_config(dev, AGP_INTEL_I820_RDCR,
294 (pci_read_config(dev, AGP_INTEL_I820_RDCR, 1)
297 case 0x1a308086: /* i845 */
298 case 0x25608086: /* i845G */
299 case 0x33408086: /* i855 */
300 case 0x35808086: /* i855GM */
301 case 0x255d8086: /* E7205 */
302 case 0x25508086: /* E7505 */
303 case 0x25708086: /* i865 */
304 case 0x25788086: /* i875P */
305 printf("%s: set MCHCFG to %x\n", __func__, (unsigned)
306 (pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
308 pci_write_config(dev, AGP_INTEL_MCHCFG,
309 (pci_read_config(dev, AGP_INTEL_I845_MCHCFG, 1)
312 default: /* Intel Generic (maybe) */
313 printf("%s: set NBXCFG to %x\n", __func__,
314 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
316 pci_write_config(dev, AGP_INTEL_NBXCFG,
317 (pci_read_config(dev, AGP_INTEL_NBXCFG, 4)
320 pci_write_config(dev, AGP_INTEL_ATTBASE, 0, 4);
321 AGP_SET_APERTURE(dev, sc->initial_aperture);
322 agp_free_gatt(sc->gatt);
328 agp_intel_get_aperture(device_t dev)
330 struct agp_intel_softc *sc = device_get_softc(dev);
333 apsize = pci_read_config(dev, AGP_INTEL_APSIZE, 1) & sc->aperture_mask;
336 * The size is determined by the number of low bits of
337 * register APBASE which are forced to zero. The low 22 bits
338 * are always forced to zero and each zero bit in the apsize
339 * field just read forces the corresponding bit in the 27:22
340 * to be zero. We calculate the aperture size accordingly.
342 return (((apsize ^ sc->aperture_mask) << 22) | ((1 << 22) - 1)) + 1;
346 agp_intel_set_aperture(device_t dev, u_int32_t aperture)
348 struct agp_intel_softc *sc = device_get_softc(dev);
352 * Reverse the magic from get_aperture.
354 apsize = ((aperture - 1) >> 22) ^ sc->aperture_mask;
357 * Double check for sanity.
359 if ((((apsize ^ sc->aperture_mask) << 22) | ((1 << 22) - 1)) + 1 != aperture)
362 pci_write_config(dev, AGP_INTEL_APSIZE, apsize, 1);
368 agp_intel_bind_page(device_t dev, int offset, vm_offset_t physical)
370 struct agp_intel_softc *sc = device_get_softc(dev);
372 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
375 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
380 agp_intel_unbind_page(device_t dev, int offset)
382 struct agp_intel_softc *sc = device_get_softc(dev);
384 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
387 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
392 agp_intel_flush_tlb(device_t dev)
396 val = pci_read_config(dev, AGP_INTEL_AGPCTRL, 4);
397 pci_write_config(dev, AGP_INTEL_AGPCTRL, val & ~(1 << 7), 4);
398 pci_write_config(dev, AGP_INTEL_AGPCTRL, val, 4);
401 static device_method_t agp_intel_methods[] = {
402 /* Device interface */
403 DEVMETHOD(device_probe, agp_intel_probe),
404 DEVMETHOD(device_attach, agp_intel_attach),
405 DEVMETHOD(device_detach, agp_intel_detach),
406 DEVMETHOD(device_shutdown, bus_generic_shutdown),
407 DEVMETHOD(device_suspend, bus_generic_suspend),
408 DEVMETHOD(device_resume, bus_generic_resume),
411 DEVMETHOD(agp_get_aperture, agp_intel_get_aperture),
412 DEVMETHOD(agp_set_aperture, agp_intel_set_aperture),
413 DEVMETHOD(agp_bind_page, agp_intel_bind_page),
414 DEVMETHOD(agp_unbind_page, agp_intel_unbind_page),
415 DEVMETHOD(agp_flush_tlb, agp_intel_flush_tlb),
416 DEVMETHOD(agp_enable, agp_generic_enable),
417 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory),
418 DEVMETHOD(agp_free_memory, agp_generic_free_memory),
419 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory),
420 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory),
425 static driver_t agp_intel_driver = {
428 sizeof(struct agp_intel_softc),
431 static devclass_t agp_devclass;
433 DRIVER_MODULE(agp_intel, hostb, agp_intel_driver, agp_devclass, 0, 0);
434 MODULE_DEPEND(agp_intel, agp, 1, 1, 1);
435 MODULE_DEPEND(agp_intel, pci, 1, 1, 1);