amd_iommu_init.c 35 KB

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