1/*-
2 * Copyright (c) 2010 Nathan Whitehorn
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 <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
62struct 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
70static int
71agp_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
112static int
113agp_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
165static int
166agp_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
189static uint32_t
190agp_apple_get_aperture(device_t dev)
191{
192	struct agp_apple_softc *sc = device_get_softc(dev);
193
194	return (sc->aperture);
195}
196
197static int
198agp_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
220static int
221agp_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	__asm __volatile("dcbst 0,%0; sync" ::
230	    "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory");
231	return (0);
232}
233
234static int
235agp_apple_unbind_page(device_t dev, vm_offset_t offset)
236{
237	struct agp_apple_softc *sc = device_get_softc(dev);
238
239	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
240		return EINVAL;
241
242	sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
243	__asm __volatile("dcbst 0,%0; sync" ::
244	    "r"(&sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT]) : "memory");
245	return (0);
246}
247
248static void
249agp_apple_flush_tlb(device_t dev)
250{
251	struct agp_apple_softc *sc = device_get_softc(dev);
252	uint32_t cntrl = UNIN_AGP_GART_ENABLE;
253
254	if (sc->u3)
255		cntrl |= UNIN_AGP_U3_GART_PERFRD;
256
257	pci_write_config(dev, UNIN_AGP_GART_CONTROL,
258	    cntrl | UNIN_AGP_GART_INVAL, 4);
259	pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
260
261	if (sc->needs_2x_reset) {
262		pci_write_config(dev, UNIN_AGP_GART_CONTROL,
263		    cntrl | UNIN_AGP_GART_2XRESET, 4);
264		pci_write_config(dev, UNIN_AGP_GART_CONTROL, cntrl, 4);
265	}
266}
267
268static device_method_t agp_apple_methods[] = {
269	/* Device interface */
270	DEVMETHOD(device_probe,		agp_apple_probe),
271	DEVMETHOD(device_attach,	agp_apple_attach),
272	DEVMETHOD(device_detach,	agp_apple_detach),
273	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
274	DEVMETHOD(device_suspend,	bus_generic_suspend),
275	DEVMETHOD(device_resume,	bus_generic_resume),
276
277	/* AGP interface */
278	DEVMETHOD(agp_get_aperture,	agp_apple_get_aperture),
279	DEVMETHOD(agp_set_aperture,	agp_apple_set_aperture),
280	DEVMETHOD(agp_bind_page,	agp_apple_bind_page),
281	DEVMETHOD(agp_unbind_page,	agp_apple_unbind_page),
282	DEVMETHOD(agp_flush_tlb,	agp_apple_flush_tlb),
283	DEVMETHOD(agp_enable,		agp_generic_enable),
284	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
285	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
286	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
287	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
288
289	{ 0, 0 }
290};
291
292static driver_t agp_apple_driver = {
293	"agp",
294	agp_apple_methods,
295	sizeof(struct agp_apple_softc),
296};
297
298static devclass_t agp_devclass;
299
300DRIVER_MODULE(agp_apple, hostb, agp_apple_driver, agp_devclass, 0, 0);
301MODULE_DEPEND(agp_apple, agp, 1, 1, 1);
302MODULE_DEPEND(agp_apple, pci, 1, 1, 1);
303