agp_i810.c revision 1.44 1 1.44 jnemeth /* $NetBSD: agp_i810.c,v 1.44 2007/10/30 12:22:54 jnemeth Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*-
4 1.1 fvdl * Copyright (c) 2000 Doug Rabson
5 1.1 fvdl * Copyright (c) 2000 Ruslan Ermilov
6 1.1 fvdl * All rights reserved.
7 1.1 fvdl *
8 1.1 fvdl * Redistribution and use in source and binary forms, with or without
9 1.1 fvdl * modification, are permitted provided that the following conditions
10 1.1 fvdl * are met:
11 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
12 1.1 fvdl * notice, this list of conditions and the following disclaimer.
13 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
15 1.1 fvdl * documentation and/or other materials provided with the distribution.
16 1.1 fvdl *
17 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 fvdl * SUCH DAMAGE.
28 1.1 fvdl *
29 1.1 fvdl * $FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
30 1.1 fvdl */
31 1.9 lukem
32 1.9 lukem #include <sys/cdefs.h>
33 1.44 jnemeth __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.44 2007/10/30 12:22:54 jnemeth Exp $");
34 1.1 fvdl
35 1.1 fvdl #include <sys/param.h>
36 1.1 fvdl #include <sys/systm.h>
37 1.1 fvdl #include <sys/malloc.h>
38 1.1 fvdl #include <sys/kernel.h>
39 1.1 fvdl #include <sys/lock.h>
40 1.1 fvdl #include <sys/proc.h>
41 1.1 fvdl #include <sys/device.h>
42 1.1 fvdl #include <sys/conf.h>
43 1.1 fvdl
44 1.1 fvdl #include <uvm/uvm_extern.h>
45 1.1 fvdl
46 1.1 fvdl #include <dev/pci/pcivar.h>
47 1.1 fvdl #include <dev/pci/pcireg.h>
48 1.1 fvdl #include <dev/pci/pcidevs.h>
49 1.1 fvdl #include <dev/pci/agpvar.h>
50 1.1 fvdl #include <dev/pci/agpreg.h>
51 1.1 fvdl
52 1.1 fvdl #include <sys/agpio.h>
53 1.1 fvdl
54 1.43 ad #include <sys/bus.h>
55 1.1 fvdl
56 1.20 tron #include "agp_intel.h"
57 1.20 tron
58 1.1 fvdl #define READ1(off) bus_space_read_1(isc->bst, isc->bsh, off)
59 1.14 scw #define READ4(off) bus_space_read_4(isc->bst, isc->bsh, off)
60 1.1 fvdl #define WRITE4(off,v) bus_space_write_4(isc->bst, isc->bsh, off, v)
61 1.28 christos #define WRITEGTT(off, v) \
62 1.28 christos do { \
63 1.28 christos if (isc->chiptype == CHIP_I915) { \
64 1.28 christos bus_space_write_4(isc->gtt_bst, isc->gtt_bsh, \
65 1.28 christos (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4, \
66 1.28 christos (v)); \
67 1.42 markd } else if (isc->chiptype == CHIP_I965) { \
68 1.42 markd WRITE4(AGP_I965_GTT + \
69 1.42 markd (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4, \
70 1.42 markd (v)); \
71 1.28 christos } else { \
72 1.28 christos WRITE4(AGP_I810_GTT + \
73 1.28 christos (u_int32_t)((off) >> AGP_PAGE_SHIFT) * 4, \
74 1.28 christos (v)); \
75 1.28 christos } \
76 1.28 christos } while (0)
77 1.1 fvdl
78 1.14 scw #define CHIP_I810 0 /* i810/i815 */
79 1.17 hannken #define CHIP_I830 1 /* 830M/845G */
80 1.17 hannken #define CHIP_I855 2 /* 852GM/855GM/865G */
81 1.32 simonb #define CHIP_I915 3 /* 915G/915GM/945G/945GM */
82 1.42 markd #define CHIP_I965 4 /* 965Q */
83 1.14 scw
84 1.1 fvdl struct agp_i810_softc {
85 1.1 fvdl u_int32_t initial_aperture; /* aperture size at startup */
86 1.1 fvdl struct agp_gatt *gatt;
87 1.14 scw int chiptype; /* i810-like or i830 */
88 1.14 scw u_int32_t dcache_size; /* i810 only */
89 1.14 scw u_int32_t stolen; /* number of i830/845 gtt entries
90 1.14 scw for stolen memory */
91 1.28 christos bus_space_tag_t bst; /* register bus_space tag */
92 1.28 christos bus_space_handle_t bsh; /* register bus_space handle */
93 1.28 christos bus_space_tag_t gtt_bst; /* GTT bus_space tag */
94 1.28 christos bus_space_handle_t gtt_bsh; /* GTT bus_space handle */
95 1.1 fvdl struct pci_attach_args vga_pa;
96 1.24 jmcneill
97 1.24 jmcneill void *sc_powerhook;
98 1.24 jmcneill struct pci_conf_state sc_pciconf;
99 1.1 fvdl };
100 1.1 fvdl
101 1.1 fvdl static u_int32_t agp_i810_get_aperture(struct agp_softc *);
102 1.1 fvdl static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
103 1.1 fvdl static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
104 1.1 fvdl static int agp_i810_unbind_page(struct agp_softc *, off_t);
105 1.1 fvdl static void agp_i810_flush_tlb(struct agp_softc *);
106 1.1 fvdl static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
107 1.1 fvdl static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
108 1.1 fvdl vsize_t);
109 1.1 fvdl static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
110 1.1 fvdl static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
111 1.1 fvdl static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
112 1.24 jmcneill static void agp_i810_powerhook(int, void *);
113 1.1 fvdl
114 1.26 thorpej static struct agp_methods agp_i810_methods = {
115 1.1 fvdl agp_i810_get_aperture,
116 1.1 fvdl agp_i810_set_aperture,
117 1.1 fvdl agp_i810_bind_page,
118 1.1 fvdl agp_i810_unbind_page,
119 1.1 fvdl agp_i810_flush_tlb,
120 1.1 fvdl agp_i810_enable,
121 1.1 fvdl agp_i810_alloc_memory,
122 1.1 fvdl agp_i810_free_memory,
123 1.1 fvdl agp_i810_bind_memory,
124 1.1 fvdl agp_i810_unbind_memory,
125 1.1 fvdl };
126 1.1 fvdl
127 1.6 thorpej /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
128 1.1 fvdl static int
129 1.1 fvdl agp_i810_vgamatch(struct pci_attach_args *pa)
130 1.1 fvdl {
131 1.6 thorpej
132 1.2 fvdl if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
133 1.2 fvdl PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
134 1.6 thorpej return (0);
135 1.6 thorpej
136 1.1 fvdl switch (PCI_PRODUCT(pa->pa_id)) {
137 1.1 fvdl case PCI_PRODUCT_INTEL_82810_GC:
138 1.1 fvdl case PCI_PRODUCT_INTEL_82810_DC100_GC:
139 1.1 fvdl case PCI_PRODUCT_INTEL_82810E_GC:
140 1.1 fvdl case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
141 1.14 scw case PCI_PRODUCT_INTEL_82830MP_IV:
142 1.14 scw case PCI_PRODUCT_INTEL_82845G_IGD:
143 1.17 hannken case PCI_PRODUCT_INTEL_82855GM_IGD:
144 1.18 tron case PCI_PRODUCT_INTEL_82865_IGD:
145 1.28 christos case PCI_PRODUCT_INTEL_82915G_IGD:
146 1.28 christos case PCI_PRODUCT_INTEL_82915GM_IGD:
147 1.32 simonb case PCI_PRODUCT_INTEL_82945P_IGD:
148 1.32 simonb case PCI_PRODUCT_INTEL_82945GM_IGD:
149 1.32 simonb case PCI_PRODUCT_INTEL_82945GM_IGD_1:
150 1.42 markd case PCI_PRODUCT_INTEL_82965Q_IGD:
151 1.42 markd case PCI_PRODUCT_INTEL_82965Q_IGD_1:
152 1.44 jnemeth case PCI_PRODUCT_INTEL_82965G_IGD:
153 1.44 jnemeth case PCI_PRODUCT_INTEL_82965G_IGD_1:
154 1.6 thorpej return (1);
155 1.1 fvdl }
156 1.1 fvdl
157 1.6 thorpej return (0);
158 1.1 fvdl }
159 1.1 fvdl
160 1.42 markd static int
161 1.42 markd agp_i965_map_aperture(struct pci_attach_args *pa, struct agp_softc *sc, int reg)
162 1.42 markd {
163 1.42 markd /*
164 1.42 markd * Find the aperture. Don't map it (yet), this would
165 1.42 markd * eat KVA.
166 1.42 markd */
167 1.42 markd if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg,
168 1.42 markd PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_64BIT, &sc->as_apaddr, &sc->as_apsize,
169 1.42 markd &sc->as_apflags) != 0)
170 1.42 markd return ENXIO;
171 1.42 markd
172 1.42 markd sc->as_apt = pa->pa_memt;
173 1.42 markd
174 1.42 markd return 0;
175 1.42 markd }
176 1.42 markd
177 1.1 fvdl int
178 1.1 fvdl agp_i810_attach(struct device *parent, struct device *self, void *aux)
179 1.1 fvdl {
180 1.1 fvdl struct agp_softc *sc = (void *)self;
181 1.1 fvdl struct agp_i810_softc *isc;
182 1.1 fvdl struct agp_gatt *gatt;
183 1.28 christos int error, apbase;
184 1.37 drochner bus_size_t mmadrsize;
185 1.1 fvdl
186 1.10 tsutsui isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
187 1.1 fvdl if (isc == NULL) {
188 1.15 thorpej aprint_error(": can't allocate chipset-specific softc\n");
189 1.1 fvdl return ENOMEM;
190 1.1 fvdl }
191 1.1 fvdl sc->as_chipc = isc;
192 1.1 fvdl sc->as_methods = &agp_i810_methods;
193 1.1 fvdl
194 1.1 fvdl if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
195 1.20 tron #if NAGP_INTEL > 0
196 1.19 tron const struct pci_attach_args *pa = aux;
197 1.19 tron
198 1.19 tron switch (PCI_PRODUCT(pa->pa_id)) {
199 1.19 tron case PCI_PRODUCT_INTEL_82840_HB:
200 1.19 tron case PCI_PRODUCT_INTEL_82865_HB:
201 1.21 tron case PCI_PRODUCT_INTEL_82845G_DRAM:
202 1.23 xtraeme case PCI_PRODUCT_INTEL_82815_FULL_HUB:
203 1.19 tron return agp_intel_attach(parent, self, aux);
204 1.20 tron }
205 1.20 tron #endif
206 1.15 thorpej aprint_error(": can't find internal VGA device config space\n");
207 1.1 fvdl free(isc, M_AGP);
208 1.1 fvdl return ENOENT;
209 1.1 fvdl }
210 1.1 fvdl
211 1.1 fvdl /* XXXfvdl */
212 1.1 fvdl sc->as_dmat = isc->vga_pa.pa_dmat;
213 1.1 fvdl
214 1.14 scw switch (PCI_PRODUCT(isc->vga_pa.pa_id)) {
215 1.14 scw case PCI_PRODUCT_INTEL_82810_GC:
216 1.14 scw case PCI_PRODUCT_INTEL_82810_DC100_GC:
217 1.14 scw case PCI_PRODUCT_INTEL_82810E_GC:
218 1.14 scw case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
219 1.14 scw isc->chiptype = CHIP_I810;
220 1.14 scw break;
221 1.14 scw case PCI_PRODUCT_INTEL_82830MP_IV:
222 1.14 scw case PCI_PRODUCT_INTEL_82845G_IGD:
223 1.14 scw isc->chiptype = CHIP_I830;
224 1.14 scw break;
225 1.17 hannken case PCI_PRODUCT_INTEL_82855GM_IGD:
226 1.18 tron case PCI_PRODUCT_INTEL_82865_IGD:
227 1.17 hannken isc->chiptype = CHIP_I855;
228 1.17 hannken break;
229 1.28 christos case PCI_PRODUCT_INTEL_82915G_IGD:
230 1.28 christos case PCI_PRODUCT_INTEL_82915GM_IGD:
231 1.32 simonb case PCI_PRODUCT_INTEL_82945P_IGD:
232 1.32 simonb case PCI_PRODUCT_INTEL_82945GM_IGD:
233 1.32 simonb case PCI_PRODUCT_INTEL_82945GM_IGD_1:
234 1.28 christos isc->chiptype = CHIP_I915;
235 1.28 christos break;
236 1.42 markd case PCI_PRODUCT_INTEL_82965Q_IGD:
237 1.42 markd case PCI_PRODUCT_INTEL_82965Q_IGD_1:
238 1.44 jnemeth case PCI_PRODUCT_INTEL_82965G_IGD:
239 1.44 jnemeth case PCI_PRODUCT_INTEL_82965G_IGD_1:
240 1.42 markd isc->chiptype = CHIP_I965;
241 1.42 markd break;
242 1.14 scw }
243 1.14 scw
244 1.28 christos apbase = isc->chiptype == CHIP_I915 ? AGP_I915_GMADR : AGP_I810_GMADR;
245 1.42 markd if (isc->chiptype == CHIP_I965) {
246 1.42 markd error = agp_i965_map_aperture(&isc->vga_pa, sc, AGP_I965_GMADR);
247 1.42 markd } else {
248 1.42 markd error = agp_map_aperture(&isc->vga_pa, sc, apbase);
249 1.42 markd }
250 1.1 fvdl if (error != 0) {
251 1.28 christos aprint_error(": can't map aperture\n");
252 1.28 christos free(isc, M_AGP);
253 1.1 fvdl return error;
254 1.1 fvdl }
255 1.1 fvdl
256 1.28 christos if (isc->chiptype == CHIP_I915) {
257 1.28 christos error = pci_mapreg_map(&isc->vga_pa, AGP_I915_MMADR,
258 1.39 drochner PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
259 1.39 drochner NULL, &mmadrsize);
260 1.28 christos if (error != 0) {
261 1.28 christos aprint_error(": can't map mmadr registers\n");
262 1.28 christos agp_generic_detach(sc);
263 1.28 christos return error;
264 1.28 christos }
265 1.28 christos error = pci_mapreg_map(&isc->vga_pa, AGP_I915_GTTADR,
266 1.28 christos PCI_MAPREG_TYPE_MEM, 0, &isc->gtt_bst, &isc->gtt_bsh,
267 1.28 christos NULL, NULL);
268 1.28 christos if (error != 0) {
269 1.28 christos aprint_error(": can't map gttadr registers\n");
270 1.28 christos /* XXX we should release mmadr here */
271 1.28 christos agp_generic_detach(sc);
272 1.28 christos return error;
273 1.28 christos }
274 1.42 markd } else if (isc->chiptype == CHIP_I965) {
275 1.42 markd error = pci_mapreg_map(&isc->vga_pa, AGP_I965_MMADR,
276 1.42 markd PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
277 1.42 markd NULL, &mmadrsize);
278 1.42 markd if (error != 0) {
279 1.42 markd aprint_error(": can't map mmadr registers\n");
280 1.42 markd agp_generic_detach(sc);
281 1.42 markd return error;
282 1.42 markd }
283 1.28 christos } else {
284 1.28 christos error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
285 1.39 drochner PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh,
286 1.39 drochner NULL, &mmadrsize);
287 1.28 christos if (error != 0) {
288 1.28 christos aprint_error(": can't map mmadr registers\n");
289 1.28 christos agp_generic_detach(sc);
290 1.28 christos return error;
291 1.28 christos }
292 1.28 christos }
293 1.28 christos
294 1.1 fvdl isc->initial_aperture = AGP_GET_APERTURE(sc);
295 1.1 fvdl
296 1.14 scw gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
297 1.14 scw if (!gatt) {
298 1.14 scw agp_generic_detach(sc);
299 1.14 scw return ENOMEM;
300 1.14 scw }
301 1.14 scw isc->gatt = gatt;
302 1.14 scw
303 1.14 scw gatt->ag_entries = AGP_GET_APERTURE(sc) >> AGP_PAGE_SHIFT;
304 1.1 fvdl
305 1.14 scw if (isc->chiptype == CHIP_I810) {
306 1.36 christos void *virtual;
307 1.14 scw int dummyseg;
308 1.31 tron
309 1.14 scw /* Some i810s have on-chip memory called dcache */
310 1.14 scw if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
311 1.14 scw isc->dcache_size = 4 * 1024 * 1024;
312 1.14 scw else
313 1.14 scw isc->dcache_size = 0;
314 1.14 scw
315 1.14 scw /* According to the specs the gatt on the i810 must be 64k */
316 1.14 scw if (agp_alloc_dmamem(sc->as_dmat, 64 * 1024,
317 1.31 tron 0, &gatt->ag_dmamap, &virtual, &gatt->ag_physical,
318 1.31 tron &gatt->ag_dmaseg, 1, &dummyseg) != 0) {
319 1.14 scw free(gatt, M_AGP);
320 1.1 fvdl agp_generic_detach(sc);
321 1.1 fvdl return ENOMEM;
322 1.1 fvdl }
323 1.31 tron gatt->ag_virtual = (uint32_t *)virtual;
324 1.14 scw
325 1.14 scw gatt->ag_size = gatt->ag_entries * sizeof(u_int32_t);
326 1.14 scw memset(gatt->ag_virtual, 0, gatt->ag_size);
327 1.25 perry
328 1.14 scw agp_flush_cache();
329 1.14 scw /* Install the GATT. */
330 1.14 scw WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
331 1.17 hannken } else if (isc->chiptype == CHIP_I830) {
332 1.14 scw /* The i830 automatically initializes the 128k gatt on boot. */
333 1.14 scw pcireg_t reg;
334 1.14 scw u_int32_t pgtblctl;
335 1.14 scw u_int16_t gcc1;
336 1.14 scw
337 1.14 scw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
338 1.14 scw gcc1 = (u_int16_t)(reg >> 16);
339 1.14 scw switch (gcc1 & AGP_I830_GCC1_GMS) {
340 1.14 scw case AGP_I830_GCC1_GMS_STOLEN_512:
341 1.14 scw isc->stolen = (512 - 132) * 1024 / 4096;
342 1.14 scw break;
343 1.25 perry case AGP_I830_GCC1_GMS_STOLEN_1024:
344 1.14 scw isc->stolen = (1024 - 132) * 1024 / 4096;
345 1.14 scw break;
346 1.25 perry case AGP_I830_GCC1_GMS_STOLEN_8192:
347 1.14 scw isc->stolen = (8192 - 132) * 1024 / 4096;
348 1.14 scw break;
349 1.14 scw default:
350 1.14 scw isc->stolen = 0;
351 1.15 thorpej aprint_error(
352 1.15 thorpej ": unknown memory configuration, disabling\n");
353 1.14 scw agp_generic_detach(sc);
354 1.14 scw return EINVAL;
355 1.14 scw }
356 1.14 scw if (isc->stolen > 0) {
357 1.17 hannken aprint_error(": detected %dk stolen memory\n%s",
358 1.17 hannken isc->stolen * 4, sc->as_dev.dv_xname);
359 1.14 scw }
360 1.17 hannken
361 1.17 hannken /* GATT address is already in there, make sure it's enabled */
362 1.17 hannken pgtblctl = READ4(AGP_I810_PGTBL_CTL);
363 1.17 hannken pgtblctl |= 1;
364 1.17 hannken WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
365 1.17 hannken
366 1.17 hannken gatt->ag_physical = pgtblctl & ~1;
367 1.42 markd } else if (isc->chiptype == CHIP_I855 || isc->chiptype == CHIP_I915 ||
368 1.42 markd isc->chiptype == CHIP_I965) {
369 1.17 hannken pcireg_t reg;
370 1.42 markd u_int32_t pgtblctl, stolen;
371 1.17 hannken u_int16_t gcc1;
372 1.17 hannken
373 1.42 markd /* Stolen memory is set up at the beginning of the aperture by
374 1.42 markd * the BIOS, consisting of the GATT followed by 4kb for the
375 1.42 markd * BIOS display.
376 1.42 markd */
377 1.42 markd switch (isc->chiptype) {
378 1.42 markd case CHIP_I855:
379 1.42 markd stolen = 128 + 4;
380 1.42 markd break;
381 1.42 markd case CHIP_I915:
382 1.42 markd stolen = 256 + 4;
383 1.42 markd break;
384 1.42 markd case CHIP_I965:
385 1.42 markd stolen = 512 + 4;
386 1.42 markd break;
387 1.42 markd default:
388 1.42 markd aprint_error(": bad chiptype\n");
389 1.42 markd agp_generic_detach(sc);
390 1.42 markd return EINVAL;
391 1.42 markd }
392 1.42 markd
393 1.17 hannken reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I855_GCC1);
394 1.17 hannken gcc1 = (u_int16_t)(reg >> 16);
395 1.17 hannken switch (gcc1 & AGP_I855_GCC1_GMS) {
396 1.17 hannken case AGP_I855_GCC1_GMS_STOLEN_1M:
397 1.42 markd isc->stolen = (1024 - stolen) * 1024 / 4096;
398 1.17 hannken break;
399 1.17 hannken case AGP_I855_GCC1_GMS_STOLEN_4M:
400 1.42 markd isc->stolen = (4096 - stolen) * 1024 / 4096;
401 1.17 hannken break;
402 1.17 hannken case AGP_I855_GCC1_GMS_STOLEN_8M:
403 1.42 markd isc->stolen = (8192 - stolen) * 1024 / 4096;
404 1.17 hannken break;
405 1.17 hannken case AGP_I855_GCC1_GMS_STOLEN_16M:
406 1.42 markd isc->stolen = (16384 - stolen) * 1024 / 4096;
407 1.17 hannken break;
408 1.17 hannken case AGP_I855_GCC1_GMS_STOLEN_32M:
409 1.42 markd isc->stolen = (32768 - stolen) * 1024 / 4096;
410 1.41 sborrill break;
411 1.41 sborrill case AGP_I915_GCC1_GMS_STOLEN_48M:
412 1.42 markd isc->stolen = (49152 - stolen) * 1024 / 4096;
413 1.41 sborrill break;
414 1.41 sborrill case AGP_I915_GCC1_GMS_STOLEN_64M:
415 1.42 markd isc->stolen = (65536 - stolen) * 1024 / 4096;
416 1.41 sborrill break;
417 1.28 christos default:
418 1.28 christos isc->stolen = 0;
419 1.28 christos aprint_error(
420 1.28 christos ": unknown memory configuration, disabling\n");
421 1.28 christos agp_generic_detach(sc);
422 1.28 christos return EINVAL;
423 1.28 christos }
424 1.28 christos if (isc->stolen > 0) {
425 1.28 christos aprint_error(": detected %dk stolen memory\n%s",
426 1.28 christos isc->stolen * 4, sc->as_dev.dv_xname);
427 1.28 christos }
428 1.28 christos
429 1.28 christos /* GATT address is already in there, make sure it's enabled */
430 1.28 christos pgtblctl = READ4(AGP_I810_PGTBL_CTL);
431 1.28 christos pgtblctl |= 1;
432 1.28 christos WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
433 1.28 christos
434 1.28 christos gatt->ag_physical = pgtblctl & ~1;
435 1.1 fvdl }
436 1.1 fvdl
437 1.1 fvdl /*
438 1.1 fvdl * Make sure the chipset can see everything.
439 1.1 fvdl */
440 1.1 fvdl agp_flush_cache();
441 1.14 scw
442 1.33 jmcneill isc->sc_powerhook = powerhook_establish(sc->as_dev.dv_xname,
443 1.33 jmcneill agp_i810_powerhook, sc);
444 1.24 jmcneill if (isc->sc_powerhook == NULL)
445 1.24 jmcneill printf("%s: WARNING: unable to establish PCI power hook\n",
446 1.24 jmcneill sc->as_dev.dv_xname);
447 1.24 jmcneill
448 1.40 christos #if 0
449 1.37 drochner /*
450 1.37 drochner * another device (drm) may need access to this region
451 1.37 drochner * we do not need it anymore
452 1.37 drochner */
453 1.37 drochner bus_space_unmap(isc->bst, isc->bsh, mmadrsize);
454 1.40 christos #endif
455 1.37 drochner
456 1.1 fvdl return 0;
457 1.1 fvdl }
458 1.1 fvdl
459 1.1 fvdl #if 0
460 1.1 fvdl static int
461 1.1 fvdl agp_i810_detach(struct agp_softc *sc)
462 1.1 fvdl {
463 1.1 fvdl int error;
464 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
465 1.1 fvdl
466 1.1 fvdl error = agp_generic_detach(sc);
467 1.1 fvdl if (error)
468 1.1 fvdl return error;
469 1.1 fvdl
470 1.1 fvdl /* Clear the GATT base. */
471 1.14 scw if (sc->chiptype == CHIP_I810) {
472 1.14 scw WRITE4(AGP_I810_PGTBL_CTL, 0);
473 1.14 scw } else {
474 1.14 scw unsigned int pgtblctl;
475 1.14 scw pgtblctl = READ4(AGP_I810_PGTBL_CTL);
476 1.14 scw pgtblctl &= ~1;
477 1.14 scw WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
478 1.14 scw }
479 1.1 fvdl
480 1.1 fvdl /* Put the aperture back the way it started. */
481 1.1 fvdl AGP_SET_APERTURE(sc, isc->initial_aperture);
482 1.1 fvdl
483 1.14 scw if (sc->chiptype == CHIP_I810) {
484 1.14 scw agp_free_dmamem(sc->as_dmat, gatt->ag_size, gatt->ag_dmamap,
485 1.36 christos (void *)gatt->ag_virtual, &gatt->ag_dmaseg, 1);
486 1.14 scw }
487 1.14 scw free(sc->gatt, M_AGP);
488 1.1 fvdl
489 1.1 fvdl return 0;
490 1.1 fvdl }
491 1.1 fvdl #endif
492 1.1 fvdl
493 1.1 fvdl static u_int32_t
494 1.1 fvdl agp_i810_get_aperture(struct agp_softc *sc)
495 1.1 fvdl {
496 1.14 scw struct agp_i810_softc *isc = sc->as_chipc;
497 1.14 scw pcireg_t reg;
498 1.42 markd u_int16_t miscc, gcc1, msac;
499 1.14 scw
500 1.42 markd switch (isc->chiptype) {
501 1.42 markd case CHIP_I810:
502 1.14 scw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
503 1.14 scw miscc = (u_int16_t)(reg >> 16);
504 1.14 scw if ((miscc & AGP_I810_MISCC_WINSIZE) ==
505 1.14 scw AGP_I810_MISCC_WINSIZE_32)
506 1.14 scw return 32 * 1024 * 1024;
507 1.14 scw else
508 1.14 scw return 64 * 1024 * 1024;
509 1.42 markd case CHIP_I830:
510 1.14 scw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
511 1.14 scw gcc1 = (u_int16_t)(reg >> 16);
512 1.14 scw if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
513 1.14 scw return 64 * 1024 * 1024;
514 1.14 scw else
515 1.14 scw return 128 * 1024 * 1024;
516 1.42 markd case CHIP_I855:
517 1.17 hannken return 128 * 1024 * 1024;
518 1.42 markd case CHIP_I915:
519 1.28 christos reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I915_MSAC);
520 1.28 christos msac = (u_int16_t)(reg >> 16);
521 1.28 christos if (msac & AGP_I915_MSAC_APER_128M)
522 1.28 christos return 128 * 1024 * 1024;
523 1.28 christos else
524 1.28 christos return 256 * 1024 * 1024;
525 1.42 markd case CHIP_I965:
526 1.42 markd return 512 * 1024 * 1024;
527 1.42 markd default:
528 1.42 markd aprint_error(": Unknown chipset\n");
529 1.14 scw }
530 1.42 markd
531 1.42 markd return 0;
532 1.1 fvdl }
533 1.1 fvdl
534 1.1 fvdl static int
535 1.1 fvdl agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
536 1.1 fvdl {
537 1.14 scw struct agp_i810_softc *isc = sc->as_chipc;
538 1.14 scw pcireg_t reg;
539 1.42 markd u_int16_t miscc, gcc1;
540 1.14 scw
541 1.42 markd switch (isc->chiptype) {
542 1.42 markd case CHIP_I810:
543 1.14 scw /*
544 1.14 scw * Double check for sanity.
545 1.14 scw */
546 1.14 scw if (aperture != (32 * 1024 * 1024) &&
547 1.14 scw aperture != (64 * 1024 * 1024)) {
548 1.14 scw printf("%s: bad aperture size %d\n",
549 1.14 scw sc->as_dev.dv_xname, aperture);
550 1.14 scw return EINVAL;
551 1.14 scw }
552 1.1 fvdl
553 1.14 scw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
554 1.14 scw miscc = (u_int16_t)(reg >> 16);
555 1.14 scw miscc &= ~AGP_I810_MISCC_WINSIZE;
556 1.14 scw if (aperture == 32 * 1024 * 1024)
557 1.14 scw miscc |= AGP_I810_MISCC_WINSIZE_32;
558 1.14 scw else
559 1.14 scw miscc |= AGP_I810_MISCC_WINSIZE_64;
560 1.14 scw
561 1.14 scw reg &= 0x0000ffff;
562 1.14 scw reg |= ((pcireg_t)miscc) << 16;
563 1.14 scw pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, reg);
564 1.42 markd break;
565 1.42 markd case CHIP_I830:
566 1.14 scw if (aperture != (64 * 1024 * 1024) &&
567 1.14 scw aperture != (128 * 1024 * 1024)) {
568 1.14 scw printf("%s: bad aperture size %d\n",
569 1.14 scw sc->as_dev.dv_xname, aperture);
570 1.14 scw return EINVAL;
571 1.14 scw }
572 1.14 scw reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I830_GCC0);
573 1.14 scw gcc1 = (u_int16_t)(reg >> 16);
574 1.14 scw gcc1 &= ~AGP_I830_GCC1_GMASIZE;
575 1.14 scw if (aperture == 64 * 1024 * 1024)
576 1.14 scw gcc1 |= AGP_I830_GCC1_GMASIZE_64;
577 1.14 scw else
578 1.14 scw gcc1 |= AGP_I830_GCC1_GMASIZE_128;
579 1.14 scw
580 1.14 scw reg &= 0x0000ffff;
581 1.14 scw reg |= ((pcireg_t)gcc1) << 16;
582 1.14 scw pci_conf_write(sc->as_pc, sc->as_tag, AGP_I830_GCC0, reg);
583 1.42 markd break;
584 1.42 markd case CHIP_I855:
585 1.42 markd case CHIP_I915:
586 1.28 christos if (aperture != agp_i810_get_aperture(sc)) {
587 1.17 hannken printf("%s: bad aperture size %d\n",
588 1.17 hannken sc->as_dev.dv_xname, aperture);
589 1.17 hannken return EINVAL;
590 1.17 hannken }
591 1.42 markd break;
592 1.42 markd case CHIP_I965:
593 1.42 markd if (aperture != 512 * 1024 * 1024) {
594 1.42 markd printf("%s: bad aperture size %d\n",
595 1.42 markd sc->as_dev.dv_xname, aperture);
596 1.42 markd return EINVAL;
597 1.42 markd }
598 1.42 markd break;
599 1.1 fvdl }
600 1.1 fvdl
601 1.1 fvdl return 0;
602 1.1 fvdl }
603 1.1 fvdl
604 1.1 fvdl static int
605 1.1 fvdl agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
606 1.1 fvdl {
607 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
608 1.1 fvdl
609 1.14 scw if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
610 1.29 rpaulo #ifdef AGP_DEBUG
611 1.14 scw printf("%s: failed: offset 0x%08x, shift %d, entries %d\n",
612 1.14 scw sc->as_dev.dv_xname, (int)offset, AGP_PAGE_SHIFT,
613 1.14 scw isc->gatt->ag_entries);
614 1.14 scw #endif
615 1.1 fvdl return EINVAL;
616 1.14 scw }
617 1.14 scw
618 1.17 hannken if (isc->chiptype != CHIP_I830) {
619 1.14 scw if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
620 1.29 rpaulo #ifdef AGP_DEBUG
621 1.14 scw printf("%s: trying to bind into stolen memory",
622 1.14 scw sc->as_dev.dv_xname);
623 1.14 scw #endif
624 1.14 scw return EINVAL;
625 1.14 scw }
626 1.14 scw }
627 1.1 fvdl
628 1.28 christos WRITEGTT(offset, physical | 1);
629 1.1 fvdl return 0;
630 1.1 fvdl }
631 1.1 fvdl
632 1.1 fvdl static int
633 1.1 fvdl agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
634 1.1 fvdl {
635 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
636 1.1 fvdl
637 1.1 fvdl if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
638 1.1 fvdl return EINVAL;
639 1.1 fvdl
640 1.17 hannken if (isc->chiptype != CHIP_I810 ) {
641 1.14 scw if ((offset >> AGP_PAGE_SHIFT) < isc->stolen) {
642 1.29 rpaulo #ifdef AGP_DEBUG
643 1.14 scw printf("%s: trying to unbind from stolen memory",
644 1.14 scw sc->as_dev.dv_xname);
645 1.14 scw #endif
646 1.14 scw return EINVAL;
647 1.14 scw }
648 1.14 scw }
649 1.14 scw
650 1.28 christos WRITEGTT(offset, 0);
651 1.1 fvdl return 0;
652 1.1 fvdl }
653 1.1 fvdl
654 1.1 fvdl /*
655 1.1 fvdl * Writing via memory mapped registers already flushes all TLBs.
656 1.1 fvdl */
657 1.1 fvdl static void
658 1.35 christos agp_i810_flush_tlb(struct agp_softc *sc)
659 1.1 fvdl {
660 1.1 fvdl }
661 1.1 fvdl
662 1.1 fvdl static int
663 1.35 christos agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
664 1.1 fvdl {
665 1.1 fvdl
666 1.1 fvdl return 0;
667 1.1 fvdl }
668 1.1 fvdl
669 1.1 fvdl static struct agp_memory *
670 1.1 fvdl agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
671 1.1 fvdl {
672 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
673 1.1 fvdl struct agp_memory *mem;
674 1.1 fvdl
675 1.29 rpaulo #ifdef AGP_DEBUG
676 1.28 christos printf("AGP: alloc(%d, 0x%x)\n", type, (int) size);
677 1.28 christos #endif
678 1.28 christos
679 1.1 fvdl if ((size & (AGP_PAGE_SIZE - 1)) != 0)
680 1.1 fvdl return 0;
681 1.1 fvdl
682 1.1 fvdl if (sc->as_allocated + size > sc->as_maxmem)
683 1.1 fvdl return 0;
684 1.1 fvdl
685 1.1 fvdl if (type == 1) {
686 1.1 fvdl /*
687 1.1 fvdl * Mapping local DRAM into GATT.
688 1.1 fvdl */
689 1.17 hannken if (isc->chiptype != CHIP_I810 )
690 1.14 scw return 0;
691 1.1 fvdl if (size != isc->dcache_size)
692 1.1 fvdl return 0;
693 1.1 fvdl } else if (type == 2) {
694 1.1 fvdl /*
695 1.28 christos * Bogus mapping for the hardware cursor.
696 1.1 fvdl */
697 1.28 christos if (size != AGP_PAGE_SIZE && size != 4 * AGP_PAGE_SIZE)
698 1.1 fvdl return 0;
699 1.1 fvdl }
700 1.1 fvdl
701 1.10 tsutsui mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
702 1.1 fvdl if (mem == NULL)
703 1.1 fvdl return NULL;
704 1.1 fvdl mem->am_id = sc->as_nextid++;
705 1.1 fvdl mem->am_size = size;
706 1.1 fvdl mem->am_type = type;
707 1.1 fvdl
708 1.1 fvdl if (type == 2) {
709 1.1 fvdl /*
710 1.28 christos * Allocate and wire down the memory now so that we can
711 1.1 fvdl * get its physical address.
712 1.1 fvdl */
713 1.1 fvdl mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
714 1.1 fvdl M_WAITOK);
715 1.1 fvdl if (mem->am_dmaseg == NULL) {
716 1.1 fvdl free(mem, M_AGP);
717 1.1 fvdl return NULL;
718 1.1 fvdl }
719 1.1 fvdl if (agp_alloc_dmamem(sc->as_dmat, size, 0,
720 1.1 fvdl &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
721 1.1 fvdl mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
722 1.1 fvdl free(mem->am_dmaseg, M_AGP);
723 1.1 fvdl free(mem, M_AGP);
724 1.1 fvdl return NULL;
725 1.1 fvdl }
726 1.28 christos memset(mem->am_virtual, 0, size);
727 1.1 fvdl } else if (type != 1) {
728 1.4 drochner if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
729 1.4 drochner size, 0, BUS_DMA_NOWAIT,
730 1.4 drochner &mem->am_dmamap) != 0) {
731 1.1 fvdl free(mem, M_AGP);
732 1.1 fvdl return NULL;
733 1.1 fvdl }
734 1.1 fvdl }
735 1.1 fvdl
736 1.1 fvdl TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
737 1.1 fvdl sc->as_allocated += size;
738 1.1 fvdl
739 1.1 fvdl return mem;
740 1.1 fvdl }
741 1.1 fvdl
742 1.1 fvdl static int
743 1.1 fvdl agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
744 1.1 fvdl {
745 1.1 fvdl if (mem->am_is_bound)
746 1.1 fvdl return EBUSY;
747 1.1 fvdl
748 1.1 fvdl if (mem->am_type == 2) {
749 1.1 fvdl agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
750 1.1 fvdl mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
751 1.1 fvdl free(mem->am_dmaseg, M_AGP);
752 1.1 fvdl }
753 1.1 fvdl
754 1.1 fvdl sc->as_allocated -= mem->am_size;
755 1.1 fvdl TAILQ_REMOVE(&sc->as_memory, mem, am_link);
756 1.1 fvdl free(mem, M_AGP);
757 1.1 fvdl return 0;
758 1.1 fvdl }
759 1.1 fvdl
760 1.1 fvdl static int
761 1.1 fvdl agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
762 1.1 fvdl off_t offset)
763 1.1 fvdl {
764 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
765 1.4 drochner u_int32_t regval, i;
766 1.4 drochner
767 1.4 drochner /*
768 1.4 drochner * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
769 1.4 drochner * X server for mysterious reasons which leads to crashes if we write
770 1.4 drochner * to the GTT through the MMIO window.
771 1.4 drochner * Until the issue is solved, simply restore it.
772 1.4 drochner */
773 1.37 drochner
774 1.37 drochner #if 0
775 1.4 drochner regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
776 1.4 drochner if (regval != (isc->gatt->ag_physical | 1)) {
777 1.4 drochner printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
778 1.4 drochner regval);
779 1.4 drochner bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
780 1.4 drochner isc->gatt->ag_physical | 1);
781 1.4 drochner }
782 1.37 drochner #endif
783 1.37 drochner regval = 0;
784 1.1 fvdl
785 1.5 drochner if (mem->am_type == 2) {
786 1.28 christos WRITEGTT(offset, mem->am_physical | 1);
787 1.5 drochner mem->am_offset = offset;
788 1.5 drochner mem->am_is_bound = 1;
789 1.1 fvdl return 0;
790 1.5 drochner }
791 1.5 drochner
792 1.1 fvdl if (mem->am_type != 1)
793 1.1 fvdl return agp_generic_bind_memory(sc, mem, offset);
794 1.1 fvdl
795 1.17 hannken if (isc->chiptype != CHIP_I810)
796 1.14 scw return EINVAL;
797 1.14 scw
798 1.28 christos for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
799 1.28 christos WRITEGTT(offset, i | 3);
800 1.13 drochner mem->am_is_bound = 1;
801 1.1 fvdl return 0;
802 1.1 fvdl }
803 1.1 fvdl
804 1.1 fvdl static int
805 1.1 fvdl agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
806 1.1 fvdl {
807 1.1 fvdl struct agp_i810_softc *isc = sc->as_chipc;
808 1.1 fvdl u_int32_t i;
809 1.1 fvdl
810 1.5 drochner if (mem->am_type == 2) {
811 1.28 christos WRITEGTT(mem->am_offset, 0);
812 1.5 drochner mem->am_offset = 0;
813 1.5 drochner mem->am_is_bound = 0;
814 1.1 fvdl return 0;
815 1.5 drochner }
816 1.1 fvdl
817 1.1 fvdl if (mem->am_type != 1)
818 1.1 fvdl return agp_generic_unbind_memory(sc, mem);
819 1.14 scw
820 1.17 hannken if (isc->chiptype != CHIP_I810)
821 1.14 scw return EINVAL;
822 1.1 fvdl
823 1.1 fvdl for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
824 1.28 christos WRITEGTT(i, 0);
825 1.13 drochner mem->am_is_bound = 0;
826 1.1 fvdl return 0;
827 1.1 fvdl }
828 1.24 jmcneill
829 1.24 jmcneill static void
830 1.24 jmcneill agp_i810_powerhook(int why, void *arg)
831 1.24 jmcneill {
832 1.24 jmcneill struct agp_softc *sc = (struct agp_softc *)arg;
833 1.24 jmcneill struct agp_i810_softc *isc = sc->as_chipc;
834 1.24 jmcneill
835 1.24 jmcneill if (why == PWR_RESUME) {
836 1.24 jmcneill pci_conf_restore(sc->as_pc, sc->as_tag, &isc->sc_pciconf);
837 1.24 jmcneill agp_flush_cache();
838 1.24 jmcneill } else if ((why == PWR_STANDBY) || (why == PWR_SUSPEND))
839 1.24 jmcneill pci_conf_capture(sc->as_pc, sc->as_tag, &isc->sc_pciconf);
840 1.24 jmcneill
841 1.24 jmcneill return;
842 1.24 jmcneill }
843