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