functiontables.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. static int phm_run_table(struct pp_hwmgr *hwmgr,
  28. struct phm_runtime_table_header *rt_table,
  29. void *input,
  30. void *output,
  31. void *temp_storage)
  32. {
  33. int result = 0;
  34. phm_table_function *function;
  35. if (rt_table->function_list == NULL) {
  36. pr_debug("this function not implement!\n");
  37. return 0;
  38. }
  39. for (function = rt_table->function_list; NULL != *function; function++) {
  40. int tmp = (*function)(hwmgr, input, output, temp_storage, result);
  41. if (tmp == PP_Result_TableImmediateExit)
  42. break;
  43. if (tmp) {
  44. if (0 == result)
  45. result = tmp;
  46. if (rt_table->exit_error)
  47. break;
  48. }
  49. }
  50. return result;
  51. }
  52. int phm_dispatch_table(struct pp_hwmgr *hwmgr,
  53. struct phm_runtime_table_header *rt_table,
  54. void *input, void *output)
  55. {
  56. int result;
  57. void *temp_storage;
  58. if (hwmgr == NULL || rt_table == NULL) {
  59. pr_err("Invalid Parameter!\n");
  60. return -EINVAL;
  61. }
  62. if (0 != rt_table->storage_size) {
  63. temp_storage = kzalloc(rt_table->storage_size, GFP_KERNEL);
  64. if (temp_storage == NULL) {
  65. pr_err("Could not allocate table temporary storage\n");
  66. return -ENOMEM;
  67. }
  68. } else {
  69. temp_storage = NULL;
  70. }
  71. result = phm_run_table(hwmgr, rt_table, input, output, temp_storage);
  72. kfree(temp_storage);
  73. return result;
  74. }
  75. int phm_construct_table(struct pp_hwmgr *hwmgr,
  76. const struct phm_master_table_header *master_table,
  77. struct phm_runtime_table_header *rt_table)
  78. {
  79. uint32_t function_count = 0;
  80. const struct phm_master_table_item *table_item;
  81. uint32_t size;
  82. phm_table_function *run_time_list;
  83. phm_table_function *rtf;
  84. if (hwmgr == NULL || master_table == NULL || rt_table == NULL) {
  85. pr_err("Invalid Parameter!\n");
  86. return -EINVAL;
  87. }
  88. for (table_item = master_table->master_list;
  89. NULL != table_item->tableFunction; table_item++) {
  90. if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
  91. (table_item->isFunctionNeededInRuntimeTable(hwmgr)))
  92. function_count++;
  93. }
  94. size = (function_count + 1) * sizeof(phm_table_function);
  95. run_time_list = kzalloc(size, GFP_KERNEL);
  96. if (NULL == run_time_list)
  97. return -ENOMEM;
  98. rtf = run_time_list;
  99. for (table_item = master_table->master_list;
  100. NULL != table_item->tableFunction; table_item++) {
  101. if ((rtf - run_time_list) > function_count) {
  102. pr_err("Check function results have changed\n");
  103. kfree(run_time_list);
  104. return -EINVAL;
  105. }
  106. if ((NULL == table_item->isFunctionNeededInRuntimeTable) ||
  107. (table_item->isFunctionNeededInRuntimeTable(hwmgr))) {
  108. *(rtf++) = table_item->tableFunction;
  109. }
  110. }
  111. if ((rtf - run_time_list) > function_count) {
  112. pr_err("Check function results have changed\n");
  113. kfree(run_time_list);
  114. return -EINVAL;
  115. }
  116. *rtf = NULL;
  117. rt_table->function_list = run_time_list;
  118. rt_table->exit_error = (0 != (master_table->flags & PHM_MasterTableFlag_ExitOnError));
  119. rt_table->storage_size = master_table->storage_size;
  120. return 0;
  121. }
  122. int phm_destroy_table(struct pp_hwmgr *hwmgr,
  123. struct phm_runtime_table_header *rt_table)
  124. {
  125. if (hwmgr == NULL || rt_table == NULL) {
  126. pr_err("Invalid Parameter\n");
  127. return -EINVAL;
  128. }
  129. if (NULL == rt_table->function_list)
  130. return 0;
  131. kfree(rt_table->function_list);
  132. rt_table->function_list = NULL;
  133. rt_table->storage_size = 0;
  134. rt_table->exit_error = false;
  135. return 0;
  136. }