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>
31 #include <util/resource.h>
33 #include <resource-monitor/resource-monitor.h>
40 static int bus_get_value_from_hal_power(struct resource *res,
41 const struct resource_attribute *attr,
44 struct bus_context *ctx;
46 int *val = (int *)data;
49 if (!res || !attr || !data)
52 ctx = get_resource_privdata(res);
56 if (!ctx->device_name) {
57 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
58 get_resource_name(res));
62 type = get_resource_type(res);
65 case BUS_ATTR_CUR_FREQ:
66 _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name);
68 case BUS_ATTR_MIN_FREQ:
69 _val = hal_power_dvfs_get_min_freq(type, ctx->device_name);
71 case BUS_ATTR_MAX_FREQ:
72 _val = hal_power_dvfs_get_max_freq(type, ctx->device_name);
74 case BUS_ATTR_AVAILABLE_MIN_FREQ:
75 _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name);
77 case BUS_ATTR_AVAILABLE_MAX_FREQ:
78 _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name);
92 static int bus_get_curr_governor(struct resource *res,
93 const struct resource_attribute *attr,
96 struct bus_context *ctx;;
97 char *buf = (char *)data;
100 if (!res || !attr || !data)
103 ctx = get_resource_privdata(res);
107 if (!ctx->device_name) {
108 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
109 get_resource_name(res));
113 val = hal_power_dvfs_get_curr_governor(get_resource_type(res),
114 ctx->device_name, buf);
121 static const struct resource_attribute bus_attrs[] = {
123 .name = "BUS_ATTR_CUR_FREQ",
124 .id = BUS_ATTR_CUR_FREQ,
125 .type = DATA_TYPE_INT,
127 .get = bus_get_value_from_hal_power,
130 .name = "BUS_ATTR_MIN_FREQ",
131 .id = BUS_ATTR_MIN_FREQ,
132 .type = DATA_TYPE_INT,
134 .get = bus_get_value_from_hal_power,
137 .name = "BUS_ATTR_MAX_FREQ",
138 .id = BUS_ATTR_MAX_FREQ,
139 .type = DATA_TYPE_INT,
141 .get = bus_get_value_from_hal_power,
144 .name = "BUS_ATTR_AVAILABLE_MIN_FREQ",
145 .id = BUS_ATTR_AVAILABLE_MIN_FREQ,
146 .type = DATA_TYPE_INT,
148 .get = bus_get_value_from_hal_power,
151 .name = "BUS_ATTR_AVAILABLE_MAX_FREQ",
152 .id = BUS_ATTR_AVAILABLE_MAX_FREQ,
153 .type = DATA_TYPE_INT,
155 .get = bus_get_value_from_hal_power,
158 .name = "BUS_ATTR_CUR_GOVERNOR",
159 .id = BUS_ATTR_CUR_GOVERNOR,
160 .type = DATA_TYPE_STRING,
162 .get = bus_get_curr_governor,
167 static int bus_setup_device_id(struct resource *res,
168 const struct resource_control *ctrl,
171 struct bus_context *ctx;
172 const struct resource_device *device;
173 int device_id = (int)(intptr_t)data;
178 ctx = get_resource_privdata(res);
182 device = find_resource_device(get_resource_type(res), device_id);
184 _E("Not available resource: type: %s, index: %d\n",
185 get_resource_name(res), device_id);
189 if (ctx->device_name)
190 free(ctx->device_name);
192 ctx->device_name = g_strdup(device->name);
193 ctx->index = device_id;
198 static const struct resource_control bus_ctrls[] = {
200 .name = "BUS_CTRL_DEVICE_ID",
201 .id = BUS_CTRL_DEVICE_ID,
203 .set = bus_setup_device_id,
208 static int bus_init(struct resource *res)
210 struct bus_context *ctx;
212 ctx = calloc(1, sizeof(struct bus_context));
216 set_resource_privdata(res, ctx);
221 static void bus_exit(struct resource *res)
223 struct bus_context *ctx;
228 ctx = get_resource_privdata(res);
232 if (ctx->device_name) {
233 free(ctx->device_name);
234 ctx->device_name = NULL;
237 set_resource_privdata(res, NULL);
240 static const struct resource_driver bus_resource_driver = {
242 .type = RESOURCE_TYPE_BUS,
244 .num_attrs = ARRAY_SIZE(bus_attrs),
246 .num_ctrls = ARRAY_SIZE(bus_ctrls),
252 RESOURCE_DRIVER_REGISTER(&bus_resource_driver)