intel-iommu.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505
  1. /*
  2. * Copyright © 2006-2014 Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * Authors: David Woodhouse <dwmw2@infradead.org>,
  14. * Ashok Raj <ashok.raj@intel.com>,
  15. * Shaohua Li <shaohua.li@intel.com>,
  16. * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
  17. * Fenghua Yu <fenghua.yu@intel.com>
  18. * Joerg Roedel <jroedel@suse.de>
  19. */
  20. #define pr_fmt(fmt) "DMAR: " fmt
  21. #include <linux/init.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/export.h>
  25. #include <linux/slab.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/pci.h>
  30. #include <linux/dmar.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/mempool.h>
  33. #include <linux/memory.h>
  34. #include <linux/cpu.h>
  35. #include <linux/timer.h>
  36. #include <linux/io.h>
  37. #include <linux/iova.h>
  38. #include <linux/iommu.h>
  39. #include <linux/intel-iommu.h>
  40. #include <linux/syscore_ops.h>
  41. #include <linux/tboot.h>
  42. #include <linux/dmi.h>
  43. #include <linux/pci-ats.h>
  44. #include <linux/memblock.h>
  45. #include <linux/dma-contiguous.h>
  46. #include <linux/dma-direct.h>
  47. #include <linux/crash_dump.h>
  48. #include <asm/irq_remapping.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/iommu.h>
  51. #include "irq_remapping.h"
  52. #include "intel-pasid.h"
  53. #define ROOT_SIZE VTD_PAGE_SIZE
  54. #define CONTEXT_SIZE VTD_PAGE_SIZE
  55. #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  56. #define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
  57. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  58. #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
  59. #define IOAPIC_RANGE_START (0xfee00000)
  60. #define IOAPIC_RANGE_END (0xfeefffff)
  61. #define IOVA_START_ADDR (0x1000)
  62. #define DEFAULT_DOMAIN_ADDRESS_WIDTH 57
  63. #define MAX_AGAW_WIDTH 64
  64. #define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
  65. #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
  66. #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
  67. /* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
  68. to match. That way, we can use 'unsigned long' for PFNs with impunity. */
  69. #define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
  70. __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
  71. #define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
  72. /* IO virtual address start page frame number */
  73. #define IOVA_START_PFN (1)
  74. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  75. /* page table handling */
  76. #define LEVEL_STRIDE (9)
  77. #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
  78. /*
  79. * This bitmap is used to advertise the page sizes our hardware support
  80. * to the IOMMU core, which will then use this information to split
  81. * physically contiguous memory regions it is mapping into page sizes
  82. * that we support.
  83. *
  84. * Traditionally the IOMMU core just handed us the mappings directly,
  85. * after making sure the size is an order of a 4KiB page and that the
  86. * mapping has natural alignment.
  87. *
  88. * To retain this behavior, we currently advertise that we support
  89. * all page sizes that are an order of 4KiB.
  90. *
  91. * If at some point we'd like to utilize the IOMMU core's new behavior,
  92. * we could change this to advertise the real page sizes we support.
  93. */
  94. #define INTEL_IOMMU_PGSIZES (~0xFFFUL)
  95. static inline int agaw_to_level(int agaw)
  96. {
  97. return agaw + 2;
  98. }
  99. static inline int agaw_to_width(int agaw)
  100. {
  101. return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
  102. }
  103. static inline int width_to_agaw(int width)
  104. {
  105. return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
  106. }
  107. static inline unsigned int level_to_offset_bits(int level)
  108. {
  109. return (level - 1) * LEVEL_STRIDE;
  110. }
  111. static inline int pfn_level_offset(unsigned long pfn, int level)
  112. {
  113. return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
  114. }
  115. static inline unsigned long level_mask(int level)
  116. {
  117. return -1UL << level_to_offset_bits(level);
  118. }
  119. static inline unsigned long level_size(int level)
  120. {
  121. return 1UL << level_to_offset_bits(level);
  122. }
  123. static inline unsigned long align_to_level(unsigned long pfn, int level)
  124. {
  125. return (pfn + level_size(level) - 1) & level_mask(level);
  126. }
  127. static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
  128. {
  129. return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
  130. }
  131. /* VT-d pages must always be _smaller_ than MM pages. Otherwise things
  132. are never going to work. */
  133. static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
  134. {
  135. return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
  136. }
  137. static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
  138. {
  139. return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
  140. }
  141. static inline unsigned long page_to_dma_pfn(struct page *pg)
  142. {
  143. return mm_to_dma_pfn(page_to_pfn(pg));
  144. }
  145. static inline unsigned long virt_to_dma_pfn(void *p)
  146. {
  147. return page_to_dma_pfn(virt_to_page(p));
  148. }
  149. /* global iommu list, set NULL for ignored DMAR units */
  150. static struct intel_iommu **g_iommus;
  151. static void __init check_tylersburg_isoch(void);
  152. static int rwbf_quirk;
  153. /*
  154. * set to 1 to panic kernel if can't successfully enable VT-d
  155. * (used when kernel is launched w/ TXT)
  156. */
  157. static int force_on = 0;
  158. int intel_iommu_tboot_noforce;
  159. /*
  160. * 0: Present
  161. * 1-11: Reserved
  162. * 12-63: Context Ptr (12 - (haw-1))
  163. * 64-127: Reserved
  164. */
  165. struct root_entry {
  166. u64 lo;
  167. u64 hi;
  168. };
  169. #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
  170. /*
  171. * Take a root_entry and return the Lower Context Table Pointer (LCTP)
  172. * if marked present.
  173. */
  174. static phys_addr_t root_entry_lctp(struct root_entry *re)
  175. {
  176. if (!(re->lo & 1))
  177. return 0;
  178. return re->lo & VTD_PAGE_MASK;
  179. }
  180. /*
  181. * Take a root_entry and return the Upper Context Table Pointer (UCTP)
  182. * if marked present.
  183. */
  184. static phys_addr_t root_entry_uctp(struct root_entry *re)
  185. {
  186. if (!(re->hi & 1))
  187. return 0;
  188. return re->hi & VTD_PAGE_MASK;
  189. }
  190. /*
  191. * low 64 bits:
  192. * 0: present
  193. * 1: fault processing disable
  194. * 2-3: translation type
  195. * 12-63: address space root
  196. * high 64 bits:
  197. * 0-2: address width
  198. * 3-6: aval
  199. * 8-23: domain id
  200. */
  201. struct context_entry {
  202. u64 lo;
  203. u64 hi;
  204. };
  205. static inline void context_clear_pasid_enable(struct context_entry *context)
  206. {
  207. context->lo &= ~(1ULL << 11);
  208. }
  209. static inline bool context_pasid_enabled(struct context_entry *context)
  210. {
  211. return !!(context->lo & (1ULL << 11));
  212. }
  213. static inline void context_set_copied(struct context_entry *context)
  214. {
  215. context->hi |= (1ull << 3);
  216. }
  217. static inline bool context_copied(struct context_entry *context)
  218. {
  219. return !!(context->hi & (1ULL << 3));
  220. }
  221. static inline bool __context_present(struct context_entry *context)
  222. {
  223. return (context->lo & 1);
  224. }
  225. static inline bool context_present(struct context_entry *context)
  226. {
  227. return context_pasid_enabled(context) ?
  228. __context_present(context) :
  229. __context_present(context) && !context_copied(context);
  230. }
  231. static inline void context_set_present(struct context_entry *context)
  232. {
  233. context->lo |= 1;
  234. }
  235. static inline void context_set_fault_enable(struct context_entry *context)
  236. {
  237. context->lo &= (((u64)-1) << 2) | 1;
  238. }
  239. static inline void context_set_translation_type(struct context_entry *context,
  240. unsigned long value)
  241. {
  242. context->lo &= (((u64)-1) << 4) | 3;
  243. context->lo |= (value & 3) << 2;
  244. }
  245. static inline void context_set_address_root(struct context_entry *context,
  246. unsigned long value)
  247. {
  248. context->lo &= ~VTD_PAGE_MASK;
  249. context->lo |= value & VTD_PAGE_MASK;
  250. }
  251. static inline void context_set_address_width(struct context_entry *context,
  252. unsigned long value)
  253. {
  254. context->hi |= value & 7;
  255. }
  256. static inline void context_set_domain_id(struct context_entry *context,
  257. unsigned long value)
  258. {
  259. context->hi |= (value & ((1 << 16) - 1)) << 8;
  260. }
  261. static inline int context_domain_id(struct context_entry *c)
  262. {
  263. return((c->hi >> 8) & 0xffff);
  264. }
  265. static inline void context_clear_entry(struct context_entry *context)
  266. {
  267. context->lo = 0;
  268. context->hi = 0;
  269. }
  270. /*
  271. * 0: readable
  272. * 1: writable
  273. * 2-6: reserved
  274. * 7: super page
  275. * 8-10: available
  276. * 11: snoop behavior
  277. * 12-63: Host physcial address
  278. */
  279. struct dma_pte {
  280. u64 val;
  281. };
  282. static inline void dma_clear_pte(struct dma_pte *pte)
  283. {
  284. pte->val = 0;
  285. }
  286. static inline u64 dma_pte_addr(struct dma_pte *pte)
  287. {
  288. #ifdef CONFIG_64BIT
  289. return pte->val & VTD_PAGE_MASK;
  290. #else
  291. /* Must have a full atomic 64-bit read */
  292. return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
  293. #endif
  294. }
  295. static inline bool dma_pte_present(struct dma_pte *pte)
  296. {
  297. return (pte->val & 3) != 0;
  298. }
  299. static inline bool dma_pte_superpage(struct dma_pte *pte)
  300. {
  301. return (pte->val & DMA_PTE_LARGE_PAGE);
  302. }
  303. static inline int first_pte_in_page(struct dma_pte *pte)
  304. {
  305. return !((unsigned long)pte & ~VTD_PAGE_MASK);
  306. }
  307. /*
  308. * This domain is a statically identity mapping domain.
  309. * 1. This domain creats a static 1:1 mapping to all usable memory.
  310. * 2. It maps to each iommu if successful.
  311. * 3. Each iommu mapps to this domain if successful.
  312. */
  313. static struct dmar_domain *si_domain;
  314. static int hw_pass_through = 1;
  315. /*
  316. * Domain represents a virtual machine, more than one devices
  317. * across iommus may be owned in one domain, e.g. kvm guest.
  318. */
  319. #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
  320. /* si_domain contains mulitple devices */
  321. #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
  322. #define for_each_domain_iommu(idx, domain) \
  323. for (idx = 0; idx < g_num_of_iommus; idx++) \
  324. if (domain->iommu_refcnt[idx])
  325. struct dmar_rmrr_unit {
  326. struct list_head list; /* list of rmrr units */
  327. struct acpi_dmar_header *hdr; /* ACPI header */
  328. u64 base_address; /* reserved base address*/
  329. u64 end_address; /* reserved end address */
  330. struct dmar_dev_scope *devices; /* target devices */
  331. int devices_cnt; /* target device count */
  332. };
  333. struct dmar_atsr_unit {
  334. struct list_head list; /* list of ATSR units */
  335. struct acpi_dmar_header *hdr; /* ACPI header */
  336. struct dmar_dev_scope *devices; /* target devices */
  337. int devices_cnt; /* target device count */
  338. u8 include_all:1; /* include all ports */
  339. };
  340. static LIST_HEAD(dmar_atsr_units);
  341. static LIST_HEAD(dmar_rmrr_units);
  342. #define for_each_rmrr_units(rmrr) \
  343. list_for_each_entry(rmrr, &dmar_rmrr_units, list)
  344. /* bitmap for indexing intel_iommus */
  345. static int g_num_of_iommus;
  346. static void domain_exit(struct dmar_domain *domain);
  347. static void domain_remove_dev_info(struct dmar_domain *domain);
  348. static void dmar_remove_one_dev_info(struct dmar_domain *domain,
  349. struct device *dev);
  350. static void __dmar_remove_one_dev_info(struct device_domain_info *info);
  351. static void domain_context_clear(struct intel_iommu *iommu,
  352. struct device *dev);
  353. static int domain_detach_iommu(struct dmar_domain *domain,
  354. struct intel_iommu *iommu);
  355. #ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
  356. int dmar_disabled = 0;
  357. #else
  358. int dmar_disabled = 1;
  359. #endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
  360. int intel_iommu_enabled = 0;
  361. EXPORT_SYMBOL_GPL(intel_iommu_enabled);
  362. static int dmar_map_gfx = 1;
  363. static int dmar_forcedac;
  364. static int intel_iommu_strict;
  365. static int intel_iommu_superpage = 1;
  366. static int intel_iommu_ecs = 1;
  367. static int intel_iommu_pasid28;
  368. static int iommu_identity_mapping;
  369. #define IDENTMAP_ALL 1
  370. #define IDENTMAP_GFX 2
  371. #define IDENTMAP_AZALIA 4
  372. /* Broadwell and Skylake have broken ECS support — normal so-called "second
  373. * level" translation of DMA requests-without-PASID doesn't actually happen
  374. * unless you also set the NESTE bit in an extended context-entry. Which of
  375. * course means that SVM doesn't work because it's trying to do nested
  376. * translation of the physical addresses it finds in the process page tables,
  377. * through the IOVA->phys mapping found in the "second level" page tables.
  378. *
  379. * The VT-d specification was retroactively changed to change the definition
  380. * of the capability bits and pretend that Broadwell/Skylake never happened...
  381. * but unfortunately the wrong bit was changed. It's ECS which is broken, but
  382. * for some reason it was the PASID capability bit which was redefined (from
  383. * bit 28 on BDW/SKL to bit 40 in future).
  384. *
  385. * So our test for ECS needs to eschew those implementations which set the old
  386. * PASID capabiity bit 28, since those are the ones on which ECS is broken.
  387. * Unless we are working around the 'pasid28' limitations, that is, by putting
  388. * the device into passthrough mode for normal DMA and thus masking the bug.
  389. */
  390. #define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
  391. (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))
  392. /* PASID support is thus enabled if ECS is enabled and *either* of the old
  393. * or new capability bits are set. */
  394. #define pasid_enabled(iommu) (ecs_enabled(iommu) && \
  395. (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))
  396. int intel_iommu_gfx_mapped;
  397. EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
  398. #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
  399. static DEFINE_SPINLOCK(device_domain_lock);
  400. static LIST_HEAD(device_domain_list);
  401. /*
  402. * Iterate over elements in device_domain_list and call the specified
  403. * callback @fn against each element. This helper should only be used
  404. * in the context where the device_domain_lock has already been holden.
  405. */
  406. int for_each_device_domain(int (*fn)(struct device_domain_info *info,
  407. void *data), void *data)
  408. {
  409. int ret = 0;
  410. struct device_domain_info *info;
  411. assert_spin_locked(&device_domain_lock);
  412. list_for_each_entry(info, &device_domain_list, global) {
  413. ret = fn(info, data);
  414. if (ret)
  415. return ret;
  416. }
  417. return 0;
  418. }
  419. const struct iommu_ops intel_iommu_ops;
  420. static bool translation_pre_enabled(struct intel_iommu *iommu)
  421. {
  422. return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
  423. }
  424. static void clear_translation_pre_enabled(struct intel_iommu *iommu)
  425. {
  426. iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
  427. }
  428. static void init_translation_status(struct intel_iommu *iommu)
  429. {
  430. u32 gsts;
  431. gsts = readl(iommu->reg + DMAR_GSTS_REG);
  432. if (gsts & DMA_GSTS_TES)
  433. iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
  434. }
  435. /* Convert generic 'struct iommu_domain to private struct dmar_domain */
  436. static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
  437. {
  438. return container_of(dom, struct dmar_domain, domain);
  439. }
  440. static int __init intel_iommu_setup(char *str)
  441. {
  442. if (!str)
  443. return -EINVAL;
  444. while (*str) {
  445. if (!strncmp(str, "on", 2)) {
  446. dmar_disabled = 0;
  447. pr_info("IOMMU enabled\n");
  448. } else if (!strncmp(str, "off", 3)) {
  449. dmar_disabled = 1;
  450. pr_info("IOMMU disabled\n");
  451. } else if (!strncmp(str, "igfx_off", 8)) {
  452. dmar_map_gfx = 0;
  453. pr_info("Disable GFX device mapping\n");
  454. } else if (!strncmp(str, "forcedac", 8)) {
  455. pr_info("Forcing DAC for PCI devices\n");
  456. dmar_forcedac = 1;
  457. } else if (!strncmp(str, "strict", 6)) {
  458. pr_info("Disable batched IOTLB flush\n");
  459. intel_iommu_strict = 1;
  460. } else if (!strncmp(str, "sp_off", 6)) {
  461. pr_info("Disable supported super page\n");
  462. intel_iommu_superpage = 0;
  463. } else if (!strncmp(str, "ecs_off", 7)) {
  464. printk(KERN_INFO
  465. "Intel-IOMMU: disable extended context table support\n");
  466. intel_iommu_ecs = 0;
  467. } else if (!strncmp(str, "pasid28", 7)) {
  468. printk(KERN_INFO
  469. "Intel-IOMMU: enable pre-production PASID support\n");
  470. intel_iommu_pasid28 = 1;
  471. iommu_identity_mapping |= IDENTMAP_GFX;
  472. } else if (!strncmp(str, "tboot_noforce", 13)) {
  473. printk(KERN_INFO
  474. "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
  475. intel_iommu_tboot_noforce = 1;
  476. }
  477. str += strcspn(str, ",");
  478. while (*str == ',')
  479. str++;
  480. }
  481. return 0;
  482. }
  483. __setup("intel_iommu=", intel_iommu_setup);
  484. static struct kmem_cache *iommu_domain_cache;
  485. static struct kmem_cache *iommu_devinfo_cache;
  486. static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
  487. {
  488. struct dmar_domain **domains;
  489. int idx = did >> 8;
  490. domains = iommu->domains[idx];
  491. if (!domains)
  492. return NULL;
  493. return domains[did & 0xff];
  494. }
  495. static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
  496. struct dmar_domain *domain)
  497. {
  498. struct dmar_domain **domains;
  499. int idx = did >> 8;
  500. if (!iommu->domains[idx]) {
  501. size_t size = 256 * sizeof(struct dmar_domain *);
  502. iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
  503. }
  504. domains = iommu->domains[idx];
  505. if (WARN_ON(!domains))
  506. return;
  507. else
  508. domains[did & 0xff] = domain;
  509. }
  510. void *alloc_pgtable_page(int node)
  511. {
  512. struct page *page;
  513. void *vaddr = NULL;
  514. page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
  515. if (page)
  516. vaddr = page_address(page);
  517. return vaddr;
  518. }
  519. void free_pgtable_page(void *vaddr)
  520. {
  521. free_page((unsigned long)vaddr);
  522. }
  523. static inline void *alloc_domain_mem(void)
  524. {
  525. return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
  526. }
  527. static void free_domain_mem(void *vaddr)
  528. {
  529. kmem_cache_free(iommu_domain_cache, vaddr);
  530. }
  531. static inline void * alloc_devinfo_mem(void)
  532. {
  533. return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
  534. }
  535. static inline void free_devinfo_mem(void *vaddr)
  536. {
  537. kmem_cache_free(iommu_devinfo_cache, vaddr);
  538. }
  539. static inline int domain_type_is_vm(struct dmar_domain *domain)
  540. {
  541. return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
  542. }
  543. static inline int domain_type_is_si(struct dmar_domain *domain)
  544. {
  545. return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
  546. }
  547. static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
  548. {
  549. return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
  550. DOMAIN_FLAG_STATIC_IDENTITY);
  551. }
  552. static inline int domain_pfn_supported(struct dmar_domain *domain,
  553. unsigned long pfn)
  554. {
  555. int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
  556. return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
  557. }
  558. static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
  559. {
  560. unsigned long sagaw;
  561. int agaw = -1;
  562. sagaw = cap_sagaw(iommu->cap);
  563. for (agaw = width_to_agaw(max_gaw);
  564. agaw >= 0; agaw--) {
  565. if (test_bit(agaw, &sagaw))
  566. break;
  567. }
  568. return agaw;
  569. }
  570. /*
  571. * Calculate max SAGAW for each iommu.
  572. */
  573. int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
  574. {
  575. return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
  576. }
  577. /*
  578. * calculate agaw for each iommu.
  579. * "SAGAW" may be different across iommus, use a default agaw, and
  580. * get a supported less agaw for iommus that don't support the default agaw.
  581. */
  582. int iommu_calculate_agaw(struct intel_iommu *iommu)
  583. {
  584. return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
  585. }
  586. /* This functionin only returns single iommu in a domain */
  587. struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
  588. {
  589. int iommu_id;
  590. /* si_domain and vm domain should not get here. */
  591. BUG_ON(domain_type_is_vm_or_si(domain));
  592. for_each_domain_iommu(iommu_id, domain)
  593. break;
  594. if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
  595. return NULL;
  596. return g_iommus[iommu_id];
  597. }
  598. static void domain_update_iommu_coherency(struct dmar_domain *domain)
  599. {
  600. struct dmar_drhd_unit *drhd;
  601. struct intel_iommu *iommu;
  602. bool found = false;
  603. int i;
  604. domain->iommu_coherency = 1;
  605. for_each_domain_iommu(i, domain) {
  606. found = true;
  607. if (!ecap_coherent(g_iommus[i]->ecap)) {
  608. domain->iommu_coherency = 0;
  609. break;
  610. }
  611. }
  612. if (found)
  613. return;
  614. /* No hardware attached; use lowest common denominator */
  615. rcu_read_lock();
  616. for_each_active_iommu(iommu, drhd) {
  617. if (!ecap_coherent(iommu->ecap)) {
  618. domain->iommu_coherency = 0;
  619. break;
  620. }
  621. }
  622. rcu_read_unlock();
  623. }
  624. static int domain_update_iommu_snooping(struct intel_iommu *skip)
  625. {
  626. struct dmar_drhd_unit *drhd;
  627. struct intel_iommu *iommu;
  628. int ret = 1;
  629. rcu_read_lock();
  630. for_each_active_iommu(iommu, drhd) {
  631. if (iommu != skip) {
  632. if (!ecap_sc_support(iommu->ecap)) {
  633. ret = 0;
  634. break;
  635. }
  636. }
  637. }
  638. rcu_read_unlock();
  639. return ret;
  640. }
  641. static int domain_update_iommu_superpage(struct intel_iommu *skip)
  642. {
  643. struct dmar_drhd_unit *drhd;
  644. struct intel_iommu *iommu;
  645. int mask = 0xf;
  646. if (!intel_iommu_superpage) {
  647. return 0;
  648. }
  649. /* set iommu_superpage to the smallest common denominator */
  650. rcu_read_lock();
  651. for_each_active_iommu(iommu, drhd) {
  652. if (iommu != skip) {
  653. mask &= cap_super_page_val(iommu->cap);
  654. if (!mask)
  655. break;
  656. }
  657. }
  658. rcu_read_unlock();
  659. return fls(mask);
  660. }
  661. /* Some capabilities may be different across iommus */
  662. static void domain_update_iommu_cap(struct dmar_domain *domain)
  663. {
  664. domain_update_iommu_coherency(domain);
  665. domain->iommu_snooping = domain_update_iommu_snooping(NULL);
  666. domain->iommu_superpage = domain_update_iommu_superpage(NULL);
  667. }
  668. static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
  669. u8 bus, u8 devfn, int alloc)
  670. {
  671. struct root_entry *root = &iommu->root_entry[bus];
  672. struct context_entry *context;
  673. u64 *entry;
  674. entry = &root->lo;
  675. if (ecs_enabled(iommu)) {
  676. if (devfn >= 0x80) {
  677. devfn -= 0x80;
  678. entry = &root->hi;
  679. }
  680. devfn *= 2;
  681. }
  682. if (*entry & 1)
  683. context = phys_to_virt(*entry & VTD_PAGE_MASK);
  684. else {
  685. unsigned long phy_addr;
  686. if (!alloc)
  687. return NULL;
  688. context = alloc_pgtable_page(iommu->node);
  689. if (!context)
  690. return NULL;
  691. __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
  692. phy_addr = virt_to_phys((void *)context);
  693. *entry = phy_addr | 1;
  694. __iommu_flush_cache(iommu, entry, sizeof(*entry));
  695. }
  696. return &context[devfn];
  697. }
  698. static int iommu_dummy(struct device *dev)
  699. {
  700. return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
  701. }
  702. static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
  703. {
  704. struct dmar_drhd_unit *drhd = NULL;
  705. struct intel_iommu *iommu;
  706. struct device *tmp;
  707. struct pci_dev *ptmp, *pdev = NULL;
  708. u16 segment = 0;
  709. int i;
  710. if (iommu_dummy(dev))
  711. return NULL;
  712. if (dev_is_pci(dev)) {
  713. struct pci_dev *pf_pdev;
  714. pdev = to_pci_dev(dev);
  715. #ifdef CONFIG_X86
  716. /* VMD child devices currently cannot be handled individually */
  717. if (is_vmd(pdev->bus))
  718. return NULL;
  719. #endif
  720. /* VFs aren't listed in scope tables; we need to look up
  721. * the PF instead to find the IOMMU. */
  722. pf_pdev = pci_physfn(pdev);
  723. dev = &pf_pdev->dev;
  724. segment = pci_domain_nr(pdev->bus);
  725. } else if (has_acpi_companion(dev))
  726. dev = &ACPI_COMPANION(dev)->dev;
  727. rcu_read_lock();
  728. for_each_active_iommu(iommu, drhd) {
  729. if (pdev && segment != drhd->segment)
  730. continue;
  731. for_each_active_dev_scope(drhd->devices,
  732. drhd->devices_cnt, i, tmp) {
  733. if (tmp == dev) {
  734. /* For a VF use its original BDF# not that of the PF
  735. * which we used for the IOMMU lookup. Strictly speaking
  736. * we could do this for all PCI devices; we only need to
  737. * get the BDF# from the scope table for ACPI matches. */
  738. if (pdev && pdev->is_virtfn)
  739. goto got_pdev;
  740. *bus = drhd->devices[i].bus;
  741. *devfn = drhd->devices[i].devfn;
  742. goto out;
  743. }
  744. if (!pdev || !dev_is_pci(tmp))
  745. continue;
  746. ptmp = to_pci_dev(tmp);
  747. if (ptmp->subordinate &&
  748. ptmp->subordinate->number <= pdev->bus->number &&
  749. ptmp->subordinate->busn_res.end >= pdev->bus->number)
  750. goto got_pdev;
  751. }
  752. if (pdev && drhd->include_all) {
  753. got_pdev:
  754. *bus = pdev->bus->number;
  755. *devfn = pdev->devfn;
  756. goto out;
  757. }
  758. }
  759. iommu = NULL;
  760. out:
  761. rcu_read_unlock();
  762. return iommu;
  763. }
  764. static void domain_flush_cache(struct dmar_domain *domain,
  765. void *addr, int size)
  766. {
  767. if (!domain->iommu_coherency)
  768. clflush_cache_range(addr, size);
  769. }
  770. static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
  771. {
  772. struct context_entry *context;
  773. int ret = 0;
  774. unsigned long flags;
  775. spin_lock_irqsave(&iommu->lock, flags);
  776. context = iommu_context_addr(iommu, bus, devfn, 0);
  777. if (context)
  778. ret = context_present(context);
  779. spin_unlock_irqrestore(&iommu->lock, flags);
  780. return ret;
  781. }
  782. static void free_context_table(struct intel_iommu *iommu)
  783. {
  784. int i;
  785. unsigned long flags;
  786. struct context_entry *context;
  787. spin_lock_irqsave(&iommu->lock, flags);
  788. if (!iommu->root_entry) {
  789. goto out;
  790. }
  791. for (i = 0; i < ROOT_ENTRY_NR; i++) {
  792. context = iommu_context_addr(iommu, i, 0, 0);
  793. if (context)
  794. free_pgtable_page(context);
  795. if (!ecs_enabled(iommu))
  796. continue;
  797. context = iommu_context_addr(iommu, i, 0x80, 0);
  798. if (context)
  799. free_pgtable_page(context);
  800. }
  801. free_pgtable_page(iommu->root_entry);
  802. iommu->root_entry = NULL;
  803. out:
  804. spin_unlock_irqrestore(&iommu->lock, flags);
  805. }
  806. static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
  807. unsigned long pfn, int *target_level)
  808. {
  809. struct dma_pte *parent, *pte = NULL;
  810. int level = agaw_to_level(domain->agaw);
  811. int offset;
  812. BUG_ON(!domain->pgd);
  813. if (!domain_pfn_supported(domain, pfn))
  814. /* Address beyond IOMMU's addressing capabilities. */
  815. return NULL;
  816. parent = domain->pgd;
  817. while (1) {
  818. void *tmp_page;
  819. offset = pfn_level_offset(pfn, level);
  820. pte = &parent[offset];
  821. if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
  822. break;
  823. if (level == *target_level)
  824. break;
  825. if (!dma_pte_present(pte)) {
  826. uint64_t pteval;
  827. tmp_page = alloc_pgtable_page(domain->nid);
  828. if (!tmp_page)
  829. return NULL;
  830. domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
  831. pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
  832. if (cmpxchg64(&pte->val, 0ULL, pteval))
  833. /* Someone else set it while we were thinking; use theirs. */
  834. free_pgtable_page(tmp_page);
  835. else
  836. domain_flush_cache(domain, pte, sizeof(*pte));
  837. }
  838. if (level == 1)
  839. break;
  840. parent = phys_to_virt(dma_pte_addr(pte));
  841. level--;
  842. }
  843. if (!*target_level)
  844. *target_level = level;
  845. return pte;
  846. }
  847. /* return address's pte at specific level */
  848. static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
  849. unsigned long pfn,
  850. int level, int *large_page)
  851. {
  852. struct dma_pte *parent, *pte = NULL;
  853. int total = agaw_to_level(domain->agaw);
  854. int offset;
  855. parent = domain->pgd;
  856. while (level <= total) {
  857. offset = pfn_level_offset(pfn, total);
  858. pte = &parent[offset];
  859. if (level == total)
  860. return pte;
  861. if (!dma_pte_present(pte)) {
  862. *large_page = total;
  863. break;
  864. }
  865. if (dma_pte_superpage(pte)) {
  866. *large_page = total;
  867. return pte;
  868. }
  869. parent = phys_to_virt(dma_pte_addr(pte));
  870. total--;
  871. }
  872. return NULL;
  873. }
  874. /* clear last level pte, a tlb flush should be followed */
  875. static void dma_pte_clear_range(struct dmar_domain *domain,
  876. unsigned long start_pfn,
  877. unsigned long last_pfn)
  878. {
  879. unsigned int large_page = 1;
  880. struct dma_pte *first_pte, *pte;
  881. BUG_ON(!domain_pfn_supported(domain, start_pfn));
  882. BUG_ON(!domain_pfn_supported(domain, last_pfn));
  883. BUG_ON(start_pfn > last_pfn);
  884. /* we don't need lock here; nobody else touches the iova range */
  885. do {
  886. large_page = 1;
  887. first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
  888. if (!pte) {
  889. start_pfn = align_to_level(start_pfn + 1, large_page + 1);
  890. continue;
  891. }
  892. do {
  893. dma_clear_pte(pte);
  894. start_pfn += lvl_to_nr_pages(large_page);
  895. pte++;
  896. } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
  897. domain_flush_cache(domain, first_pte,
  898. (void *)pte - (void *)first_pte);
  899. } while (start_pfn && start_pfn <= last_pfn);
  900. }
  901. static void dma_pte_free_level(struct dmar_domain *domain, int level,
  902. int retain_level, struct dma_pte *pte,
  903. unsigned long pfn, unsigned long start_pfn,
  904. unsigned long last_pfn)
  905. {
  906. pfn = max(start_pfn, pfn);
  907. pte = &pte[pfn_level_offset(pfn, level)];
  908. do {
  909. unsigned long level_pfn;
  910. struct dma_pte *level_pte;
  911. if (!dma_pte_present(pte) || dma_pte_superpage(pte))
  912. goto next;
  913. level_pfn = pfn & level_mask(level);
  914. level_pte = phys_to_virt(dma_pte_addr(pte));
  915. if (level > 2) {
  916. dma_pte_free_level(domain, level - 1, retain_level,
  917. level_pte, level_pfn, start_pfn,
  918. last_pfn);
  919. }
  920. /*
  921. * Free the page table if we're below the level we want to
  922. * retain and the range covers the entire table.
  923. */
  924. if (level < retain_level && !(start_pfn > level_pfn ||
  925. last_pfn < level_pfn + level_size(level) - 1)) {
  926. dma_clear_pte(pte);
  927. domain_flush_cache(domain, pte, sizeof(*pte));
  928. free_pgtable_page(level_pte);
  929. }
  930. next:
  931. pfn += level_size(level);
  932. } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
  933. }
  934. /*
  935. * clear last level (leaf) ptes and free page table pages below the
  936. * level we wish to keep intact.
  937. */
  938. static void dma_pte_free_pagetable(struct dmar_domain *domain,
  939. unsigned long start_pfn,
  940. unsigned long last_pfn,
  941. int retain_level)
  942. {
  943. BUG_ON(!domain_pfn_supported(domain, start_pfn));
  944. BUG_ON(!domain_pfn_supported(domain, last_pfn));
  945. BUG_ON(start_pfn > last_pfn);
  946. dma_pte_clear_range(domain, start_pfn, last_pfn);
  947. /* We don't need lock here; nobody else touches the iova range */
  948. dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level,
  949. domain->pgd, 0, start_pfn, last_pfn);
  950. /* free pgd */
  951. if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
  952. free_pgtable_page(domain->pgd);
  953. domain->pgd = NULL;
  954. }
  955. }
  956. /* When a page at a given level is being unlinked from its parent, we don't
  957. need to *modify* it at all. All we need to do is make a list of all the
  958. pages which can be freed just as soon as we've flushed the IOTLB and we
  959. know the hardware page-walk will no longer touch them.
  960. The 'pte' argument is the *parent* PTE, pointing to the page that is to
  961. be freed. */
  962. static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
  963. int level, struct dma_pte *pte,
  964. struct page *freelist)
  965. {
  966. struct page *pg;
  967. pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
  968. pg->freelist = freelist;
  969. freelist = pg;
  970. if (level == 1)
  971. return freelist;
  972. pte = page_address(pg);
  973. do {
  974. if (dma_pte_present(pte) && !dma_pte_superpage(pte))
  975. freelist = dma_pte_list_pagetables(domain, level - 1,
  976. pte, freelist);
  977. pte++;
  978. } while (!first_pte_in_page(pte));
  979. return freelist;
  980. }
  981. static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
  982. struct dma_pte *pte, unsigned long pfn,
  983. unsigned long start_pfn,
  984. unsigned long last_pfn,
  985. struct page *freelist)
  986. {
  987. struct dma_pte *first_pte = NULL, *last_pte = NULL;
  988. pfn = max(start_pfn, pfn);
  989. pte = &pte[pfn_level_offset(pfn, level)];
  990. do {
  991. unsigned long level_pfn;
  992. if (!dma_pte_present(pte))
  993. goto next;
  994. level_pfn = pfn & level_mask(level);
  995. /* If range covers entire pagetable, free it */
  996. if (start_pfn <= level_pfn &&
  997. last_pfn >= level_pfn + level_size(level) - 1) {
  998. /* These suborbinate page tables are going away entirely. Don't
  999. bother to clear them; we're just going to *free* them. */
  1000. if (level > 1 && !dma_pte_superpage(pte))
  1001. freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
  1002. dma_clear_pte(pte);
  1003. if (!first_pte)
  1004. first_pte = pte;
  1005. last_pte = pte;
  1006. } else if (level > 1) {
  1007. /* Recurse down into a level that isn't *entirely* obsolete */
  1008. freelist = dma_pte_clear_level(domain, level - 1,
  1009. phys_to_virt(dma_pte_addr(pte)),
  1010. level_pfn, start_pfn, last_pfn,
  1011. freelist);
  1012. }
  1013. next:
  1014. pfn += level_size(level);
  1015. } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
  1016. if (first_pte)
  1017. domain_flush_cache(domain, first_pte,
  1018. (void *)++last_pte - (void *)first_pte);
  1019. return freelist;
  1020. }
  1021. /* We can't just free the pages because the IOMMU may still be walking
  1022. the page tables, and may have cached the intermediate levels. The
  1023. pages can only be freed after the IOTLB flush has been done. */
  1024. static struct page *domain_unmap(struct dmar_domain *domain,
  1025. unsigned long start_pfn,
  1026. unsigned long last_pfn)
  1027. {
  1028. struct page *freelist = NULL;
  1029. BUG_ON(!domain_pfn_supported(domain, start_pfn));
  1030. BUG_ON(!domain_pfn_supported(domain, last_pfn));
  1031. BUG_ON(start_pfn > last_pfn);
  1032. /* we don't need lock here; nobody else touches the iova range */
  1033. freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
  1034. domain->pgd, 0, start_pfn, last_pfn, NULL);
  1035. /* free pgd */
  1036. if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
  1037. struct page *pgd_page = virt_to_page(domain->pgd);
  1038. pgd_page->freelist = freelist;
  1039. freelist = pgd_page;
  1040. domain->pgd = NULL;
  1041. }
  1042. return freelist;
  1043. }
  1044. static void dma_free_pagelist(struct page *freelist)
  1045. {
  1046. struct page *pg;
  1047. while ((pg = freelist)) {
  1048. freelist = pg->freelist;
  1049. free_pgtable_page(page_address(pg));
  1050. }
  1051. }
  1052. static void iova_entry_free(unsigned long data)
  1053. {
  1054. struct page *freelist = (struct page *)data;
  1055. dma_free_pagelist(freelist);
  1056. }
  1057. /* iommu handling */
  1058. static int iommu_alloc_root_entry(struct intel_iommu *iommu)
  1059. {
  1060. struct root_entry *root;
  1061. unsigned long flags;
  1062. root = (struct root_entry *)alloc_pgtable_page(iommu->node);
  1063. if (!root) {
  1064. pr_err("Allocating root entry for %s failed\n",
  1065. iommu->name);
  1066. return -ENOMEM;
  1067. }
  1068. __iommu_flush_cache(iommu, root, ROOT_SIZE);
  1069. spin_lock_irqsave(&iommu->lock, flags);
  1070. iommu->root_entry = root;
  1071. spin_unlock_irqrestore(&iommu->lock, flags);
  1072. return 0;
  1073. }
  1074. static void iommu_set_root_entry(struct intel_iommu *iommu)
  1075. {
  1076. u64 addr;
  1077. u32 sts;
  1078. unsigned long flag;
  1079. addr = virt_to_phys(iommu->root_entry);
  1080. if (ecs_enabled(iommu))
  1081. addr |= DMA_RTADDR_RTT;
  1082. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1083. dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
  1084. writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
  1085. /* Make sure hardware complete it */
  1086. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  1087. readl, (sts & DMA_GSTS_RTPS), sts);
  1088. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1089. }
  1090. static void iommu_flush_write_buffer(struct intel_iommu *iommu)
  1091. {
  1092. u32 val;
  1093. unsigned long flag;
  1094. if (!rwbf_quirk && !cap_rwbf(iommu->cap))
  1095. return;
  1096. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1097. writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
  1098. /* Make sure hardware complete it */
  1099. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  1100. readl, (!(val & DMA_GSTS_WBFS)), val);
  1101. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1102. }
  1103. /* return value determine if we need a write buffer flush */
  1104. static void __iommu_flush_context(struct intel_iommu *iommu,
  1105. u16 did, u16 source_id, u8 function_mask,
  1106. u64 type)
  1107. {
  1108. u64 val = 0;
  1109. unsigned long flag;
  1110. switch (type) {
  1111. case DMA_CCMD_GLOBAL_INVL:
  1112. val = DMA_CCMD_GLOBAL_INVL;
  1113. break;
  1114. case DMA_CCMD_DOMAIN_INVL:
  1115. val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
  1116. break;
  1117. case DMA_CCMD_DEVICE_INVL:
  1118. val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
  1119. | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
  1120. break;
  1121. default:
  1122. BUG();
  1123. }
  1124. val |= DMA_CCMD_ICC;
  1125. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1126. dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
  1127. /* Make sure hardware complete it */
  1128. IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
  1129. dmar_readq, (!(val & DMA_CCMD_ICC)), val);
  1130. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1131. }
  1132. /* return value determine if we need a write buffer flush */
  1133. static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
  1134. u64 addr, unsigned int size_order, u64 type)
  1135. {
  1136. int tlb_offset = ecap_iotlb_offset(iommu->ecap);
  1137. u64 val = 0, val_iva = 0;
  1138. unsigned long flag;
  1139. switch (type) {
  1140. case DMA_TLB_GLOBAL_FLUSH:
  1141. /* global flush doesn't need set IVA_REG */
  1142. val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
  1143. break;
  1144. case DMA_TLB_DSI_FLUSH:
  1145. val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
  1146. break;
  1147. case DMA_TLB_PSI_FLUSH:
  1148. val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
  1149. /* IH bit is passed in as part of address */
  1150. val_iva = size_order | addr;
  1151. break;
  1152. default:
  1153. BUG();
  1154. }
  1155. /* Note: set drain read/write */
  1156. #if 0
  1157. /*
  1158. * This is probably to be super secure.. Looks like we can
  1159. * ignore it without any impact.
  1160. */
  1161. if (cap_read_drain(iommu->cap))
  1162. val |= DMA_TLB_READ_DRAIN;
  1163. #endif
  1164. if (cap_write_drain(iommu->cap))
  1165. val |= DMA_TLB_WRITE_DRAIN;
  1166. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1167. /* Note: Only uses first TLB reg currently */
  1168. if (val_iva)
  1169. dmar_writeq(iommu->reg + tlb_offset, val_iva);
  1170. dmar_writeq(iommu->reg + tlb_offset + 8, val);
  1171. /* Make sure hardware complete it */
  1172. IOMMU_WAIT_OP(iommu, tlb_offset + 8,
  1173. dmar_readq, (!(val & DMA_TLB_IVT)), val);
  1174. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1175. /* check IOTLB invalidation granularity */
  1176. if (DMA_TLB_IAIG(val) == 0)
  1177. pr_err("Flush IOTLB failed\n");
  1178. if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
  1179. pr_debug("TLB flush request %Lx, actual %Lx\n",
  1180. (unsigned long long)DMA_TLB_IIRG(type),
  1181. (unsigned long long)DMA_TLB_IAIG(val));
  1182. }
  1183. static struct device_domain_info *
  1184. iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
  1185. u8 bus, u8 devfn)
  1186. {
  1187. struct device_domain_info *info;
  1188. assert_spin_locked(&device_domain_lock);
  1189. if (!iommu->qi)
  1190. return NULL;
  1191. list_for_each_entry(info, &domain->devices, link)
  1192. if (info->iommu == iommu && info->bus == bus &&
  1193. info->devfn == devfn) {
  1194. if (info->ats_supported && info->dev)
  1195. return info;
  1196. break;
  1197. }
  1198. return NULL;
  1199. }
  1200. static void domain_update_iotlb(struct dmar_domain *domain)
  1201. {
  1202. struct device_domain_info *info;
  1203. bool has_iotlb_device = false;
  1204. assert_spin_locked(&device_domain_lock);
  1205. list_for_each_entry(info, &domain->devices, link) {
  1206. struct pci_dev *pdev;
  1207. if (!info->dev || !dev_is_pci(info->dev))
  1208. continue;
  1209. pdev = to_pci_dev(info->dev);
  1210. if (pdev->ats_enabled) {
  1211. has_iotlb_device = true;
  1212. break;
  1213. }
  1214. }
  1215. domain->has_iotlb_device = has_iotlb_device;
  1216. }
  1217. static void iommu_enable_dev_iotlb(struct device_domain_info *info)
  1218. {
  1219. struct pci_dev *pdev;
  1220. assert_spin_locked(&device_domain_lock);
  1221. if (!info || !dev_is_pci(info->dev))
  1222. return;
  1223. pdev = to_pci_dev(info->dev);
  1224. /* For IOMMU that supports device IOTLB throttling (DIT), we assign
  1225. * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge
  1226. * queue depth at PF level. If DIT is not set, PFSID will be treated as
  1227. * reserved, which should be set to 0.
  1228. */
  1229. if (!ecap_dit(info->iommu->ecap))
  1230. info->pfsid = 0;
  1231. else {
  1232. struct pci_dev *pf_pdev;
  1233. /* pdev will be returned if device is not a vf */
  1234. pf_pdev = pci_physfn(pdev);
  1235. info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn);
  1236. }
  1237. #ifdef CONFIG_INTEL_IOMMU_SVM
  1238. /* The PCIe spec, in its wisdom, declares that the behaviour of
  1239. the device if you enable PASID support after ATS support is
  1240. undefined. So always enable PASID support on devices which
  1241. have it, even if we can't yet know if we're ever going to
  1242. use it. */
  1243. if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
  1244. info->pasid_enabled = 1;
  1245. if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
  1246. info->pri_enabled = 1;
  1247. #endif
  1248. if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
  1249. info->ats_enabled = 1;
  1250. domain_update_iotlb(info->domain);
  1251. info->ats_qdep = pci_ats_queue_depth(pdev);
  1252. }
  1253. }
  1254. static void iommu_disable_dev_iotlb(struct device_domain_info *info)
  1255. {
  1256. struct pci_dev *pdev;
  1257. assert_spin_locked(&device_domain_lock);
  1258. if (!dev_is_pci(info->dev))
  1259. return;
  1260. pdev = to_pci_dev(info->dev);
  1261. if (info->ats_enabled) {
  1262. pci_disable_ats(pdev);
  1263. info->ats_enabled = 0;
  1264. domain_update_iotlb(info->domain);
  1265. }
  1266. #ifdef CONFIG_INTEL_IOMMU_SVM
  1267. if (info->pri_enabled) {
  1268. pci_disable_pri(pdev);
  1269. info->pri_enabled = 0;
  1270. }
  1271. if (info->pasid_enabled) {
  1272. pci_disable_pasid(pdev);
  1273. info->pasid_enabled = 0;
  1274. }
  1275. #endif
  1276. }
  1277. static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
  1278. u64 addr, unsigned mask)
  1279. {
  1280. u16 sid, qdep;
  1281. unsigned long flags;
  1282. struct device_domain_info *info;
  1283. if (!domain->has_iotlb_device)
  1284. return;
  1285. spin_lock_irqsave(&device_domain_lock, flags);
  1286. list_for_each_entry(info, &domain->devices, link) {
  1287. if (!info->ats_enabled)
  1288. continue;
  1289. sid = info->bus << 8 | info->devfn;
  1290. qdep = info->ats_qdep;
  1291. qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
  1292. qdep, addr, mask);
  1293. }
  1294. spin_unlock_irqrestore(&device_domain_lock, flags);
  1295. }
  1296. static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
  1297. struct dmar_domain *domain,
  1298. unsigned long pfn, unsigned int pages,
  1299. int ih, int map)
  1300. {
  1301. unsigned int mask = ilog2(__roundup_pow_of_two(pages));
  1302. uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
  1303. u16 did = domain->iommu_did[iommu->seq_id];
  1304. BUG_ON(pages == 0);
  1305. if (ih)
  1306. ih = 1 << 6;
  1307. /*
  1308. * Fallback to domain selective flush if no PSI support or the size is
  1309. * too big.
  1310. * PSI requires page size to be 2 ^ x, and the base address is naturally
  1311. * aligned to the size
  1312. */
  1313. if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
  1314. iommu->flush.flush_iotlb(iommu, did, 0, 0,
  1315. DMA_TLB_DSI_FLUSH);
  1316. else
  1317. iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
  1318. DMA_TLB_PSI_FLUSH);
  1319. /*
  1320. * In caching mode, changes of pages from non-present to present require
  1321. * flush. However, device IOTLB doesn't need to be flushed in this case.
  1322. */
  1323. if (!cap_caching_mode(iommu->cap) || !map)
  1324. iommu_flush_dev_iotlb(domain, addr, mask);
  1325. }
  1326. /* Notification for newly created mappings */
  1327. static inline void __mapping_notify_one(struct intel_iommu *iommu,
  1328. struct dmar_domain *domain,
  1329. unsigned long pfn, unsigned int pages)
  1330. {
  1331. /* It's a non-present to present mapping. Only flush if caching mode */
  1332. if (cap_caching_mode(iommu->cap))
  1333. iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
  1334. else
  1335. iommu_flush_write_buffer(iommu);
  1336. }
  1337. static void iommu_flush_iova(struct iova_domain *iovad)
  1338. {
  1339. struct dmar_domain *domain;
  1340. int idx;
  1341. domain = container_of(iovad, struct dmar_domain, iovad);
  1342. for_each_domain_iommu(idx, domain) {
  1343. struct intel_iommu *iommu = g_iommus[idx];
  1344. u16 did = domain->iommu_did[iommu->seq_id];
  1345. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  1346. if (!cap_caching_mode(iommu->cap))
  1347. iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
  1348. 0, MAX_AGAW_PFN_WIDTH);
  1349. }
  1350. }
  1351. static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
  1352. {
  1353. u32 pmen;
  1354. unsigned long flags;
  1355. if (!cap_plmr(iommu->cap) && !cap_phmr(iommu->cap))
  1356. return;
  1357. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  1358. pmen = readl(iommu->reg + DMAR_PMEN_REG);
  1359. pmen &= ~DMA_PMEN_EPM;
  1360. writel(pmen, iommu->reg + DMAR_PMEN_REG);
  1361. /* wait for the protected region status bit to clear */
  1362. IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
  1363. readl, !(pmen & DMA_PMEN_PRS), pmen);
  1364. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  1365. }
  1366. static void iommu_enable_translation(struct intel_iommu *iommu)
  1367. {
  1368. u32 sts;
  1369. unsigned long flags;
  1370. raw_spin_lock_irqsave(&iommu->register_lock, flags);
  1371. iommu->gcmd |= DMA_GCMD_TE;
  1372. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  1373. /* Make sure hardware complete it */
  1374. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  1375. readl, (sts & DMA_GSTS_TES), sts);
  1376. raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
  1377. }
  1378. static void iommu_disable_translation(struct intel_iommu *iommu)
  1379. {
  1380. u32 sts;
  1381. unsigned long flag;
  1382. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  1383. iommu->gcmd &= ~DMA_GCMD_TE;
  1384. writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
  1385. /* Make sure hardware complete it */
  1386. IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
  1387. readl, (!(sts & DMA_GSTS_TES)), sts);
  1388. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  1389. }
  1390. static int iommu_init_domains(struct intel_iommu *iommu)
  1391. {
  1392. u32 ndomains, nlongs;
  1393. size_t size;
  1394. ndomains = cap_ndoms(iommu->cap);
  1395. pr_debug("%s: Number of Domains supported <%d>\n",
  1396. iommu->name, ndomains);
  1397. nlongs = BITS_TO_LONGS(ndomains);
  1398. spin_lock_init(&iommu->lock);
  1399. iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
  1400. if (!iommu->domain_ids) {
  1401. pr_err("%s: Allocating domain id array failed\n",
  1402. iommu->name);
  1403. return -ENOMEM;
  1404. }
  1405. size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
  1406. iommu->domains = kzalloc(size, GFP_KERNEL);
  1407. if (iommu->domains) {
  1408. size = 256 * sizeof(struct dmar_domain *);
  1409. iommu->domains[0] = kzalloc(size, GFP_KERNEL);
  1410. }
  1411. if (!iommu->domains || !iommu->domains[0]) {
  1412. pr_err("%s: Allocating domain array failed\n",
  1413. iommu->name);
  1414. kfree(iommu->domain_ids);
  1415. kfree(iommu->domains);
  1416. iommu->domain_ids = NULL;
  1417. iommu->domains = NULL;
  1418. return -ENOMEM;
  1419. }
  1420. /*
  1421. * If Caching mode is set, then invalid translations are tagged
  1422. * with domain-id 0, hence we need to pre-allocate it. We also
  1423. * use domain-id 0 as a marker for non-allocated domain-id, so
  1424. * make sure it is not used for a real domain.
  1425. */
  1426. set_bit(0, iommu->domain_ids);
  1427. return 0;
  1428. }
  1429. static void disable_dmar_iommu(struct intel_iommu *iommu)
  1430. {
  1431. struct device_domain_info *info, *tmp;
  1432. unsigned long flags;
  1433. if (!iommu->domains || !iommu->domain_ids)
  1434. return;
  1435. again:
  1436. spin_lock_irqsave(&device_domain_lock, flags);
  1437. list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
  1438. struct dmar_domain *domain;
  1439. if (info->iommu != iommu)
  1440. continue;
  1441. if (!info->dev || !info->domain)
  1442. continue;
  1443. domain = info->domain;
  1444. __dmar_remove_one_dev_info(info);
  1445. if (!domain_type_is_vm_or_si(domain)) {
  1446. /*
  1447. * The domain_exit() function can't be called under
  1448. * device_domain_lock, as it takes this lock itself.
  1449. * So release the lock here and re-run the loop
  1450. * afterwards.
  1451. */
  1452. spin_unlock_irqrestore(&device_domain_lock, flags);
  1453. domain_exit(domain);
  1454. goto again;
  1455. }
  1456. }
  1457. spin_unlock_irqrestore(&device_domain_lock, flags);
  1458. if (iommu->gcmd & DMA_GCMD_TE)
  1459. iommu_disable_translation(iommu);
  1460. }
  1461. static void free_dmar_iommu(struct intel_iommu *iommu)
  1462. {
  1463. if ((iommu->domains) && (iommu->domain_ids)) {
  1464. int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
  1465. int i;
  1466. for (i = 0; i < elems; i++)
  1467. kfree(iommu->domains[i]);
  1468. kfree(iommu->domains);
  1469. kfree(iommu->domain_ids);
  1470. iommu->domains = NULL;
  1471. iommu->domain_ids = NULL;
  1472. }
  1473. g_iommus[iommu->seq_id] = NULL;
  1474. /* free context mapping */
  1475. free_context_table(iommu);
  1476. #ifdef CONFIG_INTEL_IOMMU_SVM
  1477. if (pasid_enabled(iommu)) {
  1478. if (ecap_prs(iommu->ecap))
  1479. intel_svm_finish_prq(iommu);
  1480. intel_svm_exit(iommu);
  1481. }
  1482. #endif
  1483. }
  1484. static struct dmar_domain *alloc_domain(int flags)
  1485. {
  1486. struct dmar_domain *domain;
  1487. domain = alloc_domain_mem();
  1488. if (!domain)
  1489. return NULL;
  1490. memset(domain, 0, sizeof(*domain));
  1491. domain->nid = -1;
  1492. domain->flags = flags;
  1493. domain->has_iotlb_device = false;
  1494. INIT_LIST_HEAD(&domain->devices);
  1495. return domain;
  1496. }
  1497. /* Must be called with iommu->lock */
  1498. static int domain_attach_iommu(struct dmar_domain *domain,
  1499. struct intel_iommu *iommu)
  1500. {
  1501. unsigned long ndomains;
  1502. int num;
  1503. assert_spin_locked(&device_domain_lock);
  1504. assert_spin_locked(&iommu->lock);
  1505. domain->iommu_refcnt[iommu->seq_id] += 1;
  1506. domain->iommu_count += 1;
  1507. if (domain->iommu_refcnt[iommu->seq_id] == 1) {
  1508. ndomains = cap_ndoms(iommu->cap);
  1509. num = find_first_zero_bit(iommu->domain_ids, ndomains);
  1510. if (num >= ndomains) {
  1511. pr_err("%s: No free domain ids\n", iommu->name);
  1512. domain->iommu_refcnt[iommu->seq_id] -= 1;
  1513. domain->iommu_count -= 1;
  1514. return -ENOSPC;
  1515. }
  1516. set_bit(num, iommu->domain_ids);
  1517. set_iommu_domain(iommu, num, domain);
  1518. domain->iommu_did[iommu->seq_id] = num;
  1519. domain->nid = iommu->node;
  1520. domain_update_iommu_cap(domain);
  1521. }
  1522. return 0;
  1523. }
  1524. static int domain_detach_iommu(struct dmar_domain *domain,
  1525. struct intel_iommu *iommu)
  1526. {
  1527. int num, count = INT_MAX;
  1528. assert_spin_locked(&device_domain_lock);
  1529. assert_spin_locked(&iommu->lock);
  1530. domain->iommu_refcnt[iommu->seq_id] -= 1;
  1531. count = --domain->iommu_count;
  1532. if (domain->iommu_refcnt[iommu->seq_id] == 0) {
  1533. num = domain->iommu_did[iommu->seq_id];
  1534. clear_bit(num, iommu->domain_ids);
  1535. set_iommu_domain(iommu, num, NULL);
  1536. domain_update_iommu_cap(domain);
  1537. domain->iommu_did[iommu->seq_id] = 0;
  1538. }
  1539. return count;
  1540. }
  1541. static struct iova_domain reserved_iova_list;
  1542. static struct lock_class_key reserved_rbtree_key;
  1543. static int dmar_init_reserved_ranges(void)
  1544. {
  1545. struct pci_dev *pdev = NULL;
  1546. struct iova *iova;
  1547. int i;
  1548. init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN);
  1549. lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
  1550. &reserved_rbtree_key);
  1551. /* IOAPIC ranges shouldn't be accessed by DMA */
  1552. iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
  1553. IOVA_PFN(IOAPIC_RANGE_END));
  1554. if (!iova) {
  1555. pr_err("Reserve IOAPIC range failed\n");
  1556. return -ENODEV;
  1557. }
  1558. /* Reserve all PCI MMIO to avoid peer-to-peer access */
  1559. for_each_pci_dev(pdev) {
  1560. struct resource *r;
  1561. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  1562. r = &pdev->resource[i];
  1563. if (!r->flags || !(r->flags & IORESOURCE_MEM))
  1564. continue;
  1565. iova = reserve_iova(&reserved_iova_list,
  1566. IOVA_PFN(r->start),
  1567. IOVA_PFN(r->end));
  1568. if (!iova) {
  1569. pr_err("Reserve iova failed\n");
  1570. return -ENODEV;
  1571. }
  1572. }
  1573. }
  1574. return 0;
  1575. }
  1576. static void domain_reserve_special_ranges(struct dmar_domain *domain)
  1577. {
  1578. copy_reserved_iova(&reserved_iova_list, &domain->iovad);
  1579. }
  1580. static inline int guestwidth_to_adjustwidth(int gaw)
  1581. {
  1582. int agaw;
  1583. int r = (gaw - 12) % 9;
  1584. if (r == 0)
  1585. agaw = gaw;
  1586. else
  1587. agaw = gaw + 9 - r;
  1588. if (agaw > 64)
  1589. agaw = 64;
  1590. return agaw;
  1591. }
  1592. static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
  1593. int guest_width)
  1594. {
  1595. int adjust_width, agaw;
  1596. unsigned long sagaw;
  1597. int err;
  1598. init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
  1599. err = init_iova_flush_queue(&domain->iovad,
  1600. iommu_flush_iova, iova_entry_free);
  1601. if (err)
  1602. return err;
  1603. domain_reserve_special_ranges(domain);
  1604. /* calculate AGAW */
  1605. if (guest_width > cap_mgaw(iommu->cap))
  1606. guest_width = cap_mgaw(iommu->cap);
  1607. domain->gaw = guest_width;
  1608. adjust_width = guestwidth_to_adjustwidth(guest_width);
  1609. agaw = width_to_agaw(adjust_width);
  1610. sagaw = cap_sagaw(iommu->cap);
  1611. if (!test_bit(agaw, &sagaw)) {
  1612. /* hardware doesn't support it, choose a bigger one */
  1613. pr_debug("Hardware doesn't support agaw %d\n", agaw);
  1614. agaw = find_next_bit(&sagaw, 5, agaw);
  1615. if (agaw >= 5)
  1616. return -ENODEV;
  1617. }
  1618. domain->agaw = agaw;
  1619. if (ecap_coherent(iommu->ecap))
  1620. domain->iommu_coherency = 1;
  1621. else
  1622. domain->iommu_coherency = 0;
  1623. if (ecap_sc_support(iommu->ecap))
  1624. domain->iommu_snooping = 1;
  1625. else
  1626. domain->iommu_snooping = 0;
  1627. if (intel_iommu_superpage)
  1628. domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
  1629. else
  1630. domain->iommu_superpage = 0;
  1631. domain->nid = iommu->node;
  1632. /* always allocate the top pgd */
  1633. domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
  1634. if (!domain->pgd)
  1635. return -ENOMEM;
  1636. __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
  1637. return 0;
  1638. }
  1639. static void domain_exit(struct dmar_domain *domain)
  1640. {
  1641. struct page *freelist = NULL;
  1642. /* Domain 0 is reserved, so dont process it */
  1643. if (!domain)
  1644. return;
  1645. /* Remove associated devices and clear attached or cached domains */
  1646. rcu_read_lock();
  1647. domain_remove_dev_info(domain);
  1648. rcu_read_unlock();
  1649. /* destroy iovas */
  1650. put_iova_domain(&domain->iovad);
  1651. freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
  1652. dma_free_pagelist(freelist);
  1653. free_domain_mem(domain);
  1654. }
  1655. static int domain_context_mapping_one(struct dmar_domain *domain,
  1656. struct intel_iommu *iommu,
  1657. u8 bus, u8 devfn)
  1658. {
  1659. u16 did = domain->iommu_did[iommu->seq_id];
  1660. int translation = CONTEXT_TT_MULTI_LEVEL;
  1661. struct device_domain_info *info = NULL;
  1662. struct context_entry *context;
  1663. unsigned long flags;
  1664. struct dma_pte *pgd;
  1665. int ret, agaw;
  1666. WARN_ON(did == 0);
  1667. if (hw_pass_through && domain_type_is_si(domain))
  1668. translation = CONTEXT_TT_PASS_THROUGH;
  1669. pr_debug("Set context mapping for %02x:%02x.%d\n",
  1670. bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
  1671. BUG_ON(!domain->pgd);
  1672. spin_lock_irqsave(&device_domain_lock, flags);
  1673. spin_lock(&iommu->lock);
  1674. ret = -ENOMEM;
  1675. context = iommu_context_addr(iommu, bus, devfn, 1);
  1676. if (!context)
  1677. goto out_unlock;
  1678. ret = 0;
  1679. if (context_present(context))
  1680. goto out_unlock;
  1681. /*
  1682. * For kdump cases, old valid entries may be cached due to the
  1683. * in-flight DMA and copied pgtable, but there is no unmapping
  1684. * behaviour for them, thus we need an explicit cache flush for
  1685. * the newly-mapped device. For kdump, at this point, the device
  1686. * is supposed to finish reset at its driver probe stage, so no
  1687. * in-flight DMA will exist, and we don't need to worry anymore
  1688. * hereafter.
  1689. */
  1690. if (context_copied(context)) {
  1691. u16 did_old = context_domain_id(context);
  1692. if (did_old < cap_ndoms(iommu->cap)) {
  1693. iommu->flush.flush_context(iommu, did_old,
  1694. (((u16)bus) << 8) | devfn,
  1695. DMA_CCMD_MASK_NOBIT,
  1696. DMA_CCMD_DEVICE_INVL);
  1697. iommu->flush.flush_iotlb(iommu, did_old, 0, 0,
  1698. DMA_TLB_DSI_FLUSH);
  1699. }
  1700. }
  1701. pgd = domain->pgd;
  1702. context_clear_entry(context);
  1703. context_set_domain_id(context, did);
  1704. /*
  1705. * Skip top levels of page tables for iommu which has less agaw
  1706. * than default. Unnecessary for PT mode.
  1707. */
  1708. if (translation != CONTEXT_TT_PASS_THROUGH) {
  1709. for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
  1710. ret = -ENOMEM;
  1711. pgd = phys_to_virt(dma_pte_addr(pgd));
  1712. if (!dma_pte_present(pgd))
  1713. goto out_unlock;
  1714. }
  1715. info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
  1716. if (info && info->ats_supported)
  1717. translation = CONTEXT_TT_DEV_IOTLB;
  1718. else
  1719. translation = CONTEXT_TT_MULTI_LEVEL;
  1720. context_set_address_root(context, virt_to_phys(pgd));
  1721. context_set_address_width(context, agaw);
  1722. } else {
  1723. /*
  1724. * In pass through mode, AW must be programmed to
  1725. * indicate the largest AGAW value supported by
  1726. * hardware. And ASR is ignored by hardware.
  1727. */
  1728. context_set_address_width(context, iommu->msagaw);
  1729. }
  1730. context_set_translation_type(context, translation);
  1731. context_set_fault_enable(context);
  1732. context_set_present(context);
  1733. domain_flush_cache(domain, context, sizeof(*context));
  1734. /*
  1735. * It's a non-present to present mapping. If hardware doesn't cache
  1736. * non-present entry we only need to flush the write-buffer. If the
  1737. * _does_ cache non-present entries, then it does so in the special
  1738. * domain #0, which we have to flush:
  1739. */
  1740. if (cap_caching_mode(iommu->cap)) {
  1741. iommu->flush.flush_context(iommu, 0,
  1742. (((u16)bus) << 8) | devfn,
  1743. DMA_CCMD_MASK_NOBIT,
  1744. DMA_CCMD_DEVICE_INVL);
  1745. iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
  1746. } else {
  1747. iommu_flush_write_buffer(iommu);
  1748. }
  1749. iommu_enable_dev_iotlb(info);
  1750. ret = 0;
  1751. out_unlock:
  1752. spin_unlock(&iommu->lock);
  1753. spin_unlock_irqrestore(&device_domain_lock, flags);
  1754. return ret;
  1755. }
  1756. struct domain_context_mapping_data {
  1757. struct dmar_domain *domain;
  1758. struct intel_iommu *iommu;
  1759. };
  1760. static int domain_context_mapping_cb(struct pci_dev *pdev,
  1761. u16 alias, void *opaque)
  1762. {
  1763. struct domain_context_mapping_data *data = opaque;
  1764. return domain_context_mapping_one(data->domain, data->iommu,
  1765. PCI_BUS_NUM(alias), alias & 0xff);
  1766. }
  1767. static int
  1768. domain_context_mapping(struct dmar_domain *domain, struct device *dev)
  1769. {
  1770. struct intel_iommu *iommu;
  1771. u8 bus, devfn;
  1772. struct domain_context_mapping_data data;
  1773. iommu = device_to_iommu(dev, &bus, &devfn);
  1774. if (!iommu)
  1775. return -ENODEV;
  1776. if (!dev_is_pci(dev))
  1777. return domain_context_mapping_one(domain, iommu, bus, devfn);
  1778. data.domain = domain;
  1779. data.iommu = iommu;
  1780. return pci_for_each_dma_alias(to_pci_dev(dev),
  1781. &domain_context_mapping_cb, &data);
  1782. }
  1783. static int domain_context_mapped_cb(struct pci_dev *pdev,
  1784. u16 alias, void *opaque)
  1785. {
  1786. struct intel_iommu *iommu = opaque;
  1787. return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
  1788. }
  1789. static int domain_context_mapped(struct device *dev)
  1790. {
  1791. struct intel_iommu *iommu;
  1792. u8 bus, devfn;
  1793. iommu = device_to_iommu(dev, &bus, &devfn);
  1794. if (!iommu)
  1795. return -ENODEV;
  1796. if (!dev_is_pci(dev))
  1797. return device_context_mapped(iommu, bus, devfn);
  1798. return !pci_for_each_dma_alias(to_pci_dev(dev),
  1799. domain_context_mapped_cb, iommu);
  1800. }
  1801. /* Returns a number of VTD pages, but aligned to MM page size */
  1802. static inline unsigned long aligned_nrpages(unsigned long host_addr,
  1803. size_t size)
  1804. {
  1805. host_addr &= ~PAGE_MASK;
  1806. return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
  1807. }
  1808. /* Return largest possible superpage level for a given mapping */
  1809. static inline int hardware_largepage_caps(struct dmar_domain *domain,
  1810. unsigned long iov_pfn,
  1811. unsigned long phy_pfn,
  1812. unsigned long pages)
  1813. {
  1814. int support, level = 1;
  1815. unsigned long pfnmerge;
  1816. support = domain->iommu_superpage;
  1817. /* To use a large page, the virtual *and* physical addresses
  1818. must be aligned to 2MiB/1GiB/etc. Lower bits set in either
  1819. of them will mean we have to use smaller pages. So just
  1820. merge them and check both at once. */
  1821. pfnmerge = iov_pfn | phy_pfn;
  1822. while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
  1823. pages >>= VTD_STRIDE_SHIFT;
  1824. if (!pages)
  1825. break;
  1826. pfnmerge >>= VTD_STRIDE_SHIFT;
  1827. level++;
  1828. support--;
  1829. }
  1830. return level;
  1831. }
  1832. static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
  1833. struct scatterlist *sg, unsigned long phys_pfn,
  1834. unsigned long nr_pages, int prot)
  1835. {
  1836. struct dma_pte *first_pte = NULL, *pte = NULL;
  1837. phys_addr_t uninitialized_var(pteval);
  1838. unsigned long sg_res = 0;
  1839. unsigned int largepage_lvl = 0;
  1840. unsigned long lvl_pages = 0;
  1841. BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
  1842. if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
  1843. return -EINVAL;
  1844. prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
  1845. if (!sg) {
  1846. sg_res = nr_pages;
  1847. pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
  1848. }
  1849. while (nr_pages > 0) {
  1850. uint64_t tmp;
  1851. if (!sg_res) {
  1852. unsigned int pgoff = sg->offset & ~PAGE_MASK;
  1853. sg_res = aligned_nrpages(sg->offset, sg->length);
  1854. sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff;
  1855. sg->dma_length = sg->length;
  1856. pteval = (sg_phys(sg) - pgoff) | prot;
  1857. phys_pfn = pteval >> VTD_PAGE_SHIFT;
  1858. }
  1859. if (!pte) {
  1860. largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
  1861. first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
  1862. if (!pte)
  1863. return -ENOMEM;
  1864. /* It is large page*/
  1865. if (largepage_lvl > 1) {
  1866. unsigned long nr_superpages, end_pfn;
  1867. pteval |= DMA_PTE_LARGE_PAGE;
  1868. lvl_pages = lvl_to_nr_pages(largepage_lvl);
  1869. nr_superpages = sg_res / lvl_pages;
  1870. end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
  1871. /*
  1872. * Ensure that old small page tables are
  1873. * removed to make room for superpage(s).
  1874. * We're adding new large pages, so make sure
  1875. * we don't remove their parent tables.
  1876. */
  1877. dma_pte_free_pagetable(domain, iov_pfn, end_pfn,
  1878. largepage_lvl + 1);
  1879. } else {
  1880. pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
  1881. }
  1882. }
  1883. /* We don't need lock here, nobody else
  1884. * touches the iova range
  1885. */
  1886. tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
  1887. if (tmp) {
  1888. static int dumps = 5;
  1889. pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
  1890. iov_pfn, tmp, (unsigned long long)pteval);
  1891. if (dumps) {
  1892. dumps--;
  1893. debug_dma_dump_mappings(NULL);
  1894. }
  1895. WARN_ON(1);
  1896. }
  1897. lvl_pages = lvl_to_nr_pages(largepage_lvl);
  1898. BUG_ON(nr_pages < lvl_pages);
  1899. BUG_ON(sg_res < lvl_pages);
  1900. nr_pages -= lvl_pages;
  1901. iov_pfn += lvl_pages;
  1902. phys_pfn += lvl_pages;
  1903. pteval += lvl_pages * VTD_PAGE_SIZE;
  1904. sg_res -= lvl_pages;
  1905. /* If the next PTE would be the first in a new page, then we
  1906. need to flush the cache on the entries we've just written.
  1907. And then we'll need to recalculate 'pte', so clear it and
  1908. let it get set again in the if (!pte) block above.
  1909. If we're done (!nr_pages) we need to flush the cache too.
  1910. Also if we've been setting superpages, we may need to
  1911. recalculate 'pte' and switch back to smaller pages for the
  1912. end of the mapping, if the trailing size is not enough to
  1913. use another superpage (i.e. sg_res < lvl_pages). */
  1914. pte++;
  1915. if (!nr_pages || first_pte_in_page(pte) ||
  1916. (largepage_lvl > 1 && sg_res < lvl_pages)) {
  1917. domain_flush_cache(domain, first_pte,
  1918. (void *)pte - (void *)first_pte);
  1919. pte = NULL;
  1920. }
  1921. if (!sg_res && nr_pages)
  1922. sg = sg_next(sg);
  1923. }
  1924. return 0;
  1925. }
  1926. static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
  1927. struct scatterlist *sg, unsigned long phys_pfn,
  1928. unsigned long nr_pages, int prot)
  1929. {
  1930. int ret;
  1931. struct intel_iommu *iommu;
  1932. /* Do the real mapping first */
  1933. ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
  1934. if (ret)
  1935. return ret;
  1936. /* Notify about the new mapping */
  1937. if (domain_type_is_vm(domain)) {
  1938. /* VM typed domains can have more than one IOMMUs */
  1939. int iommu_id;
  1940. for_each_domain_iommu(iommu_id, domain) {
  1941. iommu = g_iommus[iommu_id];
  1942. __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
  1943. }
  1944. } else {
  1945. /* General domains only have one IOMMU */
  1946. iommu = domain_get_iommu(domain);
  1947. __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
  1948. }
  1949. return 0;
  1950. }
  1951. static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
  1952. struct scatterlist *sg, unsigned long nr_pages,
  1953. int prot)
  1954. {
  1955. return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
  1956. }
  1957. static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
  1958. unsigned long phys_pfn, unsigned long nr_pages,
  1959. int prot)
  1960. {
  1961. return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
  1962. }
  1963. static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
  1964. {
  1965. unsigned long flags;
  1966. struct context_entry *context;
  1967. u16 did_old;
  1968. if (!iommu)
  1969. return;
  1970. spin_lock_irqsave(&iommu->lock, flags);
  1971. context = iommu_context_addr(iommu, bus, devfn, 0);
  1972. if (!context) {
  1973. spin_unlock_irqrestore(&iommu->lock, flags);
  1974. return;
  1975. }
  1976. did_old = context_domain_id(context);
  1977. context_clear_entry(context);
  1978. __iommu_flush_cache(iommu, context, sizeof(*context));
  1979. spin_unlock_irqrestore(&iommu->lock, flags);
  1980. iommu->flush.flush_context(iommu,
  1981. did_old,
  1982. (((u16)bus) << 8) | devfn,
  1983. DMA_CCMD_MASK_NOBIT,
  1984. DMA_CCMD_DEVICE_INVL);
  1985. iommu->flush.flush_iotlb(iommu,
  1986. did_old,
  1987. 0,
  1988. 0,
  1989. DMA_TLB_DSI_FLUSH);
  1990. }
  1991. static inline void unlink_domain_info(struct device_domain_info *info)
  1992. {
  1993. assert_spin_locked(&device_domain_lock);
  1994. list_del(&info->link);
  1995. list_del(&info->global);
  1996. if (info->dev)
  1997. info->dev->archdata.iommu = NULL;
  1998. }
  1999. static void domain_remove_dev_info(struct dmar_domain *domain)
  2000. {
  2001. struct device_domain_info *info, *tmp;
  2002. unsigned long flags;
  2003. spin_lock_irqsave(&device_domain_lock, flags);
  2004. list_for_each_entry_safe(info, tmp, &domain->devices, link)
  2005. __dmar_remove_one_dev_info(info);
  2006. spin_unlock_irqrestore(&device_domain_lock, flags);
  2007. }
  2008. /*
  2009. * find_domain
  2010. * Note: we use struct device->archdata.iommu stores the info
  2011. */
  2012. static struct dmar_domain *find_domain(struct device *dev)
  2013. {
  2014. struct device_domain_info *info;
  2015. /* No lock here, assumes no domain exit in normal case */
  2016. info = dev->archdata.iommu;
  2017. if (likely(info))
  2018. return info->domain;
  2019. return NULL;
  2020. }
  2021. static inline struct device_domain_info *
  2022. dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
  2023. {
  2024. struct device_domain_info *info;
  2025. list_for_each_entry(info, &device_domain_list, global)
  2026. if (info->iommu->segment == segment && info->bus == bus &&
  2027. info->devfn == devfn)
  2028. return info;
  2029. return NULL;
  2030. }
  2031. static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
  2032. int bus, int devfn,
  2033. struct device *dev,
  2034. struct dmar_domain *domain)
  2035. {
  2036. struct dmar_domain *found = NULL;
  2037. struct device_domain_info *info;
  2038. unsigned long flags;
  2039. int ret;
  2040. info = alloc_devinfo_mem();
  2041. if (!info)
  2042. return NULL;
  2043. info->bus = bus;
  2044. info->devfn = devfn;
  2045. info->ats_supported = info->pasid_supported = info->pri_supported = 0;
  2046. info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
  2047. info->ats_qdep = 0;
  2048. info->dev = dev;
  2049. info->domain = domain;
  2050. info->iommu = iommu;
  2051. info->pasid_table = NULL;
  2052. if (dev && dev_is_pci(dev)) {
  2053. struct pci_dev *pdev = to_pci_dev(info->dev);
  2054. if (!pci_ats_disabled() &&
  2055. ecap_dev_iotlb_support(iommu->ecap) &&
  2056. pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
  2057. dmar_find_matched_atsr_unit(pdev))
  2058. info->ats_supported = 1;
  2059. if (ecs_enabled(iommu)) {
  2060. if (pasid_enabled(iommu)) {
  2061. int features = pci_pasid_features(pdev);
  2062. if (features >= 0)
  2063. info->pasid_supported = features | 1;
  2064. }
  2065. if (info->ats_supported && ecap_prs(iommu->ecap) &&
  2066. pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
  2067. info->pri_supported = 1;
  2068. }
  2069. }
  2070. spin_lock_irqsave(&device_domain_lock, flags);
  2071. if (dev)
  2072. found = find_domain(dev);
  2073. if (!found) {
  2074. struct device_domain_info *info2;
  2075. info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
  2076. if (info2) {
  2077. found = info2->domain;
  2078. info2->dev = dev;
  2079. }
  2080. }
  2081. if (found) {
  2082. spin_unlock_irqrestore(&device_domain_lock, flags);
  2083. free_devinfo_mem(info);
  2084. /* Caller must free the original domain */
  2085. return found;
  2086. }
  2087. spin_lock(&iommu->lock);
  2088. ret = domain_attach_iommu(domain, iommu);
  2089. spin_unlock(&iommu->lock);
  2090. if (ret) {
  2091. spin_unlock_irqrestore(&device_domain_lock, flags);
  2092. free_devinfo_mem(info);
  2093. return NULL;
  2094. }
  2095. list_add(&info->link, &domain->devices);
  2096. list_add(&info->global, &device_domain_list);
  2097. if (dev)
  2098. dev->archdata.iommu = info;
  2099. if (dev && dev_is_pci(dev) && info->pasid_supported) {
  2100. ret = intel_pasid_alloc_table(dev);
  2101. if (ret) {
  2102. pr_warn("No pasid table for %s, pasid disabled\n",
  2103. dev_name(dev));
  2104. info->pasid_supported = 0;
  2105. }
  2106. }
  2107. spin_unlock_irqrestore(&device_domain_lock, flags);
  2108. if (dev && domain_context_mapping(domain, dev)) {
  2109. pr_err("Domain context map for %s failed\n", dev_name(dev));
  2110. dmar_remove_one_dev_info(domain, dev);
  2111. return NULL;
  2112. }
  2113. return domain;
  2114. }
  2115. static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
  2116. {
  2117. *(u16 *)opaque = alias;
  2118. return 0;
  2119. }
  2120. static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
  2121. {
  2122. struct device_domain_info *info = NULL;
  2123. struct dmar_domain *domain = NULL;
  2124. struct intel_iommu *iommu;
  2125. u16 dma_alias;
  2126. unsigned long flags;
  2127. u8 bus, devfn;
  2128. iommu = device_to_iommu(dev, &bus, &devfn);
  2129. if (!iommu)
  2130. return NULL;
  2131. if (dev_is_pci(dev)) {
  2132. struct pci_dev *pdev = to_pci_dev(dev);
  2133. pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
  2134. spin_lock_irqsave(&device_domain_lock, flags);
  2135. info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
  2136. PCI_BUS_NUM(dma_alias),
  2137. dma_alias & 0xff);
  2138. if (info) {
  2139. iommu = info->iommu;
  2140. domain = info->domain;
  2141. }
  2142. spin_unlock_irqrestore(&device_domain_lock, flags);
  2143. /* DMA alias already has a domain, use it */
  2144. if (info)
  2145. goto out;
  2146. }
  2147. /* Allocate and initialize new domain for the device */
  2148. domain = alloc_domain(0);
  2149. if (!domain)
  2150. return NULL;
  2151. if (domain_init(domain, iommu, gaw)) {
  2152. domain_exit(domain);
  2153. return NULL;
  2154. }
  2155. out:
  2156. return domain;
  2157. }
  2158. static struct dmar_domain *set_domain_for_dev(struct device *dev,
  2159. struct dmar_domain *domain)
  2160. {
  2161. struct intel_iommu *iommu;
  2162. struct dmar_domain *tmp;
  2163. u16 req_id, dma_alias;
  2164. u8 bus, devfn;
  2165. iommu = device_to_iommu(dev, &bus, &devfn);
  2166. if (!iommu)
  2167. return NULL;
  2168. req_id = ((u16)bus << 8) | devfn;
  2169. if (dev_is_pci(dev)) {
  2170. struct pci_dev *pdev = to_pci_dev(dev);
  2171. pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
  2172. /* register PCI DMA alias device */
  2173. if (req_id != dma_alias) {
  2174. tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
  2175. dma_alias & 0xff, NULL, domain);
  2176. if (!tmp || tmp != domain)
  2177. return tmp;
  2178. }
  2179. }
  2180. tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
  2181. if (!tmp || tmp != domain)
  2182. return tmp;
  2183. return domain;
  2184. }
  2185. static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
  2186. {
  2187. struct dmar_domain *domain, *tmp;
  2188. domain = find_domain(dev);
  2189. if (domain)
  2190. goto out;
  2191. domain = find_or_alloc_domain(dev, gaw);
  2192. if (!domain)
  2193. goto out;
  2194. tmp = set_domain_for_dev(dev, domain);
  2195. if (!tmp || domain != tmp) {
  2196. domain_exit(domain);
  2197. domain = tmp;
  2198. }
  2199. out:
  2200. return domain;
  2201. }
  2202. static int iommu_domain_identity_map(struct dmar_domain *domain,
  2203. unsigned long long start,
  2204. unsigned long long end)
  2205. {
  2206. unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
  2207. unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
  2208. if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
  2209. dma_to_mm_pfn(last_vpfn))) {
  2210. pr_err("Reserving iova failed\n");
  2211. return -ENOMEM;
  2212. }
  2213. pr_debug("Mapping reserved region %llx-%llx\n", start, end);
  2214. /*
  2215. * RMRR range might have overlap with physical memory range,
  2216. * clear it first
  2217. */
  2218. dma_pte_clear_range(domain, first_vpfn, last_vpfn);
  2219. return __domain_mapping(domain, first_vpfn, NULL,
  2220. first_vpfn, last_vpfn - first_vpfn + 1,
  2221. DMA_PTE_READ|DMA_PTE_WRITE);
  2222. }
  2223. static int domain_prepare_identity_map(struct device *dev,
  2224. struct dmar_domain *domain,
  2225. unsigned long long start,
  2226. unsigned long long end)
  2227. {
  2228. /* For _hardware_ passthrough, don't bother. But for software
  2229. passthrough, we do it anyway -- it may indicate a memory
  2230. range which is reserved in E820, so which didn't get set
  2231. up to start with in si_domain */
  2232. if (domain == si_domain && hw_pass_through) {
  2233. pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
  2234. dev_name(dev), start, end);
  2235. return 0;
  2236. }
  2237. pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
  2238. dev_name(dev), start, end);
  2239. if (end < start) {
  2240. WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
  2241. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  2242. dmi_get_system_info(DMI_BIOS_VENDOR),
  2243. dmi_get_system_info(DMI_BIOS_VERSION),
  2244. dmi_get_system_info(DMI_PRODUCT_VERSION));
  2245. return -EIO;
  2246. }
  2247. if (end >> agaw_to_width(domain->agaw)) {
  2248. WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
  2249. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  2250. agaw_to_width(domain->agaw),
  2251. dmi_get_system_info(DMI_BIOS_VENDOR),
  2252. dmi_get_system_info(DMI_BIOS_VERSION),
  2253. dmi_get_system_info(DMI_PRODUCT_VERSION));
  2254. return -EIO;
  2255. }
  2256. return iommu_domain_identity_map(domain, start, end);
  2257. }
  2258. static int iommu_prepare_identity_map(struct device *dev,
  2259. unsigned long long start,
  2260. unsigned long long end)
  2261. {
  2262. struct dmar_domain *domain;
  2263. int ret;
  2264. domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
  2265. if (!domain)
  2266. return -ENOMEM;
  2267. ret = domain_prepare_identity_map(dev, domain, start, end);
  2268. if (ret)
  2269. domain_exit(domain);
  2270. return ret;
  2271. }
  2272. static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
  2273. struct device *dev)
  2274. {
  2275. if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
  2276. return 0;
  2277. return iommu_prepare_identity_map(dev, rmrr->base_address,
  2278. rmrr->end_address);
  2279. }
  2280. #ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
  2281. static inline void iommu_prepare_isa(void)
  2282. {
  2283. struct pci_dev *pdev;
  2284. int ret;
  2285. pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  2286. if (!pdev)
  2287. return;
  2288. pr_info("Prepare 0-16MiB unity mapping for LPC\n");
  2289. ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
  2290. if (ret)
  2291. pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
  2292. pci_dev_put(pdev);
  2293. }
  2294. #else
  2295. static inline void iommu_prepare_isa(void)
  2296. {
  2297. return;
  2298. }
  2299. #endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
  2300. static int md_domain_init(struct dmar_domain *domain, int guest_width);
  2301. static int __init si_domain_init(int hw)
  2302. {
  2303. int nid, ret = 0;
  2304. si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
  2305. if (!si_domain)
  2306. return -EFAULT;
  2307. if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
  2308. domain_exit(si_domain);
  2309. return -EFAULT;
  2310. }
  2311. pr_debug("Identity mapping domain allocated\n");
  2312. if (hw)
  2313. return 0;
  2314. for_each_online_node(nid) {
  2315. unsigned long start_pfn, end_pfn;
  2316. int i;
  2317. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
  2318. ret = iommu_domain_identity_map(si_domain,
  2319. PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
  2320. if (ret)
  2321. return ret;
  2322. }
  2323. }
  2324. return 0;
  2325. }
  2326. static int identity_mapping(struct device *dev)
  2327. {
  2328. struct device_domain_info *info;
  2329. if (likely(!iommu_identity_mapping))
  2330. return 0;
  2331. info = dev->archdata.iommu;
  2332. if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
  2333. return (info->domain == si_domain);
  2334. return 0;
  2335. }
  2336. static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
  2337. {
  2338. struct dmar_domain *ndomain;
  2339. struct intel_iommu *iommu;
  2340. u8 bus, devfn;
  2341. iommu = device_to_iommu(dev, &bus, &devfn);
  2342. if (!iommu)
  2343. return -ENODEV;
  2344. ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
  2345. if (ndomain != domain)
  2346. return -EBUSY;
  2347. return 0;
  2348. }
  2349. static bool device_has_rmrr(struct device *dev)
  2350. {
  2351. struct dmar_rmrr_unit *rmrr;
  2352. struct device *tmp;
  2353. int i;
  2354. rcu_read_lock();
  2355. for_each_rmrr_units(rmrr) {
  2356. /*
  2357. * Return TRUE if this RMRR contains the device that
  2358. * is passed in.
  2359. */
  2360. for_each_active_dev_scope(rmrr->devices,
  2361. rmrr->devices_cnt, i, tmp)
  2362. if (tmp == dev) {
  2363. rcu_read_unlock();
  2364. return true;
  2365. }
  2366. }
  2367. rcu_read_unlock();
  2368. return false;
  2369. }
  2370. /*
  2371. * There are a couple cases where we need to restrict the functionality of
  2372. * devices associated with RMRRs. The first is when evaluating a device for
  2373. * identity mapping because problems exist when devices are moved in and out
  2374. * of domains and their respective RMRR information is lost. This means that
  2375. * a device with associated RMRRs will never be in a "passthrough" domain.
  2376. * The second is use of the device through the IOMMU API. This interface
  2377. * expects to have full control of the IOVA space for the device. We cannot
  2378. * satisfy both the requirement that RMRR access is maintained and have an
  2379. * unencumbered IOVA space. We also have no ability to quiesce the device's
  2380. * use of the RMRR space or even inform the IOMMU API user of the restriction.
  2381. * We therefore prevent devices associated with an RMRR from participating in
  2382. * the IOMMU API, which eliminates them from device assignment.
  2383. *
  2384. * In both cases we assume that PCI USB devices with RMRRs have them largely
  2385. * for historical reasons and that the RMRR space is not actively used post
  2386. * boot. This exclusion may change if vendors begin to abuse it.
  2387. *
  2388. * The same exception is made for graphics devices, with the requirement that
  2389. * any use of the RMRR regions will be torn down before assigning the device
  2390. * to a guest.
  2391. */
  2392. static bool device_is_rmrr_locked(struct device *dev)
  2393. {
  2394. if (!device_has_rmrr(dev))
  2395. return false;
  2396. if (dev_is_pci(dev)) {
  2397. struct pci_dev *pdev = to_pci_dev(dev);
  2398. if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
  2399. return false;
  2400. }
  2401. return true;
  2402. }
  2403. static int iommu_should_identity_map(struct device *dev, int startup)
  2404. {
  2405. if (dev_is_pci(dev)) {
  2406. struct pci_dev *pdev = to_pci_dev(dev);
  2407. if (device_is_rmrr_locked(dev))
  2408. return 0;
  2409. if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
  2410. return 1;
  2411. if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
  2412. return 1;
  2413. if (!(iommu_identity_mapping & IDENTMAP_ALL))
  2414. return 0;
  2415. /*
  2416. * We want to start off with all devices in the 1:1 domain, and
  2417. * take them out later if we find they can't access all of memory.
  2418. *
  2419. * However, we can't do this for PCI devices behind bridges,
  2420. * because all PCI devices behind the same bridge will end up
  2421. * with the same source-id on their transactions.
  2422. *
  2423. * Practically speaking, we can't change things around for these
  2424. * devices at run-time, because we can't be sure there'll be no
  2425. * DMA transactions in flight for any of their siblings.
  2426. *
  2427. * So PCI devices (unless they're on the root bus) as well as
  2428. * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
  2429. * the 1:1 domain, just in _case_ one of their siblings turns out
  2430. * not to be able to map all of memory.
  2431. */
  2432. if (!pci_is_pcie(pdev)) {
  2433. if (!pci_is_root_bus(pdev->bus))
  2434. return 0;
  2435. if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
  2436. return 0;
  2437. } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
  2438. return 0;
  2439. } else {
  2440. if (device_has_rmrr(dev))
  2441. return 0;
  2442. }
  2443. /*
  2444. * At boot time, we don't yet know if devices will be 64-bit capable.
  2445. * Assume that they will — if they turn out not to be, then we can
  2446. * take them out of the 1:1 domain later.
  2447. */
  2448. if (!startup) {
  2449. /*
  2450. * If the device's dma_mask is less than the system's memory
  2451. * size then this is not a candidate for identity mapping.
  2452. */
  2453. u64 dma_mask = *dev->dma_mask;
  2454. if (dev->coherent_dma_mask &&
  2455. dev->coherent_dma_mask < dma_mask)
  2456. dma_mask = dev->coherent_dma_mask;
  2457. return dma_mask >= dma_get_required_mask(dev);
  2458. }
  2459. return 1;
  2460. }
  2461. static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
  2462. {
  2463. int ret;
  2464. if (!iommu_should_identity_map(dev, 1))
  2465. return 0;
  2466. ret = domain_add_dev_info(si_domain, dev);
  2467. if (!ret)
  2468. pr_info("%s identity mapping for device %s\n",
  2469. hw ? "Hardware" : "Software", dev_name(dev));
  2470. else if (ret == -ENODEV)
  2471. /* device not associated with an iommu */
  2472. ret = 0;
  2473. return ret;
  2474. }
  2475. static int __init iommu_prepare_static_identity_mapping(int hw)
  2476. {
  2477. struct pci_dev *pdev = NULL;
  2478. struct dmar_drhd_unit *drhd;
  2479. struct intel_iommu *iommu;
  2480. struct device *dev;
  2481. int i;
  2482. int ret = 0;
  2483. for_each_pci_dev(pdev) {
  2484. ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
  2485. if (ret)
  2486. return ret;
  2487. }
  2488. for_each_active_iommu(iommu, drhd)
  2489. for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
  2490. struct acpi_device_physical_node *pn;
  2491. struct acpi_device *adev;
  2492. if (dev->bus != &acpi_bus_type)
  2493. continue;
  2494. adev= to_acpi_device(dev);
  2495. mutex_lock(&adev->physical_node_lock);
  2496. list_for_each_entry(pn, &adev->physical_node_list, node) {
  2497. ret = dev_prepare_static_identity_mapping(pn->dev, hw);
  2498. if (ret)
  2499. break;
  2500. }
  2501. mutex_unlock(&adev->physical_node_lock);
  2502. if (ret)
  2503. return ret;
  2504. }
  2505. return 0;
  2506. }
  2507. static void intel_iommu_init_qi(struct intel_iommu *iommu)
  2508. {
  2509. /*
  2510. * Start from the sane iommu hardware state.
  2511. * If the queued invalidation is already initialized by us
  2512. * (for example, while enabling interrupt-remapping) then
  2513. * we got the things already rolling from a sane state.
  2514. */
  2515. if (!iommu->qi) {
  2516. /*
  2517. * Clear any previous faults.
  2518. */
  2519. dmar_fault(-1, iommu);
  2520. /*
  2521. * Disable queued invalidation if supported and already enabled
  2522. * before OS handover.
  2523. */
  2524. dmar_disable_qi(iommu);
  2525. }
  2526. if (dmar_enable_qi(iommu)) {
  2527. /*
  2528. * Queued Invalidate not enabled, use Register Based Invalidate
  2529. */
  2530. iommu->flush.flush_context = __iommu_flush_context;
  2531. iommu->flush.flush_iotlb = __iommu_flush_iotlb;
  2532. pr_info("%s: Using Register based invalidation\n",
  2533. iommu->name);
  2534. } else {
  2535. iommu->flush.flush_context = qi_flush_context;
  2536. iommu->flush.flush_iotlb = qi_flush_iotlb;
  2537. pr_info("%s: Using Queued invalidation\n", iommu->name);
  2538. }
  2539. }
  2540. static int copy_context_table(struct intel_iommu *iommu,
  2541. struct root_entry *old_re,
  2542. struct context_entry **tbl,
  2543. int bus, bool ext)
  2544. {
  2545. int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
  2546. struct context_entry *new_ce = NULL, ce;
  2547. struct context_entry *old_ce = NULL;
  2548. struct root_entry re;
  2549. phys_addr_t old_ce_phys;
  2550. tbl_idx = ext ? bus * 2 : bus;
  2551. memcpy(&re, old_re, sizeof(re));
  2552. for (devfn = 0; devfn < 256; devfn++) {
  2553. /* First calculate the correct index */
  2554. idx = (ext ? devfn * 2 : devfn) % 256;
  2555. if (idx == 0) {
  2556. /* First save what we may have and clean up */
  2557. if (new_ce) {
  2558. tbl[tbl_idx] = new_ce;
  2559. __iommu_flush_cache(iommu, new_ce,
  2560. VTD_PAGE_SIZE);
  2561. pos = 1;
  2562. }
  2563. if (old_ce)
  2564. memunmap(old_ce);
  2565. ret = 0;
  2566. if (devfn < 0x80)
  2567. old_ce_phys = root_entry_lctp(&re);
  2568. else
  2569. old_ce_phys = root_entry_uctp(&re);
  2570. if (!old_ce_phys) {
  2571. if (ext && devfn == 0) {
  2572. /* No LCTP, try UCTP */
  2573. devfn = 0x7f;
  2574. continue;
  2575. } else {
  2576. goto out;
  2577. }
  2578. }
  2579. ret = -ENOMEM;
  2580. old_ce = memremap(old_ce_phys, PAGE_SIZE,
  2581. MEMREMAP_WB);
  2582. if (!old_ce)
  2583. goto out;
  2584. new_ce = alloc_pgtable_page(iommu->node);
  2585. if (!new_ce)
  2586. goto out_unmap;
  2587. ret = 0;
  2588. }
  2589. /* Now copy the context entry */
  2590. memcpy(&ce, old_ce + idx, sizeof(ce));
  2591. if (!__context_present(&ce))
  2592. continue;
  2593. did = context_domain_id(&ce);
  2594. if (did >= 0 && did < cap_ndoms(iommu->cap))
  2595. set_bit(did, iommu->domain_ids);
  2596. /*
  2597. * We need a marker for copied context entries. This
  2598. * marker needs to work for the old format as well as
  2599. * for extended context entries.
  2600. *
  2601. * Bit 67 of the context entry is used. In the old
  2602. * format this bit is available to software, in the
  2603. * extended format it is the PGE bit, but PGE is ignored
  2604. * by HW if PASIDs are disabled (and thus still
  2605. * available).
  2606. *
  2607. * So disable PASIDs first and then mark the entry
  2608. * copied. This means that we don't copy PASID
  2609. * translations from the old kernel, but this is fine as
  2610. * faults there are not fatal.
  2611. */
  2612. context_clear_pasid_enable(&ce);
  2613. context_set_copied(&ce);
  2614. new_ce[idx] = ce;
  2615. }
  2616. tbl[tbl_idx + pos] = new_ce;
  2617. __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
  2618. out_unmap:
  2619. memunmap(old_ce);
  2620. out:
  2621. return ret;
  2622. }
  2623. static int copy_translation_tables(struct intel_iommu *iommu)
  2624. {
  2625. struct context_entry **ctxt_tbls;
  2626. struct root_entry *old_rt;
  2627. phys_addr_t old_rt_phys;
  2628. int ctxt_table_entries;
  2629. unsigned long flags;
  2630. u64 rtaddr_reg;
  2631. int bus, ret;
  2632. bool new_ext, ext;
  2633. rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
  2634. ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
  2635. new_ext = !!ecap_ecs(iommu->ecap);
  2636. /*
  2637. * The RTT bit can only be changed when translation is disabled,
  2638. * but disabling translation means to open a window for data
  2639. * corruption. So bail out and don't copy anything if we would
  2640. * have to change the bit.
  2641. */
  2642. if (new_ext != ext)
  2643. return -EINVAL;
  2644. old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
  2645. if (!old_rt_phys)
  2646. return -EINVAL;
  2647. old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
  2648. if (!old_rt)
  2649. return -ENOMEM;
  2650. /* This is too big for the stack - allocate it from slab */
  2651. ctxt_table_entries = ext ? 512 : 256;
  2652. ret = -ENOMEM;
  2653. ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL);
  2654. if (!ctxt_tbls)
  2655. goto out_unmap;
  2656. for (bus = 0; bus < 256; bus++) {
  2657. ret = copy_context_table(iommu, &old_rt[bus],
  2658. ctxt_tbls, bus, ext);
  2659. if (ret) {
  2660. pr_err("%s: Failed to copy context table for bus %d\n",
  2661. iommu->name, bus);
  2662. continue;
  2663. }
  2664. }
  2665. spin_lock_irqsave(&iommu->lock, flags);
  2666. /* Context tables are copied, now write them to the root_entry table */
  2667. for (bus = 0; bus < 256; bus++) {
  2668. int idx = ext ? bus * 2 : bus;
  2669. u64 val;
  2670. if (ctxt_tbls[idx]) {
  2671. val = virt_to_phys(ctxt_tbls[idx]) | 1;
  2672. iommu->root_entry[bus].lo = val;
  2673. }
  2674. if (!ext || !ctxt_tbls[idx + 1])
  2675. continue;
  2676. val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
  2677. iommu->root_entry[bus].hi = val;
  2678. }
  2679. spin_unlock_irqrestore(&iommu->lock, flags);
  2680. kfree(ctxt_tbls);
  2681. __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
  2682. ret = 0;
  2683. out_unmap:
  2684. memunmap(old_rt);
  2685. return ret;
  2686. }
  2687. static int __init init_dmars(void)
  2688. {
  2689. struct dmar_drhd_unit *drhd;
  2690. struct dmar_rmrr_unit *rmrr;
  2691. bool copied_tables = false;
  2692. struct device *dev;
  2693. struct intel_iommu *iommu;
  2694. int i, ret;
  2695. /*
  2696. * for each drhd
  2697. * allocate root
  2698. * initialize and program root entry to not present
  2699. * endfor
  2700. */
  2701. for_each_drhd_unit(drhd) {
  2702. /*
  2703. * lock not needed as this is only incremented in the single
  2704. * threaded kernel __init code path all other access are read
  2705. * only
  2706. */
  2707. if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
  2708. g_num_of_iommus++;
  2709. continue;
  2710. }
  2711. pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
  2712. }
  2713. /* Preallocate enough resources for IOMMU hot-addition */
  2714. if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
  2715. g_num_of_iommus = DMAR_UNITS_SUPPORTED;
  2716. g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
  2717. GFP_KERNEL);
  2718. if (!g_iommus) {
  2719. pr_err("Allocating global iommu array failed\n");
  2720. ret = -ENOMEM;
  2721. goto error;
  2722. }
  2723. for_each_active_iommu(iommu, drhd) {
  2724. /*
  2725. * Find the max pasid size of all IOMMU's in the system.
  2726. * We need to ensure the system pasid table is no bigger
  2727. * than the smallest supported.
  2728. */
  2729. if (pasid_enabled(iommu)) {
  2730. u32 temp = 2 << ecap_pss(iommu->ecap);
  2731. intel_pasid_max_id = min_t(u32, temp,
  2732. intel_pasid_max_id);
  2733. }
  2734. g_iommus[iommu->seq_id] = iommu;
  2735. intel_iommu_init_qi(iommu);
  2736. ret = iommu_init_domains(iommu);
  2737. if (ret)
  2738. goto free_iommu;
  2739. init_translation_status(iommu);
  2740. if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
  2741. iommu_disable_translation(iommu);
  2742. clear_translation_pre_enabled(iommu);
  2743. pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
  2744. iommu->name);
  2745. }
  2746. /*
  2747. * TBD:
  2748. * we could share the same root & context tables
  2749. * among all IOMMU's. Need to Split it later.
  2750. */
  2751. ret = iommu_alloc_root_entry(iommu);
  2752. if (ret)
  2753. goto free_iommu;
  2754. if (translation_pre_enabled(iommu)) {
  2755. pr_info("Translation already enabled - trying to copy translation structures\n");
  2756. ret = copy_translation_tables(iommu);
  2757. if (ret) {
  2758. /*
  2759. * We found the IOMMU with translation
  2760. * enabled - but failed to copy over the
  2761. * old root-entry table. Try to proceed
  2762. * by disabling translation now and
  2763. * allocating a clean root-entry table.
  2764. * This might cause DMAR faults, but
  2765. * probably the dump will still succeed.
  2766. */
  2767. pr_err("Failed to copy translation tables from previous kernel for %s\n",
  2768. iommu->name);
  2769. iommu_disable_translation(iommu);
  2770. clear_translation_pre_enabled(iommu);
  2771. } else {
  2772. pr_info("Copied translation tables from previous kernel for %s\n",
  2773. iommu->name);
  2774. copied_tables = true;
  2775. }
  2776. }
  2777. if (!ecap_pass_through(iommu->ecap))
  2778. hw_pass_through = 0;
  2779. #ifdef CONFIG_INTEL_IOMMU_SVM
  2780. if (pasid_enabled(iommu))
  2781. intel_svm_init(iommu);
  2782. #endif
  2783. }
  2784. /*
  2785. * Now that qi is enabled on all iommus, set the root entry and flush
  2786. * caches. This is required on some Intel X58 chipsets, otherwise the
  2787. * flush_context function will loop forever and the boot hangs.
  2788. */
  2789. for_each_active_iommu(iommu, drhd) {
  2790. iommu_flush_write_buffer(iommu);
  2791. iommu_set_root_entry(iommu);
  2792. iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
  2793. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
  2794. }
  2795. if (iommu_pass_through)
  2796. iommu_identity_mapping |= IDENTMAP_ALL;
  2797. #ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
  2798. dmar_map_gfx = 0;
  2799. #endif
  2800. if (!dmar_map_gfx)
  2801. iommu_identity_mapping |= IDENTMAP_GFX;
  2802. check_tylersburg_isoch();
  2803. if (iommu_identity_mapping) {
  2804. ret = si_domain_init(hw_pass_through);
  2805. if (ret)
  2806. goto free_iommu;
  2807. }
  2808. /*
  2809. * If we copied translations from a previous kernel in the kdump
  2810. * case, we can not assign the devices to domains now, as that
  2811. * would eliminate the old mappings. So skip this part and defer
  2812. * the assignment to device driver initialization time.
  2813. */
  2814. if (copied_tables)
  2815. goto domains_done;
  2816. /*
  2817. * If pass through is not set or not enabled, setup context entries for
  2818. * identity mappings for rmrr, gfx, and isa and may fall back to static
  2819. * identity mapping if iommu_identity_mapping is set.
  2820. */
  2821. if (iommu_identity_mapping) {
  2822. ret = iommu_prepare_static_identity_mapping(hw_pass_through);
  2823. if (ret) {
  2824. pr_crit("Failed to setup IOMMU pass-through\n");
  2825. goto free_iommu;
  2826. }
  2827. }
  2828. /*
  2829. * For each rmrr
  2830. * for each dev attached to rmrr
  2831. * do
  2832. * locate drhd for dev, alloc domain for dev
  2833. * allocate free domain
  2834. * allocate page table entries for rmrr
  2835. * if context not allocated for bus
  2836. * allocate and init context
  2837. * set present in root table for this bus
  2838. * init context with domain, translation etc
  2839. * endfor
  2840. * endfor
  2841. */
  2842. pr_info("Setting RMRR:\n");
  2843. for_each_rmrr_units(rmrr) {
  2844. /* some BIOS lists non-exist devices in DMAR table. */
  2845. for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
  2846. i, dev) {
  2847. ret = iommu_prepare_rmrr_dev(rmrr, dev);
  2848. if (ret)
  2849. pr_err("Mapping reserved region failed\n");
  2850. }
  2851. }
  2852. iommu_prepare_isa();
  2853. domains_done:
  2854. /*
  2855. * for each drhd
  2856. * enable fault log
  2857. * global invalidate context cache
  2858. * global invalidate iotlb
  2859. * enable translation
  2860. */
  2861. for_each_iommu(iommu, drhd) {
  2862. if (drhd->ignored) {
  2863. /*
  2864. * we always have to disable PMRs or DMA may fail on
  2865. * this device
  2866. */
  2867. if (force_on)
  2868. iommu_disable_protect_mem_regions(iommu);
  2869. continue;
  2870. }
  2871. iommu_flush_write_buffer(iommu);
  2872. #ifdef CONFIG_INTEL_IOMMU_SVM
  2873. if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
  2874. ret = intel_svm_enable_prq(iommu);
  2875. if (ret)
  2876. goto free_iommu;
  2877. }
  2878. #endif
  2879. ret = dmar_set_interrupt(iommu);
  2880. if (ret)
  2881. goto free_iommu;
  2882. if (!translation_pre_enabled(iommu))
  2883. iommu_enable_translation(iommu);
  2884. iommu_disable_protect_mem_regions(iommu);
  2885. }
  2886. return 0;
  2887. free_iommu:
  2888. for_each_active_iommu(iommu, drhd) {
  2889. disable_dmar_iommu(iommu);
  2890. free_dmar_iommu(iommu);
  2891. }
  2892. kfree(g_iommus);
  2893. error:
  2894. return ret;
  2895. }
  2896. /* This takes a number of _MM_ pages, not VTD pages */
  2897. static unsigned long intel_alloc_iova(struct device *dev,
  2898. struct dmar_domain *domain,
  2899. unsigned long nrpages, uint64_t dma_mask)
  2900. {
  2901. unsigned long iova_pfn = 0;
  2902. /* Restrict dma_mask to the width that the iommu can handle */
  2903. dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
  2904. /* Ensure we reserve the whole size-aligned region */
  2905. nrpages = __roundup_pow_of_two(nrpages);
  2906. if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
  2907. /*
  2908. * First try to allocate an io virtual address in
  2909. * DMA_BIT_MASK(32) and if that fails then try allocating
  2910. * from higher range
  2911. */
  2912. iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
  2913. IOVA_PFN(DMA_BIT_MASK(32)), false);
  2914. if (iova_pfn)
  2915. return iova_pfn;
  2916. }
  2917. iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
  2918. IOVA_PFN(dma_mask), true);
  2919. if (unlikely(!iova_pfn)) {
  2920. pr_err("Allocating %ld-page iova for %s failed",
  2921. nrpages, dev_name(dev));
  2922. return 0;
  2923. }
  2924. return iova_pfn;
  2925. }
  2926. struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
  2927. {
  2928. struct dmar_domain *domain, *tmp;
  2929. struct dmar_rmrr_unit *rmrr;
  2930. struct device *i_dev;
  2931. int i, ret;
  2932. domain = find_domain(dev);
  2933. if (domain)
  2934. goto out;
  2935. domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
  2936. if (!domain)
  2937. goto out;
  2938. /* We have a new domain - setup possible RMRRs for the device */
  2939. rcu_read_lock();
  2940. for_each_rmrr_units(rmrr) {
  2941. for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
  2942. i, i_dev) {
  2943. if (i_dev != dev)
  2944. continue;
  2945. ret = domain_prepare_identity_map(dev, domain,
  2946. rmrr->base_address,
  2947. rmrr->end_address);
  2948. if (ret)
  2949. dev_err(dev, "Mapping reserved region failed\n");
  2950. }
  2951. }
  2952. rcu_read_unlock();
  2953. tmp = set_domain_for_dev(dev, domain);
  2954. if (!tmp || domain != tmp) {
  2955. domain_exit(domain);
  2956. domain = tmp;
  2957. }
  2958. out:
  2959. if (!domain)
  2960. pr_err("Allocating domain for %s failed\n", dev_name(dev));
  2961. return domain;
  2962. }
  2963. /* Check if the dev needs to go through non-identity map and unmap process.*/
  2964. static int iommu_no_mapping(struct device *dev)
  2965. {
  2966. int found;
  2967. if (iommu_dummy(dev))
  2968. return 1;
  2969. if (!iommu_identity_mapping)
  2970. return 0;
  2971. found = identity_mapping(dev);
  2972. if (found) {
  2973. if (iommu_should_identity_map(dev, 0))
  2974. return 1;
  2975. else {
  2976. /*
  2977. * 32 bit DMA is removed from si_domain and fall back
  2978. * to non-identity mapping.
  2979. */
  2980. dmar_remove_one_dev_info(si_domain, dev);
  2981. pr_info("32bit %s uses non-identity mapping\n",
  2982. dev_name(dev));
  2983. return 0;
  2984. }
  2985. } else {
  2986. /*
  2987. * In case of a detached 64 bit DMA device from vm, the device
  2988. * is put into si_domain for identity mapping.
  2989. */
  2990. if (iommu_should_identity_map(dev, 0)) {
  2991. int ret;
  2992. ret = domain_add_dev_info(si_domain, dev);
  2993. if (!ret) {
  2994. pr_info("64bit %s uses identity mapping\n",
  2995. dev_name(dev));
  2996. return 1;
  2997. }
  2998. }
  2999. }
  3000. return 0;
  3001. }
  3002. static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
  3003. size_t size, int dir, u64 dma_mask)
  3004. {
  3005. struct dmar_domain *domain;
  3006. phys_addr_t start_paddr;
  3007. unsigned long iova_pfn;
  3008. int prot = 0;
  3009. int ret;
  3010. struct intel_iommu *iommu;
  3011. unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
  3012. BUG_ON(dir == DMA_NONE);
  3013. if (iommu_no_mapping(dev))
  3014. return paddr;
  3015. domain = get_valid_domain_for_dev(dev);
  3016. if (!domain)
  3017. return 0;
  3018. iommu = domain_get_iommu(domain);
  3019. size = aligned_nrpages(paddr, size);
  3020. iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
  3021. if (!iova_pfn)
  3022. goto error;
  3023. /*
  3024. * Check if DMAR supports zero-length reads on write only
  3025. * mappings..
  3026. */
  3027. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
  3028. !cap_zlr(iommu->cap))
  3029. prot |= DMA_PTE_READ;
  3030. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  3031. prot |= DMA_PTE_WRITE;
  3032. /*
  3033. * paddr - (paddr + size) might be partial page, we should map the whole
  3034. * page. Note: if two part of one page are separately mapped, we
  3035. * might have two guest_addr mapping to the same host paddr, but this
  3036. * is not a big problem
  3037. */
  3038. ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
  3039. mm_to_dma_pfn(paddr_pfn), size, prot);
  3040. if (ret)
  3041. goto error;
  3042. start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
  3043. start_paddr += paddr & ~PAGE_MASK;
  3044. return start_paddr;
  3045. error:
  3046. if (iova_pfn)
  3047. free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
  3048. pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
  3049. dev_name(dev), size, (unsigned long long)paddr, dir);
  3050. return 0;
  3051. }
  3052. static dma_addr_t intel_map_page(struct device *dev, struct page *page,
  3053. unsigned long offset, size_t size,
  3054. enum dma_data_direction dir,
  3055. unsigned long attrs)
  3056. {
  3057. return __intel_map_single(dev, page_to_phys(page) + offset, size,
  3058. dir, *dev->dma_mask);
  3059. }
  3060. static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
  3061. {
  3062. struct dmar_domain *domain;
  3063. unsigned long start_pfn, last_pfn;
  3064. unsigned long nrpages;
  3065. unsigned long iova_pfn;
  3066. struct intel_iommu *iommu;
  3067. struct page *freelist;
  3068. if (iommu_no_mapping(dev))
  3069. return;
  3070. domain = find_domain(dev);
  3071. BUG_ON(!domain);
  3072. iommu = domain_get_iommu(domain);
  3073. iova_pfn = IOVA_PFN(dev_addr);
  3074. nrpages = aligned_nrpages(dev_addr, size);
  3075. start_pfn = mm_to_dma_pfn(iova_pfn);
  3076. last_pfn = start_pfn + nrpages - 1;
  3077. pr_debug("Device %s unmapping: pfn %lx-%lx\n",
  3078. dev_name(dev), start_pfn, last_pfn);
  3079. freelist = domain_unmap(domain, start_pfn, last_pfn);
  3080. if (intel_iommu_strict || !has_iova_flush_queue(&domain->iovad)) {
  3081. iommu_flush_iotlb_psi(iommu, domain, start_pfn,
  3082. nrpages, !freelist, 0);
  3083. /* free iova */
  3084. free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
  3085. dma_free_pagelist(freelist);
  3086. } else {
  3087. queue_iova(&domain->iovad, iova_pfn, nrpages,
  3088. (unsigned long)freelist);
  3089. /*
  3090. * queue up the release of the unmap to save the 1/6th of the
  3091. * cpu used up by the iotlb flush operation...
  3092. */
  3093. }
  3094. }
  3095. static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
  3096. size_t size, enum dma_data_direction dir,
  3097. unsigned long attrs)
  3098. {
  3099. intel_unmap(dev, dev_addr, size);
  3100. }
  3101. static void *intel_alloc_coherent(struct device *dev, size_t size,
  3102. dma_addr_t *dma_handle, gfp_t flags,
  3103. unsigned long attrs)
  3104. {
  3105. struct page *page = NULL;
  3106. int order;
  3107. size = PAGE_ALIGN(size);
  3108. order = get_order(size);
  3109. if (!iommu_no_mapping(dev))
  3110. flags &= ~(GFP_DMA | GFP_DMA32);
  3111. else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
  3112. if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
  3113. flags |= GFP_DMA;
  3114. else
  3115. flags |= GFP_DMA32;
  3116. }
  3117. if (gfpflags_allow_blocking(flags)) {
  3118. unsigned int count = size >> PAGE_SHIFT;
  3119. page = dma_alloc_from_contiguous(dev, count, order,
  3120. flags & __GFP_NOWARN);
  3121. if (page && iommu_no_mapping(dev) &&
  3122. page_to_phys(page) + size > dev->coherent_dma_mask) {
  3123. dma_release_from_contiguous(dev, page, count);
  3124. page = NULL;
  3125. }
  3126. }
  3127. if (!page)
  3128. page = alloc_pages(flags, order);
  3129. if (!page)
  3130. return NULL;
  3131. memset(page_address(page), 0, size);
  3132. *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
  3133. DMA_BIDIRECTIONAL,
  3134. dev->coherent_dma_mask);
  3135. if (*dma_handle)
  3136. return page_address(page);
  3137. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  3138. __free_pages(page, order);
  3139. return NULL;
  3140. }
  3141. static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
  3142. dma_addr_t dma_handle, unsigned long attrs)
  3143. {
  3144. int order;
  3145. struct page *page = virt_to_page(vaddr);
  3146. size = PAGE_ALIGN(size);
  3147. order = get_order(size);
  3148. intel_unmap(dev, dma_handle, size);
  3149. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  3150. __free_pages(page, order);
  3151. }
  3152. static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
  3153. int nelems, enum dma_data_direction dir,
  3154. unsigned long attrs)
  3155. {
  3156. dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
  3157. unsigned long nrpages = 0;
  3158. struct scatterlist *sg;
  3159. int i;
  3160. for_each_sg(sglist, sg, nelems, i) {
  3161. nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
  3162. }
  3163. intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
  3164. }
  3165. static int intel_nontranslate_map_sg(struct device *hddev,
  3166. struct scatterlist *sglist, int nelems, int dir)
  3167. {
  3168. int i;
  3169. struct scatterlist *sg;
  3170. for_each_sg(sglist, sg, nelems, i) {
  3171. BUG_ON(!sg_page(sg));
  3172. sg->dma_address = sg_phys(sg);
  3173. sg->dma_length = sg->length;
  3174. }
  3175. return nelems;
  3176. }
  3177. static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
  3178. enum dma_data_direction dir, unsigned long attrs)
  3179. {
  3180. int i;
  3181. struct dmar_domain *domain;
  3182. size_t size = 0;
  3183. int prot = 0;
  3184. unsigned long iova_pfn;
  3185. int ret;
  3186. struct scatterlist *sg;
  3187. unsigned long start_vpfn;
  3188. struct intel_iommu *iommu;
  3189. BUG_ON(dir == DMA_NONE);
  3190. if (iommu_no_mapping(dev))
  3191. return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
  3192. domain = get_valid_domain_for_dev(dev);
  3193. if (!domain)
  3194. return 0;
  3195. iommu = domain_get_iommu(domain);
  3196. for_each_sg(sglist, sg, nelems, i)
  3197. size += aligned_nrpages(sg->offset, sg->length);
  3198. iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
  3199. *dev->dma_mask);
  3200. if (!iova_pfn) {
  3201. sglist->dma_length = 0;
  3202. return 0;
  3203. }
  3204. /*
  3205. * Check if DMAR supports zero-length reads on write only
  3206. * mappings..
  3207. */
  3208. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
  3209. !cap_zlr(iommu->cap))
  3210. prot |= DMA_PTE_READ;
  3211. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
  3212. prot |= DMA_PTE_WRITE;
  3213. start_vpfn = mm_to_dma_pfn(iova_pfn);
  3214. ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
  3215. if (unlikely(ret)) {
  3216. dma_pte_free_pagetable(domain, start_vpfn,
  3217. start_vpfn + size - 1,
  3218. agaw_to_level(domain->agaw) + 1);
  3219. free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
  3220. return 0;
  3221. }
  3222. return nelems;
  3223. }
  3224. static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
  3225. {
  3226. return !dma_addr;
  3227. }
  3228. const struct dma_map_ops intel_dma_ops = {
  3229. .alloc = intel_alloc_coherent,
  3230. .free = intel_free_coherent,
  3231. .map_sg = intel_map_sg,
  3232. .unmap_sg = intel_unmap_sg,
  3233. .map_page = intel_map_page,
  3234. .unmap_page = intel_unmap_page,
  3235. .mapping_error = intel_mapping_error,
  3236. #ifdef CONFIG_X86
  3237. .dma_supported = dma_direct_supported,
  3238. #endif
  3239. };
  3240. static inline int iommu_domain_cache_init(void)
  3241. {
  3242. int ret = 0;
  3243. iommu_domain_cache = kmem_cache_create("iommu_domain",
  3244. sizeof(struct dmar_domain),
  3245. 0,
  3246. SLAB_HWCACHE_ALIGN,
  3247. NULL);
  3248. if (!iommu_domain_cache) {
  3249. pr_err("Couldn't create iommu_domain cache\n");
  3250. ret = -ENOMEM;
  3251. }
  3252. return ret;
  3253. }
  3254. static inline int iommu_devinfo_cache_init(void)
  3255. {
  3256. int ret = 0;
  3257. iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
  3258. sizeof(struct device_domain_info),
  3259. 0,
  3260. SLAB_HWCACHE_ALIGN,
  3261. NULL);
  3262. if (!iommu_devinfo_cache) {
  3263. pr_err("Couldn't create devinfo cache\n");
  3264. ret = -ENOMEM;
  3265. }
  3266. return ret;
  3267. }
  3268. static int __init iommu_init_mempool(void)
  3269. {
  3270. int ret;
  3271. ret = iova_cache_get();
  3272. if (ret)
  3273. return ret;
  3274. ret = iommu_domain_cache_init();
  3275. if (ret)
  3276. goto domain_error;
  3277. ret = iommu_devinfo_cache_init();
  3278. if (!ret)
  3279. return ret;
  3280. kmem_cache_destroy(iommu_domain_cache);
  3281. domain_error:
  3282. iova_cache_put();
  3283. return -ENOMEM;
  3284. }
  3285. static void __init iommu_exit_mempool(void)
  3286. {
  3287. kmem_cache_destroy(iommu_devinfo_cache);
  3288. kmem_cache_destroy(iommu_domain_cache);
  3289. iova_cache_put();
  3290. }
  3291. static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
  3292. {
  3293. struct dmar_drhd_unit *drhd;
  3294. u32 vtbar;
  3295. int rc;
  3296. /* We know that this device on this chipset has its own IOMMU.
  3297. * If we find it under a different IOMMU, then the BIOS is lying
  3298. * to us. Hope that the IOMMU for this device is actually
  3299. * disabled, and it needs no translation...
  3300. */
  3301. rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
  3302. if (rc) {
  3303. /* "can't" happen */
  3304. dev_info(&pdev->dev, "failed to run vt-d quirk\n");
  3305. return;
  3306. }
  3307. vtbar &= 0xffff0000;
  3308. /* we know that the this iommu should be at offset 0xa000 from vtbar */
  3309. drhd = dmar_find_matched_drhd_unit(pdev);
  3310. if (!drhd || drhd->reg_base_addr - vtbar != 0xa000) {
  3311. pr_warn_once(FW_BUG "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n");
  3312. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  3313. pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
  3314. }
  3315. }
  3316. DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
  3317. static void __init init_no_remapping_devices(void)
  3318. {
  3319. struct dmar_drhd_unit *drhd;
  3320. struct device *dev;
  3321. int i;
  3322. for_each_drhd_unit(drhd) {
  3323. if (!drhd->include_all) {
  3324. for_each_active_dev_scope(drhd->devices,
  3325. drhd->devices_cnt, i, dev)
  3326. break;
  3327. /* ignore DMAR unit if no devices exist */
  3328. if (i == drhd->devices_cnt)
  3329. drhd->ignored = 1;
  3330. }
  3331. }
  3332. for_each_active_drhd_unit(drhd) {
  3333. if (drhd->include_all)
  3334. continue;
  3335. for_each_active_dev_scope(drhd->devices,
  3336. drhd->devices_cnt, i, dev)
  3337. if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
  3338. break;
  3339. if (i < drhd->devices_cnt)
  3340. continue;
  3341. /* This IOMMU has *only* gfx devices. Either bypass it or
  3342. set the gfx_mapped flag, as appropriate */
  3343. if (!dmar_map_gfx) {
  3344. drhd->ignored = 1;
  3345. for_each_active_dev_scope(drhd->devices,
  3346. drhd->devices_cnt, i, dev)
  3347. dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
  3348. }
  3349. }
  3350. }
  3351. #ifdef CONFIG_SUSPEND
  3352. static int init_iommu_hw(void)
  3353. {
  3354. struct dmar_drhd_unit *drhd;
  3355. struct intel_iommu *iommu = NULL;
  3356. for_each_active_iommu(iommu, drhd)
  3357. if (iommu->qi)
  3358. dmar_reenable_qi(iommu);
  3359. for_each_iommu(iommu, drhd) {
  3360. if (drhd->ignored) {
  3361. /*
  3362. * we always have to disable PMRs or DMA may fail on
  3363. * this device
  3364. */
  3365. if (force_on)
  3366. iommu_disable_protect_mem_regions(iommu);
  3367. continue;
  3368. }
  3369. iommu_flush_write_buffer(iommu);
  3370. iommu_set_root_entry(iommu);
  3371. iommu->flush.flush_context(iommu, 0, 0, 0,
  3372. DMA_CCMD_GLOBAL_INVL);
  3373. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
  3374. iommu_enable_translation(iommu);
  3375. iommu_disable_protect_mem_regions(iommu);
  3376. }
  3377. return 0;
  3378. }
  3379. static void iommu_flush_all(void)
  3380. {
  3381. struct dmar_drhd_unit *drhd;
  3382. struct intel_iommu *iommu;
  3383. for_each_active_iommu(iommu, drhd) {
  3384. iommu->flush.flush_context(iommu, 0, 0, 0,
  3385. DMA_CCMD_GLOBAL_INVL);
  3386. iommu->flush.flush_iotlb(iommu, 0, 0, 0,
  3387. DMA_TLB_GLOBAL_FLUSH);
  3388. }
  3389. }
  3390. static int iommu_suspend(void)
  3391. {
  3392. struct dmar_drhd_unit *drhd;
  3393. struct intel_iommu *iommu = NULL;
  3394. unsigned long flag;
  3395. for_each_active_iommu(iommu, drhd) {
  3396. iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32),
  3397. GFP_ATOMIC);
  3398. if (!iommu->iommu_state)
  3399. goto nomem;
  3400. }
  3401. iommu_flush_all();
  3402. for_each_active_iommu(iommu, drhd) {
  3403. iommu_disable_translation(iommu);
  3404. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  3405. iommu->iommu_state[SR_DMAR_FECTL_REG] =
  3406. readl(iommu->reg + DMAR_FECTL_REG);
  3407. iommu->iommu_state[SR_DMAR_FEDATA_REG] =
  3408. readl(iommu->reg + DMAR_FEDATA_REG);
  3409. iommu->iommu_state[SR_DMAR_FEADDR_REG] =
  3410. readl(iommu->reg + DMAR_FEADDR_REG);
  3411. iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
  3412. readl(iommu->reg + DMAR_FEUADDR_REG);
  3413. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  3414. }
  3415. return 0;
  3416. nomem:
  3417. for_each_active_iommu(iommu, drhd)
  3418. kfree(iommu->iommu_state);
  3419. return -ENOMEM;
  3420. }
  3421. static void iommu_resume(void)
  3422. {
  3423. struct dmar_drhd_unit *drhd;
  3424. struct intel_iommu *iommu = NULL;
  3425. unsigned long flag;
  3426. if (init_iommu_hw()) {
  3427. if (force_on)
  3428. panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
  3429. else
  3430. WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
  3431. return;
  3432. }
  3433. for_each_active_iommu(iommu, drhd) {
  3434. raw_spin_lock_irqsave(&iommu->register_lock, flag);
  3435. writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
  3436. iommu->reg + DMAR_FECTL_REG);
  3437. writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
  3438. iommu->reg + DMAR_FEDATA_REG);
  3439. writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
  3440. iommu->reg + DMAR_FEADDR_REG);
  3441. writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
  3442. iommu->reg + DMAR_FEUADDR_REG);
  3443. raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
  3444. }
  3445. for_each_active_iommu(iommu, drhd)
  3446. kfree(iommu->iommu_state);
  3447. }
  3448. static struct syscore_ops iommu_syscore_ops = {
  3449. .resume = iommu_resume,
  3450. .suspend = iommu_suspend,
  3451. };
  3452. static void __init init_iommu_pm_ops(void)
  3453. {
  3454. register_syscore_ops(&iommu_syscore_ops);
  3455. }
  3456. #else
  3457. static inline void init_iommu_pm_ops(void) {}
  3458. #endif /* CONFIG_PM */
  3459. int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
  3460. {
  3461. struct acpi_dmar_reserved_memory *rmrr;
  3462. struct dmar_rmrr_unit *rmrru;
  3463. size_t length;
  3464. rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
  3465. if (!rmrru)
  3466. goto out;
  3467. rmrru->hdr = header;
  3468. rmrr = (struct acpi_dmar_reserved_memory *)header;
  3469. rmrru->base_address = rmrr->base_address;
  3470. rmrru->end_address = rmrr->end_address;
  3471. length = rmrr->end_address - rmrr->base_address + 1;
  3472. rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
  3473. ((void *)rmrr) + rmrr->header.length,
  3474. &rmrru->devices_cnt);
  3475. if (rmrru->devices_cnt && rmrru->devices == NULL)
  3476. goto free_rmrru;
  3477. list_add(&rmrru->list, &dmar_rmrr_units);
  3478. return 0;
  3479. free_rmrru:
  3480. kfree(rmrru);
  3481. out:
  3482. return -ENOMEM;
  3483. }
  3484. static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
  3485. {
  3486. struct dmar_atsr_unit *atsru;
  3487. struct acpi_dmar_atsr *tmp;
  3488. list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
  3489. tmp = (struct acpi_dmar_atsr *)atsru->hdr;
  3490. if (atsr->segment != tmp->segment)
  3491. continue;
  3492. if (atsr->header.length != tmp->header.length)
  3493. continue;
  3494. if (memcmp(atsr, tmp, atsr->header.length) == 0)
  3495. return atsru;
  3496. }
  3497. return NULL;
  3498. }
  3499. int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
  3500. {
  3501. struct acpi_dmar_atsr *atsr;
  3502. struct dmar_atsr_unit *atsru;
  3503. if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
  3504. return 0;
  3505. atsr = container_of(hdr, struct acpi_dmar_atsr, header);
  3506. atsru = dmar_find_atsr(atsr);
  3507. if (atsru)
  3508. return 0;
  3509. atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
  3510. if (!atsru)
  3511. return -ENOMEM;
  3512. /*
  3513. * If memory is allocated from slab by ACPI _DSM method, we need to
  3514. * copy the memory content because the memory buffer will be freed
  3515. * on return.
  3516. */
  3517. atsru->hdr = (void *)(atsru + 1);
  3518. memcpy(atsru->hdr, hdr, hdr->length);
  3519. atsru->include_all = atsr->flags & 0x1;
  3520. if (!atsru->include_all) {
  3521. atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
  3522. (void *)atsr + atsr->header.length,
  3523. &atsru->devices_cnt);
  3524. if (atsru->devices_cnt && atsru->devices == NULL) {
  3525. kfree(atsru);
  3526. return -ENOMEM;
  3527. }
  3528. }
  3529. list_add_rcu(&atsru->list, &dmar_atsr_units);
  3530. return 0;
  3531. }
  3532. static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
  3533. {
  3534. dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
  3535. kfree(atsru);
  3536. }
  3537. int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
  3538. {
  3539. struct acpi_dmar_atsr *atsr;
  3540. struct dmar_atsr_unit *atsru;
  3541. atsr = container_of(hdr, struct acpi_dmar_atsr, header);
  3542. atsru = dmar_find_atsr(atsr);
  3543. if (atsru) {
  3544. list_del_rcu(&atsru->list);
  3545. synchronize_rcu();
  3546. intel_iommu_free_atsr(atsru);
  3547. }
  3548. return 0;
  3549. }
  3550. int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
  3551. {
  3552. int i;
  3553. struct device *dev;
  3554. struct acpi_dmar_atsr *atsr;
  3555. struct dmar_atsr_unit *atsru;
  3556. atsr = container_of(hdr, struct acpi_dmar_atsr, header);
  3557. atsru = dmar_find_atsr(atsr);
  3558. if (!atsru)
  3559. return 0;
  3560. if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
  3561. for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
  3562. i, dev)
  3563. return -EBUSY;
  3564. }
  3565. return 0;
  3566. }
  3567. static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
  3568. {
  3569. int sp, ret = 0;
  3570. struct intel_iommu *iommu = dmaru->iommu;
  3571. if (g_iommus[iommu->seq_id])
  3572. return 0;
  3573. if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
  3574. pr_warn("%s: Doesn't support hardware pass through.\n",
  3575. iommu->name);
  3576. return -ENXIO;
  3577. }
  3578. if (!ecap_sc_support(iommu->ecap) &&
  3579. domain_update_iommu_snooping(iommu)) {
  3580. pr_warn("%s: Doesn't support snooping.\n",
  3581. iommu->name);
  3582. return -ENXIO;
  3583. }
  3584. sp = domain_update_iommu_superpage(iommu) - 1;
  3585. if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
  3586. pr_warn("%s: Doesn't support large page.\n",
  3587. iommu->name);
  3588. return -ENXIO;
  3589. }
  3590. /*
  3591. * Disable translation if already enabled prior to OS handover.
  3592. */
  3593. if (iommu->gcmd & DMA_GCMD_TE)
  3594. iommu_disable_translation(iommu);
  3595. g_iommus[iommu->seq_id] = iommu;
  3596. ret = iommu_init_domains(iommu);
  3597. if (ret == 0)
  3598. ret = iommu_alloc_root_entry(iommu);
  3599. if (ret)
  3600. goto out;
  3601. #ifdef CONFIG_INTEL_IOMMU_SVM
  3602. if (pasid_enabled(iommu))
  3603. intel_svm_init(iommu);
  3604. #endif
  3605. if (dmaru->ignored) {
  3606. /*
  3607. * we always have to disable PMRs or DMA may fail on this device
  3608. */
  3609. if (force_on)
  3610. iommu_disable_protect_mem_regions(iommu);
  3611. return 0;
  3612. }
  3613. intel_iommu_init_qi(iommu);
  3614. iommu_flush_write_buffer(iommu);
  3615. #ifdef CONFIG_INTEL_IOMMU_SVM
  3616. if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
  3617. ret = intel_svm_enable_prq(iommu);
  3618. if (ret)
  3619. goto disable_iommu;
  3620. }
  3621. #endif
  3622. ret = dmar_set_interrupt(iommu);
  3623. if (ret)
  3624. goto disable_iommu;
  3625. iommu_set_root_entry(iommu);
  3626. iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
  3627. iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
  3628. iommu_enable_translation(iommu);
  3629. iommu_disable_protect_mem_regions(iommu);
  3630. return 0;
  3631. disable_iommu:
  3632. disable_dmar_iommu(iommu);
  3633. out:
  3634. free_dmar_iommu(iommu);
  3635. return ret;
  3636. }
  3637. int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
  3638. {
  3639. int ret = 0;
  3640. struct intel_iommu *iommu = dmaru->iommu;
  3641. if (!intel_iommu_enabled)
  3642. return 0;
  3643. if (iommu == NULL)
  3644. return -EINVAL;
  3645. if (insert) {
  3646. ret = intel_iommu_add(dmaru);
  3647. } else {
  3648. disable_dmar_iommu(iommu);
  3649. free_dmar_iommu(iommu);
  3650. }
  3651. return ret;
  3652. }
  3653. static void intel_iommu_free_dmars(void)
  3654. {
  3655. struct dmar_rmrr_unit *rmrru, *rmrr_n;
  3656. struct dmar_atsr_unit *atsru, *atsr_n;
  3657. list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
  3658. list_del(&rmrru->list);
  3659. dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
  3660. kfree(rmrru);
  3661. }
  3662. list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
  3663. list_del(&atsru->list);
  3664. intel_iommu_free_atsr(atsru);
  3665. }
  3666. }
  3667. int dmar_find_matched_atsr_unit(struct pci_dev *dev)
  3668. {
  3669. int i, ret = 1;
  3670. struct pci_bus *bus;
  3671. struct pci_dev *bridge = NULL;
  3672. struct device *tmp;
  3673. struct acpi_dmar_atsr *atsr;
  3674. struct dmar_atsr_unit *atsru;
  3675. dev = pci_physfn(dev);
  3676. for (bus = dev->bus; bus; bus = bus->parent) {
  3677. bridge = bus->self;
  3678. /* If it's an integrated device, allow ATS */
  3679. if (!bridge)
  3680. return 1;
  3681. /* Connected via non-PCIe: no ATS */
  3682. if (!pci_is_pcie(bridge) ||
  3683. pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
  3684. return 0;
  3685. /* If we found the root port, look it up in the ATSR */
  3686. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
  3687. break;
  3688. }
  3689. rcu_read_lock();
  3690. list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
  3691. atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
  3692. if (atsr->segment != pci_domain_nr(dev->bus))
  3693. continue;
  3694. for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
  3695. if (tmp == &bridge->dev)
  3696. goto out;
  3697. if (atsru->include_all)
  3698. goto out;
  3699. }
  3700. ret = 0;
  3701. out:
  3702. rcu_read_unlock();
  3703. return ret;
  3704. }
  3705. int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
  3706. {
  3707. int ret = 0;
  3708. struct dmar_rmrr_unit *rmrru;
  3709. struct dmar_atsr_unit *atsru;
  3710. struct acpi_dmar_atsr *atsr;
  3711. struct acpi_dmar_reserved_memory *rmrr;
  3712. if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
  3713. return 0;
  3714. list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
  3715. rmrr = container_of(rmrru->hdr,
  3716. struct acpi_dmar_reserved_memory, header);
  3717. if (info->event == BUS_NOTIFY_ADD_DEVICE) {
  3718. ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
  3719. ((void *)rmrr) + rmrr->header.length,
  3720. rmrr->segment, rmrru->devices,
  3721. rmrru->devices_cnt);
  3722. if(ret < 0)
  3723. return ret;
  3724. } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
  3725. dmar_remove_dev_scope(info, rmrr->segment,
  3726. rmrru->devices, rmrru->devices_cnt);
  3727. }
  3728. }
  3729. list_for_each_entry(atsru, &dmar_atsr_units, list) {
  3730. if (atsru->include_all)
  3731. continue;
  3732. atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
  3733. if (info->event == BUS_NOTIFY_ADD_DEVICE) {
  3734. ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
  3735. (void *)atsr + atsr->header.length,
  3736. atsr->segment, atsru->devices,
  3737. atsru->devices_cnt);
  3738. if (ret > 0)
  3739. break;
  3740. else if(ret < 0)
  3741. return ret;
  3742. } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
  3743. if (dmar_remove_dev_scope(info, atsr->segment,
  3744. atsru->devices, atsru->devices_cnt))
  3745. break;
  3746. }
  3747. }
  3748. return 0;
  3749. }
  3750. /*
  3751. * Here we only respond to action of unbound device from driver.
  3752. *
  3753. * Added device is not attached to its DMAR domain here yet. That will happen
  3754. * when mapping the device to iova.
  3755. */
  3756. static int device_notifier(struct notifier_block *nb,
  3757. unsigned long action, void *data)
  3758. {
  3759. struct device *dev = data;
  3760. struct dmar_domain *domain;
  3761. if (iommu_dummy(dev))
  3762. return 0;
  3763. if (action != BUS_NOTIFY_REMOVED_DEVICE)
  3764. return 0;
  3765. domain = find_domain(dev);
  3766. if (!domain)
  3767. return 0;
  3768. dmar_remove_one_dev_info(domain, dev);
  3769. if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
  3770. domain_exit(domain);
  3771. return 0;
  3772. }
  3773. static struct notifier_block device_nb = {
  3774. .notifier_call = device_notifier,
  3775. };
  3776. static int intel_iommu_memory_notifier(struct notifier_block *nb,
  3777. unsigned long val, void *v)
  3778. {
  3779. struct memory_notify *mhp = v;
  3780. unsigned long long start, end;
  3781. unsigned long start_vpfn, last_vpfn;
  3782. switch (val) {
  3783. case MEM_GOING_ONLINE:
  3784. start = mhp->start_pfn << PAGE_SHIFT;
  3785. end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
  3786. if (iommu_domain_identity_map(si_domain, start, end)) {
  3787. pr_warn("Failed to build identity map for [%llx-%llx]\n",
  3788. start, end);
  3789. return NOTIFY_BAD;
  3790. }
  3791. break;
  3792. case MEM_OFFLINE:
  3793. case MEM_CANCEL_ONLINE:
  3794. start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
  3795. last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
  3796. while (start_vpfn <= last_vpfn) {
  3797. struct iova *iova;
  3798. struct dmar_drhd_unit *drhd;
  3799. struct intel_iommu *iommu;
  3800. struct page *freelist;
  3801. iova = find_iova(&si_domain->iovad, start_vpfn);
  3802. if (iova == NULL) {
  3803. pr_debug("Failed get IOVA for PFN %lx\n",
  3804. start_vpfn);
  3805. break;
  3806. }
  3807. iova = split_and_remove_iova(&si_domain->iovad, iova,
  3808. start_vpfn, last_vpfn);
  3809. if (iova == NULL) {
  3810. pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
  3811. start_vpfn, last_vpfn);
  3812. return NOTIFY_BAD;
  3813. }
  3814. freelist = domain_unmap(si_domain, iova->pfn_lo,
  3815. iova->pfn_hi);
  3816. rcu_read_lock();
  3817. for_each_active_iommu(iommu, drhd)
  3818. iommu_flush_iotlb_psi(iommu, si_domain,
  3819. iova->pfn_lo, iova_size(iova),
  3820. !freelist, 0);
  3821. rcu_read_unlock();
  3822. dma_free_pagelist(freelist);
  3823. start_vpfn = iova->pfn_hi + 1;
  3824. free_iova_mem(iova);
  3825. }
  3826. break;
  3827. }
  3828. return NOTIFY_OK;
  3829. }
  3830. static struct notifier_block intel_iommu_memory_nb = {
  3831. .notifier_call = intel_iommu_memory_notifier,
  3832. .priority = 0
  3833. };
  3834. static void free_all_cpu_cached_iovas(unsigned int cpu)
  3835. {
  3836. int i;
  3837. for (i = 0; i < g_num_of_iommus; i++) {
  3838. struct intel_iommu *iommu = g_iommus[i];
  3839. struct dmar_domain *domain;
  3840. int did;
  3841. if (!iommu)
  3842. continue;
  3843. for (did = 0; did < cap_ndoms(iommu->cap); did++) {
  3844. domain = get_iommu_domain(iommu, (u16)did);
  3845. if (!domain)
  3846. continue;
  3847. free_cpu_cached_iovas(cpu, &domain->iovad);
  3848. }
  3849. }
  3850. }
  3851. static int intel_iommu_cpu_dead(unsigned int cpu)
  3852. {
  3853. free_all_cpu_cached_iovas(cpu);
  3854. return 0;
  3855. }
  3856. static void intel_disable_iommus(void)
  3857. {
  3858. struct intel_iommu *iommu = NULL;
  3859. struct dmar_drhd_unit *drhd;
  3860. for_each_iommu(iommu, drhd)
  3861. iommu_disable_translation(iommu);
  3862. }
  3863. static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev)
  3864. {
  3865. struct iommu_device *iommu_dev = dev_to_iommu_device(dev);
  3866. return container_of(iommu_dev, struct intel_iommu, iommu);
  3867. }
  3868. static ssize_t intel_iommu_show_version(struct device *dev,
  3869. struct device_attribute *attr,
  3870. char *buf)
  3871. {
  3872. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3873. u32 ver = readl(iommu->reg + DMAR_VER_REG);
  3874. return sprintf(buf, "%d:%d\n",
  3875. DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
  3876. }
  3877. static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
  3878. static ssize_t intel_iommu_show_address(struct device *dev,
  3879. struct device_attribute *attr,
  3880. char *buf)
  3881. {
  3882. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3883. return sprintf(buf, "%llx\n", iommu->reg_phys);
  3884. }
  3885. static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
  3886. static ssize_t intel_iommu_show_cap(struct device *dev,
  3887. struct device_attribute *attr,
  3888. char *buf)
  3889. {
  3890. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3891. return sprintf(buf, "%llx\n", iommu->cap);
  3892. }
  3893. static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
  3894. static ssize_t intel_iommu_show_ecap(struct device *dev,
  3895. struct device_attribute *attr,
  3896. char *buf)
  3897. {
  3898. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3899. return sprintf(buf, "%llx\n", iommu->ecap);
  3900. }
  3901. static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
  3902. static ssize_t intel_iommu_show_ndoms(struct device *dev,
  3903. struct device_attribute *attr,
  3904. char *buf)
  3905. {
  3906. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3907. return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
  3908. }
  3909. static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
  3910. static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
  3911. struct device_attribute *attr,
  3912. char *buf)
  3913. {
  3914. struct intel_iommu *iommu = dev_to_intel_iommu(dev);
  3915. return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
  3916. cap_ndoms(iommu->cap)));
  3917. }
  3918. static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
  3919. static struct attribute *intel_iommu_attrs[] = {
  3920. &dev_attr_version.attr,
  3921. &dev_attr_address.attr,
  3922. &dev_attr_cap.attr,
  3923. &dev_attr_ecap.attr,
  3924. &dev_attr_domains_supported.attr,
  3925. &dev_attr_domains_used.attr,
  3926. NULL,
  3927. };
  3928. static struct attribute_group intel_iommu_group = {
  3929. .name = "intel-iommu",
  3930. .attrs = intel_iommu_attrs,
  3931. };
  3932. const struct attribute_group *intel_iommu_groups[] = {
  3933. &intel_iommu_group,
  3934. NULL,
  3935. };
  3936. int __init intel_iommu_init(void)
  3937. {
  3938. int ret = -ENODEV;
  3939. struct dmar_drhd_unit *drhd;
  3940. struct intel_iommu *iommu;
  3941. /* VT-d is required for a TXT/tboot launch, so enforce that */
  3942. force_on = tboot_force_iommu();
  3943. if (iommu_init_mempool()) {
  3944. if (force_on)
  3945. panic("tboot: Failed to initialize iommu memory\n");
  3946. return -ENOMEM;
  3947. }
  3948. down_write(&dmar_global_lock);
  3949. if (dmar_table_init()) {
  3950. if (force_on)
  3951. panic("tboot: Failed to initialize DMAR table\n");
  3952. goto out_free_dmar;
  3953. }
  3954. if (dmar_dev_scope_init() < 0) {
  3955. if (force_on)
  3956. panic("tboot: Failed to initialize DMAR device scope\n");
  3957. goto out_free_dmar;
  3958. }
  3959. up_write(&dmar_global_lock);
  3960. /*
  3961. * The bus notifier takes the dmar_global_lock, so lockdep will
  3962. * complain later when we register it under the lock.
  3963. */
  3964. dmar_register_bus_notifier();
  3965. down_write(&dmar_global_lock);
  3966. if (no_iommu || dmar_disabled) {
  3967. /*
  3968. * We exit the function here to ensure IOMMU's remapping and
  3969. * mempool aren't setup, which means that the IOMMU's PMRs
  3970. * won't be disabled via the call to init_dmars(). So disable
  3971. * it explicitly here. The PMRs were setup by tboot prior to
  3972. * calling SENTER, but the kernel is expected to reset/tear
  3973. * down the PMRs.
  3974. */
  3975. if (intel_iommu_tboot_noforce) {
  3976. for_each_iommu(iommu, drhd)
  3977. iommu_disable_protect_mem_regions(iommu);
  3978. }
  3979. /*
  3980. * Make sure the IOMMUs are switched off, even when we
  3981. * boot into a kexec kernel and the previous kernel left
  3982. * them enabled
  3983. */
  3984. intel_disable_iommus();
  3985. goto out_free_dmar;
  3986. }
  3987. if (list_empty(&dmar_rmrr_units))
  3988. pr_info("No RMRR found\n");
  3989. if (list_empty(&dmar_atsr_units))
  3990. pr_info("No ATSR found\n");
  3991. if (dmar_init_reserved_ranges()) {
  3992. if (force_on)
  3993. panic("tboot: Failed to reserve iommu ranges\n");
  3994. goto out_free_reserved_range;
  3995. }
  3996. if (dmar_map_gfx)
  3997. intel_iommu_gfx_mapped = 1;
  3998. init_no_remapping_devices();
  3999. ret = init_dmars();
  4000. if (ret) {
  4001. if (force_on)
  4002. panic("tboot: Failed to initialize DMARs\n");
  4003. pr_err("Initialization failed\n");
  4004. goto out_free_reserved_range;
  4005. }
  4006. up_write(&dmar_global_lock);
  4007. pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
  4008. #if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
  4009. swiotlb = 0;
  4010. #endif
  4011. dma_ops = &intel_dma_ops;
  4012. init_iommu_pm_ops();
  4013. for_each_active_iommu(iommu, drhd) {
  4014. iommu_device_sysfs_add(&iommu->iommu, NULL,
  4015. intel_iommu_groups,
  4016. "%s", iommu->name);
  4017. iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
  4018. iommu_device_register(&iommu->iommu);
  4019. }
  4020. bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
  4021. bus_register_notifier(&pci_bus_type, &device_nb);
  4022. if (si_domain && !hw_pass_through)
  4023. register_memory_notifier(&intel_iommu_memory_nb);
  4024. cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL,
  4025. intel_iommu_cpu_dead);
  4026. intel_iommu_enabled = 1;
  4027. return 0;
  4028. out_free_reserved_range:
  4029. put_iova_domain(&reserved_iova_list);
  4030. out_free_dmar:
  4031. intel_iommu_free_dmars();
  4032. up_write(&dmar_global_lock);
  4033. iommu_exit_mempool();
  4034. return ret;
  4035. }
  4036. static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
  4037. {
  4038. struct intel_iommu *iommu = opaque;
  4039. domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
  4040. return 0;
  4041. }
  4042. /*
  4043. * NB - intel-iommu lacks any sort of reference counting for the users of
  4044. * dependent devices. If multiple endpoints have intersecting dependent
  4045. * devices, unbinding the driver from any one of them will possibly leave
  4046. * the others unable to operate.
  4047. */
  4048. static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
  4049. {
  4050. if (!iommu || !dev || !dev_is_pci(dev))
  4051. return;
  4052. pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
  4053. }
  4054. static void __dmar_remove_one_dev_info(struct device_domain_info *info)
  4055. {
  4056. struct intel_iommu *iommu;
  4057. unsigned long flags;
  4058. assert_spin_locked(&device_domain_lock);
  4059. if (WARN_ON(!info))
  4060. return;
  4061. iommu = info->iommu;
  4062. if (info->dev) {
  4063. iommu_disable_dev_iotlb(info);
  4064. domain_context_clear(iommu, info->dev);
  4065. intel_pasid_free_table(info->dev);
  4066. }
  4067. unlink_domain_info(info);
  4068. spin_lock_irqsave(&iommu->lock, flags);
  4069. domain_detach_iommu(info->domain, iommu);
  4070. spin_unlock_irqrestore(&iommu->lock, flags);
  4071. free_devinfo_mem(info);
  4072. }
  4073. static void dmar_remove_one_dev_info(struct dmar_domain *domain,
  4074. struct device *dev)
  4075. {
  4076. struct device_domain_info *info;
  4077. unsigned long flags;
  4078. spin_lock_irqsave(&device_domain_lock, flags);
  4079. info = dev->archdata.iommu;
  4080. __dmar_remove_one_dev_info(info);
  4081. spin_unlock_irqrestore(&device_domain_lock, flags);
  4082. }
  4083. static int md_domain_init(struct dmar_domain *domain, int guest_width)
  4084. {
  4085. int adjust_width;
  4086. init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
  4087. domain_reserve_special_ranges(domain);
  4088. /* calculate AGAW */
  4089. domain->gaw = guest_width;
  4090. adjust_width = guestwidth_to_adjustwidth(guest_width);
  4091. domain->agaw = width_to_agaw(adjust_width);
  4092. domain->iommu_coherency = 0;
  4093. domain->iommu_snooping = 0;
  4094. domain->iommu_superpage = 0;
  4095. domain->max_addr = 0;
  4096. /* always allocate the top pgd */
  4097. domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
  4098. if (!domain->pgd)
  4099. return -ENOMEM;
  4100. domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
  4101. return 0;
  4102. }
  4103. static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
  4104. {
  4105. struct dmar_domain *dmar_domain;
  4106. struct iommu_domain *domain;
  4107. if (type != IOMMU_DOMAIN_UNMANAGED)
  4108. return NULL;
  4109. dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
  4110. if (!dmar_domain) {
  4111. pr_err("Can't allocate dmar_domain\n");
  4112. return NULL;
  4113. }
  4114. if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
  4115. pr_err("Domain initialization failed\n");
  4116. domain_exit(dmar_domain);
  4117. return NULL;
  4118. }
  4119. domain_update_iommu_cap(dmar_domain);
  4120. domain = &dmar_domain->domain;
  4121. domain->geometry.aperture_start = 0;
  4122. domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
  4123. domain->geometry.force_aperture = true;
  4124. return domain;
  4125. }
  4126. static void intel_iommu_domain_free(struct iommu_domain *domain)
  4127. {
  4128. domain_exit(to_dmar_domain(domain));
  4129. }
  4130. static int intel_iommu_attach_device(struct iommu_domain *domain,
  4131. struct device *dev)
  4132. {
  4133. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  4134. struct intel_iommu *iommu;
  4135. int addr_width;
  4136. u8 bus, devfn;
  4137. if (device_is_rmrr_locked(dev)) {
  4138. dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
  4139. return -EPERM;
  4140. }
  4141. /* normally dev is not mapped */
  4142. if (unlikely(domain_context_mapped(dev))) {
  4143. struct dmar_domain *old_domain;
  4144. old_domain = find_domain(dev);
  4145. if (old_domain) {
  4146. rcu_read_lock();
  4147. dmar_remove_one_dev_info(old_domain, dev);
  4148. rcu_read_unlock();
  4149. if (!domain_type_is_vm_or_si(old_domain) &&
  4150. list_empty(&old_domain->devices))
  4151. domain_exit(old_domain);
  4152. }
  4153. }
  4154. iommu = device_to_iommu(dev, &bus, &devfn);
  4155. if (!iommu)
  4156. return -ENODEV;
  4157. /* check if this iommu agaw is sufficient for max mapped address */
  4158. addr_width = agaw_to_width(iommu->agaw);
  4159. if (addr_width > cap_mgaw(iommu->cap))
  4160. addr_width = cap_mgaw(iommu->cap);
  4161. if (dmar_domain->max_addr > (1LL << addr_width)) {
  4162. pr_err("%s: iommu width (%d) is not "
  4163. "sufficient for the mapped address (%llx)\n",
  4164. __func__, addr_width, dmar_domain->max_addr);
  4165. return -EFAULT;
  4166. }
  4167. dmar_domain->gaw = addr_width;
  4168. /*
  4169. * Knock out extra levels of page tables if necessary
  4170. */
  4171. while (iommu->agaw < dmar_domain->agaw) {
  4172. struct dma_pte *pte;
  4173. pte = dmar_domain->pgd;
  4174. if (dma_pte_present(pte)) {
  4175. dmar_domain->pgd = (struct dma_pte *)
  4176. phys_to_virt(dma_pte_addr(pte));
  4177. free_pgtable_page(pte);
  4178. }
  4179. dmar_domain->agaw--;
  4180. }
  4181. return domain_add_dev_info(dmar_domain, dev);
  4182. }
  4183. static void intel_iommu_detach_device(struct iommu_domain *domain,
  4184. struct device *dev)
  4185. {
  4186. dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
  4187. }
  4188. static int intel_iommu_map(struct iommu_domain *domain,
  4189. unsigned long iova, phys_addr_t hpa,
  4190. size_t size, int iommu_prot)
  4191. {
  4192. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  4193. u64 max_addr;
  4194. int prot = 0;
  4195. int ret;
  4196. if (iommu_prot & IOMMU_READ)
  4197. prot |= DMA_PTE_READ;
  4198. if (iommu_prot & IOMMU_WRITE)
  4199. prot |= DMA_PTE_WRITE;
  4200. if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
  4201. prot |= DMA_PTE_SNP;
  4202. max_addr = iova + size;
  4203. if (dmar_domain->max_addr < max_addr) {
  4204. u64 end;
  4205. /* check if minimum agaw is sufficient for mapped address */
  4206. end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
  4207. if (end < max_addr) {
  4208. pr_err("%s: iommu width (%d) is not "
  4209. "sufficient for the mapped address (%llx)\n",
  4210. __func__, dmar_domain->gaw, max_addr);
  4211. return -EFAULT;
  4212. }
  4213. dmar_domain->max_addr = max_addr;
  4214. }
  4215. /* Round up size to next multiple of PAGE_SIZE, if it and
  4216. the low bits of hpa would take us onto the next page */
  4217. size = aligned_nrpages(hpa, size);
  4218. ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
  4219. hpa >> VTD_PAGE_SHIFT, size, prot);
  4220. return ret;
  4221. }
  4222. static size_t intel_iommu_unmap(struct iommu_domain *domain,
  4223. unsigned long iova, size_t size)
  4224. {
  4225. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  4226. struct page *freelist = NULL;
  4227. unsigned long start_pfn, last_pfn;
  4228. unsigned int npages;
  4229. int iommu_id, level = 0;
  4230. /* Cope with horrid API which requires us to unmap more than the
  4231. size argument if it happens to be a large-page mapping. */
  4232. BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
  4233. if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
  4234. size = VTD_PAGE_SIZE << level_to_offset_bits(level);
  4235. start_pfn = iova >> VTD_PAGE_SHIFT;
  4236. last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
  4237. freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
  4238. npages = last_pfn - start_pfn + 1;
  4239. for_each_domain_iommu(iommu_id, dmar_domain)
  4240. iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
  4241. start_pfn, npages, !freelist, 0);
  4242. dma_free_pagelist(freelist);
  4243. if (dmar_domain->max_addr == iova + size)
  4244. dmar_domain->max_addr = iova;
  4245. return size;
  4246. }
  4247. static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
  4248. dma_addr_t iova)
  4249. {
  4250. struct dmar_domain *dmar_domain = to_dmar_domain(domain);
  4251. struct dma_pte *pte;
  4252. int level = 0;
  4253. u64 phys = 0;
  4254. pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
  4255. if (pte && dma_pte_present(pte))
  4256. phys = dma_pte_addr(pte) +
  4257. (iova & (BIT_MASK(level_to_offset_bits(level) +
  4258. VTD_PAGE_SHIFT) - 1));
  4259. return phys;
  4260. }
  4261. static bool intel_iommu_capable(enum iommu_cap cap)
  4262. {
  4263. if (cap == IOMMU_CAP_CACHE_COHERENCY)
  4264. return domain_update_iommu_snooping(NULL) == 1;
  4265. if (cap == IOMMU_CAP_INTR_REMAP)
  4266. return irq_remapping_enabled == 1;
  4267. return false;
  4268. }
  4269. static int intel_iommu_add_device(struct device *dev)
  4270. {
  4271. struct intel_iommu *iommu;
  4272. struct iommu_group *group;
  4273. u8 bus, devfn;
  4274. iommu = device_to_iommu(dev, &bus, &devfn);
  4275. if (!iommu)
  4276. return -ENODEV;
  4277. iommu_device_link(&iommu->iommu, dev);
  4278. group = iommu_group_get_for_dev(dev);
  4279. if (IS_ERR(group))
  4280. return PTR_ERR(group);
  4281. iommu_group_put(group);
  4282. return 0;
  4283. }
  4284. static void intel_iommu_remove_device(struct device *dev)
  4285. {
  4286. struct intel_iommu *iommu;
  4287. u8 bus, devfn;
  4288. iommu = device_to_iommu(dev, &bus, &devfn);
  4289. if (!iommu)
  4290. return;
  4291. iommu_group_remove_device(dev);
  4292. iommu_device_unlink(&iommu->iommu, dev);
  4293. }
  4294. static void intel_iommu_get_resv_regions(struct device *device,
  4295. struct list_head *head)
  4296. {
  4297. int prot = DMA_PTE_READ | DMA_PTE_WRITE;
  4298. struct iommu_resv_region *reg;
  4299. struct dmar_rmrr_unit *rmrr;
  4300. struct device *i_dev;
  4301. int i;
  4302. down_read(&dmar_global_lock);
  4303. for_each_rmrr_units(rmrr) {
  4304. for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
  4305. i, i_dev) {
  4306. struct iommu_resv_region *resv;
  4307. size_t length;
  4308. if (i_dev != device)
  4309. continue;
  4310. length = rmrr->end_address - rmrr->base_address + 1;
  4311. resv = iommu_alloc_resv_region(rmrr->base_address,
  4312. length, prot,
  4313. IOMMU_RESV_DIRECT);
  4314. if (!resv)
  4315. break;
  4316. list_add_tail(&resv->list, head);
  4317. }
  4318. }
  4319. up_read(&dmar_global_lock);
  4320. reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
  4321. IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
  4322. 0, IOMMU_RESV_MSI);
  4323. if (!reg)
  4324. return;
  4325. list_add_tail(&reg->list, head);
  4326. }
  4327. static void intel_iommu_put_resv_regions(struct device *dev,
  4328. struct list_head *head)
  4329. {
  4330. struct iommu_resv_region *entry, *next;
  4331. list_for_each_entry_safe(entry, next, head, list)
  4332. kfree(entry);
  4333. }
  4334. #ifdef CONFIG_INTEL_IOMMU_SVM
  4335. #define MAX_NR_PASID_BITS (20)
  4336. static inline unsigned long intel_iommu_get_pts(struct device *dev)
  4337. {
  4338. int pts, max_pasid;
  4339. max_pasid = intel_pasid_get_dev_max_id(dev);
  4340. pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS);
  4341. if (pts < 5)
  4342. return 0;
  4343. return pts - 5;
  4344. }
  4345. int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
  4346. {
  4347. struct device_domain_info *info;
  4348. struct context_entry *context;
  4349. struct dmar_domain *domain;
  4350. unsigned long flags;
  4351. u64 ctx_lo;
  4352. int ret;
  4353. domain = get_valid_domain_for_dev(sdev->dev);
  4354. if (!domain)
  4355. return -EINVAL;
  4356. spin_lock_irqsave(&device_domain_lock, flags);
  4357. spin_lock(&iommu->lock);
  4358. ret = -EINVAL;
  4359. info = sdev->dev->archdata.iommu;
  4360. if (!info || !info->pasid_supported)
  4361. goto out;
  4362. context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
  4363. if (WARN_ON(!context))
  4364. goto out;
  4365. ctx_lo = context[0].lo;
  4366. sdev->did = domain->iommu_did[iommu->seq_id];
  4367. sdev->sid = PCI_DEVID(info->bus, info->devfn);
  4368. if (!(ctx_lo & CONTEXT_PASIDE)) {
  4369. if (iommu->pasid_state_table)
  4370. context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
  4371. context[1].lo = (u64)virt_to_phys(info->pasid_table->table) |
  4372. intel_iommu_get_pts(sdev->dev);
  4373. wmb();
  4374. /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
  4375. * extended to permit requests-with-PASID if the PASIDE bit
  4376. * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
  4377. * however, the PASIDE bit is ignored and requests-with-PASID
  4378. * are unconditionally blocked. Which makes less sense.
  4379. * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
  4380. * "guest mode" translation types depending on whether ATS
  4381. * is available or not. Annoyingly, we can't use the new
  4382. * modes *unless* PASIDE is set. */
  4383. if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
  4384. ctx_lo &= ~CONTEXT_TT_MASK;
  4385. if (info->ats_supported)
  4386. ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
  4387. else
  4388. ctx_lo |= CONTEXT_TT_PT_PASID << 2;
  4389. }
  4390. ctx_lo |= CONTEXT_PASIDE;
  4391. if (iommu->pasid_state_table)
  4392. ctx_lo |= CONTEXT_DINVE;
  4393. if (info->pri_supported)
  4394. ctx_lo |= CONTEXT_PRS;
  4395. context[0].lo = ctx_lo;
  4396. wmb();
  4397. iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
  4398. DMA_CCMD_MASK_NOBIT,
  4399. DMA_CCMD_DEVICE_INVL);
  4400. }
  4401. /* Enable PASID support in the device, if it wasn't already */
  4402. if (!info->pasid_enabled)
  4403. iommu_enable_dev_iotlb(info);
  4404. if (info->ats_enabled) {
  4405. sdev->dev_iotlb = 1;
  4406. sdev->qdep = info->ats_qdep;
  4407. if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
  4408. sdev->qdep = 0;
  4409. }
  4410. ret = 0;
  4411. out:
  4412. spin_unlock(&iommu->lock);
  4413. spin_unlock_irqrestore(&device_domain_lock, flags);
  4414. return ret;
  4415. }
  4416. struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
  4417. {
  4418. struct intel_iommu *iommu;
  4419. u8 bus, devfn;
  4420. if (iommu_dummy(dev)) {
  4421. dev_warn(dev,
  4422. "No IOMMU translation for device; cannot enable SVM\n");
  4423. return NULL;
  4424. }
  4425. iommu = device_to_iommu(dev, &bus, &devfn);
  4426. if ((!iommu)) {
  4427. dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
  4428. return NULL;
  4429. }
  4430. return iommu;
  4431. }
  4432. #endif /* CONFIG_INTEL_IOMMU_SVM */
  4433. const struct iommu_ops intel_iommu_ops = {
  4434. .capable = intel_iommu_capable,
  4435. .domain_alloc = intel_iommu_domain_alloc,
  4436. .domain_free = intel_iommu_domain_free,
  4437. .attach_dev = intel_iommu_attach_device,
  4438. .detach_dev = intel_iommu_detach_device,
  4439. .map = intel_iommu_map,
  4440. .unmap = intel_iommu_unmap,
  4441. .iova_to_phys = intel_iommu_iova_to_phys,
  4442. .add_device = intel_iommu_add_device,
  4443. .remove_device = intel_iommu_remove_device,
  4444. .get_resv_regions = intel_iommu_get_resv_regions,
  4445. .put_resv_regions = intel_iommu_put_resv_regions,
  4446. .device_group = pci_device_group,
  4447. .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
  4448. };
  4449. static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
  4450. {
  4451. /* G4x/GM45 integrated gfx dmar support is totally busted. */
  4452. pr_info("Disabling IOMMU for graphics on this chipset\n");
  4453. dmar_map_gfx = 0;
  4454. }
  4455. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
  4456. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
  4457. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
  4458. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
  4459. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
  4460. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
  4461. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
  4462. static void quirk_iommu_rwbf(struct pci_dev *dev)
  4463. {
  4464. /*
  4465. * Mobile 4 Series Chipset neglects to set RWBF capability,
  4466. * but needs it. Same seems to hold for the desktop versions.
  4467. */
  4468. pr_info("Forcing write-buffer flush capability\n");
  4469. rwbf_quirk = 1;
  4470. }
  4471. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
  4472. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
  4473. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
  4474. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
  4475. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
  4476. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
  4477. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
  4478. #define GGC 0x52
  4479. #define GGC_MEMORY_SIZE_MASK (0xf << 8)
  4480. #define GGC_MEMORY_SIZE_NONE (0x0 << 8)
  4481. #define GGC_MEMORY_SIZE_1M (0x1 << 8)
  4482. #define GGC_MEMORY_SIZE_2M (0x3 << 8)
  4483. #define GGC_MEMORY_VT_ENABLED (0x8 << 8)
  4484. #define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
  4485. #define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
  4486. #define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
  4487. static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
  4488. {
  4489. unsigned short ggc;
  4490. if (pci_read_config_word(dev, GGC, &ggc))
  4491. return;
  4492. if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
  4493. pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
  4494. dmar_map_gfx = 0;
  4495. } else if (dmar_map_gfx) {
  4496. /* we have to ensure the gfx device is idle before we flush */
  4497. pr_info("Disabling batched IOTLB flush on Ironlake\n");
  4498. intel_iommu_strict = 1;
  4499. }
  4500. }
  4501. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
  4502. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
  4503. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
  4504. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
  4505. /* On Tylersburg chipsets, some BIOSes have been known to enable the
  4506. ISOCH DMAR unit for the Azalia sound device, but not give it any
  4507. TLB entries, which causes it to deadlock. Check for that. We do
  4508. this in a function called from init_dmars(), instead of in a PCI
  4509. quirk, because we don't want to print the obnoxious "BIOS broken"
  4510. message if VT-d is actually disabled.
  4511. */
  4512. static void __init check_tylersburg_isoch(void)
  4513. {
  4514. struct pci_dev *pdev;
  4515. uint32_t vtisochctrl;
  4516. /* If there's no Azalia in the system anyway, forget it. */
  4517. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
  4518. if (!pdev)
  4519. return;
  4520. pci_dev_put(pdev);
  4521. /* System Management Registers. Might be hidden, in which case
  4522. we can't do the sanity check. But that's OK, because the
  4523. known-broken BIOSes _don't_ actually hide it, so far. */
  4524. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
  4525. if (!pdev)
  4526. return;
  4527. if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
  4528. pci_dev_put(pdev);
  4529. return;
  4530. }
  4531. pci_dev_put(pdev);
  4532. /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
  4533. if (vtisochctrl & 1)
  4534. return;
  4535. /* Drop all bits other than the number of TLB entries */
  4536. vtisochctrl &= 0x1c;
  4537. /* If we have the recommended number of TLB entries (16), fine. */
  4538. if (vtisochctrl == 0x10)
  4539. return;
  4540. /* Zero TLB entries? You get to ride the short bus to school. */
  4541. if (!vtisochctrl) {
  4542. WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
  4543. "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
  4544. dmi_get_system_info(DMI_BIOS_VENDOR),
  4545. dmi_get_system_info(DMI_BIOS_VERSION),
  4546. dmi_get_system_info(DMI_PRODUCT_VERSION));
  4547. iommu_identity_mapping |= IDENTMAP_AZALIA;
  4548. return;
  4549. }
  4550. pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
  4551. vtisochctrl);
  4552. }