Statistics
| Branch: | Revision:

root / hw / spapr_pci.c @ 323abebf

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