qcom-irq-combiner.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. */
  4. /*
  5. * Driver for interrupt combiners in the Top-level Control and Status
  6. * Registers (TCSR) hardware block in Qualcomm Technologies chips.
  7. * An interrupt combiner in this block combines a set of interrupts by
  8. * OR'ing the individual interrupt signals into a summary interrupt
  9. * signal routed to a parent interrupt controller, and provides read-
  10. * only, 32-bit registers to query the status of individual interrupts.
  11. * The status bit for IRQ n is bit (n % 32) within register (n / 32)
  12. * of the given combiner. Thus, each combiner can be described as a set
  13. * of register offsets and the number of IRQs managed.
  14. */
  15. #define pr_fmt(fmt) "QCOM80B1:" fmt
  16. #include <linux/acpi.h>
  17. #include <linux/irqchip/chained_irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/platform_device.h>
  20. #define REG_SIZE 32
  21. struct combiner_reg {
  22. void __iomem *addr;
  23. unsigned long enabled;
  24. };
  25. struct combiner {
  26. struct irq_domain *domain;
  27. int parent_irq;
  28. u32 nirqs;
  29. u32 nregs;
  30. struct combiner_reg regs[];
  31. };
  32. static inline int irq_nr(u32 reg, u32 bit)
  33. {
  34. return reg * REG_SIZE + bit;
  35. }
  36. /*
  37. * Handler for the cascaded IRQ.
  38. */
  39. static void combiner_handle_irq(struct irq_desc *desc)
  40. {
  41. struct combiner *combiner = irq_desc_get_handler_data(desc);
  42. struct irq_chip *chip = irq_desc_get_chip(desc);
  43. u32 reg;
  44. chained_irq_enter(chip, desc);
  45. for (reg = 0; reg < combiner->nregs; reg++) {
  46. int hwirq;
  47. u32 bit;
  48. u32 status;
  49. bit = readl_relaxed(combiner->regs[reg].addr);
  50. status = bit & combiner->regs[reg].enabled;
  51. if (bit && !status)
  52. pr_warn_ratelimited("Unexpected IRQ on CPU%d: (%08x %08lx %p)\n",
  53. smp_processor_id(), bit,
  54. combiner->regs[reg].enabled,
  55. combiner->regs[reg].addr);
  56. while (status) {
  57. bit = __ffs(status);
  58. status &= ~(1 << bit);
  59. hwirq = irq_nr(reg, bit);
  60. generic_handle_domain_irq(combiner->domain, hwirq);
  61. }
  62. }
  63. chained_irq_exit(chip, desc);
  64. }
  65. static void combiner_irq_chip_mask_irq(struct irq_data *data)
  66. {
  67. struct combiner *combiner = irq_data_get_irq_chip_data(data);
  68. struct combiner_reg *reg = combiner->regs + data->hwirq / REG_SIZE;
  69. clear_bit(data->hwirq % REG_SIZE, &reg->enabled);
  70. }
  71. static void combiner_irq_chip_unmask_irq(struct irq_data *data)
  72. {
  73. struct combiner *combiner = irq_data_get_irq_chip_data(data);
  74. struct combiner_reg *reg = combiner->regs + data->hwirq / REG_SIZE;
  75. set_bit(data->hwirq % REG_SIZE, &reg->enabled);
  76. }
  77. static struct irq_chip irq_chip = {
  78. .irq_mask = combiner_irq_chip_mask_irq,
  79. .irq_unmask = combiner_irq_chip_unmask_irq,
  80. .name = "qcom-irq-combiner"
  81. };
  82. static int combiner_irq_map(struct irq_domain *domain, unsigned int irq,
  83. irq_hw_number_t hwirq)
  84. {
  85. irq_set_chip_and_handler(irq, &irq_chip, handle_level_irq);
  86. irq_set_chip_data(irq, domain->host_data);
  87. irq_set_noprobe(irq);
  88. return 0;
  89. }
  90. static void combiner_irq_unmap(struct irq_domain *domain, unsigned int irq)
  91. {
  92. irq_domain_reset_irq_data(irq_get_irq_data(irq));
  93. }
  94. static int combiner_irq_translate(struct irq_domain *d, struct irq_fwspec *fws,
  95. unsigned long *hwirq, unsigned int *type)
  96. {
  97. struct combiner *combiner = d->host_data;
  98. if (is_acpi_node(fws->fwnode)) {
  99. if (WARN_ON((fws->param_count != 2) ||
  100. (fws->param[0] >= combiner->nirqs) ||
  101. (fws->param[1] & IORESOURCE_IRQ_LOWEDGE) ||
  102. (fws->param[1] & IORESOURCE_IRQ_HIGHEDGE)))
  103. return -EINVAL;
  104. *hwirq = fws->param[0];
  105. *type = fws->param[1];
  106. return 0;
  107. }
  108. return -EINVAL;
  109. }
  110. static const struct irq_domain_ops domain_ops = {
  111. .map = combiner_irq_map,
  112. .unmap = combiner_irq_unmap,
  113. .translate = combiner_irq_translate
  114. };
  115. static acpi_status count_registers_cb(struct acpi_resource *ares, void *context)
  116. {
  117. int *count = context;
  118. if (ares->type == ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
  119. ++(*count);
  120. return AE_OK;
  121. }
  122. static int count_registers(struct platform_device *pdev)
  123. {
  124. acpi_handle ahandle = ACPI_HANDLE(&pdev->dev);
  125. acpi_status status;
  126. int count = 0;
  127. if (!acpi_has_method(ahandle, METHOD_NAME__CRS))
  128. return -EINVAL;
  129. status = acpi_walk_resources(ahandle, METHOD_NAME__CRS,
  130. count_registers_cb, &count);
  131. if (ACPI_FAILURE(status))
  132. return -EINVAL;
  133. return count;
  134. }
  135. struct get_registers_context {
  136. struct device *dev;
  137. struct combiner *combiner;
  138. int err;
  139. };
  140. static acpi_status get_registers_cb(struct acpi_resource *ares, void *context)
  141. {
  142. struct get_registers_context *ctx = context;
  143. struct acpi_resource_generic_register *reg;
  144. phys_addr_t paddr;
  145. void __iomem *vaddr;
  146. if (ares->type != ACPI_RESOURCE_TYPE_GENERIC_REGISTER)
  147. return AE_OK;
  148. reg = &ares->data.generic_reg;
  149. paddr = reg->address;
  150. if ((reg->space_id != ACPI_SPACE_MEM) ||
  151. (reg->bit_offset != 0) ||
  152. (reg->bit_width > REG_SIZE)) {
  153. dev_err(ctx->dev, "Bad register resource @%pa\n", &paddr);
  154. ctx->err = -EINVAL;
  155. return AE_ERROR;
  156. }
  157. vaddr = devm_ioremap(ctx->dev, reg->address, REG_SIZE);
  158. if (!vaddr) {
  159. dev_err(ctx->dev, "Can't map register @%pa\n", &paddr);
  160. ctx->err = -ENOMEM;
  161. return AE_ERROR;
  162. }
  163. ctx->combiner->regs[ctx->combiner->nregs].addr = vaddr;
  164. ctx->combiner->nirqs += reg->bit_width;
  165. ctx->combiner->nregs++;
  166. return AE_OK;
  167. }
  168. static int get_registers(struct platform_device *pdev, struct combiner *comb)
  169. {
  170. acpi_handle ahandle = ACPI_HANDLE(&pdev->dev);
  171. acpi_status status;
  172. struct get_registers_context ctx;
  173. if (!acpi_has_method(ahandle, METHOD_NAME__CRS))
  174. return -EINVAL;
  175. ctx.dev = &pdev->dev;
  176. ctx.combiner = comb;
  177. ctx.err = 0;
  178. status = acpi_walk_resources(ahandle, METHOD_NAME__CRS,
  179. get_registers_cb, &ctx);
  180. if (ACPI_FAILURE(status))
  181. return ctx.err;
  182. return 0;
  183. }
  184. static int __init combiner_probe(struct platform_device *pdev)
  185. {
  186. struct combiner *combiner;
  187. int nregs;
  188. int err;
  189. nregs = count_registers(pdev);
  190. if (nregs <= 0) {
  191. dev_err(&pdev->dev, "Error reading register resources\n");
  192. return -EINVAL;
  193. }
  194. combiner = devm_kzalloc(&pdev->dev, struct_size(combiner, regs, nregs),
  195. GFP_KERNEL);
  196. if (!combiner)
  197. return -ENOMEM;
  198. err = get_registers(pdev, combiner);
  199. if (err < 0)
  200. return err;
  201. combiner->parent_irq = platform_get_irq(pdev, 0);
  202. if (combiner->parent_irq <= 0)
  203. return -EPROBE_DEFER;
  204. combiner->domain = irq_domain_create_linear(pdev->dev.fwnode, combiner->nirqs,
  205. &domain_ops, combiner);
  206. if (!combiner->domain)
  207. /* Errors printed by irq_domain_create_linear */
  208. return -ENODEV;
  209. irq_set_chained_handler_and_data(combiner->parent_irq,
  210. combiner_handle_irq, combiner);
  211. dev_info(&pdev->dev, "Initialized with [p=%d,n=%d,r=%p]\n",
  212. combiner->parent_irq, combiner->nirqs, combiner->regs[0].addr);
  213. return 0;
  214. }
  215. static const struct acpi_device_id qcom_irq_combiner_ids[] = {
  216. { "QCOM80B1", },
  217. { }
  218. };
  219. static struct platform_driver qcom_irq_combiner_probe = {
  220. .driver = {
  221. .name = "qcom-irq-combiner",
  222. .acpi_match_table = ACPI_PTR(qcom_irq_combiner_ids),
  223. },
  224. .probe = combiner_probe,
  225. };
  226. builtin_platform_driver(qcom_irq_combiner_probe);