xref: /linux-6.15/include/linux/intel_rapl.h (revision 9050a9cd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  Data types and headers for RAPL support
4  *
5  *  Copyright (C) 2019  Intel Corporation.
6  *
7  *  Author: Zhang Rui <[email protected]>
8  */
9 
10 #ifndef __INTEL_RAPL_H__
11 #define __INTEL_RAPL_H__
12 
13 #include <linux/types.h>
14 #include <linux/powercap.h>
15 #include <linux/cpuhotplug.h>
16 
17 enum rapl_domain_type {
18 	RAPL_DOMAIN_PACKAGE,	/* entire package/socket */
19 	RAPL_DOMAIN_PP0,	/* core power plane */
20 	RAPL_DOMAIN_PP1,	/* graphics uncore */
21 	RAPL_DOMAIN_DRAM,	/* DRAM control_type */
22 	RAPL_DOMAIN_PLATFORM,	/* PSys control_type */
23 	RAPL_DOMAIN_MAX,
24 };
25 
26 enum rapl_domain_reg_id {
27 	RAPL_DOMAIN_REG_LIMIT,
28 	RAPL_DOMAIN_REG_STATUS,
29 	RAPL_DOMAIN_REG_PERF,
30 	RAPL_DOMAIN_REG_POLICY,
31 	RAPL_DOMAIN_REG_INFO,
32 	RAPL_DOMAIN_REG_PL4,
33 	RAPL_DOMAIN_REG_UNIT,
34 	RAPL_DOMAIN_REG_MAX,
35 };
36 
37 struct rapl_domain;
38 
39 enum rapl_primitives {
40 	POWER_LIMIT1,
41 	POWER_LIMIT2,
42 	POWER_LIMIT4,
43 	ENERGY_COUNTER,
44 	FW_LOCK,
45 
46 	PL1_ENABLE,		/* power limit 1, aka long term */
47 	PL1_CLAMP,		/* allow frequency to go below OS request */
48 	PL2_ENABLE,		/* power limit 2, aka short term, instantaneous */
49 	PL2_CLAMP,
50 	PL4_ENABLE,		/* power limit 4, aka max peak power */
51 
52 	TIME_WINDOW1,		/* long term */
53 	TIME_WINDOW2,		/* short term */
54 	THERMAL_SPEC_POWER,
55 	MAX_POWER,
56 
57 	MIN_POWER,
58 	MAX_TIME_WINDOW,
59 	THROTTLED_TIME,
60 	PRIORITY_LEVEL,
61 
62 	PSYS_POWER_LIMIT1,
63 	PSYS_POWER_LIMIT2,
64 	PSYS_PL1_ENABLE,
65 	PSYS_PL2_ENABLE,
66 	PSYS_TIME_WINDOW1,
67 	PSYS_TIME_WINDOW2,
68 	/* below are not raw primitive data */
69 	AVERAGE_POWER,
70 	NR_RAPL_PRIMITIVES,
71 };
72 
73 struct rapl_domain_data {
74 	u64 primitives[NR_RAPL_PRIMITIVES];
75 	unsigned long timestamp;
76 };
77 
78 #define NR_POWER_LIMITS	(POWER_LIMIT4 + 1)
79 
80 struct rapl_power_limit {
81 	struct powercap_zone_constraint *constraint;
82 	struct rapl_domain *domain;
83 	const char *name;
84 	u64 last_power_limit;
85 };
86 
87 struct rapl_package;
88 
89 #define RAPL_DOMAIN_NAME_LENGTH 16
90 
91 struct rapl_domain {
92 	char name[RAPL_DOMAIN_NAME_LENGTH];
93 	enum rapl_domain_type id;
94 	u64 regs[RAPL_DOMAIN_REG_MAX];
95 	struct powercap_zone power_zone;
96 	struct rapl_domain_data rdd;
97 	struct rapl_power_limit rpl[NR_POWER_LIMITS];
98 	u64 attr_map;		/* track capabilities */
99 	unsigned int state;
100 	unsigned int power_unit;
101 	unsigned int energy_unit;
102 	unsigned int time_unit;
103 	struct rapl_package *rp;
104 };
105 
106 struct reg_action {
107 	u64 reg;
108 	u64 mask;
109 	u64 value;
110 	int err;
111 };
112 
113 /**
114  * struct rapl_if_priv: private data for different RAPL interfaces
115  * @control_type:		Each RAPL interface must have its own powercap
116  *				control type.
117  * @platform_rapl_domain:	Optional. Some RAPL interface may have platform
118  *				level RAPL control.
119  * @pcap_rapl_online:		CPU hotplug state for each RAPL interface.
120  * @reg_unit:			Register for getting energy/power/time unit.
121  * @regs:			Register sets for different RAPL Domains.
122  * @limits:			Number of power limits supported by each domain.
123  * @read_raw:			Callback for reading RAPL interface specific
124  *				registers.
125  * @write_raw:			Callback for writing RAPL interface specific
126  *				registers.
127  * @defaults:			internal pointer to interface default settings
128  * @rpi:			internal pointer to interface primitive info
129  */
130 struct rapl_if_priv {
131 	struct powercap_control_type *control_type;
132 	enum cpuhp_state pcap_rapl_online;
133 	u64 reg_unit;
134 	u64 regs[RAPL_DOMAIN_MAX][RAPL_DOMAIN_REG_MAX];
135 	int limits[RAPL_DOMAIN_MAX];
136 	int (*read_raw)(int cpu, struct reg_action *ra);
137 	int (*write_raw)(int cpu, struct reg_action *ra);
138 	void *defaults;
139 	void *rpi;
140 };
141 
142 /* maximum rapl package domain name: package-%d-die-%d */
143 #define PACKAGE_DOMAIN_NAME_LENGTH 30
144 
145 struct rapl_package {
146 	unsigned int id;	/* logical die id, equals physical 1-die systems */
147 	unsigned int nr_domains;
148 	unsigned long domain_map;	/* bit map of active domains */
149 	struct rapl_domain *domains;	/* array of domains, sized at runtime */
150 	struct powercap_zone *power_zone;	/* keep track of parent zone */
151 	unsigned long power_limit_irq;	/* keep track of package power limit
152 					 * notify interrupt enable status.
153 					 */
154 	struct list_head plist;
155 	int lead_cpu;		/* one active cpu per package for access */
156 	/* Track active cpus */
157 	struct cpumask cpumask;
158 	char name[PACKAGE_DOMAIN_NAME_LENGTH];
159 	struct rapl_if_priv *priv;
160 };
161 
162 struct rapl_package *rapl_find_package_domain(int cpu, struct rapl_if_priv *priv);
163 struct rapl_package *rapl_add_package(int cpu, struct rapl_if_priv *priv);
164 void rapl_remove_package(struct rapl_package *rp);
165 
166 #endif /* __INTEL_RAPL_H__ */
167