amd_iommu.c 103 KB

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