13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21c9f8520SAlexander Graf /* 31c9f8520SAlexander Graf * irqchip.c: Common API for in kernel interrupt controllers 41c9f8520SAlexander Graf * Copyright (c) 2007, Intel Corporation. 51c9f8520SAlexander Graf * Copyright 2010 Red Hat, Inc. and/or its affiliates. 61c9f8520SAlexander Graf * Copyright (c) 2013, Alexander Graf <agraf@suse.de> 71c9f8520SAlexander Graf * 81c9f8520SAlexander Graf * This file is derived from virt/kvm/irq_comm.c. 91c9f8520SAlexander Graf * 101c9f8520SAlexander Graf * Authors: 111c9f8520SAlexander Graf * Yaozu (Eddie) Dong <Eddie.dong@intel.com> 121c9f8520SAlexander Graf * Alexander Graf <agraf@suse.de> 131c9f8520SAlexander Graf */ 141c9f8520SAlexander Graf 151c9f8520SAlexander Graf #include <linux/kvm_host.h> 161c9f8520SAlexander Graf #include <linux/slab.h> 17719d93cdSChristian Borntraeger #include <linux/srcu.h> 181c9f8520SAlexander Graf #include <linux/export.h> 191c9f8520SAlexander Graf #include <trace/events/kvm.h> 201c9f8520SAlexander Graf 219957c86dSPaul Mackerras int kvm_irq_map_gsi(struct kvm *kvm, 229957c86dSPaul Mackerras struct kvm_kernel_irq_routing_entry *entries, int gsi) 238ba918d4SPaul Mackerras { 249957c86dSPaul Mackerras struct kvm_irq_routing_table *irq_rt; 258ba918d4SPaul Mackerras struct kvm_kernel_irq_routing_entry *e; 268ba918d4SPaul Mackerras int n = 0; 278ba918d4SPaul Mackerras 289957c86dSPaul Mackerras irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, 299957c86dSPaul Mackerras lockdep_is_held(&kvm->irq_lock)); 30c622a3c2SPaolo Bonzini if (irq_rt && gsi < irq_rt->nr_rt_entries) { 318ba918d4SPaul Mackerras hlist_for_each_entry(e, &irq_rt->map[gsi], link) { 328ba918d4SPaul Mackerras entries[n] = *e; 338ba918d4SPaul Mackerras ++n; 348ba918d4SPaul Mackerras } 358ba918d4SPaul Mackerras } 368ba918d4SPaul Mackerras 378ba918d4SPaul Mackerras return n; 388ba918d4SPaul Mackerras } 398ba918d4SPaul Mackerras 409957c86dSPaul Mackerras int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin) 418ba918d4SPaul Mackerras { 429957c86dSPaul Mackerras struct kvm_irq_routing_table *irq_rt; 439957c86dSPaul Mackerras 449957c86dSPaul Mackerras irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); 458ba918d4SPaul Mackerras return irq_rt->chip[irqchip][pin]; 468ba918d4SPaul Mackerras } 478ba918d4SPaul Mackerras 481c9f8520SAlexander Graf int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi) 491c9f8520SAlexander Graf { 501c9f8520SAlexander Graf struct kvm_kernel_irq_routing_entry route; 511c9f8520SAlexander Graf 52d663b8a2SPaolo Bonzini if (!kvm_arch_irqchip_in_kernel(kvm) || (msi->flags & ~KVM_MSI_VALID_DEVID)) 531c9f8520SAlexander Graf return -EINVAL; 541c9f8520SAlexander Graf 551c9f8520SAlexander Graf route.msi.address_lo = msi->address_lo; 561c9f8520SAlexander Graf route.msi.address_hi = msi->address_hi; 571c9f8520SAlexander Graf route.msi.data = msi->data; 58ebe91525SEric Auger route.msi.flags = msi->flags; 59ebe91525SEric Auger route.msi.devid = msi->devid; 601c9f8520SAlexander Graf 611c9f8520SAlexander Graf return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false); 621c9f8520SAlexander Graf } 631c9f8520SAlexander Graf 641c9f8520SAlexander Graf /* 651c9f8520SAlexander Graf * Return value: 661c9f8520SAlexander Graf * < 0 Interrupt was ignored (masked or not delivered for other reasons) 671c9f8520SAlexander Graf * = 0 Interrupt was coalesced (previous irq is still pending) 681c9f8520SAlexander Graf * > 0 Number of CPUs interrupt was delivered to 691c9f8520SAlexander Graf */ 701c9f8520SAlexander Graf int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, 711c9f8520SAlexander Graf bool line_status) 721c9f8520SAlexander Graf { 738ba918d4SPaul Mackerras struct kvm_kernel_irq_routing_entry irq_set[KVM_NR_IRQCHIPS]; 748ba918d4SPaul Mackerras int ret = -1, i, idx; 751c9f8520SAlexander Graf 761c9f8520SAlexander Graf trace_kvm_set_irq(irq, level, irq_source_id); 771c9f8520SAlexander Graf 781c9f8520SAlexander Graf /* Not possible to detect if the guest uses the PIC or the 791c9f8520SAlexander Graf * IOAPIC. So set the bit in both. The guest will ignore 801c9f8520SAlexander Graf * writes to the unused one. 811c9f8520SAlexander Graf */ 82719d93cdSChristian Borntraeger idx = srcu_read_lock(&kvm->irq_srcu); 839957c86dSPaul Mackerras i = kvm_irq_map_gsi(kvm, irq_set, irq); 84719d93cdSChristian Borntraeger srcu_read_unlock(&kvm->irq_srcu, idx); 851c9f8520SAlexander Graf 861c9f8520SAlexander Graf while (i--) { 871c9f8520SAlexander Graf int r; 881c9f8520SAlexander Graf r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level, 891c9f8520SAlexander Graf line_status); 901c9f8520SAlexander Graf if (r < 0) 911c9f8520SAlexander Graf continue; 921c9f8520SAlexander Graf 931c9f8520SAlexander Graf ret = r + ((ret < 0) ? 0 : ret); 941c9f8520SAlexander Graf } 951c9f8520SAlexander Graf 961c9f8520SAlexander Graf return ret; 971c9f8520SAlexander Graf } 981c9f8520SAlexander Graf 99e73f61e4SJoerg Roedel static void free_irq_routing_table(struct kvm_irq_routing_table *rt) 100e73f61e4SJoerg Roedel { 101e73f61e4SJoerg Roedel int i; 102e73f61e4SJoerg Roedel 103e73f61e4SJoerg Roedel if (!rt) 104e73f61e4SJoerg Roedel return; 105e73f61e4SJoerg Roedel 106e73f61e4SJoerg Roedel for (i = 0; i < rt->nr_rt_entries; ++i) { 107e73f61e4SJoerg Roedel struct kvm_kernel_irq_routing_entry *e; 108e73f61e4SJoerg Roedel struct hlist_node *n; 109e73f61e4SJoerg Roedel 110e73f61e4SJoerg Roedel hlist_for_each_entry_safe(e, n, &rt->map[i], link) { 111e73f61e4SJoerg Roedel hlist_del(&e->link); 112e73f61e4SJoerg Roedel kfree(e); 113e73f61e4SJoerg Roedel } 114e73f61e4SJoerg Roedel } 115e73f61e4SJoerg Roedel 116e73f61e4SJoerg Roedel kfree(rt); 117e73f61e4SJoerg Roedel } 118e73f61e4SJoerg Roedel 1191c9f8520SAlexander Graf void kvm_free_irq_routing(struct kvm *kvm) 1201c9f8520SAlexander Graf { 1211c9f8520SAlexander Graf /* Called only during vm destruction. Nobody can use the pointer 1221c9f8520SAlexander Graf at this stage */ 123e73f61e4SJoerg Roedel struct kvm_irq_routing_table *rt = rcu_access_pointer(kvm->irq_routing); 124e73f61e4SJoerg Roedel free_irq_routing_table(rt); 1251c9f8520SAlexander Graf } 126e8cde093SAlexander Graf 127c63cf538SRadim Krčmář static int setup_routing_entry(struct kvm *kvm, 128c63cf538SRadim Krčmář struct kvm_irq_routing_table *rt, 129e8cde093SAlexander Graf struct kvm_kernel_irq_routing_entry *e, 130e8cde093SAlexander Graf const struct kvm_irq_routing_entry *ue) 131e8cde093SAlexander Graf { 132e8cde093SAlexander Graf struct kvm_kernel_irq_routing_entry *ei; 1338c6b7828SDavid Hildenbrand int r; 1341d487e9bSPaolo Bonzini u32 gsi = array_index_nospec(ue->gsi, KVM_MAX_IRQ_ROUTES); 135e8cde093SAlexander Graf 136e8cde093SAlexander Graf /* 137e8cde093SAlexander Graf * Do not allow GSI to be mapped to the same irqchip more than once. 138f33143d8SAndrey Smetanin * Allow only one to one mapping between GSI and non-irqchip routing. 139e8cde093SAlexander Graf */ 1401d487e9bSPaolo Bonzini hlist_for_each_entry(ei, &rt->map[gsi], link) 141f33143d8SAndrey Smetanin if (ei->type != KVM_IRQ_ROUTING_IRQCHIP || 142f33143d8SAndrey Smetanin ue->type != KVM_IRQ_ROUTING_IRQCHIP || 143e8cde093SAlexander Graf ue->u.irqchip.irqchip == ei->irqchip.irqchip) 1448c6b7828SDavid Hildenbrand return -EINVAL; 145e8cde093SAlexander Graf 1461d487e9bSPaolo Bonzini e->gsi = gsi; 147e8cde093SAlexander Graf e->type = ue->type; 148c63cf538SRadim Krčmář r = kvm_set_routing_entry(kvm, e, ue); 149e8cde093SAlexander Graf if (r) 1508c6b7828SDavid Hildenbrand return r; 1518ba918d4SPaul Mackerras if (e->type == KVM_IRQ_ROUTING_IRQCHIP) 1528ba918d4SPaul Mackerras rt->chip[e->irqchip.irqchip][e->irqchip.pin] = e->gsi; 153e8cde093SAlexander Graf 154e8cde093SAlexander Graf hlist_add_head(&e->link, &rt->map[e->gsi]); 1558c6b7828SDavid Hildenbrand 1568c6b7828SDavid Hildenbrand return 0; 157e8cde093SAlexander Graf } 158e8cde093SAlexander Graf 159abdb080fSAndrey Smetanin void __attribute__((weak)) kvm_arch_irq_routing_update(struct kvm *kvm) 160abdb080fSAndrey Smetanin { 161abdb080fSAndrey Smetanin } 162abdb080fSAndrey Smetanin 1635c0aea0eSDavid Hildenbrand bool __weak kvm_arch_can_set_irq_routing(struct kvm *kvm) 1645c0aea0eSDavid Hildenbrand { 1655c0aea0eSDavid Hildenbrand return true; 1665c0aea0eSDavid Hildenbrand } 1675c0aea0eSDavid Hildenbrand 168e8cde093SAlexander Graf int kvm_set_irq_routing(struct kvm *kvm, 169e8cde093SAlexander Graf const struct kvm_irq_routing_entry *ue, 170e8cde093SAlexander Graf unsigned nr, 171e8cde093SAlexander Graf unsigned flags) 172e8cde093SAlexander Graf { 173e8cde093SAlexander Graf struct kvm_irq_routing_table *new, *old; 174995a0ee9SEric Auger struct kvm_kernel_irq_routing_entry *e; 175e8cde093SAlexander Graf u32 i, j, nr_rt_entries = 0; 176e8cde093SAlexander Graf int r; 177e8cde093SAlexander Graf 178e8cde093SAlexander Graf for (i = 0; i < nr; ++i) { 179e8cde093SAlexander Graf if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES) 180e8cde093SAlexander Graf return -EINVAL; 181e8cde093SAlexander Graf nr_rt_entries = max(nr_rt_entries, ue[i].gsi); 182e8cde093SAlexander Graf } 183e8cde093SAlexander Graf 184e8cde093SAlexander Graf nr_rt_entries += 1; 185e8cde093SAlexander Graf 186b3ffd74aSGustavo A. R. Silva new = kzalloc(struct_size(new, map, nr_rt_entries), GFP_KERNEL_ACCOUNT); 187e8cde093SAlexander Graf if (!new) 188e8cde093SAlexander Graf return -ENOMEM; 189e8cde093SAlexander Graf 190e8cde093SAlexander Graf new->nr_rt_entries = nr_rt_entries; 191e8cde093SAlexander Graf for (i = 0; i < KVM_NR_IRQCHIPS; i++) 192e8cde093SAlexander Graf for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++) 193e8cde093SAlexander Graf new->chip[i][j] = -1; 194e8cde093SAlexander Graf 195e8cde093SAlexander Graf for (i = 0; i < nr; ++i) { 196e73f61e4SJoerg Roedel r = -ENOMEM; 197b12ce36aSBen Gardon e = kzalloc(sizeof(*e), GFP_KERNEL_ACCOUNT); 198e73f61e4SJoerg Roedel if (!e) 199e73f61e4SJoerg Roedel goto out; 200e73f61e4SJoerg Roedel 201e8cde093SAlexander Graf r = -EINVAL; 202995a0ee9SEric Auger switch (ue->type) { 203995a0ee9SEric Auger case KVM_IRQ_ROUTING_MSI: 204995a0ee9SEric Auger if (ue->flags & ~KVM_MSI_VALID_DEVID) 205995a0ee9SEric Auger goto free_entry; 206995a0ee9SEric Auger break; 207995a0ee9SEric Auger default: 208995a0ee9SEric Auger if (ue->flags) 209995a0ee9SEric Auger goto free_entry; 210995a0ee9SEric Auger break; 211ba60c41aSSudip Mukherjee } 212c63cf538SRadim Krčmář r = setup_routing_entry(kvm, new, e, ue); 213995a0ee9SEric Auger if (r) 214995a0ee9SEric Auger goto free_entry; 215e8cde093SAlexander Graf ++ue; 216e8cde093SAlexander Graf } 217e8cde093SAlexander Graf 218e8cde093SAlexander Graf mutex_lock(&kvm->irq_lock); 2195535f800SChristian Borntraeger old = rcu_dereference_protected(kvm->irq_routing, 1); 2209957c86dSPaul Mackerras rcu_assign_pointer(kvm->irq_routing, new); 2219957c86dSPaul Mackerras kvm_irq_routing_update(kvm); 222abdb080fSAndrey Smetanin kvm_arch_irq_routing_update(kvm); 223e8cde093SAlexander Graf mutex_unlock(&kvm->irq_lock); 224e8cde093SAlexander Graf 225abdb080fSAndrey Smetanin kvm_arch_post_irq_routing_update(kvm); 226b053b2aeSSteve Rutherford 227719d93cdSChristian Borntraeger synchronize_srcu_expedited(&kvm->irq_srcu); 228e8cde093SAlexander Graf 229e8cde093SAlexander Graf new = old; 230e8cde093SAlexander Graf r = 0; 231995a0ee9SEric Auger goto out; 232e8cde093SAlexander Graf 233995a0ee9SEric Auger free_entry: 234995a0ee9SEric Auger kfree(e); 235e8cde093SAlexander Graf out: 236e73f61e4SJoerg Roedel free_irq_routing_table(new); 237e73f61e4SJoerg Roedel 238e8cde093SAlexander Graf return r; 239e8cde093SAlexander Graf } 240*fbe4a7e8SYi Wang 241*fbe4a7e8SYi Wang /* 242*fbe4a7e8SYi Wang * Allocate empty IRQ routing by default so that additional setup isn't needed 243*fbe4a7e8SYi Wang * when userspace-driven IRQ routing is activated, and so that kvm->irq_routing 244*fbe4a7e8SYi Wang * is guaranteed to be non-NULL. 245*fbe4a7e8SYi Wang */ 246*fbe4a7e8SYi Wang int kvm_init_irq_routing(struct kvm *kvm) 247*fbe4a7e8SYi Wang { 248*fbe4a7e8SYi Wang struct kvm_irq_routing_table *new; 249*fbe4a7e8SYi Wang int chip_size; 250*fbe4a7e8SYi Wang 251*fbe4a7e8SYi Wang new = kzalloc(struct_size(new, map, 1), GFP_KERNEL_ACCOUNT); 252*fbe4a7e8SYi Wang if (!new) 253*fbe4a7e8SYi Wang return -ENOMEM; 254*fbe4a7e8SYi Wang 255*fbe4a7e8SYi Wang new->nr_rt_entries = 1; 256*fbe4a7e8SYi Wang 257*fbe4a7e8SYi Wang chip_size = sizeof(int) * KVM_NR_IRQCHIPS * KVM_IRQCHIP_NUM_PINS; 258*fbe4a7e8SYi Wang memset(new->chip, -1, chip_size); 259*fbe4a7e8SYi Wang 260*fbe4a7e8SYi Wang RCU_INIT_POINTER(kvm->irq_routing, new); 261*fbe4a7e8SYi Wang 262*fbe4a7e8SYi Wang return 0; 263*fbe4a7e8SYi Wang } 264