p_sys-t.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MIPI SyS-T framing protocol for STM devices.
  4. * Copyright (c) 2018, Intel Corporation.
  5. */
  6. #include <linux/configfs.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/slab.h>
  10. #include <linux/uuid.h>
  11. #include <linux/stm.h>
  12. #include "stm.h"
  13. enum sys_t_message_type {
  14. MIPI_SYST_TYPE_BUILD = 0,
  15. MIPI_SYST_TYPE_SHORT32,
  16. MIPI_SYST_TYPE_STRING,
  17. MIPI_SYST_TYPE_CATALOG,
  18. MIPI_SYST_TYPE_RAW = 6,
  19. MIPI_SYST_TYPE_SHORT64,
  20. MIPI_SYST_TYPE_CLOCK,
  21. MIPI_SYST_TYPE_SBD,
  22. };
  23. enum sys_t_message_severity {
  24. MIPI_SYST_SEVERITY_MAX = 0,
  25. MIPI_SYST_SEVERITY_FATAL,
  26. MIPI_SYST_SEVERITY_ERROR,
  27. MIPI_SYST_SEVERITY_WARNING,
  28. MIPI_SYST_SEVERITY_INFO,
  29. MIPI_SYST_SEVERITY_USER1,
  30. MIPI_SYST_SEVERITY_USER2,
  31. MIPI_SYST_SEVERITY_DEBUG,
  32. };
  33. enum sys_t_message_build_subtype {
  34. MIPI_SYST_BUILD_ID_COMPACT32 = 0,
  35. MIPI_SYST_BUILD_ID_COMPACT64,
  36. MIPI_SYST_BUILD_ID_LONG,
  37. };
  38. enum sys_t_message_clock_subtype {
  39. MIPI_SYST_CLOCK_TRANSPORT_SYNC = 1,
  40. };
  41. enum sys_t_message_string_subtype {
  42. MIPI_SYST_STRING_GENERIC = 1,
  43. MIPI_SYST_STRING_FUNCTIONENTER,
  44. MIPI_SYST_STRING_FUNCTIONEXIT,
  45. MIPI_SYST_STRING_INVALIDPARAM = 5,
  46. MIPI_SYST_STRING_ASSERT = 7,
  47. MIPI_SYST_STRING_PRINTF_32 = 11,
  48. MIPI_SYST_STRING_PRINTF_64 = 12,
  49. };
  50. /**
  51. * enum sys_t_message_sbd_subtype - SyS-T SBD message subtypes
  52. * @MIPI_SYST_SBD_ID32: SBD message with 32-bit message ID
  53. * @MIPI_SYST_SBD_ID64: SBD message with 64-bit message ID
  54. *
  55. * Structured Binary Data messages can send information of arbitrary length,
  56. * together with ID's that describe BLOB's content and layout.
  57. */
  58. enum sys_t_message_sbd_subtype {
  59. MIPI_SYST_SBD_ID32 = 0,
  60. MIPI_SYST_SBD_ID64 = 1,
  61. };
  62. #define MIPI_SYST_TYPE(t) ((u32)(MIPI_SYST_TYPE_ ## t))
  63. #define MIPI_SYST_SEVERITY(s) ((u32)(MIPI_SYST_SEVERITY_ ## s) << 4)
  64. #define MIPI_SYST_OPT_LOC BIT(8)
  65. #define MIPI_SYST_OPT_LEN BIT(9)
  66. #define MIPI_SYST_OPT_CHK BIT(10)
  67. #define MIPI_SYST_OPT_TS BIT(11)
  68. #define MIPI_SYST_UNIT(u) ((u32)(u) << 12)
  69. #define MIPI_SYST_ORIGIN(o) ((u32)(o) << 16)
  70. #define MIPI_SYST_OPT_GUID BIT(23)
  71. #define MIPI_SYST_SUBTYPE(s) ((u32)(MIPI_SYST_ ## s) << 24)
  72. #define MIPI_SYST_UNITLARGE(u) (MIPI_SYST_UNIT(u & 0xf) | \
  73. MIPI_SYST_ORIGIN(u >> 4))
  74. #define MIPI_SYST_TYPES(t, s) (MIPI_SYST_TYPE(t) | \
  75. MIPI_SYST_SUBTYPE(t ## _ ## s))
  76. #define DATA_HEADER (MIPI_SYST_TYPES(STRING, GENERIC) | \
  77. MIPI_SYST_SEVERITY(INFO) | \
  78. MIPI_SYST_OPT_GUID)
  79. #define CLOCK_SYNC_HEADER (MIPI_SYST_TYPES(CLOCK, TRANSPORT_SYNC) | \
  80. MIPI_SYST_SEVERITY(MAX))
  81. /*
  82. * SyS-T and ftrace headers are compatible to an extent that ftrace event ID
  83. * and args can be treated as SyS-T SBD message with 64-bit ID and arguments
  84. * BLOB right behind the header without modification. Bits [16:63] coming
  85. * together with message ID from ftrace aren't used by SBD and must be zeroed.
  86. *
  87. * 0 15 16 23 24 31 32 39 40 63
  88. * ftrace: <event_id> <flags> <preempt> <-pid-> <----> <args>
  89. * SBD: <------- msg_id ------------------------------> <BLOB>
  90. */
  91. #define SBD_HEADER (MIPI_SYST_TYPES(SBD, ID64) | \
  92. MIPI_SYST_SEVERITY(INFO) | \
  93. MIPI_SYST_OPT_GUID)
  94. struct sys_t_policy_node {
  95. uuid_t uuid;
  96. bool do_len;
  97. unsigned long ts_interval;
  98. unsigned long clocksync_interval;
  99. };
  100. struct sys_t_output {
  101. struct sys_t_policy_node node;
  102. unsigned long ts_jiffies;
  103. unsigned long clocksync_jiffies;
  104. };
  105. static void sys_t_policy_node_init(void *priv)
  106. {
  107. struct sys_t_policy_node *pn = priv;
  108. uuid_gen(&pn->uuid);
  109. }
  110. static int sys_t_output_open(void *priv, struct stm_output *output)
  111. {
  112. struct sys_t_policy_node *pn = priv;
  113. struct sys_t_output *opriv;
  114. opriv = kzalloc_obj(*opriv, GFP_ATOMIC);
  115. if (!opriv)
  116. return -ENOMEM;
  117. memcpy(&opriv->node, pn, sizeof(opriv->node));
  118. output->pdrv_private = opriv;
  119. return 0;
  120. }
  121. static void sys_t_output_close(struct stm_output *output)
  122. {
  123. kfree(output->pdrv_private);
  124. }
  125. static ssize_t sys_t_policy_uuid_show(struct config_item *item,
  126. char *page)
  127. {
  128. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  129. return sprintf(page, "%pU\n", &pn->uuid);
  130. }
  131. static ssize_t
  132. sys_t_policy_uuid_store(struct config_item *item, const char *page,
  133. size_t count)
  134. {
  135. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  136. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  137. int ret;
  138. mutex_lock(mutexp);
  139. ret = uuid_parse(page, &pn->uuid);
  140. mutex_unlock(mutexp);
  141. return ret < 0 ? ret : count;
  142. }
  143. CONFIGFS_ATTR(sys_t_policy_, uuid);
  144. static ssize_t sys_t_policy_do_len_show(struct config_item *item,
  145. char *page)
  146. {
  147. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  148. return sprintf(page, "%d\n", pn->do_len);
  149. }
  150. static ssize_t
  151. sys_t_policy_do_len_store(struct config_item *item, const char *page,
  152. size_t count)
  153. {
  154. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  155. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  156. int ret;
  157. mutex_lock(mutexp);
  158. ret = kstrtobool(page, &pn->do_len);
  159. mutex_unlock(mutexp);
  160. return ret ? ret : count;
  161. }
  162. CONFIGFS_ATTR(sys_t_policy_, do_len);
  163. static ssize_t sys_t_policy_ts_interval_show(struct config_item *item,
  164. char *page)
  165. {
  166. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  167. return sprintf(page, "%u\n", jiffies_to_msecs(pn->ts_interval));
  168. }
  169. static ssize_t
  170. sys_t_policy_ts_interval_store(struct config_item *item, const char *page,
  171. size_t count)
  172. {
  173. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  174. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  175. unsigned int ms;
  176. int ret;
  177. mutex_lock(mutexp);
  178. ret = kstrtouint(page, 10, &ms);
  179. mutex_unlock(mutexp);
  180. if (!ret) {
  181. pn->ts_interval = msecs_to_jiffies(ms);
  182. return count;
  183. }
  184. return ret;
  185. }
  186. CONFIGFS_ATTR(sys_t_policy_, ts_interval);
  187. static ssize_t sys_t_policy_clocksync_interval_show(struct config_item *item,
  188. char *page)
  189. {
  190. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  191. return sprintf(page, "%u\n", jiffies_to_msecs(pn->clocksync_interval));
  192. }
  193. static ssize_t
  194. sys_t_policy_clocksync_interval_store(struct config_item *item,
  195. const char *page, size_t count)
  196. {
  197. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  198. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  199. unsigned int ms;
  200. int ret;
  201. mutex_lock(mutexp);
  202. ret = kstrtouint(page, 10, &ms);
  203. mutex_unlock(mutexp);
  204. if (!ret) {
  205. pn->clocksync_interval = msecs_to_jiffies(ms);
  206. return count;
  207. }
  208. return ret;
  209. }
  210. CONFIGFS_ATTR(sys_t_policy_, clocksync_interval);
  211. static struct configfs_attribute *sys_t_policy_attrs[] = {
  212. &sys_t_policy_attr_uuid,
  213. &sys_t_policy_attr_do_len,
  214. &sys_t_policy_attr_ts_interval,
  215. &sys_t_policy_attr_clocksync_interval,
  216. NULL,
  217. };
  218. static inline bool sys_t_need_ts(struct sys_t_output *op)
  219. {
  220. if (op->node.ts_interval &&
  221. time_after(jiffies, op->ts_jiffies + op->node.ts_interval)) {
  222. op->ts_jiffies = jiffies;
  223. return true;
  224. }
  225. return false;
  226. }
  227. static bool sys_t_need_clock_sync(struct sys_t_output *op)
  228. {
  229. if (op->node.clocksync_interval &&
  230. time_after(jiffies,
  231. op->clocksync_jiffies + op->node.clocksync_interval)) {
  232. op->clocksync_jiffies = jiffies;
  233. return true;
  234. }
  235. return false;
  236. }
  237. static ssize_t
  238. sys_t_clock_sync(struct stm_data *data, unsigned int m, unsigned int c)
  239. {
  240. u32 header = CLOCK_SYNC_HEADER;
  241. const unsigned char nil = 0;
  242. u64 payload[2]; /* Clock value and frequency */
  243. ssize_t sz;
  244. sz = data->packet(data, m, c, STP_PACKET_DATA, STP_PACKET_TIMESTAMPED,
  245. 4, (u8 *)&header);
  246. if (sz <= 0)
  247. return sz;
  248. payload[0] = ktime_get_real_ns();
  249. payload[1] = NSEC_PER_SEC;
  250. sz = stm_data_write(data, m, c, false, &payload, sizeof(payload));
  251. if (sz <= 0)
  252. return sz;
  253. data->packet(data, m, c, STP_PACKET_FLAG, 0, 0, &nil);
  254. return sizeof(header) + sizeof(payload);
  255. }
  256. static inline u32 sys_t_header(struct stm_source_data *source)
  257. {
  258. if (source && source->type == STM_FTRACE)
  259. return SBD_HEADER;
  260. return DATA_HEADER;
  261. }
  262. static ssize_t sys_t_write_data(struct stm_data *data,
  263. struct stm_source_data *source,
  264. unsigned int master, unsigned int channel,
  265. bool ts_first, const void *buf, size_t count)
  266. {
  267. ssize_t sz;
  268. const unsigned char nil = 0;
  269. /*
  270. * Ftrace is zero-copy compatible with SyS-T SBD, but requires
  271. * special handling of first 64 bits. Trim and send them separately
  272. * to avoid damage on original ftrace buffer.
  273. */
  274. if (source && source->type == STM_FTRACE) {
  275. u64 compat_ftrace_header;
  276. ssize_t header_sz;
  277. ssize_t buf_sz;
  278. if (count < sizeof(compat_ftrace_header))
  279. return -EINVAL;
  280. /* SBD only makes use of low 16 bits (event ID) from ftrace event */
  281. compat_ftrace_header = *(u64 *)buf & 0xffff;
  282. header_sz = stm_data_write(data, master, channel, false,
  283. &compat_ftrace_header,
  284. sizeof(compat_ftrace_header));
  285. if (header_sz != sizeof(compat_ftrace_header))
  286. return header_sz;
  287. buf_sz = stm_data_write(data, master, channel, false,
  288. buf + header_sz, count - header_sz);
  289. if (buf_sz != count - header_sz)
  290. return buf_sz;
  291. sz = header_sz + buf_sz;
  292. } else {
  293. sz = stm_data_write(data, master, channel, false, buf, count);
  294. }
  295. if (sz <= 0)
  296. return sz;
  297. data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
  298. return sz;
  299. }
  300. static ssize_t sys_t_write(struct stm_data *data, struct stm_output *output,
  301. unsigned int chan, const char *buf, size_t count,
  302. struct stm_source_data *source)
  303. {
  304. struct sys_t_output *op = output->pdrv_private;
  305. unsigned int c = output->channel + chan;
  306. unsigned int m = output->master;
  307. u32 header = sys_t_header(source);
  308. u8 uuid[UUID_SIZE];
  309. ssize_t sz;
  310. /* We require an existing policy node to proceed */
  311. if (!op)
  312. return -EINVAL;
  313. if (sys_t_need_clock_sync(op)) {
  314. sz = sys_t_clock_sync(data, m, c);
  315. if (sz <= 0)
  316. return sz;
  317. }
  318. if (op->node.do_len)
  319. header |= MIPI_SYST_OPT_LEN;
  320. if (sys_t_need_ts(op))
  321. header |= MIPI_SYST_OPT_TS;
  322. /*
  323. * STP framing rules for SyS-T frames:
  324. * * the first packet of the SyS-T frame is timestamped;
  325. * * the last packet is a FLAG.
  326. */
  327. /* Message layout: HEADER / GUID / [LENGTH /][TIMESTAMP /] DATA */
  328. /* HEADER */
  329. sz = data->packet(data, m, c, STP_PACKET_DATA, STP_PACKET_TIMESTAMPED,
  330. 4, (u8 *)&header);
  331. if (sz <= 0)
  332. return sz;
  333. /* GUID */
  334. export_uuid(uuid, &op->node.uuid);
  335. sz = stm_data_write(data, m, c, false, uuid, sizeof(op->node.uuid));
  336. if (sz <= 0)
  337. return sz;
  338. /* [LENGTH] */
  339. if (op->node.do_len) {
  340. u16 length = count;
  341. sz = data->packet(data, m, c, STP_PACKET_DATA, 0, 2,
  342. (u8 *)&length);
  343. if (sz <= 0)
  344. return sz;
  345. }
  346. /* [TIMESTAMP] */
  347. if (header & MIPI_SYST_OPT_TS) {
  348. u64 ts = ktime_get_real_ns();
  349. sz = stm_data_write(data, m, c, false, &ts, sizeof(ts));
  350. if (sz <= 0)
  351. return sz;
  352. }
  353. /* DATA */
  354. return sys_t_write_data(data, source, m, c, false, buf, count);
  355. }
  356. static const struct stm_protocol_driver sys_t_pdrv = {
  357. .owner = THIS_MODULE,
  358. .name = "p_sys-t",
  359. .priv_sz = sizeof(struct sys_t_policy_node),
  360. .write = sys_t_write,
  361. .policy_attr = sys_t_policy_attrs,
  362. .policy_node_init = sys_t_policy_node_init,
  363. .output_open = sys_t_output_open,
  364. .output_close = sys_t_output_close,
  365. };
  366. static int sys_t_stm_init(void)
  367. {
  368. return stm_register_protocol(&sys_t_pdrv);
  369. }
  370. static void sys_t_stm_exit(void)
  371. {
  372. stm_unregister_protocol(&sys_t_pdrv);
  373. }
  374. module_init(sys_t_stm_init);
  375. module_exit(sys_t_stm_exit);
  376. MODULE_LICENSE("GPL v2");
  377. MODULE_DESCRIPTION("MIPI SyS-T STM framing protocol driver");
  378. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");