cachepart.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Meta cache partition manipulation.
  3. *
  4. * Copyright 2010 Imagination Technologies Ltd.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/io.h>
  8. #include <linux/errno.h>
  9. #include <asm/processor.h>
  10. #include <asm/cachepart.h>
  11. #include <asm/metag_isa.h>
  12. #include <asm/metag_mem.h>
  13. #define SYSC_DCPART(n) (SYSC_DCPART0 + SYSC_xCPARTn_STRIDE * (n))
  14. #define SYSC_ICPART(n) (SYSC_ICPART0 + SYSC_xCPARTn_STRIDE * (n))
  15. #define CACHE_ASSOCIATIVITY 4 /* 4 way set-assosiative */
  16. #define ICACHE 0
  17. #define DCACHE 1
  18. /* The CORE_CONFIG2 register is not available on Meta 1 */
  19. #ifdef CONFIG_METAG_META21
  20. unsigned int get_dcache_size(void)
  21. {
  22. unsigned int config2 = metag_in32(METAC_CORE_CONFIG2);
  23. unsigned int sz = 0x1000 << ((config2 & METAC_CORECFG2_DCSZ_BITS)
  24. >> METAC_CORECFG2_DCSZ_S);
  25. if (config2 & METAC_CORECFG2_DCSMALL_BIT)
  26. sz >>= 6;
  27. return sz;
  28. }
  29. unsigned int get_icache_size(void)
  30. {
  31. unsigned int config2 = metag_in32(METAC_CORE_CONFIG2);
  32. unsigned int sz = 0x1000 << ((config2 & METAC_CORE_C2ICSZ_BITS)
  33. >> METAC_CORE_C2ICSZ_S);
  34. if (config2 & METAC_CORECFG2_ICSMALL_BIT)
  35. sz >>= 6;
  36. return sz;
  37. }
  38. unsigned int get_global_dcache_size(void)
  39. {
  40. unsigned int cpart = metag_in32(SYSC_DCPART(hard_processor_id()));
  41. unsigned int temp = cpart & SYSC_xCPARTG_AND_BITS;
  42. return (get_dcache_size() * ((temp >> SYSC_xCPARTG_AND_S) + 1)) >> 4;
  43. }
  44. unsigned int get_global_icache_size(void)
  45. {
  46. unsigned int cpart = metag_in32(SYSC_ICPART(hard_processor_id()));
  47. unsigned int temp = cpart & SYSC_xCPARTG_AND_BITS;
  48. return (get_icache_size() * ((temp >> SYSC_xCPARTG_AND_S) + 1)) >> 4;
  49. }
  50. static int get_thread_cache_size(unsigned int cache, int thread_id)
  51. {
  52. unsigned int cache_size;
  53. unsigned int t_cache_part;
  54. unsigned int isEnabled;
  55. unsigned int offset = 0;
  56. isEnabled = (cache == DCACHE ? metag_in32(MMCU_DCACHE_CTRL_ADDR) & 0x1 :
  57. metag_in32(MMCU_ICACHE_CTRL_ADDR) & 0x1);
  58. if (!isEnabled)
  59. return 0;
  60. #if PAGE_OFFSET >= LINGLOBAL_BASE
  61. /* Checking for global cache */
  62. cache_size = (cache == DCACHE ? get_global_dcache_size() :
  63. get_global_icache_size());
  64. offset = 8;
  65. #else
  66. cache_size = (cache == DCACHE ? get_dcache_size() :
  67. get_icache_size());
  68. #endif
  69. t_cache_part = (cache == DCACHE ?
  70. (metag_in32(SYSC_DCPART(thread_id)) >> offset) & 0xF :
  71. (metag_in32(SYSC_ICPART(thread_id)) >> offset) & 0xF);
  72. switch (t_cache_part) {
  73. case 0xF:
  74. return cache_size;
  75. case 0x7:
  76. return cache_size / 2;
  77. case 0x3:
  78. return cache_size / 4;
  79. case 0x1:
  80. return cache_size / 8;
  81. case 0:
  82. return cache_size / 16;
  83. }
  84. return -1;
  85. }
  86. void check_for_cache_aliasing(int thread_id)
  87. {
  88. int thread_cache_size;
  89. unsigned int cache_type;
  90. for (cache_type = ICACHE; cache_type <= DCACHE; cache_type++) {
  91. thread_cache_size =
  92. get_thread_cache_size(cache_type, thread_id);
  93. if (thread_cache_size < 0)
  94. pr_emerg("Can't read %s cache size\n",
  95. cache_type ? "DCACHE" : "ICACHE");
  96. else if (thread_cache_size == 0)
  97. /* Cache is off. No need to check for aliasing */
  98. continue;
  99. if (thread_cache_size / CACHE_ASSOCIATIVITY > PAGE_SIZE) {
  100. pr_emerg("Potential cache aliasing detected in %s on Thread %d\n",
  101. cache_type ? "DCACHE" : "ICACHE", thread_id);
  102. pr_warn("Total %s size: %u bytes\n",
  103. cache_type ? "DCACHE" : "ICACHE",
  104. cache_type ? get_dcache_size()
  105. : get_icache_size());
  106. pr_warn("Thread %s size: %d bytes\n",
  107. cache_type ? "CACHE" : "ICACHE",
  108. thread_cache_size);
  109. pr_warn("Page Size: %lu bytes\n", PAGE_SIZE);
  110. panic("Potential cache aliasing detected");
  111. }
  112. }
  113. }
  114. #else
  115. void check_for_cache_aliasing(int thread_id)
  116. {
  117. return;
  118. }
  119. #endif