blob: bb541ebb12964e1dc5d4a90319a5bf8a3fa8747a [file] [log] [blame]
Isaku Yamahata8135aee2010-10-20 17:18:53 +09001/*
2 * ioh3420.c
3 * Intel X58 north bridge IOH
4 * PCI Express root port device id 3420
5 *
6 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
7 * VA Linux Systems Japan K.K.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 */
22
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 "ioh3420.h"
Isaku Yamahata8135aee2010-10-20 17:18:53 +090027
28#define PCI_DEVICE_ID_IOH_EPORT 0x3420 /* D0:F0 express mode */
29#define PCI_DEVICE_ID_IOH_REV 0x2
30#define IOH_EP_SSVID_OFFSET 0x40
31#define IOH_EP_SSVID_SVID PCI_VENDOR_ID_INTEL
32#define IOH_EP_SSVID_SSID 0
33#define IOH_EP_MSI_OFFSET 0x60
34#define IOH_EP_MSI_SUPPORTED_FLAGS PCI_MSI_FLAGS_MASKBIT
35#define IOH_EP_MSI_NR_VECTOR 2
36#define IOH_EP_EXP_OFFSET 0x90
37#define IOH_EP_AER_OFFSET 0x100
38
Isaku Yamahata61620c22010-11-16 17:26:10 +090039/*
40 * If two MSI vector are allocated, Advanced Error Interrupt Message Number
41 * is 1. otherwise 0.
42 * 17.12.5.10 RPERRSTS, 32:27 bit Advanced Error Interrupt Message Number.
43 */
44static uint8_t ioh3420_aer_vector(const PCIDevice *d)
45{
46 switch (msi_nr_vectors_allocated(d)) {
47 case 1:
48 return 0;
49 case 2:
50 return 1;
51 case 4:
52 case 8:
53 case 16:
54 case 32:
55 default:
56 break;
57 }
58 abort();
59 return 0;
60}
61
62static void ioh3420_aer_vector_update(PCIDevice *d)
63{
64 pcie_aer_root_set_vector(d, ioh3420_aer_vector(d));
65}
66
Isaku Yamahata8135aee2010-10-20 17:18:53 +090067static void ioh3420_write_config(PCIDevice *d,
68 uint32_t address, uint32_t val, int len)
69{
Isaku Yamahata61620c22010-11-16 17:26:10 +090070 uint32_t root_cmd =
71 pci_get_long(d->config + d->exp.aer_cap + PCI_ERR_ROOT_COMMAND);
72
Isaku Yamahata8135aee2010-10-20 17:18:53 +090073 pci_bridge_write_config(d, address, val, len);
Isaku Yamahata61620c22010-11-16 17:26:10 +090074 ioh3420_aer_vector_update(d);
Michael S. Tsirkin6bde6aa2010-10-25 07:46:47 +020075 pcie_cap_slot_write_config(d, address, val, len);
Isaku Yamahata61620c22010-11-16 17:26:10 +090076 pcie_aer_write_config(d, address, val, len);
77 pcie_aer_root_write_config(d, address, val, len, root_cmd);
Isaku Yamahata8135aee2010-10-20 17:18:53 +090078}
79
80static void ioh3420_reset(DeviceState *qdev)
81{
Anthony Liguori40021f02011-12-04 12:22:06 -060082 PCIDevice *d = PCI_DEVICE(qdev);
Jan Kiszkacbd2d432012-05-15 20:09:56 -030083
Isaku Yamahata61620c22010-11-16 17:26:10 +090084 ioh3420_aer_vector_update(d);
Isaku Yamahata8135aee2010-10-20 17:18:53 +090085 pcie_cap_root_reset(d);
86 pcie_cap_deverr_reset(d);
87 pcie_cap_slot_reset(d);
Isaku Yamahata61620c22010-11-16 17:26:10 +090088 pcie_aer_root_reset(d);
Isaku Yamahata8135aee2010-10-20 17:18:53 +090089 pci_bridge_reset(qdev);
90 pci_bridge_disable_base_limit(d);
Isaku Yamahata8135aee2010-10-20 17:18:53 +090091}
92
93static int ioh3420_initfn(PCIDevice *d)
94{
95 PCIBridge* br = DO_UPCAST(PCIBridge, dev, d);
96 PCIEPort *p = DO_UPCAST(PCIEPort, br, br);
97 PCIESlot *s = DO_UPCAST(PCIESlot, port, p);
98 int rc;
99
Alex Williamsonafb661e2013-03-14 16:01:17 -0600100 rc = pci_bridge_initfn(d, TYPE_PCIE_BUS);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900101 if (rc < 0) {
102 return rc;
103 }
104
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900105 pcie_port_init_reg(d);
106
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900107 rc = pci_bridge_ssvid_init(d, IOH_EP_SSVID_OFFSET,
108 IOH_EP_SSVID_SVID, IOH_EP_SSVID_SSID);
109 if (rc < 0) {
Isaku Yamahata61620c22010-11-16 17:26:10 +0900110 goto err_bridge;
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900111 }
112 rc = msi_init(d, IOH_EP_MSI_OFFSET, IOH_EP_MSI_NR_VECTOR,
113 IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
114 IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT);
115 if (rc < 0) {
Isaku Yamahata61620c22010-11-16 17:26:10 +0900116 goto err_bridge;
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900117 }
118 rc = pcie_cap_init(d, IOH_EP_EXP_OFFSET, PCI_EXP_TYPE_ROOT_PORT, p->port);
119 if (rc < 0) {
Isaku Yamahata61620c22010-11-16 17:26:10 +0900120 goto err_msi;
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900121 }
122 pcie_cap_deverr_init(d);
123 pcie_cap_slot_init(d, s->slot);
124 pcie_chassis_create(s->chassis);
125 rc = pcie_chassis_add_slot(s);
126 if (rc < 0) {
Isaku Yamahata61620c22010-11-16 17:26:10 +0900127 goto err_pcie_cap;
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900128 }
129 pcie_cap_root_init(d);
Isaku Yamahata61620c22010-11-16 17:26:10 +0900130 rc = pcie_aer_init(d, IOH_EP_AER_OFFSET);
131 if (rc < 0) {
132 goto err;
133 }
134 pcie_aer_root_init(d);
135 ioh3420_aer_vector_update(d);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900136 return 0;
Isaku Yamahata61620c22010-11-16 17:26:10 +0900137
138err:
139 pcie_chassis_del_slot(s);
140err_pcie_cap:
141 pcie_cap_exit(d);
142err_msi:
143 msi_uninit(d);
144err_bridge:
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600145 pci_bridge_exitfn(d);
Isaku Yamahata61620c22010-11-16 17:26:10 +0900146 return rc;
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900147}
148
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600149static void ioh3420_exitfn(PCIDevice *d)
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900150{
Isaku Yamahata61620c22010-11-16 17:26:10 +0900151 PCIBridge* br = DO_UPCAST(PCIBridge, dev, d);
152 PCIEPort *p = DO_UPCAST(PCIEPort, br, br);
153 PCIESlot *s = DO_UPCAST(PCIESlot, port, p);
154
155 pcie_aer_exit(d);
156 pcie_chassis_del_slot(s);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900157 pcie_cap_exit(d);
Isaku Yamahata61620c22010-11-16 17:26:10 +0900158 msi_uninit(d);
Alex Williamsonf90c2bc2012-07-03 22:39:27 -0600159 pci_bridge_exitfn(d);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900160}
161
162PCIESlot *ioh3420_init(PCIBus *bus, int devfn, bool multifunction,
163 const char *bus_name, pci_map_irq_fn map_irq,
164 uint8_t port, uint8_t chassis, uint16_t slot)
165{
166 PCIDevice *d;
167 PCIBridge *br;
168 DeviceState *qdev;
169
170 d = pci_create_multifunction(bus, devfn, multifunction, "ioh3420");
171 if (!d) {
172 return NULL;
173 }
174 br = DO_UPCAST(PCIBridge, dev, d);
175
176 qdev = &br->dev.qdev;
177 pci_bridge_map_irq(br, bus_name, map_irq);
178 qdev_prop_set_uint8(qdev, "port", port);
179 qdev_prop_set_uint8(qdev, "chassis", chassis);
180 qdev_prop_set_uint16(qdev, "slot", slot);
181 qdev_init_nofail(qdev);
182
183 return DO_UPCAST(PCIESlot, port, DO_UPCAST(PCIEPort, br, br));
184}
185
186static const VMStateDescription vmstate_ioh3420 = {
187 .name = "ioh-3240-express-root-port",
188 .version_id = 1,
189 .minimum_version_id = 1,
190 .minimum_version_id_old = 1,
Michael S. Tsirkin6bde6aa2010-10-25 07:46:47 +0200191 .post_load = pcie_cap_slot_post_load,
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900192 .fields = (VMStateField[]) {
193 VMSTATE_PCIE_DEVICE(port.br.dev, PCIESlot),
Isaku Yamahata61620c22010-11-16 17:26:10 +0900194 VMSTATE_STRUCT(port.br.dev.exp.aer_log, PCIESlot, 0,
195 vmstate_pcie_aer_log, PCIEAERLog),
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900196 VMSTATE_END_OF_LIST()
197 }
198};
199
Anthony Liguori40021f02011-12-04 12:22:06 -0600200static Property ioh3420_properties[] = {
201 DEFINE_PROP_UINT8("port", PCIESlot, port.port, 0),
202 DEFINE_PROP_UINT8("chassis", PCIESlot, chassis, 0),
203 DEFINE_PROP_UINT16("slot", PCIESlot, slot, 0),
204 DEFINE_PROP_UINT16("aer_log_max", PCIESlot,
205 port.br.dev.exp.aer_log.log_max,
206 PCIE_AER_LOG_MAX_DEFAULT),
207 DEFINE_PROP_END_OF_LIST(),
208};
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900209
Anthony Liguori40021f02011-12-04 12:22:06 -0600210static void ioh3420_class_init(ObjectClass *klass, void *data)
211{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600212 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600213 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900214
Anthony Liguori40021f02011-12-04 12:22:06 -0600215 k->is_express = 1;
216 k->is_bridge = 1;
217 k->config_write = ioh3420_write_config;
218 k->init = ioh3420_initfn;
219 k->exit = ioh3420_exitfn;
220 k->vendor_id = PCI_VENDOR_ID_INTEL;
221 k->device_id = PCI_DEVICE_ID_IOH_EPORT;
222 k->revision = PCI_DEVICE_ID_IOH_REV;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600223 dc->desc = "Intel IOH device id 3420 PCIE Root Port";
224 dc->reset = ioh3420_reset;
225 dc->vmsd = &vmstate_ioh3420;
226 dc->props = ioh3420_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -0600227}
228
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100229static const TypeInfo ioh3420_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600230 .name = "ioh3420",
231 .parent = TYPE_PCI_DEVICE,
232 .instance_size = sizeof(PCIESlot),
233 .class_init = ioh3420_class_init,
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900234};
235
Andreas Färber83f7d432012-02-09 15:20:55 +0100236static void ioh3420_register_types(void)
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900237{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600238 type_register_static(&ioh3420_info);
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900239}
240
Andreas Färber83f7d432012-02-09 15:20:55 +0100241type_init(ioh3420_register_types)
Isaku Yamahata8135aee2010-10-20 17:18:53 +0900242
243/*
244 * Local variables:
245 * c-indent-level: 4
246 * c-basic-offset: 4
247 * tab-width: 8
248 * indent-tab-mode: nil
249 * End:
250 */