|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright 2025 Samsung Electronics All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 14 | + * either express or implied. |
| 15 | + * See the License for the specific |
| 16 | + * language governing permissions and limitations under the License. |
| 17 | + * |
| 18 | + ****************************************************************************/ |
| 19 | + |
| 20 | +/**************************************************************************** |
| 21 | + * Included Files |
| 22 | + ****************************************************************************/ |
| 23 | + |
| 24 | +#include <tinyara/config.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include <string.h> |
| 28 | +#include <fcntl.h> |
| 29 | +#include <sys/ioctl.h> |
| 30 | +#include <errno.h> |
| 31 | +#include <unistd.h> |
| 32 | +#include <tinyara/cpu_driver.h> |
| 33 | +#include <apps/shell/tash.h> |
| 34 | + |
| 35 | +/**************************************************************************** |
| 36 | + * Preprocessor Definitions |
| 37 | + ****************************************************************************/ |
| 38 | + |
| 39 | +#define CPU1_DEVICE_PATH "/dev/cpu1" |
| 40 | + |
| 41 | +const char *cpu_states[] = { |
| 42 | + "running", //0 |
| 43 | + "hotplug", //1 |
| 44 | +}; |
| 45 | + |
| 46 | +/**************************************************************************** |
| 47 | + * Private Function Prototypes |
| 48 | + ****************************************************************************/ |
| 49 | + |
| 50 | +static int cputest_cpu1_on(int argc, char *argv[]); |
| 51 | +static int cputest_cpu1_off(int argc, char *argv[]); |
| 52 | +static int cputest_cpu_status(int argc, char *argv[]); |
| 53 | + |
| 54 | +/**************************************************************************** |
| 55 | + * Private Functions |
| 56 | + ****************************************************************************/ |
| 57 | + |
| 58 | +static int cputest_cpu1_on(int argc, char *argv[]) |
| 59 | +{ |
| 60 | + int fd; |
| 61 | + cpu_state_t state; |
| 62 | + |
| 63 | + if (sched_getcpu() > 0) { |
| 64 | + printf("can only powerup cpu1 from cpu0!\n"); |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + printf("powering up cpu1!\n"); |
| 69 | + |
| 70 | + fd = open(CPU1_DEVICE_PATH, O_RDWR); |
| 71 | + if (fd < 0) { |
| 72 | + printf("Cannot open %s file with fd = %d errno = %d\n", CPU1_DEVICE_PATH, fd, errno); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + if (ioctl(fd, IOC_CPU_ENABLE, 1) < 0) { |
| 77 | + printf("ioctl failed errno = %d\n", errno); |
| 78 | + close(fd); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + printf("cpu1 successfully enabled\n"); |
| 82 | + |
| 83 | + if (ioctl(fd, IOC_CPU_GET_STATE, &state) < 0) { |
| 84 | + printf("IOC_CPU_GET_STATE ioctl failed for %s : %d\n", CPU1_DEVICE_PATH, errno); |
| 85 | + close(fd); |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + printf("state: %d (%s)\n", state, cpu_states[state]); |
| 90 | + close(fd); |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +static int cputest_cpu1_off(int argc, char *argv[]) |
| 95 | +{ |
| 96 | + int fd; |
| 97 | + cpu_state_t state; |
| 98 | + |
| 99 | + if (sched_getcpu() > 0) { |
| 100 | + printf("can only powerdown cpu1 from cpu0!\n"); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + printf("powering down cpu1!\n"); |
| 104 | + |
| 105 | + fd = open(CPU1_DEVICE_PATH, O_RDWR); |
| 106 | + if (fd < 0) { |
| 107 | + printf("Cannot open %s file with fd = %d errno = %d\n", CPU1_DEVICE_PATH, fd, errno); |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + if (ioctl(fd, IOC_CPU_DISABLE, 1) < 0) { |
| 112 | + printf("ioctl failed errno = %d\n", errno); |
| 113 | + close(fd); |
| 114 | + return 0; |
| 115 | + } |
| 116 | + printf("cpu1 successfully disabled\n"); |
| 117 | + |
| 118 | + if (ioctl(fd, IOC_CPU_GET_STATE, &state) < 0) { |
| 119 | + printf("IOC_CPU_GET_STATE ioctl failed for %s : %d\n", CPU1_DEVICE_PATH, errno); |
| 120 | + close(fd); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + printf("state: %d (%s)\n", state, cpu_states[state]); |
| 124 | + close(fd); |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +static int cputest_cpu_status(int argc, char *argv[]) |
| 129 | +{ |
| 130 | + int fd; |
| 131 | + char devname[16]; |
| 132 | + cpu_state_t state; |
| 133 | + |
| 134 | + for (u8 i = 1; i < CONFIG_SMP_NCPUS; i++) { |
| 135 | + snprintf(devname, sizeof(devname), "/dev/cpu%d", i); |
| 136 | + |
| 137 | + fd = open(devname, O_RDWR); |
| 138 | + if (fd < 0) { |
| 139 | + printf("Cannot open %s file with fd = %d errno = %d\n", devname, fd, errno); |
| 140 | + return 0; |
| 141 | + } |
| 142 | + |
| 143 | + if (ioctl(fd, IOC_CPU_GET_STATE, &state) < 0) { |
| 144 | + printf("IOC_CPU_GET_STATE ioctl failed for %s : %d\n", devname, errno); |
| 145 | + close(fd); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + printf("cpu state for cpu%d: %d (%s)\n", i, state, cpu_states[state]); |
| 150 | + close(fd); |
| 151 | + } |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
| 155 | +const static tash_cmdlist_t cputest_cmds[] = { |
| 156 | + {"cpu1on", cputest_cpu1_on, TASH_EXECMD_ASYNC}, |
| 157 | + {"cpu1off", cputest_cpu1_off, TASH_EXECMD_ASYNC}, |
| 158 | + {"cpustatus", cputest_cpu_status, TASH_EXECMD_ASYNC}, |
| 159 | + {NULL, NULL, 0} |
| 160 | +}; |
| 161 | + |
| 162 | +/**************************************************************************** |
| 163 | + * Public Functions |
| 164 | + ****************************************************************************/ |
| 165 | + |
| 166 | +void cputest_register_cmds(void) |
| 167 | +{ |
| 168 | + /* Register the CPU test commands during system initialization */ |
| 169 | + tash_cmdlist_install(cputest_cmds); |
| 170 | +} |
0 commit comments