amd_iommu.c 85 KB

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