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];
  1105. u8 uid[ACPIHID_UID_LEN];
  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. uid[0] = '\0';
  1119. switch (e->uidf) {
  1120. case UID_NOT_PRESENT:
  1121. if (e->uidl != 0)
  1122. pr_warn(FW_BUG "Invalid UID length.\n");
  1123. break;
  1124. case UID_IS_INTEGER:
  1125. sprintf(uid, "%d", e->uid);
  1126. break;
  1127. case UID_IS_CHARACTER:
  1128. memcpy(uid, &e->uid, e->uidl);
  1129. uid[e->uidl] = '\0';
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. devid = e->devid;
  1135. DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
  1136. hid, uid,
  1137. PCI_BUS_NUM(devid),
  1138. PCI_SLOT(devid),
  1139. PCI_FUNC(devid));
  1140. flags = e->flags;
  1141. ret = add_acpi_hid_device(hid, uid, &devid, false);
  1142. if (ret)
  1143. return ret;
  1144. /*
  1145. * add_special_device might update the devid in case a
  1146. * command-line override is present. So call
  1147. * set_dev_entry_from_acpi after add_special_device.
  1148. */
  1149. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1150. break;
  1151. }
  1152. default:
  1153. break;
  1154. }
  1155. p += ivhd_entry_length(p);
  1156. }
  1157. return 0;
  1158. }
  1159. static void __init free_iommu_one(struct amd_iommu *iommu)
  1160. {
  1161. free_command_buffer(iommu);
  1162. free_event_buffer(iommu);
  1163. free_ppr_log(iommu);
  1164. free_ga_log(iommu);
  1165. iommu_unmap_mmio_space(iommu);
  1166. }
  1167. static void __init free_iommu_all(void)
  1168. {
  1169. struct amd_iommu *iommu, *next;
  1170. for_each_iommu_safe(iommu, next) {
  1171. list_del(&iommu->list);
  1172. free_iommu_one(iommu);
  1173. kfree(iommu);
  1174. }
  1175. }
  1176. /*
  1177. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  1178. * Workaround:
  1179. * BIOS should disable L2B micellaneous clock gating by setting
  1180. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  1181. */
  1182. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  1183. {
  1184. u32 value;
  1185. if ((boot_cpu_data.x86 != 0x15) ||
  1186. (boot_cpu_data.x86_model < 0x10) ||
  1187. (boot_cpu_data.x86_model > 0x1f))
  1188. return;
  1189. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1190. pci_read_config_dword(iommu->dev, 0xf4, &value);
  1191. if (value & BIT(2))
  1192. return;
  1193. /* Select NB indirect register 0x90 and enable writing */
  1194. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  1195. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  1196. pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
  1197. dev_name(&iommu->dev->dev));
  1198. /* Clear the enable writing bit */
  1199. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1200. }
  1201. /*
  1202. * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
  1203. * Workaround:
  1204. * BIOS should enable ATS write permission check by setting
  1205. * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
  1206. */
  1207. static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
  1208. {
  1209. u32 value;
  1210. if ((boot_cpu_data.x86 != 0x15) ||
  1211. (boot_cpu_data.x86_model < 0x30) ||
  1212. (boot_cpu_data.x86_model > 0x3f))
  1213. return;
  1214. /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
  1215. value = iommu_read_l2(iommu, 0x47);
  1216. if (value & BIT(0))
  1217. return;
  1218. /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
  1219. iommu_write_l2(iommu, 0x47, value | BIT(0));
  1220. pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
  1221. dev_name(&iommu->dev->dev));
  1222. }
  1223. /*
  1224. * This function clues the initialization function for one IOMMU
  1225. * together and also allocates the command buffer and programs the
  1226. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  1227. */
  1228. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
  1229. {
  1230. int ret;
  1231. raw_spin_lock_init(&iommu->lock);
  1232. /* Add IOMMU to internal data structures */
  1233. list_add_tail(&iommu->list, &amd_iommu_list);
  1234. iommu->index = amd_iommus_present++;
  1235. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  1236. WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
  1237. return -ENOSYS;
  1238. }
  1239. /* Index is fine - add IOMMU to the array */
  1240. amd_iommus[iommu->index] = iommu;
  1241. /*
  1242. * Copy data from ACPI table entry to the iommu struct
  1243. */
  1244. iommu->devid = h->devid;
  1245. iommu->cap_ptr = h->cap_ptr;
  1246. iommu->pci_seg = h->pci_seg;
  1247. iommu->mmio_phys = h->mmio_phys;
  1248. switch (h->type) {
  1249. case 0x10:
  1250. /* Check if IVHD EFR contains proper max banks/counters */
  1251. if ((h->efr_attr != 0) &&
  1252. ((h->efr_attr & (0xF << 13)) != 0) &&
  1253. ((h->efr_attr & (0x3F << 17)) != 0))
  1254. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1255. else
  1256. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1257. if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
  1258. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1259. if (((h->efr_attr & (0x1 << IOMMU_FEAT_XTSUP_SHIFT)) == 0))
  1260. amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  1261. break;
  1262. case 0x11:
  1263. case 0x40:
  1264. if (h->efr_reg & (1 << 9))
  1265. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1266. else
  1267. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1268. if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
  1269. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1270. if (((h->efr_reg & (0x1 << IOMMU_EFR_XTSUP_SHIFT)) == 0))
  1271. amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  1272. break;
  1273. default:
  1274. return -EINVAL;
  1275. }
  1276. iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
  1277. iommu->mmio_phys_end);
  1278. if (!iommu->mmio_base)
  1279. return -ENOMEM;
  1280. if (alloc_command_buffer(iommu))
  1281. return -ENOMEM;
  1282. if (alloc_event_buffer(iommu))
  1283. return -ENOMEM;
  1284. iommu->int_enabled = false;
  1285. init_translation_status(iommu);
  1286. if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
  1287. iommu_disable(iommu);
  1288. clear_translation_pre_enabled(iommu);
  1289. pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
  1290. iommu->index);
  1291. }
  1292. if (amd_iommu_pre_enabled)
  1293. amd_iommu_pre_enabled = translation_pre_enabled(iommu);
  1294. ret = init_iommu_from_acpi(iommu, h);
  1295. if (ret)
  1296. return ret;
  1297. ret = amd_iommu_create_irq_domain(iommu);
  1298. if (ret)
  1299. return ret;
  1300. /*
  1301. * Make sure IOMMU is not considered to translate itself. The IVRS
  1302. * table tells us so, but this is a lie!
  1303. */
  1304. amd_iommu_rlookup_table[iommu->devid] = NULL;
  1305. return 0;
  1306. }
  1307. /**
  1308. * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
  1309. * @ivrs Pointer to the IVRS header
  1310. *
  1311. * This function search through all IVDB of the maximum supported IVHD
  1312. */
  1313. static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
  1314. {
  1315. u8 *base = (u8 *)ivrs;
  1316. struct ivhd_header *ivhd = (struct ivhd_header *)
  1317. (base + IVRS_HEADER_LENGTH);
  1318. u8 last_type = ivhd->type;
  1319. u16 devid = ivhd->devid;
  1320. while (((u8 *)ivhd - base < ivrs->length) &&
  1321. (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
  1322. u8 *p = (u8 *) ivhd;
  1323. if (ivhd->devid == devid)
  1324. last_type = ivhd->type;
  1325. ivhd = (struct ivhd_header *)(p + ivhd->length);
  1326. }
  1327. return last_type;
  1328. }
  1329. /*
  1330. * Iterates over all IOMMU entries in the ACPI table, allocates the
  1331. * IOMMU structure and initializes it with init_iommu_one()
  1332. */
  1333. static int __init init_iommu_all(struct acpi_table_header *table)
  1334. {
  1335. u8 *p = (u8 *)table, *end = (u8 *)table;
  1336. struct ivhd_header *h;
  1337. struct amd_iommu *iommu;
  1338. int ret;
  1339. end += table->length;
  1340. p += IVRS_HEADER_LENGTH;
  1341. while (p < end) {
  1342. h = (struct ivhd_header *)p;
  1343. if (*p == amd_iommu_target_ivhd_type) {
  1344. DUMP_printk("device: %02x:%02x.%01x cap: %04x "
  1345. "seg: %d flags: %01x info %04x\n",
  1346. PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
  1347. PCI_FUNC(h->devid), h->cap_ptr,
  1348. h->pci_seg, h->flags, h->info);
  1349. DUMP_printk(" mmio-addr: %016llx\n",
  1350. h->mmio_phys);
  1351. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  1352. if (iommu == NULL)
  1353. return -ENOMEM;
  1354. ret = init_iommu_one(iommu, h);
  1355. if (ret)
  1356. return ret;
  1357. }
  1358. p += h->length;
  1359. }
  1360. WARN_ON(p != end);
  1361. return 0;
  1362. }
  1363. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  1364. u8 fxn, u64 *value, bool is_write);
  1365. static void init_iommu_perf_ctr(struct amd_iommu *iommu)
  1366. {
  1367. u64 val = 0xabcd, val2 = 0;
  1368. if (!iommu_feature(iommu, FEATURE_PC))
  1369. return;
  1370. amd_iommu_pc_present = true;
  1371. /* Check if the performance counters can be written to */
  1372. if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
  1373. (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
  1374. (val != val2)) {
  1375. pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
  1376. amd_iommu_pc_present = false;
  1377. return;
  1378. }
  1379. pr_info("AMD-Vi: IOMMU performance counters supported\n");
  1380. val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
  1381. iommu->max_banks = (u8) ((val >> 12) & 0x3f);
  1382. iommu->max_counters = (u8) ((val >> 7) & 0xf);
  1383. }
  1384. static ssize_t amd_iommu_show_cap(struct device *dev,
  1385. struct device_attribute *attr,
  1386. char *buf)
  1387. {
  1388. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1389. return sprintf(buf, "%x\n", iommu->cap);
  1390. }
  1391. static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
  1392. static ssize_t amd_iommu_show_features(struct device *dev,
  1393. struct device_attribute *attr,
  1394. char *buf)
  1395. {
  1396. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1397. return sprintf(buf, "%llx\n", iommu->features);
  1398. }
  1399. static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
  1400. static struct attribute *amd_iommu_attrs[] = {
  1401. &dev_attr_cap.attr,
  1402. &dev_attr_features.attr,
  1403. NULL,
  1404. };
  1405. static struct attribute_group amd_iommu_group = {
  1406. .name = "amd-iommu",
  1407. .attrs = amd_iommu_attrs,
  1408. };
  1409. static const struct attribute_group *amd_iommu_groups[] = {
  1410. &amd_iommu_group,
  1411. NULL,
  1412. };
  1413. static int __init iommu_init_pci(struct amd_iommu *iommu)
  1414. {
  1415. int cap_ptr = iommu->cap_ptr;
  1416. u32 range, misc, low, high;
  1417. int ret;
  1418. iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
  1419. iommu->devid & 0xff);
  1420. if (!iommu->dev)
  1421. return -ENODEV;
  1422. /* Prevent binding other PCI device drivers to IOMMU devices */
  1423. iommu->dev->match_driver = false;
  1424. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  1425. &iommu->cap);
  1426. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
  1427. &range);
  1428. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
  1429. &misc);
  1430. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  1431. amd_iommu_iotlb_sup = false;
  1432. /* read extended feature bits */
  1433. low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
  1434. high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
  1435. iommu->features = ((u64)high << 32) | low;
  1436. if (iommu_feature(iommu, FEATURE_GT)) {
  1437. int glxval;
  1438. u32 max_pasid;
  1439. u64 pasmax;
  1440. pasmax = iommu->features & FEATURE_PASID_MASK;
  1441. pasmax >>= FEATURE_PASID_SHIFT;
  1442. max_pasid = (1 << (pasmax + 1)) - 1;
  1443. amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
  1444. BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
  1445. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  1446. glxval >>= FEATURE_GLXVAL_SHIFT;
  1447. if (amd_iommu_max_glx_val == -1)
  1448. amd_iommu_max_glx_val = glxval;
  1449. else
  1450. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  1451. }
  1452. if (iommu_feature(iommu, FEATURE_GT) &&
  1453. iommu_feature(iommu, FEATURE_PPR)) {
  1454. iommu->is_iommu_v2 = true;
  1455. amd_iommu_v2_present = true;
  1456. }
  1457. if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
  1458. return -ENOMEM;
  1459. ret = iommu_init_ga(iommu);
  1460. if (ret)
  1461. return ret;
  1462. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
  1463. amd_iommu_np_cache = true;
  1464. init_iommu_perf_ctr(iommu);
  1465. if (is_rd890_iommu(iommu->dev)) {
  1466. int i, j;
  1467. iommu->root_pdev =
  1468. pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
  1469. PCI_DEVFN(0, 0));
  1470. /*
  1471. * Some rd890 systems may not be fully reconfigured by the
  1472. * BIOS, so it's necessary for us to store this information so
  1473. * it can be reprogrammed on resume
  1474. */
  1475. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1476. &iommu->stored_addr_lo);
  1477. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1478. &iommu->stored_addr_hi);
  1479. /* Low bit locks writes to configuration space */
  1480. iommu->stored_addr_lo &= ~1;
  1481. for (i = 0; i < 6; i++)
  1482. for (j = 0; j < 0x12; j++)
  1483. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  1484. for (i = 0; i < 0x83; i++)
  1485. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  1486. }
  1487. amd_iommu_erratum_746_workaround(iommu);
  1488. amd_iommu_ats_write_check_workaround(iommu);
  1489. iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
  1490. amd_iommu_groups, "ivhd%d", iommu->index);
  1491. iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
  1492. iommu_device_register(&iommu->iommu);
  1493. return pci_enable_device(iommu->dev);
  1494. }
  1495. static void print_iommu_info(void)
  1496. {
  1497. static const char * const feat_str[] = {
  1498. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  1499. "IA", "GA", "HE", "PC"
  1500. };
  1501. struct amd_iommu *iommu;
  1502. for_each_iommu(iommu) {
  1503. int i;
  1504. pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
  1505. dev_name(&iommu->dev->dev), iommu->cap_ptr);
  1506. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  1507. pr_info("AMD-Vi: Extended features (%#llx):\n",
  1508. iommu->features);
  1509. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1510. if (iommu_feature(iommu, (1ULL << i)))
  1511. pr_cont(" %s", feat_str[i]);
  1512. }
  1513. if (iommu->features & FEATURE_GAM_VAPIC)
  1514. pr_cont(" GA_vAPIC");
  1515. pr_cont("\n");
  1516. }
  1517. }
  1518. if (irq_remapping_enabled) {
  1519. pr_info("AMD-Vi: Interrupt remapping enabled\n");
  1520. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1521. pr_info("AMD-Vi: virtual APIC enabled\n");
  1522. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1523. pr_info("AMD-Vi: X2APIC enabled\n");
  1524. }
  1525. }
  1526. static int __init amd_iommu_init_pci(void)
  1527. {
  1528. struct amd_iommu *iommu;
  1529. int ret = 0;
  1530. for_each_iommu(iommu) {
  1531. ret = iommu_init_pci(iommu);
  1532. if (ret)
  1533. break;
  1534. }
  1535. /*
  1536. * Order is important here to make sure any unity map requirements are
  1537. * fulfilled. The unity mappings are created and written to the device
  1538. * table during the amd_iommu_init_api() call.
  1539. *
  1540. * After that we call init_device_table_dma() to make sure any
  1541. * uninitialized DTE will block DMA, and in the end we flush the caches
  1542. * of all IOMMUs to make sure the changes to the device table are
  1543. * active.
  1544. */
  1545. ret = amd_iommu_init_api();
  1546. init_device_table_dma();
  1547. for_each_iommu(iommu)
  1548. iommu_flush_all_caches(iommu);
  1549. if (!ret)
  1550. print_iommu_info();
  1551. return ret;
  1552. }
  1553. /****************************************************************************
  1554. *
  1555. * The following functions initialize the MSI interrupts for all IOMMUs
  1556. * in the system. It's a bit challenging because there could be multiple
  1557. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1558. * pci_dev.
  1559. *
  1560. ****************************************************************************/
  1561. static int iommu_setup_msi(struct amd_iommu *iommu)
  1562. {
  1563. int r;
  1564. r = pci_enable_msi(iommu->dev);
  1565. if (r)
  1566. return r;
  1567. r = request_threaded_irq(iommu->dev->irq,
  1568. amd_iommu_int_handler,
  1569. amd_iommu_int_thread,
  1570. 0, "AMD-Vi",
  1571. iommu);
  1572. if (r) {
  1573. pci_disable_msi(iommu->dev);
  1574. return r;
  1575. }
  1576. iommu->int_enabled = true;
  1577. return 0;
  1578. }
  1579. static int iommu_init_msi(struct amd_iommu *iommu)
  1580. {
  1581. int ret;
  1582. if (iommu->int_enabled)
  1583. goto enable_faults;
  1584. if (iommu->dev->msi_cap)
  1585. ret = iommu_setup_msi(iommu);
  1586. else
  1587. ret = -ENODEV;
  1588. if (ret)
  1589. return ret;
  1590. enable_faults:
  1591. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  1592. if (iommu->ppr_log != NULL)
  1593. iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
  1594. iommu_ga_log_enable(iommu);
  1595. return 0;
  1596. }
  1597. /****************************************************************************
  1598. *
  1599. * The next functions belong to the third pass of parsing the ACPI
  1600. * table. In this last pass the memory mapping requirements are
  1601. * gathered (like exclusion and unity mapping ranges).
  1602. *
  1603. ****************************************************************************/
  1604. static void __init free_unity_maps(void)
  1605. {
  1606. struct unity_map_entry *entry, *next;
  1607. list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
  1608. list_del(&entry->list);
  1609. kfree(entry);
  1610. }
  1611. }
  1612. /* called when we find an exclusion range definition in ACPI */
  1613. static int __init init_exclusion_range(struct ivmd_header *m)
  1614. {
  1615. int i;
  1616. switch (m->type) {
  1617. case ACPI_IVMD_TYPE:
  1618. set_device_exclusion_range(m->devid, m);
  1619. break;
  1620. case ACPI_IVMD_TYPE_ALL:
  1621. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  1622. set_device_exclusion_range(i, m);
  1623. break;
  1624. case ACPI_IVMD_TYPE_RANGE:
  1625. for (i = m->devid; i <= m->aux; ++i)
  1626. set_device_exclusion_range(i, m);
  1627. break;
  1628. default:
  1629. break;
  1630. }
  1631. return 0;
  1632. }
  1633. /* called for unity map ACPI definition */
  1634. static int __init init_unity_map_range(struct ivmd_header *m)
  1635. {
  1636. struct unity_map_entry *e = NULL;
  1637. char *s;
  1638. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1639. if (e == NULL)
  1640. return -ENOMEM;
  1641. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  1642. init_exclusion_range(m);
  1643. switch (m->type) {
  1644. default:
  1645. kfree(e);
  1646. return 0;
  1647. case ACPI_IVMD_TYPE:
  1648. s = "IVMD_TYPEi\t\t\t";
  1649. e->devid_start = e->devid_end = m->devid;
  1650. break;
  1651. case ACPI_IVMD_TYPE_ALL:
  1652. s = "IVMD_TYPE_ALL\t\t";
  1653. e->devid_start = 0;
  1654. e->devid_end = amd_iommu_last_bdf;
  1655. break;
  1656. case ACPI_IVMD_TYPE_RANGE:
  1657. s = "IVMD_TYPE_RANGE\t\t";
  1658. e->devid_start = m->devid;
  1659. e->devid_end = m->aux;
  1660. break;
  1661. }
  1662. e->address_start = PAGE_ALIGN(m->range_start);
  1663. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  1664. e->prot = m->flags >> 1;
  1665. DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
  1666. " range_start: %016llx range_end: %016llx flags: %x\n", s,
  1667. PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
  1668. PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
  1669. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  1670. e->address_start, e->address_end, m->flags);
  1671. list_add_tail(&e->list, &amd_iommu_unity_map);
  1672. return 0;
  1673. }
  1674. /* iterates over all memory definitions we find in the ACPI table */
  1675. static int __init init_memory_definitions(struct acpi_table_header *table)
  1676. {
  1677. u8 *p = (u8 *)table, *end = (u8 *)table;
  1678. struct ivmd_header *m;
  1679. end += table->length;
  1680. p += IVRS_HEADER_LENGTH;
  1681. while (p < end) {
  1682. m = (struct ivmd_header *)p;
  1683. if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
  1684. init_unity_map_range(m);
  1685. p += m->length;
  1686. }
  1687. return 0;
  1688. }
  1689. /*
  1690. * Init the device table to not allow DMA access for devices
  1691. */
  1692. static void init_device_table_dma(void)
  1693. {
  1694. u32 devid;
  1695. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1696. set_dev_entry_bit(devid, DEV_ENTRY_VALID);
  1697. set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
  1698. }
  1699. }
  1700. static void __init uninit_device_table_dma(void)
  1701. {
  1702. u32 devid;
  1703. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
  1704. amd_iommu_dev_table[devid].data[0] = 0ULL;
  1705. amd_iommu_dev_table[devid].data[1] = 0ULL;
  1706. }
  1707. }
  1708. static void init_device_table(void)
  1709. {
  1710. u32 devid;
  1711. if (!amd_iommu_irq_remap)
  1712. return;
  1713. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1714. set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
  1715. }
  1716. static void iommu_init_flags(struct amd_iommu *iommu)
  1717. {
  1718. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  1719. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  1720. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  1721. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  1722. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  1723. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  1724. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  1725. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  1726. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  1727. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  1728. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  1729. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  1730. /*
  1731. * make IOMMU memory accesses cache coherent
  1732. */
  1733. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  1734. /* Set IOTLB invalidation timeout to 1s */
  1735. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  1736. }
  1737. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  1738. {
  1739. int i, j;
  1740. u32 ioc_feature_control;
  1741. struct pci_dev *pdev = iommu->root_pdev;
  1742. /* RD890 BIOSes may not have completely reconfigured the iommu */
  1743. if (!is_rd890_iommu(iommu->dev) || !pdev)
  1744. return;
  1745. /*
  1746. * First, we need to ensure that the iommu is enabled. This is
  1747. * controlled by a register in the northbridge
  1748. */
  1749. /* Select Northbridge indirect register 0x75 and enable writing */
  1750. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  1751. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  1752. /* Enable the iommu */
  1753. if (!(ioc_feature_control & 0x1))
  1754. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  1755. /* Restore the iommu BAR */
  1756. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1757. iommu->stored_addr_lo);
  1758. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1759. iommu->stored_addr_hi);
  1760. /* Restore the l1 indirect regs for each of the 6 l1s */
  1761. for (i = 0; i < 6; i++)
  1762. for (j = 0; j < 0x12; j++)
  1763. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  1764. /* Restore the l2 indirect regs */
  1765. for (i = 0; i < 0x83; i++)
  1766. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  1767. /* Lock PCI setup registers */
  1768. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1769. iommu->stored_addr_lo | 1);
  1770. }
  1771. static void iommu_enable_ga(struct amd_iommu *iommu)
  1772. {
  1773. #ifdef CONFIG_IRQ_REMAP
  1774. switch (amd_iommu_guest_ir) {
  1775. case AMD_IOMMU_GUEST_IR_VAPIC:
  1776. iommu_feature_enable(iommu, CONTROL_GAM_EN);
  1777. /* Fall through */
  1778. case AMD_IOMMU_GUEST_IR_LEGACY_GA:
  1779. iommu_feature_enable(iommu, CONTROL_GA_EN);
  1780. iommu->irte_ops = &irte_128_ops;
  1781. break;
  1782. default:
  1783. iommu->irte_ops = &irte_32_ops;
  1784. break;
  1785. }
  1786. #endif
  1787. }
  1788. static void early_enable_iommu(struct amd_iommu *iommu)
  1789. {
  1790. iommu_disable(iommu);
  1791. iommu_init_flags(iommu);
  1792. iommu_set_device_table(iommu);
  1793. iommu_enable_command_buffer(iommu);
  1794. iommu_enable_event_buffer(iommu);
  1795. iommu_set_exclusion_range(iommu);
  1796. iommu_enable_ga(iommu);
  1797. iommu_enable_xt(iommu);
  1798. iommu_enable(iommu);
  1799. iommu_flush_all_caches(iommu);
  1800. }
  1801. /*
  1802. * This function finally enables all IOMMUs found in the system after
  1803. * they have been initialized.
  1804. *
  1805. * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
  1806. * the old content of device table entries. Not this case or copy failed,
  1807. * just continue as normal kernel does.
  1808. */
  1809. static void early_enable_iommus(void)
  1810. {
  1811. struct amd_iommu *iommu;
  1812. if (!copy_device_table()) {
  1813. /*
  1814. * If come here because of failure in copying device table from old
  1815. * kernel with all IOMMUs enabled, print error message and try to
  1816. * free allocated old_dev_tbl_cpy.
  1817. */
  1818. if (amd_iommu_pre_enabled)
  1819. pr_err("Failed to copy DEV table from previous kernel.\n");
  1820. if (old_dev_tbl_cpy != NULL)
  1821. free_pages((unsigned long)old_dev_tbl_cpy,
  1822. get_order(dev_table_size));
  1823. for_each_iommu(iommu) {
  1824. clear_translation_pre_enabled(iommu);
  1825. early_enable_iommu(iommu);
  1826. }
  1827. } else {
  1828. pr_info("Copied DEV table from previous kernel.\n");
  1829. free_pages((unsigned long)amd_iommu_dev_table,
  1830. get_order(dev_table_size));
  1831. amd_iommu_dev_table = old_dev_tbl_cpy;
  1832. for_each_iommu(iommu) {
  1833. iommu_disable_command_buffer(iommu);
  1834. iommu_disable_event_buffer(iommu);
  1835. iommu_enable_command_buffer(iommu);
  1836. iommu_enable_event_buffer(iommu);
  1837. iommu_enable_ga(iommu);
  1838. iommu_enable_xt(iommu);
  1839. iommu_set_device_table(iommu);
  1840. iommu_flush_all_caches(iommu);
  1841. }
  1842. }
  1843. #ifdef CONFIG_IRQ_REMAP
  1844. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1845. amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
  1846. #endif
  1847. }
  1848. static void enable_iommus_v2(void)
  1849. {
  1850. struct amd_iommu *iommu;
  1851. for_each_iommu(iommu) {
  1852. iommu_enable_ppr_log(iommu);
  1853. iommu_enable_gt(iommu);
  1854. }
  1855. }
  1856. static void enable_iommus(void)
  1857. {
  1858. early_enable_iommus();
  1859. enable_iommus_v2();
  1860. }
  1861. static void disable_iommus(void)
  1862. {
  1863. struct amd_iommu *iommu;
  1864. for_each_iommu(iommu)
  1865. iommu_disable(iommu);
  1866. #ifdef CONFIG_IRQ_REMAP
  1867. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  1868. amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  1869. #endif
  1870. }
  1871. /*
  1872. * Suspend/Resume support
  1873. * disable suspend until real resume implemented
  1874. */
  1875. static void amd_iommu_resume(void)
  1876. {
  1877. struct amd_iommu *iommu;
  1878. for_each_iommu(iommu)
  1879. iommu_apply_resume_quirks(iommu);
  1880. /* re-load the hardware */
  1881. enable_iommus();
  1882. amd_iommu_enable_interrupts();
  1883. }
  1884. static int amd_iommu_suspend(void)
  1885. {
  1886. /* disable IOMMUs to go out of the way for BIOS */
  1887. disable_iommus();
  1888. return 0;
  1889. }
  1890. static struct syscore_ops amd_iommu_syscore_ops = {
  1891. .suspend = amd_iommu_suspend,
  1892. .resume = amd_iommu_resume,
  1893. };
  1894. static void __init free_iommu_resources(void)
  1895. {
  1896. kmemleak_free(irq_lookup_table);
  1897. free_pages((unsigned long)irq_lookup_table,
  1898. get_order(rlookup_table_size));
  1899. irq_lookup_table = NULL;
  1900. kmem_cache_destroy(amd_iommu_irq_cache);
  1901. amd_iommu_irq_cache = NULL;
  1902. free_pages((unsigned long)amd_iommu_rlookup_table,
  1903. get_order(rlookup_table_size));
  1904. amd_iommu_rlookup_table = NULL;
  1905. free_pages((unsigned long)amd_iommu_alias_table,
  1906. get_order(alias_table_size));
  1907. amd_iommu_alias_table = NULL;
  1908. free_pages((unsigned long)amd_iommu_dev_table,
  1909. get_order(dev_table_size));
  1910. amd_iommu_dev_table = NULL;
  1911. free_iommu_all();
  1912. #ifdef CONFIG_GART_IOMMU
  1913. /*
  1914. * We failed to initialize the AMD IOMMU - try fallback to GART
  1915. * if possible.
  1916. */
  1917. gart_iommu_init();
  1918. #endif
  1919. }
  1920. /* SB IOAPIC is always on this device in AMD systems */
  1921. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  1922. static bool __init check_ioapic_information(void)
  1923. {
  1924. const char *fw_bug = FW_BUG;
  1925. bool ret, has_sb_ioapic;
  1926. int idx;
  1927. has_sb_ioapic = false;
  1928. ret = false;
  1929. /*
  1930. * If we have map overrides on the kernel command line the
  1931. * messages in this function might not describe firmware bugs
  1932. * anymore - so be careful
  1933. */
  1934. if (cmdline_maps)
  1935. fw_bug = "";
  1936. for (idx = 0; idx < nr_ioapics; idx++) {
  1937. int devid, id = mpc_ioapic_id(idx);
  1938. devid = get_ioapic_devid(id);
  1939. if (devid < 0) {
  1940. pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
  1941. fw_bug, id);
  1942. ret = false;
  1943. } else if (devid == IOAPIC_SB_DEVID) {
  1944. has_sb_ioapic = true;
  1945. ret = true;
  1946. }
  1947. }
  1948. if (!has_sb_ioapic) {
  1949. /*
  1950. * We expect the SB IOAPIC to be listed in the IVRS
  1951. * table. The system timer is connected to the SB IOAPIC
  1952. * and if we don't have it in the list the system will
  1953. * panic at boot time. This situation usually happens
  1954. * when the BIOS is buggy and provides us the wrong
  1955. * device id for the IOAPIC in the system.
  1956. */
  1957. pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
  1958. }
  1959. if (!ret)
  1960. pr_err("AMD-Vi: Disabling interrupt remapping\n");
  1961. return ret;
  1962. }
  1963. static void __init free_dma_resources(void)
  1964. {
  1965. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  1966. get_order(MAX_DOMAIN_ID/8));
  1967. amd_iommu_pd_alloc_bitmap = NULL;
  1968. free_unity_maps();
  1969. }
  1970. /*
  1971. * This is the hardware init function for AMD IOMMU in the system.
  1972. * This function is called either from amd_iommu_init or from the interrupt
  1973. * remapping setup code.
  1974. *
  1975. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  1976. * four times:
  1977. *
  1978. * 1 pass) Discover the most comprehensive IVHD type to use.
  1979. *
  1980. * 2 pass) Find the highest PCI device id the driver has to handle.
  1981. * Upon this information the size of the data structures is
  1982. * determined that needs to be allocated.
  1983. *
  1984. * 3 pass) Initialize the data structures just allocated with the
  1985. * information in the ACPI table about available AMD IOMMUs
  1986. * in the system. It also maps the PCI devices in the
  1987. * system to specific IOMMUs
  1988. *
  1989. * 4 pass) After the basic data structures are allocated and
  1990. * initialized we update them with information about memory
  1991. * remapping requirements parsed out of the ACPI table in
  1992. * this last pass.
  1993. *
  1994. * After everything is set up the IOMMUs are enabled and the necessary
  1995. * hotplug and suspend notifiers are registered.
  1996. */
  1997. static int __init early_amd_iommu_init(void)
  1998. {
  1999. struct acpi_table_header *ivrs_base;
  2000. acpi_status status;
  2001. int i, remap_cache_sz, ret = 0;
  2002. if (!amd_iommu_detected)
  2003. return -ENODEV;
  2004. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2005. if (status == AE_NOT_FOUND)
  2006. return -ENODEV;
  2007. else if (ACPI_FAILURE(status)) {
  2008. const char *err = acpi_format_exception(status);
  2009. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  2010. return -EINVAL;
  2011. }
  2012. /*
  2013. * Validate checksum here so we don't need to do it when
  2014. * we actually parse the table
  2015. */
  2016. ret = check_ivrs_checksum(ivrs_base);
  2017. if (ret)
  2018. goto out;
  2019. amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
  2020. DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
  2021. /*
  2022. * First parse ACPI tables to find the largest Bus/Dev/Func
  2023. * we need to handle. Upon this information the shared data
  2024. * structures for the IOMMUs in the system will be allocated
  2025. */
  2026. ret = find_last_devid_acpi(ivrs_base);
  2027. if (ret)
  2028. goto out;
  2029. dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
  2030. alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
  2031. rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
  2032. /* Device table - directly used by all IOMMUs */
  2033. ret = -ENOMEM;
  2034. amd_iommu_dev_table = (void *)__get_free_pages(
  2035. GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
  2036. get_order(dev_table_size));
  2037. if (amd_iommu_dev_table == NULL)
  2038. goto out;
  2039. /*
  2040. * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
  2041. * IOMMU see for that device
  2042. */
  2043. amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
  2044. get_order(alias_table_size));
  2045. if (amd_iommu_alias_table == NULL)
  2046. goto out;
  2047. /* IOMMU rlookup table - find the IOMMU for a specific device */
  2048. amd_iommu_rlookup_table = (void *)__get_free_pages(
  2049. GFP_KERNEL | __GFP_ZERO,
  2050. get_order(rlookup_table_size));
  2051. if (amd_iommu_rlookup_table == NULL)
  2052. goto out;
  2053. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  2054. GFP_KERNEL | __GFP_ZERO,
  2055. get_order(MAX_DOMAIN_ID/8));
  2056. if (amd_iommu_pd_alloc_bitmap == NULL)
  2057. goto out;
  2058. /*
  2059. * let all alias entries point to itself
  2060. */
  2061. for (i = 0; i <= amd_iommu_last_bdf; ++i)
  2062. amd_iommu_alias_table[i] = i;
  2063. /*
  2064. * never allocate domain 0 because its used as the non-allocated and
  2065. * error value placeholder
  2066. */
  2067. __set_bit(0, amd_iommu_pd_alloc_bitmap);
  2068. spin_lock_init(&amd_iommu_pd_lock);
  2069. /*
  2070. * now the data structures are allocated and basically initialized
  2071. * start the real acpi table scan
  2072. */
  2073. ret = init_iommu_all(ivrs_base);
  2074. if (ret)
  2075. goto out;
  2076. /* Disable any previously enabled IOMMUs */
  2077. if (!is_kdump_kernel() || amd_iommu_disabled)
  2078. disable_iommus();
  2079. if (amd_iommu_irq_remap)
  2080. amd_iommu_irq_remap = check_ioapic_information();
  2081. if (amd_iommu_irq_remap) {
  2082. /*
  2083. * Interrupt remapping enabled, create kmem_cache for the
  2084. * remapping tables.
  2085. */
  2086. ret = -ENOMEM;
  2087. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2088. remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
  2089. else
  2090. remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
  2091. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  2092. remap_cache_sz,
  2093. IRQ_TABLE_ALIGNMENT,
  2094. 0, NULL);
  2095. if (!amd_iommu_irq_cache)
  2096. goto out;
  2097. irq_lookup_table = (void *)__get_free_pages(
  2098. GFP_KERNEL | __GFP_ZERO,
  2099. get_order(rlookup_table_size));
  2100. kmemleak_alloc(irq_lookup_table, rlookup_table_size,
  2101. 1, GFP_KERNEL);
  2102. if (!irq_lookup_table)
  2103. goto out;
  2104. }
  2105. ret = init_memory_definitions(ivrs_base);
  2106. if (ret)
  2107. goto out;
  2108. /* init the device table */
  2109. init_device_table();
  2110. out:
  2111. /* Don't leak any ACPI memory */
  2112. acpi_put_table(ivrs_base);
  2113. ivrs_base = NULL;
  2114. return ret;
  2115. }
  2116. static int amd_iommu_enable_interrupts(void)
  2117. {
  2118. struct amd_iommu *iommu;
  2119. int ret = 0;
  2120. for_each_iommu(iommu) {
  2121. ret = iommu_init_msi(iommu);
  2122. if (ret)
  2123. goto out;
  2124. }
  2125. out:
  2126. return ret;
  2127. }
  2128. static bool detect_ivrs(void)
  2129. {
  2130. struct acpi_table_header *ivrs_base;
  2131. acpi_status status;
  2132. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2133. if (status == AE_NOT_FOUND)
  2134. return false;
  2135. else if (ACPI_FAILURE(status)) {
  2136. const char *err = acpi_format_exception(status);
  2137. pr_err("AMD-Vi: IVRS table error: %s\n", err);
  2138. return false;
  2139. }
  2140. acpi_put_table(ivrs_base);
  2141. /* Make sure ACS will be enabled during PCI probe */
  2142. pci_request_acs();
  2143. return true;
  2144. }
  2145. /****************************************************************************
  2146. *
  2147. * AMD IOMMU Initialization State Machine
  2148. *
  2149. ****************************************************************************/
  2150. static int __init state_next(void)
  2151. {
  2152. int ret = 0;
  2153. switch (init_state) {
  2154. case IOMMU_START_STATE:
  2155. if (!detect_ivrs()) {
  2156. init_state = IOMMU_NOT_FOUND;
  2157. ret = -ENODEV;
  2158. } else {
  2159. init_state = IOMMU_IVRS_DETECTED;
  2160. }
  2161. break;
  2162. case IOMMU_IVRS_DETECTED:
  2163. ret = early_amd_iommu_init();
  2164. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  2165. if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
  2166. pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
  2167. free_dma_resources();
  2168. free_iommu_resources();
  2169. init_state = IOMMU_CMDLINE_DISABLED;
  2170. ret = -EINVAL;
  2171. }
  2172. break;
  2173. case IOMMU_ACPI_FINISHED:
  2174. early_enable_iommus();
  2175. x86_platform.iommu_shutdown = disable_iommus;
  2176. init_state = IOMMU_ENABLED;
  2177. break;
  2178. case IOMMU_ENABLED:
  2179. register_syscore_ops(&amd_iommu_syscore_ops);
  2180. ret = amd_iommu_init_pci();
  2181. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  2182. enable_iommus_v2();
  2183. break;
  2184. case IOMMU_PCI_INIT:
  2185. ret = amd_iommu_enable_interrupts();
  2186. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  2187. break;
  2188. case IOMMU_INTERRUPTS_EN:
  2189. ret = amd_iommu_init_dma_ops();
  2190. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
  2191. break;
  2192. case IOMMU_DMA_OPS:
  2193. init_state = IOMMU_INITIALIZED;
  2194. break;
  2195. case IOMMU_INITIALIZED:
  2196. /* Nothing to do */
  2197. break;
  2198. case IOMMU_NOT_FOUND:
  2199. case IOMMU_INIT_ERROR:
  2200. case IOMMU_CMDLINE_DISABLED:
  2201. /* Error states => do nothing */
  2202. ret = -EINVAL;
  2203. break;
  2204. default:
  2205. /* Unknown state */
  2206. BUG();
  2207. }
  2208. return ret;
  2209. }
  2210. static int __init iommu_go_to_state(enum iommu_init_state state)
  2211. {
  2212. int ret = -EINVAL;
  2213. while (init_state != state) {
  2214. if (init_state == IOMMU_NOT_FOUND ||
  2215. init_state == IOMMU_INIT_ERROR ||
  2216. init_state == IOMMU_CMDLINE_DISABLED)
  2217. break;
  2218. ret = state_next();
  2219. }
  2220. return ret;
  2221. }
  2222. #ifdef CONFIG_IRQ_REMAP
  2223. int __init amd_iommu_prepare(void)
  2224. {
  2225. int ret;
  2226. amd_iommu_irq_remap = true;
  2227. ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
  2228. if (ret)
  2229. return ret;
  2230. return amd_iommu_irq_remap ? 0 : -ENODEV;
  2231. }
  2232. int __init amd_iommu_enable(void)
  2233. {
  2234. int ret;
  2235. ret = iommu_go_to_state(IOMMU_ENABLED);
  2236. if (ret)
  2237. return ret;
  2238. irq_remapping_enabled = 1;
  2239. return amd_iommu_xt_mode;
  2240. }
  2241. void amd_iommu_disable(void)
  2242. {
  2243. amd_iommu_suspend();
  2244. }
  2245. int amd_iommu_reenable(int mode)
  2246. {
  2247. amd_iommu_resume();
  2248. return 0;
  2249. }
  2250. int __init amd_iommu_enable_faulting(void)
  2251. {
  2252. /* We enable MSI later when PCI is initialized */
  2253. return 0;
  2254. }
  2255. #endif
  2256. /*
  2257. * This is the core init function for AMD IOMMU hardware in the system.
  2258. * This function is called from the generic x86 DMA layer initialization
  2259. * code.
  2260. */
  2261. static int __init amd_iommu_init(void)
  2262. {
  2263. struct amd_iommu *iommu;
  2264. int ret;
  2265. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  2266. if (ret) {
  2267. free_dma_resources();
  2268. if (!irq_remapping_enabled) {
  2269. disable_iommus();
  2270. free_iommu_resources();
  2271. } else {
  2272. uninit_device_table_dma();
  2273. for_each_iommu(iommu)
  2274. iommu_flush_all_caches(iommu);
  2275. }
  2276. }
  2277. for_each_iommu(iommu)
  2278. amd_iommu_debugfs_setup(iommu);
  2279. return ret;
  2280. }
  2281. static bool amd_iommu_sme_check(void)
  2282. {
  2283. if (!sme_active() || (boot_cpu_data.x86 != 0x17))
  2284. return true;
  2285. /* For Fam17h, a specific level of support is required */
  2286. if (boot_cpu_data.microcode >= 0x08001205)
  2287. return true;
  2288. if ((boot_cpu_data.microcode >= 0x08001126) &&
  2289. (boot_cpu_data.microcode <= 0x080011ff))
  2290. return true;
  2291. pr_notice("AMD-Vi: IOMMU not currently supported when SME is active\n");
  2292. return false;
  2293. }
  2294. /****************************************************************************
  2295. *
  2296. * Early detect code. This code runs at IOMMU detection time in the DMA
  2297. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  2298. * IOMMUs
  2299. *
  2300. ****************************************************************************/
  2301. int __init amd_iommu_detect(void)
  2302. {
  2303. int ret;
  2304. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  2305. return -ENODEV;
  2306. if (!amd_iommu_sme_check())
  2307. return -ENODEV;
  2308. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  2309. if (ret)
  2310. return ret;
  2311. amd_iommu_detected = true;
  2312. iommu_detected = 1;
  2313. x86_init.iommu.iommu_init = amd_iommu_init;
  2314. return 1;
  2315. }
  2316. /****************************************************************************
  2317. *
  2318. * Parsing functions for the AMD IOMMU specific kernel command line
  2319. * options.
  2320. *
  2321. ****************************************************************************/
  2322. static int __init parse_amd_iommu_dump(char *str)
  2323. {
  2324. amd_iommu_dump = true;
  2325. return 1;
  2326. }
  2327. static int __init parse_amd_iommu_intr(char *str)
  2328. {
  2329. for (; *str; ++str) {
  2330. if (strncmp(str, "legacy", 6) == 0) {
  2331. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2332. break;
  2333. }
  2334. if (strncmp(str, "vapic", 5) == 0) {
  2335. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  2336. break;
  2337. }
  2338. }
  2339. return 1;
  2340. }
  2341. static int __init parse_amd_iommu_options(char *str)
  2342. {
  2343. for (; *str; ++str) {
  2344. if (strncmp(str, "fullflush", 9) == 0)
  2345. amd_iommu_unmap_flush = true;
  2346. if (strncmp(str, "off", 3) == 0)
  2347. amd_iommu_disabled = true;
  2348. if (strncmp(str, "force_isolation", 15) == 0)
  2349. amd_iommu_force_isolation = true;
  2350. }
  2351. return 1;
  2352. }
  2353. static int __init parse_ivrs_ioapic(char *str)
  2354. {
  2355. unsigned int bus, dev, fn;
  2356. int ret, id, i;
  2357. u16 devid;
  2358. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  2359. if (ret != 4) {
  2360. pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
  2361. return 1;
  2362. }
  2363. if (early_ioapic_map_size == EARLY_MAP_SIZE) {
  2364. pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
  2365. str);
  2366. return 1;
  2367. }
  2368. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2369. cmdline_maps = true;
  2370. i = early_ioapic_map_size++;
  2371. early_ioapic_map[i].id = id;
  2372. early_ioapic_map[i].devid = devid;
  2373. early_ioapic_map[i].cmd_line = true;
  2374. return 1;
  2375. }
  2376. static int __init parse_ivrs_hpet(char *str)
  2377. {
  2378. unsigned int bus, dev, fn;
  2379. int ret, id, i;
  2380. u16 devid;
  2381. ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
  2382. if (ret != 4) {
  2383. pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
  2384. return 1;
  2385. }
  2386. if (early_hpet_map_size == EARLY_MAP_SIZE) {
  2387. pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
  2388. str);
  2389. return 1;
  2390. }
  2391. devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2392. cmdline_maps = true;
  2393. i = early_hpet_map_size++;
  2394. early_hpet_map[i].id = id;
  2395. early_hpet_map[i].devid = devid;
  2396. early_hpet_map[i].cmd_line = true;
  2397. return 1;
  2398. }
  2399. static int __init parse_ivrs_acpihid(char *str)
  2400. {
  2401. u32 bus, dev, fn;
  2402. char *hid, *uid, *p;
  2403. char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
  2404. int ret, i;
  2405. ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
  2406. if (ret != 4) {
  2407. pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
  2408. return 1;
  2409. }
  2410. p = acpiid;
  2411. hid = strsep(&p, ":");
  2412. uid = p;
  2413. if (!hid || !(*hid) || !uid) {
  2414. pr_err("AMD-Vi: Invalid command line: hid or uid\n");
  2415. return 1;
  2416. }
  2417. i = early_acpihid_map_size++;
  2418. memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
  2419. memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
  2420. early_acpihid_map[i].devid =
  2421. ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
  2422. early_acpihid_map[i].cmd_line = true;
  2423. return 1;
  2424. }
  2425. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  2426. __setup("amd_iommu=", parse_amd_iommu_options);
  2427. __setup("amd_iommu_intr=", parse_amd_iommu_intr);
  2428. __setup("ivrs_ioapic", parse_ivrs_ioapic);
  2429. __setup("ivrs_hpet", parse_ivrs_hpet);
  2430. __setup("ivrs_acpihid", parse_ivrs_acpihid);
  2431. IOMMU_INIT_FINISH(amd_iommu_detect,
  2432. gart_iommu_hole_init,
  2433. NULL,
  2434. NULL);
  2435. bool amd_iommu_v2_supported(void)
  2436. {
  2437. return amd_iommu_v2_present;
  2438. }
  2439. EXPORT_SYMBOL(amd_iommu_v2_supported);
  2440. struct amd_iommu *get_amd_iommu(unsigned int idx)
  2441. {
  2442. unsigned int i = 0;
  2443. struct amd_iommu *iommu;
  2444. for_each_iommu(iommu)
  2445. if (i++ == idx)
  2446. return iommu;
  2447. return NULL;
  2448. }
  2449. EXPORT_SYMBOL(get_amd_iommu);
  2450. /****************************************************************************
  2451. *
  2452. * IOMMU EFR Performance Counter support functionality. This code allows
  2453. * access to the IOMMU PC functionality.
  2454. *
  2455. ****************************************************************************/
  2456. u8 amd_iommu_pc_get_max_banks(unsigned int idx)
  2457. {
  2458. struct amd_iommu *iommu = get_amd_iommu(idx);
  2459. if (iommu)
  2460. return iommu->max_banks;
  2461. return 0;
  2462. }
  2463. EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
  2464. bool amd_iommu_pc_supported(void)
  2465. {
  2466. return amd_iommu_pc_present;
  2467. }
  2468. EXPORT_SYMBOL(amd_iommu_pc_supported);
  2469. u8 amd_iommu_pc_get_max_counters(unsigned int idx)
  2470. {
  2471. struct amd_iommu *iommu = get_amd_iommu(idx);
  2472. if (iommu)
  2473. return iommu->max_counters;
  2474. return 0;
  2475. }
  2476. EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
  2477. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  2478. u8 fxn, u64 *value, bool is_write)
  2479. {
  2480. u32 offset;
  2481. u32 max_offset_lim;
  2482. /* Make sure the IOMMU PC resource is available */
  2483. if (!amd_iommu_pc_present)
  2484. return -ENODEV;
  2485. /* Check for valid iommu and pc register indexing */
  2486. if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
  2487. return -ENODEV;
  2488. offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
  2489. /* Limit the offset to the hw defined mmio region aperture */
  2490. max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
  2491. (iommu->max_counters << 8) | 0x28);
  2492. if ((offset < MMIO_CNTR_REG_OFFSET) ||
  2493. (offset > max_offset_lim))
  2494. return -EINVAL;
  2495. if (is_write) {
  2496. u64 val = *value & GENMASK_ULL(47, 0);
  2497. writel((u32)val, iommu->mmio_base + offset);
  2498. writel((val >> 32), iommu->mmio_base + offset + 4);
  2499. } else {
  2500. *value = readl(iommu->mmio_base + offset + 4);
  2501. *value <<= 32;
  2502. *value |= readl(iommu->mmio_base + offset);
  2503. *value &= GENMASK_ULL(47, 0);
  2504. }
  2505. return 0;
  2506. }
  2507. int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  2508. {
  2509. if (!iommu)
  2510. return -EINVAL;
  2511. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
  2512. }
  2513. EXPORT_SYMBOL(amd_iommu_pc_get_reg);
  2514. int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  2515. {
  2516. if (!iommu)
  2517. return -EINVAL;
  2518. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
  2519. }
  2520. EXPORT_SYMBOL(amd_iommu_pc_set_reg);