Damien Roth | 07a196e | 2016-01-19 20:20:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 14 | #include <console/console.h> |
| 15 | #include <cpu/cpu.h> |
| 16 | #include <cpu/x86/lapic.h> |
| 17 | #include <cpu/intel/hyperthreading.h> |
| 18 | #include <device/device.h> |
| 19 | #include <pc80/mc146818rtc.h> |
| 20 | #include <smp/spinlock.h> |
Kyösti Mälkki | 8b28d50 | 2012-03-09 17:02:37 +0200 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | |
Kyösti Mälkki | 5c3f384 | 2014-05-08 15:27:15 +0300 | [diff] [blame] | 23 | #if CONFIG_PARALLEL_CPU_INIT |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 24 | #error Intel hyper-threading requires serialized CPU init |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | static int first_time = 1; |
| 28 | static int disable_siblings = !CONFIG_LOGICAL_CPUS; |
| 29 | |
Kyösti Mälkki | 9ed1456 | 2012-06-27 16:14:49 +0300 | [diff] [blame] | 30 | /* Return true if running thread does not have the smallest lapic ID |
| 31 | * within a CPU core. |
| 32 | */ |
| 33 | int intel_ht_sibling(void) |
| 34 | { |
| 35 | unsigned int core_ids, apic_ids, threads; |
| 36 | |
| 37 | apic_ids = 1; |
| 38 | if (cpuid_eax(0) >= 1) |
| 39 | apic_ids = (cpuid_ebx(1) >> 16) & 0xff; |
| 40 | if (apic_ids < 1) |
| 41 | apic_ids = 1; |
| 42 | |
| 43 | core_ids = 1; |
Kyösti Mälkki | df0fbc7 | 2012-07-04 12:02:58 +0300 | [diff] [blame] | 44 | if (cpuid_eax(0) >= 4) { |
| 45 | struct cpuid_result result; |
| 46 | result = cpuid_ext(4, 0); |
| 47 | core_ids += (result.eax >> 26) & 0x3f; |
| 48 | } |
Kyösti Mälkki | 9ed1456 | 2012-06-27 16:14:49 +0300 | [diff] [blame] | 49 | |
| 50 | threads = (apic_ids / core_ids); |
| 51 | return !!(lapicid() & (threads-1)); |
| 52 | } |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 53 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 54 | void intel_sibling_init(struct device *cpu) |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 55 | { |
| 56 | unsigned i, siblings; |
| 57 | struct cpuid_result result; |
| 58 | |
| 59 | /* On the bootstrap processor see if I want sibling cpus enabled */ |
| 60 | if (first_time) { |
| 61 | first_time = 0; |
| 62 | get_option(&disable_siblings, "hyper_threading"); |
| 63 | } |
| 64 | result = cpuid(1); |
| 65 | /* Is hyperthreading supported */ |
| 66 | if (!(result.edx & (1 << 28))) { |
| 67 | return; |
| 68 | } |
| 69 | /* See how many sibling cpus we have */ |
| 70 | siblings = (result.ebx >> 16) & 0xff; |
| 71 | if (siblings < 1) { |
| 72 | siblings = 1; |
| 73 | } |
| 74 | |
| 75 | printk(BIOS_DEBUG, "CPU: %u %d siblings\n", |
| 76 | cpu->path.apic.apic_id, |
| 77 | siblings); |
| 78 | |
| 79 | /* See if I am a sibling cpu */ |
| 80 | if (cpu->path.apic.apic_id & (siblings -1)) { |
| 81 | if (disable_siblings) { |
| 82 | cpu->enabled = 0; |
| 83 | } |
| 84 | return; |
| 85 | } |
| 86 | |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 87 | /* I am the primary CPU start up my siblings */ |
Elyes HAOUAS | cbe7464c | 2016-08-23 21:07:28 +0200 | [diff] [blame] | 88 | for (i = 1; i < siblings; i++) { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 89 | struct device_path cpu_path; |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 90 | struct device *new; |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 91 | /* Build the CPU device path */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 92 | cpu_path.type = DEVICE_PATH_APIC; |
| 93 | cpu_path.apic.apic_id = cpu->path.apic.apic_id + i; |
| 94 | |
| 95 | |
Elyes HAOUAS | d82be92 | 2016-07-28 18:58:27 +0200 | [diff] [blame] | 96 | /* Allocate new CPU device structure iff sibling CPU |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 97 | * was not in static device tree. |
| 98 | */ |
| 99 | new = alloc_find_dev(cpu->bus, &cpu_path); |
| 100 | |
| 101 | if (!new) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | printk(BIOS_DEBUG, "CPU: %u has sibling %u\n", |
| 106 | cpu->path.apic.apic_id, |
| 107 | new->path.apic.apic_id); |
| 108 | } |
| 109 | } |