vha_monitor.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/device.h>
  4. #include <linux/miscdevice.h>
  5. #include <linux/version.h>
  6. #include <uapi/vha.h>
  7. #include "vha_common.h"
  8. MODULE_LICENSE("GPL v2");
  9. MODULE_AUTHOR("Imagination");
  10. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,4,0)
  11. MODULE_IMPORT_NS(VHA_CORE);
  12. #endif
  13. #define VHA_MONITOR_NUM_PRIO 3
  14. static uint32_t cmd_count[VHA_MONITOR_NUM_PRIO] = {0};
  15. static bool check_session_switch = false;
  16. uint32_t last_session_id = 0;
  17. uint32_t last_priority = 0;
  18. bool last_last_subsegment = true;
  19. static void observe_enqueue_event(uint32_t devid, uint32_t sessionid, uint32_t cmdid, uint32_t priority)
  20. {
  21. cmd_count[priority]++;
  22. }
  23. static void observe_submitted_event(uint32_t devid, uint32_t sessionid, uint32_t cmdid, bool last_subsegment, uint32_t priority)
  24. {
  25. uint32_t i = 0;
  26. for (i = priority + 1; i < VHA_MONITOR_NUM_PRIO; i++) {
  27. if (cmd_count[i] > 0 ) {
  28. pr_err("ERROR: low priority inference submitted while higher priority one queued!!!\n");
  29. }
  30. }
  31. if (check_session_switch && last_session_id != sessionid && last_priority >= priority) {
  32. pr_err("ERROR: Session changed before the last subsegment!!!\n");
  33. }
  34. if (priority > last_priority && !last_last_subsegment) {
  35. pr_debug("vha low priority subsegment has been preempted\n");
  36. }
  37. if (last_subsegment)
  38. check_session_switch = false;
  39. else {
  40. last_session_id = sessionid;
  41. last_priority = priority;
  42. check_session_switch = true;
  43. }
  44. last_last_subsegment = last_subsegment;
  45. }
  46. static void observe_completed_event(uint32_t devid, uint32_t sessionid, uint32_t cmdid,
  47. uint64_t status, uint64_t cycles, uint64_t mem_usage, uint32_t priority)
  48. {
  49. cmd_count[priority]--;
  50. }
  51. static void observe_canceled_event(uint32_t devid, uint32_t sessionid, uint32_t cmdid, uint32_t priority)
  52. {
  53. cmd_count[priority]--;
  54. check_session_switch = false;
  55. last_last_subsegment = true;
  56. }
  57. static void observe_error_event(uint32_t devid, uint32_t sessionid, uint32_t cmdid, uint64_t status)
  58. {
  59. check_session_switch = false;
  60. last_last_subsegment = true;
  61. }
  62. static int vhamonitor_open(struct inode *inode, struct file *filep)
  63. {
  64. return nonseekable_open(inode, filep);
  65. }
  66. static int vhamonitor_release(struct inode *inode, struct file *filep)
  67. {
  68. return 0;
  69. }
  70. static const struct file_operations vhamonitor_fops = {
  71. .owner = THIS_MODULE,
  72. .open = vhamonitor_open,
  73. .release = vhamonitor_release,
  74. .llseek = no_llseek,
  75. };
  76. static struct miscdevice vhamonitormisc = {
  77. .name = VHA_SCOPE_DEV_NAME,
  78. .fops = &vhamonitor_fops,
  79. .minor = MISC_DYNAMIC_MINOR,
  80. };
  81. static int __init vhamonitor_init(void)
  82. {
  83. int ret = 0;
  84. /* register as observer of VHA events */
  85. vha_observe_event_enqueue(&observe_enqueue_event);
  86. vha_observe_event_submit(&observe_submitted_event);
  87. vha_observe_event_complete(&observe_completed_event);
  88. vha_observe_event_cancel(&observe_canceled_event);
  89. vha_observe_event_error(&observe_error_event);
  90. ret = misc_register(&vhamonitormisc);
  91. if (ret)
  92. pr_err("%s: misc_register failed\n", __func__);
  93. return ret;
  94. }
  95. static void __exit vhamonitor_exit(void)
  96. {
  97. misc_deregister(&vhamonitormisc);
  98. /* unregister for VHA events */
  99. vha_observe_event_enqueue(NULL);
  100. vha_observe_event_submit(NULL);
  101. vha_observe_event_complete(NULL);
  102. vha_observe_event_cancel(NULL);
  103. vha_observe_event_error(NULL);
  104. }
  105. module_init(vhamonitor_init);
  106. module_exit(vhamonitor_exit);