amd_iommu.c 96 KB

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