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