amd_iommu.c 94 KB

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