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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include "opt_bus.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/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
51struct agp_ali_softc {
52	struct agp_softc agp;
53	u_int32_t	initial_aperture; /* aperture size at startup */
54	struct agp_gatt *gatt;
55};
56
57static const char*
58agp_ali_match(device_t dev)
59{
60	if (pci_get_class(dev) != PCIC_BRIDGE
61	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
62		return NULL;
63
64	if (agp_find_caps(dev) == 0)
65		return NULL;
66
67	switch (pci_get_devid(dev)) {
68	case 0x167110b9:
69		return ("Ali M1671 host to AGP bridge");
70	case 0x154110b9:
71		return ("Ali M1541 host to AGP bridge");
72	case 0x162110b9:
73		return ("Ali M1621 host to AGP bridge");
74	};
75
76	return NULL;
77}
78
79static int
80agp_ali_probe(device_t dev)
81{
82	const char *desc;
83
84	if (resource_disabled("agp", device_get_unit(dev)))
85		return (ENXIO);
86	desc = agp_ali_match(dev);
87	if (desc) {
88		device_set_desc(dev, desc);
89		return BUS_PROBE_DEFAULT;
90	}
91
92	return ENXIO;
93}
94
95static int
96agp_ali_attach(device_t dev)
97{
98	struct agp_ali_softc *sc = device_get_softc(dev);
99	struct agp_gatt *gatt;
100	int error;
101	u_int32_t attbase;
102
103	error = agp_generic_attach(dev);
104	if (error)
105		return error;
106
107	sc->initial_aperture = AGP_GET_APERTURE(dev);
108	if (sc->initial_aperture == 0) {
109		device_printf(dev, "bad initial aperture size, disabling\n");
110		return ENXIO;
111	}
112
113	for (;;) {
114		gatt = agp_alloc_gatt(dev);
115		if (gatt)
116			break;
117
118		/*
119		 * Probably contigmalloc failure. Try reducing the
120		 * aperture so that the gatt size reduces.
121		 */
122		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
123			agp_generic_detach(dev);
124			return ENOMEM;
125		}
126	}
127	sc->gatt = gatt;
128
129	/* Install the gatt. */
130	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
131	pci_write_config(dev, AGP_ALI_ATTBASE, gatt->ag_physical |
132	    (attbase & 0xfff), 4);
133
134	/* Enable the TLB. */
135	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
136
137	return 0;
138}
139
140static int
141agp_ali_detach(device_t dev)
142{
143	struct agp_ali_softc *sc = device_get_softc(dev);
144	u_int32_t attbase;
145
146	agp_free_cdev(dev);
147
148	/* Disable the TLB.. */
149	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
150
151	/* Put the aperture back the way it started. */
152	AGP_SET_APERTURE(dev, sc->initial_aperture);
153	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
154	pci_write_config(dev, AGP_ALI_ATTBASE, attbase & 0xfff, 4);
155
156	agp_free_gatt(sc->gatt);
157	agp_free_res(dev);
158	return 0;
159}
160
161#define M 1024*1024
162
163static u_int32_t agp_ali_table[] = {
164	0,			/* 0 - invalid */
165	1,			/* 1 - invalid */
166	2,			/* 2 - invalid */
167	4*M,			/* 3 - invalid */
168	8*M,			/* 4 - invalid */
169	0,			/* 5 - invalid */
170	16*M,			/* 6 - invalid */
171	32*M,			/* 7 - invalid */
172	64*M,			/* 8 - invalid */
173	128*M,			/* 9 - invalid */
174	256*M,			/* 10 - invalid */
175};
176#define agp_ali_table_size (sizeof(agp_ali_table) / sizeof(agp_ali_table[0]))
177
178static u_int32_t
179agp_ali_get_aperture(device_t dev)
180{
181	/*
182	 * The aperture size is derived from the low bits of attbase.
183	 * I'm not sure this is correct..
184	 */
185	int i = pci_read_config(dev, AGP_ALI_ATTBASE, 4) & 0xf;
186	if (i >= agp_ali_table_size)
187		return 0;
188	return agp_ali_table[i];
189}
190
191static int
192agp_ali_set_aperture(device_t dev, u_int32_t aperture)
193{
194	int i;
195	u_int32_t attbase;
196
197	for (i = 0; i < agp_ali_table_size; i++)
198		if (agp_ali_table[i] == aperture)
199			break;
200	if (i == agp_ali_table_size)
201		return EINVAL;
202
203	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
204	pci_write_config(dev, AGP_ALI_ATTBASE, (attbase & ~0xf) | i, 4);
205	return 0;
206}
207
208static int
209agp_ali_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
210{
211	struct agp_ali_softc *sc = device_get_softc(dev);
212
213	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
214		return EINVAL;
215
216	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
217	return 0;
218}
219
220static int
221agp_ali_unbind_page(device_t dev, vm_offset_t offset)
222{
223	struct agp_ali_softc *sc = device_get_softc(dev);
224
225	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
226		return EINVAL;
227
228	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
229	return 0;
230}
231
232static void
233agp_ali_flush_tlb(device_t dev)
234{
235	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
236	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
237}
238
239static device_method_t agp_ali_methods[] = {
240	/* Device interface */
241	DEVMETHOD(device_probe,		agp_ali_probe),
242	DEVMETHOD(device_attach,	agp_ali_attach),
243	DEVMETHOD(device_detach,	agp_ali_detach),
244	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
245	DEVMETHOD(device_suspend,	bus_generic_suspend),
246	DEVMETHOD(device_resume,	bus_generic_resume),
247
248	/* AGP interface */
249	DEVMETHOD(agp_get_aperture,	agp_ali_get_aperture),
250	DEVMETHOD(agp_set_aperture,	agp_ali_set_aperture),
251	DEVMETHOD(agp_bind_page,	agp_ali_bind_page),
252	DEVMETHOD(agp_unbind_page,	agp_ali_unbind_page),
253	DEVMETHOD(agp_flush_tlb,	agp_ali_flush_tlb),
254	DEVMETHOD(agp_enable,		agp_generic_enable),
255	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
256	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
257	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
258	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
259
260	{ 0, 0 }
261};
262
263static driver_t agp_ali_driver = {
264	"agp",
265	agp_ali_methods,
266	sizeof(struct agp_ali_softc),
267};
268
269static devclass_t agp_devclass;
270
271DRIVER_MODULE(agp_ali, hostb, agp_ali_driver, agp_devclass, 0, 0);
272MODULE_DEPEND(agp_ali, agp, 1, 1, 1);
273MODULE_DEPEND(agp_ali, pci, 1, 1, 1);
274