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