init.c 94 KB

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