processor_idle.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * processor_idle - idle state submodule to the ACPI processor 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) 2004, 2005 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  8. * - Added processor hotplug support
  9. * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  10. * - Added support for C3 on SMP
  11. *
  12. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #define pr_fmt(fmt) "ACPI: " fmt
  27. #include <linux/module.h>
  28. #include <linux/acpi.h>
  29. #include <linux/dmi.h>
  30. #include <linux/sched.h> /* need_resched() */
  31. #include <linux/tick.h>
  32. #include <linux/cpuidle.h>
  33. #include <linux/cpu.h>
  34. #include <acpi/processor.h>
  35. /*
  36. * Include the apic definitions for x86 to have the APIC timer related defines
  37. * available also for UP (on SMP it gets magically included via linux/smp.h).
  38. * asm/acpi.h is not an option, as it would require more include magic. Also
  39. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  40. */
  41. #ifdef CONFIG_X86
  42. #include <asm/apic.h>
  43. #endif
  44. #define ACPI_PROCESSOR_CLASS "processor"
  45. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  46. ACPI_MODULE_NAME("processor_idle");
  47. #define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0)
  48. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  49. module_param(max_cstate, uint, 0000);
  50. static unsigned int nocst __read_mostly;
  51. module_param(nocst, uint, 0000);
  52. static int bm_check_disable __read_mostly;
  53. module_param(bm_check_disable, uint, 0000);
  54. static unsigned int latency_factor __read_mostly = 2;
  55. module_param(latency_factor, uint, 0644);
  56. static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
  57. struct cpuidle_driver acpi_idle_driver = {
  58. .name = "acpi_idle",
  59. .owner = THIS_MODULE,
  60. };
  61. #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
  62. static
  63. DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
  64. static int disabled_by_idle_boot_param(void)
  65. {
  66. return boot_option_idle_override == IDLE_POLL ||
  67. boot_option_idle_override == IDLE_HALT;
  68. }
  69. /*
  70. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  71. * For now disable this. Probably a bug somewhere else.
  72. *
  73. * To skip this limit, boot/load with a large max_cstate limit.
  74. */
  75. static int set_max_cstate(const struct dmi_system_id *id)
  76. {
  77. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  78. return 0;
  79. pr_notice("%s detected - limiting to C%ld max_cstate."
  80. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  81. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  82. max_cstate = (long)id->driver_data;
  83. return 0;
  84. }
  85. static const struct dmi_system_id processor_power_dmi_table[] = {
  86. { set_max_cstate, "Clevo 5600D", {
  87. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  88. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  89. (void *)2},
  90. { set_max_cstate, "Pavilion zv5000", {
  91. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  92. DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
  93. (void *)1},
  94. { set_max_cstate, "Asus L8400B", {
  95. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  96. DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
  97. (void *)1},
  98. {},
  99. };
  100. /*
  101. * Callers should disable interrupts before the call and enable
  102. * interrupts after return.
  103. */
  104. static void __cpuidle acpi_safe_halt(void)
  105. {
  106. if (!tif_need_resched()) {
  107. safe_halt();
  108. local_irq_disable();
  109. }
  110. }
  111. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  112. /*
  113. * Some BIOS implementations switch to C3 in the published C2 state.
  114. * This seems to be a common problem on AMD boxen, but other vendors
  115. * are affected too. We pick the most conservative approach: we assume
  116. * that the local APIC stops in both C2 and C3.
  117. */
  118. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  119. struct acpi_processor_cx *cx)
  120. {
  121. struct acpi_processor_power *pwr = &pr->power;
  122. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  123. if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
  124. return;
  125. if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
  126. type = ACPI_STATE_C1;
  127. /*
  128. * Check, if one of the previous states already marked the lapic
  129. * unstable
  130. */
  131. if (pwr->timer_broadcast_on_state < state)
  132. return;
  133. if (cx->type >= type)
  134. pr->power.timer_broadcast_on_state = state;
  135. }
  136. static void __lapic_timer_propagate_broadcast(void *arg)
  137. {
  138. struct acpi_processor *pr = (struct acpi_processor *) arg;
  139. if (pr->power.timer_broadcast_on_state < INT_MAX)
  140. tick_broadcast_enable();
  141. else
  142. tick_broadcast_disable();
  143. }
  144. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
  145. {
  146. smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
  147. (void *)pr, 1);
  148. }
  149. /* Power(C) State timer broadcast control */
  150. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  151. struct acpi_processor_cx *cx,
  152. int broadcast)
  153. {
  154. int state = cx - pr->power.states;
  155. if (state >= pr->power.timer_broadcast_on_state) {
  156. if (broadcast)
  157. tick_broadcast_enter();
  158. else
  159. tick_broadcast_exit();
  160. }
  161. }
  162. #else
  163. static void lapic_timer_check_state(int state, struct acpi_processor *pr,
  164. struct acpi_processor_cx *cstate) { }
  165. static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
  166. static void lapic_timer_state_broadcast(struct acpi_processor *pr,
  167. struct acpi_processor_cx *cx,
  168. int broadcast)
  169. {
  170. }
  171. #endif
  172. #if defined(CONFIG_X86)
  173. static void tsc_check_state(int state)
  174. {
  175. switch (boot_cpu_data.x86_vendor) {
  176. case X86_VENDOR_HYGON:
  177. case X86_VENDOR_AMD:
  178. case X86_VENDOR_INTEL:
  179. case X86_VENDOR_CENTAUR:
  180. /*
  181. * AMD Fam10h TSC will tick in all
  182. * C/P/S0/S1 states when this bit is set.
  183. */
  184. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  185. return;
  186. /*FALL THROUGH*/
  187. default:
  188. /* TSC could halt in idle, so notify users */
  189. if (state > ACPI_STATE_C1)
  190. mark_tsc_unstable("TSC halts in idle");
  191. }
  192. }
  193. #else
  194. static void tsc_check_state(int state) { return; }
  195. #endif
  196. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  197. {
  198. if (!pr->pblk)
  199. return -ENODEV;
  200. /* if info is obtained from pblk/fadt, type equals state */
  201. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  202. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  203. #ifndef CONFIG_HOTPLUG_CPU
  204. /*
  205. * Check for P_LVL2_UP flag before entering C2 and above on
  206. * an SMP system.
  207. */
  208. if ((num_online_cpus() > 1) &&
  209. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  210. return -ENODEV;
  211. #endif
  212. /* determine C2 and C3 address from pblk */
  213. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  214. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  215. /* determine latencies from FADT */
  216. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
  217. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
  218. /*
  219. * FADT specified C2 latency must be less than or equal to
  220. * 100 microseconds.
  221. */
  222. if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  223. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  224. "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
  225. /* invalidate C2 */
  226. pr->power.states[ACPI_STATE_C2].address = 0;
  227. }
  228. /*
  229. * FADT supplied C3 latency must be less than or equal to
  230. * 1000 microseconds.
  231. */
  232. if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  233. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  234. "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
  235. /* invalidate C3 */
  236. pr->power.states[ACPI_STATE_C3].address = 0;
  237. }
  238. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  239. "lvl2[0x%08x] lvl3[0x%08x]\n",
  240. pr->power.states[ACPI_STATE_C2].address,
  241. pr->power.states[ACPI_STATE_C3].address));
  242. return 0;
  243. }
  244. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  245. {
  246. if (!pr->power.states[ACPI_STATE_C1].valid) {
  247. /* set the first C-State to C1 */
  248. /* all processors need to support C1 */
  249. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  250. pr->power.states[ACPI_STATE_C1].valid = 1;
  251. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  252. snprintf(pr->power.states[ACPI_STATE_C1].desc,
  253. ACPI_CX_DESC_LEN, "ACPI HLT");
  254. }
  255. /* the C0 state only exists as a filler in our array */
  256. pr->power.states[ACPI_STATE_C0].valid = 1;
  257. return 0;
  258. }
  259. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  260. {
  261. acpi_status status;
  262. u64 count;
  263. int current_count;
  264. int i, ret = 0;
  265. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  266. union acpi_object *cst;
  267. if (nocst)
  268. return -ENODEV;
  269. current_count = 0;
  270. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  271. if (ACPI_FAILURE(status)) {
  272. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  273. return -ENODEV;
  274. }
  275. cst = buffer.pointer;
  276. /* There must be at least 2 elements */
  277. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  278. pr_err("not enough elements in _CST\n");
  279. ret = -EFAULT;
  280. goto end;
  281. }
  282. count = cst->package.elements[0].integer.value;
  283. /* Validate number of power states. */
  284. if (count < 1 || count != cst->package.count - 1) {
  285. pr_err("count given by _CST is not valid\n");
  286. ret = -EFAULT;
  287. goto end;
  288. }
  289. /* Tell driver that at least _CST is supported. */
  290. pr->flags.has_cst = 1;
  291. for (i = 1; i <= count; i++) {
  292. union acpi_object *element;
  293. union acpi_object *obj;
  294. struct acpi_power_register *reg;
  295. struct acpi_processor_cx cx;
  296. memset(&cx, 0, sizeof(cx));
  297. element = &(cst->package.elements[i]);
  298. if (element->type != ACPI_TYPE_PACKAGE)
  299. continue;
  300. if (element->package.count != 4)
  301. continue;
  302. obj = &(element->package.elements[0]);
  303. if (obj->type != ACPI_TYPE_BUFFER)
  304. continue;
  305. reg = (struct acpi_power_register *)obj->buffer.pointer;
  306. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  307. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  308. continue;
  309. /* There should be an easy way to extract an integer... */
  310. obj = &(element->package.elements[1]);
  311. if (obj->type != ACPI_TYPE_INTEGER)
  312. continue;
  313. cx.type = obj->integer.value;
  314. /*
  315. * Some buggy BIOSes won't list C1 in _CST -
  316. * Let acpi_processor_get_power_info_default() handle them later
  317. */
  318. if (i == 1 && cx.type != ACPI_STATE_C1)
  319. current_count++;
  320. cx.address = reg->address;
  321. cx.index = current_count + 1;
  322. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  323. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  324. if (acpi_processor_ffh_cstate_probe
  325. (pr->id, &cx, reg) == 0) {
  326. cx.entry_method = ACPI_CSTATE_FFH;
  327. } else if (cx.type == ACPI_STATE_C1) {
  328. /*
  329. * C1 is a special case where FIXED_HARDWARE
  330. * can be handled in non-MWAIT way as well.
  331. * In that case, save this _CST entry info.
  332. * Otherwise, ignore this info and continue.
  333. */
  334. cx.entry_method = ACPI_CSTATE_HALT;
  335. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  336. } else {
  337. continue;
  338. }
  339. if (cx.type == ACPI_STATE_C1 &&
  340. (boot_option_idle_override == IDLE_NOMWAIT)) {
  341. /*
  342. * In most cases the C1 space_id obtained from
  343. * _CST object is FIXED_HARDWARE access mode.
  344. * But when the option of idle=halt is added,
  345. * the entry_method type should be changed from
  346. * CSTATE_FFH to CSTATE_HALT.
  347. * When the option of idle=nomwait is added,
  348. * the C1 entry_method type should be
  349. * CSTATE_HALT.
  350. */
  351. cx.entry_method = ACPI_CSTATE_HALT;
  352. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  353. }
  354. } else {
  355. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  356. cx.address);
  357. }
  358. if (cx.type == ACPI_STATE_C1) {
  359. cx.valid = 1;
  360. }
  361. obj = &(element->package.elements[2]);
  362. if (obj->type != ACPI_TYPE_INTEGER)
  363. continue;
  364. cx.latency = obj->integer.value;
  365. obj = &(element->package.elements[3]);
  366. if (obj->type != ACPI_TYPE_INTEGER)
  367. continue;
  368. current_count++;
  369. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  370. /*
  371. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  372. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  373. */
  374. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  375. pr_warn("Limiting number of power states to max (%d)\n",
  376. ACPI_PROCESSOR_MAX_POWER);
  377. pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  378. break;
  379. }
  380. }
  381. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  382. current_count));
  383. /* Validate number of power states discovered */
  384. if (current_count < 2)
  385. ret = -EFAULT;
  386. end:
  387. kfree(buffer.pointer);
  388. return ret;
  389. }
  390. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  391. struct acpi_processor_cx *cx)
  392. {
  393. static int bm_check_flag = -1;
  394. static int bm_control_flag = -1;
  395. if (!cx->address)
  396. return;
  397. /*
  398. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  399. * DMA transfers are used by any ISA device to avoid livelock.
  400. * Note that we could disable Type-F DMA (as recommended by
  401. * the erratum), but this is known to disrupt certain ISA
  402. * devices thus we take the conservative approach.
  403. */
  404. else if (errata.piix4.fdma) {
  405. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  406. "C3 not supported on PIIX4 with Type-F DMA\n"));
  407. return;
  408. }
  409. /* All the logic here assumes flags.bm_check is same across all CPUs */
  410. if (bm_check_flag == -1) {
  411. /* Determine whether bm_check is needed based on CPU */
  412. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  413. bm_check_flag = pr->flags.bm_check;
  414. bm_control_flag = pr->flags.bm_control;
  415. } else {
  416. pr->flags.bm_check = bm_check_flag;
  417. pr->flags.bm_control = bm_control_flag;
  418. }
  419. if (pr->flags.bm_check) {
  420. if (!pr->flags.bm_control) {
  421. if (pr->flags.has_cst != 1) {
  422. /* bus mastering control is necessary */
  423. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  424. "C3 support requires BM control\n"));
  425. return;
  426. } else {
  427. /* Here we enter C3 without bus mastering */
  428. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  429. "C3 support without BM control\n"));
  430. }
  431. }
  432. } else {
  433. /*
  434. * WBINVD should be set in fadt, for C3 state to be
  435. * supported on when bm_check is not required.
  436. */
  437. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  438. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  439. "Cache invalidation should work properly"
  440. " for C3 to be enabled on SMP systems\n"));
  441. return;
  442. }
  443. }
  444. /*
  445. * Otherwise we've met all of our C3 requirements.
  446. * Normalize the C3 latency to expidite policy. Enable
  447. * checking of bus mastering status (bm_check) so we can
  448. * use this in our C3 policy
  449. */
  450. cx->valid = 1;
  451. /*
  452. * On older chipsets, BM_RLD needs to be set
  453. * in order for Bus Master activity to wake the
  454. * system from C3. Newer chipsets handle DMA
  455. * during C3 automatically and BM_RLD is a NOP.
  456. * In either case, the proper way to
  457. * handle BM_RLD is to set it and leave it set.
  458. */
  459. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  460. return;
  461. }
  462. static int acpi_processor_power_verify(struct acpi_processor *pr)
  463. {
  464. unsigned int i;
  465. unsigned int working = 0;
  466. pr->power.timer_broadcast_on_state = INT_MAX;
  467. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  468. struct acpi_processor_cx *cx = &pr->power.states[i];
  469. switch (cx->type) {
  470. case ACPI_STATE_C1:
  471. cx->valid = 1;
  472. break;
  473. case ACPI_STATE_C2:
  474. if (!cx->address)
  475. break;
  476. cx->valid = 1;
  477. break;
  478. case ACPI_STATE_C3:
  479. acpi_processor_power_verify_c3(pr, cx);
  480. break;
  481. }
  482. if (!cx->valid)
  483. continue;
  484. lapic_timer_check_state(i, pr, cx);
  485. tsc_check_state(cx->type);
  486. working++;
  487. }
  488. lapic_timer_propagate_broadcast(pr);
  489. return (working);
  490. }
  491. static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
  492. {
  493. unsigned int i;
  494. int result;
  495. /* NOTE: the idle thread may not be running while calling
  496. * this function */
  497. /* Zero initialize all the C-states info. */
  498. memset(pr->power.states, 0, sizeof(pr->power.states));
  499. result = acpi_processor_get_power_info_cst(pr);
  500. if (result == -ENODEV)
  501. result = acpi_processor_get_power_info_fadt(pr);
  502. if (result)
  503. return result;
  504. acpi_processor_get_power_info_default(pr);
  505. pr->power.count = acpi_processor_power_verify(pr);
  506. /*
  507. * if one state of type C2 or C3 is available, mark this
  508. * CPU as being "idle manageable"
  509. */
  510. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  511. if (pr->power.states[i].valid) {
  512. pr->power.count = i;
  513. if (pr->power.states[i].type >= ACPI_STATE_C2)
  514. pr->flags.power = 1;
  515. }
  516. }
  517. return 0;
  518. }
  519. /**
  520. * acpi_idle_bm_check - checks if bus master activity was detected
  521. */
  522. static int acpi_idle_bm_check(void)
  523. {
  524. u32 bm_status = 0;
  525. if (bm_check_disable)
  526. return 0;
  527. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  528. if (bm_status)
  529. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  530. /*
  531. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  532. * the true state of bus mastering activity; forcing us to
  533. * manually check the BMIDEA bit of each IDE channel.
  534. */
  535. else if (errata.piix4.bmisx) {
  536. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  537. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  538. bm_status = 1;
  539. }
  540. return bm_status;
  541. }
  542. /**
  543. * acpi_idle_do_entry - enter idle state using the appropriate method
  544. * @cx: cstate data
  545. *
  546. * Caller disables interrupt before call and enables interrupt after return.
  547. */
  548. static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
  549. {
  550. if (cx->entry_method == ACPI_CSTATE_FFH) {
  551. /* Call into architectural FFH based C-state */
  552. acpi_processor_ffh_cstate_enter(cx);
  553. } else if (cx->entry_method == ACPI_CSTATE_HALT) {
  554. acpi_safe_halt();
  555. } else {
  556. /* IO port based C-state */
  557. inb(cx->address);
  558. /* Dummy wait op - must do something useless after P_LVL2 read
  559. because chipsets cannot guarantee that STPCLK# signal
  560. gets asserted in time to freeze execution properly. */
  561. inl(acpi_gbl_FADT.xpm_timer_block.address);
  562. }
  563. }
  564. /**
  565. * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
  566. * @dev: the target CPU
  567. * @index: the index of suggested state
  568. */
  569. static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
  570. {
  571. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  572. ACPI_FLUSH_CPU_CACHE();
  573. while (1) {
  574. if (cx->entry_method == ACPI_CSTATE_HALT)
  575. safe_halt();
  576. else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
  577. inb(cx->address);
  578. /* See comment in acpi_idle_do_entry() */
  579. inl(acpi_gbl_FADT.xpm_timer_block.address);
  580. } else
  581. return -ENODEV;
  582. }
  583. /* Never reached */
  584. return 0;
  585. }
  586. static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
  587. {
  588. return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
  589. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
  590. }
  591. static int c3_cpu_count;
  592. static DEFINE_RAW_SPINLOCK(c3_lock);
  593. /**
  594. * acpi_idle_enter_bm - enters C3 with proper BM handling
  595. * @pr: Target processor
  596. * @cx: Target state context
  597. * @timer_bc: Whether or not to change timer mode to broadcast
  598. */
  599. static void acpi_idle_enter_bm(struct acpi_processor *pr,
  600. struct acpi_processor_cx *cx, bool timer_bc)
  601. {
  602. acpi_unlazy_tlb(smp_processor_id());
  603. /*
  604. * Must be done before busmaster disable as we might need to
  605. * access HPET !
  606. */
  607. if (timer_bc)
  608. lapic_timer_state_broadcast(pr, cx, 1);
  609. /*
  610. * disable bus master
  611. * bm_check implies we need ARB_DIS
  612. * bm_control implies whether we can do ARB_DIS
  613. *
  614. * That leaves a case where bm_check is set and bm_control is
  615. * not set. In that case we cannot do much, we enter C3
  616. * without doing anything.
  617. */
  618. if (pr->flags.bm_control) {
  619. raw_spin_lock(&c3_lock);
  620. c3_cpu_count++;
  621. /* Disable bus master arbitration when all CPUs are in C3 */
  622. if (c3_cpu_count == num_online_cpus())
  623. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  624. raw_spin_unlock(&c3_lock);
  625. }
  626. acpi_idle_do_entry(cx);
  627. /* Re-enable bus master arbitration */
  628. if (pr->flags.bm_control) {
  629. raw_spin_lock(&c3_lock);
  630. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  631. c3_cpu_count--;
  632. raw_spin_unlock(&c3_lock);
  633. }
  634. if (timer_bc)
  635. lapic_timer_state_broadcast(pr, cx, 0);
  636. }
  637. static int acpi_idle_enter(struct cpuidle_device *dev,
  638. struct cpuidle_driver *drv, int index)
  639. {
  640. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  641. struct acpi_processor *pr;
  642. pr = __this_cpu_read(processors);
  643. if (unlikely(!pr))
  644. return -EINVAL;
  645. if (cx->type != ACPI_STATE_C1) {
  646. if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
  647. index = ACPI_IDLE_STATE_START;
  648. cx = per_cpu(acpi_cstate[index], dev->cpu);
  649. } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
  650. if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
  651. acpi_idle_enter_bm(pr, cx, true);
  652. return index;
  653. } else if (drv->safe_state_index >= 0) {
  654. index = drv->safe_state_index;
  655. cx = per_cpu(acpi_cstate[index], dev->cpu);
  656. } else {
  657. acpi_safe_halt();
  658. return -EBUSY;
  659. }
  660. }
  661. }
  662. lapic_timer_state_broadcast(pr, cx, 1);
  663. if (cx->type == ACPI_STATE_C3)
  664. ACPI_FLUSH_CPU_CACHE();
  665. acpi_idle_do_entry(cx);
  666. lapic_timer_state_broadcast(pr, cx, 0);
  667. return index;
  668. }
  669. static void acpi_idle_enter_s2idle(struct cpuidle_device *dev,
  670. struct cpuidle_driver *drv, int index)
  671. {
  672. struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
  673. if (cx->type == ACPI_STATE_C3) {
  674. struct acpi_processor *pr = __this_cpu_read(processors);
  675. if (unlikely(!pr))
  676. return;
  677. if (pr->flags.bm_check) {
  678. acpi_idle_enter_bm(pr, cx, false);
  679. return;
  680. } else {
  681. ACPI_FLUSH_CPU_CACHE();
  682. }
  683. }
  684. acpi_idle_do_entry(cx);
  685. }
  686. static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
  687. struct cpuidle_device *dev)
  688. {
  689. int i, count = ACPI_IDLE_STATE_START;
  690. struct acpi_processor_cx *cx;
  691. if (max_cstate == 0)
  692. max_cstate = 1;
  693. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  694. cx = &pr->power.states[i];
  695. if (!cx->valid)
  696. continue;
  697. per_cpu(acpi_cstate[count], dev->cpu) = cx;
  698. count++;
  699. if (count == CPUIDLE_STATE_MAX)
  700. break;
  701. }
  702. if (!count)
  703. return -EINVAL;
  704. return 0;
  705. }
  706. static int acpi_processor_setup_cstates(struct acpi_processor *pr)
  707. {
  708. int i, count;
  709. struct acpi_processor_cx *cx;
  710. struct cpuidle_state *state;
  711. struct cpuidle_driver *drv = &acpi_idle_driver;
  712. if (max_cstate == 0)
  713. max_cstate = 1;
  714. if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) {
  715. cpuidle_poll_state_init(drv);
  716. count = 1;
  717. } else {
  718. count = 0;
  719. }
  720. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  721. cx = &pr->power.states[i];
  722. if (!cx->valid)
  723. continue;
  724. state = &drv->states[count];
  725. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  726. strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  727. state->exit_latency = cx->latency;
  728. state->target_residency = cx->latency * latency_factor;
  729. state->enter = acpi_idle_enter;
  730. state->flags = 0;
  731. if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
  732. state->enter_dead = acpi_idle_play_dead;
  733. drv->safe_state_index = count;
  734. }
  735. /*
  736. * Halt-induced C1 is not good for ->enter_s2idle, because it
  737. * re-enables interrupts on exit. Moreover, C1 is generally not
  738. * particularly interesting from the suspend-to-idle angle, so
  739. * avoid C1 and the situations in which we may need to fall back
  740. * to it altogether.
  741. */
  742. if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
  743. state->enter_s2idle = acpi_idle_enter_s2idle;
  744. count++;
  745. if (count == CPUIDLE_STATE_MAX)
  746. break;
  747. }
  748. drv->state_count = count;
  749. if (!count)
  750. return -EINVAL;
  751. return 0;
  752. }
  753. static inline void acpi_processor_cstate_first_run_checks(void)
  754. {
  755. acpi_status status;
  756. static int first_run;
  757. if (first_run)
  758. return;
  759. dmi_check_system(processor_power_dmi_table);
  760. max_cstate = acpi_processor_cstate_check(max_cstate);
  761. if (max_cstate < ACPI_C_STATES_MAX)
  762. pr_notice("ACPI: processor limited to max C-state %d\n",
  763. max_cstate);
  764. first_run++;
  765. if (acpi_gbl_FADT.cst_control && !nocst) {
  766. status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
  767. acpi_gbl_FADT.cst_control, 8);
  768. if (ACPI_FAILURE(status))
  769. ACPI_EXCEPTION((AE_INFO, status,
  770. "Notifying BIOS of _CST ability failed"));
  771. }
  772. }
  773. #else
  774. static inline int disabled_by_idle_boot_param(void) { return 0; }
  775. static inline void acpi_processor_cstate_first_run_checks(void) { }
  776. static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
  777. {
  778. return -ENODEV;
  779. }
  780. static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
  781. struct cpuidle_device *dev)
  782. {
  783. return -EINVAL;
  784. }
  785. static int acpi_processor_setup_cstates(struct acpi_processor *pr)
  786. {
  787. return -EINVAL;
  788. }
  789. #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
  790. struct acpi_lpi_states_array {
  791. unsigned int size;
  792. unsigned int composite_states_size;
  793. struct acpi_lpi_state *entries;
  794. struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
  795. };
  796. static int obj_get_integer(union acpi_object *obj, u32 *value)
  797. {
  798. if (obj->type != ACPI_TYPE_INTEGER)
  799. return -EINVAL;
  800. *value = obj->integer.value;
  801. return 0;
  802. }
  803. static int acpi_processor_evaluate_lpi(acpi_handle handle,
  804. struct acpi_lpi_states_array *info)
  805. {
  806. acpi_status status;
  807. int ret = 0;
  808. int pkg_count, state_idx = 1, loop;
  809. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  810. union acpi_object *lpi_data;
  811. struct acpi_lpi_state *lpi_state;
  812. status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
  813. if (ACPI_FAILURE(status)) {
  814. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
  815. return -ENODEV;
  816. }
  817. lpi_data = buffer.pointer;
  818. /* There must be at least 4 elements = 3 elements + 1 package */
  819. if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
  820. lpi_data->package.count < 4) {
  821. pr_debug("not enough elements in _LPI\n");
  822. ret = -ENODATA;
  823. goto end;
  824. }
  825. pkg_count = lpi_data->package.elements[2].integer.value;
  826. /* Validate number of power states. */
  827. if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
  828. pr_debug("count given by _LPI is not valid\n");
  829. ret = -ENODATA;
  830. goto end;
  831. }
  832. lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
  833. if (!lpi_state) {
  834. ret = -ENOMEM;
  835. goto end;
  836. }
  837. info->size = pkg_count;
  838. info->entries = lpi_state;
  839. /* LPI States start at index 3 */
  840. for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
  841. union acpi_object *element, *pkg_elem, *obj;
  842. element = &lpi_data->package.elements[loop];
  843. if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
  844. continue;
  845. pkg_elem = element->package.elements;
  846. obj = pkg_elem + 6;
  847. if (obj->type == ACPI_TYPE_BUFFER) {
  848. struct acpi_power_register *reg;
  849. reg = (struct acpi_power_register *)obj->buffer.pointer;
  850. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  851. reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
  852. continue;
  853. lpi_state->address = reg->address;
  854. lpi_state->entry_method =
  855. reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
  856. ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
  857. } else if (obj->type == ACPI_TYPE_INTEGER) {
  858. lpi_state->entry_method = ACPI_CSTATE_INTEGER;
  859. lpi_state->address = obj->integer.value;
  860. } else {
  861. continue;
  862. }
  863. /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
  864. obj = pkg_elem + 9;
  865. if (obj->type == ACPI_TYPE_STRING)
  866. strlcpy(lpi_state->desc, obj->string.pointer,
  867. ACPI_CX_DESC_LEN);
  868. lpi_state->index = state_idx;
  869. if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
  870. pr_debug("No min. residency found, assuming 10 us\n");
  871. lpi_state->min_residency = 10;
  872. }
  873. if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
  874. pr_debug("No wakeup residency found, assuming 10 us\n");
  875. lpi_state->wake_latency = 10;
  876. }
  877. if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
  878. lpi_state->flags = 0;
  879. if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
  880. lpi_state->arch_flags = 0;
  881. if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
  882. lpi_state->res_cnt_freq = 1;
  883. if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
  884. lpi_state->enable_parent_state = 0;
  885. }
  886. acpi_handle_debug(handle, "Found %d power states\n", state_idx);
  887. end:
  888. kfree(buffer.pointer);
  889. return ret;
  890. }
  891. /*
  892. * flat_state_cnt - the number of composite LPI states after the process of flattening
  893. */
  894. static int flat_state_cnt;
  895. /**
  896. * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
  897. *
  898. * @local: local LPI state
  899. * @parent: parent LPI state
  900. * @result: composite LPI state
  901. */
  902. static bool combine_lpi_states(struct acpi_lpi_state *local,
  903. struct acpi_lpi_state *parent,
  904. struct acpi_lpi_state *result)
  905. {
  906. if (parent->entry_method == ACPI_CSTATE_INTEGER) {
  907. if (!parent->address) /* 0 means autopromotable */
  908. return false;
  909. result->address = local->address + parent->address;
  910. } else {
  911. result->address = parent->address;
  912. }
  913. result->min_residency = max(local->min_residency, parent->min_residency);
  914. result->wake_latency = local->wake_latency + parent->wake_latency;
  915. result->enable_parent_state = parent->enable_parent_state;
  916. result->entry_method = local->entry_method;
  917. result->flags = parent->flags;
  918. result->arch_flags = parent->arch_flags;
  919. result->index = parent->index;
  920. strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
  921. strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
  922. strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
  923. return true;
  924. }
  925. #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0)
  926. static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
  927. struct acpi_lpi_state *t)
  928. {
  929. curr_level->composite_states[curr_level->composite_states_size++] = t;
  930. }
  931. static int flatten_lpi_states(struct acpi_processor *pr,
  932. struct acpi_lpi_states_array *curr_level,
  933. struct acpi_lpi_states_array *prev_level)
  934. {
  935. int i, j, state_count = curr_level->size;
  936. struct acpi_lpi_state *p, *t = curr_level->entries;
  937. curr_level->composite_states_size = 0;
  938. for (j = 0; j < state_count; j++, t++) {
  939. struct acpi_lpi_state *flpi;
  940. if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
  941. continue;
  942. if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
  943. pr_warn("Limiting number of LPI states to max (%d)\n",
  944. ACPI_PROCESSOR_MAX_POWER);
  945. pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  946. break;
  947. }
  948. flpi = &pr->power.lpi_states[flat_state_cnt];
  949. if (!prev_level) { /* leaf/processor node */
  950. memcpy(flpi, t, sizeof(*t));
  951. stash_composite_state(curr_level, flpi);
  952. flat_state_cnt++;
  953. continue;
  954. }
  955. for (i = 0; i < prev_level->composite_states_size; i++) {
  956. p = prev_level->composite_states[i];
  957. if (t->index <= p->enable_parent_state &&
  958. combine_lpi_states(p, t, flpi)) {
  959. stash_composite_state(curr_level, flpi);
  960. flat_state_cnt++;
  961. flpi++;
  962. }
  963. }
  964. }
  965. kfree(curr_level->entries);
  966. return 0;
  967. }
  968. static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
  969. {
  970. int ret, i;
  971. acpi_status status;
  972. acpi_handle handle = pr->handle, pr_ahandle;
  973. struct acpi_device *d = NULL;
  974. struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
  975. if (!osc_pc_lpi_support_confirmed)
  976. return -EOPNOTSUPP;
  977. if (!acpi_has_method(handle, "_LPI"))
  978. return -EINVAL;
  979. flat_state_cnt = 0;
  980. prev = &info[0];
  981. curr = &info[1];
  982. handle = pr->handle;
  983. ret = acpi_processor_evaluate_lpi(handle, prev);
  984. if (ret)
  985. return ret;
  986. flatten_lpi_states(pr, prev, NULL);
  987. status = acpi_get_parent(handle, &pr_ahandle);
  988. while (ACPI_SUCCESS(status)) {
  989. acpi_bus_get_device(pr_ahandle, &d);
  990. handle = pr_ahandle;
  991. if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
  992. break;
  993. /* can be optional ? */
  994. if (!acpi_has_method(handle, "_LPI"))
  995. break;
  996. ret = acpi_processor_evaluate_lpi(handle, curr);
  997. if (ret)
  998. break;
  999. /* flatten all the LPI states in this level of hierarchy */
  1000. flatten_lpi_states(pr, curr, prev);
  1001. tmp = prev, prev = curr, curr = tmp;
  1002. status = acpi_get_parent(handle, &pr_ahandle);
  1003. }
  1004. pr->power.count = flat_state_cnt;
  1005. /* reset the index after flattening */
  1006. for (i = 0; i < pr->power.count; i++)
  1007. pr->power.lpi_states[i].index = i;
  1008. /* Tell driver that _LPI is supported. */
  1009. pr->flags.has_lpi = 1;
  1010. pr->flags.power = 1;
  1011. return 0;
  1012. }
  1013. int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
  1014. {
  1015. return -ENODEV;
  1016. }
  1017. int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
  1018. {
  1019. return -ENODEV;
  1020. }
  1021. /**
  1022. * acpi_idle_lpi_enter - enters an ACPI any LPI state
  1023. * @dev: the target CPU
  1024. * @drv: cpuidle driver containing cpuidle state info
  1025. * @index: index of target state
  1026. *
  1027. * Return: 0 for success or negative value for error
  1028. */
  1029. static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
  1030. struct cpuidle_driver *drv, int index)
  1031. {
  1032. struct acpi_processor *pr;
  1033. struct acpi_lpi_state *lpi;
  1034. pr = __this_cpu_read(processors);
  1035. if (unlikely(!pr))
  1036. return -EINVAL;
  1037. lpi = &pr->power.lpi_states[index];
  1038. if (lpi->entry_method == ACPI_CSTATE_FFH)
  1039. return acpi_processor_ffh_lpi_enter(lpi);
  1040. return -EINVAL;
  1041. }
  1042. static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
  1043. {
  1044. int i;
  1045. struct acpi_lpi_state *lpi;
  1046. struct cpuidle_state *state;
  1047. struct cpuidle_driver *drv = &acpi_idle_driver;
  1048. if (!pr->flags.has_lpi)
  1049. return -EOPNOTSUPP;
  1050. for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
  1051. lpi = &pr->power.lpi_states[i];
  1052. state = &drv->states[i];
  1053. snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
  1054. strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
  1055. state->exit_latency = lpi->wake_latency;
  1056. state->target_residency = lpi->min_residency;
  1057. if (lpi->arch_flags)
  1058. state->flags |= CPUIDLE_FLAG_TIMER_STOP;
  1059. state->enter = acpi_idle_lpi_enter;
  1060. drv->safe_state_index = i;
  1061. }
  1062. drv->state_count = i;
  1063. return 0;
  1064. }
  1065. /**
  1066. * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
  1067. * global state data i.e. idle routines
  1068. *
  1069. * @pr: the ACPI processor
  1070. */
  1071. static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
  1072. {
  1073. int i;
  1074. struct cpuidle_driver *drv = &acpi_idle_driver;
  1075. if (!pr->flags.power_setup_done || !pr->flags.power)
  1076. return -EINVAL;
  1077. drv->safe_state_index = -1;
  1078. for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
  1079. drv->states[i].name[0] = '\0';
  1080. drv->states[i].desc[0] = '\0';
  1081. }
  1082. if (pr->flags.has_lpi)
  1083. return acpi_processor_setup_lpi_states(pr);
  1084. return acpi_processor_setup_cstates(pr);
  1085. }
  1086. /**
  1087. * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
  1088. * device i.e. per-cpu data
  1089. *
  1090. * @pr: the ACPI processor
  1091. * @dev : the cpuidle device
  1092. */
  1093. static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
  1094. struct cpuidle_device *dev)
  1095. {
  1096. if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
  1097. return -EINVAL;
  1098. dev->cpu = pr->id;
  1099. if (pr->flags.has_lpi)
  1100. return acpi_processor_ffh_lpi_probe(pr->id);
  1101. return acpi_processor_setup_cpuidle_cx(pr, dev);
  1102. }
  1103. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  1104. {
  1105. int ret;
  1106. ret = acpi_processor_get_lpi_info(pr);
  1107. if (ret)
  1108. ret = acpi_processor_get_cstate_info(pr);
  1109. return ret;
  1110. }
  1111. int acpi_processor_hotplug(struct acpi_processor *pr)
  1112. {
  1113. int ret = 0;
  1114. struct cpuidle_device *dev;
  1115. if (disabled_by_idle_boot_param())
  1116. return 0;
  1117. if (!pr->flags.power_setup_done)
  1118. return -ENODEV;
  1119. dev = per_cpu(acpi_cpuidle_device, pr->id);
  1120. cpuidle_pause_and_lock();
  1121. cpuidle_disable_device(dev);
  1122. ret = acpi_processor_get_power_info(pr);
  1123. if (!ret && pr->flags.power) {
  1124. acpi_processor_setup_cpuidle_dev(pr, dev);
  1125. ret = cpuidle_enable_device(dev);
  1126. }
  1127. cpuidle_resume_and_unlock();
  1128. return ret;
  1129. }
  1130. int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
  1131. {
  1132. int cpu;
  1133. struct acpi_processor *_pr;
  1134. struct cpuidle_device *dev;
  1135. if (disabled_by_idle_boot_param())
  1136. return 0;
  1137. if (!pr->flags.power_setup_done)
  1138. return -ENODEV;
  1139. /*
  1140. * FIXME: Design the ACPI notification to make it once per
  1141. * system instead of once per-cpu. This condition is a hack
  1142. * to make the code that updates C-States be called once.
  1143. */
  1144. if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
  1145. /* Protect against cpu-hotplug */
  1146. get_online_cpus();
  1147. cpuidle_pause_and_lock();
  1148. /* Disable all cpuidle devices */
  1149. for_each_online_cpu(cpu) {
  1150. _pr = per_cpu(processors, cpu);
  1151. if (!_pr || !_pr->flags.power_setup_done)
  1152. continue;
  1153. dev = per_cpu(acpi_cpuidle_device, cpu);
  1154. cpuidle_disable_device(dev);
  1155. }
  1156. /* Populate Updated C-state information */
  1157. acpi_processor_get_power_info(pr);
  1158. acpi_processor_setup_cpuidle_states(pr);
  1159. /* Enable all cpuidle devices */
  1160. for_each_online_cpu(cpu) {
  1161. _pr = per_cpu(processors, cpu);
  1162. if (!_pr || !_pr->flags.power_setup_done)
  1163. continue;
  1164. acpi_processor_get_power_info(_pr);
  1165. if (_pr->flags.power) {
  1166. dev = per_cpu(acpi_cpuidle_device, cpu);
  1167. acpi_processor_setup_cpuidle_dev(_pr, dev);
  1168. cpuidle_enable_device(dev);
  1169. }
  1170. }
  1171. cpuidle_resume_and_unlock();
  1172. put_online_cpus();
  1173. }
  1174. return 0;
  1175. }
  1176. static int acpi_processor_registered;
  1177. int acpi_processor_power_init(struct acpi_processor *pr)
  1178. {
  1179. int retval;
  1180. struct cpuidle_device *dev;
  1181. if (disabled_by_idle_boot_param())
  1182. return 0;
  1183. acpi_processor_cstate_first_run_checks();
  1184. if (!acpi_processor_get_power_info(pr))
  1185. pr->flags.power_setup_done = 1;
  1186. /*
  1187. * Install the idle handler if processor power management is supported.
  1188. * Note that we use previously set idle handler will be used on
  1189. * platforms that only support C1.
  1190. */
  1191. if (pr->flags.power) {
  1192. /* Register acpi_idle_driver if not already registered */
  1193. if (!acpi_processor_registered) {
  1194. acpi_processor_setup_cpuidle_states(pr);
  1195. retval = cpuidle_register_driver(&acpi_idle_driver);
  1196. if (retval)
  1197. return retval;
  1198. pr_debug("%s registered with cpuidle\n",
  1199. acpi_idle_driver.name);
  1200. }
  1201. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1202. if (!dev)
  1203. return -ENOMEM;
  1204. per_cpu(acpi_cpuidle_device, pr->id) = dev;
  1205. acpi_processor_setup_cpuidle_dev(pr, dev);
  1206. /* Register per-cpu cpuidle_device. Cpuidle driver
  1207. * must already be registered before registering device
  1208. */
  1209. retval = cpuidle_register_device(dev);
  1210. if (retval) {
  1211. if (acpi_processor_registered == 0)
  1212. cpuidle_unregister_driver(&acpi_idle_driver);
  1213. return retval;
  1214. }
  1215. acpi_processor_registered++;
  1216. }
  1217. return 0;
  1218. }
  1219. int acpi_processor_power_exit(struct acpi_processor *pr)
  1220. {
  1221. struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
  1222. if (disabled_by_idle_boot_param())
  1223. return 0;
  1224. if (pr->flags.power) {
  1225. cpuidle_unregister_device(dev);
  1226. acpi_processor_registered--;
  1227. if (acpi_processor_registered == 0)
  1228. cpuidle_unregister_driver(&acpi_idle_driver);
  1229. }
  1230. pr->flags.power_setup_done = 0;
  1231. return 0;
  1232. }