amd_iommu_init.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Copyright (C) 2007-2008 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/gfp.h>
  22. #include <linux/list.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/msi.h>
  26. #include <asm/pci-direct.h>
  27. #include <asm/amd_iommu_types.h>
  28. #include <asm/amd_iommu.h>
  29. #include <asm/iommu.h>
  30. #include <asm/gart.h>
  31. /*
  32. * definitions for the ACPI scanning code
  33. */
  34. #define IVRS_HEADER_LENGTH 48
  35. #define ACPI_IVHD_TYPE 0x10
  36. #define ACPI_IVMD_TYPE_ALL 0x20
  37. #define ACPI_IVMD_TYPE 0x21
  38. #define ACPI_IVMD_TYPE_RANGE 0x22
  39. #define IVHD_DEV_ALL 0x01
  40. #define IVHD_DEV_SELECT 0x02
  41. #define IVHD_DEV_SELECT_RANGE_START 0x03
  42. #define IVHD_DEV_RANGE_END 0x04
  43. #define IVHD_DEV_ALIAS 0x42
  44. #define IVHD_DEV_ALIAS_RANGE 0x43
  45. #define IVHD_DEV_EXT_SELECT 0x46
  46. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  47. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  48. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  49. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  50. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  51. #define IVMD_FLAG_EXCL_RANGE 0x08
  52. #define IVMD_FLAG_UNITY_MAP 0x01
  53. #define ACPI_DEVFLAG_INITPASS 0x01
  54. #define ACPI_DEVFLAG_EXTINT 0x02
  55. #define ACPI_DEVFLAG_NMI 0x04
  56. #define ACPI_DEVFLAG_SYSMGT1 0x10
  57. #define ACPI_DEVFLAG_SYSMGT2 0x20
  58. #define ACPI_DEVFLAG_LINT0 0x40
  59. #define ACPI_DEVFLAG_LINT1 0x80
  60. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  61. /*
  62. * ACPI table definitions
  63. *
  64. * These data structures are laid over the table to parse the important values
  65. * out of it.
  66. */
  67. /*
  68. * structure describing one IOMMU in the ACPI table. Typically followed by one
  69. * or more ivhd_entrys.
  70. */
  71. struct ivhd_header {
  72. u8 type;
  73. u8 flags;
  74. u16 length;
  75. u16 devid;
  76. u16 cap_ptr;
  77. u64 mmio_phys;
  78. u16 pci_seg;
  79. u16 info;
  80. u32 reserved;
  81. } __attribute__((packed));
  82. /*
  83. * A device entry describing which devices a specific IOMMU translates and
  84. * which requestor ids they use.
  85. */
  86. struct ivhd_entry {
  87. u8 type;
  88. u16 devid;
  89. u8 flags;
  90. u32 ext;
  91. } __attribute__((packed));
  92. /*
  93. * An AMD IOMMU memory definition structure. It defines things like exclusion
  94. * ranges for devices and regions that should be unity mapped.
  95. */
  96. struct ivmd_header {
  97. u8 type;
  98. u8 flags;
  99. u16 length;
  100. u16 devid;
  101. u16 aux;
  102. u64 resv;
  103. u64 range_start;
  104. u64 range_length;
  105. } __attribute__((packed));
  106. bool amd_iommu_dump;
  107. static int __initdata amd_iommu_detected;
  108. u16 amd_iommu_last_bdf; /* largest PCI device id we have
  109. to handle */
  110. LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
  111. we find in ACPI */
  112. unsigned amd_iommu_aperture_order = 26; /* size of aperture in power of 2 */
  113. bool amd_iommu_isolate = true; /* if true, device isolation is
  114. enabled */
  115. bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
  116. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  117. system */
  118. /*
  119. * Pointer to the device table which is shared by all AMD IOMMUs
  120. * it is indexed by the PCI device id or the HT unit id and contains
  121. * information about the domain the device belongs to as well as the
  122. * page table root pointer.
  123. */
  124. struct dev_table_entry *amd_iommu_dev_table;
  125. /*
  126. * The alias table is a driver specific data structure which contains the
  127. * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
  128. * More than one device can share the same requestor id.
  129. */
  130. u16 *amd_iommu_alias_table;
  131. /*
  132. * The rlookup table is used to find the IOMMU which is responsible
  133. * for a specific device. It is also indexed by the PCI device id.
  134. */
  135. struct amd_iommu **amd_iommu_rlookup_table;
  136. /*
  137. * The pd table (protection domain table) is used to find the protection domain
  138. * data structure a device belongs to. Indexed with the PCI device id too.
  139. */
  140. struct protection_domain **amd_iommu_pd_table;
  141. /*
  142. * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
  143. * to know which ones are already in use.
  144. */
  145. unsigned long *amd_iommu_pd_alloc_bitmap;
  146. static u32 dev_table_size; /* size of the device table */
  147. static u32 alias_table_size; /* size of the alias table */
  148. static u32 rlookup_table_size; /* size if the rlookup table */
  149. static inline void update_last_devid(u16 devid)
  150. {
  151. if (devid > amd_iommu_last_bdf)
  152. amd_iommu_last_bdf = devid;
  153. }
  154. static inline unsigned long tbl_size(int entry_size)
  155. {
  156. unsigned shift = PAGE_SHIFT +
  157. get_order(amd_iommu_last_bdf * entry_size);
  158. return 1UL << shift;
  159. }
  160. /****************************************************************************
  161. *
  162. * AMD IOMMU MMIO register space handling functions
  163. *
  164. * These functions are used to program the IOMMU device registers in
  165. * MMIO space required for that driver.
  166. *
  167. ****************************************************************************/
  168. /*
  169. * This function set the exclusion range in the IOMMU. DMA accesses to the
  170. * exclusion range are passed through untranslated
  171. */
  172. static void __init iommu_set_exclusion_range(struct amd_iommu *iommu)
  173. {
  174. u64 start = iommu->exclusion_start & PAGE_MASK;
  175. u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
  176. u64 entry;
  177. if (!iommu->exclusion_start)
  178. return;
  179. entry = start | MMIO_EXCL_ENABLE_MASK;
  180. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  181. &entry, sizeof(entry));
  182. entry = limit;
  183. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  184. &entry, sizeof(entry));
  185. }
  186. /* Programs the physical address of the device table into the IOMMU hardware */
  187. static void __init iommu_set_device_table(struct amd_iommu *iommu)
  188. {
  189. u64 entry;
  190. BUG_ON(iommu->mmio_base == NULL);
  191. entry = virt_to_phys(amd_iommu_dev_table);
  192. entry |= (dev_table_size >> 12) - 1;
  193. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  194. &entry, sizeof(entry));
  195. }
  196. /* Generic functions to enable/disable certain features of the IOMMU. */
  197. static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  198. {
  199. u32 ctrl;
  200. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  201. ctrl |= (1 << bit);
  202. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  203. }
  204. static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  205. {
  206. u32 ctrl;
  207. ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  208. ctrl &= ~(1 << bit);
  209. writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  210. }
  211. /* Function to enable the hardware */
  212. static void __init iommu_enable(struct amd_iommu *iommu)
  213. {
  214. printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at %s cap 0x%hx\n",
  215. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  216. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  217. }
  218. /* Function to enable IOMMU event logging and event interrupts */
  219. static void __init iommu_enable_event_logging(struct amd_iommu *iommu)
  220. {
  221. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  222. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  223. }
  224. /*
  225. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  226. * the system has one.
  227. */
  228. static u8 * __init iommu_map_mmio_space(u64 address)
  229. {
  230. u8 *ret;
  231. if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
  232. return NULL;
  233. ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
  234. if (ret != NULL)
  235. return ret;
  236. release_mem_region(address, MMIO_REGION_LENGTH);
  237. return NULL;
  238. }
  239. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  240. {
  241. if (iommu->mmio_base)
  242. iounmap(iommu->mmio_base);
  243. release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
  244. }
  245. /****************************************************************************
  246. *
  247. * The functions below belong to the first pass of AMD IOMMU ACPI table
  248. * parsing. In this pass we try to find out the highest device id this
  249. * code has to handle. Upon this information the size of the shared data
  250. * structures is determined later.
  251. *
  252. ****************************************************************************/
  253. /*
  254. * This function calculates the length of a given IVHD entry
  255. */
  256. static inline int ivhd_entry_length(u8 *ivhd)
  257. {
  258. return 0x04 << (*ivhd >> 6);
  259. }
  260. /*
  261. * This function reads the last device id the IOMMU has to handle from the PCI
  262. * capability header for this IOMMU
  263. */
  264. static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
  265. {
  266. u32 cap;
  267. cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
  268. update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
  269. return 0;
  270. }
  271. /*
  272. * After reading the highest device id from the IOMMU PCI capability header
  273. * this function looks if there is a higher device id defined in the ACPI table
  274. */
  275. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  276. {
  277. u8 *p = (void *)h, *end = (void *)h;
  278. struct ivhd_entry *dev;
  279. p += sizeof(*h);
  280. end += h->length;
  281. find_last_devid_on_pci(PCI_BUS(h->devid),
  282. PCI_SLOT(h->devid),
  283. PCI_FUNC(h->devid),
  284. h->cap_ptr);
  285. while (p < end) {
  286. dev = (struct ivhd_entry *)p;
  287. switch (dev->type) {
  288. case IVHD_DEV_SELECT:
  289. case IVHD_DEV_RANGE_END:
  290. case IVHD_DEV_ALIAS:
  291. case IVHD_DEV_EXT_SELECT:
  292. /* all the above subfield types refer to device ids */
  293. update_last_devid(dev->devid);
  294. break;
  295. default:
  296. break;
  297. }
  298. p += ivhd_entry_length(p);
  299. }
  300. WARN_ON(p != end);
  301. return 0;
  302. }
  303. /*
  304. * Iterate over all IVHD entries in the ACPI table and find the highest device
  305. * id which we need to handle. This is the first of three functions which parse
  306. * the ACPI table. So we check the checksum here.
  307. */
  308. static int __init find_last_devid_acpi(struct acpi_table_header *table)
  309. {
  310. int i;
  311. u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
  312. struct ivhd_header *h;
  313. /*
  314. * Validate checksum here so we don't need to do it when
  315. * we actually parse the table
  316. */
  317. for (i = 0; i < table->length; ++i)
  318. checksum += p[i];
  319. if (checksum != 0)
  320. /* ACPI table corrupt */
  321. return -ENODEV;
  322. p += IVRS_HEADER_LENGTH;
  323. end += table->length;
  324. while (p < end) {
  325. h = (struct ivhd_header *)p;
  326. switch (h->type) {
  327. case ACPI_IVHD_TYPE:
  328. find_last_devid_from_ivhd(h);
  329. break;
  330. default:
  331. break;
  332. }
  333. p += h->length;
  334. }
  335. WARN_ON(p != end);
  336. return 0;
  337. }
  338. /****************************************************************************
  339. *
  340. * The following functions belong the the code path which parses the ACPI table
  341. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  342. * data structures, initialize the device/alias/rlookup table and also
  343. * basically initialize the hardware.
  344. *
  345. ****************************************************************************/
  346. /*
  347. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  348. * write commands to that buffer later and the IOMMU will execute them
  349. * asynchronously
  350. */
  351. static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
  352. {
  353. u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  354. get_order(CMD_BUFFER_SIZE));
  355. u64 entry;
  356. if (cmd_buf == NULL)
  357. return NULL;
  358. iommu->cmd_buf_size = CMD_BUFFER_SIZE;
  359. entry = (u64)virt_to_phys(cmd_buf);
  360. entry |= MMIO_CMD_SIZE_512;
  361. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  362. &entry, sizeof(entry));
  363. /* set head and tail to zero manually */
  364. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  365. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  366. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  367. return cmd_buf;
  368. }
  369. static void __init free_command_buffer(struct amd_iommu *iommu)
  370. {
  371. free_pages((unsigned long)iommu->cmd_buf,
  372. get_order(iommu->cmd_buf_size));
  373. }
  374. /* allocates the memory where the IOMMU will log its events to */
  375. static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
  376. {
  377. u64 entry;
  378. iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  379. get_order(EVT_BUFFER_SIZE));
  380. if (iommu->evt_buf == NULL)
  381. return NULL;
  382. entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  383. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  384. &entry, sizeof(entry));
  385. iommu->evt_buf_size = EVT_BUFFER_SIZE;
  386. return iommu->evt_buf;
  387. }
  388. static void __init free_event_buffer(struct amd_iommu *iommu)
  389. {
  390. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  391. }
  392. /* sets a specific bit in the device table entry. */
  393. static void set_dev_entry_bit(u16 devid, u8 bit)
  394. {
  395. int i = (bit >> 5) & 0x07;
  396. int _bit = bit & 0x1f;
  397. amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
  398. }
  399. /* Writes the specific IOMMU for a device into the rlookup table */
  400. static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
  401. {
  402. amd_iommu_rlookup_table[devid] = iommu;
  403. }
  404. /*
  405. * This function takes the device specific flags read from the ACPI
  406. * table and sets up the device table entry with that information
  407. */
  408. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  409. u16 devid, u32 flags, u32 ext_flags)
  410. {
  411. if (flags & ACPI_DEVFLAG_INITPASS)
  412. set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
  413. if (flags & ACPI_DEVFLAG_EXTINT)
  414. set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
  415. if (flags & ACPI_DEVFLAG_NMI)
  416. set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
  417. if (flags & ACPI_DEVFLAG_SYSMGT1)
  418. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
  419. if (flags & ACPI_DEVFLAG_SYSMGT2)
  420. set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
  421. if (flags & ACPI_DEVFLAG_LINT0)
  422. set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
  423. if (flags & ACPI_DEVFLAG_LINT1)
  424. set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
  425. set_iommu_for_device(iommu, devid);
  426. }
  427. /*
  428. * Reads the device exclusion range from ACPI and initialize IOMMU with
  429. * it
  430. */
  431. static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
  432. {
  433. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  434. if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
  435. return;
  436. if (iommu) {
  437. /*
  438. * We only can configure exclusion ranges per IOMMU, not
  439. * per device. But we can enable the exclusion range per
  440. * device. This is done here
  441. */
  442. set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
  443. iommu->exclusion_start = m->range_start;
  444. iommu->exclusion_length = m->range_length;
  445. }
  446. }
  447. /*
  448. * This function reads some important data from the IOMMU PCI space and
  449. * initializes the driver data structure with it. It reads the hardware
  450. * capabilities and the first/last device entries
  451. */
  452. static void __init init_iommu_from_pci(struct amd_iommu *iommu)
  453. {
  454. int cap_ptr = iommu->cap_ptr;
  455. u32 range, misc;
  456. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  457. &iommu->cap);
  458. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  459. &range);
  460. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  461. &misc);
  462. iommu->first_device = calc_devid(MMIO_GET_BUS(range),
  463. MMIO_GET_FD(range));
  464. iommu->last_device = calc_devid(MMIO_GET_BUS(range),
  465. MMIO_GET_LD(range));
  466. iommu->evt_msi_num = MMIO_MSI_NUM(misc);
  467. }
  468. /*
  469. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  470. * initializes the hardware and our data structures with it.
  471. */
  472. static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
  473. struct ivhd_header *h)
  474. {
  475. u8 *p = (u8 *)h;
  476. u8 *end = p, flags = 0;
  477. u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
  478. u32 ext_flags = 0;
  479. bool alias = false;
  480. struct ivhd_entry *e;
  481. /*
  482. * First set the recommended feature enable bits from ACPI
  483. * into the IOMMU control registers
  484. */
  485. h->flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  486. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  487. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  488. h->flags & IVHD_FLAG_PASSPW_EN_MASK ?
  489. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  490. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  491. h->flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  492. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  493. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  494. h->flags & IVHD_FLAG_ISOC_EN_MASK ?
  495. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  496. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  497. /*
  498. * make IOMMU memory accesses cache coherent
  499. */
  500. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  501. /*
  502. * Done. Now parse the device entries
  503. */
  504. p += sizeof(struct ivhd_header);
  505. end += h->length;
  506. while (p < end) {
  507. e = (struct ivhd_entry *)p;
  508. switch (e->type) {
  509. case IVHD_DEV_ALL:
  510. DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
  511. " last device %02x:%02x.%x flags: %02x\n",
  512. PCI_BUS(iommu->first_device),
  513. PCI_SLOT(iommu->first_device),
  514. PCI_FUNC(iommu->first_device),
  515. PCI_BUS(iommu->last_device),
  516. PCI_SLOT(iommu->last_device),
  517. PCI_FUNC(iommu->last_device),
  518. e->flags);
  519. for (dev_i = iommu->first_device;
  520. dev_i <= iommu->last_device; ++dev_i)
  521. set_dev_entry_from_acpi(iommu, dev_i,
  522. e->flags, 0);
  523. break;
  524. case IVHD_DEV_SELECT:
  525. DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
  526. "flags: %02x\n",
  527. PCI_BUS(e->devid),
  528. PCI_SLOT(e->devid),
  529. PCI_FUNC(e->devid),
  530. e->flags);
  531. devid = e->devid;
  532. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  533. break;
  534. case IVHD_DEV_SELECT_RANGE_START:
  535. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  536. "devid: %02x:%02x.%x flags: %02x\n",
  537. PCI_BUS(e->devid),
  538. PCI_SLOT(e->devid),
  539. PCI_FUNC(e->devid),
  540. e->flags);
  541. devid_start = e->devid;
  542. flags = e->flags;
  543. ext_flags = 0;
  544. alias = false;
  545. break;
  546. case IVHD_DEV_ALIAS:
  547. DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
  548. "flags: %02x devid_to: %02x:%02x.%x\n",
  549. PCI_BUS(e->devid),
  550. PCI_SLOT(e->devid),
  551. PCI_FUNC(e->devid),
  552. e->flags,
  553. PCI_BUS(e->ext >> 8),
  554. PCI_SLOT(e->ext >> 8),
  555. PCI_FUNC(e->ext >> 8));
  556. devid = e->devid;
  557. devid_to = e->ext >> 8;
  558. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  559. amd_iommu_alias_table[devid] = devid_to;
  560. break;
  561. case IVHD_DEV_ALIAS_RANGE:
  562. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  563. "devid: %02x:%02x.%x flags: %02x "
  564. "devid_to: %02x:%02x.%x\n",
  565. PCI_BUS(e->devid),
  566. PCI_SLOT(e->devid),
  567. PCI_FUNC(e->devid),
  568. e->flags,
  569. PCI_BUS(e->ext >> 8),
  570. PCI_SLOT(e->ext >> 8),
  571. PCI_FUNC(e->ext >> 8));
  572. devid_start = e->devid;
  573. flags = e->flags;
  574. devid_to = e->ext >> 8;
  575. ext_flags = 0;
  576. alias = true;
  577. break;
  578. case IVHD_DEV_EXT_SELECT:
  579. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
  580. "flags: %02x ext: %08x\n",
  581. PCI_BUS(e->devid),
  582. PCI_SLOT(e->devid),
  583. PCI_FUNC(e->devid),
  584. e->flags, e->ext);
  585. devid = e->devid;
  586. set_dev_entry_from_acpi(iommu, devid, e->flags,
  587. e->ext);
  588. break;
  589. case IVHD_DEV_EXT_SELECT_RANGE:
  590. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  591. "%02x:%02x.%x flags: %02x ext: %08x\n",
  592. PCI_BUS(e->devid),
  593. PCI_SLOT(e->devid),
  594. PCI_FUNC(e->devid),
  595. e->flags, e->ext);
  596. devid_start = e->devid;
  597. flags = e->flags;
  598. ext_flags = e->ext;
  599. alias = false;
  600. break;
  601. case IVHD_DEV_RANGE_END:
  602. DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
  603. PCI_BUS(e->devid),
  604. PCI_SLOT(e->devid),
  605. PCI_FUNC(e->devid));
  606. devid = e->devid;
  607. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  608. if (alias)
  609. amd_iommu_alias_table[dev_i] = devid_to;
  610. set_dev_entry_from_acpi(iommu,
  611. amd_iommu_alias_table[dev_i],
  612. flags, ext_flags);
  613. }
  614. break;
  615. default:
  616. break;
  617. }
  618. p += ivhd_entry_length(p);
  619. }
  620. }
  621. /* Initializes the device->iommu mapping for the driver */
  622. static int __init init_iommu_devices(struct amd_iommu *iommu)
  623. {
  624. u16 i;
  625. for (i = iommu->first_device; i <= iommu->last_device; ++i)
  626. set_iommu_for_device(iommu, i);
  627. return 0;
  628. }
  629. static void __init free_iommu_one(struct amd_iommu *iommu)
  630. {
  631. free_command_buffer(iommu);
  632. free_event_buffer(iommu);
  633. iommu_unmap_mmio_space(iommu);
  634. }
  635. static void __init free_iommu_all(void)
  636. {
  637. struct amd_iommu *iommu, *next;
  638. list_for_each_entry_safe(iommu, next, &amd_iommu_list, list) {
  639. list_del(&iommu->list);
  640. free_iommu_one(iommu);
  641. kfree(iommu);
  642. }
  643. }
  644. /*
  645. * This function clues the initialization function for one IOMMU
  646. * together and also allocates the command buffer and programs the
  647. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  648. */
  649. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  650. {
  651. spin_lock_init(&iommu->lock);
  652. list_add_tail(&iommu->list, &amd_iommu_list);
  653. /*
  654. * Copy data from ACPI table entry to the iommu struct
  655. */
  656. iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
  657. if (!iommu->dev)
  658. return 1;
  659. iommu->cap_ptr = h->cap_ptr;
  660. iommu->pci_seg = h->pci_seg;
  661. iommu->mmio_phys = h->mmio_phys;
  662. iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
  663. if (!iommu->mmio_base)
  664. return -ENOMEM;
  665. iommu_set_device_table(iommu);
  666. iommu->cmd_buf = alloc_command_buffer(iommu);
  667. if (!iommu->cmd_buf)
  668. return -ENOMEM;
  669. iommu->evt_buf = alloc_event_buffer(iommu);
  670. if (!iommu->evt_buf)
  671. return -ENOMEM;
  672. iommu->int_enabled = false;
  673. init_iommu_from_pci(iommu);
  674. init_iommu_from_acpi(iommu, h);
  675. init_iommu_devices(iommu);
  676. return pci_enable_device(iommu->dev);
  677. }
  678. /*
  679. * Iterates over all IOMMU entries in the ACPI table, allocates the
  680. * IOMMU structure and initializes it with init_iommu_one()
  681. */
  682. static int __init init_iommu_all(struct acpi_table_header *table)
  683. {
  684. u8 *p = (u8 *)table, *end = (u8 *)table;
  685. struct ivhd_header *h;
  686. struct amd_iommu *iommu;
  687. int ret;
  688. end += table->length;
  689. p += IVRS_HEADER_LENGTH;
  690. while (p < end) {
  691. h = (struct ivhd_header *)p;
  692. switch (*p) {
  693. case ACPI_IVHD_TYPE:
  694. DUMP_printk("IOMMU: device: %02x:%02x.%01x cap: %04x "
  695. "seg: %d flags: %01x info %04x\n",
  696. PCI_BUS(h->devid), PCI_SLOT(h->devid),
  697. PCI_FUNC(h->devid), h->cap_ptr,
  698. h->pci_seg, h->flags, h->info);
  699. DUMP_printk(" mmio-addr: %016llx\n",
  700. h->mmio_phys);
  701. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  702. if (iommu == NULL)
  703. return -ENOMEM;
  704. ret = init_iommu_one(iommu, h);
  705. if (ret)
  706. return ret;
  707. break;
  708. default:
  709. break;
  710. }
  711. p += h->length;
  712. }
  713. WARN_ON(p != end);
  714. return 0;
  715. }
  716. /****************************************************************************
  717. *
  718. * The following functions initialize the MSI interrupts for all IOMMUs
  719. * in the system. Its a bit challenging because there could be multiple
  720. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  721. * pci_dev.
  722. *
  723. ****************************************************************************/
  724. static int __init iommu_setup_msix(struct amd_iommu *iommu)
  725. {
  726. struct amd_iommu *curr;
  727. struct msix_entry entries[32]; /* only 32 supported by AMD IOMMU */
  728. int nvec = 0, i;
  729. list_for_each_entry(curr, &amd_iommu_list, list) {
  730. if (curr->dev == iommu->dev) {
  731. entries[nvec].entry = curr->evt_msi_num;
  732. entries[nvec].vector = 0;
  733. curr->int_enabled = true;
  734. nvec++;
  735. }
  736. }
  737. if (pci_enable_msix(iommu->dev, entries, nvec)) {
  738. pci_disable_msix(iommu->dev);
  739. return 1;
  740. }
  741. for (i = 0; i < nvec; ++i) {
  742. int r = request_irq(entries->vector, amd_iommu_int_handler,
  743. IRQF_SAMPLE_RANDOM,
  744. "AMD IOMMU",
  745. NULL);
  746. if (r)
  747. goto out_free;
  748. }
  749. return 0;
  750. out_free:
  751. for (i -= 1; i >= 0; --i)
  752. free_irq(entries->vector, NULL);
  753. pci_disable_msix(iommu->dev);
  754. return 1;
  755. }
  756. static int __init iommu_setup_msi(struct amd_iommu *iommu)
  757. {
  758. int r;
  759. struct amd_iommu *curr;
  760. list_for_each_entry(curr, &amd_iommu_list, list) {
  761. if (curr->dev == iommu->dev)
  762. curr->int_enabled = true;
  763. }
  764. if (pci_enable_msi(iommu->dev))
  765. return 1;
  766. r = request_irq(iommu->dev->irq, amd_iommu_int_handler,
  767. IRQF_SAMPLE_RANDOM,
  768. "AMD IOMMU",
  769. NULL);
  770. if (r) {
  771. pci_disable_msi(iommu->dev);
  772. return 1;
  773. }
  774. return 0;
  775. }
  776. static int __init iommu_init_msi(struct amd_iommu *iommu)
  777. {
  778. if (iommu->int_enabled)
  779. return 0;
  780. if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSIX))
  781. return iommu_setup_msix(iommu);
  782. else if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
  783. return iommu_setup_msi(iommu);
  784. return 1;
  785. }
  786. /****************************************************************************
  787. *
  788. * The next functions belong to the third pass of parsing the ACPI
  789. * table. In this last pass the memory mapping requirements are
  790. * gathered (like exclusion and unity mapping reanges).
  791. *
  792. ****************************************************************************/
  793. static void __init free_unity_maps(void)
  794. {
  795. struct unity_map_entry *entry, *next;
  796. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  797. list_del(&entry->list);
  798. kfree(entry);
  799. }
  800. }
  801. /* called when we find an exclusion range definition in ACPI */
  802. static int __init init_exclusion_range(struct ivmd_header *m)
  803. {
  804. int i;
  805. switch (m->type) {
  806. case ACPI_IVMD_TYPE:
  807. set_device_exclusion_range(m->devid, m);
  808. break;
  809. case ACPI_IVMD_TYPE_ALL:
  810. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  811. set_device_exclusion_range(i, m);
  812. break;
  813. case ACPI_IVMD_TYPE_RANGE:
  814. for (i = m->devid; i <= m->aux; ++i)
  815. set_device_exclusion_range(i, m);
  816. break;
  817. default:
  818. break;
  819. }
  820. return 0;
  821. }
  822. /* called for unity map ACPI definition */
  823. static int __init init_unity_map_range(struct ivmd_header *m)
  824. {
  825. struct unity_map_entry *e = 0;
  826. char *s;
  827. e = kzalloc(sizeof(*e), GFP_KERNEL);
  828. if (e == NULL)
  829. return -ENOMEM;
  830. switch (m->type) {
  831. default:
  832. case ACPI_IVMD_TYPE:
  833. s = "IVMD_TYPEi\t\t\t";
  834. e->devid_start = e->devid_end = m->devid;
  835. break;
  836. case ACPI_IVMD_TYPE_ALL:
  837. s = "IVMD_TYPE_ALL\t\t";
  838. e->devid_start = 0;
  839. e->devid_end = amd_iommu_last_bdf;
  840. break;
  841. case ACPI_IVMD_TYPE_RANGE:
  842. s = "IVMD_TYPE_RANGE\t\t";
  843. e->devid_start = m->devid;
  844. e->devid_end = m->aux;
  845. break;
  846. }
  847. e->address_start = PAGE_ALIGN(m->range_start);
  848. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  849. e->prot = m->flags >> 1;
  850. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  851. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  852. PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
  853. PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
  854. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  855. e->address_start, e->address_end, m->flags);
  856. list_add_tail(&e->list, &amd_iommu_unity_map);
  857. return 0;
  858. }
  859. /* iterates over all memory definitions we find in the ACPI table */
  860. static int __init init_memory_definitions(struct acpi_table_header *table)
  861. {
  862. u8 *p = (u8 *)table, *end = (u8 *)table;
  863. struct ivmd_header *m;
  864. end += table->length;
  865. p += IVRS_HEADER_LENGTH;
  866. while (p < end) {
  867. m = (struct ivmd_header *)p;
  868. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  869. init_exclusion_range(m);
  870. else if (m->flags & IVMD_FLAG_UNITY_MAP)
  871. init_unity_map_range(m);
  872. p += m->length;
  873. }
  874. return 0;
  875. }
  876. /*
  877. * Init the device table to not allow DMA access for devices and
  878. * suppress all page faults
  879. */
  880. static void init_device_table(void)
  881. {
  882. u16 devid;
  883. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  884. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  885. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  886. }
  887. }
  888. /*
  889. * This function finally enables all IOMMUs found in the system after
  890. * they have been initialized
  891. */
  892. static void __init enable_iommus(void)
  893. {
  894. struct amd_iommu *iommu;
  895. list_for_each_entry(iommu, &amd_iommu_list, list) {
  896. iommu_set_exclusion_range(iommu);
  897. iommu_init_msi(iommu);
  898. iommu_enable_event_logging(iommu);
  899. iommu_enable(iommu);
  900. }
  901. }
  902. /*
  903. * Suspend/Resume support
  904. * disable suspend until real resume implemented
  905. */
  906. static int amd_iommu_resume(struct sys_device *dev)
  907. {
  908. return 0;
  909. }
  910. static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
  911. {
  912. return -EINVAL;
  913. }
  914. static struct sysdev_class amd_iommu_sysdev_class = {
  915. .name = "amd_iommu",
  916. .suspend = amd_iommu_suspend,
  917. .resume = amd_iommu_resume,
  918. };
  919. static struct sys_device device_amd_iommu = {
  920. .id = 0,
  921. .cls = &amd_iommu_sysdev_class,
  922. };
  923. /*
  924. * This is the core init function for AMD IOMMU hardware in the system.
  925. * This function is called from the generic x86 DMA layer initialization
  926. * code.
  927. *
  928. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  929. * three times:
  930. *
  931. * 1 pass) Find the highest PCI device id the driver has to handle.
  932. * Upon this information the size of the data structures is
  933. * determined that needs to be allocated.
  934. *
  935. * 2 pass) Initialize the data structures just allocated with the
  936. * information in the ACPI table about available AMD IOMMUs
  937. * in the system. It also maps the PCI devices in the
  938. * system to specific IOMMUs
  939. *
  940. * 3 pass) After the basic data structures are allocated and
  941. * initialized we update them with information about memory
  942. * remapping requirements parsed out of the ACPI table in
  943. * this last pass.
  944. *
  945. * After that the hardware is initialized and ready to go. In the last
  946. * step we do some Linux specific things like registering the driver in
  947. * the dma_ops interface and initializing the suspend/resume support
  948. * functions. Finally it prints some information about AMD IOMMUs and
  949. * the driver state and enables the hardware.
  950. */
  951. int __init amd_iommu_init(void)
  952. {
  953. int i, ret = 0;
  954. if (no_iommu) {
  955. printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
  956. return 0;
  957. }
  958. if (!amd_iommu_detected)
  959. return -ENODEV;
  960. /*
  961. * First parse ACPI tables to find the largest Bus/Dev/Func
  962. * we need to handle. Upon this information the shared data
  963. * structures for the IOMMUs in the system will be allocated
  964. */
  965. if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
  966. return -ENODEV;
  967. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  968. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  969. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  970. ret = -ENOMEM;
  971. /* Device table - directly used by all IOMMUs */
  972. amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  973. get_order(dev_table_size));
  974. if (amd_iommu_dev_table == NULL)
  975. goto out;
  976. /*
  977. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  978. * IOMMU see for that device
  979. */
  980. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  981. get_order(alias_table_size));
  982. if (amd_iommu_alias_table == NULL)
  983. goto free;
  984. /* IOMMU rlookup table - find the IOMMU for a specific device */
  985. amd_iommu_rlookup_table = (void *)__get_free_pages(
  986. GFP_KERNEL | __GFP_ZERO,
  987. get_order(rlookup_table_size));
  988. if (amd_iommu_rlookup_table == NULL)
  989. goto free;
  990. /*
  991. * Protection Domain table - maps devices to protection domains
  992. * This table has the same size as the rlookup_table
  993. */
  994. amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  995. get_order(rlookup_table_size));
  996. if (amd_iommu_pd_table == NULL)
  997. goto free;
  998. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  999. GFP_KERNEL | __GFP_ZERO,
  1000. get_order(MAX_DOMAIN_ID/8));
  1001. if (amd_iommu_pd_alloc_bitmap == NULL)
  1002. goto free;
  1003. /* init the device table */
  1004. init_device_table();
  1005. /*
  1006. * let all alias entries point to itself
  1007. */
  1008. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1009. amd_iommu_alias_table[i] = i;
  1010. /*
  1011. * never allocate domain 0 because its used as the non-allocated and
  1012. * error value placeholder
  1013. */
  1014. amd_iommu_pd_alloc_bitmap[0] = 1;
  1015. /*
  1016. * now the data structures are allocated and basically initialized
  1017. * start the real acpi table scan
  1018. */
  1019. ret = -ENODEV;
  1020. if (acpi_table_parse("IVRS", init_iommu_all) != 0)
  1021. goto free;
  1022. if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
  1023. goto free;
  1024. ret = sysdev_class_register(&amd_iommu_sysdev_class);
  1025. if (ret)
  1026. goto free;
  1027. ret = sysdev_register(&device_amd_iommu);
  1028. if (ret)
  1029. goto free;
  1030. ret = amd_iommu_init_dma_ops();
  1031. if (ret)
  1032. goto free;
  1033. enable_iommus();
  1034. printk(KERN_INFO "AMD IOMMU: aperture size is %d MB\n",
  1035. (1 << (amd_iommu_aperture_order-20)));
  1036. printk(KERN_INFO "AMD IOMMU: device isolation ");
  1037. if (amd_iommu_isolate)
  1038. printk("enabled\n");
  1039. else
  1040. printk("disabled\n");
  1041. if (amd_iommu_unmap_flush)
  1042. printk(KERN_INFO "AMD IOMMU: IO/TLB flush on unmap enabled\n");
  1043. else
  1044. printk(KERN_INFO "AMD IOMMU: Lazy IO/TLB flushing enabled\n");
  1045. out:
  1046. return ret;
  1047. free:
  1048. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1049. get_order(MAX_DOMAIN_ID/8));
  1050. free_pages((unsigned long)amd_iommu_pd_table,
  1051. get_order(rlookup_table_size));
  1052. free_pages((unsigned long)amd_iommu_rlookup_table,
  1053. get_order(rlookup_table_size));
  1054. free_pages((unsigned long)amd_iommu_alias_table,
  1055. get_order(alias_table_size));
  1056. free_pages((unsigned long)amd_iommu_dev_table,
  1057. get_order(dev_table_size));
  1058. free_iommu_all();
  1059. free_unity_maps();
  1060. goto out;
  1061. }
  1062. /****************************************************************************
  1063. *
  1064. * Early detect code. This code runs at IOMMU detection time in the DMA
  1065. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  1066. * IOMMUs
  1067. *
  1068. ****************************************************************************/
  1069. static int __init early_amd_iommu_detect(struct acpi_table_header *table)
  1070. {
  1071. return 0;
  1072. }
  1073. void __init amd_iommu_detect(void)
  1074. {
  1075. if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture))
  1076. return;
  1077. if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
  1078. iommu_detected = 1;
  1079. amd_iommu_detected = 1;
  1080. #ifdef CONFIG_GART_IOMMU
  1081. gart_iommu_aperture_disabled = 1;
  1082. gart_iommu_aperture = 0;
  1083. #endif
  1084. }
  1085. }
  1086. /****************************************************************************
  1087. *
  1088. * Parsing functions for the AMD IOMMU specific kernel command line
  1089. * options.
  1090. *
  1091. ****************************************************************************/
  1092. static int __init parse_amd_iommu_dump(char *str)
  1093. {
  1094. amd_iommu_dump = true;
  1095. return 1;
  1096. }
  1097. static int __init parse_amd_iommu_options(char *str)
  1098. {
  1099. for (; *str; ++str) {
  1100. if (strncmp(str, "isolate", 7) == 0)
  1101. amd_iommu_isolate = true;
  1102. if (strncmp(str, "share", 5) == 0)
  1103. amd_iommu_isolate = false;
  1104. if (strncmp(str, "fullflush", 9) == 0)
  1105. amd_iommu_unmap_flush = true;
  1106. }
  1107. return 1;
  1108. }
  1109. static int __init parse_amd_iommu_size_options(char *str)
  1110. {
  1111. unsigned order = PAGE_SHIFT + get_order(memparse(str, &str));
  1112. if ((order > 24) && (order < 31))
  1113. amd_iommu_aperture_order = order;
  1114. return 1;
  1115. }
  1116. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  1117. __setup("amd_iommu=", parse_amd_iommu_options);
  1118. __setup("amd_iommu_size=", parse_amd_iommu_size_options);