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