aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/kernel/setup.c
diff options
context:
space:
mode:
authorLorenzo Pieralisi <lorenzo.pieralisi@arm.com>2013-05-16 05:32:09 -0400
committerLorenzo Pieralisi <lorenzo.pieralisi@arm.com>2013-06-20 06:22:56 -0400
commit8cf72172d739639f2699131821a3ebc291287cf2 (patch)
tree9c1d1305a6c65e8a8c39f45d06d91699f28a6959 /arch/arm/kernel/setup.c
parentfd8957a96d535a992067b2c206672323a885454a (diff)
ARM: kernel: build MPIDR hash function data structure
On ARM SMP systems, cores are identified by their MPIDR register. The MPIDR guidelines in the ARM ARM do not provide strict enforcement of MPIDR layout, only recommendations that, if followed, split the MPIDR on ARM 32 bit platforms in three affinity levels. In multi-cluster systems like big.LITTLE, if the affinity guidelines are followed, the MPIDR can not be considered an index anymore. This means that the association between logical CPU in the kernel and the HW CPU identifier becomes somewhat more complicated requiring methods like hashing to associate a given MPIDR to a CPU logical index, in order for the look-up to be carried out in an efficient and scalable way. This patch provides a function in the kernel that starting from the cpu_logical_map, implement collision-free hashing of MPIDR values by checking all significative bits of MPIDR affinity level bitfields. The hashing can then be carried out through bits shifting and ORing; the resulting hash algorithm is a collision-free though not minimal hash that can be executed with few assembly instructions. The mpidr is filtered through a mpidr mask that is built by checking all bits that toggle in the set of MPIDRs corresponding to possible CPUs. Bits that do not toggle do not carry information so they do not contribute to the resulting hash. Pseudo code: /* check all bits that toggle, so they are required */ for (i = 1, mpidr_mask = 0; i < num_possible_cpus(); i++) mpidr_mask |= (cpu_logical_map(i) ^ cpu_logical_map(0)); /* * Build shifts to be applied to aff0, aff1, aff2 values to hash the mpidr * fls() returns the last bit set in a word, 0 if none * ffs() returns the first bit set in a word, 0 if none */ fs0 = mpidr_mask[7:0] ? ffs(mpidr_mask[7:0]) - 1 : 0; fs1 = mpidr_mask[15:8] ? ffs(mpidr_mask[15:8]) - 1 : 0; fs2 = mpidr_mask[23:16] ? ffs(mpidr_mask[23:16]) - 1 : 0; ls0 = fls(mpidr_mask[7:0]); ls1 = fls(mpidr_mask[15:8]); ls2 = fls(mpidr_mask[23:16]); bits0 = ls0 - fs0; bits1 = ls1 - fs1; bits2 = ls2 - fs2; aff0_shift = fs0; aff1_shift = 8 + fs1 - bits0; aff2_shift = 16 + fs2 - (bits0 + bits1); u32 hash(u32 mpidr) { u32 l0, l1, l2; u32 mpidr_masked = mpidr & mpidr_mask; l0 = mpidr_masked & 0xff; l1 = mpidr_masked & 0xff00; l2 = mpidr_masked & 0xff0000; return (l0 >> aff0_shift | l1 >> aff1_shift | l2 >> aff2_shift); } The hashing algorithm relies on the inherent properties set in the ARM ARM recommendations for the MPIDR. Exotic configurations, where for instance the MPIDR values at a given affinity level have large holes, can end up requiring big hash tables since the compression of values that can be achieved through shifting is somewhat crippled when holes are present. Kernel warns if the number of buckets of the resulting hash table exceeds the number of possible CPUs by a factor of 4, which is a symptom of a very sparse HW MPIDR configuration. The hash algorithm is quite simple and can easily be implemented in assembly code, to be used in code paths where the kernel virtual address space is not set-up (ie cpu_resume) and instruction and data fetches are strongly ordered so code must be compact and must carry out few data accesses. Cc: Will Deacon <will.deacon@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Russell King <linux@arm.linux.org.uk> Cc: Colin Cross <ccross@android.com> Cc: Santosh Shilimkar <santosh.shilimkar@ti.com> Cc: Daniel Lezcano <daniel.lezcano@linaro.org> Cc: Amit Kucheria <amit.kucheria@linaro.org> Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Reviewed-by: Dave Martin <Dave.Martin@arm.com> Reviewed-by: Nicolas Pitre <nico@linaro.org> Tested-by: Shawn Guo <shawn.guo@linaro.org> Tested-by: Kevin Hilman <khilman@linaro.org> Tested-by: Stephen Warren <swarren@wwwdotorg.org>
Diffstat (limited to 'arch/arm/kernel/setup.c')
-rw-r--r--arch/arm/kernel/setup.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c
index ca34224f891f..9048513cbe0d 100644
--- a/arch/arm/kernel/setup.c
+++ b/arch/arm/kernel/setup.c
@@ -478,6 +478,72 @@ void __init smp_setup_processor_id(void)
478 printk(KERN_INFO "Booting Linux on physical CPU 0x%x\n", mpidr); 478 printk(KERN_INFO "Booting Linux on physical CPU 0x%x\n", mpidr);
479} 479}
480 480
481struct mpidr_hash mpidr_hash;
482#ifdef CONFIG_SMP
483/**
484 * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
485 * level in order to build a linear index from an
486 * MPIDR value. Resulting algorithm is a collision
487 * free hash carried out through shifting and ORing
488 */
489static void __init smp_build_mpidr_hash(void)
490{
491 u32 i, affinity;
492 u32 fs[3], bits[3], ls, mask = 0;
493 /*
494 * Pre-scan the list of MPIDRS and filter out bits that do
495 * not contribute to affinity levels, ie they never toggle.
496 */
497 for_each_possible_cpu(i)
498 mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
499 pr_debug("mask of set bits 0x%x\n", mask);
500 /*
501 * Find and stash the last and first bit set at all affinity levels to
502 * check how many bits are required to represent them.
503 */
504 for (i = 0; i < 3; i++) {
505 affinity = MPIDR_AFFINITY_LEVEL(mask, i);
506 /*
507 * Find the MSB bit and LSB bits position
508 * to determine how many bits are required
509 * to express the affinity level.
510 */
511 ls = fls(affinity);
512 fs[i] = affinity ? ffs(affinity) - 1 : 0;
513 bits[i] = ls - fs[i];
514 }
515 /*
516 * An index can be created from the MPIDR by isolating the
517 * significant bits at each affinity level and by shifting
518 * them in order to compress the 24 bits values space to a
519 * compressed set of values. This is equivalent to hashing
520 * the MPIDR through shifting and ORing. It is a collision free
521 * hash though not minimal since some levels might contain a number
522 * of CPUs that is not an exact power of 2 and their bit
523 * representation might contain holes, eg MPIDR[7:0] = {0x2, 0x80}.
524 */
525 mpidr_hash.shift_aff[0] = fs[0];
526 mpidr_hash.shift_aff[1] = MPIDR_LEVEL_BITS + fs[1] - bits[0];
527 mpidr_hash.shift_aff[2] = 2*MPIDR_LEVEL_BITS + fs[2] -
528 (bits[1] + bits[0]);
529 mpidr_hash.mask = mask;
530 mpidr_hash.bits = bits[2] + bits[1] + bits[0];
531 pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] mask[0x%x] bits[%u]\n",
532 mpidr_hash.shift_aff[0],
533 mpidr_hash.shift_aff[1],
534 mpidr_hash.shift_aff[2],
535 mpidr_hash.mask,
536 mpidr_hash.bits);
537 /*
538 * 4x is an arbitrary value used to warn on a hash table much bigger
539 * than expected on most systems.
540 */
541 if (mpidr_hash_size() > 4 * num_possible_cpus())
542 pr_warn("Large number of MPIDR hash buckets detected\n");
543 sync_cache_w(&mpidr_hash);
544}
545#endif
546
481static void __init setup_processor(void) 547static void __init setup_processor(void)
482{ 548{
483 struct proc_info_list *list; 549 struct proc_info_list *list;
@@ -825,6 +891,7 @@ void __init setup_arch(char **cmdline_p)
825 smp_set_ops(mdesc->smp); 891 smp_set_ops(mdesc->smp);
826 } 892 }
827 smp_init_cpus(); 893 smp_init_cpus();
894 smp_build_mpidr_hash();
828 } 895 }
829#endif 896#endif
830 897