tegra_cpufreq.c revision 1.5 1 1.5 jmcneill /* $NetBSD: tegra_cpufreq.c,v 1.5 2017/04/22 23:53:24 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.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_cpufreq.c,v 1.5 2017/04/22 23:53:24 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/sysctl.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <arm/nvidia/tegra_var.h>
45 1.1 jmcneill
46 1.1 jmcneill static u_int cpufreq_busy;
47 1.1 jmcneill static struct sysctllog *cpufreq_log;
48 1.1 jmcneill static int cpufreq_node_target, cpufreq_node_current, cpufreq_node_available;
49 1.1 jmcneill
50 1.1 jmcneill static const struct tegra_cpufreq_func *cpufreq_func = NULL;
51 1.1 jmcneill
52 1.1 jmcneill static int tegra_cpufreq_freq_helper(SYSCTLFN_PROTO);
53 1.1 jmcneill static char tegra_cpufreq_available[TEGRA_CPUFREQ_MAX * 5];
54 1.1 jmcneill
55 1.1 jmcneill #define cpufreq_set_rate cpufreq_func->set_rate
56 1.1 jmcneill #define cpufreq_get_rate cpufreq_func->get_rate
57 1.1 jmcneill #define cpufreq_get_available cpufreq_func->get_available
58 1.1 jmcneill
59 1.1 jmcneill void
60 1.1 jmcneill tegra_cpufreq_register(const struct tegra_cpufreq_func *cf)
61 1.1 jmcneill {
62 1.1 jmcneill const struct sysctlnode *node, *cpunode, *freqnode;
63 1.1 jmcneill u_int availfreq[TEGRA_CPUFREQ_MAX];
64 1.1 jmcneill size_t nfreq;
65 1.1 jmcneill int error;
66 1.1 jmcneill
67 1.5 jmcneill KASSERT(cpufreq_func == NULL);
68 1.5 jmcneill cpufreq_func = cf;
69 1.1 jmcneill
70 1.1 jmcneill nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
71 1.1 jmcneill if (nfreq == 0)
72 1.1 jmcneill return;
73 1.1 jmcneill
74 1.1 jmcneill KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
75 1.1 jmcneill
76 1.1 jmcneill for (int i = 0; i < nfreq; i++) {
77 1.1 jmcneill char buf[6];
78 1.1 jmcneill snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
79 1.1 jmcneill strcat(tegra_cpufreq_available, buf);
80 1.1 jmcneill }
81 1.1 jmcneill
82 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
83 1.1 jmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
84 1.1 jmcneill NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
85 1.1 jmcneill if (error)
86 1.1 jmcneill goto sysctl_failed;
87 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
88 1.1 jmcneill 0, CTLTYPE_NODE, "cpu", NULL,
89 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
90 1.1 jmcneill if (error)
91 1.1 jmcneill goto sysctl_failed;
92 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
93 1.1 jmcneill 0, CTLTYPE_NODE, "frequency", NULL,
94 1.1 jmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
95 1.1 jmcneill if (error)
96 1.1 jmcneill goto sysctl_failed;
97 1.1 jmcneill
98 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
99 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
100 1.1 jmcneill tegra_cpufreq_freq_helper, 0, NULL, 0,
101 1.1 jmcneill CTL_CREATE, CTL_EOL);
102 1.1 jmcneill if (error)
103 1.1 jmcneill goto sysctl_failed;
104 1.1 jmcneill cpufreq_node_target = node->sysctl_num;
105 1.1 jmcneill
106 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
107 1.1 jmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "current", 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_current = node->sysctl_num;
113 1.1 jmcneill
114 1.1 jmcneill error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
115 1.1 jmcneill 0, CTLTYPE_STRING, "available", NULL,
116 1.1 jmcneill NULL, 0, tegra_cpufreq_available, 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_available = node->sysctl_num;
121 1.1 jmcneill
122 1.3 jmcneill device_printf(curcpu()->ci_dev,
123 1.3 jmcneill "setting speed to %d MHz\n", availfreq[0]);
124 1.2 jmcneill cpufreq_set_rate(availfreq[0]);
125 1.1 jmcneill
126 1.1 jmcneill return;
127 1.1 jmcneill
128 1.1 jmcneill sysctl_failed:
129 1.1 jmcneill aprint_error("cpufreq: couldn't create sysctl nodes (%d)\n", error);
130 1.1 jmcneill sysctl_teardown(&cpufreq_log);
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.1 jmcneill static int
134 1.1 jmcneill tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
135 1.1 jmcneill {
136 1.1 jmcneill struct sysctlnode node;
137 1.1 jmcneill int fq, oldfq = 0, error;
138 1.1 jmcneill
139 1.1 jmcneill node = *rnode;
140 1.1 jmcneill node.sysctl_data = &fq;
141 1.1 jmcneill
142 1.1 jmcneill fq = cpufreq_get_rate();
143 1.1 jmcneill if (rnode->sysctl_num == cpufreq_node_target)
144 1.1 jmcneill oldfq = fq;
145 1.1 jmcneill
146 1.1 jmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
147 1.1 jmcneill if (error || newp == NULL)
148 1.1 jmcneill return error;
149 1.1 jmcneill
150 1.1 jmcneill if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
151 1.1 jmcneill return 0;
152 1.1 jmcneill
153 1.1 jmcneill if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
154 1.1 jmcneill return EBUSY;
155 1.1 jmcneill
156 1.1 jmcneill error = cpufreq_set_rate(fq);
157 1.1 jmcneill if (error == 0) {
158 1.1 jmcneill pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill atomic_dec_uint(&cpufreq_busy);
162 1.1 jmcneill
163 1.1 jmcneill return error;
164 1.1 jmcneill }
165