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