xref: /qemu/hw/pci-bridge/xio3130_upstream.c (revision 97d5408f99d0c45b8142de0c5ef92d44ad876898)
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 
22*97d5408fSPeter 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 
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     }
121f055e96bSAndreas Färber     br = PCI_BRIDGE(d);
122faf1e708SIsaku Yamahata 
123f055e96bSAndreas Färber     qdev = DEVICE(d);
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 
128bcb75750SAndreas Färber     return PCIE_PORT(d);
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     .fields = (VMStateField[]) {
136bcb75750SAndreas Färber         VMSTATE_PCIE_DEVICE(parent_obj.parent_obj, PCIEPort),
137bcb75750SAndreas Färber         VMSTATE_STRUCT(parent_obj.parent_obj.exp.aer_log, PCIEPort, 0,
138f055e96bSAndreas Färber                        vmstate_pcie_aer_log, PCIEAERLog),
139faf1e708SIsaku Yamahata         VMSTATE_END_OF_LIST()
140faf1e708SIsaku Yamahata     }
141faf1e708SIsaku Yamahata };
142faf1e708SIsaku Yamahata 
14340021f08SAnthony Liguori static void xio3130_upstream_class_init(ObjectClass *klass, void *data)
14440021f08SAnthony Liguori {
14539bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
14640021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
14740021f08SAnthony Liguori 
14840021f08SAnthony Liguori     k->is_express = 1;
14940021f08SAnthony Liguori     k->is_bridge = 1;
15040021f08SAnthony Liguori     k->config_write = xio3130_upstream_write_config;
15140021f08SAnthony Liguori     k->init = xio3130_upstream_initfn;
15240021f08SAnthony Liguori     k->exit = xio3130_upstream_exitfn;
15340021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_TI;
15440021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_TI_XIO3130U;
15540021f08SAnthony Liguori     k->revision = XIO3130_REVISION;
156125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
15739bffca2SAnthony Liguori     dc->desc = "TI X3130 Upstream Port of PCI Express Switch";
15839bffca2SAnthony Liguori     dc->reset = xio3130_upstream_reset;
15939bffca2SAnthony Liguori     dc->vmsd = &vmstate_xio3130_upstream;
160faf1e708SIsaku Yamahata }
16140021f08SAnthony Liguori 
1628c43a6f0SAndreas Färber static const TypeInfo xio3130_upstream_info = {
16340021f08SAnthony Liguori     .name          = "x3130-upstream",
164bcb75750SAndreas Färber     .parent        = TYPE_PCIE_PORT,
16540021f08SAnthony Liguori     .class_init    = xio3130_upstream_class_init,
166faf1e708SIsaku Yamahata };
167faf1e708SIsaku Yamahata 
16883f7d43aSAndreas Färber static void xio3130_upstream_register_types(void)
169faf1e708SIsaku Yamahata {
17039bffca2SAnthony Liguori     type_register_static(&xio3130_upstream_info);
171faf1e708SIsaku Yamahata }
172faf1e708SIsaku Yamahata 
17383f7d43aSAndreas Färber type_init(xio3130_upstream_register_types)
174faf1e708SIsaku Yamahata 
175faf1e708SIsaku Yamahata 
176faf1e708SIsaku Yamahata /*
177faf1e708SIsaku Yamahata  * Local variables:
178faf1e708SIsaku Yamahata  *  c-indent-level: 4
179faf1e708SIsaku Yamahata  *  c-basic-offset: 4
180faf1e708SIsaku Yamahata  *  tab-width: 8
181faf1e708SIsaku Yamahata  *  indent-tab-mode: nil
182faf1e708SIsaku Yamahata  * End:
183faf1e708SIsaku Yamahata  */
184