util: resource: Add resource_device struct and functions 01/270601/4
authorChanwoo Choi <cw00.choi@samsung.com>
Sun, 6 Feb 2022 08:13:48 +0000 (17:13 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 7 Feb 2022 03:52:20 +0000 (12:52 +0900)
Add resource_device structure for expressing the each h/w resource
such as CPU, GPU, Memory BUS, battery and so on.

Each h/w board has the different h/w resource information
such as the number of h/w resource, name, access path and so on.
PASS daemon has already supported the configuration method to get
the h/w resource information like /hal/etc/pass/pass.json.
Each h/w resource has the unique name, resource type and resource index
because board is able to have the one more same resource like big.LITTLE
cpu cluster.

When creating the instance of struct resource, will use the struct
resource_drvier and resource_device with resource type and index argument
because the user of create_resource() cannot get the unique resource name
which depends on the specific h/w. But the user of create_resource()
can get the available count of specific resource type by get_resource_device_*().

[How to create the instance of struct resource]
1. Find resource_device with resource type and index
2. Find resource_driver with resource type
3. Create the instance of struct resource by using resouce_device/driver info.

And rename from get_resource_driver to find_resource_driver.

Change-Id: I8c2fa6c8d22dcea996a12048fcab7de21eca652d
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
include/util/resource.h
src/util/resource.c

index 5064e43..32745a4 100644 (file)
@@ -78,9 +78,21 @@ struct resource_driver {
        const struct resource_attribute *attrs;
 };
 
+struct resource_device {
+       char *name;
+       int type;
+
+       /*
+        * Never initialize it by user of add_resource_device().
+        * It will be initialized by add_resource_device function automatically.
+        */
+       int index;
+};
+
 struct resource {
        char *name;
        int type;
+       int index;
        void *user_data;
 
        int num_attrs;
@@ -98,12 +110,21 @@ static void __DESTRUCTOR__ module_exit(void)       \
        remove_resource_driver(resource_driver);        \
 }
 
+/* Add/remove resource driver and device */
 void add_resource_driver(const struct resource_driver *resource_driver);
 void remove_resource_driver(const struct resource_driver *resource_driver);
 
-struct resource *create_resource(int resource_type, const char *resource_name, void *user_data);
+int get_resource_device_count_all(void);
+int get_resource_device_count(int resource_type);
+const struct resource_device *find_resource_device(int resource_type, int resource_index);
+int add_resource_device(struct resource_device *resource_device);
+void remove_resource_device(struct resource_device *resource_device);
+
+/* Create/delete resource instance */
+struct resource *create_resource(int resource_type, int resource_index, void *user_data);
 void delete_resource(struct resource *resource);
 
+/* Handle resource attribute */
 int update_resource_attr(struct resource *resource, u_int64_t attr_id);
 int update_resource_attrs(struct resource *resource);
 int update_resource_attrs_interest(struct resource *resource, u_int64_t attr_interest_mask);
index 523a0dc..6805dec 100644 (file)
@@ -27,6 +27,7 @@
 #define RESOURCE_ATTR_INDEX(id)                        (63 - __builtin_clzll(id))
 
 static GList *g_resource_driver_head;
+static GList *g_resource_device_head;
 
 static gint __compare_resource_type(gconstpointer data, gconstpointer input)
 {
@@ -56,7 +57,68 @@ void remove_resource_driver(const struct resource_driver *driver)
                        g_list_remove(g_resource_driver_head, (gpointer)driver);
 }
 
-const struct resource_driver *get_resource_driver(int resource_type)
+int get_resource_device_count_all(void)
+{
+       return g_list_length(g_resource_device_head);
+}
+
+int get_resource_device_count(int resource_type)
+{
+       GList *node;
+       struct resource_device *device;
+       int count = 0;
+
+       for (node = g_resource_device_head; node != NULL; node = node->next) {
+               device = node->data;
+               if (device->type == resource_type)
+                       count++;
+       }
+
+       return count;
+}
+
+const struct resource_device *find_resource_device(int resource_type,
+                                                  int resource_index)
+{
+       GList *node;
+       const struct resource_device *device;
+
+       for (node = g_resource_device_head; node != NULL; node = node->next) {
+               device = node->data;
+               if (device->type == resource_type &&
+                               device->index == resource_index)
+                       return device;
+       }
+
+       return NULL;
+}
+
+int add_resource_device(struct resource_device *device)
+{
+       int count;
+
+       if (!device)
+               return -EINVAL;
+
+       count = get_resource_device_count(device->type);
+       device->index = count;
+
+       g_resource_device_head =
+                       g_list_append(g_resource_device_head, (gpointer)device);
+
+       return 0;
+}
+
+void remove_resource_device(struct resource_device *device)
+{
+       if (!device)
+               return;
+
+       g_resource_device_head =
+                       g_list_remove(g_resource_device_head, (gpointer)device);
+}
+
+static const struct resource_driver *find_resource_driver(int resource_type)
 {
        GList *node;
 
@@ -83,13 +145,19 @@ void delete_resource(struct resource *resource)
        free(resource);
 }
 
-struct resource *create_resource(int resource_type, const char *resource_name, void *user_data)
+struct resource *create_resource(int resource_type, int resource_index,
+                                void *user_data)
 {
+       const struct resource_device *device = NULL;
        const struct resource_driver *driver = NULL;
        struct resource *resource = NULL;
        int i;
 
-       driver = get_resource_driver(resource_type);
+       device = find_resource_device(resource_type, resource_index);
+       if (!device)
+               return NULL;
+
+       driver = find_resource_driver(resource_type);
        if (!driver)
                return NULL;
 
@@ -97,8 +165,9 @@ struct resource *create_resource(int resource_type, const char *resource_name, v
        if (!resource)
                return NULL;
 
-       resource->type = resource_type;
-       resource->name = g_strdup(resource_name);
+       resource->type = device->type;
+       resource->name = g_strdup(device->name);
+       resource->index = device->index;
        resource->user_data = user_data;
        resource->num_attrs = driver->num_attrs;
        resource->attrs = driver->attrs;