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