genfb_pci.c revision 1.32 1 /* $NetBSD: genfb_pci.c,v 1.32 2011/03/08 03:22:29 macallan 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.32 2011/03/08 03:22:29 macallan 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 /*
135 * if some MD code handed us a framebuffer VA we use that instead of
136 * mapping our own
137 */
138 if (sc->sc_gen.sc_fbaddr == NULL) {
139 if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
140 sc->sc_gen.sc_fbsize,
141 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
142 &sc->sc_memh) != 0) {
143 aprint_error_dev(self, "unable to map the framebuffer\n");
144 return;
145 }
146 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
147 } else
148 aprint_debug("%s: recycling existing fb mapping at %lx\n",
149 device_xname(sc->sc_gen.sc_dev), (unsigned long)sc->sc_gen.sc_fbaddr);
150
151 /* mmap()able bus ranges */
152 idx = 0;
153 bar = PCI_MAPREG_START;
154 while (bar <= PCI_MAPREG_ROM) {
155
156 sc->sc_bars[(bar - PCI_MAPREG_START) >> 2] = rom =
157 pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
158
159 if ((bar >= PCI_MAPREG_END && bar < PCI_MAPREG_ROM) ||
160 pci_mapreg_probe(sc->sc_pc, sc->sc_pcitag, bar, &type)
161 == 0) {
162 /* skip unimplemented and non-BAR registers */
163 bar += 4;
164 continue;
165 }
166 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM ||
167 PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_ROM) {
168 pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
169 &sc->sc_ranges[idx].offset,
170 &sc->sc_ranges[idx].size,
171 &sc->sc_ranges[idx].flags);
172 idx++;
173 }
174 if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
175 pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom |
176 PCI_MAPREG_ROM_ENABLE);
177 }
178 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM &&
179 PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
180 bar += 8;
181 else
182 bar += 4;
183 }
184
185 sc->sc_ranges_used = idx;
186
187 ops.genfb_ioctl = pci_genfb_ioctl;
188 ops.genfb_mmap = pci_genfb_mmap;
189 ops.genfb_borrow = pci_genfb_borrow;
190
191 if (genfb_attach(&sc->sc_gen, &ops) == 0) {
192
193 /* now try to attach a DRM */
194 config_found_ia(self, "drm", aux, pci_genfb_drm_print);
195 }
196 }
197
198 static int
199 pci_genfb_drm_print(void *aux, const char *pnp)
200 {
201 if (pnp)
202 aprint_normal("drm at %s", pnp);
203 return (UNCONF);
204 }
205
206
207 static int
208 pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
209 struct lwp *l)
210 {
211 struct pci_genfb_softc *sc = v;
212
213 switch (cmd) {
214 case WSDISPLAYIO_GTYPE:
215 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
216 return 0;
217
218 /* PCI config read/write passthrough. */
219 case PCI_IOC_CFGREAD:
220 case PCI_IOC_CFGWRITE:
221 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
222 cmd, data, flag, l);
223
224 case WSDISPLAYIO_GET_BUSID:
225 return wsdisplayio_busid_pci(sc->sc_gen.sc_dev, sc->sc_pc,
226 sc->sc_pcitag, data);
227
228 case WSDISPLAYIO_SMODE: {
229 int new_mode = *(int*)data, i;
230 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
231 for (i = 0; i < 9; i++)
232 pci_conf_write(sc->sc_pc,
233 sc->sc_pcitag,
234 0x10 + (i << 2),
235 sc->sc_bars[i]);
236 }
237 }
238 return 0;
239 }
240
241 return EPASSTHROUGH;
242 }
243
244 static paddr_t
245 pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
246 {
247 struct pci_genfb_softc *sc = v;
248 struct range *r;
249 int i;
250
251 if (offset == 0)
252 sc->sc_want_wsfb = 1;
253
254 /*
255 * regular fb mapping at 0
256 * since some Sun firmware likes to put PCI resources low enough
257 * to collide with the wsfb mapping we only allow it after asking
258 * for offset 0
259 */
260 DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
261 (uint32_t)sc->sc_gen.sc_fbsize);
262 if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
263 (sc->sc_want_wsfb == 1)) {
264
265 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
266 offset, prot,
267 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
268 }
269
270 /*
271 * restrict all other mappings to processes with superuser privileges
272 * or the kernel itself
273 */
274 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
275 NULL) != 0) {
276 aprint_normal_dev(sc->sc_gen.sc_dev, "mmap() rejected.\n");
277 return -1;
278 }
279
280 #ifdef WSFB_FAKE_VGA_FB
281 if ((offset >= 0xa0000) && (offset < 0xbffff)) {
282
283 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
284 offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
285 }
286 #endif
287
288 /*
289 * XXX this should be generalized, let's just
290 * #define PCI_IOAREA_PADDR
291 * #define PCI_IOAREA_OFFSET
292 * #define PCI_IOAREA_SIZE
293 * somewhere in a MD header and compile this code only if all are
294 * present
295 */
296 /*
297 * no.
298 * PCI_IOAREA_PADDR would be completely, utterly wrong and completely
299 * useless for the following reasons:
300 * - it's a bus address, not a physical address
301 * - there's no guarantee it's the same for each host bridge
302 * - it's already taken care of by the IO tag
303 * PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
304 * PCI_IOAREA_SIZE is also useless:
305 * - many cards don't decode more than 16 bit IO anyway
306 * - even machines with more than 64kB IO space try to keep everything
307 * within 64kB for the reason above
308 * - IO ranges tend to be small so in most cases you can't cram enough
309 * cards into a single machine to exhaust 64kB IO space
310 * - machines which need this tend to prefer memory space anyway
311 * - the only use for this right now is to allow the Xserver to map
312 * VGA registers on macppc and a few other powerpc ports, shark uses
313 * a similar mechanism, and what they need is always within 64kB
314 */
315 #ifdef PCI_MAGIC_IO_RANGE
316 /* allow to map our IO space */
317 if ((offset >= PCI_MAGIC_IO_RANGE) &&
318 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
319 return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
320 0, prot, BUS_SPACE_MAP_LINEAR);
321 }
322 #endif
323
324 /* allow to mmap() our BARs */
325 /* maybe the ROM BAR too? */
326 for (i = 0; i < sc->sc_ranges_used; i++) {
327
328 r = &sc->sc_ranges[i];
329 if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
330 return bus_space_mmap(sc->sc_memt, offset, 0, prot,
331 r->flags);
332 }
333 }
334
335 return -1;
336 }
337
338 int
339 pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
340 {
341 struct pci_genfb_softc *sc = opaque;
342
343 if (sc == NULL)
344 return 0;
345 if (!sc->sc_gen.sc_fboffset)
346 return 0;
347 if (sc->sc_gen.sc_fboffset != addr)
348 return 0;
349 *hdlp = sc->sc_memh;
350 return 1;
351 }
352
353 static bool
354 pci_genfb_shutdown(device_t self, int flags)
355 {
356 genfb_enable_polling(self);
357 return true;
358 }
359