ipi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 error code on failure
  20. */
  21. 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 -EINVAL;
  30. }
  31. if (!cpumask_subset(dest, cpu_possible_mask)) {
  32. pr_warn("Reservation is not in possible_cpu_mask\n");
  33. return -EINVAL;
  34. }
  35. nr_irqs = cpumask_weight(dest);
  36. if (!nr_irqs) {
  37. pr_warn("Reservation for empty destination mask\n");
  38. return -EINVAL;
  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 -EINVAL;
  68. }
  69. }
  70. virq = irq_domain_alloc_descs(-1, nr_irqs, 0, NUMA_NO_NODE, NULL);
  71. if (virq <= 0) {
  72. pr_warn("Can't reserve IPI, failed to alloc descs\n");
  73. return -ENOMEM;
  74. }
  75. virq = __irq_domain_alloc_irqs(domain, virq, nr_irqs, NUMA_NO_NODE,
  76. (void *) dest, true, NULL);
  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. irq_set_status_flags(virq + i, IRQ_NO_BALANCING);
  86. }
  87. return virq;
  88. free_descs:
  89. irq_free_descs(virq, nr_irqs);
  90. return -EBUSY;
  91. }
  92. /**
  93. * irq_destroy_ipi() - unreserve an IPI that was previously allocated
  94. * @irq: linux irq number to be destroyed
  95. * @dest: cpumask of cpus which should have the IPI removed
  96. *
  97. * The IPIs allocated with irq_reserve_ipi() are retuerned to the system
  98. * destroying all virqs associated with them.
  99. *
  100. * Return 0 on success or error code on failure.
  101. */
  102. int irq_destroy_ipi(unsigned int irq, const struct cpumask *dest)
  103. {
  104. struct irq_data *data = irq_get_irq_data(irq);
  105. struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
  106. struct irq_domain *domain;
  107. unsigned int nr_irqs;
  108. if (!irq || !data || !ipimask)
  109. return -EINVAL;
  110. domain = data->domain;
  111. if (WARN_ON(domain == NULL))
  112. return -EINVAL;
  113. if (!irq_domain_is_ipi(domain)) {
  114. pr_warn("Trying to destroy a non IPI domain!\n");
  115. return -EINVAL;
  116. }
  117. if (WARN_ON(!cpumask_subset(dest, ipimask)))
  118. /*
  119. * Must be destroying a subset of CPUs to which this IPI
  120. * was set up to target
  121. */
  122. return -EINVAL;
  123. if (irq_domain_is_ipi_per_cpu(domain)) {
  124. irq = irq + cpumask_first(dest) - data->common->ipi_offset;
  125. nr_irqs = cpumask_weight(dest);
  126. } else {
  127. nr_irqs = 1;
  128. }
  129. irq_domain_free_irqs(irq, nr_irqs);
  130. return 0;
  131. }
  132. /**
  133. * ipi_get_hwirq - Get the hwirq associated with an IPI to a cpu
  134. * @irq: linux irq number
  135. * @cpu: the target cpu
  136. *
  137. * When dealing with coprocessors IPI, we need to inform the coprocessor of
  138. * the hwirq it needs to use to receive and send IPIs.
  139. *
  140. * Returns hwirq value on success and INVALID_HWIRQ on failure.
  141. */
  142. irq_hw_number_t ipi_get_hwirq(unsigned int irq, unsigned int cpu)
  143. {
  144. struct irq_data *data = irq_get_irq_data(irq);
  145. struct cpumask *ipimask = data ? irq_data_get_affinity_mask(data) : NULL;
  146. if (!data || !ipimask || cpu >= nr_cpu_ids)
  147. return INVALID_HWIRQ;
  148. if (!cpumask_test_cpu(cpu, ipimask))
  149. return INVALID_HWIRQ;
  150. /*
  151. * Get the real hardware irq number if the underlying implementation
  152. * uses a seperate irq per cpu. If the underlying implementation uses
  153. * a single hardware irq for all cpus then the IPI send mechanism
  154. * needs to take care of the cpu destinations.
  155. */
  156. if (irq_domain_is_ipi_per_cpu(data->domain))
  157. data = irq_get_irq_data(irq + cpu - data->common->ipi_offset);
  158. return data ? irqd_to_hwirq(data) : INVALID_HWIRQ;
  159. }
  160. EXPORT_SYMBOL_GPL(ipi_get_hwirq);
  161. static int ipi_send_verify(struct irq_chip *chip, struct irq_data *data,
  162. const struct cpumask *dest, unsigned int cpu)
  163. {
  164. struct cpumask *ipimask = irq_data_get_affinity_mask(data);
  165. if (!chip || !ipimask)
  166. return -EINVAL;
  167. if (!chip->ipi_send_single && !chip->ipi_send_mask)
  168. return -EINVAL;
  169. if (cpu >= nr_cpu_ids)
  170. return -EINVAL;
  171. if (dest) {
  172. if (!cpumask_subset(dest, ipimask))
  173. return -EINVAL;
  174. } else {
  175. if (!cpumask_test_cpu(cpu, ipimask))
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. /**
  181. * __ipi_send_single - send an IPI to a target Linux SMP CPU
  182. * @desc: pointer to irq_desc of the IRQ
  183. * @cpu: destination CPU, must in the destination mask passed to
  184. * irq_reserve_ipi()
  185. *
  186. * This function is for architecture or core code to speed up IPI sending. Not
  187. * usable from driver code.
  188. *
  189. * Returns zero on success and negative error number on failure.
  190. */
  191. int __ipi_send_single(struct irq_desc *desc, unsigned int cpu)
  192. {
  193. struct irq_data *data = irq_desc_get_irq_data(desc);
  194. struct irq_chip *chip = irq_data_get_irq_chip(data);
  195. #ifdef DEBUG
  196. /*
  197. * Minimise the overhead by omitting the checks for Linux SMP IPIs.
  198. * Since the callers should be arch or core code which is generally
  199. * trusted, only check for errors when debugging.
  200. */
  201. if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
  202. return -EINVAL;
  203. #endif
  204. if (!chip->ipi_send_single) {
  205. chip->ipi_send_mask(data, cpumask_of(cpu));
  206. return 0;
  207. }
  208. /* FIXME: Store this information in irqdata flags */
  209. if (irq_domain_is_ipi_per_cpu(data->domain) &&
  210. cpu != data->common->ipi_offset) {
  211. /* use the correct data for that cpu */
  212. unsigned irq = data->irq + cpu - data->common->ipi_offset;
  213. data = irq_get_irq_data(irq);
  214. }
  215. chip->ipi_send_single(data, cpu);
  216. return 0;
  217. }
  218. /**
  219. * ipi_send_mask - send an IPI to target Linux SMP CPU(s)
  220. * @desc: pointer to irq_desc of the IRQ
  221. * @dest: dest CPU(s), must be a subset of the mask passed to
  222. * irq_reserve_ipi()
  223. *
  224. * This function is for architecture or core code to speed up IPI sending. Not
  225. * usable from driver code.
  226. *
  227. * Returns zero on success and negative error number on failure.
  228. */
  229. int __ipi_send_mask(struct irq_desc *desc, const struct cpumask *dest)
  230. {
  231. struct irq_data *data = irq_desc_get_irq_data(desc);
  232. struct irq_chip *chip = irq_data_get_irq_chip(data);
  233. unsigned int cpu;
  234. #ifdef DEBUG
  235. /*
  236. * Minimise the overhead by omitting the checks for Linux SMP IPIs.
  237. * Since the callers should be arch or core code which is generally
  238. * trusted, only check for errors when debugging.
  239. */
  240. if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
  241. return -EINVAL;
  242. #endif
  243. if (chip->ipi_send_mask) {
  244. chip->ipi_send_mask(data, dest);
  245. return 0;
  246. }
  247. if (irq_domain_is_ipi_per_cpu(data->domain)) {
  248. unsigned int base = data->irq;
  249. for_each_cpu(cpu, dest) {
  250. unsigned irq = base + cpu - data->common->ipi_offset;
  251. data = irq_get_irq_data(irq);
  252. chip->ipi_send_single(data, cpu);
  253. }
  254. } else {
  255. for_each_cpu(cpu, dest)
  256. chip->ipi_send_single(data, cpu);
  257. }
  258. return 0;
  259. }
  260. /**
  261. * ipi_send_single - Send an IPI to a single CPU
  262. * @virq: linux irq number from irq_reserve_ipi()
  263. * @cpu: destination CPU, must in the destination mask passed to
  264. * irq_reserve_ipi()
  265. *
  266. * Returns zero on success and negative error number on failure.
  267. */
  268. int ipi_send_single(unsigned int virq, unsigned int cpu)
  269. {
  270. struct irq_desc *desc = irq_to_desc(virq);
  271. struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
  272. struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
  273. if (WARN_ON_ONCE(ipi_send_verify(chip, data, NULL, cpu)))
  274. return -EINVAL;
  275. return __ipi_send_single(desc, cpu);
  276. }
  277. EXPORT_SYMBOL_GPL(ipi_send_single);
  278. /**
  279. * ipi_send_mask - Send an IPI to target CPU(s)
  280. * @virq: linux irq number from irq_reserve_ipi()
  281. * @dest: dest CPU(s), must be a subset of the mask passed to
  282. * irq_reserve_ipi()
  283. *
  284. * Returns zero on success and negative error number on failure.
  285. */
  286. int ipi_send_mask(unsigned int virq, const struct cpumask *dest)
  287. {
  288. struct irq_desc *desc = irq_to_desc(virq);
  289. struct irq_data *data = desc ? irq_desc_get_irq_data(desc) : NULL;
  290. struct irq_chip *chip = data ? irq_data_get_irq_chip(data) : NULL;
  291. if (WARN_ON_ONCE(ipi_send_verify(chip, data, dest, 0)))
  292. return -EINVAL;
  293. return __ipi_send_mask(desc, dest);
  294. }
  295. EXPORT_SYMBOL_GPL(ipi_send_mask);