intel_idle.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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 avn_cstates[] = {
  701. {
  702. .name = "C1-AVN",
  703. .desc = "MWAIT 0x00",
  704. .flags = MWAIT2flg(0x00),
  705. .exit_latency = 2,
  706. .target_residency = 2,
  707. .enter = &intel_idle,
  708. .enter_freeze = intel_idle_freeze, },
  709. {
  710. .name = "C6-AVN",
  711. .desc = "MWAIT 0x51",
  712. .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
  713. .exit_latency = 15,
  714. .target_residency = 45,
  715. .enter = &intel_idle,
  716. .enter_freeze = intel_idle_freeze, },
  717. {
  718. .enter = NULL }
  719. };
  720. static struct cpuidle_state knl_cstates[] = {
  721. {
  722. .name = "C1-KNL",
  723. .desc = "MWAIT 0x00",
  724. .flags = MWAIT2flg(0x00),
  725. .exit_latency = 1,
  726. .target_residency = 2,
  727. .enter = &intel_idle,
  728. .enter_freeze = intel_idle_freeze },
  729. {
  730. .name = "C6-KNL",
  731. .desc = "MWAIT 0x10",
  732. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  733. .exit_latency = 120,
  734. .target_residency = 500,
  735. .enter = &intel_idle,
  736. .enter_freeze = intel_idle_freeze },
  737. {
  738. .enter = NULL }
  739. };
  740. static struct cpuidle_state bxt_cstates[] = {
  741. {
  742. .name = "C1-BXT",
  743. .desc = "MWAIT 0x00",
  744. .flags = MWAIT2flg(0x00),
  745. .exit_latency = 2,
  746. .target_residency = 2,
  747. .enter = &intel_idle,
  748. .enter_freeze = intel_idle_freeze, },
  749. {
  750. .name = "C1E-BXT",
  751. .desc = "MWAIT 0x01",
  752. .flags = MWAIT2flg(0x01),
  753. .exit_latency = 10,
  754. .target_residency = 20,
  755. .enter = &intel_idle,
  756. .enter_freeze = intel_idle_freeze, },
  757. {
  758. .name = "C6-BXT",
  759. .desc = "MWAIT 0x20",
  760. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  761. .exit_latency = 133,
  762. .target_residency = 133,
  763. .enter = &intel_idle,
  764. .enter_freeze = intel_idle_freeze, },
  765. {
  766. .name = "C7s-BXT",
  767. .desc = "MWAIT 0x31",
  768. .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
  769. .exit_latency = 155,
  770. .target_residency = 155,
  771. .enter = &intel_idle,
  772. .enter_freeze = intel_idle_freeze, },
  773. {
  774. .name = "C8-BXT",
  775. .desc = "MWAIT 0x40",
  776. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  777. .exit_latency = 1000,
  778. .target_residency = 1000,
  779. .enter = &intel_idle,
  780. .enter_freeze = intel_idle_freeze, },
  781. {
  782. .name = "C9-BXT",
  783. .desc = "MWAIT 0x50",
  784. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  785. .exit_latency = 2000,
  786. .target_residency = 2000,
  787. .enter = &intel_idle,
  788. .enter_freeze = intel_idle_freeze, },
  789. {
  790. .name = "C10-BXT",
  791. .desc = "MWAIT 0x60",
  792. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  793. .exit_latency = 10000,
  794. .target_residency = 10000,
  795. .enter = &intel_idle,
  796. .enter_freeze = intel_idle_freeze, },
  797. {
  798. .enter = NULL }
  799. };
  800. static struct cpuidle_state dnv_cstates[] = {
  801. {
  802. .name = "C1-DNV",
  803. .desc = "MWAIT 0x00",
  804. .flags = MWAIT2flg(0x00),
  805. .exit_latency = 2,
  806. .target_residency = 2,
  807. .enter = &intel_idle,
  808. .enter_freeze = intel_idle_freeze, },
  809. {
  810. .name = "C1E-DNV",
  811. .desc = "MWAIT 0x01",
  812. .flags = MWAIT2flg(0x01),
  813. .exit_latency = 10,
  814. .target_residency = 20,
  815. .enter = &intel_idle,
  816. .enter_freeze = intel_idle_freeze, },
  817. {
  818. .name = "C6-DNV",
  819. .desc = "MWAIT 0x20",
  820. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  821. .exit_latency = 50,
  822. .target_residency = 500,
  823. .enter = &intel_idle,
  824. .enter_freeze = intel_idle_freeze, },
  825. {
  826. .enter = NULL }
  827. };
  828. /**
  829. * intel_idle
  830. * @dev: cpuidle_device
  831. * @drv: cpuidle driver
  832. * @index: index of cpuidle state
  833. *
  834. * Must be called under local_irq_disable().
  835. */
  836. static int intel_idle(struct cpuidle_device *dev,
  837. struct cpuidle_driver *drv, int index)
  838. {
  839. unsigned long ecx = 1; /* break on interrupt flag */
  840. struct cpuidle_state *state = &drv->states[index];
  841. unsigned long eax = flg2MWAIT(state->flags);
  842. unsigned int cstate;
  843. int cpu = smp_processor_id();
  844. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  845. /*
  846. * leave_mm() to avoid costly and often unnecessary wakeups
  847. * for flushing the user TLB's associated with the active mm.
  848. */
  849. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  850. leave_mm(cpu);
  851. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  852. tick_broadcast_enter();
  853. mwait_idle_with_hints(eax, ecx);
  854. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  855. tick_broadcast_exit();
  856. return index;
  857. }
  858. /**
  859. * intel_idle_freeze - simplified "enter" callback routine for suspend-to-idle
  860. * @dev: cpuidle_device
  861. * @drv: cpuidle driver
  862. * @index: state index
  863. */
  864. static void intel_idle_freeze(struct cpuidle_device *dev,
  865. struct cpuidle_driver *drv, int index)
  866. {
  867. unsigned long ecx = 1; /* break on interrupt flag */
  868. unsigned long eax = flg2MWAIT(drv->states[index].flags);
  869. mwait_idle_with_hints(eax, ecx);
  870. }
  871. static void __setup_broadcast_timer(void *arg)
  872. {
  873. unsigned long on = (unsigned long)arg;
  874. if (on)
  875. tick_broadcast_enable();
  876. else
  877. tick_broadcast_disable();
  878. }
  879. static int cpu_hotplug_notify(struct notifier_block *n,
  880. unsigned long action, void *hcpu)
  881. {
  882. int hotcpu = (unsigned long)hcpu;
  883. struct cpuidle_device *dev;
  884. switch (action & ~CPU_TASKS_FROZEN) {
  885. case CPU_ONLINE:
  886. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  887. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  888. (void *)true, 1);
  889. /*
  890. * Some systems can hotplug a cpu at runtime after
  891. * the kernel has booted, we have to initialize the
  892. * driver in this case
  893. */
  894. dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
  895. if (dev->registered)
  896. break;
  897. if (intel_idle_cpu_init(hotcpu))
  898. return NOTIFY_BAD;
  899. break;
  900. }
  901. return NOTIFY_OK;
  902. }
  903. static struct notifier_block cpu_hotplug_notifier = {
  904. .notifier_call = cpu_hotplug_notify,
  905. };
  906. static void auto_demotion_disable(void *dummy)
  907. {
  908. unsigned long long msr_bits;
  909. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  910. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  911. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  912. }
  913. static void c1e_promotion_disable(void *dummy)
  914. {
  915. unsigned long long msr_bits;
  916. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  917. msr_bits &= ~0x2;
  918. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  919. }
  920. static const struct idle_cpu idle_cpu_nehalem = {
  921. .state_table = nehalem_cstates,
  922. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  923. .disable_promotion_to_c1e = true,
  924. };
  925. static const struct idle_cpu idle_cpu_atom = {
  926. .state_table = atom_cstates,
  927. };
  928. static const struct idle_cpu idle_cpu_lincroft = {
  929. .state_table = atom_cstates,
  930. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  931. };
  932. static const struct idle_cpu idle_cpu_snb = {
  933. .state_table = snb_cstates,
  934. .disable_promotion_to_c1e = true,
  935. };
  936. static const struct idle_cpu idle_cpu_byt = {
  937. .state_table = byt_cstates,
  938. .disable_promotion_to_c1e = true,
  939. .byt_auto_demotion_disable_flag = true,
  940. };
  941. static const struct idle_cpu idle_cpu_cht = {
  942. .state_table = cht_cstates,
  943. .disable_promotion_to_c1e = true,
  944. .byt_auto_demotion_disable_flag = true,
  945. };
  946. static const struct idle_cpu idle_cpu_ivb = {
  947. .state_table = ivb_cstates,
  948. .disable_promotion_to_c1e = true,
  949. };
  950. static const struct idle_cpu idle_cpu_ivt = {
  951. .state_table = ivt_cstates,
  952. .disable_promotion_to_c1e = true,
  953. };
  954. static const struct idle_cpu idle_cpu_hsw = {
  955. .state_table = hsw_cstates,
  956. .disable_promotion_to_c1e = true,
  957. };
  958. static const struct idle_cpu idle_cpu_bdw = {
  959. .state_table = bdw_cstates,
  960. .disable_promotion_to_c1e = true,
  961. };
  962. static const struct idle_cpu idle_cpu_skl = {
  963. .state_table = skl_cstates,
  964. .disable_promotion_to_c1e = true,
  965. };
  966. static const struct idle_cpu idle_cpu_skx = {
  967. .state_table = skx_cstates,
  968. .disable_promotion_to_c1e = true,
  969. };
  970. static const struct idle_cpu idle_cpu_avn = {
  971. .state_table = avn_cstates,
  972. .disable_promotion_to_c1e = true,
  973. };
  974. static const struct idle_cpu idle_cpu_knl = {
  975. .state_table = knl_cstates,
  976. };
  977. static const struct idle_cpu idle_cpu_bxt = {
  978. .state_table = bxt_cstates,
  979. .disable_promotion_to_c1e = true,
  980. };
  981. static const struct idle_cpu idle_cpu_dnv = {
  982. .state_table = dnv_cstates,
  983. .disable_promotion_to_c1e = true,
  984. };
  985. #define ICPU(model, cpu) \
  986. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
  987. static const struct x86_cpu_id intel_idle_ids[] __initconst = {
  988. ICPU(INTEL_FAM6_NEHALEM_EP, idle_cpu_nehalem),
  989. ICPU(INTEL_FAM6_NEHALEM, idle_cpu_nehalem),
  990. ICPU(INTEL_FAM6_WESTMERE2, idle_cpu_nehalem),
  991. ICPU(INTEL_FAM6_WESTMERE, idle_cpu_nehalem),
  992. ICPU(INTEL_FAM6_WESTMERE_EP, idle_cpu_nehalem),
  993. ICPU(INTEL_FAM6_NEHALEM_EX, idle_cpu_nehalem),
  994. ICPU(INTEL_FAM6_ATOM_PINEVIEW, idle_cpu_atom),
  995. ICPU(INTEL_FAM6_ATOM_LINCROFT, idle_cpu_lincroft),
  996. ICPU(INTEL_FAM6_WESTMERE_EX, idle_cpu_nehalem),
  997. ICPU(INTEL_FAM6_SANDYBRIDGE, idle_cpu_snb),
  998. ICPU(INTEL_FAM6_SANDYBRIDGE_X, idle_cpu_snb),
  999. ICPU(INTEL_FAM6_ATOM_CEDARVIEW, idle_cpu_atom),
  1000. ICPU(INTEL_FAM6_ATOM_SILVERMONT1, idle_cpu_byt),
  1001. ICPU(INTEL_FAM6_ATOM_AIRMONT, idle_cpu_cht),
  1002. ICPU(INTEL_FAM6_IVYBRIDGE, idle_cpu_ivb),
  1003. ICPU(INTEL_FAM6_IVYBRIDGE_X, idle_cpu_ivt),
  1004. ICPU(INTEL_FAM6_HASWELL_CORE, idle_cpu_hsw),
  1005. ICPU(INTEL_FAM6_HASWELL_X, idle_cpu_hsw),
  1006. ICPU(INTEL_FAM6_HASWELL_ULT, idle_cpu_hsw),
  1007. ICPU(INTEL_FAM6_HASWELL_GT3E, idle_cpu_hsw),
  1008. ICPU(INTEL_FAM6_ATOM_SILVERMONT2, idle_cpu_avn),
  1009. ICPU(INTEL_FAM6_BROADWELL_CORE, idle_cpu_bdw),
  1010. ICPU(INTEL_FAM6_BROADWELL_GT3E, idle_cpu_bdw),
  1011. ICPU(INTEL_FAM6_BROADWELL_X, idle_cpu_bdw),
  1012. ICPU(INTEL_FAM6_BROADWELL_XEON_D, idle_cpu_bdw),
  1013. ICPU(INTEL_FAM6_SKYLAKE_MOBILE, idle_cpu_skl),
  1014. ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, idle_cpu_skl),
  1015. ICPU(INTEL_FAM6_KABYLAKE_MOBILE, idle_cpu_skl),
  1016. ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, idle_cpu_skl),
  1017. ICPU(INTEL_FAM6_SKYLAKE_X, idle_cpu_skx),
  1018. ICPU(INTEL_FAM6_XEON_PHI_KNL, idle_cpu_knl),
  1019. ICPU(INTEL_FAM6_ATOM_GOLDMONT, idle_cpu_bxt),
  1020. ICPU(INTEL_FAM6_ATOM_DENVERTON, idle_cpu_dnv),
  1021. {}
  1022. };
  1023. /*
  1024. * intel_idle_probe()
  1025. */
  1026. static int __init intel_idle_probe(void)
  1027. {
  1028. unsigned int eax, ebx, ecx;
  1029. const struct x86_cpu_id *id;
  1030. if (max_cstate == 0) {
  1031. pr_debug(PREFIX "disabled\n");
  1032. return -EPERM;
  1033. }
  1034. id = x86_match_cpu(intel_idle_ids);
  1035. if (!id) {
  1036. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1037. boot_cpu_data.x86 == 6)
  1038. pr_debug(PREFIX "does not run on family %d model %d\n",
  1039. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1040. return -ENODEV;
  1041. }
  1042. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  1043. return -ENODEV;
  1044. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  1045. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  1046. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  1047. !mwait_substates)
  1048. return -ENODEV;
  1049. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  1050. icpu = (const struct idle_cpu *)id->driver_data;
  1051. cpuidle_state_table = icpu->state_table;
  1052. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  1053. " model 0x%X\n", boot_cpu_data.x86_model);
  1054. return 0;
  1055. }
  1056. /*
  1057. * intel_idle_cpuidle_devices_uninit()
  1058. * Unregisters the cpuidle devices.
  1059. */
  1060. static void intel_idle_cpuidle_devices_uninit(void)
  1061. {
  1062. int i;
  1063. struct cpuidle_device *dev;
  1064. for_each_online_cpu(i) {
  1065. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  1066. cpuidle_unregister_device(dev);
  1067. }
  1068. }
  1069. /*
  1070. * ivt_idle_state_table_update(void)
  1071. *
  1072. * Tune IVT multi-socket targets
  1073. * Assumption: num_sockets == (max_package_num + 1)
  1074. */
  1075. static void ivt_idle_state_table_update(void)
  1076. {
  1077. /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
  1078. int cpu, package_num, num_sockets = 1;
  1079. for_each_online_cpu(cpu) {
  1080. package_num = topology_physical_package_id(cpu);
  1081. if (package_num + 1 > num_sockets) {
  1082. num_sockets = package_num + 1;
  1083. if (num_sockets > 4) {
  1084. cpuidle_state_table = ivt_cstates_8s;
  1085. return;
  1086. }
  1087. }
  1088. }
  1089. if (num_sockets > 2)
  1090. cpuidle_state_table = ivt_cstates_4s;
  1091. /* else, 1 and 2 socket systems use default ivt_cstates */
  1092. }
  1093. /*
  1094. * Translate IRTL (Interrupt Response Time Limit) MSR to usec
  1095. */
  1096. static unsigned int irtl_ns_units[] = {
  1097. 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
  1098. static unsigned long long irtl_2_usec(unsigned long long irtl)
  1099. {
  1100. unsigned long long ns;
  1101. ns = irtl_ns_units[(irtl >> 10) & 0x3];
  1102. return div64_u64((irtl & 0x3FF) * ns, 1000);
  1103. }
  1104. /*
  1105. * bxt_idle_state_table_update(void)
  1106. *
  1107. * On BXT, we trust the IRTL to show the definitive maximum latency
  1108. * We use the same value for target_residency.
  1109. */
  1110. static void bxt_idle_state_table_update(void)
  1111. {
  1112. unsigned long long msr;
  1113. rdmsrl(MSR_PKGC6_IRTL, msr);
  1114. if (msr) {
  1115. unsigned int usec = irtl_2_usec(msr);
  1116. bxt_cstates[2].exit_latency = usec;
  1117. bxt_cstates[2].target_residency = usec;
  1118. }
  1119. rdmsrl(MSR_PKGC7_IRTL, msr);
  1120. if (msr) {
  1121. unsigned int usec = irtl_2_usec(msr);
  1122. bxt_cstates[3].exit_latency = usec;
  1123. bxt_cstates[3].target_residency = usec;
  1124. }
  1125. rdmsrl(MSR_PKGC8_IRTL, msr);
  1126. if (msr) {
  1127. unsigned int usec = irtl_2_usec(msr);
  1128. bxt_cstates[4].exit_latency = usec;
  1129. bxt_cstates[4].target_residency = usec;
  1130. }
  1131. rdmsrl(MSR_PKGC9_IRTL, msr);
  1132. if (msr) {
  1133. unsigned int usec = irtl_2_usec(msr);
  1134. bxt_cstates[5].exit_latency = usec;
  1135. bxt_cstates[5].target_residency = usec;
  1136. }
  1137. rdmsrl(MSR_PKGC10_IRTL, msr);
  1138. if (msr) {
  1139. unsigned int usec = irtl_2_usec(msr);
  1140. bxt_cstates[6].exit_latency = usec;
  1141. bxt_cstates[6].target_residency = usec;
  1142. }
  1143. }
  1144. /*
  1145. * sklh_idle_state_table_update(void)
  1146. *
  1147. * On SKL-H (model 0x5e) disable C8 and C9 if:
  1148. * C10 is enabled and SGX disabled
  1149. */
  1150. static void sklh_idle_state_table_update(void)
  1151. {
  1152. unsigned long long msr;
  1153. unsigned int eax, ebx, ecx, edx;
  1154. /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
  1155. if (max_cstate <= 7)
  1156. return;
  1157. /* if PC10 not present in CPUID.MWAIT.EDX */
  1158. if ((mwait_substates & (0xF << 28)) == 0)
  1159. return;
  1160. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr);
  1161. /* PC10 is not enabled in PKG C-state limit */
  1162. if ((msr & 0xF) != 8)
  1163. return;
  1164. ecx = 0;
  1165. cpuid(7, &eax, &ebx, &ecx, &edx);
  1166. /* if SGX is present */
  1167. if (ebx & (1 << 2)) {
  1168. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1169. /* if SGX is enabled */
  1170. if (msr & (1 << 18))
  1171. return;
  1172. }
  1173. skl_cstates[5].disabled = 1; /* C8-SKL */
  1174. skl_cstates[6].disabled = 1; /* C9-SKL */
  1175. }
  1176. /*
  1177. * intel_idle_state_table_update()
  1178. *
  1179. * Update the default state_table for this CPU-id
  1180. */
  1181. static void intel_idle_state_table_update(void)
  1182. {
  1183. switch (boot_cpu_data.x86_model) {
  1184. case INTEL_FAM6_IVYBRIDGE_X:
  1185. ivt_idle_state_table_update();
  1186. break;
  1187. case INTEL_FAM6_ATOM_GOLDMONT:
  1188. bxt_idle_state_table_update();
  1189. break;
  1190. case INTEL_FAM6_SKYLAKE_DESKTOP:
  1191. sklh_idle_state_table_update();
  1192. break;
  1193. }
  1194. }
  1195. /*
  1196. * intel_idle_cpuidle_driver_init()
  1197. * allocate, initialize cpuidle_states
  1198. */
  1199. static void __init intel_idle_cpuidle_driver_init(void)
  1200. {
  1201. int cstate;
  1202. struct cpuidle_driver *drv = &intel_idle_driver;
  1203. intel_idle_state_table_update();
  1204. drv->state_count = 1;
  1205. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  1206. int num_substates, mwait_hint, mwait_cstate;
  1207. if ((cpuidle_state_table[cstate].enter == NULL) &&
  1208. (cpuidle_state_table[cstate].enter_freeze == NULL))
  1209. break;
  1210. if (cstate + 1 > max_cstate) {
  1211. printk(PREFIX "max_cstate %d reached\n",
  1212. max_cstate);
  1213. break;
  1214. }
  1215. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  1216. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  1217. /* number of sub-states for this state in CPUID.MWAIT */
  1218. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  1219. & MWAIT_SUBSTATE_MASK;
  1220. /* if NO sub-states for this state in CPUID, skip it */
  1221. if (num_substates == 0)
  1222. continue;
  1223. /* if state marked as disabled, skip it */
  1224. if (cpuidle_state_table[cstate].disabled != 0) {
  1225. pr_debug(PREFIX "state %s is disabled",
  1226. cpuidle_state_table[cstate].name);
  1227. continue;
  1228. }
  1229. if (((mwait_cstate + 1) > 2) &&
  1230. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1231. mark_tsc_unstable("TSC halts in idle"
  1232. " states deeper than C2");
  1233. drv->states[drv->state_count] = /* structure copy */
  1234. cpuidle_state_table[cstate];
  1235. drv->state_count += 1;
  1236. }
  1237. if (icpu->byt_auto_demotion_disable_flag) {
  1238. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  1239. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  1240. }
  1241. }
  1242. /*
  1243. * intel_idle_cpu_init()
  1244. * allocate, initialize, register cpuidle_devices
  1245. * @cpu: cpu/core to initialize
  1246. */
  1247. static int intel_idle_cpu_init(int cpu)
  1248. {
  1249. struct cpuidle_device *dev;
  1250. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1251. dev->cpu = cpu;
  1252. if (cpuidle_register_device(dev)) {
  1253. pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
  1254. return -EIO;
  1255. }
  1256. if (icpu->auto_demotion_disable_flags)
  1257. smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
  1258. if (icpu->disable_promotion_to_c1e)
  1259. smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
  1260. return 0;
  1261. }
  1262. static int __init intel_idle_init(void)
  1263. {
  1264. int retval, i;
  1265. /* Do not load intel_idle at all for now if idle= is passed */
  1266. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  1267. return -ENODEV;
  1268. retval = intel_idle_probe();
  1269. if (retval)
  1270. return retval;
  1271. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  1272. if (intel_idle_cpuidle_devices == NULL)
  1273. return -ENOMEM;
  1274. intel_idle_cpuidle_driver_init();
  1275. retval = cpuidle_register_driver(&intel_idle_driver);
  1276. if (retval) {
  1277. struct cpuidle_driver *drv = cpuidle_get_driver();
  1278. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  1279. drv ? drv->name : "none");
  1280. free_percpu(intel_idle_cpuidle_devices);
  1281. return retval;
  1282. }
  1283. cpu_notifier_register_begin();
  1284. for_each_online_cpu(i) {
  1285. retval = intel_idle_cpu_init(i);
  1286. if (retval) {
  1287. intel_idle_cpuidle_devices_uninit();
  1288. cpu_notifier_register_done();
  1289. cpuidle_unregister_driver(&intel_idle_driver);
  1290. free_percpu(intel_idle_cpuidle_devices);
  1291. return retval;
  1292. }
  1293. }
  1294. __register_cpu_notifier(&cpu_hotplug_notifier);
  1295. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  1296. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  1297. else
  1298. on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
  1299. cpu_notifier_register_done();
  1300. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  1301. lapic_timer_reliable_states);
  1302. return 0;
  1303. }
  1304. device_initcall(intel_idle_init);
  1305. /*
  1306. * We are not really modular, but we used to support that. Meaning we also
  1307. * support "intel_idle.max_cstate=..." at boot and also a read-only export of
  1308. * it at /sys/module/intel_idle/parameters/max_cstate -- so using module_param
  1309. * is the easiest way (currently) to continue doing that.
  1310. */
  1311. module_param(max_cstate, int, 0444);