switch_queue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/cpu.h>
  4. #include <linux/freezer.h>
  5. #include <linux/list.h>
  6. #include <linux/mutex.h>
  7. #include <linux/semaphore.h>
  8. #include <linux/kthread.h>
  9. #include <linux/delay.h>
  10. #include "nt_smc_call.h"
  11. #include "utdriver_macro.h"
  12. #include "sched_status.h"
  13. #define CAPI_CALL 0x01
  14. #define FDRV_CALL 0x02
  15. #define BDRV_CALL 0x03
  16. #define SCHED_CALL 0x04
  17. #define FP_SYS_NO 100
  18. #define VFS_SYS_NO 0x08
  19. #define REETIME_SYS_NO 0x07
  20. #define printk(fmt, args...) printk("\033[;34m[TEEI][TZDriver][switch_fn]"fmt"\033[0m", ##args)
  21. struct switch_head_struct
  22. {
  23. struct list_head head;
  24. };
  25. struct switch_call_struct
  26. {
  27. int switch_type;
  28. unsigned long buff_addr;
  29. };
  30. static void switch_fn(struct kthread_work *work);
  31. struct smc_call_struct {
  32. unsigned long local_cmd;
  33. u32 teei_cmd_type;
  34. u32 dev_file_id;
  35. u32 svc_id;
  36. u32 cmd_id;
  37. u32 context;
  38. u32 enc_id;
  39. void *cmd_buf;
  40. size_t cmd_len;
  41. void *resp_buf;
  42. size_t resp_len;
  43. void *meta_data;
  44. void *info_data;
  45. size_t info_len;
  46. int *ret_resp_len;
  47. int *error_code;
  48. struct semaphore *psema;
  49. int retVal;
  50. };
  51. extern struct kthread_worker ut_fastcall_worker;
  52. extern int forward_call_flag;
  53. extern int fp_call_flag;
  54. extern int teei_vfs_flag;
  55. extern int irq_call_flag;
  56. extern void nt_sched_t_call(void);
  57. extern int __send_fp_command(unsigned long share_memory_size);
  58. extern int __vfs_handle(struct service_handler *handler);
  59. extern int __reetime_handle(struct service_handler *handler);
  60. extern int __teei_smc_call(unsigned long local_smc_cmd,
  61. u32 teei_cmd_type,
  62. u32 dev_file_id,
  63. u32 svc_id,
  64. u32 cmd_id,
  65. u32 context,
  66. u32 enc_id,
  67. const void *cmd_buf,
  68. size_t cmd_len,
  69. void *resp_buf,
  70. size_t resp_len,
  71. const void *meta_data,
  72. const void *info_data,
  73. size_t info_len,
  74. int *ret_resp_len,
  75. int *error_code,
  76. struct semaphore *psema);
  77. static struct switch_call_struct *create_switch_call_struct(void)
  78. {
  79. struct switch_call_struct *tmp_entry = NULL;
  80. tmp_entry = kmalloc(sizeof(struct switch_call_struct), GFP_KERNEL);
  81. if (tmp_entry == NULL)
  82. printk("[%s][%d] kmalloc failed!!!\n", __func__, __LINE__);
  83. return tmp_entry;
  84. }
  85. static int init_switch_call_struct(struct switch_call_struct *ent, int work_type, unsigned long buff)
  86. {
  87. if ((ent == NULL)) {
  88. printk("[%s][%d] the paraments are wrong!\n", __func__, __LINE__);
  89. return -EINVAL;
  90. }
  91. ent->switch_type = work_type;
  92. ent->buff_addr = buff;
  93. return 0;
  94. }
  95. static int destroy_switch_call_struct(struct switch_call_struct *ent)
  96. {
  97. kfree(ent);
  98. return 0;
  99. }
  100. struct ut_smc_call_work {
  101. struct kthread_work work;
  102. void *data;
  103. };
  104. static int ut_smc_call(void *buff)
  105. {
  106. struct ut_smc_call_work usc_work = {
  107. KTHREAD_WORK_INIT(usc_work.work, switch_fn),
  108. .data = buff,
  109. };
  110. if (!queue_kthread_work(&ut_fastcall_worker, &usc_work.work))
  111. return -1;
  112. flush_kthread_work(&usc_work.work);
  113. return 0;
  114. }
  115. static int check_work_type(int work_type)
  116. {
  117. switch(work_type) {
  118. case CAPI_CALL:
  119. case FDRV_CALL:
  120. case BDRV_CALL:
  121. case SCHED_CALL:
  122. return 0;
  123. default:
  124. return -EINVAL;
  125. }
  126. }
  127. int add_work_entry(int work_type, unsigned long buff)
  128. {
  129. struct switch_call_struct *work_entry = NULL;
  130. int retVal = 0;
  131. retVal = check_work_type(work_type);
  132. if (retVal != 0) {
  133. printk("[%s][%d] with wrong work_type!\n", __func__, __LINE__);
  134. return retVal;
  135. }
  136. work_entry = create_switch_call_struct();
  137. if (work_entry == NULL) {
  138. printk("[%s][%d] There is no enough memory!\n", __func__, __LINE__);
  139. return -ENOMEM;
  140. }
  141. retVal = init_switch_call_struct(work_entry, work_type, buff);
  142. if (retVal != 0) {
  143. printk("[%s][%d] init_switch_call_struct failed!\n", __func__, __LINE__);
  144. destroy_switch_call_struct(work_entry);
  145. return retVal;
  146. }
  147. #if 0
  148. retVal = add_switch_call_link(&switch_queue_head, work_entry);
  149. #else
  150. retVal = ut_smc_call((void *)work_entry);
  151. #endif
  152. return retVal;
  153. }
  154. #if 0
  155. int del_work_entry(struct switch_call_struct *ent)
  156. {
  157. int retVal = 0;
  158. retVal = del_switch_call_link(ent);
  159. if (retVal != 0) {
  160. printk("[%s][%d] del_switch_call_link failed %d!\n", __func__, __LINE__, retVal);
  161. return retVal;
  162. }
  163. retVal = destroy_switch_call_struct(ent);
  164. if (retVal != 0) {
  165. printk("[%s][%d] destroy_switch_call_struct failed %d!\n", __func__, __LINE__, retVal);
  166. return retVal;
  167. }
  168. return 0;
  169. }
  170. #endif
  171. int get_call_type(struct switch_call_struct *ent)
  172. {
  173. if (ent == NULL)
  174. return -EINVAL;
  175. return ent->switch_type;
  176. }
  177. int handle_sched_call(void *buff)
  178. {
  179. nt_sched_t();
  180. return 0;
  181. }
  182. int handle_capi_call(void *buff)
  183. {
  184. struct smc_call_struct *cd = NULL;
  185. cd = (struct smc_call_struct *)buff;
  186. /* with a rmb() */
  187. rmb();
  188. cd->retVal = __teei_smc_call(cd->local_cmd,
  189. cd->teei_cmd_type,
  190. cd->dev_file_id,
  191. cd->svc_id,
  192. cd->cmd_id,
  193. cd->context,
  194. cd->enc_id,
  195. cd->cmd_buf,
  196. cd->cmd_len,
  197. cd->resp_buf,
  198. cd->resp_len,
  199. cd->meta_data,
  200. cd->info_data,
  201. cd->info_len,
  202. cd->ret_resp_len,
  203. cd->error_code,
  204. cd->psema);
  205. /* with a wmb() */
  206. wmb();
  207. return 0;
  208. }
  209. struct fdrv_call_struct {
  210. int fdrv_call_type;
  211. int fdrv_call_buff_size;
  212. int retVal;
  213. };
  214. int handle_fdrv_call(void *buff)
  215. {
  216. struct fdrv_call_struct *cd = NULL;
  217. cd = (struct fdrv_call_struct *)buff;
  218. /* with a rmb() */
  219. rmb();
  220. switch(cd->fdrv_call_type) {
  221. case FP_SYS_NO:
  222. cd->retVal = __send_fp_command(cd->fdrv_call_buff_size);
  223. break;
  224. default:
  225. cd->retVal = -EINVAL;
  226. }
  227. /* with a wmb() */
  228. wmb();
  229. return 0;
  230. }
  231. struct bdrv_call_struct {
  232. int bdrv_call_type;
  233. struct service_handler *handler;
  234. int retVal;
  235. };
  236. int handle_bdrv_call(void *buff)
  237. {
  238. struct bdrv_call_struct *cd = NULL;
  239. cd = (struct bdrv_call_struct *)buff;
  240. /* with a rmb() */
  241. rmb();
  242. switch(cd->bdrv_call_type) {
  243. case VFS_SYS_NO:
  244. cd->retVal = __vfs_handle(cd->handler);
  245. kfree(buff);
  246. break;
  247. case REETIME_SYS_NO:
  248. cd->retVal = __reetime_handle(cd->handler);
  249. kfree(buff);
  250. break;
  251. default:
  252. cd->retVal = -EINVAL;
  253. }
  254. /* with a wmb() */
  255. wmb();
  256. return 0;
  257. }
  258. int handle_switch_call(void *buff)
  259. {
  260. #if 0
  261. if (forward_call_flag == GLSCH_FOR_SOTER) {
  262. forward_call_flag = GLSCH_NONE;
  263. msleep(10);
  264. nt_sched_t_call();
  265. } else if (irq_call_flag == GLSCH_HIGH) {
  266. printk("[%s][%d]**************************\n", __func__, __LINE__ );
  267. irq_call_flag = GLSCH_NONE;
  268. nt_sched_t_call();
  269. /*msleep_interruptible(10);*/
  270. } else if (fp_call_flag == GLSCH_HIGH) {
  271. printk("[%s][%d]**************************\n", __func__, __LINE__ );
  272. if (teei_vfs_flag == 0) {
  273. nt_sched_t_call();
  274. } else {
  275. msleep_interruptible(1);
  276. }
  277. } else if (forward_call_flag == GLSCH_LOW) {
  278. printk("[%s][%d]**************************\n", __func__, __LINE__ );
  279. if (teei_vfs_flag == 0) {
  280. nt_sched_t_call();
  281. } else {
  282. msleep_interruptible(1);
  283. }
  284. } else {
  285. printk("[%s][%d]**************************\n", __func__, __LINE__ );
  286. msleep_interruptible(1);
  287. }
  288. #else
  289. nt_sched_t();
  290. #endif
  291. }
  292. static void switch_fn(struct kthread_work *work)
  293. {
  294. struct ut_smc_call_work *switch_work = NULL;
  295. struct switch_call_struct *switch_ent = NULL;
  296. int call_type = 0;
  297. int retVal = 0;
  298. switch_work = container_of(work, struct ut_smc_call_work, work);
  299. switch_ent = (struct switch_call_struct *)switch_work->data;
  300. call_type = get_call_type(switch_ent);
  301. switch (call_type) {
  302. case CAPI_CALL:
  303. retVal = handle_capi_call(switch_ent->buff_addr);
  304. if (retVal < 0) {
  305. printk("[%s][%d] fail to handle ClientAPI!\n", __func__, __LINE__);
  306. }
  307. break;
  308. case FDRV_CALL:
  309. retVal = handle_fdrv_call(switch_ent->buff_addr);
  310. if (retVal < 0) {
  311. printk("[%s][%d] fail to handle F-driver!\n", __func__, __LINE__);
  312. }
  313. break;
  314. case BDRV_CALL:
  315. retVal = handle_bdrv_call(switch_ent->buff_addr);
  316. if (retVal < 0) {
  317. printk("[%s][%d] fail to handle B-driver!\n", __func__, __LINE__);
  318. }
  319. break;
  320. case SCHED_CALL:
  321. retVal = handle_sched_call(switch_ent->buff_addr);
  322. if (retVal < 0) {
  323. printk("[%s][%d] fail to handle sched-Call!\n", __func__, __LINE__);
  324. }
  325. break;
  326. default:
  327. printk("switch fn handles a undefined call! [%d] \n", call_type);
  328. }
  329. retVal = destroy_switch_call_struct(switch_ent);
  330. if (retVal != 0) {
  331. printk("[%s][%d] destroy_switch_call_struct failed %d!\n", __func__, __LINE__, retVal);
  332. return retVal;
  333. }
  334. return 0;
  335. }