irq-partition-percpu.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2016 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqchip.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/irqchip/irq-partition-percpu.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. struct partition_desc {
  26. int nr_parts;
  27. struct partition_affinity *parts;
  28. struct irq_domain *domain;
  29. struct irq_desc *chained_desc;
  30. unsigned long *bitmap;
  31. struct irq_domain_ops ops;
  32. };
  33. static bool partition_check_cpu(struct partition_desc *part,
  34. unsigned int cpu, unsigned int hwirq)
  35. {
  36. return cpumask_test_cpu(cpu, &part->parts[hwirq].mask);
  37. }
  38. static void partition_irq_mask(struct irq_data *d)
  39. {
  40. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  41. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  42. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  43. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  44. chip->irq_mask)
  45. chip->irq_mask(data);
  46. }
  47. static void partition_irq_unmask(struct irq_data *d)
  48. {
  49. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  50. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  51. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  52. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  53. chip->irq_unmask)
  54. chip->irq_unmask(data);
  55. }
  56. static int partition_irq_set_irqchip_state(struct irq_data *d,
  57. enum irqchip_irq_state which,
  58. bool val)
  59. {
  60. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  61. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  62. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  63. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  64. chip->irq_set_irqchip_state)
  65. return chip->irq_set_irqchip_state(data, which, val);
  66. return -EINVAL;
  67. }
  68. static int partition_irq_get_irqchip_state(struct irq_data *d,
  69. enum irqchip_irq_state which,
  70. bool *val)
  71. {
  72. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  73. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  74. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  75. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  76. chip->irq_get_irqchip_state)
  77. return chip->irq_get_irqchip_state(data, which, val);
  78. return -EINVAL;
  79. }
  80. static int partition_irq_set_type(struct irq_data *d, unsigned int type)
  81. {
  82. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  83. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  84. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  85. if (chip->irq_set_type)
  86. return chip->irq_set_type(data, type);
  87. return -EINVAL;
  88. }
  89. static void partition_irq_print_chip(struct irq_data *d, struct seq_file *p)
  90. {
  91. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  92. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  93. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  94. seq_printf(p, " %5s-%lu", chip->name, data->hwirq);
  95. }
  96. static struct irq_chip partition_irq_chip = {
  97. .irq_mask = partition_irq_mask,
  98. .irq_unmask = partition_irq_unmask,
  99. .irq_set_type = partition_irq_set_type,
  100. .irq_get_irqchip_state = partition_irq_get_irqchip_state,
  101. .irq_set_irqchip_state = partition_irq_set_irqchip_state,
  102. .irq_print_chip = partition_irq_print_chip,
  103. };
  104. static void partition_handle_irq(struct irq_desc *desc)
  105. {
  106. struct partition_desc *part = irq_desc_get_handler_data(desc);
  107. struct irq_chip *chip = irq_desc_get_chip(desc);
  108. int cpu = smp_processor_id();
  109. int hwirq;
  110. chained_irq_enter(chip, desc);
  111. for_each_set_bit(hwirq, part->bitmap, part->nr_parts) {
  112. if (partition_check_cpu(part, cpu, hwirq))
  113. break;
  114. }
  115. if (unlikely(hwirq == part->nr_parts)) {
  116. handle_bad_irq(desc);
  117. } else {
  118. unsigned int irq;
  119. irq = irq_find_mapping(part->domain, hwirq);
  120. generic_handle_irq(irq);
  121. }
  122. chained_irq_exit(chip, desc);
  123. }
  124. static int partition_domain_alloc(struct irq_domain *domain, unsigned int virq,
  125. unsigned int nr_irqs, void *arg)
  126. {
  127. int ret;
  128. irq_hw_number_t hwirq;
  129. unsigned int type;
  130. struct irq_fwspec *fwspec = arg;
  131. struct partition_desc *part;
  132. BUG_ON(nr_irqs != 1);
  133. ret = domain->ops->translate(domain, fwspec, &hwirq, &type);
  134. if (ret)
  135. return ret;
  136. part = domain->host_data;
  137. set_bit(hwirq, part->bitmap);
  138. irq_set_chained_handler_and_data(irq_desc_get_irq(part->chained_desc),
  139. partition_handle_irq, part);
  140. irq_set_percpu_devid_partition(virq, &part->parts[hwirq].mask);
  141. irq_domain_set_info(domain, virq, hwirq, &partition_irq_chip, part,
  142. handle_percpu_devid_irq, NULL, NULL);
  143. irq_set_status_flags(virq, IRQ_NOAUTOEN);
  144. return 0;
  145. }
  146. static void partition_domain_free(struct irq_domain *domain, unsigned int virq,
  147. unsigned int nr_irqs)
  148. {
  149. struct irq_data *d;
  150. BUG_ON(nr_irqs != 1);
  151. d = irq_domain_get_irq_data(domain, virq);
  152. irq_set_handler(virq, NULL);
  153. irq_domain_reset_irq_data(d);
  154. }
  155. int partition_translate_id(struct partition_desc *desc, void *partition_id)
  156. {
  157. struct partition_affinity *part = NULL;
  158. int i;
  159. for (i = 0; i < desc->nr_parts; i++) {
  160. if (desc->parts[i].partition_id == partition_id) {
  161. part = &desc->parts[i];
  162. break;
  163. }
  164. }
  165. if (WARN_ON(!part)) {
  166. pr_err("Failed to find partition\n");
  167. return -EINVAL;
  168. }
  169. return i;
  170. }
  171. struct partition_desc *partition_create_desc(struct fwnode_handle *fwnode,
  172. struct partition_affinity *parts,
  173. int nr_parts,
  174. int chained_irq,
  175. const struct irq_domain_ops *ops)
  176. {
  177. struct partition_desc *desc;
  178. struct irq_domain *d;
  179. BUG_ON(!ops->select || !ops->translate);
  180. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  181. if (!desc)
  182. return NULL;
  183. desc->ops = *ops;
  184. desc->ops.free = partition_domain_free;
  185. desc->ops.alloc = partition_domain_alloc;
  186. d = irq_domain_create_linear(fwnode, nr_parts, &desc->ops, desc);
  187. if (!d)
  188. goto out;
  189. desc->domain = d;
  190. desc->bitmap = kzalloc(sizeof(long) * BITS_TO_LONGS(nr_parts),
  191. GFP_KERNEL);
  192. if (WARN_ON(!desc->bitmap))
  193. goto out;
  194. desc->chained_desc = irq_to_desc(chained_irq);
  195. desc->nr_parts = nr_parts;
  196. desc->parts = parts;
  197. return desc;
  198. out:
  199. if (d)
  200. irq_domain_remove(d);
  201. kfree(desc);
  202. return NULL;
  203. }
  204. struct irq_domain *partition_get_domain(struct partition_desc *dsc)
  205. {
  206. if (dsc)
  207. return dsc->domain;
  208. return NULL;
  209. }