ftrace.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. .link = stm_ftrace_link,
  23. .unlink = stm_ftrace_unlink,
  24. },
  25. };
  26. /**
  27. * stm_ftrace_write() - write data to STM via 'stm_ftrace' source
  28. * @buf: buffer containing the data packet
  29. * @len: length of the data packet
  30. */
  31. static void notrace __nocfi
  32. stm_ftrace_write(struct trace_export *export, const void *buf, unsigned int len)
  33. {
  34. struct stm_ftrace *stm = container_of(export, struct stm_ftrace, ftrace);
  35. /* This is called from trace system with preemption disabled */
  36. unsigned int cpu = smp_processor_id();
  37. stm_source_write(&stm->data, STM_FTRACE_CHAN + cpu, buf, len);
  38. }
  39. static int stm_ftrace_link(struct stm_source_data *data)
  40. {
  41. struct stm_ftrace *sf = container_of(data, struct stm_ftrace, data);
  42. sf->ftrace.write = stm_ftrace_write;
  43. sf->ftrace.flags = TRACE_EXPORT_FUNCTION | TRACE_EXPORT_EVENT
  44. | TRACE_EXPORT_MARKER;
  45. return register_ftrace_export(&sf->ftrace);
  46. }
  47. static void stm_ftrace_unlink(struct stm_source_data *data)
  48. {
  49. struct stm_ftrace *sf = container_of(data, struct stm_ftrace, data);
  50. unregister_ftrace_export(&sf->ftrace);
  51. }
  52. static int __init stm_ftrace_init(void)
  53. {
  54. int ret;
  55. stm_ftrace.data.nr_chans = roundup_pow_of_two(num_possible_cpus());
  56. ret = stm_source_register_device(NULL, &stm_ftrace.data);
  57. if (ret)
  58. pr_err("Failed to register stm_source - ftrace.\n");
  59. return ret;
  60. }
  61. static void __exit stm_ftrace_exit(void)
  62. {
  63. stm_source_unregister_device(&stm_ftrace.data);
  64. }
  65. module_init(stm_ftrace_init);
  66. module_exit(stm_ftrace_exit);
  67. MODULE_LICENSE("GPL v2");
  68. MODULE_DESCRIPTION("stm_ftrace driver");
  69. MODULE_AUTHOR("Chunyan Zhang <[email protected]>");