1*93124ce0Srin /* $NetBSD: genfb_pci.c,v 1.45 2022/10/02 02:37:27 rin Exp $ */
213daabb0Smacallan
313daabb0Smacallan /*-
413daabb0Smacallan * Copyright (c) 2007 Michael Lorenz
513daabb0Smacallan * All rights reserved.
613daabb0Smacallan *
713daabb0Smacallan * Redistribution and use in source and binary forms, with or without
813daabb0Smacallan * modification, are permitted provided that the following conditions
913daabb0Smacallan * are met:
1013daabb0Smacallan * 1. Redistributions of source code must retain the above copyright
1113daabb0Smacallan * notice, this list of conditions and the following disclaimer.
1213daabb0Smacallan * 2. Redistributions in binary form must reproduce the above copyright
1313daabb0Smacallan * notice, this list of conditions and the following disclaimer in the
1413daabb0Smacallan * documentation and/or other materials provided with the distribution.
1513daabb0Smacallan *
1613daabb0Smacallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1713daabb0Smacallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1813daabb0Smacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1913daabb0Smacallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2013daabb0Smacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2113daabb0Smacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2213daabb0Smacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2313daabb0Smacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2413daabb0Smacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2513daabb0Smacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2613daabb0Smacallan * POSSIBILITY OF SUCH DAMAGE.
2713daabb0Smacallan */
2813daabb0Smacallan
2913daabb0Smacallan #include <sys/cdefs.h>
30*93124ce0Srin __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.45 2022/10/02 02:37:27 rin Exp $");
3113daabb0Smacallan
3213daabb0Smacallan #include <sys/param.h>
3313daabb0Smacallan #include <sys/systm.h>
3413daabb0Smacallan #include <sys/kernel.h>
3513daabb0Smacallan #include <sys/device.h>
3613daabb0Smacallan #include <sys/proc.h>
3713daabb0Smacallan #include <sys/mutex.h>
3813daabb0Smacallan #include <sys/ioctl.h>
3913daabb0Smacallan #include <sys/kernel.h>
4013daabb0Smacallan #include <sys/systm.h>
4113daabb0Smacallan #include <sys/kauth.h>
4213daabb0Smacallan
4313daabb0Smacallan #include <dev/pci/pcidevs.h>
4413daabb0Smacallan #include <dev/pci/pcireg.h>
4513daabb0Smacallan #include <dev/pci/pcivar.h>
4613daabb0Smacallan #include <dev/pci/pciio.h>
4713daabb0Smacallan
4813daabb0Smacallan #include <dev/wsfb/genfbvar.h>
49e28cc170Scegger #include <dev/pci/wsdisplay_pci.h>
5013daabb0Smacallan
511338e788Sjmcneill #include <dev/pci/genfb_pcivar.h>
521338e788Sjmcneill
5313daabb0Smacallan #include "opt_wsfb.h"
5413daabb0Smacallan #include "opt_genfb.h"
5513daabb0Smacallan
5623cfff08Smacallan #ifdef GENFB_PCI_DEBUG
5723cfff08Smacallan # define DPRINTF printf
5823cfff08Smacallan #else
5923cfff08Smacallan # define DPRINTF while (0) printf
6023cfff08Smacallan #endif
6123cfff08Smacallan
62a591bc88Scegger static int pci_genfb_match(device_t, cfdata_t, void *);
63a591bc88Scegger static void pci_genfb_attach(device_t, device_t, void *);
6413daabb0Smacallan static int pci_genfb_ioctl(void *, void *, u_long, void *, int,
6513daabb0Smacallan struct lwp *);
6613daabb0Smacallan static paddr_t pci_genfb_mmap(void *, void *, off_t, int);
67ec798a6eSjmcneill static int pci_genfb_borrow(void *, bus_addr_t, bus_space_handle_t *);
6823cfff08Smacallan static int pci_genfb_drm_print(void *, const char *);
699204517cSjmcneill static bool pci_genfb_shutdown(device_t, int);
7023cfff08Smacallan
714fbc897fSmacallan CFATTACH_DECL_NEW(genfb_pci, sizeof(struct pci_genfb_softc),
7213daabb0Smacallan pci_genfb_match, pci_genfb_attach, NULL, NULL);
7313daabb0Smacallan
7413daabb0Smacallan static int
pci_genfb_match(device_t parent,cfdata_t match,void * aux)75a591bc88Scegger pci_genfb_match(device_t parent, cfdata_t match, void *aux)
7613daabb0Smacallan {
7713daabb0Smacallan struct pci_attach_args *pa = aux;
78a71612a2Sjmcneill int matchlvl = 1;
79a71612a2Sjmcneill
80e04f5ff1Sjmcneill if (!genfb_is_enabled())
81e04f5ff1Sjmcneill return 0; /* explicitly disabled by MD code */
82e04f5ff1Sjmcneill
83a71612a2Sjmcneill if (genfb_is_console())
84a71612a2Sjmcneill matchlvl = 5; /* beat VGA */
8513daabb0Smacallan
8613daabb0Smacallan if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
8713daabb0Smacallan PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
88a71612a2Sjmcneill return matchlvl;
8913daabb0Smacallan
9013daabb0Smacallan if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
91a71612a2Sjmcneill return matchlvl;
9213daabb0Smacallan
9313daabb0Smacallan return 0;
9413daabb0Smacallan }
9513daabb0Smacallan
9613daabb0Smacallan static void
pci_genfb_attach(device_t parent,device_t self,void * aux)97a591bc88Scegger pci_genfb_attach(device_t parent, device_t self, void *aux)
9813daabb0Smacallan {
99b8169823Scegger struct pci_genfb_softc *sc = device_private(self);
10013daabb0Smacallan struct pci_attach_args *pa = aux;
101b41195f8Sriastradh static const struct genfb_ops zero_ops;
102b41195f8Sriastradh struct genfb_ops ops = zero_ops;
1038ffcf003Smacallan pcireg_t rom;
10413daabb0Smacallan int idx, bar, type;
10571baff76Srin bool skip;
10613daabb0Smacallan
107d8e1a7b6Sdrochner pci_aprint_devinfo(pa, NULL);
10813daabb0Smacallan
1094fbc897fSmacallan sc->sc_gen.sc_dev = self;
11013daabb0Smacallan sc->sc_memt = pa->pa_memt;
11113daabb0Smacallan sc->sc_iot = pa->pa_iot;
11213daabb0Smacallan sc->sc_pc = pa->pa_pc;
11313daabb0Smacallan sc->sc_pcitag = pa->pa_tag;
11448d017a9Smacallan sc->sc_want_wsfb = 0;
11548d017a9Smacallan
11613daabb0Smacallan genfb_init(&sc->sc_gen);
11713daabb0Smacallan
1188aa0a1bfSjmcneill /* firmware / MD code responsible for restoring the display */
1198aa0a1bfSjmcneill if (sc->sc_gen.sc_pmfcb == NULL)
1209204517cSjmcneill pmf_device_register1(self, NULL, NULL,
1219204517cSjmcneill pci_genfb_shutdown);
1228aa0a1bfSjmcneill else
1239204517cSjmcneill pmf_device_register1(self,
1248aa0a1bfSjmcneill sc->sc_gen.sc_pmfcb->gpc_suspend,
1259204517cSjmcneill sc->sc_gen.sc_pmfcb->gpc_resume,
1269204517cSjmcneill pci_genfb_shutdown);
1278aa0a1bfSjmcneill
12823cfff08Smacallan if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
12972141a16Sjmcneill aprint_debug_dev(self, "not configured by firmware\n");
13023cfff08Smacallan return;
13123cfff08Smacallan }
13223cfff08Smacallan
133e5f1c17aSmacallan /*
134e5f1c17aSmacallan * if some MD code handed us a framebuffer VA we use that instead of
135e5f1c17aSmacallan * mapping our own
136e5f1c17aSmacallan */
137e5f1c17aSmacallan if (sc->sc_gen.sc_fbaddr == NULL) {
13813daabb0Smacallan if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
13936599bc7Sjmcneill sc->sc_gen.sc_fbsize,
14036599bc7Sjmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
14136599bc7Sjmcneill &sc->sc_memh) != 0) {
1428bc54e5bSmsaitoh aprint_error_dev(self,
1438bc54e5bSmsaitoh "unable to map the framebuffer\n");
1441ed6b6fcSmrg return;
14513daabb0Smacallan }
1468bc54e5bSmsaitoh sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt,
1478bc54e5bSmsaitoh sc->sc_memh);
148e5f1c17aSmacallan } else
149e5f1c17aSmacallan aprint_debug("%s: recycling existing fb mapping at %lx\n",
1508bc54e5bSmsaitoh device_xname(sc->sc_gen.sc_dev),
1518bc54e5bSmsaitoh (unsigned long)sc->sc_gen.sc_fbaddr);
15213daabb0Smacallan
15313daabb0Smacallan /* mmap()able bus ranges */
15413daabb0Smacallan idx = 0;
15571baff76Srin skip = false;
15671baff76Srin for (bar = PCI_MAPREG_START; bar <= PCI_MAPREG_ROM; bar += 4) {
15713daabb0Smacallan
15871baff76Srin rom = pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
15971baff76Srin if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
16071baff76Srin rom |= PCI_MAPREG_ROM_ENABLE;
16171baff76Srin pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom);
16271baff76Srin }
16371baff76Srin sc->sc_bars[(bar - PCI_MAPREG_START) >> 2] = rom;
16471baff76Srin if (skip || (bar >= PCI_MAPREG_END && bar < PCI_MAPREG_ROM) ||
165ecc6ababSphx pci_mapreg_probe(sc->sc_pc, sc->sc_pcitag, bar, &type)
166ecc6ababSphx == 0) {
16771baff76Srin /*
16871baff76Srin * skip unimplemented, non-BAR registers, or
169*93124ce0Srin * higher words of 64-bit BARs.
17071baff76Srin */
17171baff76Srin skip = false;
172ecc6ababSphx continue;
173ecc6ababSphx }
174b9412125Sjmcneill if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM ||
175b9412125Sjmcneill PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_ROM) {
17613daabb0Smacallan pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
17713daabb0Smacallan &sc->sc_ranges[idx].offset,
17813daabb0Smacallan &sc->sc_ranges[idx].size,
17913daabb0Smacallan &sc->sc_ranges[idx].flags);
18013daabb0Smacallan idx++;
18113daabb0Smacallan }
182b9412125Sjmcneill if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM &&
183b9412125Sjmcneill PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
18471baff76Srin skip = true;
18513daabb0Smacallan }
1868ffcf003Smacallan
18713daabb0Smacallan sc->sc_ranges_used = idx;
18813daabb0Smacallan
18913daabb0Smacallan ops.genfb_ioctl = pci_genfb_ioctl;
19013daabb0Smacallan ops.genfb_mmap = pci_genfb_mmap;
191ec798a6eSjmcneill ops.genfb_borrow = pci_genfb_borrow;
19213daabb0Smacallan
1937bca1efeSphx if (genfb_attach(&sc->sc_gen, &ops) == 0) {
19423cfff08Smacallan
19523cfff08Smacallan /* now try to attach a DRM */
1962685996bSthorpej config_found(self, aux, pci_genfb_drm_print,
197c7fb772bSthorpej CFARGS(.iattr = "drm"));
19813daabb0Smacallan }
1997bca1efeSphx }
20013daabb0Smacallan
20113daabb0Smacallan static int
pci_genfb_drm_print(void * aux,const char * pnp)20223cfff08Smacallan pci_genfb_drm_print(void *aux, const char *pnp)
20323cfff08Smacallan {
20423cfff08Smacallan if (pnp)
2051a53395eSjmcneill aprint_normal("drm at %s", pnp);
2061a53395eSjmcneill return (UNCONF);
20723cfff08Smacallan }
20823cfff08Smacallan
20923cfff08Smacallan
21023cfff08Smacallan static int
pci_genfb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)21113daabb0Smacallan pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
21213daabb0Smacallan struct lwp *l)
21313daabb0Smacallan {
21413daabb0Smacallan struct pci_genfb_softc *sc = v;
21591c7e8cbSrin size_t i;
21691c7e8cbSrin int new_mode;
21713daabb0Smacallan
21813daabb0Smacallan switch (cmd) {
21913daabb0Smacallan case WSDISPLAYIO_GTYPE:
22013daabb0Smacallan *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
22113daabb0Smacallan return 0;
22213daabb0Smacallan
22313daabb0Smacallan /* PCI config read/write passthrough. */
22413daabb0Smacallan case PCI_IOC_CFGREAD:
22513daabb0Smacallan case PCI_IOC_CFGWRITE:
226ca7fa246Scegger return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
227ca7fa246Scegger cmd, data, flag, l);
228e28cc170Scegger
229e28cc170Scegger case WSDISPLAYIO_GET_BUSID:
230e28cc170Scegger return wsdisplayio_busid_pci(sc->sc_gen.sc_dev, sc->sc_pc,
231e28cc170Scegger sc->sc_pcitag, data);
232e28cc170Scegger
2335ad4029aSrin case WSDISPLAYIO_SMODE:
2345ad4029aSrin new_mode = *(int *)data;
23523cfff08Smacallan if (new_mode == WSDISPLAYIO_MODE_EMUL) {
23691c7e8cbSrin for (i = 0; i < __arraycount(sc->sc_bars); i++)
23791c7e8cbSrin pci_conf_write(sc->sc_pc, sc->sc_pcitag,
23891c7e8cbSrin PCI_BAR(i), sc->sc_bars[i]);
23923cfff08Smacallan }
24023cfff08Smacallan return 0;
24113daabb0Smacallan }
24213daabb0Smacallan
24313daabb0Smacallan return EPASSTHROUGH;
24413daabb0Smacallan }
24513daabb0Smacallan
24613daabb0Smacallan static paddr_t
pci_genfb_mmap(void * v,void * vs,off_t offset,int prot)24713daabb0Smacallan pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
24813daabb0Smacallan {
24913daabb0Smacallan struct pci_genfb_softc *sc = v;
25013daabb0Smacallan struct range *r;
25113daabb0Smacallan int i;
25213daabb0Smacallan
25348d017a9Smacallan if (offset == 0)
25448d017a9Smacallan sc->sc_want_wsfb = 1;
25548d017a9Smacallan
25648d017a9Smacallan /*
25748d017a9Smacallan * regular fb mapping at 0
25848d017a9Smacallan * since some Sun firmware likes to put PCI resources low enough
25948d017a9Smacallan * to collide with the wsfb mapping we only allow it after asking
26048d017a9Smacallan * for offset 0
26148d017a9Smacallan */
26223cfff08Smacallan DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
26323cfff08Smacallan (uint32_t)sc->sc_gen.sc_fbsize);
26448d017a9Smacallan if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
26548d017a9Smacallan (sc->sc_want_wsfb == 1)) {
26613daabb0Smacallan
26713daabb0Smacallan return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
26836599bc7Sjmcneill offset, prot,
26936599bc7Sjmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
27013daabb0Smacallan }
27113daabb0Smacallan
27213daabb0Smacallan /*
27313daabb0Smacallan * restrict all other mappings to processes with superuser privileges
27413daabb0Smacallan * or the kernel itself
27513daabb0Smacallan */
2760c9d8d15Selad if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
2770c9d8d15Selad NULL, NULL, NULL, NULL) != 0) {
2784fbc897fSmacallan aprint_normal_dev(sc->sc_gen.sc_dev, "mmap() rejected.\n");
27913daabb0Smacallan return -1;
28013daabb0Smacallan }
28113daabb0Smacallan
28213daabb0Smacallan #ifdef WSFB_FAKE_VGA_FB
28313daabb0Smacallan if ((offset >= 0xa0000) && (offset < 0xbffff)) {
28413daabb0Smacallan
28513daabb0Smacallan return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
28613daabb0Smacallan offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
28713daabb0Smacallan }
28813daabb0Smacallan #endif
28913daabb0Smacallan
29013daabb0Smacallan /*
29113daabb0Smacallan * XXX this should be generalized, let's just
29213daabb0Smacallan * #define PCI_IOAREA_PADDR
29313daabb0Smacallan * #define PCI_IOAREA_OFFSET
29413daabb0Smacallan * #define PCI_IOAREA_SIZE
29513daabb0Smacallan * somewhere in a MD header and compile this code only if all are
29613daabb0Smacallan * present
29713daabb0Smacallan */
2988ffcf003Smacallan /*
2998ffcf003Smacallan * no.
3008ffcf003Smacallan * PCI_IOAREA_PADDR would be completely, utterly wrong and completely
3018ffcf003Smacallan * useless for the following reasons:
3028ffcf003Smacallan * - it's a bus address, not a physical address
3038ffcf003Smacallan * - there's no guarantee it's the same for each host bridge
3048ffcf003Smacallan * - it's already taken care of by the IO tag
3058ffcf003Smacallan * PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
3068ffcf003Smacallan * PCI_IOAREA_SIZE is also useless:
3078ffcf003Smacallan * - many cards don't decode more than 16 bit IO anyway
3088ffcf003Smacallan * - even machines with more than 64kB IO space try to keep everything
3098ffcf003Smacallan * within 64kB for the reason above
3108ffcf003Smacallan * - IO ranges tend to be small so in most cases you can't cram enough
3118ffcf003Smacallan * cards into a single machine to exhaust 64kB IO space
3128ffcf003Smacallan * - machines which need this tend to prefer memory space anyway
3138ffcf003Smacallan * - the only use for this right now is to allow the Xserver to map
3148ffcf003Smacallan * VGA registers on macppc and a few other powerpc ports, shark uses
3158ffcf003Smacallan * a similar mechanism, and what they need is always within 64kB
3168ffcf003Smacallan */
317ee03e6b4Smacallan #ifdef PCI_MAGIC_IO_RANGE
31813daabb0Smacallan /* allow to map our IO space */
319ee03e6b4Smacallan if ((offset >= PCI_MAGIC_IO_RANGE) &&
320ee03e6b4Smacallan (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
321ee03e6b4Smacallan return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
322ee03e6b4Smacallan 0, prot, BUS_SPACE_MAP_LINEAR);
32313daabb0Smacallan }
32413daabb0Smacallan #endif
32513daabb0Smacallan
32613daabb0Smacallan /* allow to mmap() our BARs */
32723cfff08Smacallan /* maybe the ROM BAR too? */
32813daabb0Smacallan for (i = 0; i < sc->sc_ranges_used; i++) {
32913daabb0Smacallan
33013daabb0Smacallan r = &sc->sc_ranges[i];
33113daabb0Smacallan if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
33213daabb0Smacallan return bus_space_mmap(sc->sc_memt, offset, 0, prot,
33313daabb0Smacallan r->flags);
33413daabb0Smacallan }
33513daabb0Smacallan }
33613daabb0Smacallan
33713daabb0Smacallan return -1;
33813daabb0Smacallan }
339ec798a6eSjmcneill
340ec798a6eSjmcneill int
pci_genfb_borrow(void * opaque,bus_addr_t addr,bus_space_handle_t * hdlp)341ec798a6eSjmcneill pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
342ec798a6eSjmcneill {
343ec798a6eSjmcneill struct pci_genfb_softc *sc = opaque;
344ec798a6eSjmcneill
345ec798a6eSjmcneill if (sc == NULL)
346ec798a6eSjmcneill return 0;
347ec798a6eSjmcneill if (!sc->sc_gen.sc_fboffset)
348ec798a6eSjmcneill return 0;
349ec798a6eSjmcneill if (sc->sc_gen.sc_fboffset != addr)
350ec798a6eSjmcneill return 0;
351ec798a6eSjmcneill *hdlp = sc->sc_memh;
352ec798a6eSjmcneill return 1;
353ec798a6eSjmcneill }
3549204517cSjmcneill
3559204517cSjmcneill static bool
pci_genfb_shutdown(device_t self,int flags)3569204517cSjmcneill pci_genfb_shutdown(device_t self, int flags)
3579204517cSjmcneill {
3589204517cSjmcneill genfb_enable_polling(self);
3599204517cSjmcneill return true;
3609204517cSjmcneill }
361