]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_pci_vfio.c
target/ppc, spapr: Move VPA information to machine_data
[mirror_qemu.git] / hw / ppc / spapr_pci_vfio.c
CommitLineData
9fc34ada
AK
1/*
2 * QEMU sPAPR PCI host for VFIO
3 *
4 * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
0d75590d 20#include "qemu/osdep.h"
a9c94277 21#include <linux/vfio.h>
4771d756
PB
22#include "qemu-common.h"
23#include "cpu.h"
9fc34ada
AK
24#include "hw/ppc/spapr.h"
25#include "hw/pci-host/spapr.h"
6319b1da 26#include "hw/pci/msix.h"
cf7087db 27#include "hw/vfio/vfio.h"
72700d7e 28#include "qemu/error-report.h"
a1aa1309 29#include "sysemu/qtest.h"
9fc34ada 30
c1fa017c
DG
31bool spapr_phb_eeh_available(sPAPRPHBState *sphb)
32{
33 return vfio_eeh_as_ok(&sphb->iommu_as);
34}
35
76a9e9f6 36static void spapr_phb_vfio_eeh_reenable(sPAPRPHBState *sphb)
aef87d1b 37{
76a9e9f6 38 vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_ENABLE);
aef87d1b
GS
39}
40
fbb4e983 41void spapr_phb_vfio_reset(DeviceState *qdev)
9fc34ada 42{
aef87d1b
GS
43 /*
44 * The PE might be in frozen state. To reenable the EEH
45 * functionality on it will clean the frozen state, which
46 * ensures that the contained PCI devices will work properly
47 * after reboot.
48 */
76a9e9f6 49 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev));
9fc34ada
AK
50}
51
fbb4e983
DG
52int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
53 unsigned int addr, int option)
2aad88f4 54{
76a9e9f6 55 uint32_t op;
2aad88f4
GS
56 int ret;
57
58 switch (option) {
59 case RTAS_EEH_DISABLE:
76a9e9f6 60 op = VFIO_EEH_PE_DISABLE;
2aad88f4
GS
61 break;
62 case RTAS_EEH_ENABLE: {
63 PCIHostState *phb;
64 PCIDevice *pdev;
65
66 /*
67 * The EEH functionality is enabled on basis of PCI device,
68 * instead of PE. We need check the validity of the PCI
69 * device address.
70 */
71 phb = PCI_HOST_BRIDGE(sphb);
72 pdev = pci_find_device(phb->bus,
73 (addr >> 16) & 0xFF, (addr >> 8) & 0xFF);
d76548a9 74 if (!pdev || !object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
2aad88f4
GS
75 return RTAS_OUT_PARAM_ERROR;
76 }
77
76a9e9f6 78 op = VFIO_EEH_PE_ENABLE;
2aad88f4
GS
79 break;
80 }
81 case RTAS_EEH_THAW_IO:
76a9e9f6 82 op = VFIO_EEH_PE_UNFREEZE_IO;
2aad88f4
GS
83 break;
84 case RTAS_EEH_THAW_DMA:
76a9e9f6 85 op = VFIO_EEH_PE_UNFREEZE_DMA;
2aad88f4
GS
86 break;
87 default:
88 return RTAS_OUT_PARAM_ERROR;
89 }
90
76a9e9f6 91 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
2aad88f4
GS
92 if (ret < 0) {
93 return RTAS_OUT_HW_ERROR;
94 }
95
96 return RTAS_OUT_SUCCESS;
97}
98
fbb4e983 99int spapr_phb_vfio_eeh_get_state(sPAPRPHBState *sphb, int *state)
2aad88f4 100{
2aad88f4
GS
101 int ret;
102
76a9e9f6 103 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_GET_STATE);
2aad88f4
GS
104 if (ret < 0) {
105 return RTAS_OUT_PARAM_ERROR;
106 }
107
108 *state = ret;
109 return RTAS_OUT_SUCCESS;
110}
111
6319b1da
GS
112static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *bus,
113 PCIDevice *pdev,
114 void *opaque)
115{
116 /* Check if the device is VFIO PCI device */
117 if (!object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
118 return;
119 }
120
121 /*
122 * The MSIx table will be cleaned out by reset. We need
123 * disable it so that it can be reenabled properly. Also,
124 * the cached MSIx table should be cleared as it's not
125 * reflecting the contents in hardware.
126 */
127 if (msix_enabled(pdev)) {
128 uint16_t flags;
129
130 flags = pci_host_config_read_common(pdev,
131 pdev->msix_cap + PCI_MSIX_FLAGS,
132 pci_config_size(pdev), 2);
133 flags &= ~PCI_MSIX_FLAGS_ENABLE;
134 pci_host_config_write_common(pdev,
135 pdev->msix_cap + PCI_MSIX_FLAGS,
136 pci_config_size(pdev), flags, 2);
137 }
138
139 msix_reset(pdev);
140}
141
142static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque)
143{
144 pci_for_each_device(bus, pci_bus_num(bus),
145 spapr_phb_vfio_eeh_clear_dev_msix, NULL);
146}
147
148static void spapr_phb_vfio_eeh_pre_reset(sPAPRPHBState *sphb)
149{
150 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
151
152 pci_for_each_bus(phb->bus, spapr_phb_vfio_eeh_clear_bus_msix, NULL);
153}
154
fbb4e983 155int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
2aad88f4 156{
76a9e9f6 157 uint32_t op;
2aad88f4
GS
158 int ret;
159
160 switch (option) {
161 case RTAS_SLOT_RESET_DEACTIVATE:
76a9e9f6 162 op = VFIO_EEH_PE_RESET_DEACTIVATE;
2aad88f4
GS
163 break;
164 case RTAS_SLOT_RESET_HOT:
6319b1da 165 spapr_phb_vfio_eeh_pre_reset(sphb);
76a9e9f6 166 op = VFIO_EEH_PE_RESET_HOT;
2aad88f4
GS
167 break;
168 case RTAS_SLOT_RESET_FUNDAMENTAL:
6319b1da 169 spapr_phb_vfio_eeh_pre_reset(sphb);
76a9e9f6 170 op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
2aad88f4
GS
171 break;
172 default:
173 return RTAS_OUT_PARAM_ERROR;
174 }
175
76a9e9f6 176 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
2aad88f4
GS
177 if (ret < 0) {
178 return RTAS_OUT_HW_ERROR;
179 }
180
181 return RTAS_OUT_SUCCESS;
182}
183
fbb4e983 184int spapr_phb_vfio_eeh_configure(sPAPRPHBState *sphb)
2aad88f4 185{
2aad88f4
GS
186 int ret;
187
76a9e9f6 188 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_CONFIGURE);
2aad88f4
GS
189 if (ret < 0) {
190 return RTAS_OUT_PARAM_ERROR;
191 }
192
193 return RTAS_OUT_SUCCESS;
194}