amd_iommu_init.c 53 KB

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