intel_idle.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. * There is currently no kernel-based automatic probing/loading mechanism
  47. * if the driver is built as a module.
  48. */
  49. /* un-comment DEBUG to enable pr_debug() statements */
  50. #define DEBUG
  51. #include <linux/kernel.h>
  52. #include <linux/cpuidle.h>
  53. #include <linux/clockchips.h>
  54. #include <trace/events/power.h>
  55. #include <linux/sched.h>
  56. #include <linux/notifier.h>
  57. #include <linux/cpu.h>
  58. #include <linux/module.h>
  59. #include <asm/cpu_device_id.h>
  60. #include <asm/mwait.h>
  61. #include <asm/msr.h>
  62. #define INTEL_IDLE_VERSION "0.4"
  63. #define PREFIX "intel_idle: "
  64. static struct cpuidle_driver intel_idle_driver = {
  65. .name = "intel_idle",
  66. .owner = THIS_MODULE,
  67. };
  68. /* intel_idle.max_cstate=0 disables driver */
  69. static int max_cstate = CPUIDLE_STATE_MAX - 1;
  70. static unsigned int mwait_substates;
  71. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  72. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  73. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  74. struct idle_cpu {
  75. struct cpuidle_state *state_table;
  76. /*
  77. * Hardware C-state auto-demotion may not always be optimal.
  78. * Indicate which enable bits to clear here.
  79. */
  80. unsigned long auto_demotion_disable_flags;
  81. bool byt_auto_demotion_disable_flag;
  82. bool disable_promotion_to_c1e;
  83. };
  84. static const struct idle_cpu *icpu;
  85. static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
  86. static int intel_idle(struct cpuidle_device *dev,
  87. struct cpuidle_driver *drv, int index);
  88. static int intel_idle_cpu_init(int cpu);
  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) | CPUIDLE_FLAG_TIME_VALID,
  116. .exit_latency = 3,
  117. .target_residency = 6,
  118. .enter = &intel_idle },
  119. {
  120. .name = "C1E-NHM",
  121. .desc = "MWAIT 0x01",
  122. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  123. .exit_latency = 10,
  124. .target_residency = 20,
  125. .enter = &intel_idle },
  126. {
  127. .name = "C3-NHM",
  128. .desc = "MWAIT 0x10",
  129. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  130. .exit_latency = 20,
  131. .target_residency = 80,
  132. .enter = &intel_idle },
  133. {
  134. .name = "C6-NHM",
  135. .desc = "MWAIT 0x20",
  136. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  137. .exit_latency = 200,
  138. .target_residency = 800,
  139. .enter = &intel_idle },
  140. {
  141. .enter = NULL }
  142. };
  143. static struct cpuidle_state snb_cstates[] = {
  144. {
  145. .name = "C1-SNB",
  146. .desc = "MWAIT 0x00",
  147. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  148. .exit_latency = 2,
  149. .target_residency = 2,
  150. .enter = &intel_idle },
  151. {
  152. .name = "C1E-SNB",
  153. .desc = "MWAIT 0x01",
  154. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  155. .exit_latency = 10,
  156. .target_residency = 20,
  157. .enter = &intel_idle },
  158. {
  159. .name = "C3-SNB",
  160. .desc = "MWAIT 0x10",
  161. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  162. .exit_latency = 80,
  163. .target_residency = 211,
  164. .enter = &intel_idle },
  165. {
  166. .name = "C6-SNB",
  167. .desc = "MWAIT 0x20",
  168. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  169. .exit_latency = 104,
  170. .target_residency = 345,
  171. .enter = &intel_idle },
  172. {
  173. .name = "C7-SNB",
  174. .desc = "MWAIT 0x30",
  175. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  176. .exit_latency = 109,
  177. .target_residency = 345,
  178. .enter = &intel_idle },
  179. {
  180. .enter = NULL }
  181. };
  182. static struct cpuidle_state byt_cstates[] = {
  183. {
  184. .name = "C1-BYT",
  185. .desc = "MWAIT 0x00",
  186. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  187. .exit_latency = 1,
  188. .target_residency = 1,
  189. .enter = &intel_idle },
  190. {
  191. .name = "C1E-BYT",
  192. .desc = "MWAIT 0x01",
  193. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  194. .exit_latency = 15,
  195. .target_residency = 30,
  196. .enter = &intel_idle },
  197. {
  198. .name = "C6N-BYT",
  199. .desc = "MWAIT 0x58",
  200. .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  201. .exit_latency = 40,
  202. .target_residency = 275,
  203. .enter = &intel_idle },
  204. {
  205. .name = "C6S-BYT",
  206. .desc = "MWAIT 0x52",
  207. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  208. .exit_latency = 140,
  209. .target_residency = 560,
  210. .enter = &intel_idle },
  211. {
  212. .name = "C7-BYT",
  213. .desc = "MWAIT 0x60",
  214. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  215. .exit_latency = 1200,
  216. .target_residency = 1500,
  217. .enter = &intel_idle },
  218. {
  219. .name = "C7S-BYT",
  220. .desc = "MWAIT 0x64",
  221. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  222. .exit_latency = 10000,
  223. .target_residency = 20000,
  224. .enter = &intel_idle },
  225. {
  226. .enter = NULL }
  227. };
  228. static struct cpuidle_state ivb_cstates[] = {
  229. {
  230. .name = "C1-IVB",
  231. .desc = "MWAIT 0x00",
  232. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  233. .exit_latency = 1,
  234. .target_residency = 1,
  235. .enter = &intel_idle },
  236. {
  237. .name = "C1E-IVB",
  238. .desc = "MWAIT 0x01",
  239. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  240. .exit_latency = 10,
  241. .target_residency = 20,
  242. .enter = &intel_idle },
  243. {
  244. .name = "C3-IVB",
  245. .desc = "MWAIT 0x10",
  246. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  247. .exit_latency = 59,
  248. .target_residency = 156,
  249. .enter = &intel_idle },
  250. {
  251. .name = "C6-IVB",
  252. .desc = "MWAIT 0x20",
  253. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  254. .exit_latency = 80,
  255. .target_residency = 300,
  256. .enter = &intel_idle },
  257. {
  258. .name = "C7-IVB",
  259. .desc = "MWAIT 0x30",
  260. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  261. .exit_latency = 87,
  262. .target_residency = 300,
  263. .enter = &intel_idle },
  264. {
  265. .enter = NULL }
  266. };
  267. static struct cpuidle_state ivt_cstates[] = {
  268. {
  269. .name = "C1-IVT",
  270. .desc = "MWAIT 0x00",
  271. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  272. .exit_latency = 1,
  273. .target_residency = 1,
  274. .enter = &intel_idle },
  275. {
  276. .name = "C1E-IVT",
  277. .desc = "MWAIT 0x01",
  278. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  279. .exit_latency = 10,
  280. .target_residency = 80,
  281. .enter = &intel_idle },
  282. {
  283. .name = "C3-IVT",
  284. .desc = "MWAIT 0x10",
  285. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  286. .exit_latency = 59,
  287. .target_residency = 156,
  288. .enter = &intel_idle },
  289. {
  290. .name = "C6-IVT",
  291. .desc = "MWAIT 0x20",
  292. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  293. .exit_latency = 82,
  294. .target_residency = 300,
  295. .enter = &intel_idle },
  296. {
  297. .enter = NULL }
  298. };
  299. static struct cpuidle_state ivt_cstates_4s[] = {
  300. {
  301. .name = "C1-IVT-4S",
  302. .desc = "MWAIT 0x00",
  303. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  304. .exit_latency = 1,
  305. .target_residency = 1,
  306. .enter = &intel_idle },
  307. {
  308. .name = "C1E-IVT-4S",
  309. .desc = "MWAIT 0x01",
  310. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  311. .exit_latency = 10,
  312. .target_residency = 250,
  313. .enter = &intel_idle },
  314. {
  315. .name = "C3-IVT-4S",
  316. .desc = "MWAIT 0x10",
  317. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  318. .exit_latency = 59,
  319. .target_residency = 300,
  320. .enter = &intel_idle },
  321. {
  322. .name = "C6-IVT-4S",
  323. .desc = "MWAIT 0x20",
  324. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  325. .exit_latency = 84,
  326. .target_residency = 400,
  327. .enter = &intel_idle },
  328. {
  329. .enter = NULL }
  330. };
  331. static struct cpuidle_state ivt_cstates_8s[] = {
  332. {
  333. .name = "C1-IVT-8S",
  334. .desc = "MWAIT 0x00",
  335. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  336. .exit_latency = 1,
  337. .target_residency = 1,
  338. .enter = &intel_idle },
  339. {
  340. .name = "C1E-IVT-8S",
  341. .desc = "MWAIT 0x01",
  342. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  343. .exit_latency = 10,
  344. .target_residency = 500,
  345. .enter = &intel_idle },
  346. {
  347. .name = "C3-IVT-8S",
  348. .desc = "MWAIT 0x10",
  349. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  350. .exit_latency = 59,
  351. .target_residency = 600,
  352. .enter = &intel_idle },
  353. {
  354. .name = "C6-IVT-8S",
  355. .desc = "MWAIT 0x20",
  356. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  357. .exit_latency = 88,
  358. .target_residency = 700,
  359. .enter = &intel_idle },
  360. {
  361. .enter = NULL }
  362. };
  363. static struct cpuidle_state hsw_cstates[] = {
  364. {
  365. .name = "C1-HSW",
  366. .desc = "MWAIT 0x00",
  367. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  368. .exit_latency = 2,
  369. .target_residency = 2,
  370. .enter = &intel_idle },
  371. {
  372. .name = "C1E-HSW",
  373. .desc = "MWAIT 0x01",
  374. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  375. .exit_latency = 10,
  376. .target_residency = 20,
  377. .enter = &intel_idle },
  378. {
  379. .name = "C3-HSW",
  380. .desc = "MWAIT 0x10",
  381. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  382. .exit_latency = 33,
  383. .target_residency = 100,
  384. .enter = &intel_idle },
  385. {
  386. .name = "C6-HSW",
  387. .desc = "MWAIT 0x20",
  388. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  389. .exit_latency = 133,
  390. .target_residency = 400,
  391. .enter = &intel_idle },
  392. {
  393. .name = "C7s-HSW",
  394. .desc = "MWAIT 0x32",
  395. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  396. .exit_latency = 166,
  397. .target_residency = 500,
  398. .enter = &intel_idle },
  399. {
  400. .name = "C8-HSW",
  401. .desc = "MWAIT 0x40",
  402. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  403. .exit_latency = 300,
  404. .target_residency = 900,
  405. .enter = &intel_idle },
  406. {
  407. .name = "C9-HSW",
  408. .desc = "MWAIT 0x50",
  409. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  410. .exit_latency = 600,
  411. .target_residency = 1800,
  412. .enter = &intel_idle },
  413. {
  414. .name = "C10-HSW",
  415. .desc = "MWAIT 0x60",
  416. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  417. .exit_latency = 2600,
  418. .target_residency = 7700,
  419. .enter = &intel_idle },
  420. {
  421. .enter = NULL }
  422. };
  423. static struct cpuidle_state bdw_cstates[] = {
  424. {
  425. .name = "C1-BDW",
  426. .desc = "MWAIT 0x00",
  427. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  428. .exit_latency = 2,
  429. .target_residency = 2,
  430. .enter = &intel_idle },
  431. {
  432. .name = "C1E-BDW",
  433. .desc = "MWAIT 0x01",
  434. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  435. .exit_latency = 10,
  436. .target_residency = 20,
  437. .enter = &intel_idle },
  438. {
  439. .name = "C3-BDW",
  440. .desc = "MWAIT 0x10",
  441. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  442. .exit_latency = 40,
  443. .target_residency = 100,
  444. .enter = &intel_idle },
  445. {
  446. .name = "C6-BDW",
  447. .desc = "MWAIT 0x20",
  448. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  449. .exit_latency = 133,
  450. .target_residency = 400,
  451. .enter = &intel_idle },
  452. {
  453. .name = "C7s-BDW",
  454. .desc = "MWAIT 0x32",
  455. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  456. .exit_latency = 166,
  457. .target_residency = 500,
  458. .enter = &intel_idle },
  459. {
  460. .name = "C8-BDW",
  461. .desc = "MWAIT 0x40",
  462. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  463. .exit_latency = 300,
  464. .target_residency = 900,
  465. .enter = &intel_idle },
  466. {
  467. .name = "C9-BDW",
  468. .desc = "MWAIT 0x50",
  469. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  470. .exit_latency = 600,
  471. .target_residency = 1800,
  472. .enter = &intel_idle },
  473. {
  474. .name = "C10-BDW",
  475. .desc = "MWAIT 0x60",
  476. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  477. .exit_latency = 2600,
  478. .target_residency = 7700,
  479. .enter = &intel_idle },
  480. {
  481. .enter = NULL }
  482. };
  483. static struct cpuidle_state atom_cstates[] = {
  484. {
  485. .name = "C1E-ATM",
  486. .desc = "MWAIT 0x00",
  487. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  488. .exit_latency = 10,
  489. .target_residency = 20,
  490. .enter = &intel_idle },
  491. {
  492. .name = "C2-ATM",
  493. .desc = "MWAIT 0x10",
  494. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID,
  495. .exit_latency = 20,
  496. .target_residency = 80,
  497. .enter = &intel_idle },
  498. {
  499. .name = "C4-ATM",
  500. .desc = "MWAIT 0x30",
  501. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  502. .exit_latency = 100,
  503. .target_residency = 400,
  504. .enter = &intel_idle },
  505. {
  506. .name = "C6-ATM",
  507. .desc = "MWAIT 0x52",
  508. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  509. .exit_latency = 140,
  510. .target_residency = 560,
  511. .enter = &intel_idle },
  512. {
  513. .enter = NULL }
  514. };
  515. static struct cpuidle_state avn_cstates[] = {
  516. {
  517. .name = "C1-AVN",
  518. .desc = "MWAIT 0x00",
  519. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  520. .exit_latency = 2,
  521. .target_residency = 2,
  522. .enter = &intel_idle },
  523. {
  524. .name = "C6-AVN",
  525. .desc = "MWAIT 0x51",
  526. .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  527. .exit_latency = 15,
  528. .target_residency = 45,
  529. .enter = &intel_idle },
  530. {
  531. .enter = NULL }
  532. };
  533. /**
  534. * intel_idle
  535. * @dev: cpuidle_device
  536. * @drv: cpuidle driver
  537. * @index: index of cpuidle state
  538. *
  539. * Must be called under local_irq_disable().
  540. */
  541. static int intel_idle(struct cpuidle_device *dev,
  542. struct cpuidle_driver *drv, int index)
  543. {
  544. unsigned long ecx = 1; /* break on interrupt flag */
  545. struct cpuidle_state *state = &drv->states[index];
  546. unsigned long eax = flg2MWAIT(state->flags);
  547. unsigned int cstate;
  548. int cpu = smp_processor_id();
  549. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  550. /*
  551. * leave_mm() to avoid costly and often unnecessary wakeups
  552. * for flushing the user TLB's associated with the active mm.
  553. */
  554. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  555. leave_mm(cpu);
  556. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  557. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  558. mwait_idle_with_hints(eax, ecx);
  559. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  560. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  561. return index;
  562. }
  563. static void __setup_broadcast_timer(void *arg)
  564. {
  565. unsigned long reason = (unsigned long)arg;
  566. int cpu = smp_processor_id();
  567. reason = reason ?
  568. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  569. clockevents_notify(reason, &cpu);
  570. }
  571. static int cpu_hotplug_notify(struct notifier_block *n,
  572. unsigned long action, void *hcpu)
  573. {
  574. int hotcpu = (unsigned long)hcpu;
  575. struct cpuidle_device *dev;
  576. switch (action & ~CPU_TASKS_FROZEN) {
  577. case CPU_ONLINE:
  578. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  579. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  580. (void *)true, 1);
  581. /*
  582. * Some systems can hotplug a cpu at runtime after
  583. * the kernel has booted, we have to initialize the
  584. * driver in this case
  585. */
  586. dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
  587. if (!dev->registered)
  588. intel_idle_cpu_init(hotcpu);
  589. break;
  590. }
  591. return NOTIFY_OK;
  592. }
  593. static struct notifier_block cpu_hotplug_notifier = {
  594. .notifier_call = cpu_hotplug_notify,
  595. };
  596. static void auto_demotion_disable(void *dummy)
  597. {
  598. unsigned long long msr_bits;
  599. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  600. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  601. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  602. }
  603. static void c1e_promotion_disable(void *dummy)
  604. {
  605. unsigned long long msr_bits;
  606. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  607. msr_bits &= ~0x2;
  608. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  609. }
  610. static const struct idle_cpu idle_cpu_nehalem = {
  611. .state_table = nehalem_cstates,
  612. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  613. .disable_promotion_to_c1e = true,
  614. };
  615. static const struct idle_cpu idle_cpu_atom = {
  616. .state_table = atom_cstates,
  617. };
  618. static const struct idle_cpu idle_cpu_lincroft = {
  619. .state_table = atom_cstates,
  620. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  621. };
  622. static const struct idle_cpu idle_cpu_snb = {
  623. .state_table = snb_cstates,
  624. .disable_promotion_to_c1e = true,
  625. };
  626. static const struct idle_cpu idle_cpu_byt = {
  627. .state_table = byt_cstates,
  628. .disable_promotion_to_c1e = true,
  629. .byt_auto_demotion_disable_flag = true,
  630. };
  631. static const struct idle_cpu idle_cpu_ivb = {
  632. .state_table = ivb_cstates,
  633. .disable_promotion_to_c1e = true,
  634. };
  635. static const struct idle_cpu idle_cpu_ivt = {
  636. .state_table = ivt_cstates,
  637. .disable_promotion_to_c1e = true,
  638. };
  639. static const struct idle_cpu idle_cpu_hsw = {
  640. .state_table = hsw_cstates,
  641. .disable_promotion_to_c1e = true,
  642. };
  643. static const struct idle_cpu idle_cpu_bdw = {
  644. .state_table = bdw_cstates,
  645. .disable_promotion_to_c1e = true,
  646. };
  647. static const struct idle_cpu idle_cpu_avn = {
  648. .state_table = avn_cstates,
  649. .disable_promotion_to_c1e = true,
  650. };
  651. #define ICPU(model, cpu) \
  652. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
  653. static const struct x86_cpu_id intel_idle_ids[] = {
  654. ICPU(0x1a, idle_cpu_nehalem),
  655. ICPU(0x1e, idle_cpu_nehalem),
  656. ICPU(0x1f, idle_cpu_nehalem),
  657. ICPU(0x25, idle_cpu_nehalem),
  658. ICPU(0x2c, idle_cpu_nehalem),
  659. ICPU(0x2e, idle_cpu_nehalem),
  660. ICPU(0x1c, idle_cpu_atom),
  661. ICPU(0x26, idle_cpu_lincroft),
  662. ICPU(0x2f, idle_cpu_nehalem),
  663. ICPU(0x2a, idle_cpu_snb),
  664. ICPU(0x2d, idle_cpu_snb),
  665. ICPU(0x36, idle_cpu_atom),
  666. ICPU(0x37, idle_cpu_byt),
  667. ICPU(0x3a, idle_cpu_ivb),
  668. ICPU(0x3e, idle_cpu_ivt),
  669. ICPU(0x3c, idle_cpu_hsw),
  670. ICPU(0x3f, idle_cpu_hsw),
  671. ICPU(0x45, idle_cpu_hsw),
  672. ICPU(0x46, idle_cpu_hsw),
  673. ICPU(0x4d, idle_cpu_avn),
  674. ICPU(0x3d, idle_cpu_bdw),
  675. ICPU(0x4f, idle_cpu_bdw),
  676. ICPU(0x56, idle_cpu_bdw),
  677. {}
  678. };
  679. MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
  680. /*
  681. * intel_idle_probe()
  682. */
  683. static int __init intel_idle_probe(void)
  684. {
  685. unsigned int eax, ebx, ecx;
  686. const struct x86_cpu_id *id;
  687. if (max_cstate == 0) {
  688. pr_debug(PREFIX "disabled\n");
  689. return -EPERM;
  690. }
  691. id = x86_match_cpu(intel_idle_ids);
  692. if (!id) {
  693. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  694. boot_cpu_data.x86 == 6)
  695. pr_debug(PREFIX "does not run on family %d model %d\n",
  696. boot_cpu_data.x86, boot_cpu_data.x86_model);
  697. return -ENODEV;
  698. }
  699. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  700. return -ENODEV;
  701. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  702. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  703. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  704. !mwait_substates)
  705. return -ENODEV;
  706. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  707. icpu = (const struct idle_cpu *)id->driver_data;
  708. cpuidle_state_table = icpu->state_table;
  709. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  710. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  711. else
  712. on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
  713. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  714. " model 0x%X\n", boot_cpu_data.x86_model);
  715. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  716. lapic_timer_reliable_states);
  717. return 0;
  718. }
  719. /*
  720. * intel_idle_cpuidle_devices_uninit()
  721. * unregister, free cpuidle_devices
  722. */
  723. static void intel_idle_cpuidle_devices_uninit(void)
  724. {
  725. int i;
  726. struct cpuidle_device *dev;
  727. for_each_online_cpu(i) {
  728. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  729. cpuidle_unregister_device(dev);
  730. }
  731. free_percpu(intel_idle_cpuidle_devices);
  732. return;
  733. }
  734. /*
  735. * intel_idle_state_table_update()
  736. *
  737. * Update the default state_table for this CPU-id
  738. *
  739. * Currently used to access tuned IVT multi-socket targets
  740. * Assumption: num_sockets == (max_package_num + 1)
  741. */
  742. void intel_idle_state_table_update(void)
  743. {
  744. /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
  745. if (boot_cpu_data.x86_model == 0x3e) { /* IVT */
  746. int cpu, package_num, num_sockets = 1;
  747. for_each_online_cpu(cpu) {
  748. package_num = topology_physical_package_id(cpu);
  749. if (package_num + 1 > num_sockets) {
  750. num_sockets = package_num + 1;
  751. if (num_sockets > 4) {
  752. cpuidle_state_table = ivt_cstates_8s;
  753. return;
  754. }
  755. }
  756. }
  757. if (num_sockets > 2)
  758. cpuidle_state_table = ivt_cstates_4s;
  759. /* else, 1 and 2 socket systems use default ivt_cstates */
  760. }
  761. return;
  762. }
  763. /*
  764. * intel_idle_cpuidle_driver_init()
  765. * allocate, initialize cpuidle_states
  766. */
  767. static int __init intel_idle_cpuidle_driver_init(void)
  768. {
  769. int cstate;
  770. struct cpuidle_driver *drv = &intel_idle_driver;
  771. intel_idle_state_table_update();
  772. drv->state_count = 1;
  773. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  774. int num_substates, mwait_hint, mwait_cstate;
  775. if (cpuidle_state_table[cstate].enter == NULL)
  776. break;
  777. if (cstate + 1 > max_cstate) {
  778. printk(PREFIX "max_cstate %d reached\n",
  779. max_cstate);
  780. break;
  781. }
  782. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  783. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  784. /* number of sub-states for this state in CPUID.MWAIT */
  785. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  786. & MWAIT_SUBSTATE_MASK;
  787. /* if NO sub-states for this state in CPUID, skip it */
  788. if (num_substates == 0)
  789. continue;
  790. if (((mwait_cstate + 1) > 2) &&
  791. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  792. mark_tsc_unstable("TSC halts in idle"
  793. " states deeper than C2");
  794. drv->states[drv->state_count] = /* structure copy */
  795. cpuidle_state_table[cstate];
  796. drv->state_count += 1;
  797. }
  798. if (icpu->auto_demotion_disable_flags)
  799. on_each_cpu(auto_demotion_disable, NULL, 1);
  800. if (icpu->byt_auto_demotion_disable_flag) {
  801. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  802. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  803. }
  804. if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */
  805. on_each_cpu(c1e_promotion_disable, NULL, 1);
  806. return 0;
  807. }
  808. /*
  809. * intel_idle_cpu_init()
  810. * allocate, initialize, register cpuidle_devices
  811. * @cpu: cpu/core to initialize
  812. */
  813. static int intel_idle_cpu_init(int cpu)
  814. {
  815. struct cpuidle_device *dev;
  816. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  817. dev->cpu = cpu;
  818. if (cpuidle_register_device(dev)) {
  819. pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
  820. intel_idle_cpuidle_devices_uninit();
  821. return -EIO;
  822. }
  823. if (icpu->auto_demotion_disable_flags)
  824. smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
  825. if (icpu->disable_promotion_to_c1e)
  826. smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
  827. return 0;
  828. }
  829. static int __init intel_idle_init(void)
  830. {
  831. int retval, i;
  832. /* Do not load intel_idle at all for now if idle= is passed */
  833. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  834. return -ENODEV;
  835. retval = intel_idle_probe();
  836. if (retval)
  837. return retval;
  838. intel_idle_cpuidle_driver_init();
  839. retval = cpuidle_register_driver(&intel_idle_driver);
  840. if (retval) {
  841. struct cpuidle_driver *drv = cpuidle_get_driver();
  842. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  843. drv ? drv->name : "none");
  844. return retval;
  845. }
  846. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  847. if (intel_idle_cpuidle_devices == NULL)
  848. return -ENOMEM;
  849. cpu_notifier_register_begin();
  850. for_each_online_cpu(i) {
  851. retval = intel_idle_cpu_init(i);
  852. if (retval) {
  853. cpu_notifier_register_done();
  854. cpuidle_unregister_driver(&intel_idle_driver);
  855. return retval;
  856. }
  857. }
  858. __register_cpu_notifier(&cpu_hotplug_notifier);
  859. cpu_notifier_register_done();
  860. return 0;
  861. }
  862. static void __exit intel_idle_exit(void)
  863. {
  864. intel_idle_cpuidle_devices_uninit();
  865. cpuidle_unregister_driver(&intel_idle_driver);
  866. cpu_notifier_register_begin();
  867. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  868. on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
  869. __unregister_cpu_notifier(&cpu_hotplug_notifier);
  870. cpu_notifier_register_done();
  871. return;
  872. }
  873. module_init(intel_idle_init);
  874. module_exit(intel_idle_exit);
  875. module_param(max_cstate, int, 0444);
  876. MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
  877. MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
  878. MODULE_LICENSE("GPL");