xref: /qemu/hw/pci-bridge/xio3130_downstream.c (revision f18c697b55d1374af67b84c581abaece8ab2aca3)
148ebf2f9SIsaku Yamahata /*
248ebf2f9SIsaku Yamahata  * x3130_downstream.c
348ebf2f9SIsaku Yamahata  * TI X3130 pci express downstream port switch
448ebf2f9SIsaku Yamahata  *
548ebf2f9SIsaku Yamahata  * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
648ebf2f9SIsaku Yamahata  *                    VA Linux Systems Japan K.K.
748ebf2f9SIsaku Yamahata  *
848ebf2f9SIsaku Yamahata  * This program is free software; you can redistribute it and/or modify
948ebf2f9SIsaku Yamahata  * it under the terms of the GNU General Public License as published by
1048ebf2f9SIsaku Yamahata  * the Free Software Foundation; either version 2 of the License, or
1148ebf2f9SIsaku Yamahata  * (at your option) any later version.
1248ebf2f9SIsaku Yamahata  *
1348ebf2f9SIsaku Yamahata  * This program is distributed in the hope that it will be useful,
1448ebf2f9SIsaku Yamahata  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1548ebf2f9SIsaku Yamahata  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1648ebf2f9SIsaku Yamahata  * GNU General Public License for more details.
1748ebf2f9SIsaku Yamahata  *
1848ebf2f9SIsaku Yamahata  * You should have received a copy of the GNU General Public License along
1948ebf2f9SIsaku Yamahata  * with this program; if not, see <http://www.gnu.org/licenses/>.
2048ebf2f9SIsaku Yamahata  */
2148ebf2f9SIsaku 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_downstream.h"
271108b2f8SCao jin #include "qapi/error.h"
2848ebf2f9SIsaku Yamahata 
2948ebf2f9SIsaku Yamahata #define PCI_DEVICE_ID_TI_XIO3130D       0x8233  /* downstream port */
3048ebf2f9SIsaku Yamahata #define XIO3130_REVISION                0x1
3148ebf2f9SIsaku Yamahata #define XIO3130_MSI_OFFSET              0x70
3248ebf2f9SIsaku Yamahata #define XIO3130_MSI_SUPPORTED_FLAGS     PCI_MSI_FLAGS_64BIT
3348ebf2f9SIsaku Yamahata #define XIO3130_MSI_NR_VECTOR           1
3448ebf2f9SIsaku Yamahata #define XIO3130_SSVID_OFFSET            0x80
3548ebf2f9SIsaku Yamahata #define XIO3130_SSVID_SVID              0
3648ebf2f9SIsaku Yamahata #define XIO3130_SSVID_SSID              0
3748ebf2f9SIsaku Yamahata #define XIO3130_EXP_OFFSET              0x90
3848ebf2f9SIsaku Yamahata #define XIO3130_AER_OFFSET              0x100
3948ebf2f9SIsaku Yamahata 
4048ebf2f9SIsaku Yamahata static void xio3130_downstream_write_config(PCIDevice *d, uint32_t address,
4148ebf2f9SIsaku Yamahata                                          uint32_t val, int len)
4248ebf2f9SIsaku Yamahata {
4348ebf2f9SIsaku Yamahata     pci_bridge_write_config(d, address, val, len);
4448ebf2f9SIsaku Yamahata     pcie_cap_flr_write_config(d, address, val, len);
456bde6aaaSMichael S. Tsirkin     pcie_cap_slot_write_config(d, address, val, len);
4609b926d4SIsaku Yamahata     pcie_aer_write_config(d, address, val, len);
4748ebf2f9SIsaku Yamahata }
4848ebf2f9SIsaku Yamahata 
4948ebf2f9SIsaku Yamahata static void xio3130_downstream_reset(DeviceState *qdev)
5048ebf2f9SIsaku Yamahata {
5140021f08SAnthony Liguori     PCIDevice *d = PCI_DEVICE(qdev);
52cbd2d434SJan Kiszka 
5348ebf2f9SIsaku Yamahata     pcie_cap_deverr_reset(d);
5448ebf2f9SIsaku Yamahata     pcie_cap_slot_reset(d);
55821be9dbSKnut Omang     pcie_cap_arifwd_reset(d);
5648ebf2f9SIsaku Yamahata     pci_bridge_reset(qdev);
5748ebf2f9SIsaku Yamahata }
5848ebf2f9SIsaku Yamahata 
5948ebf2f9SIsaku Yamahata static int xio3130_downstream_initfn(PCIDevice *d)
6048ebf2f9SIsaku Yamahata {
61bcb75750SAndreas Färber     PCIEPort *p = PCIE_PORT(d);
62bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
6348ebf2f9SIsaku Yamahata     int rc;
641108b2f8SCao jin     Error *err = NULL;
6548ebf2f9SIsaku Yamahata 
669cfaa007SCao jin     pci_bridge_initfn(d, TYPE_PCIE_BUS);
6748ebf2f9SIsaku Yamahata     pcie_port_init_reg(d);
6848ebf2f9SIsaku Yamahata 
6948ebf2f9SIsaku Yamahata     rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR,
7048ebf2f9SIsaku Yamahata                   XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
711108b2f8SCao jin                   XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT, &err);
7248ebf2f9SIsaku Yamahata     if (rc < 0) {
731108b2f8SCao jin         assert(rc == -ENOTSUP);
741108b2f8SCao jin         error_report_err(err);
7509b926d4SIsaku Yamahata         goto err_bridge;
7648ebf2f9SIsaku Yamahata     }
7752ea63deSCao jin 
7848ebf2f9SIsaku Yamahata     rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET,
7948ebf2f9SIsaku Yamahata                                XIO3130_SSVID_SVID, XIO3130_SSVID_SSID);
8048ebf2f9SIsaku Yamahata     if (rc < 0) {
8109b926d4SIsaku Yamahata         goto err_bridge;
8248ebf2f9SIsaku Yamahata     }
8352ea63deSCao jin 
8448ebf2f9SIsaku Yamahata     rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_DOWNSTREAM,
8548ebf2f9SIsaku Yamahata                        p->port);
8648ebf2f9SIsaku Yamahata     if (rc < 0) {
8709b926d4SIsaku Yamahata         goto err_msi;
8848ebf2f9SIsaku Yamahata     }
890ead87c8SIsaku Yamahata     pcie_cap_flr_init(d);
9048ebf2f9SIsaku Yamahata     pcie_cap_deverr_init(d);
9148ebf2f9SIsaku Yamahata     pcie_cap_slot_init(d, s->slot);
9252ea63deSCao jin     pcie_cap_arifwd_init(d);
9352ea63deSCao jin 
9448ebf2f9SIsaku Yamahata     pcie_chassis_create(s->chassis);
9548ebf2f9SIsaku Yamahata     rc = pcie_chassis_add_slot(s);
9648ebf2f9SIsaku Yamahata     if (rc < 0) {
9709b926d4SIsaku Yamahata         goto err_pcie_cap;
9848ebf2f9SIsaku Yamahata     }
9952ea63deSCao jin 
100*f18c697bSDou Liyang     rc = pcie_aer_init(d, PCI_ERR_VER, XIO3130_AER_OFFSET,
101*f18c697bSDou Liyang                        PCI_ERR_SIZEOF, &err);
10209b926d4SIsaku Yamahata     if (rc < 0) {
10333848ceeSCao jin         error_report_err(err);
10409b926d4SIsaku Yamahata         goto err;
10509b926d4SIsaku Yamahata     }
10648ebf2f9SIsaku Yamahata 
10748ebf2f9SIsaku Yamahata     return 0;
10809b926d4SIsaku Yamahata 
10909b926d4SIsaku Yamahata err:
11009b926d4SIsaku Yamahata     pcie_chassis_del_slot(s);
11109b926d4SIsaku Yamahata err_pcie_cap:
11209b926d4SIsaku Yamahata     pcie_cap_exit(d);
11309b926d4SIsaku Yamahata err_msi:
11409b926d4SIsaku Yamahata     msi_uninit(d);
11509b926d4SIsaku Yamahata err_bridge:
116f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
11709b926d4SIsaku Yamahata     return rc;
11848ebf2f9SIsaku Yamahata }
11948ebf2f9SIsaku Yamahata 
120f90c2bcdSAlex Williamson static void xio3130_downstream_exitfn(PCIDevice *d)
12148ebf2f9SIsaku Yamahata {
122bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
12309b926d4SIsaku Yamahata 
12409b926d4SIsaku Yamahata     pcie_aer_exit(d);
12509b926d4SIsaku Yamahata     pcie_chassis_del_slot(s);
12648ebf2f9SIsaku Yamahata     pcie_cap_exit(d);
12709b926d4SIsaku Yamahata     msi_uninit(d);
128f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
12948ebf2f9SIsaku Yamahata }
13048ebf2f9SIsaku Yamahata 
13148ebf2f9SIsaku Yamahata PCIESlot *xio3130_downstream_init(PCIBus *bus, int devfn, bool multifunction,
13248ebf2f9SIsaku Yamahata                                   const char *bus_name, pci_map_irq_fn map_irq,
13348ebf2f9SIsaku Yamahata                                   uint8_t port, uint8_t chassis,
13448ebf2f9SIsaku Yamahata                                   uint16_t slot)
13548ebf2f9SIsaku Yamahata {
13648ebf2f9SIsaku Yamahata     PCIDevice *d;
13748ebf2f9SIsaku Yamahata     PCIBridge *br;
13848ebf2f9SIsaku Yamahata     DeviceState *qdev;
13948ebf2f9SIsaku Yamahata 
14048ebf2f9SIsaku Yamahata     d = pci_create_multifunction(bus, devfn, multifunction,
14148ebf2f9SIsaku Yamahata                                  "xio3130-downstream");
14248ebf2f9SIsaku Yamahata     if (!d) {
14348ebf2f9SIsaku Yamahata         return NULL;
14448ebf2f9SIsaku Yamahata     }
145f055e96bSAndreas Färber     br = PCI_BRIDGE(d);
14648ebf2f9SIsaku Yamahata 
147f055e96bSAndreas Färber     qdev = DEVICE(d);
14848ebf2f9SIsaku Yamahata     pci_bridge_map_irq(br, bus_name, map_irq);
14948ebf2f9SIsaku Yamahata     qdev_prop_set_uint8(qdev, "port", port);
15048ebf2f9SIsaku Yamahata     qdev_prop_set_uint8(qdev, "chassis", chassis);
15148ebf2f9SIsaku Yamahata     qdev_prop_set_uint16(qdev, "slot", slot);
15248ebf2f9SIsaku Yamahata     qdev_init_nofail(qdev);
15348ebf2f9SIsaku Yamahata 
154bcb75750SAndreas Färber     return PCIE_SLOT(d);
15548ebf2f9SIsaku Yamahata }
15648ebf2f9SIsaku Yamahata 
157f23b6bdcSMarcel Apfelbaum static Property xio3130_downstream_props[] = {
158f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present,
159f23b6bdcSMarcel Apfelbaum                     QEMU_PCIE_SLTCAP_PCP_BITNR, true),
160f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_END_OF_LIST()
161f23b6bdcSMarcel Apfelbaum };
162f23b6bdcSMarcel Apfelbaum 
16348ebf2f9SIsaku Yamahata static const VMStateDescription vmstate_xio3130_downstream = {
16448ebf2f9SIsaku Yamahata     .name = "xio3130-express-downstream-port",
16548ebf2f9SIsaku Yamahata     .version_id = 1,
16648ebf2f9SIsaku Yamahata     .minimum_version_id = 1,
1676bde6aaaSMichael S. Tsirkin     .post_load = pcie_cap_slot_post_load,
16848ebf2f9SIsaku Yamahata     .fields = (VMStateField[]) {
169bcb75750SAndreas Färber         VMSTATE_PCIE_DEVICE(parent_obj.parent_obj.parent_obj, PCIESlot),
170bcb75750SAndreas Färber         VMSTATE_STRUCT(parent_obj.parent_obj.parent_obj.exp.aer_log,
171bcb75750SAndreas Färber                        PCIESlot, 0, vmstate_pcie_aer_log, PCIEAERLog),
17248ebf2f9SIsaku Yamahata         VMSTATE_END_OF_LIST()
17348ebf2f9SIsaku Yamahata     }
17448ebf2f9SIsaku Yamahata };
17548ebf2f9SIsaku Yamahata 
17640021f08SAnthony Liguori static void xio3130_downstream_class_init(ObjectClass *klass, void *data)
17740021f08SAnthony Liguori {
17839bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
17940021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
18040021f08SAnthony Liguori 
18140021f08SAnthony Liguori     k->is_express = 1;
18240021f08SAnthony Liguori     k->is_bridge = 1;
18340021f08SAnthony Liguori     k->config_write = xio3130_downstream_write_config;
18440021f08SAnthony Liguori     k->init = xio3130_downstream_initfn;
18540021f08SAnthony Liguori     k->exit = xio3130_downstream_exitfn;
18640021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_TI;
18740021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_TI_XIO3130D;
18840021f08SAnthony Liguori     k->revision = XIO3130_REVISION;
189125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
19039bffca2SAnthony Liguori     dc->desc = "TI X3130 Downstream Port of PCI Express Switch";
19139bffca2SAnthony Liguori     dc->reset = xio3130_downstream_reset;
19239bffca2SAnthony Liguori     dc->vmsd = &vmstate_xio3130_downstream;
193f23b6bdcSMarcel Apfelbaum     dc->props = xio3130_downstream_props;
19448ebf2f9SIsaku Yamahata }
19540021f08SAnthony Liguori 
1968c43a6f0SAndreas Färber static const TypeInfo xio3130_downstream_info = {
19740021f08SAnthony Liguori     .name          = "xio3130-downstream",
198bcb75750SAndreas Färber     .parent        = TYPE_PCIE_SLOT,
19940021f08SAnthony Liguori     .class_init    = xio3130_downstream_class_init,
20048ebf2f9SIsaku Yamahata };
20148ebf2f9SIsaku Yamahata 
20283f7d43aSAndreas Färber static void xio3130_downstream_register_types(void)
20348ebf2f9SIsaku Yamahata {
20439bffca2SAnthony Liguori     type_register_static(&xio3130_downstream_info);
20548ebf2f9SIsaku Yamahata }
20648ebf2f9SIsaku Yamahata 
20783f7d43aSAndreas Färber type_init(xio3130_downstream_register_types)
208