xref: /qemu/hw/pci-bridge/ioh3420.c (revision f18c697b55d1374af67b84c581abaece8ab2aca3)
18135aeedSIsaku Yamahata /*
28135aeedSIsaku Yamahata  * ioh3420.c
38135aeedSIsaku Yamahata  * Intel X58 north bridge IOH
48135aeedSIsaku Yamahata  * PCI Express root port device id 3420
58135aeedSIsaku Yamahata  *
68135aeedSIsaku Yamahata  * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
78135aeedSIsaku Yamahata  *                    VA Linux Systems Japan K.K.
88135aeedSIsaku Yamahata  *
98135aeedSIsaku Yamahata  * This program is free software; you can redistribute it and/or modify
108135aeedSIsaku Yamahata  * it under the terms of the GNU General Public License as published by
118135aeedSIsaku Yamahata  * the Free Software Foundation; either version 2 of the License, or
128135aeedSIsaku Yamahata  * (at your option) any later version.
138135aeedSIsaku Yamahata  *
148135aeedSIsaku Yamahata  * This program is distributed in the hope that it will be useful,
158135aeedSIsaku Yamahata  * but WITHOUT ANY WARRANTY; without even the implied warranty of
168135aeedSIsaku Yamahata  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
178135aeedSIsaku Yamahata  * GNU General Public License for more details.
188135aeedSIsaku Yamahata  *
198135aeedSIsaku Yamahata  * You should have received a copy of the GNU General Public License along
208135aeedSIsaku Yamahata  * with this program; if not, see <http://www.gnu.org/licenses/>.
218135aeedSIsaku Yamahata  */
228135aeedSIsaku Yamahata 
2397d5408fSPeter Maydell #include "qemu/osdep.h"
2483c9f4caSPaolo Bonzini #include "hw/pci/pci_ids.h"
2583c9f4caSPaolo Bonzini #include "hw/pci/msi.h"
2683c9f4caSPaolo Bonzini #include "hw/pci/pcie.h"
2747b43a1fSPaolo Bonzini #include "ioh3420.h"
281108b2f8SCao jin #include "qapi/error.h"
298135aeedSIsaku Yamahata 
308135aeedSIsaku Yamahata #define PCI_DEVICE_ID_IOH_EPORT         0x3420  /* D0:F0 express mode */
318135aeedSIsaku Yamahata #define PCI_DEVICE_ID_IOH_REV           0x2
328135aeedSIsaku Yamahata #define IOH_EP_SSVID_OFFSET             0x40
338135aeedSIsaku Yamahata #define IOH_EP_SSVID_SVID               PCI_VENDOR_ID_INTEL
348135aeedSIsaku Yamahata #define IOH_EP_SSVID_SSID               0
358135aeedSIsaku Yamahata #define IOH_EP_MSI_OFFSET               0x60
368135aeedSIsaku Yamahata #define IOH_EP_MSI_SUPPORTED_FLAGS      PCI_MSI_FLAGS_MASKBIT
378135aeedSIsaku Yamahata #define IOH_EP_MSI_NR_VECTOR            2
388135aeedSIsaku Yamahata #define IOH_EP_EXP_OFFSET               0x90
398135aeedSIsaku Yamahata #define IOH_EP_AER_OFFSET               0x100
408135aeedSIsaku Yamahata 
4161620c2fSIsaku Yamahata /*
4261620c2fSIsaku Yamahata  * If two MSI vector are allocated, Advanced Error Interrupt Message Number
4361620c2fSIsaku Yamahata  * is 1. otherwise 0.
4461620c2fSIsaku Yamahata  * 17.12.5.10 RPERRSTS,  32:27 bit Advanced Error Interrupt Message Number.
4561620c2fSIsaku Yamahata  */
4661620c2fSIsaku Yamahata static uint8_t ioh3420_aer_vector(const PCIDevice *d)
4761620c2fSIsaku Yamahata {
4861620c2fSIsaku Yamahata     switch (msi_nr_vectors_allocated(d)) {
4961620c2fSIsaku Yamahata     case 1:
5061620c2fSIsaku Yamahata         return 0;
5161620c2fSIsaku Yamahata     case 2:
5261620c2fSIsaku Yamahata         return 1;
5361620c2fSIsaku Yamahata     case 4:
5461620c2fSIsaku Yamahata     case 8:
5561620c2fSIsaku Yamahata     case 16:
5661620c2fSIsaku Yamahata     case 32:
5761620c2fSIsaku Yamahata     default:
5861620c2fSIsaku Yamahata         break;
5961620c2fSIsaku Yamahata     }
6061620c2fSIsaku Yamahata     abort();
6161620c2fSIsaku Yamahata     return 0;
6261620c2fSIsaku Yamahata }
6361620c2fSIsaku Yamahata 
6461620c2fSIsaku Yamahata static void ioh3420_aer_vector_update(PCIDevice *d)
6561620c2fSIsaku Yamahata {
6661620c2fSIsaku Yamahata     pcie_aer_root_set_vector(d, ioh3420_aer_vector(d));
6761620c2fSIsaku Yamahata }
6861620c2fSIsaku Yamahata 
698135aeedSIsaku Yamahata static void ioh3420_write_config(PCIDevice *d,
708135aeedSIsaku Yamahata                                    uint32_t address, uint32_t val, int len)
718135aeedSIsaku Yamahata {
7261620c2fSIsaku Yamahata     uint32_t root_cmd =
7361620c2fSIsaku Yamahata         pci_get_long(d->config + d->exp.aer_cap + PCI_ERR_ROOT_COMMAND);
7461620c2fSIsaku Yamahata 
758135aeedSIsaku Yamahata     pci_bridge_write_config(d, address, val, len);
7661620c2fSIsaku Yamahata     ioh3420_aer_vector_update(d);
776bde6aaaSMichael S. Tsirkin     pcie_cap_slot_write_config(d, address, val, len);
7861620c2fSIsaku Yamahata     pcie_aer_write_config(d, address, val, len);
7961620c2fSIsaku Yamahata     pcie_aer_root_write_config(d, address, val, len, root_cmd);
808135aeedSIsaku Yamahata }
818135aeedSIsaku Yamahata 
828135aeedSIsaku Yamahata static void ioh3420_reset(DeviceState *qdev)
838135aeedSIsaku Yamahata {
8440021f08SAnthony Liguori     PCIDevice *d = PCI_DEVICE(qdev);
85cbd2d434SJan Kiszka 
8661620c2fSIsaku Yamahata     ioh3420_aer_vector_update(d);
878135aeedSIsaku Yamahata     pcie_cap_root_reset(d);
888135aeedSIsaku Yamahata     pcie_cap_deverr_reset(d);
898135aeedSIsaku Yamahata     pcie_cap_slot_reset(d);
90a74b8702SKnut Omang     pcie_cap_arifwd_reset(d);
9161620c2fSIsaku Yamahata     pcie_aer_root_reset(d);
928135aeedSIsaku Yamahata     pci_bridge_reset(qdev);
938135aeedSIsaku Yamahata     pci_bridge_disable_base_limit(d);
948135aeedSIsaku Yamahata }
958135aeedSIsaku Yamahata 
968135aeedSIsaku Yamahata static int ioh3420_initfn(PCIDevice *d)
978135aeedSIsaku Yamahata {
98bcb75750SAndreas Färber     PCIEPort *p = PCIE_PORT(d);
99bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
1008135aeedSIsaku Yamahata     int rc;
1011108b2f8SCao jin     Error *err = NULL;
1028135aeedSIsaku Yamahata 
1032c533c54SMarcel Apfelbaum     pci_config_set_interrupt_pin(d->config, 1);
1049cfaa007SCao jin     pci_bridge_initfn(d, TYPE_PCIE_BUS);
1058135aeedSIsaku Yamahata     pcie_port_init_reg(d);
1068135aeedSIsaku Yamahata 
1078135aeedSIsaku Yamahata     rc = pci_bridge_ssvid_init(d, IOH_EP_SSVID_OFFSET,
1088135aeedSIsaku Yamahata                                IOH_EP_SSVID_SVID, IOH_EP_SSVID_SSID);
1098135aeedSIsaku Yamahata     if (rc < 0) {
11061620c2fSIsaku Yamahata         goto err_bridge;
1118135aeedSIsaku Yamahata     }
11252ea63deSCao jin 
1138135aeedSIsaku Yamahata     rc = msi_init(d, IOH_EP_MSI_OFFSET, IOH_EP_MSI_NR_VECTOR,
1148135aeedSIsaku Yamahata                   IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT,
1151108b2f8SCao jin                   IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT, &err);
1168135aeedSIsaku Yamahata     if (rc < 0) {
1171108b2f8SCao jin         assert(rc == -ENOTSUP);
1181108b2f8SCao jin         error_report_err(err);
11961620c2fSIsaku Yamahata         goto err_bridge;
1208135aeedSIsaku Yamahata     }
12152ea63deSCao jin 
1228135aeedSIsaku Yamahata     rc = pcie_cap_init(d, IOH_EP_EXP_OFFSET, PCI_EXP_TYPE_ROOT_PORT, p->port);
1238135aeedSIsaku Yamahata     if (rc < 0) {
12461620c2fSIsaku Yamahata         goto err_msi;
1258135aeedSIsaku Yamahata     }
126821be9dbSKnut Omang 
127a74b8702SKnut Omang     pcie_cap_arifwd_init(d);
1288135aeedSIsaku Yamahata     pcie_cap_deverr_init(d);
1298135aeedSIsaku Yamahata     pcie_cap_slot_init(d, s->slot);
13052ea63deSCao jin     pcie_cap_root_init(d);
13152ea63deSCao jin 
1328135aeedSIsaku Yamahata     pcie_chassis_create(s->chassis);
1338135aeedSIsaku Yamahata     rc = pcie_chassis_add_slot(s);
1348135aeedSIsaku Yamahata     if (rc < 0) {
13561620c2fSIsaku Yamahata         goto err_pcie_cap;
1368135aeedSIsaku Yamahata     }
13752ea63deSCao jin 
138*f18c697bSDou Liyang     rc = pcie_aer_init(d, PCI_ERR_VER, IOH_EP_AER_OFFSET,
139*f18c697bSDou Liyang                        PCI_ERR_SIZEOF, &err);
14061620c2fSIsaku Yamahata     if (rc < 0) {
14133848ceeSCao jin         error_report_err(err);
14261620c2fSIsaku Yamahata         goto err;
14361620c2fSIsaku Yamahata     }
14461620c2fSIsaku Yamahata     pcie_aer_root_init(d);
14561620c2fSIsaku Yamahata     ioh3420_aer_vector_update(d);
14652ea63deSCao jin 
1478135aeedSIsaku Yamahata     return 0;
14861620c2fSIsaku Yamahata 
14961620c2fSIsaku Yamahata err:
15061620c2fSIsaku Yamahata     pcie_chassis_del_slot(s);
15161620c2fSIsaku Yamahata err_pcie_cap:
15261620c2fSIsaku Yamahata     pcie_cap_exit(d);
15361620c2fSIsaku Yamahata err_msi:
15461620c2fSIsaku Yamahata     msi_uninit(d);
15561620c2fSIsaku Yamahata err_bridge:
156f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
15761620c2fSIsaku Yamahata     return rc;
1588135aeedSIsaku Yamahata }
1598135aeedSIsaku Yamahata 
160f90c2bcdSAlex Williamson static void ioh3420_exitfn(PCIDevice *d)
1618135aeedSIsaku Yamahata {
162bcb75750SAndreas Färber     PCIESlot *s = PCIE_SLOT(d);
16361620c2fSIsaku Yamahata 
16461620c2fSIsaku Yamahata     pcie_aer_exit(d);
16561620c2fSIsaku Yamahata     pcie_chassis_del_slot(s);
1668135aeedSIsaku Yamahata     pcie_cap_exit(d);
16761620c2fSIsaku Yamahata     msi_uninit(d);
168f90c2bcdSAlex Williamson     pci_bridge_exitfn(d);
1698135aeedSIsaku Yamahata }
1708135aeedSIsaku Yamahata 
171f23b6bdcSMarcel Apfelbaum static Property ioh3420_props[] = {
172f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present,
173f23b6bdcSMarcel Apfelbaum                     QEMU_PCIE_SLTCAP_PCP_BITNR, true),
174f23b6bdcSMarcel Apfelbaum     DEFINE_PROP_END_OF_LIST()
175f23b6bdcSMarcel Apfelbaum };
176f23b6bdcSMarcel Apfelbaum 
1778135aeedSIsaku Yamahata static const VMStateDescription vmstate_ioh3420 = {
1788135aeedSIsaku Yamahata     .name = "ioh-3240-express-root-port",
1798135aeedSIsaku Yamahata     .version_id = 1,
1808135aeedSIsaku Yamahata     .minimum_version_id = 1,
1816bde6aaaSMichael S. Tsirkin     .post_load = pcie_cap_slot_post_load,
1828135aeedSIsaku Yamahata     .fields = (VMStateField[]) {
183bcb75750SAndreas Färber         VMSTATE_PCIE_DEVICE(parent_obj.parent_obj.parent_obj, PCIESlot),
184bcb75750SAndreas Färber         VMSTATE_STRUCT(parent_obj.parent_obj.parent_obj.exp.aer_log,
185bcb75750SAndreas Färber                        PCIESlot, 0, vmstate_pcie_aer_log, PCIEAERLog),
1868135aeedSIsaku Yamahata         VMSTATE_END_OF_LIST()
1878135aeedSIsaku Yamahata     }
1888135aeedSIsaku Yamahata };
1898135aeedSIsaku Yamahata 
19040021f08SAnthony Liguori static void ioh3420_class_init(ObjectClass *klass, void *data)
19140021f08SAnthony Liguori {
19239bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
19340021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
19440021f08SAnthony Liguori 
19540021f08SAnthony Liguori     k->is_express = 1;
19640021f08SAnthony Liguori     k->is_bridge = 1;
19740021f08SAnthony Liguori     k->config_write = ioh3420_write_config;
19840021f08SAnthony Liguori     k->init = ioh3420_initfn;
19940021f08SAnthony Liguori     k->exit = ioh3420_exitfn;
20040021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
20140021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_IOH_EPORT;
20240021f08SAnthony Liguori     k->revision = PCI_DEVICE_ID_IOH_REV;
203125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
20439bffca2SAnthony Liguori     dc->desc = "Intel IOH device id 3420 PCIE Root Port";
20539bffca2SAnthony Liguori     dc->reset = ioh3420_reset;
20639bffca2SAnthony Liguori     dc->vmsd = &vmstate_ioh3420;
207f23b6bdcSMarcel Apfelbaum     dc->props = ioh3420_props;
2088135aeedSIsaku Yamahata }
20940021f08SAnthony Liguori 
2108c43a6f0SAndreas Färber static const TypeInfo ioh3420_info = {
21140021f08SAnthony Liguori     .name          = "ioh3420",
212bcb75750SAndreas Färber     .parent        = TYPE_PCIE_SLOT,
21340021f08SAnthony Liguori     .class_init    = ioh3420_class_init,
2148135aeedSIsaku Yamahata };
2158135aeedSIsaku Yamahata 
21683f7d43aSAndreas Färber static void ioh3420_register_types(void)
2178135aeedSIsaku Yamahata {
21839bffca2SAnthony Liguori     type_register_static(&ioh3420_info);
2198135aeedSIsaku Yamahata }
2208135aeedSIsaku Yamahata 
22183f7d43aSAndreas Färber type_init(ioh3420_register_types)
222