cam_smmu_api.c 104 KB

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