2 * PASS (Power Aware System Service) - CPU Resource Driver
4 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * @file resource-cpu.c
27 #include <hal/hal-power.h>
29 #include <util/common.h>
31 #include <util/resource.h>
33 #include <tmonitor/tmonitor.h>
40 static int cpu_get_value_from_hal_power(const struct resource *res,
41 const struct resource_attribute *attr,
44 struct cpu_context *ctx;
45 int *val = (int *)data;
48 if (!res || !res->priv || !attr || !data)
53 if (!ctx->device_name) {
54 _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name);
59 case CPU_ATTR_CUR_FREQ:
60 _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name);
62 case CPU_ATTR_MIN_FREQ:
63 _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name);
65 case CPU_ATTR_MAX_FREQ:
66 _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name);
68 case CPU_ATTR_AVAILABLE_MIN_FREQ:
69 _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name);
71 case CPU_ATTR_AVAILABLE_MAX_FREQ:
72 _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name);
86 static int cpu_get_curr_governor(const struct resource *res,
87 const struct resource_attribute *attr,
90 struct cpu_context *ctx;
91 char *buf = (char *)data;
94 if (!res || !res->priv || !attr || !data)
99 if (!ctx->device_name) {
100 _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name);
104 val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf);
111 static const struct resource_attribute cpu_attrs[] = {
113 .name = "CPU_ATTR_CUR_FREQ",
114 .id = CPU_ATTR_CUR_FREQ,
115 .type = DATA_TYPE_INT,
117 .get = cpu_get_value_from_hal_power,
120 .name = "CPU_ATTR_MIN_FREQ",
121 .id = CPU_ATTR_MIN_FREQ,
122 .type = DATA_TYPE_INT,
124 .get = cpu_get_value_from_hal_power,
127 .name = "CPU_ATTR_MAX_FREQ",
128 .id = CPU_ATTR_MAX_FREQ,
129 .type = DATA_TYPE_INT,
131 .get = cpu_get_value_from_hal_power,
134 .name = "CPU_ATTR_AVAILABLE_MIN_FREQ",
135 .id = CPU_ATTR_AVAILABLE_MIN_FREQ,
136 .type = DATA_TYPE_INT,
138 .get = cpu_get_value_from_hal_power,
141 .name = "CPU_ATTR_AVAILABLE_MAX_FREQ",
142 .id = CPU_ATTR_AVAILABLE_MAX_FREQ,
143 .type = DATA_TYPE_INT,
145 .get = cpu_get_value_from_hal_power,
148 .name = "CPU_ATTR_CUR_GOVERNOR",
149 .id = CPU_ATTR_CUR_GOVERNOR,
150 .type = DATA_TYPE_STRING,
152 .get = cpu_get_curr_governor,
157 static int cpu_setup_cluster_id(const struct resource *res,
158 const struct resource_control *ctrl,
161 struct cpu_context *ctx;
162 const struct resource_device *device;
163 int resource_index = (int)(intptr_t)data;
165 if (!res || !res->priv || !ctrl)
168 device = find_resource_device(res->type, resource_index);
170 _E("Not available resource: type: %s, index: %d\n",
171 res->name, resource_index);
177 if (ctx->device_name)
178 free(ctx->device_name);
180 ctx->device_name = g_strdup(device->name);
181 ctx->index = resource_index;
186 static const struct resource_control cpu_ctrls[] = {
188 .name = "CPU_CTRL_CLUSTER_ID",
189 .id = CPU_CTRL_CLUSTER_ID,
191 .set = cpu_setup_cluster_id,
196 static int cpu_init(struct resource *res)
198 struct cpu_context *ctx;
200 ctx = calloc(1, sizeof(struct cpu_context));
209 static void cpu_exit(struct resource *res)
211 struct cpu_context *ctx;
213 if (res && res->priv) {
215 if (ctx->device_name) {
216 free(ctx->device_name);
217 ctx->device_name = NULL;
224 static const struct resource_driver cpu_resource_driver = {
226 .type = RESOURCE_TYPE_CPU,
228 .num_attrs = ARRAY_SIZE(cpu_attrs),
230 .num_ctrls = ARRAY_SIZE(cpu_ctrls),
236 RESOURCE_DRIVER_REGISTER(&cpu_resource_driver)