qdf_threads.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /**
  19. * DOC: qdf_threads
  20. * QCA driver framework (QDF) thread APIs
  21. */
  22. /* Include Files */
  23. #include <qdf_threads.h>
  24. #include <qdf_types.h>
  25. #include <qdf_trace.h>
  26. #include <linux/jiffies.h>
  27. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
  28. #include <linux/sched.h>
  29. #else
  30. #include <linux/sched/signal.h>
  31. #endif /* KERNEL_VERSION(4, 11, 0) */
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/kthread.h>
  35. #include <linux/stacktrace.h>
  36. #include <qdf_defer.h>
  37. #include <qdf_module.h>
  38. /* Function declarations and documenation */
  39. typedef int (*qdf_thread_os_func)(void *data);
  40. /**
  41. * qdf_sleep() - sleep
  42. * @ms_interval : Number of milliseconds to suspend the current thread.
  43. * A value of 0 may or may not cause the current thread to yield.
  44. *
  45. * This function suspends the execution of the current thread
  46. * until the specified time out interval elapses.
  47. *
  48. * Return: none
  49. */
  50. void qdf_sleep(uint32_t ms_interval)
  51. {
  52. if (in_interrupt()) {
  53. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
  54. "%s cannot be called from interrupt context!!!",
  55. __func__);
  56. return;
  57. }
  58. msleep_interruptible(ms_interval);
  59. }
  60. qdf_export_symbol(qdf_sleep);
  61. /**
  62. * qdf_sleep_us() - sleep
  63. * @us_interval : Number of microseconds to suspend the current thread.
  64. * A value of 0 may or may not cause the current thread to yield.
  65. *
  66. * This function suspends the execution of the current thread
  67. * until the specified time out interval elapses.
  68. *
  69. * Return : none
  70. */
  71. void qdf_sleep_us(uint32_t us_interval)
  72. {
  73. unsigned long timeout = usecs_to_jiffies(us_interval) + 1;
  74. if (in_interrupt()) {
  75. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
  76. "%s cannot be called from interrupt context!!!",
  77. __func__);
  78. return;
  79. }
  80. while (timeout && !signal_pending(current))
  81. timeout = schedule_timeout_interruptible(timeout);
  82. }
  83. qdf_export_symbol(qdf_sleep_us);
  84. /**
  85. * qdf_busy_wait() - busy wait
  86. * @us_interval : Number of microseconds to busy wait.
  87. *
  88. * This function places the current thread in busy wait until the specified
  89. * time out interval elapses. If the interval is greater than 50us on WM, the
  90. * behaviour is undefined.
  91. *
  92. * Return : none
  93. */
  94. void qdf_busy_wait(uint32_t us_interval)
  95. {
  96. udelay(us_interval);
  97. }
  98. qdf_export_symbol(qdf_busy_wait);
  99. void qdf_set_user_nice(qdf_thread_t *thread, long nice)
  100. {
  101. set_user_nice(thread, nice);
  102. }
  103. qdf_export_symbol(qdf_set_user_nice);
  104. qdf_thread_t *qdf_create_thread(int (*thread_handler)(void *data), void *data,
  105. const char thread_name[])
  106. {
  107. return kthread_create(thread_handler, data, thread_name);
  108. }
  109. qdf_export_symbol(qdf_create_thread);
  110. static uint16_t qdf_thread_id;
  111. qdf_thread_t *qdf_thread_run(qdf_thread_func callback, void *context)
  112. {
  113. struct task_struct *thread;
  114. thread = kthread_create((qdf_thread_os_func)callback, context,
  115. "qdf %u", qdf_thread_id++);
  116. if (IS_ERR(thread))
  117. return NULL;
  118. get_task_struct(thread);
  119. wake_up_process(thread);
  120. return thread;
  121. }
  122. qdf_export_symbol(qdf_thread_run);
  123. QDF_STATUS qdf_thread_join(qdf_thread_t *thread)
  124. {
  125. QDF_STATUS status;
  126. QDF_BUG(thread);
  127. status = (QDF_STATUS)kthread_stop(thread);
  128. put_task_struct(thread);
  129. return status;
  130. }
  131. qdf_export_symbol(qdf_thread_join);
  132. bool qdf_thread_should_stop(void)
  133. {
  134. return kthread_should_stop();
  135. }
  136. qdf_export_symbol(qdf_thread_should_stop);
  137. int qdf_wake_up_process(qdf_thread_t *thread)
  138. {
  139. return wake_up_process(thread);
  140. }
  141. qdf_export_symbol(qdf_wake_up_process);
  142. /* save_stack_trace_tsk() is exported for:
  143. * 1) non-arm architectures
  144. * 2) arm architectures in kernel versions >=4.14
  145. * 3) backported kernels defining BACKPORTED_EXPORT_SAVE_STACK_TRACE_TSK_ARM
  146. */
  147. #if (defined(WLAN_HOST_ARCH_ARM) && !WLAN_HOST_ARCH_ARM) || \
  148. LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || \
  149. defined(BACKPORTED_EXPORT_SAVE_STACK_TRACE_TSK_ARM)
  150. #define QDF_PRINT_TRACE_COUNT 32
  151. void qdf_print_thread_trace(qdf_thread_t *thread)
  152. {
  153. const int spaces = 4;
  154. struct task_struct *task = thread;
  155. unsigned long entries[QDF_PRINT_TRACE_COUNT] = {0};
  156. struct stack_trace trace = {
  157. .nr_entries = 0,
  158. .skip = 0,
  159. .entries = &entries[0],
  160. .max_entries = QDF_PRINT_TRACE_COUNT,
  161. };
  162. save_stack_trace_tsk(task, &trace);
  163. print_stack_trace(&trace, spaces);
  164. }
  165. #else
  166. void qdf_print_thread_trace(qdf_thread_t *thread) { }
  167. #endif /* KERNEL_VERSION(4, 14, 0) */
  168. qdf_export_symbol(qdf_print_thread_trace);
  169. qdf_thread_t *qdf_get_current_task(void)
  170. {
  171. return current;
  172. }
  173. qdf_export_symbol(qdf_get_current_task);