5 #include <linux/types.h>
6 #include <linux/rbtree.h>
7 #include <linux/list.h>
14 #define IRQ_MAX_GSI 64
15 #define IRQCHIP_MASTER 0
16 #define IRQCHIP_SLAVE 1
17 #define IRQCHIP_IOAPIC 2
19 static u8 next_line
= 3;
20 static u8 next_dev
= 1;
21 static struct rb_root pci_tree
= RB_ROOT
;
23 /* First 24 GSIs are routed between IRQCHIPs and IOAPICs */
26 struct kvm_irq_routing
*irq_routing
;
28 static int irq__add_routing(u32 gsi
, u32 type
, u32 irqchip
, u32 pin
)
30 if (gsi
>= IRQ_MAX_GSI
)
33 irq_routing
->entries
[irq_routing
->nr
++] =
34 (struct kvm_irq_routing_entry
) {
37 .u
.irqchip
.irqchip
= irqchip
,
44 static struct pci_dev
*search(struct rb_root
*root
, u32 id
)
46 struct rb_node
*node
= root
->rb_node
;
49 struct pci_dev
*data
= container_of(node
, struct pci_dev
, node
);
52 result
= id
- data
->id
;
57 node
= node
->rb_right
;
64 static int insert(struct rb_root
*root
, struct pci_dev
*data
)
66 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
68 /* Figure out where to put new node */
70 struct pci_dev
*this = container_of(*new, struct pci_dev
, node
);
71 int result
= data
->id
- this->id
;
75 new = &((*new)->rb_left
);
77 new = &((*new)->rb_right
);
82 /* Add new node and rebalance tree. */
83 rb_link_node(&data
->node
, parent
, new);
84 rb_insert_color(&data
->node
, root
);
89 int irq__register_device(u32 dev
, u8
*num
, u8
*pin
, u8
*line
)
93 node
= search(&pci_tree
, dev
);
96 /* We haven't found a node - First device of it's kind */
97 node
= malloc(sizeof(*node
));
101 *node
= (struct pci_dev
) {
104 * PCI supports only INTA#,B#,C#,D# per device.
105 * A#,B#,C#,D# are allowed for multifunctional
106 * devices so stick with A# for our single
112 INIT_LIST_HEAD(&node
->lines
);
114 if (insert(&pci_tree
, node
) != 1) {
121 /* This device already has a pin assigned, give out a new line and device id */
122 struct irq_line
*new = malloc(sizeof(*new));
126 new->line
= next_line
++;
131 list_add(&new->node
, &node
->lines
);
139 void irq__init(struct kvm
*kvm
)
143 irq_routing
= malloc(sizeof(struct kvm_irq_routing
) +
144 IRQ_MAX_GSI
* sizeof(struct kvm_irq_routing_entry
));
145 if (irq_routing
== NULL
)
146 die("Failed allocating space for GSI table");
148 /* Hook first 8 GSIs to master IRQCHIP */
149 for (i
= 0; i
< 8; i
++)
151 irq__add_routing(i
, KVM_IRQ_ROUTING_IRQCHIP
, IRQCHIP_MASTER
, i
);
153 /* Hook next 8 GSIs to slave IRQCHIP */
154 for (i
= 8; i
< 16; i
++)
155 irq__add_routing(i
, KVM_IRQ_ROUTING_IRQCHIP
, IRQCHIP_SLAVE
, i
- 8);
157 /* Last but not least, IOAPIC */
158 for (i
= 0; i
< 24; i
++) {
160 irq__add_routing(i
, KVM_IRQ_ROUTING_IRQCHIP
, IRQCHIP_IOAPIC
, 2);
162 irq__add_routing(i
, KVM_IRQ_ROUTING_IRQCHIP
, IRQCHIP_IOAPIC
, i
);
165 r
= ioctl(kvm
->vm_fd
, KVM_SET_GSI_ROUTING
, irq_routing
);
167 die("Failed setting GSI routes");
170 int irq__add_msix_route(struct kvm
*kvm
, u32 low
, u32 high
, u32 data
)
174 irq_routing
->entries
[irq_routing
->nr
++] =
175 (struct kvm_irq_routing_entry
) {
177 .type
= KVM_IRQ_ROUTING_MSI
,
178 .u
.msi
.address_lo
= low
,
179 .u
.msi
.address_hi
= high
,
183 r
= ioctl(kvm
->vm_fd
, KVM_SET_GSI_ROUTING
, irq_routing
);
190 struct rb_node
*irq__get_pci_tree(void)
192 return rb_first(&pci_tree
);