ipi.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. use core::sync::atomic::{AtomicPtr, Ordering::Relaxed};
  2. use rustsbi::SbiRet;
  3. use crate::board::SBI_IMPL;
  4. use crate::riscv_spec::{current_hartid, stimecmp};
  5. use crate::sbi::hsm::remote_hsm;
  6. use crate::sbi::rfence;
  7. use crate::sbi::trap;
  8. use crate::sbi::trap_stack::ROOT_STACK;
  9. use crate::sbi::extensions::{hart_extension_probe, Extension};
  10. pub(crate) const IPI_TYPE_SSOFT: u8 = 1 << 0;
  11. pub(crate) const IPI_TYPE_FENCE: u8 = 1 << 1;
  12. #[allow(unused)]
  13. pub trait IpiDevice {
  14. fn read_mtime(&self) -> u64;
  15. fn write_mtime(&self, val: u64);
  16. fn read_mtimecmp(&self, hart_idx: usize) -> u64;
  17. fn write_mtimecmp(&self, hart_idx: usize, val: u64);
  18. fn read_msip(&self, hart_idx: usize) -> bool;
  19. fn set_msip(&self, hart_idx: usize);
  20. fn clear_msip(&self, hart_idx: usize);
  21. }
  22. pub struct SbiIpi<'a, T: IpiDevice> {
  23. pub ipi_dev: &'a AtomicPtr<T>,
  24. pub max_hart_id: usize,
  25. }
  26. impl<'a, T: IpiDevice> rustsbi::Timer for SbiIpi<'a, T> {
  27. #[inline]
  28. fn set_timer(&self, stime_value: u64) {
  29. if hart_extension_probe(current_hartid(),Extension::SSTC) {
  30. stimecmp::set(stime_value);
  31. unsafe {
  32. riscv::register::mie::set_mtimer();
  33. }
  34. } else {
  35. self.write_mtimecmp(current_hartid(), stime_value);
  36. unsafe {
  37. riscv::register::mip::clear_stimer();
  38. riscv::register::mie::set_mtimer();
  39. }
  40. }
  41. }
  42. }
  43. impl<'a, T: IpiDevice> rustsbi::Ipi for SbiIpi<'a, T> {
  44. #[inline]
  45. fn send_ipi(&self, hart_mask: rustsbi::HartMask) -> SbiRet {
  46. for hart_id in 0..=self.max_hart_id {
  47. if hart_mask.has_bit(hart_id) && remote_hsm(hart_id).unwrap().allow_ipi() {
  48. let old_ipi_type = set_ipi_type(hart_id, IPI_TYPE_SSOFT);
  49. if old_ipi_type == 0 {
  50. unsafe {
  51. (*self.ipi_dev.load(Relaxed)).set_msip(hart_id);
  52. }
  53. }
  54. }
  55. }
  56. SbiRet::success(0)
  57. }
  58. }
  59. impl<'a, T: IpiDevice> SbiIpi<'a, T> {
  60. pub fn new(ipi_dev: &'a AtomicPtr<T>, max_hart_id: usize) -> Self {
  61. Self {
  62. ipi_dev,
  63. max_hart_id,
  64. }
  65. }
  66. pub fn send_ipi_by_fence(
  67. &self,
  68. hart_mask: rustsbi::HartMask,
  69. ctx: rfence::RFenceCTX,
  70. ) -> SbiRet {
  71. for hart_id in 0..=self.max_hart_id {
  72. if hart_mask.has_bit(hart_id) && remote_hsm(hart_id).unwrap().allow_ipi() {
  73. rfence::remote_rfence(hart_id).unwrap().set(ctx);
  74. rfence::local_rfence().unwrap().add();
  75. if hart_id == current_hartid() {
  76. continue;
  77. }
  78. let old_ipi_type = set_ipi_type(hart_id, IPI_TYPE_FENCE);
  79. if old_ipi_type == 0 {
  80. unsafe {
  81. (*self.ipi_dev.load(Relaxed)).set_msip(hart_id);
  82. }
  83. }
  84. }
  85. }
  86. while !rfence::local_rfence().unwrap().is_sync() {
  87. trap::rfence_signle_handler();
  88. }
  89. SbiRet::success(0)
  90. }
  91. #[inline]
  92. pub fn get_time(&self) -> usize {
  93. unsafe { (*self.ipi_dev.load(Relaxed)).read_mtime() as usize }
  94. }
  95. #[inline]
  96. pub fn get_timeh(&self) -> usize {
  97. unsafe { ((*self.ipi_dev.load(Relaxed)).read_mtime() >> 32) as usize }
  98. }
  99. #[inline]
  100. pub fn set_msip(&self, hart_idx: usize) {
  101. unsafe { (*self.ipi_dev.load(Relaxed)).set_msip(hart_idx) }
  102. }
  103. #[inline]
  104. pub fn clear_msip(&self, hart_idx: usize) {
  105. unsafe { (*self.ipi_dev.load(Relaxed)).clear_msip(hart_idx) }
  106. }
  107. #[inline]
  108. pub fn write_mtimecmp(&self, hart_idx: usize, val: u64) {
  109. unsafe { (*self.ipi_dev.load(Relaxed)).write_mtimecmp(hart_idx, val) }
  110. }
  111. #[inline]
  112. pub fn clear(&self) {
  113. let hart_id = current_hartid();
  114. unsafe {
  115. (*self.ipi_dev.load(Relaxed)).clear_msip(hart_id);
  116. (*self.ipi_dev.load(Relaxed)).write_mtimecmp(hart_id, u64::MAX);
  117. }
  118. }
  119. }
  120. pub fn set_ipi_type(hart_id: usize, event_id: u8) -> u8 {
  121. unsafe {
  122. ROOT_STACK
  123. .get_unchecked_mut(hart_id)
  124. .hart_context()
  125. .ipi_type
  126. .fetch_or(event_id, Relaxed)
  127. }
  128. }
  129. pub fn get_and_reset_ipi_type() -> u8 {
  130. unsafe {
  131. ROOT_STACK
  132. .get_unchecked_mut(current_hartid())
  133. .hart_context()
  134. .ipi_type
  135. .swap(0, Relaxed)
  136. }
  137. }
  138. pub fn clear_msip() {
  139. unsafe { SBI_IMPL.assume_init_ref() }
  140. .ipi
  141. .as_ref()
  142. .unwrap()
  143. .clear_msip(current_hartid())
  144. }
  145. pub fn clear_mtime() {
  146. unsafe { SBI_IMPL.assume_init_ref() }
  147. .ipi
  148. .as_ref()
  149. .unwrap()
  150. .write_mtimecmp(current_hartid(), u64::MAX)
  151. }
  152. pub fn clear_all() {
  153. unsafe { SBI_IMPL.assume_init_ref() }
  154. .ipi
  155. .as_ref()
  156. .unwrap()
  157. .clear();
  158. }