]>
Commit | Line | Data |
---|---|---|
3384f95c DG |
1 | /* |
2 | * QEMU sPAPR PCI host originated from Uninorth PCI host | |
3 | * | |
4 | * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation. | |
5 | * Copyright (C) 2011 David Gibson, IBM Corporation. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
0d75590d | 25 | #include "qemu/osdep.h" |
da34e65c | 26 | #include "qapi/error.h" |
4771d756 PB |
27 | #include "qemu-common.h" |
28 | #include "cpu.h" | |
83c9f4ca | 29 | #include "hw/hw.h" |
1d2d9742 | 30 | #include "hw/sysbus.h" |
83c9f4ca PB |
31 | #include "hw/pci/pci.h" |
32 | #include "hw/pci/msi.h" | |
33 | #include "hw/pci/msix.h" | |
34 | #include "hw/pci/pci_host.h" | |
0d09e41a PB |
35 | #include "hw/ppc/spapr.h" |
36 | #include "hw/pci-host/spapr.h" | |
022c62cb | 37 | #include "exec/address-spaces.h" |
3384f95c | 38 | #include <libfdt.h> |
a2950fb6 | 39 | #include "trace.h" |
295d51aa | 40 | #include "qemu/error-report.h" |
7454c7af | 41 | #include "qapi/qmp/qerror.h" |
3384f95c | 42 | |
1d2d9742 | 43 | #include "hw/pci/pci_bridge.h" |
06aac7bd | 44 | #include "hw/pci/pci_bus.h" |
62083979 | 45 | #include "hw/ppc/spapr_drc.h" |
7454c7af | 46 | #include "sysemu/device_tree.h" |
77ac58dd | 47 | #include "sysemu/kvm.h" |
3384f95c | 48 | |
c1fa017c DG |
49 | #include "hw/vfio/vfio.h" |
50 | ||
0ee2c058 AK |
51 | /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */ |
52 | #define RTAS_QUERY_FN 0 | |
53 | #define RTAS_CHANGE_FN 1 | |
54 | #define RTAS_RESET_FN 2 | |
55 | #define RTAS_CHANGE_MSI_FN 3 | |
56 | #define RTAS_CHANGE_MSIX_FN 4 | |
57 | ||
58 | /* Interrupt types to return on RTAS_CHANGE_* */ | |
59 | #define RTAS_TYPE_MSI 1 | |
60 | #define RTAS_TYPE_MSIX 2 | |
61 | ||
9b7d9284 ND |
62 | #define FDT_NAME_MAX 128 |
63 | ||
7454c7af MR |
64 | #define _FDT(exp) \ |
65 | do { \ | |
66 | int ret = (exp); \ | |
67 | if (ret < 0) { \ | |
68 | return ret; \ | |
69 | } \ | |
70 | } while (0) | |
71 | ||
28e02042 | 72 | sPAPRPHBState *spapr_pci_find_phb(sPAPRMachineState *spapr, uint64_t buid) |
3384f95c | 73 | { |
8c9f64df | 74 | sPAPRPHBState *sphb; |
3384f95c | 75 | |
8c9f64df AF |
76 | QLIST_FOREACH(sphb, &spapr->phbs, list) { |
77 | if (sphb->buid != buid) { | |
3384f95c DG |
78 | continue; |
79 | } | |
8c9f64df | 80 | return sphb; |
9894c5d4 AK |
81 | } |
82 | ||
83 | return NULL; | |
84 | } | |
85 | ||
28e02042 | 86 | PCIDevice *spapr_pci_find_dev(sPAPRMachineState *spapr, uint64_t buid, |
46c5874e | 87 | uint32_t config_addr) |
9894c5d4 | 88 | { |
46c5874e | 89 | sPAPRPHBState *sphb = spapr_pci_find_phb(spapr, buid); |
8558d942 | 90 | PCIHostState *phb = PCI_HOST_BRIDGE(sphb); |
5dac82ce | 91 | int bus_num = (config_addr >> 16) & 0xFF; |
9894c5d4 AK |
92 | int devfn = (config_addr >> 8) & 0xFF; |
93 | ||
94 | if (!phb) { | |
95 | return NULL; | |
96 | } | |
3384f95c | 97 | |
5dac82ce | 98 | return pci_find_device(phb->bus, bus_num, devfn); |
3384f95c DG |
99 | } |
100 | ||
3f7565c9 BH |
101 | static uint32_t rtas_pci_cfgaddr(uint32_t arg) |
102 | { | |
92615a5a | 103 | /* This handles the encoding of extended config space addresses */ |
3f7565c9 BH |
104 | return ((arg >> 20) & 0xf00) | (arg & 0xff); |
105 | } | |
106 | ||
28e02042 | 107 | static void finish_read_pci_config(sPAPRMachineState *spapr, uint64_t buid, |
92615a5a DG |
108 | uint32_t addr, uint32_t size, |
109 | target_ulong rets) | |
88045ac5 | 110 | { |
92615a5a DG |
111 | PCIDevice *pci_dev; |
112 | uint32_t val; | |
113 | ||
114 | if ((size != 1) && (size != 2) && (size != 4)) { | |
115 | /* access must be 1, 2 or 4 bytes */ | |
a64d325d | 116 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
92615a5a | 117 | return; |
88045ac5 | 118 | } |
88045ac5 | 119 | |
46c5874e | 120 | pci_dev = spapr_pci_find_dev(spapr, buid, addr); |
92615a5a DG |
121 | addr = rtas_pci_cfgaddr(addr); |
122 | ||
123 | if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) { | |
124 | /* Access must be to a valid device, within bounds and | |
125 | * naturally aligned */ | |
a64d325d | 126 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
92615a5a | 127 | return; |
88045ac5 | 128 | } |
92615a5a DG |
129 | |
130 | val = pci_host_config_read_common(pci_dev, addr, | |
131 | pci_config_size(pci_dev), size); | |
132 | ||
a64d325d | 133 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); |
92615a5a | 134 | rtas_st(rets, 1, val); |
88045ac5 AG |
135 | } |
136 | ||
28e02042 | 137 | static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
3384f95c DG |
138 | uint32_t token, uint32_t nargs, |
139 | target_ulong args, | |
140 | uint32_t nret, target_ulong rets) | |
141 | { | |
92615a5a DG |
142 | uint64_t buid; |
143 | uint32_t size, addr; | |
3384f95c | 144 | |
92615a5a | 145 | if ((nargs != 4) || (nret != 2)) { |
a64d325d | 146 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
3384f95c DG |
147 | return; |
148 | } | |
92615a5a | 149 | |
a14aa92b | 150 | buid = rtas_ldq(args, 1); |
3384f95c | 151 | size = rtas_ld(args, 3); |
92615a5a DG |
152 | addr = rtas_ld(args, 0); |
153 | ||
154 | finish_read_pci_config(spapr, buid, addr, size, rets); | |
3384f95c DG |
155 | } |
156 | ||
28e02042 | 157 | static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
3384f95c DG |
158 | uint32_t token, uint32_t nargs, |
159 | target_ulong args, | |
160 | uint32_t nret, target_ulong rets) | |
161 | { | |
92615a5a | 162 | uint32_t size, addr; |
3384f95c | 163 | |
92615a5a | 164 | if ((nargs != 2) || (nret != 2)) { |
a64d325d | 165 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
3384f95c DG |
166 | return; |
167 | } | |
92615a5a | 168 | |
3384f95c | 169 | size = rtas_ld(args, 1); |
92615a5a DG |
170 | addr = rtas_ld(args, 0); |
171 | ||
172 | finish_read_pci_config(spapr, 0, addr, size, rets); | |
173 | } | |
174 | ||
28e02042 | 175 | static void finish_write_pci_config(sPAPRMachineState *spapr, uint64_t buid, |
92615a5a DG |
176 | uint32_t addr, uint32_t size, |
177 | uint32_t val, target_ulong rets) | |
178 | { | |
179 | PCIDevice *pci_dev; | |
180 | ||
181 | if ((size != 1) && (size != 2) && (size != 4)) { | |
182 | /* access must be 1, 2 or 4 bytes */ | |
a64d325d | 183 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
92615a5a DG |
184 | return; |
185 | } | |
186 | ||
46c5874e | 187 | pci_dev = spapr_pci_find_dev(spapr, buid, addr); |
92615a5a DG |
188 | addr = rtas_pci_cfgaddr(addr); |
189 | ||
190 | if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) { | |
191 | /* Access must be to a valid device, within bounds and | |
192 | * naturally aligned */ | |
a64d325d | 193 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
92615a5a DG |
194 | return; |
195 | } | |
196 | ||
197 | pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev), | |
198 | val, size); | |
199 | ||
a64d325d | 200 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); |
3384f95c DG |
201 | } |
202 | ||
28e02042 | 203 | static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
3384f95c DG |
204 | uint32_t token, uint32_t nargs, |
205 | target_ulong args, | |
206 | uint32_t nret, target_ulong rets) | |
207 | { | |
92615a5a | 208 | uint64_t buid; |
3384f95c | 209 | uint32_t val, size, addr; |
3384f95c | 210 | |
92615a5a | 211 | if ((nargs != 5) || (nret != 1)) { |
a64d325d | 212 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
3384f95c DG |
213 | return; |
214 | } | |
92615a5a | 215 | |
a14aa92b | 216 | buid = rtas_ldq(args, 1); |
3384f95c DG |
217 | val = rtas_ld(args, 4); |
218 | size = rtas_ld(args, 3); | |
92615a5a DG |
219 | addr = rtas_ld(args, 0); |
220 | ||
221 | finish_write_pci_config(spapr, buid, addr, size, val, rets); | |
3384f95c DG |
222 | } |
223 | ||
28e02042 | 224 | static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
3384f95c DG |
225 | uint32_t token, uint32_t nargs, |
226 | target_ulong args, | |
227 | uint32_t nret, target_ulong rets) | |
228 | { | |
229 | uint32_t val, size, addr; | |
3384f95c | 230 | |
92615a5a | 231 | if ((nargs != 3) || (nret != 1)) { |
a64d325d | 232 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
3384f95c DG |
233 | return; |
234 | } | |
92615a5a DG |
235 | |
236 | ||
3384f95c DG |
237 | val = rtas_ld(args, 2); |
238 | size = rtas_ld(args, 1); | |
92615a5a DG |
239 | addr = rtas_ld(args, 0); |
240 | ||
241 | finish_write_pci_config(spapr, 0, addr, size, val, rets); | |
3384f95c DG |
242 | } |
243 | ||
0ee2c058 AK |
244 | /* |
245 | * Set MSI/MSIX message data. | |
246 | * This is required for msi_notify()/msix_notify() which | |
247 | * will write at the addresses via spapr_msi_write(). | |
9a321e92 AK |
248 | * |
249 | * If hwaddr == 0, all entries will have .data == first_irq i.e. | |
250 | * table will be reset. | |
0ee2c058 | 251 | */ |
f1c2dc7c AK |
252 | static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix, |
253 | unsigned first_irq, unsigned req_num) | |
0ee2c058 AK |
254 | { |
255 | unsigned i; | |
f1c2dc7c | 256 | MSIMessage msg = { .address = addr, .data = first_irq }; |
0ee2c058 AK |
257 | |
258 | if (!msix) { | |
259 | msi_set_message(pdev, msg); | |
260 | trace_spapr_pci_msi_setup(pdev->name, 0, msg.address); | |
261 | return; | |
262 | } | |
263 | ||
9a321e92 | 264 | for (i = 0; i < req_num; ++i) { |
0ee2c058 AK |
265 | msix_set_message(pdev, i, msg); |
266 | trace_spapr_pci_msi_setup(pdev->name, i, msg.address); | |
9a321e92 AK |
267 | if (addr) { |
268 | ++msg.data; | |
269 | } | |
0ee2c058 AK |
270 | } |
271 | } | |
272 | ||
28e02042 | 273 | static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
0ee2c058 AK |
274 | uint32_t token, uint32_t nargs, |
275 | target_ulong args, uint32_t nret, | |
276 | target_ulong rets) | |
277 | { | |
278 | uint32_t config_addr = rtas_ld(args, 0); | |
a14aa92b | 279 | uint64_t buid = rtas_ldq(args, 1); |
0ee2c058 AK |
280 | unsigned int func = rtas_ld(args, 3); |
281 | unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */ | |
282 | unsigned int seq_num = rtas_ld(args, 5); | |
283 | unsigned int ret_intr_type; | |
d4a63ac8 | 284 | unsigned int irq, max_irqs = 0; |
0ee2c058 AK |
285 | sPAPRPHBState *phb = NULL; |
286 | PCIDevice *pdev = NULL; | |
9a321e92 AK |
287 | spapr_pci_msi *msi; |
288 | int *config_addr_key; | |
a005b3ef | 289 | Error *err = NULL; |
0ee2c058 AK |
290 | |
291 | switch (func) { | |
292 | case RTAS_CHANGE_MSI_FN: | |
293 | case RTAS_CHANGE_FN: | |
294 | ret_intr_type = RTAS_TYPE_MSI; | |
295 | break; | |
296 | case RTAS_CHANGE_MSIX_FN: | |
297 | ret_intr_type = RTAS_TYPE_MSIX; | |
298 | break; | |
299 | default: | |
295d51aa | 300 | error_report("rtas_ibm_change_msi(%u) is not implemented", func); |
a64d325d | 301 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); |
0ee2c058 AK |
302 | return; |
303 | } | |
304 | ||
305 | /* Fins sPAPRPHBState */ | |
46c5874e | 306 | phb = spapr_pci_find_phb(spapr, buid); |
0ee2c058 | 307 | if (phb) { |
46c5874e | 308 | pdev = spapr_pci_find_dev(spapr, buid, config_addr); |
0ee2c058 AK |
309 | } |
310 | if (!phb || !pdev) { | |
a64d325d | 311 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); |
0ee2c058 AK |
312 | return; |
313 | } | |
314 | ||
ce266b75 GK |
315 | msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr); |
316 | ||
0ee2c058 AK |
317 | /* Releasing MSIs */ |
318 | if (!req_num) { | |
9a321e92 AK |
319 | if (!msi) { |
320 | trace_spapr_pci_msi("Releasing wrong config", config_addr); | |
a64d325d | 321 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
0ee2c058 AK |
322 | return; |
323 | } | |
9a321e92 AK |
324 | |
325 | xics_free(spapr->icp, msi->first_irq, msi->num); | |
32420522 | 326 | if (msi_present(pdev)) { |
d4a63ac8 | 327 | spapr_msi_setmsg(pdev, 0, false, 0, 0); |
32420522 AK |
328 | } |
329 | if (msix_present(pdev)) { | |
d4a63ac8 | 330 | spapr_msi_setmsg(pdev, 0, true, 0, 0); |
32420522 | 331 | } |
9a321e92 AK |
332 | g_hash_table_remove(phb->msi, &config_addr); |
333 | ||
334 | trace_spapr_pci_msi("Released MSIs", config_addr); | |
a64d325d | 335 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); |
0ee2c058 AK |
336 | rtas_st(rets, 1, 0); |
337 | return; | |
338 | } | |
339 | ||
340 | /* Enabling MSI */ | |
341 | ||
28668b5f AK |
342 | /* Check if the device supports as many IRQs as requested */ |
343 | if (ret_intr_type == RTAS_TYPE_MSI) { | |
344 | max_irqs = msi_nr_vectors_allocated(pdev); | |
345 | } else if (ret_intr_type == RTAS_TYPE_MSIX) { | |
346 | max_irqs = pdev->msix_entries_nr; | |
347 | } | |
348 | if (!max_irqs) { | |
9a321e92 AK |
349 | error_report("Requested interrupt type %d is not enabled for device %x", |
350 | ret_intr_type, config_addr); | |
28668b5f AK |
351 | rtas_st(rets, 0, -1); /* Hardware error */ |
352 | return; | |
353 | } | |
354 | /* Correct the number if the guest asked for too many */ | |
355 | if (req_num > max_irqs) { | |
9a321e92 | 356 | trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs); |
28668b5f | 357 | req_num = max_irqs; |
9a321e92 AK |
358 | irq = 0; /* to avoid misleading trace */ |
359 | goto out; | |
28668b5f AK |
360 | } |
361 | ||
9a321e92 AK |
362 | /* Allocate MSIs */ |
363 | irq = xics_alloc_block(spapr->icp, 0, req_num, false, | |
a005b3ef GK |
364 | ret_intr_type == RTAS_TYPE_MSI, &err); |
365 | if (err) { | |
366 | error_reportf_err(err, "Can't allocate MSIs for device %x: ", | |
367 | config_addr); | |
a64d325d | 368 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
0ee2c058 AK |
369 | return; |
370 | } | |
371 | ||
ce266b75 GK |
372 | /* Release previous MSIs */ |
373 | if (msi) { | |
374 | xics_free(spapr->icp, msi->first_irq, msi->num); | |
375 | g_hash_table_remove(phb->msi, &config_addr); | |
376 | } | |
377 | ||
0ee2c058 | 378 | /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */ |
8c46f7ec | 379 | spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX, |
9a321e92 | 380 | irq, req_num); |
0ee2c058 | 381 | |
9a321e92 AK |
382 | /* Add MSI device to cache */ |
383 | msi = g_new(spapr_pci_msi, 1); | |
384 | msi->first_irq = irq; | |
385 | msi->num = req_num; | |
386 | config_addr_key = g_new(int, 1); | |
387 | *config_addr_key = config_addr; | |
388 | g_hash_table_insert(phb->msi, config_addr_key, msi); | |
389 | ||
390 | out: | |
a64d325d | 391 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); |
0ee2c058 AK |
392 | rtas_st(rets, 1, req_num); |
393 | rtas_st(rets, 2, ++seq_num); | |
b359bd6a SB |
394 | if (nret > 3) { |
395 | rtas_st(rets, 3, ret_intr_type); | |
396 | } | |
0ee2c058 | 397 | |
9a321e92 | 398 | trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq); |
0ee2c058 AK |
399 | } |
400 | ||
210b580b | 401 | static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu, |
28e02042 | 402 | sPAPRMachineState *spapr, |
0ee2c058 AK |
403 | uint32_t token, |
404 | uint32_t nargs, | |
405 | target_ulong args, | |
406 | uint32_t nret, | |
407 | target_ulong rets) | |
408 | { | |
409 | uint32_t config_addr = rtas_ld(args, 0); | |
a14aa92b | 410 | uint64_t buid = rtas_ldq(args, 1); |
0ee2c058 | 411 | unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3); |
0ee2c058 | 412 | sPAPRPHBState *phb = NULL; |
9a321e92 AK |
413 | PCIDevice *pdev = NULL; |
414 | spapr_pci_msi *msi; | |
0ee2c058 | 415 | |
9a321e92 | 416 | /* Find sPAPRPHBState */ |
46c5874e | 417 | phb = spapr_pci_find_phb(spapr, buid); |
9a321e92 | 418 | if (phb) { |
46c5874e | 419 | pdev = spapr_pci_find_dev(spapr, buid, config_addr); |
9a321e92 AK |
420 | } |
421 | if (!phb || !pdev) { | |
a64d325d | 422 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); |
0ee2c058 AK |
423 | return; |
424 | } | |
425 | ||
426 | /* Find device descriptor and start IRQ */ | |
9a321e92 AK |
427 | msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr); |
428 | if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) { | |
429 | trace_spapr_pci_msi("Failed to return vector", config_addr); | |
a64d325d | 430 | rtas_st(rets, 0, RTAS_OUT_HW_ERROR); |
0ee2c058 AK |
431 | return; |
432 | } | |
9a321e92 | 433 | intr_src_num = msi->first_irq + ioa_intr_num; |
0ee2c058 AK |
434 | trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num, |
435 | intr_src_num); | |
436 | ||
a64d325d | 437 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); |
0ee2c058 AK |
438 | rtas_st(rets, 1, intr_src_num); |
439 | rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */ | |
440 | } | |
441 | ||
ee954280 | 442 | static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu, |
28e02042 | 443 | sPAPRMachineState *spapr, |
ee954280 GS |
444 | uint32_t token, uint32_t nargs, |
445 | target_ulong args, uint32_t nret, | |
446 | target_ulong rets) | |
447 | { | |
448 | sPAPRPHBState *sphb; | |
ee954280 GS |
449 | uint32_t addr, option; |
450 | uint64_t buid; | |
451 | int ret; | |
452 | ||
453 | if ((nargs != 4) || (nret != 1)) { | |
454 | goto param_error_exit; | |
455 | } | |
456 | ||
a14aa92b | 457 | buid = rtas_ldq(args, 1); |
ee954280 GS |
458 | addr = rtas_ld(args, 0); |
459 | option = rtas_ld(args, 3); | |
460 | ||
46c5874e | 461 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
462 | if (!sphb) { |
463 | goto param_error_exit; | |
464 | } | |
465 | ||
fbb4e983 | 466 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
467 | goto param_error_exit; |
468 | } | |
469 | ||
fbb4e983 | 470 | ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option); |
ee954280 GS |
471 | rtas_st(rets, 0, ret); |
472 | return; | |
473 | ||
474 | param_error_exit: | |
475 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
476 | } | |
477 | ||
478 | static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu, | |
28e02042 | 479 | sPAPRMachineState *spapr, |
ee954280 GS |
480 | uint32_t token, uint32_t nargs, |
481 | target_ulong args, uint32_t nret, | |
482 | target_ulong rets) | |
483 | { | |
484 | sPAPRPHBState *sphb; | |
ee954280 GS |
485 | PCIDevice *pdev; |
486 | uint32_t addr, option; | |
487 | uint64_t buid; | |
488 | ||
489 | if ((nargs != 4) || (nret != 2)) { | |
490 | goto param_error_exit; | |
491 | } | |
492 | ||
a14aa92b | 493 | buid = rtas_ldq(args, 1); |
46c5874e | 494 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
495 | if (!sphb) { |
496 | goto param_error_exit; | |
497 | } | |
498 | ||
fbb4e983 | 499 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
500 | goto param_error_exit; |
501 | } | |
502 | ||
503 | /* | |
504 | * We always have PE address of form "00BB0001". "BB" | |
505 | * represents the bus number of PE's primary bus. | |
506 | */ | |
507 | option = rtas_ld(args, 3); | |
508 | switch (option) { | |
509 | case RTAS_GET_PE_ADDR: | |
510 | addr = rtas_ld(args, 0); | |
46c5874e | 511 | pdev = spapr_pci_find_dev(spapr, buid, addr); |
ee954280 GS |
512 | if (!pdev) { |
513 | goto param_error_exit; | |
514 | } | |
515 | ||
516 | rtas_st(rets, 1, (pci_bus_num(pdev->bus) << 16) + 1); | |
517 | break; | |
518 | case RTAS_GET_PE_MODE: | |
519 | rtas_st(rets, 1, RTAS_PE_MODE_SHARED); | |
520 | break; | |
521 | default: | |
522 | goto param_error_exit; | |
523 | } | |
524 | ||
525 | rtas_st(rets, 0, RTAS_OUT_SUCCESS); | |
526 | return; | |
527 | ||
528 | param_error_exit: | |
529 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
530 | } | |
531 | ||
532 | static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu, | |
28e02042 | 533 | sPAPRMachineState *spapr, |
ee954280 GS |
534 | uint32_t token, uint32_t nargs, |
535 | target_ulong args, uint32_t nret, | |
536 | target_ulong rets) | |
537 | { | |
538 | sPAPRPHBState *sphb; | |
ee954280 GS |
539 | uint64_t buid; |
540 | int state, ret; | |
541 | ||
542 | if ((nargs != 3) || (nret != 4 && nret != 5)) { | |
543 | goto param_error_exit; | |
544 | } | |
545 | ||
a14aa92b | 546 | buid = rtas_ldq(args, 1); |
46c5874e | 547 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
548 | if (!sphb) { |
549 | goto param_error_exit; | |
550 | } | |
551 | ||
fbb4e983 | 552 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
553 | goto param_error_exit; |
554 | } | |
555 | ||
fbb4e983 | 556 | ret = spapr_phb_vfio_eeh_get_state(sphb, &state); |
ee954280 GS |
557 | rtas_st(rets, 0, ret); |
558 | if (ret != RTAS_OUT_SUCCESS) { | |
559 | return; | |
560 | } | |
561 | ||
562 | rtas_st(rets, 1, state); | |
563 | rtas_st(rets, 2, RTAS_EEH_SUPPORT); | |
564 | rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO); | |
565 | if (nret >= 5) { | |
566 | rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO); | |
567 | } | |
568 | return; | |
569 | ||
570 | param_error_exit: | |
571 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
572 | } | |
573 | ||
574 | static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu, | |
28e02042 | 575 | sPAPRMachineState *spapr, |
ee954280 GS |
576 | uint32_t token, uint32_t nargs, |
577 | target_ulong args, uint32_t nret, | |
578 | target_ulong rets) | |
579 | { | |
580 | sPAPRPHBState *sphb; | |
ee954280 GS |
581 | uint32_t option; |
582 | uint64_t buid; | |
583 | int ret; | |
584 | ||
585 | if ((nargs != 4) || (nret != 1)) { | |
586 | goto param_error_exit; | |
587 | } | |
588 | ||
a14aa92b | 589 | buid = rtas_ldq(args, 1); |
ee954280 | 590 | option = rtas_ld(args, 3); |
46c5874e | 591 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
592 | if (!sphb) { |
593 | goto param_error_exit; | |
594 | } | |
595 | ||
fbb4e983 | 596 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
597 | goto param_error_exit; |
598 | } | |
599 | ||
fbb4e983 | 600 | ret = spapr_phb_vfio_eeh_reset(sphb, option); |
ee954280 GS |
601 | rtas_st(rets, 0, ret); |
602 | return; | |
603 | ||
604 | param_error_exit: | |
605 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
606 | } | |
607 | ||
608 | static void rtas_ibm_configure_pe(PowerPCCPU *cpu, | |
28e02042 | 609 | sPAPRMachineState *spapr, |
ee954280 GS |
610 | uint32_t token, uint32_t nargs, |
611 | target_ulong args, uint32_t nret, | |
612 | target_ulong rets) | |
613 | { | |
614 | sPAPRPHBState *sphb; | |
ee954280 GS |
615 | uint64_t buid; |
616 | int ret; | |
617 | ||
618 | if ((nargs != 3) || (nret != 1)) { | |
619 | goto param_error_exit; | |
620 | } | |
621 | ||
a14aa92b | 622 | buid = rtas_ldq(args, 1); |
46c5874e | 623 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
624 | if (!sphb) { |
625 | goto param_error_exit; | |
626 | } | |
627 | ||
fbb4e983 | 628 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
629 | goto param_error_exit; |
630 | } | |
631 | ||
fbb4e983 | 632 | ret = spapr_phb_vfio_eeh_configure(sphb); |
ee954280 GS |
633 | rtas_st(rets, 0, ret); |
634 | return; | |
635 | ||
636 | param_error_exit: | |
637 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
638 | } | |
639 | ||
640 | /* To support it later */ | |
641 | static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu, | |
28e02042 | 642 | sPAPRMachineState *spapr, |
ee954280 GS |
643 | uint32_t token, uint32_t nargs, |
644 | target_ulong args, uint32_t nret, | |
645 | target_ulong rets) | |
646 | { | |
647 | sPAPRPHBState *sphb; | |
ee954280 GS |
648 | int option; |
649 | uint64_t buid; | |
650 | ||
651 | if ((nargs != 8) || (nret != 1)) { | |
652 | goto param_error_exit; | |
653 | } | |
654 | ||
a14aa92b | 655 | buid = rtas_ldq(args, 1); |
46c5874e | 656 | sphb = spapr_pci_find_phb(spapr, buid); |
ee954280 GS |
657 | if (!sphb) { |
658 | goto param_error_exit; | |
659 | } | |
660 | ||
fbb4e983 | 661 | if (!spapr_phb_eeh_available(sphb)) { |
ee954280 GS |
662 | goto param_error_exit; |
663 | } | |
664 | ||
665 | option = rtas_ld(args, 7); | |
666 | switch (option) { | |
667 | case RTAS_SLOT_TEMP_ERR_LOG: | |
668 | case RTAS_SLOT_PERM_ERR_LOG: | |
669 | break; | |
670 | default: | |
671 | goto param_error_exit; | |
672 | } | |
673 | ||
674 | /* We don't have error log yet */ | |
675 | rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND); | |
676 | return; | |
677 | ||
678 | param_error_exit: | |
679 | rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); | |
680 | } | |
681 | ||
7fb0bd34 DG |
682 | static int pci_spapr_swizzle(int slot, int pin) |
683 | { | |
684 | return (slot + pin) % PCI_NUM_PINS; | |
685 | } | |
686 | ||
3384f95c DG |
687 | static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num) |
688 | { | |
689 | /* | |
690 | * Here we need to convert pci_dev + irq_num to some unique value | |
7fb0bd34 DG |
691 | * which is less than number of IRQs on the specific bus (4). We |
692 | * use standard PCI swizzling, that is (slot number + pin number) | |
693 | * % 4. | |
3384f95c | 694 | */ |
7fb0bd34 | 695 | return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num); |
3384f95c DG |
696 | } |
697 | ||
698 | static void pci_spapr_set_irq(void *opaque, int irq_num, int level) | |
699 | { | |
700 | /* | |
701 | * Here we use the number returned by pci_spapr_map_irq to find a | |
702 | * corresponding qemu_irq. | |
703 | */ | |
704 | sPAPRPHBState *phb = opaque; | |
705 | ||
caae58cb | 706 | trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq); |
a307d594 | 707 | qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level); |
3384f95c DG |
708 | } |
709 | ||
5cc7a967 AK |
710 | static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin) |
711 | { | |
712 | sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque); | |
713 | PCIINTxRoute route; | |
714 | ||
715 | route.mode = PCI_INTX_ENABLED; | |
716 | route.irq = sphb->lsi_table[pin].irq; | |
717 | ||
718 | return route; | |
719 | } | |
720 | ||
0ee2c058 AK |
721 | /* |
722 | * MSI/MSIX memory region implementation. | |
723 | * The handler handles both MSI and MSIX. | |
724 | * For MSI-X, the vector number is encoded as a part of the address, | |
725 | * data is set to 0. | |
726 | * For MSI, the vector number is encoded in least bits in data. | |
727 | */ | |
a8170e5e | 728 | static void spapr_msi_write(void *opaque, hwaddr addr, |
0ee2c058 AK |
729 | uint64_t data, unsigned size) |
730 | { | |
28e02042 | 731 | sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); |
f1c2dc7c | 732 | uint32_t irq = data; |
0ee2c058 AK |
733 | |
734 | trace_spapr_pci_msi_write(addr, data, irq); | |
735 | ||
736 | qemu_irq_pulse(xics_get_qirq(spapr->icp, irq)); | |
737 | } | |
738 | ||
739 | static const MemoryRegionOps spapr_msi_ops = { | |
740 | /* There is no .read as the read result is undefined by PCI spec */ | |
741 | .read = NULL, | |
742 | .write = spapr_msi_write, | |
743 | .endianness = DEVICE_LITTLE_ENDIAN | |
744 | }; | |
745 | ||
298a9710 DG |
746 | /* |
747 | * PHB PCI device | |
748 | */ | |
e00387d5 | 749 | static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn) |
edded454 DG |
750 | { |
751 | sPAPRPHBState *phb = opaque; | |
752 | ||
e00387d5 | 753 | return &phb->iommu_as; |
edded454 DG |
754 | } |
755 | ||
16b0ea1d ND |
756 | static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev) |
757 | { | |
758 | char *path = NULL, *buf = NULL, *host = NULL; | |
759 | ||
760 | /* Get the PCI VFIO host id */ | |
761 | host = object_property_get_str(OBJECT(pdev), "host", NULL); | |
762 | if (!host) { | |
763 | goto err_out; | |
764 | } | |
765 | ||
766 | /* Construct the path of the file that will give us the DT location */ | |
767 | path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host); | |
768 | g_free(host); | |
769 | if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { | |
770 | goto err_out; | |
771 | } | |
772 | g_free(path); | |
773 | ||
774 | /* Construct and read from host device tree the loc-code */ | |
775 | path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf); | |
776 | g_free(buf); | |
777 | if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { | |
778 | goto err_out; | |
779 | } | |
780 | return buf; | |
781 | ||
782 | err_out: | |
783 | g_free(path); | |
784 | return NULL; | |
785 | } | |
786 | ||
787 | static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev) | |
788 | { | |
789 | char *buf; | |
790 | const char *devtype = "qemu"; | |
791 | uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)))); | |
792 | ||
793 | if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { | |
794 | buf = spapr_phb_vfio_get_loc_code(sphb, pdev); | |
795 | if (buf) { | |
796 | return buf; | |
797 | } | |
798 | devtype = "vfio"; | |
799 | } | |
800 | /* | |
801 | * For emulated devices and VFIO-failure case, make up | |
802 | * the loc-code. | |
803 | */ | |
804 | buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x", | |
805 | devtype, pdev->name, sphb->index, busnr, | |
806 | PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); | |
807 | return buf; | |
808 | } | |
809 | ||
7454c7af MR |
810 | /* Macros to operate with address in OF binding to PCI */ |
811 | #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p)) | |
812 | #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */ | |
813 | #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */ | |
814 | #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */ | |
815 | #define b_ss(x) b_x((x), 24, 2) /* the space code */ | |
816 | #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */ | |
817 | #define b_ddddd(x) b_x((x), 11, 5) /* device number */ | |
818 | #define b_fff(x) b_x((x), 8, 3) /* function number */ | |
819 | #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */ | |
820 | ||
821 | /* for 'reg'/'assigned-addresses' OF properties */ | |
822 | #define RESOURCE_CELLS_SIZE 2 | |
823 | #define RESOURCE_CELLS_ADDRESS 3 | |
824 | ||
825 | typedef struct ResourceFields { | |
826 | uint32_t phys_hi; | |
827 | uint32_t phys_mid; | |
828 | uint32_t phys_lo; | |
829 | uint32_t size_hi; | |
830 | uint32_t size_lo; | |
831 | } QEMU_PACKED ResourceFields; | |
832 | ||
833 | typedef struct ResourceProps { | |
834 | ResourceFields reg[8]; | |
835 | ResourceFields assigned[7]; | |
836 | uint32_t reg_len; | |
837 | uint32_t assigned_len; | |
838 | } ResourceProps; | |
839 | ||
840 | /* fill in the 'reg'/'assigned-resources' OF properties for | |
841 | * a PCI device. 'reg' describes resource requirements for a | |
842 | * device's IO/MEM regions, 'assigned-addresses' describes the | |
843 | * actual resource assignments. | |
844 | * | |
845 | * the properties are arrays of ('phys-addr', 'size') pairs describing | |
846 | * the addressable regions of the PCI device, where 'phys-addr' is a | |
847 | * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to | |
848 | * (phys.hi, phys.mid, phys.lo), and 'size' is a | |
849 | * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo). | |
850 | * | |
851 | * phys.hi = 0xYYXXXXZZ, where: | |
852 | * 0xYY = npt000ss | |
853 | * ||| | | |
72187935 ND |
854 | * ||| +-- space code |
855 | * ||| | | |
856 | * ||| + 00 if configuration space | |
857 | * ||| + 01 if IO region, | |
858 | * ||| + 10 if 32-bit MEM region | |
859 | * ||| + 11 if 64-bit MEM region | |
860 | * ||| | |
7454c7af MR |
861 | * ||+------ for non-relocatable IO: 1 if aliased |
862 | * || for relocatable IO: 1 if below 64KB | |
863 | * || for MEM: 1 if below 1MB | |
864 | * |+------- 1 if region is prefetchable | |
865 | * +-------- 1 if region is non-relocatable | |
866 | * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function | |
867 | * bits respectively | |
868 | * 0xZZ = rrrrrrrr, the register number of the BAR corresponding | |
869 | * to the region | |
870 | * | |
871 | * phys.mid and phys.lo correspond respectively to the hi/lo portions | |
872 | * of the actual address of the region. | |
873 | * | |
874 | * how the phys-addr/size values are used differ slightly between | |
875 | * 'reg' and 'assigned-addresses' properties. namely, 'reg' has | |
876 | * an additional description for the config space region of the | |
877 | * device, and in the case of QEMU has n=0 and phys.mid=phys.lo=0 | |
878 | * to describe the region as relocatable, with an address-mapping | |
879 | * that corresponds directly to the PHB's address space for the | |
880 | * resource. 'assigned-addresses' always has n=1 set with an absolute | |
881 | * address assigned for the resource. in general, 'assigned-addresses' | |
882 | * won't be populated, since addresses for PCI devices are generally | |
883 | * unmapped initially and left to the guest to assign. | |
884 | * | |
885 | * note also that addresses defined in these properties are, at least | |
886 | * for PAPR guests, relative to the PHBs IO/MEM windows, and | |
887 | * correspond directly to the addresses in the BARs. | |
888 | * | |
889 | * in accordance with PCI Bus Binding to Open Firmware, | |
890 | * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7, | |
891 | * Appendix C. | |
892 | */ | |
893 | static void populate_resource_props(PCIDevice *d, ResourceProps *rp) | |
894 | { | |
895 | int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d)))); | |
896 | uint32_t dev_id = (b_bbbbbbbb(bus_num) | | |
897 | b_ddddd(PCI_SLOT(d->devfn)) | | |
898 | b_fff(PCI_FUNC(d->devfn))); | |
899 | ResourceFields *reg, *assigned; | |
900 | int i, reg_idx = 0, assigned_idx = 0; | |
901 | ||
902 | /* config space region */ | |
903 | reg = &rp->reg[reg_idx++]; | |
904 | reg->phys_hi = cpu_to_be32(dev_id); | |
905 | reg->phys_mid = 0; | |
906 | reg->phys_lo = 0; | |
907 | reg->size_hi = 0; | |
908 | reg->size_lo = 0; | |
909 | ||
910 | for (i = 0; i < PCI_NUM_REGIONS; i++) { | |
911 | if (!d->io_regions[i].size) { | |
912 | continue; | |
913 | } | |
914 | ||
915 | reg = &rp->reg[reg_idx++]; | |
916 | ||
917 | reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i))); | |
918 | if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) { | |
919 | reg->phys_hi |= cpu_to_be32(b_ss(1)); | |
72187935 ND |
920 | } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
921 | reg->phys_hi |= cpu_to_be32(b_ss(3)); | |
7454c7af MR |
922 | } else { |
923 | reg->phys_hi |= cpu_to_be32(b_ss(2)); | |
924 | } | |
925 | reg->phys_mid = 0; | |
926 | reg->phys_lo = 0; | |
927 | reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32); | |
928 | reg->size_lo = cpu_to_be32(d->io_regions[i].size); | |
929 | ||
930 | if (d->io_regions[i].addr == PCI_BAR_UNMAPPED) { | |
931 | continue; | |
932 | } | |
933 | ||
934 | assigned = &rp->assigned[assigned_idx++]; | |
935 | assigned->phys_hi = cpu_to_be32(reg->phys_hi | b_n(1)); | |
936 | assigned->phys_mid = cpu_to_be32(d->io_regions[i].addr >> 32); | |
937 | assigned->phys_lo = cpu_to_be32(d->io_regions[i].addr); | |
938 | assigned->size_hi = reg->size_hi; | |
939 | assigned->size_lo = reg->size_lo; | |
940 | } | |
941 | ||
942 | rp->reg_len = reg_idx * sizeof(ResourceFields); | |
943 | rp->assigned_len = assigned_idx * sizeof(ResourceFields); | |
944 | } | |
945 | ||
e634b89c ND |
946 | static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb, |
947 | PCIDevice *pdev); | |
948 | ||
7454c7af | 949 | static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, |
16b0ea1d | 950 | sPAPRPHBState *sphb) |
7454c7af MR |
951 | { |
952 | ResourceProps rp; | |
953 | bool is_bridge = false; | |
16b0ea1d ND |
954 | int pci_status, err; |
955 | char *buf = NULL; | |
e634b89c | 956 | uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev); |
a8ad731a | 957 | uint32_t max_msi, max_msix; |
7454c7af MR |
958 | |
959 | if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) == | |
960 | PCI_HEADER_TYPE_BRIDGE) { | |
961 | is_bridge = true; | |
962 | } | |
963 | ||
964 | /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */ | |
965 | _FDT(fdt_setprop_cell(fdt, offset, "vendor-id", | |
966 | pci_default_read_config(dev, PCI_VENDOR_ID, 2))); | |
967 | _FDT(fdt_setprop_cell(fdt, offset, "device-id", | |
968 | pci_default_read_config(dev, PCI_DEVICE_ID, 2))); | |
969 | _FDT(fdt_setprop_cell(fdt, offset, "revision-id", | |
970 | pci_default_read_config(dev, PCI_REVISION_ID, 1))); | |
971 | _FDT(fdt_setprop_cell(fdt, offset, "class-code", | |
4a7c3474 | 972 | pci_default_read_config(dev, PCI_CLASS_PROG, 3))); |
7454c7af MR |
973 | if (pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)) { |
974 | _FDT(fdt_setprop_cell(fdt, offset, "interrupts", | |
975 | pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1))); | |
976 | } | |
977 | ||
978 | if (!is_bridge) { | |
979 | _FDT(fdt_setprop_cell(fdt, offset, "min-grant", | |
980 | pci_default_read_config(dev, PCI_MIN_GNT, 1))); | |
981 | _FDT(fdt_setprop_cell(fdt, offset, "max-latency", | |
982 | pci_default_read_config(dev, PCI_MAX_LAT, 1))); | |
983 | } | |
984 | ||
985 | if (pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)) { | |
986 | _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id", | |
987 | pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2))); | |
988 | } | |
989 | ||
990 | if (pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)) { | |
991 | _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id", | |
992 | pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2))); | |
993 | } | |
994 | ||
995 | _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size", | |
996 | pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1))); | |
997 | ||
998 | /* the following fdt cells are masked off the pci status register */ | |
999 | pci_status = pci_default_read_config(dev, PCI_STATUS, 2); | |
1000 | _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed", | |
1001 | PCI_STATUS_DEVSEL_MASK & pci_status)); | |
1002 | ||
1003 | if (pci_status & PCI_STATUS_FAST_BACK) { | |
1004 | _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0)); | |
1005 | } | |
1006 | if (pci_status & PCI_STATUS_66MHZ) { | |
1007 | _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0)); | |
1008 | } | |
1009 | if (pci_status & PCI_STATUS_UDF) { | |
1010 | _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0)); | |
1011 | } | |
1012 | ||
1013 | /* NOTE: this is normally generated by firmware via path/unit name, | |
1014 | * but in our case we must set it manually since it does not get | |
1015 | * processed by OF beforehand | |
1016 | */ | |
1017 | _FDT(fdt_setprop_string(fdt, offset, "name", "pci")); | |
16b0ea1d ND |
1018 | buf = spapr_phb_get_loc_code(sphb, dev); |
1019 | if (!buf) { | |
1020 | error_report("Failed setting the ibm,loc-code"); | |
1021 | return -1; | |
1022 | } | |
1023 | ||
1024 | err = fdt_setprop_string(fdt, offset, "ibm,loc-code", buf); | |
1025 | g_free(buf); | |
1026 | if (err < 0) { | |
1027 | return err; | |
1028 | } | |
1029 | ||
e634b89c ND |
1030 | if (drc_index) { |
1031 | _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)); | |
1032 | } | |
7454c7af MR |
1033 | |
1034 | _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", | |
1035 | RESOURCE_CELLS_ADDRESS)); | |
1036 | _FDT(fdt_setprop_cell(fdt, offset, "#size-cells", | |
1037 | RESOURCE_CELLS_SIZE)); | |
a8ad731a MR |
1038 | |
1039 | max_msi = msi_nr_vectors_allocated(dev); | |
1040 | if (max_msi) { | |
1041 | _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi)); | |
1042 | } | |
1043 | max_msix = dev->msix_entries_nr; | |
1044 | if (max_msix) { | |
1045 | _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix)); | |
1046 | } | |
7454c7af MR |
1047 | |
1048 | populate_resource_props(dev, &rp); | |
1049 | _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len)); | |
1050 | _FDT(fdt_setprop(fdt, offset, "assigned-addresses", | |
1051 | (uint8_t *)rp.assigned, rp.assigned_len)); | |
1052 | ||
1053 | return 0; | |
1054 | } | |
1055 | ||
1056 | /* create OF node for pci device and required OF DT properties */ | |
1d2d9742 | 1057 | static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev, |
1d2d9742 | 1058 | void *fdt, int node_offset) |
7454c7af | 1059 | { |
1d2d9742 | 1060 | int offset, ret; |
7454c7af MR |
1061 | int slot = PCI_SLOT(dev->devfn); |
1062 | int func = PCI_FUNC(dev->devfn); | |
9b7d9284 | 1063 | char nodename[FDT_NAME_MAX]; |
7454c7af | 1064 | |
7454c7af | 1065 | if (func != 0) { |
9b7d9284 | 1066 | snprintf(nodename, FDT_NAME_MAX, "pci@%x,%x", slot, func); |
7454c7af | 1067 | } else { |
9b7d9284 | 1068 | snprintf(nodename, FDT_NAME_MAX, "pci@%x", slot); |
7454c7af | 1069 | } |
1d2d9742 | 1070 | offset = fdt_add_subnode(fdt, node_offset, nodename); |
e634b89c ND |
1071 | ret = spapr_populate_pci_child_dt(dev, fdt, offset, phb); |
1072 | ||
7454c7af | 1073 | g_assert(!ret); |
1d2d9742 ND |
1074 | if (ret) { |
1075 | return 0; | |
1076 | } | |
1077 | return offset; | |
7454c7af MR |
1078 | } |
1079 | ||
1080 | static void spapr_phb_add_pci_device(sPAPRDRConnector *drc, | |
1081 | sPAPRPHBState *phb, | |
1082 | PCIDevice *pdev, | |
1083 | Error **errp) | |
1084 | { | |
1085 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
1086 | DeviceState *dev = DEVICE(pdev); | |
7454c7af | 1087 | void *fdt = NULL; |
1d2d9742 | 1088 | int fdt_start_offset = 0, fdt_size; |
7454c7af | 1089 | |
185181f8 DG |
1090 | if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { |
1091 | sPAPRTCETable *tcet = spapr_tce_find_by_liobn(phb->dma_liobn); | |
1092 | ||
1093 | spapr_tce_set_need_vfio(tcet, true); | |
1094 | } | |
1095 | ||
7454c7af | 1096 | if (dev->hotplugged) { |
1d2d9742 | 1097 | fdt = create_device_tree(&fdt_size); |
e634b89c | 1098 | fdt_start_offset = spapr_create_pci_child_dt(phb, pdev, fdt, 0); |
1d2d9742 ND |
1099 | if (!fdt_start_offset) { |
1100 | error_setg(errp, "Failed to create pci child device tree node"); | |
1101 | goto out; | |
1102 | } | |
7454c7af MR |
1103 | } |
1104 | ||
1105 | drck->attach(drc, DEVICE(pdev), | |
1106 | fdt, fdt_start_offset, !dev->hotplugged, errp); | |
1d2d9742 | 1107 | out: |
7454c7af MR |
1108 | if (*errp) { |
1109 | g_free(fdt); | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | static void spapr_phb_remove_pci_device_cb(DeviceState *dev, void *opaque) | |
1114 | { | |
1115 | /* some version guests do not wait for completion of a device | |
1116 | * cleanup (generally done asynchronously by the kernel) before | |
1117 | * signaling to QEMU that the device is safe, but instead sleep | |
1118 | * for some 'safe' period of time. unfortunately on a busy host | |
1119 | * this sleep isn't guaranteed to be long enough, resulting in | |
1120 | * bad things like IRQ lines being left asserted during final | |
1121 | * device removal. to deal with this we call reset just prior | |
1122 | * to finalizing the device, which will put the device back into | |
1123 | * an 'idle' state, as the device cleanup code expects. | |
1124 | */ | |
1125 | pci_device_reset(PCI_DEVICE(dev)); | |
1126 | object_unparent(OBJECT(dev)); | |
1127 | } | |
1128 | ||
1129 | static void spapr_phb_remove_pci_device(sPAPRDRConnector *drc, | |
1130 | sPAPRPHBState *phb, | |
1131 | PCIDevice *pdev, | |
1132 | Error **errp) | |
1133 | { | |
1134 | sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
1135 | ||
1136 | drck->detach(drc, DEVICE(pdev), spapr_phb_remove_pci_device_cb, phb, errp); | |
1137 | } | |
1138 | ||
788d2599 MR |
1139 | static sPAPRDRConnector *spapr_phb_get_pci_func_drc(sPAPRPHBState *phb, |
1140 | uint32_t busnr, | |
1141 | int32_t devfn) | |
7454c7af | 1142 | { |
7454c7af MR |
1143 | return spapr_dr_connector_by_id(SPAPR_DR_CONNECTOR_TYPE_PCI, |
1144 | (phb->index << 16) | | |
1145 | (busnr << 8) | | |
788d2599 MR |
1146 | devfn); |
1147 | } | |
1148 | ||
1149 | static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb, | |
1150 | PCIDevice *pdev) | |
1151 | { | |
1152 | uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)))); | |
1153 | return spapr_phb_get_pci_func_drc(phb, busnr, pdev->devfn); | |
7454c7af MR |
1154 | } |
1155 | ||
1d2d9742 ND |
1156 | static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb, |
1157 | PCIDevice *pdev) | |
1158 | { | |
1159 | sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev); | |
1160 | sPAPRDRConnectorClass *drck; | |
1161 | ||
1162 | if (!drc) { | |
1163 | return 0; | |
1164 | } | |
1165 | ||
1166 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
1167 | return drck->get_index(drc); | |
1168 | } | |
1169 | ||
7454c7af MR |
1170 | static void spapr_phb_hot_plug_child(HotplugHandler *plug_handler, |
1171 | DeviceState *plugged_dev, Error **errp) | |
1172 | { | |
1173 | sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); | |
1174 | PCIDevice *pdev = PCI_DEVICE(plugged_dev); | |
1175 | sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev); | |
1176 | Error *local_err = NULL; | |
788d2599 MR |
1177 | PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))); |
1178 | uint32_t slotnr = PCI_SLOT(pdev->devfn); | |
7454c7af MR |
1179 | |
1180 | /* if DR is disabled we don't need to do anything in the case of | |
1181 | * hotplug or coldplug callbacks | |
1182 | */ | |
1183 | if (!phb->dr_enabled) { | |
1184 | /* if this is a hotplug operation initiated by the user | |
1185 | * we need to let them know it's not enabled | |
1186 | */ | |
1187 | if (plugged_dev->hotplugged) { | |
c6bd8c70 MA |
1188 | error_setg(errp, QERR_BUS_NO_HOTPLUG, |
1189 | object_get_typename(OBJECT(phb))); | |
7454c7af MR |
1190 | } |
1191 | return; | |
1192 | } | |
1193 | ||
1194 | g_assert(drc); | |
1195 | ||
788d2599 MR |
1196 | /* Following the QEMU convention used for PCIe multifunction |
1197 | * hotplug, we do not allow functions to be hotplugged to a | |
1198 | * slot that already has function 0 present | |
1199 | */ | |
1200 | if (plugged_dev->hotplugged && bus->devices[PCI_DEVFN(slotnr, 0)] && | |
1201 | PCI_FUNC(pdev->devfn) != 0) { | |
1202 | error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s," | |
1203 | " additional functions can no longer be exposed to guest.", | |
1204 | slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name); | |
1205 | return; | |
1206 | } | |
1207 | ||
7454c7af MR |
1208 | spapr_phb_add_pci_device(drc, phb, pdev, &local_err); |
1209 | if (local_err) { | |
1210 | error_propagate(errp, local_err); | |
1211 | return; | |
1212 | } | |
788d2599 MR |
1213 | |
1214 | /* If this is function 0, signal hotplug for all the device functions. | |
1215 | * Otherwise defer sending the hotplug event. | |
1216 | */ | |
1217 | if (plugged_dev->hotplugged && PCI_FUNC(pdev->devfn) == 0) { | |
1218 | int i; | |
1219 | ||
1220 | for (i = 0; i < 8; i++) { | |
1221 | sPAPRDRConnector *func_drc; | |
1222 | sPAPRDRConnectorClass *func_drck; | |
1223 | sPAPRDREntitySense state; | |
1224 | ||
1225 | func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), | |
1226 | PCI_DEVFN(slotnr, i)); | |
1227 | func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); | |
1228 | func_drck->entity_sense(func_drc, &state); | |
1229 | ||
1230 | if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { | |
1231 | spapr_hotplug_req_add_by_index(func_drc); | |
1232 | } | |
1233 | } | |
c5bc152b | 1234 | } |
7454c7af MR |
1235 | } |
1236 | ||
1237 | static void spapr_phb_hot_unplug_child(HotplugHandler *plug_handler, | |
1238 | DeviceState *plugged_dev, Error **errp) | |
1239 | { | |
1240 | sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); | |
1241 | PCIDevice *pdev = PCI_DEVICE(plugged_dev); | |
1242 | sPAPRDRConnectorClass *drck; | |
1243 | sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev); | |
1244 | Error *local_err = NULL; | |
1245 | ||
1246 | if (!phb->dr_enabled) { | |
c6bd8c70 MA |
1247 | error_setg(errp, QERR_BUS_NO_HOTPLUG, |
1248 | object_get_typename(OBJECT(phb))); | |
7454c7af MR |
1249 | return; |
1250 | } | |
1251 | ||
1252 | g_assert(drc); | |
1253 | ||
1254 | drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); | |
1255 | if (!drck->release_pending(drc)) { | |
788d2599 MR |
1256 | PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))); |
1257 | uint32_t slotnr = PCI_SLOT(pdev->devfn); | |
1258 | sPAPRDRConnector *func_drc; | |
1259 | sPAPRDRConnectorClass *func_drck; | |
1260 | sPAPRDREntitySense state; | |
1261 | int i; | |
1262 | ||
1263 | /* ensure any other present functions are pending unplug */ | |
1264 | if (PCI_FUNC(pdev->devfn) == 0) { | |
1265 | for (i = 1; i < 8; i++) { | |
1266 | func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), | |
1267 | PCI_DEVFN(slotnr, i)); | |
1268 | func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); | |
1269 | func_drck->entity_sense(func_drc, &state); | |
1270 | if (state == SPAPR_DR_ENTITY_SENSE_PRESENT | |
1271 | && !func_drck->release_pending(func_drc)) { | |
1272 | error_setg(errp, | |
1273 | "PCI: slot %d, function %d still present. " | |
1274 | "Must unplug all non-0 functions first.", | |
1275 | slotnr, i); | |
1276 | return; | |
1277 | } | |
1278 | } | |
1279 | } | |
1280 | ||
7454c7af MR |
1281 | spapr_phb_remove_pci_device(drc, phb, pdev, &local_err); |
1282 | if (local_err) { | |
1283 | error_propagate(errp, local_err); | |
1284 | return; | |
1285 | } | |
788d2599 MR |
1286 | |
1287 | /* if this isn't func 0, defer unplug event. otherwise signal removal | |
1288 | * for all present functions | |
1289 | */ | |
1290 | if (PCI_FUNC(pdev->devfn) == 0) { | |
1291 | for (i = 7; i >= 0; i--) { | |
1292 | func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus), | |
1293 | PCI_DEVFN(slotnr, i)); | |
1294 | func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); | |
1295 | func_drck->entity_sense(func_drc, &state); | |
1296 | if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { | |
1297 | spapr_hotplug_req_remove_by_index(func_drc); | |
1298 | } | |
1299 | } | |
1300 | } | |
7454c7af MR |
1301 | } |
1302 | } | |
1303 | ||
c6ba42f6 | 1304 | static void spapr_phb_realize(DeviceState *dev, Error **errp) |
3384f95c | 1305 | { |
28e02042 | 1306 | sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); |
c6ba42f6 | 1307 | SysBusDevice *s = SYS_BUS_DEVICE(dev); |
8c9f64df | 1308 | sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s); |
8558d942 | 1309 | PCIHostState *phb = PCI_HOST_BRIDGE(s); |
298a9710 DG |
1310 | char *namebuf; |
1311 | int i; | |
3384f95c | 1312 | PCIBus *bus; |
8c46f7ec | 1313 | uint64_t msi_window_size = 4096; |
a36304fd DG |
1314 | sPAPRTCETable *tcet; |
1315 | uint32_t nb_table; | |
3384f95c | 1316 | |
421b1b27 | 1317 | if (sphb->index != (uint32_t)-1) { |
caae58cb DG |
1318 | hwaddr windows_base; |
1319 | ||
421b1b27 DG |
1320 | if ((sphb->buid != (uint64_t)-1) || (sphb->dma_liobn != (uint32_t)-1) |
1321 | || (sphb->mem_win_addr != (hwaddr)-1) | |
1322 | || (sphb->io_win_addr != (hwaddr)-1)) { | |
c6ba42f6 AK |
1323 | error_setg(errp, "Either \"index\" or other parameters must" |
1324 | " be specified for PAPR PHB, not both"); | |
1325 | return; | |
caae58cb DG |
1326 | } |
1327 | ||
3e4ac968 DG |
1328 | if (sphb->index > SPAPR_PCI_MAX_INDEX) { |
1329 | error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)", | |
1330 | SPAPR_PCI_MAX_INDEX); | |
1331 | return; | |
1332 | } | |
1333 | ||
caae58cb | 1334 | sphb->buid = SPAPR_PCI_BASE_BUID + sphb->index; |
c8545818 | 1335 | sphb->dma_liobn = SPAPR_PCI_LIOBN(sphb->index, 0); |
caae58cb DG |
1336 | |
1337 | windows_base = SPAPR_PCI_WINDOW_BASE | |
1338 | + sphb->index * SPAPR_PCI_WINDOW_SPACING; | |
1339 | sphb->mem_win_addr = windows_base + SPAPR_PCI_MMIO_WIN_OFF; | |
1340 | sphb->io_win_addr = windows_base + SPAPR_PCI_IO_WIN_OFF; | |
caae58cb DG |
1341 | } |
1342 | ||
421b1b27 | 1343 | if (sphb->buid == (uint64_t)-1) { |
c6ba42f6 AK |
1344 | error_setg(errp, "BUID not specified for PHB"); |
1345 | return; | |
caae58cb DG |
1346 | } |
1347 | ||
421b1b27 | 1348 | if (sphb->dma_liobn == (uint32_t)-1) { |
c6ba42f6 AK |
1349 | error_setg(errp, "LIOBN not specified for PHB"); |
1350 | return; | |
caae58cb DG |
1351 | } |
1352 | ||
421b1b27 | 1353 | if (sphb->mem_win_addr == (hwaddr)-1) { |
c6ba42f6 AK |
1354 | error_setg(errp, "Memory window address not specified for PHB"); |
1355 | return; | |
caae58cb DG |
1356 | } |
1357 | ||
421b1b27 | 1358 | if (sphb->io_win_addr == (hwaddr)-1) { |
c6ba42f6 AK |
1359 | error_setg(errp, "IO window address not specified for PHB"); |
1360 | return; | |
caae58cb DG |
1361 | } |
1362 | ||
46c5874e | 1363 | if (spapr_pci_find_phb(spapr, sphb->buid)) { |
c6ba42f6 AK |
1364 | error_setg(errp, "PCI host bridges must have unique BUIDs"); |
1365 | return; | |
caae58cb DG |
1366 | } |
1367 | ||
8c9f64df | 1368 | sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid); |
caae58cb | 1369 | |
8c9f64df | 1370 | namebuf = alloca(strlen(sphb->dtbusname) + 32); |
3384f95c | 1371 | |
298a9710 | 1372 | /* Initialize memory regions */ |
8c9f64df | 1373 | sprintf(namebuf, "%s.mmio", sphb->dtbusname); |
92b8e39c | 1374 | memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX); |
3384f95c | 1375 | |
8c9f64df | 1376 | sprintf(namebuf, "%s.mmio-alias", sphb->dtbusname); |
40c5dce9 PB |
1377 | memory_region_init_alias(&sphb->memwindow, OBJECT(sphb), |
1378 | namebuf, &sphb->memspace, | |
8c9f64df AF |
1379 | SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size); |
1380 | memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr, | |
1381 | &sphb->memwindow); | |
3384f95c | 1382 | |
fabe9ee1 | 1383 | /* Initialize IO regions */ |
8c9f64df | 1384 | sprintf(namebuf, "%s.io", sphb->dtbusname); |
40c5dce9 PB |
1385 | memory_region_init(&sphb->iospace, OBJECT(sphb), |
1386 | namebuf, SPAPR_PCI_IO_WIN_SIZE); | |
3384f95c | 1387 | |
a3cfa18e | 1388 | sprintf(namebuf, "%s.io-alias", sphb->dtbusname); |
66aab867 | 1389 | memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf, |
fabe9ee1 | 1390 | &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE); |
8c9f64df | 1391 | memory_region_add_subregion(get_system_memory(), sphb->io_win_addr, |
a3cfa18e | 1392 | &sphb->iowindow); |
1b8601b0 AK |
1393 | |
1394 | bus = pci_register_bus(dev, NULL, | |
8c9f64df AF |
1395 | pci_spapr_set_irq, pci_spapr_map_irq, sphb, |
1396 | &sphb->memspace, &sphb->iospace, | |
60a0e443 | 1397 | PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS); |
8c9f64df | 1398 | phb->bus = bus; |
7454c7af | 1399 | qbus_set_hotplug_handler(BUS(phb->bus), DEVICE(sphb), NULL); |
298a9710 | 1400 | |
cca7fad5 AK |
1401 | /* |
1402 | * Initialize PHB address space. | |
1403 | * By default there will be at least one subregion for default | |
1404 | * 32bit DMA window. | |
1405 | * Later the guest might want to create another DMA window | |
1406 | * which will become another memory subregion. | |
1407 | */ | |
1408 | sprintf(namebuf, "%s.iommu-root", sphb->dtbusname); | |
1409 | ||
1410 | memory_region_init(&sphb->iommu_root, OBJECT(sphb), | |
1411 | namebuf, UINT64_MAX); | |
1412 | address_space_init(&sphb->iommu_as, &sphb->iommu_root, | |
1413 | sphb->dtbusname); | |
1414 | ||
8c46f7ec GK |
1415 | /* |
1416 | * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors, | |
1417 | * we need to allocate some memory to catch those writes coming | |
1418 | * from msi_notify()/msix_notify(). | |
1419 | * As MSIMessage:addr is going to be the same and MSIMessage:data | |
1420 | * is going to be a VIRQ number, 4 bytes of the MSI MR will only | |
1421 | * be used. | |
1422 | * | |
1423 | * For KVM we want to ensure that this memory is a full page so that | |
1424 | * our memory slot is of page size granularity. | |
1425 | */ | |
1426 | #ifdef CONFIG_KVM | |
1427 | if (kvm_enabled()) { | |
1428 | msi_window_size = getpagesize(); | |
1429 | } | |
1430 | #endif | |
1431 | ||
1432 | memory_region_init_io(&sphb->msiwindow, NULL, &spapr_msi_ops, spapr, | |
1433 | "msi", msi_window_size); | |
1434 | memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW, | |
1435 | &sphb->msiwindow); | |
1436 | ||
e00387d5 | 1437 | pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb); |
edded454 | 1438 | |
5cc7a967 AK |
1439 | pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq); |
1440 | ||
8c9f64df | 1441 | QLIST_INSERT_HEAD(&spapr->phbs, sphb, list); |
298a9710 DG |
1442 | |
1443 | /* Initialize the LSI table */ | |
7fb0bd34 | 1444 | for (i = 0; i < PCI_NUM_PINS; i++) { |
a307d594 | 1445 | uint32_t irq; |
a005b3ef | 1446 | Error *local_err = NULL; |
298a9710 | 1447 | |
a005b3ef GK |
1448 | irq = xics_alloc_block(spapr->icp, 0, 1, true, false, &local_err); |
1449 | if (local_err) { | |
1450 | error_propagate(errp, local_err); | |
1451 | error_prepend(errp, "can't allocate LSIs: "); | |
c6ba42f6 | 1452 | return; |
298a9710 DG |
1453 | } |
1454 | ||
8c9f64df | 1455 | sphb->lsi_table[i].irq = irq; |
298a9710 | 1456 | } |
da6ccee4 | 1457 | |
62083979 MR |
1458 | /* allocate connectors for child PCI devices */ |
1459 | if (sphb->dr_enabled) { | |
1460 | for (i = 0; i < PCI_SLOT_MAX * 8; i++) { | |
1461 | spapr_dr_connector_new(OBJECT(phb), | |
1462 | SPAPR_DR_CONNECTOR_TYPE_PCI, | |
1463 | (sphb->index << 16) | i); | |
1464 | } | |
1465 | } | |
1466 | ||
f93caaac | 1467 | nb_table = sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT; |
e28c16f6 | 1468 | tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn, |
3e1a01cb | 1469 | 0, SPAPR_TCE_PAGE_SHIFT, nb_table, false); |
e28c16f6 | 1470 | if (!tcet) { |
da6ccee4 AK |
1471 | error_setg(errp, "Unable to create TCE table for %s", |
1472 | sphb->dtbusname); | |
a36304fd | 1473 | return; |
da6ccee4 | 1474 | } |
cca7fad5 AK |
1475 | |
1476 | /* Register default 32bit DMA window */ | |
f93caaac | 1477 | memory_region_add_subregion(&sphb->iommu_root, sphb->dma_win_addr, |
e28c16f6 | 1478 | spapr_tce_get_iommu(tcet)); |
a36304fd DG |
1479 | |
1480 | sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free); | |
298a9710 DG |
1481 | } |
1482 | ||
e28c16f6 | 1483 | static int spapr_phb_children_reset(Object *child, void *opaque) |
eddeed26 | 1484 | { |
e28c16f6 AK |
1485 | DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE); |
1486 | ||
1487 | if (dev) { | |
1488 | device_reset(dev); | |
1489 | } | |
eddeed26 | 1490 | |
e28c16f6 AK |
1491 | return 0; |
1492 | } | |
1493 | ||
1494 | static void spapr_phb_reset(DeviceState *qdev) | |
1495 | { | |
eddeed26 | 1496 | /* Reset the IOMMU state */ |
e28c16f6 | 1497 | object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL); |
fbb4e983 DG |
1498 | |
1499 | if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) { | |
1500 | spapr_phb_vfio_reset(qdev); | |
1501 | } | |
eddeed26 DG |
1502 | } |
1503 | ||
298a9710 | 1504 | static Property spapr_phb_properties[] = { |
3e4ac968 | 1505 | DEFINE_PROP_UINT32("index", sPAPRPHBState, index, -1), |
c7bcc85d PB |
1506 | DEFINE_PROP_UINT64("buid", sPAPRPHBState, buid, -1), |
1507 | DEFINE_PROP_UINT32("liobn", sPAPRPHBState, dma_liobn, -1), | |
1508 | DEFINE_PROP_UINT64("mem_win_addr", sPAPRPHBState, mem_win_addr, -1), | |
1509 | DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState, mem_win_size, | |
1510 | SPAPR_PCI_MMIO_WIN_SIZE), | |
1511 | DEFINE_PROP_UINT64("io_win_addr", sPAPRPHBState, io_win_addr, -1), | |
1512 | DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState, io_win_size, | |
1513 | SPAPR_PCI_IO_WIN_SIZE), | |
7619c7b0 MR |
1514 | DEFINE_PROP_BOOL("dynamic-reconfiguration", sPAPRPHBState, dr_enabled, |
1515 | true), | |
f93caaac DG |
1516 | /* Default DMA window is 0..1GB */ |
1517 | DEFINE_PROP_UINT64("dma_win_addr", sPAPRPHBState, dma_win_addr, 0), | |
1518 | DEFINE_PROP_UINT64("dma_win_size", sPAPRPHBState, dma_win_size, 0x40000000), | |
298a9710 DG |
1519 | DEFINE_PROP_END_OF_LIST(), |
1520 | }; | |
1521 | ||
1112cf94 DG |
1522 | static const VMStateDescription vmstate_spapr_pci_lsi = { |
1523 | .name = "spapr_pci/lsi", | |
1524 | .version_id = 1, | |
1525 | .minimum_version_id = 1, | |
3aff6c2f | 1526 | .fields = (VMStateField[]) { |
1112cf94 DG |
1527 | VMSTATE_UINT32_EQUAL(irq, struct spapr_pci_lsi), |
1528 | ||
1529 | VMSTATE_END_OF_LIST() | |
1530 | }, | |
1531 | }; | |
1532 | ||
1533 | static const VMStateDescription vmstate_spapr_pci_msi = { | |
9a321e92 | 1534 | .name = "spapr_pci/msi", |
1112cf94 DG |
1535 | .version_id = 1, |
1536 | .minimum_version_id = 1, | |
9a321e92 AK |
1537 | .fields = (VMStateField []) { |
1538 | VMSTATE_UINT32(key, spapr_pci_msi_mig), | |
1539 | VMSTATE_UINT32(value.first_irq, spapr_pci_msi_mig), | |
1540 | VMSTATE_UINT32(value.num, spapr_pci_msi_mig), | |
1112cf94 DG |
1541 | VMSTATE_END_OF_LIST() |
1542 | }, | |
1543 | }; | |
1544 | ||
9a321e92 AK |
1545 | static void spapr_pci_pre_save(void *opaque) |
1546 | { | |
1547 | sPAPRPHBState *sphb = opaque; | |
708414f0 MA |
1548 | GHashTableIter iter; |
1549 | gpointer key, value; | |
1550 | int i; | |
9a321e92 | 1551 | |
012aef07 MA |
1552 | g_free(sphb->msi_devs); |
1553 | sphb->msi_devs = NULL; | |
708414f0 MA |
1554 | sphb->msi_devs_num = g_hash_table_size(sphb->msi); |
1555 | if (!sphb->msi_devs_num) { | |
9a321e92 AK |
1556 | return; |
1557 | } | |
708414f0 | 1558 | sphb->msi_devs = g_malloc(sphb->msi_devs_num * sizeof(spapr_pci_msi_mig)); |
9a321e92 | 1559 | |
708414f0 MA |
1560 | g_hash_table_iter_init(&iter, sphb->msi); |
1561 | for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) { | |
1562 | sphb->msi_devs[i].key = *(uint32_t *) key; | |
1563 | sphb->msi_devs[i].value = *(spapr_pci_msi *) value; | |
1564 | } | |
9a321e92 AK |
1565 | } |
1566 | ||
1567 | static int spapr_pci_post_load(void *opaque, int version_id) | |
1568 | { | |
1569 | sPAPRPHBState *sphb = opaque; | |
1570 | gpointer key, value; | |
1571 | int i; | |
1572 | ||
1573 | for (i = 0; i < sphb->msi_devs_num; ++i) { | |
1574 | key = g_memdup(&sphb->msi_devs[i].key, | |
1575 | sizeof(sphb->msi_devs[i].key)); | |
1576 | value = g_memdup(&sphb->msi_devs[i].value, | |
1577 | sizeof(sphb->msi_devs[i].value)); | |
1578 | g_hash_table_insert(sphb->msi, key, value); | |
1579 | } | |
012aef07 MA |
1580 | g_free(sphb->msi_devs); |
1581 | sphb->msi_devs = NULL; | |
9a321e92 AK |
1582 | sphb->msi_devs_num = 0; |
1583 | ||
1584 | return 0; | |
1585 | } | |
1586 | ||
1112cf94 DG |
1587 | static const VMStateDescription vmstate_spapr_pci = { |
1588 | .name = "spapr_pci", | |
9a321e92 AK |
1589 | .version_id = 2, |
1590 | .minimum_version_id = 2, | |
1591 | .pre_save = spapr_pci_pre_save, | |
1592 | .post_load = spapr_pci_post_load, | |
3aff6c2f | 1593 | .fields = (VMStateField[]) { |
1112cf94 DG |
1594 | VMSTATE_UINT64_EQUAL(buid, sPAPRPHBState), |
1595 | VMSTATE_UINT32_EQUAL(dma_liobn, sPAPRPHBState), | |
1596 | VMSTATE_UINT64_EQUAL(mem_win_addr, sPAPRPHBState), | |
1597 | VMSTATE_UINT64_EQUAL(mem_win_size, sPAPRPHBState), | |
1598 | VMSTATE_UINT64_EQUAL(io_win_addr, sPAPRPHBState), | |
1599 | VMSTATE_UINT64_EQUAL(io_win_size, sPAPRPHBState), | |
1112cf94 DG |
1600 | VMSTATE_STRUCT_ARRAY(lsi_table, sPAPRPHBState, PCI_NUM_PINS, 0, |
1601 | vmstate_spapr_pci_lsi, struct spapr_pci_lsi), | |
9a321e92 AK |
1602 | VMSTATE_INT32(msi_devs_num, sPAPRPHBState), |
1603 | VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, sPAPRPHBState, msi_devs_num, 0, | |
1604 | vmstate_spapr_pci_msi, spapr_pci_msi_mig), | |
1112cf94 DG |
1605 | VMSTATE_END_OF_LIST() |
1606 | }, | |
1607 | }; | |
1608 | ||
568f0690 DG |
1609 | static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge, |
1610 | PCIBus *rootbus) | |
1611 | { | |
1612 | sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge); | |
1613 | ||
1614 | return sphb->dtbusname; | |
1615 | } | |
1616 | ||
298a9710 DG |
1617 | static void spapr_phb_class_init(ObjectClass *klass, void *data) |
1618 | { | |
568f0690 | 1619 | PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass); |
298a9710 | 1620 | DeviceClass *dc = DEVICE_CLASS(klass); |
7454c7af | 1621 | HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass); |
298a9710 | 1622 | |
568f0690 | 1623 | hc->root_bus_path = spapr_phb_root_bus_path; |
c6ba42f6 | 1624 | dc->realize = spapr_phb_realize; |
298a9710 | 1625 | dc->props = spapr_phb_properties; |
eddeed26 | 1626 | dc->reset = spapr_phb_reset; |
1112cf94 | 1627 | dc->vmsd = &vmstate_spapr_pci; |
09aa9a52 AK |
1628 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
1629 | dc->cannot_instantiate_with_device_add_yet = false; | |
7454c7af MR |
1630 | hp->plug = spapr_phb_hot_plug_child; |
1631 | hp->unplug = spapr_phb_hot_unplug_child; | |
298a9710 | 1632 | } |
3384f95c | 1633 | |
4240abff | 1634 | static const TypeInfo spapr_phb_info = { |
8c9f64df | 1635 | .name = TYPE_SPAPR_PCI_HOST_BRIDGE, |
8558d942 | 1636 | .parent = TYPE_PCI_HOST_BRIDGE, |
298a9710 DG |
1637 | .instance_size = sizeof(sPAPRPHBState), |
1638 | .class_init = spapr_phb_class_init, | |
7454c7af MR |
1639 | .interfaces = (InterfaceInfo[]) { |
1640 | { TYPE_HOTPLUG_HANDLER }, | |
1641 | { } | |
1642 | } | |
298a9710 DG |
1643 | }; |
1644 | ||
28e02042 | 1645 | PCIHostState *spapr_create_phb(sPAPRMachineState *spapr, int index) |
298a9710 DG |
1646 | { |
1647 | DeviceState *dev; | |
1648 | ||
8c9f64df | 1649 | dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE); |
caae58cb | 1650 | qdev_prop_set_uint32(dev, "index", index); |
298a9710 | 1651 | qdev_init_nofail(dev); |
caae58cb DG |
1652 | |
1653 | return PCI_HOST_BRIDGE(dev); | |
3384f95c DG |
1654 | } |
1655 | ||
1d2d9742 ND |
1656 | typedef struct sPAPRFDT { |
1657 | void *fdt; | |
1658 | int node_off; | |
1659 | sPAPRPHBState *sphb; | |
1660 | } sPAPRFDT; | |
1661 | ||
1662 | static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev, | |
1663 | void *opaque) | |
1664 | { | |
1665 | PCIBus *sec_bus; | |
1666 | sPAPRFDT *p = opaque; | |
1667 | int offset; | |
1668 | sPAPRFDT s_fdt; | |
1d2d9742 | 1669 | |
e634b89c | 1670 | offset = spapr_create_pci_child_dt(p->sphb, pdev, p->fdt, p->node_off); |
1d2d9742 ND |
1671 | if (!offset) { |
1672 | error_report("Failed to create pci child device tree node"); | |
1673 | return; | |
1674 | } | |
1675 | ||
1676 | if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) != | |
1677 | PCI_HEADER_TYPE_BRIDGE)) { | |
1678 | return; | |
1679 | } | |
1680 | ||
1681 | sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); | |
1682 | if (!sec_bus) { | |
1683 | return; | |
1684 | } | |
1685 | ||
1686 | s_fdt.fdt = p->fdt; | |
1687 | s_fdt.node_off = offset; | |
1688 | s_fdt.sphb = p->sphb; | |
1689 | pci_for_each_device(sec_bus, pci_bus_num(sec_bus), | |
1690 | spapr_populate_pci_devices_dt, | |
1691 | &s_fdt); | |
1692 | } | |
1693 | ||
1694 | static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev, | |
1695 | void *opaque) | |
1696 | { | |
1697 | unsigned int *bus_no = opaque; | |
1698 | unsigned int primary = *bus_no; | |
1699 | unsigned int subordinate = 0xff; | |
1700 | PCIBus *sec_bus = NULL; | |
1701 | ||
1702 | if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) != | |
1703 | PCI_HEADER_TYPE_BRIDGE)) { | |
1704 | return; | |
1705 | } | |
1706 | ||
1707 | (*bus_no)++; | |
1708 | pci_default_write_config(pdev, PCI_PRIMARY_BUS, primary, 1); | |
1709 | pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1); | |
1710 | pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1); | |
1711 | ||
1712 | sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); | |
1713 | if (!sec_bus) { | |
1714 | return; | |
1715 | } | |
1716 | ||
1717 | pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, subordinate, 1); | |
1718 | pci_for_each_device(sec_bus, pci_bus_num(sec_bus), | |
1719 | spapr_phb_pci_enumerate_bridge, bus_no); | |
1720 | pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1); | |
1721 | } | |
1722 | ||
1723 | static void spapr_phb_pci_enumerate(sPAPRPHBState *phb) | |
1724 | { | |
1725 | PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus; | |
1726 | unsigned int bus_no = 0; | |
1727 | ||
1728 | pci_for_each_device(bus, pci_bus_num(bus), | |
1729 | spapr_phb_pci_enumerate_bridge, | |
1730 | &bus_no); | |
1731 | ||
1732 | } | |
1733 | ||
e0fdbd7c AK |
1734 | int spapr_populate_pci_dt(sPAPRPHBState *phb, |
1735 | uint32_t xics_phandle, | |
1736 | void *fdt) | |
3384f95c | 1737 | { |
62083979 | 1738 | int bus_off, i, j, ret; |
9b7d9284 | 1739 | char nodename[FDT_NAME_MAX]; |
3384f95c | 1740 | uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) }; |
b194df47 AK |
1741 | const uint64_t mmiosize = memory_region_size(&phb->memwindow); |
1742 | const uint64_t w32max = (1ULL << 32) - SPAPR_PCI_MEM_WIN_BUS_OFFSET; | |
1743 | const uint64_t w32size = MIN(w32max, mmiosize); | |
1744 | const uint64_t w64size = (mmiosize > w32size) ? (mmiosize - w32size) : 0; | |
3384f95c DG |
1745 | struct { |
1746 | uint32_t hi; | |
1747 | uint64_t child; | |
1748 | uint64_t parent; | |
1749 | uint64_t size; | |
c4889f54 | 1750 | } QEMU_PACKED ranges[] = { |
3384f95c DG |
1751 | { |
1752 | cpu_to_be32(b_ss(1)), cpu_to_be64(0), | |
1753 | cpu_to_be64(phb->io_win_addr), | |
1754 | cpu_to_be64(memory_region_size(&phb->iospace)), | |
1755 | }, | |
1756 | { | |
1757 | cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET), | |
1758 | cpu_to_be64(phb->mem_win_addr), | |
b194df47 AK |
1759 | cpu_to_be64(w32size), |
1760 | }, | |
1761 | { | |
1762 | cpu_to_be32(b_ss(3)), cpu_to_be64(1ULL << 32), | |
1763 | cpu_to_be64(phb->mem_win_addr + w32size), | |
1764 | cpu_to_be64(w64size) | |
3384f95c DG |
1765 | }, |
1766 | }; | |
b194df47 | 1767 | const unsigned sizeof_ranges = (w64size ? 3 : 2) * sizeof(ranges[0]); |
3384f95c DG |
1768 | uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 }; |
1769 | uint32_t interrupt_map_mask[] = { | |
7fb0bd34 DG |
1770 | cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)}; |
1771 | uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7]; | |
ccf9ff85 | 1772 | sPAPRTCETable *tcet; |
1d2d9742 ND |
1773 | PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus; |
1774 | sPAPRFDT s_fdt; | |
3384f95c DG |
1775 | |
1776 | /* Start populating the FDT */ | |
9b7d9284 | 1777 | snprintf(nodename, FDT_NAME_MAX, "pci@%" PRIx64, phb->buid); |
3384f95c DG |
1778 | bus_off = fdt_add_subnode(fdt, 0, nodename); |
1779 | if (bus_off < 0) { | |
1780 | return bus_off; | |
1781 | } | |
1782 | ||
3384f95c DG |
1783 | /* Write PHB properties */ |
1784 | _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci")); | |
1785 | _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB")); | |
1786 | _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3)); | |
1787 | _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2)); | |
1788 | _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1)); | |
1789 | _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0)); | |
1790 | _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range))); | |
b194df47 | 1791 | _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges)); |
3384f95c | 1792 | _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg))); |
3f7565c9 | 1793 | _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1)); |
9dbae977 | 1794 | _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", XICS_IRQS)); |
3384f95c | 1795 | |
4d8d5467 BH |
1796 | /* Build the interrupt-map, this must matches what is done |
1797 | * in pci_spapr_map_irq | |
1798 | */ | |
1799 | _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask", | |
1800 | &interrupt_map_mask, sizeof(interrupt_map_mask))); | |
7fb0bd34 DG |
1801 | for (i = 0; i < PCI_SLOT_MAX; i++) { |
1802 | for (j = 0; j < PCI_NUM_PINS; j++) { | |
1803 | uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j]; | |
1804 | int lsi_num = pci_spapr_swizzle(i, j); | |
1805 | ||
1806 | irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0)); | |
1807 | irqmap[1] = 0; | |
1808 | irqmap[2] = 0; | |
1809 | irqmap[3] = cpu_to_be32(j+1); | |
1810 | irqmap[4] = cpu_to_be32(xics_phandle); | |
a307d594 | 1811 | irqmap[5] = cpu_to_be32(phb->lsi_table[lsi_num].irq); |
7fb0bd34 DG |
1812 | irqmap[6] = cpu_to_be32(0x8); |
1813 | } | |
3384f95c | 1814 | } |
3384f95c DG |
1815 | /* Write interrupt map */ |
1816 | _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map, | |
7fb0bd34 | 1817 | sizeof(interrupt_map))); |
3384f95c | 1818 | |
ccf9ff85 | 1819 | tcet = spapr_tce_find_by_liobn(SPAPR_PCI_LIOBN(phb->index, 0)); |
da34fed7 TH |
1820 | if (!tcet) { |
1821 | return -1; | |
1822 | } | |
ccf9ff85 AK |
1823 | spapr_dma_dt(fdt, bus_off, "ibm,dma-window", |
1824 | tcet->liobn, tcet->bus_offset, | |
1825 | tcet->nb_table << tcet->page_shift); | |
edded454 | 1826 | |
1d2d9742 ND |
1827 | /* Walk the bridges and program the bus numbers*/ |
1828 | spapr_phb_pci_enumerate(phb); | |
1829 | _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1)); | |
1830 | ||
1831 | /* Populate tree nodes with PCI devices attached */ | |
1832 | s_fdt.fdt = fdt; | |
1833 | s_fdt.node_off = bus_off; | |
1834 | s_fdt.sphb = phb; | |
1835 | pci_for_each_device(bus, pci_bus_num(bus), | |
1836 | spapr_populate_pci_devices_dt, | |
1837 | &s_fdt); | |
1838 | ||
62083979 MR |
1839 | ret = spapr_drc_populate_dt(fdt, bus_off, OBJECT(phb), |
1840 | SPAPR_DR_CONNECTOR_TYPE_PCI); | |
1841 | if (ret) { | |
1842 | return ret; | |
1843 | } | |
1844 | ||
3384f95c DG |
1845 | return 0; |
1846 | } | |
298a9710 | 1847 | |
fa28f71b AK |
1848 | void spapr_pci_rtas_init(void) |
1849 | { | |
3a3b8502 AK |
1850 | spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config", |
1851 | rtas_read_pci_config); | |
1852 | spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config", | |
1853 | rtas_write_pci_config); | |
1854 | spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config", | |
1855 | rtas_ibm_read_pci_config); | |
1856 | spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config", | |
1857 | rtas_ibm_write_pci_config); | |
226419d6 | 1858 | if (msi_nonbroken) { |
3a3b8502 AK |
1859 | spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER, |
1860 | "ibm,query-interrupt-source-number", | |
0ee2c058 | 1861 | rtas_ibm_query_interrupt_source_number); |
3a3b8502 AK |
1862 | spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi", |
1863 | rtas_ibm_change_msi); | |
0ee2c058 | 1864 | } |
ee954280 GS |
1865 | |
1866 | spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION, | |
1867 | "ibm,set-eeh-option", | |
1868 | rtas_ibm_set_eeh_option); | |
1869 | spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2, | |
1870 | "ibm,get-config-addr-info2", | |
1871 | rtas_ibm_get_config_addr_info2); | |
1872 | spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2, | |
1873 | "ibm,read-slot-reset-state2", | |
1874 | rtas_ibm_read_slot_reset_state2); | |
1875 | spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET, | |
1876 | "ibm,set-slot-reset", | |
1877 | rtas_ibm_set_slot_reset); | |
1878 | spapr_rtas_register(RTAS_IBM_CONFIGURE_PE, | |
1879 | "ibm,configure-pe", | |
1880 | rtas_ibm_configure_pe); | |
1881 | spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL, | |
1882 | "ibm,slot-error-detail", | |
1883 | rtas_ibm_slot_error_detail); | |
fa28f71b AK |
1884 | } |
1885 | ||
8c9f64df | 1886 | static void spapr_pci_register_types(void) |
298a9710 DG |
1887 | { |
1888 | type_register_static(&spapr_phb_info); | |
1889 | } | |
8c9f64df AF |
1890 | |
1891 | type_init(spapr_pci_register_types) | |
eefaccc0 DG |
1892 | |
1893 | static int spapr_switch_one_vga(DeviceState *dev, void *opaque) | |
1894 | { | |
1895 | bool be = *(bool *)opaque; | |
1896 | ||
1897 | if (object_dynamic_cast(OBJECT(dev), "VGA") | |
1898 | || object_dynamic_cast(OBJECT(dev), "secondary-vga")) { | |
1899 | object_property_set_bool(OBJECT(dev), be, "big-endian-framebuffer", | |
1900 | &error_abort); | |
1901 | } | |
1902 | return 0; | |
1903 | } | |
1904 | ||
1905 | void spapr_pci_switch_vga(bool big_endian) | |
1906 | { | |
28e02042 | 1907 | sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); |
eefaccc0 DG |
1908 | sPAPRPHBState *sphb; |
1909 | ||
1910 | /* | |
1911 | * For backward compatibility with existing guests, we switch | |
1912 | * the endianness of the VGA controller when changing the guest | |
1913 | * interrupt mode | |
1914 | */ | |
1915 | QLIST_FOREACH(sphb, &spapr->phbs, list) { | |
1916 | BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus; | |
1917 | qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL, | |
1918 | &big_endian); | |
1919 | } | |
1920 | } |