xref: /openbmc/linux/kernel/irq/ipi.c (revision 3b8e29a82dd16c1f2061e0b955a71cd36eeb061b)
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;
97d17bf24eSQais Yousef 	}
98d17bf24eSQais Yousef 	return virq;
99d17bf24eSQais Yousef 
100d17bf24eSQais Yousef free_descs:
101d17bf24eSQais Yousef 	irq_free_descs(virq, nr_irqs);
102d17bf24eSQais Yousef 	return 0;
103d17bf24eSQais Yousef }
104d17bf24eSQais Yousef 
105d17bf24eSQais Yousef /**
106d17bf24eSQais Yousef  * irq_destroy_ipi() - unreserve an IPI that was previously allocated
107d17bf24eSQais Yousef  * @irq:	linux irq number to be destroyed
108d17bf24eSQais Yousef  *
109d17bf24eSQais Yousef  * Return the IPIs allocated with irq_reserve_ipi() to the system destroying
110d17bf24eSQais Yousef  * all virqs associated with them.
111d17bf24eSQais Yousef  */
112d17bf24eSQais Yousef void irq_destroy_ipi(unsigned int irq)
113d17bf24eSQais Yousef {
114d17bf24eSQais Yousef 	struct irq_data *data = irq_get_irq_data(irq);
115d17bf24eSQais Yousef 	struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
116d17bf24eSQais Yousef 	struct irq_domain *domain;
117d17bf24eSQais Yousef 	unsigned int nr_irqs;
118d17bf24eSQais Yousef 
119d17bf24eSQais Yousef 	if (!irq || !data || !ipimask)
120d17bf24eSQais Yousef 		return;
121d17bf24eSQais Yousef 
122d17bf24eSQais Yousef 	domain = data->domain;
123d17bf24eSQais Yousef 	if (WARN_ON(domain == NULL))
124d17bf24eSQais Yousef 		return;
125d17bf24eSQais Yousef 
126d17bf24eSQais Yousef 	if (!irq_domain_is_ipi(domain)) {
127d17bf24eSQais Yousef 		pr_warn("Trying to destroy a non IPI domain!\n");
128d17bf24eSQais Yousef 		return;
129d17bf24eSQais Yousef 	}
130d17bf24eSQais Yousef 
131d17bf24eSQais Yousef 	if (irq_domain_is_ipi_per_cpu(domain))
132d17bf24eSQais Yousef 		nr_irqs = cpumask_weight(ipimask);
133d17bf24eSQais Yousef 	else
134d17bf24eSQais Yousef 		nr_irqs = 1;
135d17bf24eSQais Yousef 
136d17bf24eSQais Yousef 	irq_domain_free_irqs(irq, nr_irqs);
137d17bf24eSQais Yousef }
138f9bce791SQais Yousef 
139f9bce791SQais Yousef /**
140f9bce791SQais Yousef  * ipi_get_hwirq - Get the hwirq associated with an IPI to a cpu
141f9bce791SQais Yousef  * @irq:	linux irq number
142f9bce791SQais Yousef  * @cpu:	the target cpu
143f9bce791SQais Yousef  *
144f9bce791SQais Yousef  * When dealing with coprocessors IPI, we need to inform the coprocessor of
145f9bce791SQais Yousef  * the hwirq it needs to use to receive and send IPIs.
146f9bce791SQais Yousef  *
147f9bce791SQais Yousef  * Returns hwirq value on success and INVALID_HWIRQ on failure.
148f9bce791SQais Yousef  */
149f9bce791SQais Yousef irq_hw_number_t ipi_get_hwirq(unsigned int irq, unsigned int cpu)
150f9bce791SQais Yousef {
151f9bce791SQais Yousef 	struct irq_data *data = irq_get_irq_data(irq);
152f9bce791SQais Yousef 	struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
153f9bce791SQais Yousef 
154f9bce791SQais Yousef 	if (!data || !ipimask || cpu > nr_cpu_ids)
155f9bce791SQais Yousef 		return INVALID_HWIRQ;
156f9bce791SQais Yousef 
157f9bce791SQais Yousef 	if (!cpumask_test_cpu(cpu, ipimask))
158f9bce791SQais Yousef 		return INVALID_HWIRQ;
159f9bce791SQais Yousef 
160f9bce791SQais Yousef 	/*
161f9bce791SQais Yousef 	 * Get the real hardware irq number if the underlying implementation
162f9bce791SQais Yousef 	 * uses a seperate irq per cpu. If the underlying implementation uses
163f9bce791SQais Yousef 	 * a single hardware irq for all cpus then the IPI send mechanism
164*3b8e29a8SQais Yousef 	 * needs to take care of the cpu destinations.
165f9bce791SQais Yousef 	 */
166f9bce791SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain))
167f9bce791SQais Yousef 		data = irq_get_irq_data(irq + cpu - data->common->ipi_offset);
168f9bce791SQais Yousef 
169f9bce791SQais Yousef 	return data ? irqd_to_hwirq(data) : INVALID_HWIRQ;
170f9bce791SQais Yousef }
171f9bce791SQais Yousef EXPORT_SYMBOL_GPL(ipi_get_hwirq);
172*3b8e29a8SQais Yousef 
173*3b8e29a8SQais Yousef static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data,
174*3b8e29a8SQais Yousef 			   const struct cpumask *dest, unsigned int cpu)
175*3b8e29a8SQais Yousef {
176*3b8e29a8SQais Yousef 	struct cpumask *ipimask = irq_data_get_affinity_mask(data);
177*3b8e29a8SQais Yousef 
178*3b8e29a8SQais Yousef 	if (!chip || !ipimask)
179*3b8e29a8SQais Yousef 		return -EINVAL;
180*3b8e29a8SQais Yousef 
181*3b8e29a8SQais Yousef 	if (!chip->ipi_send_single && !chip->ipi_send_mask)
182*3b8e29a8SQais Yousef 		return -EINVAL;
183*3b8e29a8SQais Yousef 
184*3b8e29a8SQais Yousef 	if (cpu > nr_cpu_ids)
185*3b8e29a8SQais Yousef 		return -EINVAL;
186*3b8e29a8SQais Yousef 
187*3b8e29a8SQais Yousef 	if (dest) {
188*3b8e29a8SQais Yousef 		if (!cpumask_subset(dest, ipimask))
189*3b8e29a8SQais Yousef 			return -EINVAL;
190*3b8e29a8SQais Yousef 	} else {
191*3b8e29a8SQais Yousef 		if (!cpumask_test_cpu(cpu, ipimask))
192*3b8e29a8SQais Yousef 			return -EINVAL;
193*3b8e29a8SQais Yousef 	}
194*3b8e29a8SQais Yousef 	return 0;
195*3b8e29a8SQais Yousef }
196*3b8e29a8SQais Yousef 
197*3b8e29a8SQais Yousef /**
198*3b8e29a8SQais Yousef  * __ipi_send_single - send an IPI to a target Linux SMP CPU
199*3b8e29a8SQais Yousef  * @desc:	pointer to irq_desc of the IRQ
200*3b8e29a8SQais Yousef  * @cpu:	destination CPU, must in the destination mask passed to
201*3b8e29a8SQais Yousef  *		irq_reserve_ipi()
202*3b8e29a8SQais Yousef  *
203*3b8e29a8SQais Yousef  * This function is for architecture or core code to speed up IPI sending. Not
204*3b8e29a8SQais Yousef  * usable from driver code.
205*3b8e29a8SQais Yousef  *
206*3b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
207*3b8e29a8SQais Yousef  */
208*3b8e29a8SQais Yousef int __ipi_send_single(struct irq_desc *desc, unsigned int cpu)
209*3b8e29a8SQais Yousef {
210*3b8e29a8SQais Yousef 	struct irq_data *data = irq_desc_get_irq_data(desc);
211*3b8e29a8SQais Yousef 	struct irq_chip *chip = irq_data_get_irq_chip(data);
212*3b8e29a8SQais Yousef 
213*3b8e29a8SQais Yousef #ifdef DEBUG
214*3b8e29a8SQais Yousef 	/*
215*3b8e29a8SQais Yousef 	 * Minimise the overhead by omitting the checks for Linux SMP IPIs.
216*3b8e29a8SQais Yousef 	 * Since the callers should be arch or core code which is generally
217*3b8e29a8SQais Yousef 	 * trusted, only check for errors when debugging.
218*3b8e29a8SQais Yousef 	 */
219*3b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
220*3b8e29a8SQais Yousef 		return -EINVAL;
221*3b8e29a8SQais Yousef #endif
222*3b8e29a8SQais Yousef 	if (!chip->ipi_send_single) {
223*3b8e29a8SQais Yousef 		chip->ipi_send_mask(data, cpumask_of(cpu));
224*3b8e29a8SQais Yousef 		return 0;
225*3b8e29a8SQais Yousef 	}
226*3b8e29a8SQais Yousef 
227*3b8e29a8SQais Yousef 	/* FIXME: Store this information in irqdata flags */
228*3b8e29a8SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain) &&
229*3b8e29a8SQais Yousef 	    cpu != data->common->ipi_offset) {
230*3b8e29a8SQais Yousef 		/* use the correct data for that cpu */
231*3b8e29a8SQais Yousef 		unsigned irq = data->irq + cpu - data->common->ipi_offset;
232*3b8e29a8SQais Yousef 
233*3b8e29a8SQais Yousef 		data = irq_get_irq_data(irq);
234*3b8e29a8SQais Yousef 	}
235*3b8e29a8SQais Yousef 	chip->ipi_send_single(data, cpu);
236*3b8e29a8SQais Yousef 	return 0;
237*3b8e29a8SQais Yousef }
238*3b8e29a8SQais Yousef 
239*3b8e29a8SQais Yousef /**
240*3b8e29a8SQais Yousef  * ipi_send_mask - send an IPI to target Linux SMP CPU(s)
241*3b8e29a8SQais Yousef  * @desc:	pointer to irq_desc of the IRQ
242*3b8e29a8SQais Yousef  * @dest:	dest CPU(s), must be a subset of the mask passed to
243*3b8e29a8SQais Yousef  *		irq_reserve_ipi()
244*3b8e29a8SQais Yousef  *
245*3b8e29a8SQais Yousef  * This function is for architecture or core code to speed up IPI sending. Not
246*3b8e29a8SQais Yousef  * usable from driver code.
247*3b8e29a8SQais Yousef  *
248*3b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
249*3b8e29a8SQais Yousef  */
250*3b8e29a8SQais Yousef int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest)
251*3b8e29a8SQais Yousef {
252*3b8e29a8SQais Yousef 	struct irq_data *data = irq_desc_get_irq_data(desc);
253*3b8e29a8SQais Yousef 	struct irq_chip *chip = irq_data_get_irq_chip(data);
254*3b8e29a8SQais Yousef 	unsigned int cpu;
255*3b8e29a8SQais Yousef 
256*3b8e29a8SQais Yousef #ifdef DEBUG
257*3b8e29a8SQais Yousef 	/*
258*3b8e29a8SQais Yousef 	 * Minimise the overhead by omitting the checks for Linux SMP IPIs.
259*3b8e29a8SQais Yousef 	 * Since the callers should be arch or core code which is generally
260*3b8e29a8SQais Yousef 	 * trusted, only check for errors when debugging.
261*3b8e29a8SQais Yousef 	 */
262*3b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
263*3b8e29a8SQais Yousef 		return -EINVAL;
264*3b8e29a8SQais Yousef #endif
265*3b8e29a8SQais Yousef 	if (chip->ipi_send_mask) {
266*3b8e29a8SQais Yousef 		chip->ipi_send_mask(data, dest);
267*3b8e29a8SQais Yousef 		return 0;
268*3b8e29a8SQais Yousef 	}
269*3b8e29a8SQais Yousef 
270*3b8e29a8SQais Yousef 	if (irq_domain_is_ipi_per_cpu(data->domain)) {
271*3b8e29a8SQais Yousef 		unsigned int base = data->irq;
272*3b8e29a8SQais Yousef 
273*3b8e29a8SQais Yousef 		for_each_cpu(cpu, dest) {
274*3b8e29a8SQais Yousef 			unsigned irq = base + cpu - data->common->ipi_offset;
275*3b8e29a8SQais Yousef 
276*3b8e29a8SQais Yousef 			data = irq_get_irq_data(irq);
277*3b8e29a8SQais Yousef 			chip->ipi_send_single(data, cpu);
278*3b8e29a8SQais Yousef 		}
279*3b8e29a8SQais Yousef 	} else {
280*3b8e29a8SQais Yousef 		for_each_cpu(cpu, dest)
281*3b8e29a8SQais Yousef 			chip->ipi_send_single(data, cpu);
282*3b8e29a8SQais Yousef 	}
283*3b8e29a8SQais Yousef 	return 0;
284*3b8e29a8SQais Yousef }
285*3b8e29a8SQais Yousef 
286*3b8e29a8SQais Yousef /**
287*3b8e29a8SQais Yousef  * ipi_send_single - Send an IPI to a single CPU
288*3b8e29a8SQais Yousef  * @virq:	linux irq number from irq_reserve_ipi()
289*3b8e29a8SQais Yousef  * @cpu:	destination CPU, must in the destination mask passed to
290*3b8e29a8SQais Yousef  *		irq_reserve_ipi()
291*3b8e29a8SQais Yousef  *
292*3b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
293*3b8e29a8SQais Yousef  */
294*3b8e29a8SQais Yousef int ipi_send_single(unsigned int virq, unsigned int cpu)
295*3b8e29a8SQais Yousef {
296*3b8e29a8SQais Yousef 	struct irq_desc *desc = irq_to_desc(virq);
297*3b8e29a8SQais Yousef 	struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
298*3b8e29a8SQais Yousef 	struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
299*3b8e29a8SQais Yousef 
300*3b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
301*3b8e29a8SQais Yousef 		return -EINVAL;
302*3b8e29a8SQais Yousef 
303*3b8e29a8SQais Yousef 	return __ipi_send_single(desc, cpu);
304*3b8e29a8SQais Yousef }
305*3b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_single);
306*3b8e29a8SQais Yousef 
307*3b8e29a8SQais Yousef /**
308*3b8e29a8SQais Yousef  * ipi_send_mask - Send an IPI to target CPU(s)
309*3b8e29a8SQais Yousef  * @virq:	linux irq number from irq_reserve_ipi()
310*3b8e29a8SQais Yousef  * @dest:	dest CPU(s), must be a subset of the mask passed to
311*3b8e29a8SQais Yousef  *		irq_reserve_ipi()
312*3b8e29a8SQais Yousef  *
313*3b8e29a8SQais Yousef  * Returns zero on success and negative error number on failure.
314*3b8e29a8SQais Yousef  */
315*3b8e29a8SQais Yousef int ipi_send_mask(unsigned int virq, const struct cpumask *dest)
316*3b8e29a8SQais Yousef {
317*3b8e29a8SQais Yousef 	struct irq_desc *desc = irq_to_desc(virq);
318*3b8e29a8SQais Yousef 	struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
319*3b8e29a8SQais Yousef 	struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
320*3b8e29a8SQais Yousef 
321*3b8e29a8SQais Yousef 	if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
322*3b8e29a8SQais Yousef 		return -EINVAL;
323*3b8e29a8SQais Yousef 
324*3b8e29a8SQais Yousef 	return __ipi_send_mask(desc, dest);
325*3b8e29a8SQais Yousef }
326*3b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_mask);
327