intel_idle.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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. */
  47. /* un-comment DEBUG to enable pr_debug() statements */
  48. #define DEBUG
  49. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  50. #include <linux/kernel.h>
  51. #include <linux/cpuidle.h>
  52. #include <linux/tick.h>
  53. #include <trace/events/power.h>
  54. #include <linux/sched.h>
  55. #include <linux/notifier.h>
  56. #include <linux/cpu.h>
  57. #include <linux/moduleparam.h>
  58. #include <asm/cpu_device_id.h>
  59. #include <asm/intel-family.h>
  60. #include <asm/mwait.h>
  61. #include <asm/msr.h>
  62. #define INTEL_IDLE_VERSION "0.4.1"
  63. static struct cpuidle_driver intel_idle_driver = {
  64. .name = "intel_idle",
  65. .owner = THIS_MODULE,
  66. };
  67. /* intel_idle.max_cstate=0 disables driver */
  68. static int max_cstate = CPUIDLE_STATE_MAX - 1;
  69. static unsigned int mwait_substates;
  70. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  71. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  72. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  73. struct idle_cpu {
  74. struct cpuidle_state *state_table;
  75. /*
  76. * Hardware C-state auto-demotion may not always be optimal.
  77. * Indicate which enable bits to clear here.
  78. */
  79. unsigned long auto_demotion_disable_flags;
  80. bool byt_auto_demotion_disable_flag;
  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 void intel_idle_s2idle(struct cpuidle_device *dev,
  88. struct cpuidle_driver *drv, int index);
  89. static struct cpuidle_state *cpuidle_state_table;
  90. /*
  91. * Set this flag for states where the HW flushes the TLB for us
  92. * and so we don't need cross-calls to keep it consistent.
  93. * If this flag is set, SW flushes the TLB, so even if the
  94. * HW doesn't do the flushing, this flag is safe to use.
  95. */
  96. #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
  97. /*
  98. * MWAIT takes an 8-bit "hint" in EAX "suggesting"
  99. * the C-state (top nibble) and sub-state (bottom nibble)
  100. * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
  101. *
  102. * We store the hint at the top of our "flags" for each state.
  103. */
  104. #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
  105. #define MWAIT2flg(eax) ((eax & 0xFF) << 24)
  106. /*
  107. * States are indexed by the cstate number,
  108. * which is also the index into the MWAIT hint array.
  109. * Thus C0 is a dummy.
  110. */
  111. static struct cpuidle_state nehalem_cstates[] = {
  112. {
  113. .name = "C1",
  114. .desc = "MWAIT 0x00",
  115. .flags = MWAIT2flg(0x00),
  116. .exit_latency = 3,
  117. .target_residency = 6,
  118. .enter = &intel_idle,
  119. .enter_s2idle = intel_idle_s2idle, },
  120. {
  121. .name = "C1E",
  122. .desc = "MWAIT 0x01",
  123. .flags = MWAIT2flg(0x01),
  124. .exit_latency = 10,
  125. .target_residency = 20,
  126. .enter = &intel_idle,
  127. .enter_s2idle = intel_idle_s2idle, },
  128. {
  129. .name = "C3",
  130. .desc = "MWAIT 0x10",
  131. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  132. .exit_latency = 20,
  133. .target_residency = 80,
  134. .enter = &intel_idle,
  135. .enter_s2idle = intel_idle_s2idle, },
  136. {
  137. .name = "C6",
  138. .desc = "MWAIT 0x20",
  139. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  140. .exit_latency = 200,
  141. .target_residency = 800,
  142. .enter = &intel_idle,
  143. .enter_s2idle = intel_idle_s2idle, },
  144. {
  145. .enter = NULL }
  146. };
  147. static struct cpuidle_state snb_cstates[] = {
  148. {
  149. .name = "C1",
  150. .desc = "MWAIT 0x00",
  151. .flags = MWAIT2flg(0x00),
  152. .exit_latency = 2,
  153. .target_residency = 2,
  154. .enter = &intel_idle,
  155. .enter_s2idle = intel_idle_s2idle, },
  156. {
  157. .name = "C1E",
  158. .desc = "MWAIT 0x01",
  159. .flags = MWAIT2flg(0x01),
  160. .exit_latency = 10,
  161. .target_residency = 20,
  162. .enter = &intel_idle,
  163. .enter_s2idle = intel_idle_s2idle, },
  164. {
  165. .name = "C3",
  166. .desc = "MWAIT 0x10",
  167. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  168. .exit_latency = 80,
  169. .target_residency = 211,
  170. .enter = &intel_idle,
  171. .enter_s2idle = intel_idle_s2idle, },
  172. {
  173. .name = "C6",
  174. .desc = "MWAIT 0x20",
  175. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  176. .exit_latency = 104,
  177. .target_residency = 345,
  178. .enter = &intel_idle,
  179. .enter_s2idle = intel_idle_s2idle, },
  180. {
  181. .name = "C7",
  182. .desc = "MWAIT 0x30",
  183. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  184. .exit_latency = 109,
  185. .target_residency = 345,
  186. .enter = &intel_idle,
  187. .enter_s2idle = intel_idle_s2idle, },
  188. {
  189. .enter = NULL }
  190. };
  191. static struct cpuidle_state byt_cstates[] = {
  192. {
  193. .name = "C1",
  194. .desc = "MWAIT 0x00",
  195. .flags = MWAIT2flg(0x00),
  196. .exit_latency = 1,
  197. .target_residency = 1,
  198. .enter = &intel_idle,
  199. .enter_s2idle = intel_idle_s2idle, },
  200. {
  201. .name = "C6N",
  202. .desc = "MWAIT 0x58",
  203. .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
  204. .exit_latency = 300,
  205. .target_residency = 275,
  206. .enter = &intel_idle,
  207. .enter_s2idle = intel_idle_s2idle, },
  208. {
  209. .name = "C6S",
  210. .desc = "MWAIT 0x52",
  211. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  212. .exit_latency = 500,
  213. .target_residency = 560,
  214. .enter = &intel_idle,
  215. .enter_s2idle = intel_idle_s2idle, },
  216. {
  217. .name = "C7",
  218. .desc = "MWAIT 0x60",
  219. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  220. .exit_latency = 1200,
  221. .target_residency = 4000,
  222. .enter = &intel_idle,
  223. .enter_s2idle = intel_idle_s2idle, },
  224. {
  225. .name = "C7S",
  226. .desc = "MWAIT 0x64",
  227. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  228. .exit_latency = 10000,
  229. .target_residency = 20000,
  230. .enter = &intel_idle,
  231. .enter_s2idle = intel_idle_s2idle, },
  232. {
  233. .enter = NULL }
  234. };
  235. static struct cpuidle_state cht_cstates[] = {
  236. {
  237. .name = "C1",
  238. .desc = "MWAIT 0x00",
  239. .flags = MWAIT2flg(0x00),
  240. .exit_latency = 1,
  241. .target_residency = 1,
  242. .enter = &intel_idle,
  243. .enter_s2idle = intel_idle_s2idle, },
  244. {
  245. .name = "C6N",
  246. .desc = "MWAIT 0x58",
  247. .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
  248. .exit_latency = 80,
  249. .target_residency = 275,
  250. .enter = &intel_idle,
  251. .enter_s2idle = intel_idle_s2idle, },
  252. {
  253. .name = "C6S",
  254. .desc = "MWAIT 0x52",
  255. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  256. .exit_latency = 200,
  257. .target_residency = 560,
  258. .enter = &intel_idle,
  259. .enter_s2idle = intel_idle_s2idle, },
  260. {
  261. .name = "C7",
  262. .desc = "MWAIT 0x60",
  263. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  264. .exit_latency = 1200,
  265. .target_residency = 4000,
  266. .enter = &intel_idle,
  267. .enter_s2idle = intel_idle_s2idle, },
  268. {
  269. .name = "C7S",
  270. .desc = "MWAIT 0x64",
  271. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  272. .exit_latency = 10000,
  273. .target_residency = 20000,
  274. .enter = &intel_idle,
  275. .enter_s2idle = intel_idle_s2idle, },
  276. {
  277. .enter = NULL }
  278. };
  279. static struct cpuidle_state ivb_cstates[] = {
  280. {
  281. .name = "C1",
  282. .desc = "MWAIT 0x00",
  283. .flags = MWAIT2flg(0x00),
  284. .exit_latency = 1,
  285. .target_residency = 1,
  286. .enter = &intel_idle,
  287. .enter_s2idle = intel_idle_s2idle, },
  288. {
  289. .name = "C1E",
  290. .desc = "MWAIT 0x01",
  291. .flags = MWAIT2flg(0x01),
  292. .exit_latency = 10,
  293. .target_residency = 20,
  294. .enter = &intel_idle,
  295. .enter_s2idle = intel_idle_s2idle, },
  296. {
  297. .name = "C3",
  298. .desc = "MWAIT 0x10",
  299. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  300. .exit_latency = 59,
  301. .target_residency = 156,
  302. .enter = &intel_idle,
  303. .enter_s2idle = intel_idle_s2idle, },
  304. {
  305. .name = "C6",
  306. .desc = "MWAIT 0x20",
  307. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  308. .exit_latency = 80,
  309. .target_residency = 300,
  310. .enter = &intel_idle,
  311. .enter_s2idle = intel_idle_s2idle, },
  312. {
  313. .name = "C7",
  314. .desc = "MWAIT 0x30",
  315. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  316. .exit_latency = 87,
  317. .target_residency = 300,
  318. .enter = &intel_idle,
  319. .enter_s2idle = intel_idle_s2idle, },
  320. {
  321. .enter = NULL }
  322. };
  323. static struct cpuidle_state ivt_cstates[] = {
  324. {
  325. .name = "C1",
  326. .desc = "MWAIT 0x00",
  327. .flags = MWAIT2flg(0x00),
  328. .exit_latency = 1,
  329. .target_residency = 1,
  330. .enter = &intel_idle,
  331. .enter_s2idle = intel_idle_s2idle, },
  332. {
  333. .name = "C1E",
  334. .desc = "MWAIT 0x01",
  335. .flags = MWAIT2flg(0x01),
  336. .exit_latency = 10,
  337. .target_residency = 80,
  338. .enter = &intel_idle,
  339. .enter_s2idle = intel_idle_s2idle, },
  340. {
  341. .name = "C3",
  342. .desc = "MWAIT 0x10",
  343. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  344. .exit_latency = 59,
  345. .target_residency = 156,
  346. .enter = &intel_idle,
  347. .enter_s2idle = intel_idle_s2idle, },
  348. {
  349. .name = "C6",
  350. .desc = "MWAIT 0x20",
  351. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  352. .exit_latency = 82,
  353. .target_residency = 300,
  354. .enter = &intel_idle,
  355. .enter_s2idle = intel_idle_s2idle, },
  356. {
  357. .enter = NULL }
  358. };
  359. static struct cpuidle_state ivt_cstates_4s[] = {
  360. {
  361. .name = "C1",
  362. .desc = "MWAIT 0x00",
  363. .flags = MWAIT2flg(0x00),
  364. .exit_latency = 1,
  365. .target_residency = 1,
  366. .enter = &intel_idle,
  367. .enter_s2idle = intel_idle_s2idle, },
  368. {
  369. .name = "C1E",
  370. .desc = "MWAIT 0x01",
  371. .flags = MWAIT2flg(0x01),
  372. .exit_latency = 10,
  373. .target_residency = 250,
  374. .enter = &intel_idle,
  375. .enter_s2idle = intel_idle_s2idle, },
  376. {
  377. .name = "C3",
  378. .desc = "MWAIT 0x10",
  379. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  380. .exit_latency = 59,
  381. .target_residency = 300,
  382. .enter = &intel_idle,
  383. .enter_s2idle = intel_idle_s2idle, },
  384. {
  385. .name = "C6",
  386. .desc = "MWAIT 0x20",
  387. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  388. .exit_latency = 84,
  389. .target_residency = 400,
  390. .enter = &intel_idle,
  391. .enter_s2idle = intel_idle_s2idle, },
  392. {
  393. .enter = NULL }
  394. };
  395. static struct cpuidle_state ivt_cstates_8s[] = {
  396. {
  397. .name = "C1",
  398. .desc = "MWAIT 0x00",
  399. .flags = MWAIT2flg(0x00),
  400. .exit_latency = 1,
  401. .target_residency = 1,
  402. .enter = &intel_idle,
  403. .enter_s2idle = intel_idle_s2idle, },
  404. {
  405. .name = "C1E",
  406. .desc = "MWAIT 0x01",
  407. .flags = MWAIT2flg(0x01),
  408. .exit_latency = 10,
  409. .target_residency = 500,
  410. .enter = &intel_idle,
  411. .enter_s2idle = intel_idle_s2idle, },
  412. {
  413. .name = "C3",
  414. .desc = "MWAIT 0x10",
  415. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  416. .exit_latency = 59,
  417. .target_residency = 600,
  418. .enter = &intel_idle,
  419. .enter_s2idle = intel_idle_s2idle, },
  420. {
  421. .name = "C6",
  422. .desc = "MWAIT 0x20",
  423. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  424. .exit_latency = 88,
  425. .target_residency = 700,
  426. .enter = &intel_idle,
  427. .enter_s2idle = intel_idle_s2idle, },
  428. {
  429. .enter = NULL }
  430. };
  431. static struct cpuidle_state hsw_cstates[] = {
  432. {
  433. .name = "C1",
  434. .desc = "MWAIT 0x00",
  435. .flags = MWAIT2flg(0x00),
  436. .exit_latency = 2,
  437. .target_residency = 2,
  438. .enter = &intel_idle,
  439. .enter_s2idle = intel_idle_s2idle, },
  440. {
  441. .name = "C1E",
  442. .desc = "MWAIT 0x01",
  443. .flags = MWAIT2flg(0x01),
  444. .exit_latency = 10,
  445. .target_residency = 20,
  446. .enter = &intel_idle,
  447. .enter_s2idle = intel_idle_s2idle, },
  448. {
  449. .name = "C3",
  450. .desc = "MWAIT 0x10",
  451. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  452. .exit_latency = 33,
  453. .target_residency = 100,
  454. .enter = &intel_idle,
  455. .enter_s2idle = intel_idle_s2idle, },
  456. {
  457. .name = "C6",
  458. .desc = "MWAIT 0x20",
  459. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  460. .exit_latency = 133,
  461. .target_residency = 400,
  462. .enter = &intel_idle,
  463. .enter_s2idle = intel_idle_s2idle, },
  464. {
  465. .name = "C7s",
  466. .desc = "MWAIT 0x32",
  467. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
  468. .exit_latency = 166,
  469. .target_residency = 500,
  470. .enter = &intel_idle,
  471. .enter_s2idle = intel_idle_s2idle, },
  472. {
  473. .name = "C8",
  474. .desc = "MWAIT 0x40",
  475. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  476. .exit_latency = 300,
  477. .target_residency = 900,
  478. .enter = &intel_idle,
  479. .enter_s2idle = intel_idle_s2idle, },
  480. {
  481. .name = "C9",
  482. .desc = "MWAIT 0x50",
  483. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  484. .exit_latency = 600,
  485. .target_residency = 1800,
  486. .enter = &intel_idle,
  487. .enter_s2idle = intel_idle_s2idle, },
  488. {
  489. .name = "C10",
  490. .desc = "MWAIT 0x60",
  491. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  492. .exit_latency = 2600,
  493. .target_residency = 7700,
  494. .enter = &intel_idle,
  495. .enter_s2idle = intel_idle_s2idle, },
  496. {
  497. .enter = NULL }
  498. };
  499. static struct cpuidle_state bdw_cstates[] = {
  500. {
  501. .name = "C1",
  502. .desc = "MWAIT 0x00",
  503. .flags = MWAIT2flg(0x00),
  504. .exit_latency = 2,
  505. .target_residency = 2,
  506. .enter = &intel_idle,
  507. .enter_s2idle = intel_idle_s2idle, },
  508. {
  509. .name = "C1E",
  510. .desc = "MWAIT 0x01",
  511. .flags = MWAIT2flg(0x01),
  512. .exit_latency = 10,
  513. .target_residency = 20,
  514. .enter = &intel_idle,
  515. .enter_s2idle = intel_idle_s2idle, },
  516. {
  517. .name = "C3",
  518. .desc = "MWAIT 0x10",
  519. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  520. .exit_latency = 40,
  521. .target_residency = 100,
  522. .enter = &intel_idle,
  523. .enter_s2idle = intel_idle_s2idle, },
  524. {
  525. .name = "C6",
  526. .desc = "MWAIT 0x20",
  527. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  528. .exit_latency = 133,
  529. .target_residency = 400,
  530. .enter = &intel_idle,
  531. .enter_s2idle = intel_idle_s2idle, },
  532. {
  533. .name = "C7s",
  534. .desc = "MWAIT 0x32",
  535. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
  536. .exit_latency = 166,
  537. .target_residency = 500,
  538. .enter = &intel_idle,
  539. .enter_s2idle = intel_idle_s2idle, },
  540. {
  541. .name = "C8",
  542. .desc = "MWAIT 0x40",
  543. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  544. .exit_latency = 300,
  545. .target_residency = 900,
  546. .enter = &intel_idle,
  547. .enter_s2idle = intel_idle_s2idle, },
  548. {
  549. .name = "C9",
  550. .desc = "MWAIT 0x50",
  551. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  552. .exit_latency = 600,
  553. .target_residency = 1800,
  554. .enter = &intel_idle,
  555. .enter_s2idle = intel_idle_s2idle, },
  556. {
  557. .name = "C10",
  558. .desc = "MWAIT 0x60",
  559. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  560. .exit_latency = 2600,
  561. .target_residency = 7700,
  562. .enter = &intel_idle,
  563. .enter_s2idle = intel_idle_s2idle, },
  564. {
  565. .enter = NULL }
  566. };
  567. static struct cpuidle_state skl_cstates[] = {
  568. {
  569. .name = "C1",
  570. .desc = "MWAIT 0x00",
  571. .flags = MWAIT2flg(0x00),
  572. .exit_latency = 2,
  573. .target_residency = 2,
  574. .enter = &intel_idle,
  575. .enter_s2idle = intel_idle_s2idle, },
  576. {
  577. .name = "C1E",
  578. .desc = "MWAIT 0x01",
  579. .flags = MWAIT2flg(0x01),
  580. .exit_latency = 10,
  581. .target_residency = 20,
  582. .enter = &intel_idle,
  583. .enter_s2idle = intel_idle_s2idle, },
  584. {
  585. .name = "C3",
  586. .desc = "MWAIT 0x10",
  587. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  588. .exit_latency = 70,
  589. .target_residency = 100,
  590. .enter = &intel_idle,
  591. .enter_s2idle = intel_idle_s2idle, },
  592. {
  593. .name = "C6",
  594. .desc = "MWAIT 0x20",
  595. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  596. .exit_latency = 85,
  597. .target_residency = 200,
  598. .enter = &intel_idle,
  599. .enter_s2idle = intel_idle_s2idle, },
  600. {
  601. .name = "C7s",
  602. .desc = "MWAIT 0x33",
  603. .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED,
  604. .exit_latency = 124,
  605. .target_residency = 800,
  606. .enter = &intel_idle,
  607. .enter_s2idle = intel_idle_s2idle, },
  608. {
  609. .name = "C8",
  610. .desc = "MWAIT 0x40",
  611. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  612. .exit_latency = 200,
  613. .target_residency = 800,
  614. .enter = &intel_idle,
  615. .enter_s2idle = intel_idle_s2idle, },
  616. {
  617. .name = "C9",
  618. .desc = "MWAIT 0x50",
  619. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  620. .exit_latency = 480,
  621. .target_residency = 5000,
  622. .enter = &intel_idle,
  623. .enter_s2idle = intel_idle_s2idle, },
  624. {
  625. .name = "C10",
  626. .desc = "MWAIT 0x60",
  627. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  628. .exit_latency = 890,
  629. .target_residency = 5000,
  630. .enter = &intel_idle,
  631. .enter_s2idle = intel_idle_s2idle, },
  632. {
  633. .enter = NULL }
  634. };
  635. static struct cpuidle_state skx_cstates[] = {
  636. {
  637. .name = "C1",
  638. .desc = "MWAIT 0x00",
  639. .flags = MWAIT2flg(0x00),
  640. .exit_latency = 2,
  641. .target_residency = 2,
  642. .enter = &intel_idle,
  643. .enter_s2idle = intel_idle_s2idle, },
  644. {
  645. .name = "C1E",
  646. .desc = "MWAIT 0x01",
  647. .flags = MWAIT2flg(0x01),
  648. .exit_latency = 10,
  649. .target_residency = 20,
  650. .enter = &intel_idle,
  651. .enter_s2idle = intel_idle_s2idle, },
  652. {
  653. .name = "C6",
  654. .desc = "MWAIT 0x20",
  655. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  656. .exit_latency = 133,
  657. .target_residency = 600,
  658. .enter = &intel_idle,
  659. .enter_s2idle = intel_idle_s2idle, },
  660. {
  661. .enter = NULL }
  662. };
  663. static struct cpuidle_state atom_cstates[] = {
  664. {
  665. .name = "C1E",
  666. .desc = "MWAIT 0x00",
  667. .flags = MWAIT2flg(0x00),
  668. .exit_latency = 10,
  669. .target_residency = 20,
  670. .enter = &intel_idle,
  671. .enter_s2idle = intel_idle_s2idle, },
  672. {
  673. .name = "C2",
  674. .desc = "MWAIT 0x10",
  675. .flags = MWAIT2flg(0x10),
  676. .exit_latency = 20,
  677. .target_residency = 80,
  678. .enter = &intel_idle,
  679. .enter_s2idle = intel_idle_s2idle, },
  680. {
  681. .name = "C4",
  682. .desc = "MWAIT 0x30",
  683. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  684. .exit_latency = 100,
  685. .target_residency = 400,
  686. .enter = &intel_idle,
  687. .enter_s2idle = intel_idle_s2idle, },
  688. {
  689. .name = "C6",
  690. .desc = "MWAIT 0x52",
  691. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  692. .exit_latency = 140,
  693. .target_residency = 560,
  694. .enter = &intel_idle,
  695. .enter_s2idle = intel_idle_s2idle, },
  696. {
  697. .enter = NULL }
  698. };
  699. static struct cpuidle_state tangier_cstates[] = {
  700. {
  701. .name = "C1",
  702. .desc = "MWAIT 0x00",
  703. .flags = MWAIT2flg(0x00),
  704. .exit_latency = 1,
  705. .target_residency = 4,
  706. .enter = &intel_idle,
  707. .enter_s2idle = intel_idle_s2idle, },
  708. {
  709. .name = "C4",
  710. .desc = "MWAIT 0x30",
  711. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
  712. .exit_latency = 100,
  713. .target_residency = 400,
  714. .enter = &intel_idle,
  715. .enter_s2idle = intel_idle_s2idle, },
  716. {
  717. .name = "C6",
  718. .desc = "MWAIT 0x52",
  719. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
  720. .exit_latency = 140,
  721. .target_residency = 560,
  722. .enter = &intel_idle,
  723. .enter_s2idle = intel_idle_s2idle, },
  724. {
  725. .name = "C7",
  726. .desc = "MWAIT 0x60",
  727. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  728. .exit_latency = 1200,
  729. .target_residency = 4000,
  730. .enter = &intel_idle,
  731. .enter_s2idle = intel_idle_s2idle, },
  732. {
  733. .name = "C9",
  734. .desc = "MWAIT 0x64",
  735. .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
  736. .exit_latency = 10000,
  737. .target_residency = 20000,
  738. .enter = &intel_idle,
  739. .enter_s2idle = intel_idle_s2idle, },
  740. {
  741. .enter = NULL }
  742. };
  743. static struct cpuidle_state avn_cstates[] = {
  744. {
  745. .name = "C1",
  746. .desc = "MWAIT 0x00",
  747. .flags = MWAIT2flg(0x00),
  748. .exit_latency = 2,
  749. .target_residency = 2,
  750. .enter = &intel_idle,
  751. .enter_s2idle = intel_idle_s2idle, },
  752. {
  753. .name = "C6",
  754. .desc = "MWAIT 0x51",
  755. .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
  756. .exit_latency = 15,
  757. .target_residency = 45,
  758. .enter = &intel_idle,
  759. .enter_s2idle = intel_idle_s2idle, },
  760. {
  761. .enter = NULL }
  762. };
  763. static struct cpuidle_state knl_cstates[] = {
  764. {
  765. .name = "C1",
  766. .desc = "MWAIT 0x00",
  767. .flags = MWAIT2flg(0x00),
  768. .exit_latency = 1,
  769. .target_residency = 2,
  770. .enter = &intel_idle,
  771. .enter_s2idle = intel_idle_s2idle },
  772. {
  773. .name = "C6",
  774. .desc = "MWAIT 0x10",
  775. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
  776. .exit_latency = 120,
  777. .target_residency = 500,
  778. .enter = &intel_idle,
  779. .enter_s2idle = intel_idle_s2idle },
  780. {
  781. .enter = NULL }
  782. };
  783. static struct cpuidle_state bxt_cstates[] = {
  784. {
  785. .name = "C1",
  786. .desc = "MWAIT 0x00",
  787. .flags = MWAIT2flg(0x00),
  788. .exit_latency = 2,
  789. .target_residency = 2,
  790. .enter = &intel_idle,
  791. .enter_s2idle = intel_idle_s2idle, },
  792. {
  793. .name = "C1E",
  794. .desc = "MWAIT 0x01",
  795. .flags = MWAIT2flg(0x01),
  796. .exit_latency = 10,
  797. .target_residency = 20,
  798. .enter = &intel_idle,
  799. .enter_s2idle = intel_idle_s2idle, },
  800. {
  801. .name = "C6",
  802. .desc = "MWAIT 0x20",
  803. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  804. .exit_latency = 133,
  805. .target_residency = 133,
  806. .enter = &intel_idle,
  807. .enter_s2idle = intel_idle_s2idle, },
  808. {
  809. .name = "C7s",
  810. .desc = "MWAIT 0x31",
  811. .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
  812. .exit_latency = 155,
  813. .target_residency = 155,
  814. .enter = &intel_idle,
  815. .enter_s2idle = intel_idle_s2idle, },
  816. {
  817. .name = "C8",
  818. .desc = "MWAIT 0x40",
  819. .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
  820. .exit_latency = 1000,
  821. .target_residency = 1000,
  822. .enter = &intel_idle,
  823. .enter_s2idle = intel_idle_s2idle, },
  824. {
  825. .name = "C9",
  826. .desc = "MWAIT 0x50",
  827. .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
  828. .exit_latency = 2000,
  829. .target_residency = 2000,
  830. .enter = &intel_idle,
  831. .enter_s2idle = intel_idle_s2idle, },
  832. {
  833. .name = "C10",
  834. .desc = "MWAIT 0x60",
  835. .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
  836. .exit_latency = 10000,
  837. .target_residency = 10000,
  838. .enter = &intel_idle,
  839. .enter_s2idle = intel_idle_s2idle, },
  840. {
  841. .enter = NULL }
  842. };
  843. static struct cpuidle_state dnv_cstates[] = {
  844. {
  845. .name = "C1",
  846. .desc = "MWAIT 0x00",
  847. .flags = MWAIT2flg(0x00),
  848. .exit_latency = 2,
  849. .target_residency = 2,
  850. .enter = &intel_idle,
  851. .enter_s2idle = intel_idle_s2idle, },
  852. {
  853. .name = "C1E",
  854. .desc = "MWAIT 0x01",
  855. .flags = MWAIT2flg(0x01),
  856. .exit_latency = 10,
  857. .target_residency = 20,
  858. .enter = &intel_idle,
  859. .enter_s2idle = intel_idle_s2idle, },
  860. {
  861. .name = "C6",
  862. .desc = "MWAIT 0x20",
  863. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
  864. .exit_latency = 50,
  865. .target_residency = 500,
  866. .enter = &intel_idle,
  867. .enter_s2idle = intel_idle_s2idle, },
  868. {
  869. .enter = NULL }
  870. };
  871. /**
  872. * intel_idle
  873. * @dev: cpuidle_device
  874. * @drv: cpuidle driver
  875. * @index: index of cpuidle state
  876. *
  877. * Must be called under local_irq_disable().
  878. */
  879. static __cpuidle int intel_idle(struct cpuidle_device *dev,
  880. struct cpuidle_driver *drv, int index)
  881. {
  882. unsigned long ecx = 1; /* break on interrupt flag */
  883. struct cpuidle_state *state = &drv->states[index];
  884. unsigned long eax = flg2MWAIT(state->flags);
  885. unsigned int cstate;
  886. bool uninitialized_var(tick);
  887. int cpu = smp_processor_id();
  888. /*
  889. * leave_mm() to avoid costly and often unnecessary wakeups
  890. * for flushing the user TLB's associated with the active mm.
  891. */
  892. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  893. leave_mm(cpu);
  894. if (!static_cpu_has(X86_FEATURE_ARAT)) {
  895. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) &
  896. MWAIT_CSTATE_MASK) + 1;
  897. tick = false;
  898. if (!(lapic_timer_reliable_states & (1 << (cstate)))) {
  899. tick = true;
  900. tick_broadcast_enter();
  901. }
  902. }
  903. mwait_idle_with_hints(eax, ecx);
  904. if (!static_cpu_has(X86_FEATURE_ARAT) && tick)
  905. tick_broadcast_exit();
  906. return index;
  907. }
  908. /**
  909. * intel_idle_s2idle - simplified "enter" callback routine for suspend-to-idle
  910. * @dev: cpuidle_device
  911. * @drv: cpuidle driver
  912. * @index: state index
  913. */
  914. static void intel_idle_s2idle(struct cpuidle_device *dev,
  915. struct cpuidle_driver *drv, int index)
  916. {
  917. unsigned long ecx = 1; /* break on interrupt flag */
  918. unsigned long eax = flg2MWAIT(drv->states[index].flags);
  919. mwait_idle_with_hints(eax, ecx);
  920. }
  921. static void __setup_broadcast_timer(bool on)
  922. {
  923. if (on)
  924. tick_broadcast_enable();
  925. else
  926. tick_broadcast_disable();
  927. }
  928. static void auto_demotion_disable(void)
  929. {
  930. unsigned long long msr_bits;
  931. rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
  932. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  933. wrmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
  934. }
  935. static void c1e_promotion_disable(void)
  936. {
  937. unsigned long long msr_bits;
  938. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  939. msr_bits &= ~0x2;
  940. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  941. }
  942. static const struct idle_cpu idle_cpu_nehalem = {
  943. .state_table = nehalem_cstates,
  944. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  945. .disable_promotion_to_c1e = true,
  946. };
  947. static const struct idle_cpu idle_cpu_atom = {
  948. .state_table = atom_cstates,
  949. };
  950. static const struct idle_cpu idle_cpu_tangier = {
  951. .state_table = tangier_cstates,
  952. };
  953. static const struct idle_cpu idle_cpu_lincroft = {
  954. .state_table = atom_cstates,
  955. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  956. };
  957. static const struct idle_cpu idle_cpu_snb = {
  958. .state_table = snb_cstates,
  959. .disable_promotion_to_c1e = true,
  960. };
  961. static const struct idle_cpu idle_cpu_byt = {
  962. .state_table = byt_cstates,
  963. .disable_promotion_to_c1e = true,
  964. .byt_auto_demotion_disable_flag = true,
  965. };
  966. static const struct idle_cpu idle_cpu_cht = {
  967. .state_table = cht_cstates,
  968. .disable_promotion_to_c1e = true,
  969. .byt_auto_demotion_disable_flag = true,
  970. };
  971. static const struct idle_cpu idle_cpu_ivb = {
  972. .state_table = ivb_cstates,
  973. .disable_promotion_to_c1e = true,
  974. };
  975. static const struct idle_cpu idle_cpu_ivt = {
  976. .state_table = ivt_cstates,
  977. .disable_promotion_to_c1e = true,
  978. };
  979. static const struct idle_cpu idle_cpu_hsw = {
  980. .state_table = hsw_cstates,
  981. .disable_promotion_to_c1e = true,
  982. };
  983. static const struct idle_cpu idle_cpu_bdw = {
  984. .state_table = bdw_cstates,
  985. .disable_promotion_to_c1e = true,
  986. };
  987. static const struct idle_cpu idle_cpu_skl = {
  988. .state_table = skl_cstates,
  989. .disable_promotion_to_c1e = true,
  990. };
  991. static const struct idle_cpu idle_cpu_skx = {
  992. .state_table = skx_cstates,
  993. .disable_promotion_to_c1e = true,
  994. };
  995. static const struct idle_cpu idle_cpu_avn = {
  996. .state_table = avn_cstates,
  997. .disable_promotion_to_c1e = true,
  998. };
  999. static const struct idle_cpu idle_cpu_knl = {
  1000. .state_table = knl_cstates,
  1001. };
  1002. static const struct idle_cpu idle_cpu_bxt = {
  1003. .state_table = bxt_cstates,
  1004. .disable_promotion_to_c1e = true,
  1005. };
  1006. static const struct idle_cpu idle_cpu_dnv = {
  1007. .state_table = dnv_cstates,
  1008. .disable_promotion_to_c1e = true,
  1009. };
  1010. #define ICPU(model, cpu) \
  1011. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&cpu }
  1012. static const struct x86_cpu_id intel_idle_ids[] __initconst = {
  1013. ICPU(INTEL_FAM6_NEHALEM_EP, idle_cpu_nehalem),
  1014. ICPU(INTEL_FAM6_NEHALEM, idle_cpu_nehalem),
  1015. ICPU(INTEL_FAM6_NEHALEM_G, idle_cpu_nehalem),
  1016. ICPU(INTEL_FAM6_WESTMERE, idle_cpu_nehalem),
  1017. ICPU(INTEL_FAM6_WESTMERE_EP, idle_cpu_nehalem),
  1018. ICPU(INTEL_FAM6_NEHALEM_EX, idle_cpu_nehalem),
  1019. ICPU(INTEL_FAM6_ATOM_BONNELL, idle_cpu_atom),
  1020. ICPU(INTEL_FAM6_ATOM_BONNELL_MID, idle_cpu_lincroft),
  1021. ICPU(INTEL_FAM6_WESTMERE_EX, idle_cpu_nehalem),
  1022. ICPU(INTEL_FAM6_SANDYBRIDGE, idle_cpu_snb),
  1023. ICPU(INTEL_FAM6_SANDYBRIDGE_X, idle_cpu_snb),
  1024. ICPU(INTEL_FAM6_ATOM_SALTWELL, idle_cpu_atom),
  1025. ICPU(INTEL_FAM6_ATOM_SILVERMONT, idle_cpu_byt),
  1026. ICPU(INTEL_FAM6_ATOM_SILVERMONT_MID, idle_cpu_tangier),
  1027. ICPU(INTEL_FAM6_ATOM_AIRMONT, idle_cpu_cht),
  1028. ICPU(INTEL_FAM6_IVYBRIDGE, idle_cpu_ivb),
  1029. ICPU(INTEL_FAM6_IVYBRIDGE_X, idle_cpu_ivt),
  1030. ICPU(INTEL_FAM6_HASWELL_CORE, idle_cpu_hsw),
  1031. ICPU(INTEL_FAM6_HASWELL_X, idle_cpu_hsw),
  1032. ICPU(INTEL_FAM6_HASWELL_ULT, idle_cpu_hsw),
  1033. ICPU(INTEL_FAM6_HASWELL_GT3E, idle_cpu_hsw),
  1034. ICPU(INTEL_FAM6_ATOM_SILVERMONT_X, idle_cpu_avn),
  1035. ICPU(INTEL_FAM6_BROADWELL_CORE, idle_cpu_bdw),
  1036. ICPU(INTEL_FAM6_BROADWELL_GT3E, idle_cpu_bdw),
  1037. ICPU(INTEL_FAM6_BROADWELL_X, idle_cpu_bdw),
  1038. ICPU(INTEL_FAM6_BROADWELL_XEON_D, idle_cpu_bdw),
  1039. ICPU(INTEL_FAM6_SKYLAKE_MOBILE, idle_cpu_skl),
  1040. ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, idle_cpu_skl),
  1041. ICPU(INTEL_FAM6_KABYLAKE_MOBILE, idle_cpu_skl),
  1042. ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, idle_cpu_skl),
  1043. ICPU(INTEL_FAM6_SKYLAKE_X, idle_cpu_skx),
  1044. ICPU(INTEL_FAM6_XEON_PHI_KNL, idle_cpu_knl),
  1045. ICPU(INTEL_FAM6_XEON_PHI_KNM, idle_cpu_knl),
  1046. ICPU(INTEL_FAM6_ATOM_GOLDMONT, idle_cpu_bxt),
  1047. ICPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, idle_cpu_bxt),
  1048. ICPU(INTEL_FAM6_ATOM_GOLDMONT_X, idle_cpu_dnv),
  1049. {}
  1050. };
  1051. /*
  1052. * intel_idle_probe()
  1053. */
  1054. static int __init intel_idle_probe(void)
  1055. {
  1056. unsigned int eax, ebx, ecx;
  1057. const struct x86_cpu_id *id;
  1058. if (max_cstate == 0) {
  1059. pr_debug("disabled\n");
  1060. return -EPERM;
  1061. }
  1062. id = x86_match_cpu(intel_idle_ids);
  1063. if (!id) {
  1064. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1065. boot_cpu_data.x86 == 6)
  1066. pr_debug("does not run on family %d model %d\n",
  1067. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1068. return -ENODEV;
  1069. }
  1070. if (!boot_cpu_has(X86_FEATURE_MWAIT)) {
  1071. pr_debug("Please enable MWAIT in BIOS SETUP\n");
  1072. return -ENODEV;
  1073. }
  1074. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  1075. return -ENODEV;
  1076. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  1077. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  1078. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  1079. !mwait_substates)
  1080. return -ENODEV;
  1081. pr_debug("MWAIT substates: 0x%x\n", mwait_substates);
  1082. icpu = (const struct idle_cpu *)id->driver_data;
  1083. cpuidle_state_table = icpu->state_table;
  1084. pr_debug("v" INTEL_IDLE_VERSION " model 0x%X\n",
  1085. boot_cpu_data.x86_model);
  1086. return 0;
  1087. }
  1088. /*
  1089. * intel_idle_cpuidle_devices_uninit()
  1090. * Unregisters the cpuidle devices.
  1091. */
  1092. static void intel_idle_cpuidle_devices_uninit(void)
  1093. {
  1094. int i;
  1095. struct cpuidle_device *dev;
  1096. for_each_online_cpu(i) {
  1097. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  1098. cpuidle_unregister_device(dev);
  1099. }
  1100. }
  1101. /*
  1102. * ivt_idle_state_table_update(void)
  1103. *
  1104. * Tune IVT multi-socket targets
  1105. * Assumption: num_sockets == (max_package_num + 1)
  1106. */
  1107. static void ivt_idle_state_table_update(void)
  1108. {
  1109. /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
  1110. int cpu, package_num, num_sockets = 1;
  1111. for_each_online_cpu(cpu) {
  1112. package_num = topology_physical_package_id(cpu);
  1113. if (package_num + 1 > num_sockets) {
  1114. num_sockets = package_num + 1;
  1115. if (num_sockets > 4) {
  1116. cpuidle_state_table = ivt_cstates_8s;
  1117. return;
  1118. }
  1119. }
  1120. }
  1121. if (num_sockets > 2)
  1122. cpuidle_state_table = ivt_cstates_4s;
  1123. /* else, 1 and 2 socket systems use default ivt_cstates */
  1124. }
  1125. /*
  1126. * Translate IRTL (Interrupt Response Time Limit) MSR to usec
  1127. */
  1128. static unsigned int irtl_ns_units[] = {
  1129. 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
  1130. static unsigned long long irtl_2_usec(unsigned long long irtl)
  1131. {
  1132. unsigned long long ns;
  1133. if (!irtl)
  1134. return 0;
  1135. ns = irtl_ns_units[(irtl >> 10) & 0x7];
  1136. return div64_u64((irtl & 0x3FF) * ns, 1000);
  1137. }
  1138. /*
  1139. * bxt_idle_state_table_update(void)
  1140. *
  1141. * On BXT, we trust the IRTL to show the definitive maximum latency
  1142. * We use the same value for target_residency.
  1143. */
  1144. static void bxt_idle_state_table_update(void)
  1145. {
  1146. unsigned long long msr;
  1147. unsigned int usec;
  1148. rdmsrl(MSR_PKGC6_IRTL, msr);
  1149. usec = irtl_2_usec(msr);
  1150. if (usec) {
  1151. bxt_cstates[2].exit_latency = usec;
  1152. bxt_cstates[2].target_residency = usec;
  1153. }
  1154. rdmsrl(MSR_PKGC7_IRTL, msr);
  1155. usec = irtl_2_usec(msr);
  1156. if (usec) {
  1157. bxt_cstates[3].exit_latency = usec;
  1158. bxt_cstates[3].target_residency = usec;
  1159. }
  1160. rdmsrl(MSR_PKGC8_IRTL, msr);
  1161. usec = irtl_2_usec(msr);
  1162. if (usec) {
  1163. bxt_cstates[4].exit_latency = usec;
  1164. bxt_cstates[4].target_residency = usec;
  1165. }
  1166. rdmsrl(MSR_PKGC9_IRTL, msr);
  1167. usec = irtl_2_usec(msr);
  1168. if (usec) {
  1169. bxt_cstates[5].exit_latency = usec;
  1170. bxt_cstates[5].target_residency = usec;
  1171. }
  1172. rdmsrl(MSR_PKGC10_IRTL, msr);
  1173. usec = irtl_2_usec(msr);
  1174. if (usec) {
  1175. bxt_cstates[6].exit_latency = usec;
  1176. bxt_cstates[6].target_residency = usec;
  1177. }
  1178. }
  1179. /*
  1180. * sklh_idle_state_table_update(void)
  1181. *
  1182. * On SKL-H (model 0x5e) disable C8 and C9 if:
  1183. * C10 is enabled and SGX disabled
  1184. */
  1185. static void sklh_idle_state_table_update(void)
  1186. {
  1187. unsigned long long msr;
  1188. unsigned int eax, ebx, ecx, edx;
  1189. /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
  1190. if (max_cstate <= 7)
  1191. return;
  1192. /* if PC10 not present in CPUID.MWAIT.EDX */
  1193. if ((mwait_substates & (0xF << 28)) == 0)
  1194. return;
  1195. rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr);
  1196. /* PC10 is not enabled in PKG C-state limit */
  1197. if ((msr & 0xF) != 8)
  1198. return;
  1199. ecx = 0;
  1200. cpuid(7, &eax, &ebx, &ecx, &edx);
  1201. /* if SGX is present */
  1202. if (ebx & (1 << 2)) {
  1203. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1204. /* if SGX is enabled */
  1205. if (msr & (1 << 18))
  1206. return;
  1207. }
  1208. skl_cstates[5].disabled = 1; /* C8-SKL */
  1209. skl_cstates[6].disabled = 1; /* C9-SKL */
  1210. }
  1211. /*
  1212. * intel_idle_state_table_update()
  1213. *
  1214. * Update the default state_table for this CPU-id
  1215. */
  1216. static void intel_idle_state_table_update(void)
  1217. {
  1218. switch (boot_cpu_data.x86_model) {
  1219. case INTEL_FAM6_IVYBRIDGE_X:
  1220. ivt_idle_state_table_update();
  1221. break;
  1222. case INTEL_FAM6_ATOM_GOLDMONT:
  1223. case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
  1224. bxt_idle_state_table_update();
  1225. break;
  1226. case INTEL_FAM6_SKYLAKE_DESKTOP:
  1227. sklh_idle_state_table_update();
  1228. break;
  1229. }
  1230. }
  1231. /*
  1232. * intel_idle_cpuidle_driver_init()
  1233. * allocate, initialize cpuidle_states
  1234. */
  1235. static void __init intel_idle_cpuidle_driver_init(void)
  1236. {
  1237. int cstate;
  1238. struct cpuidle_driver *drv = &intel_idle_driver;
  1239. intel_idle_state_table_update();
  1240. cpuidle_poll_state_init(drv);
  1241. drv->state_count = 1;
  1242. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  1243. int num_substates, mwait_hint, mwait_cstate;
  1244. if ((cpuidle_state_table[cstate].enter == NULL) &&
  1245. (cpuidle_state_table[cstate].enter_s2idle == NULL))
  1246. break;
  1247. if (cstate + 1 > max_cstate) {
  1248. pr_info("max_cstate %d reached\n", max_cstate);
  1249. break;
  1250. }
  1251. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  1252. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  1253. /* number of sub-states for this state in CPUID.MWAIT */
  1254. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  1255. & MWAIT_SUBSTATE_MASK;
  1256. /* if NO sub-states for this state in CPUID, skip it */
  1257. if (num_substates == 0)
  1258. continue;
  1259. /* if state marked as disabled, skip it */
  1260. if (cpuidle_state_table[cstate].disabled != 0) {
  1261. pr_debug("state %s is disabled\n",
  1262. cpuidle_state_table[cstate].name);
  1263. continue;
  1264. }
  1265. if (((mwait_cstate + 1) > 2) &&
  1266. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  1267. mark_tsc_unstable("TSC halts in idle"
  1268. " states deeper than C2");
  1269. drv->states[drv->state_count] = /* structure copy */
  1270. cpuidle_state_table[cstate];
  1271. drv->state_count += 1;
  1272. }
  1273. if (icpu->byt_auto_demotion_disable_flag) {
  1274. wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
  1275. wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
  1276. }
  1277. }
  1278. /*
  1279. * intel_idle_cpu_init()
  1280. * allocate, initialize, register cpuidle_devices
  1281. * @cpu: cpu/core to initialize
  1282. */
  1283. static int intel_idle_cpu_init(unsigned int cpu)
  1284. {
  1285. struct cpuidle_device *dev;
  1286. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1287. dev->cpu = cpu;
  1288. if (cpuidle_register_device(dev)) {
  1289. pr_debug("cpuidle_register_device %d failed!\n", cpu);
  1290. return -EIO;
  1291. }
  1292. if (icpu->auto_demotion_disable_flags)
  1293. auto_demotion_disable();
  1294. if (icpu->disable_promotion_to_c1e)
  1295. c1e_promotion_disable();
  1296. return 0;
  1297. }
  1298. static int intel_idle_cpu_online(unsigned int cpu)
  1299. {
  1300. struct cpuidle_device *dev;
  1301. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  1302. __setup_broadcast_timer(true);
  1303. /*
  1304. * Some systems can hotplug a cpu at runtime after
  1305. * the kernel has booted, we have to initialize the
  1306. * driver in this case
  1307. */
  1308. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  1309. if (!dev->registered)
  1310. return intel_idle_cpu_init(cpu);
  1311. return 0;
  1312. }
  1313. static int __init intel_idle_init(void)
  1314. {
  1315. int retval;
  1316. /* Do not load intel_idle at all for now if idle= is passed */
  1317. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  1318. return -ENODEV;
  1319. retval = intel_idle_probe();
  1320. if (retval)
  1321. return retval;
  1322. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  1323. if (intel_idle_cpuidle_devices == NULL)
  1324. return -ENOMEM;
  1325. intel_idle_cpuidle_driver_init();
  1326. retval = cpuidle_register_driver(&intel_idle_driver);
  1327. if (retval) {
  1328. struct cpuidle_driver *drv = cpuidle_get_driver();
  1329. printk(KERN_DEBUG pr_fmt("intel_idle yielding to %s\n"),
  1330. drv ? drv->name : "none");
  1331. goto init_driver_fail;
  1332. }
  1333. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  1334. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  1335. retval = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "idle/intel:online",
  1336. intel_idle_cpu_online, NULL);
  1337. if (retval < 0)
  1338. goto hp_setup_fail;
  1339. pr_debug("lapic_timer_reliable_states 0x%x\n",
  1340. lapic_timer_reliable_states);
  1341. return 0;
  1342. hp_setup_fail:
  1343. intel_idle_cpuidle_devices_uninit();
  1344. cpuidle_unregister_driver(&intel_idle_driver);
  1345. init_driver_fail:
  1346. free_percpu(intel_idle_cpuidle_devices);
  1347. return retval;
  1348. }
  1349. device_initcall(intel_idle_init);
  1350. /*
  1351. * We are not really modular, but we used to support that. Meaning we also
  1352. * support "intel_idle.max_cstate=..." at boot and also a read-only export of
  1353. * it at /sys/module/intel_idle/parameters/max_cstate -- so using module_param
  1354. * is the easiest way (currently) to continue doing that.
  1355. */
  1356. module_param(max_cstate, int, 0444);