processor_idle.c 38 KB

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