tegra_cpufreq.c revision 1.3 1 1.3 jmcneill /* $NetBSD: tegra_cpufreq.c,v 1.3 2015/12/22 22:10:36 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_cpufreq.c,v 1.3 2015/12/22 22:10:36 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 int tegra_cpufreq_freq_helper(SYSCTLFN_PROTO);
54 1.1 jmcneill static char tegra_cpufreq_available[TEGRA_CPUFREQ_MAX * 5];
55 1.1 jmcneill
56 1.1 jmcneill #define cpufreq_set_rate cpufreq_func->set_rate
57 1.1 jmcneill #define cpufreq_get_rate cpufreq_func->get_rate
58 1.1 jmcneill #define cpufreq_get_available cpufreq_func->get_available
59 1.1 jmcneill
60 1.1 jmcneill void
61 1.1 jmcneill tegra_cpufreq_register(const struct tegra_cpufreq_func *cf)
62 1.1 jmcneill {
63 1.1 jmcneill KASSERT(cpufreq_func == NULL);
64 1.1 jmcneill cpufreq_func = cf;
65 1.1 jmcneill }
66 1.1 jmcneill
67 1.1 jmcneill void
68 1.1 jmcneill tegra_cpufreq_init(void)
69 1.1 jmcneill {
70 1.1 jmcneill const struct sysctlnode *node, *cpunode, *freqnode;
71 1.1 jmcneill u_int availfreq[TEGRA_CPUFREQ_MAX];
72 1.1 jmcneill size_t nfreq;
73 1.1 jmcneill int error;
74 1.1 jmcneill
75 1.1 jmcneill if (cpufreq_func == NULL)
76 1.1 jmcneill return;
77 1.1 jmcneill
78 1.1 jmcneill nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
79 1.1 jmcneill if (nfreq == 0)
80 1.1 jmcneill return;
81 1.1 jmcneill
82 1.1 jmcneill KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
83 1.1 jmcneill
84 1.1 jmcneill for (int i = 0; i < nfreq; i++) {
85 1.1 jmcneill char buf[6];
86 1.1 jmcneill snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
87 1.1 jmcneill strcat(tegra_cpufreq_available, buf);
88 1.1 jmcneill }
89 1.1 jmcneill
90 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
91 1.1 jmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
92 1.1 jmcneill NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
93 1.1 jmcneill if (error)
94 1.1 jmcneill goto sysctl_failed;
95 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
96 1.1 jmcneill 0, CTLTYPE_NODE, "cpu", NULL,
97 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
98 1.1 jmcneill if (error)
99 1.1 jmcneill goto sysctl_failed;
100 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
101 1.1 jmcneill 0, CTLTYPE_NODE, "frequency", NULL,
102 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
103 1.1 jmcneill if (error)
104 1.1 jmcneill goto sysctl_failed;
105 1.1 jmcneill
106 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
107 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
108 1.1 jmcneill tegra_cpufreq_freq_helper, 0, NULL, 0,
109 1.1 jmcneill CTL_CREATE, CTL_EOL);
110 1.1 jmcneill if (error)
111 1.1 jmcneill goto sysctl_failed;
112 1.1 jmcneill cpufreq_node_target = node->sysctl_num;
113 1.1 jmcneill
114 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
115 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
116 1.1 jmcneill tegra_cpufreq_freq_helper, 0, NULL, 0,
117 1.1 jmcneill CTL_CREATE, CTL_EOL);
118 1.1 jmcneill if (error)
119 1.1 jmcneill goto sysctl_failed;
120 1.1 jmcneill cpufreq_node_current = node->sysctl_num;
121 1.1 jmcneill
122 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
123 1.1 jmcneill 0, CTLTYPE_STRING, "available", NULL,
124 1.1 jmcneill NULL, 0, tegra_cpufreq_available, 0,
125 1.1 jmcneill CTL_CREATE, CTL_EOL);
126 1.1 jmcneill if (error)
127 1.1 jmcneill goto sysctl_failed;
128 1.1 jmcneill cpufreq_node_available = node->sysctl_num;
129 1.1 jmcneill
130 1.3 jmcneill device_printf(curcpu()->ci_dev,
131 1.3 jmcneill "setting speed to %d MHz\n", availfreq[0]);
132 1.2 jmcneill cpufreq_set_rate(availfreq[0]);
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 int
142 1.1 jmcneill tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
143 1.1 jmcneill {
144 1.1 jmcneill struct sysctlnode node;
145 1.1 jmcneill int fq, oldfq = 0, error;
146 1.1 jmcneill
147 1.1 jmcneill node = *rnode;
148 1.1 jmcneill node.sysctl_data = &fq;
149 1.1 jmcneill
150 1.1 jmcneill fq = cpufreq_get_rate();
151 1.1 jmcneill if (rnode->sysctl_num == cpufreq_node_target)
152 1.1 jmcneill oldfq = fq;
153 1.1 jmcneill
154 1.1 jmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
155 1.1 jmcneill if (error || newp == NULL)
156 1.1 jmcneill return error;
157 1.1 jmcneill
158 1.1 jmcneill if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
159 1.1 jmcneill return 0;
160 1.1 jmcneill
161 1.1 jmcneill if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
162 1.1 jmcneill return EBUSY;
163 1.1 jmcneill
164 1.1 jmcneill error = cpufreq_set_rate(fq);
165 1.1 jmcneill if (error == 0) {
166 1.1 jmcneill pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill atomic_dec_uint(&cpufreq_busy);
170 1.1 jmcneill
171 1.1 jmcneill return error;
172 1.1 jmcneill }
173