intel_idle.c 37 KB

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