processor_idle.c 38 KB

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