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 * 22*7cec18a3SMatt Redfearn * On success it'll return linux irq number and error code on failure 23d17bf24eSQais Yousef */ 24*7cec18a3SMatt Redfearn 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"); 33*7cec18a3SMatt Redfearn return -EINVAL; 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"); 38*7cec18a3SMatt Redfearn return -EINVAL; 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"); 44*7cec18a3SMatt Redfearn return -EINVAL; 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"); 75*7cec18a3SMatt Redfearn return -EINVAL; 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"); 82*7cec18a3SMatt Redfearn return -ENOMEM; 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); 103*7cec18a3SMatt Redfearn return -EBUSY; 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 * 111*7cec18a3SMatt Redfearn * The IPIs allocated with irq_reserve_ipi() are retuerned to the system 112*7cec18a3SMatt Redfearn * destroying all virqs associated with them. 113*7cec18a3SMatt Redfearn * 114*7cec18a3SMatt Redfearn * Return 0 on success or error code on failure. 115d17bf24eSQais Yousef */ 116*7cec18a3SMatt Redfearn int irq_destroy_ipi(unsigned int irq, const struct cpumask *dest) 117d17bf24eSQais Yousef { 118d17bf24eSQais Yousef struct irq_data *data = irq_get_irq_data(irq); 119d17bf24eSQais Yousef struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL; 120d17bf24eSQais Yousef struct irq_domain *domain; 121d17bf24eSQais Yousef unsigned int nr_irqs; 122d17bf24eSQais Yousef 123d17bf24eSQais Yousef if (!irq || !data || !ipimask) 124*7cec18a3SMatt Redfearn return -EINVAL; 125d17bf24eSQais Yousef 126d17bf24eSQais Yousef domain = data->domain; 127d17bf24eSQais Yousef if (WARN_ON(domain == NULL)) 128d17bf24eSQais Yousef return; 129d17bf24eSQais Yousef 130d17bf24eSQais Yousef if (!irq_domain_is_ipi(domain)) { 131d17bf24eSQais Yousef pr_warn("Trying to destroy a non IPI domain!\n"); 132*7cec18a3SMatt Redfearn return -EINVAL; 133d17bf24eSQais Yousef } 134d17bf24eSQais Yousef 13501292ceaSMatt Redfearn if (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 */ 140*7cec18a3SMatt 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); 150*7cec18a3SMatt Redfearn return 0; 151d17bf24eSQais Yousef } 152f9bce791SQais Yousef 153f9bce791SQais Yousef /** 154f9bce791SQais Yousef * ipi_get_hwirq - Get the hwirq associated with an IPI to a cpu 155f9bce791SQais Yousef * @irq: linux irq number 156f9bce791SQais Yousef * @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 * 161f9bce791SQais Yousef * Returns hwirq value on success and 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); 166f9bce791SQais Yousef struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL; 167f9bce791SQais Yousef 168f9bce791SQais Yousef if (!data || !ipimask || cpu > nr_cpu_ids) 169f9bce791SQais Yousef return INVALID_HWIRQ; 170f9bce791SQais Yousef 171f9bce791SQais Yousef if (!cpumask_test_cpu(cpu, ipimask)) 172f9bce791SQais Yousef return INVALID_HWIRQ; 173f9bce791SQais Yousef 174f9bce791SQais Yousef /* 175f9bce791SQais Yousef * Get the real hardware irq number if the underlying implementation 176f9bce791SQais Yousef * uses a seperate irq per cpu. If the underlying implementation uses 177f9bce791SQais Yousef * a single hardware irq for all cpus then the IPI send mechanism 1783b8e29a8SQais Yousef * needs to take care of the cpu destinations. 179f9bce791SQais Yousef */ 180f9bce791SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain)) 181f9bce791SQais Yousef data = irq_get_irq_data(irq + cpu - data->common->ipi_offset); 182f9bce791SQais Yousef 183f9bce791SQais Yousef return data ? irqd_to_hwirq(data) : INVALID_HWIRQ; 184f9bce791SQais Yousef } 185f9bce791SQais Yousef EXPORT_SYMBOL_GPL(ipi_get_hwirq); 1863b8e29a8SQais Yousef 1873b8e29a8SQais Yousef static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data, 1883b8e29a8SQais Yousef const struct cpumask *dest, unsigned int cpu) 1893b8e29a8SQais Yousef { 1903b8e29a8SQais Yousef struct cpumask *ipimask = irq_data_get_affinity_mask(data); 1913b8e29a8SQais Yousef 1923b8e29a8SQais Yousef if (!chip || !ipimask) 1933b8e29a8SQais Yousef return -EINVAL; 1943b8e29a8SQais Yousef 1953b8e29a8SQais Yousef if (!chip->ipi_send_single && !chip->ipi_send_mask) 1963b8e29a8SQais Yousef return -EINVAL; 1973b8e29a8SQais Yousef 1983b8e29a8SQais Yousef if (cpu > nr_cpu_ids) 1993b8e29a8SQais Yousef return -EINVAL; 2003b8e29a8SQais Yousef 2013b8e29a8SQais Yousef if (dest) { 2023b8e29a8SQais Yousef if (!cpumask_subset(dest, ipimask)) 2033b8e29a8SQais Yousef return -EINVAL; 2043b8e29a8SQais Yousef } else { 2053b8e29a8SQais Yousef if (!cpumask_test_cpu(cpu, ipimask)) 2063b8e29a8SQais Yousef return -EINVAL; 2073b8e29a8SQais Yousef } 2083b8e29a8SQais Yousef return 0; 2093b8e29a8SQais Yousef } 2103b8e29a8SQais Yousef 2113b8e29a8SQais Yousef /** 2123b8e29a8SQais Yousef * __ipi_send_single - send an IPI to a target Linux SMP CPU 2133b8e29a8SQais Yousef * @desc: pointer to irq_desc of the IRQ 2143b8e29a8SQais Yousef * @cpu: destination CPU, must in the destination mask passed to 2153b8e29a8SQais Yousef * irq_reserve_ipi() 2163b8e29a8SQais Yousef * 2173b8e29a8SQais Yousef * This function is for architecture or core code to speed up IPI sending. Not 2183b8e29a8SQais Yousef * usable from driver code. 2193b8e29a8SQais Yousef * 2203b8e29a8SQais Yousef * Returns zero on success and negative error number on failure. 2213b8e29a8SQais Yousef */ 2223b8e29a8SQais Yousef int __ipi_send_single(struct irq_desc *desc, unsigned int cpu) 2233b8e29a8SQais Yousef { 2243b8e29a8SQais Yousef struct irq_data *data = irq_desc_get_irq_data(desc); 2253b8e29a8SQais Yousef struct irq_chip *chip = irq_data_get_irq_chip(data); 2263b8e29a8SQais Yousef 2273b8e29a8SQais Yousef #ifdef DEBUG 2283b8e29a8SQais Yousef /* 2293b8e29a8SQais Yousef * Minimise the overhead by omitting the checks for Linux SMP IPIs. 2303b8e29a8SQais Yousef * Since the callers should be arch or core code which is generally 2313b8e29a8SQais Yousef * trusted, only check for errors when debugging. 2323b8e29a8SQais Yousef */ 2333b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu))) 2343b8e29a8SQais Yousef return -EINVAL; 2353b8e29a8SQais Yousef #endif 2363b8e29a8SQais Yousef if (!chip->ipi_send_single) { 2373b8e29a8SQais Yousef chip->ipi_send_mask(data, cpumask_of(cpu)); 2383b8e29a8SQais Yousef return 0; 2393b8e29a8SQais Yousef } 2403b8e29a8SQais Yousef 2413b8e29a8SQais Yousef /* FIXME: Store this information in irqdata flags */ 2423b8e29a8SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain) && 2433b8e29a8SQais Yousef cpu != data->common->ipi_offset) { 2443b8e29a8SQais Yousef /* use the correct data for that cpu */ 2453b8e29a8SQais Yousef unsigned irq = data->irq + cpu - data->common->ipi_offset; 2463b8e29a8SQais Yousef 2473b8e29a8SQais Yousef data = irq_get_irq_data(irq); 2483b8e29a8SQais Yousef } 2493b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2503b8e29a8SQais Yousef return 0; 2513b8e29a8SQais Yousef } 2523b8e29a8SQais Yousef 2533b8e29a8SQais Yousef /** 2543b8e29a8SQais Yousef * ipi_send_mask - send an IPI to target Linux SMP CPU(s) 2553b8e29a8SQais Yousef * @desc: pointer to irq_desc of the IRQ 2563b8e29a8SQais Yousef * @dest: dest CPU(s), must be a subset of the mask passed to 2573b8e29a8SQais Yousef * irq_reserve_ipi() 2583b8e29a8SQais Yousef * 2593b8e29a8SQais Yousef * This function is for architecture or core code to speed up IPI sending. Not 2603b8e29a8SQais Yousef * usable from driver code. 2613b8e29a8SQais Yousef * 2623b8e29a8SQais Yousef * Returns zero on success and negative error number on failure. 2633b8e29a8SQais Yousef */ 2643b8e29a8SQais Yousef int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest) 2653b8e29a8SQais Yousef { 2663b8e29a8SQais Yousef struct irq_data *data = irq_desc_get_irq_data(desc); 2673b8e29a8SQais Yousef struct irq_chip *chip = irq_data_get_irq_chip(data); 2683b8e29a8SQais Yousef unsigned int cpu; 2693b8e29a8SQais Yousef 2703b8e29a8SQais Yousef #ifdef DEBUG 2713b8e29a8SQais Yousef /* 2723b8e29a8SQais Yousef * Minimise the overhead by omitting the checks for Linux SMP IPIs. 2733b8e29a8SQais Yousef * Since the callers should be arch or core code which is generally 2743b8e29a8SQais Yousef * trusted, only check for errors when debugging. 2753b8e29a8SQais Yousef */ 2763b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0))) 2773b8e29a8SQais Yousef return -EINVAL; 2783b8e29a8SQais Yousef #endif 2793b8e29a8SQais Yousef if (chip->ipi_send_mask) { 2803b8e29a8SQais Yousef chip->ipi_send_mask(data, dest); 2813b8e29a8SQais Yousef return 0; 2823b8e29a8SQais Yousef } 2833b8e29a8SQais Yousef 2843b8e29a8SQais Yousef if (irq_domain_is_ipi_per_cpu(data->domain)) { 2853b8e29a8SQais Yousef unsigned int base = data->irq; 2863b8e29a8SQais Yousef 2873b8e29a8SQais Yousef for_each_cpu(cpu, dest) { 2883b8e29a8SQais Yousef unsigned irq = base + cpu - data->common->ipi_offset; 2893b8e29a8SQais Yousef 2903b8e29a8SQais Yousef data = irq_get_irq_data(irq); 2913b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2923b8e29a8SQais Yousef } 2933b8e29a8SQais Yousef } else { 2943b8e29a8SQais Yousef for_each_cpu(cpu, dest) 2953b8e29a8SQais Yousef chip->ipi_send_single(data, cpu); 2963b8e29a8SQais Yousef } 2973b8e29a8SQais Yousef return 0; 2983b8e29a8SQais Yousef } 2993b8e29a8SQais Yousef 3003b8e29a8SQais Yousef /** 3013b8e29a8SQais Yousef * ipi_send_single - Send an IPI to a single CPU 3023b8e29a8SQais Yousef * @virq: linux irq number from irq_reserve_ipi() 3033b8e29a8SQais Yousef * @cpu: destination CPU, must in the destination mask passed to 3043b8e29a8SQais Yousef * irq_reserve_ipi() 3053b8e29a8SQais Yousef * 3063b8e29a8SQais Yousef * Returns zero on success and negative error number on failure. 3073b8e29a8SQais Yousef */ 3083b8e29a8SQais Yousef int ipi_send_single(unsigned int virq, unsigned int cpu) 3093b8e29a8SQais Yousef { 3103b8e29a8SQais Yousef struct irq_desc *desc = irq_to_desc(virq); 3113b8e29a8SQais Yousef struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL; 3123b8e29a8SQais Yousef struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL; 3133b8e29a8SQais Yousef 3143b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu))) 3153b8e29a8SQais Yousef return -EINVAL; 3163b8e29a8SQais Yousef 3173b8e29a8SQais Yousef return __ipi_send_single(desc, cpu); 3183b8e29a8SQais Yousef } 3193b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_single); 3203b8e29a8SQais Yousef 3213b8e29a8SQais Yousef /** 3223b8e29a8SQais Yousef * ipi_send_mask - Send an IPI to target CPU(s) 3233b8e29a8SQais Yousef * @virq: linux irq number from irq_reserve_ipi() 3243b8e29a8SQais Yousef * @dest: dest CPU(s), must be a subset of the mask passed to 3253b8e29a8SQais Yousef * irq_reserve_ipi() 3263b8e29a8SQais Yousef * 3273b8e29a8SQais Yousef * Returns zero on success and negative error number on failure. 3283b8e29a8SQais Yousef */ 3293b8e29a8SQais Yousef int ipi_send_mask(unsigned int virq, const struct cpumask *dest) 3303b8e29a8SQais Yousef { 3313b8e29a8SQais Yousef struct irq_desc *desc = irq_to_desc(virq); 3323b8e29a8SQais Yousef struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL; 3333b8e29a8SQais Yousef struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL; 3343b8e29a8SQais Yousef 3353b8e29a8SQais Yousef if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0))) 3363b8e29a8SQais Yousef return -EINVAL; 3373b8e29a8SQais Yousef 3383b8e29a8SQais Yousef return __ipi_send_mask(desc, dest); 3393b8e29a8SQais Yousef } 3403b8e29a8SQais Yousef EXPORT_SYMBOL_GPL(ipi_send_mask); 341