Home | History | Annotate | Line # | Download | only in oea
cpu_speedctl.c revision 1.2
      1  1.2       rin /*	$NetBSD: cpu_speedctl.c,v 1.2 2020/07/06 09:34:17 rin Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2006 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.2       rin 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.2       rin __KERNEL_RCSID(0, "$NetBSD: cpu_speedctl.c,v 1.2 2020/07/06 09:34:17 rin Exp $");
     31  1.1  macallan 
     32  1.2       rin #ifdef _KERNEL_OPT
     33  1.1  macallan #include "opt_ppcparam.h"
     34  1.2       rin #endif
     35  1.1  macallan 
     36  1.1  macallan #include <sys/param.h>
     37  1.1  macallan #include <sys/systm.h>
     38  1.1  macallan #include <sys/device.h>
     39  1.1  macallan #include <sys/types.h>
     40  1.1  macallan 
     41  1.1  macallan #include <powerpc/psl.h>
     42  1.1  macallan #include <powerpc/spr.h>
     43  1.1  macallan #include <powerpc/oea/cpufeat.h>
     44  1.1  macallan #include <powerpc/oea/spr.h>
     45  1.1  macallan 
     46  1.1  macallan #include <sys/sysctl.h>
     47  1.1  macallan #include <dev/ofw/openfirm.h>
     48  1.1  macallan 
     49  1.1  macallan void init_scom_speedctl(void);
     50  1.1  macallan static int scom_speedctl = 0;
     51  1.1  macallan static uint32_t scom_speeds[2];
     52  1.1  macallan static uint32_t scom_reg[2];
     53  1.1  macallan static int  sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
     54  1.1  macallan static int  sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
     55  1.1  macallan static int  sysctl_cpuspeed_available(SYSCTLFN_ARGS);
     56  1.1  macallan 
     57  1.1  macallan void
     58  1.1  macallan init_scom_speedctl(void)
     59  1.1  macallan {
     60  1.1  macallan 	int node;
     61  1.1  macallan 	const struct sysctlnode *sysctl_node, *me, *freq;
     62  1.1  macallan 
     63  1.1  macallan 	/* do this only once */
     64  1.1  macallan 	if (scom_speedctl != 0) return;
     65  1.1  macallan 	scom_speedctl = 1;
     66  1.1  macallan 
     67  1.1  macallan 	node = OF_finddevice("/cpus/@0");
     68  1.1  macallan 	if (OF_getprop(node, "power-mode-data", scom_reg, 8) != 8)
     69  1.1  macallan 		return;
     70  1.1  macallan 	if (OF_getprop(node, "clock-frequency", scom_speeds, 4) != 4)
     71  1.1  macallan 		return;
     72  1.1  macallan 	scom_speeds[0] = scom_speeds[0] / 1000000; /* Hz -> MHz */
     73  1.1  macallan 	scom_speeds[1] = scom_speeds[0] / 2;
     74  1.1  macallan 
     75  1.1  macallan 	sysctl_node = NULL;
     76  1.1  macallan 
     77  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     78  1.1  macallan 	    &me,
     79  1.1  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
     80  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
     81  1.1  macallan 		printf("couldn't create 'cpu' node\n");
     82  1.1  macallan 
     83  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     84  1.1  macallan 	    &freq,
     85  1.1  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
     86  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
     87  1.1  macallan 		printf("couldn't create 'frequency' node\n");
     88  1.1  macallan 
     89  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     90  1.1  macallan 	    &sysctl_node,
     91  1.1  macallan 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
     92  1.1  macallan 	    CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
     93  1.1  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
     94  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
     95  1.1  macallan 	} else
     96  1.1  macallan 		printf("couldn't create 'target' node\n");
     97  1.1  macallan 
     98  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
     99  1.1  macallan 	    &sysctl_node,
    100  1.1  macallan 	    CTLFLAG_READWRITE,
    101  1.1  macallan 	    CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
    102  1.1  macallan 	    1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    103  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    104  1.1  macallan 	} else
    105  1.1  macallan 		printf("couldn't create 'current' node\n");
    106  1.1  macallan 
    107  1.1  macallan 	if (sysctl_createv(NULL, 0, NULL,
    108  1.1  macallan 	    &sysctl_node,
    109  1.1  macallan 	    CTLFLAG_READWRITE,
    110  1.1  macallan 	    CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
    111  1.1  macallan 	    2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    112  1.1  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    113  1.1  macallan 	} else
    114  1.1  macallan 		printf("couldn't create 'available' node\n");
    115  1.1  macallan }
    116  1.1  macallan 
    117  1.1  macallan static int
    118  1.1  macallan sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
    119  1.1  macallan {
    120  1.1  macallan 	struct sysctlnode node = *rnode;
    121  1.1  macallan 	int speed, nspeed = -1, mhz;
    122  1.1  macallan 
    123  1.1  macallan 	speed = (scom_read(SCOM_PSR) >> 56) & 3;
    124  1.1  macallan 	if (speed > 1) speed = 1;
    125  1.1  macallan 	mhz = scom_speeds[speed];
    126  1.1  macallan 	node.sysctl_data = &mhz;
    127  1.1  macallan 
    128  1.1  macallan 	if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    129  1.1  macallan 		int new_reg;
    130  1.1  macallan 
    131  1.1  macallan 		new_reg = *(int *)node.sysctl_data;
    132  1.1  macallan 		if (new_reg == scom_speeds[0]) {
    133  1.1  macallan 			nspeed = 0;
    134  1.1  macallan 		} else if (new_reg == scom_speeds[1]) {
    135  1.1  macallan 			nspeed = 1;
    136  1.1  macallan 		} else {
    137  1.1  macallan 			printf("%s: new_reg %d\n", __func__, new_reg);
    138  1.1  macallan 			return EINVAL;
    139  1.1  macallan 		}
    140  1.1  macallan 		if (nspeed != speed) {
    141  1.1  macallan 			volatile uint64_t junk;
    142  1.1  macallan 			scom_write(SCOM_PCR, 0x80000000);
    143  1.1  macallan 			scom_write(SCOM_PCR, scom_reg[nspeed]);
    144  1.1  macallan 			junk = scom_read(SCOM_PSR);
    145  1.1  macallan 			__USE(junk);
    146  1.1  macallan 		}
    147  1.1  macallan 		return 0;
    148  1.1  macallan 	}
    149  1.1  macallan 	return EINVAL;
    150  1.1  macallan }
    151  1.1  macallan 
    152  1.1  macallan static int
    153  1.1  macallan sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
    154  1.1  macallan {
    155  1.1  macallan 	struct sysctlnode node = *rnode;
    156  1.1  macallan 	uint64_t speed;
    157  1.1  macallan 	int mhz;
    158  1.1  macallan 
    159  1.1  macallan 	speed = (scom_read(SCOM_PSR) >> 56) & 3;
    160  1.1  macallan 	if (speed > 1) speed = 1;
    161  1.1  macallan 
    162  1.1  macallan 	mhz = scom_speeds[speed];
    163  1.1  macallan 	node.sysctl_data = &mhz;
    164  1.1  macallan 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    165  1.1  macallan }
    166  1.1  macallan 
    167  1.1  macallan static int
    168  1.1  macallan sysctl_cpuspeed_available(SYSCTLFN_ARGS)
    169  1.1  macallan {
    170  1.1  macallan 	struct sysctlnode node = *rnode;
    171  1.1  macallan 	char buf[128];
    172  1.1  macallan 
    173  1.1  macallan 	snprintf(buf, 128, "%d %d", scom_speeds[0], scom_speeds[1]);
    174  1.1  macallan 	node.sysctl_data = buf;
    175  1.1  macallan 	return(sysctl_lookup(SYSCTLFN_CALL(&node)));
    176  1.1  macallan }
    177  1.1  macallan 
    178  1.1  macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
    179  1.1  macallan {
    180  1.1  macallan 
    181  1.1  macallan 	sysctl_createv(NULL, 0, NULL, NULL,
    182  1.1  macallan 		       CTLFLAG_PERMANENT,
    183  1.1  macallan 		       CTLTYPE_NODE, "machdep", NULL,
    184  1.1  macallan 		       NULL, 0, NULL, 0,
    185  1.1  macallan 		       CTL_MACHDEP, CTL_EOL);
    186  1.1  macallan }
    187