acpi-cpufreq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 cpufreq_frequency_table *freq_table;
  59. unsigned int resume;
  60. unsigned int cpu_feature;
  61. unsigned int acpi_perf_cpu;
  62. cpumask_var_t freqdomain_cpus;
  63. void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val);
  64. u32 (*cpu_freq_read)(struct acpi_pct_register *reg);
  65. };
  66. /* acpi_perf_data is a pointer to percpu data. */
  67. static struct acpi_processor_performance __percpu *acpi_perf_data;
  68. static inline struct acpi_processor_performance *to_perf_data(struct acpi_cpufreq_data *data)
  69. {
  70. return per_cpu_ptr(acpi_perf_data, data->acpi_perf_cpu);
  71. }
  72. static struct cpufreq_driver acpi_cpufreq_driver;
  73. static unsigned int acpi_pstate_strict;
  74. static struct msr __percpu *msrs;
  75. static bool boost_state(unsigned int cpu)
  76. {
  77. u32 lo, hi;
  78. u64 msr;
  79. switch (boot_cpu_data.x86_vendor) {
  80. case X86_VENDOR_INTEL:
  81. rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
  82. msr = lo | ((u64)hi << 32);
  83. return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
  84. case X86_VENDOR_AMD:
  85. rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
  86. msr = lo | ((u64)hi << 32);
  87. return !(msr & MSR_K7_HWCR_CPB_DIS);
  88. }
  89. return false;
  90. }
  91. static void boost_set_msrs(bool enable, const struct cpumask *cpumask)
  92. {
  93. u32 cpu;
  94. u32 msr_addr;
  95. u64 msr_mask;
  96. switch (boot_cpu_data.x86_vendor) {
  97. case X86_VENDOR_INTEL:
  98. msr_addr = MSR_IA32_MISC_ENABLE;
  99. msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
  100. break;
  101. case X86_VENDOR_AMD:
  102. msr_addr = MSR_K7_HWCR;
  103. msr_mask = MSR_K7_HWCR_CPB_DIS;
  104. break;
  105. default:
  106. return;
  107. }
  108. rdmsr_on_cpus(cpumask, msr_addr, msrs);
  109. for_each_cpu(cpu, cpumask) {
  110. struct msr *reg = per_cpu_ptr(msrs, cpu);
  111. if (enable)
  112. reg->q &= ~msr_mask;
  113. else
  114. reg->q |= msr_mask;
  115. }
  116. wrmsr_on_cpus(cpumask, msr_addr, msrs);
  117. }
  118. static int set_boost(int val)
  119. {
  120. get_online_cpus();
  121. boost_set_msrs(val, cpu_online_mask);
  122. put_online_cpus();
  123. pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
  124. return 0;
  125. }
  126. static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
  127. {
  128. struct acpi_cpufreq_data *data = policy->driver_data;
  129. if (unlikely(!data))
  130. return -ENODEV;
  131. return cpufreq_show_cpus(data->freqdomain_cpus, buf);
  132. }
  133. cpufreq_freq_attr_ro(freqdomain_cpus);
  134. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  135. static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
  136. size_t count)
  137. {
  138. int ret;
  139. unsigned int val = 0;
  140. if (!acpi_cpufreq_driver.set_boost)
  141. return -EINVAL;
  142. ret = kstrtouint(buf, 10, &val);
  143. if (ret || val > 1)
  144. return -EINVAL;
  145. set_boost(val);
  146. return count;
  147. }
  148. static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
  149. {
  150. return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
  151. }
  152. cpufreq_freq_attr_rw(cpb);
  153. #endif
  154. static int check_est_cpu(unsigned int cpuid)
  155. {
  156. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  157. return cpu_has(cpu, X86_FEATURE_EST);
  158. }
  159. static int check_amd_hwpstate_cpu(unsigned int cpuid)
  160. {
  161. struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
  162. return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
  163. }
  164. static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
  165. {
  166. struct acpi_processor_performance *perf;
  167. int i;
  168. perf = to_perf_data(data);
  169. for (i = 0; i < perf->state_count; i++) {
  170. if (value == perf->states[i].status)
  171. return data->freq_table[i].frequency;
  172. }
  173. return 0;
  174. }
  175. static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
  176. {
  177. struct cpufreq_frequency_table *pos;
  178. struct acpi_processor_performance *perf;
  179. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  180. msr &= AMD_MSR_RANGE;
  181. else
  182. msr &= INTEL_MSR_RANGE;
  183. perf = to_perf_data(data);
  184. cpufreq_for_each_entry(pos, data->freq_table)
  185. if (msr == perf->states[pos->driver_data].status)
  186. return pos->frequency;
  187. return data->freq_table[0].frequency;
  188. }
  189. static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
  190. {
  191. switch (data->cpu_feature) {
  192. case SYSTEM_INTEL_MSR_CAPABLE:
  193. case SYSTEM_AMD_MSR_CAPABLE:
  194. return extract_msr(val, data);
  195. case SYSTEM_IO_CAPABLE:
  196. return extract_io(val, data);
  197. default:
  198. return 0;
  199. }
  200. }
  201. static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used)
  202. {
  203. u32 val, dummy;
  204. rdmsr(MSR_IA32_PERF_CTL, val, dummy);
  205. return val;
  206. }
  207. static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val)
  208. {
  209. u32 lo, hi;
  210. rdmsr(MSR_IA32_PERF_CTL, lo, hi);
  211. lo = (lo & ~INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE);
  212. wrmsr(MSR_IA32_PERF_CTL, lo, hi);
  213. }
  214. static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used)
  215. {
  216. u32 val, dummy;
  217. rdmsr(MSR_AMD_PERF_CTL, val, dummy);
  218. return val;
  219. }
  220. static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val)
  221. {
  222. wrmsr(MSR_AMD_PERF_CTL, val, 0);
  223. }
  224. static u32 cpu_freq_read_io(struct acpi_pct_register *reg)
  225. {
  226. u32 val;
  227. acpi_os_read_port(reg->address, &val, reg->bit_width);
  228. return val;
  229. }
  230. static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val)
  231. {
  232. acpi_os_write_port(reg->address, val, reg->bit_width);
  233. }
  234. struct drv_cmd {
  235. struct acpi_pct_register *reg;
  236. u32 val;
  237. union {
  238. void (*write)(struct acpi_pct_register *reg, u32 val);
  239. u32 (*read)(struct acpi_pct_register *reg);
  240. } func;
  241. };
  242. /* Called via smp_call_function_single(), on the target CPU */
  243. static void do_drv_read(void *_cmd)
  244. {
  245. struct drv_cmd *cmd = _cmd;
  246. cmd->val = cmd->func.read(cmd->reg);
  247. }
  248. static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)
  249. {
  250. struct acpi_processor_performance *perf = to_perf_data(data);
  251. struct drv_cmd cmd = {
  252. .reg = &perf->control_register,
  253. .func.read = data->cpu_freq_read,
  254. };
  255. int err;
  256. err = smp_call_function_any(mask, do_drv_read, &cmd, 1);
  257. WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
  258. return cmd.val;
  259. }
  260. /* Called via smp_call_function_many(), on the target CPUs */
  261. static void do_drv_write(void *_cmd)
  262. {
  263. struct drv_cmd *cmd = _cmd;
  264. cmd->func.write(cmd->reg, cmd->val);
  265. }
  266. static void drv_write(struct acpi_cpufreq_data *data,
  267. const struct cpumask *mask, u32 val)
  268. {
  269. struct acpi_processor_performance *perf = to_perf_data(data);
  270. struct drv_cmd cmd = {
  271. .reg = &perf->control_register,
  272. .val = val,
  273. .func.write = data->cpu_freq_write,
  274. };
  275. int this_cpu;
  276. this_cpu = get_cpu();
  277. if (cpumask_test_cpu(this_cpu, mask))
  278. do_drv_write(&cmd);
  279. smp_call_function_many(mask, do_drv_write, &cmd, 1);
  280. put_cpu();
  281. }
  282. static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
  283. {
  284. u32 val;
  285. if (unlikely(cpumask_empty(mask)))
  286. return 0;
  287. val = drv_read(data, mask);
  288. pr_debug("get_cur_val = %u\n", val);
  289. return val;
  290. }
  291. static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
  292. {
  293. struct acpi_cpufreq_data *data;
  294. struct cpufreq_policy *policy;
  295. unsigned int freq;
  296. unsigned int cached_freq;
  297. pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
  298. policy = cpufreq_cpu_get_raw(cpu);
  299. if (unlikely(!policy))
  300. return 0;
  301. data = policy->driver_data;
  302. if (unlikely(!data || !data->freq_table))
  303. return 0;
  304. cached_freq = data->freq_table[to_perf_data(data)->state].frequency;
  305. freq = extract_freq(get_cur_val(cpumask_of(cpu), data), data);
  306. if (freq != cached_freq) {
  307. /*
  308. * The dreaded BIOS frequency change behind our back.
  309. * Force set the frequency on next target call.
  310. */
  311. data->resume = 1;
  312. }
  313. pr_debug("cur freq = %u\n", freq);
  314. return freq;
  315. }
  316. static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
  317. struct acpi_cpufreq_data *data)
  318. {
  319. unsigned int cur_freq;
  320. unsigned int i;
  321. for (i = 0; i < 100; i++) {
  322. cur_freq = extract_freq(get_cur_val(mask, data), data);
  323. if (cur_freq == freq)
  324. return 1;
  325. udelay(10);
  326. }
  327. return 0;
  328. }
  329. static int acpi_cpufreq_target(struct cpufreq_policy *policy,
  330. unsigned int index)
  331. {
  332. struct acpi_cpufreq_data *data = policy->driver_data;
  333. struct acpi_processor_performance *perf;
  334. const struct cpumask *mask;
  335. unsigned int next_perf_state = 0; /* Index into perf table */
  336. int result = 0;
  337. if (unlikely(data == NULL || data->freq_table == NULL)) {
  338. return -ENODEV;
  339. }
  340. perf = to_perf_data(data);
  341. next_perf_state = data->freq_table[index].driver_data;
  342. if (perf->state == next_perf_state) {
  343. if (unlikely(data->resume)) {
  344. pr_debug("Called after resume, resetting to P%d\n",
  345. next_perf_state);
  346. data->resume = 0;
  347. } else {
  348. pr_debug("Already at target state (P%d)\n",
  349. next_perf_state);
  350. return 0;
  351. }
  352. }
  353. /*
  354. * The core won't allow CPUs to go away until the governor has been
  355. * stopped, so we can rely on the stability of policy->cpus.
  356. */
  357. mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ?
  358. cpumask_of(policy->cpu) : policy->cpus;
  359. drv_write(data, mask, perf->states[next_perf_state].control);
  360. if (acpi_pstate_strict) {
  361. if (!check_freqs(mask, data->freq_table[index].frequency,
  362. data)) {
  363. pr_debug("acpi_cpufreq_target failed (%d)\n",
  364. policy->cpu);
  365. result = -EAGAIN;
  366. }
  367. }
  368. if (!result)
  369. perf->state = next_perf_state;
  370. return result;
  371. }
  372. static unsigned long
  373. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  374. {
  375. struct acpi_processor_performance *perf;
  376. perf = to_perf_data(data);
  377. if (cpu_khz) {
  378. /* search the closest match to cpu_khz */
  379. unsigned int i;
  380. unsigned long freq;
  381. unsigned long freqn = perf->states[0].core_frequency * 1000;
  382. for (i = 0; i < (perf->state_count-1); i++) {
  383. freq = freqn;
  384. freqn = perf->states[i+1].core_frequency * 1000;
  385. if ((2 * cpu_khz) > (freqn + freq)) {
  386. perf->state = i;
  387. return freq;
  388. }
  389. }
  390. perf->state = perf->state_count-1;
  391. return freqn;
  392. } else {
  393. /* assume CPU is at P0... */
  394. perf->state = 0;
  395. return perf->states[0].core_frequency * 1000;
  396. }
  397. }
  398. static void free_acpi_perf_data(void)
  399. {
  400. unsigned int i;
  401. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  402. for_each_possible_cpu(i)
  403. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  404. ->shared_cpu_map);
  405. free_percpu(acpi_perf_data);
  406. }
  407. static int boost_notify(struct notifier_block *nb, unsigned long action,
  408. void *hcpu)
  409. {
  410. unsigned cpu = (long)hcpu;
  411. const struct cpumask *cpumask;
  412. cpumask = get_cpu_mask(cpu);
  413. /*
  414. * Clear the boost-disable bit on the CPU_DOWN path so that
  415. * this cpu cannot block the remaining ones from boosting. On
  416. * the CPU_UP path we simply keep the boost-disable flag in
  417. * sync with the current global state.
  418. */
  419. switch (action) {
  420. case CPU_DOWN_FAILED:
  421. case CPU_DOWN_FAILED_FROZEN:
  422. case CPU_ONLINE:
  423. case CPU_ONLINE_FROZEN:
  424. boost_set_msrs(acpi_cpufreq_driver.boost_enabled, cpumask);
  425. break;
  426. case CPU_DOWN_PREPARE:
  427. case CPU_DOWN_PREPARE_FROZEN:
  428. boost_set_msrs(1, cpumask);
  429. break;
  430. default:
  431. break;
  432. }
  433. return NOTIFY_OK;
  434. }
  435. static struct notifier_block boost_nb = {
  436. .notifier_call = boost_notify,
  437. };
  438. /*
  439. * acpi_cpufreq_early_init - initialize ACPI P-States library
  440. *
  441. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  442. * in order to determine correct frequency and voltage pairings. We can
  443. * do _PDC and _PSD and find out the processor dependency for the
  444. * actual init that will happen later...
  445. */
  446. static int __init acpi_cpufreq_early_init(void)
  447. {
  448. unsigned int i;
  449. pr_debug("acpi_cpufreq_early_init\n");
  450. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  451. if (!acpi_perf_data) {
  452. pr_debug("Memory allocation error for acpi_perf_data.\n");
  453. return -ENOMEM;
  454. }
  455. for_each_possible_cpu(i) {
  456. if (!zalloc_cpumask_var_node(
  457. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  458. GFP_KERNEL, cpu_to_node(i))) {
  459. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  460. free_acpi_perf_data();
  461. return -ENOMEM;
  462. }
  463. }
  464. /* Do initialization in ACPI core */
  465. acpi_processor_preregister_performance(acpi_perf_data);
  466. return 0;
  467. }
  468. #ifdef CONFIG_SMP
  469. /*
  470. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  471. * or do it in BIOS firmware and won't inform about it to OS. If not
  472. * detected, this has a side effect of making CPU run at a different speed
  473. * than OS intended it to run at. Detect it and handle it cleanly.
  474. */
  475. static int bios_with_sw_any_bug;
  476. static int sw_any_bug_found(const struct dmi_system_id *d)
  477. {
  478. bios_with_sw_any_bug = 1;
  479. return 0;
  480. }
  481. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  482. {
  483. .callback = sw_any_bug_found,
  484. .ident = "Supermicro Server X6DLP",
  485. .matches = {
  486. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  487. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  488. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  489. },
  490. },
  491. { }
  492. };
  493. static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
  494. {
  495. /* Intel Xeon Processor 7100 Series Specification Update
  496. * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
  497. * AL30: A Machine Check Exception (MCE) Occurring during an
  498. * Enhanced Intel SpeedStep Technology Ratio Change May Cause
  499. * Both Processor Cores to Lock Up. */
  500. if (c->x86_vendor == X86_VENDOR_INTEL) {
  501. if ((c->x86 == 15) &&
  502. (c->x86_model == 6) &&
  503. (c->x86_mask == 8)) {
  504. printk(KERN_INFO "acpi-cpufreq: Intel(R) "
  505. "Xeon(R) 7100 Errata AL30, processors may "
  506. "lock up on frequency changes: disabling "
  507. "acpi-cpufreq.\n");
  508. return -ENODEV;
  509. }
  510. }
  511. return 0;
  512. }
  513. #endif
  514. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  515. {
  516. unsigned int i;
  517. unsigned int valid_states = 0;
  518. unsigned int cpu = policy->cpu;
  519. struct acpi_cpufreq_data *data;
  520. unsigned int result = 0;
  521. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  522. struct acpi_processor_performance *perf;
  523. #ifdef CONFIG_SMP
  524. static int blacklisted;
  525. #endif
  526. pr_debug("acpi_cpufreq_cpu_init\n");
  527. #ifdef CONFIG_SMP
  528. if (blacklisted)
  529. return blacklisted;
  530. blacklisted = acpi_cpufreq_blacklist(c);
  531. if (blacklisted)
  532. return blacklisted;
  533. #endif
  534. data = kzalloc(sizeof(*data), GFP_KERNEL);
  535. if (!data)
  536. return -ENOMEM;
  537. if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
  538. result = -ENOMEM;
  539. goto err_free;
  540. }
  541. perf = per_cpu_ptr(acpi_perf_data, cpu);
  542. data->acpi_perf_cpu = cpu;
  543. policy->driver_data = data;
  544. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  545. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  546. result = acpi_processor_register_performance(perf, cpu);
  547. if (result)
  548. goto err_free_mask;
  549. policy->shared_type = perf->shared_type;
  550. /*
  551. * Will let policy->cpus know about dependency only when software
  552. * coordination is required.
  553. */
  554. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  555. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  556. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  557. }
  558. cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
  559. #ifdef CONFIG_SMP
  560. dmi_check_system(sw_any_bug_dmi_table);
  561. if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
  562. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  563. cpumask_copy(policy->cpus, topology_core_cpumask(cpu));
  564. }
  565. if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
  566. cpumask_clear(policy->cpus);
  567. cpumask_set_cpu(cpu, policy->cpus);
  568. cpumask_copy(data->freqdomain_cpus,
  569. topology_sibling_cpumask(cpu));
  570. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  571. pr_info_once(PFX "overriding BIOS provided _PSD data\n");
  572. }
  573. #endif
  574. /* capability check */
  575. if (perf->state_count <= 1) {
  576. pr_debug("No P-States\n");
  577. result = -ENODEV;
  578. goto err_unreg;
  579. }
  580. if (perf->control_register.space_id != perf->status_register.space_id) {
  581. result = -ENODEV;
  582. goto err_unreg;
  583. }
  584. switch (perf->control_register.space_id) {
  585. case ACPI_ADR_SPACE_SYSTEM_IO:
  586. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  587. boot_cpu_data.x86 == 0xf) {
  588. pr_debug("AMD K8 systems must use native drivers.\n");
  589. result = -ENODEV;
  590. goto err_unreg;
  591. }
  592. pr_debug("SYSTEM IO addr space\n");
  593. data->cpu_feature = SYSTEM_IO_CAPABLE;
  594. data->cpu_freq_read = cpu_freq_read_io;
  595. data->cpu_freq_write = cpu_freq_write_io;
  596. break;
  597. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  598. pr_debug("HARDWARE addr space\n");
  599. if (check_est_cpu(cpu)) {
  600. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  601. data->cpu_freq_read = cpu_freq_read_intel;
  602. data->cpu_freq_write = cpu_freq_write_intel;
  603. break;
  604. }
  605. if (check_amd_hwpstate_cpu(cpu)) {
  606. data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
  607. data->cpu_freq_read = cpu_freq_read_amd;
  608. data->cpu_freq_write = cpu_freq_write_amd;
  609. break;
  610. }
  611. result = -ENODEV;
  612. goto err_unreg;
  613. default:
  614. pr_debug("Unknown addr space %d\n",
  615. (u32) (perf->control_register.space_id));
  616. result = -ENODEV;
  617. goto err_unreg;
  618. }
  619. data->freq_table = kzalloc(sizeof(*data->freq_table) *
  620. (perf->state_count+1), GFP_KERNEL);
  621. if (!data->freq_table) {
  622. result = -ENOMEM;
  623. goto err_unreg;
  624. }
  625. /* detect transition latency */
  626. policy->cpuinfo.transition_latency = 0;
  627. for (i = 0; i < perf->state_count; i++) {
  628. if ((perf->states[i].transition_latency * 1000) >
  629. policy->cpuinfo.transition_latency)
  630. policy->cpuinfo.transition_latency =
  631. perf->states[i].transition_latency * 1000;
  632. }
  633. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  634. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  635. policy->cpuinfo.transition_latency > 20 * 1000) {
  636. policy->cpuinfo.transition_latency = 20 * 1000;
  637. printk_once(KERN_INFO
  638. "P-state transition latency capped at 20 uS\n");
  639. }
  640. /* table init */
  641. for (i = 0; i < perf->state_count; i++) {
  642. if (i > 0 && perf->states[i].core_frequency >=
  643. data->freq_table[valid_states-1].frequency / 1000)
  644. continue;
  645. data->freq_table[valid_states].driver_data = i;
  646. data->freq_table[valid_states].frequency =
  647. perf->states[i].core_frequency * 1000;
  648. valid_states++;
  649. }
  650. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  651. perf->state = 0;
  652. result = cpufreq_table_validate_and_show(policy, data->freq_table);
  653. if (result)
  654. goto err_freqfree;
  655. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  656. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  657. switch (perf->control_register.space_id) {
  658. case ACPI_ADR_SPACE_SYSTEM_IO:
  659. /*
  660. * The core will not set policy->cur, because
  661. * cpufreq_driver->get is NULL, so we need to set it here.
  662. * However, we have to guess it, because the current speed is
  663. * unknown and not detectable via IO ports.
  664. */
  665. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  666. break;
  667. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  668. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  669. break;
  670. default:
  671. break;
  672. }
  673. /* notify BIOS that we exist */
  674. acpi_processor_notify_smm(THIS_MODULE);
  675. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  676. for (i = 0; i < perf->state_count; i++)
  677. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  678. (i == perf->state ? '*' : ' '), i,
  679. (u32) perf->states[i].core_frequency,
  680. (u32) perf->states[i].power,
  681. (u32) perf->states[i].transition_latency);
  682. /*
  683. * the first call to ->target() should result in us actually
  684. * writing something to the appropriate registers.
  685. */
  686. data->resume = 1;
  687. return result;
  688. err_freqfree:
  689. kfree(data->freq_table);
  690. err_unreg:
  691. acpi_processor_unregister_performance(cpu);
  692. err_free_mask:
  693. free_cpumask_var(data->freqdomain_cpus);
  694. err_free:
  695. kfree(data);
  696. policy->driver_data = NULL;
  697. return result;
  698. }
  699. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  700. {
  701. struct acpi_cpufreq_data *data = policy->driver_data;
  702. pr_debug("acpi_cpufreq_cpu_exit\n");
  703. if (data) {
  704. policy->driver_data = NULL;
  705. acpi_processor_unregister_performance(data->acpi_perf_cpu);
  706. free_cpumask_var(data->freqdomain_cpus);
  707. kfree(data->freq_table);
  708. kfree(data);
  709. }
  710. return 0;
  711. }
  712. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  713. {
  714. struct acpi_cpufreq_data *data = policy->driver_data;
  715. pr_debug("acpi_cpufreq_resume\n");
  716. data->resume = 1;
  717. return 0;
  718. }
  719. static struct freq_attr *acpi_cpufreq_attr[] = {
  720. &cpufreq_freq_attr_scaling_available_freqs,
  721. &freqdomain_cpus,
  722. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  723. &cpb,
  724. #endif
  725. NULL,
  726. };
  727. static struct cpufreq_driver acpi_cpufreq_driver = {
  728. .verify = cpufreq_generic_frequency_table_verify,
  729. .target_index = acpi_cpufreq_target,
  730. .bios_limit = acpi_processor_get_bios_limit,
  731. .init = acpi_cpufreq_cpu_init,
  732. .exit = acpi_cpufreq_cpu_exit,
  733. .resume = acpi_cpufreq_resume,
  734. .name = "acpi-cpufreq",
  735. .attr = acpi_cpufreq_attr,
  736. };
  737. static void __init acpi_cpufreq_boost_init(void)
  738. {
  739. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  740. msrs = msrs_alloc();
  741. if (!msrs)
  742. return;
  743. acpi_cpufreq_driver.set_boost = set_boost;
  744. acpi_cpufreq_driver.boost_enabled = boost_state(0);
  745. cpu_notifier_register_begin();
  746. /* Force all MSRs to the same value */
  747. boost_set_msrs(acpi_cpufreq_driver.boost_enabled,
  748. cpu_online_mask);
  749. __register_cpu_notifier(&boost_nb);
  750. cpu_notifier_register_done();
  751. }
  752. }
  753. static void acpi_cpufreq_boost_exit(void)
  754. {
  755. if (msrs) {
  756. unregister_cpu_notifier(&boost_nb);
  757. msrs_free(msrs);
  758. msrs = NULL;
  759. }
  760. }
  761. static int __init acpi_cpufreq_init(void)
  762. {
  763. int ret;
  764. if (acpi_disabled)
  765. return -ENODEV;
  766. /* don't keep reloading if cpufreq_driver exists */
  767. if (cpufreq_get_current_driver())
  768. return -EEXIST;
  769. pr_debug("acpi_cpufreq_init\n");
  770. ret = acpi_cpufreq_early_init();
  771. if (ret)
  772. return ret;
  773. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  774. /* this is a sysfs file with a strange name and an even stranger
  775. * semantic - per CPU instantiation, but system global effect.
  776. * Lets enable it only on AMD CPUs for compatibility reasons and
  777. * only if configured. This is considered legacy code, which
  778. * will probably be removed at some point in the future.
  779. */
  780. if (!check_amd_hwpstate_cpu(0)) {
  781. struct freq_attr **attr;
  782. pr_debug("CPB unsupported, do not expose it\n");
  783. for (attr = acpi_cpufreq_attr; *attr; attr++)
  784. if (*attr == &cpb) {
  785. *attr = NULL;
  786. break;
  787. }
  788. }
  789. #endif
  790. acpi_cpufreq_boost_init();
  791. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  792. if (ret) {
  793. free_acpi_perf_data();
  794. acpi_cpufreq_boost_exit();
  795. }
  796. return ret;
  797. }
  798. static void __exit acpi_cpufreq_exit(void)
  799. {
  800. pr_debug("acpi_cpufreq_exit\n");
  801. acpi_cpufreq_boost_exit();
  802. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  803. free_acpi_perf_data();
  804. }
  805. module_param(acpi_pstate_strict, uint, 0644);
  806. MODULE_PARM_DESC(acpi_pstate_strict,
  807. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  808. "performed during frequency changes.");
  809. late_initcall(acpi_cpufreq_init);
  810. module_exit(acpi_cpufreq_exit);
  811. static const struct x86_cpu_id acpi_cpufreq_ids[] = {
  812. X86_FEATURE_MATCH(X86_FEATURE_ACPI),
  813. X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
  814. {}
  815. };
  816. MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
  817. static const struct acpi_device_id processor_device_ids[] = {
  818. {ACPI_PROCESSOR_OBJECT_HID, },
  819. {ACPI_PROCESSOR_DEVICE_HID, },
  820. {},
  821. };
  822. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  823. MODULE_ALIAS("acpi");