Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm64/kernel/topology.c |
| 3 | * |
| 4 | * Copyright (C) 2011,2013,2014 Linaro Limited. |
| 5 | * |
| 6 | * Based on the arm32 version written by Vincent Guittot in turn based on |
| 7 | * 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 | |
| 14 | #include <linux/acpi.h> |
| 15 | #include <linux/arch_topology.h> |
| 16 | #include <linux/cacheinfo.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/percpu.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | |
| 20 | #include <asm/cpu.h> |
| 21 | #include <asm/cputype.h> |
| 22 | #include <asm/topology.h> |
| 23 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | void store_cpu_topology(unsigned int cpuid) |
| 25 | { |
| 26 | struct cpu_topology *cpuid_topo = &cpu_topology[cpuid]; |
| 27 | u64 mpidr; |
| 28 | |
| 29 | if (cpuid_topo->package_id != -1) |
| 30 | goto topology_populated; |
| 31 | |
| 32 | mpidr = read_cpuid_mpidr(); |
| 33 | |
| 34 | /* Uniprocessor systems can rely on default topology values */ |
| 35 | if (mpidr & MPIDR_UP_BITMASK) |
| 36 | return; |
| 37 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 38 | /* |
| 39 | * This would be the place to create cpu topology based on MPIDR. |
| 40 | * |
| 41 | * However, it cannot be trusted to depict the actual topology; some |
| 42 | * pieces of the architecture enforce an artificial cap on Aff0 values |
| 43 | * (e.g. GICv3's ICC_SGI1R_EL1 limits it to 15), leading to an |
| 44 | * artificial cycling of Aff1, Aff2 and Aff3 values. IOW, these end up |
| 45 | * having absolutely no relationship to the actual underlying system |
| 46 | * topology, and cannot be reasonably used as core / package ID. |
| 47 | * |
| 48 | * If the MT bit is set, Aff0 *could* be used to define a thread ID, but |
| 49 | * we still wouldn't be able to obtain a sane core ID. This means we |
| 50 | * need to entirely ignore MPIDR for any topology deduction. |
| 51 | */ |
| 52 | cpuid_topo->thread_id = -1; |
| 53 | cpuid_topo->core_id = cpuid; |
| 54 | cpuid_topo->package_id = cpu_to_node(cpuid); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 55 | |
| 56 | pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n", |
| 57 | cpuid, cpuid_topo->package_id, cpuid_topo->core_id, |
| 58 | cpuid_topo->thread_id, mpidr); |
| 59 | |
| 60 | topology_populated: |
| 61 | update_siblings_masks(cpuid); |
| 62 | } |
| 63 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | #ifdef CONFIG_ACPI |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 65 | static bool __init acpi_cpu_is_threaded(int cpu) |
| 66 | { |
| 67 | int is_threaded = acpi_pptt_cpu_is_thread(cpu); |
| 68 | |
| 69 | /* |
| 70 | * if the PPTT doesn't have thread information, assume a homogeneous |
| 71 | * machine and return the current CPU's thread state. |
| 72 | */ |
| 73 | if (is_threaded < 0) |
| 74 | is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK; |
| 75 | |
| 76 | return !!is_threaded; |
| 77 | } |
| 78 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | /* |
| 80 | * Propagate the topology information of the processor_topology_node tree to the |
| 81 | * cpu_topology array. |
| 82 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 83 | int __init parse_acpi_topology(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 84 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 85 | int cpu, topology_id; |
| 86 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 87 | if (acpi_disabled) |
| 88 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | |
| 90 | for_each_possible_cpu(cpu) { |
| 91 | int i, cache_id; |
| 92 | |
| 93 | topology_id = find_acpi_cpu_topology(cpu, 0); |
| 94 | if (topology_id < 0) |
| 95 | return topology_id; |
| 96 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 97 | if (acpi_cpu_is_threaded(cpu)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 98 | cpu_topology[cpu].thread_id = topology_id; |
| 99 | topology_id = find_acpi_cpu_topology(cpu, 1); |
| 100 | cpu_topology[cpu].core_id = topology_id; |
| 101 | } else { |
| 102 | cpu_topology[cpu].thread_id = -1; |
| 103 | cpu_topology[cpu].core_id = topology_id; |
| 104 | } |
| 105 | topology_id = find_acpi_cpu_topology_package(cpu); |
| 106 | cpu_topology[cpu].package_id = topology_id; |
| 107 | |
| 108 | i = acpi_find_last_cache_level(cpu); |
| 109 | |
| 110 | if (i > 0) { |
| 111 | /* |
| 112 | * this is the only part of cpu_topology that has |
| 113 | * a direct relationship with the cache topology |
| 114 | */ |
| 115 | cache_id = find_acpi_cpu_cache_topology(cpu, i); |
| 116 | if (cache_id > 0) |
| 117 | cpu_topology[cpu].llc_id = cache_id; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 125 | |