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