amd_iommu.c 100 KB

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