tz_log.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/errno.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/msm_ion.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/of.h>
  19. #include <linux/dma-buf.h>
  20. #include <linux/qcom_scm.h>
  21. #include <linux/qtee_shmbridge.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/version.h>
  24. #include "misc/qseecomi.h"
  25. /* QSEE_LOG_BUF_SIZE = 32K */
  26. #define QSEE_LOG_BUF_SIZE 0x8000
  27. /* enlarged qsee log buf size is 128K by default */
  28. #define QSEE_LOG_BUF_SIZE_V2 0x20000
  29. /* TZ Diagnostic Area legacy version number */
  30. #define TZBSP_DIAG_MAJOR_VERSION_LEGACY 2
  31. /* TZ Diagnostic Area version number */
  32. #define TZBSP_FVER_MAJOR_MINOR_MASK 0x3FF /* 10 bits */
  33. #define TZBSP_FVER_MAJOR_SHIFT 22
  34. #define TZBSP_FVER_MINOR_SHIFT 12
  35. #define TZBSP_DIAG_MAJOR_VERSION_V9 9
  36. #define TZBSP_DIAG_MINOR_VERSION_V2 2
  37. #define TZBSP_DIAG_MINOR_VERSION_V21 3
  38. #define TZBSP_DIAG_MINOR_VERSION_V22 4
  39. /* TZ Diag Feature Version Id */
  40. #define QCOM_SCM_FEAT_DIAG_ID 0x06
  41. /*
  42. * Preprocessor Definitions and Constants
  43. */
  44. #define TZBSP_MAX_CPU_COUNT 0x08
  45. /*
  46. * Number of VMID Tables
  47. */
  48. #define TZBSP_DIAG_NUM_OF_VMID 16
  49. /*
  50. * VMID Description length
  51. */
  52. #define TZBSP_DIAG_VMID_DESC_LEN 7
  53. /*
  54. * Number of Interrupts
  55. */
  56. #define TZBSP_DIAG_INT_NUM 32
  57. /*
  58. * Length of descriptive name associated with Interrupt
  59. */
  60. #define TZBSP_MAX_INT_DESC 16
  61. /*
  62. * TZ 3.X version info
  63. */
  64. #define QSEE_VERSION_TZ_3_X 0x800000
  65. /*
  66. * TZ 4.X version info
  67. */
  68. #define QSEE_VERSION_TZ_4_X 0x1000000
  69. #define TZBSP_AES_256_ENCRYPTED_KEY_SIZE 256
  70. #define TZBSP_NONCE_LEN 12
  71. #define TZBSP_TAG_LEN 16
  72. #define ENCRYPTED_TZ_LOG_ID 0
  73. #define ENCRYPTED_QSEE_LOG_ID 1
  74. /*
  75. * Directory for TZ DBG logs
  76. */
  77. #define TZDBG_DIR_NAME "tzdbg"
  78. /*
  79. * VMID Table
  80. */
  81. struct tzdbg_vmid_t {
  82. uint8_t vmid; /* Virtual Machine Identifier */
  83. uint8_t desc[TZBSP_DIAG_VMID_DESC_LEN]; /* ASCII Text */
  84. };
  85. /*
  86. * Boot Info Table
  87. */
  88. struct tzdbg_boot_info_t {
  89. uint32_t wb_entry_cnt; /* Warmboot entry CPU Counter */
  90. uint32_t wb_exit_cnt; /* Warmboot exit CPU Counter */
  91. uint32_t pc_entry_cnt; /* Power Collapse entry CPU Counter */
  92. uint32_t pc_exit_cnt; /* Power Collapse exit CPU counter */
  93. uint32_t warm_jmp_addr; /* Last Warmboot Jump Address */
  94. uint32_t spare; /* Reserved for future use. */
  95. };
  96. /*
  97. * Boot Info Table for 64-bit
  98. */
  99. struct tzdbg_boot_info64_t {
  100. uint32_t wb_entry_cnt; /* Warmboot entry CPU Counter */
  101. uint32_t wb_exit_cnt; /* Warmboot exit CPU Counter */
  102. uint32_t pc_entry_cnt; /* Power Collapse entry CPU Counter */
  103. uint32_t pc_exit_cnt; /* Power Collapse exit CPU counter */
  104. uint32_t psci_entry_cnt;/* PSCI syscall entry CPU Counter */
  105. uint32_t psci_exit_cnt; /* PSCI syscall exit CPU Counter */
  106. uint64_t warm_jmp_addr; /* Last Warmboot Jump Address */
  107. uint32_t warm_jmp_instr; /* Last Warmboot Jump Address Instruction */
  108. };
  109. /*
  110. * Reset Info Table
  111. */
  112. struct tzdbg_reset_info_t {
  113. uint32_t reset_type; /* Reset Reason */
  114. uint32_t reset_cnt; /* Number of resets occurred/CPU */
  115. };
  116. /*
  117. * Interrupt Info Table
  118. */
  119. struct tzdbg_int_t {
  120. /*
  121. * Type of Interrupt/exception
  122. */
  123. uint16_t int_info;
  124. /*
  125. * Availability of the slot
  126. */
  127. uint8_t avail;
  128. /*
  129. * Reserved for future use
  130. */
  131. uint8_t spare;
  132. /*
  133. * Interrupt # for IRQ and FIQ
  134. */
  135. uint32_t int_num;
  136. /*
  137. * ASCII text describing type of interrupt e.g:
  138. * Secure Timer, EBI XPU. This string is always null terminated,
  139. * supporting at most TZBSP_MAX_INT_DESC characters.
  140. * Any additional characters are truncated.
  141. */
  142. uint8_t int_desc[TZBSP_MAX_INT_DESC];
  143. uint64_t int_count[TZBSP_MAX_CPU_COUNT]; /* # of times seen per CPU */
  144. };
  145. /*
  146. * Interrupt Info Table used in tz version >=4.X
  147. */
  148. struct tzdbg_int_t_tz40 {
  149. uint16_t int_info;
  150. uint8_t avail;
  151. uint8_t spare;
  152. uint32_t int_num;
  153. uint8_t int_desc[TZBSP_MAX_INT_DESC];
  154. uint32_t int_count[TZBSP_MAX_CPU_COUNT]; /* uint32_t in TZ ver >= 4.x*/
  155. };
  156. /* warm boot reason for cores */
  157. struct tzbsp_diag_wakeup_info_t {
  158. /* Wake source info : APCS_GICC_HPPIR */
  159. uint32_t HPPIR;
  160. /* Wake source info : APCS_GICC_AHPPIR */
  161. uint32_t AHPPIR;
  162. };
  163. /*
  164. * Log ring buffer position
  165. */
  166. struct tzdbg_log_pos_t {
  167. uint16_t wrap;
  168. uint16_t offset;
  169. };
  170. struct tzdbg_log_pos_v2_t {
  171. uint32_t wrap;
  172. uint32_t offset;
  173. };
  174. /*
  175. * Log ring buffer
  176. */
  177. struct tzdbg_log_t {
  178. struct tzdbg_log_pos_t log_pos;
  179. /* open ended array to the end of the 4K IMEM buffer */
  180. uint8_t log_buf[];
  181. };
  182. struct tzdbg_log_v2_t {
  183. struct tzdbg_log_pos_v2_t log_pos;
  184. /* open ended array to the end of the 4K IMEM buffer */
  185. uint8_t log_buf[];
  186. };
  187. struct tzbsp_encr_info_for_log_chunk_t {
  188. uint32_t size_to_encr;
  189. uint8_t nonce[TZBSP_NONCE_LEN];
  190. uint8_t tag[TZBSP_TAG_LEN];
  191. };
  192. /*
  193. * Only `ENTIRE_LOG` will be used unless the
  194. * "OEM_tz_num_of_diag_log_chunks_to_encr" devcfg field >= 2.
  195. * If this is true, the diag log will be encrypted in two
  196. * separate chunks: a smaller chunk containing only error
  197. * fatal logs and a bigger "rest of the log" chunk. In this
  198. * case, `ERR_FATAL_LOG_CHUNK` and `BIG_LOG_CHUNK` will be
  199. * used instead of `ENTIRE_LOG`.
  200. */
  201. enum tzbsp_encr_info_for_log_chunks_idx_t {
  202. BIG_LOG_CHUNK = 0,
  203. ENTIRE_LOG = 1,
  204. ERR_FATAL_LOG_CHUNK = 1,
  205. MAX_NUM_OF_CHUNKS,
  206. };
  207. struct tzbsp_encr_info_t {
  208. uint32_t num_of_chunks;
  209. struct tzbsp_encr_info_for_log_chunk_t chunks[MAX_NUM_OF_CHUNKS];
  210. uint8_t key[TZBSP_AES_256_ENCRYPTED_KEY_SIZE];
  211. };
  212. /*
  213. * Diagnostic Table
  214. * Note: This is the reference data structure for tz diagnostic table
  215. * supporting TZBSP_MAX_CPU_COUNT, the real diagnostic data is directly
  216. * copied into buffer from i/o memory.
  217. */
  218. struct tzdbg_t {
  219. uint32_t magic_num;
  220. uint32_t version;
  221. /*
  222. * Number of CPU's
  223. */
  224. uint32_t cpu_count;
  225. /*
  226. * Offset of VMID Table
  227. */
  228. uint32_t vmid_info_off;
  229. /*
  230. * Offset of Boot Table
  231. */
  232. uint32_t boot_info_off;
  233. /*
  234. * Offset of Reset info Table
  235. */
  236. uint32_t reset_info_off;
  237. /*
  238. * Offset of Interrupt info Table
  239. */
  240. uint32_t int_info_off;
  241. /*
  242. * Ring Buffer Offset
  243. */
  244. uint32_t ring_off;
  245. /*
  246. * Ring Buffer Length
  247. */
  248. uint32_t ring_len;
  249. /* Offset for Wakeup info */
  250. uint32_t wakeup_info_off;
  251. union {
  252. /* The elements in below structure have to be used for TZ where
  253. * diag version = TZBSP_DIAG_MINOR_VERSION_V2
  254. */
  255. struct {
  256. /*
  257. * VMID to EE Mapping
  258. */
  259. struct tzdbg_vmid_t vmid_info[TZBSP_DIAG_NUM_OF_VMID];
  260. /*
  261. * Boot Info
  262. */
  263. struct tzdbg_boot_info_t boot_info[TZBSP_MAX_CPU_COUNT];
  264. /*
  265. * Reset Info
  266. */
  267. struct tzdbg_reset_info_t reset_info[TZBSP_MAX_CPU_COUNT];
  268. uint32_t num_interrupts;
  269. struct tzdbg_int_t int_info[TZBSP_DIAG_INT_NUM];
  270. /* Wake up info */
  271. struct tzbsp_diag_wakeup_info_t wakeup_info[TZBSP_MAX_CPU_COUNT];
  272. uint8_t key[TZBSP_AES_256_ENCRYPTED_KEY_SIZE];
  273. uint8_t nonce[TZBSP_NONCE_LEN];
  274. uint8_t tag[TZBSP_TAG_LEN];
  275. };
  276. /* The elements in below structure have to be used for TZ where
  277. * diag version = TZBSP_DIAG_MINOR_VERSION_V21
  278. */
  279. struct {
  280. uint32_t encr_info_for_log_off;
  281. /*
  282. * VMID to EE Mapping
  283. */
  284. struct tzdbg_vmid_t vmid_info_v2[TZBSP_DIAG_NUM_OF_VMID];
  285. /*
  286. * Boot Info
  287. */
  288. struct tzdbg_boot_info_t boot_info_v2[TZBSP_MAX_CPU_COUNT];
  289. /*
  290. * Reset Info
  291. */
  292. struct tzdbg_reset_info_t reset_info_v2[TZBSP_MAX_CPU_COUNT];
  293. uint32_t num_interrupts_v2;
  294. struct tzdbg_int_t int_info_v2[TZBSP_DIAG_INT_NUM];
  295. /* Wake up info */
  296. struct tzbsp_diag_wakeup_info_t wakeup_info_v2[TZBSP_MAX_CPU_COUNT];
  297. struct tzbsp_encr_info_t encr_info_for_log;
  298. };
  299. };
  300. /*
  301. * We need at least 2K for the ring buffer
  302. */
  303. struct tzdbg_log_t ring_buffer; /* TZ Ring Buffer */
  304. };
  305. struct hypdbg_log_pos_t {
  306. uint16_t wrap;
  307. uint16_t offset;
  308. };
  309. struct rmdbg_log_hdr_t {
  310. uint32_t write_idx;
  311. uint32_t size;
  312. };
  313. struct rmdbg_log_pos_t {
  314. uint32_t read_idx;
  315. uint32_t size;
  316. };
  317. struct hypdbg_boot_info_t {
  318. uint32_t warm_entry_cnt;
  319. uint32_t warm_exit_cnt;
  320. };
  321. struct hypdbg_t {
  322. /* Magic Number */
  323. uint32_t magic_num;
  324. /* Number of CPU's */
  325. uint32_t cpu_count;
  326. /* Ring Buffer Offset */
  327. uint32_t ring_off;
  328. /* Ring buffer position mgmt */
  329. struct hypdbg_log_pos_t log_pos;
  330. uint32_t log_len;
  331. /* S2 fault numbers */
  332. uint32_t s2_fault_counter;
  333. /* Boot Info */
  334. struct hypdbg_boot_info_t boot_info[TZBSP_MAX_CPU_COUNT];
  335. /* Ring buffer pointer */
  336. uint8_t log_buf_p[];
  337. };
  338. /*
  339. * Enumeration order for VMID's
  340. */
  341. enum tzdbg_stats_type {
  342. TZDBG_BOOT = 0,
  343. TZDBG_RESET,
  344. TZDBG_INTERRUPT,
  345. TZDBG_VMID,
  346. TZDBG_GENERAL,
  347. TZDBG_LOG,
  348. TZDBG_QSEE_LOG,
  349. TZDBG_HYP_GENERAL,
  350. TZDBG_HYP_LOG,
  351. TZDBG_RM_LOG,
  352. TZDBG_STATS_MAX
  353. };
  354. struct tzdbg_stat {
  355. size_t display_len;
  356. size_t display_offset;
  357. char *name;
  358. char *data;
  359. };
  360. struct tzdbg {
  361. void __iomem *virt_iobase;
  362. void __iomem *hyp_virt_iobase;
  363. void __iomem *rmlog_virt_iobase;
  364. struct tzdbg_t *diag_buf;
  365. struct hypdbg_t *hyp_diag_buf;
  366. uint8_t *rm_diag_buf;
  367. char *disp_buf;
  368. int debug_tz[TZDBG_STATS_MAX];
  369. struct tzdbg_stat stat[TZDBG_STATS_MAX];
  370. uint32_t hyp_debug_rw_buf_size;
  371. uint32_t rmlog_rw_buf_size;
  372. bool is_hyplog_enabled;
  373. uint32_t tz_version;
  374. bool is_encrypted_log_enabled;
  375. bool is_enlarged_buf;
  376. bool is_full_encrypted_tz_logs_supported;
  377. bool is_full_encrypted_tz_logs_enabled;
  378. int tz_diag_minor_version;
  379. int tz_diag_major_version;
  380. };
  381. struct tzbsp_encr_log_t {
  382. /* Magic Number */
  383. uint32_t magic_num;
  384. /* version NUMBER */
  385. uint32_t version;
  386. /* encrypted log size */
  387. uint32_t encr_log_buff_size;
  388. /* Wrap value*/
  389. uint16_t wrap_count;
  390. /* AES encryption key wrapped up with oem public key*/
  391. uint8_t key[TZBSP_AES_256_ENCRYPTED_KEY_SIZE];
  392. /* Nonce used for encryption*/
  393. uint8_t nonce[TZBSP_NONCE_LEN];
  394. /* Tag to be used for Validation */
  395. uint8_t tag[TZBSP_TAG_LEN];
  396. /* Encrypted log buffer */
  397. uint8_t log_buf[1];
  398. };
  399. struct encrypted_log_info {
  400. phys_addr_t paddr;
  401. void *vaddr;
  402. size_t size;
  403. uint64_t shmb_handle;
  404. };
  405. static struct tzdbg tzdbg = {
  406. .stat[TZDBG_BOOT].name = "boot",
  407. .stat[TZDBG_RESET].name = "reset",
  408. .stat[TZDBG_INTERRUPT].name = "interrupt",
  409. .stat[TZDBG_VMID].name = "vmid",
  410. .stat[TZDBG_GENERAL].name = "general",
  411. .stat[TZDBG_LOG].name = "log",
  412. .stat[TZDBG_QSEE_LOG].name = "qsee_log",
  413. .stat[TZDBG_HYP_GENERAL].name = "hyp_general",
  414. .stat[TZDBG_HYP_LOG].name = "hyp_log",
  415. .stat[TZDBG_RM_LOG].name = "rm_log",
  416. };
  417. static struct tzdbg_log_t *g_qsee_log;
  418. static struct tzdbg_log_v2_t *g_qsee_log_v2;
  419. static dma_addr_t coh_pmem;
  420. static uint32_t debug_rw_buf_size;
  421. static uint32_t display_buf_size;
  422. static uint32_t qseelog_buf_size;
  423. static phys_addr_t disp_buf_paddr;
  424. static uint64_t qseelog_shmbridge_handle;
  425. static struct encrypted_log_info enc_qseelog_info;
  426. static struct encrypted_log_info enc_tzlog_info;
  427. /*
  428. * Debugfs data structure and functions
  429. */
  430. static int _disp_tz_general_stats(void)
  431. {
  432. int len = 0;
  433. len += scnprintf(tzdbg.disp_buf + len, debug_rw_buf_size - 1,
  434. " Version : 0x%x\n"
  435. " Magic Number : 0x%x\n"
  436. " Number of CPU : %d\n",
  437. tzdbg.diag_buf->version,
  438. tzdbg.diag_buf->magic_num,
  439. tzdbg.diag_buf->cpu_count);
  440. tzdbg.stat[TZDBG_GENERAL].data = tzdbg.disp_buf;
  441. return len;
  442. }
  443. static int _disp_tz_vmid_stats(void)
  444. {
  445. int i, num_vmid;
  446. int len = 0;
  447. struct tzdbg_vmid_t *ptr;
  448. ptr = (struct tzdbg_vmid_t *)((unsigned char *)tzdbg.diag_buf +
  449. tzdbg.diag_buf->vmid_info_off);
  450. num_vmid = ((tzdbg.diag_buf->boot_info_off -
  451. tzdbg.diag_buf->vmid_info_off)/
  452. (sizeof(struct tzdbg_vmid_t)));
  453. for (i = 0; i < num_vmid; i++) {
  454. if (ptr->vmid < 0xFF) {
  455. len += scnprintf(tzdbg.disp_buf + len,
  456. (debug_rw_buf_size - 1) - len,
  457. " 0x%x %s\n",
  458. (uint32_t)ptr->vmid, (uint8_t *)ptr->desc);
  459. }
  460. if (len > (debug_rw_buf_size - 1)) {
  461. pr_warn("%s: Cannot fit all info into the buffer\n",
  462. __func__);
  463. break;
  464. }
  465. ptr++;
  466. }
  467. tzdbg.stat[TZDBG_VMID].data = tzdbg.disp_buf;
  468. return len;
  469. }
  470. static int _disp_tz_boot_stats(void)
  471. {
  472. int i;
  473. int len = 0;
  474. struct tzdbg_boot_info_t *ptr = NULL;
  475. struct tzdbg_boot_info64_t *ptr_64 = NULL;
  476. pr_info("qsee_version = 0x%x\n", tzdbg.tz_version);
  477. if (tzdbg.tz_version >= QSEE_VERSION_TZ_3_X) {
  478. ptr_64 = (struct tzdbg_boot_info64_t *)((unsigned char *)
  479. tzdbg.diag_buf + tzdbg.diag_buf->boot_info_off);
  480. } else {
  481. ptr = (struct tzdbg_boot_info_t *)((unsigned char *)
  482. tzdbg.diag_buf + tzdbg.diag_buf->boot_info_off);
  483. }
  484. for (i = 0; i < tzdbg.diag_buf->cpu_count; i++) {
  485. if (tzdbg.tz_version >= QSEE_VERSION_TZ_3_X) {
  486. len += scnprintf(tzdbg.disp_buf + len,
  487. (debug_rw_buf_size - 1) - len,
  488. " CPU #: %d\n"
  489. " Warmboot jump address : 0x%llx\n"
  490. " Warmboot entry CPU counter : 0x%x\n"
  491. " Warmboot exit CPU counter : 0x%x\n"
  492. " Power Collapse entry CPU counter : 0x%x\n"
  493. " Power Collapse exit CPU counter : 0x%x\n"
  494. " Psci entry CPU counter : 0x%x\n"
  495. " Psci exit CPU counter : 0x%x\n"
  496. " Warmboot Jump Address Instruction : 0x%x\n",
  497. i, (uint64_t)ptr_64->warm_jmp_addr,
  498. ptr_64->wb_entry_cnt,
  499. ptr_64->wb_exit_cnt,
  500. ptr_64->pc_entry_cnt,
  501. ptr_64->pc_exit_cnt,
  502. ptr_64->psci_entry_cnt,
  503. ptr_64->psci_exit_cnt,
  504. ptr_64->warm_jmp_instr);
  505. if (len > (debug_rw_buf_size - 1)) {
  506. pr_warn("%s: Cannot fit all info into the buffer\n",
  507. __func__);
  508. break;
  509. }
  510. ptr_64++;
  511. } else {
  512. len += scnprintf(tzdbg.disp_buf + len,
  513. (debug_rw_buf_size - 1) - len,
  514. " CPU #: %d\n"
  515. " Warmboot jump address : 0x%x\n"
  516. " Warmboot entry CPU counter: 0x%x\n"
  517. " Warmboot exit CPU counter : 0x%x\n"
  518. " Power Collapse entry CPU counter: 0x%x\n"
  519. " Power Collapse exit CPU counter : 0x%x\n",
  520. i, ptr->warm_jmp_addr,
  521. ptr->wb_entry_cnt,
  522. ptr->wb_exit_cnt,
  523. ptr->pc_entry_cnt,
  524. ptr->pc_exit_cnt);
  525. if (len > (debug_rw_buf_size - 1)) {
  526. pr_warn("%s: Cannot fit all info into the buffer\n",
  527. __func__);
  528. break;
  529. }
  530. ptr++;
  531. }
  532. }
  533. tzdbg.stat[TZDBG_BOOT].data = tzdbg.disp_buf;
  534. return len;
  535. }
  536. static int _disp_tz_reset_stats(void)
  537. {
  538. int i;
  539. int len = 0;
  540. struct tzdbg_reset_info_t *ptr;
  541. ptr = (struct tzdbg_reset_info_t *)((unsigned char *)tzdbg.diag_buf +
  542. tzdbg.diag_buf->reset_info_off);
  543. for (i = 0; i < tzdbg.diag_buf->cpu_count; i++) {
  544. len += scnprintf(tzdbg.disp_buf + len,
  545. (debug_rw_buf_size - 1) - len,
  546. " CPU #: %d\n"
  547. " Reset Type (reason) : 0x%x\n"
  548. " Reset counter : 0x%x\n",
  549. i, ptr->reset_type, ptr->reset_cnt);
  550. if (len > (debug_rw_buf_size - 1)) {
  551. pr_warn("%s: Cannot fit all info into the buffer\n",
  552. __func__);
  553. break;
  554. }
  555. ptr++;
  556. }
  557. tzdbg.stat[TZDBG_RESET].data = tzdbg.disp_buf;
  558. return len;
  559. }
  560. static int _disp_tz_interrupt_stats(void)
  561. {
  562. int i, j;
  563. int len = 0;
  564. int *num_int;
  565. void *ptr;
  566. struct tzdbg_int_t *tzdbg_ptr;
  567. struct tzdbg_int_t_tz40 *tzdbg_ptr_tz40;
  568. num_int = (uint32_t *)((unsigned char *)tzdbg.diag_buf +
  569. (tzdbg.diag_buf->int_info_off - sizeof(uint32_t)));
  570. ptr = ((unsigned char *)tzdbg.diag_buf +
  571. tzdbg.diag_buf->int_info_off);
  572. pr_info("qsee_version = 0x%x\n", tzdbg.tz_version);
  573. if (tzdbg.tz_version < QSEE_VERSION_TZ_4_X) {
  574. tzdbg_ptr = ptr;
  575. for (i = 0; i < (*num_int); i++) {
  576. len += scnprintf(tzdbg.disp_buf + len,
  577. (debug_rw_buf_size - 1) - len,
  578. " Interrupt Number : 0x%x\n"
  579. " Type of Interrupt : 0x%x\n"
  580. " Description of interrupt : %s\n",
  581. tzdbg_ptr->int_num,
  582. (uint32_t)tzdbg_ptr->int_info,
  583. (uint8_t *)tzdbg_ptr->int_desc);
  584. for (j = 0; j < tzdbg.diag_buf->cpu_count; j++) {
  585. len += scnprintf(tzdbg.disp_buf + len,
  586. (debug_rw_buf_size - 1) - len,
  587. " int_count on CPU # %d : %u\n",
  588. (uint32_t)j,
  589. (uint32_t)tzdbg_ptr->int_count[j]);
  590. }
  591. len += scnprintf(tzdbg.disp_buf + len,
  592. debug_rw_buf_size - 1, "\n");
  593. if (len > (debug_rw_buf_size - 1)) {
  594. pr_warn("%s: Cannot fit all info into buf\n",
  595. __func__);
  596. break;
  597. }
  598. tzdbg_ptr++;
  599. }
  600. } else {
  601. tzdbg_ptr_tz40 = ptr;
  602. for (i = 0; i < (*num_int); i++) {
  603. len += scnprintf(tzdbg.disp_buf + len,
  604. (debug_rw_buf_size - 1) - len,
  605. " Interrupt Number : 0x%x\n"
  606. " Type of Interrupt : 0x%x\n"
  607. " Description of interrupt : %s\n",
  608. tzdbg_ptr_tz40->int_num,
  609. (uint32_t)tzdbg_ptr_tz40->int_info,
  610. (uint8_t *)tzdbg_ptr_tz40->int_desc);
  611. for (j = 0; j < tzdbg.diag_buf->cpu_count; j++) {
  612. len += scnprintf(tzdbg.disp_buf + len,
  613. (debug_rw_buf_size - 1) - len,
  614. " int_count on CPU # %d : %u\n",
  615. (uint32_t)j,
  616. (uint32_t)tzdbg_ptr_tz40->int_count[j]);
  617. }
  618. len += scnprintf(tzdbg.disp_buf + len,
  619. debug_rw_buf_size - 1, "\n");
  620. if (len > (debug_rw_buf_size - 1)) {
  621. pr_warn("%s: Cannot fit all info into buf\n",
  622. __func__);
  623. break;
  624. }
  625. tzdbg_ptr_tz40++;
  626. }
  627. }
  628. tzdbg.stat[TZDBG_INTERRUPT].data = tzdbg.disp_buf;
  629. return len;
  630. }
  631. static int _disp_tz_log_stats_legacy(void)
  632. {
  633. int len = 0;
  634. unsigned char *ptr;
  635. ptr = (unsigned char *)tzdbg.diag_buf +
  636. tzdbg.diag_buf->ring_off;
  637. len += scnprintf(tzdbg.disp_buf, (debug_rw_buf_size - 1) - len,
  638. "%s\n", ptr);
  639. tzdbg.stat[TZDBG_LOG].data = tzdbg.disp_buf;
  640. return len;
  641. }
  642. static int _disp_log_stats(struct tzdbg_log_t *log,
  643. struct tzdbg_log_pos_t *log_start, uint32_t log_len,
  644. size_t count, uint32_t buf_idx)
  645. {
  646. uint32_t wrap_start;
  647. uint32_t wrap_end;
  648. uint32_t wrap_cnt;
  649. int max_len;
  650. int len = 0;
  651. int i = 0;
  652. wrap_start = log_start->wrap;
  653. wrap_end = log->log_pos.wrap;
  654. /* Calculate difference in # of buffer wrap-arounds */
  655. if (wrap_end >= wrap_start)
  656. wrap_cnt = wrap_end - wrap_start;
  657. else {
  658. /* wrap counter has wrapped around, invalidate start position */
  659. wrap_cnt = 2;
  660. }
  661. if (wrap_cnt > 1) {
  662. /* end position has wrapped around more than once, */
  663. /* current start no longer valid */
  664. log_start->wrap = log->log_pos.wrap - 1;
  665. log_start->offset = (log->log_pos.offset + 1) % log_len;
  666. } else if ((wrap_cnt == 1) &&
  667. (log->log_pos.offset > log_start->offset)) {
  668. /* end position has overwritten start */
  669. log_start->offset = (log->log_pos.offset + 1) % log_len;
  670. }
  671. pr_debug("diag_buf wrap = %u, offset = %u\n",
  672. log->log_pos.wrap, log->log_pos.offset);
  673. while (log_start->offset == log->log_pos.offset) {
  674. /*
  675. * No data in ring buffer,
  676. * so we'll hang around until something happens
  677. */
  678. unsigned long t = msleep_interruptible(50);
  679. if (t != 0) {
  680. /* Some event woke us up, so let's quit */
  681. return 0;
  682. }
  683. if (buf_idx == TZDBG_LOG)
  684. memcpy_fromio((void *)tzdbg.diag_buf, tzdbg.virt_iobase,
  685. debug_rw_buf_size);
  686. }
  687. max_len = (count > debug_rw_buf_size) ? debug_rw_buf_size : count;
  688. pr_debug("diag_buf wrap = %u, offset = %u\n",
  689. log->log_pos.wrap, log->log_pos.offset);
  690. /*
  691. * Read from ring buff while there is data and space in return buff
  692. */
  693. while ((log_start->offset != log->log_pos.offset) && (len < max_len)) {
  694. tzdbg.disp_buf[i++] = log->log_buf[log_start->offset];
  695. log_start->offset = (log_start->offset + 1) % log_len;
  696. if (log_start->offset == 0)
  697. ++log_start->wrap;
  698. ++len;
  699. }
  700. /*
  701. * return buffer to caller
  702. */
  703. tzdbg.stat[buf_idx].data = tzdbg.disp_buf;
  704. return len;
  705. }
  706. static int _disp_log_stats_v2(struct tzdbg_log_v2_t *log,
  707. struct tzdbg_log_pos_v2_t *log_start, uint32_t log_len,
  708. size_t count, uint32_t buf_idx)
  709. {
  710. uint32_t wrap_start;
  711. uint32_t wrap_end;
  712. uint32_t wrap_cnt;
  713. int max_len;
  714. int len = 0;
  715. int i = 0;
  716. wrap_start = log_start->wrap;
  717. wrap_end = log->log_pos.wrap;
  718. /* Calculate difference in # of buffer wrap-arounds */
  719. if (wrap_end >= wrap_start)
  720. wrap_cnt = wrap_end - wrap_start;
  721. else {
  722. /* wrap counter has wrapped around, invalidate start position */
  723. wrap_cnt = 2;
  724. }
  725. if (wrap_cnt > 1) {
  726. /* end position has wrapped around more than once, */
  727. /* current start no longer valid */
  728. log_start->wrap = log->log_pos.wrap - 1;
  729. log_start->offset = (log->log_pos.offset + 1) % log_len;
  730. } else if ((wrap_cnt == 1) &&
  731. (log->log_pos.offset > log_start->offset)) {
  732. /* end position has overwritten start */
  733. log_start->offset = (log->log_pos.offset + 1) % log_len;
  734. }
  735. pr_debug("diag_buf wrap = %u, offset = %u\n",
  736. log->log_pos.wrap, log->log_pos.offset);
  737. while (log_start->offset == log->log_pos.offset) {
  738. /*
  739. * No data in ring buffer,
  740. * so we'll hang around until something happens
  741. */
  742. unsigned long t = msleep_interruptible(50);
  743. if (t != 0) {
  744. /* Some event woke us up, so let's quit */
  745. return 0;
  746. }
  747. if (buf_idx == TZDBG_LOG)
  748. memcpy_fromio((void *)tzdbg.diag_buf, tzdbg.virt_iobase,
  749. debug_rw_buf_size);
  750. }
  751. max_len = (count > debug_rw_buf_size) ? debug_rw_buf_size : count;
  752. pr_debug("diag_buf wrap = %u, offset = %u\n",
  753. log->log_pos.wrap, log->log_pos.offset);
  754. /*
  755. * Read from ring buff while there is data and space in return buff
  756. */
  757. while ((log_start->offset != log->log_pos.offset) && (len < max_len)) {
  758. tzdbg.disp_buf[i++] = log->log_buf[log_start->offset];
  759. log_start->offset = (log_start->offset + 1) % log_len;
  760. if (log_start->offset == 0)
  761. ++log_start->wrap;
  762. ++len;
  763. }
  764. /*
  765. * return buffer to caller
  766. */
  767. tzdbg.stat[buf_idx].data = tzdbg.disp_buf;
  768. return len;
  769. }
  770. static int __disp_hyp_log_stats(uint8_t *log,
  771. struct hypdbg_log_pos_t *log_start, uint32_t log_len,
  772. size_t count, uint32_t buf_idx)
  773. {
  774. struct hypdbg_t *hyp = tzdbg.hyp_diag_buf;
  775. unsigned long t = 0;
  776. uint32_t wrap_start;
  777. uint32_t wrap_end;
  778. uint32_t wrap_cnt;
  779. int max_len;
  780. int len = 0;
  781. int i = 0;
  782. wrap_start = log_start->wrap;
  783. wrap_end = hyp->log_pos.wrap;
  784. /* Calculate difference in # of buffer wrap-arounds */
  785. if (wrap_end >= wrap_start)
  786. wrap_cnt = wrap_end - wrap_start;
  787. else {
  788. /* wrap counter has wrapped around, invalidate start position */
  789. wrap_cnt = 2;
  790. }
  791. if (wrap_cnt > 1) {
  792. /* end position has wrapped around more than once, */
  793. /* current start no longer valid */
  794. log_start->wrap = hyp->log_pos.wrap - 1;
  795. log_start->offset = (hyp->log_pos.offset + 1) % log_len;
  796. } else if ((wrap_cnt == 1) &&
  797. (hyp->log_pos.offset > log_start->offset)) {
  798. /* end position has overwritten start */
  799. log_start->offset = (hyp->log_pos.offset + 1) % log_len;
  800. }
  801. while (log_start->offset == hyp->log_pos.offset) {
  802. /*
  803. * No data in ring buffer,
  804. * so we'll hang around until something happens
  805. */
  806. t = msleep_interruptible(50);
  807. if (t != 0) {
  808. /* Some event woke us up, so let's quit */
  809. return 0;
  810. }
  811. /* TZDBG_HYP_LOG */
  812. memcpy_fromio((void *)tzdbg.hyp_diag_buf, tzdbg.hyp_virt_iobase,
  813. tzdbg.hyp_debug_rw_buf_size);
  814. }
  815. max_len = (count > tzdbg.hyp_debug_rw_buf_size) ?
  816. tzdbg.hyp_debug_rw_buf_size : count;
  817. /*
  818. * Read from ring buff while there is data and space in return buff
  819. */
  820. while ((log_start->offset != hyp->log_pos.offset) && (len < max_len)) {
  821. tzdbg.disp_buf[i++] = log[log_start->offset];
  822. log_start->offset = (log_start->offset + 1) % log_len;
  823. if (log_start->offset == 0)
  824. ++log_start->wrap;
  825. ++len;
  826. }
  827. /*
  828. * return buffer to caller
  829. */
  830. tzdbg.stat[buf_idx].data = tzdbg.disp_buf;
  831. return len;
  832. }
  833. static int __disp_rm_log_stats(uint8_t *log_ptr, uint32_t max_len)
  834. {
  835. uint32_t i = 0;
  836. /*
  837. * Transfer data from rm dialog buff to display buffer in user space
  838. */
  839. while ((i < max_len) && (i < display_buf_size)) {
  840. tzdbg.disp_buf[i] = log_ptr[i];
  841. i++;
  842. }
  843. if (i != max_len)
  844. pr_err("Dropping RM log message, max_len:%d display_buf_size:%d\n",
  845. i, display_buf_size);
  846. tzdbg.stat[TZDBG_RM_LOG].data = tzdbg.disp_buf;
  847. return i;
  848. }
  849. static int print_text(char *intro_message,
  850. unsigned char *text_addr,
  851. unsigned int size,
  852. char *buf, uint32_t buf_len)
  853. {
  854. unsigned int i;
  855. int len = 0;
  856. pr_debug("begin address %p, size %d\n", text_addr, size);
  857. len += scnprintf(buf + len, buf_len - len, "%s\n", intro_message);
  858. for (i = 0; i < size; i++) {
  859. if (buf_len <= len + 6) {
  860. pr_err("buffer not enough, buf_len %d, len %d\n",
  861. buf_len, len);
  862. return buf_len;
  863. }
  864. len += scnprintf(buf + len, buf_len - len, "%02hhx ",
  865. text_addr[i]);
  866. if ((i & 0x1f) == 0x1f)
  867. len += scnprintf(buf + len, buf_len - len, "%c", '\n');
  868. }
  869. len += scnprintf(buf + len, buf_len - len, "%c", '\n');
  870. return len;
  871. }
  872. static int _disp_encrpted_log_stats(struct encrypted_log_info *enc_log_info,
  873. enum tzdbg_stats_type type, uint32_t log_id)
  874. {
  875. int ret = 0, len = 0;
  876. struct tzbsp_encr_log_t *encr_log_head;
  877. uint32_t size = 0;
  878. if ((!tzdbg.is_full_encrypted_tz_logs_supported) &&
  879. (tzdbg.is_full_encrypted_tz_logs_enabled))
  880. pr_info("TZ not supporting full encrypted log functionality\n");
  881. ret = qcom_scm_request_encrypted_log(enc_log_info->paddr,
  882. enc_log_info->size, log_id, tzdbg.is_full_encrypted_tz_logs_supported,
  883. tzdbg.is_full_encrypted_tz_logs_enabled);
  884. if (ret)
  885. return 0;
  886. encr_log_head = (struct tzbsp_encr_log_t *)(enc_log_info->vaddr);
  887. pr_debug("display_buf_size = %d, encr_log_buff_size = %d\n",
  888. display_buf_size, encr_log_head->encr_log_buff_size);
  889. size = encr_log_head->encr_log_buff_size;
  890. len += scnprintf(tzdbg.disp_buf + len,
  891. (display_buf_size - 1) - len,
  892. "\n-------- New Encrypted %s --------\n",
  893. ((log_id == ENCRYPTED_QSEE_LOG_ID) ?
  894. "QSEE Log" : "TZ Dialog"));
  895. len += scnprintf(tzdbg.disp_buf + len,
  896. (display_buf_size - 1) - len,
  897. "\nMagic_Num :\n0x%x\n"
  898. "\nVerion :\n%d\n"
  899. "\nEncr_Log_Buff_Size :\n%d\n"
  900. "\nWrap_Count :\n%d\n",
  901. encr_log_head->magic_num,
  902. encr_log_head->version,
  903. encr_log_head->encr_log_buff_size,
  904. encr_log_head->wrap_count);
  905. len += print_text("\nKey : ", encr_log_head->key,
  906. TZBSP_AES_256_ENCRYPTED_KEY_SIZE,
  907. tzdbg.disp_buf + len, display_buf_size);
  908. len += print_text("\nNonce : ", encr_log_head->nonce,
  909. TZBSP_NONCE_LEN,
  910. tzdbg.disp_buf + len, display_buf_size - len);
  911. len += print_text("\nTag : ", encr_log_head->tag,
  912. TZBSP_TAG_LEN,
  913. tzdbg.disp_buf + len, display_buf_size - len);
  914. if (len > display_buf_size - size)
  915. pr_warn("Cannot fit all info into the buffer\n");
  916. pr_debug("encrypted log size %d, disply buffer size %d, used len %d\n",
  917. size, display_buf_size, len);
  918. len += print_text("\nLog : ", encr_log_head->log_buf, size,
  919. tzdbg.disp_buf + len, display_buf_size - len);
  920. memset(enc_log_info->vaddr, 0, enc_log_info->size);
  921. tzdbg.stat[type].data = tzdbg.disp_buf;
  922. return len;
  923. }
  924. static int _disp_tz_log_stats(size_t count)
  925. {
  926. static struct tzdbg_log_pos_v2_t log_start_v2 = {0};
  927. static struct tzdbg_log_pos_t log_start = {0};
  928. struct tzdbg_log_v2_t *log_v2_ptr;
  929. struct tzdbg_log_t *log_ptr;
  930. log_ptr = (struct tzdbg_log_t *)((unsigned char *)tzdbg.diag_buf +
  931. tzdbg.diag_buf->ring_off -
  932. offsetof(struct tzdbg_log_t, log_buf));
  933. log_v2_ptr = (struct tzdbg_log_v2_t *)((unsigned char *)tzdbg.diag_buf +
  934. tzdbg.diag_buf->ring_off -
  935. offsetof(struct tzdbg_log_v2_t, log_buf));
  936. if (!tzdbg.is_enlarged_buf)
  937. return _disp_log_stats(log_ptr, &log_start,
  938. tzdbg.diag_buf->ring_len, count, TZDBG_LOG);
  939. return _disp_log_stats_v2(log_v2_ptr, &log_start_v2,
  940. tzdbg.diag_buf->ring_len, count, TZDBG_LOG);
  941. }
  942. static int _disp_hyp_log_stats(size_t count)
  943. {
  944. static struct hypdbg_log_pos_t log_start = {0};
  945. uint8_t *log_ptr;
  946. uint32_t log_len;
  947. log_ptr = (uint8_t *)((unsigned char *)tzdbg.hyp_diag_buf +
  948. tzdbg.hyp_diag_buf->ring_off);
  949. log_len = tzdbg.hyp_debug_rw_buf_size - tzdbg.hyp_diag_buf->ring_off;
  950. return __disp_hyp_log_stats(log_ptr, &log_start,
  951. log_len, count, TZDBG_HYP_LOG);
  952. }
  953. static int _disp_rm_log_stats(size_t count)
  954. {
  955. static struct rmdbg_log_pos_t log_start = { 0 };
  956. struct rmdbg_log_hdr_t *p_log_hdr = NULL;
  957. uint8_t *log_ptr = NULL;
  958. uint32_t log_len = 0;
  959. static bool wrap_around = { false };
  960. /* Return 0 to close the display file,if there is nothing else to do */
  961. if ((log_start.size == 0x0) && wrap_around) {
  962. wrap_around = false;
  963. return 0;
  964. }
  965. /* Copy RM log data to tzdbg diag buffer for the first time */
  966. /* Initialize the tracking data structure */
  967. if (tzdbg.rmlog_rw_buf_size != 0) {
  968. if (!wrap_around) {
  969. memcpy_fromio((void *)tzdbg.rm_diag_buf,
  970. tzdbg.rmlog_virt_iobase,
  971. tzdbg.rmlog_rw_buf_size);
  972. /* get RM header info first */
  973. p_log_hdr = (struct rmdbg_log_hdr_t *)tzdbg.rm_diag_buf;
  974. /* Update RM log buffer index tracker and its size */
  975. log_start.read_idx = 0x0;
  976. log_start.size = p_log_hdr->size;
  977. }
  978. /* Update RM log buffer starting ptr */
  979. log_ptr =
  980. (uint8_t *) ((unsigned char *)tzdbg.rm_diag_buf +
  981. sizeof(struct rmdbg_log_hdr_t));
  982. } else {
  983. /* Return 0 to close the display file,if there is nothing else to do */
  984. pr_err("There is no RM log to read, size is %d!\n",
  985. tzdbg.rmlog_rw_buf_size);
  986. return 0;
  987. }
  988. log_len = log_start.size;
  989. log_ptr += log_start.read_idx;
  990. /* Check if we exceed the max length provided by user space */
  991. log_len = (count > log_len) ? log_len : count;
  992. /* Update tracking data structure */
  993. log_start.size -= log_len;
  994. log_start.read_idx += log_len;
  995. if (log_start.size)
  996. wrap_around = true;
  997. return __disp_rm_log_stats(log_ptr, log_len);
  998. }
  999. static int _disp_qsee_log_stats(size_t count)
  1000. {
  1001. static struct tzdbg_log_pos_t log_start = {0};
  1002. static struct tzdbg_log_pos_v2_t log_start_v2 = {0};
  1003. if (!tzdbg.is_enlarged_buf)
  1004. return _disp_log_stats(g_qsee_log, &log_start,
  1005. QSEE_LOG_BUF_SIZE - sizeof(struct tzdbg_log_pos_t),
  1006. count, TZDBG_QSEE_LOG);
  1007. return _disp_log_stats_v2(g_qsee_log_v2, &log_start_v2,
  1008. QSEE_LOG_BUF_SIZE_V2 - sizeof(struct tzdbg_log_pos_v2_t),
  1009. count, TZDBG_QSEE_LOG);
  1010. }
  1011. static int _disp_hyp_general_stats(size_t count)
  1012. {
  1013. int len = 0;
  1014. int i;
  1015. struct hypdbg_boot_info_t *ptr = NULL;
  1016. len += scnprintf((unsigned char *)tzdbg.disp_buf + len,
  1017. tzdbg.hyp_debug_rw_buf_size - 1,
  1018. " Magic Number : 0x%x\n"
  1019. " CPU Count : 0x%x\n"
  1020. " S2 Fault Counter: 0x%x\n",
  1021. tzdbg.hyp_diag_buf->magic_num,
  1022. tzdbg.hyp_diag_buf->cpu_count,
  1023. tzdbg.hyp_diag_buf->s2_fault_counter);
  1024. ptr = tzdbg.hyp_diag_buf->boot_info;
  1025. for (i = 0; i < tzdbg.hyp_diag_buf->cpu_count; i++) {
  1026. len += scnprintf((unsigned char *)tzdbg.disp_buf + len,
  1027. (tzdbg.hyp_debug_rw_buf_size - 1) - len,
  1028. " CPU #: %d\n"
  1029. " Warmboot entry CPU counter: 0x%x\n"
  1030. " Warmboot exit CPU counter : 0x%x\n",
  1031. i, ptr->warm_entry_cnt, ptr->warm_exit_cnt);
  1032. if (len > (tzdbg.hyp_debug_rw_buf_size - 1)) {
  1033. pr_warn("%s: Cannot fit all info into the buffer\n",
  1034. __func__);
  1035. break;
  1036. }
  1037. ptr++;
  1038. }
  1039. tzdbg.stat[TZDBG_HYP_GENERAL].data = (char *)tzdbg.disp_buf;
  1040. return len;
  1041. }
  1042. static ssize_t tzdbg_fs_read_unencrypted(int tz_id, char __user *buf,
  1043. size_t count, loff_t *offp)
  1044. {
  1045. int len = 0;
  1046. if (tz_id == TZDBG_BOOT || tz_id == TZDBG_RESET ||
  1047. tz_id == TZDBG_INTERRUPT || tz_id == TZDBG_GENERAL ||
  1048. tz_id == TZDBG_VMID || tz_id == TZDBG_LOG)
  1049. memcpy_fromio((void *)tzdbg.diag_buf, tzdbg.virt_iobase,
  1050. debug_rw_buf_size);
  1051. if (tz_id == TZDBG_HYP_GENERAL || tz_id == TZDBG_HYP_LOG)
  1052. memcpy_fromio((void *)tzdbg.hyp_diag_buf,
  1053. tzdbg.hyp_virt_iobase,
  1054. tzdbg.hyp_debug_rw_buf_size);
  1055. switch (tz_id) {
  1056. case TZDBG_BOOT:
  1057. len = _disp_tz_boot_stats();
  1058. break;
  1059. case TZDBG_RESET:
  1060. len = _disp_tz_reset_stats();
  1061. break;
  1062. case TZDBG_INTERRUPT:
  1063. len = _disp_tz_interrupt_stats();
  1064. break;
  1065. case TZDBG_GENERAL:
  1066. len = _disp_tz_general_stats();
  1067. break;
  1068. case TZDBG_VMID:
  1069. len = _disp_tz_vmid_stats();
  1070. break;
  1071. case TZDBG_LOG:
  1072. if (TZBSP_DIAG_MAJOR_VERSION_LEGACY <
  1073. (tzdbg.diag_buf->version >> 16)) {
  1074. len = _disp_tz_log_stats(count);
  1075. *offp = 0;
  1076. } else {
  1077. len = _disp_tz_log_stats_legacy();
  1078. }
  1079. break;
  1080. case TZDBG_QSEE_LOG:
  1081. len = _disp_qsee_log_stats(count);
  1082. *offp = 0;
  1083. break;
  1084. case TZDBG_HYP_GENERAL:
  1085. len = _disp_hyp_general_stats(count);
  1086. break;
  1087. case TZDBG_HYP_LOG:
  1088. len = _disp_hyp_log_stats(count);
  1089. *offp = 0;
  1090. break;
  1091. case TZDBG_RM_LOG:
  1092. len = _disp_rm_log_stats(count);
  1093. *offp = 0;
  1094. break;
  1095. default:
  1096. break;
  1097. }
  1098. if (len > count)
  1099. len = count;
  1100. return simple_read_from_buffer(buf, len, offp,
  1101. tzdbg.stat[tz_id].data, len);
  1102. }
  1103. static ssize_t tzdbg_fs_read_encrypted(int tz_id, char __user *buf,
  1104. size_t count, loff_t *offp)
  1105. {
  1106. int len = 0, ret = 0;
  1107. struct tzdbg_stat *stat = &(tzdbg.stat[tz_id]);
  1108. pr_debug("%s: tz_id = %d\n", __func__, tz_id);
  1109. if (tz_id >= TZDBG_STATS_MAX) {
  1110. pr_err("invalid encrypted log id %d\n", tz_id);
  1111. return ret;
  1112. }
  1113. if (!stat->display_len) {
  1114. if (tz_id == TZDBG_QSEE_LOG)
  1115. stat->display_len = _disp_encrpted_log_stats(
  1116. &enc_qseelog_info,
  1117. tz_id, ENCRYPTED_QSEE_LOG_ID);
  1118. else
  1119. stat->display_len = _disp_encrpted_log_stats(
  1120. &enc_tzlog_info,
  1121. tz_id, ENCRYPTED_TZ_LOG_ID);
  1122. stat->display_offset = 0;
  1123. }
  1124. len = stat->display_len;
  1125. if (len > count)
  1126. len = count;
  1127. *offp = 0;
  1128. ret = simple_read_from_buffer(buf, len, offp,
  1129. tzdbg.stat[tz_id].data + stat->display_offset,
  1130. count);
  1131. stat->display_offset += ret;
  1132. stat->display_len -= ret;
  1133. pr_debug("ret = %d, offset = %d\n", ret, (int)(*offp));
  1134. pr_debug("display_len = %d, offset = %d\n",
  1135. stat->display_len, stat->display_offset);
  1136. return ret;
  1137. }
  1138. static ssize_t tzdbg_fs_read(struct file *file, char __user *buf,
  1139. size_t count, loff_t *offp)
  1140. {
  1141. struct seq_file *seq = file->private_data;
  1142. int tz_id = TZDBG_STATS_MAX;
  1143. if (seq)
  1144. tz_id = *(int *)(seq->private);
  1145. else {
  1146. pr_err("%s: Seq data null unable to proceed\n", __func__);
  1147. return 0;
  1148. }
  1149. if (!tzdbg.is_encrypted_log_enabled ||
  1150. (tz_id == TZDBG_HYP_GENERAL || tz_id == TZDBG_HYP_LOG)
  1151. || tz_id == TZDBG_RM_LOG)
  1152. return tzdbg_fs_read_unencrypted(tz_id, buf, count, offp);
  1153. else
  1154. return tzdbg_fs_read_encrypted(tz_id, buf, count, offp);
  1155. }
  1156. static int tzdbg_procfs_open(struct inode *inode, struct file *file)
  1157. {
  1158. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(6,0,0))
  1159. return single_open(file, NULL, PDE_DATA(inode));
  1160. #else
  1161. return single_open(file, NULL, pde_data(inode));
  1162. #endif
  1163. }
  1164. static int tzdbg_procfs_release(struct inode *inode, struct file *file)
  1165. {
  1166. return single_release(inode, file);
  1167. }
  1168. struct proc_ops tzdbg_fops = {
  1169. .proc_flags = PROC_ENTRY_PERMANENT,
  1170. .proc_read = tzdbg_fs_read,
  1171. .proc_open = tzdbg_procfs_open,
  1172. .proc_release = tzdbg_procfs_release,
  1173. };
  1174. /*
  1175. * Allocates log buffer from ION, registers the buffer at TZ
  1176. */
  1177. static int tzdbg_register_qsee_log_buf(struct platform_device *pdev)
  1178. {
  1179. int ret = 0;
  1180. void *buf = NULL;
  1181. uint32_t ns_vmids[] = {VMID_HLOS};
  1182. uint32_t ns_vm_perms[] = {PERM_READ | PERM_WRITE};
  1183. uint32_t ns_vm_nums = 1;
  1184. if (tzdbg.is_enlarged_buf) {
  1185. if (of_property_read_u32((&pdev->dev)->of_node,
  1186. "qseelog-buf-size-v2", &qseelog_buf_size)) {
  1187. pr_debug("Enlarged qseelog buf size isn't defined\n");
  1188. qseelog_buf_size = QSEE_LOG_BUF_SIZE_V2;
  1189. }
  1190. } else {
  1191. qseelog_buf_size = QSEE_LOG_BUF_SIZE;
  1192. }
  1193. pr_debug("qseelog buf size is 0x%x\n", qseelog_buf_size);
  1194. buf = dma_alloc_coherent(&pdev->dev,
  1195. qseelog_buf_size, &coh_pmem, GFP_KERNEL);
  1196. if (buf == NULL)
  1197. return -ENOMEM;
  1198. if (!tzdbg.is_encrypted_log_enabled) {
  1199. ret = qtee_shmbridge_register(coh_pmem,
  1200. qseelog_buf_size, ns_vmids, ns_vm_perms, ns_vm_nums,
  1201. PERM_READ | PERM_WRITE,
  1202. &qseelog_shmbridge_handle);
  1203. if (ret) {
  1204. pr_err("failed to create bridge for qsee_log buf\n");
  1205. goto exit_free_mem;
  1206. }
  1207. }
  1208. g_qsee_log = (struct tzdbg_log_t *)buf;
  1209. g_qsee_log->log_pos.wrap = g_qsee_log->log_pos.offset = 0;
  1210. g_qsee_log_v2 = (struct tzdbg_log_v2_t *)buf;
  1211. g_qsee_log_v2->log_pos.wrap = g_qsee_log_v2->log_pos.offset = 0;
  1212. ret = qcom_scm_register_qsee_log_buf(coh_pmem, qseelog_buf_size);
  1213. if (ret != QSEOS_RESULT_SUCCESS) {
  1214. pr_err(
  1215. "%s: scm_call to register log buf failed, resp result =%lld\n",
  1216. __func__, ret);
  1217. goto exit_dereg_bridge;
  1218. }
  1219. return ret;
  1220. exit_dereg_bridge:
  1221. if (!tzdbg.is_encrypted_log_enabled)
  1222. qtee_shmbridge_deregister(qseelog_shmbridge_handle);
  1223. exit_free_mem:
  1224. dma_free_coherent(&pdev->dev, qseelog_buf_size,
  1225. (void *)g_qsee_log, coh_pmem);
  1226. return ret;
  1227. }
  1228. static void tzdbg_free_qsee_log_buf(struct platform_device *pdev)
  1229. {
  1230. if (!tzdbg.is_encrypted_log_enabled)
  1231. qtee_shmbridge_deregister(qseelog_shmbridge_handle);
  1232. dma_free_coherent(&pdev->dev, qseelog_buf_size,
  1233. (void *)g_qsee_log, coh_pmem);
  1234. }
  1235. static int tzdbg_allocate_encrypted_log_buf(struct platform_device *pdev)
  1236. {
  1237. int ret = 0;
  1238. uint32_t ns_vmids[] = {VMID_HLOS};
  1239. uint32_t ns_vm_perms[] = {PERM_READ | PERM_WRITE};
  1240. uint32_t ns_vm_nums = 1;
  1241. if (!tzdbg.is_encrypted_log_enabled)
  1242. return 0;
  1243. /* max encrypted qsee log buf zize (include header, and page align) */
  1244. enc_qseelog_info.size = qseelog_buf_size + PAGE_SIZE;
  1245. enc_qseelog_info.vaddr = dma_alloc_coherent(&pdev->dev,
  1246. enc_qseelog_info.size,
  1247. &enc_qseelog_info.paddr, GFP_KERNEL);
  1248. if (enc_qseelog_info.vaddr == NULL)
  1249. return -ENOMEM;
  1250. ret = qtee_shmbridge_register(enc_qseelog_info.paddr,
  1251. enc_qseelog_info.size, ns_vmids,
  1252. ns_vm_perms, ns_vm_nums,
  1253. PERM_READ | PERM_WRITE, &enc_qseelog_info.shmb_handle);
  1254. if (ret) {
  1255. pr_err("failed to create encr_qsee_log bridge, ret %d\n", ret);
  1256. goto exit_free_qseelog;
  1257. }
  1258. pr_debug("Alloc memory for encr_qsee_log, size = %zu\n",
  1259. enc_qseelog_info.size);
  1260. enc_tzlog_info.size = debug_rw_buf_size;
  1261. enc_tzlog_info.vaddr = dma_alloc_coherent(&pdev->dev,
  1262. enc_tzlog_info.size,
  1263. &enc_tzlog_info.paddr, GFP_KERNEL);
  1264. if (enc_tzlog_info.vaddr == NULL)
  1265. goto exit_unreg_qseelog;
  1266. ret = qtee_shmbridge_register(enc_tzlog_info.paddr,
  1267. enc_tzlog_info.size, ns_vmids, ns_vm_perms, ns_vm_nums,
  1268. PERM_READ | PERM_WRITE, &enc_tzlog_info.shmb_handle);
  1269. if (ret) {
  1270. pr_err("failed to create encr_tz_log bridge, ret = %d\n", ret);
  1271. goto exit_free_tzlog;
  1272. }
  1273. pr_debug("Alloc memory for encr_tz_log, size %zu\n",
  1274. enc_qseelog_info.size);
  1275. return 0;
  1276. exit_free_tzlog:
  1277. dma_free_coherent(&pdev->dev, enc_tzlog_info.size,
  1278. enc_tzlog_info.vaddr, enc_tzlog_info.paddr);
  1279. exit_unreg_qseelog:
  1280. qtee_shmbridge_deregister(enc_qseelog_info.shmb_handle);
  1281. exit_free_qseelog:
  1282. dma_free_coherent(&pdev->dev, enc_qseelog_info.size,
  1283. enc_qseelog_info.vaddr, enc_qseelog_info.paddr);
  1284. return -ENOMEM;
  1285. }
  1286. static void tzdbg_free_encrypted_log_buf(struct platform_device *pdev)
  1287. {
  1288. qtee_shmbridge_deregister(enc_tzlog_info.shmb_handle);
  1289. dma_free_coherent(&pdev->dev, enc_tzlog_info.size,
  1290. enc_tzlog_info.vaddr, enc_tzlog_info.paddr);
  1291. qtee_shmbridge_deregister(enc_qseelog_info.shmb_handle);
  1292. dma_free_coherent(&pdev->dev, enc_qseelog_info.size,
  1293. enc_qseelog_info.vaddr, enc_qseelog_info.paddr);
  1294. }
  1295. static bool is_hyp_dir(int tzdbg_stat_type)
  1296. {
  1297. switch(tzdbg_stat_type)
  1298. {
  1299. case TZDBG_HYP_GENERAL:
  1300. case TZDBG_HYP_LOG:
  1301. case TZDBG_RM_LOG:
  1302. return true;
  1303. default:
  1304. return false;
  1305. }
  1306. return false;
  1307. }
  1308. static int tzdbg_fs_init(struct platform_device *pdev)
  1309. {
  1310. int rc = 0;
  1311. int i;
  1312. struct proc_dir_entry *dent_dir;
  1313. struct proc_dir_entry *dent;
  1314. dent_dir = proc_mkdir(TZDBG_DIR_NAME, NULL);
  1315. if (dent_dir == NULL) {
  1316. dev_err(&pdev->dev, "tzdbg proc_mkdir failed\n");
  1317. return -ENOMEM;
  1318. }
  1319. for (i = 0; i < TZDBG_STATS_MAX; i++) {
  1320. tzdbg.debug_tz[i] = i;
  1321. /*
  1322. * If hypervisor is disabled, do not create
  1323. * hyp_general, hyp_log and rm_log directories,
  1324. * as accessing them would give segmentation fault
  1325. */
  1326. if ((!tzdbg.is_hyplog_enabled) && (is_hyp_dir(i))) {
  1327. continue;
  1328. }
  1329. dent = proc_create_data(tzdbg.stat[i].name,
  1330. 0444, dent_dir,
  1331. &tzdbg_fops, &tzdbg.debug_tz[i]);
  1332. if (dent == NULL) {
  1333. dev_err(&pdev->dev, "TZ proc_create_data failed\n");
  1334. rc = -ENOMEM;
  1335. goto err;
  1336. }
  1337. }
  1338. platform_set_drvdata(pdev, dent_dir);
  1339. return 0;
  1340. err:
  1341. remove_proc_entry(TZDBG_DIR_NAME, NULL);
  1342. return rc;
  1343. }
  1344. static void tzdbg_fs_exit(struct platform_device *pdev)
  1345. {
  1346. struct proc_dir_entry *dent_dir;
  1347. dent_dir = platform_get_drvdata(pdev);
  1348. if (dent_dir)
  1349. remove_proc_entry(TZDBG_DIR_NAME, NULL);
  1350. }
  1351. static int __update_hypdbg_base(struct platform_device *pdev,
  1352. void __iomem *virt_iobase)
  1353. {
  1354. phys_addr_t hypdiag_phy_iobase;
  1355. uint32_t hyp_address_offset;
  1356. uint32_t hyp_size_offset;
  1357. struct hypdbg_t *hyp;
  1358. uint32_t *ptr = NULL;
  1359. if (of_property_read_u32((&pdev->dev)->of_node, "hyplog-address-offset",
  1360. &hyp_address_offset)) {
  1361. dev_err(&pdev->dev, "hyplog address offset is not defined\n");
  1362. return -EINVAL;
  1363. }
  1364. if (of_property_read_u32((&pdev->dev)->of_node, "hyplog-size-offset",
  1365. &hyp_size_offset)) {
  1366. dev_err(&pdev->dev, "hyplog size offset is not defined\n");
  1367. return -EINVAL;
  1368. }
  1369. hypdiag_phy_iobase = readl_relaxed(virt_iobase + hyp_address_offset);
  1370. tzdbg.hyp_debug_rw_buf_size = readl_relaxed(virt_iobase +
  1371. hyp_size_offset);
  1372. tzdbg.hyp_virt_iobase = devm_ioremap(&pdev->dev,
  1373. hypdiag_phy_iobase,
  1374. tzdbg.hyp_debug_rw_buf_size);
  1375. if (!tzdbg.hyp_virt_iobase) {
  1376. dev_err(&pdev->dev, "ERROR could not ioremap: start=%pr, len=%u\n",
  1377. &hypdiag_phy_iobase, tzdbg.hyp_debug_rw_buf_size);
  1378. return -ENXIO;
  1379. }
  1380. ptr = kzalloc(tzdbg.hyp_debug_rw_buf_size, GFP_KERNEL);
  1381. if (!ptr)
  1382. return -ENOMEM;
  1383. tzdbg.hyp_diag_buf = (struct hypdbg_t *)ptr;
  1384. hyp = tzdbg.hyp_diag_buf;
  1385. hyp->log_pos.wrap = hyp->log_pos.offset = 0;
  1386. return 0;
  1387. }
  1388. static int __update_rmlog_base(struct platform_device *pdev,
  1389. void __iomem *virt_iobase)
  1390. {
  1391. uint32_t rmlog_address;
  1392. uint32_t rmlog_size;
  1393. uint32_t *ptr = NULL;
  1394. /* if we don't get the node just ignore it */
  1395. if (of_property_read_u32((&pdev->dev)->of_node, "rmlog-address",
  1396. &rmlog_address)) {
  1397. dev_err(&pdev->dev, "RM log address is not defined\n");
  1398. tzdbg.rmlog_rw_buf_size = 0;
  1399. return 0;
  1400. }
  1401. /* if we don't get the node just ignore it */
  1402. if (of_property_read_u32((&pdev->dev)->of_node, "rmlog-size",
  1403. &rmlog_size)) {
  1404. dev_err(&pdev->dev, "RM log size is not defined\n");
  1405. tzdbg.rmlog_rw_buf_size = 0;
  1406. return 0;
  1407. }
  1408. tzdbg.rmlog_rw_buf_size = rmlog_size;
  1409. /* Check if there is RM log to read */
  1410. if (!tzdbg.rmlog_rw_buf_size) {
  1411. tzdbg.rmlog_virt_iobase = NULL;
  1412. tzdbg.rm_diag_buf = NULL;
  1413. dev_err(&pdev->dev, "RM log size is %d\n",
  1414. tzdbg.rmlog_rw_buf_size);
  1415. return 0;
  1416. }
  1417. tzdbg.rmlog_virt_iobase = devm_ioremap(&pdev->dev,
  1418. rmlog_address,
  1419. rmlog_size);
  1420. if (!tzdbg.rmlog_virt_iobase) {
  1421. dev_err(&pdev->dev, "ERROR could not ioremap: start=%pr, len=%u\n",
  1422. rmlog_address, tzdbg.rmlog_rw_buf_size);
  1423. return -ENXIO;
  1424. }
  1425. ptr = kzalloc(tzdbg.rmlog_rw_buf_size, GFP_KERNEL);
  1426. if (!ptr)
  1427. return -ENOMEM;
  1428. tzdbg.rm_diag_buf = (uint8_t *)ptr;
  1429. return 0;
  1430. }
  1431. static int tzdbg_get_tz_version(void)
  1432. {
  1433. u64 version;
  1434. int ret = 0;
  1435. ret = qcom_scm_get_tz_log_feat_id(&version);
  1436. if (ret) {
  1437. pr_err("%s: scm_call to get tz version failed\n",
  1438. __func__);
  1439. return ret;
  1440. }
  1441. tzdbg.tz_version = version;
  1442. ret = qcom_scm_get_tz_feat_id_version(QCOM_SCM_FEAT_DIAG_ID, &version);
  1443. if (ret) {
  1444. pr_err("%s: scm_call to get tz diag version failed, ret = %d\n",
  1445. __func__, ret);
  1446. return ret;
  1447. }
  1448. pr_warn("tz diag version is %x\n", version);
  1449. tzdbg.tz_diag_major_version =
  1450. ((version >> TZBSP_FVER_MAJOR_SHIFT) & TZBSP_FVER_MAJOR_MINOR_MASK);
  1451. tzdbg.tz_diag_minor_version =
  1452. ((version >> TZBSP_FVER_MINOR_SHIFT) & TZBSP_FVER_MAJOR_MINOR_MASK);
  1453. if (tzdbg.tz_diag_major_version == TZBSP_DIAG_MAJOR_VERSION_V9) {
  1454. switch (tzdbg.tz_diag_minor_version) {
  1455. case TZBSP_DIAG_MINOR_VERSION_V2:
  1456. case TZBSP_DIAG_MINOR_VERSION_V21:
  1457. case TZBSP_DIAG_MINOR_VERSION_V22:
  1458. tzdbg.is_enlarged_buf = true;
  1459. break;
  1460. default:
  1461. tzdbg.is_enlarged_buf = false;
  1462. }
  1463. } else {
  1464. tzdbg.is_enlarged_buf = false;
  1465. }
  1466. return ret;
  1467. }
  1468. static void tzdbg_query_encrypted_log(void)
  1469. {
  1470. int ret = 0;
  1471. uint64_t enabled;
  1472. ret = qcom_scm_query_encrypted_log_feature(&enabled);
  1473. if (ret) {
  1474. if (ret == -EIO)
  1475. pr_info("SCM_CALL : SYS CALL NOT SUPPORTED IN TZ\n");
  1476. else
  1477. pr_err("scm_call QUERY_ENCR_LOG_FEATURE failed ret %d\n", ret);
  1478. tzdbg.is_encrypted_log_enabled = false;
  1479. } else {
  1480. pr_warn("encrypted qseelog enabled is %d\n", enabled);
  1481. tzdbg.is_encrypted_log_enabled = enabled;
  1482. }
  1483. }
  1484. /*
  1485. * Driver functions
  1486. */
  1487. static int tz_log_probe(struct platform_device *pdev)
  1488. {
  1489. struct resource *resource;
  1490. void __iomem *virt_iobase;
  1491. phys_addr_t tzdiag_phy_iobase;
  1492. uint32_t *ptr = NULL;
  1493. int ret = 0;
  1494. ret = tzdbg_get_tz_version();
  1495. if (ret)
  1496. return ret;
  1497. /*
  1498. * Get address that stores the physical location diagnostic data
  1499. */
  1500. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1501. if (!resource) {
  1502. dev_err(&pdev->dev,
  1503. "%s: ERROR Missing MEM resource\n", __func__);
  1504. return -ENXIO;
  1505. }
  1506. /*
  1507. * Get the debug buffer size
  1508. */
  1509. debug_rw_buf_size = resource_size(resource);
  1510. /*
  1511. * Map address that stores the physical location diagnostic data
  1512. */
  1513. virt_iobase = devm_ioremap(&pdev->dev, resource->start,
  1514. debug_rw_buf_size);
  1515. if (!virt_iobase) {
  1516. dev_err(&pdev->dev,
  1517. "%s: ERROR could not ioremap: start=%pr, len=%u\n",
  1518. __func__, &resource->start,
  1519. (unsigned int)(debug_rw_buf_size));
  1520. return -ENXIO;
  1521. }
  1522. if (pdev->dev.of_node) {
  1523. tzdbg.is_hyplog_enabled = of_property_read_bool(
  1524. (&pdev->dev)->of_node, "qcom,hyplog-enabled");
  1525. if (tzdbg.is_hyplog_enabled) {
  1526. ret = __update_hypdbg_base(pdev, virt_iobase);
  1527. if (ret) {
  1528. dev_err(&pdev->dev,
  1529. "%s: fail to get hypdbg_base ret %d\n",
  1530. __func__, ret);
  1531. return -EINVAL;
  1532. }
  1533. ret = __update_rmlog_base(pdev, virt_iobase);
  1534. if (ret) {
  1535. dev_err(&pdev->dev,
  1536. "%s: fail to get rmlog_base ret %d\n",
  1537. __func__, ret);
  1538. return -EINVAL;
  1539. }
  1540. } else {
  1541. dev_info(&pdev->dev, "Hyp log service not support\n");
  1542. }
  1543. } else {
  1544. dev_dbg(&pdev->dev, "Device tree data is not found\n");
  1545. }
  1546. /*
  1547. * Retrieve the address of diagnostic data
  1548. */
  1549. tzdiag_phy_iobase = readl_relaxed(virt_iobase);
  1550. tzdbg_query_encrypted_log();
  1551. /*
  1552. * Map the diagnostic information area if encryption is disabled
  1553. */
  1554. if (!tzdbg.is_encrypted_log_enabled) {
  1555. tzdbg.virt_iobase = devm_ioremap(&pdev->dev,
  1556. tzdiag_phy_iobase, debug_rw_buf_size);
  1557. if (!tzdbg.virt_iobase) {
  1558. dev_err(&pdev->dev,
  1559. "%s: could not ioremap: start=%pr, len=%u\n",
  1560. __func__, &tzdiag_phy_iobase,
  1561. debug_rw_buf_size);
  1562. return -ENXIO;
  1563. }
  1564. /* allocate diag_buf */
  1565. ptr = kzalloc(debug_rw_buf_size, GFP_KERNEL);
  1566. if (ptr == NULL)
  1567. return -ENOMEM;
  1568. tzdbg.diag_buf = (struct tzdbg_t *)ptr;
  1569. } else {
  1570. if ((tzdbg.tz_diag_major_version == TZBSP_DIAG_MAJOR_VERSION_V9) &&
  1571. (tzdbg.tz_diag_minor_version >= TZBSP_DIAG_MINOR_VERSION_V22))
  1572. tzdbg.is_full_encrypted_tz_logs_supported = true;
  1573. if (pdev->dev.of_node) {
  1574. tzdbg.is_full_encrypted_tz_logs_enabled = of_property_read_bool(
  1575. (&pdev->dev)->of_node, "qcom,full-encrypted-tz-logs-enabled");
  1576. }
  1577. }
  1578. /* register unencrypted qsee log buffer */
  1579. ret = tzdbg_register_qsee_log_buf(pdev);
  1580. if (ret)
  1581. goto exit_free_diag_buf;
  1582. /* allocate encrypted qsee and tz log buffer */
  1583. ret = tzdbg_allocate_encrypted_log_buf(pdev);
  1584. if (ret) {
  1585. dev_err(&pdev->dev,
  1586. " %s: Failed to allocate encrypted log buffer\n",
  1587. __func__);
  1588. goto exit_free_qsee_log_buf;
  1589. }
  1590. /* allocate display_buf */
  1591. if (UINT_MAX/4 < qseelog_buf_size) {
  1592. pr_err("display_buf_size integer overflow\n");
  1593. goto exit_free_qsee_log_buf;
  1594. }
  1595. display_buf_size = qseelog_buf_size * 4;
  1596. tzdbg.disp_buf = dma_alloc_coherent(&pdev->dev, display_buf_size,
  1597. &disp_buf_paddr, GFP_KERNEL);
  1598. if (tzdbg.disp_buf == NULL) {
  1599. ret = -ENOMEM;
  1600. goto exit_free_encr_log_buf;
  1601. }
  1602. if (tzdbg_fs_init(pdev))
  1603. goto exit_free_disp_buf;
  1604. return 0;
  1605. exit_free_disp_buf:
  1606. dma_free_coherent(&pdev->dev, display_buf_size,
  1607. (void *)tzdbg.disp_buf, disp_buf_paddr);
  1608. exit_free_encr_log_buf:
  1609. tzdbg_free_encrypted_log_buf(pdev);
  1610. exit_free_qsee_log_buf:
  1611. tzdbg_free_qsee_log_buf(pdev);
  1612. exit_free_diag_buf:
  1613. if (!tzdbg.is_encrypted_log_enabled)
  1614. kfree(tzdbg.diag_buf);
  1615. return -ENXIO;
  1616. }
  1617. static int tz_log_remove(struct platform_device *pdev)
  1618. {
  1619. tzdbg_fs_exit(pdev);
  1620. dma_free_coherent(&pdev->dev, display_buf_size,
  1621. (void *)tzdbg.disp_buf, disp_buf_paddr);
  1622. tzdbg_free_encrypted_log_buf(pdev);
  1623. tzdbg_free_qsee_log_buf(pdev);
  1624. if (!tzdbg.is_encrypted_log_enabled)
  1625. kfree(tzdbg.diag_buf);
  1626. return 0;
  1627. }
  1628. static const struct of_device_id tzlog_match[] = {
  1629. {.compatible = "qcom,tz-log"},
  1630. {}
  1631. };
  1632. static struct platform_driver tz_log_driver = {
  1633. .probe = tz_log_probe,
  1634. .remove = tz_log_remove,
  1635. .driver = {
  1636. .name = "tz_log",
  1637. .of_match_table = tzlog_match,
  1638. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1639. },
  1640. };
  1641. module_platform_driver(tz_log_driver);
  1642. MODULE_LICENSE("GPL v2");
  1643. MODULE_DESCRIPTION("TZ Log driver");