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