plic.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. */
  10. #include "rtthread.h"
  11. #include "plic.h"
  12. #include <riscv_io.h>
  13. #include "encoding.h"
  14. /*
  15. * Each PLIC interrupt source can be assigned a priority by writing
  16. * to its 32-bit memory-mapped priority register.
  17. * The QEMU-virt (the same as FU540-C000) supports 7 levels of priority.
  18. * A priority value of 0 is reserved to mean "never interrupt" and
  19. * effectively disables the interrupt.
  20. * Priority 1 is the lowest active priority, and priority 7 is the highest.
  21. * Ties between global interrupts of the same priority are broken by
  22. * the Interrupt ID; interrupts with the lowest ID have the highest
  23. * effective priority.
  24. */
  25. void plic_set_priority(int irq, int priority)
  26. {
  27. *(uint32_t*)PLIC_PRIORITY(irq) = priority;
  28. }
  29. /*
  30. * Each global interrupt can be enabled by setting the corresponding
  31. * bit in the enables registers.
  32. */
  33. void plic_irq_enable(int irq)
  34. {
  35. int hart = r_mhartid();
  36. *(uint32_t*)PLIC_ENABLE(hart) = ((*(uint32_t*)PLIC_ENABLE(hart)) | (1 << irq));
  37. #ifdef RISCV_S_MODE
  38. set_csr(sie, read_csr(sie) | MIP_SEIP);
  39. #else
  40. set_csr(mie, read_csr(mie) | MIP_MEIP);
  41. #endif
  42. }
  43. void plic_irq_disable(int irq)
  44. {
  45. int hart = r_mhartid();
  46. *(uint32_t*)PLIC_ENABLE(hart) = (((*(uint32_t*)PLIC_ENABLE(hart)) & (~(1 << irq))));
  47. }
  48. /*
  49. * PLIC will mask all interrupts of a priority less than or equal to threshold.
  50. * Maximum threshold is 7.
  51. * For example, a threshold value of zero permits all interrupts with
  52. * non-zero priority, whereas a value of 7 masks all interrupts.
  53. * Notice, the threshold is global for PLIC, not for each interrupt source.
  54. */
  55. void plic_set_threshold(int threshold)
  56. {
  57. int hart = r_mhartid();
  58. *(uint32_t*)PLIC_THRESHOLD(hart) = threshold;
  59. }
  60. /*
  61. * DESCRIPTION:
  62. * Query the PLIC what interrupt we should serve.
  63. * Perform an interrupt claim by reading the claim register, which
  64. * returns the ID of the highest-priority pending interrupt or zero if there
  65. * is no pending interrupt.
  66. * A successful claim also atomically clears the corresponding pending bit
  67. * on the interrupt source.
  68. * RETURN VALUE:
  69. * the ID of the highest-priority pending interrupt or zero if there
  70. * is no pending interrupt.
  71. */
  72. int plic_claim(void)
  73. {
  74. int hart = r_mhartid();
  75. int irq = *(uint32_t*)PLIC_CLAIM(hart);
  76. return irq;
  77. }
  78. /*
  79. * DESCRIPTION:
  80. * Writing the interrupt ID it received from the claim (irq) to the
  81. * complete register would signal the PLIC we've served this IRQ.
  82. * The PLIC does not check whether the completion ID is the same as the
  83. * last claim ID for that target. If the completion ID does not match an
  84. * interrupt source that is currently enabled for the target, the completion
  85. * is silently ignored.
  86. * RETURN VALUE: none
  87. */
  88. void plic_complete(int irq)
  89. {
  90. int hart = r_mhartid();
  91. *(uint32_t*)PLIC_COMPLETE(hart) = irq;
  92. }