cam_smmu_api.c 112 KB

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