1329b84e4SThomas Gleixner /* 2329b84e4SThomas Gleixner * This file is subject to the terms and conditions of the GNU General Public 3329b84e4SThomas Gleixner * License. See the file "COPYING" in the main directory of this archive 4329b84e4SThomas Gleixner * for more details. 5329b84e4SThomas Gleixner * 6329b84e4SThomas Gleixner * SGI UV IRQ functions 7329b84e4SThomas Gleixner * 8329b84e4SThomas Gleixner * Copyright (C) 2008 Silicon Graphics, Inc. All rights reserved. 9329b84e4SThomas Gleixner */ 10329b84e4SThomas Gleixner 11cc3ae7b0SPaul Gortmaker #include <linux/export.h> 12329b84e4SThomas Gleixner #include <linux/rbtree.h> 13329b84e4SThomas Gleixner #include <linux/slab.h> 14329b84e4SThomas Gleixner #include <linux/irq.h> 15329b84e4SThomas Gleixner 16d746d1ebSJiang Liu #include <asm/irqdomain.h> 17329b84e4SThomas Gleixner #include <asm/apic.h> 18329b84e4SThomas Gleixner #include <asm/uv/uv_irq.h> 19329b84e4SThomas Gleixner #include <asm/uv/uv_hub.h> 20329b84e4SThomas Gleixner 21329b84e4SThomas Gleixner /* MMR offset and pnode of hub sourcing interrupts for a given irq */ 22329b84e4SThomas Gleixner struct uv_irq_2_mmr_pnode { 23329b84e4SThomas Gleixner unsigned long offset; 24329b84e4SThomas Gleixner int pnode; 25329b84e4SThomas Gleixner }; 26329b84e4SThomas Gleixner 2743fe1abcSJiang Liu static void uv_program_mmr(struct irq_cfg *cfg, struct uv_irq_2_mmr_pnode *info) 28329b84e4SThomas Gleixner { 29329b84e4SThomas Gleixner unsigned long mmr_value; 30329b84e4SThomas Gleixner struct uv_IO_APIC_route_entry *entry; 31329b84e4SThomas Gleixner 32329b84e4SThomas Gleixner BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != 33329b84e4SThomas Gleixner sizeof(unsigned long)); 34329b84e4SThomas Gleixner 35329b84e4SThomas Gleixner mmr_value = 0; 36329b84e4SThomas Gleixner entry = (struct uv_IO_APIC_route_entry *)&mmr_value; 37329b84e4SThomas Gleixner entry->vector = cfg->vector; 3872161299SThomas Gleixner entry->delivery_mode = apic->delivery_mode; 39*8c44963bSThomas Gleixner entry->dest_mode = apic->dest_mode_logical; 40329b84e4SThomas Gleixner entry->polarity = 0; 41329b84e4SThomas Gleixner entry->trigger = 0; 42329b84e4SThomas Gleixner entry->mask = 0; 43331dd19eSJiang Liu entry->dest = cfg->dest_apicid; 44329b84e4SThomas Gleixner 4543fe1abcSJiang Liu uv_write_global_mmr64(info->pnode, info->offset, mmr_value); 46329b84e4SThomas Gleixner } 47329b84e4SThomas Gleixner 4843fe1abcSJiang Liu static void uv_noop(struct irq_data *data) { } 4943fe1abcSJiang Liu 50329b84e4SThomas Gleixner static int 51329b84e4SThomas Gleixner uv_set_irq_affinity(struct irq_data *data, const struct cpumask *mask, 52329b84e4SThomas Gleixner bool force) 53329b84e4SThomas Gleixner { 5443fe1abcSJiang Liu struct irq_data *parent = data->parent_data; 55a9786091SJiang Liu struct irq_cfg *cfg = irqd_cfg(data); 5643fe1abcSJiang Liu int ret; 57329b84e4SThomas Gleixner 5843fe1abcSJiang Liu ret = parent->chip->irq_set_affinity(parent, mask, force); 5943fe1abcSJiang Liu if (ret >= 0) { 6043fe1abcSJiang Liu uv_program_mmr(cfg, data->chip_data); 6143fe1abcSJiang Liu send_cleanup_vector(cfg); 6243fe1abcSJiang Liu } 6343fe1abcSJiang Liu 6443fe1abcSJiang Liu return ret; 6543fe1abcSJiang Liu } 6643fe1abcSJiang Liu 6743fe1abcSJiang Liu static struct irq_chip uv_irq_chip = { 6843fe1abcSJiang Liu .name = "UV-CORE", 6943fe1abcSJiang Liu .irq_mask = uv_noop, 7043fe1abcSJiang Liu .irq_unmask = uv_noop, 71839b0f1cSThomas Gleixner .irq_eoi = apic_ack_irq, 7243fe1abcSJiang Liu .irq_set_affinity = uv_set_irq_affinity, 7343fe1abcSJiang Liu }; 7443fe1abcSJiang Liu 7543fe1abcSJiang Liu static int uv_domain_alloc(struct irq_domain *domain, unsigned int virq, 7643fe1abcSJiang Liu unsigned int nr_irqs, void *arg) 7743fe1abcSJiang Liu { 7843fe1abcSJiang Liu struct uv_irq_2_mmr_pnode *chip_data; 7943fe1abcSJiang Liu struct irq_alloc_info *info = arg; 8043fe1abcSJiang Liu struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq); 8143fe1abcSJiang Liu int ret; 8243fe1abcSJiang Liu 8343fe1abcSJiang Liu if (nr_irqs > 1 || !info || info->type != X86_IRQ_ALLOC_TYPE_UV) 8443fe1abcSJiang Liu return -EINVAL; 8543fe1abcSJiang Liu 8643fe1abcSJiang Liu chip_data = kmalloc_node(sizeof(*chip_data), GFP_KERNEL, 875f2dbbc5SJiang Liu irq_data_get_node(irq_data)); 8843fe1abcSJiang Liu if (!chip_data) 8943fe1abcSJiang Liu return -ENOMEM; 9043fe1abcSJiang Liu 9143fe1abcSJiang Liu ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); 9243fe1abcSJiang Liu if (ret >= 0) { 930f5cbdafSThomas Gleixner if (info->uv.limit == UV_AFFINITY_CPU) 9443fe1abcSJiang Liu irq_set_status_flags(virq, IRQ_NO_BALANCING); 9543fe1abcSJiang Liu else 9643fe1abcSJiang Liu irq_set_status_flags(virq, IRQ_MOVE_PCNTXT); 9743fe1abcSJiang Liu 980f5cbdafSThomas Gleixner chip_data->pnode = uv_blade_to_pnode(info->uv.blade); 990f5cbdafSThomas Gleixner chip_data->offset = info->uv.offset; 10043fe1abcSJiang Liu irq_domain_set_info(domain, virq, virq, &uv_irq_chip, chip_data, 1010f5cbdafSThomas Gleixner handle_percpu_irq, NULL, info->uv.name); 10243fe1abcSJiang Liu } else { 10343fe1abcSJiang Liu kfree(chip_data); 10443fe1abcSJiang Liu } 10543fe1abcSJiang Liu 10643fe1abcSJiang Liu return ret; 10743fe1abcSJiang Liu } 10843fe1abcSJiang Liu 10943fe1abcSJiang Liu static void uv_domain_free(struct irq_domain *domain, unsigned int virq, 11043fe1abcSJiang Liu unsigned int nr_irqs) 11143fe1abcSJiang Liu { 11243fe1abcSJiang Liu struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq); 11343fe1abcSJiang Liu 11443fe1abcSJiang Liu BUG_ON(nr_irqs != 1); 11543fe1abcSJiang Liu kfree(irq_data->chip_data); 11643fe1abcSJiang Liu irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT); 11743fe1abcSJiang Liu irq_clear_status_flags(virq, IRQ_NO_BALANCING); 11843fe1abcSJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs); 11943fe1abcSJiang Liu } 12043fe1abcSJiang Liu 12143fe1abcSJiang Liu /* 12243fe1abcSJiang Liu * Re-target the irq to the specified CPU and enable the specified MMR located 12343fe1abcSJiang Liu * on the specified blade to allow the sending of MSIs to the specified CPU. 12443fe1abcSJiang Liu */ 12572491643SThomas Gleixner static int uv_domain_activate(struct irq_domain *domain, 126702cb0a0SThomas Gleixner struct irq_data *irq_data, bool reserve) 12743fe1abcSJiang Liu { 12843fe1abcSJiang Liu uv_program_mmr(irqd_cfg(irq_data), irq_data->chip_data); 12972491643SThomas Gleixner return 0; 13043fe1abcSJiang Liu } 13143fe1abcSJiang Liu 13243fe1abcSJiang Liu /* 13343fe1abcSJiang Liu * Disable the specified MMR located on the specified blade so that MSIs are 13443fe1abcSJiang Liu * longer allowed to be sent. 13543fe1abcSJiang Liu */ 13643fe1abcSJiang Liu static void uv_domain_deactivate(struct irq_domain *domain, 13743fe1abcSJiang Liu struct irq_data *irq_data) 13843fe1abcSJiang Liu { 13943fe1abcSJiang Liu unsigned long mmr_value; 14043fe1abcSJiang Liu struct uv_IO_APIC_route_entry *entry; 141329b84e4SThomas Gleixner 142329b84e4SThomas Gleixner mmr_value = 0; 143329b84e4SThomas Gleixner entry = (struct uv_IO_APIC_route_entry *)&mmr_value; 14443fe1abcSJiang Liu entry->mask = 1; 14543fe1abcSJiang Liu uv_program_mmr(irqd_cfg(irq_data), irq_data->chip_data); 14643fe1abcSJiang Liu } 147329b84e4SThomas Gleixner 148eb18cf55SThomas Gleixner static const struct irq_domain_ops uv_domain_ops = { 14943fe1abcSJiang Liu .alloc = uv_domain_alloc, 15043fe1abcSJiang Liu .free = uv_domain_free, 15143fe1abcSJiang Liu .activate = uv_domain_activate, 15243fe1abcSJiang Liu .deactivate = uv_domain_deactivate, 15343fe1abcSJiang Liu }; 154329b84e4SThomas Gleixner 15543fe1abcSJiang Liu static struct irq_domain *uv_get_irq_domain(void) 15643fe1abcSJiang Liu { 15743fe1abcSJiang Liu static struct irq_domain *uv_domain; 15843fe1abcSJiang Liu static DEFINE_MUTEX(uv_lock); 159f8409a6aSThomas Gleixner struct fwnode_handle *fn; 160329b84e4SThomas Gleixner 16143fe1abcSJiang Liu mutex_lock(&uv_lock); 162f8409a6aSThomas Gleixner if (uv_domain) 163f8409a6aSThomas Gleixner goto out; 164f8409a6aSThomas Gleixner 165f8409a6aSThomas Gleixner fn = irq_domain_alloc_named_fwnode("UV-CORE"); 166f8409a6aSThomas Gleixner if (!fn) 167f8409a6aSThomas Gleixner goto out; 168f8409a6aSThomas Gleixner 169f8409a6aSThomas Gleixner uv_domain = irq_domain_create_tree(fn, &uv_domain_ops, NULL); 17043fe1abcSJiang Liu if (uv_domain) 17143fe1abcSJiang Liu uv_domain->parent = x86_vector_domain; 172e3beca48SThomas Gleixner else 173e3beca48SThomas Gleixner irq_domain_free_fwnode(fn); 174f8409a6aSThomas Gleixner out: 17543fe1abcSJiang Liu mutex_unlock(&uv_lock); 176329b84e4SThomas Gleixner 17743fe1abcSJiang Liu return uv_domain; 178329b84e4SThomas Gleixner } 179329b84e4SThomas Gleixner 180329b84e4SThomas Gleixner /* 181329b84e4SThomas Gleixner * Set up a mapping of an available irq and vector, and enable the specified 182329b84e4SThomas Gleixner * MMR that defines the MSI that is to be sent to the specified CPU when an 183329b84e4SThomas Gleixner * interrupt is raised. 184329b84e4SThomas Gleixner */ 185329b84e4SThomas Gleixner int uv_setup_irq(char *irq_name, int cpu, int mmr_blade, 186329b84e4SThomas Gleixner unsigned long mmr_offset, int limit) 187329b84e4SThomas Gleixner { 188331dd19eSJiang Liu struct irq_alloc_info info; 18943fe1abcSJiang Liu struct irq_domain *domain = uv_get_irq_domain(); 19043fe1abcSJiang Liu 19143fe1abcSJiang Liu if (!domain) 19243fe1abcSJiang Liu return -ENOMEM; 193329b84e4SThomas Gleixner 194331dd19eSJiang Liu init_irq_alloc_info(&info, cpumask_of(cpu)); 19543fe1abcSJiang Liu info.type = X86_IRQ_ALLOC_TYPE_UV; 1960f5cbdafSThomas Gleixner info.uv.limit = limit; 1970f5cbdafSThomas Gleixner info.uv.blade = mmr_blade; 1980f5cbdafSThomas Gleixner info.uv.offset = mmr_offset; 1990f5cbdafSThomas Gleixner info.uv.name = irq_name; 200329b84e4SThomas Gleixner 20143fe1abcSJiang Liu return irq_domain_alloc_irqs(domain, 1, 20243fe1abcSJiang Liu uv_blade_to_memory_nid(mmr_blade), &info); 203329b84e4SThomas Gleixner } 204329b84e4SThomas Gleixner EXPORT_SYMBOL_GPL(uv_setup_irq); 205329b84e4SThomas Gleixner 206329b84e4SThomas Gleixner /* 207329b84e4SThomas Gleixner * Tear down a mapping of an irq and vector, and disable the specified MMR that 208329b84e4SThomas Gleixner * defined the MSI that was to be sent to the specified CPU when an interrupt 209329b84e4SThomas Gleixner * was raised. 210329b84e4SThomas Gleixner * 211329b84e4SThomas Gleixner * Set mmr_blade and mmr_offset to what was passed in on uv_setup_irq(). 212329b84e4SThomas Gleixner */ 213329b84e4SThomas Gleixner void uv_teardown_irq(unsigned int irq) 214329b84e4SThomas Gleixner { 215331dd19eSJiang Liu irq_domain_free_irqs(irq, 1); 216329b84e4SThomas Gleixner } 217329b84e4SThomas Gleixner EXPORT_SYMBOL_GPL(uv_teardown_irq); 218