ftrace.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple kernel driver to link kernel Ftrace and an STM device
  4. * Copyright (c) 2016, Linaro Ltd.
  5. *
  6. * STM Ftrace will be registered as a trace_export.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/stm.h>
  10. #include <linux/trace.h>
  11. #define STM_FTRACE_NR_CHANNELS 1
  12. #define STM_FTRACE_CHAN 0
  13. static int stm_ftrace_link(struct stm_source_data *data);
  14. static void stm_ftrace_unlink(struct stm_source_data *data);
  15. static struct stm_ftrace {
  16. struct stm_source_data data;
  17. struct trace_export ftrace;
  18. } stm_ftrace = {
  19. .data = {
  20. .name = "ftrace",
  21. .nr_chans = STM_FTRACE_NR_CHANNELS,
  22. .type = STM_FTRACE,
  23. .link = stm_ftrace_link,
  24. .unlink = stm_ftrace_unlink,
  25. },
  26. };
  27. /**
  28. * stm_ftrace_write() - write data to STM via 'stm_ftrace' source
  29. * @buf: buffer containing the data packet
  30. * @len: length of the data packet
  31. */
  32. static void notrace
  33. stm_ftrace_write(struct trace_export *export, const void *buf, unsigned int len)
  34. {
  35. struct stm_ftrace *stm = container_of(export, struct stm_ftrace, ftrace);
  36. /* This is called from trace system with preemption disabled */
  37. unsigned int cpu = smp_processor_id();
  38. stm_source_write(&stm->data, STM_FTRACE_CHAN + cpu, buf, len);
  39. }
  40. static int stm_ftrace_link(struct stm_source_data *data)
  41. {
  42. struct stm_ftrace *sf = container_of(data, struct stm_ftrace, data);
  43. sf->ftrace.write = stm_ftrace_write;
  44. sf->ftrace.flags = TRACE_EXPORT_FUNCTION | TRACE_EXPORT_EVENT
  45. | TRACE_EXPORT_MARKER;
  46. return register_ftrace_export(&sf->ftrace);
  47. }
  48. static void stm_ftrace_unlink(struct stm_source_data *data)
  49. {
  50. struct stm_ftrace *sf = container_of(data, struct stm_ftrace, data);
  51. unregister_ftrace_export(&sf->ftrace);
  52. }
  53. static int __init stm_ftrace_init(void)
  54. {
  55. int ret;
  56. stm_ftrace.data.nr_chans = roundup_pow_of_two(num_possible_cpus());
  57. ret = stm_source_register_device(NULL, &stm_ftrace.data);
  58. if (ret)
  59. pr_err("Failed to register stm_source - ftrace.\n");
  60. return ret;
  61. }
  62. static void __exit stm_ftrace_exit(void)
  63. {
  64. stm_source_unregister_device(&stm_ftrace.data);
  65. }
  66. module_init(stm_ftrace_init);
  67. module_exit(stm_ftrace_exit);
  68. MODULE_LICENSE("GPL v2");
  69. MODULE_DESCRIPTION("stm_ftrace driver");
  70. MODULE_AUTHOR("Chunyan Zhang <zhang.chunyan@linaro.org>");