topology.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define MPIDR_SMP_BITMASK (0x3 << 30)
  23. #define MPIDR_SMP_VALUE (0x2 << 30)
  24. #define MPIDR_MT_BITMASK (0x1 << 24)
  25. /*
  26. * These masks reflect the current use of the affinity levels.
  27. * The affinity level can be up to 16 bits according to ARM ARM
  28. */
  29. #define MPIDR_LEVEL0_MASK 0x3
  30. #define MPIDR_LEVEL0_SHIFT 0
  31. #define MPIDR_LEVEL1_MASK 0xF
  32. #define MPIDR_LEVEL1_SHIFT 8
  33. #define MPIDR_LEVEL2_MASK 0xFF
  34. #define MPIDR_LEVEL2_SHIFT 16
  35. struct cputopo_arm cpu_topology[NR_CPUS];
  36. const struct cpumask *cpu_coregroup_mask(int cpu)
  37. {
  38. return &cpu_topology[cpu].core_sibling;
  39. }
  40. /*
  41. * store_cpu_topology is called at boot when only one cpu is running
  42. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  43. * which prevents simultaneous write access to cpu_topology array
  44. */
  45. void store_cpu_topology(unsigned int cpuid)
  46. {
  47. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  48. unsigned int mpidr;
  49. unsigned int cpu;
  50. /* If the cpu topology has been already set, just return */
  51. if (cpuid_topo->core_id != -1)
  52. return;
  53. mpidr = read_cpuid_mpidr();
  54. /* create cpu topology mapping */
  55. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  56. /*
  57. * This is a multiprocessor system
  58. * multiprocessor format & multiprocessor mode field are set
  59. */
  60. if (mpidr & MPIDR_MT_BITMASK) {
  61. /* core performance interdependency */
  62. cpuid_topo->thread_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  63. & MPIDR_LEVEL0_MASK;
  64. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  65. & MPIDR_LEVEL1_MASK;
  66. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL2_SHIFT)
  67. & MPIDR_LEVEL2_MASK;
  68. } else {
  69. /* largely independent cores */
  70. cpuid_topo->thread_id = -1;
  71. cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
  72. & MPIDR_LEVEL0_MASK;
  73. cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
  74. & MPIDR_LEVEL1_MASK;
  75. }
  76. } else {
  77. /*
  78. * This is an uniprocessor system
  79. * we are in multiprocessor format but uniprocessor system
  80. * or in the old uniprocessor format
  81. */
  82. cpuid_topo->thread_id = -1;
  83. cpuid_topo->core_id = 0;
  84. cpuid_topo->socket_id = -1;
  85. }
  86. /* update core and thread sibling masks */
  87. for_each_possible_cpu(cpu) {
  88. struct cputopo_arm *cpu_topo = &cpu_topology[cpu];
  89. if (cpuid_topo->socket_id == cpu_topo->socket_id) {
  90. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  91. if (cpu != cpuid)
  92. cpumask_set_cpu(cpu,
  93. &cpuid_topo->core_sibling);
  94. if (cpuid_topo->core_id == cpu_topo->core_id) {
  95. cpumask_set_cpu(cpuid,
  96. &cpu_topo->thread_sibling);
  97. if (cpu != cpuid)
  98. cpumask_set_cpu(cpu,
  99. &cpuid_topo->thread_sibling);
  100. }
  101. }
  102. }
  103. smp_wmb();
  104. printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  105. cpuid, cpu_topology[cpuid].thread_id,
  106. cpu_topology[cpuid].core_id,
  107. cpu_topology[cpuid].socket_id, mpidr);
  108. }
  109. /*
  110. * init_cpu_topology is called at boot when only one cpu is running
  111. * which prevent simultaneous write access to cpu_topology array
  112. */
  113. void init_cpu_topology(void)
  114. {
  115. unsigned int cpu;
  116. /* init core mask */
  117. for_each_possible_cpu(cpu) {
  118. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  119. cpu_topo->thread_id = -1;
  120. cpu_topo->core_id = -1;
  121. cpu_topo->socket_id = -1;
  122. cpumask_clear(&cpu_topo->core_sibling);
  123. cpumask_clear(&cpu_topo->thread_sibling);
  124. }
  125. smp_wmb();
  126. }