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