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 - always assert "expr" evaluates to false. 88 */ 89 #define qdf_assert_always(expr) __qdf_assert(expr) 90 91 /** 92 * qdf_target_assert_always - always 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(const struct qdf_mac_addr *mac_addr1, 332 const 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(const 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(const 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 const 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 value 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 value 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 value 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_abs - Get absolute value 505 */ 506 #define qdf_abs(x) __qdf_abs(x) 507 508 /** 509 * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order 510 */ 511 #define qdf_ntohs(x) __qdf_ntohs(x) 512 513 /** 514 * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order 515 */ 516 #define qdf_ntohl(x) __qdf_ntohl(x) 517 518 /** 519 * qdf_htons - Convert a 16-bit value from host byte order to network byte order 520 */ 521 #define qdf_htons(x) __qdf_htons(x) 522 523 /** 524 * qdf_htonl - Convert a 32-bit value from host byte order to network byte order 525 */ 526 #define qdf_htonl(x) __qdf_htonl(x) 527 528 /** 529 * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to 530 * little-endian byte order 531 * 532 * @x: value to be converted 533 */ 534 #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x) 535 536 /** 537 * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to 538 * little-endian byte order 539 * 540 * @x: value to be converted 541 */ 542 #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x) 543 544 /** 545 * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to 546 * little-endian byte order 547 * 548 * @x: value to be converted 549 */ 550 #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x) 551 552 /** 553 * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order 554 * to CPU byte order 555 * 556 * @x: value to be converted 557 */ 558 #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x) 559 560 /** 561 * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte 562 * order to CPU byte order 563 * 564 * @x: value to be converted 565 */ 566 #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x) 567 568 /** 569 * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte 570 * order to CPU byte order 571 * 572 * @x: value to be converted 573 */ 574 #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x) 575 576 /** 577 * qdf_cpu_to_be16 - Convert a 16-bit value from CPU byte order to 578 * big-endian byte order 579 * 580 * @x: value to be converted 581 */ 582 #define qdf_cpu_to_be16(x) __qdf_cpu_to_be16(x) 583 584 /** 585 * qdf_cpu_to_be32 - Convert a 32-bit value from CPU byte order to 586 * big-endian byte order 587 * 588 * @x: value to be converted 589 */ 590 #define qdf_cpu_to_be32(x) __qdf_cpu_to_be32(x) 591 592 /** 593 * qdf_cpu_to_be64 - Convert a 64-bit value from CPU byte order to 594 * big-endian byte order 595 * 596 * @x: value to be converted 597 */ 598 #define qdf_cpu_to_be64(x) __qdf_cpu_to_be64(x) 599 600 601 /** 602 * qdf_be16_to_cpu - Convert a 16-bit value from big-endian byte order 603 * to CPU byte order 604 * 605 * @x: value to be converted 606 */ 607 #define qdf_be16_to_cpu(x) __qdf_be16_to_cpu(x) 608 609 /** 610 * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order 611 * to CPU byte order 612 * 613 * @x: value to be converted 614 */ 615 #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x) 616 617 /** 618 * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order 619 * to CPU byte order 620 * 621 * @x: value to be converted 622 */ 623 #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x) 624 625 /** 626 * qdf_function - replace with the name of the current function 627 */ 628 #define qdf_function __qdf_function 629 630 /** 631 * qdf_min - minimum of two numbers 632 */ 633 #define qdf_min(a, b) __qdf_min(a, b) 634 635 /** 636 * qdf_ffz() - find first (least significant) zero bit 637 * @mask: the bitmask to check 638 * 639 * Return: The zero-based index of the first zero bit, or -1 if none are found 640 */ 641 #define qdf_ffz(mask) __qdf_ffz(mask) 642 643 /** 644 * qdf_prefetch - prefetches the cacheline for read 645 * 646 * @x: address to be prefetched 647 */ 648 #define qdf_prefetch(x) __qdf_prefetch(x) 649 650 /** 651 * qdf_get_pwr2() - get next power of 2 integer from input value 652 * @value: input value to find next power of 2 integer 653 * 654 * Get next power of 2 integer from input value 655 * 656 * Return: Power of 2 integer 657 */ 658 static inline int qdf_get_pwr2(int value) 659 { 660 int log2; 661 662 if (QDF_IS_PWR2(value)) 663 return value; 664 665 log2 = 0; 666 while (value) { 667 value >>= 1; 668 log2++; 669 } 670 return 1 << log2; 671 } 672 673 static inline 674 int qdf_get_cpu(void) 675 { 676 return __qdf_get_cpu(); 677 } 678 679 /** 680 * qdf_get_hweight8() - count num of 1's in 8-bit bitmap 681 * @value: input bitmap 682 * 683 * Count num of 1's set in the 8-bit bitmap 684 * 685 * Return: num of 1's 686 */ 687 static inline 688 unsigned int qdf_get_hweight8(unsigned int w) 689 { 690 unsigned int res = w - ((w >> 1) & 0x55); 691 res = (res & 0x33) + ((res >> 2) & 0x33); 692 return (res + (res >> 4)) & 0x0F; 693 } 694 695 /** 696 * qdf_get_hweight16() - count num of 1's in 16-bit bitmap 697 * @value: input bitmap 698 * 699 * Count num of 1's set in the 16-bit bitmap 700 * 701 * Return: num of 1's 702 */ 703 static inline 704 unsigned int qdf_get_hweight16(unsigned int w) 705 { 706 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); 707 708 res = (res & 0x3333) + ((res >> 2) & 0x3333); 709 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); 710 return (res & 0x00FF) + ((res >> 8) & 0x00FF); 711 } 712 713 /** 714 * qdf_get_hweight32() - count num of 1's in 32-bit bitmap 715 * @value: input bitmap 716 * 717 * Count num of 1's set in the 32-bit bitmap 718 * 719 * Return: num of 1's 720 */ 721 static inline 722 unsigned int qdf_get_hweight32(unsigned int w) 723 { 724 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); 725 726 res = (res & 0x33333333) + ((res >> 2) & 0x33333333); 727 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); 728 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); 729 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); 730 } 731 732 /** 733 * qdf_device_init_wakeup() - allow a device to wake up the aps system 734 * @qdf_dev: the qdf device context 735 * @enable: enable/disable the device as a wakeup source 736 * 737 * Return: 0 or errno 738 */ 739 static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable) 740 { 741 return __qdf_device_init_wakeup(qdf_dev, enable); 742 } 743 744 static inline 745 uint64_t qdf_get_totalramsize(void) 746 { 747 return __qdf_get_totalramsize(); 748 } 749 750 /** 751 * qdf_get_lower_32_bits() - get lower 32 bits from an address. 752 * @addr: address 753 * 754 * This api returns the lower 32 bits of an address. 755 * 756 * Return: lower 32 bits. 757 */ 758 static inline 759 uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr) 760 { 761 return __qdf_get_lower_32_bits(addr); 762 } 763 764 /** 765 * qdf_get_upper_32_bits() - get upper 32 bits from an address. 766 * @addr: address 767 * 768 * This api returns the upper 32 bits of an address. 769 * 770 * Return: upper 32 bits. 771 */ 772 static inline 773 uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr) 774 { 775 return __qdf_get_upper_32_bits(addr); 776 } 777 778 /** 779 * qdf_rounddown_pow_of_two() - Round down to nearest power of two 780 * @n: number to be tested 781 * 782 * Test if the input number is power of two, and return the nearest power of two 783 * 784 * Return: number rounded down to the nearest power of two 785 */ 786 static inline 787 unsigned long qdf_rounddown_pow_of_two(unsigned long n) 788 { 789 return __qdf_rounddown_pow_of_two(n); 790 } 791 792 /** 793 * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr 794 * @dev: device pointer 795 * @addr_bits: max number of bits allowed in dma address 796 * 797 * This API sets the maximum allowed number of bits in the dma address. 798 * 799 * Return: 0 - success, non zero - failure 800 */ 801 static inline 802 int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits) 803 { 804 return __qdf_set_dma_coherent_mask(dev, addr_bits); 805 } 806 807 /** 808 * qdf_do_div() - wrapper function for kernel macro(do_div). 809 * @dividend: Dividend value 810 * @divisor : Divisor value 811 * 812 * Return: Quotient 813 */ 814 static inline 815 uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor) 816 { 817 return __qdf_do_div(dividend, divisor); 818 } 819 820 /** 821 * qdf_do_div_rem() - wrapper function for kernel macro(do_div) 822 * to get remainder. 823 * @dividend: Dividend value 824 * @divisor : Divisor value 825 * 826 * Return: remainder 827 */ 828 static inline 829 uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor) 830 { 831 return __qdf_do_div_rem(dividend, divisor); 832 } 833 834 /** 835 * qdf_get_random_bytes() - returns nbytes bytes of random 836 * data 837 * 838 * Return: random bytes of data 839 */ 840 static inline 841 void qdf_get_random_bytes(void *buf, int nbytes) 842 { 843 return __qdf_get_random_bytes(buf, nbytes); 844 } 845 846 /** 847 * qdf_hex_to_bin() - QDF API to Convert hexa decimal ASCII character to 848 * unsigned integer value. 849 * @ch: hexa decimal ASCII character 850 * 851 * Return: For hexa decimal ASCII char return actual decimal value 852 * else -1 for bad input. 853 */ 854 static inline 855 int qdf_hex_to_bin(char ch) 856 { 857 return __qdf_hex_to_bin(ch); 858 } 859 860 /** 861 * qdf_hex_str_to_binary() - QDF API to Convert string of hexa decimal 862 * ASCII characters to array of unsigned integers. 863 * @dst: output array to hold converted values 864 * @src: input string of hexa decimal ASCII characters 865 * @count: size of dst string 866 * 867 * This function is used to convert string of hexa decimal characters to 868 * array of unsigned integers and caller should ensure: 869 * a) @dst, @src are not NULL, 870 * b) size of @dst should be (size of src / 2) 871 * 872 * Example 1: 873 * src = 11aa, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'a' 874 * count = (size of src / 2) = 2 875 * after conversion, dst[0] = 0x11, dst[1] = oxAA and return (0). 876 * 877 * Example 2: 878 * src = 11az, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'z' 879 * src[3] is not ASCII hexa decimal character, return negative value (-1). 880 * 881 * Return: For a string of hexa decimal ASCII characters return 0 882 * else -1 for bad input. 883 */ 884 static inline 885 int qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count) 886 { 887 return __qdf_hex_str_to_binary(dst, src, count); 888 } 889 890 /** 891 * qdf_fls() - find last set bit in a given 32 bit input 892 * @x: 32 bit mask 893 * 894 * Return: zero if the input is zero, otherwise returns the bit 895 * position of the last set bit, where the LSB is 1 and MSB is 32. 896 */ 897 static inline 898 int qdf_fls(uint32_t x) 899 { 900 return __qdf_fls(x); 901 } 902 903 /** 904 * qdf_get_smp_processor_id() - Get the current CPU id 905 * 906 * Return: current CPU id 907 */ 908 static inline int qdf_get_smp_processor_id(void) 909 { 910 return __qdf_get_smp_processor_id(); 911 } 912 #endif /*_QDF_UTIL_H*/ 913