xref: /freebsd/sys/dev/agp/agp_apple.c (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Nathan Whitehorn
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
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 <machine/resource.h>
43 
44 #include <dev/agp/agppriv.h>
45 #include <dev/agp/agpreg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_object.h>
51 #include <vm/pmap.h>
52 
53 #define UNIN_AGP_GART_BASE	0x8c
54 #define UNIN_AGP_BASE_ADDR	0x90
55 #define UNIN_AGP_GART_CONTROL	0x94
56 
57 #define UNIN_AGP_GART_INVAL	0x00000001
58 #define UNIN_AGP_GART_ENABLE	0x00000100
59 #define UNIN_AGP_GART_2XRESET	0x00010000
60 #define UNIN_AGP_U3_GART_PERFRD	0x00080000
61 
62 struct agp_apple_softc {
63 	struct agp_softc agp;
64 	uint32_t	aperture;
65 	struct agp_gatt *gatt;
66 	int		u3;
67 	int		needs_2x_reset;
68 };
69 
70 static int
71 agp_apple_probe(device_t dev)
72 {
73 
74 	if (resource_disabled("agp", device_get_unit(dev)))
75 		return (ENXIO);
76 
77 	if (pci_get_class(dev) != PCIC_BRIDGE
78 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
79 		return (ENXIO);
80 
81 	if (agp_find_caps(dev) == 0)
82 		return (ENXIO);
83 
84 	if (pci_get_class(dev) != PCIC_BRIDGE
85 	    || pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
86 		return (ENXIO);
87 
88 	switch (pci_get_devid(dev)) {
89 	case 0x0020106b:
90 	case 0x0027106b:
91 		device_set_desc(dev, "Apple UniNorth AGP Bridge");
92 		return (BUS_PROBE_DEFAULT);
93 	case 0x002d106b:
94 		device_set_desc(dev, "Apple UniNorth 1.5 AGP Bridge");
95 		return (BUS_PROBE_DEFAULT);
96 	case 0x0034106b:
97 		device_set_desc(dev, "Apple UniNorth 2 AGP Bridge");
98 		return (BUS_PROBE_DEFAULT);
99 	case 0x004b106b:
100 	case 0x0058106b:
101 	case 0x0059106b:
102 		device_set_desc(dev, "Apple U3 AGP Bridge");
103 		return (BUS_PROBE_DEFAULT);
104 	case 0x0066106b:
105 		device_set_desc(dev, "Apple Intrepid AGP Bridge");
106 		return (BUS_PROBE_DEFAULT);
107 	}
108 
109 	return (ENXIO);
110 }
111 
112 static int
113 agp_apple_attach(device_t dev)
114 {
115 	struct agp_apple_softc *sc = device_get_softc(dev);
116 	int error;
117 
118 	/* Record quirks */
119 	sc->needs_2x_reset = 0;
120 	sc->u3 = 0;
121 	switch (pci_get_devid(dev)) {
122 	case 0x0020106b:
123 	case 0x0027106b:
124 		sc->needs_2x_reset = 1;
125 		break;
126 	case 0x004b106b:
127 	case 0x0058106b:
128 	case 0x0059106b:
129 		sc->u3 = 1;
130 		break;
131 	}
132 
133 	/* Set the aperture bus address base (must be 0) */
134 	pci_write_config(dev, UNIN_AGP_BASE_ADDR, 0, 4);
135 	agp_set_aperture_resource(dev, -1);
136 
137 	error = agp_generic_attach(dev);
138 	if (error)
139 		return (error);
140 
141 	sc->aperture = 256*1024*1024;
142 
143 	for (sc->aperture = 256*1024*1024; sc->aperture >= 4*1024*1024;
144 	    sc->aperture /= 2) {
145 		sc->gatt = agp_alloc_gatt(dev);
146 		if (sc->gatt)
147 			break;
148 	}
149 	if (sc->aperture < 4*1024*1024) {
150 		agp_generic_detach(dev);
151 		return ENOMEM;
152 	}
153 
154 	/* Install the gatt. */
155 	AGP_SET_APERTURE(dev, sc->aperture);
156 
157 	/* XXX: U3 scratch page? */
158 
159 	/* Enable the aperture and TLB. */
160 	AGP_FLUSH_TLB(dev);
161 
162 	return (0);
163 }
164 
165 static int
166 agp_apple_detach(device_t dev)
167 {
168 	struct agp_apple_softc *sc = device_get_softc(dev);
169 
170 	agp_free_cdev(dev);
171 
172 	/* Disable the aperture and TLB */
173 	pci_write_config(dev, UNIN_AGP_GART_CONTROL, UNIN_AGP_GART_INVAL, 4);
174 	pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
175 
176 	if (sc->needs_2x_reset) {
177 		pci_write_config(dev, UNIN_AGP_GART_CONTROL,
178 		    UNIN_AGP_GART_2XRESET, 4);
179 		pci_write_config(dev, UNIN_AGP_GART_CONTROL, 0, 4);
180 	}
181 
182 	AGP_SET_APERTURE(dev, 0);
183 
184 	agp_free_gatt(sc->gatt);
185 	agp_free_res(dev);
186 	return 0;
187 }
188 
189 static uint32_t
190 agp_apple_get_aperture(device_t dev)
191 {
192 	struct agp_apple_softc *sc = device_get_softc(dev);
193 
194 	return (sc->aperture);
195 }
196 
197 static int
198 agp_apple_set_aperture(device_t dev, uint32_t aperture)
199 {
200 	struct agp_apple_softc *sc = device_get_softc(dev);
201 
202 	/*
203 	 * Check for a multiple of 4 MB and make sure it is within the
204 	 * programmable range.
205 	 */
206 	if (aperture % (4*1024*1024)
207 	    || aperture < 4*1024*1024
208 	    || aperture > ((sc->u3) ? 512 : 256)*1024*1024)
209 		return EINVAL;
210 
211 	/* The aperture value is a multiple of 4 MB */
212 	aperture /= (4*1024*1024);
213 
214 	pci_write_config(dev, UNIN_AGP_GART_BASE,
215 	    (sc->gatt->ag_physical & 0xfffff000) | aperture, 4);
216 
217 	return (0);
218 }
219 
220 static int
221 agp_apple_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
222 {
223 	struct agp_apple_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] = physical;
229 	return (0);
230 }
231 
232 static int
233 agp_apple_unbind_page(device_t dev, vm_offset_t offset)
234 {
235 	struct agp_apple_softc *sc = device_get_softc(dev);
236 
237 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
238 		return EINVAL;
239 
240 	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
241 	return (0);
242 }
243 
244 static void
245 agp_apple_flush_tlb(device_t dev)
246 {
247 	struct agp_apple_softc *sc = device_get_softc(dev);
248 	uint32_t cntrl = UNIN_AGP_GART_ENABLE;
249 
250 	if (sc->u3)
251 		cntrl |= UNIN_AGP_U3_GART_PERFRD;
252 
253 	pci_write_config(dev, UNIN_AGP_GART_CONTROL,
254 	    cntrl | UNIN_AGP_GART_INVAL, 4);
255 	pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
256 
257 	if (sc->needs_2x_reset) {
258 		pci_write_config(dev, UNIN_AGP_GART_CONTROL,
259 		    cntrl | UNIN_AGP_GART_2XRESET, 4);
260 		pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
261 	}
262 }
263 
264 static device_method_t agp_apple_methods[] = {
265 	/* Device interface */
266 	DEVMETHOD(device_probe,		agp_apple_probe),
267 	DEVMETHOD(device_attach,	agp_apple_attach),
268 	DEVMETHOD(device_detach,	agp_apple_detach),
269 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
270 	DEVMETHOD(device_suspend,	bus_generic_suspend),
271 	DEVMETHOD(device_resume,	bus_generic_resume),
272 
273 	/* AGP interface */
274 	DEVMETHOD(agp_get_aperture,	agp_apple_get_aperture),
275 	DEVMETHOD(agp_set_aperture,	agp_apple_set_aperture),
276 	DEVMETHOD(agp_bind_page,	agp_apple_bind_page),
277 	DEVMETHOD(agp_unbind_page,	agp_apple_unbind_page),
278 	DEVMETHOD(agp_flush_tlb,	agp_apple_flush_tlb),
279 	DEVMETHOD(agp_enable,		agp_generic_enable),
280 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
281 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
282 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
283 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
284 	{ 0, 0 }
285 };
286 
287 static driver_t agp_apple_driver = {
288 	"agp",
289 	agp_apple_methods,
290 	sizeof(struct agp_apple_softc),
291 };
292 
293 DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, 0, 0);
294 MODULE_DEPEND(agp_apple, agp, 1, 1, 1);
295 MODULE_DEPEND(agp_apple, pci, 1, 1, 1);
296