cpu_speedctl.c revision 1.1 1 /* $NetBSD: cpu_speedctl.c,v 1.1 2018/06/01 18:06:58 macallan 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 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: cpu_speedctl.c,v 1.1 2018/06/01 18:06:58 macallan Exp $");
30
31 #include "opt_ppcparam.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/types.h>
37
38 #include <powerpc/psl.h>
39 #include <powerpc/spr.h>
40 #include <powerpc/oea/cpufeat.h>
41 #include <powerpc/oea/spr.h>
42
43 #include <sys/sysctl.h>
44 #include <dev/ofw/openfirm.h>
45
46 void init_scom_speedctl(void);
47 static int scom_speedctl = 0;
48 static uint32_t scom_speeds[2];
49 static uint32_t scom_reg[2];
50 static int sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
51 static int sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
52 static int sysctl_cpuspeed_available(SYSCTLFN_ARGS);
53
54 void
55 init_scom_speedctl(void)
56 {
57 int node;
58 const struct sysctlnode *sysctl_node, *me, *freq;
59
60 /* do this only once */
61 if (scom_speedctl != 0) return;
62 scom_speedctl = 1;
63
64 node = OF_finddevice("/cpus/@0");
65 if (OF_getprop(node, "power-mode-data", scom_reg, 8) != 8)
66 return;
67 if (OF_getprop(node, "clock-frequency", scom_speeds, 4) != 4)
68 return;
69 scom_speeds[0] = scom_speeds[0] / 1000000; /* Hz -> MHz */
70 scom_speeds[1] = scom_speeds[0] / 2;
71
72 sysctl_node = NULL;
73
74 if (sysctl_createv(NULL, 0, NULL,
75 &me,
76 CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
77 0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
78 printf("couldn't create 'cpu' node\n");
79
80 if (sysctl_createv(NULL, 0, NULL,
81 &freq,
82 CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
83 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
84 printf("couldn't create 'frequency' node\n");
85
86 if (sysctl_createv(NULL, 0, NULL,
87 &sysctl_node,
88 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
89 CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
90 0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
91 CTL_CREATE, CTL_EOL) == 0) {
92 } else
93 printf("couldn't create 'target' node\n");
94
95 if (sysctl_createv(NULL, 0, NULL,
96 &sysctl_node,
97 CTLFLAG_READWRITE,
98 CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
99 1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
100 CTL_CREATE, CTL_EOL) == 0) {
101 } else
102 printf("couldn't create 'current' node\n");
103
104 if (sysctl_createv(NULL, 0, NULL,
105 &sysctl_node,
106 CTLFLAG_READWRITE,
107 CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
108 2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
109 CTL_CREATE, CTL_EOL) == 0) {
110 } else
111 printf("couldn't create 'available' node\n");
112 }
113
114 static int
115 sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
116 {
117 struct sysctlnode node = *rnode;
118 int speed, nspeed = -1, mhz;
119
120 speed = (scom_read(SCOM_PSR) >> 56) & 3;
121 if (speed > 1) speed = 1;
122 mhz = scom_speeds[speed];
123 node.sysctl_data = &mhz;
124
125 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
126 int new_reg;
127
128 new_reg = *(int *)node.sysctl_data;
129 if (new_reg == scom_speeds[0]) {
130 nspeed = 0;
131 } else if (new_reg == scom_speeds[1]) {
132 nspeed = 1;
133 } else {
134 printf("%s: new_reg %d\n", __func__, new_reg);
135 return EINVAL;
136 }
137 if (nspeed != speed) {
138 volatile uint64_t junk;
139 scom_write(SCOM_PCR, 0x80000000);
140 scom_write(SCOM_PCR, scom_reg[nspeed]);
141 junk = scom_read(SCOM_PSR);
142 __USE(junk);
143 }
144 return 0;
145 }
146 return EINVAL;
147 }
148
149 static int
150 sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
151 {
152 struct sysctlnode node = *rnode;
153 uint64_t speed;
154 int mhz;
155
156 speed = (scom_read(SCOM_PSR) >> 56) & 3;
157 if (speed > 1) speed = 1;
158
159 mhz = scom_speeds[speed];
160 node.sysctl_data = &mhz;
161 return sysctl_lookup(SYSCTLFN_CALL(&node));
162 }
163
164 static int
165 sysctl_cpuspeed_available(SYSCTLFN_ARGS)
166 {
167 struct sysctlnode node = *rnode;
168 char buf[128];
169
170 snprintf(buf, 128, "%d %d", scom_speeds[0], scom_speeds[1]);
171 node.sysctl_data = buf;
172 return(sysctl_lookup(SYSCTLFN_CALL(&node)));
173 }
174
175 SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
176 {
177
178 sysctl_createv(NULL, 0, NULL, NULL,
179 CTLFLAG_PERMANENT,
180 CTLTYPE_NODE, "machdep", NULL,
181 NULL, 0, NULL, 0,
182 CTL_MACHDEP, CTL_EOL);
183 }
184