cam_smmu_api.c 123 KB

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