/* $NetBSD: genfb_pci.c,v 1.45 2022/10/02 02:37:27 rin Exp $ */
/*-
* Copyright (c) 2007 Michael Lorenz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* firmware / MD code responsible for restoring the display */
if (sc->sc_gen.sc_pmfcb == NULL)
pmf_device_register1(self, NULL, NULL,
pci_genfb_shutdown);
else
pmf_device_register1(self,
sc->sc_gen.sc_pmfcb->gpc_suspend,
sc->sc_gen.sc_pmfcb->gpc_resume,
pci_genfb_shutdown);
if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
aprint_debug_dev(self, "not configured by firmware\n");
return;
}
/*
* if some MD code handed us a framebuffer VA we use that instead of
* mapping our own
*/
if (sc->sc_gen.sc_fbaddr == NULL) {
if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
sc->sc_gen.sc_fbsize,
BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
&sc->sc_memh) != 0) {
aprint_error_dev(self,
"unable to map the framebuffer\n");
return;
}
sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt,
sc->sc_memh);
} else
aprint_debug("%s: recycling existing fb mapping at %lx\n",
device_xname(sc->sc_gen.sc_dev),
(unsigned long)sc->sc_gen.sc_fbaddr);
/* mmap()able bus ranges */
idx = 0;
skip = false;
for (bar = PCI_MAPREG_START; bar <= PCI_MAPREG_ROM; bar += 4) {
rom = pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
if ((bar == PCI_MAPREG_ROM) && (rom != 0)) {
rom |= PCI_MAPREG_ROM_ENABLE;
pci_conf_write(sc->sc_pc, sc->sc_pcitag, bar, rom);
}
sc->sc_bars[(bar - PCI_MAPREG_START) >> 2] = rom;
if (skip || (bar >= PCI_MAPREG_END && bar < PCI_MAPREG_ROM) ||
pci_mapreg_probe(sc->sc_pc, sc->sc_pcitag, bar, &type)
== 0) {
/*
* skip unimplemented, non-BAR registers, or
* higher words of 64-bit BARs.
*/
skip = false;
continue;
}
if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM ||
PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_ROM) {
pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
&sc->sc_ranges[idx].offset,
&sc->sc_ranges[idx].size,
&sc->sc_ranges[idx].flags);
idx++;
}
if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM &&
PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
skip = true;
}
/* PCI config read/write passthrough. */
case PCI_IOC_CFGREAD:
case PCI_IOC_CFGWRITE:
return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
cmd, data, flag, l);
case WSDISPLAYIO_GET_BUSID:
return wsdisplayio_busid_pci(sc->sc_gen.sc_dev, sc->sc_pc,
sc->sc_pcitag, data);
case WSDISPLAYIO_SMODE:
new_mode = *(int *)data;
if (new_mode == WSDISPLAYIO_MODE_EMUL) {
for (i = 0; i < __arraycount(sc->sc_bars); i++)
pci_conf_write(sc->sc_pc, sc->sc_pcitag,
PCI_BAR(i), sc->sc_bars[i]);
}
return 0;
}
return EPASSTHROUGH;
}
static paddr_t
pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
{
struct pci_genfb_softc *sc = v;
struct range *r;
int i;
if (offset == 0)
sc->sc_want_wsfb = 1;
/*
* regular fb mapping at 0
* since some Sun firmware likes to put PCI resources low enough
* to collide with the wsfb mapping we only allow it after asking
* for offset 0
*/
DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
(uint32_t)sc->sc_gen.sc_fbsize);
if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
(sc->sc_want_wsfb == 1)) {
/*
* restrict all other mappings to processes with superuser privileges
* or the kernel itself
*/
if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
NULL, NULL, NULL, NULL) != 0) {
aprint_normal_dev(sc->sc_gen.sc_dev, "mmap() rejected.\n");
return -1;
}
/*
* XXX this should be generalized, let's just
* #define PCI_IOAREA_PADDR
* #define PCI_IOAREA_OFFSET
* #define PCI_IOAREA_SIZE
* somewhere in a MD header and compile this code only if all are
* present
*/
/*
* no.
* PCI_IOAREA_PADDR would be completely, utterly wrong and completely
* useless for the following reasons:
* - it's a bus address, not a physical address
* - there's no guarantee it's the same for each host bridge
* - it's already taken care of by the IO tag
* PCI_IOAREA_OFFSET is the same as PCI_MAGIC_IO_RANGE
* PCI_IOAREA_SIZE is also useless:
* - many cards don't decode more than 16 bit IO anyway
* - even machines with more than 64kB IO space try to keep everything
* within 64kB for the reason above
* - IO ranges tend to be small so in most cases you can't cram enough
* cards into a single machine to exhaust 64kB IO space
* - machines which need this tend to prefer memory space anyway
* - the only use for this right now is to allow the Xserver to map
* VGA registers on macppc and a few other powerpc ports, shark uses
* a similar mechanism, and what they need is always within 64kB
*/
#ifdef PCI_MAGIC_IO_RANGE
/* allow to map our IO space */
if ((offset >= PCI_MAGIC_IO_RANGE) &&
(offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
0, prot, BUS_SPACE_MAP_LINEAR);
}
#endif
/* allow to mmap() our BARs */
/* maybe the ROM BAR too? */
for (i = 0; i < sc->sc_ranges_used; i++) {