xref: /openbmc/linux/kernel/irq/ipi.c (revision 01292cea)
1d17bf24eSQais Yousef /*
2d17bf24eSQais Yousef  * linux/kernel/irq/ipi.c
3d17bf24eSQais Yousef  *
4d17bf24eSQais Yousef  * Copyright (C) 2015 Imagination Technologies Ltd
5d17bf24eSQais Yousef  * Author: Qais Yousef <qais.yousef@imgtec.com>
6d17bf24eSQais Yousef  *
7d17bf24eSQais Yousef  * This file contains driver APIs to the IPI subsystem.
8d17bf24eSQais Yousef  */
9d17bf24eSQais Yousef 
10d17bf24eSQais Yousef #define pr_fmt(fmt) "genirq/ipi: " fmt
11d17bf24eSQais Yousef 
12d17bf24eSQais Yousef #include <linux/irqdomain.h>
13d17bf24eSQais Yousef #include <linux/irq.h>
14d17bf24eSQais Yousef 
15d17bf24eSQais Yousef /**
16d17bf24eSQais Yousef  * irq_reserve_ipi() - Setup an IPI to destination cpumask
17d17bf24eSQais Yousef  * @domain:	IPI domain
18d17bf24eSQais Yousef  * @dest:	cpumask of cpus which can receive the IPI
19d17bf24eSQais Yousef  *
20d17bf24eSQais Yousef  * Allocate a virq that can be used to send IPI to any CPU in dest mask.
21d17bf24eSQais Yousef  *
22d17bf24eSQais Yousef  * On success it'll return linux irq number and 0 on failure
23d17bf24eSQais Yousef  */
24d17bf24eSQais Yousef unsigned int irq_reserve_ipi(struct irq_domain *domain,
25d17bf24eSQais Yousef 			     const struct cpumask *dest)
26d17bf24eSQais Yousef {
27d17bf24eSQais Yousef 	unsigned int nr_irqs, offset;
28d17bf24eSQais Yousef 	struct irq_data *data;
29d17bf24eSQais Yousef 	int virq, i;
30d17bf24eSQais Yousef 
31d17bf24eSQais Yousef 	if (!domain ||!irq_domain_is_ipi(domain)) {
32d17bf24eSQais Yousef 		pr_warn("Reservation on a non IPI domain\n");
33d17bf24eSQais Yousef 		return 0;
34d17bf24eSQais Yousef 	}
35d17bf24eSQais Yousef 
36d17bf24eSQais Yousef 	if (!cpumask_subset(dest, cpu_possible_mask)) {
37d17bf24eSQais Yousef 		pr_warn("Reservation is not in possible_cpu_mask\n");
38d17bf24eSQais Yousef 		return 0;
39d17bf24eSQais Yousef 	}
40d17bf24eSQais Yousef 
41d17bf24eSQais Yousef 	nr_irqs = cpumask_weight(dest);
42d17bf24eSQais Yousef 	if (!nr_irqs) {
43d17bf24eSQais Yousef 		pr_warn("Reservation for empty destination mask\n");
44d17bf24eSQais Yousef 		return 0;
45d17bf24eSQais Yousef 	}
46d17bf24eSQais Yousef 
47d17bf24eSQais Yousef 	if (irq_domain_is_ipi_single(domain)) {
48d17bf24eSQais Yousef 		/*
49d17bf24eSQais Yousef 		 * If the underlying implementation uses a single HW irq on
50d17bf24eSQais Yousef 		 * all cpus then we only need a single Linux irq number for
51d17bf24eSQais Yousef 		 * it. We have no restrictions vs. the destination mask. The
52d17bf24eSQais Yousef 		 * underlying implementation can deal with holes nicely.
53d17bf24eSQais Yousef 		 */
54d17bf24eSQais Yousef 		nr_irqs = 1;
55d17bf24eSQais Yousef 		offset = 0;
56d17bf24eSQais Yousef 	} else {
57d17bf24eSQais Yousef 		unsigned int next;
58d17bf24eSQais Yousef 
59d17bf24eSQais Yousef 		/*
60d17bf24eSQais Yousef 		 * The IPI requires a seperate HW irq on each CPU. We require
61d17bf24eSQais Yousef 		 * that the destination mask is consecutive. If an
62d17bf24eSQais Yousef 		 * implementation needs to support holes, it can reserve
63d17bf24eSQais Yousef 		 * several IPI ranges.
64d17bf24eSQais Yousef 		 */
65d17bf24eSQais Yousef 		offset = cpumask_first(dest);
66d17bf24eSQais Yousef 		/*
67d17bf24eSQais Yousef 		 * Find a hole and if found look for another set bit after the
68d17bf24eSQais Yousef 		 * hole. For now we don't support this scenario.
69d17bf24eSQais Yousef 		 */
70d17bf24eSQais Yousef 		next = cpumask_next_zero(offset, dest);
71d17bf24eSQais Yousef 		if (next < nr_cpu_ids)
72d17bf24eSQais Yousef 			next = cpumask_next(next, dest);
73d17bf24eSQais Yousef 		if (next < nr_cpu_ids) {
74d17bf24eSQais Yousef 			pr_warn("Destination mask has holes\n");
75d17bf24eSQais Yousef 			return 0;
76d17bf24eSQais Yousef 		}
77d17bf24eSQais Yousef 	}
78d17bf24eSQais Yousef 
79d17bf24eSQais Yousef 	virq = irq_domain_alloc_descs(-1, nr_irqs, 0, NUMA_NO_NODE);
80d17bf24eSQais Yousef 	if (virq <= 0) {
81d17bf24eSQais Yousef 		pr_warn("Can't reserve IPI, failed to alloc descs\n");
82d17bf24eSQais Yousef 		return 0;
83d17bf24eSQais Yousef 	}
84d17bf24eSQais Yousef 
85d17bf24eSQais Yousef 	virq = __irq_domain_alloc_irqs(domain, virq, nr_irqs, NUMA_NO_NODE,
86d17bf24eSQais Yousef 				       (void *) dest, true);
87d17bf24eSQais Yousef 
88d17bf24eSQais Yousef 	if (virq <= 0) {
89d17bf24eSQais Yousef 		pr_warn("Can't reserve IPI, failed to alloc hw irqs\n");
90d17bf24eSQais Yousef 		goto free_descs;
91d17bf24eSQais Yousef 	}
92d17bf24eSQais Yousef 
93d17bf24eSQais Yousef 	for (i = 0; i < nr_irqs; i++) {
94d17bf24eSQais Yousef 		data = irq_get_irq_data(virq + i);
95d17bf24eSQais Yousef 		cpumask_copy(data->common->affinity, dest);
96d17bf24eSQais Yousef 		data->common->ipi_offset = offset;
974589f450SMatt Redfearn 		irq_set_status_flags(virq + i, IRQ_NO_BALANCING);
98d17bf24eSQais Yousef 	}
99d17bf24eSQais Yousef 	return virq;
100d17bf24eSQais Yousef 
101d17bf24eSQais Yousef free_descs:
102d17bf24eSQais Yousef 	irq_free_descs(virq, nr_irqs);
103d17bf24eSQais Yousef 	return 0;
104d17bf24eSQais Yousef }
105d17bf24eSQais Yousef 
106d17bf24eSQais Yousef /**
107d17bf24eSQais Yousef  * irq_destroy_ipi() - unreserve an IPI that was previously allocated
108d17bf24eSQais Yousef  * @irq:	linux irq number to be destroyed
10901292ceaSMatt Redfearn  * @dest:	cpumask of cpus which should have the IPI removed
110d17bf24eSQais Yousef  *
111d17bf24eSQais Yousef  * Return the IPIs allocated with irq_reserve_ipi() to the system destroying
112d17bf24eSQais Yousef  * all virqs associated with them.
113d17bf24eSQais Yousef  */
11401292ceaSMatt Redfearn void irq_destroy_ipi(unsigned int irq, const struct cpumask *dest)
115d17bf24eSQais Yousef {
116d17bf24eSQais Yousef 	struct irq_data *data = irq_get_irq_data(irq);
117d17bf24eSQais Yousef 	struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
118d17bf24eSQais Yousef 	struct irq_domain *domain;
119d17bf24eSQais Yousef 	unsigned int nr_irqs;
120d17bf24eSQais Yousef 
121d17bf24eSQais Yousef 	if (!irq || !data || !ipimask)
122d17bf24eSQais Yousef 		return;
123d17bf24eSQais Yousef 
124d17bf24eSQais Yousef 	domain = data->domain;
125d17bf24eSQais Yousef 	if (WARN_ON(domain == NULL))
126d17bf24eSQais Yousef 		return;
127d17bf24eSQais Yousef 
128d17bf24eSQais Yousef 	if (!irq_domain_is_ipi(domain)) {
129d17bf24eSQais Yousef 		pr_warn("Trying to destroy a non IPI domain!\n");
130d17bf24eSQais Yousef 		return;
131d17bf24eSQais Yousef 	}
132d17bf24eSQais Yousef 
13301292ceaSMatt Redfearn 	if (WARN_ON(!cpumask_subset(dest, ipimask)))
13401292ceaSMatt Redfearn 		/*
13501292ceaSMatt Redfearn 		 * Must be destroying a subset of CPUs to which this IPI
13601292ceaSMatt Redfearn 		 * was set up to target
13701292ceaSMatt Redfearn 		 */
13801292ceaSMatt Redfearn 		return;
13901292ceaSMatt Redfearn 
14001292ceaSMatt Redfearn 	if (irq_domain_is_ipi_per_cpu(domain)) {
14101292ceaSMatt Redfearn 		irq = irq + cpumask_first(dest) - data->common->ipi_offset;
14201292ceaSMatt Redfearn 		nr_irqs = cpumask_weight(dest);
14301292ceaSMatt Redfearn 	} else {
144d17bf24eSQais Yousef 		nr_irqs = 1;
14501292ceaSMatt Redfearn 	}
146d17bf24eSQais Yousef 
147d17bf24eSQais Yousef 	irq_domain_free_irqs(irq, nr_irqs);
148d17bf24eSQais Yousef }
149f9bce791SQais Yousef 
150f9bce791SQais Yousef /**
151f9bce791SQais Yousef  * ipi_get_hwirq - Get the hwirq associated with an IPI to a cpu
152f9bce791SQais Yousef  * @irq:	linux irq number
153f9bce791SQais Yousef  * @cpu:	the target cpu
154f9bce791SQais Yousef  *
155f9bce791SQais Yousef  * When dealing with coprocessors IPI, we need to inform the coprocessor of
156f9bce791SQais Yousef  * the hwirq it needs to use to receive and send IPIs.
157f9bce791SQais Yousef  *
158f9bce791SQais Yousef  * Returns hwirq value on success and INVALID_HWIRQ on failure.
159f9bce791SQais Yousef  */
160f9bce791SQais Yousef irq_hw_number_t ipi_get_hwirq(unsigned int irq, unsigned int cpu)
161f9bce791SQais Yousef {
162f9bce791SQais Yousef 	struct irq_data *data = irq_get_irq_data(irq);
163f9bce791SQais Yousef 	struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
164f9bce791SQais Yousef 
165f9bce791SQais Yousef 	if (!data || !ipimask || cpu > nr_cpu_ids)
166f9bce791SQais Yousef 		return INVALID_HWIRQ;
167f9bce791SQais Yousef 
168f9bce791SQais Yousef 	if (!cpumask_test_cpu(cpu, ipimask))
169f9bce791SQais Yousef 		return INVALID_HWIRQ;
170f9bce791SQais Yousef 
171f9bce791SQais Yousef 	/*
172f9bce791SQais Yousef 	 * Get the real hardware irq number if the underlying implementation
173f9bce791SQais Yousef 	 * uses a seperate irq per cpu. If the underlying implementation uses
174f9bce791SQais Yousef 	 * a single hardware irq for all cpus then the IPI send mechanism
1753b8e29a8SQais Yousef 	 * needs to take care of the cpu destinations.
176f9bce791SQais Yousef 	 */
177f9bce791SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain))
178f9bce791SQais Yousef 		data = irq_get_irq_data(irq + cpu - data->common->ipi_offset);
179f9bce791SQais Yousef 
180f9bce791SQais Yousef 	return data ? irqd_to_hwirq(data) : INVALID_HWIRQ;
181f9bce791SQais Yousef }
182f9bce791SQais Yousef EXPORT_SYMBOL_GPL(ipi_get_hwirq);
1833b8e29a8SQais Yousef 
1843b8e29a8SQais Yousef static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data,
1853b8e29a8SQais Yousef 			   const struct cpumask *dest, unsigned int cpu)
1863b8e29a8SQais Yousef {
1873b8e29a8SQais Yousef 	struct cpumask *ipimask = irq_data_get_affinity_mask(data);
1883b8e29a8SQais Yousef 
1893b8e29a8SQais Yousef 	if (!chip || !ipimask)
1903b8e29a8SQais Yousef 		return -EINVAL;
1913b8e29a8SQais Yousef 
1923b8e29a8SQais Yousef 	if (!chip->ipi_send_single && !chip->ipi_send_mask)
1933b8e29a8SQais Yousef 		return -EINVAL;
1943b8e29a8SQais Yousef 
1953b8e29a8SQais Yousef 	if (cpu > nr_cpu_ids)
1963b8e29a8SQais Yousef 		return -EINVAL;
1973b8e29a8SQais Yousef 
1983b8e29a8SQais Yousef 	if (dest) {
1993b8e29a8SQais Yousef 		if (!cpumask_subset(dest, ipimask))
2003b8e29a8SQais Yousef 			return -EINVAL;
2013b8e29a8SQais Yousef 	} else {
2023b8e29a8SQais Yousef 		if (!cpumask_test_cpu(cpu, ipimask))
2033b8e29a8SQais Yousef 			return -EINVAL;
2043b8e29a8SQais Yousef 	}
2053b8e29a8SQais Yousef 	return 0;
2063b8e29a8SQais Yousef }
2073b8e29a8SQais Yousef 
2083b8e29a8SQais Yousef /**
2093b8e29a8SQais Yousef  * __ipi_send_single - send an IPI to a target Linux SMP CPU
2103b8e29a8SQais Yousef  * @desc:	pointer to irq_desc of the IRQ
2113b8e29a8SQais Yousef  * @cpu:	destination CPU, must in the destination mask passed to
2123b8e29a8SQais Yousef  *		irq_reserve_ipi()
2133b8e29a8SQais Yousef  *
2143b8e29a8SQais Yousef  * This function is for architecture or core code to speed up IPI sending. Not
2153b8e29a8SQais Yousef  * usable from driver code.
2163b8e29a8SQais Yousef  *
2173b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
2183b8e29a8SQais Yousef  */
2193b8e29a8SQais Yousef int __ipi_send_single(struct irq_desc *desc, unsigned int cpu)
2203b8e29a8SQais Yousef {
2213b8e29a8SQais Yousef 	struct irq_data *data = irq_desc_get_irq_data(desc);
2223b8e29a8SQais Yousef 	struct irq_chip *chip = irq_data_get_irq_chip(data);
2233b8e29a8SQais Yousef 
2243b8e29a8SQais Yousef #ifdef DEBUG
2253b8e29a8SQais Yousef 	/*
2263b8e29a8SQais Yousef 	 * Minimise the overhead by omitting the checks for Linux SMP IPIs.
2273b8e29a8SQais Yousef 	 * Since the callers should be arch or core code which is generally
2283b8e29a8SQais Yousef 	 * trusted, only check for errors when debugging.
2293b8e29a8SQais Yousef 	 */
2303b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
2313b8e29a8SQais Yousef 		return -EINVAL;
2323b8e29a8SQais Yousef #endif
2333b8e29a8SQais Yousef 	if (!chip->ipi_send_single) {
2343b8e29a8SQais Yousef 		chip->ipi_send_mask(data, cpumask_of(cpu));
2353b8e29a8SQais Yousef 		return 0;
2363b8e29a8SQais Yousef 	}
2373b8e29a8SQais Yousef 
2383b8e29a8SQais Yousef 	/* FIXME: Store this information in irqdata flags */
2393b8e29a8SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain) &&
2403b8e29a8SQais Yousef 	    cpu != data->common->ipi_offset) {
2413b8e29a8SQais Yousef 		/* use the correct data for that cpu */
2423b8e29a8SQais Yousef 		unsigned irq = data->irq + cpu - data->common->ipi_offset;
2433b8e29a8SQais Yousef 
2443b8e29a8SQais Yousef 		data = irq_get_irq_data(irq);
2453b8e29a8SQais Yousef 	}
2463b8e29a8SQais Yousef 	chip->ipi_send_single(data, cpu);
2473b8e29a8SQais Yousef 	return 0;
2483b8e29a8SQais Yousef }
2493b8e29a8SQais Yousef 
2503b8e29a8SQais Yousef /**
2513b8e29a8SQais Yousef  * ipi_send_mask - send an IPI to target Linux SMP CPU(s)
2523b8e29a8SQais Yousef  * @desc:	pointer to irq_desc of the IRQ
2533b8e29a8SQais Yousef  * @dest:	dest CPU(s), must be a subset of the mask passed to
2543b8e29a8SQais Yousef  *		irq_reserve_ipi()
2553b8e29a8SQais Yousef  *
2563b8e29a8SQais Yousef  * This function is for architecture or core code to speed up IPI sending. Not
2573b8e29a8SQais Yousef  * usable from driver code.
2583b8e29a8SQais Yousef  *
2593b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
2603b8e29a8SQais Yousef  */
2613b8e29a8SQais Yousef int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest)
2623b8e29a8SQais Yousef {
2633b8e29a8SQais Yousef 	struct irq_data *data = irq_desc_get_irq_data(desc);
2643b8e29a8SQais Yousef 	struct irq_chip *chip = irq_data_get_irq_chip(data);
2653b8e29a8SQais Yousef 	unsigned int cpu;
2663b8e29a8SQais Yousef 
2673b8e29a8SQais Yousef #ifdef DEBUG
2683b8e29a8SQais Yousef 	/*
2693b8e29a8SQais Yousef 	 * Minimise the overhead by omitting the checks for Linux SMP IPIs.
2703b8e29a8SQais Yousef 	 * Since the callers should be arch or core code which is generally
2713b8e29a8SQais Yousef 	 * trusted, only check for errors when debugging.
2723b8e29a8SQais Yousef 	 */
2733b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
2743b8e29a8SQais Yousef 		return -EINVAL;
2753b8e29a8SQais Yousef #endif
2763b8e29a8SQais Yousef 	if (chip->ipi_send_mask) {
2773b8e29a8SQais Yousef 		chip->ipi_send_mask(data, dest);
2783b8e29a8SQais Yousef 		return 0;
2793b8e29a8SQais Yousef 	}
2803b8e29a8SQais Yousef 
2813b8e29a8SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain)) {
2823b8e29a8SQais Yousef 		unsigned int base = data->irq;
2833b8e29a8SQais Yousef 
2843b8e29a8SQais Yousef 		for_each_cpu(cpu, dest) {
2853b8e29a8SQais Yousef 			unsigned irq = base + cpu - data->common->ipi_offset;
2863b8e29a8SQais Yousef 
2873b8e29a8SQais Yousef 			data = irq_get_irq_data(irq);
2883b8e29a8SQais Yousef 			chip->ipi_send_single(data, cpu);
2893b8e29a8SQais Yousef 		}
2903b8e29a8SQais Yousef 	} else {
2913b8e29a8SQais Yousef 		for_each_cpu(cpu, dest)
2923b8e29a8SQais Yousef 			chip->ipi_send_single(data, cpu);
2933b8e29a8SQais Yousef 	}
2943b8e29a8SQais Yousef 	return 0;
2953b8e29a8SQais Yousef }
2963b8e29a8SQais Yousef 
2973b8e29a8SQais Yousef /**
2983b8e29a8SQais Yousef  * ipi_send_single - Send an IPI to a single CPU
2993b8e29a8SQais Yousef  * @virq:	linux irq number from irq_reserve_ipi()
3003b8e29a8SQais Yousef  * @cpu:	destination CPU, must in the destination mask passed to
3013b8e29a8SQais Yousef  *		irq_reserve_ipi()
3023b8e29a8SQais Yousef  *
3033b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
3043b8e29a8SQais Yousef  */
3053b8e29a8SQais Yousef int ipi_send_single(unsigned int virq, unsigned int cpu)
3063b8e29a8SQais Yousef {
3073b8e29a8SQais Yousef 	struct irq_desc *desc = irq_to_desc(virq);
3083b8e29a8SQais Yousef 	struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
3093b8e29a8SQais Yousef 	struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
3103b8e29a8SQais Yousef 
3113b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
3123b8e29a8SQais Yousef 		return -EINVAL;
3133b8e29a8SQais Yousef 
3143b8e29a8SQais Yousef 	return __ipi_send_single(desc, cpu);
3153b8e29a8SQais Yousef }
3163b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_single);
3173b8e29a8SQais Yousef 
3183b8e29a8SQais Yousef /**
3193b8e29a8SQais Yousef  * ipi_send_mask - Send an IPI to target CPU(s)
3203b8e29a8SQais Yousef  * @virq:	linux irq number from irq_reserve_ipi()
3213b8e29a8SQais Yousef  * @dest:	dest CPU(s), must be a subset of the mask passed to
3223b8e29a8SQais Yousef  *		irq_reserve_ipi()
3233b8e29a8SQais Yousef  *
3243b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
3253b8e29a8SQais Yousef  */
3263b8e29a8SQais Yousef int ipi_send_mask(unsigned int virq, const struct cpumask *dest)
3273b8e29a8SQais Yousef {
3283b8e29a8SQais Yousef 	struct irq_desc *desc = irq_to_desc(virq);
3293b8e29a8SQais Yousef 	struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
3303b8e29a8SQais Yousef 	struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
3313b8e29a8SQais Yousef 
3323b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
3333b8e29a8SQais Yousef 		return -EINVAL;
3343b8e29a8SQais Yousef 
3353b8e29a8SQais Yousef 	return __ipi_send_mask(desc, dest);
3363b8e29a8SQais Yousef }
3373b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_mask);
338