acpi-cpufreq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  45. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  46. MODULE_LICENSE("GPL");
  47. #define PFX "acpi-cpufreq: "
  48. enum {
  49. UNDEFINED_CAPABLE = 0,
  50. SYSTEM_INTEL_MSR_CAPABLE,
  51. SYSTEM_AMD_MSR_CAPABLE,
  52. SYSTEM_IO_CAPABLE,
  53. };
  54. #define INTEL_MSR_RANGE (0xffff)
  55. #define AMD_MSR_RANGE (0x7)
  56. #define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
  57. struct acpi_cpufreq_data {
  58. struct acpi_processor_performance *acpi_data;
  59. struct cpufreq_frequency_table *freq_table;
  60. unsigned int resume;
  61. unsigned int cpu_feature;
  62. cpumask_var_t freqdomain_cpus;
  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. static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
  146. {
  147. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  148. return cpufreq_show_cpus(data->freqdomain_cpus, buf);
  149. }
  150. cpufreq_freq_attr_ro(freqdomain_cpus);
  151. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  152. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  153. size_t count)
  154. {
  155. return _store_boost(buf, count);
  156. }
  157. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  158. {
  159. return sprintf(buf, "%u\n", boost_enabled);
  160. }
  161. cpufreq_freq_attr_rw(cpb);
  162. #endif
  163. static int check_est_cpu(unsigned int cpuid)
  164. {
  165. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  166. return cpu_has(cpu, X86_FEATURE_EST);
  167. }
  168. static int check_amd_hwpstate_cpu(unsigned int cpuid)
  169. {
  170. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  171. return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
  172. }
  173. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  174. {
  175. struct acpi_processor_performance *perf;
  176. int i;
  177. perf = data->acpi_data;
  178. for (i = 0; i < perf->state_count; i++) {
  179. if (value == perf->states[i].status)
  180. return data->freq_table[i].frequency;
  181. }
  182. return 0;
  183. }
  184. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  185. {
  186. int i;
  187. struct acpi_processor_performance *perf;
  188. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  189. msr &= AMD_MSR_RANGE;
  190. else
  191. msr &= INTEL_MSR_RANGE;
  192. perf = data->acpi_data;
  193. for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  194. if (msr == perf->states[data->freq_table[i].driver_data].status)
  195. return data->freq_table[i].frequency;
  196. }
  197. return data->freq_table[0].frequency;
  198. }
  199. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  200. {
  201. switch (data->cpu_feature) {
  202. case SYSTEM_INTEL_MSR_CAPABLE:
  203. case SYSTEM_AMD_MSR_CAPABLE:
  204. return extract_msr(val, data);
  205. case SYSTEM_IO_CAPABLE:
  206. return extract_io(val, data);
  207. default:
  208. return 0;
  209. }
  210. }
  211. struct msr_addr {
  212. u32 reg;
  213. };
  214. struct io_addr {
  215. u16 port;
  216. u8 bit_width;
  217. };
  218. struct drv_cmd {
  219. unsigned int type;
  220. const struct cpumask *mask;
  221. union {
  222. struct msr_addr msr;
  223. struct io_addr io;
  224. } addr;
  225. u32 val;
  226. };
  227. /* Called via smp_call_function_single(), on the target CPU */
  228. static void do_drv_read(void *_cmd)
  229. {
  230. struct drv_cmd *cmd = _cmd;
  231. u32 h;
  232. switch (cmd->type) {
  233. case SYSTEM_INTEL_MSR_CAPABLE:
  234. case SYSTEM_AMD_MSR_CAPABLE:
  235. rdmsr(cmd->addr.msr.reg, cmd->val, h);
  236. break;
  237. case SYSTEM_IO_CAPABLE:
  238. acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
  239. &cmd->val,
  240. (u32)cmd->addr.io.bit_width);
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. /* Called via smp_call_function_many(), on the target CPUs */
  247. static void do_drv_write(void *_cmd)
  248. {
  249. struct drv_cmd *cmd = _cmd;
  250. u32 lo, hi;
  251. switch (cmd->type) {
  252. case SYSTEM_INTEL_MSR_CAPABLE:
  253. rdmsr(cmd->addr.msr.reg, lo, hi);
  254. lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
  255. wrmsr(cmd->addr.msr.reg, lo, hi);
  256. break;
  257. case SYSTEM_AMD_MSR_CAPABLE:
  258. wrmsr(cmd->addr.msr.reg, cmd->val, 0);
  259. break;
  260. case SYSTEM_IO_CAPABLE:
  261. acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
  262. cmd->val,
  263. (u32)cmd->addr.io.bit_width);
  264. break;
  265. default:
  266. break;
  267. }
  268. }
  269. static void drv_read(struct drv_cmd *cmd)
  270. {
  271. int err;
  272. cmd->val = 0;
  273. err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
  274. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  275. }
  276. static void drv_write(struct drv_cmd *cmd)
  277. {
  278. int this_cpu;
  279. this_cpu = get_cpu();
  280. if (cpumask_test_cpu(this_cpu, cmd->mask))
  281. do_drv_write(cmd);
  282. smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
  283. put_cpu();
  284. }
  285. static u32 get_cur_val(const struct cpumask *mask)
  286. {
  287. struct acpi_processor_performance *perf;
  288. struct drv_cmd cmd;
  289. if (unlikely(cpumask_empty(mask)))
  290. return 0;
  291. switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
  292. case SYSTEM_INTEL_MSR_CAPABLE:
  293. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  294. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  295. break;
  296. case SYSTEM_AMD_MSR_CAPABLE:
  297. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  298. cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
  299. break;
  300. case SYSTEM_IO_CAPABLE:
  301. cmd.type = SYSTEM_IO_CAPABLE;
  302. perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
  303. cmd.addr.io.port = perf->control_register.address;
  304. cmd.addr.io.bit_width = perf->control_register.bit_width;
  305. break;
  306. default:
  307. return 0;
  308. }
  309. cmd.mask = mask;
  310. drv_read(&cmd);
  311. pr_debug("get_cur_val = %u\n", cmd.val);
  312. return cmd.val;
  313. }
  314. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  315. {
  316. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
  317. unsigned int freq;
  318. unsigned int cached_freq;
  319. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  320. if (unlikely(data == NULL ||
  321. data->acpi_data == NULL || data->freq_table == NULL)) {
  322. return 0;
  323. }
  324. cached_freq = data->freq_table[data->acpi_data->state].frequency;
  325. freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
  326. if (freq != cached_freq) {
  327. /*
  328. * The dreaded BIOS frequency change behind our back.
  329. * Force set the frequency on next target call.
  330. */
  331. data->resume = 1;
  332. }
  333. pr_debug("cur freq = %u\n", freq);
  334. return freq;
  335. }
  336. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  337. struct acpi_cpufreq_data *data)
  338. {
  339. unsigned int cur_freq;
  340. unsigned int i;
  341. for (i = 0; i < 100; i++) {
  342. cur_freq = extract_freq(get_cur_val(mask), data);
  343. if (cur_freq == freq)
  344. return 1;
  345. udelay(10);
  346. }
  347. return 0;
  348. }
  349. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  350. unsigned int target_freq, unsigned int relation)
  351. {
  352. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  353. struct acpi_processor_performance *perf;
  354. struct cpufreq_freqs freqs;
  355. struct drv_cmd cmd;
  356. unsigned int next_state = 0; /* Index into freq_table */
  357. unsigned int next_perf_state = 0; /* Index into perf table */
  358. int result = 0;
  359. pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
  360. if (unlikely(data == NULL ||
  361. data->acpi_data == NULL || data->freq_table == NULL)) {
  362. return -ENODEV;
  363. }
  364. perf = data->acpi_data;
  365. result = cpufreq_frequency_table_target(policy,
  366. data->freq_table,
  367. target_freq,
  368. relation, &next_state);
  369. if (unlikely(result)) {
  370. result = -ENODEV;
  371. goto out;
  372. }
  373. next_perf_state = data->freq_table[next_state].driver_data;
  374. if (perf->state == next_perf_state) {
  375. if (unlikely(data->resume)) {
  376. pr_debug("Called after resume, resetting to P%d\n",
  377. next_perf_state);
  378. data->resume = 0;
  379. } else {
  380. pr_debug("Already at target state (P%d)\n",
  381. next_perf_state);
  382. goto out;
  383. }
  384. }
  385. switch (data->cpu_feature) {
  386. case SYSTEM_INTEL_MSR_CAPABLE:
  387. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  388. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  389. cmd.val = (u32) perf->states[next_perf_state].control;
  390. break;
  391. case SYSTEM_AMD_MSR_CAPABLE:
  392. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  393. cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
  394. cmd.val = (u32) perf->states[next_perf_state].control;
  395. break;
  396. case SYSTEM_IO_CAPABLE:
  397. cmd.type = SYSTEM_IO_CAPABLE;
  398. cmd.addr.io.port = perf->control_register.address;
  399. cmd.addr.io.bit_width = perf->control_register.bit_width;
  400. cmd.val = (u32) perf->states[next_perf_state].control;
  401. break;
  402. default:
  403. result = -ENODEV;
  404. goto out;
  405. }
  406. /* cpufreq holds the hotplug lock, so we are safe from here on */
  407. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  408. cmd.mask = policy->cpus;
  409. else
  410. cmd.mask = cpumask_of(policy->cpu);
  411. freqs.old = perf->states[perf->state].core_frequency * 1000;
  412. freqs.new = data->freq_table[next_state].frequency;
  413. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  414. drv_write(&cmd);
  415. if (acpi_pstate_strict) {
  416. if (!check_freqs(cmd.mask, freqs.new, data)) {
  417. pr_debug("acpi_cpufreq_target failed (%d)\n",
  418. policy->cpu);
  419. result = -EAGAIN;
  420. freqs.new = freqs.old;
  421. }
  422. }
  423. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  424. if (!result)
  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(*data), GFP_KERNEL);
  595. if (!data)
  596. return -ENOMEM;
  597. if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
  598. result = -ENOMEM;
  599. goto err_free;
  600. }
  601. data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
  602. per_cpu(acfreq_data, cpu) = data;
  603. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  604. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  605. result = acpi_processor_register_performance(data->acpi_data, cpu);
  606. if (result)
  607. goto err_free_mask;
  608. perf = data->acpi_data;
  609. policy->shared_type = perf->shared_type;
  610. /*
  611. * Will let policy->cpus know about dependency only when software
  612. * coordination is required.
  613. */
  614. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  615. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  616. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  617. }
  618. cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
  619. #ifdef CONFIG_SMP
  620. dmi_check_system(sw_any_bug_dmi_table);
  621. if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
  622. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  623. cpumask_copy(policy->cpus, cpu_core_mask(cpu));
  624. }
  625. if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
  626. cpumask_clear(policy->cpus);
  627. cpumask_set_cpu(cpu, policy->cpus);
  628. cpumask_copy(data->freqdomain_cpus, cpu_sibling_mask(cpu));
  629. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  630. pr_info_once(PFX "overriding BIOS provided _PSD data\n");
  631. }
  632. #endif
  633. /* capability check */
  634. if (perf->state_count <= 1) {
  635. pr_debug("No P-States\n");
  636. result = -ENODEV;
  637. goto err_unreg;
  638. }
  639. if (perf->control_register.space_id != perf->status_register.space_id) {
  640. result = -ENODEV;
  641. goto err_unreg;
  642. }
  643. switch (perf->control_register.space_id) {
  644. case ACPI_ADR_SPACE_SYSTEM_IO:
  645. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  646. boot_cpu_data.x86 == 0xf) {
  647. pr_debug("AMD K8 systems must use native drivers.\n");
  648. result = -ENODEV;
  649. goto err_unreg;
  650. }
  651. pr_debug("SYSTEM IO addr space\n");
  652. data->cpu_feature = SYSTEM_IO_CAPABLE;
  653. break;
  654. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  655. pr_debug("HARDWARE addr space\n");
  656. if (check_est_cpu(cpu)) {
  657. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  658. break;
  659. }
  660. if (check_amd_hwpstate_cpu(cpu)) {
  661. data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
  662. break;
  663. }
  664. result = -ENODEV;
  665. goto err_unreg;
  666. default:
  667. pr_debug("Unknown addr space %d\n",
  668. (u32) (perf->control_register.space_id));
  669. result = -ENODEV;
  670. goto err_unreg;
  671. }
  672. data->freq_table = kmalloc(sizeof(*data->freq_table) *
  673. (perf->state_count+1), GFP_KERNEL);
  674. if (!data->freq_table) {
  675. result = -ENOMEM;
  676. goto err_unreg;
  677. }
  678. /* detect transition latency */
  679. policy->cpuinfo.transition_latency = 0;
  680. for (i = 0; i < perf->state_count; i++) {
  681. if ((perf->states[i].transition_latency * 1000) >
  682. policy->cpuinfo.transition_latency)
  683. policy->cpuinfo.transition_latency =
  684. perf->states[i].transition_latency * 1000;
  685. }
  686. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  687. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  688. policy->cpuinfo.transition_latency > 20 * 1000) {
  689. policy->cpuinfo.transition_latency = 20 * 1000;
  690. printk_once(KERN_INFO
  691. "P-state transition latency capped at 20 uS\n");
  692. }
  693. /* table init */
  694. for (i = 0; i < perf->state_count; i++) {
  695. if (i > 0 && perf->states[i].core_frequency >=
  696. data->freq_table[valid_states-1].frequency / 1000)
  697. continue;
  698. data->freq_table[valid_states].driver_data = i;
  699. data->freq_table[valid_states].frequency =
  700. perf->states[i].core_frequency * 1000;
  701. valid_states++;
  702. }
  703. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  704. perf->state = 0;
  705. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  706. if (result)
  707. goto err_freqfree;
  708. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  709. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  710. switch (perf->control_register.space_id) {
  711. case ACPI_ADR_SPACE_SYSTEM_IO:
  712. /* Current speed is unknown and not detectable by IO port */
  713. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  714. break;
  715. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  716. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  717. policy->cur = get_cur_freq_on_cpu(cpu);
  718. break;
  719. default:
  720. break;
  721. }
  722. /* notify BIOS that we exist */
  723. acpi_processor_notify_smm(THIS_MODULE);
  724. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  725. for (i = 0; i < perf->state_count; i++)
  726. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  727. (i == perf->state ? '*' : ' '), i,
  728. (u32) perf->states[i].core_frequency,
  729. (u32) perf->states[i].power,
  730. (u32) perf->states[i].transition_latency);
  731. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  732. /*
  733. * the first call to ->target() should result in us actually
  734. * writing something to the appropriate registers.
  735. */
  736. data->resume = 1;
  737. return result;
  738. err_freqfree:
  739. kfree(data->freq_table);
  740. err_unreg:
  741. acpi_processor_unregister_performance(perf, cpu);
  742. err_free_mask:
  743. free_cpumask_var(data->freqdomain_cpus);
  744. err_free:
  745. kfree(data);
  746. per_cpu(acfreq_data, cpu) = NULL;
  747. return result;
  748. }
  749. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  750. {
  751. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  752. pr_debug("acpi_cpufreq_cpu_exit\n");
  753. if (data) {
  754. cpufreq_frequency_table_put_attr(policy->cpu);
  755. per_cpu(acfreq_data, policy->cpu) = NULL;
  756. acpi_processor_unregister_performance(data->acpi_data,
  757. policy->cpu);
  758. free_cpumask_var(data->freqdomain_cpus);
  759. kfree(data->freq_table);
  760. kfree(data);
  761. }
  762. return 0;
  763. }
  764. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  765. {
  766. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  767. pr_debug("acpi_cpufreq_resume\n");
  768. data->resume = 1;
  769. return 0;
  770. }
  771. static struct freq_attr *acpi_cpufreq_attr[] = {
  772. &cpufreq_freq_attr_scaling_available_freqs,
  773. &freqdomain_cpus,
  774. NULL, /* this is a placeholder for cpb, do not remove */
  775. NULL,
  776. };
  777. static struct cpufreq_driver acpi_cpufreq_driver = {
  778. .verify = acpi_cpufreq_verify,
  779. .target = acpi_cpufreq_target,
  780. .bios_limit = acpi_processor_get_bios_limit,
  781. .init = acpi_cpufreq_cpu_init,
  782. .exit = acpi_cpufreq_cpu_exit,
  783. .resume = acpi_cpufreq_resume,
  784. .name = "acpi-cpufreq",
  785. .attr = acpi_cpufreq_attr,
  786. };
  787. static void __init acpi_cpufreq_boost_init(void)
  788. {
  789. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  790. msrs = msrs_alloc();
  791. if (!msrs)
  792. return;
  793. boost_supported = true;
  794. boost_enabled = boost_state(0);
  795. get_online_cpus();
  796. /* Force all MSRs to the same value */
  797. boost_set_msrs(boost_enabled, cpu_online_mask);
  798. register_cpu_notifier(&boost_nb);
  799. put_online_cpus();
  800. } else
  801. global_boost.attr.mode = 0444;
  802. /* We create the boost file in any case, though for systems without
  803. * hardware support it will be read-only and hardwired to return 0.
  804. */
  805. if (cpufreq_sysfs_create_file(&(global_boost.attr)))
  806. pr_warn(PFX "could not register global boost sysfs file\n");
  807. else
  808. pr_debug("registered global boost sysfs file\n");
  809. }
  810. static void __exit acpi_cpufreq_boost_exit(void)
  811. {
  812. cpufreq_sysfs_remove_file(&(global_boost.attr));
  813. if (msrs) {
  814. unregister_cpu_notifier(&boost_nb);
  815. msrs_free(msrs);
  816. msrs = NULL;
  817. }
  818. }
  819. static int __init acpi_cpufreq_init(void)
  820. {
  821. int ret;
  822. /* don't keep reloading if cpufreq_driver exists */
  823. if (cpufreq_get_current_driver())
  824. return 0;
  825. if (acpi_disabled)
  826. return 0;
  827. pr_debug("acpi_cpufreq_init\n");
  828. ret = acpi_cpufreq_early_init();
  829. if (ret)
  830. return ret;
  831. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  832. /* this is a sysfs file with a strange name and an even stranger
  833. * semantic - per CPU instantiation, but system global effect.
  834. * Lets enable it only on AMD CPUs for compatibility reasons and
  835. * only if configured. This is considered legacy code, which
  836. * will probably be removed at some point in the future.
  837. */
  838. if (check_amd_hwpstate_cpu(0)) {
  839. struct freq_attr **iter;
  840. pr_debug("adding sysfs entry for cpb\n");
  841. for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
  842. ;
  843. /* make sure there is a terminator behind it */
  844. if (iter[1] == NULL)
  845. *iter = &cpb;
  846. }
  847. #endif
  848. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  849. if (ret)
  850. free_acpi_perf_data();
  851. else
  852. acpi_cpufreq_boost_init();
  853. return ret;
  854. }
  855. static void __exit acpi_cpufreq_exit(void)
  856. {
  857. pr_debug("acpi_cpufreq_exit\n");
  858. acpi_cpufreq_boost_exit();
  859. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  860. free_acpi_perf_data();
  861. }
  862. module_param(acpi_pstate_strict, uint, 0644);
  863. MODULE_PARM_DESC(acpi_pstate_strict,
  864. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  865. "performed during frequency changes.");
  866. late_initcall(acpi_cpufreq_init);
  867. module_exit(acpi_cpufreq_exit);
  868. static const struct x86_cpu_id acpi_cpufreq_ids[] = {
  869. X86_FEATURE_MATCH(X86_FEATURE_ACPI),
  870. X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
  871. {}
  872. };
  873. MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
  874. static const struct acpi_device_id processor_device_ids[] = {
  875. {ACPI_PROCESSOR_OBJECT_HID, },
  876. {ACPI_PROCESSOR_DEVICE_HID, },
  877. {},
  878. };
  879. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  880. MODULE_ALIAS("acpi");