amd_iommu_init.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/acpi.h>
  21. #include <linux/list.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/syscore_ops.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/msi.h>
  27. #include <linux/amd-iommu.h>
  28. #include <linux/export.h>
  29. #include <linux/iommu.h>
  30. #include <linux/kmemleak.h>
  31. #include <linux/mem_encrypt.h>
  32. #include <asm/pci-direct.h>
  33. #include <asm/iommu.h>
  34. #include <asm/gart.h>
  35. #include <asm/x86_init.h>
  36. #include <asm/iommu_table.h>
  37. #include <asm/io_apic.h>
  38. #include <asm/irq_remapping.h>
  39. #include <linux/crash_dump.h>
  40. #include "amd_iommu_proto.h"
  41. #include "amd_iommu_types.h"
  42. #include "irq_remapping.h"
  43. /*
  44. * definitions for the ACPI scanning code
  45. */
  46. #define IVRS_HEADER_LENGTH 48
  47. #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
  48. #define ACPI_IVMD_TYPE_ALL 0x20
  49. #define ACPI_IVMD_TYPE 0x21
  50. #define ACPI_IVMD_TYPE_RANGE 0x22
  51. #define IVHD_DEV_ALL 0x01
  52. #define IVHD_DEV_SELECT 0x02
  53. #define IVHD_DEV_SELECT_RANGE_START 0x03
  54. #define IVHD_DEV_RANGE_END 0x04
  55. #define IVHD_DEV_ALIAS 0x42
  56. #define IVHD_DEV_ALIAS_RANGE 0x43
  57. #define IVHD_DEV_EXT_SELECT 0x46
  58. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  59. #define IVHD_DEV_SPECIAL 0x48
  60. #define IVHD_DEV_ACPI_HID 0xf0
  61. #define UID_NOT_PRESENT 0
  62. #define UID_IS_INTEGER 1
  63. #define UID_IS_CHARACTER 2
  64. #define IVHD_SPECIAL_IOAPIC 1
  65. #define IVHD_SPECIAL_HPET 2
  66. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  67. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  68. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  69. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  70. #define IVMD_FLAG_EXCL_RANGE 0x08
  71. #define IVMD_FLAG_UNITY_MAP 0x01
  72. #define ACPI_DEVFLAG_INITPASS 0x01
  73. #define ACPI_DEVFLAG_EXTINT 0x02
  74. #define ACPI_DEVFLAG_NMI 0x04
  75. #define ACPI_DEVFLAG_SYSMGT1 0x10
  76. #define ACPI_DEVFLAG_SYSMGT2 0x20
  77. #define ACPI_DEVFLAG_LINT0 0x40
  78. #define ACPI_DEVFLAG_LINT1 0x80
  79. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  80. #define LOOP_TIMEOUT 100000
  81. /*
  82. * ACPI table definitions
  83. *
  84. * These data structures are laid over the table to parse the important values
  85. * out of it.
  86. */
  87. extern const struct iommu_ops amd_iommu_ops;
  88. /*
  89. * structure describing one IOMMU in the ACPI table. Typically followed by one
  90. * or more ivhd_entrys.
  91. */
  92. struct ivhd_header {
  93. u8 type;
  94. u8 flags;
  95. u16 length;
  96. u16 devid;
  97. u16 cap_ptr;
  98. u64 mmio_phys;
  99. u16 pci_seg;
  100. u16 info;
  101. u32 efr_attr;
  102. /* Following only valid on IVHD type 11h and 40h */
  103. u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
  104. u64 res;
  105. } __attribute__((packed));
  106. /*
  107. * A device entry describing which devices a specific IOMMU translates and
  108. * which requestor ids they use.
  109. */
  110. struct ivhd_entry {
  111. u8 type;
  112. u16 devid;
  113. u8 flags;
  114. u32 ext;
  115. u32 hidh;
  116. u64 cid;
  117. u8 uidf;
  118. u8 uidl;
  119. u8 uid;
  120. } __attribute__((packed));
  121. /*
  122. * An AMD IOMMU memory definition structure. It defines things like exclusion
  123. * ranges for devices and regions that should be unity mapped.
  124. */
  125. struct ivmd_header {
  126. u8 type;
  127. u8 flags;
  128. u16 length;
  129. u16 devid;
  130. u16 aux;
  131. u64 resv;
  132. u64 range_start;
  133. u64 range_length;
  134. } __attribute__((packed));
  135. bool amd_iommu_dump;
  136. bool amd_iommu_irq_remap __read_mostly;
  137. int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  138. static int amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
  139. static bool amd_iommu_detected;
  140. static bool __initdata amd_iommu_disabled;
  141. static int amd_iommu_target_ivhd_type;
  142. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  143. to handle */
  144. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  145. we find in ACPI */
  146. bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
  147. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  148. system */
  149. /* Array to assign indices to IOMMUs*/
  150. struct amd_iommu *amd_iommus[MAX_IOMMUS];
  151. /* Number of IOMMUs present in the system */
  152. static int amd_iommus_present;
  153. /* IOMMUs have a non-present cache? */
  154. bool amd_iommu_np_cache __read_mostly;
  155. bool amd_iommu_iotlb_sup __read_mostly = true;
  156. u32 amd_iommu_max_pasid __read_mostly = ~0;
  157. bool amd_iommu_v2_present __read_mostly;
  158. static bool amd_iommu_pc_present __read_mostly;
  159. bool amd_iommu_force_isolation __read_mostly;
  160. /*
  161. * List of protection domains - used during resume
  162. */
  163. LIST_HEAD(amd_iommu_pd_list);
  164. spinlock_t amd_iommu_pd_lock;
  165. /*
  166. * Pointer to the device table which is shared by all AMD IOMMUs
  167. * it is indexed by the PCI device id or the HT unit id and contains
  168. * information about the domain the device belongs to as well as the
  169. * page table root pointer.
  170. */
  171. struct dev_table_entry *amd_iommu_dev_table;
  172. /*
  173. * Pointer to a device table which the content of old device table
  174. * will be copied to. It's only be used in kdump kernel.
  175. */
  176. static struct dev_table_entry *old_dev_tbl_cpy;
  177. /*
  178. * The alias table is a driver specific data structure which contains the
  179. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  180. * More than one device can share the same requestor id.
  181. */
  182. u16 *amd_iommu_alias_table;
  183. /*
  184. * The rlookup table is used to find the IOMMU which is responsible
  185. * for a specific device. It is also indexed by the PCI device id.
  186. */
  187. struct amd_iommu **amd_iommu_rlookup_table;
  188. EXPORT_SYMBOL(amd_iommu_rlookup_table);
  189. /*
  190. * This table is used to find the irq remapping table for a given device id
  191. * quickly.
  192. */
  193. struct irq_remap_table **irq_lookup_table;
  194. /*
  195. * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
  196. * to know which ones are already in use.
  197. */
  198. unsigned long *amd_iommu_pd_alloc_bitmap;
  199. static u32 dev_table_size; /* size of the device table */
  200. static u32 alias_table_size; /* size of the alias table */
  201. static u32 rlookup_table_size; /* size if the rlookup table */
  202. enum iommu_init_state {
  203. IOMMU_START_STATE,
  204. IOMMU_IVRS_DETECTED,
  205. IOMMU_ACPI_FINISHED,
  206. IOMMU_ENABLED,
  207. IOMMU_PCI_INIT,
  208. IOMMU_INTERRUPTS_EN,
  209. IOMMU_DMA_OPS,
  210. IOMMU_INITIALIZED,
  211. IOMMU_NOT_FOUND,
  212. IOMMU_INIT_ERROR,
  213. IOMMU_CMDLINE_DISABLED,
  214. };
  215. /* Early ioapic and hpet maps from kernel command line */
  216. #define EARLY_MAP_SIZE 4
  217. static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
  218. static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
  219. static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
  220. static int __initdata early_ioapic_map_size;
  221. static int __initdata early_hpet_map_size;
  222. static int __initdata early_acpihid_map_size;
  223. static bool __initdata cmdline_maps;
  224. static enum iommu_init_state init_state = IOMMU_START_STATE;
  225. static int amd_iommu_enable_interrupts(void);
  226. static int __init iommu_go_to_state(enum iommu_init_state state);
  227. static void init_device_table_dma(void);
  228. static bool amd_iommu_pre_enabled = true;
  229. bool translation_pre_enabled(struct amd_iommu *iommu)
  230. {
  231. return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
  232. }
  233. EXPORT_SYMBOL(translation_pre_enabled);
  234. static void clear_translation_pre_enabled(struct amd_iommu *iommu)
  235. {
  236. iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  237. }
  238. static void init_translation_status(struct amd_iommu *iommu)
  239. {
  240. u64 ctrl;
  241. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  242. if (ctrl & (1<<CONTROL_IOMMU_EN))
  243. iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  244. }
  245. static inline void update_last_devid(u16 devid)
  246. {
  247. if (devid > amd_iommu_last_bdf)
  248. amd_iommu_last_bdf = devid;
  249. }
  250. static inline unsigned long tbl_size(int entry_size)
  251. {
  252. unsigned shift = PAGE_SHIFT +
  253. get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
  254. return 1UL << shift;
  255. }
  256. int amd_iommu_get_num_iommus(void)
  257. {
  258. return amd_iommus_present;
  259. }
  260. /* Access to l1 and l2 indexed register spaces */
  261. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  262. {
  263. u32 val;
  264. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  265. pci_read_config_dword(iommu->dev, 0xfc, &val);
  266. return val;
  267. }
  268. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  269. {
  270. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  271. pci_write_config_dword(iommu->dev, 0xfc, val);
  272. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  273. }
  274. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  275. {
  276. u32 val;
  277. pci_write_config_dword(iommu->dev, 0xf0, address);
  278. pci_read_config_dword(iommu->dev, 0xf4, &val);
  279. return val;
  280. }
  281. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  282. {
  283. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  284. pci_write_config_dword(iommu->dev, 0xf4, val);
  285. }
  286. /****************************************************************************
  287. *
  288. * AMD IOMMU MMIO register space handling functions
  289. *
  290. * These functions are used to program the IOMMU device registers in
  291. * MMIO space required for that driver.
  292. *
  293. ****************************************************************************/
  294. /*
  295. * This function set the exclusion range in the IOMMU. DMA accesses to the
  296. * exclusion range are passed through untranslated
  297. */
  298. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  299. {
  300. u64 start = iommu->exclusion_start & PAGE_MASK;
  301. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  302. u64 entry;
  303. if (!iommu->exclusion_start)
  304. return;
  305. entry = start | MMIO_EXCL_ENABLE_MASK;
  306. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  307. &entry, sizeof(entry));
  308. entry = limit;
  309. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  310. &entry, sizeof(entry));
  311. }
  312. /* Programs the physical address of the device table into the IOMMU hardware */
  313. static void iommu_set_device_table(struct amd_iommu *iommu)
  314. {
  315. u64 entry;
  316. BUG_ON(iommu->mmio_base == NULL);
  317. entry = iommu_virt_to_phys(amd_iommu_dev_table);
  318. entry |= (dev_table_size >> 12) - 1;
  319. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  320. &entry, sizeof(entry));
  321. }
  322. /* Generic functions to enable/disable certain features of the IOMMU. */
  323. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  324. {
  325. u64 ctrl;
  326. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  327. ctrl |= (1ULL << bit);
  328. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  329. }
  330. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  331. {
  332. u64 ctrl;
  333. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  334. ctrl &= ~(1ULL << bit);
  335. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  336. }
  337. static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
  338. {
  339. u64 ctrl;
  340. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  341. ctrl &= ~CTRL_INV_TO_MASK;
  342. ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
  343. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  344. }
  345. /* Function to enable the hardware */
  346. static void iommu_enable(struct amd_iommu *iommu)
  347. {
  348. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  349. }
  350. static void iommu_disable(struct amd_iommu *iommu)
  351. {
  352. /* Disable command buffer */
  353. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  354. /* Disable event logging and event interrupts */
  355. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  356. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  357. /* Disable IOMMU GA_LOG */
  358. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  359. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  360. /* Disable IOMMU hardware itself */
  361. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  362. }
  363. /*
  364. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  365. * the system has one.
  366. */
  367. static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
  368. {
  369. if (!request_mem_region(address, end, "amd_iommu")) {
  370. pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
  371. address, end);
  372. pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
  373. return NULL;
  374. }
  375. return (u8 __iomem *)ioremap_nocache(address, end);
  376. }
  377. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  378. {
  379. if (iommu->mmio_base)
  380. iounmap(iommu->mmio_base);
  381. release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
  382. }
  383. static inline u32 get_ivhd_header_size(struct ivhd_header *h)
  384. {
  385. u32 size = 0;
  386. switch (h->type) {
  387. case 0x10:
  388. size = 24;
  389. break;
  390. case 0x11:
  391. case 0x40:
  392. size = 40;
  393. break;
  394. }
  395. return size;
  396. }
  397. /****************************************************************************
  398. *
  399. * The functions below belong to the first pass of AMD IOMMU ACPI table
  400. * parsing. In this pass we try to find out the highest device id this
  401. * code has to handle. Upon this information the size of the shared data
  402. * structures is determined later.
  403. *
  404. ****************************************************************************/
  405. /*
  406. * This function calculates the length of a given IVHD entry
  407. */
  408. static inline int ivhd_entry_length(u8 *ivhd)
  409. {
  410. u32 type = ((struct ivhd_entry *)ivhd)->type;
  411. if (type < 0x80) {
  412. return 0x04 << (*ivhd >> 6);
  413. } else if (type == IVHD_DEV_ACPI_HID) {
  414. /* For ACPI_HID, offset 21 is uid len */
  415. return *((u8 *)ivhd + 21) + 22;
  416. }
  417. return 0;
  418. }
  419. /*
  420. * After reading the highest device id from the IOMMU PCI capability header
  421. * this function looks if there is a higher device id defined in the ACPI table
  422. */
  423. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  424. {
  425. u8 *p = (void *)h, *end = (void *)h;
  426. struct ivhd_entry *dev;
  427. u32 ivhd_size = get_ivhd_header_size(h);
  428. if (!ivhd_size) {
  429. pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
  430. return -EINVAL;
  431. }
  432. p += ivhd_size;
  433. end += h->length;
  434. while (p < end) {
  435. dev = (struct ivhd_entry *)p;
  436. switch (dev->type) {
  437. case IVHD_DEV_ALL:
  438. /* Use maximum BDF value for DEV_ALL */
  439. update_last_devid(0xffff);
  440. break;
  441. case IVHD_DEV_SELECT:
  442. case IVHD_DEV_RANGE_END:
  443. case IVHD_DEV_ALIAS:
  444. case IVHD_DEV_EXT_SELECT:
  445. /* all the above subfield types refer to device ids */
  446. update_last_devid(dev->devid);
  447. break;
  448. default:
  449. break;
  450. }
  451. p += ivhd_entry_length(p);
  452. }
  453. WARN_ON(p != end);
  454. return 0;
  455. }
  456. static int __init check_ivrs_checksum(struct acpi_table_header *table)
  457. {
  458. int i;
  459. u8 checksum = 0, *p = (u8 *)table;
  460. for (i = 0; i < table->length; ++i)
  461. checksum += p[i];
  462. if (checksum != 0) {
  463. /* ACPI table corrupt */
  464. pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
  465. return -ENODEV;
  466. }
  467. return 0;
  468. }
  469. /*
  470. * Iterate over all IVHD entries in the ACPI table and find the highest device
  471. * id which we need to handle. This is the first of three functions which parse
  472. * the ACPI table. So we check the checksum here.
  473. */
  474. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  475. {
  476. u8 *p = (u8 *)table, *end = (u8 *)table;
  477. struct ivhd_header *h;
  478. p += IVRS_HEADER_LENGTH;
  479. end += table->length;
  480. while (p < end) {
  481. h = (struct ivhd_header *)p;
  482. if (h->type == amd_iommu_target_ivhd_type) {
  483. int ret = find_last_devid_from_ivhd(h);
  484. if (ret)
  485. return ret;
  486. }
  487. p += h->length;
  488. }
  489. WARN_ON(p != end);
  490. return 0;
  491. }
  492. /****************************************************************************
  493. *
  494. * The following functions belong to the code path which parses the ACPI table
  495. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  496. * data structures, initialize the device/alias/rlookup table and also
  497. * basically initialize the hardware.
  498. *
  499. ****************************************************************************/
  500. /*
  501. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  502. * write commands to that buffer later and the IOMMU will execute them
  503. * asynchronously
  504. */
  505. static int __init alloc_command_buffer(struct amd_iommu *iommu)
  506. {
  507. iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  508. get_order(CMD_BUFFER_SIZE));
  509. return iommu->cmd_buf ? 0 : -ENOMEM;
  510. }
  511. /*
  512. * This function resets the command buffer if the IOMMU stopped fetching
  513. * commands from it.
  514. */
  515. void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  516. {
  517. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  518. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  519. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  520. iommu->cmd_buf_head = 0;
  521. iommu->cmd_buf_tail = 0;
  522. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  523. }
  524. /*
  525. * This function writes the command buffer address to the hardware and
  526. * enables it.
  527. */
  528. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  529. {
  530. u64 entry;
  531. BUG_ON(iommu->cmd_buf == NULL);
  532. entry = iommu_virt_to_phys(iommu->cmd_buf);
  533. entry |= MMIO_CMD_SIZE_512;
  534. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  535. &entry, sizeof(entry));
  536. amd_iommu_reset_cmd_buffer(iommu);
  537. }
  538. /*
  539. * This function disables the command buffer
  540. */
  541. static void iommu_disable_command_buffer(struct amd_iommu *iommu)
  542. {
  543. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  544. }
  545. static void __init free_command_buffer(struct amd_iommu *iommu)
  546. {
  547. free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
  548. }
  549. /* allocates the memory where the IOMMU will log its events to */
  550. static int __init alloc_event_buffer(struct amd_iommu *iommu)
  551. {
  552. iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  553. get_order(EVT_BUFFER_SIZE));
  554. return iommu->evt_buf ? 0 : -ENOMEM;
  555. }
  556. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  557. {
  558. u64 entry;
  559. BUG_ON(iommu->evt_buf == NULL);
  560. entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  561. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  562. &entry, sizeof(entry));
  563. /* set head and tail to zero manually */
  564. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  565. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  566. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  567. }
  568. /*
  569. * This function disables the event log buffer
  570. */
  571. static void iommu_disable_event_buffer(struct amd_iommu *iommu)
  572. {
  573. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  574. }
  575. static void __init free_event_buffer(struct amd_iommu *iommu)
  576. {
  577. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  578. }
  579. /* allocates the memory where the IOMMU will log its events to */
  580. static int __init alloc_ppr_log(struct amd_iommu *iommu)
  581. {
  582. iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  583. get_order(PPR_LOG_SIZE));
  584. return iommu->ppr_log ? 0 : -ENOMEM;
  585. }
  586. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  587. {
  588. u64 entry;
  589. if (iommu->ppr_log == NULL)
  590. return;
  591. entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  592. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  593. &entry, sizeof(entry));
  594. /* set head and tail to zero manually */
  595. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  596. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  597. iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
  598. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  599. }
  600. static void __init free_ppr_log(struct amd_iommu *iommu)
  601. {
  602. if (iommu->ppr_log == NULL)
  603. return;
  604. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  605. }
  606. static void free_ga_log(struct amd_iommu *iommu)
  607. {
  608. #ifdef CONFIG_IRQ_REMAP
  609. if (iommu->ga_log)
  610. free_pages((unsigned long)iommu->ga_log,
  611. get_order(GA_LOG_SIZE));
  612. if (iommu->ga_log_tail)
  613. free_pages((unsigned long)iommu->ga_log_tail,
  614. get_order(8));
  615. #endif
  616. }
  617. static int iommu_ga_log_enable(struct amd_iommu *iommu)
  618. {
  619. #ifdef CONFIG_IRQ_REMAP
  620. u32 status, i;
  621. if (!iommu->ga_log)
  622. return -EINVAL;
  623. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  624. /* Check if already running */
  625. if (status & (MMIO_STATUS_GALOG_RUN_MASK))
  626. return 0;
  627. iommu_feature_enable(iommu, CONTROL_GAINT_EN);
  628. iommu_feature_enable(iommu, CONTROL_GALOG_EN);
  629. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  630. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  631. if (status & (MMIO_STATUS_GALOG_RUN_MASK))
  632. break;
  633. }
  634. if (i >= LOOP_TIMEOUT)
  635. return -EINVAL;
  636. #endif /* CONFIG_IRQ_REMAP */
  637. return 0;
  638. }
  639. #ifdef CONFIG_IRQ_REMAP
  640. static int iommu_init_ga_log(struct amd_iommu *iommu)
  641. {
  642. u64 entry;
  643. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  644. return 0;
  645. iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  646. get_order(GA_LOG_SIZE));
  647. if (!iommu->ga_log)
  648. goto err_out;
  649. iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  650. get_order(8));
  651. if (!iommu->ga_log_tail)
  652. goto err_out;
  653. entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
  654. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
  655. &entry, sizeof(entry));
  656. entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
  657. (BIT_ULL(52)-1)) & ~7ULL;
  658. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
  659. &entry, sizeof(entry));
  660. writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  661. writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  662. return 0;
  663. err_out:
  664. free_ga_log(iommu);
  665. return -EINVAL;
  666. }
  667. #endif /* CONFIG_IRQ_REMAP */
  668. static int iommu_init_ga(struct amd_iommu *iommu)
  669. {
  670. int ret = 0;
  671. #ifdef CONFIG_IRQ_REMAP
  672. /* Note: We have already checked GASup from IVRS table.
  673. * Now, we need to make sure that GAMSup is set.
  674. */
  675. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  676. !iommu_feature(iommu, FEATURE_GAM_VAPIC))
  677. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  678. ret = iommu_init_ga_log(iommu);
  679. #endif /* CONFIG_IRQ_REMAP */
  680. return ret;
  681. }
  682. static void iommu_enable_xt(struct amd_iommu *iommu)
  683. {
  684. #ifdef CONFIG_IRQ_REMAP
  685. /*
  686. * XT mode (32-bit APIC destination ID) requires
  687. * GA mode (128-bit IRTE support) as a prerequisite.
  688. */
  689. if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
  690. amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  691. iommu_feature_enable(iommu, CONTROL_XT_EN);
  692. #endif /* CONFIG_IRQ_REMAP */
  693. }
  694. static void iommu_enable_gt(struct amd_iommu *iommu)
  695. {
  696. if (!iommu_feature(iommu, FEATURE_GT))
  697. return;
  698. iommu_feature_enable(iommu, CONTROL_GT_EN);
  699. }
  700. /* sets a specific bit in the device table entry. */
  701. static void set_dev_entry_bit(u16 devid, u8 bit)
  702. {
  703. int i = (bit >> 6) & 0x03;
  704. int _bit = bit & 0x3f;
  705. amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
  706. }
  707. static int get_dev_entry_bit(u16 devid, u8 bit)
  708. {
  709. int i = (bit >> 6) & 0x03;
  710. int _bit = bit & 0x3f;
  711. return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  712. }
  713. static bool copy_device_table(void)
  714. {
  715. u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
  716. struct dev_table_entry *old_devtb = NULL;
  717. u32 lo, hi, devid, old_devtb_size;
  718. phys_addr_t old_devtb_phys;
  719. struct amd_iommu *iommu;
  720. u16 dom_id, dte_v, irq_v;
  721. gfp_t gfp_flag;
  722. u64 tmp;
  723. if (!amd_iommu_pre_enabled)
  724. return false;
  725. pr_warn("Translation is already enabled - trying to copy translation structures\n");
  726. for_each_iommu(iommu) {
  727. /* All IOMMUs should use the same device table with the same size */
  728. lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
  729. hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
  730. entry = (((u64) hi) << 32) + lo;
  731. if (last_entry && last_entry != entry) {
  732. pr_err("IOMMU:%d should use the same dev table as others!\n",
  733. iommu->index);
  734. return false;
  735. }
  736. last_entry = entry;
  737. old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
  738. if (old_devtb_size != dev_table_size) {
  739. pr_err("The device table size of IOMMU:%d is not expected!\n",
  740. iommu->index);
  741. return false;
  742. }
  743. }
  744. /*
  745. * When SME is enabled in the first kernel, the entry includes the
  746. * memory encryption mask(sme_me_mask), we must remove the memory
  747. * encryption mask to obtain the true physical address in kdump kernel.
  748. */
  749. old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
  750. if (old_devtb_phys >= 0x100000000ULL) {
  751. pr_err("The address of old device table is above 4G, not trustworthy!\n");
  752. return false;
  753. }
  754. old_devtb = (sme_active() && is_kdump_kernel())
  755. ? (__force void *)ioremap_encrypted(old_devtb_phys,
  756. dev_table_size)
  757. : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
  758. if (!old_devtb)
  759. return false;
  760. gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
  761. old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
  762. get_order(dev_table_size));
  763. if (old_dev_tbl_cpy == NULL) {
  764. pr_err("Failed to allocate memory for copying old device table!\n");
  765. return false;
  766. }
  767. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  768. old_dev_tbl_cpy[devid] = old_devtb[devid];
  769. dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
  770. dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
  771. if (dte_v && dom_id) {
  772. old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
  773. old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
  774. __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
  775. /* If gcr3 table existed, mask it out */
  776. if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
  777. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  778. tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  779. old_dev_tbl_cpy[devid].data[1] &= ~tmp;
  780. tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
  781. tmp |= DTE_FLAG_GV;
  782. old_dev_tbl_cpy[devid].data[0] &= ~tmp;
  783. }
  784. }
  785. irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
  786. int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
  787. int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
  788. if (irq_v && (int_ctl || int_tab_len)) {
  789. if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
  790. (int_tab_len != DTE_IRQ_TABLE_LEN)) {
  791. pr_err("Wrong old irq remapping flag: %#x\n", devid);
  792. return false;
  793. }
  794. old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
  795. }
  796. }
  797. memunmap(old_devtb);
  798. return true;
  799. }
  800. void amd_iommu_apply_erratum_63(u16 devid)
  801. {
  802. int sysmgt;
  803. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  804. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  805. if (sysmgt == 0x01)
  806. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  807. }
  808. /* Writes the specific IOMMU for a device into the rlookup table */
  809. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  810. {
  811. amd_iommu_rlookup_table[devid] = iommu;
  812. }
  813. /*
  814. * This function takes the device specific flags read from the ACPI
  815. * table and sets up the device table entry with that information
  816. */
  817. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  818. u16 devid, u32 flags, u32 ext_flags)
  819. {
  820. if (flags & ACPI_DEVFLAG_INITPASS)
  821. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  822. if (flags & ACPI_DEVFLAG_EXTINT)
  823. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  824. if (flags & ACPI_DEVFLAG_NMI)
  825. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  826. if (flags & ACPI_DEVFLAG_SYSMGT1)
  827. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  828. if (flags & ACPI_DEVFLAG_SYSMGT2)
  829. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  830. if (flags & ACPI_DEVFLAG_LINT0)
  831. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  832. if (flags & ACPI_DEVFLAG_LINT1)
  833. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  834. amd_iommu_apply_erratum_63(devid);
  835. set_iommu_for_device(iommu, devid);
  836. }
  837. static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
  838. {
  839. struct devid_map *entry;
  840. struct list_head *list;
  841. if (type == IVHD_SPECIAL_IOAPIC)
  842. list = &ioapic_map;
  843. else if (type == IVHD_SPECIAL_HPET)
  844. list = &hpet_map;
  845. else
  846. return -EINVAL;
  847. list_for_each_entry(entry, list, list) {
  848. if (!(entry->id == id && entry->cmd_line))
  849. continue;
  850. pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
  851. type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
  852. *devid = entry->devid;
  853. return 0;
  854. }
  855. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  856. if (!entry)
  857. return -ENOMEM;
  858. entry->id = id;
  859. entry->devid = *devid;
  860. entry->cmd_line = cmd_line;
  861. list_add_tail(&entry->list, list);
  862. return 0;
  863. }
  864. static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
  865. bool cmd_line)
  866. {
  867. struct acpihid_map_entry *entry;
  868. struct list_head *list = &acpihid_map;
  869. list_for_each_entry(entry, list, list) {
  870. if (strcmp(entry->hid, hid) ||
  871. (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
  872. !entry->cmd_line)
  873. continue;
  874. pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
  875. hid, uid);
  876. *devid = entry->devid;
  877. return 0;
  878. }
  879. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  880. if (!entry)
  881. return -ENOMEM;
  882. memcpy(entry->uid, uid, strlen(uid));
  883. memcpy(entry->hid, hid, strlen(hid));
  884. entry->devid = *devid;
  885. entry->cmd_line = cmd_line;
  886. entry->root_devid = (entry->devid & (~0x7));
  887. pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
  888. entry->cmd_line ? "cmd" : "ivrs",
  889. entry->hid, entry->uid, entry->root_devid);
  890. list_add_tail(&entry->list, list);
  891. return 0;
  892. }
  893. static int __init add_early_maps(void)
  894. {
  895. int i, ret;
  896. for (i = 0; i < early_ioapic_map_size; ++i) {
  897. ret = add_special_device(IVHD_SPECIAL_IOAPIC,
  898. early_ioapic_map[i].id,
  899. &early_ioapic_map[i].devid,
  900. early_ioapic_map[i].cmd_line);
  901. if (ret)
  902. return ret;
  903. }
  904. for (i = 0; i < early_hpet_map_size; ++i) {
  905. ret = add_special_device(IVHD_SPECIAL_HPET,
  906. early_hpet_map[i].id,
  907. &early_hpet_map[i].devid,
  908. early_hpet_map[i].cmd_line);
  909. if (ret)
  910. return ret;
  911. }
  912. for (i = 0; i < early_acpihid_map_size; ++i) {
  913. ret = add_acpi_hid_device(early_acpihid_map[i].hid,
  914. early_acpihid_map[i].uid,
  915. &early_acpihid_map[i].devid,
  916. early_acpihid_map[i].cmd_line);
  917. if (ret)
  918. return ret;
  919. }
  920. return 0;
  921. }
  922. /*
  923. * Reads the device exclusion range from ACPI and initializes the IOMMU with
  924. * it
  925. */
  926. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  927. {
  928. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  929. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  930. return;
  931. if (iommu) {
  932. /*
  933. * We only can configure exclusion ranges per IOMMU, not
  934. * per device. But we can enable the exclusion range per
  935. * device. This is done here
  936. */
  937. set_dev_entry_bit(devid, DEV_ENTRY_EX);
  938. iommu->exclusion_start = m->range_start;
  939. iommu->exclusion_length = m->range_length;
  940. }
  941. }
  942. /*
  943. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  944. * initializes the hardware and our data structures with it.
  945. */
  946. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  947. struct ivhd_header *h)
  948. {
  949. u8 *p = (u8 *)h;
  950. u8 *end = p, flags = 0;
  951. u16 devid = 0, devid_start = 0, devid_to = 0;
  952. u32 dev_i, ext_flags = 0;
  953. bool alias = false;
  954. struct ivhd_entry *e;
  955. u32 ivhd_size;
  956. int ret;
  957. ret = add_early_maps();
  958. if (ret)
  959. return ret;
  960. /*
  961. * First save the recommended feature enable bits from ACPI
  962. */
  963. iommu->acpi_flags = h->flags;
  964. /*
  965. * Done. Now parse the device entries
  966. */
  967. ivhd_size = get_ivhd_header_size(h);
  968. if (!ivhd_size) {
  969. pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
  970. return -EINVAL;
  971. }
  972. p += ivhd_size;
  973. end += h->length;
  974. while (p < end) {
  975. e = (struct ivhd_entry *)p;
  976. switch (e->type) {
  977. case IVHD_DEV_ALL:
  978. DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
  979. for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
  980. set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
  981. break;
  982. case IVHD_DEV_SELECT:
  983. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  984. "flags: %02x\n",
  985. PCI_BUS_NUM(e->devid),
  986. PCI_SLOT(e->devid),
  987. PCI_FUNC(e->devid),
  988. e->flags);
  989. devid = e->devid;
  990. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  991. break;
  992. case IVHD_DEV_SELECT_RANGE_START:
  993. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  994. "devid: %02x:%02x.%x flags: %02x\n",
  995. PCI_BUS_NUM(e->devid),
  996. PCI_SLOT(e->devid),
  997. PCI_FUNC(e->devid),
  998. e->flags);
  999. devid_start = e->devid;
  1000. flags = e->flags;
  1001. ext_flags = 0;
  1002. alias = false;
  1003. break;
  1004. case IVHD_DEV_ALIAS:
  1005. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  1006. "flags: %02x devid_to: %02x:%02x.%x\n",
  1007. PCI_BUS_NUM(e->devid),
  1008. PCI_SLOT(e->devid),
  1009. PCI_FUNC(e->devid),
  1010. e->flags,
  1011. PCI_BUS_NUM(e->ext >> 8),
  1012. PCI_SLOT(e->ext >> 8),
  1013. PCI_FUNC(e->ext >> 8));
  1014. devid = e->devid;
  1015. devid_to = e->ext >> 8;
  1016. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  1017. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  1018. amd_iommu_alias_table[devid] = devid_to;
  1019. break;
  1020. case IVHD_DEV_ALIAS_RANGE:
  1021. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  1022. "devid: %02x:%02x.%x flags: %02x "
  1023. "devid_to: %02x:%02x.%x\n",
  1024. PCI_BUS_NUM(e->devid),
  1025. PCI_SLOT(e->devid),
  1026. PCI_FUNC(e->devid),
  1027. e->flags,
  1028. PCI_BUS_NUM(e->ext >> 8),
  1029. PCI_SLOT(e->ext >> 8),
  1030. PCI_FUNC(e->ext >> 8));
  1031. devid_start = e->devid;
  1032. flags = e->flags;
  1033. devid_to = e->ext >> 8;
  1034. ext_flags = 0;
  1035. alias = true;
  1036. break;
  1037. case IVHD_DEV_EXT_SELECT:
  1038. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  1039. "flags: %02x ext: %08x\n",
  1040. PCI_BUS_NUM(e->devid),
  1041. PCI_SLOT(e->devid),
  1042. PCI_FUNC(e->devid),
  1043. e->flags, e->ext);
  1044. devid = e->devid;
  1045. set_dev_entry_from_acpi(iommu, devid, e->flags,
  1046. e->ext);
  1047. break;
  1048. case IVHD_DEV_EXT_SELECT_RANGE:
  1049. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  1050. "%02x:%02x.%x flags: %02x ext: %08x\n",
  1051. PCI_BUS_NUM(e->devid),
  1052. PCI_SLOT(e->devid),
  1053. PCI_FUNC(e->devid),
  1054. e->flags, e->ext);
  1055. devid_start = e->devid;
  1056. flags = e->flags;
  1057. ext_flags = e->ext;
  1058. alias = false;
  1059. break;
  1060. case IVHD_DEV_RANGE_END:
  1061. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  1062. PCI_BUS_NUM(e->devid),
  1063. PCI_SLOT(e->devid),
  1064. PCI_FUNC(e->devid));
  1065. devid = e->devid;
  1066. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  1067. if (alias) {
  1068. amd_iommu_alias_table[dev_i] = devid_to;
  1069. set_dev_entry_from_acpi(iommu,
  1070. devid_to, flags, ext_flags);
  1071. }
  1072. set_dev_entry_from_acpi(iommu, dev_i,
  1073. flags, ext_flags);
  1074. }
  1075. break;
  1076. case IVHD_DEV_SPECIAL: {
  1077. u8 handle, type;
  1078. const char *var;
  1079. u16 devid;
  1080. int ret;
  1081. handle = e->ext & 0xff;
  1082. devid = (e->ext >> 8) & 0xffff;
  1083. type = (e->ext >> 24) & 0xff;
  1084. if (type == IVHD_SPECIAL_IOAPIC)
  1085. var = "IOAPIC";
  1086. else if (type == IVHD_SPECIAL_HPET)
  1087. var = "HPET";
  1088. else
  1089. var = "UNKNOWN";
  1090. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
  1091. var, (int)handle,
  1092. PCI_BUS_NUM(devid),
  1093. PCI_SLOT(devid),
  1094. PCI_FUNC(devid));
  1095. ret = add_special_device(type, handle, &devid, false);
  1096. if (ret)
  1097. return ret;
  1098. /*
  1099. * add_special_device might update the devid in case a
  1100. * command-line override is present. So call
  1101. * set_dev_entry_from_acpi after add_special_device.
  1102. */
  1103. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1104. break;
  1105. }
  1106. case IVHD_DEV_ACPI_HID: {
  1107. u16 devid;
  1108. u8 hid[ACPIHID_HID_LEN] = {0};
  1109. u8 uid[ACPIHID_UID_LEN] = {0};
  1110. int ret;
  1111. if (h->type != 0x40) {
  1112. pr_err(FW_BUG "Invalid IVHD device type %#x\n",
  1113. e->type);
  1114. break;
  1115. }
  1116. memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
  1117. hid[ACPIHID_HID_LEN - 1] = '\0';
  1118. if (!(*hid)) {
  1119. pr_err(FW_BUG "Invalid HID.\n");
  1120. break;
  1121. }
  1122. switch (e->uidf) {
  1123. case UID_NOT_PRESENT:
  1124. if (e->uidl != 0)
  1125. pr_warn(FW_BUG "Invalid UID length.\n");
  1126. break;
  1127. case UID_IS_INTEGER:
  1128. sprintf(uid, "%d", e->uid);
  1129. break;
  1130. case UID_IS_CHARACTER:
  1131. memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
  1132. uid[ACPIHID_UID_LEN - 1] = '\0';
  1133. break;
  1134. default:
  1135. break;
  1136. }
  1137. devid = e->devid;
  1138. DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
  1139. hid, uid,
  1140. PCI_BUS_NUM(devid),
  1141. PCI_SLOT(devid),
  1142. PCI_FUNC(devid));
  1143. flags = e->flags;
  1144. ret = add_acpi_hid_device(hid, uid, &devid, false);
  1145. if (ret)
  1146. return ret;
  1147. /*
  1148. * add_special_device might update the devid in case a
  1149. * command-line override is present. So call
  1150. * set_dev_entry_from_acpi after add_special_device.
  1151. */
  1152. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1153. break;
  1154. }
  1155. default:
  1156. break;
  1157. }
  1158. p += ivhd_entry_length(p);
  1159. }
  1160. return 0;
  1161. }
  1162. static void __init free_iommu_one(struct amd_iommu *iommu)
  1163. {
  1164. free_command_buffer(iommu);
  1165. free_event_buffer(iommu);
  1166. free_ppr_log(iommu);
  1167. free_ga_log(iommu);
  1168. iommu_unmap_mmio_space(iommu);
  1169. }
  1170. static void __init free_iommu_all(void)
  1171. {
  1172. struct amd_iommu *iommu, *next;
  1173. for_each_iommu_safe(iommu, next) {
  1174. list_del(&iommu->list);
  1175. free_iommu_one(iommu);
  1176. kfree(iommu);
  1177. }
  1178. }
  1179. /*
  1180. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  1181. * Workaround:
  1182. * BIOS should disable L2B micellaneous clock gating by setting
  1183. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  1184. */
  1185. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  1186. {
  1187. u32 value;
  1188. if ((boot_cpu_data.x86 != 0x15) ||
  1189. (boot_cpu_data.x86_model < 0x10) ||
  1190. (boot_cpu_data.x86_model > 0x1f))
  1191. return;
  1192. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1193. pci_read_config_dword(iommu->dev, 0xf4, &value);
  1194. if (value & BIT(2))
  1195. return;
  1196. /* Select NB indirect register 0x90 and enable writing */
  1197. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  1198. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  1199. pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
  1200. dev_name(&iommu->dev->dev));
  1201. /* Clear the enable writing bit */
  1202. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1203. }
  1204. /*
  1205. * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
  1206. * Workaround:
  1207. * BIOS should enable ATS write permission check by setting
  1208. * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
  1209. */
  1210. static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
  1211. {
  1212. u32 value;
  1213. if ((boot_cpu_data.x86 != 0x15) ||
  1214. (boot_cpu_data.x86_model < 0x30) ||
  1215. (boot_cpu_data.x86_model > 0x3f))
  1216. return;
  1217. /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
  1218. value = iommu_read_l2(iommu, 0x47);
  1219. if (value & BIT(0))
  1220. return;
  1221. /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
  1222. iommu_write_l2(iommu, 0x47, value | BIT(0));
  1223. pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
  1224. dev_name(&iommu->dev->dev));
  1225. }
  1226. /*
  1227. * This function clues the initialization function for one IOMMU
  1228. * together and also allocates the command buffer and programs the
  1229. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  1230. */
  1231. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  1232. {
  1233. int ret;
  1234. raw_spin_lock_init(&iommu->lock);
  1235. /* Add IOMMU to internal data structures */
  1236. list_add_tail(&iommu->list, &amd_iommu_list);
  1237. iommu->index = amd_iommus_present++;
  1238. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  1239. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  1240. return -ENOSYS;
  1241. }
  1242. /* Index is fine - add IOMMU to the array */
  1243. amd_iommus[iommu->index] = iommu;
  1244. /*
  1245. * Copy data from ACPI table entry to the iommu struct
  1246. */
  1247. iommu->devid = h->devid;
  1248. iommu->cap_ptr = h->cap_ptr;
  1249. iommu->pci_seg = h->pci_seg;
  1250. iommu->mmio_phys = h->mmio_phys;
  1251. switch (h->type) {
  1252. case 0x10:
  1253. /* Check if IVHD EFR contains proper max banks/counters */
  1254. if ((h->efr_attr != 0) &&
  1255. ((h->efr_attr & (0xF << 13)) != 0) &&
  1256. ((h->efr_attr & (0x3F << 17)) != 0))
  1257. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1258. else
  1259. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1260. if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
  1261. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1262. if (((h->efr_attr & (0x1 << IOMMU_FEAT_XTSUP_SHIFT)) == 0))
  1263. amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  1264. break;
  1265. case 0x11:
  1266. case 0x40:
  1267. if (h->efr_reg & (1 << 9))
  1268. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1269. else
  1270. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1271. if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
  1272. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1273. if (((h->efr_reg & (0x1 << IOMMU_EFR_XTSUP_SHIFT)) == 0))
  1274. amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  1275. break;
  1276. default:
  1277. return -EINVAL;
  1278. }
  1279. iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
  1280. iommu->mmio_phys_end);
  1281. if (!iommu->mmio_base)
  1282. return -ENOMEM;
  1283. if (alloc_command_buffer(iommu))
  1284. return -ENOMEM;
  1285. if (alloc_event_buffer(iommu))
  1286. return -ENOMEM;
  1287. iommu->int_enabled = false;
  1288. init_translation_status(iommu);
  1289. if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
  1290. iommu_disable(iommu);
  1291. clear_translation_pre_enabled(iommu);
  1292. pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
  1293. iommu->index);
  1294. }
  1295. if (amd_iommu_pre_enabled)
  1296. amd_iommu_pre_enabled = translation_pre_enabled(iommu);
  1297. ret = init_iommu_from_acpi(iommu, h);
  1298. if (ret)
  1299. return ret;
  1300. ret = amd_iommu_create_irq_domain(iommu);
  1301. if (ret)
  1302. return ret;
  1303. /*
  1304. * Make sure IOMMU is not considered to translate itself. The IVRS
  1305. * table tells us so, but this is a lie!
  1306. */
  1307. amd_iommu_rlookup_table[iommu->devid] = NULL;
  1308. return 0;
  1309. }
  1310. /**
  1311. * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
  1312. * @ivrs Pointer to the IVRS header
  1313. *
  1314. * This function search through all IVDB of the maximum supported IVHD
  1315. */
  1316. static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
  1317. {
  1318. u8 *base = (u8 *)ivrs;
  1319. struct ivhd_header *ivhd = (struct ivhd_header *)
  1320. (base + IVRS_HEADER_LENGTH);
  1321. u8 last_type = ivhd->type;
  1322. u16 devid = ivhd->devid;
  1323. while (((u8 *)ivhd - base < ivrs->length) &&
  1324. (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
  1325. u8 *p = (u8 *) ivhd;
  1326. if (ivhd->devid == devid)
  1327. last_type = ivhd->type;
  1328. ivhd = (struct ivhd_header *)(p + ivhd->length);
  1329. }
  1330. return last_type;
  1331. }
  1332. /*
  1333. * Iterates over all IOMMU entries in the ACPI table, allocates the
  1334. * IOMMU structure and initializes it with init_iommu_one()
  1335. */
  1336. static int __init init_iommu_all(struct acpi_table_header *table)
  1337. {
  1338. u8 *p = (u8 *)table, *end = (u8 *)table;
  1339. struct ivhd_header *h;
  1340. struct amd_iommu *iommu;
  1341. int ret;
  1342. end += table->length;
  1343. p += IVRS_HEADER_LENGTH;
  1344. while (p < end) {
  1345. h = (struct ivhd_header *)p;
  1346. if (*p == amd_iommu_target_ivhd_type) {
  1347. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  1348. "seg: %d flags: %01x info %04x\n",
  1349. PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
  1350. PCI_FUNC(h->devid), h->cap_ptr,
  1351. h->pci_seg, h->flags, h->info);
  1352. DUMP_printk(" mmio-addr: %016llx\n",
  1353. h->mmio_phys);
  1354. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  1355. if (iommu == NULL)
  1356. return -ENOMEM;
  1357. ret = init_iommu_one(iommu, h);
  1358. if (ret)
  1359. return ret;
  1360. }
  1361. p += h->length;
  1362. }
  1363. WARN_ON(p != end);
  1364. return 0;
  1365. }
  1366. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  1367. u8 fxn, u64 *value, bool is_write);
  1368. static void init_iommu_perf_ctr(struct amd_iommu *iommu)
  1369. {
  1370. u64 val = 0xabcd, val2 = 0;
  1371. if (!iommu_feature(iommu, FEATURE_PC))
  1372. return;
  1373. amd_iommu_pc_present = true;
  1374. /* Check if the performance counters can be written to */
  1375. if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
  1376. (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
  1377. (val != val2)) {
  1378. pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
  1379. amd_iommu_pc_present = false;
  1380. return;
  1381. }
  1382. pr_info("AMD-Vi: IOMMU performance counters supported\n");
  1383. val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
  1384. iommu->max_banks = (u8) ((val >> 12) & 0x3f);
  1385. iommu->max_counters = (u8) ((val >> 7) & 0xf);
  1386. }
  1387. static ssize_t amd_iommu_show_cap(struct device *dev,
  1388. struct device_attribute *attr,
  1389. char *buf)
  1390. {
  1391. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1392. return sprintf(buf, "%x\n", iommu->cap);
  1393. }
  1394. static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
  1395. static ssize_t amd_iommu_show_features(struct device *dev,
  1396. struct device_attribute *attr,
  1397. char *buf)
  1398. {
  1399. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1400. return sprintf(buf, "%llx\n", iommu->features);
  1401. }
  1402. static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
  1403. static struct attribute *amd_iommu_attrs[] = {
  1404. &dev_attr_cap.attr,
  1405. &dev_attr_features.attr,
  1406. NULL,
  1407. };
  1408. static struct attribute_group amd_iommu_group = {
  1409. .name = "amd-iommu",
  1410. .attrs = amd_iommu_attrs,
  1411. };
  1412. static const struct attribute_group *amd_iommu_groups[] = {
  1413. &amd_iommu_group,
  1414. NULL,
  1415. };
  1416. static int __init iommu_init_pci(struct amd_iommu *iommu)
  1417. {
  1418. int cap_ptr = iommu->cap_ptr;
  1419. u32 range, misc, low, high;
  1420. int ret;
  1421. iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
  1422. iommu->devid & 0xff);
  1423. if (!iommu->dev)
  1424. return -ENODEV;
  1425. /* Prevent binding other PCI device drivers to IOMMU devices */
  1426. iommu->dev->match_driver = false;
  1427. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  1428. &iommu->cap);
  1429. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  1430. &range);
  1431. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  1432. &misc);
  1433. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  1434. amd_iommu_iotlb_sup = false;
  1435. /* read extended feature bits */
  1436. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  1437. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  1438. iommu->features = ((u64)high << 32) | low;
  1439. if (iommu_feature(iommu, FEATURE_GT)) {
  1440. int glxval;
  1441. u32 max_pasid;
  1442. u64 pasmax;
  1443. pasmax = iommu->features & FEATURE_PASID_MASK;
  1444. pasmax >>= FEATURE_PASID_SHIFT;
  1445. max_pasid = (1 << (pasmax + 1)) - 1;
  1446. amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
  1447. BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
  1448. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  1449. glxval >>= FEATURE_GLXVAL_SHIFT;
  1450. if (amd_iommu_max_glx_val == -1)
  1451. amd_iommu_max_glx_val = glxval;
  1452. else
  1453. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  1454. }
  1455. if (iommu_feature(iommu, FEATURE_GT) &&
  1456. iommu_feature(iommu, FEATURE_PPR)) {
  1457. iommu->is_iommu_v2 = true;
  1458. amd_iommu_v2_present = true;
  1459. }
  1460. if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
  1461. return -ENOMEM;
  1462. ret = iommu_init_ga(iommu);
  1463. if (ret)
  1464. return ret;
  1465. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  1466. amd_iommu_np_cache = true;
  1467. init_iommu_perf_ctr(iommu);
  1468. if (is_rd890_iommu(iommu->dev)) {
  1469. int i, j;
  1470. iommu->root_pdev =
  1471. pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
  1472. PCI_DEVFN(0, 0));
  1473. /*
  1474. * Some rd890 systems may not be fully reconfigured by the
  1475. * BIOS, so it's necessary for us to store this information so
  1476. * it can be reprogrammed on resume
  1477. */
  1478. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1479. &iommu->stored_addr_lo);
  1480. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1481. &iommu->stored_addr_hi);
  1482. /* Low bit locks writes to configuration space */
  1483. iommu->stored_addr_lo &= ~1;
  1484. for (i = 0; i < 6; i++)
  1485. for (j = 0; j < 0x12; j++)
  1486. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  1487. for (i = 0; i < 0x83; i++)
  1488. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  1489. }
  1490. amd_iommu_erratum_746_workaround(iommu);
  1491. amd_iommu_ats_write_check_workaround(iommu);
  1492. iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
  1493. amd_iommu_groups, "ivhd%d", iommu->index);
  1494. iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
  1495. iommu_device_register(&iommu->iommu);
  1496. return pci_enable_device(iommu->dev);
  1497. }
  1498. static void print_iommu_info(void)
  1499. {
  1500. static const char * const feat_str[] = {
  1501. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  1502. "IA", "GA", "HE", "PC"
  1503. };
  1504. struct amd_iommu *iommu;
  1505. for_each_iommu(iommu) {
  1506. int i;
  1507. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  1508. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  1509. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  1510. pr_info("AMD-Vi: Extended features (%#llx):\n",
  1511. iommu->features);
  1512. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1513. if (iommu_feature(iommu, (1ULL << i)))
  1514. pr_cont(" %s", feat_str[i]);
  1515. }
  1516. if (iommu->features & FEATURE_GAM_VAPIC)
  1517. pr_cont(" GA_vAPIC");
  1518. pr_cont("\n");
  1519. }
  1520. }
  1521. if (irq_remapping_enabled) {
  1522. pr_info("AMD-Vi: Interrupt remapping enabled\n");
  1523. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1524. pr_info("AMD-Vi: virtual APIC enabled\n");
  1525. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1526. pr_info("AMD-Vi: X2APIC enabled\n");
  1527. }
  1528. }
  1529. static int __init amd_iommu_init_pci(void)
  1530. {
  1531. struct amd_iommu *iommu;
  1532. int ret = 0;
  1533. for_each_iommu(iommu) {
  1534. ret = iommu_init_pci(iommu);
  1535. if (ret)
  1536. break;
  1537. }
  1538. /*
  1539. * Order is important here to make sure any unity map requirements are
  1540. * fulfilled. The unity mappings are created and written to the device
  1541. * table during the amd_iommu_init_api() call.
  1542. *
  1543. * After that we call init_device_table_dma() to make sure any
  1544. * uninitialized DTE will block DMA, and in the end we flush the caches
  1545. * of all IOMMUs to make sure the changes to the device table are
  1546. * active.
  1547. */
  1548. ret = amd_iommu_init_api();
  1549. init_device_table_dma();
  1550. for_each_iommu(iommu)
  1551. iommu_flush_all_caches(iommu);
  1552. if (!ret)
  1553. print_iommu_info();
  1554. return ret;
  1555. }
  1556. /****************************************************************************
  1557. *
  1558. * The following functions initialize the MSI interrupts for all IOMMUs
  1559. * in the system. It's a bit challenging because there could be multiple
  1560. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1561. * pci_dev.
  1562. *
  1563. ****************************************************************************/
  1564. static int iommu_setup_msi(struct amd_iommu *iommu)
  1565. {
  1566. int r;
  1567. r = pci_enable_msi(iommu->dev);
  1568. if (r)
  1569. return r;
  1570. r = request_threaded_irq(iommu->dev->irq,
  1571. amd_iommu_int_handler,
  1572. amd_iommu_int_thread,
  1573. 0, "AMD-Vi",
  1574. iommu);
  1575. if (r) {
  1576. pci_disable_msi(iommu->dev);
  1577. return r;
  1578. }
  1579. iommu->int_enabled = true;
  1580. return 0;
  1581. }
  1582. static int iommu_init_msi(struct amd_iommu *iommu)
  1583. {
  1584. int ret;
  1585. if (iommu->int_enabled)
  1586. goto enable_faults;
  1587. if (iommu->dev->msi_cap)
  1588. ret = iommu_setup_msi(iommu);
  1589. else
  1590. ret = -ENODEV;
  1591. if (ret)
  1592. return ret;
  1593. enable_faults:
  1594. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  1595. if (iommu->ppr_log != NULL)
  1596. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  1597. iommu_ga_log_enable(iommu);
  1598. return 0;
  1599. }
  1600. /****************************************************************************
  1601. *
  1602. * The next functions belong to the third pass of parsing the ACPI
  1603. * table. In this last pass the memory mapping requirements are
  1604. * gathered (like exclusion and unity mapping ranges).
  1605. *
  1606. ****************************************************************************/
  1607. static void __init free_unity_maps(void)
  1608. {
  1609. struct unity_map_entry *entry, *next;
  1610. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  1611. list_del(&entry->list);
  1612. kfree(entry);
  1613. }
  1614. }
  1615. /* called when we find an exclusion range definition in ACPI */
  1616. static int __init init_exclusion_range(struct ivmd_header *m)
  1617. {
  1618. int i;
  1619. switch (m->type) {
  1620. case ACPI_IVMD_TYPE:
  1621. set_device_exclusion_range(m->devid, m);
  1622. break;
  1623. case ACPI_IVMD_TYPE_ALL:
  1624. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1625. set_device_exclusion_range(i, m);
  1626. break;
  1627. case ACPI_IVMD_TYPE_RANGE:
  1628. for (i = m->devid; i <= m->aux; ++i)
  1629. set_device_exclusion_range(i, m);
  1630. break;
  1631. default:
  1632. break;
  1633. }
  1634. return 0;
  1635. }
  1636. /* called for unity map ACPI definition */
  1637. static int __init init_unity_map_range(struct ivmd_header *m)
  1638. {
  1639. struct unity_map_entry *e = NULL;
  1640. char *s;
  1641. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1642. if (e == NULL)
  1643. return -ENOMEM;
  1644. switch (m->type) {
  1645. default:
  1646. kfree(e);
  1647. return 0;
  1648. case ACPI_IVMD_TYPE:
  1649. s = "IVMD_TYPEi\t\t\t";
  1650. e->devid_start = e->devid_end = m->devid;
  1651. break;
  1652. case ACPI_IVMD_TYPE_ALL:
  1653. s = "IVMD_TYPE_ALL\t\t";
  1654. e->devid_start = 0;
  1655. e->devid_end = amd_iommu_last_bdf;
  1656. break;
  1657. case ACPI_IVMD_TYPE_RANGE:
  1658. s = "IVMD_TYPE_RANGE\t\t";
  1659. e->devid_start = m->devid;
  1660. e->devid_end = m->aux;
  1661. break;
  1662. }
  1663. e->address_start = PAGE_ALIGN(m->range_start);
  1664. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1665. e->prot = m->flags >> 1;
  1666. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1667. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1668. PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
  1669. PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
  1670. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1671. e->address_start, e->address_end, m->flags);
  1672. list_add_tail(&e->list, &amd_iommu_unity_map);
  1673. return 0;
  1674. }
  1675. /* iterates over all memory definitions we find in the ACPI table */
  1676. static int __init init_memory_definitions(struct acpi_table_header *table)
  1677. {
  1678. u8 *p = (u8 *)table, *end = (u8 *)table;
  1679. struct ivmd_header *m;
  1680. end += table->length;
  1681. p += IVRS_HEADER_LENGTH;
  1682. while (p < end) {
  1683. m = (struct ivmd_header *)p;
  1684. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1685. init_exclusion_range(m);
  1686. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  1687. init_unity_map_range(m);
  1688. p += m->length;
  1689. }
  1690. return 0;
  1691. }
  1692. /*
  1693. * Init the device table to not allow DMA access for devices
  1694. */
  1695. static void init_device_table_dma(void)
  1696. {
  1697. u32 devid;
  1698. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1699. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1700. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1701. }
  1702. }
  1703. static void __init uninit_device_table_dma(void)
  1704. {
  1705. u32 devid;
  1706. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1707. amd_iommu_dev_table[devid].data[0] = 0ULL;
  1708. amd_iommu_dev_table[devid].data[1] = 0ULL;
  1709. }
  1710. }
  1711. static void init_device_table(void)
  1712. {
  1713. u32 devid;
  1714. if (!amd_iommu_irq_remap)
  1715. return;
  1716. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1717. set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
  1718. }
  1719. static void iommu_init_flags(struct amd_iommu *iommu)
  1720. {
  1721. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1722. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1723. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1724. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1725. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1726. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1727. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1728. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1729. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1730. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1731. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1732. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1733. /*
  1734. * make IOMMU memory accesses cache coherent
  1735. */
  1736. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1737. /* Set IOTLB invalidation timeout to 1s */
  1738. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1739. }
  1740. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1741. {
  1742. int i, j;
  1743. u32 ioc_feature_control;
  1744. struct pci_dev *pdev = iommu->root_pdev;
  1745. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1746. if (!is_rd890_iommu(iommu->dev) || !pdev)
  1747. return;
  1748. /*
  1749. * First, we need to ensure that the iommu is enabled. This is
  1750. * controlled by a register in the northbridge
  1751. */
  1752. /* Select Northbridge indirect register 0x75 and enable writing */
  1753. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1754. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1755. /* Enable the iommu */
  1756. if (!(ioc_feature_control & 0x1))
  1757. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1758. /* Restore the iommu BAR */
  1759. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1760. iommu->stored_addr_lo);
  1761. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1762. iommu->stored_addr_hi);
  1763. /* Restore the l1 indirect regs for each of the 6 l1s */
  1764. for (i = 0; i < 6; i++)
  1765. for (j = 0; j < 0x12; j++)
  1766. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1767. /* Restore the l2 indirect regs */
  1768. for (i = 0; i < 0x83; i++)
  1769. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1770. /* Lock PCI setup registers */
  1771. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1772. iommu->stored_addr_lo | 1);
  1773. }
  1774. static void iommu_enable_ga(struct amd_iommu *iommu)
  1775. {
  1776. #ifdef CONFIG_IRQ_REMAP
  1777. switch (amd_iommu_guest_ir) {
  1778. case AMD_IOMMU_GUEST_IR_VAPIC:
  1779. iommu_feature_enable(iommu, CONTROL_GAM_EN);
  1780. /* Fall through */
  1781. case AMD_IOMMU_GUEST_IR_LEGACY_GA:
  1782. iommu_feature_enable(iommu, CONTROL_GA_EN);
  1783. iommu->irte_ops = &irte_128_ops;
  1784. break;
  1785. default:
  1786. iommu->irte_ops = &irte_32_ops;
  1787. break;
  1788. }
  1789. #endif
  1790. }
  1791. static void early_enable_iommu(struct amd_iommu *iommu)
  1792. {
  1793. iommu_disable(iommu);
  1794. iommu_init_flags(iommu);
  1795. iommu_set_device_table(iommu);
  1796. iommu_enable_command_buffer(iommu);
  1797. iommu_enable_event_buffer(iommu);
  1798. iommu_set_exclusion_range(iommu);
  1799. iommu_enable_ga(iommu);
  1800. iommu_enable_xt(iommu);
  1801. iommu_enable(iommu);
  1802. iommu_flush_all_caches(iommu);
  1803. }
  1804. /*
  1805. * This function finally enables all IOMMUs found in the system after
  1806. * they have been initialized.
  1807. *
  1808. * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
  1809. * the old content of device table entries. Not this case or copy failed,
  1810. * just continue as normal kernel does.
  1811. */
  1812. static void early_enable_iommus(void)
  1813. {
  1814. struct amd_iommu *iommu;
  1815. if (!copy_device_table()) {
  1816. /*
  1817. * If come here because of failure in copying device table from old
  1818. * kernel with all IOMMUs enabled, print error message and try to
  1819. * free allocated old_dev_tbl_cpy.
  1820. */
  1821. if (amd_iommu_pre_enabled)
  1822. pr_err("Failed to copy DEV table from previous kernel.\n");
  1823. if (old_dev_tbl_cpy != NULL)
  1824. free_pages((unsigned long)old_dev_tbl_cpy,
  1825. get_order(dev_table_size));
  1826. for_each_iommu(iommu) {
  1827. clear_translation_pre_enabled(iommu);
  1828. early_enable_iommu(iommu);
  1829. }
  1830. } else {
  1831. pr_info("Copied DEV table from previous kernel.\n");
  1832. free_pages((unsigned long)amd_iommu_dev_table,
  1833. get_order(dev_table_size));
  1834. amd_iommu_dev_table = old_dev_tbl_cpy;
  1835. for_each_iommu(iommu) {
  1836. iommu_disable_command_buffer(iommu);
  1837. iommu_disable_event_buffer(iommu);
  1838. iommu_enable_command_buffer(iommu);
  1839. iommu_enable_event_buffer(iommu);
  1840. iommu_enable_ga(iommu);
  1841. iommu_enable_xt(iommu);
  1842. iommu_set_device_table(iommu);
  1843. iommu_flush_all_caches(iommu);
  1844. }
  1845. }
  1846. #ifdef CONFIG_IRQ_REMAP
  1847. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1848. amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
  1849. #endif
  1850. }
  1851. static void enable_iommus_v2(void)
  1852. {
  1853. struct amd_iommu *iommu;
  1854. for_each_iommu(iommu) {
  1855. iommu_enable_ppr_log(iommu);
  1856. iommu_enable_gt(iommu);
  1857. }
  1858. }
  1859. static void enable_iommus(void)
  1860. {
  1861. early_enable_iommus();
  1862. enable_iommus_v2();
  1863. }
  1864. static void disable_iommus(void)
  1865. {
  1866. struct amd_iommu *iommu;
  1867. for_each_iommu(iommu)
  1868. iommu_disable(iommu);
  1869. #ifdef CONFIG_IRQ_REMAP
  1870. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1871. amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  1872. #endif
  1873. }
  1874. /*
  1875. * Suspend/Resume support
  1876. * disable suspend until real resume implemented
  1877. */
  1878. static void amd_iommu_resume(void)
  1879. {
  1880. struct amd_iommu *iommu;
  1881. for_each_iommu(iommu)
  1882. iommu_apply_resume_quirks(iommu);
  1883. /* re-load the hardware */
  1884. enable_iommus();
  1885. amd_iommu_enable_interrupts();
  1886. }
  1887. static int amd_iommu_suspend(void)
  1888. {
  1889. /* disable IOMMUs to go out of the way for BIOS */
  1890. disable_iommus();
  1891. return 0;
  1892. }
  1893. static struct syscore_ops amd_iommu_syscore_ops = {
  1894. .suspend = amd_iommu_suspend,
  1895. .resume = amd_iommu_resume,
  1896. };
  1897. static void __init free_iommu_resources(void)
  1898. {
  1899. kmemleak_free(irq_lookup_table);
  1900. free_pages((unsigned long)irq_lookup_table,
  1901. get_order(rlookup_table_size));
  1902. irq_lookup_table = NULL;
  1903. kmem_cache_destroy(amd_iommu_irq_cache);
  1904. amd_iommu_irq_cache = NULL;
  1905. free_pages((unsigned long)amd_iommu_rlookup_table,
  1906. get_order(rlookup_table_size));
  1907. amd_iommu_rlookup_table = NULL;
  1908. free_pages((unsigned long)amd_iommu_alias_table,
  1909. get_order(alias_table_size));
  1910. amd_iommu_alias_table = NULL;
  1911. free_pages((unsigned long)amd_iommu_dev_table,
  1912. get_order(dev_table_size));
  1913. amd_iommu_dev_table = NULL;
  1914. free_iommu_all();
  1915. #ifdef CONFIG_GART_IOMMU
  1916. /*
  1917. * We failed to initialize the AMD IOMMU - try fallback to GART
  1918. * if possible.
  1919. */
  1920. gart_iommu_init();
  1921. #endif
  1922. }
  1923. /* SB IOAPIC is always on this device in AMD systems */
  1924. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  1925. static bool __init check_ioapic_information(void)
  1926. {
  1927. const char *fw_bug = FW_BUG;
  1928. bool ret, has_sb_ioapic;
  1929. int idx;
  1930. has_sb_ioapic = false;
  1931. ret = false;
  1932. /*
  1933. * If we have map overrides on the kernel command line the
  1934. * messages in this function might not describe firmware bugs
  1935. * anymore - so be careful
  1936. */
  1937. if (cmdline_maps)
  1938. fw_bug = "";
  1939. for (idx = 0; idx < nr_ioapics; idx++) {
  1940. int devid, id = mpc_ioapic_id(idx);
  1941. devid = get_ioapic_devid(id);
  1942. if (devid < 0) {
  1943. pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
  1944. fw_bug, id);
  1945. ret = false;
  1946. } else if (devid == IOAPIC_SB_DEVID) {
  1947. has_sb_ioapic = true;
  1948. ret = true;
  1949. }
  1950. }
  1951. if (!has_sb_ioapic) {
  1952. /*
  1953. * We expect the SB IOAPIC to be listed in the IVRS
  1954. * table. The system timer is connected to the SB IOAPIC
  1955. * and if we don't have it in the list the system will
  1956. * panic at boot time. This situation usually happens
  1957. * when the BIOS is buggy and provides us the wrong
  1958. * device id for the IOAPIC in the system.
  1959. */
  1960. pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
  1961. }
  1962. if (!ret)
  1963. pr_err("AMD-Vi: Disabling interrupt remapping\n");
  1964. return ret;
  1965. }
  1966. static void __init free_dma_resources(void)
  1967. {
  1968. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1969. get_order(MAX_DOMAIN_ID/8));
  1970. amd_iommu_pd_alloc_bitmap = NULL;
  1971. free_unity_maps();
  1972. }
  1973. /*
  1974. * This is the hardware init function for AMD IOMMU in the system.
  1975. * This function is called either from amd_iommu_init or from the interrupt
  1976. * remapping setup code.
  1977. *
  1978. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1979. * four times:
  1980. *
  1981. * 1 pass) Discover the most comprehensive IVHD type to use.
  1982. *
  1983. * 2 pass) Find the highest PCI device id the driver has to handle.
  1984. * Upon this information the size of the data structures is
  1985. * determined that needs to be allocated.
  1986. *
  1987. * 3 pass) Initialize the data structures just allocated with the
  1988. * information in the ACPI table about available AMD IOMMUs
  1989. * in the system. It also maps the PCI devices in the
  1990. * system to specific IOMMUs
  1991. *
  1992. * 4 pass) After the basic data structures are allocated and
  1993. * initialized we update them with information about memory
  1994. * remapping requirements parsed out of the ACPI table in
  1995. * this last pass.
  1996. *
  1997. * After everything is set up the IOMMUs are enabled and the necessary
  1998. * hotplug and suspend notifiers are registered.
  1999. */
  2000. static int __init early_amd_iommu_init(void)
  2001. {
  2002. struct acpi_table_header *ivrs_base;
  2003. acpi_status status;
  2004. int i, remap_cache_sz, ret = 0;
  2005. if (!amd_iommu_detected)
  2006. return -ENODEV;
  2007. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2008. if (status == AE_NOT_FOUND)
  2009. return -ENODEV;
  2010. else if (ACPI_FAILURE(status)) {
  2011. const char *err = acpi_format_exception(status);
  2012. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  2013. return -EINVAL;
  2014. }
  2015. /*
  2016. * Validate checksum here so we don't need to do it when
  2017. * we actually parse the table
  2018. */
  2019. ret = check_ivrs_checksum(ivrs_base);
  2020. if (ret)
  2021. goto out;
  2022. amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
  2023. DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
  2024. /*
  2025. * First parse ACPI tables to find the largest Bus/Dev/Func
  2026. * we need to handle. Upon this information the shared data
  2027. * structures for the IOMMUs in the system will be allocated
  2028. */
  2029. ret = find_last_devid_acpi(ivrs_base);
  2030. if (ret)
  2031. goto out;
  2032. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  2033. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  2034. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  2035. /* Device table - directly used by all IOMMUs */
  2036. ret = -ENOMEM;
  2037. amd_iommu_dev_table = (void *)__get_free_pages(
  2038. GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
  2039. get_order(dev_table_size));
  2040. if (amd_iommu_dev_table == NULL)
  2041. goto out;
  2042. /*
  2043. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  2044. * IOMMU see for that device
  2045. */
  2046. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  2047. get_order(alias_table_size));
  2048. if (amd_iommu_alias_table == NULL)
  2049. goto out;
  2050. /* IOMMU rlookup table - find the IOMMU for a specific device */
  2051. amd_iommu_rlookup_table = (void *)__get_free_pages(
  2052. GFP_KERNEL | __GFP_ZERO,
  2053. get_order(rlookup_table_size));
  2054. if (amd_iommu_rlookup_table == NULL)
  2055. goto out;
  2056. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  2057. GFP_KERNEL | __GFP_ZERO,
  2058. get_order(MAX_DOMAIN_ID/8));
  2059. if (amd_iommu_pd_alloc_bitmap == NULL)
  2060. goto out;
  2061. /*
  2062. * let all alias entries point to itself
  2063. */
  2064. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  2065. amd_iommu_alias_table[i] = i;
  2066. /*
  2067. * never allocate domain 0 because its used as the non-allocated and
  2068. * error value placeholder
  2069. */
  2070. __set_bit(0, amd_iommu_pd_alloc_bitmap);
  2071. spin_lock_init(&amd_iommu_pd_lock);
  2072. /*
  2073. * now the data structures are allocated and basically initialized
  2074. * start the real acpi table scan
  2075. */
  2076. ret = init_iommu_all(ivrs_base);
  2077. if (ret)
  2078. goto out;
  2079. /* Disable any previously enabled IOMMUs */
  2080. if (!is_kdump_kernel() || amd_iommu_disabled)
  2081. disable_iommus();
  2082. if (amd_iommu_irq_remap)
  2083. amd_iommu_irq_remap = check_ioapic_information();
  2084. if (amd_iommu_irq_remap) {
  2085. /*
  2086. * Interrupt remapping enabled, create kmem_cache for the
  2087. * remapping tables.
  2088. */
  2089. ret = -ENOMEM;
  2090. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2091. remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
  2092. else
  2093. remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
  2094. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  2095. remap_cache_sz,
  2096. IRQ_TABLE_ALIGNMENT,
  2097. 0, NULL);
  2098. if (!amd_iommu_irq_cache)
  2099. goto out;
  2100. irq_lookup_table = (void *)__get_free_pages(
  2101. GFP_KERNEL | __GFP_ZERO,
  2102. get_order(rlookup_table_size));
  2103. kmemleak_alloc(irq_lookup_table, rlookup_table_size,
  2104. 1, GFP_KERNEL);
  2105. if (!irq_lookup_table)
  2106. goto out;
  2107. }
  2108. ret = init_memory_definitions(ivrs_base);
  2109. if (ret)
  2110. goto out;
  2111. /* init the device table */
  2112. init_device_table();
  2113. out:
  2114. /* Don't leak any ACPI memory */
  2115. acpi_put_table(ivrs_base);
  2116. ivrs_base = NULL;
  2117. return ret;
  2118. }
  2119. static int amd_iommu_enable_interrupts(void)
  2120. {
  2121. struct amd_iommu *iommu;
  2122. int ret = 0;
  2123. for_each_iommu(iommu) {
  2124. ret = iommu_init_msi(iommu);
  2125. if (ret)
  2126. goto out;
  2127. }
  2128. out:
  2129. return ret;
  2130. }
  2131. static bool detect_ivrs(void)
  2132. {
  2133. struct acpi_table_header *ivrs_base;
  2134. acpi_status status;
  2135. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2136. if (status == AE_NOT_FOUND)
  2137. return false;
  2138. else if (ACPI_FAILURE(status)) {
  2139. const char *err = acpi_format_exception(status);
  2140. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  2141. return false;
  2142. }
  2143. acpi_put_table(ivrs_base);
  2144. /* Make sure ACS will be enabled during PCI probe */
  2145. pci_request_acs();
  2146. return true;
  2147. }
  2148. /****************************************************************************
  2149. *
  2150. * AMD IOMMU Initialization State Machine
  2151. *
  2152. ****************************************************************************/
  2153. static int __init state_next(void)
  2154. {
  2155. int ret = 0;
  2156. switch (init_state) {
  2157. case IOMMU_START_STATE:
  2158. if (!detect_ivrs()) {
  2159. init_state = IOMMU_NOT_FOUND;
  2160. ret = -ENODEV;
  2161. } else {
  2162. init_state = IOMMU_IVRS_DETECTED;
  2163. }
  2164. break;
  2165. case IOMMU_IVRS_DETECTED:
  2166. ret = early_amd_iommu_init();
  2167. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  2168. if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
  2169. pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
  2170. free_dma_resources();
  2171. free_iommu_resources();
  2172. init_state = IOMMU_CMDLINE_DISABLED;
  2173. ret = -EINVAL;
  2174. }
  2175. break;
  2176. case IOMMU_ACPI_FINISHED:
  2177. early_enable_iommus();
  2178. x86_platform.iommu_shutdown = disable_iommus;
  2179. init_state = IOMMU_ENABLED;
  2180. break;
  2181. case IOMMU_ENABLED:
  2182. register_syscore_ops(&amd_iommu_syscore_ops);
  2183. ret = amd_iommu_init_pci();
  2184. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  2185. enable_iommus_v2();
  2186. break;
  2187. case IOMMU_PCI_INIT:
  2188. ret = amd_iommu_enable_interrupts();
  2189. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  2190. break;
  2191. case IOMMU_INTERRUPTS_EN:
  2192. ret = amd_iommu_init_dma_ops();
  2193. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  2194. break;
  2195. case IOMMU_DMA_OPS:
  2196. init_state = IOMMU_INITIALIZED;
  2197. break;
  2198. case IOMMU_INITIALIZED:
  2199. /* Nothing to do */
  2200. break;
  2201. case IOMMU_NOT_FOUND:
  2202. case IOMMU_INIT_ERROR:
  2203. case IOMMU_CMDLINE_DISABLED:
  2204. /* Error states => do nothing */
  2205. ret = -EINVAL;
  2206. break;
  2207. default:
  2208. /* Unknown state */
  2209. BUG();
  2210. }
  2211. return ret;
  2212. }
  2213. static int __init iommu_go_to_state(enum iommu_init_state state)
  2214. {
  2215. int ret = -EINVAL;
  2216. while (init_state != state) {
  2217. if (init_state == IOMMU_NOT_FOUND ||
  2218. init_state == IOMMU_INIT_ERROR ||
  2219. init_state == IOMMU_CMDLINE_DISABLED)
  2220. break;
  2221. ret = state_next();
  2222. }
  2223. return ret;
  2224. }
  2225. #ifdef CONFIG_IRQ_REMAP
  2226. int __init amd_iommu_prepare(void)
  2227. {
  2228. int ret;
  2229. amd_iommu_irq_remap = true;
  2230. ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
  2231. if (ret)
  2232. return ret;
  2233. return amd_iommu_irq_remap ? 0 : -ENODEV;
  2234. }
  2235. int __init amd_iommu_enable(void)
  2236. {
  2237. int ret;
  2238. ret = iommu_go_to_state(IOMMU_ENABLED);
  2239. if (ret)
  2240. return ret;
  2241. irq_remapping_enabled = 1;
  2242. return amd_iommu_xt_mode;
  2243. }
  2244. void amd_iommu_disable(void)
  2245. {
  2246. amd_iommu_suspend();
  2247. }
  2248. int amd_iommu_reenable(int mode)
  2249. {
  2250. amd_iommu_resume();
  2251. return 0;
  2252. }
  2253. int __init amd_iommu_enable_faulting(void)
  2254. {
  2255. /* We enable MSI later when PCI is initialized */
  2256. return 0;
  2257. }
  2258. #endif
  2259. /*
  2260. * This is the core init function for AMD IOMMU hardware in the system.
  2261. * This function is called from the generic x86 DMA layer initialization
  2262. * code.
  2263. */
  2264. static int __init amd_iommu_init(void)
  2265. {
  2266. struct amd_iommu *iommu;
  2267. int ret;
  2268. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  2269. if (ret) {
  2270. free_dma_resources();
  2271. if (!irq_remapping_enabled) {
  2272. disable_iommus();
  2273. free_iommu_resources();
  2274. } else {
  2275. uninit_device_table_dma();
  2276. for_each_iommu(iommu)
  2277. iommu_flush_all_caches(iommu);
  2278. }
  2279. }
  2280. for_each_iommu(iommu)
  2281. amd_iommu_debugfs_setup(iommu);
  2282. return ret;
  2283. }
  2284. static bool amd_iommu_sme_check(void)
  2285. {
  2286. if (!sme_active() || (boot_cpu_data.x86 != 0x17))
  2287. return true;
  2288. /* For Fam17h, a specific level of support is required */
  2289. if (boot_cpu_data.microcode >= 0x08001205)
  2290. return true;
  2291. if ((boot_cpu_data.microcode >= 0x08001126) &&
  2292. (boot_cpu_data.microcode <= 0x080011ff))
  2293. return true;
  2294. pr_notice("AMD-Vi: IOMMU not currently supported when SME is active\n");
  2295. return false;
  2296. }
  2297. /****************************************************************************
  2298. *
  2299. * Early detect code. This code runs at IOMMU detection time in the DMA
  2300. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  2301. * IOMMUs
  2302. *
  2303. ****************************************************************************/
  2304. int __init amd_iommu_detect(void)
  2305. {
  2306. int ret;
  2307. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  2308. return -ENODEV;
  2309. if (!amd_iommu_sme_check())
  2310. return -ENODEV;
  2311. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  2312. if (ret)
  2313. return ret;
  2314. amd_iommu_detected = true;
  2315. iommu_detected = 1;
  2316. x86_init.iommu.iommu_init = amd_iommu_init;
  2317. return 1;
  2318. }
  2319. /****************************************************************************
  2320. *
  2321. * Parsing functions for the AMD IOMMU specific kernel command line
  2322. * options.
  2323. *
  2324. ****************************************************************************/
  2325. static int __init parse_amd_iommu_dump(char *str)
  2326. {
  2327. amd_iommu_dump = true;
  2328. return 1;
  2329. }
  2330. static int __init parse_amd_iommu_intr(char *str)
  2331. {
  2332. for (; *str; ++str) {
  2333. if (strncmp(str, "legacy", 6) == 0) {
  2334. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  2335. break;
  2336. }
  2337. if (strncmp(str, "vapic", 5) == 0) {
  2338. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  2339. break;
  2340. }
  2341. }
  2342. return 1;
  2343. }
  2344. static int __init parse_amd_iommu_options(char *str)
  2345. {
  2346. for (; *str; ++str) {
  2347. if (strncmp(str, "fullflush", 9) == 0)
  2348. amd_iommu_unmap_flush = true;
  2349. if (strncmp(str, "off", 3) == 0)
  2350. amd_iommu_disabled = true;
  2351. if (strncmp(str, "force_isolation", 15) == 0)
  2352. amd_iommu_force_isolation = true;
  2353. }
  2354. return 1;
  2355. }
  2356. static int __init parse_ivrs_ioapic(char *str)
  2357. {
  2358. unsigned int bus, dev, fn;
  2359. int ret, id, i;
  2360. u16 devid;
  2361. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  2362. if (ret != 4) {
  2363. pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
  2364. return 1;
  2365. }
  2366. if (early_ioapic_map_size == EARLY_MAP_SIZE) {
  2367. pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
  2368. str);
  2369. return 1;
  2370. }
  2371. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2372. cmdline_maps = true;
  2373. i = early_ioapic_map_size++;
  2374. early_ioapic_map[i].id = id;
  2375. early_ioapic_map[i].devid = devid;
  2376. early_ioapic_map[i].cmd_line = true;
  2377. return 1;
  2378. }
  2379. static int __init parse_ivrs_hpet(char *str)
  2380. {
  2381. unsigned int bus, dev, fn;
  2382. int ret, id, i;
  2383. u16 devid;
  2384. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  2385. if (ret != 4) {
  2386. pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
  2387. return 1;
  2388. }
  2389. if (early_hpet_map_size == EARLY_MAP_SIZE) {
  2390. pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
  2391. str);
  2392. return 1;
  2393. }
  2394. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2395. cmdline_maps = true;
  2396. i = early_hpet_map_size++;
  2397. early_hpet_map[i].id = id;
  2398. early_hpet_map[i].devid = devid;
  2399. early_hpet_map[i].cmd_line = true;
  2400. return 1;
  2401. }
  2402. static int __init parse_ivrs_acpihid(char *str)
  2403. {
  2404. u32 bus, dev, fn;
  2405. char *hid, *uid, *p;
  2406. char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
  2407. int ret, i;
  2408. ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
  2409. if (ret != 4) {
  2410. pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
  2411. return 1;
  2412. }
  2413. p = acpiid;
  2414. hid = strsep(&p, ":");
  2415. uid = p;
  2416. if (!hid || !(*hid) || !uid) {
  2417. pr_err("AMD-Vi: Invalid command line: hid or uid\n");
  2418. return 1;
  2419. }
  2420. i = early_acpihid_map_size++;
  2421. memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
  2422. memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
  2423. early_acpihid_map[i].devid =
  2424. ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2425. early_acpihid_map[i].cmd_line = true;
  2426. return 1;
  2427. }
  2428. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  2429. __setup("amd_iommu=", parse_amd_iommu_options);
  2430. __setup("amd_iommu_intr=", parse_amd_iommu_intr);
  2431. __setup("ivrs_ioapic", parse_ivrs_ioapic);
  2432. __setup("ivrs_hpet", parse_ivrs_hpet);
  2433. __setup("ivrs_acpihid", parse_ivrs_acpihid);
  2434. IOMMU_INIT_FINISH(amd_iommu_detect,
  2435. gart_iommu_hole_init,
  2436. NULL,
  2437. NULL);
  2438. bool amd_iommu_v2_supported(void)
  2439. {
  2440. return amd_iommu_v2_present;
  2441. }
  2442. EXPORT_SYMBOL(amd_iommu_v2_supported);
  2443. struct amd_iommu *get_amd_iommu(unsigned int idx)
  2444. {
  2445. unsigned int i = 0;
  2446. struct amd_iommu *iommu;
  2447. for_each_iommu(iommu)
  2448. if (i++ == idx)
  2449. return iommu;
  2450. return NULL;
  2451. }
  2452. EXPORT_SYMBOL(get_amd_iommu);
  2453. /****************************************************************************
  2454. *
  2455. * IOMMU EFR Performance Counter support functionality. This code allows
  2456. * access to the IOMMU PC functionality.
  2457. *
  2458. ****************************************************************************/
  2459. u8 amd_iommu_pc_get_max_banks(unsigned int idx)
  2460. {
  2461. struct amd_iommu *iommu = get_amd_iommu(idx);
  2462. if (iommu)
  2463. return iommu->max_banks;
  2464. return 0;
  2465. }
  2466. EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
  2467. bool amd_iommu_pc_supported(void)
  2468. {
  2469. return amd_iommu_pc_present;
  2470. }
  2471. EXPORT_SYMBOL(amd_iommu_pc_supported);
  2472. u8 amd_iommu_pc_get_max_counters(unsigned int idx)
  2473. {
  2474. struct amd_iommu *iommu = get_amd_iommu(idx);
  2475. if (iommu)
  2476. return iommu->max_counters;
  2477. return 0;
  2478. }
  2479. EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
  2480. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  2481. u8 fxn, u64 *value, bool is_write)
  2482. {
  2483. u32 offset;
  2484. u32 max_offset_lim;
  2485. /* Make sure the IOMMU PC resource is available */
  2486. if (!amd_iommu_pc_present)
  2487. return -ENODEV;
  2488. /* Check for valid iommu and pc register indexing */
  2489. if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
  2490. return -ENODEV;
  2491. offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
  2492. /* Limit the offset to the hw defined mmio region aperture */
  2493. max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
  2494. (iommu->max_counters << 8) | 0x28);
  2495. if ((offset < MMIO_CNTR_REG_OFFSET) ||
  2496. (offset > max_offset_lim))
  2497. return -EINVAL;
  2498. if (is_write) {
  2499. u64 val = *value & GENMASK_ULL(47, 0);
  2500. writel((u32)val, iommu->mmio_base + offset);
  2501. writel((val >> 32), iommu->mmio_base + offset + 4);
  2502. } else {
  2503. *value = readl(iommu->mmio_base + offset + 4);
  2504. *value <<= 32;
  2505. *value |= readl(iommu->mmio_base + offset);
  2506. *value &= GENMASK_ULL(47, 0);
  2507. }
  2508. return 0;
  2509. }
  2510. int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  2511. {
  2512. if (!iommu)
  2513. return -EINVAL;
  2514. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
  2515. }
  2516. EXPORT_SYMBOL(amd_iommu_pc_get_reg);
  2517. int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  2518. {
  2519. if (!iommu)
  2520. return -EINVAL;
  2521. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
  2522. }
  2523. EXPORT_SYMBOL(amd_iommu_pc_set_reg);