Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / agp / agp_i810.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2000 Ruslan Ermilov
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/sys/pci/agp_i810.c,v 1.1.2.5 2002/09/15 08:45:41 anholt Exp $
28 */
29
30/*
31 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
32 */
33
34#include "opt_bus.h"
35#include "opt_pci.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/kernel.h>
41#include <sys/bus.h>
42#include <sys/lock.h>
43
44#include <pci/pcivar.h>
45#include <pci/pcireg.h>
46#include <pci/agppriv.h>
47#include <pci/agpreg.h>
48
49#include <vm/vm.h>
50#include <vm/vm_object.h>
51#include <vm/vm_page.h>
52#include <vm/vm_pageout.h>
53#include <vm/pmap.h>
54
55#include <machine/bus.h>
56#include <machine/resource.h>
57#include <sys/rman.h>
58
59MALLOC_DECLARE(M_AGP);
60
61#define READ1(off) bus_space_read_1(sc->bst, sc->bsh, off)
62#define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off)
63#define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v)
64
65#define CHIP_I810 0 /* i810/i815 */
66#define CHIP_I830 1 /* i830/i845 */
67
68struct agp_i810_softc {
69 struct agp_softc agp;
70 u_int32_t initial_aperture; /* aperture size at startup */
71 struct agp_gatt *gatt;
72 int chiptype; /* i810-like or i830 */
73 u_int32_t dcache_size; /* i810 only */
74 u_int32_t stolen; /* number of i830/845 gtt entries for stolen memory */
75 device_t bdev; /* bridge device */
76 struct resource *regs; /* memory mapped GC registers */
77 bus_space_tag_t bst; /* bus_space tag */
78 bus_space_handle_t bsh; /* bus_space handle */
79};
80
81static const char*
82agp_i810_match(device_t dev)
83{
84 if (pci_get_class(dev) != PCIC_DISPLAY
85 || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
86 return NULL;
87
88 switch (pci_get_devid(dev)) {
89 case 0x71218086:
90 return ("Intel 82810 (i810 GMCH) SVGA controller");
91
92 case 0x71238086:
93 return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
94
95 case 0x71258086:
96 return ("Intel 82810E (i810E GMCH) SVGA controller");
97
98 case 0x11328086:
99 return ("Intel 82815 (i815 GMCH) SVGA controller");
100
101 case 0x35778086:
102 return ("Intel 82830 (i830M GMCH) SVGA controller");
103
104 case 0x25628086:
105 return ("Intel 82845 (i845 GMCH) SVGA controller");
106 };
107
108 return NULL;
109}
110
111/*
112 * Find bridge device.
113 */
114static device_t
115agp_i810_find_bridge(device_t dev)
116{
117 device_t *children, child;
118 int nchildren, i;
119 u_int32_t devid;
120
121 /*
122 * Calculate bridge device's ID.
123 */
124 devid = pci_get_devid(dev);
125 switch (devid) {
126 case 0x71218086:
127 case 0x71238086:
128 case 0x71258086:
129 devid -= 0x10000;
130 break;
131
132 case 0x11328086:
133 case 0x35778086:
134 case 0x25628086:
135 devid -= 0x20000;
136 break;
137 };
138 if (device_get_children(device_get_parent(dev), &children, &nchildren))
139 return 0;
140
141 for (i = 0; i < nchildren; i++) {
142 child = children[i];
143
144 if (pci_get_devid(child) == devid) {
145 free(children, M_TEMP);
146 return child;
147 }
148 }
149 free(children, M_TEMP);
150 return 0;
151}
152
153static int
154agp_i810_probe(device_t dev)
155{
156 const char *desc;
157
158 desc = agp_i810_match(dev);
159 if (desc) {
160 device_t bdev;
161 u_int8_t smram;
162 int devid = pci_get_devid(dev);
163
164 bdev = agp_i810_find_bridge(dev);
165 if (!bdev) {
166 if (bootverbose)
167 printf("I810: can't find bridge device\n");
168 return ENXIO;
169 }
170
171 /*
172 * checking whether internal graphics device has been activated.
173 */
174 if ( (devid != 0x35778086 ) &&
175 (devid != 0x25628086 ) ) {
176 smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
177 if ((smram & AGP_I810_SMRAM_GMS)
178 == AGP_I810_SMRAM_GMS_DISABLED) {
179 if (bootverbose)
180 printf("I810: disabled, not probing\n");
181 return ENXIO;
182 }
183 } else { /* I830MG */
184 unsigned int gcc1;
185 gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
186 if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
187 if (bootverbose)
188 printf("I830: disabled, not probing\n");
189 return ENXIO;
190 }
191 }
192
193 device_verbose(dev);
194 device_set_desc(dev, desc);
195 return 0;
196 }
197
198 return ENXIO;
199}
200
201static int
202agp_i810_attach(device_t dev)
203{
204 struct agp_i810_softc *sc = device_get_softc(dev);
205 struct agp_gatt *gatt;
206 int error, rid;
207
208 sc->bdev = agp_i810_find_bridge(dev);
209 if (!sc->bdev)
210 return ENOENT;
211
212 error = agp_generic_attach(dev);
213 if (error)
214 return error;
215
216 switch (pci_get_devid(dev)) {
217 case 0x71218086:
218 case 0x71238086:
219 case 0x71258086:
220 case 0x11328086:
221 sc->chiptype = CHIP_I810;
222 break;
223 case 0x35778086:
224 case 0x25628086:
225 sc->chiptype = CHIP_I830;
226 break;
227 };
228
229 /* Same for i810 and i830 */
230 rid = AGP_I810_MMADR;
231 sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
232 0, ~0, 1, RF_ACTIVE);
233 if (!sc->regs) {
234 agp_generic_detach(dev);
235 return ENOMEM;
236 }
237 sc->bst = rman_get_bustag(sc->regs);
238 sc->bsh = rman_get_bushandle(sc->regs);
239
240 sc->initial_aperture = AGP_GET_APERTURE(dev);
241
242 gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
243 if (!gatt) {
244 agp_generic_detach(dev);
245 return ENOMEM;
246 }
247 sc->gatt = gatt;
248
249 gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
250
251 if ( sc->chiptype == CHIP_I810 ) {
252 /* Some i810s have on-chip memory called dcache */
253 if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
254 sc->dcache_size = 4 * 1024 * 1024;
255 else
256 sc->dcache_size = 0;
257
258 /* According to the specs the gatt on the i810 must be 64k */
259 gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
260 0, ~0, PAGE_SIZE, 0);
261 if (!gatt->ag_virtual) {
262 if (bootverbose)
263 device_printf(dev, "contiguous allocation failed\n");
264 free(gatt, M_AGP);
265 agp_generic_detach(dev);
266 return ENOMEM;
267 }
268 bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
269
270 gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
271 agp_flush_cache();
272 /* Install the GATT. */
273 WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
274 } else {
275 /* The i830 automatically initializes the 128k gatt on boot. */
276 unsigned int gcc1, pgtblctl;
277
278 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
279 switch (gcc1 & AGP_I830_GCC1_GMS) {
280 case AGP_I830_GCC1_GMS_STOLEN_512:
281 sc->stolen = (512 - 132) * 1024 / 4096;
282 break;
283 case AGP_I830_GCC1_GMS_STOLEN_1024:
284 sc->stolen = (1024 - 132) * 1024 / 4096;
285 break;
286 case AGP_I830_GCC1_GMS_STOLEN_8192:
287 sc->stolen = (8192 - 132) * 1024 / 4096;
288 break;
289 default:
290 sc->stolen = 0;
291 device_printf(dev, "unknown memory configuration, disabling\n");
292 agp_generic_detach(dev);
293 return EINVAL;
294 }
295 if (sc->stolen > 0)
296 device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
297 device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
298
299 /* GATT address is already in there, make sure it's enabled */
300 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
301#if 0
302 device_printf(dev, "PGTBL_CTL is 0x%08x\n", pgtblctl);
303#endif
304 pgtblctl |= 1;
305 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
306
307 gatt->ag_physical = pgtblctl & ~1;
308 }
309
310 /*
311 * Make sure the chipset can see everything.
312 */
313 agp_flush_cache();
314
315 return 0;
316}
317
318static int
319agp_i810_detach(device_t dev)
320{
321 struct agp_i810_softc *sc = device_get_softc(dev);
322 int error;
323
324 error = agp_generic_detach(dev);
325 if (error)
326 return error;
327
328 /* Clear the GATT base. */
329 if ( sc->chiptype == CHIP_I810 ) {
330 WRITE4(AGP_I810_PGTBL_CTL, 0);
331 } else {
332 unsigned int pgtblctl;
333 pgtblctl = READ4(AGP_I810_PGTBL_CTL);
334 pgtblctl &= ~1;
335 WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
336 }
337
338 /* Put the aperture back the way it started. */
339 AGP_SET_APERTURE(dev, sc->initial_aperture);
340
341 if ( sc->chiptype == CHIP_I810 ) {
342 contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
343 }
344 free(sc->gatt, M_AGP);
345
346 bus_release_resource(dev, SYS_RES_MEMORY,
347 AGP_I810_MMADR, sc->regs);
348
349 return 0;
350}
351
352static u_int32_t
353agp_i810_get_aperture(device_t dev)
354{
355 struct agp_i810_softc *sc = device_get_softc(dev);
356
357 if ( sc->chiptype == CHIP_I810 ) {
358 u_int16_t miscc;
359 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
360 if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
361 return 32 * 1024 * 1024;
362 else
363 return 64 * 1024 * 1024;
364 } else { /* I830 */
365 unsigned int gcc1;
366
367 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
368 if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
369 return 64 * 1024 * 1024;
370 else
371 return 128 * 1024 * 1024;
372 }
373}
374
375static int
376agp_i810_set_aperture(device_t dev, u_int32_t aperture)
377{
378 struct agp_i810_softc *sc = device_get_softc(dev);
379 u_int16_t miscc;
380
381 if ( sc->chiptype == CHIP_I810 ) {
382 /*
383 * Double check for sanity.
384 */
385 if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
386 device_printf(dev, "bad aperture size %d\n", aperture);
387 return EINVAL;
388 }
389
390 miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
391 miscc &= ~AGP_I810_MISCC_WINSIZE;
392 if (aperture == 32 * 1024 * 1024)
393 miscc |= AGP_I810_MISCC_WINSIZE_32;
394 else
395 miscc |= AGP_I810_MISCC_WINSIZE_64;
396
397 pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
398 } else { /* I830 */
399 unsigned int gcc1;
400
401 if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
402 device_printf(dev, "bad aperture size %d\n", aperture);
403 return EINVAL;
404 }
405 gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
406 gcc1 &= ~AGP_I830_GCC1_GMASIZE;
407 if (aperture == 64 * 1024 * 1024)
408 gcc1 |= AGP_I830_GCC1_GMASIZE_64;
409 else
410 gcc1 |= AGP_I830_GCC1_GMASIZE_128;
411
412 pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
413 }
414
415 return 0;
416}
417
418static int
419agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
420{
421 struct agp_i810_softc *sc = device_get_softc(dev);
422
423 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
424 device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
425 return EINVAL;
426 }
427
428 if ( sc->chiptype == CHIP_I830 ) {
429 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
430 device_printf(dev, "trying to bind into stolen memory");
431 return EINVAL;
432 }
433 }
434
435 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
436 return 0;
437}
438
439static int
440agp_i810_unbind_page(device_t dev, int offset)
441{
442 struct agp_i810_softc *sc = device_get_softc(dev);
443
444 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
445 return EINVAL;
446
447 if ( sc->chiptype == CHIP_I830 ) {
448 if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
449 device_printf(dev, "trying to unbind from stolen memory");
450 return EINVAL;
451 }
452 }
453
454 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
455 return 0;
456}
457
458/*
459 * Writing via memory mapped registers already flushes all TLBs.
460 */
461static void
462agp_i810_flush_tlb(device_t dev)
463{
464}
465
466static int
467agp_i810_enable(device_t dev, u_int32_t mode)
468{
469
470 return 0;
471}
472
473static struct agp_memory *
474agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
475{
476 struct agp_i810_softc *sc = device_get_softc(dev);
477 struct agp_memory *mem;
478
479 if ((size & (AGP_PAGE_SIZE - 1)) != 0)
480 return 0;
481
482 if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
483 return 0;
484
485 if (type == 1) {
486 /*
487 * Mapping local DRAM into GATT.
488 */
489 if ( sc->chiptype == CHIP_I830 )
490 return 0;
491 if (size != sc->dcache_size)
492 return 0;
493 } else if (type == 2) {
494 /*
495 * Bogus mapping of a single page for the hardware cursor.
496 */
497 if (size != AGP_PAGE_SIZE)
498 return 0;
499 }
500
501 mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
502 mem->am_id = sc->agp.as_nextid++;
503 mem->am_size = size;
504 mem->am_type = type;
505 if (type != 1)
506 mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
507 atop(round_page(size)));
508 else
509 mem->am_obj = 0;
510
511 if (type == 2) {
512 /*
513 * Allocate and wire down the page now so that we can
514 * get its physical address.
515 */
516 vm_page_t m;
517 m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_ZERO|VM_ALLOC_RETRY);
518 if ((m->flags & PG_ZERO) == 0)
519 vm_page_zero_fill(m);
520 vm_page_wire(m);
521 mem->am_physical = VM_PAGE_TO_PHYS(m);
522 vm_page_wakeup(m);
523 } else {
524 mem->am_physical = 0;
525 }
526
527 mem->am_offset = 0;
528 mem->am_is_bound = 0;
529 TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
530 sc->agp.as_allocated += size;
531
532 return mem;
533}
534
535static int
536agp_i810_free_memory(device_t dev, struct agp_memory *mem)
537{
538 struct agp_i810_softc *sc = device_get_softc(dev);
539
540 if (mem->am_is_bound)
541 return EBUSY;
542
543 if (mem->am_type == 2) {
544 /*
545 * Unwire the page which we wired in alloc_memory.
546 */
547 vm_page_t m = vm_page_lookup(mem->am_obj, 0);
548 vm_page_unwire(m, 0);
549 }
550
551 sc->agp.as_allocated -= mem->am_size;
552 TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
553 if (mem->am_obj)
554 vm_object_deallocate(mem->am_obj);
555 free(mem, M_AGP);
556 return 0;
557}
558
559static int
560agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
561 vm_offset_t offset)
562{
563 struct agp_i810_softc *sc = device_get_softc(dev);
564 vm_offset_t i;
565
566 if (mem->am_type != 1)
567 return agp_generic_bind_memory(dev, mem, offset);
568
569 if ( sc->chiptype == CHIP_I830 )
570 return EINVAL;
571
572 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
573 WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
574 i | 3);
575 }
576
577 return 0;
578}
579
580static int
581agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
582{
583 struct agp_i810_softc *sc = device_get_softc(dev);
584 vm_offset_t i;
585
586 if (mem->am_type != 1)
587 return agp_generic_unbind_memory(dev, mem);
588
589 if ( sc->chiptype == CHIP_I830 )
590 return EINVAL;
591
592 for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
593 WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
594
595 return 0;
596}
597
598static device_method_t agp_i810_methods[] = {
599 /* Device interface */
600 DEVMETHOD(device_probe, agp_i810_probe),
601 DEVMETHOD(device_attach, agp_i810_attach),
602 DEVMETHOD(device_detach, agp_i810_detach),
603 DEVMETHOD(device_shutdown, bus_generic_shutdown),
604 DEVMETHOD(device_suspend, bus_generic_suspend),
605 DEVMETHOD(device_resume, bus_generic_resume),
606
607 /* AGP interface */
608 DEVMETHOD(agp_get_aperture, agp_i810_get_aperture),
609 DEVMETHOD(agp_set_aperture, agp_i810_set_aperture),
610 DEVMETHOD(agp_bind_page, agp_i810_bind_page),
611 DEVMETHOD(agp_unbind_page, agp_i810_unbind_page),
612 DEVMETHOD(agp_flush_tlb, agp_i810_flush_tlb),
613 DEVMETHOD(agp_enable, agp_i810_enable),
614 DEVMETHOD(agp_alloc_memory, agp_i810_alloc_memory),
615 DEVMETHOD(agp_free_memory, agp_i810_free_memory),
616 DEVMETHOD(agp_bind_memory, agp_i810_bind_memory),
617 DEVMETHOD(agp_unbind_memory, agp_i810_unbind_memory),
618
619 { 0, 0 }
620};
621
622static driver_t agp_i810_driver = {
623 "agp",
624 agp_i810_methods,
625 sizeof(struct agp_i810_softc),
626};
627
628static devclass_t agp_devclass;
629
630DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);