Statistics
| Branch: | Revision:

root / hw / spapr_pci.c @ 43997225

History | View | Annotate | Download (15.3 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 3384f95c David Gibson
#include "hw.h"
26 3384f95c David Gibson
#include "pci.h"
27 3384f95c David Gibson
#include "pci_host.h"
28 3384f95c David Gibson
#include "hw/spapr.h"
29 3384f95c David Gibson
#include "hw/spapr_pci.h"
30 3384f95c David Gibson
#include "exec-memory.h"
31 3384f95c David Gibson
#include <libfdt.h>
32 3384f95c David Gibson
33 3384f95c David Gibson
#include "hw/pci_internals.h"
34 3384f95c David Gibson
35 3384f95c David Gibson
static PCIDevice *find_dev(sPAPREnvironment *spapr,
36 3384f95c David Gibson
                           uint64_t buid, uint32_t config_addr)
37 3384f95c David Gibson
{
38 3384f95c David Gibson
    DeviceState *qdev;
39 3384f95c David Gibson
    int devfn = (config_addr >> 8) & 0xFF;
40 3384f95c David Gibson
    sPAPRPHBState *phb;
41 3384f95c David Gibson
42 3384f95c David Gibson
    QLIST_FOREACH(phb, &spapr->phbs, list) {
43 3384f95c David Gibson
        if (phb->buid != buid) {
44 3384f95c David Gibson
            continue;
45 3384f95c David Gibson
        }
46 3384f95c David Gibson
47 3a26360d Anthony Liguori
        QTAILQ_FOREACH(qdev, &phb->host_state.bus->qbus.children, sibling) {
48 3384f95c David Gibson
            PCIDevice *dev = (PCIDevice *)qdev;
49 3384f95c David Gibson
            if (dev->devfn == devfn) {
50 3384f95c David Gibson
                return dev;
51 3384f95c David Gibson
            }
52 3384f95c David Gibson
        }
53 3384f95c David Gibson
    }
54 3384f95c David Gibson
55 3384f95c David Gibson
    return NULL;
56 3384f95c David Gibson
}
57 3384f95c David Gibson
58 3f7565c9 Benjamin Herrenschmidt
static uint32_t rtas_pci_cfgaddr(uint32_t arg)
59 3f7565c9 Benjamin Herrenschmidt
{
60 92615a5a David Gibson
    /* This handles the encoding of extended config space addresses */
61 3f7565c9 Benjamin Herrenschmidt
    return ((arg >> 20) & 0xf00) | (arg & 0xff);
62 3f7565c9 Benjamin Herrenschmidt
}
63 3f7565c9 Benjamin Herrenschmidt
64 92615a5a David Gibson
static void finish_read_pci_config(sPAPREnvironment *spapr, uint64_t buid,
65 92615a5a David Gibson
                                   uint32_t addr, uint32_t size,
66 92615a5a David Gibson
                                   target_ulong rets)
67 88045ac5 Alexander Graf
{
68 92615a5a David Gibson
    PCIDevice *pci_dev;
69 92615a5a David Gibson
    uint32_t val;
70 92615a5a David Gibson
71 92615a5a David Gibson
    if ((size != 1) && (size != 2) && (size != 4)) {
72 92615a5a David Gibson
        /* access must be 1, 2 or 4 bytes */
73 92615a5a David Gibson
        rtas_st(rets, 0, -1);
74 92615a5a David Gibson
        return;
75 88045ac5 Alexander Graf
    }
76 88045ac5 Alexander Graf
77 92615a5a David Gibson
    pci_dev = find_dev(spapr, buid, addr);
78 92615a5a David Gibson
    addr = rtas_pci_cfgaddr(addr);
79 92615a5a David Gibson
80 92615a5a David Gibson
    if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
81 92615a5a David Gibson
        /* Access must be to a valid device, within bounds and
82 92615a5a David Gibson
         * naturally aligned */
83 92615a5a David Gibson
        rtas_st(rets, 0, -1);
84 92615a5a David Gibson
        return;
85 88045ac5 Alexander Graf
    }
86 92615a5a David Gibson
87 92615a5a David Gibson
    val = pci_host_config_read_common(pci_dev, addr,
88 92615a5a David Gibson
                                      pci_config_size(pci_dev), size);
89 92615a5a David Gibson
90 92615a5a David Gibson
    rtas_st(rets, 0, 0);
91 92615a5a David Gibson
    rtas_st(rets, 1, val);
92 88045ac5 Alexander Graf
}
93 88045ac5 Alexander Graf
94 3384f95c David Gibson
static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr,
95 3384f95c David Gibson
                                     uint32_t token, uint32_t nargs,
96 3384f95c David Gibson
                                     target_ulong args,
97 3384f95c David Gibson
                                     uint32_t nret, target_ulong rets)
98 3384f95c David Gibson
{
99 92615a5a David Gibson
    uint64_t buid;
100 92615a5a David Gibson
    uint32_t size, addr;
101 3384f95c David Gibson
102 92615a5a David Gibson
    if ((nargs != 4) || (nret != 2)) {
103 3384f95c David Gibson
        rtas_st(rets, 0, -1);
104 3384f95c David Gibson
        return;
105 3384f95c David Gibson
    }
106 92615a5a David Gibson
107 92615a5a David Gibson
    buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
108 3384f95c David Gibson
    size = rtas_ld(args, 3);
109 92615a5a David Gibson
    addr = rtas_ld(args, 0);
110 92615a5a David Gibson
111 92615a5a David Gibson
    finish_read_pci_config(spapr, buid, addr, size, rets);
112 3384f95c David Gibson
}
113 3384f95c David Gibson
114 3384f95c David Gibson
static void rtas_read_pci_config(sPAPREnvironment *spapr,
115 3384f95c David Gibson
                                 uint32_t token, uint32_t nargs,
116 3384f95c David Gibson
                                 target_ulong args,
117 3384f95c David Gibson
                                 uint32_t nret, target_ulong rets)
118 3384f95c David Gibson
{
119 92615a5a David Gibson
    uint32_t size, addr;
120 3384f95c David Gibson
121 92615a5a David Gibson
    if ((nargs != 2) || (nret != 2)) {
122 3384f95c David Gibson
        rtas_st(rets, 0, -1);
123 3384f95c David Gibson
        return;
124 3384f95c David Gibson
    }
125 92615a5a David Gibson
126 3384f95c David Gibson
    size = rtas_ld(args, 1);
127 92615a5a David Gibson
    addr = rtas_ld(args, 0);
128 92615a5a David Gibson
129 92615a5a David Gibson
    finish_read_pci_config(spapr, 0, addr, size, rets);
130 92615a5a David Gibson
}
131 92615a5a David Gibson
132 92615a5a David Gibson
static void finish_write_pci_config(sPAPREnvironment *spapr, uint64_t buid,
133 92615a5a David Gibson
                                    uint32_t addr, uint32_t size,
134 92615a5a David Gibson
                                    uint32_t val, target_ulong rets)
135 92615a5a David Gibson
{
136 92615a5a David Gibson
    PCIDevice *pci_dev;
137 92615a5a David Gibson
138 92615a5a David Gibson
    if ((size != 1) && (size != 2) && (size != 4)) {
139 92615a5a David Gibson
        /* access must be 1, 2 or 4 bytes */
140 92615a5a David Gibson
        rtas_st(rets, 0, -1);
141 92615a5a David Gibson
        return;
142 92615a5a David Gibson
    }
143 92615a5a David Gibson
144 92615a5a David Gibson
    pci_dev = find_dev(spapr, buid, addr);
145 92615a5a David Gibson
    addr = rtas_pci_cfgaddr(addr);
146 92615a5a David Gibson
147 92615a5a David Gibson
    if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
148 92615a5a David Gibson
        /* Access must be to a valid device, within bounds and
149 92615a5a David Gibson
         * naturally aligned */
150 92615a5a David Gibson
        rtas_st(rets, 0, -1);
151 92615a5a David Gibson
        return;
152 92615a5a David Gibson
    }
153 92615a5a David Gibson
154 92615a5a David Gibson
    pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
155 92615a5a David Gibson
                                 val, size);
156 92615a5a David Gibson
157 3384f95c David Gibson
    rtas_st(rets, 0, 0);
158 3384f95c David Gibson
}
159 3384f95c David Gibson
160 3384f95c David Gibson
static void rtas_ibm_write_pci_config(sPAPREnvironment *spapr,
161 3384f95c David Gibson
                                      uint32_t token, uint32_t nargs,
162 3384f95c David Gibson
                                      target_ulong args,
163 3384f95c David Gibson
                                      uint32_t nret, target_ulong rets)
164 3384f95c David Gibson
{
165 92615a5a David Gibson
    uint64_t buid;
166 3384f95c David Gibson
    uint32_t val, size, addr;
167 3384f95c David Gibson
168 92615a5a David Gibson
    if ((nargs != 5) || (nret != 1)) {
169 3384f95c David Gibson
        rtas_st(rets, 0, -1);
170 3384f95c David Gibson
        return;
171 3384f95c David Gibson
    }
172 92615a5a David Gibson
173 92615a5a David Gibson
    buid = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 2);
174 3384f95c David Gibson
    val = rtas_ld(args, 4);
175 3384f95c David Gibson
    size = rtas_ld(args, 3);
176 92615a5a David Gibson
    addr = rtas_ld(args, 0);
177 92615a5a David Gibson
178 92615a5a David Gibson
    finish_write_pci_config(spapr, buid, addr, size, val, rets);
179 3384f95c David Gibson
}
180 3384f95c David Gibson
181 3384f95c David Gibson
static void rtas_write_pci_config(sPAPREnvironment *spapr,
182 3384f95c David Gibson
                                  uint32_t token, uint32_t nargs,
183 3384f95c David Gibson
                                  target_ulong args,
184 3384f95c David Gibson
                                  uint32_t nret, target_ulong rets)
185 3384f95c David Gibson
{
186 3384f95c David Gibson
    uint32_t val, size, addr;
187 3384f95c David Gibson
188 92615a5a David Gibson
    if ((nargs != 3) || (nret != 1)) {
189 3384f95c David Gibson
        rtas_st(rets, 0, -1);
190 3384f95c David Gibson
        return;
191 3384f95c David Gibson
    }
192 92615a5a David Gibson
193 92615a5a David Gibson
194 3384f95c David Gibson
    val = rtas_ld(args, 2);
195 3384f95c David Gibson
    size = rtas_ld(args, 1);
196 92615a5a David Gibson
    addr = rtas_ld(args, 0);
197 92615a5a David Gibson
198 92615a5a David Gibson
    finish_write_pci_config(spapr, 0, addr, size, val, rets);
199 3384f95c David Gibson
}
200 3384f95c David Gibson
201 3384f95c David Gibson
static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
202 3384f95c David Gibson
{
203 3384f95c David Gibson
    /*
204 3384f95c David Gibson
     * Here we need to convert pci_dev + irq_num to some unique value
205 3384f95c David Gibson
     * which is less than number of IRQs on the specific bus (now it
206 3384f95c David Gibson
     * is 16).  At the moment irq_num == device_id (number of the
207 3384f95c David Gibson
     * slot?)
208 3384f95c David Gibson
     * FIXME: we should swizzle in fn and irq_num
209 3384f95c David Gibson
     */
210 3384f95c David Gibson
    return (pci_dev->devfn >> 3) % SPAPR_PCI_NUM_LSI;
211 3384f95c David Gibson
}
212 3384f95c David Gibson
213 3384f95c David Gibson
static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
214 3384f95c David Gibson
{
215 3384f95c David Gibson
    /*
216 3384f95c David Gibson
     * Here we use the number returned by pci_spapr_map_irq to find a
217 3384f95c David Gibson
     * corresponding qemu_irq.
218 3384f95c David Gibson
     */
219 3384f95c David Gibson
    sPAPRPHBState *phb = opaque;
220 3384f95c David Gibson
221 3384f95c David Gibson
    qemu_set_irq(phb->lsi_table[irq_num].qirq, level);
222 3384f95c David Gibson
}
223 3384f95c David Gibson
224 3384f95c David Gibson
static uint64_t spapr_io_read(void *opaque, target_phys_addr_t addr,
225 3384f95c David Gibson
                              unsigned size)
226 3384f95c David Gibson
{
227 3384f95c David Gibson
    switch (size) {
228 3384f95c David Gibson
    case 1:
229 3384f95c David Gibson
        return cpu_inb(addr);
230 3384f95c David Gibson
    case 2:
231 3384f95c David Gibson
        return cpu_inw(addr);
232 3384f95c David Gibson
    case 4:
233 3384f95c David Gibson
        return cpu_inl(addr);
234 3384f95c David Gibson
    }
235 3384f95c David Gibson
    assert(0);
236 3384f95c David Gibson
}
237 3384f95c David Gibson
238 3384f95c David Gibson
static void spapr_io_write(void *opaque, target_phys_addr_t addr,
239 3384f95c David Gibson
                           uint64_t data, unsigned size)
240 3384f95c David Gibson
{
241 3384f95c David Gibson
    switch (size) {
242 3384f95c David Gibson
    case 1:
243 3384f95c David Gibson
        cpu_outb(addr, data);
244 3384f95c David Gibson
        return;
245 3384f95c David Gibson
    case 2:
246 3384f95c David Gibson
        cpu_outw(addr, data);
247 3384f95c David Gibson
        return;
248 3384f95c David Gibson
    case 4:
249 3384f95c David Gibson
        cpu_outl(addr, data);
250 3384f95c David Gibson
        return;
251 3384f95c David Gibson
    }
252 3384f95c David Gibson
    assert(0);
253 3384f95c David Gibson
}
254 3384f95c David Gibson
255 a348f108 Stefan Weil
static const MemoryRegionOps spapr_io_ops = {
256 3384f95c David Gibson
    .endianness = DEVICE_LITTLE_ENDIAN,
257 3384f95c David Gibson
    .read = spapr_io_read,
258 3384f95c David Gibson
    .write = spapr_io_write
259 3384f95c David Gibson
};
260 3384f95c David Gibson
261 298a9710 David Gibson
/*
262 298a9710 David Gibson
 * PHB PCI device
263 298a9710 David Gibson
 */
264 298a9710 David Gibson
static int spapr_phb_init(SysBusDevice *s)
265 3384f95c David Gibson
{
266 298a9710 David Gibson
    sPAPRPHBState *phb = FROM_SYSBUS(sPAPRPHBState, s);
267 298a9710 David Gibson
    char *namebuf;
268 298a9710 David Gibson
    int i;
269 3384f95c David Gibson
    PCIBus *bus;
270 3384f95c David Gibson
271 298a9710 David Gibson
    phb->dtbusname = g_strdup_printf("pci@%" PRIx64, phb->buid);
272 298a9710 David Gibson
    namebuf = alloca(strlen(phb->dtbusname) + 32);
273 3384f95c David Gibson
274 298a9710 David Gibson
    /* Initialize memory regions */
275 298a9710 David Gibson
    sprintf(namebuf, "%s.mmio", phb->dtbusname);
276 3384f95c David Gibson
    memory_region_init(&phb->memspace, namebuf, INT64_MAX);
277 3384f95c David Gibson
278 298a9710 David Gibson
    sprintf(namebuf, "%s.mmio-alias", phb->dtbusname);
279 3384f95c David Gibson
    memory_region_init_alias(&phb->memwindow, namebuf, &phb->memspace,
280 298a9710 David Gibson
                             SPAPR_PCI_MEM_WIN_BUS_OFFSET, phb->mem_win_size);
281 298a9710 David Gibson
    memory_region_add_subregion(get_system_memory(), phb->mem_win_addr,
282 3384f95c David Gibson
                                &phb->memwindow);
283 3384f95c David Gibson
284 3384f95c David Gibson
    /* On ppc, we only have MMIO no specific IO space from the CPU
285 3384f95c David Gibson
     * perspective.  In theory we ought to be able to embed the PCI IO
286 3384f95c David Gibson
     * memory region direction in the system memory space.  However,
287 3384f95c David Gibson
     * if any of the IO BAR subregions use the old_portio mechanism,
288 3384f95c David Gibson
     * that won't be processed properly unless accessed from the
289 3384f95c David Gibson
     * system io address space.  This hack to bounce things via
290 3384f95c David Gibson
     * system_io works around the problem until all the users of
291 3384f95c David Gibson
     * old_portion are updated */
292 298a9710 David Gibson
    sprintf(namebuf, "%s.io", phb->dtbusname);
293 3384f95c David Gibson
    memory_region_init(&phb->iospace, namebuf, SPAPR_PCI_IO_WIN_SIZE);
294 3384f95c David Gibson
    /* FIXME: fix to support multiple PHBs */
295 3384f95c David Gibson
    memory_region_add_subregion(get_system_io(), 0, &phb->iospace);
296 3384f95c David Gibson
297 298a9710 David Gibson
    sprintf(namebuf, "%s.io-alias", phb->dtbusname);
298 3384f95c David Gibson
    memory_region_init_io(&phb->iowindow, &spapr_io_ops, phb,
299 3384f95c David Gibson
                          namebuf, SPAPR_PCI_IO_WIN_SIZE);
300 298a9710 David Gibson
    memory_region_add_subregion(get_system_memory(), phb->io_win_addr,
301 3384f95c David Gibson
                                &phb->iowindow);
302 3384f95c David Gibson
303 298a9710 David Gibson
    bus = pci_register_bus(&phb->busdev.qdev,
304 298a9710 David Gibson
                           phb->busname ? phb->busname : phb->dtbusname,
305 298a9710 David Gibson
                           pci_spapr_set_irq, pci_spapr_map_irq, phb,
306 298a9710 David Gibson
                           &phb->memspace, &phb->iospace,
307 298a9710 David Gibson
                           PCI_DEVFN(0, 0), SPAPR_PCI_NUM_LSI);
308 298a9710 David Gibson
    phb->host_state.bus = bus;
309 298a9710 David Gibson
310 298a9710 David Gibson
    QLIST_INSERT_HEAD(&spapr->phbs, phb, list);
311 298a9710 David Gibson
312 298a9710 David Gibson
    /* Initialize the LSI table */
313 298a9710 David Gibson
    for (i = 0; i < SPAPR_PCI_NUM_LSI; i++) {
314 298a9710 David Gibson
        qemu_irq qirq;
315 298a9710 David Gibson
        uint32_t num;
316 298a9710 David Gibson
317 298a9710 David Gibson
        qirq = spapr_allocate_lsi(0, &num);
318 298a9710 David Gibson
        if (!qirq) {
319 298a9710 David Gibson
            return -1;
320 298a9710 David Gibson
        }
321 298a9710 David Gibson
322 298a9710 David Gibson
        phb->lsi_table[i].dt_irq = num;
323 298a9710 David Gibson
        phb->lsi_table[i].qirq = qirq;
324 298a9710 David Gibson
    }
325 298a9710 David Gibson
326 298a9710 David Gibson
    return 0;
327 298a9710 David Gibson
}
328 298a9710 David Gibson
329 298a9710 David Gibson
static Property spapr_phb_properties[] = {
330 298a9710 David Gibson
    DEFINE_PROP_HEX64("buid", sPAPRPHBState, buid, 0),
331 298a9710 David Gibson
    DEFINE_PROP_STRING("busname", sPAPRPHBState, busname),
332 298a9710 David Gibson
    DEFINE_PROP_HEX64("mem_win_addr", sPAPRPHBState, mem_win_addr, 0),
333 298a9710 David Gibson
    DEFINE_PROP_HEX64("mem_win_size", sPAPRPHBState, mem_win_size, 0x20000000),
334 298a9710 David Gibson
    DEFINE_PROP_HEX64("io_win_addr", sPAPRPHBState, io_win_addr, 0),
335 298a9710 David Gibson
    DEFINE_PROP_HEX64("io_win_size", sPAPRPHBState, io_win_size, 0x10000),
336 298a9710 David Gibson
    DEFINE_PROP_END_OF_LIST(),
337 298a9710 David Gibson
};
338 298a9710 David Gibson
339 298a9710 David Gibson
static void spapr_phb_class_init(ObjectClass *klass, void *data)
340 298a9710 David Gibson
{
341 298a9710 David Gibson
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
342 298a9710 David Gibson
    DeviceClass *dc = DEVICE_CLASS(klass);
343 298a9710 David Gibson
344 298a9710 David Gibson
    sdc->init = spapr_phb_init;
345 298a9710 David Gibson
    dc->props = spapr_phb_properties;
346 3384f95c David Gibson
347 3384f95c David Gibson
    spapr_rtas_register("read-pci-config", rtas_read_pci_config);
348 3384f95c David Gibson
    spapr_rtas_register("write-pci-config", rtas_write_pci_config);
349 3384f95c David Gibson
    spapr_rtas_register("ibm,read-pci-config", rtas_ibm_read_pci_config);
350 3384f95c David Gibson
    spapr_rtas_register("ibm,write-pci-config", rtas_ibm_write_pci_config);
351 298a9710 David Gibson
}
352 3384f95c David Gibson
353 298a9710 David Gibson
static TypeInfo spapr_phb_info = {
354 298a9710 David Gibson
    .name          = "spapr-pci-host-bridge",
355 298a9710 David Gibson
    .parent        = TYPE_SYS_BUS_DEVICE,
356 298a9710 David Gibson
    .instance_size = sizeof(sPAPRPHBState),
357 298a9710 David Gibson
    .class_init    = spapr_phb_class_init,
358 298a9710 David Gibson
};
359 298a9710 David Gibson
360 298a9710 David Gibson
void spapr_create_phb(sPAPREnvironment *spapr,
361 298a9710 David Gibson
                      const char *busname, uint64_t buid,
362 298a9710 David Gibson
                      uint64_t mem_win_addr, uint64_t mem_win_size,
363 298a9710 David Gibson
                      uint64_t io_win_addr)
364 298a9710 David Gibson
{
365 298a9710 David Gibson
    DeviceState *dev;
366 298a9710 David Gibson
367 298a9710 David Gibson
    dev = qdev_create(NULL, spapr_phb_info.name);
368 3384f95c David Gibson
369 298a9710 David Gibson
    if (busname) {
370 298a9710 David Gibson
        qdev_prop_set_string(dev, "busname", g_strdup(busname));
371 298a9710 David Gibson
    }
372 298a9710 David Gibson
    qdev_prop_set_uint64(dev, "buid", buid);
373 298a9710 David Gibson
    qdev_prop_set_uint64(dev, "mem_win_addr", mem_win_addr);
374 298a9710 David Gibson
    qdev_prop_set_uint64(dev, "mem_win_size", mem_win_size);
375 298a9710 David Gibson
    qdev_prop_set_uint64(dev, "io_win_addr", io_win_addr);
376 298a9710 David Gibson
377 298a9710 David Gibson
    qdev_init_nofail(dev);
378 3384f95c David Gibson
}
379 3384f95c David Gibson
380 3384f95c David Gibson
/* Macros to operate with address in OF binding to PCI */
381 3384f95c David Gibson
#define b_x(x, p, l)    (((x) & ((1<<(l))-1)) << (p))
382 3384f95c David Gibson
#define b_n(x)          b_x((x), 31, 1) /* 0 if relocatable */
383 3384f95c David Gibson
#define b_p(x)          b_x((x), 30, 1) /* 1 if prefetchable */
384 3384f95c David Gibson
#define b_t(x)          b_x((x), 29, 1) /* 1 if the address is aliased */
385 3384f95c David Gibson
#define b_ss(x)         b_x((x), 24, 2) /* the space code */
386 3384f95c David Gibson
#define b_bbbbbbbb(x)   b_x((x), 16, 8) /* bus number */
387 3384f95c David Gibson
#define b_ddddd(x)      b_x((x), 11, 5) /* device number */
388 3384f95c David Gibson
#define b_fff(x)        b_x((x), 8, 3)  /* function number */
389 3384f95c David Gibson
#define b_rrrrrrrr(x)   b_x((x), 0, 8)  /* register number */
390 3384f95c David Gibson
391 3384f95c David Gibson
int spapr_populate_pci_devices(sPAPRPHBState *phb,
392 3384f95c David Gibson
                               uint32_t xics_phandle,
393 3384f95c David Gibson
                               void *fdt)
394 3384f95c David Gibson
{
395 3384f95c David Gibson
    PCIBus *bus = phb->host_state.bus;
396 4d8d5467 Benjamin Herrenschmidt
    int bus_off, i;
397 3384f95c David Gibson
    char nodename[256];
398 3384f95c David Gibson
    uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
399 3384f95c David Gibson
    struct {
400 3384f95c David Gibson
        uint32_t hi;
401 3384f95c David Gibson
        uint64_t child;
402 3384f95c David Gibson
        uint64_t parent;
403 3384f95c David Gibson
        uint64_t size;
404 3384f95c David Gibson
    } __attribute__((packed)) ranges[] = {
405 3384f95c David Gibson
        {
406 3384f95c David Gibson
            cpu_to_be32(b_ss(1)), cpu_to_be64(0),
407 3384f95c David Gibson
            cpu_to_be64(phb->io_win_addr),
408 3384f95c David Gibson
            cpu_to_be64(memory_region_size(&phb->iospace)),
409 3384f95c David Gibson
        },
410 3384f95c David Gibson
        {
411 3384f95c David Gibson
            cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
412 3384f95c David Gibson
            cpu_to_be64(phb->mem_win_addr),
413 3384f95c David Gibson
            cpu_to_be64(memory_region_size(&phb->memwindow)),
414 3384f95c David Gibson
        },
415 3384f95c David Gibson
    };
416 3384f95c David Gibson
    uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
417 3384f95c David Gibson
    uint32_t interrupt_map_mask[] = {
418 4d8d5467 Benjamin Herrenschmidt
        cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, 0x0};
419 3384f95c David Gibson
    uint32_t interrupt_map[bus->nirq][7];
420 3384f95c David Gibson
421 3384f95c David Gibson
    /* Start populating the FDT */
422 3384f95c David Gibson
    sprintf(nodename, "pci@%" PRIx64, phb->buid);
423 3384f95c David Gibson
    bus_off = fdt_add_subnode(fdt, 0, nodename);
424 3384f95c David Gibson
    if (bus_off < 0) {
425 3384f95c David Gibson
        return bus_off;
426 3384f95c David Gibson
    }
427 3384f95c David Gibson
428 3384f95c David Gibson
#define _FDT(exp) \
429 3384f95c David Gibson
    do { \
430 3384f95c David Gibson
        int ret = (exp);                                           \
431 3384f95c David Gibson
        if (ret < 0) {                                             \
432 3384f95c David Gibson
            return ret;                                            \
433 3384f95c David Gibson
        }                                                          \
434 3384f95c David Gibson
    } while (0)
435 3384f95c David Gibson
436 3384f95c David Gibson
    /* Write PHB properties */
437 3384f95c David Gibson
    _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
438 3384f95c David Gibson
    _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
439 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
440 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
441 3384f95c David Gibson
    _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
442 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
443 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
444 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof(ranges)));
445 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
446 3f7565c9 Benjamin Herrenschmidt
    _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
447 3384f95c David Gibson
448 4d8d5467 Benjamin Herrenschmidt
    /* Build the interrupt-map, this must matches what is done
449 4d8d5467 Benjamin Herrenschmidt
     * in pci_spapr_map_irq
450 4d8d5467 Benjamin Herrenschmidt
     */
451 4d8d5467 Benjamin Herrenschmidt
    _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
452 4d8d5467 Benjamin Herrenschmidt
                     &interrupt_map_mask, sizeof(interrupt_map_mask)));
453 4d8d5467 Benjamin Herrenschmidt
    for (i = 0; i < 7; i++) {
454 4d8d5467 Benjamin Herrenschmidt
        uint32_t *irqmap = interrupt_map[i];
455 4d8d5467 Benjamin Herrenschmidt
        irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
456 3384f95c David Gibson
        irqmap[1] = 0;
457 3384f95c David Gibson
        irqmap[2] = 0;
458 3384f95c David Gibson
        irqmap[3] = 0;
459 3384f95c David Gibson
        irqmap[4] = cpu_to_be32(xics_phandle);
460 4d8d5467 Benjamin Herrenschmidt
        irqmap[5] = cpu_to_be32(phb->lsi_table[i % SPAPR_PCI_NUM_LSI].dt_irq);
461 3384f95c David Gibson
        irqmap[6] = cpu_to_be32(0x8);
462 3384f95c David Gibson
    }
463 3384f95c David Gibson
    /* Write interrupt map */
464 3384f95c David Gibson
    _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
465 4d8d5467 Benjamin Herrenschmidt
                     7 * sizeof(interrupt_map[0])));
466 3384f95c David Gibson
467 3384f95c David Gibson
    return 0;
468 3384f95c David Gibson
}
469 298a9710 David Gibson
470 298a9710 David Gibson
static void register_types(void)
471 298a9710 David Gibson
{
472 298a9710 David Gibson
    type_register_static(&spapr_phb_info);
473 298a9710 David Gibson
}
474 298a9710 David Gibson
type_init(register_types)