coresight-cpu-debug.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Coresight CPU Debug Module
  2. ==========================
  3. Author: Leo Yan <leo.yan@linaro.org>
  4. Date: April 5th, 2017
  5. Introduction
  6. ------------
  7. Coresight CPU debug module is defined in ARMv8-a architecture reference manual
  8. (ARM DDI 0487A.k) Chapter 'Part H: External debug', the CPU can integrate
  9. debug module and it is mainly used for two modes: self-hosted debug and
  10. external debug. Usually the external debug mode is well known as the external
  11. debugger connects with SoC from JTAG port; on the other hand the program can
  12. explore debugging method which rely on self-hosted debug mode, this document
  13. is to focus on this part.
  14. The debug module provides sample-based profiling extension, which can be used
  15. to sample CPU program counter, secure state and exception level, etc; usually
  16. every CPU has one dedicated debug module to be connected. Based on self-hosted
  17. debug mechanism, Linux kernel can access these related registers from mmio
  18. region when the kernel panic happens. The callback notifier for kernel panic
  19. will dump related registers for every CPU; finally this is good for assistant
  20. analysis for panic.
  21. Implementation
  22. --------------
  23. - During driver registration, it uses EDDEVID and EDDEVID1 - two device ID
  24. registers to decide if sample-based profiling is implemented or not. On some
  25. platforms this hardware feature is fully or partially implemented; and if
  26. this feature is not supported then registration will fail.
  27. - At the time this documentation was written, the debug driver mainly relies on
  28. information gathered by the kernel panic callback notifier from three
  29. sampling registers: EDPCSR, EDVIDSR and EDCIDSR: from EDPCSR we can get
  30. program counter; EDVIDSR has information for secure state, exception level,
  31. bit width, etc; EDCIDSR is context ID value which contains the sampled value
  32. of CONTEXTIDR_EL1.
  33. - The driver supports a CPU running in either AArch64 or AArch32 mode. The
  34. registers naming convention is a bit different between them, AArch64 uses
  35. 'ED' for register prefix (ARM DDI 0487A.k, chapter H9.1) and AArch32 uses
  36. 'DBG' as prefix (ARM DDI 0487A.k, chapter G5.1). The driver is unified to
  37. use AArch64 naming convention.
  38. - ARMv8-a (ARM DDI 0487A.k) and ARMv7-a (ARM DDI 0406C.b) have different
  39. register bits definition. So the driver consolidates two difference:
  40. If PCSROffset=0b0000, on ARMv8-a the feature of EDPCSR is not implemented;
  41. but ARMv7-a defines "PCSR samples are offset by a value that depends on the
  42. instruction set state". For ARMv7-a, the driver checks furthermore if CPU
  43. runs with ARM or thumb instruction set and calibrate PCSR value, the
  44. detailed description for offset is in ARMv7-a ARM (ARM DDI 0406C.b) chapter
  45. C11.11.34 "DBGPCSR, Program Counter Sampling Register".
  46. If PCSROffset=0b0010, ARMv8-a defines "EDPCSR implemented, and samples have
  47. no offset applied and do not sample the instruction set state in AArch32
  48. state". So on ARMv8 if EDDEVID1.PCSROffset is 0b0010 and the CPU operates
  49. in AArch32 state, EDPCSR is not sampled; when the CPU operates in AArch64
  50. state EDPCSR is sampled and no offset are applied.
  51. Clock and power domain
  52. ----------------------
  53. Before accessing debug registers, we should ensure the clock and power domain
  54. have been enabled properly. In ARMv8-a ARM (ARM DDI 0487A.k) chapter 'H9.1
  55. Debug registers', the debug registers are spread into two domains: the debug
  56. domain and the CPU domain.
  57. +---------------+
  58. | |
  59. | |
  60. +----------+--+ |
  61. dbg_clock -->| |**| |<-- cpu_clock
  62. | Debug |**| CPU |
  63. dbg_power_domain -->| |**| |<-- cpu_power_domain
  64. +----------+--+ |
  65. | |
  66. | |
  67. +---------------+
  68. For debug domain, the user uses DT binding "clocks" and "power-domains" to
  69. specify the corresponding clock source and power supply for the debug logic.
  70. The driver calls the pm_runtime_{put|get} operations as needed to handle the
  71. debug power domain.
  72. For CPU domain, the different SoC designs have different power management
  73. schemes and finally this heavily impacts external debug module. So we can
  74. divide into below cases:
  75. - On systems with a sane power controller which can behave correctly with
  76. respect to CPU power domain, the CPU power domain can be controlled by
  77. register EDPRCR in driver. The driver firstly writes bit EDPRCR.COREPURQ
  78. to power up the CPU, and then writes bit EDPRCR.CORENPDRQ for emulation
  79. of CPU power down. As result, this can ensure the CPU power domain is
  80. powered on properly during the period when access debug related registers;
  81. - Some designs will power down an entire cluster if all CPUs on the cluster
  82. are powered down - including the parts of the debug registers that should
  83. remain powered in the debug power domain. The bits in EDPRCR are not
  84. respected in these cases, so these designs do not support debug over
  85. power down in the way that the CoreSight / Debug designers anticipated.
  86. This means that even checking EDPRSR has the potential to cause a bus hang
  87. if the target register is unpowered.
  88. In this case, accessing to the debug registers while they are not powered
  89. is a recipe for disaster; so we need preventing CPU low power states at boot
  90. time or when user enable module at the run time. Please see chapter
  91. "How to use the module" for detailed usage info for this.
  92. Device Tree Bindings
  93. --------------------
  94. See Documentation/devicetree/bindings/arm/coresight-cpu-debug.txt for details.
  95. How to use the module
  96. ---------------------
  97. If you want to enable debugging functionality at boot time, you can add
  98. "coresight_cpu_debug.enable=1" to the kernel command line parameter.
  99. The driver also can work as module, so can enable the debugging when insmod
  100. module:
  101. # insmod coresight_cpu_debug.ko debug=1
  102. When boot time or insmod module you have not enabled the debugging, the driver
  103. uses the debugfs file system to provide a knob to dynamically enable or disable
  104. debugging:
  105. To enable it, write a '1' into /sys/kernel/debug/coresight_cpu_debug/enable:
  106. # echo 1 > /sys/kernel/debug/coresight_cpu_debug/enable
  107. To disable it, write a '0' into /sys/kernel/debug/coresight_cpu_debug/enable:
  108. # echo 0 > /sys/kernel/debug/coresight_cpu_debug/enable
  109. As explained in chapter "Clock and power domain", if you are working on one
  110. platform which has idle states to power off debug logic and the power
  111. controller cannot work well for the request from EDPRCR, then you should
  112. firstly constraint CPU idle states before enable CPU debugging feature; so can
  113. ensure the accessing to debug logic.
  114. If you want to limit idle states at boot time, you can use "nohlt" or
  115. "cpuidle.off=1" in the kernel command line.
  116. At the runtime you can disable idle states with below methods:
  117. It is possible to disable CPU idle states by way of the PM QoS
  118. subsystem, more specifically by using the "/dev/cpu_dma_latency"
  119. interface (see Documentation/power/pm_qos_interface.txt for more
  120. details). As specified in the PM QoS documentation the requested
  121. parameter will stay in effect until the file descriptor is released.
  122. For example:
  123. # exec 3<> /dev/cpu_dma_latency; echo 0 >&3
  124. ...
  125. Do some work...
  126. ...
  127. # exec 3<>-
  128. The same can also be done from an application program.
  129. Disable specific CPU's specific idle state from cpuidle sysfs (see
  130. Documentation/cpuidle/sysfs.txt):
  131. # echo 1 > /sys/devices/system/cpu/cpu$cpu/cpuidle/state$state/disable
  132. Output format
  133. -------------
  134. Here is an example of the debugging output format:
  135. ARM external debug module:
  136. coresight-cpu-debug 850000.debug: CPU[0]:
  137. coresight-cpu-debug 850000.debug: EDPRSR: 00000001 (Power:On DLK:Unlock)
  138. coresight-cpu-debug 850000.debug: EDPCSR: handle_IPI+0x174/0x1d8
  139. coresight-cpu-debug 850000.debug: EDCIDSR: 00000000
  140. coresight-cpu-debug 850000.debug: EDVIDSR: 90000000 (State:Non-secure Mode:EL1/0 Width:64bits VMID:0)
  141. coresight-cpu-debug 852000.debug: CPU[1]:
  142. coresight-cpu-debug 852000.debug: EDPRSR: 00000001 (Power:On DLK:Unlock)
  143. coresight-cpu-debug 852000.debug: EDPCSR: debug_notifier_call+0x23c/0x358
  144. coresight-cpu-debug 852000.debug: EDCIDSR: 00000000
  145. coresight-cpu-debug 852000.debug: EDVIDSR: 90000000 (State:Non-secure Mode:EL1/0 Width:64bits VMID:0)