amd_iommu_init.c 74 KB

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