xref: /netbsd-src/sys/arch/arm/nvidia/tegra_cpufreq.c (revision 095f6375b12b0fb47b872d38c979a1c14ab8be5e)
1*095f6375Sjmcneill /* $NetBSD: tegra_cpufreq.c,v 1.5 2017/04/22 23:53:24 jmcneill Exp $ */
2a4e03213Sjmcneill 
3a4e03213Sjmcneill /*-
4a4e03213Sjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5a4e03213Sjmcneill  * All rights reserved.
6a4e03213Sjmcneill  *
7a4e03213Sjmcneill  * Redistribution and use in source and binary forms, with or without
8a4e03213Sjmcneill  * modification, are permitted provided that the following conditions
9a4e03213Sjmcneill  * are met:
10a4e03213Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11a4e03213Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12a4e03213Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13a4e03213Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14a4e03213Sjmcneill  *    documentation and/or other materials provided with the distribution.
15a4e03213Sjmcneill  *
16a4e03213Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a4e03213Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a4e03213Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a4e03213Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a4e03213Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21a4e03213Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22a4e03213Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23a4e03213Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24a4e03213Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a4e03213Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a4e03213Sjmcneill  * SUCH DAMAGE.
27a4e03213Sjmcneill  */
28a4e03213Sjmcneill 
29a4e03213Sjmcneill #include "locators.h"
30a4e03213Sjmcneill 
31a4e03213Sjmcneill #include <sys/cdefs.h>
32*095f6375Sjmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_cpufreq.c,v 1.5 2017/04/22 23:53:24 jmcneill Exp $");
33a4e03213Sjmcneill 
34a4e03213Sjmcneill #include <sys/param.h>
35a4e03213Sjmcneill #include <sys/bus.h>
36a4e03213Sjmcneill #include <sys/device.h>
37a4e03213Sjmcneill #include <sys/intr.h>
38a4e03213Sjmcneill #include <sys/systm.h>
39a4e03213Sjmcneill #include <sys/kernel.h>
40a4e03213Sjmcneill #include <sys/atomic.h>
41a4e03213Sjmcneill #include <sys/kmem.h>
42a4e03213Sjmcneill #include <sys/sysctl.h>
43a4e03213Sjmcneill 
44a4e03213Sjmcneill #include <arm/nvidia/tegra_var.h>
45a4e03213Sjmcneill 
46a4e03213Sjmcneill static u_int cpufreq_busy;
47a4e03213Sjmcneill static struct sysctllog *cpufreq_log;
48a4e03213Sjmcneill static int cpufreq_node_target, cpufreq_node_current, cpufreq_node_available;
49a4e03213Sjmcneill 
50a4e03213Sjmcneill static const struct tegra_cpufreq_func *cpufreq_func = NULL;
51a4e03213Sjmcneill 
52a4e03213Sjmcneill static int	tegra_cpufreq_freq_helper(SYSCTLFN_PROTO);
53a4e03213Sjmcneill static char 	tegra_cpufreq_available[TEGRA_CPUFREQ_MAX * 5];
54a4e03213Sjmcneill 
55a4e03213Sjmcneill #define cpufreq_set_rate	cpufreq_func->set_rate
56a4e03213Sjmcneill #define cpufreq_get_rate	cpufreq_func->get_rate
57a4e03213Sjmcneill #define cpufreq_get_available	cpufreq_func->get_available
58a4e03213Sjmcneill 
59a4e03213Sjmcneill void
tegra_cpufreq_register(const struct tegra_cpufreq_func * cf)60a4e03213Sjmcneill tegra_cpufreq_register(const struct tegra_cpufreq_func *cf)
61a4e03213Sjmcneill {
62a4e03213Sjmcneill 	const struct sysctlnode *node, *cpunode, *freqnode;
63a4e03213Sjmcneill 	u_int availfreq[TEGRA_CPUFREQ_MAX];
64a4e03213Sjmcneill 	size_t nfreq;
65a4e03213Sjmcneill 	int error;
66a4e03213Sjmcneill 
67*095f6375Sjmcneill 	KASSERT(cpufreq_func == NULL);
68*095f6375Sjmcneill 	cpufreq_func = cf;
69a4e03213Sjmcneill 
70a4e03213Sjmcneill 	nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
71a4e03213Sjmcneill 	if (nfreq == 0)
72a4e03213Sjmcneill 		return;
73a4e03213Sjmcneill 
74a4e03213Sjmcneill 	KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
75a4e03213Sjmcneill 
76a4e03213Sjmcneill 	for (int i = 0; i < nfreq; i++) {
77a4e03213Sjmcneill 		char buf[6];
78a4e03213Sjmcneill 		snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
79a4e03213Sjmcneill 		strcat(tegra_cpufreq_available, buf);
80a4e03213Sjmcneill 	}
81a4e03213Sjmcneill 
82a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
83a4e03213Sjmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
84a4e03213Sjmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
85a4e03213Sjmcneill 	if (error)
86a4e03213Sjmcneill 		goto sysctl_failed;
87a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
88a4e03213Sjmcneill 	    0, CTLTYPE_NODE, "cpu", NULL,
89a4e03213Sjmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
90a4e03213Sjmcneill 	if (error)
91a4e03213Sjmcneill 		goto sysctl_failed;
92a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
93a4e03213Sjmcneill 	    0, CTLTYPE_NODE, "frequency", NULL,
94a4e03213Sjmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
95a4e03213Sjmcneill 	if (error)
96a4e03213Sjmcneill 		goto sysctl_failed;
97a4e03213Sjmcneill 
98a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
99a4e03213Sjmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
100a4e03213Sjmcneill 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
101a4e03213Sjmcneill 	    CTL_CREATE, CTL_EOL);
102a4e03213Sjmcneill 	if (error)
103a4e03213Sjmcneill 		goto sysctl_failed;
104a4e03213Sjmcneill 	cpufreq_node_target = node->sysctl_num;
105a4e03213Sjmcneill 
106a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
107a4e03213Sjmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
108a4e03213Sjmcneill 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
109a4e03213Sjmcneill 	    CTL_CREATE, CTL_EOL);
110a4e03213Sjmcneill 	if (error)
111a4e03213Sjmcneill 		goto sysctl_failed;
112a4e03213Sjmcneill 	cpufreq_node_current = node->sysctl_num;
113a4e03213Sjmcneill 
114a4e03213Sjmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
115a4e03213Sjmcneill 	    0, CTLTYPE_STRING, "available", NULL,
116a4e03213Sjmcneill 	    NULL, 0, tegra_cpufreq_available, 0,
117a4e03213Sjmcneill 	    CTL_CREATE, CTL_EOL);
118a4e03213Sjmcneill 	if (error)
119a4e03213Sjmcneill 		goto sysctl_failed;
120a4e03213Sjmcneill 	cpufreq_node_available = node->sysctl_num;
121a4e03213Sjmcneill 
12293e0bfebSjmcneill 	device_printf(curcpu()->ci_dev,
12393e0bfebSjmcneill 	    "setting speed to %d MHz\n", availfreq[0]);
124b2b0f53cSjmcneill 	cpufreq_set_rate(availfreq[0]);
125a4e03213Sjmcneill 
126a4e03213Sjmcneill 	return;
127a4e03213Sjmcneill 
128a4e03213Sjmcneill sysctl_failed:
129a4e03213Sjmcneill 	aprint_error("cpufreq: couldn't create sysctl nodes (%d)\n", error);
130a4e03213Sjmcneill 	sysctl_teardown(&cpufreq_log);
131a4e03213Sjmcneill }
132a4e03213Sjmcneill 
133a4e03213Sjmcneill static int
tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)134a4e03213Sjmcneill tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
135a4e03213Sjmcneill {
136a4e03213Sjmcneill 	struct sysctlnode node;
137a4e03213Sjmcneill 	int fq, oldfq = 0, error;
138a4e03213Sjmcneill 
139a4e03213Sjmcneill 	node = *rnode;
140a4e03213Sjmcneill 	node.sysctl_data = &fq;
141a4e03213Sjmcneill 
142a4e03213Sjmcneill 	fq = cpufreq_get_rate();
143a4e03213Sjmcneill 	if (rnode->sysctl_num == cpufreq_node_target)
144a4e03213Sjmcneill 		oldfq = fq;
145a4e03213Sjmcneill 
146a4e03213Sjmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
147a4e03213Sjmcneill 	if (error || newp == NULL)
148a4e03213Sjmcneill 		return error;
149a4e03213Sjmcneill 
150a4e03213Sjmcneill 	if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
151a4e03213Sjmcneill 		return 0;
152a4e03213Sjmcneill 
153a4e03213Sjmcneill 	if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
154a4e03213Sjmcneill 		return EBUSY;
155a4e03213Sjmcneill 
156a4e03213Sjmcneill 	error = cpufreq_set_rate(fq);
157a4e03213Sjmcneill 	if (error == 0) {
158a4e03213Sjmcneill 		pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
159a4e03213Sjmcneill 	}
160a4e03213Sjmcneill 
161a4e03213Sjmcneill 	atomic_dec_uint(&cpufreq_busy);
162a4e03213Sjmcneill 
163a4e03213Sjmcneill 	return error;
164a4e03213Sjmcneill }
165