amd_iommu.c 97 KB

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