tcm.c 7.5 KB

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