acpi-cpufreq.c 25 KB

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