init.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <jroedel@suse.de>
  5. * Leo Duran <leo.duran@amd.com>
  6. */
  7. #define pr_fmt(fmt) "AMD-Vi: " fmt
  8. #define dev_fmt(fmt) pr_fmt(fmt)
  9. #include <linux/pci.h>
  10. #include <linux/acpi.h>
  11. #include <linux/list.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/syscore_ops.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/msi.h>
  16. #include <linux/irq.h>
  17. #include <linux/amd-iommu.h>
  18. #include <linux/export.h>
  19. #include <linux/kmemleak.h>
  20. #include <linux/cc_platform.h>
  21. #include <linux/iopoll.h>
  22. #include <asm/pci-direct.h>
  23. #include <asm/iommu.h>
  24. #include <asm/apic.h>
  25. #include <asm/gart.h>
  26. #include <asm/x86_init.h>
  27. #include <asm/io_apic.h>
  28. #include <asm/irq_remapping.h>
  29. #include <asm/set_memory.h>
  30. #include <asm/sev.h>
  31. #include <linux/crash_dump.h>
  32. #include "amd_iommu.h"
  33. #include "../irq_remapping.h"
  34. #include "../iommu-pages.h"
  35. /*
  36. * definitions for the ACPI scanning code
  37. */
  38. #define IVRS_HEADER_LENGTH 48
  39. #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
  40. #define ACPI_IVMD_TYPE_ALL 0x20
  41. #define ACPI_IVMD_TYPE 0x21
  42. #define ACPI_IVMD_TYPE_RANGE 0x22
  43. #define IVHD_DEV_ALL 0x01
  44. #define IVHD_DEV_SELECT 0x02
  45. #define IVHD_DEV_SELECT_RANGE_START 0x03
  46. #define IVHD_DEV_RANGE_END 0x04
  47. #define IVHD_DEV_ALIAS 0x42
  48. #define IVHD_DEV_ALIAS_RANGE 0x43
  49. #define IVHD_DEV_EXT_SELECT 0x46
  50. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  51. #define IVHD_DEV_SPECIAL 0x48
  52. #define IVHD_DEV_ACPI_HID 0xf0
  53. #define UID_NOT_PRESENT 0
  54. #define UID_IS_INTEGER 1
  55. #define UID_IS_CHARACTER 2
  56. #define IVHD_SPECIAL_IOAPIC 1
  57. #define IVHD_SPECIAL_HPET 2
  58. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  59. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  60. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  61. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  62. #define IVMD_FLAG_EXCL_RANGE 0x08
  63. #define IVMD_FLAG_IW 0x04
  64. #define IVMD_FLAG_IR 0x02
  65. #define IVMD_FLAG_UNITY_MAP 0x01
  66. #define ACPI_DEVFLAG_INITPASS 0x01
  67. #define ACPI_DEVFLAG_EXTINT 0x02
  68. #define ACPI_DEVFLAG_NMI 0x04
  69. #define ACPI_DEVFLAG_SYSMGT1 0x10
  70. #define ACPI_DEVFLAG_SYSMGT2 0x20
  71. #define ACPI_DEVFLAG_LINT0 0x40
  72. #define ACPI_DEVFLAG_LINT1 0x80
  73. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  74. #define IVRS_GET_SBDF_ID(seg, bus, dev, fn) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \
  75. | ((dev & 0x1f) << 3) | (fn & 0x7))
  76. /*
  77. * ACPI table definitions
  78. *
  79. * These data structures are laid over the table to parse the important values
  80. * out of it.
  81. */
  82. /*
  83. * structure describing one IOMMU in the ACPI table. Typically followed by one
  84. * or more ivhd_entrys.
  85. */
  86. struct ivhd_header {
  87. u8 type;
  88. u8 flags;
  89. u16 length;
  90. u16 devid;
  91. u16 cap_ptr;
  92. u64 mmio_phys;
  93. u16 pci_seg;
  94. u16 info;
  95. u32 efr_attr;
  96. /* Following only valid on IVHD type 11h and 40h */
  97. u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
  98. u64 efr_reg2;
  99. } __attribute__((packed));
  100. /*
  101. * A device entry describing which devices a specific IOMMU translates and
  102. * which requestor ids they use.
  103. */
  104. struct ivhd_entry {
  105. u8 type;
  106. u16 devid;
  107. u8 flags;
  108. struct_group(ext_hid,
  109. u32 ext;
  110. u32 hidh;
  111. );
  112. u64 cid;
  113. u8 uidf;
  114. u8 uidl;
  115. u8 uid;
  116. } __attribute__((packed));
  117. /*
  118. * An AMD IOMMU memory definition structure. It defines things like exclusion
  119. * ranges for devices and regions that should be unity mapped.
  120. */
  121. struct ivmd_header {
  122. u8 type;
  123. u8 flags;
  124. u16 length;
  125. u16 devid;
  126. u16 aux;
  127. u16 pci_seg;
  128. u8 resv[6];
  129. u64 range_start;
  130. u64 range_length;
  131. } __attribute__((packed));
  132. bool amd_iommu_dump;
  133. bool amd_iommu_irq_remap __read_mostly;
  134. enum protection_domain_mode amd_iommu_pgtable = PD_MODE_V1;
  135. /* Host page table level */
  136. u8 amd_iommu_hpt_level;
  137. /* Guest page table level */
  138. int amd_iommu_gpt_level = PAGE_MODE_4_LEVEL;
  139. int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  140. static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  141. static bool amd_iommu_detected;
  142. static bool amd_iommu_disabled __initdata;
  143. static bool amd_iommu_force_enable __initdata;
  144. static bool amd_iommu_irtcachedis;
  145. static int amd_iommu_target_ivhd_type;
  146. /* Global EFR and EFR2 registers */
  147. u64 amd_iommu_efr;
  148. u64 amd_iommu_efr2;
  149. /* Host (v1) page table is not supported*/
  150. bool amd_iommu_hatdis;
  151. /* SNP is enabled on the system? */
  152. bool amd_iommu_snp_en;
  153. EXPORT_SYMBOL(amd_iommu_snp_en);
  154. LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
  155. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the system */
  156. LIST_HEAD(amd_ivhd_dev_flags_list); /* list of all IVHD device entry settings */
  157. /* Number of IOMMUs present in the system */
  158. static int amd_iommus_present;
  159. /* IOMMUs have a non-present cache? */
  160. bool amd_iommu_np_cache __read_mostly;
  161. bool amd_iommu_iotlb_sup __read_mostly = true;
  162. static bool amd_iommu_pc_present __read_mostly;
  163. bool amdr_ivrs_remap_support __read_mostly;
  164. bool amd_iommu_force_isolation __read_mostly;
  165. unsigned long amd_iommu_pgsize_bitmap __ro_after_init = AMD_IOMMU_PGSIZES;
  166. enum iommu_init_state {
  167. IOMMU_START_STATE,
  168. IOMMU_IVRS_DETECTED,
  169. IOMMU_ACPI_FINISHED,
  170. IOMMU_ENABLED,
  171. IOMMU_PCI_INIT,
  172. IOMMU_INTERRUPTS_EN,
  173. IOMMU_INITIALIZED,
  174. IOMMU_NOT_FOUND,
  175. IOMMU_INIT_ERROR,
  176. IOMMU_CMDLINE_DISABLED,
  177. };
  178. /* Early ioapic and hpet maps from kernel command line */
  179. #define EARLY_MAP_SIZE 4
  180. static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
  181. static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
  182. static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
  183. static int __initdata early_ioapic_map_size;
  184. static int __initdata early_hpet_map_size;
  185. static int __initdata early_acpihid_map_size;
  186. static bool __initdata cmdline_maps;
  187. static enum iommu_init_state init_state = IOMMU_START_STATE;
  188. static int amd_iommu_enable_interrupts(void);
  189. static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);
  190. static bool amd_iommu_pre_enabled = true;
  191. static u32 amd_iommu_ivinfo __initdata;
  192. bool translation_pre_enabled(struct amd_iommu *iommu)
  193. {
  194. return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
  195. }
  196. static void clear_translation_pre_enabled(struct amd_iommu *iommu)
  197. {
  198. iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  199. }
  200. static void init_translation_status(struct amd_iommu *iommu)
  201. {
  202. u64 ctrl;
  203. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  204. if (ctrl & (1<<CONTROL_IOMMU_EN))
  205. iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  206. }
  207. int amd_iommu_get_num_iommus(void)
  208. {
  209. return amd_iommus_present;
  210. }
  211. bool amd_iommu_ht_range_ignore(void)
  212. {
  213. return check_feature2(FEATURE_HT_RANGE_IGNORE);
  214. }
  215. /*
  216. * Iterate through all the IOMMUs to get common EFR
  217. * masks among all IOMMUs and warn if found inconsistency.
  218. */
  219. static __init void get_global_efr(void)
  220. {
  221. struct amd_iommu *iommu;
  222. for_each_iommu(iommu) {
  223. u64 tmp = iommu->features;
  224. u64 tmp2 = iommu->features2;
  225. if (list_is_first(&iommu->list, &amd_iommu_list)) {
  226. amd_iommu_efr = tmp;
  227. amd_iommu_efr2 = tmp2;
  228. continue;
  229. }
  230. if (amd_iommu_efr == tmp &&
  231. amd_iommu_efr2 == tmp2)
  232. continue;
  233. pr_err(FW_BUG
  234. "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",
  235. tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,
  236. iommu->index, iommu->pci_seg->id,
  237. PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),
  238. PCI_FUNC(iommu->devid));
  239. amd_iommu_efr &= tmp;
  240. amd_iommu_efr2 &= tmp2;
  241. }
  242. pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
  243. }
  244. /*
  245. * For IVHD type 0x11/0x40, EFR is also available via IVHD.
  246. * Default to IVHD EFR since it is available sooner
  247. * (i.e. before PCI init).
  248. */
  249. static void __init early_iommu_features_init(struct amd_iommu *iommu,
  250. struct ivhd_header *h)
  251. {
  252. if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {
  253. iommu->features = h->efr_reg;
  254. iommu->features2 = h->efr_reg2;
  255. }
  256. if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
  257. amdr_ivrs_remap_support = true;
  258. }
  259. /* Access to l1 and l2 indexed register spaces */
  260. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  261. {
  262. u32 val;
  263. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  264. pci_read_config_dword(iommu->dev, 0xfc, &val);
  265. return val;
  266. }
  267. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  268. {
  269. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  270. pci_write_config_dword(iommu->dev, 0xfc, val);
  271. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  272. }
  273. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  274. {
  275. u32 val;
  276. pci_write_config_dword(iommu->dev, 0xf0, address);
  277. pci_read_config_dword(iommu->dev, 0xf4, &val);
  278. return val;
  279. }
  280. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  281. {
  282. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  283. pci_write_config_dword(iommu->dev, 0xf4, val);
  284. }
  285. /****************************************************************************
  286. *
  287. * AMD IOMMU MMIO register space handling functions
  288. *
  289. * These functions are used to program the IOMMU device registers in
  290. * MMIO space required for that driver.
  291. *
  292. ****************************************************************************/
  293. /*
  294. * This function set the exclusion range in the IOMMU. DMA accesses to the
  295. * exclusion range are passed through untranslated
  296. */
  297. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  298. {
  299. u64 start = iommu->exclusion_start & PAGE_MASK;
  300. u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
  301. u64 entry;
  302. if (!iommu->exclusion_start)
  303. return;
  304. entry = start | MMIO_EXCL_ENABLE_MASK;
  305. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  306. &entry, sizeof(entry));
  307. entry = limit;
  308. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  309. &entry, sizeof(entry));
  310. }
  311. static void iommu_set_cwwb_range(struct amd_iommu *iommu)
  312. {
  313. u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
  314. u64 entry = start & PM_ADDR_MASK;
  315. if (!check_feature(FEATURE_SNP))
  316. return;
  317. /* Note:
  318. * Re-purpose Exclusion base/limit registers for Completion wait
  319. * write-back base/limit.
  320. */
  321. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  322. &entry, sizeof(entry));
  323. /* Note:
  324. * Default to 4 Kbytes, which can be specified by setting base
  325. * address equal to the limit address.
  326. */
  327. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  328. &entry, sizeof(entry));
  329. }
  330. /* Programs the physical address of the device table into the IOMMU hardware */
  331. static void iommu_set_device_table(struct amd_iommu *iommu)
  332. {
  333. u64 entry;
  334. u32 dev_table_size = iommu->pci_seg->dev_table_size;
  335. void *dev_table = (void *)get_dev_table(iommu);
  336. BUG_ON(iommu->mmio_base == NULL);
  337. if (is_kdump_kernel())
  338. return;
  339. entry = iommu_virt_to_phys(dev_table);
  340. entry |= (dev_table_size >> 12) - 1;
  341. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  342. &entry, sizeof(entry));
  343. }
  344. static void iommu_feature_set(struct amd_iommu *iommu, u64 val, u64 mask, u8 shift)
  345. {
  346. u64 ctrl;
  347. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  348. mask <<= shift;
  349. ctrl &= ~mask;
  350. ctrl |= (val << shift) & mask;
  351. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  352. }
  353. /* Generic functions to enable/disable certain features of the IOMMU. */
  354. void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  355. {
  356. iommu_feature_set(iommu, 1ULL, 1ULL, bit);
  357. }
  358. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  359. {
  360. iommu_feature_set(iommu, 0ULL, 1ULL, bit);
  361. }
  362. /* Function to enable the hardware */
  363. static void iommu_enable(struct amd_iommu *iommu)
  364. {
  365. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  366. }
  367. static void iommu_disable(struct amd_iommu *iommu)
  368. {
  369. if (!iommu->mmio_base)
  370. return;
  371. /* Disable command buffer */
  372. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  373. /* Disable event logging and event interrupts */
  374. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  375. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  376. /* Disable IOMMU GA_LOG */
  377. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  378. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  379. /* Disable IOMMU PPR logging */
  380. iommu_feature_disable(iommu, CONTROL_PPRLOG_EN);
  381. iommu_feature_disable(iommu, CONTROL_PPRINT_EN);
  382. /* Disable IOMMU hardware itself */
  383. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  384. /* Clear IRTE cache disabling bit */
  385. iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
  386. }
  387. /*
  388. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  389. * the system has one.
  390. */
  391. static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
  392. {
  393. if (!request_mem_region(address, end, "amd_iommu")) {
  394. pr_err("Can not reserve memory region %llx-%llx for mmio\n",
  395. address, end);
  396. pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
  397. return NULL;
  398. }
  399. return (u8 __iomem *)ioremap(address, end);
  400. }
  401. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  402. {
  403. if (iommu->mmio_base)
  404. iounmap(iommu->mmio_base);
  405. release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
  406. }
  407. static inline u32 get_ivhd_header_size(struct ivhd_header *h)
  408. {
  409. u32 size = 0;
  410. switch (h->type) {
  411. case 0x10:
  412. size = 24;
  413. break;
  414. case 0x11:
  415. case 0x40:
  416. size = 40;
  417. break;
  418. }
  419. return size;
  420. }
  421. /****************************************************************************
  422. *
  423. * The functions below belong to the first pass of AMD IOMMU ACPI table
  424. * parsing. In this pass we try to find out the highest device id this
  425. * code has to handle. Upon this information the size of the shared data
  426. * structures is determined later.
  427. *
  428. ****************************************************************************/
  429. /*
  430. * This function calculates the length of a given IVHD entry
  431. */
  432. static inline int ivhd_entry_length(u8 *ivhd)
  433. {
  434. u32 type = ((struct ivhd_entry *)ivhd)->type;
  435. if (type < 0x80) {
  436. return 0x04 << (*ivhd >> 6);
  437. } else if (type == IVHD_DEV_ACPI_HID) {
  438. /* For ACPI_HID, offset 21 is uid len */
  439. return *((u8 *)ivhd + 21) + 22;
  440. }
  441. return 0;
  442. }
  443. /*
  444. * After reading the highest device id from the IOMMU PCI capability header
  445. * this function looks if there is a higher device id defined in the ACPI table
  446. */
  447. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  448. {
  449. u8 *p = (void *)h, *end = (void *)h;
  450. struct ivhd_entry *dev;
  451. int last_devid = -EINVAL;
  452. u32 ivhd_size = get_ivhd_header_size(h);
  453. if (!ivhd_size) {
  454. pr_err("Unsupported IVHD type %#x\n", h->type);
  455. return -EINVAL;
  456. }
  457. p += ivhd_size;
  458. end += h->length;
  459. while (p < end) {
  460. dev = (struct ivhd_entry *)p;
  461. switch (dev->type) {
  462. case IVHD_DEV_ALL:
  463. /* Use maximum BDF value for DEV_ALL */
  464. return 0xffff;
  465. case IVHD_DEV_SELECT:
  466. case IVHD_DEV_RANGE_END:
  467. case IVHD_DEV_ALIAS:
  468. case IVHD_DEV_EXT_SELECT:
  469. /* all the above subfield types refer to device ids */
  470. if (dev->devid > last_devid)
  471. last_devid = dev->devid;
  472. break;
  473. default:
  474. break;
  475. }
  476. p += ivhd_entry_length(p);
  477. }
  478. WARN_ON(p != end);
  479. return last_devid;
  480. }
  481. static int __init check_ivrs_checksum(struct acpi_table_header *table)
  482. {
  483. int i;
  484. u8 checksum = 0, *p = (u8 *)table;
  485. for (i = 0; i < table->length; ++i)
  486. checksum += p[i];
  487. if (checksum != 0) {
  488. /* ACPI table corrupt */
  489. pr_err(FW_BUG "IVRS invalid checksum\n");
  490. return -ENODEV;
  491. }
  492. return 0;
  493. }
  494. /*
  495. * Iterate over all IVHD entries in the ACPI table and find the highest device
  496. * id which we need to handle. This is the first of three functions which parse
  497. * the ACPI table. So we check the checksum here.
  498. */
  499. static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)
  500. {
  501. u8 *p = (u8 *)table, *end = (u8 *)table;
  502. struct ivhd_header *h;
  503. int last_devid, last_bdf = 0;
  504. p += IVRS_HEADER_LENGTH;
  505. end += table->length;
  506. while (p < end) {
  507. h = (struct ivhd_header *)p;
  508. if (h->pci_seg == pci_seg &&
  509. h->type == amd_iommu_target_ivhd_type) {
  510. last_devid = find_last_devid_from_ivhd(h);
  511. if (last_devid < 0)
  512. return -EINVAL;
  513. if (last_devid > last_bdf)
  514. last_bdf = last_devid;
  515. }
  516. p += h->length;
  517. }
  518. WARN_ON(p != end);
  519. return last_bdf;
  520. }
  521. /****************************************************************************
  522. *
  523. * The following functions belong to the code path which parses the ACPI table
  524. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  525. * data structures, initialize the per PCI segment device/alias/rlookup table
  526. * and also basically initialize the hardware.
  527. *
  528. ****************************************************************************/
  529. /* Allocate per PCI segment device table */
  530. static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)
  531. {
  532. pci_seg->dev_table = iommu_alloc_pages_sz(GFP_KERNEL | GFP_DMA32,
  533. pci_seg->dev_table_size);
  534. if (!pci_seg->dev_table)
  535. return -ENOMEM;
  536. return 0;
  537. }
  538. static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
  539. {
  540. if (is_kdump_kernel())
  541. memunmap((void *)pci_seg->dev_table);
  542. else
  543. iommu_free_pages(pci_seg->dev_table);
  544. pci_seg->dev_table = NULL;
  545. }
  546. /* Allocate per PCI segment IOMMU rlookup table. */
  547. static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
  548. {
  549. pci_seg->rlookup_table = kvzalloc_objs(*pci_seg->rlookup_table,
  550. pci_seg->last_bdf + 1);
  551. if (pci_seg->rlookup_table == NULL)
  552. return -ENOMEM;
  553. return 0;
  554. }
  555. static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
  556. {
  557. kvfree(pci_seg->rlookup_table);
  558. pci_seg->rlookup_table = NULL;
  559. }
  560. static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
  561. {
  562. pci_seg->irq_lookup_table = kvzalloc_objs(*pci_seg->irq_lookup_table,
  563. pci_seg->last_bdf + 1);
  564. if (pci_seg->irq_lookup_table == NULL)
  565. return -ENOMEM;
  566. return 0;
  567. }
  568. static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
  569. {
  570. kvfree(pci_seg->irq_lookup_table);
  571. pci_seg->irq_lookup_table = NULL;
  572. }
  573. static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
  574. {
  575. int i;
  576. pci_seg->alias_table = kvmalloc_objs(*pci_seg->alias_table,
  577. pci_seg->last_bdf + 1);
  578. if (!pci_seg->alias_table)
  579. return -ENOMEM;
  580. /*
  581. * let all alias entries point to itself
  582. */
  583. for (i = 0; i <= pci_seg->last_bdf; ++i)
  584. pci_seg->alias_table[i] = i;
  585. return 0;
  586. }
  587. static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
  588. {
  589. kvfree(pci_seg->alias_table);
  590. pci_seg->alias_table = NULL;
  591. }
  592. static inline void *iommu_memremap(unsigned long paddr, size_t size)
  593. {
  594. phys_addr_t phys;
  595. if (!paddr)
  596. return NULL;
  597. /*
  598. * Obtain true physical address in kdump kernel when SME is enabled.
  599. * Currently, previous kernel with SME enabled and kdump kernel
  600. * with SME support disabled is not supported.
  601. */
  602. phys = __sme_clr(paddr);
  603. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  604. return (__force void *)ioremap_encrypted(phys, size);
  605. else
  606. return memremap(phys, size, MEMREMAP_WB);
  607. }
  608. /*
  609. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  610. * write commands to that buffer later and the IOMMU will execute them
  611. * asynchronously
  612. */
  613. static int __init alloc_command_buffer(struct amd_iommu *iommu)
  614. {
  615. iommu->cmd_buf = iommu_alloc_pages_sz(GFP_KERNEL, CMD_BUFFER_SIZE);
  616. return iommu->cmd_buf ? 0 : -ENOMEM;
  617. }
  618. /*
  619. * Interrupt handler has processed all pending events and adjusted head
  620. * and tail pointer. Reset overflow mask and restart logging again.
  621. */
  622. void amd_iommu_restart_log(struct amd_iommu *iommu, const char *evt_type,
  623. u8 cntrl_intr, u8 cntrl_log,
  624. u32 status_run_mask, u32 status_overflow_mask)
  625. {
  626. u32 status;
  627. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  628. if (status & status_run_mask)
  629. return;
  630. pr_info_ratelimited("IOMMU %s log restarting\n", evt_type);
  631. iommu_feature_disable(iommu, cntrl_log);
  632. iommu_feature_disable(iommu, cntrl_intr);
  633. writel(status_overflow_mask, iommu->mmio_base + MMIO_STATUS_OFFSET);
  634. iommu_feature_enable(iommu, cntrl_intr);
  635. iommu_feature_enable(iommu, cntrl_log);
  636. }
  637. /*
  638. * This function restarts event logging in case the IOMMU experienced
  639. * an event log buffer overflow.
  640. */
  641. void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
  642. {
  643. amd_iommu_restart_log(iommu, "Event", CONTROL_EVT_INT_EN,
  644. CONTROL_EVT_LOG_EN, MMIO_STATUS_EVT_RUN_MASK,
  645. MMIO_STATUS_EVT_OVERFLOW_MASK);
  646. }
  647. /*
  648. * This function restarts event logging in case the IOMMU experienced
  649. * GA log overflow.
  650. */
  651. void amd_iommu_restart_ga_log(struct amd_iommu *iommu)
  652. {
  653. amd_iommu_restart_log(iommu, "GA", CONTROL_GAINT_EN,
  654. CONTROL_GALOG_EN, MMIO_STATUS_GALOG_RUN_MASK,
  655. MMIO_STATUS_GALOG_OVERFLOW_MASK);
  656. }
  657. /*
  658. * This function resets the command buffer if the IOMMU stopped fetching
  659. * commands from it.
  660. */
  661. static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  662. {
  663. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  664. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  665. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  666. iommu->cmd_buf_head = 0;
  667. iommu->cmd_buf_tail = 0;
  668. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  669. }
  670. /*
  671. * This function writes the command buffer address to the hardware and
  672. * enables it.
  673. */
  674. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  675. {
  676. u64 entry;
  677. BUG_ON(iommu->cmd_buf == NULL);
  678. if (!is_kdump_kernel()) {
  679. /*
  680. * Command buffer is re-used for kdump kernel and setting
  681. * of MMIO register is not required.
  682. */
  683. entry = iommu_virt_to_phys(iommu->cmd_buf);
  684. entry |= MMIO_CMD_SIZE_512;
  685. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  686. &entry, sizeof(entry));
  687. }
  688. amd_iommu_reset_cmd_buffer(iommu);
  689. }
  690. /*
  691. * This function disables the command buffer
  692. */
  693. static void iommu_disable_command_buffer(struct amd_iommu *iommu)
  694. {
  695. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  696. }
  697. static void __init free_command_buffer(struct amd_iommu *iommu)
  698. {
  699. iommu_free_pages(iommu->cmd_buf);
  700. }
  701. void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu, gfp_t gfp,
  702. size_t size)
  703. {
  704. void *buf;
  705. size = PAGE_ALIGN(size);
  706. buf = iommu_alloc_pages_sz(gfp, size);
  707. if (!buf)
  708. return NULL;
  709. if (check_feature(FEATURE_SNP) &&
  710. set_memory_4k((unsigned long)buf, size / PAGE_SIZE)) {
  711. iommu_free_pages(buf);
  712. return NULL;
  713. }
  714. return buf;
  715. }
  716. /* allocates the memory where the IOMMU will log its events to */
  717. static int __init alloc_event_buffer(struct amd_iommu *iommu)
  718. {
  719. iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL,
  720. EVT_BUFFER_SIZE);
  721. return iommu->evt_buf ? 0 : -ENOMEM;
  722. }
  723. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  724. {
  725. u64 entry;
  726. BUG_ON(iommu->evt_buf == NULL);
  727. if (!is_kdump_kernel()) {
  728. /*
  729. * Event buffer is re-used for kdump kernel and setting
  730. * of MMIO register is not required.
  731. */
  732. entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  733. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  734. &entry, sizeof(entry));
  735. }
  736. /* set head and tail to zero manually */
  737. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  738. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  739. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  740. }
  741. /*
  742. * This function disables the event log buffer
  743. */
  744. static void iommu_disable_event_buffer(struct amd_iommu *iommu)
  745. {
  746. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  747. }
  748. static void __init free_event_buffer(struct amd_iommu *iommu)
  749. {
  750. iommu_free_pages(iommu->evt_buf);
  751. }
  752. static void free_ga_log(struct amd_iommu *iommu)
  753. {
  754. #ifdef CONFIG_IRQ_REMAP
  755. iommu_free_pages(iommu->ga_log);
  756. iommu_free_pages(iommu->ga_log_tail);
  757. #endif
  758. }
  759. #ifdef CONFIG_IRQ_REMAP
  760. static int iommu_ga_log_enable(struct amd_iommu *iommu)
  761. {
  762. u32 status, i;
  763. u64 entry;
  764. if (!iommu->ga_log)
  765. return -EINVAL;
  766. entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
  767. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
  768. &entry, sizeof(entry));
  769. entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
  770. (BIT_ULL(52)-1)) & ~7ULL;
  771. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
  772. &entry, sizeof(entry));
  773. writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  774. writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  775. iommu_feature_enable(iommu, CONTROL_GAINT_EN);
  776. iommu_feature_enable(iommu, CONTROL_GALOG_EN);
  777. for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {
  778. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  779. if (status & (MMIO_STATUS_GALOG_RUN_MASK))
  780. break;
  781. udelay(10);
  782. }
  783. if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))
  784. return -EINVAL;
  785. return 0;
  786. }
  787. static int iommu_init_ga_log(struct amd_iommu *iommu)
  788. {
  789. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  790. return 0;
  791. iommu->ga_log = iommu_alloc_pages_sz(GFP_KERNEL, GA_LOG_SIZE);
  792. if (!iommu->ga_log)
  793. goto err_out;
  794. iommu->ga_log_tail = iommu_alloc_pages_sz(GFP_KERNEL, 8);
  795. if (!iommu->ga_log_tail)
  796. goto err_out;
  797. return 0;
  798. err_out:
  799. free_ga_log(iommu);
  800. return -EINVAL;
  801. }
  802. #endif /* CONFIG_IRQ_REMAP */
  803. static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
  804. {
  805. iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL, 1);
  806. if (!iommu->cmd_sem)
  807. return -ENOMEM;
  808. iommu->cmd_sem_paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
  809. return 0;
  810. }
  811. static int __init remap_event_buffer(struct amd_iommu *iommu)
  812. {
  813. u64 paddr;
  814. pr_info_once("Re-using event buffer from the previous kernel\n");
  815. paddr = readq(iommu->mmio_base + MMIO_EVT_BUF_OFFSET) & PM_ADDR_MASK;
  816. iommu->evt_buf = iommu_memremap(paddr, EVT_BUFFER_SIZE);
  817. return iommu->evt_buf ? 0 : -ENOMEM;
  818. }
  819. static int __init remap_command_buffer(struct amd_iommu *iommu)
  820. {
  821. u64 paddr;
  822. pr_info_once("Re-using command buffer from the previous kernel\n");
  823. paddr = readq(iommu->mmio_base + MMIO_CMD_BUF_OFFSET) & PM_ADDR_MASK;
  824. iommu->cmd_buf = iommu_memremap(paddr, CMD_BUFFER_SIZE);
  825. return iommu->cmd_buf ? 0 : -ENOMEM;
  826. }
  827. static int __init remap_or_alloc_cwwb_sem(struct amd_iommu *iommu)
  828. {
  829. u64 paddr;
  830. if (check_feature(FEATURE_SNP)) {
  831. /*
  832. * When SNP is enabled, the exclusion base register is used for the
  833. * completion wait buffer (CWB) address. Read and re-use it.
  834. */
  835. pr_info_once("Re-using CWB buffers from the previous kernel\n");
  836. paddr = readq(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET) & PM_ADDR_MASK;
  837. iommu->cmd_sem = iommu_memremap(paddr, PAGE_SIZE);
  838. if (!iommu->cmd_sem)
  839. return -ENOMEM;
  840. iommu->cmd_sem_paddr = paddr;
  841. } else {
  842. return alloc_cwwb_sem(iommu);
  843. }
  844. return 0;
  845. }
  846. static int __init alloc_iommu_buffers(struct amd_iommu *iommu)
  847. {
  848. int ret;
  849. /*
  850. * Reuse/Remap the previous kernel's allocated completion wait
  851. * command and event buffers for kdump boot.
  852. */
  853. if (is_kdump_kernel()) {
  854. ret = remap_or_alloc_cwwb_sem(iommu);
  855. if (ret)
  856. return ret;
  857. ret = remap_command_buffer(iommu);
  858. if (ret)
  859. return ret;
  860. ret = remap_event_buffer(iommu);
  861. if (ret)
  862. return ret;
  863. } else {
  864. ret = alloc_cwwb_sem(iommu);
  865. if (ret)
  866. return ret;
  867. ret = alloc_command_buffer(iommu);
  868. if (ret)
  869. return ret;
  870. ret = alloc_event_buffer(iommu);
  871. if (ret)
  872. return ret;
  873. }
  874. return 0;
  875. }
  876. static void __init free_cwwb_sem(struct amd_iommu *iommu)
  877. {
  878. if (iommu->cmd_sem)
  879. iommu_free_pages((void *)iommu->cmd_sem);
  880. }
  881. static void __init unmap_cwwb_sem(struct amd_iommu *iommu)
  882. {
  883. if (iommu->cmd_sem) {
  884. if (check_feature(FEATURE_SNP))
  885. memunmap((void *)iommu->cmd_sem);
  886. else
  887. iommu_free_pages((void *)iommu->cmd_sem);
  888. }
  889. }
  890. static void __init unmap_command_buffer(struct amd_iommu *iommu)
  891. {
  892. memunmap((void *)iommu->cmd_buf);
  893. }
  894. static void __init unmap_event_buffer(struct amd_iommu *iommu)
  895. {
  896. memunmap(iommu->evt_buf);
  897. }
  898. static void __init free_iommu_buffers(struct amd_iommu *iommu)
  899. {
  900. if (is_kdump_kernel()) {
  901. unmap_cwwb_sem(iommu);
  902. unmap_command_buffer(iommu);
  903. unmap_event_buffer(iommu);
  904. } else {
  905. free_cwwb_sem(iommu);
  906. free_command_buffer(iommu);
  907. free_event_buffer(iommu);
  908. }
  909. }
  910. static void iommu_enable_xt(struct amd_iommu *iommu)
  911. {
  912. #ifdef CONFIG_IRQ_REMAP
  913. /*
  914. * XT mode (32-bit APIC destination ID) requires
  915. * GA mode (128-bit IRTE support) as a prerequisite.
  916. */
  917. if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
  918. amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  919. iommu_feature_enable(iommu, CONTROL_XT_EN);
  920. #endif /* CONFIG_IRQ_REMAP */
  921. }
  922. static void iommu_enable_gt(struct amd_iommu *iommu)
  923. {
  924. if (!check_feature(FEATURE_GT))
  925. return;
  926. iommu_feature_enable(iommu, CONTROL_GT_EN);
  927. /*
  928. * This feature needs to be enabled prior to a call
  929. * to iommu_snp_enable(). Since this function is called
  930. * in early_enable_iommu(), it is safe to enable here.
  931. */
  932. if (check_feature2(FEATURE_GCR3TRPMODE))
  933. iommu_feature_enable(iommu, CONTROL_GCR3TRPMODE);
  934. }
  935. /* sets a specific bit in the device table entry. */
  936. static void set_dte_bit(struct dev_table_entry *dte, u8 bit)
  937. {
  938. int i = (bit >> 6) & 0x03;
  939. int _bit = bit & 0x3f;
  940. dte->data[i] |= (1UL << _bit);
  941. }
  942. static bool __reuse_device_table(struct amd_iommu *iommu)
  943. {
  944. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  945. struct dev_table_entry *old_dev_tbl_entry;
  946. u32 lo, hi, old_devtb_size, devid;
  947. phys_addr_t old_devtb_phys;
  948. u16 dom_id;
  949. bool dte_v;
  950. u64 entry;
  951. /* Each IOMMU use separate device table with the same size */
  952. lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
  953. hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
  954. entry = (((u64) hi) << 32) + lo;
  955. old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
  956. if (old_devtb_size != pci_seg->dev_table_size) {
  957. pr_err("The device table size of IOMMU:%d is not expected!\n",
  958. iommu->index);
  959. return false;
  960. }
  961. /*
  962. * When SME is enabled in the first kernel, the entry includes the
  963. * memory encryption mask(sme_me_mask), we must remove the memory
  964. * encryption mask to obtain the true physical address in kdump kernel.
  965. */
  966. old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
  967. if (old_devtb_phys >= 0x100000000ULL) {
  968. pr_err("The address of old device table is above 4G, not trustworthy!\n");
  969. return false;
  970. }
  971. /*
  972. * Re-use the previous kernel's device table for kdump.
  973. */
  974. pci_seg->old_dev_tbl_cpy = iommu_memremap(old_devtb_phys, pci_seg->dev_table_size);
  975. if (pci_seg->old_dev_tbl_cpy == NULL) {
  976. pr_err("Failed to remap memory for reusing old device table!\n");
  977. return false;
  978. }
  979. for (devid = 0; devid <= pci_seg->last_bdf; devid++) {
  980. old_dev_tbl_entry = &pci_seg->old_dev_tbl_cpy[devid];
  981. dte_v = FIELD_GET(DTE_FLAG_V, old_dev_tbl_entry->data[0]);
  982. dom_id = FIELD_GET(DTE_DOMID_MASK, old_dev_tbl_entry->data[1]);
  983. if (!dte_v || !dom_id)
  984. continue;
  985. /*
  986. * ID reservation can fail with -ENOSPC when there
  987. * are multiple devices present in the same domain,
  988. * hence check only for -ENOMEM.
  989. */
  990. if (amd_iommu_pdom_id_reserve(dom_id, GFP_KERNEL) == -ENOMEM)
  991. return false;
  992. }
  993. return true;
  994. }
  995. static bool reuse_device_table(void)
  996. {
  997. struct amd_iommu *iommu;
  998. struct amd_iommu_pci_seg *pci_seg;
  999. if (!amd_iommu_pre_enabled)
  1000. return false;
  1001. pr_warn("Translation is already enabled - trying to reuse translation structures\n");
  1002. /*
  1003. * All IOMMUs within PCI segment shares common device table.
  1004. * Hence reuse device table only once per PCI segment.
  1005. */
  1006. for_each_pci_segment(pci_seg) {
  1007. for_each_iommu(iommu) {
  1008. if (pci_seg->id != iommu->pci_seg->id)
  1009. continue;
  1010. if (!__reuse_device_table(iommu))
  1011. return false;
  1012. break;
  1013. }
  1014. }
  1015. return true;
  1016. }
  1017. struct dev_table_entry *amd_iommu_get_ivhd_dte_flags(u16 segid, u16 devid)
  1018. {
  1019. struct ivhd_dte_flags *e;
  1020. unsigned int best_len = UINT_MAX;
  1021. struct dev_table_entry *dte = NULL;
  1022. for_each_ivhd_dte_flags(e) {
  1023. /*
  1024. * Need to go through the whole list to find the smallest range,
  1025. * which contains the devid.
  1026. */
  1027. if ((e->segid == segid) &&
  1028. (e->devid_first <= devid) && (devid <= e->devid_last)) {
  1029. unsigned int len = e->devid_last - e->devid_first;
  1030. if (len < best_len) {
  1031. dte = &(e->dte);
  1032. best_len = len;
  1033. }
  1034. }
  1035. }
  1036. return dte;
  1037. }
  1038. static bool search_ivhd_dte_flags(u16 segid, u16 first, u16 last)
  1039. {
  1040. struct ivhd_dte_flags *e;
  1041. for_each_ivhd_dte_flags(e) {
  1042. if ((e->segid == segid) &&
  1043. (e->devid_first == first) &&
  1044. (e->devid_last == last))
  1045. return true;
  1046. }
  1047. return false;
  1048. }
  1049. /*
  1050. * This function takes the device specific flags read from the ACPI
  1051. * table and sets up the device table entry with that information
  1052. */
  1053. static void __init
  1054. set_dev_entry_from_acpi_range(struct amd_iommu *iommu, u16 first, u16 last,
  1055. u32 flags, u32 ext_flags)
  1056. {
  1057. int i;
  1058. struct dev_table_entry dte = {};
  1059. /* Parse IVHD DTE setting flags and store information */
  1060. if (flags) {
  1061. struct ivhd_dte_flags *d;
  1062. if (search_ivhd_dte_flags(iommu->pci_seg->id, first, last))
  1063. return;
  1064. d = kzalloc_obj(struct ivhd_dte_flags);
  1065. if (!d)
  1066. return;
  1067. pr_debug("%s: devid range %#x:%#x\n", __func__, first, last);
  1068. if (flags & ACPI_DEVFLAG_INITPASS)
  1069. set_dte_bit(&dte, DEV_ENTRY_INIT_PASS);
  1070. if (flags & ACPI_DEVFLAG_EXTINT)
  1071. set_dte_bit(&dte, DEV_ENTRY_EINT_PASS);
  1072. if (flags & ACPI_DEVFLAG_NMI)
  1073. set_dte_bit(&dte, DEV_ENTRY_NMI_PASS);
  1074. if (flags & ACPI_DEVFLAG_SYSMGT1)
  1075. set_dte_bit(&dte, DEV_ENTRY_SYSMGT1);
  1076. if (flags & ACPI_DEVFLAG_SYSMGT2)
  1077. set_dte_bit(&dte, DEV_ENTRY_SYSMGT2);
  1078. if (flags & ACPI_DEVFLAG_LINT0)
  1079. set_dte_bit(&dte, DEV_ENTRY_LINT0_PASS);
  1080. if (flags & ACPI_DEVFLAG_LINT1)
  1081. set_dte_bit(&dte, DEV_ENTRY_LINT1_PASS);
  1082. /* Apply erratum 63, which needs info in initial_dte */
  1083. if (FIELD_GET(DTE_DATA1_SYSMGT_MASK, dte.data[1]) == 0x1)
  1084. dte.data[0] |= DTE_FLAG_IW;
  1085. memcpy(&d->dte, &dte, sizeof(dte));
  1086. d->segid = iommu->pci_seg->id;
  1087. d->devid_first = first;
  1088. d->devid_last = last;
  1089. list_add_tail(&d->list, &amd_ivhd_dev_flags_list);
  1090. }
  1091. for (i = first; i <= last; i++) {
  1092. if (flags) {
  1093. struct dev_table_entry *dev_table = get_dev_table(iommu);
  1094. memcpy(&dev_table[i], &dte, sizeof(dte));
  1095. }
  1096. amd_iommu_set_rlookup_table(iommu, i);
  1097. }
  1098. }
  1099. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  1100. u16 devid, u32 flags, u32 ext_flags)
  1101. {
  1102. set_dev_entry_from_acpi_range(iommu, devid, devid, flags, ext_flags);
  1103. }
  1104. int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)
  1105. {
  1106. struct devid_map *entry;
  1107. struct list_head *list;
  1108. if (type == IVHD_SPECIAL_IOAPIC)
  1109. list = &ioapic_map;
  1110. else if (type == IVHD_SPECIAL_HPET)
  1111. list = &hpet_map;
  1112. else
  1113. return -EINVAL;
  1114. list_for_each_entry(entry, list, list) {
  1115. if (!(entry->id == id && entry->cmd_line))
  1116. continue;
  1117. pr_info("Command-line override present for %s id %d - ignoring\n",
  1118. type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
  1119. *devid = entry->devid;
  1120. return 0;
  1121. }
  1122. entry = kzalloc_obj(*entry);
  1123. if (!entry)
  1124. return -ENOMEM;
  1125. entry->id = id;
  1126. entry->devid = *devid;
  1127. entry->cmd_line = cmd_line;
  1128. list_add_tail(&entry->list, list);
  1129. return 0;
  1130. }
  1131. static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
  1132. bool cmd_line)
  1133. {
  1134. struct acpihid_map_entry *entry;
  1135. struct list_head *list = &acpihid_map;
  1136. list_for_each_entry(entry, list, list) {
  1137. if (strcmp(entry->hid, hid) ||
  1138. (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
  1139. !entry->cmd_line)
  1140. continue;
  1141. pr_info("Command-line override for hid:%s uid:%s\n",
  1142. hid, uid);
  1143. *devid = entry->devid;
  1144. return 0;
  1145. }
  1146. entry = kzalloc_obj(*entry);
  1147. if (!entry)
  1148. return -ENOMEM;
  1149. memcpy(entry->uid, uid, strlen(uid));
  1150. memcpy(entry->hid, hid, strlen(hid));
  1151. entry->devid = *devid;
  1152. entry->cmd_line = cmd_line;
  1153. entry->root_devid = (entry->devid & (~0x7));
  1154. pr_info("%s, add hid:%s, uid:%s, rdevid:%#x\n",
  1155. entry->cmd_line ? "cmd" : "ivrs",
  1156. entry->hid, entry->uid, entry->root_devid);
  1157. list_add_tail(&entry->list, list);
  1158. return 0;
  1159. }
  1160. static int __init add_early_maps(void)
  1161. {
  1162. int i, ret;
  1163. for (i = 0; i < early_ioapic_map_size; ++i) {
  1164. ret = add_special_device(IVHD_SPECIAL_IOAPIC,
  1165. early_ioapic_map[i].id,
  1166. &early_ioapic_map[i].devid,
  1167. early_ioapic_map[i].cmd_line);
  1168. if (ret)
  1169. return ret;
  1170. }
  1171. for (i = 0; i < early_hpet_map_size; ++i) {
  1172. ret = add_special_device(IVHD_SPECIAL_HPET,
  1173. early_hpet_map[i].id,
  1174. &early_hpet_map[i].devid,
  1175. early_hpet_map[i].cmd_line);
  1176. if (ret)
  1177. return ret;
  1178. }
  1179. for (i = 0; i < early_acpihid_map_size; ++i) {
  1180. ret = add_acpi_hid_device(early_acpihid_map[i].hid,
  1181. early_acpihid_map[i].uid,
  1182. &early_acpihid_map[i].devid,
  1183. early_acpihid_map[i].cmd_line);
  1184. if (ret)
  1185. return ret;
  1186. }
  1187. return 0;
  1188. }
  1189. /*
  1190. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  1191. * initializes the hardware and our data structures with it.
  1192. */
  1193. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  1194. struct ivhd_header *h)
  1195. {
  1196. u8 *p = (u8 *)h;
  1197. u8 *end = p, flags = 0;
  1198. u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;
  1199. u32 dev_i, ext_flags = 0;
  1200. bool alias = false;
  1201. struct ivhd_entry *e;
  1202. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  1203. u32 ivhd_size;
  1204. int ret;
  1205. ret = add_early_maps();
  1206. if (ret)
  1207. return ret;
  1208. amd_iommu_apply_ivrs_quirks();
  1209. /*
  1210. * First save the recommended feature enable bits from ACPI
  1211. */
  1212. iommu->acpi_flags = h->flags;
  1213. /*
  1214. * Done. Now parse the device entries
  1215. */
  1216. ivhd_size = get_ivhd_header_size(h);
  1217. if (!ivhd_size) {
  1218. pr_err("Unsupported IVHD type %#x\n", h->type);
  1219. return -EINVAL;
  1220. }
  1221. p += ivhd_size;
  1222. end += h->length;
  1223. while (p < end) {
  1224. e = (struct ivhd_entry *)p;
  1225. seg_id = pci_seg->id;
  1226. switch (e->type) {
  1227. case IVHD_DEV_ALL:
  1228. DUMP_printk(" DEV_ALL\t\t\tsetting: %#02x\n", e->flags);
  1229. set_dev_entry_from_acpi_range(iommu, 0, pci_seg->last_bdf, e->flags, 0);
  1230. break;
  1231. case IVHD_DEV_SELECT:
  1232. DUMP_printk(" DEV_SELECT\t\t\tdevid: %04x:%02x:%02x.%x flags: %#02x\n",
  1233. seg_id, PCI_BUS_NUM(e->devid),
  1234. PCI_SLOT(e->devid),
  1235. PCI_FUNC(e->devid),
  1236. e->flags);
  1237. devid = e->devid;
  1238. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1239. break;
  1240. case IVHD_DEV_SELECT_RANGE_START:
  1241. DUMP_printk(" DEV_SELECT_RANGE_START\tdevid: %04x:%02x:%02x.%x flags: %#02x\n",
  1242. seg_id, PCI_BUS_NUM(e->devid),
  1243. PCI_SLOT(e->devid),
  1244. PCI_FUNC(e->devid),
  1245. e->flags);
  1246. devid_start = e->devid;
  1247. flags = e->flags;
  1248. ext_flags = 0;
  1249. alias = false;
  1250. break;
  1251. case IVHD_DEV_ALIAS:
  1252. DUMP_printk(" DEV_ALIAS\t\t\tdevid: %04x:%02x:%02x.%x flags: %#02x devid_to: %02x:%02x.%x\n",
  1253. seg_id, PCI_BUS_NUM(e->devid),
  1254. PCI_SLOT(e->devid),
  1255. PCI_FUNC(e->devid),
  1256. e->flags,
  1257. PCI_BUS_NUM(e->ext >> 8),
  1258. PCI_SLOT(e->ext >> 8),
  1259. PCI_FUNC(e->ext >> 8));
  1260. devid = e->devid;
  1261. devid_to = e->ext >> 8;
  1262. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  1263. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  1264. pci_seg->alias_table[devid] = devid_to;
  1265. break;
  1266. case IVHD_DEV_ALIAS_RANGE:
  1267. DUMP_printk(" DEV_ALIAS_RANGE\t\tdevid: %04x:%02x:%02x.%x flags: %#02x devid_to: %04x:%02x:%02x.%x\n",
  1268. seg_id, PCI_BUS_NUM(e->devid),
  1269. PCI_SLOT(e->devid),
  1270. PCI_FUNC(e->devid),
  1271. e->flags,
  1272. seg_id, PCI_BUS_NUM(e->ext >> 8),
  1273. PCI_SLOT(e->ext >> 8),
  1274. PCI_FUNC(e->ext >> 8));
  1275. devid_start = e->devid;
  1276. flags = e->flags;
  1277. devid_to = e->ext >> 8;
  1278. ext_flags = 0;
  1279. alias = true;
  1280. break;
  1281. case IVHD_DEV_EXT_SELECT:
  1282. DUMP_printk(" DEV_EXT_SELECT\t\tdevid: %04x:%02x:%02x.%x flags: %#02x ext: %08x\n",
  1283. seg_id, PCI_BUS_NUM(e->devid),
  1284. PCI_SLOT(e->devid),
  1285. PCI_FUNC(e->devid),
  1286. e->flags, e->ext);
  1287. devid = e->devid;
  1288. set_dev_entry_from_acpi(iommu, devid, e->flags,
  1289. e->ext);
  1290. break;
  1291. case IVHD_DEV_EXT_SELECT_RANGE:
  1292. DUMP_printk(" DEV_EXT_SELECT_RANGE\tdevid: %04x:%02x:%02x.%x flags: %#02x ext: %08x\n",
  1293. seg_id, PCI_BUS_NUM(e->devid),
  1294. PCI_SLOT(e->devid),
  1295. PCI_FUNC(e->devid),
  1296. e->flags, e->ext);
  1297. devid_start = e->devid;
  1298. flags = e->flags;
  1299. ext_flags = e->ext;
  1300. alias = false;
  1301. break;
  1302. case IVHD_DEV_RANGE_END:
  1303. DUMP_printk(" DEV_RANGE_END\t\tdevid: %04x:%02x:%02x.%x\n",
  1304. seg_id, PCI_BUS_NUM(e->devid),
  1305. PCI_SLOT(e->devid),
  1306. PCI_FUNC(e->devid));
  1307. devid = e->devid;
  1308. if (alias) {
  1309. for (dev_i = devid_start; dev_i <= devid; ++dev_i)
  1310. pci_seg->alias_table[dev_i] = devid_to;
  1311. set_dev_entry_from_acpi(iommu, devid_to, flags, ext_flags);
  1312. }
  1313. set_dev_entry_from_acpi_range(iommu, devid_start, devid, flags, ext_flags);
  1314. break;
  1315. case IVHD_DEV_SPECIAL: {
  1316. u8 handle, type;
  1317. const char *var;
  1318. u32 devid;
  1319. int ret;
  1320. handle = e->ext & 0xff;
  1321. devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));
  1322. type = (e->ext >> 24) & 0xff;
  1323. if (type == IVHD_SPECIAL_IOAPIC)
  1324. var = "IOAPIC";
  1325. else if (type == IVHD_SPECIAL_HPET)
  1326. var = "HPET";
  1327. else
  1328. var = "UNKNOWN";
  1329. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x, flags: %#02x\n",
  1330. var, (int)handle,
  1331. seg_id, PCI_BUS_NUM(devid),
  1332. PCI_SLOT(devid),
  1333. PCI_FUNC(devid),
  1334. e->flags);
  1335. ret = add_special_device(type, handle, &devid, false);
  1336. if (ret)
  1337. return ret;
  1338. /*
  1339. * add_special_device might update the devid in case a
  1340. * command-line override is present. So call
  1341. * set_dev_entry_from_acpi after add_special_device.
  1342. */
  1343. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1344. break;
  1345. }
  1346. case IVHD_DEV_ACPI_HID: {
  1347. u32 devid;
  1348. u8 hid[ACPIHID_HID_LEN];
  1349. u8 uid[ACPIHID_UID_LEN];
  1350. int ret;
  1351. if (h->type != 0x40) {
  1352. pr_err(FW_BUG "Invalid IVHD device type %#x\n",
  1353. e->type);
  1354. break;
  1355. }
  1356. BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
  1357. memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
  1358. hid[ACPIHID_HID_LEN - 1] = '\0';
  1359. if (!(*hid)) {
  1360. pr_err(FW_BUG "Invalid HID.\n");
  1361. break;
  1362. }
  1363. uid[0] = '\0';
  1364. switch (e->uidf) {
  1365. case UID_NOT_PRESENT:
  1366. if (e->uidl != 0)
  1367. pr_warn(FW_BUG "Invalid UID length.\n");
  1368. break;
  1369. case UID_IS_INTEGER:
  1370. sprintf(uid, "%d", e->uid);
  1371. break;
  1372. case UID_IS_CHARACTER:
  1373. memcpy(uid, &e->uid, e->uidl);
  1374. uid[e->uidl] = '\0';
  1375. break;
  1376. default:
  1377. break;
  1378. }
  1379. devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
  1380. DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x, flags: %#02x\n",
  1381. hid, uid, seg_id,
  1382. PCI_BUS_NUM(devid),
  1383. PCI_SLOT(devid),
  1384. PCI_FUNC(devid),
  1385. e->flags);
  1386. flags = e->flags;
  1387. ret = add_acpi_hid_device(hid, uid, &devid, false);
  1388. if (ret)
  1389. return ret;
  1390. /*
  1391. * add_special_device might update the devid in case a
  1392. * command-line override is present. So call
  1393. * set_dev_entry_from_acpi after add_special_device.
  1394. */
  1395. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1396. break;
  1397. }
  1398. default:
  1399. break;
  1400. }
  1401. p += ivhd_entry_length(p);
  1402. }
  1403. return 0;
  1404. }
  1405. /* Allocate PCI segment data structure */
  1406. static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
  1407. struct acpi_table_header *ivrs_base)
  1408. {
  1409. struct amd_iommu_pci_seg *pci_seg;
  1410. int last_bdf;
  1411. /*
  1412. * First parse ACPI tables to find the largest Bus/Dev/Func we need to
  1413. * handle in this PCI segment. Upon this information the shared data
  1414. * structures for the PCI segments in the system will be allocated.
  1415. */
  1416. last_bdf = find_last_devid_acpi(ivrs_base, id);
  1417. if (last_bdf < 0)
  1418. return NULL;
  1419. pci_seg = kzalloc_obj(struct amd_iommu_pci_seg);
  1420. if (pci_seg == NULL)
  1421. return NULL;
  1422. pci_seg->last_bdf = last_bdf;
  1423. DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
  1424. pci_seg->dev_table_size =
  1425. max(roundup_pow_of_two((last_bdf + 1) * DEV_TABLE_ENTRY_SIZE),
  1426. SZ_4K);
  1427. pci_seg->id = id;
  1428. init_llist_head(&pci_seg->dev_data_list);
  1429. INIT_LIST_HEAD(&pci_seg->unity_map);
  1430. list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
  1431. if (alloc_dev_table(pci_seg))
  1432. goto err_free_pci_seg;
  1433. if (alloc_alias_table(pci_seg))
  1434. goto err_free_dev_table;
  1435. if (alloc_rlookup_table(pci_seg))
  1436. goto err_free_alias_table;
  1437. return pci_seg;
  1438. err_free_alias_table:
  1439. free_alias_table(pci_seg);
  1440. err_free_dev_table:
  1441. free_dev_table(pci_seg);
  1442. err_free_pci_seg:
  1443. list_del(&pci_seg->list);
  1444. kfree(pci_seg);
  1445. return NULL;
  1446. }
  1447. static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,
  1448. struct acpi_table_header *ivrs_base)
  1449. {
  1450. struct amd_iommu_pci_seg *pci_seg;
  1451. for_each_pci_segment(pci_seg) {
  1452. if (pci_seg->id == id)
  1453. return pci_seg;
  1454. }
  1455. return alloc_pci_segment(id, ivrs_base);
  1456. }
  1457. static void __init free_pci_segments(void)
  1458. {
  1459. struct amd_iommu_pci_seg *pci_seg, *next;
  1460. for_each_pci_segment_safe(pci_seg, next) {
  1461. list_del(&pci_seg->list);
  1462. free_irq_lookup_table(pci_seg);
  1463. free_rlookup_table(pci_seg);
  1464. free_alias_table(pci_seg);
  1465. free_dev_table(pci_seg);
  1466. kfree(pci_seg);
  1467. }
  1468. }
  1469. static void __init free_sysfs(struct amd_iommu *iommu)
  1470. {
  1471. if (iommu->iommu.dev) {
  1472. iommu_device_unregister(&iommu->iommu);
  1473. iommu_device_sysfs_remove(&iommu->iommu);
  1474. }
  1475. }
  1476. static void __init free_iommu_one(struct amd_iommu *iommu)
  1477. {
  1478. free_sysfs(iommu);
  1479. free_iommu_buffers(iommu);
  1480. amd_iommu_free_ppr_log(iommu);
  1481. free_ga_log(iommu);
  1482. iommu_unmap_mmio_space(iommu);
  1483. amd_iommu_iopf_uninit(iommu);
  1484. }
  1485. static void __init free_iommu_all(void)
  1486. {
  1487. struct amd_iommu *iommu, *next;
  1488. for_each_iommu_safe(iommu, next) {
  1489. list_del(&iommu->list);
  1490. free_iommu_one(iommu);
  1491. kfree(iommu);
  1492. }
  1493. }
  1494. /*
  1495. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  1496. * Workaround:
  1497. * BIOS should disable L2B micellaneous clock gating by setting
  1498. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  1499. */
  1500. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  1501. {
  1502. u32 value;
  1503. if ((boot_cpu_data.x86 != 0x15) ||
  1504. (boot_cpu_data.x86_model < 0x10) ||
  1505. (boot_cpu_data.x86_model > 0x1f))
  1506. return;
  1507. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1508. pci_read_config_dword(iommu->dev, 0xf4, &value);
  1509. if (value & BIT(2))
  1510. return;
  1511. /* Select NB indirect register 0x90 and enable writing */
  1512. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  1513. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  1514. pci_info(iommu->dev, "Applying erratum 746 workaround\n");
  1515. /* Clear the enable writing bit */
  1516. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1517. }
  1518. /*
  1519. * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
  1520. * Workaround:
  1521. * BIOS should enable ATS write permission check by setting
  1522. * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
  1523. */
  1524. static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
  1525. {
  1526. u32 value;
  1527. if ((boot_cpu_data.x86 != 0x15) ||
  1528. (boot_cpu_data.x86_model < 0x30) ||
  1529. (boot_cpu_data.x86_model > 0x3f))
  1530. return;
  1531. /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
  1532. value = iommu_read_l2(iommu, 0x47);
  1533. if (value & BIT(0))
  1534. return;
  1535. /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
  1536. iommu_write_l2(iommu, 0x47, value | BIT(0));
  1537. pci_info(iommu->dev, "Applying ATS write check workaround\n");
  1538. }
  1539. /*
  1540. * This function glues the initialization function for one IOMMU
  1541. * together and also allocates the command buffer and programs the
  1542. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  1543. */
  1544. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
  1545. struct acpi_table_header *ivrs_base)
  1546. {
  1547. struct amd_iommu_pci_seg *pci_seg;
  1548. pci_seg = get_pci_segment(h->pci_seg, ivrs_base);
  1549. if (pci_seg == NULL)
  1550. return -ENOMEM;
  1551. iommu->pci_seg = pci_seg;
  1552. raw_spin_lock_init(&iommu->lock);
  1553. iommu->cmd_sem_val = 0;
  1554. /* Add IOMMU to internal data structures */
  1555. list_add_tail(&iommu->list, &amd_iommu_list);
  1556. iommu->index = amd_iommus_present++;
  1557. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  1558. WARN(1, "System has more IOMMUs than supported by this driver\n");
  1559. return -ENOSYS;
  1560. }
  1561. /*
  1562. * Copy data from ACPI table entry to the iommu struct
  1563. */
  1564. iommu->devid = h->devid;
  1565. iommu->cap_ptr = h->cap_ptr;
  1566. iommu->mmio_phys = h->mmio_phys;
  1567. switch (h->type) {
  1568. case 0x10:
  1569. /* Check if IVHD EFR contains proper max banks/counters */
  1570. if ((h->efr_attr != 0) &&
  1571. ((h->efr_attr & (0xF << 13)) != 0) &&
  1572. ((h->efr_attr & (0x3F << 17)) != 0))
  1573. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1574. else
  1575. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1576. /* GAM requires GA mode. */
  1577. if ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0)
  1578. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1579. break;
  1580. case 0x11:
  1581. case 0x40:
  1582. if (h->efr_reg & (1 << 9))
  1583. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1584. else
  1585. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1586. /* XT and GAM require GA mode. */
  1587. if ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0) {
  1588. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1589. break;
  1590. }
  1591. if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
  1592. amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
  1593. if (h->efr_attr & BIT(IOMMU_IVHD_ATTR_HATDIS_SHIFT)) {
  1594. pr_warn_once("Host Address Translation is not supported.\n");
  1595. amd_iommu_hatdis = true;
  1596. }
  1597. early_iommu_features_init(iommu, h);
  1598. break;
  1599. default:
  1600. return -EINVAL;
  1601. }
  1602. iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
  1603. iommu->mmio_phys_end);
  1604. if (!iommu->mmio_base)
  1605. return -ENOMEM;
  1606. return init_iommu_from_acpi(iommu, h);
  1607. }
  1608. static int __init init_iommu_one_late(struct amd_iommu *iommu)
  1609. {
  1610. int ret;
  1611. ret = alloc_iommu_buffers(iommu);
  1612. if (ret)
  1613. return ret;
  1614. iommu->int_enabled = false;
  1615. init_translation_status(iommu);
  1616. if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
  1617. iommu_disable(iommu);
  1618. clear_translation_pre_enabled(iommu);
  1619. pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
  1620. iommu->index);
  1621. }
  1622. if (amd_iommu_pre_enabled)
  1623. amd_iommu_pre_enabled = translation_pre_enabled(iommu);
  1624. if (amd_iommu_irq_remap) {
  1625. ret = amd_iommu_create_irq_domain(iommu);
  1626. if (ret)
  1627. return ret;
  1628. }
  1629. /*
  1630. * Make sure IOMMU is not considered to translate itself. The IVRS
  1631. * table tells us so, but this is a lie!
  1632. */
  1633. iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
  1634. return 0;
  1635. }
  1636. /**
  1637. * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
  1638. * @ivrs: Pointer to the IVRS header
  1639. *
  1640. * This function search through all IVDB of the maximum supported IVHD
  1641. */
  1642. static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
  1643. {
  1644. u8 *base = (u8 *)ivrs;
  1645. struct ivhd_header *ivhd = (struct ivhd_header *)
  1646. (base + IVRS_HEADER_LENGTH);
  1647. u8 last_type = ivhd->type;
  1648. u16 devid = ivhd->devid;
  1649. while (((u8 *)ivhd - base < ivrs->length) &&
  1650. (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
  1651. u8 *p = (u8 *) ivhd;
  1652. if (ivhd->devid == devid)
  1653. last_type = ivhd->type;
  1654. ivhd = (struct ivhd_header *)(p + ivhd->length);
  1655. }
  1656. return last_type;
  1657. }
  1658. /*
  1659. * Iterates over all IOMMU entries in the ACPI table, allocates the
  1660. * IOMMU structure and initializes it with init_iommu_one()
  1661. */
  1662. static int __init init_iommu_all(struct acpi_table_header *table)
  1663. {
  1664. u8 *p = (u8 *)table, *end = (u8 *)table;
  1665. struct ivhd_header *h;
  1666. struct amd_iommu *iommu;
  1667. int ret;
  1668. end += table->length;
  1669. p += IVRS_HEADER_LENGTH;
  1670. /* Phase 1: Process all IVHD blocks */
  1671. while (p < end) {
  1672. h = (struct ivhd_header *)p;
  1673. if (*p == amd_iommu_target_ivhd_type) {
  1674. DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "
  1675. "flags: %01x info %04x\n",
  1676. h->pci_seg, PCI_BUS_NUM(h->devid),
  1677. PCI_SLOT(h->devid), PCI_FUNC(h->devid),
  1678. h->cap_ptr, h->flags, h->info);
  1679. DUMP_printk(" mmio-addr: %016llx\n",
  1680. h->mmio_phys);
  1681. iommu = kzalloc_obj(struct amd_iommu);
  1682. if (iommu == NULL)
  1683. return -ENOMEM;
  1684. ret = init_iommu_one(iommu, h, table);
  1685. if (ret)
  1686. return ret;
  1687. }
  1688. p += h->length;
  1689. }
  1690. WARN_ON(p != end);
  1691. /* Phase 2 : Early feature support check */
  1692. get_global_efr();
  1693. /* Phase 3 : Enabling IOMMU features */
  1694. for_each_iommu(iommu) {
  1695. ret = init_iommu_one_late(iommu);
  1696. if (ret)
  1697. return ret;
  1698. }
  1699. return 0;
  1700. }
  1701. static void init_iommu_perf_ctr(struct amd_iommu *iommu)
  1702. {
  1703. u64 val;
  1704. struct pci_dev *pdev = iommu->dev;
  1705. if (!check_feature(FEATURE_PC))
  1706. return;
  1707. amd_iommu_pc_present = true;
  1708. pci_info(pdev, "IOMMU performance counters supported\n");
  1709. val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
  1710. iommu->max_banks = (u8) ((val >> 12) & 0x3f);
  1711. iommu->max_counters = (u8) ((val >> 7) & 0xf);
  1712. return;
  1713. }
  1714. static ssize_t amd_iommu_show_cap(struct device *dev,
  1715. struct device_attribute *attr,
  1716. char *buf)
  1717. {
  1718. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1719. return sysfs_emit(buf, "%x\n", iommu->cap);
  1720. }
  1721. static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
  1722. static ssize_t amd_iommu_show_features(struct device *dev,
  1723. struct device_attribute *attr,
  1724. char *buf)
  1725. {
  1726. return sysfs_emit(buf, "%llx:%llx\n", amd_iommu_efr, amd_iommu_efr2);
  1727. }
  1728. static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
  1729. static struct attribute *amd_iommu_attrs[] = {
  1730. &dev_attr_cap.attr,
  1731. &dev_attr_features.attr,
  1732. NULL,
  1733. };
  1734. static struct attribute_group amd_iommu_group = {
  1735. .name = "amd-iommu",
  1736. .attrs = amd_iommu_attrs,
  1737. };
  1738. static const struct attribute_group *amd_iommu_groups[] = {
  1739. &amd_iommu_group,
  1740. NULL,
  1741. };
  1742. /*
  1743. * Note: IVHD 0x11 and 0x40 also contains exact copy
  1744. * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
  1745. * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
  1746. */
  1747. static void __init late_iommu_features_init(struct amd_iommu *iommu)
  1748. {
  1749. u64 features, features2;
  1750. if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
  1751. return;
  1752. /* read extended feature bits */
  1753. features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
  1754. features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
  1755. if (!amd_iommu_efr) {
  1756. amd_iommu_efr = features;
  1757. amd_iommu_efr2 = features2;
  1758. return;
  1759. }
  1760. /*
  1761. * Sanity check and warn if EFR values from
  1762. * IVHD and MMIO conflict.
  1763. */
  1764. if (features != amd_iommu_efr ||
  1765. features2 != amd_iommu_efr2) {
  1766. pr_warn(FW_WARN
  1767. "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
  1768. features, amd_iommu_efr,
  1769. features2, amd_iommu_efr2);
  1770. }
  1771. }
  1772. static int __init iommu_init_pci(struct amd_iommu *iommu)
  1773. {
  1774. int cap_ptr = iommu->cap_ptr;
  1775. int ret;
  1776. iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
  1777. PCI_BUS_NUM(iommu->devid),
  1778. iommu->devid & 0xff);
  1779. if (!iommu->dev)
  1780. return -ENODEV;
  1781. /* ACPI _PRT won't have an IRQ for IOMMU */
  1782. iommu->dev->irq_managed = 1;
  1783. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  1784. &iommu->cap);
  1785. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  1786. amd_iommu_iotlb_sup = false;
  1787. late_iommu_features_init(iommu);
  1788. if (check_feature(FEATURE_GT)) {
  1789. int glxval;
  1790. u64 pasmax;
  1791. pasmax = FIELD_GET(FEATURE_PASMAX, amd_iommu_efr);
  1792. iommu->iommu.max_pasids = (1 << (pasmax + 1)) - 1;
  1793. BUG_ON(iommu->iommu.max_pasids & ~PASID_MASK);
  1794. glxval = FIELD_GET(FEATURE_GLX, amd_iommu_efr);
  1795. if (amd_iommu_max_glx_val == -1)
  1796. amd_iommu_max_glx_val = glxval;
  1797. else
  1798. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  1799. iommu_enable_gt(iommu);
  1800. }
  1801. if (check_feature(FEATURE_PPR) && amd_iommu_alloc_ppr_log(iommu))
  1802. return -ENOMEM;
  1803. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
  1804. pr_info("Using strict mode due to virtualization\n");
  1805. iommu_set_dma_strict();
  1806. amd_iommu_np_cache = true;
  1807. }
  1808. init_iommu_perf_ctr(iommu);
  1809. if (is_rd890_iommu(iommu->dev)) {
  1810. int i, j;
  1811. iommu->root_pdev =
  1812. pci_get_domain_bus_and_slot(iommu->pci_seg->id,
  1813. iommu->dev->bus->number,
  1814. PCI_DEVFN(0, 0));
  1815. /*
  1816. * Some rd890 systems may not be fully reconfigured by the
  1817. * BIOS, so it's necessary for us to store this information so
  1818. * it can be reprogrammed on resume
  1819. */
  1820. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1821. &iommu->stored_addr_lo);
  1822. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1823. &iommu->stored_addr_hi);
  1824. /* Low bit locks writes to configuration space */
  1825. iommu->stored_addr_lo &= ~1;
  1826. for (i = 0; i < 6; i++)
  1827. for (j = 0; j < 0x12; j++)
  1828. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  1829. for (i = 0; i < 0x83; i++)
  1830. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  1831. }
  1832. amd_iommu_erratum_746_workaround(iommu);
  1833. amd_iommu_ats_write_check_workaround(iommu);
  1834. ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
  1835. amd_iommu_groups, "ivhd%d", iommu->index);
  1836. if (ret)
  1837. return ret;
  1838. /*
  1839. * Allocate per IOMMU IOPF queue here so that in attach device path,
  1840. * PRI capable device can be added to IOPF queue
  1841. */
  1842. if (amd_iommu_gt_ppr_supported()) {
  1843. ret = amd_iommu_iopf_init(iommu);
  1844. if (ret)
  1845. return ret;
  1846. }
  1847. ret = iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
  1848. if (ret || amd_iommu_pgtable == PD_MODE_NONE) {
  1849. /*
  1850. * Remove sysfs if DMA translation is not supported by the
  1851. * IOMMU. Do not return an error to enable IRQ remapping
  1852. * in state_next(), DTE[V, TV] must eventually be set to 0.
  1853. */
  1854. iommu_device_sysfs_remove(&iommu->iommu);
  1855. }
  1856. return pci_enable_device(iommu->dev);
  1857. }
  1858. static void print_iommu_info(void)
  1859. {
  1860. int i;
  1861. static const char * const feat_str[] = {
  1862. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  1863. "IA", "GA", "HE", "PC"
  1864. };
  1865. if (amd_iommu_efr) {
  1866. pr_info("Extended features (%#llx, %#llx):", amd_iommu_efr, amd_iommu_efr2);
  1867. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1868. if (check_feature(1ULL << i))
  1869. pr_cont(" %s", feat_str[i]);
  1870. }
  1871. if (check_feature(FEATURE_GAM_VAPIC))
  1872. pr_cont(" GA_vAPIC");
  1873. if (check_feature(FEATURE_SNP))
  1874. pr_cont(" SNP");
  1875. if (check_feature2(FEATURE_SEVSNPIO_SUP))
  1876. pr_cont(" SEV-TIO");
  1877. pr_cont("\n");
  1878. }
  1879. if (irq_remapping_enabled) {
  1880. pr_info("Interrupt remapping enabled\n");
  1881. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1882. pr_info("X2APIC enabled\n");
  1883. }
  1884. if (amd_iommu_pgtable == PD_MODE_V2) {
  1885. pr_info("V2 page table enabled (Paging mode : %d level)\n",
  1886. amd_iommu_gpt_level);
  1887. }
  1888. }
  1889. static int __init amd_iommu_init_pci(void)
  1890. {
  1891. struct amd_iommu *iommu;
  1892. struct amd_iommu_pci_seg *pci_seg;
  1893. int ret;
  1894. /* Init global identity domain before registering IOMMU */
  1895. amd_iommu_init_identity_domain();
  1896. for_each_iommu(iommu) {
  1897. ret = iommu_init_pci(iommu);
  1898. if (ret) {
  1899. pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
  1900. iommu->index, ret);
  1901. goto out;
  1902. }
  1903. /* Need to setup range after PCI init */
  1904. iommu_set_cwwb_range(iommu);
  1905. }
  1906. /*
  1907. * Order is important here to make sure any unity map requirements are
  1908. * fulfilled. The unity mappings are created and written to the device
  1909. * table during the iommu_init_pci() call.
  1910. *
  1911. * After that we call init_device_table_dma() to make sure any
  1912. * uninitialized DTE will block DMA, and in the end we flush the caches
  1913. * of all IOMMUs to make sure the changes to the device table are
  1914. * active.
  1915. */
  1916. for_each_pci_segment(pci_seg)
  1917. init_device_table_dma(pci_seg);
  1918. for_each_iommu(iommu)
  1919. amd_iommu_flush_all_caches(iommu);
  1920. print_iommu_info();
  1921. out:
  1922. return ret;
  1923. }
  1924. /****************************************************************************
  1925. *
  1926. * The following functions initialize the MSI interrupts for all IOMMUs
  1927. * in the system. It's a bit challenging because there could be multiple
  1928. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1929. * pci_dev.
  1930. *
  1931. ****************************************************************************/
  1932. static int iommu_setup_msi(struct amd_iommu *iommu)
  1933. {
  1934. int r;
  1935. r = pci_enable_msi(iommu->dev);
  1936. if (r)
  1937. return r;
  1938. r = request_threaded_irq(iommu->dev->irq, NULL, amd_iommu_int_thread,
  1939. IRQF_ONESHOT, "AMD-Vi", iommu);
  1940. if (r) {
  1941. pci_disable_msi(iommu->dev);
  1942. return r;
  1943. }
  1944. return 0;
  1945. }
  1946. union intcapxt {
  1947. u64 capxt;
  1948. struct {
  1949. u64 reserved_0 : 2,
  1950. dest_mode_logical : 1,
  1951. reserved_1 : 5,
  1952. destid_0_23 : 24,
  1953. vector : 8,
  1954. reserved_2 : 16,
  1955. destid_24_31 : 8;
  1956. };
  1957. } __attribute__ ((packed));
  1958. static struct irq_chip intcapxt_controller;
  1959. static int intcapxt_irqdomain_activate(struct irq_domain *domain,
  1960. struct irq_data *irqd, bool reserve)
  1961. {
  1962. return 0;
  1963. }
  1964. static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
  1965. struct irq_data *irqd)
  1966. {
  1967. }
  1968. static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
  1969. unsigned int nr_irqs, void *arg)
  1970. {
  1971. struct irq_alloc_info *info = arg;
  1972. int i, ret;
  1973. if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
  1974. return -EINVAL;
  1975. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  1976. if (ret < 0)
  1977. return ret;
  1978. for (i = virq; i < virq + nr_irqs; i++) {
  1979. struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
  1980. irqd->chip = &intcapxt_controller;
  1981. irqd->hwirq = info->hwirq;
  1982. irqd->chip_data = info->data;
  1983. __irq_set_handler(i, handle_edge_irq, 0, "edge");
  1984. }
  1985. return ret;
  1986. }
  1987. static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
  1988. unsigned int nr_irqs)
  1989. {
  1990. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  1991. }
  1992. static void intcapxt_unmask_irq(struct irq_data *irqd)
  1993. {
  1994. struct amd_iommu *iommu = irqd->chip_data;
  1995. struct irq_cfg *cfg = irqd_cfg(irqd);
  1996. union intcapxt xt;
  1997. xt.capxt = 0ULL;
  1998. xt.dest_mode_logical = apic->dest_mode_logical;
  1999. xt.vector = cfg->vector;
  2000. xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
  2001. xt.destid_24_31 = cfg->dest_apicid >> 24;
  2002. writeq(xt.capxt, iommu->mmio_base + irqd->hwirq);
  2003. }
  2004. static void intcapxt_mask_irq(struct irq_data *irqd)
  2005. {
  2006. struct amd_iommu *iommu = irqd->chip_data;
  2007. writeq(0, iommu->mmio_base + irqd->hwirq);
  2008. }
  2009. static int intcapxt_set_affinity(struct irq_data *irqd,
  2010. const struct cpumask *mask, bool force)
  2011. {
  2012. struct irq_data *parent = irqd->parent_data;
  2013. int ret;
  2014. ret = parent->chip->irq_set_affinity(parent, mask, force);
  2015. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  2016. return ret;
  2017. return 0;
  2018. }
  2019. static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
  2020. {
  2021. return on ? -EOPNOTSUPP : 0;
  2022. }
  2023. static struct irq_chip intcapxt_controller = {
  2024. .name = "IOMMU-MSI",
  2025. .irq_unmask = intcapxt_unmask_irq,
  2026. .irq_mask = intcapxt_mask_irq,
  2027. .irq_ack = irq_chip_ack_parent,
  2028. .irq_retrigger = irq_chip_retrigger_hierarchy,
  2029. .irq_set_affinity = intcapxt_set_affinity,
  2030. .irq_set_wake = intcapxt_set_wake,
  2031. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_MOVE_DEFERRED,
  2032. };
  2033. static const struct irq_domain_ops intcapxt_domain_ops = {
  2034. .alloc = intcapxt_irqdomain_alloc,
  2035. .free = intcapxt_irqdomain_free,
  2036. .activate = intcapxt_irqdomain_activate,
  2037. .deactivate = intcapxt_irqdomain_deactivate,
  2038. };
  2039. static struct irq_domain *iommu_irqdomain;
  2040. static struct irq_domain *iommu_get_irqdomain(void)
  2041. {
  2042. struct fwnode_handle *fn;
  2043. /* No need for locking here (yet) as the init is single-threaded */
  2044. if (iommu_irqdomain)
  2045. return iommu_irqdomain;
  2046. fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
  2047. if (!fn)
  2048. return NULL;
  2049. iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
  2050. fn, &intcapxt_domain_ops,
  2051. NULL);
  2052. if (!iommu_irqdomain)
  2053. irq_domain_free_fwnode(fn);
  2054. return iommu_irqdomain;
  2055. }
  2056. static int __iommu_setup_intcapxt(struct amd_iommu *iommu, const char *devname,
  2057. int hwirq, irq_handler_t thread_fn)
  2058. {
  2059. struct irq_domain *domain;
  2060. struct irq_alloc_info info;
  2061. int irq, ret;
  2062. int node = dev_to_node(&iommu->dev->dev);
  2063. domain = iommu_get_irqdomain();
  2064. if (!domain)
  2065. return -ENXIO;
  2066. init_irq_alloc_info(&info, NULL);
  2067. info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
  2068. info.data = iommu;
  2069. info.hwirq = hwirq;
  2070. irq = irq_domain_alloc_irqs(domain, 1, node, &info);
  2071. if (irq < 0) {
  2072. irq_domain_remove(domain);
  2073. return irq;
  2074. }
  2075. ret = request_threaded_irq(irq, NULL, thread_fn, IRQF_ONESHOT, devname,
  2076. iommu);
  2077. if (ret) {
  2078. irq_domain_free_irqs(irq, 1);
  2079. irq_domain_remove(domain);
  2080. return ret;
  2081. }
  2082. return 0;
  2083. }
  2084. static int iommu_setup_intcapxt(struct amd_iommu *iommu)
  2085. {
  2086. int ret;
  2087. snprintf(iommu->evt_irq_name, sizeof(iommu->evt_irq_name),
  2088. "AMD-Vi%d-Evt", iommu->index);
  2089. ret = __iommu_setup_intcapxt(iommu, iommu->evt_irq_name,
  2090. MMIO_INTCAPXT_EVT_OFFSET,
  2091. amd_iommu_int_thread_evtlog);
  2092. if (ret)
  2093. return ret;
  2094. snprintf(iommu->ppr_irq_name, sizeof(iommu->ppr_irq_name),
  2095. "AMD-Vi%d-PPR", iommu->index);
  2096. ret = __iommu_setup_intcapxt(iommu, iommu->ppr_irq_name,
  2097. MMIO_INTCAPXT_PPR_OFFSET,
  2098. amd_iommu_int_thread_pprlog);
  2099. if (ret)
  2100. return ret;
  2101. #ifdef CONFIG_IRQ_REMAP
  2102. snprintf(iommu->ga_irq_name, sizeof(iommu->ga_irq_name),
  2103. "AMD-Vi%d-GA", iommu->index);
  2104. ret = __iommu_setup_intcapxt(iommu, iommu->ga_irq_name,
  2105. MMIO_INTCAPXT_GALOG_OFFSET,
  2106. amd_iommu_int_thread_galog);
  2107. #endif
  2108. return ret;
  2109. }
  2110. static int iommu_init_irq(struct amd_iommu *iommu)
  2111. {
  2112. int ret;
  2113. if (iommu->int_enabled)
  2114. goto enable_faults;
  2115. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  2116. ret = iommu_setup_intcapxt(iommu);
  2117. else if (iommu->dev->msi_cap)
  2118. ret = iommu_setup_msi(iommu);
  2119. else
  2120. ret = -ENODEV;
  2121. if (ret)
  2122. return ret;
  2123. iommu->int_enabled = true;
  2124. enable_faults:
  2125. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  2126. iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
  2127. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  2128. return 0;
  2129. }
  2130. /****************************************************************************
  2131. *
  2132. * The next functions belong to the third pass of parsing the ACPI
  2133. * table. In this last pass the memory mapping requirements are
  2134. * gathered (like exclusion and unity mapping ranges).
  2135. *
  2136. ****************************************************************************/
  2137. static void __init free_unity_maps(void)
  2138. {
  2139. struct unity_map_entry *entry, *next;
  2140. struct amd_iommu_pci_seg *p, *pci_seg;
  2141. for_each_pci_segment_safe(pci_seg, p) {
  2142. list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
  2143. list_del(&entry->list);
  2144. kfree(entry);
  2145. }
  2146. }
  2147. }
  2148. /* called for unity map ACPI definition */
  2149. static int __init init_unity_map_range(struct ivmd_header *m,
  2150. struct acpi_table_header *ivrs_base)
  2151. {
  2152. struct unity_map_entry *e = NULL;
  2153. struct amd_iommu_pci_seg *pci_seg;
  2154. char *s;
  2155. pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
  2156. if (pci_seg == NULL)
  2157. return -ENOMEM;
  2158. e = kzalloc_obj(*e);
  2159. if (e == NULL)
  2160. return -ENOMEM;
  2161. switch (m->type) {
  2162. default:
  2163. kfree(e);
  2164. return 0;
  2165. case ACPI_IVMD_TYPE:
  2166. s = "IVMD_TYPEi\t\t\t";
  2167. e->devid_start = e->devid_end = m->devid;
  2168. break;
  2169. case ACPI_IVMD_TYPE_ALL:
  2170. s = "IVMD_TYPE_ALL\t\t";
  2171. e->devid_start = 0;
  2172. e->devid_end = pci_seg->last_bdf;
  2173. break;
  2174. case ACPI_IVMD_TYPE_RANGE:
  2175. s = "IVMD_TYPE_RANGE\t\t";
  2176. e->devid_start = m->devid;
  2177. e->devid_end = m->aux;
  2178. break;
  2179. }
  2180. e->address_start = PAGE_ALIGN(m->range_start);
  2181. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  2182. e->prot = m->flags >> 1;
  2183. /*
  2184. * Treat per-device exclusion ranges as r/w unity-mapped regions
  2185. * since some buggy BIOSes might lead to the overwritten exclusion
  2186. * range (exclusion_start and exclusion_length members). This
  2187. * happens when there are multiple exclusion ranges (IVMD entries)
  2188. * defined in ACPI table.
  2189. */
  2190. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  2191. e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
  2192. DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
  2193. "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
  2194. " flags: %x\n", s, m->pci_seg,
  2195. PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
  2196. PCI_FUNC(e->devid_start), m->pci_seg,
  2197. PCI_BUS_NUM(e->devid_end),
  2198. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  2199. e->address_start, e->address_end, m->flags);
  2200. list_add_tail(&e->list, &pci_seg->unity_map);
  2201. return 0;
  2202. }
  2203. /* iterates over all memory definitions we find in the ACPI table */
  2204. static int __init init_memory_definitions(struct acpi_table_header *table)
  2205. {
  2206. u8 *p = (u8 *)table, *end = (u8 *)table;
  2207. struct ivmd_header *m;
  2208. end += table->length;
  2209. p += IVRS_HEADER_LENGTH;
  2210. while (p < end) {
  2211. m = (struct ivmd_header *)p;
  2212. if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
  2213. init_unity_map_range(m, table);
  2214. p += m->length;
  2215. }
  2216. return 0;
  2217. }
  2218. /*
  2219. * Init the device table to not allow DMA access for devices
  2220. */
  2221. static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
  2222. {
  2223. u32 devid;
  2224. struct dev_table_entry *dev_table = pci_seg->dev_table;
  2225. if (!dev_table || amd_iommu_pgtable == PD_MODE_NONE)
  2226. return;
  2227. for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
  2228. set_dte_bit(&dev_table[devid], DEV_ENTRY_VALID);
  2229. if (!amd_iommu_snp_en)
  2230. set_dte_bit(&dev_table[devid], DEV_ENTRY_TRANSLATION);
  2231. }
  2232. }
  2233. static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
  2234. {
  2235. u32 devid;
  2236. struct dev_table_entry *dev_table = pci_seg->dev_table;
  2237. if (dev_table == NULL)
  2238. return;
  2239. for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
  2240. dev_table[devid].data[0] = 0ULL;
  2241. dev_table[devid].data[1] = 0ULL;
  2242. }
  2243. }
  2244. static void init_device_table(void)
  2245. {
  2246. struct amd_iommu_pci_seg *pci_seg;
  2247. u32 devid;
  2248. if (!amd_iommu_irq_remap)
  2249. return;
  2250. for_each_pci_segment(pci_seg) {
  2251. for (devid = 0; devid <= pci_seg->last_bdf; ++devid)
  2252. set_dte_bit(&pci_seg->dev_table[devid], DEV_ENTRY_IRQ_TBL_EN);
  2253. }
  2254. }
  2255. static void iommu_init_flags(struct amd_iommu *iommu)
  2256. {
  2257. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  2258. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  2259. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  2260. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  2261. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  2262. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  2263. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  2264. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  2265. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  2266. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  2267. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  2268. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  2269. /*
  2270. * make IOMMU memory accesses cache coherent
  2271. */
  2272. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  2273. /* Set IOTLB invalidation timeout to 1s */
  2274. iommu_feature_set(iommu, CTRL_INV_TO_1S, CTRL_INV_TO_MASK, CONTROL_INV_TIMEOUT);
  2275. /* Enable Enhanced Peripheral Page Request Handling */
  2276. if (check_feature(FEATURE_EPHSUP))
  2277. iommu_feature_enable(iommu, CONTROL_EPH_EN);
  2278. }
  2279. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  2280. {
  2281. int i, j;
  2282. u32 ioc_feature_control;
  2283. struct pci_dev *pdev = iommu->root_pdev;
  2284. /* RD890 BIOSes may not have completely reconfigured the iommu */
  2285. if (!is_rd890_iommu(iommu->dev) || !pdev)
  2286. return;
  2287. /*
  2288. * First, we need to ensure that the iommu is enabled. This is
  2289. * controlled by a register in the northbridge
  2290. */
  2291. /* Select Northbridge indirect register 0x75 and enable writing */
  2292. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  2293. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  2294. /* Enable the iommu */
  2295. if (!(ioc_feature_control & 0x1))
  2296. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  2297. /* Restore the iommu BAR */
  2298. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  2299. iommu->stored_addr_lo);
  2300. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  2301. iommu->stored_addr_hi);
  2302. /* Restore the l1 indirect regs for each of the 6 l1s */
  2303. for (i = 0; i < 6; i++)
  2304. for (j = 0; j < 0x12; j++)
  2305. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  2306. /* Restore the l2 indirect regs */
  2307. for (i = 0; i < 0x83; i++)
  2308. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  2309. /* Lock PCI setup registers */
  2310. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  2311. iommu->stored_addr_lo | 1);
  2312. }
  2313. static void iommu_enable_ga(struct amd_iommu *iommu)
  2314. {
  2315. #ifdef CONFIG_IRQ_REMAP
  2316. switch (amd_iommu_guest_ir) {
  2317. case AMD_IOMMU_GUEST_IR_VAPIC:
  2318. case AMD_IOMMU_GUEST_IR_LEGACY_GA:
  2319. iommu_feature_enable(iommu, CONTROL_GA_EN);
  2320. iommu->irte_ops = &irte_128_ops;
  2321. break;
  2322. default:
  2323. iommu->irte_ops = &irte_32_ops;
  2324. break;
  2325. }
  2326. #endif
  2327. }
  2328. static void iommu_disable_irtcachedis(struct amd_iommu *iommu)
  2329. {
  2330. iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
  2331. }
  2332. static void iommu_enable_irtcachedis(struct amd_iommu *iommu)
  2333. {
  2334. u64 ctrl;
  2335. if (!amd_iommu_irtcachedis)
  2336. return;
  2337. /*
  2338. * Note:
  2339. * The support for IRTCacheDis feature is dertermined by
  2340. * checking if the bit is writable.
  2341. */
  2342. iommu_feature_enable(iommu, CONTROL_IRTCACHEDIS);
  2343. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  2344. ctrl &= (1ULL << CONTROL_IRTCACHEDIS);
  2345. if (ctrl)
  2346. iommu->irtcachedis_enabled = true;
  2347. pr_info("iommu%d (%#06x) : IRT cache is %s\n",
  2348. iommu->index, iommu->devid,
  2349. iommu->irtcachedis_enabled ? "disabled" : "enabled");
  2350. }
  2351. static void iommu_enable_2k_int(struct amd_iommu *iommu)
  2352. {
  2353. if (!FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
  2354. return;
  2355. iommu_feature_set(iommu,
  2356. CONTROL_NUM_INT_REMAP_MODE_2K,
  2357. CONTROL_NUM_INT_REMAP_MODE_MASK,
  2358. CONTROL_NUM_INT_REMAP_MODE);
  2359. }
  2360. static void early_enable_iommu(struct amd_iommu *iommu)
  2361. {
  2362. iommu_disable(iommu);
  2363. iommu_init_flags(iommu);
  2364. iommu_set_device_table(iommu);
  2365. iommu_enable_command_buffer(iommu);
  2366. iommu_enable_event_buffer(iommu);
  2367. iommu_set_exclusion_range(iommu);
  2368. iommu_enable_gt(iommu);
  2369. iommu_enable_ga(iommu);
  2370. iommu_enable_xt(iommu);
  2371. iommu_enable_irtcachedis(iommu);
  2372. iommu_enable_2k_int(iommu);
  2373. iommu_enable(iommu);
  2374. amd_iommu_flush_all_caches(iommu);
  2375. }
  2376. /*
  2377. * This function finally enables all IOMMUs found in the system after
  2378. * they have been initialized.
  2379. *
  2380. * Or if in kdump kernel and IOMMUs are all pre-enabled, try to reuse
  2381. * the old content of device table entries. Not this case or reuse failed,
  2382. * just continue as normal kernel does.
  2383. */
  2384. static void early_enable_iommus(void)
  2385. {
  2386. struct amd_iommu *iommu;
  2387. struct amd_iommu_pci_seg *pci_seg;
  2388. if (!reuse_device_table()) {
  2389. /*
  2390. * If come here because of failure in reusing device table from old
  2391. * kernel with all IOMMUs enabled, print error message and try to
  2392. * free allocated old_dev_tbl_cpy.
  2393. */
  2394. if (amd_iommu_pre_enabled) {
  2395. pr_err("Failed to reuse DEV table from previous kernel.\n");
  2396. /*
  2397. * Bail out early if unable to remap/reuse DEV table from
  2398. * previous kernel if SNP enabled as IOMMU commands will
  2399. * time out without DEV table and cause kdump boot panic.
  2400. */
  2401. BUG_ON(check_feature(FEATURE_SNP));
  2402. }
  2403. for_each_pci_segment(pci_seg) {
  2404. if (pci_seg->old_dev_tbl_cpy != NULL) {
  2405. memunmap((void *)pci_seg->old_dev_tbl_cpy);
  2406. pci_seg->old_dev_tbl_cpy = NULL;
  2407. }
  2408. }
  2409. for_each_iommu(iommu) {
  2410. clear_translation_pre_enabled(iommu);
  2411. early_enable_iommu(iommu);
  2412. }
  2413. } else {
  2414. pr_info("Reused DEV table from previous kernel.\n");
  2415. for_each_pci_segment(pci_seg) {
  2416. iommu_free_pages(pci_seg->dev_table);
  2417. pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;
  2418. }
  2419. for_each_iommu(iommu) {
  2420. iommu_disable_command_buffer(iommu);
  2421. iommu_disable_event_buffer(iommu);
  2422. iommu_disable_irtcachedis(iommu);
  2423. iommu_enable_command_buffer(iommu);
  2424. iommu_enable_event_buffer(iommu);
  2425. iommu_enable_ga(iommu);
  2426. iommu_enable_xt(iommu);
  2427. iommu_enable_irtcachedis(iommu);
  2428. iommu_enable_2k_int(iommu);
  2429. iommu_set_device_table(iommu);
  2430. amd_iommu_flush_all_caches(iommu);
  2431. }
  2432. }
  2433. }
  2434. static void enable_iommus_ppr(void)
  2435. {
  2436. struct amd_iommu *iommu;
  2437. if (!amd_iommu_gt_ppr_supported())
  2438. return;
  2439. for_each_iommu(iommu)
  2440. amd_iommu_enable_ppr_log(iommu);
  2441. }
  2442. static void enable_iommus_vapic(void)
  2443. {
  2444. #ifdef CONFIG_IRQ_REMAP
  2445. u32 status, i;
  2446. struct amd_iommu *iommu;
  2447. for_each_iommu(iommu) {
  2448. /*
  2449. * Disable GALog if already running. It could have been enabled
  2450. * in the previous boot before kdump.
  2451. */
  2452. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  2453. if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
  2454. continue;
  2455. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  2456. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  2457. /*
  2458. * Need to set and poll check the GALOGRun bit to zero before
  2459. * we can set/ modify GA Log registers safely.
  2460. */
  2461. for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {
  2462. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  2463. if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
  2464. break;
  2465. udelay(10);
  2466. }
  2467. if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))
  2468. return;
  2469. }
  2470. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  2471. !check_feature(FEATURE_GAM_VAPIC)) {
  2472. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2473. return;
  2474. }
  2475. if (amd_iommu_snp_en &&
  2476. !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
  2477. pr_warn("Force to disable Virtual APIC due to SNP\n");
  2478. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2479. return;
  2480. }
  2481. /* Enabling GAM and SNPAVIC support */
  2482. for_each_iommu(iommu) {
  2483. if (iommu_init_ga_log(iommu) ||
  2484. iommu_ga_log_enable(iommu))
  2485. return;
  2486. iommu_feature_enable(iommu, CONTROL_GAM_EN);
  2487. if (amd_iommu_snp_en)
  2488. iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
  2489. }
  2490. amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
  2491. pr_info("Virtual APIC enabled\n");
  2492. #endif
  2493. }
  2494. static void disable_iommus(void)
  2495. {
  2496. struct amd_iommu *iommu;
  2497. for_each_iommu(iommu)
  2498. iommu_disable(iommu);
  2499. #ifdef CONFIG_IRQ_REMAP
  2500. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  2501. amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  2502. #endif
  2503. }
  2504. /*
  2505. * Suspend/Resume support
  2506. * disable suspend until real resume implemented
  2507. */
  2508. static void amd_iommu_resume(void *data)
  2509. {
  2510. struct amd_iommu *iommu;
  2511. for_each_iommu(iommu)
  2512. iommu_apply_resume_quirks(iommu);
  2513. /* re-load the hardware */
  2514. for_each_iommu(iommu)
  2515. early_enable_iommu(iommu);
  2516. amd_iommu_enable_interrupts();
  2517. }
  2518. static int amd_iommu_suspend(void *data)
  2519. {
  2520. /* disable IOMMUs to go out of the way for BIOS */
  2521. disable_iommus();
  2522. return 0;
  2523. }
  2524. static const struct syscore_ops amd_iommu_syscore_ops = {
  2525. .suspend = amd_iommu_suspend,
  2526. .resume = amd_iommu_resume,
  2527. };
  2528. static struct syscore amd_iommu_syscore = {
  2529. .ops = &amd_iommu_syscore_ops,
  2530. };
  2531. static void __init free_iommu_resources(void)
  2532. {
  2533. free_iommu_all();
  2534. free_pci_segments();
  2535. }
  2536. /* SB IOAPIC is always on this device in AMD systems */
  2537. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  2538. static bool __init check_ioapic_information(void)
  2539. {
  2540. const char *fw_bug = FW_BUG;
  2541. bool ret, has_sb_ioapic;
  2542. int idx;
  2543. has_sb_ioapic = false;
  2544. ret = false;
  2545. /*
  2546. * If we have map overrides on the kernel command line the
  2547. * messages in this function might not describe firmware bugs
  2548. * anymore - so be careful
  2549. */
  2550. if (cmdline_maps)
  2551. fw_bug = "";
  2552. for (idx = 0; idx < nr_ioapics; idx++) {
  2553. int devid, id = mpc_ioapic_id(idx);
  2554. devid = get_ioapic_devid(id);
  2555. if (devid < 0) {
  2556. pr_err("%s: IOAPIC[%d] not in IVRS table\n",
  2557. fw_bug, id);
  2558. ret = false;
  2559. } else if (devid == IOAPIC_SB_DEVID) {
  2560. has_sb_ioapic = true;
  2561. ret = true;
  2562. }
  2563. }
  2564. if (!has_sb_ioapic) {
  2565. /*
  2566. * We expect the SB IOAPIC to be listed in the IVRS
  2567. * table. The system timer is connected to the SB IOAPIC
  2568. * and if we don't have it in the list the system will
  2569. * panic at boot time. This situation usually happens
  2570. * when the BIOS is buggy and provides us the wrong
  2571. * device id for the IOAPIC in the system.
  2572. */
  2573. pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
  2574. }
  2575. if (!ret)
  2576. pr_err("Disabling interrupt remapping\n");
  2577. return ret;
  2578. }
  2579. static void __init free_dma_resources(void)
  2580. {
  2581. amd_iommu_pdom_id_destroy();
  2582. free_unity_maps();
  2583. }
  2584. static void __init ivinfo_init(void *ivrs)
  2585. {
  2586. amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
  2587. }
  2588. /*
  2589. * This is the hardware init function for AMD IOMMU in the system.
  2590. * This function is called either from amd_iommu_init or from the interrupt
  2591. * remapping setup code.
  2592. *
  2593. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  2594. * four times:
  2595. *
  2596. * 1 pass) Discover the most comprehensive IVHD type to use.
  2597. *
  2598. * 2 pass) Find the highest PCI device id the driver has to handle.
  2599. * Upon this information the size of the data structures is
  2600. * determined that needs to be allocated.
  2601. *
  2602. * 3 pass) Initialize the data structures just allocated with the
  2603. * information in the ACPI table about available AMD IOMMUs
  2604. * in the system. It also maps the PCI devices in the
  2605. * system to specific IOMMUs
  2606. *
  2607. * 4 pass) After the basic data structures are allocated and
  2608. * initialized we update them with information about memory
  2609. * remapping requirements parsed out of the ACPI table in
  2610. * this last pass.
  2611. *
  2612. * After everything is set up the IOMMUs are enabled and the necessary
  2613. * hotplug and suspend notifiers are registered.
  2614. */
  2615. static int __init early_amd_iommu_init(void)
  2616. {
  2617. struct acpi_table_header *ivrs_base;
  2618. int ret;
  2619. acpi_status status;
  2620. u8 efr_hats;
  2621. if (!amd_iommu_detected)
  2622. return -ENODEV;
  2623. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2624. if (status == AE_NOT_FOUND)
  2625. return -ENODEV;
  2626. else if (ACPI_FAILURE(status)) {
  2627. const char *err = acpi_format_exception(status);
  2628. pr_err("IVRS table error: %s\n", err);
  2629. return -EINVAL;
  2630. }
  2631. if (!boot_cpu_has(X86_FEATURE_CX16)) {
  2632. pr_err("Failed to initialize. The CMPXCHG16B feature is required.\n");
  2633. ret = -EINVAL;
  2634. goto out;
  2635. }
  2636. /*
  2637. * Validate checksum here so we don't need to do it when
  2638. * we actually parse the table
  2639. */
  2640. ret = check_ivrs_checksum(ivrs_base);
  2641. if (ret)
  2642. goto out;
  2643. ivinfo_init(ivrs_base);
  2644. amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
  2645. DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
  2646. /*
  2647. * now the data structures are allocated and basically initialized
  2648. * start the real acpi table scan
  2649. */
  2650. ret = init_iommu_all(ivrs_base);
  2651. if (ret)
  2652. goto out;
  2653. /* 5 level guest page table */
  2654. if (cpu_feature_enabled(X86_FEATURE_LA57) &&
  2655. FIELD_GET(FEATURE_GATS, amd_iommu_efr) == GUEST_PGTABLE_5_LEVEL)
  2656. amd_iommu_gpt_level = PAGE_MODE_5_LEVEL;
  2657. efr_hats = FIELD_GET(FEATURE_HATS, amd_iommu_efr);
  2658. if (efr_hats != 0x3) {
  2659. /*
  2660. * efr[HATS] bits specify the maximum host translation level
  2661. * supported, with LEVEL 4 being initial max level.
  2662. */
  2663. amd_iommu_hpt_level = efr_hats + PAGE_MODE_4_LEVEL;
  2664. } else {
  2665. pr_warn_once(FW_BUG "Disable host address translation due to invalid translation level (%#x).\n",
  2666. efr_hats);
  2667. amd_iommu_hatdis = true;
  2668. }
  2669. if (amd_iommu_pgtable == PD_MODE_V2) {
  2670. if (!amd_iommu_v2_pgtbl_supported()) {
  2671. pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
  2672. amd_iommu_pgtable = PD_MODE_V1;
  2673. }
  2674. }
  2675. if (amd_iommu_hatdis) {
  2676. /*
  2677. * Host (v1) page table is not available. Attempt to use
  2678. * Guest (v2) page table.
  2679. */
  2680. if (amd_iommu_v2_pgtbl_supported())
  2681. amd_iommu_pgtable = PD_MODE_V2;
  2682. else
  2683. amd_iommu_pgtable = PD_MODE_NONE;
  2684. }
  2685. /* Disable any previously enabled IOMMUs */
  2686. if (!is_kdump_kernel() || amd_iommu_disabled)
  2687. disable_iommus();
  2688. if (amd_iommu_irq_remap)
  2689. amd_iommu_irq_remap = check_ioapic_information();
  2690. if (amd_iommu_irq_remap) {
  2691. struct amd_iommu_pci_seg *pci_seg;
  2692. ret = -ENOMEM;
  2693. for_each_pci_segment(pci_seg) {
  2694. if (alloc_irq_lookup_table(pci_seg))
  2695. goto out;
  2696. }
  2697. }
  2698. ret = init_memory_definitions(ivrs_base);
  2699. if (ret)
  2700. goto out;
  2701. /* init the device table */
  2702. init_device_table();
  2703. out:
  2704. /* Don't leak any ACPI memory */
  2705. acpi_put_table(ivrs_base);
  2706. return ret;
  2707. }
  2708. static int amd_iommu_enable_interrupts(void)
  2709. {
  2710. struct amd_iommu *iommu;
  2711. int ret = 0;
  2712. for_each_iommu(iommu) {
  2713. ret = iommu_init_irq(iommu);
  2714. if (ret)
  2715. goto out;
  2716. }
  2717. /*
  2718. * Interrupt handler is ready to process interrupts. Enable
  2719. * PPR and GA log interrupt for all IOMMUs.
  2720. */
  2721. enable_iommus_vapic();
  2722. enable_iommus_ppr();
  2723. out:
  2724. return ret;
  2725. }
  2726. static bool __init detect_ivrs(void)
  2727. {
  2728. struct acpi_table_header *ivrs_base;
  2729. acpi_status status;
  2730. int i;
  2731. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2732. if (status == AE_NOT_FOUND)
  2733. return false;
  2734. else if (ACPI_FAILURE(status)) {
  2735. const char *err = acpi_format_exception(status);
  2736. pr_err("IVRS table error: %s\n", err);
  2737. return false;
  2738. }
  2739. acpi_put_table(ivrs_base);
  2740. if (amd_iommu_force_enable)
  2741. goto out;
  2742. /* Don't use IOMMU if there is Stoney Ridge graphics */
  2743. for (i = 0; i < 32; i++) {
  2744. u32 pci_id;
  2745. pci_id = read_pci_config(0, i, 0, 0);
  2746. if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
  2747. pr_info("Disable IOMMU on Stoney Ridge\n");
  2748. return false;
  2749. }
  2750. }
  2751. out:
  2752. /* Make sure ACS will be enabled during PCI probe */
  2753. pci_request_acs();
  2754. return true;
  2755. }
  2756. static __init void iommu_snp_enable(void)
  2757. {
  2758. #ifdef CONFIG_KVM_AMD_SEV
  2759. if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
  2760. return;
  2761. /*
  2762. * The SNP support requires that IOMMU must be enabled, and is
  2763. * configured with V1 page table (DTE[Mode] = 0 is not supported).
  2764. */
  2765. if (no_iommu || iommu_default_passthrough()) {
  2766. pr_warn("SNP: IOMMU disabled or configured in passthrough mode, SNP cannot be supported.\n");
  2767. goto disable_snp;
  2768. }
  2769. if (amd_iommu_pgtable != PD_MODE_V1) {
  2770. pr_warn("SNP: IOMMU is configured with V2 page table mode, SNP cannot be supported.\n");
  2771. goto disable_snp;
  2772. }
  2773. amd_iommu_snp_en = check_feature(FEATURE_SNP);
  2774. if (!amd_iommu_snp_en) {
  2775. pr_warn("SNP: IOMMU SNP feature not enabled, SNP cannot be supported.\n");
  2776. goto disable_snp;
  2777. }
  2778. /*
  2779. * Enable host SNP support once SNP support is checked on IOMMU.
  2780. */
  2781. if (snp_rmptable_init()) {
  2782. pr_warn("SNP: RMP initialization failed, SNP cannot be supported.\n");
  2783. goto disable_snp;
  2784. }
  2785. pr_info("IOMMU SNP support enabled.\n");
  2786. return;
  2787. disable_snp:
  2788. cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
  2789. #endif
  2790. }
  2791. /****************************************************************************
  2792. *
  2793. * AMD IOMMU Initialization State Machine
  2794. *
  2795. ****************************************************************************/
  2796. static int __init state_next(void)
  2797. {
  2798. int ret = 0;
  2799. switch (init_state) {
  2800. case IOMMU_START_STATE:
  2801. if (!detect_ivrs()) {
  2802. init_state = IOMMU_NOT_FOUND;
  2803. ret = -ENODEV;
  2804. } else {
  2805. init_state = IOMMU_IVRS_DETECTED;
  2806. }
  2807. break;
  2808. case IOMMU_IVRS_DETECTED:
  2809. if (amd_iommu_disabled) {
  2810. init_state = IOMMU_CMDLINE_DISABLED;
  2811. ret = -EINVAL;
  2812. } else {
  2813. ret = early_amd_iommu_init();
  2814. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  2815. }
  2816. break;
  2817. case IOMMU_ACPI_FINISHED:
  2818. early_enable_iommus();
  2819. x86_platform.iommu_shutdown = disable_iommus;
  2820. init_state = IOMMU_ENABLED;
  2821. break;
  2822. case IOMMU_ENABLED:
  2823. register_syscore(&amd_iommu_syscore);
  2824. iommu_snp_enable();
  2825. ret = amd_iommu_init_pci();
  2826. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  2827. break;
  2828. case IOMMU_PCI_INIT:
  2829. ret = amd_iommu_enable_interrupts();
  2830. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  2831. break;
  2832. case IOMMU_INTERRUPTS_EN:
  2833. init_state = IOMMU_INITIALIZED;
  2834. break;
  2835. case IOMMU_INITIALIZED:
  2836. /* Nothing to do */
  2837. break;
  2838. case IOMMU_NOT_FOUND:
  2839. case IOMMU_INIT_ERROR:
  2840. case IOMMU_CMDLINE_DISABLED:
  2841. /* Error states => do nothing */
  2842. ret = -EINVAL;
  2843. break;
  2844. default:
  2845. /* Unknown state */
  2846. BUG();
  2847. }
  2848. if (ret) {
  2849. free_dma_resources();
  2850. if (!irq_remapping_enabled) {
  2851. disable_iommus();
  2852. free_iommu_resources();
  2853. } else {
  2854. struct amd_iommu *iommu;
  2855. struct amd_iommu_pci_seg *pci_seg;
  2856. for_each_pci_segment(pci_seg)
  2857. uninit_device_table_dma(pci_seg);
  2858. for_each_iommu(iommu)
  2859. amd_iommu_flush_all_caches(iommu);
  2860. }
  2861. }
  2862. return ret;
  2863. }
  2864. static int __init iommu_go_to_state(enum iommu_init_state state)
  2865. {
  2866. int ret = -EINVAL;
  2867. while (init_state != state) {
  2868. if (init_state == IOMMU_NOT_FOUND ||
  2869. init_state == IOMMU_INIT_ERROR ||
  2870. init_state == IOMMU_CMDLINE_DISABLED)
  2871. break;
  2872. ret = state_next();
  2873. }
  2874. /*
  2875. * SNP platform initilazation requires IOMMUs to be fully configured.
  2876. * If the SNP support on IOMMUs has NOT been checked, simply mark SNP
  2877. * as unsupported. If the SNP support on IOMMUs has been checked and
  2878. * host SNP support enabled but RMP enforcement has not been enabled
  2879. * in IOMMUs, then the system is in a half-baked state, but can limp
  2880. * along as all memory should be Hypervisor-Owned in the RMP. WARN,
  2881. * but leave SNP as "supported" to avoid confusing the kernel.
  2882. */
  2883. if (ret && cc_platform_has(CC_ATTR_HOST_SEV_SNP) &&
  2884. !WARN_ON_ONCE(amd_iommu_snp_en))
  2885. cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
  2886. return ret;
  2887. }
  2888. #ifdef CONFIG_IRQ_REMAP
  2889. int __init amd_iommu_prepare(void)
  2890. {
  2891. int ret;
  2892. amd_iommu_irq_remap = true;
  2893. ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
  2894. if (ret) {
  2895. amd_iommu_irq_remap = false;
  2896. return ret;
  2897. }
  2898. return amd_iommu_irq_remap ? 0 : -ENODEV;
  2899. }
  2900. int __init amd_iommu_enable(void)
  2901. {
  2902. int ret;
  2903. ret = iommu_go_to_state(IOMMU_ENABLED);
  2904. if (ret)
  2905. return ret;
  2906. irq_remapping_enabled = 1;
  2907. return amd_iommu_xt_mode;
  2908. }
  2909. void amd_iommu_disable(void)
  2910. {
  2911. amd_iommu_suspend(NULL);
  2912. }
  2913. int amd_iommu_reenable(int mode)
  2914. {
  2915. amd_iommu_resume(NULL);
  2916. return 0;
  2917. }
  2918. int amd_iommu_enable_faulting(unsigned int cpu)
  2919. {
  2920. /* We enable MSI later when PCI is initialized */
  2921. return 0;
  2922. }
  2923. #endif
  2924. /*
  2925. * This is the core init function for AMD IOMMU hardware in the system.
  2926. * This function is called from the generic x86 DMA layer initialization
  2927. * code.
  2928. */
  2929. static int __init amd_iommu_init(void)
  2930. {
  2931. int ret;
  2932. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  2933. #ifdef CONFIG_GART_IOMMU
  2934. if (ret && list_empty(&amd_iommu_list)) {
  2935. /*
  2936. * We failed to initialize the AMD IOMMU - try fallback
  2937. * to GART if possible.
  2938. */
  2939. gart_iommu_init();
  2940. }
  2941. #endif
  2942. if (!ret)
  2943. amd_iommu_debugfs_setup();
  2944. return ret;
  2945. }
  2946. static bool amd_iommu_sme_check(void)
  2947. {
  2948. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
  2949. (boot_cpu_data.x86 != 0x17))
  2950. return true;
  2951. /* For Fam17h, a specific level of support is required */
  2952. if (boot_cpu_data.microcode >= 0x08001205)
  2953. return true;
  2954. if ((boot_cpu_data.microcode >= 0x08001126) &&
  2955. (boot_cpu_data.microcode <= 0x080011ff))
  2956. return true;
  2957. pr_notice("IOMMU not currently supported when SME is active\n");
  2958. return false;
  2959. }
  2960. /****************************************************************************
  2961. *
  2962. * Early detect code. This code runs at IOMMU detection time in the DMA
  2963. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  2964. * IOMMUs
  2965. *
  2966. ****************************************************************************/
  2967. void __init amd_iommu_detect(void)
  2968. {
  2969. int ret;
  2970. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  2971. goto disable_snp;
  2972. if (!amd_iommu_sme_check())
  2973. goto disable_snp;
  2974. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  2975. if (ret)
  2976. goto disable_snp;
  2977. amd_iommu_detected = true;
  2978. iommu_detected = 1;
  2979. x86_init.iommu.iommu_init = amd_iommu_init;
  2980. return;
  2981. disable_snp:
  2982. if (cc_platform_has(CC_ATTR_HOST_SEV_SNP))
  2983. cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
  2984. }
  2985. /****************************************************************************
  2986. *
  2987. * Parsing functions for the AMD IOMMU specific kernel command line
  2988. * options.
  2989. *
  2990. ****************************************************************************/
  2991. static int __init parse_amd_iommu_dump(char *str)
  2992. {
  2993. amd_iommu_dump = true;
  2994. return 1;
  2995. }
  2996. static int __init parse_amd_iommu_intr(char *str)
  2997. {
  2998. for (; *str; ++str) {
  2999. if (strncmp(str, "legacy", 6) == 0) {
  3000. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  3001. break;
  3002. }
  3003. if (strncmp(str, "vapic", 5) == 0) {
  3004. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  3005. break;
  3006. }
  3007. }
  3008. return 1;
  3009. }
  3010. static int __init parse_amd_iommu_options(char *str)
  3011. {
  3012. if (!str)
  3013. return -EINVAL;
  3014. while (*str) {
  3015. if (strncmp(str, "fullflush", 9) == 0) {
  3016. pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
  3017. iommu_set_dma_strict();
  3018. } else if (strncmp(str, "force_enable", 12) == 0) {
  3019. amd_iommu_force_enable = true;
  3020. } else if (strncmp(str, "off", 3) == 0) {
  3021. amd_iommu_disabled = true;
  3022. } else if (strncmp(str, "force_isolation", 15) == 0) {
  3023. amd_iommu_force_isolation = true;
  3024. } else if (strncmp(str, "pgtbl_v1", 8) == 0) {
  3025. amd_iommu_pgtable = PD_MODE_V1;
  3026. } else if (strncmp(str, "pgtbl_v2", 8) == 0) {
  3027. amd_iommu_pgtable = PD_MODE_V2;
  3028. } else if (strncmp(str, "irtcachedis", 11) == 0) {
  3029. amd_iommu_irtcachedis = true;
  3030. } else if (strncmp(str, "nohugepages", 11) == 0) {
  3031. pr_info("Restricting V1 page-sizes to 4KiB");
  3032. amd_iommu_pgsize_bitmap = AMD_IOMMU_PGSIZES_4K;
  3033. } else if (strncmp(str, "v2_pgsizes_only", 15) == 0) {
  3034. pr_info("Restricting V1 page-sizes to 4KiB/2MiB/1GiB");
  3035. amd_iommu_pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
  3036. } else {
  3037. pr_notice("Unknown option - '%s'\n", str);
  3038. }
  3039. str += strcspn(str, ",");
  3040. while (*str == ',')
  3041. str++;
  3042. }
  3043. return 1;
  3044. }
  3045. static int __init parse_ivrs_ioapic(char *str)
  3046. {
  3047. u32 seg = 0, bus, dev, fn;
  3048. int id, i;
  3049. u32 devid;
  3050. if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  3051. sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
  3052. goto found;
  3053. if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  3054. sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
  3055. pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
  3056. str, id, seg, bus, dev, fn);
  3057. goto found;
  3058. }
  3059. pr_err("Invalid command line: ivrs_ioapic%s\n", str);
  3060. return 1;
  3061. found:
  3062. if (early_ioapic_map_size == EARLY_MAP_SIZE) {
  3063. pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
  3064. str);
  3065. return 1;
  3066. }
  3067. devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  3068. cmdline_maps = true;
  3069. i = early_ioapic_map_size++;
  3070. early_ioapic_map[i].id = id;
  3071. early_ioapic_map[i].devid = devid;
  3072. early_ioapic_map[i].cmd_line = true;
  3073. return 1;
  3074. }
  3075. static int __init parse_ivrs_hpet(char *str)
  3076. {
  3077. u32 seg = 0, bus, dev, fn;
  3078. int id, i;
  3079. u32 devid;
  3080. if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  3081. sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
  3082. goto found;
  3083. if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  3084. sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
  3085. pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
  3086. str, id, seg, bus, dev, fn);
  3087. goto found;
  3088. }
  3089. pr_err("Invalid command line: ivrs_hpet%s\n", str);
  3090. return 1;
  3091. found:
  3092. if (early_hpet_map_size == EARLY_MAP_SIZE) {
  3093. pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
  3094. str);
  3095. return 1;
  3096. }
  3097. devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  3098. cmdline_maps = true;
  3099. i = early_hpet_map_size++;
  3100. early_hpet_map[i].id = id;
  3101. early_hpet_map[i].devid = devid;
  3102. early_hpet_map[i].cmd_line = true;
  3103. return 1;
  3104. }
  3105. #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
  3106. static int __init parse_ivrs_acpihid(char *str)
  3107. {
  3108. u32 seg = 0, bus, dev, fn;
  3109. char *hid, *uid, *p, *addr;
  3110. char acpiid[ACPIID_LEN + 1] = { }; /* size with NULL terminator */
  3111. int i;
  3112. addr = strchr(str, '@');
  3113. if (!addr) {
  3114. addr = strchr(str, '=');
  3115. if (!addr)
  3116. goto not_found;
  3117. ++addr;
  3118. if (strlen(addr) > ACPIID_LEN)
  3119. goto not_found;
  3120. if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
  3121. sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
  3122. pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
  3123. str, acpiid, seg, bus, dev, fn);
  3124. goto found;
  3125. }
  3126. goto not_found;
  3127. }
  3128. /* We have the '@', make it the terminator to get just the acpiid */
  3129. *addr++ = 0;
  3130. if (strlen(str) > ACPIID_LEN)
  3131. goto not_found;
  3132. if (sscanf(str, "=%s", acpiid) != 1)
  3133. goto not_found;
  3134. if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
  3135. sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
  3136. goto found;
  3137. not_found:
  3138. pr_err("Invalid command line: ivrs_acpihid%s\n", str);
  3139. return 1;
  3140. found:
  3141. p = acpiid;
  3142. hid = strsep(&p, ":");
  3143. uid = p;
  3144. if (!hid || !(*hid) || !uid) {
  3145. pr_err("Invalid command line: hid or uid\n");
  3146. return 1;
  3147. }
  3148. /*
  3149. * Ignore leading zeroes after ':', so e.g., AMDI0095:00
  3150. * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
  3151. */
  3152. while (*uid == '0' && *(uid + 1))
  3153. uid++;
  3154. if (strlen(hid) >= ACPIHID_HID_LEN) {
  3155. pr_err("Invalid command line: hid is too long\n");
  3156. return 1;
  3157. } else if (strlen(uid) >= ACPIHID_UID_LEN) {
  3158. pr_err("Invalid command line: uid is too long\n");
  3159. return 1;
  3160. }
  3161. i = early_acpihid_map_size++;
  3162. memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
  3163. memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
  3164. early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  3165. early_acpihid_map[i].cmd_line = true;
  3166. return 1;
  3167. }
  3168. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  3169. __setup("amd_iommu=", parse_amd_iommu_options);
  3170. __setup("amd_iommu_intr=", parse_amd_iommu_intr);
  3171. __setup("ivrs_ioapic", parse_ivrs_ioapic);
  3172. __setup("ivrs_hpet", parse_ivrs_hpet);
  3173. __setup("ivrs_acpihid", parse_ivrs_acpihid);
  3174. bool amd_iommu_pasid_supported(void)
  3175. {
  3176. /* CPU page table size should match IOMMU guest page table size */
  3177. if (cpu_feature_enabled(X86_FEATURE_LA57) &&
  3178. amd_iommu_gpt_level != PAGE_MODE_5_LEVEL)
  3179. return false;
  3180. /*
  3181. * Since DTE[Mode]=0 is prohibited on SNP-enabled system
  3182. * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without
  3183. * setting up IOMMUv1 page table.
  3184. */
  3185. return amd_iommu_gt_ppr_supported() && !amd_iommu_snp_en;
  3186. }
  3187. struct amd_iommu *get_amd_iommu(unsigned int idx)
  3188. {
  3189. unsigned int i = 0;
  3190. struct amd_iommu *iommu;
  3191. for_each_iommu(iommu)
  3192. if (i++ == idx)
  3193. return iommu;
  3194. return NULL;
  3195. }
  3196. /****************************************************************************
  3197. *
  3198. * IOMMU EFR Performance Counter support functionality. This code allows
  3199. * access to the IOMMU PC functionality.
  3200. *
  3201. ****************************************************************************/
  3202. u8 amd_iommu_pc_get_max_banks(unsigned int idx)
  3203. {
  3204. struct amd_iommu *iommu = get_amd_iommu(idx);
  3205. if (iommu)
  3206. return iommu->max_banks;
  3207. return 0;
  3208. }
  3209. bool amd_iommu_pc_supported(void)
  3210. {
  3211. return amd_iommu_pc_present;
  3212. }
  3213. u8 amd_iommu_pc_get_max_counters(unsigned int idx)
  3214. {
  3215. struct amd_iommu *iommu = get_amd_iommu(idx);
  3216. if (iommu)
  3217. return iommu->max_counters;
  3218. return 0;
  3219. }
  3220. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  3221. u8 fxn, u64 *value, bool is_write)
  3222. {
  3223. u32 offset;
  3224. u32 max_offset_lim;
  3225. /* Make sure the IOMMU PC resource is available */
  3226. if (!amd_iommu_pc_present)
  3227. return -ENODEV;
  3228. /* Check for valid iommu and pc register indexing */
  3229. if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
  3230. return -ENODEV;
  3231. offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
  3232. /* Limit the offset to the hw defined mmio region aperture */
  3233. max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
  3234. (iommu->max_counters << 8) | 0x28);
  3235. if ((offset < MMIO_CNTR_REG_OFFSET) ||
  3236. (offset > max_offset_lim))
  3237. return -EINVAL;
  3238. if (is_write) {
  3239. u64 val = *value & GENMASK_ULL(47, 0);
  3240. writel((u32)val, iommu->mmio_base + offset);
  3241. writel((val >> 32), iommu->mmio_base + offset + 4);
  3242. } else {
  3243. *value = readl(iommu->mmio_base + offset + 4);
  3244. *value <<= 32;
  3245. *value |= readl(iommu->mmio_base + offset);
  3246. *value &= GENMASK_ULL(47, 0);
  3247. }
  3248. return 0;
  3249. }
  3250. int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  3251. {
  3252. if (!iommu)
  3253. return -EINVAL;
  3254. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
  3255. }
  3256. int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  3257. {
  3258. if (!iommu)
  3259. return -EINVAL;
  3260. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
  3261. }
  3262. #ifdef CONFIG_KVM_AMD_SEV
  3263. static int iommu_page_make_shared(void *page)
  3264. {
  3265. unsigned long paddr, pfn;
  3266. paddr = iommu_virt_to_phys(page);
  3267. /* Cbit maybe set in the paddr */
  3268. pfn = __sme_clr(paddr) >> PAGE_SHIFT;
  3269. if (!(pfn % PTRS_PER_PMD)) {
  3270. int ret, level;
  3271. bool assigned;
  3272. ret = snp_lookup_rmpentry(pfn, &assigned, &level);
  3273. if (ret) {
  3274. pr_warn("IOMMU PFN %lx RMP lookup failed, ret %d\n", pfn, ret);
  3275. return ret;
  3276. }
  3277. if (!assigned) {
  3278. pr_warn("IOMMU PFN %lx not assigned in RMP table\n", pfn);
  3279. return -EINVAL;
  3280. }
  3281. if (level > PG_LEVEL_4K) {
  3282. ret = psmash(pfn);
  3283. if (!ret)
  3284. goto done;
  3285. pr_warn("PSMASH failed for IOMMU PFN %lx huge RMP entry, ret: %d, level: %d\n",
  3286. pfn, ret, level);
  3287. return ret;
  3288. }
  3289. }
  3290. done:
  3291. return rmp_make_shared(pfn, PG_LEVEL_4K);
  3292. }
  3293. static int iommu_make_shared(void *va, size_t size)
  3294. {
  3295. void *page;
  3296. int ret;
  3297. if (!va)
  3298. return 0;
  3299. for (page = va; page < (va + size); page += PAGE_SIZE) {
  3300. ret = iommu_page_make_shared(page);
  3301. if (ret)
  3302. return ret;
  3303. }
  3304. return 0;
  3305. }
  3306. int amd_iommu_snp_disable(void)
  3307. {
  3308. struct amd_iommu *iommu;
  3309. int ret;
  3310. if (!amd_iommu_snp_en)
  3311. return 0;
  3312. for_each_iommu(iommu) {
  3313. ret = iommu_make_shared(iommu->evt_buf, EVT_BUFFER_SIZE);
  3314. if (ret)
  3315. return ret;
  3316. ret = iommu_make_shared(iommu->ppr_log, PPR_LOG_SIZE);
  3317. if (ret)
  3318. return ret;
  3319. ret = iommu_make_shared((void *)iommu->cmd_sem, PAGE_SIZE);
  3320. if (ret)
  3321. return ret;
  3322. }
  3323. return 0;
  3324. }
  3325. EXPORT_SYMBOL_GPL(amd_iommu_snp_disable);
  3326. bool amd_iommu_sev_tio_supported(void)
  3327. {
  3328. return check_feature2(FEATURE_SEVSNPIO_SUP);
  3329. }
  3330. EXPORT_SYMBOL_GPL(amd_iommu_sev_tio_supported);
  3331. #endif