cam_smmu_api.c 112 KB

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