amd_iommu.c 85 KB

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