2 * libqos PCI bindings for PC
4 * Copyright IBM, Corp. 2012-2013
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "libqos/pci-pc.h"
16 #include "hw/pci/pci_regs.h"
18 #include "qemu-common.h"
19 #include "qemu/host-utils.h"
23 #define ACPI_PCIHP_ADDR 0xae00
24 #define PCI_EJ_BASE 0x0008
26 typedef struct QPCIBusPC
30 uint32_t pci_hole_start
;
31 uint32_t pci_hole_size
;
32 uint32_t pci_hole_alloc
;
34 uint16_t pci_iohole_start
;
35 uint16_t pci_iohole_size
;
36 uint16_t pci_iohole_alloc
;
39 static uint8_t qpci_pc_io_readb(QPCIBus
*bus
, void *addr
)
41 uintptr_t port
= (uintptr_t)addr
;
53 static uint16_t qpci_pc_io_readw(QPCIBus
*bus
, void *addr
)
55 uintptr_t port
= (uintptr_t)addr
;
67 static uint32_t qpci_pc_io_readl(QPCIBus
*bus
, void *addr
)
69 uintptr_t port
= (uintptr_t)addr
;
81 static void qpci_pc_io_writeb(QPCIBus
*bus
, void *addr
, uint8_t value
)
83 uintptr_t port
= (uintptr_t)addr
;
92 static void qpci_pc_io_writew(QPCIBus
*bus
, void *addr
, uint16_t value
)
94 uintptr_t port
= (uintptr_t)addr
;
103 static void qpci_pc_io_writel(QPCIBus
*bus
, void *addr
, uint32_t value
)
105 uintptr_t port
= (uintptr_t)addr
;
107 if (port
< 0x10000) {
114 static uint8_t qpci_pc_config_readb(QPCIBus
*bus
, int devfn
, uint8_t offset
)
116 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
120 static uint16_t qpci_pc_config_readw(QPCIBus
*bus
, int devfn
, uint8_t offset
)
122 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
126 static uint32_t qpci_pc_config_readl(QPCIBus
*bus
, int devfn
, uint8_t offset
)
128 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
132 static void qpci_pc_config_writeb(QPCIBus
*bus
, int devfn
, uint8_t offset
, uint8_t value
)
134 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
138 static void qpci_pc_config_writew(QPCIBus
*bus
, int devfn
, uint8_t offset
, uint16_t value
)
140 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
144 static void qpci_pc_config_writel(QPCIBus
*bus
, int devfn
, uint8_t offset
, uint32_t value
)
146 outl(0xcf8, (1U << 31) | (devfn
<< 8) | offset
);
150 static void *qpci_pc_iomap(QPCIBus
*bus
, QPCIDevice
*dev
, int barno
, uint64_t *sizeptr
)
152 QPCIBusPC
*s
= container_of(bus
, QPCIBusPC
, bus
);
153 static const int bar_reg_map
[] = {
154 PCI_BASE_ADDRESS_0
, PCI_BASE_ADDRESS_1
, PCI_BASE_ADDRESS_2
,
155 PCI_BASE_ADDRESS_3
, PCI_BASE_ADDRESS_4
, PCI_BASE_ADDRESS_5
,
162 g_assert(barno
>= 0 && barno
<= 5);
163 bar_reg
= bar_reg_map
[barno
];
165 qpci_config_writel(dev
, bar_reg
, 0xFFFFFFFF);
166 addr
= qpci_config_readl(dev
, bar_reg
);
168 io_type
= addr
& PCI_BASE_ADDRESS_SPACE
;
169 if (io_type
== PCI_BASE_ADDRESS_SPACE_IO
) {
170 addr
&= PCI_BASE_ADDRESS_IO_MASK
;
172 addr
&= PCI_BASE_ADDRESS_MEM_MASK
;
175 size
= (1ULL << ctzl(addr
));
183 if (io_type
== PCI_BASE_ADDRESS_SPACE_IO
) {
186 g_assert((s
->pci_iohole_alloc
+ size
) <= s
->pci_iohole_size
);
187 loc
= s
->pci_iohole_start
+ s
->pci_iohole_alloc
;
188 s
->pci_iohole_alloc
+= size
;
190 qpci_config_writel(dev
, bar_reg
, loc
| PCI_BASE_ADDRESS_SPACE_IO
);
192 return (void *)(intptr_t)loc
;
196 g_assert((s
->pci_hole_alloc
+ size
) <= s
->pci_hole_size
);
197 loc
= s
->pci_hole_start
+ s
->pci_hole_alloc
;
198 s
->pci_hole_alloc
+= size
;
200 qpci_config_writel(dev
, bar_reg
, loc
);
202 return (void *)(intptr_t)loc
;
206 static void qpci_pc_iounmap(QPCIBus
*bus
, void *data
)
211 QPCIBus
*qpci_init_pc(void)
215 ret
= g_malloc(sizeof(*ret
));
217 ret
->bus
.io_readb
= qpci_pc_io_readb
;
218 ret
->bus
.io_readw
= qpci_pc_io_readw
;
219 ret
->bus
.io_readl
= qpci_pc_io_readl
;
221 ret
->bus
.io_writeb
= qpci_pc_io_writeb
;
222 ret
->bus
.io_writew
= qpci_pc_io_writew
;
223 ret
->bus
.io_writel
= qpci_pc_io_writel
;
225 ret
->bus
.config_readb
= qpci_pc_config_readb
;
226 ret
->bus
.config_readw
= qpci_pc_config_readw
;
227 ret
->bus
.config_readl
= qpci_pc_config_readl
;
229 ret
->bus
.config_writeb
= qpci_pc_config_writeb
;
230 ret
->bus
.config_writew
= qpci_pc_config_writew
;
231 ret
->bus
.config_writel
= qpci_pc_config_writel
;
233 ret
->bus
.iomap
= qpci_pc_iomap
;
234 ret
->bus
.iounmap
= qpci_pc_iounmap
;
236 ret
->pci_hole_start
= 0xE0000000;
237 ret
->pci_hole_size
= 0x20000000;
238 ret
->pci_hole_alloc
= 0;
240 ret
->pci_iohole_start
= 0xc000;
241 ret
->pci_iohole_size
= 0x4000;
242 ret
->pci_iohole_alloc
= 0;
247 void qpci_free_pc(QPCIBus
*bus
)
249 QPCIBusPC
*s
= container_of(bus
, QPCIBusPC
, bus
);
254 void qpci_plug_device_test(const char *driver
, const char *id
,
255 uint8_t slot
, const char *opts
)
260 cmd
= g_strdup_printf("{'execute': 'device_add',"
267 opts
? opts
: "", opts
? "," : "",
272 g_assert(!qdict_haskey(response
, "error"));
276 void qpci_unplug_acpi_device_test(const char *id
, uint8_t slot
)
281 cmd
= g_strdup_printf("{'execute': 'device_del',"
288 g_assert(!qdict_haskey(response
, "error"));
291 outb(ACPI_PCIHP_ADDR
+ PCI_EJ_BASE
, 1 << slot
);
295 g_assert(qdict_haskey(response
, "event"));
296 g_assert(!strcmp(qdict_get_str(response
, "event"), "DEVICE_DELETED"));