cache.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC cache.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2015 Jan Henrik Weinstock <jan.weinstock@rwth-aachen.de>
  11. */
  12. #include <asm/spr.h>
  13. #include <asm/spr_defs.h>
  14. #include <asm/cache.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/cpuinfo.h>
  17. #include <asm/tlbflush.h>
  18. /*
  19. * Check if the cache component exists.
  20. */
  21. bool cpu_cache_is_present(const unsigned int cache_type)
  22. {
  23. unsigned long upr = mfspr(SPR_UPR);
  24. unsigned long mask = SPR_UPR_UP | cache_type;
  25. return !((upr & mask) ^ mask);
  26. }
  27. static __always_inline void cache_loop(unsigned long paddr, unsigned long end,
  28. const unsigned short reg, const unsigned int cache_type)
  29. {
  30. if (!cpu_cache_is_present(cache_type))
  31. return;
  32. while (paddr < end) {
  33. mtspr(reg, paddr);
  34. paddr += L1_CACHE_BYTES;
  35. }
  36. }
  37. static __always_inline void cache_loop_page(struct page *page, const unsigned short reg,
  38. const unsigned int cache_type)
  39. {
  40. unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
  41. unsigned long end = paddr + PAGE_SIZE;
  42. paddr &= ~(L1_CACHE_BYTES - 1);
  43. cache_loop(paddr, end, reg, cache_type);
  44. }
  45. void local_dcache_page_flush(struct page *page)
  46. {
  47. cache_loop_page(page, SPR_DCBFR, SPR_UPR_DCP);
  48. }
  49. EXPORT_SYMBOL(local_dcache_page_flush);
  50. void local_icache_page_inv(struct page *page)
  51. {
  52. cache_loop_page(page, SPR_ICBIR, SPR_UPR_ICP);
  53. }
  54. EXPORT_SYMBOL(local_icache_page_inv);
  55. void local_dcache_range_flush(unsigned long start, unsigned long end)
  56. {
  57. cache_loop(start, end, SPR_DCBFR, SPR_UPR_DCP);
  58. }
  59. void local_dcache_range_inv(unsigned long start, unsigned long end)
  60. {
  61. cache_loop(start, end, SPR_DCBIR, SPR_UPR_DCP);
  62. }
  63. void local_icache_range_inv(unsigned long start, unsigned long end)
  64. {
  65. cache_loop(start, end, SPR_ICBIR, SPR_UPR_ICP);
  66. }
  67. void update_cache(struct vm_area_struct *vma, unsigned long address,
  68. pte_t *pte)
  69. {
  70. unsigned long pfn = pte_val(*pte) >> PAGE_SHIFT;
  71. struct folio *folio = page_folio(pfn_to_page(pfn));
  72. int dirty = !test_and_set_bit(PG_dc_clean, &folio->flags.f);
  73. /*
  74. * Since icaches do not snoop for updated data on OpenRISC, we
  75. * must write back and invalidate any dirty pages manually. We
  76. * can skip data pages, since they will not end up in icaches.
  77. */
  78. if ((vma->vm_flags & VM_EXEC) && dirty) {
  79. unsigned int nr = folio_nr_pages(folio);
  80. while (nr--)
  81. sync_icache_dcache(folio_page(folio, nr));
  82. }
  83. }