intel_rapl.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. /*
  2. * Intel Running Average Power Limit (RAPL) Driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/list.h>
  22. #include <linux/types.h>
  23. #include <linux/device.h>
  24. #include <linux/slab.h>
  25. #include <linux/log2.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/delay.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/cpu.h>
  30. #include <linux/powercap.h>
  31. #include <asm/iosf_mbi.h>
  32. #include <asm/processor.h>
  33. #include <asm/cpu_device_id.h>
  34. #include <asm/intel-family.h>
  35. /* Local defines */
  36. #define MSR_PLATFORM_POWER_LIMIT 0x0000065C
  37. /* bitmasks for RAPL MSRs, used by primitive access functions */
  38. #define ENERGY_STATUS_MASK 0xffffffff
  39. #define POWER_LIMIT1_MASK 0x7FFF
  40. #define POWER_LIMIT1_ENABLE BIT(15)
  41. #define POWER_LIMIT1_CLAMP BIT(16)
  42. #define POWER_LIMIT2_MASK (0x7FFFULL<<32)
  43. #define POWER_LIMIT2_ENABLE BIT_ULL(47)
  44. #define POWER_LIMIT2_CLAMP BIT_ULL(48)
  45. #define POWER_PACKAGE_LOCK BIT_ULL(63)
  46. #define POWER_PP_LOCK BIT(31)
  47. #define TIME_WINDOW1_MASK (0x7FULL<<17)
  48. #define TIME_WINDOW2_MASK (0x7FULL<<49)
  49. #define POWER_UNIT_OFFSET 0
  50. #define POWER_UNIT_MASK 0x0F
  51. #define ENERGY_UNIT_OFFSET 0x08
  52. #define ENERGY_UNIT_MASK 0x1F00
  53. #define TIME_UNIT_OFFSET 0x10
  54. #define TIME_UNIT_MASK 0xF0000
  55. #define POWER_INFO_MAX_MASK (0x7fffULL<<32)
  56. #define POWER_INFO_MIN_MASK (0x7fffULL<<16)
  57. #define POWER_INFO_MAX_TIME_WIN_MASK (0x3fULL<<48)
  58. #define POWER_INFO_THERMAL_SPEC_MASK 0x7fff
  59. #define PERF_STATUS_THROTTLE_TIME_MASK 0xffffffff
  60. #define PP_POLICY_MASK 0x1F
  61. /* Non HW constants */
  62. #define RAPL_PRIMITIVE_DERIVED BIT(1) /* not from raw data */
  63. #define RAPL_PRIMITIVE_DUMMY BIT(2)
  64. #define TIME_WINDOW_MAX_MSEC 40000
  65. #define TIME_WINDOW_MIN_MSEC 250
  66. #define ENERGY_UNIT_SCALE 1000 /* scale from driver unit to powercap unit */
  67. enum unit_type {
  68. ARBITRARY_UNIT, /* no translation */
  69. POWER_UNIT,
  70. ENERGY_UNIT,
  71. TIME_UNIT,
  72. };
  73. enum rapl_domain_type {
  74. RAPL_DOMAIN_PACKAGE, /* entire package/socket */
  75. RAPL_DOMAIN_PP0, /* core power plane */
  76. RAPL_DOMAIN_PP1, /* graphics uncore */
  77. RAPL_DOMAIN_DRAM,/* DRAM control_type */
  78. RAPL_DOMAIN_PLATFORM, /* PSys control_type */
  79. RAPL_DOMAIN_MAX,
  80. };
  81. enum rapl_domain_msr_id {
  82. RAPL_DOMAIN_MSR_LIMIT,
  83. RAPL_DOMAIN_MSR_STATUS,
  84. RAPL_DOMAIN_MSR_PERF,
  85. RAPL_DOMAIN_MSR_POLICY,
  86. RAPL_DOMAIN_MSR_INFO,
  87. RAPL_DOMAIN_MSR_MAX,
  88. };
  89. /* per domain data, some are optional */
  90. enum rapl_primitives {
  91. ENERGY_COUNTER,
  92. POWER_LIMIT1,
  93. POWER_LIMIT2,
  94. FW_LOCK,
  95. PL1_ENABLE, /* power limit 1, aka long term */
  96. PL1_CLAMP, /* allow frequency to go below OS request */
  97. PL2_ENABLE, /* power limit 2, aka short term, instantaneous */
  98. PL2_CLAMP,
  99. TIME_WINDOW1, /* long term */
  100. TIME_WINDOW2, /* short term */
  101. THERMAL_SPEC_POWER,
  102. MAX_POWER,
  103. MIN_POWER,
  104. MAX_TIME_WINDOW,
  105. THROTTLED_TIME,
  106. PRIORITY_LEVEL,
  107. /* below are not raw primitive data */
  108. AVERAGE_POWER,
  109. NR_RAPL_PRIMITIVES,
  110. };
  111. #define NR_RAW_PRIMITIVES (NR_RAPL_PRIMITIVES - 2)
  112. /* Can be expanded to include events, etc.*/
  113. struct rapl_domain_data {
  114. u64 primitives[NR_RAPL_PRIMITIVES];
  115. unsigned long timestamp;
  116. };
  117. struct msrl_action {
  118. u32 msr_no;
  119. u64 clear_mask;
  120. u64 set_mask;
  121. int err;
  122. };
  123. #define DOMAIN_STATE_INACTIVE BIT(0)
  124. #define DOMAIN_STATE_POWER_LIMIT_SET BIT(1)
  125. #define DOMAIN_STATE_BIOS_LOCKED BIT(2)
  126. #define NR_POWER_LIMITS (2)
  127. struct rapl_power_limit {
  128. struct powercap_zone_constraint *constraint;
  129. int prim_id; /* primitive ID used to enable */
  130. struct rapl_domain *domain;
  131. const char *name;
  132. };
  133. static const char pl1_name[] = "long_term";
  134. static const char pl2_name[] = "short_term";
  135. struct rapl_package;
  136. struct rapl_domain {
  137. const char *name;
  138. enum rapl_domain_type id;
  139. int msrs[RAPL_DOMAIN_MSR_MAX];
  140. struct powercap_zone power_zone;
  141. struct rapl_domain_data rdd;
  142. struct rapl_power_limit rpl[NR_POWER_LIMITS];
  143. u64 attr_map; /* track capabilities */
  144. unsigned int state;
  145. unsigned int domain_energy_unit;
  146. struct rapl_package *rp;
  147. };
  148. #define power_zone_to_rapl_domain(_zone) \
  149. container_of(_zone, struct rapl_domain, power_zone)
  150. /* Each physical package contains multiple domains, these are the common
  151. * data across RAPL domains within a package.
  152. */
  153. struct rapl_package {
  154. unsigned int id; /* physical package/socket id */
  155. unsigned int nr_domains;
  156. unsigned long domain_map; /* bit map of active domains */
  157. unsigned int power_unit;
  158. unsigned int energy_unit;
  159. unsigned int time_unit;
  160. struct rapl_domain *domains; /* array of domains, sized at runtime */
  161. struct powercap_zone *power_zone; /* keep track of parent zone */
  162. int nr_cpus; /* active cpus on the package, topology info is lost during
  163. * cpu hotplug. so we have to track ourselves.
  164. */
  165. unsigned long power_limit_irq; /* keep track of package power limit
  166. * notify interrupt enable status.
  167. */
  168. struct list_head plist;
  169. int lead_cpu; /* one active cpu per package for access */
  170. };
  171. struct rapl_defaults {
  172. u8 floor_freq_reg_addr;
  173. int (*check_unit)(struct rapl_package *rp, int cpu);
  174. void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
  175. u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
  176. bool to_raw);
  177. unsigned int dram_domain_energy_unit;
  178. };
  179. static struct rapl_defaults *rapl_defaults;
  180. /* Sideband MBI registers */
  181. #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
  182. #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
  183. #define PACKAGE_PLN_INT_SAVED BIT(0)
  184. #define MAX_PRIM_NAME (32)
  185. /* per domain data. used to describe individual knobs such that access function
  186. * can be consolidated into one instead of many inline functions.
  187. */
  188. struct rapl_primitive_info {
  189. const char *name;
  190. u64 mask;
  191. int shift;
  192. enum rapl_domain_msr_id id;
  193. enum unit_type unit;
  194. u32 flag;
  195. };
  196. #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
  197. .name = #p, \
  198. .mask = m, \
  199. .shift = s, \
  200. .id = i, \
  201. .unit = u, \
  202. .flag = f \
  203. }
  204. static void rapl_init_domains(struct rapl_package *rp);
  205. static int rapl_read_data_raw(struct rapl_domain *rd,
  206. enum rapl_primitives prim,
  207. bool xlate, u64 *data);
  208. static int rapl_write_data_raw(struct rapl_domain *rd,
  209. enum rapl_primitives prim,
  210. unsigned long long value);
  211. static u64 rapl_unit_xlate(struct rapl_domain *rd,
  212. enum unit_type type, u64 value,
  213. int to_raw);
  214. static void package_power_limit_irq_save(struct rapl_package *rp);
  215. static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
  216. static const char * const rapl_domain_names[] = {
  217. "package",
  218. "core",
  219. "uncore",
  220. "dram",
  221. "psys",
  222. };
  223. static struct powercap_control_type *control_type; /* PowerCap Controller */
  224. static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
  225. /* caller to ensure CPU hotplug lock is held */
  226. static struct rapl_package *find_package_by_id(int id)
  227. {
  228. struct rapl_package *rp;
  229. list_for_each_entry(rp, &rapl_packages, plist) {
  230. if (rp->id == id)
  231. return rp;
  232. }
  233. return NULL;
  234. }
  235. /* caller must hold cpu hotplug lock */
  236. static void rapl_cleanup_data(void)
  237. {
  238. struct rapl_package *p, *tmp;
  239. list_for_each_entry_safe(p, tmp, &rapl_packages, plist) {
  240. kfree(p->domains);
  241. list_del(&p->plist);
  242. kfree(p);
  243. }
  244. }
  245. static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
  246. {
  247. struct rapl_domain *rd;
  248. u64 energy_now;
  249. /* prevent CPU hotplug, make sure the RAPL domain does not go
  250. * away while reading the counter.
  251. */
  252. get_online_cpus();
  253. rd = power_zone_to_rapl_domain(power_zone);
  254. if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
  255. *energy_raw = energy_now;
  256. put_online_cpus();
  257. return 0;
  258. }
  259. put_online_cpus();
  260. return -EIO;
  261. }
  262. static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
  263. {
  264. struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
  265. *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
  266. return 0;
  267. }
  268. static int release_zone(struct powercap_zone *power_zone)
  269. {
  270. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  271. struct rapl_package *rp = rd->rp;
  272. /* package zone is the last zone of a package, we can free
  273. * memory here since all children has been unregistered.
  274. */
  275. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  276. kfree(rd);
  277. rp->domains = NULL;
  278. }
  279. return 0;
  280. }
  281. static int find_nr_power_limit(struct rapl_domain *rd)
  282. {
  283. int i, nr_pl = 0;
  284. for (i = 0; i < NR_POWER_LIMITS; i++) {
  285. if (rd->rpl[i].name)
  286. nr_pl++;
  287. }
  288. return nr_pl;
  289. }
  290. static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
  291. {
  292. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  293. if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
  294. return -EACCES;
  295. get_online_cpus();
  296. rapl_write_data_raw(rd, PL1_ENABLE, mode);
  297. if (rapl_defaults->set_floor_freq)
  298. rapl_defaults->set_floor_freq(rd, mode);
  299. put_online_cpus();
  300. return 0;
  301. }
  302. static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
  303. {
  304. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  305. u64 val;
  306. if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
  307. *mode = false;
  308. return 0;
  309. }
  310. get_online_cpus();
  311. if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
  312. put_online_cpus();
  313. return -EIO;
  314. }
  315. *mode = val;
  316. put_online_cpus();
  317. return 0;
  318. }
  319. /* per RAPL domain ops, in the order of rapl_domain_type */
  320. static const struct powercap_zone_ops zone_ops[] = {
  321. /* RAPL_DOMAIN_PACKAGE */
  322. {
  323. .get_energy_uj = get_energy_counter,
  324. .get_max_energy_range_uj = get_max_energy_counter,
  325. .release = release_zone,
  326. .set_enable = set_domain_enable,
  327. .get_enable = get_domain_enable,
  328. },
  329. /* RAPL_DOMAIN_PP0 */
  330. {
  331. .get_energy_uj = get_energy_counter,
  332. .get_max_energy_range_uj = get_max_energy_counter,
  333. .release = release_zone,
  334. .set_enable = set_domain_enable,
  335. .get_enable = get_domain_enable,
  336. },
  337. /* RAPL_DOMAIN_PP1 */
  338. {
  339. .get_energy_uj = get_energy_counter,
  340. .get_max_energy_range_uj = get_max_energy_counter,
  341. .release = release_zone,
  342. .set_enable = set_domain_enable,
  343. .get_enable = get_domain_enable,
  344. },
  345. /* RAPL_DOMAIN_DRAM */
  346. {
  347. .get_energy_uj = get_energy_counter,
  348. .get_max_energy_range_uj = get_max_energy_counter,
  349. .release = release_zone,
  350. .set_enable = set_domain_enable,
  351. .get_enable = get_domain_enable,
  352. },
  353. /* RAPL_DOMAIN_PLATFORM */
  354. {
  355. .get_energy_uj = get_energy_counter,
  356. .get_max_energy_range_uj = get_max_energy_counter,
  357. .release = release_zone,
  358. .set_enable = set_domain_enable,
  359. .get_enable = get_domain_enable,
  360. },
  361. };
  362. /*
  363. * Constraint index used by powercap can be different than power limit (PL)
  364. * index in that some PLs maybe missing due to non-existant MSRs. So we
  365. * need to convert here by finding the valid PLs only (name populated).
  366. */
  367. static int contraint_to_pl(struct rapl_domain *rd, int cid)
  368. {
  369. int i, j;
  370. for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
  371. if ((rd->rpl[i].name) && j++ == cid) {
  372. pr_debug("%s: index %d\n", __func__, i);
  373. return i;
  374. }
  375. }
  376. pr_err("Cannot find matching power limit for constraint %d\n", cid);
  377. return -EINVAL;
  378. }
  379. static int set_power_limit(struct powercap_zone *power_zone, int cid,
  380. u64 power_limit)
  381. {
  382. struct rapl_domain *rd;
  383. struct rapl_package *rp;
  384. int ret = 0;
  385. int id;
  386. get_online_cpus();
  387. rd = power_zone_to_rapl_domain(power_zone);
  388. id = contraint_to_pl(rd, cid);
  389. if (id < 0) {
  390. ret = id;
  391. goto set_exit;
  392. }
  393. rp = rd->rp;
  394. if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
  395. dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
  396. rd->name);
  397. ret = -EACCES;
  398. goto set_exit;
  399. }
  400. switch (rd->rpl[id].prim_id) {
  401. case PL1_ENABLE:
  402. rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
  403. break;
  404. case PL2_ENABLE:
  405. rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
  406. break;
  407. default:
  408. ret = -EINVAL;
  409. }
  410. if (!ret)
  411. package_power_limit_irq_save(rp);
  412. set_exit:
  413. put_online_cpus();
  414. return ret;
  415. }
  416. static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
  417. u64 *data)
  418. {
  419. struct rapl_domain *rd;
  420. u64 val;
  421. int prim;
  422. int ret = 0;
  423. int id;
  424. get_online_cpus();
  425. rd = power_zone_to_rapl_domain(power_zone);
  426. id = contraint_to_pl(rd, cid);
  427. if (id < 0) {
  428. ret = id;
  429. goto get_exit;
  430. }
  431. switch (rd->rpl[id].prim_id) {
  432. case PL1_ENABLE:
  433. prim = POWER_LIMIT1;
  434. break;
  435. case PL2_ENABLE:
  436. prim = POWER_LIMIT2;
  437. break;
  438. default:
  439. put_online_cpus();
  440. return -EINVAL;
  441. }
  442. if (rapl_read_data_raw(rd, prim, true, &val))
  443. ret = -EIO;
  444. else
  445. *data = val;
  446. get_exit:
  447. put_online_cpus();
  448. return ret;
  449. }
  450. static int set_time_window(struct powercap_zone *power_zone, int cid,
  451. u64 window)
  452. {
  453. struct rapl_domain *rd;
  454. int ret = 0;
  455. int id;
  456. get_online_cpus();
  457. rd = power_zone_to_rapl_domain(power_zone);
  458. id = contraint_to_pl(rd, cid);
  459. if (id < 0) {
  460. ret = id;
  461. goto set_time_exit;
  462. }
  463. switch (rd->rpl[id].prim_id) {
  464. case PL1_ENABLE:
  465. rapl_write_data_raw(rd, TIME_WINDOW1, window);
  466. break;
  467. case PL2_ENABLE:
  468. rapl_write_data_raw(rd, TIME_WINDOW2, window);
  469. break;
  470. default:
  471. ret = -EINVAL;
  472. }
  473. set_time_exit:
  474. put_online_cpus();
  475. return ret;
  476. }
  477. static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
  478. {
  479. struct rapl_domain *rd;
  480. u64 val;
  481. int ret = 0;
  482. int id;
  483. get_online_cpus();
  484. rd = power_zone_to_rapl_domain(power_zone);
  485. id = contraint_to_pl(rd, cid);
  486. if (id < 0) {
  487. ret = id;
  488. goto get_time_exit;
  489. }
  490. switch (rd->rpl[id].prim_id) {
  491. case PL1_ENABLE:
  492. ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
  493. break;
  494. case PL2_ENABLE:
  495. ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
  496. break;
  497. default:
  498. put_online_cpus();
  499. return -EINVAL;
  500. }
  501. if (!ret)
  502. *data = val;
  503. get_time_exit:
  504. put_online_cpus();
  505. return ret;
  506. }
  507. static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
  508. {
  509. struct rapl_domain *rd;
  510. int id;
  511. rd = power_zone_to_rapl_domain(power_zone);
  512. id = contraint_to_pl(rd, cid);
  513. if (id >= 0)
  514. return rd->rpl[id].name;
  515. return NULL;
  516. }
  517. static int get_max_power(struct powercap_zone *power_zone, int id,
  518. u64 *data)
  519. {
  520. struct rapl_domain *rd;
  521. u64 val;
  522. int prim;
  523. int ret = 0;
  524. get_online_cpus();
  525. rd = power_zone_to_rapl_domain(power_zone);
  526. switch (rd->rpl[id].prim_id) {
  527. case PL1_ENABLE:
  528. prim = THERMAL_SPEC_POWER;
  529. break;
  530. case PL2_ENABLE:
  531. prim = MAX_POWER;
  532. break;
  533. default:
  534. put_online_cpus();
  535. return -EINVAL;
  536. }
  537. if (rapl_read_data_raw(rd, prim, true, &val))
  538. ret = -EIO;
  539. else
  540. *data = val;
  541. put_online_cpus();
  542. return ret;
  543. }
  544. static const struct powercap_zone_constraint_ops constraint_ops = {
  545. .set_power_limit_uw = set_power_limit,
  546. .get_power_limit_uw = get_current_power_limit,
  547. .set_time_window_us = set_time_window,
  548. .get_time_window_us = get_time_window,
  549. .get_max_power_uw = get_max_power,
  550. .get_name = get_constraint_name,
  551. };
  552. /* called after domain detection and package level data are set */
  553. static void rapl_init_domains(struct rapl_package *rp)
  554. {
  555. int i;
  556. struct rapl_domain *rd = rp->domains;
  557. for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
  558. unsigned int mask = rp->domain_map & (1 << i);
  559. switch (mask) {
  560. case BIT(RAPL_DOMAIN_PACKAGE):
  561. rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
  562. rd->id = RAPL_DOMAIN_PACKAGE;
  563. rd->msrs[0] = MSR_PKG_POWER_LIMIT;
  564. rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
  565. rd->msrs[2] = MSR_PKG_PERF_STATUS;
  566. rd->msrs[3] = 0;
  567. rd->msrs[4] = MSR_PKG_POWER_INFO;
  568. rd->rpl[0].prim_id = PL1_ENABLE;
  569. rd->rpl[0].name = pl1_name;
  570. rd->rpl[1].prim_id = PL2_ENABLE;
  571. rd->rpl[1].name = pl2_name;
  572. break;
  573. case BIT(RAPL_DOMAIN_PP0):
  574. rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
  575. rd->id = RAPL_DOMAIN_PP0;
  576. rd->msrs[0] = MSR_PP0_POWER_LIMIT;
  577. rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
  578. rd->msrs[2] = 0;
  579. rd->msrs[3] = MSR_PP0_POLICY;
  580. rd->msrs[4] = 0;
  581. rd->rpl[0].prim_id = PL1_ENABLE;
  582. rd->rpl[0].name = pl1_name;
  583. break;
  584. case BIT(RAPL_DOMAIN_PP1):
  585. rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
  586. rd->id = RAPL_DOMAIN_PP1;
  587. rd->msrs[0] = MSR_PP1_POWER_LIMIT;
  588. rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
  589. rd->msrs[2] = 0;
  590. rd->msrs[3] = MSR_PP1_POLICY;
  591. rd->msrs[4] = 0;
  592. rd->rpl[0].prim_id = PL1_ENABLE;
  593. rd->rpl[0].name = pl1_name;
  594. break;
  595. case BIT(RAPL_DOMAIN_DRAM):
  596. rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
  597. rd->id = RAPL_DOMAIN_DRAM;
  598. rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
  599. rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
  600. rd->msrs[2] = MSR_DRAM_PERF_STATUS;
  601. rd->msrs[3] = 0;
  602. rd->msrs[4] = MSR_DRAM_POWER_INFO;
  603. rd->rpl[0].prim_id = PL1_ENABLE;
  604. rd->rpl[0].name = pl1_name;
  605. rd->domain_energy_unit =
  606. rapl_defaults->dram_domain_energy_unit;
  607. if (rd->domain_energy_unit)
  608. pr_info("DRAM domain energy unit %dpj\n",
  609. rd->domain_energy_unit);
  610. break;
  611. }
  612. if (mask) {
  613. rd->rp = rp;
  614. rd++;
  615. }
  616. }
  617. }
  618. static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
  619. u64 value, int to_raw)
  620. {
  621. u64 units = 1;
  622. struct rapl_package *rp = rd->rp;
  623. u64 scale = 1;
  624. switch (type) {
  625. case POWER_UNIT:
  626. units = rp->power_unit;
  627. break;
  628. case ENERGY_UNIT:
  629. scale = ENERGY_UNIT_SCALE;
  630. /* per domain unit takes precedence */
  631. if (rd->domain_energy_unit)
  632. units = rd->domain_energy_unit;
  633. else
  634. units = rp->energy_unit;
  635. break;
  636. case TIME_UNIT:
  637. return rapl_defaults->compute_time_window(rp, value, to_raw);
  638. case ARBITRARY_UNIT:
  639. default:
  640. return value;
  641. };
  642. if (to_raw)
  643. return div64_u64(value, units) * scale;
  644. value *= units;
  645. return div64_u64(value, scale);
  646. }
  647. /* in the order of enum rapl_primitives */
  648. static struct rapl_primitive_info rpi[] = {
  649. /* name, mask, shift, msr index, unit divisor */
  650. PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
  651. RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
  652. PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
  653. RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
  654. PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
  655. RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
  656. PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
  657. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  658. PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
  659. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  660. PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
  661. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  662. PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
  663. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  664. PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
  665. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  666. PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
  667. RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
  668. PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
  669. RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
  670. PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
  671. 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  672. PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
  673. RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  674. PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
  675. RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  676. PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
  677. RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
  678. PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
  679. RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
  680. PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
  681. RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
  682. /* non-hardware */
  683. PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
  684. RAPL_PRIMITIVE_DERIVED),
  685. {NULL, 0, 0, 0},
  686. };
  687. /* Read primitive data based on its related struct rapl_primitive_info.
  688. * if xlate flag is set, return translated data based on data units, i.e.
  689. * time, energy, and power.
  690. * RAPL MSRs are non-architectual and are laid out not consistently across
  691. * domains. Here we use primitive info to allow writing consolidated access
  692. * functions.
  693. * For a given primitive, it is processed by MSR mask and shift. Unit conversion
  694. * is pre-assigned based on RAPL unit MSRs read at init time.
  695. * 63-------------------------- 31--------------------------- 0
  696. * | xxxxx (mask) |
  697. * | |<- shift ----------------|
  698. * 63-------------------------- 31--------------------------- 0
  699. */
  700. static int rapl_read_data_raw(struct rapl_domain *rd,
  701. enum rapl_primitives prim,
  702. bool xlate, u64 *data)
  703. {
  704. u64 value, final;
  705. u32 msr;
  706. struct rapl_primitive_info *rp = &rpi[prim];
  707. int cpu;
  708. if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
  709. return -EINVAL;
  710. msr = rd->msrs[rp->id];
  711. if (!msr)
  712. return -EINVAL;
  713. cpu = rd->rp->lead_cpu;
  714. /* special-case package domain, which uses a different bit*/
  715. if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
  716. rp->mask = POWER_PACKAGE_LOCK;
  717. rp->shift = 63;
  718. }
  719. /* non-hardware data are collected by the polling thread */
  720. if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
  721. *data = rd->rdd.primitives[prim];
  722. return 0;
  723. }
  724. if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
  725. pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
  726. return -EIO;
  727. }
  728. final = value & rp->mask;
  729. final = final >> rp->shift;
  730. if (xlate)
  731. *data = rapl_unit_xlate(rd, rp->unit, final, 0);
  732. else
  733. *data = final;
  734. return 0;
  735. }
  736. static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
  737. {
  738. int err;
  739. u64 val;
  740. err = rdmsrl_safe(msr_no, &val);
  741. if (err)
  742. goto out;
  743. val &= ~clear_mask;
  744. val |= set_mask;
  745. err = wrmsrl_safe(msr_no, val);
  746. out:
  747. return err;
  748. }
  749. static void msrl_update_func(void *info)
  750. {
  751. struct msrl_action *ma = info;
  752. ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
  753. }
  754. /* Similar use of primitive info in the read counterpart */
  755. static int rapl_write_data_raw(struct rapl_domain *rd,
  756. enum rapl_primitives prim,
  757. unsigned long long value)
  758. {
  759. struct rapl_primitive_info *rp = &rpi[prim];
  760. int cpu;
  761. u64 bits;
  762. struct msrl_action ma;
  763. int ret;
  764. cpu = rd->rp->lead_cpu;
  765. bits = rapl_unit_xlate(rd, rp->unit, value, 1);
  766. bits |= bits << rp->shift;
  767. memset(&ma, 0, sizeof(ma));
  768. ma.msr_no = rd->msrs[rp->id];
  769. ma.clear_mask = rp->mask;
  770. ma.set_mask = bits;
  771. ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
  772. if (ret)
  773. WARN_ON_ONCE(ret);
  774. else
  775. ret = ma.err;
  776. return ret;
  777. }
  778. /*
  779. * Raw RAPL data stored in MSRs are in certain scales. We need to
  780. * convert them into standard units based on the units reported in
  781. * the RAPL unit MSRs. This is specific to CPUs as the method to
  782. * calculate units differ on different CPUs.
  783. * We convert the units to below format based on CPUs.
  784. * i.e.
  785. * energy unit: picoJoules : Represented in picoJoules by default
  786. * power unit : microWatts : Represented in milliWatts by default
  787. * time unit : microseconds: Represented in seconds by default
  788. */
  789. static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
  790. {
  791. u64 msr_val;
  792. u32 value;
  793. if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
  794. pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
  795. MSR_RAPL_POWER_UNIT, cpu);
  796. return -ENODEV;
  797. }
  798. value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
  799. rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
  800. value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
  801. rp->power_unit = 1000000 / (1 << value);
  802. value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
  803. rp->time_unit = 1000000 / (1 << value);
  804. pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
  805. rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
  806. return 0;
  807. }
  808. static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
  809. {
  810. u64 msr_val;
  811. u32 value;
  812. if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
  813. pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
  814. MSR_RAPL_POWER_UNIT, cpu);
  815. return -ENODEV;
  816. }
  817. value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
  818. rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
  819. value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
  820. rp->power_unit = (1 << value) * 1000;
  821. value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
  822. rp->time_unit = 1000000 / (1 << value);
  823. pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
  824. rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
  825. return 0;
  826. }
  827. static void power_limit_irq_save_cpu(void *info)
  828. {
  829. u32 l, h = 0;
  830. struct rapl_package *rp = (struct rapl_package *)info;
  831. /* save the state of PLN irq mask bit before disabling it */
  832. rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
  833. if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
  834. rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
  835. rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
  836. }
  837. l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
  838. wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  839. }
  840. /* REVISIT:
  841. * When package power limit is set artificially low by RAPL, LVT
  842. * thermal interrupt for package power limit should be ignored
  843. * since we are not really exceeding the real limit. The intention
  844. * is to avoid excessive interrupts while we are trying to save power.
  845. * A useful feature might be routing the package_power_limit interrupt
  846. * to userspace via eventfd. once we have a usecase, this is simple
  847. * to do by adding an atomic notifier.
  848. */
  849. static void package_power_limit_irq_save(struct rapl_package *rp)
  850. {
  851. if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
  852. return;
  853. smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
  854. }
  855. static void power_limit_irq_restore_cpu(void *info)
  856. {
  857. u32 l, h = 0;
  858. struct rapl_package *rp = (struct rapl_package *)info;
  859. rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
  860. if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
  861. l |= PACKAGE_THERM_INT_PLN_ENABLE;
  862. else
  863. l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
  864. wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  865. }
  866. /* restore per package power limit interrupt enable state */
  867. static void package_power_limit_irq_restore(struct rapl_package *rp)
  868. {
  869. if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
  870. return;
  871. /* irq enable state not saved, nothing to restore */
  872. if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
  873. return;
  874. smp_call_function_single(rp->lead_cpu, power_limit_irq_restore_cpu, rp, 1);
  875. }
  876. static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
  877. {
  878. int nr_powerlimit = find_nr_power_limit(rd);
  879. /* always enable clamp such that p-state can go below OS requested
  880. * range. power capping priority over guranteed frequency.
  881. */
  882. rapl_write_data_raw(rd, PL1_CLAMP, mode);
  883. /* some domains have pl2 */
  884. if (nr_powerlimit > 1) {
  885. rapl_write_data_raw(rd, PL2_ENABLE, mode);
  886. rapl_write_data_raw(rd, PL2_CLAMP, mode);
  887. }
  888. }
  889. static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
  890. {
  891. static u32 power_ctrl_orig_val;
  892. u32 mdata;
  893. if (!rapl_defaults->floor_freq_reg_addr) {
  894. pr_err("Invalid floor frequency config register\n");
  895. return;
  896. }
  897. if (!power_ctrl_orig_val)
  898. iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
  899. rapl_defaults->floor_freq_reg_addr,
  900. &power_ctrl_orig_val);
  901. mdata = power_ctrl_orig_val;
  902. if (enable) {
  903. mdata &= ~(0x7f << 8);
  904. mdata |= 1 << 8;
  905. }
  906. iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
  907. rapl_defaults->floor_freq_reg_addr, mdata);
  908. }
  909. static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
  910. bool to_raw)
  911. {
  912. u64 f, y; /* fraction and exp. used for time unit */
  913. /*
  914. * Special processing based on 2^Y*(1+F/4), refer
  915. * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
  916. */
  917. if (!to_raw) {
  918. f = (value & 0x60) >> 5;
  919. y = value & 0x1f;
  920. value = (1 << y) * (4 + f) * rp->time_unit / 4;
  921. } else {
  922. do_div(value, rp->time_unit);
  923. y = ilog2(value);
  924. f = div64_u64(4 * (value - (1 << y)), 1 << y);
  925. value = (y & 0x1f) | ((f & 0x3) << 5);
  926. }
  927. return value;
  928. }
  929. static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
  930. bool to_raw)
  931. {
  932. /*
  933. * Atom time unit encoding is straight forward val * time_unit,
  934. * where time_unit is default to 1 sec. Never 0.
  935. */
  936. if (!to_raw)
  937. return (value) ? value *= rp->time_unit : rp->time_unit;
  938. else
  939. value = div64_u64(value, rp->time_unit);
  940. return value;
  941. }
  942. static const struct rapl_defaults rapl_defaults_core = {
  943. .floor_freq_reg_addr = 0,
  944. .check_unit = rapl_check_unit_core,
  945. .set_floor_freq = set_floor_freq_default,
  946. .compute_time_window = rapl_compute_time_window_core,
  947. };
  948. static const struct rapl_defaults rapl_defaults_hsw_server = {
  949. .check_unit = rapl_check_unit_core,
  950. .set_floor_freq = set_floor_freq_default,
  951. .compute_time_window = rapl_compute_time_window_core,
  952. .dram_domain_energy_unit = 15300,
  953. };
  954. static const struct rapl_defaults rapl_defaults_byt = {
  955. .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
  956. .check_unit = rapl_check_unit_atom,
  957. .set_floor_freq = set_floor_freq_atom,
  958. .compute_time_window = rapl_compute_time_window_atom,
  959. };
  960. static const struct rapl_defaults rapl_defaults_tng = {
  961. .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
  962. .check_unit = rapl_check_unit_atom,
  963. .set_floor_freq = set_floor_freq_atom,
  964. .compute_time_window = rapl_compute_time_window_atom,
  965. };
  966. static const struct rapl_defaults rapl_defaults_ann = {
  967. .floor_freq_reg_addr = 0,
  968. .check_unit = rapl_check_unit_atom,
  969. .set_floor_freq = NULL,
  970. .compute_time_window = rapl_compute_time_window_atom,
  971. };
  972. static const struct rapl_defaults rapl_defaults_cht = {
  973. .floor_freq_reg_addr = 0,
  974. .check_unit = rapl_check_unit_atom,
  975. .set_floor_freq = NULL,
  976. .compute_time_window = rapl_compute_time_window_atom,
  977. };
  978. #define RAPL_CPU(_model, _ops) { \
  979. .vendor = X86_VENDOR_INTEL, \
  980. .family = 6, \
  981. .model = _model, \
  982. .driver_data = (kernel_ulong_t)&_ops, \
  983. }
  984. static const struct x86_cpu_id rapl_ids[] __initconst = {
  985. RAPL_CPU(INTEL_FAM6_SANDYBRIDGE, rapl_defaults_core),
  986. RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X, rapl_defaults_core),
  987. RAPL_CPU(INTEL_FAM6_IVYBRIDGE, rapl_defaults_core),
  988. RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X, rapl_defaults_core),
  989. RAPL_CPU(INTEL_FAM6_HASWELL_CORE, rapl_defaults_core),
  990. RAPL_CPU(INTEL_FAM6_HASWELL_ULT, rapl_defaults_core),
  991. RAPL_CPU(INTEL_FAM6_HASWELL_GT3E, rapl_defaults_core),
  992. RAPL_CPU(INTEL_FAM6_HASWELL_X, rapl_defaults_hsw_server),
  993. RAPL_CPU(INTEL_FAM6_BROADWELL_CORE, rapl_defaults_core),
  994. RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E, rapl_defaults_core),
  995. RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D, rapl_defaults_core),
  996. RAPL_CPU(INTEL_FAM6_BROADWELL_X, rapl_defaults_hsw_server),
  997. RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP, rapl_defaults_core),
  998. RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE, rapl_defaults_core),
  999. RAPL_CPU(INTEL_FAM6_SKYLAKE_X, rapl_defaults_hsw_server),
  1000. RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE, rapl_defaults_core),
  1001. RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP, rapl_defaults_core),
  1002. RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT1, rapl_defaults_byt),
  1003. RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT, rapl_defaults_cht),
  1004. RAPL_CPU(INTEL_FAM6_ATOM_MERRIFIELD, rapl_defaults_tng),
  1005. RAPL_CPU(INTEL_FAM6_ATOM_MOOREFIELD, rapl_defaults_ann),
  1006. RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT, rapl_defaults_core),
  1007. RAPL_CPU(INTEL_FAM6_ATOM_DENVERTON, rapl_defaults_core),
  1008. RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL, rapl_defaults_hsw_server),
  1009. {}
  1010. };
  1011. MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
  1012. /* read once for all raw primitive data for all packages, domains */
  1013. static void rapl_update_domain_data(void)
  1014. {
  1015. int dmn, prim;
  1016. u64 val;
  1017. struct rapl_package *rp;
  1018. list_for_each_entry(rp, &rapl_packages, plist) {
  1019. for (dmn = 0; dmn < rp->nr_domains; dmn++) {
  1020. pr_debug("update package %d domain %s data\n", rp->id,
  1021. rp->domains[dmn].name);
  1022. /* exclude non-raw primitives */
  1023. for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++)
  1024. if (!rapl_read_data_raw(&rp->domains[dmn], prim,
  1025. rpi[prim].unit,
  1026. &val))
  1027. rp->domains[dmn].rdd.primitives[prim] =
  1028. val;
  1029. }
  1030. }
  1031. }
  1032. static int rapl_unregister_powercap(void)
  1033. {
  1034. struct rapl_package *rp;
  1035. struct rapl_domain *rd, *rd_package = NULL;
  1036. /* unregister all active rapl packages from the powercap layer,
  1037. * hotplug lock held
  1038. */
  1039. list_for_each_entry(rp, &rapl_packages, plist) {
  1040. package_power_limit_irq_restore(rp);
  1041. for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
  1042. rd++) {
  1043. pr_debug("remove package, undo power limit on %d: %s\n",
  1044. rp->id, rd->name);
  1045. rapl_write_data_raw(rd, PL1_ENABLE, 0);
  1046. rapl_write_data_raw(rd, PL1_CLAMP, 0);
  1047. if (find_nr_power_limit(rd) > 1) {
  1048. rapl_write_data_raw(rd, PL2_ENABLE, 0);
  1049. rapl_write_data_raw(rd, PL2_CLAMP, 0);
  1050. }
  1051. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  1052. rd_package = rd;
  1053. continue;
  1054. }
  1055. powercap_unregister_zone(control_type, &rd->power_zone);
  1056. }
  1057. /* do the package zone last */
  1058. if (rd_package)
  1059. powercap_unregister_zone(control_type,
  1060. &rd_package->power_zone);
  1061. }
  1062. if (platform_rapl_domain) {
  1063. powercap_unregister_zone(control_type,
  1064. &platform_rapl_domain->power_zone);
  1065. kfree(platform_rapl_domain);
  1066. }
  1067. powercap_unregister_control_type(control_type);
  1068. return 0;
  1069. }
  1070. static int rapl_package_register_powercap(struct rapl_package *rp)
  1071. {
  1072. struct rapl_domain *rd;
  1073. int ret = 0;
  1074. char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
  1075. struct powercap_zone *power_zone = NULL;
  1076. int nr_pl;
  1077. /* first we register package domain as the parent zone*/
  1078. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1079. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  1080. nr_pl = find_nr_power_limit(rd);
  1081. pr_debug("register socket %d package domain %s\n",
  1082. rp->id, rd->name);
  1083. memset(dev_name, 0, sizeof(dev_name));
  1084. snprintf(dev_name, sizeof(dev_name), "%s-%d",
  1085. rd->name, rp->id);
  1086. power_zone = powercap_register_zone(&rd->power_zone,
  1087. control_type,
  1088. dev_name, NULL,
  1089. &zone_ops[rd->id],
  1090. nr_pl,
  1091. &constraint_ops);
  1092. if (IS_ERR(power_zone)) {
  1093. pr_debug("failed to register package, %d\n",
  1094. rp->id);
  1095. ret = PTR_ERR(power_zone);
  1096. goto exit_package;
  1097. }
  1098. /* track parent zone in per package/socket data */
  1099. rp->power_zone = power_zone;
  1100. /* done, only one package domain per socket */
  1101. break;
  1102. }
  1103. }
  1104. if (!power_zone) {
  1105. pr_err("no package domain found, unknown topology!\n");
  1106. ret = -ENODEV;
  1107. goto exit_package;
  1108. }
  1109. /* now register domains as children of the socket/package*/
  1110. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1111. if (rd->id == RAPL_DOMAIN_PACKAGE)
  1112. continue;
  1113. /* number of power limits per domain varies */
  1114. nr_pl = find_nr_power_limit(rd);
  1115. power_zone = powercap_register_zone(&rd->power_zone,
  1116. control_type, rd->name,
  1117. rp->power_zone,
  1118. &zone_ops[rd->id], nr_pl,
  1119. &constraint_ops);
  1120. if (IS_ERR(power_zone)) {
  1121. pr_debug("failed to register power_zone, %d:%s:%s\n",
  1122. rp->id, rd->name, dev_name);
  1123. ret = PTR_ERR(power_zone);
  1124. goto err_cleanup;
  1125. }
  1126. }
  1127. exit_package:
  1128. return ret;
  1129. err_cleanup:
  1130. /* clean up previously initialized domains within the package if we
  1131. * failed after the first domain setup.
  1132. */
  1133. while (--rd >= rp->domains) {
  1134. pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
  1135. powercap_unregister_zone(control_type, &rd->power_zone);
  1136. }
  1137. return ret;
  1138. }
  1139. static int rapl_register_psys(void)
  1140. {
  1141. struct rapl_domain *rd;
  1142. struct powercap_zone *power_zone;
  1143. u64 val;
  1144. if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
  1145. return -ENODEV;
  1146. if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
  1147. return -ENODEV;
  1148. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  1149. if (!rd)
  1150. return -ENOMEM;
  1151. rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
  1152. rd->id = RAPL_DOMAIN_PLATFORM;
  1153. rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
  1154. rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
  1155. rd->rpl[0].prim_id = PL1_ENABLE;
  1156. rd->rpl[0].name = pl1_name;
  1157. rd->rpl[1].prim_id = PL2_ENABLE;
  1158. rd->rpl[1].name = pl2_name;
  1159. rd->rp = find_package_by_id(0);
  1160. power_zone = powercap_register_zone(&rd->power_zone, control_type,
  1161. "psys", NULL,
  1162. &zone_ops[RAPL_DOMAIN_PLATFORM],
  1163. 2, &constraint_ops);
  1164. if (IS_ERR(power_zone)) {
  1165. kfree(rd);
  1166. return PTR_ERR(power_zone);
  1167. }
  1168. platform_rapl_domain = rd;
  1169. return 0;
  1170. }
  1171. static int rapl_register_powercap(void)
  1172. {
  1173. struct rapl_domain *rd;
  1174. struct rapl_package *rp;
  1175. int ret = 0;
  1176. control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
  1177. if (IS_ERR(control_type)) {
  1178. pr_debug("failed to register powercap control_type.\n");
  1179. return PTR_ERR(control_type);
  1180. }
  1181. /* read the initial data */
  1182. rapl_update_domain_data();
  1183. list_for_each_entry(rp, &rapl_packages, plist)
  1184. if (rapl_package_register_powercap(rp))
  1185. goto err_cleanup_package;
  1186. /* Don't bail out if PSys is not supported */
  1187. rapl_register_psys();
  1188. return ret;
  1189. err_cleanup_package:
  1190. /* clean up previously initialized packages */
  1191. list_for_each_entry_continue_reverse(rp, &rapl_packages, plist) {
  1192. for (rd = rp->domains; rd < rp->domains + rp->nr_domains;
  1193. rd++) {
  1194. pr_debug("unregister zone/package %d, %s domain\n",
  1195. rp->id, rd->name);
  1196. powercap_unregister_zone(control_type, &rd->power_zone);
  1197. }
  1198. }
  1199. return ret;
  1200. }
  1201. static int rapl_check_domain(int cpu, int domain)
  1202. {
  1203. unsigned msr;
  1204. u64 val = 0;
  1205. switch (domain) {
  1206. case RAPL_DOMAIN_PACKAGE:
  1207. msr = MSR_PKG_ENERGY_STATUS;
  1208. break;
  1209. case RAPL_DOMAIN_PP0:
  1210. msr = MSR_PP0_ENERGY_STATUS;
  1211. break;
  1212. case RAPL_DOMAIN_PP1:
  1213. msr = MSR_PP1_ENERGY_STATUS;
  1214. break;
  1215. case RAPL_DOMAIN_DRAM:
  1216. msr = MSR_DRAM_ENERGY_STATUS;
  1217. break;
  1218. case RAPL_DOMAIN_PLATFORM:
  1219. /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
  1220. return -EINVAL;
  1221. default:
  1222. pr_err("invalid domain id %d\n", domain);
  1223. return -EINVAL;
  1224. }
  1225. /* make sure domain counters are available and contains non-zero
  1226. * values, otherwise skip it.
  1227. */
  1228. if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
  1229. return -ENODEV;
  1230. return 0;
  1231. }
  1232. /*
  1233. * Check if power limits are available. Two cases when they are not available:
  1234. * 1. Locked by BIOS, in this case we still provide read-only access so that
  1235. * users can see what limit is set by the BIOS.
  1236. * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
  1237. * exist at all. In this case, we do not show the contraints in powercap.
  1238. *
  1239. * Called after domains are detected and initialized.
  1240. */
  1241. static void rapl_detect_powerlimit(struct rapl_domain *rd)
  1242. {
  1243. u64 val64;
  1244. int i;
  1245. /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
  1246. if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
  1247. if (val64) {
  1248. pr_info("RAPL package %d domain %s locked by BIOS\n",
  1249. rd->rp->id, rd->name);
  1250. rd->state |= DOMAIN_STATE_BIOS_LOCKED;
  1251. }
  1252. }
  1253. /* check if power limit MSRs exists, otherwise domain is monitoring only */
  1254. for (i = 0; i < NR_POWER_LIMITS; i++) {
  1255. int prim = rd->rpl[i].prim_id;
  1256. if (rapl_read_data_raw(rd, prim, false, &val64))
  1257. rd->rpl[i].name = NULL;
  1258. }
  1259. }
  1260. /* Detect active and valid domains for the given CPU, caller must
  1261. * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
  1262. */
  1263. static int rapl_detect_domains(struct rapl_package *rp, int cpu)
  1264. {
  1265. int i;
  1266. int ret = 0;
  1267. struct rapl_domain *rd;
  1268. for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
  1269. /* use physical package id to read counters */
  1270. if (!rapl_check_domain(cpu, i)) {
  1271. rp->domain_map |= 1 << i;
  1272. pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
  1273. }
  1274. }
  1275. rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
  1276. if (!rp->nr_domains) {
  1277. pr_debug("no valid rapl domains found in package %d\n", rp->id);
  1278. ret = -ENODEV;
  1279. goto done;
  1280. }
  1281. pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
  1282. rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
  1283. GFP_KERNEL);
  1284. if (!rp->domains) {
  1285. ret = -ENOMEM;
  1286. goto done;
  1287. }
  1288. rapl_init_domains(rp);
  1289. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
  1290. rapl_detect_powerlimit(rd);
  1291. done:
  1292. return ret;
  1293. }
  1294. static bool is_package_new(int package)
  1295. {
  1296. struct rapl_package *rp;
  1297. /* caller prevents cpu hotplug, there will be no new packages added
  1298. * or deleted while traversing the package list, no need for locking.
  1299. */
  1300. list_for_each_entry(rp, &rapl_packages, plist)
  1301. if (package == rp->id)
  1302. return false;
  1303. return true;
  1304. }
  1305. /* RAPL interface can be made of a two-level hierarchy: package level and domain
  1306. * level. We first detect the number of packages then domains of each package.
  1307. * We have to consider the possiblity of CPU online/offline due to hotplug and
  1308. * other scenarios.
  1309. */
  1310. static int rapl_detect_topology(void)
  1311. {
  1312. int i;
  1313. int phy_package_id;
  1314. struct rapl_package *new_package, *rp;
  1315. for_each_online_cpu(i) {
  1316. phy_package_id = topology_physical_package_id(i);
  1317. if (is_package_new(phy_package_id)) {
  1318. new_package = kzalloc(sizeof(*rp), GFP_KERNEL);
  1319. if (!new_package) {
  1320. rapl_cleanup_data();
  1321. return -ENOMEM;
  1322. }
  1323. /* add the new package to the list */
  1324. new_package->id = phy_package_id;
  1325. new_package->nr_cpus = 1;
  1326. /* use the first active cpu of the package to access */
  1327. new_package->lead_cpu = i;
  1328. /* check if the package contains valid domains */
  1329. if (rapl_detect_domains(new_package, i) ||
  1330. rapl_defaults->check_unit(new_package, i)) {
  1331. kfree(new_package->domains);
  1332. kfree(new_package);
  1333. /* free up the packages already initialized */
  1334. rapl_cleanup_data();
  1335. return -ENODEV;
  1336. }
  1337. INIT_LIST_HEAD(&new_package->plist);
  1338. list_add(&new_package->plist, &rapl_packages);
  1339. } else {
  1340. rp = find_package_by_id(phy_package_id);
  1341. if (rp)
  1342. ++rp->nr_cpus;
  1343. }
  1344. }
  1345. return 0;
  1346. }
  1347. /* called from CPU hotplug notifier, hotplug lock held */
  1348. static void rapl_remove_package(struct rapl_package *rp)
  1349. {
  1350. struct rapl_domain *rd, *rd_package = NULL;
  1351. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1352. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  1353. rd_package = rd;
  1354. continue;
  1355. }
  1356. pr_debug("remove package %d, %s domain\n", rp->id, rd->name);
  1357. powercap_unregister_zone(control_type, &rd->power_zone);
  1358. }
  1359. /* do parent zone last */
  1360. powercap_unregister_zone(control_type, &rd_package->power_zone);
  1361. list_del(&rp->plist);
  1362. kfree(rp);
  1363. }
  1364. /* called from CPU hotplug notifier, hotplug lock held */
  1365. static int rapl_add_package(int cpu)
  1366. {
  1367. int ret = 0;
  1368. int phy_package_id;
  1369. struct rapl_package *rp;
  1370. phy_package_id = topology_physical_package_id(cpu);
  1371. rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
  1372. if (!rp)
  1373. return -ENOMEM;
  1374. /* add the new package to the list */
  1375. rp->id = phy_package_id;
  1376. rp->nr_cpus = 1;
  1377. rp->lead_cpu = cpu;
  1378. /* check if the package contains valid domains */
  1379. if (rapl_detect_domains(rp, cpu) ||
  1380. rapl_defaults->check_unit(rp, cpu)) {
  1381. ret = -ENODEV;
  1382. goto err_free_package;
  1383. }
  1384. if (!rapl_package_register_powercap(rp)) {
  1385. INIT_LIST_HEAD(&rp->plist);
  1386. list_add(&rp->plist, &rapl_packages);
  1387. return ret;
  1388. }
  1389. err_free_package:
  1390. kfree(rp->domains);
  1391. kfree(rp);
  1392. return ret;
  1393. }
  1394. /* Handles CPU hotplug on multi-socket systems.
  1395. * If a CPU goes online as the first CPU of the physical package
  1396. * we add the RAPL package to the system. Similarly, when the last
  1397. * CPU of the package is removed, we remove the RAPL package and its
  1398. * associated domains. Cooling devices are handled accordingly at
  1399. * per-domain level.
  1400. */
  1401. static int rapl_cpu_callback(struct notifier_block *nfb,
  1402. unsigned long action, void *hcpu)
  1403. {
  1404. unsigned long cpu = (unsigned long)hcpu;
  1405. int phy_package_id;
  1406. struct rapl_package *rp;
  1407. int lead_cpu;
  1408. phy_package_id = topology_physical_package_id(cpu);
  1409. switch (action) {
  1410. case CPU_ONLINE:
  1411. case CPU_ONLINE_FROZEN:
  1412. case CPU_DOWN_FAILED:
  1413. case CPU_DOWN_FAILED_FROZEN:
  1414. rp = find_package_by_id(phy_package_id);
  1415. if (rp)
  1416. ++rp->nr_cpus;
  1417. else
  1418. rapl_add_package(cpu);
  1419. break;
  1420. case CPU_DOWN_PREPARE:
  1421. case CPU_DOWN_PREPARE_FROZEN:
  1422. rp = find_package_by_id(phy_package_id);
  1423. if (!rp)
  1424. break;
  1425. if (--rp->nr_cpus == 0)
  1426. rapl_remove_package(rp);
  1427. else if (cpu == rp->lead_cpu) {
  1428. /* choose another active cpu in the package */
  1429. lead_cpu = cpumask_any_but(topology_core_cpumask(cpu), cpu);
  1430. if (lead_cpu < nr_cpu_ids)
  1431. rp->lead_cpu = lead_cpu;
  1432. else /* should never go here */
  1433. pr_err("no active cpu available for package %d\n",
  1434. phy_package_id);
  1435. }
  1436. }
  1437. return NOTIFY_OK;
  1438. }
  1439. static struct notifier_block rapl_cpu_notifier = {
  1440. .notifier_call = rapl_cpu_callback,
  1441. };
  1442. static int __init rapl_init(void)
  1443. {
  1444. int ret = 0;
  1445. const struct x86_cpu_id *id;
  1446. id = x86_match_cpu(rapl_ids);
  1447. if (!id) {
  1448. pr_err("driver does not support CPU family %d model %d\n",
  1449. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1450. return -ENODEV;
  1451. }
  1452. rapl_defaults = (struct rapl_defaults *)id->driver_data;
  1453. cpu_notifier_register_begin();
  1454. /* prevent CPU hotplug during detection */
  1455. get_online_cpus();
  1456. ret = rapl_detect_topology();
  1457. if (ret)
  1458. goto done;
  1459. if (rapl_register_powercap()) {
  1460. rapl_cleanup_data();
  1461. ret = -ENODEV;
  1462. goto done;
  1463. }
  1464. __register_hotcpu_notifier(&rapl_cpu_notifier);
  1465. done:
  1466. put_online_cpus();
  1467. cpu_notifier_register_done();
  1468. return ret;
  1469. }
  1470. static void __exit rapl_exit(void)
  1471. {
  1472. cpu_notifier_register_begin();
  1473. get_online_cpus();
  1474. __unregister_hotcpu_notifier(&rapl_cpu_notifier);
  1475. rapl_unregister_powercap();
  1476. rapl_cleanup_data();
  1477. put_online_cpus();
  1478. cpu_notifier_register_done();
  1479. }
  1480. module_init(rapl_init);
  1481. module_exit(rapl_exit);
  1482. MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
  1483. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
  1484. MODULE_LICENSE("GPL v2");