cam_smmu_api.c 87 KB

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