amd_iommu.c 94 KB

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