blob: 4ad0440aa164b308aeec378c9f8dab17a83270e0 [file] [log] [blame]
Isaku Yamahatafaf1e702010-10-20 17:18:54 +09001/*
2 * xio3130_upstream.c
3 * TI X3130 pci express upstream port switch
4 *
5 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
6 * VA Linux Systems Japan K.K.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
Peter Maydell97d54082016-01-26 18:17:15 +000022#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010023#include "hw/pci/pci_ids.h"
24#include "hw/pci/msi.h"
25#include "hw/pci/pcie.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010026#include "xio3130_upstream.h"
Cao jin1108b2f2016-06-20 14:13:39 +080027#include "qapi/error.h"
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090028
29#define PCI_DEVICE_ID_TI_XIO3130U 0x8232 /* upstream port */
30#define XIO3130_REVISION 0x2
31#define XIO3130_MSI_OFFSET 0x70
32#define XIO3130_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_64BIT
33#define XIO3130_MSI_NR_VECTOR 1
34#define XIO3130_SSVID_OFFSET 0x80
35#define XIO3130_SSVID_SVID 0
36#define XIO3130_SSVID_SSID 0
37#define XIO3130_EXP_OFFSET 0x90
38#define XIO3130_AER_OFFSET 0x100
39
40static void xio3130_upstream_write_config(PCIDevice *d, uint32_t address,
41 uint32_t val, int len)
42{
43 pci_bridge_write_config(d, address, val, len);
44 pcie_cap_flr_write_config(d, address, val, len);
Isaku Yamahataa158f922010-11-16 17:26:11 +090045 pcie_aer_write_config(d, address, val, len);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090046}
47
48static void xio3130_upstream_reset(DeviceState *qdev)
49{
Anthony Liguori40021f02011-12-04 12:22:06 -060050 PCIDevice *d = PCI_DEVICE(qdev);
Jan Kiszkacbd2d432012-05-15 20:09:56 -030051
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090052 pci_bridge_reset(qdev);
53 pcie_cap_deverr_reset(d);
54}
55
56static int xio3130_upstream_initfn(PCIDevice *d)
57{
Andreas Färberbcb75752013-07-12 19:56:00 +020058 PCIEPort *p = PCIE_PORT(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090059 int rc;
Cao jin1108b2f2016-06-20 14:13:39 +080060 Error *err = NULL;
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090061
Cao jin9cfaa002016-01-15 10:23:32 +080062 pci_bridge_initfn(d, TYPE_PCIE_BUS);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090063 pcie_port_init_reg(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090064
65 rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR,
66 XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
Cao jin1108b2f2016-06-20 14:13:39 +080067 XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT, &err);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090068 if (rc < 0) {
Cao jin1108b2f2016-06-20 14:13:39 +080069 assert(rc == -ENOTSUP);
70 error_report_err(err);
Isaku Yamahataa158f922010-11-16 17:26:11 +090071 goto err_bridge;
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090072 }
Cao jin52ea63d2016-06-10 17:54:23 +080073
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090074 rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET,
75 XIO3130_SSVID_SVID, XIO3130_SSVID_SSID);
76 if (rc < 0) {
Isaku Yamahataa158f922010-11-16 17:26:11 +090077 goto err_bridge;
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090078 }
Cao jin52ea63d2016-06-10 17:54:23 +080079
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090080 rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_UPSTREAM,
81 p->port);
82 if (rc < 0) {
Isaku Yamahataa158f922010-11-16 17:26:11 +090083 goto err_msi;
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090084 }
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090085 pcie_cap_flr_init(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090086 pcie_cap_deverr_init(d);
Cao jin52ea63d2016-06-10 17:54:23 +080087
Chen Fan8d86ada2016-02-19 09:42:28 -070088 rc = pcie_aer_init(d, XIO3130_AER_OFFSET, PCI_ERR_SIZEOF);
Isaku Yamahataa158f922010-11-16 17:26:11 +090089 if (rc < 0) {
90 goto err;
91 }
Isaku Yamahatafaf1e702010-10-20 17:18:54 +090092
93 return 0;
Isaku Yamahataa158f922010-11-16 17:26:11 +090094
95err:
96 pcie_cap_exit(d);
97err_msi:
98 msi_uninit(d);
99err_bridge:
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600100 pci_bridge_exitfn(d);
Isaku Yamahataa158f922010-11-16 17:26:11 +0900101 return rc;
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900102}
103
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600104static void xio3130_upstream_exitfn(PCIDevice *d)
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900105{
Isaku Yamahataa158f922010-11-16 17:26:11 +0900106 pcie_aer_exit(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900107 pcie_cap_exit(d);
Isaku Yamahataa158f922010-11-16 17:26:11 +0900108 msi_uninit(d);
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600109 pci_bridge_exitfn(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900110}
111
112PCIEPort *xio3130_upstream_init(PCIBus *bus, int devfn, bool multifunction,
113 const char *bus_name, pci_map_irq_fn map_irq,
114 uint8_t port)
115{
116 PCIDevice *d;
117 PCIBridge *br;
118 DeviceState *qdev;
119
120 d = pci_create_multifunction(bus, devfn, multifunction, "x3130-upstream");
121 if (!d) {
122 return NULL;
123 }
Andreas Färberf055e962013-07-11 17:13:43 +0200124 br = PCI_BRIDGE(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900125
Andreas Färberf055e962013-07-11 17:13:43 +0200126 qdev = DEVICE(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900127 pci_bridge_map_irq(br, bus_name, map_irq);
128 qdev_prop_set_uint8(qdev, "port", port);
129 qdev_init_nofail(qdev);
130
Andreas Färberbcb75752013-07-12 19:56:00 +0200131 return PCIE_PORT(d);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900132}
133
134static const VMStateDescription vmstate_xio3130_upstream = {
135 .name = "xio3130-express-upstream-port",
136 .version_id = 1,
137 .minimum_version_id = 1,
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900138 .fields = (VMStateField[]) {
Andreas Färberbcb75752013-07-12 19:56:00 +0200139 VMSTATE_PCIE_DEVICE(parent_obj.parent_obj, PCIEPort),
140 VMSTATE_STRUCT(parent_obj.parent_obj.exp.aer_log, PCIEPort, 0,
Andreas Färberf055e962013-07-11 17:13:43 +0200141 vmstate_pcie_aer_log, PCIEAERLog),
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900142 VMSTATE_END_OF_LIST()
143 }
144};
145
Anthony Liguori40021f02011-12-04 12:22:06 -0600146static void xio3130_upstream_class_init(ObjectClass *klass, void *data)
147{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600148 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600149 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900150
Anthony Liguori40021f02011-12-04 12:22:06 -0600151 k->is_express = 1;
152 k->is_bridge = 1;
153 k->config_write = xio3130_upstream_write_config;
154 k->init = xio3130_upstream_initfn;
155 k->exit = xio3130_upstream_exitfn;
156 k->vendor_id = PCI_VENDOR_ID_TI;
157 k->device_id = PCI_DEVICE_ID_TI_XIO3130U;
158 k->revision = XIO3130_REVISION;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300159 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600160 dc->desc = "TI X3130 Upstream Port of PCI Express Switch";
161 dc->reset = xio3130_upstream_reset;
162 dc->vmsd = &vmstate_xio3130_upstream;
Anthony Liguori40021f02011-12-04 12:22:06 -0600163}
164
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100165static const TypeInfo xio3130_upstream_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600166 .name = "x3130-upstream",
Andreas Färberbcb75752013-07-12 19:56:00 +0200167 .parent = TYPE_PCIE_PORT,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600168 .class_init = xio3130_upstream_class_init,
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900169};
170
Andreas Färber83f7d432012-02-09 15:20:55 +0100171static void xio3130_upstream_register_types(void)
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900172{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600173 type_register_static(&xio3130_upstream_info);
Isaku Yamahatafaf1e702010-10-20 17:18:54 +0900174}
175
Andreas Färber83f7d432012-02-09 15:20:55 +0100176type_init(xio3130_upstream_register_types)