tcm.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 (bit 0) and/or DTCM (bit 16) is present in the
  20. 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. The TCM memory exists totally outside the MMU and will
  29. override any MMU mappings.
  30. Code executing inside the ITCM does not "see" any MMU mappings
  31. and e.g. register accesses must be made to physical addresses.
  32. TCM is used for a few things:
  33. - FIQ and other interrupt handlers that need deterministic
  34. timing and cannot wait for cache misses.
  35. - Idle loops where all external RAM is set to self-refresh
  36. retention mode, so only on-chip RAM is accessible by
  37. the CPU and then we hang inside ITCM waiting for an
  38. interrupt.
  39. - Other operations which implies shutting off or reconfiguring
  40. the external RAM controller.
  41. There is an interface for using TCM on the ARM architecture
  42. in <asm/tcm.h>. Using this interface it is possible to:
  43. - Define the physical address and size of ITCM and DTCM.
  44. - Tag functions to be compiled into ITCM.
  45. - Tag data and constants to be allocated to DTCM and ITCM.
  46. - Have the remaining TCM RAM added to a special
  47. allocation pool with gen_pool_create() and gen_pool_add()
  48. and provice tcm_alloc() and tcm_free() for this
  49. memory. Such a heap is great for things like saving
  50. device state when shutting off device power domains.
  51. A machine that has TCM memory shall select HAVE_TCM in
  52. arch/arm/Kconfig for itself, and then the
  53. rest of the functionality will depend on the physical
  54. location and size of ITCM and DTCM to be defined in
  55. mach/memory.h for the machine. Code that needs to use
  56. TCM shall #include <asm/tcm.h> If the TCM is not located
  57. at the place given in memory.h it will be moved using
  58. the TCM Region registers.
  59. Functions to go into itcm can be tagged like this:
  60. int __tcmfunc foo(int bar);
  61. Variables to go into dtcm can be tagged like this:
  62. int __tcmdata foo;
  63. Constants can be tagged like this:
  64. int __tcmconst foo;
  65. To put assembler into TCM just use
  66. .section ".tcm.text" or .section ".tcm.data"
  67. respectively.
  68. Example code:
  69. #include <asm/tcm.h>
  70. /* Uninitialized data */
  71. static u32 __tcmdata tcmvar;
  72. /* Initialized data */
  73. static u32 __tcmdata tcmassigned = 0x2BADBABEU;
  74. /* Constant */
  75. static const u32 __tcmconst tcmconst = 0xCAFEBABEU;
  76. static void __tcmlocalfunc tcm_to_tcm(void)
  77. {
  78. int i;
  79. for (i = 0; i < 100; i++)
  80. tcmvar ++;
  81. }
  82. static void __tcmfunc hello_tcm(void)
  83. {
  84. /* Some abstract code that runs in ITCM */
  85. int i;
  86. for (i = 0; i < 100; i++) {
  87. tcmvar ++;
  88. }
  89. tcm_to_tcm();
  90. }
  91. static void __init test_tcm(void)
  92. {
  93. u32 *tcmem;
  94. int i;
  95. hello_tcm();
  96. printk("Hello TCM executed from ITCM RAM\n");
  97. printk("TCM variable from testrun: %u @ %p\n", tcmvar, &tcmvar);
  98. tcmvar = 0xDEADBEEFU;
  99. printk("TCM variable: 0x%x @ %p\n", tcmvar, &tcmvar);
  100. printk("TCM assigned variable: 0x%x @ %p\n", tcmassigned, &tcmassigned);
  101. printk("TCM constant: 0x%x @ %p\n", tcmconst, &tcmconst);
  102. /* Allocate some TCM memory from the pool */
  103. tcmem = tcm_alloc(20);
  104. if (tcmem) {
  105. printk("TCM Allocated 20 bytes of TCM @ %p\n", tcmem);
  106. tcmem[0] = 0xDEADBEEFU;
  107. tcmem[1] = 0x2BADBABEU;
  108. tcmem[2] = 0xCAFEBABEU;
  109. tcmem[3] = 0xDEADBEEFU;
  110. tcmem[4] = 0x2BADBABEU;
  111. for (i = 0; i < 5; i++)
  112. printk("TCM tcmem[%d] = %08x\n", i, tcmem[i]);
  113. tcm_free(tcmem, 20);
  114. }
  115. }