ipmi: Allow BMC device properties to be set
[qemu.git] / hw / ppc / spapr_pci_vfio.c
blob053efb03bd776dadecd0925bf3246da58e9984eb
1 /*
2 * QEMU sPAPR PCI host for VFIO
4 * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include <linux/vfio.h>
22 #include "qapi/error.h"
23 #include "qemu-common.h"
24 #include "cpu.h"
25 #include "hw/ppc/spapr.h"
26 #include "hw/pci-host/spapr.h"
27 #include "hw/pci/msix.h"
28 #include "hw/vfio/vfio.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/qtest.h"
32 bool spapr_phb_eeh_available(sPAPRPHBState *sphb)
34 return vfio_eeh_as_ok(&sphb->iommu_as);
37 static void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
39 vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_ENABLE);
42 void spapr_phb_vfio_reset(DeviceState *qdev)
45 * The PE might be in frozen state. To reenable the EEH
46 * functionality on it will clean the frozen state, which
47 * ensures that the contained PCI devices will work properly
48 * after reboot.
50 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev));
53 int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
54 unsigned int addr, int option)
56 uint32_t op;
57 int ret;
59 switch (option) {
60 case RTAS_EEH_DISABLE:
61 op = VFIO_EEH_PE_DISABLE;
62 break;
63 case RTAS_EEH_ENABLE: {
64 PCIHostState *phb;
65 PCIDevice *pdev;
68 * The EEH functionality is enabled on basis of PCI device,
69 * instead of PE. We need check the validity of the PCI
70 * device address.
72 phb = PCI_HOST_BRIDGE(sphb);
73 pdev = pci_find_device(phb->bus,
74 (addr >> 16) & 0xFF, (addr >> 8) & 0xFF);
75 if (!pdev || !object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
76 return RTAS_OUT_PARAM_ERROR;
79 op = VFIO_EEH_PE_ENABLE;
80 break;
82 case RTAS_EEH_THAW_IO:
83 op = VFIO_EEH_PE_UNFREEZE_IO;
84 break;
85 case RTAS_EEH_THAW_DMA:
86 op = VFIO_EEH_PE_UNFREEZE_DMA;
87 break;
88 default:
89 return RTAS_OUT_PARAM_ERROR;
92 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
93 if (ret < 0) {
94 return RTAS_OUT_HW_ERROR;
97 return RTAS_OUT_SUCCESS;
100 int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
102 int ret;
104 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_GET_STATE);
105 if (ret < 0) {
106 return RTAS_OUT_PARAM_ERROR;
109 *state = ret;
110 return RTAS_OUT_SUCCESS;
113 static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *bus,
114 PCIDevice *pdev,
115 void *opaque)
117 /* Check if the device is VFIO PCI device */
118 if (!object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
119 return;
123 * The MSIx table will be cleaned out by reset. We need
124 * disable it so that it can be reenabled properly. Also,
125 * the cached MSIx table should be cleared as it's not
126 * reflecting the contents in hardware.
128 if (msix_enabled(pdev)) {
129 uint16_t flags;
131 flags = pci_host_config_read_common(pdev,
132 pdev->msix_cap + PCI_MSIX_FLAGS,
133 pci_config_size(pdev), 2);
134 flags &= ~PCI_MSIX_FLAGS_ENABLE;
135 pci_host_config_write_common(pdev,
136 pdev->msix_cap + PCI_MSIX_FLAGS,
137 pci_config_size(pdev), flags, 2);
140 msix_reset(pdev);
143 static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque)
145 pci_for_each_device(bus, pci_bus_num(bus),
146 spapr_phb_vfio_eeh_clear_dev_msix, NULL);
149 static void spapr_phb_vfio_eeh_pre_reset(sPAPRPHBState *sphb)
151 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
153 pci_for_each_bus(phb->bus, spapr_phb_vfio_eeh_clear_bus_msix, NULL);
156 int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
158 uint32_t op;
159 int ret;
161 switch (option) {
162 case RTAS_SLOT_RESET_DEACTIVATE:
163 op = VFIO_EEH_PE_RESET_DEACTIVATE;
164 break;
165 case RTAS_SLOT_RESET_HOT:
166 spapr_phb_vfio_eeh_pre_reset(sphb);
167 op = VFIO_EEH_PE_RESET_HOT;
168 break;
169 case RTAS_SLOT_RESET_FUNDAMENTAL:
170 spapr_phb_vfio_eeh_pre_reset(sphb);
171 op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
172 break;
173 default:
174 return RTAS_OUT_PARAM_ERROR;
177 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
178 if (ret < 0) {
179 return RTAS_OUT_HW_ERROR;
182 return RTAS_OUT_SUCCESS;
185 int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
187 int ret;
189 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_CONFIGURE);
190 if (ret < 0) {
191 return RTAS_OUT_PARAM_ERROR;
194 return RTAS_OUT_SUCCESS;