processor_idle.c 38 KB

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