amd_iommu_init.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  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/slab.h>
  23. #include <linux/syscore_ops.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/msi.h>
  26. #include <linux/amd-iommu.h>
  27. #include <linux/export.h>
  28. #include <linux/acpi.h>
  29. #include <acpi/acpi.h>
  30. #include <asm/pci-direct.h>
  31. #include <asm/iommu.h>
  32. #include <asm/gart.h>
  33. #include <asm/x86_init.h>
  34. #include <asm/iommu_table.h>
  35. #include "amd_iommu_proto.h"
  36. #include "amd_iommu_types.h"
  37. #include "irq_remapping.h"
  38. /*
  39. * definitions for the ACPI scanning code
  40. */
  41. #define IVRS_HEADER_LENGTH 48
  42. #define ACPI_IVHD_TYPE 0x10
  43. #define ACPI_IVMD_TYPE_ALL 0x20
  44. #define ACPI_IVMD_TYPE 0x21
  45. #define ACPI_IVMD_TYPE_RANGE 0x22
  46. #define IVHD_DEV_ALL 0x01
  47. #define IVHD_DEV_SELECT 0x02
  48. #define IVHD_DEV_SELECT_RANGE_START 0x03
  49. #define IVHD_DEV_RANGE_END 0x04
  50. #define IVHD_DEV_ALIAS 0x42
  51. #define IVHD_DEV_ALIAS_RANGE 0x43
  52. #define IVHD_DEV_EXT_SELECT 0x46
  53. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  54. #define IVHD_DEV_SPECIAL 0x48
  55. #define IVHD_SPECIAL_IOAPIC 1
  56. #define IVHD_SPECIAL_HPET 2
  57. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  58. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  59. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  60. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  61. #define IVMD_FLAG_EXCL_RANGE 0x08
  62. #define IVMD_FLAG_UNITY_MAP 0x01
  63. #define ACPI_DEVFLAG_INITPASS 0x01
  64. #define ACPI_DEVFLAG_EXTINT 0x02
  65. #define ACPI_DEVFLAG_NMI 0x04
  66. #define ACPI_DEVFLAG_SYSMGT1 0x10
  67. #define ACPI_DEVFLAG_SYSMGT2 0x20
  68. #define ACPI_DEVFLAG_LINT0 0x40
  69. #define ACPI_DEVFLAG_LINT1 0x80
  70. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  71. /*
  72. * ACPI table definitions
  73. *
  74. * These data structures are laid over the table to parse the important values
  75. * out of it.
  76. */
  77. /*
  78. * structure describing one IOMMU in the ACPI table. Typically followed by one
  79. * or more ivhd_entrys.
  80. */
  81. struct ivhd_header {
  82. u8 type;
  83. u8 flags;
  84. u16 length;
  85. u16 devid;
  86. u16 cap_ptr;
  87. u64 mmio_phys;
  88. u16 pci_seg;
  89. u16 info;
  90. u32 reserved;
  91. } __attribute__((packed));
  92. /*
  93. * A device entry describing which devices a specific IOMMU translates and
  94. * which requestor ids they use.
  95. */
  96. struct ivhd_entry {
  97. u8 type;
  98. u16 devid;
  99. u8 flags;
  100. u32 ext;
  101. } __attribute__((packed));
  102. /*
  103. * An AMD IOMMU memory definition structure. It defines things like exclusion
  104. * ranges for devices and regions that should be unity mapped.
  105. */
  106. struct ivmd_header {
  107. u8 type;
  108. u8 flags;
  109. u16 length;
  110. u16 devid;
  111. u16 aux;
  112. u64 resv;
  113. u64 range_start;
  114. u64 range_length;
  115. } __attribute__((packed));
  116. bool amd_iommu_dump;
  117. bool amd_iommu_irq_remap __read_mostly;
  118. static bool amd_iommu_detected;
  119. static bool __initdata amd_iommu_disabled;
  120. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  121. to handle */
  122. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  123. we find in ACPI */
  124. u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
  125. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  126. system */
  127. /* Array to assign indices to IOMMUs*/
  128. struct amd_iommu *amd_iommus[MAX_IOMMUS];
  129. int amd_iommus_present;
  130. /* IOMMUs have a non-present cache? */
  131. bool amd_iommu_np_cache __read_mostly;
  132. bool amd_iommu_iotlb_sup __read_mostly = true;
  133. u32 amd_iommu_max_pasids __read_mostly = ~0;
  134. bool amd_iommu_v2_present __read_mostly;
  135. bool amd_iommu_force_isolation __read_mostly;
  136. /*
  137. * List of protection domains - used during resume
  138. */
  139. LIST_HEAD(amd_iommu_pd_list);
  140. spinlock_t amd_iommu_pd_lock;
  141. /*
  142. * Pointer to the device table which is shared by all AMD IOMMUs
  143. * it is indexed by the PCI device id or the HT unit id and contains
  144. * information about the domain the device belongs to as well as the
  145. * page table root pointer.
  146. */
  147. struct dev_table_entry *amd_iommu_dev_table;
  148. /*
  149. * The alias table is a driver specific data structure which contains the
  150. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  151. * More than one device can share the same requestor id.
  152. */
  153. u16 *amd_iommu_alias_table;
  154. /*
  155. * The rlookup table is used to find the IOMMU which is responsible
  156. * for a specific device. It is also indexed by the PCI device id.
  157. */
  158. struct amd_iommu **amd_iommu_rlookup_table;
  159. /*
  160. * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
  161. * to know which ones are already in use.
  162. */
  163. unsigned long *amd_iommu_pd_alloc_bitmap;
  164. static u32 dev_table_size; /* size of the device table */
  165. static u32 alias_table_size; /* size of the alias table */
  166. static u32 rlookup_table_size; /* size if the rlookup table */
  167. enum iommu_init_state {
  168. IOMMU_START_STATE,
  169. IOMMU_IVRS_DETECTED,
  170. IOMMU_ACPI_FINISHED,
  171. IOMMU_ENABLED,
  172. IOMMU_PCI_INIT,
  173. IOMMU_INTERRUPTS_EN,
  174. IOMMU_DMA_OPS,
  175. IOMMU_INITIALIZED,
  176. IOMMU_NOT_FOUND,
  177. IOMMU_INIT_ERROR,
  178. };
  179. static enum iommu_init_state init_state = IOMMU_START_STATE;
  180. static int amd_iommu_enable_interrupts(void);
  181. static int __init iommu_go_to_state(enum iommu_init_state state);
  182. static inline void update_last_devid(u16 devid)
  183. {
  184. if (devid > amd_iommu_last_bdf)
  185. amd_iommu_last_bdf = devid;
  186. }
  187. static inline unsigned long tbl_size(int entry_size)
  188. {
  189. unsigned shift = PAGE_SHIFT +
  190. get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
  191. return 1UL << shift;
  192. }
  193. /* Access to l1 and l2 indexed register spaces */
  194. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  195. {
  196. u32 val;
  197. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  198. pci_read_config_dword(iommu->dev, 0xfc, &val);
  199. return val;
  200. }
  201. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  202. {
  203. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  204. pci_write_config_dword(iommu->dev, 0xfc, val);
  205. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  206. }
  207. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  208. {
  209. u32 val;
  210. pci_write_config_dword(iommu->dev, 0xf0, address);
  211. pci_read_config_dword(iommu->dev, 0xf4, &val);
  212. return val;
  213. }
  214. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  215. {
  216. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  217. pci_write_config_dword(iommu->dev, 0xf4, val);
  218. }
  219. /****************************************************************************
  220. *
  221. * AMD IOMMU MMIO register space handling functions
  222. *
  223. * These functions are used to program the IOMMU device registers in
  224. * MMIO space required for that driver.
  225. *
  226. ****************************************************************************/
  227. /*
  228. * This function set the exclusion range in the IOMMU. DMA accesses to the
  229. * exclusion range are passed through untranslated
  230. */
  231. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  232. {
  233. u64 start = iommu->exclusion_start & PAGE_MASK;
  234. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  235. u64 entry;
  236. if (!iommu->exclusion_start)
  237. return;
  238. entry = start | MMIO_EXCL_ENABLE_MASK;
  239. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  240. &entry, sizeof(entry));
  241. entry = limit;
  242. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  243. &entry, sizeof(entry));
  244. }
  245. /* Programs the physical address of the device table into the IOMMU hardware */
  246. static void iommu_set_device_table(struct amd_iommu *iommu)
  247. {
  248. u64 entry;
  249. BUG_ON(iommu->mmio_base == NULL);
  250. entry = virt_to_phys(amd_iommu_dev_table);
  251. entry |= (dev_table_size >> 12) - 1;
  252. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  253. &entry, sizeof(entry));
  254. }
  255. /* Generic functions to enable/disable certain features of the IOMMU. */
  256. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  257. {
  258. u32 ctrl;
  259. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  260. ctrl |= (1 << bit);
  261. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  262. }
  263. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  264. {
  265. u32 ctrl;
  266. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  267. ctrl &= ~(1 << bit);
  268. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  269. }
  270. static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
  271. {
  272. u32 ctrl;
  273. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  274. ctrl &= ~CTRL_INV_TO_MASK;
  275. ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
  276. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  277. }
  278. /* Function to enable the hardware */
  279. static void iommu_enable(struct amd_iommu *iommu)
  280. {
  281. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  282. }
  283. static void iommu_disable(struct amd_iommu *iommu)
  284. {
  285. /* Disable command buffer */
  286. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  287. /* Disable event logging and event interrupts */
  288. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  289. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  290. /* Disable IOMMU hardware itself */
  291. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  292. }
  293. /*
  294. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  295. * the system has one.
  296. */
  297. static u8 __iomem * __init iommu_map_mmio_space(u64 address)
  298. {
  299. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
  300. pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
  301. address);
  302. pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
  303. return NULL;
  304. }
  305. return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
  306. }
  307. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  308. {
  309. if (iommu->mmio_base)
  310. iounmap(iommu->mmio_base);
  311. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  312. }
  313. /****************************************************************************
  314. *
  315. * The functions below belong to the first pass of AMD IOMMU ACPI table
  316. * parsing. In this pass we try to find out the highest device id this
  317. * code has to handle. Upon this information the size of the shared data
  318. * structures is determined later.
  319. *
  320. ****************************************************************************/
  321. /*
  322. * This function calculates the length of a given IVHD entry
  323. */
  324. static inline int ivhd_entry_length(u8 *ivhd)
  325. {
  326. return 0x04 << (*ivhd >> 6);
  327. }
  328. /*
  329. * This function reads the last device id the IOMMU has to handle from the PCI
  330. * capability header for this IOMMU
  331. */
  332. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  333. {
  334. u32 cap;
  335. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  336. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  337. return 0;
  338. }
  339. /*
  340. * After reading the highest device id from the IOMMU PCI capability header
  341. * this function looks if there is a higher device id defined in the ACPI table
  342. */
  343. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  344. {
  345. u8 *p = (void *)h, *end = (void *)h;
  346. struct ivhd_entry *dev;
  347. p += sizeof(*h);
  348. end += h->length;
  349. find_last_devid_on_pci(PCI_BUS(h->devid),
  350. PCI_SLOT(h->devid),
  351. PCI_FUNC(h->devid),
  352. h->cap_ptr);
  353. while (p < end) {
  354. dev = (struct ivhd_entry *)p;
  355. switch (dev->type) {
  356. case IVHD_DEV_SELECT:
  357. case IVHD_DEV_RANGE_END:
  358. case IVHD_DEV_ALIAS:
  359. case IVHD_DEV_EXT_SELECT:
  360. /* all the above subfield types refer to device ids */
  361. update_last_devid(dev->devid);
  362. break;
  363. default:
  364. break;
  365. }
  366. p += ivhd_entry_length(p);
  367. }
  368. WARN_ON(p != end);
  369. return 0;
  370. }
  371. /*
  372. * Iterate over all IVHD entries in the ACPI table and find the highest device
  373. * id which we need to handle. This is the first of three functions which parse
  374. * the ACPI table. So we check the checksum here.
  375. */
  376. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  377. {
  378. int i;
  379. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  380. struct ivhd_header *h;
  381. /*
  382. * Validate checksum here so we don't need to do it when
  383. * we actually parse the table
  384. */
  385. for (i = 0; i < table->length; ++i)
  386. checksum += p[i];
  387. if (checksum != 0)
  388. /* ACPI table corrupt */
  389. return -ENODEV;
  390. p += IVRS_HEADER_LENGTH;
  391. end += table->length;
  392. while (p < end) {
  393. h = (struct ivhd_header *)p;
  394. switch (h->type) {
  395. case ACPI_IVHD_TYPE:
  396. find_last_devid_from_ivhd(h);
  397. break;
  398. default:
  399. break;
  400. }
  401. p += h->length;
  402. }
  403. WARN_ON(p != end);
  404. return 0;
  405. }
  406. /****************************************************************************
  407. *
  408. * The following functions belong the the code path which parses the ACPI table
  409. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  410. * data structures, initialize the device/alias/rlookup table and also
  411. * basically initialize the hardware.
  412. *
  413. ****************************************************************************/
  414. /*
  415. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  416. * write commands to that buffer later and the IOMMU will execute them
  417. * asynchronously
  418. */
  419. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  420. {
  421. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  422. get_order(CMD_BUFFER_SIZE));
  423. if (cmd_buf == NULL)
  424. return NULL;
  425. iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
  426. return cmd_buf;
  427. }
  428. /*
  429. * This function resets the command buffer if the IOMMU stopped fetching
  430. * commands from it.
  431. */
  432. void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  433. {
  434. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  435. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  436. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  437. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  438. }
  439. /*
  440. * This function writes the command buffer address to the hardware and
  441. * enables it.
  442. */
  443. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  444. {
  445. u64 entry;
  446. BUG_ON(iommu->cmd_buf == NULL);
  447. entry = (u64)virt_to_phys(iommu->cmd_buf);
  448. entry |= MMIO_CMD_SIZE_512;
  449. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  450. &entry, sizeof(entry));
  451. amd_iommu_reset_cmd_buffer(iommu);
  452. iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
  453. }
  454. static void __init free_command_buffer(struct amd_iommu *iommu)
  455. {
  456. free_pages((unsigned long)iommu->cmd_buf,
  457. get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
  458. }
  459. /* allocates the memory where the IOMMU will log its events to */
  460. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  461. {
  462. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  463. get_order(EVT_BUFFER_SIZE));
  464. if (iommu->evt_buf == NULL)
  465. return NULL;
  466. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  467. return iommu->evt_buf;
  468. }
  469. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  470. {
  471. u64 entry;
  472. BUG_ON(iommu->evt_buf == NULL);
  473. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  474. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  475. &entry, sizeof(entry));
  476. /* set head and tail to zero manually */
  477. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  478. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  479. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  480. }
  481. static void __init free_event_buffer(struct amd_iommu *iommu)
  482. {
  483. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  484. }
  485. /* allocates the memory where the IOMMU will log its events to */
  486. static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
  487. {
  488. iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  489. get_order(PPR_LOG_SIZE));
  490. if (iommu->ppr_log == NULL)
  491. return NULL;
  492. return iommu->ppr_log;
  493. }
  494. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  495. {
  496. u64 entry;
  497. if (iommu->ppr_log == NULL)
  498. return;
  499. entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  500. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  501. &entry, sizeof(entry));
  502. /* set head and tail to zero manually */
  503. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  504. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  505. iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
  506. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  507. }
  508. static void __init free_ppr_log(struct amd_iommu *iommu)
  509. {
  510. if (iommu->ppr_log == NULL)
  511. return;
  512. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  513. }
  514. static void iommu_enable_gt(struct amd_iommu *iommu)
  515. {
  516. if (!iommu_feature(iommu, FEATURE_GT))
  517. return;
  518. iommu_feature_enable(iommu, CONTROL_GT_EN);
  519. }
  520. /* sets a specific bit in the device table entry. */
  521. static void set_dev_entry_bit(u16 devid, u8 bit)
  522. {
  523. int i = (bit >> 6) & 0x03;
  524. int _bit = bit & 0x3f;
  525. amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
  526. }
  527. static int get_dev_entry_bit(u16 devid, u8 bit)
  528. {
  529. int i = (bit >> 6) & 0x03;
  530. int _bit = bit & 0x3f;
  531. return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  532. }
  533. void amd_iommu_apply_erratum_63(u16 devid)
  534. {
  535. int sysmgt;
  536. sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
  537. (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
  538. if (sysmgt == 0x01)
  539. set_dev_entry_bit(devid, DEV_ENTRY_IW);
  540. }
  541. /* Writes the specific IOMMU for a device into the rlookup table */
  542. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  543. {
  544. amd_iommu_rlookup_table[devid] = iommu;
  545. }
  546. /*
  547. * This function takes the device specific flags read from the ACPI
  548. * table and sets up the device table entry with that information
  549. */
  550. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  551. u16 devid, u32 flags, u32 ext_flags)
  552. {
  553. if (flags & ACPI_DEVFLAG_INITPASS)
  554. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  555. if (flags & ACPI_DEVFLAG_EXTINT)
  556. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  557. if (flags & ACPI_DEVFLAG_NMI)
  558. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  559. if (flags & ACPI_DEVFLAG_SYSMGT1)
  560. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  561. if (flags & ACPI_DEVFLAG_SYSMGT2)
  562. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  563. if (flags & ACPI_DEVFLAG_LINT0)
  564. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  565. if (flags & ACPI_DEVFLAG_LINT1)
  566. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  567. amd_iommu_apply_erratum_63(devid);
  568. set_iommu_for_device(iommu, devid);
  569. }
  570. static int add_special_device(u8 type, u8 id, u16 devid)
  571. {
  572. struct devid_map *entry;
  573. struct list_head *list;
  574. if (type != IVHD_SPECIAL_IOAPIC && type != IVHD_SPECIAL_HPET)
  575. return -EINVAL;
  576. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  577. if (!entry)
  578. return -ENOMEM;
  579. entry->id = id;
  580. entry->devid = devid;
  581. if (type == IVHD_SPECIAL_IOAPIC)
  582. list = &ioapic_map;
  583. else
  584. list = &hpet_map;
  585. list_add_tail(&entry->list, list);
  586. return 0;
  587. }
  588. /*
  589. * Reads the device exclusion range from ACPI and initialize IOMMU with
  590. * it
  591. */
  592. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  593. {
  594. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  595. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  596. return;
  597. if (iommu) {
  598. /*
  599. * We only can configure exclusion ranges per IOMMU, not
  600. * per device. But we can enable the exclusion range per
  601. * device. This is done here
  602. */
  603. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  604. iommu->exclusion_start = m->range_start;
  605. iommu->exclusion_length = m->range_length;
  606. }
  607. }
  608. /*
  609. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  610. * initializes the hardware and our data structures with it.
  611. */
  612. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  613. struct ivhd_header *h)
  614. {
  615. u8 *p = (u8 *)h;
  616. u8 *end = p, flags = 0;
  617. u16 devid = 0, devid_start = 0, devid_to = 0;
  618. u32 dev_i, ext_flags = 0;
  619. bool alias = false;
  620. struct ivhd_entry *e;
  621. /*
  622. * First save the recommended feature enable bits from ACPI
  623. */
  624. iommu->acpi_flags = h->flags;
  625. /*
  626. * Done. Now parse the device entries
  627. */
  628. p += sizeof(struct ivhd_header);
  629. end += h->length;
  630. while (p < end) {
  631. e = (struct ivhd_entry *)p;
  632. switch (e->type) {
  633. case IVHD_DEV_ALL:
  634. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  635. " last device %02x:%02x.%x flags: %02x\n",
  636. PCI_BUS(iommu->first_device),
  637. PCI_SLOT(iommu->first_device),
  638. PCI_FUNC(iommu->first_device),
  639. PCI_BUS(iommu->last_device),
  640. PCI_SLOT(iommu->last_device),
  641. PCI_FUNC(iommu->last_device),
  642. e->flags);
  643. for (dev_i = iommu->first_device;
  644. dev_i <= iommu->last_device; ++dev_i)
  645. set_dev_entry_from_acpi(iommu, dev_i,
  646. e->flags, 0);
  647. break;
  648. case IVHD_DEV_SELECT:
  649. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  650. "flags: %02x\n",
  651. PCI_BUS(e->devid),
  652. PCI_SLOT(e->devid),
  653. PCI_FUNC(e->devid),
  654. e->flags);
  655. devid = e->devid;
  656. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  657. break;
  658. case IVHD_DEV_SELECT_RANGE_START:
  659. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  660. "devid: %02x:%02x.%x flags: %02x\n",
  661. PCI_BUS(e->devid),
  662. PCI_SLOT(e->devid),
  663. PCI_FUNC(e->devid),
  664. e->flags);
  665. devid_start = e->devid;
  666. flags = e->flags;
  667. ext_flags = 0;
  668. alias = false;
  669. break;
  670. case IVHD_DEV_ALIAS:
  671. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  672. "flags: %02x devid_to: %02x:%02x.%x\n",
  673. PCI_BUS(e->devid),
  674. PCI_SLOT(e->devid),
  675. PCI_FUNC(e->devid),
  676. e->flags,
  677. PCI_BUS(e->ext >> 8),
  678. PCI_SLOT(e->ext >> 8),
  679. PCI_FUNC(e->ext >> 8));
  680. devid = e->devid;
  681. devid_to = e->ext >> 8;
  682. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  683. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  684. amd_iommu_alias_table[devid] = devid_to;
  685. break;
  686. case IVHD_DEV_ALIAS_RANGE:
  687. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  688. "devid: %02x:%02x.%x flags: %02x "
  689. "devid_to: %02x:%02x.%x\n",
  690. PCI_BUS(e->devid),
  691. PCI_SLOT(e->devid),
  692. PCI_FUNC(e->devid),
  693. e->flags,
  694. PCI_BUS(e->ext >> 8),
  695. PCI_SLOT(e->ext >> 8),
  696. PCI_FUNC(e->ext >> 8));
  697. devid_start = e->devid;
  698. flags = e->flags;
  699. devid_to = e->ext >> 8;
  700. ext_flags = 0;
  701. alias = true;
  702. break;
  703. case IVHD_DEV_EXT_SELECT:
  704. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  705. "flags: %02x ext: %08x\n",
  706. PCI_BUS(e->devid),
  707. PCI_SLOT(e->devid),
  708. PCI_FUNC(e->devid),
  709. e->flags, e->ext);
  710. devid = e->devid;
  711. set_dev_entry_from_acpi(iommu, devid, e->flags,
  712. e->ext);
  713. break;
  714. case IVHD_DEV_EXT_SELECT_RANGE:
  715. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  716. "%02x:%02x.%x flags: %02x ext: %08x\n",
  717. PCI_BUS(e->devid),
  718. PCI_SLOT(e->devid),
  719. PCI_FUNC(e->devid),
  720. e->flags, e->ext);
  721. devid_start = e->devid;
  722. flags = e->flags;
  723. ext_flags = e->ext;
  724. alias = false;
  725. break;
  726. case IVHD_DEV_RANGE_END:
  727. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  728. PCI_BUS(e->devid),
  729. PCI_SLOT(e->devid),
  730. PCI_FUNC(e->devid));
  731. devid = e->devid;
  732. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  733. if (alias) {
  734. amd_iommu_alias_table[dev_i] = devid_to;
  735. set_dev_entry_from_acpi(iommu,
  736. devid_to, flags, ext_flags);
  737. }
  738. set_dev_entry_from_acpi(iommu, dev_i,
  739. flags, ext_flags);
  740. }
  741. break;
  742. case IVHD_DEV_SPECIAL: {
  743. u8 handle, type;
  744. const char *var;
  745. u16 devid;
  746. int ret;
  747. handle = e->ext & 0xff;
  748. devid = (e->ext >> 8) & 0xffff;
  749. type = (e->ext >> 24) & 0xff;
  750. if (type == IVHD_SPECIAL_IOAPIC)
  751. var = "IOAPIC";
  752. else if (type == IVHD_SPECIAL_HPET)
  753. var = "HPET";
  754. else
  755. var = "UNKNOWN";
  756. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
  757. var, (int)handle,
  758. PCI_BUS(devid),
  759. PCI_SLOT(devid),
  760. PCI_FUNC(devid));
  761. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  762. ret = add_special_device(type, handle, devid);
  763. if (ret)
  764. return ret;
  765. break;
  766. }
  767. default:
  768. break;
  769. }
  770. p += ivhd_entry_length(p);
  771. }
  772. return 0;
  773. }
  774. /* Initializes the device->iommu mapping for the driver */
  775. static int __init init_iommu_devices(struct amd_iommu *iommu)
  776. {
  777. u32 i;
  778. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  779. set_iommu_for_device(iommu, i);
  780. return 0;
  781. }
  782. static void __init free_iommu_one(struct amd_iommu *iommu)
  783. {
  784. free_command_buffer(iommu);
  785. free_event_buffer(iommu);
  786. free_ppr_log(iommu);
  787. iommu_unmap_mmio_space(iommu);
  788. }
  789. static void __init free_iommu_all(void)
  790. {
  791. struct amd_iommu *iommu, *next;
  792. for_each_iommu_safe(iommu, next) {
  793. list_del(&iommu->list);
  794. free_iommu_one(iommu);
  795. kfree(iommu);
  796. }
  797. }
  798. /*
  799. * This function clues the initialization function for one IOMMU
  800. * together and also allocates the command buffer and programs the
  801. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  802. */
  803. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  804. {
  805. int ret;
  806. spin_lock_init(&iommu->lock);
  807. /* Add IOMMU to internal data structures */
  808. list_add_tail(&iommu->list, &amd_iommu_list);
  809. iommu->index = amd_iommus_present++;
  810. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  811. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  812. return -ENOSYS;
  813. }
  814. /* Index is fine - add IOMMU to the array */
  815. amd_iommus[iommu->index] = iommu;
  816. /*
  817. * Copy data from ACPI table entry to the iommu struct
  818. */
  819. iommu->devid = h->devid;
  820. iommu->cap_ptr = h->cap_ptr;
  821. iommu->pci_seg = h->pci_seg;
  822. iommu->mmio_phys = h->mmio_phys;
  823. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  824. if (!iommu->mmio_base)
  825. return -ENOMEM;
  826. iommu->cmd_buf = alloc_command_buffer(iommu);
  827. if (!iommu->cmd_buf)
  828. return -ENOMEM;
  829. iommu->evt_buf = alloc_event_buffer(iommu);
  830. if (!iommu->evt_buf)
  831. return -ENOMEM;
  832. iommu->int_enabled = false;
  833. ret = init_iommu_from_acpi(iommu, h);
  834. if (ret)
  835. return ret;
  836. init_iommu_devices(iommu);
  837. return 0;
  838. }
  839. /*
  840. * Iterates over all IOMMU entries in the ACPI table, allocates the
  841. * IOMMU structure and initializes it with init_iommu_one()
  842. */
  843. static int __init init_iommu_all(struct acpi_table_header *table)
  844. {
  845. u8 *p = (u8 *)table, *end = (u8 *)table;
  846. struct ivhd_header *h;
  847. struct amd_iommu *iommu;
  848. int ret;
  849. end += table->length;
  850. p += IVRS_HEADER_LENGTH;
  851. while (p < end) {
  852. h = (struct ivhd_header *)p;
  853. switch (*p) {
  854. case ACPI_IVHD_TYPE:
  855. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  856. "seg: %d flags: %01x info %04x\n",
  857. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  858. PCI_FUNC(h->devid), h->cap_ptr,
  859. h->pci_seg, h->flags, h->info);
  860. DUMP_printk(" mmio-addr: %016llx\n",
  861. h->mmio_phys);
  862. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  863. if (iommu == NULL)
  864. return -ENOMEM;
  865. ret = init_iommu_one(iommu, h);
  866. if (ret)
  867. return ret;
  868. break;
  869. default:
  870. break;
  871. }
  872. p += h->length;
  873. }
  874. WARN_ON(p != end);
  875. return 0;
  876. }
  877. static int iommu_init_pci(struct amd_iommu *iommu)
  878. {
  879. int cap_ptr = iommu->cap_ptr;
  880. u32 range, misc, low, high;
  881. iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
  882. iommu->devid & 0xff);
  883. if (!iommu->dev)
  884. return -ENODEV;
  885. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  886. &iommu->cap);
  887. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  888. &range);
  889. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  890. &misc);
  891. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  892. MMIO_GET_FD(range));
  893. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  894. MMIO_GET_LD(range));
  895. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  896. amd_iommu_iotlb_sup = false;
  897. /* read extended feature bits */
  898. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  899. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  900. iommu->features = ((u64)high << 32) | low;
  901. if (iommu_feature(iommu, FEATURE_GT)) {
  902. int glxval;
  903. u32 pasids;
  904. u64 shift;
  905. shift = iommu->features & FEATURE_PASID_MASK;
  906. shift >>= FEATURE_PASID_SHIFT;
  907. pasids = (1 << shift);
  908. amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
  909. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  910. glxval >>= FEATURE_GLXVAL_SHIFT;
  911. if (amd_iommu_max_glx_val == -1)
  912. amd_iommu_max_glx_val = glxval;
  913. else
  914. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  915. }
  916. if (iommu_feature(iommu, FEATURE_GT) &&
  917. iommu_feature(iommu, FEATURE_PPR)) {
  918. iommu->is_iommu_v2 = true;
  919. amd_iommu_v2_present = true;
  920. }
  921. if (iommu_feature(iommu, FEATURE_PPR)) {
  922. iommu->ppr_log = alloc_ppr_log(iommu);
  923. if (!iommu->ppr_log)
  924. return -ENOMEM;
  925. }
  926. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  927. amd_iommu_np_cache = true;
  928. if (is_rd890_iommu(iommu->dev)) {
  929. int i, j;
  930. iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
  931. PCI_DEVFN(0, 0));
  932. /*
  933. * Some rd890 systems may not be fully reconfigured by the
  934. * BIOS, so it's necessary for us to store this information so
  935. * it can be reprogrammed on resume
  936. */
  937. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  938. &iommu->stored_addr_lo);
  939. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  940. &iommu->stored_addr_hi);
  941. /* Low bit locks writes to configuration space */
  942. iommu->stored_addr_lo &= ~1;
  943. for (i = 0; i < 6; i++)
  944. for (j = 0; j < 0x12; j++)
  945. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  946. for (i = 0; i < 0x83; i++)
  947. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  948. }
  949. return pci_enable_device(iommu->dev);
  950. }
  951. static void print_iommu_info(void)
  952. {
  953. static const char * const feat_str[] = {
  954. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  955. "IA", "GA", "HE", "PC"
  956. };
  957. struct amd_iommu *iommu;
  958. for_each_iommu(iommu) {
  959. int i;
  960. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  961. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  962. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  963. pr_info("AMD-Vi: Extended features: ");
  964. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  965. if (iommu_feature(iommu, (1ULL << i)))
  966. pr_cont(" %s", feat_str[i]);
  967. }
  968. }
  969. pr_cont("\n");
  970. }
  971. }
  972. static int __init amd_iommu_init_pci(void)
  973. {
  974. struct amd_iommu *iommu;
  975. int ret = 0;
  976. for_each_iommu(iommu) {
  977. ret = iommu_init_pci(iommu);
  978. if (ret)
  979. break;
  980. }
  981. ret = amd_iommu_init_devices();
  982. print_iommu_info();
  983. return ret;
  984. }
  985. /****************************************************************************
  986. *
  987. * The following functions initialize the MSI interrupts for all IOMMUs
  988. * in the system. Its a bit challenging because there could be multiple
  989. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  990. * pci_dev.
  991. *
  992. ****************************************************************************/
  993. static int iommu_setup_msi(struct amd_iommu *iommu)
  994. {
  995. int r;
  996. r = pci_enable_msi(iommu->dev);
  997. if (r)
  998. return r;
  999. r = request_threaded_irq(iommu->dev->irq,
  1000. amd_iommu_int_handler,
  1001. amd_iommu_int_thread,
  1002. 0, "AMD-Vi",
  1003. iommu->dev);
  1004. if (r) {
  1005. pci_disable_msi(iommu->dev);
  1006. return r;
  1007. }
  1008. iommu->int_enabled = true;
  1009. return 0;
  1010. }
  1011. static int iommu_init_msi(struct amd_iommu *iommu)
  1012. {
  1013. int ret;
  1014. if (iommu->int_enabled)
  1015. goto enable_faults;
  1016. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  1017. ret = iommu_setup_msi(iommu);
  1018. else
  1019. ret = -ENODEV;
  1020. if (ret)
  1021. return ret;
  1022. enable_faults:
  1023. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  1024. if (iommu->ppr_log != NULL)
  1025. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  1026. return 0;
  1027. }
  1028. /****************************************************************************
  1029. *
  1030. * The next functions belong to the third pass of parsing the ACPI
  1031. * table. In this last pass the memory mapping requirements are
  1032. * gathered (like exclusion and unity mapping reanges).
  1033. *
  1034. ****************************************************************************/
  1035. static void __init free_unity_maps(void)
  1036. {
  1037. struct unity_map_entry *entry, *next;
  1038. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  1039. list_del(&entry->list);
  1040. kfree(entry);
  1041. }
  1042. }
  1043. /* called when we find an exclusion range definition in ACPI */
  1044. static int __init init_exclusion_range(struct ivmd_header *m)
  1045. {
  1046. int i;
  1047. switch (m->type) {
  1048. case ACPI_IVMD_TYPE:
  1049. set_device_exclusion_range(m->devid, m);
  1050. break;
  1051. case ACPI_IVMD_TYPE_ALL:
  1052. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1053. set_device_exclusion_range(i, m);
  1054. break;
  1055. case ACPI_IVMD_TYPE_RANGE:
  1056. for (i = m->devid; i <= m->aux; ++i)
  1057. set_device_exclusion_range(i, m);
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. return 0;
  1063. }
  1064. /* called for unity map ACPI definition */
  1065. static int __init init_unity_map_range(struct ivmd_header *m)
  1066. {
  1067. struct unity_map_entry *e = NULL;
  1068. char *s;
  1069. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1070. if (e == NULL)
  1071. return -ENOMEM;
  1072. switch (m->type) {
  1073. default:
  1074. kfree(e);
  1075. return 0;
  1076. case ACPI_IVMD_TYPE:
  1077. s = "IVMD_TYPEi\t\t\t";
  1078. e->devid_start = e->devid_end = m->devid;
  1079. break;
  1080. case ACPI_IVMD_TYPE_ALL:
  1081. s = "IVMD_TYPE_ALL\t\t";
  1082. e->devid_start = 0;
  1083. e->devid_end = amd_iommu_last_bdf;
  1084. break;
  1085. case ACPI_IVMD_TYPE_RANGE:
  1086. s = "IVMD_TYPE_RANGE\t\t";
  1087. e->devid_start = m->devid;
  1088. e->devid_end = m->aux;
  1089. break;
  1090. }
  1091. e->address_start = PAGE_ALIGN(m->range_start);
  1092. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1093. e->prot = m->flags >> 1;
  1094. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1095. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1096. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  1097. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  1098. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1099. e->address_start, e->address_end, m->flags);
  1100. list_add_tail(&e->list, &amd_iommu_unity_map);
  1101. return 0;
  1102. }
  1103. /* iterates over all memory definitions we find in the ACPI table */
  1104. static int __init init_memory_definitions(struct acpi_table_header *table)
  1105. {
  1106. u8 *p = (u8 *)table, *end = (u8 *)table;
  1107. struct ivmd_header *m;
  1108. end += table->length;
  1109. p += IVRS_HEADER_LENGTH;
  1110. while (p < end) {
  1111. m = (struct ivmd_header *)p;
  1112. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1113. init_exclusion_range(m);
  1114. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  1115. init_unity_map_range(m);
  1116. p += m->length;
  1117. }
  1118. return 0;
  1119. }
  1120. /*
  1121. * Init the device table to not allow DMA access for devices and
  1122. * suppress all page faults
  1123. */
  1124. static void init_device_table(void)
  1125. {
  1126. u32 devid;
  1127. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1128. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1129. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1130. }
  1131. }
  1132. static void iommu_init_flags(struct amd_iommu *iommu)
  1133. {
  1134. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1135. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1136. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1137. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1138. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1139. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1140. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1141. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1142. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1143. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1144. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1145. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1146. /*
  1147. * make IOMMU memory accesses cache coherent
  1148. */
  1149. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1150. /* Set IOTLB invalidation timeout to 1s */
  1151. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1152. }
  1153. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1154. {
  1155. int i, j;
  1156. u32 ioc_feature_control;
  1157. struct pci_dev *pdev = iommu->root_pdev;
  1158. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1159. if (!is_rd890_iommu(iommu->dev) || !pdev)
  1160. return;
  1161. /*
  1162. * First, we need to ensure that the iommu is enabled. This is
  1163. * controlled by a register in the northbridge
  1164. */
  1165. /* Select Northbridge indirect register 0x75 and enable writing */
  1166. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1167. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1168. /* Enable the iommu */
  1169. if (!(ioc_feature_control & 0x1))
  1170. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1171. /* Restore the iommu BAR */
  1172. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1173. iommu->stored_addr_lo);
  1174. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1175. iommu->stored_addr_hi);
  1176. /* Restore the l1 indirect regs for each of the 6 l1s */
  1177. for (i = 0; i < 6; i++)
  1178. for (j = 0; j < 0x12; j++)
  1179. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1180. /* Restore the l2 indirect regs */
  1181. for (i = 0; i < 0x83; i++)
  1182. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1183. /* Lock PCI setup registers */
  1184. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1185. iommu->stored_addr_lo | 1);
  1186. }
  1187. /*
  1188. * This function finally enables all IOMMUs found in the system after
  1189. * they have been initialized
  1190. */
  1191. static void early_enable_iommus(void)
  1192. {
  1193. struct amd_iommu *iommu;
  1194. for_each_iommu(iommu) {
  1195. iommu_disable(iommu);
  1196. iommu_init_flags(iommu);
  1197. iommu_set_device_table(iommu);
  1198. iommu_enable_command_buffer(iommu);
  1199. iommu_enable_event_buffer(iommu);
  1200. iommu_set_exclusion_range(iommu);
  1201. iommu_enable(iommu);
  1202. iommu_flush_all_caches(iommu);
  1203. }
  1204. }
  1205. static void enable_iommus_v2(void)
  1206. {
  1207. struct amd_iommu *iommu;
  1208. for_each_iommu(iommu) {
  1209. iommu_enable_ppr_log(iommu);
  1210. iommu_enable_gt(iommu);
  1211. }
  1212. }
  1213. static void enable_iommus(void)
  1214. {
  1215. early_enable_iommus();
  1216. enable_iommus_v2();
  1217. }
  1218. static void disable_iommus(void)
  1219. {
  1220. struct amd_iommu *iommu;
  1221. for_each_iommu(iommu)
  1222. iommu_disable(iommu);
  1223. }
  1224. /*
  1225. * Suspend/Resume support
  1226. * disable suspend until real resume implemented
  1227. */
  1228. static void amd_iommu_resume(void)
  1229. {
  1230. struct amd_iommu *iommu;
  1231. for_each_iommu(iommu)
  1232. iommu_apply_resume_quirks(iommu);
  1233. /* re-load the hardware */
  1234. enable_iommus();
  1235. amd_iommu_enable_interrupts();
  1236. }
  1237. static int amd_iommu_suspend(void)
  1238. {
  1239. /* disable IOMMUs to go out of the way for BIOS */
  1240. disable_iommus();
  1241. return 0;
  1242. }
  1243. static struct syscore_ops amd_iommu_syscore_ops = {
  1244. .suspend = amd_iommu_suspend,
  1245. .resume = amd_iommu_resume,
  1246. };
  1247. static void __init free_on_init_error(void)
  1248. {
  1249. if (amd_iommu_irq_cache) {
  1250. kmem_cache_destroy(amd_iommu_irq_cache);
  1251. amd_iommu_irq_cache = NULL;
  1252. }
  1253. amd_iommu_uninit_devices();
  1254. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1255. get_order(MAX_DOMAIN_ID/8));
  1256. free_pages((unsigned long)amd_iommu_rlookup_table,
  1257. get_order(rlookup_table_size));
  1258. free_pages((unsigned long)amd_iommu_alias_table,
  1259. get_order(alias_table_size));
  1260. free_pages((unsigned long)amd_iommu_dev_table,
  1261. get_order(dev_table_size));
  1262. free_iommu_all();
  1263. free_unity_maps();
  1264. #ifdef CONFIG_GART_IOMMU
  1265. /*
  1266. * We failed to initialize the AMD IOMMU - try fallback to GART
  1267. * if possible.
  1268. */
  1269. gart_iommu_init();
  1270. #endif
  1271. }
  1272. /*
  1273. * This is the hardware init function for AMD IOMMU in the system.
  1274. * This function is called either from amd_iommu_init or from the interrupt
  1275. * remapping setup code.
  1276. *
  1277. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1278. * three times:
  1279. *
  1280. * 1 pass) Find the highest PCI device id the driver has to handle.
  1281. * Upon this information the size of the data structures is
  1282. * determined that needs to be allocated.
  1283. *
  1284. * 2 pass) Initialize the data structures just allocated with the
  1285. * information in the ACPI table about available AMD IOMMUs
  1286. * in the system. It also maps the PCI devices in the
  1287. * system to specific IOMMUs
  1288. *
  1289. * 3 pass) After the basic data structures are allocated and
  1290. * initialized we update them with information about memory
  1291. * remapping requirements parsed out of the ACPI table in
  1292. * this last pass.
  1293. *
  1294. * After everything is set up the IOMMUs are enabled and the necessary
  1295. * hotplug and suspend notifiers are registered.
  1296. */
  1297. static int __init early_amd_iommu_init(void)
  1298. {
  1299. struct acpi_table_header *ivrs_base;
  1300. acpi_size ivrs_size;
  1301. acpi_status status;
  1302. int i, ret = 0;
  1303. if (!amd_iommu_detected)
  1304. return -ENODEV;
  1305. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1306. if (status == AE_NOT_FOUND)
  1307. return -ENODEV;
  1308. else if (ACPI_FAILURE(status)) {
  1309. const char *err = acpi_format_exception(status);
  1310. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1311. return -EINVAL;
  1312. }
  1313. /*
  1314. * First parse ACPI tables to find the largest Bus/Dev/Func
  1315. * we need to handle. Upon this information the shared data
  1316. * structures for the IOMMUs in the system will be allocated
  1317. */
  1318. ret = find_last_devid_acpi(ivrs_base);
  1319. if (ret)
  1320. goto out;
  1321. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  1322. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  1323. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  1324. /* Device table - directly used by all IOMMUs */
  1325. ret = -ENOMEM;
  1326. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1327. get_order(dev_table_size));
  1328. if (amd_iommu_dev_table == NULL)
  1329. goto out;
  1330. /*
  1331. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  1332. * IOMMU see for that device
  1333. */
  1334. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  1335. get_order(alias_table_size));
  1336. if (amd_iommu_alias_table == NULL)
  1337. goto out;
  1338. /* IOMMU rlookup table - find the IOMMU for a specific device */
  1339. amd_iommu_rlookup_table = (void *)__get_free_pages(
  1340. GFP_KERNEL | __GFP_ZERO,
  1341. get_order(rlookup_table_size));
  1342. if (amd_iommu_rlookup_table == NULL)
  1343. goto out;
  1344. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  1345. GFP_KERNEL | __GFP_ZERO,
  1346. get_order(MAX_DOMAIN_ID/8));
  1347. if (amd_iommu_pd_alloc_bitmap == NULL)
  1348. goto out;
  1349. /* init the device table */
  1350. init_device_table();
  1351. /*
  1352. * let all alias entries point to itself
  1353. */
  1354. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1355. amd_iommu_alias_table[i] = i;
  1356. /*
  1357. * never allocate domain 0 because its used as the non-allocated and
  1358. * error value placeholder
  1359. */
  1360. amd_iommu_pd_alloc_bitmap[0] = 1;
  1361. spin_lock_init(&amd_iommu_pd_lock);
  1362. /*
  1363. * now the data structures are allocated and basically initialized
  1364. * start the real acpi table scan
  1365. */
  1366. ret = init_iommu_all(ivrs_base);
  1367. if (ret)
  1368. goto out;
  1369. if (amd_iommu_irq_remap) {
  1370. /*
  1371. * Interrupt remapping enabled, create kmem_cache for the
  1372. * remapping tables.
  1373. */
  1374. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  1375. MAX_IRQS_PER_TABLE * sizeof(u32),
  1376. IRQ_TABLE_ALIGNMENT,
  1377. 0, NULL);
  1378. if (!amd_iommu_irq_cache)
  1379. goto out;
  1380. }
  1381. ret = init_memory_definitions(ivrs_base);
  1382. if (ret)
  1383. goto out;
  1384. out:
  1385. /* Don't leak any ACPI memory */
  1386. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1387. ivrs_base = NULL;
  1388. return ret;
  1389. }
  1390. static int amd_iommu_enable_interrupts(void)
  1391. {
  1392. struct amd_iommu *iommu;
  1393. int ret = 0;
  1394. for_each_iommu(iommu) {
  1395. ret = iommu_init_msi(iommu);
  1396. if (ret)
  1397. goto out;
  1398. }
  1399. out:
  1400. return ret;
  1401. }
  1402. static bool detect_ivrs(void)
  1403. {
  1404. struct acpi_table_header *ivrs_base;
  1405. acpi_size ivrs_size;
  1406. acpi_status status;
  1407. status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
  1408. if (status == AE_NOT_FOUND)
  1409. return false;
  1410. else if (ACPI_FAILURE(status)) {
  1411. const char *err = acpi_format_exception(status);
  1412. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  1413. return false;
  1414. }
  1415. early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
  1416. /* Make sure ACS will be enabled during PCI probe */
  1417. pci_request_acs();
  1418. if (!disable_irq_remap)
  1419. amd_iommu_irq_remap = true;
  1420. return true;
  1421. }
  1422. static int amd_iommu_init_dma(void)
  1423. {
  1424. int ret;
  1425. if (iommu_pass_through)
  1426. ret = amd_iommu_init_passthrough();
  1427. else
  1428. ret = amd_iommu_init_dma_ops();
  1429. if (ret)
  1430. return ret;
  1431. amd_iommu_init_api();
  1432. amd_iommu_init_notifier();
  1433. return 0;
  1434. }
  1435. /****************************************************************************
  1436. *
  1437. * AMD IOMMU Initialization State Machine
  1438. *
  1439. ****************************************************************************/
  1440. static int __init state_next(void)
  1441. {
  1442. int ret = 0;
  1443. switch (init_state) {
  1444. case IOMMU_START_STATE:
  1445. if (!detect_ivrs()) {
  1446. init_state = IOMMU_NOT_FOUND;
  1447. ret = -ENODEV;
  1448. } else {
  1449. init_state = IOMMU_IVRS_DETECTED;
  1450. }
  1451. break;
  1452. case IOMMU_IVRS_DETECTED:
  1453. ret = early_amd_iommu_init();
  1454. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  1455. break;
  1456. case IOMMU_ACPI_FINISHED:
  1457. early_enable_iommus();
  1458. register_syscore_ops(&amd_iommu_syscore_ops);
  1459. x86_platform.iommu_shutdown = disable_iommus;
  1460. init_state = IOMMU_ENABLED;
  1461. break;
  1462. case IOMMU_ENABLED:
  1463. ret = amd_iommu_init_pci();
  1464. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  1465. enable_iommus_v2();
  1466. break;
  1467. case IOMMU_PCI_INIT:
  1468. ret = amd_iommu_enable_interrupts();
  1469. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  1470. break;
  1471. case IOMMU_INTERRUPTS_EN:
  1472. ret = amd_iommu_init_dma();
  1473. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  1474. break;
  1475. case IOMMU_DMA_OPS:
  1476. init_state = IOMMU_INITIALIZED;
  1477. break;
  1478. case IOMMU_INITIALIZED:
  1479. /* Nothing to do */
  1480. break;
  1481. case IOMMU_NOT_FOUND:
  1482. case IOMMU_INIT_ERROR:
  1483. /* Error states => do nothing */
  1484. ret = -EINVAL;
  1485. break;
  1486. default:
  1487. /* Unknown state */
  1488. BUG();
  1489. }
  1490. return ret;
  1491. }
  1492. static int __init iommu_go_to_state(enum iommu_init_state state)
  1493. {
  1494. int ret = 0;
  1495. while (init_state != state) {
  1496. ret = state_next();
  1497. if (init_state == IOMMU_NOT_FOUND ||
  1498. init_state == IOMMU_INIT_ERROR)
  1499. break;
  1500. }
  1501. return ret;
  1502. }
  1503. /*
  1504. * This is the core init function for AMD IOMMU hardware in the system.
  1505. * This function is called from the generic x86 DMA layer initialization
  1506. * code.
  1507. */
  1508. static int __init amd_iommu_init(void)
  1509. {
  1510. int ret;
  1511. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  1512. if (ret) {
  1513. disable_iommus();
  1514. free_on_init_error();
  1515. }
  1516. return ret;
  1517. }
  1518. /****************************************************************************
  1519. *
  1520. * Early detect code. This code runs at IOMMU detection time in the DMA
  1521. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1522. * IOMMUs
  1523. *
  1524. ****************************************************************************/
  1525. int __init amd_iommu_detect(void)
  1526. {
  1527. int ret;
  1528. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  1529. return -ENODEV;
  1530. if (amd_iommu_disabled)
  1531. return -ENODEV;
  1532. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  1533. if (ret)
  1534. return ret;
  1535. amd_iommu_detected = true;
  1536. iommu_detected = 1;
  1537. x86_init.iommu.iommu_init = amd_iommu_init;
  1538. return 0;
  1539. }
  1540. /****************************************************************************
  1541. *
  1542. * Parsing functions for the AMD IOMMU specific kernel command line
  1543. * options.
  1544. *
  1545. ****************************************************************************/
  1546. static int __init parse_amd_iommu_dump(char *str)
  1547. {
  1548. amd_iommu_dump = true;
  1549. return 1;
  1550. }
  1551. static int __init parse_amd_iommu_options(char *str)
  1552. {
  1553. for (; *str; ++str) {
  1554. if (strncmp(str, "fullflush", 9) == 0)
  1555. amd_iommu_unmap_flush = true;
  1556. if (strncmp(str, "off", 3) == 0)
  1557. amd_iommu_disabled = true;
  1558. if (strncmp(str, "force_isolation", 15) == 0)
  1559. amd_iommu_force_isolation = true;
  1560. }
  1561. return 1;
  1562. }
  1563. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1564. __setup("amd_iommu=", parse_amd_iommu_options);
  1565. IOMMU_INIT_FINISH(amd_iommu_detect,
  1566. gart_iommu_hole_init,
  1567. NULL,
  1568. NULL);
  1569. bool amd_iommu_v2_supported(void)
  1570. {
  1571. return amd_iommu_v2_present;
  1572. }
  1573. EXPORT_SYMBOL(amd_iommu_v2_supported);