1 /* $NetBSD: drmfb_pci.c,v 1.5 2021/12/19 10:37:32 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.5 2021/12/19 10:37:32 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 <linux/pci.h>
66
67 #include <drm/drm_device.h>
68 #include <drm/drm_fb_helper.h>
69
70 #include <drm/drmfb.h>
71 #include <drm/drmfb_pci.h>
72
73 /*
74 * drmfb_pci_mmap: Implementation of drmfb_params::dp_mmap. Don't use
75 * this for dp_mmapfb -- how to get at the framebuffer is device-
76 * specific.
77 */
78 paddr_t
drmfb_pci_mmap(struct drmfb_softc * sc,off_t offset,int prot)79 drmfb_pci_mmap(struct drmfb_softc *sc, off_t offset, int prot)
80 {
81 struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
82 const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
83 unsigned i;
84
85 for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
86 pcireg_t type;
87 bus_addr_t addr;
88 bus_size_t size;
89 int flags;
90
91 /* Interrogate the BAR. */
92 if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
93 &type))
94 continue;
95 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
96 continue;
97 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
98 &addr, &size, &flags))
99 continue;
100
101 /* Try to map it if it's in range. */
102 if ((addr <= offset) && (offset < (addr + size)))
103 return bus_space_mmap(pa->pa_memt, addr,
104 (offset - addr), prot, flags);
105
106 /* Skip a slot if this was a 64-bit BAR. */
107 if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
108 (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
109 i += 1;
110 }
111
112 /* Failure! */
113 return -1;
114 }
115
116 /*
117 * drmfb_pci_ioctl: Implementation of drmfb_params::dp_ioctl. Provides:
118 *
119 * - WSDISPLAY_GET_BUSID
120 * - WSDISPLAY_GTYPE
121 *
122 * Additionally allows access to PCI registers.
123 *
124 * XXX Do we need to provide access to PCI registers? I can't find
125 * anything that uses this functionality, and as is it is very
126 * dangerous.
127 */
128 int
drmfb_pci_ioctl(struct drmfb_softc * sc,unsigned long cmd,void * data,int flag,struct lwp * l)129 drmfb_pci_ioctl(struct drmfb_softc *sc, unsigned long cmd, void *data,
130 int flag, struct lwp *l)
131 {
132 struct drm_device *const dev = sc->sc_da.da_fb_helper->dev;
133 struct pci_attach_args *const pa = &dev->pdev->pd_pa;
134
135 switch (cmd) {
136 /* PCI config read/write passthrough. */
137 case PCI_IOC_CFGREAD:
138 case PCI_IOC_CFGWRITE:
139 return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
140
141 /* PCI-specific wsdisplay ioctls. */
142 case WSDISPLAYIO_GET_BUSID:
143 return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
144 data);
145 case WSDISPLAYIO_GTYPE:
146 *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
147 return 0;
148
149 default:
150 return EPASSTHROUGH;
151 }
152 }
153
154 bool
drmfb_pci_is_vga_console(struct drm_device * dev)155 drmfb_pci_is_vga_console(struct drm_device *dev)
156 {
157
158 #if NVGA > 0
159 return vga_is_console(dev->pdev->pd_pa.pa_iot, -1);
160 #else
161 return false;
162 #endif
163 }
164