topology.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * arch/arm/kernel/topology.c
  3. *
  4. * Copyright (C) 2011 Linaro Limited.
  5. * Written by: Vincent Guittot
  6. *
  7. * based on arch/sh/kernel/topology.c
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/cpu.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/percpu.h>
  18. #include <linux/node.h>
  19. #include <linux/nodemask.h>
  20. #include <linux/sched.h>
  21. #include <asm/cputype.h>
  22. #include <asm/topology.h>
  23. #define MPIDR_SMP_BITMASK (0x3 << 30)
  24. #define MPIDR_SMP_VALUE (0x2 << 30)
  25. #define MPIDR_MT_BITMASK (0x1 << 24)
  26. /*
  27. * These masks reflect the current use of the affinity levels.
  28. * The affinity level can be up to 16 bits according to ARM ARM
  29. */
  30. #define MPIDR_LEVEL0_MASK 0x3
  31. #define MPIDR_LEVEL0_SHIFT 0
  32. #define MPIDR_LEVEL1_MASK 0xF
  33. #define MPIDR_LEVEL1_SHIFT 8
  34. #define MPIDR_LEVEL2_MASK 0xFF
  35. #define MPIDR_LEVEL2_SHIFT 16
  36. struct cputopo_arm cpu_topology[NR_CPUS];
  37. EXPORT_SYMBOL_GPL(cpu_topology);
  38. const struct cpumask *cpu_coregroup_mask(int cpu)
  39. {
  40. return &cpu_topology[cpu].core_sibling;
  41. }
  42. /*
  43. * store_cpu_topology is called at boot when only one cpu is running
  44. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  45. * which prevents simultaneous write access to cpu_topology array
  46. */
  47. void store_cpu_topology(unsigned int cpuid)
  48. {
  49. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  50. unsigned int mpidr;
  51. unsigned int cpu;
  52. /* If the cpu topology has been already set, just return */
  53. if (cpuid_topo->core_id != -1)
  54. return;
  55. mpidr = read_cpuid_mpidr();
  56. /* create cpu topology mapping */
  57. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  58. /*
  59. * This is a multiprocessor system
  60. * multiprocessor format & multiprocessor mode field are set
  61. */
  62. if (mpidr & MPIDR_MT_BITMASK) {
  63. /* core performance interdependency */
  64. cpuid_topo->thread_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  65. & MPIDR_LEVEL0_MASK;
  66. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  67. & MPIDR_LEVEL1_MASK;
  68. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL2_SHIFT)
  69. & MPIDR_LEVEL2_MASK;
  70. } else {
  71. /* largely independent cores */
  72. cpuid_topo->thread_id = -1;
  73. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  74. & MPIDR_LEVEL0_MASK;
  75. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  76. & MPIDR_LEVEL1_MASK;
  77. }
  78. } else {
  79. /*
  80. * This is an uniprocessor system
  81. * we are in multiprocessor format but uniprocessor system
  82. * or in the old uniprocessor format
  83. */
  84. cpuid_topo->thread_id = -1;
  85. cpuid_topo->core_id = 0;
  86. cpuid_topo->socket_id = -1;
  87. }
  88. /* update core and thread sibling masks */
  89. for_each_possible_cpu(cpu) {
  90. struct cputopo_arm *cpu_topo = &cpu_topology[cpu];
  91. if (cpuid_topo->socket_id == cpu_topo->socket_id) {
  92. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  93. if (cpu != cpuid)
  94. cpumask_set_cpu(cpu,
  95. &cpuid_topo->core_sibling);
  96. if (cpuid_topo->core_id == cpu_topo->core_id) {
  97. cpumask_set_cpu(cpuid,
  98. &cpu_topo->thread_sibling);
  99. if (cpu != cpuid)
  100. cpumask_set_cpu(cpu,
  101. &cpuid_topo->thread_sibling);
  102. }
  103. }
  104. }
  105. smp_wmb();
  106. printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  107. cpuid, cpu_topology[cpuid].thread_id,
  108. cpu_topology[cpuid].core_id,
  109. cpu_topology[cpuid].socket_id, mpidr);
  110. }
  111. /*
  112. * init_cpu_topology is called at boot when only one cpu is running
  113. * which prevent simultaneous write access to cpu_topology array
  114. */
  115. void init_cpu_topology(void)
  116. {
  117. unsigned int cpu;
  118. /* init core mask */
  119. for_each_possible_cpu(cpu) {
  120. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  121. cpu_topo->thread_id = -1;
  122. cpu_topo->core_id = -1;
  123. cpu_topo->socket_id = -1;
  124. cpumask_clear(&cpu_topo->core_sibling);
  125. cpumask_clear(&cpu_topo->thread_sibling);
  126. }
  127. smp_wmb();
  128. }