intel_idle.c 37 KB

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