amd_iommu.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/ratelimit.h>
  20. #include <linux/pci.h>
  21. #include <linux/acpi.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pci-ats.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/slab.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/dma-direct.h>
  31. #include <linux/iommu-helper.h>
  32. #include <linux/iommu.h>
  33. #include <linux/delay.h>
  34. #include <linux/amd-iommu.h>
  35. #include <linux/notifier.h>
  36. #include <linux/export.h>
  37. #include <linux/irq.h>
  38. #include <linux/msi.h>
  39. #include <linux/dma-contiguous.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/percpu.h>
  42. #include <linux/iova.h>
  43. #include <asm/irq_remapping.h>
  44. #include <asm/io_apic.h>
  45. #include <asm/apic.h>
  46. #include <asm/hw_irq.h>
  47. #include <asm/msidef.h>
  48. #include <asm/proto.h>
  49. #include <asm/iommu.h>
  50. #include <asm/gart.h>
  51. #include <asm/dma.h>
  52. #include "amd_iommu_proto.h"
  53. #include "amd_iommu_types.h"
  54. #include "irq_remapping.h"
  55. #define AMD_IOMMU_MAPPING_ERROR 0
  56. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  57. #define LOOP_TIMEOUT 100000
  58. /* IO virtual address start page frame number */
  59. #define IOVA_START_PFN (1)
  60. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  61. /* Reserved IOVA ranges */
  62. #define MSI_RANGE_START (0xfee00000)
  63. #define MSI_RANGE_END (0xfeefffff)
  64. #define HT_RANGE_START (0xfd00000000ULL)
  65. #define HT_RANGE_END (0xffffffffffULL)
  66. /*
  67. * This bitmap is used to advertise the page sizes our hardware support
  68. * to the IOMMU core, which will then use this information to split
  69. * physically contiguous memory regions it is mapping into page sizes
  70. * that we support.
  71. *
  72. * 512GB Pages are not supported due to a hardware bug
  73. */
  74. #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
  75. static DEFINE_SPINLOCK(amd_iommu_devtable_lock);
  76. static DEFINE_SPINLOCK(pd_bitmap_lock);
  77. /* List of all available dev_data structures */
  78. static LLIST_HEAD(dev_data_list);
  79. LIST_HEAD(ioapic_map);
  80. LIST_HEAD(hpet_map);
  81. LIST_HEAD(acpihid_map);
  82. /*
  83. * Domain for untranslated devices - only allocated
  84. * if iommu=pt passed on kernel cmd line.
  85. */
  86. const struct iommu_ops amd_iommu_ops;
  87. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  88. int amd_iommu_max_glx_val = -1;
  89. static const struct dma_map_ops amd_iommu_dma_ops;
  90. /*
  91. * general struct to manage commands send to an IOMMU
  92. */
  93. struct iommu_cmd {
  94. u32 data[4];
  95. };
  96. struct kmem_cache *amd_iommu_irq_cache;
  97. static void update_domain(struct protection_domain *domain);
  98. static int protection_domain_init(struct protection_domain *domain);
  99. static void detach_device(struct device *dev);
  100. static void iova_domain_flush_tlb(struct iova_domain *iovad);
  101. /*
  102. * Data container for a dma_ops specific protection domain
  103. */
  104. struct dma_ops_domain {
  105. /* generic protection domain information */
  106. struct protection_domain domain;
  107. /* IOVA RB-Tree */
  108. struct iova_domain iovad;
  109. };
  110. static struct iova_domain reserved_iova_ranges;
  111. static struct lock_class_key reserved_rbtree_key;
  112. /****************************************************************************
  113. *
  114. * Helper functions
  115. *
  116. ****************************************************************************/
  117. static inline int match_hid_uid(struct device *dev,
  118. struct acpihid_map_entry *entry)
  119. {
  120. struct acpi_device *adev = ACPI_COMPANION(dev);
  121. const char *hid, *uid;
  122. if (!adev)
  123. return -ENODEV;
  124. hid = acpi_device_hid(adev);
  125. uid = acpi_device_uid(adev);
  126. if (!hid || !(*hid))
  127. return -ENODEV;
  128. if (!uid || !(*uid))
  129. return strcmp(hid, entry->hid);
  130. if (!(*entry->uid))
  131. return strcmp(hid, entry->hid);
  132. return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
  133. }
  134. static inline u16 get_pci_device_id(struct device *dev)
  135. {
  136. struct pci_dev *pdev = to_pci_dev(dev);
  137. return PCI_DEVID(pdev->bus->number, pdev->devfn);
  138. }
  139. static inline int get_acpihid_device_id(struct device *dev,
  140. struct acpihid_map_entry **entry)
  141. {
  142. struct acpihid_map_entry *p;
  143. list_for_each_entry(p, &acpihid_map, list) {
  144. if (!match_hid_uid(dev, p)) {
  145. if (entry)
  146. *entry = p;
  147. return p->devid;
  148. }
  149. }
  150. return -EINVAL;
  151. }
  152. static inline int get_device_id(struct device *dev)
  153. {
  154. int devid;
  155. if (dev_is_pci(dev))
  156. devid = get_pci_device_id(dev);
  157. else
  158. devid = get_acpihid_device_id(dev, NULL);
  159. return devid;
  160. }
  161. static struct protection_domain *to_pdomain(struct iommu_domain *dom)
  162. {
  163. return container_of(dom, struct protection_domain, domain);
  164. }
  165. static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
  166. {
  167. BUG_ON(domain->flags != PD_DMA_OPS_MASK);
  168. return container_of(domain, struct dma_ops_domain, domain);
  169. }
  170. static struct iommu_dev_data *alloc_dev_data(u16 devid)
  171. {
  172. struct iommu_dev_data *dev_data;
  173. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  174. if (!dev_data)
  175. return NULL;
  176. dev_data->devid = devid;
  177. ratelimit_default_init(&dev_data->rs);
  178. llist_add(&dev_data->dev_data_list, &dev_data_list);
  179. return dev_data;
  180. }
  181. static struct iommu_dev_data *search_dev_data(u16 devid)
  182. {
  183. struct iommu_dev_data *dev_data;
  184. struct llist_node *node;
  185. if (llist_empty(&dev_data_list))
  186. return NULL;
  187. node = dev_data_list.first;
  188. llist_for_each_entry(dev_data, node, dev_data_list) {
  189. if (dev_data->devid == devid)
  190. return dev_data;
  191. }
  192. return NULL;
  193. }
  194. static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
  195. {
  196. *(u16 *)data = alias;
  197. return 0;
  198. }
  199. static u16 get_alias(struct device *dev)
  200. {
  201. struct pci_dev *pdev = to_pci_dev(dev);
  202. u16 devid, ivrs_alias, pci_alias;
  203. /* The callers make sure that get_device_id() does not fail here */
  204. devid = get_device_id(dev);
  205. /* For ACPI HID devices, we simply return the devid as such */
  206. if (!dev_is_pci(dev))
  207. return devid;
  208. ivrs_alias = amd_iommu_alias_table[devid];
  209. pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
  210. if (ivrs_alias == pci_alias)
  211. return ivrs_alias;
  212. /*
  213. * DMA alias showdown
  214. *
  215. * The IVRS is fairly reliable in telling us about aliases, but it
  216. * can't know about every screwy device. If we don't have an IVRS
  217. * reported alias, use the PCI reported alias. In that case we may
  218. * still need to initialize the rlookup and dev_table entries if the
  219. * alias is to a non-existent device.
  220. */
  221. if (ivrs_alias == devid) {
  222. if (!amd_iommu_rlookup_table[pci_alias]) {
  223. amd_iommu_rlookup_table[pci_alias] =
  224. amd_iommu_rlookup_table[devid];
  225. memcpy(amd_iommu_dev_table[pci_alias].data,
  226. amd_iommu_dev_table[devid].data,
  227. sizeof(amd_iommu_dev_table[pci_alias].data));
  228. }
  229. return pci_alias;
  230. }
  231. pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
  232. "for device %s[%04x:%04x], kernel reported alias "
  233. "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
  234. PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
  235. PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
  236. PCI_FUNC(pci_alias));
  237. /*
  238. * If we don't have a PCI DMA alias and the IVRS alias is on the same
  239. * bus, then the IVRS table may know about a quirk that we don't.
  240. */
  241. if (pci_alias == devid &&
  242. PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
  243. pci_add_dma_alias(pdev, ivrs_alias & 0xff);
  244. pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
  245. PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
  246. dev_name(dev));
  247. }
  248. return ivrs_alias;
  249. }
  250. static struct iommu_dev_data *find_dev_data(u16 devid)
  251. {
  252. struct iommu_dev_data *dev_data;
  253. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  254. dev_data = search_dev_data(devid);
  255. if (dev_data == NULL) {
  256. dev_data = alloc_dev_data(devid);
  257. if (!dev_data)
  258. return NULL;
  259. if (translation_pre_enabled(iommu))
  260. dev_data->defer_attach = true;
  261. }
  262. return dev_data;
  263. }
  264. struct iommu_dev_data *get_dev_data(struct device *dev)
  265. {
  266. return dev->archdata.iommu;
  267. }
  268. EXPORT_SYMBOL(get_dev_data);
  269. /*
  270. * Find or create an IOMMU group for a acpihid device.
  271. */
  272. static struct iommu_group *acpihid_device_group(struct device *dev)
  273. {
  274. struct acpihid_map_entry *p, *entry = NULL;
  275. int devid;
  276. devid = get_acpihid_device_id(dev, &entry);
  277. if (devid < 0)
  278. return ERR_PTR(devid);
  279. list_for_each_entry(p, &acpihid_map, list) {
  280. if ((devid == p->devid) && p->group)
  281. entry->group = p->group;
  282. }
  283. if (!entry->group)
  284. entry->group = generic_device_group(dev);
  285. else
  286. iommu_group_ref_get(entry->group);
  287. return entry->group;
  288. }
  289. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  290. {
  291. static const int caps[] = {
  292. PCI_EXT_CAP_ID_ATS,
  293. PCI_EXT_CAP_ID_PRI,
  294. PCI_EXT_CAP_ID_PASID,
  295. };
  296. int i, pos;
  297. if (pci_ats_disabled())
  298. return false;
  299. for (i = 0; i < 3; ++i) {
  300. pos = pci_find_ext_capability(pdev, caps[i]);
  301. if (pos == 0)
  302. return false;
  303. }
  304. return true;
  305. }
  306. static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
  307. {
  308. struct iommu_dev_data *dev_data;
  309. dev_data = get_dev_data(&pdev->dev);
  310. return dev_data->errata & (1 << erratum) ? true : false;
  311. }
  312. /*
  313. * This function checks if the driver got a valid device from the caller to
  314. * avoid dereferencing invalid pointers.
  315. */
  316. static bool check_device(struct device *dev)
  317. {
  318. int devid;
  319. if (!dev || !dev->dma_mask)
  320. return false;
  321. devid = get_device_id(dev);
  322. if (devid < 0)
  323. return false;
  324. /* Out of our scope? */
  325. if (devid > amd_iommu_last_bdf)
  326. return false;
  327. if (amd_iommu_rlookup_table[devid] == NULL)
  328. return false;
  329. return true;
  330. }
  331. static void init_iommu_group(struct device *dev)
  332. {
  333. struct iommu_group *group;
  334. group = iommu_group_get_for_dev(dev);
  335. if (IS_ERR(group))
  336. return;
  337. iommu_group_put(group);
  338. }
  339. static int iommu_init_device(struct device *dev)
  340. {
  341. struct iommu_dev_data *dev_data;
  342. struct amd_iommu *iommu;
  343. int devid;
  344. if (dev->archdata.iommu)
  345. return 0;
  346. devid = get_device_id(dev);
  347. if (devid < 0)
  348. return devid;
  349. iommu = amd_iommu_rlookup_table[devid];
  350. dev_data = find_dev_data(devid);
  351. if (!dev_data)
  352. return -ENOMEM;
  353. dev_data->alias = get_alias(dev);
  354. /*
  355. * By default we use passthrough mode for IOMMUv2 capable device.
  356. * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
  357. * invalid address), we ignore the capability for the device so
  358. * it'll be forced to go into translation mode.
  359. */
  360. if ((iommu_pass_through || !amd_iommu_force_isolation) &&
  361. dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
  362. struct amd_iommu *iommu;
  363. iommu = amd_iommu_rlookup_table[dev_data->devid];
  364. dev_data->iommu_v2 = iommu->is_iommu_v2;
  365. }
  366. dev->archdata.iommu = dev_data;
  367. iommu_device_link(&iommu->iommu, dev);
  368. return 0;
  369. }
  370. static void iommu_ignore_device(struct device *dev)
  371. {
  372. u16 alias;
  373. int devid;
  374. devid = get_device_id(dev);
  375. if (devid < 0)
  376. return;
  377. alias = get_alias(dev);
  378. memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
  379. memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
  380. amd_iommu_rlookup_table[devid] = NULL;
  381. amd_iommu_rlookup_table[alias] = NULL;
  382. }
  383. static void iommu_uninit_device(struct device *dev)
  384. {
  385. struct iommu_dev_data *dev_data;
  386. struct amd_iommu *iommu;
  387. int devid;
  388. devid = get_device_id(dev);
  389. if (devid < 0)
  390. return;
  391. iommu = amd_iommu_rlookup_table[devid];
  392. dev_data = search_dev_data(devid);
  393. if (!dev_data)
  394. return;
  395. if (dev_data->domain)
  396. detach_device(dev);
  397. iommu_device_unlink(&iommu->iommu, dev);
  398. iommu_group_remove_device(dev);
  399. /* Remove dma-ops */
  400. dev->dma_ops = NULL;
  401. /*
  402. * We keep dev_data around for unplugged devices and reuse it when the
  403. * device is re-plugged - not doing so would introduce a ton of races.
  404. */
  405. }
  406. /****************************************************************************
  407. *
  408. * Interrupt handling functions
  409. *
  410. ****************************************************************************/
  411. static void dump_dte_entry(u16 devid)
  412. {
  413. int i;
  414. for (i = 0; i < 4; ++i)
  415. pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
  416. amd_iommu_dev_table[devid].data[i]);
  417. }
  418. static void dump_command(unsigned long phys_addr)
  419. {
  420. struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
  421. int i;
  422. for (i = 0; i < 4; ++i)
  423. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  424. }
  425. static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
  426. u64 address, int flags)
  427. {
  428. struct iommu_dev_data *dev_data = NULL;
  429. struct pci_dev *pdev;
  430. pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
  431. devid & 0xff);
  432. if (pdev)
  433. dev_data = get_dev_data(&pdev->dev);
  434. if (dev_data && __ratelimit(&dev_data->rs)) {
  435. dev_err(&pdev->dev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  436. domain_id, address, flags);
  437. } else if (printk_ratelimit()) {
  438. pr_err("AMD-Vi: Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  439. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  440. domain_id, address, flags);
  441. }
  442. if (pdev)
  443. pci_dev_put(pdev);
  444. }
  445. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  446. {
  447. struct device *dev = iommu->iommu.dev;
  448. int type, devid, pasid, flags, tag;
  449. volatile u32 *event = __evt;
  450. int count = 0;
  451. u64 address;
  452. retry:
  453. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  454. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  455. pasid = PPR_PASID(*(u64 *)&event[0]);
  456. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  457. address = (u64)(((u64)event[3]) << 32) | event[2];
  458. if (type == 0) {
  459. /* Did we hit the erratum? */
  460. if (++count == LOOP_TIMEOUT) {
  461. pr_err("AMD-Vi: No event written to event log\n");
  462. return;
  463. }
  464. udelay(1);
  465. goto retry;
  466. }
  467. if (type == EVENT_TYPE_IO_FAULT) {
  468. amd_iommu_report_page_fault(devid, pasid, address, flags);
  469. return;
  470. }
  471. switch (type) {
  472. case EVENT_TYPE_ILL_DEV:
  473. dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  474. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  475. pasid, address, flags);
  476. dump_dte_entry(devid);
  477. break;
  478. case EVENT_TYPE_DEV_TAB_ERR:
  479. dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  480. "address=0x%016llx flags=0x%04x]\n",
  481. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  482. address, flags);
  483. break;
  484. case EVENT_TYPE_PAGE_TAB_ERR:
  485. dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  486. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  487. pasid, address, flags);
  488. break;
  489. case EVENT_TYPE_ILL_CMD:
  490. dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  491. dump_command(address);
  492. break;
  493. case EVENT_TYPE_CMD_HARD_ERR:
  494. dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%016llx flags=0x%04x]\n",
  495. address, flags);
  496. break;
  497. case EVENT_TYPE_IOTLB_INV_TO:
  498. dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%016llx]\n",
  499. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  500. address);
  501. break;
  502. case EVENT_TYPE_INV_DEV_REQ:
  503. dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  504. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  505. pasid, address, flags);
  506. break;
  507. case EVENT_TYPE_INV_PPR_REQ:
  508. pasid = ((event[0] >> 16) & 0xFFFF)
  509. | ((event[1] << 6) & 0xF0000);
  510. tag = event[1] & 0x03FF;
  511. dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  512. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  513. pasid, address, flags);
  514. break;
  515. default:
  516. dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
  517. event[0], event[1], event[2], event[3]);
  518. }
  519. memset(__evt, 0, 4 * sizeof(u32));
  520. }
  521. static void iommu_poll_events(struct amd_iommu *iommu)
  522. {
  523. u32 head, tail;
  524. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  525. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  526. while (head != tail) {
  527. iommu_print_event(iommu, iommu->evt_buf + head);
  528. head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
  529. }
  530. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  531. }
  532. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
  533. {
  534. struct amd_iommu_fault fault;
  535. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  536. pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
  537. return;
  538. }
  539. fault.address = raw[1];
  540. fault.pasid = PPR_PASID(raw[0]);
  541. fault.device_id = PPR_DEVID(raw[0]);
  542. fault.tag = PPR_TAG(raw[0]);
  543. fault.flags = PPR_FLAGS(raw[0]);
  544. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  545. }
  546. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  547. {
  548. u32 head, tail;
  549. if (iommu->ppr_log == NULL)
  550. return;
  551. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  552. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  553. while (head != tail) {
  554. volatile u64 *raw;
  555. u64 entry[2];
  556. int i;
  557. raw = (u64 *)(iommu->ppr_log + head);
  558. /*
  559. * Hardware bug: Interrupt may arrive before the entry is
  560. * written to memory. If this happens we need to wait for the
  561. * entry to arrive.
  562. */
  563. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  564. if (PPR_REQ_TYPE(raw[0]) != 0)
  565. break;
  566. udelay(1);
  567. }
  568. /* Avoid memcpy function-call overhead */
  569. entry[0] = raw[0];
  570. entry[1] = raw[1];
  571. /*
  572. * To detect the hardware bug we need to clear the entry
  573. * back to zero.
  574. */
  575. raw[0] = raw[1] = 0UL;
  576. /* Update head pointer of hardware ring-buffer */
  577. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  578. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  579. /* Handle PPR entry */
  580. iommu_handle_ppr_entry(iommu, entry);
  581. /* Refresh ring-buffer information */
  582. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  583. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  584. }
  585. }
  586. #ifdef CONFIG_IRQ_REMAP
  587. static int (*iommu_ga_log_notifier)(u32);
  588. int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
  589. {
  590. iommu_ga_log_notifier = notifier;
  591. return 0;
  592. }
  593. EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
  594. static void iommu_poll_ga_log(struct amd_iommu *iommu)
  595. {
  596. u32 head, tail, cnt = 0;
  597. if (iommu->ga_log == NULL)
  598. return;
  599. head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  600. tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  601. while (head != tail) {
  602. volatile u64 *raw;
  603. u64 log_entry;
  604. raw = (u64 *)(iommu->ga_log + head);
  605. cnt++;
  606. /* Avoid memcpy function-call overhead */
  607. log_entry = *raw;
  608. /* Update head pointer of hardware ring-buffer */
  609. head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
  610. writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  611. /* Handle GA entry */
  612. switch (GA_REQ_TYPE(log_entry)) {
  613. case GA_GUEST_NR:
  614. if (!iommu_ga_log_notifier)
  615. break;
  616. pr_debug("AMD-Vi: %s: devid=%#x, ga_tag=%#x\n",
  617. __func__, GA_DEVID(log_entry),
  618. GA_TAG(log_entry));
  619. if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
  620. pr_err("AMD-Vi: GA log notifier failed.\n");
  621. break;
  622. default:
  623. break;
  624. }
  625. }
  626. }
  627. #endif /* CONFIG_IRQ_REMAP */
  628. #define AMD_IOMMU_INT_MASK \
  629. (MMIO_STATUS_EVT_INT_MASK | \
  630. MMIO_STATUS_PPR_INT_MASK | \
  631. MMIO_STATUS_GALOG_INT_MASK)
  632. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  633. {
  634. struct amd_iommu *iommu = (struct amd_iommu *) data;
  635. u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  636. while (status & AMD_IOMMU_INT_MASK) {
  637. /* Enable EVT and PPR and GA interrupts again */
  638. writel(AMD_IOMMU_INT_MASK,
  639. iommu->mmio_base + MMIO_STATUS_OFFSET);
  640. if (status & MMIO_STATUS_EVT_INT_MASK) {
  641. pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
  642. iommu_poll_events(iommu);
  643. }
  644. if (status & MMIO_STATUS_PPR_INT_MASK) {
  645. pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
  646. iommu_poll_ppr_log(iommu);
  647. }
  648. #ifdef CONFIG_IRQ_REMAP
  649. if (status & MMIO_STATUS_GALOG_INT_MASK) {
  650. pr_devel("AMD-Vi: Processing IOMMU GA Log\n");
  651. iommu_poll_ga_log(iommu);
  652. }
  653. #endif
  654. /*
  655. * Hardware bug: ERBT1312
  656. * When re-enabling interrupt (by writing 1
  657. * to clear the bit), the hardware might also try to set
  658. * the interrupt bit in the event status register.
  659. * In this scenario, the bit will be set, and disable
  660. * subsequent interrupts.
  661. *
  662. * Workaround: The IOMMU driver should read back the
  663. * status register and check if the interrupt bits are cleared.
  664. * If not, driver will need to go through the interrupt handler
  665. * again and re-clear the bits
  666. */
  667. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  668. }
  669. return IRQ_HANDLED;
  670. }
  671. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  672. {
  673. return IRQ_WAKE_THREAD;
  674. }
  675. /****************************************************************************
  676. *
  677. * IOMMU command queuing functions
  678. *
  679. ****************************************************************************/
  680. static int wait_on_sem(volatile u64 *sem)
  681. {
  682. int i = 0;
  683. while (*sem == 0 && i < LOOP_TIMEOUT) {
  684. udelay(1);
  685. i += 1;
  686. }
  687. if (i == LOOP_TIMEOUT) {
  688. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  689. return -EIO;
  690. }
  691. return 0;
  692. }
  693. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  694. struct iommu_cmd *cmd)
  695. {
  696. u8 *target;
  697. target = iommu->cmd_buf + iommu->cmd_buf_tail;
  698. iommu->cmd_buf_tail += sizeof(*cmd);
  699. iommu->cmd_buf_tail %= CMD_BUFFER_SIZE;
  700. /* Copy command to buffer */
  701. memcpy(target, cmd, sizeof(*cmd));
  702. /* Tell the IOMMU about it */
  703. writel(iommu->cmd_buf_tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  704. }
  705. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  706. {
  707. u64 paddr = iommu_virt_to_phys((void *)address);
  708. WARN_ON(address & 0x7ULL);
  709. memset(cmd, 0, sizeof(*cmd));
  710. cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
  711. cmd->data[1] = upper_32_bits(paddr);
  712. cmd->data[2] = 1;
  713. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  714. }
  715. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  716. {
  717. memset(cmd, 0, sizeof(*cmd));
  718. cmd->data[0] = devid;
  719. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  720. }
  721. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  722. size_t size, u16 domid, int pde)
  723. {
  724. u64 pages;
  725. bool s;
  726. pages = iommu_num_pages(address, size, PAGE_SIZE);
  727. s = false;
  728. if (pages > 1) {
  729. /*
  730. * If we have to flush more than one page, flush all
  731. * TLB entries for this domain
  732. */
  733. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  734. s = true;
  735. }
  736. address &= PAGE_MASK;
  737. memset(cmd, 0, sizeof(*cmd));
  738. cmd->data[1] |= domid;
  739. cmd->data[2] = lower_32_bits(address);
  740. cmd->data[3] = upper_32_bits(address);
  741. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  742. if (s) /* size bit - we flush more than one 4kb page */
  743. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  744. if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
  745. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  746. }
  747. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  748. u64 address, size_t size)
  749. {
  750. u64 pages;
  751. bool s;
  752. pages = iommu_num_pages(address, size, PAGE_SIZE);
  753. s = false;
  754. if (pages > 1) {
  755. /*
  756. * If we have to flush more than one page, flush all
  757. * TLB entries for this domain
  758. */
  759. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  760. s = true;
  761. }
  762. address &= PAGE_MASK;
  763. memset(cmd, 0, sizeof(*cmd));
  764. cmd->data[0] = devid;
  765. cmd->data[0] |= (qdep & 0xff) << 24;
  766. cmd->data[1] = devid;
  767. cmd->data[2] = lower_32_bits(address);
  768. cmd->data[3] = upper_32_bits(address);
  769. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  770. if (s)
  771. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  772. }
  773. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
  774. u64 address, bool size)
  775. {
  776. memset(cmd, 0, sizeof(*cmd));
  777. address &= ~(0xfffULL);
  778. cmd->data[0] = pasid;
  779. cmd->data[1] = domid;
  780. cmd->data[2] = lower_32_bits(address);
  781. cmd->data[3] = upper_32_bits(address);
  782. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  783. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  784. if (size)
  785. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  786. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  787. }
  788. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
  789. int qdep, u64 address, bool size)
  790. {
  791. memset(cmd, 0, sizeof(*cmd));
  792. address &= ~(0xfffULL);
  793. cmd->data[0] = devid;
  794. cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
  795. cmd->data[0] |= (qdep & 0xff) << 24;
  796. cmd->data[1] = devid;
  797. cmd->data[1] |= (pasid & 0xff) << 16;
  798. cmd->data[2] = lower_32_bits(address);
  799. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  800. cmd->data[3] = upper_32_bits(address);
  801. if (size)
  802. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  803. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  804. }
  805. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
  806. int status, int tag, bool gn)
  807. {
  808. memset(cmd, 0, sizeof(*cmd));
  809. cmd->data[0] = devid;
  810. if (gn) {
  811. cmd->data[1] = pasid;
  812. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  813. }
  814. cmd->data[3] = tag & 0x1ff;
  815. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  816. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  817. }
  818. static void build_inv_all(struct iommu_cmd *cmd)
  819. {
  820. memset(cmd, 0, sizeof(*cmd));
  821. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  822. }
  823. static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  824. {
  825. memset(cmd, 0, sizeof(*cmd));
  826. cmd->data[0] = devid;
  827. CMD_SET_TYPE(cmd, CMD_INV_IRT);
  828. }
  829. /*
  830. * Writes the command to the IOMMUs command buffer and informs the
  831. * hardware about the new command.
  832. */
  833. static int __iommu_queue_command_sync(struct amd_iommu *iommu,
  834. struct iommu_cmd *cmd,
  835. bool sync)
  836. {
  837. unsigned int count = 0;
  838. u32 left, next_tail;
  839. next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  840. again:
  841. left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
  842. if (left <= 0x20) {
  843. /* Skip udelay() the first time around */
  844. if (count++) {
  845. if (count == LOOP_TIMEOUT) {
  846. pr_err("AMD-Vi: Command buffer timeout\n");
  847. return -EIO;
  848. }
  849. udelay(1);
  850. }
  851. /* Update head and recheck remaining space */
  852. iommu->cmd_buf_head = readl(iommu->mmio_base +
  853. MMIO_CMD_HEAD_OFFSET);
  854. goto again;
  855. }
  856. copy_cmd_to_buffer(iommu, cmd);
  857. /* Do we need to make sure all commands are processed? */
  858. iommu->need_sync = sync;
  859. return 0;
  860. }
  861. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  862. struct iommu_cmd *cmd,
  863. bool sync)
  864. {
  865. unsigned long flags;
  866. int ret;
  867. raw_spin_lock_irqsave(&iommu->lock, flags);
  868. ret = __iommu_queue_command_sync(iommu, cmd, sync);
  869. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  870. return ret;
  871. }
  872. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  873. {
  874. return iommu_queue_command_sync(iommu, cmd, true);
  875. }
  876. /*
  877. * This function queues a completion wait command into the command
  878. * buffer of an IOMMU
  879. */
  880. static int iommu_completion_wait(struct amd_iommu *iommu)
  881. {
  882. struct iommu_cmd cmd;
  883. unsigned long flags;
  884. int ret;
  885. if (!iommu->need_sync)
  886. return 0;
  887. build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
  888. raw_spin_lock_irqsave(&iommu->lock, flags);
  889. iommu->cmd_sem = 0;
  890. ret = __iommu_queue_command_sync(iommu, &cmd, false);
  891. if (ret)
  892. goto out_unlock;
  893. ret = wait_on_sem(&iommu->cmd_sem);
  894. out_unlock:
  895. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  896. return ret;
  897. }
  898. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  899. {
  900. struct iommu_cmd cmd;
  901. build_inv_dte(&cmd, devid);
  902. return iommu_queue_command(iommu, &cmd);
  903. }
  904. static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
  905. {
  906. u32 devid;
  907. for (devid = 0; devid <= 0xffff; ++devid)
  908. iommu_flush_dte(iommu, devid);
  909. iommu_completion_wait(iommu);
  910. }
  911. /*
  912. * This function uses heavy locking and may disable irqs for some time. But
  913. * this is no issue because it is only called during resume.
  914. */
  915. static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
  916. {
  917. u32 dom_id;
  918. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  919. struct iommu_cmd cmd;
  920. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  921. dom_id, 1);
  922. iommu_queue_command(iommu, &cmd);
  923. }
  924. iommu_completion_wait(iommu);
  925. }
  926. static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
  927. {
  928. struct iommu_cmd cmd;
  929. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  930. dom_id, 1);
  931. iommu_queue_command(iommu, &cmd);
  932. iommu_completion_wait(iommu);
  933. }
  934. static void amd_iommu_flush_all(struct amd_iommu *iommu)
  935. {
  936. struct iommu_cmd cmd;
  937. build_inv_all(&cmd);
  938. iommu_queue_command(iommu, &cmd);
  939. iommu_completion_wait(iommu);
  940. }
  941. static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
  942. {
  943. struct iommu_cmd cmd;
  944. build_inv_irt(&cmd, devid);
  945. iommu_queue_command(iommu, &cmd);
  946. }
  947. static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
  948. {
  949. u32 devid;
  950. for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
  951. iommu_flush_irt(iommu, devid);
  952. iommu_completion_wait(iommu);
  953. }
  954. void iommu_flush_all_caches(struct amd_iommu *iommu)
  955. {
  956. if (iommu_feature(iommu, FEATURE_IA)) {
  957. amd_iommu_flush_all(iommu);
  958. } else {
  959. amd_iommu_flush_dte_all(iommu);
  960. amd_iommu_flush_irt_all(iommu);
  961. amd_iommu_flush_tlb_all(iommu);
  962. }
  963. }
  964. /*
  965. * Command send function for flushing on-device TLB
  966. */
  967. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  968. u64 address, size_t size)
  969. {
  970. struct amd_iommu *iommu;
  971. struct iommu_cmd cmd;
  972. int qdep;
  973. qdep = dev_data->ats.qdep;
  974. iommu = amd_iommu_rlookup_table[dev_data->devid];
  975. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  976. return iommu_queue_command(iommu, &cmd);
  977. }
  978. /*
  979. * Command send function for invalidating a device table entry
  980. */
  981. static int device_flush_dte(struct iommu_dev_data *dev_data)
  982. {
  983. struct amd_iommu *iommu;
  984. u16 alias;
  985. int ret;
  986. iommu = amd_iommu_rlookup_table[dev_data->devid];
  987. alias = dev_data->alias;
  988. ret = iommu_flush_dte(iommu, dev_data->devid);
  989. if (!ret && alias != dev_data->devid)
  990. ret = iommu_flush_dte(iommu, alias);
  991. if (ret)
  992. return ret;
  993. if (dev_data->ats.enabled)
  994. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  995. return ret;
  996. }
  997. /*
  998. * TLB invalidation function which is called from the mapping functions.
  999. * It invalidates a single PTE if the range to flush is within a single
  1000. * page. Otherwise it flushes the whole TLB of the IOMMU.
  1001. */
  1002. static void __domain_flush_pages(struct protection_domain *domain,
  1003. u64 address, size_t size, int pde)
  1004. {
  1005. struct iommu_dev_data *dev_data;
  1006. struct iommu_cmd cmd;
  1007. int ret = 0, i;
  1008. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  1009. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1010. if (!domain->dev_iommu[i])
  1011. continue;
  1012. /*
  1013. * Devices of this domain are behind this IOMMU
  1014. * We need a TLB flush
  1015. */
  1016. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  1017. }
  1018. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1019. if (!dev_data->ats.enabled)
  1020. continue;
  1021. ret |= device_flush_iotlb(dev_data, address, size);
  1022. }
  1023. WARN_ON(ret);
  1024. }
  1025. static void domain_flush_pages(struct protection_domain *domain,
  1026. u64 address, size_t size)
  1027. {
  1028. __domain_flush_pages(domain, address, size, 0);
  1029. }
  1030. /* Flush the whole IO/TLB for a given protection domain */
  1031. static void domain_flush_tlb(struct protection_domain *domain)
  1032. {
  1033. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  1034. }
  1035. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  1036. static void domain_flush_tlb_pde(struct protection_domain *domain)
  1037. {
  1038. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  1039. }
  1040. static void domain_flush_complete(struct protection_domain *domain)
  1041. {
  1042. int i;
  1043. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1044. if (domain && !domain->dev_iommu[i])
  1045. continue;
  1046. /*
  1047. * Devices of this domain are behind this IOMMU
  1048. * We need to wait for completion of all commands.
  1049. */
  1050. iommu_completion_wait(amd_iommus[i]);
  1051. }
  1052. }
  1053. /*
  1054. * This function flushes the DTEs for all devices in domain
  1055. */
  1056. static void domain_flush_devices(struct protection_domain *domain)
  1057. {
  1058. struct iommu_dev_data *dev_data;
  1059. list_for_each_entry(dev_data, &domain->dev_list, list)
  1060. device_flush_dte(dev_data);
  1061. }
  1062. /****************************************************************************
  1063. *
  1064. * The functions below are used the create the page table mappings for
  1065. * unity mapped regions.
  1066. *
  1067. ****************************************************************************/
  1068. /*
  1069. * This function is used to add another level to an IO page table. Adding
  1070. * another level increases the size of the address space by 9 bits to a size up
  1071. * to 64 bits.
  1072. */
  1073. static void increase_address_space(struct protection_domain *domain,
  1074. gfp_t gfp)
  1075. {
  1076. unsigned long flags;
  1077. u64 *pte;
  1078. spin_lock_irqsave(&domain->lock, flags);
  1079. if (WARN_ON_ONCE(domain->mode == PAGE_MODE_6_LEVEL))
  1080. /* address space already 64 bit large */
  1081. goto out;
  1082. pte = (void *)get_zeroed_page(gfp);
  1083. if (!pte)
  1084. goto out;
  1085. *pte = PM_LEVEL_PDE(domain->mode,
  1086. iommu_virt_to_phys(domain->pt_root));
  1087. domain->pt_root = pte;
  1088. domain->mode += 1;
  1089. domain->updated = true;
  1090. out:
  1091. spin_unlock_irqrestore(&domain->lock, flags);
  1092. return;
  1093. }
  1094. static u64 *alloc_pte(struct protection_domain *domain,
  1095. unsigned long address,
  1096. unsigned long page_size,
  1097. u64 **pte_page,
  1098. gfp_t gfp)
  1099. {
  1100. int level, end_lvl;
  1101. u64 *pte, *page;
  1102. BUG_ON(!is_power_of_2(page_size));
  1103. while (address > PM_LEVEL_SIZE(domain->mode))
  1104. increase_address_space(domain, gfp);
  1105. level = domain->mode - 1;
  1106. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1107. address = PAGE_SIZE_ALIGN(address, page_size);
  1108. end_lvl = PAGE_SIZE_LEVEL(page_size);
  1109. while (level > end_lvl) {
  1110. u64 __pte, __npte;
  1111. __pte = *pte;
  1112. if (!IOMMU_PTE_PRESENT(__pte)) {
  1113. page = (u64 *)get_zeroed_page(gfp);
  1114. if (!page)
  1115. return NULL;
  1116. __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
  1117. /* pte could have been changed somewhere. */
  1118. if (cmpxchg64(pte, __pte, __npte) != __pte) {
  1119. free_page((unsigned long)page);
  1120. continue;
  1121. }
  1122. }
  1123. /* No level skipping support yet */
  1124. if (PM_PTE_LEVEL(*pte) != level)
  1125. return NULL;
  1126. level -= 1;
  1127. pte = IOMMU_PTE_PAGE(*pte);
  1128. if (pte_page && level == end_lvl)
  1129. *pte_page = pte;
  1130. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1131. }
  1132. return pte;
  1133. }
  1134. /*
  1135. * This function checks if there is a PTE for a given dma address. If
  1136. * there is one, it returns the pointer to it.
  1137. */
  1138. static u64 *fetch_pte(struct protection_domain *domain,
  1139. unsigned long address,
  1140. unsigned long *page_size)
  1141. {
  1142. int level;
  1143. u64 *pte;
  1144. *page_size = 0;
  1145. if (address > PM_LEVEL_SIZE(domain->mode))
  1146. return NULL;
  1147. level = domain->mode - 1;
  1148. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1149. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1150. while (level > 0) {
  1151. /* Not Present */
  1152. if (!IOMMU_PTE_PRESENT(*pte))
  1153. return NULL;
  1154. /* Large PTE */
  1155. if (PM_PTE_LEVEL(*pte) == 7 ||
  1156. PM_PTE_LEVEL(*pte) == 0)
  1157. break;
  1158. /* No level skipping support yet */
  1159. if (PM_PTE_LEVEL(*pte) != level)
  1160. return NULL;
  1161. level -= 1;
  1162. /* Walk to the next level */
  1163. pte = IOMMU_PTE_PAGE(*pte);
  1164. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1165. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1166. }
  1167. if (PM_PTE_LEVEL(*pte) == 0x07) {
  1168. unsigned long pte_mask;
  1169. /*
  1170. * If we have a series of large PTEs, make
  1171. * sure to return a pointer to the first one.
  1172. */
  1173. *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
  1174. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  1175. pte = (u64 *)(((unsigned long)pte) & pte_mask);
  1176. }
  1177. return pte;
  1178. }
  1179. /*
  1180. * Generic mapping functions. It maps a physical address into a DMA
  1181. * address space. It allocates the page table pages if necessary.
  1182. * In the future it can be extended to a generic mapping function
  1183. * supporting all features of AMD IOMMU page tables like level skipping
  1184. * and full 64 bit address spaces.
  1185. */
  1186. static int iommu_map_page(struct protection_domain *dom,
  1187. unsigned long bus_addr,
  1188. unsigned long phys_addr,
  1189. unsigned long page_size,
  1190. int prot,
  1191. gfp_t gfp)
  1192. {
  1193. u64 __pte, *pte;
  1194. int i, count;
  1195. BUG_ON(!IS_ALIGNED(bus_addr, page_size));
  1196. BUG_ON(!IS_ALIGNED(phys_addr, page_size));
  1197. if (!(prot & IOMMU_PROT_MASK))
  1198. return -EINVAL;
  1199. count = PAGE_SIZE_PTE_COUNT(page_size);
  1200. pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
  1201. if (!pte)
  1202. return -ENOMEM;
  1203. for (i = 0; i < count; ++i)
  1204. if (IOMMU_PTE_PRESENT(pte[i]))
  1205. return -EBUSY;
  1206. if (count > 1) {
  1207. __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size);
  1208. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  1209. } else
  1210. __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  1211. if (prot & IOMMU_PROT_IR)
  1212. __pte |= IOMMU_PTE_IR;
  1213. if (prot & IOMMU_PROT_IW)
  1214. __pte |= IOMMU_PTE_IW;
  1215. for (i = 0; i < count; ++i)
  1216. pte[i] = __pte;
  1217. update_domain(dom);
  1218. return 0;
  1219. }
  1220. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  1221. unsigned long bus_addr,
  1222. unsigned long page_size)
  1223. {
  1224. unsigned long long unmapped;
  1225. unsigned long unmap_size;
  1226. u64 *pte;
  1227. BUG_ON(!is_power_of_2(page_size));
  1228. unmapped = 0;
  1229. while (unmapped < page_size) {
  1230. pte = fetch_pte(dom, bus_addr, &unmap_size);
  1231. if (pte) {
  1232. int i, count;
  1233. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  1234. for (i = 0; i < count; i++)
  1235. pte[i] = 0ULL;
  1236. }
  1237. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  1238. unmapped += unmap_size;
  1239. }
  1240. BUG_ON(unmapped && !is_power_of_2(unmapped));
  1241. return unmapped;
  1242. }
  1243. /****************************************************************************
  1244. *
  1245. * The next functions belong to the address allocator for the dma_ops
  1246. * interface functions.
  1247. *
  1248. ****************************************************************************/
  1249. static unsigned long dma_ops_alloc_iova(struct device *dev,
  1250. struct dma_ops_domain *dma_dom,
  1251. unsigned int pages, u64 dma_mask)
  1252. {
  1253. unsigned long pfn = 0;
  1254. pages = __roundup_pow_of_two(pages);
  1255. if (dma_mask > DMA_BIT_MASK(32))
  1256. pfn = alloc_iova_fast(&dma_dom->iovad, pages,
  1257. IOVA_PFN(DMA_BIT_MASK(32)), false);
  1258. if (!pfn)
  1259. pfn = alloc_iova_fast(&dma_dom->iovad, pages,
  1260. IOVA_PFN(dma_mask), true);
  1261. return (pfn << PAGE_SHIFT);
  1262. }
  1263. static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
  1264. unsigned long address,
  1265. unsigned int pages)
  1266. {
  1267. pages = __roundup_pow_of_two(pages);
  1268. address >>= PAGE_SHIFT;
  1269. free_iova_fast(&dma_dom->iovad, address, pages);
  1270. }
  1271. /****************************************************************************
  1272. *
  1273. * The next functions belong to the domain allocation. A domain is
  1274. * allocated for every IOMMU as the default domain. If device isolation
  1275. * is enabled, every device get its own domain. The most important thing
  1276. * about domains is the page table mapping the DMA address space they
  1277. * contain.
  1278. *
  1279. ****************************************************************************/
  1280. /*
  1281. * This function adds a protection domain to the global protection domain list
  1282. */
  1283. static void add_domain_to_list(struct protection_domain *domain)
  1284. {
  1285. unsigned long flags;
  1286. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1287. list_add(&domain->list, &amd_iommu_pd_list);
  1288. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1289. }
  1290. /*
  1291. * This function removes a protection domain to the global
  1292. * protection domain list
  1293. */
  1294. static void del_domain_from_list(struct protection_domain *domain)
  1295. {
  1296. unsigned long flags;
  1297. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1298. list_del(&domain->list);
  1299. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1300. }
  1301. static u16 domain_id_alloc(void)
  1302. {
  1303. int id;
  1304. spin_lock(&pd_bitmap_lock);
  1305. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1306. BUG_ON(id == 0);
  1307. if (id > 0 && id < MAX_DOMAIN_ID)
  1308. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1309. else
  1310. id = 0;
  1311. spin_unlock(&pd_bitmap_lock);
  1312. return id;
  1313. }
  1314. static void domain_id_free(int id)
  1315. {
  1316. spin_lock(&pd_bitmap_lock);
  1317. if (id > 0 && id < MAX_DOMAIN_ID)
  1318. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1319. spin_unlock(&pd_bitmap_lock);
  1320. }
  1321. #define DEFINE_FREE_PT_FN(LVL, FN) \
  1322. static void free_pt_##LVL (unsigned long __pt) \
  1323. { \
  1324. unsigned long p; \
  1325. u64 *pt; \
  1326. int i; \
  1327. \
  1328. pt = (u64 *)__pt; \
  1329. \
  1330. for (i = 0; i < 512; ++i) { \
  1331. /* PTE present? */ \
  1332. if (!IOMMU_PTE_PRESENT(pt[i])) \
  1333. continue; \
  1334. \
  1335. /* Large PTE? */ \
  1336. if (PM_PTE_LEVEL(pt[i]) == 0 || \
  1337. PM_PTE_LEVEL(pt[i]) == 7) \
  1338. continue; \
  1339. \
  1340. p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
  1341. FN(p); \
  1342. } \
  1343. free_page((unsigned long)pt); \
  1344. }
  1345. DEFINE_FREE_PT_FN(l2, free_page)
  1346. DEFINE_FREE_PT_FN(l3, free_pt_l2)
  1347. DEFINE_FREE_PT_FN(l4, free_pt_l3)
  1348. DEFINE_FREE_PT_FN(l5, free_pt_l4)
  1349. DEFINE_FREE_PT_FN(l6, free_pt_l5)
  1350. static void free_pagetable(struct protection_domain *domain)
  1351. {
  1352. unsigned long root = (unsigned long)domain->pt_root;
  1353. switch (domain->mode) {
  1354. case PAGE_MODE_NONE:
  1355. break;
  1356. case PAGE_MODE_1_LEVEL:
  1357. free_page(root);
  1358. break;
  1359. case PAGE_MODE_2_LEVEL:
  1360. free_pt_l2(root);
  1361. break;
  1362. case PAGE_MODE_3_LEVEL:
  1363. free_pt_l3(root);
  1364. break;
  1365. case PAGE_MODE_4_LEVEL:
  1366. free_pt_l4(root);
  1367. break;
  1368. case PAGE_MODE_5_LEVEL:
  1369. free_pt_l5(root);
  1370. break;
  1371. case PAGE_MODE_6_LEVEL:
  1372. free_pt_l6(root);
  1373. break;
  1374. default:
  1375. BUG();
  1376. }
  1377. }
  1378. static void free_gcr3_tbl_level1(u64 *tbl)
  1379. {
  1380. u64 *ptr;
  1381. int i;
  1382. for (i = 0; i < 512; ++i) {
  1383. if (!(tbl[i] & GCR3_VALID))
  1384. continue;
  1385. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1386. free_page((unsigned long)ptr);
  1387. }
  1388. }
  1389. static void free_gcr3_tbl_level2(u64 *tbl)
  1390. {
  1391. u64 *ptr;
  1392. int i;
  1393. for (i = 0; i < 512; ++i) {
  1394. if (!(tbl[i] & GCR3_VALID))
  1395. continue;
  1396. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1397. free_gcr3_tbl_level1(ptr);
  1398. }
  1399. }
  1400. static void free_gcr3_table(struct protection_domain *domain)
  1401. {
  1402. if (domain->glx == 2)
  1403. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1404. else if (domain->glx == 1)
  1405. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1406. else
  1407. BUG_ON(domain->glx != 0);
  1408. free_page((unsigned long)domain->gcr3_tbl);
  1409. }
  1410. static void dma_ops_domain_flush_tlb(struct dma_ops_domain *dom)
  1411. {
  1412. domain_flush_tlb(&dom->domain);
  1413. domain_flush_complete(&dom->domain);
  1414. }
  1415. static void iova_domain_flush_tlb(struct iova_domain *iovad)
  1416. {
  1417. struct dma_ops_domain *dom;
  1418. dom = container_of(iovad, struct dma_ops_domain, iovad);
  1419. dma_ops_domain_flush_tlb(dom);
  1420. }
  1421. /*
  1422. * Free a domain, only used if something went wrong in the
  1423. * allocation path and we need to free an already allocated page table
  1424. */
  1425. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1426. {
  1427. if (!dom)
  1428. return;
  1429. del_domain_from_list(&dom->domain);
  1430. put_iova_domain(&dom->iovad);
  1431. free_pagetable(&dom->domain);
  1432. if (dom->domain.id)
  1433. domain_id_free(dom->domain.id);
  1434. kfree(dom);
  1435. }
  1436. /*
  1437. * Allocates a new protection domain usable for the dma_ops functions.
  1438. * It also initializes the page table and the address allocator data
  1439. * structures required for the dma_ops interface
  1440. */
  1441. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1442. {
  1443. struct dma_ops_domain *dma_dom;
  1444. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1445. if (!dma_dom)
  1446. return NULL;
  1447. if (protection_domain_init(&dma_dom->domain))
  1448. goto free_dma_dom;
  1449. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  1450. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1451. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1452. if (!dma_dom->domain.pt_root)
  1453. goto free_dma_dom;
  1454. init_iova_domain(&dma_dom->iovad, PAGE_SIZE, IOVA_START_PFN);
  1455. if (init_iova_flush_queue(&dma_dom->iovad, iova_domain_flush_tlb, NULL))
  1456. goto free_dma_dom;
  1457. /* Initialize reserved ranges */
  1458. copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
  1459. add_domain_to_list(&dma_dom->domain);
  1460. return dma_dom;
  1461. free_dma_dom:
  1462. dma_ops_domain_free(dma_dom);
  1463. return NULL;
  1464. }
  1465. /*
  1466. * little helper function to check whether a given protection domain is a
  1467. * dma_ops domain
  1468. */
  1469. static bool dma_ops_domain(struct protection_domain *domain)
  1470. {
  1471. return domain->flags & PD_DMA_OPS_MASK;
  1472. }
  1473. static void set_dte_entry(u16 devid, struct protection_domain *domain,
  1474. bool ats, bool ppr)
  1475. {
  1476. u64 pte_root = 0;
  1477. u64 flags = 0;
  1478. u32 old_domid;
  1479. if (domain->mode != PAGE_MODE_NONE)
  1480. pte_root = iommu_virt_to_phys(domain->pt_root);
  1481. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1482. << DEV_ENTRY_MODE_SHIFT;
  1483. pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
  1484. flags = amd_iommu_dev_table[devid].data[1];
  1485. if (ats)
  1486. flags |= DTE_FLAG_IOTLB;
  1487. if (ppr) {
  1488. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  1489. if (iommu_feature(iommu, FEATURE_EPHSUP))
  1490. pte_root |= 1ULL << DEV_ENTRY_PPR;
  1491. }
  1492. if (domain->flags & PD_IOMMUV2_MASK) {
  1493. u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
  1494. u64 glx = domain->glx;
  1495. u64 tmp;
  1496. pte_root |= DTE_FLAG_GV;
  1497. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1498. /* First mask out possible old values for GCR3 table */
  1499. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1500. flags &= ~tmp;
  1501. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1502. flags &= ~tmp;
  1503. /* Encode GCR3 table into DTE */
  1504. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1505. pte_root |= tmp;
  1506. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1507. flags |= tmp;
  1508. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1509. flags |= tmp;
  1510. }
  1511. flags &= ~DEV_DOMID_MASK;
  1512. flags |= domain->id;
  1513. old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
  1514. amd_iommu_dev_table[devid].data[1] = flags;
  1515. amd_iommu_dev_table[devid].data[0] = pte_root;
  1516. /*
  1517. * A kdump kernel might be replacing a domain ID that was copied from
  1518. * the previous kernel--if so, it needs to flush the translation cache
  1519. * entries for the old domain ID that is being overwritten
  1520. */
  1521. if (old_domid) {
  1522. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  1523. amd_iommu_flush_tlb_domid(iommu, old_domid);
  1524. }
  1525. }
  1526. static void clear_dte_entry(u16 devid)
  1527. {
  1528. /* remove entry from the device table seen by the hardware */
  1529. amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
  1530. amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
  1531. amd_iommu_apply_erratum_63(devid);
  1532. }
  1533. static void do_attach(struct iommu_dev_data *dev_data,
  1534. struct protection_domain *domain)
  1535. {
  1536. struct amd_iommu *iommu;
  1537. u16 alias;
  1538. bool ats;
  1539. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1540. alias = dev_data->alias;
  1541. ats = dev_data->ats.enabled;
  1542. /* Update data structures */
  1543. dev_data->domain = domain;
  1544. list_add(&dev_data->list, &domain->dev_list);
  1545. /* Do reference counting */
  1546. domain->dev_iommu[iommu->index] += 1;
  1547. domain->dev_cnt += 1;
  1548. /* Update device table */
  1549. set_dte_entry(dev_data->devid, domain, ats, dev_data->iommu_v2);
  1550. if (alias != dev_data->devid)
  1551. set_dte_entry(alias, domain, ats, dev_data->iommu_v2);
  1552. device_flush_dte(dev_data);
  1553. }
  1554. static void do_detach(struct iommu_dev_data *dev_data)
  1555. {
  1556. struct protection_domain *domain = dev_data->domain;
  1557. struct amd_iommu *iommu;
  1558. u16 alias;
  1559. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1560. alias = dev_data->alias;
  1561. /* Update data structures */
  1562. dev_data->domain = NULL;
  1563. list_del(&dev_data->list);
  1564. clear_dte_entry(dev_data->devid);
  1565. if (alias != dev_data->devid)
  1566. clear_dte_entry(alias);
  1567. /* Flush the DTE entry */
  1568. device_flush_dte(dev_data);
  1569. /* Flush IOTLB */
  1570. domain_flush_tlb_pde(domain);
  1571. /* Wait for the flushes to finish */
  1572. domain_flush_complete(domain);
  1573. /* decrease reference counters - needs to happen after the flushes */
  1574. domain->dev_iommu[iommu->index] -= 1;
  1575. domain->dev_cnt -= 1;
  1576. }
  1577. /*
  1578. * If a device is not yet associated with a domain, this function makes the
  1579. * device visible in the domain
  1580. */
  1581. static int __attach_device(struct iommu_dev_data *dev_data,
  1582. struct protection_domain *domain)
  1583. {
  1584. int ret;
  1585. /* lock domain */
  1586. spin_lock(&domain->lock);
  1587. ret = -EBUSY;
  1588. if (dev_data->domain != NULL)
  1589. goto out_unlock;
  1590. /* Attach alias group root */
  1591. do_attach(dev_data, domain);
  1592. ret = 0;
  1593. out_unlock:
  1594. /* ready */
  1595. spin_unlock(&domain->lock);
  1596. return ret;
  1597. }
  1598. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1599. {
  1600. pci_disable_ats(pdev);
  1601. pci_disable_pri(pdev);
  1602. pci_disable_pasid(pdev);
  1603. }
  1604. /* FIXME: Change generic reset-function to do the same */
  1605. static int pri_reset_while_enabled(struct pci_dev *pdev)
  1606. {
  1607. u16 control;
  1608. int pos;
  1609. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1610. if (!pos)
  1611. return -EINVAL;
  1612. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  1613. control |= PCI_PRI_CTRL_RESET;
  1614. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  1615. return 0;
  1616. }
  1617. static int pdev_iommuv2_enable(struct pci_dev *pdev)
  1618. {
  1619. bool reset_enable;
  1620. int reqs, ret;
  1621. /* FIXME: Hardcode number of outstanding requests for now */
  1622. reqs = 32;
  1623. if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
  1624. reqs = 1;
  1625. reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
  1626. /* Only allow access to user-accessible pages */
  1627. ret = pci_enable_pasid(pdev, 0);
  1628. if (ret)
  1629. goto out_err;
  1630. /* First reset the PRI state of the device */
  1631. ret = pci_reset_pri(pdev);
  1632. if (ret)
  1633. goto out_err;
  1634. /* Enable PRI */
  1635. ret = pci_enable_pri(pdev, reqs);
  1636. if (ret)
  1637. goto out_err;
  1638. if (reset_enable) {
  1639. ret = pri_reset_while_enabled(pdev);
  1640. if (ret)
  1641. goto out_err;
  1642. }
  1643. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1644. if (ret)
  1645. goto out_err;
  1646. return 0;
  1647. out_err:
  1648. pci_disable_pri(pdev);
  1649. pci_disable_pasid(pdev);
  1650. return ret;
  1651. }
  1652. /* FIXME: Move this to PCI code */
  1653. #define PCI_PRI_TLP_OFF (1 << 15)
  1654. static bool pci_pri_tlp_required(struct pci_dev *pdev)
  1655. {
  1656. u16 status;
  1657. int pos;
  1658. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1659. if (!pos)
  1660. return false;
  1661. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  1662. return (status & PCI_PRI_TLP_OFF) ? true : false;
  1663. }
  1664. /*
  1665. * If a device is not yet associated with a domain, this function makes the
  1666. * device visible in the domain
  1667. */
  1668. static int attach_device(struct device *dev,
  1669. struct protection_domain *domain)
  1670. {
  1671. struct pci_dev *pdev;
  1672. struct iommu_dev_data *dev_data;
  1673. unsigned long flags;
  1674. int ret;
  1675. dev_data = get_dev_data(dev);
  1676. if (!dev_is_pci(dev))
  1677. goto skip_ats_check;
  1678. pdev = to_pci_dev(dev);
  1679. if (domain->flags & PD_IOMMUV2_MASK) {
  1680. if (!dev_data->passthrough)
  1681. return -EINVAL;
  1682. if (dev_data->iommu_v2) {
  1683. if (pdev_iommuv2_enable(pdev) != 0)
  1684. return -EINVAL;
  1685. dev_data->ats.enabled = true;
  1686. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1687. dev_data->pri_tlp = pci_pri_tlp_required(pdev);
  1688. }
  1689. } else if (amd_iommu_iotlb_sup &&
  1690. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1691. dev_data->ats.enabled = true;
  1692. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1693. }
  1694. skip_ats_check:
  1695. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1696. ret = __attach_device(dev_data, domain);
  1697. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1698. /*
  1699. * We might boot into a crash-kernel here. The crashed kernel
  1700. * left the caches in the IOMMU dirty. So we have to flush
  1701. * here to evict all dirty stuff.
  1702. */
  1703. domain_flush_tlb_pde(domain);
  1704. domain_flush_complete(domain);
  1705. return ret;
  1706. }
  1707. /*
  1708. * Removes a device from a protection domain (unlocked)
  1709. */
  1710. static void __detach_device(struct iommu_dev_data *dev_data)
  1711. {
  1712. struct protection_domain *domain;
  1713. domain = dev_data->domain;
  1714. spin_lock(&domain->lock);
  1715. do_detach(dev_data);
  1716. spin_unlock(&domain->lock);
  1717. }
  1718. /*
  1719. * Removes a device from a protection domain (with devtable_lock held)
  1720. */
  1721. static void detach_device(struct device *dev)
  1722. {
  1723. struct protection_domain *domain;
  1724. struct iommu_dev_data *dev_data;
  1725. unsigned long flags;
  1726. dev_data = get_dev_data(dev);
  1727. domain = dev_data->domain;
  1728. /*
  1729. * First check if the device is still attached. It might already
  1730. * be detached from its domain because the generic
  1731. * iommu_detach_group code detached it and we try again here in
  1732. * our alias handling.
  1733. */
  1734. if (WARN_ON(!dev_data->domain))
  1735. return;
  1736. /* lock device table */
  1737. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1738. __detach_device(dev_data);
  1739. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1740. if (!dev_is_pci(dev))
  1741. return;
  1742. if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
  1743. pdev_iommuv2_disable(to_pci_dev(dev));
  1744. else if (dev_data->ats.enabled)
  1745. pci_disable_ats(to_pci_dev(dev));
  1746. dev_data->ats.enabled = false;
  1747. }
  1748. static int amd_iommu_add_device(struct device *dev)
  1749. {
  1750. struct iommu_dev_data *dev_data;
  1751. struct iommu_domain *domain;
  1752. struct amd_iommu *iommu;
  1753. int ret, devid;
  1754. if (!check_device(dev) || get_dev_data(dev))
  1755. return 0;
  1756. devid = get_device_id(dev);
  1757. if (devid < 0)
  1758. return devid;
  1759. iommu = amd_iommu_rlookup_table[devid];
  1760. ret = iommu_init_device(dev);
  1761. if (ret) {
  1762. if (ret != -ENOTSUPP)
  1763. pr_err("Failed to initialize device %s - trying to proceed anyway\n",
  1764. dev_name(dev));
  1765. iommu_ignore_device(dev);
  1766. dev->dma_ops = &dma_direct_ops;
  1767. goto out;
  1768. }
  1769. init_iommu_group(dev);
  1770. dev_data = get_dev_data(dev);
  1771. BUG_ON(!dev_data);
  1772. if (iommu_pass_through || dev_data->iommu_v2)
  1773. iommu_request_dm_for_dev(dev);
  1774. /* Domains are initialized for this device - have a look what we ended up with */
  1775. domain = iommu_get_domain_for_dev(dev);
  1776. if (domain->type == IOMMU_DOMAIN_IDENTITY)
  1777. dev_data->passthrough = true;
  1778. else
  1779. dev->dma_ops = &amd_iommu_dma_ops;
  1780. out:
  1781. iommu_completion_wait(iommu);
  1782. return 0;
  1783. }
  1784. static void amd_iommu_remove_device(struct device *dev)
  1785. {
  1786. struct amd_iommu *iommu;
  1787. int devid;
  1788. if (!check_device(dev))
  1789. return;
  1790. devid = get_device_id(dev);
  1791. if (devid < 0)
  1792. return;
  1793. iommu = amd_iommu_rlookup_table[devid];
  1794. iommu_uninit_device(dev);
  1795. iommu_completion_wait(iommu);
  1796. }
  1797. static struct iommu_group *amd_iommu_device_group(struct device *dev)
  1798. {
  1799. if (dev_is_pci(dev))
  1800. return pci_device_group(dev);
  1801. return acpihid_device_group(dev);
  1802. }
  1803. /*****************************************************************************
  1804. *
  1805. * The next functions belong to the dma_ops mapping/unmapping code.
  1806. *
  1807. *****************************************************************************/
  1808. /*
  1809. * In the dma_ops path we only have the struct device. This function
  1810. * finds the corresponding IOMMU, the protection domain and the
  1811. * requestor id for a given device.
  1812. * If the device is not yet associated with a domain this is also done
  1813. * in this function.
  1814. */
  1815. static struct protection_domain *get_domain(struct device *dev)
  1816. {
  1817. struct protection_domain *domain;
  1818. struct iommu_domain *io_domain;
  1819. if (!check_device(dev))
  1820. return ERR_PTR(-EINVAL);
  1821. domain = get_dev_data(dev)->domain;
  1822. if (domain == NULL && get_dev_data(dev)->defer_attach) {
  1823. get_dev_data(dev)->defer_attach = false;
  1824. io_domain = iommu_get_domain_for_dev(dev);
  1825. domain = to_pdomain(io_domain);
  1826. attach_device(dev, domain);
  1827. }
  1828. if (domain == NULL)
  1829. return ERR_PTR(-EBUSY);
  1830. if (!dma_ops_domain(domain))
  1831. return ERR_PTR(-EBUSY);
  1832. return domain;
  1833. }
  1834. static void update_device_table(struct protection_domain *domain)
  1835. {
  1836. struct iommu_dev_data *dev_data;
  1837. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1838. set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled,
  1839. dev_data->iommu_v2);
  1840. if (dev_data->devid == dev_data->alias)
  1841. continue;
  1842. /* There is an alias, update device table entry for it */
  1843. set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled,
  1844. dev_data->iommu_v2);
  1845. }
  1846. }
  1847. static void update_domain(struct protection_domain *domain)
  1848. {
  1849. if (!domain->updated)
  1850. return;
  1851. update_device_table(domain);
  1852. domain_flush_devices(domain);
  1853. domain_flush_tlb_pde(domain);
  1854. domain->updated = false;
  1855. }
  1856. static int dir2prot(enum dma_data_direction direction)
  1857. {
  1858. if (direction == DMA_TO_DEVICE)
  1859. return IOMMU_PROT_IR;
  1860. else if (direction == DMA_FROM_DEVICE)
  1861. return IOMMU_PROT_IW;
  1862. else if (direction == DMA_BIDIRECTIONAL)
  1863. return IOMMU_PROT_IW | IOMMU_PROT_IR;
  1864. else
  1865. return 0;
  1866. }
  1867. /*
  1868. * This function contains common code for mapping of a physically
  1869. * contiguous memory region into DMA address space. It is used by all
  1870. * mapping functions provided with this IOMMU driver.
  1871. * Must be called with the domain lock held.
  1872. */
  1873. static dma_addr_t __map_single(struct device *dev,
  1874. struct dma_ops_domain *dma_dom,
  1875. phys_addr_t paddr,
  1876. size_t size,
  1877. enum dma_data_direction direction,
  1878. u64 dma_mask)
  1879. {
  1880. dma_addr_t offset = paddr & ~PAGE_MASK;
  1881. dma_addr_t address, start, ret;
  1882. unsigned int pages;
  1883. int prot = 0;
  1884. int i;
  1885. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1886. paddr &= PAGE_MASK;
  1887. address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
  1888. if (address == AMD_IOMMU_MAPPING_ERROR)
  1889. goto out;
  1890. prot = dir2prot(direction);
  1891. start = address;
  1892. for (i = 0; i < pages; ++i) {
  1893. ret = iommu_map_page(&dma_dom->domain, start, paddr,
  1894. PAGE_SIZE, prot, GFP_ATOMIC);
  1895. if (ret)
  1896. goto out_unmap;
  1897. paddr += PAGE_SIZE;
  1898. start += PAGE_SIZE;
  1899. }
  1900. address += offset;
  1901. if (unlikely(amd_iommu_np_cache)) {
  1902. domain_flush_pages(&dma_dom->domain, address, size);
  1903. domain_flush_complete(&dma_dom->domain);
  1904. }
  1905. out:
  1906. return address;
  1907. out_unmap:
  1908. for (--i; i >= 0; --i) {
  1909. start -= PAGE_SIZE;
  1910. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1911. }
  1912. domain_flush_tlb(&dma_dom->domain);
  1913. domain_flush_complete(&dma_dom->domain);
  1914. dma_ops_free_iova(dma_dom, address, pages);
  1915. return AMD_IOMMU_MAPPING_ERROR;
  1916. }
  1917. /*
  1918. * Does the reverse of the __map_single function. Must be called with
  1919. * the domain lock held too
  1920. */
  1921. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1922. dma_addr_t dma_addr,
  1923. size_t size,
  1924. int dir)
  1925. {
  1926. dma_addr_t i, start;
  1927. unsigned int pages;
  1928. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1929. dma_addr &= PAGE_MASK;
  1930. start = dma_addr;
  1931. for (i = 0; i < pages; ++i) {
  1932. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1933. start += PAGE_SIZE;
  1934. }
  1935. if (amd_iommu_unmap_flush) {
  1936. domain_flush_tlb(&dma_dom->domain);
  1937. domain_flush_complete(&dma_dom->domain);
  1938. dma_ops_free_iova(dma_dom, dma_addr, pages);
  1939. } else {
  1940. pages = __roundup_pow_of_two(pages);
  1941. queue_iova(&dma_dom->iovad, dma_addr >> PAGE_SHIFT, pages, 0);
  1942. }
  1943. }
  1944. /*
  1945. * The exported map_single function for dma_ops.
  1946. */
  1947. static dma_addr_t map_page(struct device *dev, struct page *page,
  1948. unsigned long offset, size_t size,
  1949. enum dma_data_direction dir,
  1950. unsigned long attrs)
  1951. {
  1952. phys_addr_t paddr = page_to_phys(page) + offset;
  1953. struct protection_domain *domain;
  1954. struct dma_ops_domain *dma_dom;
  1955. u64 dma_mask;
  1956. domain = get_domain(dev);
  1957. if (PTR_ERR(domain) == -EINVAL)
  1958. return (dma_addr_t)paddr;
  1959. else if (IS_ERR(domain))
  1960. return AMD_IOMMU_MAPPING_ERROR;
  1961. dma_mask = *dev->dma_mask;
  1962. dma_dom = to_dma_ops_domain(domain);
  1963. return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
  1964. }
  1965. /*
  1966. * The exported unmap_single function for dma_ops.
  1967. */
  1968. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1969. enum dma_data_direction dir, unsigned long attrs)
  1970. {
  1971. struct protection_domain *domain;
  1972. struct dma_ops_domain *dma_dom;
  1973. domain = get_domain(dev);
  1974. if (IS_ERR(domain))
  1975. return;
  1976. dma_dom = to_dma_ops_domain(domain);
  1977. __unmap_single(dma_dom, dma_addr, size, dir);
  1978. }
  1979. static int sg_num_pages(struct device *dev,
  1980. struct scatterlist *sglist,
  1981. int nelems)
  1982. {
  1983. unsigned long mask, boundary_size;
  1984. struct scatterlist *s;
  1985. int i, npages = 0;
  1986. mask = dma_get_seg_boundary(dev);
  1987. boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
  1988. 1UL << (BITS_PER_LONG - PAGE_SHIFT);
  1989. for_each_sg(sglist, s, nelems, i) {
  1990. int p, n;
  1991. s->dma_address = npages << PAGE_SHIFT;
  1992. p = npages % boundary_size;
  1993. n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  1994. if (p + n > boundary_size)
  1995. npages += boundary_size - p;
  1996. npages += n;
  1997. }
  1998. return npages;
  1999. }
  2000. /*
  2001. * The exported map_sg function for dma_ops (handles scatter-gather
  2002. * lists).
  2003. */
  2004. static int map_sg(struct device *dev, struct scatterlist *sglist,
  2005. int nelems, enum dma_data_direction direction,
  2006. unsigned long attrs)
  2007. {
  2008. int mapped_pages = 0, npages = 0, prot = 0, i;
  2009. struct protection_domain *domain;
  2010. struct dma_ops_domain *dma_dom;
  2011. struct scatterlist *s;
  2012. unsigned long address;
  2013. u64 dma_mask;
  2014. domain = get_domain(dev);
  2015. if (IS_ERR(domain))
  2016. return 0;
  2017. dma_dom = to_dma_ops_domain(domain);
  2018. dma_mask = *dev->dma_mask;
  2019. npages = sg_num_pages(dev, sglist, nelems);
  2020. address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
  2021. if (address == AMD_IOMMU_MAPPING_ERROR)
  2022. goto out_err;
  2023. prot = dir2prot(direction);
  2024. /* Map all sg entries */
  2025. for_each_sg(sglist, s, nelems, i) {
  2026. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  2027. for (j = 0; j < pages; ++j) {
  2028. unsigned long bus_addr, phys_addr;
  2029. int ret;
  2030. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  2031. phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
  2032. ret = iommu_map_page(domain, bus_addr, phys_addr,
  2033. PAGE_SIZE, prot,
  2034. GFP_ATOMIC | __GFP_NOWARN);
  2035. if (ret)
  2036. goto out_unmap;
  2037. mapped_pages += 1;
  2038. }
  2039. }
  2040. /* Everything is mapped - write the right values into s->dma_address */
  2041. for_each_sg(sglist, s, nelems, i) {
  2042. /*
  2043. * Add in the remaining piece of the scatter-gather offset that
  2044. * was masked out when we were determining the physical address
  2045. * via (sg_phys(s) & PAGE_MASK) earlier.
  2046. */
  2047. s->dma_address += address + (s->offset & ~PAGE_MASK);
  2048. s->dma_length = s->length;
  2049. }
  2050. return nelems;
  2051. out_unmap:
  2052. pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
  2053. dev_name(dev), npages);
  2054. for_each_sg(sglist, s, nelems, i) {
  2055. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  2056. for (j = 0; j < pages; ++j) {
  2057. unsigned long bus_addr;
  2058. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  2059. iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
  2060. if (--mapped_pages == 0)
  2061. goto out_free_iova;
  2062. }
  2063. }
  2064. out_free_iova:
  2065. free_iova_fast(&dma_dom->iovad, address >> PAGE_SHIFT, npages);
  2066. out_err:
  2067. return 0;
  2068. }
  2069. /*
  2070. * The exported map_sg function for dma_ops (handles scatter-gather
  2071. * lists).
  2072. */
  2073. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  2074. int nelems, enum dma_data_direction dir,
  2075. unsigned long attrs)
  2076. {
  2077. struct protection_domain *domain;
  2078. struct dma_ops_domain *dma_dom;
  2079. unsigned long startaddr;
  2080. int npages = 2;
  2081. domain = get_domain(dev);
  2082. if (IS_ERR(domain))
  2083. return;
  2084. startaddr = sg_dma_address(sglist) & PAGE_MASK;
  2085. dma_dom = to_dma_ops_domain(domain);
  2086. npages = sg_num_pages(dev, sglist, nelems);
  2087. __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
  2088. }
  2089. /*
  2090. * The exported alloc_coherent function for dma_ops.
  2091. */
  2092. static void *alloc_coherent(struct device *dev, size_t size,
  2093. dma_addr_t *dma_addr, gfp_t flag,
  2094. unsigned long attrs)
  2095. {
  2096. u64 dma_mask = dev->coherent_dma_mask;
  2097. struct protection_domain *domain;
  2098. struct dma_ops_domain *dma_dom;
  2099. struct page *page;
  2100. domain = get_domain(dev);
  2101. if (PTR_ERR(domain) == -EINVAL) {
  2102. page = alloc_pages(flag, get_order(size));
  2103. *dma_addr = page_to_phys(page);
  2104. return page_address(page);
  2105. } else if (IS_ERR(domain))
  2106. return NULL;
  2107. dma_dom = to_dma_ops_domain(domain);
  2108. size = PAGE_ALIGN(size);
  2109. dma_mask = dev->coherent_dma_mask;
  2110. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  2111. flag |= __GFP_ZERO;
  2112. page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
  2113. if (!page) {
  2114. if (!gfpflags_allow_blocking(flag))
  2115. return NULL;
  2116. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  2117. get_order(size), flag & __GFP_NOWARN);
  2118. if (!page)
  2119. return NULL;
  2120. }
  2121. if (!dma_mask)
  2122. dma_mask = *dev->dma_mask;
  2123. *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
  2124. size, DMA_BIDIRECTIONAL, dma_mask);
  2125. if (*dma_addr == AMD_IOMMU_MAPPING_ERROR)
  2126. goto out_free;
  2127. return page_address(page);
  2128. out_free:
  2129. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2130. __free_pages(page, get_order(size));
  2131. return NULL;
  2132. }
  2133. /*
  2134. * The exported free_coherent function for dma_ops.
  2135. */
  2136. static void free_coherent(struct device *dev, size_t size,
  2137. void *virt_addr, dma_addr_t dma_addr,
  2138. unsigned long attrs)
  2139. {
  2140. struct protection_domain *domain;
  2141. struct dma_ops_domain *dma_dom;
  2142. struct page *page;
  2143. page = virt_to_page(virt_addr);
  2144. size = PAGE_ALIGN(size);
  2145. domain = get_domain(dev);
  2146. if (IS_ERR(domain))
  2147. goto free_mem;
  2148. dma_dom = to_dma_ops_domain(domain);
  2149. __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
  2150. free_mem:
  2151. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2152. __free_pages(page, get_order(size));
  2153. }
  2154. /*
  2155. * This function is called by the DMA layer to find out if we can handle a
  2156. * particular device. It is part of the dma_ops.
  2157. */
  2158. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  2159. {
  2160. if (!dma_direct_supported(dev, mask))
  2161. return 0;
  2162. return check_device(dev);
  2163. }
  2164. static int amd_iommu_mapping_error(struct device *dev, dma_addr_t dma_addr)
  2165. {
  2166. return dma_addr == AMD_IOMMU_MAPPING_ERROR;
  2167. }
  2168. static const struct dma_map_ops amd_iommu_dma_ops = {
  2169. .alloc = alloc_coherent,
  2170. .free = free_coherent,
  2171. .map_page = map_page,
  2172. .unmap_page = unmap_page,
  2173. .map_sg = map_sg,
  2174. .unmap_sg = unmap_sg,
  2175. .dma_supported = amd_iommu_dma_supported,
  2176. .mapping_error = amd_iommu_mapping_error,
  2177. };
  2178. static int init_reserved_iova_ranges(void)
  2179. {
  2180. struct pci_dev *pdev = NULL;
  2181. struct iova *val;
  2182. init_iova_domain(&reserved_iova_ranges, PAGE_SIZE, IOVA_START_PFN);
  2183. lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
  2184. &reserved_rbtree_key);
  2185. /* MSI memory range */
  2186. val = reserve_iova(&reserved_iova_ranges,
  2187. IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
  2188. if (!val) {
  2189. pr_err("Reserving MSI range failed\n");
  2190. return -ENOMEM;
  2191. }
  2192. /* HT memory range */
  2193. val = reserve_iova(&reserved_iova_ranges,
  2194. IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
  2195. if (!val) {
  2196. pr_err("Reserving HT range failed\n");
  2197. return -ENOMEM;
  2198. }
  2199. /*
  2200. * Memory used for PCI resources
  2201. * FIXME: Check whether we can reserve the PCI-hole completly
  2202. */
  2203. for_each_pci_dev(pdev) {
  2204. int i;
  2205. for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
  2206. struct resource *r = &pdev->resource[i];
  2207. if (!(r->flags & IORESOURCE_MEM))
  2208. continue;
  2209. val = reserve_iova(&reserved_iova_ranges,
  2210. IOVA_PFN(r->start),
  2211. IOVA_PFN(r->end));
  2212. if (!val) {
  2213. pr_err("Reserve pci-resource range failed\n");
  2214. return -ENOMEM;
  2215. }
  2216. }
  2217. }
  2218. return 0;
  2219. }
  2220. int __init amd_iommu_init_api(void)
  2221. {
  2222. int ret, err = 0;
  2223. ret = iova_cache_get();
  2224. if (ret)
  2225. return ret;
  2226. ret = init_reserved_iova_ranges();
  2227. if (ret)
  2228. return ret;
  2229. err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
  2230. if (err)
  2231. return err;
  2232. #ifdef CONFIG_ARM_AMBA
  2233. err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
  2234. if (err)
  2235. return err;
  2236. #endif
  2237. err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
  2238. if (err)
  2239. return err;
  2240. return 0;
  2241. }
  2242. int __init amd_iommu_init_dma_ops(void)
  2243. {
  2244. swiotlb = (iommu_pass_through || sme_me_mask) ? 1 : 0;
  2245. iommu_detected = 1;
  2246. /*
  2247. * In case we don't initialize SWIOTLB (actually the common case
  2248. * when AMD IOMMU is enabled and SME is not active), make sure there
  2249. * are global dma_ops set as a fall-back for devices not handled by
  2250. * this driver (for example non-PCI devices). When SME is active,
  2251. * make sure that swiotlb variable remains set so the global dma_ops
  2252. * continue to be SWIOTLB.
  2253. */
  2254. if (!swiotlb)
  2255. dma_ops = &dma_direct_ops;
  2256. if (amd_iommu_unmap_flush)
  2257. pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
  2258. else
  2259. pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
  2260. return 0;
  2261. }
  2262. /*****************************************************************************
  2263. *
  2264. * The following functions belong to the exported interface of AMD IOMMU
  2265. *
  2266. * This interface allows access to lower level functions of the IOMMU
  2267. * like protection domain handling and assignement of devices to domains
  2268. * which is not possible with the dma_ops interface.
  2269. *
  2270. *****************************************************************************/
  2271. static void cleanup_domain(struct protection_domain *domain)
  2272. {
  2273. struct iommu_dev_data *entry;
  2274. unsigned long flags;
  2275. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2276. while (!list_empty(&domain->dev_list)) {
  2277. entry = list_first_entry(&domain->dev_list,
  2278. struct iommu_dev_data, list);
  2279. BUG_ON(!entry->domain);
  2280. __detach_device(entry);
  2281. }
  2282. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2283. }
  2284. static void protection_domain_free(struct protection_domain *domain)
  2285. {
  2286. if (!domain)
  2287. return;
  2288. del_domain_from_list(domain);
  2289. if (domain->id)
  2290. domain_id_free(domain->id);
  2291. kfree(domain);
  2292. }
  2293. static int protection_domain_init(struct protection_domain *domain)
  2294. {
  2295. spin_lock_init(&domain->lock);
  2296. mutex_init(&domain->api_lock);
  2297. domain->id = domain_id_alloc();
  2298. if (!domain->id)
  2299. return -ENOMEM;
  2300. INIT_LIST_HEAD(&domain->dev_list);
  2301. return 0;
  2302. }
  2303. static struct protection_domain *protection_domain_alloc(void)
  2304. {
  2305. struct protection_domain *domain;
  2306. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  2307. if (!domain)
  2308. return NULL;
  2309. if (protection_domain_init(domain))
  2310. goto out_err;
  2311. add_domain_to_list(domain);
  2312. return domain;
  2313. out_err:
  2314. kfree(domain);
  2315. return NULL;
  2316. }
  2317. static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
  2318. {
  2319. struct protection_domain *pdomain;
  2320. struct dma_ops_domain *dma_domain;
  2321. switch (type) {
  2322. case IOMMU_DOMAIN_UNMANAGED:
  2323. pdomain = protection_domain_alloc();
  2324. if (!pdomain)
  2325. return NULL;
  2326. pdomain->mode = PAGE_MODE_3_LEVEL;
  2327. pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  2328. if (!pdomain->pt_root) {
  2329. protection_domain_free(pdomain);
  2330. return NULL;
  2331. }
  2332. pdomain->domain.geometry.aperture_start = 0;
  2333. pdomain->domain.geometry.aperture_end = ~0ULL;
  2334. pdomain->domain.geometry.force_aperture = true;
  2335. break;
  2336. case IOMMU_DOMAIN_DMA:
  2337. dma_domain = dma_ops_domain_alloc();
  2338. if (!dma_domain) {
  2339. pr_err("AMD-Vi: Failed to allocate\n");
  2340. return NULL;
  2341. }
  2342. pdomain = &dma_domain->domain;
  2343. break;
  2344. case IOMMU_DOMAIN_IDENTITY:
  2345. pdomain = protection_domain_alloc();
  2346. if (!pdomain)
  2347. return NULL;
  2348. pdomain->mode = PAGE_MODE_NONE;
  2349. break;
  2350. default:
  2351. return NULL;
  2352. }
  2353. return &pdomain->domain;
  2354. }
  2355. static void amd_iommu_domain_free(struct iommu_domain *dom)
  2356. {
  2357. struct protection_domain *domain;
  2358. struct dma_ops_domain *dma_dom;
  2359. domain = to_pdomain(dom);
  2360. if (domain->dev_cnt > 0)
  2361. cleanup_domain(domain);
  2362. BUG_ON(domain->dev_cnt != 0);
  2363. if (!dom)
  2364. return;
  2365. switch (dom->type) {
  2366. case IOMMU_DOMAIN_DMA:
  2367. /* Now release the domain */
  2368. dma_dom = to_dma_ops_domain(domain);
  2369. dma_ops_domain_free(dma_dom);
  2370. break;
  2371. default:
  2372. if (domain->mode != PAGE_MODE_NONE)
  2373. free_pagetable(domain);
  2374. if (domain->flags & PD_IOMMUV2_MASK)
  2375. free_gcr3_table(domain);
  2376. protection_domain_free(domain);
  2377. break;
  2378. }
  2379. }
  2380. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2381. struct device *dev)
  2382. {
  2383. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2384. struct amd_iommu *iommu;
  2385. int devid;
  2386. if (!check_device(dev))
  2387. return;
  2388. devid = get_device_id(dev);
  2389. if (devid < 0)
  2390. return;
  2391. if (dev_data->domain != NULL)
  2392. detach_device(dev);
  2393. iommu = amd_iommu_rlookup_table[devid];
  2394. if (!iommu)
  2395. return;
  2396. #ifdef CONFIG_IRQ_REMAP
  2397. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  2398. (dom->type == IOMMU_DOMAIN_UNMANAGED))
  2399. dev_data->use_vapic = 0;
  2400. #endif
  2401. iommu_completion_wait(iommu);
  2402. }
  2403. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2404. struct device *dev)
  2405. {
  2406. struct protection_domain *domain = to_pdomain(dom);
  2407. struct iommu_dev_data *dev_data;
  2408. struct amd_iommu *iommu;
  2409. int ret;
  2410. if (!check_device(dev))
  2411. return -EINVAL;
  2412. dev_data = dev->archdata.iommu;
  2413. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2414. if (!iommu)
  2415. return -EINVAL;
  2416. if (dev_data->domain)
  2417. detach_device(dev);
  2418. ret = attach_device(dev, domain);
  2419. #ifdef CONFIG_IRQ_REMAP
  2420. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  2421. if (dom->type == IOMMU_DOMAIN_UNMANAGED)
  2422. dev_data->use_vapic = 1;
  2423. else
  2424. dev_data->use_vapic = 0;
  2425. }
  2426. #endif
  2427. iommu_completion_wait(iommu);
  2428. return ret;
  2429. }
  2430. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2431. phys_addr_t paddr, size_t page_size, int iommu_prot)
  2432. {
  2433. struct protection_domain *domain = to_pdomain(dom);
  2434. int prot = 0;
  2435. int ret;
  2436. if (domain->mode == PAGE_MODE_NONE)
  2437. return -EINVAL;
  2438. if (iommu_prot & IOMMU_READ)
  2439. prot |= IOMMU_PROT_IR;
  2440. if (iommu_prot & IOMMU_WRITE)
  2441. prot |= IOMMU_PROT_IW;
  2442. mutex_lock(&domain->api_lock);
  2443. ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
  2444. mutex_unlock(&domain->api_lock);
  2445. return ret;
  2446. }
  2447. static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2448. size_t page_size)
  2449. {
  2450. struct protection_domain *domain = to_pdomain(dom);
  2451. size_t unmap_size;
  2452. if (domain->mode == PAGE_MODE_NONE)
  2453. return 0;
  2454. mutex_lock(&domain->api_lock);
  2455. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2456. mutex_unlock(&domain->api_lock);
  2457. return unmap_size;
  2458. }
  2459. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2460. dma_addr_t iova)
  2461. {
  2462. struct protection_domain *domain = to_pdomain(dom);
  2463. unsigned long offset_mask, pte_pgsize;
  2464. u64 *pte, __pte;
  2465. if (domain->mode == PAGE_MODE_NONE)
  2466. return iova;
  2467. pte = fetch_pte(domain, iova, &pte_pgsize);
  2468. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2469. return 0;
  2470. offset_mask = pte_pgsize - 1;
  2471. __pte = __sme_clr(*pte & PM_ADDR_MASK);
  2472. return (__pte & ~offset_mask) | (iova & offset_mask);
  2473. }
  2474. static bool amd_iommu_capable(enum iommu_cap cap)
  2475. {
  2476. switch (cap) {
  2477. case IOMMU_CAP_CACHE_COHERENCY:
  2478. return true;
  2479. case IOMMU_CAP_INTR_REMAP:
  2480. return (irq_remapping_enabled == 1);
  2481. case IOMMU_CAP_NOEXEC:
  2482. return false;
  2483. }
  2484. return false;
  2485. }
  2486. static void amd_iommu_get_resv_regions(struct device *dev,
  2487. struct list_head *head)
  2488. {
  2489. struct iommu_resv_region *region;
  2490. struct unity_map_entry *entry;
  2491. int devid;
  2492. devid = get_device_id(dev);
  2493. if (devid < 0)
  2494. return;
  2495. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  2496. int type, prot = 0;
  2497. size_t length;
  2498. if (devid < entry->devid_start || devid > entry->devid_end)
  2499. continue;
  2500. type = IOMMU_RESV_DIRECT;
  2501. length = entry->address_end - entry->address_start;
  2502. if (entry->prot & IOMMU_PROT_IR)
  2503. prot |= IOMMU_READ;
  2504. if (entry->prot & IOMMU_PROT_IW)
  2505. prot |= IOMMU_WRITE;
  2506. if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
  2507. /* Exclusion range */
  2508. type = IOMMU_RESV_RESERVED;
  2509. region = iommu_alloc_resv_region(entry->address_start,
  2510. length, prot, type);
  2511. if (!region) {
  2512. pr_err("Out of memory allocating dm-regions for %s\n",
  2513. dev_name(dev));
  2514. return;
  2515. }
  2516. list_add_tail(&region->list, head);
  2517. }
  2518. region = iommu_alloc_resv_region(MSI_RANGE_START,
  2519. MSI_RANGE_END - MSI_RANGE_START + 1,
  2520. 0, IOMMU_RESV_MSI);
  2521. if (!region)
  2522. return;
  2523. list_add_tail(&region->list, head);
  2524. region = iommu_alloc_resv_region(HT_RANGE_START,
  2525. HT_RANGE_END - HT_RANGE_START + 1,
  2526. 0, IOMMU_RESV_RESERVED);
  2527. if (!region)
  2528. return;
  2529. list_add_tail(&region->list, head);
  2530. }
  2531. static void amd_iommu_put_resv_regions(struct device *dev,
  2532. struct list_head *head)
  2533. {
  2534. struct iommu_resv_region *entry, *next;
  2535. list_for_each_entry_safe(entry, next, head, list)
  2536. kfree(entry);
  2537. }
  2538. static void amd_iommu_apply_resv_region(struct device *dev,
  2539. struct iommu_domain *domain,
  2540. struct iommu_resv_region *region)
  2541. {
  2542. struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
  2543. unsigned long start, end;
  2544. start = IOVA_PFN(region->start);
  2545. end = IOVA_PFN(region->start + region->length - 1);
  2546. WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
  2547. }
  2548. static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
  2549. struct device *dev)
  2550. {
  2551. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2552. return dev_data->defer_attach;
  2553. }
  2554. static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
  2555. {
  2556. struct protection_domain *dom = to_pdomain(domain);
  2557. domain_flush_tlb_pde(dom);
  2558. domain_flush_complete(dom);
  2559. }
  2560. static void amd_iommu_iotlb_range_add(struct iommu_domain *domain,
  2561. unsigned long iova, size_t size)
  2562. {
  2563. }
  2564. const struct iommu_ops amd_iommu_ops = {
  2565. .capable = amd_iommu_capable,
  2566. .domain_alloc = amd_iommu_domain_alloc,
  2567. .domain_free = amd_iommu_domain_free,
  2568. .attach_dev = amd_iommu_attach_device,
  2569. .detach_dev = amd_iommu_detach_device,
  2570. .map = amd_iommu_map,
  2571. .unmap = amd_iommu_unmap,
  2572. .iova_to_phys = amd_iommu_iova_to_phys,
  2573. .add_device = amd_iommu_add_device,
  2574. .remove_device = amd_iommu_remove_device,
  2575. .device_group = amd_iommu_device_group,
  2576. .get_resv_regions = amd_iommu_get_resv_regions,
  2577. .put_resv_regions = amd_iommu_put_resv_regions,
  2578. .apply_resv_region = amd_iommu_apply_resv_region,
  2579. .is_attach_deferred = amd_iommu_is_attach_deferred,
  2580. .pgsize_bitmap = AMD_IOMMU_PGSIZES,
  2581. .flush_iotlb_all = amd_iommu_flush_iotlb_all,
  2582. .iotlb_range_add = amd_iommu_iotlb_range_add,
  2583. .iotlb_sync = amd_iommu_flush_iotlb_all,
  2584. };
  2585. /*****************************************************************************
  2586. *
  2587. * The next functions do a basic initialization of IOMMU for pass through
  2588. * mode
  2589. *
  2590. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2591. * DMA-API translation.
  2592. *
  2593. *****************************************************************************/
  2594. /* IOMMUv2 specific functions */
  2595. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  2596. {
  2597. return atomic_notifier_chain_register(&ppr_notifier, nb);
  2598. }
  2599. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  2600. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  2601. {
  2602. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  2603. }
  2604. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2605. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2606. {
  2607. struct protection_domain *domain = to_pdomain(dom);
  2608. unsigned long flags;
  2609. spin_lock_irqsave(&domain->lock, flags);
  2610. /* Update data structure */
  2611. domain->mode = PAGE_MODE_NONE;
  2612. domain->updated = true;
  2613. /* Make changes visible to IOMMUs */
  2614. update_domain(domain);
  2615. /* Page-table is not visible to IOMMU anymore, so free it */
  2616. free_pagetable(domain);
  2617. spin_unlock_irqrestore(&domain->lock, flags);
  2618. }
  2619. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2620. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2621. {
  2622. struct protection_domain *domain = to_pdomain(dom);
  2623. unsigned long flags;
  2624. int levels, ret;
  2625. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  2626. return -EINVAL;
  2627. /* Number of GCR3 table levels required */
  2628. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2629. levels += 1;
  2630. if (levels > amd_iommu_max_glx_val)
  2631. return -EINVAL;
  2632. spin_lock_irqsave(&domain->lock, flags);
  2633. /*
  2634. * Save us all sanity checks whether devices already in the
  2635. * domain support IOMMUv2. Just force that the domain has no
  2636. * devices attached when it is switched into IOMMUv2 mode.
  2637. */
  2638. ret = -EBUSY;
  2639. if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
  2640. goto out;
  2641. ret = -ENOMEM;
  2642. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2643. if (domain->gcr3_tbl == NULL)
  2644. goto out;
  2645. domain->glx = levels;
  2646. domain->flags |= PD_IOMMUV2_MASK;
  2647. domain->updated = true;
  2648. update_domain(domain);
  2649. ret = 0;
  2650. out:
  2651. spin_unlock_irqrestore(&domain->lock, flags);
  2652. return ret;
  2653. }
  2654. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2655. static int __flush_pasid(struct protection_domain *domain, int pasid,
  2656. u64 address, bool size)
  2657. {
  2658. struct iommu_dev_data *dev_data;
  2659. struct iommu_cmd cmd;
  2660. int i, ret;
  2661. if (!(domain->flags & PD_IOMMUV2_MASK))
  2662. return -EINVAL;
  2663. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2664. /*
  2665. * IOMMU TLB needs to be flushed before Device TLB to
  2666. * prevent device TLB refill from IOMMU TLB
  2667. */
  2668. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  2669. if (domain->dev_iommu[i] == 0)
  2670. continue;
  2671. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2672. if (ret != 0)
  2673. goto out;
  2674. }
  2675. /* Wait until IOMMU TLB flushes are complete */
  2676. domain_flush_complete(domain);
  2677. /* Now flush device TLBs */
  2678. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2679. struct amd_iommu *iommu;
  2680. int qdep;
  2681. /*
  2682. There might be non-IOMMUv2 capable devices in an IOMMUv2
  2683. * domain.
  2684. */
  2685. if (!dev_data->ats.enabled)
  2686. continue;
  2687. qdep = dev_data->ats.qdep;
  2688. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2689. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2690. qdep, address, size);
  2691. ret = iommu_queue_command(iommu, &cmd);
  2692. if (ret != 0)
  2693. goto out;
  2694. }
  2695. /* Wait until all device TLBs are flushed */
  2696. domain_flush_complete(domain);
  2697. ret = 0;
  2698. out:
  2699. return ret;
  2700. }
  2701. static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
  2702. u64 address)
  2703. {
  2704. return __flush_pasid(domain, pasid, address, false);
  2705. }
  2706. int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
  2707. u64 address)
  2708. {
  2709. struct protection_domain *domain = to_pdomain(dom);
  2710. unsigned long flags;
  2711. int ret;
  2712. spin_lock_irqsave(&domain->lock, flags);
  2713. ret = __amd_iommu_flush_page(domain, pasid, address);
  2714. spin_unlock_irqrestore(&domain->lock, flags);
  2715. return ret;
  2716. }
  2717. EXPORT_SYMBOL(amd_iommu_flush_page);
  2718. static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
  2719. {
  2720. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2721. true);
  2722. }
  2723. int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
  2724. {
  2725. struct protection_domain *domain = to_pdomain(dom);
  2726. unsigned long flags;
  2727. int ret;
  2728. spin_lock_irqsave(&domain->lock, flags);
  2729. ret = __amd_iommu_flush_tlb(domain, pasid);
  2730. spin_unlock_irqrestore(&domain->lock, flags);
  2731. return ret;
  2732. }
  2733. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2734. static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
  2735. {
  2736. int index;
  2737. u64 *pte;
  2738. while (true) {
  2739. index = (pasid >> (9 * level)) & 0x1ff;
  2740. pte = &root[index];
  2741. if (level == 0)
  2742. break;
  2743. if (!(*pte & GCR3_VALID)) {
  2744. if (!alloc)
  2745. return NULL;
  2746. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2747. if (root == NULL)
  2748. return NULL;
  2749. *pte = iommu_virt_to_phys(root) | GCR3_VALID;
  2750. }
  2751. root = iommu_phys_to_virt(*pte & PAGE_MASK);
  2752. level -= 1;
  2753. }
  2754. return pte;
  2755. }
  2756. static int __set_gcr3(struct protection_domain *domain, int pasid,
  2757. unsigned long cr3)
  2758. {
  2759. u64 *pte;
  2760. if (domain->mode != PAGE_MODE_NONE)
  2761. return -EINVAL;
  2762. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2763. if (pte == NULL)
  2764. return -ENOMEM;
  2765. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2766. return __amd_iommu_flush_tlb(domain, pasid);
  2767. }
  2768. static int __clear_gcr3(struct protection_domain *domain, int pasid)
  2769. {
  2770. u64 *pte;
  2771. if (domain->mode != PAGE_MODE_NONE)
  2772. return -EINVAL;
  2773. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2774. if (pte == NULL)
  2775. return 0;
  2776. *pte = 0;
  2777. return __amd_iommu_flush_tlb(domain, pasid);
  2778. }
  2779. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
  2780. unsigned long cr3)
  2781. {
  2782. struct protection_domain *domain = to_pdomain(dom);
  2783. unsigned long flags;
  2784. int ret;
  2785. spin_lock_irqsave(&domain->lock, flags);
  2786. ret = __set_gcr3(domain, pasid, cr3);
  2787. spin_unlock_irqrestore(&domain->lock, flags);
  2788. return ret;
  2789. }
  2790. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2791. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
  2792. {
  2793. struct protection_domain *domain = to_pdomain(dom);
  2794. unsigned long flags;
  2795. int ret;
  2796. spin_lock_irqsave(&domain->lock, flags);
  2797. ret = __clear_gcr3(domain, pasid);
  2798. spin_unlock_irqrestore(&domain->lock, flags);
  2799. return ret;
  2800. }
  2801. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2802. int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
  2803. int status, int tag)
  2804. {
  2805. struct iommu_dev_data *dev_data;
  2806. struct amd_iommu *iommu;
  2807. struct iommu_cmd cmd;
  2808. dev_data = get_dev_data(&pdev->dev);
  2809. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2810. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2811. tag, dev_data->pri_tlp);
  2812. return iommu_queue_command(iommu, &cmd);
  2813. }
  2814. EXPORT_SYMBOL(amd_iommu_complete_ppr);
  2815. struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
  2816. {
  2817. struct protection_domain *pdomain;
  2818. pdomain = get_domain(&pdev->dev);
  2819. if (IS_ERR(pdomain))
  2820. return NULL;
  2821. /* Only return IOMMUv2 domains */
  2822. if (!(pdomain->flags & PD_IOMMUV2_MASK))
  2823. return NULL;
  2824. return &pdomain->domain;
  2825. }
  2826. EXPORT_SYMBOL(amd_iommu_get_v2_domain);
  2827. void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
  2828. {
  2829. struct iommu_dev_data *dev_data;
  2830. if (!amd_iommu_v2_supported())
  2831. return;
  2832. dev_data = get_dev_data(&pdev->dev);
  2833. dev_data->errata |= (1 << erratum);
  2834. }
  2835. EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
  2836. int amd_iommu_device_info(struct pci_dev *pdev,
  2837. struct amd_iommu_device_info *info)
  2838. {
  2839. int max_pasids;
  2840. int pos;
  2841. if (pdev == NULL || info == NULL)
  2842. return -EINVAL;
  2843. if (!amd_iommu_v2_supported())
  2844. return -EINVAL;
  2845. memset(info, 0, sizeof(*info));
  2846. if (!pci_ats_disabled()) {
  2847. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
  2848. if (pos)
  2849. info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  2850. }
  2851. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  2852. if (pos)
  2853. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  2854. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  2855. if (pos) {
  2856. int features;
  2857. max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
  2858. max_pasids = min(max_pasids, (1 << 20));
  2859. info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  2860. info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
  2861. features = pci_pasid_features(pdev);
  2862. if (features & PCI_PASID_CAP_EXEC)
  2863. info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  2864. if (features & PCI_PASID_CAP_PRIV)
  2865. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  2866. }
  2867. return 0;
  2868. }
  2869. EXPORT_SYMBOL(amd_iommu_device_info);
  2870. #ifdef CONFIG_IRQ_REMAP
  2871. /*****************************************************************************
  2872. *
  2873. * Interrupt Remapping Implementation
  2874. *
  2875. *****************************************************************************/
  2876. static struct irq_chip amd_ir_chip;
  2877. static DEFINE_SPINLOCK(iommu_table_lock);
  2878. static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
  2879. {
  2880. u64 dte;
  2881. dte = amd_iommu_dev_table[devid].data[2];
  2882. dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
  2883. dte |= iommu_virt_to_phys(table->table);
  2884. dte |= DTE_IRQ_REMAP_INTCTL;
  2885. dte |= DTE_IRQ_TABLE_LEN;
  2886. dte |= DTE_IRQ_REMAP_ENABLE;
  2887. amd_iommu_dev_table[devid].data[2] = dte;
  2888. }
  2889. static struct irq_remap_table *get_irq_table(u16 devid)
  2890. {
  2891. struct irq_remap_table *table;
  2892. if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
  2893. "%s: no iommu for devid %x\n", __func__, devid))
  2894. return NULL;
  2895. table = irq_lookup_table[devid];
  2896. if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
  2897. return NULL;
  2898. return table;
  2899. }
  2900. static struct irq_remap_table *__alloc_irq_table(void)
  2901. {
  2902. struct irq_remap_table *table;
  2903. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2904. if (!table)
  2905. return NULL;
  2906. table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
  2907. if (!table->table) {
  2908. kfree(table);
  2909. return NULL;
  2910. }
  2911. raw_spin_lock_init(&table->lock);
  2912. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2913. memset(table->table, 0,
  2914. MAX_IRQS_PER_TABLE * sizeof(u32));
  2915. else
  2916. memset(table->table, 0,
  2917. (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
  2918. return table;
  2919. }
  2920. static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
  2921. struct irq_remap_table *table)
  2922. {
  2923. irq_lookup_table[devid] = table;
  2924. set_dte_irq_entry(devid, table);
  2925. iommu_flush_dte(iommu, devid);
  2926. }
  2927. static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
  2928. void *data)
  2929. {
  2930. struct irq_remap_table *table = data;
  2931. irq_lookup_table[alias] = table;
  2932. set_dte_irq_entry(alias, table);
  2933. iommu_flush_dte(amd_iommu_rlookup_table[alias], alias);
  2934. return 0;
  2935. }
  2936. static struct irq_remap_table *alloc_irq_table(u16 devid, struct pci_dev *pdev)
  2937. {
  2938. struct irq_remap_table *table = NULL;
  2939. struct irq_remap_table *new_table = NULL;
  2940. struct amd_iommu *iommu;
  2941. unsigned long flags;
  2942. u16 alias;
  2943. spin_lock_irqsave(&iommu_table_lock, flags);
  2944. iommu = amd_iommu_rlookup_table[devid];
  2945. if (!iommu)
  2946. goto out_unlock;
  2947. table = irq_lookup_table[devid];
  2948. if (table)
  2949. goto out_unlock;
  2950. alias = amd_iommu_alias_table[devid];
  2951. table = irq_lookup_table[alias];
  2952. if (table) {
  2953. set_remap_table_entry(iommu, devid, table);
  2954. goto out_wait;
  2955. }
  2956. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2957. /* Nothing there yet, allocate new irq remapping table */
  2958. new_table = __alloc_irq_table();
  2959. if (!new_table)
  2960. return NULL;
  2961. spin_lock_irqsave(&iommu_table_lock, flags);
  2962. table = irq_lookup_table[devid];
  2963. if (table)
  2964. goto out_unlock;
  2965. table = irq_lookup_table[alias];
  2966. if (table) {
  2967. set_remap_table_entry(iommu, devid, table);
  2968. goto out_wait;
  2969. }
  2970. table = new_table;
  2971. new_table = NULL;
  2972. if (pdev)
  2973. pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
  2974. table);
  2975. else
  2976. set_remap_table_entry(iommu, devid, table);
  2977. if (devid != alias)
  2978. set_remap_table_entry(iommu, alias, table);
  2979. out_wait:
  2980. iommu_completion_wait(iommu);
  2981. out_unlock:
  2982. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2983. if (new_table) {
  2984. kmem_cache_free(amd_iommu_irq_cache, new_table->table);
  2985. kfree(new_table);
  2986. }
  2987. return table;
  2988. }
  2989. static int alloc_irq_index(u16 devid, int count, bool align,
  2990. struct pci_dev *pdev)
  2991. {
  2992. struct irq_remap_table *table;
  2993. int index, c, alignment = 1;
  2994. unsigned long flags;
  2995. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  2996. if (!iommu)
  2997. return -ENODEV;
  2998. table = alloc_irq_table(devid, pdev);
  2999. if (!table)
  3000. return -ENODEV;
  3001. if (align)
  3002. alignment = roundup_pow_of_two(count);
  3003. raw_spin_lock_irqsave(&table->lock, flags);
  3004. /* Scan table for free entries */
  3005. for (index = ALIGN(table->min_index, alignment), c = 0;
  3006. index < MAX_IRQS_PER_TABLE;) {
  3007. if (!iommu->irte_ops->is_allocated(table, index)) {
  3008. c += 1;
  3009. } else {
  3010. c = 0;
  3011. index = ALIGN(index + 1, alignment);
  3012. continue;
  3013. }
  3014. if (c == count) {
  3015. for (; c != 0; --c)
  3016. iommu->irte_ops->set_allocated(table, index - c + 1);
  3017. index -= count - 1;
  3018. goto out;
  3019. }
  3020. index++;
  3021. }
  3022. index = -ENOSPC;
  3023. out:
  3024. raw_spin_unlock_irqrestore(&table->lock, flags);
  3025. return index;
  3026. }
  3027. static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
  3028. struct amd_ir_data *data)
  3029. {
  3030. struct irq_remap_table *table;
  3031. struct amd_iommu *iommu;
  3032. unsigned long flags;
  3033. struct irte_ga *entry;
  3034. iommu = amd_iommu_rlookup_table[devid];
  3035. if (iommu == NULL)
  3036. return -EINVAL;
  3037. table = get_irq_table(devid);
  3038. if (!table)
  3039. return -ENOMEM;
  3040. raw_spin_lock_irqsave(&table->lock, flags);
  3041. entry = (struct irte_ga *)table->table;
  3042. entry = &entry[index];
  3043. entry->lo.fields_remap.valid = 0;
  3044. entry->hi.val = irte->hi.val;
  3045. entry->lo.val = irte->lo.val;
  3046. entry->lo.fields_remap.valid = 1;
  3047. if (data)
  3048. data->ref = entry;
  3049. raw_spin_unlock_irqrestore(&table->lock, flags);
  3050. iommu_flush_irt(iommu, devid);
  3051. iommu_completion_wait(iommu);
  3052. return 0;
  3053. }
  3054. static int modify_irte(u16 devid, int index, union irte *irte)
  3055. {
  3056. struct irq_remap_table *table;
  3057. struct amd_iommu *iommu;
  3058. unsigned long flags;
  3059. iommu = amd_iommu_rlookup_table[devid];
  3060. if (iommu == NULL)
  3061. return -EINVAL;
  3062. table = get_irq_table(devid);
  3063. if (!table)
  3064. return -ENOMEM;
  3065. raw_spin_lock_irqsave(&table->lock, flags);
  3066. table->table[index] = irte->val;
  3067. raw_spin_unlock_irqrestore(&table->lock, flags);
  3068. iommu_flush_irt(iommu, devid);
  3069. iommu_completion_wait(iommu);
  3070. return 0;
  3071. }
  3072. static void free_irte(u16 devid, int index)
  3073. {
  3074. struct irq_remap_table *table;
  3075. struct amd_iommu *iommu;
  3076. unsigned long flags;
  3077. iommu = amd_iommu_rlookup_table[devid];
  3078. if (iommu == NULL)
  3079. return;
  3080. table = get_irq_table(devid);
  3081. if (!table)
  3082. return;
  3083. raw_spin_lock_irqsave(&table->lock, flags);
  3084. iommu->irte_ops->clear_allocated(table, index);
  3085. raw_spin_unlock_irqrestore(&table->lock, flags);
  3086. iommu_flush_irt(iommu, devid);
  3087. iommu_completion_wait(iommu);
  3088. }
  3089. static void irte_prepare(void *entry,
  3090. u32 delivery_mode, u32 dest_mode,
  3091. u8 vector, u32 dest_apicid, int devid)
  3092. {
  3093. union irte *irte = (union irte *) entry;
  3094. irte->val = 0;
  3095. irte->fields.vector = vector;
  3096. irte->fields.int_type = delivery_mode;
  3097. irte->fields.destination = dest_apicid;
  3098. irte->fields.dm = dest_mode;
  3099. irte->fields.valid = 1;
  3100. }
  3101. static void irte_ga_prepare(void *entry,
  3102. u32 delivery_mode, u32 dest_mode,
  3103. u8 vector, u32 dest_apicid, int devid)
  3104. {
  3105. struct irte_ga *irte = (struct irte_ga *) entry;
  3106. irte->lo.val = 0;
  3107. irte->hi.val = 0;
  3108. irte->lo.fields_remap.int_type = delivery_mode;
  3109. irte->lo.fields_remap.dm = dest_mode;
  3110. irte->hi.fields.vector = vector;
  3111. irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
  3112. irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
  3113. irte->lo.fields_remap.valid = 1;
  3114. }
  3115. static void irte_activate(void *entry, u16 devid, u16 index)
  3116. {
  3117. union irte *irte = (union irte *) entry;
  3118. irte->fields.valid = 1;
  3119. modify_irte(devid, index, irte);
  3120. }
  3121. static void irte_ga_activate(void *entry, u16 devid, u16 index)
  3122. {
  3123. struct irte_ga *irte = (struct irte_ga *) entry;
  3124. irte->lo.fields_remap.valid = 1;
  3125. modify_irte_ga(devid, index, irte, NULL);
  3126. }
  3127. static void irte_deactivate(void *entry, u16 devid, u16 index)
  3128. {
  3129. union irte *irte = (union irte *) entry;
  3130. irte->fields.valid = 0;
  3131. modify_irte(devid, index, irte);
  3132. }
  3133. static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
  3134. {
  3135. struct irte_ga *irte = (struct irte_ga *) entry;
  3136. irte->lo.fields_remap.valid = 0;
  3137. modify_irte_ga(devid, index, irte, NULL);
  3138. }
  3139. static void irte_set_affinity(void *entry, u16 devid, u16 index,
  3140. u8 vector, u32 dest_apicid)
  3141. {
  3142. union irte *irte = (union irte *) entry;
  3143. irte->fields.vector = vector;
  3144. irte->fields.destination = dest_apicid;
  3145. modify_irte(devid, index, irte);
  3146. }
  3147. static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
  3148. u8 vector, u32 dest_apicid)
  3149. {
  3150. struct irte_ga *irte = (struct irte_ga *) entry;
  3151. if (!irte->lo.fields_remap.guest_mode) {
  3152. irte->hi.fields.vector = vector;
  3153. irte->lo.fields_remap.destination =
  3154. APICID_TO_IRTE_DEST_LO(dest_apicid);
  3155. irte->hi.fields.destination =
  3156. APICID_TO_IRTE_DEST_HI(dest_apicid);
  3157. modify_irte_ga(devid, index, irte, NULL);
  3158. }
  3159. }
  3160. #define IRTE_ALLOCATED (~1U)
  3161. static void irte_set_allocated(struct irq_remap_table *table, int index)
  3162. {
  3163. table->table[index] = IRTE_ALLOCATED;
  3164. }
  3165. static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
  3166. {
  3167. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3168. struct irte_ga *irte = &ptr[index];
  3169. memset(&irte->lo.val, 0, sizeof(u64));
  3170. memset(&irte->hi.val, 0, sizeof(u64));
  3171. irte->hi.fields.vector = 0xff;
  3172. }
  3173. static bool irte_is_allocated(struct irq_remap_table *table, int index)
  3174. {
  3175. union irte *ptr = (union irte *)table->table;
  3176. union irte *irte = &ptr[index];
  3177. return irte->val != 0;
  3178. }
  3179. static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
  3180. {
  3181. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3182. struct irte_ga *irte = &ptr[index];
  3183. return irte->hi.fields.vector != 0;
  3184. }
  3185. static void irte_clear_allocated(struct irq_remap_table *table, int index)
  3186. {
  3187. table->table[index] = 0;
  3188. }
  3189. static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
  3190. {
  3191. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3192. struct irte_ga *irte = &ptr[index];
  3193. memset(&irte->lo.val, 0, sizeof(u64));
  3194. memset(&irte->hi.val, 0, sizeof(u64));
  3195. }
  3196. static int get_devid(struct irq_alloc_info *info)
  3197. {
  3198. int devid = -1;
  3199. switch (info->type) {
  3200. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3201. devid = get_ioapic_devid(info->ioapic_id);
  3202. break;
  3203. case X86_IRQ_ALLOC_TYPE_HPET:
  3204. devid = get_hpet_devid(info->hpet_id);
  3205. break;
  3206. case X86_IRQ_ALLOC_TYPE_MSI:
  3207. case X86_IRQ_ALLOC_TYPE_MSIX:
  3208. devid = get_device_id(&info->msi_dev->dev);
  3209. break;
  3210. default:
  3211. BUG_ON(1);
  3212. break;
  3213. }
  3214. return devid;
  3215. }
  3216. static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
  3217. {
  3218. struct amd_iommu *iommu;
  3219. int devid;
  3220. if (!info)
  3221. return NULL;
  3222. devid = get_devid(info);
  3223. if (devid >= 0) {
  3224. iommu = amd_iommu_rlookup_table[devid];
  3225. if (iommu)
  3226. return iommu->ir_domain;
  3227. }
  3228. return NULL;
  3229. }
  3230. static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
  3231. {
  3232. struct amd_iommu *iommu;
  3233. int devid;
  3234. if (!info)
  3235. return NULL;
  3236. switch (info->type) {
  3237. case X86_IRQ_ALLOC_TYPE_MSI:
  3238. case X86_IRQ_ALLOC_TYPE_MSIX:
  3239. devid = get_device_id(&info->msi_dev->dev);
  3240. if (devid < 0)
  3241. return NULL;
  3242. iommu = amd_iommu_rlookup_table[devid];
  3243. if (iommu)
  3244. return iommu->msi_domain;
  3245. break;
  3246. default:
  3247. break;
  3248. }
  3249. return NULL;
  3250. }
  3251. struct irq_remap_ops amd_iommu_irq_ops = {
  3252. .prepare = amd_iommu_prepare,
  3253. .enable = amd_iommu_enable,
  3254. .disable = amd_iommu_disable,
  3255. .reenable = amd_iommu_reenable,
  3256. .enable_faulting = amd_iommu_enable_faulting,
  3257. .get_ir_irq_domain = get_ir_irq_domain,
  3258. .get_irq_domain = get_irq_domain,
  3259. };
  3260. static void irq_remapping_prepare_irte(struct amd_ir_data *data,
  3261. struct irq_cfg *irq_cfg,
  3262. struct irq_alloc_info *info,
  3263. int devid, int index, int sub_handle)
  3264. {
  3265. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3266. struct msi_msg *msg = &data->msi_entry;
  3267. struct IO_APIC_route_entry *entry;
  3268. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  3269. if (!iommu)
  3270. return;
  3271. data->irq_2_irte.devid = devid;
  3272. data->irq_2_irte.index = index + sub_handle;
  3273. iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
  3274. apic->irq_dest_mode, irq_cfg->vector,
  3275. irq_cfg->dest_apicid, devid);
  3276. switch (info->type) {
  3277. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3278. /* Setup IOAPIC entry */
  3279. entry = info->ioapic_entry;
  3280. info->ioapic_entry = NULL;
  3281. memset(entry, 0, sizeof(*entry));
  3282. entry->vector = index;
  3283. entry->mask = 0;
  3284. entry->trigger = info->ioapic_trigger;
  3285. entry->polarity = info->ioapic_polarity;
  3286. /* Mask level triggered irqs. */
  3287. if (info->ioapic_trigger)
  3288. entry->mask = 1;
  3289. break;
  3290. case X86_IRQ_ALLOC_TYPE_HPET:
  3291. case X86_IRQ_ALLOC_TYPE_MSI:
  3292. case X86_IRQ_ALLOC_TYPE_MSIX:
  3293. msg->address_hi = MSI_ADDR_BASE_HI;
  3294. msg->address_lo = MSI_ADDR_BASE_LO;
  3295. msg->data = irte_info->index;
  3296. break;
  3297. default:
  3298. BUG_ON(1);
  3299. break;
  3300. }
  3301. }
  3302. struct amd_irte_ops irte_32_ops = {
  3303. .prepare = irte_prepare,
  3304. .activate = irte_activate,
  3305. .deactivate = irte_deactivate,
  3306. .set_affinity = irte_set_affinity,
  3307. .set_allocated = irte_set_allocated,
  3308. .is_allocated = irte_is_allocated,
  3309. .clear_allocated = irte_clear_allocated,
  3310. };
  3311. struct amd_irte_ops irte_128_ops = {
  3312. .prepare = irte_ga_prepare,
  3313. .activate = irte_ga_activate,
  3314. .deactivate = irte_ga_deactivate,
  3315. .set_affinity = irte_ga_set_affinity,
  3316. .set_allocated = irte_ga_set_allocated,
  3317. .is_allocated = irte_ga_is_allocated,
  3318. .clear_allocated = irte_ga_clear_allocated,
  3319. };
  3320. static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
  3321. unsigned int nr_irqs, void *arg)
  3322. {
  3323. struct irq_alloc_info *info = arg;
  3324. struct irq_data *irq_data;
  3325. struct amd_ir_data *data = NULL;
  3326. struct irq_cfg *cfg;
  3327. int i, ret, devid;
  3328. int index;
  3329. if (!info)
  3330. return -EINVAL;
  3331. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
  3332. info->type != X86_IRQ_ALLOC_TYPE_MSIX)
  3333. return -EINVAL;
  3334. /*
  3335. * With IRQ remapping enabled, don't need contiguous CPU vectors
  3336. * to support multiple MSI interrupts.
  3337. */
  3338. if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
  3339. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  3340. devid = get_devid(info);
  3341. if (devid < 0)
  3342. return -EINVAL;
  3343. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  3344. if (ret < 0)
  3345. return ret;
  3346. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
  3347. struct irq_remap_table *table;
  3348. struct amd_iommu *iommu;
  3349. table = alloc_irq_table(devid, NULL);
  3350. if (table) {
  3351. if (!table->min_index) {
  3352. /*
  3353. * Keep the first 32 indexes free for IOAPIC
  3354. * interrupts.
  3355. */
  3356. table->min_index = 32;
  3357. iommu = amd_iommu_rlookup_table[devid];
  3358. for (i = 0; i < 32; ++i)
  3359. iommu->irte_ops->set_allocated(table, i);
  3360. }
  3361. WARN_ON(table->min_index != 32);
  3362. index = info->ioapic_pin;
  3363. } else {
  3364. index = -ENOMEM;
  3365. }
  3366. } else if (info->type == X86_IRQ_ALLOC_TYPE_MSI ||
  3367. info->type == X86_IRQ_ALLOC_TYPE_MSIX) {
  3368. bool align = (info->type == X86_IRQ_ALLOC_TYPE_MSI);
  3369. index = alloc_irq_index(devid, nr_irqs, align, info->msi_dev);
  3370. } else {
  3371. index = alloc_irq_index(devid, nr_irqs, false, NULL);
  3372. }
  3373. if (index < 0) {
  3374. pr_warn("Failed to allocate IRTE\n");
  3375. ret = index;
  3376. goto out_free_parent;
  3377. }
  3378. for (i = 0; i < nr_irqs; i++) {
  3379. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3380. cfg = irqd_cfg(irq_data);
  3381. if (!irq_data || !cfg) {
  3382. ret = -EINVAL;
  3383. goto out_free_data;
  3384. }
  3385. ret = -ENOMEM;
  3386. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3387. if (!data)
  3388. goto out_free_data;
  3389. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  3390. data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
  3391. else
  3392. data->entry = kzalloc(sizeof(struct irte_ga),
  3393. GFP_KERNEL);
  3394. if (!data->entry) {
  3395. kfree(data);
  3396. goto out_free_data;
  3397. }
  3398. irq_data->hwirq = (devid << 16) + i;
  3399. irq_data->chip_data = data;
  3400. irq_data->chip = &amd_ir_chip;
  3401. irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
  3402. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  3403. }
  3404. return 0;
  3405. out_free_data:
  3406. for (i--; i >= 0; i--) {
  3407. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3408. if (irq_data)
  3409. kfree(irq_data->chip_data);
  3410. }
  3411. for (i = 0; i < nr_irqs; i++)
  3412. free_irte(devid, index + i);
  3413. out_free_parent:
  3414. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3415. return ret;
  3416. }
  3417. static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
  3418. unsigned int nr_irqs)
  3419. {
  3420. struct irq_2_irte *irte_info;
  3421. struct irq_data *irq_data;
  3422. struct amd_ir_data *data;
  3423. int i;
  3424. for (i = 0; i < nr_irqs; i++) {
  3425. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3426. if (irq_data && irq_data->chip_data) {
  3427. data = irq_data->chip_data;
  3428. irte_info = &data->irq_2_irte;
  3429. free_irte(irte_info->devid, irte_info->index);
  3430. kfree(data->entry);
  3431. kfree(data);
  3432. }
  3433. }
  3434. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3435. }
  3436. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3437. struct amd_ir_data *ir_data,
  3438. struct irq_2_irte *irte_info,
  3439. struct irq_cfg *cfg);
  3440. static int irq_remapping_activate(struct irq_domain *domain,
  3441. struct irq_data *irq_data, bool reserve)
  3442. {
  3443. struct amd_ir_data *data = irq_data->chip_data;
  3444. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3445. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3446. struct irq_cfg *cfg = irqd_cfg(irq_data);
  3447. if (!iommu)
  3448. return 0;
  3449. iommu->irte_ops->activate(data->entry, irte_info->devid,
  3450. irte_info->index);
  3451. amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
  3452. return 0;
  3453. }
  3454. static void irq_remapping_deactivate(struct irq_domain *domain,
  3455. struct irq_data *irq_data)
  3456. {
  3457. struct amd_ir_data *data = irq_data->chip_data;
  3458. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3459. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3460. if (iommu)
  3461. iommu->irte_ops->deactivate(data->entry, irte_info->devid,
  3462. irte_info->index);
  3463. }
  3464. static const struct irq_domain_ops amd_ir_domain_ops = {
  3465. .alloc = irq_remapping_alloc,
  3466. .free = irq_remapping_free,
  3467. .activate = irq_remapping_activate,
  3468. .deactivate = irq_remapping_deactivate,
  3469. };
  3470. static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
  3471. {
  3472. struct amd_iommu *iommu;
  3473. struct amd_iommu_pi_data *pi_data = vcpu_info;
  3474. struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
  3475. struct amd_ir_data *ir_data = data->chip_data;
  3476. struct irte_ga *irte = (struct irte_ga *) ir_data->entry;
  3477. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3478. struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
  3479. /* Note:
  3480. * This device has never been set up for guest mode.
  3481. * we should not modify the IRTE
  3482. */
  3483. if (!dev_data || !dev_data->use_vapic)
  3484. return 0;
  3485. pi_data->ir_data = ir_data;
  3486. /* Note:
  3487. * SVM tries to set up for VAPIC mode, but we are in
  3488. * legacy mode. So, we force legacy mode instead.
  3489. */
  3490. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  3491. pr_debug("AMD-Vi: %s: Fall back to using intr legacy remap\n",
  3492. __func__);
  3493. pi_data->is_guest_mode = false;
  3494. }
  3495. iommu = amd_iommu_rlookup_table[irte_info->devid];
  3496. if (iommu == NULL)
  3497. return -EINVAL;
  3498. pi_data->prev_ga_tag = ir_data->cached_ga_tag;
  3499. if (pi_data->is_guest_mode) {
  3500. /* Setting */
  3501. irte->hi.fields.ga_root_ptr = (pi_data->base >> 12);
  3502. irte->hi.fields.vector = vcpu_pi_info->vector;
  3503. irte->lo.fields_vapic.ga_log_intr = 1;
  3504. irte->lo.fields_vapic.guest_mode = 1;
  3505. irte->lo.fields_vapic.ga_tag = pi_data->ga_tag;
  3506. ir_data->cached_ga_tag = pi_data->ga_tag;
  3507. } else {
  3508. /* Un-Setting */
  3509. struct irq_cfg *cfg = irqd_cfg(data);
  3510. irte->hi.val = 0;
  3511. irte->lo.val = 0;
  3512. irte->hi.fields.vector = cfg->vector;
  3513. irte->lo.fields_remap.guest_mode = 0;
  3514. irte->lo.fields_remap.destination =
  3515. APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
  3516. irte->hi.fields.destination =
  3517. APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
  3518. irte->lo.fields_remap.int_type = apic->irq_delivery_mode;
  3519. irte->lo.fields_remap.dm = apic->irq_dest_mode;
  3520. /*
  3521. * This communicates the ga_tag back to the caller
  3522. * so that it can do all the necessary clean up.
  3523. */
  3524. ir_data->cached_ga_tag = 0;
  3525. }
  3526. return modify_irte_ga(irte_info->devid, irte_info->index, irte, ir_data);
  3527. }
  3528. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3529. struct amd_ir_data *ir_data,
  3530. struct irq_2_irte *irte_info,
  3531. struct irq_cfg *cfg)
  3532. {
  3533. /*
  3534. * Atomically updates the IRTE with the new destination, vector
  3535. * and flushes the interrupt entry cache.
  3536. */
  3537. iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
  3538. irte_info->index, cfg->vector,
  3539. cfg->dest_apicid);
  3540. }
  3541. static int amd_ir_set_affinity(struct irq_data *data,
  3542. const struct cpumask *mask, bool force)
  3543. {
  3544. struct amd_ir_data *ir_data = data->chip_data;
  3545. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3546. struct irq_cfg *cfg = irqd_cfg(data);
  3547. struct irq_data *parent = data->parent_data;
  3548. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3549. int ret;
  3550. if (!iommu)
  3551. return -ENODEV;
  3552. ret = parent->chip->irq_set_affinity(parent, mask, force);
  3553. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  3554. return ret;
  3555. amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
  3556. /*
  3557. * After this point, all the interrupts will start arriving
  3558. * at the new destination. So, time to cleanup the previous
  3559. * vector allocation.
  3560. */
  3561. send_cleanup_vector(cfg);
  3562. return IRQ_SET_MASK_OK_DONE;
  3563. }
  3564. static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  3565. {
  3566. struct amd_ir_data *ir_data = irq_data->chip_data;
  3567. *msg = ir_data->msi_entry;
  3568. }
  3569. static struct irq_chip amd_ir_chip = {
  3570. .name = "AMD-IR",
  3571. .irq_ack = apic_ack_irq,
  3572. .irq_set_affinity = amd_ir_set_affinity,
  3573. .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
  3574. .irq_compose_msi_msg = ir_compose_msi_msg,
  3575. };
  3576. int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
  3577. {
  3578. struct fwnode_handle *fn;
  3579. fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
  3580. if (!fn)
  3581. return -ENOMEM;
  3582. iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
  3583. irq_domain_free_fwnode(fn);
  3584. if (!iommu->ir_domain)
  3585. return -ENOMEM;
  3586. iommu->ir_domain->parent = arch_get_ir_parent_domain();
  3587. iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
  3588. "AMD-IR-MSI",
  3589. iommu->index);
  3590. return 0;
  3591. }
  3592. int amd_iommu_update_ga(int cpu, bool is_run, void *data)
  3593. {
  3594. unsigned long flags;
  3595. struct amd_iommu *iommu;
  3596. struct irq_remap_table *table;
  3597. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  3598. int devid = ir_data->irq_2_irte.devid;
  3599. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  3600. struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
  3601. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
  3602. !ref || !entry || !entry->lo.fields_vapic.guest_mode)
  3603. return 0;
  3604. iommu = amd_iommu_rlookup_table[devid];
  3605. if (!iommu)
  3606. return -ENODEV;
  3607. table = get_irq_table(devid);
  3608. if (!table)
  3609. return -ENODEV;
  3610. raw_spin_lock_irqsave(&table->lock, flags);
  3611. if (ref->lo.fields_vapic.guest_mode) {
  3612. if (cpu >= 0) {
  3613. ref->lo.fields_vapic.destination =
  3614. APICID_TO_IRTE_DEST_LO(cpu);
  3615. ref->hi.fields.destination =
  3616. APICID_TO_IRTE_DEST_HI(cpu);
  3617. }
  3618. ref->lo.fields_vapic.is_run = is_run;
  3619. barrier();
  3620. }
  3621. raw_spin_unlock_irqrestore(&table->lock, flags);
  3622. iommu_flush_irt(iommu, devid);
  3623. iommu_completion_wait(iommu);
  3624. return 0;
  3625. }
  3626. EXPORT_SYMBOL(amd_iommu_update_ga);
  3627. #endif