amd_iommu_init.c 34 KB

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