topology.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/init.h>
  16. #include <linux/percpu.h>
  17. #include <linux/node.h>
  18. #include <linux/nodemask.h>
  19. #include <linux/sched.h>
  20. #include <asm/cputype.h>
  21. #include <asm/topology.h>
  22. /*
  23. * cpu power scale management
  24. */
  25. /*
  26. * cpu power table
  27. * This per cpu data structure describes the relative capacity of each core.
  28. * On a heteregenous system, cores don't have the same computation capacity
  29. * and we reflect that difference in the cpu_power field so the scheduler can
  30. * take this difference into account during load balance. A per cpu structure
  31. * is preferred because each CPU updates its own cpu_power field during the
  32. * load balance except for idle cores. One idle core is selected to run the
  33. * rebalance_domains for all idle cores and the cpu_power can be updated
  34. * during this sequence.
  35. */
  36. static DEFINE_PER_CPU(unsigned long, cpu_scale);
  37. unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
  38. {
  39. return per_cpu(cpu_scale, cpu);
  40. }
  41. static void set_power_scale(unsigned int cpu, unsigned long power)
  42. {
  43. per_cpu(cpu_scale, cpu) = power;
  44. }
  45. /*
  46. * cpu topology management
  47. */
  48. #define MPIDR_SMP_BITMASK (0x3 << 30)
  49. #define MPIDR_SMP_VALUE (0x2 << 30)
  50. #define MPIDR_MT_BITMASK (0x1 << 24)
  51. /*
  52. * These masks reflect the current use of the affinity levels.
  53. * The affinity level can be up to 16 bits according to ARM ARM
  54. */
  55. #define MPIDR_LEVEL0_MASK 0x3
  56. #define MPIDR_LEVEL0_SHIFT 0
  57. #define MPIDR_LEVEL1_MASK 0xF
  58. #define MPIDR_LEVEL1_SHIFT 8
  59. #define MPIDR_LEVEL2_MASK 0xFF
  60. #define MPIDR_LEVEL2_SHIFT 16
  61. /*
  62. * cpu topology table
  63. */
  64. struct cputopo_arm cpu_topology[NR_CPUS];
  65. const struct cpumask *cpu_coregroup_mask(int cpu)
  66. {
  67. return &cpu_topology[cpu].core_sibling;
  68. }
  69. /*
  70. * store_cpu_topology is called at boot when only one cpu is running
  71. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  72. * which prevents simultaneous write access to cpu_topology array
  73. */
  74. void store_cpu_topology(unsigned int cpuid)
  75. {
  76. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  77. unsigned int mpidr;
  78. unsigned int cpu;
  79. /* If the cpu topology has been already set, just return */
  80. if (cpuid_topo->core_id != -1)
  81. return;
  82. mpidr = read_cpuid_mpidr();
  83. /* create cpu topology mapping */
  84. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  85. /*
  86. * This is a multiprocessor system
  87. * multiprocessor format & multiprocessor mode field are set
  88. */
  89. if (mpidr & MPIDR_MT_BITMASK) {
  90. /* core performance interdependency */
  91. cpuid_topo->thread_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  92. & MPIDR_LEVEL0_MASK;
  93. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  94. & MPIDR_LEVEL1_MASK;
  95. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL2_SHIFT)
  96. & MPIDR_LEVEL2_MASK;
  97. } else {
  98. /* largely independent cores */
  99. cpuid_topo->thread_id = -1;
  100. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  101. & MPIDR_LEVEL0_MASK;
  102. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  103. & MPIDR_LEVEL1_MASK;
  104. }
  105. } else {
  106. /*
  107. * This is an uniprocessor system
  108. * we are in multiprocessor format but uniprocessor system
  109. * or in the old uniprocessor format
  110. */
  111. cpuid_topo->thread_id = -1;
  112. cpuid_topo->core_id = 0;
  113. cpuid_topo->socket_id = -1;
  114. }
  115. /* update core and thread sibling masks */
  116. for_each_possible_cpu(cpu) {
  117. struct cputopo_arm *cpu_topo = &cpu_topology[cpu];
  118. if (cpuid_topo->socket_id == cpu_topo->socket_id) {
  119. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  120. if (cpu != cpuid)
  121. cpumask_set_cpu(cpu,
  122. &cpuid_topo->core_sibling);
  123. if (cpuid_topo->core_id == cpu_topo->core_id) {
  124. cpumask_set_cpu(cpuid,
  125. &cpu_topo->thread_sibling);
  126. if (cpu != cpuid)
  127. cpumask_set_cpu(cpu,
  128. &cpuid_topo->thread_sibling);
  129. }
  130. }
  131. }
  132. smp_wmb();
  133. printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  134. cpuid, cpu_topology[cpuid].thread_id,
  135. cpu_topology[cpuid].core_id,
  136. cpu_topology[cpuid].socket_id, mpidr);
  137. }
  138. /*
  139. * init_cpu_topology is called at boot when only one cpu is running
  140. * which prevent simultaneous write access to cpu_topology array
  141. */
  142. void init_cpu_topology(void)
  143. {
  144. unsigned int cpu;
  145. /* init core mask and power*/
  146. for_each_possible_cpu(cpu) {
  147. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  148. cpu_topo->thread_id = -1;
  149. cpu_topo->core_id = -1;
  150. cpu_topo->socket_id = -1;
  151. cpumask_clear(&cpu_topo->core_sibling);
  152. cpumask_clear(&cpu_topo->thread_sibling);
  153. set_power_scale(cpu, SCHED_POWER_SCALE);
  154. }
  155. smp_wmb();
  156. }