acpi-cpufreq.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. int result = 0;
  353. pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
  354. if (unlikely(data == NULL ||
  355. data->acpi_data == NULL || data->freq_table == NULL)) {
  356. return -ENODEV;
  357. }
  358. perf = data->acpi_data;
  359. result = cpufreq_frequency_table_target(policy,
  360. data->freq_table,
  361. target_freq,
  362. relation, &next_state);
  363. if (unlikely(result)) {
  364. result = -ENODEV;
  365. goto out;
  366. }
  367. next_perf_state = data->freq_table[next_state].index;
  368. if (perf->state == next_perf_state) {
  369. if (unlikely(data->resume)) {
  370. pr_debug("Called after resume, resetting to P%d\n",
  371. next_perf_state);
  372. data->resume = 0;
  373. } else {
  374. pr_debug("Already at target state (P%d)\n",
  375. next_perf_state);
  376. goto out;
  377. }
  378. }
  379. switch (data->cpu_feature) {
  380. case SYSTEM_INTEL_MSR_CAPABLE:
  381. cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
  382. cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
  383. cmd.val = (u32) perf->states[next_perf_state].control;
  384. break;
  385. case SYSTEM_AMD_MSR_CAPABLE:
  386. cmd.type = SYSTEM_AMD_MSR_CAPABLE;
  387. cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
  388. cmd.val = (u32) perf->states[next_perf_state].control;
  389. break;
  390. case SYSTEM_IO_CAPABLE:
  391. cmd.type = SYSTEM_IO_CAPABLE;
  392. cmd.addr.io.port = perf->control_register.address;
  393. cmd.addr.io.bit_width = perf->control_register.bit_width;
  394. cmd.val = (u32) perf->states[next_perf_state].control;
  395. break;
  396. default:
  397. result = -ENODEV;
  398. goto out;
  399. }
  400. /* cpufreq holds the hotplug lock, so we are safe from here on */
  401. if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
  402. cmd.mask = policy->cpus;
  403. else
  404. cmd.mask = cpumask_of(policy->cpu);
  405. freqs.old = perf->states[perf->state].core_frequency * 1000;
  406. freqs.new = data->freq_table[next_state].frequency;
  407. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  408. drv_write(&cmd);
  409. if (acpi_pstate_strict) {
  410. if (!check_freqs(cmd.mask, freqs.new, data)) {
  411. pr_debug("acpi_cpufreq_target failed (%d)\n",
  412. policy->cpu);
  413. result = -EAGAIN;
  414. goto out;
  415. }
  416. }
  417. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  418. perf->state = next_perf_state;
  419. out:
  420. return result;
  421. }
  422. static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
  423. {
  424. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  425. pr_debug("acpi_cpufreq_verify\n");
  426. return cpufreq_frequency_table_verify(policy, data->freq_table);
  427. }
  428. static unsigned long
  429. acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
  430. {
  431. struct acpi_processor_performance *perf = data->acpi_data;
  432. if (cpu_khz) {
  433. /* search the closest match to cpu_khz */
  434. unsigned int i;
  435. unsigned long freq;
  436. unsigned long freqn = perf->states[0].core_frequency * 1000;
  437. for (i = 0; i < (perf->state_count-1); i++) {
  438. freq = freqn;
  439. freqn = perf->states[i+1].core_frequency * 1000;
  440. if ((2 * cpu_khz) > (freqn + freq)) {
  441. perf->state = i;
  442. return freq;
  443. }
  444. }
  445. perf->state = perf->state_count-1;
  446. return freqn;
  447. } else {
  448. /* assume CPU is at P0... */
  449. perf->state = 0;
  450. return perf->states[0].core_frequency * 1000;
  451. }
  452. }
  453. static void free_acpi_perf_data(void)
  454. {
  455. unsigned int i;
  456. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  457. for_each_possible_cpu(i)
  458. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  459. ->shared_cpu_map);
  460. free_percpu(acpi_perf_data);
  461. }
  462. static int boost_notify(struct notifier_block *nb, unsigned long action,
  463. void *hcpu)
  464. {
  465. unsigned cpu = (long)hcpu;
  466. const struct cpumask *cpumask;
  467. cpumask = get_cpu_mask(cpu);
  468. /*
  469. * Clear the boost-disable bit on the CPU_DOWN path so that
  470. * this cpu cannot block the remaining ones from boosting. On
  471. * the CPU_UP path we simply keep the boost-disable flag in
  472. * sync with the current global state.
  473. */
  474. switch (action) {
  475. case CPU_UP_PREPARE:
  476. case CPU_UP_PREPARE_FROZEN:
  477. boost_set_msrs(boost_enabled, cpumask);
  478. break;
  479. case CPU_DOWN_PREPARE:
  480. case CPU_DOWN_PREPARE_FROZEN:
  481. boost_set_msrs(1, cpumask);
  482. break;
  483. default:
  484. break;
  485. }
  486. return NOTIFY_OK;
  487. }
  488. static struct notifier_block boost_nb = {
  489. .notifier_call = boost_notify,
  490. };
  491. /*
  492. * acpi_cpufreq_early_init - initialize ACPI P-States library
  493. *
  494. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  495. * in order to determine correct frequency and voltage pairings. We can
  496. * do _PDC and _PSD and find out the processor dependency for the
  497. * actual init that will happen later...
  498. */
  499. static int __init acpi_cpufreq_early_init(void)
  500. {
  501. unsigned int i;
  502. pr_debug("acpi_cpufreq_early_init\n");
  503. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  504. if (!acpi_perf_data) {
  505. pr_debug("Memory allocation error for acpi_perf_data.\n");
  506. return -ENOMEM;
  507. }
  508. for_each_possible_cpu(i) {
  509. if (!zalloc_cpumask_var_node(
  510. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  511. GFP_KERNEL, cpu_to_node(i))) {
  512. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  513. free_acpi_perf_data();
  514. return -ENOMEM;
  515. }
  516. }
  517. /* Do initialization in ACPI core */
  518. acpi_processor_preregister_performance(acpi_perf_data);
  519. return 0;
  520. }
  521. #ifdef CONFIG_SMP
  522. /*
  523. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  524. * or do it in BIOS firmware and won't inform about it to OS. If not
  525. * detected, this has a side effect of making CPU run at a different speed
  526. * than OS intended it to run at. Detect it and handle it cleanly.
  527. */
  528. static int bios_with_sw_any_bug;
  529. static int sw_any_bug_found(const struct dmi_system_id *d)
  530. {
  531. bios_with_sw_any_bug = 1;
  532. return 0;
  533. }
  534. static const struct dmi_system_id sw_any_bug_dmi_table[] = {
  535. {
  536. .callback = sw_any_bug_found,
  537. .ident = "Supermicro Server X6DLP",
  538. .matches = {
  539. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  540. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  541. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  542. },
  543. },
  544. { }
  545. };
  546. static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
  547. {
  548. /* Intel Xeon Processor 7100 Series Specification Update
  549. * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
  550. * AL30: A Machine Check Exception (MCE) Occurring during an
  551. * Enhanced Intel SpeedStep Technology Ratio Change May Cause
  552. * Both Processor Cores to Lock Up. */
  553. if (c->x86_vendor == X86_VENDOR_INTEL) {
  554. if ((c->x86 == 15) &&
  555. (c->x86_model == 6) &&
  556. (c->x86_mask == 8)) {
  557. printk(KERN_INFO "acpi-cpufreq: Intel(R) "
  558. "Xeon(R) 7100 Errata AL30, processors may "
  559. "lock up on frequency changes: disabling "
  560. "acpi-cpufreq.\n");
  561. return -ENODEV;
  562. }
  563. }
  564. return 0;
  565. }
  566. #endif
  567. static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
  568. {
  569. unsigned int i;
  570. unsigned int valid_states = 0;
  571. unsigned int cpu = policy->cpu;
  572. struct acpi_cpufreq_data *data;
  573. unsigned int result = 0;
  574. struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
  575. struct acpi_processor_performance *perf;
  576. #ifdef CONFIG_SMP
  577. static int blacklisted;
  578. #endif
  579. pr_debug("acpi_cpufreq_cpu_init\n");
  580. #ifdef CONFIG_SMP
  581. if (blacklisted)
  582. return blacklisted;
  583. blacklisted = acpi_cpufreq_blacklist(c);
  584. if (blacklisted)
  585. return blacklisted;
  586. #endif
  587. data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
  588. if (!data)
  589. return -ENOMEM;
  590. data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
  591. per_cpu(acfreq_data, cpu) = data;
  592. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
  593. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  594. result = acpi_processor_register_performance(data->acpi_data, cpu);
  595. if (result)
  596. goto err_free;
  597. perf = data->acpi_data;
  598. policy->shared_type = perf->shared_type;
  599. /*
  600. * Will let policy->cpus know about dependency only when software
  601. * coordination is required.
  602. */
  603. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  604. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  605. cpumask_copy(policy->cpus, perf->shared_cpu_map);
  606. }
  607. #ifdef CONFIG_SMP
  608. dmi_check_system(sw_any_bug_dmi_table);
  609. if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
  610. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  611. cpumask_copy(policy->cpus, cpu_core_mask(cpu));
  612. }
  613. if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
  614. cpumask_clear(policy->cpus);
  615. cpumask_set_cpu(cpu, policy->cpus);
  616. policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
  617. pr_info_once(PFX "overriding BIOS provided _PSD data\n");
  618. }
  619. #endif
  620. /* capability check */
  621. if (perf->state_count <= 1) {
  622. pr_debug("No P-States\n");
  623. result = -ENODEV;
  624. goto err_unreg;
  625. }
  626. if (perf->control_register.space_id != perf->status_register.space_id) {
  627. result = -ENODEV;
  628. goto err_unreg;
  629. }
  630. switch (perf->control_register.space_id) {
  631. case ACPI_ADR_SPACE_SYSTEM_IO:
  632. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  633. boot_cpu_data.x86 == 0xf) {
  634. pr_debug("AMD K8 systems must use native drivers.\n");
  635. result = -ENODEV;
  636. goto err_unreg;
  637. }
  638. pr_debug("SYSTEM IO addr space\n");
  639. data->cpu_feature = SYSTEM_IO_CAPABLE;
  640. break;
  641. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  642. pr_debug("HARDWARE addr space\n");
  643. if (check_est_cpu(cpu)) {
  644. data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
  645. break;
  646. }
  647. if (check_amd_hwpstate_cpu(cpu)) {
  648. data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
  649. break;
  650. }
  651. result = -ENODEV;
  652. goto err_unreg;
  653. default:
  654. pr_debug("Unknown addr space %d\n",
  655. (u32) (perf->control_register.space_id));
  656. result = -ENODEV;
  657. goto err_unreg;
  658. }
  659. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  660. (perf->state_count+1), GFP_KERNEL);
  661. if (!data->freq_table) {
  662. result = -ENOMEM;
  663. goto err_unreg;
  664. }
  665. /* detect transition latency */
  666. policy->cpuinfo.transition_latency = 0;
  667. for (i = 0; i < perf->state_count; i++) {
  668. if ((perf->states[i].transition_latency * 1000) >
  669. policy->cpuinfo.transition_latency)
  670. policy->cpuinfo.transition_latency =
  671. perf->states[i].transition_latency * 1000;
  672. }
  673. /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
  674. if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
  675. policy->cpuinfo.transition_latency > 20 * 1000) {
  676. policy->cpuinfo.transition_latency = 20 * 1000;
  677. printk_once(KERN_INFO
  678. "P-state transition latency capped at 20 uS\n");
  679. }
  680. /* table init */
  681. for (i = 0; i < perf->state_count; i++) {
  682. if (i > 0 && perf->states[i].core_frequency >=
  683. data->freq_table[valid_states-1].frequency / 1000)
  684. continue;
  685. data->freq_table[valid_states].index = i;
  686. data->freq_table[valid_states].frequency =
  687. perf->states[i].core_frequency * 1000;
  688. valid_states++;
  689. }
  690. data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
  691. perf->state = 0;
  692. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  693. if (result)
  694. goto err_freqfree;
  695. if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
  696. printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
  697. switch (perf->control_register.space_id) {
  698. case ACPI_ADR_SPACE_SYSTEM_IO:
  699. /* Current speed is unknown and not detectable by IO port */
  700. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  701. break;
  702. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  703. acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
  704. policy->cur = get_cur_freq_on_cpu(cpu);
  705. break;
  706. default:
  707. break;
  708. }
  709. /* notify BIOS that we exist */
  710. acpi_processor_notify_smm(THIS_MODULE);
  711. /* Check for APERF/MPERF support in hardware */
  712. if (boot_cpu_has(X86_FEATURE_APERFMPERF))
  713. acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
  714. pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
  715. for (i = 0; i < perf->state_count; i++)
  716. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  717. (i == perf->state ? '*' : ' '), i,
  718. (u32) perf->states[i].core_frequency,
  719. (u32) perf->states[i].power,
  720. (u32) perf->states[i].transition_latency);
  721. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  722. /*
  723. * the first call to ->target() should result in us actually
  724. * writing something to the appropriate registers.
  725. */
  726. data->resume = 1;
  727. return result;
  728. err_freqfree:
  729. kfree(data->freq_table);
  730. err_unreg:
  731. acpi_processor_unregister_performance(perf, cpu);
  732. err_free:
  733. kfree(data);
  734. per_cpu(acfreq_data, cpu) = NULL;
  735. return result;
  736. }
  737. static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  738. {
  739. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  740. pr_debug("acpi_cpufreq_cpu_exit\n");
  741. if (data) {
  742. cpufreq_frequency_table_put_attr(policy->cpu);
  743. per_cpu(acfreq_data, policy->cpu) = NULL;
  744. acpi_processor_unregister_performance(data->acpi_data,
  745. policy->cpu);
  746. kfree(data->freq_table);
  747. kfree(data);
  748. }
  749. return 0;
  750. }
  751. static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
  752. {
  753. struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
  754. pr_debug("acpi_cpufreq_resume\n");
  755. data->resume = 1;
  756. return 0;
  757. }
  758. static struct freq_attr *acpi_cpufreq_attr[] = {
  759. &cpufreq_freq_attr_scaling_available_freqs,
  760. NULL, /* this is a placeholder for cpb, do not remove */
  761. NULL,
  762. };
  763. static struct cpufreq_driver acpi_cpufreq_driver = {
  764. .verify = acpi_cpufreq_verify,
  765. .target = acpi_cpufreq_target,
  766. .bios_limit = acpi_processor_get_bios_limit,
  767. .init = acpi_cpufreq_cpu_init,
  768. .exit = acpi_cpufreq_cpu_exit,
  769. .resume = acpi_cpufreq_resume,
  770. .name = "acpi-cpufreq",
  771. .owner = THIS_MODULE,
  772. .attr = acpi_cpufreq_attr,
  773. };
  774. static void __init acpi_cpufreq_boost_init(void)
  775. {
  776. if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
  777. msrs = msrs_alloc();
  778. if (!msrs)
  779. return;
  780. boost_supported = true;
  781. boost_enabled = boost_state(0);
  782. get_online_cpus();
  783. /* Force all MSRs to the same value */
  784. boost_set_msrs(boost_enabled, cpu_online_mask);
  785. register_cpu_notifier(&boost_nb);
  786. put_online_cpus();
  787. } else
  788. global_boost.attr.mode = 0444;
  789. /* We create the boost file in any case, though for systems without
  790. * hardware support it will be read-only and hardwired to return 0.
  791. */
  792. if (sysfs_create_file(cpufreq_global_kobject, &(global_boost.attr)))
  793. pr_warn(PFX "could not register global boost sysfs file\n");
  794. else
  795. pr_debug("registered global boost sysfs file\n");
  796. }
  797. static void __exit acpi_cpufreq_boost_exit(void)
  798. {
  799. sysfs_remove_file(cpufreq_global_kobject, &(global_boost.attr));
  800. if (msrs) {
  801. unregister_cpu_notifier(&boost_nb);
  802. msrs_free(msrs);
  803. msrs = NULL;
  804. }
  805. }
  806. static int __init acpi_cpufreq_init(void)
  807. {
  808. int ret;
  809. if (acpi_disabled)
  810. return 0;
  811. pr_debug("acpi_cpufreq_init\n");
  812. ret = acpi_cpufreq_early_init();
  813. if (ret)
  814. return ret;
  815. #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
  816. /* this is a sysfs file with a strange name and an even stranger
  817. * semantic - per CPU instantiation, but system global effect.
  818. * Lets enable it only on AMD CPUs for compatibility reasons and
  819. * only if configured. This is considered legacy code, which
  820. * will probably be removed at some point in the future.
  821. */
  822. if (check_amd_hwpstate_cpu(0)) {
  823. struct freq_attr **iter;
  824. pr_debug("adding sysfs entry for cpb\n");
  825. for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
  826. ;
  827. /* make sure there is a terminator behind it */
  828. if (iter[1] == NULL)
  829. *iter = &cpb;
  830. }
  831. #endif
  832. ret = cpufreq_register_driver(&acpi_cpufreq_driver);
  833. if (ret)
  834. free_acpi_perf_data();
  835. else
  836. acpi_cpufreq_boost_init();
  837. return ret;
  838. }
  839. static void __exit acpi_cpufreq_exit(void)
  840. {
  841. pr_debug("acpi_cpufreq_exit\n");
  842. acpi_cpufreq_boost_exit();
  843. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  844. free_acpi_perf_data();
  845. }
  846. module_param(acpi_pstate_strict, uint, 0644);
  847. MODULE_PARM_DESC(acpi_pstate_strict,
  848. "value 0 or non-zero. non-zero -> strict ACPI checks are "
  849. "performed during frequency changes.");
  850. late_initcall(acpi_cpufreq_init);
  851. module_exit(acpi_cpufreq_exit);
  852. static const struct x86_cpu_id acpi_cpufreq_ids[] = {
  853. X86_FEATURE_MATCH(X86_FEATURE_ACPI),
  854. X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
  855. {}
  856. };
  857. MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
  858. MODULE_ALIAS("acpi");