intel_idle.c 28 KB

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