2 * Copyright (C) 2010 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
10 #include "hw/xen_common.h"
11 #include "hw/xen_backend.h"
13 #include "xen-mapcache.h"
16 /* Xen specific function for piix pci */
18 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
20 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
23 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
25 xc_hvm_set_pci_intx_level(xen_xc
, xen_domid
, 0, 0, irq_num
>> 2,
29 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
33 /* Scan for updates to PCI link routes (0x60-0x63). */
34 for (i
= 0; i
< len
; i
++) {
35 uint8_t v
= (val
>> (8 * i
)) & 0xff;
40 if (((address
+ i
) >= 0x60) && ((address
+ i
) <= 0x63)) {
41 xc_hvm_set_pci_link_route(xen_xc
, xen_domid
, address
+ i
- 0x60, v
);
46 /* Xen Interrupt Controller */
48 static void xen_set_irq(void *opaque
, int irq
, int level
)
50 xc_hvm_set_isa_irq_level(xen_xc
, xen_domid
, irq
, level
);
53 qemu_irq
*xen_interrupt_controller_init(void)
55 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
60 static void xen_ram_init(ram_addr_t ram_size
)
63 ram_addr_t below_4g_mem_size
, above_4g_mem_size
= 0;
65 new_block
= qemu_mallocz(sizeof (*new_block
));
66 pstrcpy(new_block
->idstr
, sizeof (new_block
->idstr
), "xen.ram");
67 new_block
->host
= NULL
;
68 new_block
->offset
= 0;
69 new_block
->length
= ram_size
;
71 QLIST_INSERT_HEAD(&ram_list
.blocks
, new_block
, next
);
73 ram_list
.phys_dirty
= qemu_realloc(ram_list
.phys_dirty
,
74 new_block
->length
>> TARGET_PAGE_BITS
);
75 memset(ram_list
.phys_dirty
+ (new_block
->offset
>> TARGET_PAGE_BITS
),
76 0xff, new_block
->length
>> TARGET_PAGE_BITS
);
78 if (ram_size
>= 0xe0000000 ) {
79 above_4g_mem_size
= ram_size
- 0xe0000000;
80 below_4g_mem_size
= 0xe0000000;
82 below_4g_mem_size
= ram_size
;
85 cpu_register_physical_memory(0, below_4g_mem_size
, new_block
->offset
);
86 #if TARGET_PHYS_ADDR_BITS > 32
87 if (above_4g_mem_size
> 0) {
88 cpu_register_physical_memory(0x100000000ULL
, above_4g_mem_size
,
89 new_block
->offset
+ below_4g_mem_size
);
94 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
)
100 trace_xen_ram_alloc(ram_addr
, size
);
102 nr_pfn
= size
>> TARGET_PAGE_BITS
;
103 pfn_list
= qemu_malloc(sizeof (*pfn_list
) * nr_pfn
);
105 for (i
= 0; i
< nr_pfn
; i
++) {
106 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
109 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
110 hw_error("xen: failed to populate ram at %lx", ram_addr
);
117 /* VCPU Operations, MMIO, IO ring ... */
119 static void xen_reset_vcpu(void *opaque
)
121 CPUState
*env
= opaque
;
126 void xen_vcpu_init(void)
130 if ((first_cpu
= qemu_get_cpu(0))) {
131 qemu_register_reset(xen_reset_vcpu
, first_cpu
);
132 xen_reset_vcpu(first_cpu
);
140 xen_xc
= xen_xc_interface_open(0, 0, 0);
141 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
142 xen_be_printf(NULL
, 0, "can't open xen interface\n");
149 int xen_hvm_init(void)
151 /* Init RAM management */
152 qemu_map_cache_init();
153 xen_ram_init(ram_size
);