amd_iommu.c 98 KB

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