intel_idle.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. * intel_idle.c - native hardware idle loop for modern Intel processors
  3. *
  4. * Copyright (c) 2013, Intel Corporation.
  5. * Len Brown <len.brown@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. /*
  21. * intel_idle is a cpuidle driver that loads on specific Intel processors
  22. * in lieu of the legacy ACPI processor_idle driver. The intent is to
  23. * make Linux more efficient on these processors, as intel_idle knows
  24. * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
  25. */
  26. /*
  27. * Design Assumptions
  28. *
  29. * All CPUs have same idle states as boot CPU
  30. *
  31. * Chipset BM_STS (bus master status) bit is a NOP
  32. * for preventing entry into deep C-stats
  33. */
  34. /*
  35. * Known limitations
  36. *
  37. * The driver currently initializes for_each_online_cpu() upon modprobe.
  38. * It it unaware of subsequent processors hot-added to the system.
  39. * This means that if you boot with maxcpus=n and later online
  40. * processors above n, those processors will use C1 only.
  41. *
  42. * ACPI has a .suspend hack to turn off deep c-statees during suspend
  43. * to avoid complications with the lapic timer workaround.
  44. * Have not seen issues with suspend, but may need same workaround here.
  45. *
  46. */
  47. /* un-comment DEBUG to enable pr_debug() statements */
  48. #define DEBUG
  49. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  50. #include <linux/kernel.h>
  51. #include <linux/cpuidle.h>
  52. #include <linux/tick.h>
  53. #include <trace/events/power.h>
  54. #include <linux/sched.h>
  55. #include <linux/notifier.h>
  56. #include <linux/cpu.h>
  57. #include <linux/moduleparam.h>
  58. #include <asm/cpu_device_id.h>
  59. #include <asm/intel-family.h>
  60. #include <asm/mwait.h>
  61. #include <asm/msr.h>
  62. #define INTEL_IDLE_VERSION "0.4.1"
  63. static struct cpuidle_driver intel_idle_driver = {
  64. .name = "intel_idle",
  65. .owner = THIS_MODULE,
  66. };
  67. /* intel_idle.max_cstate=0 disables driver */
  68. static int max_cstate = CPUIDLE_STATE_MAX - 1;
  69. static unsigned int mwait_substates;
  70. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  71. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  72. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  73. struct idle_cpu {
  74. struct cpuidle_state *state_table;
  75. /*
  76. * Hardware C-state auto-demotion may not always be optimal.
  77. * Indicate which enable bits to clear here.
  78. */
  79. unsigned long auto_demotion_disable_flags;
  80. bool byt_auto_demotion_disable_flag;
  81. bool disable_promotion_to_c1e;
  82. };
  83. static const struct idle_cpu *icpu;
  84. static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
  85. static int intel_idle(struct cpuidle_device *dev,
  86. struct cpuidle_driver *drv, int index);
  87. static void intel_idle_s2idle(struct cpuidle_device *dev,
  88. struct cpuidle_driver *drv, int index);
  89. static struct cpuidle_state *cpuidle_state_table;
  90. /*
  91. * Set this flag for states where the HW flushes the TLB for us
  92. * and so we don't need cross-calls to keep it consistent.
  93. * If this flag is set, SW flushes the TLB, so even if the
  94. * HW doesn't do the flushing, this flag is safe to use.
  95. */
  96. #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
  97. /*
  98. * MWAIT takes an 8-bit "hint" in EAX "suggesting"
  99. * the C-state (top nibble) and sub-state (bottom nibble)
  100. * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
  101. *
  102. * We store the hint at the top of our "flags" for each state.
  103. */
  104. #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
  105. #define MWAIT2flg(eax) ((eax & 0xFF) << 24)
  106. /*
  107. * States are indexed by the cstate number,
  108. * which is also the index into the MWAIT hint array.
  109. * Thus C0 is a dummy.
  110. */
  111. static struct cpuidle_state nehalem_cstates[] = {
  112. {
  113. .name = "C1",
  114. .desc = "MWAIT 0x00",
  115. .flags = MWAIT2flg(0x00),
  116. .exit_latency = 3,
  117. .target_residency = 6,
  118. .enter = &intel_idle,
  119. .enter_s2idle = intel_idle_s2idle, },
  120. {
  121. .name = "C1E",
  122. .desc = "MWAIT 0x01",
  123. .flags = MWAIT2flg(0x01),
  124. .exit_latency = 10,
  125. .target_residency = 20,
  126. .enter = &intel_idle,
  127. .enter_s2idle = intel_idle_s2idle, },
  128. {
  129. .name = "C3",
  130. .desc = "MWAIT 0x10",
  131. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  132. .exit_latency = 20,
  133. .target_residency = 80,
  134. .enter = &intel_idle,
  135. .enter_s2idle = intel_idle_s2idle, },
  136. {
  137. .name = "C6",
  138. .desc = "MWAIT 0x20",
  139. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  140. .exit_latency = 200,
  141. .target_residency = 800,
  142. .enter = &intel_idle,
  143. .enter_s2idle = intel_idle_s2idle, },
  144. {
  145. .enter = NULL }
  146. };
  147. static struct cpuidle_state snb_cstates[] = {
  148. {
  149. .name = "C1",
  150. .desc = "MWAIT 0x00",
  151. .flags = MWAIT2flg(0x00),
  152. .exit_latency = 2,
  153. .target_residency = 2,
  154. .enter = &intel_idle,
  155. .enter_s2idle = intel_idle_s2idle, },
  156. {
  157. .name = "C1E",
  158. .desc = "MWAIT 0x01",
  159. .flags = MWAIT2flg(0x01),
  160. .exit_latency = 10,
  161. .target_residency = 20,
  162. .enter = &intel_idle,
  163. .enter_s2idle = intel_idle_s2idle, },
  164. {
  165. .name = "C3",
  166. .desc = "MWAIT 0x10",
  167. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  168. .exit_latency = 80,
  169. .target_residency = 211,
  170. .enter = &intel_idle,
  171. .enter_s2idle = intel_idle_s2idle, },
  172. {
  173. .name = "C6",
  174. .desc = "MWAIT 0x20",
  175. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  176. .exit_latency = 104,
  177. .target_residency = 345,
  178. .enter = &intel_idle,
  179. .enter_s2idle = intel_idle_s2idle, },
  180. {
  181. .name = "C7",
  182. .desc = "MWAIT 0x30",
  183. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  184. .exit_latency = 109,
  185. .target_residency = 345,
  186. .enter = &intel_idle,
  187. .enter_s2idle = intel_idle_s2idle, },
  188. {
  189. .enter = NULL }
  190. };
  191. static struct cpuidle_state byt_cstates[] = {
  192. {
  193. .name = "C1",
  194. .desc = "MWAIT 0x00",
  195. .flags = MWAIT2flg(0x00),
  196. .exit_latency = 1,
  197. .target_residency = 1,
  198. .enter = &intel_idle,
  199. .enter_s2idle = intel_idle_s2idle, },
  200. {
  201. .name = "C6N",
  202. .desc = "MWAIT 0x58",
  203. .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
  204. .exit_latency = 300,
  205. .target_residency = 275,
  206. .enter = &intel_idle,
  207. .enter_s2idle = intel_idle_s2idle, },
  208. {
  209. .name = "C6S",
  210. .desc = "MWAIT 0x52",
  211. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  212. .exit_latency = 500,
  213. .target_residency = 560,
  214. .enter = &intel_idle,
  215. .enter_s2idle = intel_idle_s2idle, },
  216. {
  217. .name = "C7",
  218. .desc = "MWAIT 0x60",
  219. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  220. .exit_latency = 1200,
  221. .target_residency = 4000,
  222. .enter = &intel_idle,
  223. .enter_s2idle = intel_idle_s2idle, },
  224. {
  225. .name = "C7S",
  226. .desc = "MWAIT 0x64",
  227. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  228. .exit_latency = 10000,
  229. .target_residency = 20000,
  230. .enter = &intel_idle,
  231. .enter_s2idle = intel_idle_s2idle, },
  232. {
  233. .enter = NULL }
  234. };
  235. static struct cpuidle_state cht_cstates[] = {
  236. {
  237. .name = "C1",
  238. .desc = "MWAIT 0x00",
  239. .flags = MWAIT2flg(0x00),
  240. .exit_latency = 1,
  241. .target_residency = 1,
  242. .enter = &intel_idle,
  243. .enter_s2idle = intel_idle_s2idle, },
  244. {
  245. .name = "C6N",
  246. .desc = "MWAIT 0x58",
  247. .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
  248. .exit_latency = 80,
  249. .target_residency = 275,
  250. .enter = &intel_idle,
  251. .enter_s2idle = intel_idle_s2idle, },
  252. {
  253. .name = "C6S",
  254. .desc = "MWAIT 0x52",
  255. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  256. .exit_latency = 200,
  257. .target_residency = 560,
  258. .enter = &intel_idle,
  259. .enter_s2idle = intel_idle_s2idle, },
  260. {
  261. .name = "C7",
  262. .desc = "MWAIT 0x60",
  263. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  264. .exit_latency = 1200,
  265. .target_residency = 4000,
  266. .enter = &intel_idle,
  267. .enter_s2idle = intel_idle_s2idle, },
  268. {
  269. .name = "C7S",
  270. .desc = "MWAIT 0x64",
  271. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  272. .exit_latency = 10000,
  273. .target_residency = 20000,
  274. .enter = &intel_idle,
  275. .enter_s2idle = intel_idle_s2idle, },
  276. {
  277. .enter = NULL }
  278. };
  279. static struct cpuidle_state ivb_cstates[] = {
  280. {
  281. .name = "C1",
  282. .desc = "MWAIT 0x00",
  283. .flags = MWAIT2flg(0x00),
  284. .exit_latency = 1,
  285. .target_residency = 1,
  286. .enter = &intel_idle,
  287. .enter_s2idle = intel_idle_s2idle, },
  288. {
  289. .name = "C1E",
  290. .desc = "MWAIT 0x01",
  291. .flags = MWAIT2flg(0x01),
  292. .exit_latency = 10,
  293. .target_residency = 20,
  294. .enter = &intel_idle,
  295. .enter_s2idle = intel_idle_s2idle, },
  296. {
  297. .name = "C3",
  298. .desc = "MWAIT 0x10",
  299. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  300. .exit_latency = 59,
  301. .target_residency = 156,
  302. .enter = &intel_idle,
  303. .enter_s2idle = intel_idle_s2idle, },
  304. {
  305. .name = "C6",
  306. .desc = "MWAIT 0x20",
  307. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  308. .exit_latency = 80,
  309. .target_residency = 300,
  310. .enter = &intel_idle,
  311. .enter_s2idle = intel_idle_s2idle, },
  312. {
  313. .name = "C7",
  314. .desc = "MWAIT 0x30",
  315. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  316. .exit_latency = 87,
  317. .target_residency = 300,
  318. .enter = &intel_idle,
  319. .enter_s2idle = intel_idle_s2idle, },
  320. {
  321. .enter = NULL }
  322. };
  323. static struct cpuidle_state ivt_cstates[] = {
  324. {
  325. .name = "C1",
  326. .desc = "MWAIT 0x00",
  327. .flags = MWAIT2flg(0x00),
  328. .exit_latency = 1,
  329. .target_residency = 1,
  330. .enter = &intel_idle,
  331. .enter_s2idle = intel_idle_s2idle, },
  332. {
  333. .name = "C1E",
  334. .desc = "MWAIT 0x01",
  335. .flags = MWAIT2flg(0x01),
  336. .exit_latency = 10,
  337. .target_residency = 80,
  338. .enter = &intel_idle,
  339. .enter_s2idle = intel_idle_s2idle, },
  340. {
  341. .name = "C3",
  342. .desc = "MWAIT 0x10",
  343. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  344. .exit_latency = 59,
  345. .target_residency = 156,
  346. .enter = &intel_idle,
  347. .enter_s2idle = intel_idle_s2idle, },
  348. {
  349. .name = "C6",
  350. .desc = "MWAIT 0x20",
  351. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  352. .exit_latency = 82,
  353. .target_residency = 300,
  354. .enter = &intel_idle,
  355. .enter_s2idle = intel_idle_s2idle, },
  356. {
  357. .enter = NULL }
  358. };
  359. static struct cpuidle_state ivt_cstates_4s[] = {
  360. {
  361. .name = "C1",
  362. .desc = "MWAIT 0x00",
  363. .flags = MWAIT2flg(0x00),
  364. .exit_latency = 1,
  365. .target_residency = 1,
  366. .enter = &intel_idle,
  367. .enter_s2idle = intel_idle_s2idle, },
  368. {
  369. .name = "C1E",
  370. .desc = "MWAIT 0x01",
  371. .flags = MWAIT2flg(0x01),
  372. .exit_latency = 10,
  373. .target_residency = 250,
  374. .enter = &intel_idle,
  375. .enter_s2idle = intel_idle_s2idle, },
  376. {
  377. .name = "C3",
  378. .desc = "MWAIT 0x10",
  379. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  380. .exit_latency = 59,
  381. .target_residency = 300,
  382. .enter = &intel_idle,
  383. .enter_s2idle = intel_idle_s2idle, },
  384. {
  385. .name = "C6",
  386. .desc = "MWAIT 0x20",
  387. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  388. .exit_latency = 84,
  389. .target_residency = 400,
  390. .enter = &intel_idle,
  391. .enter_s2idle = intel_idle_s2idle, },
  392. {
  393. .enter = NULL }
  394. };
  395. static struct cpuidle_state ivt_cstates_8s[] = {
  396. {
  397. .name = "C1",
  398. .desc = "MWAIT 0x00",
  399. .flags = MWAIT2flg(0x00),
  400. .exit_latency = 1,
  401. .target_residency = 1,
  402. .enter = &intel_idle,
  403. .enter_s2idle = intel_idle_s2idle, },
  404. {
  405. .name = "C1E",
  406. .desc = "MWAIT 0x01",
  407. .flags = MWAIT2flg(0x01),
  408. .exit_latency = 10,
  409. .target_residency = 500,
  410. .enter = &intel_idle,
  411. .enter_s2idle = intel_idle_s2idle, },
  412. {
  413. .name = "C3",
  414. .desc = "MWAIT 0x10",
  415. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  416. .exit_latency = 59,
  417. .target_residency = 600,
  418. .enter = &intel_idle,
  419. .enter_s2idle = intel_idle_s2idle, },
  420. {
  421. .name = "C6",
  422. .desc = "MWAIT 0x20",
  423. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  424. .exit_latency = 88,
  425. .target_residency = 700,
  426. .enter = &intel_idle,
  427. .enter_s2idle = intel_idle_s2idle, },
  428. {
  429. .enter = NULL }
  430. };
  431. static struct cpuidle_state hsw_cstates[] = {
  432. {
  433. .name = "C1",
  434. .desc = "MWAIT 0x00",
  435. .flags = MWAIT2flg(0x00),
  436. .exit_latency = 2,
  437. .target_residency = 2,
  438. .enter = &intel_idle,
  439. .enter_s2idle = intel_idle_s2idle, },
  440. {
  441. .name = "C1E",
  442. .desc = "MWAIT 0x01",
  443. .flags = MWAIT2flg(0x01),
  444. .exit_latency = 10,
  445. .target_residency = 20,
  446. .enter = &intel_idle,
  447. .enter_s2idle = intel_idle_s2idle, },
  448. {
  449. .name = "C3",
  450. .desc = "MWAIT 0x10",
  451. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  452. .exit_latency = 33,
  453. .target_residency = 100,
  454. .enter = &intel_idle,
  455. .enter_s2idle = intel_idle_s2idle, },
  456. {
  457. .name = "C6",
  458. .desc = "MWAIT 0x20",
  459. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  460. .exit_latency = 133,
  461. .target_residency = 400,
  462. .enter = &intel_idle,
  463. .enter_s2idle = intel_idle_s2idle, },
  464. {
  465. .name = "C7s",
  466. .desc = "MWAIT 0x32",
  467. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
  468. .exit_latency = 166,
  469. .target_residency = 500,
  470. .enter = &intel_idle,
  471. .enter_s2idle = intel_idle_s2idle, },
  472. {
  473. .name = "C8",
  474. .desc = "MWAIT 0x40",
  475. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  476. .exit_latency = 300,
  477. .target_residency = 900,
  478. .enter = &intel_idle,
  479. .enter_s2idle = intel_idle_s2idle, },
  480. {
  481. .name = "C9",
  482. .desc = "MWAIT 0x50",
  483. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  484. .exit_latency = 600,
  485. .target_residency = 1800,
  486. .enter = &intel_idle,
  487. .enter_s2idle = intel_idle_s2idle, },
  488. {
  489. .name = "C10",
  490. .desc = "MWAIT 0x60",
  491. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  492. .exit_latency = 2600,
  493. .target_residency = 7700,
  494. .enter = &intel_idle,
  495. .enter_s2idle = intel_idle_s2idle, },
  496. {
  497. .enter = NULL }
  498. };
  499. static struct cpuidle_state bdw_cstates[] = {
  500. {
  501. .name = "C1",
  502. .desc = "MWAIT 0x00",
  503. .flags = MWAIT2flg(0x00),
  504. .exit_latency = 2,
  505. .target_residency = 2,
  506. .enter = &intel_idle,
  507. .enter_s2idle = intel_idle_s2idle, },
  508. {
  509. .name = "C1E",
  510. .desc = "MWAIT 0x01",
  511. .flags = MWAIT2flg(0x01),
  512. .exit_latency = 10,
  513. .target_residency = 20,
  514. .enter = &intel_idle,
  515. .enter_s2idle = intel_idle_s2idle, },
  516. {
  517. .name = "C3",
  518. .desc = "MWAIT 0x10",
  519. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  520. .exit_latency = 40,
  521. .target_residency = 100,
  522. .enter = &intel_idle,
  523. .enter_s2idle = intel_idle_s2idle, },
  524. {
  525. .name = "C6",
  526. .desc = "MWAIT 0x20",
  527. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  528. .exit_latency = 133,
  529. .target_residency = 400,
  530. .enter = &intel_idle,
  531. .enter_s2idle = intel_idle_s2idle, },
  532. {
  533. .name = "C7s",
  534. .desc = "MWAIT 0x32",
  535. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
  536. .exit_latency = 166,
  537. .target_residency = 500,
  538. .enter = &intel_idle,
  539. .enter_s2idle = intel_idle_s2idle, },
  540. {
  541. .name = "C8",
  542. .desc = "MWAIT 0x40",
  543. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  544. .exit_latency = 300,
  545. .target_residency = 900,
  546. .enter = &intel_idle,
  547. .enter_s2idle = intel_idle_s2idle, },
  548. {
  549. .name = "C9",
  550. .desc = "MWAIT 0x50",
  551. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  552. .exit_latency = 600,
  553. .target_residency = 1800,
  554. .enter = &intel_idle,
  555. .enter_s2idle = intel_idle_s2idle, },
  556. {
  557. .name = "C10",
  558. .desc = "MWAIT 0x60",
  559. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  560. .exit_latency = 2600,
  561. .target_residency = 7700,
  562. .enter = &intel_idle,
  563. .enter_s2idle = intel_idle_s2idle, },
  564. {
  565. .enter = NULL }
  566. };
  567. static struct cpuidle_state skl_cstates[] = {
  568. {
  569. .name = "C1",
  570. .desc = "MWAIT 0x00",
  571. .flags = MWAIT2flg(0x00),
  572. .exit_latency = 2,
  573. .target_residency = 2,
  574. .enter = &intel_idle,
  575. .enter_s2idle = intel_idle_s2idle, },
  576. {
  577. .name = "C1E",
  578. .desc = "MWAIT 0x01",
  579. .flags = MWAIT2flg(0x01),
  580. .exit_latency = 10,
  581. .target_residency = 20,
  582. .enter = &intel_idle,
  583. .enter_s2idle = intel_idle_s2idle, },
  584. {
  585. .name = "C3",
  586. .desc = "MWAIT 0x10",
  587. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  588. .exit_latency = 70,
  589. .target_residency = 100,
  590. .enter = &intel_idle,
  591. .enter_s2idle = intel_idle_s2idle, },
  592. {
  593. .name = "C6",
  594. .desc = "MWAIT 0x20",
  595. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  596. .exit_latency = 85,
  597. .target_residency = 200,
  598. .enter = &intel_idle,
  599. .enter_s2idle = intel_idle_s2idle, },
  600. {
  601. .name = "C7s",
  602. .desc = "MWAIT 0x33",
  603. .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED,
  604. .exit_latency = 124,
  605. .target_residency = 800,
  606. .enter = &intel_idle,
  607. .enter_s2idle = intel_idle_s2idle, },
  608. {
  609. .name = "C8",
  610. .desc = "MWAIT 0x40",
  611. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  612. .exit_latency = 200,
  613. .target_residency = 800,
  614. .enter = &intel_idle,
  615. .enter_s2idle = intel_idle_s2idle, },
  616. {
  617. .name = "C9",
  618. .desc = "MWAIT 0x50",
  619. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  620. .exit_latency = 480,
  621. .target_residency = 5000,
  622. .enter = &intel_idle,
  623. .enter_s2idle = intel_idle_s2idle, },
  624. {
  625. .name = "C10",
  626. .desc = "MWAIT 0x60",
  627. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  628. .exit_latency = 890,
  629. .target_residency = 5000,
  630. .enter = &intel_idle,
  631. .enter_s2idle = intel_idle_s2idle, },
  632. {
  633. .enter = NULL }
  634. };
  635. static struct cpuidle_state skx_cstates[] = {
  636. {
  637. .name = "C1",
  638. .desc = "MWAIT 0x00",
  639. .flags = MWAIT2flg(0x00),
  640. .exit_latency = 2,
  641. .target_residency = 2,
  642. .enter = &intel_idle,
  643. .enter_s2idle = intel_idle_s2idle, },
  644. {
  645. .name = "C1E",
  646. .desc = "MWAIT 0x01",
  647. .flags = MWAIT2flg(0x01),
  648. .exit_latency = 10,
  649. .target_residency = 20,
  650. .enter = &intel_idle,
  651. .enter_s2idle = intel_idle_s2idle, },
  652. {
  653. .name = "C6",
  654. .desc = "MWAIT 0x20",
  655. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  656. .exit_latency = 133,
  657. .target_residency = 600,
  658. .enter = &intel_idle,
  659. .enter_s2idle = intel_idle_s2idle, },
  660. {
  661. .enter = NULL }
  662. };
  663. static struct cpuidle_state atom_cstates[] = {
  664. {
  665. .name = "C1E",
  666. .desc = "MWAIT 0x00",
  667. .flags = MWAIT2flg(0x00),
  668. .exit_latency = 10,
  669. .target_residency = 20,
  670. .enter = &intel_idle,
  671. .enter_s2idle = intel_idle_s2idle, },
  672. {
  673. .name = "C2",
  674. .desc = "MWAIT 0x10",
  675. .flags = MWAIT2flg(0x10),
  676. .exit_latency = 20,
  677. .target_residency = 80,
  678. .enter = &intel_idle,
  679. .enter_s2idle = intel_idle_s2idle, },
  680. {
  681. .name = "C4",
  682. .desc = "MWAIT 0x30",
  683. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  684. .exit_latency = 100,
  685. .target_residency = 400,
  686. .enter = &intel_idle,
  687. .enter_s2idle = intel_idle_s2idle, },
  688. {
  689. .name = "C6",
  690. .desc = "MWAIT 0x52",
  691. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  692. .exit_latency = 140,
  693. .target_residency = 560,
  694. .enter = &intel_idle,
  695. .enter_s2idle = intel_idle_s2idle, },
  696. {
  697. .enter = NULL }
  698. };
  699. static struct cpuidle_state tangier_cstates[] = {
  700. {
  701. .name = "C1",
  702. .desc = "MWAIT 0x00",
  703. .flags = MWAIT2flg(0x00),
  704. .exit_latency = 1,
  705. .target_residency = 4,
  706. .enter = &intel_idle,
  707. .enter_s2idle = intel_idle_s2idle, },
  708. {
  709. .name = "C4",
  710. .desc = "MWAIT 0x30",
  711. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  712. .exit_latency = 100,
  713. .target_residency = 400,
  714. .enter = &intel_idle,
  715. .enter_s2idle = intel_idle_s2idle, },
  716. {
  717. .name = "C6",
  718. .desc = "MWAIT 0x52",
  719. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  720. .exit_latency = 140,
  721. .target_residency = 560,
  722. .enter = &intel_idle,
  723. .enter_s2idle = intel_idle_s2idle, },
  724. {
  725. .name = "C7",
  726. .desc = "MWAIT 0x60",
  727. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  728. .exit_latency = 1200,
  729. .target_residency = 4000,
  730. .enter = &intel_idle,
  731. .enter_s2idle = intel_idle_s2idle, },
  732. {
  733. .name = "C9",
  734. .desc = "MWAIT 0x64",
  735. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  736. .exit_latency = 10000,
  737. .target_residency = 20000,
  738. .enter = &intel_idle,
  739. .enter_s2idle = intel_idle_s2idle, },
  740. {
  741. .enter = NULL }
  742. };
  743. static struct cpuidle_state avn_cstates[] = {
  744. {
  745. .name = "C1",
  746. .desc = "MWAIT 0x00",
  747. .flags = MWAIT2flg(0x00),
  748. .exit_latency = 2,
  749. .target_residency = 2,
  750. .enter = &intel_idle,
  751. .enter_s2idle = intel_idle_s2idle, },
  752. {
  753. .name = "C6",
  754. .desc = "MWAIT 0x51",
  755. .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
  756. .exit_latency = 15,
  757. .target_residency = 45,
  758. .enter = &intel_idle,
  759. .enter_s2idle = intel_idle_s2idle, },
  760. {
  761. .enter = NULL }
  762. };
  763. static struct cpuidle_state knl_cstates[] = {
  764. {
  765. .name = "C1",
  766. .desc = "MWAIT 0x00",
  767. .flags = MWAIT2flg(0x00),
  768. .exit_latency = 1,
  769. .target_residency = 2,
  770. .enter = &intel_idle,
  771. .enter_s2idle = intel_idle_s2idle },
  772. {
  773. .name = "C6",
  774. .desc = "MWAIT 0x10",
  775. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  776. .exit_latency = 120,
  777. .target_residency = 500,
  778. .enter = &intel_idle,
  779. .enter_s2idle = intel_idle_s2idle },
  780. {
  781. .enter = NULL }
  782. };
  783. static struct cpuidle_state bxt_cstates[] = {
  784. {
  785. .name = "C1",
  786. .desc = "MWAIT 0x00",
  787. .flags = MWAIT2flg(0x00),
  788. .exit_latency = 2,
  789. .target_residency = 2,
  790. .enter = &intel_idle,
  791. .enter_s2idle = intel_idle_s2idle, },
  792. {
  793. .name = "C1E",
  794. .desc = "MWAIT 0x01",
  795. .flags = MWAIT2flg(0x01),
  796. .exit_latency = 10,
  797. .target_residency = 20,
  798. .enter = &intel_idle,
  799. .enter_s2idle = intel_idle_s2idle, },
  800. {
  801. .name = "C6",
  802. .desc = "MWAIT 0x20",
  803. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  804. .exit_latency = 133,
  805. .target_residency = 133,
  806. .enter = &intel_idle,
  807. .enter_s2idle = intel_idle_s2idle, },
  808. {
  809. .name = "C7s",
  810. .desc = "MWAIT 0x31",
  811. .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
  812. .exit_latency = 155,
  813. .target_residency = 155,
  814. .enter = &intel_idle,
  815. .enter_s2idle = intel_idle_s2idle, },
  816. {
  817. .name = "C8",
  818. .desc = "MWAIT 0x40",
  819. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  820. .exit_latency = 1000,
  821. .target_residency = 1000,
  822. .enter = &intel_idle,
  823. .enter_s2idle = intel_idle_s2idle, },
  824. {
  825. .name = "C9",
  826. .desc = "MWAIT 0x50",
  827. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  828. .exit_latency = 2000,
  829. .target_residency = 2000,
  830. .enter = &intel_idle,
  831. .enter_s2idle = intel_idle_s2idle, },
  832. {
  833. .name = "C10",
  834. .desc = "MWAIT 0x60",
  835. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  836. .exit_latency = 10000,
  837. .target_residency = 10000,
  838. .enter = &intel_idle,
  839. .enter_s2idle = intel_idle_s2idle, },
  840. {
  841. .enter = NULL }
  842. };
  843. static struct cpuidle_state dnv_cstates[] = {
  844. {
  845. .name = "C1",
  846. .desc = "MWAIT 0x00",
  847. .flags = MWAIT2flg(0x00),
  848. .exit_latency = 2,
  849. .target_residency = 2,
  850. .enter = &intel_idle,
  851. .enter_s2idle = intel_idle_s2idle, },
  852. {
  853. .name = "C1E",
  854. .desc = "MWAIT 0x01",
  855. .flags = MWAIT2flg(0x01),
  856. .exit_latency = 10,
  857. .target_residency = 20,
  858. .enter = &intel_idle,
  859. .enter_s2idle = intel_idle_s2idle, },
  860. {
  861. .name = "C6",
  862. .desc = "MWAIT 0x20",
  863. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  864. .exit_latency = 50,
  865. .target_residency = 500,
  866. .enter = &intel_idle,
  867. .enter_s2idle = intel_idle_s2idle, },
  868. {
  869. .enter = NULL }
  870. };
  871. /**
  872. * intel_idle
  873. * @dev: cpuidle_device
  874. * @drv: cpuidle driver
  875. * @index: index of cpuidle state
  876. *
  877. * Must be called under local_irq_disable().
  878. */
  879. static __cpuidle int intel_idle(struct cpuidle_device *dev,
  880. struct cpuidle_driver *drv, int index)
  881. {
  882. unsigned long ecx = 1; /* break on interrupt flag */
  883. struct cpuidle_state *state = &drv->states[index];
  884. unsigned long eax = flg2MWAIT(state->flags);
  885. unsigned int cstate;
  886. bool uninitialized_var(tick);
  887. /*
  888. * NB: if CPUIDLE_FLAG_TLB_FLUSHED is set, this idle transition
  889. * will probably flush the TLB. It's not guaranteed to flush
  890. * the TLB, though, so it's not clear that we can do anything
  891. * useful with this knowledge.
  892. */
  893. if (!static_cpu_has(X86_FEATURE_ARAT)) {
  894. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) &
  895. MWAIT_CSTATE_MASK) + 1;
  896. tick = false;
  897. if (!(lapic_timer_reliable_states & (1 << (cstate)))) {
  898. tick = true;
  899. tick_broadcast_enter();
  900. }
  901. }
  902. mwait_idle_with_hints(eax, ecx);
  903. if (!static_cpu_has(X86_FEATURE_ARAT) && tick)
  904. tick_broadcast_exit();
  905. return index;
  906. }
  907. /**
  908. * intel_idle_s2idle - simplified "enter" callback routine for suspend-to-idle
  909. * @dev: cpuidle_device
  910. * @drv: cpuidle driver
  911. * @index: state index
  912. */
  913. static void intel_idle_s2idle(struct cpuidle_device *dev,
  914. struct cpuidle_driver *drv, int index)
  915. {
  916. unsigned long ecx = 1; /* break on interrupt flag */
  917. unsigned long eax = flg2MWAIT(drv->states[index].flags);
  918. mwait_idle_with_hints(eax, ecx);
  919. }
  920. static void __setup_broadcast_timer(bool on)
  921. {
  922. if (on)
  923. tick_broadcast_enable();
  924. else
  925. tick_broadcast_disable();
  926. }
  927. static void auto_demotion_disable(void)
  928. {
  929. unsigned long long msr_bits;
  930. rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
  931. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  932. wrmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
  933. }
  934. static void c1e_promotion_disable(void)
  935. {
  936. unsigned long long msr_bits;
  937. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  938. msr_bits &= ~0x2;
  939. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  940. }
  941. static const struct idle_cpu idle_cpu_nehalem = {
  942. .state_table = nehalem_cstates,
  943. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  944. .disable_promotion_to_c1e = true,
  945. };
  946. static const struct idle_cpu idle_cpu_atom = {
  947. .state_table = atom_cstates,
  948. };
  949. static const struct idle_cpu idle_cpu_tangier = {
  950. .state_table = tangier_cstates,
  951. };
  952. static const struct idle_cpu idle_cpu_lincroft = {
  953. .state_table = atom_cstates,
  954. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  955. };
  956. static const struct idle_cpu idle_cpu_snb = {
  957. .state_table = snb_cstates,
  958. .disable_promotion_to_c1e = true,
  959. };
  960. static const struct idle_cpu idle_cpu_byt = {
  961. .state_table = byt_cstates,
  962. .disable_promotion_to_c1e = true,
  963. .byt_auto_demotion_disable_flag = true,
  964. };
  965. static const struct idle_cpu idle_cpu_cht = {
  966. .state_table = cht_cstates,
  967. .disable_promotion_to_c1e = true,
  968. .byt_auto_demotion_disable_flag = true,
  969. };
  970. static const struct idle_cpu idle_cpu_ivb = {
  971. .state_table = ivb_cstates,
  972. .disable_promotion_to_c1e = true,
  973. };
  974. static const struct idle_cpu idle_cpu_ivt = {
  975. .state_table = ivt_cstates,
  976. .disable_promotion_to_c1e = true,
  977. };
  978. static const struct idle_cpu idle_cpu_hsw = {
  979. .state_table = hsw_cstates,
  980. .disable_promotion_to_c1e = true,
  981. };
  982. static const struct idle_cpu idle_cpu_bdw = {
  983. .state_table = bdw_cstates,
  984. .disable_promotion_to_c1e = true,
  985. };
  986. static const struct idle_cpu idle_cpu_skl = {
  987. .state_table = skl_cstates,
  988. .disable_promotion_to_c1e = true,
  989. };
  990. static const struct idle_cpu idle_cpu_skx = {
  991. .state_table = skx_cstates,
  992. .disable_promotion_to_c1e = true,
  993. };
  994. static const struct idle_cpu idle_cpu_avn = {
  995. .state_table = avn_cstates,
  996. .disable_promotion_to_c1e = true,
  997. };
  998. static const struct idle_cpu idle_cpu_knl = {
  999. .state_table = knl_cstates,
  1000. };
  1001. static const struct idle_cpu idle_cpu_bxt = {
  1002. .state_table = bxt_cstates,
  1003. .disable_promotion_to_c1e = true,
  1004. };
  1005. static const struct idle_cpu idle_cpu_dnv = {
  1006. .state_table = dnv_cstates,
  1007. .disable_promotion_to_c1e = true,
  1008. };
  1009. #define ICPU(model, cpu) \
  1010. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&cpu }
  1011. static const struct x86_cpu_id intel_idle_ids[] __initconst = {
  1012. ICPU(INTEL_FAM6_NEHALEM_EP, idle_cpu_nehalem),
  1013. ICPU(INTEL_FAM6_NEHALEM, idle_cpu_nehalem),
  1014. ICPU(INTEL_FAM6_NEHALEM_G, idle_cpu_nehalem),
  1015. ICPU(INTEL_FAM6_WESTMERE, idle_cpu_nehalem),
  1016. ICPU(INTEL_FAM6_WESTMERE_EP, idle_cpu_nehalem),
  1017. ICPU(INTEL_FAM6_NEHALEM_EX, idle_cpu_nehalem),
  1018. ICPU(INTEL_FAM6_ATOM_PINEVIEW, idle_cpu_atom),
  1019. ICPU(INTEL_FAM6_ATOM_LINCROFT, idle_cpu_lincroft),
  1020. ICPU(INTEL_FAM6_WESTMERE_EX, idle_cpu_nehalem),
  1021. ICPU(INTEL_FAM6_SANDYBRIDGE, idle_cpu_snb),
  1022. ICPU(INTEL_FAM6_SANDYBRIDGE_X, idle_cpu_snb),
  1023. ICPU(INTEL_FAM6_ATOM_CEDARVIEW, idle_cpu_atom),
  1024. ICPU(INTEL_FAM6_ATOM_SILVERMONT1, idle_cpu_byt),
  1025. ICPU(INTEL_FAM6_ATOM_MERRIFIELD, idle_cpu_tangier),
  1026. ICPU(INTEL_FAM6_ATOM_AIRMONT, idle_cpu_cht),
  1027. ICPU(INTEL_FAM6_IVYBRIDGE, idle_cpu_ivb),
  1028. ICPU(INTEL_FAM6_IVYBRIDGE_X, idle_cpu_ivt),
  1029. ICPU(INTEL_FAM6_HASWELL_CORE, idle_cpu_hsw),
  1030. ICPU(INTEL_FAM6_HASWELL_X, idle_cpu_hsw),
  1031. ICPU(INTEL_FAM6_HASWELL_ULT, idle_cpu_hsw),
  1032. ICPU(INTEL_FAM6_HASWELL_GT3E, idle_cpu_hsw),
  1033. ICPU(INTEL_FAM6_ATOM_SILVERMONT2, idle_cpu_avn),
  1034. ICPU(INTEL_FAM6_BROADWELL_CORE, idle_cpu_bdw),
  1035. ICPU(INTEL_FAM6_BROADWELL_GT3E, idle_cpu_bdw),
  1036. ICPU(INTEL_FAM6_BROADWELL_X, idle_cpu_bdw),
  1037. ICPU(INTEL_FAM6_BROADWELL_XEON_D, idle_cpu_bdw),
  1038. ICPU(INTEL_FAM6_SKYLAKE_MOBILE, idle_cpu_skl),
  1039. ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, idle_cpu_skl),
  1040. ICPU(INTEL_FAM6_KABYLAKE_MOBILE, idle_cpu_skl),
  1041. ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, idle_cpu_skl),
  1042. ICPU(INTEL_FAM6_SKYLAKE_X, idle_cpu_skx),
  1043. ICPU(INTEL_FAM6_XEON_PHI_KNL, idle_cpu_knl),
  1044. ICPU(INTEL_FAM6_XEON_PHI_KNM, idle_cpu_knl),
  1045. ICPU(INTEL_FAM6_ATOM_GOLDMONT, idle_cpu_bxt),
  1046. ICPU(INTEL_FAM6_ATOM_GEMINI_LAKE, idle_cpu_bxt),
  1047. ICPU(INTEL_FAM6_ATOM_DENVERTON, idle_cpu_dnv),
  1048. {}
  1049. };
  1050. /*
  1051. * intel_idle_probe()
  1052. */
  1053. static int __init intel_idle_probe(void)
  1054. {
  1055. unsigned int eax, ebx, ecx;
  1056. const struct x86_cpu_id *id;
  1057. if (max_cstate == 0) {
  1058. pr_debug("disabled\n");
  1059. return -EPERM;
  1060. }
  1061. id = x86_match_cpu(intel_idle_ids);
  1062. if (!id) {
  1063. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1064. boot_cpu_data.x86 == 6)
  1065. pr_debug("does not run on family %d model %d\n",
  1066. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1067. return -ENODEV;
  1068. }
  1069. if (!boot_cpu_has(X86_FEATURE_MWAIT)) {
  1070. pr_debug("Please enable MWAIT in BIOS SETUP\n");
  1071. return -ENODEV;
  1072. }
  1073. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  1074. return -ENODEV;
  1075. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  1076. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  1077. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  1078. !mwait_substates)
  1079. return -ENODEV;
  1080. pr_debug("MWAIT substates: 0x%x\n", mwait_substates);
  1081. icpu = (const struct idle_cpu *)id->driver_data;
  1082. cpuidle_state_table = icpu->state_table;
  1083. pr_debug("v" INTEL_IDLE_VERSION " model 0x%X\n",
  1084. boot_cpu_data.x86_model);
  1085. return 0;
  1086. }
  1087. /*
  1088. * intel_idle_cpuidle_devices_uninit()
  1089. * Unregisters the cpuidle devices.
  1090. */
  1091. static void intel_idle_cpuidle_devices_uninit(void)
  1092. {
  1093. int i;
  1094. struct cpuidle_device *dev;
  1095. for_each_online_cpu(i) {
  1096. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  1097. cpuidle_unregister_device(dev);
  1098. }
  1099. }
  1100. /*
  1101. * ivt_idle_state_table_update(void)
  1102. *
  1103. * Tune IVT multi-socket targets
  1104. * Assumption: num_sockets == (max_package_num + 1)
  1105. */
  1106. static void ivt_idle_state_table_update(void)
  1107. {
  1108. /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
  1109. int cpu, package_num, num_sockets = 1;
  1110. for_each_online_cpu(cpu) {
  1111. package_num = topology_physical_package_id(cpu);
  1112. if (package_num + 1 > num_sockets) {
  1113. num_sockets = package_num + 1;
  1114. if (num_sockets > 4) {
  1115. cpuidle_state_table = ivt_cstates_8s;
  1116. return;
  1117. }
  1118. }
  1119. }
  1120. if (num_sockets > 2)
  1121. cpuidle_state_table = ivt_cstates_4s;
  1122. /* else, 1 and 2 socket systems use default ivt_cstates */
  1123. }
  1124. /*
  1125. * Translate IRTL (Interrupt Response Time Limit) MSR to usec
  1126. */
  1127. static unsigned int irtl_ns_units[] = {
  1128. 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
  1129. static unsigned long long irtl_2_usec(unsigned long long irtl)
  1130. {
  1131. unsigned long long ns;
  1132. if (!irtl)
  1133. return 0;
  1134. ns = irtl_ns_units[(irtl >> 10) & 0x7];
  1135. return div64_u64((irtl & 0x3FF) * ns, 1000);
  1136. }
  1137. /*
  1138. * bxt_idle_state_table_update(void)
  1139. *
  1140. * On BXT, we trust the IRTL to show the definitive maximum latency
  1141. * We use the same value for target_residency.
  1142. */
  1143. static void bxt_idle_state_table_update(void)
  1144. {
  1145. unsigned long long msr;
  1146. unsigned int usec;
  1147. rdmsrl(MSR_PKGC6_IRTL, msr);
  1148. usec = irtl_2_usec(msr);
  1149. if (usec) {
  1150. bxt_cstates[2].exit_latency = usec;
  1151. bxt_cstates[2].target_residency = usec;
  1152. }
  1153. rdmsrl(MSR_PKGC7_IRTL, msr);
  1154. usec = irtl_2_usec(msr);
  1155. if (usec) {
  1156. bxt_cstates[3].exit_latency = usec;
  1157. bxt_cstates[3].target_residency = usec;
  1158. }
  1159. rdmsrl(MSR_PKGC8_IRTL, msr);
  1160. usec = irtl_2_usec(msr);
  1161. if (usec) {
  1162. bxt_cstates[4].exit_latency = usec;
  1163. bxt_cstates[4].target_residency = usec;
  1164. }
  1165. rdmsrl(MSR_PKGC9_IRTL, msr);
  1166. usec = irtl_2_usec(msr);
  1167. if (usec) {
  1168. bxt_cstates[5].exit_latency = usec;
  1169. bxt_cstates[5].target_residency = usec;
  1170. }
  1171. rdmsrl(MSR_PKGC10_IRTL, msr);
  1172. usec = irtl_2_usec(msr);
  1173. if (usec) {
  1174. bxt_cstates[6].exit_latency = usec;
  1175. bxt_cstates[6].target_residency = usec;
  1176. }
  1177. }
  1178. /*
  1179. * sklh_idle_state_table_update(void)
  1180. *
  1181. * On SKL-H (model 0x5e) disable C8 and C9 if:
  1182. * C10 is enabled and SGX disabled
  1183. */
  1184. static void sklh_idle_state_table_update(void)
  1185. {
  1186. unsigned long long msr;
  1187. unsigned int eax, ebx, ecx, edx;
  1188. /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
  1189. if (max_cstate <= 7)
  1190. return;
  1191. /* if PC10 not present in CPUID.MWAIT.EDX */
  1192. if ((mwait_substates & (0xF << 28)) == 0)
  1193. return;
  1194. rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr);
  1195. /* PC10 is not enabled in PKG C-state limit */
  1196. if ((msr & 0xF) != 8)
  1197. return;
  1198. ecx = 0;
  1199. cpuid(7, &eax, &ebx, &ecx, &edx);
  1200. /* if SGX is present */
  1201. if (ebx & (1 << 2)) {
  1202. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1203. /* if SGX is enabled */
  1204. if (msr & (1 << 18))
  1205. return;
  1206. }
  1207. skl_cstates[5].disabled = 1; /* C8-SKL */
  1208. skl_cstates[6].disabled = 1; /* C9-SKL */
  1209. }
  1210. /*
  1211. * intel_idle_state_table_update()
  1212. *
  1213. * Update the default state_table for this CPU-id
  1214. */
  1215. static void intel_idle_state_table_update(void)
  1216. {
  1217. switch (boot_cpu_data.x86_model) {
  1218. case INTEL_FAM6_IVYBRIDGE_X:
  1219. ivt_idle_state_table_update();
  1220. break;
  1221. case INTEL_FAM6_ATOM_GOLDMONT:
  1222. case INTEL_FAM6_ATOM_GEMINI_LAKE:
  1223. bxt_idle_state_table_update();
  1224. break;
  1225. case INTEL_FAM6_SKYLAKE_DESKTOP:
  1226. sklh_idle_state_table_update();
  1227. break;
  1228. }
  1229. }
  1230. /*
  1231. * intel_idle_cpuidle_driver_init()
  1232. * allocate, initialize cpuidle_states
  1233. */
  1234. static void __init intel_idle_cpuidle_driver_init(void)
  1235. {
  1236. int cstate;
  1237. struct cpuidle_driver *drv = &intel_idle_driver;
  1238. intel_idle_state_table_update();
  1239. cpuidle_poll_state_init(drv);
  1240. drv->state_count = 1;
  1241. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  1242. int num_substates, mwait_hint, mwait_cstate;
  1243. if ((cpuidle_state_table[cstate].enter == NULL) &&
  1244. (cpuidle_state_table[cstate].enter_s2idle == NULL))
  1245. break;
  1246. if (cstate + 1 > max_cstate) {
  1247. pr_info("max_cstate %d reached\n", max_cstate);
  1248. break;
  1249. }
  1250. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  1251. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  1252. /* number of sub-states for this state in CPUID.MWAIT */
  1253. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  1254. & MWAIT_SUBSTATE_MASK;
  1255. /* if NO sub-states for this state in CPUID, skip it */
  1256. if (num_substates == 0)
  1257. continue;
  1258. /* if state marked as disabled, skip it */
  1259. if (cpuidle_state_table[cstate].disabled != 0) {
  1260. pr_debug("state %s is disabled\n",
  1261. cpuidle_state_table[cstate].name);
  1262. continue;
  1263. }
  1264. if (((mwait_cstate + 1) > 2) &&
  1265. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1266. mark_tsc_unstable("TSC halts in idle"
  1267. " states deeper than C2");
  1268. drv->states[drv->state_count] = /* structure copy */
  1269. cpuidle_state_table[cstate];
  1270. drv->state_count += 1;
  1271. }
  1272. if (icpu->byt_auto_demotion_disable_flag) {
  1273. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  1274. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  1275. }
  1276. }
  1277. /*
  1278. * intel_idle_cpu_init()
  1279. * allocate, initialize, register cpuidle_devices
  1280. * @cpu: cpu/core to initialize
  1281. */
  1282. static int intel_idle_cpu_init(unsigned int cpu)
  1283. {
  1284. struct cpuidle_device *dev;
  1285. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1286. dev->cpu = cpu;
  1287. if (cpuidle_register_device(dev)) {
  1288. pr_debug("cpuidle_register_device %d failed!\n", cpu);
  1289. return -EIO;
  1290. }
  1291. if (icpu->auto_demotion_disable_flags)
  1292. auto_demotion_disable();
  1293. if (icpu->disable_promotion_to_c1e)
  1294. c1e_promotion_disable();
  1295. return 0;
  1296. }
  1297. static int intel_idle_cpu_online(unsigned int cpu)
  1298. {
  1299. struct cpuidle_device *dev;
  1300. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  1301. __setup_broadcast_timer(true);
  1302. /*
  1303. * Some systems can hotplug a cpu at runtime after
  1304. * the kernel has booted, we have to initialize the
  1305. * driver in this case
  1306. */
  1307. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1308. if (!dev->registered)
  1309. return intel_idle_cpu_init(cpu);
  1310. return 0;
  1311. }
  1312. static int __init intel_idle_init(void)
  1313. {
  1314. int retval;
  1315. /* Do not load intel_idle at all for now if idle= is passed */
  1316. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  1317. return -ENODEV;
  1318. retval = intel_idle_probe();
  1319. if (retval)
  1320. return retval;
  1321. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  1322. if (intel_idle_cpuidle_devices == NULL)
  1323. return -ENOMEM;
  1324. intel_idle_cpuidle_driver_init();
  1325. retval = cpuidle_register_driver(&intel_idle_driver);
  1326. if (retval) {
  1327. struct cpuidle_driver *drv = cpuidle_get_driver();
  1328. printk(KERN_DEBUG pr_fmt("intel_idle yielding to %s\n"),
  1329. drv ? drv->name : "none");
  1330. goto init_driver_fail;
  1331. }
  1332. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  1333. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  1334. retval = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "idle/intel:online",
  1335. intel_idle_cpu_online, NULL);
  1336. if (retval < 0)
  1337. goto hp_setup_fail;
  1338. pr_debug("lapic_timer_reliable_states 0x%x\n",
  1339. lapic_timer_reliable_states);
  1340. return 0;
  1341. hp_setup_fail:
  1342. intel_idle_cpuidle_devices_uninit();
  1343. cpuidle_unregister_driver(&intel_idle_driver);
  1344. init_driver_fail:
  1345. free_percpu(intel_idle_cpuidle_devices);
  1346. return retval;
  1347. }
  1348. device_initcall(intel_idle_init);
  1349. /*
  1350. * We are not really modular, but we used to support that. Meaning we also
  1351. * support "intel_idle.max_cstate=..." at boot and also a read-only export of
  1352. * it at /sys/module/intel_idle/parameters/max_cstate -- so using module_param
  1353. * is the easiest way (currently) to continue doing that.
  1354. */
  1355. module_param(max_cstate, int, 0444);