pmf_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <arch.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <lib/pmf/pmf.h>
  13. #include <lib/utils_def.h>
  14. #include <plat/common/platform.h>
  15. /*******************************************************************************
  16. * The 'pmf_svc_descs' array holds the PMF service descriptors exported by
  17. * services by placing them in the '.pmf_svc_descs' linker section.
  18. * The 'pmf_svc_descs_indices' array holds the index of a descriptor in the
  19. * 'pmf_svc_descs' array. The TIF[15:10] bits in the time-stamp id are used
  20. * to get an index into the 'pmf_svc_descs_indices' array. This gives the
  21. * index of the descriptor in the 'pmf_svc_descs' array which contains the
  22. * service function pointers.
  23. ******************************************************************************/
  24. IMPORT_SYM(uintptr_t, __PMF_SVC_DESCS_START__, PMF_SVC_DESCS_START);
  25. IMPORT_SYM(uintptr_t, __PMF_SVC_DESCS_END__, PMF_SVC_DESCS_END);
  26. IMPORT_SYM(uintptr_t, __PMF_PERCPU_TIMESTAMP_END__, PMF_PERCPU_TIMESTAMP_END);
  27. IMPORT_SYM(uintptr_t, __PMF_TIMESTAMP_START__, PMF_TIMESTAMP_ARRAY_START);
  28. #define PMF_PERCPU_TIMESTAMP_SIZE (PMF_PERCPU_TIMESTAMP_END - PMF_TIMESTAMP_ARRAY_START)
  29. #define PMF_SVC_DESCS_MAX 10
  30. /*
  31. * This is used to traverse through registered PMF services.
  32. */
  33. static pmf_svc_desc_t *pmf_svc_descs;
  34. /*
  35. * This array is used to store registered PMF services in sorted order.
  36. */
  37. static int pmf_svc_descs_indices[PMF_SVC_DESCS_MAX];
  38. /*
  39. * This is used to track total number of successfully registered PMF services.
  40. */
  41. static int pmf_num_services;
  42. /*
  43. * This is the main PMF function that initialize registered
  44. * PMF services and also sort them in ascending order.
  45. */
  46. int pmf_setup(void)
  47. {
  48. int rc, ii, jj = 0;
  49. int pmf_svc_descs_num, temp_val;
  50. /* If no PMF services are registered then simply bail out */
  51. pmf_svc_descs_num = (PMF_SVC_DESCS_END - PMF_SVC_DESCS_START)/
  52. sizeof(pmf_svc_desc_t);
  53. if (pmf_svc_descs_num == 0)
  54. return 0;
  55. assert(pmf_svc_descs_num < PMF_SVC_DESCS_MAX);
  56. pmf_svc_descs = (pmf_svc_desc_t *) PMF_SVC_DESCS_START;
  57. for (ii = 0; ii < pmf_svc_descs_num; ii++) {
  58. assert(pmf_svc_descs[ii].get_ts != NULL);
  59. /*
  60. * Call the initialization routine for this
  61. * PMF service, if it is defined.
  62. */
  63. if (pmf_svc_descs[ii].init != NULL) {
  64. rc = pmf_svc_descs[ii].init();
  65. if (rc != 0) {
  66. WARN("Could not initialize PMF"
  67. "service %s - skipping \n",
  68. pmf_svc_descs[ii].name);
  69. continue;
  70. }
  71. }
  72. /* Update the pmf_svc_descs_indices array */
  73. pmf_svc_descs_indices[jj++] = ii;
  74. }
  75. pmf_num_services = jj;
  76. /*
  77. * Sort the successfully registered PMF services
  78. * according to service ID
  79. */
  80. for (ii = 1; ii < pmf_num_services; ii++) {
  81. for (jj = 0; jj < (pmf_num_services - ii); jj++) {
  82. if ((pmf_svc_descs[jj].svc_config & PMF_SVC_ID_MASK) >
  83. (pmf_svc_descs[jj + 1].svc_config &
  84. PMF_SVC_ID_MASK)) {
  85. temp_val = pmf_svc_descs_indices[jj];
  86. pmf_svc_descs_indices[jj] =
  87. pmf_svc_descs_indices[jj+1];
  88. pmf_svc_descs_indices[jj+1] = temp_val;
  89. }
  90. }
  91. }
  92. return 0;
  93. }
  94. /*
  95. * This function implements binary search to find registered
  96. * PMF service based on Service ID provided in `tid` argument.
  97. */
  98. static pmf_svc_desc_t *get_service(unsigned int tid)
  99. {
  100. int low = 0;
  101. int mid;
  102. int high = pmf_num_services;
  103. unsigned int svc_id = tid & PMF_SVC_ID_MASK;
  104. int index;
  105. unsigned int desc_svc_id;
  106. if (pmf_num_services == 0)
  107. return NULL;
  108. assert(pmf_svc_descs != NULL);
  109. do {
  110. mid = (low + high) / 2;
  111. index = pmf_svc_descs_indices[mid];
  112. desc_svc_id = pmf_svc_descs[index].svc_config & PMF_SVC_ID_MASK;
  113. if (svc_id < desc_svc_id)
  114. high = mid - 1;
  115. if (svc_id > desc_svc_id)
  116. low = mid + 1;
  117. } while ((svc_id != desc_svc_id) && (low <= high));
  118. /*
  119. * Make sure the Service found supports the tid range.
  120. */
  121. if ((svc_id == desc_svc_id) && ((tid & PMF_TID_MASK) <
  122. (pmf_svc_descs[index].svc_config & PMF_TID_MASK)))
  123. return (pmf_svc_desc_t *)&pmf_svc_descs[index];
  124. return NULL;
  125. }
  126. /*
  127. * This function gets the time-stamp value for the PMF services
  128. * registered for SMC interface based on `tid` and `mpidr`.
  129. */
  130. int pmf_get_timestamp_smc(unsigned int tid,
  131. u_register_t mpidr,
  132. unsigned int flags,
  133. unsigned long long *ts_value)
  134. {
  135. pmf_svc_desc_t *svc_desc;
  136. assert(ts_value != NULL);
  137. /* Search for registered service. */
  138. svc_desc = get_service(tid);
  139. if ((svc_desc == NULL) || (plat_core_pos_by_mpidr(mpidr) < 0)) {
  140. *ts_value = 0;
  141. return -EINVAL;
  142. } else {
  143. /* Call the service time-stamp handler. */
  144. *ts_value = svc_desc->get_ts(tid, mpidr, flags);
  145. return 0;
  146. }
  147. }
  148. /*
  149. * This function can be used to dump `ts` value for given `tid`.
  150. * Assumption is that the console is already initialized.
  151. */
  152. void __pmf_dump_timestamp(unsigned int tid, unsigned long long ts)
  153. {
  154. printf("PMF:cpu %u tid %u ts %llu\n",
  155. plat_my_core_pos(), tid, ts);
  156. }
  157. /*
  158. * This function calculate the address identified by
  159. * `base_addr`, `tid` and `cpuid`.
  160. */
  161. static inline uintptr_t calc_ts_addr(uintptr_t base_addr,
  162. unsigned int tid,
  163. unsigned int cpuid)
  164. {
  165. assert(cpuid < PLATFORM_CORE_COUNT);
  166. assert(base_addr >= PMF_TIMESTAMP_ARRAY_START);
  167. assert(base_addr < ((PMF_TIMESTAMP_ARRAY_START +
  168. PMF_PERCPU_TIMESTAMP_SIZE) - ((tid & PMF_TID_MASK) *
  169. sizeof(unsigned long long))));
  170. base_addr += ((cpuid * PMF_PERCPU_TIMESTAMP_SIZE) +
  171. ((tid & PMF_TID_MASK) * sizeof(unsigned long long)));
  172. return base_addr;
  173. }
  174. /*
  175. * This function stores the `ts` value to the storage identified by
  176. * `base_addr`, `tid` and current cpu id.
  177. * Note: The timestamp addresses are cache line aligned per cpu
  178. * and only the owning CPU would ever write into it.
  179. */
  180. void __pmf_store_timestamp(uintptr_t base_addr,
  181. unsigned int tid,
  182. unsigned long long ts)
  183. {
  184. unsigned long long *ts_addr = (unsigned long long *)calc_ts_addr(base_addr,
  185. tid, plat_my_core_pos());
  186. *ts_addr = ts;
  187. }
  188. /*
  189. * This is the cached version of `pmf_store_my_timestamp`
  190. * Note: The timestamp addresses are cache line aligned per cpu
  191. * and only the owning CPU would ever write into it.
  192. */
  193. void __pmf_store_timestamp_with_cache_maint(uintptr_t base_addr,
  194. unsigned int tid,
  195. unsigned long long ts)
  196. {
  197. unsigned long long *ts_addr = (unsigned long long *)calc_ts_addr(base_addr,
  198. tid, plat_my_core_pos());
  199. *ts_addr = ts;
  200. flush_dcache_range((uintptr_t)ts_addr, sizeof(unsigned long long));
  201. }
  202. /*
  203. * This function retrieves the `ts` value from the storage identified by
  204. * `base_addr`, `tid` and `cpuid`.
  205. * Note: The timestamp addresses are cache line aligned per cpu.
  206. */
  207. unsigned long long __pmf_get_timestamp(uintptr_t base_addr,
  208. unsigned int tid,
  209. unsigned int cpuid,
  210. unsigned int flags)
  211. {
  212. assert(cpuid < PLATFORM_CORE_COUNT);
  213. unsigned long long *ts_addr = (unsigned long long *)calc_ts_addr(base_addr,
  214. tid, cpuid);
  215. if ((flags & PMF_CACHE_MAINT) != 0U)
  216. inv_dcache_range((uintptr_t)ts_addr, sizeof(unsigned long long));
  217. return *ts_addr;
  218. }