amd_iommu_init.c 42 KB

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