xref: /netbsd-src/sys/arch/arm/nvidia/tegra_cpufreq.c (revision 095f6375b12b0fb47b872d38c979a1c14ab8be5e)
1 /* $NetBSD: tegra_cpufreq.c,v 1.5 2017/04/22 23:53:24 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@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.5 2017/04/22 23:53:24 jmcneill 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
tegra_cpufreq_register(const struct tegra_cpufreq_func * cf)60 tegra_cpufreq_register(const struct tegra_cpufreq_func *cf)
61 {
62 	const struct sysctlnode *node, *cpunode, *freqnode;
63 	u_int availfreq[TEGRA_CPUFREQ_MAX];
64 	size_t nfreq;
65 	int error;
66 
67 	KASSERT(cpufreq_func == NULL);
68 	cpufreq_func = cf;
69 
70 	nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
71 	if (nfreq == 0)
72 		return;
73 
74 	KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
75 
76 	for (int i = 0; i < nfreq; i++) {
77 		char buf[6];
78 		snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
79 		strcat(tegra_cpufreq_available, buf);
80 	}
81 
82 	error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
83 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
84 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
85 	if (error)
86 		goto sysctl_failed;
87 	error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
88 	    0, CTLTYPE_NODE, "cpu", NULL,
89 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
90 	if (error)
91 		goto sysctl_failed;
92 	error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
93 	    0, CTLTYPE_NODE, "frequency", NULL,
94 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
95 	if (error)
96 		goto sysctl_failed;
97 
98 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
99 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
100 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
101 	    CTL_CREATE, CTL_EOL);
102 	if (error)
103 		goto sysctl_failed;
104 	cpufreq_node_target = node->sysctl_num;
105 
106 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
107 	    CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
108 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
109 	    CTL_CREATE, CTL_EOL);
110 	if (error)
111 		goto sysctl_failed;
112 	cpufreq_node_current = node->sysctl_num;
113 
114 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
115 	    0, CTLTYPE_STRING, "available", NULL,
116 	    NULL, 0, tegra_cpufreq_available, 0,
117 	    CTL_CREATE, CTL_EOL);
118 	if (error)
119 		goto sysctl_failed;
120 	cpufreq_node_available = node->sysctl_num;
121 
122 	device_printf(curcpu()->ci_dev,
123 	    "setting speed to %d MHz\n", availfreq[0]);
124 	cpufreq_set_rate(availfreq[0]);
125 
126 	return;
127 
128 sysctl_failed:
129 	aprint_error("cpufreq: couldn't create sysctl nodes (%d)\n", error);
130 	sysctl_teardown(&cpufreq_log);
131 }
132 
133 static int
tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)134 tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
135 {
136 	struct sysctlnode node;
137 	int fq, oldfq = 0, error;
138 
139 	node = *rnode;
140 	node.sysctl_data = &fq;
141 
142 	fq = cpufreq_get_rate();
143 	if (rnode->sysctl_num == cpufreq_node_target)
144 		oldfq = fq;
145 
146 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
147 	if (error || newp == NULL)
148 		return error;
149 
150 	if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
151 		return 0;
152 
153 	if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
154 		return EBUSY;
155 
156 	error = cpufreq_set_rate(fq);
157 	if (error == 0) {
158 		pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
159 	}
160 
161 	atomic_dec_uint(&cpufreq_busy);
162 
163 	return error;
164 }
165