amd_iommu.c 104 KB

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