intel_rapl.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. unsigned long power_limit_irq; /* keep track of package power limit
  163. * notify interrupt enable status.
  164. */
  165. struct list_head plist;
  166. int lead_cpu; /* one active cpu per package for access */
  167. /* Track active cpus */
  168. struct cpumask cpumask;
  169. };
  170. struct rapl_defaults {
  171. u8 floor_freq_reg_addr;
  172. int (*check_unit)(struct rapl_package *rp, int cpu);
  173. void (*set_floor_freq)(struct rapl_domain *rd, bool mode);
  174. u64 (*compute_time_window)(struct rapl_package *rp, u64 val,
  175. bool to_raw);
  176. unsigned int dram_domain_energy_unit;
  177. };
  178. static struct rapl_defaults *rapl_defaults;
  179. /* Sideband MBI registers */
  180. #define IOSF_CPU_POWER_BUDGET_CTL_BYT (0x2)
  181. #define IOSF_CPU_POWER_BUDGET_CTL_TNG (0xdf)
  182. #define PACKAGE_PLN_INT_SAVED BIT(0)
  183. #define MAX_PRIM_NAME (32)
  184. /* per domain data. used to describe individual knobs such that access function
  185. * can be consolidated into one instead of many inline functions.
  186. */
  187. struct rapl_primitive_info {
  188. const char *name;
  189. u64 mask;
  190. int shift;
  191. enum rapl_domain_msr_id id;
  192. enum unit_type unit;
  193. u32 flag;
  194. };
  195. #define PRIMITIVE_INFO_INIT(p, m, s, i, u, f) { \
  196. .name = #p, \
  197. .mask = m, \
  198. .shift = s, \
  199. .id = i, \
  200. .unit = u, \
  201. .flag = f \
  202. }
  203. static void rapl_init_domains(struct rapl_package *rp);
  204. static int rapl_read_data_raw(struct rapl_domain *rd,
  205. enum rapl_primitives prim,
  206. bool xlate, u64 *data);
  207. static int rapl_write_data_raw(struct rapl_domain *rd,
  208. enum rapl_primitives prim,
  209. unsigned long long value);
  210. static u64 rapl_unit_xlate(struct rapl_domain *rd,
  211. enum unit_type type, u64 value,
  212. int to_raw);
  213. static void package_power_limit_irq_save(struct rapl_package *rp);
  214. static LIST_HEAD(rapl_packages); /* guarded by CPU hotplug lock */
  215. static const char * const rapl_domain_names[] = {
  216. "package",
  217. "core",
  218. "uncore",
  219. "dram",
  220. "psys",
  221. };
  222. static struct powercap_control_type *control_type; /* PowerCap Controller */
  223. static struct rapl_domain *platform_rapl_domain; /* Platform (PSys) domain */
  224. /* caller to ensure CPU hotplug lock is held */
  225. static struct rapl_package *find_package_by_id(int id)
  226. {
  227. struct rapl_package *rp;
  228. list_for_each_entry(rp, &rapl_packages, plist) {
  229. if (rp->id == id)
  230. return rp;
  231. }
  232. return NULL;
  233. }
  234. static int get_energy_counter(struct powercap_zone *power_zone, u64 *energy_raw)
  235. {
  236. struct rapl_domain *rd;
  237. u64 energy_now;
  238. /* prevent CPU hotplug, make sure the RAPL domain does not go
  239. * away while reading the counter.
  240. */
  241. get_online_cpus();
  242. rd = power_zone_to_rapl_domain(power_zone);
  243. if (!rapl_read_data_raw(rd, ENERGY_COUNTER, true, &energy_now)) {
  244. *energy_raw = energy_now;
  245. put_online_cpus();
  246. return 0;
  247. }
  248. put_online_cpus();
  249. return -EIO;
  250. }
  251. static int get_max_energy_counter(struct powercap_zone *pcd_dev, u64 *energy)
  252. {
  253. struct rapl_domain *rd = power_zone_to_rapl_domain(pcd_dev);
  254. *energy = rapl_unit_xlate(rd, ENERGY_UNIT, ENERGY_STATUS_MASK, 0);
  255. return 0;
  256. }
  257. static int release_zone(struct powercap_zone *power_zone)
  258. {
  259. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  260. struct rapl_package *rp = rd->rp;
  261. /* package zone is the last zone of a package, we can free
  262. * memory here since all children has been unregistered.
  263. */
  264. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  265. kfree(rd);
  266. rp->domains = NULL;
  267. }
  268. return 0;
  269. }
  270. static int find_nr_power_limit(struct rapl_domain *rd)
  271. {
  272. int i, nr_pl = 0;
  273. for (i = 0; i < NR_POWER_LIMITS; i++) {
  274. if (rd->rpl[i].name)
  275. nr_pl++;
  276. }
  277. return nr_pl;
  278. }
  279. static int set_domain_enable(struct powercap_zone *power_zone, bool mode)
  280. {
  281. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  282. if (rd->state & DOMAIN_STATE_BIOS_LOCKED)
  283. return -EACCES;
  284. get_online_cpus();
  285. rapl_write_data_raw(rd, PL1_ENABLE, mode);
  286. if (rapl_defaults->set_floor_freq)
  287. rapl_defaults->set_floor_freq(rd, mode);
  288. put_online_cpus();
  289. return 0;
  290. }
  291. static int get_domain_enable(struct powercap_zone *power_zone, bool *mode)
  292. {
  293. struct rapl_domain *rd = power_zone_to_rapl_domain(power_zone);
  294. u64 val;
  295. if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
  296. *mode = false;
  297. return 0;
  298. }
  299. get_online_cpus();
  300. if (rapl_read_data_raw(rd, PL1_ENABLE, true, &val)) {
  301. put_online_cpus();
  302. return -EIO;
  303. }
  304. *mode = val;
  305. put_online_cpus();
  306. return 0;
  307. }
  308. /* per RAPL domain ops, in the order of rapl_domain_type */
  309. static const struct powercap_zone_ops zone_ops[] = {
  310. /* RAPL_DOMAIN_PACKAGE */
  311. {
  312. .get_energy_uj = get_energy_counter,
  313. .get_max_energy_range_uj = get_max_energy_counter,
  314. .release = release_zone,
  315. .set_enable = set_domain_enable,
  316. .get_enable = get_domain_enable,
  317. },
  318. /* RAPL_DOMAIN_PP0 */
  319. {
  320. .get_energy_uj = get_energy_counter,
  321. .get_max_energy_range_uj = get_max_energy_counter,
  322. .release = release_zone,
  323. .set_enable = set_domain_enable,
  324. .get_enable = get_domain_enable,
  325. },
  326. /* RAPL_DOMAIN_PP1 */
  327. {
  328. .get_energy_uj = get_energy_counter,
  329. .get_max_energy_range_uj = get_max_energy_counter,
  330. .release = release_zone,
  331. .set_enable = set_domain_enable,
  332. .get_enable = get_domain_enable,
  333. },
  334. /* RAPL_DOMAIN_DRAM */
  335. {
  336. .get_energy_uj = get_energy_counter,
  337. .get_max_energy_range_uj = get_max_energy_counter,
  338. .release = release_zone,
  339. .set_enable = set_domain_enable,
  340. .get_enable = get_domain_enable,
  341. },
  342. /* RAPL_DOMAIN_PLATFORM */
  343. {
  344. .get_energy_uj = get_energy_counter,
  345. .get_max_energy_range_uj = get_max_energy_counter,
  346. .release = release_zone,
  347. .set_enable = set_domain_enable,
  348. .get_enable = get_domain_enable,
  349. },
  350. };
  351. /*
  352. * Constraint index used by powercap can be different than power limit (PL)
  353. * index in that some PLs maybe missing due to non-existant MSRs. So we
  354. * need to convert here by finding the valid PLs only (name populated).
  355. */
  356. static int contraint_to_pl(struct rapl_domain *rd, int cid)
  357. {
  358. int i, j;
  359. for (i = 0, j = 0; i < NR_POWER_LIMITS; i++) {
  360. if ((rd->rpl[i].name) && j++ == cid) {
  361. pr_debug("%s: index %d\n", __func__, i);
  362. return i;
  363. }
  364. }
  365. pr_err("Cannot find matching power limit for constraint %d\n", cid);
  366. return -EINVAL;
  367. }
  368. static int set_power_limit(struct powercap_zone *power_zone, int cid,
  369. u64 power_limit)
  370. {
  371. struct rapl_domain *rd;
  372. struct rapl_package *rp;
  373. int ret = 0;
  374. int id;
  375. get_online_cpus();
  376. rd = power_zone_to_rapl_domain(power_zone);
  377. id = contraint_to_pl(rd, cid);
  378. if (id < 0) {
  379. ret = id;
  380. goto set_exit;
  381. }
  382. rp = rd->rp;
  383. if (rd->state & DOMAIN_STATE_BIOS_LOCKED) {
  384. dev_warn(&power_zone->dev, "%s locked by BIOS, monitoring only\n",
  385. rd->name);
  386. ret = -EACCES;
  387. goto set_exit;
  388. }
  389. switch (rd->rpl[id].prim_id) {
  390. case PL1_ENABLE:
  391. rapl_write_data_raw(rd, POWER_LIMIT1, power_limit);
  392. break;
  393. case PL2_ENABLE:
  394. rapl_write_data_raw(rd, POWER_LIMIT2, power_limit);
  395. break;
  396. default:
  397. ret = -EINVAL;
  398. }
  399. if (!ret)
  400. package_power_limit_irq_save(rp);
  401. set_exit:
  402. put_online_cpus();
  403. return ret;
  404. }
  405. static int get_current_power_limit(struct powercap_zone *power_zone, int cid,
  406. u64 *data)
  407. {
  408. struct rapl_domain *rd;
  409. u64 val;
  410. int prim;
  411. int ret = 0;
  412. int id;
  413. get_online_cpus();
  414. rd = power_zone_to_rapl_domain(power_zone);
  415. id = contraint_to_pl(rd, cid);
  416. if (id < 0) {
  417. ret = id;
  418. goto get_exit;
  419. }
  420. switch (rd->rpl[id].prim_id) {
  421. case PL1_ENABLE:
  422. prim = POWER_LIMIT1;
  423. break;
  424. case PL2_ENABLE:
  425. prim = POWER_LIMIT2;
  426. break;
  427. default:
  428. put_online_cpus();
  429. return -EINVAL;
  430. }
  431. if (rapl_read_data_raw(rd, prim, true, &val))
  432. ret = -EIO;
  433. else
  434. *data = val;
  435. get_exit:
  436. put_online_cpus();
  437. return ret;
  438. }
  439. static int set_time_window(struct powercap_zone *power_zone, int cid,
  440. u64 window)
  441. {
  442. struct rapl_domain *rd;
  443. int ret = 0;
  444. int id;
  445. get_online_cpus();
  446. rd = power_zone_to_rapl_domain(power_zone);
  447. id = contraint_to_pl(rd, cid);
  448. if (id < 0) {
  449. ret = id;
  450. goto set_time_exit;
  451. }
  452. switch (rd->rpl[id].prim_id) {
  453. case PL1_ENABLE:
  454. rapl_write_data_raw(rd, TIME_WINDOW1, window);
  455. break;
  456. case PL2_ENABLE:
  457. rapl_write_data_raw(rd, TIME_WINDOW2, window);
  458. break;
  459. default:
  460. ret = -EINVAL;
  461. }
  462. set_time_exit:
  463. put_online_cpus();
  464. return ret;
  465. }
  466. static int get_time_window(struct powercap_zone *power_zone, int cid, u64 *data)
  467. {
  468. struct rapl_domain *rd;
  469. u64 val;
  470. int ret = 0;
  471. int id;
  472. get_online_cpus();
  473. rd = power_zone_to_rapl_domain(power_zone);
  474. id = contraint_to_pl(rd, cid);
  475. if (id < 0) {
  476. ret = id;
  477. goto get_time_exit;
  478. }
  479. switch (rd->rpl[id].prim_id) {
  480. case PL1_ENABLE:
  481. ret = rapl_read_data_raw(rd, TIME_WINDOW1, true, &val);
  482. break;
  483. case PL2_ENABLE:
  484. ret = rapl_read_data_raw(rd, TIME_WINDOW2, true, &val);
  485. break;
  486. default:
  487. put_online_cpus();
  488. return -EINVAL;
  489. }
  490. if (!ret)
  491. *data = val;
  492. get_time_exit:
  493. put_online_cpus();
  494. return ret;
  495. }
  496. static const char *get_constraint_name(struct powercap_zone *power_zone, int cid)
  497. {
  498. struct rapl_domain *rd;
  499. int id;
  500. rd = power_zone_to_rapl_domain(power_zone);
  501. id = contraint_to_pl(rd, cid);
  502. if (id >= 0)
  503. return rd->rpl[id].name;
  504. return NULL;
  505. }
  506. static int get_max_power(struct powercap_zone *power_zone, int id,
  507. u64 *data)
  508. {
  509. struct rapl_domain *rd;
  510. u64 val;
  511. int prim;
  512. int ret = 0;
  513. get_online_cpus();
  514. rd = power_zone_to_rapl_domain(power_zone);
  515. switch (rd->rpl[id].prim_id) {
  516. case PL1_ENABLE:
  517. prim = THERMAL_SPEC_POWER;
  518. break;
  519. case PL2_ENABLE:
  520. prim = MAX_POWER;
  521. break;
  522. default:
  523. put_online_cpus();
  524. return -EINVAL;
  525. }
  526. if (rapl_read_data_raw(rd, prim, true, &val))
  527. ret = -EIO;
  528. else
  529. *data = val;
  530. put_online_cpus();
  531. return ret;
  532. }
  533. static const struct powercap_zone_constraint_ops constraint_ops = {
  534. .set_power_limit_uw = set_power_limit,
  535. .get_power_limit_uw = get_current_power_limit,
  536. .set_time_window_us = set_time_window,
  537. .get_time_window_us = get_time_window,
  538. .get_max_power_uw = get_max_power,
  539. .get_name = get_constraint_name,
  540. };
  541. /* called after domain detection and package level data are set */
  542. static void rapl_init_domains(struct rapl_package *rp)
  543. {
  544. int i;
  545. struct rapl_domain *rd = rp->domains;
  546. for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
  547. unsigned int mask = rp->domain_map & (1 << i);
  548. switch (mask) {
  549. case BIT(RAPL_DOMAIN_PACKAGE):
  550. rd->name = rapl_domain_names[RAPL_DOMAIN_PACKAGE];
  551. rd->id = RAPL_DOMAIN_PACKAGE;
  552. rd->msrs[0] = MSR_PKG_POWER_LIMIT;
  553. rd->msrs[1] = MSR_PKG_ENERGY_STATUS;
  554. rd->msrs[2] = MSR_PKG_PERF_STATUS;
  555. rd->msrs[3] = 0;
  556. rd->msrs[4] = MSR_PKG_POWER_INFO;
  557. rd->rpl[0].prim_id = PL1_ENABLE;
  558. rd->rpl[0].name = pl1_name;
  559. rd->rpl[1].prim_id = PL2_ENABLE;
  560. rd->rpl[1].name = pl2_name;
  561. break;
  562. case BIT(RAPL_DOMAIN_PP0):
  563. rd->name = rapl_domain_names[RAPL_DOMAIN_PP0];
  564. rd->id = RAPL_DOMAIN_PP0;
  565. rd->msrs[0] = MSR_PP0_POWER_LIMIT;
  566. rd->msrs[1] = MSR_PP0_ENERGY_STATUS;
  567. rd->msrs[2] = 0;
  568. rd->msrs[3] = MSR_PP0_POLICY;
  569. rd->msrs[4] = 0;
  570. rd->rpl[0].prim_id = PL1_ENABLE;
  571. rd->rpl[0].name = pl1_name;
  572. break;
  573. case BIT(RAPL_DOMAIN_PP1):
  574. rd->name = rapl_domain_names[RAPL_DOMAIN_PP1];
  575. rd->id = RAPL_DOMAIN_PP1;
  576. rd->msrs[0] = MSR_PP1_POWER_LIMIT;
  577. rd->msrs[1] = MSR_PP1_ENERGY_STATUS;
  578. rd->msrs[2] = 0;
  579. rd->msrs[3] = MSR_PP1_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_DRAM):
  585. rd->name = rapl_domain_names[RAPL_DOMAIN_DRAM];
  586. rd->id = RAPL_DOMAIN_DRAM;
  587. rd->msrs[0] = MSR_DRAM_POWER_LIMIT;
  588. rd->msrs[1] = MSR_DRAM_ENERGY_STATUS;
  589. rd->msrs[2] = MSR_DRAM_PERF_STATUS;
  590. rd->msrs[3] = 0;
  591. rd->msrs[4] = MSR_DRAM_POWER_INFO;
  592. rd->rpl[0].prim_id = PL1_ENABLE;
  593. rd->rpl[0].name = pl1_name;
  594. rd->domain_energy_unit =
  595. rapl_defaults->dram_domain_energy_unit;
  596. if (rd->domain_energy_unit)
  597. pr_info("DRAM domain energy unit %dpj\n",
  598. rd->domain_energy_unit);
  599. break;
  600. }
  601. if (mask) {
  602. rd->rp = rp;
  603. rd++;
  604. }
  605. }
  606. }
  607. static u64 rapl_unit_xlate(struct rapl_domain *rd, enum unit_type type,
  608. u64 value, int to_raw)
  609. {
  610. u64 units = 1;
  611. struct rapl_package *rp = rd->rp;
  612. u64 scale = 1;
  613. switch (type) {
  614. case POWER_UNIT:
  615. units = rp->power_unit;
  616. break;
  617. case ENERGY_UNIT:
  618. scale = ENERGY_UNIT_SCALE;
  619. /* per domain unit takes precedence */
  620. if (rd->domain_energy_unit)
  621. units = rd->domain_energy_unit;
  622. else
  623. units = rp->energy_unit;
  624. break;
  625. case TIME_UNIT:
  626. return rapl_defaults->compute_time_window(rp, value, to_raw);
  627. case ARBITRARY_UNIT:
  628. default:
  629. return value;
  630. };
  631. if (to_raw)
  632. return div64_u64(value, units) * scale;
  633. value *= units;
  634. return div64_u64(value, scale);
  635. }
  636. /* in the order of enum rapl_primitives */
  637. static struct rapl_primitive_info rpi[] = {
  638. /* name, mask, shift, msr index, unit divisor */
  639. PRIMITIVE_INFO_INIT(ENERGY_COUNTER, ENERGY_STATUS_MASK, 0,
  640. RAPL_DOMAIN_MSR_STATUS, ENERGY_UNIT, 0),
  641. PRIMITIVE_INFO_INIT(POWER_LIMIT1, POWER_LIMIT1_MASK, 0,
  642. RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
  643. PRIMITIVE_INFO_INIT(POWER_LIMIT2, POWER_LIMIT2_MASK, 32,
  644. RAPL_DOMAIN_MSR_LIMIT, POWER_UNIT, 0),
  645. PRIMITIVE_INFO_INIT(FW_LOCK, POWER_PP_LOCK, 31,
  646. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  647. PRIMITIVE_INFO_INIT(PL1_ENABLE, POWER_LIMIT1_ENABLE, 15,
  648. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  649. PRIMITIVE_INFO_INIT(PL1_CLAMP, POWER_LIMIT1_CLAMP, 16,
  650. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  651. PRIMITIVE_INFO_INIT(PL2_ENABLE, POWER_LIMIT2_ENABLE, 47,
  652. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  653. PRIMITIVE_INFO_INIT(PL2_CLAMP, POWER_LIMIT2_CLAMP, 48,
  654. RAPL_DOMAIN_MSR_LIMIT, ARBITRARY_UNIT, 0),
  655. PRIMITIVE_INFO_INIT(TIME_WINDOW1, TIME_WINDOW1_MASK, 17,
  656. RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
  657. PRIMITIVE_INFO_INIT(TIME_WINDOW2, TIME_WINDOW2_MASK, 49,
  658. RAPL_DOMAIN_MSR_LIMIT, TIME_UNIT, 0),
  659. PRIMITIVE_INFO_INIT(THERMAL_SPEC_POWER, POWER_INFO_THERMAL_SPEC_MASK,
  660. 0, RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  661. PRIMITIVE_INFO_INIT(MAX_POWER, POWER_INFO_MAX_MASK, 32,
  662. RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  663. PRIMITIVE_INFO_INIT(MIN_POWER, POWER_INFO_MIN_MASK, 16,
  664. RAPL_DOMAIN_MSR_INFO, POWER_UNIT, 0),
  665. PRIMITIVE_INFO_INIT(MAX_TIME_WINDOW, POWER_INFO_MAX_TIME_WIN_MASK, 48,
  666. RAPL_DOMAIN_MSR_INFO, TIME_UNIT, 0),
  667. PRIMITIVE_INFO_INIT(THROTTLED_TIME, PERF_STATUS_THROTTLE_TIME_MASK, 0,
  668. RAPL_DOMAIN_MSR_PERF, TIME_UNIT, 0),
  669. PRIMITIVE_INFO_INIT(PRIORITY_LEVEL, PP_POLICY_MASK, 0,
  670. RAPL_DOMAIN_MSR_POLICY, ARBITRARY_UNIT, 0),
  671. /* non-hardware */
  672. PRIMITIVE_INFO_INIT(AVERAGE_POWER, 0, 0, 0, POWER_UNIT,
  673. RAPL_PRIMITIVE_DERIVED),
  674. {NULL, 0, 0, 0},
  675. };
  676. /* Read primitive data based on its related struct rapl_primitive_info.
  677. * if xlate flag is set, return translated data based on data units, i.e.
  678. * time, energy, and power.
  679. * RAPL MSRs are non-architectual and are laid out not consistently across
  680. * domains. Here we use primitive info to allow writing consolidated access
  681. * functions.
  682. * For a given primitive, it is processed by MSR mask and shift. Unit conversion
  683. * is pre-assigned based on RAPL unit MSRs read at init time.
  684. * 63-------------------------- 31--------------------------- 0
  685. * | xxxxx (mask) |
  686. * | |<- shift ----------------|
  687. * 63-------------------------- 31--------------------------- 0
  688. */
  689. static int rapl_read_data_raw(struct rapl_domain *rd,
  690. enum rapl_primitives prim,
  691. bool xlate, u64 *data)
  692. {
  693. u64 value, final;
  694. u32 msr;
  695. struct rapl_primitive_info *rp = &rpi[prim];
  696. int cpu;
  697. if (!rp->name || rp->flag & RAPL_PRIMITIVE_DUMMY)
  698. return -EINVAL;
  699. msr = rd->msrs[rp->id];
  700. if (!msr)
  701. return -EINVAL;
  702. cpu = rd->rp->lead_cpu;
  703. /* special-case package domain, which uses a different bit*/
  704. if (prim == FW_LOCK && rd->id == RAPL_DOMAIN_PACKAGE) {
  705. rp->mask = POWER_PACKAGE_LOCK;
  706. rp->shift = 63;
  707. }
  708. /* non-hardware data are collected by the polling thread */
  709. if (rp->flag & RAPL_PRIMITIVE_DERIVED) {
  710. *data = rd->rdd.primitives[prim];
  711. return 0;
  712. }
  713. if (rdmsrl_safe_on_cpu(cpu, msr, &value)) {
  714. pr_debug("failed to read msr 0x%x on cpu %d\n", msr, cpu);
  715. return -EIO;
  716. }
  717. final = value & rp->mask;
  718. final = final >> rp->shift;
  719. if (xlate)
  720. *data = rapl_unit_xlate(rd, rp->unit, final, 0);
  721. else
  722. *data = final;
  723. return 0;
  724. }
  725. static int msrl_update_safe(u32 msr_no, u64 clear_mask, u64 set_mask)
  726. {
  727. int err;
  728. u64 val;
  729. err = rdmsrl_safe(msr_no, &val);
  730. if (err)
  731. goto out;
  732. val &= ~clear_mask;
  733. val |= set_mask;
  734. err = wrmsrl_safe(msr_no, val);
  735. out:
  736. return err;
  737. }
  738. static void msrl_update_func(void *info)
  739. {
  740. struct msrl_action *ma = info;
  741. ma->err = msrl_update_safe(ma->msr_no, ma->clear_mask, ma->set_mask);
  742. }
  743. /* Similar use of primitive info in the read counterpart */
  744. static int rapl_write_data_raw(struct rapl_domain *rd,
  745. enum rapl_primitives prim,
  746. unsigned long long value)
  747. {
  748. struct rapl_primitive_info *rp = &rpi[prim];
  749. int cpu;
  750. u64 bits;
  751. struct msrl_action ma;
  752. int ret;
  753. cpu = rd->rp->lead_cpu;
  754. bits = rapl_unit_xlate(rd, rp->unit, value, 1);
  755. bits <<= rp->shift;
  756. bits &= rp->mask;
  757. memset(&ma, 0, sizeof(ma));
  758. ma.msr_no = rd->msrs[rp->id];
  759. ma.clear_mask = rp->mask;
  760. ma.set_mask = bits;
  761. ret = smp_call_function_single(cpu, msrl_update_func, &ma, 1);
  762. if (ret)
  763. WARN_ON_ONCE(ret);
  764. else
  765. ret = ma.err;
  766. return ret;
  767. }
  768. /*
  769. * Raw RAPL data stored in MSRs are in certain scales. We need to
  770. * convert them into standard units based on the units reported in
  771. * the RAPL unit MSRs. This is specific to CPUs as the method to
  772. * calculate units differ on different CPUs.
  773. * We convert the units to below format based on CPUs.
  774. * i.e.
  775. * energy unit: picoJoules : Represented in picoJoules by default
  776. * power unit : microWatts : Represented in milliWatts by default
  777. * time unit : microseconds: Represented in seconds by default
  778. */
  779. static int rapl_check_unit_core(struct rapl_package *rp, int cpu)
  780. {
  781. u64 msr_val;
  782. u32 value;
  783. if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
  784. pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
  785. MSR_RAPL_POWER_UNIT, cpu);
  786. return -ENODEV;
  787. }
  788. value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
  789. rp->energy_unit = ENERGY_UNIT_SCALE * 1000000 / (1 << value);
  790. value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
  791. rp->power_unit = 1000000 / (1 << value);
  792. value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
  793. rp->time_unit = 1000000 / (1 << value);
  794. pr_debug("Core CPU package %d energy=%dpJ, time=%dus, power=%duW\n",
  795. rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
  796. return 0;
  797. }
  798. static int rapl_check_unit_atom(struct rapl_package *rp, int cpu)
  799. {
  800. u64 msr_val;
  801. u32 value;
  802. if (rdmsrl_safe_on_cpu(cpu, MSR_RAPL_POWER_UNIT, &msr_val)) {
  803. pr_err("Failed to read power unit MSR 0x%x on CPU %d, exit.\n",
  804. MSR_RAPL_POWER_UNIT, cpu);
  805. return -ENODEV;
  806. }
  807. value = (msr_val & ENERGY_UNIT_MASK) >> ENERGY_UNIT_OFFSET;
  808. rp->energy_unit = ENERGY_UNIT_SCALE * 1 << value;
  809. value = (msr_val & POWER_UNIT_MASK) >> POWER_UNIT_OFFSET;
  810. rp->power_unit = (1 << value) * 1000;
  811. value = (msr_val & TIME_UNIT_MASK) >> TIME_UNIT_OFFSET;
  812. rp->time_unit = 1000000 / (1 << value);
  813. pr_debug("Atom package %d energy=%dpJ, time=%dus, power=%duW\n",
  814. rp->id, rp->energy_unit, rp->time_unit, rp->power_unit);
  815. return 0;
  816. }
  817. static void power_limit_irq_save_cpu(void *info)
  818. {
  819. u32 l, h = 0;
  820. struct rapl_package *rp = (struct rapl_package *)info;
  821. /* save the state of PLN irq mask bit before disabling it */
  822. rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
  823. if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED)) {
  824. rp->power_limit_irq = l & PACKAGE_THERM_INT_PLN_ENABLE;
  825. rp->power_limit_irq |= PACKAGE_PLN_INT_SAVED;
  826. }
  827. l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
  828. wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  829. }
  830. /* REVISIT:
  831. * When package power limit is set artificially low by RAPL, LVT
  832. * thermal interrupt for package power limit should be ignored
  833. * since we are not really exceeding the real limit. The intention
  834. * is to avoid excessive interrupts while we are trying to save power.
  835. * A useful feature might be routing the package_power_limit interrupt
  836. * to userspace via eventfd. once we have a usecase, this is simple
  837. * to do by adding an atomic notifier.
  838. */
  839. static void package_power_limit_irq_save(struct rapl_package *rp)
  840. {
  841. if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
  842. return;
  843. smp_call_function_single(rp->lead_cpu, power_limit_irq_save_cpu, rp, 1);
  844. }
  845. /*
  846. * Restore per package power limit interrupt enable state. Called from cpu
  847. * hotplug code on package removal.
  848. */
  849. static void package_power_limit_irq_restore(struct rapl_package *rp)
  850. {
  851. u32 l, h;
  852. if (!boot_cpu_has(X86_FEATURE_PTS) || !boot_cpu_has(X86_FEATURE_PLN))
  853. return;
  854. /* irq enable state not saved, nothing to restore */
  855. if (!(rp->power_limit_irq & PACKAGE_PLN_INT_SAVED))
  856. return;
  857. rdmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, &l, &h);
  858. if (rp->power_limit_irq & PACKAGE_THERM_INT_PLN_ENABLE)
  859. l |= PACKAGE_THERM_INT_PLN_ENABLE;
  860. else
  861. l &= ~PACKAGE_THERM_INT_PLN_ENABLE;
  862. wrmsr_safe(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  863. }
  864. static void set_floor_freq_default(struct rapl_domain *rd, bool mode)
  865. {
  866. int nr_powerlimit = find_nr_power_limit(rd);
  867. /* always enable clamp such that p-state can go below OS requested
  868. * range. power capping priority over guranteed frequency.
  869. */
  870. rapl_write_data_raw(rd, PL1_CLAMP, mode);
  871. /* some domains have pl2 */
  872. if (nr_powerlimit > 1) {
  873. rapl_write_data_raw(rd, PL2_ENABLE, mode);
  874. rapl_write_data_raw(rd, PL2_CLAMP, mode);
  875. }
  876. }
  877. static void set_floor_freq_atom(struct rapl_domain *rd, bool enable)
  878. {
  879. static u32 power_ctrl_orig_val;
  880. u32 mdata;
  881. if (!rapl_defaults->floor_freq_reg_addr) {
  882. pr_err("Invalid floor frequency config register\n");
  883. return;
  884. }
  885. if (!power_ctrl_orig_val)
  886. iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_CR_READ,
  887. rapl_defaults->floor_freq_reg_addr,
  888. &power_ctrl_orig_val);
  889. mdata = power_ctrl_orig_val;
  890. if (enable) {
  891. mdata &= ~(0x7f << 8);
  892. mdata |= 1 << 8;
  893. }
  894. iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_CR_WRITE,
  895. rapl_defaults->floor_freq_reg_addr, mdata);
  896. }
  897. static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
  898. bool to_raw)
  899. {
  900. u64 f, y; /* fraction and exp. used for time unit */
  901. /*
  902. * Special processing based on 2^Y*(1+F/4), refer
  903. * to Intel Software Developer's manual Vol.3B: CH 14.9.3.
  904. */
  905. if (!to_raw) {
  906. f = (value & 0x60) >> 5;
  907. y = value & 0x1f;
  908. value = (1 << y) * (4 + f) * rp->time_unit / 4;
  909. } else {
  910. do_div(value, rp->time_unit);
  911. y = ilog2(value);
  912. f = div64_u64(4 * (value - (1 << y)), 1 << y);
  913. value = (y & 0x1f) | ((f & 0x3) << 5);
  914. }
  915. return value;
  916. }
  917. static u64 rapl_compute_time_window_atom(struct rapl_package *rp, u64 value,
  918. bool to_raw)
  919. {
  920. /*
  921. * Atom time unit encoding is straight forward val * time_unit,
  922. * where time_unit is default to 1 sec. Never 0.
  923. */
  924. if (!to_raw)
  925. return (value) ? value *= rp->time_unit : rp->time_unit;
  926. else
  927. value = div64_u64(value, rp->time_unit);
  928. return value;
  929. }
  930. static const struct rapl_defaults rapl_defaults_core = {
  931. .floor_freq_reg_addr = 0,
  932. .check_unit = rapl_check_unit_core,
  933. .set_floor_freq = set_floor_freq_default,
  934. .compute_time_window = rapl_compute_time_window_core,
  935. };
  936. static const struct rapl_defaults rapl_defaults_hsw_server = {
  937. .check_unit = rapl_check_unit_core,
  938. .set_floor_freq = set_floor_freq_default,
  939. .compute_time_window = rapl_compute_time_window_core,
  940. .dram_domain_energy_unit = 15300,
  941. };
  942. static const struct rapl_defaults rapl_defaults_byt = {
  943. .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_BYT,
  944. .check_unit = rapl_check_unit_atom,
  945. .set_floor_freq = set_floor_freq_atom,
  946. .compute_time_window = rapl_compute_time_window_atom,
  947. };
  948. static const struct rapl_defaults rapl_defaults_tng = {
  949. .floor_freq_reg_addr = IOSF_CPU_POWER_BUDGET_CTL_TNG,
  950. .check_unit = rapl_check_unit_atom,
  951. .set_floor_freq = set_floor_freq_atom,
  952. .compute_time_window = rapl_compute_time_window_atom,
  953. };
  954. static const struct rapl_defaults rapl_defaults_ann = {
  955. .floor_freq_reg_addr = 0,
  956. .check_unit = rapl_check_unit_atom,
  957. .set_floor_freq = NULL,
  958. .compute_time_window = rapl_compute_time_window_atom,
  959. };
  960. static const struct rapl_defaults rapl_defaults_cht = {
  961. .floor_freq_reg_addr = 0,
  962. .check_unit = rapl_check_unit_atom,
  963. .set_floor_freq = NULL,
  964. .compute_time_window = rapl_compute_time_window_atom,
  965. };
  966. #define RAPL_CPU(_model, _ops) { \
  967. .vendor = X86_VENDOR_INTEL, \
  968. .family = 6, \
  969. .model = _model, \
  970. .driver_data = (kernel_ulong_t)&_ops, \
  971. }
  972. static const struct x86_cpu_id rapl_ids[] __initconst = {
  973. RAPL_CPU(INTEL_FAM6_SANDYBRIDGE, rapl_defaults_core),
  974. RAPL_CPU(INTEL_FAM6_SANDYBRIDGE_X, rapl_defaults_core),
  975. RAPL_CPU(INTEL_FAM6_IVYBRIDGE, rapl_defaults_core),
  976. RAPL_CPU(INTEL_FAM6_IVYBRIDGE_X, rapl_defaults_core),
  977. RAPL_CPU(INTEL_FAM6_HASWELL_CORE, rapl_defaults_core),
  978. RAPL_CPU(INTEL_FAM6_HASWELL_ULT, rapl_defaults_core),
  979. RAPL_CPU(INTEL_FAM6_HASWELL_GT3E, rapl_defaults_core),
  980. RAPL_CPU(INTEL_FAM6_HASWELL_X, rapl_defaults_hsw_server),
  981. RAPL_CPU(INTEL_FAM6_BROADWELL_CORE, rapl_defaults_core),
  982. RAPL_CPU(INTEL_FAM6_BROADWELL_GT3E, rapl_defaults_core),
  983. RAPL_CPU(INTEL_FAM6_BROADWELL_XEON_D, rapl_defaults_core),
  984. RAPL_CPU(INTEL_FAM6_BROADWELL_X, rapl_defaults_hsw_server),
  985. RAPL_CPU(INTEL_FAM6_SKYLAKE_DESKTOP, rapl_defaults_core),
  986. RAPL_CPU(INTEL_FAM6_SKYLAKE_MOBILE, rapl_defaults_core),
  987. RAPL_CPU(INTEL_FAM6_SKYLAKE_X, rapl_defaults_hsw_server),
  988. RAPL_CPU(INTEL_FAM6_KABYLAKE_MOBILE, rapl_defaults_core),
  989. RAPL_CPU(INTEL_FAM6_KABYLAKE_DESKTOP, rapl_defaults_core),
  990. RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT, rapl_defaults_byt),
  991. RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT, rapl_defaults_cht),
  992. RAPL_CPU(INTEL_FAM6_ATOM_SILVERMONT_MID,rapl_defaults_tng),
  993. RAPL_CPU(INTEL_FAM6_ATOM_AIRMONT_MID, rapl_defaults_ann),
  994. RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT, rapl_defaults_core),
  995. RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, rapl_defaults_core),
  996. RAPL_CPU(INTEL_FAM6_ATOM_GOLDMONT_X, rapl_defaults_core),
  997. RAPL_CPU(INTEL_FAM6_XEON_PHI_KNL, rapl_defaults_hsw_server),
  998. RAPL_CPU(INTEL_FAM6_XEON_PHI_KNM, rapl_defaults_hsw_server),
  999. {}
  1000. };
  1001. MODULE_DEVICE_TABLE(x86cpu, rapl_ids);
  1002. /* Read once for all raw primitive data for domains */
  1003. static void rapl_update_domain_data(struct rapl_package *rp)
  1004. {
  1005. int dmn, prim;
  1006. u64 val;
  1007. for (dmn = 0; dmn < rp->nr_domains; dmn++) {
  1008. pr_debug("update package %d domain %s data\n", rp->id,
  1009. rp->domains[dmn].name);
  1010. /* exclude non-raw primitives */
  1011. for (prim = 0; prim < NR_RAW_PRIMITIVES; prim++) {
  1012. if (!rapl_read_data_raw(&rp->domains[dmn], prim,
  1013. rpi[prim].unit, &val))
  1014. rp->domains[dmn].rdd.primitives[prim] = val;
  1015. }
  1016. }
  1017. }
  1018. static void rapl_unregister_powercap(void)
  1019. {
  1020. if (platform_rapl_domain) {
  1021. powercap_unregister_zone(control_type,
  1022. &platform_rapl_domain->power_zone);
  1023. kfree(platform_rapl_domain);
  1024. }
  1025. powercap_unregister_control_type(control_type);
  1026. }
  1027. static int rapl_package_register_powercap(struct rapl_package *rp)
  1028. {
  1029. struct rapl_domain *rd;
  1030. char dev_name[17]; /* max domain name = 7 + 1 + 8 for int + 1 for null*/
  1031. struct powercap_zone *power_zone = NULL;
  1032. int nr_pl, ret;;
  1033. /* Update the domain data of the new package */
  1034. rapl_update_domain_data(rp);
  1035. /* first we register package domain as the parent zone*/
  1036. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1037. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  1038. nr_pl = find_nr_power_limit(rd);
  1039. pr_debug("register socket %d package domain %s\n",
  1040. rp->id, rd->name);
  1041. memset(dev_name, 0, sizeof(dev_name));
  1042. snprintf(dev_name, sizeof(dev_name), "%s-%d",
  1043. rd->name, rp->id);
  1044. power_zone = powercap_register_zone(&rd->power_zone,
  1045. control_type,
  1046. dev_name, NULL,
  1047. &zone_ops[rd->id],
  1048. nr_pl,
  1049. &constraint_ops);
  1050. if (IS_ERR(power_zone)) {
  1051. pr_debug("failed to register package, %d\n",
  1052. rp->id);
  1053. return PTR_ERR(power_zone);
  1054. }
  1055. /* track parent zone in per package/socket data */
  1056. rp->power_zone = power_zone;
  1057. /* done, only one package domain per socket */
  1058. break;
  1059. }
  1060. }
  1061. if (!power_zone) {
  1062. pr_err("no package domain found, unknown topology!\n");
  1063. return -ENODEV;
  1064. }
  1065. /* now register domains as children of the socket/package*/
  1066. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1067. if (rd->id == RAPL_DOMAIN_PACKAGE)
  1068. continue;
  1069. /* number of power limits per domain varies */
  1070. nr_pl = find_nr_power_limit(rd);
  1071. power_zone = powercap_register_zone(&rd->power_zone,
  1072. control_type, rd->name,
  1073. rp->power_zone,
  1074. &zone_ops[rd->id], nr_pl,
  1075. &constraint_ops);
  1076. if (IS_ERR(power_zone)) {
  1077. pr_debug("failed to register power_zone, %d:%s:%s\n",
  1078. rp->id, rd->name, dev_name);
  1079. ret = PTR_ERR(power_zone);
  1080. goto err_cleanup;
  1081. }
  1082. }
  1083. return 0;
  1084. err_cleanup:
  1085. /*
  1086. * Clean up previously initialized domains within the package if we
  1087. * failed after the first domain setup.
  1088. */
  1089. while (--rd >= rp->domains) {
  1090. pr_debug("unregister package %d domain %s\n", rp->id, rd->name);
  1091. powercap_unregister_zone(control_type, &rd->power_zone);
  1092. }
  1093. return ret;
  1094. }
  1095. static int __init rapl_register_psys(void)
  1096. {
  1097. struct rapl_domain *rd;
  1098. struct powercap_zone *power_zone;
  1099. u64 val;
  1100. if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_ENERGY_STATUS, &val) || !val)
  1101. return -ENODEV;
  1102. if (rdmsrl_safe_on_cpu(0, MSR_PLATFORM_POWER_LIMIT, &val) || !val)
  1103. return -ENODEV;
  1104. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  1105. if (!rd)
  1106. return -ENOMEM;
  1107. rd->name = rapl_domain_names[RAPL_DOMAIN_PLATFORM];
  1108. rd->id = RAPL_DOMAIN_PLATFORM;
  1109. rd->msrs[0] = MSR_PLATFORM_POWER_LIMIT;
  1110. rd->msrs[1] = MSR_PLATFORM_ENERGY_STATUS;
  1111. rd->rpl[0].prim_id = PL1_ENABLE;
  1112. rd->rpl[0].name = pl1_name;
  1113. rd->rpl[1].prim_id = PL2_ENABLE;
  1114. rd->rpl[1].name = pl2_name;
  1115. rd->rp = find_package_by_id(0);
  1116. power_zone = powercap_register_zone(&rd->power_zone, control_type,
  1117. "psys", NULL,
  1118. &zone_ops[RAPL_DOMAIN_PLATFORM],
  1119. 2, &constraint_ops);
  1120. if (IS_ERR(power_zone)) {
  1121. kfree(rd);
  1122. return PTR_ERR(power_zone);
  1123. }
  1124. platform_rapl_domain = rd;
  1125. return 0;
  1126. }
  1127. static int __init rapl_register_powercap(void)
  1128. {
  1129. control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
  1130. if (IS_ERR(control_type)) {
  1131. pr_debug("failed to register powercap control_type.\n");
  1132. return PTR_ERR(control_type);
  1133. }
  1134. return 0;
  1135. }
  1136. static int rapl_check_domain(int cpu, int domain)
  1137. {
  1138. unsigned msr;
  1139. u64 val = 0;
  1140. switch (domain) {
  1141. case RAPL_DOMAIN_PACKAGE:
  1142. msr = MSR_PKG_ENERGY_STATUS;
  1143. break;
  1144. case RAPL_DOMAIN_PP0:
  1145. msr = MSR_PP0_ENERGY_STATUS;
  1146. break;
  1147. case RAPL_DOMAIN_PP1:
  1148. msr = MSR_PP1_ENERGY_STATUS;
  1149. break;
  1150. case RAPL_DOMAIN_DRAM:
  1151. msr = MSR_DRAM_ENERGY_STATUS;
  1152. break;
  1153. case RAPL_DOMAIN_PLATFORM:
  1154. /* PSYS(PLATFORM) is not a CPU domain, so avoid printng error */
  1155. return -EINVAL;
  1156. default:
  1157. pr_err("invalid domain id %d\n", domain);
  1158. return -EINVAL;
  1159. }
  1160. /* make sure domain counters are available and contains non-zero
  1161. * values, otherwise skip it.
  1162. */
  1163. if (rdmsrl_safe_on_cpu(cpu, msr, &val) || !val)
  1164. return -ENODEV;
  1165. return 0;
  1166. }
  1167. /*
  1168. * Check if power limits are available. Two cases when they are not available:
  1169. * 1. Locked by BIOS, in this case we still provide read-only access so that
  1170. * users can see what limit is set by the BIOS.
  1171. * 2. Some CPUs make some domains monitoring only which means PLx MSRs may not
  1172. * exist at all. In this case, we do not show the contraints in powercap.
  1173. *
  1174. * Called after domains are detected and initialized.
  1175. */
  1176. static void rapl_detect_powerlimit(struct rapl_domain *rd)
  1177. {
  1178. u64 val64;
  1179. int i;
  1180. /* check if the domain is locked by BIOS, ignore if MSR doesn't exist */
  1181. if (!rapl_read_data_raw(rd, FW_LOCK, false, &val64)) {
  1182. if (val64) {
  1183. pr_info("RAPL package %d domain %s locked by BIOS\n",
  1184. rd->rp->id, rd->name);
  1185. rd->state |= DOMAIN_STATE_BIOS_LOCKED;
  1186. }
  1187. }
  1188. /* check if power limit MSRs exists, otherwise domain is monitoring only */
  1189. for (i = 0; i < NR_POWER_LIMITS; i++) {
  1190. int prim = rd->rpl[i].prim_id;
  1191. if (rapl_read_data_raw(rd, prim, false, &val64))
  1192. rd->rpl[i].name = NULL;
  1193. }
  1194. }
  1195. /* Detect active and valid domains for the given CPU, caller must
  1196. * ensure the CPU belongs to the targeted package and CPU hotlug is disabled.
  1197. */
  1198. static int rapl_detect_domains(struct rapl_package *rp, int cpu)
  1199. {
  1200. struct rapl_domain *rd;
  1201. int i;
  1202. for (i = 0; i < RAPL_DOMAIN_MAX; i++) {
  1203. /* use physical package id to read counters */
  1204. if (!rapl_check_domain(cpu, i)) {
  1205. rp->domain_map |= 1 << i;
  1206. pr_info("Found RAPL domain %s\n", rapl_domain_names[i]);
  1207. }
  1208. }
  1209. rp->nr_domains = bitmap_weight(&rp->domain_map, RAPL_DOMAIN_MAX);
  1210. if (!rp->nr_domains) {
  1211. pr_debug("no valid rapl domains found in package %d\n", rp->id);
  1212. return -ENODEV;
  1213. }
  1214. pr_debug("found %d domains on package %d\n", rp->nr_domains, rp->id);
  1215. rp->domains = kcalloc(rp->nr_domains + 1, sizeof(struct rapl_domain),
  1216. GFP_KERNEL);
  1217. if (!rp->domains)
  1218. return -ENOMEM;
  1219. rapl_init_domains(rp);
  1220. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++)
  1221. rapl_detect_powerlimit(rd);
  1222. return 0;
  1223. }
  1224. /* called from CPU hotplug notifier, hotplug lock held */
  1225. static void rapl_remove_package(struct rapl_package *rp)
  1226. {
  1227. struct rapl_domain *rd, *rd_package = NULL;
  1228. package_power_limit_irq_restore(rp);
  1229. for (rd = rp->domains; rd < rp->domains + rp->nr_domains; rd++) {
  1230. rapl_write_data_raw(rd, PL1_ENABLE, 0);
  1231. rapl_write_data_raw(rd, PL1_CLAMP, 0);
  1232. if (find_nr_power_limit(rd) > 1) {
  1233. rapl_write_data_raw(rd, PL2_ENABLE, 0);
  1234. rapl_write_data_raw(rd, PL2_CLAMP, 0);
  1235. }
  1236. if (rd->id == RAPL_DOMAIN_PACKAGE) {
  1237. rd_package = rd;
  1238. continue;
  1239. }
  1240. pr_debug("remove package, undo power limit on %d: %s\n",
  1241. rp->id, rd->name);
  1242. powercap_unregister_zone(control_type, &rd->power_zone);
  1243. }
  1244. /* do parent zone last */
  1245. powercap_unregister_zone(control_type, &rd_package->power_zone);
  1246. list_del(&rp->plist);
  1247. kfree(rp);
  1248. }
  1249. /* called from CPU hotplug notifier, hotplug lock held */
  1250. static struct rapl_package *rapl_add_package(int cpu, int pkgid)
  1251. {
  1252. struct rapl_package *rp;
  1253. int ret;
  1254. rp = kzalloc(sizeof(struct rapl_package), GFP_KERNEL);
  1255. if (!rp)
  1256. return ERR_PTR(-ENOMEM);
  1257. /* add the new package to the list */
  1258. rp->id = pkgid;
  1259. rp->lead_cpu = cpu;
  1260. /* check if the package contains valid domains */
  1261. if (rapl_detect_domains(rp, cpu) ||
  1262. rapl_defaults->check_unit(rp, cpu)) {
  1263. ret = -ENODEV;
  1264. goto err_free_package;
  1265. }
  1266. ret = rapl_package_register_powercap(rp);
  1267. if (!ret) {
  1268. INIT_LIST_HEAD(&rp->plist);
  1269. list_add(&rp->plist, &rapl_packages);
  1270. return rp;
  1271. }
  1272. err_free_package:
  1273. kfree(rp->domains);
  1274. kfree(rp);
  1275. return ERR_PTR(ret);
  1276. }
  1277. /* Handles CPU hotplug on multi-socket systems.
  1278. * If a CPU goes online as the first CPU of the physical package
  1279. * we add the RAPL package to the system. Similarly, when the last
  1280. * CPU of the package is removed, we remove the RAPL package and its
  1281. * associated domains. Cooling devices are handled accordingly at
  1282. * per-domain level.
  1283. */
  1284. static int rapl_cpu_online(unsigned int cpu)
  1285. {
  1286. int pkgid = topology_physical_package_id(cpu);
  1287. struct rapl_package *rp;
  1288. rp = find_package_by_id(pkgid);
  1289. if (!rp) {
  1290. rp = rapl_add_package(cpu, pkgid);
  1291. if (IS_ERR(rp))
  1292. return PTR_ERR(rp);
  1293. }
  1294. cpumask_set_cpu(cpu, &rp->cpumask);
  1295. return 0;
  1296. }
  1297. static int rapl_cpu_down_prep(unsigned int cpu)
  1298. {
  1299. int pkgid = topology_physical_package_id(cpu);
  1300. struct rapl_package *rp;
  1301. int lead_cpu;
  1302. rp = find_package_by_id(pkgid);
  1303. if (!rp)
  1304. return 0;
  1305. cpumask_clear_cpu(cpu, &rp->cpumask);
  1306. lead_cpu = cpumask_first(&rp->cpumask);
  1307. if (lead_cpu >= nr_cpu_ids)
  1308. rapl_remove_package(rp);
  1309. else if (rp->lead_cpu == cpu)
  1310. rp->lead_cpu = lead_cpu;
  1311. return 0;
  1312. }
  1313. static enum cpuhp_state pcap_rapl_online;
  1314. static int __init rapl_init(void)
  1315. {
  1316. const struct x86_cpu_id *id;
  1317. int ret;
  1318. id = x86_match_cpu(rapl_ids);
  1319. if (!id) {
  1320. pr_err("driver does not support CPU family %d model %d\n",
  1321. boot_cpu_data.x86, boot_cpu_data.x86_model);
  1322. return -ENODEV;
  1323. }
  1324. rapl_defaults = (struct rapl_defaults *)id->driver_data;
  1325. ret = rapl_register_powercap();
  1326. if (ret)
  1327. return ret;
  1328. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powercap/rapl:online",
  1329. rapl_cpu_online, rapl_cpu_down_prep);
  1330. if (ret < 0)
  1331. goto err_unreg;
  1332. pcap_rapl_online = ret;
  1333. /* Don't bail out if PSys is not supported */
  1334. rapl_register_psys();
  1335. return 0;
  1336. err_unreg:
  1337. rapl_unregister_powercap();
  1338. return ret;
  1339. }
  1340. static void __exit rapl_exit(void)
  1341. {
  1342. cpuhp_remove_state(pcap_rapl_online);
  1343. rapl_unregister_powercap();
  1344. }
  1345. module_init(rapl_init);
  1346. module_exit(rapl_exit);
  1347. MODULE_DESCRIPTION("Driver for Intel RAPL (Running Average Power Limit)");
  1348. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
  1349. MODULE_LICENSE("GPL v2");