agp_intel.c revision 1.3.4.2 1 /* $NetBSD: agp_intel.c,v 1.3.4.2 2001/10/01 12:45:51 fvdl Exp $ */
2
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 * $FreeBSD: src/sys/pci/agp_intel.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
29 */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/proc.h>
37 #include <sys/agpio.h>
38 #include <sys/device.h>
39 #include <sys/agpio.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/agpvar.h>
46 #include <dev/pci/agpreg.h>
47
48 #include <machine/bus.h>
49
50 struct agp_intel_softc {
51 u_int32_t initial_aperture; /* aperture size at startup */
52 struct agp_gatt *gatt;
53 };
54
55 static u_int32_t agp_intel_get_aperture(struct agp_softc *);
56 static int agp_intel_set_aperture(struct agp_softc *, u_int32_t);
57 static int agp_intel_bind_page(struct agp_softc *, off_t, bus_addr_t);
58 static int agp_intel_unbind_page(struct agp_softc *, off_t);
59 static void agp_intel_flush_tlb(struct agp_softc *);
60
61 struct agp_methods agp_intel_methods = {
62 agp_intel_get_aperture,
63 agp_intel_set_aperture,
64 agp_intel_bind_page,
65 agp_intel_unbind_page,
66 agp_intel_flush_tlb,
67 agp_generic_enable,
68 agp_generic_alloc_memory,
69 agp_generic_free_memory,
70 agp_generic_bind_memory,
71 agp_generic_unbind_memory,
72 };
73
74 int
75 agp_intel_attach(struct device *parent, struct device *self, void *aux)
76 {
77 struct agp_softc *sc = (struct agp_softc *)self;
78 struct pci_attach_args *pa= aux;
79 struct agp_intel_softc *isc;
80 struct agp_gatt *gatt;
81 pcireg_t reg;
82
83 isc = malloc(sizeof *isc, M_AGP, M_NOWAIT);
84 if (isc == NULL) {
85 printf(": can't allocate chipset-specific softc\n");
86 return ENOMEM;
87 }
88 memset(isc, 0, sizeof *isc);
89
90 sc->as_methods = &agp_intel_methods;
91 sc->as_chipc = isc;
92
93 pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
94 NULL);
95
96 if (agp_map_aperture(pa, sc) != 0) {
97 printf(": can't map aperture\n");
98 free(isc, M_AGP);
99 sc->as_chipc = NULL;
100 return ENXIO;
101 }
102
103 isc->initial_aperture = AGP_GET_APERTURE(sc);
104
105 for (;;) {
106 gatt = agp_alloc_gatt(sc);
107 if (gatt)
108 break;
109
110 /*
111 * Probably contigmalloc failure. Try reducing the
112 * aperture so that the gatt size reduces.
113 */
114 if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
115 agp_generic_detach(sc);
116 printf(": failed to set aperture\n");
117 return ENOMEM;
118 }
119 }
120 isc->gatt = gatt;
121
122 /* Install the gatt. */
123 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE,
124 gatt->ag_physical);
125
126 /* Enable things, clear errors etc. */
127 /* XXXfvdl get rid of the magic constants */
128 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
129 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
130 reg &= ~(1 << 10);
131 reg |= (1 << 9);
132 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
133
134 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_STS);
135 reg &= ~0x00ff0000;
136 reg |= (7 << 16);
137 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_STS, reg);
138
139 return 0;
140 }
141
142 #if 0
143 static int
144 agp_intel_detach(struct agp_softc *sc)
145 {
146 int error;
147 pcireg_t reg;
148 struct agp_intel_softc *isc = sc->as_chipc;
149
150 error = agp_generic_detach(sc);
151 if (error)
152 return error;
153
154 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
155 reg &= ~(1 << 9);
156 printf("%s: set NBXCFG to %x\n", __FUNCTION__, reg);
157 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
158 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE, 0);
159 AGP_SET_APERTURE(sc, isc->initial_aperture);
160 agp_free_gatt(sc, isc->gatt);
161
162 return 0;
163 }
164 #endif
165
166 static u_int32_t
167 agp_intel_get_aperture(struct agp_softc *sc)
168 {
169 u_int32_t apsize;
170
171 apsize = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE) & 0x1f;
172
173 /*
174 * The size is determined by the number of low bits of
175 * register APBASE which are forced to zero. The low 22 bits
176 * are always forced to zero and each zero bit in the apsize
177 * field just read forces the corresponding bit in the 27:22
178 * to be zero. We calculate the aperture size accordingly.
179 */
180 return (((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1;
181 }
182
183 static int
184 agp_intel_set_aperture(struct agp_softc *sc, u_int32_t aperture)
185 {
186 u_int32_t apsize;
187 pcireg_t reg;
188
189 /*
190 * Reverse the magic from get_aperture.
191 */
192 apsize = ((aperture - 1) >> 22) ^ 0x1f;
193
194 /*
195 * Double check for sanity.
196 */
197 if ((((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1 != aperture)
198 return EINVAL;
199
200 reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE);
201 reg = (reg & 0xffffff00) | apsize;
202 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE, reg);
203
204 return 0;
205 }
206
207 static int
208 agp_intel_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
209 {
210 struct agp_intel_softc *isc = sc->as_chipc;
211
212 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
213 return EINVAL;
214
215 isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
216 return 0;
217 }
218
219 static int
220 agp_intel_unbind_page(struct agp_softc *sc, off_t offset)
221 {
222 struct agp_intel_softc *isc = sc->as_chipc;
223
224 if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
225 return EINVAL;
226
227 isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
228 return 0;
229 }
230
231 static void
232 agp_intel_flush_tlb(struct agp_softc *sc)
233 {
234 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2200);
235 pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
236 }
237