001-mtk-0004-mips-add-support-for-noncached_alloc.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. From d7cfa1cb5602a1d936df36ee70869753835de28e Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Fri, 20 May 2022 11:21:51 +0800
  4. Subject: [PATCH 04/25] mips: add support for noncached_alloc()
  5. This patch adds support for noncached_alloc() which was only supported by
  6. ARM platform.
  7. Unlike the ARM platform, MMU is not used in u-boot for MIPS. Instead, KSEG
  8. is provided to access uncached memory. So most code of this patch is copied
  9. from cache.c of ARM platform, with only two differences:
  10. 1. MMU is untouched in noncached_set_region()
  11. 2. Address returned by noncached_alloc() is converted using KSEG1ADDR()
  12. Reviewed-by: Daniel Schwierzeck <[email protected]>
  13. Signed-off-by: Weijie Gao <[email protected]>
  14. ---
  15. arch/mips/include/asm/system.h | 20 ++++++++++++++++
  16. arch/mips/lib/cache.c | 43 ++++++++++++++++++++++++++++++++++
  17. 2 files changed, 63 insertions(+)
  18. diff --git a/arch/mips/include/asm/system.h b/arch/mips/include/asm/system.h
  19. index 79e638844b..89a2ac209f 100644
  20. --- a/arch/mips/include/asm/system.h
  21. +++ b/arch/mips/include/asm/system.h
  22. @@ -282,4 +282,24 @@ static inline void instruction_hazard_barrier(void)
  23. : "=&r"(tmp));
  24. }
  25. +#ifdef CONFIG_SYS_NONCACHED_MEMORY
  26. +/* 1MB granularity */
  27. +#define MMU_SECTION_SHIFT 20
  28. +#define MMU_SECTION_SIZE (1 << MMU_SECTION_SHIFT)
  29. +
  30. +/**
  31. + * noncached_init() - Initialize non-cached memory region
  32. + *
  33. + * Initialize non-cached memory area. This memory region will be typically
  34. + * located right below the malloc() area and be accessed from KSEG1.
  35. + *
  36. + * It is called during the generic post-relocation init sequence.
  37. + *
  38. + * Return: 0 if OK
  39. + */
  40. +int noncached_init(void);
  41. +
  42. +phys_addr_t noncached_alloc(size_t size, size_t align);
  43. +#endif /* CONFIG_SYS_NONCACHED_MEMORY */
  44. +
  45. #endif /* _ASM_SYSTEM_H */
  46. diff --git a/arch/mips/lib/cache.c b/arch/mips/lib/cache.c
  47. index ec652f0fba..d23b38d6b9 100644
  48. --- a/arch/mips/lib/cache.c
  49. +++ b/arch/mips/lib/cache.c
  50. @@ -6,6 +6,7 @@
  51. #include <common.h>
  52. #include <cpu_func.h>
  53. +#include <malloc.h>
  54. #include <asm/cache.h>
  55. #include <asm/cacheops.h>
  56. #include <asm/cm.h>
  57. @@ -197,3 +198,45 @@ void dcache_disable(void)
  58. /* ensure the pipeline doesn't contain now-invalid instructions */
  59. instruction_hazard_barrier();
  60. }
  61. +
  62. +#ifdef CONFIG_SYS_NONCACHED_MEMORY
  63. +static unsigned long noncached_start;
  64. +static unsigned long noncached_end;
  65. +static unsigned long noncached_next;
  66. +
  67. +void noncached_set_region(void)
  68. +{
  69. +}
  70. +
  71. +int noncached_init(void)
  72. +{
  73. + phys_addr_t start, end;
  74. + size_t size;
  75. +
  76. + /* If this calculation changes, update board_f.c:reserve_noncached() */
  77. + end = ALIGN(mem_malloc_start, MMU_SECTION_SIZE) - MMU_SECTION_SIZE;
  78. + size = ALIGN(CONFIG_SYS_NONCACHED_MEMORY, MMU_SECTION_SIZE);
  79. + start = end - size;
  80. +
  81. + debug("mapping memory %pa-%pa non-cached\n", &start, &end);
  82. +
  83. + noncached_start = start;
  84. + noncached_end = end;
  85. + noncached_next = start;
  86. +
  87. + return 0;
  88. +}
  89. +
  90. +phys_addr_t noncached_alloc(size_t size, size_t align)
  91. +{
  92. + phys_addr_t next = ALIGN(noncached_next, align);
  93. +
  94. + if (next >= noncached_end || (noncached_end - next) < size)
  95. + return 0;
  96. +
  97. + debug("allocated %zu bytes of uncached memory @%pa\n", size, &next);
  98. + noncached_next = next + size;
  99. +
  100. + return CKSEG1ADDR(next);
  101. +}
  102. +#endif /* CONFIG_SYS_NONCACHED_MEMORY */
  103. --
  104. 2.36.1