amd_iommu.c 106 KB

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