intel_idle.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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_NEHALEM_G, 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. if (!irtl)
  1102. return 0;
  1103. ns = irtl_ns_units[(irtl >> 10) & 0x7];
  1104. return div64_u64((irtl & 0x3FF) * ns, 1000);
  1105. }
  1106. /*
  1107. * bxt_idle_state_table_update(void)
  1108. *
  1109. * On BXT, we trust the IRTL to show the definitive maximum latency
  1110. * We use the same value for target_residency.
  1111. */
  1112. static void bxt_idle_state_table_update(void)
  1113. {
  1114. unsigned long long msr;
  1115. unsigned int usec;
  1116. rdmsrl(MSR_PKGC6_IRTL, msr);
  1117. usec = irtl_2_usec(msr);
  1118. if (usec) {
  1119. bxt_cstates[2].exit_latency = usec;
  1120. bxt_cstates[2].target_residency = usec;
  1121. }
  1122. rdmsrl(MSR_PKGC7_IRTL, msr);
  1123. usec = irtl_2_usec(msr);
  1124. if (usec) {
  1125. bxt_cstates[3].exit_latency = usec;
  1126. bxt_cstates[3].target_residency = usec;
  1127. }
  1128. rdmsrl(MSR_PKGC8_IRTL, msr);
  1129. usec = irtl_2_usec(msr);
  1130. if (usec) {
  1131. bxt_cstates[4].exit_latency = usec;
  1132. bxt_cstates[4].target_residency = usec;
  1133. }
  1134. rdmsrl(MSR_PKGC9_IRTL, msr);
  1135. usec = irtl_2_usec(msr);
  1136. if (usec) {
  1137. bxt_cstates[5].exit_latency = usec;
  1138. bxt_cstates[5].target_residency = usec;
  1139. }
  1140. rdmsrl(MSR_PKGC10_IRTL, msr);
  1141. usec = irtl_2_usec(msr);
  1142. if (usec) {
  1143. bxt_cstates[6].exit_latency = usec;
  1144. bxt_cstates[6].target_residency = usec;
  1145. }
  1146. }
  1147. /*
  1148. * sklh_idle_state_table_update(void)
  1149. *
  1150. * On SKL-H (model 0x5e) disable C8 and C9 if:
  1151. * C10 is enabled and SGX disabled
  1152. */
  1153. static void sklh_idle_state_table_update(void)
  1154. {
  1155. unsigned long long msr;
  1156. unsigned int eax, ebx, ecx, edx;
  1157. /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
  1158. if (max_cstate <= 7)
  1159. return;
  1160. /* if PC10 not present in CPUID.MWAIT.EDX */
  1161. if ((mwait_substates & (0xF << 28)) == 0)
  1162. return;
  1163. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr);
  1164. /* PC10 is not enabled in PKG C-state limit */
  1165. if ((msr & 0xF) != 8)
  1166. return;
  1167. ecx = 0;
  1168. cpuid(7, &eax, &ebx, &ecx, &edx);
  1169. /* if SGX is present */
  1170. if (ebx & (1 << 2)) {
  1171. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1172. /* if SGX is enabled */
  1173. if (msr & (1 << 18))
  1174. return;
  1175. }
  1176. skl_cstates[5].disabled = 1; /* C8-SKL */
  1177. skl_cstates[6].disabled = 1; /* C9-SKL */
  1178. }
  1179. /*
  1180. * intel_idle_state_table_update()
  1181. *
  1182. * Update the default state_table for this CPU-id
  1183. */
  1184. static void intel_idle_state_table_update(void)
  1185. {
  1186. switch (boot_cpu_data.x86_model) {
  1187. case INTEL_FAM6_IVYBRIDGE_X:
  1188. ivt_idle_state_table_update();
  1189. break;
  1190. case INTEL_FAM6_ATOM_GOLDMONT:
  1191. bxt_idle_state_table_update();
  1192. break;
  1193. case INTEL_FAM6_SKYLAKE_DESKTOP:
  1194. sklh_idle_state_table_update();
  1195. break;
  1196. }
  1197. }
  1198. /*
  1199. * intel_idle_cpuidle_driver_init()
  1200. * allocate, initialize cpuidle_states
  1201. */
  1202. static void __init intel_idle_cpuidle_driver_init(void)
  1203. {
  1204. int cstate;
  1205. struct cpuidle_driver *drv = &intel_idle_driver;
  1206. intel_idle_state_table_update();
  1207. drv->state_count = 1;
  1208. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  1209. int num_substates, mwait_hint, mwait_cstate;
  1210. if ((cpuidle_state_table[cstate].enter == NULL) &&
  1211. (cpuidle_state_table[cstate].enter_freeze == NULL))
  1212. break;
  1213. if (cstate + 1 > max_cstate) {
  1214. printk(PREFIX "max_cstate %d reached\n",
  1215. max_cstate);
  1216. break;
  1217. }
  1218. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  1219. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  1220. /* number of sub-states for this state in CPUID.MWAIT */
  1221. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  1222. & MWAIT_SUBSTATE_MASK;
  1223. /* if NO sub-states for this state in CPUID, skip it */
  1224. if (num_substates == 0)
  1225. continue;
  1226. /* if state marked as disabled, skip it */
  1227. if (cpuidle_state_table[cstate].disabled != 0) {
  1228. pr_debug(PREFIX "state %s is disabled",
  1229. cpuidle_state_table[cstate].name);
  1230. continue;
  1231. }
  1232. if (((mwait_cstate + 1) > 2) &&
  1233. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1234. mark_tsc_unstable("TSC halts in idle"
  1235. " states deeper than C2");
  1236. drv->states[drv->state_count] = /* structure copy */
  1237. cpuidle_state_table[cstate];
  1238. drv->state_count += 1;
  1239. }
  1240. if (icpu->byt_auto_demotion_disable_flag) {
  1241. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  1242. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  1243. }
  1244. }
  1245. /*
  1246. * intel_idle_cpu_init()
  1247. * allocate, initialize, register cpuidle_devices
  1248. * @cpu: cpu/core to initialize
  1249. */
  1250. static int intel_idle_cpu_init(int cpu)
  1251. {
  1252. struct cpuidle_device *dev;
  1253. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1254. dev->cpu = cpu;
  1255. if (cpuidle_register_device(dev)) {
  1256. pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
  1257. return -EIO;
  1258. }
  1259. if (icpu->auto_demotion_disable_flags)
  1260. smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
  1261. if (icpu->disable_promotion_to_c1e)
  1262. smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
  1263. return 0;
  1264. }
  1265. static int __init intel_idle_init(void)
  1266. {
  1267. int retval, i;
  1268. /* Do not load intel_idle at all for now if idle= is passed */
  1269. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  1270. return -ENODEV;
  1271. retval = intel_idle_probe();
  1272. if (retval)
  1273. return retval;
  1274. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  1275. if (intel_idle_cpuidle_devices == NULL)
  1276. return -ENOMEM;
  1277. intel_idle_cpuidle_driver_init();
  1278. retval = cpuidle_register_driver(&intel_idle_driver);
  1279. if (retval) {
  1280. struct cpuidle_driver *drv = cpuidle_get_driver();
  1281. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  1282. drv ? drv->name : "none");
  1283. free_percpu(intel_idle_cpuidle_devices);
  1284. return retval;
  1285. }
  1286. cpu_notifier_register_begin();
  1287. for_each_online_cpu(i) {
  1288. retval = intel_idle_cpu_init(i);
  1289. if (retval) {
  1290. intel_idle_cpuidle_devices_uninit();
  1291. cpu_notifier_register_done();
  1292. cpuidle_unregister_driver(&intel_idle_driver);
  1293. free_percpu(intel_idle_cpuidle_devices);
  1294. return retval;
  1295. }
  1296. }
  1297. __register_cpu_notifier(&cpu_hotplug_notifier);
  1298. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  1299. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  1300. else
  1301. on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
  1302. cpu_notifier_register_done();
  1303. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  1304. lapic_timer_reliable_states);
  1305. return 0;
  1306. }
  1307. device_initcall(intel_idle_init);
  1308. /*
  1309. * We are not really modular, but we used to support that. Meaning we also
  1310. * support "intel_idle.max_cstate=..." at boot and also a read-only export of
  1311. * it at /sys/module/intel_idle/parameters/max_cstate -- so using module_param
  1312. * is the easiest way (currently) to continue doing that.
  1313. */
  1314. module_param(max_cstate, int, 0444);