amd_iommu.c 97 KB

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