amd_iommu_init.c 39 KB

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