1faf1e708SIsaku Yamahata /* 2faf1e708SIsaku Yamahata * xio3130_upstream.c 3faf1e708SIsaku Yamahata * TI X3130 pci express upstream port switch 4faf1e708SIsaku Yamahata * 5faf1e708SIsaku Yamahata * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp> 6faf1e708SIsaku Yamahata * VA Linux Systems Japan K.K. 7faf1e708SIsaku Yamahata * 8faf1e708SIsaku Yamahata * This program is free software; you can redistribute it and/or modify 9faf1e708SIsaku Yamahata * it under the terms of the GNU General Public License as published by 10faf1e708SIsaku Yamahata * the Free Software Foundation; either version 2 of the License, or 11faf1e708SIsaku Yamahata * (at your option) any later version. 12faf1e708SIsaku Yamahata * 13faf1e708SIsaku Yamahata * This program is distributed in the hope that it will be useful, 14faf1e708SIsaku Yamahata * but WITHOUT ANY WARRANTY; without even the implied warranty of 15faf1e708SIsaku Yamahata * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16faf1e708SIsaku Yamahata * GNU General Public License for more details. 17faf1e708SIsaku Yamahata * 18faf1e708SIsaku Yamahata * You should have received a copy of the GNU General Public License along 19faf1e708SIsaku Yamahata * with this program; if not, see <http://www.gnu.org/licenses/>. 20faf1e708SIsaku Yamahata */ 21faf1e708SIsaku Yamahata 2297d5408fSPeter Maydell #include "qemu/osdep.h" 2383c9f4caSPaolo Bonzini #include "hw/pci/pci_ids.h" 2483c9f4caSPaolo Bonzini #include "hw/pci/msi.h" 2583c9f4caSPaolo Bonzini #include "hw/pci/pcie.h" 2647b43a1fSPaolo Bonzini #include "xio3130_upstream.h" 27faf1e708SIsaku Yamahata 28faf1e708SIsaku Yamahata #define PCI_DEVICE_ID_TI_XIO3130U 0x8232 /* upstream port */ 29faf1e708SIsaku Yamahata #define XIO3130_REVISION 0x2 30faf1e708SIsaku Yamahata #define XIO3130_MSI_OFFSET 0x70 31faf1e708SIsaku Yamahata #define XIO3130_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_64BIT 32faf1e708SIsaku Yamahata #define XIO3130_MSI_NR_VECTOR 1 33faf1e708SIsaku Yamahata #define XIO3130_SSVID_OFFSET 0x80 34faf1e708SIsaku Yamahata #define XIO3130_SSVID_SVID 0 35faf1e708SIsaku Yamahata #define XIO3130_SSVID_SSID 0 36faf1e708SIsaku Yamahata #define XIO3130_EXP_OFFSET 0x90 37faf1e708SIsaku Yamahata #define XIO3130_AER_OFFSET 0x100 38faf1e708SIsaku Yamahata 39faf1e708SIsaku Yamahata static void xio3130_upstream_write_config(PCIDevice *d, uint32_t address, 40faf1e708SIsaku Yamahata uint32_t val, int len) 41faf1e708SIsaku Yamahata { 42faf1e708SIsaku Yamahata pci_bridge_write_config(d, address, val, len); 43faf1e708SIsaku Yamahata pcie_cap_flr_write_config(d, address, val, len); 44a158f92fSIsaku Yamahata pcie_aer_write_config(d, address, val, len); 45faf1e708SIsaku Yamahata } 46faf1e708SIsaku Yamahata 47faf1e708SIsaku Yamahata static void xio3130_upstream_reset(DeviceState *qdev) 48faf1e708SIsaku Yamahata { 4940021f08SAnthony Liguori PCIDevice *d = PCI_DEVICE(qdev); 50cbd2d434SJan Kiszka 51faf1e708SIsaku Yamahata pci_bridge_reset(qdev); 52faf1e708SIsaku Yamahata pcie_cap_deverr_reset(d); 53faf1e708SIsaku Yamahata } 54faf1e708SIsaku Yamahata 55faf1e708SIsaku Yamahata static int xio3130_upstream_initfn(PCIDevice *d) 56faf1e708SIsaku Yamahata { 57bcb75750SAndreas Färber PCIEPort *p = PCIE_PORT(d); 58faf1e708SIsaku Yamahata int rc; 59faf1e708SIsaku Yamahata 609cfaa007SCao jin pci_bridge_initfn(d, TYPE_PCIE_BUS); 61faf1e708SIsaku Yamahata pcie_port_init_reg(d); 62faf1e708SIsaku Yamahata 63faf1e708SIsaku Yamahata rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR, 64faf1e708SIsaku Yamahata XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT, 65faf1e708SIsaku Yamahata XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT); 66faf1e708SIsaku Yamahata if (rc < 0) { 67a158f92fSIsaku Yamahata goto err_bridge; 68faf1e708SIsaku Yamahata } 69*52ea63deSCao jin 70faf1e708SIsaku Yamahata rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET, 71faf1e708SIsaku Yamahata XIO3130_SSVID_SVID, XIO3130_SSVID_SSID); 72faf1e708SIsaku Yamahata if (rc < 0) { 73a158f92fSIsaku Yamahata goto err_bridge; 74faf1e708SIsaku Yamahata } 75*52ea63deSCao jin 76faf1e708SIsaku Yamahata rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_UPSTREAM, 77faf1e708SIsaku Yamahata p->port); 78faf1e708SIsaku Yamahata if (rc < 0) { 79a158f92fSIsaku Yamahata goto err_msi; 80faf1e708SIsaku Yamahata } 81faf1e708SIsaku Yamahata pcie_cap_flr_init(d); 82faf1e708SIsaku Yamahata pcie_cap_deverr_init(d); 83*52ea63deSCao jin 848d86ada2SChen Fan rc = pcie_aer_init(d, XIO3130_AER_OFFSET, PCI_ERR_SIZEOF); 85a158f92fSIsaku Yamahata if (rc < 0) { 86a158f92fSIsaku Yamahata goto err; 87a158f92fSIsaku Yamahata } 88faf1e708SIsaku Yamahata 89faf1e708SIsaku Yamahata return 0; 90a158f92fSIsaku Yamahata 91a158f92fSIsaku Yamahata err: 92a158f92fSIsaku Yamahata pcie_cap_exit(d); 93a158f92fSIsaku Yamahata err_msi: 94a158f92fSIsaku Yamahata msi_uninit(d); 95a158f92fSIsaku Yamahata err_bridge: 96f90c2bcdSAlex Williamson pci_bridge_exitfn(d); 97a158f92fSIsaku Yamahata return rc; 98faf1e708SIsaku Yamahata } 99faf1e708SIsaku Yamahata 100f90c2bcdSAlex Williamson static void xio3130_upstream_exitfn(PCIDevice *d) 101faf1e708SIsaku Yamahata { 102a158f92fSIsaku Yamahata pcie_aer_exit(d); 103faf1e708SIsaku Yamahata pcie_cap_exit(d); 104a158f92fSIsaku Yamahata msi_uninit(d); 105f90c2bcdSAlex Williamson pci_bridge_exitfn(d); 106faf1e708SIsaku Yamahata } 107faf1e708SIsaku Yamahata 108faf1e708SIsaku Yamahata PCIEPort *xio3130_upstream_init(PCIBus *bus, int devfn, bool multifunction, 109faf1e708SIsaku Yamahata const char *bus_name, pci_map_irq_fn map_irq, 110faf1e708SIsaku Yamahata uint8_t port) 111faf1e708SIsaku Yamahata { 112faf1e708SIsaku Yamahata PCIDevice *d; 113faf1e708SIsaku Yamahata PCIBridge *br; 114faf1e708SIsaku Yamahata DeviceState *qdev; 115faf1e708SIsaku Yamahata 116faf1e708SIsaku Yamahata d = pci_create_multifunction(bus, devfn, multifunction, "x3130-upstream"); 117faf1e708SIsaku Yamahata if (!d) { 118faf1e708SIsaku Yamahata return NULL; 119faf1e708SIsaku Yamahata } 120f055e96bSAndreas Färber br = PCI_BRIDGE(d); 121faf1e708SIsaku Yamahata 122f055e96bSAndreas Färber qdev = DEVICE(d); 123faf1e708SIsaku Yamahata pci_bridge_map_irq(br, bus_name, map_irq); 124faf1e708SIsaku Yamahata qdev_prop_set_uint8(qdev, "port", port); 125faf1e708SIsaku Yamahata qdev_init_nofail(qdev); 126faf1e708SIsaku Yamahata 127bcb75750SAndreas Färber return PCIE_PORT(d); 128faf1e708SIsaku Yamahata } 129faf1e708SIsaku Yamahata 130faf1e708SIsaku Yamahata static const VMStateDescription vmstate_xio3130_upstream = { 131faf1e708SIsaku Yamahata .name = "xio3130-express-upstream-port", 132faf1e708SIsaku Yamahata .version_id = 1, 133faf1e708SIsaku Yamahata .minimum_version_id = 1, 134faf1e708SIsaku Yamahata .fields = (VMStateField[]) { 135bcb75750SAndreas Färber VMSTATE_PCIE_DEVICE(parent_obj.parent_obj, PCIEPort), 136bcb75750SAndreas Färber VMSTATE_STRUCT(parent_obj.parent_obj.exp.aer_log, PCIEPort, 0, 137f055e96bSAndreas Färber vmstate_pcie_aer_log, PCIEAERLog), 138faf1e708SIsaku Yamahata VMSTATE_END_OF_LIST() 139faf1e708SIsaku Yamahata } 140faf1e708SIsaku Yamahata }; 141faf1e708SIsaku Yamahata 14240021f08SAnthony Liguori static void xio3130_upstream_class_init(ObjectClass *klass, void *data) 14340021f08SAnthony Liguori { 14439bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 14540021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 14640021f08SAnthony Liguori 14740021f08SAnthony Liguori k->is_express = 1; 14840021f08SAnthony Liguori k->is_bridge = 1; 14940021f08SAnthony Liguori k->config_write = xio3130_upstream_write_config; 15040021f08SAnthony Liguori k->init = xio3130_upstream_initfn; 15140021f08SAnthony Liguori k->exit = xio3130_upstream_exitfn; 15240021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_TI; 15340021f08SAnthony Liguori k->device_id = PCI_DEVICE_ID_TI_XIO3130U; 15440021f08SAnthony Liguori k->revision = XIO3130_REVISION; 155125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 15639bffca2SAnthony Liguori dc->desc = "TI X3130 Upstream Port of PCI Express Switch"; 15739bffca2SAnthony Liguori dc->reset = xio3130_upstream_reset; 15839bffca2SAnthony Liguori dc->vmsd = &vmstate_xio3130_upstream; 159faf1e708SIsaku Yamahata } 16040021f08SAnthony Liguori 1618c43a6f0SAndreas Färber static const TypeInfo xio3130_upstream_info = { 16240021f08SAnthony Liguori .name = "x3130-upstream", 163bcb75750SAndreas Färber .parent = TYPE_PCIE_PORT, 16440021f08SAnthony Liguori .class_init = xio3130_upstream_class_init, 165faf1e708SIsaku Yamahata }; 166faf1e708SIsaku Yamahata 16783f7d43aSAndreas Färber static void xio3130_upstream_register_types(void) 168faf1e708SIsaku Yamahata { 16939bffca2SAnthony Liguori type_register_static(&xio3130_upstream_info); 170faf1e708SIsaku Yamahata } 171faf1e708SIsaku Yamahata 17283f7d43aSAndreas Färber type_init(xio3130_upstream_register_types) 173faf1e708SIsaku Yamahata 174faf1e708SIsaku Yamahata 175faf1e708SIsaku Yamahata /* 176faf1e708SIsaku Yamahata * Local variables: 177faf1e708SIsaku Yamahata * c-indent-level: 4 178faf1e708SIsaku Yamahata * c-basic-offset: 4 179faf1e708SIsaku Yamahata * tab-width: 8 180faf1e708SIsaku Yamahata * indent-tab-mode: nil 181faf1e708SIsaku Yamahata * End: 182faf1e708SIsaku Yamahata */ 183