init.c 95 KB

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