amd_iommu.c 94 KB

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