tcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <asm/traps.h>
  21. #define TCMTR_FORMAT_MASK 0xe0000000U
  22. static struct gen_pool *tcm_pool;
  23. static bool dtcm_present;
  24. static bool itcm_present;
  25. /* TCM section definitions from the linker */
  26. extern char __itcm_start, __sitcm_text, __eitcm_text;
  27. extern char __dtcm_start, __sdtcm_data, __edtcm_data;
  28. /* These will be increased as we run */
  29. u32 dtcm_end = DTCM_OFFSET;
  30. u32 itcm_end = ITCM_OFFSET;
  31. /*
  32. * TCM memory resources
  33. */
  34. static struct resource dtcm_res = {
  35. .name = "DTCM RAM",
  36. .start = DTCM_OFFSET,
  37. .end = DTCM_OFFSET,
  38. .flags = IORESOURCE_MEM
  39. };
  40. static struct resource itcm_res = {
  41. .name = "ITCM RAM",
  42. .start = ITCM_OFFSET,
  43. .end = ITCM_OFFSET,
  44. .flags = IORESOURCE_MEM
  45. };
  46. static struct map_desc dtcm_iomap[] __initdata = {
  47. {
  48. .virtual = DTCM_OFFSET,
  49. .pfn = __phys_to_pfn(DTCM_OFFSET),
  50. .length = 0,
  51. .type = MT_MEMORY_RW_DTCM
  52. }
  53. };
  54. static struct map_desc itcm_iomap[] __initdata = {
  55. {
  56. .virtual = ITCM_OFFSET,
  57. .pfn = __phys_to_pfn(ITCM_OFFSET),
  58. .length = 0,
  59. .type = MT_MEMORY_RWX_ITCM,
  60. }
  61. };
  62. /*
  63. * Allocate a chunk of TCM memory
  64. */
  65. void *tcm_alloc(size_t len)
  66. {
  67. unsigned long vaddr;
  68. if (!tcm_pool)
  69. return NULL;
  70. vaddr = gen_pool_alloc(tcm_pool, len);
  71. if (!vaddr)
  72. return NULL;
  73. return (void *) vaddr;
  74. }
  75. EXPORT_SYMBOL(tcm_alloc);
  76. /*
  77. * Free a chunk of TCM memory
  78. */
  79. void tcm_free(void *addr, size_t len)
  80. {
  81. gen_pool_free(tcm_pool, (unsigned long) addr, len);
  82. }
  83. EXPORT_SYMBOL(tcm_free);
  84. bool tcm_dtcm_present(void)
  85. {
  86. return dtcm_present;
  87. }
  88. EXPORT_SYMBOL(tcm_dtcm_present);
  89. bool tcm_itcm_present(void)
  90. {
  91. return itcm_present;
  92. }
  93. EXPORT_SYMBOL(tcm_itcm_present);
  94. static int __init setup_tcm_bank(u8 type, u8 bank, u8 banks,
  95. u32 *offset)
  96. {
  97. const int tcm_sizes[16] = { 0, -1, -1, 4, 8, 16, 32, 64, 128,
  98. 256, 512, 1024, -1, -1, -1, -1 };
  99. u32 tcm_region;
  100. int tcm_size;
  101. /*
  102. * If there are more than one TCM bank of this type,
  103. * select the TCM bank to operate on in the TCM selection
  104. * register.
  105. */
  106. if (banks > 1)
  107. asm("mcr p15, 0, %0, c9, c2, 0"
  108. : /* No output operands */
  109. : "r" (bank));
  110. /* Read the special TCM region register c9, 0 */
  111. if (!type)
  112. asm("mrc p15, 0, %0, c9, c1, 0"
  113. : "=r" (tcm_region));
  114. else
  115. asm("mrc p15, 0, %0, c9, c1, 1"
  116. : "=r" (tcm_region));
  117. tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f];
  118. if (tcm_size < 0) {
  119. pr_err("CPU: %sTCM%d of unknown size\n",
  120. type ? "I" : "D", bank);
  121. return -EINVAL;
  122. } else if (tcm_size > 32) {
  123. pr_err("CPU: %sTCM%d larger than 32k found\n",
  124. type ? "I" : "D", bank);
  125. return -EINVAL;
  126. } else {
  127. pr_info("CPU: found %sTCM%d %dk @ %08x, %senabled\n",
  128. type ? "I" : "D",
  129. bank,
  130. tcm_size,
  131. (tcm_region & 0xfffff000U),
  132. (tcm_region & 1) ? "" : "not ");
  133. }
  134. /* Not much fun you can do with a size 0 bank */
  135. if (tcm_size == 0)
  136. return 0;
  137. /* Force move the TCM bank to where we want it, enable */
  138. tcm_region = *offset | (tcm_region & 0x00000ffeU) | 1;
  139. if (!type)
  140. asm("mcr p15, 0, %0, c9, c1, 0"
  141. : /* No output operands */
  142. : "r" (tcm_region));
  143. else
  144. asm("mcr p15, 0, %0, c9, c1, 1"
  145. : /* No output operands */
  146. : "r" (tcm_region));
  147. /* Increase offset */
  148. *offset += (tcm_size << 10);
  149. pr_info("CPU: moved %sTCM%d %dk to %08x, enabled\n",
  150. type ? "I" : "D",
  151. bank,
  152. tcm_size,
  153. (tcm_region & 0xfffff000U));
  154. return 0;
  155. }
  156. /*
  157. * When we are running in the non-secure world and the secure world
  158. * has not explicitly given us access to the TCM we will get an
  159. * undefined error when reading the TCM region register in the
  160. * setup_tcm_bank function (above).
  161. *
  162. * There are two variants of this register read that we need to trap,
  163. * the read for the data TCM and the read for the instruction TCM:
  164. * c0370628: ee196f11 mrc 15, 0, r6, cr9, cr1, {0}
  165. * c0370674: ee196f31 mrc 15, 0, r6, cr9, cr1, {1}
  166. *
  167. * Our undef hook mask explicitly matches all fields of the encoded
  168. * instruction other than the destination register. The mask also
  169. * only allows operand 2 to have the values 0 or 1.
  170. *
  171. * The undefined hook is defined as __init and __initdata, and therefore
  172. * must be removed before tcm_init returns.
  173. *
  174. * In this particular case (MRC with ARM condition code ALways) the
  175. * Thumb-2 and ARM instruction encoding are identical, so this hook
  176. * will work on a Thumb-2 kernel.
  177. *
  178. * See A8.8.107, DDI0406C_C ARM Architecture Reference Manual, Encoding
  179. * T1/A1 for the bit-by-bit details.
  180. *
  181. * mrc p15, 0, XX, c9, c1, 0
  182. * mrc p15, 0, XX, c9, c1, 1
  183. * | | | | | | | +---- opc2 0|1 = 000|001
  184. * | | | | | | +------- CRm 0 = 0001
  185. * | | | | | +----------- CRn 0 = 1001
  186. * | | | | +--------------- Rt ? = ????
  187. * | | | +------------------- opc1 0 = 000
  188. * | | +----------------------- coproc 15 = 1111
  189. * | +-------------------------- condition ALways = 1110
  190. * +----------------------------- instruction MRC = 1110
  191. *
  192. * Encoding this as per A8.8.107 of DDI0406C, Encoding T1/A1, yields:
  193. * 1111 1111 1111 1111 0000 1111 1101 1111 Required Mask
  194. * 1110 1110 0001 1001 ???? 1111 0001 0001 mrc p15, 0, XX, c9, c1, 0
  195. * 1110 1110 0001 1001 ???? 1111 0011 0001 mrc p15, 0, XX, c9, c1, 1
  196. * [ ] [ ] [ ]| [ ] [ ] [ ] [ ]| +--- CRm
  197. * | | | | | | | | +----- SBO
  198. * | | | | | | | +------- opc2
  199. * | | | | | | +----------- coproc
  200. * | | | | | +---------------- Rt
  201. * | | | | +--------------------- CRn
  202. * | | | +------------------------- SBO
  203. * | | +--------------------------- opc1
  204. * | +------------------------------- instruction
  205. * +------------------------------------ condition
  206. */
  207. #define TCM_REGION_READ_MASK 0xffff0fdf
  208. #define TCM_REGION_READ_INSTR 0xee190f11
  209. #define DEST_REG_SHIFT 12
  210. #define DEST_REG_MASK 0xf
  211. static int __init tcm_handler(struct pt_regs *regs, unsigned int instr)
  212. {
  213. regs->uregs[(instr >> DEST_REG_SHIFT) & DEST_REG_MASK] = 0;
  214. regs->ARM_pc += 4;
  215. return 0;
  216. }
  217. static struct undef_hook tcm_hook __initdata = {
  218. .instr_mask = TCM_REGION_READ_MASK,
  219. .instr_val = TCM_REGION_READ_INSTR,
  220. .cpsr_mask = MODE_MASK,
  221. .cpsr_val = SVC_MODE,
  222. .fn = tcm_handler
  223. };
  224. /*
  225. * This initializes the TCM memory
  226. */
  227. void __init tcm_init(void)
  228. {
  229. u32 tcm_status;
  230. u8 dtcm_banks;
  231. u8 itcm_banks;
  232. size_t dtcm_code_sz = &__edtcm_data - &__sdtcm_data;
  233. size_t itcm_code_sz = &__eitcm_text - &__sitcm_text;
  234. char *start;
  235. char *end;
  236. char *ram;
  237. int ret;
  238. int i;
  239. /*
  240. * Prior to ARMv5 there is no TCM, and trying to read the status
  241. * register will hang the processor.
  242. */
  243. if (cpu_architecture() < CPU_ARCH_ARMv5) {
  244. if (dtcm_code_sz || itcm_code_sz)
  245. pr_info("CPU TCM: %u bytes of DTCM and %u bytes of "
  246. "ITCM code compiled in, but no TCM present "
  247. "in pre-v5 CPU\n", dtcm_code_sz, itcm_code_sz);
  248. return;
  249. }
  250. tcm_status = read_cpuid_tcmstatus();
  251. /*
  252. * This code only supports v6-compatible TCMTR implementations.
  253. */
  254. if (tcm_status & TCMTR_FORMAT_MASK)
  255. return;
  256. dtcm_banks = (tcm_status >> 16) & 0x03;
  257. itcm_banks = (tcm_status & 0x03);
  258. register_undef_hook(&tcm_hook);
  259. /* Values greater than 2 for D/ITCM banks are "reserved" */
  260. if (dtcm_banks > 2)
  261. dtcm_banks = 0;
  262. if (itcm_banks > 2)
  263. itcm_banks = 0;
  264. /* Setup DTCM if present */
  265. if (dtcm_banks > 0) {
  266. for (i = 0; i < dtcm_banks; i++) {
  267. ret = setup_tcm_bank(0, i, dtcm_banks, &dtcm_end);
  268. if (ret)
  269. goto unregister;
  270. }
  271. /* This means you compiled more code than fits into DTCM */
  272. if (dtcm_code_sz > (dtcm_end - DTCM_OFFSET)) {
  273. pr_info("CPU DTCM: %u bytes of code compiled to "
  274. "DTCM but only %lu bytes of DTCM present\n",
  275. dtcm_code_sz, (dtcm_end - DTCM_OFFSET));
  276. goto no_dtcm;
  277. }
  278. /*
  279. * This means that the DTCM sizes were 0 or the DTCM banks
  280. * were inaccessible due to TrustZone configuration.
  281. */
  282. if (!(dtcm_end - DTCM_OFFSET))
  283. goto no_dtcm;
  284. dtcm_res.end = dtcm_end - 1;
  285. request_resource(&iomem_resource, &dtcm_res);
  286. dtcm_iomap[0].length = dtcm_end - DTCM_OFFSET;
  287. iotable_init(dtcm_iomap, 1);
  288. /* Copy data from RAM to DTCM */
  289. start = &__sdtcm_data;
  290. end = &__edtcm_data;
  291. ram = &__dtcm_start;
  292. memcpy(start, ram, dtcm_code_sz);
  293. pr_debug("CPU DTCM: copied data from %p - %p\n",
  294. start, end);
  295. dtcm_present = true;
  296. } else if (dtcm_code_sz) {
  297. pr_info("CPU DTCM: %u bytes of code compiled to DTCM but no "
  298. "DTCM banks present in CPU\n", dtcm_code_sz);
  299. }
  300. no_dtcm:
  301. /* Setup ITCM if present */
  302. if (itcm_banks > 0) {
  303. for (i = 0; i < itcm_banks; i++) {
  304. ret = setup_tcm_bank(1, i, itcm_banks, &itcm_end);
  305. if (ret)
  306. goto unregister;
  307. }
  308. /* This means you compiled more code than fits into ITCM */
  309. if (itcm_code_sz > (itcm_end - ITCM_OFFSET)) {
  310. pr_info("CPU ITCM: %u bytes of code compiled to "
  311. "ITCM but only %lu bytes of ITCM present\n",
  312. itcm_code_sz, (itcm_end - ITCM_OFFSET));
  313. goto unregister;
  314. }
  315. /*
  316. * This means that the ITCM sizes were 0 or the ITCM banks
  317. * were inaccessible due to TrustZone configuration.
  318. */
  319. if (!(itcm_end - ITCM_OFFSET))
  320. goto unregister;
  321. itcm_res.end = itcm_end - 1;
  322. request_resource(&iomem_resource, &itcm_res);
  323. itcm_iomap[0].length = itcm_end - ITCM_OFFSET;
  324. iotable_init(itcm_iomap, 1);
  325. /* Copy code from RAM to ITCM */
  326. start = &__sitcm_text;
  327. end = &__eitcm_text;
  328. ram = &__itcm_start;
  329. memcpy(start, ram, itcm_code_sz);
  330. pr_debug("CPU ITCM: copied code from %p - %p\n",
  331. start, end);
  332. itcm_present = true;
  333. } else if (itcm_code_sz) {
  334. pr_info("CPU ITCM: %u bytes of code compiled to ITCM but no "
  335. "ITCM banks present in CPU\n", itcm_code_sz);
  336. }
  337. unregister:
  338. unregister_undef_hook(&tcm_hook);
  339. }
  340. /*
  341. * This creates the TCM memory pool and has to be done later,
  342. * during the core_initicalls, since the allocator is not yet
  343. * up and running when the first initialization runs.
  344. */
  345. static int __init setup_tcm_pool(void)
  346. {
  347. u32 dtcm_pool_start = (u32) &__edtcm_data;
  348. u32 itcm_pool_start = (u32) &__eitcm_text;
  349. int ret;
  350. /*
  351. * Set up malloc pool, 2^2 = 4 bytes granularity since
  352. * the TCM is sometimes just 4 KiB. NB: pages and cache
  353. * line alignments does not matter in TCM!
  354. */
  355. tcm_pool = gen_pool_create(2, -1);
  356. pr_debug("Setting up TCM memory pool\n");
  357. /* Add the rest of DTCM to the TCM pool */
  358. if (dtcm_present) {
  359. if (dtcm_pool_start < dtcm_end) {
  360. ret = gen_pool_add(tcm_pool, dtcm_pool_start,
  361. dtcm_end - dtcm_pool_start, -1);
  362. if (ret) {
  363. pr_err("CPU DTCM: could not add DTCM " \
  364. "remainder to pool!\n");
  365. return ret;
  366. }
  367. pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \
  368. "the TCM memory pool\n",
  369. dtcm_end - dtcm_pool_start,
  370. dtcm_pool_start);
  371. }
  372. }
  373. /* Add the rest of ITCM to the TCM pool */
  374. if (itcm_present) {
  375. if (itcm_pool_start < itcm_end) {
  376. ret = gen_pool_add(tcm_pool, itcm_pool_start,
  377. itcm_end - itcm_pool_start, -1);
  378. if (ret) {
  379. pr_err("CPU ITCM: could not add ITCM " \
  380. "remainder to pool!\n");
  381. return ret;
  382. }
  383. pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \
  384. "the TCM memory pool\n",
  385. itcm_end - itcm_pool_start,
  386. itcm_pool_start);
  387. }
  388. }
  389. return 0;
  390. }
  391. core_initcall(setup_tcm_pool);