Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[cascardo/linux.git] / drivers / gpu / drm / amd / powerplay / hwmgr / functiontables.c
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include "hwmgr.h"
27
28 static int phm_run_table(struct pp_hwmgr *hwmgr,
29                          struct phm_runtime_table_header *rt_table,
30                          void *input,
31                          void *output,
32                          void *temp_storage)
33 {
34         int result = 0;
35         phm_table_function *function;
36
37         if (rt_table->function_list == NULL) {
38                 printk(KERN_INFO "[ powerplay ] this function not implement!\n");
39                 return 0;
40         }
41
42         for (function = rt_table->function_list; NULL != *function; function++) {
43                 int tmp = (*function)(hwmgr, input, output, temp_storage, result);
44
45                 if (tmp == PP_Result_TableImmediateExit)
46                         break;
47                 if (tmp) {
48                         if (0 == result)
49                                 result = tmp;
50                         if (rt_table->exit_error)
51                                 break;
52                 }
53         }
54
55         return result;
56 }
57
58 int phm_dispatch_table(struct pp_hwmgr *hwmgr,
59                        struct phm_runtime_table_header *rt_table,
60                        void *input, void *output)
61 {
62         int result = 0;
63         void *temp_storage = NULL;
64
65         if (hwmgr == NULL || rt_table == NULL) {
66                 printk(KERN_ERR "[ powerplay ] Invalid Parameter!\n");
67                 return -EINVAL;
68         }
69
70         if (0 != rt_table->storage_size) {
71                 temp_storage = kzalloc(rt_table->storage_size, GFP_KERNEL);
72                 if (temp_storage == NULL) {
73                         printk(KERN_ERR "[ powerplay ] Could not allocate table temporary storage\n");
74                         return -ENOMEM;
75                 }
76         }
77
78         result = phm_run_table(hwmgr, rt_table, input, output, temp_storage);
79
80         if (NULL != temp_storage)
81                 kfree(temp_storage);
82
83         return result;
84 }
85
86 int phm_construct_table(struct pp_hwmgr *hwmgr,
87                         const struct phm_master_table_header *master_table,
88                         struct phm_runtime_table_header *rt_table)
89 {
90         uint32_t function_count = 0;
91         const struct phm_master_table_item *table_item;
92         uint32_t size;
93         phm_table_function *run_time_list;
94         phm_table_function *rtf;
95
96         if (hwmgr == NULL || master_table == NULL || rt_table == NULL) {
97                 printk(KERN_ERR "[ powerplay ] Invalid Parameter!\n");
98                 return -EINVAL;
99         }
100
101         for (table_item = master_table->master_list;
102                 NULL != table_item->tableFunction; table_item++) {
103                 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
104                     (table_item->isFunctionNeededInRuntimeTable(hwmgr)))
105                         function_count++;
106         }
107
108         size = (function_count + 1) * sizeof(phm_table_function);
109         run_time_list = kzalloc(size, GFP_KERNEL);
110
111         if (NULL == run_time_list)
112                 return -ENOMEM;
113
114         rtf = run_time_list;
115         for (table_item = master_table->master_list;
116                 NULL != table_item->tableFunction; table_item++) {
117                 if ((rtf - run_time_list) > function_count) {
118                         printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
119                         kfree(run_time_list);
120                         return -EINVAL;
121                 }
122
123                 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
124                      (table_item->isFunctionNeededInRuntimeTable(hwmgr))) {
125                         *(rtf++) = table_item->tableFunction;
126                 }
127         }
128
129         if ((rtf - run_time_list) > function_count) {
130                 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
131                 kfree(run_time_list);
132                 return -EINVAL;
133         }
134
135         *rtf = NULL;
136         rt_table->function_list = run_time_list;
137         rt_table->exit_error = (0 != (master_table->flags & PHM_MasterTableFlag_ExitOnError));
138         rt_table->storage_size = master_table->storage_size;
139         return 0;
140 }
141
142 int phm_destroy_table(struct pp_hwmgr *hwmgr,
143                       struct phm_runtime_table_header *rt_table)
144 {
145         if (hwmgr == NULL || rt_table == NULL) {
146                 printk(KERN_ERR "[ powerplay ] Invalid Parameter\n");
147                 return -EINVAL;
148         }
149
150         if (NULL == rt_table->function_list)
151                 return 0;
152
153         kfree(rt_table->function_list);
154
155         rt_table->function_list = NULL;
156         rt_table->storage_size = 0;
157         rt_table->exit_error = false;
158
159         return 0;
160 }