p_ost.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copied from drivers/hwtracing/stm.p-sys-t.c as of commit d69d5e83110f
  4. * ("stm class: Add MIPI SyS-T protocol support").
  5. *
  6. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  7. * Copyright (c) 2021 The Linux Foundation. All rights reserved.
  8. * Copyright (c) 2018, Intel Corporation.
  9. *
  10. * MIPI OST framing protocol for STM devices.
  11. */
  12. #include <linux/configfs.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/stm.h>
  17. #include <linux/sched/clock.h>
  18. #include "stm.h"
  19. #define OST_TOKEN_STARTSIMPLE (0x10)
  20. #define OST_VERSION_MIPI1 (0x10 << 8)
  21. #define OST_ENTITY_FTRACE (0x01 << 16)
  22. #define OST_CONTROL_PROTOCOL (0x0 << 24)
  23. #define DATA_HEADER (OST_TOKEN_STARTSIMPLE | OST_VERSION_MIPI1 | \
  24. OST_ENTITY_FTRACE | OST_CONTROL_PROTOCOL)
  25. #define STM_MAKE_VERSION(ma, mi) ((ma << 8) | mi)
  26. #define STM_HEADER_MAGIC (0x5953)
  27. static ssize_t notrace __nocfi ost_write(struct stm_data *data,
  28. struct stm_output *output, unsigned int chan,
  29. const char *buf, size_t count)
  30. {
  31. unsigned int c = output->channel + chan;
  32. unsigned int m = output->master;
  33. const unsigned char nil = 0;
  34. u32 header = DATA_HEADER;
  35. u8 trc_hdr[24];
  36. ssize_t sz;
  37. /*
  38. * STP framing rules for OST frames:
  39. * * the first packet of the OST frame is marked;
  40. * * the last packet is a FLAG.
  41. */
  42. /* Message layout: HEADER / DATA / TAIL */
  43. /* HEADER */
  44. sz = data->packet(data, m, c, STP_PACKET_DATA, STP_PACKET_MARKED,
  45. 4, (u8 *)&header);
  46. if (sz <= 0)
  47. return sz;
  48. *(uint16_t *)(trc_hdr) = STM_MAKE_VERSION(0, 3);
  49. *(uint16_t *)(trc_hdr + 2) = STM_HEADER_MAGIC;
  50. *(uint32_t *)(trc_hdr + 4) = raw_smp_processor_id();
  51. *(uint64_t *)(trc_hdr + 8) = sched_clock();
  52. *(uint64_t *)(trc_hdr + 16) = task_tgid_nr(get_current());
  53. sz = stm_data_write(data, m, c, false, trc_hdr, sizeof(trc_hdr));
  54. if (sz <= 0)
  55. return sz;
  56. /* DATA */
  57. sz = stm_data_write(data, m, c, false, buf, count);
  58. /* TAIL */
  59. if (sz > 0)
  60. data->packet(data, m, c, STP_PACKET_FLAG,
  61. STP_PACKET_TIMESTAMPED, 0, &nil);
  62. return sz;
  63. }
  64. static const struct stm_protocol_driver ost_pdrv = {
  65. .owner = THIS_MODULE,
  66. .name = "p_ost",
  67. .write = ost_write,
  68. };
  69. static int ost_stm_init(void)
  70. {
  71. return stm_register_protocol(&ost_pdrv);
  72. }
  73. static void ost_stm_exit(void)
  74. {
  75. stm_unregister_protocol(&ost_pdrv);
  76. }
  77. module_init(ost_stm_init);
  78. module_exit(ost_stm_exit);
  79. MODULE_LICENSE("GPL");
  80. MODULE_DESCRIPTION("MIPI Open System Trace STM framing protocol driver");