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 173b35e7e6SRandy Dunlap * @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 * 213b35e7e6SRandy Dunlap * Return: Linux IRQ number on success or 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 /* 59c5f48c0aSIngo 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 1073b35e7e6SRandy Dunlap * @irq: Linux IRQ number to be destroyed 1083b35e7e6SRandy Dunlap * @dest: cpumask of CPUs which should have the IPI removed 109d17bf24eSQais Yousef * 1105c982c58SKrzysztof Kozlowski * The IPIs allocated with irq_reserve_ipi() are returned to the system 1117cec18a3SMatt Redfearn * destroying all virqs associated with them. 1127cec18a3SMatt Redfearn * 1133b35e7e6SRandy Dunlap * 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); 118*4d0b8298SSamuel Holland const struct cpumask *ipimask; 119d17bf24eSQais Yousef struct irq_domain *domain; 120d17bf24eSQais Yousef unsigned int nr_irqs; 121d17bf24eSQais Yousef 122*4d0b8298SSamuel Holland if (!irq || !data) 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 134*4d0b8298SSamuel Holland ipimask = irq_data_get_affinity_mask(data); 135*4d0b8298SSamuel Holland if (!ipimask || WARN_ON(!cpumask_subset(dest, ipimask))) 13601292ceaSMatt Redfearn /* 13701292ceaSMatt Redfearn * Must be destroying a subset of CPUs to which this IPI 13801292ceaSMatt Redfearn * was set up to target 13901292ceaSMatt Redfearn */ 1407cec18a3SMatt Redfearn return -EINVAL; 14101292ceaSMatt Redfearn 14201292ceaSMatt Redfearn if (irq_domain_is_ipi_per_cpu(domain)) { 14301292ceaSMatt Redfearn irq = irq + cpumask_first(dest) - data->common->ipi_offset; 14401292ceaSMatt Redfearn nr_irqs = cpumask_weight(dest); 14501292ceaSMatt Redfearn } else { 146d17bf24eSQais Yousef nr_irqs = 1; 14701292ceaSMatt Redfearn } 148d17bf24eSQais Yousef 149d17bf24eSQais Yousef irq_domain_free_irqs(irq, nr_irqs); 1507cec18a3SMatt Redfearn return 0; 151d17bf24eSQais Yousef } 152f9bce791SQais Yousef 153f9bce791SQais Yousef /** 1543b35e7e6SRandy Dunlap * ipi_get_hwirq - Get the hwirq associated with an IPI to a CPU 1553b35e7e6SRandy Dunlap * @irq: Linux IRQ number 1563b35e7e6SRandy Dunlap * @cpu: the target CPU 157f9bce791SQais Yousef * 158f9bce791SQais Yousef * When dealing with coprocessors IPI, we need to inform the coprocessor of 159f9bce791SQais Yousef * the hwirq it needs to use to receive and send IPIs. 160f9bce791SQais Yousef * 1613b35e7e6SRandy Dunlap * Return: hwirq value on success or INVALID_HWIRQ on failure. 162f9bce791SQais Yousef */ 163f9bce791SQais Yousef irq_hw_number_t ipi_get_hwirq(unsigned int irq, unsigned int cpu) 164f9bce791SQais Yousef { 165f9bce791SQais Yousef struct irq_data *data = irq_get_irq_data(irq); 166*4d0b8298SSamuel Holland const struct cpumask *ipimask; 167f9bce791SQais Yousef 168*4d0b8298SSamuel Holland if (!data || cpu >= nr_cpu_ids) 169f9bce791SQais Yousef return INVALID_HWIRQ; 170f9bce791SQais Yousef 171*4d0b8298SSamuel Holland ipimask = irq_data_get_affinity_mask(data); 172*4d0b8298SSamuel Holland if (!ipimask || !cpumask_test_cpu(cpu, ipimask)) 173f9bce791SQais Yousef return INVALID_HWIRQ; 174f9bce791SQais Yousef 175f9bce791SQais Yousef /* 176f9bce791SQais Yousef * Get the real hardware irq number if the underlying implementation 177c5f48c0aSIngo Molnar * uses a separate irq per cpu. If the underlying implementation uses 178f9bce791SQais Yousef * a single hardware irq for all cpus then the IPI send mechanism 1793b8e29a8SQais Yousef * needs to take care of the cpu destinations. 180f9bce791SQais Yousef */ 181f9bce791SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain)) 182f9bce791SQais Yousef data = irq_get_irq_data(irq + cpu - data->common->ipi_offset); 183f9bce791SQais Yousef 184f9bce791SQais Yousef return data ? irqd_to_hwirq(data) : INVALID_HWIRQ; 185f9bce791SQais Yousef } 186f9bce791SQais Yousef EXPORT_SYMBOL_GPL(ipi_get_hwirq); 1873b8e29a8SQais Yousef 1883b8e29a8SQais Yousef static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data, 1893b8e29a8SQais Yousef const struct cpumask *dest, unsigned int cpu) 1903b8e29a8SQais Yousef { 191*4d0b8298SSamuel Holland const struct cpumask *ipimask = irq_data_get_affinity_mask(data); 1923b8e29a8SQais Yousef 1933b8e29a8SQais Yousef if (!chip || !ipimask) 1943b8e29a8SQais Yousef return -EINVAL; 1953b8e29a8SQais Yousef 1963b8e29a8SQais Yousef if (!chip->ipi_send_single && !chip->ipi_send_mask) 1973b8e29a8SQais Yousef return -EINVAL; 1983b8e29a8SQais Yousef 1998fbbe2d7SAlexey Dobriyan if (cpu >= nr_cpu_ids) 2003b8e29a8SQais Yousef return -EINVAL; 2013b8e29a8SQais Yousef 2023b8e29a8SQais Yousef if (dest) { 2033b8e29a8SQais Yousef if (!cpumask_subset(dest, ipimask)) 2043b8e29a8SQais Yousef return -EINVAL; 2053b8e29a8SQais Yousef } else { 2063b8e29a8SQais Yousef if (!cpumask_test_cpu(cpu, ipimask)) 2073b8e29a8SQais Yousef return -EINVAL; 2083b8e29a8SQais Yousef } 2093b8e29a8SQais Yousef return 0; 2103b8e29a8SQais Yousef } 2113b8e29a8SQais Yousef 2123b8e29a8SQais Yousef /** 2133b8e29a8SQais Yousef * __ipi_send_single - send an IPI to a target Linux SMP CPU 2143b8e29a8SQais Yousef * @desc: pointer to irq_desc of the IRQ 2153b8e29a8SQais Yousef * @cpu: destination CPU, must in the destination mask passed to 2163b8e29a8SQais Yousef * irq_reserve_ipi() 2173b8e29a8SQais Yousef * 2183b8e29a8SQais Yousef * This function is for architecture or core code to speed up IPI sending. Not 2193b8e29a8SQais Yousef * usable from driver code. 2203b8e29a8SQais Yousef * 2213b35e7e6SRandy Dunlap * Return: %0 on success or negative error number on failure. 2223b8e29a8SQais Yousef */ 2233b8e29a8SQais Yousef int __ipi_send_single(struct irq_desc *desc, unsigned int cpu) 2243b8e29a8SQais Yousef { 2253b8e29a8SQais Yousef struct irq_data *data = irq_desc_get_irq_data(desc); 2263b8e29a8SQais Yousef struct irq_chip *chip = irq_data_get_irq_chip(data); 2273b8e29a8SQais Yousef 2283b8e29a8SQais Yousef #ifdef DEBUG 2293b8e29a8SQais Yousef /* 2303b8e29a8SQais Yousef * Minimise the overhead by omitting the checks for Linux SMP IPIs. 2313b8e29a8SQais Yousef * Since the callers should be arch or core code which is generally 2323b8e29a8SQais Yousef * trusted, only check for errors when debugging. 2333b8e29a8SQais Yousef */ 2343b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu))) 2353b8e29a8SQais Yousef return -EINVAL; 2363b8e29a8SQais Yousef #endif 2373b8e29a8SQais Yousef if (!chip->ipi_send_single) { 2383b8e29a8SQais Yousef chip->ipi_send_mask(data, cpumask_of(cpu)); 2393b8e29a8SQais Yousef return 0; 2403b8e29a8SQais Yousef } 2413b8e29a8SQais Yousef 2423b8e29a8SQais Yousef /* FIXME: Store this information in irqdata flags */ 2433b8e29a8SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain) && 2443b8e29a8SQais Yousef cpu != data->common->ipi_offset) { 2453b8e29a8SQais Yousef /* use the correct data for that cpu */ 2463b8e29a8SQais Yousef unsigned irq = data->irq + cpu - data->common->ipi_offset; 2473b8e29a8SQais Yousef 2483b8e29a8SQais Yousef data = irq_get_irq_data(irq); 2493b8e29a8SQais Yousef } 2503b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2513b8e29a8SQais Yousef return 0; 2523b8e29a8SQais Yousef } 2533b8e29a8SQais Yousef 2543b8e29a8SQais Yousef /** 2553b35e7e6SRandy Dunlap * __ipi_send_mask - send an IPI to target Linux SMP CPU(s) 2563b8e29a8SQais Yousef * @desc: pointer to irq_desc of the IRQ 2573b8e29a8SQais Yousef * @dest: dest CPU(s), must be a subset of the mask passed to 2583b8e29a8SQais Yousef * irq_reserve_ipi() 2593b8e29a8SQais Yousef * 2603b8e29a8SQais Yousef * This function is for architecture or core code to speed up IPI sending. Not 2613b8e29a8SQais Yousef * usable from driver code. 2623b8e29a8SQais Yousef * 2633b35e7e6SRandy Dunlap * Return: %0 on success or negative error number on failure. 2643b8e29a8SQais Yousef */ 2653b8e29a8SQais Yousef int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest) 2663b8e29a8SQais Yousef { 2673b8e29a8SQais Yousef struct irq_data *data = irq_desc_get_irq_data(desc); 2683b8e29a8SQais Yousef struct irq_chip *chip = irq_data_get_irq_chip(data); 2693b8e29a8SQais Yousef unsigned int cpu; 2703b8e29a8SQais Yousef 2713b8e29a8SQais Yousef #ifdef DEBUG 2723b8e29a8SQais Yousef /* 2733b8e29a8SQais Yousef * Minimise the overhead by omitting the checks for Linux SMP IPIs. 2743b8e29a8SQais Yousef * Since the callers should be arch or core code which is generally 2753b8e29a8SQais Yousef * trusted, only check for errors when debugging. 2763b8e29a8SQais Yousef */ 2773b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0))) 2783b8e29a8SQais Yousef return -EINVAL; 2793b8e29a8SQais Yousef #endif 2803b8e29a8SQais Yousef if (chip->ipi_send_mask) { 2813b8e29a8SQais Yousef chip->ipi_send_mask(data, dest); 2823b8e29a8SQais Yousef return 0; 2833b8e29a8SQais Yousef } 2843b8e29a8SQais Yousef 2853b8e29a8SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain)) { 2863b8e29a8SQais Yousef unsigned int base = data->irq; 2873b8e29a8SQais Yousef 2883b8e29a8SQais Yousef for_each_cpu(cpu, dest) { 2893b8e29a8SQais Yousef unsigned irq = base + cpu - data->common->ipi_offset; 2903b8e29a8SQais Yousef 2913b8e29a8SQais Yousef data = irq_get_irq_data(irq); 2923b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2933b8e29a8SQais Yousef } 2943b8e29a8SQais Yousef } else { 2953b8e29a8SQais Yousef for_each_cpu(cpu, dest) 2963b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2973b8e29a8SQais Yousef } 2983b8e29a8SQais Yousef return 0; 2993b8e29a8SQais Yousef } 3003b8e29a8SQais Yousef 3013b8e29a8SQais Yousef /** 3023b8e29a8SQais Yousef * ipi_send_single - Send an IPI to a single CPU 3033b35e7e6SRandy Dunlap * @virq: Linux IRQ number from irq_reserve_ipi() 3043b8e29a8SQais Yousef * @cpu: destination CPU, must in the destination mask passed to 3053b8e29a8SQais Yousef * irq_reserve_ipi() 3063b8e29a8SQais Yousef * 3073b35e7e6SRandy Dunlap * Return: %0 on success or negative error number on failure. 3083b8e29a8SQais Yousef */ 3093b8e29a8SQais Yousef int ipi_send_single(unsigned int virq, unsigned int cpu) 3103b8e29a8SQais Yousef { 3113b8e29a8SQais Yousef struct irq_desc *desc = irq_to_desc(virq); 3123b8e29a8SQais Yousef struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL; 3133b8e29a8SQais Yousef struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL; 3143b8e29a8SQais Yousef 3153b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu))) 3163b8e29a8SQais Yousef return -EINVAL; 3173b8e29a8SQais Yousef 3183b8e29a8SQais Yousef return __ipi_send_single(desc, cpu); 3193b8e29a8SQais Yousef } 3203b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_single); 3213b8e29a8SQais Yousef 3223b8e29a8SQais Yousef /** 3233b8e29a8SQais Yousef * ipi_send_mask - Send an IPI to target CPU(s) 3243b35e7e6SRandy Dunlap * @virq: Linux IRQ number from irq_reserve_ipi() 3253b8e29a8SQais Yousef * @dest: dest CPU(s), must be a subset of the mask passed to 3263b8e29a8SQais Yousef * irq_reserve_ipi() 3273b8e29a8SQais Yousef * 3283b35e7e6SRandy Dunlap * Return: %0 on success or negative error number on failure. 3293b8e29a8SQais Yousef */ 3303b8e29a8SQais Yousef int ipi_send_mask(unsigned int virq, const struct cpumask *dest) 3313b8e29a8SQais Yousef { 3323b8e29a8SQais Yousef struct irq_desc *desc = irq_to_desc(virq); 3333b8e29a8SQais Yousef struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL; 3343b8e29a8SQais Yousef struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL; 3353b8e29a8SQais Yousef 3363b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0))) 3373b8e29a8SQais Yousef return -EINVAL; 3383b8e29a8SQais Yousef 3393b8e29a8SQais Yousef return __ipi_send_mask(desc, dest); 3403b8e29a8SQais Yousef } 3413b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_mask); 342