irq-riscv-aplic-main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Western Digital Corporation or its affiliates.
  4. * Copyright (C) 2022 Ventana Micro Systems Inc.
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/irqchip/riscv-aplic.h>
  9. #include <linux/irqchip/riscv-imsic.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/printk.h>
  15. #include "irq-riscv-aplic-main.h"
  16. void aplic_irq_unmask(struct irq_data *d)
  17. {
  18. struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
  19. writel(d->hwirq, priv->regs + APLIC_SETIENUM);
  20. }
  21. void aplic_irq_mask(struct irq_data *d)
  22. {
  23. struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
  24. writel(d->hwirq, priv->regs + APLIC_CLRIENUM);
  25. }
  26. int aplic_irq_set_type(struct irq_data *d, unsigned int type)
  27. {
  28. struct aplic_priv *priv = irq_data_get_irq_chip_data(d);
  29. void __iomem *sourcecfg;
  30. u32 val = 0;
  31. switch (type) {
  32. case IRQ_TYPE_NONE:
  33. val = APLIC_SOURCECFG_SM_INACTIVE;
  34. break;
  35. case IRQ_TYPE_LEVEL_LOW:
  36. val = APLIC_SOURCECFG_SM_LEVEL_LOW;
  37. break;
  38. case IRQ_TYPE_LEVEL_HIGH:
  39. val = APLIC_SOURCECFG_SM_LEVEL_HIGH;
  40. break;
  41. case IRQ_TYPE_EDGE_FALLING:
  42. val = APLIC_SOURCECFG_SM_EDGE_FALL;
  43. break;
  44. case IRQ_TYPE_EDGE_RISING:
  45. val = APLIC_SOURCECFG_SM_EDGE_RISE;
  46. break;
  47. default:
  48. return -EINVAL;
  49. }
  50. sourcecfg = priv->regs + APLIC_SOURCECFG_BASE;
  51. sourcecfg += (d->hwirq - 1) * sizeof(u32);
  52. writel(val, sourcecfg);
  53. return 0;
  54. }
  55. int aplic_irqdomain_translate(struct irq_fwspec *fwspec, u32 gsi_base,
  56. unsigned long *hwirq, unsigned int *type)
  57. {
  58. if (WARN_ON(fwspec->param_count < 2))
  59. return -EINVAL;
  60. if (WARN_ON(!fwspec->param[0]))
  61. return -EINVAL;
  62. /* For DT, gsi_base is always zero. */
  63. *hwirq = fwspec->param[0] - gsi_base;
  64. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  65. WARN_ON(*type == IRQ_TYPE_NONE);
  66. return 0;
  67. }
  68. void aplic_init_hw_global(struct aplic_priv *priv, bool msi_mode)
  69. {
  70. u32 val;
  71. #ifdef CONFIG_RISCV_M_MODE
  72. u32 valh;
  73. if (msi_mode) {
  74. val = lower_32_bits(priv->msicfg.base_ppn);
  75. valh = FIELD_PREP(APLIC_xMSICFGADDRH_BAPPN, upper_32_bits(priv->msicfg.base_ppn));
  76. valh |= FIELD_PREP(APLIC_xMSICFGADDRH_LHXW, priv->msicfg.lhxw);
  77. valh |= FIELD_PREP(APLIC_xMSICFGADDRH_HHXW, priv->msicfg.hhxw);
  78. valh |= FIELD_PREP(APLIC_xMSICFGADDRH_LHXS, priv->msicfg.lhxs);
  79. valh |= FIELD_PREP(APLIC_xMSICFGADDRH_HHXS, priv->msicfg.hhxs);
  80. writel(val, priv->regs + APLIC_xMSICFGADDR);
  81. writel(valh, priv->regs + APLIC_xMSICFGADDRH);
  82. }
  83. #endif
  84. /* Setup APLIC domaincfg register */
  85. val = readl(priv->regs + APLIC_DOMAINCFG);
  86. val |= APLIC_DOMAINCFG_IE;
  87. if (msi_mode)
  88. val |= APLIC_DOMAINCFG_DM;
  89. writel(val, priv->regs + APLIC_DOMAINCFG);
  90. if (readl(priv->regs + APLIC_DOMAINCFG) != val)
  91. dev_warn(priv->dev, "unable to write 0x%x in domaincfg\n", val);
  92. }
  93. static void aplic_init_hw_irqs(struct aplic_priv *priv)
  94. {
  95. int i;
  96. /* Disable all interrupts */
  97. for (i = 0; i <= priv->nr_irqs; i += 32)
  98. writel(-1U, priv->regs + APLIC_CLRIE_BASE + (i / 32) * sizeof(u32));
  99. /* Set interrupt type and default priority for all interrupts */
  100. for (i = 1; i <= priv->nr_irqs; i++) {
  101. writel(0, priv->regs + APLIC_SOURCECFG_BASE + (i - 1) * sizeof(u32));
  102. writel(APLIC_DEFAULT_PRIORITY,
  103. priv->regs + APLIC_TARGET_BASE + (i - 1) * sizeof(u32));
  104. }
  105. /* Clear APLIC domaincfg */
  106. writel(0, priv->regs + APLIC_DOMAINCFG);
  107. }
  108. #ifdef CONFIG_ACPI
  109. static const struct acpi_device_id aplic_acpi_match[] = {
  110. { "RSCV0002", 0 },
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(acpi, aplic_acpi_match);
  114. #endif
  115. int aplic_setup_priv(struct aplic_priv *priv, struct device *dev, void __iomem *regs)
  116. {
  117. struct device_node *np = to_of_node(dev->fwnode);
  118. struct of_phandle_args parent;
  119. int rc;
  120. /* Save device pointer and register base */
  121. priv->dev = dev;
  122. priv->regs = regs;
  123. if (np) {
  124. /* Find out number of interrupt sources */
  125. rc = of_property_read_u32(np, "riscv,num-sources", &priv->nr_irqs);
  126. if (rc) {
  127. dev_err(dev, "failed to get number of interrupt sources\n");
  128. return rc;
  129. }
  130. /*
  131. * Find out number of IDCs based on parent interrupts
  132. *
  133. * If "msi-parent" property is present then we ignore the
  134. * APLIC IDCs which forces the APLIC driver to use MSI mode.
  135. */
  136. if (!of_property_present(np, "msi-parent")) {
  137. while (!of_irq_parse_one(np, priv->nr_idcs, &parent))
  138. priv->nr_idcs++;
  139. }
  140. } else {
  141. rc = riscv_acpi_get_gsi_info(dev->fwnode, &priv->gsi_base, &priv->acpi_aplic_id,
  142. &priv->nr_irqs, &priv->nr_idcs);
  143. if (rc) {
  144. dev_err(dev, "failed to find GSI mapping\n");
  145. return rc;
  146. }
  147. }
  148. /* Setup initial state APLIC interrupts */
  149. aplic_init_hw_irqs(priv);
  150. return 0;
  151. }
  152. static int aplic_probe(struct platform_device *pdev)
  153. {
  154. struct device *dev = &pdev->dev;
  155. bool msi_mode = false;
  156. void __iomem *regs;
  157. int rc;
  158. /* Map the MMIO registers */
  159. regs = devm_platform_ioremap_resource(pdev, 0);
  160. if (IS_ERR(regs)) {
  161. dev_err(dev, "failed map MMIO registers\n");
  162. return PTR_ERR(regs);
  163. }
  164. /*
  165. * If msi-parent property is present then setup APLIC MSI
  166. * mode otherwise setup APLIC direct mode.
  167. */
  168. if (is_of_node(dev->fwnode))
  169. msi_mode = of_property_present(to_of_node(dev->fwnode), "msi-parent");
  170. else
  171. msi_mode = imsic_acpi_get_fwnode(NULL) ? 1 : 0;
  172. if (msi_mode)
  173. rc = aplic_msi_setup(dev, regs);
  174. else
  175. rc = aplic_direct_setup(dev, regs);
  176. if (rc)
  177. dev_err_probe(dev, rc, "failed to setup APLIC in %s mode\n",
  178. msi_mode ? "MSI" : "direct");
  179. #ifdef CONFIG_ACPI
  180. if (!acpi_disabled)
  181. acpi_dev_clear_dependencies(ACPI_COMPANION(dev));
  182. #endif
  183. return rc;
  184. }
  185. static const struct of_device_id aplic_match[] = {
  186. { .compatible = "riscv,aplic" },
  187. {}
  188. };
  189. static struct platform_driver aplic_driver = {
  190. .driver = {
  191. .name = "riscv-aplic",
  192. .of_match_table = aplic_match,
  193. .acpi_match_table = ACPI_PTR(aplic_acpi_match),
  194. },
  195. .probe = aplic_probe,
  196. };
  197. builtin_platform_driver(aplic_driver);