Statistics
| Branch: | Revision:

root / hw / ppc / spapr_pci.c @ 84af6d9f

History | View | Annotate | Download (27.2 kB)

1 3384f95c David Gibson
/*
2 3384f95c David Gibson
 * QEMU sPAPR PCI host originated from Uninorth PCI host
3 3384f95c David Gibson
 *
4 3384f95c David Gibson
 * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
5 3384f95c David Gibson
 * Copyright (C) 2011 David Gibson, IBM Corporation.
6 3384f95c David Gibson
 *
7 3384f95c David Gibson
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 3384f95c David Gibson
 * of this software and associated documentation files (the "Software"), to deal
9 3384f95c David Gibson
 * in the Software without restriction, including without limitation the rights
10 3384f95c David Gibson
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 3384f95c David Gibson
 * copies of the Software, and to permit persons to whom the Software is
12 3384f95c David Gibson
 * furnished to do so, subject to the following conditions:
13 3384f95c David Gibson
 *
14 3384f95c David Gibson
 * The above copyright notice and this permission notice shall be included in
15 3384f95c David Gibson
 * all copies or substantial portions of the Software.
16 3384f95c David Gibson
 *
17 3384f95c David Gibson
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 3384f95c David Gibson
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 3384f95c David Gibson
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 3384f95c David Gibson
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 3384f95c David Gibson
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 3384f95c David Gibson
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 3384f95c David Gibson
 * THE SOFTWARE.
24 3384f95c David Gibson
 */
25 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
26 83c9f4ca Paolo Bonzini
#include "hw/pci/pci.h"
27 83c9f4ca Paolo Bonzini
#include "hw/pci/msi.h"
28 83c9f4ca Paolo Bonzini
#include "hw/pci/msix.h"
29 83c9f4ca Paolo Bonzini
#include "hw/pci/pci_host.h"
30 0d09e41a Paolo Bonzini
#include "hw/ppc/spapr.h"
31 0d09e41a Paolo Bonzini
#include "hw/pci-host/spapr.h"
32 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
33 3384f95c David Gibson
#include <libfdt.h>
34 a2950fb6 Alexey Kardashevskiy
#include "trace.h"
35 3384f95c David Gibson
36 06aac7bd Michael S. Tsirkin
#include "hw/pci/pci_bus.h"
37 3384f95c David Gibson
38 0ee2c058 Alexey Kardashevskiy
/* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
39 0ee2c058 Alexey Kardashevskiy
#define RTAS_QUERY_FN           0
40 0ee2c058 Alexey Kardashevskiy
#define RTAS_CHANGE_FN          1
41 0ee2c058 Alexey Kardashevskiy
#define RTAS_RESET_FN           2
42 0ee2c058 Alexey Kardashevskiy
#define RTAS_CHANGE_MSI_FN      3
43 0ee2c058 Alexey Kardashevskiy
#define RTAS_CHANGE_MSIX_FN     4
44 0ee2c058 Alexey Kardashevskiy
45 0ee2c058 Alexey Kardashevskiy
/* Interrupt types to return on RTAS_CHANGE_* */
46 0ee2c058 Alexey Kardashevskiy
#define RTAS_TYPE_MSI           1
47 0ee2c058 Alexey Kardashevskiy
#define RTAS_TYPE_MSIX          2
48 0ee2c058 Alexey Kardashevskiy
49 9894c5d4 Alexey Kardashevskiy
static sPAPRPHBState *find_phb(sPAPREnvironment *spapr, uint64_t buid)
50 3384f95c David Gibson
{
51 8c9f64df Andreas Färber
    sPAPRPHBState *sphb;
52 3384f95c David Gibson
53 8c9f64df Andreas Färber
    QLIST_FOREACH(sphb, &spapr->phbs, list) {
54 8c9f64df Andreas Färber
        if (sphb->buid != buid) {
55 3384f95c David Gibson
            continue;
56 3384f95c David Gibson
        }
57 8c9f64df Andreas Färber
        return sphb;
58 9894c5d4 Alexey Kardashevskiy
    }
59 9894c5d4 Alexey Kardashevskiy
60 9894c5d4 Alexey Kardashevskiy
    return NULL;
61 9894c5d4 Alexey Kardashevskiy
}
62 9894c5d4 Alexey Kardashevskiy
63 9894c5d4 Alexey Kardashevskiy
static PCIDevice *find_dev(sPAPREnvironment *spapr, uint64_t buid,
64 9894c5d4 Alexey Kardashevskiy
                           uint32_t config_addr)
65 9894c5d4 Alexey Kardashevskiy
{
66 8c9f64df Andreas Färber
    sPAPRPHBState *sphb = find_phb(spapr, buid);
67 8558d942 Andreas Färber
    PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
68 8c9f64df Andreas Färber
    BusState *bus = BUS(phb->bus);
69 9894c5d4 Alexey Kardashevskiy
    BusChild *kid;
70 9894c5d4 Alexey Kardashevskiy
    int devfn = (config_addr >> 8) & 0xFF;
71 9894c5d4 Alexey Kardashevskiy
72 9894c5d4 Alexey Kardashevskiy
    if (!phb) {
73 9894c5d4 Alexey Kardashevskiy
        return NULL;
74 9894c5d4 Alexey Kardashevskiy
    }
75 3384f95c David Gibson
76 8c9f64df Andreas Färber
    QTAILQ_FOREACH(kid, &bus->children, sibling) {
77 9894c5d4 Alexey Kardashevskiy
        PCIDevice *dev = (PCIDevice *)kid->child;
78 9894c5d4 Alexey Kardashevskiy
        if (dev->devfn == devfn) {
79 9894c5d4 Alexey Kardashevskiy
            return dev;
80 3384f95c David Gibson
        }
81 3384f95c David Gibson
    }
82 3384f95c David Gibson
83 3384f95c David Gibson
    return NULL;
84 3384f95c David Gibson
}
85 3384f95c David Gibson
86 3f7565c9 Benjamin Herrenschmidt
static uint32_t rtas_pci_cfgaddr(uint32_t arg)
87 3f7565c9 Benjamin Herrenschmidt
{
88 92615a5a David Gibson
    /* This handles the encoding of extended config space addresses */
89 3f7565c9 Benjamin Herrenschmidt
    return ((arg >> 20) & 0xf00) | (arg & 0xff);
90 3f7565c9 Benjamin Herrenschmidt
}
91 3f7565c9 Benjamin Herrenschmidt
92 92615a5a David Gibson
static void finish_read_pci_config(sPAPREnvironment *spapr, uint64_t buid,
93 92615a5a David Gibson
                                   uint32_t addr, uint32_t size,
94 92615a5a David Gibson
                                   target_ulong rets)
95 88045ac5 Alexander Graf
{
96 92615a5a David Gibson
    PCIDevice *pci_dev;
97 92615a5a David Gibson
    uint32_t val;
98 92615a5a David Gibson
99 92615a5a David Gibson
    if ((size != 1) && (size != 2) && (size != 4)) {
100 92615a5a David Gibson
        /* access must be 1, 2 or 4 bytes */
101 92615a5a David Gibson
        rtas_st(rets, 0, -1);
102 92615a5a David Gibson
        return;
103 88045ac5 Alexander Graf
    }
104 88045ac5 Alexander Graf
105 92615a5a David Gibson
    pci_dev = find_dev(spapr, buid, addr);
106 92615a5a David Gibson
    addr = rtas_pci_cfgaddr(addr);
107 92615a5a David Gibson
108 92615a5a David Gibson
    if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
109 92615a5a David Gibson
        /* Access must be to a valid device, within bounds and
110 92615a5a David Gibson
         * naturally aligned */
111 92615a5a David Gibson
        rtas_st(rets, 0, -1);
112 92615a5a David Gibson
        return;
113 88045ac5 Alexander Graf
    }
114 92615a5a David Gibson
115 92615a5a David Gibson
    val = pci_host_config_read_common(pci_dev, addr,
116 92615a5a David Gibson
                                      pci_config_size(pci_dev), size);
117 92615a5a David Gibson
118 92615a5a David Gibson
    rtas_st(rets, 0, 0);
119 92615a5a David Gibson
    rtas_st(rets, 1, val);
120 88045ac5 Alexander Graf
}
121 88045ac5 Alexander Graf
122 210b580b Anthony Liguori
static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
123 3384f95c David Gibson
                                     uint32_t token, uint32_t nargs,
124 3384f95c David Gibson
                                     target_ulong args,
125 3384f95c David Gibson
                                     uint32_t nret, target_ulong rets)
126 3384f95c David Gibson
{
127 92615a5a David Gibson
    uint64_t buid;
128 92615a5a David Gibson
    uint32_t size, addr;
129 3384f95c David Gibson
130 92615a5a David Gibson
    if ((nargs != 4) || (nret != 2)) {
131 3384f95c David Gibson
        rtas_st(rets, 0, -1);
132 3384f95c David Gibson
        return;
133 3384f95c David Gibson
    }
134 92615a5a David Gibson
135 92615a5a David Gibson
    buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
136 3384f95c David Gibson
    size = rtas_ld(args, 3);
137 92615a5a David Gibson
    addr = rtas_ld(args, 0);
138 92615a5a David Gibson
139 92615a5a David Gibson
    finish_read_pci_config(spapr, buid, addr, size, rets);
140 3384f95c David Gibson
}
141 3384f95c David Gibson
142 210b580b Anthony Liguori
static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
143 3384f95c David Gibson
                                 uint32_t token, uint32_t nargs,
144 3384f95c David Gibson
                                 target_ulong args,
145 3384f95c David Gibson
                                 uint32_t nret, target_ulong rets)
146 3384f95c David Gibson
{
147 92615a5a David Gibson
    uint32_t size, addr;
148 3384f95c David Gibson
149 92615a5a David Gibson
    if ((nargs != 2) || (nret != 2)) {
150 3384f95c David Gibson
        rtas_st(rets, 0, -1);
151 3384f95c David Gibson
        return;
152 3384f95c David Gibson
    }
153 92615a5a David Gibson
154 3384f95c David Gibson
    size = rtas_ld(args, 1);
155 92615a5a David Gibson
    addr = rtas_ld(args, 0);
156 92615a5a David Gibson
157 92615a5a David Gibson
    finish_read_pci_config(spapr, 0, addr, size, rets);
158 92615a5a David Gibson
}
159 92615a5a David Gibson
160 92615a5a David Gibson
static void finish_write_pci_config(sPAPREnvironment *spapr, uint64_t buid,
161 92615a5a David Gibson
                                    uint32_t addr, uint32_t size,
162 92615a5a David Gibson
                                    uint32_t val, target_ulong rets)
163 92615a5a David Gibson
{
164 92615a5a David Gibson
    PCIDevice *pci_dev;
165 92615a5a David Gibson
166 92615a5a David Gibson
    if ((size != 1) && (size != 2) && (size != 4)) {
167 92615a5a David Gibson
        /* access must be 1, 2 or 4 bytes */
168 92615a5a David Gibson
        rtas_st(rets, 0, -1);
169 92615a5a David Gibson
        return;
170 92615a5a David Gibson
    }
171 92615a5a David Gibson
172 92615a5a David Gibson
    pci_dev = find_dev(spapr, buid, addr);
173 92615a5a David Gibson
    addr = rtas_pci_cfgaddr(addr);
174 92615a5a David Gibson
175 92615a5a David Gibson
    if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
176 92615a5a David Gibson
        /* Access must be to a valid device, within bounds and
177 92615a5a David Gibson
         * naturally aligned */
178 92615a5a David Gibson
        rtas_st(rets, 0, -1);
179 92615a5a David Gibson
        return;
180 92615a5a David Gibson
    }
181 92615a5a David Gibson
182 92615a5a David Gibson
    pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
183 92615a5a David Gibson
                                 val, size);
184 92615a5a David Gibson
185 3384f95c David Gibson
    rtas_st(rets, 0, 0);
186 3384f95c David Gibson
}
187 3384f95c David Gibson
188 210b580b Anthony Liguori
static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
189 3384f95c David Gibson
                                      uint32_t token, uint32_t nargs,
190 3384f95c David Gibson
                                      target_ulong args,
191 3384f95c David Gibson
                                      uint32_t nret, target_ulong rets)
192 3384f95c David Gibson
{
193 92615a5a David Gibson
    uint64_t buid;
194 3384f95c David Gibson
    uint32_t val, size, addr;
195 3384f95c David Gibson
196 92615a5a David Gibson
    if ((nargs != 5) || (nret != 1)) {
197 3384f95c David Gibson
        rtas_st(rets, 0, -1);
198 3384f95c David Gibson
        return;
199 3384f95c David Gibson
    }
200 92615a5a David Gibson
201 92615a5a David Gibson
    buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
202 3384f95c David Gibson
    val = rtas_ld(args, 4);
203 3384f95c David Gibson
    size = rtas_ld(args, 3);
204 92615a5a David Gibson
    addr = rtas_ld(args, 0);
205 92615a5a David Gibson
206 92615a5a David Gibson
    finish_write_pci_config(spapr, buid, addr, size, val, rets);
207 3384f95c David Gibson
}
208 3384f95c David Gibson
209 210b580b Anthony Liguori
static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPREnvironment *spapr,
210 3384f95c David Gibson
                                  uint32_t token, uint32_t nargs,
211 3384f95c David Gibson
                                  target_ulong args,
212 3384f95c David Gibson
                                  uint32_t nret, target_ulong rets)
213 3384f95c David Gibson
{
214 3384f95c David Gibson
    uint32_t val, size, addr;
215 3384f95c David Gibson
216 92615a5a David Gibson
    if ((nargs != 3) || (nret != 1)) {
217 3384f95c David Gibson
        rtas_st(rets, 0, -1);
218 3384f95c David Gibson
        return;
219 3384f95c David Gibson
    }
220 92615a5a David Gibson
221 92615a5a David Gibson
222 3384f95c David Gibson
    val = rtas_ld(args, 2);
223 3384f95c David Gibson
    size = rtas_ld(args, 1);
224 92615a5a David Gibson
    addr = rtas_ld(args, 0);
225 92615a5a David Gibson
226 92615a5a David Gibson
    finish_write_pci_config(spapr, 0, addr, size, val, rets);
227 3384f95c David Gibson
}
228 3384f95c David Gibson
229 0ee2c058 Alexey Kardashevskiy
/*
230 0ee2c058 Alexey Kardashevskiy
 * Find an entry with config_addr or returns the empty one if not found AND
231 0ee2c058 Alexey Kardashevskiy
 * alloc_new is set.
232 0ee2c058 Alexey Kardashevskiy
 * At the moment the msi_table entries are never released so there is
233 0ee2c058 Alexey Kardashevskiy
 * no point to look till the end of the list if we need to find the free entry.
234 0ee2c058 Alexey Kardashevskiy
 */
235 0ee2c058 Alexey Kardashevskiy
static int spapr_msicfg_find(sPAPRPHBState *phb, uint32_t config_addr,
236 0ee2c058 Alexey Kardashevskiy
                             bool alloc_new)
237 0ee2c058 Alexey Kardashevskiy
{
238 0ee2c058 Alexey Kardashevskiy
    int i;
239 0ee2c058 Alexey Kardashevskiy
240 0ee2c058 Alexey Kardashevskiy
    for (i = 0; i < SPAPR_MSIX_MAX_DEVS; ++i) {
241 0ee2c058 Alexey Kardashevskiy
        if (!phb->msi_table[i].nvec) {
242 0ee2c058 Alexey Kardashevskiy
            break;
243 0ee2c058 Alexey Kardashevskiy
        }
244 0ee2c058 Alexey Kardashevskiy
        if (phb->msi_table[i].config_addr == config_addr) {
245 0ee2c058 Alexey Kardashevskiy
            return i;
246 0ee2c058 Alexey Kardashevskiy
        }
247 0ee2c058 Alexey Kardashevskiy
    }
248 0ee2c058 Alexey Kardashevskiy
    if ((i < SPAPR_MSIX_MAX_DEVS) && alloc_new) {
249 0ee2c058 Alexey Kardashevskiy
        trace_spapr_pci_msi("Allocating new MSI config", i, config_addr);
250 0ee2c058 Alexey Kardashevskiy
        return i;
251 0ee2c058 Alexey Kardashevskiy
    }
252 0ee2c058 Alexey Kardashevskiy
253 0ee2c058 Alexey Kardashevskiy
    return -1;
254 0ee2c058 Alexey Kardashevskiy
}
255 0ee2c058 Alexey Kardashevskiy
256 0ee2c058 Alexey Kardashevskiy
/*
257 0ee2c058 Alexey Kardashevskiy
 * Set MSI/MSIX message data.
258 0ee2c058 Alexey Kardashevskiy
 * This is required for msi_notify()/msix_notify() which
259 0ee2c058 Alexey Kardashevskiy
 * will write at the addresses via spapr_msi_write().
260 0ee2c058 Alexey Kardashevskiy
 */
261 a8170e5e Avi Kivity
static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr,
262 0ee2c058 Alexey Kardashevskiy
                             bool msix, unsigned req_num)
263 0ee2c058 Alexey Kardashevskiy
{
264 0ee2c058 Alexey Kardashevskiy
    unsigned i;
265 0ee2c058 Alexey Kardashevskiy
    MSIMessage msg = { .address = addr, .data = 0 };
266 0ee2c058 Alexey Kardashevskiy
267 0ee2c058 Alexey Kardashevskiy
    if (!msix) {
268 0ee2c058 Alexey Kardashevskiy
        msi_set_message(pdev, msg);
269 0ee2c058 Alexey Kardashevskiy
        trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
270 0ee2c058 Alexey Kardashevskiy
        return;
271 0ee2c058 Alexey Kardashevskiy
    }
272 0ee2c058 Alexey Kardashevskiy
273 0ee2c058 Alexey Kardashevskiy
    for (i = 0; i < req_num; ++i) {
274 0ee2c058 Alexey Kardashevskiy
        msg.address = addr | (i << 2);
275 0ee2c058 Alexey Kardashevskiy
        msix_set_message(pdev, i, msg);
276 0ee2c058 Alexey Kardashevskiy
        trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
277 0ee2c058 Alexey Kardashevskiy
    }
278 0ee2c058 Alexey Kardashevskiy
}
279 0ee2c058 Alexey Kardashevskiy
280 210b580b Anthony Liguori
static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPREnvironment *spapr,
281 0ee2c058 Alexey Kardashevskiy
                                uint32_t token, uint32_t nargs,
282 0ee2c058 Alexey Kardashevskiy
                                target_ulong args, uint32_t nret,
283 0ee2c058 Alexey Kardashevskiy
                                target_ulong rets)
284 0ee2c058 Alexey Kardashevskiy
{
285 0ee2c058 Alexey Kardashevskiy
    uint32_t config_addr = rtas_ld(args, 0);
286 0ee2c058 Alexey Kardashevskiy
    uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
287 0ee2c058 Alexey Kardashevskiy
    unsigned int func = rtas_ld(args, 3);
288 0ee2c058 Alexey Kardashevskiy
    unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
289 0ee2c058 Alexey Kardashevskiy
    unsigned int seq_num = rtas_ld(args, 5);
290 0ee2c058 Alexey Kardashevskiy
    unsigned int ret_intr_type;
291 0ee2c058 Alexey Kardashevskiy
    int ndev, irq;
292 0ee2c058 Alexey Kardashevskiy
    sPAPRPHBState *phb = NULL;
293 0ee2c058 Alexey Kardashevskiy
    PCIDevice *pdev = NULL;
294 0ee2c058 Alexey Kardashevskiy
295 0ee2c058 Alexey Kardashevskiy
    switch (func) {
296 0ee2c058 Alexey Kardashevskiy
    case RTAS_CHANGE_MSI_FN:
297 0ee2c058 Alexey Kardashevskiy
    case RTAS_CHANGE_FN:
298 0ee2c058 Alexey Kardashevskiy
        ret_intr_type = RTAS_TYPE_MSI;
299 0ee2c058 Alexey Kardashevskiy
        break;
300 0ee2c058 Alexey Kardashevskiy
    case RTAS_CHANGE_MSIX_FN:
301 0ee2c058 Alexey Kardashevskiy
        ret_intr_type = RTAS_TYPE_MSIX;
302 0ee2c058 Alexey Kardashevskiy
        break;
303 0ee2c058 Alexey Kardashevskiy
    default:
304 0ee2c058 Alexey Kardashevskiy
        fprintf(stderr, "rtas_ibm_change_msi(%u) is not implemented\n", func);
305 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -3); /* Parameter error */
306 0ee2c058 Alexey Kardashevskiy
        return;
307 0ee2c058 Alexey Kardashevskiy
    }
308 0ee2c058 Alexey Kardashevskiy
309 0ee2c058 Alexey Kardashevskiy
    /* Fins sPAPRPHBState */
310 0ee2c058 Alexey Kardashevskiy
    phb = find_phb(spapr, buid);
311 0ee2c058 Alexey Kardashevskiy
    if (phb) {
312 0ee2c058 Alexey Kardashevskiy
        pdev = find_dev(spapr, buid, config_addr);
313 0ee2c058 Alexey Kardashevskiy
    }
314 0ee2c058 Alexey Kardashevskiy
    if (!phb || !pdev) {
315 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -3); /* Parameter error */
316 0ee2c058 Alexey Kardashevskiy
        return;
317 0ee2c058 Alexey Kardashevskiy
    }
318 0ee2c058 Alexey Kardashevskiy
319 0ee2c058 Alexey Kardashevskiy
    /* Releasing MSIs */
320 0ee2c058 Alexey Kardashevskiy
    if (!req_num) {
321 0ee2c058 Alexey Kardashevskiy
        ndev = spapr_msicfg_find(phb, config_addr, false);
322 0ee2c058 Alexey Kardashevskiy
        if (ndev < 0) {
323 0ee2c058 Alexey Kardashevskiy
            trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
324 0ee2c058 Alexey Kardashevskiy
            rtas_st(rets, 0, -1); /* Hardware error */
325 0ee2c058 Alexey Kardashevskiy
            return;
326 0ee2c058 Alexey Kardashevskiy
        }
327 0ee2c058 Alexey Kardashevskiy
        trace_spapr_pci_msi("Released MSIs", ndev, config_addr);
328 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, 0);
329 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 1, 0);
330 0ee2c058 Alexey Kardashevskiy
        return;
331 0ee2c058 Alexey Kardashevskiy
    }
332 0ee2c058 Alexey Kardashevskiy
333 0ee2c058 Alexey Kardashevskiy
    /* Enabling MSI */
334 0ee2c058 Alexey Kardashevskiy
335 0ee2c058 Alexey Kardashevskiy
    /* Find a device number in the map to add or reuse the existing one */
336 0ee2c058 Alexey Kardashevskiy
    ndev = spapr_msicfg_find(phb, config_addr, true);
337 0ee2c058 Alexey Kardashevskiy
    if (ndev >= SPAPR_MSIX_MAX_DEVS || ndev < 0) {
338 0ee2c058 Alexey Kardashevskiy
        fprintf(stderr, "No free entry for a new MSI device\n");
339 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -1); /* Hardware error */
340 0ee2c058 Alexey Kardashevskiy
        return;
341 0ee2c058 Alexey Kardashevskiy
    }
342 0ee2c058 Alexey Kardashevskiy
    trace_spapr_pci_msi("Configuring MSI", ndev, config_addr);
343 0ee2c058 Alexey Kardashevskiy
344 0ee2c058 Alexey Kardashevskiy
    /* Check if there is an old config and MSI number has not changed */
345 0ee2c058 Alexey Kardashevskiy
    if (phb->msi_table[ndev].nvec && (req_num != phb->msi_table[ndev].nvec)) {
346 0ee2c058 Alexey Kardashevskiy
        /* Unexpected behaviour */
347 0ee2c058 Alexey Kardashevskiy
        fprintf(stderr, "Cannot reuse MSI config for device#%d", ndev);
348 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -1); /* Hardware error */
349 0ee2c058 Alexey Kardashevskiy
        return;
350 0ee2c058 Alexey Kardashevskiy
    }
351 0ee2c058 Alexey Kardashevskiy
352 0ee2c058 Alexey Kardashevskiy
    /* There is no cached config, allocate MSIs */
353 0ee2c058 Alexey Kardashevskiy
    if (!phb->msi_table[ndev].nvec) {
354 70c68cf6 Alexey Kardashevskiy
        irq = spapr_allocate_irq_block(req_num, false);
355 0ee2c058 Alexey Kardashevskiy
        if (irq < 0) {
356 0ee2c058 Alexey Kardashevskiy
            fprintf(stderr, "Cannot allocate MSIs for device#%d", ndev);
357 0ee2c058 Alexey Kardashevskiy
            rtas_st(rets, 0, -1); /* Hardware error */
358 0ee2c058 Alexey Kardashevskiy
            return;
359 0ee2c058 Alexey Kardashevskiy
        }
360 0ee2c058 Alexey Kardashevskiy
        phb->msi_table[ndev].irq = irq;
361 0ee2c058 Alexey Kardashevskiy
        phb->msi_table[ndev].nvec = req_num;
362 0ee2c058 Alexey Kardashevskiy
        phb->msi_table[ndev].config_addr = config_addr;
363 0ee2c058 Alexey Kardashevskiy
    }
364 0ee2c058 Alexey Kardashevskiy
365 0ee2c058 Alexey Kardashevskiy
    /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
366 0ee2c058 Alexey Kardashevskiy
    spapr_msi_setmsg(pdev, phb->msi_win_addr | (ndev << 16),
367 0ee2c058 Alexey Kardashevskiy
                     ret_intr_type == RTAS_TYPE_MSIX, req_num);
368 0ee2c058 Alexey Kardashevskiy
369 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 0, 0);
370 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 1, req_num);
371 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 2, ++seq_num);
372 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 3, ret_intr_type);
373 0ee2c058 Alexey Kardashevskiy
374 0ee2c058 Alexey Kardashevskiy
    trace_spapr_pci_rtas_ibm_change_msi(func, req_num);
375 0ee2c058 Alexey Kardashevskiy
}
376 0ee2c058 Alexey Kardashevskiy
377 210b580b Anthony Liguori
static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
378 210b580b Anthony Liguori
                                                   sPAPREnvironment *spapr,
379 0ee2c058 Alexey Kardashevskiy
                                                   uint32_t token,
380 0ee2c058 Alexey Kardashevskiy
                                                   uint32_t nargs,
381 0ee2c058 Alexey Kardashevskiy
                                                   target_ulong args,
382 0ee2c058 Alexey Kardashevskiy
                                                   uint32_t nret,
383 0ee2c058 Alexey Kardashevskiy
                                                   target_ulong rets)
384 0ee2c058 Alexey Kardashevskiy
{
385 0ee2c058 Alexey Kardashevskiy
    uint32_t config_addr = rtas_ld(args, 0);
386 0ee2c058 Alexey Kardashevskiy
    uint64_t buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
387 0ee2c058 Alexey Kardashevskiy
    unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
388 0ee2c058 Alexey Kardashevskiy
    int ndev;
389 0ee2c058 Alexey Kardashevskiy
    sPAPRPHBState *phb = NULL;
390 0ee2c058 Alexey Kardashevskiy
391 0ee2c058 Alexey Kardashevskiy
    /* Fins sPAPRPHBState */
392 0ee2c058 Alexey Kardashevskiy
    phb = find_phb(spapr, buid);
393 0ee2c058 Alexey Kardashevskiy
    if (!phb) {
394 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -3); /* Parameter error */
395 0ee2c058 Alexey Kardashevskiy
        return;
396 0ee2c058 Alexey Kardashevskiy
    }
397 0ee2c058 Alexey Kardashevskiy
398 0ee2c058 Alexey Kardashevskiy
    /* Find device descriptor and start IRQ */
399 0ee2c058 Alexey Kardashevskiy
    ndev = spapr_msicfg_find(phb, config_addr, false);
400 0ee2c058 Alexey Kardashevskiy
    if (ndev < 0) {
401 0ee2c058 Alexey Kardashevskiy
        trace_spapr_pci_msi("MSI has not been enabled", -1, config_addr);
402 0ee2c058 Alexey Kardashevskiy
        rtas_st(rets, 0, -1); /* Hardware error */
403 0ee2c058 Alexey Kardashevskiy
        return;
404 0ee2c058 Alexey Kardashevskiy
    }
405 0ee2c058 Alexey Kardashevskiy
406 0ee2c058 Alexey Kardashevskiy
    intr_src_num = phb->msi_table[ndev].irq + ioa_intr_num;
407 0ee2c058 Alexey Kardashevskiy
    trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
408 0ee2c058 Alexey Kardashevskiy
                                                           intr_src_num);
409 0ee2c058 Alexey Kardashevskiy
410 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 0, 0);
411 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 1, intr_src_num);
412 0ee2c058 Alexey Kardashevskiy
    rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
413 0ee2c058 Alexey Kardashevskiy
}
414 0ee2c058 Alexey Kardashevskiy
415 7fb0bd34 David Gibson
static int pci_spapr_swizzle(int slot, int pin)
416 7fb0bd34 David Gibson
{
417 7fb0bd34 David Gibson
    return (slot + pin) % PCI_NUM_PINS;
418 7fb0bd34 David Gibson
}
419 7fb0bd34 David Gibson
420 3384f95c David Gibson
static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
421 3384f95c David Gibson
{
422 3384f95c David Gibson
    /*
423 3384f95c David Gibson
     * Here we need to convert pci_dev + irq_num to some unique value
424 7fb0bd34 David Gibson
     * which is less than number of IRQs on the specific bus (4).  We
425 7fb0bd34 David Gibson
     * use standard PCI swizzling, that is (slot number + pin number)
426 7fb0bd34 David Gibson
     * % 4.
427 3384f95c David Gibson
     */
428 7fb0bd34 David Gibson
    return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
429 3384f95c David Gibson
}
430 3384f95c David Gibson
431 3384f95c David Gibson
static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
432 3384f95c David Gibson
{
433 3384f95c David Gibson
    /*
434 3384f95c David Gibson
     * Here we use the number returned by pci_spapr_map_irq to find a
435 3384f95c David Gibson
     * corresponding qemu_irq.
436 3384f95c David Gibson
     */
437 3384f95c David Gibson
    sPAPRPHBState *phb = opaque;
438 3384f95c David Gibson
439 caae58cb David Gibson
    trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
440 a307d594 Alexey Kardashevskiy
    qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
441 3384f95c David Gibson
}
442 3384f95c David Gibson
443 a3cfa18e David Gibson
static uint64_t spapr_io_read(void *opaque, hwaddr addr,
444 a3cfa18e David Gibson
                              unsigned size)
445 a3cfa18e David Gibson
{
446 a3cfa18e David Gibson
    switch (size) {
447 a3cfa18e David Gibson
    case 1:
448 a3cfa18e David Gibson
        return cpu_inb(addr);
449 a3cfa18e David Gibson
    case 2:
450 a3cfa18e David Gibson
        return cpu_inw(addr);
451 a3cfa18e David Gibson
    case 4:
452 a3cfa18e David Gibson
        return cpu_inl(addr);
453 a3cfa18e David Gibson
    }
454 a3cfa18e David Gibson
    assert(0);
455 a3cfa18e David Gibson
}
456 a3cfa18e David Gibson
457 a3cfa18e David Gibson
static void spapr_io_write(void *opaque, hwaddr addr,
458 a3cfa18e David Gibson
                           uint64_t data, unsigned size)
459 a3cfa18e David Gibson
{
460 a3cfa18e David Gibson
    switch (size) {
461 a3cfa18e David Gibson
    case 1:
462 a3cfa18e David Gibson
        cpu_outb(addr, data);
463 a3cfa18e David Gibson
        return;
464 a3cfa18e David Gibson
    case 2:
465 a3cfa18e David Gibson
        cpu_outw(addr, data);
466 a3cfa18e David Gibson
        return;
467 a3cfa18e David Gibson
    case 4:
468 a3cfa18e David Gibson
        cpu_outl(addr, data);
469 a3cfa18e David Gibson
        return;
470 a3cfa18e David Gibson
    }
471 a3cfa18e David Gibson
    assert(0);
472 a3cfa18e David Gibson
}
473 a3cfa18e David Gibson
474 a3cfa18e David Gibson
static const MemoryRegionOps spapr_io_ops = {
475 a3cfa18e David Gibson
    .endianness = DEVICE_LITTLE_ENDIAN,
476 a3cfa18e David Gibson
    .read = spapr_io_read,
477 a3cfa18e David Gibson
    .write = spapr_io_write
478 a3cfa18e David Gibson
};
479 a3cfa18e David Gibson
480 298a9710 David Gibson
/*
481 0ee2c058 Alexey Kardashevskiy
 * MSI/MSIX memory region implementation.
482 0ee2c058 Alexey Kardashevskiy
 * The handler handles both MSI and MSIX.
483 0ee2c058 Alexey Kardashevskiy
 * For MSI-X, the vector number is encoded as a part of the address,
484 0ee2c058 Alexey Kardashevskiy
 * data is set to 0.
485 0ee2c058 Alexey Kardashevskiy
 * For MSI, the vector number is encoded in least bits in data.
486 0ee2c058 Alexey Kardashevskiy
 */
487 a8170e5e Avi Kivity
static void spapr_msi_write(void *opaque, hwaddr addr,
488 0ee2c058 Alexey Kardashevskiy
                            uint64_t data, unsigned size)
489 0ee2c058 Alexey Kardashevskiy
{
490 0ee2c058 Alexey Kardashevskiy
    sPAPRPHBState *phb = opaque;
491 0ee2c058 Alexey Kardashevskiy
    int ndev = addr >> 16;
492 0ee2c058 Alexey Kardashevskiy
    int vec = ((addr & 0xFFFF) >> 2) | data;
493 0ee2c058 Alexey Kardashevskiy
    uint32_t irq = phb->msi_table[ndev].irq + vec;
494 0ee2c058 Alexey Kardashevskiy
495 0ee2c058 Alexey Kardashevskiy
    trace_spapr_pci_msi_write(addr, data, irq);
496 0ee2c058 Alexey Kardashevskiy
497 0ee2c058 Alexey Kardashevskiy
    qemu_irq_pulse(xics_get_qirq(spapr->icp, irq));
498 0ee2c058 Alexey Kardashevskiy
}
499 0ee2c058 Alexey Kardashevskiy
500 0ee2c058 Alexey Kardashevskiy
static const MemoryRegionOps spapr_msi_ops = {
501 0ee2c058 Alexey Kardashevskiy
    /* There is no .read as the read result is undefined by PCI spec */
502 0ee2c058 Alexey Kardashevskiy
    .read = NULL,
503 0ee2c058 Alexey Kardashevskiy
    .write = spapr_msi_write,
504 0ee2c058 Alexey Kardashevskiy
    .endianness = DEVICE_LITTLE_ENDIAN
505 0ee2c058 Alexey Kardashevskiy
};
506 0ee2c058 Alexey Kardashevskiy
507 0ee2c058 Alexey Kardashevskiy
/*
508 298a9710 David Gibson
 * PHB PCI device
509 298a9710 David Gibson
 */
510 e00387d5 Avi Kivity
static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
511 edded454 David Gibson
{
512 edded454 David Gibson
    sPAPRPHBState *phb = opaque;
513 edded454 David Gibson
514 e00387d5 Avi Kivity
    return &phb->iommu_as;
515 edded454 David Gibson
}
516 edded454 David Gibson
517 298a9710 David Gibson
static int spapr_phb_init(SysBusDevice *s)
518 3384f95c David Gibson
{
519 8c9f64df Andreas Färber
    sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
520 8558d942 Andreas Färber
    PCIHostState *phb = PCI_HOST_BRIDGE(s);
521 89dfd6e1 David Gibson
    const char *busname;
522 298a9710 David Gibson
    char *namebuf;
523 298a9710 David Gibson
    int i;
524 3384f95c David Gibson
    PCIBus *bus;
525 3384f95c David Gibson
526 caae58cb David Gibson
    if (sphb->index != -1) {
527 caae58cb David Gibson
        hwaddr windows_base;
528 caae58cb David Gibson
529 caae58cb David Gibson
        if ((sphb->buid != -1) || (sphb->dma_liobn != -1)
530 caae58cb David Gibson
            || (sphb->mem_win_addr != -1)
531 caae58cb David Gibson
            || (sphb->io_win_addr != -1)
532 caae58cb David Gibson
            || (sphb->msi_win_addr != -1)) {
533 caae58cb David Gibson
            fprintf(stderr, "Either \"index\" or other parameters must"
534 caae58cb David Gibson
                    " be specified for PAPR PHB, not both\n");
535 caae58cb David Gibson
            return -1;
536 caae58cb David Gibson
        }
537 caae58cb David Gibson
538 caae58cb David Gibson
        sphb->buid = SPAPR_PCI_BASE_BUID + sphb->index;
539 caae58cb David Gibson
        sphb->dma_liobn = SPAPR_PCI_BASE_LIOBN + sphb->index;
540 caae58cb David Gibson
541 caae58cb David Gibson
        windows_base = SPAPR_PCI_WINDOW_BASE
542 caae58cb David Gibson
            + sphb->index * SPAPR_PCI_WINDOW_SPACING;
543 caae58cb David Gibson
        sphb->mem_win_addr = windows_base + SPAPR_PCI_MMIO_WIN_OFF;
544 caae58cb David Gibson
        sphb->io_win_addr = windows_base + SPAPR_PCI_IO_WIN_OFF;
545 caae58cb David Gibson
        sphb->msi_win_addr = windows_base + SPAPR_PCI_MSI_WIN_OFF;
546 caae58cb David Gibson
    }
547 caae58cb David Gibson
548 caae58cb David Gibson
    if (sphb->buid == -1) {
549 caae58cb David Gibson
        fprintf(stderr, "BUID not specified for PHB\n");
550 caae58cb David Gibson
        return -1;
551 caae58cb David Gibson
    }
552 caae58cb David Gibson
553 caae58cb David Gibson
    if (sphb->dma_liobn == -1) {
554 caae58cb David Gibson
        fprintf(stderr, "LIOBN not specified for PHB\n");
555 caae58cb David Gibson
        return -1;
556 caae58cb David Gibson
    }
557 caae58cb David Gibson
558 caae58cb David Gibson
    if (sphb->mem_win_addr == -1) {
559 caae58cb David Gibson
        fprintf(stderr, "Memory window address not specified for PHB\n");
560 caae58cb David Gibson
        return -1;
561 caae58cb David Gibson
    }
562 caae58cb David Gibson
563 caae58cb David Gibson
    if (sphb->io_win_addr == -1) {
564 caae58cb David Gibson
        fprintf(stderr, "IO window address not specified for PHB\n");
565 caae58cb David Gibson
        return -1;
566 caae58cb David Gibson
    }
567 caae58cb David Gibson
568 caae58cb David Gibson
    if (sphb->msi_win_addr == -1) {
569 caae58cb David Gibson
        fprintf(stderr, "MSI window address not specified for PHB\n");
570 caae58cb David Gibson
        return -1;
571 caae58cb David Gibson
    }
572 caae58cb David Gibson
573 caae58cb David Gibson
    if (find_phb(spapr, sphb->buid)) {
574 caae58cb David Gibson
        fprintf(stderr, "PCI host bridges must have unique BUIDs\n");
575 caae58cb David Gibson
        return -1;
576 caae58cb David Gibson
    }
577 caae58cb David Gibson
578 8c9f64df Andreas Färber
    sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
579 caae58cb David Gibson
580 8c9f64df Andreas Färber
    namebuf = alloca(strlen(sphb->dtbusname) + 32);
581 3384f95c David Gibson
582 298a9710 David Gibson
    /* Initialize memory regions */
583 8c9f64df Andreas Färber
    sprintf(namebuf, "%s.mmio", sphb->dtbusname);
584 2c9b15ca Paolo Bonzini
    memory_region_init(&sphb->memspace, NULL, namebuf, INT64_MAX);
585 3384f95c David Gibson
586 8c9f64df Andreas Färber
    sprintf(namebuf, "%s.mmio-alias", sphb->dtbusname);
587 2c9b15ca Paolo Bonzini
    memory_region_init_alias(&sphb->memwindow, NULL, namebuf, &sphb->memspace,
588 8c9f64df Andreas Färber
                             SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
589 8c9f64df Andreas Färber
    memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
590 8c9f64df Andreas Färber
                                &sphb->memwindow);
591 3384f95c David Gibson
592 3384f95c David Gibson
    /* On ppc, we only have MMIO no specific IO space from the CPU
593 3384f95c David Gibson
     * perspective.  In theory we ought to be able to embed the PCI IO
594 3384f95c David Gibson
     * memory region direction in the system memory space.  However,
595 3384f95c David Gibson
     * if any of the IO BAR subregions use the old_portio mechanism,
596 3384f95c David Gibson
     * that won't be processed properly unless accessed from the
597 3384f95c David Gibson
     * system io address space.  This hack to bounce things via
598 3384f95c David Gibson
     * system_io works around the problem until all the users of
599 3384f95c David Gibson
     * old_portion are updated */
600 8c9f64df Andreas Färber
    sprintf(namebuf, "%s.io", sphb->dtbusname);
601 2c9b15ca Paolo Bonzini
    memory_region_init(&sphb->iospace, NULL, namebuf, SPAPR_PCI_IO_WIN_SIZE);
602 a3cfa18e David Gibson
    /* FIXME: fix to support multiple PHBs */
603 a3cfa18e David Gibson
    memory_region_add_subregion(get_system_io(), 0, &sphb->iospace);
604 3384f95c David Gibson
605 a3cfa18e David Gibson
    sprintf(namebuf, "%s.io-alias", sphb->dtbusname);
606 2c9b15ca Paolo Bonzini
    memory_region_init_io(&sphb->iowindow, NULL, &spapr_io_ops, sphb,
607 a3cfa18e David Gibson
                          namebuf, SPAPR_PCI_IO_WIN_SIZE);
608 8c9f64df Andreas Färber
    memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
609 a3cfa18e David Gibson
                                &sphb->iowindow);
610 3384f95c David Gibson
611 0ee2c058 Alexey Kardashevskiy
    /* As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
612 0ee2c058 Alexey Kardashevskiy
     * we need to allocate some memory to catch those writes coming
613 0ee2c058 Alexey Kardashevskiy
     * from msi_notify()/msix_notify() */
614 0ee2c058 Alexey Kardashevskiy
    if (msi_supported) {
615 8c9f64df Andreas Färber
        sprintf(namebuf, "%s.msi", sphb->dtbusname);
616 2c9b15ca Paolo Bonzini
        memory_region_init_io(&sphb->msiwindow, NULL, &spapr_msi_ops, sphb,
617 0ee2c058 Alexey Kardashevskiy
                              namebuf, SPAPR_MSIX_MAX_DEVS * 0x10000);
618 8c9f64df Andreas Färber
        memory_region_add_subregion(get_system_memory(), sphb->msi_win_addr,
619 8c9f64df Andreas Färber
                                    &sphb->msiwindow);
620 0ee2c058 Alexey Kardashevskiy
    }
621 0ee2c058 Alexey Kardashevskiy
622 89dfd6e1 David Gibson
    /*
623 89dfd6e1 David Gibson
     * Selecting a busname is more complex than you'd think, due to
624 89dfd6e1 David Gibson
     * interacting constraints.  If the user has specified an id
625 89dfd6e1 David Gibson
     * explicitly for the phb , then we want to use the qdev default
626 89dfd6e1 David Gibson
     * of naming the bus based on the bridge device (so the user can
627 89dfd6e1 David Gibson
     * then assign devices to it in the way they expect).  For the
628 89dfd6e1 David Gibson
     * first / default PCI bus (index=0) we want to use just "pci"
629 89dfd6e1 David Gibson
     * because libvirt expects there to be a bus called, simply,
630 89dfd6e1 David Gibson
     * "pci".  Otherwise, we use the same name as in the device tree,
631 89dfd6e1 David Gibson
     * since it's unique by construction, and makes the guest visible
632 89dfd6e1 David Gibson
     * BUID clear.
633 89dfd6e1 David Gibson
     */
634 89dfd6e1 David Gibson
    if (s->qdev.id) {
635 89dfd6e1 David Gibson
        busname = NULL;
636 89dfd6e1 David Gibson
    } else if (sphb->index == 0) {
637 89dfd6e1 David Gibson
        busname = "pci";
638 89dfd6e1 David Gibson
    } else {
639 89dfd6e1 David Gibson
        busname = sphb->dtbusname;
640 89dfd6e1 David Gibson
    }
641 89dfd6e1 David Gibson
    bus = pci_register_bus(DEVICE(s), busname,
642 8c9f64df Andreas Färber
                           pci_spapr_set_irq, pci_spapr_map_irq, sphb,
643 8c9f64df Andreas Färber
                           &sphb->memspace, &sphb->iospace,
644 60a0e443 Alex Williamson
                           PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS);
645 8c9f64df Andreas Färber
    phb->bus = bus;
646 298a9710 David Gibson
647 8c9f64df Andreas Färber
    sphb->dma_window_start = 0;
648 8c9f64df Andreas Färber
    sphb->dma_window_size = 0x40000000;
649 84af6d9f Paolo Bonzini
    sphb->tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn,
650 84af6d9f Paolo Bonzini
                                     sphb->dma_window_size);
651 2b7dc949 Paolo Bonzini
    if (!sphb->tcet) {
652 caae58cb David Gibson
        fprintf(stderr, "Unable to create TCE table for %s\n", sphb->dtbusname);
653 caae58cb David Gibson
        return -1;
654 caae58cb David Gibson
    }
655 7dca8043 Alexey Kardashevskiy
    address_space_init(&sphb->iommu_as, spapr_tce_get_iommu(sphb->tcet),
656 7dca8043 Alexey Kardashevskiy
                       sphb->dtbusname);
657 7dca8043 Alexey Kardashevskiy
658 e00387d5 Avi Kivity
    pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
659 edded454 David Gibson
660 8c9f64df Andreas Färber
    QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
661 298a9710 David Gibson
662 298a9710 David Gibson
    /* Initialize the LSI table */
663 7fb0bd34 David Gibson
    for (i = 0; i < PCI_NUM_PINS; i++) {
664 a307d594 Alexey Kardashevskiy
        uint32_t irq;
665 298a9710 David Gibson
666 a307d594 Alexey Kardashevskiy
        irq = spapr_allocate_lsi(0);
667 a307d594 Alexey Kardashevskiy
        if (!irq) {
668 298a9710 David Gibson
            return -1;
669 298a9710 David Gibson
        }
670 298a9710 David Gibson
671 8c9f64df Andreas Färber
        sphb->lsi_table[i].irq = irq;
672 298a9710 David Gibson
    }
673 298a9710 David Gibson
674 298a9710 David Gibson
    return 0;
675 298a9710 David Gibson
}
676 298a9710 David Gibson
677 eddeed26 David Gibson
static void spapr_phb_reset(DeviceState *qdev)
678 eddeed26 David Gibson
{
679 1356b98d Andreas Färber
    SysBusDevice *s = SYS_BUS_DEVICE(qdev);
680 eddeed26 David Gibson
    sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
681 eddeed26 David Gibson
682 eddeed26 David Gibson
    /* Reset the IOMMU state */
683 2b7dc949 Paolo Bonzini
    spapr_tce_reset(sphb->tcet);
684 eddeed26 David Gibson
}
685 eddeed26 David Gibson
686 298a9710 David Gibson
static Property spapr_phb_properties[] = {
687 caae58cb David Gibson
    DEFINE_PROP_INT32("index", sPAPRPHBState, index, -1),
688 caae58cb David Gibson
    DEFINE_PROP_HEX64("buid", sPAPRPHBState, buid, -1),
689 caae58cb David Gibson
    DEFINE_PROP_HEX32("liobn", sPAPRPHBState, dma_liobn, -1),
690 caae58cb David Gibson
    DEFINE_PROP_HEX64("mem_win_addr", sPAPRPHBState, mem_win_addr, -1),
691 caae58cb David Gibson
    DEFINE_PROP_HEX64("mem_win_size", sPAPRPHBState, mem_win_size,
692 caae58cb David Gibson
                      SPAPR_PCI_MMIO_WIN_SIZE),
693 caae58cb David Gibson
    DEFINE_PROP_HEX64("io_win_addr", sPAPRPHBState, io_win_addr, -1),
694 caae58cb David Gibson
    DEFINE_PROP_HEX64("io_win_size", sPAPRPHBState, io_win_size,
695 caae58cb David Gibson
                      SPAPR_PCI_IO_WIN_SIZE),
696 caae58cb David Gibson
    DEFINE_PROP_HEX64("msi_win_addr", sPAPRPHBState, msi_win_addr, -1),
697 298a9710 David Gibson
    DEFINE_PROP_END_OF_LIST(),
698 298a9710 David Gibson
};
699 298a9710 David Gibson
700 298a9710 David Gibson
static void spapr_phb_class_init(ObjectClass *klass, void *data)
701 298a9710 David Gibson
{
702 298a9710 David Gibson
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
703 298a9710 David Gibson
    DeviceClass *dc = DEVICE_CLASS(klass);
704 298a9710 David Gibson
705 298a9710 David Gibson
    sdc->init = spapr_phb_init;
706 298a9710 David Gibson
    dc->props = spapr_phb_properties;
707 eddeed26 David Gibson
    dc->reset = spapr_phb_reset;
708 298a9710 David Gibson
}
709 3384f95c David Gibson
710 4240abff Andreas Färber
static const TypeInfo spapr_phb_info = {
711 8c9f64df Andreas Färber
    .name          = TYPE_SPAPR_PCI_HOST_BRIDGE,
712 8558d942 Andreas Färber
    .parent        = TYPE_PCI_HOST_BRIDGE,
713 298a9710 David Gibson
    .instance_size = sizeof(sPAPRPHBState),
714 298a9710 David Gibson
    .class_init    = spapr_phb_class_init,
715 298a9710 David Gibson
};
716 298a9710 David Gibson
717 89dfd6e1 David Gibson
PCIHostState *spapr_create_phb(sPAPREnvironment *spapr, int index)
718 298a9710 David Gibson
{
719 298a9710 David Gibson
    DeviceState *dev;
720 298a9710 David Gibson
721 8c9f64df Andreas Färber
    dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
722 caae58cb David Gibson
    qdev_prop_set_uint32(dev, "index", index);
723 298a9710 David Gibson
    qdev_init_nofail(dev);
724 caae58cb David Gibson
725 caae58cb David Gibson
    return PCI_HOST_BRIDGE(dev);
726 3384f95c David Gibson
}
727 3384f95c David Gibson
728 3384f95c David Gibson
/* Macros to operate with address in OF binding to PCI */
729 3384f95c David Gibson
#define b_x(x, p, l)    (((x) & ((1<<(l))-1)) << (p))
730 3384f95c David Gibson
#define b_n(x)          b_x((x), 31, 1) /* 0 if relocatable */
731 3384f95c David Gibson
#define b_p(x)          b_x((x), 30, 1) /* 1 if prefetchable */
732 3384f95c David Gibson
#define b_t(x)          b_x((x), 29, 1) /* 1 if the address is aliased */
733 3384f95c David Gibson
#define b_ss(x)         b_x((x), 24, 2) /* the space code */
734 3384f95c David Gibson
#define b_bbbbbbbb(x)   b_x((x), 16, 8) /* bus number */
735 3384f95c David Gibson
#define b_ddddd(x)      b_x((x), 11, 5) /* device number */
736 3384f95c David Gibson
#define b_fff(x)        b_x((x), 8, 3)  /* function number */
737 3384f95c David Gibson
#define b_rrrrrrrr(x)   b_x((x), 0, 8)  /* register number */
738 3384f95c David Gibson
739 e0fdbd7c Alexey Kardashevskiy
int spapr_populate_pci_dt(sPAPRPHBState *phb,
740 e0fdbd7c Alexey Kardashevskiy
                          uint32_t xics_phandle,
741 e0fdbd7c Alexey Kardashevskiy
                          void *fdt)
742 3384f95c David Gibson
{
743 7fb0bd34 David Gibson
    int bus_off, i, j;
744 3384f95c David Gibson
    char nodename[256];
745 3384f95c David Gibson
    uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
746 3384f95c David Gibson
    struct {
747 3384f95c David Gibson
        uint32_t hi;
748 3384f95c David Gibson
        uint64_t child;
749 3384f95c David Gibson
        uint64_t parent;
750 3384f95c David Gibson
        uint64_t size;
751 c4889f54 Alexey Kardashevskiy
    } QEMU_PACKED ranges[] = {
752 3384f95c David Gibson
        {
753 3384f95c David Gibson
            cpu_to_be32(b_ss(1)), cpu_to_be64(0),
754 3384f95c David Gibson
            cpu_to_be64(phb->io_win_addr),
755 3384f95c David Gibson
            cpu_to_be64(memory_region_size(&phb->iospace)),
756 3384f95c David Gibson
        },
757 3384f95c David Gibson
        {
758 3384f95c David Gibson
            cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
759 3384f95c David Gibson
            cpu_to_be64(phb->mem_win_addr),
760 3384f95c David Gibson
            cpu_to_be64(memory_region_size(&phb->memwindow)),
761 3384f95c David Gibson
        },
762 3384f95c David Gibson
    };
763 3384f95c David Gibson
    uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
764 3384f95c David Gibson
    uint32_t interrupt_map_mask[] = {
765 7fb0bd34 David Gibson
        cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
766 7fb0bd34 David Gibson
    uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
767 3384f95c David Gibson
768 3384f95c David Gibson
    /* Start populating the FDT */
769 3384f95c David Gibson
    sprintf(nodename, "pci@%" PRIx64, phb->buid);
770 3384f95c David Gibson
    bus_off = fdt_add_subnode(fdt, 0, nodename);
771 3384f95c David Gibson
    if (bus_off < 0) {
772 3384f95c David Gibson
        return bus_off;
773 3384f95c David Gibson
    }
774 3384f95c David Gibson
775 3384f95c David Gibson
#define _FDT(exp) \
776 3384f95c David Gibson
    do { \
777 3384f95c David Gibson
        int ret = (exp);                                           \
778 3384f95c David Gibson
        if (ret < 0) {                                             \
779 3384f95c David Gibson
            return ret;                                            \
780 3384f95c David Gibson
        }                                                          \
781 3384f95c David Gibson
    } while (0)
782 3384f95c David Gibson
783 3384f95c David Gibson
    /* Write PHB properties */
784 3384f95c David Gibson
    _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
785 3384f95c David Gibson
    _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
786 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
787 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
788 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
789 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
790 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
791 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges)));
792 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
793 3f7565c9 Benjamin Herrenschmidt
    _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
794 3384f95c David Gibson
795 4d8d5467 Benjamin Herrenschmidt
    /* Build the interrupt-map, this must matches what is done
796 4d8d5467 Benjamin Herrenschmidt
     * in pci_spapr_map_irq
797 4d8d5467 Benjamin Herrenschmidt
     */
798 4d8d5467 Benjamin Herrenschmidt
    _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
799 4d8d5467 Benjamin Herrenschmidt
                     &interrupt_map_mask, sizeof(interrupt_map_mask)));
800 7fb0bd34 David Gibson
    for (i = 0; i < PCI_SLOT_MAX; i++) {
801 7fb0bd34 David Gibson
        for (j = 0; j < PCI_NUM_PINS; j++) {
802 7fb0bd34 David Gibson
            uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
803 7fb0bd34 David Gibson
            int lsi_num = pci_spapr_swizzle(i, j);
804 7fb0bd34 David Gibson
805 7fb0bd34 David Gibson
            irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
806 7fb0bd34 David Gibson
            irqmap[1] = 0;
807 7fb0bd34 David Gibson
            irqmap[2] = 0;
808 7fb0bd34 David Gibson
            irqmap[3] = cpu_to_be32(j+1);
809 7fb0bd34 David Gibson
            irqmap[4] = cpu_to_be32(xics_phandle);
810 a307d594 Alexey Kardashevskiy
            irqmap[5] = cpu_to_be32(phb->lsi_table[lsi_num].irq);
811 7fb0bd34 David Gibson
            irqmap[6] = cpu_to_be32(0x8);
812 7fb0bd34 David Gibson
        }
813 3384f95c David Gibson
    }
814 3384f95c David Gibson
    /* Write interrupt map */
815 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
816 7fb0bd34 David Gibson
                     sizeof(interrupt_map)));
817 3384f95c David Gibson
818 5c4cbcf2 Alexey Kardashevskiy
    spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
819 5c4cbcf2 Alexey Kardashevskiy
                 phb->dma_liobn, phb->dma_window_start,
820 5c4cbcf2 Alexey Kardashevskiy
                 phb->dma_window_size);
821 edded454 David Gibson
822 3384f95c David Gibson
    return 0;
823 3384f95c David Gibson
}
824 298a9710 David Gibson
825 fa28f71b Alexey Kardashevskiy
void spapr_pci_rtas_init(void)
826 fa28f71b Alexey Kardashevskiy
{
827 fa28f71b Alexey Kardashevskiy
    spapr_rtas_register("read-pci-config", rtas_read_pci_config);
828 fa28f71b Alexey Kardashevskiy
    spapr_rtas_register("write-pci-config", rtas_write_pci_config);
829 fa28f71b Alexey Kardashevskiy
    spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config);
830 fa28f71b Alexey Kardashevskiy
    spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config);
831 0ee2c058 Alexey Kardashevskiy
    if (msi_supported) {
832 0ee2c058 Alexey Kardashevskiy
        spapr_rtas_register("ibm,query-interrupt-source-number",
833 0ee2c058 Alexey Kardashevskiy
                            rtas_ibm_query_interrupt_source_number);
834 0ee2c058 Alexey Kardashevskiy
        spapr_rtas_register("ibm,change-msi", rtas_ibm_change_msi);
835 0ee2c058 Alexey Kardashevskiy
    }
836 fa28f71b Alexey Kardashevskiy
}
837 fa28f71b Alexey Kardashevskiy
838 8c9f64df Andreas Färber
static void spapr_pci_register_types(void)
839 298a9710 David Gibson
{
840 298a9710 David Gibson
    type_register_static(&spapr_phb_info);
841 298a9710 David Gibson
}
842 8c9f64df Andreas Färber
843 8c9f64df Andreas Färber
type_init(spapr_pci_register_types)