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
phm_run_table(struct pp_hwmgr * hwmgr,struct phm_runtime_table_header * rt_table,void * input,void * output,void * temp_storage)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
phm_dispatch_table(struct pp_hwmgr * hwmgr,struct phm_runtime_table_header * rt_table,void * input,void * output)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;
63 void *temp_storage;
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 } else {
77 temp_storage = NULL;
78 }
79
80 result = phm_run_table(hwmgr, rt_table, input, output, temp_storage);
81
82 kfree(temp_storage);
83
84 return result;
85 }
86
phm_construct_table(struct pp_hwmgr * hwmgr,const struct phm_master_table_header * master_table,struct phm_runtime_table_header * rt_table)87 int phm_construct_table(struct pp_hwmgr *hwmgr,
88 const struct phm_master_table_header *master_table,
89 struct phm_runtime_table_header *rt_table)
90 {
91 uint32_t function_count = 0;
92 const struct phm_master_table_item *table_item;
93 uint32_t size;
94 phm_table_function *run_time_list;
95 phm_table_function *rtf;
96
97 if (hwmgr == NULL || master_table == NULL || rt_table == NULL) {
98 printk(KERN_ERR "[ powerplay ] Invalid Parameter!\n");
99 return -EINVAL;
100 }
101
102 for (table_item = master_table->master_list;
103 NULL != table_item->tableFunction; table_item++) {
104 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
105 (table_item->isFunctionNeededInRuntimeTable(hwmgr)))
106 function_count++;
107 }
108
109 size = (function_count + 1) * sizeof(phm_table_function);
110 run_time_list = kzalloc(size, GFP_KERNEL);
111
112 if (NULL == run_time_list)
113 return -ENOMEM;
114
115 rtf = run_time_list;
116 for (table_item = master_table->master_list;
117 NULL != table_item->tableFunction; table_item++) {
118 if ((rtf - run_time_list) > function_count) {
119 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
120 kfree(run_time_list);
121 return -EINVAL;
122 }
123
124 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
125 (table_item->isFunctionNeededInRuntimeTable(hwmgr))) {
126 *(rtf++) = table_item->tableFunction;
127 }
128 }
129
130 if ((rtf - run_time_list) > function_count) {
131 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
132 kfree(run_time_list);
133 return -EINVAL;
134 }
135
136 *rtf = NULL;
137 rt_table->function_list = run_time_list;
138 rt_table->exit_error = (0 != (master_table->flags & PHM_MasterTableFlag_ExitOnError));
139 rt_table->storage_size = master_table->storage_size;
140 return 0;
141 }
142
phm_destroy_table(struct pp_hwmgr * hwmgr,struct phm_runtime_table_header * rt_table)143 int phm_destroy_table(struct pp_hwmgr *hwmgr,
144 struct phm_runtime_table_header *rt_table)
145 {
146 if (hwmgr == NULL || rt_table == NULL) {
147 printk(KERN_ERR "[ powerplay ] Invalid Parameter\n");
148 return -EINVAL;
149 }
150
151 if (NULL == rt_table->function_list)
152 return 0;
153
154 kfree(rt_table->function_list);
155
156 rt_table->function_list = NULL;
157 rt_table->storage_size = 0;
158 rt_table->exit_error = false;
159
160 return 0;
161 }
162