amd_iommu.c 98 KB

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