amd_iommu_init.c 75 KB

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