ipi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * linux/kernel/irq/ipi.c
  3. *
  4. * Copyright (C) 2015 Imagination Technologies Ltd
  5. * Author: Qais Yousef <qais.yousef@imgtec.com>
  6. *
  7. * This file contains driver APIs to the IPI subsystem.
  8. */
  9. #define pr_fmt(fmt) "genirq/ipi: " fmt
  10. #include <linux/irqdomain.h>
  11. #include <linux/irq.h>
  12. /**
  13. * irq_reserve_ipi() - Setup an IPI to destination cpumask
  14. * @domain: IPI domain
  15. * @dest: cpumask of cpus which can receive the IPI
  16. *
  17. * Allocate a virq that can be used to send IPI to any CPU in dest mask.
  18. *
  19. * On success it'll return linux irq number and 0 on failure
  20. */
  21. unsigned int irq_reserve_ipi(struct irq_domain *domain,
  22. const struct cpumask *dest)
  23. {
  24. unsigned int nr_irqs, offset;
  25. struct irq_data *data;
  26. int virq, i;
  27. if (!domain ||!irq_domain_is_ipi(domain)) {
  28. pr_warn("Reservation on a non IPI domain\n");
  29. return 0;
  30. }
  31. if (!cpumask_subset(dest, cpu_possible_mask)) {
  32. pr_warn("Reservation is not in possible_cpu_mask\n");
  33. return 0;
  34. }
  35. nr_irqs = cpumask_weight(dest);
  36. if (!nr_irqs) {
  37. pr_warn("Reservation for empty destination mask\n");
  38. return 0;
  39. }
  40. if (irq_domain_is_ipi_single(domain)) {
  41. /*
  42. * If the underlying implementation uses a single HW irq on
  43. * all cpus then we only need a single Linux irq number for
  44. * it. We have no restrictions vs. the destination mask. The
  45. * underlying implementation can deal with holes nicely.
  46. */
  47. nr_irqs = 1;
  48. offset = 0;
  49. } else {
  50. unsigned int next;
  51. /*
  52. * The IPI requires a seperate HW irq on each CPU. We require
  53. * that the destination mask is consecutive. If an
  54. * implementation needs to support holes, it can reserve
  55. * several IPI ranges.
  56. */
  57. offset = cpumask_first(dest);
  58. /*
  59. * Find a hole and if found look for another set bit after the
  60. * hole. For now we don't support this scenario.
  61. */
  62. next = cpumask_next_zero(offset, dest);
  63. if (next < nr_cpu_ids)
  64. next = cpumask_next(next, dest);
  65. if (next < nr_cpu_ids) {
  66. pr_warn("Destination mask has holes\n");
  67. return 0;
  68. }
  69. }
  70. virq = irq_domain_alloc_descs(-1, nr_irqs, 0, NUMA_NO_NODE);
  71. if (virq <= 0) {
  72. pr_warn("Can't reserve IPI, failed to alloc descs\n");
  73. return 0;
  74. }
  75. virq = __irq_domain_alloc_irqs(domain, virq, nr_irqs, NUMA_NO_NODE,
  76. (void *) dest, true);
  77. if (virq <= 0) {
  78. pr_warn("Can't reserve IPI, failed to alloc hw irqs\n");
  79. goto free_descs;
  80. }
  81. for (i = 0; i < nr_irqs; i++) {
  82. data = irq_get_irq_data(virq + i);
  83. cpumask_copy(data->common->affinity, dest);
  84. data->common->ipi_offset = offset;
  85. }
  86. return virq;
  87. free_descs:
  88. irq_free_descs(virq, nr_irqs);
  89. return 0;
  90. }
  91. /**
  92. * irq_destroy_ipi() - unreserve an IPI that was previously allocated
  93. * @irq: linux irq number to be destroyed
  94. *
  95. * Return the IPIs allocated with irq_reserve_ipi() to the system destroying
  96. * all virqs associated with them.
  97. */
  98. void irq_destroy_ipi(unsigned int irq)
  99. {
  100. struct irq_data *data = irq_get_irq_data(irq);
  101. struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
  102. struct irq_domain *domain;
  103. unsigned int nr_irqs;
  104. if (!irq || !data || !ipimask)
  105. return;
  106. domain = data->domain;
  107. if (WARN_ON(domain == NULL))
  108. return;
  109. if (!irq_domain_is_ipi(domain)) {
  110. pr_warn("Trying to destroy a non IPI domain!\n");
  111. return;
  112. }
  113. if (irq_domain_is_ipi_per_cpu(domain))
  114. nr_irqs = cpumask_weight(ipimask);
  115. else
  116. nr_irqs = 1;
  117. irq_domain_free_irqs(irq, nr_irqs);
  118. }
  119. /**
  120. * ipi_get_hwirq - Get the hwirq associated with an IPI to a cpu
  121. * @irq: linux irq number
  122. * @cpu: the target cpu
  123. *
  124. * When dealing with coprocessors IPI, we need to inform the coprocessor of
  125. * the hwirq it needs to use to receive and send IPIs.
  126. *
  127. * Returns hwirq value on success and INVALID_HWIRQ on failure.
  128. */
  129. irq_hw_number_t ipi_get_hwirq(unsigned int irq, unsigned int cpu)
  130. {
  131. struct irq_data *data = irq_get_irq_data(irq);
  132. struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
  133. if (!data || !ipimask || cpu > nr_cpu_ids)
  134. return INVALID_HWIRQ;
  135. if (!cpumask_test_cpu(cpu, ipimask))
  136. return INVALID_HWIRQ;
  137. /*
  138. * Get the real hardware irq number if the underlying implementation
  139. * uses a seperate irq per cpu. If the underlying implementation uses
  140. * a single hardware irq for all cpus then the IPI send mechanism
  141. * needs to take care of the cpu destinations.
  142. */
  143. if (irq_domain_is_ipi_per_cpu(data->domain))
  144. data = irq_get_irq_data(irq + cpu - data->common->ipi_offset);
  145. return data ? irqd_to_hwirq(data) : INVALID_HWIRQ;
  146. }
  147. EXPORT_SYMBOL_GPL(ipi_get_hwirq);
  148. static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data,
  149. const struct cpumask *dest, unsigned int cpu)
  150. {
  151. struct cpumask *ipimask = irq_data_get_affinity_mask(data);
  152. if (!chip || !ipimask)
  153. return -EINVAL;
  154. if (!chip->ipi_send_single && !chip->ipi_send_mask)
  155. return -EINVAL;
  156. if (cpu > nr_cpu_ids)
  157. return -EINVAL;
  158. if (dest) {
  159. if (!cpumask_subset(dest, ipimask))
  160. return -EINVAL;
  161. } else {
  162. if (!cpumask_test_cpu(cpu, ipimask))
  163. return -EINVAL;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * __ipi_send_single - send an IPI to a target Linux SMP CPU
  169. * @desc: pointer to irq_desc of the IRQ
  170. * @cpu: destination CPU, must in the destination mask passed to
  171. * irq_reserve_ipi()
  172. *
  173. * This function is for architecture or core code to speed up IPI sending. Not
  174. * usable from driver code.
  175. *
  176. * Returns zero on success and negative error number on failure.
  177. */
  178. int __ipi_send_single(struct irq_desc *desc, unsigned int cpu)
  179. {
  180. struct irq_data *data = irq_desc_get_irq_data(desc);
  181. struct irq_chip *chip = irq_data_get_irq_chip(data);
  182. #ifdef DEBUG
  183. /*
  184. * Minimise the overhead by omitting the checks for Linux SMP IPIs.
  185. * Since the callers should be arch or core code which is generally
  186. * trusted, only check for errors when debugging.
  187. */
  188. if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
  189. return -EINVAL;
  190. #endif
  191. if (!chip->ipi_send_single) {
  192. chip->ipi_send_mask(data, cpumask_of(cpu));
  193. return 0;
  194. }
  195. /* FIXME: Store this information in irqdata flags */
  196. if (irq_domain_is_ipi_per_cpu(data->domain) &&
  197. cpu != data->common->ipi_offset) {
  198. /* use the correct data for that cpu */
  199. unsigned irq = data->irq + cpu - data->common->ipi_offset;
  200. data = irq_get_irq_data(irq);
  201. }
  202. chip->ipi_send_single(data, cpu);
  203. return 0;
  204. }
  205. /**
  206. * ipi_send_mask - send an IPI to target Linux SMP CPU(s)
  207. * @desc: pointer to irq_desc of the IRQ
  208. * @dest: dest CPU(s), must be a subset of the mask passed to
  209. * irq_reserve_ipi()
  210. *
  211. * This function is for architecture or core code to speed up IPI sending. Not
  212. * usable from driver code.
  213. *
  214. * Returns zero on success and negative error number on failure.
  215. */
  216. int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest)
  217. {
  218. struct irq_data *data = irq_desc_get_irq_data(desc);
  219. struct irq_chip *chip = irq_data_get_irq_chip(data);
  220. unsigned int cpu;
  221. #ifdef DEBUG
  222. /*
  223. * Minimise the overhead by omitting the checks for Linux SMP IPIs.
  224. * Since the callers should be arch or core code which is generally
  225. * trusted, only check for errors when debugging.
  226. */
  227. if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
  228. return -EINVAL;
  229. #endif
  230. if (chip->ipi_send_mask) {
  231. chip->ipi_send_mask(data, dest);
  232. return 0;
  233. }
  234. if (irq_domain_is_ipi_per_cpu(data->domain)) {
  235. unsigned int base = data->irq;
  236. for_each_cpu(cpu, dest) {
  237. unsigned irq = base + cpu - data->common->ipi_offset;
  238. data = irq_get_irq_data(irq);
  239. chip->ipi_send_single(data, cpu);
  240. }
  241. } else {
  242. for_each_cpu(cpu, dest)
  243. chip->ipi_send_single(data, cpu);
  244. }
  245. return 0;
  246. }
  247. /**
  248. * ipi_send_single - Send an IPI to a single CPU
  249. * @virq: linux irq number from irq_reserve_ipi()
  250. * @cpu: destination CPU, must in the destination mask passed to
  251. * irq_reserve_ipi()
  252. *
  253. * Returns zero on success and negative error number on failure.
  254. */
  255. int ipi_send_single(unsigned int virq, unsigned int cpu)
  256. {
  257. struct irq_desc *desc = irq_to_desc(virq);
  258. struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
  259. struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
  260. if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
  261. return -EINVAL;
  262. return __ipi_send_single(desc, cpu);
  263. }
  264. EXPORT_SYMBOL_GPL(ipi_send_single);
  265. /**
  266. * ipi_send_mask - Send an IPI to target CPU(s)
  267. * @virq: linux irq number from irq_reserve_ipi()
  268. * @dest: dest CPU(s), must be a subset of the mask passed to
  269. * irq_reserve_ipi()
  270. *
  271. * Returns zero on success and negative error number on failure.
  272. */
  273. int ipi_send_mask(unsigned int virq, const struct cpumask *dest)
  274. {
  275. struct irq_desc *desc = irq_to_desc(virq);
  276. struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
  277. struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
  278. if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
  279. return -EINVAL;
  280. return __ipi_send_mask(desc, dest);
  281. }
  282. EXPORT_SYMBOL_GPL(ipi_send_mask);