irq-partition-percpu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <[email protected]>
  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. generic_handle_domain_irq(part->domain, hwirq);
  108. chained_irq_exit(chip, desc);
  109. }
  110. static int partition_domain_alloc(struct irq_domain *domain, unsigned int virq,
  111. unsigned int nr_irqs, void *arg)
  112. {
  113. int ret;
  114. irq_hw_number_t hwirq;
  115. unsigned int type;
  116. struct irq_fwspec *fwspec = arg;
  117. struct partition_desc *part;
  118. BUG_ON(nr_irqs != 1);
  119. ret = domain->ops->translate(domain, fwspec, &hwirq, &type);
  120. if (ret)
  121. return ret;
  122. part = domain->host_data;
  123. set_bit(hwirq, part->bitmap);
  124. irq_set_chained_handler_and_data(irq_desc_get_irq(part->chained_desc),
  125. partition_handle_irq, part);
  126. irq_set_percpu_devid_partition(virq, &part->parts[hwirq].mask);
  127. irq_domain_set_info(domain, virq, hwirq, &partition_irq_chip, part,
  128. handle_percpu_devid_irq, NULL, NULL);
  129. irq_set_status_flags(virq, IRQ_NOAUTOEN);
  130. return 0;
  131. }
  132. static void partition_domain_free(struct irq_domain *domain, unsigned int virq,
  133. unsigned int nr_irqs)
  134. {
  135. struct irq_data *d;
  136. BUG_ON(nr_irqs != 1);
  137. d = irq_domain_get_irq_data(domain, virq);
  138. irq_set_handler(virq, NULL);
  139. irq_domain_reset_irq_data(d);
  140. }
  141. int partition_translate_id(struct partition_desc *desc, void *partition_id)
  142. {
  143. struct partition_affinity *part = NULL;
  144. int i;
  145. for (i = 0; i < desc->nr_parts; i++) {
  146. if (desc->parts[i].partition_id == partition_id) {
  147. part = &desc->parts[i];
  148. break;
  149. }
  150. }
  151. if (WARN_ON(!part)) {
  152. pr_err("Failed to find partition\n");
  153. return -EINVAL;
  154. }
  155. return i;
  156. }
  157. struct partition_desc *partition_create_desc(struct fwnode_handle *fwnode,
  158. struct partition_affinity *parts,
  159. int nr_parts,
  160. int chained_irq,
  161. const struct irq_domain_ops *ops)
  162. {
  163. struct partition_desc *desc;
  164. struct irq_domain *d;
  165. BUG_ON(!ops->select || !ops->translate);
  166. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  167. if (!desc)
  168. return NULL;
  169. desc->ops = *ops;
  170. desc->ops.free = partition_domain_free;
  171. desc->ops.alloc = partition_domain_alloc;
  172. d = irq_domain_create_linear(fwnode, nr_parts, &desc->ops, desc);
  173. if (!d)
  174. goto out;
  175. desc->domain = d;
  176. desc->bitmap = bitmap_zalloc(nr_parts, GFP_KERNEL);
  177. if (WARN_ON(!desc->bitmap))
  178. goto out;
  179. desc->chained_desc = irq_to_desc(chained_irq);
  180. desc->nr_parts = nr_parts;
  181. desc->parts = parts;
  182. return desc;
  183. out:
  184. if (d)
  185. irq_domain_remove(d);
  186. kfree(desc);
  187. return NULL;
  188. }
  189. struct irq_domain *partition_get_domain(struct partition_desc *dsc)
  190. {
  191. if (dsc)
  192. return dsc->domain;
  193. return NULL;
  194. }