]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pci/vga_pci.c
MFV r323913: 8600 ZFS channel programs - snapshot
[FreeBSD/FreeBSD.git] / sys / dev / pci / vga_pci.c
1 /*-
2  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
32  * drm(4) should attach as children of this device.
33  *
34  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
35  * in or rename it.
36  */
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45
46 #if defined(__amd64__) || defined(__i386__)
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 #endif
50
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53
54 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */
55
56 struct vga_resource {
57         struct resource *vr_res;
58         int     vr_refs;
59 };
60
61 struct vga_pci_softc {
62         device_t        vga_msi_child;  /* Child driver using MSI. */
63         struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
64         struct vga_resource vga_bios;
65 };
66
67 SYSCTL_DECL(_hw_pci);
68
69 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
70 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
71     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
72     u_int flags);
73 static int      vga_pci_release_resource(device_t dev, device_t child, int type,
74     int rid, struct resource *r);
75
76 int vga_pci_default_unit = -1;
77 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
78     &vga_pci_default_unit, -1, "Default VGA-compatible display");
79
80 int
81 vga_pci_is_boot_display(device_t dev)
82 {
83         int unit;
84         device_t pcib;
85         uint16_t config;
86
87         /* Check that the given device is a video card */
88         if ((pci_get_class(dev) != PCIC_DISPLAY &&
89             (pci_get_class(dev) != PCIC_OLD ||
90              pci_get_subclass(dev) != PCIS_OLD_VGA)))
91                 return (0);
92
93         unit = device_get_unit(dev);
94
95         if (vga_pci_default_unit >= 0) {
96                 /*
97                  * The boot display device was determined by a previous
98                  * call to this function, or the user forced it using
99                  * the hw.pci.default_vgapci_unit tunable.
100                  */
101                 return (vga_pci_default_unit == unit);
102         }
103
104         /*
105          * The primary video card used as a boot display must have the
106          * "I/O" and "Memory Address Space Decoding" bits set in its
107          * Command register.
108          *
109          * Furthermore, if the card is attached to a bridge, instead of
110          * the root PCI bus, the bridge must have the "VGA Enable" bit
111          * set in its Control register.
112          */
113
114         pcib = device_get_parent(device_get_parent(dev));
115         if (device_get_devclass(device_get_parent(pcib)) ==
116             devclass_find("pci")) {
117                 /*
118                  * The parent bridge is a PCI-to-PCI bridge: check the
119                  * value of the "VGA Enable" bit.
120                  */
121                 config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
122                 if ((config & PCIB_BCR_VGA_ENABLE) == 0)
123                         return (0);
124         }
125
126         config = pci_read_config(dev, PCIR_COMMAND, 2);
127         if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
128                 return (0);
129
130         /*
131          * Disable interrupts until a chipset driver is loaded for
132          * this PCI device. Else unhandled display adapter interrupts
133          * might freeze the CPU.
134          */
135         pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
136
137         /* This video card is the boot display: record its unit number. */
138         vga_pci_default_unit = unit;
139         device_set_flags(dev, 1);
140
141         return (1);
142 }
143
144 void *
145 vga_pci_map_bios(device_t dev, size_t *size)
146 {
147         int rid;
148         struct resource *res;
149
150 #if defined(__amd64__) || defined(__i386__)
151         if (vga_pci_is_boot_display(dev)) {
152                 /*
153                  * On x86, the System BIOS copy the default display
154                  * device's Video BIOS at a fixed location in system
155                  * memory (0xC0000, 128 kBytes long) at boot time.
156                  *
157                  * We use this copy for the default boot device, because
158                  * the original ROM may not be valid after boot.
159                  */
160
161                 *size = VGA_PCI_BIOS_SHADOW_SIZE;
162                 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
163         }
164 #endif
165
166         rid = PCIR_BIOS;
167         res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0,
168             ~0, 1, RF_ACTIVE);
169         if (res == NULL) {
170                 return (NULL);
171         }
172
173         *size = rman_get_size(res);
174         return (rman_get_virtual(res));
175 }
176
177 void
178 vga_pci_unmap_bios(device_t dev, void *bios)
179 {
180         struct vga_resource *vr;
181
182         if (bios == NULL) {
183                 return;
184         }
185
186 #if defined(__amd64__) || defined(__i386__)
187         if (vga_pci_is_boot_display(dev)) {
188                 /* We mapped the BIOS shadow copy located at 0xC0000. */
189                 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
190
191                 return;
192         }
193 #endif
194
195         /*
196          * Look up the PCIR_BIOS resource in our softc.  It should match
197          * the address we returned previously.
198          */
199         vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
200         KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
201         KASSERT(rman_get_virtual(vr->vr_res) == bios,
202             ("vga_pci_unmap_bios: mismatch"));
203         vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
204             vr->vr_res);
205 }
206
207 int
208 vga_pci_repost(device_t dev)
209 {
210 #if defined(__amd64__) || defined(__i386__)
211         x86regs_t regs;
212
213         if (!vga_pci_is_boot_display(dev))
214                 return (EINVAL);
215
216         if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL)
217                 return (ENOTSUP);
218
219         x86bios_init_regs(&regs);
220
221         regs.R_AH = pci_get_bus(dev);
222         regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07);
223         regs.R_DL = 0x80;
224
225         device_printf(dev, "REPOSTing\n");
226         x86bios_call(&regs, X86BIOS_PHYSTOSEG(VGA_PCI_BIOS_SHADOW_ADDR + 3),
227             X86BIOS_PHYSTOOFF(VGA_PCI_BIOS_SHADOW_ADDR + 3));
228
229         x86bios_get_intr(0x10);
230
231         return (0);
232 #else
233         return (ENOTSUP);
234 #endif
235 }
236
237 static int
238 vga_pci_probe(device_t dev)
239 {
240
241         switch (pci_get_class(dev)) {
242         case PCIC_DISPLAY:
243                 break;
244         case PCIC_OLD:
245                 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
246                         return (ENXIO);
247                 break;
248         default:
249                 return (ENXIO);
250         }
251
252         /* Probe default display. */
253         vga_pci_is_boot_display(dev);
254
255         device_set_desc(dev, "VGA-compatible display");
256         return (BUS_PROBE_GENERIC);
257 }
258
259 static int
260 vga_pci_attach(device_t dev)
261 {
262
263         bus_generic_probe(dev);
264
265         /* Always create a drm child for now to make it easier on drm. */
266         device_add_child(dev, "drm", -1);
267         device_add_child(dev, "drmn", -1);
268         bus_generic_attach(dev);
269
270         if (vga_pci_is_boot_display(dev))
271                 device_printf(dev, "Boot video device\n");
272
273         return (0);
274 }
275
276 static int
277 vga_pci_suspend(device_t dev)
278 {
279
280         return (bus_generic_suspend(dev));
281 }
282
283 static int
284 vga_pci_resume(device_t dev)
285 {
286
287         return (bus_generic_resume(dev));
288 }
289
290 /* Bus interface. */
291
292 static int
293 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
294 {
295
296         return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
297 }
298
299 static int
300 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
301 {
302
303         return (EINVAL);
304 }
305
306 static int
307 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
308     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
309     void **cookiep)
310 {
311         return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
312             filter, intr, arg, cookiep));
313 }
314
315 static int
316 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
317     void *cookie)
318 {
319         return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
320 }
321
322 static struct vga_resource *
323 lookup_res(struct vga_pci_softc *sc, int rid)
324 {
325         int bar;
326
327         if (rid == PCIR_BIOS)
328                 return (&sc->vga_bios);
329         bar = PCI_RID2BAR(rid);
330         if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
331                 return (&sc->vga_bars[bar]);
332         return (NULL);
333 }
334
335 static struct resource *
336 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
337     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
338 {
339         struct vga_resource *vr;
340
341         switch (type) {
342         case SYS_RES_MEMORY:
343         case SYS_RES_IOPORT:
344                 /*
345                  * For BARs, we cache the resource so that we only allocate it
346                  * from the PCI bus once.
347                  */
348                 vr = lookup_res(device_get_softc(dev), *rid);
349                 if (vr == NULL)
350                         return (NULL);
351                 if (vr->vr_res == NULL)
352                         vr->vr_res = bus_alloc_resource(dev, type, rid, start,
353                             end, count, flags);
354                 if (vr->vr_res != NULL)
355                         vr->vr_refs++;
356                 return (vr->vr_res);
357         }
358         return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
359 }
360
361 static int
362 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
363     struct resource *r)
364 {
365         struct vga_resource *vr;
366         int error;
367
368         switch (type) {
369         case SYS_RES_MEMORY:
370         case SYS_RES_IOPORT:
371                 /*
372                  * For BARs, we release the resource from the PCI bus
373                  * when the last child reference goes away.
374                  */
375                 vr = lookup_res(device_get_softc(dev), rid);
376                 if (vr == NULL)
377                         return (EINVAL);
378                 if (vr->vr_res == NULL)
379                         return (EINVAL);
380                 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
381                 if (vr->vr_refs > 1) {
382                         vr->vr_refs--;
383                         return (0);
384                 }
385                 KASSERT(vr->vr_refs > 0,
386                     ("vga_pci resource reference count underflow"));
387                 error = bus_release_resource(dev, type, rid, r);
388                 if (error == 0) {
389                         vr->vr_res = NULL;
390                         vr->vr_refs = 0;
391                 }
392                 return (error);
393         }
394
395         return (bus_release_resource(dev, type, rid, r));
396 }
397
398 /* PCI interface. */
399
400 static uint32_t
401 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
402 {
403
404         return (pci_read_config(dev, reg, width));
405 }
406
407 static void
408 vga_pci_write_config(device_t dev, device_t child, int reg,
409     uint32_t val, int width)
410 {
411
412         pci_write_config(dev, reg, val, width);
413 }
414
415 static int
416 vga_pci_enable_busmaster(device_t dev, device_t child)
417 {
418
419         return (pci_enable_busmaster(dev));
420 }
421
422 static int
423 vga_pci_disable_busmaster(device_t dev, device_t child)
424 {
425
426         return (pci_disable_busmaster(dev));
427 }
428
429 static int
430 vga_pci_enable_io(device_t dev, device_t child, int space)
431 {
432
433         device_printf(dev, "child %s requested pci_enable_io\n",
434             device_get_nameunit(child));
435         return (pci_enable_io(dev, space));
436 }
437
438 static int
439 vga_pci_disable_io(device_t dev, device_t child, int space)
440 {
441
442         device_printf(dev, "child %s requested pci_disable_io\n",
443             device_get_nameunit(child));
444         return (pci_disable_io(dev, space));
445 }
446
447 static int
448 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
449 {
450
451         return (pci_get_vpd_ident(dev, identptr));
452 }
453
454 static int
455 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
456     const char **vptr)
457 {
458
459         return (pci_get_vpd_readonly(dev, kw, vptr));
460 }
461
462 static int
463 vga_pci_set_powerstate(device_t dev, device_t child, int state)
464 {
465
466         device_printf(dev, "child %s requested pci_set_powerstate\n",
467             device_get_nameunit(child));
468         return (pci_set_powerstate(dev, state));
469 }
470
471 static int
472 vga_pci_get_powerstate(device_t dev, device_t child)
473 {
474
475         device_printf(dev, "child %s requested pci_get_powerstate\n",
476             device_get_nameunit(child));
477         return (pci_get_powerstate(dev));
478 }
479
480 static int
481 vga_pci_assign_interrupt(device_t dev, device_t child)
482 {
483
484         device_printf(dev, "child %s requested pci_assign_interrupt\n",
485             device_get_nameunit(child));
486         return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
487 }
488
489 static int
490 vga_pci_find_cap(device_t dev, device_t child, int capability,
491     int *capreg)
492 {
493
494         return (pci_find_cap(dev, capability, capreg));
495 }
496
497 static int
498 vga_pci_find_extcap(device_t dev, device_t child, int capability,
499     int *capreg)
500 {
501
502         return (pci_find_extcap(dev, capability, capreg));
503 }
504
505 static int
506 vga_pci_find_htcap(device_t dev, device_t child, int capability,
507     int *capreg)
508 {
509
510         return (pci_find_htcap(dev, capability, capreg));
511 }
512
513 static int
514 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
515 {
516         struct vga_pci_softc *sc;
517         int error;
518
519         sc = device_get_softc(dev);
520         if (sc->vga_msi_child != NULL)
521                 return (EBUSY);
522         error = pci_alloc_msi(dev, count);
523         if (error == 0)
524                 sc->vga_msi_child = child;
525         return (error);
526 }
527
528 static int
529 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
530 {
531         struct vga_pci_softc *sc;
532         int error;
533
534         sc = device_get_softc(dev);
535         if (sc->vga_msi_child != NULL)
536                 return (EBUSY);
537         error = pci_alloc_msix(dev, count);
538         if (error == 0)
539                 sc->vga_msi_child = child;
540         return (error);
541 }
542
543 static int
544 vga_pci_remap_msix(device_t dev, device_t child, int count,
545     const u_int *vectors)
546 {
547         struct vga_pci_softc *sc;
548
549         sc = device_get_softc(dev);
550         if (sc->vga_msi_child != child)
551                 return (ENXIO);
552         return (pci_remap_msix(dev, count, vectors));
553 }
554
555 static int
556 vga_pci_release_msi(device_t dev, device_t child)
557 {
558         struct vga_pci_softc *sc;
559         int error;
560
561         sc = device_get_softc(dev);
562         if (sc->vga_msi_child != child)
563                 return (ENXIO);
564         error = pci_release_msi(dev);
565         if (error == 0)
566                 sc->vga_msi_child = NULL;
567         return (error);
568 }
569
570 static int
571 vga_pci_msi_count(device_t dev, device_t child)
572 {
573
574         return (pci_msi_count(dev));
575 }
576
577 static int
578 vga_pci_msix_count(device_t dev, device_t child)
579 {
580
581         return (pci_msix_count(dev));
582 }
583
584 static bus_dma_tag_t
585 vga_pci_get_dma_tag(device_t bus, device_t child)
586 {
587
588         return (bus_get_dma_tag(bus));
589 }
590
591 static device_method_t vga_pci_methods[] = {
592         /* Device interface */
593         DEVMETHOD(device_probe,         vga_pci_probe),
594         DEVMETHOD(device_attach,        vga_pci_attach),
595         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
596         DEVMETHOD(device_suspend,       vga_pci_suspend),
597         DEVMETHOD(device_resume,        vga_pci_resume),
598
599         /* Bus interface */
600         DEVMETHOD(bus_read_ivar,        vga_pci_read_ivar),
601         DEVMETHOD(bus_write_ivar,       vga_pci_write_ivar),
602         DEVMETHOD(bus_setup_intr,       vga_pci_setup_intr),
603         DEVMETHOD(bus_teardown_intr,    vga_pci_teardown_intr),
604         DEVMETHOD(bus_alloc_resource,   vga_pci_alloc_resource),
605         DEVMETHOD(bus_release_resource, vga_pci_release_resource),
606         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
607         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
608         DEVMETHOD(bus_get_dma_tag,      vga_pci_get_dma_tag),
609
610         /* PCI interface */
611         DEVMETHOD(pci_read_config,      vga_pci_read_config),
612         DEVMETHOD(pci_write_config,     vga_pci_write_config),
613         DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
614         DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
615         DEVMETHOD(pci_enable_io,        vga_pci_enable_io),
616         DEVMETHOD(pci_disable_io,       vga_pci_disable_io),
617         DEVMETHOD(pci_get_vpd_ident,    vga_pci_get_vpd_ident),
618         DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
619         DEVMETHOD(pci_get_powerstate,   vga_pci_get_powerstate),
620         DEVMETHOD(pci_set_powerstate,   vga_pci_set_powerstate),
621         DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
622         DEVMETHOD(pci_find_cap,         vga_pci_find_cap),
623         DEVMETHOD(pci_find_extcap,      vga_pci_find_extcap),
624         DEVMETHOD(pci_find_htcap,       vga_pci_find_htcap),
625         DEVMETHOD(pci_alloc_msi,        vga_pci_alloc_msi),
626         DEVMETHOD(pci_alloc_msix,       vga_pci_alloc_msix),
627         DEVMETHOD(pci_remap_msix,       vga_pci_remap_msix),
628         DEVMETHOD(pci_release_msi,      vga_pci_release_msi),
629         DEVMETHOD(pci_msi_count,        vga_pci_msi_count),
630         DEVMETHOD(pci_msix_count,       vga_pci_msix_count),
631
632         { 0, 0 }
633 };
634
635 static driver_t vga_pci_driver = {
636         "vgapci",
637         vga_pci_methods,
638         sizeof(struct vga_pci_softc),
639 };
640
641 static devclass_t vga_devclass;
642
643 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
644 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1);