tcm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2008-2009 ST-Ericsson AB
  3. * License terms: GNU General Public License (GPL) version 2
  4. * TCM memory handling for ARM systems
  5. *
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. * Author: Rickard Andersson <rickard.andersson@stericsson.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/stddef.h>
  13. #include <linux/ioport.h>
  14. #include <linux/genalloc.h>
  15. #include <linux/string.h> /* memcpy */
  16. #include <asm/cputype.h>
  17. #include <asm/mach/map.h>
  18. #include <asm/memory.h>
  19. #include <asm/system_info.h>
  20. #include "tcm.h"
  21. static struct gen_pool *tcm_pool;
  22. static bool dtcm_present;
  23. static bool itcm_present;
  24. /* TCM section definitions from the linker */
  25. extern char __itcm_start, __sitcm_text, __eitcm_text;
  26. extern char __dtcm_start, __sdtcm_data, __edtcm_data;
  27. /* These will be increased as we run */
  28. u32 dtcm_end = DTCM_OFFSET;
  29. u32 itcm_end = ITCM_OFFSET;
  30. /*
  31. * TCM memory resources
  32. */
  33. static struct resource dtcm_res = {
  34. .name = "DTCM RAM",
  35. .start = DTCM_OFFSET,
  36. .end = DTCM_OFFSET,
  37. .flags = IORESOURCE_MEM
  38. };
  39. static struct resource itcm_res = {
  40. .name = "ITCM RAM",
  41. .start = ITCM_OFFSET,
  42. .end = ITCM_OFFSET,
  43. .flags = IORESOURCE_MEM
  44. };
  45. static struct map_desc dtcm_iomap[] __initdata = {
  46. {
  47. .virtual = DTCM_OFFSET,
  48. .pfn = __phys_to_pfn(DTCM_OFFSET),
  49. .length = 0,
  50. .type = MT_MEMORY_DTCM
  51. }
  52. };
  53. static struct map_desc itcm_iomap[] __initdata = {
  54. {
  55. .virtual = ITCM_OFFSET,
  56. .pfn = __phys_to_pfn(ITCM_OFFSET),
  57. .length = 0,
  58. .type = MT_MEMORY_ITCM
  59. }
  60. };
  61. /*
  62. * Allocate a chunk of TCM memory
  63. */
  64. void *tcm_alloc(size_t len)
  65. {
  66. unsigned long vaddr;
  67. if (!tcm_pool)
  68. return NULL;
  69. vaddr = gen_pool_alloc(tcm_pool, len);
  70. if (!vaddr)
  71. return NULL;
  72. return (void *) vaddr;
  73. }
  74. EXPORT_SYMBOL(tcm_alloc);
  75. /*
  76. * Free a chunk of TCM memory
  77. */
  78. void tcm_free(void *addr, size_t len)
  79. {
  80. gen_pool_free(tcm_pool, (unsigned long) addr, len);
  81. }
  82. EXPORT_SYMBOL(tcm_free);
  83. bool tcm_dtcm_present(void)
  84. {
  85. return dtcm_present;
  86. }
  87. EXPORT_SYMBOL(tcm_dtcm_present);
  88. bool tcm_itcm_present(void)
  89. {
  90. return itcm_present;
  91. }
  92. EXPORT_SYMBOL(tcm_itcm_present);
  93. static int __init setup_tcm_bank(u8 type, u8 bank, u8 banks,
  94. u32 *offset)
  95. {
  96. const int tcm_sizes[16] = { 0, -1, -1, 4, 8, 16, 32, 64, 128,
  97. 256, 512, 1024, -1, -1, -1, -1 };
  98. u32 tcm_region;
  99. int tcm_size;
  100. /*
  101. * If there are more than one TCM bank of this type,
  102. * select the TCM bank to operate on in the TCM selection
  103. * register.
  104. */
  105. if (banks > 1)
  106. asm("mcr p15, 0, %0, c9, c2, 0"
  107. : /* No output operands */
  108. : "r" (bank));
  109. /* Read the special TCM region register c9, 0 */
  110. if (!type)
  111. asm("mrc p15, 0, %0, c9, c1, 0"
  112. : "=r" (tcm_region));
  113. else
  114. asm("mrc p15, 0, %0, c9, c1, 1"
  115. : "=r" (tcm_region));
  116. tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f];
  117. if (tcm_size < 0) {
  118. pr_err("CPU: %sTCM%d of unknown size\n",
  119. type ? "I" : "D", bank);
  120. return -EINVAL;
  121. } else if (tcm_size > 32) {
  122. pr_err("CPU: %sTCM%d larger than 32k found\n",
  123. type ? "I" : "D", bank);
  124. return -EINVAL;
  125. } else {
  126. pr_info("CPU: found %sTCM%d %dk @ %08x, %senabled\n",
  127. type ? "I" : "D",
  128. bank,
  129. tcm_size,
  130. (tcm_region & 0xfffff000U),
  131. (tcm_region & 1) ? "" : "not ");
  132. }
  133. /* Not much fun you can do with a size 0 bank */
  134. if (tcm_size == 0)
  135. return 0;
  136. /* Force move the TCM bank to where we want it, enable */
  137. tcm_region = *offset | (tcm_region & 0x00000ffeU) | 1;
  138. if (!type)
  139. asm("mcr p15, 0, %0, c9, c1, 0"
  140. : /* No output operands */
  141. : "r" (tcm_region));
  142. else
  143. asm("mcr p15, 0, %0, c9, c1, 1"
  144. : /* No output operands */
  145. : "r" (tcm_region));
  146. /* Increase offset */
  147. *offset += (tcm_size << 10);
  148. pr_info("CPU: moved %sTCM%d %dk to %08x, enabled\n",
  149. type ? "I" : "D",
  150. bank,
  151. tcm_size,
  152. (tcm_region & 0xfffff000U));
  153. return 0;
  154. }
  155. /*
  156. * This initializes the TCM memory
  157. */
  158. void __init tcm_init(void)
  159. {
  160. u32 tcm_status;
  161. u8 dtcm_banks;
  162. u8 itcm_banks;
  163. size_t dtcm_code_sz = &__edtcm_data - &__sdtcm_data;
  164. size_t itcm_code_sz = &__eitcm_text - &__sitcm_text;
  165. char *start;
  166. char *end;
  167. char *ram;
  168. int ret;
  169. int i;
  170. /*
  171. * Prior to ARMv5 there is no TCM, and trying to read the status
  172. * register will hang the processor.
  173. */
  174. if (cpu_architecture() < CPU_ARCH_ARMv5) {
  175. if (dtcm_code_sz || itcm_code_sz)
  176. pr_info("CPU TCM: %u bytes of DTCM and %u bytes of "
  177. "ITCM code compiled in, but no TCM present "
  178. "in pre-v5 CPU\n", dtcm_code_sz, itcm_code_sz);
  179. return;
  180. }
  181. tcm_status = read_cpuid_tcmstatus();
  182. dtcm_banks = (tcm_status >> 16) & 0x03;
  183. itcm_banks = (tcm_status & 0x03);
  184. /* Values greater than 2 for D/ITCM banks are "reserved" */
  185. if (dtcm_banks > 2)
  186. dtcm_banks = 0;
  187. if (itcm_banks > 2)
  188. itcm_banks = 0;
  189. /* Setup DTCM if present */
  190. if (dtcm_banks > 0) {
  191. for (i = 0; i < dtcm_banks; i++) {
  192. ret = setup_tcm_bank(0, i, dtcm_banks, &dtcm_end);
  193. if (ret)
  194. return;
  195. }
  196. /* This means you compiled more code than fits into DTCM */
  197. if (dtcm_code_sz > (dtcm_end - DTCM_OFFSET)) {
  198. pr_info("CPU DTCM: %u bytes of code compiled to "
  199. "DTCM but only %lu bytes of DTCM present\n",
  200. dtcm_code_sz, (dtcm_end - DTCM_OFFSET));
  201. goto no_dtcm;
  202. }
  203. dtcm_res.end = dtcm_end - 1;
  204. request_resource(&iomem_resource, &dtcm_res);
  205. dtcm_iomap[0].length = dtcm_end - DTCM_OFFSET;
  206. iotable_init(dtcm_iomap, 1);
  207. /* Copy data from RAM to DTCM */
  208. start = &__sdtcm_data;
  209. end = &__edtcm_data;
  210. ram = &__dtcm_start;
  211. memcpy(start, ram, dtcm_code_sz);
  212. pr_debug("CPU DTCM: copied data from %p - %p\n",
  213. start, end);
  214. dtcm_present = true;
  215. } else if (dtcm_code_sz) {
  216. pr_info("CPU DTCM: %u bytes of code compiled to DTCM but no "
  217. "DTCM banks present in CPU\n", dtcm_code_sz);
  218. }
  219. no_dtcm:
  220. /* Setup ITCM if present */
  221. if (itcm_banks > 0) {
  222. for (i = 0; i < itcm_banks; i++) {
  223. ret = setup_tcm_bank(1, i, itcm_banks, &itcm_end);
  224. if (ret)
  225. return;
  226. }
  227. /* This means you compiled more code than fits into ITCM */
  228. if (itcm_code_sz > (itcm_end - ITCM_OFFSET)) {
  229. pr_info("CPU ITCM: %u bytes of code compiled to "
  230. "ITCM but only %lu bytes of ITCM present\n",
  231. itcm_code_sz, (itcm_end - ITCM_OFFSET));
  232. return;
  233. }
  234. itcm_res.end = itcm_end - 1;
  235. request_resource(&iomem_resource, &itcm_res);
  236. itcm_iomap[0].length = itcm_end - ITCM_OFFSET;
  237. iotable_init(itcm_iomap, 1);
  238. /* Copy code from RAM to ITCM */
  239. start = &__sitcm_text;
  240. end = &__eitcm_text;
  241. ram = &__itcm_start;
  242. memcpy(start, ram, itcm_code_sz);
  243. pr_debug("CPU ITCM: copied code from %p - %p\n",
  244. start, end);
  245. itcm_present = true;
  246. } else if (itcm_code_sz) {
  247. pr_info("CPU ITCM: %u bytes of code compiled to ITCM but no "
  248. "ITCM banks present in CPU\n", itcm_code_sz);
  249. }
  250. }
  251. /*
  252. * This creates the TCM memory pool and has to be done later,
  253. * during the core_initicalls, since the allocator is not yet
  254. * up and running when the first initialization runs.
  255. */
  256. static int __init setup_tcm_pool(void)
  257. {
  258. u32 dtcm_pool_start = (u32) &__edtcm_data;
  259. u32 itcm_pool_start = (u32) &__eitcm_text;
  260. int ret;
  261. /*
  262. * Set up malloc pool, 2^2 = 4 bytes granularity since
  263. * the TCM is sometimes just 4 KiB. NB: pages and cache
  264. * line alignments does not matter in TCM!
  265. */
  266. tcm_pool = gen_pool_create(2, -1);
  267. pr_debug("Setting up TCM memory pool\n");
  268. /* Add the rest of DTCM to the TCM pool */
  269. if (dtcm_present) {
  270. if (dtcm_pool_start < dtcm_end) {
  271. ret = gen_pool_add(tcm_pool, dtcm_pool_start,
  272. dtcm_end - dtcm_pool_start, -1);
  273. if (ret) {
  274. pr_err("CPU DTCM: could not add DTCM " \
  275. "remainder to pool!\n");
  276. return ret;
  277. }
  278. pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \
  279. "the TCM memory pool\n",
  280. dtcm_end - dtcm_pool_start,
  281. dtcm_pool_start);
  282. }
  283. }
  284. /* Add the rest of ITCM to the TCM pool */
  285. if (itcm_present) {
  286. if (itcm_pool_start < itcm_end) {
  287. ret = gen_pool_add(tcm_pool, itcm_pool_start,
  288. itcm_end - itcm_pool_start, -1);
  289. if (ret) {
  290. pr_err("CPU ITCM: could not add ITCM " \
  291. "remainder to pool!\n");
  292. return ret;
  293. }
  294. pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \
  295. "the TCM memory pool\n",
  296. itcm_end - itcm_pool_start,
  297. itcm_pool_start);
  298. }
  299. }
  300. return 0;
  301. }
  302. core_initcall(setup_tcm_pool);