amd_iommu_init.c 63 KB

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