amd_iommu_init.c 42 KB

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