teei_client_main.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/module.h>
  4. #include <linux/fs.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/cdev.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/sched.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/io.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/wait.h>
  15. #include <asm/cacheflush.h>
  16. #include <linux/delay.h>
  17. #include <linux/irq.h>
  18. #include <asm/uaccess.h>
  19. #include <mach/mt_clkmgr.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. /*#include <mach/eint.h>*/
  23. /*#include <mach/eint_drv.h>*/
  24. #include <linux/irqchip/mt-eic.h>
  25. #include <linux/compat.h>
  26. #include <linux/freezer.h>
  27. #include <linux/cpumask.h>
  28. #include "tpd.h"
  29. #include <linux/delay.h>
  30. #include <linux/cpu.h>
  31. #include "teei_client.h"
  32. #include "teei_common.h"
  33. #include "teei_id.h"
  34. #include "teei_debug.h"
  35. #include "smc_id.h"
  36. /* #include "TEEI.h" */
  37. #include "tz_service.h"
  38. #include "nt_smc_call.h"
  39. #include "teei_client_main.h"
  40. #include "utos_version.h"
  41. #include <linux/of.h>
  42. #include <linux/of_address.h>
  43. #include <linux/completion.h>
  44. #include <linux/kthread.h>
  45. #include <linux/module.h>
  46. #include <linux/device.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/cpu.h>
  49. #include <linux/moduleparam.h>
  50. #define MAX_BUFF_SIZE (4096)
  51. #define NQ_SIZE (4096)
  52. #define CTL_BUFF_SIZE (4096)
  53. #define VDRV_MAX_SIZE (0x80000)
  54. #define NQ_VALID 1
  55. #define TEEI_VFS_NUM 0x8
  56. #define F_CREATE_NQ_ID 0x01
  57. #define F_CREATE_CTL_ID 0x02
  58. #define F_CREATE_VDRV_ID 0x04
  59. #define MESSAGE_SIZE (4096)
  60. #define VALID_TYPE (1)
  61. #define INVALID_TYPE (0)
  62. #define FAST_CALL_TYPE (0x100)
  63. #define STANDARD_CALL_TYPE (0x200)
  64. #define TYPE_NONE (0x300)
  65. #define FAST_CREAT_NQ (0x40)
  66. #define FAST_ACK_CREAT_NQ (0x41)
  67. #define FAST_CREAT_VDRV (0x42)
  68. #define FAST_ACK_CREAT_VDRV (0x43)
  69. #define FAST_CREAT_SYS_CTL (0x44)
  70. #define FAST_ACK_CREAT_SYS_CTL (0x45)
  71. #define NQ_CALL_TYPE (0x60)
  72. #define VDRV_CALL_TYPE (0x61)
  73. #define SCHD_CALL_TYPE (0x62)
  74. #define FDRV_ACK_TYPE (0x63)
  75. #define STD_INIT_CONTEXT (0x80)
  76. #define STD_ACK_INIT_CONTEXT (0x81)
  77. #define STD_OPEN_SESSION (0x82)
  78. #define STD_ACK_OPEN_SESSION (0x83)
  79. #define STD_INVOKE_CMD (0x84)
  80. #define STD_ACK_INVOKE_CMD (0x85)
  81. #define STD_CLOSE_SESSION (0x86)
  82. #define STD_ACK_CLOSE_SESSION (0x87)
  83. #define STD_CLOSE_CONTEXT (0x88)
  84. #define STD_ACK_CLOSE_CONTEXT (0x89)
  85. #define GLSCH_NEG (0x03)
  86. #define GLSCH_NONE (0x00)
  87. #define GLSCH_LOW (0x01)
  88. #define GLSCH_HIGH (0x02)
  89. #define GLSCH_FOR_SOTER (0x04)
  90. #define BOOT_IRQ (283)
  91. #define SCHED_IRQ (284)
  92. #define SOTER_IRQ (285)
  93. #define FP_ACK_IRQ (287)
  94. #define BDRV_IRQ (278)
  95. #define TEEI_LOG_IRQ (277)
  96. #define SOTER_ERROR_IRQ (276)
  97. #define TLOG_CONTEXT_LEN (300)
  98. #define TLOG_MAX_CNT (50)
  99. #define TLOG_UNUSE (0)
  100. #define TLOG_INUSE (1)
  101. #if 0
  102. /******************************
  103. * Message header
  104. ******************************/
  105. struct message_head {
  106. unsigned int invalid_flag;
  107. unsigned int message_type;
  108. unsigned int child_type;
  109. unsigned int param_length;
  110. };
  111. /******************************
  112. * Fast call structures
  113. ******************************/
  114. struct create_NQ_struct {
  115. unsigned int n_t_nq_phy_addr;
  116. unsigned int n_t_size;
  117. unsigned int t_n_nq_phy_addr;
  118. unsigned int t_n_size;
  119. };
  120. struct create_vdrv_struct {
  121. unsigned int vdrv_type;
  122. unsigned int vdrv_phy_addr;
  123. unsigned int vdrv_size;
  124. };
  125. struct create_sys_ctl_struct {
  126. unsigned int sys_ctl_phy_addr;
  127. unsigned int sys_ctl_size;
  128. };
  129. struct ack_fast_call_struct {
  130. int retVal;
  131. };
  132. /*********************************
  133. * Standard call structures
  134. *********************************/
  135. struct std_msg_struct {
  136. unsigned int direction_flag;
  137. unsigned int meg_type;
  138. };
  139. #endif
  140. struct fdrv_call_struct {
  141. int fdrv_call_type;
  142. int fdrv_call_buff_size;
  143. int retVal;
  144. };
  145. #define CAPI_CALL 0x01
  146. #define FDRV_CALL 0x02
  147. #define BDRV_CALL 0x03
  148. #define SCHED_CALL 0x04
  149. #define FP_SYS_NO 100
  150. #define VFS_SYS_NO 0x08
  151. #define REETIME_SYS_NO 0x07
  152. unsigned long cpu_notify_flag = 0;
  153. static int current_cpu_id = 0x00;
  154. static int tz_driver_cpu_callback(struct notifier_block *nfb,
  155. unsigned long action, void *hcpu);
  156. static struct notifier_block tz_driver_cpu_notifer = {
  157. .notifier_call = tz_driver_cpu_callback,
  158. };
  159. struct smc_call_struct {
  160. unsigned long local_cmd;
  161. u32 teei_cmd_type;
  162. u32 dev_file_id;
  163. u32 svc_id;
  164. u32 cmd_id;
  165. u32 context;
  166. u32 enc_id;
  167. void *cmd_buf;
  168. size_t cmd_len;
  169. void *resp_buf;
  170. size_t resp_len;
  171. void *meta_data;
  172. void *info_data;
  173. size_t info_len;
  174. int *ret_resp_len;
  175. int *error_code;
  176. struct semaphore *psema;
  177. int retVal;
  178. };
  179. struct smc_call_struct smc_call_entry;
  180. #define printk(fmt, args...) printk("\033[;34m[TEEI][TZDriver]"fmt"\033[0m", ##args)
  181. asmlinkage long sys_setpriority(int which, int who, int niceval);
  182. asmlinkage long sys_getpriority(int which, int who);
  183. static struct teei_contexts_head {
  184. u32 dev_file_cnt;
  185. struct list_head context_list;
  186. struct rw_semaphore teei_contexts_sem;
  187. } teei_contexts_head;
  188. struct teei_shared_mem_head {
  189. int shared_mem_cnt;
  190. struct list_head shared_mem_list;
  191. };
  192. struct tlog_struct {
  193. int valid;
  194. struct work_struct work;
  195. char context[TLOG_CONTEXT_LEN];
  196. };
  197. #if 0
  198. static int sub_pid;
  199. #else
  200. static struct task_struct *teei_fastcall_task;
  201. static struct task_struct *teei_switch_task;
  202. DEFINE_KTHREAD_WORKER(ut_fastcall_worker);
  203. #endif
  204. static struct cpumask mask = { CPU_BITS_NONE };
  205. int switch_enable_flag = 0;
  206. int forward_call_flag = 0;
  207. int irq_call_flag = 0;
  208. int fp_call_flag = 0;
  209. unsigned long sys_ctl_buffer = NULL;
  210. unsigned long vdrv_buffer = NULL;
  211. unsigned long teei_config_flag = 0;
  212. static struct tlog_struct tlog_ent[TLOG_MAX_CNT];
  213. unsigned int soter_error_flag = 0;
  214. DECLARE_COMPLETION(global_down_lock);
  215. EXPORT_SYMBOL_GPL(global_down_lock);
  216. extern int add_work_entry(int work_type, unsigned long buff);
  217. /*
  218. * structures and MACROs for NQ buffer
  219. */
  220. #define NQ_BUFF_SIZE (4096)
  221. #define NQ_BLOCK_SIZE (32)
  222. #define BLOCK_MAX_COUNT (NQ_BUFF_SIZE / NQ_BLOCK_SIZE - 1)
  223. #define STD_NQ_ACK_ID 0x01
  224. #define TEE_NAME_SIZE (255)
  225. #define START_STATUS (0)
  226. #define END_STATUS (1)
  227. #define VFS_SIZE (512 * 1024)
  228. #define FP_BUFF_SIZE (512 * 1024)
  229. static DEFINE_MUTEX(nt_t_NQ_lock);
  230. static DEFINE_MUTEX(t_nt_NQ_lock);
  231. struct semaphore smc_lock;
  232. //struct semaphore global_down_lock;
  233. struct semaphore api_lock;
  234. static int print_context(void);
  235. struct NQ_head {
  236. unsigned int start_index;
  237. unsigned int end_index;
  238. unsigned int Max_count;
  239. unsigned char reserve[20];
  240. };
  241. struct NQ_entry {
  242. unsigned int valid_flag;
  243. unsigned int length;
  244. unsigned int buffer_addr;
  245. unsigned char reserve[20];
  246. };
  247. static void *tz_malloc(size_t size, int flags)
  248. {
  249. void *ptr = kmalloc(size, flags|GFP_ATOMIC);
  250. return ptr;
  251. }
  252. void *tz_malloc_shared_mem(size_t size, int flags)
  253. {
  254. return (void *) __get_free_pages(flags, get_order(ROUND_UP(size, SZ_4K)));
  255. }
  256. void tz_free_shared_mem(void *addr, size_t size)
  257. {
  258. free_pages((unsigned long)addr, get_order(ROUND_UP(size, SZ_4K)));
  259. }
  260. static struct class *driver_class;
  261. static dev_t teei_client_device_no;
  262. static struct cdev teei_client_cdev;
  263. /* static u32 cacheline_size; */
  264. unsigned long device_file_cnt = 0;
  265. static struct teei_smc_cdata teei_smc_cd[NR_CPUS];
  266. struct semaphore boot_sema;
  267. struct semaphore fp_lock;
  268. unsigned long boot_vfs_addr;
  269. unsigned long boot_soter_flag;
  270. extern struct mutex pm_mutex;
  271. #if 1
  272. /*
  273. * structures for context & session management
  274. */
  275. struct teei_context {
  276. unsigned long cont_id; /* ID */
  277. char tee_name[TEE_NAME_SIZE]; /* Name */
  278. unsigned long sess_cnt; /* session counter */
  279. unsigned long shared_mem_cnt; /* share memory counter */
  280. struct list_head link; /* link list for teei_context */
  281. struct list_head sess_link; /* link list for the sessions of this context */
  282. struct list_head shared_mem_list; /* link list for the share memory of this context */
  283. struct semaphore cont_lock;
  284. };
  285. struct teei_session {
  286. int sess_id; /* ID */
  287. struct teei_context *parent_cont; /* the teei_context pointer of this session */
  288. struct list_head link; /* link list for teei_session */
  289. struct list_head encode_list; /* link list for the encode of this session */
  290. struct list_head shared_mem_list; /* link list for the share memory of this session */
  291. };
  292. struct teei_context *teei_create_context(int dev_count)
  293. {
  294. struct teei_context *cont = NULL;
  295. cont = kmalloc(sizeof(struct teei_context), GFP_KERNEL);
  296. if (cont == NULL)
  297. return NULL;
  298. cont->cont_id = dev_count;
  299. cont->sess_cnt = 0;
  300. memset(cont->tee_name, 0, TEE_NAME_SIZE);
  301. INIT_LIST_HEAD(&(cont->link));
  302. INIT_LIST_HEAD(&(cont->sess_link));
  303. INIT_LIST_HEAD(&(cont->shared_mem_list));
  304. list_add(&(cont->link), &(teei_contexts_head.context_list));
  305. teei_contexts_head.dev_file_cnt++;
  306. return cont;
  307. }
  308. struct teei_session *teei_create_session(struct teei_context *cont)
  309. {
  310. struct teei_session *sess = NULL;
  311. sess = kmalloc(sizeof(struct teei_session), GFP_KERNEL);
  312. if (sess == NULL)
  313. return NULL;
  314. sess->sess_id = (unsigned long)sess;
  315. sess->parent_cont = cont;
  316. INIT_LIST_HEAD(&(sess->link));
  317. INIT_LIST_HEAD(&(sess->encode_list));
  318. INIT_LIST_HEAD(&(sess->shared_mem_list));
  319. list_add(&(sess->link), &(cont->sess_link));
  320. cont->sess_cnt = cont->sess_cnt + 1;
  321. return sess;
  322. }
  323. /*add by microtrust*/
  324. static unsigned int get_master_cpu_id(unsigned int gic_irqs)
  325. {
  326. unsigned int i;
  327. i = gic_irqs >> 2;
  328. unsigned long gic_base = 0x10231000 + 0x800 + i * 4 ;
  329. void __iomem *dist_base = (void __iomem *)gic_base;
  330. unsigned int target_id = readl_relaxed(dist_base);
  331. target_id = target_id >> ((gic_irqs & 0x03) * 8);
  332. target_id = target_id & 0xff;
  333. return target_id;
  334. }
  335. /*****************************************/
  336. /*extern u32 get_irq_target_microtrust(void);*/
  337. int get_current_cpuid(void)
  338. {
  339. return current_cpu_id;
  340. }
  341. static void secondary_nt_sched_t(void *info)
  342. {
  343. nt_sched_t();
  344. }
  345. void nt_sched_t_call(void)
  346. {
  347. #if 0
  348. nt_sched_t();
  349. #else
  350. int cpu_id = 0;
  351. #if 0
  352. get_online_cpus();
  353. cpu_id = get_current_cpuid();
  354. smp_call_function_single(cpu_id, secondary_nt_sched_t, NULL, 1);
  355. put_online_cpus();
  356. #else
  357. int retVal = 0;
  358. retVal = add_work_entry(SCHED_CALL, NULL);
  359. if (retVal != 0) {
  360. printk("[%s][%d] add_work_entry function failed!\n", __func__, __LINE__);
  361. }
  362. #endif
  363. #endif
  364. return;
  365. }
  366. /*********************************************************************************
  367. * global_fn Global schedule thread function.
  368. *
  369. * switch_enable_flag: Waiting for the n_switch_to_t_os_stage2 finished
  370. * forward_call_flag: Forward direction call flag
  371. *
  372. *********************************************************************************/
  373. int global_fn(void)
  374. {
  375. int retVal = 0;
  376. /* forward_call_flag = GLSCH_NONE; */
  377. while (1) {
  378. #if 0
  379. if ((forward_call_flag == GLSCH_NONE) && (fp_call_flag == GLSCH_NONE)) {
  380. set_freezable();
  381. set_current_state(TASK_INTERRUPTIBLE);
  382. } else {
  383. set_nofreezable();
  384. set_current_state(TASK_UNINTERRUPTIBLE);
  385. }
  386. #else
  387. set_freezable();
  388. set_current_state(TASK_INTERRUPTIBLE);
  389. #endif
  390. if (teei_config_flag == 1) {
  391. retVal = wait_for_completion_interruptible(&global_down_lock);
  392. if (retVal == -ERESTARTSYS) {
  393. printk("[%s][%d]*********down &global_down_lock failed *****************\n", __func__, __LINE__ );
  394. continue;
  395. }
  396. }
  397. /* down(&smc_lock); */
  398. retVal = down_interruptible(&smc_lock);
  399. if (retVal != 0) {
  400. printk("[%s][%d]*********down &smc_lock failed *****************\n", __func__, __LINE__ );
  401. complete(&global_down_lock);
  402. continue;
  403. }
  404. if (forward_call_flag == GLSCH_FOR_SOTER) {
  405. forward_call_flag = GLSCH_NONE;
  406. msleep(10);
  407. nt_sched_t_call();
  408. } else if (irq_call_flag == GLSCH_HIGH) {
  409. /* printk("[%s][%d]**************************\n", __func__, __LINE__ ); */
  410. irq_call_flag = GLSCH_NONE;
  411. nt_sched_t_call();
  412. /*msleep_interruptible(10);*/
  413. } else if (fp_call_flag == GLSCH_HIGH) {
  414. /* printk("[%s][%d]**************************\n", __func__, __LINE__ ); */
  415. if (teei_vfs_flag == 0) {
  416. nt_sched_t_call();
  417. } else {
  418. up(&smc_lock);
  419. msleep_interruptible(1);
  420. }
  421. } else if (forward_call_flag == GLSCH_LOW) {
  422. /* printk("[%s][%d]**************************\n", __func__, __LINE__ ); */
  423. if (teei_vfs_flag == 0) {
  424. nt_sched_t_call();
  425. } else {
  426. up(&smc_lock);
  427. msleep_interruptible(1);
  428. }
  429. } else {
  430. /* printk("[%s][%d]**************************\n", __func__, __LINE__ ); */
  431. up(&smc_lock);
  432. msleep_interruptible(1);
  433. }
  434. }
  435. }
  436. struct teei_encode {
  437. struct list_head head;
  438. int encode_id;
  439. void *ker_req_data_addr;
  440. void *ker_res_data_addr;
  441. u32 enc_req_offset;
  442. u32 enc_res_offset;
  443. u32 enc_req_pos;
  444. u32 enc_res_pos;
  445. u32 dec_res_pos;
  446. u32 dec_offset;
  447. struct teei_encode_meta *meta;
  448. };
  449. struct teei_shared_mem {
  450. struct list_head head;
  451. struct list_head s_head;
  452. void *index;
  453. void *k_addr;
  454. void *u_addr;
  455. u32 len;
  456. };
  457. static int teei_client_prepare_encode(void *private_data,
  458. struct teei_client_encode_cmd *enc,
  459. struct teei_encode **penc_context,
  460. struct teei_session **psession);
  461. /******************************************************************************/
  462. static void post_smc_work(u32 cmd_addr);
  463. static void init_smc_work(void);
  464. struct tz_work_entry {
  465. u32 cmd_addr;
  466. struct work_struct work;
  467. };
  468. static struct work_queue *tz_wq;
  469. static struct tz_work_entry tz_work_ent;
  470. struct semaphore tz_sem;
  471. static void tz_work_func(struct work_struct *entry)
  472. {
  473. struct tz_work_entry *md = container_of(entry, struct tz_work_entry, work);
  474. #if 0
  475. /* disable touchscreen interrupt */
  476. disable_irq_nosync(350);
  477. printk("=====TZ DISABLE================\n");
  478. n_send_mem_info_to_ta(md->cmd_addr, sizeof(struct teei_smc_cmd));
  479. /* enable touchscreen interrupt */
  480. enable_irq(350);
  481. printk("======TZ ENABLE================\n");
  482. #endif
  483. up(&tz_sem);
  484. }
  485. static void post_smc_work(u32 cmd_addr)
  486. {
  487. tz_work_ent.cmd_addr = cmd_addr;
  488. INIT_WORK(&(tz_work_ent.work), tz_work_func);
  489. queue_work(tz_wq, &(tz_work_ent.work));
  490. down(&tz_sem);
  491. wait_for_service_done();
  492. }
  493. static void init_smc_work(void)
  494. {
  495. sema_init(&tz_sem, 0);
  496. tz_wq = create_workqueue("TZ SMC WORK");
  497. }
  498. /**
  499. * @brief
  500. *
  501. * @param cmd_addr phys address
  502. *
  503. * @return
  504. */
  505. static u32 _teei_smc(u32 cmd_addr, int size, int valid_flag)
  506. {
  507. int i = 0;
  508. #if 1
  509. /* disable touchscreen interrupt */
  510. add_nq_entry(cmd_addr, size, valid_flag);
  511. set_sch_nq_cmd();
  512. Flush_Dcache_By_Area((unsigned long)t_nt_buffer, (unsigned long)t_nt_buffer + 0x1000);
  513. n_invoke_t_nq(0, 0, 0);
  514. #else
  515. post_smc_work(cmd_addr);
  516. #endif
  517. return 0;
  518. }
  519. /**
  520. * @brief
  521. *
  522. * @param teei_smc handler for secondary cores
  523. *
  524. * @return
  525. */
  526. static void secondary_teei_smc_handler(void *info)
  527. {
  528. struct teei_smc_cdata *cd = (struct teei_smc_cdata *)info;
  529. /* with a rmb() */
  530. rmb();
  531. TDEBUG("secondary teei smc handler...");
  532. cd->ret_val = _teei_smc(cd->cmd_addr, cd->size, cd->valid_flag);
  533. /* with a wmb() */
  534. wmb();
  535. TDEBUG("done smc on primary ");
  536. }
  537. /**
  538. * @brief
  539. *
  540. * @param This function takes care of posting the smc to the
  541. * primary core
  542. *
  543. * @return
  544. */
  545. static u32 post_teei_smc(int cpu_id, u32 cmd_addr, int size, int valid_flag)
  546. {
  547. struct teei_smc_cdata *cd = &teei_smc_cd[cpu_id];
  548. TDEBUG("Post from secondary ...");
  549. cd->cmd_addr = cmd_addr;
  550. cd->size = size;
  551. cd->valid_flag = valid_flag;
  552. cd->ret_val = 0;
  553. /* with a wmb() */
  554. wmb();
  555. get_online_cpus();
  556. smp_call_function_single(cpu_id, secondary_teei_smc_handler, (void *)cd, 1);
  557. put_online_cpus();
  558. /* with a rmb() */
  559. rmb();
  560. TDEBUG("completed smc on secondary ");
  561. return cd->ret_val;
  562. }
  563. /**
  564. * @brief
  565. *
  566. * @param teei_smc wrapper to handle the multi core case
  567. *
  568. * @return
  569. */
  570. static u32 teei_smc(u32 cmd_addr, int size, int valid_flag)
  571. {
  572. #if 0
  573. int cpu_id = smp_processor_id();
  574. /* int cpu_id = raw_smp_processor_id(); */
  575. if (cpu_id != 0) {
  576. /* with mb */
  577. mb();
  578. printk("[%s][%d]\n", __func__, __LINE__);
  579. return post_teei_smc(0, cmd_addr, size, valid_flag); /* post it to primary */
  580. } else {
  581. printk("[%s][%d]\n", __func__, __LINE__);
  582. return _teei_smc(cmd_addr, size, valid_flag); /* called directly on primary core */
  583. }
  584. #else
  585. return _teei_smc(cmd_addr, size, valid_flag);
  586. /* return post_teei_smc(0, cmd_addr, size, valid_flag); */
  587. #endif
  588. }
  589. /**
  590. * @brief
  591. * call smc
  592. * @param svc_id - service identifier
  593. * @param cmd_id - command identifier
  594. * @param context - session context
  595. * @param enc_id - encoder identifier
  596. * @param cmd_buf - command buffer
  597. * @param cmd_len - command buffer length
  598. * @param resp_buf - response buffer
  599. * @param resp_len - response buffer length
  600. * @param meta_data
  601. * @param ret_resp_len
  602. *
  603. * @return
  604. */
  605. int __teei_smc_call(unsigned long local_smc_cmd,
  606. u32 teei_cmd_type,
  607. u32 dev_file_id,
  608. u32 svc_id,
  609. u32 cmd_id,
  610. u32 context,
  611. u32 enc_id,
  612. const void *cmd_buf,
  613. size_t cmd_len,
  614. void *resp_buf,
  615. size_t resp_len,
  616. const void *meta_data,
  617. const void *info_data,
  618. size_t info_len,
  619. int *ret_resp_len,
  620. int *error_code,
  621. struct semaphore *psema)
  622. {
  623. int ret = 50;
  624. void *smc_cmd_phys = 0;
  625. struct teei_smc_cmd *smc_cmd = NULL;
  626. struct teei_shared_mem *temp_shared_mem = NULL;
  627. struct teei_context *temp_cont = NULL;
  628. #if 0
  629. smc_cmd = (struct teei_smc_cmd *)tz_malloc_shared_mem(sizeof(struct teei_smc_cmd), GFP_KERNEL | GFP_DMA);
  630. if (!smc_cmd) {
  631. TERR("tz_malloc failed for smc command");
  632. ret = -ENOMEM;
  633. goto out;
  634. }
  635. #else
  636. smc_cmd = (struct teei_smc_cmd *)local_smc_cmd;
  637. #endif
  638. if (ret_resp_len)
  639. *ret_resp_len = 0;
  640. smc_cmd->teei_cmd_type = teei_cmd_type;
  641. smc_cmd->dev_file_id = dev_file_id;
  642. smc_cmd->src_id = svc_id;
  643. smc_cmd->src_context = task_tgid_vnr(current);
  644. smc_cmd->id = cmd_id;
  645. smc_cmd->context = context;
  646. smc_cmd->enc_id = enc_id;
  647. smc_cmd->src_context = task_tgid_vnr(current);
  648. smc_cmd->req_buf_len = cmd_len;
  649. smc_cmd->resp_buf_len = resp_len;
  650. smc_cmd->info_buf_len = info_len;
  651. smc_cmd->ret_resp_buf_len = 0;
  652. if (NULL == psema)
  653. return -EINVAL;
  654. else
  655. smc_cmd->teei_sema = psema;
  656. if (cmd_buf != NULL) {
  657. smc_cmd->req_buf_phys = virt_to_phys((void *)cmd_buf);
  658. Flush_Dcache_By_Area((unsigned long)cmd_buf, (unsigned long)cmd_buf + cmd_len);
  659. Flush_Dcache_By_Area((unsigned long)&cmd_buf, (unsigned long)&cmd_buf + sizeof(int));
  660. } else
  661. smc_cmd->req_buf_phys = 0;
  662. if (resp_buf) {
  663. smc_cmd->resp_buf_phys = virt_to_phys((void *)resp_buf);
  664. Flush_Dcache_By_Area((unsigned long)resp_buf, (unsigned long)resp_buf + resp_len);
  665. } else
  666. smc_cmd->resp_buf_phys = 0;
  667. if (meta_data) {
  668. smc_cmd->meta_data_phys = virt_to_phys(meta_data);
  669. Flush_Dcache_By_Area((unsigned long)meta_data, (unsigned long)meta_data +
  670. sizeof(struct teei_encode_meta) * (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS));
  671. } else
  672. smc_cmd->meta_data_phys = 0;
  673. if (info_data) {
  674. smc_cmd->info_buf_phys = virt_to_phys(info_data);
  675. Flush_Dcache_By_Area((unsigned long)info_data, (unsigned long)info_data + info_len);
  676. } else
  677. smc_cmd->info_buf_phys = 0;
  678. smc_cmd_phys = virt_to_phys((void *)smc_cmd);
  679. smc_cmd->error_code = 0;
  680. Flush_Dcache_By_Area((unsigned long)smc_cmd, (unsigned long)smc_cmd + sizeof(struct teei_smc_cmd));
  681. Flush_Dcache_By_Area((unsigned long)&smc_cmd, (unsigned long)&smc_cmd + sizeof(int));
  682. /* down(&smc_lock); */
  683. list_for_each_entry(temp_cont,
  684. &teei_contexts_head.context_list,
  685. link) {
  686. if (temp_cont->cont_id == dev_file_id) {
  687. list_for_each_entry(temp_shared_mem,
  688. &temp_cont->shared_mem_list,
  689. head) {
  690. Flush_Dcache_By_Area((unsigned long)temp_shared_mem->k_addr, (unsigned long)temp_shared_mem->k_addr + temp_shared_mem->len);
  691. }
  692. }
  693. }
  694. forward_call_flag = GLSCH_LOW;
  695. ret = teei_smc(smc_cmd_phys, sizeof(struct teei_smc_cmd), NQ_VALID);
  696. /* down(psema); */
  697. #if 0
  698. Invalidate_Dcache_By_Area((unsigned long)smc_cmd, (unsigned long)smc_cmd + sizeof(struct teei_smc_cmd));
  699. if (cmd_buf)
  700. Invalidate_Dcache_By_Area((unsigned long)cmd_buf, (unsigned long)cmd_buf + cmd_len);
  701. if (resp_buf)
  702. Invalidate_Dcache_By_Area((unsigned long)resp_buf, (unsigned long)resp_buf + resp_len);
  703. if (meta_data)
  704. Invalidate_Dcache_By_Area((unsigned long)meta_data, (unsigned long)meta_data +
  705. sizeof(struct teei_encode_meta) * (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS));
  706. if (info_data)
  707. Invalidate_Dcache_By_Area((unsigned long)info_data, (unsigned long)info_data + info_len);
  708. if (error_code)
  709. *error_code = smc_cmd->error_code;
  710. if (ret) {
  711. printk("[%s][%d] smc_call returns error!\n", __func__, __LINE__);
  712. goto out;
  713. }
  714. if (ret_resp_len)
  715. *ret_resp_len = smc_cmd->ret_resp_buf_len;
  716. out:
  717. if (smc_cmd)
  718. tz_free_shared_mem(smc_cmd, sizeof(struct teei_smc_cmd));
  719. return ret;
  720. #endif
  721. return 0;
  722. }
  723. #if 1
  724. static void secondary_teei_smc_call(void *info)
  725. {
  726. struct smc_call_struct *cd = (struct smc_call_struct *)info;
  727. /* with a rmb() */
  728. rmb();
  729. TDEBUG("secondary teei smc call...");
  730. if (cd->cmd_buf != NULL) {
  731. /* Flush_Dcache_By_Area((unsigned long)(cd->cmd_buf), (unsigned long)(cd->cmd_buf) + cd->cmd_len);*/
  732. }
  733. if (cd->resp_buf != NULL) {
  734. /* Flush_Dcache_By_Area((unsigned long)(cd->resp_buf), (unsigned long)(cd->resp_buf) + cd->resp_len);*/
  735. }
  736. if (cd->meta_data != NULL) {
  737. /* Flush_Dcache_By_Area((unsigned long)(cd->meta_data), (unsigned long)(cd->meta_data) +
  738. sizeof(struct teei_encode_meta) * (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS));*/
  739. }
  740. if (cd->info_data != NULL) {
  741. /* Flush_Dcache_By_Area((unsigned long)(cd->info_data), (unsigned long)(cd->info_data) + cd->info_len);*/
  742. }
  743. cd->retVal = __teei_smc_call(cd->local_cmd,
  744. cd->teei_cmd_type,
  745. cd->dev_file_id,
  746. cd->svc_id,
  747. cd->cmd_id,
  748. cd->context,
  749. cd->enc_id,
  750. cd->cmd_buf,
  751. cd->cmd_len,
  752. cd->resp_buf,
  753. cd->resp_len,
  754. cd->meta_data,
  755. cd->info_data,
  756. cd->info_len,
  757. cd->ret_resp_len,
  758. cd->error_code,
  759. cd->psema);
  760. /* with a wmb() */
  761. wmb();
  762. TDEBUG("done smc on primary ");
  763. }
  764. int teei_smc_call(u32 teei_cmd_type,
  765. u32 dev_file_id,
  766. u32 svc_id,
  767. u32 cmd_id,
  768. u32 context,
  769. u32 enc_id,
  770. const void *cmd_buf,
  771. size_t cmd_len,
  772. void *resp_buf,
  773. size_t resp_len,
  774. const void *meta_data,
  775. const void *info_data,
  776. size_t info_len,
  777. int *ret_resp_len,
  778. int *error_code,
  779. struct semaphore *psema)
  780. {
  781. int cpu_id = 0;
  782. int retVal = 0;
  783. struct teei_smc_cmd *local_smc_cmd = (struct teei_smc_cmd *)tz_malloc_shared_mem(sizeof(struct teei_smc_cmd), GFP_KERNEL | GFP_DMA);
  784. if (local_smc_cmd == NULL) {
  785. printk("[%s][%d] tz_malloc_shared_mem failed!\n", __func__, __LINE__);
  786. return -1;
  787. }
  788. smc_call_entry.local_cmd = local_smc_cmd;
  789. smc_call_entry.teei_cmd_type = teei_cmd_type;
  790. smc_call_entry.dev_file_id = dev_file_id;
  791. smc_call_entry.svc_id = svc_id;
  792. smc_call_entry.cmd_id = cmd_id;
  793. smc_call_entry.context = context;
  794. smc_call_entry.enc_id = enc_id;
  795. smc_call_entry.cmd_buf = cmd_buf;
  796. smc_call_entry.cmd_len = cmd_len;
  797. smc_call_entry.resp_buf = resp_buf;
  798. smc_call_entry.resp_len = resp_len;
  799. smc_call_entry.meta_data = meta_data;
  800. smc_call_entry.info_data = info_data;
  801. smc_call_entry.info_len = info_len;
  802. smc_call_entry.ret_resp_len = ret_resp_len;
  803. smc_call_entry.error_code = error_code;
  804. smc_call_entry.psema = psema;
  805. if (teei_config_flag == 1) {
  806. complete(&global_down_lock);
  807. }
  808. down(&smc_lock);
  809. /* with a wmb() */
  810. wmb();
  811. #if 0
  812. get_online_cpus();
  813. cpu_id = get_current_cpuid();
  814. smp_call_function_single(cpu_id, secondary_teei_smc_call, (void *)(&smc_call_entry), 1);
  815. put_online_cpus();
  816. #else
  817. Flush_Dcache_By_Area((unsigned long)&smc_call_entry, (unsigned long)&smc_call_entry + sizeof(smc_call_entry));
  818. retVal = add_work_entry(CAPI_CALL, (unsigned long)&smc_call_entry);
  819. if (retVal != 0) {
  820. tz_free_shared_mem(local_smc_cmd, sizeof(struct teei_smc_cmd));
  821. return retVal;
  822. }
  823. #endif
  824. down(psema);
  825. rmb();
  826. if (ret_resp_len)
  827. *ret_resp_len = local_smc_cmd->ret_resp_buf_len;
  828. tz_free_shared_mem(local_smc_cmd, sizeof(struct teei_smc_cmd));
  829. return smc_call_entry.retVal;
  830. }
  831. #endif
  832. int service_smc_call(u32 teei_cmd_type, u32 dev_file_id, u32 svc_id,
  833. u32 cmd_id, u32 context, u32 enc_id,
  834. const void *cmd_buf,
  835. size_t cmd_len,
  836. void *resp_buf,
  837. size_t resp_len,
  838. const void *meta_data,
  839. int *ret_resp_len,
  840. void *wq,
  841. void *arg_lock, int *error_code)
  842. {
  843. return 0;
  844. }
  845. static int teei_client_close_session_for_service(
  846. void *private_data,
  847. struct teei_session *temp_ses)
  848. {
  849. struct ser_ses_id *ses_close = (struct ser_ses_id *)tz_malloc_shared_mem(sizeof(struct ser_ses_id), GFP_KERNEL | GFP_DMA );
  850. struct teei_context *curr_cont = NULL;
  851. struct teei_encode *temp_encode = NULL;
  852. struct teei_encode *enc_context = NULL;
  853. struct teei_shared_mem *shared_mem = NULL;
  854. struct teei_shared_mem *temp_shared = NULL;
  855. unsigned long dev_file_id = (unsigned long)private_data;
  856. int retVal = 0;
  857. int *res = (int *)tz_malloc_shared_mem(4, GFP_KERNEL | GFP_DMA);
  858. int error_code = 0;
  859. if (temp_ses == NULL)
  860. return -EINVAL;
  861. if (ses_close == NULL)
  862. return -ENOMEM;
  863. if (res == NULL)
  864. return -ENOMEM;
  865. ses_close->session_id = temp_ses->sess_id;
  866. printk("======== ses_close->session_id = %d =========\n", ses_close->session_id);
  867. curr_cont = temp_ses->parent_cont;
  868. retVal = teei_smc_call(TEEI_CMD_TYPE_CLOSE_SESSION,
  869. dev_file_id,
  870. 0,
  871. TEEI_GLOBAL_CMD_ID_CLOSE_SESSION,
  872. 0,
  873. 0,
  874. ses_close,
  875. sizeof(struct ser_ses_id),
  876. res,
  877. sizeof(int),
  878. NULL,
  879. NULL,
  880. 0,
  881. NULL,
  882. &error_code,
  883. &(curr_cont->cont_lock));
  884. if (!list_empty(&temp_ses->encode_list)) {
  885. list_for_each_entry_safe(enc_context,
  886. temp_encode,
  887. &temp_ses->encode_list,
  888. head) {
  889. if (enc_context) {
  890. list_del(&enc_context->head);
  891. kfree(enc_context);
  892. }
  893. }
  894. }
  895. if (!list_empty(&temp_ses->shared_mem_list)) {
  896. list_for_each_entry_safe(shared_mem,
  897. temp_shared,
  898. &temp_ses->shared_mem_list,
  899. s_head) {
  900. if (shared_mem == NULL)
  901. continue;
  902. list_del(&shared_mem->s_head);
  903. if (shared_mem->k_addr)
  904. free_pages((unsigned long)shared_mem->k_addr,
  905. get_order(ROUND_UP(shared_mem->len, SZ_4K)));
  906. kfree(shared_mem);
  907. }
  908. }
  909. list_del(&temp_ses->link);
  910. curr_cont->sess_cnt = curr_cont->sess_cnt - 1;
  911. kfree(temp_ses);
  912. tz_free_shared_mem(res, 4);
  913. tz_free_shared_mem(ses_close, sizeof(struct ser_ses_id));
  914. return 0;
  915. }
  916. static int teei_client_service_init(struct teei_context *dev_file, struct ser_ses_id *ses_open)
  917. {
  918. return 0;
  919. }
  920. /**
  921. * @brief close the context with context_ID equal private_data
  922. *
  923. * @return EINVAL: Can not find the context with ID equal private_data
  924. * 0: success
  925. */
  926. static int teei_client_service_exit(void *private_data)
  927. {
  928. struct teei_shared_mem *temp_shared_mem = NULL;
  929. struct teei_shared_mem *temp_pos = NULL;
  930. struct teei_context *temp_context = NULL;
  931. struct teei_context *temp_context_pos = NULL;
  932. struct teei_session *temp_ses = NULL;
  933. struct teei_session *temp_ses_pos = NULL;
  934. unsigned long dev_file_id = 0;
  935. dev_file_id = (unsigned long)(private_data);
  936. down_write(&(teei_contexts_head.teei_contexts_sem));
  937. list_for_each_entry_safe(temp_context,
  938. temp_context_pos,
  939. &teei_contexts_head.context_list,
  940. link) {
  941. if (temp_context->cont_id == dev_file_id) {
  942. list_for_each_entry_safe(temp_shared_mem,
  943. temp_pos,
  944. &temp_context->shared_mem_list,
  945. head) {
  946. if (temp_shared_mem) {
  947. list_del(&(temp_shared_mem->head));
  948. if (temp_shared_mem->k_addr) {
  949. free_pages((unsigned long)temp_shared_mem->k_addr,
  950. get_order(ROUND_UP(temp_shared_mem->len, SZ_4K)));
  951. }
  952. kfree(temp_shared_mem);
  953. }
  954. }
  955. if (!list_empty(&temp_context->sess_link)) {
  956. list_for_each_entry_safe(temp_ses,
  957. temp_ses_pos,
  958. &temp_context->sess_link,
  959. link)
  960. teei_client_close_session_for_service(private_data, temp_ses);
  961. }
  962. list_del(&temp_context->link);
  963. kfree(temp_context);
  964. up_write(&(teei_contexts_head.teei_contexts_sem));
  965. return 0;
  966. }
  967. }
  968. return -EINVAL;
  969. }
  970. /**
  971. * @brief
  972. *
  973. * @param argp
  974. *
  975. * @return
  976. */
  977. static int teei_client_session_init(void *private_data, void *argp)
  978. {
  979. struct teei_context *temp_cont = NULL;
  980. struct teei_session *ses_new = NULL;
  981. struct user_ses_init ses_init;
  982. int ctx_found = 0;
  983. unsigned long dev_file_id = (unsigned long)private_data;
  984. TDEBUG("inside session init");
  985. if (copy_from_user(&ses_init, argp, sizeof(ses_init))) {
  986. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  987. return -EFAULT;
  988. }
  989. down_read(&(teei_contexts_head.teei_contexts_sem));
  990. /*print_context();*/
  991. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  992. if (temp_cont->cont_id == dev_file_id) {
  993. ctx_found = 1;
  994. break;
  995. }
  996. }
  997. up_read(&(teei_contexts_head.teei_contexts_sem));
  998. if (!ctx_found) {
  999. printk("[%s][%d] can't find context.\n", __func__, __LINE__);
  1000. return -EINVAL;
  1001. }
  1002. ses_new = (struct teei_session *)tz_malloc(sizeof(struct teei_session), GFP_KERNEL);
  1003. if (ses_new == NULL) {
  1004. printk("[%s][%d] tz_malloc failed.\n", __func__, __LINE__);
  1005. return -ENOMEM;
  1006. }
  1007. ses_init.session_id = (unsigned long)ses_new;
  1008. ses_new->sess_id = (unsigned long)ses_new;
  1009. ses_new->parent_cont = temp_cont;
  1010. INIT_LIST_HEAD(&ses_new->link);
  1011. INIT_LIST_HEAD(&ses_new->encode_list);
  1012. INIT_LIST_HEAD(&ses_new->shared_mem_list);
  1013. list_add_tail(&ses_new->link, &temp_cont->sess_link);
  1014. if (copy_to_user(argp, &ses_init, sizeof(ses_init))) {
  1015. list_del(&ses_new->link);
  1016. kfree(ses_new);
  1017. return -EFAULT;
  1018. }
  1019. return 0;
  1020. }
  1021. /**
  1022. * @brief Open one session
  1023. *
  1024. * @param argp
  1025. *
  1026. * @return 0: success
  1027. * EFAULT: copy data from user space OR copy data back to the user space error
  1028. * EINVLA: can not find the context OR session structure.
  1029. */
  1030. static int teei_client_session_open(void *private_data, void *argp)
  1031. {
  1032. struct teei_context *temp_cont = NULL;
  1033. struct teei_session *ses_new = NULL;
  1034. struct teei_encode *enc_temp = NULL;
  1035. struct ser_ses_id *ses_open = (struct ser_ses_id *)tz_malloc_shared_mem(sizeof(struct ser_ses_id), GFP_KERNEL | GFP_DMA );
  1036. int ctx_found = 0;
  1037. int sess_found = 0;
  1038. int enc_found = 0;
  1039. int retVal = 0;
  1040. unsigned long dev_file_id = (unsigned long)private_data;
  1041. printk("ses open [%ld]\n", (unsigned long)ses_open);
  1042. if (ses_open == NULL) {
  1043. return -EFAULT;
  1044. }
  1045. /* Get the paraments about this session from user space. */
  1046. if (copy_from_user(ses_open, argp, sizeof(struct ser_ses_id))) {
  1047. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1048. tz_free_shared_mem(ses_open, sizeof(struct ser_ses_id));
  1049. return -EFAULT;
  1050. }
  1051. /* Search the teei_context structure */
  1052. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1053. if (temp_cont->cont_id == dev_file_id) {
  1054. ctx_found = 1;
  1055. break;
  1056. }
  1057. }
  1058. if (ctx_found == 0) {
  1059. printk("[%s][%d] can't find context!\n", __func__, __LINE__);
  1060. tz_free_shared_mem(ses_open, sizeof(struct ser_ses_id));
  1061. return -EINVAL;
  1062. }
  1063. /* Search the teei_session structure */
  1064. list_for_each_entry(ses_new, &temp_cont->sess_link, link) {
  1065. if (ses_new->sess_id == ses_open->session_id) {
  1066. sess_found = 1;
  1067. break;
  1068. }
  1069. }
  1070. if (sess_found == 0) {
  1071. printk("[%s][%d] can't find session!\n", __func__, __LINE__);
  1072. tz_free_shared_mem(ses_open, sizeof(struct ser_ses_id));
  1073. return -EINVAL;
  1074. }
  1075. /* Search the teei_encode structure */
  1076. list_for_each_entry(enc_temp, &ses_new->encode_list, head) {
  1077. if (enc_temp->encode_id == ses_open->paramtype) {
  1078. enc_found = 1;
  1079. break;
  1080. }
  1081. }
  1082. /* Invoke the smc_call */
  1083. if (enc_found) {
  1084. /* This session had been encoded. */
  1085. retVal = teei_smc_call(TEEI_CMD_TYPE_OPEN_SESSION,
  1086. dev_file_id,
  1087. 0,
  1088. 0,
  1089. 0,
  1090. 0,
  1091. enc_temp->ker_req_data_addr,
  1092. enc_temp->enc_req_offset,
  1093. enc_temp->ker_res_data_addr,
  1094. enc_temp->enc_res_offset,
  1095. enc_temp->meta,
  1096. ses_open,
  1097. sizeof(struct ser_ses_id),
  1098. NULL,
  1099. NULL,
  1100. &(temp_cont->cont_lock));
  1101. } else {
  1102. /* This session didn't have been encoded */
  1103. retVal = teei_smc_call(TEEI_CMD_TYPE_OPEN_SESSION,
  1104. dev_file_id,
  1105. 0,
  1106. 0,
  1107. 0,
  1108. 0,
  1109. NULL,
  1110. 0,
  1111. NULL,
  1112. 0,
  1113. NULL,
  1114. ses_open,
  1115. sizeof(struct ser_ses_id),
  1116. NULL,
  1117. NULL,
  1118. &(temp_cont->cont_lock));
  1119. }
  1120. if (retVal != SMC_SUCCESS) {
  1121. printk("[%s][%d] open session smc error!\n", __func__, __LINE__);
  1122. goto clean_hdr_buf;
  1123. }
  1124. if (ses_open->session_id == -1)
  1125. printk("[%s][%d] invalid session id!\n", __func__, __LINE__);
  1126. /* Copy the result back to the user space */
  1127. ses_new->sess_id = ses_open->session_id;
  1128. if (copy_to_user(argp, ses_open, sizeof(struct ser_ses_id))) {
  1129. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1130. retVal = -EFAULT;
  1131. goto clean_hdr_buf;
  1132. }
  1133. tz_free_shared_mem(ses_open, sizeof(struct ser_ses_id));
  1134. return 0;
  1135. clean_hdr_buf:
  1136. list_del(&ses_new->link);
  1137. tz_free_shared_mem(ses_open, sizeof(struct ser_ses_id));
  1138. kfree(ses_new);
  1139. return retVal;
  1140. }
  1141. /**
  1142. * @brief
  1143. *
  1144. * @param argp
  1145. *
  1146. * @return
  1147. */
  1148. static int teei_client_session_close(void *private_data, void *argp)
  1149. {
  1150. struct teei_context *temp_cont = NULL;
  1151. struct teei_session *temp_ses = NULL;
  1152. int retVal = -EFAULT;
  1153. unsigned long dev_file_id = (unsigned long)private_data;
  1154. struct ser_ses_id ses_close;
  1155. if (copy_from_user(&ses_close, argp, sizeof(ses_close))) {
  1156. printk("[%s][%d] copy from user failed.\n ", __func__, __LINE__);
  1157. return -EFAULT;
  1158. }
  1159. down_read(&(teei_contexts_head.teei_contexts_sem));
  1160. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1161. if (temp_cont->cont_id == dev_file_id) {
  1162. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  1163. if (temp_ses->sess_id == ses_close.session_id) {
  1164. teei_client_close_session_for_service(private_data, temp_ses);
  1165. retVal = 0;
  1166. goto copy_to_user;
  1167. }
  1168. }
  1169. }
  1170. }
  1171. copy_to_user:
  1172. up_read(&(teei_contexts_head.teei_contexts_sem));
  1173. if (copy_to_user(argp, &ses_close, sizeof(ses_close))) {
  1174. printk("[%s][%d] copy from user failed.\n", __func__, __LINE__);
  1175. return -EFAULT;
  1176. }
  1177. return retVal;
  1178. }
  1179. /**
  1180. * @brief Map the vma with the free pages
  1181. *
  1182. * @param filp
  1183. * @param vma
  1184. *
  1185. * @return 0: success
  1186. * EINVAL: Invalid parament
  1187. * ENOMEM: No enough memory
  1188. */
  1189. static int teei_client_mmap(struct file *filp, struct vm_area_struct *vma)
  1190. {
  1191. int retVal = 0;
  1192. struct teei_shared_mem *share_mem_entry = NULL;
  1193. int context_found = 0;
  1194. unsigned long alloc_addr = 0;
  1195. struct teei_context *cont = NULL;
  1196. long length = vma->vm_end - vma->vm_start;
  1197. /* Reasch the context with ID equal filp->private_data */
  1198. down_read(&(teei_contexts_head.teei_contexts_sem));
  1199. list_for_each_entry(cont, &(teei_contexts_head.context_list), link) {
  1200. if (cont->cont_id == (unsigned long)filp->private_data) {
  1201. context_found = 1;
  1202. break;
  1203. }
  1204. }
  1205. if (context_found == 0) {
  1206. up_read(&(teei_contexts_head.teei_contexts_sem));
  1207. return -EINVAL;
  1208. }
  1209. /* Alloc one teei_share_mem structure */
  1210. share_mem_entry = tz_malloc(sizeof(struct teei_shared_mem), GFP_KERNEL);
  1211. if (share_mem_entry == NULL) {
  1212. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1213. up_read(&(teei_contexts_head.teei_contexts_sem));
  1214. return -ENOMEM;
  1215. }
  1216. /* Get free pages from Kernel. */
  1217. alloc_addr = (unsigned long) __get_free_pages(GFP_KERNEL | GFP_DMA , get_order(ROUND_UP(length, SZ_4K)));
  1218. if (alloc_addr == 0) {
  1219. printk("[%s][%d] get free pages failed!\n", __func__, __LINE__);
  1220. kfree(share_mem_entry);
  1221. up_read(&(teei_contexts_head.teei_contexts_sem));
  1222. return -ENOMEM;
  1223. }
  1224. /* Remap the free pages to the VMA */
  1225. retVal = remap_pfn_range(vma, vma->vm_start, ((virt_to_phys((void *)alloc_addr)) >> PAGE_SHIFT),
  1226. length, vma->vm_page_prot);
  1227. if (retVal) {
  1228. printk("[%s][%d] remap_pfn_range failed!\n", __func__, __LINE__);
  1229. kfree(share_mem_entry);
  1230. free_pages(alloc_addr, get_order(ROUND_UP(length, SZ_4K)));
  1231. up_read(&(teei_contexts_head.teei_contexts_sem));
  1232. return retVal;
  1233. }
  1234. /* Add the teei_share_mem into the teei_context struct */
  1235. share_mem_entry->k_addr = (void *)alloc_addr;
  1236. share_mem_entry->len = length;
  1237. share_mem_entry->u_addr = (void *)vma->vm_start;
  1238. share_mem_entry->index = share_mem_entry->u_addr;
  1239. cont->shared_mem_cnt++;
  1240. list_add_tail(&(share_mem_entry->head), &(cont->shared_mem_list));
  1241. up_read(&(teei_contexts_head.teei_contexts_sem));
  1242. return 0;
  1243. }
  1244. /**
  1245. * @brief Send a command to TEE
  1246. *
  1247. * @param argp
  1248. *
  1249. * @return EINVAL: Invalid parament
  1250. * EFAULT: copy data from user space OR copy data back to the user space error
  1251. * 0: success
  1252. */
  1253. static int teei_client_send_cmd(void *private_data, void *argp)
  1254. {
  1255. int retVal = 0;
  1256. struct teei_client_encode_cmd enc;
  1257. unsigned long dev_file_id = 0;
  1258. struct teei_context *temp_cont = NULL;
  1259. struct teei_session *temp_ses = NULL;
  1260. struct teei_encode *enc_temp = NULL;
  1261. int ctx_found = 0;
  1262. int sess_found = 0;
  1263. int enc_found = 0;
  1264. dev_file_id = (unsigned long)private_data;
  1265. if (copy_from_user(&enc, argp, sizeof(enc))) {
  1266. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1267. return -EFAULT;
  1268. }
  1269. down_read(&(teei_contexts_head.teei_contexts_sem));
  1270. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1271. if (temp_cont->cont_id == dev_file_id) {
  1272. ctx_found = 1;
  1273. break;
  1274. }
  1275. }
  1276. up_read(&(teei_contexts_head.teei_contexts_sem));
  1277. if (ctx_found == 0) {
  1278. printk("[%s][%d] can't find context data!\n", __func__, __LINE__);
  1279. return -EINVAL;
  1280. }
  1281. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  1282. if (temp_ses->sess_id == enc.session_id) {
  1283. sess_found = 1;
  1284. break;
  1285. }
  1286. }
  1287. if (sess_found == 0) {
  1288. printk("[%s][%d] can't find session data!\n", __func__, __LINE__);
  1289. return -EINVAL;
  1290. }
  1291. if (enc.encode_id != -1) {
  1292. list_for_each_entry(enc_temp, &temp_ses->encode_list, head) {
  1293. if (enc_temp->encode_id == enc.encode_id) {
  1294. enc_found = 1;
  1295. break;
  1296. }
  1297. }
  1298. } else {
  1299. retVal = teei_client_prepare_encode(private_data, &enc, &enc_temp, &temp_ses);
  1300. if (retVal == 0)
  1301. enc_found = 1;
  1302. }
  1303. if (enc_found == 0) {
  1304. printk("[%s][%d] can't find encode data!\n", __func__, __LINE__);
  1305. return -EINVAL;
  1306. }
  1307. retVal = teei_smc_call(TEEI_CMD_TYPE_INVOKE_COMMAND,
  1308. dev_file_id,
  1309. 0,
  1310. enc.cmd_id,
  1311. enc.session_id,
  1312. enc.encode_id,
  1313. enc_temp->ker_req_data_addr,
  1314. enc_temp->enc_req_offset,
  1315. enc_temp->ker_res_data_addr,
  1316. enc_temp->enc_res_offset,
  1317. enc_temp->meta,
  1318. NULL,
  1319. 0,
  1320. &enc.return_value,
  1321. &enc.return_origin,
  1322. &(temp_cont->cont_lock));
  1323. if (retVal != SMC_SUCCESS)
  1324. printk("[%s][%d] send cmd secure call failed!\n", __func__, __LINE__);
  1325. if (copy_to_user(argp, &enc, sizeof(enc))) {
  1326. printk("[%s][%d] copy to user failed!\n", __func__, __LINE__);
  1327. return -EFAULT;
  1328. }
  1329. return retVal;
  1330. }
  1331. static int teei_client_context_init(void *private_data, void *argp)
  1332. {
  1333. int retVal = 0;
  1334. struct teei_context *temp_cont = NULL;
  1335. unsigned long dev_file_id = (unsigned long)private_data;
  1336. struct ctx_data ctx;
  1337. int dev_found = 0;
  1338. int error_code = 0;
  1339. int *resp_flag = tz_malloc_shared_mem(4, GFP_KERNEL | GFP_DMA);
  1340. char *name = tz_malloc_shared_mem(sizeof(ctx.name), GFP_KERNEL | GFP_DMA);
  1341. if (resp_flag == NULL)
  1342. return -ENOMEM;
  1343. if (name == NULL)
  1344. return -ENOMEM;
  1345. if (copy_from_user(&ctx, argp, sizeof(ctx))) {
  1346. printk("[%s][%d] copy from user failed.\n ", __func__, __LINE__);
  1347. return -EFAULT;
  1348. }
  1349. memcpy(name, ctx.name, sizeof(ctx.name));
  1350. printk("[%s][%d] context name = %s.\n ", __func__, __LINE__, name);
  1351. Flush_Dcache_By_Area((unsigned long)name,
  1352. (unsigned long)name+sizeof(ctx.name));
  1353. down_write(&(teei_contexts_head.teei_contexts_sem));
  1354. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1355. if (temp_cont->cont_id == dev_file_id) {
  1356. dev_found = 1;
  1357. break;
  1358. }
  1359. }
  1360. up_write(&(teei_contexts_head.teei_contexts_sem));
  1361. if (dev_found) {
  1362. strcpy(temp_cont->tee_name, ctx.name);
  1363. retVal = teei_smc_call(TEEI_CMD_TYPE_INITILIZE_CONTEXT, dev_file_id,
  1364. 0, 0, 0, 0, name, 255, resp_flag, 4, NULL,
  1365. NULL, 0, NULL, &error_code, &(temp_cont->cont_lock));
  1366. }
  1367. ctx.ctx_ret = !(*resp_flag);
  1368. tz_free_shared_mem(resp_flag, 4);
  1369. tz_free_shared_mem(name, sizeof(ctx.name));
  1370. if (copy_to_user(argp, &ctx, sizeof(ctx))) {
  1371. printk("[%s][%d]copy from user failed!\n", __func__, __LINE__);
  1372. return -EFAULT;
  1373. }
  1374. return retVal;
  1375. }
  1376. static int teei_client_context_close(void *private_data, void *argp)
  1377. {
  1378. int retVal = 0;
  1379. struct teei_context *temp_cont = NULL;
  1380. unsigned long dev_file_id = (unsigned long)private_data;
  1381. struct ctx_data ctx;
  1382. int dev_found = 0;
  1383. int *resp_flag = (int *) tz_malloc_shared_mem(4, GFP_KERNEL | GFP_DMA);
  1384. int error_code = 0;
  1385. if (resp_flag == NULL)
  1386. return -ENOMEM;
  1387. if (copy_from_user(&ctx, argp, sizeof(ctx))) {
  1388. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1389. return -EFAULT;
  1390. }
  1391. down_write(&(teei_contexts_head.teei_contexts_sem));
  1392. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1393. printk("cont_id = %ld =======\n", temp_cont->cont_id);
  1394. if (temp_cont->cont_id == dev_file_id) {
  1395. dev_found = 1;
  1396. break;
  1397. }
  1398. }
  1399. up_write(&(teei_contexts_head.teei_contexts_sem));
  1400. if (dev_found) {
  1401. strcpy(temp_cont->tee_name, ctx.name);
  1402. retVal = teei_smc_call(TEEI_CMD_TYPE_FINALIZE_CONTEXT, dev_file_id,
  1403. 0, 0, 0, 0,
  1404. NULL, 0, resp_flag, 4, NULL, NULL,
  1405. 0, NULL, &error_code, &(temp_cont->cont_lock));
  1406. }
  1407. ctx.ctx_ret = *resp_flag;
  1408. tz_free_shared_mem(resp_flag, 4);
  1409. if (copy_to_user(argp, &ctx, sizeof(ctx))) {
  1410. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1411. return -EFAULT;
  1412. }
  1413. return retVal;
  1414. }
  1415. /**
  1416. * @brief Delete the operation with the encode_id
  1417. *
  1418. * @param argp
  1419. *
  1420. * @return EINVAL: Invalid parament
  1421. * EFAULT: copy data from user space OR copy data back to the user space error
  1422. * 0: success
  1423. */
  1424. static int teei_client_operation_release(void *private_data, void *argp)
  1425. {
  1426. struct teei_client_encode_cmd enc;
  1427. struct teei_encode *enc_context = NULL;
  1428. struct teei_context *temp_cont = NULL;
  1429. struct teei_session *temp_ses = NULL;
  1430. int ctx_found = 0;
  1431. int session_found = 0;
  1432. int enc_found = 0;
  1433. unsigned long dev_file_id = (unsigned long)private_data;
  1434. if (copy_from_user(&enc, argp, sizeof(enc))) {
  1435. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1436. return -EFAULT;
  1437. }
  1438. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1439. if (temp_cont->cont_id == dev_file_id) {
  1440. ctx_found = 1;
  1441. break;
  1442. }
  1443. }
  1444. if (ctx_found == 0) {
  1445. printk("[%s][%d] ctx_found failed!\n", __func__, __LINE__);
  1446. return -EINVAL;
  1447. }
  1448. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  1449. if (temp_ses->sess_id == enc.session_id) {
  1450. session_found = 1;
  1451. break;
  1452. }
  1453. }
  1454. if (session_found == 0) {
  1455. printk("[%s][%d] session_found failed!\n", __func__, __LINE__);
  1456. return -EINVAL;
  1457. }
  1458. if (enc.encode_id != -1) {
  1459. list_for_each_entry(enc_context, &temp_ses->encode_list, head) {
  1460. if (enc_context->encode_id == enc.encode_id) {
  1461. enc_found = 1;
  1462. break;
  1463. }
  1464. }
  1465. }
  1466. if (enc_found == 0) {
  1467. printk("[%s][%d] enc_found failed!\n", __func__, __LINE__);
  1468. return -EINVAL;
  1469. } else {
  1470. if (enc_context->ker_req_data_addr)
  1471. tz_free_shared_mem(enc_context->ker_req_data_addr, TEEI_1K_SIZE);
  1472. if (enc_context->ker_res_data_addr)
  1473. tz_free_shared_mem(enc_context->ker_res_data_addr, TEEI_1K_SIZE);
  1474. list_del(&enc_context->head);
  1475. /* kfree(enc_context->meta); */
  1476. tz_free_shared_mem(enc_context->meta, sizeof(struct teei_encode_meta) *
  1477. (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS));
  1478. kfree(enc_context);
  1479. }
  1480. return 0;
  1481. }
  1482. /**
  1483. * @brief search the teei_encode and teei_session structures
  1484. * if there is no structure, create one.
  1485. * @param private_data Context ID
  1486. * @param enc
  1487. * @param penc_context
  1488. * @param psession
  1489. *
  1490. * @return EINVAL: Invalid parament
  1491. * ENOMEM: No enough memory
  1492. * 0: success
  1493. */
  1494. static int teei_client_prepare_encode(void *private_data,
  1495. struct teei_client_encode_cmd *enc,
  1496. struct teei_encode **penc_context,
  1497. struct teei_session **psession)
  1498. {
  1499. struct teei_context *temp_cont = NULL;
  1500. struct teei_session *temp_ses = NULL;
  1501. struct teei_encode *enc_context = NULL;
  1502. int session_found = 0;
  1503. int enc_found = 0;
  1504. int retVal = 0;
  1505. unsigned long dev_file_id = (unsigned long)private_data;
  1506. /* search the context session with private_data */
  1507. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1508. if (temp_cont->cont_id == dev_file_id) {
  1509. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  1510. if (temp_ses->sess_id == enc->session_id) {
  1511. session_found = 1;
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. if (session_found == 1)
  1517. break;
  1518. }
  1519. if (!session_found) {
  1520. printk("[%s][%d] session (ID: %x) not found!\n", __func__, __LINE__, enc->session_id);
  1521. return -EINVAL;
  1522. }
  1523. /*
  1524. * check if the enc struct had been inited.
  1525. */
  1526. if (enc->encode_id != -1) {
  1527. list_for_each_entry(enc_context, &temp_ses->encode_list, head) {
  1528. if (enc_context->encode_id == enc->encode_id) {
  1529. enc_found = 1;
  1530. break;
  1531. }
  1532. }
  1533. }
  1534. /* create one command parament block */
  1535. if (!enc_found) {
  1536. enc_context = (struct teei_encode *)tz_malloc(sizeof(struct teei_encode), GFP_KERNEL);
  1537. if (enc_context == NULL) {
  1538. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1539. return -ENOMEM;
  1540. }
  1541. enc_context->meta = tz_malloc_shared_mem(sizeof(struct teei_encode_meta) *
  1542. (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS),
  1543. GFP_KERNEL | GFP_DMA);
  1544. if (enc_context->meta == NULL) {
  1545. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1546. kfree(enc_context);
  1547. return -ENOMEM;
  1548. }
  1549. memset(enc_context->meta, 0, sizeof(struct teei_encode_meta) *
  1550. (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS));
  1551. enc_context->encode_id = (unsigned long)enc_context;
  1552. enc_context->ker_req_data_addr = NULL;
  1553. enc_context->ker_res_data_addr = NULL;
  1554. enc_context->enc_req_offset = 0;
  1555. enc_context->enc_res_offset = 0;
  1556. enc_context->enc_req_pos = 0;
  1557. enc_context->enc_res_pos = TEEI_MAX_REQ_PARAMS;
  1558. enc_context->dec_res_pos = TEEI_MAX_REQ_PARAMS;
  1559. enc_context->dec_offset = 0;
  1560. list_add_tail(&enc_context->head, &temp_ses->encode_list);
  1561. enc->encode_id = enc_context->encode_id;
  1562. }
  1563. /* return the enc_context & temp_ses */
  1564. *penc_context = enc_context;
  1565. *psession = temp_ses;
  1566. return retVal;
  1567. }
  1568. /**
  1569. * @brief
  1570. *
  1571. * @param argp
  1572. *
  1573. * @return
  1574. */
  1575. static int teei_client_encode_uint32(void *private_data, void *argp)
  1576. {
  1577. struct teei_client_encode_cmd enc;
  1578. int retVal = 0;
  1579. struct teei_session *session = NULL;
  1580. struct teei_encode *enc_context = NULL;
  1581. if (copy_from_user(&enc, argp, sizeof(enc))) {
  1582. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1583. return -EFAULT;
  1584. }
  1585. retVal = teei_client_prepare_encode(private_data, &enc, &enc_context, &session);
  1586. if (retVal != 0) {
  1587. printk("[%s][%d] failed!\n", __func__, __LINE__);
  1588. return retVal;
  1589. }
  1590. /*
  1591. TZDebug("enc.data= %p",(void *)enc.data);
  1592. printk("[%s][%d] enc.encode_id = %x\n", __func__, __LINE__, enc.encode_id);
  1593. printk("[%s][%d] enc_context->encode_id = %x\n", __func__, __LINE__, enc_context->encode_id);
  1594. */
  1595. if (enc.param_type == TEEIC_PARAM_IN) {
  1596. if (enc_context->ker_req_data_addr == NULL) {
  1597. enc_context->ker_req_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1598. if (enc_context->ker_req_data_addr == NULL) {
  1599. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1600. retVal = -ENOMEM;
  1601. goto ret_encode_u32;
  1602. }
  1603. }
  1604. if ((enc_context->enc_req_offset + sizeof(u32) <= TEEI_1K_SIZE) &&
  1605. (enc_context->enc_req_pos < TEEI_MAX_REQ_PARAMS)) {
  1606. u64 addr = enc.data;
  1607. void __user *pt = compat_ptr((unsigned int *)addr);
  1608. u32 value = 0;
  1609. copy_from_user(&value, pt, 4);
  1610. *(u32 *)((char *)enc_context->ker_req_data_addr + enc_context->enc_req_offset) = value;
  1611. TZDebug("value %x", value);
  1612. enc_context->enc_req_offset += sizeof(u32);
  1613. enc_context->meta[enc_context->enc_req_pos].type = TEEI_ENC_UINT32;
  1614. enc_context->meta[enc_context->enc_req_pos].len = sizeof(u32);
  1615. enc_context->meta[enc_context->enc_req_pos].value_flag = enc.value_flag;
  1616. enc_context->meta[enc_context->enc_req_pos].param_pos = enc.param_pos;
  1617. enc_context->meta[enc_context->enc_req_pos].param_pos_type = enc.param_pos_type;
  1618. enc_context->enc_req_pos++;
  1619. } else {
  1620. tz_free_shared_mem(enc_context->ker_req_data_addr, TEEI_1K_SIZE);
  1621. retVal = -ENOMEM;
  1622. goto ret_encode_u32;
  1623. }
  1624. } else if (enc.param_type == TEEIC_PARAM_OUT) {
  1625. if (!enc_context->ker_res_data_addr) {
  1626. enc_context->ker_res_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1627. if (!enc_context->ker_res_data_addr) {
  1628. printk("[%s][%d] tz_malloc failed\n", __func__, __LINE__);
  1629. retVal = -ENOMEM;
  1630. goto ret_encode_u32;
  1631. }
  1632. }
  1633. if ((enc_context->enc_res_offset + sizeof(u32) <= TEEI_1K_SIZE) &&
  1634. (enc_context->enc_res_pos < (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS))) {
  1635. if (enc.data != NULL) {
  1636. u64 addr = enc.data;
  1637. void __user *pt = compat_ptr((unsigned int *)addr);
  1638. /*
  1639. u32 value = 0;
  1640. copy_from_user(&value, pt, 4);
  1641. TZDebug("value %x", value);
  1642. */
  1643. enc_context->meta[enc_context->enc_res_pos].usr_addr = pt;
  1644. } else {
  1645. enc_context->meta[enc_context->enc_res_pos].usr_addr = 0;
  1646. }
  1647. enc_context->enc_res_offset += sizeof(u32);
  1648. enc_context->meta[enc_context->enc_res_pos].type = TEEI_ENC_UINT32;
  1649. enc_context->meta[enc_context->enc_res_pos].len = sizeof(u32);
  1650. enc_context->meta[enc_context->enc_res_pos].value_flag = enc.value_flag;
  1651. enc_context->meta[enc_context->enc_res_pos].param_pos = enc.param_pos;
  1652. enc_context->meta[enc_context->enc_res_pos].param_pos_type = enc.param_pos_type;
  1653. enc_context->enc_res_pos++;
  1654. } else {
  1655. /* kfree(enc_context->ker_res_data_addr); */
  1656. tz_free_shared_mem(enc_context->ker_res_data_addr, TEEI_1K_SIZE);
  1657. retVal = -ENOMEM;
  1658. goto ret_encode_u32;
  1659. }
  1660. }
  1661. ret_encode_u32:
  1662. if (copy_to_user(argp, &enc, sizeof(enc))) {
  1663. TERR("copy from user failed ");
  1664. retVal = -EFAULT;
  1665. }
  1666. return retVal;
  1667. }
  1668. /**
  1669. * @brief
  1670. *
  1671. * @param argp
  1672. *
  1673. * @return
  1674. */
  1675. static int teei_client_encode_array(void *private_data, void *argp)
  1676. {
  1677. struct teei_client_encode_cmd enc;
  1678. int retVal = 0;
  1679. struct teei_encode *enc_context = NULL;
  1680. struct teei_session *session = NULL;
  1681. if (copy_from_user(&enc, argp, sizeof(enc))) {
  1682. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1683. return -EFAULT;
  1684. }
  1685. retVal = teei_client_prepare_encode(private_data, &enc, &enc_context, &session);
  1686. if (retVal)
  1687. goto return_func;
  1688. if (enc.param_type == TEEIC_PARAM_IN) {
  1689. if (NULL == enc_context->ker_req_data_addr) {
  1690. /* printk("[%s][%d] allocate req data data.\n", __func__, __LINE__); */
  1691. enc_context->ker_req_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1692. if (!enc_context->ker_req_data_addr) {
  1693. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1694. retVal = -ENOMEM;
  1695. goto ret_encode_array;
  1696. }
  1697. }
  1698. if ((enc_context->enc_req_offset + enc.len <= TEEI_1K_SIZE) &&
  1699. (enc_context->enc_req_pos < TEEI_MAX_REQ_PARAMS)) {
  1700. if (copy_from_user(
  1701. (char *)enc_context->ker_req_data_addr + enc_context->enc_req_offset,
  1702. enc.data , enc.len)) {
  1703. printk("[%s][%d] copy from user failed.\n", __func__, __LINE__);
  1704. retVal = -EFAULT;
  1705. goto ret_encode_array;
  1706. }
  1707. enc_context->enc_req_offset += enc.len;
  1708. enc_context->meta[enc_context->enc_req_pos].type = TEEI_ENC_ARRAY;
  1709. enc_context->meta[enc_context->enc_req_pos].len = enc.len;
  1710. enc_context->meta[enc_context->enc_req_pos].param_pos = enc.param_pos;
  1711. enc_context->meta[enc_context->enc_req_pos].param_pos_type = enc.param_pos_type;
  1712. enc_context->enc_req_pos++;
  1713. } else {
  1714. /* kfree(enc_context->ker_req_data_addr); */
  1715. tz_free_shared_mem(enc_context->ker_req_data_addr, TEEI_1K_SIZE);
  1716. retVal = -ENOMEM;
  1717. goto ret_encode_array;
  1718. }
  1719. } else if (enc.param_type == TEEIC_PARAM_OUT) {
  1720. if (NULL == enc_context->ker_res_data_addr) {
  1721. enc_context->ker_res_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1722. if (NULL == enc_context->ker_res_data_addr) {
  1723. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1724. retVal = -ENOMEM;
  1725. goto ret_encode_array;
  1726. }
  1727. }
  1728. if ((enc_context->enc_res_offset + enc.len <= TEEI_1K_SIZE) &&
  1729. (enc_context->enc_res_pos <
  1730. (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS))) {
  1731. if (enc.data != NULL) {
  1732. enc_context->meta[enc_context->enc_res_pos].usr_addr
  1733. = (unsigned long)enc.data;
  1734. } else {
  1735. enc_context->meta[enc_context->enc_res_pos].usr_addr = 0;
  1736. }
  1737. enc_context->enc_res_offset += enc.len;
  1738. enc_context->meta[enc_context->enc_res_pos].type = TEEI_ENC_ARRAY;
  1739. enc_context->meta[enc_context->enc_res_pos].len = enc.len;
  1740. enc_context->meta[enc_context->enc_res_pos].param_pos = enc.param_pos;
  1741. enc_context->meta[enc_context->enc_res_pos].param_pos_type = enc.param_pos_type;
  1742. enc_context->enc_res_pos++;
  1743. } else {
  1744. /* kfree(enc_context->ker_res_data_addr); */
  1745. tz_free_shared_mem(enc_context->ker_req_data_addr, TEEI_1K_SIZE);
  1746. retVal = -ENOMEM;
  1747. goto ret_encode_array;
  1748. }
  1749. }
  1750. ret_encode_array:
  1751. if (copy_to_user(argp, &enc, sizeof(enc))) {
  1752. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1753. return -EFAULT;
  1754. }
  1755. return_func:
  1756. TDEBUG("[%s][%d] teei_client_encode_array end!\n", __func__, __LINE__);
  1757. return retVal;
  1758. }
  1759. /**
  1760. * @brief
  1761. *
  1762. * @param argp
  1763. *
  1764. * @return
  1765. */
  1766. static int teei_client_encode_mem_ref(void *private_data, void *argp)
  1767. {
  1768. struct teei_client_encode_cmd enc;
  1769. int retVal = 0;
  1770. int shared_mem_found = 0;
  1771. struct teei_encode *enc_context = NULL;
  1772. struct teei_session *session = NULL;
  1773. struct teei_shared_mem *temp_shared_mem = NULL;
  1774. if (copy_from_user(&enc, argp, sizeof(enc))) {
  1775. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1776. return -EFAULT;
  1777. }
  1778. retVal = teei_client_prepare_encode(private_data, &enc, &enc_context, &session);
  1779. if (retVal != 0)
  1780. goto return_func;
  1781. list_for_each_entry(temp_shared_mem, &session->shared_mem_list, s_head) {
  1782. u64 addr = enc.data;
  1783. if (temp_shared_mem && temp_shared_mem->index == (u32 *)addr) {
  1784. shared_mem_found = 1;
  1785. break;
  1786. }
  1787. }
  1788. if (shared_mem_found == 0) {
  1789. struct teei_context *temp_cont = NULL;
  1790. list_for_each_entry(temp_cont,
  1791. &teei_contexts_head.context_list,
  1792. link) {
  1793. if (temp_cont->cont_id == (unsigned long)private_data) {
  1794. list_for_each_entry(temp_shared_mem,
  1795. &temp_cont->shared_mem_list,
  1796. head) {
  1797. if (temp_shared_mem->index == (u32)enc.data) {
  1798. shared_mem_found = 1;
  1799. break;
  1800. }
  1801. }
  1802. break;
  1803. }
  1804. if (shared_mem_found == 1)
  1805. break;
  1806. }
  1807. }
  1808. if (!shared_mem_found) {
  1809. retVal = -EINVAL;
  1810. goto return_func;
  1811. }
  1812. if (enc.param_type == TEEIC_PARAM_IN) {
  1813. if (NULL == enc_context->ker_req_data_addr) {
  1814. enc_context->ker_req_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1815. if (NULL == enc_context->ker_req_data_addr) {
  1816. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1817. retVal = -ENOMEM;
  1818. goto ret_encode_array;
  1819. }
  1820. }
  1821. if ((enc_context->enc_req_offset + sizeof(u32) <= TEEI_1K_SIZE) &&
  1822. (enc_context->enc_req_pos < TEEI_MAX_REQ_PARAMS)) {
  1823. *(u32 *)((char *)enc_context->ker_req_data_addr + enc_context->enc_req_offset)
  1824. = virt_to_phys((char *)temp_shared_mem->k_addr+enc.offset);
  1825. Flush_Dcache_By_Area((unsigned long)(temp_shared_mem->k_addr+enc.offset),
  1826. (unsigned long)temp_shared_mem->k_addr+enc.offset+enc.len);
  1827. enc_context->enc_req_offset += sizeof(u32);
  1828. enc_context->meta[enc_context->enc_req_pos].usr_addr
  1829. = (unsigned long)((char *)temp_shared_mem->u_addr + enc.offset);
  1830. enc_context->meta[enc_context->enc_req_pos].type = TEEI_MEM_REF;
  1831. enc_context->meta[enc_context->enc_req_pos].len = enc.len;
  1832. enc_context->meta[enc_context->enc_req_pos].param_pos = enc.param_pos;
  1833. enc_context->meta[enc_context->enc_req_pos].param_pos_type = enc.param_pos_type;
  1834. enc_context->enc_req_pos++;
  1835. } else {
  1836. /* kfree(enc_context->ker_req_data_addr); */
  1837. tz_free_shared_mem(enc_context->ker_req_data_addr, TEEI_1K_SIZE);
  1838. retVal = -ENOMEM;
  1839. goto ret_encode_array;
  1840. }
  1841. } else if (enc.param_type == TEEIC_PARAM_OUT) {
  1842. if (!enc_context->ker_res_data_addr) {
  1843. enc_context->ker_res_data_addr = tz_malloc_shared_mem(TEEI_1K_SIZE, GFP_KERNEL | GFP_DMA);
  1844. if (!enc_context->ker_res_data_addr) {
  1845. printk("[%s][%d] tz_malloc failed!\n", __func__, __LINE__);
  1846. retVal = -ENOMEM;
  1847. goto ret_encode_array;
  1848. }
  1849. }
  1850. if ((enc_context->enc_res_offset + sizeof(u32) <= TEEI_1K_SIZE) &&
  1851. (enc_context->enc_res_pos < (TEEI_MAX_RES_PARAMS + TEEI_MAX_REQ_PARAMS))) {
  1852. *(u32 *)((char *)enc_context->ker_res_data_addr +
  1853. enc_context->enc_res_offset)
  1854. = virt_to_phys((char *)temp_shared_mem->k_addr + enc.offset);
  1855. enc_context->enc_res_offset += sizeof(u32);
  1856. enc_context->meta[enc_context->enc_res_pos].usr_addr
  1857. = (unsigned long)((char *)temp_shared_mem->u_addr + enc.offset);
  1858. enc_context->meta[enc_context->enc_res_pos].type
  1859. = TEEI_MEM_REF;
  1860. enc_context->meta[enc_context->enc_res_pos].len = enc.len;
  1861. enc_context->meta[enc_context->enc_res_pos].param_pos = enc.param_pos;
  1862. enc_context->meta[enc_context->enc_res_pos].param_pos_type = enc.param_pos_type;
  1863. enc_context->enc_res_pos++;
  1864. } else {
  1865. /* kfree(enc_context->ker_res_data_addr); */
  1866. tz_free_shared_mem(enc_context->ker_res_data_addr, TEEI_1K_SIZE);
  1867. retVal = -ENOMEM;
  1868. goto ret_encode_array;
  1869. }
  1870. }
  1871. ret_encode_array:
  1872. if (copy_to_user(argp, &enc, sizeof(enc))) {
  1873. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1874. return -EFAULT;
  1875. }
  1876. return_func:
  1877. /* printk("[%s][%d] teei_client_encode_mem_ref end.\n", __func__, __LINE__); */
  1878. return retVal;
  1879. }
  1880. static int print_context(void)
  1881. {
  1882. struct teei_context *temp_cont = NULL;
  1883. struct teei_session *temp_sess = NULL;
  1884. struct teei_encode *dec_context = NULL;
  1885. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1886. printk("[%s][%d] context id [%lx]\n", __func__, __LINE__, temp_cont->cont_id);
  1887. list_for_each_entry(temp_sess, &temp_cont->sess_link, link) {
  1888. printk("[%s][%d] session id [%x]\n", __func__, __LINE__, temp_sess->sess_id);
  1889. /*
  1890. list_for_each_entry(dec_context, &temp_sess->encode_list, head) {
  1891. printk("[%s][%d] encode_id [%x]\n", __func__, __LINE__, dec_context->encode_id);
  1892. }
  1893. */
  1894. }
  1895. }
  1896. return 0;
  1897. }
  1898. /**
  1899. * @brief
  1900. *
  1901. * @param dec
  1902. * @param pdec_context
  1903. *
  1904. * @return
  1905. */
  1906. static int teei_client_prepare_decode(void *private_data,
  1907. struct teei_client_encode_cmd *dec,
  1908. struct teei_encode **pdec_context)
  1909. {
  1910. struct teei_context *temp_cont = NULL;
  1911. struct teei_session *temp_ses = NULL;
  1912. struct teei_encode *dec_context = NULL;
  1913. int session_found = 0;
  1914. int enc_found = 0;
  1915. unsigned long dev_file_id = (unsigned long)private_data;
  1916. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  1917. if (temp_cont->cont_id == dev_file_id) {
  1918. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  1919. if (temp_ses->sess_id == dec->session_id) {
  1920. session_found = 1;
  1921. break;
  1922. }
  1923. }
  1924. break;
  1925. }
  1926. }
  1927. if (0 == session_found) {
  1928. printk("[%s][%d] session not found!\n", __func__, __LINE__);
  1929. return -EINVAL;
  1930. }
  1931. if (dec->encode_id != -1) {
  1932. list_for_each_entry(dec_context, &temp_ses->encode_list, head) {
  1933. if (dec_context->encode_id == dec->encode_id) {
  1934. enc_found = 1;
  1935. break;
  1936. }
  1937. }
  1938. }
  1939. /* print_context(); */
  1940. if (0 == enc_found) {
  1941. printk("[%s][%d] encode[%x] not found!\n", __func__, __LINE__, dec->encode_id);
  1942. return -EINVAL;
  1943. }
  1944. *pdec_context = dec_context;
  1945. return 0;
  1946. }
  1947. /**
  1948. * @brief Decode the uint32 result
  1949. *
  1950. * @param argp
  1951. *
  1952. * @return EFAULT: fail to copy data from user space OR to user space.
  1953. * 0: success
  1954. */
  1955. static int teei_client_decode_uint32(void *private_data, void *argp)
  1956. {
  1957. struct teei_client_encode_cmd dec;
  1958. int retVal = 0;
  1959. struct teei_encode *dec_context = NULL;
  1960. if (copy_from_user(&dec, argp, sizeof(dec))) {
  1961. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  1962. return -EFAULT;
  1963. }
  1964. retVal = teei_client_prepare_decode(private_data, &dec, &dec_context);
  1965. if (retVal != 0) {
  1966. printk("[%s][%d] teei_client_prepare_decode failed!\n", __func__, __LINE__);
  1967. goto return_func;
  1968. }
  1969. if ((dec_context->dec_res_pos <= dec_context->enc_res_pos) &&
  1970. (dec_context->meta[dec_context->dec_res_pos].type == TEEI_ENC_UINT32)) {
  1971. if (dec_context->meta[dec_context->dec_res_pos].usr_addr) {
  1972. dec.data = (void *)dec_context->meta[dec_context->dec_res_pos].usr_addr;
  1973. TZDebug("data %x", dec.data);
  1974. }
  1975. u32 value = 0;
  1976. u64 addr = dec.data;
  1977. void __user *pt = compat_ptr((unsigned int *)addr);
  1978. /* *(u32 *)dec.data = *((u32 *)((char *)dec_context->ker_res_data_addr + dec_context->dec_offset)); */
  1979. value = *((u32 *)((char *)dec_context->ker_res_data_addr + dec_context->dec_offset));
  1980. copy_to_user(pt, &value, 4);
  1981. dec_context->dec_offset += sizeof(u32);
  1982. dec_context->dec_res_pos++;
  1983. }
  1984. if (copy_to_user(argp, &dec, sizeof(dec))) {
  1985. printk("[%s][%d] copy to user failed.\n", __func__, __LINE__);
  1986. return -EFAULT;
  1987. }
  1988. return_func:
  1989. return retVal;
  1990. }
  1991. /**
  1992. * @brief
  1993. *
  1994. * @param argp
  1995. *
  1996. * @return
  1997. */
  1998. static int teei_client_decode_array_space(void *private_data, void *argp)
  1999. {
  2000. struct teei_client_encode_cmd dec;
  2001. int retVal = 0;
  2002. struct teei_encode *dec_context = NULL;
  2003. if (copy_from_user(&dec, argp, sizeof(dec))) {
  2004. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  2005. return -EFAULT;
  2006. }
  2007. retVal = teei_client_prepare_decode(private_data, &dec, &dec_context);
  2008. if (retVal != 0)
  2009. goto return_func;
  2010. if ((dec_context->dec_res_pos <= dec_context->enc_res_pos) &&
  2011. (dec_context->meta[dec_context->dec_res_pos].type
  2012. == TEEI_ENC_ARRAY)) {
  2013. if (dec_context->meta[dec_context->dec_res_pos].len >=
  2014. dec_context->meta[dec_context->dec_res_pos].ret_len) {
  2015. if (dec_context->meta[dec_context->dec_res_pos].usr_addr)
  2016. dec.data = (void *)dec_context->meta[dec_context->dec_res_pos].usr_addr;
  2017. if (copy_to_user(dec.data, (char *)dec_context->ker_res_data_addr + dec_context->dec_offset,
  2018. dec_context->meta[dec_context->dec_res_pos].ret_len)) {
  2019. printk("[%s][%d] copy from user failed while copying array!\n", __func__, __LINE__);
  2020. retVal = -EFAULT;
  2021. goto return_func;
  2022. }
  2023. } else {
  2024. printk("[%s][%d] buffer length is small. Length required %x and supplied length %x,pos %x ",
  2025. __func__, __LINE__,
  2026. dec_context->meta[dec_context->dec_res_pos].ret_len,
  2027. dec_context->meta[dec_context->dec_res_pos].len,
  2028. dec_context->dec_res_pos);
  2029. retVal = -EFAULT;
  2030. goto return_func;
  2031. }
  2032. dec.len = dec_context->meta[dec_context->dec_res_pos].ret_len;
  2033. dec_context->dec_offset += dec_context->meta[dec_context->dec_res_pos].len;
  2034. dec_context->dec_res_pos++;
  2035. } else if ((dec_context->dec_res_pos <= dec_context->enc_res_pos) &&
  2036. (dec_context->meta[dec_context->dec_res_pos].type == TEEI_MEM_REF)) {
  2037. if (dec_context->meta[dec_context->dec_res_pos].len >=
  2038. dec_context->meta[dec_context->dec_res_pos].ret_len) {
  2039. dec.data = (void *)dec_context->meta[dec_context->dec_res_pos].usr_addr;
  2040. unsigned long pmem = *(u32 *)((char *)dec_context->ker_res_data_addr + dec_context->dec_offset);
  2041. char *mem = NULL;
  2042. unsigned long addr = (unsigned long)phys_to_virt(pmem);
  2043. mem = (char *)addr;
  2044. Invalidate_Dcache_By_Area((unsigned long)mem,
  2045. (unsigned long)mem+dec_context->meta[dec_context->dec_res_pos].ret_len + 1);
  2046. } else {
  2047. printk("[%s][%d] buffer length is small. Length required %x and supplied length %x",
  2048. __func__, __LINE__,
  2049. dec_context->meta[dec_context->dec_res_pos].ret_len,
  2050. dec_context->meta[dec_context->dec_res_pos].len);
  2051. retVal = -EFAULT;
  2052. goto return_func;
  2053. }
  2054. dec.len = dec_context->meta[dec_context->dec_res_pos].ret_len;
  2055. dec_context->dec_offset += sizeof(u32);
  2056. dec_context->dec_res_pos++;
  2057. }
  2058. else {
  2059. printk("[%s][%d] invalid data type or decoder at wrong position!\n", __func__, __LINE__);
  2060. retVal = -EINVAL;
  2061. goto return_func;
  2062. }
  2063. if (copy_to_user(argp, &dec, sizeof(dec))) {
  2064. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  2065. retVal = -EFAULT;
  2066. goto return_func;
  2067. }
  2068. return_func:
  2069. TDEBUG("[%s][%d] teei_client_decode_array_space end.\n", __func__, __LINE__);
  2070. return retVal;
  2071. }
  2072. /**
  2073. * @brief
  2074. *
  2075. * @param argp
  2076. *
  2077. * @return
  2078. */
  2079. static int teei_client_get_decode_type(void *private_data, void *argp)
  2080. {
  2081. struct teei_client_encode_cmd dec;
  2082. int retVal = 0;
  2083. struct teei_encode *dec_context = NULL;
  2084. if (copy_from_user(&dec, argp, sizeof(dec))) {
  2085. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  2086. return -EFAULT;
  2087. }
  2088. retVal = teei_client_prepare_decode(private_data, &dec, &dec_context);
  2089. if (retVal != 0)
  2090. return retVal;
  2091. if (dec_context->dec_res_pos <= dec_context->enc_res_pos)
  2092. dec.data = (unsigned long)dec_context->meta[dec_context->dec_res_pos].type;
  2093. else
  2094. return -EINVAL;
  2095. if (copy_to_user(argp, &dec, sizeof(dec))) {
  2096. printk("[%s][%d] copy to user failed!\n", __func__, __LINE__);
  2097. return -EFAULT;
  2098. }
  2099. return retVal;
  2100. }
  2101. /**
  2102. * @brief
  2103. *
  2104. * @param argp
  2105. *
  2106. * @return
  2107. */
  2108. static int teei_client_shared_mem_alloc(void *private_data, void *argp)
  2109. {
  2110. #if 0
  2111. struct teei_shared_mem *temp_shared_mem = NULL;
  2112. struct teei_session_shared_mem_info mem_info;
  2113. struct teei_context *temp_cont = NULL;
  2114. struct teei_session *temp_ses = NULL;
  2115. int session_found = 0;
  2116. unsigned long dev_file_id = (unsigned long)private_data;
  2117. if (copy_from_user(&mem_info, argp, sizeof(mem_info))) {
  2118. printk("[%s][%d] copy from user failed!\n", __func__, __LINE__);
  2119. return -EFAULT;
  2120. }
  2121. /*
  2122. printk("[%s][%d] service id 0x%lx session id 0x%lx user mem addr 0x%p.\n",
  2123. __func__, __LINE__,
  2124. mem_info.service_id,
  2125. mem_info.session_id,
  2126. mem_info.user_mem_addr);
  2127. */
  2128. list_for_each_entry(temp_cont,
  2129. &teei_contexts_head.context_list,
  2130. link) {
  2131. if (temp_cont->cont_id == dev_file_id) {
  2132. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  2133. if (temp_ses->sess_id == mem_info.session_id) {
  2134. session_found = 1;
  2135. break;
  2136. }
  2137. }
  2138. break;
  2139. }
  2140. }
  2141. if (session_found == 0) {
  2142. printk("[%s][%d] session not found!\n", __func__, __LINE__);
  2143. return -EINVAL;
  2144. }
  2145. list_for_each_entry(temp_shared_mem, &temp_cont->shared_mem_list, head) {
  2146. if (temp_shared_mem->index == (void *)mem_info.user_mem_addr) {
  2147. list_del(&temp_shared_mem->head);
  2148. temp_cont->shared_mem_cnt--;
  2149. list_add_tail(&temp_shared_mem->s_head, &temp_ses->shared_mem_list);
  2150. break;
  2151. }
  2152. }
  2153. #endif
  2154. return 0;
  2155. }
  2156. /**
  2157. * @brief
  2158. *
  2159. * @param argp
  2160. *
  2161. * @return
  2162. */
  2163. static int teei_client_shared_mem_free(void *private_data, void *argp)
  2164. {
  2165. struct teei_shared_mem *temp_shared_mem = NULL;
  2166. struct teei_session_shared_mem_info mem_info;
  2167. struct teei_context *temp_cont = NULL;
  2168. struct teei_session *temp_ses = NULL;
  2169. struct teei_shared_mem *temp_pos = NULL;
  2170. int session_found = 0;
  2171. unsigned long dev_file_id = (unsigned long)private_data;
  2172. if (copy_from_user(&mem_info, argp, sizeof(mem_info))) {
  2173. printk("[%s][%d] copy from user failed.\n", __func__, __LINE__);
  2174. return -EFAULT;
  2175. }
  2176. TZDebug("session id 0x%x user mem addr %x ",
  2177. mem_info.session_id,
  2178. mem_info.user_mem_addr);
  2179. list_for_each_entry(temp_cont,
  2180. &teei_contexts_head.context_list,
  2181. link) {
  2182. if (temp_cont->cont_id == dev_file_id) {
  2183. TDEBUG("found file id");
  2184. list_for_each_entry_safe(temp_shared_mem,
  2185. temp_pos,
  2186. &temp_cont->shared_mem_list,
  2187. head) {
  2188. if (temp_shared_mem && temp_shared_mem->u_addr == mem_info.user_mem_addr) {
  2189. list_del(&temp_shared_mem->head);
  2190. if (temp_shared_mem->k_addr)
  2191. free_pages((u64)temp_shared_mem->k_addr,
  2192. get_order(ROUND_UP(temp_shared_mem->len, SZ_4K)));
  2193. kfree(temp_shared_mem);
  2194. }
  2195. }
  2196. }
  2197. }
  2198. #if 0
  2199. list_for_each_entry(temp_cont, &teei_contexts_head.context_list, link) {
  2200. if (temp_cont->cont_id == dev_file_id) {
  2201. list_for_each_entry(temp_ses, &temp_cont->sess_link, link) {
  2202. TZDebug("list:session id %x", temp_ses->sess_id);
  2203. if (temp_ses->sess_id == mem_info.session_id) {
  2204. session_found = 1;
  2205. break;
  2206. }
  2207. }
  2208. break;
  2209. }
  2210. }
  2211. if (session_found == 0) {
  2212. printk("[%s][%d] session not found!\n", __func__, __LINE__);
  2213. return -EINVAL;
  2214. }
  2215. list_for_each_entry(temp_shared_mem, &temp_ses->shared_mem_list, s_head) {
  2216. if (temp_shared_mem && temp_shared_mem->index == (void *)mem_info.user_mem_addr) {
  2217. list_del(&temp_shared_mem->s_head);
  2218. if (temp_shared_mem->k_addr)
  2219. free_pages((unsigned long)temp_shared_mem->k_addr,
  2220. get_order(ROUND_UP(temp_shared_mem->len, SZ_4K)));
  2221. kfree(temp_shared_mem);
  2222. break;
  2223. }
  2224. }
  2225. #endif
  2226. return 0;
  2227. }
  2228. /**
  2229. * @brief
  2230. *
  2231. * @param file
  2232. * @param cmd
  2233. * @param arg
  2234. * @return
  2235. */
  2236. static long teei_client_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  2237. {
  2238. int retVal = 0;
  2239. void *argp = (void __user *) arg;
  2240. if (teei_config_flag == 0) {
  2241. printk("Error: soter is NOT ready, Can not support IOCTL!\n");
  2242. return -ECANCELED;
  2243. }
  2244. down(&api_lock);
  2245. mutex_lock(&pm_mutex);
  2246. switch (cmd) {
  2247. case TEEI_CLIENT_IOCTL_INITCONTEXT_REQ:
  2248. printk("[%s][%d] TEEI_CLIENT_IOCTL_INITCONTEXT beginning .....\n", __func__, __LINE__);
  2249. retVal = teei_client_context_init(file->private_data, argp);
  2250. if (retVal != 0)
  2251. printk("[%s][%d] failed init context %x.\n", __func__, __LINE__, retVal);
  2252. printk("[%s][%d] TEEI_CLIENT_IOCTL_INITCONTEXT end .....\n", __func__, __LINE__);
  2253. break;
  2254. case TEEI_CLIENT_IOCTL_CLOSECONTEXT_REQ:
  2255. printk("[%s][%d] TEEI_CLIENT_IOCTL_CLOSECONTEXT beginning .....\n", __func__, __LINE__);
  2256. retVal = teei_client_context_close(file->private_data, argp);
  2257. if (retVal != 0)
  2258. printk("[%s][%d] failed close context: %x.\n", __func__, __LINE__, retVal);
  2259. printk("[%s][%d] TEEI_CLIENT_IOCTL_CLOSECONTEXT end .....\n", __func__, __LINE__);
  2260. break;
  2261. case TEEI_CLIENT_IOCTL_SES_INIT_REQ:
  2262. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_INIT beginning .....\n", __func__, __LINE__);
  2263. retVal = teei_client_session_init(file->private_data, argp);
  2264. if (retVal != 0)
  2265. printk("[%s][%d] failed session init: %x.\n", __func__, __LINE__, retVal);
  2266. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_INIT end .....\n", __func__, __LINE__);
  2267. break;
  2268. case TEEI_CLIENT_IOCTL_SES_OPEN_REQ:
  2269. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_OPEN beginning .....\n", __func__, __LINE__);
  2270. retVal = teei_client_session_open(file->private_data, argp);
  2271. if (retVal != 0)
  2272. printk("[%s][%d] failed session open: %x.\n", __func__, __LINE__, retVal);
  2273. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_OPEN end .....\n", __func__, __LINE__);
  2274. break;
  2275. case TEEI_CLIENT_IOCTL_SES_CLOSE_REQ:
  2276. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_CLOSE beginning .....\n", __func__, __LINE__);
  2277. retVal = teei_client_session_close(file->private_data, argp);
  2278. if (retVal != 0)
  2279. printk("[%s][%d] failed session close: %x.\n", __func__, __LINE__, retVal);
  2280. printk("[%s][%d] TEEI_CLIENT_IOCTL_SES_CLOSE end .....\n", __func__, __LINE__);
  2281. break;
  2282. case TEEI_CLIENT_IOCTL_OPERATION_RELEASE:
  2283. printk("[%s][%d] TEEI_CLIENT_IOCTL_OPERATION_RELEASE beginning .....\n", __func__, __LINE__);
  2284. retVal = teei_client_operation_release(file->private_data, argp);
  2285. if (retVal != 0)
  2286. printk("[%s][%d] failed operation release: %x.\n", __func__, __LINE__, retVal);
  2287. printk("[%s][%d] TEEI_CLIENT_IOCTL_OPERATION_RELEASE end .....\n", __func__, __LINE__);
  2288. break;
  2289. case TEEI_CLIENT_IOCTL_SEND_CMD_REQ:
  2290. printk("[%s][%d] TEEI_CLIENT_IOCTL_SEND_CMD beginning .....\n", __func__, __LINE__);
  2291. retVal = teei_client_send_cmd(file->private_data, argp);
  2292. if (retVal != 0)
  2293. printk("[%s][%d] failed send cmd: %x.\n", __func__, __LINE__, retVal);
  2294. printk("[%s][%d] TEEI_CLIENT_IOCTL_SEND_CMD end .....\n", __func__, __LINE__);
  2295. break;
  2296. case TEEI_CLIENT_IOCTL_GET_DECODE_TYPE:
  2297. printk("[%s][%d] TEEI_CLIENT_IOCTL_GET_DECODE beginning .....\n", __func__, __LINE__);
  2298. retVal = teei_client_get_decode_type(file->private_data, argp);
  2299. if (retVal != 0)
  2300. printk("[%s][%d] failed decode cmd: %x.\n", __func__, __LINE__, retVal);
  2301. printk("[%s][%d] TEEI_CLIENT_IOCTL_GET_DECODE end .....\n", __func__, __LINE__);
  2302. break;
  2303. case TEEI_CLIENT_IOCTL_ENC_UINT32:
  2304. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_UINT32 beginning .....\n", __func__, __LINE__);
  2305. retVal = teei_client_encode_uint32(file->private_data, argp);
  2306. if (retVal != 0)
  2307. printk("[%s][%d] failed teei_client_encode_cmd: %x.\n", __func__, __LINE__, retVal);
  2308. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_UINT32 end .....\n", __func__, __LINE__);
  2309. break;
  2310. case TEEI_CLIENT_IOCTL_DEC_UINT32:
  2311. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_UINT32 beginning .....\n", __func__, __LINE__);
  2312. retVal = teei_client_decode_uint32(file->private_data, argp);
  2313. if (retVal != 0)
  2314. printk("[%s][%d] failed teei_client_decode_cmd: %x.\n", __func__, __LINE__, retVal);
  2315. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_UINT32 end .....\n", __func__, __LINE__);
  2316. break;
  2317. case TEEI_CLIENT_IOCTL_ENC_ARRAY:
  2318. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_ARRAY beginning .....\n", __func__, __LINE__);
  2319. retVal = teei_client_encode_array(file->private_data, argp);
  2320. if (retVal != 0)
  2321. printk("[%s][%d] failed teei_client_encode_cmd: %x.\n", __func__, __LINE__, retVal);
  2322. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_ARRAY end .....\n", __func__, __LINE__);
  2323. break;
  2324. case TEEI_CLIENT_IOCTL_DEC_ARRAY_SPACE:
  2325. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_ARRAY_SPACE beginning .....\n", __func__, __LINE__);
  2326. retVal = teei_client_decode_array_space(file->private_data, argp);
  2327. if (retVal != 0)
  2328. printk("[%s][%d] failed teei_client_decode_cmd: %x.\n", __func__, __LINE__, retVal);
  2329. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_ARRAY_SPACE end .....\n", __func__, __LINE__);
  2330. break;
  2331. case TEEI_CLIENT_IOCTL_ENC_MEM_REF:
  2332. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_MEM_REF beginning .....\n", __func__, __LINE__);
  2333. retVal = teei_client_encode_mem_ref(file->private_data, argp);
  2334. if (retVal != 0)
  2335. printk("[%s][%d] failed teei_client_encode_cmd: %x.\n", __func__, __LINE__, retVal);
  2336. printk("[%s][%d] TEEI_CLIENT_IOCTL_DEC_MEM_REF end .....\n", __func__, __LINE__);
  2337. break;
  2338. case TEEI_CLIENT_IOCTL_ENC_ARRAY_SPACE:
  2339. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_ARRAY_SPACE beginning .....\n", __func__, __LINE__);
  2340. retVal = teei_client_encode_mem_ref(file->private_data, argp);
  2341. if (retVal != 0)
  2342. printk("[%s][%d] failed teei_client_encode_cmd: %x.\n", __func__, __LINE__, retVal);
  2343. printk("[%s][%d] TEEI_CLIENT_IOCTL_ENC_ARRAY_SPACE end .....\n", __func__, __LINE__);
  2344. break;
  2345. case TEEI_CLIENT_IOCTL_SHR_MEM_ALLOCATE_REQ:
  2346. printk("[%s][%d] TEEI_CLIENT_IOCTL_SHR_MEM_ALLOCATE beginning .....\n", __func__, __LINE__);
  2347. retVal = teei_client_shared_mem_alloc(file->private_data, argp);
  2348. if (retVal != 0)
  2349. printk("[%s][%d] failed teei_client_shared_mem_alloc: %x.\n", __func__, __LINE__, retVal);
  2350. printk("[%s][%d] TEEI_CLIENT_IOCTL_SHR_MEM_ALLOCATE end .....\n", __func__, __LINE__);
  2351. break;
  2352. case TEEI_CLIENT_IOCTL_SHR_MEM_FREE_REQ:
  2353. printk("[%s][%d] TEEI_CLIENT_IOCTL_SHR_MEM_FREE beginning .....\n", __func__, __LINE__);
  2354. retVal = teei_client_shared_mem_free(file->private_data, argp);
  2355. if (retVal != 0)
  2356. printk("[%s][%d] failed teei_client_shared_mem_free: %x.\n", __func__, __LINE__, retVal);
  2357. printk("[%s][%d] TEEI_CLIENT_IOCTL_SHR_MEM_FREE end .....\n", __func__, __LINE__);
  2358. break;
  2359. case TEEI_GET_TEEI_CONFIG_STAT:
  2360. printk("[%s][%d] TEEI_GET_TEEI_CONFIG_STAT beginning .....\n", __func__, __LINE__);
  2361. retVal = teei_config_flag;
  2362. printk("[%s][%d] TEEI_GET_TEEI_CONFIG_STAT end .....\n", __func__, __LINE__);
  2363. break;
  2364. default:
  2365. printk("[%s][%d] command not found!\n", __func__, __LINE__);
  2366. retVal = -EINVAL;
  2367. }
  2368. mutex_unlock(&pm_mutex);
  2369. up(&api_lock);
  2370. return retVal;
  2371. }
  2372. /**
  2373. * @brief The open operation of /dev/teei_client device node.
  2374. *
  2375. * @param inode
  2376. * @param file
  2377. *
  2378. * @return ENOMEM: no enough memory in the linux kernel
  2379. * 0: on success
  2380. */
  2381. static int teei_client_open(struct inode *inode, struct file *file)
  2382. {
  2383. struct teei_context *new_context = NULL;
  2384. device_file_cnt++;
  2385. file->private_data = (void *)device_file_cnt;
  2386. new_context = (struct teei_context *)tz_malloc(sizeof(struct teei_context), GFP_KERNEL);
  2387. if (new_context == NULL) {
  2388. printk("tz_malloc failed for new dev file allocation!\n");
  2389. return -ENOMEM;
  2390. }
  2391. new_context->cont_id = device_file_cnt;
  2392. INIT_LIST_HEAD(&(new_context->sess_link));
  2393. INIT_LIST_HEAD(&(new_context->link));
  2394. new_context->shared_mem_cnt = 0;
  2395. INIT_LIST_HEAD(&(new_context->shared_mem_list));
  2396. sema_init(&(new_context->cont_lock), 0);
  2397. down_write(&(teei_contexts_head.teei_contexts_sem));
  2398. list_add(&(new_context->link), &(teei_contexts_head.context_list));
  2399. teei_contexts_head.dev_file_cnt++;
  2400. up_write(&(teei_contexts_head.teei_contexts_sem));
  2401. return 0;
  2402. }
  2403. /**
  2404. * @brief The release operation of /dev/teei_client device node.
  2405. *
  2406. * @param inode: device inode structure
  2407. * @param file: struct file
  2408. *
  2409. * @return 0: on success
  2410. */
  2411. static int teei_client_release(struct inode *inode, struct file *file)
  2412. {
  2413. int retVal = 0;
  2414. retVal = teei_client_service_exit(file->private_data);
  2415. return retVal;
  2416. }
  2417. #endif
  2418. static irqreturn_t mt_deint_soft_revert_isr(unsigned irq, struct irq_desc *desc)
  2419. {
  2420. irq_set_irq_type(irq, IRQF_TRIGGER_RISING);
  2421. return IRQ_HANDLED;
  2422. }
  2423. static void eint_gic_attach(void)
  2424. {
  2425. int ret = 0;
  2426. ret = mt_eint_set_deint(6, 189);
  2427. if (ret == 0)
  2428. printk("mt_eint_set_deint done\n");
  2429. else {
  2430. printk("mt_eint_set_deint fail\n");
  2431. return;
  2432. }
  2433. mt_irq_set_polarity(189, IRQF_TRIGGER_FALLING); /*IRQF_TRIGGER_RISING*/
  2434. /*
  2435. ret = request_irq(189, (irq_handler_t)mt_deint_soft_revert_isr,
  2436. IRQF_TRIGGER_RISING, "EINT-AUTOUNMASK", NULL);
  2437. if (ret > 0) {
  2438. printk(KERN_ERR "EINT IRQ LINE NOT AVAILABLE!!\n");
  2439. return;
  2440. }
  2441. printk("EINT %d request_irq done\n",eint_num);
  2442. irq_set_irq_type(189, IRQF_TRIGGER_RISING);
  2443. printk("trigger EINT %d done\n",eint_num);
  2444. printk("MASK 0x%x\n",mt_eint_get_mask(6));
  2445. mt_eint_clr_deint(eint_num);
  2446. */
  2447. }
  2448. /**
  2449. * @brief
  2450. *
  2451. * @return
  2452. */
  2453. static void teei_client_smc_init(void)
  2454. {
  2455. #if 0
  2456. neu_disable_touch_irq();
  2457. eint_gic_attach();
  2458. enable_clock(25, "i2c");
  2459. n_switch_to_t_os_stage2();
  2460. switch_enable_flag = 1;
  2461. mt_eint_clr_deint(6);
  2462. neu_enable_touch_irq();
  2463. #endif
  2464. }
  2465. #if 0
  2466. void NQ_init(unsigned long NQ_buff)
  2467. {
  2468. memset((char *)NQ_buff, 0, NQ_BUFF_SIZE);
  2469. }
  2470. static __always_inline unsigned int get_end_index(struct NQ_head *nq_head)
  2471. {
  2472. if (nq_head->end_index == BLOCK_MAX_COUNT)
  2473. return 1;
  2474. else
  2475. return nq_head->end_index + 1;
  2476. }
  2477. #endif
  2478. static irqreturn_t nt_sched_irq_handler(void)
  2479. {
  2480. int cpu_id = raw_smp_processor_id();
  2481. if (boot_soter_flag == START_STATUS) {
  2482. forward_call_flag = GLSCH_FOR_SOTER;
  2483. up(&smc_lock);
  2484. return IRQ_HANDLED;
  2485. } else {
  2486. up(&smc_lock);
  2487. return IRQ_HANDLED;
  2488. }
  2489. }
  2490. int register_sched_irq_handler(void)
  2491. {
  2492. int retVal = 0;
  2493. #ifdef CONFIG_OF
  2494. int irq_num = 0;
  2495. struct device_node *node;
  2496. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2497. irq_num = irq_of_parse_and_map(node, 5);
  2498. retVal = request_irq(irq_num, nt_sched_irq_handler, 0, "tz_drivers_service", NULL);
  2499. if (retVal)
  2500. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2501. else
  2502. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2503. #else
  2504. /* register 284 IRQ */
  2505. retVal = request_irq(SCHED_IRQ, nt_sched_irq_handler, 0, "tz_drivers_service", NULL);
  2506. if (retVal)
  2507. printk("ERROR for request_irq %d error code : %d.\n", SCHED_IRQ, retVal);
  2508. else
  2509. printk("request irq [ %d ] OK.\n", SCHED_IRQ);
  2510. #endif
  2511. return 0;
  2512. }
  2513. static irqreturn_t nt_soter_irq_handler(void)
  2514. {
  2515. int cpu_id = raw_smp_processor_id();
  2516. irq_call_flag = GLSCH_HIGH;
  2517. up(&smc_lock);
  2518. if (teei_config_flag == 1) {
  2519. complete(&global_down_lock);
  2520. }
  2521. return IRQ_HANDLED;
  2522. }
  2523. int register_soter_irq_handler(void)
  2524. {
  2525. int retVal = 0;
  2526. #ifdef CONFIG_OF
  2527. int irq_num = 0;
  2528. struct device_node *node;
  2529. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2530. irq_num = irq_of_parse_and_map(node, 6);
  2531. retVal = request_irq(irq_num, nt_soter_irq_handler, 0, "tz_drivers_service", NULL);
  2532. if (retVal)
  2533. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2534. else
  2535. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2536. #else
  2537. /* register 285 IRQ */
  2538. retVal = request_irq(SOTER_IRQ, nt_soter_irq_handler, 0, "tz_drivers_service", NULL);
  2539. if (retVal)
  2540. printk("ERROR for request_irq %d error code : %d.\n", SOTER_IRQ, retVal);
  2541. else
  2542. printk("request irq [ %d ] OK.\n", SOTER_IRQ);
  2543. #endif
  2544. return 0;
  2545. }
  2546. static irqreturn_t nt_error_irq_handler(void)
  2547. {
  2548. printk("secure system ERROR !\n");
  2549. soter_error_flag = 1;
  2550. up(&(boot_sema));
  2551. up(&smc_lock);
  2552. return IRQ_HANDLED;
  2553. }
  2554. int register_error_irq_handler(void)
  2555. {
  2556. int retVal = 0;
  2557. #ifdef CONFIG_OF
  2558. int irq_num = 0;
  2559. struct device_node *node;
  2560. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2561. irq_num = irq_of_parse_and_map(node, 0);
  2562. retVal = request_irq(irq_num, nt_error_irq_handler, 0, "tz_drivers_service", NULL);
  2563. if (retVal)
  2564. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2565. else
  2566. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2567. #else
  2568. /* register 276 IRQ */
  2569. retVal = request_irq(SOTER_ERROR_IRQ, nt_error_irq_handler, 0, "tz_drivers_service", NULL);
  2570. if (retVal)
  2571. printk("ERROR for request_irq %d error code : %d.\n", SOTER_ERROR_IRQ, retVal);
  2572. else
  2573. printk("request irq [ %d ] OK.\n", SOTER_ERROR_IRQ);
  2574. #endif
  2575. return 0;
  2576. }
  2577. static irqreturn_t nt_fp_ack_handler(void)
  2578. {
  2579. fp_call_flag = GLSCH_NONE;
  2580. up(&boot_sema);
  2581. up(&smc_lock);
  2582. return IRQ_HANDLED;
  2583. }
  2584. int register_fp_ack_handler(void)
  2585. {
  2586. int retVal = 0;
  2587. #ifdef CONFIG_OF
  2588. int irq_num = 0;
  2589. struct device_node *node;
  2590. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2591. irq_num = irq_of_parse_and_map(node, 7);
  2592. retVal = request_irq(irq_num, nt_fp_ack_handler, 0, "tz_drivers_service", NULL);
  2593. if (retVal)
  2594. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2595. else
  2596. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2597. #else
  2598. /* register 287 IRQ */
  2599. retVal = request_irq(FP_ACK_IRQ, nt_fp_ack_handler, 0, "tz_drivers_service", NULL);
  2600. if (retVal)
  2601. printk("ERROR for request_irq %d error code : %d.\n", FP_ACK_IRQ, retVal);
  2602. else
  2603. printk("request irq [ %d ] OK.\n", FP_ACK_IRQ);
  2604. #endif
  2605. return 0;
  2606. }
  2607. int get_bdrv_id(void)
  2608. {
  2609. int driver_id = 0;
  2610. driver_id = *((int *)bdrv_message_buff);
  2611. return driver_id;
  2612. }
  2613. static irqreturn_t nt_bdrv_handler(void)
  2614. {
  2615. int bdrv_id = 0;
  2616. int cpu_id = raw_smp_processor_id();
  2617. bdrv_id = get_bdrv_id();
  2618. #if 0
  2619. if (bdrv_id == TEEI_VFS_NUM) {
  2620. teei_vfs_flag = 1;
  2621. }
  2622. #endif
  2623. teei_vfs_flag = 1;
  2624. add_bdrv_queue(bdrv_id);
  2625. up(&smc_lock);
  2626. return IRQ_HANDLED;
  2627. }
  2628. int register_bdrv_handler(void)
  2629. {
  2630. int retVal = 0;
  2631. #ifdef CONFIG_OF
  2632. int irq_num = 0;
  2633. struct device_node *node;
  2634. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2635. irq_num = irq_of_parse_and_map(node, 2);
  2636. retVal = request_irq(irq_num, nt_bdrv_handler, 0, "tz_drivers_service", NULL);
  2637. if (retVal)
  2638. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2639. else
  2640. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2641. #else
  2642. /* register 278 IRQ */
  2643. retVal = request_irq(BDRV_IRQ, nt_bdrv_handler, 0, "tz_drivers_service", NULL);
  2644. if (retVal)
  2645. printk("ERROR for request_irq %d error code : %d.\n", BDRV_IRQ, retVal);
  2646. else
  2647. printk("request irq [ %d ] OK.\n", BDRV_IRQ);
  2648. #endif
  2649. return 0;
  2650. }
  2651. static irqreturn_t nt_boot_irq_handler(void)
  2652. {
  2653. int cpu_id = raw_smp_processor_id();
  2654. if (boot_soter_flag == START_STATUS) {
  2655. printk("boot irq handler if\n");
  2656. boot_soter_flag = END_STATUS;
  2657. up(&smc_lock);
  2658. up(&(boot_sema));
  2659. return IRQ_HANDLED;
  2660. } else {
  2661. printk("boot irq hanler else\n");
  2662. if (forward_call_flag == GLSCH_NONE)
  2663. forward_call_flag = GLSCH_NEG;
  2664. else
  2665. forward_call_flag = GLSCH_NONE;
  2666. up(&smc_lock);
  2667. up(&(boot_sema));
  2668. return IRQ_HANDLED;
  2669. }
  2670. }
  2671. void init_tlog_entry(void)
  2672. {
  2673. int i = 0;
  2674. for (i = 0; i < TLOG_MAX_CNT; i++)
  2675. tlog_ent[i].valid = TLOG_UNUSE;
  2676. return;
  2677. }
  2678. int search_tlog_entry(void)
  2679. {
  2680. int i = 0;
  2681. for (i = 0; i < TLOG_MAX_CNT; i++) {
  2682. if (tlog_ent[i].valid == TLOG_UNUSE) {
  2683. tlog_ent[i].valid = TLOG_INUSE;
  2684. return i;
  2685. }
  2686. }
  2687. return -1;
  2688. }
  2689. void tlog_func(struct work_struct *entry)
  2690. {
  2691. struct tlog_struct *ts = container_of(entry, struct tlog_struct, work);
  2692. printk("TLOG %s", (char *)(ts->context));
  2693. ts->valid = TLOG_UNUSE;
  2694. return;
  2695. }
  2696. irqreturn_t tlog_handler(void)
  2697. {
  2698. int pos = 0;
  2699. printk("~tlog handler~\n");
  2700. pos = search_tlog_entry();
  2701. if (-1 != pos) {
  2702. memset(tlog_ent[pos].context, 0, TLOG_CONTEXT_LEN);
  2703. memcpy(tlog_ent[pos].context, (char *)tlog_message_buff, TLOG_CONTEXT_LEN);
  2704. Flush_Dcache_By_Area((unsigned long)tlog_message_buff, (unsigned long)tlog_message_buff + TLOG_CONTEXT_LEN);
  2705. INIT_WORK(&(tlog_ent[pos].work), tlog_func);
  2706. queue_work(secure_wq, &(tlog_ent[pos].work));
  2707. }
  2708. // irq_call_flag = GLSCH_HIGH;
  2709. // up(&smc_lock);
  2710. return IRQ_HANDLED;
  2711. }
  2712. int register_tlog_handler(void)
  2713. {
  2714. int retVal = 0;
  2715. #ifdef CONFIG_OF
  2716. int irq_num = 0;
  2717. struct device_node *node;
  2718. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2719. irq_num = irq_of_parse_and_map(node, 1);
  2720. retVal = request_irq(irq_num, tlog_handler, 0, "tz_drivers_service", NULL);
  2721. if (retVal)
  2722. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2723. else
  2724. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2725. #else
  2726. /* register 277 IRQ */
  2727. retVal = request_irq(TEEI_LOG_IRQ, tlog_handler, 0, "tz_drivers_service", NULL);
  2728. if (retVal)
  2729. printk("ERROR for request_irq %d error code : %d.\n", TEEI_LOG_IRQ, retVal);
  2730. else
  2731. printk("request irq [ %d ] OK.\n", TEEI_LOG_IRQ);
  2732. #endif
  2733. return 0;
  2734. }
  2735. int register_boot_irq_handler(void)
  2736. {
  2737. int retVal = 0;
  2738. #ifdef CONFIG_OF
  2739. int irq_num = 0;
  2740. struct device_node *node;
  2741. node = of_find_compatible_node(NULL, NULL, "microtrust,utos");
  2742. irq_num = irq_of_parse_and_map(node, 4);
  2743. retVal = request_irq(irq_num, nt_boot_irq_handler, 0, "tz_drivers_service", NULL);
  2744. if (retVal)
  2745. printk("[CONFIG_OF] [%s] ERROR for request_irq %d error code : %d.\n", __func__, irq_num, retVal);
  2746. else
  2747. printk("[CONFIG_OF] [%s] request irq [ %d ] OK.\n", __func__, irq_num);
  2748. #else
  2749. /* register 283 IRQ */
  2750. retVal = request_irq(BOOT_IRQ, nt_boot_irq_handler, 0, "tz_drivers_service", NULL);
  2751. if (retVal)
  2752. printk("ERROR for request_irq %d error code : %d.\n", BOOT_IRQ, retVal);
  2753. else
  2754. printk("request irq [ %d ] OK.\n", BOOT_IRQ);
  2755. #endif
  2756. return 0;
  2757. }
  2758. static void secondary_boot_stage2(void *info)
  2759. {
  2760. n_switch_to_t_os_stage2();
  2761. }
  2762. static void boot_stage2(void)
  2763. {
  2764. int cpu_id = 0;
  2765. get_online_cpus();
  2766. cpu_id = get_current_cpuid();
  2767. smp_call_function_single(cpu_id, secondary_boot_stage2, NULL, 1);
  2768. put_online_cpus();
  2769. }
  2770. int switch_to_t_os_stages2(void)
  2771. {
  2772. down(&(boot_sema));
  2773. down(&(smc_lock));
  2774. /* n_switch_to_t_os_stage2(); */
  2775. boot_stage2();
  2776. if (forward_call_flag == GLSCH_NONE)
  2777. forward_call_flag = GLSCH_LOW;
  2778. else if (forward_call_flag == GLSCH_NEG)
  2779. forward_call_flag = GLSCH_NONE;
  2780. else
  2781. return -1;
  2782. down(&(boot_sema));
  2783. up(&(boot_sema));
  2784. return 0;
  2785. }
  2786. static void secondary_load_tee(void *info)
  2787. {
  2788. n_invoke_t_load_tee(0, 0, 0);
  2789. }
  2790. static void load_tee(void)
  2791. {
  2792. int cpu_id = 0;
  2793. get_online_cpus();
  2794. cpu_id = get_current_cpuid();
  2795. smp_call_function_single(cpu_id, secondary_load_tee, NULL, 1);
  2796. put_online_cpus();
  2797. }
  2798. int t_os_load_image(void)
  2799. {
  2800. /* down the boot_sema. */
  2801. down(&(boot_sema));
  2802. /* N_INVOKE_T_LOAD_TEE to TOS */
  2803. set_sch_load_img_cmd();
  2804. down(&smc_lock);
  2805. /* n_invoke_t_load_tee(0, 0, 0); */
  2806. load_tee();
  2807. /* start HIGH level glschedule. */
  2808. if (forward_call_flag == GLSCH_NONE)
  2809. forward_call_flag = GLSCH_LOW;
  2810. else if (forward_call_flag == GLSCH_NEG)
  2811. forward_call_flag = GLSCH_NONE;
  2812. else
  2813. return -1;
  2814. /* block here until the TOS ack N_SWITCH_TO_T_OS_STAGE2 */
  2815. down(&(boot_sema));
  2816. up(&(boot_sema));
  2817. return 0;
  2818. }
  2819. /**
  2820. * @brief
  2821. */
  2822. static const struct file_operations teei_client_fops = {
  2823. .owner = THIS_MODULE,
  2824. .unlocked_ioctl = teei_client_ioctl,
  2825. .compat_ioctl = teei_client_ioctl,
  2826. .open = teei_client_open,
  2827. .mmap = teei_client_mmap,
  2828. .release = teei_client_release
  2829. };
  2830. #define TEEI_CONFIG_FULL_PATH_DEV_NAME "/dev/teei_config"
  2831. #define TEEI_CONFIG_DEV "teei_config"
  2832. #define TEEI_CONFIG_IOC_MAGIC 0x775B777E /* "TEEI Client" */
  2833. /**
  2834. * @brief Map the vma with the free pages
  2835. *
  2836. * @param filp
  2837. * @param vma
  2838. *
  2839. * @return 0: success
  2840. * EINVAL: Invalid parament
  2841. * ENOMEM: No enough memory
  2842. */
  2843. static int teei_config_mmap(struct file *filp, struct vm_area_struct *vma)
  2844. {
  2845. return 0;
  2846. }
  2847. static int init_teei_framework(void);
  2848. /**
  2849. * @brief
  2850. *
  2851. * @param file
  2852. * @param cmd
  2853. * @param arg
  2854. *
  2855. * @return
  2856. */
  2857. #define TEEI_CONFIG_IOCTL_INIT_TEEI _IOWR(TEEI_CONFIG_IOC_MAGIC, 3, int)
  2858. unsigned int teei_flags = 0;
  2859. static long teei_config_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  2860. {
  2861. int retVal = 0;
  2862. void *argp = (void __user *) arg;
  2863. switch (cmd) {
  2864. case TEEI_CONFIG_IOCTL_INIT_TEEI:
  2865. if (teei_flags == 1) {
  2866. break;
  2867. } else {
  2868. init_teei_framework();
  2869. teei_flags = 1;
  2870. }
  2871. break;
  2872. default:
  2873. printk("[%s][%d] command not found!\n", __func__, __LINE__);
  2874. retVal = -EINVAL;
  2875. }
  2876. return retVal;
  2877. }
  2878. int is_teei_ready(void)
  2879. {
  2880. return teei_flags;
  2881. }
  2882. EXPORT_SYMBOL(is_teei_ready);
  2883. /**
  2884. * @brief The open operation of /dev/teei_config device node.
  2885. *
  2886. * @param inode
  2887. * @param file
  2888. *
  2889. * @return ENOMEM: no enough memory in the linux kernel
  2890. * 0: on success
  2891. */
  2892. static int teei_config_open(struct inode *inode, struct file *file)
  2893. {
  2894. return 0;
  2895. }
  2896. /**
  2897. * @brief The release operation of /dev/teei_config device node.
  2898. *
  2899. * @param inode: device inode structure
  2900. * @param file: struct file
  2901. *
  2902. * @return 0: on success
  2903. */
  2904. static int teei_config_release(struct inode *inode, struct file *file)
  2905. {
  2906. return 0;
  2907. }
  2908. /**
  2909. * @brief
  2910. */
  2911. static const struct file_operations teei_config_fops = {
  2912. .owner = THIS_MODULE,
  2913. .unlocked_ioctl = teei_config_ioctl,
  2914. .open = teei_config_open,
  2915. .mmap = teei_config_mmap,
  2916. .release = teei_config_release
  2917. };
  2918. static void secondary_teei_invoke_drv(void)
  2919. {
  2920. n_invoke_t_drv(0, 0, 0);
  2921. return;
  2922. }
  2923. static void post_teei_invoke_drv(int cpu_id)
  2924. {
  2925. get_online_cpus();
  2926. smp_call_function_single(cpu_id,
  2927. secondary_teei_invoke_drv,
  2928. NULL,
  2929. 1);
  2930. put_online_cpus();
  2931. return;
  2932. }
  2933. static void teei_invoke_drv(void)
  2934. {
  2935. #if 0
  2936. int cpu_id = smp_processor_id();
  2937. /* int cpu_id = raw_smp_processor_id(); */
  2938. if (cpu_id != 0) {
  2939. /* call the mb function */
  2940. mb();
  2941. post_teei_invoke_drv(0); /* post it to primary */
  2942. } else {
  2943. /* printk("[%s][%d]\n", __func__, __LINE__); */
  2944. n_invoke_t_drv(0, 0, 0); /* called directly on primary core */
  2945. }
  2946. #else
  2947. int cpu_id = 0;
  2948. cpu_id = get_current_cpuid();
  2949. post_teei_invoke_drv(cpu_id);
  2950. #endif
  2951. return;
  2952. }
  2953. int __send_fp_command(unsigned long share_memory_size)
  2954. {
  2955. /* down(&smc_lock); */
  2956. /* down(&boot_sema); */
  2957. set_fp_command(share_memory_size);
  2958. Flush_Dcache_By_Area((unsigned long)fp_buff_addr, fp_buff_addr + FP_BUFF_SIZE);
  2959. /* Flush_Dcache_By_Area((unsigned long)vfs_flush_address, vfs_flush_address + VFS_SIZE); */
  2960. #if 0
  2961. teei_invoke_drv();
  2962. #else
  2963. fp_call_flag = GLSCH_HIGH;
  2964. n_invoke_t_drv(0, 0, 0);
  2965. #endif
  2966. /* down(&boot_sema); */
  2967. /* up(&boot_sema); */
  2968. return 0;
  2969. }
  2970. struct fp_command_struct {
  2971. unsigned long mem_size;
  2972. int retVal;
  2973. };
  2974. struct fp_command_struct fp_command_entry;
  2975. static void secondary_send_fp_command(void *info)
  2976. {
  2977. struct fp_command_struct *cd = (struct fp_command_struct *)info;
  2978. /* with a rmb() */
  2979. rmb();
  2980. cd->retVal = __send_fp_command(cd->mem_size);
  2981. /* with a wmb() */
  2982. wmb();
  2983. }
  2984. int send_fp_command(unsigned long share_memory_size)
  2985. {
  2986. int cpu_id = 0;
  2987. int retVal = 0;
  2988. struct fdrv_call_struct fdrv_ent;
  2989. down(&fp_lock);
  2990. mutex_lock(&pm_mutex);
  2991. if (teei_config_flag == 1) {
  2992. complete(&global_down_lock);
  2993. }
  2994. down(&smc_lock);
  2995. down(&boot_sema);
  2996. #if 0
  2997. fp_command_entry.mem_size = share_memory_size;
  2998. #else
  2999. fdrv_ent.fdrv_call_type = FP_SYS_NO;
  3000. fdrv_ent.fdrv_call_buff_size = share_memory_size;
  3001. #endif
  3002. /* with a wmb() */
  3003. wmb();
  3004. #if 0
  3005. get_online_cpus();
  3006. cpu_id = get_current_cpuid();
  3007. smp_call_function_single(cpu_id, secondary_send_fp_command, (void *)(&fp_command_entry), 1);
  3008. put_online_cpus();
  3009. #else
  3010. Flush_Dcache_By_Area((unsigned long)&fdrv_ent, (unsigned long)&fdrv_ent + sizeof(struct fdrv_call_struct));
  3011. retVal = add_work_entry(FDRV_CALL, (unsigned long)&fdrv_ent);
  3012. if (retVal != 0) {
  3013. mutex_unlock(&pm_mutex);
  3014. up(&fp_lock);
  3015. return retVal;
  3016. }
  3017. #endif
  3018. down(&boot_sema);
  3019. up(&boot_sema);
  3020. rmb();
  3021. mutex_unlock(&pm_mutex);
  3022. up(&fp_lock);
  3023. return fdrv_ent.retVal;
  3024. }
  3025. struct boot_stage1_struct {
  3026. unsigned long vir_addr;
  3027. };
  3028. struct boot_stage1_struct boot_stage1_entry;
  3029. static void secondary_boot_stage1(void *info)
  3030. {
  3031. struct boot_stage1_struct *cd = (struct boot_stage1_struct *)info;
  3032. /* with a rmb() */
  3033. rmb();
  3034. n_init_t_boot_stage1(cd->vir_addr, 0, 0);
  3035. /* with a wmb() */
  3036. wmb();
  3037. }
  3038. static void boot_stage1(unsigned long vir_address)
  3039. {
  3040. int cpu_id = 0;
  3041. boot_stage1_entry.vir_addr = vir_address;
  3042. /* with a wmb() */
  3043. wmb();
  3044. get_online_cpus();
  3045. cpu_id = get_current_cpuid();
  3046. printk("current cpu id [%d]\n", cpu_id);
  3047. smp_call_function_single(cpu_id, secondary_boot_stage1, (void *)(&boot_stage1_entry), 1);
  3048. put_online_cpus();
  3049. /* with a rmb() */
  3050. rmb();
  3051. }
  3052. /**
  3053. * @brief init TEEI Framework
  3054. * init Soter OS
  3055. * init Global Schedule
  3056. * init Forward Call Service
  3057. * init CallBack Service
  3058. * @return
  3059. */
  3060. static int init_teei_framework(void)
  3061. {
  3062. long retVal = 0;
  3063. int i = 0;
  3064. unsigned long secure_mem_p = 0;
  3065. boot_soter_flag = START_STATUS;
  3066. sema_init(&(boot_sema), 1);
  3067. sema_init(&(fp_lock), 1);
  3068. sema_init(&(api_lock), 1);
  3069. register_boot_irq_handler();
  3070. register_sched_irq_handler();
  3071. register_switch_irq_handler();
  3072. register_soter_irq_handler();
  3073. register_fp_ack_handler();
  3074. register_bdrv_handler();
  3075. register_tlog_handler();
  3076. register_error_irq_handler();
  3077. secure_wq = create_workqueue("Secure Call");
  3078. daulOS_VFS_write_share_mem = kmalloc(VDRV_MAX_SIZE, GFP_KERNEL);
  3079. if (daulOS_VFS_write_share_mem == NULL) {
  3080. printk("[%s][%d] kmalloc daulOS_VFS_write_share_mem failed!\n", __func__, __LINE__);
  3081. return -1;
  3082. }
  3083. daulOS_VFS_read_share_mem = kmalloc(VDRV_MAX_SIZE, GFP_KERNEL);
  3084. if (daulOS_VFS_read_share_mem == NULL) {
  3085. printk("[%s][%d] kmalloc daulOS_VFS_read_share_mem failed!\n", __func__, __LINE__);
  3086. return -1;
  3087. }
  3088. printk("[%s][%d]\n", __func__, __LINE__);
  3089. printk("[%s][%d] VFS_SIZE = %d, SZ_4K = %d\n", __func__, __LINE__, VFS_SIZE, SZ_4K);
  3090. printk("[%s][%d] ROUND_UP(VFS_SIZE, SZ_4K) = %d\n", __func__, __LINE__, ROUND_UP(VFS_SIZE, SZ_4K));
  3091. printk("[%s][%d] get_order(ROUND_UP(VFS_SIZE, SZ_4K)) = %d\n", __func__, __LINE__, get_order(ROUND_UP(VFS_SIZE, SZ_4K)));
  3092. boot_vfs_addr = (unsigned long) __get_free_pages(GFP_KERNEL | GFP_DMA , get_order(ROUND_UP(VFS_SIZE, SZ_4K)));
  3093. printk("[%s][%d]\n", __func__, __LINE__);
  3094. if (boot_vfs_addr == NULL) {
  3095. printk("[%s][%d]ERROR: There is no enough memory for booting Soter!\n", __func__, __LINE__);
  3096. return -1;
  3097. }
  3098. printk("[%s][%d]\n", __func__, __LINE__);
  3099. down(&(boot_sema));
  3100. down(&(smc_lock));
  3101. printk("[%s][%d]\n", __func__, __LINE__);
  3102. /* n_init_t_boot_stage1((unsigned long)virt_to_phys(boot_vfs_addr), 0, 0); */
  3103. boot_stage1((unsigned long)virt_to_phys(boot_vfs_addr));
  3104. down(&(boot_sema));
  3105. up(&(boot_sema));
  3106. free_pages(boot_vfs_addr, get_order(ROUND_UP(VFS_SIZE, SZ_4K)));
  3107. boot_soter_flag = END_STATUS;
  3108. if (soter_error_flag == 1) {
  3109. return -1;
  3110. }
  3111. /**printk("[%s][%d] begin to load Soter services.\n", __func__, __LINE__);
  3112. **switch_to_t_os_stages2();
  3113. **printk("[%s][%d] load Soter services successfully.\n", __func__, __LINE__);*/
  3114. printk("[%s][%d] begin to create the command buffer!\n", __func__, __LINE__);
  3115. retVal = create_cmd_buff();
  3116. if (retVal < 0) {
  3117. printk("[%s][%d] create_cmd_buff failed !\n", __func__, __LINE__);
  3118. return retVal;
  3119. }
  3120. printk("[%s][%d] end of creating the command buffer!\n", __func__, __LINE__);
  3121. printk("[%s][%d] begin to load Soter services.\n", __func__, __LINE__);
  3122. switch_to_t_os_stages2();
  3123. printk("[%s][%d] load Soter services successfully.\n", __func__, __LINE__);
  3124. if (soter_error_flag == 1) {
  3125. return -1;
  3126. }
  3127. init_smc_work();
  3128. printk("[%s][%d] begin to init daulOS services.\n", __func__, __LINE__);
  3129. retVal = teei_service_init();
  3130. if (retVal == -1)
  3131. return -1;
  3132. printk("[%s][%d] init daulOS services successfully.\n", __func__, __LINE__);
  3133. printk("[%s][%d] begin to load TEEs.\n", __func__, __LINE__);
  3134. t_os_load_image();
  3135. if (soter_error_flag == 1)
  3136. return -1;
  3137. printk("[%s][%d] load TEEs successfully.\n", __func__, __LINE__);
  3138. teei_config_flag = 1;
  3139. return 0;
  3140. }
  3141. static dev_t teei_config_device_no;
  3142. static struct cdev teei_config_cdev;
  3143. static struct class *config_driver_class;
  3144. /**
  3145. * @brief TEEI Agent Driver initialization
  3146. * initialize Microtrust Tee environment
  3147. * @return
  3148. **/
  3149. static int teei_config_init(void)
  3150. {
  3151. int ret_code = 0;
  3152. long retVal = 0;
  3153. struct device *class_dev = NULL;
  3154. ret_code = alloc_chrdev_region(&teei_config_device_no, 0, 1, TEEI_CONFIG_DEV);
  3155. if (ret_code < 0) {
  3156. printk("alloc_chrdev_region failed %x.\n", ret_code);
  3157. return ret_code;
  3158. }
  3159. config_driver_class = class_create(THIS_MODULE, TEEI_CONFIG_DEV);
  3160. if (IS_ERR(config_driver_class)) {
  3161. ret_code = -ENOMEM;
  3162. printk("class_create failed %x\n", ret_code);
  3163. goto unregister_chrdev_region;
  3164. }
  3165. class_dev = device_create(config_driver_class, NULL, teei_config_device_no, NULL, TEEI_CONFIG_DEV);
  3166. if (NULL == class_dev) {
  3167. printk("class_device_create failed %x\n", ret_code);
  3168. ret_code = -ENOMEM;
  3169. goto class_destroy;
  3170. }
  3171. cdev_init(&teei_config_cdev, &teei_config_fops);
  3172. teei_config_cdev.owner = THIS_MODULE;
  3173. ret_code = cdev_add(&teei_config_cdev, MKDEV(MAJOR(teei_config_device_no), 0), 1);
  3174. if (ret_code < 0) {
  3175. printk("cdev_add failed %x\n", ret_code);
  3176. goto class_device_destroy;
  3177. }
  3178. goto return_fn;
  3179. class_device_destroy:
  3180. device_destroy(driver_class, teei_config_device_no);
  3181. class_destroy:
  3182. class_destroy(driver_class);
  3183. unregister_chrdev_region:
  3184. unregister_chrdev_region(teei_config_device_no, 1);
  3185. return_fn:
  3186. return ret_code;
  3187. }
  3188. static int teei_cpu_id[] = {0x0000, 0x0001, 0x0002, 0x0003, 0x0100, 0x0101, 0x0102, 0x0103};
  3189. static int __cpuinit tz_driver_cpu_callback(struct notifier_block *self,
  3190. unsigned long action, void *hcpu)
  3191. {
  3192. unsigned int cpu = (unsigned long)hcpu;
  3193. /*unsigned int sched_cpu = 4;*/
  3194. unsigned int sched_cpu = get_current_cpuid();
  3195. struct cpumask mtee_mask = { CPU_BITS_NONE };
  3196. int retVal = 0;
  3197. int i;
  3198. switch (action) {
  3199. case CPU_DOWN_PREPARE:
  3200. case CPU_DOWN_PREPARE_FROZEN:
  3201. if (cpu == sched_cpu) {
  3202. printk("cpu down prepare ************************\n");
  3203. retVal = down_trylock(&smc_lock);
  3204. if (retVal == 1) {
  3205. return NOTIFY_BAD;
  3206. }
  3207. else {
  3208. cpu_notify_flag = 1;
  3209. for_each_online_cpu(i) {
  3210. printk("current on line cpu [%d]\n", i);
  3211. if (i == cpu) {
  3212. continue;
  3213. }
  3214. current_cpu_id = i;
  3215. }
  3216. #if 1
  3217. printk("[%s][%d]brefore cpumask set cpu\n", __func__, __LINE__);
  3218. cpumask_set_cpu(current_cpu_id, &mtee_mask);
  3219. /*cpumask_set_cpu(current_cpu_id, &mask);*/
  3220. printk("[%s][%d]after cpumask set cpu\n", __func__, __LINE__);
  3221. #if 0
  3222. if (sched_setaffinity(sub_pid, &mtee_mask) == -1)
  3223. printk("warning: could not set CPU affinity, continuing...\n");
  3224. #endif
  3225. set_cpus_allowed(teei_switch_task, mtee_mask);
  3226. #endif
  3227. /* TODO smc_call to notify ATF to switch the CPU*/
  3228. /* NT_switch_T(current_cpu_id);*/
  3229. printk("current cpu id \n");
  3230. nt_sched_core(teei_cpu_id[current_cpu_id], teei_cpu_id[cpu], 0);
  3231. printk("change cpu id = [%d]\n", current_cpu_id);
  3232. }
  3233. }
  3234. break;
  3235. case CPU_DOWN_FAILED:
  3236. if (cpu_notify_flag == 1) {
  3237. printk("cpu down failed *************************\n");
  3238. up(&smc_lock);
  3239. cpu_notify_flag = 0;
  3240. }
  3241. break;
  3242. case CPU_DEAD:
  3243. case CPU_DEAD_FROZEN:
  3244. if (cpu_notify_flag == 1) {
  3245. printk("cpu down success ***********************\n");
  3246. up(&smc_lock);
  3247. cpu_notify_flag = 0;
  3248. }
  3249. break;
  3250. }
  3251. return NOTIFY_OK;
  3252. }
  3253. /**
  3254. * @brief TEEI Agent Driver initialization
  3255. * initialize service framework
  3256. * @return
  3257. */
  3258. static int teei_client_init(void)
  3259. {
  3260. int ret_code = 0;
  3261. long retVal = 0;
  3262. struct device *class_dev = NULL;
  3263. long prior = 0;
  3264. unsigned long irq_status = 0;
  3265. /* printk("TEEI Agent Driver Module Init ...\n"); */
  3266. printk("=============================================================\n\n");
  3267. printk("~~~~~~~uTos version [%s]~~~~~~~\n",UTOS_VERSION);
  3268. printk("=============================================================\n\n");
  3269. ret_code = alloc_chrdev_region(&teei_client_device_no, 0, 1, TEEI_CLIENT_DEV);
  3270. if (ret_code < 0) {
  3271. printk("alloc_chrdev_region failed %x\n", ret_code);
  3272. return ret_code;
  3273. }
  3274. driver_class = class_create(THIS_MODULE, TEEI_CLIENT_DEV);
  3275. if (IS_ERR(driver_class)) {
  3276. ret_code = -ENOMEM;
  3277. printk("class_create failed %x\n", ret_code);
  3278. goto unregister_chrdev_region;
  3279. }
  3280. class_dev = device_create(driver_class, NULL, teei_client_device_no, NULL, TEEI_CLIENT_DEV);
  3281. if (NULL == class_dev) {
  3282. printk("class_device_create failed %x\n", ret_code);
  3283. ret_code = -ENOMEM;
  3284. goto class_destroy;
  3285. }
  3286. cdev_init(&teei_client_cdev, &teei_client_fops);
  3287. teei_client_cdev.owner = THIS_MODULE;
  3288. ret_code = cdev_add(&teei_client_cdev, MKDEV(MAJOR(teei_client_device_no), 0), 1);
  3289. if (ret_code < 0) {
  3290. printk("cdev_add failed %x\n", ret_code);
  3291. goto class_device_destroy;
  3292. }
  3293. memset(&teei_contexts_head, 0, sizeof(teei_contexts_head));
  3294. teei_contexts_head.dev_file_cnt = 0;
  3295. init_rwsem(&teei_contexts_head.teei_contexts_sem);
  3296. INIT_LIST_HEAD(&teei_contexts_head.context_list);
  3297. init_tlog_entry();
  3298. sema_init(&(smc_lock), 1);
  3299. //sema_init(&(global_down_lock), 0);
  3300. int i;
  3301. for_each_online_cpu(i) {
  3302. current_cpu_id = i;
  3303. printk("init stage : current_cpu_id = %d\n", current_cpu_id);
  3304. }
  3305. printk("begin to create sub_thread.\n");
  3306. #if 0
  3307. sub_pid = kernel_thread(global_fn, NULL, CLONE_KERNEL);
  3308. retVal = sys_setpriority(PRIO_PROCESS, sub_pid, -3);
  3309. #else
  3310. //struct sched_param param = {.sched_priority = -20 };
  3311. teei_fastcall_task = kthread_create(global_fn, NULL, "teei_fastcall_thread");
  3312. if (IS_ERR(teei_fastcall_task)) {
  3313. printk("create fastcall thread failed: %d\n", PTR_ERR(teei_fastcall_task));
  3314. goto fastcall_thread_fail;
  3315. }
  3316. //sched_setscheduler_nocheck(teei_fastcall_task, SCHED_NORMAL, &param);
  3317. //get_task_struct(teei_fastcall_task);
  3318. wake_up_process(teei_fastcall_task);
  3319. #endif
  3320. printk("create the sub_thread successfully!\n");
  3321. #if 0
  3322. cpumask_set_cpu(get_current_cpuid(), &mask);
  3323. retVal = sched_setaffinity(sub_pid, &mask);
  3324. if (retVal != 0)
  3325. printk("warning: could not set CPU affinity, retVal = [%l] continuing...\n", retVal);
  3326. #endif
  3327. /* create the switch thread */
  3328. teei_switch_task = kthread_create(kthread_worker_fn, &ut_fastcall_worker, "teei_switch_thread");
  3329. if (IS_ERR(teei_switch_task)) {
  3330. printk("create switch thread failed: %ld\n", PTR_ERR(teei_switch_task));
  3331. teei_switch_task = NULL;
  3332. goto fastcall_thread_fail;
  3333. }
  3334. //sched_setscheduler_nocheck(teei_switch_task, SCHED_NORMAL, &param);
  3335. //get_task_struct(teei_switch_task);
  3336. wake_up_process(teei_switch_task);
  3337. cpumask_set_cpu(get_current_cpuid(), &mask);
  3338. set_cpus_allowed(teei_switch_task, mask);
  3339. register_cpu_notifier(&tz_driver_cpu_notifer);
  3340. printk("after register cpu notify\n");
  3341. teei_config_init();
  3342. goto return_fn;
  3343. fastcall_thread_fail:
  3344. class_device_destroy:
  3345. device_destroy(driver_class, teei_client_device_no);
  3346. class_destroy:
  3347. class_destroy(driver_class);
  3348. unregister_chrdev_region:
  3349. unregister_chrdev_region(teei_client_device_no, 1);
  3350. return_fn:
  3351. return ret_code;
  3352. }
  3353. /**
  3354. * @brief
  3355. */
  3356. static void teei_client_exit(void)
  3357. {
  3358. TINFO("teei_client exit");
  3359. device_destroy(driver_class, teei_client_device_no);
  3360. class_destroy(driver_class);
  3361. unregister_chrdev_region(teei_client_device_no, 1);
  3362. }
  3363. MODULE_LICENSE("GPL v2");
  3364. MODULE_AUTHOR("TEEI <www.microtrust.com>");
  3365. MODULE_DESCRIPTION("TEEI Agent");
  3366. MODULE_VERSION("1.00");
  3367. module_init(teei_client_init);
  3368. module_exit(teei_client_exit);