agp_apple.c revision 1.2.2.1 1 /* $NetBSD: agp_apple.c,v 1.2.2.1 2008/02/18 21:05:55 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
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 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: agp_apple.c,v 1.2.2.1 2008/02/18 21:05:55 mjf Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/agpio.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/agpvar.h>
48 #include <dev/pci/agpreg.h>
49
50 #include <sys/bus.h>
51
52 static u_int32_t agp_apple_get_aperture(struct agp_softc *);
53 static int agp_apple_set_aperture(struct agp_softc *, u_int32_t);
54 static int agp_apple_bind_page(struct agp_softc *, off_t, bus_addr_t);
55 static int agp_apple_unbind_page(struct agp_softc *, off_t);
56 static void agp_apple_flush_tlb(struct agp_softc *);
57
58 static struct agp_methods agp_apple_methods = {
59 agp_apple_get_aperture,
60 agp_apple_set_aperture,
61 agp_apple_bind_page,
62 agp_apple_unbind_page,
63 agp_apple_flush_tlb,
64 agp_generic_enable,
65 agp_generic_alloc_memory,
66 agp_generic_free_memory,
67 agp_generic_bind_memory,
68 agp_generic_unbind_memory,
69 };
70
71 struct agp_apple_softc {
72 u_int32_t initial_aperture; /* aperture size at startup */
73 struct agp_gatt *gatt;
74 };
75
76 int
77 agp_apple_attach(struct device *parent, struct device *self, void *aux)
78 {
79 struct pci_attach_args *pa = aux;
80 struct agp_softc *sc = (void *)self;
81 struct agp_apple_softc *asc;
82 struct agp_gatt *gatt;
83
84 asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
85 if (asc == NULL) {
86 aprint_error(": can't allocate chipset-specific softc\n");
87 return ENOMEM;
88 }
89 sc->as_chipc = asc;
90 sc->as_methods = &agp_apple_methods;
91 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
92 NULL);
93
94 sc->as_apaddr = 0;
95 sc->as_apsize = 8 * 1024 * 1024;
96 sc->as_apflags = 0;
97 sc->as_pc = pa->pa_pc;
98 sc->as_tag = pa->pa_tag;
99 sc->as_apt = pa->pa_memt;
100
101 asc->initial_aperture = sc->as_apsize;
102
103 for (;;) {
104 gatt = agp_alloc_gatt(sc);
105 if (gatt)
106 break;
107 sc->as_apsize = sc->as_apsize >> 1;
108 if (sc->as_apsize == 0) {
109 aprint_error(": can't set aperture size\n");
110 return ENOMEM;
111 }
112 }
113 asc->gatt = gatt;
114
115 /* Install the gatt. */
116 aprint_error("gatt: %08x %d MB\n", gatt->ag_physical, sc->as_apsize >> 20);
117 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_BASE,
118 (gatt->ag_physical & 0xfffff000) |
119 (sc->as_apsize >> 22));
120
121 /* Enable the aperture. */
122 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
123 APPLE_GART_EN);
124 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
125 APPLE_GART_EN | APPLE_GART_INV);
126 pci_conf_write(pa->pa_pc, pa->pa_tag, APPLE_UNINORTH_GART_CTRL,
127 APPLE_GART_EN);
128 return 0;
129 }
130
131 static u_int32_t
132 agp_apple_get_aperture(struct agp_softc *sc)
133 {
134 #if 0
135 u_int32_t apsize = 0;
136
137 aprint_error("%s: ", __func__);
138 apsize = pci_conf_read(sc->as_pc, sc->as_tag,
139 APPLE_UNINORTH_GART_BASE) & 0x0000ffff;
140 aprint_error("%08x\n", apsize);
141 return (apsize << 22);
142 #else
143 return sc->as_apsize;
144 #endif
145 }
146
147 static int
148 agp_apple_set_aperture(struct agp_softc *sc, u_int32_t aperture)
149 {
150 pcireg_t reg;
151
152 aprint_error("%s: %08x\n", __func__, aperture);
153 reg = pci_conf_read(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE)
154 & 0xfffff000;
155 reg |= ((aperture >> 22) & 0xfff);
156 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_BASE, reg);
157 sc->as_apsize = aperture;
158 return 0;
159 }
160
161 static int
162 agp_apple_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
163 {
164 struct agp_apple_softc *asc = sc->as_chipc;
165
166 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
167 return EINVAL;
168
169 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = htole32(physical);
170 return 0;
171 }
172
173 static int
174 agp_apple_unbind_page(struct agp_softc *sc, off_t offset)
175 {
176 struct agp_apple_softc *asc = sc->as_chipc;
177
178 if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
179 return EINVAL;
180
181 asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
182 return 0;
183 }
184
185 static void
186 agp_apple_flush_tlb(struct agp_softc *sc)
187 {
188
189 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
190 APPLE_GART_EN);
191 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
192 APPLE_GART_EN | APPLE_GART_INV);
193 pci_conf_write(sc->as_pc, sc->as_tag, APPLE_UNINORTH_GART_CTRL,
194 APPLE_GART_EN);
195 }
196