remoteqdss.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <soc/qcom/scm.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/dma-mapping.h>
  20. #define REMOTEQDSS_FLAG_QUIET (BIT(0))
  21. static unsigned long remoteqdss_dbg_flags;
  22. module_param_named(dbg_flags, remoteqdss_dbg_flags, ulong, 0644);
  23. static struct dentry *remoteqdss_dir;
  24. #define REMOTEQDSS_ERR(fmt, ...) \
  25. pr_debug("%s: " fmt, __func__, ## __VA_ARGS__)
  26. #define REMOTEQDSS_ERR_CALLER(fmt, caller, ...) \
  27. pr_debug("%pf: " fmt, caller, ## __VA_ARGS__)
  28. struct qdss_msg_translation {
  29. u64 val;
  30. char *msg;
  31. };
  32. /*
  33. * id Unique identifier
  34. * sw_entity_group Array index
  35. * sw_event_group Array index
  36. * dir Parent debugfs directory
  37. */
  38. struct remoteqdss_data {
  39. uint32_t id;
  40. uint32_t sw_entity_group;
  41. uint32_t sw_event_group;
  42. struct dentry *dir;
  43. };
  44. static struct device dma_dev;
  45. /* Allowed message formats */
  46. enum remoteqdss_cmd_id {
  47. CMD_ID_QUERY_SWEVENT_TAG,
  48. CMD_ID_FILTER_SWTRACE_STATE,
  49. CMD_ID_QUERY_SWTRACE_STATE,
  50. CMD_ID_FILTER_SWEVENT,
  51. CMD_ID_QUERY_SWEVENT,
  52. CMD_ID_FILTER_SWENTITY,
  53. CMD_ID_QUERY_SWENTITY,
  54. };
  55. struct remoteqdss_header_fmt {
  56. uint32_t subsys_id;
  57. uint32_t cmd_id;
  58. };
  59. struct remoteqdss_filter_swtrace_state_fmt {
  60. struct remoteqdss_header_fmt h;
  61. uint32_t state;
  62. };
  63. struct remoteqdss_filter_swevent_fmt {
  64. struct remoteqdss_header_fmt h;
  65. uint32_t event_group;
  66. uint32_t event_mask;
  67. };
  68. struct remoteqdss_query_swevent_fmt {
  69. struct remoteqdss_header_fmt h;
  70. uint32_t event_group;
  71. };
  72. struct remoteqdss_filter_swentity_fmt {
  73. struct remoteqdss_header_fmt h;
  74. uint32_t entity_group;
  75. uint32_t entity_mask;
  76. };
  77. struct remoteqdss_query_swentity_fmt {
  78. struct remoteqdss_header_fmt h;
  79. uint32_t entity_group;
  80. };
  81. /* msgs is a null terminated array */
  82. static void remoteqdss_err_translation(struct qdss_msg_translation *msgs,
  83. u64 err, const void *caller)
  84. {
  85. static DEFINE_RATELIMIT_STATE(rl, 5 * HZ, 2);
  86. struct qdss_msg_translation *msg;
  87. if (!err)
  88. return;
  89. if (remoteqdss_dbg_flags & REMOTEQDSS_FLAG_QUIET)
  90. return;
  91. for (msg = msgs; msg->msg; msg++) {
  92. if (err == msg->val && __ratelimit(&rl)) {
  93. REMOTEQDSS_ERR_CALLER("0x%llx: %s\n", caller, err,
  94. msg->msg);
  95. return;
  96. }
  97. }
  98. REMOTEQDSS_ERR_CALLER("Error 0x%llx\n", caller, err);
  99. }
  100. /* Shared across all remoteqdss scm functions */
  101. #define SCM_CMD_ID (0x1)
  102. /* Response Values */
  103. #define SCM_CMD_FAIL (0x80)
  104. #define SCM_QDSS_UNAVAILABLE (0x81)
  105. #define SCM_UNINITIALIZED (0x82)
  106. #define SCM_BAD_ARG (0x83)
  107. #define SCM_BAD_SUBSYS (0x85)
  108. static struct qdss_msg_translation remoteqdss_scm_msgs[] = {
  109. {SCM_CMD_FAIL,
  110. "Command failed"},
  111. {SCM_QDSS_UNAVAILABLE,
  112. "QDSS not available or cannot turn QDSS (clock) on"},
  113. {SCM_UNINITIALIZED,
  114. "Tracer not initialized or unable to initialize"},
  115. {SCM_BAD_ARG,
  116. "Invalid parameter value"},
  117. {SCM_BAD_SUBSYS,
  118. "Incorrect subsys ID"},
  119. {}
  120. };
  121. static struct remoteqdss_data *create_remoteqdss_data(u32 id)
  122. {
  123. struct remoteqdss_data *data;
  124. data = kzalloc(sizeof(*data), GFP_KERNEL);
  125. if (!data)
  126. return NULL;
  127. data->id = id;
  128. return data;
  129. }
  130. static void free_remoteqdss_data(struct remoteqdss_data *data)
  131. {
  132. kfree(data);
  133. }
  134. static int remoteqdss_do_scm_call(struct scm_desc *desc,
  135. dma_addr_t addr, size_t size, const void *caller)
  136. {
  137. int ret;
  138. memset(desc, 0, sizeof(*desc));
  139. desc->args[0] = dma_to_phys(&dma_dev, addr);
  140. desc->args[1] = size;
  141. desc->arginfo = SCM_ARGS(2, SCM_RO, SCM_VAL);
  142. ret = scm_call2(
  143. SCM_SIP_FNID(SCM_SVC_QDSS, SCM_CMD_ID),
  144. desc);
  145. if (ret)
  146. return ret;
  147. remoteqdss_err_translation(remoteqdss_scm_msgs, desc->ret[0], caller);
  148. ret = desc->ret[0] ? -EINVAL : 0;
  149. return ret;
  150. }
  151. static int remoteqdss_scm_query_swtrace(void *priv, u64 *val)
  152. {
  153. struct remoteqdss_data *data = priv;
  154. int ret;
  155. struct scm_desc desc;
  156. struct remoteqdss_header_fmt *fmt;
  157. dma_addr_t addr;
  158. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  159. if (!fmt)
  160. return -ENOMEM;
  161. fmt->subsys_id = data->id;
  162. fmt->cmd_id = CMD_ID_QUERY_SWTRACE_STATE;
  163. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  164. __builtin_return_address(0));
  165. *val = desc.ret[1];
  166. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  167. return ret;
  168. }
  169. static int remoteqdss_scm_filter_swtrace(void *priv, u64 val)
  170. {
  171. struct remoteqdss_data *data = priv;
  172. int ret;
  173. struct scm_desc desc;
  174. struct remoteqdss_filter_swtrace_state_fmt *fmt;
  175. dma_addr_t addr;
  176. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  177. if (!fmt)
  178. return -ENOMEM;
  179. fmt->h.subsys_id = data->id;
  180. fmt->h.cmd_id = CMD_ID_FILTER_SWTRACE_STATE;
  181. fmt->state = (uint32_t)val;
  182. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  183. __builtin_return_address(0));
  184. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  185. return ret;
  186. }
  187. DEFINE_SIMPLE_ATTRIBUTE(fops_sw_trace_output,
  188. remoteqdss_scm_query_swtrace,
  189. remoteqdss_scm_filter_swtrace,
  190. "0x%llx\n");
  191. static int remoteqdss_scm_query_tag(void *priv, u64 *val)
  192. {
  193. struct remoteqdss_data *data = priv;
  194. int ret;
  195. struct scm_desc desc;
  196. struct remoteqdss_header_fmt *fmt;
  197. dma_addr_t addr;
  198. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  199. if (!fmt)
  200. return -ENOMEM;
  201. fmt->subsys_id = data->id;
  202. fmt->cmd_id = CMD_ID_QUERY_SWEVENT_TAG;
  203. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  204. __builtin_return_address(0));
  205. *val = desc.ret[1];
  206. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  207. return ret;
  208. }
  209. DEFINE_SIMPLE_ATTRIBUTE(fops_tag,
  210. remoteqdss_scm_query_tag,
  211. NULL,
  212. "0x%llx\n");
  213. static int remoteqdss_scm_query_swevent(void *priv, u64 *val)
  214. {
  215. struct remoteqdss_data *data = priv;
  216. int ret;
  217. struct scm_desc desc;
  218. struct remoteqdss_query_swevent_fmt *fmt;
  219. dma_addr_t addr;
  220. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  221. if (!fmt)
  222. return -ENOMEM;
  223. fmt->h.subsys_id = data->id;
  224. fmt->h.cmd_id = CMD_ID_QUERY_SWEVENT;
  225. fmt->event_group = data->sw_event_group;
  226. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  227. __builtin_return_address(0));
  228. *val = desc.ret[1];
  229. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  230. return ret;
  231. }
  232. static int remoteqdss_scm_filter_swevent(void *priv, u64 val)
  233. {
  234. struct remoteqdss_data *data = priv;
  235. int ret;
  236. struct scm_desc desc;
  237. struct remoteqdss_filter_swevent_fmt *fmt;
  238. dma_addr_t addr;
  239. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  240. if (!fmt)
  241. return -ENOMEM;
  242. fmt->h.subsys_id = data->id;
  243. fmt->h.cmd_id = CMD_ID_FILTER_SWEVENT;
  244. fmt->event_group = data->sw_event_group;
  245. fmt->event_mask = (uint32_t)val;
  246. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  247. __builtin_return_address(0));
  248. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  249. return ret;
  250. }
  251. DEFINE_SIMPLE_ATTRIBUTE(fops_swevent,
  252. remoteqdss_scm_query_swevent,
  253. remoteqdss_scm_filter_swevent,
  254. "0x%llx\n");
  255. static int remoteqdss_scm_query_swentity(void *priv, u64 *val)
  256. {
  257. struct remoteqdss_data *data = priv;
  258. int ret;
  259. struct scm_desc desc;
  260. struct remoteqdss_query_swentity_fmt *fmt;
  261. dma_addr_t addr;
  262. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  263. if (!fmt)
  264. return -ENOMEM;
  265. fmt->h.subsys_id = data->id;
  266. fmt->h.cmd_id = CMD_ID_QUERY_SWENTITY;
  267. fmt->entity_group = data->sw_entity_group;
  268. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  269. __builtin_return_address(0));
  270. *val = desc.ret[1];
  271. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  272. return ret;
  273. }
  274. static int remoteqdss_scm_filter_swentity(void *priv, u64 val)
  275. {
  276. struct remoteqdss_data *data = priv;
  277. int ret;
  278. struct scm_desc desc;
  279. struct remoteqdss_filter_swentity_fmt *fmt;
  280. dma_addr_t addr;
  281. fmt = dma_alloc_coherent(&dma_dev, sizeof(*fmt), &addr, GFP_KERNEL);
  282. if (!fmt)
  283. return -ENOMEM;
  284. fmt->h.subsys_id = data->id;
  285. fmt->h.cmd_id = CMD_ID_FILTER_SWENTITY;
  286. fmt->entity_group = data->sw_entity_group;
  287. fmt->entity_mask = (uint32_t)val;
  288. ret = remoteqdss_do_scm_call(&desc, addr, sizeof(*fmt),
  289. __builtin_return_address(0));
  290. dma_free_coherent(&dma_dev, sizeof(*fmt), fmt, addr);
  291. return ret;
  292. }
  293. DEFINE_SIMPLE_ATTRIBUTE(fops_swentity,
  294. remoteqdss_scm_query_swentity,
  295. remoteqdss_scm_filter_swentity,
  296. "0x%llx\n");
  297. static void __init enumerate_scm_devices(struct dentry *parent)
  298. {
  299. u64 unused;
  300. int ret;
  301. struct remoteqdss_data *data;
  302. struct dentry *dentry;
  303. if (!is_scm_armv8())
  304. return;
  305. data = create_remoteqdss_data(0);
  306. if (!data)
  307. return;
  308. /* Assume failure means device not present */
  309. ret = remoteqdss_scm_query_swtrace(data, &unused);
  310. if (ret)
  311. goto out;
  312. data->dir = debugfs_create_dir("tz", parent);
  313. if (IS_ERR_OR_NULL(data->dir))
  314. goto out;
  315. dentry = debugfs_create_file("sw_trace_output", 0644,
  316. data->dir, data, &fops_sw_trace_output);
  317. if (IS_ERR_OR_NULL(dentry))
  318. goto out;
  319. dentry = debugfs_create_u32("sw_entity_group", 0644,
  320. data->dir, &data->sw_entity_group);
  321. if (IS_ERR_OR_NULL(dentry))
  322. goto out;
  323. dentry = debugfs_create_u32("sw_event_group", 0644,
  324. data->dir, &data->sw_event_group);
  325. if (IS_ERR_OR_NULL(dentry))
  326. goto out;
  327. dentry = debugfs_create_file("tag", 0444,
  328. data->dir, data, &fops_tag);
  329. if (IS_ERR_OR_NULL(dentry))
  330. goto out;
  331. dentry = debugfs_create_file("swevent", 0644,
  332. data->dir, data, &fops_swevent);
  333. if (IS_ERR_OR_NULL(dentry))
  334. goto out;
  335. dentry = debugfs_create_file("swentity", 0644,
  336. data->dir, data, &fops_swentity);
  337. if (IS_ERR_OR_NULL(dentry))
  338. goto out;
  339. return;
  340. out:
  341. debugfs_remove_recursive(data->dir);
  342. free_remoteqdss_data(data);
  343. }
  344. static int __init remoteqdss_init(void)
  345. {
  346. unsigned long old_flags = remoteqdss_dbg_flags;
  347. int ret;
  348. /* Set up DMA */
  349. arch_setup_dma_ops(&dma_dev, 0, U64_MAX, NULL, false);
  350. ret = dma_coerce_mask_and_coherent(&dma_dev, DMA_BIT_MASK(64));
  351. if (ret)
  352. return ret;
  353. /*
  354. * disable normal error messages while checking
  355. * if support is present.
  356. */
  357. remoteqdss_dbg_flags |= REMOTEQDSS_FLAG_QUIET;
  358. remoteqdss_dir = debugfs_create_dir("remoteqdss", NULL);
  359. if (!remoteqdss_dir)
  360. return 0;
  361. enumerate_scm_devices(remoteqdss_dir);
  362. remoteqdss_dbg_flags = old_flags;
  363. return 0;
  364. }
  365. late_initcall(remoteqdss_init);