intel_telemetry_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel SoC Core Telemetry Driver
  4. * Copyright (C) 2015, Intel Corporation.
  5. * All Rights Reserved.
  6. *
  7. * Telemetry Framework provides platform related PM and performance statistics.
  8. * This file provides the core telemetry API implementation.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <asm/intel_telemetry.h>
  13. #define DRIVER_NAME "intel_telemetry_core"
  14. struct telemetry_core_config {
  15. struct telemetry_plt_config *plt_config;
  16. const struct telemetry_core_ops *telem_ops;
  17. };
  18. static struct telemetry_core_config telm_core_conf;
  19. static int telemetry_def_update_events(struct telemetry_evtconfig pss_evtconfig,
  20. struct telemetry_evtconfig ioss_evtconfig)
  21. {
  22. return 0;
  23. }
  24. static int telemetry_def_set_sampling_period(u8 pss_period, u8 ioss_period)
  25. {
  26. return 0;
  27. }
  28. static int telemetry_def_get_sampling_period(u8 *pss_min_period,
  29. u8 *pss_max_period,
  30. u8 *ioss_min_period,
  31. u8 *ioss_max_period)
  32. {
  33. return 0;
  34. }
  35. static int telemetry_def_get_eventconfig(
  36. struct telemetry_evtconfig *pss_evtconfig,
  37. struct telemetry_evtconfig *ioss_evtconfig,
  38. int pss_len, int ioss_len)
  39. {
  40. return 0;
  41. }
  42. static int telemetry_def_get_trace_verbosity(enum telemetry_unit telem_unit,
  43. u32 *verbosity)
  44. {
  45. return 0;
  46. }
  47. static int telemetry_def_set_trace_verbosity(enum telemetry_unit telem_unit,
  48. u32 verbosity)
  49. {
  50. return 0;
  51. }
  52. static int telemetry_def_raw_read_eventlog(enum telemetry_unit telem_unit,
  53. struct telemetry_evtlog *evtlog,
  54. int len, int log_all_evts)
  55. {
  56. return 0;
  57. }
  58. static int telemetry_def_read_eventlog(enum telemetry_unit telem_unit,
  59. struct telemetry_evtlog *evtlog,
  60. int len, int log_all_evts)
  61. {
  62. return 0;
  63. }
  64. static int telemetry_def_add_events(u8 num_pss_evts, u8 num_ioss_evts,
  65. u32 *pss_evtmap, u32 *ioss_evtmap)
  66. {
  67. return 0;
  68. }
  69. static int telemetry_def_reset_events(void)
  70. {
  71. return 0;
  72. }
  73. static const struct telemetry_core_ops telm_defpltops = {
  74. .set_sampling_period = telemetry_def_set_sampling_period,
  75. .get_sampling_period = telemetry_def_get_sampling_period,
  76. .get_trace_verbosity = telemetry_def_get_trace_verbosity,
  77. .set_trace_verbosity = telemetry_def_set_trace_verbosity,
  78. .raw_read_eventlog = telemetry_def_raw_read_eventlog,
  79. .get_eventconfig = telemetry_def_get_eventconfig,
  80. .read_eventlog = telemetry_def_read_eventlog,
  81. .update_events = telemetry_def_update_events,
  82. .reset_events = telemetry_def_reset_events,
  83. .add_events = telemetry_def_add_events,
  84. };
  85. /**
  86. * telemetry_update_events() - Update telemetry Configuration
  87. * @pss_evtconfig: PSS related config. No change if num_evts = 0.
  88. * @pss_evtconfig: IOSS related config. No change if num_evts = 0.
  89. *
  90. * This API updates the IOSS & PSS Telemetry configuration. Old config
  91. * is overwritten. Call telemetry_reset_events when logging is over
  92. * All sample period values should be in the form of:
  93. * bits[6:3] -> value; bits [0:2]-> Exponent; Period = (Value *16^Exponent)
  94. *
  95. * Return: 0 success, < 0 for failure
  96. */
  97. int telemetry_update_events(struct telemetry_evtconfig pss_evtconfig,
  98. struct telemetry_evtconfig ioss_evtconfig)
  99. {
  100. return telm_core_conf.telem_ops->update_events(pss_evtconfig,
  101. ioss_evtconfig);
  102. }
  103. EXPORT_SYMBOL_GPL(telemetry_update_events);
  104. /**
  105. * telemetry_set_sampling_period() - Sets the IOSS & PSS sampling period
  106. * @pss_period: placeholder for PSS Period to be set.
  107. * Set to 0 if not required to be updated
  108. * @ioss_period: placeholder for IOSS Period to be set
  109. * Set to 0 if not required to be updated
  110. *
  111. * All values should be in the form of:
  112. * bits[6:3] -> value; bits [0:2]-> Exponent; Period = (Value *16^Exponent)
  113. *
  114. * Return: 0 success, < 0 for failure
  115. */
  116. int telemetry_set_sampling_period(u8 pss_period, u8 ioss_period)
  117. {
  118. return telm_core_conf.telem_ops->set_sampling_period(pss_period,
  119. ioss_period);
  120. }
  121. EXPORT_SYMBOL_GPL(telemetry_set_sampling_period);
  122. /**
  123. * telemetry_get_sampling_period() - Get IOSS & PSS min & max sampling period
  124. * @pss_min_period: placeholder for PSS Min Period supported
  125. * @pss_max_period: placeholder for PSS Max Period supported
  126. * @ioss_min_period: placeholder for IOSS Min Period supported
  127. * @ioss_max_period: placeholder for IOSS Max Period supported
  128. *
  129. * All values should be in the form of:
  130. * bits[6:3] -> value; bits [0:2]-> Exponent; Period = (Value *16^Exponent)
  131. *
  132. * Return: 0 success, < 0 for failure
  133. */
  134. int telemetry_get_sampling_period(u8 *pss_min_period, u8 *pss_max_period,
  135. u8 *ioss_min_period, u8 *ioss_max_period)
  136. {
  137. return telm_core_conf.telem_ops->get_sampling_period(pss_min_period,
  138. pss_max_period,
  139. ioss_min_period,
  140. ioss_max_period);
  141. }
  142. EXPORT_SYMBOL_GPL(telemetry_get_sampling_period);
  143. /**
  144. * telemetry_reset_events() - Restore the IOSS & PSS configuration to default
  145. *
  146. * Return: 0 success, < 0 for failure
  147. */
  148. int telemetry_reset_events(void)
  149. {
  150. return telm_core_conf.telem_ops->reset_events();
  151. }
  152. EXPORT_SYMBOL_GPL(telemetry_reset_events);
  153. /**
  154. * telemetry_get_eventconfig() - Returns the pss and ioss events enabled
  155. * @pss_evtconfig: Pointer to PSS related configuration.
  156. * @pss_evtconfig: Pointer to IOSS related configuration.
  157. * @pss_len: Number of u32 elements allocated for pss_evtconfig array
  158. * @ioss_len: Number of u32 elements allocated for ioss_evtconfig array
  159. *
  160. * Return: 0 success, < 0 for failure
  161. */
  162. int telemetry_get_eventconfig(struct telemetry_evtconfig *pss_evtconfig,
  163. struct telemetry_evtconfig *ioss_evtconfig,
  164. int pss_len, int ioss_len)
  165. {
  166. return telm_core_conf.telem_ops->get_eventconfig(pss_evtconfig,
  167. ioss_evtconfig,
  168. pss_len, ioss_len);
  169. }
  170. EXPORT_SYMBOL_GPL(telemetry_get_eventconfig);
  171. /**
  172. * telemetry_add_events() - Add IOSS & PSS configuration to existing settings.
  173. * @num_pss_evts: Number of PSS Events (<29) in pss_evtmap. Can be 0.
  174. * @num_ioss_evts: Number of IOSS Events (<29) in ioss_evtmap. Can be 0.
  175. * @pss_evtmap: Array of PSS Event-IDs to Enable
  176. * @ioss_evtmap: Array of PSS Event-IDs to Enable
  177. *
  178. * Events are appended to Old Configuration. In case of total events > 28, it
  179. * returns error. Call telemetry_reset_events to reset after eventlog done
  180. *
  181. * Return: 0 success, < 0 for failure
  182. */
  183. int telemetry_add_events(u8 num_pss_evts, u8 num_ioss_evts,
  184. u32 *pss_evtmap, u32 *ioss_evtmap)
  185. {
  186. return telm_core_conf.telem_ops->add_events(num_pss_evts,
  187. num_ioss_evts, pss_evtmap,
  188. ioss_evtmap);
  189. }
  190. EXPORT_SYMBOL_GPL(telemetry_add_events);
  191. /**
  192. * telemetry_read_events() - Fetches samples as specified by evtlog.telem_evt_id
  193. * @telem_unit: Specify whether IOSS or PSS Read
  194. * @evtlog: Array of telemetry_evtlog structs to fill data
  195. * evtlog.telem_evt_id specifies the ids to read
  196. * @len: Length of array of evtlog
  197. *
  198. * Return: number of eventlogs read for success, < 0 for failure
  199. */
  200. int telemetry_read_events(enum telemetry_unit telem_unit,
  201. struct telemetry_evtlog *evtlog, int len)
  202. {
  203. return telm_core_conf.telem_ops->read_eventlog(telem_unit, evtlog,
  204. len, 0);
  205. }
  206. EXPORT_SYMBOL_GPL(telemetry_read_events);
  207. /**
  208. * telemetry_raw_read_events() - Fetch samples specified by evtlog.telem_evt_id
  209. * @telem_unit: Specify whether IOSS or PSS Read
  210. * @evtlog: Array of telemetry_evtlog structs to fill data
  211. * evtlog.telem_evt_id specifies the ids to read
  212. * @len: Length of array of evtlog
  213. *
  214. * The caller must take care of locking in this case.
  215. *
  216. * Return: number of eventlogs read for success, < 0 for failure
  217. */
  218. int telemetry_raw_read_events(enum telemetry_unit telem_unit,
  219. struct telemetry_evtlog *evtlog, int len)
  220. {
  221. return telm_core_conf.telem_ops->raw_read_eventlog(telem_unit, evtlog,
  222. len, 0);
  223. }
  224. EXPORT_SYMBOL_GPL(telemetry_raw_read_events);
  225. /**
  226. * telemetry_read_eventlog() - Fetch the Telemetry log from PSS or IOSS
  227. * @telem_unit: Specify whether IOSS or PSS Read
  228. * @evtlog: Array of telemetry_evtlog structs to fill data
  229. * @len: Length of array of evtlog
  230. *
  231. * Return: number of eventlogs read for success, < 0 for failure
  232. */
  233. int telemetry_read_eventlog(enum telemetry_unit telem_unit,
  234. struct telemetry_evtlog *evtlog, int len)
  235. {
  236. return telm_core_conf.telem_ops->read_eventlog(telem_unit, evtlog,
  237. len, 1);
  238. }
  239. EXPORT_SYMBOL_GPL(telemetry_read_eventlog);
  240. /**
  241. * telemetry_raw_read_eventlog() - Fetch the Telemetry log from PSS or IOSS
  242. * @telem_unit: Specify whether IOSS or PSS Read
  243. * @evtlog: Array of telemetry_evtlog structs to fill data
  244. * @len: Length of array of evtlog
  245. *
  246. * The caller must take care of locking in this case.
  247. *
  248. * Return: number of eventlogs read for success, < 0 for failure
  249. */
  250. int telemetry_raw_read_eventlog(enum telemetry_unit telem_unit,
  251. struct telemetry_evtlog *evtlog, int len)
  252. {
  253. return telm_core_conf.telem_ops->raw_read_eventlog(telem_unit, evtlog,
  254. len, 1);
  255. }
  256. EXPORT_SYMBOL_GPL(telemetry_raw_read_eventlog);
  257. /**
  258. * telemetry_get_trace_verbosity() - Get the IOSS & PSS Trace verbosity
  259. * @telem_unit: Specify whether IOSS or PSS Read
  260. * @verbosity: Pointer to return Verbosity
  261. *
  262. * Return: 0 success, < 0 for failure
  263. */
  264. int telemetry_get_trace_verbosity(enum telemetry_unit telem_unit,
  265. u32 *verbosity)
  266. {
  267. return telm_core_conf.telem_ops->get_trace_verbosity(telem_unit,
  268. verbosity);
  269. }
  270. EXPORT_SYMBOL_GPL(telemetry_get_trace_verbosity);
  271. /**
  272. * telemetry_set_trace_verbosity() - Update the IOSS & PSS Trace verbosity
  273. * @telem_unit: Specify whether IOSS or PSS Read
  274. * @verbosity: Verbosity to set
  275. *
  276. * Return: 0 success, < 0 for failure
  277. */
  278. int telemetry_set_trace_verbosity(enum telemetry_unit telem_unit, u32 verbosity)
  279. {
  280. return telm_core_conf.telem_ops->set_trace_verbosity(telem_unit,
  281. verbosity);
  282. }
  283. EXPORT_SYMBOL_GPL(telemetry_set_trace_verbosity);
  284. /**
  285. * telemetry_set_pltdata() - Set the platform specific Data
  286. * @ops: Pointer to ops structure
  287. * @pltconfig: Platform config data
  288. *
  289. * Usage by other than telemetry pltdrv module is invalid
  290. *
  291. * Return: 0 success, < 0 for failure
  292. */
  293. int telemetry_set_pltdata(const struct telemetry_core_ops *ops,
  294. struct telemetry_plt_config *pltconfig)
  295. {
  296. if (ops)
  297. telm_core_conf.telem_ops = ops;
  298. if (pltconfig)
  299. telm_core_conf.plt_config = pltconfig;
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(telemetry_set_pltdata);
  303. /**
  304. * telemetry_clear_pltdata() - Clear the platform specific Data
  305. *
  306. * Usage by other than telemetry pltdrv module is invalid
  307. *
  308. * Return: 0 success, < 0 for failure
  309. */
  310. int telemetry_clear_pltdata(void)
  311. {
  312. telm_core_conf.telem_ops = &telm_defpltops;
  313. telm_core_conf.plt_config = NULL;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(telemetry_clear_pltdata);
  317. /**
  318. * telemetry_get_pltdata() - Return telemetry platform config
  319. *
  320. * May be used by other telemetry modules to get platform specific
  321. * configuration.
  322. */
  323. struct telemetry_plt_config *telemetry_get_pltdata(void)
  324. {
  325. return telm_core_conf.plt_config;
  326. }
  327. EXPORT_SYMBOL_GPL(telemetry_get_pltdata);
  328. static inline int telemetry_get_pssevtname(enum telemetry_unit telem_unit,
  329. const char **name, int len)
  330. {
  331. struct telemetry_unit_config psscfg;
  332. int i;
  333. if (!telm_core_conf.plt_config)
  334. return -EINVAL;
  335. psscfg = telm_core_conf.plt_config->pss_config;
  336. if (len > psscfg.ssram_evts_used)
  337. len = psscfg.ssram_evts_used;
  338. for (i = 0; i < len; i++)
  339. name[i] = psscfg.telem_evts[i].name;
  340. return 0;
  341. }
  342. static inline int telemetry_get_iossevtname(enum telemetry_unit telem_unit,
  343. const char **name, int len)
  344. {
  345. struct telemetry_unit_config iosscfg;
  346. int i;
  347. if (!(telm_core_conf.plt_config))
  348. return -EINVAL;
  349. iosscfg = telm_core_conf.plt_config->ioss_config;
  350. if (len > iosscfg.ssram_evts_used)
  351. len = iosscfg.ssram_evts_used;
  352. for (i = 0; i < len; i++)
  353. name[i] = iosscfg.telem_evts[i].name;
  354. return 0;
  355. }
  356. /**
  357. * telemetry_get_evtname() - Checkif platform config is valid
  358. * @telem_unit: Telemetry Unit to check
  359. * @name: Array of character pointers to contain name
  360. * @len: length of array name provided by user
  361. *
  362. * Usage by other than telemetry debugfs module is invalid
  363. *
  364. * Return: 0 success, < 0 for failure
  365. */
  366. int telemetry_get_evtname(enum telemetry_unit telem_unit,
  367. const char **name, int len)
  368. {
  369. int ret = -EINVAL;
  370. if (telem_unit == TELEM_PSS)
  371. ret = telemetry_get_pssevtname(telem_unit, name, len);
  372. else if (telem_unit == TELEM_IOSS)
  373. ret = telemetry_get_iossevtname(telem_unit, name, len);
  374. return ret;
  375. }
  376. EXPORT_SYMBOL_GPL(telemetry_get_evtname);
  377. static int __init telemetry_module_init(void)
  378. {
  379. pr_info(pr_fmt(DRIVER_NAME) " Init\n");
  380. telm_core_conf.telem_ops = &telm_defpltops;
  381. return 0;
  382. }
  383. static void __exit telemetry_module_exit(void)
  384. {
  385. }
  386. module_init(telemetry_module_init);
  387. module_exit(telemetry_module_exit);
  388. MODULE_AUTHOR("Souvik Kumar Chakravarty <souvik.k.chakravarty@intel.com>");
  389. MODULE_DESCRIPTION("Intel SoC Telemetry Interface");
  390. MODULE_LICENSE("GPL v2");