amd_iommu_init.c 56 KB

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