intel_idle.c 23 KB

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