Merge tag 'iwlwifi-for-kalle-2016-01-26_2' of https://git.kernel.org/pub/scm/linux...
[linux-2.6-block.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         for (function = rt_table->function_list; NULL != *function; function++) {
38                 int tmp = (*function)(hwmgr, input, output, temp_storage, result);
39
40                 if (tmp == PP_Result_TableImmediateExit)
41                         break;
42                 if (tmp) {
43                         if (0 == result)
44                                 result = tmp;
45                         if (rt_table->exit_error)
46                                 break;
47                 }
48         }
49
50         return result;
51 }
52
53 int phm_dispatch_table(struct pp_hwmgr *hwmgr,
54                        struct phm_runtime_table_header *rt_table,
55                        void *input, void *output)
56 {
57         int result = 0;
58         void *temp_storage = NULL;
59
60         if (hwmgr == NULL || rt_table == NULL || rt_table->function_list == NULL) {
61                 printk(KERN_ERR "[ powerplay ] Invalid Parameter!\n");
62                 return 0; /*temp return ture because some function not implement on some asic */
63         }
64
65         if (0 != rt_table->storage_size) {
66                 temp_storage = kzalloc(rt_table->storage_size, GFP_KERNEL);
67                 if (temp_storage == NULL) {
68                         printk(KERN_ERR "[ powerplay ] Could not allocate table temporary storage\n");
69                         return -ENOMEM;
70                 }
71         }
72
73         result = phm_run_table(hwmgr, rt_table, input, output, temp_storage);
74
75         if (NULL != temp_storage)
76                 kfree(temp_storage);
77
78         return result;
79 }
80
81 int phm_construct_table(struct pp_hwmgr *hwmgr,
82                         struct phm_master_table_header *master_table,
83                         struct phm_runtime_table_header *rt_table)
84 {
85         uint32_t function_count = 0;
86         const struct phm_master_table_item *table_item;
87         uint32_t size;
88         phm_table_function *run_time_list;
89         phm_table_function *rtf;
90
91         if (hwmgr == NULL || master_table == NULL || rt_table == NULL) {
92                 printk(KERN_ERR "[ powerplay ] Invalid Parameter!\n");
93                 return -EINVAL;
94         }
95
96         for (table_item = master_table->master_list;
97                 NULL != table_item->tableFunction; table_item++) {
98                 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
99                     (table_item->isFunctionNeededInRuntimeTable(hwmgr)))
100                         function_count++;
101         }
102
103         size = (function_count + 1) * sizeof(phm_table_function);
104         run_time_list = kzalloc(size, GFP_KERNEL);
105
106         if (NULL == run_time_list)
107                 return -ENOMEM;
108
109         rtf = run_time_list;
110         for (table_item = master_table->master_list;
111                 NULL != table_item->tableFunction; table_item++) {
112                 if ((rtf - run_time_list) > function_count) {
113                         printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
114                         kfree(run_time_list);
115                         return -EINVAL;
116                 }
117
118                 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
119                      (table_item->isFunctionNeededInRuntimeTable(hwmgr))) {
120                         *(rtf++) = table_item->tableFunction;
121                 }
122         }
123
124         if ((rtf - run_time_list) > function_count) {
125                 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
126                 kfree(run_time_list);
127                 return -EINVAL;
128         }
129
130         *rtf = NULL;
131         rt_table->function_list = run_time_list;
132         rt_table->exit_error = (0 != (master_table->flags & PHM_MasterTableFlag_ExitOnError));
133         rt_table->storage_size = master_table->storage_size;
134         return 0;
135 }
136
137 int phm_destroy_table(struct pp_hwmgr *hwmgr,
138                       struct phm_runtime_table_header *rt_table)
139 {
140         if (hwmgr == NULL || rt_table == NULL) {
141                 printk(KERN_ERR "[ powerplay ] Invalid Parameter\n");
142                 return -EINVAL;
143         }
144
145         if (NULL == rt_table->function_list)
146                 return 0;
147
148         kfree(rt_table->function_list);
149
150         rt_table->function_list = NULL;
151         rt_table->storage_size = 0;
152         rt_table->exit_error = false;
153
154         return 0;
155 }