xref: /qemu/hw/pci-bridge/xio3130_downstream.c (revision 2841ab435bca9f102311e01bf157d5fa878935dc)
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"
26c6329a2dSPhilippe Mathieu-Daudé #include "hw/pci/pcie_port.h"
271108b2f8SCao jin #include "qapi/error.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
2948ebf2f9SIsaku Yamahata 
3048ebf2f9SIsaku Yamahata #define PCI_DEVICE_ID_TI_XIO3130D       0x8233  /* downstream port */
3148ebf2f9SIsaku Yamahata #define XIO3130_REVISION                0x1
3248ebf2f9SIsaku Yamahata #define XIO3130_MSI_OFFSET              0x70
3348ebf2f9SIsaku Yamahata #define XIO3130_MSI_SUPPORTED_FLAGS     PCI_MSI_FLAGS_64BIT
3448ebf2f9SIsaku Yamahata #define XIO3130_MSI_NR_VECTOR           1
3548ebf2f9SIsaku Yamahata #define XIO3130_SSVID_OFFSET            0x80
3648ebf2f9SIsaku Yamahata #define XIO3130_SSVID_SVID              0
3748ebf2f9SIsaku Yamahata #define XIO3130_SSVID_SSID              0
3848ebf2f9SIsaku Yamahata #define XIO3130_EXP_OFFSET              0x90
3948ebf2f9SIsaku Yamahata #define XIO3130_AER_OFFSET              0x100
4048ebf2f9SIsaku Yamahata 
4148ebf2f9SIsaku Yamahata static void xio3130_downstream_write_config(PCIDevice *d, uint32_t address,
4248ebf2f9SIsaku Yamahata                                          uint32_t val, int len)
4348ebf2f9SIsaku Yamahata {
44*2841ab43SMichael S. Tsirkin     uint16_t slt_ctl, slt_sta;
45*2841ab43SMichael S. Tsirkin 
46*2841ab43SMichael S. Tsirkin     pcie_cap_slot_get(d, &slt_sta, &slt_ctl);
4748ebf2f9SIsaku Yamahata     pci_bridge_write_config(d, address, val, len);
4848ebf2f9SIsaku Yamahata     pcie_cap_flr_write_config(d, address, val, len);
49*2841ab43SMichael S. Tsirkin     pcie_cap_slot_write_config(d, slt_ctl, slt_sta, address, val, len);
5009b926d4SIsaku Yamahata     pcie_aer_write_config(d, address, val, len);
5148ebf2f9SIsaku Yamahata }
5248ebf2f9SIsaku Yamahata 
5348ebf2f9SIsaku Yamahata static void xio3130_downstream_reset(DeviceState *qdev)
5448ebf2f9SIsaku Yamahata {
5540021f08SAnthony Liguori     PCIDevice *d = PCI_DEVICE(qdev);
56cbd2d434SJan Kiszka 
5748ebf2f9SIsaku Yamahata     pcie_cap_deverr_reset(d);
5848ebf2f9SIsaku Yamahata     pcie_cap_slot_reset(d);
59821be9dbSKnut Omang     pcie_cap_arifwd_reset(d);
6048ebf2f9SIsaku Yamahata     pci_bridge_reset(qdev);
6148ebf2f9SIsaku Yamahata }
6248ebf2f9SIsaku Yamahata 
63f8cd1b02SMao Zhongyi static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
6448ebf2f9SIsaku Yamahata {
65bcb75750SAndreas Färber     PCIEPort *p = PCIE_PORT(d);
66bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
6748ebf2f9SIsaku Yamahata     int rc;
6848ebf2f9SIsaku Yamahata 
699cfaa007SCao jin     pci_bridge_initfn(d, TYPE_PCIE_BUS);
7048ebf2f9SIsaku Yamahata     pcie_port_init_reg(d);
7148ebf2f9SIsaku Yamahata 
7248ebf2f9SIsaku Yamahata     rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR,
7348ebf2f9SIsaku Yamahata                   XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
74f8cd1b02SMao Zhongyi                   XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT,
75f8cd1b02SMao Zhongyi                   errp);
7648ebf2f9SIsaku Yamahata     if (rc < 0) {
771108b2f8SCao jin         assert(rc == -ENOTSUP);
7809b926d4SIsaku Yamahata         goto err_bridge;
7948ebf2f9SIsaku Yamahata     }
8052ea63deSCao jin 
8148ebf2f9SIsaku Yamahata     rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET,
82f8cd1b02SMao Zhongyi                                XIO3130_SSVID_SVID, XIO3130_SSVID_SSID,
83f8cd1b02SMao Zhongyi                                errp);
8448ebf2f9SIsaku Yamahata     if (rc < 0) {
8509b926d4SIsaku Yamahata         goto err_bridge;
8648ebf2f9SIsaku Yamahata     }
8752ea63deSCao jin 
8848ebf2f9SIsaku Yamahata     rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_DOWNSTREAM,
89f8cd1b02SMao Zhongyi                        p->port, errp);
9048ebf2f9SIsaku Yamahata     if (rc < 0) {
9109b926d4SIsaku Yamahata         goto err_msi;
9248ebf2f9SIsaku Yamahata     }
930ead87c8SIsaku Yamahata     pcie_cap_flr_init(d);
9448ebf2f9SIsaku Yamahata     pcie_cap_deverr_init(d);
9548ebf2f9SIsaku Yamahata     pcie_cap_slot_init(d, s->slot);
9652ea63deSCao jin     pcie_cap_arifwd_init(d);
9752ea63deSCao jin 
9848ebf2f9SIsaku Yamahata     pcie_chassis_create(s->chassis);
9948ebf2f9SIsaku Yamahata     rc = pcie_chassis_add_slot(s);
10048ebf2f9SIsaku Yamahata     if (rc < 0) {
1018b3d2634SEduardo Habkost         error_setg(errp, "Can't add chassis slot, error %d", rc);
10209b926d4SIsaku Yamahata         goto err_pcie_cap;
10348ebf2f9SIsaku Yamahata     }
10452ea63deSCao jin 
105f18c697bSDou Liyang     rc = pcie_aer_init(d, PCI_ERR_VER, XIO3130_AER_OFFSET,
106f8cd1b02SMao Zhongyi                        PCI_ERR_SIZEOF, errp);
10709b926d4SIsaku Yamahata     if (rc < 0) {
10809b926d4SIsaku Yamahata         goto err;
10909b926d4SIsaku Yamahata     }
11048ebf2f9SIsaku Yamahata 
111f8cd1b02SMao Zhongyi     return;
11209b926d4SIsaku Yamahata 
11309b926d4SIsaku Yamahata err:
11409b926d4SIsaku Yamahata     pcie_chassis_del_slot(s);
11509b926d4SIsaku Yamahata err_pcie_cap:
11609b926d4SIsaku Yamahata     pcie_cap_exit(d);
11709b926d4SIsaku Yamahata err_msi:
11809b926d4SIsaku Yamahata     msi_uninit(d);
11909b926d4SIsaku Yamahata err_bridge:
120f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
12148ebf2f9SIsaku Yamahata }
12248ebf2f9SIsaku Yamahata 
123f90c2bcdSAlex Williamson static void xio3130_downstream_exitfn(PCIDevice *d)
12448ebf2f9SIsaku Yamahata {
125bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
12609b926d4SIsaku Yamahata 
12709b926d4SIsaku Yamahata     pcie_aer_exit(d);
12809b926d4SIsaku Yamahata     pcie_chassis_del_slot(s);
12948ebf2f9SIsaku Yamahata     pcie_cap_exit(d);
13009b926d4SIsaku Yamahata     msi_uninit(d);
131f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
13248ebf2f9SIsaku Yamahata }
13348ebf2f9SIsaku Yamahata 
134f23b6bdcSMarcel Apfelbaum static Property xio3130_downstream_props[] = {
135f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present,
136f23b6bdcSMarcel Apfelbaum                     QEMU_PCIE_SLTCAP_PCP_BITNR, true),
137f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_END_OF_LIST()
138f23b6bdcSMarcel Apfelbaum };
139f23b6bdcSMarcel Apfelbaum 
14048ebf2f9SIsaku Yamahata static const VMStateDescription vmstate_xio3130_downstream = {
14148ebf2f9SIsaku Yamahata     .name = "xio3130-express-downstream-port",
1429d6b9db1SPeter Xu     .priority = MIG_PRI_PCI_BUS,
14348ebf2f9SIsaku Yamahata     .version_id = 1,
14448ebf2f9SIsaku Yamahata     .minimum_version_id = 1,
1456bde6aaaSMichael S. Tsirkin     .post_load = pcie_cap_slot_post_load,
14648ebf2f9SIsaku Yamahata     .fields = (VMStateField[]) {
14720daa90aSDr. David Alan Gilbert         VMSTATE_PCI_DEVICE(parent_obj.parent_obj.parent_obj, PCIESlot),
148bcb75750SAndreas Färber         VMSTATE_STRUCT(parent_obj.parent_obj.parent_obj.exp.aer_log,
149bcb75750SAndreas Färber                        PCIESlot, 0, vmstate_pcie_aer_log, PCIEAERLog),
15048ebf2f9SIsaku Yamahata         VMSTATE_END_OF_LIST()
15148ebf2f9SIsaku Yamahata     }
15248ebf2f9SIsaku Yamahata };
15348ebf2f9SIsaku Yamahata 
15440021f08SAnthony Liguori static void xio3130_downstream_class_init(ObjectClass *klass, void *data)
15540021f08SAnthony Liguori {
15639bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
15740021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
15840021f08SAnthony Liguori 
15991f4c995SDavid Gibson     k->is_bridge = true;
16040021f08SAnthony Liguori     k->config_write = xio3130_downstream_write_config;
161f8cd1b02SMao Zhongyi     k->realize = xio3130_downstream_realize;
16240021f08SAnthony Liguori     k->exit = xio3130_downstream_exitfn;
16340021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_TI;
16440021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_TI_XIO3130D;
16540021f08SAnthony Liguori     k->revision = XIO3130_REVISION;
166125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
16739bffca2SAnthony Liguori     dc->desc = "TI X3130 Downstream Port of PCI Express Switch";
16839bffca2SAnthony Liguori     dc->reset = xio3130_downstream_reset;
16939bffca2SAnthony Liguori     dc->vmsd = &vmstate_xio3130_downstream;
170f23b6bdcSMarcel Apfelbaum     dc->props = xio3130_downstream_props;
17148ebf2f9SIsaku Yamahata }
17240021f08SAnthony Liguori 
1738c43a6f0SAndreas Färber static const TypeInfo xio3130_downstream_info = {
17440021f08SAnthony Liguori     .name          = "xio3130-downstream",
175bcb75750SAndreas Färber     .parent        = TYPE_PCIE_SLOT,
17640021f08SAnthony Liguori     .class_init    = xio3130_downstream_class_init,
17771d78767SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
17871d78767SEduardo Habkost         { INTERFACE_PCIE_DEVICE },
17971d78767SEduardo Habkost         { }
18071d78767SEduardo Habkost     },
18148ebf2f9SIsaku Yamahata };
18248ebf2f9SIsaku Yamahata 
18383f7d43aSAndreas Färber static void xio3130_downstream_register_types(void)
18448ebf2f9SIsaku Yamahata {
18539bffca2SAnthony Liguori     type_register_static(&xio3130_downstream_info);
18648ebf2f9SIsaku Yamahata }
18748ebf2f9SIsaku Yamahata 
18883f7d43aSAndreas Färber type_init(xio3130_downstream_register_types)
189