xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_util.h (revision ec13c2ea397bc11464d596ff86f4da9fdc154da8)
1 /*
2  * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: qdf_util.h
22  * This file defines utility functions.
23  */
24 
25 #ifndef _QDF_UTIL_H
26 #define _QDF_UTIL_H
27 
28 #include <i_qdf_util.h>
29 
30 #ifdef QCA_CONFIG_SMP
31 #define QDF_MAX_AVAILABLE_CPU	8
32 #else
33 #define QDF_MAX_AVAILABLE_CPU	1
34 #endif
35 
36 typedef __qdf_wait_queue_head_t qdf_wait_queue_head_t;
37 
38 /**
39  * qdf_unlikely - Compiler-dependent macro denoting code likely to execute
40  * @_expr: expression to be checked
41  */
42 #define qdf_unlikely(_expr)     __qdf_unlikely(_expr)
43 
44 /**
45  * qdf_likely - Compiler-dependent macro denoting code unlikely to execute
46  * @_expr: expression to be checked
47  */
48 #define qdf_likely(_expr)       __qdf_likely(_expr)
49 
50 /**
51  * qdf_wmb - write memory barrier.
52  */
53 #define qdf_wmb()                 __qdf_wmb()
54 
55 /**
56  * qdf_rmb - read memory barrier.
57  */
58 #define qdf_rmb()                 __qdf_rmb()
59 
60 /**
61  * qdf_mb - read + write memory barrier.
62  */
63 #define qdf_mb()                 __qdf_mb()
64 
65 /**
66  * qdf_ioread32 - read a register
67  * @offset: register address
68  */
69 #define qdf_ioread32(offset)            __qdf_ioread32(offset)
70 /**
71  * qdf_iowrite32 - write a register
72  * @offset: register address
73  * @value: value to write (32bit value)
74  */
75 #define qdf_iowrite32(offset, value)    __qdf_iowrite32(offset, value)
76 
77 /**
78  * qdf_assert - assert "expr" evaluates to false.
79  */
80 #ifdef QDF_DEBUG
81 #define qdf_assert(expr)         __qdf_assert(expr)
82 #else
83 #define qdf_assert(expr)
84 #endif /* QDF_DEBUG */
85 
86 /**
87  * qdf_assert_always - alway assert "expr" evaluates to false.
88  */
89 #define qdf_assert_always(expr)  __qdf_assert(expr)
90 
91 /**
92  * qdf_target_assert_always - alway target assert "expr" evaluates to false.
93  */
94 #define qdf_target_assert_always(expr)  __qdf_target_assert(expr)
95 
96 #define QDF_SET_PARAM(__param, __val)    ((__param) |= (1 << (__val)))
97 #define QDF_HAS_PARAM(__param, __val)    ((__param) &  (1 << (__val)))
98 #define QDF_CLEAR_PARAM(__param, __val)  ((__param) &= (~(1 << (__val))))
99 
100 /**
101  * QDF_MAX - get maximum of two values
102  * @_x: 1st argument
103  * @_y: 2nd argument
104  */
105 #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
106 
107 /**
108  * QDF_MIN - get minimum of two values
109  * @_x: 1st argument
110  * @_y: 2nd argument
111  */
112 #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
113 
114 /**
115  * QDF_IS_ADDR_BROADCAST - is mac address broadcast mac address
116  * @_a: pointer to mac address
117  */
118 #define QDF_IS_ADDR_BROADCAST(_a)  \
119 	((_a)[0] == 0xff &&        \
120 	 (_a)[1] == 0xff &&        \
121 	 (_a)[2] == 0xff &&        \
122 	 (_a)[3] == 0xff &&        \
123 	 (_a)[4] == 0xff &&        \
124 	 (_a)[5] == 0xff)
125 
126 /* Get number of bits from the index bit */
127 #define QDF_GET_BITS(_val, _index, _num_bits) \
128 		(((_val) >> (_index)) & ((1 << (_num_bits)) - 1))
129 
130 /* Set val to number of bits from the index bit */
131 #define QDF_SET_BITS(_var, _index, _num_bits, _val) do { \
132 		(_var) &= ~(((1 << (_num_bits)) - 1) << (_index)); \
133 		(_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << (_index)); \
134 		} while (0)
135 
136 #define QDF_SET_BITS64(_var, _tmp, _index, _num_bits, _val) do { \
137 		(_var) = (((_var) & 0xffffffff00000000) >> 32); \
138 		(_var) &= ~(((1 << (_num_bits)) - 1) << ((_index) - 32)); \
139 		(_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << ((_index) - 32)); \
140 		(_var) = (((_var) & 0x00000000ffffffff) << 32); \
141 		(_var) |= ((_tmp) & 0x00000000ffffffff); \
142 		} while (0)
143 
144 /* Get number of bits from the index bit supporting 64 bits */
145 #define QDF_GET_BITS64(_val, _index, _num_bits) \
146 		(((_val) >> (_index)) & ((1LLU << (_num_bits)) - 1))
147 
148 #define QDF_DECLARE_EWMA(name, factor, weight) \
149 	__QDF_DECLARE_EWMA(name, factor, weight)
150 
151 #define qdf_ewma_tx_lag __qdf_ewma_tx_lag
152 
153 #define qdf_ewma_tx_lag_init(tx_lag) \
154 	__qdf_ewma_tx_lag_init(tx_lag)
155 
156 #define qdf_ewma_tx_lag_add(tx_lag, value) \
157 	__qdf_ewma_tx_lag_add(tx_lag, value)
158 
159 #define qdf_ewma_tx_lag_read(tx_lag) \
160 	 __qdf_ewma_tx_lag_read(tx_lag)
161 
162 #define qdf_ewma_rx_rssi __qdf_ewma_rx_rssi
163 
164 #define qdf_ewma_rx_rssi_init(rx_rssi) \
165 	__qdf_ewma_rx_rssi_init(rx_rssi)
166 
167 #define qdf_ewma_rx_rssi_add(rx_rssi, value) \
168 	__qdf_ewma_rx_rssi_add(rx_rssi, value)
169 
170 #define qdf_ewma_rx_rssi_read(rx_rssi) \
171 	__qdf_ewma_rx_rssi_read(rx_rssi)
172 
173 #define QDF_CHAR_BIT 8
174 
175 /**
176  * qdf_bitmap - Define a bitmap
177  * @name: name of the bitmap
178  * @bits: num of bits in the bitmap
179  *
180  * Return: none
181  */
182 #define qdf_bitmap(name, bits) __qdf_bitmap(name, bits)
183 
184 /**
185  * qdf_set_bit() - set bit in address
186  * @nr: bit number to be set
187  * @addr: address buffer pointer
188  *
189  * Return: none
190  */
191 #define qdf_set_bit(nr, addr)    __qdf_set_bit(nr, addr)
192 
193 /**
194  * qdf_clear_bit() - clear bit in address
195  * @nr: bit number to be clear
196  * @addr: address buffer pointer
197  *
198  * Return: none
199  */
200 #define qdf_clear_bit(nr, addr)    __qdf_clear_bit(nr, addr)
201 
202 /**
203  * qdf_test_bit() - test bit position in address
204  * @nr: bit number to be tested
205  * @addr: address buffer pointer
206  *
207  * Return: none
208  */
209 #define qdf_test_bit(nr, addr)    __qdf_test_bit(nr, addr)
210 
211 /**
212  * qdf_test_and_clear_bit() - test and clear bit position in address
213  * @nr: bit number to be tested
214  * @addr: address buffer pointer
215  *
216  * Return: none
217  */
218 #define qdf_test_and_clear_bit(nr, addr)    __qdf_test_and_clear_bit(nr, addr)
219 
220 /**
221  * qdf_find_first_bit() - find first bit position in address
222  * @addr: address buffer pointer
223  * @nbits: number of bits
224  *
225  * Return: position first set bit in addr
226  */
227 #define qdf_find_first_bit(addr, nbits)    __qdf_find_first_bit(addr, nbits)
228 
229 /**
230  * qdf_bitmap_empty() - Check if bitmap is empty
231  * @addr: Address buffer pointer
232  * @nbits: Number of bits
233  *
234  * Return: True if no bit set, else false
235  */
236 #define qdf_bitmap_empty(addr, nbits)    __qdf_bitmap_empty(addr, nbits)
237 
238 /**
239  * qdf_bitmap_and() - AND operation on the bitmap
240  * @dst: Destination buffer pointer
241  * @src1: First source buffer pointer
242  * @src2: Second source buffer pointer
243  * @nbits: Number of bits
244  *
245  * Return: Bitwise and of src1 and src2 in dst
246  */
247 #define qdf_bitmap_and(dst, src1, src2, nbits) \
248 		__qdf_bitmap_and(dst, src1, src2, nbits)
249 
250 #define qdf_wait_queue_interruptible(wait_queue, condition) \
251 		__qdf_wait_queue_interruptible(wait_queue, condition)
252 
253 /**
254  * qdf_wait_queue_timeout() - wait for specified time on given condition
255  * @wait_queue: wait queue to wait on
256  * @condition: condition to wait on
257  * @timeout: timeout value in jiffies
258  *
259  * Return: 0 if condition becomes false after timeout
260  *         1 or remaining jiffies, if condition becomes true during timeout
261  */
262 #define qdf_wait_queue_timeout(wait_queue, condition, timeout) \
263 			__qdf_wait_queue_timeout(wait_queue, \
264 						condition, timeout)
265 
266 
267 #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q)
268 
269 #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q)
270 
271 /**
272  * qdf_wake_up() - wakes up sleeping waitqueue
273  * @wait_queue: wait queue, which needs wake up
274  *
275  * Return: none
276  */
277 #define qdf_wake_up(_q) __qdf_wake_up(_q)
278 
279 #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q)
280 
281 /**
282  * qdf_container_of - cast a member of a structure out to the containing
283  * structure
284  * @ptr: the pointer to the member.
285  * @type: the type of the container struct this is embedded in.
286  * @member: the name of the member within the struct.
287  */
288 #define qdf_container_of(ptr, type, member) \
289 	 __qdf_container_of(ptr, type, member)
290 
291 /**
292  * qdf_is_pwr2 - test input value is power of 2 integer
293  * @value: input integer
294  */
295 #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
296 
297 /**
298  * qdf_roundup() - roundup the input value
299  * @x: value to roundup
300  * @y: input value rounded to multiple of this
301  *
302  * Return: rounded value
303  */
304 #define qdf_roundup(x, y) __qdf_roundup(x, y)
305 
306 /**
307  * qdf_ceil() - roundup of x/y
308  * @x: dividend
309  * @y: divisor
310  *
311  * Return: rounded value
312  */
313 #define qdf_ceil(x, y) __qdf_ceil(x, y)
314 
315 /**
316  * qdf_in_interrupt - returns true if in interrupt context
317  */
318 #define qdf_in_interrupt  __qdf_in_interrupt
319 
320 /**
321  * qdf_is_macaddr_equal() - compare two QDF MacAddress
322  * @mac_addr1: Pointer to one qdf MacAddress to compare
323  * @mac_addr2: Pointer to the other qdf MacAddress to compare
324  *
325  * This function returns a bool that tells if a two QDF MacAddress'
326  * are equivalent.
327  *
328  * Return: true if the MacAddress's are equal
329  * not true if the MacAddress's are not equal
330  */
331 static inline bool qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1,
332 					struct qdf_mac_addr *mac_addr2)
333 {
334 	return __qdf_is_macaddr_equal(mac_addr1, mac_addr2);
335 }
336 
337 
338 /**
339  * qdf_is_macaddr_zero() - check for a MacAddress of all zeros.
340  * @mac_addr: pointer to the struct qdf_mac_addr to check.
341  *
342  * This function returns a bool that tells if a MacAddress is made up of
343  * all zeros.
344  *
345  * Return: true if the MacAddress is all Zeros
346  * false if the MacAddress is not all Zeros.
347  */
348 static inline bool qdf_is_macaddr_zero(struct qdf_mac_addr *mac_addr)
349 {
350 	struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INIT;
351 
352 	return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr);
353 }
354 
355 /**
356  * qdf_zero_macaddr() - zero out a MacAddress
357  * @mac_addr: pointer to the struct qdf_mac_addr to zero.
358  *
359  * This function zeros out a QDF MacAddress type.
360  *
361  * Return: none
362  */
363 static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
364 {
365 	__qdf_zero_macaddr(mac_addr);
366 }
367 
368 
369 /**
370  * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address
371  * @mac_addr1: pointer to the qdf MacAddress to check
372  *
373  * This function returns a bool that tells if a the input QDF MacAddress
374  * is a "group" address. Group addresses have the 'group address bit' turned
375  * on in the MacAddress. Group addresses are made up of Broadcast and
376  * Multicast addresses.
377  *
378  * Return: true if the input MacAddress is a Group address
379  * false if the input MacAddress is not a Group address
380  */
381 static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr)
382 {
383 	return mac_addr->bytes[0] & 0x01;
384 }
385 
386 
387 /**
388  * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
389  * @mac_addr: Pointer to the qdf MacAddress to check
390  *
391  * This function returns a bool that tells if a the input QDF MacAddress
392  * is a "broadcast" address.
393  *
394  * Return: true if the input MacAddress is a broadcast address
395  * flase if the input MacAddress is not a broadcast address
396  */
397 static inline bool qdf_is_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
398 {
399 	struct qdf_mac_addr broadcast_mac_addr = QDF_MAC_ADDR_BCAST_INIT;
400 	return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr);
401 }
402 
403 /**
404  * qdf_copy_macaddr() - copy a QDF MacAddress
405  * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination)
406  * @src_addr: pointer to the qdf MacAddress to copy FROM (the source)
407  *
408  * This function copies a QDF MacAddress into another QDF MacAddress.
409  *
410  * Return: none
411  */
412 static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr,
413 				    struct qdf_mac_addr *src_addr)
414 {
415 	*dst_addr = *src_addr;
416 }
417 
418 /**
419  * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
420  * @mac_addr: pointer to the qdf MacAddress to set to broadcast
421  *
422  * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
423  * MacAddress contains all 0xFF bytes.
424  *
425  * Return: none
426  */
427 static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
428 {
429 	__qdf_set_macaddr_broadcast(mac_addr);
430 }
431 
432 /**
433  * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
434  * endianness.
435  * @ptr: Starting address of a byte array
436  * @value: The value to assign to the byte array
437  *
438  * Caller must validate the byte array has enough space to hold the vlaue
439  *
440  * Return: The address to the byte after the assignment. This may or may not
441  * be valid. Caller to verify.
442  */
443 static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value)
444 {
445 #if defined(ANI_BIG_BYTE_ENDIAN)
446 	*(ptr) = (uint8_t) (value >> 8);
447 	*(ptr + 1) = (uint8_t) (value);
448 #else
449 	*(ptr + 1) = (uint8_t) (value >> 8);
450 	*(ptr) = (uint8_t) (value);
451 #endif
452 	return ptr + 2;
453 }
454 
455 /**
456  * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
457  * CPU's endianness.
458  * @ptr: Starting address of a byte array
459  * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to
460  * assign to this location.
461  *
462  * Caller must validate the byte array has enough space to hold the vlaue
463  *
464  * Return: The address to the byte after the assignment. This may or may not
465  * be valid. Caller to verify.
466  */
467 static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value)
468 {
469 #if defined(ANI_BIG_BYTE_ENDIAN)
470 	*value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
471 #else
472 	*value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
473 #endif
474 	return ptr + 2;
475 }
476 
477 /**
478  * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
479  * CPU's endianness.
480  * @ptr: Starting address of a byte array
481  * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to
482  * assign to this location.
483  *
484  * Caller must validate the byte array has enough space to hold the vlaue
485  *
486  * Return: The address to the byte after the assignment. This may or may not
487  * be valid. Caller to verify.
488  */
489 static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value)
490 {
491 #if defined(ANI_BIG_BYTE_ENDIAN)
492 	*value = ((uint32_t) (*(ptr) << 24) |
493 		   (uint32_t) (*(ptr + 1) << 16) |
494 		   (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
495 #else
496 	*value = ((uint32_t) (*(ptr + 3) << 24) |
497 		   (uint32_t) (*(ptr + 2) << 16) |
498 		   (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
499 #endif
500 	return ptr + 4;
501 }
502 
503 /**
504  * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order
505  */
506 #define qdf_ntohs(x)                         __qdf_ntohs(x)
507 
508 /**
509  * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order
510  */
511 #define qdf_ntohl(x)                         __qdf_ntohl(x)
512 
513 /**
514  * qdf_htons - Convert a 16-bit value from host byte order to network byte order
515  */
516 #define qdf_htons(x)                         __qdf_htons(x)
517 
518 /**
519  * qdf_htonl - Convert a 32-bit value from host byte order to network byte order
520  */
521 #define qdf_htonl(x)                         __qdf_htonl(x)
522 
523 /**
524  * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to
525  * little-endian byte order
526  *
527  * @x: value to be converted
528  */
529 #define qdf_cpu_to_le16(x)                   __qdf_cpu_to_le16(x)
530 
531 /**
532  * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to
533  * little-endian byte order
534  *
535  * @x: value to be converted
536  */
537 #define qdf_cpu_to_le32(x)                   __qdf_cpu_to_le32(x)
538 
539 /**
540  * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
541  * little-endian byte order
542  *
543  * @x: value to be converted
544  */
545 #define qdf_cpu_to_le64(x)                   __qdf_cpu_to_le64(x)
546 
547 /**
548  * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
549  * to CPU byte order
550  *
551  * @x: value to be converted
552  */
553 #define qdf_le16_to_cpu(x)                   __qdf_le16_to_cpu(x)
554 
555 /**
556  * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte
557  * order to CPU byte order
558  *
559  * @x: value to be converted
560  */
561 #define qdf_le32_to_cpu(x)                   __qdf_le32_to_cpu(x)
562 
563 /**
564  * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte
565  * order to CPU byte order
566  *
567  * @x: value to be converted
568  */
569 #define qdf_le64_to_cpu(x)                   __qdf_le64_to_cpu(x)
570 
571 /**
572  * qdf_cpu_to_be16 - Convert a 16-bit value from CPU byte order to
573  * big-endian byte order
574  *
575  * @x: value to be converted
576  */
577 #define qdf_cpu_to_be16(x)                   __qdf_cpu_to_be16(x)
578 
579 /**
580  * qdf_cpu_to_be32 - Convert a 32-bit value from CPU byte order to
581  * big-endian byte order
582  *
583  * @x: value to be converted
584  */
585 #define qdf_cpu_to_be32(x)                   __qdf_cpu_to_be32(x)
586 
587 /**
588  * qdf_cpu_to_be64 - Convert a 64-bit value from CPU byte order to
589  * big-endian byte order
590  *
591  * @x: value to be converted
592  */
593 #define qdf_cpu_to_be64(x)                   __qdf_cpu_to_be64(x)
594 
595 
596 /**
597  * qdf_be16_to_cpu - Convert a 16-bit value from big-endian byte order
598  * to CPU byte order
599  *
600  * @x: value to be converted
601  */
602 #define qdf_be16_to_cpu(x)                   __qdf_be16_to_cpu(x)
603 
604 /**
605  * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order
606  * to CPU byte order
607  *
608  * @x: value to be converted
609  */
610 #define qdf_be32_to_cpu(x)                   __qdf_be32_to_cpu(x)
611 
612 /**
613  * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order
614  * to CPU byte order
615  *
616  * @x: value to be converted
617  */
618 #define qdf_be64_to_cpu(x)                   __qdf_be64_to_cpu(x)
619 
620 /**
621  * qdf_function - replace with the name of the current function
622  */
623 #define qdf_function             __qdf_function
624 
625 /**
626  * qdf_min - minimum of two numbers
627  */
628 #define qdf_min(a, b)   __qdf_min(a, b)
629 
630 /**
631  * qdf_ffz() - find first (least significant) zero bit
632  * @mask: the bitmask to check
633  *
634  * Return: The zero-based index of the first zero bit, or -1 if none are found
635  */
636 #define qdf_ffz(mask) __qdf_ffz(mask)
637 
638 /**
639  * qdf_prefetch - prefetches the cacheline for read
640  *
641  * @x: address to be prefetched
642  */
643 #define qdf_prefetch(x)                   __qdf_prefetch(x)
644 
645 /**
646  * qdf_get_pwr2() - get next power of 2 integer from input value
647  * @value: input value to find next power of 2 integer
648  *
649  * Get next power of 2 integer from input value
650  *
651  * Return: Power of 2 integer
652  */
653 static inline int qdf_get_pwr2(int value)
654 {
655 	int log2;
656 
657 	if (QDF_IS_PWR2(value))
658 		return value;
659 
660 	log2 = 0;
661 	while (value) {
662 		value >>= 1;
663 		log2++;
664 	}
665 	return 1 << log2;
666 }
667 
668 static inline
669 int qdf_get_cpu(void)
670 {
671 	return __qdf_get_cpu();
672 }
673 
674 /**
675  * qdf_get_hweight8() - count num of 1's in 8-bit bitmap
676  * @value: input bitmap
677  *
678  * Count num of 1's set in the 8-bit bitmap
679  *
680  * Return: num of 1's
681  */
682 static inline
683 unsigned int qdf_get_hweight8(unsigned int w)
684 {
685 	unsigned int res = w - ((w >> 1) & 0x55);
686 	res = (res & 0x33) + ((res >> 2) & 0x33);
687 	return (res + (res >> 4)) & 0x0F;
688 }
689 
690 /**
691  * qdf_get_hweight16() - count num of 1's in 16-bit bitmap
692  * @value: input bitmap
693  *
694  * Count num of 1's set in the 16-bit bitmap
695  *
696  * Return: num of 1's
697  */
698 static inline
699 unsigned int qdf_get_hweight16(unsigned int w)
700 {
701 	unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
702 
703 	res = (res & 0x3333) + ((res >> 2) & 0x3333);
704 	res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
705 	return (res & 0x00FF) + ((res >> 8) & 0x00FF);
706 }
707 
708 /**
709  * qdf_get_hweight32() - count num of 1's in 32-bit bitmap
710  * @value: input bitmap
711  *
712  * Count num of 1's set in the 32-bit bitmap
713  *
714  * Return: num of 1's
715  */
716 static inline
717 unsigned int qdf_get_hweight32(unsigned int w)
718 {
719 	unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
720 
721 	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
722 	res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
723 	res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
724 	return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
725 }
726 
727 /**
728  * qdf_device_init_wakeup() - allow a device to wake up the aps system
729  * @qdf_dev: the qdf device context
730  * @enable: enable/disable the device as a wakup source
731  *
732  * Return: 0 or errno
733  */
734 static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable)
735 {
736 	return __qdf_device_init_wakeup(qdf_dev, enable);
737 }
738 
739 static inline
740 uint64_t qdf_get_totalramsize(void)
741 {
742 	return __qdf_get_totalramsize();
743 }
744 
745 /**
746  * qdf_get_lower_32_bits() - get lower 32 bits from an address.
747  * @addr: address
748  *
749  * This api returns the lower 32 bits of an address.
750  *
751  * Return: lower 32 bits.
752  */
753 static inline
754 uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr)
755 {
756 	return __qdf_get_lower_32_bits(addr);
757 }
758 
759 /**
760  * qdf_get_upper_32_bits() - get upper 32 bits from an address.
761  * @addr: address
762  *
763  * This api returns the upper 32 bits of an address.
764  *
765  * Return: upper 32 bits.
766  */
767 static inline
768 uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr)
769 {
770 	return __qdf_get_upper_32_bits(addr);
771 }
772 
773 /**
774  * qdf_rounddown_pow_of_two() - Round down to nearest power of two
775  * @n: number to be tested
776  *
777  * Test if the input number is power of two, and return the nearest power of two
778  *
779  * Return: number rounded down to the nearest power of two
780  */
781 static inline
782 unsigned long qdf_rounddown_pow_of_two(unsigned long n)
783 {
784 	return __qdf_rounddown_pow_of_two(n);
785 }
786 
787 /**
788  * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
789  * @dev: device pointer
790  * @addr_bits: max number of bits allowed in dma address
791  *
792  * This API sets the maximum allowed number of bits in the dma address.
793  *
794  * Return: 0 - success, non zero - failure
795  */
796 static inline
797 int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
798 {
799 	return __qdf_set_dma_coherent_mask(dev, addr_bits);
800 }
801 
802 /**
803  * qdf_do_div() - wrapper function for kernel macro(do_div).
804  * @dividend: Dividend value
805  * @divisor : Divisor value
806  *
807  * Return: Quotient
808  */
809 static inline
810 uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor)
811 {
812 	return __qdf_do_div(dividend, divisor);
813 }
814 
815 /**
816  * qdf_do_div_rem() - wrapper function for kernel macro(do_div)
817  *                    to get remainder.
818  * @dividend: Dividend value
819  * @divisor : Divisor value
820  *
821  * Return: remainder
822  */
823 static inline
824 uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor)
825 {
826 	return __qdf_do_div_rem(dividend, divisor);
827 }
828 
829 /**
830  * qdf_get_random_bytes() - returns nbytes bytes of random
831  * data
832  *
833  * Return: random bytes of data
834  */
835 static inline
836 void qdf_get_random_bytes(void *buf, int nbytes)
837 {
838 	return __qdf_get_random_bytes(buf, nbytes);
839 }
840 
841 /**
842  * qdf_hex_to_bin() - QDF API to Convert hexa decimal ASCII character to
843  * unsigned integer value.
844  * @ch: hexa decimal ASCII character
845  *
846  * Return: For hexa decimal ASCII char return actual decimal value
847  *	   else -1 for bad input.
848  */
849 static inline
850 int qdf_hex_to_bin(char ch)
851 {
852 	return __qdf_hex_to_bin(ch);
853 }
854 
855 /**
856  * qdf_hex_str_to_binary() - QDF API to Convert string of hexa decimal
857  * ASCII characters to array of unsigned integers.
858  * @dst: output array to hold converted values
859  * @src: input string of hexa decimal ASCII characters
860  * @count: size of dst string
861  *
862  * This function is used to convert string of hexa decimal characters to
863  * array of unsigned integers and caller should ensure:
864  *	a) @dst, @src are not NULL,
865  *	b) size of @dst should be (size of src / 2)
866  *
867  * Example 1:
868  * src = 11aa, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'a'
869  * count = (size of src / 2) = 2
870  * after conversion, dst[0] = 0x11, dst[1] = oxAA and return (0).
871  *
872  * Example 2:
873  * src = 11az, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'z'
874  * src[3] is not ASCII hexa decimal character, return negative value (-1).
875  *
876  * Return: For a string of hexa decimal ASCII characters return 0
877  *	   else -1 for bad input.
878  */
879 static inline
880 int qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count)
881 {
882 	return __qdf_hex_str_to_binary(dst, src, count);
883 }
884 
885 /**
886  * qdf_fls() - find last set bit in a given 32 bit input
887  * @x: 32 bit mask
888  *
889  * Return: zero if the input is zero, otherwise returns the bit
890  * position of the last set bit, where the LSB is 1 and MSB is 32.
891  */
892 static inline
893 int qdf_fls(uint32_t x)
894 {
895 	return __qdf_fls(x);
896 }
897 
898 /**
899  * qdf_get_smp_processor_id() - Get the current CPU id
900  *
901  * Return: current CPU id
902  */
903 static inline int qdf_get_smp_processor_id(void)
904 {
905 	return __qdf_get_smp_processor_id();
906 }
907 #endif /*_QDF_UTIL_H*/
908