amd_iommu_init.c 75 KB

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