amd_iommu_init.c 39 KB

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