blob: 473e2dd95044bff9d616d37995d895adb498c300 [file] [log] [blame]
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +09001/*
2 * x3130_downstream.c
3 * TI X3130 pci express downstream 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"
Philippe Mathieu-Daudéc6329a22018-10-10 23:53:53 +020026#include "hw/pci/pcie_port.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020027#include "hw/qdev-properties.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020028#include "migration/vmstate.h"
Cao jin1108b2f2016-06-20 14:13:39 +080029#include "qapi/error.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020030#include "qemu/module.h"
Igor Mammedovc41481a2022-03-01 10:11:58 -050031#include "hw/pci-bridge/xio3130_downstream.h"
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090032
33#define PCI_DEVICE_ID_TI_XIO3130D 0x8233 /* downstream port */
34#define XIO3130_REVISION 0x1
35#define XIO3130_MSI_OFFSET 0x70
36#define XIO3130_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_64BIT
37#define XIO3130_MSI_NR_VECTOR 1
38#define XIO3130_SSVID_OFFSET 0x80
39#define XIO3130_SSVID_SVID 0
40#define XIO3130_SSVID_SSID 0
41#define XIO3130_EXP_OFFSET 0x90
42#define XIO3130_AER_OFFSET 0x100
43
44static void xio3130_downstream_write_config(PCIDevice *d, uint32_t address,
45 uint32_t val, int len)
46{
Michael S. Tsirkin2841ab42019-06-21 00:12:22 -040047 uint16_t slt_ctl, slt_sta;
48
Michael S. Tsirkin8e2e95e2019-07-11 15:24:18 -040049 pcie_cap_slot_get(d, &slt_ctl, &slt_sta);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090050 pci_bridge_write_config(d, address, val, len);
51 pcie_cap_flr_write_config(d, address, val, len);
Michael S. Tsirkin2841ab42019-06-21 00:12:22 -040052 pcie_cap_slot_write_config(d, slt_ctl, slt_sta, address, val, len);
Isaku Yamahata09b926d2010-11-16 17:26:12 +090053 pcie_aer_write_config(d, address, val, len);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090054}
55
56static void xio3130_downstream_reset(DeviceState *qdev)
57{
Anthony Liguori40021f02011-12-04 12:22:06 -060058 PCIDevice *d = PCI_DEVICE(qdev);
Jan Kiszkacbd2d432012-05-15 20:09:56 -030059
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090060 pcie_cap_deverr_reset(d);
61 pcie_cap_slot_reset(d);
Knut Omang821be9d2014-08-24 15:32:18 +020062 pcie_cap_arifwd_reset(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090063 pci_bridge_reset(qdev);
64}
65
Mao Zhongyif8cd1b02017-06-27 14:16:52 +080066static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090067{
Andreas Färberbcb75752013-07-12 19:56:00 +020068 PCIEPort *p = PCIE_PORT(d);
69 PCIESlot *s = PCIE_SLOT(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090070 int rc;
71
Cao jin9cfaa002016-01-15 10:23:32 +080072 pci_bridge_initfn(d, TYPE_PCIE_BUS);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090073 pcie_port_init_reg(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090074
75 rc = msi_init(d, XIO3130_MSI_OFFSET, XIO3130_MSI_NR_VECTOR,
76 XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
Mao Zhongyif8cd1b02017-06-27 14:16:52 +080077 XIO3130_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT,
78 errp);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090079 if (rc < 0) {
Cao jin1108b2f2016-06-20 14:13:39 +080080 assert(rc == -ENOTSUP);
Isaku Yamahata09b926d2010-11-16 17:26:12 +090081 goto err_bridge;
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090082 }
Cao jin52ea63d2016-06-10 17:54:23 +080083
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090084 rc = pci_bridge_ssvid_init(d, XIO3130_SSVID_OFFSET,
Mao Zhongyif8cd1b02017-06-27 14:16:52 +080085 XIO3130_SSVID_SVID, XIO3130_SSVID_SSID,
86 errp);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090087 if (rc < 0) {
Jonathan Camerona1058132022-02-18 10:23:03 +000088 goto err_msi;
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090089 }
Cao jin52ea63d2016-06-10 17:54:23 +080090
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090091 rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_DOWNSTREAM,
Mao Zhongyif8cd1b02017-06-27 14:16:52 +080092 p->port, errp);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090093 if (rc < 0) {
Isaku Yamahata09b926d2010-11-16 17:26:12 +090094 goto err_msi;
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090095 }
Isaku Yamahata0ead87c2010-12-22 15:14:35 +090096 pcie_cap_flr_init(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +090097 pcie_cap_deverr_init(d);
Julia Suvorova530a0962020-02-26 18:46:07 +010098 pcie_cap_slot_init(d, s);
Cao jin52ea63d2016-06-10 17:54:23 +080099 pcie_cap_arifwd_init(d);
100
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900101 pcie_chassis_create(s->chassis);
102 rc = pcie_chassis_add_slot(s);
103 if (rc < 0) {
Eduardo Habkost8b3d2632017-08-25 16:54:06 -0300104 error_setg(errp, "Can't add chassis slot, error %d", rc);
Isaku Yamahata09b926d2010-11-16 17:26:12 +0900105 goto err_pcie_cap;
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900106 }
Cao jin52ea63d2016-06-10 17:54:23 +0800107
Dou Liyangf18c6972016-12-21 16:21:31 +0800108 rc = pcie_aer_init(d, PCI_ERR_VER, XIO3130_AER_OFFSET,
Mao Zhongyif8cd1b02017-06-27 14:16:52 +0800109 PCI_ERR_SIZEOF, errp);
Isaku Yamahata09b926d2010-11-16 17:26:12 +0900110 if (rc < 0) {
111 goto err;
112 }
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900113
Mao Zhongyif8cd1b02017-06-27 14:16:52 +0800114 return;
Isaku Yamahata09b926d2010-11-16 17:26:12 +0900115
116err:
117 pcie_chassis_del_slot(s);
118err_pcie_cap:
119 pcie_cap_exit(d);
120err_msi:
121 msi_uninit(d);
122err_bridge:
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600123 pci_bridge_exitfn(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900124}
125
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600126static void xio3130_downstream_exitfn(PCIDevice *d)
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900127{
Andreas Färberbcb75752013-07-12 19:56:00 +0200128 PCIESlot *s = PCIE_SLOT(d);
Isaku Yamahata09b926d2010-11-16 17:26:12 +0900129
130 pcie_aer_exit(d);
131 pcie_chassis_del_slot(s);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900132 pcie_cap_exit(d);
Isaku Yamahata09b926d2010-11-16 17:26:12 +0900133 msi_uninit(d);
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600134 pci_bridge_exitfn(d);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900135}
136
Marcel Apfelbaumf23b6bd2014-06-23 17:32:48 +0300137static Property xio3130_downstream_props[] = {
138 DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present,
139 QEMU_PCIE_SLTCAP_PCP_BITNR, true),
140 DEFINE_PROP_END_OF_LIST()
141};
142
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900143static const VMStateDescription vmstate_xio3130_downstream = {
144 .name = "xio3130-express-downstream-port",
Peter Xu9d6b9db2018-02-06 15:39:33 +0800145 .priority = MIG_PRI_PCI_BUS,
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900146 .version_id = 1,
147 .minimum_version_id = 1,
Michael S. Tsirkin6bde6aa2010-10-25 07:46:47 +0200148 .post_load = pcie_cap_slot_post_load,
Richard Hendersonf026c572023-12-21 14:16:26 +1100149 .fields = (const VMStateField[]) {
Dr. David Alan Gilbert20daa902016-12-14 19:58:29 +0000150 VMSTATE_PCI_DEVICE(parent_obj.parent_obj.parent_obj, PCIESlot),
Andreas Färberbcb75752013-07-12 19:56:00 +0200151 VMSTATE_STRUCT(parent_obj.parent_obj.parent_obj.exp.aer_log,
152 PCIESlot, 0, vmstate_pcie_aer_log, PCIEAERLog),
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900153 VMSTATE_END_OF_LIST()
154 }
155};
156
Anthony Liguori40021f02011-12-04 12:22:06 -0600157static void xio3130_downstream_class_init(ObjectClass *klass, void *data)
158{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600159 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600160 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900161
Anthony Liguori40021f02011-12-04 12:22:06 -0600162 k->config_write = xio3130_downstream_write_config;
Mao Zhongyif8cd1b02017-06-27 14:16:52 +0800163 k->realize = xio3130_downstream_realize;
Anthony Liguori40021f02011-12-04 12:22:06 -0600164 k->exit = xio3130_downstream_exitfn;
165 k->vendor_id = PCI_VENDOR_ID_TI;
166 k->device_id = PCI_DEVICE_ID_TI_XIO3130D;
167 k->revision = XIO3130_REVISION;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300168 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600169 dc->desc = "TI X3130 Downstream Port of PCI Express Switch";
Peter Maydelle3d08142024-09-13 15:31:44 +0100170 device_class_set_legacy_reset(dc, xio3130_downstream_reset);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600171 dc->vmsd = &vmstate_xio3130_downstream;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400172 device_class_set_props(dc, xio3130_downstream_props);
Anthony Liguori40021f02011-12-04 12:22:06 -0600173}
174
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100175static const TypeInfo xio3130_downstream_info = {
Igor Mammedovc41481a2022-03-01 10:11:58 -0500176 .name = TYPE_XIO3130_DOWNSTREAM,
Andreas Färberbcb75752013-07-12 19:56:00 +0200177 .parent = TYPE_PCIE_SLOT,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600178 .class_init = xio3130_downstream_class_init,
Eduardo Habkost71d78762017-09-27 16:56:33 -0300179 .interfaces = (InterfaceInfo[]) {
180 { INTERFACE_PCIE_DEVICE },
181 { }
182 },
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900183};
184
Andreas Färber83f7d432012-02-09 15:20:55 +0100185static void xio3130_downstream_register_types(void)
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900186{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600187 type_register_static(&xio3130_downstream_info);
Isaku Yamahata48ebf2f2010-10-20 17:18:55 +0900188}
189
Andreas Färber83f7d432012-02-09 15:20:55 +0100190type_init(xio3130_downstream_register_types)