tcm.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ARM TCM (Tightly-Coupled Memory) handling in Linux
  2. ----
  3. Written by Linus Walleij <linus.walleij@stericsson.com>
  4. Some ARM SoC:s have a so-called TCM (Tightly-Coupled Memory).
  5. This is usually just a few (4-64) KiB of RAM inside the ARM
  6. processor.
  7. Due to being embedded inside the CPU The TCM has a
  8. Harvard-architecture, so there is an ITCM (instruction TCM)
  9. and a DTCM (data TCM). The DTCM can not contain any
  10. instructions, but the ITCM can actually contain data.
  11. The size of DTCM or ITCM is minimum 4KiB so the typical
  12. minimum configuration is 4KiB ITCM and 4KiB DTCM.
  13. ARM CPU:s have special registers to read out status, physical
  14. location and size of TCM memories. arch/arm/include/asm/cputype.h
  15. defines a CPUID_TCM register that you can read out from the
  16. system control coprocessor. Documentation from ARM can be found
  17. at http://infocenter.arm.com, search for "TCM Status Register"
  18. to see documents for all CPUs. Reading this register you can
  19. determine if ITCM (bits 1-0) and/or DTCM (bit 17-16) is present
  20. in the machine.
  21. There is further a TCM region register (search for "TCM Region
  22. Registers" at the ARM site) that can report and modify the location
  23. size of TCM memories at runtime. This is used to read out and modify
  24. TCM location and size. Notice that this is not a MMU table: you
  25. actually move the physical location of the TCM around. At the
  26. place you put it, it will mask any underlying RAM from the
  27. CPU so it is usually wise not to overlap any physical RAM with
  28. the TCM.
  29. The TCM memory can then be remapped to another address again using
  30. the MMU, but notice that the TCM if often used in situations where
  31. the MMU is turned off. To avoid confusion the current Linux
  32. implementation will map the TCM 1 to 1 from physical to virtual
  33. memory in the location specified by the kernel. Currently Linux
  34. will map ITCM to 0xfffe0000 and on, and DTCM to 0xfffe8000 and
  35. on, supporting a maximum of 32KiB of ITCM and 32KiB of DTCM.
  36. Newer versions of the region registers also support dividing these
  37. TCMs in two separate banks, so for example an 8KiB ITCM is divided
  38. into two 4KiB banks with its own control registers. The idea is to
  39. be able to lock and hide one of the banks for use by the secure
  40. world (TrustZone).
  41. TCM is used for a few things:
  42. - FIQ and other interrupt handlers that need deterministic
  43. timing and cannot wait for cache misses.
  44. - Idle loops where all external RAM is set to self-refresh
  45. retention mode, so only on-chip RAM is accessible by
  46. the CPU and then we hang inside ITCM waiting for an
  47. interrupt.
  48. - Other operations which implies shutting off or reconfiguring
  49. the external RAM controller.
  50. There is an interface for using TCM on the ARM architecture
  51. in <asm/tcm.h>. Using this interface it is possible to:
  52. - Define the physical address and size of ITCM and DTCM.
  53. - Tag functions to be compiled into ITCM.
  54. - Tag data and constants to be allocated to DTCM and ITCM.
  55. - Have the remaining TCM RAM added to a special
  56. allocation pool with gen_pool_create() and gen_pool_add()
  57. and provice tcm_alloc() and tcm_free() for this
  58. memory. Such a heap is great for things like saving
  59. device state when shutting off device power domains.
  60. A machine that has TCM memory shall select HAVE_TCM from
  61. arch/arm/Kconfig for itself. Code that needs to use TCM shall
  62. #include <asm/tcm.h>
  63. Functions to go into itcm can be tagged like this:
  64. int __tcmfunc foo(int bar);
  65. Since these are marked to become long_calls and you may want
  66. to have functions called locally inside the TCM without
  67. wasting space, there is also the __tcmlocalfunc prefix that
  68. will make the call relative.
  69. Variables to go into dtcm can be tagged like this:
  70. int __tcmdata foo;
  71. Constants can be tagged like this:
  72. int __tcmconst foo;
  73. To put assembler into TCM just use
  74. .section ".tcm.text" or .section ".tcm.data"
  75. respectively.
  76. Example code:
  77. #include <asm/tcm.h>
  78. /* Uninitialized data */
  79. static u32 __tcmdata tcmvar;
  80. /* Initialized data */
  81. static u32 __tcmdata tcmassigned = 0x2BADBABEU;
  82. /* Constant */
  83. static const u32 __tcmconst tcmconst = 0xCAFEBABEU;
  84. static void __tcmlocalfunc tcm_to_tcm(void)
  85. {
  86. int i;
  87. for (i = 0; i < 100; i++)
  88. tcmvar ++;
  89. }
  90. static void __tcmfunc hello_tcm(void)
  91. {
  92. /* Some abstract code that runs in ITCM */
  93. int i;
  94. for (i = 0; i < 100; i++) {
  95. tcmvar ++;
  96. }
  97. tcm_to_tcm();
  98. }
  99. static void __init test_tcm(void)
  100. {
  101. u32 *tcmem;
  102. int i;
  103. hello_tcm();
  104. printk("Hello TCM executed from ITCM RAM\n");
  105. printk("TCM variable from testrun: %u @ %p\n", tcmvar, &tcmvar);
  106. tcmvar = 0xDEADBEEFU;
  107. printk("TCM variable: 0x%x @ %p\n", tcmvar, &tcmvar);
  108. printk("TCM assigned variable: 0x%x @ %p\n", tcmassigned, &tcmassigned);
  109. printk("TCM constant: 0x%x @ %p\n", tcmconst, &tcmconst);
  110. /* Allocate some TCM memory from the pool */
  111. tcmem = tcm_alloc(20);
  112. if (tcmem) {
  113. printk("TCM Allocated 20 bytes of TCM @ %p\n", tcmem);
  114. tcmem[0] = 0xDEADBEEFU;
  115. tcmem[1] = 0x2BADBABEU;
  116. tcmem[2] = 0xCAFEBABEU;
  117. tcmem[3] = 0xDEADBEEFU;
  118. tcmem[4] = 0x2BADBABEU;
  119. for (i = 0; i < 5; i++)
  120. printk("TCM tcmem[%d] = %08x\n", i, tcmem[i]);
  121. tcm_free(tcmem, 20);
  122. }
  123. }