tcm.c 12 KB

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