intel_iommu.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915
  1. /*
  2. * QEMU emulation of an Intel IOMMU (VT-d)
  3. * (DMA Remapping device)
  4. *
  5. * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com>
  6. * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/error-report.h"
  21. #include "qemu/main-loop.h"
  22. #include "qapi/error.h"
  23. #include "hw/sysbus.h"
  24. #include "intel_iommu_internal.h"
  25. #include "hw/pci/pci.h"
  26. #include "hw/pci/pci_bus.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/i386/pc.h"
  29. #include "hw/i386/apic-msidef.h"
  30. #include "hw/i386/x86-iommu.h"
  31. #include "hw/pci-host/q35.h"
  32. #include "system/kvm.h"
  33. #include "system/dma.h"
  34. #include "system/system.h"
  35. #include "hw/i386/apic_internal.h"
  36. #include "kvm/kvm_i386.h"
  37. #include "migration/vmstate.h"
  38. #include "trace.h"
  39. /* context entry operations */
  40. #define VTD_CE_GET_RID2PASID(ce) \
  41. ((ce)->val[1] & VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK)
  42. #define VTD_CE_GET_PASID_DIR_TABLE(ce) \
  43. ((ce)->val[0] & VTD_PASID_DIR_BASE_ADDR_MASK)
  44. /* pe operations */
  45. #define VTD_PE_GET_TYPE(pe) ((pe)->val[0] & VTD_SM_PASID_ENTRY_PGTT)
  46. #define VTD_PE_GET_FL_LEVEL(pe) \
  47. (4 + (((pe)->val[2] >> 2) & VTD_SM_PASID_ENTRY_FLPM))
  48. #define VTD_PE_GET_SL_LEVEL(pe) \
  49. (2 + (((pe)->val[0] >> 2) & VTD_SM_PASID_ENTRY_AW))
  50. /*
  51. * PCI bus number (or SID) is not reliable since the device is usaully
  52. * initialized before guest can configure the PCI bridge
  53. * (SECONDARY_BUS_NUMBER).
  54. */
  55. struct vtd_as_key {
  56. PCIBus *bus;
  57. uint8_t devfn;
  58. uint32_t pasid;
  59. };
  60. /* bus/devfn is PCI device's real BDF not the aliased one */
  61. struct vtd_hiod_key {
  62. PCIBus *bus;
  63. uint8_t devfn;
  64. };
  65. struct vtd_as_raw_key {
  66. uint16_t sid;
  67. uint32_t pasid;
  68. };
  69. struct vtd_iotlb_key {
  70. uint64_t gfn;
  71. uint32_t pasid;
  72. uint16_t sid;
  73. uint8_t level;
  74. };
  75. static void vtd_address_space_refresh_all(IntelIOMMUState *s);
  76. static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n);
  77. static void vtd_panic_require_caching_mode(void)
  78. {
  79. error_report("We need to set caching-mode=on for intel-iommu to enable "
  80. "device assignment with IOMMU protection.");
  81. exit(1);
  82. }
  83. static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
  84. uint64_t wmask, uint64_t w1cmask)
  85. {
  86. stq_le_p(&s->csr[addr], val);
  87. stq_le_p(&s->wmask[addr], wmask);
  88. stq_le_p(&s->w1cmask[addr], w1cmask);
  89. }
  90. static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
  91. {
  92. stq_le_p(&s->womask[addr], mask);
  93. }
  94. static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
  95. uint32_t wmask, uint32_t w1cmask)
  96. {
  97. stl_le_p(&s->csr[addr], val);
  98. stl_le_p(&s->wmask[addr], wmask);
  99. stl_le_p(&s->w1cmask[addr], w1cmask);
  100. }
  101. static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
  102. {
  103. stl_le_p(&s->womask[addr], mask);
  104. }
  105. /* "External" get/set operations */
  106. static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
  107. {
  108. uint64_t oldval = ldq_le_p(&s->csr[addr]);
  109. uint64_t wmask = ldq_le_p(&s->wmask[addr]);
  110. uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
  111. stq_le_p(&s->csr[addr],
  112. ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
  113. }
  114. static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
  115. {
  116. uint32_t oldval = ldl_le_p(&s->csr[addr]);
  117. uint32_t wmask = ldl_le_p(&s->wmask[addr]);
  118. uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
  119. stl_le_p(&s->csr[addr],
  120. ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
  121. }
  122. static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
  123. {
  124. uint64_t val = ldq_le_p(&s->csr[addr]);
  125. uint64_t womask = ldq_le_p(&s->womask[addr]);
  126. return val & ~womask;
  127. }
  128. static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
  129. {
  130. uint32_t val = ldl_le_p(&s->csr[addr]);
  131. uint32_t womask = ldl_le_p(&s->womask[addr]);
  132. return val & ~womask;
  133. }
  134. /* "Internal" get/set operations */
  135. static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
  136. {
  137. return ldq_le_p(&s->csr[addr]);
  138. }
  139. static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
  140. {
  141. return ldl_le_p(&s->csr[addr]);
  142. }
  143. static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
  144. {
  145. stq_le_p(&s->csr[addr], val);
  146. }
  147. static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
  148. uint32_t clear, uint32_t mask)
  149. {
  150. uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
  151. stl_le_p(&s->csr[addr], new_val);
  152. return new_val;
  153. }
  154. static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
  155. uint64_t clear, uint64_t mask)
  156. {
  157. uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
  158. stq_le_p(&s->csr[addr], new_val);
  159. return new_val;
  160. }
  161. static inline void vtd_iommu_lock(IntelIOMMUState *s)
  162. {
  163. qemu_mutex_lock(&s->iommu_lock);
  164. }
  165. static inline void vtd_iommu_unlock(IntelIOMMUState *s)
  166. {
  167. qemu_mutex_unlock(&s->iommu_lock);
  168. }
  169. static void vtd_update_scalable_state(IntelIOMMUState *s)
  170. {
  171. uint64_t val = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
  172. if (s->scalable_mode) {
  173. s->root_scalable = val & VTD_RTADDR_SMT;
  174. }
  175. }
  176. static void vtd_update_iq_dw(IntelIOMMUState *s)
  177. {
  178. uint64_t val = vtd_get_quad_raw(s, DMAR_IQA_REG);
  179. if (s->ecap & VTD_ECAP_SMTS &&
  180. val & VTD_IQA_DW_MASK) {
  181. s->iq_dw = true;
  182. } else {
  183. s->iq_dw = false;
  184. }
  185. }
  186. /* Whether the address space needs to notify new mappings */
  187. static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
  188. {
  189. return as->notifier_flags & IOMMU_NOTIFIER_MAP;
  190. }
  191. /* GHashTable functions */
  192. static gboolean vtd_iotlb_equal(gconstpointer v1, gconstpointer v2)
  193. {
  194. const struct vtd_iotlb_key *key1 = v1;
  195. const struct vtd_iotlb_key *key2 = v2;
  196. return key1->sid == key2->sid &&
  197. key1->pasid == key2->pasid &&
  198. key1->level == key2->level &&
  199. key1->gfn == key2->gfn;
  200. }
  201. static guint vtd_iotlb_hash(gconstpointer v)
  202. {
  203. const struct vtd_iotlb_key *key = v;
  204. uint64_t hash64 = key->gfn | ((uint64_t)(key->sid) << VTD_IOTLB_SID_SHIFT) |
  205. (uint64_t)(key->level - 1) << VTD_IOTLB_LVL_SHIFT |
  206. (uint64_t)(key->pasid) << VTD_IOTLB_PASID_SHIFT;
  207. return (guint)((hash64 >> 32) ^ (hash64 & 0xffffffffU));
  208. }
  209. static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2)
  210. {
  211. const struct vtd_as_key *key1 = v1;
  212. const struct vtd_as_key *key2 = v2;
  213. return (key1->bus == key2->bus) && (key1->devfn == key2->devfn) &&
  214. (key1->pasid == key2->pasid);
  215. }
  216. /*
  217. * Note that we use pointer to PCIBus as the key, so hashing/shifting
  218. * based on the pointer value is intended. Note that we deal with
  219. * collisions through vtd_as_equal().
  220. */
  221. static guint vtd_as_hash(gconstpointer v)
  222. {
  223. const struct vtd_as_key *key = v;
  224. guint value = (guint)(uintptr_t)key->bus;
  225. return (guint)(value << 8 | key->devfn);
  226. }
  227. /* Same implementation as vtd_as_hash() */
  228. static guint vtd_hiod_hash(gconstpointer v)
  229. {
  230. return vtd_as_hash(v);
  231. }
  232. static gboolean vtd_hiod_equal(gconstpointer v1, gconstpointer v2)
  233. {
  234. const struct vtd_hiod_key *key1 = v1;
  235. const struct vtd_hiod_key *key2 = v2;
  236. return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
  237. }
  238. static void vtd_hiod_destroy(gpointer v)
  239. {
  240. object_unref(v);
  241. }
  242. static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
  243. gpointer user_data)
  244. {
  245. VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
  246. uint16_t domain_id = *(uint16_t *)user_data;
  247. return entry->domain_id == domain_id;
  248. }
  249. /* The shift of an addr for a certain level of paging structure */
  250. static inline uint32_t vtd_pt_level_shift(uint32_t level)
  251. {
  252. assert(level != 0);
  253. return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_LEVEL_BITS;
  254. }
  255. static inline uint64_t vtd_pt_level_page_mask(uint32_t level)
  256. {
  257. return ~((1ULL << vtd_pt_level_shift(level)) - 1);
  258. }
  259. static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
  260. gpointer user_data)
  261. {
  262. VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
  263. VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
  264. uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
  265. uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
  266. if (entry->domain_id != info->domain_id) {
  267. return false;
  268. }
  269. /*
  270. * According to spec, IOTLB entries caching first-stage (PGTT=001b) or
  271. * nested (PGTT=011b) mapping associated with specified domain-id are
  272. * invalidated. Nested isn't supported yet, so only need to check 001b.
  273. */
  274. if (entry->pgtt == VTD_SM_PASID_ENTRY_FLT) {
  275. return true;
  276. }
  277. return (entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb;
  278. }
  279. static gboolean vtd_hash_remove_by_page_piotlb(gpointer key, gpointer value,
  280. gpointer user_data)
  281. {
  282. VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
  283. VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
  284. uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
  285. uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
  286. /*
  287. * According to spec, PASID-based-IOTLB Invalidation in page granularity
  288. * doesn't invalidate IOTLB entries caching second-stage (PGTT=010b)
  289. * or pass-through (PGTT=100b) mappings. Nested isn't supported yet,
  290. * so only need to check first-stage (PGTT=001b) mappings.
  291. */
  292. if (entry->pgtt != VTD_SM_PASID_ENTRY_FLT) {
  293. return false;
  294. }
  295. return entry->domain_id == info->domain_id && entry->pasid == info->pasid &&
  296. ((entry->gfn & info->mask) == gfn || entry->gfn == gfn_tlb);
  297. }
  298. /* Reset all the gen of VTDAddressSpace to zero and set the gen of
  299. * IntelIOMMUState to 1. Must be called with IOMMU lock held.
  300. */
  301. static void vtd_reset_context_cache_locked(IntelIOMMUState *s)
  302. {
  303. VTDAddressSpace *vtd_as;
  304. GHashTableIter as_it;
  305. trace_vtd_context_cache_reset();
  306. g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
  307. while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
  308. vtd_as->context_cache_entry.context_cache_gen = 0;
  309. }
  310. s->context_cache_gen = 1;
  311. }
  312. /* Must be called with IOMMU lock held. */
  313. static void vtd_reset_iotlb_locked(IntelIOMMUState *s)
  314. {
  315. assert(s->iotlb);
  316. g_hash_table_remove_all(s->iotlb);
  317. }
  318. static void vtd_reset_iotlb(IntelIOMMUState *s)
  319. {
  320. vtd_iommu_lock(s);
  321. vtd_reset_iotlb_locked(s);
  322. vtd_iommu_unlock(s);
  323. }
  324. static void vtd_reset_caches(IntelIOMMUState *s)
  325. {
  326. vtd_iommu_lock(s);
  327. vtd_reset_iotlb_locked(s);
  328. vtd_reset_context_cache_locked(s);
  329. vtd_iommu_unlock(s);
  330. }
  331. static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
  332. {
  333. return (addr & vtd_pt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
  334. }
  335. /* Must be called with IOMMU lock held */
  336. static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
  337. uint32_t pasid, hwaddr addr)
  338. {
  339. struct vtd_iotlb_key key;
  340. VTDIOTLBEntry *entry;
  341. unsigned level;
  342. for (level = VTD_PT_LEVEL; level < VTD_PML4_LEVEL; level++) {
  343. key.gfn = vtd_get_iotlb_gfn(addr, level);
  344. key.level = level;
  345. key.sid = source_id;
  346. key.pasid = pasid;
  347. entry = g_hash_table_lookup(s->iotlb, &key);
  348. if (entry) {
  349. goto out;
  350. }
  351. }
  352. out:
  353. return entry;
  354. }
  355. /* Must be with IOMMU lock held */
  356. static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
  357. uint16_t domain_id, hwaddr addr, uint64_t pte,
  358. uint8_t access_flags, uint32_t level,
  359. uint32_t pasid, uint8_t pgtt)
  360. {
  361. VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
  362. struct vtd_iotlb_key *key = g_malloc(sizeof(*key));
  363. uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
  364. trace_vtd_iotlb_page_update(source_id, addr, pte, domain_id);
  365. if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
  366. trace_vtd_iotlb_reset("iotlb exceeds size limit");
  367. vtd_reset_iotlb_locked(s);
  368. }
  369. entry->gfn = gfn;
  370. entry->domain_id = domain_id;
  371. entry->pte = pte;
  372. entry->access_flags = access_flags;
  373. entry->mask = vtd_pt_level_page_mask(level);
  374. entry->pasid = pasid;
  375. entry->pgtt = pgtt;
  376. key->gfn = gfn;
  377. key->sid = source_id;
  378. key->level = level;
  379. key->pasid = pasid;
  380. g_hash_table_replace(s->iotlb, key, entry);
  381. }
  382. /* Given the reg addr of both the message data and address, generate an
  383. * interrupt via MSI.
  384. */
  385. static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
  386. hwaddr mesg_data_reg)
  387. {
  388. MSIMessage msi;
  389. assert(mesg_data_reg < DMAR_REG_SIZE);
  390. assert(mesg_addr_reg < DMAR_REG_SIZE);
  391. msi.address = vtd_get_long_raw(s, mesg_addr_reg);
  392. msi.data = vtd_get_long_raw(s, mesg_data_reg);
  393. trace_vtd_irq_generate(msi.address, msi.data);
  394. apic_get_class(NULL)->send_msi(&msi);
  395. }
  396. /* Generate a fault event to software via MSI if conditions are met.
  397. * Notice that the value of FSTS_REG being passed to it should be the one
  398. * before any update.
  399. */
  400. static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
  401. {
  402. if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
  403. pre_fsts & VTD_FSTS_IQE) {
  404. error_report_once("There are previous interrupt conditions "
  405. "to be serviced by software, fault event "
  406. "is not generated");
  407. return;
  408. }
  409. vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
  410. if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
  411. error_report_once("Interrupt Mask set, irq is not generated");
  412. } else {
  413. vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
  414. vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
  415. }
  416. }
  417. /* Check if the Fault (F) field of the Fault Recording Register referenced by
  418. * @index is Set.
  419. */
  420. static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
  421. {
  422. /* Each reg is 128-bit */
  423. hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
  424. addr += 8; /* Access the high 64-bit half */
  425. assert(index < DMAR_FRCD_REG_NR);
  426. return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
  427. }
  428. /* Update the PPF field of Fault Status Register.
  429. * Should be called whenever change the F field of any fault recording
  430. * registers.
  431. */
  432. static void vtd_update_fsts_ppf(IntelIOMMUState *s)
  433. {
  434. uint32_t i;
  435. uint32_t ppf_mask = 0;
  436. for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
  437. if (vtd_is_frcd_set(s, i)) {
  438. ppf_mask = VTD_FSTS_PPF;
  439. break;
  440. }
  441. }
  442. vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
  443. trace_vtd_fsts_ppf(!!ppf_mask);
  444. }
  445. static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
  446. {
  447. /* Each reg is 128-bit */
  448. hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
  449. addr += 8; /* Access the high 64-bit half */
  450. assert(index < DMAR_FRCD_REG_NR);
  451. vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
  452. vtd_update_fsts_ppf(s);
  453. }
  454. /* Must not update F field now, should be done later */
  455. static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
  456. uint64_t hi, uint64_t lo)
  457. {
  458. hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
  459. assert(index < DMAR_FRCD_REG_NR);
  460. vtd_set_quad_raw(s, frcd_reg_addr, lo);
  461. vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
  462. trace_vtd_frr_new(index, hi, lo);
  463. }
  464. /* Try to collapse multiple pending faults from the same requester */
  465. static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
  466. {
  467. uint32_t i;
  468. uint64_t frcd_reg;
  469. hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
  470. for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
  471. frcd_reg = vtd_get_quad_raw(s, addr);
  472. if ((frcd_reg & VTD_FRCD_F) &&
  473. ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
  474. return true;
  475. }
  476. addr += 16; /* 128-bit for each */
  477. }
  478. return false;
  479. }
  480. /* Log and report an DMAR (address translation) fault to software */
  481. static void vtd_report_frcd_fault(IntelIOMMUState *s, uint64_t source_id,
  482. uint64_t hi, uint64_t lo)
  483. {
  484. uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
  485. if (fsts_reg & VTD_FSTS_PFO) {
  486. error_report_once("New fault is not recorded due to "
  487. "Primary Fault Overflow");
  488. return;
  489. }
  490. if (vtd_try_collapse_fault(s, source_id)) {
  491. error_report_once("New fault is not recorded due to "
  492. "compression of faults");
  493. return;
  494. }
  495. if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
  496. error_report_once("Next Fault Recording Reg is used, "
  497. "new fault is not recorded, set PFO field");
  498. vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
  499. return;
  500. }
  501. vtd_record_frcd(s, s->next_frcd_reg, hi, lo);
  502. if (fsts_reg & VTD_FSTS_PPF) {
  503. error_report_once("There are pending faults already, "
  504. "fault event is not generated");
  505. vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
  506. s->next_frcd_reg++;
  507. if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
  508. s->next_frcd_reg = 0;
  509. }
  510. } else {
  511. vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
  512. VTD_FSTS_FRI(s->next_frcd_reg));
  513. vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
  514. s->next_frcd_reg++;
  515. if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
  516. s->next_frcd_reg = 0;
  517. }
  518. /* This case actually cause the PPF to be Set.
  519. * So generate fault event (interrupt).
  520. */
  521. vtd_generate_fault_event(s, fsts_reg);
  522. }
  523. }
  524. /* Log and report an DMAR (address translation) fault to software */
  525. static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
  526. hwaddr addr, VTDFaultReason fault,
  527. bool is_write, bool is_pasid,
  528. uint32_t pasid)
  529. {
  530. uint64_t hi, lo;
  531. assert(fault < VTD_FR_MAX);
  532. trace_vtd_dmar_fault(source_id, fault, addr, is_write);
  533. lo = VTD_FRCD_FI(addr);
  534. hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault) |
  535. VTD_FRCD_PV(pasid) | VTD_FRCD_PP(is_pasid);
  536. if (!is_write) {
  537. hi |= VTD_FRCD_T;
  538. }
  539. vtd_report_frcd_fault(s, source_id, hi, lo);
  540. }
  541. static void vtd_report_ir_fault(IntelIOMMUState *s, uint64_t source_id,
  542. VTDFaultReason fault, uint16_t index)
  543. {
  544. uint64_t hi, lo;
  545. lo = VTD_FRCD_IR_IDX(index);
  546. hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
  547. vtd_report_frcd_fault(s, source_id, hi, lo);
  548. }
  549. /* Handle Invalidation Queue Errors of queued invalidation interface error
  550. * conditions.
  551. */
  552. static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
  553. {
  554. uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
  555. vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
  556. vtd_generate_fault_event(s, fsts_reg);
  557. }
  558. /* Set the IWC field and try to generate an invalidation completion interrupt */
  559. static void vtd_generate_completion_event(IntelIOMMUState *s)
  560. {
  561. if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
  562. trace_vtd_inv_desc_wait_irq("One pending, skip current");
  563. return;
  564. }
  565. vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
  566. vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
  567. if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
  568. trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
  569. "new event not generated");
  570. return;
  571. } else {
  572. /* Generate the interrupt event */
  573. trace_vtd_inv_desc_wait_irq("Generating complete event");
  574. vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
  575. vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
  576. }
  577. }
  578. static inline bool vtd_root_entry_present(IntelIOMMUState *s,
  579. VTDRootEntry *re,
  580. uint8_t devfn)
  581. {
  582. if (s->root_scalable && devfn > UINT8_MAX / 2) {
  583. return re->hi & VTD_ROOT_ENTRY_P;
  584. }
  585. return re->lo & VTD_ROOT_ENTRY_P;
  586. }
  587. static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
  588. VTDRootEntry *re)
  589. {
  590. dma_addr_t addr;
  591. addr = s->root + index * sizeof(*re);
  592. if (dma_memory_read(&address_space_memory, addr,
  593. re, sizeof(*re), MEMTXATTRS_UNSPECIFIED)) {
  594. re->lo = 0;
  595. return -VTD_FR_ROOT_TABLE_INV;
  596. }
  597. re->lo = le64_to_cpu(re->lo);
  598. re->hi = le64_to_cpu(re->hi);
  599. return 0;
  600. }
  601. static inline bool vtd_ce_present(VTDContextEntry *context)
  602. {
  603. return context->lo & VTD_CONTEXT_ENTRY_P;
  604. }
  605. static int vtd_get_context_entry_from_root(IntelIOMMUState *s,
  606. VTDRootEntry *re,
  607. uint8_t index,
  608. VTDContextEntry *ce)
  609. {
  610. dma_addr_t addr, ce_size;
  611. /* we have checked that root entry is present */
  612. ce_size = s->root_scalable ? VTD_CTX_ENTRY_SCALABLE_SIZE :
  613. VTD_CTX_ENTRY_LEGACY_SIZE;
  614. if (s->root_scalable && index > UINT8_MAX / 2) {
  615. index = index & (~VTD_DEVFN_CHECK_MASK);
  616. addr = re->hi & VTD_ROOT_ENTRY_CTP;
  617. } else {
  618. addr = re->lo & VTD_ROOT_ENTRY_CTP;
  619. }
  620. addr = addr + index * ce_size;
  621. if (dma_memory_read(&address_space_memory, addr,
  622. ce, ce_size, MEMTXATTRS_UNSPECIFIED)) {
  623. return -VTD_FR_CONTEXT_TABLE_INV;
  624. }
  625. ce->lo = le64_to_cpu(ce->lo);
  626. ce->hi = le64_to_cpu(ce->hi);
  627. if (ce_size == VTD_CTX_ENTRY_SCALABLE_SIZE) {
  628. ce->val[2] = le64_to_cpu(ce->val[2]);
  629. ce->val[3] = le64_to_cpu(ce->val[3]);
  630. }
  631. return 0;
  632. }
  633. static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
  634. {
  635. return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
  636. }
  637. static inline uint64_t vtd_get_pte_addr(uint64_t pte, uint8_t aw)
  638. {
  639. return pte & VTD_PT_BASE_ADDR_MASK(aw);
  640. }
  641. /* Whether the pte indicates the address of the page frame */
  642. static inline bool vtd_is_last_pte(uint64_t pte, uint32_t level)
  643. {
  644. return level == VTD_PT_LEVEL || (pte & VTD_PT_PAGE_SIZE_MASK);
  645. }
  646. /* Get the content of a pte located in @base_addr[@index] */
  647. static uint64_t vtd_get_pte(dma_addr_t base_addr, uint32_t index)
  648. {
  649. uint64_t pte;
  650. assert(index < VTD_PT_ENTRY_NR);
  651. if (dma_memory_read(&address_space_memory,
  652. base_addr + index * sizeof(pte),
  653. &pte, sizeof(pte), MEMTXATTRS_UNSPECIFIED)) {
  654. pte = (uint64_t)-1;
  655. return pte;
  656. }
  657. pte = le64_to_cpu(pte);
  658. return pte;
  659. }
  660. /* Given an iova and the level of paging structure, return the offset
  661. * of current level.
  662. */
  663. static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
  664. {
  665. return (iova >> vtd_pt_level_shift(level)) &
  666. ((1ULL << VTD_LEVEL_BITS) - 1);
  667. }
  668. /* Check Capability Register to see if the @level of page-table is supported */
  669. static inline bool vtd_is_sl_level_supported(IntelIOMMUState *s, uint32_t level)
  670. {
  671. return VTD_CAP_SAGAW_MASK & s->cap &
  672. (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
  673. }
  674. static inline bool vtd_is_fl_level_supported(IntelIOMMUState *s, uint32_t level)
  675. {
  676. return level == VTD_PML4_LEVEL;
  677. }
  678. /* Return true if check passed, otherwise false */
  679. static inline bool vtd_pe_type_check(IntelIOMMUState *s, VTDPASIDEntry *pe)
  680. {
  681. switch (VTD_PE_GET_TYPE(pe)) {
  682. case VTD_SM_PASID_ENTRY_FLT:
  683. return !!(s->ecap & VTD_ECAP_FLTS);
  684. case VTD_SM_PASID_ENTRY_SLT:
  685. return !!(s->ecap & VTD_ECAP_SLTS);
  686. case VTD_SM_PASID_ENTRY_NESTED:
  687. /* Not support NESTED page table type yet */
  688. return false;
  689. case VTD_SM_PASID_ENTRY_PT:
  690. return !!(s->ecap & VTD_ECAP_PT);
  691. default:
  692. /* Unknown type */
  693. return false;
  694. }
  695. }
  696. static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire)
  697. {
  698. return pdire->val & 1;
  699. }
  700. /**
  701. * Caller of this function should check present bit if wants
  702. * to use pdir entry for further usage except for fpd bit check.
  703. */
  704. static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base,
  705. uint32_t pasid,
  706. VTDPASIDDirEntry *pdire)
  707. {
  708. uint32_t index;
  709. dma_addr_t addr, entry_size;
  710. index = VTD_PASID_DIR_INDEX(pasid);
  711. entry_size = VTD_PASID_DIR_ENTRY_SIZE;
  712. addr = pasid_dir_base + index * entry_size;
  713. if (dma_memory_read(&address_space_memory, addr,
  714. pdire, entry_size, MEMTXATTRS_UNSPECIFIED)) {
  715. return -VTD_FR_PASID_DIR_ACCESS_ERR;
  716. }
  717. pdire->val = le64_to_cpu(pdire->val);
  718. return 0;
  719. }
  720. static inline bool vtd_pe_present(VTDPASIDEntry *pe)
  721. {
  722. return pe->val[0] & VTD_PASID_ENTRY_P;
  723. }
  724. static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s,
  725. uint32_t pasid,
  726. dma_addr_t addr,
  727. VTDPASIDEntry *pe)
  728. {
  729. uint8_t pgtt;
  730. uint32_t index;
  731. dma_addr_t entry_size;
  732. index = VTD_PASID_TABLE_INDEX(pasid);
  733. entry_size = VTD_PASID_ENTRY_SIZE;
  734. addr = addr + index * entry_size;
  735. if (dma_memory_read(&address_space_memory, addr,
  736. pe, entry_size, MEMTXATTRS_UNSPECIFIED)) {
  737. return -VTD_FR_PASID_TABLE_ACCESS_ERR;
  738. }
  739. for (size_t i = 0; i < ARRAY_SIZE(pe->val); i++) {
  740. pe->val[i] = le64_to_cpu(pe->val[i]);
  741. }
  742. /* Do translation type check */
  743. if (!vtd_pe_type_check(s, pe)) {
  744. return -VTD_FR_PASID_TABLE_ENTRY_INV;
  745. }
  746. pgtt = VTD_PE_GET_TYPE(pe);
  747. if (pgtt == VTD_SM_PASID_ENTRY_SLT &&
  748. !vtd_is_sl_level_supported(s, VTD_PE_GET_SL_LEVEL(pe))) {
  749. return -VTD_FR_PASID_TABLE_ENTRY_INV;
  750. }
  751. if (pgtt == VTD_SM_PASID_ENTRY_FLT &&
  752. !vtd_is_fl_level_supported(s, VTD_PE_GET_FL_LEVEL(pe))) {
  753. return -VTD_FR_PASID_TABLE_ENTRY_INV;
  754. }
  755. return 0;
  756. }
  757. /**
  758. * Caller of this function should check present bit if wants
  759. * to use pasid entry for further usage except for fpd bit check.
  760. */
  761. static int vtd_get_pe_from_pdire(IntelIOMMUState *s,
  762. uint32_t pasid,
  763. VTDPASIDDirEntry *pdire,
  764. VTDPASIDEntry *pe)
  765. {
  766. dma_addr_t addr = pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK;
  767. return vtd_get_pe_in_pasid_leaf_table(s, pasid, addr, pe);
  768. }
  769. /**
  770. * This function gets a pasid entry from a specified pasid
  771. * table (includes dir and leaf table) with a specified pasid.
  772. * Sanity check should be done to ensure return a present
  773. * pasid entry to caller.
  774. */
  775. static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s,
  776. dma_addr_t pasid_dir_base,
  777. uint32_t pasid,
  778. VTDPASIDEntry *pe)
  779. {
  780. int ret;
  781. VTDPASIDDirEntry pdire;
  782. ret = vtd_get_pdire_from_pdir_table(pasid_dir_base,
  783. pasid, &pdire);
  784. if (ret) {
  785. return ret;
  786. }
  787. if (!vtd_pdire_present(&pdire)) {
  788. return -VTD_FR_PASID_DIR_ENTRY_P;
  789. }
  790. ret = vtd_get_pe_from_pdire(s, pasid, &pdire, pe);
  791. if (ret) {
  792. return ret;
  793. }
  794. if (!vtd_pe_present(pe)) {
  795. return -VTD_FR_PASID_ENTRY_P;
  796. }
  797. return 0;
  798. }
  799. static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s,
  800. VTDContextEntry *ce,
  801. VTDPASIDEntry *pe,
  802. uint32_t pasid)
  803. {
  804. dma_addr_t pasid_dir_base;
  805. int ret = 0;
  806. if (pasid == PCI_NO_PASID) {
  807. pasid = VTD_CE_GET_RID2PASID(ce);
  808. }
  809. pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
  810. ret = vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe);
  811. return ret;
  812. }
  813. static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s,
  814. VTDContextEntry *ce,
  815. bool *pe_fpd_set,
  816. uint32_t pasid)
  817. {
  818. int ret;
  819. dma_addr_t pasid_dir_base;
  820. VTDPASIDDirEntry pdire;
  821. VTDPASIDEntry pe;
  822. if (pasid == PCI_NO_PASID) {
  823. pasid = VTD_CE_GET_RID2PASID(ce);
  824. }
  825. pasid_dir_base = VTD_CE_GET_PASID_DIR_TABLE(ce);
  826. /*
  827. * No present bit check since fpd is meaningful even
  828. * if the present bit is clear.
  829. */
  830. ret = vtd_get_pdire_from_pdir_table(pasid_dir_base, pasid, &pdire);
  831. if (ret) {
  832. return ret;
  833. }
  834. if (pdire.val & VTD_PASID_DIR_FPD) {
  835. *pe_fpd_set = true;
  836. return 0;
  837. }
  838. if (!vtd_pdire_present(&pdire)) {
  839. return -VTD_FR_PASID_DIR_ENTRY_P;
  840. }
  841. /*
  842. * No present bit check since fpd is meaningful even
  843. * if the present bit is clear.
  844. */
  845. ret = vtd_get_pe_from_pdire(s, pasid, &pdire, &pe);
  846. if (ret) {
  847. return ret;
  848. }
  849. if (pe.val[0] & VTD_PASID_ENTRY_FPD) {
  850. *pe_fpd_set = true;
  851. }
  852. return 0;
  853. }
  854. /* Get the page-table level that hardware should use for the second-level
  855. * page-table walk from the Address Width field of context-entry.
  856. */
  857. static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
  858. {
  859. return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
  860. }
  861. static uint32_t vtd_get_iova_level(IntelIOMMUState *s,
  862. VTDContextEntry *ce,
  863. uint32_t pasid)
  864. {
  865. VTDPASIDEntry pe;
  866. if (s->root_scalable) {
  867. vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
  868. if (s->flts) {
  869. return VTD_PE_GET_FL_LEVEL(&pe);
  870. } else {
  871. return VTD_PE_GET_SL_LEVEL(&pe);
  872. }
  873. }
  874. return vtd_ce_get_level(ce);
  875. }
  876. static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
  877. {
  878. return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
  879. }
  880. static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s,
  881. VTDContextEntry *ce,
  882. uint32_t pasid)
  883. {
  884. VTDPASIDEntry pe;
  885. if (s->root_scalable) {
  886. vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
  887. return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9;
  888. }
  889. return vtd_ce_get_agaw(ce);
  890. }
  891. static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
  892. {
  893. return ce->lo & VTD_CONTEXT_ENTRY_TT;
  894. }
  895. /* Only for Legacy Mode. Return true if check passed, otherwise false */
  896. static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
  897. VTDContextEntry *ce)
  898. {
  899. switch (vtd_ce_get_type(ce)) {
  900. case VTD_CONTEXT_TT_MULTI_LEVEL:
  901. /* Always supported */
  902. break;
  903. case VTD_CONTEXT_TT_DEV_IOTLB:
  904. if (!x86_iommu->dt_supported) {
  905. error_report_once("%s: DT specified but not supported", __func__);
  906. return false;
  907. }
  908. break;
  909. case VTD_CONTEXT_TT_PASS_THROUGH:
  910. if (!x86_iommu->pt_supported) {
  911. error_report_once("%s: PT specified but not supported", __func__);
  912. return false;
  913. }
  914. break;
  915. default:
  916. /* Unknown type */
  917. error_report_once("%s: unknown ce type: %"PRIu32, __func__,
  918. vtd_ce_get_type(ce));
  919. return false;
  920. }
  921. return true;
  922. }
  923. static inline uint64_t vtd_iova_limit(IntelIOMMUState *s,
  924. VTDContextEntry *ce, uint8_t aw,
  925. uint32_t pasid)
  926. {
  927. uint32_t ce_agaw = vtd_get_iova_agaw(s, ce, pasid);
  928. return 1ULL << MIN(ce_agaw, aw);
  929. }
  930. /* Return true if IOVA passes range check, otherwise false. */
  931. static inline bool vtd_iova_sl_range_check(IntelIOMMUState *s,
  932. uint64_t iova, VTDContextEntry *ce,
  933. uint8_t aw, uint32_t pasid)
  934. {
  935. /*
  936. * Check if @iova is above 2^X-1, where X is the minimum of MGAW
  937. * in CAP_REG and AW in context-entry.
  938. */
  939. return !(iova & ~(vtd_iova_limit(s, ce, aw, pasid) - 1));
  940. }
  941. static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s,
  942. VTDContextEntry *ce,
  943. uint32_t pasid)
  944. {
  945. VTDPASIDEntry pe;
  946. if (s->root_scalable) {
  947. vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
  948. if (s->flts) {
  949. return pe.val[2] & VTD_SM_PASID_ENTRY_FLPTPTR;
  950. } else {
  951. return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR;
  952. }
  953. }
  954. return vtd_ce_get_slpt_base(ce);
  955. }
  956. /*
  957. * Rsvd field masks for spte:
  958. * vtd_spte_rsvd 4k pages
  959. * vtd_spte_rsvd_large large pages
  960. *
  961. * We support only 3-level and 4-level page tables (see vtd_init() which
  962. * sets only VTD_CAP_SAGAW_39bit and maybe VTD_CAP_SAGAW_48bit bits in s->cap).
  963. */
  964. #define VTD_SPTE_RSVD_LEN 5
  965. static uint64_t vtd_spte_rsvd[VTD_SPTE_RSVD_LEN];
  966. static uint64_t vtd_spte_rsvd_large[VTD_SPTE_RSVD_LEN];
  967. static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
  968. {
  969. uint64_t rsvd_mask;
  970. /*
  971. * We should have caught a guest-mis-programmed level earlier,
  972. * via vtd_is_sl_level_supported.
  973. */
  974. assert(level < VTD_SPTE_RSVD_LEN);
  975. /*
  976. * Zero level doesn't exist. The smallest level is VTD_PT_LEVEL=1 and
  977. * checked by vtd_is_last_pte().
  978. */
  979. assert(level);
  980. if ((level == VTD_PD_LEVEL || level == VTD_PDP_LEVEL) &&
  981. (slpte & VTD_PT_PAGE_SIZE_MASK)) {
  982. /* large page */
  983. rsvd_mask = vtd_spte_rsvd_large[level];
  984. } else {
  985. rsvd_mask = vtd_spte_rsvd[level];
  986. }
  987. return slpte & rsvd_mask;
  988. }
  989. /* Given the @iova, get relevant @slptep. @slpte_level will be the last level
  990. * of the translation, can be used for deciding the size of large page.
  991. */
  992. static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce,
  993. uint64_t iova, bool is_write,
  994. uint64_t *slptep, uint32_t *slpte_level,
  995. bool *reads, bool *writes, uint8_t aw_bits,
  996. uint32_t pasid)
  997. {
  998. dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
  999. uint32_t level = vtd_get_iova_level(s, ce, pasid);
  1000. uint32_t offset;
  1001. uint64_t slpte;
  1002. uint64_t access_right_check;
  1003. if (!vtd_iova_sl_range_check(s, iova, ce, aw_bits, pasid)) {
  1004. error_report_once("%s: detected IOVA overflow (iova=0x%" PRIx64 ","
  1005. "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
  1006. return -VTD_FR_ADDR_BEYOND_MGAW;
  1007. }
  1008. /* FIXME: what is the Atomics request here? */
  1009. access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
  1010. while (true) {
  1011. offset = vtd_iova_level_offset(iova, level);
  1012. slpte = vtd_get_pte(addr, offset);
  1013. if (slpte == (uint64_t)-1) {
  1014. error_report_once("%s: detected read error on DMAR slpte "
  1015. "(iova=0x%" PRIx64 ", pasid=0x%" PRIx32 ")",
  1016. __func__, iova, pasid);
  1017. if (level == vtd_get_iova_level(s, ce, pasid)) {
  1018. /* Invalid programming of context-entry */
  1019. return -VTD_FR_CONTEXT_ENTRY_INV;
  1020. } else {
  1021. return -VTD_FR_PAGING_ENTRY_INV;
  1022. }
  1023. }
  1024. *reads = (*reads) && (slpte & VTD_SL_R);
  1025. *writes = (*writes) && (slpte & VTD_SL_W);
  1026. if (!(slpte & access_right_check)) {
  1027. error_report_once("%s: detected slpte permission error "
  1028. "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
  1029. "slpte=0x%" PRIx64 ", write=%d, pasid=0x%"
  1030. PRIx32 ")", __func__, iova, level,
  1031. slpte, is_write, pasid);
  1032. return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
  1033. }
  1034. if (vtd_slpte_nonzero_rsvd(slpte, level)) {
  1035. error_report_once("%s: detected splte reserve non-zero "
  1036. "iova=0x%" PRIx64 ", level=0x%" PRIx32
  1037. "slpte=0x%" PRIx64 ", pasid=0x%" PRIX32 ")",
  1038. __func__, iova, level, slpte, pasid);
  1039. return -VTD_FR_PAGING_ENTRY_RSVD;
  1040. }
  1041. if (vtd_is_last_pte(slpte, level)) {
  1042. *slptep = slpte;
  1043. *slpte_level = level;
  1044. break;
  1045. }
  1046. addr = vtd_get_pte_addr(slpte, aw_bits);
  1047. level--;
  1048. }
  1049. return 0;
  1050. }
  1051. typedef int (*vtd_page_walk_hook)(const IOMMUTLBEvent *event, void *private);
  1052. /**
  1053. * Constant information used during page walking
  1054. *
  1055. * @hook_fn: hook func to be called when detected page
  1056. * @private: private data to be passed into hook func
  1057. * @notify_unmap: whether we should notify invalid entries
  1058. * @as: VT-d address space of the device
  1059. * @aw: maximum address width
  1060. * @domain: domain ID of the page walk
  1061. */
  1062. typedef struct {
  1063. VTDAddressSpace *as;
  1064. vtd_page_walk_hook hook_fn;
  1065. void *private;
  1066. bool notify_unmap;
  1067. uint8_t aw;
  1068. uint16_t domain_id;
  1069. } vtd_page_walk_info;
  1070. static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
  1071. {
  1072. VTDAddressSpace *as = info->as;
  1073. vtd_page_walk_hook hook_fn = info->hook_fn;
  1074. void *private = info->private;
  1075. IOMMUTLBEntry *entry = &event->entry;
  1076. DMAMap target = {
  1077. .iova = entry->iova,
  1078. .size = entry->addr_mask,
  1079. .translated_addr = entry->translated_addr,
  1080. .perm = entry->perm,
  1081. };
  1082. const DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
  1083. if (event->type == IOMMU_NOTIFIER_UNMAP && !info->notify_unmap) {
  1084. trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
  1085. return 0;
  1086. }
  1087. assert(hook_fn);
  1088. /* Update local IOVA mapped ranges */
  1089. if (event->type == IOMMU_NOTIFIER_MAP) {
  1090. if (mapped) {
  1091. /* If it's exactly the same translation, skip */
  1092. if (!memcmp(mapped, &target, sizeof(target))) {
  1093. trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask,
  1094. entry->translated_addr);
  1095. return 0;
  1096. } else {
  1097. /*
  1098. * Translation changed. Normally this should not
  1099. * happen, but it can happen when with buggy guest
  1100. * OSes. Note that there will be a small window that
  1101. * we don't have map at all. But that's the best
  1102. * effort we can do. The ideal way to emulate this is
  1103. * atomically modify the PTE to follow what has
  1104. * changed, but we can't. One example is that vfio
  1105. * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
  1106. * interface to modify a mapping (meanwhile it seems
  1107. * meaningless to even provide one). Anyway, let's
  1108. * mark this as a TODO in case one day we'll have
  1109. * a better solution.
  1110. */
  1111. IOMMUAccessFlags cache_perm = entry->perm;
  1112. int ret;
  1113. /* Emulate an UNMAP */
  1114. event->type = IOMMU_NOTIFIER_UNMAP;
  1115. entry->perm = IOMMU_NONE;
  1116. trace_vtd_page_walk_one(info->domain_id,
  1117. entry->iova,
  1118. entry->translated_addr,
  1119. entry->addr_mask,
  1120. entry->perm);
  1121. ret = hook_fn(event, private);
  1122. if (ret) {
  1123. return ret;
  1124. }
  1125. /* Drop any existing mapping */
  1126. iova_tree_remove(as->iova_tree, target);
  1127. /* Recover the correct type */
  1128. event->type = IOMMU_NOTIFIER_MAP;
  1129. entry->perm = cache_perm;
  1130. }
  1131. }
  1132. iova_tree_insert(as->iova_tree, &target);
  1133. } else {
  1134. if (!mapped) {
  1135. /* Skip since we didn't map this range at all */
  1136. trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
  1137. return 0;
  1138. }
  1139. iova_tree_remove(as->iova_tree, target);
  1140. }
  1141. trace_vtd_page_walk_one(info->domain_id, entry->iova,
  1142. entry->translated_addr, entry->addr_mask,
  1143. entry->perm);
  1144. return hook_fn(event, private);
  1145. }
  1146. /**
  1147. * vtd_page_walk_level - walk over specific level for IOVA range
  1148. *
  1149. * @addr: base GPA addr to start the walk
  1150. * @start: IOVA range start address
  1151. * @end: IOVA range end address (start <= addr < end)
  1152. * @read: whether parent level has read permission
  1153. * @write: whether parent level has write permission
  1154. * @info: constant information for the page walk
  1155. */
  1156. static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
  1157. uint64_t end, uint32_t level, bool read,
  1158. bool write, vtd_page_walk_info *info)
  1159. {
  1160. bool read_cur, write_cur, entry_valid;
  1161. uint32_t offset;
  1162. uint64_t slpte;
  1163. uint64_t subpage_size, subpage_mask;
  1164. IOMMUTLBEvent event;
  1165. uint64_t iova = start;
  1166. uint64_t iova_next;
  1167. int ret = 0;
  1168. trace_vtd_page_walk_level(addr, level, start, end);
  1169. subpage_size = 1ULL << vtd_pt_level_shift(level);
  1170. subpage_mask = vtd_pt_level_page_mask(level);
  1171. while (iova < end) {
  1172. iova_next = (iova & subpage_mask) + subpage_size;
  1173. offset = vtd_iova_level_offset(iova, level);
  1174. slpte = vtd_get_pte(addr, offset);
  1175. if (slpte == (uint64_t)-1) {
  1176. trace_vtd_page_walk_skip_read(iova, iova_next);
  1177. goto next;
  1178. }
  1179. if (vtd_slpte_nonzero_rsvd(slpte, level)) {
  1180. trace_vtd_page_walk_skip_reserve(iova, iova_next);
  1181. goto next;
  1182. }
  1183. /* Permissions are stacked with parents' */
  1184. read_cur = read && (slpte & VTD_SL_R);
  1185. write_cur = write && (slpte & VTD_SL_W);
  1186. /*
  1187. * As long as we have either read/write permission, this is a
  1188. * valid entry. The rule works for both page entries and page
  1189. * table entries.
  1190. */
  1191. entry_valid = read_cur | write_cur;
  1192. if (!vtd_is_last_pte(slpte, level) && entry_valid) {
  1193. /*
  1194. * This is a valid PDE (or even bigger than PDE). We need
  1195. * to walk one further level.
  1196. */
  1197. ret = vtd_page_walk_level(vtd_get_pte_addr(slpte, info->aw),
  1198. iova, MIN(iova_next, end), level - 1,
  1199. read_cur, write_cur, info);
  1200. } else {
  1201. /*
  1202. * This means we are either:
  1203. *
  1204. * (1) the real page entry (either 4K page, or huge page)
  1205. * (2) the whole range is invalid
  1206. *
  1207. * In either case, we send an IOTLB notification down.
  1208. */
  1209. event.entry.target_as = &address_space_memory;
  1210. event.entry.iova = iova & subpage_mask;
  1211. event.entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
  1212. event.entry.addr_mask = ~subpage_mask;
  1213. /* NOTE: this is only meaningful if entry_valid == true */
  1214. event.entry.translated_addr = vtd_get_pte_addr(slpte, info->aw);
  1215. event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP :
  1216. IOMMU_NOTIFIER_UNMAP;
  1217. ret = vtd_page_walk_one(&event, info);
  1218. }
  1219. if (ret < 0) {
  1220. return ret;
  1221. }
  1222. next:
  1223. iova = iova_next;
  1224. }
  1225. return 0;
  1226. }
  1227. /**
  1228. * vtd_page_walk - walk specific IOVA range, and call the hook
  1229. *
  1230. * @s: intel iommu state
  1231. * @ce: context entry to walk upon
  1232. * @start: IOVA address to start the walk
  1233. * @end: IOVA range end address (start <= addr < end)
  1234. * @info: page walking information struct
  1235. */
  1236. static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce,
  1237. uint64_t start, uint64_t end,
  1238. vtd_page_walk_info *info,
  1239. uint32_t pasid)
  1240. {
  1241. dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
  1242. uint32_t level = vtd_get_iova_level(s, ce, pasid);
  1243. if (!vtd_iova_sl_range_check(s, start, ce, info->aw, pasid)) {
  1244. return -VTD_FR_ADDR_BEYOND_MGAW;
  1245. }
  1246. if (!vtd_iova_sl_range_check(s, end, ce, info->aw, pasid)) {
  1247. /* Fix end so that it reaches the maximum */
  1248. end = vtd_iova_limit(s, ce, info->aw, pasid);
  1249. }
  1250. return vtd_page_walk_level(addr, start, end, level, true, true, info);
  1251. }
  1252. static int vtd_root_entry_rsvd_bits_check(IntelIOMMUState *s,
  1253. VTDRootEntry *re)
  1254. {
  1255. /* Legacy Mode reserved bits check */
  1256. if (!s->root_scalable &&
  1257. (re->hi || (re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
  1258. goto rsvd_err;
  1259. /* Scalable Mode reserved bits check */
  1260. if (s->root_scalable &&
  1261. ((re->lo & VTD_ROOT_ENTRY_RSVD(s->aw_bits)) ||
  1262. (re->hi & VTD_ROOT_ENTRY_RSVD(s->aw_bits))))
  1263. goto rsvd_err;
  1264. return 0;
  1265. rsvd_err:
  1266. error_report_once("%s: invalid root entry: hi=0x%"PRIx64
  1267. ", lo=0x%"PRIx64,
  1268. __func__, re->hi, re->lo);
  1269. return -VTD_FR_ROOT_ENTRY_RSVD;
  1270. }
  1271. static inline int vtd_context_entry_rsvd_bits_check(IntelIOMMUState *s,
  1272. VTDContextEntry *ce)
  1273. {
  1274. if (!s->root_scalable &&
  1275. (ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI ||
  1276. ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
  1277. error_report_once("%s: invalid context entry: hi=%"PRIx64
  1278. ", lo=%"PRIx64" (reserved nonzero)",
  1279. __func__, ce->hi, ce->lo);
  1280. return -VTD_FR_CONTEXT_ENTRY_RSVD;
  1281. }
  1282. if (s->root_scalable &&
  1283. (ce->val[0] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(s->aw_bits) ||
  1284. ce->val[1] & VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 ||
  1285. ce->val[2] ||
  1286. ce->val[3])) {
  1287. error_report_once("%s: invalid context entry: val[3]=%"PRIx64
  1288. ", val[2]=%"PRIx64
  1289. ", val[1]=%"PRIx64
  1290. ", val[0]=%"PRIx64" (reserved nonzero)",
  1291. __func__, ce->val[3], ce->val[2],
  1292. ce->val[1], ce->val[0]);
  1293. return -VTD_FR_CONTEXT_ENTRY_RSVD;
  1294. }
  1295. return 0;
  1296. }
  1297. static int vtd_ce_rid2pasid_check(IntelIOMMUState *s,
  1298. VTDContextEntry *ce)
  1299. {
  1300. VTDPASIDEntry pe;
  1301. /*
  1302. * Make sure in Scalable Mode, a present context entry
  1303. * has valid rid2pasid setting, which includes valid
  1304. * rid2pasid field and corresponding pasid entry setting
  1305. */
  1306. return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID);
  1307. }
  1308. /* Map a device to its corresponding domain (context-entry) */
  1309. static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
  1310. uint8_t devfn, VTDContextEntry *ce)
  1311. {
  1312. VTDRootEntry re;
  1313. int ret_fr;
  1314. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  1315. ret_fr = vtd_get_root_entry(s, bus_num, &re);
  1316. if (ret_fr) {
  1317. return ret_fr;
  1318. }
  1319. if (!vtd_root_entry_present(s, &re, devfn)) {
  1320. /* Not error - it's okay we don't have root entry. */
  1321. trace_vtd_re_not_present(bus_num);
  1322. return -VTD_FR_ROOT_ENTRY_P;
  1323. }
  1324. ret_fr = vtd_root_entry_rsvd_bits_check(s, &re);
  1325. if (ret_fr) {
  1326. return ret_fr;
  1327. }
  1328. ret_fr = vtd_get_context_entry_from_root(s, &re, devfn, ce);
  1329. if (ret_fr) {
  1330. return ret_fr;
  1331. }
  1332. if (!vtd_ce_present(ce)) {
  1333. /* Not error - it's okay we don't have context entry. */
  1334. trace_vtd_ce_not_present(bus_num, devfn);
  1335. return -VTD_FR_CONTEXT_ENTRY_P;
  1336. }
  1337. ret_fr = vtd_context_entry_rsvd_bits_check(s, ce);
  1338. if (ret_fr) {
  1339. return ret_fr;
  1340. }
  1341. /* Check if the programming of context-entry is valid */
  1342. if (!s->root_scalable &&
  1343. !vtd_is_sl_level_supported(s, vtd_ce_get_level(ce))) {
  1344. error_report_once("%s: invalid context entry: hi=%"PRIx64
  1345. ", lo=%"PRIx64" (level %d not supported)",
  1346. __func__, ce->hi, ce->lo,
  1347. vtd_ce_get_level(ce));
  1348. return -VTD_FR_CONTEXT_ENTRY_INV;
  1349. }
  1350. if (!s->root_scalable) {
  1351. /* Do translation type check */
  1352. if (!vtd_ce_type_check(x86_iommu, ce)) {
  1353. /* Errors dumped in vtd_ce_type_check() */
  1354. return -VTD_FR_CONTEXT_ENTRY_INV;
  1355. }
  1356. } else {
  1357. /*
  1358. * Check if the programming of context-entry.rid2pasid
  1359. * and corresponding pasid setting is valid, and thus
  1360. * avoids to check pasid entry fetching result in future
  1361. * helper function calling.
  1362. */
  1363. ret_fr = vtd_ce_rid2pasid_check(s, ce);
  1364. if (ret_fr) {
  1365. return ret_fr;
  1366. }
  1367. }
  1368. return 0;
  1369. }
  1370. static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event,
  1371. void *private)
  1372. {
  1373. memory_region_notify_iommu(private, 0, *event);
  1374. return 0;
  1375. }
  1376. static uint16_t vtd_get_domain_id(IntelIOMMUState *s,
  1377. VTDContextEntry *ce,
  1378. uint32_t pasid)
  1379. {
  1380. VTDPASIDEntry pe;
  1381. if (s->root_scalable) {
  1382. vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
  1383. return VTD_SM_PASID_ENTRY_DID(pe.val[1]);
  1384. }
  1385. return VTD_CONTEXT_ENTRY_DID(ce->hi);
  1386. }
  1387. static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
  1388. VTDContextEntry *ce,
  1389. hwaddr addr, hwaddr size)
  1390. {
  1391. IntelIOMMUState *s = vtd_as->iommu_state;
  1392. vtd_page_walk_info info = {
  1393. .hook_fn = vtd_sync_shadow_page_hook,
  1394. .private = (void *)&vtd_as->iommu,
  1395. .notify_unmap = true,
  1396. .aw = s->aw_bits,
  1397. .as = vtd_as,
  1398. .domain_id = vtd_get_domain_id(s, ce, vtd_as->pasid),
  1399. };
  1400. return vtd_page_walk(s, ce, addr, addr + size, &info, vtd_as->pasid);
  1401. }
  1402. static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
  1403. {
  1404. int ret;
  1405. VTDContextEntry ce;
  1406. IOMMUNotifier *n;
  1407. /* If no MAP notifier registered, we simply invalidate all the cache */
  1408. if (!vtd_as_has_map_notifier(vtd_as)) {
  1409. IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
  1410. memory_region_unmap_iommu_notifier_range(n);
  1411. }
  1412. return 0;
  1413. }
  1414. ret = vtd_dev_to_context_entry(vtd_as->iommu_state,
  1415. pci_bus_num(vtd_as->bus),
  1416. vtd_as->devfn, &ce);
  1417. if (ret) {
  1418. if (ret == -VTD_FR_CONTEXT_ENTRY_P) {
  1419. /*
  1420. * It's a valid scenario to have a context entry that is
  1421. * not present. For example, when a device is removed
  1422. * from an existing domain then the context entry will be
  1423. * zeroed by the guest before it was put into another
  1424. * domain. When this happens, instead of synchronizing
  1425. * the shadow pages we should invalidate all existing
  1426. * mappings and notify the backends.
  1427. */
  1428. IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
  1429. vtd_address_space_unmap(vtd_as, n);
  1430. }
  1431. ret = 0;
  1432. }
  1433. return ret;
  1434. }
  1435. return vtd_sync_shadow_page_table_range(vtd_as, &ce, 0, UINT64_MAX);
  1436. }
  1437. /*
  1438. * Check if specific device is configured to bypass address
  1439. * translation for DMA requests. In Scalable Mode, bypass
  1440. * 1st-level translation or 2nd-level translation, it depends
  1441. * on PGTT setting.
  1442. */
  1443. static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
  1444. uint32_t pasid)
  1445. {
  1446. VTDPASIDEntry pe;
  1447. int ret;
  1448. if (s->root_scalable) {
  1449. ret = vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid);
  1450. if (ret) {
  1451. /*
  1452. * This error is guest triggerable. We should assumt PT
  1453. * not enabled for safety.
  1454. */
  1455. return false;
  1456. }
  1457. return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
  1458. }
  1459. return (vtd_ce_get_type(ce) == VTD_CONTEXT_TT_PASS_THROUGH);
  1460. }
  1461. static bool vtd_as_pt_enabled(VTDAddressSpace *as)
  1462. {
  1463. IntelIOMMUState *s;
  1464. VTDContextEntry ce;
  1465. assert(as);
  1466. s = as->iommu_state;
  1467. if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
  1468. &ce)) {
  1469. /*
  1470. * Possibly failed to parse the context entry for some reason
  1471. * (e.g., during init, or any guest configuration errors on
  1472. * context entries). We should assume PT not enabled for
  1473. * safety.
  1474. */
  1475. return false;
  1476. }
  1477. return vtd_dev_pt_enabled(s, &ce, as->pasid);
  1478. }
  1479. /* Return whether the device is using IOMMU translation. */
  1480. static bool vtd_switch_address_space(VTDAddressSpace *as)
  1481. {
  1482. bool use_iommu, pt;
  1483. /* Whether we need to take the BQL on our own */
  1484. bool take_bql = !bql_locked();
  1485. assert(as);
  1486. use_iommu = as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as);
  1487. pt = as->iommu_state->dmar_enabled && vtd_as_pt_enabled(as);
  1488. trace_vtd_switch_address_space(pci_bus_num(as->bus),
  1489. VTD_PCI_SLOT(as->devfn),
  1490. VTD_PCI_FUNC(as->devfn),
  1491. use_iommu);
  1492. /*
  1493. * It's possible that we reach here without BQL, e.g., when called
  1494. * from vtd_pt_enable_fast_path(). However the memory APIs need
  1495. * it. We'd better make sure we have had it already, or, take it.
  1496. */
  1497. if (take_bql) {
  1498. bql_lock();
  1499. }
  1500. /* Turn off first then on the other */
  1501. if (use_iommu) {
  1502. memory_region_set_enabled(&as->nodmar, false);
  1503. memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
  1504. /*
  1505. * vt-d spec v3.4 3.14:
  1506. *
  1507. * """
  1508. * Requests-with-PASID with input address in range 0xFEEx_xxxx
  1509. * are translated normally like any other request-with-PASID
  1510. * through DMA-remapping hardware.
  1511. * """
  1512. *
  1513. * Need to disable ir for as with PASID.
  1514. */
  1515. if (as->pasid != PCI_NO_PASID) {
  1516. memory_region_set_enabled(&as->iommu_ir, false);
  1517. } else {
  1518. memory_region_set_enabled(&as->iommu_ir, true);
  1519. }
  1520. } else {
  1521. memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
  1522. memory_region_set_enabled(&as->nodmar, true);
  1523. }
  1524. /*
  1525. * vtd-spec v3.4 3.14:
  1526. *
  1527. * """
  1528. * Requests-with-PASID with input address in range 0xFEEx_xxxx are
  1529. * translated normally like any other request-with-PASID through
  1530. * DMA-remapping hardware. However, if such a request is processed
  1531. * using pass-through translation, it will be blocked as described
  1532. * in the paragraph below.
  1533. *
  1534. * Software must not program paging-structure entries to remap any
  1535. * address to the interrupt address range. Untranslated requests
  1536. * and translation requests that result in an address in the
  1537. * interrupt range will be blocked with condition code LGN.4 or
  1538. * SGN.8.
  1539. * """
  1540. *
  1541. * We enable per as memory region (iommu_ir_fault) for catching
  1542. * the translation for interrupt range through PASID + PT.
  1543. */
  1544. if (pt && as->pasid != PCI_NO_PASID) {
  1545. memory_region_set_enabled(&as->iommu_ir_fault, true);
  1546. } else {
  1547. memory_region_set_enabled(&as->iommu_ir_fault, false);
  1548. }
  1549. if (take_bql) {
  1550. bql_unlock();
  1551. }
  1552. return use_iommu;
  1553. }
  1554. static void vtd_switch_address_space_all(IntelIOMMUState *s)
  1555. {
  1556. VTDAddressSpace *vtd_as;
  1557. GHashTableIter iter;
  1558. g_hash_table_iter_init(&iter, s->vtd_address_spaces);
  1559. while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_as)) {
  1560. vtd_switch_address_space(vtd_as);
  1561. }
  1562. }
  1563. static const bool vtd_qualified_faults[] = {
  1564. [VTD_FR_RESERVED] = false,
  1565. [VTD_FR_ROOT_ENTRY_P] = false,
  1566. [VTD_FR_CONTEXT_ENTRY_P] = true,
  1567. [VTD_FR_CONTEXT_ENTRY_INV] = true,
  1568. [VTD_FR_ADDR_BEYOND_MGAW] = true,
  1569. [VTD_FR_WRITE] = true,
  1570. [VTD_FR_READ] = true,
  1571. [VTD_FR_PAGING_ENTRY_INV] = true,
  1572. [VTD_FR_ROOT_TABLE_INV] = false,
  1573. [VTD_FR_CONTEXT_TABLE_INV] = false,
  1574. [VTD_FR_INTERRUPT_ADDR] = true,
  1575. [VTD_FR_ROOT_ENTRY_RSVD] = false,
  1576. [VTD_FR_PAGING_ENTRY_RSVD] = true,
  1577. [VTD_FR_CONTEXT_ENTRY_TT] = true,
  1578. [VTD_FR_PASID_DIR_ACCESS_ERR] = false,
  1579. [VTD_FR_PASID_DIR_ENTRY_P] = true,
  1580. [VTD_FR_PASID_TABLE_ACCESS_ERR] = false,
  1581. [VTD_FR_PASID_ENTRY_P] = true,
  1582. [VTD_FR_PASID_TABLE_ENTRY_INV] = true,
  1583. [VTD_FR_FS_PAGING_ENTRY_INV] = true,
  1584. [VTD_FR_FS_PAGING_ENTRY_P] = true,
  1585. [VTD_FR_FS_PAGING_ENTRY_RSVD] = true,
  1586. [VTD_FR_PASID_ENTRY_FSPTPTR_INV] = true,
  1587. [VTD_FR_FS_NON_CANONICAL] = true,
  1588. [VTD_FR_FS_PAGING_ENTRY_US] = true,
  1589. [VTD_FR_SM_WRITE] = true,
  1590. [VTD_FR_SM_INTERRUPT_ADDR] = true,
  1591. [VTD_FR_FS_BIT_UPDATE_FAILED] = true,
  1592. [VTD_FR_MAX] = false,
  1593. };
  1594. /* To see if a fault condition is "qualified", which is reported to software
  1595. * only if the FPD field in the context-entry used to process the faulting
  1596. * request is 0.
  1597. */
  1598. static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
  1599. {
  1600. return vtd_qualified_faults[fault];
  1601. }
  1602. static inline bool vtd_is_interrupt_addr(hwaddr addr)
  1603. {
  1604. return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
  1605. }
  1606. static gboolean vtd_find_as_by_sid_and_pasid(gpointer key, gpointer value,
  1607. gpointer user_data)
  1608. {
  1609. struct vtd_as_key *as_key = (struct vtd_as_key *)key;
  1610. struct vtd_as_raw_key *target = (struct vtd_as_raw_key *)user_data;
  1611. uint16_t sid = PCI_BUILD_BDF(pci_bus_num(as_key->bus), as_key->devfn);
  1612. return (as_key->pasid == target->pasid) && (sid == target->sid);
  1613. }
  1614. static VTDAddressSpace *vtd_get_as_by_sid_and_pasid(IntelIOMMUState *s,
  1615. uint16_t sid,
  1616. uint32_t pasid)
  1617. {
  1618. struct vtd_as_raw_key key = {
  1619. .sid = sid,
  1620. .pasid = pasid
  1621. };
  1622. return g_hash_table_find(s->vtd_address_spaces,
  1623. vtd_find_as_by_sid_and_pasid, &key);
  1624. }
  1625. static VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
  1626. {
  1627. return vtd_get_as_by_sid_and_pasid(s, sid, PCI_NO_PASID);
  1628. }
  1629. static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
  1630. {
  1631. VTDAddressSpace *vtd_as;
  1632. bool success = false;
  1633. vtd_as = vtd_get_as_by_sid(s, source_id);
  1634. if (!vtd_as) {
  1635. goto out;
  1636. }
  1637. if (vtd_switch_address_space(vtd_as) == false) {
  1638. /* We switched off IOMMU region successfully. */
  1639. success = true;
  1640. }
  1641. out:
  1642. trace_vtd_pt_enable_fast_path(source_id, success);
  1643. }
  1644. /*
  1645. * Rsvd field masks for fpte:
  1646. * vtd_fpte_rsvd 4k pages
  1647. * vtd_fpte_rsvd_large large pages
  1648. *
  1649. * We support only 4-level page tables.
  1650. */
  1651. #define VTD_FPTE_RSVD_LEN 5
  1652. static uint64_t vtd_fpte_rsvd[VTD_FPTE_RSVD_LEN];
  1653. static uint64_t vtd_fpte_rsvd_large[VTD_FPTE_RSVD_LEN];
  1654. static bool vtd_flpte_nonzero_rsvd(uint64_t flpte, uint32_t level)
  1655. {
  1656. uint64_t rsvd_mask;
  1657. /*
  1658. * We should have caught a guest-mis-programmed level earlier,
  1659. * via vtd_is_fl_level_supported.
  1660. */
  1661. assert(level < VTD_FPTE_RSVD_LEN);
  1662. /*
  1663. * Zero level doesn't exist. The smallest level is VTD_PT_LEVEL=1 and
  1664. * checked by vtd_is_last_pte().
  1665. */
  1666. assert(level);
  1667. if ((level == VTD_PD_LEVEL || level == VTD_PDP_LEVEL) &&
  1668. (flpte & VTD_PT_PAGE_SIZE_MASK)) {
  1669. /* large page */
  1670. rsvd_mask = vtd_fpte_rsvd_large[level];
  1671. } else {
  1672. rsvd_mask = vtd_fpte_rsvd[level];
  1673. }
  1674. return flpte & rsvd_mask;
  1675. }
  1676. static inline bool vtd_flpte_present(uint64_t flpte)
  1677. {
  1678. return !!(flpte & VTD_FL_P);
  1679. }
  1680. /* Return true if IOVA is canonical, otherwise false. */
  1681. static bool vtd_iova_fl_check_canonical(IntelIOMMUState *s, uint64_t iova,
  1682. VTDContextEntry *ce, uint32_t pasid)
  1683. {
  1684. uint64_t iova_limit = vtd_iova_limit(s, ce, s->aw_bits, pasid);
  1685. uint64_t upper_bits_mask = ~(iova_limit - 1);
  1686. uint64_t upper_bits = iova & upper_bits_mask;
  1687. bool msb = ((iova & (iova_limit >> 1)) != 0);
  1688. if (msb) {
  1689. return upper_bits == upper_bits_mask;
  1690. } else {
  1691. return !upper_bits;
  1692. }
  1693. }
  1694. static MemTxResult vtd_set_flag_in_pte(dma_addr_t base_addr, uint32_t index,
  1695. uint64_t pte, uint64_t flag)
  1696. {
  1697. if (pte & flag) {
  1698. return MEMTX_OK;
  1699. }
  1700. pte |= flag;
  1701. pte = cpu_to_le64(pte);
  1702. return dma_memory_write(&address_space_memory,
  1703. base_addr + index * sizeof(pte),
  1704. &pte, sizeof(pte),
  1705. MEMTXATTRS_UNSPECIFIED);
  1706. }
  1707. /*
  1708. * Given the @iova, get relevant @flptep. @flpte_level will be the last level
  1709. * of the translation, can be used for deciding the size of large page.
  1710. */
  1711. static int vtd_iova_to_flpte(IntelIOMMUState *s, VTDContextEntry *ce,
  1712. uint64_t iova, bool is_write,
  1713. uint64_t *flptep, uint32_t *flpte_level,
  1714. bool *reads, bool *writes, uint8_t aw_bits,
  1715. uint32_t pasid)
  1716. {
  1717. dma_addr_t addr = vtd_get_iova_pgtbl_base(s, ce, pasid);
  1718. uint32_t level = vtd_get_iova_level(s, ce, pasid);
  1719. uint32_t offset;
  1720. uint64_t flpte, flag_ad = VTD_FL_A;
  1721. if (!vtd_iova_fl_check_canonical(s, iova, ce, pasid)) {
  1722. error_report_once("%s: detected non canonical IOVA (iova=0x%" PRIx64 ","
  1723. "pasid=0x%" PRIx32 ")", __func__, iova, pasid);
  1724. return -VTD_FR_FS_NON_CANONICAL;
  1725. }
  1726. while (true) {
  1727. offset = vtd_iova_level_offset(iova, level);
  1728. flpte = vtd_get_pte(addr, offset);
  1729. if (flpte == (uint64_t)-1) {
  1730. if (level == vtd_get_iova_level(s, ce, pasid)) {
  1731. /* Invalid programming of pasid-entry */
  1732. return -VTD_FR_PASID_ENTRY_FSPTPTR_INV;
  1733. } else {
  1734. return -VTD_FR_FS_PAGING_ENTRY_INV;
  1735. }
  1736. }
  1737. if (!vtd_flpte_present(flpte)) {
  1738. *reads = false;
  1739. *writes = false;
  1740. return -VTD_FR_FS_PAGING_ENTRY_P;
  1741. }
  1742. /* No emulated device supports supervisor privilege request yet */
  1743. if (!(flpte & VTD_FL_US)) {
  1744. *reads = false;
  1745. *writes = false;
  1746. return -VTD_FR_FS_PAGING_ENTRY_US;
  1747. }
  1748. *reads = true;
  1749. *writes = (*writes) && (flpte & VTD_FL_RW);
  1750. if (is_write && !(flpte & VTD_FL_RW)) {
  1751. return -VTD_FR_SM_WRITE;
  1752. }
  1753. if (vtd_flpte_nonzero_rsvd(flpte, level)) {
  1754. error_report_once("%s: detected flpte reserved non-zero "
  1755. "iova=0x%" PRIx64 ", level=0x%" PRIx32
  1756. "flpte=0x%" PRIx64 ", pasid=0x%" PRIX32 ")",
  1757. __func__, iova, level, flpte, pasid);
  1758. return -VTD_FR_FS_PAGING_ENTRY_RSVD;
  1759. }
  1760. if (vtd_is_last_pte(flpte, level) && is_write) {
  1761. flag_ad |= VTD_FL_D;
  1762. }
  1763. if (vtd_set_flag_in_pte(addr, offset, flpte, flag_ad) != MEMTX_OK) {
  1764. return -VTD_FR_FS_BIT_UPDATE_FAILED;
  1765. }
  1766. if (vtd_is_last_pte(flpte, level)) {
  1767. *flptep = flpte;
  1768. *flpte_level = level;
  1769. return 0;
  1770. }
  1771. addr = vtd_get_pte_addr(flpte, aw_bits);
  1772. level--;
  1773. }
  1774. }
  1775. static void vtd_report_fault(IntelIOMMUState *s,
  1776. int err, bool is_fpd_set,
  1777. uint16_t source_id,
  1778. hwaddr addr,
  1779. bool is_write,
  1780. bool is_pasid,
  1781. uint32_t pasid)
  1782. {
  1783. if (is_fpd_set && vtd_is_qualified_fault(err)) {
  1784. trace_vtd_fault_disabled();
  1785. } else {
  1786. vtd_report_dmar_fault(s, source_id, addr, err, is_write,
  1787. is_pasid, pasid);
  1788. }
  1789. }
  1790. /* Map dev to context-entry then do a paging-structures walk to do a iommu
  1791. * translation.
  1792. *
  1793. * Called from RCU critical section.
  1794. *
  1795. * @bus_num: The bus number
  1796. * @devfn: The devfn, which is the combined of device and function number
  1797. * @is_write: The access is a write operation
  1798. * @entry: IOMMUTLBEntry that contain the addr to be translated and result
  1799. *
  1800. * Returns true if translation is successful, otherwise false.
  1801. */
  1802. static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
  1803. uint8_t devfn, hwaddr addr, bool is_write,
  1804. IOMMUTLBEntry *entry)
  1805. {
  1806. IntelIOMMUState *s = vtd_as->iommu_state;
  1807. VTDContextEntry ce;
  1808. uint8_t bus_num = pci_bus_num(bus);
  1809. VTDContextCacheEntry *cc_entry;
  1810. uint64_t pte, page_mask;
  1811. uint32_t level, pasid = vtd_as->pasid;
  1812. uint16_t source_id = PCI_BUILD_BDF(bus_num, devfn);
  1813. int ret_fr;
  1814. bool is_fpd_set = false;
  1815. bool reads = true;
  1816. bool writes = true;
  1817. uint8_t access_flags, pgtt;
  1818. bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
  1819. VTDIOTLBEntry *iotlb_entry;
  1820. uint64_t xlat, size;
  1821. /*
  1822. * We have standalone memory region for interrupt addresses, we
  1823. * should never receive translation requests in this region.
  1824. */
  1825. assert(!vtd_is_interrupt_addr(addr));
  1826. vtd_iommu_lock(s);
  1827. cc_entry = &vtd_as->context_cache_entry;
  1828. /* Try to fetch pte from IOTLB, we don't need RID2PASID logic */
  1829. if (!rid2pasid) {
  1830. iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
  1831. if (iotlb_entry) {
  1832. trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
  1833. iotlb_entry->domain_id);
  1834. pte = iotlb_entry->pte;
  1835. access_flags = iotlb_entry->access_flags;
  1836. page_mask = iotlb_entry->mask;
  1837. goto out;
  1838. }
  1839. }
  1840. /* Try to fetch context-entry from cache first */
  1841. if (cc_entry->context_cache_gen == s->context_cache_gen) {
  1842. trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
  1843. cc_entry->context_entry.lo,
  1844. cc_entry->context_cache_gen);
  1845. ce = cc_entry->context_entry;
  1846. is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
  1847. if (!is_fpd_set && s->root_scalable) {
  1848. ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
  1849. if (ret_fr) {
  1850. vtd_report_fault(s, -ret_fr, is_fpd_set,
  1851. source_id, addr, is_write,
  1852. false, 0);
  1853. goto error;
  1854. }
  1855. }
  1856. } else {
  1857. ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
  1858. is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
  1859. if (!ret_fr && !is_fpd_set && s->root_scalable) {
  1860. ret_fr = vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid);
  1861. }
  1862. if (ret_fr) {
  1863. vtd_report_fault(s, -ret_fr, is_fpd_set,
  1864. source_id, addr, is_write,
  1865. false, 0);
  1866. goto error;
  1867. }
  1868. /* Update context-cache */
  1869. trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
  1870. cc_entry->context_cache_gen,
  1871. s->context_cache_gen);
  1872. cc_entry->context_entry = ce;
  1873. cc_entry->context_cache_gen = s->context_cache_gen;
  1874. }
  1875. if (rid2pasid) {
  1876. pasid = VTD_CE_GET_RID2PASID(&ce);
  1877. }
  1878. /*
  1879. * We don't need to translate for pass-through context entries.
  1880. * Also, let's ignore IOTLB caching as well for PT devices.
  1881. */
  1882. if (vtd_dev_pt_enabled(s, &ce, pasid)) {
  1883. entry->iova = addr & VTD_PAGE_MASK_4K;
  1884. entry->translated_addr = entry->iova;
  1885. entry->addr_mask = ~VTD_PAGE_MASK_4K;
  1886. entry->perm = IOMMU_RW;
  1887. trace_vtd_translate_pt(source_id, entry->iova);
  1888. /*
  1889. * When this happens, it means firstly caching-mode is not
  1890. * enabled, and this is the first passthrough translation for
  1891. * the device. Let's enable the fast path for passthrough.
  1892. *
  1893. * When passthrough is disabled again for the device, we can
  1894. * capture it via the context entry invalidation, then the
  1895. * IOMMU region can be swapped back.
  1896. */
  1897. vtd_pt_enable_fast_path(s, source_id);
  1898. vtd_iommu_unlock(s);
  1899. return true;
  1900. }
  1901. /* Try to fetch pte from IOTLB for RID2PASID slow path */
  1902. if (rid2pasid) {
  1903. iotlb_entry = vtd_lookup_iotlb(s, source_id, pasid, addr);
  1904. if (iotlb_entry) {
  1905. trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->pte,
  1906. iotlb_entry->domain_id);
  1907. pte = iotlb_entry->pte;
  1908. access_flags = iotlb_entry->access_flags;
  1909. page_mask = iotlb_entry->mask;
  1910. goto out;
  1911. }
  1912. }
  1913. if (s->flts && s->root_scalable) {
  1914. ret_fr = vtd_iova_to_flpte(s, &ce, addr, is_write, &pte, &level,
  1915. &reads, &writes, s->aw_bits, pasid);
  1916. pgtt = VTD_SM_PASID_ENTRY_FLT;
  1917. } else {
  1918. ret_fr = vtd_iova_to_slpte(s, &ce, addr, is_write, &pte, &level,
  1919. &reads, &writes, s->aw_bits, pasid);
  1920. pgtt = VTD_SM_PASID_ENTRY_SLT;
  1921. }
  1922. if (!ret_fr) {
  1923. xlat = vtd_get_pte_addr(pte, s->aw_bits);
  1924. size = ~vtd_pt_level_page_mask(level) + 1;
  1925. /*
  1926. * Per VT-d spec 4.1 section 3.15: Untranslated requests and translation
  1927. * requests that result in an address in the interrupt range will be
  1928. * blocked with condition code LGN.4 or SGN.8.
  1929. */
  1930. if ((xlat <= VTD_INTERRUPT_ADDR_LAST &&
  1931. xlat + size - 1 >= VTD_INTERRUPT_ADDR_FIRST)) {
  1932. error_report_once("%s: xlat address is in interrupt range "
  1933. "(iova=0x%" PRIx64 ", level=0x%" PRIx32 ", "
  1934. "pte=0x%" PRIx64 ", write=%d, "
  1935. "xlat=0x%" PRIx64 ", size=0x%" PRIx64 ", "
  1936. "pasid=0x%" PRIx32 ")",
  1937. __func__, addr, level, pte, is_write,
  1938. xlat, size, pasid);
  1939. ret_fr = s->scalable_mode ? -VTD_FR_SM_INTERRUPT_ADDR :
  1940. -VTD_FR_INTERRUPT_ADDR;
  1941. }
  1942. }
  1943. if (ret_fr) {
  1944. vtd_report_fault(s, -ret_fr, is_fpd_set, source_id,
  1945. addr, is_write, pasid != PCI_NO_PASID, pasid);
  1946. goto error;
  1947. }
  1948. page_mask = vtd_pt_level_page_mask(level);
  1949. access_flags = IOMMU_ACCESS_FLAG(reads, writes);
  1950. vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid),
  1951. addr, pte, access_flags, level, pasid, pgtt);
  1952. out:
  1953. vtd_iommu_unlock(s);
  1954. entry->iova = addr & page_mask;
  1955. entry->translated_addr = vtd_get_pte_addr(pte, s->aw_bits) & page_mask;
  1956. entry->addr_mask = ~page_mask;
  1957. entry->perm = access_flags;
  1958. return true;
  1959. error:
  1960. vtd_iommu_unlock(s);
  1961. entry->iova = 0;
  1962. entry->translated_addr = 0;
  1963. entry->addr_mask = 0;
  1964. entry->perm = IOMMU_NONE;
  1965. return false;
  1966. }
  1967. static void vtd_root_table_setup(IntelIOMMUState *s)
  1968. {
  1969. s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
  1970. s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
  1971. vtd_update_scalable_state(s);
  1972. trace_vtd_reg_dmar_root(s->root, s->root_scalable);
  1973. }
  1974. static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
  1975. uint32_t index, uint32_t mask)
  1976. {
  1977. x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
  1978. }
  1979. static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
  1980. {
  1981. uint64_t value = 0;
  1982. value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
  1983. s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
  1984. s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
  1985. s->intr_eime = value & VTD_IRTA_EIME;
  1986. /* Notify global invalidation */
  1987. vtd_iec_notify_all(s, true, 0, 0);
  1988. trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
  1989. }
  1990. static void vtd_iommu_replay_all(IntelIOMMUState *s)
  1991. {
  1992. VTDAddressSpace *vtd_as;
  1993. QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
  1994. vtd_address_space_sync(vtd_as);
  1995. }
  1996. }
  1997. static void vtd_context_global_invalidate(IntelIOMMUState *s)
  1998. {
  1999. trace_vtd_inv_desc_cc_global();
  2000. /* Protects context cache */
  2001. vtd_iommu_lock(s);
  2002. s->context_cache_gen++;
  2003. if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
  2004. vtd_reset_context_cache_locked(s);
  2005. }
  2006. vtd_iommu_unlock(s);
  2007. vtd_address_space_refresh_all(s);
  2008. /*
  2009. * From VT-d spec 6.5.2.1, a global context entry invalidation
  2010. * should be followed by a IOTLB global invalidation, so we should
  2011. * be safe even without this. Hoewever, let's replay the region as
  2012. * well to be safer, and go back here when we need finer tunes for
  2013. * VT-d emulation codes.
  2014. */
  2015. vtd_iommu_replay_all(s);
  2016. }
  2017. /* Do a context-cache device-selective invalidation.
  2018. * @func_mask: FM field after shifting
  2019. */
  2020. static void vtd_context_device_invalidate(IntelIOMMUState *s,
  2021. uint16_t source_id,
  2022. uint16_t func_mask)
  2023. {
  2024. GHashTableIter as_it;
  2025. uint16_t mask;
  2026. VTDAddressSpace *vtd_as;
  2027. uint8_t bus_n, devfn;
  2028. trace_vtd_inv_desc_cc_devices(source_id, func_mask);
  2029. switch (func_mask & 3) {
  2030. case 0:
  2031. mask = 0; /* No bits in the SID field masked */
  2032. break;
  2033. case 1:
  2034. mask = 4; /* Mask bit 2 in the SID field */
  2035. break;
  2036. case 2:
  2037. mask = 6; /* Mask bit 2:1 in the SID field */
  2038. break;
  2039. case 3:
  2040. mask = 7; /* Mask bit 2:0 in the SID field */
  2041. break;
  2042. default:
  2043. g_assert_not_reached();
  2044. }
  2045. mask = ~mask;
  2046. bus_n = VTD_SID_TO_BUS(source_id);
  2047. devfn = VTD_SID_TO_DEVFN(source_id);
  2048. g_hash_table_iter_init(&as_it, s->vtd_address_spaces);
  2049. while (g_hash_table_iter_next(&as_it, NULL, (void **)&vtd_as)) {
  2050. if ((pci_bus_num(vtd_as->bus) == bus_n) &&
  2051. (vtd_as->devfn & mask) == (devfn & mask)) {
  2052. trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(vtd_as->devfn),
  2053. VTD_PCI_FUNC(vtd_as->devfn));
  2054. vtd_iommu_lock(s);
  2055. vtd_as->context_cache_entry.context_cache_gen = 0;
  2056. vtd_iommu_unlock(s);
  2057. /*
  2058. * Do switch address space when needed, in case if the
  2059. * device passthrough bit is switched.
  2060. */
  2061. vtd_switch_address_space(vtd_as);
  2062. /*
  2063. * So a device is moving out of (or moving into) a
  2064. * domain, resync the shadow page table.
  2065. * This won't bring bad even if we have no such
  2066. * notifier registered - the IOMMU notification
  2067. * framework will skip MAP notifications if that
  2068. * happened.
  2069. */
  2070. vtd_address_space_sync(vtd_as);
  2071. }
  2072. }
  2073. }
  2074. /* Context-cache invalidation
  2075. * Returns the Context Actual Invalidation Granularity.
  2076. * @val: the content of the CCMD_REG
  2077. */
  2078. static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
  2079. {
  2080. uint64_t caig;
  2081. uint64_t type = val & VTD_CCMD_CIRG_MASK;
  2082. switch (type) {
  2083. case VTD_CCMD_DOMAIN_INVL:
  2084. /* Fall through */
  2085. case VTD_CCMD_GLOBAL_INVL:
  2086. caig = VTD_CCMD_GLOBAL_INVL_A;
  2087. vtd_context_global_invalidate(s);
  2088. break;
  2089. case VTD_CCMD_DEVICE_INVL:
  2090. caig = VTD_CCMD_DEVICE_INVL_A;
  2091. vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
  2092. break;
  2093. default:
  2094. error_report_once("%s: invalid context: 0x%" PRIx64,
  2095. __func__, val);
  2096. caig = 0;
  2097. }
  2098. return caig;
  2099. }
  2100. static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
  2101. {
  2102. trace_vtd_inv_desc_iotlb_global();
  2103. vtd_reset_iotlb(s);
  2104. vtd_iommu_replay_all(s);
  2105. }
  2106. static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
  2107. {
  2108. VTDContextEntry ce;
  2109. VTDAddressSpace *vtd_as;
  2110. trace_vtd_inv_desc_iotlb_domain(domain_id);
  2111. vtd_iommu_lock(s);
  2112. g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
  2113. &domain_id);
  2114. vtd_iommu_unlock(s);
  2115. QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
  2116. if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
  2117. vtd_as->devfn, &ce) &&
  2118. domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
  2119. vtd_address_space_sync(vtd_as);
  2120. }
  2121. }
  2122. }
  2123. /*
  2124. * There is no pasid field in iotlb invalidation descriptor, so PCI_NO_PASID
  2125. * is passed as parameter. Piotlb invalidation supports pasid, pasid in its
  2126. * descriptor is passed which should not be PCI_NO_PASID.
  2127. */
  2128. static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
  2129. uint16_t domain_id, hwaddr addr,
  2130. uint8_t am, uint32_t pasid)
  2131. {
  2132. VTDAddressSpace *vtd_as;
  2133. VTDContextEntry ce;
  2134. int ret;
  2135. hwaddr size = (1 << am) * VTD_PAGE_SIZE;
  2136. QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
  2137. ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
  2138. vtd_as->devfn, &ce);
  2139. if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
  2140. uint32_t rid2pasid = PCI_NO_PASID;
  2141. if (s->root_scalable) {
  2142. rid2pasid = VTD_CE_GET_RID2PASID(&ce);
  2143. }
  2144. /*
  2145. * In legacy mode, vtd_as->pasid == pasid is always true.
  2146. * In scalable mode, for vtd address space backing a PCI
  2147. * device without pasid, needs to compare pasid with
  2148. * rid2pasid of this device.
  2149. */
  2150. if (!(vtd_as->pasid == pasid ||
  2151. (vtd_as->pasid == PCI_NO_PASID && pasid == rid2pasid))) {
  2152. continue;
  2153. }
  2154. if (vtd_as_has_map_notifier(vtd_as)) {
  2155. /*
  2156. * When stage-1 translation is off, as long as we have MAP
  2157. * notifications registered in any of our IOMMU notifiers,
  2158. * we need to sync the shadow page table. Otherwise VFIO
  2159. * device attaches to nested page table instead of shadow
  2160. * page table, so no need to sync.
  2161. */
  2162. if (!s->flts || !s->root_scalable) {
  2163. vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
  2164. }
  2165. } else {
  2166. /*
  2167. * For UNMAP-only notifiers, we don't need to walk the
  2168. * page tables. We just deliver the PSI down to
  2169. * invalidate caches.
  2170. */
  2171. const IOMMUTLBEvent event = {
  2172. .type = IOMMU_NOTIFIER_UNMAP,
  2173. .entry = {
  2174. .target_as = &address_space_memory,
  2175. .iova = addr,
  2176. .translated_addr = 0,
  2177. .addr_mask = size - 1,
  2178. .perm = IOMMU_NONE,
  2179. },
  2180. };
  2181. memory_region_notify_iommu(&vtd_as->iommu, 0, event);
  2182. }
  2183. }
  2184. }
  2185. }
  2186. static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
  2187. hwaddr addr, uint8_t am)
  2188. {
  2189. VTDIOTLBPageInvInfo info;
  2190. trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
  2191. assert(am <= VTD_MAMV);
  2192. info.domain_id = domain_id;
  2193. info.addr = addr;
  2194. info.mask = ~((1 << am) - 1);
  2195. vtd_iommu_lock(s);
  2196. g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
  2197. vtd_iommu_unlock(s);
  2198. vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID);
  2199. }
  2200. /* Flush IOTLB
  2201. * Returns the IOTLB Actual Invalidation Granularity.
  2202. * @val: the content of the IOTLB_REG
  2203. */
  2204. static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
  2205. {
  2206. uint64_t iaig;
  2207. uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
  2208. uint16_t domain_id;
  2209. hwaddr addr;
  2210. uint8_t am;
  2211. switch (type) {
  2212. case VTD_TLB_GLOBAL_FLUSH:
  2213. iaig = VTD_TLB_GLOBAL_FLUSH_A;
  2214. vtd_iotlb_global_invalidate(s);
  2215. break;
  2216. case VTD_TLB_DSI_FLUSH:
  2217. domain_id = VTD_TLB_DID(val);
  2218. iaig = VTD_TLB_DSI_FLUSH_A;
  2219. vtd_iotlb_domain_invalidate(s, domain_id);
  2220. break;
  2221. case VTD_TLB_PSI_FLUSH:
  2222. domain_id = VTD_TLB_DID(val);
  2223. addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
  2224. am = VTD_IVA_AM(addr);
  2225. addr = VTD_IVA_ADDR(addr);
  2226. if (am > VTD_MAMV) {
  2227. error_report_once("%s: address mask overflow: 0x%" PRIx64,
  2228. __func__, vtd_get_quad_raw(s, DMAR_IVA_REG));
  2229. iaig = 0;
  2230. break;
  2231. }
  2232. iaig = VTD_TLB_PSI_FLUSH_A;
  2233. vtd_iotlb_page_invalidate(s, domain_id, addr, am);
  2234. break;
  2235. default:
  2236. error_report_once("%s: invalid granularity: 0x%" PRIx64,
  2237. __func__, val);
  2238. iaig = 0;
  2239. }
  2240. return iaig;
  2241. }
  2242. static void vtd_fetch_inv_desc(IntelIOMMUState *s);
  2243. static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
  2244. {
  2245. return s->qi_enabled && (s->iq_tail == s->iq_head) &&
  2246. (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
  2247. }
  2248. static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
  2249. {
  2250. uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
  2251. trace_vtd_inv_qi_enable(en);
  2252. if (en) {
  2253. s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
  2254. /* 2^(x+8) entries */
  2255. s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8 - (s->iq_dw ? 1 : 0));
  2256. s->qi_enabled = true;
  2257. trace_vtd_inv_qi_setup(s->iq, s->iq_size);
  2258. /* Ok - report back to driver */
  2259. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
  2260. if (s->iq_tail != 0) {
  2261. /*
  2262. * This is a spec violation but Windows guests are known to set up
  2263. * Queued Invalidation this way so we allow the write and process
  2264. * Invalidation Descriptors right away.
  2265. */
  2266. trace_vtd_warn_invalid_qi_tail(s->iq_tail);
  2267. if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
  2268. vtd_fetch_inv_desc(s);
  2269. }
  2270. }
  2271. } else {
  2272. if (vtd_queued_inv_disable_check(s)) {
  2273. /* disable Queued Invalidation */
  2274. vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
  2275. s->iq_head = 0;
  2276. s->qi_enabled = false;
  2277. /* Ok - report back to driver */
  2278. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
  2279. } else {
  2280. error_report_once("%s: detected improper state when disable QI "
  2281. "(head=0x%x, tail=0x%x, last_type=%d)",
  2282. __func__,
  2283. s->iq_head, s->iq_tail, s->iq_last_desc_type);
  2284. }
  2285. }
  2286. }
  2287. /* Set Root Table Pointer */
  2288. static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
  2289. {
  2290. vtd_root_table_setup(s);
  2291. /* Ok - report back to driver */
  2292. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
  2293. vtd_reset_caches(s);
  2294. vtd_address_space_refresh_all(s);
  2295. }
  2296. /* Set Interrupt Remap Table Pointer */
  2297. static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
  2298. {
  2299. vtd_interrupt_remap_table_setup(s);
  2300. /* Ok - report back to driver */
  2301. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
  2302. }
  2303. /* Handle Translation Enable/Disable */
  2304. static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
  2305. {
  2306. if (s->dmar_enabled == en) {
  2307. return;
  2308. }
  2309. trace_vtd_dmar_enable(en);
  2310. if (en) {
  2311. s->dmar_enabled = true;
  2312. /* Ok - report back to driver */
  2313. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
  2314. } else {
  2315. s->dmar_enabled = false;
  2316. /* Clear the index of Fault Recording Register */
  2317. s->next_frcd_reg = 0;
  2318. /* Ok - report back to driver */
  2319. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
  2320. }
  2321. vtd_reset_caches(s);
  2322. vtd_address_space_refresh_all(s);
  2323. }
  2324. /* Handle Interrupt Remap Enable/Disable */
  2325. static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
  2326. {
  2327. trace_vtd_ir_enable(en);
  2328. if (en) {
  2329. s->intr_enabled = true;
  2330. /* Ok - report back to driver */
  2331. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
  2332. } else {
  2333. s->intr_enabled = false;
  2334. /* Ok - report back to driver */
  2335. vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
  2336. }
  2337. }
  2338. /* Handle write to Global Command Register */
  2339. static void vtd_handle_gcmd_write(IntelIOMMUState *s)
  2340. {
  2341. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  2342. uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
  2343. uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
  2344. uint32_t changed = status ^ val;
  2345. trace_vtd_reg_write_gcmd(status, val);
  2346. if ((changed & VTD_GCMD_TE) && s->dma_translation) {
  2347. /* Translation enable/disable */
  2348. vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
  2349. }
  2350. if (val & VTD_GCMD_SRTP) {
  2351. /* Set/update the root-table pointer */
  2352. vtd_handle_gcmd_srtp(s);
  2353. }
  2354. if (changed & VTD_GCMD_QIE) {
  2355. /* Queued Invalidation Enable */
  2356. vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
  2357. }
  2358. if (val & VTD_GCMD_SIRTP) {
  2359. /* Set/update the interrupt remapping root-table pointer */
  2360. vtd_handle_gcmd_sirtp(s);
  2361. }
  2362. if ((changed & VTD_GCMD_IRE) &&
  2363. x86_iommu_ir_supported(x86_iommu)) {
  2364. /* Interrupt remap enable/disable */
  2365. vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
  2366. }
  2367. }
  2368. /* Handle write to Context Command Register */
  2369. static void vtd_handle_ccmd_write(IntelIOMMUState *s)
  2370. {
  2371. uint64_t ret;
  2372. uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
  2373. /* Context-cache invalidation request */
  2374. if (val & VTD_CCMD_ICC) {
  2375. if (s->qi_enabled) {
  2376. error_report_once("Queued Invalidation enabled, "
  2377. "should not use register-based invalidation");
  2378. return;
  2379. }
  2380. ret = vtd_context_cache_invalidate(s, val);
  2381. /* Invalidation completed. Change something to show */
  2382. vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
  2383. ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
  2384. ret);
  2385. }
  2386. }
  2387. /* Handle write to IOTLB Invalidation Register */
  2388. static void vtd_handle_iotlb_write(IntelIOMMUState *s)
  2389. {
  2390. uint64_t ret;
  2391. uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
  2392. /* IOTLB invalidation request */
  2393. if (val & VTD_TLB_IVT) {
  2394. if (s->qi_enabled) {
  2395. error_report_once("Queued Invalidation enabled, "
  2396. "should not use register-based invalidation");
  2397. return;
  2398. }
  2399. ret = vtd_iotlb_flush(s, val);
  2400. /* Invalidation completed. Change something to show */
  2401. vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
  2402. ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
  2403. VTD_TLB_FLUSH_GRANU_MASK_A, ret);
  2404. }
  2405. }
  2406. /* Fetch an Invalidation Descriptor from the Invalidation Queue */
  2407. static bool vtd_get_inv_desc(IntelIOMMUState *s,
  2408. VTDInvDesc *inv_desc)
  2409. {
  2410. dma_addr_t base_addr = s->iq;
  2411. uint32_t offset = s->iq_head;
  2412. uint32_t dw = s->iq_dw ? 32 : 16;
  2413. dma_addr_t addr = base_addr + offset * dw;
  2414. if (dma_memory_read(&address_space_memory, addr,
  2415. inv_desc, dw, MEMTXATTRS_UNSPECIFIED)) {
  2416. error_report_once("Read INV DESC failed.");
  2417. return false;
  2418. }
  2419. inv_desc->lo = le64_to_cpu(inv_desc->lo);
  2420. inv_desc->hi = le64_to_cpu(inv_desc->hi);
  2421. if (dw == 32) {
  2422. inv_desc->val[2] = le64_to_cpu(inv_desc->val[2]);
  2423. inv_desc->val[3] = le64_to_cpu(inv_desc->val[3]);
  2424. }
  2425. return true;
  2426. }
  2427. static bool vtd_inv_desc_reserved_check(IntelIOMMUState *s,
  2428. VTDInvDesc *inv_desc,
  2429. uint64_t mask[4], bool dw,
  2430. const char *func_name,
  2431. const char *desc_type)
  2432. {
  2433. if (s->iq_dw) {
  2434. if (inv_desc->val[0] & mask[0] || inv_desc->val[1] & mask[1] ||
  2435. inv_desc->val[2] & mask[2] || inv_desc->val[3] & mask[3]) {
  2436. error_report("%s: invalid %s desc val[3]: 0x%"PRIx64
  2437. " val[2]: 0x%"PRIx64" val[1]=0x%"PRIx64
  2438. " val[0]=0x%"PRIx64" (reserved nonzero)",
  2439. func_name, desc_type, inv_desc->val[3],
  2440. inv_desc->val[2], inv_desc->val[1],
  2441. inv_desc->val[0]);
  2442. return false;
  2443. }
  2444. } else {
  2445. if (dw) {
  2446. error_report("%s: 256-bit %s desc in 128-bit invalidation queue",
  2447. func_name, desc_type);
  2448. return false;
  2449. }
  2450. if (inv_desc->lo & mask[0] || inv_desc->hi & mask[1]) {
  2451. error_report("%s: invalid %s desc: hi=%"PRIx64", lo=%"PRIx64
  2452. " (reserved nonzero)", func_name, desc_type,
  2453. inv_desc->hi, inv_desc->lo);
  2454. return false;
  2455. }
  2456. }
  2457. return true;
  2458. }
  2459. static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
  2460. {
  2461. uint64_t mask[4] = {VTD_INV_DESC_WAIT_RSVD_LO, VTD_INV_DESC_WAIT_RSVD_HI,
  2462. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2463. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, false,
  2464. __func__, "wait")) {
  2465. return false;
  2466. }
  2467. if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
  2468. /* Status Write */
  2469. uint32_t status_data = (uint32_t)(inv_desc->lo >>
  2470. VTD_INV_DESC_WAIT_DATA_SHIFT);
  2471. assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
  2472. /* FIXME: need to be masked with HAW? */
  2473. dma_addr_t status_addr = inv_desc->hi;
  2474. trace_vtd_inv_desc_wait_sw(status_addr, status_data);
  2475. status_data = cpu_to_le32(status_data);
  2476. if (dma_memory_write(&address_space_memory, status_addr,
  2477. &status_data, sizeof(status_data),
  2478. MEMTXATTRS_UNSPECIFIED)) {
  2479. trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
  2480. return false;
  2481. }
  2482. } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
  2483. /* Interrupt flag */
  2484. vtd_generate_completion_event(s);
  2485. } else {
  2486. error_report_once("%s: invalid wait desc: hi=%"PRIx64", lo=%"PRIx64
  2487. " (unknown type)", __func__, inv_desc->hi,
  2488. inv_desc->lo);
  2489. return false;
  2490. }
  2491. return true;
  2492. }
  2493. static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
  2494. VTDInvDesc *inv_desc)
  2495. {
  2496. uint16_t sid, fmask;
  2497. uint64_t mask[4] = {VTD_INV_DESC_CC_RSVD, VTD_INV_DESC_ALL_ONE,
  2498. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2499. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, false,
  2500. __func__, "cc inv")) {
  2501. return false;
  2502. }
  2503. switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
  2504. case VTD_INV_DESC_CC_DOMAIN:
  2505. trace_vtd_inv_desc_cc_domain(
  2506. (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
  2507. /* Fall through */
  2508. case VTD_INV_DESC_CC_GLOBAL:
  2509. vtd_context_global_invalidate(s);
  2510. break;
  2511. case VTD_INV_DESC_CC_DEVICE:
  2512. sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
  2513. fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
  2514. vtd_context_device_invalidate(s, sid, fmask);
  2515. break;
  2516. default:
  2517. error_report_once("%s: invalid cc inv desc: hi=%"PRIx64", lo=%"PRIx64
  2518. " (invalid type)", __func__, inv_desc->hi,
  2519. inv_desc->lo);
  2520. return false;
  2521. }
  2522. return true;
  2523. }
  2524. static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
  2525. {
  2526. uint16_t domain_id;
  2527. uint8_t am;
  2528. hwaddr addr;
  2529. uint64_t mask[4] = {VTD_INV_DESC_IOTLB_RSVD_LO, VTD_INV_DESC_IOTLB_RSVD_HI,
  2530. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2531. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, false,
  2532. __func__, "iotlb inv")) {
  2533. return false;
  2534. }
  2535. switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
  2536. case VTD_INV_DESC_IOTLB_GLOBAL:
  2537. vtd_iotlb_global_invalidate(s);
  2538. break;
  2539. case VTD_INV_DESC_IOTLB_DOMAIN:
  2540. domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
  2541. vtd_iotlb_domain_invalidate(s, domain_id);
  2542. break;
  2543. case VTD_INV_DESC_IOTLB_PAGE:
  2544. domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
  2545. addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
  2546. am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
  2547. if (am > VTD_MAMV) {
  2548. error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
  2549. ", lo=0x%"PRIx64" (am=%u > VTD_MAMV=%u)",
  2550. __func__, inv_desc->hi, inv_desc->lo,
  2551. am, (unsigned)VTD_MAMV);
  2552. return false;
  2553. }
  2554. vtd_iotlb_page_invalidate(s, domain_id, addr, am);
  2555. break;
  2556. default:
  2557. error_report_once("%s: invalid iotlb inv desc: hi=0x%"PRIx64
  2558. ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
  2559. __func__, inv_desc->hi, inv_desc->lo,
  2560. inv_desc->lo & VTD_INV_DESC_IOTLB_G);
  2561. return false;
  2562. }
  2563. return true;
  2564. }
  2565. static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value,
  2566. gpointer user_data)
  2567. {
  2568. VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
  2569. VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
  2570. return ((entry->domain_id == info->domain_id) &&
  2571. (entry->pasid == info->pasid));
  2572. }
  2573. static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s,
  2574. uint16_t domain_id, uint32_t pasid)
  2575. {
  2576. VTDIOTLBPageInvInfo info;
  2577. VTDAddressSpace *vtd_as;
  2578. VTDContextEntry ce;
  2579. info.domain_id = domain_id;
  2580. info.pasid = pasid;
  2581. vtd_iommu_lock(s);
  2582. g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid,
  2583. &info);
  2584. vtd_iommu_unlock(s);
  2585. QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
  2586. if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
  2587. vtd_as->devfn, &ce) &&
  2588. domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
  2589. uint32_t rid2pasid = VTD_CE_GET_RID2PASID(&ce);
  2590. if ((vtd_as->pasid != PCI_NO_PASID || pasid != rid2pasid) &&
  2591. vtd_as->pasid != pasid) {
  2592. continue;
  2593. }
  2594. if (!s->flts || !vtd_as_has_map_notifier(vtd_as)) {
  2595. vtd_address_space_sync(vtd_as);
  2596. }
  2597. }
  2598. }
  2599. }
  2600. static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
  2601. uint32_t pasid, hwaddr addr, uint8_t am)
  2602. {
  2603. VTDIOTLBPageInvInfo info;
  2604. info.domain_id = domain_id;
  2605. info.pasid = pasid;
  2606. info.addr = addr;
  2607. info.mask = ~((1 << am) - 1);
  2608. vtd_iommu_lock(s);
  2609. g_hash_table_foreach_remove(s->iotlb,
  2610. vtd_hash_remove_by_page_piotlb, &info);
  2611. vtd_iommu_unlock(s);
  2612. vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, pasid);
  2613. }
  2614. static bool vtd_process_piotlb_desc(IntelIOMMUState *s,
  2615. VTDInvDesc *inv_desc)
  2616. {
  2617. uint16_t domain_id;
  2618. uint32_t pasid;
  2619. hwaddr addr;
  2620. uint8_t am;
  2621. uint64_t mask[4] = {VTD_INV_DESC_PIOTLB_RSVD_VAL0,
  2622. VTD_INV_DESC_PIOTLB_RSVD_VAL1,
  2623. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2624. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, true,
  2625. __func__, "piotlb inv")) {
  2626. return false;
  2627. }
  2628. domain_id = VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]);
  2629. pasid = VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]);
  2630. switch (inv_desc->val[0] & VTD_INV_DESC_PIOTLB_G) {
  2631. case VTD_INV_DESC_PIOTLB_ALL_IN_PASID:
  2632. vtd_piotlb_pasid_invalidate(s, domain_id, pasid);
  2633. break;
  2634. case VTD_INV_DESC_PIOTLB_PSI_IN_PASID:
  2635. am = VTD_INV_DESC_PIOTLB_AM(inv_desc->val[1]);
  2636. addr = (hwaddr) VTD_INV_DESC_PIOTLB_ADDR(inv_desc->val[1]);
  2637. vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am);
  2638. break;
  2639. default:
  2640. error_report_once("%s: invalid piotlb inv desc: hi=0x%"PRIx64
  2641. ", lo=0x%"PRIx64" (type mismatch: 0x%llx)",
  2642. __func__, inv_desc->val[1], inv_desc->val[0],
  2643. inv_desc->val[0] & VTD_INV_DESC_IOTLB_G);
  2644. return false;
  2645. }
  2646. return true;
  2647. }
  2648. static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
  2649. VTDInvDesc *inv_desc)
  2650. {
  2651. uint64_t mask[4] = {VTD_INV_DESC_IEC_RSVD, VTD_INV_DESC_ALL_ONE,
  2652. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2653. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, false,
  2654. __func__, "iec inv")) {
  2655. return false;
  2656. }
  2657. trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
  2658. inv_desc->iec.index,
  2659. inv_desc->iec.index_mask);
  2660. vtd_iec_notify_all(s, !inv_desc->iec.granularity,
  2661. inv_desc->iec.index,
  2662. inv_desc->iec.index_mask);
  2663. return true;
  2664. }
  2665. static void do_invalidate_device_tlb(VTDAddressSpace *vtd_dev_as,
  2666. bool size, hwaddr addr)
  2667. {
  2668. /*
  2669. * According to ATS spec table 2.4:
  2670. * S = 0, bits 15:12 = xxxx range size: 4K
  2671. * S = 1, bits 15:12 = xxx0 range size: 8K
  2672. * S = 1, bits 15:12 = xx01 range size: 16K
  2673. * S = 1, bits 15:12 = x011 range size: 32K
  2674. * S = 1, bits 15:12 = 0111 range size: 64K
  2675. * ...
  2676. */
  2677. IOMMUTLBEvent event;
  2678. uint64_t sz;
  2679. if (size) {
  2680. sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
  2681. addr &= ~(sz - 1);
  2682. } else {
  2683. sz = VTD_PAGE_SIZE;
  2684. }
  2685. event.type = IOMMU_NOTIFIER_DEVIOTLB_UNMAP;
  2686. event.entry.target_as = &vtd_dev_as->as;
  2687. event.entry.addr_mask = sz - 1;
  2688. event.entry.iova = addr;
  2689. event.entry.perm = IOMMU_NONE;
  2690. event.entry.translated_addr = 0;
  2691. memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
  2692. }
  2693. static bool vtd_process_device_piotlb_desc(IntelIOMMUState *s,
  2694. VTDInvDesc *inv_desc)
  2695. {
  2696. uint16_t sid;
  2697. VTDAddressSpace *vtd_dev_as;
  2698. bool size;
  2699. bool global;
  2700. hwaddr addr;
  2701. uint32_t pasid;
  2702. uint64_t mask[4] = {VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_VAL0,
  2703. VTD_INV_DESC_PASID_DEVICE_IOTLB_RSVD_VAL1,
  2704. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2705. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, true,
  2706. __func__, "device piotlb inv")) {
  2707. return false;
  2708. }
  2709. global = VTD_INV_DESC_PASID_DEVICE_IOTLB_GLOBAL(inv_desc->hi);
  2710. size = VTD_INV_DESC_PASID_DEVICE_IOTLB_SIZE(inv_desc->hi);
  2711. addr = VTD_INV_DESC_PASID_DEVICE_IOTLB_ADDR(inv_desc->hi);
  2712. sid = VTD_INV_DESC_PASID_DEVICE_IOTLB_SID(inv_desc->lo);
  2713. if (global) {
  2714. QLIST_FOREACH(vtd_dev_as, &s->vtd_as_with_notifiers, next) {
  2715. if ((vtd_dev_as->pasid != PCI_NO_PASID) &&
  2716. (PCI_BUILD_BDF(pci_bus_num(vtd_dev_as->bus),
  2717. vtd_dev_as->devfn) == sid)) {
  2718. do_invalidate_device_tlb(vtd_dev_as, size, addr);
  2719. }
  2720. }
  2721. } else {
  2722. pasid = VTD_INV_DESC_PASID_DEVICE_IOTLB_PASID(inv_desc->lo);
  2723. vtd_dev_as = vtd_get_as_by_sid_and_pasid(s, sid, pasid);
  2724. if (!vtd_dev_as) {
  2725. return true;
  2726. }
  2727. do_invalidate_device_tlb(vtd_dev_as, size, addr);
  2728. }
  2729. return true;
  2730. }
  2731. static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
  2732. VTDInvDesc *inv_desc)
  2733. {
  2734. VTDAddressSpace *vtd_dev_as;
  2735. hwaddr addr;
  2736. uint16_t sid;
  2737. bool size;
  2738. uint64_t mask[4] = {VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO,
  2739. VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI,
  2740. VTD_INV_DESC_ALL_ONE, VTD_INV_DESC_ALL_ONE};
  2741. if (!vtd_inv_desc_reserved_check(s, inv_desc, mask, false,
  2742. __func__, "dev-iotlb inv")) {
  2743. return false;
  2744. }
  2745. addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
  2746. sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
  2747. size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
  2748. /*
  2749. * Using sid is OK since the guest should have finished the
  2750. * initialization of both the bus and device.
  2751. */
  2752. vtd_dev_as = vtd_get_as_by_sid(s, sid);
  2753. if (!vtd_dev_as) {
  2754. goto done;
  2755. }
  2756. do_invalidate_device_tlb(vtd_dev_as, size, addr);
  2757. done:
  2758. return true;
  2759. }
  2760. static bool vtd_process_inv_desc(IntelIOMMUState *s)
  2761. {
  2762. VTDInvDesc inv_desc;
  2763. uint8_t desc_type;
  2764. trace_vtd_inv_qi_head(s->iq_head);
  2765. if (!vtd_get_inv_desc(s, &inv_desc)) {
  2766. s->iq_last_desc_type = VTD_INV_DESC_NONE;
  2767. return false;
  2768. }
  2769. desc_type = VTD_INV_DESC_TYPE(inv_desc.lo);
  2770. /* FIXME: should update at first or at last? */
  2771. s->iq_last_desc_type = desc_type;
  2772. switch (desc_type) {
  2773. case VTD_INV_DESC_CC:
  2774. trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
  2775. if (!vtd_process_context_cache_desc(s, &inv_desc)) {
  2776. return false;
  2777. }
  2778. break;
  2779. case VTD_INV_DESC_IOTLB:
  2780. trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
  2781. if (!vtd_process_iotlb_desc(s, &inv_desc)) {
  2782. return false;
  2783. }
  2784. break;
  2785. case VTD_INV_DESC_PIOTLB:
  2786. trace_vtd_inv_desc("p-iotlb", inv_desc.val[1], inv_desc.val[0]);
  2787. if (!vtd_process_piotlb_desc(s, &inv_desc)) {
  2788. return false;
  2789. }
  2790. break;
  2791. case VTD_INV_DESC_WAIT:
  2792. trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
  2793. if (!vtd_process_wait_desc(s, &inv_desc)) {
  2794. return false;
  2795. }
  2796. break;
  2797. case VTD_INV_DESC_IEC:
  2798. trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
  2799. if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
  2800. return false;
  2801. }
  2802. break;
  2803. case VTD_INV_DESC_DEV_PIOTLB:
  2804. trace_vtd_inv_desc("device-piotlb", inv_desc.hi, inv_desc.lo);
  2805. if (!vtd_process_device_piotlb_desc(s, &inv_desc)) {
  2806. return false;
  2807. }
  2808. break;
  2809. case VTD_INV_DESC_DEVICE:
  2810. trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
  2811. if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
  2812. return false;
  2813. }
  2814. break;
  2815. /*
  2816. * TODO: the entity of below two cases will be implemented in future series.
  2817. * To make guest (which integrates scalable mode support patch set in
  2818. * iommu driver) work, just return true is enough so far.
  2819. */
  2820. case VTD_INV_DESC_PC:
  2821. if (s->scalable_mode) {
  2822. break;
  2823. }
  2824. /* fallthrough */
  2825. default:
  2826. error_report_once("%s: invalid inv desc: hi=%"PRIx64", lo=%"PRIx64
  2827. " (unknown type)", __func__, inv_desc.hi,
  2828. inv_desc.lo);
  2829. return false;
  2830. }
  2831. s->iq_head++;
  2832. if (s->iq_head == s->iq_size) {
  2833. s->iq_head = 0;
  2834. }
  2835. return true;
  2836. }
  2837. /* Try to fetch and process more Invalidation Descriptors */
  2838. static void vtd_fetch_inv_desc(IntelIOMMUState *s)
  2839. {
  2840. int qi_shift;
  2841. /* Refer to 10.4.23 of VT-d spec 3.0 */
  2842. qi_shift = s->iq_dw ? VTD_IQH_QH_SHIFT_5 : VTD_IQH_QH_SHIFT_4;
  2843. trace_vtd_inv_qi_fetch();
  2844. if (s->iq_tail >= s->iq_size) {
  2845. /* Detects an invalid Tail pointer */
  2846. error_report_once("%s: detected invalid QI tail "
  2847. "(tail=0x%x, size=0x%x)",
  2848. __func__, s->iq_tail, s->iq_size);
  2849. vtd_handle_inv_queue_error(s);
  2850. return;
  2851. }
  2852. while (s->iq_head != s->iq_tail) {
  2853. if (!vtd_process_inv_desc(s)) {
  2854. /* Invalidation Queue Errors */
  2855. vtd_handle_inv_queue_error(s);
  2856. break;
  2857. }
  2858. /* Must update the IQH_REG in time */
  2859. vtd_set_quad_raw(s, DMAR_IQH_REG,
  2860. (((uint64_t)(s->iq_head)) << qi_shift) &
  2861. VTD_IQH_QH_MASK);
  2862. }
  2863. }
  2864. /* Handle write to Invalidation Queue Tail Register */
  2865. static void vtd_handle_iqt_write(IntelIOMMUState *s)
  2866. {
  2867. uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
  2868. if (s->iq_dw && (val & VTD_IQT_QT_256_RSV_BIT)) {
  2869. error_report_once("%s: RSV bit is set: val=0x%"PRIx64,
  2870. __func__, val);
  2871. vtd_handle_inv_queue_error(s);
  2872. return;
  2873. }
  2874. s->iq_tail = VTD_IQT_QT(s->iq_dw, val);
  2875. trace_vtd_inv_qi_tail(s->iq_tail);
  2876. if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
  2877. /* Process Invalidation Queue here */
  2878. vtd_fetch_inv_desc(s);
  2879. }
  2880. }
  2881. static void vtd_handle_fsts_write(IntelIOMMUState *s)
  2882. {
  2883. uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
  2884. uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
  2885. uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
  2886. if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
  2887. vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
  2888. trace_vtd_fsts_clear_ip();
  2889. }
  2890. /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
  2891. * Descriptors if there are any when Queued Invalidation is enabled?
  2892. */
  2893. }
  2894. static void vtd_handle_fectl_write(IntelIOMMUState *s)
  2895. {
  2896. uint32_t fectl_reg;
  2897. /* FIXME: when software clears the IM field, check the IP field. But do we
  2898. * need to compare the old value and the new value to conclude that
  2899. * software clears the IM field? Or just check if the IM field is zero?
  2900. */
  2901. fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
  2902. trace_vtd_reg_write_fectl(fectl_reg);
  2903. if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
  2904. vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
  2905. vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
  2906. }
  2907. }
  2908. static void vtd_handle_ics_write(IntelIOMMUState *s)
  2909. {
  2910. uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
  2911. uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
  2912. if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
  2913. trace_vtd_reg_ics_clear_ip();
  2914. vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
  2915. }
  2916. }
  2917. static void vtd_handle_iectl_write(IntelIOMMUState *s)
  2918. {
  2919. uint32_t iectl_reg;
  2920. /* FIXME: when software clears the IM field, check the IP field. But do we
  2921. * need to compare the old value and the new value to conclude that
  2922. * software clears the IM field? Or just check if the IM field is zero?
  2923. */
  2924. iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
  2925. trace_vtd_reg_write_iectl(iectl_reg);
  2926. if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
  2927. vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
  2928. vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
  2929. }
  2930. }
  2931. static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
  2932. {
  2933. IntelIOMMUState *s = opaque;
  2934. uint64_t val;
  2935. trace_vtd_reg_read(addr, size);
  2936. if (addr + size > DMAR_REG_SIZE) {
  2937. error_report_once("%s: MMIO over range: addr=0x%" PRIx64
  2938. " size=0x%x", __func__, addr, size);
  2939. return (uint64_t)-1;
  2940. }
  2941. switch (addr) {
  2942. /* Root Table Address Register, 64-bit */
  2943. case DMAR_RTADDR_REG:
  2944. val = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
  2945. if (size == 4) {
  2946. val = val & ((1ULL << 32) - 1);
  2947. }
  2948. break;
  2949. case DMAR_RTADDR_REG_HI:
  2950. assert(size == 4);
  2951. val = vtd_get_quad_raw(s, DMAR_RTADDR_REG) >> 32;
  2952. break;
  2953. /* Invalidation Queue Address Register, 64-bit */
  2954. case DMAR_IQA_REG:
  2955. val = s->iq |
  2956. (vtd_get_quad(s, DMAR_IQA_REG) &
  2957. (VTD_IQA_QS | VTD_IQA_DW_MASK));
  2958. if (size == 4) {
  2959. val = val & ((1ULL << 32) - 1);
  2960. }
  2961. break;
  2962. case DMAR_IQA_REG_HI:
  2963. assert(size == 4);
  2964. val = s->iq >> 32;
  2965. break;
  2966. default:
  2967. if (size == 4) {
  2968. val = vtd_get_long(s, addr);
  2969. } else {
  2970. val = vtd_get_quad(s, addr);
  2971. }
  2972. }
  2973. return val;
  2974. }
  2975. static void vtd_mem_write(void *opaque, hwaddr addr,
  2976. uint64_t val, unsigned size)
  2977. {
  2978. IntelIOMMUState *s = opaque;
  2979. trace_vtd_reg_write(addr, size, val);
  2980. if (addr + size > DMAR_REG_SIZE) {
  2981. error_report_once("%s: MMIO over range: addr=0x%" PRIx64
  2982. " size=0x%x", __func__, addr, size);
  2983. return;
  2984. }
  2985. switch (addr) {
  2986. /* Global Command Register, 32-bit */
  2987. case DMAR_GCMD_REG:
  2988. vtd_set_long(s, addr, val);
  2989. vtd_handle_gcmd_write(s);
  2990. break;
  2991. /* Context Command Register, 64-bit */
  2992. case DMAR_CCMD_REG:
  2993. if (size == 4) {
  2994. vtd_set_long(s, addr, val);
  2995. } else {
  2996. vtd_set_quad(s, addr, val);
  2997. vtd_handle_ccmd_write(s);
  2998. }
  2999. break;
  3000. case DMAR_CCMD_REG_HI:
  3001. assert(size == 4);
  3002. vtd_set_long(s, addr, val);
  3003. vtd_handle_ccmd_write(s);
  3004. break;
  3005. /* IOTLB Invalidation Register, 64-bit */
  3006. case DMAR_IOTLB_REG:
  3007. if (size == 4) {
  3008. vtd_set_long(s, addr, val);
  3009. } else {
  3010. vtd_set_quad(s, addr, val);
  3011. vtd_handle_iotlb_write(s);
  3012. }
  3013. break;
  3014. case DMAR_IOTLB_REG_HI:
  3015. assert(size == 4);
  3016. vtd_set_long(s, addr, val);
  3017. vtd_handle_iotlb_write(s);
  3018. break;
  3019. /* Invalidate Address Register, 64-bit */
  3020. case DMAR_IVA_REG:
  3021. if (size == 4) {
  3022. vtd_set_long(s, addr, val);
  3023. } else {
  3024. vtd_set_quad(s, addr, val);
  3025. }
  3026. break;
  3027. case DMAR_IVA_REG_HI:
  3028. assert(size == 4);
  3029. vtd_set_long(s, addr, val);
  3030. break;
  3031. /* Fault Status Register, 32-bit */
  3032. case DMAR_FSTS_REG:
  3033. assert(size == 4);
  3034. vtd_set_long(s, addr, val);
  3035. vtd_handle_fsts_write(s);
  3036. break;
  3037. /* Fault Event Control Register, 32-bit */
  3038. case DMAR_FECTL_REG:
  3039. assert(size == 4);
  3040. vtd_set_long(s, addr, val);
  3041. vtd_handle_fectl_write(s);
  3042. break;
  3043. /* Fault Event Data Register, 32-bit */
  3044. case DMAR_FEDATA_REG:
  3045. assert(size == 4);
  3046. vtd_set_long(s, addr, val);
  3047. break;
  3048. /* Fault Event Address Register, 32-bit */
  3049. case DMAR_FEADDR_REG:
  3050. if (size == 4) {
  3051. vtd_set_long(s, addr, val);
  3052. } else {
  3053. /*
  3054. * While the register is 32-bit only, some guests (Xen...) write to
  3055. * it with 64-bit.
  3056. */
  3057. vtd_set_quad(s, addr, val);
  3058. }
  3059. break;
  3060. /* Fault Event Upper Address Register, 32-bit */
  3061. case DMAR_FEUADDR_REG:
  3062. assert(size == 4);
  3063. vtd_set_long(s, addr, val);
  3064. break;
  3065. /* Protected Memory Enable Register, 32-bit */
  3066. case DMAR_PMEN_REG:
  3067. assert(size == 4);
  3068. vtd_set_long(s, addr, val);
  3069. break;
  3070. /* Root Table Address Register, 64-bit */
  3071. case DMAR_RTADDR_REG:
  3072. if (size == 4) {
  3073. vtd_set_long(s, addr, val);
  3074. } else {
  3075. vtd_set_quad(s, addr, val);
  3076. }
  3077. break;
  3078. case DMAR_RTADDR_REG_HI:
  3079. assert(size == 4);
  3080. vtd_set_long(s, addr, val);
  3081. break;
  3082. /* Invalidation Queue Tail Register, 64-bit */
  3083. case DMAR_IQT_REG:
  3084. if (size == 4) {
  3085. vtd_set_long(s, addr, val);
  3086. } else {
  3087. vtd_set_quad(s, addr, val);
  3088. }
  3089. vtd_handle_iqt_write(s);
  3090. break;
  3091. case DMAR_IQT_REG_HI:
  3092. assert(size == 4);
  3093. vtd_set_long(s, addr, val);
  3094. /* 19:63 of IQT_REG is RsvdZ, do nothing here */
  3095. break;
  3096. /* Invalidation Queue Address Register, 64-bit */
  3097. case DMAR_IQA_REG:
  3098. if (size == 4) {
  3099. vtd_set_long(s, addr, val);
  3100. } else {
  3101. vtd_set_quad(s, addr, val);
  3102. }
  3103. vtd_update_iq_dw(s);
  3104. break;
  3105. case DMAR_IQA_REG_HI:
  3106. assert(size == 4);
  3107. vtd_set_long(s, addr, val);
  3108. break;
  3109. /* Invalidation Completion Status Register, 32-bit */
  3110. case DMAR_ICS_REG:
  3111. assert(size == 4);
  3112. vtd_set_long(s, addr, val);
  3113. vtd_handle_ics_write(s);
  3114. break;
  3115. /* Invalidation Event Control Register, 32-bit */
  3116. case DMAR_IECTL_REG:
  3117. assert(size == 4);
  3118. vtd_set_long(s, addr, val);
  3119. vtd_handle_iectl_write(s);
  3120. break;
  3121. /* Invalidation Event Data Register, 32-bit */
  3122. case DMAR_IEDATA_REG:
  3123. assert(size == 4);
  3124. vtd_set_long(s, addr, val);
  3125. break;
  3126. /* Invalidation Event Address Register, 32-bit */
  3127. case DMAR_IEADDR_REG:
  3128. assert(size == 4);
  3129. vtd_set_long(s, addr, val);
  3130. break;
  3131. /* Invalidation Event Upper Address Register, 32-bit */
  3132. case DMAR_IEUADDR_REG:
  3133. assert(size == 4);
  3134. vtd_set_long(s, addr, val);
  3135. break;
  3136. /* Fault Recording Registers, 128-bit */
  3137. case DMAR_FRCD_REG_0_0:
  3138. if (size == 4) {
  3139. vtd_set_long(s, addr, val);
  3140. } else {
  3141. vtd_set_quad(s, addr, val);
  3142. }
  3143. break;
  3144. case DMAR_FRCD_REG_0_1:
  3145. assert(size == 4);
  3146. vtd_set_long(s, addr, val);
  3147. break;
  3148. case DMAR_FRCD_REG_0_2:
  3149. if (size == 4) {
  3150. vtd_set_long(s, addr, val);
  3151. } else {
  3152. vtd_set_quad(s, addr, val);
  3153. /* May clear bit 127 (Fault), update PPF */
  3154. vtd_update_fsts_ppf(s);
  3155. }
  3156. break;
  3157. case DMAR_FRCD_REG_0_3:
  3158. assert(size == 4);
  3159. vtd_set_long(s, addr, val);
  3160. /* May clear bit 127 (Fault), update PPF */
  3161. vtd_update_fsts_ppf(s);
  3162. break;
  3163. case DMAR_IRTA_REG:
  3164. if (size == 4) {
  3165. vtd_set_long(s, addr, val);
  3166. } else {
  3167. vtd_set_quad(s, addr, val);
  3168. }
  3169. break;
  3170. case DMAR_IRTA_REG_HI:
  3171. assert(size == 4);
  3172. vtd_set_long(s, addr, val);
  3173. break;
  3174. default:
  3175. if (size == 4) {
  3176. vtd_set_long(s, addr, val);
  3177. } else {
  3178. vtd_set_quad(s, addr, val);
  3179. }
  3180. }
  3181. }
  3182. static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
  3183. IOMMUAccessFlags flag, int iommu_idx)
  3184. {
  3185. VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
  3186. IntelIOMMUState *s = vtd_as->iommu_state;
  3187. IOMMUTLBEntry iotlb = {
  3188. /* We'll fill in the rest later. */
  3189. .target_as = &address_space_memory,
  3190. };
  3191. bool success;
  3192. if (likely(s->dmar_enabled)) {
  3193. success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
  3194. addr, flag & IOMMU_WO, &iotlb);
  3195. } else {
  3196. /* DMAR disabled, passthrough, use 4k-page*/
  3197. iotlb.iova = addr & VTD_PAGE_MASK_4K;
  3198. iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
  3199. iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
  3200. iotlb.perm = IOMMU_RW;
  3201. success = true;
  3202. }
  3203. if (likely(success)) {
  3204. trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
  3205. VTD_PCI_SLOT(vtd_as->devfn),
  3206. VTD_PCI_FUNC(vtd_as->devfn),
  3207. iotlb.iova, iotlb.translated_addr,
  3208. iotlb.addr_mask);
  3209. } else {
  3210. error_report_once("%s: detected translation failure "
  3211. "(dev=%02x:%02x:%02x, iova=0x%" PRIx64 ")",
  3212. __func__, pci_bus_num(vtd_as->bus),
  3213. VTD_PCI_SLOT(vtd_as->devfn),
  3214. VTD_PCI_FUNC(vtd_as->devfn),
  3215. addr);
  3216. }
  3217. return iotlb;
  3218. }
  3219. static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
  3220. IOMMUNotifierFlag old,
  3221. IOMMUNotifierFlag new,
  3222. Error **errp)
  3223. {
  3224. VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
  3225. IntelIOMMUState *s = vtd_as->iommu_state;
  3226. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  3227. /* TODO: add support for VFIO and vhost users */
  3228. if (s->snoop_control) {
  3229. error_setg_errno(errp, ENOTSUP,
  3230. "Snoop Control with vhost or VFIO is not supported");
  3231. return -ENOTSUP;
  3232. }
  3233. if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
  3234. error_setg_errno(errp, ENOTSUP,
  3235. "device %02x.%02x.%x requires caching mode",
  3236. pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
  3237. PCI_FUNC(vtd_as->devfn));
  3238. return -ENOTSUP;
  3239. }
  3240. if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
  3241. error_setg_errno(errp, ENOTSUP,
  3242. "device %02x.%02x.%x requires device IOTLB mode",
  3243. pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
  3244. PCI_FUNC(vtd_as->devfn));
  3245. return -ENOTSUP;
  3246. }
  3247. /* Update per-address-space notifier flags */
  3248. vtd_as->notifier_flags = new;
  3249. if (old == IOMMU_NOTIFIER_NONE) {
  3250. QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
  3251. } else if (new == IOMMU_NOTIFIER_NONE) {
  3252. QLIST_REMOVE(vtd_as, next);
  3253. }
  3254. return 0;
  3255. }
  3256. static int vtd_post_load(void *opaque, int version_id)
  3257. {
  3258. IntelIOMMUState *iommu = opaque;
  3259. /*
  3260. * We don't need to migrate the root_scalable because we can
  3261. * simply do the calculation after the loading is complete. We
  3262. * can actually do similar things with root, dmar_enabled, etc.
  3263. * however since we've had them already so we'd better keep them
  3264. * for compatibility of migration.
  3265. */
  3266. vtd_update_scalable_state(iommu);
  3267. vtd_update_iq_dw(iommu);
  3268. /*
  3269. * Memory regions are dynamically turned on/off depending on
  3270. * context entry configurations from the guest. After migration,
  3271. * we need to make sure the memory regions are still correct.
  3272. */
  3273. vtd_switch_address_space_all(iommu);
  3274. return 0;
  3275. }
  3276. static const VMStateDescription vtd_vmstate = {
  3277. .name = "iommu-intel",
  3278. .version_id = 1,
  3279. .minimum_version_id = 1,
  3280. .priority = MIG_PRI_IOMMU,
  3281. .post_load = vtd_post_load,
  3282. .fields = (const VMStateField[]) {
  3283. VMSTATE_UINT64(root, IntelIOMMUState),
  3284. VMSTATE_UINT64(intr_root, IntelIOMMUState),
  3285. VMSTATE_UINT64(iq, IntelIOMMUState),
  3286. VMSTATE_UINT32(intr_size, IntelIOMMUState),
  3287. VMSTATE_UINT16(iq_head, IntelIOMMUState),
  3288. VMSTATE_UINT16(iq_tail, IntelIOMMUState),
  3289. VMSTATE_UINT16(iq_size, IntelIOMMUState),
  3290. VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
  3291. VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
  3292. VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
  3293. VMSTATE_UNUSED(1), /* bool root_extended is obsolete by VT-d */
  3294. VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
  3295. VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
  3296. VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
  3297. VMSTATE_BOOL(intr_eime, IntelIOMMUState),
  3298. VMSTATE_END_OF_LIST()
  3299. }
  3300. };
  3301. static const MemoryRegionOps vtd_mem_ops = {
  3302. .read = vtd_mem_read,
  3303. .write = vtd_mem_write,
  3304. .endianness = DEVICE_LITTLE_ENDIAN,
  3305. .impl = {
  3306. .min_access_size = 4,
  3307. .max_access_size = 8,
  3308. },
  3309. .valid = {
  3310. .min_access_size = 4,
  3311. .max_access_size = 8,
  3312. },
  3313. };
  3314. static const Property vtd_properties[] = {
  3315. DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
  3316. DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
  3317. ON_OFF_AUTO_AUTO),
  3318. DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
  3319. DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits,
  3320. VTD_HOST_ADDRESS_WIDTH),
  3321. DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
  3322. DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FALSE),
  3323. DEFINE_PROP_BOOL("x-flts", IntelIOMMUState, flts, FALSE),
  3324. DEFINE_PROP_BOOL("snoop-control", IntelIOMMUState, snoop_control, false),
  3325. DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false),
  3326. DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true),
  3327. DEFINE_PROP_BOOL("dma-translation", IntelIOMMUState, dma_translation, true),
  3328. DEFINE_PROP_BOOL("stale-tm", IntelIOMMUState, stale_tm, false),
  3329. DEFINE_PROP_BOOL("fs1gp", IntelIOMMUState, fs1gp, true),
  3330. };
  3331. /* Read IRTE entry with specific index */
  3332. static bool vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
  3333. VTD_IR_TableEntry *entry, uint16_t sid,
  3334. bool do_fault)
  3335. {
  3336. static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
  3337. {0xffff, 0xfffb, 0xfff9, 0xfff8};
  3338. dma_addr_t addr = 0x00;
  3339. uint16_t mask, source_id;
  3340. uint8_t bus, bus_max, bus_min;
  3341. if (index >= iommu->intr_size) {
  3342. error_report_once("%s: index too large: ind=0x%x",
  3343. __func__, index);
  3344. if (do_fault) {
  3345. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_INDEX_OVER, index);
  3346. }
  3347. return false;
  3348. }
  3349. addr = iommu->intr_root + index * sizeof(*entry);
  3350. if (dma_memory_read(&address_space_memory, addr,
  3351. entry, sizeof(*entry), MEMTXATTRS_UNSPECIFIED)) {
  3352. error_report_once("%s: read failed: ind=0x%x addr=0x%" PRIx64,
  3353. __func__, index, addr);
  3354. if (do_fault) {
  3355. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_ROOT_INVAL, index);
  3356. }
  3357. return false;
  3358. }
  3359. entry->data[0] = le64_to_cpu(entry->data[0]);
  3360. entry->data[1] = le64_to_cpu(entry->data[1]);
  3361. trace_vtd_ir_irte_get(index, entry->data[1], entry->data[0]);
  3362. /*
  3363. * The remaining potential fault conditions are "qualified" by the
  3364. * Fault Processing Disable bit in the IRTE. Even "not present".
  3365. * So just clear the do_fault flag if PFD is set, which will
  3366. * prevent faults being raised.
  3367. */
  3368. if (entry->irte.fault_disable) {
  3369. do_fault = false;
  3370. }
  3371. if (!entry->irte.present) {
  3372. error_report_once("%s: detected non-present IRTE "
  3373. "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
  3374. __func__, index, entry->data[1], entry->data[0]);
  3375. if (do_fault) {
  3376. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_ENTRY_P, index);
  3377. }
  3378. return false;
  3379. }
  3380. if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
  3381. entry->irte.__reserved_2) {
  3382. error_report_once("%s: detected non-zero reserved IRTE "
  3383. "(index=%u, high=0x%" PRIx64 ", low=0x%" PRIx64 ")",
  3384. __func__, index, entry->data[1], entry->data[0]);
  3385. if (do_fault) {
  3386. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_IRTE_RSVD, index);
  3387. }
  3388. return false;
  3389. }
  3390. if (sid != X86_IOMMU_SID_INVALID) {
  3391. /* Validate IRTE SID */
  3392. source_id = entry->irte.source_id;
  3393. switch (entry->irte.sid_vtype) {
  3394. case VTD_SVT_NONE:
  3395. break;
  3396. case VTD_SVT_ALL:
  3397. mask = vtd_svt_mask[entry->irte.sid_q];
  3398. if ((source_id & mask) != (sid & mask)) {
  3399. error_report_once("%s: invalid IRTE SID "
  3400. "(index=%u, sid=%u, source_id=%u)",
  3401. __func__, index, sid, source_id);
  3402. if (do_fault) {
  3403. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_SID_ERR, index);
  3404. }
  3405. return false;
  3406. }
  3407. break;
  3408. case VTD_SVT_BUS:
  3409. bus_max = source_id >> 8;
  3410. bus_min = source_id & 0xff;
  3411. bus = sid >> 8;
  3412. if (bus > bus_max || bus < bus_min) {
  3413. error_report_once("%s: invalid SVT_BUS "
  3414. "(index=%u, bus=%u, min=%u, max=%u)",
  3415. __func__, index, bus, bus_min, bus_max);
  3416. if (do_fault) {
  3417. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_SID_ERR, index);
  3418. }
  3419. return false;
  3420. }
  3421. break;
  3422. default:
  3423. error_report_once("%s: detected invalid IRTE SVT "
  3424. "(index=%u, type=%d)", __func__,
  3425. index, entry->irte.sid_vtype);
  3426. /* Take this as verification failure. */
  3427. if (do_fault) {
  3428. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_SID_ERR, index);
  3429. }
  3430. return false;
  3431. }
  3432. }
  3433. return true;
  3434. }
  3435. /* Fetch IRQ information of specific IR index */
  3436. static bool vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
  3437. X86IOMMUIrq *irq, uint16_t sid, bool do_fault)
  3438. {
  3439. VTD_IR_TableEntry irte = {};
  3440. if (!vtd_irte_get(iommu, index, &irte, sid, do_fault)) {
  3441. return false;
  3442. }
  3443. irq->trigger_mode = irte.irte.trigger_mode;
  3444. irq->vector = irte.irte.vector;
  3445. irq->delivery_mode = irte.irte.delivery_mode;
  3446. irq->dest = irte.irte.dest_id;
  3447. if (!iommu->intr_eime) {
  3448. #define VTD_IR_APIC_DEST_MASK (0xff00ULL)
  3449. #define VTD_IR_APIC_DEST_SHIFT (8)
  3450. irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
  3451. VTD_IR_APIC_DEST_SHIFT;
  3452. }
  3453. irq->dest_mode = irte.irte.dest_mode;
  3454. irq->redir_hint = irte.irte.redir_hint;
  3455. trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
  3456. irq->delivery_mode, irq->dest, irq->dest_mode);
  3457. return true;
  3458. }
  3459. /* Interrupt remapping for MSI/MSI-X entry */
  3460. static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
  3461. MSIMessage *origin,
  3462. MSIMessage *translated,
  3463. uint16_t sid, bool do_fault)
  3464. {
  3465. VTD_IR_MSIAddress addr;
  3466. uint16_t index;
  3467. X86IOMMUIrq irq = {};
  3468. assert(origin && translated);
  3469. trace_vtd_ir_remap_msi_req(origin->address, origin->data);
  3470. if (!iommu || !iommu->intr_enabled) {
  3471. memcpy(translated, origin, sizeof(*origin));
  3472. goto out;
  3473. }
  3474. if (origin->address & VTD_MSI_ADDR_HI_MASK) {
  3475. error_report_once("%s: MSI address high 32 bits non-zero detected: "
  3476. "address=0x%" PRIx64, __func__, origin->address);
  3477. if (do_fault) {
  3478. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_RSVD, 0);
  3479. }
  3480. return -EINVAL;
  3481. }
  3482. addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
  3483. if (addr.addr.__head != 0xfee) {
  3484. error_report_once("%s: MSI address low 32 bit invalid: 0x%" PRIx32,
  3485. __func__, addr.data);
  3486. if (do_fault) {
  3487. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_RSVD, 0);
  3488. }
  3489. return -EINVAL;
  3490. }
  3491. /* This is compatible mode. */
  3492. if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
  3493. memcpy(translated, origin, sizeof(*origin));
  3494. goto out;
  3495. }
  3496. index = addr.addr.index_h << 15 | addr.addr.index_l;
  3497. #define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff)
  3498. #define VTD_IR_MSI_DATA_RESERVED (0xffff0000)
  3499. if (addr.addr.sub_valid) {
  3500. /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
  3501. index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
  3502. }
  3503. if (!vtd_remap_irq_get(iommu, index, &irq, sid, do_fault)) {
  3504. return -EINVAL;
  3505. }
  3506. if (addr.addr.sub_valid) {
  3507. trace_vtd_ir_remap_type("MSI");
  3508. if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
  3509. error_report_once("%s: invalid IR MSI "
  3510. "(sid=%u, address=0x%" PRIx64
  3511. ", data=0x%" PRIx32 ")",
  3512. __func__, sid, origin->address, origin->data);
  3513. if (do_fault) {
  3514. vtd_report_ir_fault(iommu, sid, VTD_FR_IR_REQ_RSVD, 0);
  3515. }
  3516. return -EINVAL;
  3517. }
  3518. } else {
  3519. uint8_t vector = origin->data & 0xff;
  3520. uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
  3521. trace_vtd_ir_remap_type("IOAPIC");
  3522. /* IOAPIC entry vector should be aligned with IRTE vector
  3523. * (see vt-d spec 5.1.5.1). */
  3524. if (vector != irq.vector) {
  3525. trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
  3526. }
  3527. /* The Trigger Mode field must match the Trigger Mode in the IRTE.
  3528. * (see vt-d spec 5.1.5.1). */
  3529. if (trigger_mode != irq.trigger_mode) {
  3530. trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
  3531. irq.trigger_mode);
  3532. }
  3533. }
  3534. /*
  3535. * We'd better keep the last two bits, assuming that guest OS
  3536. * might modify it. Keep it does not hurt after all.
  3537. */
  3538. irq.msi_addr_last_bits = addr.addr.__not_care;
  3539. /* Translate X86IOMMUIrq to MSI message */
  3540. x86_iommu_irq_to_msi_message(&irq, translated);
  3541. out:
  3542. trace_vtd_ir_remap_msi(origin->address, origin->data,
  3543. translated->address, translated->data);
  3544. return 0;
  3545. }
  3546. static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
  3547. MSIMessage *dst, uint16_t sid)
  3548. {
  3549. return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
  3550. src, dst, sid, false);
  3551. }
  3552. static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
  3553. uint64_t *data, unsigned size,
  3554. MemTxAttrs attrs)
  3555. {
  3556. return MEMTX_OK;
  3557. }
  3558. static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
  3559. uint64_t value, unsigned size,
  3560. MemTxAttrs attrs)
  3561. {
  3562. int ret = 0;
  3563. MSIMessage from = {}, to = {};
  3564. uint16_t sid = X86_IOMMU_SID_INVALID;
  3565. from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
  3566. from.data = (uint32_t) value;
  3567. if (!attrs.unspecified) {
  3568. /* We have explicit Source ID */
  3569. sid = attrs.requester_id;
  3570. }
  3571. ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid, true);
  3572. if (ret) {
  3573. /* Drop this interrupt */
  3574. return MEMTX_ERROR;
  3575. }
  3576. apic_get_class(NULL)->send_msi(&to);
  3577. return MEMTX_OK;
  3578. }
  3579. static const MemoryRegionOps vtd_mem_ir_ops = {
  3580. .read_with_attrs = vtd_mem_ir_read,
  3581. .write_with_attrs = vtd_mem_ir_write,
  3582. .endianness = DEVICE_LITTLE_ENDIAN,
  3583. .impl = {
  3584. .min_access_size = 4,
  3585. .max_access_size = 4,
  3586. },
  3587. .valid = {
  3588. .min_access_size = 4,
  3589. .max_access_size = 4,
  3590. },
  3591. };
  3592. static void vtd_report_ir_illegal_access(VTDAddressSpace *vtd_as,
  3593. hwaddr addr, bool is_write)
  3594. {
  3595. IntelIOMMUState *s = vtd_as->iommu_state;
  3596. uint8_t bus_n = pci_bus_num(vtd_as->bus);
  3597. uint16_t sid = PCI_BUILD_BDF(bus_n, vtd_as->devfn);
  3598. bool is_fpd_set = false;
  3599. VTDContextEntry ce;
  3600. assert(vtd_as->pasid != PCI_NO_PASID);
  3601. /* Try out best to fetch FPD, we can't do anything more */
  3602. if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
  3603. is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
  3604. if (!is_fpd_set && s->root_scalable) {
  3605. vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid);
  3606. }
  3607. }
  3608. vtd_report_fault(s, VTD_FR_SM_INTERRUPT_ADDR,
  3609. is_fpd_set, sid, addr, is_write,
  3610. true, vtd_as->pasid);
  3611. }
  3612. static MemTxResult vtd_mem_ir_fault_read(void *opaque, hwaddr addr,
  3613. uint64_t *data, unsigned size,
  3614. MemTxAttrs attrs)
  3615. {
  3616. vtd_report_ir_illegal_access(opaque, addr, false);
  3617. return MEMTX_ERROR;
  3618. }
  3619. static MemTxResult vtd_mem_ir_fault_write(void *opaque, hwaddr addr,
  3620. uint64_t value, unsigned size,
  3621. MemTxAttrs attrs)
  3622. {
  3623. vtd_report_ir_illegal_access(opaque, addr, true);
  3624. return MEMTX_ERROR;
  3625. }
  3626. static const MemoryRegionOps vtd_mem_ir_fault_ops = {
  3627. .read_with_attrs = vtd_mem_ir_fault_read,
  3628. .write_with_attrs = vtd_mem_ir_fault_write,
  3629. .endianness = DEVICE_LITTLE_ENDIAN,
  3630. .impl = {
  3631. .min_access_size = 1,
  3632. .max_access_size = 8,
  3633. },
  3634. .valid = {
  3635. .min_access_size = 1,
  3636. .max_access_size = 8,
  3637. },
  3638. };
  3639. VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
  3640. int devfn, unsigned int pasid)
  3641. {
  3642. /*
  3643. * We can't simply use sid here since the bus number might not be
  3644. * initialized by the guest.
  3645. */
  3646. struct vtd_as_key key = {
  3647. .bus = bus,
  3648. .devfn = devfn,
  3649. .pasid = pasid,
  3650. };
  3651. VTDAddressSpace *vtd_dev_as;
  3652. char name[128];
  3653. vtd_dev_as = g_hash_table_lookup(s->vtd_address_spaces, &key);
  3654. if (!vtd_dev_as) {
  3655. struct vtd_as_key *new_key = g_malloc(sizeof(*new_key));
  3656. new_key->bus = bus;
  3657. new_key->devfn = devfn;
  3658. new_key->pasid = pasid;
  3659. if (pasid == PCI_NO_PASID) {
  3660. snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn),
  3661. PCI_FUNC(devfn));
  3662. } else {
  3663. snprintf(name, sizeof(name), "vtd-%02x.%x-pasid-%x", PCI_SLOT(devfn),
  3664. PCI_FUNC(devfn), pasid);
  3665. }
  3666. vtd_dev_as = g_new0(VTDAddressSpace, 1);
  3667. vtd_dev_as->bus = bus;
  3668. vtd_dev_as->devfn = (uint8_t)devfn;
  3669. vtd_dev_as->pasid = pasid;
  3670. vtd_dev_as->iommu_state = s;
  3671. vtd_dev_as->context_cache_entry.context_cache_gen = 0;
  3672. vtd_dev_as->iova_tree = iova_tree_new();
  3673. memory_region_init(&vtd_dev_as->root, OBJECT(s), name, UINT64_MAX);
  3674. address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, "vtd-root");
  3675. /*
  3676. * Build the DMAR-disabled container with aliases to the
  3677. * shared MRs. Note that aliasing to a shared memory region
  3678. * could help the memory API to detect same FlatViews so we
  3679. * can have devices to share the same FlatView when DMAR is
  3680. * disabled (either by not providing "intel_iommu=on" or with
  3681. * "iommu=pt"). It will greatly reduce the total number of
  3682. * FlatViews of the system hence VM runs faster.
  3683. */
  3684. memory_region_init_alias(&vtd_dev_as->nodmar, OBJECT(s),
  3685. "vtd-nodmar", &s->mr_nodmar, 0,
  3686. memory_region_size(&s->mr_nodmar));
  3687. /*
  3688. * Build the per-device DMAR-enabled container.
  3689. *
  3690. * TODO: currently we have per-device IOMMU memory region only
  3691. * because we have per-device IOMMU notifiers for devices. If
  3692. * one day we can abstract the IOMMU notifiers out of the
  3693. * memory regions then we can also share the same memory
  3694. * region here just like what we've done above with the nodmar
  3695. * region.
  3696. */
  3697. strcat(name, "-dmar");
  3698. memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
  3699. TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
  3700. name, UINT64_MAX);
  3701. memory_region_init_alias(&vtd_dev_as->iommu_ir, OBJECT(s), "vtd-ir",
  3702. &s->mr_ir, 0, memory_region_size(&s->mr_ir));
  3703. memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->iommu),
  3704. VTD_INTERRUPT_ADDR_FIRST,
  3705. &vtd_dev_as->iommu_ir, 1);
  3706. /*
  3707. * This region is used for catching fault to access interrupt
  3708. * range via passthrough + PASID. See also
  3709. * vtd_switch_address_space(). We can't use alias since we
  3710. * need to know the sid which is valid for MSI who uses
  3711. * bus_master_as (see msi_send_message()).
  3712. */
  3713. memory_region_init_io(&vtd_dev_as->iommu_ir_fault, OBJECT(s),
  3714. &vtd_mem_ir_fault_ops, vtd_dev_as, "vtd-no-ir",
  3715. VTD_INTERRUPT_ADDR_SIZE);
  3716. /*
  3717. * Hook to root since when PT is enabled vtd_dev_as->iommu
  3718. * will be disabled.
  3719. */
  3720. memory_region_add_subregion_overlap(MEMORY_REGION(&vtd_dev_as->root),
  3721. VTD_INTERRUPT_ADDR_FIRST,
  3722. &vtd_dev_as->iommu_ir_fault, 2);
  3723. /*
  3724. * Hook both the containers under the root container, we
  3725. * switch between DMAR & noDMAR by enable/disable
  3726. * corresponding sub-containers
  3727. */
  3728. memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
  3729. MEMORY_REGION(&vtd_dev_as->iommu),
  3730. 0);
  3731. memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
  3732. &vtd_dev_as->nodmar, 0);
  3733. vtd_switch_address_space(vtd_dev_as);
  3734. g_hash_table_insert(s->vtd_address_spaces, new_key, vtd_dev_as);
  3735. }
  3736. return vtd_dev_as;
  3737. }
  3738. static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
  3739. Error **errp)
  3740. {
  3741. HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_GET_CLASS(hiod);
  3742. int ret;
  3743. if (!hiodc->get_cap) {
  3744. error_setg(errp, ".get_cap() not implemented");
  3745. return false;
  3746. }
  3747. /* Common checks */
  3748. ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_AW_BITS, errp);
  3749. if (ret < 0) {
  3750. return false;
  3751. }
  3752. if (s->aw_bits > ret) {
  3753. error_setg(errp, "aw-bits %d > host aw-bits %d", s->aw_bits, ret);
  3754. return false;
  3755. }
  3756. if (!s->flts) {
  3757. /* All checks requested by VTD stage-2 translation pass */
  3758. return true;
  3759. }
  3760. error_setg(errp, "host device is uncompatible with stage-1 translation");
  3761. return false;
  3762. }
  3763. static bool vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
  3764. HostIOMMUDevice *hiod, Error **errp)
  3765. {
  3766. IntelIOMMUState *s = opaque;
  3767. struct vtd_as_key key = {
  3768. .bus = bus,
  3769. .devfn = devfn,
  3770. };
  3771. struct vtd_as_key *new_key;
  3772. assert(hiod);
  3773. vtd_iommu_lock(s);
  3774. if (g_hash_table_lookup(s->vtd_host_iommu_dev, &key)) {
  3775. error_setg(errp, "Host IOMMU device already exist");
  3776. vtd_iommu_unlock(s);
  3777. return false;
  3778. }
  3779. if (!vtd_check_hiod(s, hiod, errp)) {
  3780. vtd_iommu_unlock(s);
  3781. return false;
  3782. }
  3783. new_key = g_malloc(sizeof(*new_key));
  3784. new_key->bus = bus;
  3785. new_key->devfn = devfn;
  3786. object_ref(hiod);
  3787. g_hash_table_insert(s->vtd_host_iommu_dev, new_key, hiod);
  3788. vtd_iommu_unlock(s);
  3789. return true;
  3790. }
  3791. static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)
  3792. {
  3793. IntelIOMMUState *s = opaque;
  3794. struct vtd_as_key key = {
  3795. .bus = bus,
  3796. .devfn = devfn,
  3797. };
  3798. vtd_iommu_lock(s);
  3799. if (!g_hash_table_lookup(s->vtd_host_iommu_dev, &key)) {
  3800. vtd_iommu_unlock(s);
  3801. return;
  3802. }
  3803. g_hash_table_remove(s->vtd_host_iommu_dev, &key);
  3804. vtd_iommu_unlock(s);
  3805. }
  3806. /* Unmap the whole range in the notifier's scope. */
  3807. static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
  3808. {
  3809. hwaddr total, remain;
  3810. hwaddr start = n->start;
  3811. hwaddr end = n->end;
  3812. IntelIOMMUState *s = as->iommu_state;
  3813. DMAMap map;
  3814. /*
  3815. * Note: all the codes in this function has a assumption that IOVA
  3816. * bits are no more than VTD_MGAW bits (which is restricted by
  3817. * VT-d spec), otherwise we need to consider overflow of 64 bits.
  3818. */
  3819. if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) {
  3820. /*
  3821. * Don't need to unmap regions that is bigger than the whole
  3822. * VT-d supported address space size
  3823. */
  3824. end = VTD_ADDRESS_SIZE(s->aw_bits) - 1;
  3825. }
  3826. assert(start <= end);
  3827. total = remain = end - start + 1;
  3828. while (remain >= VTD_PAGE_SIZE) {
  3829. IOMMUTLBEvent event;
  3830. uint64_t mask = dma_aligned_pow2_mask(start, end, s->aw_bits);
  3831. uint64_t size = mask + 1;
  3832. assert(size);
  3833. event.type = IOMMU_NOTIFIER_UNMAP;
  3834. event.entry.iova = start;
  3835. event.entry.addr_mask = mask;
  3836. event.entry.target_as = &address_space_memory;
  3837. event.entry.perm = IOMMU_NONE;
  3838. /* This field is meaningless for unmap */
  3839. event.entry.translated_addr = 0;
  3840. memory_region_notify_iommu_one(n, &event);
  3841. start += size;
  3842. remain -= size;
  3843. }
  3844. assert(!remain);
  3845. trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
  3846. VTD_PCI_SLOT(as->devfn),
  3847. VTD_PCI_FUNC(as->devfn),
  3848. n->start, total);
  3849. map.iova = n->start;
  3850. map.size = total - 1; /* Inclusive */
  3851. iova_tree_remove(as->iova_tree, map);
  3852. }
  3853. static void vtd_address_space_unmap_all(IntelIOMMUState *s)
  3854. {
  3855. VTDAddressSpace *vtd_as;
  3856. IOMMUNotifier *n;
  3857. QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
  3858. IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
  3859. vtd_address_space_unmap(vtd_as, n);
  3860. }
  3861. }
  3862. }
  3863. static void vtd_address_space_refresh_all(IntelIOMMUState *s)
  3864. {
  3865. vtd_address_space_unmap_all(s);
  3866. vtd_switch_address_space_all(s);
  3867. }
  3868. static int vtd_replay_hook(const IOMMUTLBEvent *event, void *private)
  3869. {
  3870. memory_region_notify_iommu_one(private, event);
  3871. return 0;
  3872. }
  3873. static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
  3874. {
  3875. VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
  3876. IntelIOMMUState *s = vtd_as->iommu_state;
  3877. uint8_t bus_n = pci_bus_num(vtd_as->bus);
  3878. VTDContextEntry ce;
  3879. DMAMap map = { .iova = 0, .size = HWADDR_MAX };
  3880. /* replay is protected by BQL, page walk will re-setup it safely */
  3881. iova_tree_remove(vtd_as->iova_tree, map);
  3882. if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
  3883. trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" :
  3884. "legacy mode",
  3885. bus_n, PCI_SLOT(vtd_as->devfn),
  3886. PCI_FUNC(vtd_as->devfn),
  3887. vtd_get_domain_id(s, &ce, vtd_as->pasid),
  3888. ce.hi, ce.lo);
  3889. if (n->notifier_flags & IOMMU_NOTIFIER_MAP) {
  3890. /* This is required only for MAP typed notifiers */
  3891. vtd_page_walk_info info = {
  3892. .hook_fn = vtd_replay_hook,
  3893. .private = (void *)n,
  3894. .notify_unmap = false,
  3895. .aw = s->aw_bits,
  3896. .as = vtd_as,
  3897. .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
  3898. };
  3899. vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
  3900. }
  3901. } else {
  3902. trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
  3903. PCI_FUNC(vtd_as->devfn));
  3904. }
  3905. return;
  3906. }
  3907. static void vtd_cap_init(IntelIOMMUState *s)
  3908. {
  3909. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  3910. s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
  3911. VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
  3912. VTD_CAP_MGAW(s->aw_bits);
  3913. if (s->dma_drain) {
  3914. s->cap |= VTD_CAP_DRAIN;
  3915. }
  3916. if (s->dma_translation) {
  3917. if (s->aw_bits >= VTD_HOST_AW_39BIT) {
  3918. s->cap |= VTD_CAP_SAGAW_39bit;
  3919. }
  3920. if (s->aw_bits >= VTD_HOST_AW_48BIT) {
  3921. s->cap |= VTD_CAP_SAGAW_48bit;
  3922. }
  3923. }
  3924. s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
  3925. if (x86_iommu_ir_supported(x86_iommu)) {
  3926. s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
  3927. if (s->intr_eim == ON_OFF_AUTO_ON) {
  3928. s->ecap |= VTD_ECAP_EIM;
  3929. }
  3930. assert(s->intr_eim != ON_OFF_AUTO_AUTO);
  3931. }
  3932. if (x86_iommu->dt_supported) {
  3933. s->ecap |= VTD_ECAP_DT;
  3934. }
  3935. if (x86_iommu->pt_supported) {
  3936. s->ecap |= VTD_ECAP_PT;
  3937. }
  3938. if (s->caching_mode) {
  3939. s->cap |= VTD_CAP_CM;
  3940. }
  3941. /* TODO: read cap/ecap from host to decide which cap to be exposed. */
  3942. if (s->flts) {
  3943. s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_FLTS;
  3944. if (s->fs1gp) {
  3945. s->cap |= VTD_CAP_FS1GP;
  3946. }
  3947. } else if (s->scalable_mode) {
  3948. s->ecap |= VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS;
  3949. }
  3950. if (s->snoop_control) {
  3951. s->ecap |= VTD_ECAP_SC;
  3952. }
  3953. if (s->pasid) {
  3954. s->ecap |= VTD_ECAP_PASID;
  3955. }
  3956. }
  3957. /*
  3958. * Do the initialization. It will also be called when reset, so pay
  3959. * attention when adding new initialization stuff.
  3960. */
  3961. static void vtd_init(IntelIOMMUState *s)
  3962. {
  3963. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  3964. memset(s->csr, 0, DMAR_REG_SIZE);
  3965. memset(s->wmask, 0, DMAR_REG_SIZE);
  3966. memset(s->w1cmask, 0, DMAR_REG_SIZE);
  3967. memset(s->womask, 0, DMAR_REG_SIZE);
  3968. s->root = 0;
  3969. s->root_scalable = false;
  3970. s->dmar_enabled = false;
  3971. s->intr_enabled = false;
  3972. s->iq_head = 0;
  3973. s->iq_tail = 0;
  3974. s->iq = 0;
  3975. s->iq_size = 0;
  3976. s->qi_enabled = false;
  3977. s->iq_last_desc_type = VTD_INV_DESC_NONE;
  3978. s->iq_dw = false;
  3979. s->next_frcd_reg = 0;
  3980. vtd_cap_init(s);
  3981. /*
  3982. * Rsvd field masks for spte
  3983. */
  3984. vtd_spte_rsvd[0] = ~0ULL;
  3985. vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
  3986. x86_iommu->dt_supported && s->stale_tm);
  3987. vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
  3988. vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
  3989. vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
  3990. vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
  3991. x86_iommu->dt_supported && s->stale_tm);
  3992. vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
  3993. x86_iommu->dt_supported && s->stale_tm);
  3994. /*
  3995. * Rsvd field masks for fpte
  3996. */
  3997. vtd_fpte_rsvd[0] = ~0ULL;
  3998. vtd_fpte_rsvd[1] = VTD_FPTE_PAGE_L1_RSVD_MASK(s->aw_bits);
  3999. vtd_fpte_rsvd[2] = VTD_FPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
  4000. vtd_fpte_rsvd[3] = VTD_FPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
  4001. vtd_fpte_rsvd[4] = VTD_FPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
  4002. vtd_fpte_rsvd_large[2] = VTD_FPTE_LPAGE_L2_RSVD_MASK(s->aw_bits);
  4003. vtd_fpte_rsvd_large[3] = VTD_FPTE_LPAGE_L3_RSVD_MASK(s->aw_bits);
  4004. if (s->scalable_mode || s->snoop_control) {
  4005. vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
  4006. vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
  4007. vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
  4008. }
  4009. vtd_reset_caches(s);
  4010. /* Define registers with default values and bit semantics */
  4011. vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
  4012. vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
  4013. vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
  4014. vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
  4015. vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
  4016. vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
  4017. vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffffc00ULL, 0);
  4018. vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
  4019. vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
  4020. /* Advanced Fault Logging not supported */
  4021. vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
  4022. vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
  4023. vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
  4024. vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
  4025. /* Treated as RsvdZ when EIM in ECAP_REG is not supported
  4026. * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
  4027. */
  4028. vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
  4029. /* Treated as RO for implementations that PLMR and PHMR fields reported
  4030. * as Clear in the CAP_REG.
  4031. * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
  4032. */
  4033. vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
  4034. vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
  4035. vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
  4036. vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff807ULL, 0);
  4037. vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
  4038. vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
  4039. vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
  4040. vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
  4041. /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
  4042. vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
  4043. /* IOTLB registers */
  4044. vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
  4045. vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
  4046. vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
  4047. /* Fault Recording Registers, 128-bit */
  4048. vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
  4049. vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
  4050. /*
  4051. * Interrupt remapping registers.
  4052. */
  4053. vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
  4054. }
  4055. /* Should not reset address_spaces when reset because devices will still use
  4056. * the address space they got at first (won't ask the bus again).
  4057. */
  4058. static void vtd_reset_exit(Object *obj, ResetType type)
  4059. {
  4060. IntelIOMMUState *s = INTEL_IOMMU_DEVICE(obj);
  4061. trace_vtd_reset_exit();
  4062. vtd_init(s);
  4063. vtd_address_space_refresh_all(s);
  4064. }
  4065. static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  4066. {
  4067. IntelIOMMUState *s = opaque;
  4068. VTDAddressSpace *vtd_as;
  4069. assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
  4070. vtd_as = vtd_find_add_as(s, bus, devfn, PCI_NO_PASID);
  4071. return &vtd_as->as;
  4072. }
  4073. static PCIIOMMUOps vtd_iommu_ops = {
  4074. .get_address_space = vtd_host_dma_iommu,
  4075. .set_iommu_device = vtd_dev_set_iommu_device,
  4076. .unset_iommu_device = vtd_dev_unset_iommu_device,
  4077. };
  4078. static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
  4079. {
  4080. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  4081. if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu_ir_supported(x86_iommu)) {
  4082. error_setg(errp, "eim=on cannot be selected without intremap=on");
  4083. return false;
  4084. }
  4085. if (s->intr_eim == ON_OFF_AUTO_AUTO) {
  4086. s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
  4087. && x86_iommu_ir_supported(x86_iommu) ?
  4088. ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
  4089. }
  4090. if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
  4091. if (kvm_irqchip_is_split() && !kvm_enable_x2apic()) {
  4092. error_setg(errp, "eim=on requires support on the KVM side"
  4093. "(X2APIC_API, first shipped in v4.7)");
  4094. return false;
  4095. }
  4096. }
  4097. if (!s->scalable_mode && s->flts) {
  4098. error_setg(errp, "x-flts is only available in scalable mode");
  4099. return false;
  4100. }
  4101. if (!s->flts && s->aw_bits != VTD_HOST_AW_39BIT &&
  4102. s->aw_bits != VTD_HOST_AW_48BIT) {
  4103. error_setg(errp, "%s: supported values for aw-bits are: %d, %d",
  4104. s->scalable_mode ? "Scalable mode(flts=off)" : "Legacy mode",
  4105. VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
  4106. return false;
  4107. }
  4108. if (s->flts && s->aw_bits != VTD_HOST_AW_48BIT) {
  4109. error_setg(errp,
  4110. "Scalable mode(flts=on): supported value for aw-bits is: %d",
  4111. VTD_HOST_AW_48BIT);
  4112. return false;
  4113. }
  4114. if (s->scalable_mode && !s->dma_drain) {
  4115. error_setg(errp, "Need to set dma_drain for scalable mode");
  4116. return false;
  4117. }
  4118. if (s->pasid && !s->scalable_mode) {
  4119. error_setg(errp, "Need to set scalable mode for PASID");
  4120. return false;
  4121. }
  4122. return true;
  4123. }
  4124. static int vtd_machine_done_notify_one(Object *child, void *unused)
  4125. {
  4126. IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
  4127. /*
  4128. * We hard-coded here because vfio-pci is the only special case
  4129. * here. Let's be more elegant in the future when we can, but so
  4130. * far there seems to be no better way.
  4131. */
  4132. if (object_dynamic_cast(child, "vfio-pci") && !iommu->caching_mode) {
  4133. vtd_panic_require_caching_mode();
  4134. }
  4135. return 0;
  4136. }
  4137. static void vtd_machine_done_hook(Notifier *notifier, void *unused)
  4138. {
  4139. object_child_foreach_recursive(object_get_root(),
  4140. vtd_machine_done_notify_one, NULL);
  4141. }
  4142. static Notifier vtd_machine_done_notify = {
  4143. .notify = vtd_machine_done_hook,
  4144. };
  4145. static void vtd_realize(DeviceState *dev, Error **errp)
  4146. {
  4147. MachineState *ms = MACHINE(qdev_get_machine());
  4148. PCMachineState *pcms = PC_MACHINE(ms);
  4149. X86MachineState *x86ms = X86_MACHINE(ms);
  4150. PCIBus *bus = pcms->pcibus;
  4151. IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
  4152. X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
  4153. if (s->pasid && x86_iommu->dt_supported) {
  4154. /*
  4155. * PASID-based-Device-TLB Invalidate Descriptor is not
  4156. * implemented and it requires support from vhost layer which
  4157. * needs to be implemented in the future.
  4158. */
  4159. error_setg(errp, "PASID based device IOTLB is not supported");
  4160. return;
  4161. }
  4162. if (!vtd_decide_config(s, errp)) {
  4163. return;
  4164. }
  4165. QLIST_INIT(&s->vtd_as_with_notifiers);
  4166. qemu_mutex_init(&s->iommu_lock);
  4167. memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
  4168. "intel_iommu", DMAR_REG_SIZE);
  4169. memory_region_add_subregion(get_system_memory(),
  4170. Q35_HOST_BRIDGE_IOMMU_ADDR, &s->csrmem);
  4171. /* Create the shared memory regions by all devices */
  4172. memory_region_init(&s->mr_nodmar, OBJECT(s), "vtd-nodmar",
  4173. UINT64_MAX);
  4174. memory_region_init_io(&s->mr_ir, OBJECT(s), &vtd_mem_ir_ops,
  4175. s, "vtd-ir", VTD_INTERRUPT_ADDR_SIZE);
  4176. memory_region_init_alias(&s->mr_sys_alias, OBJECT(s),
  4177. "vtd-sys-alias", get_system_memory(), 0,
  4178. memory_region_size(get_system_memory()));
  4179. memory_region_add_subregion_overlap(&s->mr_nodmar, 0,
  4180. &s->mr_sys_alias, 0);
  4181. memory_region_add_subregion_overlap(&s->mr_nodmar,
  4182. VTD_INTERRUPT_ADDR_FIRST,
  4183. &s->mr_ir, 1);
  4184. /* No corresponding destroy */
  4185. s->iotlb = g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal,
  4186. g_free, g_free);
  4187. s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
  4188. g_free, g_free);
  4189. s->vtd_host_iommu_dev = g_hash_table_new_full(vtd_hiod_hash, vtd_hiod_equal,
  4190. g_free, vtd_hiod_destroy);
  4191. vtd_init(s);
  4192. pci_setup_iommu(bus, &vtd_iommu_ops, dev);
  4193. /* Pseudo address space under root PCI bus. */
  4194. x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
  4195. qemu_add_machine_init_done_notifier(&vtd_machine_done_notify);
  4196. }
  4197. static void vtd_class_init(ObjectClass *klass, void *data)
  4198. {
  4199. DeviceClass *dc = DEVICE_CLASS(klass);
  4200. X86IOMMUClass *x86_class = X86_IOMMU_DEVICE_CLASS(klass);
  4201. ResettableClass *rc = RESETTABLE_CLASS(klass);
  4202. /*
  4203. * Use 'exit' reset phase to make sure all DMA requests
  4204. * have been quiesced during 'enter' or 'hold' phase
  4205. */
  4206. rc->phases.exit = vtd_reset_exit;
  4207. dc->vmsd = &vtd_vmstate;
  4208. device_class_set_props(dc, vtd_properties);
  4209. dc->hotpluggable = false;
  4210. x86_class->realize = vtd_realize;
  4211. x86_class->int_remap = vtd_int_remap;
  4212. /* Supported by the pc-q35-* machine types */
  4213. dc->user_creatable = true;
  4214. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  4215. dc->desc = "Intel IOMMU (VT-d) DMA Remapping device";
  4216. }
  4217. static const TypeInfo vtd_info = {
  4218. .name = TYPE_INTEL_IOMMU_DEVICE,
  4219. .parent = TYPE_X86_IOMMU_DEVICE,
  4220. .instance_size = sizeof(IntelIOMMUState),
  4221. .class_init = vtd_class_init,
  4222. };
  4223. static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
  4224. void *data)
  4225. {
  4226. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  4227. imrc->translate = vtd_iommu_translate;
  4228. imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
  4229. imrc->replay = vtd_iommu_replay;
  4230. }
  4231. static const TypeInfo vtd_iommu_memory_region_info = {
  4232. .parent = TYPE_IOMMU_MEMORY_REGION,
  4233. .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
  4234. .class_init = vtd_iommu_memory_region_class_init,
  4235. };
  4236. static void vtd_register_types(void)
  4237. {
  4238. type_register_static(&vtd_info);
  4239. type_register_static(&vtd_iommu_memory_region_info);
  4240. }
  4241. type_init(vtd_register_types)