amd_iommu.c 106 KB

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