acpi-cpufreq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * acpi-cpufreq.c - ACPI Processor P-States Driver
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  24. *
  25. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/smp.h>
  31. #include <linux/sched.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/compiler.h>
  34. #include <linux/dmi.h>
  35. #include <linux/slab.h>
  36. #include <linux/acpi.h>
  37. #include <linux/io.h>
  38. #include <linux/delay.h>
  39. #include <linux/uaccess.h>
  40. #include <acpi/processor.h>
  41. #include <asm/msr.h>
  42. #include <asm/processor.h>
  43. #include <asm/cpufeature.h>
  44. #include "mperf.h"
  45. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  46. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  47. MODULE_LICENSE("GPL");
  48. #define PFX "acpi-cpufreq: "
  49. enum {
  50. UNDEFINED_CAPABLE = 0,
  51. SYSTEM_INTEL_MSR_CAPABLE,
  52. SYSTEM_AMD_MSR_CAPABLE,
  53. SYSTEM_IO_CAPABLE,
  54. };
  55. #define INTEL_MSR_RANGE (0xffff)
  56. #define AMD_MSR_RANGE (0x7)
  57. #define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
  58. struct acpi_cpufreq_data {
  59. struct acpi_processor_performance *acpi_data;
  60. struct cpufreq_frequency_table *freq_table;
  61. unsigned int resume;
  62. unsigned int cpu_feature;
  63. };
  64. static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
  65. /* acpi_perf_data is a pointer to percpu data. */
  66. static struct acpi_processor_performance __percpu *acpi_perf_data;
  67. static struct cpufreq_driver acpi_cpufreq_driver;
  68. static unsigned int acpi_pstate_strict;
  69. static bool boost_enabled, boost_supported;
  70. static struct msr __percpu *msrs;
  71. static bool boost_state(unsigned int cpu)
  72. {
  73. u32 lo, hi;
  74. u64 msr;
  75. switch (boot_cpu_data.x86_vendor) {
  76. case X86_VENDOR_INTEL:
  77. rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
  78. msr = lo | ((u64)hi << 32);
  79. return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
  80. case X86_VENDOR_AMD:
  81. rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
  82. msr = lo | ((u64)hi << 32);
  83. return !(msr & MSR_K7_HWCR_CPB_DIS);
  84. }
  85. return false;
  86. }
  87. static void boost_set_msrs(bool enable, const struct cpumask *cpumask)
  88. {
  89. u32 cpu;
  90. u32 msr_addr;
  91. u64 msr_mask;
  92. switch (boot_cpu_data.x86_vendor) {
  93. case X86_VENDOR_INTEL:
  94. msr_addr = MSR_IA32_MISC_ENABLE;
  95. msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
  96. break;
  97. case X86_VENDOR_AMD:
  98. msr_addr = MSR_K7_HWCR;
  99. msr_mask = MSR_K7_HWCR_CPB_DIS;
  100. break;
  101. default:
  102. return;
  103. }
  104. rdmsr_on_cpus(cpumask, msr_addr, msrs);
  105. for_each_cpu(cpu, cpumask) {
  106. struct msr *reg = per_cpu_ptr(msrs, cpu);
  107. if (enable)
  108. reg->q &= ~msr_mask;
  109. else
  110. reg->q |= msr_mask;
  111. }
  112. wrmsr_on_cpus(cpumask, msr_addr, msrs);
  113. }
  114. static ssize_t _store_boost(const char *buf, size_t count)
  115. {
  116. int ret;
  117. unsigned long val = 0;
  118. if (!boost_supported)
  119. return -EINVAL;
  120. ret = kstrtoul(buf, 10, &val);
  121. if (ret || (val > 1))
  122. return -EINVAL;
  123. if ((val && boost_enabled) || (!val && !boost_enabled))
  124. return count;
  125. get_online_cpus();
  126. boost_set_msrs(val, cpu_online_mask);
  127. put_online_cpus();
  128. boost_enabled = val;
  129. pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
  130. return count;
  131. }
  132. static ssize_t store_global_boost(struct kobject *kobj, struct attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. return _store_boost(buf, count);
  136. }
  137. static ssize_t show_global_boost(struct kobject *kobj,
  138. struct attribute *attr, char *buf)
  139. {
  140. return sprintf(buf, "%u\n", boost_enabled);
  141. }
  142. static struct global_attr global_boost = __ATTR(boost, 0644,
  143. show_global_boost,
  144. store_global_boost);
  145. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  146. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  147. size_t count)
  148. {
  149. return _store_boost(buf, count);
  150. }
  151. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  152. {
  153. return sprintf(buf, "%u\n", boost_enabled);
  154. }
  155. static struct freq_attr cpb = __ATTR(cpb, 0644, show_cpb, store_cpb);
  156. #endif
  157. static int check_est_cpu(unsigned int cpuid)
  158. {
  159. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  160. return cpu_has(cpu, X86_FEATURE_EST);
  161. }
  162. static int check_amd_hwpstate_cpu(unsigned int cpuid)
  163. {
  164. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  165. return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
  166. }
  167. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  168. {
  169. struct acpi_processor_performance *perf;
  170. int i;
  171. perf = data->acpi_data;
  172. for (i = 0; i < perf->state_count; i++) {
  173. if (value == perf->states[i].status)
  174. return data->freq_table[i].frequency;
  175. }
  176. return 0;
  177. }
  178. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  179. {
  180. int i;
  181. struct acpi_processor_performance *perf;
  182. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  183. msr &= AMD_MSR_RANGE;
  184. else
  185. msr &= INTEL_MSR_RANGE;
  186. perf = data->acpi_data;
  187. for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  188. if (msr == perf->states[data->freq_table[i].index].status)
  189. return data->freq_table[i].frequency;
  190. }
  191. return data->freq_table[0].frequency;
  192. }
  193. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  194. {
  195. switch (data->cpu_feature) {
  196. case SYSTEM_INTEL_MSR_CAPABLE:
  197. case SYSTEM_AMD_MSR_CAPABLE:
  198. return extract_msr(val, data);
  199. case SYSTEM_IO_CAPABLE:
  200. return extract_io(val, data);
  201. default:
  202. return 0;
  203. }
  204. }
  205. struct msr_addr {
  206. u32 reg;
  207. };
  208. struct io_addr {
  209. u16 port;
  210. u8 bit_width;
  211. };
  212. struct drv_cmd {
  213. unsigned int type;
  214. const struct cpumask *mask;
  215. union {
  216. struct msr_addr msr;
  217. struct io_addr io;
  218. } addr;
  219. u32 val;
  220. };
  221. /* Called via smp_call_function_single(), on the target CPU */
  222. static void do_drv_read(void *_cmd)
  223. {
  224. struct drv_cmd *cmd = _cmd;
  225. u32 h;
  226. switch (cmd->type) {
  227. case SYSTEM_INTEL_MSR_CAPABLE:
  228. case SYSTEM_AMD_MSR_CAPABLE:
  229. rdmsr(cmd->addr.msr.reg, cmd->val, h);
  230. break;
  231. case SYSTEM_IO_CAPABLE:
  232. acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
  233. &cmd->val,
  234. (u32)cmd->addr.io.bit_width);
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. /* Called via smp_call_function_many(), on the target CPUs */
  241. static void do_drv_write(void *_cmd)
  242. {
  243. struct drv_cmd *cmd = _cmd;
  244. u32 lo, hi;
  245. switch (cmd->type) {
  246. case SYSTEM_INTEL_MSR_CAPABLE:
  247. rdmsr(cmd->addr.msr.reg, lo, hi);
  248. lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
  249. wrmsr(cmd->addr.msr.reg, lo, hi);
  250. break;
  251. case SYSTEM_AMD_MSR_CAPABLE:
  252. wrmsr(cmd->addr.msr.reg, cmd->val, 0);
  253. break;
  254. case SYSTEM_IO_CAPABLE:
  255. acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
  256. cmd->val,
  257. (u32)cmd->addr.io.bit_width);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. static void drv_read(struct drv_cmd *cmd)
  264. {
  265. int err;
  266. cmd->val = 0;
  267. err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
  268. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  269. }
  270. static void drv_write(struct drv_cmd *cmd)
  271. {
  272. int this_cpu;
  273. this_cpu = get_cpu();
  274. if (cpumask_test_cpu(this_cpu, cmd->mask))
  275. do_drv_write(cmd);
  276. smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
  277. put_cpu();
  278. }
  279. static u32 get_cur_val(const struct cpumask *mask)
  280. {
  281. struct acpi_processor_performance *perf;
  282. struct drv_cmd cmd;
  283. if (unlikely(cpumask_empty(mask)))
  284. return 0;
  285. switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
  286. case SYSTEM_INTEL_MSR_CAPABLE:
  287. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  288. cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
  289. break;
  290. case SYSTEM_AMD_MSR_CAPABLE:
  291. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  292. cmd.addr.msr.reg = MSR_AMD_PERF_STATUS;
  293. break;
  294. case SYSTEM_IO_CAPABLE:
  295. cmd.type = SYSTEM_IO_CAPABLE;
  296. perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
  297. cmd.addr.io.port = perf->control_register.address;
  298. cmd.addr.io.bit_width = perf->control_register.bit_width;
  299. break;
  300. default:
  301. return 0;
  302. }
  303. cmd.mask = mask;
  304. drv_read(&cmd);
  305. pr_debug("get_cur_val = %u\n", cmd.val);
  306. return cmd.val;
  307. }
  308. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  309. {
  310. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
  311. unsigned int freq;
  312. unsigned int cached_freq;
  313. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  314. if (unlikely(data == NULL ||
  315. data->acpi_data == NULL || data->freq_table == NULL)) {
  316. return 0;
  317. }
  318. cached_freq = data->freq_table[data->acpi_data->state].frequency;
  319. freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
  320. if (freq != cached_freq) {
  321. /*
  322. * The dreaded BIOS frequency change behind our back.
  323. * Force set the frequency on next target call.
  324. */
  325. data->resume = 1;
  326. }
  327. pr_debug("cur freq = %u\n", freq);
  328. return freq;
  329. }
  330. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  331. struct acpi_cpufreq_data *data)
  332. {
  333. unsigned int cur_freq;
  334. unsigned int i;
  335. for (i = 0; i < 100; i++) {
  336. cur_freq = extract_freq(get_cur_val(mask), data);
  337. if (cur_freq == freq)
  338. return 1;
  339. udelay(10);
  340. }
  341. return 0;
  342. }
  343. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  344. unsigned int target_freq, unsigned int relation)
  345. {
  346. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  347. struct acpi_processor_performance *perf;
  348. struct cpufreq_freqs freqs;
  349. struct drv_cmd cmd;
  350. unsigned int next_state = 0; /* Index into freq_table */
  351. unsigned int next_perf_state = 0; /* Index into perf table */
  352. unsigned int i;
  353. int result = 0;
  354. pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
  355. if (unlikely(data == NULL ||
  356. data->acpi_data == NULL || data->freq_table == NULL)) {
  357. return -ENODEV;
  358. }
  359. perf = data->acpi_data;
  360. result = cpufreq_frequency_table_target(policy,
  361. data->freq_table,
  362. target_freq,
  363. relation, &next_state);
  364. if (unlikely(result)) {
  365. result = -ENODEV;
  366. goto out;
  367. }
  368. next_perf_state = data->freq_table[next_state].index;
  369. if (perf->state == next_perf_state) {
  370. if (unlikely(data->resume)) {
  371. pr_debug("Called after resume, resetting to P%d\n",
  372. next_perf_state);
  373. data->resume = 0;
  374. } else {
  375. pr_debug("Already at target state (P%d)\n",
  376. next_perf_state);
  377. goto out;
  378. }
  379. }
  380. switch (data->cpu_feature) {
  381. case SYSTEM_INTEL_MSR_CAPABLE:
  382. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  383. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  384. cmd.val = (u32) perf->states[next_perf_state].control;
  385. break;
  386. case SYSTEM_AMD_MSR_CAPABLE:
  387. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  388. cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
  389. cmd.val = (u32) perf->states[next_perf_state].control;
  390. break;
  391. case SYSTEM_IO_CAPABLE:
  392. cmd.type = SYSTEM_IO_CAPABLE;
  393. cmd.addr.io.port = perf->control_register.address;
  394. cmd.addr.io.bit_width = perf->control_register.bit_width;
  395. cmd.val = (u32) perf->states[next_perf_state].control;
  396. break;
  397. default:
  398. result = -ENODEV;
  399. goto out;
  400. }
  401. /* cpufreq holds the hotplug lock, so we are safe from here on */
  402. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  403. cmd.mask = policy->cpus;
  404. else
  405. cmd.mask = cpumask_of(policy->cpu);
  406. freqs.old = perf->states[perf->state].core_frequency * 1000;
  407. freqs.new = data->freq_table[next_state].frequency;
  408. for_each_cpu(i, policy->cpus) {
  409. freqs.cpu = i;
  410. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  411. }
  412. drv_write(&cmd);
  413. if (acpi_pstate_strict) {
  414. if (!check_freqs(cmd.mask, freqs.new, data)) {
  415. pr_debug("acpi_cpufreq_target failed (%d)\n",
  416. policy->cpu);
  417. result = -EAGAIN;
  418. goto out;
  419. }
  420. }
  421. for_each_cpu(i, policy->cpus) {
  422. freqs.cpu = i;
  423. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  424. }
  425. perf->state = next_perf_state;
  426. out:
  427. return result;
  428. }
  429. static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
  430. {
  431. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  432. pr_debug("acpi_cpufreq_verify\n");
  433. return cpufreq_frequency_table_verify(policy, data->freq_table);
  434. }
  435. static unsigned long
  436. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  437. {
  438. struct acpi_processor_performance *perf = data->acpi_data;
  439. if (cpu_khz) {
  440. /* search the closest match to cpu_khz */
  441. unsigned int i;
  442. unsigned long freq;
  443. unsigned long freqn = perf->states[0].core_frequency * 1000;
  444. for (i = 0; i < (perf->state_count-1); i++) {
  445. freq = freqn;
  446. freqn = perf->states[i+1].core_frequency * 1000;
  447. if ((2 * cpu_khz) > (freqn + freq)) {
  448. perf->state = i;
  449. return freq;
  450. }
  451. }
  452. perf->state = perf->state_count-1;
  453. return freqn;
  454. } else {
  455. /* assume CPU is at P0... */
  456. perf->state = 0;
  457. return perf->states[0].core_frequency * 1000;
  458. }
  459. }
  460. static void free_acpi_perf_data(void)
  461. {
  462. unsigned int i;
  463. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  464. for_each_possible_cpu(i)
  465. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  466. ->shared_cpu_map);
  467. free_percpu(acpi_perf_data);
  468. }
  469. static int boost_notify(struct notifier_block *nb, unsigned long action,
  470. void *hcpu)
  471. {
  472. unsigned cpu = (long)hcpu;
  473. const struct cpumask *cpumask;
  474. cpumask = get_cpu_mask(cpu);
  475. /*
  476. * Clear the boost-disable bit on the CPU_DOWN path so that
  477. * this cpu cannot block the remaining ones from boosting. On
  478. * the CPU_UP path we simply keep the boost-disable flag in
  479. * sync with the current global state.
  480. */
  481. switch (action) {
  482. case CPU_UP_PREPARE:
  483. case CPU_UP_PREPARE_FROZEN:
  484. boost_set_msrs(boost_enabled, cpumask);
  485. break;
  486. case CPU_DOWN_PREPARE:
  487. case CPU_DOWN_PREPARE_FROZEN:
  488. boost_set_msrs(1, cpumask);
  489. break;
  490. default:
  491. break;
  492. }
  493. return NOTIFY_OK;
  494. }
  495. static struct notifier_block boost_nb = {
  496. .notifier_call = boost_notify,
  497. };
  498. /*
  499. * acpi_cpufreq_early_init - initialize ACPI P-States library
  500. *
  501. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  502. * in order to determine correct frequency and voltage pairings. We can
  503. * do _PDC and _PSD and find out the processor dependency for the
  504. * actual init that will happen later...
  505. */
  506. static int __init acpi_cpufreq_early_init(void)
  507. {
  508. unsigned int i;
  509. pr_debug("acpi_cpufreq_early_init\n");
  510. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  511. if (!acpi_perf_data) {
  512. pr_debug("Memory allocation error for acpi_perf_data.\n");
  513. return -ENOMEM;
  514. }
  515. for_each_possible_cpu(i) {
  516. if (!zalloc_cpumask_var_node(
  517. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  518. GFP_KERNEL, cpu_to_node(i))) {
  519. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  520. free_acpi_perf_data();
  521. return -ENOMEM;
  522. }
  523. }
  524. /* Do initialization in ACPI core */
  525. acpi_processor_preregister_performance(acpi_perf_data);
  526. return 0;
  527. }
  528. #ifdef CONFIG_SMP
  529. /*
  530. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  531. * or do it in BIOS firmware and won't inform about it to OS. If not
  532. * detected, this has a side effect of making CPU run at a different speed
  533. * than OS intended it to run at. Detect it and handle it cleanly.
  534. */
  535. static int bios_with_sw_any_bug;
  536. static int sw_any_bug_found(const struct dmi_system_id *d)
  537. {
  538. bios_with_sw_any_bug = 1;
  539. return 0;
  540. }
  541. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  542. {
  543. .callback = sw_any_bug_found,
  544. .ident = "Supermicro Server X6DLP",
  545. .matches = {
  546. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  547. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  548. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  549. },
  550. },
  551. { }
  552. };
  553. static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
  554. {
  555. /* Intel Xeon Processor 7100 Series Specification Update
  556. * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
  557. * AL30: A Machine Check Exception (MCE) Occurring during an
  558. * Enhanced Intel SpeedStep Technology Ratio Change May Cause
  559. * Both Processor Cores to Lock Up. */
  560. if (c->x86_vendor == X86_VENDOR_INTEL) {
  561. if ((c->x86 == 15) &&
  562. (c->x86_model == 6) &&
  563. (c->x86_mask == 8)) {
  564. printk(KERN_INFO "acpi-cpufreq: Intel(R) "
  565. "Xeon(R) 7100 Errata AL30, processors may "
  566. "lock up on frequency changes: disabling "
  567. "acpi-cpufreq.\n");
  568. return -ENODEV;
  569. }
  570. }
  571. return 0;
  572. }
  573. #endif
  574. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  575. {
  576. unsigned int i;
  577. unsigned int valid_states = 0;
  578. unsigned int cpu = policy->cpu;
  579. struct acpi_cpufreq_data *data;
  580. unsigned int result = 0;
  581. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  582. struct acpi_processor_performance *perf;
  583. #ifdef CONFIG_SMP
  584. static int blacklisted;
  585. #endif
  586. pr_debug("acpi_cpufreq_cpu_init\n");
  587. #ifdef CONFIG_SMP
  588. if (blacklisted)
  589. return blacklisted;
  590. blacklisted = acpi_cpufreq_blacklist(c);
  591. if (blacklisted)
  592. return blacklisted;
  593. #endif
  594. data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
  595. if (!data)
  596. return -ENOMEM;
  597. data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
  598. per_cpu(acfreq_data, cpu) = data;
  599. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  600. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  601. result = acpi_processor_register_performance(data->acpi_data, cpu);
  602. if (result)
  603. goto err_free;
  604. perf = data->acpi_data;
  605. policy->shared_type = perf->shared_type;
  606. /*
  607. * Will let policy->cpus know about dependency only when software
  608. * coordination is required.
  609. */
  610. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  611. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  612. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  613. }
  614. #ifdef CONFIG_SMP
  615. dmi_check_system(sw_any_bug_dmi_table);
  616. if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
  617. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  618. cpumask_copy(policy->cpus, cpu_core_mask(cpu));
  619. }
  620. if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
  621. cpumask_clear(policy->cpus);
  622. cpumask_set_cpu(cpu, policy->cpus);
  623. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  624. pr_info_once(PFX "overriding BIOS provided _PSD data\n");
  625. }
  626. #endif
  627. /* capability check */
  628. if (perf->state_count <= 1) {
  629. pr_debug("No P-States\n");
  630. result = -ENODEV;
  631. goto err_unreg;
  632. }
  633. if (perf->control_register.space_id != perf->status_register.space_id) {
  634. result = -ENODEV;
  635. goto err_unreg;
  636. }
  637. switch (perf->control_register.space_id) {
  638. case ACPI_ADR_SPACE_SYSTEM_IO:
  639. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  640. boot_cpu_data.x86 == 0xf) {
  641. pr_debug("AMD K8 systems must use native drivers.\n");
  642. result = -ENODEV;
  643. goto err_unreg;
  644. }
  645. pr_debug("SYSTEM IO addr space\n");
  646. data->cpu_feature = SYSTEM_IO_CAPABLE;
  647. break;
  648. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  649. pr_debug("HARDWARE addr space\n");
  650. if (check_est_cpu(cpu)) {
  651. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  652. break;
  653. }
  654. if (check_amd_hwpstate_cpu(cpu)) {
  655. data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
  656. break;
  657. }
  658. result = -ENODEV;
  659. goto err_unreg;
  660. default:
  661. pr_debug("Unknown addr space %d\n",
  662. (u32) (perf->control_register.space_id));
  663. result = -ENODEV;
  664. goto err_unreg;
  665. }
  666. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  667. (perf->state_count+1), GFP_KERNEL);
  668. if (!data->freq_table) {
  669. result = -ENOMEM;
  670. goto err_unreg;
  671. }
  672. /* detect transition latency */
  673. policy->cpuinfo.transition_latency = 0;
  674. for (i = 0; i < perf->state_count; i++) {
  675. if ((perf->states[i].transition_latency * 1000) >
  676. policy->cpuinfo.transition_latency)
  677. policy->cpuinfo.transition_latency =
  678. perf->states[i].transition_latency * 1000;
  679. }
  680. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  681. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  682. policy->cpuinfo.transition_latency > 20 * 1000) {
  683. policy->cpuinfo.transition_latency = 20 * 1000;
  684. printk_once(KERN_INFO
  685. "P-state transition latency capped at 20 uS\n");
  686. }
  687. /* table init */
  688. for (i = 0; i < perf->state_count; i++) {
  689. if (i > 0 && perf->states[i].core_frequency >=
  690. data->freq_table[valid_states-1].frequency / 1000)
  691. continue;
  692. data->freq_table[valid_states].index = i;
  693. data->freq_table[valid_states].frequency =
  694. perf->states[i].core_frequency * 1000;
  695. valid_states++;
  696. }
  697. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  698. perf->state = 0;
  699. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  700. if (result)
  701. goto err_freqfree;
  702. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  703. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  704. switch (perf->control_register.space_id) {
  705. case ACPI_ADR_SPACE_SYSTEM_IO:
  706. /* Current speed is unknown and not detectable by IO port */
  707. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  708. break;
  709. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  710. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  711. policy->cur = get_cur_freq_on_cpu(cpu);
  712. break;
  713. default:
  714. break;
  715. }
  716. /* notify BIOS that we exist */
  717. acpi_processor_notify_smm(THIS_MODULE);
  718. /* Check for APERF/MPERF support in hardware */
  719. if (boot_cpu_has(X86_FEATURE_APERFMPERF))
  720. acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
  721. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  722. for (i = 0; i < perf->state_count; i++)
  723. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  724. (i == perf->state ? '*' : ' '), i,
  725. (u32) perf->states[i].core_frequency,
  726. (u32) perf->states[i].power,
  727. (u32) perf->states[i].transition_latency);
  728. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  729. /*
  730. * the first call to ->target() should result in us actually
  731. * writing something to the appropriate registers.
  732. */
  733. data->resume = 1;
  734. return result;
  735. err_freqfree:
  736. kfree(data->freq_table);
  737. err_unreg:
  738. acpi_processor_unregister_performance(perf, cpu);
  739. err_free:
  740. kfree(data);
  741. per_cpu(acfreq_data, cpu) = NULL;
  742. return result;
  743. }
  744. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  745. {
  746. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  747. pr_debug("acpi_cpufreq_cpu_exit\n");
  748. if (data) {
  749. cpufreq_frequency_table_put_attr(policy->cpu);
  750. per_cpu(acfreq_data, policy->cpu) = NULL;
  751. acpi_processor_unregister_performance(data->acpi_data,
  752. policy->cpu);
  753. kfree(data->freq_table);
  754. kfree(data);
  755. }
  756. return 0;
  757. }
  758. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  759. {
  760. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  761. pr_debug("acpi_cpufreq_resume\n");
  762. data->resume = 1;
  763. return 0;
  764. }
  765. static struct freq_attr *acpi_cpufreq_attr[] = {
  766. &cpufreq_freq_attr_scaling_available_freqs,
  767. NULL, /* this is a placeholder for cpb, do not remove */
  768. NULL,
  769. };
  770. static struct cpufreq_driver acpi_cpufreq_driver = {
  771. .verify = acpi_cpufreq_verify,
  772. .target = acpi_cpufreq_target,
  773. .bios_limit = acpi_processor_get_bios_limit,
  774. .init = acpi_cpufreq_cpu_init,
  775. .exit = acpi_cpufreq_cpu_exit,
  776. .resume = acpi_cpufreq_resume,
  777. .name = "acpi-cpufreq",
  778. .owner = THIS_MODULE,
  779. .attr = acpi_cpufreq_attr,
  780. };
  781. static void __init acpi_cpufreq_boost_init(void)
  782. {
  783. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  784. msrs = msrs_alloc();
  785. if (!msrs)
  786. return;
  787. boost_supported = true;
  788. boost_enabled = boost_state(0);
  789. get_online_cpus();
  790. /* Force all MSRs to the same value */
  791. boost_set_msrs(boost_enabled, cpu_online_mask);
  792. register_cpu_notifier(&boost_nb);
  793. put_online_cpus();
  794. } else
  795. global_boost.attr.mode = 0444;
  796. /* We create the boost file in any case, though for systems without
  797. * hardware support it will be read-only and hardwired to return 0.
  798. */
  799. if (sysfs_create_file(cpufreq_global_kobject, &(global_boost.attr)))
  800. pr_warn(PFX "could not register global boost sysfs file\n");
  801. else
  802. pr_debug("registered global boost sysfs file\n");
  803. }
  804. static void __exit acpi_cpufreq_boost_exit(void)
  805. {
  806. sysfs_remove_file(cpufreq_global_kobject, &(global_boost.attr));
  807. if (msrs) {
  808. unregister_cpu_notifier(&boost_nb);
  809. msrs_free(msrs);
  810. msrs = NULL;
  811. }
  812. }
  813. static int __init acpi_cpufreq_init(void)
  814. {
  815. int ret;
  816. if (acpi_disabled)
  817. return 0;
  818. pr_debug("acpi_cpufreq_init\n");
  819. ret = acpi_cpufreq_early_init();
  820. if (ret)
  821. return ret;
  822. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  823. /* this is a sysfs file with a strange name and an even stranger
  824. * semantic - per CPU instantiation, but system global effect.
  825. * Lets enable it only on AMD CPUs for compatibility reasons and
  826. * only if configured. This is considered legacy code, which
  827. * will probably be removed at some point in the future.
  828. */
  829. if (check_amd_hwpstate_cpu(0)) {
  830. struct freq_attr **iter;
  831. pr_debug("adding sysfs entry for cpb\n");
  832. for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
  833. ;
  834. /* make sure there is a terminator behind it */
  835. if (iter[1] == NULL)
  836. *iter = &cpb;
  837. }
  838. #endif
  839. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  840. if (ret)
  841. free_acpi_perf_data();
  842. else
  843. acpi_cpufreq_boost_init();
  844. return ret;
  845. }
  846. static void __exit acpi_cpufreq_exit(void)
  847. {
  848. pr_debug("acpi_cpufreq_exit\n");
  849. acpi_cpufreq_boost_exit();
  850. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  851. free_acpi_perf_data();
  852. }
  853. module_param(acpi_pstate_strict, uint, 0644);
  854. MODULE_PARM_DESC(acpi_pstate_strict,
  855. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  856. "performed during frequency changes.");
  857. late_initcall(acpi_cpufreq_init);
  858. module_exit(acpi_cpufreq_exit);
  859. static const struct x86_cpu_id acpi_cpufreq_ids[] = {
  860. X86_FEATURE_MATCH(X86_FEATURE_ACPI),
  861. X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
  862. {}
  863. };
  864. MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
  865. MODULE_ALIAS("acpi");