amd_iommu.c 93 KB

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