drm/amd/powerplay: add hardware manager sub-component
[deliverable/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 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 -1;
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 -1;
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 if (NULL == run_time_list)
106 return -1;
107
108 rtf = run_time_list;
109 for (table_item = master_table->master_list;
110 NULL != table_item->tableFunction; table_item++) {
111 if ((rtf - run_time_list) > function_count) {
112 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
113 kfree(run_time_list);
114 return -1;
115 }
116
117 if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
118 (table_item->isFunctionNeededInRuntimeTable(hwmgr))) {
119 *(rtf++) = table_item->tableFunction;
120 }
121 }
122
123 if ((rtf - run_time_list) > function_count) {
124 printk(KERN_ERR "[ powerplay ] Check function results have changed\n");
125 kfree(run_time_list);
126 return -1;
127 }
128
129 *rtf = NULL;
130 rt_table->function_list = run_time_list;
131 rt_table->exit_error = (0 != (master_table->flags & PHM_MasterTableFlag_ExitOnError));
132 rt_table->storage_size = master_table->storage_size;
133 return 0;
134 }
135
136 int phm_destroy_table(struct pp_hwmgr *hwmgr,
137 struct phm_runtime_table_header *rt_table)
138 {
139 if (hwmgr == NULL || rt_table == NULL) {
140 printk(KERN_ERR "[ powerplay ] Invalid Parameter\n");
141 return -1;
142 }
143
144 if (NULL == rt_table->function_list)
145 return 0;
146
147 kfree(rt_table->function_list);
148
149 rt_table->function_list = NULL;
150 rt_table->storage_size = 0;
151 rt_table->exit_error = false;
152
153 return 0;
154 }
This page took 0.037634 seconds and 5 git commands to generate.