amd_iommu_init.c 44 KB

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