irq-partition-percpu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irqchip.h>
  9. #include <linux/irqchip/chained_irq.h>
  10. #include <linux/irqchip/irq-partition-percpu.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. struct partition_desc {
  15. int nr_parts;
  16. struct partition_affinity *parts;
  17. struct irq_domain *domain;
  18. struct irq_desc *chained_desc;
  19. unsigned long *bitmap;
  20. struct irq_domain_ops ops;
  21. };
  22. static bool partition_check_cpu(struct partition_desc *part,
  23. unsigned int cpu, unsigned int hwirq)
  24. {
  25. return cpumask_test_cpu(cpu, &part->parts[hwirq].mask);
  26. }
  27. static void partition_irq_mask(struct irq_data *d)
  28. {
  29. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  30. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  31. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  32. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  33. chip->irq_mask)
  34. chip->irq_mask(data);
  35. }
  36. static void partition_irq_unmask(struct irq_data *d)
  37. {
  38. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  39. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  40. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  41. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  42. chip->irq_unmask)
  43. chip->irq_unmask(data);
  44. }
  45. static int partition_irq_set_irqchip_state(struct irq_data *d,
  46. enum irqchip_irq_state which,
  47. bool val)
  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_set_irqchip_state)
  54. return chip->irq_set_irqchip_state(data, which, val);
  55. return -EINVAL;
  56. }
  57. static int partition_irq_get_irqchip_state(struct irq_data *d,
  58. enum irqchip_irq_state which,
  59. bool *val)
  60. {
  61. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  62. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  63. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  64. if (partition_check_cpu(part, smp_processor_id(), d->hwirq) &&
  65. chip->irq_get_irqchip_state)
  66. return chip->irq_get_irqchip_state(data, which, val);
  67. return -EINVAL;
  68. }
  69. static int partition_irq_set_type(struct irq_data *d, unsigned int type)
  70. {
  71. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  72. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  73. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  74. if (chip->irq_set_type)
  75. return chip->irq_set_type(data, type);
  76. return -EINVAL;
  77. }
  78. static void partition_irq_print_chip(struct irq_data *d, struct seq_file *p)
  79. {
  80. struct partition_desc *part = irq_data_get_irq_chip_data(d);
  81. struct irq_chip *chip = irq_desc_get_chip(part->chained_desc);
  82. struct irq_data *data = irq_desc_get_irq_data(part->chained_desc);
  83. seq_printf(p, " %5s-%lu", chip->name, data->hwirq);
  84. }
  85. static struct irq_chip partition_irq_chip = {
  86. .irq_mask = partition_irq_mask,
  87. .irq_unmask = partition_irq_unmask,
  88. .irq_set_type = partition_irq_set_type,
  89. .irq_get_irqchip_state = partition_irq_get_irqchip_state,
  90. .irq_set_irqchip_state = partition_irq_set_irqchip_state,
  91. .irq_print_chip = partition_irq_print_chip,
  92. };
  93. static void partition_handle_irq(struct irq_desc *desc)
  94. {
  95. struct partition_desc *part = irq_desc_get_handler_data(desc);
  96. struct irq_chip *chip = irq_desc_get_chip(desc);
  97. int cpu = smp_processor_id();
  98. int hwirq;
  99. chained_irq_enter(chip, desc);
  100. for_each_set_bit(hwirq, part->bitmap, part->nr_parts) {
  101. if (partition_check_cpu(part, cpu, hwirq))
  102. break;
  103. }
  104. if (unlikely(hwirq == part->nr_parts)) {
  105. handle_bad_irq(desc);
  106. } else {
  107. unsigned int irq;
  108. irq = irq_find_mapping(part->domain, hwirq);
  109. generic_handle_irq(irq);
  110. }
  111. chained_irq_exit(chip, desc);
  112. }
  113. static int partition_domain_alloc(struct irq_domain *domain, unsigned int virq,
  114. unsigned int nr_irqs, void *arg)
  115. {
  116. int ret;
  117. irq_hw_number_t hwirq;
  118. unsigned int type;
  119. struct irq_fwspec *fwspec = arg;
  120. struct partition_desc *part;
  121. BUG_ON(nr_irqs != 1);
  122. ret = domain->ops->translate(domain, fwspec, &hwirq, &type);
  123. if (ret)
  124. return ret;
  125. part = domain->host_data;
  126. set_bit(hwirq, part->bitmap);
  127. irq_set_chained_handler_and_data(irq_desc_get_irq(part->chained_desc),
  128. partition_handle_irq, part);
  129. irq_set_percpu_devid_partition(virq, &part->parts[hwirq].mask);
  130. irq_domain_set_info(domain, virq, hwirq, &partition_irq_chip, part,
  131. handle_percpu_devid_irq, NULL, NULL);
  132. irq_set_status_flags(virq, IRQ_NOAUTOEN);
  133. return 0;
  134. }
  135. static void partition_domain_free(struct irq_domain *domain, unsigned int virq,
  136. unsigned int nr_irqs)
  137. {
  138. struct irq_data *d;
  139. BUG_ON(nr_irqs != 1);
  140. d = irq_domain_get_irq_data(domain, virq);
  141. irq_set_handler(virq, NULL);
  142. irq_domain_reset_irq_data(d);
  143. }
  144. int partition_translate_id(struct partition_desc *desc, void *partition_id)
  145. {
  146. struct partition_affinity *part = NULL;
  147. int i;
  148. for (i = 0; i < desc->nr_parts; i++) {
  149. if (desc->parts[i].partition_id == partition_id) {
  150. part = &desc->parts[i];
  151. break;
  152. }
  153. }
  154. if (WARN_ON(!part)) {
  155. pr_err("Failed to find partition\n");
  156. return -EINVAL;
  157. }
  158. return i;
  159. }
  160. struct partition_desc *partition_create_desc(struct fwnode_handle *fwnode,
  161. struct partition_affinity *parts,
  162. int nr_parts,
  163. int chained_irq,
  164. const struct irq_domain_ops *ops)
  165. {
  166. struct partition_desc *desc;
  167. struct irq_domain *d;
  168. BUG_ON(!ops->select || !ops->translate);
  169. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  170. if (!desc)
  171. return NULL;
  172. desc->ops = *ops;
  173. desc->ops.free = partition_domain_free;
  174. desc->ops.alloc = partition_domain_alloc;
  175. d = irq_domain_create_linear(fwnode, nr_parts, &desc->ops, desc);
  176. if (!d)
  177. goto out;
  178. desc->domain = d;
  179. desc->bitmap = kcalloc(BITS_TO_LONGS(nr_parts), sizeof(long),
  180. GFP_KERNEL);
  181. if (WARN_ON(!desc->bitmap))
  182. goto out;
  183. desc->chained_desc = irq_to_desc(chained_irq);
  184. desc->nr_parts = nr_parts;
  185. desc->parts = parts;
  186. return desc;
  187. out:
  188. if (d)
  189. irq_domain_remove(d);
  190. kfree(desc);
  191. return NULL;
  192. }
  193. struct irq_domain *partition_get_domain(struct partition_desc *dsc)
  194. {
  195. if (dsc)
  196. return dsc->domain;
  197. return NULL;
  198. }