plic.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-05-20 bigmagic first version
  9. * 2022-09-16 WangXiaoyao Porting to rv64
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <stdint.h>
  14. #include "plic.h"
  15. #include <riscv_io.h>
  16. #include "encoding.h"
  17. #include <riscv.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #ifdef RT_USING_SMART
  21. #include <ioremap.h>
  22. #else
  23. #define rt_ioremap(addr, ...) (addr)
  24. #endif
  25. size_t plic_base = 0x0c000000L;
  26. /*
  27. * Each PLIC interrupt source can be assigned a priority by writing
  28. * to its 32-bit memory-mapped priority register.
  29. * The QEMU-virt (the same as FU540-C000) supports 7 levels of priority.
  30. * A priority value of 0 is reserved to mean "never interrupt" and
  31. * effectively disables the interrupt.
  32. * Priority 1 is the lowest active priority, and priority 7 is the highest.
  33. * Ties between global interrupts of the same priority are broken by
  34. * the Interrupt ID; interrupts with the lowest ID have the highest
  35. * effective priority.
  36. */
  37. void plic_set_priority(int irq, int priority)
  38. {
  39. *(uint32_t *)PLIC_PRIORITY(irq) = priority;
  40. }
  41. /*
  42. * Each global interrupt can be enabled by setting the corresponding
  43. * bit in the enables registers.
  44. */
  45. void plic_irq_enable(int irq)
  46. {
  47. int hart = __raw_hartid();
  48. *(uint32_t *)PLIC_ENABLE(hart) = ((*(uint32_t *)PLIC_ENABLE(hart)) | (1 << irq));
  49. #ifdef RISCV_S_MODE
  50. set_csr(sie, read_csr(sie) | MIP_SEIP);
  51. #else
  52. set_csr(mie, read_csr(mie) | MIP_MEIP);
  53. #endif
  54. }
  55. void plic_irq_disable(int irq)
  56. {
  57. int hart = __raw_hartid();
  58. *(uint32_t *)PLIC_ENABLE(hart) = (((*(uint32_t *)PLIC_ENABLE(hart)) & (~(1 << irq))));
  59. }
  60. /*
  61. * PLIC will mask all interrupts of a priority less than or equal to threshold.
  62. * Maximum threshold is 7.
  63. * For example, a threshold value of zero permits all interrupts with
  64. * non-zero priority, whereas a value of 7 masks all interrupts.
  65. * Notice, the threshold is global for PLIC, not for each interrupt source.
  66. */
  67. void plic_set_threshold(int threshold)
  68. {
  69. int hart = __raw_hartid();
  70. *(uint32_t *)PLIC_THRESHOLD(hart) = threshold;
  71. }
  72. /*
  73. * DESCRIPTION:
  74. * Query the PLIC what interrupt we should serve.
  75. * Perform an interrupt claim by reading the claim register, which
  76. * returns the ID of the highest-priority pending interrupt or zero if there
  77. * is no pending interrupt.
  78. * A successful claim also atomically clears the corresponding pending bit
  79. * on the interrupt source.
  80. * RETURN VALUE:
  81. * the ID of the highest-priority pending interrupt or zero if there
  82. * is no pending interrupt.
  83. */
  84. int plic_claim(void)
  85. {
  86. int hart = __raw_hartid();
  87. int irq = *(uint32_t *)PLIC_CLAIM(hart);
  88. return irq;
  89. }
  90. /*
  91. * DESCRIPTION:
  92. * Writing the interrupt ID it received from the claim (irq) to the
  93. * complete register would signal the PLIC we've served this IRQ.
  94. * The PLIC does not check whether the completion ID is the same as the
  95. * last claim ID for that target. If the completion ID does not match an
  96. * interrupt source that is currently enabled for the target, the completion
  97. * is silently ignored.
  98. * RETURN VALUE: none
  99. */
  100. void plic_complete(int irq)
  101. {
  102. int hart = __raw_hartid();
  103. *(uint32_t *)PLIC_COMPLETE(hart) = irq;
  104. }
  105. void plic_set_ie(rt_uint32_t word_index, rt_uint32_t val)
  106. {
  107. volatile void *plic_ie = (void *)(rt_size_t)(plic_base + PLIC_ENABLE_BASE + 0x80 + word_index * 4);
  108. writel(val, plic_ie);
  109. }
  110. static void _set_sie(int hartid)
  111. {
  112. for (size_t i = hartid * WORD_CNT_BYTE; i < 32; i++)
  113. plic_set_ie(i, 0xffffffff);
  114. }
  115. void plic_init()
  116. {
  117. // PLIC takes up 64 MB space
  118. plic_base = (size_t)rt_ioremap((void *)plic_base, 64 * 1024 * 1024);
  119. plic_set_threshold(0);
  120. for (int i = 0; i < CONFIG_IRQ_NR; i++)
  121. {
  122. plic_set_priority(i, 1);
  123. }
  124. // in a single core system, only current context was set
  125. _set_sie(__raw_hartid());
  126. }