rtas_pci.c revision 331722
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: stable/11/sys/powerpc/pseries/rtas_pci.c 331722 2018-03-29 02:50:57Z eadler $"); 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#include <sys/rman.h> 36 37#include <dev/ofw/openfirm.h> 38#include <dev/ofw/ofw_pci.h> 39#include <dev/ofw/ofw_bus.h> 40#include <dev/ofw/ofw_bus_subr.h> 41#include <dev/ofw/ofwpci.h> 42 43#include <dev/pci/pcivar.h> 44#include <dev/pci/pcireg.h> 45 46#include <machine/bus.h> 47#include <machine/intr_machdep.h> 48#include <machine/md_var.h> 49#include <machine/pio.h> 50#include <machine/resource.h> 51#include <machine/rtas.h> 52 53#include <vm/vm.h> 54#include <vm/pmap.h> 55 56#include <powerpc/pseries/plpar_iommu.h> 57 58#include "pcib_if.h" 59#include "iommu_if.h" 60 61/* 62 * Device interface. 63 */ 64static int rtaspci_probe(device_t); 65static int rtaspci_attach(device_t); 66 67/* 68 * pcib interface. 69 */ 70static u_int32_t rtaspci_read_config(device_t, u_int, u_int, u_int, 71 u_int, int); 72static void rtaspci_write_config(device_t, u_int, u_int, u_int, 73 u_int, u_int32_t, int); 74 75/* 76 * Driver methods. 77 */ 78static device_method_t rtaspci_methods[] = { 79 /* Device interface */ 80 DEVMETHOD(device_probe, rtaspci_probe), 81 DEVMETHOD(device_attach, rtaspci_attach), 82 83 /* pcib interface */ 84 DEVMETHOD(pcib_read_config, rtaspci_read_config), 85 DEVMETHOD(pcib_write_config, rtaspci_write_config), 86 87 DEVMETHOD_END 88}; 89 90struct rtaspci_softc { 91 struct ofw_pci_softc pci_sc; 92 93 struct ofw_pci_register sc_pcir; 94 95 cell_t read_pci_config, write_pci_config; 96 cell_t ex_read_pci_config, ex_write_pci_config; 97 int sc_extended_config; 98}; 99 100static devclass_t rtaspci_devclass; 101DEFINE_CLASS_1(pcib, rtaspci_driver, rtaspci_methods, 102 sizeof(struct rtaspci_softc), ofw_pci_driver); 103DRIVER_MODULE(rtaspci, ofwbus, rtaspci_driver, rtaspci_devclass, 0, 0); 104 105static int 106rtaspci_probe(device_t dev) 107{ 108 const char *type; 109 110 if (!rtas_exists()) 111 return (ENXIO); 112 113 type = ofw_bus_get_type(dev); 114 115 if (OF_getproplen(ofw_bus_get_node(dev), "used-by-rtas") < 0) 116 return (ENXIO); 117 if (type == NULL || strcmp(type, "pci") != 0) 118 return (ENXIO); 119 120 device_set_desc(dev, "RTAS Host-PCI bridge"); 121 return (BUS_PROBE_GENERIC); 122} 123 124static int 125rtaspci_attach(device_t dev) 126{ 127 struct rtaspci_softc *sc; 128 129 sc = device_get_softc(dev); 130 131 if (OF_getencprop(ofw_bus_get_node(dev), "reg", (pcell_t *)&sc->sc_pcir, 132 sizeof(sc->sc_pcir)) == -1) 133 return (ENXIO); 134 135 sc->read_pci_config = rtas_token_lookup("read-pci-config"); 136 sc->write_pci_config = rtas_token_lookup("write-pci-config"); 137 sc->ex_read_pci_config = rtas_token_lookup("ibm,read-pci-config"); 138 sc->ex_write_pci_config = rtas_token_lookup("ibm,write-pci-config"); 139 140 sc->sc_extended_config = 0; 141 OF_getencprop(ofw_bus_get_node(dev), "ibm,pci-config-space-type", 142 &sc->sc_extended_config, sizeof(sc->sc_extended_config)); 143 144 return (ofw_pci_attach(dev)); 145} 146 147static uint32_t 148rtaspci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 149 int width) 150{ 151 struct rtaspci_softc *sc; 152 uint32_t retval = 0xffffffff; 153 uint32_t config_addr; 154 int error, pcierror; 155 156 sc = device_get_softc(dev); 157 158 config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) | 159 ((func & 0x7) << 8) | (reg & 0xff); 160 if (sc->sc_extended_config) 161 config_addr |= (reg & 0xf00) << 16; 162 163 if (sc->ex_read_pci_config != -1) 164 error = rtas_call_method(sc->ex_read_pci_config, 4, 2, 165 config_addr, sc->sc_pcir.phys_hi, 166 sc->sc_pcir.phys_mid, width, &pcierror, &retval); 167 else 168 error = rtas_call_method(sc->read_pci_config, 2, 2, 169 config_addr, width, &pcierror, &retval); 170 171 /* Sign-extend output */ 172 switch (width) { 173 case 1: 174 retval = (int32_t)(int8_t)(retval); 175 break; 176 case 2: 177 retval = (int32_t)(int16_t)(retval); 178 break; 179 } 180 181 if (error < 0 || pcierror != 0) 182 retval = 0xffffffff; 183 184 return (retval); 185} 186 187static void 188rtaspci_write_config(device_t dev, u_int bus, u_int slot, u_int func, 189 u_int reg, uint32_t val, int width) 190{ 191 struct rtaspci_softc *sc; 192 uint32_t config_addr; 193 int pcierror; 194 195 sc = device_get_softc(dev); 196 197 config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) | 198 ((func & 0x7) << 8) | (reg & 0xff); 199 if (sc->sc_extended_config) 200 config_addr |= (reg & 0xf00) << 16; 201 202 if (sc->ex_write_pci_config != -1) 203 rtas_call_method(sc->ex_write_pci_config, 5, 1, config_addr, 204 sc->sc_pcir.phys_hi, sc->sc_pcir.phys_mid, 205 width, val, &pcierror); 206 else 207 rtas_call_method(sc->write_pci_config, 3, 1, config_addr, 208 width, val, &pcierror); 209} 210 211