hgsl_utils.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __HGSL_UTILS_H
  7. #define __HGSL_UTILS_H
  8. #include <linux/vmalloc.h>
  9. #include <linux/mm.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/stdarg.h>
  14. enum {
  15. LOG_LEVEL_ERROR,
  16. LOG_LEVEL_WARN,
  17. LOG_LEVEL_INFO,
  18. LOG_LEVEL_DEBUG,
  19. LOG_LEVEL_NUM
  20. };
  21. #define LOGE(...) hgsl_log(LOG_LEVEL_ERROR, __func__, __LINE__, ##__VA_ARGS__)
  22. #define LOGW(...) hgsl_log(LOG_LEVEL_WARN, __func__, __LINE__, ##__VA_ARGS__)
  23. #define LOGI(...)
  24. #define LOGD(...)
  25. #define OS_UNUSED(param) ((void)param)
  26. static inline void *hgsl_malloc(size_t size)
  27. {
  28. if (size <= PAGE_SIZE)
  29. return kmalloc(size, GFP_KERNEL);
  30. return vmalloc(size);
  31. }
  32. static inline void *hgsl_zalloc(size_t size)
  33. {
  34. if (size <= PAGE_SIZE)
  35. return kzalloc(size, GFP_KERNEL);
  36. return vzalloc(size);
  37. }
  38. static inline void hgsl_free(void *ptr)
  39. {
  40. if (ptr != NULL) {
  41. if (is_vmalloc_addr(ptr))
  42. vfree(ptr);
  43. else
  44. kfree(ptr);
  45. }
  46. }
  47. static inline void hgsl_log(unsigned int level, const char * const fun,
  48. unsigned int line, const char *format, ...)
  49. {
  50. va_list arglist;
  51. char buffer[512];
  52. const char *tag = NULL;
  53. unsigned int offset = 0;
  54. struct pid *pid = task_tgid(current);
  55. struct task_struct *task = pid_task(pid, PIDTYPE_PID);
  56. switch (level) {
  57. case LOG_LEVEL_DEBUG:
  58. tag = "DEBUG";
  59. break;
  60. case LOG_LEVEL_INFO:
  61. tag = "INFO";
  62. break;
  63. case LOG_LEVEL_WARN:
  64. tag = "WARNING";
  65. break;
  66. case LOG_LEVEL_ERROR:
  67. tag = "ERROR";
  68. break;
  69. default:
  70. tag = "UNKNOWN";
  71. break;
  72. }
  73. if (task)
  74. snprintf(buffer, sizeof(buffer), "HGSL [%s] [%s:%u] [%s:%u:%u]",
  75. tag, fun, line, task->comm, task_pid_nr(task), current->pid);
  76. else
  77. snprintf(buffer, sizeof(buffer), "HGSL [%s] [%s:%u]",
  78. tag, fun, line);
  79. offset = strlen(buffer);
  80. va_start(arglist, format);
  81. vsnprintf(buffer + offset, sizeof(buffer) - offset, format, arglist);
  82. va_end(arglist);
  83. pr_err("%s\n", buffer);
  84. }
  85. #endif /* __HGSL_UTILS_H */