1 /* $NetBSD: genfb_pci.c,v 1.1 2007/04/10 02:16:48 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 * 3. Neither the name of The NetBSD Foundation nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.1 2007/04/10 02:16:48 macallan Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/proc.h> 40 #include <sys/mutex.h> 41 #include <sys/ioctl.h> 42 #include <sys/kernel.h> 43 #include <sys/systm.h> 44 #include <sys/kauth.h> 45 46 #include <dev/pci/pcidevs.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pciio.h> 50 51 #include <dev/wsfb/genfbvar.h> 52 53 #include "opt_wsfb.h" 54 #include "opt_genfb.h" 55 56 struct range { 57 bus_addr_t offset; 58 bus_size_t size; 59 int flags; 60 }; 61 62 struct pci_genfb_softc { 63 struct genfb_softc sc_gen; 64 65 pci_chipset_tag_t sc_pc; 66 pcitag_t sc_pcitag; 67 bus_space_tag_t sc_memt; 68 bus_space_tag_t sc_iot; 69 bus_space_handle_t sc_memh; 70 struct range sc_ranges[8]; 71 int sc_ranges_used; 72 }; 73 74 static int pci_genfb_match(struct device *, struct cfdata *, void *); 75 static void pci_genfb_attach(struct device *, struct device *, void *); 76 static int pci_genfb_ioctl(void *, void *, u_long, void *, int, 77 struct lwp *); 78 static paddr_t pci_genfb_mmap(void *, void *, off_t, int); 79 80 CFATTACH_DECL(genfb_pci, sizeof(struct pci_genfb_softc), 81 pci_genfb_match, pci_genfb_attach, NULL, NULL); 82 83 static int 84 pci_genfb_match(struct device *parent, struct cfdata *match, void *aux) 85 { 86 struct pci_attach_args *pa = aux; 87 88 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE && 89 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL) 90 return 1; 91 92 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY) 93 return 1; 94 95 return 0; 96 } 97 98 static void 99 pci_genfb_attach(struct device *parent, struct device *self, void *aux) 100 { 101 struct pci_genfb_softc *sc = (struct pci_genfb_softc *)self; 102 struct pci_attach_args *pa = aux; 103 struct genfb_ops ops; 104 int idx, bar, type; 105 char devinfo[256]; 106 107 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 108 printf(": %s\n", devinfo); 109 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 115 genfb_init(&sc->sc_gen); 116 117 if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset, 118 sc->sc_gen.sc_fbsize, BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) { 119 120 panic("%s: unable to map the framebuffer\n", self->dv_xname); 121 } 122 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh); 123 124 /* mmap()able bus ranges */ 125 idx = 0; 126 bar = 0x10; 127 while (bar < 0x30) { 128 129 type = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, bar); 130 if ((type == PCI_MAPREG_TYPE_MEM) || 131 (type == PCI_MAPREG_TYPE_ROM)) { 132 133 pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type, 134 &sc->sc_ranges[idx].offset, 135 &sc->sc_ranges[idx].size, 136 &sc->sc_ranges[idx].flags); 137 idx++; 138 } 139 bar += 4; 140 } 141 sc->sc_ranges_used = idx; 142 143 ops.genfb_ioctl = pci_genfb_ioctl; 144 ops.genfb_mmap = pci_genfb_mmap; 145 146 genfb_attach(&sc->sc_gen, &ops); 147 } 148 149 static int 150 pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 151 struct lwp *l) 152 { 153 struct pci_genfb_softc *sc = v; 154 155 switch (cmd) { 156 case WSDISPLAYIO_GTYPE: 157 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 158 return 0; 159 160 /* PCI config read/write passthrough. */ 161 case PCI_IOC_CFGREAD: 162 case PCI_IOC_CFGWRITE: 163 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag, 164 cmd, data, flag, l)); 165 } 166 167 return EPASSTHROUGH; 168 } 169 170 static paddr_t 171 pci_genfb_mmap(void *v, void *vs, off_t offset, int prot) 172 { 173 struct pci_genfb_softc *sc = v; 174 struct range *r; 175 struct lwp *me; 176 int i; 177 178 /* regular fb mapping at 0 */ 179 if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) { 180 181 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, 182 offset, prot, BUS_SPACE_MAP_LINEAR); 183 } 184 185 /* 186 * restrict all other mappings to processes with superuser privileges 187 * or the kernel itself 188 */ 189 me = curlwp; 190 if (me != NULL) { 191 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER, 192 NULL) != 0) { 193 aprint_normal("%s: mmap() rejected.\n", 194 sc->sc_gen.sc_dev.dv_xname); 195 return -1; 196 } 197 } 198 199 #ifdef WSFB_FAKE_VGA_FB 200 if ((offset >= 0xa0000) && (offset < 0xbffff)) { 201 202 return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, 203 offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR); 204 } 205 #endif 206 207 /* 208 * XXX this should be generalized, let's just 209 * #define PCI_IOAREA_PADDR 210 * #define PCI_IOAREA_OFFSET 211 * #define PCI_IOAREA_SIZE 212 * somewhere in a MD header and compile this code only if all are 213 * present 214 */ 215 #ifdef macppc 216 /* allow to map our IO space */ 217 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) { 218 return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot, 219 BUS_SPACE_MAP_LINEAR); 220 } 221 #endif 222 223 /* allow to mmap() our BARs */ 224 for (i = 0; i < sc->sc_ranges_used; i++) { 225 226 r = &sc->sc_ranges[i]; 227 if ((offset >= r->offset) && (offset < (r->offset + r->size))) { 228 return bus_space_mmap(sc->sc_memt, offset, 0, prot, 229 r->flags); 230 } 231 } 232 233 return -1; 234 } 235