xref: /linux-6.15/include/linux/intel_rapl.h (revision bf44b901)
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 	FW_HIGH_LOCK,
46 
47 	PL1_ENABLE,		/* power limit 1, aka long term */
48 	PL1_CLAMP,		/* allow frequency to go below OS request */
49 	PL2_ENABLE,		/* power limit 2, aka short term, instantaneous */
50 	PL2_CLAMP,
51 	PL4_ENABLE,		/* power limit 4, aka max peak power */
52 
53 	TIME_WINDOW1,		/* long term */
54 	TIME_WINDOW2,		/* short term */
55 	THERMAL_SPEC_POWER,
56 	MAX_POWER,
57 
58 	MIN_POWER,
59 	MAX_TIME_WINDOW,
60 	THROTTLED_TIME,
61 	PRIORITY_LEVEL,
62 
63 	PSYS_POWER_LIMIT1,
64 	PSYS_POWER_LIMIT2,
65 	PSYS_PL1_ENABLE,
66 	PSYS_PL2_ENABLE,
67 	PSYS_TIME_WINDOW1,
68 	PSYS_TIME_WINDOW2,
69 	/* below are not raw primitive data */
70 	AVERAGE_POWER,
71 	NR_RAPL_PRIMITIVES,
72 };
73 
74 struct rapl_domain_data {
75 	u64 primitives[NR_RAPL_PRIMITIVES];
76 	unsigned long timestamp;
77 };
78 
79 #define NR_POWER_LIMITS	(POWER_LIMIT4 + 1)
80 
81 struct rapl_power_limit {
82 	struct powercap_zone_constraint *constraint;
83 	struct rapl_domain *domain;
84 	const char *name;
85 	bool locked;
86 	u64 last_power_limit;
87 };
88 
89 struct rapl_package;
90 
91 #define RAPL_DOMAIN_NAME_LENGTH 16
92 
93 struct rapl_domain {
94 	char name[RAPL_DOMAIN_NAME_LENGTH];
95 	enum rapl_domain_type id;
96 	u64 regs[RAPL_DOMAIN_REG_MAX];
97 	struct powercap_zone power_zone;
98 	struct rapl_domain_data rdd;
99 	struct rapl_power_limit rpl[NR_POWER_LIMITS];
100 	u64 attr_map;		/* track capabilities */
101 	unsigned int state;
102 	unsigned int power_unit;
103 	unsigned int energy_unit;
104 	unsigned int time_unit;
105 	struct rapl_package *rp;
106 };
107 
108 struct reg_action {
109 	u64 reg;
110 	u64 mask;
111 	u64 value;
112 	int err;
113 };
114 
115 /**
116  * struct rapl_if_priv: private data for different RAPL interfaces
117  * @control_type:		Each RAPL interface must have its own powercap
118  *				control type.
119  * @platform_rapl_domain:	Optional. Some RAPL interface may have platform
120  *				level RAPL control.
121  * @pcap_rapl_online:		CPU hotplug state for each RAPL interface.
122  * @reg_unit:			Register for getting energy/power/time unit.
123  * @regs:			Register sets for different RAPL Domains.
124  * @limits:			Number of power limits supported by each domain.
125  * @read_raw:			Callback for reading RAPL interface specific
126  *				registers.
127  * @write_raw:			Callback for writing RAPL interface specific
128  *				registers.
129  * @defaults:			internal pointer to interface default settings
130  * @rpi:			internal pointer to interface primitive info
131  */
132 struct rapl_if_priv {
133 	struct powercap_control_type *control_type;
134 	enum cpuhp_state pcap_rapl_online;
135 	u64 reg_unit;
136 	u64 regs[RAPL_DOMAIN_MAX][RAPL_DOMAIN_REG_MAX];
137 	int limits[RAPL_DOMAIN_MAX];
138 	int (*read_raw)(int id, struct reg_action *ra);
139 	int (*write_raw)(int id, struct reg_action *ra);
140 	void *defaults;
141 	void *rpi;
142 };
143 
144 /* maximum rapl package domain name: package-%d-die-%d */
145 #define PACKAGE_DOMAIN_NAME_LENGTH 30
146 
147 struct rapl_package {
148 	unsigned int id;	/* logical die id, equals physical 1-die systems */
149 	unsigned int nr_domains;
150 	unsigned long domain_map;	/* bit map of active domains */
151 	struct rapl_domain *domains;	/* array of domains, sized at runtime */
152 	struct powercap_zone *power_zone;	/* keep track of parent zone */
153 	unsigned long power_limit_irq;	/* keep track of package power limit
154 					 * notify interrupt enable status.
155 					 */
156 	struct list_head plist;
157 	int lead_cpu;		/* one active cpu per package for access */
158 	/* Track active cpus */
159 	struct cpumask cpumask;
160 	char name[PACKAGE_DOMAIN_NAME_LENGTH];
161 	struct rapl_if_priv *priv;
162 };
163 
164 struct rapl_package *rapl_find_package_domain(int id, struct rapl_if_priv *priv, bool id_is_cpu);
165 struct rapl_package *rapl_add_package(int id, struct rapl_if_priv *priv, bool id_is_cpu);
166 void rapl_remove_package(struct rapl_package *rp);
167 
168 #endif /* __INTEL_RAPL_H__ */
169