pic-gic-common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-01-30 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #define DBG_TAG "pic.gic*"
  13. #define DBG_LVL DBG_LOG
  14. #include <rtdbg.h>
  15. #include <drivers/pic.h>
  16. #include "pic-gicv2.h"
  17. #include "pic-gic-common.h"
  18. void gic_common_init_quirk_ofw(const struct rt_ofw_node *ic_np, const struct gic_quirk *quirks, void *data)
  19. {
  20. for (; quirks->desc; ++quirks)
  21. {
  22. if (!quirks->compatible || !rt_ofw_node_is_compatible(ic_np, quirks->compatible))
  23. {
  24. continue;
  25. }
  26. RT_ASSERT(quirks->init != RT_NULL);
  27. if (!quirks->init(data))
  28. {
  29. LOG_I("Enable workaround for %s", quirks->desc);
  30. }
  31. }
  32. }
  33. void gic_common_init_quirk_hw(rt_uint32_t iidr, const struct gic_quirk *quirks, void *data)
  34. {
  35. for (; quirks->desc; ++quirks)
  36. {
  37. if (quirks->compatible)
  38. {
  39. continue;
  40. }
  41. if (quirks->iidr == (iidr & quirks->iidr_mask))
  42. {
  43. RT_ASSERT(quirks->init != RT_NULL);
  44. if (!quirks->init(data))
  45. {
  46. LOG_I("Enable workaround for %s", quirks->desc);
  47. }
  48. }
  49. }
  50. }
  51. void gic_common_sgi_config(void *base, void *data, int irq_base)
  52. {
  53. #ifdef RT_USING_SMP
  54. if (irq_base < 2)
  55. {
  56. struct rt_pic_irq *pirq;
  57. #define DECLARE_GIC_IPI(ipi, hwirq) \
  58. rt_pic_config_ipi(data, ipi, hwirq); \
  59. pirq = rt_pic_find_ipi(data, ipi); \
  60. pirq->mode = RT_IRQ_MODE_EDGE_RISING; \
  61. DECLARE_GIC_IPI(RT_SCHEDULE_IPI, RT_SCHEDULE_IPI);
  62. DECLARE_GIC_IPI(RT_STOP_IPI, RT_STOP_IPI);
  63. DECLARE_GIC_IPI(RT_SMP_CALL_IPI, RT_SMP_CALL_IPI);
  64. #undef DECLARE_GIC_IPI
  65. }
  66. #endif /* RT_USING_SMP */
  67. }
  68. rt_err_t gic_common_configure_irq(void *base, int irq, rt_uint32_t mode, void (*sync_access)(void *), void *data)
  69. {
  70. rt_err_t err = RT_EOK;
  71. rt_ubase_t level;
  72. rt_uint32_t val, oldval;
  73. rt_uint32_t confoff = (irq / 16) * 4;
  74. rt_uint32_t confmask = 0x2 << ((irq % 16) * 2);
  75. static struct rt_spinlock ic_lock = { 0 };
  76. level = rt_spin_lock_irqsave(&ic_lock);
  77. val = oldval = HWREG32(base + confoff);
  78. if (mode & RT_IRQ_MODE_LEVEL_MASK)
  79. {
  80. /* Level-sensitive */
  81. val &= ~confmask;
  82. }
  83. else if (mode & RT_IRQ_MODE_EDGE_BOTH)
  84. {
  85. /* Edge-triggered */
  86. val |= confmask;
  87. }
  88. if (val != oldval)
  89. {
  90. HWREG32(base + confoff) = val;
  91. if (HWREG32(base + confoff) != val)
  92. {
  93. err = -RT_EINVAL;
  94. }
  95. if (sync_access)
  96. {
  97. sync_access(data);
  98. }
  99. }
  100. rt_spin_unlock_irqrestore(&ic_lock, level);
  101. return err;
  102. }
  103. void gic_common_dist_config(void *base, int max_irqs, void (*sync_access)(void *), void *data)
  104. {
  105. rt_uint32_t i;
  106. /* Set all global interrupts to be level triggered, active low. */
  107. for (i = 32; i < max_irqs; i += 16)
  108. {
  109. HWREG32(base + GIC_DIST_CONFIG + i / 4) = GICD_INT_ACTLOW_LVLTRIG;
  110. }
  111. /* Set priority on all global interrupts. */
  112. for (i = 32; i < max_irqs; i += 4)
  113. {
  114. HWREG32(base + GIC_DIST_PRI + i * 4 / 4) = GICD_INT_DEF_PRI_X4;
  115. }
  116. /* Disable all SPIs. */
  117. for (i = 32; i < max_irqs; i += 32)
  118. {
  119. HWREG32(base + GIC_DIST_ACTIVE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  120. HWREG32(base + GIC_DIST_ENABLE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  121. }
  122. if (sync_access)
  123. {
  124. sync_access(data);
  125. }
  126. }
  127. void gic_common_cpu_config(void *base, int nr, void (*sync_access)(void *), void *data)
  128. {
  129. rt_uint32_t i;
  130. /* Disable all SGIs, PPIs. */
  131. for (i = 0; i < nr; i += 32)
  132. {
  133. HWREG32(base + GIC_DIST_ACTIVE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  134. HWREG32(base + GIC_DIST_ENABLE_CLEAR + i / 8) = GICD_INT_EN_CLR_X32;
  135. }
  136. /* Set priority on all PPI and SGI. */
  137. for (i = 0; i < nr; i += 4)
  138. {
  139. HWREG32(base + GIC_DIST_PRI + i * 4 / 4) = GICD_INT_DEF_PRI_X4;
  140. }
  141. if (sync_access)
  142. {
  143. sync_access(data);
  144. }
  145. }
  146. void gic_fill_ppi_affinity(rt_bitmap_t *affinity)
  147. {
  148. for (int cpuid = 0; cpuid < RT_CPUS_NR; ++cpuid)
  149. {
  150. RT_IRQ_AFFINITY_SET(affinity, cpuid);
  151. }
  152. }