amd_iommu_init.c 35 KB

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