xref: /linux/arch/x86/platform/uv/uv_irq.c (revision f8409a6a4bf86e2d90ec8460df2874e4e19ebb27)
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;
38329b84e4SThomas Gleixner 	entry->delivery_mode	= apic->irq_delivery_mode;
39329b84e4SThomas Gleixner 	entry->dest_mode	= apic->irq_dest_mode;
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 
5043fe1abcSJiang Liu static void uv_ack_apic(struct irq_data *data)
51329b84e4SThomas Gleixner {
5243fe1abcSJiang Liu 	ack_APIC_irq();
53329b84e4SThomas Gleixner }
54329b84e4SThomas Gleixner 
55329b84e4SThomas Gleixner static int
56329b84e4SThomas Gleixner uv_set_irq_affinity(struct irq_data *data, const struct cpumask *mask,
57329b84e4SThomas Gleixner 		    bool force)
58329b84e4SThomas Gleixner {
5943fe1abcSJiang Liu 	struct irq_data *parent = data->parent_data;
60a9786091SJiang Liu 	struct irq_cfg *cfg = irqd_cfg(data);
6143fe1abcSJiang Liu 	int ret;
62329b84e4SThomas Gleixner 
6343fe1abcSJiang Liu 	ret = parent->chip->irq_set_affinity(parent, mask, force);
6443fe1abcSJiang Liu 	if (ret >= 0) {
6543fe1abcSJiang Liu 		uv_program_mmr(cfg, data->chip_data);
6643fe1abcSJiang Liu 		send_cleanup_vector(cfg);
6743fe1abcSJiang Liu 	}
6843fe1abcSJiang Liu 
6943fe1abcSJiang Liu 	return ret;
7043fe1abcSJiang Liu }
7143fe1abcSJiang Liu 
7243fe1abcSJiang Liu static struct irq_chip uv_irq_chip = {
7343fe1abcSJiang Liu 	.name			= "UV-CORE",
7443fe1abcSJiang Liu 	.irq_mask		= uv_noop,
7543fe1abcSJiang Liu 	.irq_unmask		= uv_noop,
7643fe1abcSJiang Liu 	.irq_eoi		= uv_ack_apic,
7743fe1abcSJiang Liu 	.irq_set_affinity	= uv_set_irq_affinity,
7843fe1abcSJiang Liu };
7943fe1abcSJiang Liu 
8043fe1abcSJiang Liu static int uv_domain_alloc(struct irq_domain *domain, unsigned int virq,
8143fe1abcSJiang Liu 			   unsigned int nr_irqs, void *arg)
8243fe1abcSJiang Liu {
8343fe1abcSJiang Liu 	struct uv_irq_2_mmr_pnode *chip_data;
8443fe1abcSJiang Liu 	struct irq_alloc_info *info = arg;
8543fe1abcSJiang Liu 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
8643fe1abcSJiang Liu 	int ret;
8743fe1abcSJiang Liu 
8843fe1abcSJiang Liu 	if (nr_irqs > 1 || !info || info->type != X86_IRQ_ALLOC_TYPE_UV)
8943fe1abcSJiang Liu 		return -EINVAL;
9043fe1abcSJiang Liu 
9143fe1abcSJiang Liu 	chip_data = kmalloc_node(sizeof(*chip_data), GFP_KERNEL,
925f2dbbc5SJiang Liu 				 irq_data_get_node(irq_data));
9343fe1abcSJiang Liu 	if (!chip_data)
9443fe1abcSJiang Liu 		return -ENOMEM;
9543fe1abcSJiang Liu 
9643fe1abcSJiang Liu 	ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
9743fe1abcSJiang Liu 	if (ret >= 0) {
9843fe1abcSJiang Liu 		if (info->uv_limit == UV_AFFINITY_CPU)
9943fe1abcSJiang Liu 			irq_set_status_flags(virq, IRQ_NO_BALANCING);
10043fe1abcSJiang Liu 		else
10143fe1abcSJiang Liu 			irq_set_status_flags(virq, IRQ_MOVE_PCNTXT);
10243fe1abcSJiang Liu 
10343fe1abcSJiang Liu 		chip_data->pnode = uv_blade_to_pnode(info->uv_blade);
10443fe1abcSJiang Liu 		chip_data->offset = info->uv_offset;
10543fe1abcSJiang Liu 		irq_domain_set_info(domain, virq, virq, &uv_irq_chip, chip_data,
10643fe1abcSJiang Liu 				    handle_percpu_irq, NULL, info->uv_name);
10743fe1abcSJiang Liu 	} else {
10843fe1abcSJiang Liu 		kfree(chip_data);
10943fe1abcSJiang Liu 	}
11043fe1abcSJiang Liu 
11143fe1abcSJiang Liu 	return ret;
11243fe1abcSJiang Liu }
11343fe1abcSJiang Liu 
11443fe1abcSJiang Liu static void uv_domain_free(struct irq_domain *domain, unsigned int virq,
11543fe1abcSJiang Liu 			   unsigned int nr_irqs)
11643fe1abcSJiang Liu {
11743fe1abcSJiang Liu 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
11843fe1abcSJiang Liu 
11943fe1abcSJiang Liu 	BUG_ON(nr_irqs != 1);
12043fe1abcSJiang Liu 	kfree(irq_data->chip_data);
12143fe1abcSJiang Liu 	irq_clear_status_flags(virq, IRQ_MOVE_PCNTXT);
12243fe1abcSJiang Liu 	irq_clear_status_flags(virq, IRQ_NO_BALANCING);
12343fe1abcSJiang Liu 	irq_domain_free_irqs_top(domain, virq, nr_irqs);
12443fe1abcSJiang Liu }
12543fe1abcSJiang Liu 
12643fe1abcSJiang Liu /*
12743fe1abcSJiang Liu  * Re-target the irq to the specified CPU and enable the specified MMR located
12843fe1abcSJiang Liu  * on the specified blade to allow the sending of MSIs to the specified CPU.
12943fe1abcSJiang Liu  */
13043fe1abcSJiang Liu static void uv_domain_activate(struct irq_domain *domain,
13143fe1abcSJiang Liu 			       struct irq_data *irq_data)
13243fe1abcSJiang Liu {
13343fe1abcSJiang Liu 	uv_program_mmr(irqd_cfg(irq_data), irq_data->chip_data);
13443fe1abcSJiang Liu }
13543fe1abcSJiang Liu 
13643fe1abcSJiang Liu /*
13743fe1abcSJiang Liu  * Disable the specified MMR located on the specified blade so that MSIs are
13843fe1abcSJiang Liu  * longer allowed to be sent.
13943fe1abcSJiang Liu  */
14043fe1abcSJiang Liu static void uv_domain_deactivate(struct irq_domain *domain,
14143fe1abcSJiang Liu 				 struct irq_data *irq_data)
14243fe1abcSJiang Liu {
14343fe1abcSJiang Liu 	unsigned long mmr_value;
14443fe1abcSJiang Liu 	struct uv_IO_APIC_route_entry *entry;
145329b84e4SThomas Gleixner 
146329b84e4SThomas Gleixner 	mmr_value = 0;
147329b84e4SThomas Gleixner 	entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
14843fe1abcSJiang Liu 	entry->mask = 1;
14943fe1abcSJiang Liu 	uv_program_mmr(irqd_cfg(irq_data), irq_data->chip_data);
15043fe1abcSJiang Liu }
151329b84e4SThomas Gleixner 
152eb18cf55SThomas Gleixner static const struct irq_domain_ops uv_domain_ops = {
15343fe1abcSJiang Liu 	.alloc		= uv_domain_alloc,
15443fe1abcSJiang Liu 	.free		= uv_domain_free,
15543fe1abcSJiang Liu 	.activate	= uv_domain_activate,
15643fe1abcSJiang Liu 	.deactivate	= uv_domain_deactivate,
15743fe1abcSJiang Liu };
158329b84e4SThomas Gleixner 
15943fe1abcSJiang Liu static struct irq_domain *uv_get_irq_domain(void)
16043fe1abcSJiang Liu {
16143fe1abcSJiang Liu 	static struct irq_domain *uv_domain;
16243fe1abcSJiang Liu 	static DEFINE_MUTEX(uv_lock);
163*f8409a6aSThomas Gleixner 	struct fwnode_handle *fn;
164329b84e4SThomas Gleixner 
16543fe1abcSJiang Liu 	mutex_lock(&uv_lock);
166*f8409a6aSThomas Gleixner 	if (uv_domain)
167*f8409a6aSThomas Gleixner 		goto out;
168*f8409a6aSThomas Gleixner 
169*f8409a6aSThomas Gleixner 	fn = irq_domain_alloc_named_fwnode("UV-CORE");
170*f8409a6aSThomas Gleixner 	if (!fn)
171*f8409a6aSThomas Gleixner 		goto out;
172*f8409a6aSThomas Gleixner 
173*f8409a6aSThomas Gleixner 	uv_domain = irq_domain_create_tree(fn, &uv_domain_ops, NULL);
174*f8409a6aSThomas Gleixner 	irq_domain_free_fwnode(fn);
17543fe1abcSJiang Liu 	if (uv_domain)
17643fe1abcSJiang Liu 		uv_domain->parent = x86_vector_domain;
177*f8409a6aSThomas Gleixner out:
17843fe1abcSJiang Liu 	mutex_unlock(&uv_lock);
179329b84e4SThomas Gleixner 
18043fe1abcSJiang Liu 	return uv_domain;
181329b84e4SThomas Gleixner }
182329b84e4SThomas Gleixner 
183329b84e4SThomas Gleixner /*
184329b84e4SThomas Gleixner  * Set up a mapping of an available irq and vector, and enable the specified
185329b84e4SThomas Gleixner  * MMR that defines the MSI that is to be sent to the specified CPU when an
186329b84e4SThomas Gleixner  * interrupt is raised.
187329b84e4SThomas Gleixner  */
188329b84e4SThomas Gleixner int uv_setup_irq(char *irq_name, int cpu, int mmr_blade,
189329b84e4SThomas Gleixner 		 unsigned long mmr_offset, int limit)
190329b84e4SThomas Gleixner {
191331dd19eSJiang Liu 	struct irq_alloc_info info;
19243fe1abcSJiang Liu 	struct irq_domain *domain = uv_get_irq_domain();
19343fe1abcSJiang Liu 
19443fe1abcSJiang Liu 	if (!domain)
19543fe1abcSJiang Liu 		return -ENOMEM;
196329b84e4SThomas Gleixner 
197331dd19eSJiang Liu 	init_irq_alloc_info(&info, cpumask_of(cpu));
19843fe1abcSJiang Liu 	info.type = X86_IRQ_ALLOC_TYPE_UV;
19943fe1abcSJiang Liu 	info.uv_limit = limit;
20043fe1abcSJiang Liu 	info.uv_blade = mmr_blade;
20143fe1abcSJiang Liu 	info.uv_offset = mmr_offset;
20243fe1abcSJiang Liu 	info.uv_name = irq_name;
203329b84e4SThomas Gleixner 
20443fe1abcSJiang Liu 	return irq_domain_alloc_irqs(domain, 1,
20543fe1abcSJiang Liu 				     uv_blade_to_memory_nid(mmr_blade), &info);
206329b84e4SThomas Gleixner }
207329b84e4SThomas Gleixner EXPORT_SYMBOL_GPL(uv_setup_irq);
208329b84e4SThomas Gleixner 
209329b84e4SThomas Gleixner /*
210329b84e4SThomas Gleixner  * Tear down a mapping of an irq and vector, and disable the specified MMR that
211329b84e4SThomas Gleixner  * defined the MSI that was to be sent to the specified CPU when an interrupt
212329b84e4SThomas Gleixner  * was raised.
213329b84e4SThomas Gleixner  *
214329b84e4SThomas Gleixner  * Set mmr_blade and mmr_offset to what was passed in on uv_setup_irq().
215329b84e4SThomas Gleixner  */
216329b84e4SThomas Gleixner void uv_teardown_irq(unsigned int irq)
217329b84e4SThomas Gleixner {
218331dd19eSJiang Liu 	irq_domain_free_irqs(irq, 1);
219329b84e4SThomas Gleixner }
220329b84e4SThomas Gleixner EXPORT_SYMBOL_GPL(uv_teardown_irq);
221