processor_idle.c 38 KB

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