amd_iommu.c 97 KB

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