amd_iommu_init.c 45 KB

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