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