tz_counter.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/kthread.h>
  4. #include <linux/freezer.h>
  5. #include "tz_cross/trustzone.h"
  6. #include "tz_cross/ta_icnt.h"
  7. #include "trustzone/kree/system.h"
  8. #include "kree_int.h"
  9. #include "tz_counter.h"
  10. #ifdef ENABLE_INC_ONLY_COUNTER
  11. uint32_t TEECK_Icnt_Counter(KREE_SESSION_HANDLE session,
  12. uint32_t *a, uint32_t *b)
  13. {
  14. MTEEC_PARAM param[4];
  15. uint32_t paramTypes;
  16. TZ_RESULT ret;
  17. paramTypes = TZ_ParamTypes2(TZPT_VALUE_OUTPUT, TZPT_VALUE_OUTPUT);
  18. ret = KREE_TeeServiceCall(session, TZCMD_ICNT_COUNT, paramTypes, param);
  19. if (ret != TZ_RESULT_SUCCESS)
  20. pr_warn("ServiceCall error %d\n", ret);
  21. *a = param[0].value.a;
  22. *b = param[1].value.a;
  23. return ret;
  24. }
  25. uint32_t TEECK_Icnt_Rate(KREE_SESSION_HANDLE session, uint32_t *a)
  26. {
  27. MTEEC_PARAM param[4];
  28. uint32_t paramTypes;
  29. TZ_RESULT ret;
  30. paramTypes = TZ_ParamTypes1(TZPT_VALUE_OUTPUT);
  31. ret = KREE_TeeServiceCall(session, TZCMD_ICNT_RATE, paramTypes, param);
  32. if (ret != TZ_RESULT_SUCCESS)
  33. pr_warn("ServiceCall error %d\n", ret);
  34. *a = param[0].value.a;
  35. return ret;
  36. }
  37. #define THREAD_COUNT_FREQ 10
  38. int update_counter_thread(void *data)
  39. {
  40. TZ_RESULT ret;
  41. KREE_SESSION_HANDLE icnt_session;
  42. uint32_t result;
  43. uint32_t a, b, rate;
  44. uint32_t nsec = THREAD_COUNT_FREQ;
  45. ret = KREE_CreateSession(TZ_TA_ICNT_UUID, &icnt_session);
  46. if (ret != TZ_RESULT_SUCCESS) {
  47. pr_warn("CreateSession error %d\n", ret);
  48. return 1;
  49. }
  50. result = TEECK_Icnt_Rate(icnt_session, &rate);
  51. if (result == TZ_RESULT_SUCCESS) {
  52. /* pr_debug("(yjdbg) rate: %d\n", rate); */
  53. nsec = (0xffffffff / rate);
  54. nsec -= 600;
  55. /* pr_debug("(yjdbg) rate: %d\n", nsec); */
  56. }
  57. set_freezable();
  58. for (;;) {
  59. if (kthread_should_stop())
  60. break;
  61. if (try_to_freeze())
  62. continue;
  63. result = TEECK_Icnt_Counter(icnt_session, &a, &b);
  64. if (result == TZ_RESULT_SUCCESS) {
  65. pr_debug("(yjdbg) tz_test TZCMD_ICNT_COUNT: 0x%x, 0x%x\n",
  66. a, b);
  67. }
  68. schedule_timeout_interruptible(HZ * nsec);
  69. }
  70. ret = KREE_CloseSession(icnt_session);
  71. if (ret != TZ_RESULT_SUCCESS) {
  72. pr_warn("CloseSession error %d\n", ret);
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. #endif