intel_idle.c 27 KB

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