Home | History | Annotate | Line # | Download | only in nvidia
tegra_cpufreq.c revision 1.3.2.1
      1  1.3.2.1  pgoyette /* $NetBSD: tegra_cpufreq.c,v 1.3.2.1 2017/01/07 08:56:11 pgoyette 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.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: tegra_cpufreq.c,v 1.3.2.1 2017/01/07 08:56:11 pgoyette 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 	KASSERT(cpufreq_func == NULL);
     63      1.1  jmcneill 	cpufreq_func = cf;
     64      1.1  jmcneill }
     65      1.1  jmcneill 
     66      1.1  jmcneill void
     67      1.1  jmcneill tegra_cpufreq_init(void)
     68      1.1  jmcneill {
     69      1.1  jmcneill 	const struct sysctlnode *node, *cpunode, *freqnode;
     70      1.1  jmcneill 	u_int availfreq[TEGRA_CPUFREQ_MAX];
     71      1.1  jmcneill 	size_t nfreq;
     72      1.1  jmcneill 	int error;
     73      1.1  jmcneill 
     74      1.1  jmcneill 	if (cpufreq_func == NULL)
     75      1.1  jmcneill 		return;
     76      1.1  jmcneill 
     77      1.1  jmcneill 	nfreq = cpufreq_get_available(availfreq, TEGRA_CPUFREQ_MAX);
     78      1.1  jmcneill 	if (nfreq == 0)
     79      1.1  jmcneill 		return;
     80      1.1  jmcneill 
     81      1.1  jmcneill 	KASSERT(nfreq <= TEGRA_CPUFREQ_MAX);
     82      1.1  jmcneill 
     83      1.1  jmcneill 	for (int i = 0; i < nfreq; i++) {
     84      1.1  jmcneill 		char buf[6];
     85      1.1  jmcneill 		snprintf(buf, sizeof(buf), i ? " %u" : "%u", availfreq[i]);
     86      1.1  jmcneill 		strcat(tegra_cpufreq_available, buf);
     87      1.1  jmcneill 	}
     88      1.1  jmcneill 
     89      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
     90      1.1  jmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
     91      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
     92      1.1  jmcneill 	if (error)
     93      1.1  jmcneill 		goto sysctl_failed;
     94      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
     95      1.1  jmcneill 	    0, CTLTYPE_NODE, "cpu", NULL,
     96      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
     97      1.1  jmcneill 	if (error)
     98      1.1  jmcneill 		goto sysctl_failed;
     99      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
    100      1.1  jmcneill 	    0, CTLTYPE_NODE, "frequency", NULL,
    101      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    102      1.1  jmcneill 	if (error)
    103      1.1  jmcneill 		goto sysctl_failed;
    104      1.1  jmcneill 
    105      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    106      1.1  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    107      1.1  jmcneill 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
    108      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    109      1.1  jmcneill 	if (error)
    110      1.1  jmcneill 		goto sysctl_failed;
    111      1.1  jmcneill 	cpufreq_node_target = node->sysctl_num;
    112      1.1  jmcneill 
    113      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    114      1.1  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
    115      1.1  jmcneill 	    tegra_cpufreq_freq_helper, 0, NULL, 0,
    116      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    117      1.1  jmcneill 	if (error)
    118      1.1  jmcneill 		goto sysctl_failed;
    119      1.1  jmcneill 	cpufreq_node_current = node->sysctl_num;
    120      1.1  jmcneill 
    121      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    122      1.1  jmcneill 	    0, CTLTYPE_STRING, "available", NULL,
    123      1.1  jmcneill 	    NULL, 0, tegra_cpufreq_available, 0,
    124      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    125      1.1  jmcneill 	if (error)
    126      1.1  jmcneill 		goto sysctl_failed;
    127      1.1  jmcneill 	cpufreq_node_available = node->sysctl_num;
    128      1.1  jmcneill 
    129      1.3  jmcneill 	device_printf(curcpu()->ci_dev,
    130      1.3  jmcneill 	    "setting speed to %d MHz\n", availfreq[0]);
    131      1.2  jmcneill 	cpufreq_set_rate(availfreq[0]);
    132      1.1  jmcneill 
    133      1.1  jmcneill 	return;
    134      1.1  jmcneill 
    135      1.1  jmcneill sysctl_failed:
    136      1.1  jmcneill 	aprint_error("cpufreq: couldn't create sysctl nodes (%d)\n", error);
    137      1.1  jmcneill 	sysctl_teardown(&cpufreq_log);
    138      1.1  jmcneill }
    139      1.1  jmcneill 
    140      1.1  jmcneill static int
    141      1.1  jmcneill tegra_cpufreq_freq_helper(SYSCTLFN_ARGS)
    142      1.1  jmcneill {
    143      1.1  jmcneill 	struct sysctlnode node;
    144      1.1  jmcneill 	int fq, oldfq = 0, error;
    145      1.1  jmcneill 
    146      1.1  jmcneill 	node = *rnode;
    147      1.1  jmcneill 	node.sysctl_data = &fq;
    148      1.1  jmcneill 
    149      1.1  jmcneill 	fq = cpufreq_get_rate();
    150      1.1  jmcneill 	if (rnode->sysctl_num == cpufreq_node_target)
    151      1.1  jmcneill 		oldfq = fq;
    152      1.1  jmcneill 
    153      1.1  jmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    154      1.1  jmcneill 	if (error || newp == NULL)
    155      1.1  jmcneill 		return error;
    156      1.1  jmcneill 
    157      1.1  jmcneill 	if (fq == oldfq || rnode->sysctl_num != cpufreq_node_target)
    158      1.1  jmcneill 		return 0;
    159      1.1  jmcneill 
    160      1.1  jmcneill 	if (atomic_cas_uint(&cpufreq_busy, 0, 1) != 0)
    161      1.1  jmcneill 		return EBUSY;
    162      1.1  jmcneill 
    163      1.1  jmcneill 	error = cpufreq_set_rate(fq);
    164      1.1  jmcneill 	if (error == 0) {
    165      1.1  jmcneill 		pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
    166      1.1  jmcneill 	}
    167      1.1  jmcneill 
    168      1.1  jmcneill 	atomic_dec_uint(&cpufreq_busy);
    169      1.1  jmcneill 
    170      1.1  jmcneill 	return error;
    171      1.1  jmcneill }
    172