intel_idle.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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.1"
  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 skl_cstates[] = {
  570. {
  571. .name = "C1-SKL",
  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 = "C1E-SKL",
  580. .desc = "MWAIT 0x01",
  581. .flags = MWAIT2flg(0x01),
  582. .exit_latency = 10,
  583. .target_residency = 20,
  584. .enter = &intel_idle,
  585. .enter_freeze = intel_idle_freeze, },
  586. {
  587. .name = "C3-SKL",
  588. .desc = "MWAIT 0x10",
  589. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  590. .exit_latency = 70,
  591. .target_residency = 100,
  592. .enter = &intel_idle,
  593. .enter_freeze = intel_idle_freeze, },
  594. {
  595. .name = "C6-SKL",
  596. .desc = "MWAIT 0x20",
  597. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  598. .exit_latency = 85,
  599. .target_residency = 200,
  600. .enter = &intel_idle,
  601. .enter_freeze = intel_idle_freeze, },
  602. {
  603. .name = "C7s-SKL",
  604. .desc = "MWAIT 0x33",
  605. .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED,
  606. .exit_latency = 124,
  607. .target_residency = 800,
  608. .enter = &intel_idle,
  609. .enter_freeze = intel_idle_freeze, },
  610. {
  611. .name = "C8-SKL",
  612. .desc = "MWAIT 0x40",
  613. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  614. .exit_latency = 200,
  615. .target_residency = 800,
  616. .enter = &intel_idle,
  617. .enter_freeze = intel_idle_freeze, },
  618. {
  619. .name = "C9-SKL",
  620. .desc = "MWAIT 0x50",
  621. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  622. .exit_latency = 480,
  623. .target_residency = 5000,
  624. .enter = &intel_idle,
  625. .enter_freeze = intel_idle_freeze, },
  626. {
  627. .name = "C10-SKL",
  628. .desc = "MWAIT 0x60",
  629. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  630. .exit_latency = 890,
  631. .target_residency = 5000,
  632. .enter = &intel_idle,
  633. .enter_freeze = intel_idle_freeze, },
  634. {
  635. .enter = NULL }
  636. };
  637. static struct cpuidle_state skx_cstates[] = {
  638. {
  639. .name = "C1-SKX",
  640. .desc = "MWAIT 0x00",
  641. .flags = MWAIT2flg(0x00),
  642. .exit_latency = 2,
  643. .target_residency = 2,
  644. .enter = &intel_idle,
  645. .enter_freeze = intel_idle_freeze, },
  646. {
  647. .name = "C1E-SKX",
  648. .desc = "MWAIT 0x01",
  649. .flags = MWAIT2flg(0x01),
  650. .exit_latency = 10,
  651. .target_residency = 20,
  652. .enter = &intel_idle,
  653. .enter_freeze = intel_idle_freeze, },
  654. {
  655. .name = "C6-SKX",
  656. .desc = "MWAIT 0x20",
  657. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  658. .exit_latency = 133,
  659. .target_residency = 600,
  660. .enter = &intel_idle,
  661. .enter_freeze = intel_idle_freeze, },
  662. {
  663. .enter = NULL }
  664. };
  665. static struct cpuidle_state atom_cstates[] = {
  666. {
  667. .name = "C1E-ATM",
  668. .desc = "MWAIT 0x00",
  669. .flags = MWAIT2flg(0x00),
  670. .exit_latency = 10,
  671. .target_residency = 20,
  672. .enter = &intel_idle,
  673. .enter_freeze = intel_idle_freeze, },
  674. {
  675. .name = "C2-ATM",
  676. .desc = "MWAIT 0x10",
  677. .flags = MWAIT2flg(0x10),
  678. .exit_latency = 20,
  679. .target_residency = 80,
  680. .enter = &intel_idle,
  681. .enter_freeze = intel_idle_freeze, },
  682. {
  683. .name = "C4-ATM",
  684. .desc = "MWAIT 0x30",
  685. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  686. .exit_latency = 100,
  687. .target_residency = 400,
  688. .enter = &intel_idle,
  689. .enter_freeze = intel_idle_freeze, },
  690. {
  691. .name = "C6-ATM",
  692. .desc = "MWAIT 0x52",
  693. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  694. .exit_latency = 140,
  695. .target_residency = 560,
  696. .enter = &intel_idle,
  697. .enter_freeze = intel_idle_freeze, },
  698. {
  699. .enter = NULL }
  700. };
  701. static struct cpuidle_state avn_cstates[] = {
  702. {
  703. .name = "C1-AVN",
  704. .desc = "MWAIT 0x00",
  705. .flags = MWAIT2flg(0x00),
  706. .exit_latency = 2,
  707. .target_residency = 2,
  708. .enter = &intel_idle,
  709. .enter_freeze = intel_idle_freeze, },
  710. {
  711. .name = "C6-AVN",
  712. .desc = "MWAIT 0x51",
  713. .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
  714. .exit_latency = 15,
  715. .target_residency = 45,
  716. .enter = &intel_idle,
  717. .enter_freeze = intel_idle_freeze, },
  718. {
  719. .enter = NULL }
  720. };
  721. static struct cpuidle_state knl_cstates[] = {
  722. {
  723. .name = "C1-KNL",
  724. .desc = "MWAIT 0x00",
  725. .flags = MWAIT2flg(0x00),
  726. .exit_latency = 1,
  727. .target_residency = 2,
  728. .enter = &intel_idle,
  729. .enter_freeze = intel_idle_freeze },
  730. {
  731. .name = "C6-KNL",
  732. .desc = "MWAIT 0x10",
  733. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  734. .exit_latency = 120,
  735. .target_residency = 500,
  736. .enter = &intel_idle,
  737. .enter_freeze = intel_idle_freeze },
  738. {
  739. .enter = NULL }
  740. };
  741. static struct cpuidle_state bxt_cstates[] = {
  742. {
  743. .name = "C1-BXT",
  744. .desc = "MWAIT 0x00",
  745. .flags = MWAIT2flg(0x00),
  746. .exit_latency = 2,
  747. .target_residency = 2,
  748. .enter = &intel_idle,
  749. .enter_freeze = intel_idle_freeze, },
  750. {
  751. .name = "C1E-BXT",
  752. .desc = "MWAIT 0x01",
  753. .flags = MWAIT2flg(0x01),
  754. .exit_latency = 10,
  755. .target_residency = 20,
  756. .enter = &intel_idle,
  757. .enter_freeze = intel_idle_freeze, },
  758. {
  759. .name = "C6-BXT",
  760. .desc = "MWAIT 0x20",
  761. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  762. .exit_latency = 133,
  763. .target_residency = 133,
  764. .enter = &intel_idle,
  765. .enter_freeze = intel_idle_freeze, },
  766. {
  767. .name = "C7s-BXT",
  768. .desc = "MWAIT 0x31",
  769. .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
  770. .exit_latency = 155,
  771. .target_residency = 155,
  772. .enter = &intel_idle,
  773. .enter_freeze = intel_idle_freeze, },
  774. {
  775. .name = "C8-BXT",
  776. .desc = "MWAIT 0x40",
  777. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  778. .exit_latency = 1000,
  779. .target_residency = 1000,
  780. .enter = &intel_idle,
  781. .enter_freeze = intel_idle_freeze, },
  782. {
  783. .name = "C9-BXT",
  784. .desc = "MWAIT 0x50",
  785. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  786. .exit_latency = 2000,
  787. .target_residency = 2000,
  788. .enter = &intel_idle,
  789. .enter_freeze = intel_idle_freeze, },
  790. {
  791. .name = "C10-BXT",
  792. .desc = "MWAIT 0x60",
  793. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  794. .exit_latency = 10000,
  795. .target_residency = 10000,
  796. .enter = &intel_idle,
  797. .enter_freeze = intel_idle_freeze, },
  798. {
  799. .enter = NULL }
  800. };
  801. /**
  802. * intel_idle
  803. * @dev: cpuidle_device
  804. * @drv: cpuidle driver
  805. * @index: index of cpuidle state
  806. *
  807. * Must be called under local_irq_disable().
  808. */
  809. static int intel_idle(struct cpuidle_device *dev,
  810. struct cpuidle_driver *drv, int index)
  811. {
  812. unsigned long ecx = 1; /* break on interrupt flag */
  813. struct cpuidle_state *state = &drv->states[index];
  814. unsigned long eax = flg2MWAIT(state->flags);
  815. unsigned int cstate;
  816. int cpu = smp_processor_id();
  817. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  818. /*
  819. * leave_mm() to avoid costly and often unnecessary wakeups
  820. * for flushing the user TLB's associated with the active mm.
  821. */
  822. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  823. leave_mm(cpu);
  824. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  825. tick_broadcast_enter();
  826. mwait_idle_with_hints(eax, ecx);
  827. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  828. tick_broadcast_exit();
  829. return index;
  830. }
  831. /**
  832. * intel_idle_freeze - simplified "enter" callback routine for suspend-to-idle
  833. * @dev: cpuidle_device
  834. * @drv: cpuidle driver
  835. * @index: state index
  836. */
  837. static void intel_idle_freeze(struct cpuidle_device *dev,
  838. struct cpuidle_driver *drv, int index)
  839. {
  840. unsigned long ecx = 1; /* break on interrupt flag */
  841. unsigned long eax = flg2MWAIT(drv->states[index].flags);
  842. mwait_idle_with_hints(eax, ecx);
  843. }
  844. static void __setup_broadcast_timer(void *arg)
  845. {
  846. unsigned long on = (unsigned long)arg;
  847. if (on)
  848. tick_broadcast_enable();
  849. else
  850. tick_broadcast_disable();
  851. }
  852. static int cpu_hotplug_notify(struct notifier_block *n,
  853. unsigned long action, void *hcpu)
  854. {
  855. int hotcpu = (unsigned long)hcpu;
  856. struct cpuidle_device *dev;
  857. switch (action & ~CPU_TASKS_FROZEN) {
  858. case CPU_ONLINE:
  859. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  860. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  861. (void *)true, 1);
  862. /*
  863. * Some systems can hotplug a cpu at runtime after
  864. * the kernel has booted, we have to initialize the
  865. * driver in this case
  866. */
  867. dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
  868. if (dev->registered)
  869. break;
  870. if (intel_idle_cpu_init(hotcpu))
  871. return NOTIFY_BAD;
  872. break;
  873. }
  874. return NOTIFY_OK;
  875. }
  876. static struct notifier_block cpu_hotplug_notifier = {
  877. .notifier_call = cpu_hotplug_notify,
  878. };
  879. static void auto_demotion_disable(void *dummy)
  880. {
  881. unsigned long long msr_bits;
  882. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  883. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  884. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  885. }
  886. static void c1e_promotion_disable(void *dummy)
  887. {
  888. unsigned long long msr_bits;
  889. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  890. msr_bits &= ~0x2;
  891. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  892. }
  893. static const struct idle_cpu idle_cpu_nehalem = {
  894. .state_table = nehalem_cstates,
  895. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  896. .disable_promotion_to_c1e = true,
  897. };
  898. static const struct idle_cpu idle_cpu_atom = {
  899. .state_table = atom_cstates,
  900. };
  901. static const struct idle_cpu idle_cpu_lincroft = {
  902. .state_table = atom_cstates,
  903. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  904. };
  905. static const struct idle_cpu idle_cpu_snb = {
  906. .state_table = snb_cstates,
  907. .disable_promotion_to_c1e = true,
  908. };
  909. static const struct idle_cpu idle_cpu_byt = {
  910. .state_table = byt_cstates,
  911. .disable_promotion_to_c1e = true,
  912. .byt_auto_demotion_disable_flag = true,
  913. };
  914. static const struct idle_cpu idle_cpu_cht = {
  915. .state_table = cht_cstates,
  916. .disable_promotion_to_c1e = true,
  917. .byt_auto_demotion_disable_flag = true,
  918. };
  919. static const struct idle_cpu idle_cpu_ivb = {
  920. .state_table = ivb_cstates,
  921. .disable_promotion_to_c1e = true,
  922. };
  923. static const struct idle_cpu idle_cpu_ivt = {
  924. .state_table = ivt_cstates,
  925. .disable_promotion_to_c1e = true,
  926. };
  927. static const struct idle_cpu idle_cpu_hsw = {
  928. .state_table = hsw_cstates,
  929. .disable_promotion_to_c1e = true,
  930. };
  931. static const struct idle_cpu idle_cpu_bdw = {
  932. .state_table = bdw_cstates,
  933. .disable_promotion_to_c1e = true,
  934. };
  935. static const struct idle_cpu idle_cpu_skl = {
  936. .state_table = skl_cstates,
  937. .disable_promotion_to_c1e = true,
  938. };
  939. static const struct idle_cpu idle_cpu_skx = {
  940. .state_table = skx_cstates,
  941. .disable_promotion_to_c1e = true,
  942. };
  943. static const struct idle_cpu idle_cpu_avn = {
  944. .state_table = avn_cstates,
  945. .disable_promotion_to_c1e = true,
  946. };
  947. static const struct idle_cpu idle_cpu_knl = {
  948. .state_table = knl_cstates,
  949. };
  950. static const struct idle_cpu idle_cpu_bxt = {
  951. .state_table = bxt_cstates,
  952. .disable_promotion_to_c1e = true,
  953. };
  954. #define ICPU(model, cpu) \
  955. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
  956. static const struct x86_cpu_id intel_idle_ids[] __initconst = {
  957. ICPU(0x1a, idle_cpu_nehalem),
  958. ICPU(0x1e, idle_cpu_nehalem),
  959. ICPU(0x1f, idle_cpu_nehalem),
  960. ICPU(0x25, idle_cpu_nehalem),
  961. ICPU(0x2c, idle_cpu_nehalem),
  962. ICPU(0x2e, idle_cpu_nehalem),
  963. ICPU(0x1c, idle_cpu_atom),
  964. ICPU(0x26, idle_cpu_lincroft),
  965. ICPU(0x2f, idle_cpu_nehalem),
  966. ICPU(0x2a, idle_cpu_snb),
  967. ICPU(0x2d, idle_cpu_snb),
  968. ICPU(0x36, idle_cpu_atom),
  969. ICPU(0x37, idle_cpu_byt),
  970. ICPU(0x4c, idle_cpu_cht),
  971. ICPU(0x3a, idle_cpu_ivb),
  972. ICPU(0x3e, idle_cpu_ivt),
  973. ICPU(0x3c, idle_cpu_hsw),
  974. ICPU(0x3f, idle_cpu_hsw),
  975. ICPU(0x45, idle_cpu_hsw),
  976. ICPU(0x46, idle_cpu_hsw),
  977. ICPU(0x4d, idle_cpu_avn),
  978. ICPU(0x3d, idle_cpu_bdw),
  979. ICPU(0x47, idle_cpu_bdw),
  980. ICPU(0x4f, idle_cpu_bdw),
  981. ICPU(0x56, idle_cpu_bdw),
  982. ICPU(0x4e, idle_cpu_skl),
  983. ICPU(0x5e, idle_cpu_skl),
  984. ICPU(0x8e, idle_cpu_skl),
  985. ICPU(0x9e, idle_cpu_skl),
  986. ICPU(0x55, idle_cpu_skx),
  987. ICPU(0x57, idle_cpu_knl),
  988. ICPU(0x5c, idle_cpu_bxt),
  989. {}
  990. };
  991. MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
  992. /*
  993. * intel_idle_probe()
  994. */
  995. static int __init intel_idle_probe(void)
  996. {
  997. unsigned int eax, ebx, ecx;
  998. const struct x86_cpu_id *id;
  999. if (max_cstate == 0) {
  1000. pr_debug(PREFIX "disabled\n");
  1001. return -EPERM;
  1002. }
  1003. id = x86_match_cpu(intel_idle_ids);
  1004. if (!id) {
  1005. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1006. boot_cpu_data.x86 == 6)
  1007. pr_debug(PREFIX "does not run on family %d model %d\n",
  1008. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1009. return -ENODEV;
  1010. }
  1011. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  1012. return -ENODEV;
  1013. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  1014. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  1015. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  1016. !mwait_substates)
  1017. return -ENODEV;
  1018. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  1019. icpu = (const struct idle_cpu *)id->driver_data;
  1020. cpuidle_state_table = icpu->state_table;
  1021. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  1022. " model 0x%X\n", boot_cpu_data.x86_model);
  1023. return 0;
  1024. }
  1025. /*
  1026. * intel_idle_cpuidle_devices_uninit()
  1027. * Unregisters the cpuidle devices.
  1028. */
  1029. static void intel_idle_cpuidle_devices_uninit(void)
  1030. {
  1031. int i;
  1032. struct cpuidle_device *dev;
  1033. for_each_online_cpu(i) {
  1034. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  1035. cpuidle_unregister_device(dev);
  1036. }
  1037. }
  1038. /*
  1039. * ivt_idle_state_table_update(void)
  1040. *
  1041. * Tune IVT multi-socket targets
  1042. * Assumption: num_sockets == (max_package_num + 1)
  1043. */
  1044. static void ivt_idle_state_table_update(void)
  1045. {
  1046. /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
  1047. int cpu, package_num, num_sockets = 1;
  1048. for_each_online_cpu(cpu) {
  1049. package_num = topology_physical_package_id(cpu);
  1050. if (package_num + 1 > num_sockets) {
  1051. num_sockets = package_num + 1;
  1052. if (num_sockets > 4) {
  1053. cpuidle_state_table = ivt_cstates_8s;
  1054. return;
  1055. }
  1056. }
  1057. }
  1058. if (num_sockets > 2)
  1059. cpuidle_state_table = ivt_cstates_4s;
  1060. /* else, 1 and 2 socket systems use default ivt_cstates */
  1061. }
  1062. /*
  1063. * Translate IRTL (Interrupt Response Time Limit) MSR to usec
  1064. */
  1065. static unsigned int irtl_ns_units[] = {
  1066. 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
  1067. static unsigned long long irtl_2_usec(unsigned long long irtl)
  1068. {
  1069. unsigned long long ns;
  1070. ns = irtl_ns_units[(irtl >> 10) & 0x3];
  1071. return div64_u64((irtl & 0x3FF) * ns, 1000);
  1072. }
  1073. /*
  1074. * bxt_idle_state_table_update(void)
  1075. *
  1076. * On BXT, we trust the IRTL to show the definitive maximum latency
  1077. * We use the same value for target_residency.
  1078. */
  1079. static void bxt_idle_state_table_update(void)
  1080. {
  1081. unsigned long long msr;
  1082. rdmsrl(MSR_PKGC6_IRTL, msr);
  1083. if (msr) {
  1084. unsigned int usec = irtl_2_usec(msr);
  1085. bxt_cstates[2].exit_latency = usec;
  1086. bxt_cstates[2].target_residency = usec;
  1087. }
  1088. rdmsrl(MSR_PKGC7_IRTL, msr);
  1089. if (msr) {
  1090. unsigned int usec = irtl_2_usec(msr);
  1091. bxt_cstates[3].exit_latency = usec;
  1092. bxt_cstates[3].target_residency = usec;
  1093. }
  1094. rdmsrl(MSR_PKGC8_IRTL, msr);
  1095. if (msr) {
  1096. unsigned int usec = irtl_2_usec(msr);
  1097. bxt_cstates[4].exit_latency = usec;
  1098. bxt_cstates[4].target_residency = usec;
  1099. }
  1100. rdmsrl(MSR_PKGC9_IRTL, msr);
  1101. if (msr) {
  1102. unsigned int usec = irtl_2_usec(msr);
  1103. bxt_cstates[5].exit_latency = usec;
  1104. bxt_cstates[5].target_residency = usec;
  1105. }
  1106. rdmsrl(MSR_PKGC10_IRTL, msr);
  1107. if (msr) {
  1108. unsigned int usec = irtl_2_usec(msr);
  1109. bxt_cstates[6].exit_latency = usec;
  1110. bxt_cstates[6].target_residency = usec;
  1111. }
  1112. }
  1113. /*
  1114. * sklh_idle_state_table_update(void)
  1115. *
  1116. * On SKL-H (model 0x5e) disable C8 and C9 if:
  1117. * C10 is enabled and SGX disabled
  1118. */
  1119. static void sklh_idle_state_table_update(void)
  1120. {
  1121. unsigned long long msr;
  1122. unsigned int eax, ebx, ecx, edx;
  1123. /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
  1124. if (max_cstate <= 7)
  1125. return;
  1126. /* if PC10 not present in CPUID.MWAIT.EDX */
  1127. if ((mwait_substates & (0xF << 28)) == 0)
  1128. return;
  1129. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr);
  1130. /* PC10 is not enabled in PKG C-state limit */
  1131. if ((msr & 0xF) != 8)
  1132. return;
  1133. ecx = 0;
  1134. cpuid(7, &eax, &ebx, &ecx, &edx);
  1135. /* if SGX is present */
  1136. if (ebx & (1 << 2)) {
  1137. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1138. /* if SGX is enabled */
  1139. if (msr & (1 << 18))
  1140. return;
  1141. }
  1142. skl_cstates[5].disabled = 1; /* C8-SKL */
  1143. skl_cstates[6].disabled = 1; /* C9-SKL */
  1144. }
  1145. /*
  1146. * intel_idle_state_table_update()
  1147. *
  1148. * Update the default state_table for this CPU-id
  1149. */
  1150. static void intel_idle_state_table_update(void)
  1151. {
  1152. switch (boot_cpu_data.x86_model) {
  1153. case 0x3e: /* IVT */
  1154. ivt_idle_state_table_update();
  1155. break;
  1156. case 0x5c: /* BXT */
  1157. bxt_idle_state_table_update();
  1158. break;
  1159. case 0x5e: /* SKL-H */
  1160. sklh_idle_state_table_update();
  1161. break;
  1162. }
  1163. }
  1164. /*
  1165. * intel_idle_cpuidle_driver_init()
  1166. * allocate, initialize cpuidle_states
  1167. */
  1168. static void __init intel_idle_cpuidle_driver_init(void)
  1169. {
  1170. int cstate;
  1171. struct cpuidle_driver *drv = &intel_idle_driver;
  1172. intel_idle_state_table_update();
  1173. drv->state_count = 1;
  1174. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  1175. int num_substates, mwait_hint, mwait_cstate;
  1176. if ((cpuidle_state_table[cstate].enter == NULL) &&
  1177. (cpuidle_state_table[cstate].enter_freeze == NULL))
  1178. break;
  1179. if (cstate + 1 > max_cstate) {
  1180. printk(PREFIX "max_cstate %d reached\n",
  1181. max_cstate);
  1182. break;
  1183. }
  1184. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  1185. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  1186. /* number of sub-states for this state in CPUID.MWAIT */
  1187. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  1188. & MWAIT_SUBSTATE_MASK;
  1189. /* if NO sub-states for this state in CPUID, skip it */
  1190. if (num_substates == 0)
  1191. continue;
  1192. /* if state marked as disabled, skip it */
  1193. if (cpuidle_state_table[cstate].disabled != 0) {
  1194. pr_debug(PREFIX "state %s is disabled",
  1195. cpuidle_state_table[cstate].name);
  1196. continue;
  1197. }
  1198. if (((mwait_cstate + 1) > 2) &&
  1199. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1200. mark_tsc_unstable("TSC halts in idle"
  1201. " states deeper than C2");
  1202. drv->states[drv->state_count] = /* structure copy */
  1203. cpuidle_state_table[cstate];
  1204. drv->state_count += 1;
  1205. }
  1206. if (icpu->byt_auto_demotion_disable_flag) {
  1207. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  1208. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  1209. }
  1210. }
  1211. /*
  1212. * intel_idle_cpu_init()
  1213. * allocate, initialize, register cpuidle_devices
  1214. * @cpu: cpu/core to initialize
  1215. */
  1216. static int intel_idle_cpu_init(int cpu)
  1217. {
  1218. struct cpuidle_device *dev;
  1219. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1220. dev->cpu = cpu;
  1221. if (cpuidle_register_device(dev)) {
  1222. pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
  1223. return -EIO;
  1224. }
  1225. if (icpu->auto_demotion_disable_flags)
  1226. smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
  1227. if (icpu->disable_promotion_to_c1e)
  1228. smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
  1229. return 0;
  1230. }
  1231. static int __init intel_idle_init(void)
  1232. {
  1233. int retval, i;
  1234. /* Do not load intel_idle at all for now if idle= is passed */
  1235. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  1236. return -ENODEV;
  1237. retval = intel_idle_probe();
  1238. if (retval)
  1239. return retval;
  1240. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  1241. if (intel_idle_cpuidle_devices == NULL)
  1242. return -ENOMEM;
  1243. intel_idle_cpuidle_driver_init();
  1244. retval = cpuidle_register_driver(&intel_idle_driver);
  1245. if (retval) {
  1246. struct cpuidle_driver *drv = cpuidle_get_driver();
  1247. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  1248. drv ? drv->name : "none");
  1249. free_percpu(intel_idle_cpuidle_devices);
  1250. return retval;
  1251. }
  1252. cpu_notifier_register_begin();
  1253. for_each_online_cpu(i) {
  1254. retval = intel_idle_cpu_init(i);
  1255. if (retval) {
  1256. intel_idle_cpuidle_devices_uninit();
  1257. cpu_notifier_register_done();
  1258. cpuidle_unregister_driver(&intel_idle_driver);
  1259. free_percpu(intel_idle_cpuidle_devices);
  1260. return retval;
  1261. }
  1262. }
  1263. __register_cpu_notifier(&cpu_hotplug_notifier);
  1264. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  1265. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  1266. else
  1267. on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
  1268. cpu_notifier_register_done();
  1269. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  1270. lapic_timer_reliable_states);
  1271. return 0;
  1272. }
  1273. static void __exit intel_idle_exit(void)
  1274. {
  1275. struct cpuidle_device *dev;
  1276. int i;
  1277. cpu_notifier_register_begin();
  1278. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  1279. on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
  1280. __unregister_cpu_notifier(&cpu_hotplug_notifier);
  1281. for_each_possible_cpu(i) {
  1282. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  1283. cpuidle_unregister_device(dev);
  1284. }
  1285. cpu_notifier_register_done();
  1286. cpuidle_unregister_driver(&intel_idle_driver);
  1287. free_percpu(intel_idle_cpuidle_devices);
  1288. }
  1289. module_init(intel_idle_init);
  1290. module_exit(intel_idle_exit);
  1291. module_param(max_cstate, int, 0444);
  1292. MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
  1293. MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
  1294. MODULE_LICENSE("GPL");