162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef __ASM_GENERIC_GETORDER_H 362306a36Sopenharmony_ci#define __ASM_GENERIC_GETORDER_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#ifndef __ASSEMBLY__ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/compiler.h> 862306a36Sopenharmony_ci#include <linux/log2.h> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci/** 1162306a36Sopenharmony_ci * get_order - Determine the allocation order of a memory size 1262306a36Sopenharmony_ci * @size: The size for which to get the order 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * Determine the allocation order of a particular sized block of memory. This 1562306a36Sopenharmony_ci * is on a logarithmic scale, where: 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * 0 -> 2^0 * PAGE_SIZE and below 1862306a36Sopenharmony_ci * 1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1 1962306a36Sopenharmony_ci * 2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1 2062306a36Sopenharmony_ci * 3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1 2162306a36Sopenharmony_ci * 4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1 2262306a36Sopenharmony_ci * ... 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * The order returned is used to find the smallest allocation granule required 2562306a36Sopenharmony_ci * to hold an object of the specified size. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * The result is undefined if the size is 0. 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_cistatic __always_inline __attribute_const__ int get_order(unsigned long size) 3062306a36Sopenharmony_ci{ 3162306a36Sopenharmony_ci if (__builtin_constant_p(size)) { 3262306a36Sopenharmony_ci if (!size) 3362306a36Sopenharmony_ci return BITS_PER_LONG - PAGE_SHIFT; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci if (size < (1UL << PAGE_SHIFT)) 3662306a36Sopenharmony_ci return 0; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci return ilog2((size) - 1) - PAGE_SHIFT + 1; 3962306a36Sopenharmony_ci } 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci size--; 4262306a36Sopenharmony_ci size >>= PAGE_SHIFT; 4362306a36Sopenharmony_ci#if BITS_PER_LONG == 32 4462306a36Sopenharmony_ci return fls(size); 4562306a36Sopenharmony_ci#else 4662306a36Sopenharmony_ci return fls64(size); 4762306a36Sopenharmony_ci#endif 4862306a36Sopenharmony_ci} 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */ 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci#endif /* __ASM_GENERIC_GETORDER_H */ 53