amd_iommu_init.c 35 KB

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