amd_iommu_init.c 75 KB

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