Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / arch / x86 / kernel / itmt.c
blob1cb3ca9bba49b4fbff33f9f1926e572b9d69f991
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * itmt.c: Support Intel Turbo Boost Max Technology 3.0
5 * (C) Copyright 2016 Intel Corporation
6 * Author: Tim Chen <tim.c.chen@linux.intel.com>
8 * On platforms supporting Intel Turbo Boost Max Technology 3.0, (ITMT),
9 * the maximum turbo frequencies of some cores in a CPU package may be
10 * higher than for the other cores in the same package. In that case,
11 * better performance can be achieved by making the scheduler prefer
12 * to run tasks on the CPUs with higher max turbo frequencies.
14 * This file provides functions and data structures for enabling the
15 * scheduler to favor scheduling on cores can be boosted to a higher
16 * frequency under ITMT.
19 #include <linux/sched.h>
20 #include <linux/cpumask.h>
21 #include <linux/cpuset.h>
22 #include <linux/mutex.h>
23 #include <linux/sysctl.h>
24 #include <linux/nodemask.h>
26 static DEFINE_MUTEX(itmt_update_mutex);
27 DEFINE_PER_CPU_READ_MOSTLY(int, sched_core_priority);
29 /* Boolean to track if system has ITMT capabilities */
30 static bool __read_mostly sched_itmt_capable;
33 * Boolean to control whether we want to move processes to cpu capable
34 * of higher turbo frequency for cpus supporting Intel Turbo Boost Max
35 * Technology 3.0.
37 * It can be set via /proc/sys/kernel/sched_itmt_enabled
39 unsigned int __read_mostly sysctl_sched_itmt_enabled;
41 static int sched_itmt_update_handler(struct ctl_table *table, int write,
42 void __user *buffer, size_t *lenp,
43 loff_t *ppos)
45 unsigned int old_sysctl;
46 int ret;
48 mutex_lock(&itmt_update_mutex);
50 if (!sched_itmt_capable) {
51 mutex_unlock(&itmt_update_mutex);
52 return -EINVAL;
55 old_sysctl = sysctl_sched_itmt_enabled;
56 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
58 if (!ret && write && old_sysctl != sysctl_sched_itmt_enabled) {
59 x86_topology_update = true;
60 rebuild_sched_domains();
63 mutex_unlock(&itmt_update_mutex);
65 return ret;
68 static struct ctl_table itmt_kern_table[] = {
70 .procname = "sched_itmt_enabled",
71 .data = &sysctl_sched_itmt_enabled,
72 .maxlen = sizeof(unsigned int),
73 .mode = 0644,
74 .proc_handler = sched_itmt_update_handler,
75 .extra1 = SYSCTL_ZERO,
76 .extra2 = SYSCTL_ONE,
81 static struct ctl_table itmt_root_table[] = {
83 .procname = "kernel",
84 .mode = 0555,
85 .child = itmt_kern_table,
90 static struct ctl_table_header *itmt_sysctl_header;
92 /**
93 * sched_set_itmt_support() - Indicate platform supports ITMT
95 * This function is used by the OS to indicate to scheduler that the platform
96 * is capable of supporting the ITMT feature.
98 * The current scheme has the pstate driver detects if the system
99 * is ITMT capable and call sched_set_itmt_support.
101 * This must be done only after sched_set_itmt_core_prio
102 * has been called to set the cpus' priorities.
103 * It must not be called with cpu hot plug lock
104 * held as we need to acquire the lock to rebuild sched domains
105 * later.
107 * Return: 0 on success
109 int sched_set_itmt_support(void)
111 mutex_lock(&itmt_update_mutex);
113 if (sched_itmt_capable) {
114 mutex_unlock(&itmt_update_mutex);
115 return 0;
118 itmt_sysctl_header = register_sysctl_table(itmt_root_table);
119 if (!itmt_sysctl_header) {
120 mutex_unlock(&itmt_update_mutex);
121 return -ENOMEM;
124 sched_itmt_capable = true;
126 sysctl_sched_itmt_enabled = 1;
128 x86_topology_update = true;
129 rebuild_sched_domains();
131 mutex_unlock(&itmt_update_mutex);
133 return 0;
137 * sched_clear_itmt_support() - Revoke platform's support of ITMT
139 * This function is used by the OS to indicate that it has
140 * revoked the platform's support of ITMT feature.
142 * It must not be called with cpu hot plug lock
143 * held as we need to acquire the lock to rebuild sched domains
144 * later.
146 void sched_clear_itmt_support(void)
148 mutex_lock(&itmt_update_mutex);
150 if (!sched_itmt_capable) {
151 mutex_unlock(&itmt_update_mutex);
152 return;
154 sched_itmt_capable = false;
156 if (itmt_sysctl_header) {
157 unregister_sysctl_table(itmt_sysctl_header);
158 itmt_sysctl_header = NULL;
161 if (sysctl_sched_itmt_enabled) {
162 /* disable sched_itmt if we are no longer ITMT capable */
163 sysctl_sched_itmt_enabled = 0;
164 x86_topology_update = true;
165 rebuild_sched_domains();
168 mutex_unlock(&itmt_update_mutex);
171 int arch_asym_cpu_priority(int cpu)
173 return per_cpu(sched_core_priority, cpu);
177 * sched_set_itmt_core_prio() - Set CPU priority based on ITMT
178 * @prio: Priority of cpu core
179 * @core_cpu: The cpu number associated with the core
181 * The pstate driver will find out the max boost frequency
182 * and call this function to set a priority proportional
183 * to the max boost frequency. CPU with higher boost
184 * frequency will receive higher priority.
186 * No need to rebuild sched domain after updating
187 * the CPU priorities. The sched domains have no
188 * dependency on CPU priorities.
190 void sched_set_itmt_core_prio(int prio, int core_cpu)
192 int cpu, i = 1;
194 for_each_cpu(cpu, topology_sibling_cpumask(core_cpu)) {
195 int smt_prio;
198 * Ensure that the siblings are moved to the end
199 * of the priority chain and only used when
200 * all other high priority cpus are out of capacity.
202 smt_prio = prio * smp_num_siblings / i;
203 per_cpu(sched_core_priority, cpu) = smt_prio;
204 i++;