amd_iommu.c 95 KB

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