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