cam_smmu_api.c 95 KB

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