iommu.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <[email protected]>
  5. * Leo Duran <[email protected]>
  6. */
  7. #define pr_fmt(fmt) "AMD-Vi: " fmt
  8. #define dev_fmt(fmt) pr_fmt(fmt)
  9. #include <linux/ratelimit.h>
  10. #include <linux/pci.h>
  11. #include <linux/acpi.h>
  12. #include <linux/pci-ats.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/slab.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/dma-map-ops.h>
  18. #include <linux/dma-direct.h>
  19. #include <linux/iommu-helper.h>
  20. #include <linux/delay.h>
  21. #include <linux/amd-iommu.h>
  22. #include <linux/notifier.h>
  23. #include <linux/export.h>
  24. #include <linux/irq.h>
  25. #include <linux/msi.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/percpu.h>
  28. #include <linux/io-pgtable.h>
  29. #include <linux/cc_platform.h>
  30. #include <asm/irq_remapping.h>
  31. #include <asm/io_apic.h>
  32. #include <asm/apic.h>
  33. #include <asm/hw_irq.h>
  34. #include <asm/proto.h>
  35. #include <asm/iommu.h>
  36. #include <asm/gart.h>
  37. #include <asm/dma.h>
  38. #include "amd_iommu.h"
  39. #include "../dma-iommu.h"
  40. #include "../irq_remapping.h"
  41. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  42. #define LOOP_TIMEOUT 100000
  43. /* IO virtual address start page frame number */
  44. #define IOVA_START_PFN (1)
  45. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  46. /* Reserved IOVA ranges */
  47. #define MSI_RANGE_START (0xfee00000)
  48. #define MSI_RANGE_END (0xfeefffff)
  49. #define HT_RANGE_START (0xfd00000000ULL)
  50. #define HT_RANGE_END (0xffffffffffULL)
  51. #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
  52. static DEFINE_SPINLOCK(pd_bitmap_lock);
  53. LIST_HEAD(ioapic_map);
  54. LIST_HEAD(hpet_map);
  55. LIST_HEAD(acpihid_map);
  56. const struct iommu_ops amd_iommu_ops;
  57. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  58. int amd_iommu_max_glx_val = -1;
  59. /*
  60. * general struct to manage commands send to an IOMMU
  61. */
  62. struct iommu_cmd {
  63. u32 data[4];
  64. };
  65. struct kmem_cache *amd_iommu_irq_cache;
  66. static void detach_device(struct device *dev);
  67. static int domain_enable_v2(struct protection_domain *domain, int pasids);
  68. /****************************************************************************
  69. *
  70. * Helper functions
  71. *
  72. ****************************************************************************/
  73. static inline int get_acpihid_device_id(struct device *dev,
  74. struct acpihid_map_entry **entry)
  75. {
  76. struct acpi_device *adev = ACPI_COMPANION(dev);
  77. struct acpihid_map_entry *p;
  78. if (!adev)
  79. return -ENODEV;
  80. list_for_each_entry(p, &acpihid_map, list) {
  81. if (acpi_dev_hid_uid_match(adev, p->hid,
  82. p->uid[0] ? p->uid : NULL)) {
  83. if (entry)
  84. *entry = p;
  85. return p->devid;
  86. }
  87. }
  88. return -EINVAL;
  89. }
  90. static inline int get_device_sbdf_id(struct device *dev)
  91. {
  92. int sbdf;
  93. if (dev_is_pci(dev))
  94. sbdf = get_pci_sbdf_id(to_pci_dev(dev));
  95. else
  96. sbdf = get_acpihid_device_id(dev, NULL);
  97. return sbdf;
  98. }
  99. struct dev_table_entry *get_dev_table(struct amd_iommu *iommu)
  100. {
  101. struct dev_table_entry *dev_table;
  102. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  103. BUG_ON(pci_seg == NULL);
  104. dev_table = pci_seg->dev_table;
  105. BUG_ON(dev_table == NULL);
  106. return dev_table;
  107. }
  108. static inline u16 get_device_segment(struct device *dev)
  109. {
  110. u16 seg;
  111. if (dev_is_pci(dev)) {
  112. struct pci_dev *pdev = to_pci_dev(dev);
  113. seg = pci_domain_nr(pdev->bus);
  114. } else {
  115. u32 devid = get_acpihid_device_id(dev, NULL);
  116. seg = PCI_SBDF_TO_SEGID(devid);
  117. }
  118. return seg;
  119. }
  120. /* Writes the specific IOMMU for a device into the PCI segment rlookup table */
  121. void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid)
  122. {
  123. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  124. pci_seg->rlookup_table[devid] = iommu;
  125. }
  126. static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid)
  127. {
  128. struct amd_iommu_pci_seg *pci_seg;
  129. for_each_pci_segment(pci_seg) {
  130. if (pci_seg->id == seg)
  131. return pci_seg->rlookup_table[devid];
  132. }
  133. return NULL;
  134. }
  135. static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
  136. {
  137. u16 seg = get_device_segment(dev);
  138. int devid = get_device_sbdf_id(dev);
  139. if (devid < 0)
  140. return NULL;
  141. return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
  142. }
  143. static struct protection_domain *to_pdomain(struct iommu_domain *dom)
  144. {
  145. return container_of(dom, struct protection_domain, domain);
  146. }
  147. static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
  148. {
  149. struct iommu_dev_data *dev_data;
  150. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  151. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  152. if (!dev_data)
  153. return NULL;
  154. spin_lock_init(&dev_data->lock);
  155. dev_data->devid = devid;
  156. ratelimit_default_init(&dev_data->rs);
  157. llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
  158. return dev_data;
  159. }
  160. static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
  161. {
  162. struct iommu_dev_data *dev_data;
  163. struct llist_node *node;
  164. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  165. if (llist_empty(&pci_seg->dev_data_list))
  166. return NULL;
  167. node = pci_seg->dev_data_list.first;
  168. llist_for_each_entry(dev_data, node, dev_data_list) {
  169. if (dev_data->devid == devid)
  170. return dev_data;
  171. }
  172. return NULL;
  173. }
  174. static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
  175. {
  176. struct amd_iommu *iommu;
  177. struct dev_table_entry *dev_table;
  178. u16 devid = pci_dev_id(pdev);
  179. if (devid == alias)
  180. return 0;
  181. iommu = rlookup_amd_iommu(&pdev->dev);
  182. if (!iommu)
  183. return 0;
  184. amd_iommu_set_rlookup_table(iommu, alias);
  185. dev_table = get_dev_table(iommu);
  186. memcpy(dev_table[alias].data,
  187. dev_table[devid].data,
  188. sizeof(dev_table[alias].data));
  189. return 0;
  190. }
  191. static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
  192. {
  193. struct pci_dev *pdev;
  194. if (!dev_is_pci(dev))
  195. return;
  196. pdev = to_pci_dev(dev);
  197. /*
  198. * The IVRS alias stored in the alias table may not be
  199. * part of the PCI DMA aliases if it's bus differs
  200. * from the original device.
  201. */
  202. clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], NULL);
  203. pci_for_each_dma_alias(pdev, clone_alias, NULL);
  204. }
  205. static void setup_aliases(struct amd_iommu *iommu, struct device *dev)
  206. {
  207. struct pci_dev *pdev = to_pci_dev(dev);
  208. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  209. u16 ivrs_alias;
  210. /* For ACPI HID devices, there are no aliases */
  211. if (!dev_is_pci(dev))
  212. return;
  213. /*
  214. * Add the IVRS alias to the pci aliases if it is on the same
  215. * bus. The IVRS table may know about a quirk that we don't.
  216. */
  217. ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)];
  218. if (ivrs_alias != pci_dev_id(pdev) &&
  219. PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
  220. pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
  221. clone_aliases(iommu, dev);
  222. }
  223. static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)
  224. {
  225. struct iommu_dev_data *dev_data;
  226. dev_data = search_dev_data(iommu, devid);
  227. if (dev_data == NULL) {
  228. dev_data = alloc_dev_data(iommu, devid);
  229. if (!dev_data)
  230. return NULL;
  231. if (translation_pre_enabled(iommu))
  232. dev_data->defer_attach = true;
  233. }
  234. return dev_data;
  235. }
  236. /*
  237. * Find or create an IOMMU group for a acpihid device.
  238. */
  239. static struct iommu_group *acpihid_device_group(struct device *dev)
  240. {
  241. struct acpihid_map_entry *p, *entry = NULL;
  242. int devid;
  243. devid = get_acpihid_device_id(dev, &entry);
  244. if (devid < 0)
  245. return ERR_PTR(devid);
  246. list_for_each_entry(p, &acpihid_map, list) {
  247. if ((devid == p->devid) && p->group)
  248. entry->group = p->group;
  249. }
  250. if (!entry->group)
  251. entry->group = generic_device_group(dev);
  252. else
  253. iommu_group_ref_get(entry->group);
  254. return entry->group;
  255. }
  256. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  257. {
  258. static const int caps[] = {
  259. PCI_EXT_CAP_ID_PRI,
  260. PCI_EXT_CAP_ID_PASID,
  261. };
  262. int i, pos;
  263. if (!pci_ats_supported(pdev))
  264. return false;
  265. for (i = 0; i < 2; ++i) {
  266. pos = pci_find_ext_capability(pdev, caps[i]);
  267. if (pos == 0)
  268. return false;
  269. }
  270. return true;
  271. }
  272. /*
  273. * This function checks if the driver got a valid device from the caller to
  274. * avoid dereferencing invalid pointers.
  275. */
  276. static bool check_device(struct device *dev)
  277. {
  278. struct amd_iommu_pci_seg *pci_seg;
  279. struct amd_iommu *iommu;
  280. int devid, sbdf;
  281. if (!dev)
  282. return false;
  283. sbdf = get_device_sbdf_id(dev);
  284. if (sbdf < 0)
  285. return false;
  286. devid = PCI_SBDF_TO_DEVID(sbdf);
  287. iommu = rlookup_amd_iommu(dev);
  288. if (!iommu)
  289. return false;
  290. /* Out of our scope? */
  291. pci_seg = iommu->pci_seg;
  292. if (devid > pci_seg->last_bdf)
  293. return false;
  294. return true;
  295. }
  296. static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
  297. {
  298. struct iommu_dev_data *dev_data;
  299. int devid, sbdf;
  300. if (dev_iommu_priv_get(dev))
  301. return 0;
  302. sbdf = get_device_sbdf_id(dev);
  303. if (sbdf < 0)
  304. return sbdf;
  305. devid = PCI_SBDF_TO_DEVID(sbdf);
  306. dev_data = find_dev_data(iommu, devid);
  307. if (!dev_data)
  308. return -ENOMEM;
  309. dev_data->dev = dev;
  310. setup_aliases(iommu, dev);
  311. /*
  312. * By default we use passthrough mode for IOMMUv2 capable device.
  313. * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
  314. * invalid address), we ignore the capability for the device so
  315. * it'll be forced to go into translation mode.
  316. */
  317. if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
  318. dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
  319. dev_data->iommu_v2 = iommu->is_iommu_v2;
  320. }
  321. dev_iommu_priv_set(dev, dev_data);
  322. return 0;
  323. }
  324. static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
  325. {
  326. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  327. struct dev_table_entry *dev_table = get_dev_table(iommu);
  328. int devid, sbdf;
  329. sbdf = get_device_sbdf_id(dev);
  330. if (sbdf < 0)
  331. return;
  332. devid = PCI_SBDF_TO_DEVID(sbdf);
  333. pci_seg->rlookup_table[devid] = NULL;
  334. memset(&dev_table[devid], 0, sizeof(struct dev_table_entry));
  335. setup_aliases(iommu, dev);
  336. }
  337. static void amd_iommu_uninit_device(struct device *dev)
  338. {
  339. struct iommu_dev_data *dev_data;
  340. dev_data = dev_iommu_priv_get(dev);
  341. if (!dev_data)
  342. return;
  343. if (dev_data->domain)
  344. detach_device(dev);
  345. dev_iommu_priv_set(dev, NULL);
  346. /*
  347. * We keep dev_data around for unplugged devices and reuse it when the
  348. * device is re-plugged - not doing so would introduce a ton of races.
  349. */
  350. }
  351. /****************************************************************************
  352. *
  353. * Interrupt handling functions
  354. *
  355. ****************************************************************************/
  356. static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
  357. {
  358. int i;
  359. struct dev_table_entry *dev_table = get_dev_table(iommu);
  360. for (i = 0; i < 4; ++i)
  361. pr_err("DTE[%d]: %016llx\n", i, dev_table[devid].data[i]);
  362. }
  363. static void dump_command(unsigned long phys_addr)
  364. {
  365. struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
  366. int i;
  367. for (i = 0; i < 4; ++i)
  368. pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
  369. }
  370. static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event)
  371. {
  372. struct iommu_dev_data *dev_data = NULL;
  373. int devid, vmg_tag, flags;
  374. struct pci_dev *pdev;
  375. u64 spa;
  376. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  377. vmg_tag = (event[1]) & 0xFFFF;
  378. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  379. spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
  380. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  381. devid & 0xff);
  382. if (pdev)
  383. dev_data = dev_iommu_priv_get(&pdev->dev);
  384. if (dev_data) {
  385. if (__ratelimit(&dev_data->rs)) {
  386. pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
  387. vmg_tag, spa, flags);
  388. }
  389. } else {
  390. pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
  391. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  392. vmg_tag, spa, flags);
  393. }
  394. if (pdev)
  395. pci_dev_put(pdev);
  396. }
  397. static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event)
  398. {
  399. struct iommu_dev_data *dev_data = NULL;
  400. int devid, flags_rmp, vmg_tag, flags;
  401. struct pci_dev *pdev;
  402. u64 gpa;
  403. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  404. flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
  405. vmg_tag = (event[1]) & 0xFFFF;
  406. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  407. gpa = ((u64)event[3] << 32) | event[2];
  408. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  409. devid & 0xff);
  410. if (pdev)
  411. dev_data = dev_iommu_priv_get(&pdev->dev);
  412. if (dev_data) {
  413. if (__ratelimit(&dev_data->rs)) {
  414. pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
  415. vmg_tag, gpa, flags_rmp, flags);
  416. }
  417. } else {
  418. pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
  419. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  420. vmg_tag, gpa, flags_rmp, flags);
  421. }
  422. if (pdev)
  423. pci_dev_put(pdev);
  424. }
  425. #define IS_IOMMU_MEM_TRANSACTION(flags) \
  426. (((flags) & EVENT_FLAG_I) == 0)
  427. #define IS_WRITE_REQUEST(flags) \
  428. ((flags) & EVENT_FLAG_RW)
  429. static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
  430. u16 devid, u16 domain_id,
  431. u64 address, int flags)
  432. {
  433. struct iommu_dev_data *dev_data = NULL;
  434. struct pci_dev *pdev;
  435. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  436. devid & 0xff);
  437. if (pdev)
  438. dev_data = dev_iommu_priv_get(&pdev->dev);
  439. if (dev_data) {
  440. /*
  441. * If this is a DMA fault (for which the I(nterrupt)
  442. * bit will be unset), allow report_iommu_fault() to
  443. * prevent logging it.
  444. */
  445. if (IS_IOMMU_MEM_TRANSACTION(flags)) {
  446. /* Device not attached to domain properly */
  447. if (dev_data->domain == NULL) {
  448. pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
  449. pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n",
  450. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
  451. PCI_FUNC(devid), domain_id);
  452. goto out;
  453. }
  454. if (!report_iommu_fault(&dev_data->domain->domain,
  455. &pdev->dev, address,
  456. IS_WRITE_REQUEST(flags) ?
  457. IOMMU_FAULT_WRITE :
  458. IOMMU_FAULT_READ))
  459. goto out;
  460. }
  461. if (__ratelimit(&dev_data->rs)) {
  462. pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
  463. domain_id, address, flags);
  464. }
  465. } else {
  466. pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
  467. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  468. domain_id, address, flags);
  469. }
  470. out:
  471. if (pdev)
  472. pci_dev_put(pdev);
  473. }
  474. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  475. {
  476. struct device *dev = iommu->iommu.dev;
  477. int type, devid, flags, tag;
  478. volatile u32 *event = __evt;
  479. int count = 0;
  480. u64 address;
  481. u32 pasid;
  482. retry:
  483. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  484. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  485. pasid = (event[0] & EVENT_DOMID_MASK_HI) |
  486. (event[1] & EVENT_DOMID_MASK_LO);
  487. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  488. address = (u64)(((u64)event[3]) << 32) | event[2];
  489. if (type == 0) {
  490. /* Did we hit the erratum? */
  491. if (++count == LOOP_TIMEOUT) {
  492. pr_err("No event written to event log\n");
  493. return;
  494. }
  495. udelay(1);
  496. goto retry;
  497. }
  498. if (type == EVENT_TYPE_IO_FAULT) {
  499. amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
  500. return;
  501. }
  502. switch (type) {
  503. case EVENT_TYPE_ILL_DEV:
  504. dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
  505. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  506. pasid, address, flags);
  507. dump_dte_entry(iommu, devid);
  508. break;
  509. case EVENT_TYPE_DEV_TAB_ERR:
  510. dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
  511. "address=0x%llx flags=0x%04x]\n",
  512. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  513. address, flags);
  514. break;
  515. case EVENT_TYPE_PAGE_TAB_ERR:
  516. dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
  517. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  518. pasid, address, flags);
  519. break;
  520. case EVENT_TYPE_ILL_CMD:
  521. dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
  522. dump_command(address);
  523. break;
  524. case EVENT_TYPE_CMD_HARD_ERR:
  525. dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
  526. address, flags);
  527. break;
  528. case EVENT_TYPE_IOTLB_INV_TO:
  529. dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
  530. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  531. address);
  532. break;
  533. case EVENT_TYPE_INV_DEV_REQ:
  534. dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
  535. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  536. pasid, address, flags);
  537. break;
  538. case EVENT_TYPE_RMP_FAULT:
  539. amd_iommu_report_rmp_fault(iommu, event);
  540. break;
  541. case EVENT_TYPE_RMP_HW_ERR:
  542. amd_iommu_report_rmp_hw_error(iommu, event);
  543. break;
  544. case EVENT_TYPE_INV_PPR_REQ:
  545. pasid = PPR_PASID(*((u64 *)__evt));
  546. tag = event[1] & 0x03FF;
  547. dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
  548. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  549. pasid, address, flags, tag);
  550. break;
  551. default:
  552. dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
  553. event[0], event[1], event[2], event[3]);
  554. }
  555. memset(__evt, 0, 4 * sizeof(u32));
  556. }
  557. static void iommu_poll_events(struct amd_iommu *iommu)
  558. {
  559. u32 head, tail;
  560. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  561. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  562. while (head != tail) {
  563. iommu_print_event(iommu, iommu->evt_buf + head);
  564. head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
  565. }
  566. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  567. }
  568. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
  569. {
  570. struct amd_iommu_fault fault;
  571. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  572. pr_err_ratelimited("Unknown PPR request received\n");
  573. return;
  574. }
  575. fault.address = raw[1];
  576. fault.pasid = PPR_PASID(raw[0]);
  577. fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0]));
  578. fault.tag = PPR_TAG(raw[0]);
  579. fault.flags = PPR_FLAGS(raw[0]);
  580. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  581. }
  582. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  583. {
  584. u32 head, tail;
  585. if (iommu->ppr_log == NULL)
  586. return;
  587. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  588. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  589. while (head != tail) {
  590. volatile u64 *raw;
  591. u64 entry[2];
  592. int i;
  593. raw = (u64 *)(iommu->ppr_log + head);
  594. /*
  595. * Hardware bug: Interrupt may arrive before the entry is
  596. * written to memory. If this happens we need to wait for the
  597. * entry to arrive.
  598. */
  599. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  600. if (PPR_REQ_TYPE(raw[0]) != 0)
  601. break;
  602. udelay(1);
  603. }
  604. /* Avoid memcpy function-call overhead */
  605. entry[0] = raw[0];
  606. entry[1] = raw[1];
  607. /*
  608. * To detect the hardware bug we need to clear the entry
  609. * back to zero.
  610. */
  611. raw[0] = raw[1] = 0UL;
  612. /* Update head pointer of hardware ring-buffer */
  613. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  614. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  615. /* Handle PPR entry */
  616. iommu_handle_ppr_entry(iommu, entry);
  617. /* Refresh ring-buffer information */
  618. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  619. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  620. }
  621. }
  622. #ifdef CONFIG_IRQ_REMAP
  623. static int (*iommu_ga_log_notifier)(u32);
  624. int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
  625. {
  626. iommu_ga_log_notifier = notifier;
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
  630. static void iommu_poll_ga_log(struct amd_iommu *iommu)
  631. {
  632. u32 head, tail, cnt = 0;
  633. if (iommu->ga_log == NULL)
  634. return;
  635. head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  636. tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  637. while (head != tail) {
  638. volatile u64 *raw;
  639. u64 log_entry;
  640. raw = (u64 *)(iommu->ga_log + head);
  641. cnt++;
  642. /* Avoid memcpy function-call overhead */
  643. log_entry = *raw;
  644. /* Update head pointer of hardware ring-buffer */
  645. head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
  646. writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  647. /* Handle GA entry */
  648. switch (GA_REQ_TYPE(log_entry)) {
  649. case GA_GUEST_NR:
  650. if (!iommu_ga_log_notifier)
  651. break;
  652. pr_debug("%s: devid=%#x, ga_tag=%#x\n",
  653. __func__, GA_DEVID(log_entry),
  654. GA_TAG(log_entry));
  655. if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
  656. pr_err("GA log notifier failed.\n");
  657. break;
  658. default:
  659. break;
  660. }
  661. }
  662. }
  663. static void
  664. amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
  665. {
  666. if (!irq_remapping_enabled || !dev_is_pci(dev) ||
  667. pci_dev_has_special_msi_domain(to_pci_dev(dev)))
  668. return;
  669. dev_set_msi_domain(dev, iommu->msi_domain);
  670. }
  671. #else /* CONFIG_IRQ_REMAP */
  672. static inline void
  673. amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
  674. #endif /* !CONFIG_IRQ_REMAP */
  675. #define AMD_IOMMU_INT_MASK \
  676. (MMIO_STATUS_EVT_OVERFLOW_INT_MASK | \
  677. MMIO_STATUS_EVT_INT_MASK | \
  678. MMIO_STATUS_PPR_INT_MASK | \
  679. MMIO_STATUS_GALOG_OVERFLOW_MASK | \
  680. MMIO_STATUS_GALOG_INT_MASK)
  681. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  682. {
  683. struct amd_iommu *iommu = (struct amd_iommu *) data;
  684. u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  685. while (status & AMD_IOMMU_INT_MASK) {
  686. /* Enable interrupt sources again */
  687. writel(AMD_IOMMU_INT_MASK,
  688. iommu->mmio_base + MMIO_STATUS_OFFSET);
  689. if (status & MMIO_STATUS_EVT_INT_MASK) {
  690. pr_devel("Processing IOMMU Event Log\n");
  691. iommu_poll_events(iommu);
  692. }
  693. if (status & MMIO_STATUS_PPR_INT_MASK) {
  694. pr_devel("Processing IOMMU PPR Log\n");
  695. iommu_poll_ppr_log(iommu);
  696. }
  697. #ifdef CONFIG_IRQ_REMAP
  698. if (status & (MMIO_STATUS_GALOG_INT_MASK |
  699. MMIO_STATUS_GALOG_OVERFLOW_MASK)) {
  700. pr_devel("Processing IOMMU GA Log\n");
  701. iommu_poll_ga_log(iommu);
  702. }
  703. if (status & MMIO_STATUS_GALOG_OVERFLOW_MASK) {
  704. pr_info_ratelimited("IOMMU GA Log overflow\n");
  705. amd_iommu_restart_ga_log(iommu);
  706. }
  707. #endif
  708. if (status & MMIO_STATUS_EVT_OVERFLOW_INT_MASK) {
  709. pr_info_ratelimited("IOMMU event log overflow\n");
  710. amd_iommu_restart_event_logging(iommu);
  711. }
  712. /*
  713. * Hardware bug: ERBT1312
  714. * When re-enabling interrupt (by writing 1
  715. * to clear the bit), the hardware might also try to set
  716. * the interrupt bit in the event status register.
  717. * In this scenario, the bit will be set, and disable
  718. * subsequent interrupts.
  719. *
  720. * Workaround: The IOMMU driver should read back the
  721. * status register and check if the interrupt bits are cleared.
  722. * If not, driver will need to go through the interrupt handler
  723. * again and re-clear the bits
  724. */
  725. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  726. }
  727. return IRQ_HANDLED;
  728. }
  729. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  730. {
  731. return IRQ_WAKE_THREAD;
  732. }
  733. /****************************************************************************
  734. *
  735. * IOMMU command queuing functions
  736. *
  737. ****************************************************************************/
  738. static int wait_on_sem(struct amd_iommu *iommu, u64 data)
  739. {
  740. int i = 0;
  741. while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
  742. udelay(1);
  743. i += 1;
  744. }
  745. if (i == LOOP_TIMEOUT) {
  746. pr_alert("Completion-Wait loop timed out\n");
  747. return -EIO;
  748. }
  749. return 0;
  750. }
  751. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  752. struct iommu_cmd *cmd)
  753. {
  754. u8 *target;
  755. u32 tail;
  756. /* Copy command to buffer */
  757. tail = iommu->cmd_buf_tail;
  758. target = iommu->cmd_buf + tail;
  759. memcpy(target, cmd, sizeof(*cmd));
  760. tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  761. iommu->cmd_buf_tail = tail;
  762. /* Tell the IOMMU about it */
  763. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  764. }
  765. static void build_completion_wait(struct iommu_cmd *cmd,
  766. struct amd_iommu *iommu,
  767. u64 data)
  768. {
  769. u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
  770. memset(cmd, 0, sizeof(*cmd));
  771. cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
  772. cmd->data[1] = upper_32_bits(paddr);
  773. cmd->data[2] = lower_32_bits(data);
  774. cmd->data[3] = upper_32_bits(data);
  775. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  776. }
  777. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  778. {
  779. memset(cmd, 0, sizeof(*cmd));
  780. cmd->data[0] = devid;
  781. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  782. }
  783. /*
  784. * Builds an invalidation address which is suitable for one page or multiple
  785. * pages. Sets the size bit (S) as needed is more than one page is flushed.
  786. */
  787. static inline u64 build_inv_address(u64 address, size_t size)
  788. {
  789. u64 pages, end, msb_diff;
  790. pages = iommu_num_pages(address, size, PAGE_SIZE);
  791. if (pages == 1)
  792. return address & PAGE_MASK;
  793. end = address + size - 1;
  794. /*
  795. * msb_diff would hold the index of the most significant bit that
  796. * flipped between the start and end.
  797. */
  798. msb_diff = fls64(end ^ address) - 1;
  799. /*
  800. * Bits 63:52 are sign extended. If for some reason bit 51 is different
  801. * between the start and the end, invalidate everything.
  802. */
  803. if (unlikely(msb_diff > 51)) {
  804. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  805. } else {
  806. /*
  807. * The msb-bit must be clear on the address. Just set all the
  808. * lower bits.
  809. */
  810. address |= (1ull << msb_diff) - 1;
  811. }
  812. /* Clear bits 11:0 */
  813. address &= PAGE_MASK;
  814. /* Set the size bit - we flush more than one 4kb page */
  815. return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
  816. }
  817. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  818. size_t size, u16 domid, int pde)
  819. {
  820. u64 inv_address = build_inv_address(address, size);
  821. memset(cmd, 0, sizeof(*cmd));
  822. cmd->data[1] |= domid;
  823. cmd->data[2] = lower_32_bits(inv_address);
  824. cmd->data[3] = upper_32_bits(inv_address);
  825. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  826. if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
  827. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  828. }
  829. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  830. u64 address, size_t size)
  831. {
  832. u64 inv_address = build_inv_address(address, size);
  833. memset(cmd, 0, sizeof(*cmd));
  834. cmd->data[0] = devid;
  835. cmd->data[0] |= (qdep & 0xff) << 24;
  836. cmd->data[1] = devid;
  837. cmd->data[2] = lower_32_bits(inv_address);
  838. cmd->data[3] = upper_32_bits(inv_address);
  839. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  840. }
  841. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
  842. u64 address, bool size)
  843. {
  844. memset(cmd, 0, sizeof(*cmd));
  845. address &= ~(0xfffULL);
  846. cmd->data[0] = pasid;
  847. cmd->data[1] = domid;
  848. cmd->data[2] = lower_32_bits(address);
  849. cmd->data[3] = upper_32_bits(address);
  850. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  851. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  852. if (size)
  853. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  854. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  855. }
  856. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
  857. int qdep, u64 address, bool size)
  858. {
  859. memset(cmd, 0, sizeof(*cmd));
  860. address &= ~(0xfffULL);
  861. cmd->data[0] = devid;
  862. cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
  863. cmd->data[0] |= (qdep & 0xff) << 24;
  864. cmd->data[1] = devid;
  865. cmd->data[1] |= (pasid & 0xff) << 16;
  866. cmd->data[2] = lower_32_bits(address);
  867. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  868. cmd->data[3] = upper_32_bits(address);
  869. if (size)
  870. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  871. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  872. }
  873. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
  874. int status, int tag, bool gn)
  875. {
  876. memset(cmd, 0, sizeof(*cmd));
  877. cmd->data[0] = devid;
  878. if (gn) {
  879. cmd->data[1] = pasid;
  880. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  881. }
  882. cmd->data[3] = tag & 0x1ff;
  883. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  884. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  885. }
  886. static void build_inv_all(struct iommu_cmd *cmd)
  887. {
  888. memset(cmd, 0, sizeof(*cmd));
  889. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  890. }
  891. static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  892. {
  893. memset(cmd, 0, sizeof(*cmd));
  894. cmd->data[0] = devid;
  895. CMD_SET_TYPE(cmd, CMD_INV_IRT);
  896. }
  897. /*
  898. * Writes the command to the IOMMUs command buffer and informs the
  899. * hardware about the new command.
  900. */
  901. static int __iommu_queue_command_sync(struct amd_iommu *iommu,
  902. struct iommu_cmd *cmd,
  903. bool sync)
  904. {
  905. unsigned int count = 0;
  906. u32 left, next_tail;
  907. next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  908. again:
  909. left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
  910. if (left <= 0x20) {
  911. /* Skip udelay() the first time around */
  912. if (count++) {
  913. if (count == LOOP_TIMEOUT) {
  914. pr_err("Command buffer timeout\n");
  915. return -EIO;
  916. }
  917. udelay(1);
  918. }
  919. /* Update head and recheck remaining space */
  920. iommu->cmd_buf_head = readl(iommu->mmio_base +
  921. MMIO_CMD_HEAD_OFFSET);
  922. goto again;
  923. }
  924. copy_cmd_to_buffer(iommu, cmd);
  925. /* Do we need to make sure all commands are processed? */
  926. iommu->need_sync = sync;
  927. return 0;
  928. }
  929. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  930. struct iommu_cmd *cmd,
  931. bool sync)
  932. {
  933. unsigned long flags;
  934. int ret;
  935. raw_spin_lock_irqsave(&iommu->lock, flags);
  936. ret = __iommu_queue_command_sync(iommu, cmd, sync);
  937. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  938. return ret;
  939. }
  940. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  941. {
  942. return iommu_queue_command_sync(iommu, cmd, true);
  943. }
  944. /*
  945. * This function queues a completion wait command into the command
  946. * buffer of an IOMMU
  947. */
  948. static int iommu_completion_wait(struct amd_iommu *iommu)
  949. {
  950. struct iommu_cmd cmd;
  951. unsigned long flags;
  952. int ret;
  953. u64 data;
  954. if (!iommu->need_sync)
  955. return 0;
  956. raw_spin_lock_irqsave(&iommu->lock, flags);
  957. data = ++iommu->cmd_sem_val;
  958. build_completion_wait(&cmd, iommu, data);
  959. ret = __iommu_queue_command_sync(iommu, &cmd, false);
  960. if (ret)
  961. goto out_unlock;
  962. ret = wait_on_sem(iommu, data);
  963. out_unlock:
  964. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  965. return ret;
  966. }
  967. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  968. {
  969. struct iommu_cmd cmd;
  970. build_inv_dte(&cmd, devid);
  971. return iommu_queue_command(iommu, &cmd);
  972. }
  973. static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
  974. {
  975. u32 devid;
  976. u16 last_bdf = iommu->pci_seg->last_bdf;
  977. for (devid = 0; devid <= last_bdf; ++devid)
  978. iommu_flush_dte(iommu, devid);
  979. iommu_completion_wait(iommu);
  980. }
  981. /*
  982. * This function uses heavy locking and may disable irqs for some time. But
  983. * this is no issue because it is only called during resume.
  984. */
  985. static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
  986. {
  987. u32 dom_id;
  988. u16 last_bdf = iommu->pci_seg->last_bdf;
  989. for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
  990. struct iommu_cmd cmd;
  991. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  992. dom_id, 1);
  993. iommu_queue_command(iommu, &cmd);
  994. }
  995. iommu_completion_wait(iommu);
  996. }
  997. static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
  998. {
  999. struct iommu_cmd cmd;
  1000. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  1001. dom_id, 1);
  1002. iommu_queue_command(iommu, &cmd);
  1003. iommu_completion_wait(iommu);
  1004. }
  1005. static void amd_iommu_flush_all(struct amd_iommu *iommu)
  1006. {
  1007. struct iommu_cmd cmd;
  1008. build_inv_all(&cmd);
  1009. iommu_queue_command(iommu, &cmd);
  1010. iommu_completion_wait(iommu);
  1011. }
  1012. static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
  1013. {
  1014. struct iommu_cmd cmd;
  1015. build_inv_irt(&cmd, devid);
  1016. iommu_queue_command(iommu, &cmd);
  1017. }
  1018. static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
  1019. {
  1020. u32 devid;
  1021. u16 last_bdf = iommu->pci_seg->last_bdf;
  1022. for (devid = 0; devid <= last_bdf; devid++)
  1023. iommu_flush_irt(iommu, devid);
  1024. iommu_completion_wait(iommu);
  1025. }
  1026. void iommu_flush_all_caches(struct amd_iommu *iommu)
  1027. {
  1028. if (iommu_feature(iommu, FEATURE_IA)) {
  1029. amd_iommu_flush_all(iommu);
  1030. } else {
  1031. amd_iommu_flush_dte_all(iommu);
  1032. amd_iommu_flush_irt_all(iommu);
  1033. amd_iommu_flush_tlb_all(iommu);
  1034. }
  1035. }
  1036. /*
  1037. * Command send function for flushing on-device TLB
  1038. */
  1039. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  1040. u64 address, size_t size)
  1041. {
  1042. struct amd_iommu *iommu;
  1043. struct iommu_cmd cmd;
  1044. int qdep;
  1045. qdep = dev_data->ats.qdep;
  1046. iommu = rlookup_amd_iommu(dev_data->dev);
  1047. if (!iommu)
  1048. return -EINVAL;
  1049. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  1050. return iommu_queue_command(iommu, &cmd);
  1051. }
  1052. static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
  1053. {
  1054. struct amd_iommu *iommu = data;
  1055. return iommu_flush_dte(iommu, alias);
  1056. }
  1057. /*
  1058. * Command send function for invalidating a device table entry
  1059. */
  1060. static int device_flush_dte(struct iommu_dev_data *dev_data)
  1061. {
  1062. struct amd_iommu *iommu;
  1063. struct pci_dev *pdev = NULL;
  1064. struct amd_iommu_pci_seg *pci_seg;
  1065. u16 alias;
  1066. int ret;
  1067. iommu = rlookup_amd_iommu(dev_data->dev);
  1068. if (!iommu)
  1069. return -EINVAL;
  1070. if (dev_is_pci(dev_data->dev))
  1071. pdev = to_pci_dev(dev_data->dev);
  1072. if (pdev)
  1073. ret = pci_for_each_dma_alias(pdev,
  1074. device_flush_dte_alias, iommu);
  1075. else
  1076. ret = iommu_flush_dte(iommu, dev_data->devid);
  1077. if (ret)
  1078. return ret;
  1079. pci_seg = iommu->pci_seg;
  1080. alias = pci_seg->alias_table[dev_data->devid];
  1081. if (alias != dev_data->devid) {
  1082. ret = iommu_flush_dte(iommu, alias);
  1083. if (ret)
  1084. return ret;
  1085. }
  1086. if (dev_data->ats.enabled)
  1087. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  1088. return ret;
  1089. }
  1090. /*
  1091. * TLB invalidation function which is called from the mapping functions.
  1092. * It invalidates a single PTE if the range to flush is within a single
  1093. * page. Otherwise it flushes the whole TLB of the IOMMU.
  1094. */
  1095. static void __domain_flush_pages(struct protection_domain *domain,
  1096. u64 address, size_t size, int pde)
  1097. {
  1098. struct iommu_dev_data *dev_data;
  1099. struct iommu_cmd cmd;
  1100. int ret = 0, i;
  1101. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  1102. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1103. if (!domain->dev_iommu[i])
  1104. continue;
  1105. /*
  1106. * Devices of this domain are behind this IOMMU
  1107. * We need a TLB flush
  1108. */
  1109. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  1110. }
  1111. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1112. if (!dev_data->ats.enabled)
  1113. continue;
  1114. ret |= device_flush_iotlb(dev_data, address, size);
  1115. }
  1116. WARN_ON(ret);
  1117. }
  1118. static void domain_flush_pages(struct protection_domain *domain,
  1119. u64 address, size_t size, int pde)
  1120. {
  1121. if (likely(!amd_iommu_np_cache)) {
  1122. __domain_flush_pages(domain, address, size, pde);
  1123. return;
  1124. }
  1125. /*
  1126. * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
  1127. * In such setups it is best to avoid flushes of ranges which are not
  1128. * naturally aligned, since it would lead to flushes of unmodified
  1129. * PTEs. Such flushes would require the hypervisor to do more work than
  1130. * necessary. Therefore, perform repeated flushes of aligned ranges
  1131. * until you cover the range. Each iteration flushes the smaller
  1132. * between the natural alignment of the address that we flush and the
  1133. * greatest naturally aligned region that fits in the range.
  1134. */
  1135. while (size != 0) {
  1136. int addr_alignment = __ffs(address);
  1137. int size_alignment = __fls(size);
  1138. int min_alignment;
  1139. size_t flush_size;
  1140. /*
  1141. * size is always non-zero, but address might be zero, causing
  1142. * addr_alignment to be negative. As the casting of the
  1143. * argument in __ffs(address) to long might trim the high bits
  1144. * of the address on x86-32, cast to long when doing the check.
  1145. */
  1146. if (likely((unsigned long)address != 0))
  1147. min_alignment = min(addr_alignment, size_alignment);
  1148. else
  1149. min_alignment = size_alignment;
  1150. flush_size = 1ul << min_alignment;
  1151. __domain_flush_pages(domain, address, flush_size, pde);
  1152. address += flush_size;
  1153. size -= flush_size;
  1154. }
  1155. }
  1156. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  1157. void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
  1158. {
  1159. domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  1160. }
  1161. void amd_iommu_domain_flush_complete(struct protection_domain *domain)
  1162. {
  1163. int i;
  1164. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1165. if (domain && !domain->dev_iommu[i])
  1166. continue;
  1167. /*
  1168. * Devices of this domain are behind this IOMMU
  1169. * We need to wait for completion of all commands.
  1170. */
  1171. iommu_completion_wait(amd_iommus[i]);
  1172. }
  1173. }
  1174. /* Flush the not present cache if it exists */
  1175. static void domain_flush_np_cache(struct protection_domain *domain,
  1176. dma_addr_t iova, size_t size)
  1177. {
  1178. if (unlikely(amd_iommu_np_cache)) {
  1179. unsigned long flags;
  1180. spin_lock_irqsave(&domain->lock, flags);
  1181. domain_flush_pages(domain, iova, size, 1);
  1182. amd_iommu_domain_flush_complete(domain);
  1183. spin_unlock_irqrestore(&domain->lock, flags);
  1184. }
  1185. }
  1186. /*
  1187. * This function flushes the DTEs for all devices in domain
  1188. */
  1189. static void domain_flush_devices(struct protection_domain *domain)
  1190. {
  1191. struct iommu_dev_data *dev_data;
  1192. list_for_each_entry(dev_data, &domain->dev_list, list)
  1193. device_flush_dte(dev_data);
  1194. }
  1195. /****************************************************************************
  1196. *
  1197. * The next functions belong to the domain allocation. A domain is
  1198. * allocated for every IOMMU as the default domain. If device isolation
  1199. * is enabled, every device get its own domain. The most important thing
  1200. * about domains is the page table mapping the DMA address space they
  1201. * contain.
  1202. *
  1203. ****************************************************************************/
  1204. static u16 domain_id_alloc(void)
  1205. {
  1206. int id;
  1207. spin_lock(&pd_bitmap_lock);
  1208. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1209. BUG_ON(id == 0);
  1210. if (id > 0 && id < MAX_DOMAIN_ID)
  1211. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1212. else
  1213. id = 0;
  1214. spin_unlock(&pd_bitmap_lock);
  1215. return id;
  1216. }
  1217. static void domain_id_free(int id)
  1218. {
  1219. spin_lock(&pd_bitmap_lock);
  1220. if (id > 0 && id < MAX_DOMAIN_ID)
  1221. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1222. spin_unlock(&pd_bitmap_lock);
  1223. }
  1224. static void free_gcr3_tbl_level1(u64 *tbl)
  1225. {
  1226. u64 *ptr;
  1227. int i;
  1228. for (i = 0; i < 512; ++i) {
  1229. if (!(tbl[i] & GCR3_VALID))
  1230. continue;
  1231. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1232. free_page((unsigned long)ptr);
  1233. }
  1234. }
  1235. static void free_gcr3_tbl_level2(u64 *tbl)
  1236. {
  1237. u64 *ptr;
  1238. int i;
  1239. for (i = 0; i < 512; ++i) {
  1240. if (!(tbl[i] & GCR3_VALID))
  1241. continue;
  1242. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1243. free_gcr3_tbl_level1(ptr);
  1244. }
  1245. }
  1246. static void free_gcr3_table(struct protection_domain *domain)
  1247. {
  1248. if (domain->glx == 2)
  1249. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1250. else if (domain->glx == 1)
  1251. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1252. else
  1253. BUG_ON(domain->glx != 0);
  1254. free_page((unsigned long)domain->gcr3_tbl);
  1255. }
  1256. static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
  1257. struct protection_domain *domain, bool ats, bool ppr)
  1258. {
  1259. u64 pte_root = 0;
  1260. u64 flags = 0;
  1261. u32 old_domid;
  1262. struct dev_table_entry *dev_table = get_dev_table(iommu);
  1263. if (domain->iop.mode != PAGE_MODE_NONE)
  1264. pte_root = iommu_virt_to_phys(domain->iop.root);
  1265. pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
  1266. << DEV_ENTRY_MODE_SHIFT;
  1267. pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
  1268. /*
  1269. * When SNP is enabled, Only set TV bit when IOMMU
  1270. * page translation is in use.
  1271. */
  1272. if (!amd_iommu_snp_en || (domain->id != 0))
  1273. pte_root |= DTE_FLAG_TV;
  1274. flags = dev_table[devid].data[1];
  1275. if (ats)
  1276. flags |= DTE_FLAG_IOTLB;
  1277. if (ppr) {
  1278. if (iommu_feature(iommu, FEATURE_EPHSUP))
  1279. pte_root |= 1ULL << DEV_ENTRY_PPR;
  1280. }
  1281. if (domain->flags & PD_IOMMUV2_MASK) {
  1282. u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
  1283. u64 glx = domain->glx;
  1284. u64 tmp;
  1285. pte_root |= DTE_FLAG_GV;
  1286. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1287. /* First mask out possible old values for GCR3 table */
  1288. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1289. flags &= ~tmp;
  1290. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1291. flags &= ~tmp;
  1292. /* Encode GCR3 table into DTE */
  1293. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1294. pte_root |= tmp;
  1295. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1296. flags |= tmp;
  1297. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1298. flags |= tmp;
  1299. if (domain->flags & PD_GIOV_MASK)
  1300. pte_root |= DTE_FLAG_GIOV;
  1301. }
  1302. flags &= ~DEV_DOMID_MASK;
  1303. flags |= domain->id;
  1304. old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
  1305. dev_table[devid].data[1] = flags;
  1306. dev_table[devid].data[0] = pte_root;
  1307. /*
  1308. * A kdump kernel might be replacing a domain ID that was copied from
  1309. * the previous kernel--if so, it needs to flush the translation cache
  1310. * entries for the old domain ID that is being overwritten
  1311. */
  1312. if (old_domid) {
  1313. amd_iommu_flush_tlb_domid(iommu, old_domid);
  1314. }
  1315. }
  1316. static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
  1317. {
  1318. struct dev_table_entry *dev_table = get_dev_table(iommu);
  1319. /* remove entry from the device table seen by the hardware */
  1320. dev_table[devid].data[0] = DTE_FLAG_V;
  1321. if (!amd_iommu_snp_en)
  1322. dev_table[devid].data[0] |= DTE_FLAG_TV;
  1323. dev_table[devid].data[1] &= DTE_FLAG_MASK;
  1324. amd_iommu_apply_erratum_63(iommu, devid);
  1325. }
  1326. static void do_attach(struct iommu_dev_data *dev_data,
  1327. struct protection_domain *domain)
  1328. {
  1329. struct amd_iommu *iommu;
  1330. bool ats;
  1331. iommu = rlookup_amd_iommu(dev_data->dev);
  1332. if (!iommu)
  1333. return;
  1334. ats = dev_data->ats.enabled;
  1335. /* Update data structures */
  1336. dev_data->domain = domain;
  1337. list_add(&dev_data->list, &domain->dev_list);
  1338. /* Do reference counting */
  1339. domain->dev_iommu[iommu->index] += 1;
  1340. domain->dev_cnt += 1;
  1341. /* Update device table */
  1342. set_dte_entry(iommu, dev_data->devid, domain,
  1343. ats, dev_data->iommu_v2);
  1344. clone_aliases(iommu, dev_data->dev);
  1345. device_flush_dte(dev_data);
  1346. }
  1347. static void do_detach(struct iommu_dev_data *dev_data)
  1348. {
  1349. struct protection_domain *domain = dev_data->domain;
  1350. struct amd_iommu *iommu;
  1351. iommu = rlookup_amd_iommu(dev_data->dev);
  1352. if (!iommu)
  1353. return;
  1354. /* Update data structures */
  1355. dev_data->domain = NULL;
  1356. list_del(&dev_data->list);
  1357. clear_dte_entry(iommu, dev_data->devid);
  1358. clone_aliases(iommu, dev_data->dev);
  1359. /* Flush the DTE entry */
  1360. device_flush_dte(dev_data);
  1361. /* Flush IOTLB */
  1362. amd_iommu_domain_flush_tlb_pde(domain);
  1363. /* Wait for the flushes to finish */
  1364. amd_iommu_domain_flush_complete(domain);
  1365. /* decrease reference counters - needs to happen after the flushes */
  1366. domain->dev_iommu[iommu->index] -= 1;
  1367. domain->dev_cnt -= 1;
  1368. }
  1369. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1370. {
  1371. pci_disable_ats(pdev);
  1372. pci_disable_pri(pdev);
  1373. pci_disable_pasid(pdev);
  1374. }
  1375. static int pdev_pri_ats_enable(struct pci_dev *pdev)
  1376. {
  1377. int ret;
  1378. /* Only allow access to user-accessible pages */
  1379. ret = pci_enable_pasid(pdev, 0);
  1380. if (ret)
  1381. return ret;
  1382. /* First reset the PRI state of the device */
  1383. ret = pci_reset_pri(pdev);
  1384. if (ret)
  1385. goto out_err_pasid;
  1386. /* Enable PRI */
  1387. /* FIXME: Hardcode number of outstanding requests for now */
  1388. ret = pci_enable_pri(pdev, 32);
  1389. if (ret)
  1390. goto out_err_pasid;
  1391. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1392. if (ret)
  1393. goto out_err_pri;
  1394. return 0;
  1395. out_err_pri:
  1396. pci_disable_pri(pdev);
  1397. out_err_pasid:
  1398. pci_disable_pasid(pdev);
  1399. return ret;
  1400. }
  1401. /*
  1402. * If a device is not yet associated with a domain, this function makes the
  1403. * device visible in the domain
  1404. */
  1405. static int attach_device(struct device *dev,
  1406. struct protection_domain *domain)
  1407. {
  1408. struct iommu_dev_data *dev_data;
  1409. struct pci_dev *pdev;
  1410. unsigned long flags;
  1411. int ret;
  1412. spin_lock_irqsave(&domain->lock, flags);
  1413. dev_data = dev_iommu_priv_get(dev);
  1414. spin_lock(&dev_data->lock);
  1415. ret = -EBUSY;
  1416. if (dev_data->domain != NULL)
  1417. goto out;
  1418. if (!dev_is_pci(dev))
  1419. goto skip_ats_check;
  1420. pdev = to_pci_dev(dev);
  1421. if (domain->flags & PD_IOMMUV2_MASK) {
  1422. struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
  1423. ret = -EINVAL;
  1424. /*
  1425. * In case of using AMD_IOMMU_V1 page table mode and the device
  1426. * is enabling for PPR/ATS support (using v2 table),
  1427. * we need to make sure that the domain type is identity map.
  1428. */
  1429. if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
  1430. def_domain->type != IOMMU_DOMAIN_IDENTITY) {
  1431. goto out;
  1432. }
  1433. if (dev_data->iommu_v2) {
  1434. if (pdev_pri_ats_enable(pdev) != 0)
  1435. goto out;
  1436. dev_data->ats.enabled = true;
  1437. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1438. dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
  1439. }
  1440. } else if (amd_iommu_iotlb_sup &&
  1441. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1442. dev_data->ats.enabled = true;
  1443. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1444. }
  1445. skip_ats_check:
  1446. ret = 0;
  1447. do_attach(dev_data, domain);
  1448. /*
  1449. * We might boot into a crash-kernel here. The crashed kernel
  1450. * left the caches in the IOMMU dirty. So we have to flush
  1451. * here to evict all dirty stuff.
  1452. */
  1453. amd_iommu_domain_flush_tlb_pde(domain);
  1454. amd_iommu_domain_flush_complete(domain);
  1455. out:
  1456. spin_unlock(&dev_data->lock);
  1457. spin_unlock_irqrestore(&domain->lock, flags);
  1458. return ret;
  1459. }
  1460. /*
  1461. * Removes a device from a protection domain (with devtable_lock held)
  1462. */
  1463. static void detach_device(struct device *dev)
  1464. {
  1465. struct protection_domain *domain;
  1466. struct iommu_dev_data *dev_data;
  1467. unsigned long flags;
  1468. dev_data = dev_iommu_priv_get(dev);
  1469. domain = dev_data->domain;
  1470. spin_lock_irqsave(&domain->lock, flags);
  1471. spin_lock(&dev_data->lock);
  1472. /*
  1473. * First check if the device is still attached. It might already
  1474. * be detached from its domain because the generic
  1475. * iommu_detach_group code detached it and we try again here in
  1476. * our alias handling.
  1477. */
  1478. if (WARN_ON(!dev_data->domain))
  1479. goto out;
  1480. do_detach(dev_data);
  1481. if (!dev_is_pci(dev))
  1482. goto out;
  1483. if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
  1484. pdev_iommuv2_disable(to_pci_dev(dev));
  1485. else if (dev_data->ats.enabled)
  1486. pci_disable_ats(to_pci_dev(dev));
  1487. dev_data->ats.enabled = false;
  1488. out:
  1489. spin_unlock(&dev_data->lock);
  1490. spin_unlock_irqrestore(&domain->lock, flags);
  1491. }
  1492. static struct iommu_device *amd_iommu_probe_device(struct device *dev)
  1493. {
  1494. struct iommu_device *iommu_dev;
  1495. struct amd_iommu *iommu;
  1496. int ret;
  1497. if (!check_device(dev))
  1498. return ERR_PTR(-ENODEV);
  1499. iommu = rlookup_amd_iommu(dev);
  1500. if (!iommu)
  1501. return ERR_PTR(-ENODEV);
  1502. /* Not registered yet? */
  1503. if (!iommu->iommu.ops)
  1504. return ERR_PTR(-ENODEV);
  1505. if (dev_iommu_priv_get(dev))
  1506. return &iommu->iommu;
  1507. ret = iommu_init_device(iommu, dev);
  1508. if (ret) {
  1509. if (ret != -ENOTSUPP)
  1510. dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
  1511. iommu_dev = ERR_PTR(ret);
  1512. iommu_ignore_device(iommu, dev);
  1513. } else {
  1514. amd_iommu_set_pci_msi_domain(dev, iommu);
  1515. iommu_dev = &iommu->iommu;
  1516. }
  1517. iommu_completion_wait(iommu);
  1518. return iommu_dev;
  1519. }
  1520. static void amd_iommu_probe_finalize(struct device *dev)
  1521. {
  1522. /* Domains are initialized for this device - have a look what we ended up with */
  1523. set_dma_ops(dev, NULL);
  1524. iommu_setup_dma_ops(dev, 0, U64_MAX);
  1525. }
  1526. static void amd_iommu_release_device(struct device *dev)
  1527. {
  1528. struct amd_iommu *iommu;
  1529. if (!check_device(dev))
  1530. return;
  1531. iommu = rlookup_amd_iommu(dev);
  1532. if (!iommu)
  1533. return;
  1534. amd_iommu_uninit_device(dev);
  1535. iommu_completion_wait(iommu);
  1536. }
  1537. static struct iommu_group *amd_iommu_device_group(struct device *dev)
  1538. {
  1539. if (dev_is_pci(dev))
  1540. return pci_device_group(dev);
  1541. return acpihid_device_group(dev);
  1542. }
  1543. /*****************************************************************************
  1544. *
  1545. * The next functions belong to the dma_ops mapping/unmapping code.
  1546. *
  1547. *****************************************************************************/
  1548. static void update_device_table(struct protection_domain *domain)
  1549. {
  1550. struct iommu_dev_data *dev_data;
  1551. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1552. struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
  1553. if (!iommu)
  1554. continue;
  1555. set_dte_entry(iommu, dev_data->devid, domain,
  1556. dev_data->ats.enabled, dev_data->iommu_v2);
  1557. clone_aliases(iommu, dev_data->dev);
  1558. }
  1559. }
  1560. void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
  1561. {
  1562. update_device_table(domain);
  1563. domain_flush_devices(domain);
  1564. }
  1565. void amd_iommu_domain_update(struct protection_domain *domain)
  1566. {
  1567. /* Update device table */
  1568. amd_iommu_update_and_flush_device_table(domain);
  1569. /* Flush domain TLB(s) and wait for completion */
  1570. amd_iommu_domain_flush_tlb_pde(domain);
  1571. amd_iommu_domain_flush_complete(domain);
  1572. }
  1573. /*****************************************************************************
  1574. *
  1575. * The following functions belong to the exported interface of AMD IOMMU
  1576. *
  1577. * This interface allows access to lower level functions of the IOMMU
  1578. * like protection domain handling and assignement of devices to domains
  1579. * which is not possible with the dma_ops interface.
  1580. *
  1581. *****************************************************************************/
  1582. static void cleanup_domain(struct protection_domain *domain)
  1583. {
  1584. struct iommu_dev_data *entry;
  1585. unsigned long flags;
  1586. spin_lock_irqsave(&domain->lock, flags);
  1587. while (!list_empty(&domain->dev_list)) {
  1588. entry = list_first_entry(&domain->dev_list,
  1589. struct iommu_dev_data, list);
  1590. BUG_ON(!entry->domain);
  1591. do_detach(entry);
  1592. }
  1593. spin_unlock_irqrestore(&domain->lock, flags);
  1594. }
  1595. static void protection_domain_free(struct protection_domain *domain)
  1596. {
  1597. if (!domain)
  1598. return;
  1599. if (domain->iop.pgtbl_cfg.tlb)
  1600. free_io_pgtable_ops(&domain->iop.iop.ops);
  1601. if (domain->id)
  1602. domain_id_free(domain->id);
  1603. kfree(domain);
  1604. }
  1605. static int protection_domain_init_v1(struct protection_domain *domain, int mode)
  1606. {
  1607. u64 *pt_root = NULL;
  1608. BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
  1609. spin_lock_init(&domain->lock);
  1610. domain->id = domain_id_alloc();
  1611. if (!domain->id)
  1612. return -ENOMEM;
  1613. INIT_LIST_HEAD(&domain->dev_list);
  1614. if (mode != PAGE_MODE_NONE) {
  1615. pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1616. if (!pt_root) {
  1617. domain_id_free(domain->id);
  1618. return -ENOMEM;
  1619. }
  1620. }
  1621. amd_iommu_domain_set_pgtable(domain, pt_root, mode);
  1622. return 0;
  1623. }
  1624. static int protection_domain_init_v2(struct protection_domain *domain)
  1625. {
  1626. spin_lock_init(&domain->lock);
  1627. domain->id = domain_id_alloc();
  1628. if (!domain->id)
  1629. return -ENOMEM;
  1630. INIT_LIST_HEAD(&domain->dev_list);
  1631. domain->flags |= PD_GIOV_MASK;
  1632. domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
  1633. if (domain_enable_v2(domain, 1)) {
  1634. domain_id_free(domain->id);
  1635. return -ENOMEM;
  1636. }
  1637. return 0;
  1638. }
  1639. static struct protection_domain *protection_domain_alloc(unsigned int type)
  1640. {
  1641. struct io_pgtable_ops *pgtbl_ops;
  1642. struct protection_domain *domain;
  1643. int pgtable;
  1644. int mode = DEFAULT_PGTABLE_LEVEL;
  1645. int ret;
  1646. /*
  1647. * Force IOMMU v1 page table when iommu=pt and
  1648. * when allocating domain for pass-through devices.
  1649. */
  1650. if (type == IOMMU_DOMAIN_IDENTITY) {
  1651. pgtable = AMD_IOMMU_V1;
  1652. mode = PAGE_MODE_NONE;
  1653. } else if (type == IOMMU_DOMAIN_UNMANAGED) {
  1654. pgtable = AMD_IOMMU_V1;
  1655. } else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
  1656. pgtable = amd_iommu_pgtable;
  1657. } else {
  1658. return NULL;
  1659. }
  1660. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1661. if (!domain)
  1662. return NULL;
  1663. switch (pgtable) {
  1664. case AMD_IOMMU_V1:
  1665. ret = protection_domain_init_v1(domain, mode);
  1666. break;
  1667. case AMD_IOMMU_V2:
  1668. ret = protection_domain_init_v2(domain);
  1669. break;
  1670. default:
  1671. ret = -EINVAL;
  1672. }
  1673. if (ret)
  1674. goto out_err;
  1675. pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
  1676. if (!pgtbl_ops) {
  1677. domain_id_free(domain->id);
  1678. goto out_err;
  1679. }
  1680. return domain;
  1681. out_err:
  1682. kfree(domain);
  1683. return NULL;
  1684. }
  1685. static inline u64 dma_max_address(void)
  1686. {
  1687. if (amd_iommu_pgtable == AMD_IOMMU_V1)
  1688. return ~0ULL;
  1689. /* V2 with 4 level page table */
  1690. return ((1ULL << PM_LEVEL_SHIFT(PAGE_MODE_4_LEVEL)) - 1);
  1691. }
  1692. static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
  1693. {
  1694. struct protection_domain *domain;
  1695. /*
  1696. * Since DTE[Mode]=0 is prohibited on SNP-enabled system,
  1697. * default to use IOMMU_DOMAIN_DMA[_FQ].
  1698. */
  1699. if (amd_iommu_snp_en && (type == IOMMU_DOMAIN_IDENTITY))
  1700. return NULL;
  1701. domain = protection_domain_alloc(type);
  1702. if (!domain)
  1703. return NULL;
  1704. domain->domain.geometry.aperture_start = 0;
  1705. domain->domain.geometry.aperture_end = dma_max_address();
  1706. domain->domain.geometry.force_aperture = true;
  1707. return &domain->domain;
  1708. }
  1709. static void amd_iommu_domain_free(struct iommu_domain *dom)
  1710. {
  1711. struct protection_domain *domain;
  1712. domain = to_pdomain(dom);
  1713. if (domain->dev_cnt > 0)
  1714. cleanup_domain(domain);
  1715. BUG_ON(domain->dev_cnt != 0);
  1716. if (!dom)
  1717. return;
  1718. if (domain->flags & PD_IOMMUV2_MASK)
  1719. free_gcr3_table(domain);
  1720. protection_domain_free(domain);
  1721. }
  1722. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1723. struct device *dev)
  1724. {
  1725. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  1726. struct amd_iommu *iommu;
  1727. if (!check_device(dev))
  1728. return;
  1729. if (dev_data->domain != NULL)
  1730. detach_device(dev);
  1731. iommu = rlookup_amd_iommu(dev);
  1732. if (!iommu)
  1733. return;
  1734. #ifdef CONFIG_IRQ_REMAP
  1735. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  1736. (dom->type == IOMMU_DOMAIN_UNMANAGED))
  1737. dev_data->use_vapic = 0;
  1738. #endif
  1739. iommu_completion_wait(iommu);
  1740. }
  1741. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1742. struct device *dev)
  1743. {
  1744. struct protection_domain *domain = to_pdomain(dom);
  1745. struct iommu_dev_data *dev_data;
  1746. struct amd_iommu *iommu;
  1747. int ret;
  1748. if (!check_device(dev))
  1749. return -EINVAL;
  1750. dev_data = dev_iommu_priv_get(dev);
  1751. dev_data->defer_attach = false;
  1752. iommu = rlookup_amd_iommu(dev);
  1753. if (!iommu)
  1754. return -EINVAL;
  1755. if (dev_data->domain)
  1756. detach_device(dev);
  1757. ret = attach_device(dev, domain);
  1758. #ifdef CONFIG_IRQ_REMAP
  1759. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  1760. if (dom->type == IOMMU_DOMAIN_UNMANAGED)
  1761. dev_data->use_vapic = 1;
  1762. else
  1763. dev_data->use_vapic = 0;
  1764. }
  1765. #endif
  1766. iommu_completion_wait(iommu);
  1767. return ret;
  1768. }
  1769. static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
  1770. unsigned long iova, size_t size)
  1771. {
  1772. struct protection_domain *domain = to_pdomain(dom);
  1773. struct io_pgtable_ops *ops = &domain->iop.iop.ops;
  1774. if (ops->map_pages)
  1775. domain_flush_np_cache(domain, iova, size);
  1776. }
  1777. static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
  1778. phys_addr_t paddr, size_t pgsize, size_t pgcount,
  1779. int iommu_prot, gfp_t gfp, size_t *mapped)
  1780. {
  1781. struct protection_domain *domain = to_pdomain(dom);
  1782. struct io_pgtable_ops *ops = &domain->iop.iop.ops;
  1783. int prot = 0;
  1784. int ret = -EINVAL;
  1785. if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
  1786. (domain->iop.mode == PAGE_MODE_NONE))
  1787. return -EINVAL;
  1788. if (iommu_prot & IOMMU_READ)
  1789. prot |= IOMMU_PROT_IR;
  1790. if (iommu_prot & IOMMU_WRITE)
  1791. prot |= IOMMU_PROT_IW;
  1792. if (ops->map_pages) {
  1793. ret = ops->map_pages(ops, iova, paddr, pgsize,
  1794. pgcount, prot, gfp, mapped);
  1795. }
  1796. return ret;
  1797. }
  1798. static void amd_iommu_iotlb_gather_add_page(struct iommu_domain *domain,
  1799. struct iommu_iotlb_gather *gather,
  1800. unsigned long iova, size_t size)
  1801. {
  1802. /*
  1803. * AMD's IOMMU can flush as many pages as necessary in a single flush.
  1804. * Unless we run in a virtual machine, which can be inferred according
  1805. * to whether "non-present cache" is on, it is probably best to prefer
  1806. * (potentially) too extensive TLB flushing (i.e., more misses) over
  1807. * mutliple TLB flushes (i.e., more flushes). For virtual machines the
  1808. * hypervisor needs to synchronize the host IOMMU PTEs with those of
  1809. * the guest, and the trade-off is different: unnecessary TLB flushes
  1810. * should be avoided.
  1811. */
  1812. if (amd_iommu_np_cache &&
  1813. iommu_iotlb_gather_is_disjoint(gather, iova, size))
  1814. iommu_iotlb_sync(domain, gather);
  1815. iommu_iotlb_gather_add_range(gather, iova, size);
  1816. }
  1817. static size_t amd_iommu_unmap_pages(struct iommu_domain *dom, unsigned long iova,
  1818. size_t pgsize, size_t pgcount,
  1819. struct iommu_iotlb_gather *gather)
  1820. {
  1821. struct protection_domain *domain = to_pdomain(dom);
  1822. struct io_pgtable_ops *ops = &domain->iop.iop.ops;
  1823. size_t r;
  1824. if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
  1825. (domain->iop.mode == PAGE_MODE_NONE))
  1826. return 0;
  1827. r = (ops->unmap_pages) ? ops->unmap_pages(ops, iova, pgsize, pgcount, NULL) : 0;
  1828. if (r)
  1829. amd_iommu_iotlb_gather_add_page(dom, gather, iova, r);
  1830. return r;
  1831. }
  1832. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1833. dma_addr_t iova)
  1834. {
  1835. struct protection_domain *domain = to_pdomain(dom);
  1836. struct io_pgtable_ops *ops = &domain->iop.iop.ops;
  1837. return ops->iova_to_phys(ops, iova);
  1838. }
  1839. static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
  1840. {
  1841. switch (cap) {
  1842. case IOMMU_CAP_CACHE_COHERENCY:
  1843. return true;
  1844. case IOMMU_CAP_INTR_REMAP:
  1845. return (irq_remapping_enabled == 1);
  1846. case IOMMU_CAP_NOEXEC:
  1847. return false;
  1848. case IOMMU_CAP_PRE_BOOT_PROTECTION:
  1849. return amdr_ivrs_remap_support;
  1850. default:
  1851. break;
  1852. }
  1853. return false;
  1854. }
  1855. static void amd_iommu_get_resv_regions(struct device *dev,
  1856. struct list_head *head)
  1857. {
  1858. struct iommu_resv_region *region;
  1859. struct unity_map_entry *entry;
  1860. struct amd_iommu *iommu;
  1861. struct amd_iommu_pci_seg *pci_seg;
  1862. int devid, sbdf;
  1863. sbdf = get_device_sbdf_id(dev);
  1864. if (sbdf < 0)
  1865. return;
  1866. devid = PCI_SBDF_TO_DEVID(sbdf);
  1867. iommu = rlookup_amd_iommu(dev);
  1868. if (!iommu)
  1869. return;
  1870. pci_seg = iommu->pci_seg;
  1871. list_for_each_entry(entry, &pci_seg->unity_map, list) {
  1872. int type, prot = 0;
  1873. size_t length;
  1874. if (devid < entry->devid_start || devid > entry->devid_end)
  1875. continue;
  1876. type = IOMMU_RESV_DIRECT;
  1877. length = entry->address_end - entry->address_start;
  1878. if (entry->prot & IOMMU_PROT_IR)
  1879. prot |= IOMMU_READ;
  1880. if (entry->prot & IOMMU_PROT_IW)
  1881. prot |= IOMMU_WRITE;
  1882. if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
  1883. /* Exclusion range */
  1884. type = IOMMU_RESV_RESERVED;
  1885. region = iommu_alloc_resv_region(entry->address_start,
  1886. length, prot, type,
  1887. GFP_KERNEL);
  1888. if (!region) {
  1889. dev_err(dev, "Out of memory allocating dm-regions\n");
  1890. return;
  1891. }
  1892. list_add_tail(&region->list, head);
  1893. }
  1894. region = iommu_alloc_resv_region(MSI_RANGE_START,
  1895. MSI_RANGE_END - MSI_RANGE_START + 1,
  1896. 0, IOMMU_RESV_MSI, GFP_KERNEL);
  1897. if (!region)
  1898. return;
  1899. list_add_tail(&region->list, head);
  1900. region = iommu_alloc_resv_region(HT_RANGE_START,
  1901. HT_RANGE_END - HT_RANGE_START + 1,
  1902. 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
  1903. if (!region)
  1904. return;
  1905. list_add_tail(&region->list, head);
  1906. }
  1907. bool amd_iommu_is_attach_deferred(struct device *dev)
  1908. {
  1909. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  1910. return dev_data->defer_attach;
  1911. }
  1912. EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
  1913. static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
  1914. {
  1915. struct protection_domain *dom = to_pdomain(domain);
  1916. unsigned long flags;
  1917. spin_lock_irqsave(&dom->lock, flags);
  1918. amd_iommu_domain_flush_tlb_pde(dom);
  1919. amd_iommu_domain_flush_complete(dom);
  1920. spin_unlock_irqrestore(&dom->lock, flags);
  1921. }
  1922. static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
  1923. struct iommu_iotlb_gather *gather)
  1924. {
  1925. struct protection_domain *dom = to_pdomain(domain);
  1926. unsigned long flags;
  1927. spin_lock_irqsave(&dom->lock, flags);
  1928. domain_flush_pages(dom, gather->start, gather->end - gather->start + 1, 1);
  1929. amd_iommu_domain_flush_complete(dom);
  1930. spin_unlock_irqrestore(&dom->lock, flags);
  1931. }
  1932. static int amd_iommu_def_domain_type(struct device *dev)
  1933. {
  1934. struct iommu_dev_data *dev_data;
  1935. dev_data = dev_iommu_priv_get(dev);
  1936. if (!dev_data)
  1937. return 0;
  1938. /*
  1939. * Do not identity map IOMMUv2 capable devices when:
  1940. * - memory encryption is active, because some of those devices
  1941. * (AMD GPUs) don't have the encryption bit in their DMA-mask
  1942. * and require remapping.
  1943. * - SNP is enabled, because it prohibits DTE[Mode]=0.
  1944. */
  1945. if (dev_data->iommu_v2 &&
  1946. !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
  1947. !amd_iommu_snp_en) {
  1948. return IOMMU_DOMAIN_IDENTITY;
  1949. }
  1950. return 0;
  1951. }
  1952. static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
  1953. {
  1954. /* IOMMU_PTE_FC is always set */
  1955. return true;
  1956. }
  1957. const struct iommu_ops amd_iommu_ops = {
  1958. .capable = amd_iommu_capable,
  1959. .domain_alloc = amd_iommu_domain_alloc,
  1960. .probe_device = amd_iommu_probe_device,
  1961. .release_device = amd_iommu_release_device,
  1962. .probe_finalize = amd_iommu_probe_finalize,
  1963. .device_group = amd_iommu_device_group,
  1964. .get_resv_regions = amd_iommu_get_resv_regions,
  1965. .is_attach_deferred = amd_iommu_is_attach_deferred,
  1966. .pgsize_bitmap = AMD_IOMMU_PGSIZES,
  1967. .def_domain_type = amd_iommu_def_domain_type,
  1968. .default_domain_ops = &(const struct iommu_domain_ops) {
  1969. .attach_dev = amd_iommu_attach_device,
  1970. .detach_dev = amd_iommu_detach_device,
  1971. .map_pages = amd_iommu_map_pages,
  1972. .unmap_pages = amd_iommu_unmap_pages,
  1973. .iotlb_sync_map = amd_iommu_iotlb_sync_map,
  1974. .iova_to_phys = amd_iommu_iova_to_phys,
  1975. .flush_iotlb_all = amd_iommu_flush_iotlb_all,
  1976. .iotlb_sync = amd_iommu_iotlb_sync,
  1977. .free = amd_iommu_domain_free,
  1978. .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
  1979. }
  1980. };
  1981. /*****************************************************************************
  1982. *
  1983. * The next functions do a basic initialization of IOMMU for pass through
  1984. * mode
  1985. *
  1986. * In passthrough mode the IOMMU is initialized and enabled but not used for
  1987. * DMA-API translation.
  1988. *
  1989. *****************************************************************************/
  1990. /* IOMMUv2 specific functions */
  1991. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  1992. {
  1993. return atomic_notifier_chain_register(&ppr_notifier, nb);
  1994. }
  1995. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  1996. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  1997. {
  1998. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  1999. }
  2000. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2001. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2002. {
  2003. struct protection_domain *domain = to_pdomain(dom);
  2004. unsigned long flags;
  2005. spin_lock_irqsave(&domain->lock, flags);
  2006. if (domain->iop.pgtbl_cfg.tlb)
  2007. free_io_pgtable_ops(&domain->iop.iop.ops);
  2008. spin_unlock_irqrestore(&domain->lock, flags);
  2009. }
  2010. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2011. /* Note: This function expects iommu_domain->lock to be held prior calling the function. */
  2012. static int domain_enable_v2(struct protection_domain *domain, int pasids)
  2013. {
  2014. int levels;
  2015. /* Number of GCR3 table levels required */
  2016. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2017. levels += 1;
  2018. if (levels > amd_iommu_max_glx_val)
  2019. return -EINVAL;
  2020. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2021. if (domain->gcr3_tbl == NULL)
  2022. return -ENOMEM;
  2023. domain->glx = levels;
  2024. domain->flags |= PD_IOMMUV2_MASK;
  2025. amd_iommu_domain_update(domain);
  2026. return 0;
  2027. }
  2028. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2029. {
  2030. struct protection_domain *pdom = to_pdomain(dom);
  2031. unsigned long flags;
  2032. int ret;
  2033. spin_lock_irqsave(&pdom->lock, flags);
  2034. /*
  2035. * Save us all sanity checks whether devices already in the
  2036. * domain support IOMMUv2. Just force that the domain has no
  2037. * devices attached when it is switched into IOMMUv2 mode.
  2038. */
  2039. ret = -EBUSY;
  2040. if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK)
  2041. goto out;
  2042. if (!pdom->gcr3_tbl)
  2043. ret = domain_enable_v2(pdom, pasids);
  2044. out:
  2045. spin_unlock_irqrestore(&pdom->lock, flags);
  2046. return ret;
  2047. }
  2048. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2049. static int __flush_pasid(struct protection_domain *domain, u32 pasid,
  2050. u64 address, bool size)
  2051. {
  2052. struct iommu_dev_data *dev_data;
  2053. struct iommu_cmd cmd;
  2054. int i, ret;
  2055. if (!(domain->flags & PD_IOMMUV2_MASK))
  2056. return -EINVAL;
  2057. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2058. /*
  2059. * IOMMU TLB needs to be flushed before Device TLB to
  2060. * prevent device TLB refill from IOMMU TLB
  2061. */
  2062. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  2063. if (domain->dev_iommu[i] == 0)
  2064. continue;
  2065. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2066. if (ret != 0)
  2067. goto out;
  2068. }
  2069. /* Wait until IOMMU TLB flushes are complete */
  2070. amd_iommu_domain_flush_complete(domain);
  2071. /* Now flush device TLBs */
  2072. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2073. struct amd_iommu *iommu;
  2074. int qdep;
  2075. /*
  2076. There might be non-IOMMUv2 capable devices in an IOMMUv2
  2077. * domain.
  2078. */
  2079. if (!dev_data->ats.enabled)
  2080. continue;
  2081. qdep = dev_data->ats.qdep;
  2082. iommu = rlookup_amd_iommu(dev_data->dev);
  2083. if (!iommu)
  2084. continue;
  2085. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2086. qdep, address, size);
  2087. ret = iommu_queue_command(iommu, &cmd);
  2088. if (ret != 0)
  2089. goto out;
  2090. }
  2091. /* Wait until all device TLBs are flushed */
  2092. amd_iommu_domain_flush_complete(domain);
  2093. ret = 0;
  2094. out:
  2095. return ret;
  2096. }
  2097. static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
  2098. u64 address)
  2099. {
  2100. return __flush_pasid(domain, pasid, address, false);
  2101. }
  2102. int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
  2103. u64 address)
  2104. {
  2105. struct protection_domain *domain = to_pdomain(dom);
  2106. unsigned long flags;
  2107. int ret;
  2108. spin_lock_irqsave(&domain->lock, flags);
  2109. ret = __amd_iommu_flush_page(domain, pasid, address);
  2110. spin_unlock_irqrestore(&domain->lock, flags);
  2111. return ret;
  2112. }
  2113. EXPORT_SYMBOL(amd_iommu_flush_page);
  2114. static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
  2115. {
  2116. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2117. true);
  2118. }
  2119. int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
  2120. {
  2121. struct protection_domain *domain = to_pdomain(dom);
  2122. unsigned long flags;
  2123. int ret;
  2124. spin_lock_irqsave(&domain->lock, flags);
  2125. ret = __amd_iommu_flush_tlb(domain, pasid);
  2126. spin_unlock_irqrestore(&domain->lock, flags);
  2127. return ret;
  2128. }
  2129. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2130. static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
  2131. {
  2132. int index;
  2133. u64 *pte;
  2134. while (true) {
  2135. index = (pasid >> (9 * level)) & 0x1ff;
  2136. pte = &root[index];
  2137. if (level == 0)
  2138. break;
  2139. if (!(*pte & GCR3_VALID)) {
  2140. if (!alloc)
  2141. return NULL;
  2142. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2143. if (root == NULL)
  2144. return NULL;
  2145. *pte = iommu_virt_to_phys(root) | GCR3_VALID;
  2146. }
  2147. root = iommu_phys_to_virt(*pte & PAGE_MASK);
  2148. level -= 1;
  2149. }
  2150. return pte;
  2151. }
  2152. static int __set_gcr3(struct protection_domain *domain, u32 pasid,
  2153. unsigned long cr3)
  2154. {
  2155. u64 *pte;
  2156. if (domain->iop.mode != PAGE_MODE_NONE)
  2157. return -EINVAL;
  2158. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2159. if (pte == NULL)
  2160. return -ENOMEM;
  2161. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2162. return __amd_iommu_flush_tlb(domain, pasid);
  2163. }
  2164. static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
  2165. {
  2166. u64 *pte;
  2167. if (domain->iop.mode != PAGE_MODE_NONE)
  2168. return -EINVAL;
  2169. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2170. if (pte == NULL)
  2171. return 0;
  2172. *pte = 0;
  2173. return __amd_iommu_flush_tlb(domain, pasid);
  2174. }
  2175. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
  2176. unsigned long cr3)
  2177. {
  2178. struct protection_domain *domain = to_pdomain(dom);
  2179. unsigned long flags;
  2180. int ret;
  2181. spin_lock_irqsave(&domain->lock, flags);
  2182. ret = __set_gcr3(domain, pasid, cr3);
  2183. spin_unlock_irqrestore(&domain->lock, flags);
  2184. return ret;
  2185. }
  2186. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2187. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
  2188. {
  2189. struct protection_domain *domain = to_pdomain(dom);
  2190. unsigned long flags;
  2191. int ret;
  2192. spin_lock_irqsave(&domain->lock, flags);
  2193. ret = __clear_gcr3(domain, pasid);
  2194. spin_unlock_irqrestore(&domain->lock, flags);
  2195. return ret;
  2196. }
  2197. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2198. int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
  2199. int status, int tag)
  2200. {
  2201. struct iommu_dev_data *dev_data;
  2202. struct amd_iommu *iommu;
  2203. struct iommu_cmd cmd;
  2204. dev_data = dev_iommu_priv_get(&pdev->dev);
  2205. iommu = rlookup_amd_iommu(&pdev->dev);
  2206. if (!iommu)
  2207. return -ENODEV;
  2208. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2209. tag, dev_data->pri_tlp);
  2210. return iommu_queue_command(iommu, &cmd);
  2211. }
  2212. EXPORT_SYMBOL(amd_iommu_complete_ppr);
  2213. int amd_iommu_device_info(struct pci_dev *pdev,
  2214. struct amd_iommu_device_info *info)
  2215. {
  2216. int max_pasids;
  2217. int pos;
  2218. if (pdev == NULL || info == NULL)
  2219. return -EINVAL;
  2220. if (!amd_iommu_v2_supported())
  2221. return -EINVAL;
  2222. memset(info, 0, sizeof(*info));
  2223. if (pci_ats_supported(pdev))
  2224. info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  2225. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  2226. if (pos)
  2227. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  2228. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  2229. if (pos) {
  2230. int features;
  2231. max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
  2232. max_pasids = min(max_pasids, (1 << 20));
  2233. info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  2234. info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
  2235. features = pci_pasid_features(pdev);
  2236. if (features & PCI_PASID_CAP_EXEC)
  2237. info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  2238. if (features & PCI_PASID_CAP_PRIV)
  2239. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  2240. }
  2241. return 0;
  2242. }
  2243. EXPORT_SYMBOL(amd_iommu_device_info);
  2244. #ifdef CONFIG_IRQ_REMAP
  2245. /*****************************************************************************
  2246. *
  2247. * Interrupt Remapping Implementation
  2248. *
  2249. *****************************************************************************/
  2250. static struct irq_chip amd_ir_chip;
  2251. static DEFINE_SPINLOCK(iommu_table_lock);
  2252. static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
  2253. struct irq_remap_table *table)
  2254. {
  2255. u64 dte;
  2256. struct dev_table_entry *dev_table = get_dev_table(iommu);
  2257. dte = dev_table[devid].data[2];
  2258. dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
  2259. dte |= iommu_virt_to_phys(table->table);
  2260. dte |= DTE_IRQ_REMAP_INTCTL;
  2261. dte |= DTE_INTTABLEN;
  2262. dte |= DTE_IRQ_REMAP_ENABLE;
  2263. dev_table[devid].data[2] = dte;
  2264. }
  2265. static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
  2266. {
  2267. struct irq_remap_table *table;
  2268. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  2269. if (WARN_ONCE(!pci_seg->rlookup_table[devid],
  2270. "%s: no iommu for devid %x:%x\n",
  2271. __func__, pci_seg->id, devid))
  2272. return NULL;
  2273. table = pci_seg->irq_lookup_table[devid];
  2274. if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
  2275. __func__, pci_seg->id, devid))
  2276. return NULL;
  2277. return table;
  2278. }
  2279. static struct irq_remap_table *__alloc_irq_table(void)
  2280. {
  2281. struct irq_remap_table *table;
  2282. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2283. if (!table)
  2284. return NULL;
  2285. table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
  2286. if (!table->table) {
  2287. kfree(table);
  2288. return NULL;
  2289. }
  2290. raw_spin_lock_init(&table->lock);
  2291. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2292. memset(table->table, 0,
  2293. MAX_IRQS_PER_TABLE * sizeof(u32));
  2294. else
  2295. memset(table->table, 0,
  2296. (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
  2297. return table;
  2298. }
  2299. static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
  2300. struct irq_remap_table *table)
  2301. {
  2302. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  2303. pci_seg->irq_lookup_table[devid] = table;
  2304. set_dte_irq_entry(iommu, devid, table);
  2305. iommu_flush_dte(iommu, devid);
  2306. }
  2307. static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
  2308. void *data)
  2309. {
  2310. struct irq_remap_table *table = data;
  2311. struct amd_iommu_pci_seg *pci_seg;
  2312. struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
  2313. if (!iommu)
  2314. return -EINVAL;
  2315. pci_seg = iommu->pci_seg;
  2316. pci_seg->irq_lookup_table[alias] = table;
  2317. set_dte_irq_entry(iommu, alias, table);
  2318. iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
  2319. return 0;
  2320. }
  2321. static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
  2322. u16 devid, struct pci_dev *pdev)
  2323. {
  2324. struct irq_remap_table *table = NULL;
  2325. struct irq_remap_table *new_table = NULL;
  2326. struct amd_iommu_pci_seg *pci_seg;
  2327. unsigned long flags;
  2328. u16 alias;
  2329. spin_lock_irqsave(&iommu_table_lock, flags);
  2330. pci_seg = iommu->pci_seg;
  2331. table = pci_seg->irq_lookup_table[devid];
  2332. if (table)
  2333. goto out_unlock;
  2334. alias = pci_seg->alias_table[devid];
  2335. table = pci_seg->irq_lookup_table[alias];
  2336. if (table) {
  2337. set_remap_table_entry(iommu, devid, table);
  2338. goto out_wait;
  2339. }
  2340. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2341. /* Nothing there yet, allocate new irq remapping table */
  2342. new_table = __alloc_irq_table();
  2343. if (!new_table)
  2344. return NULL;
  2345. spin_lock_irqsave(&iommu_table_lock, flags);
  2346. table = pci_seg->irq_lookup_table[devid];
  2347. if (table)
  2348. goto out_unlock;
  2349. table = pci_seg->irq_lookup_table[alias];
  2350. if (table) {
  2351. set_remap_table_entry(iommu, devid, table);
  2352. goto out_wait;
  2353. }
  2354. table = new_table;
  2355. new_table = NULL;
  2356. if (pdev)
  2357. pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
  2358. table);
  2359. else
  2360. set_remap_table_entry(iommu, devid, table);
  2361. if (devid != alias)
  2362. set_remap_table_entry(iommu, alias, table);
  2363. out_wait:
  2364. iommu_completion_wait(iommu);
  2365. out_unlock:
  2366. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2367. if (new_table) {
  2368. kmem_cache_free(amd_iommu_irq_cache, new_table->table);
  2369. kfree(new_table);
  2370. }
  2371. return table;
  2372. }
  2373. static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
  2374. bool align, struct pci_dev *pdev)
  2375. {
  2376. struct irq_remap_table *table;
  2377. int index, c, alignment = 1;
  2378. unsigned long flags;
  2379. table = alloc_irq_table(iommu, devid, pdev);
  2380. if (!table)
  2381. return -ENODEV;
  2382. if (align)
  2383. alignment = roundup_pow_of_two(count);
  2384. raw_spin_lock_irqsave(&table->lock, flags);
  2385. /* Scan table for free entries */
  2386. for (index = ALIGN(table->min_index, alignment), c = 0;
  2387. index < MAX_IRQS_PER_TABLE;) {
  2388. if (!iommu->irte_ops->is_allocated(table, index)) {
  2389. c += 1;
  2390. } else {
  2391. c = 0;
  2392. index = ALIGN(index + 1, alignment);
  2393. continue;
  2394. }
  2395. if (c == count) {
  2396. for (; c != 0; --c)
  2397. iommu->irte_ops->set_allocated(table, index - c + 1);
  2398. index -= count - 1;
  2399. goto out;
  2400. }
  2401. index++;
  2402. }
  2403. index = -ENOSPC;
  2404. out:
  2405. raw_spin_unlock_irqrestore(&table->lock, flags);
  2406. return index;
  2407. }
  2408. static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
  2409. struct irte_ga *irte, struct amd_ir_data *data)
  2410. {
  2411. bool ret;
  2412. struct irq_remap_table *table;
  2413. unsigned long flags;
  2414. struct irte_ga *entry;
  2415. table = get_irq_table(iommu, devid);
  2416. if (!table)
  2417. return -ENOMEM;
  2418. raw_spin_lock_irqsave(&table->lock, flags);
  2419. entry = (struct irte_ga *)table->table;
  2420. entry = &entry[index];
  2421. ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
  2422. entry->lo.val, entry->hi.val,
  2423. irte->lo.val, irte->hi.val);
  2424. /*
  2425. * We use cmpxchg16 to atomically update the 128-bit IRTE,
  2426. * and it cannot be updated by the hardware or other processors
  2427. * behind us, so the return value of cmpxchg16 should be the
  2428. * same as the old value.
  2429. */
  2430. WARN_ON(!ret);
  2431. if (data)
  2432. data->ref = entry;
  2433. raw_spin_unlock_irqrestore(&table->lock, flags);
  2434. iommu_flush_irt(iommu, devid);
  2435. iommu_completion_wait(iommu);
  2436. return 0;
  2437. }
  2438. static int modify_irte(struct amd_iommu *iommu,
  2439. u16 devid, int index, union irte *irte)
  2440. {
  2441. struct irq_remap_table *table;
  2442. unsigned long flags;
  2443. table = get_irq_table(iommu, devid);
  2444. if (!table)
  2445. return -ENOMEM;
  2446. raw_spin_lock_irqsave(&table->lock, flags);
  2447. table->table[index] = irte->val;
  2448. raw_spin_unlock_irqrestore(&table->lock, flags);
  2449. iommu_flush_irt(iommu, devid);
  2450. iommu_completion_wait(iommu);
  2451. return 0;
  2452. }
  2453. static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
  2454. {
  2455. struct irq_remap_table *table;
  2456. unsigned long flags;
  2457. table = get_irq_table(iommu, devid);
  2458. if (!table)
  2459. return;
  2460. raw_spin_lock_irqsave(&table->lock, flags);
  2461. iommu->irte_ops->clear_allocated(table, index);
  2462. raw_spin_unlock_irqrestore(&table->lock, flags);
  2463. iommu_flush_irt(iommu, devid);
  2464. iommu_completion_wait(iommu);
  2465. }
  2466. static void irte_prepare(void *entry,
  2467. u32 delivery_mode, bool dest_mode,
  2468. u8 vector, u32 dest_apicid, int devid)
  2469. {
  2470. union irte *irte = (union irte *) entry;
  2471. irte->val = 0;
  2472. irte->fields.vector = vector;
  2473. irte->fields.int_type = delivery_mode;
  2474. irte->fields.destination = dest_apicid;
  2475. irte->fields.dm = dest_mode;
  2476. irte->fields.valid = 1;
  2477. }
  2478. static void irte_ga_prepare(void *entry,
  2479. u32 delivery_mode, bool dest_mode,
  2480. u8 vector, u32 dest_apicid, int devid)
  2481. {
  2482. struct irte_ga *irte = (struct irte_ga *) entry;
  2483. irte->lo.val = 0;
  2484. irte->hi.val = 0;
  2485. irte->lo.fields_remap.int_type = delivery_mode;
  2486. irte->lo.fields_remap.dm = dest_mode;
  2487. irte->hi.fields.vector = vector;
  2488. irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
  2489. irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
  2490. irte->lo.fields_remap.valid = 1;
  2491. }
  2492. static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2493. {
  2494. union irte *irte = (union irte *) entry;
  2495. irte->fields.valid = 1;
  2496. modify_irte(iommu, devid, index, irte);
  2497. }
  2498. static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2499. {
  2500. struct irte_ga *irte = (struct irte_ga *) entry;
  2501. irte->lo.fields_remap.valid = 1;
  2502. modify_irte_ga(iommu, devid, index, irte, NULL);
  2503. }
  2504. static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2505. {
  2506. union irte *irte = (union irte *) entry;
  2507. irte->fields.valid = 0;
  2508. modify_irte(iommu, devid, index, irte);
  2509. }
  2510. static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2511. {
  2512. struct irte_ga *irte = (struct irte_ga *) entry;
  2513. irte->lo.fields_remap.valid = 0;
  2514. modify_irte_ga(iommu, devid, index, irte, NULL);
  2515. }
  2516. static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
  2517. u8 vector, u32 dest_apicid)
  2518. {
  2519. union irte *irte = (union irte *) entry;
  2520. irte->fields.vector = vector;
  2521. irte->fields.destination = dest_apicid;
  2522. modify_irte(iommu, devid, index, irte);
  2523. }
  2524. static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
  2525. u8 vector, u32 dest_apicid)
  2526. {
  2527. struct irte_ga *irte = (struct irte_ga *) entry;
  2528. if (!irte->lo.fields_remap.guest_mode) {
  2529. irte->hi.fields.vector = vector;
  2530. irte->lo.fields_remap.destination =
  2531. APICID_TO_IRTE_DEST_LO(dest_apicid);
  2532. irte->hi.fields.destination =
  2533. APICID_TO_IRTE_DEST_HI(dest_apicid);
  2534. modify_irte_ga(iommu, devid, index, irte, NULL);
  2535. }
  2536. }
  2537. #define IRTE_ALLOCATED (~1U)
  2538. static void irte_set_allocated(struct irq_remap_table *table, int index)
  2539. {
  2540. table->table[index] = IRTE_ALLOCATED;
  2541. }
  2542. static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
  2543. {
  2544. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2545. struct irte_ga *irte = &ptr[index];
  2546. memset(&irte->lo.val, 0, sizeof(u64));
  2547. memset(&irte->hi.val, 0, sizeof(u64));
  2548. irte->hi.fields.vector = 0xff;
  2549. }
  2550. static bool irte_is_allocated(struct irq_remap_table *table, int index)
  2551. {
  2552. union irte *ptr = (union irte *)table->table;
  2553. union irte *irte = &ptr[index];
  2554. return irte->val != 0;
  2555. }
  2556. static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
  2557. {
  2558. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2559. struct irte_ga *irte = &ptr[index];
  2560. return irte->hi.fields.vector != 0;
  2561. }
  2562. static void irte_clear_allocated(struct irq_remap_table *table, int index)
  2563. {
  2564. table->table[index] = 0;
  2565. }
  2566. static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
  2567. {
  2568. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2569. struct irte_ga *irte = &ptr[index];
  2570. memset(&irte->lo.val, 0, sizeof(u64));
  2571. memset(&irte->hi.val, 0, sizeof(u64));
  2572. }
  2573. static int get_devid(struct irq_alloc_info *info)
  2574. {
  2575. switch (info->type) {
  2576. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  2577. return get_ioapic_devid(info->devid);
  2578. case X86_IRQ_ALLOC_TYPE_HPET:
  2579. return get_hpet_devid(info->devid);
  2580. case X86_IRQ_ALLOC_TYPE_PCI_MSI:
  2581. case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
  2582. return get_device_sbdf_id(msi_desc_to_dev(info->desc));
  2583. default:
  2584. WARN_ON_ONCE(1);
  2585. return -1;
  2586. }
  2587. }
  2588. struct irq_remap_ops amd_iommu_irq_ops = {
  2589. .prepare = amd_iommu_prepare,
  2590. .enable = amd_iommu_enable,
  2591. .disable = amd_iommu_disable,
  2592. .reenable = amd_iommu_reenable,
  2593. .enable_faulting = amd_iommu_enable_faulting,
  2594. };
  2595. static void fill_msi_msg(struct msi_msg *msg, u32 index)
  2596. {
  2597. msg->data = index;
  2598. msg->address_lo = 0;
  2599. msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
  2600. msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
  2601. }
  2602. static void irq_remapping_prepare_irte(struct amd_ir_data *data,
  2603. struct irq_cfg *irq_cfg,
  2604. struct irq_alloc_info *info,
  2605. int devid, int index, int sub_handle)
  2606. {
  2607. struct irq_2_irte *irte_info = &data->irq_2_irte;
  2608. struct amd_iommu *iommu = data->iommu;
  2609. if (!iommu)
  2610. return;
  2611. data->irq_2_irte.devid = devid;
  2612. data->irq_2_irte.index = index + sub_handle;
  2613. iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
  2614. apic->dest_mode_logical, irq_cfg->vector,
  2615. irq_cfg->dest_apicid, devid);
  2616. switch (info->type) {
  2617. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  2618. case X86_IRQ_ALLOC_TYPE_HPET:
  2619. case X86_IRQ_ALLOC_TYPE_PCI_MSI:
  2620. case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
  2621. fill_msi_msg(&data->msi_entry, irte_info->index);
  2622. break;
  2623. default:
  2624. BUG_ON(1);
  2625. break;
  2626. }
  2627. }
  2628. struct amd_irte_ops irte_32_ops = {
  2629. .prepare = irte_prepare,
  2630. .activate = irte_activate,
  2631. .deactivate = irte_deactivate,
  2632. .set_affinity = irte_set_affinity,
  2633. .set_allocated = irte_set_allocated,
  2634. .is_allocated = irte_is_allocated,
  2635. .clear_allocated = irte_clear_allocated,
  2636. };
  2637. struct amd_irte_ops irte_128_ops = {
  2638. .prepare = irte_ga_prepare,
  2639. .activate = irte_ga_activate,
  2640. .deactivate = irte_ga_deactivate,
  2641. .set_affinity = irte_ga_set_affinity,
  2642. .set_allocated = irte_ga_set_allocated,
  2643. .is_allocated = irte_ga_is_allocated,
  2644. .clear_allocated = irte_ga_clear_allocated,
  2645. };
  2646. static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
  2647. unsigned int nr_irqs, void *arg)
  2648. {
  2649. struct irq_alloc_info *info = arg;
  2650. struct irq_data *irq_data;
  2651. struct amd_ir_data *data = NULL;
  2652. struct amd_iommu *iommu;
  2653. struct irq_cfg *cfg;
  2654. int i, ret, devid, seg, sbdf;
  2655. int index;
  2656. if (!info)
  2657. return -EINVAL;
  2658. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
  2659. info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
  2660. return -EINVAL;
  2661. /*
  2662. * With IRQ remapping enabled, don't need contiguous CPU vectors
  2663. * to support multiple MSI interrupts.
  2664. */
  2665. if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
  2666. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  2667. sbdf = get_devid(info);
  2668. if (sbdf < 0)
  2669. return -EINVAL;
  2670. seg = PCI_SBDF_TO_SEGID(sbdf);
  2671. devid = PCI_SBDF_TO_DEVID(sbdf);
  2672. iommu = __rlookup_amd_iommu(seg, devid);
  2673. if (!iommu)
  2674. return -EINVAL;
  2675. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  2676. if (ret < 0)
  2677. return ret;
  2678. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
  2679. struct irq_remap_table *table;
  2680. table = alloc_irq_table(iommu, devid, NULL);
  2681. if (table) {
  2682. if (!table->min_index) {
  2683. /*
  2684. * Keep the first 32 indexes free for IOAPIC
  2685. * interrupts.
  2686. */
  2687. table->min_index = 32;
  2688. for (i = 0; i < 32; ++i)
  2689. iommu->irte_ops->set_allocated(table, i);
  2690. }
  2691. WARN_ON(table->min_index != 32);
  2692. index = info->ioapic.pin;
  2693. } else {
  2694. index = -ENOMEM;
  2695. }
  2696. } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
  2697. info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
  2698. bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
  2699. index = alloc_irq_index(iommu, devid, nr_irqs, align,
  2700. msi_desc_to_pci_dev(info->desc));
  2701. } else {
  2702. index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL);
  2703. }
  2704. if (index < 0) {
  2705. pr_warn("Failed to allocate IRTE\n");
  2706. ret = index;
  2707. goto out_free_parent;
  2708. }
  2709. for (i = 0; i < nr_irqs; i++) {
  2710. irq_data = irq_domain_get_irq_data(domain, virq + i);
  2711. cfg = irq_data ? irqd_cfg(irq_data) : NULL;
  2712. if (!cfg) {
  2713. ret = -EINVAL;
  2714. goto out_free_data;
  2715. }
  2716. ret = -ENOMEM;
  2717. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2718. if (!data)
  2719. goto out_free_data;
  2720. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2721. data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
  2722. else
  2723. data->entry = kzalloc(sizeof(struct irte_ga),
  2724. GFP_KERNEL);
  2725. if (!data->entry) {
  2726. kfree(data);
  2727. goto out_free_data;
  2728. }
  2729. data->iommu = iommu;
  2730. irq_data->hwirq = (devid << 16) + i;
  2731. irq_data->chip_data = data;
  2732. irq_data->chip = &amd_ir_chip;
  2733. irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
  2734. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  2735. }
  2736. return 0;
  2737. out_free_data:
  2738. for (i--; i >= 0; i--) {
  2739. irq_data = irq_domain_get_irq_data(domain, virq + i);
  2740. if (irq_data)
  2741. kfree(irq_data->chip_data);
  2742. }
  2743. for (i = 0; i < nr_irqs; i++)
  2744. free_irte(iommu, devid, index + i);
  2745. out_free_parent:
  2746. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  2747. return ret;
  2748. }
  2749. static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
  2750. unsigned int nr_irqs)
  2751. {
  2752. struct irq_2_irte *irte_info;
  2753. struct irq_data *irq_data;
  2754. struct amd_ir_data *data;
  2755. int i;
  2756. for (i = 0; i < nr_irqs; i++) {
  2757. irq_data = irq_domain_get_irq_data(domain, virq + i);
  2758. if (irq_data && irq_data->chip_data) {
  2759. data = irq_data->chip_data;
  2760. irte_info = &data->irq_2_irte;
  2761. free_irte(data->iommu, irte_info->devid, irte_info->index);
  2762. kfree(data->entry);
  2763. kfree(data);
  2764. }
  2765. }
  2766. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  2767. }
  2768. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  2769. struct amd_ir_data *ir_data,
  2770. struct irq_2_irte *irte_info,
  2771. struct irq_cfg *cfg);
  2772. static int irq_remapping_activate(struct irq_domain *domain,
  2773. struct irq_data *irq_data, bool reserve)
  2774. {
  2775. struct amd_ir_data *data = irq_data->chip_data;
  2776. struct irq_2_irte *irte_info = &data->irq_2_irte;
  2777. struct amd_iommu *iommu = data->iommu;
  2778. struct irq_cfg *cfg = irqd_cfg(irq_data);
  2779. if (!iommu)
  2780. return 0;
  2781. iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
  2782. irte_info->index);
  2783. amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
  2784. return 0;
  2785. }
  2786. static void irq_remapping_deactivate(struct irq_domain *domain,
  2787. struct irq_data *irq_data)
  2788. {
  2789. struct amd_ir_data *data = irq_data->chip_data;
  2790. struct irq_2_irte *irte_info = &data->irq_2_irte;
  2791. struct amd_iommu *iommu = data->iommu;
  2792. if (iommu)
  2793. iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
  2794. irte_info->index);
  2795. }
  2796. static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
  2797. enum irq_domain_bus_token bus_token)
  2798. {
  2799. struct amd_iommu *iommu;
  2800. int devid = -1;
  2801. if (!amd_iommu_irq_remap)
  2802. return 0;
  2803. if (x86_fwspec_is_ioapic(fwspec))
  2804. devid = get_ioapic_devid(fwspec->param[0]);
  2805. else if (x86_fwspec_is_hpet(fwspec))
  2806. devid = get_hpet_devid(fwspec->param[0]);
  2807. if (devid < 0)
  2808. return 0;
  2809. iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
  2810. return iommu && iommu->ir_domain == d;
  2811. }
  2812. static const struct irq_domain_ops amd_ir_domain_ops = {
  2813. .select = irq_remapping_select,
  2814. .alloc = irq_remapping_alloc,
  2815. .free = irq_remapping_free,
  2816. .activate = irq_remapping_activate,
  2817. .deactivate = irq_remapping_deactivate,
  2818. };
  2819. int amd_iommu_activate_guest_mode(void *data)
  2820. {
  2821. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  2822. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  2823. u64 valid;
  2824. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || !entry)
  2825. return 0;
  2826. valid = entry->lo.fields_vapic.valid;
  2827. entry->lo.val = 0;
  2828. entry->hi.val = 0;
  2829. entry->lo.fields_vapic.valid = valid;
  2830. entry->lo.fields_vapic.guest_mode = 1;
  2831. entry->lo.fields_vapic.ga_log_intr = 1;
  2832. entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
  2833. entry->hi.fields.vector = ir_data->ga_vector;
  2834. entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
  2835. return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
  2836. ir_data->irq_2_irte.index, entry, ir_data);
  2837. }
  2838. EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
  2839. int amd_iommu_deactivate_guest_mode(void *data)
  2840. {
  2841. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  2842. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  2843. struct irq_cfg *cfg = ir_data->cfg;
  2844. u64 valid;
  2845. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
  2846. !entry || !entry->lo.fields_vapic.guest_mode)
  2847. return 0;
  2848. valid = entry->lo.fields_remap.valid;
  2849. entry->lo.val = 0;
  2850. entry->hi.val = 0;
  2851. entry->lo.fields_remap.valid = valid;
  2852. entry->lo.fields_remap.dm = apic->dest_mode_logical;
  2853. entry->lo.fields_remap.int_type = apic->delivery_mode;
  2854. entry->hi.fields.vector = cfg->vector;
  2855. entry->lo.fields_remap.destination =
  2856. APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
  2857. entry->hi.fields.destination =
  2858. APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
  2859. return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
  2860. ir_data->irq_2_irte.index, entry, ir_data);
  2861. }
  2862. EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
  2863. static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
  2864. {
  2865. int ret;
  2866. struct amd_iommu_pi_data *pi_data = vcpu_info;
  2867. struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
  2868. struct amd_ir_data *ir_data = data->chip_data;
  2869. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  2870. struct iommu_dev_data *dev_data;
  2871. if (ir_data->iommu == NULL)
  2872. return -EINVAL;
  2873. dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
  2874. /* Note:
  2875. * This device has never been set up for guest mode.
  2876. * we should not modify the IRTE
  2877. */
  2878. if (!dev_data || !dev_data->use_vapic)
  2879. return 0;
  2880. ir_data->cfg = irqd_cfg(data);
  2881. pi_data->ir_data = ir_data;
  2882. /* Note:
  2883. * SVM tries to set up for VAPIC mode, but we are in
  2884. * legacy mode. So, we force legacy mode instead.
  2885. */
  2886. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  2887. pr_debug("%s: Fall back to using intr legacy remap\n",
  2888. __func__);
  2889. pi_data->is_guest_mode = false;
  2890. }
  2891. pi_data->prev_ga_tag = ir_data->cached_ga_tag;
  2892. if (pi_data->is_guest_mode) {
  2893. ir_data->ga_root_ptr = (pi_data->base >> 12);
  2894. ir_data->ga_vector = vcpu_pi_info->vector;
  2895. ir_data->ga_tag = pi_data->ga_tag;
  2896. ret = amd_iommu_activate_guest_mode(ir_data);
  2897. if (!ret)
  2898. ir_data->cached_ga_tag = pi_data->ga_tag;
  2899. } else {
  2900. ret = amd_iommu_deactivate_guest_mode(ir_data);
  2901. /*
  2902. * This communicates the ga_tag back to the caller
  2903. * so that it can do all the necessary clean up.
  2904. */
  2905. if (!ret)
  2906. ir_data->cached_ga_tag = 0;
  2907. }
  2908. return ret;
  2909. }
  2910. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  2911. struct amd_ir_data *ir_data,
  2912. struct irq_2_irte *irte_info,
  2913. struct irq_cfg *cfg)
  2914. {
  2915. /*
  2916. * Atomically updates the IRTE with the new destination, vector
  2917. * and flushes the interrupt entry cache.
  2918. */
  2919. iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
  2920. irte_info->index, cfg->vector,
  2921. cfg->dest_apicid);
  2922. }
  2923. static int amd_ir_set_affinity(struct irq_data *data,
  2924. const struct cpumask *mask, bool force)
  2925. {
  2926. struct amd_ir_data *ir_data = data->chip_data;
  2927. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  2928. struct irq_cfg *cfg = irqd_cfg(data);
  2929. struct irq_data *parent = data->parent_data;
  2930. struct amd_iommu *iommu = ir_data->iommu;
  2931. int ret;
  2932. if (!iommu)
  2933. return -ENODEV;
  2934. ret = parent->chip->irq_set_affinity(parent, mask, force);
  2935. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  2936. return ret;
  2937. amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
  2938. /*
  2939. * After this point, all the interrupts will start arriving
  2940. * at the new destination. So, time to cleanup the previous
  2941. * vector allocation.
  2942. */
  2943. send_cleanup_vector(cfg);
  2944. return IRQ_SET_MASK_OK_DONE;
  2945. }
  2946. static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  2947. {
  2948. struct amd_ir_data *ir_data = irq_data->chip_data;
  2949. *msg = ir_data->msi_entry;
  2950. }
  2951. static struct irq_chip amd_ir_chip = {
  2952. .name = "AMD-IR",
  2953. .irq_ack = apic_ack_irq,
  2954. .irq_set_affinity = amd_ir_set_affinity,
  2955. .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
  2956. .irq_compose_msi_msg = ir_compose_msi_msg,
  2957. };
  2958. int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
  2959. {
  2960. struct fwnode_handle *fn;
  2961. fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
  2962. if (!fn)
  2963. return -ENOMEM;
  2964. iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
  2965. if (!iommu->ir_domain) {
  2966. irq_domain_free_fwnode(fn);
  2967. return -ENOMEM;
  2968. }
  2969. iommu->ir_domain->parent = arch_get_ir_parent_domain();
  2970. iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
  2971. "AMD-IR-MSI",
  2972. iommu->index);
  2973. return 0;
  2974. }
  2975. int amd_iommu_update_ga(int cpu, bool is_run, void *data)
  2976. {
  2977. unsigned long flags;
  2978. struct amd_iommu *iommu;
  2979. struct irq_remap_table *table;
  2980. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  2981. int devid = ir_data->irq_2_irte.devid;
  2982. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  2983. struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
  2984. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
  2985. !ref || !entry || !entry->lo.fields_vapic.guest_mode)
  2986. return 0;
  2987. iommu = ir_data->iommu;
  2988. if (!iommu)
  2989. return -ENODEV;
  2990. table = get_irq_table(iommu, devid);
  2991. if (!table)
  2992. return -ENODEV;
  2993. raw_spin_lock_irqsave(&table->lock, flags);
  2994. if (ref->lo.fields_vapic.guest_mode) {
  2995. if (cpu >= 0) {
  2996. ref->lo.fields_vapic.destination =
  2997. APICID_TO_IRTE_DEST_LO(cpu);
  2998. ref->hi.fields.destination =
  2999. APICID_TO_IRTE_DEST_HI(cpu);
  3000. }
  3001. ref->lo.fields_vapic.is_run = is_run;
  3002. barrier();
  3003. }
  3004. raw_spin_unlock_irqrestore(&table->lock, flags);
  3005. iommu_flush_irt(iommu, devid);
  3006. iommu_completion_wait(iommu);
  3007. return 0;
  3008. }
  3009. EXPORT_SYMBOL(amd_iommu_update_ga);
  3010. #endif