amd_iommu.c 96 KB

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