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 /** 20 * DOC: qdf_threads 21 * QCA driver framework (QDF) thread APIs 22 */ 23 24 /* Include Files */ 25 #include <qdf_threads.h> 26 #include <qdf_types.h> 27 #include <qdf_trace.h> 28 #include <linux/jiffies.h> 29 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) 30 #include <linux/sched.h> 31 #else 32 #include <linux/sched/signal.h> 33 #endif /* KERNEL_VERSION(4, 11, 0) */ 34 #include <linux/delay.h> 35 #include <linux/interrupt.h> 36 #include <linux/kthread.h> 37 #include <linux/stacktrace.h> 38 #include <qdf_defer.h> 39 #include <qdf_module.h> 40 41 /* Function declarations and documenation */ 42 43 typedef int (*qdf_thread_os_func)(void *data); 44 45 /** 46 * qdf_sleep() - sleep 47 * @ms_interval : Number of milliseconds to suspend the current thread. 48 * A value of 0 may or may not cause the current thread to yield. 49 * 50 * This function suspends the execution of the current thread 51 * until the specified time out interval elapses. 52 * 53 * Return: none 54 */ 55 void qdf_sleep(uint32_t ms_interval) 56 { 57 if (in_interrupt()) { 58 QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR, 59 "%s cannot be called from interrupt context!!!", 60 __func__); 61 return; 62 } 63 msleep_interruptible(ms_interval); 64 } 65 qdf_export_symbol(qdf_sleep); 66 67 /** 68 * qdf_sleep_us() - sleep 69 * @us_interval : Number of microseconds to suspend the current thread. 70 * A value of 0 may or may not cause the current thread to yield. 71 * 72 * This function suspends the execution of the current thread 73 * until the specified time out interval elapses. 74 * 75 * Return : none 76 */ 77 void qdf_sleep_us(uint32_t us_interval) 78 { 79 unsigned long timeout = usecs_to_jiffies(us_interval) + 1; 80 81 if (in_interrupt()) { 82 QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR, 83 "%s cannot be called from interrupt context!!!", 84 __func__); 85 return; 86 } 87 88 while (timeout && !signal_pending(current)) 89 timeout = schedule_timeout_interruptible(timeout); 90 } 91 qdf_export_symbol(qdf_sleep_us); 92 93 /** 94 * qdf_busy_wait() - busy wait 95 * @us_interval : Number of microseconds to busy wait. 96 * 97 * This function places the current thread in busy wait until the specified 98 * time out interval elapses. If the interval is greater than 50us on WM, the 99 * behaviour is undefined. 100 * 101 * Return : none 102 */ 103 void qdf_busy_wait(uint32_t us_interval) 104 { 105 udelay(us_interval); 106 } 107 qdf_export_symbol(qdf_busy_wait); 108 109 void qdf_set_user_nice(qdf_thread_t *thread, long nice) 110 { 111 set_user_nice(thread, nice); 112 } 113 qdf_export_symbol(qdf_set_user_nice); 114 115 qdf_thread_t *qdf_create_thread(int (*thread_handler)(void *data), void *data, 116 const char thread_name[]) 117 { 118 return kthread_create(thread_handler, data, thread_name); 119 } 120 qdf_export_symbol(qdf_create_thread); 121 122 static uint16_t qdf_thread_id; 123 124 qdf_thread_t *qdf_thread_run(qdf_thread_func callback, void *context) 125 { 126 struct task_struct *thread; 127 128 thread = kthread_create((qdf_thread_os_func)callback, context, 129 "qdf %u", qdf_thread_id++); 130 if (IS_ERR(thread)) 131 return NULL; 132 133 get_task_struct(thread); 134 wake_up_process(thread); 135 136 return thread; 137 } 138 qdf_export_symbol(qdf_thread_run); 139 140 QDF_STATUS qdf_thread_join(qdf_thread_t *thread) 141 { 142 QDF_STATUS status; 143 144 QDF_BUG(thread); 145 146 status = (QDF_STATUS)kthread_stop(thread); 147 put_task_struct(thread); 148 149 return status; 150 } 151 qdf_export_symbol(qdf_thread_join); 152 153 bool qdf_thread_should_stop(void) 154 { 155 return kthread_should_stop(); 156 } 157 qdf_export_symbol(qdf_thread_should_stop); 158 159 int qdf_wake_up_process(qdf_thread_t *thread) 160 { 161 return wake_up_process(thread); 162 } 163 qdf_export_symbol(qdf_wake_up_process); 164 165 /* save_stack_trace_tsk() is exported for: 166 * 1) non-arm architectures 167 * 2) arm architectures in kernel versions >=4.14 168 * 3) backported kernels defining BACKPORTED_EXPORT_SAVE_STACK_TRACE_TSK_ARM 169 */ 170 #if (defined(WLAN_HOST_ARCH_ARM) && !WLAN_HOST_ARCH_ARM) || \ 171 LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) || \ 172 defined(BACKPORTED_EXPORT_SAVE_STACK_TRACE_TSK_ARM) 173 #define QDF_PRINT_TRACE_COUNT 32 174 void qdf_print_thread_trace(qdf_thread_t *thread) 175 { 176 const int spaces = 4; 177 struct task_struct *task = thread; 178 unsigned long entries[QDF_PRINT_TRACE_COUNT] = {0}; 179 struct stack_trace trace = { 180 .nr_entries = 0, 181 .skip = 0, 182 .entries = &entries[0], 183 .max_entries = QDF_PRINT_TRACE_COUNT, 184 }; 185 186 save_stack_trace_tsk(task, &trace); 187 print_stack_trace(&trace, spaces); 188 } 189 #else 190 void qdf_print_thread_trace(qdf_thread_t *thread) { } 191 #endif /* KERNEL_VERSION(4, 14, 0) */ 192 qdf_export_symbol(qdf_print_thread_trace); 193 194 qdf_thread_t *qdf_get_current_task(void) 195 { 196 return current; 197 } 198 qdf_export_symbol(qdf_get_current_task); 199