]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/pci/vga_pci.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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__) || defined(__ia64__)
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 struct vga_resource {
55         struct resource *vr_res;
56         int     vr_refs;
57 };
58
59 struct vga_pci_softc {
60         device_t        vga_msi_child;  /* Child driver using MSI. */
61         struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
62         struct vga_resource vga_bios;
63 };
64
65 SYSCTL_DECL(_hw_pci);
66
67 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
68 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
69     int type, int *rid, u_long start, u_long end, u_long count, u_int flags);
70 static int      vga_pci_release_resource(device_t dev, device_t child, int type,
71     int rid, struct resource *r);
72
73 int vga_pci_default_unit = -1;
74 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
75 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
76     &vga_pci_default_unit, -1, "Default VGA-compatible display");
77
78 int
79 vga_pci_is_boot_display(device_t dev)
80 {
81         int unit;
82         device_t pcib;
83         uint16_t config;
84
85         /* Check that the given device is a video card */
86         if ((pci_get_class(dev) != PCIC_DISPLAY &&
87             (pci_get_class(dev) != PCIC_OLD ||
88              pci_get_subclass(dev) != PCIS_OLD_VGA)))
89                 return (0);
90
91         unit = device_get_unit(dev);
92
93         if (vga_pci_default_unit >= 0) {
94                 /*
95                  * The boot display device was determined by a previous
96                  * call to this function, or the user forced it using
97                  * the hw.pci.default_vgapci_unit tunable.
98                  */
99                 return (vga_pci_default_unit == unit);
100         }
101
102         /*
103          * The primary video card used as a boot display must have the
104          * "I/O" and "Memory Address Space Decoding" bits set in its
105          * Command register.
106          *
107          * Furthermore, if the card is attached to a bridge, instead of
108          * the root PCI bus, the bridge must have the "VGA Enable" bit
109          * set in its Control register.
110          */
111
112         pcib = device_get_parent(device_get_parent(dev));
113         if (device_get_devclass(device_get_parent(pcib)) ==
114             devclass_find("pci")) {
115                 /*
116                  * The parent bridge is a PCI-to-PCI bridge: check the
117                  * value of the "VGA Enable" bit.
118                  */
119                 config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
120                 if ((config & PCIB_BCR_VGA_ENABLE) == 0)
121                         return (0);
122         }
123
124         config = pci_read_config(dev, PCIR_COMMAND, 2);
125         if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
126                 return (0);
127
128         /*
129          * Disable interrupts until a chipset driver is loaded for
130          * this PCI device. Else unhandled display adapter interrupts
131          * might freeze the CPU.
132          */
133         pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
134
135         /* This video card is the boot display: record its unit number. */
136         vga_pci_default_unit = unit;
137         device_set_flags(dev, 1);
138
139         return (1);
140 }
141
142 void *
143 vga_pci_map_bios(device_t dev, size_t *size)
144 {
145         int rid;
146         struct resource *res;
147
148 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
149         if (vga_pci_is_boot_display(dev)) {
150                 /*
151                  * On x86, the System BIOS copy the default display
152                  * device's Video BIOS at a fixed location in system
153                  * memory (0xC0000, 128 kBytes long) at boot time.
154                  *
155                  * We use this copy for the default boot device, because
156                  * the original ROM may not be valid after boot.
157                  */
158
159                 *size = VGA_PCI_BIOS_SHADOW_SIZE;
160                 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
161         }
162 #endif
163
164         rid = PCIR_BIOS;
165         res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
166             ~0ul, 1, RF_ACTIVE);
167         if (res == NULL) {
168                 return (NULL);
169         }
170
171         *size = rman_get_size(res);
172         return (rman_get_virtual(res));
173 }
174
175 void
176 vga_pci_unmap_bios(device_t dev, void *bios)
177 {
178         struct vga_resource *vr;
179
180         if (bios == NULL) {
181                 return;
182         }
183
184 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
185         if (vga_pci_is_boot_display(dev)) {
186                 /* We mapped the BIOS shadow copy located at 0xC0000. */
187                 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
188
189                 return;
190         }
191 #endif
192
193         /*
194          * Look up the PCIR_BIOS resource in our softc.  It should match
195          * the address we returned previously.
196          */
197         vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
198         KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
199         KASSERT(rman_get_virtual(vr->vr_res) == bios,
200             ("vga_pci_unmap_bios: mismatch"));
201         vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
202             vr->vr_res);
203 }
204
205 static int
206 vga_pci_probe(device_t dev)
207 {
208
209         switch (pci_get_class(dev)) {
210         case PCIC_DISPLAY:
211                 break;
212         case PCIC_OLD:
213                 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
214                         return (ENXIO);
215                 break;
216         default:
217                 return (ENXIO);
218         }
219
220         /* Probe default display. */
221         vga_pci_is_boot_display(dev);
222
223         device_set_desc(dev, "VGA-compatible display");
224         return (BUS_PROBE_GENERIC);
225 }
226
227 static int
228 vga_pci_attach(device_t dev)
229 {
230
231         bus_generic_probe(dev);
232
233         /* Always create a drm child for now to make it easier on drm. */
234         device_add_child(dev, "drm", -1);
235         device_add_child(dev, "drmn", -1);
236         bus_generic_attach(dev);
237
238         if (vga_pci_is_boot_display(dev))
239                 device_printf(dev, "Boot video device\n");
240
241         return (0);
242 }
243
244 static int
245 vga_pci_suspend(device_t dev)
246 {
247
248         return (bus_generic_suspend(dev));
249 }
250
251 static int
252 vga_pci_resume(device_t dev)
253 {
254
255         return (bus_generic_resume(dev));
256 }
257
258 /* Bus interface. */
259
260 static int
261 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
262 {
263
264         return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
265 }
266
267 static int
268 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
269 {
270
271         return (EINVAL);
272 }
273
274 static int
275 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
276     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
277     void **cookiep)
278 {
279         return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
280             filter, intr, arg, cookiep));
281 }
282
283 static int
284 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
285     void *cookie)
286 {
287         return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
288 }
289
290 static struct vga_resource *
291 lookup_res(struct vga_pci_softc *sc, int rid)
292 {
293         int bar;
294
295         if (rid == PCIR_BIOS)
296                 return (&sc->vga_bios);
297         bar = PCI_RID2BAR(rid);
298         if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
299                 return (&sc->vga_bars[bar]);
300         return (NULL);
301 }
302
303 static struct resource *
304 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
305     u_long start, u_long end, u_long count, u_int flags)
306 {
307         struct vga_resource *vr;
308
309         switch (type) {
310         case SYS_RES_MEMORY:
311         case SYS_RES_IOPORT:
312                 /*
313                  * For BARs, we cache the resource so that we only allocate it
314                  * from the PCI bus once.
315                  */
316                 vr = lookup_res(device_get_softc(dev), *rid);
317                 if (vr == NULL)
318                         return (NULL);
319                 if (vr->vr_res == NULL)
320                         vr->vr_res = bus_alloc_resource(dev, type, rid, start,
321                             end, count, flags);
322                 if (vr->vr_res != NULL)
323                         vr->vr_refs++;
324                 return (vr->vr_res);
325         }
326         return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
327 }
328
329 static int
330 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
331     struct resource *r)
332 {
333         struct vga_resource *vr;
334         int error;
335
336         switch (type) {
337         case SYS_RES_MEMORY:
338         case SYS_RES_IOPORT:
339                 /*
340                  * For BARs, we release the resource from the PCI bus
341                  * when the last child reference goes away.
342                  */
343                 vr = lookup_res(device_get_softc(dev), rid);
344                 if (vr == NULL)
345                         return (EINVAL);
346                 if (vr->vr_res == NULL)
347                         return (EINVAL);
348                 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
349                 if (vr->vr_refs > 1) {
350                         vr->vr_refs--;
351                         return (0);
352                 }
353                 KASSERT(vr->vr_refs > 0,
354                     ("vga_pci resource reference count underflow"));
355                 error = bus_release_resource(dev, type, rid, r);
356                 if (error == 0) {
357                         vr->vr_res = NULL;
358                         vr->vr_refs = 0;
359                 }
360                 return (error);
361         }
362
363         return (bus_release_resource(dev, type, rid, r));
364 }
365
366 /* PCI interface. */
367
368 static uint32_t
369 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
370 {
371
372         return (pci_read_config(dev, reg, width));
373 }
374
375 static void
376 vga_pci_write_config(device_t dev, device_t child, int reg,
377     uint32_t val, int width)
378 {
379
380         pci_write_config(dev, reg, val, width);
381 }
382
383 static int
384 vga_pci_enable_busmaster(device_t dev, device_t child)
385 {
386
387         return (pci_enable_busmaster(dev));
388 }
389
390 static int
391 vga_pci_disable_busmaster(device_t dev, device_t child)
392 {
393
394         return (pci_disable_busmaster(dev));
395 }
396
397 static int
398 vga_pci_enable_io(device_t dev, device_t child, int space)
399 {
400
401         device_printf(dev, "child %s requested pci_enable_io\n",
402             device_get_nameunit(child));
403         return (pci_enable_io(dev, space));
404 }
405
406 static int
407 vga_pci_disable_io(device_t dev, device_t child, int space)
408 {
409
410         device_printf(dev, "child %s requested pci_disable_io\n",
411             device_get_nameunit(child));
412         return (pci_disable_io(dev, space));
413 }
414
415 static int
416 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
417 {
418
419         return (pci_get_vpd_ident(dev, identptr));
420 }
421
422 static int
423 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
424     const char **vptr)
425 {
426
427         return (pci_get_vpd_readonly(dev, kw, vptr));
428 }
429
430 static int
431 vga_pci_set_powerstate(device_t dev, device_t child, int state)
432 {
433
434         device_printf(dev, "child %s requested pci_set_powerstate\n",
435             device_get_nameunit(child));
436         return (pci_set_powerstate(dev, state));
437 }
438
439 static int
440 vga_pci_get_powerstate(device_t dev, device_t child)
441 {
442
443         device_printf(dev, "child %s requested pci_get_powerstate\n",
444             device_get_nameunit(child));
445         return (pci_get_powerstate(dev));
446 }
447
448 static int
449 vga_pci_assign_interrupt(device_t dev, device_t child)
450 {
451
452         device_printf(dev, "child %s requested pci_assign_interrupt\n",
453             device_get_nameunit(child));
454         return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
455 }
456
457 static int
458 vga_pci_find_cap(device_t dev, device_t child, int capability,
459     int *capreg)
460 {
461
462         return (pci_find_cap(dev, capability, capreg));
463 }
464
465 static int
466 vga_pci_find_extcap(device_t dev, device_t child, int capability,
467     int *capreg)
468 {
469
470         return (pci_find_extcap(dev, capability, capreg));
471 }
472
473 static int
474 vga_pci_find_htcap(device_t dev, device_t child, int capability,
475     int *capreg)
476 {
477
478         return (pci_find_htcap(dev, capability, capreg));
479 }
480
481 static int
482 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
483 {
484         struct vga_pci_softc *sc;
485         int error;
486
487         sc = device_get_softc(dev);
488         if (sc->vga_msi_child != NULL)
489                 return (EBUSY);
490         error = pci_alloc_msi(dev, count);
491         if (error == 0)
492                 sc->vga_msi_child = child;
493         return (error);
494 }
495
496 static int
497 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
498 {
499         struct vga_pci_softc *sc;
500         int error;
501
502         sc = device_get_softc(dev);
503         if (sc->vga_msi_child != NULL)
504                 return (EBUSY);
505         error = pci_alloc_msix(dev, count);
506         if (error == 0)
507                 sc->vga_msi_child = child;
508         return (error);
509 }
510
511 static int
512 vga_pci_remap_msix(device_t dev, device_t child, int count,
513     const u_int *vectors)
514 {
515         struct vga_pci_softc *sc;
516
517         sc = device_get_softc(dev);
518         if (sc->vga_msi_child != child)
519                 return (ENXIO);
520         return (pci_remap_msix(dev, count, vectors));
521 }
522
523 static int
524 vga_pci_release_msi(device_t dev, device_t child)
525 {
526         struct vga_pci_softc *sc;
527         int error;
528
529         sc = device_get_softc(dev);
530         if (sc->vga_msi_child != child)
531                 return (ENXIO);
532         error = pci_release_msi(dev);
533         if (error == 0)
534                 sc->vga_msi_child = NULL;
535         return (error);
536 }
537
538 static int
539 vga_pci_msi_count(device_t dev, device_t child)
540 {
541
542         return (pci_msi_count(dev));
543 }
544
545 static int
546 vga_pci_msix_count(device_t dev, device_t child)
547 {
548
549         return (pci_msix_count(dev));
550 }
551
552 static bus_dma_tag_t
553 vga_pci_get_dma_tag(device_t bus, device_t child)
554 {
555
556         return (bus_get_dma_tag(bus));
557 }
558
559 static device_method_t vga_pci_methods[] = {
560         /* Device interface */
561         DEVMETHOD(device_probe,         vga_pci_probe),
562         DEVMETHOD(device_attach,        vga_pci_attach),
563         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
564         DEVMETHOD(device_suspend,       vga_pci_suspend),
565         DEVMETHOD(device_resume,        vga_pci_resume),
566
567         /* Bus interface */
568         DEVMETHOD(bus_read_ivar,        vga_pci_read_ivar),
569         DEVMETHOD(bus_write_ivar,       vga_pci_write_ivar),
570         DEVMETHOD(bus_setup_intr,       vga_pci_setup_intr),
571         DEVMETHOD(bus_teardown_intr,    vga_pci_teardown_intr),
572         DEVMETHOD(bus_alloc_resource,   vga_pci_alloc_resource),
573         DEVMETHOD(bus_release_resource, vga_pci_release_resource),
574         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
575         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
576         DEVMETHOD(bus_get_dma_tag,      vga_pci_get_dma_tag),
577
578         /* PCI interface */
579         DEVMETHOD(pci_read_config,      vga_pci_read_config),
580         DEVMETHOD(pci_write_config,     vga_pci_write_config),
581         DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
582         DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
583         DEVMETHOD(pci_enable_io,        vga_pci_enable_io),
584         DEVMETHOD(pci_disable_io,       vga_pci_disable_io),
585         DEVMETHOD(pci_get_vpd_ident,    vga_pci_get_vpd_ident),
586         DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
587         DEVMETHOD(pci_get_powerstate,   vga_pci_get_powerstate),
588         DEVMETHOD(pci_set_powerstate,   vga_pci_set_powerstate),
589         DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
590         DEVMETHOD(pci_find_cap,         vga_pci_find_cap),
591         DEVMETHOD(pci_find_extcap,      vga_pci_find_extcap),
592         DEVMETHOD(pci_find_htcap,       vga_pci_find_htcap),
593         DEVMETHOD(pci_alloc_msi,        vga_pci_alloc_msi),
594         DEVMETHOD(pci_alloc_msix,       vga_pci_alloc_msix),
595         DEVMETHOD(pci_remap_msix,       vga_pci_remap_msix),
596         DEVMETHOD(pci_release_msi,      vga_pci_release_msi),
597         DEVMETHOD(pci_msi_count,        vga_pci_msi_count),
598         DEVMETHOD(pci_msix_count,       vga_pci_msix_count),
599
600         { 0, 0 }
601 };
602
603 static driver_t vga_pci_driver = {
604         "vgapci",
605         vga_pci_methods,
606         sizeof(struct vga_pci_softc),
607 };
608
609 static devclass_t vga_devclass;
610
611 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);