xref: /freebsd/sys/dev/agp/agp_ali.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
159747216SDoug Rabson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
459747216SDoug Rabson  * Copyright (c) 2000 Doug Rabson
559747216SDoug Rabson  * All rights reserved.
659747216SDoug Rabson  *
759747216SDoug Rabson  * Redistribution and use in source and binary forms, with or without
859747216SDoug Rabson  * modification, are permitted provided that the following conditions
959747216SDoug Rabson  * are met:
1059747216SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1159747216SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1259747216SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1359747216SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1459747216SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1559747216SDoug Rabson  *
1659747216SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1759747216SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1859747216SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1959747216SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2059747216SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2159747216SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2259747216SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2359747216SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2459747216SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2559747216SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2659747216SDoug Rabson  * SUCH DAMAGE.
2759747216SDoug Rabson  */
2859747216SDoug Rabson 
2959747216SDoug Rabson #include <sys/param.h>
3059747216SDoug Rabson #include <sys/systm.h>
3159747216SDoug Rabson #include <sys/malloc.h>
3259747216SDoug Rabson #include <sys/kernel.h>
33f11d01c3SPoul-Henning Kamp #include <sys/module.h>
3459747216SDoug Rabson #include <sys/bus.h>
3559747216SDoug Rabson #include <sys/lock.h>
3623955314SAlfred Perlstein #include <sys/mutex.h>
3752b3919dSJohn Baldwin #include <sys/proc.h>
3859747216SDoug Rabson 
39dbac8ff4SJohn Baldwin #include <dev/agp/agppriv.h>
40dbac8ff4SJohn Baldwin #include <dev/agp/agpreg.h>
4119b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
4219b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
4359747216SDoug Rabson 
4459747216SDoug Rabson #include <vm/vm.h>
4559747216SDoug Rabson #include <vm/vm_object.h>
4659747216SDoug Rabson #include <vm/pmap.h>
4759747216SDoug Rabson 
4859747216SDoug Rabson struct agp_ali_softc {
4959747216SDoug Rabson 	struct agp_softc agp;
5059747216SDoug Rabson 	u_int32_t	initial_aperture; /* aperture size at startup */
5159747216SDoug Rabson 	struct agp_gatt *gatt;
5259747216SDoug Rabson };
5359747216SDoug Rabson 
5459747216SDoug Rabson static const char*
agp_ali_match(device_t dev)5559747216SDoug Rabson agp_ali_match(device_t dev)
5659747216SDoug Rabson {
5759747216SDoug Rabson 	if (pci_get_class(dev) != PCIC_BRIDGE
5859747216SDoug Rabson 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
5959747216SDoug Rabson 		return NULL;
6059747216SDoug Rabson 
6159747216SDoug Rabson 	if (agp_find_caps(dev) == 0)
6259747216SDoug Rabson 		return NULL;
6359747216SDoug Rabson 
6459747216SDoug Rabson 	switch (pci_get_devid(dev)) {
654ac78327SOlivier Houchard 	case 0x167110b9:
664ac78327SOlivier Houchard 		return ("Ali M1671 host to AGP bridge");
6759747216SDoug Rabson 	case 0x154110b9:
6859747216SDoug Rabson 		return ("Ali M1541 host to AGP bridge");
69124cfec0SEric Anholt 	case 0x162110b9:
70124cfec0SEric Anholt 		return ("Ali M1621 host to AGP bridge");
71dfa4d7fdSAntoine Brodin 	}
7259747216SDoug Rabson 
7359747216SDoug Rabson 	return NULL;
7459747216SDoug Rabson }
7559747216SDoug Rabson 
7659747216SDoug Rabson static int
agp_ali_probe(device_t dev)7759747216SDoug Rabson agp_ali_probe(device_t dev)
7859747216SDoug Rabson {
7959747216SDoug Rabson 	const char *desc;
8059747216SDoug Rabson 
81a8de37b0SEitan Adler 	if (resource_disabled("agp", device_get_unit(dev)))
82a8de37b0SEitan Adler 		return (ENXIO);
8359747216SDoug Rabson 	desc = agp_ali_match(dev);
8459747216SDoug Rabson 	if (desc) {
8559747216SDoug Rabson 		device_set_desc(dev, desc);
86d701c913SWarner Losh 		return BUS_PROBE_DEFAULT;
8759747216SDoug Rabson 	}
8859747216SDoug Rabson 
8959747216SDoug Rabson 	return ENXIO;
9059747216SDoug Rabson }
9159747216SDoug Rabson 
9259747216SDoug Rabson static int
agp_ali_attach(device_t dev)9359747216SDoug Rabson agp_ali_attach(device_t dev)
9459747216SDoug Rabson {
9559747216SDoug Rabson 	struct agp_ali_softc *sc = device_get_softc(dev);
9659747216SDoug Rabson 	struct agp_gatt *gatt;
9759747216SDoug Rabson 	int error;
9874674ea7SEric Anholt 	u_int32_t attbase;
9959747216SDoug Rabson 
10059747216SDoug Rabson 	error = agp_generic_attach(dev);
10159747216SDoug Rabson 	if (error)
10259747216SDoug Rabson 		return error;
10359747216SDoug Rabson 
10459747216SDoug Rabson 	sc->initial_aperture = AGP_GET_APERTURE(dev);
105a6cb9d8eSEric Anholt 	if (sc->initial_aperture == 0) {
106a6cb9d8eSEric Anholt 		device_printf(dev, "bad initial aperture size, disabling\n");
107a6cb9d8eSEric Anholt 		return ENXIO;
108a6cb9d8eSEric Anholt 	}
10959747216SDoug Rabson 
11059747216SDoug Rabson 	for (;;) {
11159747216SDoug Rabson 		gatt = agp_alloc_gatt(dev);
11259747216SDoug Rabson 		if (gatt)
11359747216SDoug Rabson 			break;
11459747216SDoug Rabson 
11559747216SDoug Rabson 		/*
11659747216SDoug Rabson 		 * Probably contigmalloc failure. Try reducing the
11759747216SDoug Rabson 		 * aperture so that the gatt size reduces.
11859747216SDoug Rabson 		 */
11959747216SDoug Rabson 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
12059747216SDoug Rabson 			agp_generic_detach(dev);
12159747216SDoug Rabson 			return ENOMEM;
12259747216SDoug Rabson 		}
12359747216SDoug Rabson 	}
12459747216SDoug Rabson 	sc->gatt = gatt;
12559747216SDoug Rabson 
12659747216SDoug Rabson 	/* Install the gatt. */
12774674ea7SEric Anholt 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
12874674ea7SEric Anholt 	pci_write_config(dev, AGP_ALI_ATTBASE, gatt->ag_physical |
1298c9610c9SEric Anholt 	    (attbase & 0xfff), 4);
13059747216SDoug Rabson 
13159747216SDoug Rabson 	/* Enable the TLB. */
13259747216SDoug Rabson 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
13359747216SDoug Rabson 
13459747216SDoug Rabson 	return 0;
13559747216SDoug Rabson }
13659747216SDoug Rabson 
13759747216SDoug Rabson static int
agp_ali_detach(device_t dev)13859747216SDoug Rabson agp_ali_detach(device_t dev)
13959747216SDoug Rabson {
14059747216SDoug Rabson 	struct agp_ali_softc *sc = device_get_softc(dev);
14174674ea7SEric Anholt 	u_int32_t attbase;
14259747216SDoug Rabson 
143f82a1d49SJohn Baldwin 	agp_free_cdev(dev);
14459747216SDoug Rabson 
14559747216SDoug Rabson 	/* Disable the TLB.. */
14659747216SDoug Rabson 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
14759747216SDoug Rabson 
14859747216SDoug Rabson 	/* Put the aperture back the way it started. */
14959747216SDoug Rabson 	AGP_SET_APERTURE(dev, sc->initial_aperture);
15074674ea7SEric Anholt 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
1518c9610c9SEric Anholt 	pci_write_config(dev, AGP_ALI_ATTBASE, attbase & 0xfff, 4);
15259747216SDoug Rabson 
15359747216SDoug Rabson 	agp_free_gatt(sc->gatt);
154f82a1d49SJohn Baldwin 	agp_free_res(dev);
15559747216SDoug Rabson 	return 0;
15659747216SDoug Rabson }
15759747216SDoug Rabson 
15859747216SDoug Rabson #define M 1024*1024
15959747216SDoug Rabson 
16059747216SDoug Rabson static u_int32_t agp_ali_table[] = {
16159747216SDoug Rabson 	0,			/* 0 - invalid */
16259747216SDoug Rabson 	1,			/* 1 - invalid */
16359747216SDoug Rabson 	2,			/* 2 - invalid */
16459747216SDoug Rabson 	4*M,			/* 3 - invalid */
16559747216SDoug Rabson 	8*M,			/* 4 - invalid */
16659747216SDoug Rabson 	0,			/* 5 - invalid */
16759747216SDoug Rabson 	16*M,			/* 6 - invalid */
16859747216SDoug Rabson 	32*M,			/* 7 - invalid */
16959747216SDoug Rabson 	64*M,			/* 8 - invalid */
17059747216SDoug Rabson 	128*M,			/* 9 - invalid */
17159747216SDoug Rabson 	256*M,			/* 10 - invalid */
17259747216SDoug Rabson };
1734ec642f1SPedro F. Giffuni #define	AGP_ALI_TABLE_SIZE nitems(agp_ali_table)
17459747216SDoug Rabson 
17559747216SDoug Rabson static u_int32_t
agp_ali_get_aperture(device_t dev)17659747216SDoug Rabson agp_ali_get_aperture(device_t dev)
17759747216SDoug Rabson {
17859747216SDoug Rabson 	/*
17959747216SDoug Rabson 	 * The aperture size is derived from the low bits of attbase.
18059747216SDoug Rabson 	 * I'm not sure this is correct..
18159747216SDoug Rabson 	 */
1828c9610c9SEric Anholt 	int i = pci_read_config(dev, AGP_ALI_ATTBASE, 4) & 0xf;
1834ec642f1SPedro F. Giffuni 	if (i >= AGP_ALI_TABLE_SIZE)
18459747216SDoug Rabson 		return 0;
18559747216SDoug Rabson 	return agp_ali_table[i];
18659747216SDoug Rabson }
18759747216SDoug Rabson 
18859747216SDoug Rabson static int
agp_ali_set_aperture(device_t dev,u_int32_t aperture)18959747216SDoug Rabson agp_ali_set_aperture(device_t dev, u_int32_t aperture)
19059747216SDoug Rabson {
19159747216SDoug Rabson 	int i;
19274674ea7SEric Anholt 	u_int32_t attbase;
19359747216SDoug Rabson 
1944ec642f1SPedro F. Giffuni 	for (i = 0; i < AGP_ALI_TABLE_SIZE; i++)
19559747216SDoug Rabson 		if (agp_ali_table[i] == aperture)
19659747216SDoug Rabson 			break;
1974ec642f1SPedro F. Giffuni 	if (i == AGP_ALI_TABLE_SIZE)
19859747216SDoug Rabson 		return EINVAL;
19959747216SDoug Rabson 
20074674ea7SEric Anholt 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
2018c9610c9SEric Anholt 	pci_write_config(dev, AGP_ALI_ATTBASE, (attbase & ~0xf) | i, 4);
20259747216SDoug Rabson 	return 0;
20359747216SDoug Rabson }
20459747216SDoug Rabson 
20559747216SDoug Rabson static int
agp_ali_bind_page(device_t dev,vm_offset_t offset,vm_offset_t physical)206446188d1SAndriy Gapon agp_ali_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
20759747216SDoug Rabson {
20859747216SDoug Rabson 	struct agp_ali_softc *sc = device_get_softc(dev);
20959747216SDoug Rabson 
210446188d1SAndriy Gapon 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
21159747216SDoug Rabson 		return EINVAL;
21259747216SDoug Rabson 
21359747216SDoug Rabson 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
21459747216SDoug Rabson 	return 0;
21559747216SDoug Rabson }
21659747216SDoug Rabson 
21759747216SDoug Rabson static int
agp_ali_unbind_page(device_t dev,vm_offset_t offset)218446188d1SAndriy Gapon agp_ali_unbind_page(device_t dev, vm_offset_t offset)
21959747216SDoug Rabson {
22059747216SDoug Rabson 	struct agp_ali_softc *sc = device_get_softc(dev);
22159747216SDoug Rabson 
222446188d1SAndriy Gapon 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
22359747216SDoug Rabson 		return EINVAL;
22459747216SDoug Rabson 
22559747216SDoug Rabson 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
22659747216SDoug Rabson 	return 0;
22759747216SDoug Rabson }
22859747216SDoug Rabson 
22959747216SDoug Rabson static void
agp_ali_flush_tlb(device_t dev)23059747216SDoug Rabson agp_ali_flush_tlb(device_t dev)
23159747216SDoug Rabson {
23259747216SDoug Rabson 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
23359747216SDoug Rabson 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
23459747216SDoug Rabson }
23559747216SDoug Rabson 
23659747216SDoug Rabson static device_method_t agp_ali_methods[] = {
23759747216SDoug Rabson 	/* Device interface */
23859747216SDoug Rabson 	DEVMETHOD(device_probe,		agp_ali_probe),
23959747216SDoug Rabson 	DEVMETHOD(device_attach,	agp_ali_attach),
24059747216SDoug Rabson 	DEVMETHOD(device_detach,	agp_ali_detach),
24159747216SDoug Rabson 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
24259747216SDoug Rabson 	DEVMETHOD(device_suspend,	bus_generic_suspend),
24359747216SDoug Rabson 	DEVMETHOD(device_resume,	bus_generic_resume),
24459747216SDoug Rabson 
24559747216SDoug Rabson 	/* AGP interface */
24659747216SDoug Rabson 	DEVMETHOD(agp_get_aperture,	agp_ali_get_aperture),
24759747216SDoug Rabson 	DEVMETHOD(agp_set_aperture,	agp_ali_set_aperture),
24859747216SDoug Rabson 	DEVMETHOD(agp_bind_page,	agp_ali_bind_page),
24959747216SDoug Rabson 	DEVMETHOD(agp_unbind_page,	agp_ali_unbind_page),
25059747216SDoug Rabson 	DEVMETHOD(agp_flush_tlb,	agp_ali_flush_tlb),
25159747216SDoug Rabson 	DEVMETHOD(agp_enable,		agp_generic_enable),
25259747216SDoug Rabson 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
25359747216SDoug Rabson 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
25459747216SDoug Rabson 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
25559747216SDoug Rabson 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
25659747216SDoug Rabson 	{ 0, 0 }
25759747216SDoug Rabson };
25859747216SDoug Rabson 
25959747216SDoug Rabson static driver_t agp_ali_driver = {
26059747216SDoug Rabson 	"agp",
26159747216SDoug Rabson 	agp_ali_methods,
26259747216SDoug Rabson 	sizeof(struct agp_ali_softc),
26359747216SDoug Rabson };
26459747216SDoug Rabson 
26574f84981SJohn Baldwin DRIVER_MODULE(agp_ali, hostb, agp_ali_driver, 0, 0);
266f246e4a1SMatthew N. Dodd MODULE_DEPEND(agp_ali, agp, 1, 1, 1);
267f246e4a1SMatthew N. Dodd MODULE_DEPEND(agp_ali, pci, 1, 1, 1);
268