blob: f2f28f63779a40d4af7d019e06e346b5e618670b [file] [log] [blame]
Damien Roth07a196e2016-01-19 20:20:15 -07001/*
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 Biedermanfcd5ace2004-10-14 19:29:29 +000014#include <console/console.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000015#include <cpu/x86/lapic.h>
16#include <cpu/intel/hyperthreading.h>
17#include <device/device.h>
18#include <pc80/mc146818rtc.h>
19#include <smp/spinlock.h>
Kyösti Mälkki8b28d502012-03-09 17:02:37 +020020#include <assert.h>
21
Julius Wernercd49cce2019-03-05 16:53:33 -080022#if CONFIG(PARALLEL_CPU_INIT)
Elyes HAOUASd82be922016-07-28 18:58:27 +020023#error Intel hyper-threading requires serialized CPU init
Sven Schnelle51676b12012-07-29 19:18:03 +020024#endif
25
26static int first_time = 1;
27static int disable_siblings = !CONFIG_LOGICAL_CPUS;
28
Kyösti Mälkki9ed14562012-06-27 16:14:49 +030029/* Return true if running thread does not have the smallest lapic ID
30 * within a CPU core.
31 */
32int intel_ht_sibling(void)
33{
34 unsigned int core_ids, apic_ids, threads;
35
36 apic_ids = 1;
37 if (cpuid_eax(0) >= 1)
38 apic_ids = (cpuid_ebx(1) >> 16) & 0xff;
39 if (apic_ids < 1)
40 apic_ids = 1;
41
42 core_ids = 1;
Kyösti Mälkkidf0fbc72012-07-04 12:02:58 +030043 if (cpuid_eax(0) >= 4) {
44 struct cpuid_result result;
45 result = cpuid_ext(4, 0);
46 core_ids += (result.eax >> 26) & 0x3f;
47 }
Kyösti Mälkki9ed14562012-06-27 16:14:49 +030048
49 threads = (apic_ids / core_ids);
50 return !!(lapicid() & (threads-1));
51}
Sven Schnelle51676b12012-07-29 19:18:03 +020052
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110053void intel_sibling_init(struct device *cpu)
Sven Schnelle51676b12012-07-29 19:18:03 +020054{
Lee Leahy73a28942017-03-15 17:52:06 -070055 unsigned int i, siblings;
Sven Schnelle51676b12012-07-29 19:18:03 +020056 struct cpuid_result result;
57
58 /* On the bootstrap processor see if I want sibling cpus enabled */
59 if (first_time) {
60 first_time = 0;
61 get_option(&disable_siblings, "hyper_threading");
62 }
63 result = cpuid(1);
64 /* Is hyperthreading supported */
Lee Leahy26eeb0f2017-03-15 18:08:50 -070065 if (!(result.edx & (1 << 28)))
Sven Schnelle51676b12012-07-29 19:18:03 +020066 return;
Lee Leahy26eeb0f2017-03-15 18:08:50 -070067
Sven Schnelle51676b12012-07-29 19:18:03 +020068 /* See how many sibling cpus we have */
69 siblings = (result.ebx >> 16) & 0xff;
Lee Leahy26eeb0f2017-03-15 18:08:50 -070070 if (siblings < 1)
Sven Schnelle51676b12012-07-29 19:18:03 +020071 siblings = 1;
Sven Schnelle51676b12012-07-29 19:18:03 +020072
73 printk(BIOS_DEBUG, "CPU: %u %d siblings\n",
74 cpu->path.apic.apic_id,
75 siblings);
76
77 /* See if I am a sibling cpu */
Lee Leahy9d62e7e2017-03-15 17:40:50 -070078 if (cpu->path.apic.apic_id & (siblings - 1)) {
Lee Leahy26eeb0f2017-03-15 18:08:50 -070079 if (disable_siblings)
Sven Schnelle51676b12012-07-29 19:18:03 +020080 cpu->enabled = 0;
Sven Schnelle51676b12012-07-29 19:18:03 +020081 return;
82 }
83
Elyes HAOUASd82be922016-07-28 18:58:27 +020084 /* I am the primary CPU start up my siblings */
Elyes HAOUAScbe7464c2016-08-23 21:07:28 +020085 for (i = 1; i < siblings; i++) {
Sven Schnelle51676b12012-07-29 19:18:03 +020086 struct device_path cpu_path;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110087 struct device *new;
Elyes HAOUASd82be922016-07-28 18:58:27 +020088 /* Build the CPU device path */
Sven Schnelle51676b12012-07-29 19:18:03 +020089 cpu_path.type = DEVICE_PATH_APIC;
90 cpu_path.apic.apic_id = cpu->path.apic.apic_id + i;
91
92
Elyes HAOUASd82be922016-07-28 18:58:27 +020093 /* Allocate new CPU device structure iff sibling CPU
Sven Schnelle51676b12012-07-29 19:18:03 +020094 * was not in static device tree.
95 */
96 new = alloc_find_dev(cpu->bus, &cpu_path);
97
Lee Leahy26eeb0f2017-03-15 18:08:50 -070098 if (!new)
Sven Schnelle51676b12012-07-29 19:18:03 +020099 continue;
Sven Schnelle51676b12012-07-29 19:18:03 +0200100
101 printk(BIOS_DEBUG, "CPU: %u has sibling %u\n",
102 cpu->path.apic.apic_id,
103 new->path.apic.apic_id);
104 }
105}