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