resource: bus: Replace resource name with 'BUS' instead of 'Memory Bus'
[platform/core/system/pass.git] / src / resource / resource-bus.c
1 /*
2  * PASS (Power Aware System Service) - Memory Bus Resource Driver
3  *
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  * @file        resource-bus.c
21  * @brief       TBD
22  * @ingroup     TBD
23  */
24
25 #include <glib.h>
26
27 #include <hal/hal-power.h>
28
29 #include <util/common.h>
30 #include <util/log.h>
31 #include <util/resource.h>
32
33 #include <resource-monitor/resource-monitor.h>
34
35 struct bus_context {
36         char *device_name;
37         int index;
38 };
39
40 static int bus_get_value_from_hal_power(struct resource *res,
41                                 const struct resource_attribute *attr,
42                                 void *data)
43 {
44         struct bus_context *ctx;
45         int type;
46         int *val = (int *)data;
47         int _val;
48
49         if (!res || !attr || !data)
50                 return -EINVAL;
51
52         ctx = get_resource_privdata(res);
53         if (!ctx)
54                 return -EINVAL;
55
56         if (!ctx->device_name) {
57                 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
58                                         get_resource_name(res));
59                 return -EINVAL;
60         }
61
62         type = get_resource_type(res);
63
64         switch (attr->id) {
65         case BUS_ATTR_CUR_FREQ:
66                 _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name);
67                 break;
68         case BUS_ATTR_MIN_FREQ:
69                 _val = hal_power_dvfs_get_min_freq(type, ctx->device_name);
70                 break;
71         case BUS_ATTR_MAX_FREQ:
72                 _val = hal_power_dvfs_get_max_freq(type, ctx->device_name);
73                 break;
74         case BUS_ATTR_AVAILABLE_MIN_FREQ:
75                 _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name);
76                 break;
77         case BUS_ATTR_AVAILABLE_MAX_FREQ:
78                 _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name);
79                 break;
80         default:
81                 return -EINVAL;
82         }
83
84         if (_val < 0)
85                 return -EINVAL;
86
87         *val = _val;
88
89         return 0;
90 }
91
92 static int bus_get_curr_governor(struct resource *res,
93                                 const struct resource_attribute *attr,
94                                 void *data)
95 {
96         struct bus_context *ctx;;
97         char *buf = (char *)data;
98         int val;
99
100         if (!res || !attr || !data)
101                 return -EINVAL;
102
103         ctx = get_resource_privdata(res);
104         if (!ctx)
105                 return -EINVAL;
106
107         if (!ctx->device_name) {
108                 _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n",
109                                         get_resource_name(res));
110                 return -EINVAL;
111         }
112
113         val = hal_power_dvfs_get_curr_governor(get_resource_type(res),
114                                                 ctx->device_name, buf);
115         if (val < 0)
116                 return -EINVAL;
117
118         return 0;
119 }
120
121 static const struct resource_attribute bus_attrs[] = {
122         {
123                 .name   = "BUS_ATTR_CUR_FREQ",
124                 .id     = BUS_ATTR_CUR_FREQ,
125                 .type   = DATA_TYPE_INT,
126                 .ops    = {
127                         .get = bus_get_value_from_hal_power,
128                 },
129         }, {
130                 .name   = "BUS_ATTR_MIN_FREQ",
131                 .id     = BUS_ATTR_MIN_FREQ,
132                 .type   = DATA_TYPE_INT,
133                 .ops    = {
134                         .get = bus_get_value_from_hal_power,
135                 },
136         }, {
137                 .name   = "BUS_ATTR_MAX_FREQ",
138                 .id     = BUS_ATTR_MAX_FREQ,
139                 .type   = DATA_TYPE_INT,
140                 .ops    = {
141                         .get = bus_get_value_from_hal_power,
142                 }
143         }, {
144                 .name   = "BUS_ATTR_AVAILABLE_MIN_FREQ",
145                 .id     = BUS_ATTR_AVAILABLE_MIN_FREQ,
146                 .type   = DATA_TYPE_INT,
147                 .ops    = {
148                         .get = bus_get_value_from_hal_power,
149                 }
150         }, {
151                 .name   = "BUS_ATTR_AVAILABLE_MAX_FREQ",
152                 .id     = BUS_ATTR_AVAILABLE_MAX_FREQ,
153                 .type   = DATA_TYPE_INT,
154                 .ops    = {
155                         .get = bus_get_value_from_hal_power,
156                 }
157         }, {
158                 .name   = "BUS_ATTR_CUR_GOVERNOR",
159                 .id     = BUS_ATTR_CUR_GOVERNOR,
160                 .type   = DATA_TYPE_STRING,
161                 .ops    = {
162                         .get = bus_get_curr_governor,
163                 }
164         },
165 };
166
167 static int bus_setup_device_id(struct resource *res,
168                                 const struct resource_control *ctrl,
169                                 const void *data)
170 {
171         struct bus_context *ctx;
172         const struct resource_device *device;
173         int device_id = (int)(intptr_t)data;
174
175         if (!res || !ctrl)
176                 return -EINVAL;
177
178         ctx = get_resource_privdata(res);
179         if (!ctx)
180                 return -EINVAL;
181
182         device = find_resource_device(get_resource_type(res), device_id);
183         if (!device) {
184                 _E("Not available resource: type: %s, index: %d\n",
185                                 get_resource_name(res), device_id);
186                 return -EINVAL;
187         }
188
189         if (ctx->device_name)
190                 free(ctx->device_name);
191
192         ctx->device_name = g_strdup(device->name);
193         ctx->index = device_id;
194
195         return 0;
196 }
197
198 static const struct resource_control bus_ctrls[] = {
199         {
200                 .name = "BUS_CTRL_DEVICE_ID",
201                 .id = BUS_CTRL_DEVICE_ID,
202                 .ops = {
203                         .set = bus_setup_device_id,
204                 },
205         },
206 };
207
208 static int bus_init(struct resource *res)
209 {
210         struct bus_context *ctx;
211
212         ctx = calloc(1, sizeof(struct bus_context));
213         if (!ctx)
214                 return -ENOMEM;
215
216         set_resource_privdata(res, ctx);
217
218         return 0;
219 }
220
221 static void bus_exit(struct resource *res)
222 {
223         struct bus_context *ctx;
224
225         if (!res)
226                 return;
227
228         ctx = get_resource_privdata(res);
229         if (!ctx)
230                 return;
231
232         if (ctx->device_name) {
233                 free(ctx->device_name);
234                 ctx->device_name = NULL;
235         }
236         free(ctx);
237         set_resource_privdata(res, NULL);
238 }
239
240 static const struct resource_driver bus_resource_driver = {
241         .name           = "BUS",
242         .type           = RESOURCE_TYPE_BUS,
243         .attrs          = bus_attrs,
244         .num_attrs      = ARRAY_SIZE(bus_attrs),
245         .ctrls          = bus_ctrls,
246         .num_ctrls      = ARRAY_SIZE(bus_ctrls),
247         .ops            = {
248                 .init = bus_init,
249                 .exit = bus_exit,
250         },
251 };
252 RESOURCE_DRIVER_REGISTER(&bus_resource_driver)