amd_iommu.c 94 KB

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