2 * PASS (Power Aware System Service) - GPU 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-gpu.c
27 #include <hal/hal-power.h>
29 #include <util/common.h>
30 #include <util/devices.h>
33 #include <libsyscommon/resource-manager.h>
34 #include <libsyscommon/resource-type.h>
35 #include <libsyscommon/resource-device.h>
37 #include <resource-monitor/resource-monitor.h>
44 static int gpu_get_value_from_hal_power(struct syscommon_resman_resource *res,
45 const struct syscommon_resman_resource_attribute *attr,
48 struct gpu_context *ctx;
50 int *val = (int *)data;
53 if (!res || !attr || !data)
56 ctx = syscommon_resman_get_resource_privdata(res);
60 if (!ctx->device_name) {
61 _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n",
62 syscommon_resman_get_resource_name(res));
66 type = syscommon_resman_get_resource_type(res);
69 case GPU_ATTR_CUR_FREQ:
70 _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name);
72 case GPU_ATTR_MIN_FREQ:
73 _val = hal_power_dvfs_get_min_freq(type, ctx->device_name);
75 case GPU_ATTR_MAX_FREQ:
76 _val = hal_power_dvfs_get_max_freq(type, ctx->device_name);
78 case GPU_ATTR_AVAILABLE_MIN_FREQ:
79 _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name);
81 case GPU_ATTR_AVAILABLE_MAX_FREQ:
82 _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name);
96 static int gpu_get_string_value(struct syscommon_resman_resource *res,
97 const struct syscommon_resman_resource_attribute *attr,
100 struct gpu_context *ctx;
101 char *buf = (char *)data;
104 if (!res || !attr || !data)
107 ctx = syscommon_resman_get_resource_privdata(res);
111 if (!ctx->device_name) {
112 _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n",
113 syscommon_resman_get_resource_name(res));
118 case GPU_ATTR_CUR_GOVERNOR:
119 val = hal_power_dvfs_get_curr_governor(syscommon_resman_get_resource_type(res),
120 ctx->device_name, buf);
125 strncpy(buf, ctx->device_name, BUFF_MAX);
132 static const struct syscommon_resman_resource_attribute gpu_attrs[] = {
134 .name = "GPU_ATTR_CUR_FREQ",
135 .id = GPU_ATTR_CUR_FREQ,
136 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
137 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
139 .get = gpu_get_value_from_hal_power,
142 .name = "GPU_ATTR_MIN_FREQ",
143 .id = GPU_ATTR_MIN_FREQ,
144 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
145 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
147 .get = gpu_get_value_from_hal_power,
150 .name = "GPU_ATTR_MAX_FREQ",
151 .id = GPU_ATTR_MAX_FREQ,
152 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
153 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
155 .get = gpu_get_value_from_hal_power,
158 .name = "GPU_ATTR_AVAILABLE_MIN_FREQ",
159 .id = GPU_ATTR_AVAILABLE_MIN_FREQ,
160 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
161 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
163 .get = gpu_get_value_from_hal_power,
166 .name = "GPU_ATTR_AVAILABLE_MAX_FREQ",
167 .id = GPU_ATTR_AVAILABLE_MAX_FREQ,
168 .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
169 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
171 .get = gpu_get_value_from_hal_power,
174 .name = "GPU_ATTR_CUR_GOVERNOR",
175 .id = GPU_ATTR_CUR_GOVERNOR,
176 .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
177 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
179 .get = gpu_get_string_value,
182 .name = "GPU_ATTR_NAME",
184 .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
185 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
187 .get = gpu_get_string_value,
192 static int gpu_setup_device_id(struct syscommon_resman_resource *res,
193 const struct syscommon_resman_resource_control *ctrl,
196 struct gpu_context *ctx;
197 const struct syscommon_resman_resource_device *device;
198 int resource_index = (int)(intptr_t)data;
203 ctx = syscommon_resman_get_resource_privdata(res);
207 device = syscommon_resman_find_resource_device(syscommon_resman_get_resource_type(res), resource_index);
209 _E("Not available resource: type: %s, index: %d\n",
210 syscommon_resman_get_resource_name(res), resource_index);
214 if (ctx->device_name)
215 free(ctx->device_name);
217 ctx->device_name = g_strdup(device->name);
218 ctx->index = resource_index;
223 static const struct syscommon_resman_resource_control gpu_ctrls[] = {
225 .name = "GPU_CTRL_DEVICE_ID",
226 .id = GPU_CTRL_DEVICE_ID,
228 .set = gpu_setup_device_id,
233 static int gpu_create(struct syscommon_resman_resource *res)
235 struct gpu_context *ctx;
237 ctx = calloc(1, sizeof(struct gpu_context));
241 syscommon_resman_set_resource_privdata(res, ctx);
246 static void gpu_delete(struct syscommon_resman_resource *res)
248 struct gpu_context *ctx;
253 ctx = syscommon_resman_get_resource_privdata(res);
257 if (ctx->device_name) {
258 free(ctx->device_name);
259 ctx->device_name = NULL;
262 syscommon_resman_set_resource_privdata(res, NULL);
265 static const struct syscommon_resman_resource_driver gpu_resource_driver = {
267 .type = RESOURCE_TYPE_GPU,
269 .num_attrs = ARRAY_SIZE(gpu_attrs),
271 .num_ctrls = ARRAY_SIZE(gpu_ctrls),
273 .create = gpu_create,
274 .delete = gpu_delete,
277 SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&gpu_resource_driver)