amd_iommu.c 97 KB

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