2 * PASS (Power Aware System Service) - Memory Bus 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-bus.c
27 #include <hal/hal-power.h>
29 #include <util/common.h>
30 #include <util/devices.h>
32 #include <libsyscommon/resource-manager.h>
33 #include <libsyscommon/resource-type.h>
34 #include <libsyscommon/resource-device.h>
36 #include <resource-monitor/resource-monitor.h>
43 static int bus_get_value_from_hal_power(int resource_id,
44 const struct syscommon_resman_resource_attribute *attr,
47 struct bus_context *ctx;
49 int *val = (int *)data;
52 if (resource_id < 0 || !attr || !data)
55 ctx = syscommon_resman_get_resource_privdata(resource_id);
59 if (!ctx->device_name) {
60 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
61 syscommon_resman_get_resource_name(resource_id));
65 type = syscommon_resman_get_resource_type(resource_id);
68 case BUS_ATTR_CUR_FREQ:
69 _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name);
71 case BUS_ATTR_MIN_FREQ:
72 _val = hal_power_dvfs_get_min_freq(type, ctx->device_name);
74 case BUS_ATTR_MAX_FREQ:
75 _val = hal_power_dvfs_get_max_freq(type, ctx->device_name);
77 case BUS_ATTR_AVAILABLE_MIN_FREQ:
78 _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name);
80 case BUS_ATTR_AVAILABLE_MAX_FREQ:
81 _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name);
95 static int bus_get_string_value(int resource_id,
96 const struct syscommon_resman_resource_attribute *attr,
99 struct bus_context *ctx;
100 char *buf = (char *)data;
103 if (resource_id < 0 || !attr || !data)
106 ctx = syscommon_resman_get_resource_privdata(resource_id);
110 if (!ctx->device_name) {
111 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
112 syscommon_resman_get_resource_name(resource_id));
117 case BUS_ATTR_CUR_GOVERNOR:
118 val = hal_power_dvfs_get_curr_governor(syscommon_resman_get_resource_type(resource_id),
119 ctx->device_name, buf);
124 strncpy(buf, ctx->device_name, BUFF_MAX);
131 static const struct syscommon_resman_resource_attribute bus_attrs[] = {
133 .name = "BUS_ATTR_CUR_FREQ",
134 .id = BUS_ATTR_CUR_FREQ,
135 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
136 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
138 .get = bus_get_value_from_hal_power,
141 .name = "BUS_ATTR_MIN_FREQ",
142 .id = BUS_ATTR_MIN_FREQ,
143 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
144 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
146 .get = bus_get_value_from_hal_power,
149 .name = "BUS_ATTR_MAX_FREQ",
150 .id = BUS_ATTR_MAX_FREQ,
151 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
152 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
154 .get = bus_get_value_from_hal_power,
157 .name = "BUS_ATTR_AVAILABLE_MIN_FREQ",
158 .id = BUS_ATTR_AVAILABLE_MIN_FREQ,
159 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
160 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
162 .get = bus_get_value_from_hal_power,
165 .name = "BUS_ATTR_AVAILABLE_MAX_FREQ",
166 .id = BUS_ATTR_AVAILABLE_MAX_FREQ,
167 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
168 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
170 .get = bus_get_value_from_hal_power,
173 .name = "BUS_ATTR_CUR_GOVERNOR",
174 .id = BUS_ATTR_CUR_GOVERNOR,
175 .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
176 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
178 .get = bus_get_string_value,
181 .name = "BUS_ATTR_NAME",
183 .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
184 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
186 .get = bus_get_string_value,
191 static int bus_setup_device_id(int resource_id,
192 const struct syscommon_resman_resource_control *ctrl,
195 struct bus_context *ctx;
196 const struct syscommon_resman_resource_device *device;
197 int device_id = (int)(intptr_t)data;
199 if (resource_id < 0 || !ctrl)
202 ctx = syscommon_resman_get_resource_privdata(resource_id);
206 device = syscommon_resman_find_resource_device(syscommon_resman_get_resource_type(resource_id), device_id);
208 _E("Not available resource: type: %s, index: %d\n",
209 syscommon_resman_get_resource_name(resource_id), device_id);
213 if (ctx->device_name)
214 free(ctx->device_name);
216 ctx->device_name = g_strdup(device->name);
217 ctx->index = device_id;
222 static const struct syscommon_resman_resource_control bus_ctrls[] = {
224 .name = "BUS_CTRL_DEVICE_ID",
225 .id = BUS_CTRL_DEVICE_ID,
227 .set = bus_setup_device_id,
232 static int bus_create(int resource_id)
234 struct bus_context *ctx;
236 ctx = calloc(1, sizeof(struct bus_context));
240 syscommon_resman_set_resource_privdata(resource_id, ctx);
245 static void bus_delete(int resource_id)
247 struct bus_context *ctx;
252 ctx = syscommon_resman_get_resource_privdata(resource_id);
256 if (ctx->device_name) {
257 free(ctx->device_name);
258 ctx->device_name = NULL;
261 syscommon_resman_set_resource_privdata(resource_id, NULL);
264 static const struct syscommon_resman_resource_driver bus_resource_driver = {
266 .type = RESOURCE_TYPE_BUS,
268 .num_attrs = ARRAY_SIZE(bus_attrs),
270 .num_ctrls = ARRAY_SIZE(bus_ctrls),
272 .create = bus_create,
273 .delete = bus_delete,
276 SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&bus_resource_driver)