xref: /freebsd/sys/sys/cpu.h (revision 302d3a21ad9aae26a55aa8d8396c69f8ca8ac33e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2007 Nate Lawson (SDG)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_CPU_H_
30 #define _SYS_CPU_H_
31 
32 #include <sys/_eventhandler.h>
33 
34 /*
35  * CPU device support.
36  */
37 
38 enum {
39 	CPU_IVAR_PCPU = BUS_IVARS_PRIVATE,
40 	CPU_IVAR_NOMINAL_MHZ,
41 	CPU_IVAR_CPUID_SIZE,
42 	CPU_IVAR_CPUID
43 };
44 
45 static __inline struct pcpu *
cpu_get_pcpu(device_t dev)46 cpu_get_pcpu(device_t dev)
47 {
48 	uintptr_t v = 0;
49 
50 	BUS_READ_IVAR(device_get_parent(dev), dev, CPU_IVAR_PCPU, &v);
51 	return ((struct pcpu *)v);
52 }
53 
54 /*
55  * Assumes the parent bus sets the CPU_IVAR_PCPU instance variable, which most
56  * CPU buses do.  A panic will ensue if not the case.  Calling devices should
57  * check for that condition (most probably in their attach function).
58  */
59 static __inline u_int
cpu_get_pcpuid(device_t dev)60 cpu_get_pcpuid(device_t dev)
61 {
62 	return (cpu_get_pcpu(dev)->pc_cpuid);
63 }
64 
65 static __inline int32_t
cpu_get_nominal_mhz(device_t dev)66 cpu_get_nominal_mhz(device_t dev)
67 {
68 	uintptr_t v = 0;
69 
70 	if (BUS_READ_IVAR(device_get_parent(dev), dev,
71 	    CPU_IVAR_NOMINAL_MHZ, &v) != 0)
72 		return (-1);
73 	return ((int32_t)v);
74 }
75 
76 static __inline const uint32_t *
cpu_get_cpuid(device_t dev,size_t * count)77 cpu_get_cpuid(device_t dev, size_t *count)
78 {
79 	uintptr_t v = 0;
80 
81 	if (BUS_READ_IVAR(device_get_parent(dev), dev,
82 	    CPU_IVAR_CPUID_SIZE, &v) != 0)
83 		return (NULL);
84 	*count = (size_t)v;
85 
86 	if (BUS_READ_IVAR(device_get_parent(dev), dev,
87 	    CPU_IVAR_CPUID, &v) != 0)
88 		return (NULL);
89 	return ((const uint32_t *)v);
90 }
91 
92 /*
93  * CPU frequency control interface.
94  */
95 
96 /* Each driver's CPU frequency setting is exported in this format. */
97 struct cf_setting {
98 	int	freq;	/* CPU clock in Mhz or 100ths of a percent. */
99 	int	volts;	/* Voltage in mV. */
100 	int	power;	/* Power consumed in mW. */
101 	int	lat;	/* Transition latency in us. */
102 	device_t dev;	/* Driver providing this setting. */
103 	int	spec[4];/* Driver-specific storage for non-standard info. */
104 };
105 
106 /* Maximum number of settings a given driver can have. */
107 #define MAX_SETTINGS		256
108 
109 /* A combination of settings is a level. */
110 struct cf_level {
111 	struct cf_setting	total_set;
112 	struct cf_setting	abs_set;
113 	struct cf_setting	rel_set[MAX_SETTINGS];
114 	int			rel_count;
115 	TAILQ_ENTRY(cf_level)	link;
116 };
117 
118 TAILQ_HEAD(cf_level_lst, cf_level);
119 
120 /* Drivers should set all unknown values to this. */
121 #define CPUFREQ_VAL_UNKNOWN	(-1)
122 
123 /*
124  * Every driver offers a type of CPU control.  Absolute levels are mutually
125  * exclusive while relative levels modify the current absolute level.  There
126  * may be multiple absolute and relative drivers available on a given
127  * system.
128  *
129  * For example, consider a system with two absolute drivers that provide
130  * frequency settings of 100, 200 and 300, 400 and a relative driver that
131  * provides settings of 50%, 100%.  The cpufreq core would export frequency
132  * levels of 50, 100, 150, 200, 300, 400.
133  *
134  * The "info only" flag signifies that settings returned by
135  * CPUFREQ_DRV_SETTINGS cannot be passed to the CPUFREQ_DRV_SET method and
136  * are only informational.  This is for some drivers that can return
137  * information about settings but rely on another machine-dependent driver
138  * for actually performing the frequency transition (e.g., ACPI performance
139  * states of type "functional fixed hardware.")
140  *
141  * The "uncached" flag tells CPUFREQ_DRV_GET to try obtaining the real
142  * instantaneous frequency from the underlying hardware regardless of cached
143  * state. It is probably a bug to not combine this with "info only"
144  */
145 #define CPUFREQ_TYPE_MASK	0xffff
146 #define CPUFREQ_TYPE_RELATIVE	(1 << 0)
147 #define CPUFREQ_TYPE_ABSOLUTE	(1 << 1)
148 #define CPUFREQ_FLAG_INFO_ONLY	(1 << 16)
149 #define CPUFREQ_FLAG_UNCACHED	(1 << 17)
150 
151 /*
152  * When setting a level, the caller indicates the priority of this request.
153  * Priorities determine, among other things, whether a level can be
154  * overridden by other callers.  For example, if the user sets a level but
155  * the system thermal driver needs to override it for emergency cooling,
156  * the driver would use a higher priority.  Once the event has passed, the
157  * driver would call cpufreq to resume any previous level.
158  */
159 #define CPUFREQ_PRIO_HIGHEST	1000000
160 #define CPUFREQ_PRIO_KERN	1000
161 #define CPUFREQ_PRIO_USER	100
162 #define CPUFREQ_PRIO_LOWEST	0
163 
164 /*
165  * Register and unregister a driver with the cpufreq core.  Once a driver
166  * is registered, it must support calls to its CPUFREQ_GET, CPUFREQ_GET_LEVEL,
167  * and CPUFREQ_SET methods.  It must also unregister before returning from
168  * its DEVICE_DETACH method.
169  */
170 int	cpufreq_register(device_t dev);
171 int	cpufreq_unregister(device_t dev);
172 
173 /*
174  * Notify the cpufreq core that the number of or values for settings have
175  * changed.
176  */
177 int	cpufreq_settings_changed(device_t dev);
178 
179 /*
180  * Eventhandlers that are called before and after a change in frequency.
181  * The new level and the result of the change (0 is success) is passed in.
182  * If the driver wishes to revoke the change from cpufreq_pre_change, it
183  * stores a non-zero error code in the result parameter and the change will
184  * not be made.  If the post-change eventhandler gets a non-zero result,
185  * no change was made and the previous level remains in effect.  If a change
186  * is revoked, the post-change eventhandler is still called with the error
187  * value supplied by the revoking driver.  This gives listeners who cached
188  * some data in preparation for a level change a chance to clean up.
189  */
190 typedef void (*cpufreq_pre_notify_fn)(void *, const struct cf_level *, int *);
191 typedef void (*cpufreq_post_notify_fn)(void *, const struct cf_level *, int);
192 EVENTHANDLER_DECLARE(cpufreq_pre_change, cpufreq_pre_notify_fn);
193 EVENTHANDLER_DECLARE(cpufreq_post_change, cpufreq_post_notify_fn);
194 
195 /*
196  * Eventhandler called when the available list of levels changed.
197  * The unit number of the device (i.e. "cpufreq0") whose levels changed
198  * is provided so the listener can retrieve the new list of levels.
199  */
200 typedef void (*cpufreq_levels_notify_fn)(void *, int);
201 EVENTHANDLER_DECLARE(cpufreq_levels_changed, cpufreq_levels_notify_fn);
202 
203 /* Allow values to be +/- a bit since sometimes we have to estimate. */
204 #define CPUFREQ_CMP(x, y)	(abs((x) - (y)) < 25)
205 
206 /*
207  * Machine-dependent functions.
208  */
209 
210 /* Estimate the current clock rate for the given CPU id. */
211 int	cpu_est_clockrate(int cpu_id, uint64_t *rate);
212 
213 #endif /* !_SYS_CPU_H_ */
214