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