amd_iommu.c 103 KB

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