tlb.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-28 WangXiaoyao the first version
  9. */
  10. #ifndef __TLB_H__
  11. #define __TLB_H__
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <rtthread.h>
  15. #include <mm_aspace.h>
  16. #include "sbi.h"
  17. #include "riscv_mmu.h"
  18. #define HANDLE_FAULT(ret) \
  19. if (__builtin_expect((ret) != SBI_SUCCESS, 0)) \
  20. LOG_W("%s failed\n", __FUNCTION__);
  21. static inline void rt_hw_tlb_invalidate_all(void)
  22. {
  23. uintptr_t mask = -1ul;
  24. HANDLE_FAULT(sbi_remote_sfence_vma(&mask, -1ul, 0, mask));
  25. }
  26. static inline void rt_hw_tlb_invalidate_all_local(void)
  27. {
  28. __asm__ volatile("sfence.vma" ::: "memory");
  29. }
  30. static inline void rt_hw_tlb_invalidate_aspace(rt_aspace_t aspace)
  31. {
  32. // TODO ASID
  33. rt_hw_tlb_invalidate_all_local();
  34. }
  35. static inline void rt_hw_tlb_invalidate_page(rt_aspace_t aspace, void *start)
  36. {
  37. __asm__ volatile("sfence.vma %0, zero" ::"r"(start) : "memory");
  38. }
  39. static inline void rt_hw_tlb_invalidate_range(rt_aspace_t aspace, void *start,
  40. size_t size, size_t stride)
  41. {
  42. // huge page is taking as normal page
  43. if (size <= ARCH_PAGE_SIZE)
  44. {
  45. rt_hw_tlb_invalidate_page(aspace, start);
  46. }
  47. else
  48. {
  49. rt_hw_tlb_invalidate_aspace(aspace);
  50. }
  51. }
  52. #endif /* __TLB_H__ */