1 /*- 2 * Copyright (c) 2011 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/module.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 36 #include <dev/ofw/openfirm.h> 37 #include <dev/ofw/ofw_pci.h> 38 #include <dev/ofw/ofw_bus.h> 39 #include <dev/ofw/ofw_bus_subr.h> 40 41 #include <dev/pci/pcivar.h> 42 #include <dev/pci/pcireg.h> 43 44 #include <machine/bus.h> 45 #include <machine/intr_machdep.h> 46 #include <machine/md_var.h> 47 #include <machine/pio.h> 48 #include <machine/resource.h> 49 #include <machine/rtas.h> 50 51 #include <sys/rman.h> 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 56 #include <powerpc/ofw/ofw_pci.h> 57 #include <powerpc/pseries/plpar_iommu.h> 58 59 #include "pcib_if.h" 60 #include "iommu_if.h" 61 62 /* 63 * Device interface. 64 */ 65 static int rtaspci_probe(device_t); 66 static int rtaspci_attach(device_t); 67 68 /* 69 * pcib interface. 70 */ 71 static u_int32_t rtaspci_read_config(device_t, u_int, u_int, u_int, 72 u_int, int); 73 static void rtaspci_write_config(device_t, u_int, u_int, u_int, 74 u_int, u_int32_t, int); 75 76 /* 77 * IOMMU LPAR interface 78 */ 79 static bus_dma_tag_t rtaspci_get_dma_tag(device_t dev, device_t child); 80 81 /* 82 * Driver methods. 83 */ 84 static device_method_t rtaspci_methods[] = { 85 /* Device interface */ 86 DEVMETHOD(device_probe, rtaspci_probe), 87 DEVMETHOD(device_attach, rtaspci_attach), 88 89 /* pcib interface */ 90 DEVMETHOD(pcib_read_config, rtaspci_read_config), 91 DEVMETHOD(pcib_write_config, rtaspci_write_config), 92 93 /* IOMMU functions */ 94 DEVMETHOD(bus_get_dma_tag, rtaspci_get_dma_tag), 95 #ifdef __powerpc64__ 96 DEVMETHOD(iommu_map, phyp_iommu_map), 97 DEVMETHOD(iommu_unmap, phyp_iommu_unmap), 98 #endif 99 100 DEVMETHOD_END 101 }; 102 103 struct rtaspci_softc { 104 struct ofw_pci_softc pci_sc; 105 bus_dma_tag_t dma_tag; 106 107 cell_t read_pci_config, write_pci_config; 108 cell_t ex_read_pci_config, ex_write_pci_config; 109 int sc_extended_config; 110 }; 111 112 static devclass_t rtaspci_devclass; 113 DEFINE_CLASS_1(pcib, rtaspci_driver, rtaspci_methods, 114 sizeof(struct rtaspci_softc), ofw_pci_driver); 115 DRIVER_MODULE(rtaspci, nexus, rtaspci_driver, rtaspci_devclass, 0, 0); 116 117 static int 118 rtaspci_probe(device_t dev) 119 { 120 const char *type; 121 122 if (!rtas_exists()) 123 return (ENXIO); 124 125 type = ofw_bus_get_type(dev); 126 127 if (OF_getproplen(ofw_bus_get_node(dev), "used-by-rtas") < 0) 128 return (ENXIO); 129 if (type == NULL || strcmp(type, "pci") != 0) 130 return (ENXIO); 131 132 device_set_desc(dev, "RTAS Host-PCI bridge"); 133 return (BUS_PROBE_GENERIC); 134 } 135 136 static int 137 rtaspci_attach(device_t dev) 138 { 139 struct rtaspci_softc *sc; 140 141 sc = device_get_softc(dev); 142 143 sc->read_pci_config = rtas_token_lookup("read-pci-config"); 144 sc->write_pci_config = rtas_token_lookup("write-pci-config"); 145 sc->ex_read_pci_config = rtas_token_lookup("ibm,read-pci-config"); 146 sc->ex_write_pci_config = rtas_token_lookup("ibm,write-pci-config"); 147 148 sc->sc_extended_config = 0; 149 OF_getprop(ofw_bus_get_node(dev), "ibm,pci-config-space-type", 150 &sc->sc_extended_config, sizeof(sc->sc_extended_config)); 151 152 bus_dma_tag_create(bus_get_dma_tag(dev), 153 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 154 NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED, 155 BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->dma_tag); 156 #ifdef __powerpc64__ 157 if (!(mfmsr() & PSL_HV)) 158 phyp_iommu_set_dma_tag(dev, dev, sc->dma_tag); 159 #endif 160 161 return (ofw_pci_attach(dev)); 162 } 163 164 static uint32_t 165 rtaspci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 166 int width) 167 { 168 struct rtaspci_softc *sc; 169 uint32_t retval = 0xffffffff; 170 uint32_t config_addr; 171 int error, pcierror; 172 173 sc = device_get_softc(dev); 174 175 config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) | 176 ((func & 0x7) << 8) | (reg & 0xff); 177 if (sc->sc_extended_config) 178 config_addr |= (reg & 0xf00) << 16; 179 180 if (sc->ex_read_pci_config != -1) 181 error = rtas_call_method(sc->ex_read_pci_config, 4, 2, 182 config_addr, sc->pci_sc.sc_pcir.phys_hi, 183 sc->pci_sc.sc_pcir.phys_mid, width, &pcierror, &retval); 184 else 185 error = rtas_call_method(sc->read_pci_config, 2, 2, 186 config_addr, width, &pcierror, &retval); 187 188 /* Sign-extend output */ 189 switch (width) { 190 case 1: 191 retval = (int32_t)(int8_t)(retval); 192 break; 193 case 2: 194 retval = (int32_t)(int16_t)(retval); 195 break; 196 } 197 198 if (error < 0 || pcierror != 0) 199 retval = 0xffffffff; 200 201 return (retval); 202 } 203 204 static void 205 rtaspci_write_config(device_t dev, u_int bus, u_int slot, u_int func, 206 u_int reg, uint32_t val, int width) 207 { 208 struct rtaspci_softc *sc; 209 uint32_t config_addr; 210 int pcierror; 211 212 sc = device_get_softc(dev); 213 214 config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) | 215 ((func & 0x7) << 8) | (reg & 0xff); 216 if (sc->sc_extended_config) 217 config_addr |= (reg & 0xf00) << 16; 218 219 if (sc->ex_write_pci_config != -1) 220 rtas_call_method(sc->ex_write_pci_config, 5, 1, config_addr, 221 sc->pci_sc.sc_pcir.phys_hi, sc->pci_sc.sc_pcir.phys_mid, 222 width, val, &pcierror); 223 else 224 rtas_call_method(sc->write_pci_config, 3, 1, config_addr, 225 width, val, &pcierror); 226 } 227 228 static bus_dma_tag_t 229 rtaspci_get_dma_tag(device_t dev, device_t child) 230 { 231 struct rtaspci_softc *sc; 232 233 sc = device_get_softc(dev); 234 return (sc->dma_tag); 235 } 236 237