agp_intel.c revision 1.3.2.4 1 1.3.2.4 nathanw /* $NetBSD: agp_intel.c,v 1.3.2.4 2002/02/28 04:13:57 nathanw Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*-
4 1.3.2.2 nathanw * Copyright (c) 2000 Doug Rabson
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.3.2.2 nathanw * are met:
10 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.3.2.2 nathanw *
16 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.3.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.3.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.3.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.3.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.3.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.3.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.3.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.3.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.2.2 nathanw * SUCH DAMAGE.
27 1.3.2.2 nathanw *
28 1.3.2.2 nathanw * $FreeBSD: src/sys/pci/agp_intel.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
29 1.3.2.2 nathanw */
30 1.3.2.2 nathanw
31 1.3.2.3 nathanw #include <sys/cdefs.h>
32 1.3.2.4 nathanw __KERNEL_RCSID(0, "$NetBSD: agp_intel.c,v 1.3.2.4 2002/02/28 04:13:57 nathanw Exp $");
33 1.3.2.3 nathanw
34 1.3.2.2 nathanw #include <sys/param.h>
35 1.3.2.2 nathanw #include <sys/systm.h>
36 1.3.2.2 nathanw #include <sys/malloc.h>
37 1.3.2.2 nathanw #include <sys/kernel.h>
38 1.3.2.2 nathanw #include <sys/lock.h>
39 1.3.2.2 nathanw #include <sys/proc.h>
40 1.3.2.2 nathanw #include <sys/agpio.h>
41 1.3.2.2 nathanw #include <sys/device.h>
42 1.3.2.2 nathanw #include <sys/agpio.h>
43 1.3.2.2 nathanw
44 1.3.2.2 nathanw #include <uvm/uvm_extern.h>
45 1.3.2.2 nathanw
46 1.3.2.2 nathanw #include <dev/pci/pcivar.h>
47 1.3.2.2 nathanw #include <dev/pci/pcireg.h>
48 1.3.2.2 nathanw #include <dev/pci/agpvar.h>
49 1.3.2.2 nathanw #include <dev/pci/agpreg.h>
50 1.3.2.2 nathanw
51 1.3.2.2 nathanw #include <machine/bus.h>
52 1.3.2.2 nathanw
53 1.3.2.2 nathanw struct agp_intel_softc {
54 1.3.2.2 nathanw u_int32_t initial_aperture; /* aperture size at startup */
55 1.3.2.2 nathanw struct agp_gatt *gatt;
56 1.3.2.2 nathanw };
57 1.3.2.2 nathanw
58 1.3.2.2 nathanw static u_int32_t agp_intel_get_aperture(struct agp_softc *);
59 1.3.2.2 nathanw static int agp_intel_set_aperture(struct agp_softc *, u_int32_t);
60 1.3.2.2 nathanw static int agp_intel_bind_page(struct agp_softc *, off_t, bus_addr_t);
61 1.3.2.2 nathanw static int agp_intel_unbind_page(struct agp_softc *, off_t);
62 1.3.2.2 nathanw static void agp_intel_flush_tlb(struct agp_softc *);
63 1.3.2.2 nathanw
64 1.3.2.2 nathanw struct agp_methods agp_intel_methods = {
65 1.3.2.2 nathanw agp_intel_get_aperture,
66 1.3.2.2 nathanw agp_intel_set_aperture,
67 1.3.2.2 nathanw agp_intel_bind_page,
68 1.3.2.2 nathanw agp_intel_unbind_page,
69 1.3.2.2 nathanw agp_intel_flush_tlb,
70 1.3.2.2 nathanw agp_generic_enable,
71 1.3.2.2 nathanw agp_generic_alloc_memory,
72 1.3.2.2 nathanw agp_generic_free_memory,
73 1.3.2.2 nathanw agp_generic_bind_memory,
74 1.3.2.2 nathanw agp_generic_unbind_memory,
75 1.3.2.2 nathanw };
76 1.3.2.2 nathanw
77 1.3.2.2 nathanw int
78 1.3.2.2 nathanw agp_intel_attach(struct device *parent, struct device *self, void *aux)
79 1.3.2.2 nathanw {
80 1.3.2.2 nathanw struct agp_softc *sc = (struct agp_softc *)self;
81 1.3.2.2 nathanw struct pci_attach_args *pa= aux;
82 1.3.2.2 nathanw struct agp_intel_softc *isc;
83 1.3.2.2 nathanw struct agp_gatt *gatt;
84 1.3.2.2 nathanw pcireg_t reg;
85 1.3.2.2 nathanw
86 1.3.2.4 nathanw isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
87 1.3.2.2 nathanw if (isc == NULL) {
88 1.3.2.2 nathanw printf(": can't allocate chipset-specific softc\n");
89 1.3.2.2 nathanw return ENOMEM;
90 1.3.2.2 nathanw }
91 1.3.2.2 nathanw
92 1.3.2.2 nathanw sc->as_methods = &agp_intel_methods;
93 1.3.2.2 nathanw sc->as_chipc = isc;
94 1.3.2.2 nathanw
95 1.3.2.2 nathanw pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
96 1.3.2.2 nathanw NULL);
97 1.3.2.2 nathanw
98 1.3.2.2 nathanw if (agp_map_aperture(pa, sc) != 0) {
99 1.3.2.2 nathanw printf(": can't map aperture\n");
100 1.3.2.2 nathanw free(isc, M_AGP);
101 1.3.2.2 nathanw sc->as_chipc = NULL;
102 1.3.2.2 nathanw return ENXIO;
103 1.3.2.2 nathanw }
104 1.3.2.2 nathanw
105 1.3.2.2 nathanw isc->initial_aperture = AGP_GET_APERTURE(sc);
106 1.3.2.2 nathanw
107 1.3.2.2 nathanw for (;;) {
108 1.3.2.2 nathanw gatt = agp_alloc_gatt(sc);
109 1.3.2.2 nathanw if (gatt)
110 1.3.2.2 nathanw break;
111 1.3.2.2 nathanw
112 1.3.2.2 nathanw /*
113 1.3.2.2 nathanw * Probably contigmalloc failure. Try reducing the
114 1.3.2.2 nathanw * aperture so that the gatt size reduces.
115 1.3.2.2 nathanw */
116 1.3.2.2 nathanw if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
117 1.3.2.2 nathanw agp_generic_detach(sc);
118 1.3.2.2 nathanw printf(": failed to set aperture\n");
119 1.3.2.2 nathanw return ENOMEM;
120 1.3.2.2 nathanw }
121 1.3.2.2 nathanw }
122 1.3.2.2 nathanw isc->gatt = gatt;
123 1.3.2.2 nathanw
124 1.3.2.2 nathanw /* Install the gatt. */
125 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE,
126 1.3.2.2 nathanw gatt->ag_physical);
127 1.3.2.2 nathanw
128 1.3.2.2 nathanw /* Enable things, clear errors etc. */
129 1.3.2.2 nathanw /* XXXfvdl get rid of the magic constants */
130 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
131 1.3.2.2 nathanw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
132 1.3.2.2 nathanw reg &= ~(1 << 10);
133 1.3.2.2 nathanw reg |= (1 << 9);
134 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
135 1.3.2.2 nathanw
136 1.3.2.2 nathanw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_STS);
137 1.3.2.2 nathanw reg &= ~0x00ff0000;
138 1.3.2.2 nathanw reg |= (7 << 16);
139 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_STS, reg);
140 1.3.2.2 nathanw
141 1.3.2.2 nathanw return 0;
142 1.3.2.2 nathanw }
143 1.3.2.2 nathanw
144 1.3.2.2 nathanw #if 0
145 1.3.2.2 nathanw static int
146 1.3.2.2 nathanw agp_intel_detach(struct agp_softc *sc)
147 1.3.2.2 nathanw {
148 1.3.2.2 nathanw int error;
149 1.3.2.2 nathanw pcireg_t reg;
150 1.3.2.2 nathanw struct agp_intel_softc *isc = sc->as_chipc;
151 1.3.2.2 nathanw
152 1.3.2.2 nathanw error = agp_generic_detach(sc);
153 1.3.2.2 nathanw if (error)
154 1.3.2.2 nathanw return error;
155 1.3.2.2 nathanw
156 1.3.2.2 nathanw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG);
157 1.3.2.2 nathanw reg &= ~(1 << 9);
158 1.3.2.2 nathanw printf("%s: set NBXCFG to %x\n", __FUNCTION__, reg);
159 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_NBXCFG, reg);
160 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_ATTBASE, 0);
161 1.3.2.2 nathanw AGP_SET_APERTURE(sc, isc->initial_aperture);
162 1.3.2.2 nathanw agp_free_gatt(sc, isc->gatt);
163 1.3.2.2 nathanw
164 1.3.2.2 nathanw return 0;
165 1.3.2.2 nathanw }
166 1.3.2.2 nathanw #endif
167 1.3.2.2 nathanw
168 1.3.2.2 nathanw static u_int32_t
169 1.3.2.2 nathanw agp_intel_get_aperture(struct agp_softc *sc)
170 1.3.2.2 nathanw {
171 1.3.2.2 nathanw u_int32_t apsize;
172 1.3.2.2 nathanw
173 1.3.2.2 nathanw apsize = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE) & 0x1f;
174 1.3.2.2 nathanw
175 1.3.2.2 nathanw /*
176 1.3.2.2 nathanw * The size is determined by the number of low bits of
177 1.3.2.2 nathanw * register APBASE which are forced to zero. The low 22 bits
178 1.3.2.2 nathanw * are always forced to zero and each zero bit in the apsize
179 1.3.2.2 nathanw * field just read forces the corresponding bit in the 27:22
180 1.3.2.2 nathanw * to be zero. We calculate the aperture size accordingly.
181 1.3.2.2 nathanw */
182 1.3.2.2 nathanw return (((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1;
183 1.3.2.2 nathanw }
184 1.3.2.2 nathanw
185 1.3.2.2 nathanw static int
186 1.3.2.2 nathanw agp_intel_set_aperture(struct agp_softc *sc, u_int32_t aperture)
187 1.3.2.2 nathanw {
188 1.3.2.2 nathanw u_int32_t apsize;
189 1.3.2.2 nathanw pcireg_t reg;
190 1.3.2.2 nathanw
191 1.3.2.2 nathanw /*
192 1.3.2.2 nathanw * Reverse the magic from get_aperture.
193 1.3.2.2 nathanw */
194 1.3.2.2 nathanw apsize = ((aperture - 1) >> 22) ^ 0x1f;
195 1.3.2.2 nathanw
196 1.3.2.2 nathanw /*
197 1.3.2.2 nathanw * Double check for sanity.
198 1.3.2.2 nathanw */
199 1.3.2.2 nathanw if ((((apsize ^ 0x1f) << 22) | ((1 << 22) - 1)) + 1 != aperture)
200 1.3.2.2 nathanw return EINVAL;
201 1.3.2.2 nathanw
202 1.3.2.2 nathanw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE);
203 1.3.2.2 nathanw reg = (reg & 0xffffff00) | apsize;
204 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_APSIZE, reg);
205 1.3.2.2 nathanw
206 1.3.2.2 nathanw return 0;
207 1.3.2.2 nathanw }
208 1.3.2.2 nathanw
209 1.3.2.2 nathanw static int
210 1.3.2.2 nathanw agp_intel_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
211 1.3.2.2 nathanw {
212 1.3.2.2 nathanw struct agp_intel_softc *isc = sc->as_chipc;
213 1.3.2.2 nathanw
214 1.3.2.2 nathanw if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
215 1.3.2.2 nathanw return EINVAL;
216 1.3.2.2 nathanw
217 1.3.2.2 nathanw isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 0x17;
218 1.3.2.2 nathanw return 0;
219 1.3.2.2 nathanw }
220 1.3.2.2 nathanw
221 1.3.2.2 nathanw static int
222 1.3.2.2 nathanw agp_intel_unbind_page(struct agp_softc *sc, off_t offset)
223 1.3.2.2 nathanw {
224 1.3.2.2 nathanw struct agp_intel_softc *isc = sc->as_chipc;
225 1.3.2.2 nathanw
226 1.3.2.2 nathanw if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
227 1.3.2.2 nathanw return EINVAL;
228 1.3.2.2 nathanw
229 1.3.2.2 nathanw isc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
230 1.3.2.2 nathanw return 0;
231 1.3.2.2 nathanw }
232 1.3.2.2 nathanw
233 1.3.2.2 nathanw static void
234 1.3.2.2 nathanw agp_intel_flush_tlb(struct agp_softc *sc)
235 1.3.2.2 nathanw {
236 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2200);
237 1.3.2.2 nathanw pci_conf_write(sc->as_pc, sc->as_tag, AGP_INTEL_AGPCTRL, 0x2280);
238 1.3.2.2 nathanw }
239