genfb_pci.c revision 1.31 1 /* $NetBSD: genfb_pci.c,v 1.31 2011/02/13 11:00:58 phx Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.31 2011/02/13 11:00:58 phx Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kauth.h>
42
43 #include <dev/pci/pcidevs.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pciio.h>
47
48 #include <dev/wsfb/genfbvar.h>
49 #include <dev/pci/wsdisplay_pci.h>
50
51 #include <dev/pci/genfb_pcivar.h>
52
53 #include "opt_wsfb.h"
54 #include "opt_genfb.h"
55
56 #ifdef GENFB_PCI_DEBUG
57 # define DPRINTF printf
58 #else
59 # define DPRINTF while (0) printf
60 #endif
61
62 static int pci_genfb_match(device_t, cfdata_t, void *);
63 static void pci_genfb_attach(device_t, device_t, void *);
64 static int pci_genfb_ioctl(void *, void *, u_long, void *, int,
65 struct lwp *);
66 static paddr_t pci_genfb_mmap(void *, void *, off_t, int);
67 static int pci_genfb_borrow(void *, bus_addr_t, bus_space_handle_t *);
68 static int pci_genfb_drm_print(void *, const char *);
69 static bool pci_genfb_shutdown(device_t, int);
70
71 CFATTACH_DECL_NEW(genfb_pci, sizeof(struct pci_genfb_softc),
72 pci_genfb_match, pci_genfb_attach, NULL, NULL);
73
74 static int
75 pci_genfb_match(device_t parent, cfdata_t match, void *aux)
76 {
77 struct pci_attach_args *pa = aux;
78 int matchlvl = 1;
79
80 if (!genfb_is_enabled())
81 return 0; /* explicitly disabled by MD code */
82
83 if (genfb_is_console())
84 matchlvl = 5; /* beat VGA */
85
86 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
87 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
88 return matchlvl;
89
90 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
91 return matchlvl;
92
93 return 0;
94 }
95
96 static void
97 pci_genfb_attach(device_t parent, device_t self, void *aux)
98 {
99 struct pci_genfb_softc *sc = device_private(self);
100 struct pci_attach_args *pa = aux;
101 struct genfb_ops ops;
102 pcireg_t rom;
103 int idx, bar, type;
104 char devinfo[256];
105
106 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
107 aprint_naive("\n");
108 aprint_normal(": %s\n", devinfo);
109
110 sc->sc_gen.sc_dev = self;
111 sc->sc_memt = pa->pa_memt;
112 sc->sc_iot = pa->pa_iot;
113 sc->sc_pc = pa->pa_pc;
114 sc->sc_pcitag = pa->pa_tag;
115 sc->sc_want_wsfb = 0;
116
117 genfb_init(&sc->sc_gen);
118
119 /* firmware / MD code responsible for restoring the display */
120 if (sc->sc_gen.sc_pmfcb == NULL)
121 pmf_device_register1(self, NULL, NULL,
122 pci_genfb_shutdown);
123 else
124 pmf_device_register1(self,
125 sc->sc_gen.sc_pmfcb->gpc_suspend,
126 sc->sc_gen.sc_pmfcb->gpc_resume,
127 pci_genfb_shutdown);
128
129 if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
130 aprint_debug_dev(self, "not configured by firmware\n");
131 return;
132 }
133
134 if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
135 sc->sc_gen.sc_fbsize,
136 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
137 &sc->sc_memh) != 0) {
138 aprint_error_dev(self, "unable to map the framebuffer\n");
139 return;
140 }
141 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
142
143 /* mmap()able bus ranges */
144 idx = 0;
145 bar = PCI_MAPREG_START;
146 while (bar <= PCI_MAPREG_ROM) {
147
148 sc->sc_bars[(bar - PCI_MAPREG_START) >> 2] = rom =
149 pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
150
151 if ((bar >= PCI_MAPREG_END && bar < PCI_MAPREG_ROM) ||
152 pci_mapreg_probe(sc->sc_pc, sc->sc_pcitag, bar, &type)
153 == 0) {
154 /* skip unimplemented and non-BAR registers */
155 bar += 4;
156 continue;
157 }
158 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM ||
159 PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_ROM) {
160 pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
161 &sc->sc_ranges[idx].offset,
162 &sc->sc_ranges[idx].size,
163 &sc->sc_ranges[idx].flags);
164 idx++;
165 }
166 if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
167 pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom |
168 PCI_MAPREG_ROM_ENABLE);
169 }
170 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM &&
171 PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
172 bar += 8;
173 else
174 bar += 4;
175 }
176
177 sc->sc_ranges_used = idx;
178
179 ops.genfb_ioctl = pci_genfb_ioctl;
180 ops.genfb_mmap = pci_genfb_mmap;
181 ops.genfb_borrow = pci_genfb_borrow;
182
183 if (genfb_attach(&sc->sc_gen, &ops) == 0) {
184
185 /* now try to attach a DRM */
186 config_found_ia(self, "drm", aux, pci_genfb_drm_print);
187 }
188 }
189
190 static int
191 pci_genfb_drm_print(void *aux, const char *pnp)
192 {
193 if (pnp)
194 aprint_normal("drm at %s", pnp);
195 return (UNCONF);
196 }
197
198
199 static int
200 pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
201 struct lwp *l)
202 {
203 struct pci_genfb_softc *sc = v;
204
205 switch (cmd) {
206 case WSDISPLAYIO_GTYPE:
207 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
208 return 0;
209
210 /* PCI config read/write passthrough. */
211 case PCI_IOC_CFGREAD:
212 case PCI_IOC_CFGWRITE:
213 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
214 cmd, data, flag, l);
215
216 case WSDISPLAYIO_GET_BUSID:
217 return wsdisplayio_busid_pci(sc->sc_gen.sc_dev, sc->sc_pc,
218 sc->sc_pcitag, data);
219
220 case WSDISPLAYIO_SMODE: {
221 int new_mode = *(int*)data, i;
222 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
223 for (i = 0; i < 9; i++)
224 pci_conf_write(sc->sc_pc,
225 sc->sc_pcitag,
226 0x10 + (i << 2),
227 sc->sc_bars[i]);
228 }
229 }
230 return 0;
231 }
232
233 return EPASSTHROUGH;
234 }
235
236 static paddr_t
237 pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
238 {
239 struct pci_genfb_softc *sc = v;
240 struct range *r;
241 int i;
242
243 if (offset == 0)
244 sc->sc_want_wsfb = 1;
245
246 /*
247 * regular fb mapping at 0
248 * since some Sun firmware likes to put PCI resources low enough
249 * to collide with the wsfb mapping we only allow it after asking
250 * for offset 0
251 */
252 DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
253 (uint32_t)sc->sc_gen.sc_fbsize);
254 if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
255 (sc->sc_want_wsfb == 1)) {
256
257 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
258 offset, prot,
259 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
260 }
261
262 /*
263 * restrict all other mappings to processes with superuser privileges
264 * or the kernel itself
265 */
266 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
267 NULL) != 0) {
268 aprint_normal_dev(sc->sc_gen.sc_dev, "mmap() rejected.\n");
269 return -1;
270 }
271
272 #ifdef WSFB_FAKE_VGA_FB
273 if ((offset >= 0xa0000) && (offset < 0xbffff)) {
274
275 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
276 offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
277 }
278 #endif
279
280 /*
281 * XXX this should be generalized, let's just
282 * #define PCI_IOAREA_PADDR
283 * #define PCI_IOAREA_OFFSET
284 * #define PCI_IOAREA_SIZE
285 * somewhere in a MD header and compile this code only if all are
286 * present
287 */
288 /*
289 * no.
290 * PCI_IOAREA_PADDR would be completely, utterly wrong and completely
291 * useless for the following reasons:
292 * - it's a bus address, not a physical address
293 * - there's no guarantee it's the same for each host bridge
294 * - it's already taken care of by the IO tag
295 * PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
296 * PCI_IOAREA_SIZE is also useless:
297 * - many cards don't decode more than 16 bit IO anyway
298 * - even machines with more than 64kB IO space try to keep everything
299 * within 64kB for the reason above
300 * - IO ranges tend to be small so in most cases you can't cram enough
301 * cards into a single machine to exhaust 64kB IO space
302 * - machines which need this tend to prefer memory space anyway
303 * - the only use for this right now is to allow the Xserver to map
304 * VGA registers on macppc and a few other powerpc ports, shark uses
305 * a similar mechanism, and what they need is always within 64kB
306 */
307 #ifdef PCI_MAGIC_IO_RANGE
308 /* allow to map our IO space */
309 if ((offset >= PCI_MAGIC_IO_RANGE) &&
310 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
311 return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
312 0, prot, BUS_SPACE_MAP_LINEAR);
313 }
314 #endif
315
316 /* allow to mmap() our BARs */
317 /* maybe the ROM BAR too? */
318 for (i = 0; i < sc->sc_ranges_used; i++) {
319
320 r = &sc->sc_ranges[i];
321 if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
322 return bus_space_mmap(sc->sc_memt, offset, 0, prot,
323 r->flags);
324 }
325 }
326
327 return -1;
328 }
329
330 int
331 pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
332 {
333 struct pci_genfb_softc *sc = opaque;
334
335 if (sc == NULL)
336 return 0;
337 if (!sc->sc_gen.sc_fboffset)
338 return 0;
339 if (sc->sc_gen.sc_fboffset != addr)
340 return 0;
341 *hdlp = sc->sc_memh;
342 return 1;
343 }
344
345 static bool
346 pci_genfb_shutdown(device_t self, int flags)
347 {
348 genfb_enable_polling(self);
349 return true;
350 }
351