amd_iommu_init.c 68 KB

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