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