amd_iommu.c 96 KB

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