cam_smmu_api.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/dma-buf.h>
  7. #include <linux/dma-direction.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/iommu.h>
  10. #include <linux/slab.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/of_address.h>
  13. #include <linux/msm_dma_iommu_mapping.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/genalloc.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/dma-iommu.h>
  18. #include <soc/qcom/secure_buffer.h>
  19. #include <media/cam_req_mgr.h>
  20. #include "cam_compat.h"
  21. #include "cam_smmu_api.h"
  22. #include "cam_debug_util.h"
  23. #include "camera_main.h"
  24. #include "cam_trace.h"
  25. #include "cam_common_util.h"
  26. #include "cam_compat.h"
  27. #define SHARED_MEM_POOL_GRANULARITY 16
  28. #define IOMMU_INVALID_DIR -1
  29. #define BYTE_SIZE 8
  30. #define COOKIE_NUM_BYTE 2
  31. #define COOKIE_SIZE (BYTE_SIZE*COOKIE_NUM_BYTE)
  32. #define COOKIE_MASK ((1<<COOKIE_SIZE)-1)
  33. #define HANDLE_INIT (-1)
  34. #define CAM_SMMU_CB_MAX 6
  35. #define CAM_SMMU_SHARED_HDL_MAX 6
  36. #define GET_SMMU_HDL(x, y) (((x) << COOKIE_SIZE) | ((y) & COOKIE_MASK))
  37. #define GET_SMMU_TABLE_IDX(x) (((x) >> COOKIE_SIZE) & COOKIE_MASK)
  38. #define CAM_SMMU_MONITOR_MAX_ENTRIES 100
  39. #define CAM_SMMU_INC_MONITOR_HEAD(head, ret) \
  40. div_u64_rem(atomic64_add_return(1, head),\
  41. CAM_SMMU_MONITOR_MAX_ENTRIES, (ret))
  42. static int g_num_pf_handled = 1;
  43. module_param(g_num_pf_handled, int, 0644);
  44. struct cam_fw_alloc_info icp_fw;
  45. struct cam_smmu_work_payload {
  46. int idx;
  47. struct iommu_domain *domain;
  48. struct device *dev;
  49. unsigned long iova;
  50. int flags;
  51. void *token;
  52. struct list_head list;
  53. };
  54. enum cam_io_coherency_mode {
  55. CAM_SMMU_NO_COHERENCY,
  56. CAM_SMMU_DMA_COHERENT,
  57. CAM_SMMU_DMA_COHERENT_HINT_CACHED,
  58. };
  59. enum cam_protection_type {
  60. CAM_PROT_INVALID,
  61. CAM_NON_SECURE,
  62. CAM_SECURE,
  63. CAM_PROT_MAX,
  64. };
  65. enum cam_iommu_type {
  66. CAM_SMMU_INVALID,
  67. CAM_QSMMU,
  68. CAM_ARM_SMMU,
  69. CAM_SMMU_MAX,
  70. };
  71. enum cam_smmu_buf_state {
  72. CAM_SMMU_BUFF_EXIST,
  73. CAM_SMMU_BUFF_NOT_EXIST,
  74. };
  75. enum cam_smmu_init_dir {
  76. CAM_SMMU_TABLE_INIT,
  77. CAM_SMMU_TABLE_DEINIT,
  78. };
  79. struct scratch_mapping {
  80. void *bitmap;
  81. size_t bits;
  82. unsigned int order;
  83. dma_addr_t base;
  84. };
  85. struct region_buf_info {
  86. struct dma_buf *buf;
  87. struct dma_buf_attachment *attach;
  88. struct sg_table *table;
  89. };
  90. struct cam_smmu_monitor {
  91. struct timespec64 timestamp;
  92. bool is_map;
  93. /* map-unmap info */
  94. int ion_fd;
  95. unsigned long i_ino;
  96. dma_addr_t paddr;
  97. size_t len;
  98. enum cam_smmu_region_id region_id;
  99. };
  100. struct cam_context_bank_info {
  101. struct device *dev;
  102. struct iommu_domain *domain;
  103. dma_addr_t va_start;
  104. size_t va_len;
  105. const char *name[CAM_SMMU_SHARED_HDL_MAX];
  106. bool is_secure;
  107. uint8_t scratch_buf_support;
  108. uint8_t firmware_support;
  109. uint8_t shared_support;
  110. uint8_t io_support;
  111. uint8_t secheap_support;
  112. uint8_t fwuncached_region_support;
  113. uint8_t qdss_support;
  114. dma_addr_t qdss_phy_addr;
  115. bool is_fw_allocated;
  116. bool is_secheap_allocated;
  117. bool is_fwuncached_buf_allocated;
  118. bool is_qdss_allocated;
  119. struct scratch_mapping scratch_map;
  120. struct gen_pool *shared_mem_pool;
  121. struct cam_smmu_region_info scratch_info;
  122. struct cam_smmu_region_info firmware_info;
  123. struct cam_smmu_region_info shared_info;
  124. struct cam_smmu_region_info io_info;
  125. struct cam_smmu_region_info secheap_info;
  126. struct cam_smmu_region_info fwuncached_region;
  127. struct cam_smmu_region_info qdss_info;
  128. struct region_buf_info secheap_buf;
  129. struct region_buf_info fwuncached_reg_buf;
  130. struct list_head smmu_buf_list;
  131. struct list_head smmu_buf_kernel_list;
  132. struct mutex lock;
  133. int handle;
  134. enum cam_smmu_ops_param state;
  135. void (*handler[CAM_SMMU_CB_MAX]) (struct cam_smmu_pf_info *pf_info);
  136. void *token[CAM_SMMU_CB_MAX];
  137. int cb_count;
  138. int secure_count;
  139. int pf_count;
  140. size_t io_mapping_size;
  141. size_t shared_mapping_size;
  142. bool is_mul_client;
  143. int device_count;
  144. int num_shared_hdl;
  145. enum cam_io_coherency_mode coherency_mode;
  146. /* discard iova - non-zero values are valid */
  147. dma_addr_t discard_iova_start;
  148. size_t discard_iova_len;
  149. atomic64_t monitor_head;
  150. struct cam_smmu_monitor monitor_entries[CAM_SMMU_MONITOR_MAX_ENTRIES];
  151. };
  152. struct cam_iommu_cb_set {
  153. struct cam_context_bank_info *cb_info;
  154. u32 cb_num;
  155. u32 cb_init_count;
  156. struct work_struct smmu_work;
  157. struct mutex payload_list_lock;
  158. struct list_head payload_list;
  159. u32 non_fatal_fault;
  160. struct dentry *dentry;
  161. bool cb_dump_enable;
  162. bool map_profile_enable;
  163. bool force_cache_allocs;
  164. bool need_shared_buffer_padding;
  165. bool is_expanded_memory;
  166. };
  167. static const struct of_device_id msm_cam_smmu_dt_match[] = {
  168. { .compatible = "qcom,msm-cam-smmu", },
  169. { .compatible = "qcom,msm-cam-smmu-cb", },
  170. { .compatible = "qcom,msm-cam-smmu-fw-dev", },
  171. {}
  172. };
  173. struct cam_dma_buff_info {
  174. struct dma_buf *buf;
  175. struct dma_buf_attachment *attach;
  176. struct sg_table *table;
  177. enum dma_data_direction dir;
  178. enum cam_smmu_region_id region_id;
  179. int iommu_dir;
  180. int ref_count;
  181. dma_addr_t paddr;
  182. struct list_head list;
  183. int ion_fd;
  184. unsigned long i_ino;
  185. size_t len;
  186. size_t phys_len;
  187. bool is_internal;
  188. struct timespec64 ts;
  189. };
  190. struct cam_sec_buff_info {
  191. struct dma_buf *buf;
  192. struct dma_buf_attachment *attach;
  193. struct sg_table *table;
  194. enum dma_data_direction dir;
  195. int ref_count;
  196. dma_addr_t paddr;
  197. struct list_head list;
  198. int ion_fd;
  199. unsigned long i_ino;
  200. size_t len;
  201. };
  202. struct cam_smmu_mini_dump_cb_info {
  203. struct cam_smmu_monitor mapping[CAM_SMMU_MONITOR_MAX_ENTRIES];
  204. struct cam_smmu_region_info scratch_info;
  205. struct cam_smmu_region_info firmware_info;
  206. struct cam_smmu_region_info shared_info;
  207. struct cam_smmu_region_info io_info;
  208. struct cam_smmu_region_info secheap_info;
  209. struct cam_smmu_region_info fwuncached_region;
  210. struct cam_smmu_region_info qdss_info;
  211. struct region_buf_info secheap_buf;
  212. struct region_buf_info fwuncached_reg_buf;
  213. char name[CAM_SMMU_SHARED_HDL_MAX][16];
  214. size_t va_len;
  215. size_t io_mapping_size;
  216. size_t shared_mapping_size;
  217. size_t discard_iova_len;
  218. int handle;
  219. int device_count;
  220. int num_shared_hdl;
  221. int cb_count;
  222. int secure_count;
  223. int pf_count;
  224. dma_addr_t va_start;
  225. dma_addr_t discard_iova_start;
  226. dma_addr_t qdss_phy_addr;
  227. enum cam_io_coherency_mode coherency_mode;
  228. enum cam_smmu_ops_param state;
  229. uint8_t scratch_buf_support;
  230. uint8_t firmware_support;
  231. uint8_t shared_support;
  232. uint8_t io_support;
  233. uint8_t secheap_support;
  234. uint8_t fwuncached_region_support;
  235. uint8_t qdss_support;
  236. bool is_mul_client;
  237. bool is_secure;
  238. bool is_fw_allocated;
  239. bool is_secheap_allocated;
  240. bool is_fwuncached_buf_allocated;
  241. bool is_qdss_allocated;
  242. };
  243. struct cam_smmu_mini_dump_info {
  244. uint32_t cb_num;
  245. struct cam_smmu_mini_dump_cb_info *cb;
  246. };
  247. static const char *qdss_region_name = "qdss";
  248. static struct cam_iommu_cb_set iommu_cb_set;
  249. static enum dma_data_direction cam_smmu_translate_dir(
  250. enum cam_smmu_map_dir dir);
  251. static bool cam_smmu_is_hdl_nonunique_or_null(int hdl);
  252. static int cam_smmu_create_iommu_handle(int idx);
  253. static int cam_smmu_create_add_handle_in_table(char *name,
  254. int *hdl);
  255. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_ion_index(int idx,
  256. int ion_fd, struct dma_buf *dma_buf);
  257. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_dma_buf(int idx,
  258. struct dma_buf *buf);
  259. static struct cam_sec_buff_info *cam_smmu_find_mapping_by_sec_buf_idx(int idx,
  260. int ion_fd, struct dma_buf *dma_buf);
  261. static int cam_smmu_init_scratch_map(struct scratch_mapping *scratch_map,
  262. dma_addr_t base, size_t size,
  263. int order);
  264. static int cam_smmu_alloc_scratch_va(struct scratch_mapping *mapping,
  265. size_t size,
  266. dma_addr_t *iova);
  267. static int cam_smmu_free_scratch_va(struct scratch_mapping *mapping,
  268. dma_addr_t addr, size_t size);
  269. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_virt_address(int idx,
  270. dma_addr_t virt_addr);
  271. static int cam_smmu_map_buffer_and_add_to_list(int idx, int ion_fd,
  272. bool dis_delayed_unmap, enum dma_data_direction dma_dir,
  273. dma_addr_t *paddr_ptr, size_t *len_ptr,
  274. enum cam_smmu_region_id region_id, bool is_internal);
  275. static int cam_smmu_map_kernel_buffer_and_add_to_list(int idx,
  276. struct dma_buf *buf, enum dma_data_direction dma_dir,
  277. dma_addr_t *paddr_ptr, size_t *len_ptr,
  278. enum cam_smmu_region_id region_id);
  279. static int cam_smmu_alloc_scratch_buffer_add_to_list(int idx,
  280. size_t virt_len,
  281. size_t phys_len,
  282. unsigned int iommu_dir,
  283. dma_addr_t *virt_addr);
  284. static int cam_smmu_unmap_buf_and_remove_from_list(
  285. struct cam_dma_buff_info *mapping_info, int idx);
  286. static int cam_smmu_free_scratch_buffer_remove_from_list(
  287. struct cam_dma_buff_info *mapping_info,
  288. int idx);
  289. static void cam_smmu_clean_user_buffer_list(int idx);
  290. static void cam_smmu_clean_kernel_buffer_list(int idx);
  291. static void cam_smmu_dump_cb_info(int idx);
  292. static void cam_smmu_print_user_list(int idx);
  293. static void cam_smmu_print_kernel_list(int idx);
  294. static void cam_smmu_print_table(void);
  295. static int cam_smmu_probe(struct platform_device *pdev);
  296. static uint32_t cam_smmu_find_closest_mapping(int idx, void *vaddr);
  297. static void cam_smmu_update_monitor_array(
  298. struct cam_context_bank_info *cb_info,
  299. bool is_map,
  300. struct cam_dma_buff_info *mapping_info)
  301. {
  302. int iterator;
  303. CAM_SMMU_INC_MONITOR_HEAD(&cb_info->monitor_head, &iterator);
  304. CAM_GET_TIMESTAMP(cb_info->monitor_entries[iterator].timestamp);
  305. cb_info->monitor_entries[iterator].is_map = is_map;
  306. cb_info->monitor_entries[iterator].ion_fd = mapping_info->ion_fd;
  307. cb_info->monitor_entries[iterator].i_ino = mapping_info->i_ino;
  308. cb_info->monitor_entries[iterator].paddr = mapping_info->paddr;
  309. cb_info->monitor_entries[iterator].len = mapping_info->len;
  310. cb_info->monitor_entries[iterator].region_id = mapping_info->region_id;
  311. }
  312. static void cam_smmu_dump_monitor_array(
  313. struct cam_context_bank_info *cb_info)
  314. {
  315. int i = 0;
  316. int64_t state_head = 0;
  317. uint32_t index, num_entries, oldest_entry;
  318. uint64_t ms, hrs, min, sec;
  319. state_head = atomic64_read(&cb_info->monitor_head);
  320. if (state_head == -1) {
  321. return;
  322. } else if (state_head < CAM_SMMU_MONITOR_MAX_ENTRIES) {
  323. num_entries = state_head;
  324. oldest_entry = 0;
  325. } else {
  326. num_entries = CAM_SMMU_MONITOR_MAX_ENTRIES;
  327. div_u64_rem(state_head + 1,
  328. CAM_SMMU_MONITOR_MAX_ENTRIES, &oldest_entry);
  329. }
  330. CAM_INFO(CAM_SMMU,
  331. "========Dumping monitor information for cb %s===========",
  332. cb_info->name[0]);
  333. index = oldest_entry;
  334. for (i = 0; i < num_entries; i++) {
  335. CAM_CONVERT_TIMESTAMP_FORMAT(cb_info->monitor_entries[index].timestamp,
  336. hrs, min, sec, ms);
  337. CAM_INFO(CAM_SMMU,
  338. "**** %llu:%llu:%llu.%llu : Index[%d] [%s] : ion_fd=%d i_ino=%lu start=0x%llx end=0x%llx len=%zu region=%d",
  339. hrs, min, sec, ms,
  340. index,
  341. cb_info->monitor_entries[index].is_map ? "MAP" : "UNMAP",
  342. cb_info->monitor_entries[index].ion_fd,
  343. cb_info->monitor_entries[index].i_ino,
  344. cb_info->monitor_entries[index].paddr,
  345. cb_info->monitor_entries[index].paddr +
  346. cb_info->monitor_entries[index].len,
  347. cb_info->monitor_entries[index].len,
  348. cb_info->monitor_entries[index].region_id);
  349. index = (index + 1) % CAM_SMMU_MONITOR_MAX_ENTRIES;
  350. }
  351. }
  352. bool cam_smmu_need_shared_buffer_padding(void)
  353. {
  354. return iommu_cb_set.need_shared_buffer_padding;
  355. }
  356. bool cam_smmu_is_expanded_memory(void)
  357. {
  358. return iommu_cb_set.is_expanded_memory;
  359. }
  360. int cam_smmu_need_force_alloc_cached(bool *force_alloc_cached)
  361. {
  362. int idx;
  363. uint32_t curr_mode = 0, final_mode = 0;
  364. bool validate = false;
  365. if (!force_alloc_cached) {
  366. CAM_ERR(CAM_SMMU, "Invalid arg");
  367. return -EINVAL;
  368. }
  369. CAM_INFO(CAM_SMMU, "force_cache_allocs=%d",
  370. iommu_cb_set.force_cache_allocs);
  371. /*
  372. * First validate whether all SMMU CBs are properly setup to comply with
  373. * iommu_cb_set.force_alloc_cached flag.
  374. * This helps as a validation check to make sure a valid DT combination
  375. * is set for a given chipset.
  376. */
  377. for (idx = 0; idx < iommu_cb_set.cb_num; idx++) {
  378. /* ignore secure cb for now. need to revisit */
  379. if (iommu_cb_set.cb_info[idx].is_secure)
  380. continue;
  381. curr_mode = iommu_cb_set.cb_info[idx].coherency_mode;
  382. /*
  383. * 1. No coherency:
  384. * We can map both CACHED and UNCACHED buffers into same CB.
  385. * We need to allocate UNCACHED buffers for Cmdbuffers
  386. * and Shared Buffers. UNCAHE support must exists with memory
  387. * allocators (ion or dma-buf-heaps) for CmdBuffers,
  388. * SharedBuffers to work - as it is difficult to do
  389. * cache operations on these buffers in camera design.
  390. * ImageBuffers can be CACHED or UNCACHED. If CACHED, clients
  391. * need to make required CACHE operations.
  392. * Cannot force all allocations to CACHE.
  393. * 2. dma-coherent:
  394. * We cannot map CACHED and UNCACHED buffers into the same CB
  395. * This means, we must force allocate all buffers to be
  396. * CACHED.
  397. * 3. dma-coherent-hint-cached
  398. * We can map both CACHED and UNCACHED buffers into the same
  399. * CB. So any option is fine force_cache_allocs.
  400. * Forcing to cache is preferable though.
  401. *
  402. * Other rule we are enforcing is - all camera CBs (except
  403. * secure CB) must have same coherency mode set. Assume one CB
  404. * is having no_coherency mode and other CB is having
  405. * dma_coherent. For no_coherency CB to work - we must not force
  406. * buffers to be CACHE (exa cmd buffers), for dma_coherent mode
  407. * we must force all buffers to be CACHED. But at the time of
  408. * allocation, we dont know to which CB we will be mapping this
  409. * buffer. So it becomes difficult to generalize cache
  410. * allocations and io coherency mode that we want to support.
  411. * So, to simplify, all camera CBs will have same mode.
  412. */
  413. CAM_DBG(CAM_SMMU, "[%s] : curr_mode=%d",
  414. iommu_cb_set.cb_info[idx].name[0], curr_mode);
  415. if (curr_mode == CAM_SMMU_NO_COHERENCY) {
  416. if (iommu_cb_set.force_cache_allocs) {
  417. CAM_ERR(CAM_SMMU,
  418. "[%s] Can't force alloc cache with no coherency",
  419. iommu_cb_set.cb_info[idx].name[0]);
  420. return -EINVAL;
  421. }
  422. } else if (curr_mode == CAM_SMMU_DMA_COHERENT) {
  423. if (!iommu_cb_set.force_cache_allocs) {
  424. CAM_ERR(CAM_SMMU,
  425. "[%s] Must force cache allocs for dma coherent device",
  426. iommu_cb_set.cb_info[idx].name[0]);
  427. return -EINVAL;
  428. }
  429. }
  430. if (validate) {
  431. if (curr_mode != final_mode) {
  432. CAM_ERR(CAM_SMMU,
  433. "[%s] CBs having different coherency modes final=%d, curr=%d",
  434. iommu_cb_set.cb_info[idx].name[0],
  435. final_mode, curr_mode);
  436. return -EINVAL;
  437. }
  438. } else {
  439. validate = true;
  440. final_mode = curr_mode;
  441. }
  442. }
  443. /*
  444. * To be more accurate - if this flag is TRUE and if this buffer will
  445. * be mapped to external devices like CVP - we need to ensure we do
  446. * one of below :
  447. * 1. CVP CB having dma-coherent or dma-coherent-hint-cached
  448. * 2. camera/cvp sw layers properly doing required cache operations. We
  449. * cannot anymore assume these buffers (camera <--> cvp) are uncached
  450. */
  451. *force_alloc_cached = iommu_cb_set.force_cache_allocs;
  452. return 0;
  453. }
  454. static void cam_smmu_page_fault_work(struct work_struct *work)
  455. {
  456. int j;
  457. int idx;
  458. struct cam_smmu_work_payload *payload;
  459. uint32_t buf_info;
  460. struct cam_smmu_pf_info pf_info;
  461. mutex_lock(&iommu_cb_set.payload_list_lock);
  462. if (list_empty(&iommu_cb_set.payload_list)) {
  463. CAM_ERR(CAM_SMMU, "Payload list empty");
  464. mutex_unlock(&iommu_cb_set.payload_list_lock);
  465. return;
  466. }
  467. payload = list_first_entry(&iommu_cb_set.payload_list,
  468. struct cam_smmu_work_payload,
  469. list);
  470. list_del(&payload->list);
  471. mutex_unlock(&iommu_cb_set.payload_list_lock);
  472. cam_check_iommu_faults(payload->domain, &pf_info);
  473. /* Dereference the payload to call the handler */
  474. idx = payload->idx;
  475. buf_info = cam_smmu_find_closest_mapping(idx, (void *)payload->iova);
  476. if (buf_info != 0)
  477. CAM_INFO(CAM_SMMU, "closest buf 0x%x idx %d", buf_info, idx);
  478. pf_info.domain = payload->domain;
  479. pf_info.dev = payload->dev;
  480. pf_info.iova = payload->iova;
  481. pf_info.flags = payload->flags;
  482. pf_info.buf_info = buf_info;
  483. for (j = 0; j < CAM_SMMU_CB_MAX; j++) {
  484. if ((iommu_cb_set.cb_info[idx].handler[j])) {
  485. pf_info.token = iommu_cb_set.cb_info[idx].token[j];
  486. iommu_cb_set.cb_info[idx].handler[j](&pf_info);
  487. }
  488. }
  489. cam_smmu_dump_cb_info(idx);
  490. kfree(payload);
  491. }
  492. static void cam_smmu_dump_cb_info(int idx)
  493. {
  494. struct cam_dma_buff_info *mapping, *mapping_temp;
  495. size_t shared_reg_len = 0, io_reg_len = 0;
  496. size_t shared_free_len = 0, io_free_len = 0;
  497. uint32_t i = 0;
  498. uint64_t ms, hrs, min, sec;
  499. struct timespec64 current_ts;
  500. struct cam_context_bank_info *cb_info =
  501. &iommu_cb_set.cb_info[idx];
  502. if (cb_info->shared_support) {
  503. shared_reg_len = cb_info->shared_info.iova_len;
  504. shared_free_len = shared_reg_len - cb_info->shared_mapping_size;
  505. }
  506. if (cb_info->io_support) {
  507. io_reg_len = cb_info->io_info.iova_len;
  508. io_free_len = io_reg_len - cb_info->io_mapping_size;
  509. }
  510. CAM_GET_TIMESTAMP(current_ts);
  511. CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
  512. CAM_ERR(CAM_SMMU,
  513. "********** %llu:%llu:%llu:%llu Context bank dump for %s **********",
  514. hrs, min, sec, ms, cb_info->name[0]);
  515. CAM_ERR(CAM_SMMU,
  516. "Usage: shared_usage=%u io_usage=%u shared_free=%u io_free=%u",
  517. (unsigned int)cb_info->shared_mapping_size,
  518. (unsigned int)cb_info->io_mapping_size,
  519. (unsigned int)shared_free_len,
  520. (unsigned int)io_free_len);
  521. if (iommu_cb_set.cb_dump_enable) {
  522. list_for_each_entry_safe(mapping, mapping_temp,
  523. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  524. i++;
  525. CAM_CONVERT_TIMESTAMP_FORMAT(mapping->ts, hrs, min, sec, ms);
  526. CAM_ERR(CAM_SMMU,
  527. "%llu:%llu:%llu:%llu: %u ion_fd=%d i_ino=%lu start=0x%x end=0x%x len=%u region=%d",
  528. hrs, min, sec, ms, i, mapping->ion_fd, mapping->i_ino,
  529. (void *)mapping->paddr,
  530. ((uint64_t)mapping->paddr +
  531. (uint64_t)mapping->len),
  532. (unsigned int)mapping->len,
  533. mapping->region_id);
  534. }
  535. cam_smmu_dump_monitor_array(&iommu_cb_set.cb_info[idx]);
  536. }
  537. }
  538. static void cam_smmu_print_user_list(int idx)
  539. {
  540. struct cam_dma_buff_info *mapping;
  541. CAM_ERR(CAM_SMMU, "index = %d", idx);
  542. list_for_each_entry(mapping,
  543. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  544. CAM_ERR(CAM_SMMU,
  545. "ion_fd = %d, i_ino=%lu, paddr= 0x%pK, len = %u, region = %d",
  546. mapping->ion_fd, mapping->i_ino, (void *)mapping->paddr,
  547. (unsigned int)mapping->len,
  548. mapping->region_id);
  549. }
  550. }
  551. static void cam_smmu_print_kernel_list(int idx)
  552. {
  553. struct cam_dma_buff_info *mapping;
  554. CAM_ERR(CAM_SMMU, "index = %d", idx);
  555. list_for_each_entry(mapping,
  556. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  557. CAM_ERR(CAM_SMMU,
  558. "dma_buf = %pK, i_ino = %lu, paddr= 0x%pK, len = %u, region = %d",
  559. mapping->buf, mapping->i_ino, (void *)mapping->paddr,
  560. (unsigned int)mapping->len,
  561. mapping->region_id);
  562. }
  563. }
  564. static void cam_smmu_print_table(void)
  565. {
  566. int i, j;
  567. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  568. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++) {
  569. CAM_ERR(CAM_SMMU,
  570. "i= %d, handle= %d, name_addr=%pK name %s",
  571. i, (int)iommu_cb_set.cb_info[i].handle,
  572. (void *)iommu_cb_set.cb_info[i].name[j],
  573. iommu_cb_set.cb_info[i].name[j]);
  574. }
  575. CAM_ERR(CAM_SMMU, "dev = %pK", iommu_cb_set.cb_info[i].dev);
  576. }
  577. }
  578. static uint32_t cam_smmu_find_closest_mapping(int idx, void *vaddr)
  579. {
  580. struct cam_dma_buff_info *mapping, *closest_mapping = NULL;
  581. unsigned long start_addr, end_addr, current_addr;
  582. uint32_t buf_handle = 0;
  583. long delta = 0, lowest_delta = 0;
  584. current_addr = (unsigned long)vaddr;
  585. list_for_each_entry(mapping,
  586. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  587. start_addr = (unsigned long)mapping->paddr;
  588. end_addr = (unsigned long)mapping->paddr + mapping->len;
  589. if (start_addr <= current_addr && current_addr <= end_addr) {
  590. closest_mapping = mapping;
  591. CAM_INFO(CAM_SMMU,
  592. "Found va 0x%lx in:0x%lx-0x%lx, fd %d i_ino %lu cb:%s",
  593. current_addr, start_addr,
  594. end_addr, mapping->ion_fd, mapping->i_ino,
  595. iommu_cb_set.cb_info[idx].name[0]);
  596. goto end;
  597. } else {
  598. if (start_addr > current_addr)
  599. delta = start_addr - current_addr;
  600. else
  601. delta = current_addr - end_addr - 1;
  602. if (delta < lowest_delta || lowest_delta == 0) {
  603. lowest_delta = delta;
  604. closest_mapping = mapping;
  605. }
  606. CAM_DBG(CAM_SMMU,
  607. "approx va %lx not in range: %lx-%lx fd = %0x i_ino %lu",
  608. current_addr, start_addr,
  609. end_addr, mapping->ion_fd, mapping->i_ino);
  610. }
  611. }
  612. end:
  613. if (closest_mapping) {
  614. buf_handle = GET_MEM_HANDLE(idx, closest_mapping->ion_fd);
  615. CAM_INFO(CAM_SMMU,
  616. "Closest map fd %d i_ino %lu 0x%lx %llu-%llu 0x%lx-0x%lx buf=%pK mem %0x",
  617. closest_mapping->ion_fd, closest_mapping->i_ino, current_addr,
  618. mapping->len, closest_mapping->len,
  619. (unsigned long)closest_mapping->paddr,
  620. (unsigned long)closest_mapping->paddr + mapping->len,
  621. closest_mapping->buf,
  622. buf_handle);
  623. } else
  624. CAM_ERR(CAM_SMMU,
  625. "Cannot find vaddr:%lx in SMMU %s virt address",
  626. current_addr, iommu_cb_set.cb_info[idx].name[0]);
  627. return buf_handle;
  628. }
  629. void cam_smmu_set_client_page_fault_handler(int handle,
  630. void (*handler_cb)(struct cam_smmu_pf_info *pf_info), void *token)
  631. {
  632. int idx, i = 0;
  633. if (!token || (handle == HANDLE_INIT)) {
  634. CAM_ERR(CAM_SMMU, "Error: token is NULL or invalid handle");
  635. return;
  636. }
  637. idx = GET_SMMU_TABLE_IDX(handle);
  638. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  639. CAM_ERR(CAM_SMMU,
  640. "Error: handle or index invalid. idx = %d hdl = %x",
  641. idx, handle);
  642. return;
  643. }
  644. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  645. if (iommu_cb_set.cb_info[idx].handle != handle) {
  646. CAM_ERR(CAM_SMMU,
  647. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  648. iommu_cb_set.cb_info[idx].handle, handle);
  649. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  650. return;
  651. }
  652. if (handler_cb) {
  653. if (iommu_cb_set.cb_info[idx].cb_count == CAM_SMMU_CB_MAX) {
  654. CAM_ERR(CAM_SMMU,
  655. "%s Should not regiester more handlers",
  656. iommu_cb_set.cb_info[idx].name[0]);
  657. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  658. return;
  659. }
  660. iommu_cb_set.cb_info[idx].cb_count++;
  661. for (i = 0; i < iommu_cb_set.cb_info[idx].cb_count; i++) {
  662. if (iommu_cb_set.cb_info[idx].token[i] == NULL) {
  663. iommu_cb_set.cb_info[idx].token[i] = token;
  664. iommu_cb_set.cb_info[idx].handler[i] =
  665. handler_cb;
  666. break;
  667. }
  668. }
  669. } else {
  670. for (i = 0; i < CAM_SMMU_CB_MAX; i++) {
  671. if (iommu_cb_set.cb_info[idx].token[i] == token) {
  672. iommu_cb_set.cb_info[idx].token[i] = NULL;
  673. iommu_cb_set.cb_info[idx].handler[i] =
  674. NULL;
  675. iommu_cb_set.cb_info[idx].cb_count--;
  676. break;
  677. }
  678. }
  679. if (i == CAM_SMMU_CB_MAX)
  680. CAM_ERR(CAM_SMMU,
  681. "Error: hdl %x no matching tokens: %s",
  682. handle, iommu_cb_set.cb_info[idx].name[0]);
  683. }
  684. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  685. }
  686. void cam_smmu_unset_client_page_fault_handler(int handle, void *token)
  687. {
  688. int idx, i = 0;
  689. if (!token || (handle == HANDLE_INIT)) {
  690. CAM_ERR(CAM_SMMU, "Error: token is NULL or invalid handle");
  691. return;
  692. }
  693. idx = GET_SMMU_TABLE_IDX(handle);
  694. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  695. CAM_ERR(CAM_SMMU,
  696. "Error: handle or index invalid. idx = %d hdl = %x",
  697. idx, handle);
  698. return;
  699. }
  700. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  701. if (iommu_cb_set.cb_info[idx].handle != handle) {
  702. CAM_ERR(CAM_SMMU,
  703. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  704. iommu_cb_set.cb_info[idx].handle, handle);
  705. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  706. return;
  707. }
  708. for (i = 0; i < CAM_SMMU_CB_MAX; i++) {
  709. if (iommu_cb_set.cb_info[idx].token[i] == token) {
  710. iommu_cb_set.cb_info[idx].token[i] = NULL;
  711. iommu_cb_set.cb_info[idx].handler[i] =
  712. NULL;
  713. iommu_cb_set.cb_info[idx].cb_count--;
  714. break;
  715. }
  716. }
  717. if (i == CAM_SMMU_CB_MAX)
  718. CAM_ERR(CAM_SMMU, "Error: hdl %x no matching tokens: %s",
  719. handle, iommu_cb_set.cb_info[idx].name[0]);
  720. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  721. }
  722. static int cam_smmu_iommu_fault_handler(struct iommu_domain *domain,
  723. struct device *dev, unsigned long iova,
  724. int flags, void *token)
  725. {
  726. char *cb_name;
  727. int idx;
  728. struct cam_smmu_work_payload *payload;
  729. if (!token) {
  730. CAM_ERR(CAM_SMMU,
  731. "token is NULL, domain = %pK, device = %pK,iova = %lX, flags = %d",
  732. domain, dev, iova, flags);
  733. return 0;
  734. }
  735. cb_name = (char *)token;
  736. /* Check whether it is in the table */
  737. for (idx = 0; idx < iommu_cb_set.cb_num; idx++) {
  738. if (!strcmp(iommu_cb_set.cb_info[idx].name[0], cb_name))
  739. break;
  740. }
  741. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  742. CAM_ERR(CAM_SMMU,
  743. "index is invalid, index = %d, token = %s, cb_num = %s",
  744. idx, cb_name, iommu_cb_set.cb_num);
  745. return 0;
  746. }
  747. if (++iommu_cb_set.cb_info[idx].pf_count > g_num_pf_handled) {
  748. CAM_INFO_RATE_LIMIT(CAM_SMMU, "PF already handled %d %d %d",
  749. g_num_pf_handled, idx,
  750. iommu_cb_set.cb_info[idx].pf_count);
  751. return 0;
  752. }
  753. payload = kzalloc(sizeof(struct cam_smmu_work_payload), GFP_ATOMIC);
  754. if (!payload)
  755. return 0;
  756. payload->domain = domain;
  757. payload->dev = dev;
  758. payload->iova = iova;
  759. payload->flags = flags;
  760. payload->token = token;
  761. payload->idx = idx;
  762. mutex_lock(&iommu_cb_set.payload_list_lock);
  763. list_add_tail(&payload->list, &iommu_cb_set.payload_list);
  764. mutex_unlock(&iommu_cb_set.payload_list_lock);
  765. cam_smmu_page_fault_work(&iommu_cb_set.smmu_work);
  766. return -ENOSYS;
  767. }
  768. void cam_smmu_reset_cb_page_fault_cnt(void)
  769. {
  770. int idx;
  771. for (idx = 0; idx < iommu_cb_set.cb_num; idx++)
  772. iommu_cb_set.cb_info[idx].pf_count = 0;
  773. }
  774. static int cam_smmu_translate_dir_to_iommu_dir(
  775. enum cam_smmu_map_dir dir)
  776. {
  777. switch (dir) {
  778. case CAM_SMMU_MAP_READ:
  779. return IOMMU_READ;
  780. case CAM_SMMU_MAP_WRITE:
  781. return IOMMU_WRITE;
  782. case CAM_SMMU_MAP_RW:
  783. return IOMMU_READ|IOMMU_WRITE;
  784. case CAM_SMMU_MAP_INVALID:
  785. default:
  786. CAM_ERR(CAM_SMMU, "Error: Direction is invalid. dir = %d", dir);
  787. break;
  788. };
  789. return IOMMU_INVALID_DIR;
  790. }
  791. static enum dma_data_direction cam_smmu_translate_dir(
  792. enum cam_smmu_map_dir dir)
  793. {
  794. switch (dir) {
  795. case CAM_SMMU_MAP_READ:
  796. return DMA_FROM_DEVICE;
  797. case CAM_SMMU_MAP_WRITE:
  798. return DMA_TO_DEVICE;
  799. case CAM_SMMU_MAP_RW:
  800. return DMA_BIDIRECTIONAL;
  801. case CAM_SMMU_MAP_INVALID:
  802. default:
  803. CAM_ERR(CAM_SMMU, "Error: Direction is invalid. dir = %d",
  804. (int)dir);
  805. break;
  806. }
  807. return DMA_NONE;
  808. }
  809. void cam_smmu_reset_iommu_table(enum cam_smmu_init_dir ops)
  810. {
  811. unsigned int i;
  812. int j = 0;
  813. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  814. iommu_cb_set.cb_info[i].handle = HANDLE_INIT;
  815. INIT_LIST_HEAD(&iommu_cb_set.cb_info[i].smmu_buf_list);
  816. INIT_LIST_HEAD(&iommu_cb_set.cb_info[i].smmu_buf_kernel_list);
  817. iommu_cb_set.cb_info[i].state = CAM_SMMU_DETACH;
  818. iommu_cb_set.cb_info[i].dev = NULL;
  819. iommu_cb_set.cb_info[i].cb_count = 0;
  820. iommu_cb_set.cb_info[i].pf_count = 0;
  821. for (j = 0; j < CAM_SMMU_CB_MAX; j++) {
  822. iommu_cb_set.cb_info[i].token[j] = NULL;
  823. iommu_cb_set.cb_info[i].handler[j] = NULL;
  824. }
  825. if (ops == CAM_SMMU_TABLE_INIT)
  826. mutex_init(&iommu_cb_set.cb_info[i].lock);
  827. else
  828. mutex_destroy(&iommu_cb_set.cb_info[i].lock);
  829. }
  830. }
  831. static bool cam_smmu_is_hdl_nonunique_or_null(int hdl)
  832. {
  833. int i;
  834. if ((hdl == HANDLE_INIT) || (!hdl)) {
  835. CAM_DBG(CAM_SMMU, "iommu handle: %d is not valid", hdl);
  836. return 1;
  837. }
  838. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  839. if (iommu_cb_set.cb_info[i].handle == HANDLE_INIT)
  840. continue;
  841. if (iommu_cb_set.cb_info[i].handle == hdl) {
  842. CAM_DBG(CAM_SMMU, "iommu handle %d conflicts",
  843. (int)hdl);
  844. return 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. /**
  850. * use low 2 bytes for handle cookie
  851. */
  852. static int cam_smmu_create_iommu_handle(int idx)
  853. {
  854. int rand, hdl = 0;
  855. get_random_bytes(&rand, COOKIE_NUM_BYTE);
  856. hdl = GET_SMMU_HDL(idx, rand);
  857. CAM_DBG(CAM_SMMU, "create handle value = %x", (int)hdl);
  858. return hdl;
  859. }
  860. static int cam_smmu_attach_device(int idx)
  861. {
  862. int rc;
  863. struct cam_context_bank_info *cb = &iommu_cb_set.cb_info[idx];
  864. /* attach the mapping to device */
  865. rc = iommu_attach_device(cb->domain, cb->dev);
  866. if (rc < 0) {
  867. CAM_ERR(CAM_SMMU, "Error: ARM IOMMU attach failed. ret = %d",
  868. rc);
  869. rc = -ENODEV;
  870. }
  871. return rc;
  872. }
  873. static int cam_smmu_create_add_handle_in_table(char *name,
  874. int *hdl)
  875. {
  876. int i, j, rc = -EINVAL;
  877. int handle;
  878. /* create handle and add in the iommu hardware table */
  879. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  880. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++) {
  881. if (strcmp(iommu_cb_set.cb_info[i].name[j], name))
  882. continue;
  883. if (iommu_cb_set.cb_info[i].handle == HANDLE_INIT) {
  884. mutex_lock(&iommu_cb_set.cb_info[i].lock);
  885. /* make sure handle is unique and non-zero*/
  886. do {
  887. handle =
  888. cam_smmu_create_iommu_handle(i);
  889. } while (cam_smmu_is_hdl_nonunique_or_null(
  890. handle));
  891. /* put handle in the table */
  892. iommu_cb_set.cb_info[i].handle = handle;
  893. iommu_cb_set.cb_info[i].cb_count = 0;
  894. if (iommu_cb_set.cb_info[i].is_secure)
  895. iommu_cb_set.cb_info[i].secure_count++;
  896. if (iommu_cb_set.cb_info[i].is_mul_client)
  897. iommu_cb_set.cb_info[i].device_count++;
  898. *hdl = handle;
  899. CAM_DBG(CAM_SMMU, "%s creates handle 0x%x",
  900. name, handle);
  901. mutex_unlock(&iommu_cb_set.cb_info[i].lock);
  902. rc = 0;
  903. goto end;
  904. } else {
  905. mutex_lock(&iommu_cb_set.cb_info[i].lock);
  906. if (iommu_cb_set.cb_info[i].is_secure) {
  907. iommu_cb_set.cb_info[i].secure_count++;
  908. *hdl = iommu_cb_set.cb_info[i].handle;
  909. mutex_unlock(
  910. &iommu_cb_set.cb_info[i].lock);
  911. return 0;
  912. }
  913. if (iommu_cb_set.cb_info[i].is_mul_client) {
  914. iommu_cb_set.cb_info[i].device_count++;
  915. *hdl = iommu_cb_set.cb_info[i].handle;
  916. mutex_unlock(
  917. &iommu_cb_set.cb_info[i].lock);
  918. CAM_DBG(CAM_SMMU,
  919. "%s already got handle 0x%x",
  920. name,
  921. iommu_cb_set.cb_info[i].handle);
  922. return 0;
  923. }
  924. CAM_ERR(CAM_SMMU,
  925. "Error: %s already got handle 0x%x",
  926. name, iommu_cb_set.cb_info[i].handle);
  927. mutex_unlock(&iommu_cb_set.cb_info[i].lock);
  928. rc = -EALREADY;
  929. goto end;
  930. }
  931. }
  932. }
  933. CAM_ERR(CAM_SMMU, "Error: Cannot find name %s or all handle exist",
  934. name);
  935. cam_smmu_print_table();
  936. end:
  937. return rc;
  938. }
  939. static int cam_smmu_init_scratch_map(struct scratch_mapping *scratch_map,
  940. dma_addr_t base, size_t size,
  941. int order)
  942. {
  943. unsigned int count = size >> (PAGE_SHIFT + order);
  944. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  945. int err = 0;
  946. if (!count) {
  947. err = -EINVAL;
  948. CAM_ERR(CAM_SMMU, "Page count is zero, size passed = %zu",
  949. size);
  950. goto bail;
  951. }
  952. scratch_map->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  953. if (!scratch_map->bitmap) {
  954. err = -ENOMEM;
  955. goto bail;
  956. }
  957. scratch_map->base = base;
  958. scratch_map->bits = BITS_PER_BYTE * bitmap_size;
  959. scratch_map->order = order;
  960. bail:
  961. return err;
  962. }
  963. static int cam_smmu_alloc_scratch_va(struct scratch_mapping *mapping,
  964. size_t size,
  965. dma_addr_t *iova)
  966. {
  967. unsigned int order = get_order(size);
  968. unsigned int align = 0;
  969. unsigned int count, start;
  970. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  971. (1 << mapping->order) - 1) >> mapping->order;
  972. /*
  973. * Transparently, add a guard page to the total count of pages
  974. * to be allocated
  975. */
  976. count++;
  977. if (order > mapping->order)
  978. align = (1 << (order - mapping->order)) - 1;
  979. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  980. count, align);
  981. if (start > mapping->bits)
  982. return -ENOMEM;
  983. bitmap_set(mapping->bitmap, start, count);
  984. *iova = mapping->base + (start << (mapping->order + PAGE_SHIFT));
  985. return 0;
  986. }
  987. static int cam_smmu_free_scratch_va(struct scratch_mapping *mapping,
  988. dma_addr_t addr, size_t size)
  989. {
  990. unsigned int start = (addr - mapping->base) >>
  991. (mapping->order + PAGE_SHIFT);
  992. unsigned int count = ((size >> PAGE_SHIFT) +
  993. (1 << mapping->order) - 1) >> mapping->order;
  994. if (!addr) {
  995. CAM_ERR(CAM_SMMU, "Error: Invalid address");
  996. return -EINVAL;
  997. }
  998. if (start + count > mapping->bits) {
  999. CAM_ERR(CAM_SMMU, "Error: Invalid page bits in scratch map");
  1000. return -EINVAL;
  1001. }
  1002. /*
  1003. * Transparently, add a guard page to the total count of pages
  1004. * to be freed
  1005. */
  1006. count++;
  1007. bitmap_clear(mapping->bitmap, start, count);
  1008. return 0;
  1009. }
  1010. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_virt_address(int idx,
  1011. dma_addr_t virt_addr)
  1012. {
  1013. struct cam_dma_buff_info *mapping;
  1014. list_for_each_entry(mapping, &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1015. list) {
  1016. if (mapping->paddr == virt_addr) {
  1017. CAM_DBG(CAM_SMMU, "Found virtual address %lx",
  1018. (unsigned long)virt_addr);
  1019. return mapping;
  1020. }
  1021. }
  1022. CAM_ERR(CAM_SMMU, "Error: Cannot find virtual address %lx by index %d",
  1023. (unsigned long)virt_addr, idx);
  1024. return NULL;
  1025. }
  1026. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_ion_index(int idx,
  1027. int ion_fd, struct dma_buf *dmabuf)
  1028. {
  1029. struct cam_dma_buff_info *mapping;
  1030. unsigned long i_ino;
  1031. if (ion_fd < 0) {
  1032. CAM_ERR(CAM_SMMU, "Invalid fd %d", ion_fd);
  1033. return NULL;
  1034. }
  1035. i_ino = file_inode(dmabuf->file)->i_ino;
  1036. list_for_each_entry(mapping,
  1037. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1038. list) {
  1039. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  1040. CAM_DBG(CAM_SMMU, "find ion_fd %d i_ino %lu", ion_fd, i_ino);
  1041. return mapping;
  1042. }
  1043. }
  1044. CAM_ERR(CAM_SMMU, "Error: Cannot find entry by index %d, fd %d i_ino %lu",
  1045. idx, ion_fd, i_ino);
  1046. return NULL;
  1047. }
  1048. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_dma_buf(int idx,
  1049. struct dma_buf *buf)
  1050. {
  1051. struct cam_dma_buff_info *mapping;
  1052. if (!buf) {
  1053. CAM_ERR(CAM_SMMU, "Invalid dma_buf");
  1054. return NULL;
  1055. }
  1056. list_for_each_entry(mapping,
  1057. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list,
  1058. list) {
  1059. if (mapping->buf == buf) {
  1060. CAM_DBG(CAM_SMMU, "find dma_buf %pK", buf);
  1061. return mapping;
  1062. }
  1063. }
  1064. CAM_ERR(CAM_SMMU, "Error: Cannot find entry by index %d", idx);
  1065. return NULL;
  1066. }
  1067. static struct cam_sec_buff_info *cam_smmu_find_mapping_by_sec_buf_idx(int idx,
  1068. int ion_fd, struct dma_buf *dmabuf)
  1069. {
  1070. struct cam_sec_buff_info *mapping;
  1071. unsigned long i_ino;
  1072. i_ino = file_inode(dmabuf->file)->i_ino;
  1073. list_for_each_entry(mapping, &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1074. list) {
  1075. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  1076. CAM_DBG(CAM_SMMU, "find ion_fd %d, i_ino %lu", ion_fd, i_ino);
  1077. return mapping;
  1078. }
  1079. }
  1080. CAM_ERR(CAM_SMMU, "Error: Cannot find fd %d i_ino %lu by index %d",
  1081. ion_fd, i_ino, idx);
  1082. return NULL;
  1083. }
  1084. static void cam_smmu_clean_user_buffer_list(int idx)
  1085. {
  1086. int ret;
  1087. struct cam_dma_buff_info *mapping_info, *temp;
  1088. list_for_each_entry_safe(mapping_info, temp,
  1089. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  1090. CAM_DBG(CAM_SMMU, "Free mapping address %pK, i = %d, fd = %d, i_ino = %lu",
  1091. (void *)mapping_info->paddr, idx,
  1092. mapping_info->ion_fd, mapping_info->i_ino);
  1093. if (mapping_info->ion_fd == 0xDEADBEEF)
  1094. /* Clean up scratch buffers */
  1095. ret = cam_smmu_free_scratch_buffer_remove_from_list(
  1096. mapping_info, idx);
  1097. else
  1098. /* Clean up regular mapped buffers */
  1099. ret = cam_smmu_unmap_buf_and_remove_from_list(
  1100. mapping_info,
  1101. idx);
  1102. if (ret < 0) {
  1103. CAM_ERR(CAM_SMMU, "Buffer delete failed: idx = %d",
  1104. idx);
  1105. CAM_ERR(CAM_SMMU,
  1106. "Buffer delete failed: addr = %lx, fd = %d, i_ino = %lu",
  1107. (unsigned long)mapping_info->paddr,
  1108. mapping_info->ion_fd, mapping_info->i_ino);
  1109. /*
  1110. * Ignore this error and continue to delete other
  1111. * buffers in the list
  1112. */
  1113. continue;
  1114. }
  1115. }
  1116. }
  1117. static void cam_smmu_clean_kernel_buffer_list(int idx)
  1118. {
  1119. int ret;
  1120. struct cam_dma_buff_info *mapping_info, *temp;
  1121. list_for_each_entry_safe(mapping_info, temp,
  1122. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  1123. CAM_DBG(CAM_SMMU,
  1124. "Free mapping address %pK, i = %d, dma_buf = %pK",
  1125. (void *)mapping_info->paddr, idx,
  1126. mapping_info->buf);
  1127. /* Clean up regular mapped buffers */
  1128. ret = cam_smmu_unmap_buf_and_remove_from_list(
  1129. mapping_info,
  1130. idx);
  1131. if (ret < 0) {
  1132. CAM_ERR(CAM_SMMU,
  1133. "Buffer delete in kernel list failed: idx = %d",
  1134. idx);
  1135. CAM_ERR(CAM_SMMU,
  1136. "Buffer delete failed: addr = %lx, dma_buf = %pK",
  1137. (unsigned long)mapping_info->paddr,
  1138. mapping_info->buf);
  1139. /*
  1140. * Ignore this error and continue to delete other
  1141. * buffers in the list
  1142. */
  1143. continue;
  1144. }
  1145. }
  1146. }
  1147. static int cam_smmu_attach(int idx)
  1148. {
  1149. int ret;
  1150. if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_ATTACH) {
  1151. ret = -EALREADY;
  1152. } else if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_DETACH) {
  1153. ret = cam_smmu_attach_device(idx);
  1154. if (ret < 0) {
  1155. CAM_ERR(CAM_SMMU, "Error: ATTACH fail");
  1156. return -ENODEV;
  1157. }
  1158. iommu_cb_set.cb_info[idx].state = CAM_SMMU_ATTACH;
  1159. ret = 0;
  1160. } else {
  1161. CAM_ERR(CAM_SMMU, "Error: Not detach/attach: %d",
  1162. iommu_cb_set.cb_info[idx].state);
  1163. ret = -EINVAL;
  1164. }
  1165. return ret;
  1166. }
  1167. static int cam_smmu_detach_device(int idx)
  1168. {
  1169. int rc = 0;
  1170. struct cam_context_bank_info *cb = &iommu_cb_set.cb_info[idx];
  1171. /* detach the mapping to device if not already detached */
  1172. if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_DETACH) {
  1173. rc = -EALREADY;
  1174. } else if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_ATTACH) {
  1175. iommu_detach_device(cb->domain, cb->dev);
  1176. iommu_cb_set.cb_info[idx].state = CAM_SMMU_DETACH;
  1177. }
  1178. return rc;
  1179. }
  1180. static int cam_smmu_alloc_iova(size_t size,
  1181. int32_t smmu_hdl, unsigned long *iova)
  1182. {
  1183. int rc = 0;
  1184. int idx;
  1185. unsigned long vaddr = 0;
  1186. if (!iova || !size || (smmu_hdl == HANDLE_INIT)) {
  1187. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1188. return -EINVAL;
  1189. }
  1190. CAM_DBG(CAM_SMMU, "Allocating iova size = %zu for smmu hdl=%X",
  1191. size, smmu_hdl);
  1192. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1193. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1194. CAM_ERR(CAM_SMMU,
  1195. "Error: handle or index invalid. idx = %d hdl = %x",
  1196. idx, smmu_hdl);
  1197. return -EINVAL;
  1198. }
  1199. if (iommu_cb_set.cb_info[idx].handle != smmu_hdl) {
  1200. CAM_ERR(CAM_SMMU,
  1201. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1202. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1203. rc = -EINVAL;
  1204. goto get_addr_end;
  1205. }
  1206. if (!iommu_cb_set.cb_info[idx].shared_support) {
  1207. CAM_ERR(CAM_SMMU,
  1208. "Error: Shared memory not supported for hdl = %X",
  1209. smmu_hdl);
  1210. rc = -EINVAL;
  1211. goto get_addr_end;
  1212. }
  1213. vaddr = gen_pool_alloc(iommu_cb_set.cb_info[idx].shared_mem_pool, size);
  1214. if (!vaddr)
  1215. return -ENOMEM;
  1216. *iova = vaddr;
  1217. get_addr_end:
  1218. return rc;
  1219. }
  1220. static int cam_smmu_free_iova(unsigned long iova, size_t size,
  1221. int32_t smmu_hdl)
  1222. {
  1223. int rc = 0;
  1224. int idx;
  1225. if (!size || (smmu_hdl == HANDLE_INIT)) {
  1226. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1227. return -EINVAL;
  1228. }
  1229. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1230. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1231. CAM_ERR(CAM_SMMU,
  1232. "Error: handle or index invalid. idx = %d hdl = %x",
  1233. idx, smmu_hdl);
  1234. return -EINVAL;
  1235. }
  1236. if (iommu_cb_set.cb_info[idx].handle != smmu_hdl) {
  1237. CAM_ERR(CAM_SMMU,
  1238. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1239. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1240. rc = -EINVAL;
  1241. goto get_addr_end;
  1242. }
  1243. gen_pool_free(iommu_cb_set.cb_info[idx].shared_mem_pool, iova, size);
  1244. get_addr_end:
  1245. return rc;
  1246. }
  1247. int cam_smmu_alloc_firmware(int32_t smmu_hdl,
  1248. dma_addr_t *iova,
  1249. uintptr_t *cpuva,
  1250. size_t *len)
  1251. {
  1252. int rc;
  1253. int32_t idx;
  1254. size_t firmware_len = 0;
  1255. size_t firmware_start = 0;
  1256. struct iommu_domain *domain;
  1257. if (!iova || !len || !cpuva || (smmu_hdl == HANDLE_INIT)) {
  1258. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1259. return -EINVAL;
  1260. }
  1261. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1262. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1263. CAM_ERR(CAM_SMMU,
  1264. "Error: handle or index invalid. idx = %d hdl = %x",
  1265. idx, smmu_hdl);
  1266. rc = -EINVAL;
  1267. goto end;
  1268. }
  1269. if (!iommu_cb_set.cb_info[idx].firmware_support) {
  1270. CAM_ERR(CAM_SMMU,
  1271. "Firmware memory not supported for this SMMU handle");
  1272. rc = -EINVAL;
  1273. goto end;
  1274. }
  1275. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1276. if (iommu_cb_set.cb_info[idx].is_fw_allocated) {
  1277. CAM_ERR(CAM_SMMU, "Trying to allocate twice");
  1278. rc = -ENOMEM;
  1279. goto unlock_and_end;
  1280. }
  1281. firmware_len = iommu_cb_set.cb_info[idx].firmware_info.iova_len;
  1282. firmware_start = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1283. CAM_DBG(CAM_SMMU, "Firmware area len from DT = %zu", firmware_len);
  1284. rc = cam_reserve_icp_fw(&icp_fw, firmware_len);
  1285. if (rc)
  1286. goto unlock_and_end;
  1287. else
  1288. CAM_DBG(CAM_SMMU, "DMA alloc returned fw = %pK, hdl = %pK",
  1289. icp_fw.fw_kva, (void *)icp_fw.fw_hdl);
  1290. domain = iommu_cb_set.cb_info[idx].domain;
  1291. /*
  1292. * Revisit this - what should we map this with - CACHED or UNCACHED?
  1293. * chipsets using dma-coherent-hint-cached - leaving it like this is
  1294. * fine as we can map both CACHED and UNCACHED on same CB.
  1295. * But on chipsets which use dma-coherent - all the buffers that are
  1296. * being mapped to this CB must be CACHED
  1297. */
  1298. rc = iommu_map(domain,
  1299. firmware_start,
  1300. (phys_addr_t) icp_fw.fw_hdl,
  1301. firmware_len,
  1302. IOMMU_READ|IOMMU_WRITE|IOMMU_PRIV);
  1303. if (rc) {
  1304. CAM_ERR(CAM_SMMU, "Failed to map FW into IOMMU");
  1305. rc = -ENOMEM;
  1306. goto alloc_fail;
  1307. }
  1308. iommu_cb_set.cb_info[idx].is_fw_allocated = true;
  1309. *iova = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1310. *cpuva = (uintptr_t)icp_fw.fw_kva;
  1311. *len = firmware_len;
  1312. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1313. return rc;
  1314. alloc_fail:
  1315. cam_unreserve_icp_fw(&icp_fw, firmware_len);
  1316. unlock_and_end:
  1317. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1318. end:
  1319. return rc;
  1320. }
  1321. EXPORT_SYMBOL(cam_smmu_alloc_firmware);
  1322. int cam_smmu_dealloc_firmware(int32_t smmu_hdl)
  1323. {
  1324. int rc = 0;
  1325. int32_t idx;
  1326. size_t firmware_len = 0;
  1327. size_t firmware_start = 0;
  1328. struct iommu_domain *domain;
  1329. size_t unmapped = 0;
  1330. if (smmu_hdl == HANDLE_INIT) {
  1331. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  1332. return -EINVAL;
  1333. }
  1334. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1335. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1336. CAM_ERR(CAM_SMMU,
  1337. "Error: handle or index invalid. idx = %d hdl = %x",
  1338. idx, smmu_hdl);
  1339. rc = -EINVAL;
  1340. goto end;
  1341. }
  1342. if (!iommu_cb_set.cb_info[idx].firmware_support) {
  1343. CAM_ERR(CAM_SMMU,
  1344. "Firmware memory not supported for this SMMU handle");
  1345. rc = -EINVAL;
  1346. goto end;
  1347. }
  1348. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1349. if (!iommu_cb_set.cb_info[idx].is_fw_allocated) {
  1350. CAM_ERR(CAM_SMMU,
  1351. "Trying to deallocate firmware that is not allocated");
  1352. rc = -ENOMEM;
  1353. goto unlock_and_end;
  1354. }
  1355. firmware_len = iommu_cb_set.cb_info[idx].firmware_info.iova_len;
  1356. firmware_start = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1357. domain = iommu_cb_set.cb_info[idx].domain;
  1358. unmapped = iommu_unmap(domain,
  1359. firmware_start,
  1360. firmware_len);
  1361. if (unmapped != firmware_len) {
  1362. CAM_ERR(CAM_SMMU, "Only %zu unmapped out of total %zu",
  1363. unmapped,
  1364. firmware_len);
  1365. rc = -EINVAL;
  1366. }
  1367. cam_unreserve_icp_fw(&icp_fw, firmware_len);
  1368. icp_fw.fw_kva = NULL;
  1369. icp_fw.fw_hdl = 0;
  1370. iommu_cb_set.cb_info[idx].is_fw_allocated = false;
  1371. unlock_and_end:
  1372. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1373. end:
  1374. return rc;
  1375. }
  1376. EXPORT_SYMBOL(cam_smmu_dealloc_firmware);
  1377. int cam_smmu_alloc_qdss(int32_t smmu_hdl,
  1378. dma_addr_t *iova,
  1379. size_t *len)
  1380. {
  1381. int rc;
  1382. int32_t idx;
  1383. size_t qdss_len = 0;
  1384. size_t qdss_start = 0;
  1385. dma_addr_t qdss_phy_addr;
  1386. struct iommu_domain *domain;
  1387. if (!iova || !len || (smmu_hdl == HANDLE_INIT)) {
  1388. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1389. return -EINVAL;
  1390. }
  1391. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1392. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1393. CAM_ERR(CAM_SMMU,
  1394. "Error: handle or index invalid. idx = %d hdl = %x",
  1395. idx, smmu_hdl);
  1396. rc = -EINVAL;
  1397. goto end;
  1398. }
  1399. if (!iommu_cb_set.cb_info[idx].qdss_support) {
  1400. CAM_ERR(CAM_SMMU,
  1401. "QDSS memory not supported for this SMMU handle");
  1402. rc = -EINVAL;
  1403. goto end;
  1404. }
  1405. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1406. if (iommu_cb_set.cb_info[idx].is_qdss_allocated) {
  1407. CAM_ERR(CAM_SMMU, "Trying to allocate twice");
  1408. rc = -ENOMEM;
  1409. goto unlock_and_end;
  1410. }
  1411. qdss_len = iommu_cb_set.cb_info[idx].qdss_info.iova_len;
  1412. qdss_start = iommu_cb_set.cb_info[idx].qdss_info.iova_start;
  1413. qdss_phy_addr = iommu_cb_set.cb_info[idx].qdss_phy_addr;
  1414. CAM_DBG(CAM_SMMU, "QDSS area len from DT = %zu", qdss_len);
  1415. domain = iommu_cb_set.cb_info[idx].domain;
  1416. /*
  1417. * Revisit this - what should we map this with - CACHED or UNCACHED?
  1418. * chipsets using dma-coherent-hint-cached - leaving it like this is
  1419. * fine as we can map both CACHED and UNCACHED on same CB.
  1420. * But on chipsets which use dma-coherent - all the buffers that are
  1421. * being mapped to this CB must be CACHED
  1422. */
  1423. rc = iommu_map(domain,
  1424. qdss_start,
  1425. qdss_phy_addr,
  1426. qdss_len,
  1427. IOMMU_READ|IOMMU_WRITE);
  1428. if (rc) {
  1429. CAM_ERR(CAM_SMMU, "Failed to map QDSS into IOMMU");
  1430. goto unlock_and_end;
  1431. }
  1432. iommu_cb_set.cb_info[idx].is_qdss_allocated = true;
  1433. *iova = iommu_cb_set.cb_info[idx].qdss_info.iova_start;
  1434. *len = qdss_len;
  1435. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1436. return rc;
  1437. unlock_and_end:
  1438. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1439. end:
  1440. return rc;
  1441. }
  1442. EXPORT_SYMBOL(cam_smmu_alloc_qdss);
  1443. int cam_smmu_dealloc_qdss(int32_t smmu_hdl)
  1444. {
  1445. int rc = 0;
  1446. int32_t idx;
  1447. size_t qdss_len = 0;
  1448. size_t qdss_start = 0;
  1449. struct iommu_domain *domain;
  1450. size_t unmapped = 0;
  1451. if (smmu_hdl == HANDLE_INIT) {
  1452. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  1453. return -EINVAL;
  1454. }
  1455. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1456. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1457. CAM_ERR(CAM_SMMU,
  1458. "Error: handle or index invalid. idx = %d hdl = %x",
  1459. idx, smmu_hdl);
  1460. rc = -EINVAL;
  1461. goto end;
  1462. }
  1463. if (!iommu_cb_set.cb_info[idx].qdss_support) {
  1464. CAM_ERR(CAM_SMMU,
  1465. "QDSS memory not supported for this SMMU handle");
  1466. rc = -EINVAL;
  1467. goto end;
  1468. }
  1469. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1470. if (!iommu_cb_set.cb_info[idx].is_qdss_allocated) {
  1471. CAM_ERR(CAM_SMMU,
  1472. "Trying to deallocate qdss that is not allocated");
  1473. rc = -ENOMEM;
  1474. goto unlock_and_end;
  1475. }
  1476. qdss_len = iommu_cb_set.cb_info[idx].qdss_info.iova_len;
  1477. qdss_start = iommu_cb_set.cb_info[idx].qdss_info.iova_start;
  1478. domain = iommu_cb_set.cb_info[idx].domain;
  1479. unmapped = iommu_unmap(domain, qdss_start, qdss_len);
  1480. if (unmapped != qdss_len) {
  1481. CAM_ERR(CAM_SMMU, "Only %zu unmapped out of total %zu",
  1482. unmapped,
  1483. qdss_len);
  1484. rc = -EINVAL;
  1485. }
  1486. iommu_cb_set.cb_info[idx].is_qdss_allocated = false;
  1487. unlock_and_end:
  1488. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1489. end:
  1490. return rc;
  1491. }
  1492. EXPORT_SYMBOL(cam_smmu_dealloc_qdss);
  1493. int cam_smmu_get_io_region_info(int32_t smmu_hdl,
  1494. dma_addr_t *iova, size_t *len,
  1495. dma_addr_t *discard_iova_start, size_t *discard_iova_len)
  1496. {
  1497. int32_t idx;
  1498. if (!iova || !len || !discard_iova_start || !discard_iova_len ||
  1499. (smmu_hdl == HANDLE_INIT)) {
  1500. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1501. return -EINVAL;
  1502. }
  1503. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1504. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1505. CAM_ERR(CAM_SMMU,
  1506. "Error: handle or index invalid. idx = %d hdl = %x",
  1507. idx, smmu_hdl);
  1508. return -EINVAL;
  1509. }
  1510. if (!iommu_cb_set.cb_info[idx].io_support) {
  1511. CAM_ERR(CAM_SMMU,
  1512. "I/O memory not supported for this SMMU handle");
  1513. return -EINVAL;
  1514. }
  1515. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1516. *iova = iommu_cb_set.cb_info[idx].io_info.iova_start;
  1517. *len = iommu_cb_set.cb_info[idx].io_info.iova_len;
  1518. *discard_iova_start =
  1519. iommu_cb_set.cb_info[idx].io_info.discard_iova_start;
  1520. *discard_iova_len =
  1521. iommu_cb_set.cb_info[idx].io_info.discard_iova_len;
  1522. CAM_DBG(CAM_SMMU,
  1523. "I/O area for hdl = %x Region:[%pK %zu] Discard:[%pK %zu]",
  1524. smmu_hdl, *iova, *len,
  1525. *discard_iova_start, *discard_iova_len);
  1526. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1527. return 0;
  1528. }
  1529. int cam_smmu_get_region_info(int32_t smmu_hdl,
  1530. enum cam_smmu_region_id region_id,
  1531. struct cam_smmu_region_info *region_info)
  1532. {
  1533. int32_t idx;
  1534. struct cam_context_bank_info *cb = NULL;
  1535. if (!region_info) {
  1536. CAM_ERR(CAM_SMMU, "Invalid region_info pointer");
  1537. return -EINVAL;
  1538. }
  1539. if (smmu_hdl == HANDLE_INIT) {
  1540. CAM_ERR(CAM_SMMU, "Invalid handle");
  1541. return -EINVAL;
  1542. }
  1543. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1544. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1545. CAM_ERR(CAM_SMMU, "Handle or index invalid. idx = %d hdl = %x",
  1546. idx, smmu_hdl);
  1547. return -EINVAL;
  1548. }
  1549. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1550. cb = &iommu_cb_set.cb_info[idx];
  1551. if (!cb) {
  1552. CAM_ERR(CAM_SMMU, "SMMU context bank pointer invalid");
  1553. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1554. return -EINVAL;
  1555. }
  1556. switch (region_id) {
  1557. case CAM_SMMU_REGION_FIRMWARE:
  1558. if (!cb->firmware_support) {
  1559. CAM_ERR(CAM_SMMU, "Firmware not supported");
  1560. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1561. return -ENODEV;
  1562. }
  1563. region_info->iova_start = cb->firmware_info.iova_start;
  1564. region_info->iova_len = cb->firmware_info.iova_len;
  1565. break;
  1566. case CAM_SMMU_REGION_SHARED:
  1567. if (!cb->shared_support) {
  1568. CAM_ERR(CAM_SMMU, "Shared mem not supported");
  1569. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1570. return -ENODEV;
  1571. }
  1572. region_info->iova_start = cb->shared_info.iova_start;
  1573. region_info->iova_len = cb->shared_info.iova_len;
  1574. break;
  1575. case CAM_SMMU_REGION_SCRATCH:
  1576. if (!cb->scratch_buf_support) {
  1577. CAM_ERR(CAM_SMMU, "Scratch memory not supported");
  1578. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1579. return -ENODEV;
  1580. }
  1581. region_info->iova_start = cb->scratch_info.iova_start;
  1582. region_info->iova_len = cb->scratch_info.iova_len;
  1583. break;
  1584. case CAM_SMMU_REGION_IO:
  1585. if (!cb->io_support) {
  1586. CAM_ERR(CAM_SMMU, "IO memory not supported");
  1587. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1588. return -ENODEV;
  1589. }
  1590. region_info->iova_start = cb->io_info.iova_start;
  1591. region_info->iova_len = cb->io_info.iova_len;
  1592. break;
  1593. case CAM_SMMU_REGION_SECHEAP:
  1594. if (!cb->secheap_support) {
  1595. CAM_ERR(CAM_SMMU, "Secondary heap not supported");
  1596. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1597. return -ENODEV;
  1598. }
  1599. region_info->iova_start = cb->secheap_info.iova_start;
  1600. region_info->iova_len = cb->secheap_info.iova_len;
  1601. break;
  1602. case CAM_SMMU_REGION_FWUNCACHED:
  1603. if (!cb->fwuncached_region_support) {
  1604. CAM_WARN(CAM_SMMU, "FW uncached region not supported");
  1605. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1606. return -ENODEV;
  1607. }
  1608. region_info->iova_start = cb->fwuncached_region.iova_start;
  1609. region_info->iova_len = cb->fwuncached_region.iova_len;
  1610. break;
  1611. default:
  1612. CAM_ERR(CAM_SMMU, "Invalid region id: %d for smmu hdl: %X",
  1613. smmu_hdl, region_id);
  1614. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1615. return -EINVAL;
  1616. }
  1617. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1618. return 0;
  1619. }
  1620. EXPORT_SYMBOL(cam_smmu_get_region_info);
  1621. int cam_smmu_reserve_buf_region(enum cam_smmu_region_id region,
  1622. int32_t smmu_hdl,
  1623. struct dma_buf *buf,
  1624. dma_addr_t *iova,
  1625. size_t *request_len)
  1626. {
  1627. struct cam_context_bank_info *cb_info;
  1628. struct region_buf_info *buf_info = NULL;
  1629. struct cam_smmu_region_info *region_info = NULL;
  1630. bool *is_buf_allocated;
  1631. bool region_supported;
  1632. size_t size = 0;
  1633. int idx;
  1634. int rc = 0;
  1635. int prot = 0;
  1636. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1637. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1638. CAM_ERR(CAM_SMMU,
  1639. "Error: handle or index invalid. idx = %d hdl = %x",
  1640. idx, smmu_hdl);
  1641. return -EINVAL;
  1642. }
  1643. cb_info = &iommu_cb_set.cb_info[idx];
  1644. if (region == CAM_SMMU_REGION_SECHEAP) {
  1645. region_supported = cb_info->secheap_support;
  1646. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  1647. region_supported = cb_info->fwuncached_region_support;
  1648. } else {
  1649. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  1650. region);
  1651. return -EINVAL;
  1652. }
  1653. if (!region_supported) {
  1654. CAM_ERR(CAM_SMMU, "Reserve for region %d not supported",
  1655. region);
  1656. return -EINVAL;
  1657. }
  1658. mutex_lock(&cb_info->lock);
  1659. if (region == CAM_SMMU_REGION_SECHEAP) {
  1660. is_buf_allocated = &cb_info->is_secheap_allocated;
  1661. buf_info = &cb_info->secheap_buf;
  1662. region_info = &cb_info->secheap_info;
  1663. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  1664. is_buf_allocated = &cb_info->is_fwuncached_buf_allocated;
  1665. buf_info = &cb_info->fwuncached_reg_buf;
  1666. region_info = &cb_info->fwuncached_region;
  1667. } else {
  1668. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  1669. region);
  1670. mutex_unlock(&cb_info->lock);
  1671. return -EINVAL;
  1672. }
  1673. if (*is_buf_allocated) {
  1674. CAM_ERR(CAM_SMMU, "Trying to allocate heap twice for region %d",
  1675. region);
  1676. rc = -ENOMEM;
  1677. mutex_unlock(&cb_info->lock);
  1678. return rc;
  1679. }
  1680. if (IS_ERR_OR_NULL(buf)) {
  1681. rc = PTR_ERR(buf);
  1682. CAM_ERR(CAM_SMMU,
  1683. "Error: dma get buf failed. rc = %d", rc);
  1684. goto err_out;
  1685. }
  1686. buf_info->buf = buf;
  1687. buf_info->attach = dma_buf_attach(buf_info->buf,
  1688. cb_info->dev);
  1689. if (IS_ERR_OR_NULL(buf_info->attach)) {
  1690. rc = PTR_ERR(buf_info->attach);
  1691. CAM_ERR(CAM_SMMU, "Error: dma buf attach failed");
  1692. goto err_put;
  1693. }
  1694. buf_info->table = dma_buf_map_attachment(buf_info->attach,
  1695. DMA_BIDIRECTIONAL);
  1696. if (IS_ERR_OR_NULL(buf_info->table)) {
  1697. rc = PTR_ERR(buf_info->table);
  1698. CAM_ERR(CAM_SMMU, "Error: dma buf map attachment failed");
  1699. goto err_detach;
  1700. }
  1701. prot = IOMMU_READ | IOMMU_WRITE;
  1702. if (iommu_cb_set.force_cache_allocs)
  1703. prot |= IOMMU_CACHE;
  1704. size = iommu_map_sg(cb_info->domain,
  1705. region_info->iova_start,
  1706. buf_info->table->sgl,
  1707. buf_info->table->orig_nents,
  1708. prot);
  1709. if (size != region_info->iova_len) {
  1710. CAM_ERR(CAM_SMMU,
  1711. "IOMMU mapping failed size=%zu, iova_len=%zu",
  1712. size, region_info->iova_len);
  1713. goto err_unmap_sg;
  1714. }
  1715. *is_buf_allocated = true;
  1716. *iova = (uint32_t)region_info->iova_start;
  1717. *request_len = region_info->iova_len;
  1718. mutex_unlock(&cb_info->lock);
  1719. return rc;
  1720. err_unmap_sg:
  1721. dma_buf_unmap_attachment(buf_info->attach,
  1722. buf_info->table,
  1723. DMA_BIDIRECTIONAL);
  1724. err_detach:
  1725. dma_buf_detach(buf_info->buf,
  1726. buf_info->attach);
  1727. err_put:
  1728. dma_buf_put(buf_info->buf);
  1729. err_out:
  1730. mutex_unlock(&cb_info->lock);
  1731. return rc;
  1732. }
  1733. EXPORT_SYMBOL(cam_smmu_reserve_buf_region);
  1734. int cam_smmu_release_buf_region(enum cam_smmu_region_id region,
  1735. int32_t smmu_hdl)
  1736. {
  1737. int idx;
  1738. size_t size = 0;
  1739. struct region_buf_info *buf_info = NULL;
  1740. struct cam_context_bank_info *cb_info;
  1741. bool *is_buf_allocated;
  1742. bool region_supported;
  1743. struct cam_smmu_region_info *region_info = NULL;
  1744. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1745. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1746. CAM_ERR(CAM_SMMU,
  1747. "Error: handle or index invalid. idx = %d hdl = %x",
  1748. idx, smmu_hdl);
  1749. return -EINVAL;
  1750. }
  1751. cb_info = &iommu_cb_set.cb_info[idx];
  1752. if (region == CAM_SMMU_REGION_SECHEAP) {
  1753. region_supported = cb_info->secheap_support;
  1754. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  1755. region_supported = cb_info->fwuncached_region_support;
  1756. } else {
  1757. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  1758. region);
  1759. return -EINVAL;
  1760. }
  1761. if (!region_supported) {
  1762. CAM_ERR(CAM_SMMU, "Secondary heap not supported");
  1763. return -EINVAL;
  1764. }
  1765. mutex_lock(&cb_info->lock);
  1766. if (region == CAM_SMMU_REGION_SECHEAP) {
  1767. is_buf_allocated = &cb_info->is_secheap_allocated;
  1768. buf_info = &cb_info->secheap_buf;
  1769. region_info = &cb_info->secheap_info;
  1770. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  1771. is_buf_allocated = &cb_info->is_fwuncached_buf_allocated;
  1772. buf_info = &cb_info->fwuncached_reg_buf;
  1773. region_info = &cb_info->fwuncached_region;
  1774. } else {
  1775. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  1776. region);
  1777. mutex_unlock(&cb_info->lock);
  1778. return -EINVAL;
  1779. }
  1780. if (!(*is_buf_allocated)) {
  1781. CAM_ERR(CAM_SMMU, "Trying to release secheap twice");
  1782. mutex_unlock(&cb_info->lock);
  1783. return -ENOMEM;
  1784. }
  1785. size = iommu_unmap(cb_info->domain,
  1786. region_info->iova_start,
  1787. region_info->iova_len);
  1788. if (size != region_info->iova_len) {
  1789. CAM_ERR(CAM_SMMU, "Failed: Unmapped = %zu, requested = %zu",
  1790. size,
  1791. region_info->iova_len);
  1792. }
  1793. dma_buf_unmap_attachment(buf_info->attach,
  1794. buf_info->table, DMA_BIDIRECTIONAL);
  1795. dma_buf_detach(buf_info->buf, buf_info->attach);
  1796. dma_buf_put(buf_info->buf);
  1797. *is_buf_allocated = false;
  1798. mutex_unlock(&cb_info->lock);
  1799. return 0;
  1800. }
  1801. EXPORT_SYMBOL(cam_smmu_release_buf_region);
  1802. static int cam_smmu_map_buffer_validate(struct dma_buf *buf,
  1803. int idx, enum dma_data_direction dma_dir, dma_addr_t *paddr_ptr,
  1804. size_t *len_ptr, enum cam_smmu_region_id region_id,
  1805. bool dis_delayed_unmap, struct cam_dma_buff_info **mapping_info)
  1806. {
  1807. struct dma_buf_attachment *attach = NULL;
  1808. struct sg_table *table = NULL;
  1809. struct iommu_domain *domain;
  1810. size_t size = 0;
  1811. unsigned long iova = 0;
  1812. int rc = 0;
  1813. struct timespec64 ts1, ts2;
  1814. long microsec = 0;
  1815. int prot = 0;
  1816. if (IS_ERR_OR_NULL(buf)) {
  1817. rc = PTR_ERR(buf);
  1818. CAM_ERR(CAM_SMMU,
  1819. "Error: dma get buf failed. rc = %d", rc);
  1820. goto err_out;
  1821. }
  1822. if (!mapping_info) {
  1823. rc = -EINVAL;
  1824. CAM_ERR(CAM_SMMU, "Error: mapping_info is invalid");
  1825. goto err_out;
  1826. }
  1827. if (iommu_cb_set.map_profile_enable)
  1828. CAM_GET_TIMESTAMP(ts1);
  1829. attach = dma_buf_attach(buf, iommu_cb_set.cb_info[idx].dev);
  1830. if (IS_ERR_OR_NULL(attach)) {
  1831. rc = PTR_ERR(attach);
  1832. CAM_ERR(CAM_SMMU, "Error: dma buf attach failed");
  1833. goto err_put;
  1834. }
  1835. if (region_id == CAM_SMMU_REGION_SHARED) {
  1836. table = dma_buf_map_attachment(attach, dma_dir);
  1837. if (IS_ERR_OR_NULL(table)) {
  1838. rc = PTR_ERR(table);
  1839. CAM_ERR(CAM_SMMU, "Error: dma map attachment failed");
  1840. goto err_detach;
  1841. }
  1842. domain = iommu_cb_set.cb_info[idx].domain;
  1843. if (!domain) {
  1844. CAM_ERR(CAM_SMMU, "CB has no domain set");
  1845. goto err_unmap_sg;
  1846. }
  1847. rc = cam_smmu_alloc_iova(*len_ptr, iommu_cb_set.cb_info[idx].handle, &iova);
  1848. if (rc < 0) {
  1849. CAM_ERR(CAM_SMMU,
  1850. "IOVA alloc failed for shared memory, size=%zu, idx=%d, handle=%d",
  1851. *len_ptr, idx,
  1852. iommu_cb_set.cb_info[idx].handle);
  1853. goto err_unmap_sg;
  1854. }
  1855. prot = IOMMU_READ | IOMMU_WRITE;
  1856. if (iommu_cb_set.force_cache_allocs)
  1857. prot |= IOMMU_CACHE;
  1858. size = iommu_map_sg(domain, iova, table->sgl, table->orig_nents,
  1859. prot);
  1860. if (size < 0) {
  1861. CAM_ERR(CAM_SMMU, "IOMMU mapping failed");
  1862. rc = cam_smmu_free_iova(iova,
  1863. size, iommu_cb_set.cb_info[idx].handle);
  1864. if (rc)
  1865. CAM_ERR(CAM_SMMU, "IOVA free failed");
  1866. rc = -ENOMEM;
  1867. goto err_unmap_sg;
  1868. } else {
  1869. CAM_DBG(CAM_SMMU,
  1870. "iommu_map_sg returned iova=%pK, size=%zu",
  1871. iova, size);
  1872. *paddr_ptr = iova;
  1873. *len_ptr = size;
  1874. }
  1875. iommu_cb_set.cb_info[idx].shared_mapping_size += *len_ptr;
  1876. } else if (region_id == CAM_SMMU_REGION_IO) {
  1877. if (!dis_delayed_unmap)
  1878. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  1879. table = dma_buf_map_attachment(attach, dma_dir);
  1880. if (IS_ERR_OR_NULL(table)) {
  1881. rc = PTR_ERR(table);
  1882. CAM_ERR(CAM_SMMU,
  1883. "Error: dma map attachment failed, size=%zu",
  1884. buf->size);
  1885. goto err_detach;
  1886. }
  1887. *paddr_ptr = sg_dma_address(table->sgl);
  1888. *len_ptr = (size_t)buf->size;
  1889. iommu_cb_set.cb_info[idx].io_mapping_size += *len_ptr;
  1890. } else {
  1891. CAM_ERR(CAM_SMMU, "Error: Wrong region id passed");
  1892. rc = -EINVAL;
  1893. goto err_detach;
  1894. }
  1895. CAM_DBG(CAM_SMMU,
  1896. "iova=%pK, region_id=%d, paddr=0x%llx, len=%zu, dma_map_attrs=%d",
  1897. iova, region_id, *paddr_ptr, *len_ptr, attach->dma_map_attrs);
  1898. if (iommu_cb_set.map_profile_enable) {
  1899. CAM_GET_TIMESTAMP(ts2);
  1900. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  1901. trace_cam_log_event("SMMUMapProfile", "size and time in micro",
  1902. *len_ptr, microsec);
  1903. }
  1904. if (table->sgl) {
  1905. CAM_DBG(CAM_SMMU,
  1906. "DMA buf: %pK, device: %pK, attach: %pK, table: %pK",
  1907. (void *)buf,
  1908. (void *)iommu_cb_set.cb_info[idx].dev,
  1909. (void *)attach, (void *)table);
  1910. CAM_DBG(CAM_SMMU, "table sgl: %pK, rc: %d, dma_address: 0x%x",
  1911. (void *)table->sgl, rc,
  1912. (unsigned int)table->sgl->dma_address);
  1913. } else {
  1914. rc = -EINVAL;
  1915. CAM_ERR(CAM_SMMU, "Error: table sgl is null");
  1916. goto err_unmap_sg;
  1917. }
  1918. /* fill up mapping_info */
  1919. *mapping_info = kzalloc(sizeof(struct cam_dma_buff_info), GFP_KERNEL);
  1920. if (!(*mapping_info)) {
  1921. rc = -ENOSPC;
  1922. goto err_alloc;
  1923. }
  1924. (*mapping_info)->buf = buf;
  1925. (*mapping_info)->attach = attach;
  1926. (*mapping_info)->table = table;
  1927. (*mapping_info)->paddr = *paddr_ptr;
  1928. (*mapping_info)->len = *len_ptr;
  1929. (*mapping_info)->dir = dma_dir;
  1930. (*mapping_info)->ref_count = 1;
  1931. (*mapping_info)->region_id = region_id;
  1932. if (!*paddr_ptr || !*len_ptr) {
  1933. CAM_ERR(CAM_SMMU, "Error: Space Allocation failed");
  1934. kfree(*mapping_info);
  1935. *mapping_info = NULL;
  1936. rc = -ENOSPC;
  1937. goto err_alloc;
  1938. }
  1939. CAM_DBG(CAM_SMMU, "idx=%d, dma_buf=%pK, dev=%pOFfp, paddr=0x%llx, len=%zu",
  1940. idx, buf,
  1941. iommu_cb_set.cb_info[idx].dev->of_node,
  1942. *paddr_ptr, *len_ptr);
  1943. /* Unmap the mapping in dma region as this is not used anyway */
  1944. if (region_id == CAM_SMMU_REGION_SHARED)
  1945. dma_buf_unmap_attachment(attach, table, dma_dir);
  1946. return 0;
  1947. err_alloc:
  1948. if (region_id == CAM_SMMU_REGION_SHARED) {
  1949. cam_smmu_free_iova(iova,
  1950. size,
  1951. iommu_cb_set.cb_info[idx].handle);
  1952. iommu_unmap(iommu_cb_set.cb_info[idx].domain,
  1953. *paddr_ptr,
  1954. *len_ptr);
  1955. }
  1956. err_unmap_sg:
  1957. dma_buf_unmap_attachment(attach, table, dma_dir);
  1958. err_detach:
  1959. dma_buf_detach(buf, attach);
  1960. err_put:
  1961. dma_buf_put(buf);
  1962. err_out:
  1963. return rc;
  1964. }
  1965. static int cam_smmu_map_buffer_and_add_to_list(int idx, int ion_fd,
  1966. bool dis_delayed_unmap, enum dma_data_direction dma_dir,
  1967. dma_addr_t *paddr_ptr, size_t *len_ptr,
  1968. enum cam_smmu_region_id region_id, bool is_internal)
  1969. {
  1970. int rc = -1;
  1971. struct cam_dma_buff_info *mapping_info = NULL;
  1972. struct dma_buf *buf = NULL;
  1973. /* returns the dma_buf structure related to an fd */
  1974. buf = dma_buf_get(ion_fd);
  1975. rc = cam_smmu_map_buffer_validate(buf, idx, dma_dir, paddr_ptr, len_ptr,
  1976. region_id, dis_delayed_unmap, &mapping_info);
  1977. if (rc) {
  1978. CAM_ERR(CAM_SMMU, "buffer validation failure");
  1979. return rc;
  1980. }
  1981. mapping_info->ion_fd = ion_fd;
  1982. mapping_info->i_ino = file_inode(buf->file)->i_ino;
  1983. mapping_info->is_internal = is_internal;
  1984. CAM_GET_TIMESTAMP(mapping_info->ts);
  1985. /* add to the list */
  1986. list_add(&mapping_info->list,
  1987. &iommu_cb_set.cb_info[idx].smmu_buf_list);
  1988. CAM_DBG(CAM_SMMU, "fd %d i_ino %lu dmabuf %pK", ion_fd, mapping_info->i_ino, buf);
  1989. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], true,
  1990. mapping_info);
  1991. return 0;
  1992. }
  1993. static int cam_smmu_map_kernel_buffer_and_add_to_list(int idx,
  1994. struct dma_buf *buf, enum dma_data_direction dma_dir,
  1995. dma_addr_t *paddr_ptr, size_t *len_ptr,
  1996. enum cam_smmu_region_id region_id)
  1997. {
  1998. int rc = -1;
  1999. struct cam_dma_buff_info *mapping_info = NULL;
  2000. rc = cam_smmu_map_buffer_validate(buf, idx, dma_dir, paddr_ptr, len_ptr,
  2001. region_id, false, &mapping_info);
  2002. if (rc) {
  2003. CAM_ERR(CAM_SMMU, "buffer validation failure");
  2004. return rc;
  2005. }
  2006. mapping_info->ion_fd = -1;
  2007. mapping_info->i_ino = file_inode(buf->file)->i_ino;
  2008. CAM_GET_TIMESTAMP(mapping_info->ts);
  2009. /* add to the list */
  2010. list_add(&mapping_info->list,
  2011. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list);
  2012. CAM_DBG(CAM_SMMU, "fd %d i_ino %lu dmabuf %pK",
  2013. mapping_info->ion_fd, mapping_info->i_ino, buf);
  2014. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], true,
  2015. mapping_info);
  2016. return 0;
  2017. }
  2018. static int cam_smmu_unmap_buf_and_remove_from_list(
  2019. struct cam_dma_buff_info *mapping_info,
  2020. int idx)
  2021. {
  2022. int rc;
  2023. size_t size;
  2024. struct iommu_domain *domain;
  2025. struct timespec64 ts1, ts2;
  2026. long microsec = 0;
  2027. if ((!mapping_info->buf) || (!mapping_info->table) ||
  2028. (!mapping_info->attach)) {
  2029. CAM_ERR(CAM_SMMU,
  2030. "Error: Invalid params dev = %pK, table = %pK",
  2031. (void *)iommu_cb_set.cb_info[idx].dev,
  2032. (void *)mapping_info->table);
  2033. CAM_ERR(CAM_SMMU, "Error:dma_buf = %pK, attach = %pK",
  2034. (void *)mapping_info->buf,
  2035. (void *)mapping_info->attach);
  2036. return -EINVAL;
  2037. }
  2038. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], false,
  2039. mapping_info);
  2040. CAM_DBG(CAM_SMMU,
  2041. "region_id=%d, paddr=0x%llx, len=%d, dma_map_attrs=%d",
  2042. mapping_info->region_id, mapping_info->paddr, mapping_info->len,
  2043. mapping_info->attach->dma_map_attrs);
  2044. if (iommu_cb_set.map_profile_enable)
  2045. CAM_GET_TIMESTAMP(ts1);
  2046. if (mapping_info->region_id == CAM_SMMU_REGION_SHARED) {
  2047. CAM_DBG(CAM_SMMU,
  2048. "Removing SHARED buffer paddr = 0x%llx, len = %zu",
  2049. mapping_info->paddr, mapping_info->len);
  2050. domain = iommu_cb_set.cb_info[idx].domain;
  2051. size = iommu_unmap(domain,
  2052. mapping_info->paddr,
  2053. mapping_info->len);
  2054. if (size != mapping_info->len) {
  2055. CAM_ERR(CAM_SMMU, "IOMMU unmap failed");
  2056. CAM_ERR(CAM_SMMU, "Unmapped = %zu, requested = %zu",
  2057. size,
  2058. mapping_info->len);
  2059. }
  2060. rc = cam_smmu_free_iova(mapping_info->paddr,
  2061. mapping_info->len,
  2062. iommu_cb_set.cb_info[idx].handle);
  2063. if (rc)
  2064. CAM_ERR(CAM_SMMU, "IOVA free failed");
  2065. iommu_cb_set.cb_info[idx].shared_mapping_size -=
  2066. mapping_info->len;
  2067. } else if (mapping_info->region_id == CAM_SMMU_REGION_IO) {
  2068. if (mapping_info->is_internal)
  2069. mapping_info->attach->dma_map_attrs |=
  2070. DMA_ATTR_SKIP_CPU_SYNC;
  2071. dma_buf_unmap_attachment(mapping_info->attach,
  2072. mapping_info->table, mapping_info->dir);
  2073. iommu_cb_set.cb_info[idx].io_mapping_size -= mapping_info->len;
  2074. }
  2075. dma_buf_detach(mapping_info->buf, mapping_info->attach);
  2076. dma_buf_put(mapping_info->buf);
  2077. if (iommu_cb_set.map_profile_enable) {
  2078. CAM_GET_TIMESTAMP(ts2);
  2079. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  2080. trace_cam_log_event("SMMUUnmapProfile",
  2081. "size and time in micro", mapping_info->len, microsec);
  2082. }
  2083. mapping_info->buf = NULL;
  2084. list_del_init(&mapping_info->list);
  2085. /* free one buffer */
  2086. kfree(mapping_info);
  2087. return 0;
  2088. }
  2089. static enum cam_smmu_buf_state cam_smmu_check_fd_in_list(int idx,
  2090. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2091. struct timespec64 **ts_mapping)
  2092. {
  2093. struct cam_dma_buff_info *mapping;
  2094. unsigned long i_ino;
  2095. i_ino = file_inode(dmabuf->file)->i_ino;
  2096. list_for_each_entry(mapping,
  2097. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  2098. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2099. *paddr_ptr = mapping->paddr;
  2100. *len_ptr = mapping->len;
  2101. *ts_mapping = &mapping->ts;
  2102. return CAM_SMMU_BUFF_EXIST;
  2103. }
  2104. }
  2105. return CAM_SMMU_BUFF_NOT_EXIST;
  2106. }
  2107. static enum cam_smmu_buf_state cam_smmu_user_reuse_fd_in_list(int idx,
  2108. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2109. struct timespec64 **ts_mapping)
  2110. {
  2111. struct cam_dma_buff_info *mapping;
  2112. unsigned long i_ino;
  2113. i_ino = file_inode(dmabuf->file)->i_ino;
  2114. list_for_each_entry(mapping,
  2115. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  2116. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2117. *paddr_ptr = mapping->paddr;
  2118. *len_ptr = mapping->len;
  2119. *ts_mapping = &mapping->ts;
  2120. mapping->ref_count++;
  2121. return CAM_SMMU_BUFF_EXIST;
  2122. }
  2123. }
  2124. return CAM_SMMU_BUFF_NOT_EXIST;
  2125. }
  2126. static enum cam_smmu_buf_state cam_smmu_check_dma_buf_in_list(int idx,
  2127. struct dma_buf *buf, dma_addr_t *paddr_ptr, size_t *len_ptr)
  2128. {
  2129. struct cam_dma_buff_info *mapping;
  2130. list_for_each_entry(mapping,
  2131. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  2132. if (mapping->buf == buf) {
  2133. *paddr_ptr = mapping->paddr;
  2134. *len_ptr = mapping->len;
  2135. return CAM_SMMU_BUFF_EXIST;
  2136. }
  2137. }
  2138. return CAM_SMMU_BUFF_NOT_EXIST;
  2139. }
  2140. static enum cam_smmu_buf_state cam_smmu_check_secure_fd_in_list(int idx,
  2141. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr)
  2142. {
  2143. struct cam_sec_buff_info *mapping;
  2144. unsigned long i_ino;
  2145. i_ino = file_inode(dmabuf->file)->i_ino;
  2146. list_for_each_entry(mapping,
  2147. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  2148. list) {
  2149. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2150. *paddr_ptr = mapping->paddr;
  2151. *len_ptr = mapping->len;
  2152. mapping->ref_count++;
  2153. return CAM_SMMU_BUFF_EXIST;
  2154. }
  2155. }
  2156. return CAM_SMMU_BUFF_NOT_EXIST;
  2157. }
  2158. static enum cam_smmu_buf_state cam_smmu_validate_secure_fd_in_list(int idx,
  2159. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr)
  2160. {
  2161. struct cam_sec_buff_info *mapping;
  2162. unsigned long i_ino;
  2163. i_ino = file_inode(dmabuf->file)->i_ino;
  2164. list_for_each_entry(mapping,
  2165. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  2166. list) {
  2167. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2168. *paddr_ptr = mapping->paddr;
  2169. *len_ptr = mapping->len;
  2170. return CAM_SMMU_BUFF_EXIST;
  2171. }
  2172. }
  2173. return CAM_SMMU_BUFF_NOT_EXIST;
  2174. }
  2175. int cam_smmu_get_handle(char *identifier, int *handle_ptr)
  2176. {
  2177. int rc = 0;
  2178. if (!identifier) {
  2179. CAM_ERR(CAM_SMMU, "Error: iommu hardware name is NULL");
  2180. return -EINVAL;
  2181. }
  2182. if (!handle_ptr) {
  2183. CAM_ERR(CAM_SMMU, "Error: handle pointer is NULL");
  2184. return -EINVAL;
  2185. }
  2186. /* create and put handle in the table */
  2187. rc = cam_smmu_create_add_handle_in_table(identifier, handle_ptr);
  2188. if (rc < 0)
  2189. CAM_ERR(CAM_SMMU, "Error: %s get handle fail, rc %d",
  2190. identifier, rc);
  2191. return rc;
  2192. }
  2193. EXPORT_SYMBOL(cam_smmu_get_handle);
  2194. int cam_smmu_ops(int handle, enum cam_smmu_ops_param ops)
  2195. {
  2196. int ret = 0, idx;
  2197. if (handle == HANDLE_INIT) {
  2198. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2199. return -EINVAL;
  2200. }
  2201. idx = GET_SMMU_TABLE_IDX(handle);
  2202. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2203. CAM_ERR(CAM_SMMU, "Error: Index invalid. idx = %d hdl = %x",
  2204. idx, handle);
  2205. return -EINVAL;
  2206. }
  2207. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2208. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2209. CAM_ERR(CAM_SMMU,
  2210. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2211. iommu_cb_set.cb_info[idx].handle, handle);
  2212. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2213. return -EINVAL;
  2214. }
  2215. switch (ops) {
  2216. case CAM_SMMU_ATTACH: {
  2217. ret = cam_smmu_attach(idx);
  2218. break;
  2219. }
  2220. case CAM_SMMU_DETACH: {
  2221. ret = cam_smmu_detach_device(idx);
  2222. break;
  2223. }
  2224. case CAM_SMMU_VOTE:
  2225. case CAM_SMMU_DEVOTE:
  2226. default:
  2227. CAM_ERR(CAM_SMMU, "Error: idx = %d, ops = %d", idx, ops);
  2228. ret = -EINVAL;
  2229. }
  2230. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2231. return ret;
  2232. }
  2233. EXPORT_SYMBOL(cam_smmu_ops);
  2234. static int cam_smmu_alloc_scratch_buffer_add_to_list(int idx,
  2235. size_t virt_len,
  2236. size_t phys_len,
  2237. unsigned int iommu_dir,
  2238. dma_addr_t *virt_addr)
  2239. {
  2240. unsigned long nents = virt_len / phys_len;
  2241. struct cam_dma_buff_info *mapping_info = NULL;
  2242. size_t unmapped;
  2243. dma_addr_t iova = 0;
  2244. struct scatterlist *sg;
  2245. int i = 0;
  2246. int rc;
  2247. struct iommu_domain *domain = NULL;
  2248. struct page *page;
  2249. struct sg_table *table = NULL;
  2250. CAM_DBG(CAM_SMMU, "nents = %lu, idx = %d, virt_len = %zx",
  2251. nents, idx, virt_len);
  2252. CAM_DBG(CAM_SMMU, "phys_len = %zx, iommu_dir = %d, virt_addr = %pK",
  2253. phys_len, iommu_dir, virt_addr);
  2254. /*
  2255. * This table will go inside the 'mapping' structure
  2256. * where it will be held until put_scratch_buffer is called
  2257. */
  2258. table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
  2259. if (!table) {
  2260. rc = -ENOMEM;
  2261. goto err_table_alloc;
  2262. }
  2263. rc = sg_alloc_table(table, nents, GFP_KERNEL);
  2264. if (rc < 0) {
  2265. rc = -EINVAL;
  2266. goto err_sg_alloc;
  2267. }
  2268. page = alloc_pages(GFP_KERNEL, get_order(phys_len));
  2269. if (!page) {
  2270. rc = -ENOMEM;
  2271. goto err_page_alloc;
  2272. }
  2273. /* Now we create the sg list */
  2274. for_each_sg(table->sgl, sg, table->nents, i)
  2275. sg_set_page(sg, page, phys_len, 0);
  2276. /* Get the domain from within our cb_set struct and map it*/
  2277. domain = iommu_cb_set.cb_info[idx].domain;
  2278. rc = cam_smmu_alloc_scratch_va(&iommu_cb_set.cb_info[idx].scratch_map,
  2279. virt_len, &iova);
  2280. if (rc < 0) {
  2281. CAM_ERR(CAM_SMMU,
  2282. "Could not find valid iova for scratch buffer");
  2283. goto err_iommu_map;
  2284. }
  2285. if (iommu_cb_set.force_cache_allocs)
  2286. iommu_dir |= IOMMU_CACHE;
  2287. if (iommu_map_sg(domain,
  2288. iova,
  2289. table->sgl,
  2290. table->nents,
  2291. iommu_dir) != virt_len) {
  2292. CAM_ERR(CAM_SMMU, "iommu_map_sg() failed");
  2293. goto err_iommu_map;
  2294. }
  2295. /* Now update our mapping information within the cb_set struct */
  2296. mapping_info = kzalloc(sizeof(struct cam_dma_buff_info), GFP_KERNEL);
  2297. if (!mapping_info) {
  2298. rc = -ENOMEM;
  2299. goto err_mapping_info;
  2300. }
  2301. mapping_info->ion_fd = 0xDEADBEEF;
  2302. mapping_info->i_ino = 0;
  2303. mapping_info->buf = NULL;
  2304. mapping_info->attach = NULL;
  2305. mapping_info->table = table;
  2306. mapping_info->paddr = iova;
  2307. mapping_info->len = virt_len;
  2308. mapping_info->iommu_dir = iommu_dir;
  2309. mapping_info->ref_count = 1;
  2310. mapping_info->phys_len = phys_len;
  2311. mapping_info->region_id = CAM_SMMU_REGION_SCRATCH;
  2312. CAM_DBG(CAM_SMMU, "paddr = %pK, len = %zx, phys_len = %zx",
  2313. (void *)mapping_info->paddr,
  2314. mapping_info->len, mapping_info->phys_len);
  2315. list_add(&mapping_info->list, &iommu_cb_set.cb_info[idx].smmu_buf_list);
  2316. *virt_addr = (dma_addr_t)iova;
  2317. CAM_DBG(CAM_SMMU, "mapped virtual address = %lx",
  2318. (unsigned long)*virt_addr);
  2319. return 0;
  2320. err_mapping_info:
  2321. unmapped = iommu_unmap(domain, iova, virt_len);
  2322. if (unmapped != virt_len)
  2323. CAM_ERR(CAM_SMMU, "Unmapped only %zx instead of %zx",
  2324. unmapped, virt_len);
  2325. err_iommu_map:
  2326. __free_pages(page, get_order(phys_len));
  2327. err_page_alloc:
  2328. sg_free_table(table);
  2329. err_sg_alloc:
  2330. kfree(table);
  2331. err_table_alloc:
  2332. return rc;
  2333. }
  2334. static int cam_smmu_free_scratch_buffer_remove_from_list(
  2335. struct cam_dma_buff_info *mapping_info,
  2336. int idx)
  2337. {
  2338. int rc = 0;
  2339. size_t unmapped;
  2340. struct iommu_domain *domain =
  2341. iommu_cb_set.cb_info[idx].domain;
  2342. struct scratch_mapping *scratch_map =
  2343. &iommu_cb_set.cb_info[idx].scratch_map;
  2344. if (!mapping_info->table) {
  2345. CAM_ERR(CAM_SMMU,
  2346. "Error: Invalid params: dev = %pK, table = %pK",
  2347. (void *)iommu_cb_set.cb_info[idx].dev,
  2348. (void *)mapping_info->table);
  2349. return -EINVAL;
  2350. }
  2351. /* Clean up the mapping_info struct from the list */
  2352. unmapped = iommu_unmap(domain, mapping_info->paddr, mapping_info->len);
  2353. if (unmapped != mapping_info->len)
  2354. CAM_ERR(CAM_SMMU, "Unmapped only %zx instead of %zx",
  2355. unmapped, mapping_info->len);
  2356. rc = cam_smmu_free_scratch_va(scratch_map,
  2357. mapping_info->paddr,
  2358. mapping_info->len);
  2359. if (rc < 0) {
  2360. CAM_ERR(CAM_SMMU,
  2361. "Error: Invalid iova while freeing scratch buffer");
  2362. rc = -EINVAL;
  2363. }
  2364. __free_pages(sg_page(mapping_info->table->sgl),
  2365. get_order(mapping_info->phys_len));
  2366. sg_free_table(mapping_info->table);
  2367. kfree(mapping_info->table);
  2368. list_del_init(&mapping_info->list);
  2369. kfree(mapping_info);
  2370. mapping_info = NULL;
  2371. return rc;
  2372. }
  2373. int cam_smmu_get_scratch_iova(int handle,
  2374. enum cam_smmu_map_dir dir,
  2375. dma_addr_t *paddr_ptr,
  2376. size_t virt_len,
  2377. size_t phys_len)
  2378. {
  2379. int idx, rc;
  2380. unsigned int iommu_dir;
  2381. if (!paddr_ptr || !virt_len || !phys_len) {
  2382. CAM_ERR(CAM_SMMU, "Error: Input pointer or lengths invalid");
  2383. return -EINVAL;
  2384. }
  2385. if (virt_len < phys_len) {
  2386. CAM_ERR(CAM_SMMU, "Error: virt_len > phys_len");
  2387. return -EINVAL;
  2388. }
  2389. if (handle == HANDLE_INIT) {
  2390. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2391. return -EINVAL;
  2392. }
  2393. iommu_dir = cam_smmu_translate_dir_to_iommu_dir(dir);
  2394. if (iommu_dir == IOMMU_INVALID_DIR) {
  2395. CAM_ERR(CAM_SMMU,
  2396. "Error: translate direction failed. dir = %d", dir);
  2397. return -EINVAL;
  2398. }
  2399. idx = GET_SMMU_TABLE_IDX(handle);
  2400. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2401. CAM_ERR(CAM_SMMU,
  2402. "Error: handle or index invalid. idx = %d hdl = %x",
  2403. idx, handle);
  2404. return -EINVAL;
  2405. }
  2406. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2407. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2408. CAM_ERR(CAM_SMMU,
  2409. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2410. iommu_cb_set.cb_info[idx].handle, handle);
  2411. rc = -EINVAL;
  2412. goto error;
  2413. }
  2414. if (!iommu_cb_set.cb_info[idx].scratch_buf_support) {
  2415. CAM_ERR(CAM_SMMU,
  2416. "Error: Context bank does not support scratch bufs");
  2417. rc = -EINVAL;
  2418. goto error;
  2419. }
  2420. CAM_DBG(CAM_SMMU, "smmu handle = %x, idx = %d, dir = %d",
  2421. handle, idx, dir);
  2422. CAM_DBG(CAM_SMMU, "virt_len = %zx, phys_len = %zx",
  2423. phys_len, virt_len);
  2424. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  2425. CAM_ERR(CAM_SMMU,
  2426. "Err:Dev %s should call SMMU attach before map buffer",
  2427. iommu_cb_set.cb_info[idx].name[0]);
  2428. rc = -EINVAL;
  2429. goto error;
  2430. }
  2431. if (!IS_ALIGNED(virt_len, PAGE_SIZE)) {
  2432. CAM_ERR(CAM_SMMU,
  2433. "Requested scratch buffer length not page aligned");
  2434. rc = -EINVAL;
  2435. goto error;
  2436. }
  2437. if (!IS_ALIGNED(virt_len, phys_len)) {
  2438. CAM_ERR(CAM_SMMU,
  2439. "Requested virt length not aligned with phys length");
  2440. rc = -EINVAL;
  2441. goto error;
  2442. }
  2443. rc = cam_smmu_alloc_scratch_buffer_add_to_list(idx,
  2444. virt_len,
  2445. phys_len,
  2446. iommu_dir,
  2447. paddr_ptr);
  2448. if (rc < 0)
  2449. CAM_ERR(CAM_SMMU, "Error: mapping or add list fail");
  2450. error:
  2451. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2452. return rc;
  2453. }
  2454. int cam_smmu_put_scratch_iova(int handle,
  2455. dma_addr_t paddr)
  2456. {
  2457. int idx;
  2458. int rc = -1;
  2459. struct cam_dma_buff_info *mapping_info;
  2460. if (handle == HANDLE_INIT) {
  2461. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2462. return -EINVAL;
  2463. }
  2464. /* find index in the iommu_cb_set.cb_info */
  2465. idx = GET_SMMU_TABLE_IDX(handle);
  2466. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2467. CAM_ERR(CAM_SMMU,
  2468. "Error: handle or index invalid. idx = %d hdl = %x",
  2469. idx, handle);
  2470. return -EINVAL;
  2471. }
  2472. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2473. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2474. CAM_ERR(CAM_SMMU,
  2475. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2476. iommu_cb_set.cb_info[idx].handle, handle);
  2477. rc = -EINVAL;
  2478. goto handle_err;
  2479. }
  2480. if (!iommu_cb_set.cb_info[idx].scratch_buf_support) {
  2481. CAM_ERR(CAM_SMMU,
  2482. "Error: Context bank does not support scratch buffers");
  2483. rc = -EINVAL;
  2484. goto handle_err;
  2485. }
  2486. /* Based on virtual address and index, we can find mapping info
  2487. * of the scratch buffer
  2488. */
  2489. mapping_info = cam_smmu_find_mapping_by_virt_address(idx, paddr);
  2490. if (!mapping_info) {
  2491. CAM_ERR(CAM_SMMU, "Error: Invalid params");
  2492. rc = -ENODEV;
  2493. goto handle_err;
  2494. }
  2495. /* unmapping one buffer from device */
  2496. rc = cam_smmu_free_scratch_buffer_remove_from_list(mapping_info, idx);
  2497. if (rc < 0) {
  2498. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  2499. goto handle_err;
  2500. }
  2501. handle_err:
  2502. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2503. return rc;
  2504. }
  2505. static int cam_smmu_map_stage2_buffer_and_add_to_list(int idx, int ion_fd,
  2506. enum dma_data_direction dma_dir, dma_addr_t *paddr_ptr,
  2507. size_t *len_ptr)
  2508. {
  2509. int rc = 0;
  2510. struct dma_buf *dmabuf = NULL;
  2511. struct dma_buf_attachment *attach = NULL;
  2512. struct sg_table *table = NULL;
  2513. struct cam_sec_buff_info *mapping_info;
  2514. /* clean the content from clients */
  2515. *paddr_ptr = (dma_addr_t)NULL;
  2516. *len_ptr = (size_t)0;
  2517. dmabuf = dma_buf_get(ion_fd);
  2518. if (IS_ERR_OR_NULL((void *)(dmabuf))) {
  2519. CAM_ERR(CAM_SMMU,
  2520. "Error: dma buf get failed, idx=%d, ion_fd=%d",
  2521. idx, ion_fd);
  2522. rc = PTR_ERR(dmabuf);
  2523. goto err_out;
  2524. }
  2525. /*
  2526. * ion_phys() is deprecated. call dma_buf_attach() and
  2527. * dma_buf_map_attachment() to get the buffer's physical
  2528. * address.
  2529. */
  2530. attach = dma_buf_attach(dmabuf, iommu_cb_set.cb_info[idx].dev);
  2531. if (IS_ERR_OR_NULL(attach)) {
  2532. CAM_ERR(CAM_SMMU,
  2533. "Error: dma buf attach failed, idx=%d, ion_fd=%d",
  2534. idx, ion_fd);
  2535. rc = PTR_ERR(attach);
  2536. goto err_put;
  2537. }
  2538. attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  2539. table = dma_buf_map_attachment(attach, dma_dir);
  2540. if (IS_ERR_OR_NULL(table)) {
  2541. CAM_ERR(CAM_SMMU, "Error: dma buf map attachment failed");
  2542. rc = PTR_ERR(table);
  2543. goto err_detach;
  2544. }
  2545. /* return addr and len to client */
  2546. *paddr_ptr = sg_phys(table->sgl);
  2547. *len_ptr = (size_t)sg_dma_len(table->sgl);
  2548. /* fill up mapping_info */
  2549. mapping_info = kzalloc(sizeof(struct cam_sec_buff_info), GFP_KERNEL);
  2550. if (!mapping_info) {
  2551. rc = -ENOMEM;
  2552. goto err_unmap_sg;
  2553. }
  2554. mapping_info->ion_fd = ion_fd;
  2555. mapping_info->i_ino = file_inode(dmabuf->file)->i_ino;
  2556. mapping_info->paddr = *paddr_ptr;
  2557. mapping_info->len = *len_ptr;
  2558. mapping_info->dir = dma_dir;
  2559. mapping_info->ref_count = 1;
  2560. mapping_info->buf = dmabuf;
  2561. mapping_info->attach = attach;
  2562. mapping_info->table = table;
  2563. CAM_DBG(CAM_SMMU, "idx=%d, ion_fd=%d, i_ino=%lu, dev=%pOFfp, paddr=0x%llx, len=%zu",
  2564. idx, ion_fd, mapping_info->i_ino,
  2565. iommu_cb_set.cb_info[idx].dev->of_node,
  2566. *paddr_ptr, *len_ptr);
  2567. /* add to the list */
  2568. list_add(&mapping_info->list, &iommu_cb_set.cb_info[idx].smmu_buf_list);
  2569. return 0;
  2570. err_unmap_sg:
  2571. dma_buf_unmap_attachment(attach, table, dma_dir);
  2572. err_detach:
  2573. dma_buf_detach(dmabuf, attach);
  2574. err_put:
  2575. dma_buf_put(dmabuf);
  2576. err_out:
  2577. return rc;
  2578. }
  2579. int cam_smmu_map_stage2_iova(int handle, int ion_fd, struct dma_buf *dmabuf,
  2580. enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr, size_t *len_ptr)
  2581. {
  2582. int idx, rc;
  2583. enum dma_data_direction dma_dir;
  2584. enum cam_smmu_buf_state buf_state;
  2585. if (!paddr_ptr || !len_ptr) {
  2586. CAM_ERR(CAM_SMMU,
  2587. "Error: Invalid inputs, paddr_ptr:%pK, len_ptr: %pK",
  2588. paddr_ptr, len_ptr);
  2589. return -EINVAL;
  2590. }
  2591. /* clean the content from clients */
  2592. *paddr_ptr = (dma_addr_t)NULL;
  2593. *len_ptr = (size_t)0;
  2594. dma_dir = cam_smmu_translate_dir(dir);
  2595. if (dma_dir == DMA_NONE) {
  2596. CAM_ERR(CAM_SMMU,
  2597. "Error: translate direction failed. dir = %d", dir);
  2598. return -EINVAL;
  2599. }
  2600. idx = GET_SMMU_TABLE_IDX(handle);
  2601. if ((handle == HANDLE_INIT) ||
  2602. (idx < 0) ||
  2603. (idx >= iommu_cb_set.cb_num)) {
  2604. CAM_ERR(CAM_SMMU,
  2605. "Error: handle or index invalid. idx = %d hdl = %x",
  2606. idx, handle);
  2607. return -EINVAL;
  2608. }
  2609. if (!iommu_cb_set.cb_info[idx].is_secure) {
  2610. CAM_ERR(CAM_SMMU,
  2611. "Error: can't map secure mem to non secure cb, idx=%d",
  2612. idx);
  2613. return -EINVAL;
  2614. }
  2615. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2616. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2617. CAM_ERR(CAM_SMMU,
  2618. "Error: hdl is not valid, idx=%d, table_hdl=%x, hdl=%x",
  2619. idx, iommu_cb_set.cb_info[idx].handle, handle);
  2620. rc = -EINVAL;
  2621. goto get_addr_end;
  2622. }
  2623. buf_state = cam_smmu_check_secure_fd_in_list(idx, ion_fd, dmabuf, paddr_ptr,
  2624. len_ptr);
  2625. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  2626. CAM_DBG(CAM_SMMU,
  2627. "fd:%d already in list idx:%d, handle=%d give same addr back",
  2628. ion_fd, idx, handle);
  2629. rc = 0;
  2630. goto get_addr_end;
  2631. }
  2632. rc = cam_smmu_map_stage2_buffer_and_add_to_list(idx, ion_fd, dma_dir,
  2633. paddr_ptr, len_ptr);
  2634. if (rc < 0) {
  2635. CAM_ERR(CAM_SMMU,
  2636. "Error: mapping or add list fail, idx=%d, handle=%d, fd=%d, rc=%d",
  2637. idx, handle, ion_fd, rc);
  2638. goto get_addr_end;
  2639. }
  2640. get_addr_end:
  2641. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2642. return rc;
  2643. }
  2644. EXPORT_SYMBOL(cam_smmu_map_stage2_iova);
  2645. static int cam_smmu_secure_unmap_buf_and_remove_from_list(
  2646. struct cam_sec_buff_info *mapping_info,
  2647. int idx)
  2648. {
  2649. if ((!mapping_info->buf) || (!mapping_info->table) ||
  2650. (!mapping_info->attach)) {
  2651. CAM_ERR(CAM_SMMU, "Error: Invalid params dev = %pK, table = %pK",
  2652. (void *)iommu_cb_set.cb_info[idx].dev,
  2653. (void *)mapping_info->table);
  2654. CAM_ERR(CAM_SMMU, "Error:dma_buf = %pK, attach = %pK\n",
  2655. (void *)mapping_info->buf,
  2656. (void *)mapping_info->attach);
  2657. return -EINVAL;
  2658. }
  2659. /* skip cache operations */
  2660. mapping_info->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  2661. /* iommu buffer clean up */
  2662. dma_buf_unmap_attachment(mapping_info->attach,
  2663. mapping_info->table, mapping_info->dir);
  2664. dma_buf_detach(mapping_info->buf, mapping_info->attach);
  2665. dma_buf_put(mapping_info->buf);
  2666. mapping_info->buf = NULL;
  2667. list_del_init(&mapping_info->list);
  2668. CAM_DBG(CAM_SMMU, "unmap fd: %d, i_ino : %lu, idx : %d",
  2669. mapping_info->ion_fd, mapping_info->i_ino, idx);
  2670. /* free one buffer */
  2671. kfree(mapping_info);
  2672. return 0;
  2673. }
  2674. int cam_smmu_unmap_stage2_iova(int handle, int ion_fd, struct dma_buf *dma_buf)
  2675. {
  2676. int idx, rc;
  2677. struct cam_sec_buff_info *mapping_info;
  2678. /* find index in the iommu_cb_set.cb_info */
  2679. idx = GET_SMMU_TABLE_IDX(handle);
  2680. if ((handle == HANDLE_INIT) ||
  2681. (idx < 0) ||
  2682. (idx >= iommu_cb_set.cb_num)) {
  2683. CAM_ERR(CAM_SMMU,
  2684. "Error: handle or index invalid. idx = %d hdl = %x",
  2685. idx, handle);
  2686. return -EINVAL;
  2687. }
  2688. if (!iommu_cb_set.cb_info[idx].is_secure) {
  2689. CAM_ERR(CAM_SMMU,
  2690. "Error: can't unmap secure mem from non secure cb");
  2691. return -EINVAL;
  2692. }
  2693. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2694. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2695. CAM_ERR(CAM_SMMU,
  2696. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2697. iommu_cb_set.cb_info[idx].handle, handle);
  2698. rc = -EINVAL;
  2699. goto put_addr_end;
  2700. }
  2701. /* based on ion fd and index, we can find mapping info of buffer */
  2702. mapping_info = cam_smmu_find_mapping_by_sec_buf_idx(idx, ion_fd, dma_buf);
  2703. if (!mapping_info) {
  2704. CAM_ERR(CAM_SMMU,
  2705. "Error: Invalid params! idx = %d, fd = %d",
  2706. idx, ion_fd);
  2707. rc = -EINVAL;
  2708. goto put_addr_end;
  2709. }
  2710. mapping_info->ref_count--;
  2711. if (mapping_info->ref_count > 0) {
  2712. CAM_DBG(CAM_SMMU,
  2713. "idx: %d fd = %d ref_count: %d",
  2714. idx, ion_fd, mapping_info->ref_count);
  2715. rc = 0;
  2716. goto put_addr_end;
  2717. }
  2718. mapping_info->ref_count = 0;
  2719. /* unmapping one buffer from device */
  2720. rc = cam_smmu_secure_unmap_buf_and_remove_from_list(mapping_info, idx);
  2721. if (rc) {
  2722. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  2723. goto put_addr_end;
  2724. }
  2725. put_addr_end:
  2726. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2727. return rc;
  2728. }
  2729. EXPORT_SYMBOL(cam_smmu_unmap_stage2_iova);
  2730. static int cam_smmu_map_iova_validate_params(int handle,
  2731. enum cam_smmu_map_dir dir,
  2732. dma_addr_t *paddr_ptr, size_t *len_ptr,
  2733. enum cam_smmu_region_id region_id)
  2734. {
  2735. int idx, rc = 0;
  2736. enum dma_data_direction dma_dir;
  2737. if (!paddr_ptr || !len_ptr) {
  2738. CAM_ERR(CAM_SMMU, "Input pointers are invalid");
  2739. return -EINVAL;
  2740. }
  2741. if (handle == HANDLE_INIT) {
  2742. CAM_ERR(CAM_SMMU, "Invalid handle");
  2743. return -EINVAL;
  2744. }
  2745. /* clean the content from clients */
  2746. *paddr_ptr = (dma_addr_t)NULL;
  2747. if (region_id != CAM_SMMU_REGION_SHARED)
  2748. *len_ptr = (size_t)0;
  2749. dma_dir = cam_smmu_translate_dir(dir);
  2750. if (dma_dir == DMA_NONE) {
  2751. CAM_ERR(CAM_SMMU, "translate direction failed. dir = %d", dir);
  2752. return -EINVAL;
  2753. }
  2754. idx = GET_SMMU_TABLE_IDX(handle);
  2755. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2756. CAM_ERR(CAM_SMMU, "handle or index invalid. idx = %d hdl = %x",
  2757. idx, handle);
  2758. return -EINVAL;
  2759. }
  2760. return rc;
  2761. }
  2762. bool cam_smmu_supports_shared_region(int handle)
  2763. {
  2764. int idx = GET_SMMU_TABLE_IDX(handle);
  2765. bool is_shared;
  2766. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2767. is_shared = (iommu_cb_set.cb_info[idx].shared_support) ? true : false;
  2768. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2769. return is_shared;
  2770. }
  2771. int cam_smmu_map_user_iova(int handle, int ion_fd, struct dma_buf *dmabuf,
  2772. bool dis_delayed_unmap, enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr,
  2773. size_t *len_ptr, enum cam_smmu_region_id region_id,
  2774. bool is_internal)
  2775. {
  2776. int idx, rc = 0;
  2777. struct timespec64 *ts = NULL;
  2778. enum cam_smmu_buf_state buf_state;
  2779. enum dma_data_direction dma_dir;
  2780. rc = cam_smmu_map_iova_validate_params(handle, dir, paddr_ptr,
  2781. len_ptr, region_id);
  2782. if (rc) {
  2783. CAM_ERR(CAM_SMMU, "initial checks failed, unable to proceed");
  2784. return rc;
  2785. }
  2786. dma_dir = (enum dma_data_direction)dir;
  2787. idx = GET_SMMU_TABLE_IDX(handle);
  2788. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2789. if (iommu_cb_set.cb_info[idx].is_secure) {
  2790. CAM_ERR(CAM_SMMU,
  2791. "Error: can't map non-secure mem to secure cb idx=%d",
  2792. idx);
  2793. rc = -EINVAL;
  2794. goto get_addr_end;
  2795. }
  2796. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2797. CAM_ERR(CAM_SMMU,
  2798. "hdl is not valid, idx=%d, table_hdl = %x, hdl = %x",
  2799. idx, iommu_cb_set.cb_info[idx].handle, handle);
  2800. rc = -EINVAL;
  2801. goto get_addr_end;
  2802. }
  2803. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  2804. CAM_ERR(CAM_SMMU,
  2805. "Err:Dev %s should call SMMU attach before map buffer",
  2806. iommu_cb_set.cb_info[idx].name[0]);
  2807. rc = -EINVAL;
  2808. goto get_addr_end;
  2809. }
  2810. buf_state = cam_smmu_user_reuse_fd_in_list(idx, ion_fd, dmabuf, paddr_ptr,
  2811. len_ptr, &ts);
  2812. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  2813. uint64_t ms = 0, hrs = 0, min = 0, sec = 0;
  2814. if (ts)
  2815. CAM_CONVERT_TIMESTAMP_FORMAT((*ts), hrs, min, sec, ms);
  2816. CAM_ERR(CAM_SMMU,
  2817. "fd=%d already in list [%llu:%llu:%lu:%llu] cb=%s idx=%d handle=%d len=%llu,give same addr back",
  2818. ion_fd, hrs, min, sec, ms,
  2819. iommu_cb_set.cb_info[idx].name[0],
  2820. idx, handle, *len_ptr);
  2821. rc = 0;
  2822. goto get_addr_end;
  2823. }
  2824. rc = cam_smmu_map_buffer_and_add_to_list(idx, ion_fd,
  2825. dis_delayed_unmap, dma_dir, paddr_ptr, len_ptr,
  2826. region_id, is_internal);
  2827. if (rc < 0) {
  2828. CAM_ERR(CAM_SMMU,
  2829. "mapping or add list fail cb:%s idx=%d, fd=%d, region=%d, rc=%d",
  2830. iommu_cb_set.cb_info[idx].name[0], idx,
  2831. ion_fd, region_id, rc);
  2832. cam_smmu_dump_cb_info(idx);
  2833. }
  2834. get_addr_end:
  2835. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2836. return rc;
  2837. }
  2838. EXPORT_SYMBOL(cam_smmu_map_user_iova);
  2839. int cam_smmu_map_kernel_iova(int handle, struct dma_buf *buf,
  2840. enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr,
  2841. size_t *len_ptr, enum cam_smmu_region_id region_id)
  2842. {
  2843. int idx, rc = 0;
  2844. enum cam_smmu_buf_state buf_state;
  2845. enum dma_data_direction dma_dir;
  2846. rc = cam_smmu_map_iova_validate_params(handle, dir, paddr_ptr,
  2847. len_ptr, region_id);
  2848. if (rc) {
  2849. CAM_ERR(CAM_SMMU, "initial checks failed, unable to proceed");
  2850. return rc;
  2851. }
  2852. dma_dir = cam_smmu_translate_dir(dir);
  2853. idx = GET_SMMU_TABLE_IDX(handle);
  2854. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2855. if (iommu_cb_set.cb_info[idx].is_secure) {
  2856. CAM_ERR(CAM_SMMU,
  2857. "Error: can't map non-secure mem to secure cb");
  2858. rc = -EINVAL;
  2859. goto get_addr_end;
  2860. }
  2861. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2862. CAM_ERR(CAM_SMMU, "hdl is not valid, table_hdl = %x, hdl = %x",
  2863. iommu_cb_set.cb_info[idx].handle, handle);
  2864. rc = -EINVAL;
  2865. goto get_addr_end;
  2866. }
  2867. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  2868. CAM_ERR(CAM_SMMU,
  2869. "Err:Dev %s should call SMMU attach before map buffer",
  2870. iommu_cb_set.cb_info[idx].name[0]);
  2871. rc = -EINVAL;
  2872. goto get_addr_end;
  2873. }
  2874. buf_state = cam_smmu_check_dma_buf_in_list(idx, buf,
  2875. paddr_ptr, len_ptr);
  2876. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  2877. CAM_ERR(CAM_SMMU,
  2878. "dma_buf :%pK already in the list", buf);
  2879. rc = -EALREADY;
  2880. goto get_addr_end;
  2881. }
  2882. rc = cam_smmu_map_kernel_buffer_and_add_to_list(idx, buf, dma_dir,
  2883. paddr_ptr, len_ptr, region_id);
  2884. if (rc < 0)
  2885. CAM_ERR(CAM_SMMU, "mapping or add list fail");
  2886. get_addr_end:
  2887. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2888. return rc;
  2889. }
  2890. EXPORT_SYMBOL(cam_smmu_map_kernel_iova);
  2891. int cam_smmu_get_iova(int handle, int ion_fd, struct dma_buf *dma_buf,
  2892. dma_addr_t *paddr_ptr, size_t *len_ptr)
  2893. {
  2894. int idx, rc = 0;
  2895. struct timespec64 *ts = NULL;
  2896. enum cam_smmu_buf_state buf_state;
  2897. if (!paddr_ptr || !len_ptr) {
  2898. CAM_ERR(CAM_SMMU, "Error: Input pointers are invalid");
  2899. return -EINVAL;
  2900. }
  2901. if (handle == HANDLE_INIT) {
  2902. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2903. return -EINVAL;
  2904. }
  2905. /* clean the content from clients */
  2906. *paddr_ptr = (dma_addr_t)NULL;
  2907. *len_ptr = (size_t)0;
  2908. idx = GET_SMMU_TABLE_IDX(handle);
  2909. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2910. CAM_ERR(CAM_SMMU,
  2911. "Error: handle or index invalid. idx = %d hdl = %x",
  2912. idx, handle);
  2913. return -EINVAL;
  2914. }
  2915. if (iommu_cb_set.cb_info[idx].is_secure) {
  2916. CAM_ERR(CAM_SMMU,
  2917. "Error: can't get non-secure mem from secure cb");
  2918. return -EINVAL;
  2919. }
  2920. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2921. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2922. CAM_ERR(CAM_SMMU,
  2923. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2924. iommu_cb_set.cb_info[idx].handle, handle);
  2925. rc = -EINVAL;
  2926. goto get_addr_end;
  2927. }
  2928. buf_state = cam_smmu_check_fd_in_list(idx, ion_fd, dma_buf, paddr_ptr,
  2929. len_ptr, &ts);
  2930. if (buf_state == CAM_SMMU_BUFF_NOT_EXIST) {
  2931. CAM_ERR(CAM_SMMU, "ion_fd:%d not in the mapped list", ion_fd);
  2932. rc = -EINVAL;
  2933. cam_smmu_dump_cb_info(idx);
  2934. goto get_addr_end;
  2935. }
  2936. get_addr_end:
  2937. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2938. return rc;
  2939. }
  2940. EXPORT_SYMBOL(cam_smmu_get_iova);
  2941. int cam_smmu_get_stage2_iova(int handle, int ion_fd, struct dma_buf *dma_buf,
  2942. dma_addr_t *paddr_ptr, size_t *len_ptr)
  2943. {
  2944. int idx, rc = 0;
  2945. enum cam_smmu_buf_state buf_state;
  2946. if (!paddr_ptr || !len_ptr) {
  2947. CAM_ERR(CAM_SMMU, "Error: Input pointers are invalid");
  2948. return -EINVAL;
  2949. }
  2950. if (handle == HANDLE_INIT) {
  2951. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2952. return -EINVAL;
  2953. }
  2954. /* clean the content from clients */
  2955. *paddr_ptr = (dma_addr_t)NULL;
  2956. *len_ptr = (size_t)0;
  2957. idx = GET_SMMU_TABLE_IDX(handle);
  2958. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2959. CAM_ERR(CAM_SMMU,
  2960. "Error: handle or index invalid. idx = %d hdl = %x",
  2961. idx, handle);
  2962. return -EINVAL;
  2963. }
  2964. if (!iommu_cb_set.cb_info[idx].is_secure) {
  2965. CAM_ERR(CAM_SMMU,
  2966. "Error: can't get secure mem from non secure cb");
  2967. return -EINVAL;
  2968. }
  2969. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2970. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2971. CAM_ERR(CAM_SMMU,
  2972. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2973. iommu_cb_set.cb_info[idx].handle, handle);
  2974. rc = -EINVAL;
  2975. goto get_addr_end;
  2976. }
  2977. buf_state = cam_smmu_validate_secure_fd_in_list(idx, ion_fd, dma_buf, paddr_ptr, len_ptr);
  2978. if (buf_state == CAM_SMMU_BUFF_NOT_EXIST) {
  2979. CAM_ERR(CAM_SMMU, "ion_fd:%d not in the mapped list", ion_fd);
  2980. rc = -EINVAL;
  2981. goto get_addr_end;
  2982. }
  2983. get_addr_end:
  2984. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2985. return rc;
  2986. }
  2987. EXPORT_SYMBOL(cam_smmu_get_stage2_iova);
  2988. static int cam_smmu_unmap_validate_params(int handle)
  2989. {
  2990. int idx;
  2991. if (handle == HANDLE_INIT) {
  2992. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2993. return -EINVAL;
  2994. }
  2995. /* find index in the iommu_cb_set.cb_info */
  2996. idx = GET_SMMU_TABLE_IDX(handle);
  2997. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2998. CAM_ERR(CAM_SMMU,
  2999. "Error: handle or index invalid. idx = %d hdl = %x",
  3000. idx, handle);
  3001. return -EINVAL;
  3002. }
  3003. return 0;
  3004. }
  3005. int cam_smmu_unmap_user_iova(int handle,
  3006. int ion_fd, struct dma_buf *dma_buf, enum cam_smmu_region_id region_id)
  3007. {
  3008. int idx, rc;
  3009. struct cam_dma_buff_info *mapping_info;
  3010. rc = cam_smmu_unmap_validate_params(handle);
  3011. if (rc) {
  3012. CAM_ERR(CAM_SMMU, "unmap util validation failure");
  3013. return rc;
  3014. }
  3015. idx = GET_SMMU_TABLE_IDX(handle);
  3016. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3017. if (iommu_cb_set.cb_info[idx].is_secure) {
  3018. CAM_ERR(CAM_SMMU,
  3019. "Error: can't unmap non-secure mem from secure cb");
  3020. rc = -EINVAL;
  3021. goto unmap_end;
  3022. }
  3023. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3024. CAM_ERR(CAM_SMMU,
  3025. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3026. iommu_cb_set.cb_info[idx].handle, handle);
  3027. rc = -EINVAL;
  3028. goto unmap_end;
  3029. }
  3030. /* Based on ion_fd & index, we can find mapping info of buffer */
  3031. mapping_info = cam_smmu_find_mapping_by_ion_index(idx, ion_fd, dma_buf);
  3032. if (!mapping_info) {
  3033. CAM_ERR(CAM_SMMU,
  3034. "Error: Invalid params idx = %d, fd = %d",
  3035. idx, ion_fd);
  3036. rc = -EINVAL;
  3037. goto unmap_end;
  3038. }
  3039. mapping_info->ref_count--;
  3040. if (mapping_info->ref_count > 0) {
  3041. CAM_DBG(CAM_SMMU,
  3042. "idx: %d fd = %d ref_count: %d",
  3043. idx, ion_fd, mapping_info->ref_count);
  3044. rc = 0;
  3045. goto unmap_end;
  3046. }
  3047. mapping_info->ref_count = 0;
  3048. /* Unmapping one buffer from device */
  3049. CAM_DBG(CAM_SMMU, "SMMU: removing buffer idx = %d", idx);
  3050. rc = cam_smmu_unmap_buf_and_remove_from_list(mapping_info, idx);
  3051. if (rc < 0)
  3052. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  3053. unmap_end:
  3054. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3055. return rc;
  3056. }
  3057. EXPORT_SYMBOL(cam_smmu_unmap_user_iova);
  3058. int cam_smmu_unmap_kernel_iova(int handle,
  3059. struct dma_buf *buf, enum cam_smmu_region_id region_id)
  3060. {
  3061. int idx, rc;
  3062. struct cam_dma_buff_info *mapping_info;
  3063. rc = cam_smmu_unmap_validate_params(handle);
  3064. if (rc) {
  3065. CAM_ERR(CAM_SMMU, "unmap util validation failure");
  3066. return rc;
  3067. }
  3068. idx = GET_SMMU_TABLE_IDX(handle);
  3069. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3070. if (iommu_cb_set.cb_info[idx].is_secure) {
  3071. CAM_ERR(CAM_SMMU,
  3072. "Error: can't unmap non-secure mem from secure cb");
  3073. rc = -EINVAL;
  3074. goto unmap_end;
  3075. }
  3076. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3077. CAM_ERR(CAM_SMMU,
  3078. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3079. iommu_cb_set.cb_info[idx].handle, handle);
  3080. rc = -EINVAL;
  3081. goto unmap_end;
  3082. }
  3083. /* Based on dma_buf & index, we can find mapping info of buffer */
  3084. mapping_info = cam_smmu_find_mapping_by_dma_buf(idx, buf);
  3085. if (!mapping_info) {
  3086. CAM_ERR(CAM_SMMU,
  3087. "Error: Invalid params idx = %d, dma_buf = %pK",
  3088. idx, buf);
  3089. rc = -EINVAL;
  3090. goto unmap_end;
  3091. }
  3092. /* Unmapping one buffer from device */
  3093. CAM_DBG(CAM_SMMU, "SMMU: removing buffer idx = %d", idx);
  3094. rc = cam_smmu_unmap_buf_and_remove_from_list(mapping_info, idx);
  3095. if (rc < 0)
  3096. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  3097. unmap_end:
  3098. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3099. return rc;
  3100. }
  3101. EXPORT_SYMBOL(cam_smmu_unmap_kernel_iova);
  3102. int cam_smmu_put_iova(int handle, int ion_fd, struct dma_buf *dma_buf)
  3103. {
  3104. int idx;
  3105. int rc = 0;
  3106. struct cam_dma_buff_info *mapping_info;
  3107. if (handle == HANDLE_INIT) {
  3108. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3109. return -EINVAL;
  3110. }
  3111. /* find index in the iommu_cb_set.cb_info */
  3112. idx = GET_SMMU_TABLE_IDX(handle);
  3113. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3114. CAM_ERR(CAM_SMMU,
  3115. "Error: handle or index invalid. idx = %d hdl = %x",
  3116. idx, handle);
  3117. return -EINVAL;
  3118. }
  3119. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3120. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3121. CAM_ERR(CAM_SMMU,
  3122. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3123. iommu_cb_set.cb_info[idx].handle, handle);
  3124. rc = -EINVAL;
  3125. goto put_addr_end;
  3126. }
  3127. /* based on ion fd and index, we can find mapping info of buffer */
  3128. mapping_info = cam_smmu_find_mapping_by_ion_index(idx, ion_fd, dma_buf);
  3129. if (!mapping_info) {
  3130. CAM_ERR(CAM_SMMU, "Error: Invalid params idx = %d, fd = %d",
  3131. idx, ion_fd);
  3132. rc = -EINVAL;
  3133. goto put_addr_end;
  3134. }
  3135. put_addr_end:
  3136. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3137. return rc;
  3138. }
  3139. EXPORT_SYMBOL(cam_smmu_put_iova);
  3140. int cam_smmu_destroy_handle(int handle)
  3141. {
  3142. int idx;
  3143. if (handle == HANDLE_INIT) {
  3144. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3145. return -EINVAL;
  3146. }
  3147. idx = GET_SMMU_TABLE_IDX(handle);
  3148. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3149. CAM_ERR(CAM_SMMU,
  3150. "Error: handle or index invalid. idx = %d hdl = %x",
  3151. idx, handle);
  3152. return -EINVAL;
  3153. }
  3154. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3155. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3156. CAM_ERR(CAM_SMMU,
  3157. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3158. iommu_cb_set.cb_info[idx].handle, handle);
  3159. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3160. return -EINVAL;
  3161. }
  3162. if (!list_empty_careful(&iommu_cb_set.cb_info[idx].smmu_buf_list)) {
  3163. CAM_ERR(CAM_SMMU, "UMD %s buffer list is not clean",
  3164. iommu_cb_set.cb_info[idx].name[0]);
  3165. cam_smmu_print_user_list(idx);
  3166. cam_smmu_clean_user_buffer_list(idx);
  3167. }
  3168. if (!list_empty_careful(
  3169. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list)) {
  3170. CAM_ERR(CAM_SMMU, "KMD %s buffer list is not clean",
  3171. iommu_cb_set.cb_info[idx].name[0]);
  3172. cam_smmu_print_kernel_list(idx);
  3173. cam_smmu_clean_kernel_buffer_list(idx);
  3174. }
  3175. if (iommu_cb_set.cb_info[idx].is_secure) {
  3176. if (iommu_cb_set.cb_info[idx].secure_count == 0) {
  3177. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3178. return -EPERM;
  3179. }
  3180. iommu_cb_set.cb_info[idx].secure_count--;
  3181. if (iommu_cb_set.cb_info[idx].secure_count == 0) {
  3182. iommu_cb_set.cb_info[idx].cb_count = 0;
  3183. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3184. }
  3185. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3186. return 0;
  3187. }
  3188. if (iommu_cb_set.cb_info[idx].is_mul_client &&
  3189. iommu_cb_set.cb_info[idx].device_count) {
  3190. iommu_cb_set.cb_info[idx].device_count--;
  3191. if (!iommu_cb_set.cb_info[idx].device_count) {
  3192. iommu_cb_set.cb_info[idx].cb_count = 0;
  3193. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3194. }
  3195. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3196. return 0;
  3197. }
  3198. iommu_cb_set.cb_info[idx].device_count = 0;
  3199. iommu_cb_set.cb_info[idx].cb_count = 0;
  3200. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3201. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3202. return 0;
  3203. }
  3204. EXPORT_SYMBOL(cam_smmu_destroy_handle);
  3205. static void cam_smmu_deinit_cb(struct cam_context_bank_info *cb)
  3206. {
  3207. if (cb->io_support && cb->domain)
  3208. cb->domain = NULL;
  3209. if (cb->shared_support) {
  3210. gen_pool_destroy(cb->shared_mem_pool);
  3211. cb->shared_mem_pool = NULL;
  3212. }
  3213. if (cb->scratch_buf_support) {
  3214. kfree(cb->scratch_map.bitmap);
  3215. cb->scratch_map.bitmap = NULL;
  3216. }
  3217. }
  3218. static void cam_smmu_release_cb(struct platform_device *pdev)
  3219. {
  3220. int i = 0;
  3221. for (i = 0; i < iommu_cb_set.cb_num; i++)
  3222. cam_smmu_deinit_cb(&iommu_cb_set.cb_info[i]);
  3223. devm_kfree(&pdev->dev, iommu_cb_set.cb_info);
  3224. iommu_cb_set.cb_num = 0;
  3225. }
  3226. static int cam_smmu_setup_cb(struct cam_context_bank_info *cb,
  3227. struct device *dev)
  3228. {
  3229. int rc = 0;
  3230. if (!cb || !dev) {
  3231. CAM_ERR(CAM_SMMU, "Error: invalid input params");
  3232. return -EINVAL;
  3233. }
  3234. cb->dev = dev;
  3235. cb->is_fw_allocated = false;
  3236. cb->is_secheap_allocated = false;
  3237. cb->is_fwuncached_buf_allocated = false;
  3238. atomic64_set(&cb->monitor_head, -1);
  3239. /* Create a pool with 64K granularity for supporting shared memory */
  3240. if (cb->shared_support) {
  3241. cb->shared_mem_pool = gen_pool_create(
  3242. SHARED_MEM_POOL_GRANULARITY, -1);
  3243. if (!cb->shared_mem_pool)
  3244. return -ENOMEM;
  3245. rc = gen_pool_add(cb->shared_mem_pool,
  3246. cb->shared_info.iova_start,
  3247. cb->shared_info.iova_len,
  3248. -1);
  3249. CAM_DBG(CAM_SMMU, "Shared mem start->%lX",
  3250. (unsigned long)cb->shared_info.iova_start);
  3251. CAM_DBG(CAM_SMMU, "Shared mem len->%zu",
  3252. cb->shared_info.iova_len);
  3253. if (rc) {
  3254. CAM_ERR(CAM_SMMU, "Genpool chunk creation failed");
  3255. gen_pool_destroy(cb->shared_mem_pool);
  3256. cb->shared_mem_pool = NULL;
  3257. return rc;
  3258. }
  3259. }
  3260. if (cb->scratch_buf_support) {
  3261. rc = cam_smmu_init_scratch_map(&cb->scratch_map,
  3262. cb->scratch_info.iova_start,
  3263. cb->scratch_info.iova_len,
  3264. 0);
  3265. if (rc < 0) {
  3266. CAM_ERR(CAM_SMMU,
  3267. "Error: failed to create scratch map");
  3268. rc = -ENODEV;
  3269. goto end;
  3270. }
  3271. }
  3272. /* create a virtual mapping */
  3273. if (cb->io_support) {
  3274. cb->domain = iommu_get_domain_for_dev(dev);
  3275. if (IS_ERR_OR_NULL(cb->domain)) {
  3276. CAM_ERR(CAM_SMMU, "Error: create domain Failed");
  3277. rc = -ENODEV;
  3278. goto end;
  3279. }
  3280. /* Enable custom iommu features, if applicable */
  3281. cam_smmu_util_iommu_custom(dev, cb->discard_iova_start,
  3282. cb->discard_iova_len);
  3283. cb->state = CAM_SMMU_ATTACH;
  3284. } else {
  3285. CAM_ERR(CAM_SMMU, "Context bank does not have IO region");
  3286. rc = -ENODEV;
  3287. goto end;
  3288. }
  3289. return rc;
  3290. end:
  3291. if (cb->shared_support) {
  3292. gen_pool_destroy(cb->shared_mem_pool);
  3293. cb->shared_mem_pool = NULL;
  3294. }
  3295. if (cb->scratch_buf_support) {
  3296. kfree(cb->scratch_map.bitmap);
  3297. cb->scratch_map.bitmap = NULL;
  3298. }
  3299. return rc;
  3300. }
  3301. static int cam_alloc_smmu_context_banks(struct device *dev)
  3302. {
  3303. struct device_node *domains_child_node = NULL;
  3304. if (!dev) {
  3305. CAM_ERR(CAM_SMMU, "Error: Invalid device");
  3306. return -ENODEV;
  3307. }
  3308. iommu_cb_set.cb_num = 0;
  3309. /* traverse thru all the child nodes and increment the cb count */
  3310. for_each_available_child_of_node(dev->of_node, domains_child_node) {
  3311. if (of_device_is_compatible(domains_child_node,
  3312. "qcom,msm-cam-smmu-cb"))
  3313. iommu_cb_set.cb_num++;
  3314. if (of_device_is_compatible(domains_child_node,
  3315. "qcom,qsmmu-cam-cb"))
  3316. iommu_cb_set.cb_num++;
  3317. }
  3318. if (iommu_cb_set.cb_num == 0) {
  3319. CAM_ERR(CAM_SMMU, "Error: no context banks present");
  3320. return -ENOENT;
  3321. }
  3322. /* allocate memory for the context banks */
  3323. iommu_cb_set.cb_info = devm_kzalloc(dev,
  3324. iommu_cb_set.cb_num * sizeof(struct cam_context_bank_info),
  3325. GFP_KERNEL);
  3326. if (!iommu_cb_set.cb_info) {
  3327. CAM_ERR(CAM_SMMU, "Error: cannot allocate context banks");
  3328. return -ENOMEM;
  3329. }
  3330. cam_smmu_reset_iommu_table(CAM_SMMU_TABLE_INIT);
  3331. iommu_cb_set.cb_init_count = 0;
  3332. CAM_DBG(CAM_SMMU, "no of context banks :%d", iommu_cb_set.cb_num);
  3333. return 0;
  3334. }
  3335. static int cam_smmu_get_discard_memory_regions(struct device_node *of_node,
  3336. dma_addr_t *discard_iova_start, size_t *discard_iova_len)
  3337. {
  3338. uint32_t discard_iova[2] = { 0 };
  3339. int num_values = 0;
  3340. int rc = 0;
  3341. if (!discard_iova_start || !discard_iova_len)
  3342. return -EINVAL;
  3343. *discard_iova_start = 0;
  3344. *discard_iova_len = 0;
  3345. num_values = of_property_count_u32_elems(of_node,
  3346. "iova-region-discard");
  3347. if (num_values <= 0) {
  3348. CAM_DBG(CAM_UTIL, "No discard region specified");
  3349. return 0;
  3350. } else if (num_values != 2) {
  3351. CAM_ERR(CAM_UTIL, "Invalid discard region specified %d",
  3352. num_values);
  3353. return -EINVAL;
  3354. }
  3355. rc = of_property_read_u32_array(of_node,
  3356. "iova-region-discard",
  3357. discard_iova, num_values);
  3358. if (rc) {
  3359. CAM_ERR(CAM_UTIL, "Can not read discard region %d", num_values);
  3360. return rc;
  3361. } else if (!discard_iova[0] || !discard_iova[1]) {
  3362. CAM_ERR(CAM_UTIL,
  3363. "Incorrect Discard region specified [0x%x 0x%x]",
  3364. discard_iova[0], discard_iova[1]);
  3365. return -EINVAL;
  3366. }
  3367. CAM_DBG(CAM_UTIL, "Discard region [0x%x 0x%x]",
  3368. discard_iova[0], discard_iova[0] + discard_iova[1]);
  3369. *discard_iova_start = discard_iova[0];
  3370. *discard_iova_len = discard_iova[1];
  3371. return 0;
  3372. }
  3373. static int cam_smmu_get_memory_regions_info(struct device_node *of_node,
  3374. struct cam_context_bank_info *cb)
  3375. {
  3376. int rc = 0;
  3377. struct device_node *mem_map_node = NULL;
  3378. struct device_node *child_node = NULL;
  3379. dma_addr_t region_start = 0;
  3380. size_t region_len = 0;
  3381. uint32_t region_id;
  3382. uint32_t qdss_region_phy_addr;
  3383. const char *region_name;
  3384. int num_regions = 0;
  3385. if (!of_node || !cb) {
  3386. CAM_ERR(CAM_SMMU, "Invalid argument(s)");
  3387. return -EINVAL;
  3388. }
  3389. mem_map_node = of_get_child_by_name(of_node, "iova-mem-map");
  3390. cb->is_secure = of_property_read_bool(of_node, "qcom,secure-cb");
  3391. /*
  3392. * We always expect a memory map node, except when it is a secure
  3393. * context bank.
  3394. */
  3395. if (!mem_map_node) {
  3396. if (cb->is_secure)
  3397. return 0;
  3398. CAM_ERR(CAM_SMMU, "iova-mem-map not present");
  3399. return -EINVAL;
  3400. }
  3401. for_each_available_child_of_node(mem_map_node, child_node) {
  3402. qdss_region_phy_addr = 0;
  3403. num_regions++;
  3404. rc = of_property_read_string(child_node,
  3405. "iova-region-name", &region_name);
  3406. if (rc < 0) {
  3407. of_node_put(mem_map_node);
  3408. CAM_ERR(CAM_SMMU, "IOVA region not found");
  3409. return -EINVAL;
  3410. }
  3411. if (iommu_cb_set.is_expanded_memory) {
  3412. rc = of_property_read_u64(child_node, "iova-region-start", &region_start);
  3413. if (rc < 0) {
  3414. of_node_put(mem_map_node);
  3415. CAM_ERR(CAM_SMMU, "Failed to read iova-region-start");
  3416. return -EINVAL;
  3417. }
  3418. rc = of_property_read_u64(child_node, "iova-region-len",
  3419. (uint64_t *)&region_len);
  3420. if (rc < 0) {
  3421. of_node_put(mem_map_node);
  3422. CAM_ERR(CAM_SMMU, "Failed to read iova-region-len");
  3423. return -EINVAL;
  3424. }
  3425. } else {
  3426. rc = of_property_read_u32(child_node, "iova-region-start",
  3427. (uint32_t *)&region_start);
  3428. if (rc < 0) {
  3429. of_node_put(mem_map_node);
  3430. CAM_ERR(CAM_SMMU, "Failed to read iova-region-start");
  3431. return -EINVAL;
  3432. }
  3433. rc = of_property_read_u32(child_node, "iova-region-len",
  3434. (uint32_t *)&region_len);
  3435. if (rc < 0) {
  3436. of_node_put(mem_map_node);
  3437. CAM_ERR(CAM_SMMU, "Failed to read iova-region-len");
  3438. return -EINVAL;
  3439. }
  3440. }
  3441. rc = of_property_read_u32(child_node, "iova-region-id", &region_id);
  3442. if (rc < 0) {
  3443. of_node_put(mem_map_node);
  3444. CAM_ERR(CAM_SMMU, "Failed to read iova-region-id");
  3445. return -EINVAL;
  3446. }
  3447. if (strcmp(region_name, qdss_region_name) == 0) {
  3448. rc = of_property_read_u32(child_node,
  3449. "qdss-phy-addr", &qdss_region_phy_addr);
  3450. if (rc < 0) {
  3451. of_node_put(mem_map_node);
  3452. CAM_ERR(CAM_SMMU,
  3453. "Failed to read qdss phy addr");
  3454. return -EINVAL;
  3455. }
  3456. }
  3457. switch (region_id) {
  3458. case CAM_SMMU_REGION_FIRMWARE:
  3459. cb->firmware_support = 1;
  3460. cb->firmware_info.iova_start = region_start;
  3461. cb->firmware_info.iova_len = region_len;
  3462. break;
  3463. case CAM_SMMU_REGION_SHARED:
  3464. cb->shared_support = 1;
  3465. cb->shared_info.iova_start = region_start;
  3466. cb->shared_info.iova_len = region_len;
  3467. break;
  3468. case CAM_SMMU_REGION_SCRATCH:
  3469. cb->scratch_buf_support = 1;
  3470. cb->scratch_info.iova_start = region_start;
  3471. cb->scratch_info.iova_len = region_len;
  3472. break;
  3473. case CAM_SMMU_REGION_IO:
  3474. cb->io_support = 1;
  3475. cb->io_info.iova_start = region_start;
  3476. cb->io_info.iova_len = region_len;
  3477. rc = cam_smmu_get_discard_memory_regions(child_node,
  3478. &cb->io_info.discard_iova_start,
  3479. &cb->io_info.discard_iova_len);
  3480. if (rc) {
  3481. CAM_ERR(CAM_SMMU,
  3482. "Invalid Discard region specified in IO region, rc=%d",
  3483. rc);
  3484. of_node_put(mem_map_node);
  3485. return -EINVAL;
  3486. }
  3487. break;
  3488. case CAM_SMMU_REGION_SECHEAP:
  3489. cb->secheap_support = 1;
  3490. cb->secheap_info.iova_start = region_start;
  3491. cb->secheap_info.iova_len = region_len;
  3492. break;
  3493. case CAM_SMMU_REGION_FWUNCACHED:
  3494. cb->fwuncached_region_support = 1;
  3495. cb->fwuncached_region.iova_start = region_start;
  3496. cb->fwuncached_region.iova_len = region_len;
  3497. break;
  3498. case CAM_SMMU_REGION_QDSS:
  3499. cb->qdss_support = 1;
  3500. cb->qdss_info.iova_start = region_start;
  3501. cb->qdss_info.iova_len = region_len;
  3502. cb->qdss_phy_addr = qdss_region_phy_addr;
  3503. break;
  3504. default:
  3505. CAM_ERR(CAM_SMMU,
  3506. "Incorrect region id present in DT file: %d",
  3507. region_id);
  3508. }
  3509. CAM_DBG(CAM_SMMU, "Found label -> %s", cb->name[0]);
  3510. CAM_DBG(CAM_SMMU, "Found region -> %s", region_name);
  3511. CAM_DBG(CAM_SMMU, "region_start -> %X", region_start);
  3512. CAM_DBG(CAM_SMMU, "region_len -> %X", region_len);
  3513. CAM_DBG(CAM_SMMU, "region_id -> %X", region_id);
  3514. }
  3515. if (cb->io_support) {
  3516. rc = cam_smmu_get_discard_memory_regions(of_node,
  3517. &cb->discard_iova_start,
  3518. &cb->discard_iova_len);
  3519. if (rc) {
  3520. CAM_ERR(CAM_SMMU,
  3521. "Invalid Discard region specified in CB, rc=%d",
  3522. rc);
  3523. of_node_put(mem_map_node);
  3524. return -EINVAL;
  3525. }
  3526. /* Make sure Discard region is properly specified */
  3527. if ((cb->discard_iova_start !=
  3528. cb->io_info.discard_iova_start) ||
  3529. (cb->discard_iova_len !=
  3530. cb->io_info.discard_iova_len)) {
  3531. CAM_ERR(CAM_SMMU,
  3532. "Mismatch Discard region specified, [0x%x 0x%x] [0x%x 0x%x]",
  3533. cb->discard_iova_start,
  3534. cb->discard_iova_len,
  3535. cb->io_info.discard_iova_start,
  3536. cb->io_info.discard_iova_len);
  3537. of_node_put(mem_map_node);
  3538. return -EINVAL;
  3539. } else if (cb->discard_iova_start && cb->discard_iova_len) {
  3540. if ((cb->discard_iova_start <=
  3541. cb->io_info.iova_start) ||
  3542. (cb->discard_iova_start >=
  3543. cb->io_info.iova_start + cb->io_info.iova_len) ||
  3544. (cb->discard_iova_start + cb->discard_iova_len >=
  3545. cb->io_info.iova_start + cb->io_info.iova_len)) {
  3546. CAM_ERR(CAM_SMMU,
  3547. "[%s] : Incorrect Discard region specified [0x%x 0x%x] in [0x%x 0x%x]",
  3548. cb->name[0],
  3549. cb->discard_iova_start,
  3550. cb->discard_iova_start + cb->discard_iova_len,
  3551. cb->io_info.iova_start,
  3552. cb->io_info.iova_start + cb->io_info.iova_len);
  3553. of_node_put(mem_map_node);
  3554. return -EINVAL;
  3555. }
  3556. CAM_INFO(CAM_SMMU,
  3557. "[%s] : Discard region specified [0x%x 0x%x] in [0x%x 0x%x]",
  3558. cb->name[0],
  3559. cb->discard_iova_start,
  3560. cb->discard_iova_start + cb->discard_iova_len,
  3561. cb->io_info.iova_start,
  3562. cb->io_info.iova_start + cb->io_info.iova_len);
  3563. }
  3564. }
  3565. of_node_put(mem_map_node);
  3566. if (!num_regions) {
  3567. CAM_ERR(CAM_SMMU,
  3568. "No memory regions found, at least one needed");
  3569. rc = -ENODEV;
  3570. }
  3571. return rc;
  3572. }
  3573. static int cam_populate_smmu_context_banks(struct device *dev,
  3574. enum cam_iommu_type type)
  3575. {
  3576. int rc = 0;
  3577. struct cam_context_bank_info *cb;
  3578. struct device *ctx = NULL;
  3579. int i = 0;
  3580. bool dma_coherent, dma_coherent_hint;
  3581. if (!dev) {
  3582. CAM_ERR(CAM_SMMU, "Error: Invalid device");
  3583. return -ENODEV;
  3584. }
  3585. /* check the bounds */
  3586. if (iommu_cb_set.cb_init_count >= iommu_cb_set.cb_num) {
  3587. CAM_ERR(CAM_SMMU, "Error: populate more than allocated cb");
  3588. rc = -EBADHANDLE;
  3589. goto cb_init_fail;
  3590. }
  3591. /* read the context bank from cb set */
  3592. cb = &iommu_cb_set.cb_info[iommu_cb_set.cb_init_count];
  3593. cb->is_mul_client =
  3594. of_property_read_bool(dev->of_node, "multiple-client-devices");
  3595. cb->num_shared_hdl = of_property_count_strings(dev->of_node,
  3596. "cam-smmu-label");
  3597. if (cb->num_shared_hdl >
  3598. CAM_SMMU_SHARED_HDL_MAX) {
  3599. CAM_ERR(CAM_CDM, "Invalid count of client names count=%d",
  3600. cb->num_shared_hdl);
  3601. rc = -EINVAL;
  3602. return rc;
  3603. }
  3604. /* set the name of the context bank */
  3605. for (i = 0; i < cb->num_shared_hdl; i++)
  3606. rc = of_property_read_string_index(dev->of_node,
  3607. "cam-smmu-label", i, &cb->name[i]);
  3608. if (rc < 0) {
  3609. CAM_ERR(CAM_SMMU,
  3610. "Error: failed to read label from sub device");
  3611. goto cb_init_fail;
  3612. }
  3613. rc = cam_smmu_get_memory_regions_info(dev->of_node,
  3614. cb);
  3615. if (rc < 0) {
  3616. CAM_ERR(CAM_SMMU, "Error: Getting region info");
  3617. return rc;
  3618. }
  3619. if (cb->is_secure) {
  3620. /* increment count to next bank */
  3621. cb->dev = dev;
  3622. iommu_cb_set.cb_init_count++;
  3623. return 0;
  3624. }
  3625. /* set up the iommu mapping for the context bank */
  3626. if (type == CAM_QSMMU) {
  3627. CAM_ERR(CAM_SMMU, "Error: QSMMU ctx not supported for : %s",
  3628. cb->name[0]);
  3629. return -ENODEV;
  3630. }
  3631. ctx = dev;
  3632. CAM_DBG(CAM_SMMU, "getting Arm SMMU ctx : %s", cb->name[0]);
  3633. cb->coherency_mode = CAM_SMMU_NO_COHERENCY;
  3634. dma_coherent = of_property_read_bool(dev->of_node, "dma-coherent");
  3635. dma_coherent_hint = of_property_read_bool(dev->of_node,
  3636. "dma-coherent-hint-cached");
  3637. if (dma_coherent && dma_coherent_hint) {
  3638. CAM_ERR(CAM_SMMU,
  3639. "[%s] : Cannot enable both dma-coherent and dma-coherent-hint-cached",
  3640. cb->name[0]);
  3641. return -EBADR;
  3642. }
  3643. if (dma_coherent)
  3644. cb->coherency_mode = CAM_SMMU_DMA_COHERENT;
  3645. else if (dma_coherent_hint)
  3646. cb->coherency_mode = CAM_SMMU_DMA_COHERENT_HINT_CACHED;
  3647. CAM_DBG(CAM_SMMU, "[%s] : io cohereny mode %d", cb->name[0],
  3648. cb->coherency_mode);
  3649. rc = cam_smmu_setup_cb(cb, ctx);
  3650. if (rc < 0) {
  3651. CAM_ERR(CAM_SMMU, "Error: failed to setup cb : %s",
  3652. cb->name[0]);
  3653. goto cb_init_fail;
  3654. }
  3655. if (cb->io_support && cb->domain)
  3656. iommu_set_fault_handler(cb->domain,
  3657. cam_smmu_iommu_fault_handler,
  3658. (void *)cb->name[0]);
  3659. if (!dev->dma_parms)
  3660. dev->dma_parms = devm_kzalloc(dev,
  3661. sizeof(*dev->dma_parms), GFP_KERNEL);
  3662. if (!dev->dma_parms) {
  3663. CAM_WARN(CAM_SMMU,
  3664. "Failed to allocate dma_params");
  3665. dev->dma_parms = NULL;
  3666. goto end;
  3667. }
  3668. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  3669. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  3670. end:
  3671. /* increment count to next bank */
  3672. iommu_cb_set.cb_init_count++;
  3673. CAM_DBG(CAM_SMMU, "X: cb init count :%d", iommu_cb_set.cb_init_count);
  3674. cb_init_fail:
  3675. return rc;
  3676. }
  3677. static void cam_smmu_mini_dump_entries(
  3678. struct cam_smmu_mini_dump_cb_info *target,
  3679. struct cam_context_bank_info *src)
  3680. {
  3681. int i = 0;
  3682. int64_t state_head = 0;
  3683. uint32_t index, num_entries, oldest_entry;
  3684. struct timespec64 *ts = NULL;
  3685. state_head = atomic64_read(&src->monitor_head);
  3686. if (state_head == -1) {
  3687. return;
  3688. } else if (state_head < CAM_SMMU_MONITOR_MAX_ENTRIES) {
  3689. num_entries = state_head;
  3690. oldest_entry = 0;
  3691. } else {
  3692. num_entries = CAM_SMMU_MONITOR_MAX_ENTRIES;
  3693. div_u64_rem(state_head + 1,
  3694. CAM_SMMU_MONITOR_MAX_ENTRIES, &oldest_entry);
  3695. }
  3696. index = oldest_entry;
  3697. for (i = 0; i < num_entries; i++) {
  3698. ts = &src->monitor_entries[index].timestamp;
  3699. memcpy(&target->mapping[index],
  3700. &src->monitor_entries[index],
  3701. sizeof(struct cam_smmu_monitor));
  3702. index = (index + 1) % CAM_SMMU_MONITOR_MAX_ENTRIES;
  3703. }
  3704. }
  3705. static unsigned long cam_smmu_mini_dump_cb(void *dst, unsigned long len)
  3706. {
  3707. struct cam_smmu_mini_dump_cb_info *cb_md;
  3708. struct cam_smmu_mini_dump_info *md;
  3709. struct cam_context_bank_info *cb;
  3710. unsigned long dumped_len = 0;
  3711. unsigned long remain_len = len;
  3712. uint32_t i = 0, j = 0;
  3713. if (!dst || len < sizeof(*md)) {
  3714. CAM_ERR(CAM_SMMU, "Invalid params dst: %pk len:%lu",
  3715. dst, len);
  3716. return 0;
  3717. }
  3718. md = (struct cam_smmu_mini_dump_info *)dst;
  3719. md->cb_num = 0;
  3720. md->cb = (struct cam_smmu_mini_dump_cb_info *)
  3721. ((uint8_t *)dst + sizeof(*md));
  3722. dumped_len += sizeof(*md);
  3723. remain_len = len - dumped_len;
  3724. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  3725. if (remain_len < sizeof(*cb_md))
  3726. goto end;
  3727. cb = &iommu_cb_set.cb_info[i];
  3728. cb_md = &md->cb[i];
  3729. cb_md->is_mul_client = cb->is_mul_client;
  3730. cb_md->is_secure = cb->is_secure;
  3731. cb_md->is_fw_allocated = cb->is_fw_allocated;
  3732. cb_md->is_secheap_allocated = cb->is_secheap_allocated;
  3733. cb_md->is_fwuncached_buf_allocated = cb->is_fwuncached_buf_allocated;
  3734. cb_md->is_qdss_allocated = cb->is_qdss_allocated;
  3735. cb_md->scratch_buf_support = cb->scratch_buf_support;
  3736. cb_md->firmware_support = cb->firmware_support;
  3737. cb_md->shared_support = cb->shared_support;
  3738. cb_md->io_support = cb->io_support;
  3739. cb_md->fwuncached_region_support = cb->fwuncached_region_support;
  3740. cb_md->qdss_support = cb->qdss_support;
  3741. cb_md->coherency_mode = cb->coherency_mode;
  3742. cb_md->state = cb->state;
  3743. cb_md->va_start = cb->va_start;
  3744. cb_md->discard_iova_start = cb->discard_iova_start;
  3745. cb_md->qdss_phy_addr = cb->qdss_phy_addr;
  3746. cb_md->va_len = cb->va_len;
  3747. cb_md->io_mapping_size = cb->io_mapping_size;
  3748. cb_md->shared_mapping_size = cb->shared_mapping_size;
  3749. cb_md->discard_iova_len = cb->discard_iova_len;
  3750. cb_md->handle = cb->handle;
  3751. cb_md->device_count = cb->device_count;
  3752. cb_md->num_shared_hdl = cb->num_shared_hdl;
  3753. cb_md->secure_count = cb->secure_count;
  3754. cb_md->cb_count = cb->cb_count;
  3755. cb_md->pf_count = cb->pf_count;
  3756. memcpy(&cb_md->scratch_info, &cb->scratch_info,
  3757. sizeof(struct cam_smmu_region_info));
  3758. memcpy(&cb_md->firmware_info, &cb->firmware_info,
  3759. sizeof(struct cam_smmu_region_info));
  3760. memcpy(&cb_md->shared_info, &cb->shared_info,
  3761. sizeof(struct cam_smmu_region_info));
  3762. memcpy(&cb_md->io_info, &cb->io_info,
  3763. sizeof(struct cam_smmu_region_info));
  3764. memcpy(&cb_md->secheap_info, &cb->secheap_info,
  3765. sizeof(struct cam_smmu_region_info));
  3766. memcpy(&cb_md->fwuncached_region, &cb->fwuncached_region,
  3767. sizeof(struct cam_smmu_region_info));
  3768. memcpy(&cb_md->qdss_info, &cb->qdss_info,
  3769. sizeof(struct cam_smmu_region_info));
  3770. memcpy(&cb_md->secheap_buf, &cb->secheap_buf,
  3771. sizeof(struct region_buf_info));
  3772. memcpy(&cb_md->fwuncached_reg_buf, &cb->fwuncached_reg_buf,
  3773. sizeof(struct region_buf_info));
  3774. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++)
  3775. scnprintf(cb_md->name[j], 16, cb->name[j]);
  3776. cam_smmu_mini_dump_entries(cb_md, cb);
  3777. dumped_len += sizeof(*cb_md);
  3778. remain_len = len - dumped_len;
  3779. md->cb_num++;
  3780. }
  3781. end:
  3782. return dumped_len;
  3783. };
  3784. static int cam_smmu_create_debug_fs(void)
  3785. {
  3786. int rc = 0;
  3787. struct dentry *dbgfileptr = NULL;
  3788. if (!cam_debugfs_available())
  3789. return 0;
  3790. rc = cam_debugfs_create_subdir("smmu", &dbgfileptr);
  3791. if (rc) {
  3792. CAM_ERR(CAM_SMMU,"DebugFS could not create directory!");
  3793. rc = -ENOENT;
  3794. goto end;
  3795. }
  3796. /* Store parent inode for cleanup in caller */
  3797. iommu_cb_set.dentry = dbgfileptr;
  3798. debugfs_create_bool("cb_dump_enable", 0644,
  3799. iommu_cb_set.dentry, &iommu_cb_set.cb_dump_enable);
  3800. debugfs_create_bool("map_profile_enable", 0644,
  3801. iommu_cb_set.dentry, &iommu_cb_set.map_profile_enable);
  3802. end:
  3803. return rc;
  3804. }
  3805. static int cam_smmu_fw_dev_component_bind(struct device *dev,
  3806. struct device *master_dev, void *data)
  3807. {
  3808. struct platform_device *pdev = to_platform_device(dev);
  3809. icp_fw.fw_dev = &pdev->dev;
  3810. icp_fw.fw_kva = NULL;
  3811. icp_fw.fw_hdl = 0;
  3812. CAM_DBG(CAM_SMMU, "FW dev component bound successfully");
  3813. return 0;
  3814. }
  3815. static void cam_smmu_fw_dev_component_unbind(struct device *dev,
  3816. struct device *master_dev, void *data)
  3817. {
  3818. struct platform_device *pdev = to_platform_device(dev);
  3819. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  3820. }
  3821. const static struct component_ops cam_smmu_fw_dev_component_ops = {
  3822. .bind = cam_smmu_fw_dev_component_bind,
  3823. .unbind = cam_smmu_fw_dev_component_unbind,
  3824. };
  3825. static int cam_smmu_cb_component_bind(struct device *dev,
  3826. struct device *master_dev, void *data)
  3827. {
  3828. int rc = 0;
  3829. struct platform_device *pdev = to_platform_device(dev);
  3830. rc = cam_populate_smmu_context_banks(dev, CAM_ARM_SMMU);
  3831. if (rc < 0) {
  3832. CAM_ERR(CAM_SMMU, "Error: populating context banks");
  3833. cam_smmu_release_cb(pdev);
  3834. return -ENOMEM;
  3835. }
  3836. CAM_DBG(CAM_SMMU, "CB component bound successfully");
  3837. return 0;
  3838. }
  3839. static void cam_smmu_cb_component_unbind(struct device *dev,
  3840. struct device *master_dev, void *data)
  3841. {
  3842. struct platform_device *pdev = to_platform_device(dev);
  3843. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  3844. }
  3845. const static struct component_ops cam_smmu_cb_component_ops = {
  3846. .bind = cam_smmu_cb_component_bind,
  3847. .unbind = cam_smmu_cb_component_unbind,
  3848. };
  3849. static int cam_smmu_cb_qsmmu_component_bind(struct device *dev,
  3850. struct device *master_dev, void *data)
  3851. {
  3852. int rc = 0;
  3853. rc = cam_populate_smmu_context_banks(dev, CAM_QSMMU);
  3854. if (rc < 0) {
  3855. CAM_ERR(CAM_SMMU, "Failed in populating context banks");
  3856. return -ENOMEM;
  3857. }
  3858. CAM_DBG(CAM_SMMU, "QSMMU CB component bound successfully");
  3859. return 0;
  3860. }
  3861. static void cam_smmu_cb_qsmmu_component_unbind(struct device *dev,
  3862. struct device *master_dev, void *data)
  3863. {
  3864. struct platform_device *pdev = to_platform_device(dev);
  3865. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  3866. }
  3867. const static struct component_ops cam_smmu_cb_qsmmu_component_ops = {
  3868. .bind = cam_smmu_cb_qsmmu_component_bind,
  3869. .unbind = cam_smmu_cb_qsmmu_component_unbind,
  3870. };
  3871. static int cam_smmu_component_bind(struct device *dev,
  3872. struct device *master_dev, void *data)
  3873. {
  3874. INIT_WORK(&iommu_cb_set.smmu_work, cam_smmu_page_fault_work);
  3875. mutex_init(&iommu_cb_set.payload_list_lock);
  3876. INIT_LIST_HEAD(&iommu_cb_set.payload_list);
  3877. cam_smmu_create_debug_fs();
  3878. iommu_cb_set.force_cache_allocs =
  3879. of_property_read_bool(dev->of_node, "force_cache_allocs");
  3880. iommu_cb_set.need_shared_buffer_padding =
  3881. of_property_read_bool(dev->of_node, "need_shared_buffer_padding");
  3882. iommu_cb_set.is_expanded_memory =
  3883. of_property_read_bool(dev->of_node, "expanded_memory");
  3884. cam_common_register_mini_dump_cb(cam_smmu_mini_dump_cb,
  3885. "cam_smmu");
  3886. CAM_DBG(CAM_SMMU, "Main component bound successfully");
  3887. return 0;
  3888. }
  3889. static void cam_smmu_component_unbind(struct device *dev,
  3890. struct device *master_dev, void *data)
  3891. {
  3892. struct platform_device *pdev = to_platform_device(dev);
  3893. /* release all the context banks and memory allocated */
  3894. cam_smmu_reset_iommu_table(CAM_SMMU_TABLE_DEINIT);
  3895. if (dev && dev->dma_parms) {
  3896. devm_kfree(dev, dev->dma_parms);
  3897. dev->dma_parms = NULL;
  3898. }
  3899. cam_smmu_release_cb(pdev);
  3900. iommu_cb_set.dentry = NULL;
  3901. }
  3902. const static struct component_ops cam_smmu_component_ops = {
  3903. .bind = cam_smmu_component_bind,
  3904. .unbind = cam_smmu_component_unbind,
  3905. };
  3906. static int cam_smmu_probe(struct platform_device *pdev)
  3907. {
  3908. int rc = 0;
  3909. struct device *dev = &pdev->dev;
  3910. dev->dma_parms = NULL;
  3911. CAM_DBG(CAM_SMMU, "Adding SMMU component: %s", pdev->name);
  3912. if (of_device_is_compatible(dev->of_node, "qcom,msm-cam-smmu")) {
  3913. rc = cam_alloc_smmu_context_banks(dev);
  3914. if (rc < 0) {
  3915. CAM_ERR(CAM_SMMU, "Failed in allocating context banks");
  3916. return -ENOMEM;
  3917. }
  3918. rc = component_add(&pdev->dev, &cam_smmu_component_ops);
  3919. } else if (of_device_is_compatible(dev->of_node,
  3920. "qcom,msm-cam-smmu-cb")) {
  3921. rc = component_add(&pdev->dev, &cam_smmu_cb_component_ops);
  3922. } else if (of_device_is_compatible(dev->of_node, "qcom,qsmmu-cam-cb")) {
  3923. rc = component_add(&pdev->dev,
  3924. &cam_smmu_cb_qsmmu_component_ops);
  3925. } else if (of_device_is_compatible(dev->of_node,
  3926. "qcom,msm-cam-smmu-fw-dev")) {
  3927. rc = component_add(&pdev->dev, &cam_smmu_fw_dev_component_ops);
  3928. } else {
  3929. CAM_ERR(CAM_SMMU, "Unrecognized child device: %s", pdev->name);
  3930. rc = -ENODEV;
  3931. }
  3932. if (rc < 0)
  3933. CAM_ERR(CAM_SMMU, "failed to add component rc: %d", rc);
  3934. return rc;
  3935. }
  3936. static int cam_smmu_remove(struct platform_device *pdev)
  3937. {
  3938. struct device *dev = &pdev->dev;
  3939. CAM_DBG(CAM_SMMU, "Removing SMMU component: %s", pdev->name);
  3940. if (of_device_is_compatible(dev->of_node, "qcom,msm-cam-smmu")) {
  3941. component_del(&pdev->dev, &cam_smmu_component_ops);
  3942. } else if (of_device_is_compatible(dev->of_node,
  3943. "qcom,msm-cam-smmu-cb")) {
  3944. component_del(&pdev->dev, &cam_smmu_cb_component_ops);
  3945. } else if (of_device_is_compatible(dev->of_node, "qcom,qsmmu-cam-cb")) {
  3946. component_del(&pdev->dev, &cam_smmu_cb_qsmmu_component_ops);
  3947. } else if (of_device_is_compatible(dev->of_node,
  3948. "qcom,msm-cam-smmu-fw-dev")) {
  3949. component_del(&pdev->dev, &cam_smmu_fw_dev_component_ops);
  3950. } else {
  3951. CAM_ERR(CAM_SMMU, "Unrecognized child device: %s", pdev->name);
  3952. return -ENODEV;
  3953. }
  3954. return 0;
  3955. }
  3956. struct platform_driver cam_smmu_driver = {
  3957. .probe = cam_smmu_probe,
  3958. .remove = cam_smmu_remove,
  3959. .driver = {
  3960. .name = "msm_cam_smmu",
  3961. .owner = THIS_MODULE,
  3962. .of_match_table = msm_cam_smmu_dt_match,
  3963. .suppress_bind_attrs = true,
  3964. },
  3965. };
  3966. int cam_smmu_init_module(void)
  3967. {
  3968. return platform_driver_register(&cam_smmu_driver);
  3969. }
  3970. void cam_smmu_exit_module(void)
  3971. {
  3972. platform_driver_unregister(&cam_smmu_driver);
  3973. }
  3974. MODULE_DESCRIPTION("MSM Camera SMMU driver");
  3975. MODULE_LICENSE("GPL v2");