amd_iommu.c 83 KB

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