amd_iommu_init.c 36 KB

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