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 2283c9f4caSPaolo Bonzini #include "hw/pci/pci_ids.h" 2383c9f4caSPaolo Bonzini #include "hw/pci/msi.h" 2483c9f4caSPaolo Bonzini #include "hw/pci/pcie.h" 2547b43a1fSPaolo Bonzini #include "xio3130_upstream.h" 26faf1e708SIsaku Yamahata 27faf1e708SIsaku Yamahata #define PCI_DEVICE_ID_TI_XIO3130U 0x8232 /* upstream port */ 28faf1e708SIsaku Yamahata #define XIO3130_REVISION 0x2 29faf1e708SIsaku Yamahata #define XIO3130_MSI_OFFSET 0x70 30faf1e708SIsaku Yamahata #define XIO3130_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_64BIT 31faf1e708SIsaku Yamahata #define XIO3130_MSI_NR_VECTOR 1 32faf1e708SIsaku Yamahata #define XIO3130_SSVID_OFFSET 0x80 33faf1e708SIsaku Yamahata #define XIO3130_SSVID_SVID 0 34faf1e708SIsaku Yamahata #define XIO3130_SSVID_SSID 0 35faf1e708SIsaku Yamahata #define XIO3130_EXP_OFFSET 0x90 36faf1e708SIsaku Yamahata #define XIO3130_AER_OFFSET 0x100 37faf1e708SIsaku Yamahata 38faf1e708SIsaku Yamahata static void xio3130_upstream_write_config(PCIDevice *d, uint32_t address, 39faf1e708SIsaku Yamahata uint32_t val, int len) 40faf1e708SIsaku Yamahata { 41faf1e708SIsaku Yamahata pci_bridge_write_config(d, address, val, len); 42faf1e708SIsaku Yamahata pcie_cap_flr_write_config(d, address, val, len); 43a158f92fSIsaku Yamahata pcie_aer_write_config(d, address, val, len); 44faf1e708SIsaku Yamahata } 45faf1e708SIsaku Yamahata 46faf1e708SIsaku Yamahata static void xio3130_upstream_reset(DeviceState *qdev) 47faf1e708SIsaku Yamahata { 4840021f08SAnthony Liguori PCIDevice *d = PCI_DEVICE(qdev); 49cbd2d434SJan Kiszka 50faf1e708SIsaku Yamahata pci_bridge_reset(qdev); 51faf1e708SIsaku Yamahata pcie_cap_deverr_reset(d); 52faf1e708SIsaku Yamahata } 53faf1e708SIsaku Yamahata 54faf1e708SIsaku Yamahata static int xio3130_upstream_initfn(PCIDevice *d) 55faf1e708SIsaku Yamahata { 56faf1e708SIsaku Yamahata PCIBridge* br = DO_UPCAST(PCIBridge, dev, d); 57faf1e708SIsaku Yamahata PCIEPort *p = DO_UPCAST(PCIEPort, br, br); 58faf1e708SIsaku Yamahata int rc; 59faf1e708SIsaku Yamahata 60afb661ebSAlex Williamson rc = pci_bridge_initfn(d, TYPE_PCIE_BUS); 61faf1e708SIsaku Yamahata if (rc < 0) { 62faf1e708SIsaku Yamahata return rc; 63faf1e708SIsaku Yamahata } 64faf1e708SIsaku Yamahata 65faf1e708SIsaku Yamahata pcie_port_init_reg(d); 66faf1e708SIsaku Yamahata 67faf1e708SIsaku Yamahata rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR, 68faf1e708SIsaku Yamahata XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT, 69faf1e708SIsaku Yamahata XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT); 70faf1e708SIsaku Yamahata if (rc < 0) { 71a158f92fSIsaku Yamahata goto err_bridge; 72faf1e708SIsaku Yamahata } 73faf1e708SIsaku Yamahata rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET, 74faf1e708SIsaku Yamahata XIO3130_SSVID_SVID, XIO3130_SSVID_SSID); 75faf1e708SIsaku Yamahata if (rc < 0) { 76a158f92fSIsaku Yamahata goto err_bridge; 77faf1e708SIsaku Yamahata } 78faf1e708SIsaku Yamahata rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_UPSTREAM, 79faf1e708SIsaku Yamahata p->port); 80faf1e708SIsaku Yamahata if (rc < 0) { 81a158f92fSIsaku Yamahata goto err_msi; 82faf1e708SIsaku Yamahata } 83faf1e708SIsaku Yamahata pcie_cap_flr_init(d); 84faf1e708SIsaku Yamahata pcie_cap_deverr_init(d); 85a158f92fSIsaku Yamahata rc = pcie_aer_init(d, XIO3130_AER_OFFSET); 86a158f92fSIsaku Yamahata if (rc < 0) { 87a158f92fSIsaku Yamahata goto err; 88a158f92fSIsaku Yamahata } 89faf1e708SIsaku Yamahata 90faf1e708SIsaku Yamahata return 0; 91a158f92fSIsaku Yamahata 92a158f92fSIsaku Yamahata err: 93a158f92fSIsaku Yamahata pcie_cap_exit(d); 94a158f92fSIsaku Yamahata err_msi: 95a158f92fSIsaku Yamahata msi_uninit(d); 96a158f92fSIsaku Yamahata err_bridge: 97f90c2bcdSAlex Williamson pci_bridge_exitfn(d); 98a158f92fSIsaku Yamahata return rc; 99faf1e708SIsaku Yamahata } 100faf1e708SIsaku Yamahata 101f90c2bcdSAlex Williamson static void xio3130_upstream_exitfn(PCIDevice *d) 102faf1e708SIsaku Yamahata { 103a158f92fSIsaku Yamahata pcie_aer_exit(d); 104faf1e708SIsaku Yamahata pcie_cap_exit(d); 105a158f92fSIsaku Yamahata msi_uninit(d); 106f90c2bcdSAlex Williamson pci_bridge_exitfn(d); 107faf1e708SIsaku Yamahata } 108faf1e708SIsaku Yamahata 109faf1e708SIsaku Yamahata PCIEPort *xio3130_upstream_init(PCIBus *bus, int devfn, bool multifunction, 110faf1e708SIsaku Yamahata const char *bus_name, pci_map_irq_fn map_irq, 111faf1e708SIsaku Yamahata uint8_t port) 112faf1e708SIsaku Yamahata { 113faf1e708SIsaku Yamahata PCIDevice *d; 114faf1e708SIsaku Yamahata PCIBridge *br; 115faf1e708SIsaku Yamahata DeviceState *qdev; 116faf1e708SIsaku Yamahata 117faf1e708SIsaku Yamahata d = pci_create_multifunction(bus, devfn, multifunction, "x3130-upstream"); 118faf1e708SIsaku Yamahata if (!d) { 119faf1e708SIsaku Yamahata return NULL; 120faf1e708SIsaku Yamahata } 121faf1e708SIsaku Yamahata br = DO_UPCAST(PCIBridge, dev, d); 122faf1e708SIsaku Yamahata 123faf1e708SIsaku Yamahata qdev = &br->dev.qdev; 124faf1e708SIsaku Yamahata pci_bridge_map_irq(br, bus_name, map_irq); 125faf1e708SIsaku Yamahata qdev_prop_set_uint8(qdev, "port", port); 126faf1e708SIsaku Yamahata qdev_init_nofail(qdev); 127faf1e708SIsaku Yamahata 128faf1e708SIsaku Yamahata return DO_UPCAST(PCIEPort, br, br); 129faf1e708SIsaku Yamahata } 130faf1e708SIsaku Yamahata 131faf1e708SIsaku Yamahata static const VMStateDescription vmstate_xio3130_upstream = { 132faf1e708SIsaku Yamahata .name = "xio3130-express-upstream-port", 133faf1e708SIsaku Yamahata .version_id = 1, 134faf1e708SIsaku Yamahata .minimum_version_id = 1, 135faf1e708SIsaku Yamahata .minimum_version_id_old = 1, 136faf1e708SIsaku Yamahata .fields = (VMStateField[]) { 137faf1e708SIsaku Yamahata VMSTATE_PCIE_DEVICE(br.dev, PCIEPort), 138a158f92fSIsaku Yamahata VMSTATE_STRUCT(br.dev.exp.aer_log, PCIEPort, 0, vmstate_pcie_aer_log, 139a158f92fSIsaku Yamahata PCIEAERLog), 140faf1e708SIsaku Yamahata VMSTATE_END_OF_LIST() 141faf1e708SIsaku Yamahata } 142faf1e708SIsaku Yamahata }; 143faf1e708SIsaku Yamahata 14440021f08SAnthony Liguori static Property xio3130_upstream_properties[] = { 145faf1e708SIsaku Yamahata DEFINE_PROP_UINT8("port", PCIEPort, port, 0), 146a158f92fSIsaku Yamahata DEFINE_PROP_UINT16("aer_log_max", PCIEPort, br.dev.exp.aer_log.log_max, 147a158f92fSIsaku Yamahata PCIE_AER_LOG_MAX_DEFAULT), 148faf1e708SIsaku Yamahata DEFINE_PROP_END_OF_LIST(), 14940021f08SAnthony Liguori }; 15040021f08SAnthony Liguori 15140021f08SAnthony Liguori static void xio3130_upstream_class_init(ObjectClass *klass, void *data) 15240021f08SAnthony Liguori { 15339bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 15440021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 15540021f08SAnthony Liguori 15640021f08SAnthony Liguori k->is_express = 1; 15740021f08SAnthony Liguori k->is_bridge = 1; 15840021f08SAnthony Liguori k->config_write = xio3130_upstream_write_config; 15940021f08SAnthony Liguori k->init = xio3130_upstream_initfn; 16040021f08SAnthony Liguori k->exit = xio3130_upstream_exitfn; 16140021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_TI; 16240021f08SAnthony Liguori k->device_id = PCI_DEVICE_ID_TI_XIO3130U; 16340021f08SAnthony Liguori k->revision = XIO3130_REVISION; 164*125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 16539bffca2SAnthony Liguori dc->desc = "TI X3130 Upstream Port of PCI Express Switch"; 16639bffca2SAnthony Liguori dc->reset = xio3130_upstream_reset; 16739bffca2SAnthony Liguori dc->vmsd = &vmstate_xio3130_upstream; 16839bffca2SAnthony Liguori dc->props = xio3130_upstream_properties; 169faf1e708SIsaku Yamahata } 17040021f08SAnthony Liguori 1718c43a6f0SAndreas Färber static const TypeInfo xio3130_upstream_info = { 17240021f08SAnthony Liguori .name = "x3130-upstream", 17339bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 17439bffca2SAnthony Liguori .instance_size = sizeof(PCIEPort), 17540021f08SAnthony Liguori .class_init = xio3130_upstream_class_init, 176faf1e708SIsaku Yamahata }; 177faf1e708SIsaku Yamahata 17883f7d43aSAndreas Färber static void xio3130_upstream_register_types(void) 179faf1e708SIsaku Yamahata { 18039bffca2SAnthony Liguori type_register_static(&xio3130_upstream_info); 181faf1e708SIsaku Yamahata } 182faf1e708SIsaku Yamahata 18383f7d43aSAndreas Färber type_init(xio3130_upstream_register_types) 184faf1e708SIsaku Yamahata 185faf1e708SIsaku Yamahata 186faf1e708SIsaku Yamahata /* 187faf1e708SIsaku Yamahata * Local variables: 188faf1e708SIsaku Yamahata * c-indent-level: 4 189faf1e708SIsaku Yamahata * c-basic-offset: 4 190faf1e708SIsaku Yamahata * tab-width: 8 191faf1e708SIsaku Yamahata * indent-tab-mode: nil 192faf1e708SIsaku Yamahata * End: 193faf1e708SIsaku Yamahata */ 194