tz_mod.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/types.h>
  4. #include <linux/kernel.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/cdev.h>
  7. #include <linux/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ioctl.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/kthread.h>
  14. #include <linux/freezer.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include "trustzone/kree/tz_mod.h"
  20. #include "trustzone/kree/mem.h"
  21. #include "trustzone/kree/system.h"
  22. #include "trustzone/kree/tz_pm.h"
  23. #include "trustzone/kree/tz_irq.h"
  24. #include "kree_int.h"
  25. #include "tz_counter.h"
  26. #include "tz_cross/ta_pm.h"
  27. #include "tz_ndbg.h"
  28. #include "tz_playready.h"
  29. #include "tz_secure_clock.h"
  30. #define MTEE_MOD_TAG "MTEE_MOD"
  31. #define TZ_PAGESIZE 0x1000 /* fix me!!!! need global define */
  32. #define PAGE_SHIFT 12 /* fix me!!!! need global define */
  33. #define TZ_DEVNAME "mtk_tz"
  34. /**Used for mapping user space address to physical memory
  35. */
  36. struct MTIOMMU_PIN_RANGE_T {
  37. void *start;
  38. void *pageArray;
  39. uint32_t size;
  40. uint32_t nrPages;
  41. uint32_t isPage;
  42. };
  43. /*****************************************************************************
  44. * FUNCTION DEFINITION
  45. *****************************************************************************/
  46. static struct cdev tz_client_cdev;
  47. static dev_t tz_client_dev;
  48. static int tz_client_open(struct inode *inode, struct file *filp);
  49. static int tz_client_release(struct inode *inode, struct file *filp);
  50. static long tz_client_ioctl(struct file *file, unsigned int cmd,
  51. unsigned long arg);
  52. #ifdef CONFIG_COMPAT
  53. static long tz_client_ioctl_compat(struct file *file, unsigned int cmd,
  54. unsigned long arg);
  55. #endif
  56. static int tz_client_init_client_info(struct file *file);
  57. static void tz_client_free_client_info(struct file *file);
  58. #define TZ_CLIENT_INIT_HANDLE_SPACE 32
  59. #define TZ_CLIENT_INIT_SHAREDMEM_SPACE 32
  60. struct tz_sharedmem_info {
  61. KREE_SHAREDMEM_HANDLE mem_handle;
  62. KREE_SESSION_HANDLE session_handle;
  63. uint32_t *resouce;
  64. };
  65. struct tz_client_info {
  66. int handle_num;
  67. struct mutex mux;
  68. KREE_SESSION_HANDLE *handles;
  69. struct tz_sharedmem_info *shm_info;
  70. int shm_info_num;
  71. };
  72. static const struct file_operations tz_client_fops = {
  73. .open = tz_client_open,
  74. .release = tz_client_release,
  75. .unlocked_ioctl = tz_client_ioctl,
  76. #ifdef CONFIG_COMPAT
  77. .compat_ioctl = tz_client_ioctl_compat,
  78. #endif
  79. .owner = THIS_MODULE,
  80. };
  81. static int tz_client_open(struct inode *inode, struct file *filp)
  82. {
  83. return tz_client_init_client_info(filp);
  84. }
  85. static int tz_client_release(struct inode *inode, struct file *filp)
  86. {
  87. tz_client_free_client_info(filp);
  88. return 0;
  89. }
  90. /* map user space pages */
  91. /* control -> 0 = write, 1 = read only memory */
  92. static long _map_user_pages(struct MTIOMMU_PIN_RANGE_T *pinRange,
  93. unsigned long uaddr, uint32_t size,
  94. uint32_t control)
  95. {
  96. int nr_pages;
  97. unsigned long first, last;
  98. struct page **pages;
  99. struct vm_area_struct *vma;
  100. int res, j;
  101. uint32_t write;
  102. if ((uaddr == 0) || (size == 0))
  103. return -EFAULT;
  104. pinRange->start = (void *)uaddr;
  105. pinRange->size = size;
  106. first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
  107. last = ((uaddr + size + PAGE_SIZE - 1) & PAGE_MASK) >> PAGE_SHIFT;
  108. nr_pages = last - first;
  109. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  110. if (pages == NULL)
  111. return -ENOMEM;
  112. pinRange->pageArray = (void *) pages;
  113. write = (control == 0) ? 1 : 0;
  114. /* Try to fault in all of the necessary pages */
  115. down_read(&current->mm->mmap_sem);
  116. vma = find_vma_intersection(current->mm, uaddr, uaddr+size);
  117. if (!vma) {
  118. res = -EFAULT;
  119. goto out;
  120. }
  121. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
  122. pinRange->isPage = 1;
  123. res = get_user_pages(current, current->mm, uaddr, nr_pages,
  124. write, 0,/* don't force */
  125. pages, NULL);
  126. } else {
  127. /* pfn mapped memory, don't touch page struct.
  128. the buffer manager (possibly ion) should make sure
  129. it won't be used for anything else */
  130. pinRange->isPage = 0;
  131. res = 0;
  132. do {
  133. unsigned long *pfns = (void *)pages;
  134. while (res < nr_pages &&
  135. uaddr + PAGE_SIZE <= vma->vm_end) {
  136. j = follow_pfn(vma, uaddr, &pfns[res]);
  137. if (j) { /* error */
  138. res = j;
  139. goto out;
  140. }
  141. uaddr += PAGE_SIZE;
  142. res++;
  143. }
  144. if (res >= nr_pages || uaddr < vma->vm_end)
  145. break;
  146. vma = find_vma_intersection(current->mm, uaddr,
  147. uaddr+1);
  148. } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
  149. }
  150. out:
  151. up_read(&current->mm->mmap_sem);
  152. if (res < 0) {
  153. pr_warn("_map_user_pages error = %d\n", res);
  154. goto out_free;
  155. }
  156. pinRange->nrPages = res;
  157. /* Errors and no page mapped should return here */
  158. if (res < nr_pages)
  159. goto out_unmap;
  160. return 0;
  161. out_unmap:
  162. pr_warn("_map_user_pages fail\n");
  163. if (pinRange->isPage) {
  164. for (j = 0; j < res; j++)
  165. put_page(pages[j]);
  166. }
  167. res = -EFAULT;
  168. out_free:
  169. kfree(pages);
  170. return res;
  171. }
  172. static void _unmap_user_pages(struct MTIOMMU_PIN_RANGE_T *pinRange)
  173. {
  174. int res;
  175. int j;
  176. struct page **pages;
  177. pages = (struct page **)pinRange->pageArray;
  178. if (pinRange->isPage) {
  179. res = pinRange->nrPages;
  180. if (res > 0) {
  181. for (j = 0; j < res; j++)
  182. put_page(pages[j]);
  183. res = 0;
  184. }
  185. }
  186. kfree(pages);
  187. }
  188. static struct tz_sharedmem_info *tz_get_sharedmem(struct tz_client_info *info,
  189. KREE_SHAREDMEM_HANDLE handle)
  190. {
  191. struct tz_sharedmem_info *shm_info;
  192. int i;
  193. /* search handle */
  194. shm_info = NULL;
  195. for (i = 0; i < info->shm_info_num; i++) {
  196. if (info->shm_info[i].mem_handle == handle) {
  197. shm_info = &info->shm_info[i];
  198. break;
  199. }
  200. }
  201. return shm_info;
  202. }
  203. /**************************************************************************
  204. * DEV tz_client_info handling
  205. **************************************************************************/
  206. static int tz_client_register_session(struct file *file,
  207. KREE_SESSION_HANDLE handle)
  208. {
  209. struct tz_client_info *info;
  210. int i, num, nspace, ret = -1;
  211. void *ptr;
  212. info = (struct tz_client_info *)file->private_data;
  213. /* lock */
  214. mutex_lock(&info->mux);
  215. /* find empty space. */
  216. num = info->handle_num;
  217. for (i = 0; i < num; i++) {
  218. if (info->handles[i] == 0) {
  219. ret = i;
  220. break;
  221. }
  222. }
  223. if (ret == -1) {
  224. /* Try grow the space */
  225. nspace = num * 2;
  226. ptr = krealloc(info->handles,
  227. nspace * sizeof(KREE_SESSION_HANDLE),
  228. GFP_KERNEL);
  229. if (ptr == 0) {
  230. mutex_unlock(&info->mux);
  231. return -ENOMEM;
  232. }
  233. ret = num;
  234. info->handle_num = nspace;
  235. info->handles = (KREE_SESSION_HANDLE *) ptr;
  236. memset(&info->handles[num], 0,
  237. (nspace - num) * sizeof(KREE_SESSION_HANDLE));
  238. }
  239. if (ret >= 0)
  240. info->handles[ret] = handle;
  241. /* unlock */
  242. mutex_unlock(&info->mux);
  243. return ret + 1;
  244. }
  245. static KREE_SESSION_HANDLE tz_client_get_session(struct file *file, int handle)
  246. {
  247. struct tz_client_info *info;
  248. KREE_SESSION_HANDLE rhandle;
  249. info = (struct tz_client_info *)file->private_data;
  250. /* lock */
  251. mutex_lock(&info->mux);
  252. if (handle <= 0 || handle > info->handle_num)
  253. rhandle = (KREE_SESSION_HANDLE) 0;
  254. else
  255. rhandle = info->handles[handle - 1];
  256. /* unlock */
  257. mutex_unlock(&info->mux);
  258. return rhandle;
  259. }
  260. static int tz_client_unregister_session(struct file *file, int handle)
  261. {
  262. struct tz_client_info *info;
  263. int ret = 0;
  264. info = (struct tz_client_info *)file->private_data;
  265. /* lock */
  266. mutex_lock(&info->mux);
  267. if (handle <= 0 || handle > info->handle_num ||
  268. info->handles[handle - 1] == 0)
  269. ret = -EINVAL;
  270. else
  271. info->handles[handle - 1] = (KREE_SESSION_HANDLE) 0;
  272. /* unlock */
  273. mutex_unlock(&info->mux);
  274. return ret;
  275. }
  276. static int
  277. tz_client_register_sharedmem(struct file *file, KREE_SESSION_HANDLE handle,
  278. KREE_SHAREDMEM_HANDLE mem_handle,
  279. uint32_t *resource)
  280. {
  281. struct tz_client_info *info;
  282. int i, num, nspace, ret = -1;
  283. void *ptr;
  284. info = (struct tz_client_info *)file->private_data;
  285. /* lock */
  286. mutex_lock(&info->mux);
  287. /* find empty space. */
  288. num = info->shm_info_num;
  289. for (i = 0; i < num; i++) {
  290. if ((int)info->shm_info[i].mem_handle == 0) {
  291. ret = i;
  292. break;
  293. }
  294. }
  295. if (ret == -1) {
  296. /* Try grow the space */
  297. nspace = num * 2;
  298. ptr = krealloc(info->shm_info,
  299. nspace * sizeof(struct tz_sharedmem_info),
  300. GFP_KERNEL);
  301. if (ptr == 0) {
  302. mutex_unlock(&info->mux);
  303. return -ENOMEM;
  304. }
  305. ret = num;
  306. info->shm_info_num = nspace;
  307. info->shm_info = (struct tz_sharedmem_info *)ptr;
  308. memset(&info->shm_info[num], 0,
  309. (nspace - num) * sizeof(struct tz_sharedmem_info));
  310. }
  311. if (ret >= 0) {
  312. info->shm_info[ret].mem_handle = mem_handle;
  313. info->shm_info[ret].session_handle = handle;
  314. info->shm_info[ret].resouce = resource;
  315. }
  316. /* unlock */
  317. mutex_unlock(&info->mux);
  318. return ret;
  319. }
  320. static int tz_client_unregister_sharedmem(struct file *file,
  321. KREE_SHAREDMEM_HANDLE handle)
  322. {
  323. struct tz_client_info *info;
  324. struct tz_sharedmem_info *shm_info;
  325. struct MTIOMMU_PIN_RANGE_T *pin;
  326. int ret = 0;
  327. info = (struct tz_client_info *)file->private_data;
  328. /* lock */
  329. mutex_lock(&info->mux);
  330. shm_info = tz_get_sharedmem(info, handle);
  331. if ((shm_info == NULL) || (shm_info->mem_handle == 0))
  332. ret = -EINVAL;
  333. else {
  334. pin = (struct MTIOMMU_PIN_RANGE_T *) shm_info->resouce;
  335. _unmap_user_pages(pin);
  336. kfree(pin);
  337. memset(shm_info, 0, sizeof(struct tz_sharedmem_info));
  338. }
  339. /* unlock */
  340. mutex_unlock(&info->mux);
  341. return ret;
  342. }
  343. static int tz_client_init_client_info(struct file *file)
  344. {
  345. struct tz_client_info *info;
  346. info = (struct tz_client_info *)
  347. kmalloc(sizeof(struct tz_client_info), GFP_KERNEL);
  348. if (!info)
  349. return -ENOMEM;
  350. info->handles = kzalloc(TZ_CLIENT_INIT_HANDLE_SPACE *
  351. sizeof(KREE_SESSION_HANDLE), GFP_KERNEL);
  352. if (!info->handles) {
  353. kfree(info);
  354. return -ENOMEM;
  355. }
  356. info->handle_num = TZ_CLIENT_INIT_HANDLE_SPACE;
  357. /* init shared memory */
  358. info->shm_info = kzalloc(TZ_CLIENT_INIT_SHAREDMEM_SPACE *
  359. sizeof(struct tz_sharedmem_info), GFP_KERNEL);
  360. if (!info->shm_info) {
  361. kfree(info->handles);
  362. kfree(info);
  363. return -ENOMEM;
  364. }
  365. info->shm_info_num = TZ_CLIENT_INIT_SHAREDMEM_SPACE;
  366. mutex_init(&info->mux);
  367. file->private_data = info;
  368. return 0;
  369. }
  370. static void tz_client_free_client_info(struct file *file)
  371. {
  372. struct tz_client_info *info;
  373. struct tz_sharedmem_info *shm_info;
  374. struct MTIOMMU_PIN_RANGE_T *pin;
  375. int i, num;
  376. info = (struct tz_client_info *)file->private_data;
  377. /* lock */
  378. mutex_lock(&info->mux);
  379. num = info->handle_num;
  380. for (i = 0; i < num; i++) {
  381. if (info->handles[i] == 0)
  382. continue;
  383. KREE_CloseSession(info->handles[i]);
  384. info->handles[i] = (KREE_SESSION_HANDLE) 0;
  385. }
  386. /* unregister shared memory */
  387. num = info->shm_info_num;
  388. for (i = 0; i < num; i++) {
  389. if (info->shm_info[i].mem_handle == 0)
  390. continue;
  391. shm_info = tz_get_sharedmem(info, info->shm_info[i].mem_handle);
  392. if (shm_info == NULL)
  393. continue;
  394. pin = (struct MTIOMMU_PIN_RANGE_T *) shm_info->resouce;
  395. _unmap_user_pages(pin);
  396. kfree(pin);
  397. }
  398. /* unlock */
  399. file->private_data = 0;
  400. mutex_unlock(&info->mux);
  401. kfree(info->handles);
  402. kfree(info->shm_info);
  403. kfree(info);
  404. }
  405. /**************************************************************************
  406. * DEV DRIVER IOCTL
  407. **************************************************************************/
  408. static long tz_client_open_session(struct file *file, unsigned long arg)
  409. {
  410. struct kree_session_cmd_param param;
  411. unsigned long cret;
  412. char uuid[40];
  413. long len;
  414. TZ_RESULT ret;
  415. KREE_SESSION_HANDLE handle;
  416. cret = copy_from_user(&param, (void *)arg, sizeof(param));
  417. if (cret)
  418. return -EFAULT;
  419. /* Check if can we access UUID string. 10 for min uuid len. */
  420. if (!access_ok(VERIFY_READ, param.data, 10))
  421. return -EFAULT;
  422. len = strncpy_from_user(uuid,
  423. (void *)(unsigned long)param.data,
  424. sizeof(uuid));
  425. if (len <= 0)
  426. return -EFAULT;
  427. uuid[sizeof(uuid) - 1] = 0;
  428. ret = KREE_CreateSession(uuid, &handle);
  429. param.ret = ret;
  430. /* Register session to fd */
  431. if (ret == TZ_RESULT_SUCCESS) {
  432. param.handle = tz_client_register_session(file, handle);
  433. if (param.handle < 0)
  434. goto error_register;
  435. }
  436. cret = copy_to_user((void *)arg, &param, sizeof(param));
  437. if (cret)
  438. goto error_copy;
  439. return 0;
  440. error_copy:
  441. tz_client_unregister_session(file, param.handle);
  442. error_register:
  443. KREE_CloseSession(handle);
  444. return -EFAULT;
  445. }
  446. static long tz_client_close_session(struct file *file, unsigned long arg)
  447. {
  448. struct kree_session_cmd_param param;
  449. unsigned long cret;
  450. TZ_RESULT ret;
  451. KREE_SESSION_HANDLE handle;
  452. cret = copy_from_user(&param, (void *)arg, sizeof(param));
  453. if (cret)
  454. return -EFAULT;
  455. handle = tz_client_get_session(file, param.handle);
  456. if (handle == 0)
  457. return -EINVAL;
  458. tz_client_unregister_session(file, param.handle);
  459. ret = KREE_CloseSession(handle);
  460. param.ret = ret;
  461. cret = copy_to_user((void *)arg, &param, sizeof(param));
  462. if (cret)
  463. return -EFAULT;
  464. return 0;
  465. }
  466. static long tz_client_tee_service(struct file *file, unsigned long arg,
  467. unsigned int compat)
  468. {
  469. struct kree_tee_service_cmd_param cparam;
  470. unsigned long cret;
  471. uint32_t tmpTypes;
  472. MTEEC_PARAM param[4], oparam[4];
  473. int i;
  474. TZ_RESULT ret;
  475. KREE_SESSION_HANDLE handle;
  476. void __user *ubuf;
  477. uint32_t ubuf_sz;
  478. cret = copy_from_user(&cparam, (void *)arg, sizeof(cparam));
  479. if (cret)
  480. return -EFAULT;
  481. if (cparam.paramTypes != TZPT_NONE || cparam.param) {
  482. /* Check if can we access param */
  483. if (!access_ok(VERIFY_READ, cparam.param, sizeof(oparam)))
  484. return -EFAULT;
  485. cret = copy_from_user(oparam,
  486. (void *)(unsigned long)cparam.param,
  487. sizeof(oparam));
  488. if (cret)
  489. return -EFAULT;
  490. }
  491. /* Check handle */
  492. handle = tz_client_get_session(file, cparam.handle);
  493. if (handle <= 0)
  494. return -EINVAL;
  495. /* Parameter processing. */
  496. memset(param, 0, sizeof(param));
  497. tmpTypes = cparam.paramTypes;
  498. for (i = 0; tmpTypes; i++) {
  499. TZ_PARAM_TYPES type = tmpTypes & 0xff;
  500. tmpTypes >>= 8;
  501. switch (type) {
  502. case TZPT_VALUE_INPUT:
  503. case TZPT_VALUE_INOUT:
  504. param[i] = oparam[i];
  505. break;
  506. case TZPT_VALUE_OUTPUT:
  507. case TZPT_NONE:
  508. /* Nothing to do */
  509. break;
  510. case TZPT_MEM_INPUT:
  511. case TZPT_MEM_OUTPUT:
  512. case TZPT_MEM_INOUT:
  513. #ifdef CONFIG_COMPAT
  514. if (compat) {
  515. ubuf = compat_ptr(oparam[i].mem32.buffer);
  516. ubuf_sz = oparam[i].mem32.size;
  517. } else
  518. #endif
  519. {
  520. ubuf = oparam[i].mem.buffer;
  521. ubuf_sz = oparam[i].mem.size;
  522. }
  523. /* Mem Access check */
  524. if (type != TZPT_MEM_OUTPUT) {
  525. if (!access_ok(VERIFY_READ, ubuf,
  526. ubuf_sz)) {
  527. cret = -EFAULT;
  528. goto error;
  529. }
  530. }
  531. if (type != TZPT_MEM_INPUT) {
  532. if (!access_ok(VERIFY_WRITE, ubuf,
  533. ubuf_sz)) {
  534. cret = -EFAULT;
  535. goto error;
  536. }
  537. }
  538. /* Allocate kernel space memory. Fail if > 4kb */
  539. if (ubuf_sz > TEE_PARAM_MEM_LIMIT) {
  540. cret = -ENOMEM;
  541. goto error;
  542. }
  543. param[i].mem.size = ubuf_sz;
  544. param[i].mem.buffer = kmalloc(param[i].mem.size,
  545. GFP_KERNEL);
  546. if (!param[i].mem.buffer) {
  547. cret = -ENOMEM;
  548. goto error;
  549. }
  550. if (type != TZPT_MEM_OUTPUT) {
  551. cret = copy_from_user(param[i].mem.buffer,
  552. ubuf,
  553. param[i].mem.size);
  554. if (cret) {
  555. cret = -EFAULT;
  556. goto error;
  557. }
  558. }
  559. break;
  560. case TZPT_MEMREF_INPUT:
  561. case TZPT_MEMREF_OUTPUT:
  562. case TZPT_MEMREF_INOUT:
  563. /* Check if share memory is valid. */
  564. /* Not done yet. */
  565. param[i] = oparam[i];
  566. break;
  567. default:
  568. /* Bad format, return. */
  569. ret = TZ_RESULT_ERROR_BAD_FORMAT;
  570. goto error;
  571. }
  572. }
  573. /* Execute. */
  574. ret = KREE_TeeServiceCallNoCheck(handle, cparam.command,
  575. cparam.paramTypes, param);
  576. /* Copy memory back. */
  577. cparam.ret = ret;
  578. tmpTypes = cparam.paramTypes;
  579. for (i = 0; tmpTypes; i++) {
  580. TZ_PARAM_TYPES type = tmpTypes & 0xff;
  581. tmpTypes >>= 8;
  582. switch (type) {
  583. case TZPT_VALUE_OUTPUT:
  584. case TZPT_VALUE_INOUT:
  585. oparam[i] = param[i];
  586. break;
  587. default:
  588. /* This should never happen */
  589. case TZPT_MEMREF_INPUT:
  590. case TZPT_MEMREF_OUTPUT:
  591. case TZPT_MEMREF_INOUT:
  592. case TZPT_VALUE_INPUT:
  593. case TZPT_NONE:
  594. /* Nothing to do */
  595. break;
  596. case TZPT_MEM_INPUT:
  597. case TZPT_MEM_OUTPUT:
  598. case TZPT_MEM_INOUT:
  599. #ifdef CONFIG_COMPAT
  600. if (compat)
  601. ubuf = compat_ptr(oparam[i].mem32.buffer);
  602. else
  603. #endif
  604. ubuf = oparam[i].mem.buffer;
  605. if (type != TZPT_MEM_INPUT) {
  606. cret = copy_to_user(ubuf,
  607. param[i].mem.buffer,
  608. param[i].mem.size);
  609. if (cret) {
  610. cret = -EFAULT;
  611. goto error;
  612. }
  613. }
  614. kfree(param[i].mem.buffer);
  615. break;
  616. }
  617. }
  618. /* Copy data back. */
  619. if (cparam.paramTypes != TZPT_NONE) {
  620. cret = copy_to_user((void *)(unsigned long)cparam.param,
  621. oparam,
  622. sizeof(oparam));
  623. if (cret)
  624. return -EFAULT;
  625. }
  626. cret = copy_to_user((void *)arg, &cparam, sizeof(cparam));
  627. if (cret)
  628. return -EFAULT;
  629. return 0;
  630. error:
  631. tmpTypes = cparam.paramTypes;
  632. for (i = 0; tmpTypes; i++) {
  633. TZ_PARAM_TYPES type = tmpTypes & 0xff;
  634. tmpTypes >>= 8;
  635. switch (type) {
  636. case TZPT_MEM_INPUT:
  637. case TZPT_MEM_OUTPUT:
  638. case TZPT_MEM_INOUT:
  639. kfree(param[i].mem.buffer);
  640. break;
  641. default:
  642. /* Don't care. */
  643. break;
  644. }
  645. }
  646. return cret;
  647. }
  648. static long tz_client_reg_sharedmem(struct file *file, unsigned long arg)
  649. {
  650. unsigned long cret;
  651. struct kree_sharedmemory_cmd_param cparam;
  652. KREE_SESSION_HANDLE session;
  653. uint32_t mem_handle;
  654. struct MTIOMMU_PIN_RANGE_T *pin;
  655. uint32_t *map_p;
  656. TZ_RESULT ret;
  657. struct page **page;
  658. int i;
  659. long errcode;
  660. unsigned long *pfns;
  661. cret = copy_from_user(&cparam, (void *)arg, sizeof(cparam));
  662. if (cret)
  663. return -EFAULT;
  664. /* session handle */
  665. session = tz_client_get_session(file, cparam.session);
  666. if (session <= 0)
  667. return -EINVAL;
  668. /* map pages
  669. */
  670. /* 1. get user pages */
  671. /* note: 'pin' resource need to keep for unregister.
  672. * It will be freed after unregisted. */
  673. pin = kzalloc(sizeof(struct MTIOMMU_PIN_RANGE_T), GFP_KERNEL);
  674. if (pin == NULL) {
  675. errcode = -ENOMEM;
  676. goto client_regshm_mapfail;
  677. }
  678. cret = _map_user_pages(pin, (unsigned long)cparam.buffer,
  679. cparam.size, cparam.control);
  680. if (cret != 0) {
  681. pr_warn("tz_client_reg_sharedmem fail: map user pages = 0x%x\n",
  682. (uint32_t) cret);
  683. errcode = -EFAULT;
  684. goto client_regshm_mapfail_1;
  685. }
  686. /* 2. build PA table */
  687. map_p = kzalloc(sizeof(uint32_t) * (pin->nrPages + 1), GFP_KERNEL);
  688. if (map_p == NULL) {
  689. errcode = -ENOMEM;
  690. goto client_regshm_mapfail_2;
  691. }
  692. map_p[0] = pin->nrPages;
  693. if (pin->isPage) {
  694. page = (struct page **)pin->pageArray;
  695. for (i = 0; i < pin->nrPages; i++) {
  696. map_p[1 + i] = PFN_PHYS(page_to_pfn(page[i])); /* PA */
  697. /* pr_debug("tz_client_reg_sharedmem ---> 0x%x\n",
  698. map_p[1+i]); */
  699. }
  700. } else { /* pfn */
  701. pfns = (unsigned long *)pin->pageArray;
  702. for (i = 0; i < pin->nrPages; i++) {
  703. map_p[1 + i] = PFN_PHYS(pfns[i]); /* get PA */
  704. /* pr_debug("tz_client_reg_sharedmem ---> 0x%x\n",
  705. map_p[1+i]); */
  706. }
  707. }
  708. /* register it ...
  709. */
  710. ret = kree_register_sharedmem(session, &mem_handle, pin->start,
  711. pin->size, (void *)map_p);
  712. if (ret != TZ_RESULT_SUCCESS) {
  713. pr_warn("tz_client_reg_sharedmem fail: register shm 0x%x\n",
  714. ret);
  715. kfree(map_p);
  716. errcode = -EFAULT;
  717. goto client_regshm_free;
  718. }
  719. kfree(map_p);
  720. /* register to fd
  721. */
  722. cret = tz_client_register_sharedmem(file, session,
  723. (KREE_SHAREDMEM_HANDLE) mem_handle,
  724. (uint32_t *) pin);
  725. if (cret < 0) {
  726. pr_warn("tz_client_reg_sharedmem fail: register fd 0x%x\n",
  727. (uint32_t) cret);
  728. errcode = -EFAULT;
  729. goto client_regshm_free_1;
  730. }
  731. cparam.mem_handle = mem_handle;
  732. cparam.ret = ret; /* TEE service call return */
  733. cret = copy_to_user((void *)arg, &cparam, sizeof(cparam));
  734. if (cret)
  735. return -EFAULT;
  736. return TZ_RESULT_SUCCESS;
  737. client_regshm_free_1:
  738. kree_unregister_sharedmem(session, mem_handle);
  739. client_regshm_free:
  740. _unmap_user_pages(pin);
  741. kfree(pin);
  742. cparam.ret = ret; /* TEE service call return */
  743. cret = copy_to_user((void *)arg, &cparam, sizeof(cparam));
  744. pr_warn("tz_client_reg_sharedmem fail: shm reg\n");
  745. return errcode;
  746. client_regshm_mapfail_2:
  747. _unmap_user_pages(pin);
  748. client_regshm_mapfail_1:
  749. kfree(pin);
  750. client_regshm_mapfail:
  751. pr_warn("tz_client_reg_sharedmem fail: map memory\n");
  752. return errcode;
  753. }
  754. static long tz_client_unreg_sharedmem(struct file *file, unsigned long arg)
  755. {
  756. unsigned long cret;
  757. struct kree_sharedmemory_cmd_param cparam;
  758. KREE_SESSION_HANDLE session;
  759. TZ_RESULT ret;
  760. cret = copy_from_user(&cparam, (void *)arg, sizeof(cparam));
  761. if (cret)
  762. return -EFAULT;
  763. /* session handle */
  764. session = tz_client_get_session(file, cparam.session);
  765. if (session <= 0)
  766. return -EINVAL;
  767. /* Unregister
  768. */
  769. ret = kree_unregister_sharedmem(session, (uint32_t) cparam.mem_handle);
  770. if (ret != TZ_RESULT_SUCCESS) {
  771. pr_warn("tz_client_unreg_sharedmem: 0x%x\n", ret);
  772. cparam.ret = ret;
  773. cret = copy_to_user((void *)arg, &cparam, sizeof(cparam));
  774. return -EFAULT;
  775. }
  776. /* unmap user pages and unregister to fd
  777. */
  778. ret = tz_client_unregister_sharedmem(file, cparam.mem_handle);
  779. if (ret != TZ_RESULT_SUCCESS) {
  780. pr_warn("tz_client_unreg_sharedmem: unregister shm = 0x%x\n",
  781. ret);
  782. return -EFAULT;
  783. }
  784. cparam.ret = ret;
  785. cret = copy_to_user((void *)arg, &cparam, sizeof(cparam));
  786. if (cret)
  787. return -EFAULT;
  788. return TZ_RESULT_SUCCESS;
  789. }
  790. static long do_tz_client_ioctl(struct file *file, unsigned int cmd,
  791. unsigned long arg, unsigned int compat)
  792. {
  793. int err = 0;
  794. /* ---------------------------------- */
  795. /* IOCTL */
  796. /* ---------------------------------- */
  797. if (_IOC_TYPE(cmd) != MTEE_IOC_MAGIC)
  798. return -ENOTTY;
  799. if (_IOC_NR(cmd) > DEV_IOC_MAXNR)
  800. return -ENOTTY;
  801. if (_IOC_DIR(cmd) & _IOC_READ)
  802. err |= !access_ok(VERIFY_WRITE, (void __user *)arg,
  803. _IOC_SIZE(cmd));
  804. if (_IOC_DIR(cmd) & _IOC_WRITE)
  805. err |= !access_ok(VERIFY_READ, (void __user *)arg,
  806. _IOC_SIZE(cmd));
  807. if (err)
  808. return -EFAULT;
  809. switch (cmd) {
  810. case MTEE_CMD_OPEN_SESSION:
  811. return tz_client_open_session(file, arg);
  812. case MTEE_CMD_CLOSE_SESSION:
  813. return tz_client_close_session(file, arg);
  814. case MTEE_CMD_TEE_SERVICE:
  815. return tz_client_tee_service(file, arg, compat);
  816. case MTEE_CMD_SHM_REG:
  817. return tz_client_reg_sharedmem(file, arg);
  818. case MTEE_CMD_SHM_UNREG:
  819. return tz_client_unreg_sharedmem(file, arg);
  820. default:
  821. return -ENOTTY;
  822. }
  823. return 0;
  824. }
  825. static long tz_client_ioctl(struct file *file, unsigned int cmd,
  826. unsigned long arg)
  827. {
  828. return do_tz_client_ioctl(file, cmd, arg, 0);
  829. }
  830. #ifdef CONFIG_COMPAT
  831. static long tz_client_ioctl_compat(struct file *file, unsigned int cmd,
  832. unsigned long arg)
  833. {
  834. return do_tz_client_ioctl(file, cmd, (unsigned long)compat_ptr(arg), 1);
  835. }
  836. #endif
  837. /* pm op funcstions */
  838. #ifdef TZ_PLAYREADY_SECURETIME_SUPPORT
  839. #ifdef CONFIG_HAS_EARLYSUSPEND
  840. static int securetime_savefile(void)
  841. {
  842. int ret = 0;
  843. KREE_SESSION_HANDLE securetime_session = 0;
  844. ret = KREE_CreateSession(TZ_TA_PLAYREADY_UUID, &securetime_session);
  845. if (ret != TZ_RESULT_SUCCESS)
  846. pr_warn("[securetime]CreateSession error %d\n", ret);
  847. TEE_update_pr_time_infile(securetime_session);
  848. ret = KREE_CloseSession(securetime_session);
  849. if (ret != TZ_RESULT_SUCCESS)
  850. pr_warn("CloseSession error %d\n", ret);
  851. return ret;
  852. }
  853. #endif /* EARLYSUSPEND */
  854. static void st_shutdown(struct platform_device *pdev)
  855. {
  856. pr_warn("[securetime]st_shutdown: kickoff\n");
  857. }
  858. #endif
  859. #ifdef CONFIG_PM_SLEEP
  860. static int tz_suspend(struct device *pdev)
  861. {
  862. TZ_RESULT tzret;
  863. tzret = kree_pm_device_ops(MTEE_SUSPEND);
  864. return (tzret != TZ_RESULT_SUCCESS) ? (-EBUSY) : (0);
  865. }
  866. static int tz_resume(struct device *pdev)
  867. {
  868. TZ_RESULT tzret;
  869. tzret = kree_pm_device_ops(MTEE_RESUME);
  870. return (tzret != TZ_RESULT_SUCCESS) ? (-EBUSY) : (0);
  871. }
  872. #endif
  873. static int tz_suspend_late(struct device *pdev)
  874. {
  875. TZ_RESULT tzret;
  876. tzret = kree_pm_device_ops(MTEE_SUSPEND_LATE);
  877. return (tzret != TZ_RESULT_SUCCESS) ? (-EBUSY) : (0);
  878. }
  879. static int tz_resume_early(struct device *pdev)
  880. {
  881. TZ_RESULT tzret;
  882. tzret = kree_pm_device_ops(MTEE_RESUME_EARLY);
  883. return (tzret != TZ_RESULT_SUCCESS) ? (-EBUSY) : (0);
  884. }
  885. static const struct dev_pm_ops tz_pm_ops = {
  886. .suspend_late = tz_suspend_late,
  887. .freeze_late = tz_suspend_late,
  888. .resume_early = tz_resume_early,
  889. .thaw_early = tz_resume_early,
  890. SET_SYSTEM_SLEEP_PM_OPS(tz_suspend, tz_resume)
  891. };
  892. static struct class *pTzClass;
  893. static struct device *pTzDevice;
  894. static int mtee_probe(struct platform_device *pdev)
  895. {
  896. int ret;
  897. TZ_RESULT tzret;
  898. #ifdef ENABLE_INC_ONLY_COUNTER
  899. struct task_struct *thread;
  900. #endif
  901. #ifdef TZ_SECURETIME_SUPPORT
  902. struct task_struct *thread_securetime_gb;
  903. #endif
  904. #ifdef TZ_PLAYREADY_SECURETIME_SUPPORT
  905. struct task_struct *thread_securetime;
  906. #ifdef CONFIG_HAS_EARLYSUSPEND
  907. register_early_suspend(&securetime_early_suspend);
  908. #endif
  909. #endif
  910. #ifdef CONFIG_OF
  911. struct device_node *parent_node;
  912. if (pdev->dev.of_node) {
  913. parent_node = of_irq_find_parent(pdev->dev.of_node);
  914. if (parent_node)
  915. kree_set_sysirq_node(parent_node);
  916. else
  917. pr_warn("can't find interrupt-parent device node from mtee\n");
  918. } else
  919. pr_warn("No mtee device node\n");
  920. #endif
  921. tz_client_dev = MKDEV(MAJOR_DEV_NUM, 0);
  922. pr_debug(" init\n");
  923. ret = register_chrdev_region(tz_client_dev, 1, TZ_DEV_NAME);
  924. if (ret) {
  925. pr_warn("[%s] register device failed, ret:%d\n",
  926. MODULE_NAME, ret);
  927. return ret;
  928. }
  929. /* initialize the device structure and register the device */
  930. cdev_init(&tz_client_cdev, &tz_client_fops);
  931. tz_client_cdev.owner = THIS_MODULE;
  932. ret = cdev_add(&tz_client_cdev, tz_client_dev, 1);
  933. if (ret < 0) {
  934. pr_warn("[%s] could not allocate chrdev for the device, ret:%d\n",
  935. MODULE_NAME,
  936. ret);
  937. return ret;
  938. }
  939. tzret = KREE_InitTZ();
  940. if (tzret != TZ_RESULT_SUCCESS) {
  941. pr_warn("tz_client_init: TZ Failed %d\n", (int)tzret);
  942. BUG();
  943. }
  944. kree_irq_init();
  945. kree_pm_init();
  946. #ifdef ENABLE_INC_ONLY_COUNTER
  947. thread = kthread_run(update_counter_thread, NULL, "update_tz_counter");
  948. #endif
  949. pr_debug("tz_client_init: successfully\n");
  950. /* tz_test(); */
  951. /* create /dev/trustzone automatically */
  952. pTzClass = class_create(THIS_MODULE, TZ_DEV_NAME);
  953. if (IS_ERR(pTzClass)) {
  954. int ret = PTR_ERR(pTzClass);
  955. pr_warn("[%s] could not create class for the device, ret:%d\n",
  956. MODULE_NAME,
  957. ret);
  958. return ret;
  959. }
  960. pTzDevice = device_create(pTzClass, NULL, tz_client_dev, NULL,
  961. TZ_DEV_NAME);
  962. #ifdef TZ_PLAYREADY_SECURETIME_SUPPORT
  963. thread_securetime = kthread_run(update_securetime_thread, NULL,
  964. "update_securetime");
  965. #endif
  966. #ifdef TZ_SECURETIME_SUPPORT
  967. thread_securetime_gb = kthread_run(update_securetime_thread_gb, NULL,
  968. "update_securetime_gb");
  969. #endif
  970. return 0;
  971. }
  972. static const struct of_device_id mtee_of_match[] = {
  973. { .compatible = "mediatek,mtee", },
  974. };
  975. MODULE_DEVICE_TABLE(of, mtee_of_match);
  976. /* add tz virtual driver for suspend/resume support */
  977. static struct platform_driver tz_driver = {
  978. .probe = mtee_probe,
  979. .driver = {
  980. .name = TZ_DEVNAME,
  981. .pm = &tz_pm_ops,
  982. .owner = THIS_MODULE,
  983. #ifdef CONFIG_OF
  984. .of_match_table = mtee_of_match,
  985. #endif
  986. },
  987. #ifdef TZ_PLAYREADY_SECURETIME_SUPPORT
  988. .shutdown = st_shutdown,
  989. #endif
  990. };
  991. /******************************************************************************
  992. * register_tz_driver
  993. *
  994. * DESCRIPTION:
  995. * register the device driver !
  996. *
  997. * PARAMETERS:
  998. * None
  999. *
  1000. * RETURNS:
  1001. * 0 for success
  1002. *
  1003. * NOTES:
  1004. * None
  1005. *
  1006. ******************************************************************************/
  1007. static int __init register_tz_driver(void)
  1008. {
  1009. int ret = 0;
  1010. #ifndef CONFIG_OF
  1011. if (platform_device_register(&tz_device)) {
  1012. ret = -ENODEV;
  1013. pr_warn("[%s] could not register device for the device, ret:%d\n",
  1014. MODULE_NAME,
  1015. ret);
  1016. return ret;
  1017. }
  1018. #endif
  1019. if (platform_driver_register(&tz_driver)) {
  1020. ret = -ENODEV;
  1021. pr_warn("[%s] could not register device for the device, ret:%d\n",
  1022. MODULE_NAME,
  1023. ret);
  1024. #ifndef CONFIG_OF
  1025. platform_device_unregister(&tz_device);
  1026. #endif
  1027. return ret;
  1028. }
  1029. return ret;
  1030. }
  1031. #ifdef TZ_PLAYREADY_SECURETIME_SUPPORT
  1032. #ifdef CONFIG_HAS_EARLYSUSPEND
  1033. static void st_early_suspend(struct early_suspend *h)
  1034. {
  1035. pr_debug("st_early_suspend: start\n");
  1036. securetime_savefile();
  1037. }
  1038. static void st_late_resume(struct early_suspend *h)
  1039. {
  1040. int ret = 0;
  1041. KREE_SESSION_HANDLE securetime_session = 0;
  1042. ret = KREE_CreateSession(TZ_TA_PLAYREADY_UUID, &securetime_session);
  1043. if (ret != TZ_RESULT_SUCCESS)
  1044. pr_warn("[securetime]CreateSession error %d\n", ret);
  1045. TEE_update_pr_time_intee(securetime_session);
  1046. ret = KREE_CloseSession(securetime_session);
  1047. if (ret != TZ_RESULT_SUCCESS)
  1048. pr_warn("[securetime]CloseSession error %d\n", ret);
  1049. }
  1050. static struct early_suspend securetime_early_suspend = {
  1051. .level = 258,
  1052. .suspend = st_early_suspend,
  1053. .resume = st_late_resume,
  1054. };
  1055. #endif
  1056. #endif
  1057. /******************************************************************************
  1058. * tz_client_init
  1059. *
  1060. * DESCRIPTION:
  1061. * Init the device driver !
  1062. *
  1063. * PARAMETERS:
  1064. * None
  1065. *
  1066. * RETURNS:
  1067. * 0 for success
  1068. *
  1069. * NOTES:
  1070. * None
  1071. *
  1072. ******************************************************************************/
  1073. static int __init tz_client_init(void)
  1074. {
  1075. int ret = 0;
  1076. ret = register_tz_driver();
  1077. if (ret) {
  1078. pr_warn("[%s] register device/driver failed, ret:%d\n",
  1079. MODULE_NAME,
  1080. ret);
  1081. return ret;
  1082. }
  1083. return 0;
  1084. }
  1085. #ifdef CONFIG_TRUSTY
  1086. device_initcall(tz_client_init);
  1087. #else
  1088. arch_initcall(tz_client_init);
  1089. #endif