tegra_cpufreq.c revision 1.1.2.4 1 /* $NetBSD: tegra_cpufreq.c,v 1.1.2.4 2016/12/05 10:54:50 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * 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 #include "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tegra_cpufreq.c,v 1.1.2.4 2016/12/05 10:54:50 skrll Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/atomic.h>
41 #include <sys/kmem.h>
42 #include <sys/sysctl.h>
43
44 #include <arm/nvidia/tegra_var.h>
45
46 static u_int cpufreq_busy;
47 static struct sysctllog *cpufreq_log;
48 static int cpufreq_node_target, cpufreq_node_current, cpufreq_node_available;
49
50 static const struct tegra_cpufreq_func *cpufreq_func = NULL;
51
52 static int tegra_cpufreq_freq_helper(SYSCTLFN_PROTO);
53 static char tegra_cpufreq_available[TEGRA_CPUFREQ_MAX * 5];
54
55 #define cpufreq_set_rate cpufreq_func->set_rate
56 #define cpufreq_get_rate cpufreq_func->get_rate
57 #define cpufreq_get_available cpufreq_func->get_available
58
59 void
60 tegra_cpufreq_register(const struct tegra_cpufreq_func *cf)
61 {
62 KASSERT(cpufreq_func == NULL);
63 cpufreq_func = cf;
64 }
65
66 void
67 tegra_cpufreq_init(void)
68 {
69 const struct sysctlnode *node, *cpunode, *freqnode;
70 u_int availfreq[TEGRA_CPUFREQ_MAX];
71 size_t nfreq;
72 int error;
73
74 if (cpufreq_func == NULL)
75 return;
76
77 nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
78 if (nfreq == 0)
79 return;
80
81 KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
82
83 for (int i = 0; i < nfreq; i++) {
84 char buf[6];
85 snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
86 strcat(tegra_cpufreq_available, buf);
87 }
88
89 error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
90 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
91 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
92 if (error)
93 goto sysctl_failed;
94 error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
95 0, CTLTYPE_NODE, "cpu", NULL,
96 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
97 if (error)
98 goto sysctl_failed;
99 error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
100 0, CTLTYPE_NODE, "frequency", NULL,
101 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
102 if (error)
103 goto sysctl_failed;
104
105 error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
106 CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
107 tegra_cpufreq_freq_helper, 0, NULL, 0,
108 CTL_CREATE, CTL_EOL);
109 if (error)
110 goto sysctl_failed;
111 cpufreq_node_target = node->sysctl_num;
112
113 error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
114 CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
115 tegra_cpufreq_freq_helper, 0, NULL, 0,
116 CTL_CREATE, CTL_EOL);
117 if (error)
118 goto sysctl_failed;
119 cpufreq_node_current = node->sysctl_num;
120
121 error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
122 0, CTLTYPE_STRING, "available", NULL,
123 NULL, 0, tegra_cpufreq_available, 0,
124 CTL_CREATE, CTL_EOL);
125 if (error)
126 goto sysctl_failed;
127 cpufreq_node_available = node->sysctl_num;
128
129 device_printf(curcpu()->ci_dev,
130 "setting speed to %d MHz\n", availfreq[0]);
131 cpufreq_set_rate(availfreq[0]);
132
133 return;
134
135 sysctl_failed:
136 aprint_error("cpufreq: couldn't create sysctl nodes (%d)\n", error);
137 sysctl_teardown(&cpufreq_log);
138 }
139
140 static int
141 tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
142 {
143 struct sysctlnode node;
144 int fq, oldfq = 0, error;
145
146 node = *rnode;
147 node.sysctl_data = &fq;
148
149 fq = cpufreq_get_rate();
150 if (rnode->sysctl_num == cpufreq_node_target)
151 oldfq = fq;
152
153 error = sysctl_lookup(SYSCTLFN_CALL(&node));
154 if (error || newp == NULL)
155 return error;
156
157 if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
158 return 0;
159
160 if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
161 return EBUSY;
162
163 error = cpufreq_set_rate(fq);
164 if (error == 0) {
165 pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
166 }
167
168 atomic_dec_uint(&cpufreq_busy);
169
170 return error;
171 }
172