agp_sis.c revision 244926
1296417Sdim/*-
2285101Semaste * Copyright (c) 2000 Doug Rabson
3353358Sdim * All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6285101Semaste * modification, are permitted provided that the following conditions
7285101Semaste * are met:
8285101Semaste * 1. Redistributions of source code must retain the above copyright
9285101Semaste *    notice, this list of conditions and the following disclaimer.
10285101Semaste * 2. Redistributions in binary form must reproduce the above copyright
11285101Semaste *    notice, this list of conditions and the following disclaimer in the
12296417Sdim *    documentation and/or other materials provided with the distribution.
13296417Sdim *
14321369Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15296417Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16314564Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17314564Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18314564Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19314564Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20314564Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21314564Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22314564Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23314564Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24296417Sdim * SUCH DAMAGE.
25285101Semaste */
26314564Sdim
27314564Sdim#include <sys/cdefs.h>
28314564Sdim__FBSDID("$FreeBSD: head/sys/dev/agp/agp_sis.c 244926 2013-01-01 18:16:49Z antoine $");
29296417Sdim
30314564Sdim#include <sys/param.h>
31285101Semaste#include <sys/systm.h>
32314564Sdim#include <sys/malloc.h>
33285101Semaste#include <sys/kernel.h>
34314564Sdim#include <sys/module.h>
35285101Semaste#include <sys/bus.h>
36314564Sdim#include <sys/lock.h>
37285101Semaste#include <sys/mutex.h>
38314564Sdim#include <sys/proc.h>
39314564Sdim
40314564Sdim#include <dev/agp/agppriv.h>
41285101Semaste#include <dev/agp/agpreg.h>
42314564Sdim#include <dev/pci/pcivar.h>
43314564Sdim#include <dev/pci/pcireg.h>
44314564Sdim
45314564Sdim#include <vm/vm.h>
46314564Sdim#include <vm/vm_object.h>
47314564Sdim#include <vm/pmap.h>
48314564Sdim
49285101Semastestruct agp_sis_softc {
50314564Sdim	struct agp_softc agp;
51314564Sdim	u_int32_t	initial_aperture; /* aperture size at startup */
52285101Semaste	struct agp_gatt *gatt;
53314564Sdim};
54314564Sdim
55285101Semastestatic const char*
56314564Sdimagp_sis_match(device_t dev)
57285101Semaste{
58314564Sdim	if (pci_get_class(dev) != PCIC_BRIDGE
59285101Semaste	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
60314564Sdim		return NULL;
61285101Semaste
62314564Sdim	if (agp_find_caps(dev) == 0)
63314564Sdim		return NULL;
64314564Sdim
65314564Sdim	switch (pci_get_devid(dev)) {
66285101Semaste	case 0x00011039:
67314564Sdim		return ("SiS 5591 host to AGP bridge");
68314564Sdim	case 0x05301039:
69314564Sdim		return ("SiS 530 host to AGP bridge");
70314564Sdim	case 0x05401039:
71314564Sdim		return ("SiS 540 host to AGP bridge");
72314564Sdim	case 0x05501039:
73314564Sdim		return ("SiS 550 host to AGP bridge");
74314564Sdim	case 0x06201039:
75314564Sdim		return ("SiS 620 host to AGP bridge");
76314564Sdim	case 0x06301039:
77314564Sdim		return ("SiS 630 host to AGP bridge");
78314564Sdim	case 0x06451039:
79314564Sdim		return ("SiS 645 host to AGP bridge");
80314564Sdim	case 0x06461039:
81314564Sdim		return ("SiS 645DX host to AGP bridge");
82314564Sdim	case 0x06481039:
83285101Semaste		return ("SiS 648 host to AGP bridge");
84314564Sdim	case 0x06501039:
85314564Sdim		return ("SiS 650 host to AGP bridge");
86314564Sdim	case 0x06511039:
87314564Sdim		return ("SiS 651 host to AGP bridge");
88314564Sdim	case 0x06551039:
89285101Semaste		return ("SiS 655 host to AGP bridge");
90314564Sdim	case 0x06611039:
91285101Semaste		return ("SiS 661 host to AGP bridge");
92314564Sdim	case 0x07301039:
93309124Sdim		return ("SiS 730 host to AGP bridge");
94314564Sdim	case 0x07351039:
95309124Sdim		return ("SiS 735 host to AGP bridge");
96314564Sdim	case 0x07401039:
97285101Semaste		return ("SiS 740 host to AGP bridge");
98314564Sdim	case 0x07411039:
99285101Semaste		return ("SiS 741 host to AGP bridge");
100314564Sdim	case 0x07451039:
101285101Semaste		return ("SiS 745 host to AGP bridge");
102314564Sdim	case 0x07461039:
103285101Semaste		return ("SiS 746 host to AGP bridge");
104314564Sdim	}
105285101Semaste
106314564Sdim	return NULL;
107285101Semaste}
108314564Sdim
109285101Semastestatic int
110314564Sdimagp_sis_probe(device_t dev)
111285101Semaste{
112314564Sdim	const char *desc;
113285101Semaste
114314564Sdim	if (resource_disabled("agp", device_get_unit(dev)))
115285101Semaste		return (ENXIO);
116314564Sdim	desc = agp_sis_match(dev);
117285101Semaste	if (desc) {
118314564Sdim		device_set_desc(dev, desc);
119285101Semaste		return BUS_PROBE_DEFAULT;
120314564Sdim	}
121285101Semaste
122314564Sdim	return ENXIO;
123285101Semaste}
124314564Sdim
125285101Semastestatic int
126314564Sdimagp_sis_attach(device_t dev)
127285101Semaste{
128314564Sdim	struct agp_sis_softc *sc = device_get_softc(dev);
129285101Semaste	struct agp_gatt *gatt;
130314564Sdim	int error;
131285101Semaste
132314564Sdim	error = agp_generic_attach(dev);
133285101Semaste	if (error)
134314564Sdim		return error;
135285101Semaste
136314564Sdim	sc->initial_aperture = AGP_GET_APERTURE(dev);
137285101Semaste
138314564Sdim	for (;;) {
139285101Semaste		gatt = agp_alloc_gatt(dev);
140314564Sdim		if (gatt)
141285101Semaste			break;
142314564Sdim
143285101Semaste		/*
144314564Sdim		 * Probably contigmalloc failure. Try reducing the
145285101Semaste		 * aperture so that the gatt size reduces.
146314564Sdim		 */
147285101Semaste		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
148314564Sdim			agp_generic_detach(dev);
149285101Semaste			return ENOMEM;
150314564Sdim		}
151285101Semaste	}
152314564Sdim	sc->gatt = gatt;
153285101Semaste
154314564Sdim	/* Install the gatt. */
155285101Semaste	pci_write_config(dev, AGP_SIS_ATTBASE, gatt->ag_physical, 4);
156314564Sdim
157285101Semaste	/* Enable the aperture. */
158314564Sdim	pci_write_config(dev, AGP_SIS_WINCTRL,
159285101Semaste			 pci_read_config(dev, AGP_SIS_WINCTRL, 1) | 3, 1);
160314564Sdim
161314564Sdim	/*
162285101Semaste	 * Enable the TLB and make it automatically invalidate entries
163314564Sdim	 * when the GATT is written.
164285101Semaste	 */
165314564Sdim	pci_write_config(dev, AGP_SIS_TLBCTRL, 0x05, 1);
166285101Semaste
167314564Sdim	return 0;
168285101Semaste}
169314564Sdim
170285101Semastestatic int
171314564Sdimagp_sis_detach(device_t dev)
172314564Sdim{
173285101Semaste	struct agp_sis_softc *sc = device_get_softc(dev);
174314564Sdim
175314564Sdim	agp_free_cdev(dev);
176314564Sdim
177314564Sdim	/* Disable the aperture.. */
178314564Sdim	pci_write_config(dev, AGP_SIS_WINCTRL,
179314564Sdim			 pci_read_config(dev, AGP_SIS_WINCTRL, 1) & ~3, 1);
180285101Semaste
181285101Semaste	/* and the TLB. */
182296417Sdim	pci_write_config(dev, AGP_SIS_TLBCTRL, 0, 1);
183
184	/* Put the aperture back the way it started. */
185	AGP_SET_APERTURE(dev, sc->initial_aperture);
186
187	agp_free_gatt(sc->gatt);
188	agp_free_res(dev);
189	return 0;
190}
191
192static u_int32_t
193agp_sis_get_aperture(device_t dev)
194{
195	int gws;
196
197	/*
198	 * The aperture size is equal to 4M<<gws.
199	 */
200	gws = (pci_read_config(dev, AGP_SIS_WINCTRL, 1) & 0x70) >> 4;
201	return (4*1024*1024) << gws;
202}
203
204static int
205agp_sis_set_aperture(device_t dev, u_int32_t aperture)
206{
207	int gws;
208
209	/*
210	 * Check for a power of two and make sure its within the
211	 * programmable range.
212	 */
213	if (aperture & (aperture - 1)
214	    || aperture < 4*1024*1024
215	    || aperture > 256*1024*1024)
216		return EINVAL;
217
218	gws = ffs(aperture / 4*1024*1024) - 1;
219
220	pci_write_config(dev, AGP_SIS_WINCTRL,
221			 ((pci_read_config(dev, AGP_SIS_WINCTRL, 1) & ~0x70)
222			  | gws << 4), 1);
223
224	return 0;
225}
226
227static int
228agp_sis_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
229{
230	struct agp_sis_softc *sc = device_get_softc(dev);
231
232	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
233		return EINVAL;
234
235	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
236	return 0;
237}
238
239static int
240agp_sis_unbind_page(device_t dev, vm_offset_t offset)
241{
242	struct agp_sis_softc *sc = device_get_softc(dev);
243
244	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
245		return EINVAL;
246
247	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
248	return 0;
249}
250
251static void
252agp_sis_flush_tlb(device_t dev)
253{
254	pci_write_config(dev, AGP_SIS_TLBFLUSH, 0x02, 1);
255}
256
257static device_method_t agp_sis_methods[] = {
258	/* Device interface */
259	DEVMETHOD(device_probe,		agp_sis_probe),
260	DEVMETHOD(device_attach,	agp_sis_attach),
261	DEVMETHOD(device_detach,	agp_sis_detach),
262	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
263	DEVMETHOD(device_suspend,	bus_generic_suspend),
264	DEVMETHOD(device_resume,	bus_generic_resume),
265
266	/* AGP interface */
267	DEVMETHOD(agp_get_aperture,	agp_sis_get_aperture),
268	DEVMETHOD(agp_set_aperture,	agp_sis_set_aperture),
269	DEVMETHOD(agp_bind_page,	agp_sis_bind_page),
270	DEVMETHOD(agp_unbind_page,	agp_sis_unbind_page),
271	DEVMETHOD(agp_flush_tlb,	agp_sis_flush_tlb),
272	DEVMETHOD(agp_enable,		agp_generic_enable),
273	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
274	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
275	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
276	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
277
278	{ 0, 0 }
279};
280
281static driver_t agp_sis_driver = {
282	"agp",
283	agp_sis_methods,
284	sizeof(struct agp_sis_softc),
285};
286
287static devclass_t agp_devclass;
288
289DRIVER_MODULE(agp_sis, hostb, agp_sis_driver, agp_devclass, 0, 0);
290MODULE_DEPEND(agp_sis, agp, 1, 1, 1);
291MODULE_DEPEND(agp_sis, pci, 1, 1, 1);
292