xref: /freebsd/sys/dev/agp/agp_ali.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 
39 #include <dev/agp/agppriv.h>
40 #include <dev/agp/agpreg.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
43 
44 #include <vm/vm.h>
45 #include <vm/vm_object.h>
46 #include <vm/pmap.h>
47 
48 struct agp_ali_softc {
49 	struct agp_softc agp;
50 	u_int32_t	initial_aperture; /* aperture size at startup */
51 	struct agp_gatt *gatt;
52 };
53 
54 static const char*
55 agp_ali_match(device_t dev)
56 {
57 	if (pci_get_class(dev) != PCIC_BRIDGE
58 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
59 		return NULL;
60 
61 	if (agp_find_caps(dev) == 0)
62 		return NULL;
63 
64 	switch (pci_get_devid(dev)) {
65 	case 0x167110b9:
66 		return ("Ali M1671 host to AGP bridge");
67 	case 0x154110b9:
68 		return ("Ali M1541 host to AGP bridge");
69 	case 0x162110b9:
70 		return ("Ali M1621 host to AGP bridge");
71 	}
72 
73 	return NULL;
74 }
75 
76 static int
77 agp_ali_probe(device_t dev)
78 {
79 	const char *desc;
80 
81 	if (resource_disabled("agp", device_get_unit(dev)))
82 		return (ENXIO);
83 	desc = agp_ali_match(dev);
84 	if (desc) {
85 		device_set_desc(dev, desc);
86 		return BUS_PROBE_DEFAULT;
87 	}
88 
89 	return ENXIO;
90 }
91 
92 static int
93 agp_ali_attach(device_t dev)
94 {
95 	struct agp_ali_softc *sc = device_get_softc(dev);
96 	struct agp_gatt *gatt;
97 	int error;
98 	u_int32_t attbase;
99 
100 	error = agp_generic_attach(dev);
101 	if (error)
102 		return error;
103 
104 	sc->initial_aperture = AGP_GET_APERTURE(dev);
105 	if (sc->initial_aperture == 0) {
106 		device_printf(dev, "bad initial aperture size, disabling\n");
107 		return ENXIO;
108 	}
109 
110 	for (;;) {
111 		gatt = agp_alloc_gatt(dev);
112 		if (gatt)
113 			break;
114 
115 		/*
116 		 * Probably contigmalloc failure. Try reducing the
117 		 * aperture so that the gatt size reduces.
118 		 */
119 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
120 			agp_generic_detach(dev);
121 			return ENOMEM;
122 		}
123 	}
124 	sc->gatt = gatt;
125 
126 	/* Install the gatt. */
127 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
128 	pci_write_config(dev, AGP_ALI_ATTBASE, gatt->ag_physical |
129 	    (attbase & 0xfff), 4);
130 
131 	/* Enable the TLB. */
132 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
133 
134 	return 0;
135 }
136 
137 static int
138 agp_ali_detach(device_t dev)
139 {
140 	struct agp_ali_softc *sc = device_get_softc(dev);
141 	u_int32_t attbase;
142 
143 	agp_free_cdev(dev);
144 
145 	/* Disable the TLB.. */
146 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
147 
148 	/* Put the aperture back the way it started. */
149 	AGP_SET_APERTURE(dev, sc->initial_aperture);
150 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
151 	pci_write_config(dev, AGP_ALI_ATTBASE, attbase & 0xfff, 4);
152 
153 	agp_free_gatt(sc->gatt);
154 	agp_free_res(dev);
155 	return 0;
156 }
157 
158 #define M 1024*1024
159 
160 static u_int32_t agp_ali_table[] = {
161 	0,			/* 0 - invalid */
162 	1,			/* 1 - invalid */
163 	2,			/* 2 - invalid */
164 	4*M,			/* 3 - invalid */
165 	8*M,			/* 4 - invalid */
166 	0,			/* 5 - invalid */
167 	16*M,			/* 6 - invalid */
168 	32*M,			/* 7 - invalid */
169 	64*M,			/* 8 - invalid */
170 	128*M,			/* 9 - invalid */
171 	256*M,			/* 10 - invalid */
172 };
173 #define	AGP_ALI_TABLE_SIZE nitems(agp_ali_table)
174 
175 static u_int32_t
176 agp_ali_get_aperture(device_t dev)
177 {
178 	/*
179 	 * The aperture size is derived from the low bits of attbase.
180 	 * I'm not sure this is correct..
181 	 */
182 	int i = pci_read_config(dev, AGP_ALI_ATTBASE, 4) & 0xf;
183 	if (i >= AGP_ALI_TABLE_SIZE)
184 		return 0;
185 	return agp_ali_table[i];
186 }
187 
188 static int
189 agp_ali_set_aperture(device_t dev, u_int32_t aperture)
190 {
191 	int i;
192 	u_int32_t attbase;
193 
194 	for (i = 0; i < AGP_ALI_TABLE_SIZE; i++)
195 		if (agp_ali_table[i] == aperture)
196 			break;
197 	if (i == AGP_ALI_TABLE_SIZE)
198 		return EINVAL;
199 
200 	attbase = pci_read_config(dev, AGP_ALI_ATTBASE, 4);
201 	pci_write_config(dev, AGP_ALI_ATTBASE, (attbase & ~0xf) | i, 4);
202 	return 0;
203 }
204 
205 static int
206 agp_ali_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
207 {
208 	struct agp_ali_softc *sc = device_get_softc(dev);
209 
210 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
211 		return EINVAL;
212 
213 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
214 	return 0;
215 }
216 
217 static int
218 agp_ali_unbind_page(device_t dev, vm_offset_t offset)
219 {
220 	struct agp_ali_softc *sc = device_get_softc(dev);
221 
222 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
223 		return EINVAL;
224 
225 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
226 	return 0;
227 }
228 
229 static void
230 agp_ali_flush_tlb(device_t dev)
231 {
232 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x90, 1);
233 	pci_write_config(dev, AGP_ALI_TLBCTRL, 0x10, 1);
234 }
235 
236 static device_method_t agp_ali_methods[] = {
237 	/* Device interface */
238 	DEVMETHOD(device_probe,		agp_ali_probe),
239 	DEVMETHOD(device_attach,	agp_ali_attach),
240 	DEVMETHOD(device_detach,	agp_ali_detach),
241 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
242 	DEVMETHOD(device_suspend,	bus_generic_suspend),
243 	DEVMETHOD(device_resume,	bus_generic_resume),
244 
245 	/* AGP interface */
246 	DEVMETHOD(agp_get_aperture,	agp_ali_get_aperture),
247 	DEVMETHOD(agp_set_aperture,	agp_ali_set_aperture),
248 	DEVMETHOD(agp_bind_page,	agp_ali_bind_page),
249 	DEVMETHOD(agp_unbind_page,	agp_ali_unbind_page),
250 	DEVMETHOD(agp_flush_tlb,	agp_ali_flush_tlb),
251 	DEVMETHOD(agp_enable,		agp_generic_enable),
252 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
253 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
254 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
255 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
256 	{ 0, 0 }
257 };
258 
259 static driver_t agp_ali_driver = {
260 	"agp",
261 	agp_ali_methods,
262 	sizeof(struct agp_ali_softc),
263 };
264 
265 DRIVER_MODULE(agp_ali, hostb, agp_ali_driver, 0, 0);
266 MODULE_DEPEND(agp_ali, agp, 1, 1, 1);
267 MODULE_DEPEND(agp_ali, pci, 1, 1, 1);
268