arm-smmu.c 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741
  1. /*
  2. * IOMMU API for ARM architected SMMU implementations.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Copyright (C) 2013 ARM Limited
  18. *
  19. * Author: Will Deacon <[email protected]>
  20. *
  21. * This driver currently supports:
  22. * - SMMUv1 and v2 implementations
  23. * - Stream-matching and stream-indexing
  24. * - v7/v8 long-descriptor format
  25. * - Non-secure access to the SMMU
  26. * - Context fault reporting
  27. */
  28. #define pr_fmt(fmt) "arm-smmu: " fmt
  29. #include <linux/atomic.h>
  30. #include <linux/delay.h>
  31. #include <linux/dma-iommu.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/err.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/io-64-nonatomic-hi-lo.h>
  37. #include <linux/iommu.h>
  38. #include <linux/iopoll.h>
  39. #include <linux/module.h>
  40. #include <linux/of.h>
  41. #include <linux/of_address.h>
  42. #include <linux/of_device.h>
  43. #include <linux/of_iommu.h>
  44. #include <linux/pci.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/slab.h>
  47. #include <linux/spinlock.h>
  48. #include <soc/qcom/scm.h>
  49. #include <soc/qcom/secure_buffer.h>
  50. #include <linux/of_platform.h>
  51. #include <linux/msm-bus.h>
  52. #include <dt-bindings/msm/msm-bus-ids.h>
  53. #include <linux/remote_spinlock.h>
  54. #include <linux/ktime.h>
  55. #include <trace/events/iommu.h>
  56. #include <linux/notifier.h>
  57. #include <dt-bindings/arm/arm-smmu.h>
  58. #include <linux/amba/bus.h>
  59. #include <soc/qcom/msm_tz_smmu.h>
  60. #include "io-pgtable.h"
  61. /* Maximum number of context banks per SMMU */
  62. #define ARM_SMMU_MAX_CBS 128
  63. /* SMMU global address space */
  64. #define ARM_SMMU_GR0(smmu) ((smmu)->base)
  65. #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
  66. /*
  67. * SMMU global address space with conditional offset to access secure
  68. * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
  69. * nsGFSYNR0: 0x450)
  70. */
  71. #define ARM_SMMU_GR0_NS(smmu) \
  72. ((smmu)->base + \
  73. ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
  74. ? 0x400 : 0))
  75. /*
  76. * Some 64-bit registers only make sense to write atomically, but in such
  77. * cases all the data relevant to AArch32 formats lies within the lower word,
  78. * therefore this actually makes more sense than it might first appear.
  79. */
  80. #ifdef CONFIG_64BIT
  81. #define smmu_write_atomic_lq writeq_relaxed
  82. #else
  83. #define smmu_write_atomic_lq writel_relaxed
  84. #endif
  85. /* Configuration registers */
  86. #define ARM_SMMU_GR0_sCR0 0x0
  87. #define sCR0_CLIENTPD (1 << 0)
  88. #define sCR0_GFRE (1 << 1)
  89. #define sCR0_GFIE (1 << 2)
  90. #define sCR0_GCFGFRE (1 << 4)
  91. #define sCR0_GCFGFIE (1 << 5)
  92. #define sCR0_USFCFG (1 << 10)
  93. #define sCR0_VMIDPNE (1 << 11)
  94. #define sCR0_PTM (1 << 12)
  95. #define sCR0_FB (1 << 13)
  96. #define sCR0_VMID16EN (1 << 31)
  97. #define sCR0_BSU_SHIFT 14
  98. #define sCR0_BSU_MASK 0x3
  99. #define sCR0_SHCFG_SHIFT 22
  100. #define sCR0_SHCFG_MASK 0x3
  101. #define sCR0_SHCFG_NSH 3
  102. /* Auxiliary Configuration register */
  103. #define ARM_SMMU_GR0_sACR 0x10
  104. /* Identification registers */
  105. #define ARM_SMMU_GR0_ID0 0x20
  106. #define ARM_SMMU_GR0_ID1 0x24
  107. #define ARM_SMMU_GR0_ID2 0x28
  108. #define ARM_SMMU_GR0_ID3 0x2c
  109. #define ARM_SMMU_GR0_ID4 0x30
  110. #define ARM_SMMU_GR0_ID5 0x34
  111. #define ARM_SMMU_GR0_ID6 0x38
  112. #define ARM_SMMU_GR0_ID7 0x3c
  113. #define ARM_SMMU_GR0_sGFSR 0x48
  114. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  115. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  116. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  117. #define ID0_S1TS (1 << 30)
  118. #define ID0_S2TS (1 << 29)
  119. #define ID0_NTS (1 << 28)
  120. #define ID0_SMS (1 << 27)
  121. #define ID0_ATOSNS (1 << 26)
  122. #define ID0_PTFS_NO_AARCH32 (1 << 25)
  123. #define ID0_PTFS_NO_AARCH32S (1 << 24)
  124. #define ID0_CTTW (1 << 14)
  125. #define ID0_NUMIRPT_SHIFT 16
  126. #define ID0_NUMIRPT_MASK 0xff
  127. #define ID0_NUMSIDB_SHIFT 9
  128. #define ID0_NUMSIDB_MASK 0xf
  129. #define ID0_NUMSMRG_SHIFT 0
  130. #define ID0_NUMSMRG_MASK 0xff
  131. #define ID1_PAGESIZE (1 << 31)
  132. #define ID1_NUMPAGENDXB_SHIFT 28
  133. #define ID1_NUMPAGENDXB_MASK 7
  134. #define ID1_NUMS2CB_SHIFT 16
  135. #define ID1_NUMS2CB_MASK 0xff
  136. #define ID1_NUMCB_SHIFT 0
  137. #define ID1_NUMCB_MASK 0xff
  138. #define ID2_OAS_SHIFT 4
  139. #define ID2_OAS_MASK 0xf
  140. #define ID2_IAS_SHIFT 0
  141. #define ID2_IAS_MASK 0xf
  142. #define ID2_UBS_SHIFT 8
  143. #define ID2_UBS_MASK 0xf
  144. #define ID2_PTFS_4K (1 << 12)
  145. #define ID2_PTFS_16K (1 << 13)
  146. #define ID2_PTFS_64K (1 << 14)
  147. #define ID2_VMID16 (1 << 15)
  148. #define ID7_MAJOR_SHIFT 4
  149. #define ID7_MAJOR_MASK 0xf
  150. /* Global TLB invalidation */
  151. #define ARM_SMMU_GR0_TLBIVMID 0x64
  152. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  153. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  154. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  155. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  156. #define sTLBGSTATUS_GSACTIVE (1 << 0)
  157. #define TLB_LOOP_TIMEOUT 500000 /* 500ms */
  158. /* Stream mapping registers */
  159. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  160. #define SMR_VALID (1 << 31)
  161. #define SMR_MASK_SHIFT 16
  162. #define SMR_MASK_MASK 0x7FFF
  163. #define SID_MASK 0x7FFF
  164. #define SMR_ID_SHIFT 0
  165. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  166. #define S2CR_CBNDX_SHIFT 0
  167. #define S2CR_CBNDX_MASK 0xff
  168. #define S2CR_TYPE_SHIFT 16
  169. #define S2CR_TYPE_MASK 0x3
  170. #define S2CR_SHCFG_SHIFT 8
  171. #define S2CR_SHCFG_MASK 0x3
  172. #define S2CR_SHCFG_NSH 0x3
  173. enum arm_smmu_s2cr_type {
  174. S2CR_TYPE_TRANS,
  175. S2CR_TYPE_BYPASS,
  176. S2CR_TYPE_FAULT,
  177. };
  178. #define S2CR_PRIVCFG_SHIFT 24
  179. #define S2CR_PRIVCFG_MASK 0x3
  180. enum arm_smmu_s2cr_privcfg {
  181. S2CR_PRIVCFG_DEFAULT,
  182. S2CR_PRIVCFG_DIPAN,
  183. S2CR_PRIVCFG_UNPRIV,
  184. S2CR_PRIVCFG_PRIV,
  185. };
  186. /* Context bank attribute registers */
  187. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  188. #define CBAR_VMID_SHIFT 0
  189. #define CBAR_VMID_MASK 0xff
  190. #define CBAR_S1_BPSHCFG_SHIFT 8
  191. #define CBAR_S1_BPSHCFG_MASK 3
  192. #define CBAR_S1_BPSHCFG_NSH 3
  193. #define CBAR_S1_MEMATTR_SHIFT 12
  194. #define CBAR_S1_MEMATTR_MASK 0xf
  195. #define CBAR_S1_MEMATTR_WB 0xf
  196. #define CBAR_TYPE_SHIFT 16
  197. #define CBAR_TYPE_MASK 0x3
  198. #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
  199. #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
  200. #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
  201. #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
  202. #define CBAR_IRPTNDX_SHIFT 24
  203. #define CBAR_IRPTNDX_MASK 0xff
  204. #define ARM_SMMU_GR1_CBFRSYNRA(n) (0x400 + ((n) << 2))
  205. #define CBFRSYNRA_SID_MASK (0xffff)
  206. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  207. #define CBA2R_RW64_32BIT (0 << 0)
  208. #define CBA2R_RW64_64BIT (1 << 0)
  209. #define CBA2R_VMID_SHIFT 16
  210. #define CBA2R_VMID_MASK 0xffff
  211. /* Translation context bank */
  212. #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
  213. #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
  214. #define ARM_SMMU_CB_SCTLR 0x0
  215. #define ARM_SMMU_CB_ACTLR 0x4
  216. #define ARM_SMMU_CB_RESUME 0x8
  217. #define ARM_SMMU_CB_TTBCR2 0x10
  218. #define ARM_SMMU_CB_TTBR0 0x20
  219. #define ARM_SMMU_CB_TTBR1 0x28
  220. #define ARM_SMMU_CB_TTBCR 0x30
  221. #define ARM_SMMU_CB_CONTEXTIDR 0x34
  222. #define ARM_SMMU_CB_S1_MAIR0 0x38
  223. #define ARM_SMMU_CB_S1_MAIR1 0x3c
  224. #define ARM_SMMU_CB_PAR 0x50
  225. #define ARM_SMMU_CB_FSR 0x58
  226. #define ARM_SMMU_CB_FSRRESTORE 0x5c
  227. #define ARM_SMMU_CB_FAR 0x60
  228. #define ARM_SMMU_CB_FSYNR0 0x68
  229. #define ARM_SMMU_CB_S1_TLBIVA 0x600
  230. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  231. #define ARM_SMMU_CB_S1_TLBIALL 0x618
  232. #define ARM_SMMU_CB_S1_TLBIVAL 0x620
  233. #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
  234. #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
  235. #define ARM_SMMU_CB_TLBSYNC 0x7f0
  236. #define ARM_SMMU_CB_TLBSTATUS 0x7f4
  237. #define TLBSTATUS_SACTIVE (1 << 0)
  238. #define ARM_SMMU_CB_ATS1PR 0x800
  239. #define ARM_SMMU_CB_ATSR 0x8f0
  240. #define SCTLR_SHCFG_SHIFT 22
  241. #define SCTLR_SHCFG_MASK 0x3
  242. #define SCTLR_SHCFG_NSH 0x3
  243. #define SCTLR_S1_ASIDPNE (1 << 12)
  244. #define SCTLR_CFCFG (1 << 7)
  245. #define SCTLR_HUPCF (1 << 8)
  246. #define SCTLR_CFIE (1 << 6)
  247. #define SCTLR_CFRE (1 << 5)
  248. #define SCTLR_E (1 << 4)
  249. #define SCTLR_AFE (1 << 2)
  250. #define SCTLR_TRE (1 << 1)
  251. #define SCTLR_M (1 << 0)
  252. #define ARM_MMU500_ACTLR_CPRE (1 << 1)
  253. #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
  254. #define ARM_SMMU_IMPL_DEF0(smmu) \
  255. ((smmu)->base + (2 * (1 << (smmu)->pgshift)))
  256. #define ARM_SMMU_IMPL_DEF1(smmu) \
  257. ((smmu)->base + (6 * (1 << (smmu)->pgshift)))
  258. #define CB_PAR_F (1 << 0)
  259. #define ATSR_ACTIVE (1 << 0)
  260. #define RESUME_RETRY (0 << 0)
  261. #define RESUME_TERMINATE (1 << 0)
  262. #define TTBCR2_SEP_SHIFT 15
  263. #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
  264. #define TTBCR2_AS (1 << 4)
  265. #define TTBRn_ASID_SHIFT 48
  266. #define FSR_MULTI (1 << 31)
  267. #define FSR_SS (1 << 30)
  268. #define FSR_UUT (1 << 8)
  269. #define FSR_ASF (1 << 7)
  270. #define FSR_TLBLKF (1 << 6)
  271. #define FSR_TLBMCF (1 << 5)
  272. #define FSR_EF (1 << 4)
  273. #define FSR_PF (1 << 3)
  274. #define FSR_AFF (1 << 2)
  275. #define FSR_TF (1 << 1)
  276. #define FSR_IGN (FSR_AFF | FSR_ASF | \
  277. FSR_TLBMCF | FSR_TLBLKF)
  278. #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
  279. FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
  280. #define FSYNR0_WNR (1 << 4)
  281. static int force_stage;
  282. module_param(force_stage, int, S_IRUGO);
  283. MODULE_PARM_DESC(force_stage,
  284. "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
  285. static bool disable_bypass;
  286. module_param(disable_bypass, bool, S_IRUGO);
  287. MODULE_PARM_DESC(disable_bypass,
  288. "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
  289. enum arm_smmu_arch_version {
  290. ARM_SMMU_V1,
  291. ARM_SMMU_V1_64K,
  292. ARM_SMMU_V2,
  293. };
  294. enum arm_smmu_implementation {
  295. GENERIC_SMMU,
  296. ARM_MMU500,
  297. CAVIUM_SMMUV2,
  298. QCOM_SMMUV2,
  299. QCOM_SMMUV500,
  300. };
  301. struct arm_smmu_impl_def_reg {
  302. u32 offset;
  303. u32 value;
  304. };
  305. /*
  306. * attach_count
  307. * The SMR and S2CR registers are only programmed when the number of
  308. * devices attached to the iommu using these registers is > 0. This
  309. * is required for the "SID switch" use case for secure display.
  310. * Protected by stream_map_mutex.
  311. */
  312. struct arm_smmu_s2cr {
  313. struct iommu_group *group;
  314. int count;
  315. int attach_count;
  316. enum arm_smmu_s2cr_type type;
  317. enum arm_smmu_s2cr_privcfg privcfg;
  318. u8 cbndx;
  319. bool cb_handoff;
  320. };
  321. #define s2cr_init_val (struct arm_smmu_s2cr){ \
  322. .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
  323. .cb_handoff = false, \
  324. }
  325. struct arm_smmu_smr {
  326. u16 mask;
  327. u16 id;
  328. bool valid;
  329. };
  330. struct arm_smmu_cb {
  331. u64 ttbr[2];
  332. u32 tcr[2];
  333. u32 mair[2];
  334. struct arm_smmu_cfg *cfg;
  335. u32 actlr;
  336. u32 attributes;
  337. };
  338. struct arm_smmu_master_cfg {
  339. struct arm_smmu_device *smmu;
  340. s16 smendx[];
  341. };
  342. #define INVALID_SMENDX -1
  343. #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
  344. #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
  345. #define fwspec_smendx(fw, i) \
  346. (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
  347. #define for_each_cfg_sme(fw, i, idx) \
  348. for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
  349. /*
  350. * Describes resources required for on/off power operation.
  351. * Separate reference count is provided for atomic/nonatomic
  352. * operations.
  353. */
  354. struct arm_smmu_power_resources {
  355. struct platform_device *pdev;
  356. struct device *dev;
  357. struct clk **clocks;
  358. int num_clocks;
  359. struct regulator_bulk_data *gdscs;
  360. int num_gdscs;
  361. uint32_t bus_client;
  362. struct msm_bus_scale_pdata *bus_dt_data;
  363. /* Protects power_count */
  364. struct mutex power_lock;
  365. int power_count;
  366. /* Protects clock_refs_count */
  367. spinlock_t clock_refs_lock;
  368. int clock_refs_count;
  369. int regulator_defer;
  370. };
  371. struct arm_smmu_arch_ops;
  372. struct arm_smmu_device {
  373. struct device *dev;
  374. void __iomem *base;
  375. unsigned long size;
  376. phys_addr_t phys_addr;
  377. unsigned long pgshift;
  378. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  379. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  380. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  381. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  382. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  383. #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
  384. #define ARM_SMMU_FEAT_VMID16 (1 << 6)
  385. #define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
  386. #define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
  387. #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
  388. #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
  389. #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
  390. u32 features;
  391. u32 options;
  392. enum arm_smmu_arch_version version;
  393. enum arm_smmu_implementation model;
  394. u32 num_context_banks;
  395. u32 num_s2_context_banks;
  396. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  397. DECLARE_BITMAP(secure_context_map, ARM_SMMU_MAX_CBS);
  398. struct arm_smmu_cb *cbs;
  399. atomic_t irptndx;
  400. u32 num_mapping_groups;
  401. u16 streamid_mask;
  402. u16 smr_mask_mask;
  403. struct arm_smmu_smr *smrs;
  404. struct arm_smmu_s2cr *s2crs;
  405. struct mutex stream_map_mutex;
  406. unsigned long va_size;
  407. unsigned long ipa_size;
  408. unsigned long pa_size;
  409. unsigned long pgsize_bitmap;
  410. u32 num_global_irqs;
  411. u32 num_context_irqs;
  412. unsigned int *irqs;
  413. struct list_head list;
  414. u32 cavium_id_base; /* Specific to Cavium */
  415. /* Specific to QCOM */
  416. struct arm_smmu_impl_def_reg *impl_def_attach_registers;
  417. unsigned int num_impl_def_attach_registers;
  418. struct arm_smmu_power_resources *pwr;
  419. struct notifier_block regulator_nb;
  420. spinlock_t atos_lock;
  421. /* protects idr */
  422. struct mutex idr_mutex;
  423. struct idr asid_idr;
  424. struct arm_smmu_arch_ops *arch_ops;
  425. void *archdata;
  426. enum tz_smmu_device_id sec_id;
  427. };
  428. enum arm_smmu_context_fmt {
  429. ARM_SMMU_CTX_FMT_NONE,
  430. ARM_SMMU_CTX_FMT_AARCH64,
  431. ARM_SMMU_CTX_FMT_AARCH32_L,
  432. ARM_SMMU_CTX_FMT_AARCH32_S,
  433. };
  434. struct arm_smmu_cfg {
  435. u8 cbndx;
  436. u8 irptndx;
  437. u32 cbar;
  438. u32 procid;
  439. u16 asid;
  440. enum arm_smmu_context_fmt fmt;
  441. };
  442. #define INVALID_IRPTNDX 0xff
  443. #define INVALID_CBNDX 0xff
  444. #define INVALID_ASID 0xffff
  445. /*
  446. * In V7L and V8L with TTBCR2.AS == 0, ASID is 8 bits.
  447. * V8L 16 with TTBCR2.AS == 1 (16 bit ASID) isn't supported yet.
  448. */
  449. #define MAX_ASID 0xff
  450. #define ARM_SMMU_CB_ASID(smmu, cfg) ((cfg)->asid)
  451. #define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1)
  452. enum arm_smmu_domain_stage {
  453. ARM_SMMU_DOMAIN_S1 = 0,
  454. ARM_SMMU_DOMAIN_S2,
  455. ARM_SMMU_DOMAIN_NESTED,
  456. };
  457. struct arm_smmu_pte_info {
  458. void *virt_addr;
  459. size_t size;
  460. struct list_head entry;
  461. };
  462. struct arm_smmu_domain {
  463. struct arm_smmu_device *smmu;
  464. struct device *dev;
  465. struct io_pgtable_ops *pgtbl_ops;
  466. struct io_pgtable_cfg pgtbl_cfg;
  467. spinlock_t pgtbl_lock;
  468. struct arm_smmu_cfg cfg;
  469. enum arm_smmu_domain_stage stage;
  470. struct mutex init_mutex; /* Protects smmu pointer */
  471. u32 attributes;
  472. bool slave_side_secure;
  473. u32 secure_vmid;
  474. struct list_head pte_info_list;
  475. struct list_head unassign_list;
  476. struct mutex assign_lock;
  477. struct list_head secure_pool_list;
  478. /* nonsecure pool protected by pgtbl_lock */
  479. struct list_head nonsecure_pool;
  480. struct iommu_domain domain;
  481. bool qsmmuv500_errata1_init;
  482. bool qsmmuv500_errata1_client;
  483. bool qsmmuv500_errata2_min_align;
  484. bool is_force_guard_page;
  485. };
  486. static DEFINE_SPINLOCK(arm_smmu_devices_lock);
  487. static LIST_HEAD(arm_smmu_devices);
  488. struct arm_smmu_option_prop {
  489. u32 opt;
  490. const char *prop;
  491. };
  492. static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
  493. static bool using_legacy_binding, using_generic_binding;
  494. static struct arm_smmu_option_prop arm_smmu_options[] = {
  495. { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
  496. { ARM_SMMU_OPT_FATAL_ASF, "qcom,fatal-asf" },
  497. { ARM_SMMU_OPT_SKIP_INIT, "qcom,skip-init" },
  498. { ARM_SMMU_OPT_DYNAMIC, "qcom,dynamic" },
  499. { ARM_SMMU_OPT_3LVL_TABLES, "qcom,use-3-lvl-tables" },
  500. { ARM_SMMU_OPT_NO_ASID_RETENTION, "qcom,no-asid-retention" },
  501. { ARM_SMMU_OPT_DISABLE_ATOS, "qcom,disable-atos" },
  502. { ARM_SMMU_OPT_MMU500_ERRATA1, "qcom,mmu500-errata-1" },
  503. { ARM_SMMU_OPT_STATIC_CB, "qcom,enable-static-cb"},
  504. { ARM_SMMU_OPT_HALT, "qcom,enable-smmu-halt"},
  505. { ARM_SMMU_OPT_HIBERNATION, "qcom,hibernation-support"},
  506. { 0, NULL},
  507. };
  508. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  509. dma_addr_t iova);
  510. static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  511. dma_addr_t iova);
  512. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain);
  513. static int arm_smmu_prepare_pgtable(void *addr, void *cookie);
  514. static void arm_smmu_unprepare_pgtable(void *cookie, void *addr, size_t size);
  515. static int arm_smmu_assign_table(struct arm_smmu_domain *smmu_domain);
  516. static void arm_smmu_unassign_table(struct arm_smmu_domain *smmu_domain);
  517. static uint64_t arm_smmu_iova_to_pte(struct iommu_domain *domain,
  518. dma_addr_t iova);
  519. static int arm_smmu_enable_s1_translations(struct arm_smmu_domain *smmu_domain);
  520. static int arm_smmu_alloc_cb(struct iommu_domain *domain,
  521. struct arm_smmu_device *smmu,
  522. struct device *dev);
  523. static struct iommu_gather_ops qsmmuv500_errata1_smmu_gather_ops;
  524. static bool arm_smmu_is_static_cb(struct arm_smmu_device *smmu);
  525. static bool arm_smmu_is_master_side_secure(struct arm_smmu_domain *smmu_domain);
  526. static bool arm_smmu_is_slave_side_secure(struct arm_smmu_domain *smmu_domain);
  527. static int msm_secure_smmu_map(struct iommu_domain *domain, unsigned long iova,
  528. phys_addr_t paddr, size_t size, int prot);
  529. static size_t msm_secure_smmu_unmap(struct iommu_domain *domain,
  530. unsigned long iova,
  531. size_t size);
  532. static size_t msm_secure_smmu_map_sg(struct iommu_domain *domain,
  533. unsigned long iova,
  534. struct scatterlist *sg,
  535. unsigned int nents, int prot);
  536. static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
  537. {
  538. return container_of(dom, struct arm_smmu_domain, domain);
  539. }
  540. static void parse_driver_options(struct arm_smmu_device *smmu)
  541. {
  542. int i = 0;
  543. do {
  544. if (of_property_read_bool(smmu->dev->of_node,
  545. arm_smmu_options[i].prop)) {
  546. smmu->options |= arm_smmu_options[i].opt;
  547. dev_dbg(smmu->dev, "option %s\n",
  548. arm_smmu_options[i].prop);
  549. }
  550. } while (arm_smmu_options[++i].opt);
  551. }
  552. static bool is_dynamic_domain(struct iommu_domain *domain)
  553. {
  554. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  555. return !!(smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC));
  556. }
  557. static int arm_smmu_restore_sec_cfg(struct arm_smmu_device *smmu, u32 cb)
  558. {
  559. int ret;
  560. int scm_ret = 0;
  561. if (!arm_smmu_is_static_cb(smmu))
  562. return 0;
  563. ret = scm_restore_sec_cfg(smmu->sec_id, cb, &scm_ret);
  564. if (ret || scm_ret) {
  565. pr_err("scm call IOMMU_SECURE_CFG failed\n");
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. static bool is_iommu_pt_coherent(struct arm_smmu_domain *smmu_domain)
  571. {
  572. if (smmu_domain->attributes &
  573. (1 << DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT))
  574. return true;
  575. else if (smmu_domain->smmu && smmu_domain->smmu->dev)
  576. return smmu_domain->smmu->dev->archdata.dma_coherent;
  577. else
  578. return false;
  579. }
  580. static bool arm_smmu_is_static_cb(struct arm_smmu_device *smmu)
  581. {
  582. return smmu->options & ARM_SMMU_OPT_STATIC_CB;
  583. }
  584. static bool arm_smmu_has_secure_vmid(struct arm_smmu_domain *smmu_domain)
  585. {
  586. return (smmu_domain->secure_vmid != VMID_INVAL);
  587. }
  588. static bool arm_smmu_is_slave_side_secure(struct arm_smmu_domain *smmu_domain)
  589. {
  590. return arm_smmu_has_secure_vmid(smmu_domain) &&
  591. smmu_domain->slave_side_secure;
  592. }
  593. static bool arm_smmu_is_master_side_secure(struct arm_smmu_domain *smmu_domain)
  594. {
  595. return arm_smmu_has_secure_vmid(smmu_domain)
  596. && !smmu_domain->slave_side_secure;
  597. }
  598. static void arm_smmu_secure_domain_lock(struct arm_smmu_domain *smmu_domain)
  599. {
  600. if (arm_smmu_is_master_side_secure(smmu_domain))
  601. mutex_lock(&smmu_domain->assign_lock);
  602. }
  603. static void arm_smmu_secure_domain_unlock(struct arm_smmu_domain *smmu_domain)
  604. {
  605. if (arm_smmu_is_master_side_secure(smmu_domain))
  606. mutex_unlock(&smmu_domain->assign_lock);
  607. }
  608. static bool arm_smmu_opt_hibernation(struct arm_smmu_device *smmu)
  609. {
  610. return smmu->options & ARM_SMMU_OPT_HIBERNATION;
  611. }
  612. /*
  613. * init()
  614. * Hook for additional device tree parsing at probe time.
  615. *
  616. * device_reset()
  617. * Hook for one-time architecture-specific register settings.
  618. *
  619. * iova_to_phys_hard()
  620. * Provides debug information. May be called from the context fault irq handler.
  621. *
  622. * init_context_bank()
  623. * Hook for architecture-specific settings which require knowledge of the
  624. * dynamically allocated context bank number.
  625. *
  626. * device_group()
  627. * Hook for checking whether a device is compatible with a said group.
  628. */
  629. struct arm_smmu_arch_ops {
  630. int (*init)(struct arm_smmu_device *smmu);
  631. void (*device_reset)(struct arm_smmu_device *smmu);
  632. phys_addr_t (*iova_to_phys_hard)(struct iommu_domain *domain,
  633. dma_addr_t iova);
  634. void (*init_context_bank)(struct arm_smmu_domain *smmu_domain,
  635. struct device *dev);
  636. int (*device_group)(struct device *dev, struct iommu_group *group);
  637. };
  638. static int arm_smmu_arch_init(struct arm_smmu_device *smmu)
  639. {
  640. if (!smmu->arch_ops)
  641. return 0;
  642. if (!smmu->arch_ops->init)
  643. return 0;
  644. return smmu->arch_ops->init(smmu);
  645. }
  646. static void arm_smmu_arch_device_reset(struct arm_smmu_device *smmu)
  647. {
  648. if (!smmu->arch_ops)
  649. return;
  650. if (!smmu->arch_ops->device_reset)
  651. return;
  652. return smmu->arch_ops->device_reset(smmu);
  653. }
  654. static void arm_smmu_arch_init_context_bank(
  655. struct arm_smmu_domain *smmu_domain, struct device *dev)
  656. {
  657. struct arm_smmu_device *smmu = smmu_domain->smmu;
  658. if (!smmu->arch_ops)
  659. return;
  660. if (!smmu->arch_ops->init_context_bank)
  661. return;
  662. return smmu->arch_ops->init_context_bank(smmu_domain, dev);
  663. }
  664. static int arm_smmu_arch_device_group(struct device *dev,
  665. struct iommu_group *group)
  666. {
  667. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  668. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  669. if (!smmu->arch_ops)
  670. return 0;
  671. if (!smmu->arch_ops->device_group)
  672. return 0;
  673. return smmu->arch_ops->device_group(dev, group);
  674. }
  675. static struct device_node *dev_get_dev_node(struct device *dev)
  676. {
  677. if (dev_is_pci(dev)) {
  678. struct pci_bus *bus = to_pci_dev(dev)->bus;
  679. while (!pci_is_root_bus(bus))
  680. bus = bus->parent;
  681. return of_node_get(bus->bridge->parent->of_node);
  682. }
  683. return of_node_get(dev->of_node);
  684. }
  685. static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
  686. {
  687. *((__be32 *)data) = cpu_to_be32(alias);
  688. return 0; /* Continue walking */
  689. }
  690. static int __find_legacy_master_phandle(struct device *dev, void *data)
  691. {
  692. struct of_phandle_iterator *it = *(void **)data;
  693. struct device_node *np = it->node;
  694. int err;
  695. of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
  696. "#stream-id-cells", 0)
  697. if (it->node == np) {
  698. *(void **)data = dev;
  699. return 1;
  700. }
  701. it->node = np;
  702. return err == -ENOENT ? 0 : err;
  703. }
  704. static struct platform_driver arm_smmu_driver;
  705. static struct iommu_ops arm_smmu_ops;
  706. static int arm_smmu_register_legacy_master(struct device *dev,
  707. struct arm_smmu_device **smmu)
  708. {
  709. struct device *smmu_dev;
  710. struct device_node *np;
  711. struct of_phandle_iterator it;
  712. void *data = &it;
  713. u32 *sids;
  714. __be32 pci_sid;
  715. int err = 0;
  716. memset(&it, 0, sizeof(it));
  717. np = dev_get_dev_node(dev);
  718. if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
  719. of_node_put(np);
  720. return -ENODEV;
  721. }
  722. it.node = np;
  723. err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
  724. __find_legacy_master_phandle);
  725. smmu_dev = data;
  726. of_node_put(np);
  727. if (err == 0)
  728. return -ENODEV;
  729. if (err < 0)
  730. return err;
  731. if (dev_is_pci(dev)) {
  732. /* "mmu-masters" assumes Stream ID == Requester ID */
  733. pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
  734. &pci_sid);
  735. it.cur = &pci_sid;
  736. it.cur_count = 1;
  737. }
  738. err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
  739. &arm_smmu_ops);
  740. if (err)
  741. return err;
  742. sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
  743. if (!sids)
  744. return -ENOMEM;
  745. *smmu = dev_get_drvdata(smmu_dev);
  746. of_phandle_iterator_args(&it, sids, it.cur_count);
  747. err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
  748. kfree(sids);
  749. return err;
  750. }
  751. static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  752. {
  753. int idx;
  754. do {
  755. idx = find_next_zero_bit(map, end, start);
  756. if (idx == end)
  757. return -ENOSPC;
  758. } while (test_and_set_bit(idx, map));
  759. return idx;
  760. }
  761. static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
  762. {
  763. clear_bit(idx, map);
  764. }
  765. static int arm_smmu_prepare_clocks(struct arm_smmu_power_resources *pwr)
  766. {
  767. int i, ret = 0;
  768. for (i = 0; i < pwr->num_clocks; ++i) {
  769. ret = clk_prepare(pwr->clocks[i]);
  770. if (ret) {
  771. dev_err(pwr->dev, "Couldn't prepare clock #%d\n", i);
  772. while (i--)
  773. clk_unprepare(pwr->clocks[i]);
  774. break;
  775. }
  776. }
  777. return ret;
  778. }
  779. static void arm_smmu_unprepare_clocks(struct arm_smmu_power_resources *pwr)
  780. {
  781. int i;
  782. for (i = pwr->num_clocks; i; --i)
  783. clk_unprepare(pwr->clocks[i - 1]);
  784. }
  785. static int arm_smmu_enable_clocks(struct arm_smmu_power_resources *pwr)
  786. {
  787. int i, ret = 0;
  788. for (i = 0; i < pwr->num_clocks; ++i) {
  789. ret = clk_enable(pwr->clocks[i]);
  790. if (ret) {
  791. dev_err(pwr->dev, "Couldn't enable clock #%d\n", i);
  792. while (i--)
  793. clk_disable(pwr->clocks[i]);
  794. break;
  795. }
  796. }
  797. return ret;
  798. }
  799. static void arm_smmu_disable_clocks(struct arm_smmu_power_resources *pwr)
  800. {
  801. int i;
  802. for (i = pwr->num_clocks; i; --i)
  803. clk_disable(pwr->clocks[i - 1]);
  804. }
  805. static int arm_smmu_request_bus(struct arm_smmu_power_resources *pwr)
  806. {
  807. if (!pwr->bus_client)
  808. return 0;
  809. return msm_bus_scale_client_update_request(pwr->bus_client, 1);
  810. }
  811. static void arm_smmu_unrequest_bus(struct arm_smmu_power_resources *pwr)
  812. {
  813. if (!pwr->bus_client)
  814. return;
  815. WARN_ON(msm_bus_scale_client_update_request(pwr->bus_client, 0));
  816. }
  817. static int arm_smmu_enable_regulators(struct arm_smmu_power_resources *pwr)
  818. {
  819. struct regulator_bulk_data *consumers;
  820. int num_consumers, ret;
  821. int i;
  822. num_consumers = pwr->num_gdscs;
  823. consumers = pwr->gdscs;
  824. for (i = 0; i < num_consumers; i++) {
  825. ret = regulator_enable(consumers[i].consumer);
  826. if (ret)
  827. goto out;
  828. }
  829. return 0;
  830. out:
  831. i -= 1;
  832. for (; i >= 0; i--)
  833. regulator_disable(consumers[i].consumer);
  834. return ret;
  835. }
  836. static int arm_smmu_disable_regulators(struct arm_smmu_power_resources *pwr)
  837. {
  838. struct regulator_bulk_data *consumers;
  839. int i;
  840. int num_consumers, ret, r;
  841. num_consumers = pwr->num_gdscs;
  842. consumers = pwr->gdscs;
  843. for (i = num_consumers - 1; i >= 0; --i) {
  844. ret = regulator_disable_deferred(consumers[i].consumer,
  845. pwr->regulator_defer);
  846. if (ret != 0)
  847. goto err;
  848. }
  849. return 0;
  850. err:
  851. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  852. for (++i; i < num_consumers; ++i) {
  853. r = regulator_enable(consumers[i].consumer);
  854. if (r != 0)
  855. pr_err("Failed to reename %s: %d\n",
  856. consumers[i].supply, r);
  857. }
  858. return ret;
  859. }
  860. /* Clocks must be prepared before this (arm_smmu_prepare_clocks) */
  861. static int arm_smmu_power_on_atomic(struct arm_smmu_power_resources *pwr)
  862. {
  863. int ret = 0;
  864. unsigned long flags;
  865. spin_lock_irqsave(&pwr->clock_refs_lock, flags);
  866. if (pwr->clock_refs_count > 0) {
  867. pwr->clock_refs_count++;
  868. spin_unlock_irqrestore(&pwr->clock_refs_lock, flags);
  869. return 0;
  870. }
  871. ret = arm_smmu_enable_clocks(pwr);
  872. if (!ret)
  873. pwr->clock_refs_count = 1;
  874. spin_unlock_irqrestore(&pwr->clock_refs_lock, flags);
  875. return ret;
  876. }
  877. /* Clocks should be unprepared after this (arm_smmu_unprepare_clocks) */
  878. static void arm_smmu_power_off_atomic(struct arm_smmu_power_resources *pwr)
  879. {
  880. unsigned long flags;
  881. spin_lock_irqsave(&pwr->clock_refs_lock, flags);
  882. if (pwr->clock_refs_count == 0) {
  883. WARN(1, "%s: bad clock_ref_count\n", dev_name(pwr->dev));
  884. spin_unlock_irqrestore(&pwr->clock_refs_lock, flags);
  885. return;
  886. } else if (pwr->clock_refs_count > 1) {
  887. pwr->clock_refs_count--;
  888. spin_unlock_irqrestore(&pwr->clock_refs_lock, flags);
  889. return;
  890. }
  891. arm_smmu_disable_clocks(pwr);
  892. pwr->clock_refs_count = 0;
  893. spin_unlock_irqrestore(&pwr->clock_refs_lock, flags);
  894. }
  895. static int arm_smmu_power_on_slow(struct arm_smmu_power_resources *pwr)
  896. {
  897. int ret;
  898. mutex_lock(&pwr->power_lock);
  899. if (pwr->power_count > 0) {
  900. pwr->power_count += 1;
  901. mutex_unlock(&pwr->power_lock);
  902. return 0;
  903. }
  904. ret = arm_smmu_request_bus(pwr);
  905. if (ret)
  906. goto out_unlock;
  907. ret = arm_smmu_enable_regulators(pwr);
  908. if (ret)
  909. goto out_disable_bus;
  910. ret = arm_smmu_prepare_clocks(pwr);
  911. if (ret)
  912. goto out_disable_regulators;
  913. pwr->power_count = 1;
  914. mutex_unlock(&pwr->power_lock);
  915. return 0;
  916. out_disable_regulators:
  917. regulator_bulk_disable(pwr->num_gdscs, pwr->gdscs);
  918. out_disable_bus:
  919. arm_smmu_unrequest_bus(pwr);
  920. out_unlock:
  921. mutex_unlock(&pwr->power_lock);
  922. return ret;
  923. }
  924. static void arm_smmu_power_off_slow(struct arm_smmu_power_resources *pwr)
  925. {
  926. mutex_lock(&pwr->power_lock);
  927. if (pwr->power_count == 0) {
  928. WARN(1, "%s: Bad power count\n", dev_name(pwr->dev));
  929. mutex_unlock(&pwr->power_lock);
  930. return;
  931. } else if (pwr->power_count > 1) {
  932. pwr->power_count--;
  933. mutex_unlock(&pwr->power_lock);
  934. return;
  935. }
  936. arm_smmu_unprepare_clocks(pwr);
  937. arm_smmu_disable_regulators(pwr);
  938. arm_smmu_unrequest_bus(pwr);
  939. pwr->power_count = 0;
  940. mutex_unlock(&pwr->power_lock);
  941. }
  942. static int arm_smmu_power_on(struct arm_smmu_power_resources *pwr)
  943. {
  944. int ret;
  945. ret = arm_smmu_power_on_slow(pwr);
  946. if (ret)
  947. return ret;
  948. ret = arm_smmu_power_on_atomic(pwr);
  949. if (ret)
  950. goto out_disable;
  951. return 0;
  952. out_disable:
  953. arm_smmu_power_off_slow(pwr);
  954. return ret;
  955. }
  956. static void arm_smmu_power_off(struct arm_smmu_power_resources *pwr)
  957. {
  958. arm_smmu_power_off_atomic(pwr);
  959. arm_smmu_power_off_slow(pwr);
  960. }
  961. /*
  962. * Must be used instead of arm_smmu_power_on if it may be called from
  963. * atomic context
  964. */
  965. static int arm_smmu_domain_power_on(struct iommu_domain *domain,
  966. struct arm_smmu_device *smmu)
  967. {
  968. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  969. int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC);
  970. if (atomic_domain)
  971. return arm_smmu_power_on_atomic(smmu->pwr);
  972. return arm_smmu_power_on(smmu->pwr);
  973. }
  974. /*
  975. * Must be used instead of arm_smmu_power_on if it may be called from
  976. * atomic context
  977. */
  978. static void arm_smmu_domain_power_off(struct iommu_domain *domain,
  979. struct arm_smmu_device *smmu)
  980. {
  981. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  982. int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC);
  983. if (atomic_domain) {
  984. arm_smmu_power_off_atomic(smmu->pwr);
  985. return;
  986. }
  987. arm_smmu_power_off(smmu->pwr);
  988. }
  989. /* Wait for any pending TLB invalidations to complete */
  990. static void arm_smmu_tlb_sync_cb(struct arm_smmu_device *smmu,
  991. int cbndx)
  992. {
  993. void __iomem *base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cbndx);
  994. u32 val;
  995. writel_relaxed(0, base + ARM_SMMU_CB_TLBSYNC);
  996. if (readl_poll_timeout_atomic(base + ARM_SMMU_CB_TLBSTATUS, val,
  997. !(val & TLBSTATUS_SACTIVE),
  998. 0, TLB_LOOP_TIMEOUT)) {
  999. trace_tlbsync_timeout(smmu->dev, 0);
  1000. dev_err(smmu->dev, "TLBSYNC timeout!\n");
  1001. }
  1002. }
  1003. static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  1004. {
  1005. int count = 0;
  1006. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1007. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
  1008. while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
  1009. & sTLBGSTATUS_GSACTIVE) {
  1010. cpu_relax();
  1011. if (++count == TLB_LOOP_TIMEOUT) {
  1012. dev_err_ratelimited(smmu->dev,
  1013. "TLB sync timed out -- SMMU may be deadlocked\n");
  1014. return;
  1015. }
  1016. udelay(1);
  1017. }
  1018. }
  1019. static void arm_smmu_tlb_sync(void *cookie)
  1020. {
  1021. struct arm_smmu_domain *smmu_domain = cookie;
  1022. arm_smmu_tlb_sync_cb(smmu_domain->smmu, smmu_domain->cfg.cbndx);
  1023. }
  1024. /* Must be called with clocks/regulators enabled */
  1025. static void arm_smmu_tlb_inv_context(void *cookie)
  1026. {
  1027. struct arm_smmu_domain *smmu_domain = cookie;
  1028. struct device *dev = smmu_domain->dev;
  1029. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1030. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1031. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  1032. void __iomem *base;
  1033. bool use_tlbiall = smmu->options & ARM_SMMU_OPT_NO_ASID_RETENTION;
  1034. ktime_t cur = ktime_get();
  1035. trace_tlbi_start(dev, 0);
  1036. if (stage1 && !use_tlbiall) {
  1037. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1038. writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg),
  1039. base + ARM_SMMU_CB_S1_TLBIASID);
  1040. arm_smmu_tlb_sync_cb(smmu, cfg->cbndx);
  1041. } else if (stage1 && use_tlbiall) {
  1042. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1043. writel_relaxed(0, base + ARM_SMMU_CB_S1_TLBIALL);
  1044. arm_smmu_tlb_sync_cb(smmu, cfg->cbndx);
  1045. } else {
  1046. base = ARM_SMMU_GR0(smmu);
  1047. writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg),
  1048. base + ARM_SMMU_GR0_TLBIVMID);
  1049. __arm_smmu_tlb_sync(smmu);
  1050. }
  1051. trace_tlbi_end(dev, ktime_us_delta(ktime_get(), cur));
  1052. }
  1053. static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  1054. size_t granule, bool leaf, void *cookie)
  1055. {
  1056. struct arm_smmu_domain *smmu_domain = cookie;
  1057. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1058. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1059. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  1060. void __iomem *reg;
  1061. bool use_tlbiall = smmu->options & ARM_SMMU_OPT_NO_ASID_RETENTION;
  1062. if (stage1 && !use_tlbiall) {
  1063. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1064. reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
  1065. if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
  1066. iova &= ~12UL;
  1067. iova |= ARM_SMMU_CB_ASID(smmu, cfg);
  1068. do {
  1069. writel_relaxed(iova, reg);
  1070. iova += granule;
  1071. } while (size -= granule);
  1072. } else {
  1073. iova >>= 12;
  1074. iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48;
  1075. do {
  1076. writeq_relaxed(iova, reg);
  1077. iova += granule >> 12;
  1078. } while (size -= granule);
  1079. }
  1080. } else if (stage1 && use_tlbiall) {
  1081. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1082. reg += ARM_SMMU_CB_S1_TLBIALL;
  1083. writel_relaxed(0, reg);
  1084. } else if (smmu->version == ARM_SMMU_V2) {
  1085. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1086. reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
  1087. ARM_SMMU_CB_S2_TLBIIPAS2;
  1088. iova >>= 12;
  1089. do {
  1090. smmu_write_atomic_lq(iova, reg);
  1091. iova += granule >> 12;
  1092. } while (size -= granule);
  1093. } else {
  1094. reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
  1095. writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg);
  1096. }
  1097. }
  1098. struct arm_smmu_secure_pool_chunk {
  1099. void *addr;
  1100. size_t size;
  1101. struct list_head list;
  1102. };
  1103. static void *arm_smmu_secure_pool_remove(struct arm_smmu_domain *smmu_domain,
  1104. size_t size)
  1105. {
  1106. struct arm_smmu_secure_pool_chunk *it;
  1107. list_for_each_entry(it, &smmu_domain->secure_pool_list, list) {
  1108. if (it->size == size) {
  1109. void *addr = it->addr;
  1110. list_del(&it->list);
  1111. kfree(it);
  1112. return addr;
  1113. }
  1114. }
  1115. return NULL;
  1116. }
  1117. static int arm_smmu_secure_pool_add(struct arm_smmu_domain *smmu_domain,
  1118. void *addr, size_t size)
  1119. {
  1120. struct arm_smmu_secure_pool_chunk *chunk;
  1121. chunk = kmalloc(sizeof(*chunk), GFP_ATOMIC);
  1122. if (!chunk)
  1123. return -ENOMEM;
  1124. chunk->addr = addr;
  1125. chunk->size = size;
  1126. memset(addr, 0, size);
  1127. list_add(&chunk->list, &smmu_domain->secure_pool_list);
  1128. return 0;
  1129. }
  1130. static void arm_smmu_secure_pool_destroy(struct arm_smmu_domain *smmu_domain)
  1131. {
  1132. struct arm_smmu_secure_pool_chunk *it, *i;
  1133. list_for_each_entry_safe(it, i, &smmu_domain->secure_pool_list, list) {
  1134. arm_smmu_unprepare_pgtable(smmu_domain, it->addr, it->size);
  1135. /* pages will be freed later (after being unassigned) */
  1136. list_del(&it->list);
  1137. kfree(it);
  1138. }
  1139. }
  1140. static void *arm_smmu_alloc_pages_exact(void *cookie,
  1141. size_t size, gfp_t gfp_mask)
  1142. {
  1143. int ret;
  1144. void *page;
  1145. struct arm_smmu_domain *smmu_domain = cookie;
  1146. if (!arm_smmu_is_master_side_secure(smmu_domain)) {
  1147. struct page *pg;
  1148. /* size is expected to be 4K with current configuration */
  1149. if (size == PAGE_SIZE) {
  1150. pg = list_first_entry_or_null(
  1151. &smmu_domain->nonsecure_pool, struct page, lru);
  1152. if (pg) {
  1153. list_del_init(&pg->lru);
  1154. return page_address(pg);
  1155. }
  1156. }
  1157. return alloc_pages_exact(size, gfp_mask);
  1158. }
  1159. page = arm_smmu_secure_pool_remove(smmu_domain, size);
  1160. if (page)
  1161. return page;
  1162. page = alloc_pages_exact(size, gfp_mask);
  1163. if (page) {
  1164. ret = arm_smmu_prepare_pgtable(page, cookie);
  1165. if (ret) {
  1166. free_pages_exact(page, size);
  1167. return NULL;
  1168. }
  1169. }
  1170. return page;
  1171. }
  1172. static void arm_smmu_free_pages_exact(void *cookie, void *virt, size_t size)
  1173. {
  1174. struct arm_smmu_domain *smmu_domain = cookie;
  1175. if (!arm_smmu_is_master_side_secure(smmu_domain)) {
  1176. free_pages_exact(virt, size);
  1177. return;
  1178. }
  1179. if (arm_smmu_secure_pool_add(smmu_domain, virt, size))
  1180. arm_smmu_unprepare_pgtable(smmu_domain, virt, size);
  1181. }
  1182. static struct iommu_gather_ops arm_smmu_gather_ops = {
  1183. .tlb_flush_all = arm_smmu_tlb_inv_context,
  1184. .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
  1185. .tlb_sync = arm_smmu_tlb_sync,
  1186. .alloc_pages_exact = arm_smmu_alloc_pages_exact,
  1187. .free_pages_exact = arm_smmu_free_pages_exact,
  1188. };
  1189. static void msm_smmu_tlb_inv_context(void *cookie)
  1190. {
  1191. }
  1192. static void msm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  1193. size_t granule, bool leaf,
  1194. void *cookie)
  1195. {
  1196. }
  1197. static void msm_smmu_tlb_sync(void *cookie)
  1198. {
  1199. }
  1200. static struct iommu_gather_ops msm_smmu_gather_ops = {
  1201. .tlb_flush_all = msm_smmu_tlb_inv_context,
  1202. .tlb_add_flush = msm_smmu_tlb_inv_range_nosync,
  1203. .tlb_sync = msm_smmu_tlb_sync,
  1204. .alloc_pages_exact = arm_smmu_alloc_pages_exact,
  1205. .free_pages_exact = arm_smmu_free_pages_exact,
  1206. };
  1207. static phys_addr_t arm_smmu_verify_fault(struct iommu_domain *domain,
  1208. dma_addr_t iova, u32 fsr)
  1209. {
  1210. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1211. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1212. const struct iommu_gather_ops *tlb = smmu_domain->pgtbl_cfg.tlb;
  1213. phys_addr_t phys;
  1214. phys_addr_t phys_post_tlbiall;
  1215. phys = arm_smmu_iova_to_phys_hard(domain, iova);
  1216. tlb->tlb_flush_all(smmu_domain);
  1217. phys_post_tlbiall = arm_smmu_iova_to_phys_hard(domain, iova);
  1218. if (phys != phys_post_tlbiall) {
  1219. dev_err(smmu->dev,
  1220. "ATOS results differed across TLBIALL...\n"
  1221. "Before: %pa After: %pa\n", &phys, &phys_post_tlbiall);
  1222. }
  1223. return (phys == 0 ? phys_post_tlbiall : phys);
  1224. }
  1225. static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  1226. {
  1227. int flags, ret, tmp;
  1228. u32 fsr, fsynr, resume;
  1229. unsigned long iova;
  1230. struct iommu_domain *domain = dev;
  1231. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1232. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1233. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1234. void __iomem *cb_base;
  1235. void __iomem *gr1_base;
  1236. bool fatal_asf = smmu->options & ARM_SMMU_OPT_FATAL_ASF;
  1237. phys_addr_t phys_soft;
  1238. u32 frsynra;
  1239. bool non_fatal_fault = !!(smmu_domain->attributes &
  1240. (1 << DOMAIN_ATTR_NON_FATAL_FAULTS));
  1241. static DEFINE_RATELIMIT_STATE(_rs,
  1242. DEFAULT_RATELIMIT_INTERVAL,
  1243. DEFAULT_RATELIMIT_BURST);
  1244. ret = arm_smmu_power_on(smmu->pwr);
  1245. if (ret)
  1246. return IRQ_NONE;
  1247. gr1_base = ARM_SMMU_GR1(smmu);
  1248. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1249. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  1250. if (!(fsr & FSR_FAULT)) {
  1251. ret = IRQ_NONE;
  1252. goto out_power_off;
  1253. }
  1254. if (fatal_asf && (fsr & FSR_ASF)) {
  1255. dev_err(smmu->dev,
  1256. "Took an address size fault. Refusing to recover.\n");
  1257. BUG();
  1258. }
  1259. fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
  1260. flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  1261. if (fsr & FSR_TF)
  1262. flags |= IOMMU_FAULT_TRANSLATION;
  1263. if (fsr & FSR_PF)
  1264. flags |= IOMMU_FAULT_PERMISSION;
  1265. if (fsr & FSR_EF)
  1266. flags |= IOMMU_FAULT_EXTERNAL;
  1267. if (fsr & FSR_SS)
  1268. flags |= IOMMU_FAULT_TRANSACTION_STALLED;
  1269. iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
  1270. phys_soft = arm_smmu_iova_to_phys(domain, iova);
  1271. frsynra = readl_relaxed(gr1_base + ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
  1272. frsynra &= CBFRSYNRA_SID_MASK;
  1273. tmp = report_iommu_fault(domain, smmu->dev, iova, flags);
  1274. if (!tmp || (tmp == -EBUSY)) {
  1275. dev_dbg(smmu->dev,
  1276. "Context fault handled by client: iova=0x%08lx, fsr=0x%x, fsynr=0x%x, cb=%d\n",
  1277. iova, fsr, fsynr, cfg->cbndx);
  1278. dev_dbg(smmu->dev,
  1279. "soft iova-to-phys=%pa\n", &phys_soft);
  1280. ret = IRQ_HANDLED;
  1281. resume = RESUME_TERMINATE;
  1282. } else {
  1283. phys_addr_t phys_atos = arm_smmu_verify_fault(domain, iova,
  1284. fsr);
  1285. if (__ratelimit(&_rs)) {
  1286. dev_err(smmu->dev,
  1287. "Unhandled context fault: iova=0x%08lx, fsr=0x%x, fsynr=0x%x, cb=%d\n",
  1288. iova, fsr, fsynr, cfg->cbndx);
  1289. dev_err(smmu->dev, "FAR = %016lx\n",
  1290. (unsigned long)iova);
  1291. dev_err(smmu->dev,
  1292. "FSR = %08x [%s%s%s%s%s%s%s%s%s]\n",
  1293. fsr,
  1294. (fsr & 0x02) ? "TF " : "",
  1295. (fsr & 0x04) ? "AFF " : "",
  1296. (fsr & 0x08) ? "PF " : "",
  1297. (fsr & 0x10) ? "EF " : "",
  1298. (fsr & 0x20) ? "TLBMCF " : "",
  1299. (fsr & 0x40) ? "TLBLKF " : "",
  1300. (fsr & 0x80) ? "MHF " : "",
  1301. (fsr & 0x40000000) ? "SS " : "",
  1302. (fsr & 0x80000000) ? "MULTI " : "");
  1303. dev_err(smmu->dev,
  1304. "soft iova-to-phys=%pa\n", &phys_soft);
  1305. if (!phys_soft)
  1306. dev_err(smmu->dev,
  1307. "SOFTWARE TABLE WALK FAILED! Looks like %s accessed an unmapped address!\n",
  1308. dev_name(smmu->dev));
  1309. if (phys_atos)
  1310. dev_err(smmu->dev, "hard iova-to-phys (ATOS)=%pa\n",
  1311. &phys_atos);
  1312. else
  1313. dev_err(smmu->dev, "hard iova-to-phys (ATOS) failed\n");
  1314. dev_err(smmu->dev, "SID=0x%x\n", frsynra);
  1315. }
  1316. ret = IRQ_NONE;
  1317. resume = RESUME_TERMINATE;
  1318. if (!non_fatal_fault) {
  1319. dev_err(smmu->dev,
  1320. "Unhandled arm-smmu context fault!\n");
  1321. BUG();
  1322. }
  1323. }
  1324. /*
  1325. * If the client returns -EBUSY, do not clear FSR and do not RESUME
  1326. * if stalled. This is required to keep the IOMMU client stalled on
  1327. * the outstanding fault. This gives the client a chance to take any
  1328. * debug action and then terminate the stalled transaction.
  1329. * So, the sequence in case of stall on fault should be:
  1330. * 1) Do not clear FSR or write to RESUME here
  1331. * 2) Client takes any debug action
  1332. * 3) Client terminates the stalled transaction and resumes the IOMMU
  1333. * 4) Client clears FSR. The FSR should only be cleared after 3) and
  1334. * not before so that the fault remains outstanding. This ensures
  1335. * SCTLR.HUPCF has the desired effect if subsequent transactions also
  1336. * need to be terminated.
  1337. */
  1338. if (tmp != -EBUSY) {
  1339. /* Clear the faulting FSR */
  1340. writel_relaxed(fsr, cb_base + ARM_SMMU_CB_FSR);
  1341. /*
  1342. * Barrier required to ensure that the FSR is cleared
  1343. * before resuming SMMU operation
  1344. */
  1345. wmb();
  1346. /* Retry or terminate any stalled transactions */
  1347. if (fsr & FSR_SS)
  1348. writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
  1349. }
  1350. out_power_off:
  1351. arm_smmu_power_off(smmu->pwr);
  1352. return ret;
  1353. }
  1354. static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
  1355. {
  1356. u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
  1357. struct arm_smmu_device *smmu = dev;
  1358. void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
  1359. if (arm_smmu_power_on(smmu->pwr))
  1360. return IRQ_NONE;
  1361. gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  1362. gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
  1363. gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
  1364. gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
  1365. if (!gfsr) {
  1366. arm_smmu_power_off(smmu->pwr);
  1367. return IRQ_NONE;
  1368. }
  1369. dev_err_ratelimited(smmu->dev,
  1370. "Unexpected global fault, this could be serious\n");
  1371. dev_err_ratelimited(smmu->dev,
  1372. "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
  1373. gfsr, gfsynr0, gfsynr1, gfsynr2);
  1374. writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
  1375. arm_smmu_power_off(smmu->pwr);
  1376. return IRQ_HANDLED;
  1377. }
  1378. static bool arm_smmu_master_attached(struct arm_smmu_device *smmu,
  1379. struct iommu_fwspec *fwspec)
  1380. {
  1381. int i, idx;
  1382. for_each_cfg_sme(fwspec, i, idx) {
  1383. if (smmu->s2crs[idx].attach_count)
  1384. return true;
  1385. }
  1386. return false;
  1387. }
  1388. static int arm_smmu_set_pt_format(struct arm_smmu_domain *smmu_domain,
  1389. struct io_pgtable_cfg *pgtbl_cfg)
  1390. {
  1391. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1392. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1393. int ret = 0;
  1394. if ((smmu->version > ARM_SMMU_V1) &&
  1395. (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) &&
  1396. !arm_smmu_has_secure_vmid(smmu_domain) &&
  1397. arm_smmu_is_static_cb(smmu)) {
  1398. ret = msm_tz_set_cb_format(smmu->sec_id, cfg->cbndx);
  1399. }
  1400. return ret;
  1401. }
  1402. static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
  1403. struct io_pgtable_cfg *pgtbl_cfg)
  1404. {
  1405. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1406. struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
  1407. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  1408. cb->cfg = cfg;
  1409. /* TTBCR */
  1410. if (stage1) {
  1411. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  1412. cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
  1413. } else {
  1414. cb->tcr[0] = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
  1415. cb->tcr[1] = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
  1416. cb->tcr[1] |= TTBCR2_SEP_UPSTREAM;
  1417. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
  1418. cb->tcr[1] |= TTBCR2_AS;
  1419. }
  1420. } else {
  1421. cb->tcr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
  1422. }
  1423. /* TTBRs */
  1424. if (stage1) {
  1425. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  1426. cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
  1427. cb->ttbr[1] = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
  1428. } else {
  1429. cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
  1430. cb->ttbr[0] |= (u64)cfg->asid << TTBRn_ASID_SHIFT;
  1431. cb->ttbr[1] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
  1432. cb->ttbr[1] |= (u64)cfg->asid << TTBRn_ASID_SHIFT;
  1433. }
  1434. } else {
  1435. cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
  1436. }
  1437. /* MAIRs (stage-1 only) */
  1438. if (stage1) {
  1439. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  1440. cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
  1441. cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
  1442. } else {
  1443. cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
  1444. cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
  1445. }
  1446. }
  1447. cb->attributes = smmu_domain->attributes;
  1448. }
  1449. static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
  1450. {
  1451. u32 reg;
  1452. bool stage1;
  1453. struct arm_smmu_cb *cb = &smmu->cbs[idx];
  1454. struct arm_smmu_cfg *cfg = cb->cfg;
  1455. void __iomem *cb_base, *gr1_base;
  1456. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, idx);
  1457. /* Unassigned context banks only need disabling */
  1458. if (!cfg) {
  1459. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  1460. return;
  1461. }
  1462. gr1_base = ARM_SMMU_GR1(smmu);
  1463. stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  1464. /* CBA2R */
  1465. if (smmu->version > ARM_SMMU_V1) {
  1466. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
  1467. reg = CBA2R_RW64_64BIT;
  1468. else
  1469. reg = CBA2R_RW64_32BIT;
  1470. /* 16-bit VMIDs live in CBA2R */
  1471. if (smmu->features & ARM_SMMU_FEAT_VMID16)
  1472. reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT;
  1473. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(idx));
  1474. }
  1475. /* CBAR */
  1476. reg = cfg->cbar;
  1477. if (smmu->version < ARM_SMMU_V2)
  1478. reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
  1479. /*
  1480. * Use the weakest shareability/memory types, so they are
  1481. * overridden by the ttbcr/pte.
  1482. */
  1483. if (stage1) {
  1484. reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
  1485. (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
  1486. } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
  1487. /* 8-bit VMIDs live in CBAR */
  1488. reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT;
  1489. }
  1490. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(idx));
  1491. /*
  1492. * TTBCR
  1493. * We must write this before the TTBRs, since it determines the
  1494. * access behaviour of some fields (in particular, ASID[15:8]).
  1495. */
  1496. if (stage1 && smmu->version > ARM_SMMU_V1)
  1497. writel_relaxed(cb->tcr[1], cb_base + ARM_SMMU_CB_TTBCR2);
  1498. writel_relaxed(cb->tcr[0], cb_base + ARM_SMMU_CB_TTBCR);
  1499. /* TTBRs */
  1500. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  1501. writel_relaxed(cfg->asid, cb_base + ARM_SMMU_CB_CONTEXTIDR);
  1502. writel_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0);
  1503. writel_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1);
  1504. } else {
  1505. writeq_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0);
  1506. if (stage1)
  1507. writeq_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1);
  1508. }
  1509. /* MAIRs (stage-1 only) */
  1510. if (stage1) {
  1511. writel_relaxed(cb->mair[0], cb_base + ARM_SMMU_CB_S1_MAIR0);
  1512. writel_relaxed(cb->mair[1], cb_base + ARM_SMMU_CB_S1_MAIR1);
  1513. }
  1514. /* ACTLR (implementation defined) */
  1515. writel_relaxed(cb->actlr, cb_base + ARM_SMMU_CB_ACTLR);
  1516. /* SCTLR */
  1517. reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE;
  1518. /* Ensure bypass transactions are Non-shareable */
  1519. reg |= SCTLR_SHCFG_NSH << SCTLR_SHCFG_SHIFT;
  1520. if (cb->attributes & (1 << DOMAIN_ATTR_CB_STALL_DISABLE)) {
  1521. reg &= ~SCTLR_CFCFG;
  1522. reg |= SCTLR_HUPCF;
  1523. }
  1524. if ((!(cb->attributes & (1 << DOMAIN_ATTR_S1_BYPASS)) &&
  1525. !(cb->attributes & (1 << DOMAIN_ATTR_EARLY_MAP))) ||
  1526. !stage1)
  1527. reg |= SCTLR_M;
  1528. if (stage1)
  1529. reg |= SCTLR_S1_ASIDPNE;
  1530. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1531. reg |= SCTLR_E;
  1532. writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
  1533. }
  1534. static int arm_smmu_init_asid(struct iommu_domain *domain,
  1535. struct arm_smmu_device *smmu)
  1536. {
  1537. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1538. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1539. bool dynamic = is_dynamic_domain(domain);
  1540. int ret;
  1541. if (!dynamic) {
  1542. cfg->asid = cfg->cbndx + 1;
  1543. } else {
  1544. mutex_lock(&smmu->idr_mutex);
  1545. ret = idr_alloc_cyclic(&smmu->asid_idr, domain,
  1546. smmu->num_context_banks + 2,
  1547. MAX_ASID + 1, GFP_KERNEL);
  1548. mutex_unlock(&smmu->idr_mutex);
  1549. if (ret < 0) {
  1550. dev_err(smmu->dev, "dynamic ASID allocation failed: %d\n",
  1551. ret);
  1552. return ret;
  1553. }
  1554. cfg->asid = ret;
  1555. }
  1556. return 0;
  1557. }
  1558. static void arm_smmu_free_asid(struct iommu_domain *domain)
  1559. {
  1560. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1561. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1562. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1563. bool dynamic = is_dynamic_domain(domain);
  1564. if (cfg->asid == INVALID_ASID || !dynamic)
  1565. return;
  1566. mutex_lock(&smmu->idr_mutex);
  1567. idr_remove(&smmu->asid_idr, cfg->asid);
  1568. mutex_unlock(&smmu->idr_mutex);
  1569. }
  1570. static int arm_smmu_init_domain_context(struct iommu_domain *domain,
  1571. struct arm_smmu_device *smmu,
  1572. struct device *dev)
  1573. {
  1574. int irq, start, ret = 0;
  1575. unsigned long ias, oas;
  1576. struct io_pgtable_ops *pgtbl_ops;
  1577. enum io_pgtable_fmt fmt;
  1578. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1579. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1580. bool is_fast = smmu_domain->attributes & (1 << DOMAIN_ATTR_FAST);
  1581. unsigned long quirks = 0;
  1582. bool dynamic;
  1583. const struct iommu_gather_ops *tlb;
  1584. mutex_lock(&smmu_domain->init_mutex);
  1585. if (smmu_domain->smmu)
  1586. goto out_unlock;
  1587. smmu_domain->cfg.irptndx = INVALID_IRPTNDX;
  1588. smmu_domain->cfg.asid = INVALID_ASID;
  1589. dynamic = is_dynamic_domain(domain);
  1590. if (dynamic && !(smmu->options & ARM_SMMU_OPT_DYNAMIC)) {
  1591. dev_err(smmu->dev, "dynamic domains not supported\n");
  1592. ret = -EPERM;
  1593. goto out_unlock;
  1594. }
  1595. if (arm_smmu_has_secure_vmid(smmu_domain) &&
  1596. arm_smmu_opt_hibernation(smmu)) {
  1597. dev_err(smmu->dev,
  1598. "Secure usecases not supported with hibernation\n");
  1599. ret = -EPERM;
  1600. goto out_unlock;
  1601. }
  1602. /*
  1603. * Mapping the requested stage onto what we support is surprisingly
  1604. * complicated, mainly because the spec allows S1+S2 SMMUs without
  1605. * support for nested translation. That means we end up with the
  1606. * following table:
  1607. *
  1608. * Requested Supported Actual
  1609. * S1 N S1
  1610. * S1 S1+S2 S1
  1611. * S1 S2 S2
  1612. * S1 S1 S1
  1613. * N N N
  1614. * N S1+S2 S2
  1615. * N S2 S2
  1616. * N S1 S1
  1617. *
  1618. * Note that you can't actually request stage-2 mappings.
  1619. */
  1620. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
  1621. smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
  1622. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
  1623. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1624. /*
  1625. * Choosing a suitable context format is even more fiddly. Until we
  1626. * grow some way for the caller to express a preference, and/or move
  1627. * the decision into the io-pgtable code where it arguably belongs,
  1628. * just aim for the closest thing to the rest of the system, and hope
  1629. * that the hardware isn't esoteric enough that we can't assume AArch64
  1630. * support to be a superset of AArch32 support...
  1631. */
  1632. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
  1633. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
  1634. if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
  1635. !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
  1636. (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
  1637. (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
  1638. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
  1639. if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
  1640. (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
  1641. ARM_SMMU_FEAT_FMT_AARCH64_16K |
  1642. ARM_SMMU_FEAT_FMT_AARCH64_4K)))
  1643. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
  1644. if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
  1645. ret = -EINVAL;
  1646. goto out_unlock;
  1647. }
  1648. switch (smmu_domain->stage) {
  1649. case ARM_SMMU_DOMAIN_S1:
  1650. cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  1651. start = smmu->num_s2_context_banks;
  1652. ias = smmu->va_size;
  1653. oas = smmu->ipa_size;
  1654. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
  1655. fmt = ARM_64_LPAE_S1;
  1656. if (smmu->options & ARM_SMMU_OPT_3LVL_TABLES)
  1657. ias = min(ias, 39UL);
  1658. } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
  1659. fmt = ARM_32_LPAE_S1;
  1660. ias = min(ias, 32UL);
  1661. oas = min(oas, 40UL);
  1662. } else {
  1663. fmt = ARM_V7S;
  1664. ias = min(ias, 32UL);
  1665. oas = min(oas, 32UL);
  1666. }
  1667. break;
  1668. case ARM_SMMU_DOMAIN_NESTED:
  1669. /*
  1670. * We will likely want to change this if/when KVM gets
  1671. * involved.
  1672. */
  1673. case ARM_SMMU_DOMAIN_S2:
  1674. cfg->cbar = CBAR_TYPE_S2_TRANS;
  1675. start = 0;
  1676. ias = smmu->ipa_size;
  1677. oas = smmu->pa_size;
  1678. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
  1679. fmt = ARM_64_LPAE_S2;
  1680. } else {
  1681. fmt = ARM_32_LPAE_S2;
  1682. ias = min(ias, 40UL);
  1683. oas = min(oas, 40UL);
  1684. }
  1685. break;
  1686. default:
  1687. ret = -EINVAL;
  1688. goto out_unlock;
  1689. }
  1690. if (is_fast)
  1691. fmt = ARM_V8L_FAST;
  1692. if (smmu_domain->attributes & (1 << DOMAIN_ATTR_USE_UPSTREAM_HINT))
  1693. quirks |= IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT;
  1694. if (is_iommu_pt_coherent(smmu_domain))
  1695. quirks |= IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT;
  1696. if ((quirks & IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT) &&
  1697. (smmu->model == QCOM_SMMUV500))
  1698. quirks |= IO_PGTABLE_QUIRK_QSMMUV500_NON_SHAREABLE;
  1699. tlb = &arm_smmu_gather_ops;
  1700. if (smmu->options & ARM_SMMU_OPT_MMU500_ERRATA1)
  1701. tlb = &qsmmuv500_errata1_smmu_gather_ops;
  1702. if (arm_smmu_is_slave_side_secure(smmu_domain))
  1703. tlb = &msm_smmu_gather_ops;
  1704. ret = arm_smmu_alloc_cb(domain, smmu, dev);
  1705. if (ret < 0)
  1706. goto out_unlock;
  1707. cfg->cbndx = ret;
  1708. if (smmu->version < ARM_SMMU_V2) {
  1709. cfg->irptndx = atomic_inc_return(&smmu->irptndx);
  1710. cfg->irptndx %= smmu->num_context_irqs;
  1711. } else {
  1712. cfg->irptndx = cfg->cbndx;
  1713. }
  1714. if (arm_smmu_is_slave_side_secure(smmu_domain)) {
  1715. smmu_domain->pgtbl_cfg = (struct io_pgtable_cfg) {
  1716. .quirks = quirks,
  1717. .pgsize_bitmap = smmu->pgsize_bitmap,
  1718. .arm_msm_secure_cfg = {
  1719. .sec_id = smmu->sec_id,
  1720. .cbndx = cfg->cbndx,
  1721. },
  1722. .tlb = tlb,
  1723. .iommu_dev = smmu->dev,
  1724. };
  1725. fmt = ARM_MSM_SECURE;
  1726. } else {
  1727. smmu_domain->pgtbl_cfg = (struct io_pgtable_cfg) {
  1728. .quirks = quirks,
  1729. .pgsize_bitmap = smmu->pgsize_bitmap,
  1730. .ias = ias,
  1731. .oas = oas,
  1732. .tlb = tlb,
  1733. .iommu_dev = smmu->dev,
  1734. };
  1735. }
  1736. smmu_domain->smmu = smmu;
  1737. smmu_domain->dev = dev;
  1738. pgtbl_ops = alloc_io_pgtable_ops(fmt, &smmu_domain->pgtbl_cfg,
  1739. smmu_domain);
  1740. if (!pgtbl_ops) {
  1741. ret = -ENOMEM;
  1742. goto out_clear_smmu;
  1743. }
  1744. /*
  1745. * assign any page table memory that might have been allocated
  1746. * during alloc_io_pgtable_ops
  1747. */
  1748. arm_smmu_secure_domain_lock(smmu_domain);
  1749. arm_smmu_assign_table(smmu_domain);
  1750. arm_smmu_secure_domain_unlock(smmu_domain);
  1751. /* Update the domain's page sizes to reflect the page table format */
  1752. domain->pgsize_bitmap = smmu_domain->pgtbl_cfg.pgsize_bitmap;
  1753. domain->geometry.aperture_end = (1UL << ias) - 1;
  1754. domain->geometry.force_aperture = true;
  1755. /* Assign an asid */
  1756. ret = arm_smmu_init_asid(domain, smmu);
  1757. if (ret)
  1758. goto out_clear_smmu;
  1759. if (!dynamic) {
  1760. /* Initialise the context bank with our page table cfg */
  1761. arm_smmu_init_context_bank(smmu_domain,
  1762. &smmu_domain->pgtbl_cfg);
  1763. arm_smmu_arch_init_context_bank(smmu_domain, dev);
  1764. arm_smmu_write_context_bank(smmu, cfg->cbndx);
  1765. /* for slave side secure, we may have to force the pagetable
  1766. * format to V8L.
  1767. */
  1768. ret = arm_smmu_set_pt_format(smmu_domain,
  1769. &smmu_domain->pgtbl_cfg);
  1770. if (ret)
  1771. goto out_clear_smmu;
  1772. /*
  1773. * Request context fault interrupt. Do this last to avoid the
  1774. * handler seeing a half-initialised domain state.
  1775. */
  1776. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  1777. ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
  1778. arm_smmu_context_fault, IRQF_ONESHOT | IRQF_SHARED,
  1779. "arm-smmu-context-fault", domain);
  1780. if (ret < 0) {
  1781. dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
  1782. cfg->irptndx, irq);
  1783. cfg->irptndx = INVALID_IRPTNDX;
  1784. goto out_clear_smmu;
  1785. }
  1786. } else {
  1787. cfg->irptndx = INVALID_IRPTNDX;
  1788. }
  1789. mutex_unlock(&smmu_domain->init_mutex);
  1790. /* Publish page table ops for map/unmap */
  1791. smmu_domain->pgtbl_ops = pgtbl_ops;
  1792. if (arm_smmu_is_slave_side_secure(smmu_domain) &&
  1793. !arm_smmu_master_attached(smmu, dev->iommu_fwspec))
  1794. arm_smmu_restore_sec_cfg(smmu, cfg->cbndx);
  1795. return 0;
  1796. out_clear_smmu:
  1797. arm_smmu_destroy_domain_context(domain);
  1798. smmu_domain->smmu = NULL;
  1799. out_unlock:
  1800. mutex_unlock(&smmu_domain->init_mutex);
  1801. return ret;
  1802. }
  1803. static void arm_smmu_domain_reinit(struct arm_smmu_domain *smmu_domain)
  1804. {
  1805. smmu_domain->cfg.irptndx = INVALID_IRPTNDX;
  1806. smmu_domain->cfg.cbndx = INVALID_CBNDX;
  1807. smmu_domain->secure_vmid = VMID_INVAL;
  1808. }
  1809. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
  1810. {
  1811. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1812. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1813. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1814. int irq;
  1815. bool dynamic;
  1816. int ret;
  1817. if (!smmu)
  1818. return;
  1819. ret = arm_smmu_power_on(smmu->pwr);
  1820. if (ret) {
  1821. WARN_ONCE(ret, "Woops, powering on smmu %p failed. Leaking context bank\n",
  1822. smmu);
  1823. return;
  1824. }
  1825. dynamic = is_dynamic_domain(domain);
  1826. if (dynamic) {
  1827. arm_smmu_free_asid(domain);
  1828. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  1829. arm_smmu_power_off(smmu->pwr);
  1830. arm_smmu_secure_domain_lock(smmu_domain);
  1831. arm_smmu_secure_pool_destroy(smmu_domain);
  1832. arm_smmu_unassign_table(smmu_domain);
  1833. arm_smmu_secure_domain_unlock(smmu_domain);
  1834. arm_smmu_domain_reinit(smmu_domain);
  1835. return;
  1836. }
  1837. /*
  1838. * Disable the context bank and free the page tables before freeing
  1839. * it.
  1840. */
  1841. smmu->cbs[cfg->cbndx].cfg = NULL;
  1842. arm_smmu_write_context_bank(smmu, cfg->cbndx);
  1843. if (cfg->irptndx != INVALID_IRPTNDX) {
  1844. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  1845. devm_free_irq(smmu->dev, irq, domain);
  1846. }
  1847. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  1848. arm_smmu_secure_domain_lock(smmu_domain);
  1849. arm_smmu_secure_pool_destroy(smmu_domain);
  1850. arm_smmu_unassign_table(smmu_domain);
  1851. arm_smmu_secure_domain_unlock(smmu_domain);
  1852. __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
  1853. /* As the nonsecure context bank index is any way set to zero,
  1854. * so, directly clearing up the secure cb bitmap.
  1855. */
  1856. if (arm_smmu_is_slave_side_secure(smmu_domain))
  1857. __arm_smmu_free_bitmap(smmu->secure_context_map, cfg->cbndx);
  1858. arm_smmu_power_off(smmu->pwr);
  1859. arm_smmu_domain_reinit(smmu_domain);
  1860. }
  1861. static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
  1862. {
  1863. struct arm_smmu_domain *smmu_domain;
  1864. /* Do not support DOMAIN_DMA for now */
  1865. if (type != IOMMU_DOMAIN_UNMANAGED)
  1866. return NULL;
  1867. /*
  1868. * Allocate the domain and initialise some of its data structures.
  1869. * We can't really do anything meaningful until we've added a
  1870. * master.
  1871. */
  1872. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  1873. if (!smmu_domain)
  1874. return NULL;
  1875. if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
  1876. iommu_get_dma_cookie(&smmu_domain->domain))) {
  1877. kfree(smmu_domain);
  1878. return NULL;
  1879. }
  1880. mutex_init(&smmu_domain->init_mutex);
  1881. spin_lock_init(&smmu_domain->pgtbl_lock);
  1882. INIT_LIST_HEAD(&smmu_domain->pte_info_list);
  1883. INIT_LIST_HEAD(&smmu_domain->unassign_list);
  1884. mutex_init(&smmu_domain->assign_lock);
  1885. INIT_LIST_HEAD(&smmu_domain->secure_pool_list);
  1886. INIT_LIST_HEAD(&smmu_domain->nonsecure_pool);
  1887. arm_smmu_domain_reinit(smmu_domain);
  1888. return &smmu_domain->domain;
  1889. }
  1890. static void arm_smmu_domain_free(struct iommu_domain *domain)
  1891. {
  1892. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1893. /*
  1894. * Free the domain resources. We assume that all devices have
  1895. * already been detached.
  1896. */
  1897. iommu_put_dma_cookie(domain);
  1898. arm_smmu_destroy_domain_context(domain);
  1899. kfree(smmu_domain);
  1900. }
  1901. static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
  1902. {
  1903. struct arm_smmu_smr *smr = smmu->smrs + idx;
  1904. u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT;
  1905. if (smr->valid)
  1906. reg |= SMR_VALID;
  1907. writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx));
  1908. }
  1909. static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
  1910. {
  1911. struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
  1912. u32 reg = (s2cr->type & S2CR_TYPE_MASK) << S2CR_TYPE_SHIFT |
  1913. (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT |
  1914. (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT |
  1915. S2CR_SHCFG_NSH << S2CR_SHCFG_SHIFT;
  1916. writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx));
  1917. }
  1918. static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
  1919. {
  1920. arm_smmu_write_s2cr(smmu, idx);
  1921. if (smmu->smrs)
  1922. arm_smmu_write_smr(smmu, idx);
  1923. }
  1924. static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
  1925. {
  1926. struct arm_smmu_smr *smrs = smmu->smrs;
  1927. int i, free_idx = -ENOSPC;
  1928. /* Stream indexing is blissfully easy */
  1929. if (!smrs)
  1930. return id;
  1931. /* Validating SMRs is... less so */
  1932. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  1933. if (!smrs[i].valid) {
  1934. /*
  1935. * Note the first free entry we come across, which
  1936. * we'll claim in the end if nothing else matches.
  1937. */
  1938. if (free_idx < 0)
  1939. free_idx = i;
  1940. continue;
  1941. }
  1942. /*
  1943. * If the new entry is _entirely_ matched by an existing entry,
  1944. * then reuse that, with the guarantee that there also cannot
  1945. * be any subsequent conflicting entries. In normal use we'd
  1946. * expect simply identical entries for this case, but there's
  1947. * no harm in accommodating the generalisation.
  1948. */
  1949. if ((mask & smrs[i].mask) == mask &&
  1950. !((id ^ smrs[i].id) & ~smrs[i].mask))
  1951. return i;
  1952. /*
  1953. * If the new entry has any other overlap with an existing one,
  1954. * though, then there always exists at least one stream ID
  1955. * which would cause a conflict, and we can't allow that risk.
  1956. */
  1957. if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
  1958. return -EINVAL;
  1959. }
  1960. return free_idx;
  1961. }
  1962. static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
  1963. {
  1964. if (--smmu->s2crs[idx].count)
  1965. return false;
  1966. smmu->s2crs[idx] = s2cr_init_val;
  1967. if (smmu->smrs)
  1968. smmu->smrs[idx].valid = false;
  1969. return true;
  1970. }
  1971. static int arm_smmu_master_alloc_smes(struct device *dev)
  1972. {
  1973. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1974. struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
  1975. struct arm_smmu_device *smmu = cfg->smmu;
  1976. struct arm_smmu_smr *smrs = smmu->smrs;
  1977. struct iommu_group *group;
  1978. int i, idx, ret;
  1979. mutex_lock(&smmu->stream_map_mutex);
  1980. /* Figure out a viable stream map entry allocation */
  1981. for_each_cfg_sme(fwspec, i, idx) {
  1982. u16 sid = fwspec->ids[i];
  1983. u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
  1984. if (idx != INVALID_SMENDX) {
  1985. ret = -EEXIST;
  1986. goto out_err;
  1987. }
  1988. ret = arm_smmu_find_sme(smmu, sid, mask);
  1989. if (ret < 0)
  1990. goto out_err;
  1991. idx = ret;
  1992. if (smrs && smmu->s2crs[idx].count == 0) {
  1993. smrs[idx].id = sid;
  1994. smrs[idx].mask = mask;
  1995. smrs[idx].valid = true;
  1996. }
  1997. smmu->s2crs[idx].count++;
  1998. cfg->smendx[i] = (s16)idx;
  1999. }
  2000. group = iommu_group_get_for_dev(dev);
  2001. if (!group)
  2002. group = ERR_PTR(-ENOMEM);
  2003. if (IS_ERR(group)) {
  2004. ret = PTR_ERR(group);
  2005. goto out_err;
  2006. }
  2007. iommu_group_put(group);
  2008. /* It worked! Don't poke the actual hardware until we've attached */
  2009. for_each_cfg_sme(fwspec, i, idx)
  2010. smmu->s2crs[idx].group = group;
  2011. mutex_unlock(&smmu->stream_map_mutex);
  2012. return 0;
  2013. out_err:
  2014. while (i--) {
  2015. arm_smmu_free_sme(smmu, cfg->smendx[i]);
  2016. cfg->smendx[i] = INVALID_SMENDX;
  2017. }
  2018. mutex_unlock(&smmu->stream_map_mutex);
  2019. return ret;
  2020. }
  2021. static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
  2022. {
  2023. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  2024. struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
  2025. int i, idx;
  2026. mutex_lock(&smmu->stream_map_mutex);
  2027. for_each_cfg_sme(fwspec, i, idx) {
  2028. if (arm_smmu_free_sme(smmu, idx))
  2029. arm_smmu_write_sme(smmu, idx);
  2030. cfg->smendx[i] = INVALID_SMENDX;
  2031. }
  2032. mutex_unlock(&smmu->stream_map_mutex);
  2033. }
  2034. static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
  2035. struct iommu_fwspec *fwspec)
  2036. {
  2037. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2038. struct arm_smmu_s2cr *s2cr = smmu->s2crs;
  2039. int i, idx;
  2040. const struct iommu_gather_ops *tlb;
  2041. tlb = smmu_domain->pgtbl_cfg.tlb;
  2042. mutex_lock(&smmu->stream_map_mutex);
  2043. for_each_cfg_sme(fwspec, i, idx) {
  2044. WARN_ON(s2cr[idx].attach_count == 0);
  2045. s2cr[idx].attach_count -= 1;
  2046. if (s2cr[idx].attach_count > 0)
  2047. continue;
  2048. writel_relaxed(0, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx));
  2049. writel_relaxed(0, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx));
  2050. }
  2051. mutex_unlock(&smmu->stream_map_mutex);
  2052. /* Ensure there are no stale mappings for this context bank */
  2053. tlb->tlb_flush_all(smmu_domain);
  2054. }
  2055. static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
  2056. struct iommu_fwspec *fwspec)
  2057. {
  2058. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2059. struct arm_smmu_s2cr *s2cr = smmu->s2crs;
  2060. enum arm_smmu_s2cr_type type = S2CR_TYPE_TRANS;
  2061. u8 cbndx = smmu_domain->cfg.cbndx;
  2062. int i, idx;
  2063. mutex_lock(&smmu->stream_map_mutex);
  2064. for_each_cfg_sme(fwspec, i, idx) {
  2065. if (s2cr[idx].attach_count++ > 0)
  2066. continue;
  2067. s2cr[idx].type = type;
  2068. s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
  2069. s2cr[idx].cbndx = cbndx;
  2070. arm_smmu_write_sme(smmu, idx);
  2071. }
  2072. mutex_unlock(&smmu->stream_map_mutex);
  2073. return 0;
  2074. }
  2075. static void arm_smmu_detach_dev(struct iommu_domain *domain,
  2076. struct device *dev)
  2077. {
  2078. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2079. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2080. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  2081. int dynamic = smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC);
  2082. int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC);
  2083. if (dynamic)
  2084. return;
  2085. if (!smmu) {
  2086. dev_err(dev, "Domain not attached; cannot detach!\n");
  2087. return;
  2088. }
  2089. if (atomic_domain)
  2090. arm_smmu_power_on_atomic(smmu->pwr);
  2091. else
  2092. arm_smmu_power_on(smmu->pwr);
  2093. arm_smmu_domain_remove_master(smmu_domain, fwspec);
  2094. arm_smmu_power_off(smmu->pwr);
  2095. }
  2096. static int arm_smmu_assign_table(struct arm_smmu_domain *smmu_domain)
  2097. {
  2098. int ret = 0;
  2099. int dest_vmids[2] = {VMID_HLOS, smmu_domain->secure_vmid};
  2100. int dest_perms[2] = {PERM_READ | PERM_WRITE, PERM_READ};
  2101. int source_vmid = VMID_HLOS;
  2102. struct arm_smmu_pte_info *pte_info, *temp;
  2103. if (!arm_smmu_is_master_side_secure(smmu_domain))
  2104. return ret;
  2105. list_for_each_entry(pte_info, &smmu_domain->pte_info_list, entry) {
  2106. ret = hyp_assign_phys(virt_to_phys(pte_info->virt_addr),
  2107. PAGE_SIZE, &source_vmid, 1,
  2108. dest_vmids, dest_perms, 2);
  2109. if (WARN_ON(ret))
  2110. break;
  2111. }
  2112. list_for_each_entry_safe(pte_info, temp, &smmu_domain->pte_info_list,
  2113. entry) {
  2114. list_del(&pte_info->entry);
  2115. kfree(pte_info);
  2116. }
  2117. return ret;
  2118. }
  2119. static void arm_smmu_unassign_table(struct arm_smmu_domain *smmu_domain)
  2120. {
  2121. int ret;
  2122. int dest_vmids = VMID_HLOS;
  2123. int dest_perms = PERM_READ | PERM_WRITE | PERM_EXEC;
  2124. int source_vmlist[2] = {VMID_HLOS, smmu_domain->secure_vmid};
  2125. struct arm_smmu_pte_info *pte_info, *temp;
  2126. if (!arm_smmu_is_master_side_secure(smmu_domain))
  2127. return;
  2128. list_for_each_entry(pte_info, &smmu_domain->unassign_list, entry) {
  2129. ret = hyp_assign_phys(virt_to_phys(pte_info->virt_addr),
  2130. PAGE_SIZE, source_vmlist, 2,
  2131. &dest_vmids, &dest_perms, 1);
  2132. if (WARN_ON(ret))
  2133. break;
  2134. free_pages_exact(pte_info->virt_addr, pte_info->size);
  2135. }
  2136. list_for_each_entry_safe(pte_info, temp, &smmu_domain->unassign_list,
  2137. entry) {
  2138. list_del(&pte_info->entry);
  2139. kfree(pte_info);
  2140. }
  2141. }
  2142. static void arm_smmu_unprepare_pgtable(void *cookie, void *addr, size_t size)
  2143. {
  2144. struct arm_smmu_domain *smmu_domain = cookie;
  2145. struct arm_smmu_pte_info *pte_info;
  2146. if (smmu_domain->slave_side_secure ||
  2147. !arm_smmu_has_secure_vmid(smmu_domain)) {
  2148. if (smmu_domain->slave_side_secure)
  2149. WARN(1, "slave side secure is enforced\n");
  2150. else
  2151. WARN(1, "Invalid VMID is set !!\n");
  2152. return;
  2153. }
  2154. pte_info = kzalloc(sizeof(struct arm_smmu_pte_info), GFP_ATOMIC);
  2155. if (!pte_info)
  2156. return;
  2157. pte_info->virt_addr = addr;
  2158. pte_info->size = size;
  2159. list_add_tail(&pte_info->entry, &smmu_domain->unassign_list);
  2160. }
  2161. static int arm_smmu_prepare_pgtable(void *addr, void *cookie)
  2162. {
  2163. struct arm_smmu_domain *smmu_domain = cookie;
  2164. struct arm_smmu_pte_info *pte_info;
  2165. if (smmu_domain->slave_side_secure ||
  2166. !arm_smmu_has_secure_vmid(smmu_domain)) {
  2167. if (smmu_domain->slave_side_secure)
  2168. WARN(1, "slave side secure is enforced\n");
  2169. else
  2170. WARN(1, "Invalid VMID is set !!\n");
  2171. return -EINVAL;
  2172. }
  2173. pte_info = kzalloc(sizeof(struct arm_smmu_pte_info), GFP_ATOMIC);
  2174. if (!pte_info)
  2175. return -ENOMEM;
  2176. pte_info->virt_addr = addr;
  2177. list_add_tail(&pte_info->entry, &smmu_domain->pte_info_list);
  2178. return 0;
  2179. }
  2180. static void arm_smmu_prealloc_memory(struct arm_smmu_domain *smmu_domain,
  2181. size_t size, struct list_head *pool)
  2182. {
  2183. int i;
  2184. u32 nr = 0;
  2185. struct page *page;
  2186. if ((smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC)) ||
  2187. arm_smmu_has_secure_vmid(smmu_domain))
  2188. return;
  2189. /* number of 2nd level pagetable entries */
  2190. nr += round_up(size, SZ_1G) >> 30;
  2191. /* number of 3rd level pagetabel entries */
  2192. nr += round_up(size, SZ_2M) >> 21;
  2193. /* Retry later with atomic allocation on error */
  2194. for (i = 0; i < nr; i++) {
  2195. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
  2196. if (!page)
  2197. break;
  2198. list_add(&page->lru, pool);
  2199. }
  2200. }
  2201. static void arm_smmu_prealloc_memory_sg(struct arm_smmu_domain *smmu_domain,
  2202. struct scatterlist *sgl, int nents,
  2203. struct list_head *pool)
  2204. {
  2205. int i;
  2206. size_t size = 0;
  2207. struct scatterlist *sg;
  2208. if ((smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC)) ||
  2209. arm_smmu_has_secure_vmid(smmu_domain))
  2210. return;
  2211. for_each_sg(sgl, sg, nents, i)
  2212. size += sg->length;
  2213. arm_smmu_prealloc_memory(smmu_domain, size, pool);
  2214. }
  2215. static void arm_smmu_release_prealloc_memory(
  2216. struct arm_smmu_domain *smmu_domain, struct list_head *list)
  2217. {
  2218. struct page *page, *tmp;
  2219. list_for_each_entry_safe(page, tmp, list, lru) {
  2220. list_del(&page->lru);
  2221. __free_pages(page, 0);
  2222. }
  2223. }
  2224. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  2225. {
  2226. int ret;
  2227. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  2228. struct arm_smmu_device *smmu;
  2229. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2230. int atomic_domain = smmu_domain->attributes & (1 << DOMAIN_ATTR_ATOMIC);
  2231. if (!fwspec || fwspec->ops != &arm_smmu_ops) {
  2232. dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
  2233. return -ENXIO;
  2234. }
  2235. /*
  2236. * FIXME: The arch/arm DMA API code tries to attach devices to its own
  2237. * domains between of_xlate() and add_device() - we have no way to cope
  2238. * with that, so until ARM gets converted to rely on groups and default
  2239. * domains, just say no (but more politely than by dereferencing NULL).
  2240. * This should be at least a WARN_ON once that's sorted.
  2241. */
  2242. if (!fwspec->iommu_priv)
  2243. return -ENODEV;
  2244. smmu = fwspec_smmu(fwspec);
  2245. /* Enable Clocks and Power */
  2246. ret = arm_smmu_power_on(smmu->pwr);
  2247. if (ret)
  2248. return ret;
  2249. /* Ensure that the domain is finalised */
  2250. ret = arm_smmu_init_domain_context(domain, smmu, dev);
  2251. if (ret < 0)
  2252. goto out_power_off;
  2253. /* Do not modify the SIDs, HW is still running */
  2254. if (is_dynamic_domain(domain)) {
  2255. ret = 0;
  2256. goto out_power_off;
  2257. }
  2258. /*
  2259. * Sanity check the domain. We don't support domains across
  2260. * different SMMUs.
  2261. */
  2262. if (smmu_domain->smmu != smmu) {
  2263. dev_err(dev,
  2264. "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
  2265. dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
  2266. ret = -EINVAL;
  2267. goto out_power_off;
  2268. }
  2269. /* Looks ok, so add the device to the domain */
  2270. ret = arm_smmu_domain_add_master(smmu_domain, fwspec);
  2271. out_power_off:
  2272. /*
  2273. * Keep an additional vote for non-atomic power until domain is
  2274. * detached
  2275. */
  2276. if (!ret && atomic_domain) {
  2277. WARN_ON(arm_smmu_power_on(smmu->pwr));
  2278. arm_smmu_power_off_atomic(smmu->pwr);
  2279. }
  2280. arm_smmu_power_off(smmu->pwr);
  2281. return ret;
  2282. }
  2283. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  2284. phys_addr_t paddr, size_t size, int prot)
  2285. {
  2286. int ret;
  2287. unsigned long flags;
  2288. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2289. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  2290. LIST_HEAD(nonsecure_pool);
  2291. if (!ops)
  2292. return -ENODEV;
  2293. if (arm_smmu_is_slave_side_secure(smmu_domain))
  2294. return msm_secure_smmu_map(domain, iova, paddr, size, prot);
  2295. arm_smmu_prealloc_memory(smmu_domain, size, &nonsecure_pool);
  2296. arm_smmu_secure_domain_lock(smmu_domain);
  2297. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2298. list_splice_init(&nonsecure_pool, &smmu_domain->nonsecure_pool);
  2299. ret = ops->map(ops, iova, paddr, size, prot);
  2300. list_splice_init(&smmu_domain->nonsecure_pool, &nonsecure_pool);
  2301. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2302. arm_smmu_assign_table(smmu_domain);
  2303. arm_smmu_secure_domain_unlock(smmu_domain);
  2304. arm_smmu_release_prealloc_memory(smmu_domain, &nonsecure_pool);
  2305. return ret;
  2306. }
  2307. static uint64_t arm_smmu_iova_to_pte(struct iommu_domain *domain,
  2308. dma_addr_t iova)
  2309. {
  2310. uint64_t ret;
  2311. unsigned long flags;
  2312. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2313. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  2314. if (!ops)
  2315. return 0;
  2316. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2317. ret = ops->iova_to_pte(ops, iova);
  2318. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2319. return ret;
  2320. }
  2321. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  2322. size_t size)
  2323. {
  2324. size_t ret;
  2325. unsigned long flags;
  2326. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2327. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  2328. if (!ops)
  2329. return 0;
  2330. if (arm_smmu_is_slave_side_secure(smmu_domain))
  2331. return msm_secure_smmu_unmap(domain, iova, size);
  2332. ret = arm_smmu_domain_power_on(domain, smmu_domain->smmu);
  2333. if (ret)
  2334. return ret;
  2335. arm_smmu_secure_domain_lock(smmu_domain);
  2336. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2337. ret = ops->unmap(ops, iova, size);
  2338. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2339. arm_smmu_domain_power_off(domain, smmu_domain->smmu);
  2340. /*
  2341. * While splitting up block mappings, we might allocate page table
  2342. * memory during unmap, so the vmids needs to be assigned to the
  2343. * memory here as well.
  2344. */
  2345. arm_smmu_assign_table(smmu_domain);
  2346. /* Also unassign any pages that were free'd during unmap */
  2347. arm_smmu_unassign_table(smmu_domain);
  2348. arm_smmu_secure_domain_unlock(smmu_domain);
  2349. return ret;
  2350. }
  2351. #define MAX_MAP_SG_BATCH_SIZE (SZ_4M)
  2352. static size_t arm_smmu_map_sg(struct iommu_domain *domain, unsigned long iova,
  2353. struct scatterlist *sg, unsigned int nents, int prot)
  2354. {
  2355. int ret;
  2356. size_t size, batch_size, size_to_unmap = 0;
  2357. unsigned long flags;
  2358. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2359. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  2360. unsigned int idx_start, idx_end;
  2361. struct scatterlist *sg_start, *sg_end;
  2362. unsigned long __saved_iova_start;
  2363. LIST_HEAD(nonsecure_pool);
  2364. if (!ops)
  2365. return -ENODEV;
  2366. if (arm_smmu_is_slave_side_secure(smmu_domain))
  2367. return msm_secure_smmu_map_sg(domain, iova, sg, nents, prot);
  2368. arm_smmu_prealloc_memory_sg(smmu_domain, sg, nents, &nonsecure_pool);
  2369. arm_smmu_secure_domain_lock(smmu_domain);
  2370. __saved_iova_start = iova;
  2371. idx_start = idx_end = 0;
  2372. sg_start = sg_end = sg;
  2373. while (idx_end < nents) {
  2374. batch_size = sg_end->length;
  2375. sg_end = sg_next(sg_end);
  2376. idx_end++;
  2377. while ((idx_end < nents) &&
  2378. (batch_size + sg_end->length < MAX_MAP_SG_BATCH_SIZE)) {
  2379. batch_size += sg_end->length;
  2380. sg_end = sg_next(sg_end);
  2381. idx_end++;
  2382. }
  2383. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2384. list_splice_init(&nonsecure_pool, &smmu_domain->nonsecure_pool);
  2385. ret = ops->map_sg(ops, iova, sg_start, idx_end - idx_start,
  2386. prot, &size);
  2387. list_splice_init(&smmu_domain->nonsecure_pool, &nonsecure_pool);
  2388. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2389. /* Returns 0 on error */
  2390. if (!ret) {
  2391. size_to_unmap = iova + size - __saved_iova_start;
  2392. goto out;
  2393. }
  2394. iova += batch_size;
  2395. idx_start = idx_end;
  2396. sg_start = sg_end;
  2397. }
  2398. out:
  2399. arm_smmu_assign_table(smmu_domain);
  2400. if (size_to_unmap) {
  2401. arm_smmu_unmap(domain, __saved_iova_start, size_to_unmap);
  2402. iova = __saved_iova_start;
  2403. }
  2404. arm_smmu_secure_domain_unlock(smmu_domain);
  2405. arm_smmu_release_prealloc_memory(smmu_domain, &nonsecure_pool);
  2406. return iova - __saved_iova_start;
  2407. }
  2408. static phys_addr_t __arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  2409. dma_addr_t iova)
  2410. {
  2411. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2412. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2413. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  2414. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  2415. struct device *dev = smmu->dev;
  2416. void __iomem *cb_base;
  2417. u32 tmp;
  2418. u64 phys;
  2419. unsigned long va;
  2420. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  2421. /* ATS1 registers can only be written atomically */
  2422. va = iova & ~0xfffUL;
  2423. if (smmu->version == ARM_SMMU_V2)
  2424. smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
  2425. else /* Register is only 32-bit in v1 */
  2426. writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
  2427. if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
  2428. !(tmp & ATSR_ACTIVE), 5, 50)) {
  2429. phys = ops->iova_to_phys(ops, iova);
  2430. dev_err(dev,
  2431. "iova to phys timed out on %pad. software table walk result=%pa.\n",
  2432. &iova, &phys);
  2433. phys = 0;
  2434. return phys;
  2435. }
  2436. phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
  2437. if (phys & CB_PAR_F) {
  2438. dev_err(dev, "translation fault!\n");
  2439. dev_err(dev, "PAR = 0x%llx\n", phys);
  2440. phys = 0;
  2441. } else {
  2442. phys = (phys & (PHYS_MASK & ~0xfffULL)) | (iova & 0xfff);
  2443. }
  2444. return phys;
  2445. }
  2446. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  2447. dma_addr_t iova)
  2448. {
  2449. phys_addr_t ret;
  2450. unsigned long flags;
  2451. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2452. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  2453. if (!ops)
  2454. return 0;
  2455. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2456. ret = ops->iova_to_phys(ops, iova);
  2457. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2458. return ret;
  2459. }
  2460. /*
  2461. * This function can sleep, and cannot be called from atomic context. Will
  2462. * power on register block if required. This restriction does not apply to the
  2463. * original iova_to_phys() op.
  2464. */
  2465. static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  2466. dma_addr_t iova)
  2467. {
  2468. phys_addr_t ret = 0;
  2469. unsigned long flags;
  2470. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2471. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2472. if (smmu->options & ARM_SMMU_OPT_DISABLE_ATOS)
  2473. return 0;
  2474. if (smmu_domain->smmu->arch_ops &&
  2475. smmu_domain->smmu->arch_ops->iova_to_phys_hard) {
  2476. ret = smmu_domain->smmu->arch_ops->iova_to_phys_hard(
  2477. domain, iova);
  2478. return ret;
  2479. }
  2480. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  2481. if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
  2482. smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
  2483. ret = __arm_smmu_iova_to_phys_hard(domain, iova);
  2484. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  2485. return ret;
  2486. }
  2487. static bool arm_smmu_capable(enum iommu_cap cap)
  2488. {
  2489. switch (cap) {
  2490. case IOMMU_CAP_CACHE_COHERENCY:
  2491. /*
  2492. * Return true here as the SMMU can always send out coherent
  2493. * requests.
  2494. */
  2495. return true;
  2496. case IOMMU_CAP_INTR_REMAP:
  2497. return true; /* MSIs are just memory writes */
  2498. case IOMMU_CAP_NOEXEC:
  2499. return true;
  2500. default:
  2501. return false;
  2502. }
  2503. }
  2504. #ifdef CONFIG_MSM_TZ_SMMU
  2505. static struct arm_smmu_device *arm_smmu_get_by_addr(void __iomem *addr)
  2506. {
  2507. struct arm_smmu_device *smmu;
  2508. unsigned long flags;
  2509. spin_lock_irqsave(&arm_smmu_devices_lock, flags);
  2510. list_for_each_entry(smmu, &arm_smmu_devices, list) {
  2511. unsigned long base = (unsigned long)smmu->base;
  2512. unsigned long mask = ~(smmu->size - 1);
  2513. if ((base & mask) == ((unsigned long)addr & mask)) {
  2514. spin_unlock_irqrestore(&arm_smmu_devices_lock, flags);
  2515. return smmu;
  2516. }
  2517. }
  2518. spin_unlock_irqrestore(&arm_smmu_devices_lock, flags);
  2519. return NULL;
  2520. }
  2521. bool arm_smmu_skip_write(void __iomem *addr)
  2522. {
  2523. struct arm_smmu_device *smmu;
  2524. int cb;
  2525. smmu = arm_smmu_get_by_addr(addr);
  2526. /* Skip write if smmu not available by now */
  2527. if (!smmu)
  2528. return true;
  2529. if (!arm_smmu_is_static_cb(smmu))
  2530. return false;
  2531. /* Do not write to global space */
  2532. if (((unsigned long)addr & (smmu->size - 1)) < (smmu->size >> 1))
  2533. return true;
  2534. /* Finally skip writing to secure CB */
  2535. cb = ((unsigned long)addr & ((smmu->size >> 1) - 1)) >> PAGE_SHIFT;
  2536. if (test_bit(cb, smmu->secure_context_map))
  2537. return true;
  2538. return false;
  2539. }
  2540. static int msm_secure_smmu_map(struct iommu_domain *domain, unsigned long iova,
  2541. phys_addr_t paddr, size_t size, int prot)
  2542. {
  2543. size_t ret;
  2544. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2545. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  2546. ret = ops->map(ops, iova, paddr, size, prot);
  2547. return ret;
  2548. }
  2549. static size_t msm_secure_smmu_unmap(struct iommu_domain *domain,
  2550. unsigned long iova,
  2551. size_t size)
  2552. {
  2553. size_t ret;
  2554. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2555. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  2556. ret = arm_smmu_domain_power_on(domain, smmu_domain->smmu);
  2557. if (ret)
  2558. return ret;
  2559. ret = ops->unmap(ops, iova, size);
  2560. arm_smmu_domain_power_off(domain, smmu_domain->smmu);
  2561. return ret;
  2562. }
  2563. static size_t msm_secure_smmu_map_sg(struct iommu_domain *domain,
  2564. unsigned long iova,
  2565. struct scatterlist *sg,
  2566. unsigned int nents, int prot)
  2567. {
  2568. int ret;
  2569. size_t size;
  2570. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2571. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  2572. ret = ops->map_sg(ops, iova, sg, nents, prot, &size);
  2573. if (!ret)
  2574. msm_secure_smmu_unmap(domain, iova, size);
  2575. return ret;
  2576. }
  2577. #endif
  2578. static struct arm_smmu_device *arm_smmu_get_by_list(struct device_node *np)
  2579. {
  2580. struct arm_smmu_device *smmu;
  2581. unsigned long flags;
  2582. spin_lock_irqsave(&arm_smmu_devices_lock, flags);
  2583. list_for_each_entry(smmu, &arm_smmu_devices, list) {
  2584. if (smmu->dev->of_node == np) {
  2585. spin_unlock_irqrestore(&arm_smmu_devices_lock, flags);
  2586. return smmu;
  2587. }
  2588. }
  2589. spin_unlock_irqrestore(&arm_smmu_devices_lock, flags);
  2590. return NULL;
  2591. }
  2592. static int arm_smmu_match_node(struct device *dev, void *data)
  2593. {
  2594. return dev->of_node == data;
  2595. }
  2596. static struct arm_smmu_device *arm_smmu_get_by_node(struct device_node *np)
  2597. {
  2598. struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
  2599. np, arm_smmu_match_node);
  2600. put_device(dev);
  2601. return dev ? dev_get_drvdata(dev) : arm_smmu_get_by_list(np);
  2602. }
  2603. static int arm_smmu_add_device(struct device *dev)
  2604. {
  2605. struct arm_smmu_device *smmu;
  2606. struct arm_smmu_master_cfg *cfg;
  2607. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  2608. int i, ret;
  2609. if (using_legacy_binding) {
  2610. ret = arm_smmu_register_legacy_master(dev, &smmu);
  2611. fwspec = dev->iommu_fwspec;
  2612. if (ret)
  2613. goto out_free;
  2614. } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
  2615. smmu = arm_smmu_get_by_node(to_of_node(fwspec->iommu_fwnode));
  2616. if (!smmu)
  2617. return -ENODEV;
  2618. } else {
  2619. return -ENODEV;
  2620. }
  2621. ret = arm_smmu_power_on(smmu->pwr);
  2622. if (ret)
  2623. goto out_free;
  2624. ret = -EINVAL;
  2625. for (i = 0; i < fwspec->num_ids; i++) {
  2626. u16 sid = fwspec->ids[i];
  2627. u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
  2628. if (sid & ~smmu->streamid_mask) {
  2629. dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
  2630. sid, smmu->streamid_mask);
  2631. goto out_pwr_off;
  2632. }
  2633. if (mask & ~smmu->smr_mask_mask) {
  2634. dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
  2635. sid, smmu->smr_mask_mask);
  2636. goto out_pwr_off;
  2637. }
  2638. }
  2639. ret = -ENOMEM;
  2640. cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
  2641. GFP_KERNEL);
  2642. if (!cfg)
  2643. goto out_pwr_off;
  2644. cfg->smmu = smmu;
  2645. fwspec->iommu_priv = cfg;
  2646. while (i--)
  2647. cfg->smendx[i] = INVALID_SMENDX;
  2648. ret = arm_smmu_master_alloc_smes(dev);
  2649. if (ret)
  2650. goto out_pwr_off;
  2651. arm_smmu_power_off(smmu->pwr);
  2652. return 0;
  2653. out_pwr_off:
  2654. arm_smmu_power_off(smmu->pwr);
  2655. out_free:
  2656. if (fwspec)
  2657. kfree(fwspec->iommu_priv);
  2658. iommu_fwspec_free(dev);
  2659. return ret;
  2660. }
  2661. static void arm_smmu_remove_device(struct device *dev)
  2662. {
  2663. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  2664. struct arm_smmu_device *smmu;
  2665. if (!fwspec || fwspec->ops != &arm_smmu_ops)
  2666. return;
  2667. smmu = fwspec_smmu(fwspec);
  2668. if (arm_smmu_power_on(smmu->pwr)) {
  2669. WARN_ON(1);
  2670. return;
  2671. }
  2672. arm_smmu_master_free_smes(fwspec);
  2673. iommu_group_remove_device(dev);
  2674. kfree(fwspec->iommu_priv);
  2675. iommu_fwspec_free(dev);
  2676. arm_smmu_power_off(smmu->pwr);
  2677. }
  2678. static struct iommu_group *arm_smmu_device_group(struct device *dev)
  2679. {
  2680. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  2681. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  2682. struct iommu_group *group = NULL;
  2683. int i, idx;
  2684. for_each_cfg_sme(fwspec, i, idx) {
  2685. if (group && smmu->s2crs[idx].group &&
  2686. group != smmu->s2crs[idx].group)
  2687. return ERR_PTR(-EINVAL);
  2688. group = smmu->s2crs[idx].group;
  2689. }
  2690. if (!group) {
  2691. if (dev_is_pci(dev))
  2692. group = pci_device_group(dev);
  2693. else
  2694. group = generic_device_group(dev);
  2695. if (IS_ERR(group))
  2696. return NULL;
  2697. }
  2698. if (arm_smmu_arch_device_group(dev, group)) {
  2699. iommu_group_put(group);
  2700. return ERR_PTR(-EINVAL);
  2701. }
  2702. return group;
  2703. }
  2704. static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
  2705. enum iommu_attr attr, void *data)
  2706. {
  2707. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2708. int ret = 0;
  2709. mutex_lock(&smmu_domain->init_mutex);
  2710. switch (attr) {
  2711. case DOMAIN_ATTR_NESTING:
  2712. *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
  2713. ret = 0;
  2714. break;
  2715. case DOMAIN_ATTR_PT_BASE_ADDR:
  2716. *((phys_addr_t *)data) =
  2717. smmu_domain->pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0];
  2718. ret = 0;
  2719. break;
  2720. case DOMAIN_ATTR_CONTEXT_BANK:
  2721. /* context bank index isn't valid until we are attached */
  2722. if (smmu_domain->smmu == NULL) {
  2723. ret = -ENODEV;
  2724. break;
  2725. }
  2726. *((unsigned int *) data) = smmu_domain->cfg.cbndx;
  2727. ret = 0;
  2728. break;
  2729. case DOMAIN_ATTR_TTBR0: {
  2730. u64 val;
  2731. struct arm_smmu_device *smmu = smmu_domain->smmu;
  2732. /* not valid until we are attached */
  2733. if (smmu == NULL) {
  2734. ret = -ENODEV;
  2735. break;
  2736. }
  2737. val = smmu_domain->pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0];
  2738. if (smmu_domain->cfg.cbar != CBAR_TYPE_S2_TRANS)
  2739. val |= (u64)ARM_SMMU_CB_ASID(smmu, &smmu_domain->cfg)
  2740. << (TTBRn_ASID_SHIFT);
  2741. *((u64 *)data) = val;
  2742. ret = 0;
  2743. break;
  2744. }
  2745. case DOMAIN_ATTR_CONTEXTIDR:
  2746. /* not valid until attached */
  2747. if (smmu_domain->smmu == NULL) {
  2748. ret = -ENODEV;
  2749. break;
  2750. }
  2751. *((u32 *)data) = smmu_domain->cfg.procid;
  2752. ret = 0;
  2753. break;
  2754. case DOMAIN_ATTR_PROCID:
  2755. *((u32 *)data) = smmu_domain->cfg.procid;
  2756. ret = 0;
  2757. break;
  2758. case DOMAIN_ATTR_DYNAMIC:
  2759. *((int *)data) = !!(smmu_domain->attributes
  2760. & (1 << DOMAIN_ATTR_DYNAMIC));
  2761. ret = 0;
  2762. break;
  2763. case DOMAIN_ATTR_NON_FATAL_FAULTS:
  2764. *((int *)data) = !!(smmu_domain->attributes
  2765. & (1 << DOMAIN_ATTR_NON_FATAL_FAULTS));
  2766. ret = 0;
  2767. break;
  2768. case DOMAIN_ATTR_S1_BYPASS:
  2769. *((int *)data) = !!(smmu_domain->attributes
  2770. & (1 << DOMAIN_ATTR_S1_BYPASS));
  2771. ret = 0;
  2772. break;
  2773. case DOMAIN_ATTR_SECURE_VMID:
  2774. *((int *)data) = smmu_domain->secure_vmid;
  2775. ret = 0;
  2776. break;
  2777. case DOMAIN_ATTR_PGTBL_INFO: {
  2778. struct iommu_pgtbl_info *info = data;
  2779. if (!(smmu_domain->attributes & (1 << DOMAIN_ATTR_FAST))) {
  2780. ret = -ENODEV;
  2781. break;
  2782. }
  2783. info->pmds = smmu_domain->pgtbl_cfg.av8l_fast_cfg.pmds;
  2784. ret = 0;
  2785. break;
  2786. }
  2787. case DOMAIN_ATTR_FAST:
  2788. *((int *)data) = !!(smmu_domain->attributes
  2789. & (1 << DOMAIN_ATTR_FAST));
  2790. ret = 0;
  2791. break;
  2792. case DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR:
  2793. *((int *)data) = !!(smmu_domain->attributes
  2794. & (1 << DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR));
  2795. ret = 0;
  2796. break;
  2797. case DOMAIN_ATTR_USE_UPSTREAM_HINT:
  2798. *((int *)data) = !!(smmu_domain->attributes &
  2799. (1 << DOMAIN_ATTR_USE_UPSTREAM_HINT));
  2800. ret = 0;
  2801. break;
  2802. case DOMAIN_ATTR_EARLY_MAP:
  2803. *((int *)data) = !!(smmu_domain->attributes
  2804. & (1 << DOMAIN_ATTR_EARLY_MAP));
  2805. ret = 0;
  2806. break;
  2807. case DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT:
  2808. if (!smmu_domain->smmu) {
  2809. ret = -ENODEV;
  2810. break;
  2811. }
  2812. *((int *)data) = is_iommu_pt_coherent(smmu_domain);
  2813. ret = 0;
  2814. break;
  2815. case DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT:
  2816. *((int *)data) = !!(smmu_domain->attributes
  2817. & (1 << DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT));
  2818. ret = 0;
  2819. break;
  2820. case DOMAIN_ATTR_CB_STALL_DISABLE:
  2821. *((int *)data) = !!(smmu_domain->attributes
  2822. & (1 << DOMAIN_ATTR_CB_STALL_DISABLE));
  2823. ret = 0;
  2824. break;
  2825. case DOMAIN_ATTR_MMU500_ERRATA_MIN_ALIGN:
  2826. *((int *)data) = smmu_domain->qsmmuv500_errata2_min_align;
  2827. ret = 0;
  2828. break;
  2829. case DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE:
  2830. *((int *)data) = !!(smmu_domain->attributes
  2831. & (1 << DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE));
  2832. ret = 0;
  2833. break;
  2834. default:
  2835. ret = -ENODEV;
  2836. break;
  2837. }
  2838. mutex_unlock(&smmu_domain->init_mutex);
  2839. return ret;
  2840. }
  2841. static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
  2842. enum iommu_attr attr, void *data)
  2843. {
  2844. int ret = 0;
  2845. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  2846. mutex_lock(&smmu_domain->init_mutex);
  2847. switch (attr) {
  2848. case DOMAIN_ATTR_NESTING:
  2849. if (smmu_domain->smmu) {
  2850. ret = -EPERM;
  2851. goto out_unlock;
  2852. }
  2853. if (*(int *)data)
  2854. smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
  2855. else
  2856. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  2857. break;
  2858. case DOMAIN_ATTR_PROCID:
  2859. if (smmu_domain->smmu != NULL) {
  2860. dev_err(smmu_domain->smmu->dev,
  2861. "cannot change procid attribute while attached\n");
  2862. ret = -EBUSY;
  2863. break;
  2864. }
  2865. smmu_domain->cfg.procid = *((u32 *)data);
  2866. ret = 0;
  2867. break;
  2868. case DOMAIN_ATTR_DYNAMIC: {
  2869. int dynamic = *((int *)data);
  2870. if (smmu_domain->smmu != NULL) {
  2871. dev_err(smmu_domain->smmu->dev,
  2872. "cannot change dynamic attribute while attached\n");
  2873. ret = -EBUSY;
  2874. break;
  2875. }
  2876. if (dynamic)
  2877. smmu_domain->attributes |= 1 << DOMAIN_ATTR_DYNAMIC;
  2878. else
  2879. smmu_domain->attributes &= ~(1 << DOMAIN_ATTR_DYNAMIC);
  2880. ret = 0;
  2881. break;
  2882. }
  2883. case DOMAIN_ATTR_CONTEXT_BANK:
  2884. /* context bank can't be set while attached */
  2885. if (smmu_domain->smmu != NULL) {
  2886. ret = -EBUSY;
  2887. break;
  2888. }
  2889. /* ... and it can only be set for dynamic contexts. */
  2890. if (!(smmu_domain->attributes & (1 << DOMAIN_ATTR_DYNAMIC))) {
  2891. ret = -EINVAL;
  2892. break;
  2893. }
  2894. /* this will be validated during attach */
  2895. smmu_domain->cfg.cbndx = *((unsigned int *)data);
  2896. ret = 0;
  2897. break;
  2898. case DOMAIN_ATTR_NON_FATAL_FAULTS: {
  2899. u32 non_fatal_faults = *((int *)data);
  2900. if (non_fatal_faults)
  2901. smmu_domain->attributes |=
  2902. 1 << DOMAIN_ATTR_NON_FATAL_FAULTS;
  2903. else
  2904. smmu_domain->attributes &=
  2905. ~(1 << DOMAIN_ATTR_NON_FATAL_FAULTS);
  2906. ret = 0;
  2907. break;
  2908. }
  2909. case DOMAIN_ATTR_S1_BYPASS: {
  2910. int bypass = *((int *)data);
  2911. /* bypass can't be changed while attached */
  2912. if (smmu_domain->smmu != NULL) {
  2913. ret = -EBUSY;
  2914. break;
  2915. }
  2916. if (bypass)
  2917. smmu_domain->attributes |= 1 << DOMAIN_ATTR_S1_BYPASS;
  2918. else
  2919. smmu_domain->attributes &=
  2920. ~(1 << DOMAIN_ATTR_S1_BYPASS);
  2921. ret = 0;
  2922. break;
  2923. }
  2924. case DOMAIN_ATTR_ATOMIC:
  2925. {
  2926. int atomic_ctx = *((int *)data);
  2927. /* can't be changed while attached */
  2928. if (smmu_domain->smmu != NULL) {
  2929. ret = -EBUSY;
  2930. break;
  2931. }
  2932. if (atomic_ctx)
  2933. smmu_domain->attributes |= (1 << DOMAIN_ATTR_ATOMIC);
  2934. else
  2935. smmu_domain->attributes &= ~(1 << DOMAIN_ATTR_ATOMIC);
  2936. break;
  2937. }
  2938. case DOMAIN_ATTR_SECURE_VMID:
  2939. if (smmu_domain->secure_vmid != VMID_INVAL) {
  2940. ret = -ENODEV;
  2941. WARN(1, "secure vmid already set!");
  2942. break;
  2943. }
  2944. smmu_domain->secure_vmid = *((int *)data);
  2945. break;
  2946. case DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR:
  2947. if (*((int *)data))
  2948. smmu_domain->attributes |=
  2949. 1 << DOMAIN_ATTR_UPSTREAM_IOVA_ALLOCATOR;
  2950. ret = 0;
  2951. break;
  2952. /*
  2953. * fast_smmu_unmap_page() and fast_smmu_alloc_iova() both
  2954. * expect that the bus/clock/regulator are already on. Thus also
  2955. * force DOMAIN_ATTR_ATOMIC to bet set.
  2956. */
  2957. case DOMAIN_ATTR_FAST:
  2958. {
  2959. int fast = *((int *)data);
  2960. if (fast) {
  2961. smmu_domain->attributes |= 1 << DOMAIN_ATTR_FAST;
  2962. smmu_domain->attributes |= 1 << DOMAIN_ATTR_ATOMIC;
  2963. }
  2964. ret = 0;
  2965. break;
  2966. }
  2967. case DOMAIN_ATTR_USE_UPSTREAM_HINT:
  2968. /* can't be changed while attached */
  2969. if (smmu_domain->smmu != NULL) {
  2970. ret = -EBUSY;
  2971. break;
  2972. }
  2973. if (*((int *)data))
  2974. smmu_domain->attributes |=
  2975. 1 << DOMAIN_ATTR_USE_UPSTREAM_HINT;
  2976. ret = 0;
  2977. break;
  2978. case DOMAIN_ATTR_EARLY_MAP: {
  2979. int early_map = *((int *)data);
  2980. ret = 0;
  2981. if (early_map) {
  2982. smmu_domain->attributes |=
  2983. 1 << DOMAIN_ATTR_EARLY_MAP;
  2984. } else {
  2985. if (smmu_domain->smmu)
  2986. ret = arm_smmu_enable_s1_translations(
  2987. smmu_domain);
  2988. if (!ret)
  2989. smmu_domain->attributes &=
  2990. ~(1 << DOMAIN_ATTR_EARLY_MAP);
  2991. }
  2992. break;
  2993. }
  2994. case DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT: {
  2995. int force_coherent = *((int *)data);
  2996. if (smmu_domain->smmu != NULL) {
  2997. dev_err(smmu_domain->smmu->dev,
  2998. "cannot change force coherent attribute while attached\n");
  2999. ret = -EBUSY;
  3000. break;
  3001. }
  3002. if (force_coherent)
  3003. smmu_domain->attributes |=
  3004. 1 << DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT;
  3005. else
  3006. smmu_domain->attributes &=
  3007. ~(1 << DOMAIN_ATTR_PAGE_TABLE_FORCE_COHERENT);
  3008. ret = 0;
  3009. break;
  3010. }
  3011. case DOMAIN_ATTR_CB_STALL_DISABLE:
  3012. if (*((int *)data))
  3013. smmu_domain->attributes |=
  3014. 1 << DOMAIN_ATTR_CB_STALL_DISABLE;
  3015. ret = 0;
  3016. break;
  3017. case DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE: {
  3018. int force_iova_guard_page = *((int *)data);
  3019. if (smmu_domain->smmu != NULL) {
  3020. dev_err(smmu_domain->smmu->dev,
  3021. "cannot change force guard page attribute while attached\n");
  3022. ret = -EBUSY;
  3023. break;
  3024. }
  3025. if (force_iova_guard_page)
  3026. smmu_domain->attributes |=
  3027. 1 << DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE;
  3028. else
  3029. smmu_domain->attributes &=
  3030. ~(1 << DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE);
  3031. ret = 0;
  3032. break;
  3033. }
  3034. default:
  3035. ret = -ENODEV;
  3036. }
  3037. out_unlock:
  3038. mutex_unlock(&smmu_domain->init_mutex);
  3039. return ret;
  3040. }
  3041. static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
  3042. {
  3043. u32 fwid = 0;
  3044. if (args->args_count > 0)
  3045. fwid |= (u16)args->args[0];
  3046. if (args->args_count > 1)
  3047. fwid |= (u16)args->args[1] << SMR_MASK_SHIFT;
  3048. return iommu_fwspec_add_ids(dev, &fwid, 1);
  3049. }
  3050. static int arm_smmu_enable_s1_translations(struct arm_smmu_domain *smmu_domain)
  3051. {
  3052. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  3053. struct arm_smmu_device *smmu = smmu_domain->smmu;
  3054. struct arm_smmu_cb *cb = &smmu->cbs[cfg->cbndx];
  3055. int ret;
  3056. ret = arm_smmu_power_on(smmu->pwr);
  3057. if (ret)
  3058. return ret;
  3059. cb->attributes &= ~(1 << DOMAIN_ATTR_EARLY_MAP);
  3060. arm_smmu_write_context_bank(smmu, cfg->cbndx);
  3061. arm_smmu_power_off(smmu->pwr);
  3062. return ret;
  3063. }
  3064. static bool arm_smmu_is_iova_coherent(struct iommu_domain *domain,
  3065. dma_addr_t iova)
  3066. {
  3067. bool ret;
  3068. unsigned long flags;
  3069. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3070. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  3071. if (!ops)
  3072. return false;
  3073. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  3074. ret = ops->is_iova_coherent(ops, iova);
  3075. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  3076. return ret;
  3077. }
  3078. static void arm_smmu_trigger_fault(struct iommu_domain *domain,
  3079. unsigned long flags)
  3080. {
  3081. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3082. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  3083. struct arm_smmu_device *smmu;
  3084. void __iomem *cb_base;
  3085. if (!smmu_domain->smmu) {
  3086. pr_err("Can't trigger faults on non-attached domains\n");
  3087. return;
  3088. }
  3089. smmu = smmu_domain->smmu;
  3090. if (arm_smmu_power_on(smmu->pwr))
  3091. return;
  3092. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  3093. dev_err(smmu->dev, "Writing 0x%lx to FSRRESTORE on cb %d\n",
  3094. flags, cfg->cbndx);
  3095. writel_relaxed(flags, cb_base + ARM_SMMU_CB_FSRRESTORE);
  3096. /* give the interrupt time to fire... */
  3097. msleep(1000);
  3098. arm_smmu_power_off(smmu->pwr);
  3099. }
  3100. static void arm_smmu_tlbi_domain(struct iommu_domain *domain)
  3101. {
  3102. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3103. const struct iommu_gather_ops *tlb = smmu_domain->pgtbl_cfg.tlb;
  3104. tlb->tlb_flush_all(smmu_domain);
  3105. }
  3106. static int arm_smmu_enable_config_clocks(struct iommu_domain *domain)
  3107. {
  3108. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3109. return arm_smmu_power_on(smmu_domain->smmu->pwr);
  3110. }
  3111. static void arm_smmu_disable_config_clocks(struct iommu_domain *domain)
  3112. {
  3113. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3114. arm_smmu_power_off(smmu_domain->smmu->pwr);
  3115. }
  3116. static struct iommu_ops arm_smmu_ops = {
  3117. .capable = arm_smmu_capable,
  3118. .domain_alloc = arm_smmu_domain_alloc,
  3119. .domain_free = arm_smmu_domain_free,
  3120. .attach_dev = arm_smmu_attach_dev,
  3121. .detach_dev = arm_smmu_detach_dev,
  3122. .map = arm_smmu_map,
  3123. .unmap = arm_smmu_unmap,
  3124. .map_sg = arm_smmu_map_sg,
  3125. .iova_to_phys = arm_smmu_iova_to_phys,
  3126. .iova_to_phys_hard = arm_smmu_iova_to_phys_hard,
  3127. .add_device = arm_smmu_add_device,
  3128. .remove_device = arm_smmu_remove_device,
  3129. .device_group = arm_smmu_device_group,
  3130. .domain_get_attr = arm_smmu_domain_get_attr,
  3131. .domain_set_attr = arm_smmu_domain_set_attr,
  3132. .of_xlate = arm_smmu_of_xlate,
  3133. .pgsize_bitmap = -1UL, /* Restricted during device attach */
  3134. .trigger_fault = arm_smmu_trigger_fault,
  3135. .tlbi_domain = arm_smmu_tlbi_domain,
  3136. .enable_config_clocks = arm_smmu_enable_config_clocks,
  3137. .disable_config_clocks = arm_smmu_disable_config_clocks,
  3138. .is_iova_coherent = arm_smmu_is_iova_coherent,
  3139. .iova_to_pte = arm_smmu_iova_to_pte,
  3140. };
  3141. #define IMPL_DEF1_MICRO_MMU_CTRL 0
  3142. #define MICRO_MMU_CTRL_LOCAL_HALT_REQ (1 << 2)
  3143. #define MICRO_MMU_CTRL_IDLE (1 << 3)
  3144. /* Definitions for implementation-defined registers */
  3145. #define ACTLR_QCOM_OSH_SHIFT 28
  3146. #define ACTLR_QCOM_OSH 1
  3147. #define ACTLR_QCOM_ISH_SHIFT 29
  3148. #define ACTLR_QCOM_ISH 1
  3149. #define ACTLR_QCOM_NSH_SHIFT 30
  3150. #define ACTLR_QCOM_NSH 1
  3151. static int qsmmuv2_wait_for_halt(struct arm_smmu_device *smmu)
  3152. {
  3153. void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu);
  3154. u32 tmp;
  3155. if (readl_poll_timeout_atomic(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL,
  3156. tmp, (tmp & MICRO_MMU_CTRL_IDLE),
  3157. 0, 30000)) {
  3158. dev_err(smmu->dev, "Couldn't halt SMMU!\n");
  3159. return -EBUSY;
  3160. }
  3161. return 0;
  3162. }
  3163. static int __qsmmuv2_halt(struct arm_smmu_device *smmu, bool wait)
  3164. {
  3165. void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu);
  3166. u32 reg;
  3167. reg = readl_relaxed(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL);
  3168. reg |= MICRO_MMU_CTRL_LOCAL_HALT_REQ;
  3169. if (arm_smmu_is_static_cb(smmu)) {
  3170. phys_addr_t impl_def1_base_phys = impl_def1_base - smmu->base +
  3171. smmu->phys_addr;
  3172. if (scm_io_write(impl_def1_base_phys +
  3173. IMPL_DEF1_MICRO_MMU_CTRL, reg)) {
  3174. dev_err(smmu->dev,
  3175. "scm_io_write fail. SMMU might not be halted");
  3176. return -EINVAL;
  3177. }
  3178. } else {
  3179. writel_relaxed(reg, impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL);
  3180. }
  3181. return wait ? qsmmuv2_wait_for_halt(smmu) : 0;
  3182. }
  3183. static int qsmmuv2_halt(struct arm_smmu_device *smmu)
  3184. {
  3185. return __qsmmuv2_halt(smmu, true);
  3186. }
  3187. static int qsmmuv2_halt_nowait(struct arm_smmu_device *smmu)
  3188. {
  3189. return __qsmmuv2_halt(smmu, false);
  3190. }
  3191. static void qsmmuv2_resume(struct arm_smmu_device *smmu)
  3192. {
  3193. void __iomem *impl_def1_base = ARM_SMMU_IMPL_DEF1(smmu);
  3194. u32 reg;
  3195. reg = readl_relaxed(impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL);
  3196. reg &= ~MICRO_MMU_CTRL_LOCAL_HALT_REQ;
  3197. if (arm_smmu_is_static_cb(smmu)) {
  3198. phys_addr_t impl_def1_base_phys = impl_def1_base - smmu->base +
  3199. smmu->phys_addr;
  3200. if (scm_io_write(impl_def1_base_phys +
  3201. IMPL_DEF1_MICRO_MMU_CTRL, reg))
  3202. dev_err(smmu->dev,
  3203. "scm_io_write fail. SMMU might not be resumed");
  3204. } else {
  3205. writel_relaxed(reg, impl_def1_base + IMPL_DEF1_MICRO_MMU_CTRL);
  3206. }
  3207. }
  3208. static void qsmmuv2_device_reset(struct arm_smmu_device *smmu)
  3209. {
  3210. int i;
  3211. u32 val;
  3212. struct arm_smmu_impl_def_reg *regs = smmu->impl_def_attach_registers;
  3213. /*
  3214. * SCTLR.M must be disabled here per ARM SMMUv2 spec
  3215. * to prevent table walks with an inconsistent state.
  3216. */
  3217. for (i = 0; i < smmu->num_context_banks; ++i) {
  3218. struct arm_smmu_cb *cb = &smmu->cbs[i];
  3219. val = ACTLR_QCOM_ISH << ACTLR_QCOM_ISH_SHIFT |
  3220. ACTLR_QCOM_OSH << ACTLR_QCOM_OSH_SHIFT |
  3221. ACTLR_QCOM_NSH << ACTLR_QCOM_NSH_SHIFT;
  3222. cb->actlr = val;
  3223. }
  3224. /* Program implementation defined registers */
  3225. qsmmuv2_halt(smmu);
  3226. for (i = 0; i < smmu->num_impl_def_attach_registers; ++i)
  3227. writel_relaxed(regs[i].value,
  3228. ARM_SMMU_GR0(smmu) + regs[i].offset);
  3229. qsmmuv2_resume(smmu);
  3230. }
  3231. static phys_addr_t qsmmuv2_iova_to_phys_hard(struct iommu_domain *domain,
  3232. dma_addr_t iova)
  3233. {
  3234. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3235. struct arm_smmu_device *smmu = smmu_domain->smmu;
  3236. int ret;
  3237. phys_addr_t phys = 0;
  3238. unsigned long flags;
  3239. u32 sctlr, sctlr_orig, fsr;
  3240. void __iomem *cb_base;
  3241. ret = arm_smmu_power_on(smmu_domain->smmu->pwr);
  3242. if (ret)
  3243. return ret;
  3244. spin_lock_irqsave(&smmu->atos_lock, flags);
  3245. cb_base = ARM_SMMU_CB_BASE(smmu) +
  3246. ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx);
  3247. qsmmuv2_halt_nowait(smmu);
  3248. writel_relaxed(RESUME_TERMINATE, cb_base + ARM_SMMU_CB_RESUME);
  3249. qsmmuv2_wait_for_halt(smmu);
  3250. /* clear FSR to allow ATOS to log any faults */
  3251. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  3252. writel_relaxed(fsr, cb_base + ARM_SMMU_CB_FSR);
  3253. /* disable stall mode momentarily */
  3254. sctlr_orig = readl_relaxed(cb_base + ARM_SMMU_CB_SCTLR);
  3255. sctlr = sctlr_orig & ~SCTLR_CFCFG;
  3256. writel_relaxed(sctlr, cb_base + ARM_SMMU_CB_SCTLR);
  3257. phys = __arm_smmu_iova_to_phys_hard(domain, iova);
  3258. /* restore SCTLR */
  3259. writel_relaxed(sctlr_orig, cb_base + ARM_SMMU_CB_SCTLR);
  3260. qsmmuv2_resume(smmu);
  3261. spin_unlock_irqrestore(&smmu->atos_lock, flags);
  3262. arm_smmu_power_off(smmu_domain->smmu->pwr);
  3263. return phys;
  3264. }
  3265. struct arm_smmu_arch_ops qsmmuv2_arch_ops = {
  3266. .device_reset = qsmmuv2_device_reset,
  3267. .iova_to_phys_hard = qsmmuv2_iova_to_phys_hard,
  3268. };
  3269. static void arm_smmu_context_bank_reset(struct arm_smmu_device *smmu)
  3270. {
  3271. int i;
  3272. u32 reg, major;
  3273. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  3274. /*
  3275. * Before clearing ARM_MMU500_ACTLR_CPRE, need to
  3276. * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
  3277. * bit is only present in MMU-500r2 onwards.
  3278. */
  3279. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
  3280. major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
  3281. if ((smmu->model == ARM_MMU500) && (major >= 2)) {
  3282. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
  3283. reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
  3284. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
  3285. }
  3286. /* Make sure all context banks are disabled and clear CB_FSR */
  3287. for (i = 0; i < smmu->num_context_banks; ++i) {
  3288. void __iomem *cb_base = ARM_SMMU_CB_BASE(smmu) +
  3289. ARM_SMMU_CB(smmu, i);
  3290. arm_smmu_write_context_bank(smmu, i);
  3291. writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
  3292. /*
  3293. * Disable MMU-500's not-particularly-beneficial next-page
  3294. * prefetcher for the sake of errata #841119 and #826419.
  3295. */
  3296. if (smmu->model == ARM_MMU500) {
  3297. reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
  3298. reg &= ~ARM_MMU500_ACTLR_CPRE;
  3299. writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
  3300. }
  3301. }
  3302. }
  3303. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  3304. {
  3305. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  3306. int i;
  3307. u32 reg;
  3308. /* clear global FSR */
  3309. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  3310. writel_relaxed(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  3311. /*
  3312. * Reset stream mapping groups: Initial values mark all SMRn as
  3313. * invalid and all S2CRn as bypass unless overridden.
  3314. */
  3315. if (!(smmu->options & ARM_SMMU_OPT_SKIP_INIT)) {
  3316. for (i = 0; i < smmu->num_mapping_groups; ++i)
  3317. arm_smmu_write_sme(smmu, i);
  3318. arm_smmu_context_bank_reset(smmu);
  3319. }
  3320. /* Invalidate the TLB, just in case */
  3321. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  3322. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  3323. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  3324. /* Enable fault reporting */
  3325. reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  3326. /* Disable TLB broadcasting. */
  3327. reg |= (sCR0_VMIDPNE | sCR0_PTM);
  3328. /* Enable client access, handling unmatched streams as appropriate */
  3329. reg &= ~sCR0_CLIENTPD;
  3330. if (disable_bypass)
  3331. reg |= sCR0_USFCFG;
  3332. else
  3333. reg &= ~sCR0_USFCFG;
  3334. /* Disable forced broadcasting */
  3335. reg &= ~sCR0_FB;
  3336. /* Don't upgrade barriers */
  3337. reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  3338. if (smmu->features & ARM_SMMU_FEAT_VMID16)
  3339. reg |= sCR0_VMID16EN;
  3340. /* Force bypass transaction to be Non-Shareable & not io-coherent */
  3341. reg &= ~(sCR0_SHCFG_MASK << sCR0_SHCFG_SHIFT);
  3342. reg |= sCR0_SHCFG_NSH << sCR0_SHCFG_SHIFT;
  3343. /* Push the button */
  3344. __arm_smmu_tlb_sync(smmu);
  3345. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  3346. /* Manage any implementation defined features */
  3347. arm_smmu_arch_device_reset(smmu);
  3348. }
  3349. static int arm_smmu_id_size_to_bits(int size)
  3350. {
  3351. switch (size) {
  3352. case 0:
  3353. return 32;
  3354. case 1:
  3355. return 36;
  3356. case 2:
  3357. return 40;
  3358. case 3:
  3359. return 42;
  3360. case 4:
  3361. return 44;
  3362. case 5:
  3363. default:
  3364. return 48;
  3365. }
  3366. }
  3367. /*
  3368. * Some context banks needs to be transferred from bootloader to HLOS in a way
  3369. * that allows ongoing traffic. The current expectation is that these context
  3370. * banks operate in bypass mode.
  3371. * Additionally, there must be exactly one device in devicetree with stream-ids
  3372. * overlapping those used by the bootloader.
  3373. */
  3374. static int arm_smmu_alloc_cb(struct iommu_domain *domain,
  3375. struct arm_smmu_device *smmu,
  3376. struct device *dev)
  3377. {
  3378. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  3379. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  3380. u32 i, idx;
  3381. int cb = -EINVAL;
  3382. bool dynamic;
  3383. /*
  3384. * Dynamic domains have already set cbndx through domain attribute.
  3385. * Verify that they picked a valid value.
  3386. */
  3387. dynamic = is_dynamic_domain(domain);
  3388. if (dynamic) {
  3389. cb = smmu_domain->cfg.cbndx;
  3390. if (cb < smmu->num_context_banks)
  3391. return cb;
  3392. else
  3393. return -EINVAL;
  3394. }
  3395. mutex_lock(&smmu->stream_map_mutex);
  3396. for_each_cfg_sme(fwspec, i, idx) {
  3397. if (smmu->s2crs[idx].cb_handoff)
  3398. cb = smmu->s2crs[idx].cbndx;
  3399. }
  3400. if (cb >= 0 && arm_smmu_is_static_cb(smmu)) {
  3401. smmu_domain->slave_side_secure = true;
  3402. if (arm_smmu_is_slave_side_secure(smmu_domain))
  3403. bitmap_set(smmu->secure_context_map, cb, 1);
  3404. }
  3405. if (cb < 0 && !arm_smmu_is_static_cb(smmu)) {
  3406. mutex_unlock(&smmu->stream_map_mutex);
  3407. return __arm_smmu_alloc_bitmap(smmu->context_map,
  3408. smmu->num_s2_context_banks,
  3409. smmu->num_context_banks);
  3410. }
  3411. for (i = 0; i < smmu->num_mapping_groups; i++) {
  3412. if (smmu->s2crs[i].cb_handoff && smmu->s2crs[i].cbndx == cb) {
  3413. if (!arm_smmu_is_static_cb(smmu))
  3414. smmu->s2crs[i].cb_handoff = false;
  3415. smmu->s2crs[i].count -= 1;
  3416. }
  3417. }
  3418. mutex_unlock(&smmu->stream_map_mutex);
  3419. return cb;
  3420. }
  3421. static void parse_static_cb_cfg(struct arm_smmu_device *smmu)
  3422. {
  3423. u32 idx = 0;
  3424. u32 val;
  3425. int ret;
  3426. if (!(arm_smmu_is_static_cb(smmu) &&
  3427. arm_smmu_opt_hibernation(smmu)))
  3428. return;
  3429. /*
  3430. * Context banks may be xpu-protected. Require a devicetree property to
  3431. * indicate which context banks HLOS has access to.
  3432. */
  3433. bitmap_set(smmu->secure_context_map, 0, ARM_SMMU_MAX_CBS);
  3434. while (idx < ARM_SMMU_MAX_CBS) {
  3435. ret = of_property_read_u32_index(
  3436. smmu->dev->of_node, "qcom,static-ns-cbs",
  3437. idx++, &val);
  3438. if (ret)
  3439. break;
  3440. bitmap_clear(smmu->secure_context_map, val, 1);
  3441. dev_dbg(smmu->dev, "Detected NS context bank: %d\n", idx);
  3442. }
  3443. }
  3444. static int arm_smmu_handoff_cbs(struct arm_smmu_device *smmu)
  3445. {
  3446. u32 i, raw_smr, raw_s2cr;
  3447. struct arm_smmu_smr smr;
  3448. struct arm_smmu_s2cr s2cr;
  3449. for (i = 0; i < smmu->num_mapping_groups; i++) {
  3450. raw_smr = readl_relaxed(ARM_SMMU_GR0(smmu) +
  3451. ARM_SMMU_GR0_SMR(i));
  3452. if (!(raw_smr & SMR_VALID))
  3453. continue;
  3454. smr.mask = (raw_smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
  3455. smr.id = (u16)raw_smr;
  3456. smr.valid = true;
  3457. raw_s2cr = readl_relaxed(ARM_SMMU_GR0(smmu) +
  3458. ARM_SMMU_GR0_S2CR(i));
  3459. memset(&s2cr, 0, sizeof(s2cr));
  3460. s2cr.group = NULL;
  3461. s2cr.count = 1;
  3462. s2cr.type = (raw_s2cr >> S2CR_TYPE_SHIFT) & S2CR_TYPE_MASK;
  3463. s2cr.privcfg = (raw_s2cr >> S2CR_PRIVCFG_SHIFT) &
  3464. S2CR_PRIVCFG_MASK;
  3465. s2cr.cbndx = (u8)raw_s2cr;
  3466. s2cr.cb_handoff = true;
  3467. if (s2cr.type != S2CR_TYPE_TRANS)
  3468. continue;
  3469. smmu->smrs[i] = smr;
  3470. smmu->s2crs[i] = s2cr;
  3471. bitmap_set(smmu->context_map, s2cr.cbndx, 1);
  3472. dev_dbg(smmu->dev, "Handoff smr: %x s2cr: %x cb: %d\n",
  3473. raw_smr, raw_s2cr, s2cr.cbndx);
  3474. }
  3475. return 0;
  3476. }
  3477. static int arm_smmu_parse_impl_def_registers(struct arm_smmu_device *smmu)
  3478. {
  3479. struct device *dev = smmu->dev;
  3480. int i, ntuples, ret;
  3481. u32 *tuples;
  3482. struct arm_smmu_impl_def_reg *regs, *regit;
  3483. if (!of_find_property(dev->of_node, "attach-impl-defs", &ntuples))
  3484. return 0;
  3485. ntuples /= sizeof(u32);
  3486. if (ntuples % 2) {
  3487. dev_err(dev,
  3488. "Invalid number of attach-impl-defs registers: %d\n",
  3489. ntuples);
  3490. return -EINVAL;
  3491. }
  3492. regs = devm_kmalloc(
  3493. dev, sizeof(*smmu->impl_def_attach_registers) * ntuples,
  3494. GFP_KERNEL);
  3495. if (!regs)
  3496. return -ENOMEM;
  3497. tuples = devm_kmalloc(dev, sizeof(u32) * ntuples * 2, GFP_KERNEL);
  3498. if (!tuples)
  3499. return -ENOMEM;
  3500. ret = of_property_read_u32_array(dev->of_node, "attach-impl-defs",
  3501. tuples, ntuples);
  3502. if (ret)
  3503. return ret;
  3504. for (i = 0, regit = regs; i < ntuples; i += 2, ++regit) {
  3505. regit->offset = tuples[i];
  3506. regit->value = tuples[i + 1];
  3507. }
  3508. devm_kfree(dev, tuples);
  3509. smmu->impl_def_attach_registers = regs;
  3510. smmu->num_impl_def_attach_registers = ntuples / 2;
  3511. return 0;
  3512. }
  3513. static int arm_smmu_init_clocks(struct arm_smmu_power_resources *pwr)
  3514. {
  3515. const char *cname;
  3516. struct property *prop;
  3517. int i;
  3518. struct device *dev = pwr->dev;
  3519. pwr->num_clocks =
  3520. of_property_count_strings(dev->of_node, "clock-names");
  3521. if (pwr->num_clocks < 1) {
  3522. pwr->num_clocks = 0;
  3523. return 0;
  3524. }
  3525. pwr->clocks = devm_kzalloc(
  3526. dev, sizeof(*pwr->clocks) * pwr->num_clocks,
  3527. GFP_KERNEL);
  3528. if (!pwr->clocks)
  3529. return -ENOMEM;
  3530. i = 0;
  3531. of_property_for_each_string(dev->of_node, "clock-names",
  3532. prop, cname) {
  3533. struct clk *c = devm_clk_get(dev, cname);
  3534. if (IS_ERR(c)) {
  3535. dev_err(dev, "Couldn't get clock: %s",
  3536. cname);
  3537. return PTR_ERR(c);
  3538. }
  3539. if (clk_get_rate(c) == 0) {
  3540. long rate = clk_round_rate(c, 1000);
  3541. clk_set_rate(c, rate);
  3542. }
  3543. pwr->clocks[i] = c;
  3544. ++i;
  3545. }
  3546. return 0;
  3547. }
  3548. static int regulator_notifier(struct notifier_block *nb,
  3549. unsigned long event, void *data)
  3550. {
  3551. int ret = 0;
  3552. struct arm_smmu_device *smmu = container_of(nb, struct arm_smmu_device,
  3553. regulator_nb);
  3554. if (event != REGULATOR_EVENT_PRE_DISABLE &&
  3555. event != REGULATOR_EVENT_ENABLE)
  3556. return NOTIFY_OK;
  3557. ret = arm_smmu_prepare_clocks(smmu->pwr);
  3558. if (ret)
  3559. goto out;
  3560. ret = arm_smmu_power_on_atomic(smmu->pwr);
  3561. if (ret)
  3562. goto unprepare_clock;
  3563. if (event == REGULATOR_EVENT_PRE_DISABLE)
  3564. qsmmuv2_halt(smmu);
  3565. else if (event == REGULATOR_EVENT_ENABLE) {
  3566. if (arm_smmu_restore_sec_cfg(smmu, 0))
  3567. goto power_off;
  3568. qsmmuv2_resume(smmu);
  3569. }
  3570. power_off:
  3571. arm_smmu_power_off_atomic(smmu->pwr);
  3572. unprepare_clock:
  3573. arm_smmu_unprepare_clocks(smmu->pwr);
  3574. out:
  3575. return NOTIFY_OK;
  3576. }
  3577. static int register_regulator_notifier(struct arm_smmu_device *smmu)
  3578. {
  3579. struct device *dev = smmu->dev;
  3580. struct regulator_bulk_data *consumers;
  3581. int ret = 0, num_consumers;
  3582. struct arm_smmu_power_resources *pwr = smmu->pwr;
  3583. if (!(smmu->options & ARM_SMMU_OPT_HALT))
  3584. goto out;
  3585. num_consumers = pwr->num_gdscs;
  3586. consumers = pwr->gdscs;
  3587. if (!num_consumers) {
  3588. dev_info(dev, "no regulator info exist for %s\n",
  3589. dev_name(dev));
  3590. goto out;
  3591. }
  3592. smmu->regulator_nb.notifier_call = regulator_notifier;
  3593. /* registering the notifier against one gdsc is sufficient as
  3594. * we do enable/disable regulators in group.
  3595. */
  3596. ret = regulator_register_notifier(consumers[0].consumer,
  3597. &smmu->regulator_nb);
  3598. if (ret)
  3599. dev_err(dev, "Regulator notifier request failed\n");
  3600. out:
  3601. return ret;
  3602. }
  3603. static int arm_smmu_init_regulators(struct arm_smmu_power_resources *pwr)
  3604. {
  3605. const char *cname;
  3606. struct property *prop;
  3607. int i, ret = 0;
  3608. struct device *dev = pwr->dev;
  3609. pwr->num_gdscs =
  3610. of_property_count_strings(dev->of_node, "qcom,regulator-names");
  3611. if (pwr->num_gdscs < 1) {
  3612. pwr->num_gdscs = 0;
  3613. return 0;
  3614. }
  3615. pwr->gdscs = devm_kzalloc(
  3616. dev, sizeof(*pwr->gdscs) * pwr->num_gdscs, GFP_KERNEL);
  3617. if (!pwr->gdscs)
  3618. return -ENOMEM;
  3619. if (!of_property_read_u32(dev->of_node,
  3620. "qcom,deferred-regulator-disable-delay",
  3621. &(pwr->regulator_defer)))
  3622. dev_info(dev, "regulator defer delay %d\n",
  3623. pwr->regulator_defer);
  3624. i = 0;
  3625. of_property_for_each_string(dev->of_node, "qcom,regulator-names",
  3626. prop, cname)
  3627. pwr->gdscs[i++].supply = cname;
  3628. ret = devm_regulator_bulk_get(dev, pwr->num_gdscs, pwr->gdscs);
  3629. return ret;
  3630. }
  3631. static int arm_smmu_init_bus_scaling(struct arm_smmu_power_resources *pwr)
  3632. {
  3633. struct device *dev = pwr->dev;
  3634. /* We don't want the bus APIs to print an error message */
  3635. if (!of_find_property(dev->of_node, "qcom,msm-bus,name", NULL)) {
  3636. dev_dbg(dev, "No bus scaling info\n");
  3637. return 0;
  3638. }
  3639. pwr->bus_dt_data = msm_bus_cl_get_pdata(pwr->pdev);
  3640. if (!pwr->bus_dt_data) {
  3641. dev_err(dev, "Unable to read bus-scaling from devicetree\n");
  3642. return -EINVAL;
  3643. }
  3644. pwr->bus_client = msm_bus_scale_register_client(pwr->bus_dt_data);
  3645. if (!pwr->bus_client) {
  3646. dev_err(dev, "Bus client registration failed\n");
  3647. return -EINVAL;
  3648. }
  3649. return 0;
  3650. }
  3651. /*
  3652. * Cleanup done by devm. Any non-devm resources must clean up themselves.
  3653. */
  3654. static struct arm_smmu_power_resources *arm_smmu_init_power_resources(
  3655. struct platform_device *pdev)
  3656. {
  3657. struct arm_smmu_power_resources *pwr;
  3658. int ret;
  3659. pwr = devm_kzalloc(&pdev->dev, sizeof(*pwr), GFP_KERNEL);
  3660. if (!pwr)
  3661. return ERR_PTR(-ENOMEM);
  3662. pwr->dev = &pdev->dev;
  3663. pwr->pdev = pdev;
  3664. mutex_init(&pwr->power_lock);
  3665. spin_lock_init(&pwr->clock_refs_lock);
  3666. ret = arm_smmu_init_clocks(pwr);
  3667. if (ret)
  3668. return ERR_PTR(ret);
  3669. ret = arm_smmu_init_regulators(pwr);
  3670. if (ret)
  3671. return ERR_PTR(ret);
  3672. ret = arm_smmu_init_bus_scaling(pwr);
  3673. if (ret)
  3674. return ERR_PTR(ret);
  3675. return pwr;
  3676. }
  3677. /*
  3678. * Bus APIs are devm-safe.
  3679. */
  3680. static void arm_smmu_exit_power_resources(struct arm_smmu_power_resources *pwr)
  3681. {
  3682. msm_bus_scale_unregister_client(pwr->bus_client);
  3683. }
  3684. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  3685. {
  3686. unsigned long size;
  3687. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  3688. u32 id;
  3689. bool cttw_dt, cttw_reg;
  3690. int i;
  3691. if (arm_smmu_restore_sec_cfg(smmu, 0))
  3692. return -ENODEV;
  3693. dev_dbg(smmu->dev, "probing hardware configuration...\n");
  3694. dev_dbg(smmu->dev, "SMMUv%d with:\n",
  3695. smmu->version == ARM_SMMU_V2 ? 2 : 1);
  3696. /* ID0 */
  3697. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  3698. /* Restrict available stages based on module parameter */
  3699. if (force_stage == 1)
  3700. id &= ~(ID0_S2TS | ID0_NTS);
  3701. else if (force_stage == 2)
  3702. id &= ~(ID0_S1TS | ID0_NTS);
  3703. if (id & ID0_S1TS) {
  3704. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  3705. dev_dbg(smmu->dev, "\tstage 1 translation\n");
  3706. }
  3707. if (id & ID0_S2TS) {
  3708. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  3709. dev_dbg(smmu->dev, "\tstage 2 translation\n");
  3710. }
  3711. if (id & ID0_NTS) {
  3712. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  3713. dev_dbg(smmu->dev, "\tnested translation\n");
  3714. }
  3715. if (!(smmu->features &
  3716. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
  3717. dev_err(smmu->dev, "\tno translation support!\n");
  3718. return -ENODEV;
  3719. }
  3720. if ((id & ID0_S1TS) &&
  3721. ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
  3722. smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
  3723. dev_dbg(smmu->dev, "\taddress translation ops\n");
  3724. }
  3725. /*
  3726. * In order for DMA API calls to work properly, we must defer to what
  3727. * the DT says about coherency, regardless of what the hardware claims.
  3728. * Fortunately, this also opens up a workaround for systems where the
  3729. * ID register value has ended up configured incorrectly.
  3730. */
  3731. cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
  3732. cttw_reg = !!(id & ID0_CTTW);
  3733. if (cttw_dt)
  3734. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  3735. if (cttw_dt || cttw_reg)
  3736. dev_dbg(smmu->dev, "\t%scoherent table walk\n",
  3737. cttw_dt ? "" : "non-");
  3738. if (cttw_dt != cttw_reg)
  3739. dev_notice(smmu->dev,
  3740. "\t(IDR0.CTTW overridden by dma-coherent property)\n");
  3741. /* Max. number of entries we have for stream matching/indexing */
  3742. size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK);
  3743. smmu->streamid_mask = size - 1;
  3744. if (id & ID0_SMS) {
  3745. u32 smr;
  3746. int i;
  3747. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  3748. size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK;
  3749. if (size == 0) {
  3750. dev_err(smmu->dev,
  3751. "stream-matching supported, but no SMRs present!\n");
  3752. return -ENODEV;
  3753. }
  3754. /*
  3755. * SMR.ID bits may not be preserved if the corresponding MASK
  3756. * bits are set, so check each one separately. We can reject
  3757. * masters later if they try to claim IDs outside these masks.
  3758. */
  3759. if (!arm_smmu_is_static_cb(smmu)) {
  3760. for (i = 0; i < size; i++) {
  3761. smr = readl_relaxed(
  3762. gr0_base + ARM_SMMU_GR0_SMR(i));
  3763. if (!(smr & SMR_VALID))
  3764. break;
  3765. }
  3766. if (i == size) {
  3767. dev_err(smmu->dev,
  3768. "Unable to compute streamid_masks\n");
  3769. return -ENODEV;
  3770. }
  3771. smr = smmu->streamid_mask << SMR_ID_SHIFT;
  3772. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(i));
  3773. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(i));
  3774. smmu->streamid_mask = smr >> SMR_ID_SHIFT;
  3775. smr = smmu->streamid_mask << SMR_MASK_SHIFT;
  3776. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(i));
  3777. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(i));
  3778. smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT;
  3779. } else {
  3780. smmu->smr_mask_mask = SMR_MASK_MASK;
  3781. smmu->streamid_mask = SID_MASK;
  3782. }
  3783. /* Zero-initialised to mark as invalid */
  3784. smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
  3785. GFP_KERNEL);
  3786. if (!smmu->smrs)
  3787. return -ENOMEM;
  3788. dev_notice(smmu->dev,
  3789. "\tstream matching with %lu register groups, mask 0x%x",
  3790. size, smmu->smr_mask_mask);
  3791. }
  3792. /* s2cr->type == 0 means translation, so initialise explicitly */
  3793. smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
  3794. GFP_KERNEL);
  3795. if (!smmu->s2crs)
  3796. return -ENOMEM;
  3797. for (i = 0; i < size; i++)
  3798. smmu->s2crs[i] = s2cr_init_val;
  3799. smmu->num_mapping_groups = size;
  3800. mutex_init(&smmu->stream_map_mutex);
  3801. if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
  3802. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
  3803. if (!(id & ID0_PTFS_NO_AARCH32S))
  3804. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
  3805. }
  3806. /* ID1 */
  3807. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  3808. smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
  3809. /* Check for size mismatch of SMMU address space from mapped region */
  3810. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  3811. size *= 2 << smmu->pgshift;
  3812. if (smmu->size != size)
  3813. dev_warn(smmu->dev,
  3814. "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
  3815. size, smmu->size);
  3816. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
  3817. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  3818. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  3819. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  3820. return -ENODEV;
  3821. }
  3822. dev_dbg(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  3823. smmu->num_context_banks, smmu->num_s2_context_banks);
  3824. /*
  3825. * Cavium CN88xx erratum #27704.
  3826. * Ensure ASID and VMID allocation is unique across all SMMUs in
  3827. * the system.
  3828. */
  3829. if (smmu->model == CAVIUM_SMMUV2) {
  3830. smmu->cavium_id_base =
  3831. atomic_add_return(smmu->num_context_banks,
  3832. &cavium_smmu_context_count);
  3833. smmu->cavium_id_base -= smmu->num_context_banks;
  3834. }
  3835. smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
  3836. sizeof(*smmu->cbs), GFP_KERNEL);
  3837. if (!smmu->cbs)
  3838. return -ENOMEM;
  3839. /* ID2 */
  3840. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  3841. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  3842. smmu->ipa_size = size;
  3843. /* The output mask is also applied for bypass */
  3844. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  3845. smmu->pa_size = size;
  3846. if (id & ID2_VMID16)
  3847. smmu->features |= ARM_SMMU_FEAT_VMID16;
  3848. /*
  3849. * What the page table walker can address actually depends on which
  3850. * descriptor format is in use, but since a) we don't know that yet,
  3851. * and b) it can vary per context bank, this will have to do...
  3852. */
  3853. if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
  3854. dev_warn(smmu->dev,
  3855. "failed to set DMA mask for table walker\n");
  3856. if (smmu->version < ARM_SMMU_V2) {
  3857. smmu->va_size = smmu->ipa_size;
  3858. if (smmu->version == ARM_SMMU_V1_64K)
  3859. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
  3860. } else {
  3861. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  3862. smmu->va_size = arm_smmu_id_size_to_bits(size);
  3863. if (id & ID2_PTFS_4K)
  3864. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
  3865. if (id & ID2_PTFS_16K)
  3866. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
  3867. if (id & ID2_PTFS_64K)
  3868. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
  3869. }
  3870. /* Now we've corralled the various formats, what'll it do? */
  3871. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
  3872. smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
  3873. if (smmu->features &
  3874. (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
  3875. smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
  3876. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
  3877. smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
  3878. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
  3879. smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
  3880. if (arm_smmu_ops.pgsize_bitmap == -1UL)
  3881. arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
  3882. else
  3883. arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
  3884. dev_dbg(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
  3885. smmu->pgsize_bitmap);
  3886. if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
  3887. dev_dbg(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
  3888. smmu->va_size, smmu->ipa_size);
  3889. if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
  3890. dev_dbg(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
  3891. smmu->ipa_size, smmu->pa_size);
  3892. return 0;
  3893. }
  3894. struct arm_smmu_match_data {
  3895. enum arm_smmu_arch_version version;
  3896. enum arm_smmu_implementation model;
  3897. struct arm_smmu_arch_ops *arch_ops;
  3898. };
  3899. #define ARM_SMMU_MATCH_DATA(name, ver, imp, ops) \
  3900. static struct arm_smmu_match_data name = { \
  3901. .version = ver, \
  3902. .model = imp, \
  3903. .arch_ops = ops, \
  3904. } \
  3905. struct arm_smmu_arch_ops qsmmuv500_arch_ops;
  3906. ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU, NULL);
  3907. ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU, NULL);
  3908. ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU, NULL);
  3909. ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500, NULL);
  3910. ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2, NULL);
  3911. ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2, &qsmmuv2_arch_ops);
  3912. ARM_SMMU_MATCH_DATA(qcom_smmuv500, ARM_SMMU_V2, QCOM_SMMUV500,
  3913. &qsmmuv500_arch_ops);
  3914. static const struct of_device_id arm_smmu_of_match[] = {
  3915. { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
  3916. { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
  3917. { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
  3918. { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
  3919. { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
  3920. { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
  3921. { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
  3922. { .compatible = "qcom,qsmmu-v500", .data = &qcom_smmuv500 },
  3923. { },
  3924. };
  3925. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  3926. #ifdef CONFIG_MSM_TZ_SMMU
  3927. int register_iommu_sec_ptbl(void)
  3928. {
  3929. struct device_node *np;
  3930. for_each_matching_node(np, arm_smmu_of_match)
  3931. if (of_find_property(np, "qcom,tz-device-id", NULL) &&
  3932. of_device_is_available(np))
  3933. break;
  3934. if (!np)
  3935. return -ENODEV;
  3936. of_node_put(np);
  3937. return msm_iommu_sec_pgtbl_init();
  3938. }
  3939. #endif
  3940. static int arm_smmu_of_iommu_configure_fixup(struct device *dev, void *data)
  3941. {
  3942. if (!dev->iommu_fwspec)
  3943. of_iommu_configure(dev, dev->of_node);
  3944. return 0;
  3945. }
  3946. static int arm_smmu_add_device_fixup(struct device *dev, void *data)
  3947. {
  3948. struct iommu_ops *ops = data;
  3949. ops->add_device(dev);
  3950. return 0;
  3951. }
  3952. static int qsmmuv500_tbu_register(struct device *dev, void *data);
  3953. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  3954. {
  3955. const struct arm_smmu_match_data *data;
  3956. struct resource *res;
  3957. struct arm_smmu_device *smmu;
  3958. struct device *dev = &pdev->dev;
  3959. int num_irqs, i, err;
  3960. bool legacy_binding;
  3961. legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
  3962. if (legacy_binding && !using_generic_binding) {
  3963. if (!using_legacy_binding)
  3964. pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
  3965. using_legacy_binding = true;
  3966. } else if (!legacy_binding && !using_legacy_binding) {
  3967. using_generic_binding = true;
  3968. } else {
  3969. dev_err(dev, "not probing due to mismatched DT properties\n");
  3970. return -ENODEV;
  3971. }
  3972. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  3973. if (!smmu) {
  3974. dev_err(dev, "failed to allocate arm_smmu_device\n");
  3975. return -ENOMEM;
  3976. }
  3977. smmu->dev = dev;
  3978. spin_lock_init(&smmu->atos_lock);
  3979. idr_init(&smmu->asid_idr);
  3980. mutex_init(&smmu->idr_mutex);
  3981. data = of_device_get_match_data(dev);
  3982. smmu->version = data->version;
  3983. smmu->model = data->model;
  3984. smmu->arch_ops = data->arch_ops;
  3985. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3986. if (res == NULL) {
  3987. dev_err(dev, "no MEM resource info\n");
  3988. return -EINVAL;
  3989. }
  3990. smmu->phys_addr = res->start;
  3991. smmu->base = devm_ioremap_resource(dev, res);
  3992. if (IS_ERR(smmu->base))
  3993. return PTR_ERR(smmu->base);
  3994. smmu->size = resource_size(res);
  3995. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  3996. &smmu->num_global_irqs)) {
  3997. dev_err(dev, "missing #global-interrupts property\n");
  3998. return -ENODEV;
  3999. }
  4000. num_irqs = 0;
  4001. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  4002. num_irqs++;
  4003. if (num_irqs > smmu->num_global_irqs)
  4004. smmu->num_context_irqs++;
  4005. }
  4006. if (!smmu->num_context_irqs) {
  4007. dev_err(dev, "found %d interrupts but expected at least %d\n",
  4008. num_irqs, smmu->num_global_irqs + 1);
  4009. return -ENODEV;
  4010. }
  4011. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  4012. GFP_KERNEL);
  4013. if (!smmu->irqs) {
  4014. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  4015. return -ENOMEM;
  4016. }
  4017. for (i = 0; i < num_irqs; ++i) {
  4018. int irq = platform_get_irq(pdev, i);
  4019. if (irq < 0) {
  4020. dev_err(dev, "failed to get irq index %d\n", i);
  4021. return -ENODEV;
  4022. }
  4023. smmu->irqs[i] = irq;
  4024. }
  4025. parse_driver_options(smmu);
  4026. parse_static_cb_cfg(smmu);
  4027. smmu->pwr = arm_smmu_init_power_resources(pdev);
  4028. if (IS_ERR(smmu->pwr))
  4029. return PTR_ERR(smmu->pwr);
  4030. err = arm_smmu_power_on(smmu->pwr);
  4031. if (err)
  4032. goto out_exit_power_resources;
  4033. smmu->sec_id = msm_dev_to_device_id(dev);
  4034. INIT_LIST_HEAD(&smmu->list);
  4035. spin_lock(&arm_smmu_devices_lock);
  4036. list_add(&smmu->list, &arm_smmu_devices);
  4037. spin_unlock(&arm_smmu_devices_lock);
  4038. err = arm_smmu_device_cfg_probe(smmu);
  4039. if (err)
  4040. goto out_power_off;
  4041. err = arm_smmu_handoff_cbs(smmu);
  4042. if (err)
  4043. goto out_power_off;
  4044. err = arm_smmu_parse_impl_def_registers(smmu);
  4045. if (err)
  4046. goto out_power_off;
  4047. if (smmu->version == ARM_SMMU_V2 &&
  4048. smmu->num_context_banks != smmu->num_context_irqs) {
  4049. dev_err(dev,
  4050. "found %d context interrupt(s) but have %d context banks. assuming %d context interrupts.\n",
  4051. smmu->num_context_irqs, smmu->num_context_banks,
  4052. smmu->num_context_banks);
  4053. smmu->num_context_irqs = smmu->num_context_banks;
  4054. }
  4055. for (i = 0; i < smmu->num_global_irqs; ++i) {
  4056. err = devm_request_threaded_irq(smmu->dev, smmu->irqs[i],
  4057. NULL, arm_smmu_global_fault,
  4058. IRQF_ONESHOT | IRQF_SHARED,
  4059. "arm-smmu global fault", smmu);
  4060. if (err) {
  4061. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  4062. i, smmu->irqs[i]);
  4063. goto out_power_off;
  4064. }
  4065. }
  4066. err = arm_smmu_arch_init(smmu);
  4067. if (err)
  4068. goto out_power_off;
  4069. of_iommu_set_ops(dev->of_node, &arm_smmu_ops);
  4070. platform_set_drvdata(pdev, smmu);
  4071. arm_smmu_device_reset(smmu);
  4072. arm_smmu_power_off(smmu->pwr);
  4073. /* bus_set_iommu depends on this. */
  4074. bus_for_each_dev(&platform_bus_type, NULL, NULL,
  4075. arm_smmu_of_iommu_configure_fixup);
  4076. /* Oh, for a proper bus abstraction */
  4077. if (!iommu_present(&platform_bus_type))
  4078. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  4079. else
  4080. bus_for_each_dev(&platform_bus_type, NULL, &arm_smmu_ops,
  4081. arm_smmu_add_device_fixup);
  4082. err = register_regulator_notifier(smmu);
  4083. if (err)
  4084. goto out_power_off;
  4085. #ifdef CONFIG_ARM_AMBA
  4086. if (!iommu_present(&amba_bustype))
  4087. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  4088. #endif
  4089. #ifdef CONFIG_PCI
  4090. if (!iommu_present(&pci_bus_type)) {
  4091. pci_request_acs();
  4092. bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  4093. }
  4094. #endif
  4095. return 0;
  4096. out_power_off:
  4097. arm_smmu_power_off(smmu->pwr);
  4098. spin_lock(&arm_smmu_devices_lock);
  4099. list_del(&smmu->list);
  4100. spin_unlock(&arm_smmu_devices_lock);
  4101. out_exit_power_resources:
  4102. arm_smmu_exit_power_resources(smmu->pwr);
  4103. return err;
  4104. }
  4105. static int arm_smmu_device_remove(struct platform_device *pdev)
  4106. {
  4107. struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
  4108. if (!smmu)
  4109. return -ENODEV;
  4110. if (arm_smmu_power_on(smmu->pwr))
  4111. return -EINVAL;
  4112. if (!(bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS) &&
  4113. (bitmap_empty(smmu->secure_context_map, ARM_SMMU_MAX_CBS) ||
  4114. arm_smmu_opt_hibernation(smmu))))
  4115. dev_err(&pdev->dev, "removing device with active domains!\n");
  4116. idr_destroy(&smmu->asid_idr);
  4117. /* Turn the thing off */
  4118. writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  4119. arm_smmu_power_off(smmu->pwr);
  4120. arm_smmu_exit_power_resources(smmu->pwr);
  4121. return 0;
  4122. }
  4123. static int arm_smmu_pm_freeze(struct device *dev)
  4124. {
  4125. struct arm_smmu_device *smmu = dev_get_drvdata(dev);
  4126. if (!arm_smmu_opt_hibernation(smmu)) {
  4127. dev_err(smmu->dev, "Aborting: Hibernation not supported\n");
  4128. return -EINVAL;
  4129. }
  4130. return 0;
  4131. }
  4132. static int arm_smmu_pm_restore(struct device *dev)
  4133. {
  4134. struct arm_smmu_device *smmu = dev_get_drvdata(dev);
  4135. int ret;
  4136. ret = arm_smmu_power_on(smmu->pwr);
  4137. if (ret)
  4138. return ret;
  4139. arm_smmu_device_reset(smmu);
  4140. arm_smmu_power_off(smmu->pwr);
  4141. return 0;
  4142. }
  4143. static const struct dev_pm_ops arm_smmu_pm_ops = {
  4144. #ifdef CONFIG_PM_SLEEP
  4145. .freeze = arm_smmu_pm_freeze,
  4146. .restore = arm_smmu_pm_restore,
  4147. #endif
  4148. };
  4149. static struct platform_driver arm_smmu_driver = {
  4150. .driver = {
  4151. .name = "arm-smmu",
  4152. .of_match_table = of_match_ptr(arm_smmu_of_match),
  4153. .pm = &arm_smmu_pm_ops,
  4154. },
  4155. .probe = arm_smmu_device_dt_probe,
  4156. .remove = arm_smmu_device_remove,
  4157. };
  4158. static struct platform_driver qsmmuv500_tbu_driver;
  4159. static int __init arm_smmu_init(void)
  4160. {
  4161. static bool registered;
  4162. int ret = 0;
  4163. struct device_node *node;
  4164. ktime_t cur;
  4165. if (registered)
  4166. return 0;
  4167. cur = ktime_get();
  4168. ret = platform_driver_register(&qsmmuv500_tbu_driver);
  4169. if (ret)
  4170. return ret;
  4171. ret = platform_driver_register(&arm_smmu_driver);
  4172. /* Disable secure usecases if hibernation support is enabled */
  4173. node = of_find_compatible_node(NULL, NULL, "qcom,qsmmu-v500");
  4174. if (IS_ENABLED(CONFIG_MSM_TZ_SMMU) && node &&
  4175. !of_find_property(node, "qcom,hibernation-support", NULL))
  4176. ret = register_iommu_sec_ptbl();
  4177. registered = !ret;
  4178. trace_smmu_init(ktime_us_delta(ktime_get(), cur));
  4179. return ret;
  4180. }
  4181. static void __exit arm_smmu_exit(void)
  4182. {
  4183. return platform_driver_unregister(&arm_smmu_driver);
  4184. }
  4185. subsys_initcall(arm_smmu_init);
  4186. module_exit(arm_smmu_exit);
  4187. static int __init arm_smmu_of_init(struct device_node *np)
  4188. {
  4189. int ret = arm_smmu_init();
  4190. if (ret)
  4191. return ret;
  4192. if (!of_platform_device_create(np, NULL, platform_bus_type.dev_root))
  4193. return -ENODEV;
  4194. return 0;
  4195. }
  4196. IOMMU_OF_DECLARE(arm_smmuv1, "arm,smmu-v1", arm_smmu_of_init);
  4197. IOMMU_OF_DECLARE(arm_smmuv2, "arm,smmu-v2", arm_smmu_of_init);
  4198. IOMMU_OF_DECLARE(arm_mmu400, "arm,mmu-400", arm_smmu_of_init);
  4199. IOMMU_OF_DECLARE(arm_mmu401, "arm,mmu-401", arm_smmu_of_init);
  4200. IOMMU_OF_DECLARE(arm_mmu500, "arm,mmu-500", arm_smmu_of_init);
  4201. IOMMU_OF_DECLARE(cavium_smmuv2, "cavium,smmu-v2", arm_smmu_of_init);
  4202. #define TCU_HW_VERSION_HLOS1 (0x18)
  4203. #define DEBUG_SID_HALT_REG 0x0
  4204. #define DEBUG_SID_HALT_VAL (0x1 << 16)
  4205. #define DEBUG_SID_HALT_SID_MASK 0x3ff
  4206. #define DEBUG_VA_ADDR_REG 0x8
  4207. #define DEBUG_TXN_TRIGG_REG 0x18
  4208. #define DEBUG_TXN_AXPROT_SHIFT 6
  4209. #define DEBUG_TXN_AXCACHE_SHIFT 2
  4210. #define DEBUG_TRX_WRITE (0x1 << 1)
  4211. #define DEBUG_TXN_READ (0x0 << 1)
  4212. #define DEBUG_TXN_TRIGGER 0x1
  4213. #define DEBUG_SR_HALT_ACK_REG 0x20
  4214. #define DEBUG_SR_HALT_ACK_VAL (0x1 << 1)
  4215. #define DEBUG_SR_ECATS_RUNNING_VAL (0x1 << 0)
  4216. #define DEBUG_PAR_REG 0x28
  4217. #define DEBUG_PAR_PA_MASK ((0x1ULL << 36) - 1)
  4218. #define DEBUG_PAR_PA_SHIFT 12
  4219. #define DEBUG_PAR_FAULT_VAL 0x1
  4220. #define TBU_DBG_TIMEOUT_US 100
  4221. #define QSMMUV500_ACTLR_DEEP_PREFETCH_MASK 0x3
  4222. #define QSMMUV500_ACTLR_DEEP_PREFETCH_SHIFT 0x8
  4223. struct actlr_setting {
  4224. struct arm_smmu_smr smr;
  4225. u32 actlr;
  4226. };
  4227. struct qsmmuv500_archdata {
  4228. struct list_head tbus;
  4229. void __iomem *tcu_base;
  4230. u32 version;
  4231. struct actlr_setting *actlrs;
  4232. u32 actlr_tbl_size;
  4233. struct arm_smmu_smr *errata1_clients;
  4234. u32 num_errata1_clients;
  4235. remote_spinlock_t errata1_lock;
  4236. ktime_t last_tlbi_ktime;
  4237. };
  4238. #define get_qsmmuv500_archdata(smmu) \
  4239. ((struct qsmmuv500_archdata *)(smmu->archdata))
  4240. struct qsmmuv500_tbu_device {
  4241. struct list_head list;
  4242. struct device *dev;
  4243. struct arm_smmu_device *smmu;
  4244. void __iomem *base;
  4245. void __iomem *status_reg;
  4246. struct arm_smmu_power_resources *pwr;
  4247. u32 sid_start;
  4248. u32 num_sids;
  4249. /* Protects halt count */
  4250. spinlock_t halt_lock;
  4251. u32 halt_count;
  4252. };
  4253. struct qsmmuv500_group_iommudata {
  4254. bool has_actlr;
  4255. u32 actlr;
  4256. };
  4257. #define to_qsmmuv500_group_iommudata(group) \
  4258. ((struct qsmmuv500_group_iommudata *) \
  4259. (iommu_group_get_iommudata(group)))
  4260. static bool arm_smmu_fwspec_match_smr(struct iommu_fwspec *fwspec,
  4261. struct arm_smmu_smr *smr)
  4262. {
  4263. struct arm_smmu_smr *smr2;
  4264. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  4265. int i, idx;
  4266. for_each_cfg_sme(fwspec, i, idx) {
  4267. smr2 = &smmu->smrs[idx];
  4268. /* Continue if table entry does not match */
  4269. if ((smr->id ^ smr2->id) & ~(smr->mask | smr2->mask))
  4270. continue;
  4271. return true;
  4272. }
  4273. return false;
  4274. }
  4275. #define ERRATA1_REMOTE_SPINLOCK "S:6"
  4276. #define ERRATA1_TLBI_INTERVAL_US 10
  4277. static bool
  4278. qsmmuv500_errata1_required(struct arm_smmu_domain *smmu_domain,
  4279. struct qsmmuv500_archdata *data)
  4280. {
  4281. bool ret = false;
  4282. int j;
  4283. struct arm_smmu_smr *smr;
  4284. struct iommu_fwspec *fwspec;
  4285. if (smmu_domain->qsmmuv500_errata1_init)
  4286. return smmu_domain->qsmmuv500_errata1_client;
  4287. fwspec = smmu_domain->dev->iommu_fwspec;
  4288. for (j = 0; j < data->num_errata1_clients; j++) {
  4289. smr = &data->errata1_clients[j];
  4290. if (arm_smmu_fwspec_match_smr(fwspec, smr)) {
  4291. ret = true;
  4292. break;
  4293. }
  4294. }
  4295. smmu_domain->qsmmuv500_errata1_init = true;
  4296. smmu_domain->qsmmuv500_errata1_client = ret;
  4297. return ret;
  4298. }
  4299. #define SCM_CONFIG_ERRATA1_CLIENT_ALL 0x2
  4300. #define SCM_CONFIG_ERRATA1 0x3
  4301. static void __qsmmuv500_errata1_tlbiall(struct arm_smmu_domain *smmu_domain)
  4302. {
  4303. struct arm_smmu_device *smmu = smmu_domain->smmu;
  4304. struct device *dev = smmu_domain->dev;
  4305. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  4306. void __iomem *base;
  4307. int ret;
  4308. ktime_t cur;
  4309. u32 val;
  4310. struct scm_desc desc = {
  4311. .args[0] = SCM_CONFIG_ERRATA1_CLIENT_ALL,
  4312. .args[1] = false,
  4313. .arginfo = SCM_ARGS(2, SCM_VAL, SCM_VAL),
  4314. };
  4315. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  4316. writel_relaxed(0, base + ARM_SMMU_CB_S1_TLBIALL);
  4317. writel_relaxed(0, base + ARM_SMMU_CB_TLBSYNC);
  4318. if (!readl_poll_timeout_atomic(base + ARM_SMMU_CB_TLBSTATUS, val,
  4319. !(val & TLBSTATUS_SACTIVE), 0, 100))
  4320. return;
  4321. ret = scm_call2_atomic(SCM_SIP_FNID(SCM_SVC_SMMU_PROGRAM,
  4322. SCM_CONFIG_ERRATA1),
  4323. &desc);
  4324. if (ret) {
  4325. dev_err(smmu->dev, "Calling into TZ to disable ERRATA1 failed - IOMMU hardware in bad state\n");
  4326. BUG();
  4327. return;
  4328. }
  4329. cur = ktime_get();
  4330. trace_tlbi_throttle_start(dev, 0);
  4331. msm_bus_noc_throttle_wa(true);
  4332. if (readl_poll_timeout_atomic(base + ARM_SMMU_CB_TLBSTATUS, val,
  4333. !(val & TLBSTATUS_SACTIVE), 0, 10000)) {
  4334. dev_err(smmu->dev, "ERRATA1 TLBSYNC timeout - IOMMU hardware in bad state");
  4335. trace_tlbsync_timeout(dev, 0);
  4336. BUG();
  4337. }
  4338. msm_bus_noc_throttle_wa(false);
  4339. trace_tlbi_throttle_end(dev, ktime_us_delta(ktime_get(), cur));
  4340. desc.args[1] = true;
  4341. ret = scm_call2_atomic(SCM_SIP_FNID(SCM_SVC_SMMU_PROGRAM,
  4342. SCM_CONFIG_ERRATA1),
  4343. &desc);
  4344. if (ret) {
  4345. dev_err(smmu->dev, "Calling into TZ to reenable ERRATA1 failed - IOMMU hardware in bad state\n");
  4346. BUG();
  4347. }
  4348. }
  4349. /* Must be called with clocks/regulators enabled */
  4350. static void qsmmuv500_errata1_tlb_inv_context(void *cookie)
  4351. {
  4352. struct arm_smmu_domain *smmu_domain = cookie;
  4353. struct device *dev = smmu_domain->dev;
  4354. struct qsmmuv500_archdata *data =
  4355. get_qsmmuv500_archdata(smmu_domain->smmu);
  4356. ktime_t cur;
  4357. unsigned long flags;
  4358. bool errata;
  4359. cur = ktime_get();
  4360. trace_tlbi_start(dev, 0);
  4361. errata = qsmmuv500_errata1_required(smmu_domain, data);
  4362. remote_spin_lock_irqsave(&data->errata1_lock, flags);
  4363. if (errata) {
  4364. s64 delta;
  4365. delta = ktime_us_delta(ktime_get(), data->last_tlbi_ktime);
  4366. if (delta < ERRATA1_TLBI_INTERVAL_US)
  4367. udelay(ERRATA1_TLBI_INTERVAL_US - delta);
  4368. __qsmmuv500_errata1_tlbiall(smmu_domain);
  4369. data->last_tlbi_ktime = ktime_get();
  4370. } else {
  4371. __qsmmuv500_errata1_tlbiall(smmu_domain);
  4372. }
  4373. remote_spin_unlock_irqrestore(&data->errata1_lock, flags);
  4374. trace_tlbi_end(dev, ktime_us_delta(ktime_get(), cur));
  4375. }
  4376. static struct iommu_gather_ops qsmmuv500_errata1_smmu_gather_ops = {
  4377. .tlb_flush_all = qsmmuv500_errata1_tlb_inv_context,
  4378. .alloc_pages_exact = arm_smmu_alloc_pages_exact,
  4379. .free_pages_exact = arm_smmu_free_pages_exact,
  4380. };
  4381. static int qsmmuv500_tbu_halt(struct qsmmuv500_tbu_device *tbu,
  4382. struct arm_smmu_domain *smmu_domain)
  4383. {
  4384. unsigned long flags;
  4385. u32 halt, fsr, sctlr_orig, sctlr, status;
  4386. void __iomem *base, *cb_base;
  4387. spin_lock_irqsave(&tbu->halt_lock, flags);
  4388. if (tbu->halt_count) {
  4389. tbu->halt_count++;
  4390. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4391. return 0;
  4392. }
  4393. cb_base = ARM_SMMU_CB_BASE(smmu_domain->smmu) +
  4394. ARM_SMMU_CB(smmu_domain->smmu, smmu_domain->cfg.cbndx);
  4395. base = tbu->base;
  4396. halt = readl_relaxed(base + DEBUG_SID_HALT_REG);
  4397. halt |= DEBUG_SID_HALT_VAL;
  4398. writel_relaxed(halt, base + DEBUG_SID_HALT_REG);
  4399. if (!readl_poll_timeout_atomic(base + DEBUG_SR_HALT_ACK_REG, status,
  4400. (status & DEBUG_SR_HALT_ACK_VAL),
  4401. 0, TBU_DBG_TIMEOUT_US))
  4402. goto out;
  4403. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  4404. if (!(fsr & FSR_FAULT)) {
  4405. dev_err(tbu->dev, "Couldn't halt TBU!\n");
  4406. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4407. return -ETIMEDOUT;
  4408. }
  4409. /*
  4410. * We are in a fault; Our request to halt the bus will not complete
  4411. * until transactions in front of us (such as the fault itself) have
  4412. * completed. Disable iommu faults and terminate any existing
  4413. * transactions.
  4414. */
  4415. sctlr_orig = readl_relaxed(cb_base + ARM_SMMU_CB_SCTLR);
  4416. sctlr = sctlr_orig & ~(SCTLR_CFCFG | SCTLR_CFIE);
  4417. writel_relaxed(sctlr, cb_base + ARM_SMMU_CB_SCTLR);
  4418. writel_relaxed(fsr, cb_base + ARM_SMMU_CB_FSR);
  4419. writel_relaxed(RESUME_TERMINATE, cb_base + ARM_SMMU_CB_RESUME);
  4420. if (readl_poll_timeout_atomic(base + DEBUG_SR_HALT_ACK_REG, status,
  4421. (status & DEBUG_SR_HALT_ACK_VAL),
  4422. 0, TBU_DBG_TIMEOUT_US)) {
  4423. dev_err(tbu->dev, "Couldn't halt TBU from fault context!\n");
  4424. writel_relaxed(sctlr_orig, cb_base + ARM_SMMU_CB_SCTLR);
  4425. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4426. return -ETIMEDOUT;
  4427. }
  4428. writel_relaxed(sctlr_orig, cb_base + ARM_SMMU_CB_SCTLR);
  4429. out:
  4430. tbu->halt_count = 1;
  4431. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4432. return 0;
  4433. }
  4434. static void qsmmuv500_tbu_resume(struct qsmmuv500_tbu_device *tbu)
  4435. {
  4436. unsigned long flags;
  4437. u32 val;
  4438. void __iomem *base;
  4439. spin_lock_irqsave(&tbu->halt_lock, flags);
  4440. if (!tbu->halt_count) {
  4441. WARN(1, "%s: bad tbu->halt_count", dev_name(tbu->dev));
  4442. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4443. return;
  4444. } else if (tbu->halt_count > 1) {
  4445. tbu->halt_count--;
  4446. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4447. return;
  4448. }
  4449. base = tbu->base;
  4450. val = readl_relaxed(base + DEBUG_SID_HALT_REG);
  4451. val &= ~DEBUG_SID_HALT_VAL;
  4452. writel_relaxed(val, base + DEBUG_SID_HALT_REG);
  4453. tbu->halt_count = 0;
  4454. spin_unlock_irqrestore(&tbu->halt_lock, flags);
  4455. }
  4456. static struct qsmmuv500_tbu_device *qsmmuv500_find_tbu(
  4457. struct arm_smmu_device *smmu, u32 sid)
  4458. {
  4459. struct qsmmuv500_tbu_device *tbu = NULL;
  4460. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4461. list_for_each_entry(tbu, &data->tbus, list) {
  4462. if (tbu->sid_start <= sid &&
  4463. sid < tbu->sid_start + tbu->num_sids)
  4464. return tbu;
  4465. }
  4466. return NULL;
  4467. }
  4468. static int qsmmuv500_ecats_lock(struct arm_smmu_domain *smmu_domain,
  4469. struct qsmmuv500_tbu_device *tbu,
  4470. unsigned long *flags)
  4471. {
  4472. struct arm_smmu_device *smmu = tbu->smmu;
  4473. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4474. u32 val;
  4475. spin_lock_irqsave(&smmu->atos_lock, *flags);
  4476. /* The status register is not accessible on version 1.0 */
  4477. if (data->version == 0x01000000)
  4478. return 0;
  4479. if (readl_poll_timeout_atomic(tbu->status_reg,
  4480. val, (val == 0x1), 0,
  4481. TBU_DBG_TIMEOUT_US)) {
  4482. dev_err(tbu->dev, "ECATS hw busy!\n");
  4483. spin_unlock_irqrestore(&smmu->atos_lock, *flags);
  4484. return -ETIMEDOUT;
  4485. }
  4486. return 0;
  4487. }
  4488. static void qsmmuv500_ecats_unlock(struct arm_smmu_domain *smmu_domain,
  4489. struct qsmmuv500_tbu_device *tbu,
  4490. unsigned long *flags)
  4491. {
  4492. struct arm_smmu_device *smmu = tbu->smmu;
  4493. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4494. /* The status register is not accessible on version 1.0 */
  4495. if (data->version != 0x01000000)
  4496. writel_relaxed(0, tbu->status_reg);
  4497. spin_unlock_irqrestore(&smmu->atos_lock, *flags);
  4498. }
  4499. /*
  4500. * Zero means failure.
  4501. */
  4502. static phys_addr_t qsmmuv500_iova_to_phys(
  4503. struct iommu_domain *domain, dma_addr_t iova, u32 sid)
  4504. {
  4505. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  4506. struct arm_smmu_device *smmu = smmu_domain->smmu;
  4507. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  4508. struct qsmmuv500_tbu_device *tbu;
  4509. int ret;
  4510. phys_addr_t phys = 0;
  4511. u64 val, fsr;
  4512. unsigned long flags;
  4513. void __iomem *cb_base;
  4514. u32 sctlr_orig, sctlr;
  4515. int needs_redo = 0;
  4516. ktime_t timeout;
  4517. /* only 36 bit iova is supported */
  4518. if (iova >= (1ULL << 36)) {
  4519. dev_err_ratelimited(smmu->dev, "ECATS: address too large: %pad\n",
  4520. &iova);
  4521. return 0;
  4522. }
  4523. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  4524. tbu = qsmmuv500_find_tbu(smmu, sid);
  4525. if (!tbu)
  4526. return 0;
  4527. ret = arm_smmu_power_on(tbu->pwr);
  4528. if (ret)
  4529. return 0;
  4530. ret = qsmmuv500_tbu_halt(tbu, smmu_domain);
  4531. if (ret)
  4532. goto out_power_off;
  4533. /*
  4534. * ECATS can trigger the fault interrupt, so disable it temporarily
  4535. * and check for an interrupt manually.
  4536. */
  4537. sctlr_orig = readl_relaxed(cb_base + ARM_SMMU_CB_SCTLR);
  4538. sctlr = sctlr_orig & ~(SCTLR_CFCFG | SCTLR_CFIE);
  4539. writel_relaxed(sctlr, cb_base + ARM_SMMU_CB_SCTLR);
  4540. /* Only one concurrent atos operation */
  4541. ret = qsmmuv500_ecats_lock(smmu_domain, tbu, &flags);
  4542. if (ret)
  4543. goto out_resume;
  4544. redo:
  4545. /* Set address and stream-id */
  4546. val = readq_relaxed(tbu->base + DEBUG_SID_HALT_REG);
  4547. val |= sid & DEBUG_SID_HALT_SID_MASK;
  4548. writeq_relaxed(val, tbu->base + DEBUG_SID_HALT_REG);
  4549. writeq_relaxed(iova, tbu->base + DEBUG_VA_ADDR_REG);
  4550. /*
  4551. * Write-back Read and Write-Allocate
  4552. * Priviledged, nonsecure, data transaction
  4553. * Read operation.
  4554. */
  4555. val = 0xF << DEBUG_TXN_AXCACHE_SHIFT;
  4556. val |= 0x3 << DEBUG_TXN_AXPROT_SHIFT;
  4557. val |= DEBUG_TXN_TRIGGER;
  4558. writeq_relaxed(val, tbu->base + DEBUG_TXN_TRIGG_REG);
  4559. ret = 0;
  4560. //based on readx_poll_timeout_atomic
  4561. timeout = ktime_add_us(ktime_get(), TBU_DBG_TIMEOUT_US);
  4562. for (;;) {
  4563. val = readl_relaxed(tbu->base + DEBUG_SR_HALT_ACK_REG);
  4564. if (!(val & DEBUG_SR_ECATS_RUNNING_VAL))
  4565. break;
  4566. val = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  4567. if (val & FSR_FAULT)
  4568. break;
  4569. if (ktime_compare(ktime_get(), timeout) > 0) {
  4570. dev_err(tbu->dev, "ECATS translation timed out!\n");
  4571. ret = -ETIMEDOUT;
  4572. break;
  4573. }
  4574. }
  4575. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  4576. if (fsr & FSR_FAULT) {
  4577. dev_err(tbu->dev, "ECATS generated a fault interrupt! FSR = %llx\n",
  4578. fsr);
  4579. ret = -EINVAL;
  4580. writel_relaxed(val, cb_base + ARM_SMMU_CB_FSR);
  4581. /*
  4582. * Clear pending interrupts
  4583. * Barrier required to ensure that the FSR is cleared
  4584. * before resuming SMMU operation
  4585. */
  4586. wmb();
  4587. writel_relaxed(RESUME_TERMINATE, cb_base + ARM_SMMU_CB_RESUME);
  4588. }
  4589. val = readq_relaxed(tbu->base + DEBUG_PAR_REG);
  4590. if (val & DEBUG_PAR_FAULT_VAL) {
  4591. dev_err(tbu->dev, "ECATS translation failed! PAR = %llx\n",
  4592. val);
  4593. ret = -EINVAL;
  4594. }
  4595. phys = (val >> DEBUG_PAR_PA_SHIFT) & DEBUG_PAR_PA_MASK;
  4596. if (ret < 0)
  4597. phys = 0;
  4598. /* Reset hardware */
  4599. writeq_relaxed(0, tbu->base + DEBUG_TXN_TRIGG_REG);
  4600. writeq_relaxed(0, tbu->base + DEBUG_VA_ADDR_REG);
  4601. /*
  4602. * After a failed translation, the next successful translation will
  4603. * incorrectly be reported as a failure.
  4604. */
  4605. if (!phys && needs_redo++ < 2)
  4606. goto redo;
  4607. writel_relaxed(sctlr_orig, cb_base + ARM_SMMU_CB_SCTLR);
  4608. qsmmuv500_ecats_unlock(smmu_domain, tbu, &flags);
  4609. out_resume:
  4610. qsmmuv500_tbu_resume(tbu);
  4611. out_power_off:
  4612. arm_smmu_power_off(tbu->pwr);
  4613. return phys;
  4614. }
  4615. static phys_addr_t qsmmuv500_iova_to_phys_hard(
  4616. struct iommu_domain *domain, dma_addr_t iova)
  4617. {
  4618. u16 sid;
  4619. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  4620. struct iommu_fwspec *fwspec;
  4621. /* Select a sid */
  4622. fwspec = smmu_domain->dev->iommu_fwspec;
  4623. sid = (u16)fwspec->ids[0];
  4624. return qsmmuv500_iova_to_phys(domain, iova, sid);
  4625. }
  4626. static void qsmmuv500_release_group_iommudata(void *data)
  4627. {
  4628. kfree(data);
  4629. }
  4630. /* If a device has a valid actlr, it must match */
  4631. static int qsmmuv500_device_group(struct device *dev,
  4632. struct iommu_group *group)
  4633. {
  4634. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  4635. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  4636. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4637. struct qsmmuv500_group_iommudata *iommudata;
  4638. u32 actlr, i;
  4639. struct arm_smmu_smr *smr;
  4640. iommudata = to_qsmmuv500_group_iommudata(group);
  4641. if (!iommudata) {
  4642. iommudata = kzalloc(sizeof(*iommudata), GFP_KERNEL);
  4643. if (!iommudata)
  4644. return -ENOMEM;
  4645. iommu_group_set_iommudata(group, iommudata,
  4646. qsmmuv500_release_group_iommudata);
  4647. }
  4648. for (i = 0; i < data->actlr_tbl_size; i++) {
  4649. smr = &data->actlrs[i].smr;
  4650. actlr = data->actlrs[i].actlr;
  4651. if (!arm_smmu_fwspec_match_smr(fwspec, smr))
  4652. continue;
  4653. if (!iommudata->has_actlr) {
  4654. iommudata->actlr = actlr;
  4655. iommudata->has_actlr = true;
  4656. } else if (iommudata->actlr != actlr) {
  4657. return -EINVAL;
  4658. }
  4659. }
  4660. return 0;
  4661. }
  4662. static void qsmmuv500_init_cb(struct arm_smmu_domain *smmu_domain,
  4663. struct device *dev)
  4664. {
  4665. struct arm_smmu_device *smmu = smmu_domain->smmu;
  4666. struct arm_smmu_cb *cb = &smmu->cbs[smmu_domain->cfg.cbndx];
  4667. struct qsmmuv500_group_iommudata *iommudata =
  4668. to_qsmmuv500_group_iommudata(dev->iommu_group);
  4669. if (!iommudata->has_actlr)
  4670. return;
  4671. cb->actlr = iommudata->actlr;
  4672. /*
  4673. * Prefetch only works properly if the start and end of all
  4674. * buffers in the page table are aligned to 16 Kb.
  4675. */
  4676. if ((iommudata->actlr >> QSMMUV500_ACTLR_DEEP_PREFETCH_SHIFT) &
  4677. QSMMUV500_ACTLR_DEEP_PREFETCH_MASK)
  4678. smmu_domain->qsmmuv500_errata2_min_align = true;
  4679. }
  4680. static int qsmmuv500_tbu_register(struct device *dev, void *cookie)
  4681. {
  4682. struct arm_smmu_device *smmu = cookie;
  4683. struct qsmmuv500_tbu_device *tbu;
  4684. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4685. if (!dev->driver) {
  4686. dev_err(dev, "TBU failed probe, QSMMUV500 cannot continue!\n");
  4687. return -EINVAL;
  4688. }
  4689. tbu = dev_get_drvdata(dev);
  4690. INIT_LIST_HEAD(&tbu->list);
  4691. tbu->smmu = smmu;
  4692. list_add(&tbu->list, &data->tbus);
  4693. return 0;
  4694. }
  4695. static int qsmmuv500_parse_errata1(struct arm_smmu_device *smmu)
  4696. {
  4697. int len, i;
  4698. struct device *dev = smmu->dev;
  4699. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4700. struct arm_smmu_smr *smrs;
  4701. const __be32 *cell;
  4702. cell = of_get_property(dev->of_node, "qcom,mmu500-errata-1", NULL);
  4703. if (!cell)
  4704. return 0;
  4705. remote_spin_lock_init(&data->errata1_lock, ERRATA1_REMOTE_SPINLOCK);
  4706. len = of_property_count_elems_of_size(
  4707. dev->of_node, "qcom,mmu500-errata-1", sizeof(u32) * 2);
  4708. if (len < 0)
  4709. return 0;
  4710. smrs = devm_kzalloc(dev, sizeof(*smrs) * len, GFP_KERNEL);
  4711. if (!smrs)
  4712. return -ENOMEM;
  4713. for (i = 0; i < len; i++) {
  4714. smrs[i].id = of_read_number(cell++, 1);
  4715. smrs[i].mask = of_read_number(cell++, 1);
  4716. }
  4717. data->errata1_clients = smrs;
  4718. data->num_errata1_clients = len;
  4719. return 0;
  4720. }
  4721. static int qsmmuv500_read_actlr_tbl(struct arm_smmu_device *smmu)
  4722. {
  4723. int len, i;
  4724. struct device *dev = smmu->dev;
  4725. struct qsmmuv500_archdata *data = get_qsmmuv500_archdata(smmu);
  4726. struct actlr_setting *actlrs;
  4727. const __be32 *cell;
  4728. cell = of_get_property(dev->of_node, "qcom,actlr", NULL);
  4729. if (!cell)
  4730. return 0;
  4731. len = of_property_count_elems_of_size(dev->of_node, "qcom,actlr",
  4732. sizeof(u32) * 3);
  4733. if (len < 0)
  4734. return 0;
  4735. actlrs = devm_kzalloc(dev, sizeof(*actlrs) * len, GFP_KERNEL);
  4736. if (!actlrs)
  4737. return -ENOMEM;
  4738. for (i = 0; i < len; i++) {
  4739. actlrs[i].smr.id = of_read_number(cell++, 1);
  4740. actlrs[i].smr.mask = of_read_number(cell++, 1);
  4741. actlrs[i].actlr = of_read_number(cell++, 1);
  4742. }
  4743. data->actlrs = actlrs;
  4744. data->actlr_tbl_size = len;
  4745. return 0;
  4746. }
  4747. static int qsmmuv500_arch_init(struct arm_smmu_device *smmu)
  4748. {
  4749. struct resource *res;
  4750. struct device *dev = smmu->dev;
  4751. struct qsmmuv500_archdata *data;
  4752. struct platform_device *pdev;
  4753. int ret;
  4754. u32 val;
  4755. void __iomem *reg;
  4756. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  4757. if (!data)
  4758. return -ENOMEM;
  4759. INIT_LIST_HEAD(&data->tbus);
  4760. pdev = container_of(dev, struct platform_device, dev);
  4761. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcu-base");
  4762. if (!res) {
  4763. dev_err(dev, "Unable to get the tcu-base\n");
  4764. return -EINVAL;
  4765. }
  4766. data->tcu_base = devm_ioremap(dev, res->start, resource_size(res));
  4767. if (IS_ERR(data->tcu_base))
  4768. return PTR_ERR(data->tcu_base);
  4769. data->version = readl_relaxed(data->tcu_base + TCU_HW_VERSION_HLOS1);
  4770. smmu->archdata = data;
  4771. if (arm_smmu_is_static_cb(smmu))
  4772. return 0;
  4773. ret = qsmmuv500_parse_errata1(smmu);
  4774. if (ret)
  4775. return ret;
  4776. ret = qsmmuv500_read_actlr_tbl(smmu);
  4777. if (ret)
  4778. return ret;
  4779. reg = ARM_SMMU_GR0(smmu);
  4780. val = readl_relaxed(reg + ARM_SMMU_GR0_sACR);
  4781. val &= ~ARM_MMU500_ACR_CACHE_LOCK;
  4782. writel_relaxed(val, reg + ARM_SMMU_GR0_sACR);
  4783. val = readl_relaxed(reg + ARM_SMMU_GR0_sACR);
  4784. /*
  4785. * Modifiying the nonsecure copy of the sACR register is only
  4786. * allowed if permission is given in the secure sACR register.
  4787. * Attempt to detect if we were able to update the value.
  4788. */
  4789. WARN_ON(val & ARM_MMU500_ACR_CACHE_LOCK);
  4790. ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  4791. if (ret)
  4792. return ret;
  4793. /* Attempt to register child devices */
  4794. ret = device_for_each_child(dev, smmu, qsmmuv500_tbu_register);
  4795. if (ret)
  4796. return -EPROBE_DEFER;
  4797. return 0;
  4798. }
  4799. struct arm_smmu_arch_ops qsmmuv500_arch_ops = {
  4800. .init = qsmmuv500_arch_init,
  4801. .iova_to_phys_hard = qsmmuv500_iova_to_phys_hard,
  4802. .init_context_bank = qsmmuv500_init_cb,
  4803. .device_group = qsmmuv500_device_group,
  4804. };
  4805. static const struct of_device_id qsmmuv500_tbu_of_match[] = {
  4806. {.compatible = "qcom,qsmmuv500-tbu"},
  4807. {}
  4808. };
  4809. static int qsmmuv500_tbu_probe(struct platform_device *pdev)
  4810. {
  4811. struct resource *res;
  4812. struct device *dev = &pdev->dev;
  4813. struct qsmmuv500_tbu_device *tbu;
  4814. const __be32 *cell;
  4815. int len;
  4816. tbu = devm_kzalloc(dev, sizeof(*tbu), GFP_KERNEL);
  4817. if (!tbu)
  4818. return -ENOMEM;
  4819. INIT_LIST_HEAD(&tbu->list);
  4820. tbu->dev = dev;
  4821. spin_lock_init(&tbu->halt_lock);
  4822. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
  4823. tbu->base = devm_ioremap_resource(dev, res);
  4824. if (IS_ERR(tbu->base))
  4825. return PTR_ERR(tbu->base);
  4826. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "status-reg");
  4827. tbu->status_reg = devm_ioremap_resource(dev, res);
  4828. if (IS_ERR(tbu->status_reg))
  4829. return PTR_ERR(tbu->status_reg);
  4830. cell = of_get_property(dev->of_node, "qcom,stream-id-range", &len);
  4831. if (!cell || len < 8)
  4832. return -EINVAL;
  4833. tbu->sid_start = of_read_number(cell, 1);
  4834. tbu->num_sids = of_read_number(cell + 1, 1);
  4835. tbu->pwr = arm_smmu_init_power_resources(pdev);
  4836. if (IS_ERR(tbu->pwr))
  4837. return PTR_ERR(tbu->pwr);
  4838. dev_set_drvdata(dev, tbu);
  4839. return 0;
  4840. }
  4841. static struct platform_driver qsmmuv500_tbu_driver = {
  4842. .driver = {
  4843. .name = "qsmmuv500-tbu",
  4844. .of_match_table = of_match_ptr(qsmmuv500_tbu_of_match),
  4845. },
  4846. .probe = qsmmuv500_tbu_probe,
  4847. };
  4848. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  4849. MODULE_AUTHOR("Will Deacon <[email protected]>");
  4850. MODULE_LICENSE("GPL v2");