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