init.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <[email protected]>
  5. * Leo Duran <[email protected]>
  6. */
  7. #define pr_fmt(fmt) "AMD-Vi: " fmt
  8. #define dev_fmt(fmt) pr_fmt(fmt)
  9. #include <linux/pci.h>
  10. #include <linux/acpi.h>
  11. #include <linux/list.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/syscore_ops.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/msi.h>
  17. #include <linux/irq.h>
  18. #include <linux/amd-iommu.h>
  19. #include <linux/export.h>
  20. #include <linux/kmemleak.h>
  21. #include <linux/cc_platform.h>
  22. #include <linux/iopoll.h>
  23. #include <asm/pci-direct.h>
  24. #include <asm/iommu.h>
  25. #include <asm/apic.h>
  26. #include <asm/gart.h>
  27. #include <asm/x86_init.h>
  28. #include <asm/io_apic.h>
  29. #include <asm/irq_remapping.h>
  30. #include <asm/set_memory.h>
  31. #include <linux/crash_dump.h>
  32. #include "amd_iommu.h"
  33. #include "../irq_remapping.h"
  34. /*
  35. * definitions for the ACPI scanning code
  36. */
  37. #define IVRS_HEADER_LENGTH 48
  38. #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
  39. #define ACPI_IVMD_TYPE_ALL 0x20
  40. #define ACPI_IVMD_TYPE 0x21
  41. #define ACPI_IVMD_TYPE_RANGE 0x22
  42. #define IVHD_DEV_ALL 0x01
  43. #define IVHD_DEV_SELECT 0x02
  44. #define IVHD_DEV_SELECT_RANGE_START 0x03
  45. #define IVHD_DEV_RANGE_END 0x04
  46. #define IVHD_DEV_ALIAS 0x42
  47. #define IVHD_DEV_ALIAS_RANGE 0x43
  48. #define IVHD_DEV_EXT_SELECT 0x46
  49. #define IVHD_DEV_EXT_SELECT_RANGE 0x47
  50. #define IVHD_DEV_SPECIAL 0x48
  51. #define IVHD_DEV_ACPI_HID 0xf0
  52. #define UID_NOT_PRESENT 0
  53. #define UID_IS_INTEGER 1
  54. #define UID_IS_CHARACTER 2
  55. #define IVHD_SPECIAL_IOAPIC 1
  56. #define IVHD_SPECIAL_HPET 2
  57. #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
  58. #define IVHD_FLAG_PASSPW_EN_MASK 0x02
  59. #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
  60. #define IVHD_FLAG_ISOC_EN_MASK 0x08
  61. #define IVMD_FLAG_EXCL_RANGE 0x08
  62. #define IVMD_FLAG_IW 0x04
  63. #define IVMD_FLAG_IR 0x02
  64. #define IVMD_FLAG_UNITY_MAP 0x01
  65. #define ACPI_DEVFLAG_INITPASS 0x01
  66. #define ACPI_DEVFLAG_EXTINT 0x02
  67. #define ACPI_DEVFLAG_NMI 0x04
  68. #define ACPI_DEVFLAG_SYSMGT1 0x10
  69. #define ACPI_DEVFLAG_SYSMGT2 0x20
  70. #define ACPI_DEVFLAG_LINT0 0x40
  71. #define ACPI_DEVFLAG_LINT1 0x80
  72. #define ACPI_DEVFLAG_ATSDIS 0x10000000
  73. #define LOOP_TIMEOUT 2000000
  74. #define IVRS_GET_SBDF_ID(seg, bus, dev, fd) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \
  75. | ((dev & 0x1f) << 3) | (fn & 0x7))
  76. /*
  77. * ACPI table definitions
  78. *
  79. * These data structures are laid over the table to parse the important values
  80. * out of it.
  81. */
  82. /*
  83. * structure describing one IOMMU in the ACPI table. Typically followed by one
  84. * or more ivhd_entrys.
  85. */
  86. struct ivhd_header {
  87. u8 type;
  88. u8 flags;
  89. u16 length;
  90. u16 devid;
  91. u16 cap_ptr;
  92. u64 mmio_phys;
  93. u16 pci_seg;
  94. u16 info;
  95. u32 efr_attr;
  96. /* Following only valid on IVHD type 11h and 40h */
  97. u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
  98. u64 efr_reg2;
  99. } __attribute__((packed));
  100. /*
  101. * A device entry describing which devices a specific IOMMU translates and
  102. * which requestor ids they use.
  103. */
  104. struct ivhd_entry {
  105. u8 type;
  106. u16 devid;
  107. u8 flags;
  108. struct_group(ext_hid,
  109. u32 ext;
  110. u32 hidh;
  111. );
  112. u64 cid;
  113. u8 uidf;
  114. u8 uidl;
  115. u8 uid;
  116. } __attribute__((packed));
  117. /*
  118. * An AMD IOMMU memory definition structure. It defines things like exclusion
  119. * ranges for devices and regions that should be unity mapped.
  120. */
  121. struct ivmd_header {
  122. u8 type;
  123. u8 flags;
  124. u16 length;
  125. u16 devid;
  126. u16 aux;
  127. u16 pci_seg;
  128. u8 resv[6];
  129. u64 range_start;
  130. u64 range_length;
  131. } __attribute__((packed));
  132. bool amd_iommu_dump;
  133. bool amd_iommu_irq_remap __read_mostly;
  134. enum io_pgtable_fmt amd_iommu_pgtable = AMD_IOMMU_V1;
  135. int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  136. static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
  137. static bool amd_iommu_detected;
  138. static bool amd_iommu_disabled __initdata;
  139. static bool amd_iommu_force_enable __initdata;
  140. static bool amd_iommu_irtcachedis;
  141. static int amd_iommu_target_ivhd_type;
  142. /* Global EFR and EFR2 registers */
  143. u64 amd_iommu_efr;
  144. u64 amd_iommu_efr2;
  145. /* SNP is enabled on the system? */
  146. bool amd_iommu_snp_en;
  147. EXPORT_SYMBOL(amd_iommu_snp_en);
  148. LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
  149. LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
  150. system */
  151. /* Array to assign indices to IOMMUs*/
  152. struct amd_iommu *amd_iommus[MAX_IOMMUS];
  153. /* Number of IOMMUs present in the system */
  154. static int amd_iommus_present;
  155. /* IOMMUs have a non-present cache? */
  156. bool amd_iommu_np_cache __read_mostly;
  157. bool amd_iommu_iotlb_sup __read_mostly = true;
  158. u32 amd_iommu_max_pasid __read_mostly = ~0;
  159. bool amd_iommu_v2_present __read_mostly;
  160. static bool amd_iommu_pc_present __read_mostly;
  161. bool amdr_ivrs_remap_support __read_mostly;
  162. bool amd_iommu_force_isolation __read_mostly;
  163. /*
  164. * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
  165. * to know which ones are already in use.
  166. */
  167. unsigned long *amd_iommu_pd_alloc_bitmap;
  168. enum iommu_init_state {
  169. IOMMU_START_STATE,
  170. IOMMU_IVRS_DETECTED,
  171. IOMMU_ACPI_FINISHED,
  172. IOMMU_ENABLED,
  173. IOMMU_PCI_INIT,
  174. IOMMU_INTERRUPTS_EN,
  175. IOMMU_INITIALIZED,
  176. IOMMU_NOT_FOUND,
  177. IOMMU_INIT_ERROR,
  178. IOMMU_CMDLINE_DISABLED,
  179. };
  180. /* Early ioapic and hpet maps from kernel command line */
  181. #define EARLY_MAP_SIZE 4
  182. static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
  183. static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
  184. static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
  185. static int __initdata early_ioapic_map_size;
  186. static int __initdata early_hpet_map_size;
  187. static int __initdata early_acpihid_map_size;
  188. static bool __initdata cmdline_maps;
  189. static enum iommu_init_state init_state = IOMMU_START_STATE;
  190. static int amd_iommu_enable_interrupts(void);
  191. static int __init iommu_go_to_state(enum iommu_init_state state);
  192. static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);
  193. static bool amd_iommu_pre_enabled = true;
  194. static u32 amd_iommu_ivinfo __initdata;
  195. bool translation_pre_enabled(struct amd_iommu *iommu)
  196. {
  197. return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
  198. }
  199. static void clear_translation_pre_enabled(struct amd_iommu *iommu)
  200. {
  201. iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  202. }
  203. static void init_translation_status(struct amd_iommu *iommu)
  204. {
  205. u64 ctrl;
  206. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  207. if (ctrl & (1<<CONTROL_IOMMU_EN))
  208. iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
  209. }
  210. static inline unsigned long tbl_size(int entry_size, int last_bdf)
  211. {
  212. unsigned shift = PAGE_SHIFT +
  213. get_order((last_bdf + 1) * entry_size);
  214. return 1UL << shift;
  215. }
  216. int amd_iommu_get_num_iommus(void)
  217. {
  218. return amd_iommus_present;
  219. }
  220. /*
  221. * Iterate through all the IOMMUs to get common EFR
  222. * masks among all IOMMUs and warn if found inconsistency.
  223. */
  224. static void get_global_efr(void)
  225. {
  226. struct amd_iommu *iommu;
  227. for_each_iommu(iommu) {
  228. u64 tmp = iommu->features;
  229. u64 tmp2 = iommu->features2;
  230. if (list_is_first(&iommu->list, &amd_iommu_list)) {
  231. amd_iommu_efr = tmp;
  232. amd_iommu_efr2 = tmp2;
  233. continue;
  234. }
  235. if (amd_iommu_efr == tmp &&
  236. amd_iommu_efr2 == tmp2)
  237. continue;
  238. pr_err(FW_BUG
  239. "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",
  240. tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,
  241. iommu->index, iommu->pci_seg->id,
  242. PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),
  243. PCI_FUNC(iommu->devid));
  244. amd_iommu_efr &= tmp;
  245. amd_iommu_efr2 &= tmp2;
  246. }
  247. pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
  248. }
  249. static bool check_feature_on_all_iommus(u64 mask)
  250. {
  251. return !!(amd_iommu_efr & mask);
  252. }
  253. /*
  254. * For IVHD type 0x11/0x40, EFR is also available via IVHD.
  255. * Default to IVHD EFR since it is available sooner
  256. * (i.e. before PCI init).
  257. */
  258. static void __init early_iommu_features_init(struct amd_iommu *iommu,
  259. struct ivhd_header *h)
  260. {
  261. if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {
  262. iommu->features = h->efr_reg;
  263. iommu->features2 = h->efr_reg2;
  264. }
  265. if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
  266. amdr_ivrs_remap_support = true;
  267. }
  268. /* Access to l1 and l2 indexed register spaces */
  269. static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
  270. {
  271. u32 val;
  272. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  273. pci_read_config_dword(iommu->dev, 0xfc, &val);
  274. return val;
  275. }
  276. static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
  277. {
  278. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
  279. pci_write_config_dword(iommu->dev, 0xfc, val);
  280. pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
  281. }
  282. static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
  283. {
  284. u32 val;
  285. pci_write_config_dword(iommu->dev, 0xf0, address);
  286. pci_read_config_dword(iommu->dev, 0xf4, &val);
  287. return val;
  288. }
  289. static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
  290. {
  291. pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
  292. pci_write_config_dword(iommu->dev, 0xf4, val);
  293. }
  294. /****************************************************************************
  295. *
  296. * AMD IOMMU MMIO register space handling functions
  297. *
  298. * These functions are used to program the IOMMU device registers in
  299. * MMIO space required for that driver.
  300. *
  301. ****************************************************************************/
  302. /*
  303. * This function set the exclusion range in the IOMMU. DMA accesses to the
  304. * exclusion range are passed through untranslated
  305. */
  306. static void iommu_set_exclusion_range(struct amd_iommu *iommu)
  307. {
  308. u64 start = iommu->exclusion_start & PAGE_MASK;
  309. u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
  310. u64 entry;
  311. if (!iommu->exclusion_start)
  312. return;
  313. entry = start | MMIO_EXCL_ENABLE_MASK;
  314. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  315. &entry, sizeof(entry));
  316. entry = limit;
  317. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  318. &entry, sizeof(entry));
  319. }
  320. static void iommu_set_cwwb_range(struct amd_iommu *iommu)
  321. {
  322. u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
  323. u64 entry = start & PM_ADDR_MASK;
  324. if (!check_feature_on_all_iommus(FEATURE_SNP))
  325. return;
  326. /* Note:
  327. * Re-purpose Exclusion base/limit registers for Completion wait
  328. * write-back base/limit.
  329. */
  330. memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
  331. &entry, sizeof(entry));
  332. /* Note:
  333. * Default to 4 Kbytes, which can be specified by setting base
  334. * address equal to the limit address.
  335. */
  336. memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
  337. &entry, sizeof(entry));
  338. }
  339. /* Programs the physical address of the device table into the IOMMU hardware */
  340. static void iommu_set_device_table(struct amd_iommu *iommu)
  341. {
  342. u64 entry;
  343. u32 dev_table_size = iommu->pci_seg->dev_table_size;
  344. void *dev_table = (void *)get_dev_table(iommu);
  345. BUG_ON(iommu->mmio_base == NULL);
  346. entry = iommu_virt_to_phys(dev_table);
  347. entry |= (dev_table_size >> 12) - 1;
  348. memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
  349. &entry, sizeof(entry));
  350. }
  351. /* Generic functions to enable/disable certain features of the IOMMU. */
  352. static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
  353. {
  354. u64 ctrl;
  355. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  356. ctrl |= (1ULL << bit);
  357. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  358. }
  359. static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
  360. {
  361. u64 ctrl;
  362. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  363. ctrl &= ~(1ULL << bit);
  364. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  365. }
  366. static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
  367. {
  368. u64 ctrl;
  369. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  370. ctrl &= ~CTRL_INV_TO_MASK;
  371. ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
  372. writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
  373. }
  374. /* Function to enable the hardware */
  375. static void iommu_enable(struct amd_iommu *iommu)
  376. {
  377. iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
  378. }
  379. static void iommu_disable(struct amd_iommu *iommu)
  380. {
  381. if (!iommu->mmio_base)
  382. return;
  383. /* Disable command buffer */
  384. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  385. /* Disable event logging and event interrupts */
  386. iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
  387. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  388. /* Disable IOMMU GA_LOG */
  389. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  390. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  391. /* Disable IOMMU hardware itself */
  392. iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
  393. /* Clear IRTE cache disabling bit */
  394. iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
  395. }
  396. /*
  397. * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
  398. * the system has one.
  399. */
  400. static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
  401. {
  402. if (!request_mem_region(address, end, "amd_iommu")) {
  403. pr_err("Can not reserve memory region %llx-%llx for mmio\n",
  404. address, end);
  405. pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
  406. return NULL;
  407. }
  408. return (u8 __iomem *)ioremap(address, end);
  409. }
  410. static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
  411. {
  412. if (iommu->mmio_base)
  413. iounmap(iommu->mmio_base);
  414. release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
  415. }
  416. static inline u32 get_ivhd_header_size(struct ivhd_header *h)
  417. {
  418. u32 size = 0;
  419. switch (h->type) {
  420. case 0x10:
  421. size = 24;
  422. break;
  423. case 0x11:
  424. case 0x40:
  425. size = 40;
  426. break;
  427. }
  428. return size;
  429. }
  430. /****************************************************************************
  431. *
  432. * The functions below belong to the first pass of AMD IOMMU ACPI table
  433. * parsing. In this pass we try to find out the highest device id this
  434. * code has to handle. Upon this information the size of the shared data
  435. * structures is determined later.
  436. *
  437. ****************************************************************************/
  438. /*
  439. * This function calculates the length of a given IVHD entry
  440. */
  441. static inline int ivhd_entry_length(u8 *ivhd)
  442. {
  443. u32 type = ((struct ivhd_entry *)ivhd)->type;
  444. if (type < 0x80) {
  445. return 0x04 << (*ivhd >> 6);
  446. } else if (type == IVHD_DEV_ACPI_HID) {
  447. /* For ACPI_HID, offset 21 is uid len */
  448. return *((u8 *)ivhd + 21) + 22;
  449. }
  450. return 0;
  451. }
  452. /*
  453. * After reading the highest device id from the IOMMU PCI capability header
  454. * this function looks if there is a higher device id defined in the ACPI table
  455. */
  456. static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
  457. {
  458. u8 *p = (void *)h, *end = (void *)h;
  459. struct ivhd_entry *dev;
  460. int last_devid = -EINVAL;
  461. u32 ivhd_size = get_ivhd_header_size(h);
  462. if (!ivhd_size) {
  463. pr_err("Unsupported IVHD type %#x\n", h->type);
  464. return -EINVAL;
  465. }
  466. p += ivhd_size;
  467. end += h->length;
  468. while (p < end) {
  469. dev = (struct ivhd_entry *)p;
  470. switch (dev->type) {
  471. case IVHD_DEV_ALL:
  472. /* Use maximum BDF value for DEV_ALL */
  473. return 0xffff;
  474. case IVHD_DEV_SELECT:
  475. case IVHD_DEV_RANGE_END:
  476. case IVHD_DEV_ALIAS:
  477. case IVHD_DEV_EXT_SELECT:
  478. /* all the above subfield types refer to device ids */
  479. if (dev->devid > last_devid)
  480. last_devid = dev->devid;
  481. break;
  482. default:
  483. break;
  484. }
  485. p += ivhd_entry_length(p);
  486. }
  487. WARN_ON(p != end);
  488. return last_devid;
  489. }
  490. static int __init check_ivrs_checksum(struct acpi_table_header *table)
  491. {
  492. int i;
  493. u8 checksum = 0, *p = (u8 *)table;
  494. for (i = 0; i < table->length; ++i)
  495. checksum += p[i];
  496. if (checksum != 0) {
  497. /* ACPI table corrupt */
  498. pr_err(FW_BUG "IVRS invalid checksum\n");
  499. return -ENODEV;
  500. }
  501. return 0;
  502. }
  503. /*
  504. * Iterate over all IVHD entries in the ACPI table and find the highest device
  505. * id which we need to handle. This is the first of three functions which parse
  506. * the ACPI table. So we check the checksum here.
  507. */
  508. static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)
  509. {
  510. u8 *p = (u8 *)table, *end = (u8 *)table;
  511. struct ivhd_header *h;
  512. int last_devid, last_bdf = 0;
  513. p += IVRS_HEADER_LENGTH;
  514. end += table->length;
  515. while (p < end) {
  516. h = (struct ivhd_header *)p;
  517. if (h->pci_seg == pci_seg &&
  518. h->type == amd_iommu_target_ivhd_type) {
  519. last_devid = find_last_devid_from_ivhd(h);
  520. if (last_devid < 0)
  521. return -EINVAL;
  522. if (last_devid > last_bdf)
  523. last_bdf = last_devid;
  524. }
  525. p += h->length;
  526. }
  527. WARN_ON(p != end);
  528. return last_bdf;
  529. }
  530. /****************************************************************************
  531. *
  532. * The following functions belong to the code path which parses the ACPI table
  533. * the second time. In this ACPI parsing iteration we allocate IOMMU specific
  534. * data structures, initialize the per PCI segment device/alias/rlookup table
  535. * and also basically initialize the hardware.
  536. *
  537. ****************************************************************************/
  538. /* Allocate per PCI segment device table */
  539. static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)
  540. {
  541. pci_seg->dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
  542. get_order(pci_seg->dev_table_size));
  543. if (!pci_seg->dev_table)
  544. return -ENOMEM;
  545. return 0;
  546. }
  547. static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
  548. {
  549. free_pages((unsigned long)pci_seg->dev_table,
  550. get_order(pci_seg->dev_table_size));
  551. pci_seg->dev_table = NULL;
  552. }
  553. /* Allocate per PCI segment IOMMU rlookup table. */
  554. static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
  555. {
  556. pci_seg->rlookup_table = (void *)__get_free_pages(
  557. GFP_KERNEL | __GFP_ZERO,
  558. get_order(pci_seg->rlookup_table_size));
  559. if (pci_seg->rlookup_table == NULL)
  560. return -ENOMEM;
  561. return 0;
  562. }
  563. static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
  564. {
  565. free_pages((unsigned long)pci_seg->rlookup_table,
  566. get_order(pci_seg->rlookup_table_size));
  567. pci_seg->rlookup_table = NULL;
  568. }
  569. static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
  570. {
  571. pci_seg->irq_lookup_table = (void *)__get_free_pages(
  572. GFP_KERNEL | __GFP_ZERO,
  573. get_order(pci_seg->rlookup_table_size));
  574. kmemleak_alloc(pci_seg->irq_lookup_table,
  575. pci_seg->rlookup_table_size, 1, GFP_KERNEL);
  576. if (pci_seg->irq_lookup_table == NULL)
  577. return -ENOMEM;
  578. return 0;
  579. }
  580. static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
  581. {
  582. kmemleak_free(pci_seg->irq_lookup_table);
  583. free_pages((unsigned long)pci_seg->irq_lookup_table,
  584. get_order(pci_seg->rlookup_table_size));
  585. pci_seg->irq_lookup_table = NULL;
  586. }
  587. static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
  588. {
  589. int i;
  590. pci_seg->alias_table = (void *)__get_free_pages(GFP_KERNEL,
  591. get_order(pci_seg->alias_table_size));
  592. if (!pci_seg->alias_table)
  593. return -ENOMEM;
  594. /*
  595. * let all alias entries point to itself
  596. */
  597. for (i = 0; i <= pci_seg->last_bdf; ++i)
  598. pci_seg->alias_table[i] = i;
  599. return 0;
  600. }
  601. static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
  602. {
  603. free_pages((unsigned long)pci_seg->alias_table,
  604. get_order(pci_seg->alias_table_size));
  605. pci_seg->alias_table = NULL;
  606. }
  607. /*
  608. * Allocates the command buffer. This buffer is per AMD IOMMU. We can
  609. * write commands to that buffer later and the IOMMU will execute them
  610. * asynchronously
  611. */
  612. static int __init alloc_command_buffer(struct amd_iommu *iommu)
  613. {
  614. iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  615. get_order(CMD_BUFFER_SIZE));
  616. return iommu->cmd_buf ? 0 : -ENOMEM;
  617. }
  618. /*
  619. * This function restarts event logging in case the IOMMU experienced
  620. * an event log buffer overflow.
  621. */
  622. void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
  623. {
  624. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  625. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  626. }
  627. /*
  628. * This function restarts event logging in case the IOMMU experienced
  629. * an GA log overflow.
  630. */
  631. void amd_iommu_restart_ga_log(struct amd_iommu *iommu)
  632. {
  633. u32 status;
  634. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  635. if (status & MMIO_STATUS_GALOG_RUN_MASK)
  636. return;
  637. pr_info_ratelimited("IOMMU GA Log restarting\n");
  638. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  639. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  640. writel(MMIO_STATUS_GALOG_OVERFLOW_MASK,
  641. iommu->mmio_base + MMIO_STATUS_OFFSET);
  642. iommu_feature_enable(iommu, CONTROL_GAINT_EN);
  643. iommu_feature_enable(iommu, CONTROL_GALOG_EN);
  644. }
  645. /*
  646. * This function resets the command buffer if the IOMMU stopped fetching
  647. * commands from it.
  648. */
  649. static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
  650. {
  651. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  652. writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  653. writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  654. iommu->cmd_buf_head = 0;
  655. iommu->cmd_buf_tail = 0;
  656. iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
  657. }
  658. /*
  659. * This function writes the command buffer address to the hardware and
  660. * enables it.
  661. */
  662. static void iommu_enable_command_buffer(struct amd_iommu *iommu)
  663. {
  664. u64 entry;
  665. BUG_ON(iommu->cmd_buf == NULL);
  666. entry = iommu_virt_to_phys(iommu->cmd_buf);
  667. entry |= MMIO_CMD_SIZE_512;
  668. memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
  669. &entry, sizeof(entry));
  670. amd_iommu_reset_cmd_buffer(iommu);
  671. }
  672. /*
  673. * This function disables the command buffer
  674. */
  675. static void iommu_disable_command_buffer(struct amd_iommu *iommu)
  676. {
  677. iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
  678. }
  679. static void __init free_command_buffer(struct amd_iommu *iommu)
  680. {
  681. free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
  682. }
  683. static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
  684. gfp_t gfp, size_t size)
  685. {
  686. int order = get_order(size);
  687. void *buf = (void *)__get_free_pages(gfp, order);
  688. if (buf &&
  689. check_feature_on_all_iommus(FEATURE_SNP) &&
  690. set_memory_4k((unsigned long)buf, (1 << order))) {
  691. free_pages((unsigned long)buf, order);
  692. buf = NULL;
  693. }
  694. return buf;
  695. }
  696. /* allocates the memory where the IOMMU will log its events to */
  697. static int __init alloc_event_buffer(struct amd_iommu *iommu)
  698. {
  699. iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
  700. EVT_BUFFER_SIZE);
  701. return iommu->evt_buf ? 0 : -ENOMEM;
  702. }
  703. static void iommu_enable_event_buffer(struct amd_iommu *iommu)
  704. {
  705. u64 entry;
  706. BUG_ON(iommu->evt_buf == NULL);
  707. entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
  708. memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
  709. &entry, sizeof(entry));
  710. /* set head and tail to zero manually */
  711. writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  712. writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  713. iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
  714. }
  715. /*
  716. * This function disables the event log buffer
  717. */
  718. static void iommu_disable_event_buffer(struct amd_iommu *iommu)
  719. {
  720. iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
  721. }
  722. static void __init free_event_buffer(struct amd_iommu *iommu)
  723. {
  724. free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
  725. }
  726. /* allocates the memory where the IOMMU will log its events to */
  727. static int __init alloc_ppr_log(struct amd_iommu *iommu)
  728. {
  729. iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
  730. PPR_LOG_SIZE);
  731. return iommu->ppr_log ? 0 : -ENOMEM;
  732. }
  733. static void iommu_enable_ppr_log(struct amd_iommu *iommu)
  734. {
  735. u64 entry;
  736. if (iommu->ppr_log == NULL)
  737. return;
  738. entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
  739. memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
  740. &entry, sizeof(entry));
  741. /* set head and tail to zero manually */
  742. writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  743. writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  744. iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
  745. iommu_feature_enable(iommu, CONTROL_PPR_EN);
  746. }
  747. static void __init free_ppr_log(struct amd_iommu *iommu)
  748. {
  749. free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
  750. }
  751. static void free_ga_log(struct amd_iommu *iommu)
  752. {
  753. #ifdef CONFIG_IRQ_REMAP
  754. free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
  755. free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
  756. #endif
  757. }
  758. #ifdef CONFIG_IRQ_REMAP
  759. static int iommu_ga_log_enable(struct amd_iommu *iommu)
  760. {
  761. u32 status, i;
  762. u64 entry;
  763. if (!iommu->ga_log)
  764. return -EINVAL;
  765. entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
  766. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
  767. &entry, sizeof(entry));
  768. entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
  769. (BIT_ULL(52)-1)) & ~7ULL;
  770. memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
  771. &entry, sizeof(entry));
  772. writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  773. writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  774. iommu_feature_enable(iommu, CONTROL_GAINT_EN);
  775. iommu_feature_enable(iommu, CONTROL_GALOG_EN);
  776. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  777. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  778. if (status & (MMIO_STATUS_GALOG_RUN_MASK))
  779. break;
  780. udelay(10);
  781. }
  782. if (WARN_ON(i >= LOOP_TIMEOUT))
  783. return -EINVAL;
  784. return 0;
  785. }
  786. static int iommu_init_ga_log(struct amd_iommu *iommu)
  787. {
  788. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  789. return 0;
  790. iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  791. get_order(GA_LOG_SIZE));
  792. if (!iommu->ga_log)
  793. goto err_out;
  794. iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  795. get_order(8));
  796. if (!iommu->ga_log_tail)
  797. goto err_out;
  798. return 0;
  799. err_out:
  800. free_ga_log(iommu);
  801. return -EINVAL;
  802. }
  803. #endif /* CONFIG_IRQ_REMAP */
  804. static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
  805. {
  806. iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
  807. return iommu->cmd_sem ? 0 : -ENOMEM;
  808. }
  809. static void __init free_cwwb_sem(struct amd_iommu *iommu)
  810. {
  811. if (iommu->cmd_sem)
  812. free_page((unsigned long)iommu->cmd_sem);
  813. }
  814. static void iommu_enable_xt(struct amd_iommu *iommu)
  815. {
  816. #ifdef CONFIG_IRQ_REMAP
  817. /*
  818. * XT mode (32-bit APIC destination ID) requires
  819. * GA mode (128-bit IRTE support) as a prerequisite.
  820. */
  821. if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
  822. amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  823. iommu_feature_enable(iommu, CONTROL_XT_EN);
  824. #endif /* CONFIG_IRQ_REMAP */
  825. }
  826. static void iommu_enable_gt(struct amd_iommu *iommu)
  827. {
  828. if (!iommu_feature(iommu, FEATURE_GT))
  829. return;
  830. iommu_feature_enable(iommu, CONTROL_GT_EN);
  831. }
  832. /* sets a specific bit in the device table entry. */
  833. static void __set_dev_entry_bit(struct dev_table_entry *dev_table,
  834. u16 devid, u8 bit)
  835. {
  836. int i = (bit >> 6) & 0x03;
  837. int _bit = bit & 0x3f;
  838. dev_table[devid].data[i] |= (1UL << _bit);
  839. }
  840. static void set_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
  841. {
  842. struct dev_table_entry *dev_table = get_dev_table(iommu);
  843. return __set_dev_entry_bit(dev_table, devid, bit);
  844. }
  845. static int __get_dev_entry_bit(struct dev_table_entry *dev_table,
  846. u16 devid, u8 bit)
  847. {
  848. int i = (bit >> 6) & 0x03;
  849. int _bit = bit & 0x3f;
  850. return (dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
  851. }
  852. static int get_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
  853. {
  854. struct dev_table_entry *dev_table = get_dev_table(iommu);
  855. return __get_dev_entry_bit(dev_table, devid, bit);
  856. }
  857. static bool __copy_device_table(struct amd_iommu *iommu)
  858. {
  859. u64 int_ctl, int_tab_len, entry = 0;
  860. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  861. struct dev_table_entry *old_devtb = NULL;
  862. u32 lo, hi, devid, old_devtb_size;
  863. phys_addr_t old_devtb_phys;
  864. u16 dom_id, dte_v, irq_v;
  865. gfp_t gfp_flag;
  866. u64 tmp;
  867. /* Each IOMMU use separate device table with the same size */
  868. lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
  869. hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
  870. entry = (((u64) hi) << 32) + lo;
  871. old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
  872. if (old_devtb_size != pci_seg->dev_table_size) {
  873. pr_err("The device table size of IOMMU:%d is not expected!\n",
  874. iommu->index);
  875. return false;
  876. }
  877. /*
  878. * When SME is enabled in the first kernel, the entry includes the
  879. * memory encryption mask(sme_me_mask), we must remove the memory
  880. * encryption mask to obtain the true physical address in kdump kernel.
  881. */
  882. old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
  883. if (old_devtb_phys >= 0x100000000ULL) {
  884. pr_err("The address of old device table is above 4G, not trustworthy!\n");
  885. return false;
  886. }
  887. old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
  888. ? (__force void *)ioremap_encrypted(old_devtb_phys,
  889. pci_seg->dev_table_size)
  890. : memremap(old_devtb_phys, pci_seg->dev_table_size, MEMREMAP_WB);
  891. if (!old_devtb)
  892. return false;
  893. gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
  894. pci_seg->old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
  895. get_order(pci_seg->dev_table_size));
  896. if (pci_seg->old_dev_tbl_cpy == NULL) {
  897. pr_err("Failed to allocate memory for copying old device table!\n");
  898. memunmap(old_devtb);
  899. return false;
  900. }
  901. for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
  902. pci_seg->old_dev_tbl_cpy[devid] = old_devtb[devid];
  903. dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
  904. dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
  905. if (dte_v && dom_id) {
  906. pci_seg->old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
  907. pci_seg->old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
  908. __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
  909. /* If gcr3 table existed, mask it out */
  910. if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
  911. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  912. tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  913. pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;
  914. tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
  915. tmp |= DTE_FLAG_GV;
  916. pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;
  917. }
  918. }
  919. irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
  920. int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
  921. int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
  922. if (irq_v && (int_ctl || int_tab_len)) {
  923. if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
  924. (int_tab_len != DTE_INTTABLEN)) {
  925. pr_err("Wrong old irq remapping flag: %#x\n", devid);
  926. memunmap(old_devtb);
  927. return false;
  928. }
  929. pci_seg->old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
  930. }
  931. }
  932. memunmap(old_devtb);
  933. return true;
  934. }
  935. static bool copy_device_table(void)
  936. {
  937. struct amd_iommu *iommu;
  938. struct amd_iommu_pci_seg *pci_seg;
  939. if (!amd_iommu_pre_enabled)
  940. return false;
  941. pr_warn("Translation is already enabled - trying to copy translation structures\n");
  942. /*
  943. * All IOMMUs within PCI segment shares common device table.
  944. * Hence copy device table only once per PCI segment.
  945. */
  946. for_each_pci_segment(pci_seg) {
  947. for_each_iommu(iommu) {
  948. if (pci_seg->id != iommu->pci_seg->id)
  949. continue;
  950. if (!__copy_device_table(iommu))
  951. return false;
  952. break;
  953. }
  954. }
  955. return true;
  956. }
  957. void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid)
  958. {
  959. int sysmgt;
  960. sysmgt = get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1) |
  961. (get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2) << 1);
  962. if (sysmgt == 0x01)
  963. set_dev_entry_bit(iommu, devid, DEV_ENTRY_IW);
  964. }
  965. /*
  966. * This function takes the device specific flags read from the ACPI
  967. * table and sets up the device table entry with that information
  968. */
  969. static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
  970. u16 devid, u32 flags, u32 ext_flags)
  971. {
  972. if (flags & ACPI_DEVFLAG_INITPASS)
  973. set_dev_entry_bit(iommu, devid, DEV_ENTRY_INIT_PASS);
  974. if (flags & ACPI_DEVFLAG_EXTINT)
  975. set_dev_entry_bit(iommu, devid, DEV_ENTRY_EINT_PASS);
  976. if (flags & ACPI_DEVFLAG_NMI)
  977. set_dev_entry_bit(iommu, devid, DEV_ENTRY_NMI_PASS);
  978. if (flags & ACPI_DEVFLAG_SYSMGT1)
  979. set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1);
  980. if (flags & ACPI_DEVFLAG_SYSMGT2)
  981. set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2);
  982. if (flags & ACPI_DEVFLAG_LINT0)
  983. set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT0_PASS);
  984. if (flags & ACPI_DEVFLAG_LINT1)
  985. set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT1_PASS);
  986. amd_iommu_apply_erratum_63(iommu, devid);
  987. amd_iommu_set_rlookup_table(iommu, devid);
  988. }
  989. int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)
  990. {
  991. struct devid_map *entry;
  992. struct list_head *list;
  993. if (type == IVHD_SPECIAL_IOAPIC)
  994. list = &ioapic_map;
  995. else if (type == IVHD_SPECIAL_HPET)
  996. list = &hpet_map;
  997. else
  998. return -EINVAL;
  999. list_for_each_entry(entry, list, list) {
  1000. if (!(entry->id == id && entry->cmd_line))
  1001. continue;
  1002. pr_info("Command-line override present for %s id %d - ignoring\n",
  1003. type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
  1004. *devid = entry->devid;
  1005. return 0;
  1006. }
  1007. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1008. if (!entry)
  1009. return -ENOMEM;
  1010. entry->id = id;
  1011. entry->devid = *devid;
  1012. entry->cmd_line = cmd_line;
  1013. list_add_tail(&entry->list, list);
  1014. return 0;
  1015. }
  1016. static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
  1017. bool cmd_line)
  1018. {
  1019. struct acpihid_map_entry *entry;
  1020. struct list_head *list = &acpihid_map;
  1021. list_for_each_entry(entry, list, list) {
  1022. if (strcmp(entry->hid, hid) ||
  1023. (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
  1024. !entry->cmd_line)
  1025. continue;
  1026. pr_info("Command-line override for hid:%s uid:%s\n",
  1027. hid, uid);
  1028. *devid = entry->devid;
  1029. return 0;
  1030. }
  1031. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1032. if (!entry)
  1033. return -ENOMEM;
  1034. memcpy(entry->uid, uid, strlen(uid));
  1035. memcpy(entry->hid, hid, strlen(hid));
  1036. entry->devid = *devid;
  1037. entry->cmd_line = cmd_line;
  1038. entry->root_devid = (entry->devid & (~0x7));
  1039. pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
  1040. entry->cmd_line ? "cmd" : "ivrs",
  1041. entry->hid, entry->uid, entry->root_devid);
  1042. list_add_tail(&entry->list, list);
  1043. return 0;
  1044. }
  1045. static int __init add_early_maps(void)
  1046. {
  1047. int i, ret;
  1048. for (i = 0; i < early_ioapic_map_size; ++i) {
  1049. ret = add_special_device(IVHD_SPECIAL_IOAPIC,
  1050. early_ioapic_map[i].id,
  1051. &early_ioapic_map[i].devid,
  1052. early_ioapic_map[i].cmd_line);
  1053. if (ret)
  1054. return ret;
  1055. }
  1056. for (i = 0; i < early_hpet_map_size; ++i) {
  1057. ret = add_special_device(IVHD_SPECIAL_HPET,
  1058. early_hpet_map[i].id,
  1059. &early_hpet_map[i].devid,
  1060. early_hpet_map[i].cmd_line);
  1061. if (ret)
  1062. return ret;
  1063. }
  1064. for (i = 0; i < early_acpihid_map_size; ++i) {
  1065. ret = add_acpi_hid_device(early_acpihid_map[i].hid,
  1066. early_acpihid_map[i].uid,
  1067. &early_acpihid_map[i].devid,
  1068. early_acpihid_map[i].cmd_line);
  1069. if (ret)
  1070. return ret;
  1071. }
  1072. return 0;
  1073. }
  1074. /*
  1075. * Takes a pointer to an AMD IOMMU entry in the ACPI table and
  1076. * initializes the hardware and our data structures with it.
  1077. */
  1078. static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
  1079. struct ivhd_header *h)
  1080. {
  1081. u8 *p = (u8 *)h;
  1082. u8 *end = p, flags = 0;
  1083. u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;
  1084. u32 dev_i, ext_flags = 0;
  1085. bool alias = false;
  1086. struct ivhd_entry *e;
  1087. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  1088. u32 ivhd_size;
  1089. int ret;
  1090. ret = add_early_maps();
  1091. if (ret)
  1092. return ret;
  1093. amd_iommu_apply_ivrs_quirks();
  1094. /*
  1095. * First save the recommended feature enable bits from ACPI
  1096. */
  1097. iommu->acpi_flags = h->flags;
  1098. /*
  1099. * Done. Now parse the device entries
  1100. */
  1101. ivhd_size = get_ivhd_header_size(h);
  1102. if (!ivhd_size) {
  1103. pr_err("Unsupported IVHD type %#x\n", h->type);
  1104. return -EINVAL;
  1105. }
  1106. p += ivhd_size;
  1107. end += h->length;
  1108. while (p < end) {
  1109. e = (struct ivhd_entry *)p;
  1110. seg_id = pci_seg->id;
  1111. switch (e->type) {
  1112. case IVHD_DEV_ALL:
  1113. DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
  1114. for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)
  1115. set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
  1116. break;
  1117. case IVHD_DEV_SELECT:
  1118. DUMP_printk(" DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "
  1119. "flags: %02x\n",
  1120. seg_id, PCI_BUS_NUM(e->devid),
  1121. PCI_SLOT(e->devid),
  1122. PCI_FUNC(e->devid),
  1123. e->flags);
  1124. devid = e->devid;
  1125. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1126. break;
  1127. case IVHD_DEV_SELECT_RANGE_START:
  1128. DUMP_printk(" DEV_SELECT_RANGE_START\t "
  1129. "devid: %04x:%02x:%02x.%x flags: %02x\n",
  1130. seg_id, PCI_BUS_NUM(e->devid),
  1131. PCI_SLOT(e->devid),
  1132. PCI_FUNC(e->devid),
  1133. e->flags);
  1134. devid_start = e->devid;
  1135. flags = e->flags;
  1136. ext_flags = 0;
  1137. alias = false;
  1138. break;
  1139. case IVHD_DEV_ALIAS:
  1140. DUMP_printk(" DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "
  1141. "flags: %02x devid_to: %02x:%02x.%x\n",
  1142. seg_id, PCI_BUS_NUM(e->devid),
  1143. PCI_SLOT(e->devid),
  1144. PCI_FUNC(e->devid),
  1145. e->flags,
  1146. PCI_BUS_NUM(e->ext >> 8),
  1147. PCI_SLOT(e->ext >> 8),
  1148. PCI_FUNC(e->ext >> 8));
  1149. devid = e->devid;
  1150. devid_to = e->ext >> 8;
  1151. set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
  1152. set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
  1153. pci_seg->alias_table[devid] = devid_to;
  1154. break;
  1155. case IVHD_DEV_ALIAS_RANGE:
  1156. DUMP_printk(" DEV_ALIAS_RANGE\t\t "
  1157. "devid: %04x:%02x:%02x.%x flags: %02x "
  1158. "devid_to: %04x:%02x:%02x.%x\n",
  1159. seg_id, PCI_BUS_NUM(e->devid),
  1160. PCI_SLOT(e->devid),
  1161. PCI_FUNC(e->devid),
  1162. e->flags,
  1163. seg_id, PCI_BUS_NUM(e->ext >> 8),
  1164. PCI_SLOT(e->ext >> 8),
  1165. PCI_FUNC(e->ext >> 8));
  1166. devid_start = e->devid;
  1167. flags = e->flags;
  1168. devid_to = e->ext >> 8;
  1169. ext_flags = 0;
  1170. alias = true;
  1171. break;
  1172. case IVHD_DEV_EXT_SELECT:
  1173. DUMP_printk(" DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "
  1174. "flags: %02x ext: %08x\n",
  1175. seg_id, PCI_BUS_NUM(e->devid),
  1176. PCI_SLOT(e->devid),
  1177. PCI_FUNC(e->devid),
  1178. e->flags, e->ext);
  1179. devid = e->devid;
  1180. set_dev_entry_from_acpi(iommu, devid, e->flags,
  1181. e->ext);
  1182. break;
  1183. case IVHD_DEV_EXT_SELECT_RANGE:
  1184. DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
  1185. "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",
  1186. seg_id, PCI_BUS_NUM(e->devid),
  1187. PCI_SLOT(e->devid),
  1188. PCI_FUNC(e->devid),
  1189. e->flags, e->ext);
  1190. devid_start = e->devid;
  1191. flags = e->flags;
  1192. ext_flags = e->ext;
  1193. alias = false;
  1194. break;
  1195. case IVHD_DEV_RANGE_END:
  1196. DUMP_printk(" DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",
  1197. seg_id, PCI_BUS_NUM(e->devid),
  1198. PCI_SLOT(e->devid),
  1199. PCI_FUNC(e->devid));
  1200. devid = e->devid;
  1201. for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
  1202. if (alias) {
  1203. pci_seg->alias_table[dev_i] = devid_to;
  1204. set_dev_entry_from_acpi(iommu,
  1205. devid_to, flags, ext_flags);
  1206. }
  1207. set_dev_entry_from_acpi(iommu, dev_i,
  1208. flags, ext_flags);
  1209. }
  1210. break;
  1211. case IVHD_DEV_SPECIAL: {
  1212. u8 handle, type;
  1213. const char *var;
  1214. u32 devid;
  1215. int ret;
  1216. handle = e->ext & 0xff;
  1217. devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));
  1218. type = (e->ext >> 24) & 0xff;
  1219. if (type == IVHD_SPECIAL_IOAPIC)
  1220. var = "IOAPIC";
  1221. else if (type == IVHD_SPECIAL_HPET)
  1222. var = "HPET";
  1223. else
  1224. var = "UNKNOWN";
  1225. DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",
  1226. var, (int)handle,
  1227. seg_id, PCI_BUS_NUM(devid),
  1228. PCI_SLOT(devid),
  1229. PCI_FUNC(devid));
  1230. ret = add_special_device(type, handle, &devid, false);
  1231. if (ret)
  1232. return ret;
  1233. /*
  1234. * add_special_device might update the devid in case a
  1235. * command-line override is present. So call
  1236. * set_dev_entry_from_acpi after add_special_device.
  1237. */
  1238. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1239. break;
  1240. }
  1241. case IVHD_DEV_ACPI_HID: {
  1242. u32 devid;
  1243. u8 hid[ACPIHID_HID_LEN];
  1244. u8 uid[ACPIHID_UID_LEN];
  1245. int ret;
  1246. if (h->type != 0x40) {
  1247. pr_err(FW_BUG "Invalid IVHD device type %#x\n",
  1248. e->type);
  1249. break;
  1250. }
  1251. BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
  1252. memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
  1253. hid[ACPIHID_HID_LEN - 1] = '\0';
  1254. if (!(*hid)) {
  1255. pr_err(FW_BUG "Invalid HID.\n");
  1256. break;
  1257. }
  1258. uid[0] = '\0';
  1259. switch (e->uidf) {
  1260. case UID_NOT_PRESENT:
  1261. if (e->uidl != 0)
  1262. pr_warn(FW_BUG "Invalid UID length.\n");
  1263. break;
  1264. case UID_IS_INTEGER:
  1265. sprintf(uid, "%d", e->uid);
  1266. break;
  1267. case UID_IS_CHARACTER:
  1268. memcpy(uid, &e->uid, e->uidl);
  1269. uid[e->uidl] = '\0';
  1270. break;
  1271. default:
  1272. break;
  1273. }
  1274. devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
  1275. DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",
  1276. hid, uid, seg_id,
  1277. PCI_BUS_NUM(devid),
  1278. PCI_SLOT(devid),
  1279. PCI_FUNC(devid));
  1280. flags = e->flags;
  1281. ret = add_acpi_hid_device(hid, uid, &devid, false);
  1282. if (ret)
  1283. return ret;
  1284. /*
  1285. * add_special_device might update the devid in case a
  1286. * command-line override is present. So call
  1287. * set_dev_entry_from_acpi after add_special_device.
  1288. */
  1289. set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
  1290. break;
  1291. }
  1292. default:
  1293. break;
  1294. }
  1295. p += ivhd_entry_length(p);
  1296. }
  1297. return 0;
  1298. }
  1299. /* Allocate PCI segment data structure */
  1300. static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
  1301. struct acpi_table_header *ivrs_base)
  1302. {
  1303. struct amd_iommu_pci_seg *pci_seg;
  1304. int last_bdf;
  1305. /*
  1306. * First parse ACPI tables to find the largest Bus/Dev/Func we need to
  1307. * handle in this PCI segment. Upon this information the shared data
  1308. * structures for the PCI segments in the system will be allocated.
  1309. */
  1310. last_bdf = find_last_devid_acpi(ivrs_base, id);
  1311. if (last_bdf < 0)
  1312. return NULL;
  1313. pci_seg = kzalloc(sizeof(struct amd_iommu_pci_seg), GFP_KERNEL);
  1314. if (pci_seg == NULL)
  1315. return NULL;
  1316. pci_seg->last_bdf = last_bdf;
  1317. DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
  1318. pci_seg->dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);
  1319. pci_seg->alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);
  1320. pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);
  1321. pci_seg->id = id;
  1322. init_llist_head(&pci_seg->dev_data_list);
  1323. INIT_LIST_HEAD(&pci_seg->unity_map);
  1324. list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
  1325. if (alloc_dev_table(pci_seg))
  1326. return NULL;
  1327. if (alloc_alias_table(pci_seg))
  1328. return NULL;
  1329. if (alloc_rlookup_table(pci_seg))
  1330. return NULL;
  1331. return pci_seg;
  1332. }
  1333. static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,
  1334. struct acpi_table_header *ivrs_base)
  1335. {
  1336. struct amd_iommu_pci_seg *pci_seg;
  1337. for_each_pci_segment(pci_seg) {
  1338. if (pci_seg->id == id)
  1339. return pci_seg;
  1340. }
  1341. return alloc_pci_segment(id, ivrs_base);
  1342. }
  1343. static void __init free_pci_segments(void)
  1344. {
  1345. struct amd_iommu_pci_seg *pci_seg, *next;
  1346. for_each_pci_segment_safe(pci_seg, next) {
  1347. list_del(&pci_seg->list);
  1348. free_irq_lookup_table(pci_seg);
  1349. free_rlookup_table(pci_seg);
  1350. free_alias_table(pci_seg);
  1351. free_dev_table(pci_seg);
  1352. kfree(pci_seg);
  1353. }
  1354. }
  1355. static void __init free_iommu_one(struct amd_iommu *iommu)
  1356. {
  1357. free_cwwb_sem(iommu);
  1358. free_command_buffer(iommu);
  1359. free_event_buffer(iommu);
  1360. free_ppr_log(iommu);
  1361. free_ga_log(iommu);
  1362. iommu_unmap_mmio_space(iommu);
  1363. }
  1364. static void __init free_iommu_all(void)
  1365. {
  1366. struct amd_iommu *iommu, *next;
  1367. for_each_iommu_safe(iommu, next) {
  1368. list_del(&iommu->list);
  1369. free_iommu_one(iommu);
  1370. kfree(iommu);
  1371. }
  1372. }
  1373. /*
  1374. * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
  1375. * Workaround:
  1376. * BIOS should disable L2B micellaneous clock gating by setting
  1377. * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
  1378. */
  1379. static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
  1380. {
  1381. u32 value;
  1382. if ((boot_cpu_data.x86 != 0x15) ||
  1383. (boot_cpu_data.x86_model < 0x10) ||
  1384. (boot_cpu_data.x86_model > 0x1f))
  1385. return;
  1386. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1387. pci_read_config_dword(iommu->dev, 0xf4, &value);
  1388. if (value & BIT(2))
  1389. return;
  1390. /* Select NB indirect register 0x90 and enable writing */
  1391. pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
  1392. pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
  1393. pci_info(iommu->dev, "Applying erratum 746 workaround\n");
  1394. /* Clear the enable writing bit */
  1395. pci_write_config_dword(iommu->dev, 0xf0, 0x90);
  1396. }
  1397. /*
  1398. * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
  1399. * Workaround:
  1400. * BIOS should enable ATS write permission check by setting
  1401. * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
  1402. */
  1403. static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
  1404. {
  1405. u32 value;
  1406. if ((boot_cpu_data.x86 != 0x15) ||
  1407. (boot_cpu_data.x86_model < 0x30) ||
  1408. (boot_cpu_data.x86_model > 0x3f))
  1409. return;
  1410. /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
  1411. value = iommu_read_l2(iommu, 0x47);
  1412. if (value & BIT(0))
  1413. return;
  1414. /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
  1415. iommu_write_l2(iommu, 0x47, value | BIT(0));
  1416. pci_info(iommu->dev, "Applying ATS write check workaround\n");
  1417. }
  1418. /*
  1419. * This function glues the initialization function for one IOMMU
  1420. * together and also allocates the command buffer and programs the
  1421. * hardware. It does NOT enable the IOMMU. This is done afterwards.
  1422. */
  1423. static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
  1424. struct acpi_table_header *ivrs_base)
  1425. {
  1426. struct amd_iommu_pci_seg *pci_seg;
  1427. pci_seg = get_pci_segment(h->pci_seg, ivrs_base);
  1428. if (pci_seg == NULL)
  1429. return -ENOMEM;
  1430. iommu->pci_seg = pci_seg;
  1431. raw_spin_lock_init(&iommu->lock);
  1432. iommu->cmd_sem_val = 0;
  1433. /* Add IOMMU to internal data structures */
  1434. list_add_tail(&iommu->list, &amd_iommu_list);
  1435. iommu->index = amd_iommus_present++;
  1436. if (unlikely(iommu->index >= MAX_IOMMUS)) {
  1437. WARN(1, "System has more IOMMUs than supported by this driver\n");
  1438. return -ENOSYS;
  1439. }
  1440. /* Index is fine - add IOMMU to the array */
  1441. amd_iommus[iommu->index] = iommu;
  1442. /*
  1443. * Copy data from ACPI table entry to the iommu struct
  1444. */
  1445. iommu->devid = h->devid;
  1446. iommu->cap_ptr = h->cap_ptr;
  1447. iommu->mmio_phys = h->mmio_phys;
  1448. switch (h->type) {
  1449. case 0x10:
  1450. /* Check if IVHD EFR contains proper max banks/counters */
  1451. if ((h->efr_attr != 0) &&
  1452. ((h->efr_attr & (0xF << 13)) != 0) &&
  1453. ((h->efr_attr & (0x3F << 17)) != 0))
  1454. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1455. else
  1456. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1457. /*
  1458. * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
  1459. * GAM also requires GA mode. Therefore, we need to
  1460. * check cmpxchg16b support before enabling it.
  1461. */
  1462. if (!boot_cpu_has(X86_FEATURE_CX16) ||
  1463. ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
  1464. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1465. break;
  1466. case 0x11:
  1467. case 0x40:
  1468. if (h->efr_reg & (1 << 9))
  1469. iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
  1470. else
  1471. iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
  1472. /*
  1473. * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
  1474. * XT, GAM also requires GA mode. Therefore, we need to
  1475. * check cmpxchg16b support before enabling them.
  1476. */
  1477. if (!boot_cpu_has(X86_FEATURE_CX16) ||
  1478. ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
  1479. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
  1480. break;
  1481. }
  1482. if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
  1483. amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
  1484. early_iommu_features_init(iommu, h);
  1485. break;
  1486. default:
  1487. return -EINVAL;
  1488. }
  1489. iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
  1490. iommu->mmio_phys_end);
  1491. if (!iommu->mmio_base)
  1492. return -ENOMEM;
  1493. return init_iommu_from_acpi(iommu, h);
  1494. }
  1495. static int __init init_iommu_one_late(struct amd_iommu *iommu)
  1496. {
  1497. int ret;
  1498. if (alloc_cwwb_sem(iommu))
  1499. return -ENOMEM;
  1500. if (alloc_command_buffer(iommu))
  1501. return -ENOMEM;
  1502. if (alloc_event_buffer(iommu))
  1503. return -ENOMEM;
  1504. iommu->int_enabled = false;
  1505. init_translation_status(iommu);
  1506. if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
  1507. iommu_disable(iommu);
  1508. clear_translation_pre_enabled(iommu);
  1509. pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
  1510. iommu->index);
  1511. }
  1512. if (amd_iommu_pre_enabled)
  1513. amd_iommu_pre_enabled = translation_pre_enabled(iommu);
  1514. if (amd_iommu_irq_remap) {
  1515. ret = amd_iommu_create_irq_domain(iommu);
  1516. if (ret)
  1517. return ret;
  1518. }
  1519. /*
  1520. * Make sure IOMMU is not considered to translate itself. The IVRS
  1521. * table tells us so, but this is a lie!
  1522. */
  1523. iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
  1524. return 0;
  1525. }
  1526. /**
  1527. * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
  1528. * @ivrs: Pointer to the IVRS header
  1529. *
  1530. * This function search through all IVDB of the maximum supported IVHD
  1531. */
  1532. static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
  1533. {
  1534. u8 *base = (u8 *)ivrs;
  1535. struct ivhd_header *ivhd = (struct ivhd_header *)
  1536. (base + IVRS_HEADER_LENGTH);
  1537. u8 last_type = ivhd->type;
  1538. u16 devid = ivhd->devid;
  1539. while (((u8 *)ivhd - base < ivrs->length) &&
  1540. (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
  1541. u8 *p = (u8 *) ivhd;
  1542. if (ivhd->devid == devid)
  1543. last_type = ivhd->type;
  1544. ivhd = (struct ivhd_header *)(p + ivhd->length);
  1545. }
  1546. return last_type;
  1547. }
  1548. /*
  1549. * Iterates over all IOMMU entries in the ACPI table, allocates the
  1550. * IOMMU structure and initializes it with init_iommu_one()
  1551. */
  1552. static int __init init_iommu_all(struct acpi_table_header *table)
  1553. {
  1554. u8 *p = (u8 *)table, *end = (u8 *)table;
  1555. struct ivhd_header *h;
  1556. struct amd_iommu *iommu;
  1557. int ret;
  1558. end += table->length;
  1559. p += IVRS_HEADER_LENGTH;
  1560. /* Phase 1: Process all IVHD blocks */
  1561. while (p < end) {
  1562. h = (struct ivhd_header *)p;
  1563. if (*p == amd_iommu_target_ivhd_type) {
  1564. DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "
  1565. "flags: %01x info %04x\n",
  1566. h->pci_seg, PCI_BUS_NUM(h->devid),
  1567. PCI_SLOT(h->devid), PCI_FUNC(h->devid),
  1568. h->cap_ptr, h->flags, h->info);
  1569. DUMP_printk(" mmio-addr: %016llx\n",
  1570. h->mmio_phys);
  1571. iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
  1572. if (iommu == NULL)
  1573. return -ENOMEM;
  1574. ret = init_iommu_one(iommu, h, table);
  1575. if (ret)
  1576. return ret;
  1577. }
  1578. p += h->length;
  1579. }
  1580. WARN_ON(p != end);
  1581. /* Phase 2 : Early feature support check */
  1582. get_global_efr();
  1583. /* Phase 3 : Enabling IOMMU features */
  1584. for_each_iommu(iommu) {
  1585. ret = init_iommu_one_late(iommu);
  1586. if (ret)
  1587. return ret;
  1588. }
  1589. return 0;
  1590. }
  1591. static void init_iommu_perf_ctr(struct amd_iommu *iommu)
  1592. {
  1593. u64 val;
  1594. struct pci_dev *pdev = iommu->dev;
  1595. if (!iommu_feature(iommu, FEATURE_PC))
  1596. return;
  1597. amd_iommu_pc_present = true;
  1598. pci_info(pdev, "IOMMU performance counters supported\n");
  1599. val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
  1600. iommu->max_banks = (u8) ((val >> 12) & 0x3f);
  1601. iommu->max_counters = (u8) ((val >> 7) & 0xf);
  1602. return;
  1603. }
  1604. static ssize_t amd_iommu_show_cap(struct device *dev,
  1605. struct device_attribute *attr,
  1606. char *buf)
  1607. {
  1608. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1609. return sprintf(buf, "%x\n", iommu->cap);
  1610. }
  1611. static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
  1612. static ssize_t amd_iommu_show_features(struct device *dev,
  1613. struct device_attribute *attr,
  1614. char *buf)
  1615. {
  1616. struct amd_iommu *iommu = dev_to_amd_iommu(dev);
  1617. return sprintf(buf, "%llx:%llx\n", iommu->features2, iommu->features);
  1618. }
  1619. static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
  1620. static struct attribute *amd_iommu_attrs[] = {
  1621. &dev_attr_cap.attr,
  1622. &dev_attr_features.attr,
  1623. NULL,
  1624. };
  1625. static struct attribute_group amd_iommu_group = {
  1626. .name = "amd-iommu",
  1627. .attrs = amd_iommu_attrs,
  1628. };
  1629. static const struct attribute_group *amd_iommu_groups[] = {
  1630. &amd_iommu_group,
  1631. NULL,
  1632. };
  1633. /*
  1634. * Note: IVHD 0x11 and 0x40 also contains exact copy
  1635. * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
  1636. * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
  1637. */
  1638. static void __init late_iommu_features_init(struct amd_iommu *iommu)
  1639. {
  1640. u64 features, features2;
  1641. if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
  1642. return;
  1643. /* read extended feature bits */
  1644. features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
  1645. features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
  1646. if (!iommu->features) {
  1647. iommu->features = features;
  1648. iommu->features2 = features2;
  1649. return;
  1650. }
  1651. /*
  1652. * Sanity check and warn if EFR values from
  1653. * IVHD and MMIO conflict.
  1654. */
  1655. if (features != iommu->features ||
  1656. features2 != iommu->features2) {
  1657. pr_warn(FW_WARN
  1658. "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
  1659. features, iommu->features,
  1660. features2, iommu->features2);
  1661. }
  1662. }
  1663. static int __init iommu_init_pci(struct amd_iommu *iommu)
  1664. {
  1665. int cap_ptr = iommu->cap_ptr;
  1666. int ret;
  1667. iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
  1668. PCI_BUS_NUM(iommu->devid),
  1669. iommu->devid & 0xff);
  1670. if (!iommu->dev)
  1671. return -ENODEV;
  1672. /* Prevent binding other PCI device drivers to IOMMU devices */
  1673. iommu->dev->match_driver = false;
  1674. pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
  1675. &iommu->cap);
  1676. if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
  1677. amd_iommu_iotlb_sup = false;
  1678. late_iommu_features_init(iommu);
  1679. if (iommu_feature(iommu, FEATURE_GT)) {
  1680. int glxval;
  1681. u32 max_pasid;
  1682. u64 pasmax;
  1683. pasmax = iommu->features & FEATURE_PASID_MASK;
  1684. pasmax >>= FEATURE_PASID_SHIFT;
  1685. max_pasid = (1 << (pasmax + 1)) - 1;
  1686. amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
  1687. BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
  1688. glxval = iommu->features & FEATURE_GLXVAL_MASK;
  1689. glxval >>= FEATURE_GLXVAL_SHIFT;
  1690. if (amd_iommu_max_glx_val == -1)
  1691. amd_iommu_max_glx_val = glxval;
  1692. else
  1693. amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
  1694. }
  1695. if (iommu_feature(iommu, FEATURE_GT) &&
  1696. iommu_feature(iommu, FEATURE_PPR)) {
  1697. iommu->is_iommu_v2 = true;
  1698. amd_iommu_v2_present = true;
  1699. }
  1700. if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
  1701. return -ENOMEM;
  1702. if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
  1703. pr_info("Using strict mode due to virtualization\n");
  1704. iommu_set_dma_strict();
  1705. amd_iommu_np_cache = true;
  1706. }
  1707. init_iommu_perf_ctr(iommu);
  1708. if (amd_iommu_pgtable == AMD_IOMMU_V2) {
  1709. if (!iommu_feature(iommu, FEATURE_GIOSUP) ||
  1710. !iommu_feature(iommu, FEATURE_GT)) {
  1711. pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
  1712. amd_iommu_pgtable = AMD_IOMMU_V1;
  1713. } else if (iommu_default_passthrough()) {
  1714. pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n");
  1715. amd_iommu_pgtable = AMD_IOMMU_V1;
  1716. }
  1717. }
  1718. if (is_rd890_iommu(iommu->dev)) {
  1719. int i, j;
  1720. iommu->root_pdev =
  1721. pci_get_domain_bus_and_slot(iommu->pci_seg->id,
  1722. iommu->dev->bus->number,
  1723. PCI_DEVFN(0, 0));
  1724. /*
  1725. * Some rd890 systems may not be fully reconfigured by the
  1726. * BIOS, so it's necessary for us to store this information so
  1727. * it can be reprogrammed on resume
  1728. */
  1729. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
  1730. &iommu->stored_addr_lo);
  1731. pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
  1732. &iommu->stored_addr_hi);
  1733. /* Low bit locks writes to configuration space */
  1734. iommu->stored_addr_lo &= ~1;
  1735. for (i = 0; i < 6; i++)
  1736. for (j = 0; j < 0x12; j++)
  1737. iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
  1738. for (i = 0; i < 0x83; i++)
  1739. iommu->stored_l2[i] = iommu_read_l2(iommu, i);
  1740. }
  1741. amd_iommu_erratum_746_workaround(iommu);
  1742. amd_iommu_ats_write_check_workaround(iommu);
  1743. ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
  1744. amd_iommu_groups, "ivhd%d", iommu->index);
  1745. if (ret)
  1746. return ret;
  1747. iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
  1748. return pci_enable_device(iommu->dev);
  1749. }
  1750. static void print_iommu_info(void)
  1751. {
  1752. static const char * const feat_str[] = {
  1753. "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
  1754. "IA", "GA", "HE", "PC"
  1755. };
  1756. struct amd_iommu *iommu;
  1757. for_each_iommu(iommu) {
  1758. struct pci_dev *pdev = iommu->dev;
  1759. int i;
  1760. pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
  1761. if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
  1762. pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);
  1763. for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
  1764. if (iommu_feature(iommu, (1ULL << i)))
  1765. pr_cont(" %s", feat_str[i]);
  1766. }
  1767. if (iommu->features & FEATURE_GAM_VAPIC)
  1768. pr_cont(" GA_vAPIC");
  1769. if (iommu->features & FEATURE_SNP)
  1770. pr_cont(" SNP");
  1771. pr_cont("\n");
  1772. }
  1773. }
  1774. if (irq_remapping_enabled) {
  1775. pr_info("Interrupt remapping enabled\n");
  1776. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1777. pr_info("X2APIC enabled\n");
  1778. }
  1779. if (amd_iommu_pgtable == AMD_IOMMU_V2)
  1780. pr_info("V2 page table enabled\n");
  1781. }
  1782. static int __init amd_iommu_init_pci(void)
  1783. {
  1784. struct amd_iommu *iommu;
  1785. struct amd_iommu_pci_seg *pci_seg;
  1786. int ret;
  1787. for_each_iommu(iommu) {
  1788. ret = iommu_init_pci(iommu);
  1789. if (ret) {
  1790. pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
  1791. iommu->index, ret);
  1792. goto out;
  1793. }
  1794. /* Need to setup range after PCI init */
  1795. iommu_set_cwwb_range(iommu);
  1796. }
  1797. /*
  1798. * Order is important here to make sure any unity map requirements are
  1799. * fulfilled. The unity mappings are created and written to the device
  1800. * table during the iommu_init_pci() call.
  1801. *
  1802. * After that we call init_device_table_dma() to make sure any
  1803. * uninitialized DTE will block DMA, and in the end we flush the caches
  1804. * of all IOMMUs to make sure the changes to the device table are
  1805. * active.
  1806. */
  1807. for_each_pci_segment(pci_seg)
  1808. init_device_table_dma(pci_seg);
  1809. for_each_iommu(iommu)
  1810. iommu_flush_all_caches(iommu);
  1811. print_iommu_info();
  1812. out:
  1813. return ret;
  1814. }
  1815. /****************************************************************************
  1816. *
  1817. * The following functions initialize the MSI interrupts for all IOMMUs
  1818. * in the system. It's a bit challenging because there could be multiple
  1819. * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
  1820. * pci_dev.
  1821. *
  1822. ****************************************************************************/
  1823. static int iommu_setup_msi(struct amd_iommu *iommu)
  1824. {
  1825. int r;
  1826. r = pci_enable_msi(iommu->dev);
  1827. if (r)
  1828. return r;
  1829. r = request_threaded_irq(iommu->dev->irq,
  1830. amd_iommu_int_handler,
  1831. amd_iommu_int_thread,
  1832. 0, "AMD-Vi",
  1833. iommu);
  1834. if (r) {
  1835. pci_disable_msi(iommu->dev);
  1836. return r;
  1837. }
  1838. return 0;
  1839. }
  1840. union intcapxt {
  1841. u64 capxt;
  1842. struct {
  1843. u64 reserved_0 : 2,
  1844. dest_mode_logical : 1,
  1845. reserved_1 : 5,
  1846. destid_0_23 : 24,
  1847. vector : 8,
  1848. reserved_2 : 16,
  1849. destid_24_31 : 8;
  1850. };
  1851. } __attribute__ ((packed));
  1852. static struct irq_chip intcapxt_controller;
  1853. static int intcapxt_irqdomain_activate(struct irq_domain *domain,
  1854. struct irq_data *irqd, bool reserve)
  1855. {
  1856. return 0;
  1857. }
  1858. static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
  1859. struct irq_data *irqd)
  1860. {
  1861. }
  1862. static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
  1863. unsigned int nr_irqs, void *arg)
  1864. {
  1865. struct irq_alloc_info *info = arg;
  1866. int i, ret;
  1867. if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
  1868. return -EINVAL;
  1869. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  1870. if (ret < 0)
  1871. return ret;
  1872. for (i = virq; i < virq + nr_irqs; i++) {
  1873. struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
  1874. irqd->chip = &intcapxt_controller;
  1875. irqd->chip_data = info->data;
  1876. __irq_set_handler(i, handle_edge_irq, 0, "edge");
  1877. }
  1878. return ret;
  1879. }
  1880. static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
  1881. unsigned int nr_irqs)
  1882. {
  1883. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  1884. }
  1885. static void intcapxt_unmask_irq(struct irq_data *irqd)
  1886. {
  1887. struct amd_iommu *iommu = irqd->chip_data;
  1888. struct irq_cfg *cfg = irqd_cfg(irqd);
  1889. union intcapxt xt;
  1890. xt.capxt = 0ULL;
  1891. xt.dest_mode_logical = apic->dest_mode_logical;
  1892. xt.vector = cfg->vector;
  1893. xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
  1894. xt.destid_24_31 = cfg->dest_apicid >> 24;
  1895. /**
  1896. * Current IOMMU implementation uses the same IRQ for all
  1897. * 3 IOMMU interrupts.
  1898. */
  1899. writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
  1900. writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
  1901. writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
  1902. }
  1903. static void intcapxt_mask_irq(struct irq_data *irqd)
  1904. {
  1905. struct amd_iommu *iommu = irqd->chip_data;
  1906. writeq(0, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
  1907. writeq(0, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
  1908. writeq(0, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
  1909. }
  1910. static int intcapxt_set_affinity(struct irq_data *irqd,
  1911. const struct cpumask *mask, bool force)
  1912. {
  1913. struct irq_data *parent = irqd->parent_data;
  1914. int ret;
  1915. ret = parent->chip->irq_set_affinity(parent, mask, force);
  1916. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  1917. return ret;
  1918. return 0;
  1919. }
  1920. static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
  1921. {
  1922. return on ? -EOPNOTSUPP : 0;
  1923. }
  1924. static struct irq_chip intcapxt_controller = {
  1925. .name = "IOMMU-MSI",
  1926. .irq_unmask = intcapxt_unmask_irq,
  1927. .irq_mask = intcapxt_mask_irq,
  1928. .irq_ack = irq_chip_ack_parent,
  1929. .irq_retrigger = irq_chip_retrigger_hierarchy,
  1930. .irq_set_affinity = intcapxt_set_affinity,
  1931. .irq_set_wake = intcapxt_set_wake,
  1932. .flags = IRQCHIP_MASK_ON_SUSPEND,
  1933. };
  1934. static const struct irq_domain_ops intcapxt_domain_ops = {
  1935. .alloc = intcapxt_irqdomain_alloc,
  1936. .free = intcapxt_irqdomain_free,
  1937. .activate = intcapxt_irqdomain_activate,
  1938. .deactivate = intcapxt_irqdomain_deactivate,
  1939. };
  1940. static struct irq_domain *iommu_irqdomain;
  1941. static struct irq_domain *iommu_get_irqdomain(void)
  1942. {
  1943. struct fwnode_handle *fn;
  1944. /* No need for locking here (yet) as the init is single-threaded */
  1945. if (iommu_irqdomain)
  1946. return iommu_irqdomain;
  1947. fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
  1948. if (!fn)
  1949. return NULL;
  1950. iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
  1951. fn, &intcapxt_domain_ops,
  1952. NULL);
  1953. if (!iommu_irqdomain)
  1954. irq_domain_free_fwnode(fn);
  1955. return iommu_irqdomain;
  1956. }
  1957. static int iommu_setup_intcapxt(struct amd_iommu *iommu)
  1958. {
  1959. struct irq_domain *domain;
  1960. struct irq_alloc_info info;
  1961. int irq, ret;
  1962. domain = iommu_get_irqdomain();
  1963. if (!domain)
  1964. return -ENXIO;
  1965. init_irq_alloc_info(&info, NULL);
  1966. info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
  1967. info.data = iommu;
  1968. irq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
  1969. if (irq < 0) {
  1970. irq_domain_remove(domain);
  1971. return irq;
  1972. }
  1973. ret = request_threaded_irq(irq, amd_iommu_int_handler,
  1974. amd_iommu_int_thread, 0, "AMD-Vi", iommu);
  1975. if (ret) {
  1976. irq_domain_free_irqs(irq, 1);
  1977. irq_domain_remove(domain);
  1978. return ret;
  1979. }
  1980. return 0;
  1981. }
  1982. static int iommu_init_irq(struct amd_iommu *iommu)
  1983. {
  1984. int ret;
  1985. if (iommu->int_enabled)
  1986. goto enable_faults;
  1987. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1988. ret = iommu_setup_intcapxt(iommu);
  1989. else if (iommu->dev->msi_cap)
  1990. ret = iommu_setup_msi(iommu);
  1991. else
  1992. ret = -ENODEV;
  1993. if (ret)
  1994. return ret;
  1995. iommu->int_enabled = true;
  1996. enable_faults:
  1997. if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
  1998. iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
  1999. iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
  2000. if (iommu->ppr_log != NULL)
  2001. iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
  2002. return 0;
  2003. }
  2004. /****************************************************************************
  2005. *
  2006. * The next functions belong to the third pass of parsing the ACPI
  2007. * table. In this last pass the memory mapping requirements are
  2008. * gathered (like exclusion and unity mapping ranges).
  2009. *
  2010. ****************************************************************************/
  2011. static void __init free_unity_maps(void)
  2012. {
  2013. struct unity_map_entry *entry, *next;
  2014. struct amd_iommu_pci_seg *p, *pci_seg;
  2015. for_each_pci_segment_safe(pci_seg, p) {
  2016. list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
  2017. list_del(&entry->list);
  2018. kfree(entry);
  2019. }
  2020. }
  2021. }
  2022. /* called for unity map ACPI definition */
  2023. static int __init init_unity_map_range(struct ivmd_header *m,
  2024. struct acpi_table_header *ivrs_base)
  2025. {
  2026. struct unity_map_entry *e = NULL;
  2027. struct amd_iommu_pci_seg *pci_seg;
  2028. char *s;
  2029. pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
  2030. if (pci_seg == NULL)
  2031. return -ENOMEM;
  2032. e = kzalloc(sizeof(*e), GFP_KERNEL);
  2033. if (e == NULL)
  2034. return -ENOMEM;
  2035. switch (m->type) {
  2036. default:
  2037. kfree(e);
  2038. return 0;
  2039. case ACPI_IVMD_TYPE:
  2040. s = "IVMD_TYPEi\t\t\t";
  2041. e->devid_start = e->devid_end = m->devid;
  2042. break;
  2043. case ACPI_IVMD_TYPE_ALL:
  2044. s = "IVMD_TYPE_ALL\t\t";
  2045. e->devid_start = 0;
  2046. e->devid_end = pci_seg->last_bdf;
  2047. break;
  2048. case ACPI_IVMD_TYPE_RANGE:
  2049. s = "IVMD_TYPE_RANGE\t\t";
  2050. e->devid_start = m->devid;
  2051. e->devid_end = m->aux;
  2052. break;
  2053. }
  2054. e->address_start = PAGE_ALIGN(m->range_start);
  2055. e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
  2056. e->prot = m->flags >> 1;
  2057. /*
  2058. * Treat per-device exclusion ranges as r/w unity-mapped regions
  2059. * since some buggy BIOSes might lead to the overwritten exclusion
  2060. * range (exclusion_start and exclusion_length members). This
  2061. * happens when there are multiple exclusion ranges (IVMD entries)
  2062. * defined in ACPI table.
  2063. */
  2064. if (m->flags & IVMD_FLAG_EXCL_RANGE)
  2065. e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
  2066. DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
  2067. "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
  2068. " flags: %x\n", s, m->pci_seg,
  2069. PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
  2070. PCI_FUNC(e->devid_start), m->pci_seg,
  2071. PCI_BUS_NUM(e->devid_end),
  2072. PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
  2073. e->address_start, e->address_end, m->flags);
  2074. list_add_tail(&e->list, &pci_seg->unity_map);
  2075. return 0;
  2076. }
  2077. /* iterates over all memory definitions we find in the ACPI table */
  2078. static int __init init_memory_definitions(struct acpi_table_header *table)
  2079. {
  2080. u8 *p = (u8 *)table, *end = (u8 *)table;
  2081. struct ivmd_header *m;
  2082. end += table->length;
  2083. p += IVRS_HEADER_LENGTH;
  2084. while (p < end) {
  2085. m = (struct ivmd_header *)p;
  2086. if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
  2087. init_unity_map_range(m, table);
  2088. p += m->length;
  2089. }
  2090. return 0;
  2091. }
  2092. /*
  2093. * Init the device table to not allow DMA access for devices
  2094. */
  2095. static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
  2096. {
  2097. u32 devid;
  2098. struct dev_table_entry *dev_table = pci_seg->dev_table;
  2099. if (dev_table == NULL)
  2100. return;
  2101. for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
  2102. __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_VALID);
  2103. if (!amd_iommu_snp_en)
  2104. __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_TRANSLATION);
  2105. }
  2106. }
  2107. static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
  2108. {
  2109. u32 devid;
  2110. struct dev_table_entry *dev_table = pci_seg->dev_table;
  2111. if (dev_table == NULL)
  2112. return;
  2113. for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
  2114. dev_table[devid].data[0] = 0ULL;
  2115. dev_table[devid].data[1] = 0ULL;
  2116. }
  2117. }
  2118. static void init_device_table(void)
  2119. {
  2120. struct amd_iommu_pci_seg *pci_seg;
  2121. u32 devid;
  2122. if (!amd_iommu_irq_remap)
  2123. return;
  2124. for_each_pci_segment(pci_seg) {
  2125. for (devid = 0; devid <= pci_seg->last_bdf; ++devid)
  2126. __set_dev_entry_bit(pci_seg->dev_table,
  2127. devid, DEV_ENTRY_IRQ_TBL_EN);
  2128. }
  2129. }
  2130. static void iommu_init_flags(struct amd_iommu *iommu)
  2131. {
  2132. iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
  2133. iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
  2134. iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
  2135. iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
  2136. iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
  2137. iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
  2138. iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
  2139. iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
  2140. iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
  2141. iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
  2142. iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
  2143. iommu_feature_disable(iommu, CONTROL_ISOC_EN);
  2144. /*
  2145. * make IOMMU memory accesses cache coherent
  2146. */
  2147. iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
  2148. /* Set IOTLB invalidation timeout to 1s */
  2149. iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
  2150. }
  2151. static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
  2152. {
  2153. int i, j;
  2154. u32 ioc_feature_control;
  2155. struct pci_dev *pdev = iommu->root_pdev;
  2156. /* RD890 BIOSes may not have completely reconfigured the iommu */
  2157. if (!is_rd890_iommu(iommu->dev) || !pdev)
  2158. return;
  2159. /*
  2160. * First, we need to ensure that the iommu is enabled. This is
  2161. * controlled by a register in the northbridge
  2162. */
  2163. /* Select Northbridge indirect register 0x75 and enable writing */
  2164. pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
  2165. pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
  2166. /* Enable the iommu */
  2167. if (!(ioc_feature_control & 0x1))
  2168. pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
  2169. /* Restore the iommu BAR */
  2170. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  2171. iommu->stored_addr_lo);
  2172. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
  2173. iommu->stored_addr_hi);
  2174. /* Restore the l1 indirect regs for each of the 6 l1s */
  2175. for (i = 0; i < 6; i++)
  2176. for (j = 0; j < 0x12; j++)
  2177. iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
  2178. /* Restore the l2 indirect regs */
  2179. for (i = 0; i < 0x83; i++)
  2180. iommu_write_l2(iommu, i, iommu->stored_l2[i]);
  2181. /* Lock PCI setup registers */
  2182. pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
  2183. iommu->stored_addr_lo | 1);
  2184. }
  2185. static void iommu_enable_ga(struct amd_iommu *iommu)
  2186. {
  2187. #ifdef CONFIG_IRQ_REMAP
  2188. switch (amd_iommu_guest_ir) {
  2189. case AMD_IOMMU_GUEST_IR_VAPIC:
  2190. case AMD_IOMMU_GUEST_IR_LEGACY_GA:
  2191. iommu_feature_enable(iommu, CONTROL_GA_EN);
  2192. iommu->irte_ops = &irte_128_ops;
  2193. break;
  2194. default:
  2195. iommu->irte_ops = &irte_32_ops;
  2196. break;
  2197. }
  2198. #endif
  2199. }
  2200. static void iommu_disable_irtcachedis(struct amd_iommu *iommu)
  2201. {
  2202. iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
  2203. }
  2204. static void iommu_enable_irtcachedis(struct amd_iommu *iommu)
  2205. {
  2206. u64 ctrl;
  2207. if (!amd_iommu_irtcachedis)
  2208. return;
  2209. /*
  2210. * Note:
  2211. * The support for IRTCacheDis feature is dertermined by
  2212. * checking if the bit is writable.
  2213. */
  2214. iommu_feature_enable(iommu, CONTROL_IRTCACHEDIS);
  2215. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  2216. ctrl &= (1ULL << CONTROL_IRTCACHEDIS);
  2217. if (ctrl)
  2218. iommu->irtcachedis_enabled = true;
  2219. pr_info("iommu%d (%#06x) : IRT cache is %s\n",
  2220. iommu->index, iommu->devid,
  2221. iommu->irtcachedis_enabled ? "disabled" : "enabled");
  2222. }
  2223. static void early_enable_iommu(struct amd_iommu *iommu)
  2224. {
  2225. iommu_disable(iommu);
  2226. iommu_init_flags(iommu);
  2227. iommu_set_device_table(iommu);
  2228. iommu_enable_command_buffer(iommu);
  2229. iommu_enable_event_buffer(iommu);
  2230. iommu_set_exclusion_range(iommu);
  2231. iommu_enable_ga(iommu);
  2232. iommu_enable_xt(iommu);
  2233. iommu_enable_irtcachedis(iommu);
  2234. iommu_enable(iommu);
  2235. iommu_flush_all_caches(iommu);
  2236. }
  2237. /*
  2238. * This function finally enables all IOMMUs found in the system after
  2239. * they have been initialized.
  2240. *
  2241. * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
  2242. * the old content of device table entries. Not this case or copy failed,
  2243. * just continue as normal kernel does.
  2244. */
  2245. static void early_enable_iommus(void)
  2246. {
  2247. struct amd_iommu *iommu;
  2248. struct amd_iommu_pci_seg *pci_seg;
  2249. if (!copy_device_table()) {
  2250. /*
  2251. * If come here because of failure in copying device table from old
  2252. * kernel with all IOMMUs enabled, print error message and try to
  2253. * free allocated old_dev_tbl_cpy.
  2254. */
  2255. if (amd_iommu_pre_enabled)
  2256. pr_err("Failed to copy DEV table from previous kernel.\n");
  2257. for_each_pci_segment(pci_seg) {
  2258. if (pci_seg->old_dev_tbl_cpy != NULL) {
  2259. free_pages((unsigned long)pci_seg->old_dev_tbl_cpy,
  2260. get_order(pci_seg->dev_table_size));
  2261. pci_seg->old_dev_tbl_cpy = NULL;
  2262. }
  2263. }
  2264. for_each_iommu(iommu) {
  2265. clear_translation_pre_enabled(iommu);
  2266. early_enable_iommu(iommu);
  2267. }
  2268. } else {
  2269. pr_info("Copied DEV table from previous kernel.\n");
  2270. for_each_pci_segment(pci_seg) {
  2271. free_pages((unsigned long)pci_seg->dev_table,
  2272. get_order(pci_seg->dev_table_size));
  2273. pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;
  2274. }
  2275. for_each_iommu(iommu) {
  2276. iommu_disable_command_buffer(iommu);
  2277. iommu_disable_event_buffer(iommu);
  2278. iommu_disable_irtcachedis(iommu);
  2279. iommu_enable_command_buffer(iommu);
  2280. iommu_enable_event_buffer(iommu);
  2281. iommu_enable_ga(iommu);
  2282. iommu_enable_xt(iommu);
  2283. iommu_enable_irtcachedis(iommu);
  2284. iommu_set_device_table(iommu);
  2285. iommu_flush_all_caches(iommu);
  2286. }
  2287. }
  2288. }
  2289. static void enable_iommus_v2(void)
  2290. {
  2291. struct amd_iommu *iommu;
  2292. for_each_iommu(iommu) {
  2293. iommu_enable_ppr_log(iommu);
  2294. iommu_enable_gt(iommu);
  2295. }
  2296. }
  2297. static void enable_iommus_vapic(void)
  2298. {
  2299. #ifdef CONFIG_IRQ_REMAP
  2300. u32 status, i;
  2301. struct amd_iommu *iommu;
  2302. for_each_iommu(iommu) {
  2303. /*
  2304. * Disable GALog if already running. It could have been enabled
  2305. * in the previous boot before kdump.
  2306. */
  2307. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  2308. if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
  2309. continue;
  2310. iommu_feature_disable(iommu, CONTROL_GALOG_EN);
  2311. iommu_feature_disable(iommu, CONTROL_GAINT_EN);
  2312. /*
  2313. * Need to set and poll check the GALOGRun bit to zero before
  2314. * we can set/ modify GA Log registers safely.
  2315. */
  2316. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  2317. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  2318. if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
  2319. break;
  2320. udelay(10);
  2321. }
  2322. if (WARN_ON(i >= LOOP_TIMEOUT))
  2323. return;
  2324. }
  2325. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  2326. !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {
  2327. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2328. return;
  2329. }
  2330. if (amd_iommu_snp_en &&
  2331. !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
  2332. pr_warn("Force to disable Virtual APIC due to SNP\n");
  2333. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2334. return;
  2335. }
  2336. /* Enabling GAM and SNPAVIC support */
  2337. for_each_iommu(iommu) {
  2338. if (iommu_init_ga_log(iommu) ||
  2339. iommu_ga_log_enable(iommu))
  2340. return;
  2341. iommu_feature_enable(iommu, CONTROL_GAM_EN);
  2342. if (amd_iommu_snp_en)
  2343. iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
  2344. }
  2345. amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
  2346. pr_info("Virtual APIC enabled\n");
  2347. #endif
  2348. }
  2349. static void enable_iommus(void)
  2350. {
  2351. early_enable_iommus();
  2352. enable_iommus_vapic();
  2353. enable_iommus_v2();
  2354. }
  2355. static void disable_iommus(void)
  2356. {
  2357. struct amd_iommu *iommu;
  2358. for_each_iommu(iommu)
  2359. iommu_disable(iommu);
  2360. #ifdef CONFIG_IRQ_REMAP
  2361. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
  2362. amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
  2363. #endif
  2364. }
  2365. /*
  2366. * Suspend/Resume support
  2367. * disable suspend until real resume implemented
  2368. */
  2369. static void amd_iommu_resume(void)
  2370. {
  2371. struct amd_iommu *iommu;
  2372. for_each_iommu(iommu)
  2373. iommu_apply_resume_quirks(iommu);
  2374. /* re-load the hardware */
  2375. enable_iommus();
  2376. amd_iommu_enable_interrupts();
  2377. }
  2378. static int amd_iommu_suspend(void)
  2379. {
  2380. /* disable IOMMUs to go out of the way for BIOS */
  2381. disable_iommus();
  2382. return 0;
  2383. }
  2384. static struct syscore_ops amd_iommu_syscore_ops = {
  2385. .suspend = amd_iommu_suspend,
  2386. .resume = amd_iommu_resume,
  2387. };
  2388. static void __init free_iommu_resources(void)
  2389. {
  2390. kmem_cache_destroy(amd_iommu_irq_cache);
  2391. amd_iommu_irq_cache = NULL;
  2392. free_iommu_all();
  2393. free_pci_segments();
  2394. }
  2395. /* SB IOAPIC is always on this device in AMD systems */
  2396. #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
  2397. static bool __init check_ioapic_information(void)
  2398. {
  2399. const char *fw_bug = FW_BUG;
  2400. bool ret, has_sb_ioapic;
  2401. int idx;
  2402. has_sb_ioapic = false;
  2403. ret = false;
  2404. /*
  2405. * If we have map overrides on the kernel command line the
  2406. * messages in this function might not describe firmware bugs
  2407. * anymore - so be careful
  2408. */
  2409. if (cmdline_maps)
  2410. fw_bug = "";
  2411. for (idx = 0; idx < nr_ioapics; idx++) {
  2412. int devid, id = mpc_ioapic_id(idx);
  2413. devid = get_ioapic_devid(id);
  2414. if (devid < 0) {
  2415. pr_err("%s: IOAPIC[%d] not in IVRS table\n",
  2416. fw_bug, id);
  2417. ret = false;
  2418. } else if (devid == IOAPIC_SB_DEVID) {
  2419. has_sb_ioapic = true;
  2420. ret = true;
  2421. }
  2422. }
  2423. if (!has_sb_ioapic) {
  2424. /*
  2425. * We expect the SB IOAPIC to be listed in the IVRS
  2426. * table. The system timer is connected to the SB IOAPIC
  2427. * and if we don't have it in the list the system will
  2428. * panic at boot time. This situation usually happens
  2429. * when the BIOS is buggy and provides us the wrong
  2430. * device id for the IOAPIC in the system.
  2431. */
  2432. pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
  2433. }
  2434. if (!ret)
  2435. pr_err("Disabling interrupt remapping\n");
  2436. return ret;
  2437. }
  2438. static void __init free_dma_resources(void)
  2439. {
  2440. free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
  2441. get_order(MAX_DOMAIN_ID/8));
  2442. amd_iommu_pd_alloc_bitmap = NULL;
  2443. free_unity_maps();
  2444. }
  2445. static void __init ivinfo_init(void *ivrs)
  2446. {
  2447. amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
  2448. }
  2449. /*
  2450. * This is the hardware init function for AMD IOMMU in the system.
  2451. * This function is called either from amd_iommu_init or from the interrupt
  2452. * remapping setup code.
  2453. *
  2454. * This function basically parses the ACPI table for AMD IOMMU (IVRS)
  2455. * four times:
  2456. *
  2457. * 1 pass) Discover the most comprehensive IVHD type to use.
  2458. *
  2459. * 2 pass) Find the highest PCI device id the driver has to handle.
  2460. * Upon this information the size of the data structures is
  2461. * determined that needs to be allocated.
  2462. *
  2463. * 3 pass) Initialize the data structures just allocated with the
  2464. * information in the ACPI table about available AMD IOMMUs
  2465. * in the system. It also maps the PCI devices in the
  2466. * system to specific IOMMUs
  2467. *
  2468. * 4 pass) After the basic data structures are allocated and
  2469. * initialized we update them with information about memory
  2470. * remapping requirements parsed out of the ACPI table in
  2471. * this last pass.
  2472. *
  2473. * After everything is set up the IOMMUs are enabled and the necessary
  2474. * hotplug and suspend notifiers are registered.
  2475. */
  2476. static int __init early_amd_iommu_init(void)
  2477. {
  2478. struct acpi_table_header *ivrs_base;
  2479. int remap_cache_sz, ret;
  2480. acpi_status status;
  2481. if (!amd_iommu_detected)
  2482. return -ENODEV;
  2483. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2484. if (status == AE_NOT_FOUND)
  2485. return -ENODEV;
  2486. else if (ACPI_FAILURE(status)) {
  2487. const char *err = acpi_format_exception(status);
  2488. pr_err("IVRS table error: %s\n", err);
  2489. return -EINVAL;
  2490. }
  2491. /*
  2492. * Validate checksum here so we don't need to do it when
  2493. * we actually parse the table
  2494. */
  2495. ret = check_ivrs_checksum(ivrs_base);
  2496. if (ret)
  2497. goto out;
  2498. ivinfo_init(ivrs_base);
  2499. amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
  2500. DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
  2501. /* Device table - directly used by all IOMMUs */
  2502. ret = -ENOMEM;
  2503. amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
  2504. GFP_KERNEL | __GFP_ZERO,
  2505. get_order(MAX_DOMAIN_ID/8));
  2506. if (amd_iommu_pd_alloc_bitmap == NULL)
  2507. goto out;
  2508. /*
  2509. * never allocate domain 0 because its used as the non-allocated and
  2510. * error value placeholder
  2511. */
  2512. __set_bit(0, amd_iommu_pd_alloc_bitmap);
  2513. /*
  2514. * now the data structures are allocated and basically initialized
  2515. * start the real acpi table scan
  2516. */
  2517. ret = init_iommu_all(ivrs_base);
  2518. if (ret)
  2519. goto out;
  2520. /* Disable any previously enabled IOMMUs */
  2521. if (!is_kdump_kernel() || amd_iommu_disabled)
  2522. disable_iommus();
  2523. if (amd_iommu_irq_remap)
  2524. amd_iommu_irq_remap = check_ioapic_information();
  2525. if (amd_iommu_irq_remap) {
  2526. struct amd_iommu_pci_seg *pci_seg;
  2527. /*
  2528. * Interrupt remapping enabled, create kmem_cache for the
  2529. * remapping tables.
  2530. */
  2531. ret = -ENOMEM;
  2532. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2533. remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
  2534. else
  2535. remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
  2536. amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
  2537. remap_cache_sz,
  2538. DTE_INTTAB_ALIGNMENT,
  2539. 0, NULL);
  2540. if (!amd_iommu_irq_cache)
  2541. goto out;
  2542. for_each_pci_segment(pci_seg) {
  2543. if (alloc_irq_lookup_table(pci_seg))
  2544. goto out;
  2545. }
  2546. }
  2547. ret = init_memory_definitions(ivrs_base);
  2548. if (ret)
  2549. goto out;
  2550. /* init the device table */
  2551. init_device_table();
  2552. out:
  2553. /* Don't leak any ACPI memory */
  2554. acpi_put_table(ivrs_base);
  2555. return ret;
  2556. }
  2557. static int amd_iommu_enable_interrupts(void)
  2558. {
  2559. struct amd_iommu *iommu;
  2560. int ret = 0;
  2561. for_each_iommu(iommu) {
  2562. ret = iommu_init_irq(iommu);
  2563. if (ret)
  2564. goto out;
  2565. }
  2566. out:
  2567. return ret;
  2568. }
  2569. static bool __init detect_ivrs(void)
  2570. {
  2571. struct acpi_table_header *ivrs_base;
  2572. acpi_status status;
  2573. int i;
  2574. status = acpi_get_table("IVRS", 0, &ivrs_base);
  2575. if (status == AE_NOT_FOUND)
  2576. return false;
  2577. else if (ACPI_FAILURE(status)) {
  2578. const char *err = acpi_format_exception(status);
  2579. pr_err("IVRS table error: %s\n", err);
  2580. return false;
  2581. }
  2582. acpi_put_table(ivrs_base);
  2583. if (amd_iommu_force_enable)
  2584. goto out;
  2585. /* Don't use IOMMU if there is Stoney Ridge graphics */
  2586. for (i = 0; i < 32; i++) {
  2587. u32 pci_id;
  2588. pci_id = read_pci_config(0, i, 0, 0);
  2589. if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
  2590. pr_info("Disable IOMMU on Stoney Ridge\n");
  2591. return false;
  2592. }
  2593. }
  2594. out:
  2595. /* Make sure ACS will be enabled during PCI probe */
  2596. pci_request_acs();
  2597. return true;
  2598. }
  2599. /****************************************************************************
  2600. *
  2601. * AMD IOMMU Initialization State Machine
  2602. *
  2603. ****************************************************************************/
  2604. static int __init state_next(void)
  2605. {
  2606. int ret = 0;
  2607. switch (init_state) {
  2608. case IOMMU_START_STATE:
  2609. if (!detect_ivrs()) {
  2610. init_state = IOMMU_NOT_FOUND;
  2611. ret = -ENODEV;
  2612. } else {
  2613. init_state = IOMMU_IVRS_DETECTED;
  2614. }
  2615. break;
  2616. case IOMMU_IVRS_DETECTED:
  2617. if (amd_iommu_disabled) {
  2618. init_state = IOMMU_CMDLINE_DISABLED;
  2619. ret = -EINVAL;
  2620. } else {
  2621. ret = early_amd_iommu_init();
  2622. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
  2623. }
  2624. break;
  2625. case IOMMU_ACPI_FINISHED:
  2626. early_enable_iommus();
  2627. x86_platform.iommu_shutdown = disable_iommus;
  2628. init_state = IOMMU_ENABLED;
  2629. break;
  2630. case IOMMU_ENABLED:
  2631. register_syscore_ops(&amd_iommu_syscore_ops);
  2632. ret = amd_iommu_init_pci();
  2633. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
  2634. enable_iommus_vapic();
  2635. enable_iommus_v2();
  2636. break;
  2637. case IOMMU_PCI_INIT:
  2638. ret = amd_iommu_enable_interrupts();
  2639. init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
  2640. break;
  2641. case IOMMU_INTERRUPTS_EN:
  2642. init_state = IOMMU_INITIALIZED;
  2643. break;
  2644. case IOMMU_INITIALIZED:
  2645. /* Nothing to do */
  2646. break;
  2647. case IOMMU_NOT_FOUND:
  2648. case IOMMU_INIT_ERROR:
  2649. case IOMMU_CMDLINE_DISABLED:
  2650. /* Error states => do nothing */
  2651. ret = -EINVAL;
  2652. break;
  2653. default:
  2654. /* Unknown state */
  2655. BUG();
  2656. }
  2657. if (ret) {
  2658. free_dma_resources();
  2659. if (!irq_remapping_enabled) {
  2660. disable_iommus();
  2661. free_iommu_resources();
  2662. } else {
  2663. struct amd_iommu *iommu;
  2664. struct amd_iommu_pci_seg *pci_seg;
  2665. for_each_pci_segment(pci_seg)
  2666. uninit_device_table_dma(pci_seg);
  2667. for_each_iommu(iommu)
  2668. iommu_flush_all_caches(iommu);
  2669. }
  2670. }
  2671. return ret;
  2672. }
  2673. static int __init iommu_go_to_state(enum iommu_init_state state)
  2674. {
  2675. int ret = -EINVAL;
  2676. while (init_state != state) {
  2677. if (init_state == IOMMU_NOT_FOUND ||
  2678. init_state == IOMMU_INIT_ERROR ||
  2679. init_state == IOMMU_CMDLINE_DISABLED)
  2680. break;
  2681. ret = state_next();
  2682. }
  2683. return ret;
  2684. }
  2685. #ifdef CONFIG_IRQ_REMAP
  2686. int __init amd_iommu_prepare(void)
  2687. {
  2688. int ret;
  2689. amd_iommu_irq_remap = true;
  2690. ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
  2691. if (ret) {
  2692. amd_iommu_irq_remap = false;
  2693. return ret;
  2694. }
  2695. return amd_iommu_irq_remap ? 0 : -ENODEV;
  2696. }
  2697. int __init amd_iommu_enable(void)
  2698. {
  2699. int ret;
  2700. ret = iommu_go_to_state(IOMMU_ENABLED);
  2701. if (ret)
  2702. return ret;
  2703. irq_remapping_enabled = 1;
  2704. return amd_iommu_xt_mode;
  2705. }
  2706. void amd_iommu_disable(void)
  2707. {
  2708. amd_iommu_suspend();
  2709. }
  2710. int amd_iommu_reenable(int mode)
  2711. {
  2712. amd_iommu_resume();
  2713. return 0;
  2714. }
  2715. int __init amd_iommu_enable_faulting(void)
  2716. {
  2717. /* We enable MSI later when PCI is initialized */
  2718. return 0;
  2719. }
  2720. #endif
  2721. /*
  2722. * This is the core init function for AMD IOMMU hardware in the system.
  2723. * This function is called from the generic x86 DMA layer initialization
  2724. * code.
  2725. */
  2726. static int __init amd_iommu_init(void)
  2727. {
  2728. struct amd_iommu *iommu;
  2729. int ret;
  2730. ret = iommu_go_to_state(IOMMU_INITIALIZED);
  2731. #ifdef CONFIG_GART_IOMMU
  2732. if (ret && list_empty(&amd_iommu_list)) {
  2733. /*
  2734. * We failed to initialize the AMD IOMMU - try fallback
  2735. * to GART if possible.
  2736. */
  2737. gart_iommu_init();
  2738. }
  2739. #endif
  2740. for_each_iommu(iommu)
  2741. amd_iommu_debugfs_setup(iommu);
  2742. return ret;
  2743. }
  2744. static bool amd_iommu_sme_check(void)
  2745. {
  2746. if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
  2747. (boot_cpu_data.x86 != 0x17))
  2748. return true;
  2749. /* For Fam17h, a specific level of support is required */
  2750. if (boot_cpu_data.microcode >= 0x08001205)
  2751. return true;
  2752. if ((boot_cpu_data.microcode >= 0x08001126) &&
  2753. (boot_cpu_data.microcode <= 0x080011ff))
  2754. return true;
  2755. pr_notice("IOMMU not currently supported when SME is active\n");
  2756. return false;
  2757. }
  2758. /****************************************************************************
  2759. *
  2760. * Early detect code. This code runs at IOMMU detection time in the DMA
  2761. * layer. It just looks if there is an IVRS ACPI table to detect AMD
  2762. * IOMMUs
  2763. *
  2764. ****************************************************************************/
  2765. int __init amd_iommu_detect(void)
  2766. {
  2767. int ret;
  2768. if (no_iommu || (iommu_detected && !gart_iommu_aperture))
  2769. return -ENODEV;
  2770. if (!amd_iommu_sme_check())
  2771. return -ENODEV;
  2772. ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
  2773. if (ret)
  2774. return ret;
  2775. amd_iommu_detected = true;
  2776. iommu_detected = 1;
  2777. x86_init.iommu.iommu_init = amd_iommu_init;
  2778. return 1;
  2779. }
  2780. /****************************************************************************
  2781. *
  2782. * Parsing functions for the AMD IOMMU specific kernel command line
  2783. * options.
  2784. *
  2785. ****************************************************************************/
  2786. static int __init parse_amd_iommu_dump(char *str)
  2787. {
  2788. amd_iommu_dump = true;
  2789. return 1;
  2790. }
  2791. static int __init parse_amd_iommu_intr(char *str)
  2792. {
  2793. for (; *str; ++str) {
  2794. if (strncmp(str, "legacy", 6) == 0) {
  2795. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
  2796. break;
  2797. }
  2798. if (strncmp(str, "vapic", 5) == 0) {
  2799. amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
  2800. break;
  2801. }
  2802. }
  2803. return 1;
  2804. }
  2805. static int __init parse_amd_iommu_options(char *str)
  2806. {
  2807. if (!str)
  2808. return -EINVAL;
  2809. while (*str) {
  2810. if (strncmp(str, "fullflush", 9) == 0) {
  2811. pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
  2812. iommu_set_dma_strict();
  2813. } else if (strncmp(str, "force_enable", 12) == 0) {
  2814. amd_iommu_force_enable = true;
  2815. } else if (strncmp(str, "off", 3) == 0) {
  2816. amd_iommu_disabled = true;
  2817. } else if (strncmp(str, "force_isolation", 15) == 0) {
  2818. amd_iommu_force_isolation = true;
  2819. } else if (strncmp(str, "pgtbl_v1", 8) == 0) {
  2820. amd_iommu_pgtable = AMD_IOMMU_V1;
  2821. } else if (strncmp(str, "pgtbl_v2", 8) == 0) {
  2822. amd_iommu_pgtable = AMD_IOMMU_V2;
  2823. } else if (strncmp(str, "irtcachedis", 11) == 0) {
  2824. amd_iommu_irtcachedis = true;
  2825. } else {
  2826. pr_notice("Unknown option - '%s'\n", str);
  2827. }
  2828. str += strcspn(str, ",");
  2829. while (*str == ',')
  2830. str++;
  2831. }
  2832. return 1;
  2833. }
  2834. static int __init parse_ivrs_ioapic(char *str)
  2835. {
  2836. u32 seg = 0, bus, dev, fn;
  2837. int id, i;
  2838. u32 devid;
  2839. if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  2840. sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
  2841. goto found;
  2842. if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  2843. sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
  2844. pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
  2845. str, id, seg, bus, dev, fn);
  2846. goto found;
  2847. }
  2848. pr_err("Invalid command line: ivrs_ioapic%s\n", str);
  2849. return 1;
  2850. found:
  2851. if (early_ioapic_map_size == EARLY_MAP_SIZE) {
  2852. pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
  2853. str);
  2854. return 1;
  2855. }
  2856. devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  2857. cmdline_maps = true;
  2858. i = early_ioapic_map_size++;
  2859. early_ioapic_map[i].id = id;
  2860. early_ioapic_map[i].devid = devid;
  2861. early_ioapic_map[i].cmd_line = true;
  2862. return 1;
  2863. }
  2864. static int __init parse_ivrs_hpet(char *str)
  2865. {
  2866. u32 seg = 0, bus, dev, fn;
  2867. int id, i;
  2868. u32 devid;
  2869. if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  2870. sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
  2871. goto found;
  2872. if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
  2873. sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
  2874. pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
  2875. str, id, seg, bus, dev, fn);
  2876. goto found;
  2877. }
  2878. pr_err("Invalid command line: ivrs_hpet%s\n", str);
  2879. return 1;
  2880. found:
  2881. if (early_hpet_map_size == EARLY_MAP_SIZE) {
  2882. pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
  2883. str);
  2884. return 1;
  2885. }
  2886. devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  2887. cmdline_maps = true;
  2888. i = early_hpet_map_size++;
  2889. early_hpet_map[i].id = id;
  2890. early_hpet_map[i].devid = devid;
  2891. early_hpet_map[i].cmd_line = true;
  2892. return 1;
  2893. }
  2894. #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
  2895. static int __init parse_ivrs_acpihid(char *str)
  2896. {
  2897. u32 seg = 0, bus, dev, fn;
  2898. char *hid, *uid, *p, *addr;
  2899. char acpiid[ACPIID_LEN] = {0};
  2900. int i;
  2901. addr = strchr(str, '@');
  2902. if (!addr) {
  2903. addr = strchr(str, '=');
  2904. if (!addr)
  2905. goto not_found;
  2906. ++addr;
  2907. if (strlen(addr) > ACPIID_LEN)
  2908. goto not_found;
  2909. if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
  2910. sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
  2911. pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
  2912. str, acpiid, seg, bus, dev, fn);
  2913. goto found;
  2914. }
  2915. goto not_found;
  2916. }
  2917. /* We have the '@', make it the terminator to get just the acpiid */
  2918. *addr++ = 0;
  2919. if (strlen(str) > ACPIID_LEN + 1)
  2920. goto not_found;
  2921. if (sscanf(str, "=%s", acpiid) != 1)
  2922. goto not_found;
  2923. if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
  2924. sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
  2925. goto found;
  2926. not_found:
  2927. pr_err("Invalid command line: ivrs_acpihid%s\n", str);
  2928. return 1;
  2929. found:
  2930. p = acpiid;
  2931. hid = strsep(&p, ":");
  2932. uid = p;
  2933. if (!hid || !(*hid) || !uid) {
  2934. pr_err("Invalid command line: hid or uid\n");
  2935. return 1;
  2936. }
  2937. /*
  2938. * Ignore leading zeroes after ':', so e.g., AMDI0095:00
  2939. * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
  2940. */
  2941. while (*uid == '0' && *(uid + 1))
  2942. uid++;
  2943. i = early_acpihid_map_size++;
  2944. memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
  2945. memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
  2946. early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
  2947. early_acpihid_map[i].cmd_line = true;
  2948. return 1;
  2949. }
  2950. __setup("amd_iommu_dump", parse_amd_iommu_dump);
  2951. __setup("amd_iommu=", parse_amd_iommu_options);
  2952. __setup("amd_iommu_intr=", parse_amd_iommu_intr);
  2953. __setup("ivrs_ioapic", parse_ivrs_ioapic);
  2954. __setup("ivrs_hpet", parse_ivrs_hpet);
  2955. __setup("ivrs_acpihid", parse_ivrs_acpihid);
  2956. bool amd_iommu_v2_supported(void)
  2957. {
  2958. /*
  2959. * Since DTE[Mode]=0 is prohibited on SNP-enabled system
  2960. * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without
  2961. * setting up IOMMUv1 page table.
  2962. */
  2963. return amd_iommu_v2_present && !amd_iommu_snp_en;
  2964. }
  2965. EXPORT_SYMBOL(amd_iommu_v2_supported);
  2966. struct amd_iommu *get_amd_iommu(unsigned int idx)
  2967. {
  2968. unsigned int i = 0;
  2969. struct amd_iommu *iommu;
  2970. for_each_iommu(iommu)
  2971. if (i++ == idx)
  2972. return iommu;
  2973. return NULL;
  2974. }
  2975. /****************************************************************************
  2976. *
  2977. * IOMMU EFR Performance Counter support functionality. This code allows
  2978. * access to the IOMMU PC functionality.
  2979. *
  2980. ****************************************************************************/
  2981. u8 amd_iommu_pc_get_max_banks(unsigned int idx)
  2982. {
  2983. struct amd_iommu *iommu = get_amd_iommu(idx);
  2984. if (iommu)
  2985. return iommu->max_banks;
  2986. return 0;
  2987. }
  2988. EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
  2989. bool amd_iommu_pc_supported(void)
  2990. {
  2991. return amd_iommu_pc_present;
  2992. }
  2993. EXPORT_SYMBOL(amd_iommu_pc_supported);
  2994. u8 amd_iommu_pc_get_max_counters(unsigned int idx)
  2995. {
  2996. struct amd_iommu *iommu = get_amd_iommu(idx);
  2997. if (iommu)
  2998. return iommu->max_counters;
  2999. return 0;
  3000. }
  3001. EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
  3002. static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
  3003. u8 fxn, u64 *value, bool is_write)
  3004. {
  3005. u32 offset;
  3006. u32 max_offset_lim;
  3007. /* Make sure the IOMMU PC resource is available */
  3008. if (!amd_iommu_pc_present)
  3009. return -ENODEV;
  3010. /* Check for valid iommu and pc register indexing */
  3011. if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
  3012. return -ENODEV;
  3013. offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
  3014. /* Limit the offset to the hw defined mmio region aperture */
  3015. max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
  3016. (iommu->max_counters << 8) | 0x28);
  3017. if ((offset < MMIO_CNTR_REG_OFFSET) ||
  3018. (offset > max_offset_lim))
  3019. return -EINVAL;
  3020. if (is_write) {
  3021. u64 val = *value & GENMASK_ULL(47, 0);
  3022. writel((u32)val, iommu->mmio_base + offset);
  3023. writel((val >> 32), iommu->mmio_base + offset + 4);
  3024. } else {
  3025. *value = readl(iommu->mmio_base + offset + 4);
  3026. *value <<= 32;
  3027. *value |= readl(iommu->mmio_base + offset);
  3028. *value &= GENMASK_ULL(47, 0);
  3029. }
  3030. return 0;
  3031. }
  3032. int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  3033. {
  3034. if (!iommu)
  3035. return -EINVAL;
  3036. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
  3037. }
  3038. int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
  3039. {
  3040. if (!iommu)
  3041. return -EINVAL;
  3042. return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
  3043. }
  3044. #ifdef CONFIG_AMD_MEM_ENCRYPT
  3045. int amd_iommu_snp_enable(void)
  3046. {
  3047. /*
  3048. * The SNP support requires that IOMMU must be enabled, and is
  3049. * not configured in the passthrough mode.
  3050. */
  3051. if (no_iommu || iommu_default_passthrough()) {
  3052. pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");
  3053. return -EINVAL;
  3054. }
  3055. /*
  3056. * Prevent enabling SNP after IOMMU_ENABLED state because this process
  3057. * affect how IOMMU driver sets up data structures and configures
  3058. * IOMMU hardware.
  3059. */
  3060. if (init_state > IOMMU_ENABLED) {
  3061. pr_err("SNP: Too late to enable SNP for IOMMU.\n");
  3062. return -EINVAL;
  3063. }
  3064. amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);
  3065. if (!amd_iommu_snp_en)
  3066. return -EINVAL;
  3067. pr_info("SNP enabled\n");
  3068. /* Enforce IOMMU v1 pagetable when SNP is enabled. */
  3069. if (amd_iommu_pgtable != AMD_IOMMU_V1) {
  3070. pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");
  3071. amd_iommu_pgtable = AMD_IOMMU_V1;
  3072. }
  3073. return 0;
  3074. }
  3075. #endif