amd_iommu_init.c 75 KB

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