cam_smmu_api.c 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/dma-buf.h>
  8. #include <linux/dma-direction.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/iommu.h>
  11. #include <linux/slab.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/of_address.h>
  14. #include <linux/msm_dma_iommu_mapping.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/genalloc.h>
  17. #include <linux/debugfs.h>
  18. #include <soc/qcom/secure_buffer.h>
  19. #include <media/cam_req_mgr.h>
  20. #include "cam_compat.h"
  21. #include "cam_smmu_api.h"
  22. #include "cam_debug_util.h"
  23. #include "camera_main.h"
  24. #include "cam_trace.h"
  25. #include "cam_common_util.h"
  26. #define SHARED_MEM_POOL_GRANULARITY 16
  27. #define IOMMU_INVALID_DIR -1
  28. #define HANDLE_INIT (-1)
  29. #define CAM_SMMU_CB_MAX 6
  30. #define CAM_SMMU_SHARED_HDL_MAX 6
  31. #define CAM_SMMU_MULTI_REGION_MAX 2
  32. #define GET_SMMU_HDL(x, y) (((x) << COOKIE_SIZE) | ((y) & COOKIE_MASK))
  33. #define GET_SMMU_MULTI_CLIENT_IDX(x) (((x) >> MULTI_CLIENT_REGION_SHIFT))
  34. #define CAM_SMMU_HDL_VALIDATE(x, y) ((x) != ((y) & CAM_SMMU_HDL_MASK))
  35. #define CAM_SMMU_MONITOR_MAX_ENTRIES 100
  36. #define CAM_SMMU_BUF_TRACKING_POOL 600
  37. #define CAM_SMMU_INC_MONITOR_HEAD(head, ret) \
  38. div_u64_rem(atomic64_add_return(1, head),\
  39. CAM_SMMU_MONITOR_MAX_ENTRIES, (ret))
  40. static int g_num_pf_handled = 1;
  41. module_param(g_num_pf_handled, int, 0644);
  42. struct cam_fw_alloc_info icp_fw;
  43. struct cam_smmu_buffer_tracker *buf_tracking_pool;
  44. struct cam_smmu_work_payload {
  45. int idx;
  46. struct iommu_domain *domain;
  47. struct device *dev;
  48. unsigned long iova;
  49. int flags;
  50. void *token;
  51. struct list_head list;
  52. };
  53. enum cam_io_coherency_mode {
  54. CAM_SMMU_NO_COHERENCY,
  55. CAM_SMMU_DMA_COHERENT,
  56. CAM_SMMU_DMA_COHERENT_HINT_CACHED,
  57. };
  58. enum cam_protection_type {
  59. CAM_PROT_INVALID,
  60. CAM_NON_SECURE,
  61. CAM_SECURE,
  62. CAM_PROT_MAX,
  63. };
  64. enum cam_iommu_type {
  65. CAM_SMMU_INVALID,
  66. CAM_QSMMU,
  67. CAM_ARM_SMMU,
  68. CAM_SMMU_MAX,
  69. };
  70. enum cam_smmu_buf_state {
  71. CAM_SMMU_BUFF_EXIST,
  72. CAM_SMMU_BUFF_NOT_EXIST,
  73. };
  74. enum cam_smmu_init_dir {
  75. CAM_SMMU_TABLE_INIT,
  76. CAM_SMMU_TABLE_DEINIT,
  77. };
  78. struct scratch_mapping {
  79. void *bitmap;
  80. size_t bits;
  81. unsigned int order;
  82. dma_addr_t base;
  83. };
  84. struct cam_smmu_monitor {
  85. struct timespec64 timestamp;
  86. bool is_map;
  87. /* map-unmap info */
  88. int ion_fd;
  89. unsigned long i_ino;
  90. dma_addr_t paddr;
  91. size_t len;
  92. enum cam_smmu_region_id region_id;
  93. };
  94. struct cam_smmu_debug {
  95. struct dentry *dentry;
  96. uint32_t fatal_pf_mask;
  97. bool cb_dump_enable;
  98. bool map_profile_enable;
  99. bool disable_buf_tracking;
  100. };
  101. struct cam_smmu_subregion_info {
  102. enum cam_smmu_subregion_id subregion_id;
  103. struct cam_smmu_region_info subregion_info;
  104. int32_t mapped_refcnt;
  105. unsigned long mapped_client_mask;
  106. bool is_allocated;
  107. };
  108. struct cam_smmu_nested_region_info {
  109. uint32_t num_subregions;
  110. struct cam_smmu_subregion_info subregions[
  111. CAM_SMMU_SUBREGION_MAX];
  112. struct cam_smmu_region_info region_info;
  113. int32_t mapped_refcnt;
  114. unsigned long mapped_client_mask;
  115. bool is_allocated;
  116. bool subregion_support;
  117. };
  118. /*
  119. * This struct holds info on multiple regions of the
  120. * same type, and each such region can have multiple
  121. * unique subregions
  122. *
  123. * A0 --> a : b : c
  124. * A1 --> a : c
  125. *
  126. * Here A0 & A1 are 2 umbrella regions of the same type,
  127. * and a, b & c are the subregions within them
  128. */
  129. struct cam_smmu_multi_region_info {
  130. int32_t num_regions;
  131. struct cam_smmu_nested_region_info nested_regions[CAM_SMMU_MULTI_REGION_MAX];
  132. };
  133. struct cam_context_bank_info {
  134. struct device *dev;
  135. struct iommu_domain *domain;
  136. dma_addr_t va_start;
  137. size_t va_len;
  138. const char *name[CAM_SMMU_SHARED_HDL_MAX];
  139. bool is_secure;
  140. uint8_t scratch_buf_support;
  141. uint8_t firmware_support;
  142. uint8_t shared_support;
  143. uint8_t io_support;
  144. uint8_t secheap_support;
  145. uint8_t fwuncached_region_support;
  146. uint8_t qdss_support;
  147. uint8_t device_region_support;
  148. dma_addr_t qdss_phy_addr;
  149. bool is_fw_allocated;
  150. bool is_secheap_allocated;
  151. bool is_qdss_allocated;
  152. bool non_fatal_faults_en;
  153. bool stall_disable_en;
  154. struct scratch_mapping scratch_map;
  155. struct gen_pool *shared_mem_pool[CAM_SMMU_MULTI_REGION_MAX];
  156. /* Regular singleton regions */
  157. struct cam_smmu_region_info scratch_info;
  158. struct cam_smmu_region_info firmware_info;
  159. struct cam_smmu_region_info secheap_info;
  160. /* Regions capable of having multiple of them */
  161. struct cam_smmu_multi_region_info shared_info;
  162. struct cam_smmu_multi_region_info io_info;
  163. struct cam_smmu_multi_region_info fwuncached_region;
  164. struct cam_smmu_multi_region_info device_region;
  165. struct cam_smmu_multi_region_info qdss_info;
  166. struct list_head smmu_buf_list;
  167. struct list_head smmu_buf_kernel_list;
  168. struct mutex lock;
  169. int handle;
  170. enum cam_smmu_ops_param state;
  171. void (*handler[CAM_SMMU_CB_MAX]) (struct cam_smmu_pf_info *pf_info);
  172. void *token[CAM_SMMU_CB_MAX];
  173. int cb_count;
  174. int secure_count;
  175. int pf_count;
  176. size_t io_mapping_size;
  177. size_t shared_mapping_size;
  178. bool is_mul_client;
  179. int device_count;
  180. int num_shared_hdl;
  181. int num_multi_regions;
  182. const char *multi_region_clients[CAM_SMMU_MULTI_REGION_MAX];
  183. enum cam_io_coherency_mode coherency_mode;
  184. /* discard iova - non-zero values are valid */
  185. dma_addr_t discard_iova_start;
  186. size_t discard_iova_len;
  187. atomic64_t monitor_head;
  188. struct cam_smmu_monitor monitor_entries[CAM_SMMU_MONITOR_MAX_ENTRIES];
  189. };
  190. struct cam_iommu_cb_set {
  191. struct cam_context_bank_info *cb_info;
  192. u32 cb_num;
  193. u32 cb_init_count;
  194. struct work_struct smmu_work;
  195. struct mutex payload_list_lock;
  196. struct list_head payload_list;
  197. struct cam_smmu_debug debug_cfg;
  198. struct list_head buf_tracker_free_list;
  199. struct cam_csf_version csf_version;
  200. spinlock_t s_lock;
  201. bool force_cache_allocs;
  202. bool need_shared_buffer_padding;
  203. bool is_expanded_memory;
  204. bool is_track_buf_disabled;
  205. };
  206. static const struct of_device_id msm_cam_smmu_dt_match[] = {
  207. { .compatible = "qcom,msm-cam-smmu", },
  208. { .compatible = "qcom,msm-cam-smmu-cb", },
  209. { .compatible = "qcom,msm-cam-smmu-fw-dev", },
  210. {}
  211. };
  212. struct cam_dma_buff_info {
  213. struct dma_buf *buf;
  214. struct dma_buf_attachment *attach;
  215. struct sg_table *table;
  216. enum dma_data_direction dir;
  217. enum cam_smmu_region_id region_id;
  218. int iommu_dir;
  219. int map_count;
  220. struct kref ref_count;
  221. dma_addr_t paddr;
  222. struct list_head list;
  223. int ion_fd;
  224. unsigned long i_ino;
  225. size_t len;
  226. size_t phys_len;
  227. bool is_internal;
  228. struct timespec64 ts;
  229. };
  230. struct cam_sec_buff_info {
  231. struct dma_buf *buf;
  232. struct dma_buf_attachment *attach;
  233. struct sg_table *table;
  234. enum dma_data_direction dir;
  235. int map_count;
  236. struct kref ref_count;
  237. dma_addr_t paddr;
  238. struct list_head list;
  239. int ion_fd;
  240. unsigned long i_ino;
  241. size_t len;
  242. };
  243. struct cam_smmu_mini_dump_cb_info {
  244. struct cam_smmu_monitor mapping[CAM_SMMU_MONITOR_MAX_ENTRIES];
  245. struct cam_smmu_region_info scratch_info;
  246. struct cam_smmu_region_info firmware_info;
  247. struct cam_smmu_region_info shared_info;
  248. struct cam_smmu_region_info io_info;
  249. struct cam_smmu_region_info secheap_info;
  250. struct cam_smmu_region_info fwuncached_region;
  251. struct cam_smmu_region_info device_mem_region;
  252. struct cam_smmu_region_info qdss_info;
  253. struct region_buf_info secheap_buf;
  254. struct region_buf_info fwuncached_reg_buf;
  255. char name[CAM_SMMU_SHARED_HDL_MAX][16];
  256. size_t va_len;
  257. size_t io_mapping_size;
  258. size_t shared_mapping_size;
  259. size_t discard_iova_len;
  260. int handle;
  261. int device_count;
  262. int num_shared_hdl;
  263. int cb_count;
  264. int secure_count;
  265. int pf_count;
  266. dma_addr_t va_start;
  267. dma_addr_t discard_iova_start;
  268. dma_addr_t qdss_phy_addr;
  269. enum cam_io_coherency_mode coherency_mode;
  270. enum cam_smmu_ops_param state;
  271. uint8_t scratch_buf_support;
  272. uint8_t firmware_support;
  273. uint8_t shared_support;
  274. uint8_t io_support;
  275. uint8_t secheap_support;
  276. uint8_t fwuncached_region_support;
  277. uint8_t qdss_support;
  278. bool is_mul_client;
  279. bool is_secure;
  280. bool is_fw_allocated;
  281. bool is_secheap_allocated;
  282. bool is_fwuncached_buf_allocated;
  283. bool is_qdss_allocated;
  284. };
  285. struct cam_smmu_mini_dump_info {
  286. uint32_t cb_num;
  287. struct cam_smmu_mini_dump_cb_info *cb;
  288. };
  289. static struct cam_iommu_cb_set iommu_cb_set;
  290. static enum dma_data_direction cam_smmu_translate_dir(
  291. enum cam_smmu_map_dir dir);
  292. static bool cam_smmu_is_hdl_nonunique_or_null(int hdl);
  293. static int cam_smmu_create_iommu_handle(int idx);
  294. static int cam_smmu_create_add_handle_in_table(char *name,
  295. int *hdl);
  296. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_ion_index(int idx,
  297. int ion_fd, struct dma_buf *dma_buf);
  298. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_dma_buf(int idx,
  299. struct dma_buf *buf);
  300. static struct cam_sec_buff_info *cam_smmu_find_mapping_by_sec_buf_idx(int idx,
  301. int ion_fd, struct dma_buf *dma_buf);
  302. static int cam_smmu_init_scratch_map(struct scratch_mapping *scratch_map,
  303. dma_addr_t base, size_t size,
  304. int order);
  305. static int cam_smmu_alloc_scratch_va(struct scratch_mapping *mapping,
  306. size_t size,
  307. dma_addr_t *iova);
  308. static int cam_smmu_free_scratch_va(struct scratch_mapping *mapping,
  309. dma_addr_t addr, size_t size);
  310. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_virt_address(int idx,
  311. dma_addr_t virt_addr);
  312. static int cam_smmu_map_buffer_and_add_to_list(int idx, int ion_fd,
  313. bool dis_delayed_unmap, enum dma_data_direction dma_dir,
  314. dma_addr_t *paddr_ptr, size_t *len_ptr,
  315. enum cam_smmu_region_id region_id, bool is_internal, struct dma_buf *dmabuf,
  316. struct kref **ref_count);
  317. static int cam_smmu_map_kernel_buffer_and_add_to_list(int idx,
  318. struct dma_buf *buf, enum dma_data_direction dma_dir,
  319. dma_addr_t *paddr_ptr, size_t *len_ptr,
  320. enum cam_smmu_region_id region_id);
  321. static int cam_smmu_alloc_scratch_buffer_add_to_list(int idx,
  322. size_t virt_len,
  323. size_t phys_len,
  324. unsigned int iommu_dir,
  325. dma_addr_t *virt_addr);
  326. static int cam_smmu_unmap_buf_and_remove_from_list(
  327. struct cam_dma_buff_info *mapping_info, int idx);
  328. static int cam_smmu_free_scratch_buffer_remove_from_list(
  329. struct cam_dma_buff_info *mapping_info,
  330. int idx);
  331. static void cam_smmu_clean_user_buffer_list(int idx);
  332. static void cam_smmu_clean_kernel_buffer_list(int idx);
  333. static void cam_smmu_dump_cb_info(int idx);
  334. static void cam_smmu_print_user_list(int idx);
  335. static void cam_smmu_print_kernel_list(int idx);
  336. static void cam_smmu_print_table(void);
  337. static int cam_smmu_probe(struct platform_device *pdev);
  338. static uint32_t cam_smmu_find_closest_mapping(int idx, void *vaddr, bool *in_map_region);
  339. static void cam_smmu_update_monitor_array(
  340. struct cam_context_bank_info *cb_info,
  341. bool is_map,
  342. struct cam_dma_buff_info *mapping_info)
  343. {
  344. int iterator;
  345. CAM_SMMU_INC_MONITOR_HEAD(&cb_info->monitor_head, &iterator);
  346. CAM_GET_TIMESTAMP(cb_info->monitor_entries[iterator].timestamp);
  347. cb_info->monitor_entries[iterator].is_map = is_map;
  348. cb_info->monitor_entries[iterator].ion_fd = mapping_info->ion_fd;
  349. cb_info->monitor_entries[iterator].i_ino = mapping_info->i_ino;
  350. cb_info->monitor_entries[iterator].paddr = mapping_info->paddr;
  351. cb_info->monitor_entries[iterator].len = mapping_info->len;
  352. cb_info->monitor_entries[iterator].region_id = mapping_info->region_id;
  353. }
  354. static void cam_smmu_dump_monitor_array(
  355. struct cam_context_bank_info *cb_info)
  356. {
  357. int i = 0;
  358. int64_t state_head = 0;
  359. uint32_t index, num_entries, oldest_entry;
  360. uint64_t ms, hrs, min, sec;
  361. state_head = atomic64_read(&cb_info->monitor_head);
  362. if (state_head == -1) {
  363. return;
  364. } else if (state_head < CAM_SMMU_MONITOR_MAX_ENTRIES) {
  365. num_entries = state_head;
  366. oldest_entry = 0;
  367. } else {
  368. num_entries = CAM_SMMU_MONITOR_MAX_ENTRIES;
  369. div_u64_rem(state_head + 1,
  370. CAM_SMMU_MONITOR_MAX_ENTRIES, &oldest_entry);
  371. }
  372. CAM_INFO(CAM_SMMU,
  373. "========Dumping monitor information for cb %s===========",
  374. cb_info->name[0]);
  375. index = oldest_entry;
  376. for (i = 0; i < num_entries; i++) {
  377. CAM_CONVERT_TIMESTAMP_FORMAT(cb_info->monitor_entries[index].timestamp,
  378. hrs, min, sec, ms);
  379. CAM_INFO(CAM_SMMU,
  380. "**** %llu:%llu:%llu.%llu : Index[%d] [%s] : ion_fd=%d i_ino=%lu start=0x%llx end=0x%llx len=%zu region=%d",
  381. hrs, min, sec, ms,
  382. index,
  383. cb_info->monitor_entries[index].is_map ? "MAP" : "UNMAP",
  384. cb_info->monitor_entries[index].ion_fd,
  385. cb_info->monitor_entries[index].i_ino,
  386. cb_info->monitor_entries[index].paddr,
  387. cb_info->monitor_entries[index].paddr +
  388. cb_info->monitor_entries[index].len,
  389. cb_info->monitor_entries[index].len,
  390. cb_info->monitor_entries[index].region_id);
  391. index = (index + 1) % CAM_SMMU_MONITOR_MAX_ENTRIES;
  392. }
  393. }
  394. bool cam_smmu_need_shared_buffer_padding(void)
  395. {
  396. return iommu_cb_set.need_shared_buffer_padding;
  397. }
  398. bool cam_smmu_is_expanded_memory(void)
  399. {
  400. return iommu_cb_set.is_expanded_memory;
  401. }
  402. int cam_smmu_need_force_alloc_cached(bool *force_alloc_cached)
  403. {
  404. int idx;
  405. uint32_t curr_mode = 0, final_mode = 0;
  406. bool validate = false;
  407. if (!force_alloc_cached) {
  408. CAM_ERR(CAM_SMMU, "Invalid arg");
  409. return -EINVAL;
  410. }
  411. CAM_INFO(CAM_SMMU, "force_cache_allocs=%d",
  412. iommu_cb_set.force_cache_allocs);
  413. /*
  414. * First validate whether all SMMU CBs are properly setup to comply with
  415. * iommu_cb_set.force_alloc_cached flag.
  416. * This helps as a validation check to make sure a valid DT combination
  417. * is set for a given chipset.
  418. */
  419. for (idx = 0; idx < iommu_cb_set.cb_num; idx++) {
  420. /* ignore secure cb for now. need to revisit */
  421. if (iommu_cb_set.cb_info[idx].is_secure)
  422. continue;
  423. curr_mode = iommu_cb_set.cb_info[idx].coherency_mode;
  424. /*
  425. * 1. No coherency:
  426. * We can map both CACHED and UNCACHED buffers into same CB.
  427. * We need to allocate UNCACHED buffers for Cmdbuffers
  428. * and Shared Buffers. UNCAHE support must exists with memory
  429. * allocators (ion or dma-buf-heaps) for CmdBuffers,
  430. * SharedBuffers to work - as it is difficult to do
  431. * cache operations on these buffers in camera design.
  432. * ImageBuffers can be CACHED or UNCACHED. If CACHED, clients
  433. * need to make required CACHE operations.
  434. * Cannot force all allocations to CACHE.
  435. * 2. dma-coherent:
  436. * We cannot map CACHED and UNCACHED buffers into the same CB
  437. * This means, we must force allocate all buffers to be
  438. * CACHED.
  439. * 3. dma-coherent-hint-cached
  440. * We can map both CACHED and UNCACHED buffers into the same
  441. * CB. So any option is fine force_cache_allocs.
  442. * Forcing to cache is preferable though.
  443. *
  444. * Other rule we are enforcing is - all camera CBs (except
  445. * secure CB) must have same coherency mode set. Assume one CB
  446. * is having no_coherency mode and other CB is having
  447. * dma_coherent. For no_coherency CB to work - we must not force
  448. * buffers to be CACHE (exa cmd buffers), for dma_coherent mode
  449. * we must force all buffers to be CACHED. But at the time of
  450. * allocation, we dont know to which CB we will be mapping this
  451. * buffer. So it becomes difficult to generalize cache
  452. * allocations and io coherency mode that we want to support.
  453. * So, to simplify, all camera CBs will have same mode.
  454. */
  455. CAM_DBG(CAM_SMMU, "[%s] : curr_mode=%d",
  456. iommu_cb_set.cb_info[idx].name[0], curr_mode);
  457. if (curr_mode == CAM_SMMU_NO_COHERENCY) {
  458. if (iommu_cb_set.force_cache_allocs) {
  459. CAM_ERR(CAM_SMMU,
  460. "[%s] Can't force alloc cache with no coherency",
  461. iommu_cb_set.cb_info[idx].name[0]);
  462. return -EINVAL;
  463. }
  464. } else if (curr_mode == CAM_SMMU_DMA_COHERENT) {
  465. if (!iommu_cb_set.force_cache_allocs) {
  466. CAM_ERR(CAM_SMMU,
  467. "[%s] Must force cache allocs for dma coherent device",
  468. iommu_cb_set.cb_info[idx].name[0]);
  469. return -EINVAL;
  470. }
  471. }
  472. if (validate) {
  473. if (curr_mode != final_mode) {
  474. CAM_ERR(CAM_SMMU,
  475. "[%s] CBs having different coherency modes final=%d, curr=%d",
  476. iommu_cb_set.cb_info[idx].name[0],
  477. final_mode, curr_mode);
  478. return -EINVAL;
  479. }
  480. } else {
  481. validate = true;
  482. final_mode = curr_mode;
  483. }
  484. }
  485. /*
  486. * To be more accurate - if this flag is TRUE and if this buffer will
  487. * be mapped to external devices like CVP - we need to ensure we do
  488. * one of below :
  489. * 1. CVP CB having dma-coherent or dma-coherent-hint-cached
  490. * 2. camera/cvp sw layers properly doing required cache operations. We
  491. * cannot anymore assume these buffers (camera <--> cvp) are uncached
  492. */
  493. *force_alloc_cached = iommu_cb_set.force_cache_allocs;
  494. return 0;
  495. }
  496. static struct cam_smmu_subregion_info *cam_smmu_find_subregion(
  497. enum cam_smmu_subregion_id subregion_id,
  498. struct cam_smmu_subregion_info *subregions)
  499. {
  500. int i;
  501. for (i = 0; i < CAM_SMMU_SUBREGION_MAX; i++) {
  502. if (subregions[i].subregion_id == subregion_id)
  503. return &subregions[i];
  504. }
  505. return NULL;
  506. }
  507. static int cam_smmu_validate_nested_region_idx(
  508. struct cam_context_bank_info *cb_info,
  509. int32_t *nested_reg_idx, int32_t region_id)
  510. {
  511. /* Array indexing starts from 0, subtracting number of regions by 1 */
  512. switch (region_id) {
  513. case CAM_SMMU_REGION_SHARED:
  514. if ((*nested_reg_idx) > (cb_info->shared_info.num_regions - 1))
  515. goto err;
  516. break;
  517. case CAM_SMMU_REGION_FWUNCACHED:
  518. if ((*nested_reg_idx) > (cb_info->fwuncached_region.num_regions - 1))
  519. goto err;
  520. break;
  521. /* For device and IO, if there is no additional region, default to index 0 */
  522. case CAM_SMMU_REGION_DEVICE:
  523. if ((*nested_reg_idx) > (cb_info->device_region.num_regions - 1))
  524. *nested_reg_idx = 0;
  525. break;
  526. case CAM_SMMU_REGION_IO:
  527. if ((*nested_reg_idx) > (cb_info->io_info.num_regions - 1))
  528. *nested_reg_idx = 0;
  529. break;
  530. case CAM_SMMU_REGION_QDSS:
  531. fallthrough;
  532. case CAM_SMMU_REGION_SECHEAP:
  533. fallthrough;
  534. case CAM_SMMU_REGION_SCRATCH:
  535. *nested_reg_idx = 0;
  536. break;
  537. default:
  538. CAM_DBG(CAM_SMMU,
  539. "Invalid region id=%u on cb=%s to get nested region index",
  540. region_id, cb_info->name[0]);
  541. break;
  542. }
  543. return 0;
  544. err:
  545. CAM_ERR(CAM_SMMU,
  546. "Nested region idx=%d exceeds max regions=%d for region_id=%d in cb=%s",
  547. nested_reg_idx, cb_info->shared_info.num_regions, region_id, cb_info->name[0]);
  548. return -EINVAL;
  549. }
  550. static inline int cam_smmu_get_multiregion_client_dev_idx(
  551. struct cam_context_bank_info *cb_info, int32_t multi_client_device_idx,
  552. int32_t region_id, int32_t *region_idx)
  553. {
  554. if (cb_info->num_multi_regions && multi_client_device_idx) {
  555. *region_idx = multi_client_device_idx;
  556. return cam_smmu_validate_nested_region_idx(cb_info, region_idx, region_id);
  557. }
  558. return 0;
  559. }
  560. static void cam_smmu_page_fault_work(struct work_struct *work)
  561. {
  562. int j;
  563. int idx;
  564. struct cam_smmu_work_payload *payload;
  565. uint32_t buf_info = 0;
  566. struct cam_smmu_pf_info pf_info;
  567. bool in_map = false;
  568. mutex_lock(&iommu_cb_set.payload_list_lock);
  569. if (list_empty(&iommu_cb_set.payload_list)) {
  570. CAM_ERR(CAM_SMMU, "Payload list empty");
  571. mutex_unlock(&iommu_cb_set.payload_list_lock);
  572. return;
  573. }
  574. payload = list_first_entry(&iommu_cb_set.payload_list,
  575. struct cam_smmu_work_payload,
  576. list);
  577. list_del(&payload->list);
  578. mutex_unlock(&iommu_cb_set.payload_list_lock);
  579. cam_check_iommu_faults(payload->domain, &pf_info);
  580. /* Dereference the payload to call the handler */
  581. idx = payload->idx;
  582. /* If fault address is null, found closest buffer is inaccurate */
  583. if (payload->iova)
  584. buf_info = cam_smmu_find_closest_mapping(idx, (void *)payload->iova, &in_map);
  585. if (buf_info != 0)
  586. CAM_INFO(CAM_SMMU, "closest buf 0x%x idx %d", buf_info, idx);
  587. pf_info.domain = payload->domain;
  588. pf_info.dev = payload->dev;
  589. pf_info.iova = payload->iova;
  590. pf_info.flags = payload->flags;
  591. pf_info.buf_info = buf_info;
  592. pf_info.is_secure = iommu_cb_set.cb_info[idx].is_secure;
  593. pf_info.in_map_region = in_map;
  594. for (j = 0; j < CAM_SMMU_CB_MAX; j++) {
  595. if ((iommu_cb_set.cb_info[idx].handler[j])) {
  596. pf_info.token = iommu_cb_set.cb_info[idx].token[j];
  597. iommu_cb_set.cb_info[idx].handler[j](&pf_info);
  598. }
  599. }
  600. cam_smmu_dump_cb_info(idx);
  601. kfree(payload);
  602. }
  603. static void cam_smmu_dump_cb_info(int idx)
  604. {
  605. struct cam_dma_buff_info *mapping, *mapping_temp;
  606. struct cam_smmu_nested_region_info *nested_reg_info;
  607. size_t shared_reg_len = 0, io_reg_len = 0;
  608. size_t shared_free_len = 0, io_free_len = 0;
  609. int32_t i = 0, j;
  610. uint64_t ms, hrs, min, sec;
  611. struct timespec64 current_ts;
  612. struct cam_context_bank_info *cb_info =
  613. &iommu_cb_set.cb_info[idx];
  614. if (cb_info->shared_support) {
  615. for (j = 0; j < cb_info->shared_info.num_regions; j++) {
  616. nested_reg_info = &cb_info->shared_info.nested_regions[j];
  617. shared_reg_len += nested_reg_info->region_info.iova_len;
  618. }
  619. shared_free_len = shared_reg_len - cb_info->shared_mapping_size;
  620. }
  621. if (cb_info->io_support) {
  622. for (j = 0; j < cb_info->shared_info.num_regions; j++) {
  623. nested_reg_info = &cb_info->io_info.nested_regions[j];
  624. io_reg_len += nested_reg_info->region_info.iova_len;
  625. }
  626. io_free_len = io_reg_len - cb_info->io_mapping_size;
  627. }
  628. CAM_GET_TIMESTAMP(current_ts);
  629. CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
  630. CAM_ERR(CAM_SMMU,
  631. "********** %llu:%llu:%llu:%llu Context bank dump for %s **********",
  632. hrs, min, sec, ms, cb_info->name[0]);
  633. CAM_ERR(CAM_SMMU,
  634. "Usage: shared_usage=%lu io_usage=%lu shared_free=%lu io_free=%lu",
  635. cb_info->shared_mapping_size, cb_info->io_mapping_size,
  636. shared_free_len, io_free_len);
  637. if (iommu_cb_set.debug_cfg.cb_dump_enable) {
  638. list_for_each_entry_safe(mapping, mapping_temp,
  639. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  640. i++;
  641. CAM_CONVERT_TIMESTAMP_FORMAT(mapping->ts, hrs, min, sec, ms);
  642. CAM_ERR(CAM_SMMU,
  643. "%llu:%llu:%llu:%llu: %u ion_fd=%d i_ino=%lu start=0x%lx end=0x%lx len=%lu region=%d",
  644. hrs, min, sec, ms, i, mapping->ion_fd, mapping->i_ino,
  645. mapping->paddr,
  646. ((uint64_t)mapping->paddr + (uint64_t)mapping->len),
  647. mapping->len, mapping->region_id);
  648. }
  649. cam_smmu_dump_monitor_array(&iommu_cb_set.cb_info[idx]);
  650. }
  651. }
  652. static void cam_smmu_print_user_list(int idx)
  653. {
  654. struct cam_dma_buff_info *mapping;
  655. CAM_ERR(CAM_SMMU, "index = %d", idx);
  656. list_for_each_entry(mapping,
  657. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  658. CAM_ERR(CAM_SMMU,
  659. "ion_fd = %d, i_ino=%lu, paddr= 0x%lx, len = %lu, region = %d",
  660. mapping->ion_fd, mapping->i_ino, mapping->paddr, mapping->len,
  661. mapping->region_id);
  662. }
  663. }
  664. static void cam_smmu_print_kernel_list(int idx)
  665. {
  666. struct cam_dma_buff_info *mapping;
  667. CAM_ERR(CAM_SMMU, "index = %d", idx);
  668. list_for_each_entry(mapping,
  669. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  670. CAM_ERR(CAM_SMMU,
  671. "dma_buf = %pK, i_ino = %lu, paddr= 0x%lx, len = %lu, region = %d",
  672. mapping->buf, mapping->i_ino, mapping->paddr,
  673. mapping->len, mapping->region_id);
  674. }
  675. }
  676. static void cam_smmu_print_table(void)
  677. {
  678. int i, j;
  679. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  680. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++) {
  681. CAM_ERR(CAM_SMMU,
  682. "i= %d, handle= %d, name_addr=%pK name %s",
  683. i, (int)iommu_cb_set.cb_info[i].handle,
  684. (void *)iommu_cb_set.cb_info[i].name[j],
  685. iommu_cb_set.cb_info[i].name[j]);
  686. }
  687. CAM_ERR(CAM_SMMU, "dev = %pK", iommu_cb_set.cb_info[i].dev);
  688. }
  689. }
  690. static uint32_t cam_smmu_find_closest_mapping(int idx, void *vaddr, bool *in_map_region)
  691. {
  692. struct cam_dma_buff_info *mapping, *closest_mapping = NULL;
  693. unsigned long start_addr, end_addr, current_addr;
  694. uint32_t buf_info = 0;
  695. long delta = 0, lowest_delta = 0;
  696. current_addr = (unsigned long)vaddr;
  697. *in_map_region = false;
  698. list_for_each_entry(mapping,
  699. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  700. start_addr = (unsigned long)mapping->paddr;
  701. end_addr = (unsigned long)mapping->paddr + mapping->len;
  702. if (start_addr <= current_addr && current_addr <= end_addr) {
  703. closest_mapping = mapping;
  704. CAM_INFO(CAM_SMMU,
  705. "Found va 0x%lx in:0x%lx-0x%lx, fd %d i_ino %lu cb:%s",
  706. current_addr, start_addr,
  707. end_addr, mapping->ion_fd, mapping->i_ino,
  708. iommu_cb_set.cb_info[idx].name[0]);
  709. goto end;
  710. } else {
  711. if (start_addr > current_addr)
  712. delta = start_addr - current_addr;
  713. else
  714. delta = current_addr - end_addr - 1;
  715. if (delta < lowest_delta || lowest_delta == 0) {
  716. lowest_delta = delta;
  717. closest_mapping = mapping;
  718. }
  719. CAM_DBG(CAM_SMMU,
  720. "approx va %lx not in range: %lx-%lx fd = %0x i_ino %lu",
  721. current_addr, start_addr,
  722. end_addr, mapping->ion_fd, mapping->i_ino);
  723. }
  724. }
  725. end:
  726. if (closest_mapping) {
  727. buf_info = closest_mapping->ion_fd;
  728. start_addr = (unsigned long)closest_mapping->paddr;
  729. end_addr = (unsigned long)closest_mapping->paddr + closest_mapping->len;
  730. if (start_addr <= current_addr && current_addr < end_addr)
  731. *in_map_region = true;
  732. CAM_INFO(CAM_SMMU,
  733. "Faulting addr 0x%lx closest map fd %d i_ino %lu %llu-%llu 0x%lx-0x%lx buf=%pK",
  734. current_addr, closest_mapping->ion_fd, closest_mapping->i_ino,
  735. mapping->len, closest_mapping->len,
  736. (unsigned long)closest_mapping->paddr,
  737. (unsigned long)closest_mapping->paddr + closest_mapping->len,
  738. closest_mapping->buf);
  739. } else
  740. CAM_ERR(CAM_SMMU,
  741. "Cannot find vaddr:%lx in SMMU %s virt address",
  742. current_addr, iommu_cb_set.cb_info[idx].name[0]);
  743. return buf_info;
  744. }
  745. void cam_smmu_set_client_page_fault_handler(int handle,
  746. void (*handler_cb)(struct cam_smmu_pf_info *pf_info), void *token)
  747. {
  748. int idx, i = 0;
  749. if (!token || (handle == HANDLE_INIT)) {
  750. CAM_ERR(CAM_SMMU, "Error: token is NULL or invalid handle");
  751. return;
  752. }
  753. idx = GET_SMMU_TABLE_IDX(handle);
  754. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  755. CAM_ERR(CAM_SMMU,
  756. "Error: handle or index invalid. idx = %d hdl = %x",
  757. idx, handle);
  758. return;
  759. }
  760. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  761. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  762. CAM_ERR(CAM_SMMU,
  763. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  764. iommu_cb_set.cb_info[idx].handle, handle);
  765. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  766. return;
  767. }
  768. if (handler_cb) {
  769. if (iommu_cb_set.cb_info[idx].cb_count == CAM_SMMU_CB_MAX) {
  770. CAM_ERR(CAM_SMMU,
  771. "%s Should not regiester more handlers",
  772. iommu_cb_set.cb_info[idx].name[0]);
  773. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  774. return;
  775. }
  776. iommu_cb_set.cb_info[idx].cb_count++;
  777. for (i = 0; i < iommu_cb_set.cb_info[idx].cb_count; i++) {
  778. if (iommu_cb_set.cb_info[idx].token[i] == NULL) {
  779. iommu_cb_set.cb_info[idx].token[i] = token;
  780. iommu_cb_set.cb_info[idx].handler[i] =
  781. handler_cb;
  782. break;
  783. }
  784. }
  785. } else {
  786. for (i = 0; i < CAM_SMMU_CB_MAX; i++) {
  787. if (iommu_cb_set.cb_info[idx].token[i] == token) {
  788. iommu_cb_set.cb_info[idx].token[i] = NULL;
  789. iommu_cb_set.cb_info[idx].handler[i] =
  790. NULL;
  791. iommu_cb_set.cb_info[idx].cb_count--;
  792. break;
  793. }
  794. }
  795. if (i == CAM_SMMU_CB_MAX)
  796. CAM_ERR(CAM_SMMU,
  797. "Error: hdl %x no matching tokens: %s",
  798. handle, iommu_cb_set.cb_info[idx].name[0]);
  799. }
  800. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  801. }
  802. void cam_smmu_unset_client_page_fault_handler(int handle, void *token)
  803. {
  804. int idx, i = 0;
  805. if (!token || (handle == HANDLE_INIT)) {
  806. CAM_ERR(CAM_SMMU, "Error: token is NULL or invalid handle");
  807. return;
  808. }
  809. idx = GET_SMMU_TABLE_IDX(handle);
  810. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  811. CAM_ERR(CAM_SMMU,
  812. "Error: handle or index invalid. idx = %d hdl = %x",
  813. idx, handle);
  814. return;
  815. }
  816. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  817. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  818. CAM_ERR(CAM_SMMU,
  819. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  820. iommu_cb_set.cb_info[idx].handle, handle);
  821. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  822. return;
  823. }
  824. for (i = 0; i < CAM_SMMU_CB_MAX; i++) {
  825. if (iommu_cb_set.cb_info[idx].token[i] == token) {
  826. iommu_cb_set.cb_info[idx].token[i] = NULL;
  827. iommu_cb_set.cb_info[idx].handler[i] =
  828. NULL;
  829. iommu_cb_set.cb_info[idx].cb_count--;
  830. break;
  831. }
  832. }
  833. if (i == CAM_SMMU_CB_MAX)
  834. CAM_ERR(CAM_SMMU, "Error: hdl %x no matching tokens: %s",
  835. handle, iommu_cb_set.cb_info[idx].name[0]);
  836. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  837. }
  838. static int cam_smmu_iommu_fault_handler(struct iommu_domain *domain,
  839. struct device *dev, unsigned long iova,
  840. int flags, void *token)
  841. {
  842. char *cb_name;
  843. int idx;
  844. struct cam_smmu_work_payload *payload;
  845. if (!token) {
  846. CAM_ERR(CAM_SMMU,
  847. "token is NULL, domain = %pK, device = %pK,iova = 0x%lx, flags = 0x%x",
  848. domain, dev, iova, flags);
  849. return 0;
  850. }
  851. cb_name = (char *)token;
  852. /* Check whether it is in the table */
  853. for (idx = 0; idx < iommu_cb_set.cb_num; idx++) {
  854. if (!strcmp(iommu_cb_set.cb_info[idx].name[0], cb_name))
  855. break;
  856. }
  857. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  858. CAM_ERR(CAM_SMMU,
  859. "index is invalid, index = %d, token = %s, cb_num = %s",
  860. idx, cb_name, iommu_cb_set.cb_num);
  861. return 0;
  862. }
  863. if (++iommu_cb_set.cb_info[idx].pf_count > g_num_pf_handled) {
  864. CAM_INFO_RATE_LIMIT(CAM_SMMU, "PF already handled %d %d %d",
  865. g_num_pf_handled, idx,
  866. iommu_cb_set.cb_info[idx].pf_count);
  867. return 0;
  868. }
  869. payload = kzalloc(sizeof(struct cam_smmu_work_payload), GFP_ATOMIC);
  870. if (!payload)
  871. return 0;
  872. payload->domain = domain;
  873. payload->dev = dev;
  874. payload->iova = iova;
  875. payload->flags = flags;
  876. payload->token = token;
  877. payload->idx = idx;
  878. mutex_lock(&iommu_cb_set.payload_list_lock);
  879. list_add_tail(&payload->list, &iommu_cb_set.payload_list);
  880. mutex_unlock(&iommu_cb_set.payload_list_lock);
  881. cam_smmu_page_fault_work(&iommu_cb_set.smmu_work);
  882. /*
  883. * If cb has faults marked as non-fatal, but if the debugfs is set for panic
  884. * trigger a panic on fault
  885. */
  886. if (iommu_cb_set.cb_info[idx].non_fatal_faults_en) {
  887. if (iommu_cb_set.debug_cfg.fatal_pf_mask & BIT(idx))
  888. CAM_TRIGGER_PANIC("SMMU context fault from soc: %s[cb_idx: %u]",
  889. iommu_cb_set.cb_info[idx].name[0], idx);
  890. }
  891. return -ENOSYS;
  892. }
  893. int cam_smmu_is_cb_non_fatal_fault_en(int smmu_hdl, bool *non_fatal_en)
  894. {
  895. int idx;
  896. if (smmu_hdl == HANDLE_INIT) {
  897. CAM_ERR(CAM_SMMU, "Invalid iommu handle %d", smmu_hdl);
  898. return -EINVAL;
  899. }
  900. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  901. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  902. CAM_ERR(CAM_SMMU,
  903. "Invalid handle or idx. idx: %d, hdl: 0x%x", idx, smmu_hdl);
  904. return -EINVAL;
  905. }
  906. *non_fatal_en = iommu_cb_set.cb_info[idx].non_fatal_faults_en;
  907. return 0;
  908. }
  909. void cam_smmu_reset_cb_page_fault_cnt(void)
  910. {
  911. int idx;
  912. for (idx = 0; idx < iommu_cb_set.cb_num; idx++)
  913. iommu_cb_set.cb_info[idx].pf_count = 0;
  914. }
  915. static int cam_smmu_translate_dir_to_iommu_dir(
  916. enum cam_smmu_map_dir dir)
  917. {
  918. switch (dir) {
  919. case CAM_SMMU_MAP_READ:
  920. return IOMMU_READ;
  921. case CAM_SMMU_MAP_WRITE:
  922. return IOMMU_WRITE;
  923. case CAM_SMMU_MAP_RW:
  924. return IOMMU_READ|IOMMU_WRITE;
  925. case CAM_SMMU_MAP_INVALID:
  926. default:
  927. CAM_ERR(CAM_SMMU, "Error: Direction is invalid. dir = %d", dir);
  928. break;
  929. }
  930. return IOMMU_INVALID_DIR;
  931. }
  932. static enum dma_data_direction cam_smmu_translate_dir(
  933. enum cam_smmu_map_dir dir)
  934. {
  935. switch (dir) {
  936. case CAM_SMMU_MAP_READ:
  937. return DMA_FROM_DEVICE;
  938. case CAM_SMMU_MAP_WRITE:
  939. return DMA_TO_DEVICE;
  940. case CAM_SMMU_MAP_RW:
  941. return DMA_BIDIRECTIONAL;
  942. case CAM_SMMU_MAP_INVALID:
  943. default:
  944. CAM_ERR(CAM_SMMU, "Error: Direction is invalid. dir = %d",
  945. (int)dir);
  946. break;
  947. }
  948. return DMA_NONE;
  949. }
  950. void cam_smmu_reset_iommu_table(enum cam_smmu_init_dir ops)
  951. {
  952. unsigned int i;
  953. int j = 0;
  954. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  955. if (ops == CAM_SMMU_TABLE_INIT)
  956. mutex_init(&iommu_cb_set.cb_info[i].lock);
  957. mutex_lock(&iommu_cb_set.cb_info[i].lock);
  958. iommu_cb_set.cb_info[i].handle = HANDLE_INIT;
  959. INIT_LIST_HEAD(&iommu_cb_set.cb_info[i].smmu_buf_list);
  960. INIT_LIST_HEAD(&iommu_cb_set.cb_info[i].smmu_buf_kernel_list);
  961. iommu_cb_set.cb_info[i].state = CAM_SMMU_DETACH;
  962. iommu_cb_set.cb_info[i].dev = NULL;
  963. iommu_cb_set.cb_info[i].cb_count = 0;
  964. iommu_cb_set.cb_info[i].pf_count = 0;
  965. for (j = 0; j < CAM_SMMU_CB_MAX; j++) {
  966. iommu_cb_set.cb_info[i].token[j] = NULL;
  967. iommu_cb_set.cb_info[i].handler[j] = NULL;
  968. }
  969. mutex_unlock(&iommu_cb_set.cb_info[i].lock);
  970. if (ops != CAM_SMMU_TABLE_INIT)
  971. mutex_destroy(&iommu_cb_set.cb_info[i].lock);
  972. }
  973. }
  974. static bool cam_smmu_is_hdl_nonunique_or_null(int hdl)
  975. {
  976. int i;
  977. if ((hdl == HANDLE_INIT) || (!hdl)) {
  978. CAM_DBG(CAM_SMMU, "iommu handle: %d is not valid", hdl);
  979. return 1;
  980. }
  981. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  982. if (iommu_cb_set.cb_info[i].handle == HANDLE_INIT)
  983. continue;
  984. if (iommu_cb_set.cb_info[i].handle == hdl) {
  985. CAM_DBG(CAM_SMMU, "iommu handle %d conflicts",
  986. (int)hdl);
  987. return 1;
  988. }
  989. }
  990. return 0;
  991. }
  992. /**
  993. * use low 2 bytes for handle cookie
  994. */
  995. static int cam_smmu_create_iommu_handle(int idx)
  996. {
  997. int rand, hdl = 0;
  998. get_random_bytes(&rand, COOKIE_NUM_BYTE);
  999. hdl = GET_SMMU_HDL(idx, rand);
  1000. CAM_DBG(CAM_SMMU, "create handle value = %x", (int)hdl);
  1001. return hdl;
  1002. }
  1003. static int cam_smmu_attach_device(int idx)
  1004. {
  1005. int rc;
  1006. struct cam_context_bank_info *cb = &iommu_cb_set.cb_info[idx];
  1007. /* attach the mapping to device */
  1008. rc = iommu_attach_device(cb->domain, cb->dev);
  1009. if (rc < 0) {
  1010. CAM_ERR(CAM_SMMU, "Error: ARM IOMMU attach failed. ret = %d",
  1011. rc);
  1012. rc = -ENODEV;
  1013. }
  1014. return rc;
  1015. }
  1016. static inline void cam_smmu_update_multiregion_dev_id(
  1017. struct cam_context_bank_info *cb_info, char *name,
  1018. int *hdl)
  1019. {
  1020. int k;
  1021. for (k = 0; k < cb_info->num_multi_regions; k++) {
  1022. if (!strcmp(cb_info->multi_region_clients[k], name)) {
  1023. *hdl |= ((k + 1) << MULTI_CLIENT_REGION_SHIFT);
  1024. CAM_DBG(CAM_SMMU, "%s got shared multi region handle 0x%x",
  1025. name, *hdl);
  1026. }
  1027. }
  1028. }
  1029. static int cam_smmu_create_add_handle_in_table(char *name,
  1030. int *hdl)
  1031. {
  1032. int i, j, rc = -EINVAL;
  1033. int handle;
  1034. /* create handle and add in the iommu hardware table */
  1035. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  1036. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++) {
  1037. if (strcmp(iommu_cb_set.cb_info[i].name[j], name))
  1038. continue;
  1039. if (iommu_cb_set.cb_info[i].handle == HANDLE_INIT) {
  1040. mutex_lock(&iommu_cb_set.cb_info[i].lock);
  1041. /* make sure handle is unique and non-zero*/
  1042. do {
  1043. handle =
  1044. cam_smmu_create_iommu_handle(i);
  1045. } while (cam_smmu_is_hdl_nonunique_or_null(
  1046. handle));
  1047. /* put handle in the table */
  1048. iommu_cb_set.cb_info[i].handle = handle;
  1049. iommu_cb_set.cb_info[i].cb_count = 0;
  1050. if (iommu_cb_set.cb_info[i].is_secure)
  1051. iommu_cb_set.cb_info[i].secure_count++;
  1052. if (iommu_cb_set.cb_info[i].is_mul_client)
  1053. iommu_cb_set.cb_info[i].device_count++;
  1054. *hdl = handle;
  1055. CAM_DBG(CAM_SMMU, "%s creates handle 0x%x",
  1056. name, handle);
  1057. mutex_unlock(&iommu_cb_set.cb_info[i].lock);
  1058. rc = 0;
  1059. goto end;
  1060. } else {
  1061. mutex_lock(&iommu_cb_set.cb_info[i].lock);
  1062. if (iommu_cb_set.cb_info[i].is_secure) {
  1063. iommu_cb_set.cb_info[i].secure_count++;
  1064. *hdl = iommu_cb_set.cb_info[i].handle;
  1065. mutex_unlock(
  1066. &iommu_cb_set.cb_info[i].lock);
  1067. return 0;
  1068. }
  1069. if (iommu_cb_set.cb_info[i].is_mul_client) {
  1070. iommu_cb_set.cb_info[i].device_count++;
  1071. *hdl = iommu_cb_set.cb_info[i].handle;
  1072. if (iommu_cb_set.cb_info[i].num_multi_regions) {
  1073. cam_smmu_update_multiregion_dev_id(
  1074. &iommu_cb_set.cb_info[i], name, hdl);
  1075. }
  1076. mutex_unlock(
  1077. &iommu_cb_set.cb_info[i].lock);
  1078. CAM_DBG(CAM_SMMU,
  1079. "%s already got handle 0x%x cb_handle 0x%x",
  1080. name, *hdl, iommu_cb_set.cb_info[i].handle);
  1081. return 0;
  1082. }
  1083. CAM_ERR(CAM_SMMU,
  1084. "Error: %s already got handle 0x%x",
  1085. name, iommu_cb_set.cb_info[i].handle);
  1086. mutex_unlock(&iommu_cb_set.cb_info[i].lock);
  1087. rc = -EALREADY;
  1088. goto end;
  1089. }
  1090. }
  1091. }
  1092. CAM_ERR(CAM_SMMU, "Error: Cannot find name %s or all handle exist",
  1093. name);
  1094. cam_smmu_print_table();
  1095. end:
  1096. return rc;
  1097. }
  1098. static int cam_smmu_init_scratch_map(struct scratch_mapping *scratch_map,
  1099. dma_addr_t base, size_t size,
  1100. int order)
  1101. {
  1102. unsigned int count = size >> (PAGE_SHIFT + order);
  1103. unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
  1104. int err = 0;
  1105. if (!count) {
  1106. err = -EINVAL;
  1107. CAM_ERR(CAM_SMMU, "Page count is zero, size passed = %zu",
  1108. size);
  1109. goto bail;
  1110. }
  1111. scratch_map->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1112. if (!scratch_map->bitmap) {
  1113. err = -ENOMEM;
  1114. goto bail;
  1115. }
  1116. scratch_map->base = base;
  1117. scratch_map->bits = BITS_PER_BYTE * bitmap_size;
  1118. scratch_map->order = order;
  1119. bail:
  1120. return err;
  1121. }
  1122. static int cam_smmu_alloc_scratch_va(struct scratch_mapping *mapping,
  1123. size_t size,
  1124. dma_addr_t *iova)
  1125. {
  1126. unsigned int order = get_order(size);
  1127. unsigned int align = 0;
  1128. unsigned int count, start;
  1129. count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
  1130. (1 << mapping->order) - 1) >> mapping->order;
  1131. /*
  1132. * Transparently, add a guard page to the total count of pages
  1133. * to be allocated
  1134. */
  1135. count++;
  1136. if (order > mapping->order)
  1137. align = (1 << (order - mapping->order)) - 1;
  1138. start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
  1139. count, align);
  1140. if (start > mapping->bits)
  1141. return -ENOMEM;
  1142. bitmap_set(mapping->bitmap, start, count);
  1143. *iova = mapping->base + (start << (mapping->order + PAGE_SHIFT));
  1144. return 0;
  1145. }
  1146. static int cam_smmu_free_scratch_va(struct scratch_mapping *mapping,
  1147. dma_addr_t addr, size_t size)
  1148. {
  1149. unsigned int start = (addr - mapping->base) >>
  1150. (mapping->order + PAGE_SHIFT);
  1151. unsigned int count = ((size >> PAGE_SHIFT) +
  1152. (1 << mapping->order) - 1) >> mapping->order;
  1153. if (!addr) {
  1154. CAM_ERR(CAM_SMMU, "Error: Invalid address");
  1155. return -EINVAL;
  1156. }
  1157. if (start + count > mapping->bits) {
  1158. CAM_ERR(CAM_SMMU, "Error: Invalid page bits in scratch map");
  1159. return -EINVAL;
  1160. }
  1161. /*
  1162. * Transparently, add a guard page to the total count of pages
  1163. * to be freed
  1164. */
  1165. count++;
  1166. bitmap_clear(mapping->bitmap, start, count);
  1167. return 0;
  1168. }
  1169. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_virt_address(int idx,
  1170. dma_addr_t virt_addr)
  1171. {
  1172. struct cam_dma_buff_info *mapping;
  1173. list_for_each_entry(mapping, &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1174. list) {
  1175. if (mapping->paddr == virt_addr) {
  1176. CAM_DBG(CAM_SMMU, "Found virtual address %lx",
  1177. (unsigned long)virt_addr);
  1178. return mapping;
  1179. }
  1180. }
  1181. CAM_ERR(CAM_SMMU, "Error: Cannot find virtual address %lx by index %d",
  1182. (unsigned long)virt_addr, idx);
  1183. return NULL;
  1184. }
  1185. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_ion_index(int idx,
  1186. int ion_fd, struct dma_buf *dmabuf)
  1187. {
  1188. struct cam_dma_buff_info *mapping;
  1189. unsigned long i_ino;
  1190. if (ion_fd < 0) {
  1191. CAM_ERR(CAM_SMMU, "Invalid fd %d", ion_fd);
  1192. return NULL;
  1193. }
  1194. i_ino = file_inode(dmabuf->file)->i_ino;
  1195. list_for_each_entry(mapping,
  1196. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1197. list) {
  1198. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  1199. CAM_DBG(CAM_SMMU, "find ion_fd %d i_ino %lu", ion_fd, i_ino);
  1200. return mapping;
  1201. }
  1202. }
  1203. CAM_ERR(CAM_SMMU, "Error: Cannot find entry by index %d, fd %d i_ino %lu",
  1204. idx, ion_fd, i_ino);
  1205. return NULL;
  1206. }
  1207. static struct cam_dma_buff_info *cam_smmu_find_mapping_by_dma_buf(int idx,
  1208. struct dma_buf *buf)
  1209. {
  1210. struct cam_dma_buff_info *mapping;
  1211. if (!buf) {
  1212. CAM_ERR(CAM_SMMU, "Invalid dma_buf");
  1213. return NULL;
  1214. }
  1215. list_for_each_entry(mapping,
  1216. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list,
  1217. list) {
  1218. if (mapping->buf == buf) {
  1219. CAM_DBG(CAM_SMMU, "find dma_buf %pK", buf);
  1220. return mapping;
  1221. }
  1222. }
  1223. CAM_ERR(CAM_SMMU, "Error: Cannot find entry by index %d", idx);
  1224. return NULL;
  1225. }
  1226. static struct cam_sec_buff_info *cam_smmu_find_mapping_by_sec_buf_idx(int idx,
  1227. int ion_fd, struct dma_buf *dmabuf)
  1228. {
  1229. struct cam_sec_buff_info *mapping;
  1230. unsigned long i_ino;
  1231. i_ino = file_inode(dmabuf->file)->i_ino;
  1232. list_for_each_entry(mapping, &iommu_cb_set.cb_info[idx].smmu_buf_list,
  1233. list) {
  1234. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  1235. CAM_DBG(CAM_SMMU, "find ion_fd %d, i_ino %lu", ion_fd, i_ino);
  1236. return mapping;
  1237. }
  1238. }
  1239. CAM_ERR(CAM_SMMU, "Error: Cannot find fd %d i_ino %lu by index %d",
  1240. ion_fd, i_ino, idx);
  1241. return NULL;
  1242. }
  1243. static void cam_smmu_clean_user_buffer_list(int idx)
  1244. {
  1245. int ret;
  1246. struct cam_dma_buff_info *mapping_info, *temp;
  1247. list_for_each_entry_safe(mapping_info, temp,
  1248. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  1249. CAM_DBG(CAM_SMMU, "Free mapping address %pK, i = %d, fd = %d, i_ino = %lu",
  1250. (void *)mapping_info->paddr, idx,
  1251. mapping_info->ion_fd, mapping_info->i_ino);
  1252. if (mapping_info->ion_fd == 0xDEADBEEF)
  1253. /* Clean up scratch buffers */
  1254. ret = cam_smmu_free_scratch_buffer_remove_from_list(
  1255. mapping_info, idx);
  1256. else
  1257. /* Clean up regular mapped buffers */
  1258. ret = cam_smmu_unmap_buf_and_remove_from_list(
  1259. mapping_info,
  1260. idx);
  1261. if (ret < 0) {
  1262. CAM_ERR(CAM_SMMU, "Buffer delete failed: idx = %d",
  1263. idx);
  1264. CAM_ERR(CAM_SMMU,
  1265. "Buffer delete failed: addr = 0x%lx, fd = %d, i_ino = %lu",
  1266. mapping_info->paddr,
  1267. mapping_info->ion_fd, mapping_info->i_ino);
  1268. /*
  1269. * Ignore this error and continue to delete other
  1270. * buffers in the list
  1271. */
  1272. continue;
  1273. }
  1274. }
  1275. }
  1276. static void cam_smmu_clean_kernel_buffer_list(int idx)
  1277. {
  1278. int ret;
  1279. struct cam_dma_buff_info *mapping_info, *temp;
  1280. list_for_each_entry_safe(mapping_info, temp,
  1281. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  1282. CAM_DBG(CAM_SMMU,
  1283. "Free mapping address %pK, i = %d, dma_buf = %pK",
  1284. (void *)mapping_info->paddr, idx,
  1285. mapping_info->buf);
  1286. /* Clean up regular mapped buffers */
  1287. ret = cam_smmu_unmap_buf_and_remove_from_list(
  1288. mapping_info,
  1289. idx);
  1290. if (ret < 0) {
  1291. CAM_ERR(CAM_SMMU,
  1292. "Buffer delete in kernel list failed: idx = %d",
  1293. idx);
  1294. CAM_ERR(CAM_SMMU,
  1295. "Buffer delete failed: addr = 0x%lx, dma_buf = %pK",
  1296. mapping_info->paddr, mapping_info->buf);
  1297. /*
  1298. * Ignore this error and continue to delete other
  1299. * buffers in the list
  1300. */
  1301. continue;
  1302. }
  1303. }
  1304. }
  1305. static int cam_smmu_attach(int idx)
  1306. {
  1307. int ret;
  1308. if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_ATTACH) {
  1309. ret = -EALREADY;
  1310. } else if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_DETACH) {
  1311. ret = cam_smmu_attach_device(idx);
  1312. if (ret < 0) {
  1313. CAM_ERR(CAM_SMMU, "Error: ATTACH fail");
  1314. return -ENODEV;
  1315. }
  1316. iommu_cb_set.cb_info[idx].state = CAM_SMMU_ATTACH;
  1317. ret = 0;
  1318. } else {
  1319. CAM_ERR(CAM_SMMU, "Error: Not detach/attach: %d",
  1320. iommu_cb_set.cb_info[idx].state);
  1321. ret = -EINVAL;
  1322. }
  1323. return ret;
  1324. }
  1325. static int cam_smmu_detach_device(int idx)
  1326. {
  1327. int rc = 0;
  1328. struct cam_context_bank_info *cb = &iommu_cb_set.cb_info[idx];
  1329. /* detach the mapping to device if not already detached */
  1330. if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_DETACH) {
  1331. rc = -EALREADY;
  1332. } else if (iommu_cb_set.cb_info[idx].state == CAM_SMMU_ATTACH) {
  1333. iommu_detach_device(cb->domain, cb->dev);
  1334. iommu_cb_set.cb_info[idx].state = CAM_SMMU_DETACH;
  1335. }
  1336. return rc;
  1337. }
  1338. static int cam_smmu_alloc_iova(size_t size,
  1339. int32_t smmu_hdl, unsigned long *iova)
  1340. {
  1341. int rc = 0, shared_mem_pool_idx = 0;
  1342. int idx, multi_client_device_idx;
  1343. unsigned long vaddr = 0;
  1344. if (!iova || !size || (smmu_hdl == HANDLE_INIT)) {
  1345. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1346. return -EINVAL;
  1347. }
  1348. CAM_DBG(CAM_SMMU, "Allocating iova size = %zu for smmu hdl=%X",
  1349. size, smmu_hdl);
  1350. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1351. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1352. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1353. CAM_ERR(CAM_SMMU,
  1354. "Error: handle or index invalid. idx = %d hdl = %x",
  1355. idx, smmu_hdl);
  1356. return -EINVAL;
  1357. }
  1358. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, smmu_hdl)) {
  1359. CAM_ERR(CAM_SMMU,
  1360. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1361. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1362. rc = -EINVAL;
  1363. goto get_addr_end;
  1364. }
  1365. if (!iommu_cb_set.cb_info[idx].shared_support) {
  1366. CAM_ERR(CAM_SMMU,
  1367. "Error: Shared memory not supported for hdl = %X",
  1368. smmu_hdl);
  1369. rc = -EINVAL;
  1370. goto get_addr_end;
  1371. }
  1372. rc = cam_smmu_get_multiregion_client_dev_idx(&iommu_cb_set.cb_info[idx],
  1373. multi_client_device_idx, CAM_SMMU_REGION_SHARED, &shared_mem_pool_idx);
  1374. if (rc)
  1375. goto get_addr_end;
  1376. vaddr = gen_pool_alloc(
  1377. iommu_cb_set.cb_info[idx].shared_mem_pool[shared_mem_pool_idx], size);
  1378. if (!vaddr)
  1379. return -ENOMEM;
  1380. *iova = vaddr;
  1381. get_addr_end:
  1382. return rc;
  1383. }
  1384. static int cam_smmu_free_iova(unsigned long iova, size_t size,
  1385. int32_t smmu_hdl)
  1386. {
  1387. int rc = 0, idx, multi_client_device_idx;
  1388. int shared_mem_pool_idx = 0;
  1389. if (!size || (smmu_hdl == HANDLE_INIT)) {
  1390. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1391. return -EINVAL;
  1392. }
  1393. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1394. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1395. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1396. CAM_ERR(CAM_SMMU,
  1397. "Error: handle or index invalid. idx = %d hdl = %x",
  1398. idx, smmu_hdl);
  1399. return -EINVAL;
  1400. }
  1401. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, smmu_hdl)) {
  1402. CAM_ERR(CAM_SMMU,
  1403. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1404. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1405. rc = -EINVAL;
  1406. goto get_addr_end;
  1407. }
  1408. rc = cam_smmu_get_multiregion_client_dev_idx(&iommu_cb_set.cb_info[idx],
  1409. multi_client_device_idx, CAM_SMMU_REGION_SHARED, &shared_mem_pool_idx);
  1410. if (rc)
  1411. goto get_addr_end;
  1412. gen_pool_free(iommu_cb_set.cb_info[idx].shared_mem_pool[shared_mem_pool_idx],
  1413. iova, size);
  1414. get_addr_end:
  1415. return rc;
  1416. }
  1417. int cam_smmu_alloc_firmware(int32_t smmu_hdl,
  1418. dma_addr_t *iova,
  1419. uintptr_t *cpuva,
  1420. size_t *len)
  1421. {
  1422. int rc;
  1423. int32_t idx;
  1424. size_t firmware_len = 0;
  1425. size_t firmware_start = 0;
  1426. struct iommu_domain *domain;
  1427. if (!iova || !len || !cpuva || (smmu_hdl == HANDLE_INIT)) {
  1428. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1429. return -EINVAL;
  1430. }
  1431. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1432. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1433. CAM_ERR(CAM_SMMU,
  1434. "Error: handle or index invalid. idx = %d hdl = %x",
  1435. idx, smmu_hdl);
  1436. rc = -EINVAL;
  1437. goto end;
  1438. }
  1439. if (!iommu_cb_set.cb_info[idx].firmware_support) {
  1440. CAM_ERR(CAM_SMMU,
  1441. "Firmware memory not supported for this SMMU handle");
  1442. rc = -EINVAL;
  1443. goto end;
  1444. }
  1445. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1446. if (iommu_cb_set.cb_info[idx].is_fw_allocated) {
  1447. CAM_ERR(CAM_SMMU, "Trying to allocate twice");
  1448. rc = -ENOMEM;
  1449. goto unlock_and_end;
  1450. }
  1451. firmware_len = iommu_cb_set.cb_info[idx].firmware_info.iova_len;
  1452. firmware_start = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1453. CAM_DBG(CAM_SMMU, "Firmware area len from DT = %zu", firmware_len);
  1454. rc = cam_reserve_icp_fw(&icp_fw, firmware_len);
  1455. if (rc)
  1456. goto unlock_and_end;
  1457. else
  1458. CAM_DBG(CAM_SMMU, "DMA alloc returned fw = %pK, hdl = %pK",
  1459. icp_fw.fw_kva, (void *)icp_fw.fw_hdl);
  1460. domain = iommu_cb_set.cb_info[idx].domain;
  1461. /*
  1462. * Revisit this - what should we map this with - CACHED or UNCACHED?
  1463. * chipsets using dma-coherent-hint-cached - leaving it like this is
  1464. * fine as we can map both CACHED and UNCACHED on same CB.
  1465. * But on chipsets which use dma-coherent - all the buffers that are
  1466. * being mapped to this CB must be CACHED
  1467. */
  1468. rc = iommu_map(domain,
  1469. firmware_start,
  1470. (phys_addr_t) icp_fw.fw_hdl,
  1471. firmware_len,
  1472. IOMMU_READ|IOMMU_WRITE|IOMMU_PRIV);
  1473. if (rc) {
  1474. CAM_ERR(CAM_SMMU, "Failed to map FW into IOMMU");
  1475. rc = -ENOMEM;
  1476. goto alloc_fail;
  1477. }
  1478. iommu_cb_set.cb_info[idx].is_fw_allocated = true;
  1479. *iova = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1480. *cpuva = (uintptr_t)icp_fw.fw_kva;
  1481. *len = firmware_len;
  1482. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1483. return rc;
  1484. alloc_fail:
  1485. cam_unreserve_icp_fw(&icp_fw, firmware_len);
  1486. unlock_and_end:
  1487. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1488. end:
  1489. return rc;
  1490. }
  1491. EXPORT_SYMBOL(cam_smmu_alloc_firmware);
  1492. int cam_smmu_dealloc_firmware(int32_t smmu_hdl)
  1493. {
  1494. int rc = 0;
  1495. int32_t idx;
  1496. size_t firmware_len = 0;
  1497. size_t firmware_start = 0;
  1498. struct iommu_domain *domain;
  1499. size_t unmapped = 0;
  1500. if (smmu_hdl == HANDLE_INIT) {
  1501. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  1502. return -EINVAL;
  1503. }
  1504. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1505. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1506. CAM_ERR(CAM_SMMU,
  1507. "Error: handle or index invalid. idx = %d hdl = %x",
  1508. idx, smmu_hdl);
  1509. rc = -EINVAL;
  1510. goto end;
  1511. }
  1512. if (!iommu_cb_set.cb_info[idx].firmware_support) {
  1513. CAM_ERR(CAM_SMMU,
  1514. "Firmware memory not supported for this SMMU handle");
  1515. rc = -EINVAL;
  1516. goto end;
  1517. }
  1518. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1519. if (!iommu_cb_set.cb_info[idx].is_fw_allocated) {
  1520. CAM_ERR(CAM_SMMU,
  1521. "Trying to deallocate firmware that is not allocated");
  1522. rc = -ENOMEM;
  1523. goto unlock_and_end;
  1524. }
  1525. firmware_len = iommu_cb_set.cb_info[idx].firmware_info.iova_len;
  1526. firmware_start = iommu_cb_set.cb_info[idx].firmware_info.iova_start;
  1527. domain = iommu_cb_set.cb_info[idx].domain;
  1528. unmapped = iommu_unmap(domain,
  1529. firmware_start,
  1530. firmware_len);
  1531. if (unmapped != firmware_len) {
  1532. CAM_ERR(CAM_SMMU, "Only %zu unmapped out of total %zu",
  1533. unmapped,
  1534. firmware_len);
  1535. rc = -EINVAL;
  1536. }
  1537. cam_unreserve_icp_fw(&icp_fw, firmware_len);
  1538. icp_fw.fw_kva = NULL;
  1539. icp_fw.fw_hdl = 0;
  1540. iommu_cb_set.cb_info[idx].is_fw_allocated = false;
  1541. unlock_and_end:
  1542. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1543. end:
  1544. return rc;
  1545. }
  1546. EXPORT_SYMBOL(cam_smmu_dealloc_firmware);
  1547. static int cam_smmu_retrieve_region_info(
  1548. struct cam_context_bank_info *cb_info,
  1549. uint32_t region_id, uint32_t subregion_id,
  1550. int32_t nested_reg_idx,
  1551. bool *is_supported, int32_t **ref_cnt,
  1552. unsigned long **mapping_mask, bool **is_allocated,
  1553. struct cam_smmu_region_info **region_info)
  1554. {
  1555. int rc = 0;
  1556. struct cam_smmu_subregion_info *subregion = NULL;
  1557. switch (region_id) {
  1558. case CAM_SMMU_REGION_QDSS: {
  1559. struct cam_smmu_multi_region_info *qdss_region;
  1560. qdss_region = &cb_info->qdss_info;
  1561. /* No subregion for QDSS */
  1562. *is_supported = cb_info->qdss_support;
  1563. *is_allocated = &qdss_region->nested_regions[nested_reg_idx].is_allocated;
  1564. *region_info = &qdss_region->nested_regions[nested_reg_idx].region_info;
  1565. *ref_cnt = &qdss_region->nested_regions[nested_reg_idx].mapped_refcnt;
  1566. *mapping_mask = &qdss_region->nested_regions[nested_reg_idx].mapped_client_mask;
  1567. }
  1568. break;
  1569. case CAM_SMMU_REGION_DEVICE: {
  1570. struct cam_smmu_multi_region_info *device_region;
  1571. *is_supported = cb_info->device_region_support;
  1572. device_region = &cb_info->device_region;
  1573. if (device_region->nested_regions[nested_reg_idx].subregion_support) {
  1574. subregion = cam_smmu_find_subregion(subregion_id,
  1575. device_region->nested_regions[nested_reg_idx].subregions);
  1576. if (IS_ERR_OR_NULL(subregion)) {
  1577. CAM_ERR(CAM_SMMU,
  1578. "Failed to find subregion: %d in region: %d cb: %s",
  1579. subregion, region_id, cb_info->name[0]);
  1580. rc = PTR_ERR(subregion);
  1581. goto end;
  1582. }
  1583. *is_allocated = &subregion->is_allocated;
  1584. *region_info = &subregion->subregion_info;
  1585. *ref_cnt = &subregion->mapped_refcnt;
  1586. *mapping_mask = &subregion->mapped_client_mask;
  1587. } else {
  1588. *is_allocated = &device_region->nested_regions[nested_reg_idx].is_allocated;
  1589. *region_info = &device_region->nested_regions[nested_reg_idx].region_info;
  1590. *ref_cnt = &device_region->nested_regions[nested_reg_idx].mapped_refcnt;
  1591. *mapping_mask =
  1592. &device_region->nested_regions[nested_reg_idx].mapped_client_mask;
  1593. }
  1594. }
  1595. break;
  1596. case CAM_SMMU_REGION_FWUNCACHED: {
  1597. struct cam_smmu_multi_region_info *fw_uncached;
  1598. fw_uncached = &cb_info->fwuncached_region;
  1599. if (fw_uncached->nested_regions[nested_reg_idx].subregion_support) {
  1600. subregion = cam_smmu_find_subregion(subregion_id,
  1601. fw_uncached->nested_regions[nested_reg_idx].subregions);
  1602. if (IS_ERR_OR_NULL(subregion)) {
  1603. CAM_ERR(CAM_SMMU,
  1604. "Failed to find subregion: %d in region: %d cb: %s",
  1605. subregion, region_id, cb_info->name[0]);
  1606. rc = PTR_ERR(subregion);
  1607. goto end;
  1608. }
  1609. *is_supported = true;
  1610. *is_allocated = &subregion->is_allocated;
  1611. *region_info = &subregion->subregion_info;
  1612. *ref_cnt = &subregion->mapped_refcnt;
  1613. *mapping_mask = &subregion->mapped_client_mask;
  1614. } else {
  1615. *is_allocated = &fw_uncached->nested_regions[nested_reg_idx].is_allocated;
  1616. *region_info = &fw_uncached->nested_regions[nested_reg_idx].region_info;
  1617. *ref_cnt = &fw_uncached->nested_regions[nested_reg_idx].mapped_refcnt;
  1618. *mapping_mask =
  1619. &fw_uncached->nested_regions[nested_reg_idx].mapped_client_mask;
  1620. }
  1621. }
  1622. break;
  1623. default:
  1624. CAM_ERR(CAM_SMMU,
  1625. "Unsupported region: %u in cb: %s for mapping known phy addr",
  1626. region_id, cb_info->name[0]);
  1627. rc = -EINVAL;
  1628. break;
  1629. }
  1630. end:
  1631. return rc;
  1632. }
  1633. int cam_smmu_map_phy_mem_region(int32_t smmu_hdl,
  1634. uint32_t region_id, uint32_t subregion_id,
  1635. dma_addr_t *iova, size_t *len)
  1636. {
  1637. int rc, idx, prot = IOMMU_READ | IOMMU_WRITE;
  1638. int multi_client_device_idx = 0, nested_reg_idx = 0;
  1639. int32_t *ref_cnt;
  1640. unsigned long *map_client_mask;
  1641. size_t region_len = 0;
  1642. dma_addr_t region_start;
  1643. bool is_supported = false;
  1644. bool *is_allocated;
  1645. struct iommu_domain *domain;
  1646. struct cam_context_bank_info *cb_info;
  1647. struct cam_smmu_region_info *region_info = NULL;
  1648. if (!iova || !len || (smmu_hdl == HANDLE_INIT)) {
  1649. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1650. return -EINVAL;
  1651. }
  1652. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1653. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1654. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1655. CAM_ERR(CAM_SMMU,
  1656. "Error: handle or index invalid. idx = %d hdl = %x",
  1657. idx, smmu_hdl);
  1658. rc = -EINVAL;
  1659. goto end;
  1660. }
  1661. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, smmu_hdl)) {
  1662. CAM_ERR(CAM_SMMU,
  1663. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1664. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1665. rc = -EINVAL;
  1666. goto end;
  1667. }
  1668. cb_info = &iommu_cb_set.cb_info[idx];
  1669. rc = cam_smmu_get_multiregion_client_dev_idx(&iommu_cb_set.cb_info[idx],
  1670. multi_client_device_idx, region_id, &nested_reg_idx);
  1671. if (rc)
  1672. goto end;
  1673. rc = cam_smmu_retrieve_region_info(cb_info, region_id, subregion_id,
  1674. nested_reg_idx, &is_supported, &ref_cnt, &map_client_mask,
  1675. &is_allocated, &region_info);
  1676. if (rc)
  1677. goto end;
  1678. if (!is_supported) {
  1679. CAM_ERR(CAM_SMMU,
  1680. "region: %u not supported for phy addr mapping in cb: %s",
  1681. region_id, cb_info->name[0]);
  1682. rc = -EINVAL;
  1683. goto end;
  1684. }
  1685. mutex_lock(&cb_info->lock);
  1686. if ((*is_allocated)) {
  1687. if (test_bit(multi_client_device_idx, map_client_mask)) {
  1688. CAM_ERR(CAM_SMMU,
  1689. "Trying to allocate region: %u [subregion: %u] twice on cb: %s",
  1690. region_id, subregion_id, cb_info->name[0]);
  1691. rc = -ENOMEM;
  1692. goto unlock_and_end;
  1693. }
  1694. set_bit(multi_client_device_idx, map_client_mask);
  1695. (*ref_cnt)++;
  1696. CAM_DBG(CAM_SMMU,
  1697. "Trying to allocate region: %u [subregion: %u] on cb: %s for different clients: 0x%x ref_cnt: %u",
  1698. region_id, subregion_id, cb_info->name[0], *map_client_mask, *ref_cnt);
  1699. *iova = region_info->iova_start;
  1700. *len = region_info->iova_len;
  1701. rc = 0;
  1702. goto unlock_and_end;
  1703. }
  1704. if (!region_info->phy_addr) {
  1705. CAM_ERR(CAM_SMMU,
  1706. "Invalid phy addr for region: %u [subregion: %u] on cb: %s",
  1707. region_id, subregion_id, cb_info->name[0]);
  1708. rc = -ENOMEM;
  1709. goto unlock_and_end;
  1710. }
  1711. region_len = region_info->iova_len;
  1712. region_start = region_info->iova_start;
  1713. CAM_DBG(CAM_SMMU,
  1714. "mapping region= %u [subregion = %u] for va = 0x%x len = %zu phy = 0x%x prot=0x%x",
  1715. region_id, subregion_id, region_start, region_len, region_info->phy_addr, prot);
  1716. domain = cb_info->domain;
  1717. if (region_id == CAM_SMMU_REGION_DEVICE)
  1718. prot |= IOMMU_MMIO;
  1719. /*
  1720. * Revisit this - what should we map this with - CACHED or UNCACHED?
  1721. * chipsets using dma-coherent-hint-cached - leaving it like this is
  1722. * fine as we can map both CACHED and UNCACHED on same CB.
  1723. * But on chipsets which use dma-coherent - all the buffers that are
  1724. * being mapped to this CB must be CACHED
  1725. */
  1726. rc = iommu_map(domain, region_start, region_info->phy_addr, region_len, prot);
  1727. if (rc) {
  1728. CAM_ERR(CAM_SMMU, "Failed to map region = %u into IOMMU cb = %s",
  1729. region_id, cb_info->name[0]);
  1730. goto unlock_and_end;
  1731. }
  1732. *is_allocated = true;
  1733. (*ref_cnt)++;
  1734. *iova = region_info->iova_start;
  1735. *len = region_info->iova_len;
  1736. set_bit(multi_client_device_idx, map_client_mask);
  1737. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1738. return rc;
  1739. unlock_and_end:
  1740. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1741. end:
  1742. return rc;
  1743. }
  1744. EXPORT_SYMBOL(cam_smmu_map_phy_mem_region);
  1745. int cam_smmu_unmap_phy_mem_region(int32_t smmu_hdl,
  1746. uint32_t region_id, uint32_t subregion_id)
  1747. {
  1748. int rc = 0, idx;
  1749. int multi_client_device_idx = 0, nested_reg_idx = 0;
  1750. int32_t *ref_cnt;
  1751. unsigned long *map_client_mask;
  1752. size_t len = 0;
  1753. dma_addr_t start = 0;
  1754. struct iommu_domain *domain;
  1755. size_t unmapped = 0;
  1756. bool is_supported = false;
  1757. bool *is_allocated = NULL;
  1758. struct cam_context_bank_info *cb_info;
  1759. struct cam_smmu_region_info *region_info = NULL;
  1760. if (smmu_hdl == HANDLE_INIT) {
  1761. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  1762. return -EINVAL;
  1763. }
  1764. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1765. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1766. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1767. CAM_ERR(CAM_SMMU,
  1768. "Error: handle or index invalid. idx = %d hdl = %x",
  1769. idx, smmu_hdl);
  1770. rc = -EINVAL;
  1771. goto end;
  1772. }
  1773. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, smmu_hdl)) {
  1774. CAM_ERR(CAM_SMMU,
  1775. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  1776. iommu_cb_set.cb_info[idx].handle, smmu_hdl);
  1777. rc = -EINVAL;
  1778. goto end;
  1779. }
  1780. cb_info = &iommu_cb_set.cb_info[idx];
  1781. rc = cam_smmu_get_multiregion_client_dev_idx(cb_info,
  1782. multi_client_device_idx, region_id, &nested_reg_idx);
  1783. if (rc)
  1784. goto end;
  1785. rc = cam_smmu_retrieve_region_info(cb_info, region_id, subregion_id,
  1786. nested_reg_idx, &is_supported, &ref_cnt, &map_client_mask,
  1787. &is_allocated, &region_info);
  1788. if (rc)
  1789. goto end;
  1790. if (!is_supported) {
  1791. CAM_ERR(CAM_SMMU,
  1792. "region: %u not supported for this SMMU handle cb: %s",
  1793. region_id, cb_info->name[0]);
  1794. rc = -EINVAL;
  1795. goto end;
  1796. }
  1797. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1798. if (!(*is_allocated)) {
  1799. CAM_ERR(CAM_SMMU,
  1800. "Trying to free region: %u that is not allocated on cb: %s",
  1801. region_id, cb_info->name[0]);
  1802. rc = -ENOMEM;
  1803. goto unlock_and_end;
  1804. }
  1805. (*ref_cnt)--;
  1806. clear_bit(multi_client_device_idx, map_client_mask);
  1807. if ((*ref_cnt) > 0) {
  1808. CAM_DBG(CAM_SMMU,
  1809. "Mapping for region: %u on cb: %s still in use refcnt: %d mapping_mask: 0x%x",
  1810. region_id, cb_info->name[0], *ref_cnt, *map_client_mask);
  1811. goto unlock_and_end;
  1812. }
  1813. len = region_info->iova_len;
  1814. start = region_info->iova_start;
  1815. domain = cb_info->domain;
  1816. unmapped = iommu_unmap(domain, start, len);
  1817. if (unmapped != len) {
  1818. CAM_ERR(CAM_SMMU, "Only %zu unmapped out of total %zu",
  1819. unmapped,
  1820. len);
  1821. rc = -EINVAL;
  1822. }
  1823. *is_allocated = false;
  1824. *ref_cnt = 0;
  1825. *map_client_mask = 0;
  1826. unlock_and_end:
  1827. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1828. end:
  1829. return rc;
  1830. }
  1831. EXPORT_SYMBOL(cam_smmu_unmap_phy_mem_region);
  1832. int cam_smmu_get_io_region_info(int32_t smmu_hdl,
  1833. dma_addr_t *iova, size_t *len,
  1834. dma_addr_t *discard_iova_start, size_t *discard_iova_len)
  1835. {
  1836. int32_t idx, rc;
  1837. int multi_client_device_idx = 0, nested_reg_idx = 0;
  1838. struct cam_smmu_nested_region_info *io_region;
  1839. if (!iova || !len || !discard_iova_start || !discard_iova_len ||
  1840. (smmu_hdl == HANDLE_INIT)) {
  1841. CAM_ERR(CAM_SMMU, "Error: Input args are invalid");
  1842. return -EINVAL;
  1843. }
  1844. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1845. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1846. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1847. CAM_ERR(CAM_SMMU,
  1848. "Error: handle or index invalid. idx = %d hdl = %x",
  1849. idx, smmu_hdl);
  1850. return -EINVAL;
  1851. }
  1852. if (!iommu_cb_set.cb_info[idx].io_support) {
  1853. CAM_ERR(CAM_SMMU,
  1854. "I/O memory not supported for this SMMU handle");
  1855. return -EINVAL;
  1856. }
  1857. rc = cam_smmu_get_multiregion_client_dev_idx(&iommu_cb_set.cb_info[idx],
  1858. multi_client_device_idx, CAM_SMMU_REGION_IO, &nested_reg_idx);
  1859. if (rc)
  1860. return rc;
  1861. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1862. io_region = &iommu_cb_set.cb_info[idx].io_info.nested_regions[nested_reg_idx];
  1863. *iova = io_region->region_info.iova_start;
  1864. *len = io_region->region_info.iova_len;
  1865. *discard_iova_start =
  1866. io_region->region_info.discard_iova_start;
  1867. *discard_iova_len =
  1868. io_region->region_info.discard_iova_len;
  1869. CAM_DBG(CAM_SMMU,
  1870. "I/O area for hdl = %x Region:[%pK %zu] Discard:[%pK %zu]",
  1871. smmu_hdl, *iova, *len,
  1872. *discard_iova_start, *discard_iova_len);
  1873. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1874. return 0;
  1875. }
  1876. int cam_smmu_get_region_info(int32_t smmu_hdl,
  1877. enum cam_smmu_region_id region_id,
  1878. struct cam_smmu_region_info *region_info)
  1879. {
  1880. int32_t idx, multi_client_device_idx = 0, nested_reg_idx = 0;
  1881. struct cam_context_bank_info *cb = NULL;
  1882. if (!region_info) {
  1883. CAM_ERR(CAM_SMMU, "Invalid params");
  1884. return -EINVAL;
  1885. }
  1886. if (smmu_hdl == HANDLE_INIT) {
  1887. CAM_ERR(CAM_SMMU, "Invalid handle");
  1888. return -EINVAL;
  1889. }
  1890. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  1891. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  1892. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  1893. CAM_ERR(CAM_SMMU, "Handle or index invalid. idx = %d hdl = %x",
  1894. idx, smmu_hdl);
  1895. return -EINVAL;
  1896. }
  1897. if (cam_smmu_get_multiregion_client_dev_idx(&iommu_cb_set.cb_info[idx],
  1898. multi_client_device_idx, region_id, &nested_reg_idx))
  1899. return -EINVAL;
  1900. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  1901. cb = &iommu_cb_set.cb_info[idx];
  1902. if (!cb) {
  1903. CAM_ERR(CAM_SMMU, "SMMU context bank pointer invalid");
  1904. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1905. return -EINVAL;
  1906. }
  1907. switch (region_id) {
  1908. case CAM_SMMU_REGION_FIRMWARE:
  1909. if (!cb->firmware_support) {
  1910. CAM_ERR(CAM_SMMU, "Firmware not supported");
  1911. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1912. return -ENODEV;
  1913. }
  1914. region_info->iova_start = cb->firmware_info.iova_start;
  1915. region_info->iova_len = cb->firmware_info.iova_len;
  1916. break;
  1917. case CAM_SMMU_REGION_SHARED: {
  1918. struct cam_smmu_nested_region_info *nested_reg_info;
  1919. if (!cb->shared_support) {
  1920. CAM_ERR(CAM_SMMU, "Shared mem not supported");
  1921. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1922. return -ENODEV;
  1923. }
  1924. nested_reg_info = &cb->shared_info.nested_regions[nested_reg_idx];
  1925. region_info->iova_start = nested_reg_info->region_info.iova_start;
  1926. region_info->iova_len = nested_reg_info->region_info.iova_len;
  1927. }
  1928. break;
  1929. case CAM_SMMU_REGION_SCRATCH:
  1930. if (!cb->scratch_buf_support) {
  1931. CAM_ERR(CAM_SMMU, "Scratch memory not supported");
  1932. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1933. return -ENODEV;
  1934. }
  1935. region_info->iova_start = cb->scratch_info.iova_start;
  1936. region_info->iova_len = cb->scratch_info.iova_len;
  1937. break;
  1938. case CAM_SMMU_REGION_IO: {
  1939. struct cam_smmu_nested_region_info *nested_reg_info;
  1940. if (!cb->io_support) {
  1941. CAM_ERR(CAM_SMMU, "IO memory not supported");
  1942. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1943. return -ENODEV;
  1944. }
  1945. nested_reg_info = &cb->io_info.nested_regions[nested_reg_idx];
  1946. region_info->iova_start = nested_reg_info->region_info.iova_start;
  1947. region_info->iova_len = nested_reg_info->region_info.iova_len;
  1948. }
  1949. break;
  1950. case CAM_SMMU_REGION_SECHEAP:
  1951. if (!cb->secheap_support) {
  1952. CAM_ERR(CAM_SMMU, "Secondary heap not supported");
  1953. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1954. return -ENODEV;
  1955. }
  1956. region_info->iova_start = cb->secheap_info.iova_start;
  1957. region_info->iova_len = cb->secheap_info.iova_len;
  1958. break;
  1959. case CAM_SMMU_REGION_FWUNCACHED: {
  1960. struct cam_smmu_nested_region_info *nested_reg_info;
  1961. if (!cb->fwuncached_region_support) {
  1962. CAM_WARN(CAM_SMMU, "FW uncached region not supported");
  1963. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1964. return -ENODEV;
  1965. }
  1966. nested_reg_info = &cb->fwuncached_region.nested_regions[nested_reg_idx];
  1967. region_info->iova_start = nested_reg_info->region_info.iova_start;
  1968. region_info->iova_len = nested_reg_info->region_info.iova_len;
  1969. }
  1970. break;
  1971. case CAM_SMMU_REGION_DEVICE: {
  1972. struct cam_smmu_nested_region_info *nested_reg_info;
  1973. if (!cb->device_region_support) {
  1974. CAM_WARN(CAM_SMMU, "device memory region not supported");
  1975. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1976. return -ENODEV;
  1977. }
  1978. nested_reg_info = &cb->device_region.nested_regions[nested_reg_idx];
  1979. region_info->iova_start = nested_reg_info->region_info.iova_start;
  1980. region_info->iova_len = nested_reg_info->region_info.iova_len;
  1981. }
  1982. break;
  1983. default:
  1984. CAM_ERR(CAM_SMMU, "Invalid region id: %d for smmu hdl: %X",
  1985. smmu_hdl, region_id);
  1986. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1987. return -EINVAL;
  1988. }
  1989. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  1990. return 0;
  1991. }
  1992. EXPORT_SYMBOL(cam_smmu_get_region_info);
  1993. int cam_smmu_reserve_buf_region(enum cam_smmu_region_id region,
  1994. int32_t smmu_hdl, struct dma_buf *buf, dma_addr_t *iova,
  1995. size_t *request_len)
  1996. {
  1997. struct cam_context_bank_info *cb_info;
  1998. struct region_buf_info *buf_info = NULL;
  1999. struct cam_smmu_region_info *region_info = NULL;
  2000. struct cam_smmu_subregion_info *subregion_info = NULL;
  2001. bool *is_buf_allocated;
  2002. bool region_supported;
  2003. size_t size = 0;
  2004. int idx, rc = 0, multi_client_device_idx, prot, nested_reg_idx;
  2005. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  2006. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  2007. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2008. CAM_ERR(CAM_SMMU,
  2009. "Error: handle or index invalid. idx = %d hdl = %x",
  2010. idx, smmu_hdl);
  2011. return -EINVAL;
  2012. }
  2013. cb_info = &iommu_cb_set.cb_info[idx];
  2014. rc = cam_smmu_get_multiregion_client_dev_idx(cb_info,
  2015. multi_client_device_idx, region, &nested_reg_idx);
  2016. if (rc)
  2017. return rc;
  2018. if (region == CAM_SMMU_REGION_SECHEAP) {
  2019. region_supported = cb_info->secheap_support;
  2020. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  2021. region_supported = cb_info->fwuncached_region_support;
  2022. } else {
  2023. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  2024. region);
  2025. return -EINVAL;
  2026. }
  2027. if (!region_supported) {
  2028. CAM_ERR(CAM_SMMU, "Reserve for region %d not supported",
  2029. region);
  2030. return -EINVAL;
  2031. }
  2032. mutex_lock(&cb_info->lock);
  2033. if (region == CAM_SMMU_REGION_SECHEAP) {
  2034. is_buf_allocated = &cb_info->is_secheap_allocated;
  2035. buf_info = &cb_info->secheap_info.buf_info;
  2036. region_info = &cb_info->secheap_info;
  2037. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  2038. struct cam_smmu_nested_region_info *nested_reg_info;
  2039. nested_reg_info =
  2040. &cb_info->fwuncached_region.nested_regions[nested_reg_idx];
  2041. if (nested_reg_info->subregion_support) {
  2042. /* For FW uncached, subregion generic is to be used */
  2043. subregion_info = cam_smmu_find_subregion(CAM_SMMU_SUBREGION_GENERIC,
  2044. nested_reg_info->subregions);
  2045. if (IS_ERR_OR_NULL(subregion_info)) {
  2046. CAM_ERR(CAM_SMMU,
  2047. "Failed to find free uncached subregion on cb: %s",
  2048. cb_info->name[0]);
  2049. mutex_unlock(&cb_info->lock);
  2050. return -EINVAL;
  2051. }
  2052. region_info = &subregion_info->subregion_info;
  2053. buf_info = &subregion_info->subregion_info.buf_info;
  2054. is_buf_allocated = &subregion_info->is_allocated;
  2055. } else {
  2056. region_info = &nested_reg_info->region_info;
  2057. buf_info = &nested_reg_info->region_info.buf_info;
  2058. is_buf_allocated = &nested_reg_info->is_allocated;
  2059. }
  2060. } else {
  2061. CAM_ERR(CAM_SMMU, "Region not supported for reserving %d",
  2062. region);
  2063. mutex_unlock(&cb_info->lock);
  2064. return -EINVAL;
  2065. }
  2066. if ((*is_buf_allocated)) {
  2067. CAM_ERR(CAM_SMMU, "Trying to allocate twice for region %d",
  2068. region);
  2069. rc = -ENOMEM;
  2070. mutex_unlock(&cb_info->lock);
  2071. return rc;
  2072. }
  2073. if (IS_ERR_OR_NULL(buf)) {
  2074. rc = PTR_ERR(buf);
  2075. CAM_ERR(CAM_SMMU,
  2076. "Error: dma get buf failed. rc = %d", rc);
  2077. goto err_out;
  2078. }
  2079. CAM_DBG(CAM_SMMU, "Map region=%d iova=0x%x len=0x%x",
  2080. region, region_info->iova_start, region_info->iova_len);
  2081. buf_info->buf = buf;
  2082. buf_info->attach = dma_buf_attach(buf_info->buf,
  2083. cb_info->dev);
  2084. if (IS_ERR_OR_NULL(buf_info->attach)) {
  2085. rc = PTR_ERR(buf_info->attach);
  2086. CAM_ERR(CAM_SMMU, "Error: dma buf attach failed");
  2087. goto err_put;
  2088. }
  2089. buf_info->table = dma_buf_map_attachment(buf_info->attach,
  2090. DMA_BIDIRECTIONAL);
  2091. if (IS_ERR_OR_NULL(buf_info->table)) {
  2092. rc = PTR_ERR(buf_info->table);
  2093. CAM_ERR(CAM_SMMU, "Error: dma buf map attachment failed");
  2094. goto err_detach;
  2095. }
  2096. prot = IOMMU_READ | IOMMU_WRITE;
  2097. if (iommu_cb_set.force_cache_allocs)
  2098. prot |= IOMMU_CACHE;
  2099. size = iommu_map_sg(cb_info->domain,
  2100. region_info->iova_start,
  2101. buf_info->table->sgl,
  2102. buf_info->table->orig_nents,
  2103. prot);
  2104. if (region_info->iova_len < size) {
  2105. CAM_ERR(CAM_SMMU,
  2106. "IOMMU mapping failed for size=%zu available iova_len=%zu",
  2107. size, region_info->iova_len);
  2108. rc = -EINVAL;
  2109. goto err_unmap_sg;
  2110. }
  2111. *iova = (uint32_t)region_info->iova_start;
  2112. /* Assign size mapped */
  2113. *request_len = size;
  2114. *is_buf_allocated = true;
  2115. mutex_unlock(&cb_info->lock);
  2116. return rc;
  2117. err_unmap_sg:
  2118. dma_buf_unmap_attachment(buf_info->attach,
  2119. buf_info->table,
  2120. DMA_BIDIRECTIONAL);
  2121. err_detach:
  2122. dma_buf_detach(buf_info->buf,
  2123. buf_info->attach);
  2124. err_put:
  2125. dma_buf_put(buf_info->buf);
  2126. err_out:
  2127. mutex_unlock(&cb_info->lock);
  2128. return rc;
  2129. }
  2130. EXPORT_SYMBOL(cam_smmu_reserve_buf_region);
  2131. int cam_smmu_release_buf_region(enum cam_smmu_region_id region,
  2132. int32_t smmu_hdl)
  2133. {
  2134. int idx, multi_client_device_idx, nested_reg_idx;
  2135. size_t size = 0;
  2136. struct region_buf_info *buf_info = NULL;
  2137. struct cam_context_bank_info *cb_info;
  2138. bool *is_buf_allocated;
  2139. bool region_supported;
  2140. struct cam_smmu_region_info *region_info = NULL;
  2141. struct cam_smmu_subregion_info *subregion_info = NULL;
  2142. idx = GET_SMMU_TABLE_IDX(smmu_hdl);
  2143. multi_client_device_idx = GET_SMMU_MULTI_CLIENT_IDX(smmu_hdl);
  2144. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2145. CAM_ERR(CAM_SMMU,
  2146. "Error: handle or index invalid. idx = %d hdl = %x",
  2147. idx, smmu_hdl);
  2148. return -EINVAL;
  2149. }
  2150. cb_info = &iommu_cb_set.cb_info[idx];
  2151. if (cam_smmu_get_multiregion_client_dev_idx(cb_info,
  2152. multi_client_device_idx, region, &nested_reg_idx))
  2153. return -EINVAL;
  2154. if (region == CAM_SMMU_REGION_SECHEAP) {
  2155. region_supported = cb_info->secheap_support;
  2156. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  2157. region_supported = cb_info->fwuncached_region_support;
  2158. } else {
  2159. CAM_ERR(CAM_SMMU, "Region not supported for release %d",
  2160. region);
  2161. return -EINVAL;
  2162. }
  2163. if (!region_supported) {
  2164. CAM_ERR(CAM_SMMU, "region: %d not supported", region);
  2165. return -EINVAL;
  2166. }
  2167. mutex_lock(&cb_info->lock);
  2168. if (region == CAM_SMMU_REGION_SECHEAP) {
  2169. is_buf_allocated = &cb_info->is_secheap_allocated;
  2170. buf_info = &cb_info->secheap_info.buf_info;
  2171. region_info = &cb_info->secheap_info;
  2172. } else if (region == CAM_SMMU_REGION_FWUNCACHED) {
  2173. struct cam_smmu_nested_region_info *nested_reg_info;
  2174. nested_reg_info =
  2175. &cb_info->fwuncached_region.nested_regions[nested_reg_idx];
  2176. if (nested_reg_info->subregion_support) {
  2177. subregion_info = cam_smmu_find_subregion(CAM_SMMU_SUBREGION_GENERIC,
  2178. nested_reg_info->subregions);
  2179. if (IS_ERR_OR_NULL(subregion_info)) {
  2180. CAM_ERR(CAM_SMMU,
  2181. "Failed to find uncached subregion on cb: %s",
  2182. cb_info->name[0]);
  2183. mutex_unlock(&cb_info->lock);
  2184. return -EINVAL;
  2185. }
  2186. is_buf_allocated = &subregion_info->is_allocated;
  2187. buf_info = &subregion_info->subregion_info.buf_info;
  2188. region_info = &subregion_info->subregion_info;
  2189. } else {
  2190. is_buf_allocated = &nested_reg_info->is_allocated;
  2191. buf_info = &nested_reg_info->region_info.buf_info;
  2192. region_info = &nested_reg_info->region_info;
  2193. }
  2194. } else {
  2195. CAM_ERR(CAM_SMMU, "Region not supported for release %d",
  2196. region);
  2197. mutex_unlock(&cb_info->lock);
  2198. return -EINVAL;
  2199. }
  2200. if (!(*is_buf_allocated)) {
  2201. CAM_ERR(CAM_SMMU,
  2202. "Trying to release freed region cb: %s region: %d",
  2203. cb_info->name[0], region);
  2204. mutex_unlock(&cb_info->lock);
  2205. return -ENOMEM;
  2206. }
  2207. size = iommu_unmap(cb_info->domain,
  2208. region_info->iova_start,
  2209. region_info->iova_len);
  2210. if (size != region_info->iova_len) {
  2211. CAM_ERR(CAM_SMMU, "Failed: Unmapped = %zu, requested = %zu",
  2212. size,
  2213. region_info->iova_len);
  2214. }
  2215. dma_buf_unmap_attachment(buf_info->attach,
  2216. buf_info->table, DMA_BIDIRECTIONAL);
  2217. dma_buf_detach(buf_info->buf, buf_info->attach);
  2218. *is_buf_allocated = false;
  2219. mutex_unlock(&cb_info->lock);
  2220. return 0;
  2221. }
  2222. EXPORT_SYMBOL(cam_smmu_release_buf_region);
  2223. static int cam_smmu_util_return_map_entry(struct cam_smmu_buffer_tracker *entry)
  2224. {
  2225. spin_lock_bh(&iommu_cb_set.s_lock);
  2226. list_add_tail(&entry->list, &iommu_cb_set.buf_tracker_free_list);
  2227. spin_unlock_bh(&iommu_cb_set.s_lock);
  2228. return 0;
  2229. }
  2230. void cam_smmu_buffer_tracker_putref(struct list_head *track_list)
  2231. {
  2232. struct cam_smmu_buffer_tracker *buffer_tracker, *temp;
  2233. if (iommu_cb_set.is_track_buf_disabled)
  2234. return;
  2235. if (!track_list || list_empty(track_list))
  2236. return;
  2237. list_for_each_entry_safe(buffer_tracker, temp, track_list, list) {
  2238. if (refcount_dec_and_test(&buffer_tracker->ref_count->refcount))
  2239. CAM_ERR(CAM_SMMU,
  2240. "[SMMU_BT] Unexpected - buffer reference [fd: 0x%x ino: 0x%x cb: %s] zeroed prior to unmap invocation",
  2241. buffer_tracker->ion_fd, buffer_tracker->i_ino,
  2242. buffer_tracker->cb_name);
  2243. else
  2244. CAM_DBG(CAM_SMMU,
  2245. "[SMMU_BT] kref_count after put, [fd: 0x%x ino: 0x%x cb: %s], count: %d",
  2246. buffer_tracker->ion_fd, buffer_tracker->i_ino,
  2247. buffer_tracker->cb_name,
  2248. kref_read(buffer_tracker->ref_count));
  2249. list_del_init(&buffer_tracker->list);
  2250. cam_smmu_util_return_map_entry(buffer_tracker);
  2251. }
  2252. }
  2253. EXPORT_SYMBOL(cam_smmu_buffer_tracker_putref);
  2254. static int cam_smmu_map_buffer_validate(struct dma_buf *buf,
  2255. int idx, enum dma_data_direction dma_dir, dma_addr_t *paddr_ptr,
  2256. size_t *len_ptr, enum cam_smmu_region_id region_id,
  2257. bool dis_delayed_unmap, struct cam_dma_buff_info **mapping_info)
  2258. {
  2259. struct dma_buf_attachment *attach = NULL;
  2260. struct sg_table *table = NULL;
  2261. struct iommu_domain *domain;
  2262. size_t size = 0;
  2263. unsigned long iova = 0;
  2264. int rc = 0;
  2265. struct timespec64 ts1, ts2;
  2266. long microsec = 0;
  2267. int prot = 0;
  2268. if (IS_ERR_OR_NULL(buf)) {
  2269. rc = PTR_ERR(buf);
  2270. CAM_ERR(CAM_SMMU,
  2271. "Error: dma get buf failed. rc = %d", rc);
  2272. goto err_out;
  2273. }
  2274. if (!mapping_info) {
  2275. rc = -EINVAL;
  2276. CAM_ERR(CAM_SMMU, "Error: mapping_info is invalid");
  2277. goto err_out;
  2278. }
  2279. if (iommu_cb_set.debug_cfg.map_profile_enable)
  2280. CAM_GET_TIMESTAMP(ts1);
  2281. attach = dma_buf_attach(buf, iommu_cb_set.cb_info[idx].dev);
  2282. if (IS_ERR_OR_NULL(attach)) {
  2283. rc = PTR_ERR(attach);
  2284. CAM_ERR(CAM_SMMU, "Error: dma buf attach failed");
  2285. goto err_out;
  2286. }
  2287. if (region_id == CAM_SMMU_REGION_SHARED) {
  2288. table = dma_buf_map_attachment(attach, dma_dir);
  2289. if (IS_ERR_OR_NULL(table)) {
  2290. rc = PTR_ERR(table);
  2291. CAM_ERR(CAM_SMMU, "Error: dma map attachment failed");
  2292. goto err_detach;
  2293. }
  2294. domain = iommu_cb_set.cb_info[idx].domain;
  2295. if (!domain) {
  2296. CAM_ERR(CAM_SMMU, "CB has no domain set");
  2297. goto err_unmap_sg;
  2298. }
  2299. rc = cam_smmu_alloc_iova(*len_ptr, iommu_cb_set.cb_info[idx].handle, &iova);
  2300. if (rc < 0) {
  2301. CAM_ERR(CAM_SMMU,
  2302. "IOVA alloc failed for shared memory, size=%zu, idx=%d, handle=%d",
  2303. *len_ptr, idx,
  2304. iommu_cb_set.cb_info[idx].handle);
  2305. goto err_unmap_sg;
  2306. }
  2307. prot = IOMMU_READ | IOMMU_WRITE;
  2308. if (iommu_cb_set.force_cache_allocs)
  2309. prot |= IOMMU_CACHE;
  2310. size = iommu_map_sg(domain, iova, table->sgl, table->orig_nents,
  2311. prot);
  2312. if (size < 0) {
  2313. CAM_ERR(CAM_SMMU, "IOMMU mapping failed");
  2314. rc = cam_smmu_free_iova(iova,
  2315. size, iommu_cb_set.cb_info[idx].handle);
  2316. if (rc)
  2317. CAM_ERR(CAM_SMMU, "IOVA free failed");
  2318. rc = -ENOMEM;
  2319. goto err_unmap_sg;
  2320. } else {
  2321. CAM_DBG(CAM_SMMU,
  2322. "iommu_map_sg returned iova=%pK, size=%zu",
  2323. iova, size);
  2324. *paddr_ptr = iova;
  2325. *len_ptr = size;
  2326. }
  2327. iommu_cb_set.cb_info[idx].shared_mapping_size += *len_ptr;
  2328. } else if (region_id == CAM_SMMU_REGION_IO) {
  2329. if (!dis_delayed_unmap)
  2330. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  2331. table = dma_buf_map_attachment(attach, dma_dir);
  2332. if (IS_ERR_OR_NULL(table)) {
  2333. rc = PTR_ERR(table);
  2334. CAM_ERR(CAM_SMMU,
  2335. "Error: dma map attachment failed, size=%zu, rc %d dma_dir %d",
  2336. buf->size, rc, dma_dir);
  2337. goto err_detach;
  2338. }
  2339. *paddr_ptr = sg_dma_address(table->sgl);
  2340. *len_ptr = (size_t)buf->size;
  2341. iommu_cb_set.cb_info[idx].io_mapping_size += *len_ptr;
  2342. } else {
  2343. CAM_ERR(CAM_SMMU, "Error: Wrong region id passed");
  2344. rc = -EINVAL;
  2345. goto err_detach;
  2346. }
  2347. CAM_DBG(CAM_SMMU,
  2348. "iova=%pK, region_id=%d, paddr=0x%llx, len=%zu, dma_map_attrs=%d",
  2349. iova, region_id, *paddr_ptr, *len_ptr, attach->dma_map_attrs);
  2350. if (iommu_cb_set.debug_cfg.map_profile_enable) {
  2351. CAM_GET_TIMESTAMP(ts2);
  2352. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  2353. trace_cam_log_event("SMMUMapProfile", "size and time in micro",
  2354. *len_ptr, microsec);
  2355. }
  2356. if (table->sgl) {
  2357. CAM_DBG(CAM_SMMU,
  2358. "DMA buf: %pK, device: %pK, attach: %pK, table: %pK",
  2359. (void *)buf,
  2360. (void *)iommu_cb_set.cb_info[idx].dev,
  2361. (void *)attach, (void *)table);
  2362. CAM_DBG(CAM_SMMU, "table sgl: %pK, rc: %d, dma_address: 0x%x",
  2363. (void *)table->sgl, rc,
  2364. (unsigned int)table->sgl->dma_address);
  2365. } else {
  2366. rc = -EINVAL;
  2367. CAM_ERR(CAM_SMMU, "Error: table sgl is null");
  2368. goto err_unmap_sg;
  2369. }
  2370. /* fill up mapping_info */
  2371. *mapping_info = kzalloc(sizeof(struct cam_dma_buff_info), GFP_KERNEL);
  2372. if (!(*mapping_info)) {
  2373. rc = -ENOSPC;
  2374. goto err_alloc;
  2375. }
  2376. (*mapping_info)->buf = buf;
  2377. (*mapping_info)->attach = attach;
  2378. (*mapping_info)->table = table;
  2379. (*mapping_info)->paddr = *paddr_ptr;
  2380. (*mapping_info)->len = *len_ptr;
  2381. (*mapping_info)->dir = dma_dir;
  2382. (*mapping_info)->map_count = 1;
  2383. (*mapping_info)->region_id = region_id;
  2384. if (!*paddr_ptr || !*len_ptr) {
  2385. CAM_ERR(CAM_SMMU, "Error: Space Allocation failed");
  2386. kfree(*mapping_info);
  2387. *mapping_info = NULL;
  2388. rc = -ENOSPC;
  2389. goto err_alloc;
  2390. }
  2391. CAM_DBG(CAM_SMMU, "idx=%d, dma_buf=%pK, dev=%pOFfp, paddr=0x%llx, len=%zu",
  2392. idx, buf,
  2393. iommu_cb_set.cb_info[idx].dev->of_node,
  2394. *paddr_ptr, *len_ptr);
  2395. /* Unmap the mapping in dma region as this is not used anyway */
  2396. if (region_id == CAM_SMMU_REGION_SHARED)
  2397. dma_buf_unmap_attachment(attach, table, dma_dir);
  2398. return 0;
  2399. err_alloc:
  2400. if (region_id == CAM_SMMU_REGION_SHARED) {
  2401. cam_smmu_free_iova(iova,
  2402. size,
  2403. iommu_cb_set.cb_info[idx].handle);
  2404. iommu_unmap(iommu_cb_set.cb_info[idx].domain,
  2405. *paddr_ptr,
  2406. *len_ptr);
  2407. }
  2408. err_unmap_sg:
  2409. dma_buf_unmap_attachment(attach, table, dma_dir);
  2410. err_detach:
  2411. dma_buf_detach(buf, attach);
  2412. err_out:
  2413. return rc;
  2414. }
  2415. static int cam_smmu_map_buffer_and_add_to_list(int idx, int ion_fd,
  2416. bool dis_delayed_unmap, enum dma_data_direction dma_dir,
  2417. dma_addr_t *paddr_ptr, size_t *len_ptr,
  2418. enum cam_smmu_region_id region_id, bool is_internal, struct dma_buf *buf,
  2419. struct kref **ref_count)
  2420. {
  2421. int rc = -1;
  2422. struct cam_dma_buff_info *mapping_info = NULL;
  2423. rc = cam_smmu_map_buffer_validate(buf, idx, dma_dir, paddr_ptr, len_ptr,
  2424. region_id, dis_delayed_unmap, &mapping_info);
  2425. if (rc) {
  2426. CAM_ERR(CAM_SMMU, "buffer validation failure");
  2427. return rc;
  2428. }
  2429. mapping_info->ion_fd = ion_fd;
  2430. mapping_info->i_ino = file_inode(buf->file)->i_ino;
  2431. mapping_info->is_internal = is_internal;
  2432. kref_init(&mapping_info->ref_count);
  2433. *ref_count = &mapping_info->ref_count;
  2434. CAM_GET_TIMESTAMP(mapping_info->ts);
  2435. /* add to the list */
  2436. list_add(&mapping_info->list,
  2437. &iommu_cb_set.cb_info[idx].smmu_buf_list);
  2438. CAM_DBG(CAM_SMMU, "fd %d i_ino %lu dmabuf %pK", ion_fd, mapping_info->i_ino, buf);
  2439. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], true,
  2440. mapping_info);
  2441. return 0;
  2442. }
  2443. static int cam_smmu_map_kernel_buffer_and_add_to_list(int idx,
  2444. struct dma_buf *buf, enum dma_data_direction dma_dir,
  2445. dma_addr_t *paddr_ptr, size_t *len_ptr,
  2446. enum cam_smmu_region_id region_id)
  2447. {
  2448. int rc = -1;
  2449. struct cam_dma_buff_info *mapping_info = NULL;
  2450. rc = cam_smmu_map_buffer_validate(buf, idx, dma_dir, paddr_ptr, len_ptr,
  2451. region_id, false, &mapping_info);
  2452. if (rc) {
  2453. CAM_ERR(CAM_SMMU, "buffer validation failure");
  2454. return rc;
  2455. }
  2456. mapping_info->ion_fd = -1;
  2457. mapping_info->i_ino = file_inode(buf->file)->i_ino;
  2458. CAM_GET_TIMESTAMP(mapping_info->ts);
  2459. /* add to the list */
  2460. list_add(&mapping_info->list,
  2461. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list);
  2462. CAM_DBG(CAM_SMMU, "fd %d i_ino %lu dmabuf %pK",
  2463. mapping_info->ion_fd, mapping_info->i_ino, buf);
  2464. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], true,
  2465. mapping_info);
  2466. return 0;
  2467. }
  2468. static int cam_smmu_unmap_buf_and_remove_from_list(
  2469. struct cam_dma_buff_info *mapping_info,
  2470. int idx)
  2471. {
  2472. int rc;
  2473. size_t size;
  2474. struct iommu_domain *domain;
  2475. struct timespec64 ts1, ts2;
  2476. long microsec = 0;
  2477. if ((!mapping_info->buf) || (!mapping_info->table) ||
  2478. (!mapping_info->attach)) {
  2479. CAM_ERR(CAM_SMMU,
  2480. "Error: Invalid params dev = %pK, table = %pK",
  2481. (void *)iommu_cb_set.cb_info[idx].dev,
  2482. (void *)mapping_info->table);
  2483. CAM_ERR(CAM_SMMU, "Error:dma_buf = %pK, attach = %pK",
  2484. (void *)mapping_info->buf,
  2485. (void *)mapping_info->attach);
  2486. return -EINVAL;
  2487. }
  2488. cam_smmu_update_monitor_array(&iommu_cb_set.cb_info[idx], false,
  2489. mapping_info);
  2490. CAM_DBG(CAM_SMMU,
  2491. "region_id=%d, paddr=0x%llx, len=%d, dma_map_attrs=%d",
  2492. mapping_info->region_id, mapping_info->paddr, mapping_info->len,
  2493. mapping_info->attach->dma_map_attrs);
  2494. if (iommu_cb_set.debug_cfg.map_profile_enable)
  2495. CAM_GET_TIMESTAMP(ts1);
  2496. if (mapping_info->region_id == CAM_SMMU_REGION_SHARED) {
  2497. CAM_DBG(CAM_SMMU,
  2498. "Removing SHARED buffer paddr = 0x%llx, len = %zu",
  2499. mapping_info->paddr, mapping_info->len);
  2500. domain = iommu_cb_set.cb_info[idx].domain;
  2501. size = iommu_unmap(domain,
  2502. mapping_info->paddr,
  2503. mapping_info->len);
  2504. if (size != mapping_info->len) {
  2505. CAM_ERR(CAM_SMMU, "IOMMU unmap failed");
  2506. CAM_ERR(CAM_SMMU, "Unmapped = %zu, requested = %zu",
  2507. size,
  2508. mapping_info->len);
  2509. }
  2510. rc = cam_smmu_free_iova(mapping_info->paddr,
  2511. mapping_info->len,
  2512. iommu_cb_set.cb_info[idx].handle);
  2513. if (rc)
  2514. CAM_ERR(CAM_SMMU, "IOVA free failed");
  2515. iommu_cb_set.cb_info[idx].shared_mapping_size -=
  2516. mapping_info->len;
  2517. } else if (mapping_info->region_id == CAM_SMMU_REGION_IO) {
  2518. if (mapping_info->is_internal)
  2519. mapping_info->attach->dma_map_attrs |=
  2520. DMA_ATTR_SKIP_CPU_SYNC;
  2521. dma_buf_unmap_attachment(mapping_info->attach,
  2522. mapping_info->table, mapping_info->dir);
  2523. iommu_cb_set.cb_info[idx].io_mapping_size -= mapping_info->len;
  2524. }
  2525. dma_buf_detach(mapping_info->buf, mapping_info->attach);
  2526. if (iommu_cb_set.debug_cfg.map_profile_enable) {
  2527. CAM_GET_TIMESTAMP(ts2);
  2528. CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
  2529. trace_cam_log_event("SMMUUnmapProfile",
  2530. "size and time in micro", mapping_info->len, microsec);
  2531. }
  2532. mapping_info->buf = NULL;
  2533. list_del_init(&mapping_info->list);
  2534. /* free one buffer */
  2535. kfree(mapping_info);
  2536. return 0;
  2537. }
  2538. static int cam_smmu_util_get_free_map_entry(struct cam_smmu_buffer_tracker **entry)
  2539. {
  2540. spin_lock_bh(&iommu_cb_set.s_lock);
  2541. if (list_empty(&iommu_cb_set.buf_tracker_free_list)) {
  2542. CAM_WARN(CAM_SMMU, "[SMMU_BT] Not enough mem to track buffer");
  2543. spin_unlock_bh(&iommu_cb_set.s_lock);
  2544. return -ENOMEM;
  2545. }
  2546. *entry = list_first_entry(&iommu_cb_set.buf_tracker_free_list,
  2547. struct cam_smmu_buffer_tracker, list);
  2548. list_del_init(&(*entry)->list);
  2549. spin_unlock_bh(&iommu_cb_set.s_lock);
  2550. return 0;
  2551. }
  2552. int cam_smmu_add_buf_to_track_list(int ion_fd, unsigned long inode,
  2553. struct kref **ref_count, struct list_head *buf_tracker, int idx)
  2554. {
  2555. int rc = 0;
  2556. struct cam_smmu_buffer_tracker *buf;
  2557. if (iommu_cb_set.is_track_buf_disabled)
  2558. return rc;
  2559. rc = cam_smmu_util_get_free_map_entry(&buf);
  2560. if (rc == -ENOMEM) {
  2561. rc = 0;
  2562. return rc;
  2563. }
  2564. kref_get(*ref_count);
  2565. buf->ion_fd = ion_fd;
  2566. buf->i_ino = inode;
  2567. buf->ref_count = *ref_count;
  2568. buf->cb_name = iommu_cb_set.cb_info[idx].name[0];
  2569. CAM_DBG(CAM_SMMU,
  2570. "[SMMU_BT] ref_cnt increased for fd 0x%x, ino 0x%x: %d, cb: %s",
  2571. buf->ion_fd, buf->i_ino, kref_read(buf->ref_count), buf->cb_name);
  2572. list_add(&buf->list, buf_tracker);
  2573. return rc;
  2574. }
  2575. static enum cam_smmu_buf_state cam_smmu_check_fd_in_list(int idx,
  2576. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2577. struct timespec64 **ts_mapping, unsigned long *inode, struct kref **ref_count)
  2578. {
  2579. struct cam_dma_buff_info *mapping;
  2580. unsigned long i_ino;
  2581. i_ino = file_inode(dmabuf->file)->i_ino;
  2582. list_for_each_entry(mapping,
  2583. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  2584. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2585. *paddr_ptr = mapping->paddr;
  2586. *len_ptr = mapping->len;
  2587. *ts_mapping = &mapping->ts;
  2588. *inode = i_ino;
  2589. *ref_count = &mapping->ref_count;
  2590. return CAM_SMMU_BUFF_EXIST;
  2591. }
  2592. }
  2593. return CAM_SMMU_BUFF_NOT_EXIST;
  2594. }
  2595. static enum cam_smmu_buf_state cam_smmu_user_reuse_fd_in_list(int idx,
  2596. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2597. struct timespec64 **ts_mapping, struct kref **ref_count)
  2598. {
  2599. struct cam_dma_buff_info *mapping;
  2600. unsigned long i_ino;
  2601. i_ino = file_inode(dmabuf->file)->i_ino;
  2602. list_for_each_entry(mapping,
  2603. &iommu_cb_set.cb_info[idx].smmu_buf_list, list) {
  2604. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2605. *paddr_ptr = mapping->paddr;
  2606. *len_ptr = mapping->len;
  2607. *ts_mapping = &mapping->ts;
  2608. mapping->map_count++;
  2609. *ref_count = &mapping->ref_count;
  2610. return CAM_SMMU_BUFF_EXIST;
  2611. }
  2612. }
  2613. return CAM_SMMU_BUFF_NOT_EXIST;
  2614. }
  2615. static enum cam_smmu_buf_state cam_smmu_check_dma_buf_in_list(int idx,
  2616. struct dma_buf *buf, dma_addr_t *paddr_ptr, size_t *len_ptr)
  2617. {
  2618. struct cam_dma_buff_info *mapping;
  2619. list_for_each_entry(mapping,
  2620. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list, list) {
  2621. if (mapping->buf == buf) {
  2622. *paddr_ptr = mapping->paddr;
  2623. *len_ptr = mapping->len;
  2624. return CAM_SMMU_BUFF_EXIST;
  2625. }
  2626. }
  2627. return CAM_SMMU_BUFF_NOT_EXIST;
  2628. }
  2629. static enum cam_smmu_buf_state cam_smmu_check_secure_fd_in_list(int idx,
  2630. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2631. struct kref **ref_count)
  2632. {
  2633. struct cam_sec_buff_info *mapping;
  2634. unsigned long i_ino;
  2635. i_ino = file_inode(dmabuf->file)->i_ino;
  2636. list_for_each_entry(mapping,
  2637. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  2638. list) {
  2639. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2640. *paddr_ptr = mapping->paddr;
  2641. *len_ptr = mapping->len;
  2642. mapping->map_count++;
  2643. *ref_count = &mapping->ref_count;
  2644. return CAM_SMMU_BUFF_EXIST;
  2645. }
  2646. }
  2647. return CAM_SMMU_BUFF_NOT_EXIST;
  2648. }
  2649. static enum cam_smmu_buf_state cam_smmu_validate_secure_fd_in_list(int idx,
  2650. int ion_fd, struct dma_buf *dmabuf, dma_addr_t *paddr_ptr, size_t *len_ptr,
  2651. unsigned long *inode, struct kref **ref_count)
  2652. {
  2653. struct cam_sec_buff_info *mapping;
  2654. unsigned long i_ino;
  2655. i_ino = file_inode(dmabuf->file)->i_ino;
  2656. list_for_each_entry(mapping,
  2657. &iommu_cb_set.cb_info[idx].smmu_buf_list,
  2658. list) {
  2659. if ((mapping->ion_fd == ion_fd) && (mapping->i_ino == i_ino)) {
  2660. *paddr_ptr = mapping->paddr;
  2661. *len_ptr = mapping->len;
  2662. *inode = i_ino;
  2663. *ref_count = &mapping->ref_count;
  2664. return CAM_SMMU_BUFF_EXIST;
  2665. }
  2666. }
  2667. return CAM_SMMU_BUFF_NOT_EXIST;
  2668. }
  2669. int cam_smmu_get_handle(char *identifier, int *handle_ptr)
  2670. {
  2671. int rc = 0;
  2672. if (!identifier) {
  2673. CAM_ERR(CAM_SMMU, "Error: iommu hardware name is NULL");
  2674. return -EINVAL;
  2675. }
  2676. if (!handle_ptr) {
  2677. CAM_ERR(CAM_SMMU, "Error: handle pointer is NULL");
  2678. return -EINVAL;
  2679. }
  2680. /* create and put handle in the table */
  2681. rc = cam_smmu_create_add_handle_in_table(identifier, handle_ptr);
  2682. if (rc < 0)
  2683. CAM_ERR(CAM_SMMU, "Error: %s get handle fail, rc %d",
  2684. identifier, rc);
  2685. return rc;
  2686. }
  2687. EXPORT_SYMBOL(cam_smmu_get_handle);
  2688. int cam_smmu_ops(int handle, enum cam_smmu_ops_param ops)
  2689. {
  2690. int ret = 0, idx;
  2691. if (handle == HANDLE_INIT) {
  2692. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2693. return -EINVAL;
  2694. }
  2695. idx = GET_SMMU_TABLE_IDX(handle);
  2696. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2697. CAM_ERR(CAM_SMMU, "Error: Index invalid. idx = %d hdl = %x",
  2698. idx, handle);
  2699. return -EINVAL;
  2700. }
  2701. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2702. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2703. CAM_ERR(CAM_SMMU,
  2704. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2705. iommu_cb_set.cb_info[idx].handle, handle);
  2706. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2707. return -EINVAL;
  2708. }
  2709. switch (ops) {
  2710. case CAM_SMMU_ATTACH: {
  2711. ret = cam_smmu_attach(idx);
  2712. break;
  2713. }
  2714. case CAM_SMMU_DETACH: {
  2715. ret = cam_smmu_detach_device(idx);
  2716. break;
  2717. }
  2718. case CAM_SMMU_VOTE:
  2719. case CAM_SMMU_DEVOTE:
  2720. default:
  2721. CAM_ERR(CAM_SMMU, "Error: idx = %d, ops = %d", idx, ops);
  2722. ret = -EINVAL;
  2723. }
  2724. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2725. return ret;
  2726. }
  2727. EXPORT_SYMBOL(cam_smmu_ops);
  2728. static int cam_smmu_alloc_scratch_buffer_add_to_list(int idx,
  2729. size_t virt_len,
  2730. size_t phys_len,
  2731. unsigned int iommu_dir,
  2732. dma_addr_t *virt_addr)
  2733. {
  2734. unsigned long nents = virt_len / phys_len;
  2735. struct cam_dma_buff_info *mapping_info = NULL;
  2736. size_t unmapped;
  2737. dma_addr_t iova = 0;
  2738. struct scatterlist *sg;
  2739. int i = 0;
  2740. int rc;
  2741. struct iommu_domain *domain = NULL;
  2742. struct page *page;
  2743. struct sg_table *table = NULL;
  2744. CAM_DBG(CAM_SMMU, "nents = %lu, idx = %d, virt_len = %zx",
  2745. nents, idx, virt_len);
  2746. CAM_DBG(CAM_SMMU, "phys_len = %zx, iommu_dir = %d, virt_addr = %pK",
  2747. phys_len, iommu_dir, virt_addr);
  2748. /*
  2749. * This table will go inside the 'mapping' structure
  2750. * where it will be held until put_scratch_buffer is called
  2751. */
  2752. table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
  2753. if (!table) {
  2754. rc = -ENOMEM;
  2755. goto err_table_alloc;
  2756. }
  2757. rc = sg_alloc_table(table, nents, GFP_KERNEL);
  2758. if (rc < 0) {
  2759. rc = -EINVAL;
  2760. goto err_sg_alloc;
  2761. }
  2762. page = alloc_pages(GFP_KERNEL, get_order(phys_len));
  2763. if (!page) {
  2764. rc = -ENOMEM;
  2765. goto err_page_alloc;
  2766. }
  2767. /* Now we create the sg list */
  2768. for_each_sg(table->sgl, sg, table->nents, i)
  2769. sg_set_page(sg, page, phys_len, 0);
  2770. /* Get the domain from within our cb_set struct and map it*/
  2771. domain = iommu_cb_set.cb_info[idx].domain;
  2772. rc = cam_smmu_alloc_scratch_va(&iommu_cb_set.cb_info[idx].scratch_map,
  2773. virt_len, &iova);
  2774. if (rc < 0) {
  2775. CAM_ERR(CAM_SMMU,
  2776. "Could not find valid iova for scratch buffer");
  2777. goto err_iommu_map;
  2778. }
  2779. if (iommu_cb_set.force_cache_allocs)
  2780. iommu_dir |= IOMMU_CACHE;
  2781. if (iommu_map_sg(domain,
  2782. iova,
  2783. table->sgl,
  2784. table->nents,
  2785. iommu_dir) != virt_len) {
  2786. CAM_ERR(CAM_SMMU, "iommu_map_sg() failed");
  2787. goto err_iommu_map;
  2788. }
  2789. /* Now update our mapping information within the cb_set struct */
  2790. mapping_info = kzalloc(sizeof(struct cam_dma_buff_info), GFP_KERNEL);
  2791. if (!mapping_info) {
  2792. rc = -ENOMEM;
  2793. goto err_mapping_info;
  2794. }
  2795. mapping_info->ion_fd = 0xDEADBEEF;
  2796. mapping_info->i_ino = 0;
  2797. mapping_info->buf = NULL;
  2798. mapping_info->attach = NULL;
  2799. mapping_info->table = table;
  2800. mapping_info->paddr = iova;
  2801. mapping_info->len = virt_len;
  2802. mapping_info->iommu_dir = iommu_dir;
  2803. mapping_info->map_count = 1;
  2804. mapping_info->phys_len = phys_len;
  2805. mapping_info->region_id = CAM_SMMU_REGION_SCRATCH;
  2806. CAM_DBG(CAM_SMMU, "paddr = %pK, len = %zx, phys_len = %zx",
  2807. (void *)mapping_info->paddr,
  2808. mapping_info->len, mapping_info->phys_len);
  2809. list_add(&mapping_info->list, &iommu_cb_set.cb_info[idx].smmu_buf_list);
  2810. *virt_addr = (dma_addr_t)iova;
  2811. CAM_DBG(CAM_SMMU, "mapped virtual address = %lx",
  2812. (unsigned long)*virt_addr);
  2813. return 0;
  2814. err_mapping_info:
  2815. unmapped = iommu_unmap(domain, iova, virt_len);
  2816. if (unmapped != virt_len)
  2817. CAM_ERR(CAM_SMMU, "Unmapped only %zx instead of %zx",
  2818. unmapped, virt_len);
  2819. err_iommu_map:
  2820. __free_pages(page, get_order(phys_len));
  2821. err_page_alloc:
  2822. sg_free_table(table);
  2823. err_sg_alloc:
  2824. kfree(table);
  2825. err_table_alloc:
  2826. return rc;
  2827. }
  2828. static int cam_smmu_free_scratch_buffer_remove_from_list(
  2829. struct cam_dma_buff_info *mapping_info,
  2830. int idx)
  2831. {
  2832. int rc = 0;
  2833. size_t unmapped;
  2834. struct iommu_domain *domain =
  2835. iommu_cb_set.cb_info[idx].domain;
  2836. struct scratch_mapping *scratch_map =
  2837. &iommu_cb_set.cb_info[idx].scratch_map;
  2838. if (!mapping_info->table) {
  2839. CAM_ERR(CAM_SMMU,
  2840. "Error: Invalid params: dev = %pK, table = %pK",
  2841. (void *)iommu_cb_set.cb_info[idx].dev,
  2842. (void *)mapping_info->table);
  2843. return -EINVAL;
  2844. }
  2845. /* Clean up the mapping_info struct from the list */
  2846. unmapped = iommu_unmap(domain, mapping_info->paddr, mapping_info->len);
  2847. if (unmapped != mapping_info->len)
  2848. CAM_ERR(CAM_SMMU, "Unmapped only %zx instead of %zx",
  2849. unmapped, mapping_info->len);
  2850. rc = cam_smmu_free_scratch_va(scratch_map,
  2851. mapping_info->paddr,
  2852. mapping_info->len);
  2853. if (rc < 0) {
  2854. CAM_ERR(CAM_SMMU,
  2855. "Error: Invalid iova while freeing scratch buffer");
  2856. rc = -EINVAL;
  2857. }
  2858. __free_pages(sg_page(mapping_info->table->sgl),
  2859. get_order(mapping_info->phys_len));
  2860. sg_free_table(mapping_info->table);
  2861. kfree(mapping_info->table);
  2862. list_del_init(&mapping_info->list);
  2863. kfree(mapping_info);
  2864. mapping_info = NULL;
  2865. return rc;
  2866. }
  2867. int cam_smmu_get_scratch_iova(int handle,
  2868. enum cam_smmu_map_dir dir,
  2869. dma_addr_t *paddr_ptr,
  2870. size_t virt_len,
  2871. size_t phys_len)
  2872. {
  2873. int idx, rc;
  2874. unsigned int iommu_dir;
  2875. if (!paddr_ptr || !virt_len || !phys_len) {
  2876. CAM_ERR(CAM_SMMU, "Error: Input pointer or lengths invalid");
  2877. return -EINVAL;
  2878. }
  2879. if (virt_len < phys_len) {
  2880. CAM_ERR(CAM_SMMU, "Error: virt_len > phys_len");
  2881. return -EINVAL;
  2882. }
  2883. if (handle == HANDLE_INIT) {
  2884. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2885. return -EINVAL;
  2886. }
  2887. iommu_dir = cam_smmu_translate_dir_to_iommu_dir(dir);
  2888. if (iommu_dir == IOMMU_INVALID_DIR) {
  2889. CAM_ERR(CAM_SMMU,
  2890. "Error: translate direction failed. dir = %d", dir);
  2891. return -EINVAL;
  2892. }
  2893. idx = GET_SMMU_TABLE_IDX(handle);
  2894. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2895. CAM_ERR(CAM_SMMU,
  2896. "Error: handle or index invalid. idx = %d hdl = %x",
  2897. idx, handle);
  2898. return -EINVAL;
  2899. }
  2900. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2901. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2902. CAM_ERR(CAM_SMMU,
  2903. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2904. iommu_cb_set.cb_info[idx].handle, handle);
  2905. rc = -EINVAL;
  2906. goto error;
  2907. }
  2908. if (!iommu_cb_set.cb_info[idx].scratch_buf_support) {
  2909. CAM_ERR(CAM_SMMU,
  2910. "Error: Context bank does not support scratch bufs");
  2911. rc = -EINVAL;
  2912. goto error;
  2913. }
  2914. CAM_DBG(CAM_SMMU, "smmu handle = %x, idx = %d, dir = %d",
  2915. handle, idx, dir);
  2916. CAM_DBG(CAM_SMMU, "virt_len = %zx, phys_len = %zx",
  2917. phys_len, virt_len);
  2918. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  2919. CAM_ERR(CAM_SMMU,
  2920. "Err:Dev %s should call SMMU attach before map buffer",
  2921. iommu_cb_set.cb_info[idx].name[0]);
  2922. rc = -EINVAL;
  2923. goto error;
  2924. }
  2925. if (!IS_ALIGNED(virt_len, PAGE_SIZE)) {
  2926. CAM_ERR(CAM_SMMU,
  2927. "Requested scratch buffer length not page aligned");
  2928. rc = -EINVAL;
  2929. goto error;
  2930. }
  2931. if (!IS_ALIGNED(virt_len, phys_len)) {
  2932. CAM_ERR(CAM_SMMU,
  2933. "Requested virt length not aligned with phys length");
  2934. rc = -EINVAL;
  2935. goto error;
  2936. }
  2937. rc = cam_smmu_alloc_scratch_buffer_add_to_list(idx,
  2938. virt_len,
  2939. phys_len,
  2940. iommu_dir,
  2941. paddr_ptr);
  2942. if (rc < 0)
  2943. CAM_ERR(CAM_SMMU, "Error: mapping or add list fail");
  2944. error:
  2945. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2946. return rc;
  2947. }
  2948. int cam_smmu_put_scratch_iova(int handle,
  2949. dma_addr_t paddr)
  2950. {
  2951. int idx;
  2952. int rc = -1;
  2953. struct cam_dma_buff_info *mapping_info;
  2954. if (handle == HANDLE_INIT) {
  2955. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  2956. return -EINVAL;
  2957. }
  2958. /* find index in the iommu_cb_set.cb_info */
  2959. idx = GET_SMMU_TABLE_IDX(handle);
  2960. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  2961. CAM_ERR(CAM_SMMU,
  2962. "Error: handle or index invalid. idx = %d hdl = %x",
  2963. idx, handle);
  2964. return -EINVAL;
  2965. }
  2966. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  2967. if (iommu_cb_set.cb_info[idx].handle != handle) {
  2968. CAM_ERR(CAM_SMMU,
  2969. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  2970. iommu_cb_set.cb_info[idx].handle, handle);
  2971. rc = -EINVAL;
  2972. goto handle_err;
  2973. }
  2974. if (!iommu_cb_set.cb_info[idx].scratch_buf_support) {
  2975. CAM_ERR(CAM_SMMU,
  2976. "Error: Context bank does not support scratch buffers");
  2977. rc = -EINVAL;
  2978. goto handle_err;
  2979. }
  2980. /* Based on virtual address and index, we can find mapping info
  2981. * of the scratch buffer
  2982. */
  2983. mapping_info = cam_smmu_find_mapping_by_virt_address(idx, paddr);
  2984. if (!mapping_info) {
  2985. CAM_ERR(CAM_SMMU, "Error: Invalid params");
  2986. rc = -ENODEV;
  2987. goto handle_err;
  2988. }
  2989. /* unmapping one buffer from device */
  2990. rc = cam_smmu_free_scratch_buffer_remove_from_list(mapping_info, idx);
  2991. if (rc < 0) {
  2992. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  2993. goto handle_err;
  2994. }
  2995. handle_err:
  2996. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  2997. return rc;
  2998. }
  2999. static int cam_smmu_map_stage2_buffer_and_add_to_list(int idx, int ion_fd,
  3000. enum dma_data_direction dma_dir, dma_addr_t *paddr_ptr,
  3001. size_t *len_ptr, struct dma_buf *dmabuf, struct kref **ref_count)
  3002. {
  3003. int rc = 0;
  3004. struct dma_buf_attachment *attach = NULL;
  3005. struct sg_table *table = NULL;
  3006. struct cam_sec_buff_info *mapping_info;
  3007. /* clean the content from clients */
  3008. *paddr_ptr = (dma_addr_t)NULL;
  3009. *len_ptr = (size_t)0;
  3010. /*
  3011. * ion_phys() is deprecated. call dma_buf_attach() and
  3012. * dma_buf_map_attachment() to get the buffer's physical
  3013. * address.
  3014. */
  3015. attach = dma_buf_attach(dmabuf, iommu_cb_set.cb_info[idx].dev);
  3016. if (IS_ERR_OR_NULL(attach)) {
  3017. CAM_ERR(CAM_SMMU,
  3018. "Error: dma buf attach failed, idx=%d, ion_fd=%d",
  3019. idx, ion_fd);
  3020. rc = PTR_ERR(attach);
  3021. goto err_out;
  3022. }
  3023. attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  3024. if (IS_CSF25(iommu_cb_set.csf_version.arch_ver,
  3025. iommu_cb_set.csf_version.max_ver))
  3026. attach->dma_map_attrs =
  3027. cam_update_dma_map_attributes(attach->dma_map_attrs);
  3028. table = dma_buf_map_attachment(attach, dma_dir);
  3029. if (IS_ERR_OR_NULL(table)) {
  3030. CAM_ERR(CAM_SMMU, "Error: dma buf map attachment failed");
  3031. rc = PTR_ERR(table);
  3032. goto err_detach;
  3033. }
  3034. /* return addr and len to client */
  3035. if (IS_CSF25(iommu_cb_set.csf_version.arch_ver,
  3036. iommu_cb_set.csf_version.max_ver))
  3037. *paddr_ptr = sg_dma_address(table->sgl);
  3038. else
  3039. *paddr_ptr = sg_phys(table->sgl);
  3040. *len_ptr = (size_t)sg_dma_len(table->sgl);
  3041. /* fill up mapping_info */
  3042. mapping_info = kzalloc(sizeof(struct cam_sec_buff_info), GFP_KERNEL);
  3043. if (!mapping_info) {
  3044. rc = -ENOMEM;
  3045. goto err_unmap_sg;
  3046. }
  3047. mapping_info->ion_fd = ion_fd;
  3048. mapping_info->i_ino = file_inode(dmabuf->file)->i_ino;
  3049. mapping_info->paddr = *paddr_ptr;
  3050. mapping_info->len = *len_ptr;
  3051. mapping_info->dir = dma_dir;
  3052. mapping_info->map_count = 1;
  3053. mapping_info->buf = dmabuf;
  3054. mapping_info->attach = attach;
  3055. mapping_info->table = table;
  3056. kref_init(&mapping_info->ref_count);
  3057. *ref_count = &mapping_info->ref_count;
  3058. CAM_DBG(CAM_SMMU, "idx=%d, ion_fd=%d, i_ino=%lu, dev=%pOFfp, paddr=0x%llx, len=%zu",
  3059. idx, ion_fd, mapping_info->i_ino,
  3060. iommu_cb_set.cb_info[idx].dev->of_node,
  3061. *paddr_ptr, *len_ptr);
  3062. /* add to the list */
  3063. list_add(&mapping_info->list, &iommu_cb_set.cb_info[idx].smmu_buf_list);
  3064. return 0;
  3065. err_unmap_sg:
  3066. dma_buf_unmap_attachment(attach, table, dma_dir);
  3067. err_detach:
  3068. dma_buf_detach(dmabuf, attach);
  3069. err_out:
  3070. return rc;
  3071. }
  3072. int cam_smmu_map_stage2_iova(int handle, int ion_fd, struct dma_buf *dmabuf,
  3073. enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr, size_t *len_ptr,
  3074. struct kref **ref_count)
  3075. {
  3076. int idx, rc;
  3077. enum dma_data_direction dma_dir;
  3078. enum cam_smmu_buf_state buf_state;
  3079. if (!paddr_ptr || !len_ptr) {
  3080. CAM_ERR(CAM_SMMU,
  3081. "Error: Invalid inputs, paddr_ptr:%pK, len_ptr: %pK",
  3082. paddr_ptr, len_ptr);
  3083. return -EINVAL;
  3084. }
  3085. /* clean the content from clients */
  3086. *paddr_ptr = (dma_addr_t)NULL;
  3087. *len_ptr = (size_t)0;
  3088. dma_dir = cam_smmu_translate_dir(dir);
  3089. if (dma_dir == DMA_NONE) {
  3090. CAM_ERR(CAM_SMMU,
  3091. "Error: translate direction failed. dir = %d", dir);
  3092. return -EINVAL;
  3093. }
  3094. idx = GET_SMMU_TABLE_IDX(handle);
  3095. if ((handle == HANDLE_INIT) ||
  3096. (idx < 0) ||
  3097. (idx >= iommu_cb_set.cb_num)) {
  3098. CAM_ERR(CAM_SMMU,
  3099. "Error: handle or index invalid. idx = %d hdl = %x",
  3100. idx, handle);
  3101. return -EINVAL;
  3102. }
  3103. if (!iommu_cb_set.cb_info[idx].is_secure) {
  3104. CAM_ERR(CAM_SMMU,
  3105. "Error: can't map secure mem to non secure cb, idx=%d",
  3106. idx);
  3107. return -EINVAL;
  3108. }
  3109. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3110. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3111. CAM_ERR(CAM_SMMU,
  3112. "Error: hdl is not valid, idx=%d, table_hdl=%x, hdl=%x",
  3113. idx, iommu_cb_set.cb_info[idx].handle, handle);
  3114. rc = -EINVAL;
  3115. goto get_addr_end;
  3116. }
  3117. buf_state = cam_smmu_check_secure_fd_in_list(idx, ion_fd, dmabuf, paddr_ptr,
  3118. len_ptr, ref_count);
  3119. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  3120. CAM_DBG(CAM_SMMU,
  3121. "fd:%d already in list idx:%d, handle=%d give same addr back",
  3122. ion_fd, idx, handle);
  3123. rc = 0;
  3124. goto get_addr_end;
  3125. }
  3126. rc = cam_smmu_map_stage2_buffer_and_add_to_list(idx, ion_fd, dma_dir,
  3127. paddr_ptr, len_ptr, dmabuf, ref_count);
  3128. if (rc < 0) {
  3129. CAM_ERR(CAM_SMMU,
  3130. "Error: mapping or add list fail, idx=%d, handle=%d, fd=%d, rc=%d",
  3131. idx, handle, ion_fd, rc);
  3132. goto get_addr_end;
  3133. }
  3134. get_addr_end:
  3135. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3136. return rc;
  3137. }
  3138. EXPORT_SYMBOL(cam_smmu_map_stage2_iova);
  3139. static int cam_smmu_secure_unmap_buf_and_remove_from_list(
  3140. struct cam_sec_buff_info *mapping_info,
  3141. int idx)
  3142. {
  3143. if ((!mapping_info->buf) || (!mapping_info->table) ||
  3144. (!mapping_info->attach)) {
  3145. CAM_ERR(CAM_SMMU, "Error: Invalid params dev = %pK, table = %pK",
  3146. (void *)iommu_cb_set.cb_info[idx].dev,
  3147. (void *)mapping_info->table);
  3148. CAM_ERR(CAM_SMMU, "Error:dma_buf = %pK, attach = %pK\n",
  3149. (void *)mapping_info->buf,
  3150. (void *)mapping_info->attach);
  3151. return -EINVAL;
  3152. }
  3153. /* skip cache operations */
  3154. mapping_info->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  3155. /* iommu buffer clean up */
  3156. dma_buf_unmap_attachment(mapping_info->attach,
  3157. mapping_info->table, mapping_info->dir);
  3158. dma_buf_detach(mapping_info->buf, mapping_info->attach);
  3159. mapping_info->buf = NULL;
  3160. list_del_init(&mapping_info->list);
  3161. CAM_DBG(CAM_SMMU, "unmap fd: %d, i_ino : %lu, idx : %d",
  3162. mapping_info->ion_fd, mapping_info->i_ino, idx);
  3163. /* free one buffer */
  3164. kfree(mapping_info);
  3165. return 0;
  3166. }
  3167. int cam_smmu_unmap_stage2_iova(int handle, int ion_fd, struct dma_buf *dma_buf,
  3168. bool force_unmap)
  3169. {
  3170. int idx, rc;
  3171. struct cam_sec_buff_info *mapping_info;
  3172. /* find index in the iommu_cb_set.cb_info */
  3173. idx = GET_SMMU_TABLE_IDX(handle);
  3174. if ((handle == HANDLE_INIT) ||
  3175. (idx < 0) ||
  3176. (idx >= iommu_cb_set.cb_num)) {
  3177. CAM_ERR(CAM_SMMU,
  3178. "Error: handle or index invalid. idx = %d hdl = %x",
  3179. idx, handle);
  3180. return -EINVAL;
  3181. }
  3182. if (!iommu_cb_set.cb_info[idx].is_secure) {
  3183. CAM_ERR(CAM_SMMU,
  3184. "Error: can't unmap secure mem from non secure cb");
  3185. return -EINVAL;
  3186. }
  3187. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3188. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3189. CAM_ERR(CAM_SMMU,
  3190. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3191. iommu_cb_set.cb_info[idx].handle, handle);
  3192. rc = -EINVAL;
  3193. goto put_addr_end;
  3194. }
  3195. /* based on ion fd and index, we can find mapping info of buffer */
  3196. mapping_info = cam_smmu_find_mapping_by_sec_buf_idx(idx, ion_fd, dma_buf);
  3197. if (!mapping_info) {
  3198. CAM_ERR(CAM_SMMU,
  3199. "Error: Invalid params! idx = %d, fd = %d",
  3200. idx, ion_fd);
  3201. rc = -EINVAL;
  3202. goto put_addr_end;
  3203. }
  3204. mapping_info->map_count--;
  3205. if (mapping_info->map_count > 0) {
  3206. CAM_DBG(CAM_SMMU,
  3207. "idx: %d fd = %d map_count: %d",
  3208. idx, ion_fd, mapping_info->map_count);
  3209. rc = 0;
  3210. goto put_addr_end;
  3211. }
  3212. mapping_info->map_count = 0;
  3213. if (!force_unmap && kref_read(&mapping_info->ref_count) > 1) {
  3214. CAM_ERR(CAM_SMMU,
  3215. "[SMMU_BT] Error: can't unmap buffer as it's still active, idx: %d, cb: %s, fd: 0x%x, ino: 0x%x, ref_count: %d",
  3216. idx, iommu_cb_set.cb_info[idx].name[0], ion_fd, mapping_info->i_ino,
  3217. kref_read(&mapping_info->ref_count));
  3218. rc = -EPERM;
  3219. goto put_addr_end;
  3220. }
  3221. /* unmapping one buffer from device */
  3222. rc = cam_smmu_secure_unmap_buf_and_remove_from_list(mapping_info, idx);
  3223. if (rc) {
  3224. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  3225. goto put_addr_end;
  3226. }
  3227. put_addr_end:
  3228. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3229. return rc;
  3230. }
  3231. EXPORT_SYMBOL(cam_smmu_unmap_stage2_iova);
  3232. static int cam_smmu_map_iova_validate_params(int handle,
  3233. enum cam_smmu_map_dir dir,
  3234. dma_addr_t *paddr_ptr, size_t *len_ptr,
  3235. enum cam_smmu_region_id region_id)
  3236. {
  3237. int idx, rc = 0;
  3238. enum dma_data_direction dma_dir;
  3239. if (!paddr_ptr || !len_ptr) {
  3240. CAM_ERR(CAM_SMMU, "Input pointers are invalid");
  3241. return -EINVAL;
  3242. }
  3243. if (handle == HANDLE_INIT) {
  3244. CAM_ERR(CAM_SMMU, "Invalid handle");
  3245. return -EINVAL;
  3246. }
  3247. /* clean the content from clients */
  3248. *paddr_ptr = (dma_addr_t)NULL;
  3249. if (region_id != CAM_SMMU_REGION_SHARED)
  3250. *len_ptr = (size_t)0;
  3251. dma_dir = cam_smmu_translate_dir(dir);
  3252. if (dma_dir == DMA_NONE) {
  3253. CAM_ERR(CAM_SMMU, "translate direction failed. dir = %d", dir);
  3254. return -EINVAL;
  3255. }
  3256. idx = GET_SMMU_TABLE_IDX(handle);
  3257. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3258. CAM_ERR(CAM_SMMU, "handle or index invalid. idx = %d hdl = %x",
  3259. idx, handle);
  3260. return -EINVAL;
  3261. }
  3262. return rc;
  3263. }
  3264. bool cam_smmu_supports_shared_region(int handle)
  3265. {
  3266. int idx = GET_SMMU_TABLE_IDX(handle);
  3267. bool is_shared;
  3268. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3269. is_shared = (iommu_cb_set.cb_info[idx].shared_support) ? true : false;
  3270. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3271. return is_shared;
  3272. }
  3273. void cam_smmu_buffer_tracker_buffer_putref(struct cam_smmu_buffer_tracker *entry)
  3274. {
  3275. if (!entry) {
  3276. CAM_WARN(CAM_ISP, "[SMMU_BT] track buffer entry is NULL");
  3277. return;
  3278. }
  3279. if (refcount_dec_and_test(&entry->ref_count->refcount))
  3280. CAM_ERR(CAM_SMMU,
  3281. "[SMMU_BT] Unexpected - buffer reference [fd: 0x%x ino: 0x%x cb: %s] zeroed prior to unmap invocation",
  3282. entry->ion_fd, entry->i_ino, entry->cb_name);
  3283. else
  3284. CAM_DBG(CAM_SMMU,
  3285. "[SMMU_BT] kref_count after put, [fd: 0x%x ino: 0x%x cb: %s], count: %d",
  3286. entry->ion_fd, entry->i_ino, entry->cb_name, kref_read(entry->ref_count));
  3287. list_del_init(&entry->list);
  3288. cam_smmu_util_return_map_entry(entry);
  3289. }
  3290. int cam_smmu_map_user_iova(int handle, int ion_fd, struct dma_buf *dmabuf,
  3291. bool dis_delayed_unmap, enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr,
  3292. size_t *len_ptr, enum cam_smmu_region_id region_id,
  3293. bool is_internal, struct kref **ref_count)
  3294. {
  3295. int idx, rc = 0;
  3296. struct timespec64 *ts = NULL;
  3297. enum cam_smmu_buf_state buf_state;
  3298. enum dma_data_direction dma_dir;
  3299. rc = cam_smmu_map_iova_validate_params(handle, dir, paddr_ptr,
  3300. len_ptr, region_id);
  3301. if (rc) {
  3302. CAM_ERR(CAM_SMMU, "initial checks failed, unable to proceed");
  3303. return rc;
  3304. }
  3305. dma_dir = (enum dma_data_direction)dir;
  3306. idx = GET_SMMU_TABLE_IDX(handle);
  3307. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3308. if (iommu_cb_set.cb_info[idx].is_secure) {
  3309. CAM_ERR(CAM_SMMU,
  3310. "Error: can't map non-secure mem to secure cb idx=%d",
  3311. idx);
  3312. rc = -EINVAL;
  3313. goto get_addr_end;
  3314. }
  3315. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3316. CAM_ERR(CAM_SMMU,
  3317. "hdl is not valid, idx=%d, table_hdl = %x, hdl = %x",
  3318. idx, iommu_cb_set.cb_info[idx].handle, handle);
  3319. rc = -EINVAL;
  3320. goto get_addr_end;
  3321. }
  3322. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  3323. CAM_ERR(CAM_SMMU,
  3324. "Err:Dev %s should call SMMU attach before map buffer",
  3325. iommu_cb_set.cb_info[idx].name[0]);
  3326. rc = -EINVAL;
  3327. goto get_addr_end;
  3328. }
  3329. buf_state = cam_smmu_user_reuse_fd_in_list(idx, ion_fd, dmabuf, paddr_ptr,
  3330. len_ptr, &ts, ref_count);
  3331. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  3332. uint64_t ms = 0, hrs = 0, min = 0, sec = 0;
  3333. if (ts)
  3334. CAM_CONVERT_TIMESTAMP_FORMAT((*ts), hrs, min, sec, ms);
  3335. CAM_ERR(CAM_SMMU,
  3336. "fd=%d already in list [%llu:%llu:%lu:%llu] cb=%s idx=%d handle=%d len=%llu,give same addr back",
  3337. ion_fd, hrs, min, sec, ms,
  3338. iommu_cb_set.cb_info[idx].name[0],
  3339. idx, handle, *len_ptr);
  3340. rc = 0;
  3341. goto get_addr_end;
  3342. }
  3343. rc = cam_smmu_map_buffer_and_add_to_list(idx, ion_fd,
  3344. dis_delayed_unmap, dma_dir, paddr_ptr, len_ptr,
  3345. region_id, is_internal, dmabuf, ref_count);
  3346. if (rc < 0) {
  3347. CAM_ERR(CAM_SMMU,
  3348. "mapping or add list fail cb:%s idx=%d, fd=%d, region=%d, rc=%d",
  3349. iommu_cb_set.cb_info[idx].name[0], idx,
  3350. ion_fd, region_id, rc);
  3351. cam_smmu_dump_cb_info(idx);
  3352. }
  3353. get_addr_end:
  3354. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3355. return rc;
  3356. }
  3357. EXPORT_SYMBOL(cam_smmu_map_user_iova);
  3358. int cam_smmu_map_kernel_iova(int handle, struct dma_buf *buf,
  3359. enum cam_smmu_map_dir dir, dma_addr_t *paddr_ptr,
  3360. size_t *len_ptr, enum cam_smmu_region_id region_id)
  3361. {
  3362. int idx, rc = 0;
  3363. enum cam_smmu_buf_state buf_state;
  3364. enum dma_data_direction dma_dir;
  3365. rc = cam_smmu_map_iova_validate_params(handle, dir, paddr_ptr,
  3366. len_ptr, region_id);
  3367. if (rc) {
  3368. CAM_ERR(CAM_SMMU, "initial checks failed, unable to proceed");
  3369. return rc;
  3370. }
  3371. dma_dir = cam_smmu_translate_dir(dir);
  3372. idx = GET_SMMU_TABLE_IDX(handle);
  3373. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3374. if (iommu_cb_set.cb_info[idx].is_secure) {
  3375. CAM_ERR(CAM_SMMU,
  3376. "Error: can't map non-secure mem to secure cb");
  3377. rc = -EINVAL;
  3378. goto get_addr_end;
  3379. }
  3380. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3381. CAM_ERR(CAM_SMMU, "hdl is not valid, table_hdl = %x, hdl = %x",
  3382. iommu_cb_set.cb_info[idx].handle, handle);
  3383. rc = -EINVAL;
  3384. goto get_addr_end;
  3385. }
  3386. if (iommu_cb_set.cb_info[idx].state != CAM_SMMU_ATTACH) {
  3387. CAM_ERR(CAM_SMMU,
  3388. "Err:Dev %s should call SMMU attach before map buffer",
  3389. iommu_cb_set.cb_info[idx].name[0]);
  3390. rc = -EINVAL;
  3391. goto get_addr_end;
  3392. }
  3393. buf_state = cam_smmu_check_dma_buf_in_list(idx, buf,
  3394. paddr_ptr, len_ptr);
  3395. if (buf_state == CAM_SMMU_BUFF_EXIST) {
  3396. CAM_ERR(CAM_SMMU,
  3397. "dma_buf :%pK already in the list", buf);
  3398. rc = -EALREADY;
  3399. goto get_addr_end;
  3400. }
  3401. rc = cam_smmu_map_kernel_buffer_and_add_to_list(idx, buf, dma_dir,
  3402. paddr_ptr, len_ptr, region_id);
  3403. if (rc < 0)
  3404. CAM_ERR(CAM_SMMU, "mapping or add list fail");
  3405. get_addr_end:
  3406. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3407. return rc;
  3408. }
  3409. EXPORT_SYMBOL(cam_smmu_map_kernel_iova);
  3410. int cam_smmu_get_iova(int handle, int ion_fd, struct dma_buf *dma_buf,
  3411. dma_addr_t *paddr_ptr, size_t *len_ptr, struct list_head *buf_tracker,
  3412. struct kref **ref_count)
  3413. {
  3414. int idx, rc = 0;
  3415. struct timespec64 *ts = NULL;
  3416. enum cam_smmu_buf_state buf_state;
  3417. unsigned long i_ino;
  3418. if (!paddr_ptr || !len_ptr) {
  3419. CAM_ERR(CAM_SMMU, "Error: Input pointers are invalid");
  3420. return -EINVAL;
  3421. }
  3422. if (handle == HANDLE_INIT) {
  3423. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3424. return -EINVAL;
  3425. }
  3426. /* clean the content from clients */
  3427. *paddr_ptr = (dma_addr_t)NULL;
  3428. *len_ptr = (size_t)0;
  3429. idx = GET_SMMU_TABLE_IDX(handle);
  3430. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3431. CAM_ERR(CAM_SMMU,
  3432. "Error: handle or index invalid. idx = %d hdl = %x",
  3433. idx, handle);
  3434. return -EINVAL;
  3435. }
  3436. if (iommu_cb_set.cb_info[idx].is_secure) {
  3437. CAM_ERR(CAM_SMMU,
  3438. "Error: can't get non-secure mem from secure cb");
  3439. return -EINVAL;
  3440. }
  3441. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3442. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3443. CAM_ERR(CAM_SMMU,
  3444. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3445. iommu_cb_set.cb_info[idx].handle, handle);
  3446. rc = -EINVAL;
  3447. goto get_addr_end;
  3448. }
  3449. buf_state = cam_smmu_check_fd_in_list(idx, ion_fd, dma_buf, paddr_ptr,
  3450. len_ptr, &ts, &i_ino, ref_count);
  3451. if (buf_state == CAM_SMMU_BUFF_NOT_EXIST) {
  3452. CAM_ERR(CAM_SMMU, "ion_fd:%d not in the mapped list", ion_fd);
  3453. rc = -EINVAL;
  3454. cam_smmu_dump_cb_info(idx);
  3455. goto get_addr_end;
  3456. }
  3457. if (buf_tracker)
  3458. rc = cam_smmu_add_buf_to_track_list(ion_fd, i_ino, ref_count, buf_tracker, idx);
  3459. get_addr_end:
  3460. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3461. return rc;
  3462. }
  3463. EXPORT_SYMBOL(cam_smmu_get_iova);
  3464. int cam_smmu_get_stage2_iova(int handle, int ion_fd, struct dma_buf *dma_buf,
  3465. dma_addr_t *paddr_ptr, size_t *len_ptr, struct list_head *buf_tracker,
  3466. struct kref **ref_count)
  3467. {
  3468. int idx, rc = 0;
  3469. enum cam_smmu_buf_state buf_state;
  3470. unsigned long i_ino;
  3471. if (!paddr_ptr || !len_ptr) {
  3472. CAM_ERR(CAM_SMMU, "Error: Input pointers are invalid");
  3473. return -EINVAL;
  3474. }
  3475. if (handle == HANDLE_INIT) {
  3476. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3477. return -EINVAL;
  3478. }
  3479. /* clean the content from clients */
  3480. *paddr_ptr = (dma_addr_t)NULL;
  3481. *len_ptr = (size_t)0;
  3482. idx = GET_SMMU_TABLE_IDX(handle);
  3483. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3484. CAM_ERR(CAM_SMMU,
  3485. "Error: handle or index invalid. idx = %d hdl = %x",
  3486. idx, handle);
  3487. return -EINVAL;
  3488. }
  3489. if (!iommu_cb_set.cb_info[idx].is_secure) {
  3490. CAM_ERR(CAM_SMMU,
  3491. "Error: can't get secure mem from non secure cb");
  3492. return -EINVAL;
  3493. }
  3494. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3495. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3496. CAM_ERR(CAM_SMMU,
  3497. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3498. iommu_cb_set.cb_info[idx].handle, handle);
  3499. rc = -EINVAL;
  3500. goto get_addr_end;
  3501. }
  3502. buf_state = cam_smmu_validate_secure_fd_in_list(idx, ion_fd, dma_buf, paddr_ptr, len_ptr,
  3503. &i_ino, ref_count);
  3504. if (buf_state == CAM_SMMU_BUFF_NOT_EXIST) {
  3505. CAM_ERR(CAM_SMMU, "ion_fd:%d not in the mapped list", ion_fd);
  3506. rc = -EINVAL;
  3507. goto get_addr_end;
  3508. }
  3509. if (buf_tracker)
  3510. rc = cam_smmu_add_buf_to_track_list(ion_fd, i_ino, ref_count, buf_tracker, idx);
  3511. get_addr_end:
  3512. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3513. return rc;
  3514. }
  3515. EXPORT_SYMBOL(cam_smmu_get_stage2_iova);
  3516. static int cam_smmu_unmap_validate_params(int handle)
  3517. {
  3518. int idx;
  3519. if (handle == HANDLE_INIT) {
  3520. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3521. return -EINVAL;
  3522. }
  3523. /* find index in the iommu_cb_set.cb_info */
  3524. idx = GET_SMMU_TABLE_IDX(handle);
  3525. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3526. CAM_ERR(CAM_SMMU,
  3527. "Error: handle or index invalid. idx = %d hdl = %x",
  3528. idx, handle);
  3529. return -EINVAL;
  3530. }
  3531. return 0;
  3532. }
  3533. int cam_smmu_unmap_user_iova(int handle,
  3534. int ion_fd, struct dma_buf *dma_buf, enum cam_smmu_region_id region_id,
  3535. bool force_unmap)
  3536. {
  3537. int idx, rc;
  3538. struct cam_dma_buff_info *mapping_info;
  3539. rc = cam_smmu_unmap_validate_params(handle);
  3540. if (rc) {
  3541. CAM_ERR(CAM_SMMU, "unmap util validation failure");
  3542. return rc;
  3543. }
  3544. idx = GET_SMMU_TABLE_IDX(handle);
  3545. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3546. if (iommu_cb_set.cb_info[idx].is_secure) {
  3547. CAM_ERR(CAM_SMMU,
  3548. "Error: can't unmap non-secure mem from secure cb");
  3549. rc = -EINVAL;
  3550. goto unmap_end;
  3551. }
  3552. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3553. CAM_ERR(CAM_SMMU,
  3554. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3555. iommu_cb_set.cb_info[idx].handle, handle);
  3556. rc = -EINVAL;
  3557. goto unmap_end;
  3558. }
  3559. /* Based on ion_fd & index, we can find mapping info of buffer */
  3560. mapping_info = cam_smmu_find_mapping_by_ion_index(idx, ion_fd, dma_buf);
  3561. if (!mapping_info) {
  3562. CAM_ERR(CAM_SMMU,
  3563. "Error: Invalid params idx = %d, fd = %d",
  3564. idx, ion_fd);
  3565. rc = -EINVAL;
  3566. goto unmap_end;
  3567. }
  3568. mapping_info->map_count--;
  3569. if (mapping_info->map_count > 0) {
  3570. CAM_DBG(CAM_SMMU,
  3571. "idx: %d, cb: %s fd = %d , ino: 0x%x, map_count: %d, ref_count: %d",
  3572. idx, iommu_cb_set.cb_info[idx].name[0], ion_fd,
  3573. mapping_info->i_ino, mapping_info->map_count,
  3574. kref_read(&mapping_info->ref_count));
  3575. rc = 0;
  3576. goto unmap_end;
  3577. }
  3578. mapping_info->map_count = 0;
  3579. if (!force_unmap && kref_read(&mapping_info->ref_count) > 1) {
  3580. CAM_ERR(CAM_SMMU,
  3581. "[SMMU_BT] Error: can't unmap buffer as it's still active, idx: %d, cb: %s, fd: 0x%x, ino: 0x%x, ref_count: %d",
  3582. idx, iommu_cb_set.cb_info[idx].name[0], ion_fd, mapping_info->i_ino,
  3583. kref_read(&mapping_info->ref_count));
  3584. rc = -EPERM;
  3585. goto unmap_end;
  3586. }
  3587. /* Unmapping one buffer from device */
  3588. CAM_DBG(CAM_SMMU, "SMMU: removing buffer idx = %d", idx);
  3589. rc = cam_smmu_unmap_buf_and_remove_from_list(mapping_info, idx);
  3590. if (rc < 0)
  3591. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  3592. unmap_end:
  3593. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3594. return rc;
  3595. }
  3596. EXPORT_SYMBOL(cam_smmu_unmap_user_iova);
  3597. int cam_smmu_unmap_kernel_iova(int handle,
  3598. struct dma_buf *buf, enum cam_smmu_region_id region_id)
  3599. {
  3600. int idx, rc;
  3601. struct cam_dma_buff_info *mapping_info;
  3602. rc = cam_smmu_unmap_validate_params(handle);
  3603. if (rc) {
  3604. CAM_ERR(CAM_SMMU, "unmap util validation failure");
  3605. return rc;
  3606. }
  3607. idx = GET_SMMU_TABLE_IDX(handle);
  3608. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3609. if (iommu_cb_set.cb_info[idx].is_secure) {
  3610. CAM_ERR(CAM_SMMU,
  3611. "Error: can't unmap non-secure mem from secure cb");
  3612. rc = -EINVAL;
  3613. goto unmap_end;
  3614. }
  3615. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3616. CAM_ERR(CAM_SMMU,
  3617. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3618. iommu_cb_set.cb_info[idx].handle, handle);
  3619. rc = -EINVAL;
  3620. goto unmap_end;
  3621. }
  3622. /* Based on dma_buf & index, we can find mapping info of buffer */
  3623. mapping_info = cam_smmu_find_mapping_by_dma_buf(idx, buf);
  3624. if (!mapping_info) {
  3625. CAM_ERR(CAM_SMMU,
  3626. "Error: Invalid params idx = %d, dma_buf = %pK",
  3627. idx, buf);
  3628. rc = -EINVAL;
  3629. goto unmap_end;
  3630. }
  3631. /* Unmapping one buffer from device */
  3632. CAM_DBG(CAM_SMMU, "SMMU: removing buffer idx = %d", idx);
  3633. rc = cam_smmu_unmap_buf_and_remove_from_list(mapping_info, idx);
  3634. if (rc < 0)
  3635. CAM_ERR(CAM_SMMU, "Error: unmap or remove list fail");
  3636. unmap_end:
  3637. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3638. return rc;
  3639. }
  3640. EXPORT_SYMBOL(cam_smmu_unmap_kernel_iova);
  3641. int cam_smmu_put_iova(int handle, int ion_fd, struct dma_buf *dma_buf)
  3642. {
  3643. int idx;
  3644. int rc = 0;
  3645. struct cam_dma_buff_info *mapping_info;
  3646. if (handle == HANDLE_INIT) {
  3647. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3648. return -EINVAL;
  3649. }
  3650. /* find index in the iommu_cb_set.cb_info */
  3651. idx = GET_SMMU_TABLE_IDX(handle);
  3652. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3653. CAM_ERR(CAM_SMMU,
  3654. "Error: handle or index invalid. idx = %d hdl = %x",
  3655. idx, handle);
  3656. return -EINVAL;
  3657. }
  3658. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3659. if (iommu_cb_set.cb_info[idx].handle != handle) {
  3660. CAM_ERR(CAM_SMMU,
  3661. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3662. iommu_cb_set.cb_info[idx].handle, handle);
  3663. rc = -EINVAL;
  3664. goto put_addr_end;
  3665. }
  3666. /* based on ion fd and index, we can find mapping info of buffer */
  3667. mapping_info = cam_smmu_find_mapping_by_ion_index(idx, ion_fd, dma_buf);
  3668. if (!mapping_info) {
  3669. CAM_ERR(CAM_SMMU, "Error: Invalid params idx = %d, fd = %d",
  3670. idx, ion_fd);
  3671. rc = -EINVAL;
  3672. goto put_addr_end;
  3673. }
  3674. put_addr_end:
  3675. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3676. return rc;
  3677. }
  3678. EXPORT_SYMBOL(cam_smmu_put_iova);
  3679. int cam_smmu_destroy_handle(int handle)
  3680. {
  3681. int idx;
  3682. if (handle == HANDLE_INIT) {
  3683. CAM_ERR(CAM_SMMU, "Error: Invalid handle");
  3684. return -EINVAL;
  3685. }
  3686. idx = GET_SMMU_TABLE_IDX(handle);
  3687. if (idx < 0 || idx >= iommu_cb_set.cb_num) {
  3688. CAM_ERR(CAM_SMMU,
  3689. "Error: handle or index invalid. idx = %d hdl = %x",
  3690. idx, handle);
  3691. return -EINVAL;
  3692. }
  3693. mutex_lock(&iommu_cb_set.cb_info[idx].lock);
  3694. if (CAM_SMMU_HDL_VALIDATE(iommu_cb_set.cb_info[idx].handle, handle)) {
  3695. CAM_ERR(CAM_SMMU,
  3696. "Error: hdl is not valid, table_hdl = %x, hdl = %x",
  3697. iommu_cb_set.cb_info[idx].handle, handle);
  3698. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3699. return -EINVAL;
  3700. }
  3701. if (!list_empty_careful(&iommu_cb_set.cb_info[idx].smmu_buf_list)) {
  3702. CAM_ERR(CAM_SMMU, "UMD %s buffer list is not clean",
  3703. iommu_cb_set.cb_info[idx].name[0]);
  3704. cam_smmu_print_user_list(idx);
  3705. cam_smmu_clean_user_buffer_list(idx);
  3706. }
  3707. if (!list_empty_careful(
  3708. &iommu_cb_set.cb_info[idx].smmu_buf_kernel_list)) {
  3709. CAM_ERR(CAM_SMMU, "KMD %s buffer list is not clean",
  3710. iommu_cb_set.cb_info[idx].name[0]);
  3711. cam_smmu_print_kernel_list(idx);
  3712. cam_smmu_clean_kernel_buffer_list(idx);
  3713. }
  3714. if (iommu_cb_set.cb_info[idx].is_secure) {
  3715. if (iommu_cb_set.cb_info[idx].secure_count == 0) {
  3716. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3717. return -EPERM;
  3718. }
  3719. iommu_cb_set.cb_info[idx].secure_count--;
  3720. if (iommu_cb_set.cb_info[idx].secure_count == 0) {
  3721. iommu_cb_set.cb_info[idx].cb_count = 0;
  3722. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3723. }
  3724. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3725. return 0;
  3726. }
  3727. if (iommu_cb_set.cb_info[idx].is_mul_client &&
  3728. iommu_cb_set.cb_info[idx].device_count) {
  3729. iommu_cb_set.cb_info[idx].device_count--;
  3730. if (!iommu_cb_set.cb_info[idx].device_count) {
  3731. iommu_cb_set.cb_info[idx].cb_count = 0;
  3732. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3733. }
  3734. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3735. return 0;
  3736. }
  3737. iommu_cb_set.cb_info[idx].device_count = 0;
  3738. iommu_cb_set.cb_info[idx].cb_count = 0;
  3739. iommu_cb_set.cb_info[idx].handle = HANDLE_INIT;
  3740. mutex_unlock(&iommu_cb_set.cb_info[idx].lock);
  3741. return 0;
  3742. }
  3743. EXPORT_SYMBOL(cam_smmu_destroy_handle);
  3744. static void cam_smmu_deinit_cb(struct cam_context_bank_info *cb)
  3745. {
  3746. int i;
  3747. if (cb->io_support && cb->domain)
  3748. cb->domain = NULL;
  3749. if (cb->shared_support) {
  3750. for (i = 0; i < cb->shared_info.num_regions; i++) {
  3751. if (cb->shared_mem_pool[i]) {
  3752. gen_pool_destroy(cb->shared_mem_pool[i]);
  3753. cb->shared_mem_pool[i] = NULL;
  3754. }
  3755. }
  3756. }
  3757. if (cb->scratch_buf_support) {
  3758. kfree(cb->scratch_map.bitmap);
  3759. cb->scratch_map.bitmap = NULL;
  3760. }
  3761. }
  3762. static void cam_smmu_release_cb(struct platform_device *pdev)
  3763. {
  3764. int i = 0;
  3765. for (i = 0; i < iommu_cb_set.cb_num; i++)
  3766. cam_smmu_deinit_cb(&iommu_cb_set.cb_info[i]);
  3767. devm_kfree(&pdev->dev, iommu_cb_set.cb_info);
  3768. iommu_cb_set.cb_num = 0;
  3769. }
  3770. static int cam_smmu_setup_cb(struct cam_context_bank_info *cb,
  3771. struct device *dev)
  3772. {
  3773. int rc = 0, i;
  3774. if (!cb || !dev) {
  3775. CAM_ERR(CAM_SMMU, "Error: invalid input params");
  3776. return -EINVAL;
  3777. }
  3778. cb->dev = dev;
  3779. cb->is_fw_allocated = false;
  3780. cb->is_secheap_allocated = false;
  3781. atomic64_set(&cb->monitor_head, -1);
  3782. /* Create a pool with 64K granularity for supporting shared memory */
  3783. if (cb->shared_support) {
  3784. for (i = 0; i < cb->shared_info.num_regions; i++) {
  3785. cb->shared_mem_pool[i] = gen_pool_create(
  3786. SHARED_MEM_POOL_GRANULARITY, -1);
  3787. if (!cb->shared_mem_pool[i])
  3788. goto end;
  3789. rc = gen_pool_add(cb->shared_mem_pool[i],
  3790. cb->shared_info.nested_regions[i].region_info.iova_start,
  3791. cb->shared_info.nested_regions[i].region_info.iova_len,
  3792. -1);
  3793. if (rc) {
  3794. CAM_ERR(CAM_SMMU, "Genpool chunk creation failed");
  3795. gen_pool_destroy(cb->shared_mem_pool[i]);
  3796. cb->shared_mem_pool[i] = NULL;
  3797. goto end;
  3798. }
  3799. CAM_DBG(CAM_SMMU, "cb: %s Shared mem start->%lX len->%zu",
  3800. cb->name[0], (unsigned long)
  3801. cb->shared_info.nested_regions[i].region_info.iova_start,
  3802. cb->shared_info.nested_regions[i].region_info.iova_len);
  3803. }
  3804. }
  3805. if (cb->scratch_buf_support) {
  3806. rc = cam_smmu_init_scratch_map(&cb->scratch_map,
  3807. cb->scratch_info.iova_start,
  3808. cb->scratch_info.iova_len,
  3809. 0);
  3810. if (rc < 0) {
  3811. CAM_ERR(CAM_SMMU,
  3812. "Error: failed to create scratch map");
  3813. rc = -ENODEV;
  3814. goto end;
  3815. }
  3816. }
  3817. /* create a virtual mapping */
  3818. if (cb->io_support) {
  3819. cb->domain = iommu_get_domain_for_dev(dev);
  3820. if (IS_ERR_OR_NULL(cb->domain)) {
  3821. CAM_ERR(CAM_SMMU, "Error: create domain Failed");
  3822. rc = -ENODEV;
  3823. goto end;
  3824. }
  3825. /* Enable custom iommu features, if applicable */
  3826. cam_smmu_util_iommu_custom(dev, cb->discard_iova_start,
  3827. cb->discard_iova_len);
  3828. cb->state = CAM_SMMU_ATTACH;
  3829. } else {
  3830. CAM_ERR(CAM_SMMU, "Context bank does not have IO region");
  3831. rc = -ENODEV;
  3832. goto end;
  3833. }
  3834. return rc;
  3835. end:
  3836. if (cb->shared_support) {
  3837. for (--i; i >= 0; i--) {
  3838. if (cb->shared_mem_pool[i]) {
  3839. gen_pool_destroy(cb->shared_mem_pool[i]);
  3840. cb->shared_mem_pool[i] = NULL;
  3841. }
  3842. }
  3843. }
  3844. if (cb->scratch_buf_support) {
  3845. kfree(cb->scratch_map.bitmap);
  3846. cb->scratch_map.bitmap = NULL;
  3847. }
  3848. return rc;
  3849. }
  3850. static int cam_alloc_smmu_context_banks(struct device *dev)
  3851. {
  3852. struct device_node *domains_child_node = NULL;
  3853. if (!dev) {
  3854. CAM_ERR(CAM_SMMU, "Error: Invalid device");
  3855. return -ENODEV;
  3856. }
  3857. iommu_cb_set.cb_num = 0;
  3858. /* traverse thru all the child nodes and increment the cb count */
  3859. for_each_available_child_of_node(dev->of_node, domains_child_node) {
  3860. if (of_device_is_compatible(domains_child_node,
  3861. "qcom,msm-cam-smmu-cb"))
  3862. iommu_cb_set.cb_num++;
  3863. if (of_device_is_compatible(domains_child_node,
  3864. "qcom,qsmmu-cam-cb"))
  3865. iommu_cb_set.cb_num++;
  3866. }
  3867. if (iommu_cb_set.cb_num == 0) {
  3868. CAM_ERR(CAM_SMMU, "Error: no context banks present");
  3869. return -ENOENT;
  3870. }
  3871. /* allocate memory for the context banks */
  3872. iommu_cb_set.cb_info = devm_kzalloc(dev,
  3873. iommu_cb_set.cb_num * sizeof(struct cam_context_bank_info),
  3874. GFP_KERNEL);
  3875. if (!iommu_cb_set.cb_info) {
  3876. CAM_ERR(CAM_SMMU, "Error: cannot allocate context banks");
  3877. return -ENOMEM;
  3878. }
  3879. cam_smmu_reset_iommu_table(CAM_SMMU_TABLE_INIT);
  3880. iommu_cb_set.cb_init_count = 0;
  3881. CAM_DBG(CAM_SMMU, "no of context banks :%d", iommu_cb_set.cb_num);
  3882. return 0;
  3883. }
  3884. static int cam_smmu_get_discard_memory_regions(struct device_node *of_node,
  3885. dma_addr_t *discard_iova_start, size_t *discard_iova_len)
  3886. {
  3887. uint32_t discard_iova[2] = { 0 };
  3888. int num_values = 0;
  3889. int rc = 0;
  3890. if (!discard_iova_start || !discard_iova_len)
  3891. return -EINVAL;
  3892. *discard_iova_start = 0;
  3893. *discard_iova_len = 0;
  3894. num_values = of_property_count_u32_elems(of_node,
  3895. "iova-region-discard");
  3896. if (num_values <= 0) {
  3897. CAM_DBG(CAM_UTIL, "No discard region specified");
  3898. return 0;
  3899. } else if (num_values != 2) {
  3900. CAM_ERR(CAM_UTIL, "Invalid discard region specified %d",
  3901. num_values);
  3902. return -EINVAL;
  3903. }
  3904. rc = of_property_read_u32_array(of_node,
  3905. "iova-region-discard",
  3906. discard_iova, num_values);
  3907. if (rc) {
  3908. CAM_ERR(CAM_UTIL, "Can not read discard region %d", num_values);
  3909. return rc;
  3910. } else if (!discard_iova[0] || !discard_iova[1]) {
  3911. CAM_ERR(CAM_UTIL,
  3912. "Incorrect Discard region specified [0x%x 0x%x]",
  3913. discard_iova[0], discard_iova[1]);
  3914. return -EINVAL;
  3915. }
  3916. CAM_DBG(CAM_UTIL, "Discard region [0x%x 0x%x]",
  3917. discard_iova[0], discard_iova[0] + discard_iova[1]);
  3918. *discard_iova_start = discard_iova[0];
  3919. *discard_iova_len = discard_iova[1];
  3920. return 0;
  3921. }
  3922. static int cam_smmu_get_iova_info_util(
  3923. struct device_node **child_node, dma_addr_t *region_start,
  3924. size_t *region_len, uint32_t *region_id)
  3925. {
  3926. int rc;
  3927. uint32_t id;
  3928. dma_addr_t start = 0;
  3929. size_t len = 0;
  3930. if (iommu_cb_set.is_expanded_memory) {
  3931. rc = of_property_read_u64(*child_node, "iova-region-start", &start);
  3932. if (rc < 0) {
  3933. CAM_ERR(CAM_SMMU, "Failed to read iova-region-start");
  3934. return -EINVAL;
  3935. }
  3936. rc = of_property_read_u64(*child_node, "iova-region-len",
  3937. (uint64_t *)&len);
  3938. if (rc < 0) {
  3939. CAM_ERR(CAM_SMMU, "Failed to read iova-region-len");
  3940. return -EINVAL;
  3941. }
  3942. } else {
  3943. rc = of_property_read_u32(*child_node, "iova-region-start",
  3944. (uint32_t *)&start);
  3945. if (rc < 0) {
  3946. CAM_ERR(CAM_SMMU, "Failed to read iova-region-start");
  3947. return -EINVAL;
  3948. }
  3949. rc = of_property_read_u32(*child_node, "iova-region-len",
  3950. (uint32_t *)&len);
  3951. if (rc < 0) {
  3952. CAM_ERR(CAM_SMMU, "Failed to read iova-region-len");
  3953. return -EINVAL;
  3954. }
  3955. }
  3956. rc = of_property_read_u32(*child_node, "iova-region-id", &id);
  3957. if (rc < 0) {
  3958. CAM_ERR(CAM_SMMU, "Failed to read iova-region-id");
  3959. return -EINVAL;
  3960. }
  3961. *region_start = start;
  3962. *region_len = len;
  3963. *region_id = id;
  3964. return 0;
  3965. }
  3966. static int cam_smmu_get_subregions_memory_info(
  3967. struct device_node **of_node,
  3968. struct cam_smmu_nested_region_info *nested_regions,
  3969. struct cam_context_bank_info *cb)
  3970. {
  3971. int rc;
  3972. uint32_t subregion_count = 0, subregion_mask = 0;
  3973. enum cam_smmu_subregion_id subregion_id;
  3974. struct device_node *sub_node = NULL;
  3975. const char *subregion_name;
  3976. dma_addr_t subregion_start = 0;
  3977. size_t subregion_len = 0;
  3978. struct cam_smmu_subregion_info *subregions = NULL;
  3979. struct device_node *child_node = *of_node;
  3980. /* Count number of child nodes */
  3981. for_each_available_child_of_node(child_node, sub_node)
  3982. subregion_count++;
  3983. if (subregion_count > CAM_SMMU_SUBREGION_MAX) {
  3984. CAM_ERR(CAM_SMMU,
  3985. "Invalid number of subregions max=%u count=%u cb=%s",
  3986. CAM_SMMU_SUBREGION_MAX, subregion_count, cb->name[0]);
  3987. return -EINVAL;
  3988. }
  3989. for_each_available_child_of_node(child_node, sub_node) {
  3990. subregions =
  3991. &nested_regions->subregions[nested_regions->num_subregions];
  3992. rc = of_property_read_string(sub_node,
  3993. "iova-region-name", &subregion_name);
  3994. if (rc < 0) {
  3995. CAM_ERR(CAM_SMMU, "IOVA subregion not found");
  3996. goto err;
  3997. }
  3998. rc = cam_smmu_get_iova_info_util(&sub_node, &subregion_start,
  3999. &subregion_len, &subregion_id);
  4000. if (rc)
  4001. goto err;
  4002. switch (subregion_id) {
  4003. case CAM_SMMU_SUBREGION_GENERIC:
  4004. if (subregion_mask & BIT(subregion_id))
  4005. goto repeated_subregion;
  4006. subregions->subregion_id = subregion_id;
  4007. subregions->subregion_info.iova_len = subregion_len;
  4008. subregions->subregion_info.iova_start = subregion_start;
  4009. break;
  4010. case CAM_SMMU_SUBREGION_SYNX_HWMUTEX:
  4011. if (subregion_mask & BIT(subregion_id))
  4012. goto repeated_subregion;
  4013. subregions->subregion_id = subregion_id;
  4014. subregions->subregion_info.iova_len = subregion_len;
  4015. subregions->subregion_info.iova_start = subregion_start;
  4016. rc = of_property_read_u32(sub_node,
  4017. "phy-addr", (uint32_t *)&subregions->subregion_info.phy_addr);
  4018. if (rc < 0) {
  4019. CAM_ERR(CAM_SMMU, "Failed to read phy addr");
  4020. goto err;
  4021. }
  4022. break;
  4023. case CAM_SMMU_SUBREGION_IPC_HWMUTEX:
  4024. if (subregion_mask & BIT(subregion_id))
  4025. goto repeated_subregion;
  4026. subregions->subregion_id = subregion_id;
  4027. subregions->subregion_info.iova_len = subregion_len;
  4028. subregions->subregion_info.iova_start = subregion_start;
  4029. rc = of_property_read_u32(sub_node,
  4030. "phy-addr", (uint32_t *)&subregions->subregion_info.phy_addr);
  4031. if (rc < 0) {
  4032. CAM_ERR(CAM_SMMU, "Failed to read phy addr");
  4033. goto err;
  4034. }
  4035. break;
  4036. case CAM_SMMU_SUBREGION_GLOBAL_SYNC_MEM:
  4037. if (subregion_mask & BIT(subregion_id))
  4038. goto repeated_subregion;
  4039. subregions->subregion_id = subregion_id;
  4040. subregions->subregion_info.iova_len = subregion_len;
  4041. subregions->subregion_info.iova_start = subregion_start;
  4042. rc = of_property_read_u32(sub_node,
  4043. "phy-addr", (uint32_t *)&subregions->subregion_info.phy_addr);
  4044. if (rc < 0) {
  4045. CAM_ERR(CAM_SMMU, "Failed to read phy addr");
  4046. goto err;
  4047. }
  4048. break;
  4049. case CAM_SMMU_SUBREGION_GLOBAL_CNTR:
  4050. if (subregion_mask & BIT(subregion_id))
  4051. goto repeated_subregion;
  4052. subregions->subregion_id = subregion_id;
  4053. subregions->subregion_info.iova_len = subregion_len;
  4054. subregions->subregion_info.iova_start = subregion_start;
  4055. rc = of_property_read_u32(sub_node,
  4056. "phy-addr", (uint32_t *)&subregions->subregion_info.phy_addr);
  4057. if (rc < 0) {
  4058. CAM_ERR(CAM_SMMU, "Failed to read phy addr");
  4059. goto err;
  4060. }
  4061. break;
  4062. default:
  4063. CAM_ERR(CAM_SMMU, "Unsupported subregion_id: %d", subregion_id);
  4064. rc = -EINVAL;
  4065. goto err;
  4066. }
  4067. subregion_mask |= BIT(subregion_id);
  4068. nested_regions->num_subregions++;
  4069. CAM_DBG(CAM_SMMU,
  4070. "cb=%s region=%d iova=0x%x len=0x%x phy=0x%pK num_subregions=%u",
  4071. cb->name[0], subregion_id, subregions->subregion_info.iova_start,
  4072. subregions->subregion_info.iova_len,
  4073. subregions->subregion_info.phy_addr, nested_regions->num_subregions);
  4074. }
  4075. return 0;
  4076. repeated_subregion:
  4077. CAM_ERR(CAM_SMMU,
  4078. "Subregion=%u was already populated in cb=%s subregion_mask=0x%x",
  4079. subregion_id, cb->name[0], subregion_mask);
  4080. rc = -EINVAL;
  4081. err:
  4082. of_node_put(child_node);
  4083. return rc;
  4084. }
  4085. static int cam_smmu_validate_discard_iova_region(
  4086. struct cam_context_bank_info *cb,
  4087. struct cam_smmu_region_info *region_info)
  4088. {
  4089. /* Make sure Discard region is properly specified */
  4090. if ((cb->discard_iova_start !=
  4091. region_info->discard_iova_start) ||
  4092. (cb->discard_iova_len !=
  4093. region_info->discard_iova_len)) {
  4094. CAM_ERR(CAM_SMMU,
  4095. "Mismatch Discard region specified, [0x%x 0x%x] [0x%x 0x%x]",
  4096. cb->discard_iova_start,
  4097. cb->discard_iova_len,
  4098. region_info->discard_iova_start,
  4099. region_info->discard_iova_len);
  4100. return -EINVAL;
  4101. } else if (cb->discard_iova_start && cb->discard_iova_len) {
  4102. if ((cb->discard_iova_start <=
  4103. region_info->iova_start) ||
  4104. (cb->discard_iova_start >=
  4105. region_info->iova_start + region_info->iova_len) ||
  4106. (cb->discard_iova_start + cb->discard_iova_len >=
  4107. region_info->iova_start + region_info->iova_len)) {
  4108. CAM_ERR(CAM_SMMU,
  4109. "[%s] : Incorrect Discard region specified [0x%x 0x%x] in [0x%x 0x%x]",
  4110. cb->name[0],
  4111. cb->discard_iova_start,
  4112. cb->discard_iova_start + cb->discard_iova_len,
  4113. region_info->iova_start,
  4114. region_info->iova_start + region_info->iova_len);
  4115. return -EINVAL;
  4116. }
  4117. CAM_INFO(CAM_SMMU,
  4118. "[%s] : Discard region specified [0x%x 0x%x] in [0x%x 0x%x]",
  4119. cb->name[0],
  4120. cb->discard_iova_start,
  4121. cb->discard_iova_start + cb->discard_iova_len,
  4122. region_info->iova_start,
  4123. region_info->iova_start + region_info->iova_len);
  4124. }
  4125. return 0;
  4126. }
  4127. static int cam_smmu_get_memory_regions_info(struct device_node *of_node,
  4128. struct cam_context_bank_info *cb)
  4129. {
  4130. int rc = 0, i;
  4131. struct device_node *mem_map_node = NULL;
  4132. struct device_node *child_node = NULL;
  4133. dma_addr_t region_start = 0;
  4134. size_t region_len = 0;
  4135. uint32_t region_id;
  4136. const char *region_name;
  4137. int num_regions = 0;
  4138. if (!of_node || !cb) {
  4139. CAM_ERR(CAM_SMMU, "Invalid argument(s)");
  4140. return -EINVAL;
  4141. }
  4142. mem_map_node = of_get_child_by_name(of_node, "iova-mem-map");
  4143. cb->is_secure = of_property_read_bool(of_node, "qcom,secure-cb");
  4144. /*
  4145. * We always expect a memory map node, except when it is a secure
  4146. * context bank.
  4147. */
  4148. if (!mem_map_node) {
  4149. if (cb->is_secure)
  4150. return 0;
  4151. CAM_ERR(CAM_SMMU, "iova-mem-map not present");
  4152. return -EINVAL;
  4153. }
  4154. for_each_available_child_of_node(mem_map_node, child_node) {
  4155. num_regions++;
  4156. rc = of_property_read_string(child_node,
  4157. "iova-region-name", &region_name);
  4158. if (rc < 0) {
  4159. of_node_put(mem_map_node);
  4160. CAM_ERR(CAM_SMMU, "IOVA region not found");
  4161. return -EINVAL;
  4162. }
  4163. rc = cam_smmu_get_iova_info_util(&child_node,
  4164. &region_start, &region_len, &region_id);
  4165. if (rc) {
  4166. of_node_put(mem_map_node);
  4167. return rc;
  4168. }
  4169. switch (region_id) {
  4170. case CAM_SMMU_REGION_FIRMWARE:
  4171. cb->firmware_support = 1;
  4172. cb->firmware_info.iova_start = region_start;
  4173. cb->firmware_info.iova_len = region_len;
  4174. break;
  4175. case CAM_SMMU_REGION_SHARED: {
  4176. int32_t num_shared_regions = cb->shared_info.num_regions;
  4177. struct cam_smmu_nested_region_info *nested_reg_info;
  4178. if (num_shared_regions >= CAM_SMMU_MULTI_REGION_MAX) {
  4179. CAM_ERR(CAM_SMMU,
  4180. "Exceeding max supported number of regions max: %u current: %u in cb: %s for region: %d",
  4181. CAM_SMMU_MULTI_REGION_MAX, num_shared_regions,
  4182. cb->name[0], region_id);
  4183. rc = -EINVAL;
  4184. goto end;
  4185. }
  4186. nested_reg_info = &cb->shared_info.nested_regions[num_shared_regions];
  4187. nested_reg_info->subregion_support =
  4188. of_property_read_bool(child_node, "subregion_support");
  4189. if (nested_reg_info->subregion_support) {
  4190. rc = cam_smmu_get_subregions_memory_info(&child_node,
  4191. nested_reg_info, cb);
  4192. if (rc)
  4193. goto end;
  4194. }
  4195. nested_reg_info->region_info.iova_start = region_start;
  4196. nested_reg_info->region_info.iova_len = region_len;
  4197. cb->shared_info.num_regions++;
  4198. cb->shared_support = 1;
  4199. }
  4200. break;
  4201. case CAM_SMMU_REGION_SCRATCH:
  4202. cb->scratch_buf_support = 1;
  4203. cb->scratch_info.iova_start = region_start;
  4204. cb->scratch_info.iova_len = region_len;
  4205. break;
  4206. case CAM_SMMU_REGION_IO: {
  4207. int32_t num_io_regions = cb->io_info.num_regions;
  4208. struct cam_smmu_nested_region_info *nested_reg_info;
  4209. if (num_io_regions >= CAM_SMMU_MULTI_REGION_MAX) {
  4210. CAM_ERR(CAM_SMMU,
  4211. "Exceeding max supported number of regions max: %u current: %u in cb: %s for region: %d",
  4212. CAM_SMMU_MULTI_REGION_MAX, num_io_regions,
  4213. cb->name[0], region_id);
  4214. rc = -EINVAL;
  4215. goto end;
  4216. }
  4217. nested_reg_info = &cb->io_info.nested_regions[num_io_regions];
  4218. nested_reg_info->subregion_support =
  4219. of_property_read_bool(child_node, "subregion_support");
  4220. if (nested_reg_info->subregion_support) {
  4221. rc = cam_smmu_get_subregions_memory_info(&child_node,
  4222. nested_reg_info, cb);
  4223. if (rc)
  4224. goto end;
  4225. }
  4226. nested_reg_info->region_info.iova_start = region_start;
  4227. nested_reg_info->region_info.iova_len = region_len;
  4228. rc = cam_smmu_get_discard_memory_regions(child_node,
  4229. &nested_reg_info->region_info.discard_iova_start,
  4230. &nested_reg_info->region_info.discard_iova_len);
  4231. if (rc) {
  4232. CAM_ERR(CAM_SMMU,
  4233. "Invalid Discard region specified in IO region, rc: %d cb: %s",
  4234. rc, cb->name[0]);
  4235. goto end;
  4236. }
  4237. cb->io_info.num_regions++;
  4238. cb->io_support = 1;
  4239. }
  4240. break;
  4241. case CAM_SMMU_REGION_SECHEAP:
  4242. cb->secheap_support = 1;
  4243. cb->secheap_info.iova_start = region_start;
  4244. cb->secheap_info.iova_len = region_len;
  4245. break;
  4246. case CAM_SMMU_REGION_FWUNCACHED:{
  4247. int32_t num_fwuncached_regions = cb->fwuncached_region.num_regions;
  4248. struct cam_smmu_nested_region_info *nested_reg_info;
  4249. if (num_fwuncached_regions >= CAM_SMMU_MULTI_REGION_MAX) {
  4250. CAM_ERR(CAM_SMMU,
  4251. "Exceeding max supported number of regions max: %u current: %u in cb: %s for region: %d",
  4252. CAM_SMMU_MULTI_REGION_MAX, num_fwuncached_regions,
  4253. cb->name[0], region_id);
  4254. rc = -EINVAL;
  4255. goto end;
  4256. }
  4257. nested_reg_info =
  4258. &cb->fwuncached_region.nested_regions[num_fwuncached_regions];
  4259. nested_reg_info->subregion_support =
  4260. of_property_read_bool(child_node, "subregion_support");
  4261. if (nested_reg_info->subregion_support) {
  4262. rc = cam_smmu_get_subregions_memory_info(&child_node,
  4263. nested_reg_info, cb);
  4264. if (rc)
  4265. goto end;
  4266. }
  4267. nested_reg_info->region_info.iova_start = region_start;
  4268. nested_reg_info->region_info.iova_len = region_len;
  4269. cb->fwuncached_region.num_regions++;
  4270. cb->fwuncached_region_support = 1;
  4271. }
  4272. break;
  4273. case CAM_SMMU_REGION_QDSS: {
  4274. int32_t num_qdss_regions = cb->qdss_info.num_regions;
  4275. struct cam_smmu_nested_region_info *nested_reg_info;
  4276. if (num_qdss_regions >= CAM_SMMU_MULTI_REGION_MAX) {
  4277. CAM_ERR(CAM_SMMU,
  4278. "Exceeding max supported number of regions max: %u current: %u in cb: %s for region: %d",
  4279. CAM_SMMU_MULTI_REGION_MAX, num_qdss_regions,
  4280. cb->name[0], region_id);
  4281. rc = -EINVAL;
  4282. goto end;
  4283. }
  4284. nested_reg_info =
  4285. &cb->qdss_info.nested_regions[num_qdss_regions];
  4286. nested_reg_info->subregion_support =
  4287. of_property_read_bool(child_node, "subregion_support");
  4288. if (nested_reg_info->subregion_support) {
  4289. CAM_ERR(CAM_SMMU,
  4290. "Subregion for QDSS not supported, failing cb: %s initialization",
  4291. cb->name[0]);
  4292. goto end;
  4293. }
  4294. nested_reg_info->region_info.iova_start = region_start;
  4295. nested_reg_info->region_info.iova_len = region_len;
  4296. /* phy-addr field is mandatory for QDSS */
  4297. rc = of_property_read_u32(child_node, "qdss-phy-addr",
  4298. (uint32_t *)&nested_reg_info->region_info.phy_addr);
  4299. if (rc) {
  4300. CAM_ERR(CAM_SMMU, "No phy-addr field for qdss in cb: %s",
  4301. cb->name[0]);
  4302. goto end;
  4303. }
  4304. cb->qdss_info.num_regions++;
  4305. cb->qdss_support = 1;
  4306. }
  4307. break;
  4308. case CAM_SMMU_REGION_DEVICE:{
  4309. int32_t num_device_regions = cb->device_region.num_regions;
  4310. struct cam_smmu_nested_region_info *nested_reg_info;
  4311. if (num_device_regions >= CAM_SMMU_MULTI_REGION_MAX) {
  4312. CAM_ERR(CAM_SMMU,
  4313. "Exceeding max supported number of regions max: %u current: %u in cb: %s for region: %d",
  4314. CAM_SMMU_MULTI_REGION_MAX, num_device_regions,
  4315. cb->name[0], region_id);
  4316. rc = -EINVAL;
  4317. goto end;
  4318. }
  4319. nested_reg_info = &cb->device_region.nested_regions[num_device_regions];
  4320. nested_reg_info->subregion_support =
  4321. of_property_read_bool(child_node, "subregion_support");
  4322. if (nested_reg_info->subregion_support) {
  4323. rc = cam_smmu_get_subregions_memory_info(&child_node,
  4324. nested_reg_info, cb);
  4325. if (rc)
  4326. goto end;
  4327. }
  4328. nested_reg_info->region_info.iova_start = region_start;
  4329. nested_reg_info->region_info.iova_len = region_len;
  4330. rc = of_property_read_u32(child_node,
  4331. "phy-addr", (uint32_t *)&nested_reg_info->region_info.phy_addr);
  4332. if (rc) {
  4333. CAM_DBG(CAM_SMMU, "No phy-addr field in device in cb: %s",
  4334. cb->name[0]);
  4335. rc = 0;
  4336. }
  4337. cb->device_region.num_regions++;
  4338. cb->device_region_support = 1;
  4339. }
  4340. break;
  4341. default:
  4342. CAM_ERR(CAM_SMMU,
  4343. "Incorrect region id present in DT file: %d",
  4344. region_id);
  4345. }
  4346. CAM_DBG(CAM_SMMU, "Found label -> %s", cb->name[0]);
  4347. CAM_DBG(CAM_SMMU, "Found region -> %s", region_name);
  4348. CAM_DBG(CAM_SMMU, "region_start -> 0x%lx", region_start);
  4349. CAM_DBG(CAM_SMMU, "region_len -> 0x%lx", region_len);
  4350. CAM_DBG(CAM_SMMU, "region_id -> 0x%x", region_id);
  4351. }
  4352. if (cb->io_support) {
  4353. rc = cam_smmu_get_discard_memory_regions(of_node,
  4354. &cb->discard_iova_start,
  4355. &cb->discard_iova_len);
  4356. if (rc) {
  4357. CAM_ERR(CAM_SMMU,
  4358. "Invalid Discard region specified in CB, rc=%d",
  4359. rc);
  4360. return -EINVAL;
  4361. }
  4362. for (i = 0; i < cb->io_info.num_regions; i++) {
  4363. rc = cam_smmu_validate_discard_iova_region(cb,
  4364. &cb->io_info.nested_regions[i].region_info);
  4365. if (rc)
  4366. goto end;
  4367. }
  4368. }
  4369. if (!num_regions) {
  4370. CAM_ERR(CAM_SMMU,
  4371. "No memory regions found, at least one needed");
  4372. rc = -ENODEV;
  4373. }
  4374. return rc;
  4375. end:
  4376. of_node_put(mem_map_node);
  4377. return rc;
  4378. }
  4379. static void cam_smmu_check_for_fault_properties(
  4380. const char *fault_property, struct cam_context_bank_info *cb)
  4381. {
  4382. if (!strcmp(fault_property, "non-fatal"))
  4383. cb->non_fatal_faults_en = true;
  4384. else if (!strcmp(fault_property, "stall-disable"))
  4385. cb->stall_disable_en = true;
  4386. CAM_DBG(CAM_SMMU, "iommu fault property: %s found for cb: %s",
  4387. fault_property, cb->name[0]);
  4388. }
  4389. static int cam_populate_smmu_context_banks(struct device *dev,
  4390. enum cam_iommu_type type)
  4391. {
  4392. int rc = 0, i, j, num_fault_props = 0;
  4393. struct cam_context_bank_info *cb;
  4394. struct device *ctx = NULL;
  4395. bool dma_coherent, dma_coherent_hint, is_found;
  4396. if (!dev) {
  4397. CAM_ERR(CAM_SMMU, "Error: Invalid device");
  4398. return -ENODEV;
  4399. }
  4400. /* check the bounds */
  4401. if (iommu_cb_set.cb_init_count >= iommu_cb_set.cb_num) {
  4402. CAM_ERR(CAM_SMMU, "Error: populate more than allocated cb");
  4403. rc = -EBADHANDLE;
  4404. goto cb_init_fail;
  4405. }
  4406. /* read the context bank from cb set */
  4407. cb = &iommu_cb_set.cb_info[iommu_cb_set.cb_init_count];
  4408. cb->is_mul_client =
  4409. of_property_read_bool(dev->of_node, "multiple-client-devices");
  4410. cb->num_shared_hdl = of_property_count_strings(dev->of_node,
  4411. "cam-smmu-label");
  4412. if (cb->num_shared_hdl >
  4413. CAM_SMMU_SHARED_HDL_MAX) {
  4414. CAM_ERR(CAM_CDM, "Invalid count of client names count=%d",
  4415. cb->num_shared_hdl);
  4416. rc = -EINVAL;
  4417. return rc;
  4418. }
  4419. /* set the name of the context bank */
  4420. for (i = 0; i < cb->num_shared_hdl; i++) {
  4421. rc = of_property_read_string_index(dev->of_node,
  4422. "cam-smmu-label", i, &cb->name[i]);
  4423. if (rc < 0) {
  4424. CAM_ERR(CAM_SMMU,
  4425. "Error: failed to read label from sub device");
  4426. goto cb_init_fail;
  4427. }
  4428. }
  4429. cb->num_multi_regions = of_property_count_strings(dev->of_node,
  4430. "multiple-same-region-clients");
  4431. if (cb->num_multi_regions > CAM_SMMU_MULTI_REGION_MAX) {
  4432. CAM_ERR(CAM_CDM, "Invalid count of multi region clients = %d",
  4433. cb->num_multi_regions);
  4434. rc = -EINVAL;
  4435. goto cb_init_fail;
  4436. }
  4437. for (j = 0; j < cb->num_multi_regions; j++) {
  4438. is_found = false;
  4439. rc = of_property_read_string_index(dev->of_node,
  4440. "multiple-same-region-clients", j, &cb->multi_region_clients[j]);
  4441. if (rc < 0) {
  4442. CAM_ERR(CAM_SMMU,
  4443. "Error: failed to read label from sub device");
  4444. goto cb_init_fail;
  4445. }
  4446. /* Needs to match shared hdl client list */
  4447. for (i = 0; i < cb->num_shared_hdl; i++) {
  4448. if (strcmp(cb->name[i], cb->multi_region_clients[j])) {
  4449. is_found = true;
  4450. break;
  4451. }
  4452. }
  4453. if (!is_found) {
  4454. CAM_ERR(CAM_SMMU,
  4455. "%s multi region client not found in shared client list cb = %s",
  4456. cb->multi_region_clients[j], cb->name[0]);
  4457. rc = -EINVAL;
  4458. goto cb_init_fail;
  4459. }
  4460. }
  4461. rc = cam_smmu_get_memory_regions_info(dev->of_node, cb);
  4462. if (rc < 0) {
  4463. CAM_ERR(CAM_SMMU, "Error: Getting region info");
  4464. return rc;
  4465. }
  4466. if (cb->is_secure) {
  4467. /* increment count to next bank */
  4468. cb->dev = dev;
  4469. iommu_cb_set.cb_init_count++;
  4470. return 0;
  4471. }
  4472. /* set up the iommu mapping for the context bank */
  4473. if (type == CAM_QSMMU) {
  4474. CAM_ERR(CAM_SMMU, "Error: QSMMU ctx not supported for : %s",
  4475. cb->name[0]);
  4476. return -ENODEV;
  4477. }
  4478. ctx = dev;
  4479. CAM_DBG(CAM_SMMU, "getting Arm SMMU ctx : %s", cb->name[0]);
  4480. cb->coherency_mode = CAM_SMMU_NO_COHERENCY;
  4481. dma_coherent = of_property_read_bool(dev->of_node, "dma-coherent");
  4482. dma_coherent_hint = of_property_read_bool(dev->of_node,
  4483. "dma-coherent-hint-cached");
  4484. if (dma_coherent && dma_coherent_hint) {
  4485. CAM_ERR(CAM_SMMU,
  4486. "[%s] : Cannot enable both dma-coherent and dma-coherent-hint-cached",
  4487. cb->name[0]);
  4488. return -EBADR;
  4489. }
  4490. if (dma_coherent)
  4491. cb->coherency_mode = CAM_SMMU_DMA_COHERENT;
  4492. else if (dma_coherent_hint)
  4493. cb->coherency_mode = CAM_SMMU_DMA_COHERENT_HINT_CACHED;
  4494. CAM_DBG(CAM_SMMU, "[%s] : io cohereny mode %d", cb->name[0],
  4495. cb->coherency_mode);
  4496. rc = cam_smmu_setup_cb(cb, ctx);
  4497. if (rc < 0) {
  4498. CAM_ERR(CAM_SMMU, "Error: failed to setup cb : %s",
  4499. cb->name[0]);
  4500. goto cb_init_fail;
  4501. }
  4502. if (cb->io_support && cb->domain) {
  4503. iommu_set_fault_handler(cb->domain,
  4504. cam_smmu_iommu_fault_handler,
  4505. (void *)cb->name[0]);
  4506. num_fault_props = of_property_count_strings(dev->of_node, "qcom,iommu-faults");
  4507. if (num_fault_props > 0) {
  4508. const char *fault_property = NULL;
  4509. for (i = 0; i < num_fault_props; i++) {
  4510. rc = of_property_read_string_index(dev->of_node,
  4511. "qcom,iommu-faults", i, &fault_property);
  4512. if (!rc)
  4513. cam_smmu_check_for_fault_properties(fault_property, cb);
  4514. }
  4515. /* Missing fault property reads is not an error */
  4516. rc = 0;
  4517. }
  4518. }
  4519. if (!dev->dma_parms)
  4520. dev->dma_parms = devm_kzalloc(dev,
  4521. sizeof(*dev->dma_parms), GFP_KERNEL);
  4522. if (!dev->dma_parms) {
  4523. CAM_WARN(CAM_SMMU,
  4524. "Failed to allocate dma_params");
  4525. dev->dma_parms = NULL;
  4526. goto end;
  4527. }
  4528. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  4529. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  4530. if (iommu_cb_set.is_expanded_memory) {
  4531. CAM_DBG(CAM_SMMU, "[%s] setting max address mask", cb->name[0]);
  4532. /* the largest address is the min(dma_mask, value_from_iommu-dma_addr_pool) */
  4533. rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  4534. if (rc)
  4535. CAM_ERR(CAM_SMMU, "[%s] Failed in setting max address mask, rc %d",
  4536. cb->name[0], rc);
  4537. }
  4538. end:
  4539. /* increment count to next bank */
  4540. iommu_cb_set.cb_init_count++;
  4541. CAM_DBG(CAM_SMMU, "X: cb init count :%d", iommu_cb_set.cb_init_count);
  4542. cb_init_fail:
  4543. return rc;
  4544. }
  4545. static void cam_smmu_mini_dump_entries(
  4546. struct cam_smmu_mini_dump_cb_info *target,
  4547. struct cam_context_bank_info *src)
  4548. {
  4549. int i = 0;
  4550. int64_t state_head = 0;
  4551. uint32_t index, num_entries, oldest_entry;
  4552. state_head = atomic64_read(&src->monitor_head);
  4553. if (state_head == -1) {
  4554. return;
  4555. } else if (state_head < CAM_SMMU_MONITOR_MAX_ENTRIES) {
  4556. num_entries = state_head;
  4557. oldest_entry = 0;
  4558. } else {
  4559. num_entries = CAM_SMMU_MONITOR_MAX_ENTRIES;
  4560. div_u64_rem(state_head + 1,
  4561. CAM_SMMU_MONITOR_MAX_ENTRIES, &oldest_entry);
  4562. }
  4563. index = oldest_entry;
  4564. for (i = 0; i < num_entries; i++) {
  4565. memcpy(&target->mapping[index],
  4566. &src->monitor_entries[index],
  4567. sizeof(struct cam_smmu_monitor));
  4568. index = (index + 1) % CAM_SMMU_MONITOR_MAX_ENTRIES;
  4569. }
  4570. }
  4571. static unsigned long cam_smmu_mini_dump_cb(void *dst, unsigned long len,
  4572. void *priv_data)
  4573. {
  4574. struct cam_smmu_mini_dump_cb_info *cb_md;
  4575. struct cam_smmu_mini_dump_info *md;
  4576. struct cam_context_bank_info *cb;
  4577. unsigned long dumped_len = 0;
  4578. unsigned long remain_len = len;
  4579. uint32_t i = 0, j = 0;
  4580. if (!dst || len < sizeof(*md)) {
  4581. CAM_ERR(CAM_SMMU, "Invalid params dst: %pk len:%lu",
  4582. dst, len);
  4583. return 0;
  4584. }
  4585. md = (struct cam_smmu_mini_dump_info *)dst;
  4586. md->cb_num = 0;
  4587. md->cb = (struct cam_smmu_mini_dump_cb_info *)
  4588. ((uint8_t *)dst + sizeof(*md));
  4589. dumped_len += sizeof(*md);
  4590. remain_len = len - dumped_len;
  4591. for (i = 0; i < iommu_cb_set.cb_num; i++) {
  4592. if (remain_len < sizeof(*cb_md))
  4593. goto end;
  4594. cb = &iommu_cb_set.cb_info[i];
  4595. cb_md = &md->cb[i];
  4596. cb_md->is_mul_client = cb->is_mul_client;
  4597. cb_md->is_secure = cb->is_secure;
  4598. cb_md->is_fw_allocated = cb->is_fw_allocated;
  4599. cb_md->is_secheap_allocated = cb->is_secheap_allocated;
  4600. cb_md->is_qdss_allocated = cb->is_qdss_allocated;
  4601. cb_md->scratch_buf_support = cb->scratch_buf_support;
  4602. cb_md->firmware_support = cb->firmware_support;
  4603. cb_md->shared_support = cb->shared_support;
  4604. cb_md->io_support = cb->io_support;
  4605. cb_md->fwuncached_region_support = cb->fwuncached_region_support;
  4606. cb_md->qdss_support = cb->qdss_support;
  4607. cb_md->coherency_mode = cb->coherency_mode;
  4608. cb_md->state = cb->state;
  4609. cb_md->va_start = cb->va_start;
  4610. cb_md->discard_iova_start = cb->discard_iova_start;
  4611. cb_md->qdss_phy_addr = cb->qdss_phy_addr;
  4612. cb_md->va_len = cb->va_len;
  4613. cb_md->io_mapping_size = cb->io_mapping_size;
  4614. cb_md->shared_mapping_size = cb->shared_mapping_size;
  4615. cb_md->discard_iova_len = cb->discard_iova_len;
  4616. cb_md->handle = cb->handle;
  4617. cb_md->device_count = cb->device_count;
  4618. cb_md->num_shared_hdl = cb->num_shared_hdl;
  4619. cb_md->secure_count = cb->secure_count;
  4620. cb_md->cb_count = cb->cb_count;
  4621. cb_md->pf_count = cb->pf_count;
  4622. memcpy(&cb_md->scratch_info, &cb->scratch_info,
  4623. sizeof(struct cam_smmu_region_info));
  4624. memcpy(&cb_md->firmware_info, &cb->firmware_info,
  4625. sizeof(struct cam_smmu_region_info));
  4626. memcpy(&cb_md->shared_info, &cb->shared_info,
  4627. sizeof(struct cam_smmu_region_info));
  4628. memcpy(&cb_md->io_info, &cb->io_info,
  4629. sizeof(struct cam_smmu_region_info));
  4630. memcpy(&cb_md->secheap_info, &cb->secheap_info,
  4631. sizeof(struct cam_smmu_region_info));
  4632. memcpy(&cb_md->fwuncached_region, &cb->fwuncached_region,
  4633. sizeof(struct cam_smmu_region_info));
  4634. memcpy(&cb_md->qdss_info, &cb->qdss_info,
  4635. sizeof(struct cam_smmu_region_info));
  4636. memcpy(&cb_md->device_mem_region, &cb->device_region,
  4637. sizeof(struct cam_smmu_region_info));
  4638. for (j = 0; j < iommu_cb_set.cb_info[i].num_shared_hdl; j++)
  4639. scnprintf(cb_md->name[j], 16, cb->name[j]);
  4640. cam_smmu_mini_dump_entries(cb_md, cb);
  4641. dumped_len += sizeof(*cb_md);
  4642. remain_len = len - dumped_len;
  4643. md->cb_num++;
  4644. }
  4645. end:
  4646. return dumped_len;
  4647. }
  4648. static int cam_smmu_set_fatal_pf_mask(void *data, u64 val)
  4649. {
  4650. iommu_cb_set.debug_cfg.fatal_pf_mask = val;
  4651. CAM_DBG(CAM_SMMU, "Set fatal page fault value: 0x%llx",
  4652. iommu_cb_set.debug_cfg.fatal_pf_mask);
  4653. return 0;
  4654. }
  4655. static int cam_smmu_get_fatal_pf_mask(void *data, u64 *val)
  4656. {
  4657. *val = iommu_cb_set.debug_cfg.fatal_pf_mask;
  4658. CAM_DBG(CAM_SMMU, "Get fatal page fault value: 0x%llx",
  4659. *val);
  4660. return 0;
  4661. }
  4662. DEFINE_DEBUGFS_ATTRIBUTE(cam_smmu_fatal_pf_mask,
  4663. cam_smmu_get_fatal_pf_mask, cam_smmu_set_fatal_pf_mask, "%16llu");
  4664. static int cam_smmu_create_debug_fs(void)
  4665. {
  4666. int rc = 0;
  4667. struct dentry *dbgfileptr = NULL;
  4668. if (!cam_debugfs_available())
  4669. return 0;
  4670. rc = cam_debugfs_create_subdir("smmu", &dbgfileptr);
  4671. if (rc) {
  4672. CAM_ERR(CAM_SMMU,"DebugFS could not create directory!");
  4673. rc = -ENOENT;
  4674. goto end;
  4675. }
  4676. /* Store parent inode for cleanup in caller */
  4677. iommu_cb_set.debug_cfg.dentry = dbgfileptr;
  4678. debugfs_create_bool("cb_dump_enable", 0644,
  4679. iommu_cb_set.debug_cfg.dentry, &iommu_cb_set.debug_cfg.cb_dump_enable);
  4680. debugfs_create_bool("map_profile_enable", 0644,
  4681. iommu_cb_set.debug_cfg.dentry, &iommu_cb_set.debug_cfg.map_profile_enable);
  4682. debugfs_create_file("fatal_pf_mask", 0644,
  4683. iommu_cb_set.debug_cfg.dentry, NULL, &cam_smmu_fatal_pf_mask);
  4684. debugfs_create_bool("disable_buf_tracking", 0644,
  4685. iommu_cb_set.debug_cfg.dentry, &iommu_cb_set.debug_cfg.disable_buf_tracking);
  4686. end:
  4687. return rc;
  4688. }
  4689. int cam_smmu_driver_init(struct cam_csf_version *csf_ver, int32_t *num_cbs)
  4690. {
  4691. int i;
  4692. /* expect inputs to be valid */
  4693. if (!csf_ver || !num_cbs) {
  4694. CAM_ERR(CAM_SMMU, "Invalid params csf: %p num_cbs: %p",
  4695. csf_ver, num_cbs);
  4696. return -EINVAL;
  4697. }
  4698. *num_cbs = iommu_cb_set.cb_num;
  4699. memcpy(csf_ver, &iommu_cb_set.csf_version, sizeof(*csf_ver));
  4700. iommu_cb_set.is_track_buf_disabled = iommu_cb_set.debug_cfg.disable_buf_tracking;
  4701. if (!iommu_cb_set.is_track_buf_disabled) {
  4702. buf_tracking_pool = kcalloc(CAM_SMMU_BUF_TRACKING_POOL,
  4703. sizeof(struct cam_smmu_buffer_tracker), GFP_KERNEL);
  4704. if (!buf_tracking_pool) {
  4705. CAM_WARN(CAM_SMMU, "[SMMU_BT] Not enough mem for buffer tracker pool");
  4706. goto end;
  4707. }
  4708. INIT_LIST_HEAD(&iommu_cb_set.buf_tracker_free_list);
  4709. for (i = 0; i < CAM_SMMU_BUF_TRACKING_POOL; i++) {
  4710. INIT_LIST_HEAD(&buf_tracking_pool[i].list);
  4711. list_add_tail(&buf_tracking_pool[i].list,
  4712. &iommu_cb_set.buf_tracker_free_list);
  4713. }
  4714. }
  4715. end:
  4716. return 0;
  4717. }
  4718. void cam_smmu_driver_deinit(void)
  4719. {
  4720. INIT_LIST_HEAD(&iommu_cb_set.buf_tracker_free_list);
  4721. kfree(buf_tracking_pool);
  4722. }
  4723. static int cam_smmu_fw_dev_component_bind(struct device *dev,
  4724. struct device *master_dev, void *data)
  4725. {
  4726. struct platform_device *pdev = to_platform_device(dev);
  4727. icp_fw.fw_dev = &pdev->dev;
  4728. icp_fw.fw_kva = NULL;
  4729. icp_fw.fw_hdl = 0;
  4730. CAM_DBG(CAM_SMMU, "FW dev component bound successfully");
  4731. return 0;
  4732. }
  4733. static void cam_smmu_fw_dev_component_unbind(struct device *dev,
  4734. struct device *master_dev, void *data)
  4735. {
  4736. struct platform_device *pdev = to_platform_device(dev);
  4737. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  4738. }
  4739. const static struct component_ops cam_smmu_fw_dev_component_ops = {
  4740. .bind = cam_smmu_fw_dev_component_bind,
  4741. .unbind = cam_smmu_fw_dev_component_unbind,
  4742. };
  4743. static int cam_smmu_cb_component_bind(struct device *dev,
  4744. struct device *master_dev, void *data)
  4745. {
  4746. int rc = 0;
  4747. struct platform_device *pdev = to_platform_device(dev);
  4748. rc = cam_populate_smmu_context_banks(dev, CAM_ARM_SMMU);
  4749. if (rc < 0) {
  4750. CAM_ERR(CAM_SMMU, "Error: populating context banks");
  4751. cam_smmu_release_cb(pdev);
  4752. return -ENOMEM;
  4753. }
  4754. CAM_DBG(CAM_SMMU, "CB component bound successfully");
  4755. return 0;
  4756. }
  4757. static void cam_smmu_cb_component_unbind(struct device *dev,
  4758. struct device *master_dev, void *data)
  4759. {
  4760. struct platform_device *pdev = to_platform_device(dev);
  4761. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  4762. }
  4763. const static struct component_ops cam_smmu_cb_component_ops = {
  4764. .bind = cam_smmu_cb_component_bind,
  4765. .unbind = cam_smmu_cb_component_unbind,
  4766. };
  4767. static int cam_smmu_cb_qsmmu_component_bind(struct device *dev,
  4768. struct device *master_dev, void *data)
  4769. {
  4770. int rc = 0;
  4771. rc = cam_populate_smmu_context_banks(dev, CAM_QSMMU);
  4772. if (rc < 0) {
  4773. CAM_ERR(CAM_SMMU, "Failed in populating context banks");
  4774. return -ENOMEM;
  4775. }
  4776. CAM_DBG(CAM_SMMU, "QSMMU CB component bound successfully");
  4777. return 0;
  4778. }
  4779. static void cam_smmu_cb_qsmmu_component_unbind(struct device *dev,
  4780. struct device *master_dev, void *data)
  4781. {
  4782. struct platform_device *pdev = to_platform_device(dev);
  4783. CAM_DBG(CAM_SMMU, "Unbinding component: %s", pdev->name);
  4784. }
  4785. const static struct component_ops cam_smmu_cb_qsmmu_component_ops = {
  4786. .bind = cam_smmu_cb_qsmmu_component_bind,
  4787. .unbind = cam_smmu_cb_qsmmu_component_unbind,
  4788. };
  4789. static int cam_smmu_component_bind(struct device *dev,
  4790. struct device *master_dev, void *data)
  4791. {
  4792. int rc;
  4793. INIT_WORK(&iommu_cb_set.smmu_work, cam_smmu_page_fault_work);
  4794. mutex_init(&iommu_cb_set.payload_list_lock);
  4795. spin_lock_init(&iommu_cb_set.s_lock);
  4796. INIT_LIST_HEAD(&iommu_cb_set.payload_list);
  4797. cam_smmu_create_debug_fs();
  4798. iommu_cb_set.force_cache_allocs =
  4799. of_property_read_bool(dev->of_node, "force_cache_allocs");
  4800. iommu_cb_set.need_shared_buffer_padding =
  4801. of_property_read_bool(dev->of_node, "need_shared_buffer_padding");
  4802. iommu_cb_set.is_expanded_memory =
  4803. of_property_read_bool(dev->of_node, "expanded_memory");
  4804. cam_common_register_mini_dump_cb(cam_smmu_mini_dump_cb,
  4805. "cam_smmu", NULL);
  4806. rc = cam_smmu_fetch_csf_version(&iommu_cb_set.csf_version);
  4807. if (rc) {
  4808. CAM_ERR(CAM_SMMU, "Failed to fetch CSF version: %d", rc);
  4809. return rc;
  4810. }
  4811. CAM_DBG(CAM_SMMU, "Main component bound successfully");
  4812. return 0;
  4813. }
  4814. static void cam_smmu_component_unbind(struct device *dev,
  4815. struct device *master_dev, void *data)
  4816. {
  4817. struct platform_device *pdev = to_platform_device(dev);
  4818. /* release all the context banks and memory allocated */
  4819. cam_smmu_reset_iommu_table(CAM_SMMU_TABLE_DEINIT);
  4820. if (dev && dev->dma_parms) {
  4821. devm_kfree(dev, dev->dma_parms);
  4822. dev->dma_parms = NULL;
  4823. }
  4824. cam_smmu_release_cb(pdev);
  4825. iommu_cb_set.debug_cfg.dentry = NULL;
  4826. }
  4827. const static struct component_ops cam_smmu_component_ops = {
  4828. .bind = cam_smmu_component_bind,
  4829. .unbind = cam_smmu_component_unbind,
  4830. };
  4831. static int cam_smmu_probe(struct platform_device *pdev)
  4832. {
  4833. int rc = 0;
  4834. struct device *dev = &pdev->dev;
  4835. dev->dma_parms = NULL;
  4836. CAM_DBG(CAM_SMMU, "Adding SMMU component: %s", pdev->name);
  4837. if (of_device_is_compatible(dev->of_node, "qcom,msm-cam-smmu")) {
  4838. rc = cam_alloc_smmu_context_banks(dev);
  4839. if (rc < 0) {
  4840. CAM_ERR(CAM_SMMU, "Failed in allocating context banks");
  4841. return -ENOMEM;
  4842. }
  4843. rc = component_add(&pdev->dev, &cam_smmu_component_ops);
  4844. } else if (of_device_is_compatible(dev->of_node,
  4845. "qcom,msm-cam-smmu-cb")) {
  4846. rc = component_add(&pdev->dev, &cam_smmu_cb_component_ops);
  4847. } else if (of_device_is_compatible(dev->of_node, "qcom,qsmmu-cam-cb")) {
  4848. rc = component_add(&pdev->dev,
  4849. &cam_smmu_cb_qsmmu_component_ops);
  4850. } else if (of_device_is_compatible(dev->of_node,
  4851. "qcom,msm-cam-smmu-fw-dev")) {
  4852. rc = component_add(&pdev->dev, &cam_smmu_fw_dev_component_ops);
  4853. } else {
  4854. CAM_ERR(CAM_SMMU, "Unrecognized child device: %s", pdev->name);
  4855. rc = -ENODEV;
  4856. }
  4857. if (rc < 0)
  4858. CAM_ERR(CAM_SMMU, "failed to add component rc: %d", rc);
  4859. return rc;
  4860. }
  4861. static int cam_smmu_remove(struct platform_device *pdev)
  4862. {
  4863. struct device *dev = &pdev->dev;
  4864. CAM_DBG(CAM_SMMU, "Removing SMMU component: %s", pdev->name);
  4865. if (of_device_is_compatible(dev->of_node, "qcom,msm-cam-smmu")) {
  4866. component_del(&pdev->dev, &cam_smmu_component_ops);
  4867. } else if (of_device_is_compatible(dev->of_node,
  4868. "qcom,msm-cam-smmu-cb")) {
  4869. component_del(&pdev->dev, &cam_smmu_cb_component_ops);
  4870. } else if (of_device_is_compatible(dev->of_node, "qcom,qsmmu-cam-cb")) {
  4871. component_del(&pdev->dev, &cam_smmu_cb_qsmmu_component_ops);
  4872. } else if (of_device_is_compatible(dev->of_node,
  4873. "qcom,msm-cam-smmu-fw-dev")) {
  4874. component_del(&pdev->dev, &cam_smmu_fw_dev_component_ops);
  4875. } else {
  4876. CAM_ERR(CAM_SMMU, "Unrecognized child device: %s", pdev->name);
  4877. return -ENODEV;
  4878. }
  4879. return 0;
  4880. }
  4881. struct platform_driver cam_smmu_driver = {
  4882. .probe = cam_smmu_probe,
  4883. .remove = cam_smmu_remove,
  4884. .driver = {
  4885. .name = "msm_cam_smmu",
  4886. .owner = THIS_MODULE,
  4887. .of_match_table = msm_cam_smmu_dt_match,
  4888. .suppress_bind_attrs = true,
  4889. },
  4890. };
  4891. int cam_smmu_init_module(void)
  4892. {
  4893. return platform_driver_register(&cam_smmu_driver);
  4894. }
  4895. void cam_smmu_exit_module(void)
  4896. {
  4897. platform_driver_unregister(&cam_smmu_driver);
  4898. }
  4899. MODULE_DESCRIPTION("MSM Camera SMMU driver");
  4900. MODULE_LICENSE("GPL v2");