drmfb_pci.c revision 1.4 1 /* $NetBSD: drmfb_pci.c,v 1.4 2021/12/19 10:33:00 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * drmfb_pci: drmfb hooks for PCI devices.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: drmfb_pci.c,v 1.4 2021/12/19 10:33:00 riastradh Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "vga.h"
41 #endif
42
43 #include <sys/types.h>
44 #include <sys/device.h>
45 #include <sys/errno.h>
46 #include <sys/systm.h>
47
48 #include <dev/pci/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/wsdisplay_pci.h>
52
53 #if NVGA > 0
54 /*
55 * XXX All we really need is vga_is_console from vgavar.h, but the
56 * header files are missing their own dependencies, so we need to
57 * explicitly drag in the other crap.
58 */
59 #include <dev/ic/mc6845reg.h>
60 #include <dev/ic/pcdisplayvar.h>
61 #include <dev/ic/vgareg.h>
62 #include <dev/ic/vgavar.h>
63 #endif
64
65 #include <drm/drm_device.h>
66 #include <drm/drm_fb_helper.h>
67
68 #include <drm/drmfb.h>
69 #include <drm/drmfb_pci.h>
70
71 /*
72 * drmfb_pci_mmap: Implementation of drmfb_params::dp_mmap. Don't use
73 * this for dp_mmapfb -- how to get at the framebuffer is device-
74 * specific.
75 */
76 paddr_t
77 drmfb_pci_mmap(struct drmfb_softc *sc, off_t offset, int prot)
78 {
79 struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
80 const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
81 unsigned i;
82
83 for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
84 pcireg_t type;
85 bus_addr_t addr;
86 bus_size_t size;
87 int flags;
88
89 /* Interrogate the BAR. */
90 if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
91 &type))
92 continue;
93 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
94 continue;
95 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
96 &addr, &size, &flags))
97 continue;
98
99 /* Try to map it if it's in range. */
100 if ((addr <= offset) && (offset < (addr + size)))
101 return bus_space_mmap(pa->pa_memt, addr,
102 (offset - addr), prot, flags);
103
104 /* Skip a slot if this was a 64-bit BAR. */
105 if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
106 (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
107 i += 1;
108 }
109
110 /* Failure! */
111 return -1;
112 }
113
114 /*
115 * drmfb_pci_ioctl: Implementation of drmfb_params::dp_ioctl. Provides:
116 *
117 * - WSDISPLAY_GET_BUSID
118 * - WSDISPLAY_GTYPE
119 *
120 * Additionally allows access to PCI registers.
121 *
122 * XXX Do we need to provide access to PCI registers? I can't find
123 * anything that uses this functionality, and as is it is very
124 * dangerous.
125 */
126 int
127 drmfb_pci_ioctl(struct drmfb_softc *sc, unsigned long cmd, void *data,
128 int flag, struct lwp *l)
129 {
130 struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
131 struct pci_attach_args *const pa = &dev->pdev->pd_pa;
132
133 switch (cmd) {
134 /* PCI config read/write passthrough. */
135 case PCI_IOC_CFGREAD:
136 case PCI_IOC_CFGWRITE:
137 return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
138
139 /* PCI-specific wsdisplay ioctls. */
140 case WSDISPLAYIO_GET_BUSID:
141 return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
142 data);
143 case WSDISPLAYIO_GTYPE:
144 *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
145 return 0;
146
147 default:
148 return EPASSTHROUGH;
149 }
150 }
151
152 bool
153 drmfb_pci_is_vga_console(struct drm_device *dev)
154 {
155
156 #if NVGA > 0
157 return vga_is_console(dev->pdev->pd_pa.pa_iot, -1);
158 #else
159 return false;
160 #endif
161 }
162