acpi-cpufreq.c 26 KB

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