1 /* $NetBSD: cpu_speedctl.c,v 1.3 2020/07/06 10:58:06 rin Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Michael Lorenz
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cpu_speedctl.c,v 1.3 2020/07/06 10:58:06 rin Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/types.h>
36
37 #include <powerpc/psl.h>
38 #include <powerpc/spr.h>
39 #include <powerpc/oea/cpufeat.h>
40 #include <powerpc/oea/spr.h>
41
42 #include <sys/sysctl.h>
43 #include <dev/ofw/openfirm.h>
44
45 void init_scom_speedctl(void);
46 static int scom_speedctl = 0;
47 static uint32_t scom_speeds[2];
48 static uint32_t scom_reg[2];
49 static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
50 static int sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
51 static int sysctl_cpuspeed_available(SYSCTLFN_ARGS);
52
53 void
init_scom_speedctl(void)54 init_scom_speedctl(void)
55 {
56 int node;
57 const struct sysctlnode *sysctl_node, *me, *freq;
58
59 /* do this only once */
60 if (scom_speedctl != 0) return;
61 scom_speedctl = 1;
62
63 node = OF_finddevice("/cpus/@0");
64 if (OF_getprop(node, "power-mode-data", scom_reg, 8) != 8)
65 return;
66 if (OF_getprop(node, "clock-frequency", scom_speeds, 4) != 4)
67 return;
68 scom_speeds[0] = scom_speeds[0] / 1000000; /* Hz -> MHz */
69 scom_speeds[1] = scom_speeds[0] / 2;
70
71 sysctl_node = NULL;
72
73 if (sysctl_createv(NULL, 0, NULL,
74 &me,
75 CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
76 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
77 printf("couldn't create 'cpu' node\n");
78
79 if (sysctl_createv(NULL, 0, NULL,
80 &freq,
81 CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
82 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
83 printf("couldn't create 'frequency' node\n");
84
85 if (sysctl_createv(NULL, 0, NULL,
86 &sysctl_node,
87 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
88 CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
89 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
90 CTL_CREATE, CTL_EOL) == 0) {
91 } else
92 printf("couldn't create 'target' node\n");
93
94 if (sysctl_createv(NULL, 0, NULL,
95 &sysctl_node,
96 CTLFLAG_READWRITE,
97 CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
98 1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
99 CTL_CREATE, CTL_EOL) == 0) {
100 } else
101 printf("couldn't create 'current' node\n");
102
103 if (sysctl_createv(NULL, 0, NULL,
104 &sysctl_node,
105 CTLFLAG_READWRITE,
106 CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
107 2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
108 CTL_CREATE, CTL_EOL) == 0) {
109 } else
110 printf("couldn't create 'available' node\n");
111 }
112
113 static int
sysctl_cpuspeed_temp(SYSCTLFN_ARGS)114 sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
115 {
116 struct sysctlnode node = *rnode;
117 int speed, nspeed = -1, mhz;
118
119 speed = (scom_read(SCOM_PSR) >> 56) & 3;
120 if (speed > 1) speed = 1;
121 mhz = scom_speeds[speed];
122 node.sysctl_data = &mhz;
123
124 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
125 int new_reg;
126
127 new_reg = *(int *)node.sysctl_data;
128 if (new_reg == scom_speeds[0]) {
129 nspeed = 0;
130 } else if (new_reg == scom_speeds[1]) {
131 nspeed = 1;
132 } else {
133 printf("%s: new_reg %d\n", __func__, new_reg);
134 return EINVAL;
135 }
136 if (nspeed != speed) {
137 volatile uint64_t junk;
138 scom_write(SCOM_PCR, 0x80000000);
139 scom_write(SCOM_PCR, scom_reg[nspeed]);
140 junk = scom_read(SCOM_PSR);
141 __USE(junk);
142 }
143 return 0;
144 }
145 return EINVAL;
146 }
147
148 static int
sysctl_cpuspeed_cur(SYSCTLFN_ARGS)149 sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
150 {
151 struct sysctlnode node = *rnode;
152 uint64_t speed;
153 int mhz;
154
155 speed = (scom_read(SCOM_PSR) >> 56) & 3;
156 if (speed > 1) speed = 1;
157
158 mhz = scom_speeds[speed];
159 node.sysctl_data = &mhz;
160 return sysctl_lookup(SYSCTLFN_CALL(&node));
161 }
162
163 static int
sysctl_cpuspeed_available(SYSCTLFN_ARGS)164 sysctl_cpuspeed_available(SYSCTLFN_ARGS)
165 {
166 struct sysctlnode node = *rnode;
167 char buf[128];
168
169 snprintf(buf, 128, "%d %d", scom_speeds[0], scom_speeds[1]);
170 node.sysctl_data = buf;
171 return(sysctl_lookup(SYSCTLFN_CALL(&node)));
172 }
173
174 SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
175 {
176
177 sysctl_createv(NULL, 0, NULL, NULL,
178 CTLFLAG_PERMANENT,
179 CTLTYPE_NODE, "machdep", NULL,
180 NULL, 0, NULL, 0,
181 CTL_MACHDEP, CTL_EOL);
182 }
183