xref: /freebsd-src/sys/powerpc/pseries/rtas_pci.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
17a8d25c0SNathan Whitehorn /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
47a8d25c0SNathan Whitehorn  * Copyright (c) 2011 Nathan Whitehorn
57a8d25c0SNathan Whitehorn  * All rights reserved.
67a8d25c0SNathan Whitehorn  *
77a8d25c0SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
87a8d25c0SNathan Whitehorn  * modification, are permitted provided that the following conditions
97a8d25c0SNathan Whitehorn  * are met:
107a8d25c0SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
117a8d25c0SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
127a8d25c0SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
137a8d25c0SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
147a8d25c0SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
157a8d25c0SNathan Whitehorn  *
167a8d25c0SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177a8d25c0SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187a8d25c0SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197a8d25c0SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207a8d25c0SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217a8d25c0SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227a8d25c0SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237a8d25c0SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247a8d25c0SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257a8d25c0SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267a8d25c0SNathan Whitehorn  * SUCH DAMAGE.
277a8d25c0SNathan Whitehorn  */
287a8d25c0SNathan Whitehorn 
297a8d25c0SNathan Whitehorn #include <sys/param.h>
307a8d25c0SNathan Whitehorn #include <sys/systm.h>
317a8d25c0SNathan Whitehorn #include <sys/module.h>
327a8d25c0SNathan Whitehorn #include <sys/bus.h>
337a8d25c0SNathan Whitehorn #include <sys/conf.h>
347a8d25c0SNathan Whitehorn #include <sys/kernel.h>
35c43a8674SZbigniew Bodek #include <sys/rman.h>
367a8d25c0SNathan Whitehorn 
377a8d25c0SNathan Whitehorn #include <dev/ofw/openfirm.h>
3836e9c2ceSZbigniew Bodek #include <dev/ofw/ofw_pci.h>
397a8d25c0SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
407a8d25c0SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
41c43a8674SZbigniew Bodek #include <dev/ofw/ofwpci.h>
427a8d25c0SNathan Whitehorn 
437a8d25c0SNathan Whitehorn #include <dev/pci/pcivar.h>
447a8d25c0SNathan Whitehorn #include <dev/pci/pcireg.h>
457a8d25c0SNathan Whitehorn 
467a8d25c0SNathan Whitehorn #include <machine/bus.h>
477a8d25c0SNathan Whitehorn #include <machine/intr_machdep.h>
487a8d25c0SNathan Whitehorn #include <machine/md_var.h>
497a8d25c0SNathan Whitehorn #include <machine/pio.h>
507a8d25c0SNathan Whitehorn #include <machine/resource.h>
517a8d25c0SNathan Whitehorn #include <machine/rtas.h>
527a8d25c0SNathan Whitehorn 
537a8d25c0SNathan Whitehorn #include <vm/vm.h>
547a8d25c0SNathan Whitehorn #include <vm/pmap.h>
557a8d25c0SNathan Whitehorn 
567a8d25c0SNathan Whitehorn #include <powerpc/pseries/plpar_iommu.h>
577a8d25c0SNathan Whitehorn 
587a8d25c0SNathan Whitehorn #include "pcib_if.h"
597a8d25c0SNathan Whitehorn #include "iommu_if.h"
607a8d25c0SNathan Whitehorn 
617a8d25c0SNathan Whitehorn /*
627a8d25c0SNathan Whitehorn  * Device interface.
637a8d25c0SNathan Whitehorn  */
647a8d25c0SNathan Whitehorn static int		rtaspci_probe(device_t);
657a8d25c0SNathan Whitehorn static int		rtaspci_attach(device_t);
667a8d25c0SNathan Whitehorn 
677a8d25c0SNathan Whitehorn /*
687a8d25c0SNathan Whitehorn  * pcib interface.
697a8d25c0SNathan Whitehorn  */
707a8d25c0SNathan Whitehorn static u_int32_t	rtaspci_read_config(device_t, u_int, u_int, u_int,
717a8d25c0SNathan Whitehorn 			    u_int, int);
727a8d25c0SNathan Whitehorn static void		rtaspci_write_config(device_t, u_int, u_int, u_int,
737a8d25c0SNathan Whitehorn 			    u_int, u_int32_t, int);
747a8d25c0SNathan Whitehorn 
757a8d25c0SNathan Whitehorn /*
767a8d25c0SNathan Whitehorn  * Driver methods.
777a8d25c0SNathan Whitehorn  */
787a8d25c0SNathan Whitehorn static device_method_t	rtaspci_methods[] = {
797a8d25c0SNathan Whitehorn 	/* Device interface */
807a8d25c0SNathan Whitehorn 	DEVMETHOD(device_probe,		rtaspci_probe),
817a8d25c0SNathan Whitehorn 	DEVMETHOD(device_attach,	rtaspci_attach),
827a8d25c0SNathan Whitehorn 
837a8d25c0SNathan Whitehorn 	/* pcib interface */
847a8d25c0SNathan Whitehorn 	DEVMETHOD(pcib_read_config,	rtaspci_read_config),
857a8d25c0SNathan Whitehorn 	DEVMETHOD(pcib_write_config,	rtaspci_write_config),
867a8d25c0SNathan Whitehorn 
877a8d25c0SNathan Whitehorn 	DEVMETHOD_END
887a8d25c0SNathan Whitehorn };
897a8d25c0SNathan Whitehorn 
907a8d25c0SNathan Whitehorn struct rtaspci_softc {
917a8d25c0SNathan Whitehorn 	struct ofw_pci_softc	pci_sc;
927a8d25c0SNathan Whitehorn 
93613d01feSNathan Whitehorn 	struct ofw_pci_register	sc_pcir;
94613d01feSNathan Whitehorn 
957a8d25c0SNathan Whitehorn 	cell_t			read_pci_config, write_pci_config;
967a8d25c0SNathan Whitehorn 	cell_t			ex_read_pci_config, ex_write_pci_config;
977a8d25c0SNathan Whitehorn 	int			sc_extended_config;
987a8d25c0SNathan Whitehorn };
997a8d25c0SNathan Whitehorn 
1007a8d25c0SNathan Whitehorn DEFINE_CLASS_1(pcib, rtaspci_driver, rtaspci_methods,
10124042910SMarcin Wojtas     sizeof(struct rtaspci_softc), ofw_pcib_driver);
1029b9a5327SJohn Baldwin DRIVER_MODULE(rtaspci, ofwbus, rtaspci_driver, 0, 0);
1037a8d25c0SNathan Whitehorn 
1047a8d25c0SNathan Whitehorn static int
rtaspci_probe(device_t dev)1057a8d25c0SNathan Whitehorn rtaspci_probe(device_t dev)
1067a8d25c0SNathan Whitehorn {
1077a8d25c0SNathan Whitehorn 	const char	*type;
1087a8d25c0SNathan Whitehorn 
1097a8d25c0SNathan Whitehorn 	if (!rtas_exists())
1107a8d25c0SNathan Whitehorn 		return (ENXIO);
1117a8d25c0SNathan Whitehorn 
1127a8d25c0SNathan Whitehorn 	type = ofw_bus_get_type(dev);
1137a8d25c0SNathan Whitehorn 
1147a8d25c0SNathan Whitehorn 	if (OF_getproplen(ofw_bus_get_node(dev), "used-by-rtas") < 0)
1157a8d25c0SNathan Whitehorn 		return (ENXIO);
1167a8d25c0SNathan Whitehorn 	if (type == NULL || strcmp(type, "pci") != 0)
1177a8d25c0SNathan Whitehorn 		return (ENXIO);
1187a8d25c0SNathan Whitehorn 
1197a8d25c0SNathan Whitehorn 	device_set_desc(dev, "RTAS Host-PCI bridge");
1207a8d25c0SNathan Whitehorn 	return (BUS_PROBE_GENERIC);
1217a8d25c0SNathan Whitehorn }
1227a8d25c0SNathan Whitehorn 
1237a8d25c0SNathan Whitehorn static int
rtaspci_attach(device_t dev)1247a8d25c0SNathan Whitehorn rtaspci_attach(device_t dev)
1257a8d25c0SNathan Whitehorn {
1267a8d25c0SNathan Whitehorn 	struct		rtaspci_softc *sc;
1277a8d25c0SNathan Whitehorn 
1287a8d25c0SNathan Whitehorn 	sc = device_get_softc(dev);
1297a8d25c0SNathan Whitehorn 
130613d01feSNathan Whitehorn 	if (OF_getencprop(ofw_bus_get_node(dev), "reg", (pcell_t *)&sc->sc_pcir,
131613d01feSNathan Whitehorn 	    sizeof(sc->sc_pcir)) == -1)
132613d01feSNathan Whitehorn 		return (ENXIO);
133613d01feSNathan Whitehorn 
1347a8d25c0SNathan Whitehorn 	sc->read_pci_config = rtas_token_lookup("read-pci-config");
1357a8d25c0SNathan Whitehorn 	sc->write_pci_config = rtas_token_lookup("write-pci-config");
1367a8d25c0SNathan Whitehorn 	sc->ex_read_pci_config = rtas_token_lookup("ibm,read-pci-config");
1377a8d25c0SNathan Whitehorn 	sc->ex_write_pci_config = rtas_token_lookup("ibm,write-pci-config");
1387a8d25c0SNathan Whitehorn 
1397a8d25c0SNathan Whitehorn 	sc->sc_extended_config = 0;
140509142e1SNathan Whitehorn 	OF_getencprop(ofw_bus_get_node(dev), "ibm,pci-config-space-type",
1417a8d25c0SNathan Whitehorn 	    &sc->sc_extended_config, sizeof(sc->sc_extended_config));
1427a8d25c0SNathan Whitehorn 
14324042910SMarcin Wojtas 	return (ofw_pcib_attach(dev));
1447a8d25c0SNathan Whitehorn }
1457a8d25c0SNathan Whitehorn 
1467a8d25c0SNathan Whitehorn static uint32_t
rtaspci_read_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,int width)1477a8d25c0SNathan Whitehorn rtaspci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
1487a8d25c0SNathan Whitehorn     int width)
1497a8d25c0SNathan Whitehorn {
1507a8d25c0SNathan Whitehorn 	struct rtaspci_softc *sc;
1517a8d25c0SNathan Whitehorn 	uint32_t retval = 0xffffffff;
1527a8d25c0SNathan Whitehorn 	uint32_t config_addr;
1537a8d25c0SNathan Whitehorn 	int error, pcierror;
1547a8d25c0SNathan Whitehorn 
1557a8d25c0SNathan Whitehorn 	sc = device_get_softc(dev);
1567a8d25c0SNathan Whitehorn 
1577a8d25c0SNathan Whitehorn 	config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) |
1587a8d25c0SNathan Whitehorn 	    ((func & 0x7) << 8) | (reg & 0xff);
1597a8d25c0SNathan Whitehorn 	if (sc->sc_extended_config)
1607a8d25c0SNathan Whitehorn 		config_addr |= (reg & 0xf00) << 16;
1617a8d25c0SNathan Whitehorn 
1627a8d25c0SNathan Whitehorn 	if (sc->ex_read_pci_config != -1)
1637a8d25c0SNathan Whitehorn 		error = rtas_call_method(sc->ex_read_pci_config, 4, 2,
164613d01feSNathan Whitehorn 		    config_addr, sc->sc_pcir.phys_hi,
165613d01feSNathan Whitehorn 		    sc->sc_pcir.phys_mid, width, &pcierror, &retval);
1667a8d25c0SNathan Whitehorn 	else
1677a8d25c0SNathan Whitehorn 		error = rtas_call_method(sc->read_pci_config, 2, 2,
1687a8d25c0SNathan Whitehorn 		    config_addr, width, &pcierror, &retval);
1697a8d25c0SNathan Whitehorn 
1707a8d25c0SNathan Whitehorn 	/* Sign-extend output */
1717a8d25c0SNathan Whitehorn 	switch (width) {
1727a8d25c0SNathan Whitehorn 	case 1:
1737a8d25c0SNathan Whitehorn 		retval = (int32_t)(int8_t)(retval);
1747a8d25c0SNathan Whitehorn 		break;
1757a8d25c0SNathan Whitehorn 	case 2:
1767a8d25c0SNathan Whitehorn 		retval = (int32_t)(int16_t)(retval);
1777a8d25c0SNathan Whitehorn 		break;
1787a8d25c0SNathan Whitehorn 	}
1797a8d25c0SNathan Whitehorn 
1807a8d25c0SNathan Whitehorn 	if (error < 0 || pcierror != 0)
1817a8d25c0SNathan Whitehorn 		retval = 0xffffffff;
1827a8d25c0SNathan Whitehorn 
1837a8d25c0SNathan Whitehorn 	return (retval);
1847a8d25c0SNathan Whitehorn }
1857a8d25c0SNathan Whitehorn 
1867a8d25c0SNathan Whitehorn static void
rtaspci_write_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,uint32_t val,int width)1877a8d25c0SNathan Whitehorn rtaspci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
1887a8d25c0SNathan Whitehorn     u_int reg, uint32_t val, int width)
1897a8d25c0SNathan Whitehorn {
1907a8d25c0SNathan Whitehorn 	struct rtaspci_softc *sc;
1917a8d25c0SNathan Whitehorn 	uint32_t config_addr;
1927a8d25c0SNathan Whitehorn 	int pcierror;
1937a8d25c0SNathan Whitehorn 
1947a8d25c0SNathan Whitehorn 	sc = device_get_softc(dev);
1957a8d25c0SNathan Whitehorn 
1967a8d25c0SNathan Whitehorn 	config_addr = ((bus & 0xff) << 16) | ((slot & 0x1f) << 11) |
1977a8d25c0SNathan Whitehorn 	    ((func & 0x7) << 8) | (reg & 0xff);
1987a8d25c0SNathan Whitehorn 	if (sc->sc_extended_config)
1997a8d25c0SNathan Whitehorn 		config_addr |= (reg & 0xf00) << 16;
2007a8d25c0SNathan Whitehorn 
2017a8d25c0SNathan Whitehorn 	if (sc->ex_write_pci_config != -1)
2027a8d25c0SNathan Whitehorn 		rtas_call_method(sc->ex_write_pci_config, 5, 1, config_addr,
203613d01feSNathan Whitehorn 		    sc->sc_pcir.phys_hi, sc->sc_pcir.phys_mid,
2047a8d25c0SNathan Whitehorn 		    width, val, &pcierror);
2057a8d25c0SNathan Whitehorn 	else
2067a8d25c0SNathan Whitehorn 		rtas_call_method(sc->write_pci_config, 3, 1, config_addr,
2077a8d25c0SNathan Whitehorn 		    width, val, &pcierror);
2087a8d25c0SNathan Whitehorn }
209