iommu.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <jroedel@suse.de>
  5. */
  6. #define pr_fmt(fmt) "iommu: " fmt
  7. #include <linux/amba/bus.h>
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/bits.h>
  11. #include <linux/bug.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/export.h>
  15. #include <linux/slab.h>
  16. #include <linux/errno.h>
  17. #include <linux/host1x_context_bus.h>
  18. #include <linux/iommu.h>
  19. #include <linux/idr.h>
  20. #include <linux/err.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci-ats.h>
  23. #include <linux/bitops.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/property.h>
  26. #include <linux/fsl/mc.h>
  27. #include <linux/module.h>
  28. #include <linux/cc_platform.h>
  29. #include <linux/cdx/cdx_bus.h>
  30. #include <trace/events/iommu.h>
  31. #include <linux/sched/mm.h>
  32. #include <linux/msi.h>
  33. #include "dma-iommu.h"
  34. #include "iommu-priv.h"
  35. static struct kset *iommu_group_kset;
  36. static DEFINE_IDA(iommu_group_ida);
  37. static DEFINE_IDA(iommu_global_pasid_ida);
  38. static unsigned int iommu_def_domain_type __read_mostly;
  39. static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
  40. static u32 iommu_cmd_line __read_mostly;
  41. struct iommu_group {
  42. struct kobject kobj;
  43. struct kobject *devices_kobj;
  44. struct list_head devices;
  45. struct xarray pasid_array;
  46. struct mutex mutex;
  47. void *iommu_data;
  48. void (*iommu_data_release)(void *iommu_data);
  49. char *name;
  50. int id;
  51. struct iommu_domain *default_domain;
  52. struct iommu_domain *blocking_domain;
  53. struct iommu_domain *domain;
  54. struct list_head entry;
  55. unsigned int owner_cnt;
  56. void *owner;
  57. };
  58. struct group_device {
  59. struct list_head list;
  60. struct device *dev;
  61. char *name;
  62. };
  63. /* Iterate over each struct group_device in a struct iommu_group */
  64. #define for_each_group_device(group, pos) \
  65. list_for_each_entry(pos, &(group)->devices, list)
  66. struct iommu_group_attribute {
  67. struct attribute attr;
  68. ssize_t (*show)(struct iommu_group *group, char *buf);
  69. ssize_t (*store)(struct iommu_group *group,
  70. const char *buf, size_t count);
  71. };
  72. static const char * const iommu_group_resv_type_string[] = {
  73. [IOMMU_RESV_DIRECT] = "direct",
  74. [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
  75. [IOMMU_RESV_RESERVED] = "reserved",
  76. [IOMMU_RESV_MSI] = "msi",
  77. [IOMMU_RESV_SW_MSI] = "msi",
  78. };
  79. #define IOMMU_CMD_LINE_DMA_API BIT(0)
  80. #define IOMMU_CMD_LINE_STRICT BIT(1)
  81. static int iommu_bus_notifier(struct notifier_block *nb,
  82. unsigned long action, void *data);
  83. static void iommu_release_device(struct device *dev);
  84. static struct iommu_domain *
  85. __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type);
  86. static int __iommu_attach_device(struct iommu_domain *domain,
  87. struct device *dev);
  88. static int __iommu_attach_group(struct iommu_domain *domain,
  89. struct iommu_group *group);
  90. enum {
  91. IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
  92. };
  93. static int __iommu_device_set_domain(struct iommu_group *group,
  94. struct device *dev,
  95. struct iommu_domain *new_domain,
  96. unsigned int flags);
  97. static int __iommu_group_set_domain_internal(struct iommu_group *group,
  98. struct iommu_domain *new_domain,
  99. unsigned int flags);
  100. static int __iommu_group_set_domain(struct iommu_group *group,
  101. struct iommu_domain *new_domain)
  102. {
  103. return __iommu_group_set_domain_internal(group, new_domain, 0);
  104. }
  105. static void __iommu_group_set_domain_nofail(struct iommu_group *group,
  106. struct iommu_domain *new_domain)
  107. {
  108. WARN_ON(__iommu_group_set_domain_internal(
  109. group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
  110. }
  111. static int iommu_setup_default_domain(struct iommu_group *group,
  112. int target_type);
  113. static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
  114. struct device *dev);
  115. static ssize_t iommu_group_store_type(struct iommu_group *group,
  116. const char *buf, size_t count);
  117. static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
  118. struct device *dev);
  119. static void __iommu_group_free_device(struct iommu_group *group,
  120. struct group_device *grp_dev);
  121. #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
  122. struct iommu_group_attribute iommu_group_attr_##_name = \
  123. __ATTR(_name, _mode, _show, _store)
  124. #define to_iommu_group_attr(_attr) \
  125. container_of(_attr, struct iommu_group_attribute, attr)
  126. #define to_iommu_group(_kobj) \
  127. container_of(_kobj, struct iommu_group, kobj)
  128. static LIST_HEAD(iommu_device_list);
  129. static DEFINE_SPINLOCK(iommu_device_lock);
  130. static const struct bus_type * const iommu_buses[] = {
  131. &platform_bus_type,
  132. #ifdef CONFIG_PCI
  133. &pci_bus_type,
  134. #endif
  135. #ifdef CONFIG_ARM_AMBA
  136. &amba_bustype,
  137. #endif
  138. #ifdef CONFIG_FSL_MC_BUS
  139. &fsl_mc_bus_type,
  140. #endif
  141. #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
  142. &host1x_context_device_bus_type,
  143. #endif
  144. #ifdef CONFIG_CDX_BUS
  145. &cdx_bus_type,
  146. #endif
  147. };
  148. /*
  149. * Use a function instead of an array here because the domain-type is a
  150. * bit-field, so an array would waste memory.
  151. */
  152. static const char *iommu_domain_type_str(unsigned int t)
  153. {
  154. switch (t) {
  155. case IOMMU_DOMAIN_BLOCKED:
  156. return "Blocked";
  157. case IOMMU_DOMAIN_IDENTITY:
  158. return "Passthrough";
  159. case IOMMU_DOMAIN_UNMANAGED:
  160. return "Unmanaged";
  161. case IOMMU_DOMAIN_DMA:
  162. case IOMMU_DOMAIN_DMA_FQ:
  163. return "Translated";
  164. case IOMMU_DOMAIN_PLATFORM:
  165. return "Platform";
  166. default:
  167. return "Unknown";
  168. }
  169. }
  170. static int __init iommu_subsys_init(void)
  171. {
  172. struct notifier_block *nb;
  173. if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
  174. if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
  175. iommu_set_default_passthrough(false);
  176. else
  177. iommu_set_default_translated(false);
  178. if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  179. pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
  180. iommu_set_default_translated(false);
  181. }
  182. }
  183. if (!iommu_default_passthrough() && !iommu_dma_strict)
  184. iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
  185. pr_info("Default domain type: %s%s\n",
  186. iommu_domain_type_str(iommu_def_domain_type),
  187. (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
  188. " (set via kernel command line)" : "");
  189. if (!iommu_default_passthrough())
  190. pr_info("DMA domain TLB invalidation policy: %s mode%s\n",
  191. iommu_dma_strict ? "strict" : "lazy",
  192. (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
  193. " (set via kernel command line)" : "");
  194. nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL);
  195. if (!nb)
  196. return -ENOMEM;
  197. for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
  198. nb[i].notifier_call = iommu_bus_notifier;
  199. bus_register_notifier(iommu_buses[i], &nb[i]);
  200. }
  201. return 0;
  202. }
  203. subsys_initcall(iommu_subsys_init);
  204. static int remove_iommu_group(struct device *dev, void *data)
  205. {
  206. if (dev->iommu && dev->iommu->iommu_dev == data)
  207. iommu_release_device(dev);
  208. return 0;
  209. }
  210. /**
  211. * iommu_device_register() - Register an IOMMU hardware instance
  212. * @iommu: IOMMU handle for the instance
  213. * @ops: IOMMU ops to associate with the instance
  214. * @hwdev: (optional) actual instance device, used for fwnode lookup
  215. *
  216. * Return: 0 on success, or an error.
  217. */
  218. int iommu_device_register(struct iommu_device *iommu,
  219. const struct iommu_ops *ops, struct device *hwdev)
  220. {
  221. int err = 0;
  222. /* We need to be able to take module references appropriately */
  223. if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
  224. return -EINVAL;
  225. iommu->ops = ops;
  226. if (hwdev)
  227. iommu->fwnode = dev_fwnode(hwdev);
  228. spin_lock(&iommu_device_lock);
  229. list_add_tail(&iommu->list, &iommu_device_list);
  230. spin_unlock(&iommu_device_lock);
  231. for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++)
  232. err = bus_iommu_probe(iommu_buses[i]);
  233. if (err)
  234. iommu_device_unregister(iommu);
  235. return err;
  236. }
  237. EXPORT_SYMBOL_GPL(iommu_device_register);
  238. void iommu_device_unregister(struct iommu_device *iommu)
  239. {
  240. for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
  241. bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group);
  242. spin_lock(&iommu_device_lock);
  243. list_del(&iommu->list);
  244. spin_unlock(&iommu_device_lock);
  245. /* Pairs with the alloc in generic_single_device_group() */
  246. iommu_group_put(iommu->singleton_group);
  247. iommu->singleton_group = NULL;
  248. }
  249. EXPORT_SYMBOL_GPL(iommu_device_unregister);
  250. #if IS_ENABLED(CONFIG_IOMMUFD_TEST)
  251. void iommu_device_unregister_bus(struct iommu_device *iommu,
  252. const struct bus_type *bus,
  253. struct notifier_block *nb)
  254. {
  255. bus_unregister_notifier(bus, nb);
  256. iommu_device_unregister(iommu);
  257. }
  258. EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
  259. /*
  260. * Register an iommu driver against a single bus. This is only used by iommufd
  261. * selftest to create a mock iommu driver. The caller must provide
  262. * some memory to hold a notifier_block.
  263. */
  264. int iommu_device_register_bus(struct iommu_device *iommu,
  265. const struct iommu_ops *ops,
  266. const struct bus_type *bus,
  267. struct notifier_block *nb)
  268. {
  269. int err;
  270. iommu->ops = ops;
  271. nb->notifier_call = iommu_bus_notifier;
  272. err = bus_register_notifier(bus, nb);
  273. if (err)
  274. return err;
  275. spin_lock(&iommu_device_lock);
  276. list_add_tail(&iommu->list, &iommu_device_list);
  277. spin_unlock(&iommu_device_lock);
  278. err = bus_iommu_probe(bus);
  279. if (err) {
  280. iommu_device_unregister_bus(iommu, bus, nb);
  281. return err;
  282. }
  283. return 0;
  284. }
  285. EXPORT_SYMBOL_GPL(iommu_device_register_bus);
  286. #endif
  287. static struct dev_iommu *dev_iommu_get(struct device *dev)
  288. {
  289. struct dev_iommu *param = dev->iommu;
  290. lockdep_assert_held(&iommu_probe_device_lock);
  291. if (param)
  292. return param;
  293. param = kzalloc(sizeof(*param), GFP_KERNEL);
  294. if (!param)
  295. return NULL;
  296. mutex_init(&param->lock);
  297. dev->iommu = param;
  298. return param;
  299. }
  300. void dev_iommu_free(struct device *dev)
  301. {
  302. struct dev_iommu *param = dev->iommu;
  303. dev->iommu = NULL;
  304. if (param->fwspec) {
  305. fwnode_handle_put(param->fwspec->iommu_fwnode);
  306. kfree(param->fwspec);
  307. }
  308. kfree(param);
  309. }
  310. /*
  311. * Internal equivalent of device_iommu_mapped() for when we care that a device
  312. * actually has API ops, and don't want false positives from VFIO-only groups.
  313. */
  314. static bool dev_has_iommu(struct device *dev)
  315. {
  316. return dev->iommu && dev->iommu->iommu_dev;
  317. }
  318. static u32 dev_iommu_get_max_pasids(struct device *dev)
  319. {
  320. u32 max_pasids = 0, bits = 0;
  321. int ret;
  322. if (dev_is_pci(dev)) {
  323. ret = pci_max_pasids(to_pci_dev(dev));
  324. if (ret > 0)
  325. max_pasids = ret;
  326. } else {
  327. ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
  328. if (!ret)
  329. max_pasids = 1UL << bits;
  330. }
  331. return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
  332. }
  333. void dev_iommu_priv_set(struct device *dev, void *priv)
  334. {
  335. /* FSL_PAMU does something weird */
  336. if (!IS_ENABLED(CONFIG_FSL_PAMU))
  337. lockdep_assert_held(&iommu_probe_device_lock);
  338. dev->iommu->priv = priv;
  339. }
  340. EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
  341. /*
  342. * Init the dev->iommu and dev->iommu_group in the struct device and get the
  343. * driver probed
  344. */
  345. static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
  346. {
  347. struct iommu_device *iommu_dev;
  348. struct iommu_group *group;
  349. int ret;
  350. if (!dev_iommu_get(dev))
  351. return -ENOMEM;
  352. if (!try_module_get(ops->owner)) {
  353. ret = -EINVAL;
  354. goto err_free;
  355. }
  356. iommu_dev = ops->probe_device(dev);
  357. if (IS_ERR(iommu_dev)) {
  358. ret = PTR_ERR(iommu_dev);
  359. goto err_module_put;
  360. }
  361. dev->iommu->iommu_dev = iommu_dev;
  362. ret = iommu_device_link(iommu_dev, dev);
  363. if (ret)
  364. goto err_release;
  365. group = ops->device_group(dev);
  366. if (WARN_ON_ONCE(group == NULL))
  367. group = ERR_PTR(-EINVAL);
  368. if (IS_ERR(group)) {
  369. ret = PTR_ERR(group);
  370. goto err_unlink;
  371. }
  372. dev->iommu_group = group;
  373. dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
  374. if (ops->is_attach_deferred)
  375. dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
  376. return 0;
  377. err_unlink:
  378. iommu_device_unlink(iommu_dev, dev);
  379. err_release:
  380. if (ops->release_device)
  381. ops->release_device(dev);
  382. err_module_put:
  383. module_put(ops->owner);
  384. err_free:
  385. dev->iommu->iommu_dev = NULL;
  386. dev_iommu_free(dev);
  387. return ret;
  388. }
  389. static void iommu_deinit_device(struct device *dev)
  390. {
  391. struct iommu_group *group = dev->iommu_group;
  392. const struct iommu_ops *ops = dev_iommu_ops(dev);
  393. lockdep_assert_held(&group->mutex);
  394. iommu_device_unlink(dev->iommu->iommu_dev, dev);
  395. /*
  396. * release_device() must stop using any attached domain on the device.
  397. * If there are still other devices in the group, they are not affected
  398. * by this callback.
  399. *
  400. * If the iommu driver provides release_domain, the core code ensures
  401. * that domain is attached prior to calling release_device. Drivers can
  402. * use this to enforce a translation on the idle iommu. Typically, the
  403. * global static blocked_domain is a good choice.
  404. *
  405. * Otherwise, the iommu driver must set the device to either an identity
  406. * or a blocking translation in release_device() and stop using any
  407. * domain pointer, as it is going to be freed.
  408. *
  409. * Regardless, if a delayed attach never occurred, then the release
  410. * should still avoid touching any hardware configuration either.
  411. */
  412. if (!dev->iommu->attach_deferred && ops->release_domain)
  413. ops->release_domain->ops->attach_dev(ops->release_domain, dev);
  414. if (ops->release_device)
  415. ops->release_device(dev);
  416. /*
  417. * If this is the last driver to use the group then we must free the
  418. * domains before we do the module_put().
  419. */
  420. if (list_empty(&group->devices)) {
  421. if (group->default_domain) {
  422. iommu_domain_free(group->default_domain);
  423. group->default_domain = NULL;
  424. }
  425. if (group->blocking_domain) {
  426. iommu_domain_free(group->blocking_domain);
  427. group->blocking_domain = NULL;
  428. }
  429. group->domain = NULL;
  430. }
  431. /* Caller must put iommu_group */
  432. dev->iommu_group = NULL;
  433. module_put(ops->owner);
  434. dev_iommu_free(dev);
  435. #ifdef CONFIG_IOMMU_DMA
  436. dev->dma_iommu = false;
  437. #endif
  438. }
  439. DEFINE_MUTEX(iommu_probe_device_lock);
  440. static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
  441. {
  442. const struct iommu_ops *ops;
  443. struct iommu_group *group;
  444. struct group_device *gdev;
  445. int ret;
  446. /*
  447. * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU
  448. * instances with non-NULL fwnodes, and client devices should have been
  449. * identified with a fwspec by this point. Otherwise, we can currently
  450. * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
  451. * be present, and that any of their registered instances has suitable
  452. * ops for probing, and thus cheekily co-opt the same mechanism.
  453. */
  454. ops = iommu_fwspec_ops(dev_iommu_fwspec_get(dev));
  455. if (!ops)
  456. return -ENODEV;
  457. /*
  458. * Serialise to avoid races between IOMMU drivers registering in
  459. * parallel and/or the "replay" calls from ACPI/OF code via client
  460. * driver probe. Once the latter have been cleaned up we should
  461. * probably be able to use device_lock() here to minimise the scope,
  462. * but for now enforcing a simple global ordering is fine.
  463. */
  464. lockdep_assert_held(&iommu_probe_device_lock);
  465. /* Device is probed already if in a group */
  466. if (dev->iommu_group)
  467. return 0;
  468. ret = iommu_init_device(dev, ops);
  469. if (ret)
  470. return ret;
  471. group = dev->iommu_group;
  472. gdev = iommu_group_alloc_device(group, dev);
  473. mutex_lock(&group->mutex);
  474. if (IS_ERR(gdev)) {
  475. ret = PTR_ERR(gdev);
  476. goto err_put_group;
  477. }
  478. /*
  479. * The gdev must be in the list before calling
  480. * iommu_setup_default_domain()
  481. */
  482. list_add_tail(&gdev->list, &group->devices);
  483. WARN_ON(group->default_domain && !group->domain);
  484. if (group->default_domain)
  485. iommu_create_device_direct_mappings(group->default_domain, dev);
  486. if (group->domain) {
  487. ret = __iommu_device_set_domain(group, dev, group->domain, 0);
  488. if (ret)
  489. goto err_remove_gdev;
  490. } else if (!group->default_domain && !group_list) {
  491. ret = iommu_setup_default_domain(group, 0);
  492. if (ret)
  493. goto err_remove_gdev;
  494. } else if (!group->default_domain) {
  495. /*
  496. * With a group_list argument we defer the default_domain setup
  497. * to the caller by providing a de-duplicated list of groups
  498. * that need further setup.
  499. */
  500. if (list_empty(&group->entry))
  501. list_add_tail(&group->entry, group_list);
  502. }
  503. if (group->default_domain)
  504. iommu_setup_dma_ops(dev);
  505. mutex_unlock(&group->mutex);
  506. return 0;
  507. err_remove_gdev:
  508. list_del(&gdev->list);
  509. __iommu_group_free_device(group, gdev);
  510. err_put_group:
  511. iommu_deinit_device(dev);
  512. mutex_unlock(&group->mutex);
  513. iommu_group_put(group);
  514. return ret;
  515. }
  516. int iommu_probe_device(struct device *dev)
  517. {
  518. const struct iommu_ops *ops;
  519. int ret;
  520. mutex_lock(&iommu_probe_device_lock);
  521. ret = __iommu_probe_device(dev, NULL);
  522. mutex_unlock(&iommu_probe_device_lock);
  523. if (ret)
  524. return ret;
  525. ops = dev_iommu_ops(dev);
  526. if (ops->probe_finalize)
  527. ops->probe_finalize(dev);
  528. return 0;
  529. }
  530. static void __iommu_group_free_device(struct iommu_group *group,
  531. struct group_device *grp_dev)
  532. {
  533. struct device *dev = grp_dev->dev;
  534. sysfs_remove_link(group->devices_kobj, grp_dev->name);
  535. sysfs_remove_link(&dev->kobj, "iommu_group");
  536. trace_remove_device_from_group(group->id, dev);
  537. /*
  538. * If the group has become empty then ownership must have been
  539. * released, and the current domain must be set back to NULL or
  540. * the default domain.
  541. */
  542. if (list_empty(&group->devices))
  543. WARN_ON(group->owner_cnt ||
  544. group->domain != group->default_domain);
  545. kfree(grp_dev->name);
  546. kfree(grp_dev);
  547. }
  548. /* Remove the iommu_group from the struct device. */
  549. static void __iommu_group_remove_device(struct device *dev)
  550. {
  551. struct iommu_group *group = dev->iommu_group;
  552. struct group_device *device;
  553. mutex_lock(&group->mutex);
  554. for_each_group_device(group, device) {
  555. if (device->dev != dev)
  556. continue;
  557. list_del(&device->list);
  558. __iommu_group_free_device(group, device);
  559. if (dev_has_iommu(dev))
  560. iommu_deinit_device(dev);
  561. else
  562. dev->iommu_group = NULL;
  563. break;
  564. }
  565. mutex_unlock(&group->mutex);
  566. /*
  567. * Pairs with the get in iommu_init_device() or
  568. * iommu_group_add_device()
  569. */
  570. iommu_group_put(group);
  571. }
  572. static void iommu_release_device(struct device *dev)
  573. {
  574. struct iommu_group *group = dev->iommu_group;
  575. if (group)
  576. __iommu_group_remove_device(dev);
  577. /* Free any fwspec if no iommu_driver was ever attached */
  578. if (dev->iommu)
  579. dev_iommu_free(dev);
  580. }
  581. static int __init iommu_set_def_domain_type(char *str)
  582. {
  583. bool pt;
  584. int ret;
  585. ret = kstrtobool(str, &pt);
  586. if (ret)
  587. return ret;
  588. if (pt)
  589. iommu_set_default_passthrough(true);
  590. else
  591. iommu_set_default_translated(true);
  592. return 0;
  593. }
  594. early_param("iommu.passthrough", iommu_set_def_domain_type);
  595. static int __init iommu_dma_setup(char *str)
  596. {
  597. int ret = kstrtobool(str, &iommu_dma_strict);
  598. if (!ret)
  599. iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
  600. return ret;
  601. }
  602. early_param("iommu.strict", iommu_dma_setup);
  603. void iommu_set_dma_strict(void)
  604. {
  605. iommu_dma_strict = true;
  606. if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
  607. iommu_def_domain_type = IOMMU_DOMAIN_DMA;
  608. }
  609. static ssize_t iommu_group_attr_show(struct kobject *kobj,
  610. struct attribute *__attr, char *buf)
  611. {
  612. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  613. struct iommu_group *group = to_iommu_group(kobj);
  614. ssize_t ret = -EIO;
  615. if (attr->show)
  616. ret = attr->show(group, buf);
  617. return ret;
  618. }
  619. static ssize_t iommu_group_attr_store(struct kobject *kobj,
  620. struct attribute *__attr,
  621. const char *buf, size_t count)
  622. {
  623. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  624. struct iommu_group *group = to_iommu_group(kobj);
  625. ssize_t ret = -EIO;
  626. if (attr->store)
  627. ret = attr->store(group, buf, count);
  628. return ret;
  629. }
  630. static const struct sysfs_ops iommu_group_sysfs_ops = {
  631. .show = iommu_group_attr_show,
  632. .store = iommu_group_attr_store,
  633. };
  634. static int iommu_group_create_file(struct iommu_group *group,
  635. struct iommu_group_attribute *attr)
  636. {
  637. return sysfs_create_file(&group->kobj, &attr->attr);
  638. }
  639. static void iommu_group_remove_file(struct iommu_group *group,
  640. struct iommu_group_attribute *attr)
  641. {
  642. sysfs_remove_file(&group->kobj, &attr->attr);
  643. }
  644. static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
  645. {
  646. return sysfs_emit(buf, "%s\n", group->name);
  647. }
  648. /**
  649. * iommu_insert_resv_region - Insert a new region in the
  650. * list of reserved regions.
  651. * @new: new region to insert
  652. * @regions: list of regions
  653. *
  654. * Elements are sorted by start address and overlapping segments
  655. * of the same type are merged.
  656. */
  657. static int iommu_insert_resv_region(struct iommu_resv_region *new,
  658. struct list_head *regions)
  659. {
  660. struct iommu_resv_region *iter, *tmp, *nr, *top;
  661. LIST_HEAD(stack);
  662. nr = iommu_alloc_resv_region(new->start, new->length,
  663. new->prot, new->type, GFP_KERNEL);
  664. if (!nr)
  665. return -ENOMEM;
  666. /* First add the new element based on start address sorting */
  667. list_for_each_entry(iter, regions, list) {
  668. if (nr->start < iter->start ||
  669. (nr->start == iter->start && nr->type <= iter->type))
  670. break;
  671. }
  672. list_add_tail(&nr->list, &iter->list);
  673. /* Merge overlapping segments of type nr->type in @regions, if any */
  674. list_for_each_entry_safe(iter, tmp, regions, list) {
  675. phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
  676. /* no merge needed on elements of different types than @new */
  677. if (iter->type != new->type) {
  678. list_move_tail(&iter->list, &stack);
  679. continue;
  680. }
  681. /* look for the last stack element of same type as @iter */
  682. list_for_each_entry_reverse(top, &stack, list)
  683. if (top->type == iter->type)
  684. goto check_overlap;
  685. list_move_tail(&iter->list, &stack);
  686. continue;
  687. check_overlap:
  688. top_end = top->start + top->length - 1;
  689. if (iter->start > top_end + 1) {
  690. list_move_tail(&iter->list, &stack);
  691. } else {
  692. top->length = max(top_end, iter_end) - top->start + 1;
  693. list_del(&iter->list);
  694. kfree(iter);
  695. }
  696. }
  697. list_splice(&stack, regions);
  698. return 0;
  699. }
  700. static int
  701. iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
  702. struct list_head *group_resv_regions)
  703. {
  704. struct iommu_resv_region *entry;
  705. int ret = 0;
  706. list_for_each_entry(entry, dev_resv_regions, list) {
  707. ret = iommu_insert_resv_region(entry, group_resv_regions);
  708. if (ret)
  709. break;
  710. }
  711. return ret;
  712. }
  713. int iommu_get_group_resv_regions(struct iommu_group *group,
  714. struct list_head *head)
  715. {
  716. struct group_device *device;
  717. int ret = 0;
  718. mutex_lock(&group->mutex);
  719. for_each_group_device(group, device) {
  720. struct list_head dev_resv_regions;
  721. /*
  722. * Non-API groups still expose reserved_regions in sysfs,
  723. * so filter out calls that get here that way.
  724. */
  725. if (!dev_has_iommu(device->dev))
  726. break;
  727. INIT_LIST_HEAD(&dev_resv_regions);
  728. iommu_get_resv_regions(device->dev, &dev_resv_regions);
  729. ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
  730. iommu_put_resv_regions(device->dev, &dev_resv_regions);
  731. if (ret)
  732. break;
  733. }
  734. mutex_unlock(&group->mutex);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
  738. static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
  739. char *buf)
  740. {
  741. struct iommu_resv_region *region, *next;
  742. struct list_head group_resv_regions;
  743. int offset = 0;
  744. INIT_LIST_HEAD(&group_resv_regions);
  745. iommu_get_group_resv_regions(group, &group_resv_regions);
  746. list_for_each_entry_safe(region, next, &group_resv_regions, list) {
  747. offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
  748. (long long)region->start,
  749. (long long)(region->start +
  750. region->length - 1),
  751. iommu_group_resv_type_string[region->type]);
  752. kfree(region);
  753. }
  754. return offset;
  755. }
  756. static ssize_t iommu_group_show_type(struct iommu_group *group,
  757. char *buf)
  758. {
  759. char *type = "unknown";
  760. mutex_lock(&group->mutex);
  761. if (group->default_domain) {
  762. switch (group->default_domain->type) {
  763. case IOMMU_DOMAIN_BLOCKED:
  764. type = "blocked";
  765. break;
  766. case IOMMU_DOMAIN_IDENTITY:
  767. type = "identity";
  768. break;
  769. case IOMMU_DOMAIN_UNMANAGED:
  770. type = "unmanaged";
  771. break;
  772. case IOMMU_DOMAIN_DMA:
  773. type = "DMA";
  774. break;
  775. case IOMMU_DOMAIN_DMA_FQ:
  776. type = "DMA-FQ";
  777. break;
  778. }
  779. }
  780. mutex_unlock(&group->mutex);
  781. return sysfs_emit(buf, "%s\n", type);
  782. }
  783. static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
  784. static IOMMU_GROUP_ATTR(reserved_regions, 0444,
  785. iommu_group_show_resv_regions, NULL);
  786. static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
  787. iommu_group_store_type);
  788. static void iommu_group_release(struct kobject *kobj)
  789. {
  790. struct iommu_group *group = to_iommu_group(kobj);
  791. pr_debug("Releasing group %d\n", group->id);
  792. if (group->iommu_data_release)
  793. group->iommu_data_release(group->iommu_data);
  794. ida_free(&iommu_group_ida, group->id);
  795. /* Domains are free'd by iommu_deinit_device() */
  796. WARN_ON(group->default_domain);
  797. WARN_ON(group->blocking_domain);
  798. kfree(group->name);
  799. kfree(group);
  800. }
  801. static const struct kobj_type iommu_group_ktype = {
  802. .sysfs_ops = &iommu_group_sysfs_ops,
  803. .release = iommu_group_release,
  804. };
  805. /**
  806. * iommu_group_alloc - Allocate a new group
  807. *
  808. * This function is called by an iommu driver to allocate a new iommu
  809. * group. The iommu group represents the minimum granularity of the iommu.
  810. * Upon successful return, the caller holds a reference to the supplied
  811. * group in order to hold the group until devices are added. Use
  812. * iommu_group_put() to release this extra reference count, allowing the
  813. * group to be automatically reclaimed once it has no devices or external
  814. * references.
  815. */
  816. struct iommu_group *iommu_group_alloc(void)
  817. {
  818. struct iommu_group *group;
  819. int ret;
  820. group = kzalloc(sizeof(*group), GFP_KERNEL);
  821. if (!group)
  822. return ERR_PTR(-ENOMEM);
  823. group->kobj.kset = iommu_group_kset;
  824. mutex_init(&group->mutex);
  825. INIT_LIST_HEAD(&group->devices);
  826. INIT_LIST_HEAD(&group->entry);
  827. xa_init(&group->pasid_array);
  828. ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
  829. if (ret < 0) {
  830. kfree(group);
  831. return ERR_PTR(ret);
  832. }
  833. group->id = ret;
  834. ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
  835. NULL, "%d", group->id);
  836. if (ret) {
  837. kobject_put(&group->kobj);
  838. return ERR_PTR(ret);
  839. }
  840. group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
  841. if (!group->devices_kobj) {
  842. kobject_put(&group->kobj); /* triggers .release & free */
  843. return ERR_PTR(-ENOMEM);
  844. }
  845. /*
  846. * The devices_kobj holds a reference on the group kobject, so
  847. * as long as that exists so will the group. We can therefore
  848. * use the devices_kobj for reference counting.
  849. */
  850. kobject_put(&group->kobj);
  851. ret = iommu_group_create_file(group,
  852. &iommu_group_attr_reserved_regions);
  853. if (ret) {
  854. kobject_put(group->devices_kobj);
  855. return ERR_PTR(ret);
  856. }
  857. ret = iommu_group_create_file(group, &iommu_group_attr_type);
  858. if (ret) {
  859. kobject_put(group->devices_kobj);
  860. return ERR_PTR(ret);
  861. }
  862. pr_debug("Allocated group %d\n", group->id);
  863. return group;
  864. }
  865. EXPORT_SYMBOL_GPL(iommu_group_alloc);
  866. /**
  867. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  868. * @group: the group
  869. *
  870. * iommu drivers can store data in the group for use when doing iommu
  871. * operations. This function provides a way to retrieve it. Caller
  872. * should hold a group reference.
  873. */
  874. void *iommu_group_get_iommudata(struct iommu_group *group)
  875. {
  876. return group->iommu_data;
  877. }
  878. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  879. /**
  880. * iommu_group_set_iommudata - set iommu_data for a group
  881. * @group: the group
  882. * @iommu_data: new data
  883. * @release: release function for iommu_data
  884. *
  885. * iommu drivers can store data in the group for use when doing iommu
  886. * operations. This function provides a way to set the data after
  887. * the group has been allocated. Caller should hold a group reference.
  888. */
  889. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  890. void (*release)(void *iommu_data))
  891. {
  892. group->iommu_data = iommu_data;
  893. group->iommu_data_release = release;
  894. }
  895. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  896. /**
  897. * iommu_group_set_name - set name for a group
  898. * @group: the group
  899. * @name: name
  900. *
  901. * Allow iommu driver to set a name for a group. When set it will
  902. * appear in a name attribute file under the group in sysfs.
  903. */
  904. int iommu_group_set_name(struct iommu_group *group, const char *name)
  905. {
  906. int ret;
  907. if (group->name) {
  908. iommu_group_remove_file(group, &iommu_group_attr_name);
  909. kfree(group->name);
  910. group->name = NULL;
  911. if (!name)
  912. return 0;
  913. }
  914. group->name = kstrdup(name, GFP_KERNEL);
  915. if (!group->name)
  916. return -ENOMEM;
  917. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  918. if (ret) {
  919. kfree(group->name);
  920. group->name = NULL;
  921. return ret;
  922. }
  923. return 0;
  924. }
  925. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  926. static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
  927. struct device *dev)
  928. {
  929. struct iommu_resv_region *entry;
  930. struct list_head mappings;
  931. unsigned long pg_size;
  932. int ret = 0;
  933. pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
  934. INIT_LIST_HEAD(&mappings);
  935. if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
  936. return -EINVAL;
  937. iommu_get_resv_regions(dev, &mappings);
  938. /* We need to consider overlapping regions for different devices */
  939. list_for_each_entry(entry, &mappings, list) {
  940. dma_addr_t start, end, addr;
  941. size_t map_size = 0;
  942. if (entry->type == IOMMU_RESV_DIRECT)
  943. dev->iommu->require_direct = 1;
  944. if ((entry->type != IOMMU_RESV_DIRECT &&
  945. entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
  946. !iommu_is_dma_domain(domain))
  947. continue;
  948. start = ALIGN(entry->start, pg_size);
  949. end = ALIGN(entry->start + entry->length, pg_size);
  950. for (addr = start; addr <= end; addr += pg_size) {
  951. phys_addr_t phys_addr;
  952. if (addr == end)
  953. goto map_end;
  954. phys_addr = iommu_iova_to_phys(domain, addr);
  955. if (!phys_addr) {
  956. map_size += pg_size;
  957. continue;
  958. }
  959. map_end:
  960. if (map_size) {
  961. ret = iommu_map(domain, addr - map_size,
  962. addr - map_size, map_size,
  963. entry->prot, GFP_KERNEL);
  964. if (ret)
  965. goto out;
  966. map_size = 0;
  967. }
  968. }
  969. }
  970. if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
  971. iommu_flush_iotlb_all(domain);
  972. out:
  973. iommu_put_resv_regions(dev, &mappings);
  974. return ret;
  975. }
  976. /* This is undone by __iommu_group_free_device() */
  977. static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
  978. struct device *dev)
  979. {
  980. int ret, i = 0;
  981. struct group_device *device;
  982. device = kzalloc(sizeof(*device), GFP_KERNEL);
  983. if (!device)
  984. return ERR_PTR(-ENOMEM);
  985. device->dev = dev;
  986. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  987. if (ret)
  988. goto err_free_device;
  989. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  990. rename:
  991. if (!device->name) {
  992. ret = -ENOMEM;
  993. goto err_remove_link;
  994. }
  995. ret = sysfs_create_link_nowarn(group->devices_kobj,
  996. &dev->kobj, device->name);
  997. if (ret) {
  998. if (ret == -EEXIST && i >= 0) {
  999. /*
  1000. * Account for the slim chance of collision
  1001. * and append an instance to the name.
  1002. */
  1003. kfree(device->name);
  1004. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  1005. kobject_name(&dev->kobj), i++);
  1006. goto rename;
  1007. }
  1008. goto err_free_name;
  1009. }
  1010. trace_add_device_to_group(group->id, dev);
  1011. dev_info(dev, "Adding to iommu group %d\n", group->id);
  1012. return device;
  1013. err_free_name:
  1014. kfree(device->name);
  1015. err_remove_link:
  1016. sysfs_remove_link(&dev->kobj, "iommu_group");
  1017. err_free_device:
  1018. kfree(device);
  1019. dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
  1020. return ERR_PTR(ret);
  1021. }
  1022. /**
  1023. * iommu_group_add_device - add a device to an iommu group
  1024. * @group: the group into which to add the device (reference should be held)
  1025. * @dev: the device
  1026. *
  1027. * This function is called by an iommu driver to add a device into a
  1028. * group. Adding a device increments the group reference count.
  1029. */
  1030. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  1031. {
  1032. struct group_device *gdev;
  1033. gdev = iommu_group_alloc_device(group, dev);
  1034. if (IS_ERR(gdev))
  1035. return PTR_ERR(gdev);
  1036. iommu_group_ref_get(group);
  1037. dev->iommu_group = group;
  1038. mutex_lock(&group->mutex);
  1039. list_add_tail(&gdev->list, &group->devices);
  1040. mutex_unlock(&group->mutex);
  1041. return 0;
  1042. }
  1043. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  1044. /**
  1045. * iommu_group_remove_device - remove a device from it's current group
  1046. * @dev: device to be removed
  1047. *
  1048. * This function is called by an iommu driver to remove the device from
  1049. * it's current group. This decrements the iommu group reference count.
  1050. */
  1051. void iommu_group_remove_device(struct device *dev)
  1052. {
  1053. struct iommu_group *group = dev->iommu_group;
  1054. if (!group)
  1055. return;
  1056. dev_info(dev, "Removing from iommu group %d\n", group->id);
  1057. __iommu_group_remove_device(dev);
  1058. }
  1059. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  1060. #if IS_ENABLED(CONFIG_LOCKDEP) && IS_ENABLED(CONFIG_IOMMU_API)
  1061. /**
  1062. * iommu_group_mutex_assert - Check device group mutex lock
  1063. * @dev: the device that has group param set
  1064. *
  1065. * This function is called by an iommu driver to check whether it holds
  1066. * group mutex lock for the given device or not.
  1067. *
  1068. * Note that this function must be called after device group param is set.
  1069. */
  1070. void iommu_group_mutex_assert(struct device *dev)
  1071. {
  1072. struct iommu_group *group = dev->iommu_group;
  1073. lockdep_assert_held(&group->mutex);
  1074. }
  1075. EXPORT_SYMBOL_GPL(iommu_group_mutex_assert);
  1076. #endif
  1077. static struct device *iommu_group_first_dev(struct iommu_group *group)
  1078. {
  1079. lockdep_assert_held(&group->mutex);
  1080. return list_first_entry(&group->devices, struct group_device, list)->dev;
  1081. }
  1082. /**
  1083. * iommu_group_for_each_dev - iterate over each device in the group
  1084. * @group: the group
  1085. * @data: caller opaque data to be passed to callback function
  1086. * @fn: caller supplied callback function
  1087. *
  1088. * This function is called by group users to iterate over group devices.
  1089. * Callers should hold a reference count to the group during callback.
  1090. * The group->mutex is held across callbacks, which will block calls to
  1091. * iommu_group_add/remove_device.
  1092. */
  1093. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  1094. int (*fn)(struct device *, void *))
  1095. {
  1096. struct group_device *device;
  1097. int ret = 0;
  1098. mutex_lock(&group->mutex);
  1099. for_each_group_device(group, device) {
  1100. ret = fn(device->dev, data);
  1101. if (ret)
  1102. break;
  1103. }
  1104. mutex_unlock(&group->mutex);
  1105. return ret;
  1106. }
  1107. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  1108. /**
  1109. * iommu_group_get - Return the group for a device and increment reference
  1110. * @dev: get the group that this device belongs to
  1111. *
  1112. * This function is called by iommu drivers and users to get the group
  1113. * for the specified device. If found, the group is returned and the group
  1114. * reference in incremented, else NULL.
  1115. */
  1116. struct iommu_group *iommu_group_get(struct device *dev)
  1117. {
  1118. struct iommu_group *group = dev->iommu_group;
  1119. if (group)
  1120. kobject_get(group->devices_kobj);
  1121. return group;
  1122. }
  1123. EXPORT_SYMBOL_GPL(iommu_group_get);
  1124. /**
  1125. * iommu_group_ref_get - Increment reference on a group
  1126. * @group: the group to use, must not be NULL
  1127. *
  1128. * This function is called by iommu drivers to take additional references on an
  1129. * existing group. Returns the given group for convenience.
  1130. */
  1131. struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
  1132. {
  1133. kobject_get(group->devices_kobj);
  1134. return group;
  1135. }
  1136. EXPORT_SYMBOL_GPL(iommu_group_ref_get);
  1137. /**
  1138. * iommu_group_put - Decrement group reference
  1139. * @group: the group to use
  1140. *
  1141. * This function is called by iommu drivers and users to release the
  1142. * iommu group. Once the reference count is zero, the group is released.
  1143. */
  1144. void iommu_group_put(struct iommu_group *group)
  1145. {
  1146. if (group)
  1147. kobject_put(group->devices_kobj);
  1148. }
  1149. EXPORT_SYMBOL_GPL(iommu_group_put);
  1150. /**
  1151. * iommu_group_id - Return ID for a group
  1152. * @group: the group to ID
  1153. *
  1154. * Return the unique ID for the group matching the sysfs group number.
  1155. */
  1156. int iommu_group_id(struct iommu_group *group)
  1157. {
  1158. return group->id;
  1159. }
  1160. EXPORT_SYMBOL_GPL(iommu_group_id);
  1161. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  1162. unsigned long *devfns);
  1163. /*
  1164. * To consider a PCI device isolated, we require ACS to support Source
  1165. * Validation, Request Redirection, Completer Redirection, and Upstream
  1166. * Forwarding. This effectively means that devices cannot spoof their
  1167. * requester ID, requests and completions cannot be redirected, and all
  1168. * transactions are forwarded upstream, even as it passes through a
  1169. * bridge where the target device is downstream.
  1170. */
  1171. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  1172. /*
  1173. * For multifunction devices which are not isolated from each other, find
  1174. * all the other non-isolated functions and look for existing groups. For
  1175. * each function, we also need to look for aliases to or from other devices
  1176. * that may already have a group.
  1177. */
  1178. static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
  1179. unsigned long *devfns)
  1180. {
  1181. struct pci_dev *tmp = NULL;
  1182. struct iommu_group *group;
  1183. if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
  1184. return NULL;
  1185. for_each_pci_dev(tmp) {
  1186. if (tmp == pdev || tmp->bus != pdev->bus ||
  1187. PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
  1188. pci_acs_enabled(tmp, REQ_ACS_FLAGS))
  1189. continue;
  1190. group = get_pci_alias_group(tmp, devfns);
  1191. if (group) {
  1192. pci_dev_put(tmp);
  1193. return group;
  1194. }
  1195. }
  1196. return NULL;
  1197. }
  1198. /*
  1199. * Look for aliases to or from the given device for existing groups. DMA
  1200. * aliases are only supported on the same bus, therefore the search
  1201. * space is quite small (especially since we're really only looking at pcie
  1202. * device, and therefore only expect multiple slots on the root complex or
  1203. * downstream switch ports). It's conceivable though that a pair of
  1204. * multifunction devices could have aliases between them that would cause a
  1205. * loop. To prevent this, we use a bitmap to track where we've been.
  1206. */
  1207. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  1208. unsigned long *devfns)
  1209. {
  1210. struct pci_dev *tmp = NULL;
  1211. struct iommu_group *group;
  1212. if (test_and_set_bit(pdev->devfn & 0xff, devfns))
  1213. return NULL;
  1214. group = iommu_group_get(&pdev->dev);
  1215. if (group)
  1216. return group;
  1217. for_each_pci_dev(tmp) {
  1218. if (tmp == pdev || tmp->bus != pdev->bus)
  1219. continue;
  1220. /* We alias them or they alias us */
  1221. if (pci_devs_are_dma_aliases(pdev, tmp)) {
  1222. group = get_pci_alias_group(tmp, devfns);
  1223. if (group) {
  1224. pci_dev_put(tmp);
  1225. return group;
  1226. }
  1227. group = get_pci_function_alias_group(tmp, devfns);
  1228. if (group) {
  1229. pci_dev_put(tmp);
  1230. return group;
  1231. }
  1232. }
  1233. }
  1234. return NULL;
  1235. }
  1236. struct group_for_pci_data {
  1237. struct pci_dev *pdev;
  1238. struct iommu_group *group;
  1239. };
  1240. /*
  1241. * DMA alias iterator callback, return the last seen device. Stop and return
  1242. * the IOMMU group if we find one along the way.
  1243. */
  1244. static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
  1245. {
  1246. struct group_for_pci_data *data = opaque;
  1247. data->pdev = pdev;
  1248. data->group = iommu_group_get(&pdev->dev);
  1249. return data->group != NULL;
  1250. }
  1251. /*
  1252. * Generic device_group call-back function. It just allocates one
  1253. * iommu-group per device.
  1254. */
  1255. struct iommu_group *generic_device_group(struct device *dev)
  1256. {
  1257. return iommu_group_alloc();
  1258. }
  1259. EXPORT_SYMBOL_GPL(generic_device_group);
  1260. /*
  1261. * Generic device_group call-back function. It just allocates one
  1262. * iommu-group per iommu driver instance shared by every device
  1263. * probed by that iommu driver.
  1264. */
  1265. struct iommu_group *generic_single_device_group(struct device *dev)
  1266. {
  1267. struct iommu_device *iommu = dev->iommu->iommu_dev;
  1268. if (!iommu->singleton_group) {
  1269. struct iommu_group *group;
  1270. group = iommu_group_alloc();
  1271. if (IS_ERR(group))
  1272. return group;
  1273. iommu->singleton_group = group;
  1274. }
  1275. return iommu_group_ref_get(iommu->singleton_group);
  1276. }
  1277. EXPORT_SYMBOL_GPL(generic_single_device_group);
  1278. /*
  1279. * Use standard PCI bus topology, isolation features, and DMA alias quirks
  1280. * to find or create an IOMMU group for a device.
  1281. */
  1282. struct iommu_group *pci_device_group(struct device *dev)
  1283. {
  1284. struct pci_dev *pdev = to_pci_dev(dev);
  1285. struct group_for_pci_data data;
  1286. struct pci_bus *bus;
  1287. struct iommu_group *group = NULL;
  1288. u64 devfns[4] = { 0 };
  1289. if (WARN_ON(!dev_is_pci(dev)))
  1290. return ERR_PTR(-EINVAL);
  1291. /*
  1292. * Find the upstream DMA alias for the device. A device must not
  1293. * be aliased due to topology in order to have its own IOMMU group.
  1294. * If we find an alias along the way that already belongs to a
  1295. * group, use it.
  1296. */
  1297. if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
  1298. return data.group;
  1299. pdev = data.pdev;
  1300. /*
  1301. * Continue upstream from the point of minimum IOMMU granularity
  1302. * due to aliases to the point where devices are protected from
  1303. * peer-to-peer DMA by PCI ACS. Again, if we find an existing
  1304. * group, use it.
  1305. */
  1306. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  1307. if (!bus->self)
  1308. continue;
  1309. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  1310. break;
  1311. pdev = bus->self;
  1312. group = iommu_group_get(&pdev->dev);
  1313. if (group)
  1314. return group;
  1315. }
  1316. /*
  1317. * Look for existing groups on device aliases. If we alias another
  1318. * device or another device aliases us, use the same group.
  1319. */
  1320. group = get_pci_alias_group(pdev, (unsigned long *)devfns);
  1321. if (group)
  1322. return group;
  1323. /*
  1324. * Look for existing groups on non-isolated functions on the same
  1325. * slot and aliases of those funcions, if any. No need to clear
  1326. * the search bitmap, the tested devfns are still valid.
  1327. */
  1328. group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
  1329. if (group)
  1330. return group;
  1331. /* No shared group found, allocate new */
  1332. return iommu_group_alloc();
  1333. }
  1334. EXPORT_SYMBOL_GPL(pci_device_group);
  1335. /* Get the IOMMU group for device on fsl-mc bus */
  1336. struct iommu_group *fsl_mc_device_group(struct device *dev)
  1337. {
  1338. struct device *cont_dev = fsl_mc_cont_dev(dev);
  1339. struct iommu_group *group;
  1340. group = iommu_group_get(cont_dev);
  1341. if (!group)
  1342. group = iommu_group_alloc();
  1343. return group;
  1344. }
  1345. EXPORT_SYMBOL_GPL(fsl_mc_device_group);
  1346. static struct iommu_domain *
  1347. __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
  1348. {
  1349. if (group->default_domain && group->default_domain->type == req_type)
  1350. return group->default_domain;
  1351. return __iommu_group_domain_alloc(group, req_type);
  1352. }
  1353. /*
  1354. * req_type of 0 means "auto" which means to select a domain based on
  1355. * iommu_def_domain_type or what the driver actually supports.
  1356. */
  1357. static struct iommu_domain *
  1358. iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
  1359. {
  1360. const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group));
  1361. struct iommu_domain *dom;
  1362. lockdep_assert_held(&group->mutex);
  1363. /*
  1364. * Allow legacy drivers to specify the domain that will be the default
  1365. * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
  1366. * domain. Do not use in new drivers.
  1367. */
  1368. if (ops->default_domain) {
  1369. if (req_type != ops->default_domain->type)
  1370. return ERR_PTR(-EINVAL);
  1371. return ops->default_domain;
  1372. }
  1373. if (req_type)
  1374. return __iommu_group_alloc_default_domain(group, req_type);
  1375. /* The driver gave no guidance on what type to use, try the default */
  1376. dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
  1377. if (!IS_ERR(dom))
  1378. return dom;
  1379. /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
  1380. if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
  1381. return ERR_PTR(-EINVAL);
  1382. dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
  1383. if (IS_ERR(dom))
  1384. return dom;
  1385. pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
  1386. iommu_def_domain_type, group->name);
  1387. return dom;
  1388. }
  1389. struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
  1390. {
  1391. return group->default_domain;
  1392. }
  1393. static int probe_iommu_group(struct device *dev, void *data)
  1394. {
  1395. struct list_head *group_list = data;
  1396. int ret;
  1397. mutex_lock(&iommu_probe_device_lock);
  1398. ret = __iommu_probe_device(dev, group_list);
  1399. mutex_unlock(&iommu_probe_device_lock);
  1400. if (ret == -ENODEV)
  1401. ret = 0;
  1402. return ret;
  1403. }
  1404. static int iommu_bus_notifier(struct notifier_block *nb,
  1405. unsigned long action, void *data)
  1406. {
  1407. struct device *dev = data;
  1408. if (action == BUS_NOTIFY_ADD_DEVICE) {
  1409. int ret;
  1410. ret = iommu_probe_device(dev);
  1411. return (ret) ? NOTIFY_DONE : NOTIFY_OK;
  1412. } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
  1413. iommu_release_device(dev);
  1414. return NOTIFY_OK;
  1415. }
  1416. return 0;
  1417. }
  1418. /*
  1419. * Combine the driver's chosen def_domain_type across all the devices in a
  1420. * group. Drivers must give a consistent result.
  1421. */
  1422. static int iommu_get_def_domain_type(struct iommu_group *group,
  1423. struct device *dev, int cur_type)
  1424. {
  1425. const struct iommu_ops *ops = dev_iommu_ops(dev);
  1426. int type;
  1427. if (ops->default_domain) {
  1428. /*
  1429. * Drivers that declare a global static default_domain will
  1430. * always choose that.
  1431. */
  1432. type = ops->default_domain->type;
  1433. } else {
  1434. if (ops->def_domain_type)
  1435. type = ops->def_domain_type(dev);
  1436. else
  1437. return cur_type;
  1438. }
  1439. if (!type || cur_type == type)
  1440. return cur_type;
  1441. if (!cur_type)
  1442. return type;
  1443. dev_err_ratelimited(
  1444. dev,
  1445. "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",
  1446. iommu_domain_type_str(cur_type), iommu_domain_type_str(type),
  1447. group->id);
  1448. /*
  1449. * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY
  1450. * takes precedence.
  1451. */
  1452. if (type == IOMMU_DOMAIN_IDENTITY)
  1453. return type;
  1454. return cur_type;
  1455. }
  1456. /*
  1457. * A target_type of 0 will select the best domain type. 0 can be returned in
  1458. * this case meaning the global default should be used.
  1459. */
  1460. static int iommu_get_default_domain_type(struct iommu_group *group,
  1461. int target_type)
  1462. {
  1463. struct device *untrusted = NULL;
  1464. struct group_device *gdev;
  1465. int driver_type = 0;
  1466. lockdep_assert_held(&group->mutex);
  1467. /*
  1468. * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an
  1469. * identity_domain and it will automatically become their default
  1470. * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.
  1471. * Override the selection to IDENTITY.
  1472. */
  1473. if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
  1474. static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&
  1475. IS_ENABLED(CONFIG_IOMMU_DMA)));
  1476. driver_type = IOMMU_DOMAIN_IDENTITY;
  1477. }
  1478. for_each_group_device(group, gdev) {
  1479. driver_type = iommu_get_def_domain_type(group, gdev->dev,
  1480. driver_type);
  1481. if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
  1482. /*
  1483. * No ARM32 using systems will set untrusted, it cannot
  1484. * work.
  1485. */
  1486. if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
  1487. return -1;
  1488. untrusted = gdev->dev;
  1489. }
  1490. }
  1491. /*
  1492. * If the common dma ops are not selected in kconfig then we cannot use
  1493. * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been
  1494. * selected.
  1495. */
  1496. if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {
  1497. if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))
  1498. return -1;
  1499. if (!driver_type)
  1500. driver_type = IOMMU_DOMAIN_IDENTITY;
  1501. }
  1502. if (untrusted) {
  1503. if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
  1504. dev_err_ratelimited(
  1505. untrusted,
  1506. "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
  1507. group->id, iommu_domain_type_str(driver_type));
  1508. return -1;
  1509. }
  1510. driver_type = IOMMU_DOMAIN_DMA;
  1511. }
  1512. if (target_type) {
  1513. if (driver_type && target_type != driver_type)
  1514. return -1;
  1515. return target_type;
  1516. }
  1517. return driver_type;
  1518. }
  1519. static void iommu_group_do_probe_finalize(struct device *dev)
  1520. {
  1521. const struct iommu_ops *ops = dev_iommu_ops(dev);
  1522. if (ops->probe_finalize)
  1523. ops->probe_finalize(dev);
  1524. }
  1525. int bus_iommu_probe(const struct bus_type *bus)
  1526. {
  1527. struct iommu_group *group, *next;
  1528. LIST_HEAD(group_list);
  1529. int ret;
  1530. ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
  1531. if (ret)
  1532. return ret;
  1533. list_for_each_entry_safe(group, next, &group_list, entry) {
  1534. struct group_device *gdev;
  1535. mutex_lock(&group->mutex);
  1536. /* Remove item from the list */
  1537. list_del_init(&group->entry);
  1538. /*
  1539. * We go to the trouble of deferred default domain creation so
  1540. * that the cross-group default domain type and the setup of the
  1541. * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
  1542. */
  1543. ret = iommu_setup_default_domain(group, 0);
  1544. if (ret) {
  1545. mutex_unlock(&group->mutex);
  1546. return ret;
  1547. }
  1548. for_each_group_device(group, gdev)
  1549. iommu_setup_dma_ops(gdev->dev);
  1550. mutex_unlock(&group->mutex);
  1551. /*
  1552. * FIXME: Mis-locked because the ops->probe_finalize() call-back
  1553. * of some IOMMU drivers calls arm_iommu_attach_device() which
  1554. * in-turn might call back into IOMMU core code, where it tries
  1555. * to take group->mutex, resulting in a deadlock.
  1556. */
  1557. for_each_group_device(group, gdev)
  1558. iommu_group_do_probe_finalize(gdev->dev);
  1559. }
  1560. return 0;
  1561. }
  1562. /**
  1563. * iommu_present() - make platform-specific assumptions about an IOMMU
  1564. * @bus: bus to check
  1565. *
  1566. * Do not use this function. You want device_iommu_mapped() instead.
  1567. *
  1568. * Return: true if some IOMMU is present and aware of devices on the given bus;
  1569. * in general it may not be the only IOMMU, and it may not have anything to do
  1570. * with whatever device you are ultimately interested in.
  1571. */
  1572. bool iommu_present(const struct bus_type *bus)
  1573. {
  1574. bool ret = false;
  1575. for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
  1576. if (iommu_buses[i] == bus) {
  1577. spin_lock(&iommu_device_lock);
  1578. ret = !list_empty(&iommu_device_list);
  1579. spin_unlock(&iommu_device_lock);
  1580. }
  1581. }
  1582. return ret;
  1583. }
  1584. EXPORT_SYMBOL_GPL(iommu_present);
  1585. /**
  1586. * device_iommu_capable() - check for a general IOMMU capability
  1587. * @dev: device to which the capability would be relevant, if available
  1588. * @cap: IOMMU capability
  1589. *
  1590. * Return: true if an IOMMU is present and supports the given capability
  1591. * for the given device, otherwise false.
  1592. */
  1593. bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
  1594. {
  1595. const struct iommu_ops *ops;
  1596. if (!dev_has_iommu(dev))
  1597. return false;
  1598. ops = dev_iommu_ops(dev);
  1599. if (!ops->capable)
  1600. return false;
  1601. return ops->capable(dev, cap);
  1602. }
  1603. EXPORT_SYMBOL_GPL(device_iommu_capable);
  1604. /**
  1605. * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
  1606. * for a group
  1607. * @group: Group to query
  1608. *
  1609. * IOMMU groups should not have differing values of
  1610. * msi_device_has_isolated_msi() for devices in a group. However nothing
  1611. * directly prevents this, so ensure mistakes don't result in isolation failures
  1612. * by checking that all the devices are the same.
  1613. */
  1614. bool iommu_group_has_isolated_msi(struct iommu_group *group)
  1615. {
  1616. struct group_device *group_dev;
  1617. bool ret = true;
  1618. mutex_lock(&group->mutex);
  1619. for_each_group_device(group, group_dev)
  1620. ret &= msi_device_has_isolated_msi(group_dev->dev);
  1621. mutex_unlock(&group->mutex);
  1622. return ret;
  1623. }
  1624. EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
  1625. /**
  1626. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  1627. * @domain: iommu domain
  1628. * @handler: fault handler
  1629. * @token: user data, will be passed back to the fault handler
  1630. *
  1631. * This function should be used by IOMMU users which want to be notified
  1632. * whenever an IOMMU fault happens.
  1633. *
  1634. * The fault handler itself should return 0 on success, and an appropriate
  1635. * error code otherwise.
  1636. */
  1637. void iommu_set_fault_handler(struct iommu_domain *domain,
  1638. iommu_fault_handler_t handler,
  1639. void *token)
  1640. {
  1641. BUG_ON(!domain);
  1642. domain->handler = handler;
  1643. domain->handler_token = token;
  1644. }
  1645. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  1646. static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
  1647. struct device *dev,
  1648. unsigned int type)
  1649. {
  1650. struct iommu_domain *domain;
  1651. unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
  1652. if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)
  1653. return ops->identity_domain;
  1654. else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)
  1655. return ops->blocked_domain;
  1656. else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)
  1657. domain = ops->domain_alloc_paging(dev);
  1658. else if (ops->domain_alloc)
  1659. domain = ops->domain_alloc(alloc_type);
  1660. else
  1661. return ERR_PTR(-EOPNOTSUPP);
  1662. /*
  1663. * Many domain_alloc ops now return ERR_PTR, make things easier for the
  1664. * driver by accepting ERR_PTR from all domain_alloc ops instead of
  1665. * having two rules.
  1666. */
  1667. if (IS_ERR(domain))
  1668. return domain;
  1669. if (!domain)
  1670. return ERR_PTR(-ENOMEM);
  1671. domain->type = type;
  1672. domain->owner = ops;
  1673. /*
  1674. * If not already set, assume all sizes by default; the driver
  1675. * may override this later
  1676. */
  1677. if (!domain->pgsize_bitmap)
  1678. domain->pgsize_bitmap = ops->pgsize_bitmap;
  1679. if (!domain->ops)
  1680. domain->ops = ops->default_domain_ops;
  1681. if (iommu_is_dma_domain(domain)) {
  1682. int rc;
  1683. rc = iommu_get_dma_cookie(domain);
  1684. if (rc) {
  1685. iommu_domain_free(domain);
  1686. return ERR_PTR(rc);
  1687. }
  1688. }
  1689. return domain;
  1690. }
  1691. static struct iommu_domain *
  1692. __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
  1693. {
  1694. struct device *dev = iommu_group_first_dev(group);
  1695. return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);
  1696. }
  1697. static int __iommu_domain_alloc_dev(struct device *dev, void *data)
  1698. {
  1699. const struct iommu_ops **ops = data;
  1700. if (!dev_has_iommu(dev))
  1701. return 0;
  1702. if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),
  1703. "Multiple IOMMU drivers present for bus %s, which the public IOMMU API can't fully support yet. You will still need to disable one or more for this to work, sorry!\n",
  1704. dev_bus_name(dev)))
  1705. return -EBUSY;
  1706. *ops = dev_iommu_ops(dev);
  1707. return 0;
  1708. }
  1709. /*
  1710. * The iommu ops in bus has been retired. Do not use this interface in
  1711. * new drivers.
  1712. */
  1713. struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
  1714. {
  1715. const struct iommu_ops *ops = NULL;
  1716. int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);
  1717. struct iommu_domain *domain;
  1718. if (err || !ops)
  1719. return NULL;
  1720. domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);
  1721. if (IS_ERR(domain))
  1722. return NULL;
  1723. return domain;
  1724. }
  1725. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  1726. /**
  1727. * iommu_paging_domain_alloc() - Allocate a paging domain
  1728. * @dev: device for which the domain is allocated
  1729. *
  1730. * Allocate a paging domain which will be managed by a kernel driver. Return
  1731. * allocated domain if successful, or a ERR pointer for failure.
  1732. */
  1733. struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
  1734. {
  1735. if (!dev_has_iommu(dev))
  1736. return ERR_PTR(-ENODEV);
  1737. return __iommu_domain_alloc(dev_iommu_ops(dev), dev, IOMMU_DOMAIN_UNMANAGED);
  1738. }
  1739. EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc);
  1740. void iommu_domain_free(struct iommu_domain *domain)
  1741. {
  1742. if (domain->type == IOMMU_DOMAIN_SVA)
  1743. mmdrop(domain->mm);
  1744. iommu_put_dma_cookie(domain);
  1745. if (domain->ops->free)
  1746. domain->ops->free(domain);
  1747. }
  1748. EXPORT_SYMBOL_GPL(iommu_domain_free);
  1749. /*
  1750. * Put the group's domain back to the appropriate core-owned domain - either the
  1751. * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
  1752. */
  1753. static void __iommu_group_set_core_domain(struct iommu_group *group)
  1754. {
  1755. struct iommu_domain *new_domain;
  1756. if (group->owner)
  1757. new_domain = group->blocking_domain;
  1758. else
  1759. new_domain = group->default_domain;
  1760. __iommu_group_set_domain_nofail(group, new_domain);
  1761. }
  1762. static int __iommu_attach_device(struct iommu_domain *domain,
  1763. struct device *dev)
  1764. {
  1765. int ret;
  1766. if (unlikely(domain->ops->attach_dev == NULL))
  1767. return -ENODEV;
  1768. ret = domain->ops->attach_dev(domain, dev);
  1769. if (ret)
  1770. return ret;
  1771. dev->iommu->attach_deferred = 0;
  1772. trace_attach_device_to_domain(dev);
  1773. return 0;
  1774. }
  1775. /**
  1776. * iommu_attach_device - Attach an IOMMU domain to a device
  1777. * @domain: IOMMU domain to attach
  1778. * @dev: Device that will be attached
  1779. *
  1780. * Returns 0 on success and error code on failure
  1781. *
  1782. * Note that EINVAL can be treated as a soft failure, indicating
  1783. * that certain configuration of the domain is incompatible with
  1784. * the device. In this case attaching a different domain to the
  1785. * device may succeed.
  1786. */
  1787. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  1788. {
  1789. /* Caller must be a probed driver on dev */
  1790. struct iommu_group *group = dev->iommu_group;
  1791. int ret;
  1792. if (!group)
  1793. return -ENODEV;
  1794. /*
  1795. * Lock the group to make sure the device-count doesn't
  1796. * change while we are attaching
  1797. */
  1798. mutex_lock(&group->mutex);
  1799. ret = -EINVAL;
  1800. if (list_count_nodes(&group->devices) != 1)
  1801. goto out_unlock;
  1802. ret = __iommu_attach_group(domain, group);
  1803. out_unlock:
  1804. mutex_unlock(&group->mutex);
  1805. return ret;
  1806. }
  1807. EXPORT_SYMBOL_GPL(iommu_attach_device);
  1808. int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
  1809. {
  1810. if (dev->iommu && dev->iommu->attach_deferred)
  1811. return __iommu_attach_device(domain, dev);
  1812. return 0;
  1813. }
  1814. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  1815. {
  1816. /* Caller must be a probed driver on dev */
  1817. struct iommu_group *group = dev->iommu_group;
  1818. if (!group)
  1819. return;
  1820. mutex_lock(&group->mutex);
  1821. if (WARN_ON(domain != group->domain) ||
  1822. WARN_ON(list_count_nodes(&group->devices) != 1))
  1823. goto out_unlock;
  1824. __iommu_group_set_core_domain(group);
  1825. out_unlock:
  1826. mutex_unlock(&group->mutex);
  1827. }
  1828. EXPORT_SYMBOL_GPL(iommu_detach_device);
  1829. struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  1830. {
  1831. /* Caller must be a probed driver on dev */
  1832. struct iommu_group *group = dev->iommu_group;
  1833. if (!group)
  1834. return NULL;
  1835. return group->domain;
  1836. }
  1837. EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
  1838. /*
  1839. * For IOMMU_DOMAIN_DMA implementations which already provide their own
  1840. * guarantees that the group and its default domain are valid and correct.
  1841. */
  1842. struct iommu_domain *iommu_get_dma_domain(struct device *dev)
  1843. {
  1844. return dev->iommu_group->default_domain;
  1845. }
  1846. static int __iommu_attach_group(struct iommu_domain *domain,
  1847. struct iommu_group *group)
  1848. {
  1849. struct device *dev;
  1850. if (group->domain && group->domain != group->default_domain &&
  1851. group->domain != group->blocking_domain)
  1852. return -EBUSY;
  1853. dev = iommu_group_first_dev(group);
  1854. if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
  1855. return -EINVAL;
  1856. return __iommu_group_set_domain(group, domain);
  1857. }
  1858. /**
  1859. * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
  1860. * @domain: IOMMU domain to attach
  1861. * @group: IOMMU group that will be attached
  1862. *
  1863. * Returns 0 on success and error code on failure
  1864. *
  1865. * Note that EINVAL can be treated as a soft failure, indicating
  1866. * that certain configuration of the domain is incompatible with
  1867. * the group. In this case attaching a different domain to the
  1868. * group may succeed.
  1869. */
  1870. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  1871. {
  1872. int ret;
  1873. mutex_lock(&group->mutex);
  1874. ret = __iommu_attach_group(domain, group);
  1875. mutex_unlock(&group->mutex);
  1876. return ret;
  1877. }
  1878. EXPORT_SYMBOL_GPL(iommu_attach_group);
  1879. /**
  1880. * iommu_group_replace_domain - replace the domain that a group is attached to
  1881. * @new_domain: new IOMMU domain to replace with
  1882. * @group: IOMMU group that will be attached to the new domain
  1883. *
  1884. * This API allows the group to switch domains without being forced to go to
  1885. * the blocking domain in-between.
  1886. *
  1887. * If the currently attached domain is a core domain (e.g. a default_domain),
  1888. * it will act just like the iommu_attach_group().
  1889. */
  1890. int iommu_group_replace_domain(struct iommu_group *group,
  1891. struct iommu_domain *new_domain)
  1892. {
  1893. int ret;
  1894. if (!new_domain)
  1895. return -EINVAL;
  1896. mutex_lock(&group->mutex);
  1897. ret = __iommu_group_set_domain(group, new_domain);
  1898. mutex_unlock(&group->mutex);
  1899. return ret;
  1900. }
  1901. EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
  1902. static int __iommu_device_set_domain(struct iommu_group *group,
  1903. struct device *dev,
  1904. struct iommu_domain *new_domain,
  1905. unsigned int flags)
  1906. {
  1907. int ret;
  1908. /*
  1909. * If the device requires IOMMU_RESV_DIRECT then we cannot allow
  1910. * the blocking domain to be attached as it does not contain the
  1911. * required 1:1 mapping. This test effectively excludes the device
  1912. * being used with iommu_group_claim_dma_owner() which will block
  1913. * vfio and iommufd as well.
  1914. */
  1915. if (dev->iommu->require_direct &&
  1916. (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
  1917. new_domain == group->blocking_domain)) {
  1918. dev_warn(dev,
  1919. "Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");
  1920. return -EINVAL;
  1921. }
  1922. if (dev->iommu->attach_deferred) {
  1923. if (new_domain == group->default_domain)
  1924. return 0;
  1925. dev->iommu->attach_deferred = 0;
  1926. }
  1927. ret = __iommu_attach_device(new_domain, dev);
  1928. if (ret) {
  1929. /*
  1930. * If we have a blocking domain then try to attach that in hopes
  1931. * of avoiding a UAF. Modern drivers should implement blocking
  1932. * domains as global statics that cannot fail.
  1933. */
  1934. if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
  1935. group->blocking_domain &&
  1936. group->blocking_domain != new_domain)
  1937. __iommu_attach_device(group->blocking_domain, dev);
  1938. return ret;
  1939. }
  1940. return 0;
  1941. }
  1942. /*
  1943. * If 0 is returned the group's domain is new_domain. If an error is returned
  1944. * then the group's domain will be set back to the existing domain unless
  1945. * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
  1946. * domains is left inconsistent. This is a driver bug to fail attach with a
  1947. * previously good domain. We try to avoid a kernel UAF because of this.
  1948. *
  1949. * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
  1950. * API works on domains and devices. Bridge that gap by iterating over the
  1951. * devices in a group. Ideally we'd have a single device which represents the
  1952. * requestor ID of the group, but we also allow IOMMU drivers to create policy
  1953. * defined minimum sets, where the physical hardware may be able to distiguish
  1954. * members, but we wish to group them at a higher level (ex. untrusted
  1955. * multi-function PCI devices). Thus we attach each device.
  1956. */
  1957. static int __iommu_group_set_domain_internal(struct iommu_group *group,
  1958. struct iommu_domain *new_domain,
  1959. unsigned int flags)
  1960. {
  1961. struct group_device *last_gdev;
  1962. struct group_device *gdev;
  1963. int result;
  1964. int ret;
  1965. lockdep_assert_held(&group->mutex);
  1966. if (group->domain == new_domain)
  1967. return 0;
  1968. if (WARN_ON(!new_domain))
  1969. return -EINVAL;
  1970. /*
  1971. * Changing the domain is done by calling attach_dev() on the new
  1972. * domain. This switch does not have to be atomic and DMA can be
  1973. * discarded during the transition. DMA must only be able to access
  1974. * either new_domain or group->domain, never something else.
  1975. */
  1976. result = 0;
  1977. for_each_group_device(group, gdev) {
  1978. ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
  1979. flags);
  1980. if (ret) {
  1981. result = ret;
  1982. /*
  1983. * Keep trying the other devices in the group. If a
  1984. * driver fails attach to an otherwise good domain, and
  1985. * does not support blocking domains, it should at least
  1986. * drop its reference on the current domain so we don't
  1987. * UAF.
  1988. */
  1989. if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
  1990. continue;
  1991. goto err_revert;
  1992. }
  1993. }
  1994. group->domain = new_domain;
  1995. return result;
  1996. err_revert:
  1997. /*
  1998. * This is called in error unwind paths. A well behaved driver should
  1999. * always allow us to attach to a domain that was already attached.
  2000. */
  2001. last_gdev = gdev;
  2002. for_each_group_device(group, gdev) {
  2003. /*
  2004. * A NULL domain can happen only for first probe, in which case
  2005. * we leave group->domain as NULL and let release clean
  2006. * everything up.
  2007. */
  2008. if (group->domain)
  2009. WARN_ON(__iommu_device_set_domain(
  2010. group, gdev->dev, group->domain,
  2011. IOMMU_SET_DOMAIN_MUST_SUCCEED));
  2012. if (gdev == last_gdev)
  2013. break;
  2014. }
  2015. return ret;
  2016. }
  2017. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  2018. {
  2019. mutex_lock(&group->mutex);
  2020. __iommu_group_set_core_domain(group);
  2021. mutex_unlock(&group->mutex);
  2022. }
  2023. EXPORT_SYMBOL_GPL(iommu_detach_group);
  2024. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  2025. {
  2026. if (domain->type == IOMMU_DOMAIN_IDENTITY)
  2027. return iova;
  2028. if (domain->type == IOMMU_DOMAIN_BLOCKED)
  2029. return 0;
  2030. return domain->ops->iova_to_phys(domain, iova);
  2031. }
  2032. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  2033. static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
  2034. phys_addr_t paddr, size_t size, size_t *count)
  2035. {
  2036. unsigned int pgsize_idx, pgsize_idx_next;
  2037. unsigned long pgsizes;
  2038. size_t offset, pgsize, pgsize_next;
  2039. size_t offset_end;
  2040. unsigned long addr_merge = paddr | iova;
  2041. /* Page sizes supported by the hardware and small enough for @size */
  2042. pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
  2043. /* Constrain the page sizes further based on the maximum alignment */
  2044. if (likely(addr_merge))
  2045. pgsizes &= GENMASK(__ffs(addr_merge), 0);
  2046. /* Make sure we have at least one suitable page size */
  2047. BUG_ON(!pgsizes);
  2048. /* Pick the biggest page size remaining */
  2049. pgsize_idx = __fls(pgsizes);
  2050. pgsize = BIT(pgsize_idx);
  2051. if (!count)
  2052. return pgsize;
  2053. /* Find the next biggest support page size, if it exists */
  2054. pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
  2055. if (!pgsizes)
  2056. goto out_set_count;
  2057. pgsize_idx_next = __ffs(pgsizes);
  2058. pgsize_next = BIT(pgsize_idx_next);
  2059. /*
  2060. * There's no point trying a bigger page size unless the virtual
  2061. * and physical addresses are similarly offset within the larger page.
  2062. */
  2063. if ((iova ^ paddr) & (pgsize_next - 1))
  2064. goto out_set_count;
  2065. /* Calculate the offset to the next page size alignment boundary */
  2066. offset = pgsize_next - (addr_merge & (pgsize_next - 1));
  2067. /*
  2068. * If size is big enough to accommodate the larger page, reduce
  2069. * the number of smaller pages.
  2070. */
  2071. if (!check_add_overflow(offset, pgsize_next, &offset_end) &&
  2072. offset_end <= size)
  2073. size = offset;
  2074. out_set_count:
  2075. *count = size >> pgsize_idx;
  2076. return pgsize;
  2077. }
  2078. static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
  2079. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  2080. {
  2081. const struct iommu_domain_ops *ops = domain->ops;
  2082. unsigned long orig_iova = iova;
  2083. unsigned int min_pagesz;
  2084. size_t orig_size = size;
  2085. phys_addr_t orig_paddr = paddr;
  2086. int ret = 0;
  2087. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  2088. return -EINVAL;
  2089. if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))
  2090. return -ENODEV;
  2091. /* find out the minimum page size supported */
  2092. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  2093. /*
  2094. * both the virtual address and the physical one, as well as
  2095. * the size of the mapping, must be aligned (at least) to the
  2096. * size of the smallest page supported by the hardware
  2097. */
  2098. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  2099. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  2100. iova, &paddr, size, min_pagesz);
  2101. return -EINVAL;
  2102. }
  2103. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  2104. while (size) {
  2105. size_t pgsize, count, mapped = 0;
  2106. pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
  2107. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
  2108. iova, &paddr, pgsize, count);
  2109. ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
  2110. gfp, &mapped);
  2111. /*
  2112. * Some pages may have been mapped, even if an error occurred,
  2113. * so we should account for those so they can be unmapped.
  2114. */
  2115. size -= mapped;
  2116. if (ret)
  2117. break;
  2118. iova += mapped;
  2119. paddr += mapped;
  2120. }
  2121. /* unroll mapping in case something went wrong */
  2122. if (ret)
  2123. iommu_unmap(domain, orig_iova, orig_size - size);
  2124. else
  2125. trace_map(orig_iova, orig_paddr, orig_size);
  2126. return ret;
  2127. }
  2128. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  2129. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  2130. {
  2131. const struct iommu_domain_ops *ops = domain->ops;
  2132. int ret;
  2133. might_sleep_if(gfpflags_allow_blocking(gfp));
  2134. /* Discourage passing strange GFP flags */
  2135. if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
  2136. __GFP_HIGHMEM)))
  2137. return -EINVAL;
  2138. ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
  2139. if (ret == 0 && ops->iotlb_sync_map) {
  2140. ret = ops->iotlb_sync_map(domain, iova, size);
  2141. if (ret)
  2142. goto out_err;
  2143. }
  2144. return ret;
  2145. out_err:
  2146. /* undo mappings already done */
  2147. iommu_unmap(domain, iova, size);
  2148. return ret;
  2149. }
  2150. EXPORT_SYMBOL_GPL(iommu_map);
  2151. static size_t __iommu_unmap(struct iommu_domain *domain,
  2152. unsigned long iova, size_t size,
  2153. struct iommu_iotlb_gather *iotlb_gather)
  2154. {
  2155. const struct iommu_domain_ops *ops = domain->ops;
  2156. size_t unmapped_page, unmapped = 0;
  2157. unsigned long orig_iova = iova;
  2158. unsigned int min_pagesz;
  2159. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  2160. return 0;
  2161. if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))
  2162. return 0;
  2163. /* find out the minimum page size supported */
  2164. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  2165. /*
  2166. * The virtual address, as well as the size of the mapping, must be
  2167. * aligned (at least) to the size of the smallest page supported
  2168. * by the hardware
  2169. */
  2170. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  2171. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  2172. iova, size, min_pagesz);
  2173. return 0;
  2174. }
  2175. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  2176. /*
  2177. * Keep iterating until we either unmap 'size' bytes (or more)
  2178. * or we hit an area that isn't mapped.
  2179. */
  2180. while (unmapped < size) {
  2181. size_t pgsize, count;
  2182. pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);
  2183. unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);
  2184. if (!unmapped_page)
  2185. break;
  2186. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  2187. iova, unmapped_page);
  2188. iova += unmapped_page;
  2189. unmapped += unmapped_page;
  2190. }
  2191. trace_unmap(orig_iova, size, unmapped);
  2192. return unmapped;
  2193. }
  2194. size_t iommu_unmap(struct iommu_domain *domain,
  2195. unsigned long iova, size_t size)
  2196. {
  2197. struct iommu_iotlb_gather iotlb_gather;
  2198. size_t ret;
  2199. iommu_iotlb_gather_init(&iotlb_gather);
  2200. ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
  2201. iommu_iotlb_sync(domain, &iotlb_gather);
  2202. return ret;
  2203. }
  2204. EXPORT_SYMBOL_GPL(iommu_unmap);
  2205. size_t iommu_unmap_fast(struct iommu_domain *domain,
  2206. unsigned long iova, size_t size,
  2207. struct iommu_iotlb_gather *iotlb_gather)
  2208. {
  2209. return __iommu_unmap(domain, iova, size, iotlb_gather);
  2210. }
  2211. EXPORT_SYMBOL_GPL(iommu_unmap_fast);
  2212. ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  2213. struct scatterlist *sg, unsigned int nents, int prot,
  2214. gfp_t gfp)
  2215. {
  2216. const struct iommu_domain_ops *ops = domain->ops;
  2217. size_t len = 0, mapped = 0;
  2218. phys_addr_t start;
  2219. unsigned int i = 0;
  2220. int ret;
  2221. might_sleep_if(gfpflags_allow_blocking(gfp));
  2222. /* Discourage passing strange GFP flags */
  2223. if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
  2224. __GFP_HIGHMEM)))
  2225. return -EINVAL;
  2226. while (i <= nents) {
  2227. phys_addr_t s_phys = sg_phys(sg);
  2228. if (len && s_phys != start + len) {
  2229. ret = __iommu_map(domain, iova + mapped, start,
  2230. len, prot, gfp);
  2231. if (ret)
  2232. goto out_err;
  2233. mapped += len;
  2234. len = 0;
  2235. }
  2236. if (sg_dma_is_bus_address(sg))
  2237. goto next;
  2238. if (len) {
  2239. len += sg->length;
  2240. } else {
  2241. len = sg->length;
  2242. start = s_phys;
  2243. }
  2244. next:
  2245. if (++i < nents)
  2246. sg = sg_next(sg);
  2247. }
  2248. if (ops->iotlb_sync_map) {
  2249. ret = ops->iotlb_sync_map(domain, iova, mapped);
  2250. if (ret)
  2251. goto out_err;
  2252. }
  2253. return mapped;
  2254. out_err:
  2255. /* undo mappings already done */
  2256. iommu_unmap(domain, iova, mapped);
  2257. return ret;
  2258. }
  2259. EXPORT_SYMBOL_GPL(iommu_map_sg);
  2260. /**
  2261. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  2262. * @domain: the iommu domain where the fault has happened
  2263. * @dev: the device where the fault has happened
  2264. * @iova: the faulting address
  2265. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  2266. *
  2267. * This function should be called by the low-level IOMMU implementations
  2268. * whenever IOMMU faults happen, to allow high-level users, that are
  2269. * interested in such events, to know about them.
  2270. *
  2271. * This event may be useful for several possible use cases:
  2272. * - mere logging of the event
  2273. * - dynamic TLB/PTE loading
  2274. * - if restarting of the faulting device is required
  2275. *
  2276. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  2277. * PTE/TLB loading will one day be supported, implementations will be able
  2278. * to tell whether it succeeded or not according to this return value).
  2279. *
  2280. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  2281. * (though fault handlers can also return -ENOSYS, in case they want to
  2282. * elicit the default behavior of the IOMMU drivers).
  2283. */
  2284. int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
  2285. unsigned long iova, int flags)
  2286. {
  2287. int ret = -ENOSYS;
  2288. /*
  2289. * if upper layers showed interest and installed a fault handler,
  2290. * invoke it.
  2291. */
  2292. if (domain->handler)
  2293. ret = domain->handler(domain, dev, iova, flags,
  2294. domain->handler_token);
  2295. trace_io_page_fault(dev, iova, flags);
  2296. return ret;
  2297. }
  2298. EXPORT_SYMBOL_GPL(report_iommu_fault);
  2299. static int __init iommu_init(void)
  2300. {
  2301. iommu_group_kset = kset_create_and_add("iommu_groups",
  2302. NULL, kernel_kobj);
  2303. BUG_ON(!iommu_group_kset);
  2304. iommu_debugfs_setup();
  2305. return 0;
  2306. }
  2307. core_initcall(iommu_init);
  2308. int iommu_enable_nesting(struct iommu_domain *domain)
  2309. {
  2310. if (domain->type != IOMMU_DOMAIN_UNMANAGED)
  2311. return -EINVAL;
  2312. if (!domain->ops->enable_nesting)
  2313. return -EINVAL;
  2314. return domain->ops->enable_nesting(domain);
  2315. }
  2316. EXPORT_SYMBOL_GPL(iommu_enable_nesting);
  2317. int iommu_set_pgtable_quirks(struct iommu_domain *domain,
  2318. unsigned long quirk)
  2319. {
  2320. if (domain->type != IOMMU_DOMAIN_UNMANAGED)
  2321. return -EINVAL;
  2322. if (!domain->ops->set_pgtable_quirks)
  2323. return -EINVAL;
  2324. return domain->ops->set_pgtable_quirks(domain, quirk);
  2325. }
  2326. EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
  2327. /**
  2328. * iommu_get_resv_regions - get reserved regions
  2329. * @dev: device for which to get reserved regions
  2330. * @list: reserved region list for device
  2331. *
  2332. * This returns a list of reserved IOVA regions specific to this device.
  2333. * A domain user should not map IOVA in these ranges.
  2334. */
  2335. void iommu_get_resv_regions(struct device *dev, struct list_head *list)
  2336. {
  2337. const struct iommu_ops *ops = dev_iommu_ops(dev);
  2338. if (ops->get_resv_regions)
  2339. ops->get_resv_regions(dev, list);
  2340. }
  2341. EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
  2342. /**
  2343. * iommu_put_resv_regions - release reserved regions
  2344. * @dev: device for which to free reserved regions
  2345. * @list: reserved region list for device
  2346. *
  2347. * This releases a reserved region list acquired by iommu_get_resv_regions().
  2348. */
  2349. void iommu_put_resv_regions(struct device *dev, struct list_head *list)
  2350. {
  2351. struct iommu_resv_region *entry, *next;
  2352. list_for_each_entry_safe(entry, next, list, list) {
  2353. if (entry->free)
  2354. entry->free(dev, entry);
  2355. else
  2356. kfree(entry);
  2357. }
  2358. }
  2359. EXPORT_SYMBOL(iommu_put_resv_regions);
  2360. struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
  2361. size_t length, int prot,
  2362. enum iommu_resv_type type,
  2363. gfp_t gfp)
  2364. {
  2365. struct iommu_resv_region *region;
  2366. region = kzalloc(sizeof(*region), gfp);
  2367. if (!region)
  2368. return NULL;
  2369. INIT_LIST_HEAD(&region->list);
  2370. region->start = start;
  2371. region->length = length;
  2372. region->prot = prot;
  2373. region->type = type;
  2374. return region;
  2375. }
  2376. EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
  2377. void iommu_set_default_passthrough(bool cmd_line)
  2378. {
  2379. if (cmd_line)
  2380. iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
  2381. iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
  2382. }
  2383. void iommu_set_default_translated(bool cmd_line)
  2384. {
  2385. if (cmd_line)
  2386. iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
  2387. iommu_def_domain_type = IOMMU_DOMAIN_DMA;
  2388. }
  2389. bool iommu_default_passthrough(void)
  2390. {
  2391. return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
  2392. }
  2393. EXPORT_SYMBOL_GPL(iommu_default_passthrough);
  2394. const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode)
  2395. {
  2396. const struct iommu_ops *ops = NULL;
  2397. struct iommu_device *iommu;
  2398. spin_lock(&iommu_device_lock);
  2399. list_for_each_entry(iommu, &iommu_device_list, list)
  2400. if (iommu->fwnode == fwnode) {
  2401. ops = iommu->ops;
  2402. break;
  2403. }
  2404. spin_unlock(&iommu_device_lock);
  2405. return ops;
  2406. }
  2407. int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode)
  2408. {
  2409. const struct iommu_ops *ops = iommu_ops_from_fwnode(iommu_fwnode);
  2410. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  2411. if (!ops)
  2412. return -EPROBE_DEFER;
  2413. if (fwspec)
  2414. return ops == iommu_fwspec_ops(fwspec) ? 0 : -EINVAL;
  2415. if (!dev_iommu_get(dev))
  2416. return -ENOMEM;
  2417. /* Preallocate for the overwhelmingly common case of 1 ID */
  2418. fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
  2419. if (!fwspec)
  2420. return -ENOMEM;
  2421. fwnode_handle_get(iommu_fwnode);
  2422. fwspec->iommu_fwnode = iommu_fwnode;
  2423. dev_iommu_fwspec_set(dev, fwspec);
  2424. return 0;
  2425. }
  2426. EXPORT_SYMBOL_GPL(iommu_fwspec_init);
  2427. void iommu_fwspec_free(struct device *dev)
  2428. {
  2429. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  2430. if (fwspec) {
  2431. fwnode_handle_put(fwspec->iommu_fwnode);
  2432. kfree(fwspec);
  2433. dev_iommu_fwspec_set(dev, NULL);
  2434. }
  2435. }
  2436. EXPORT_SYMBOL_GPL(iommu_fwspec_free);
  2437. int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids)
  2438. {
  2439. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  2440. int i, new_num;
  2441. if (!fwspec)
  2442. return -EINVAL;
  2443. new_num = fwspec->num_ids + num_ids;
  2444. if (new_num > 1) {
  2445. fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
  2446. GFP_KERNEL);
  2447. if (!fwspec)
  2448. return -ENOMEM;
  2449. dev_iommu_fwspec_set(dev, fwspec);
  2450. }
  2451. for (i = 0; i < num_ids; i++)
  2452. fwspec->ids[fwspec->num_ids + i] = ids[i];
  2453. fwspec->num_ids = new_num;
  2454. return 0;
  2455. }
  2456. EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
  2457. /*
  2458. * Per device IOMMU features.
  2459. */
  2460. int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
  2461. {
  2462. if (dev_has_iommu(dev)) {
  2463. const struct iommu_ops *ops = dev_iommu_ops(dev);
  2464. if (ops->dev_enable_feat)
  2465. return ops->dev_enable_feat(dev, feat);
  2466. }
  2467. return -ENODEV;
  2468. }
  2469. EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
  2470. /*
  2471. * The device drivers should do the necessary cleanups before calling this.
  2472. */
  2473. int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
  2474. {
  2475. if (dev_has_iommu(dev)) {
  2476. const struct iommu_ops *ops = dev_iommu_ops(dev);
  2477. if (ops->dev_disable_feat)
  2478. return ops->dev_disable_feat(dev, feat);
  2479. }
  2480. return -EBUSY;
  2481. }
  2482. EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
  2483. /**
  2484. * iommu_setup_default_domain - Set the default_domain for the group
  2485. * @group: Group to change
  2486. * @target_type: Domain type to set as the default_domain
  2487. *
  2488. * Allocate a default domain and set it as the current domain on the group. If
  2489. * the group already has a default domain it will be changed to the target_type.
  2490. * When target_type is 0 the default domain is selected based on driver and
  2491. * system preferences.
  2492. */
  2493. static int iommu_setup_default_domain(struct iommu_group *group,
  2494. int target_type)
  2495. {
  2496. struct iommu_domain *old_dom = group->default_domain;
  2497. struct group_device *gdev;
  2498. struct iommu_domain *dom;
  2499. bool direct_failed;
  2500. int req_type;
  2501. int ret;
  2502. lockdep_assert_held(&group->mutex);
  2503. req_type = iommu_get_default_domain_type(group, target_type);
  2504. if (req_type < 0)
  2505. return -EINVAL;
  2506. dom = iommu_group_alloc_default_domain(group, req_type);
  2507. if (IS_ERR(dom))
  2508. return PTR_ERR(dom);
  2509. if (group->default_domain == dom)
  2510. return 0;
  2511. /*
  2512. * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
  2513. * mapped before their device is attached, in order to guarantee
  2514. * continuity with any FW activity
  2515. */
  2516. direct_failed = false;
  2517. for_each_group_device(group, gdev) {
  2518. if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
  2519. direct_failed = true;
  2520. dev_warn_once(
  2521. gdev->dev->iommu->iommu_dev->dev,
  2522. "IOMMU driver was not able to establish FW requested direct mapping.");
  2523. }
  2524. }
  2525. /* We must set default_domain early for __iommu_device_set_domain */
  2526. group->default_domain = dom;
  2527. if (!group->domain) {
  2528. /*
  2529. * Drivers are not allowed to fail the first domain attach.
  2530. * The only way to recover from this is to fail attaching the
  2531. * iommu driver and call ops->release_device. Put the domain
  2532. * in group->default_domain so it is freed after.
  2533. */
  2534. ret = __iommu_group_set_domain_internal(
  2535. group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
  2536. if (WARN_ON(ret))
  2537. goto out_free_old;
  2538. } else {
  2539. ret = __iommu_group_set_domain(group, dom);
  2540. if (ret)
  2541. goto err_restore_def_domain;
  2542. }
  2543. /*
  2544. * Drivers are supposed to allow mappings to be installed in a domain
  2545. * before device attachment, but some don't. Hack around this defect by
  2546. * trying again after attaching. If this happens it means the device
  2547. * will not continuously have the IOMMU_RESV_DIRECT map.
  2548. */
  2549. if (direct_failed) {
  2550. for_each_group_device(group, gdev) {
  2551. ret = iommu_create_device_direct_mappings(dom, gdev->dev);
  2552. if (ret)
  2553. goto err_restore_domain;
  2554. }
  2555. }
  2556. out_free_old:
  2557. if (old_dom)
  2558. iommu_domain_free(old_dom);
  2559. return ret;
  2560. err_restore_domain:
  2561. if (old_dom)
  2562. __iommu_group_set_domain_internal(
  2563. group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
  2564. err_restore_def_domain:
  2565. if (old_dom) {
  2566. iommu_domain_free(dom);
  2567. group->default_domain = old_dom;
  2568. }
  2569. return ret;
  2570. }
  2571. /*
  2572. * Changing the default domain through sysfs requires the users to unbind the
  2573. * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
  2574. * transition. Return failure if this isn't met.
  2575. *
  2576. * We need to consider the race between this and the device release path.
  2577. * group->mutex is used here to guarantee that the device release path
  2578. * will not be entered at the same time.
  2579. */
  2580. static ssize_t iommu_group_store_type(struct iommu_group *group,
  2581. const char *buf, size_t count)
  2582. {
  2583. struct group_device *gdev;
  2584. int ret, req_type;
  2585. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2586. return -EACCES;
  2587. if (WARN_ON(!group) || !group->default_domain)
  2588. return -EINVAL;
  2589. if (sysfs_streq(buf, "identity"))
  2590. req_type = IOMMU_DOMAIN_IDENTITY;
  2591. else if (sysfs_streq(buf, "DMA"))
  2592. req_type = IOMMU_DOMAIN_DMA;
  2593. else if (sysfs_streq(buf, "DMA-FQ"))
  2594. req_type = IOMMU_DOMAIN_DMA_FQ;
  2595. else if (sysfs_streq(buf, "auto"))
  2596. req_type = 0;
  2597. else
  2598. return -EINVAL;
  2599. mutex_lock(&group->mutex);
  2600. /* We can bring up a flush queue without tearing down the domain. */
  2601. if (req_type == IOMMU_DOMAIN_DMA_FQ &&
  2602. group->default_domain->type == IOMMU_DOMAIN_DMA) {
  2603. ret = iommu_dma_init_fq(group->default_domain);
  2604. if (ret)
  2605. goto out_unlock;
  2606. group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
  2607. ret = count;
  2608. goto out_unlock;
  2609. }
  2610. /* Otherwise, ensure that device exists and no driver is bound. */
  2611. if (list_empty(&group->devices) || group->owner_cnt) {
  2612. ret = -EPERM;
  2613. goto out_unlock;
  2614. }
  2615. ret = iommu_setup_default_domain(group, req_type);
  2616. if (ret)
  2617. goto out_unlock;
  2618. /* Make sure dma_ops is appropriatley set */
  2619. for_each_group_device(group, gdev)
  2620. iommu_setup_dma_ops(gdev->dev);
  2621. out_unlock:
  2622. mutex_unlock(&group->mutex);
  2623. return ret ?: count;
  2624. }
  2625. /**
  2626. * iommu_device_use_default_domain() - Device driver wants to handle device
  2627. * DMA through the kernel DMA API.
  2628. * @dev: The device.
  2629. *
  2630. * The device driver about to bind @dev wants to do DMA through the kernel
  2631. * DMA API. Return 0 if it is allowed, otherwise an error.
  2632. */
  2633. int iommu_device_use_default_domain(struct device *dev)
  2634. {
  2635. /* Caller is the driver core during the pre-probe path */
  2636. struct iommu_group *group = dev->iommu_group;
  2637. int ret = 0;
  2638. if (!group)
  2639. return 0;
  2640. mutex_lock(&group->mutex);
  2641. /* We may race against bus_iommu_probe() finalising groups here */
  2642. if (!group->default_domain) {
  2643. ret = -EPROBE_DEFER;
  2644. goto unlock_out;
  2645. }
  2646. if (group->owner_cnt) {
  2647. if (group->domain != group->default_domain || group->owner ||
  2648. !xa_empty(&group->pasid_array)) {
  2649. ret = -EBUSY;
  2650. goto unlock_out;
  2651. }
  2652. }
  2653. group->owner_cnt++;
  2654. unlock_out:
  2655. mutex_unlock(&group->mutex);
  2656. return ret;
  2657. }
  2658. /**
  2659. * iommu_device_unuse_default_domain() - Device driver stops handling device
  2660. * DMA through the kernel DMA API.
  2661. * @dev: The device.
  2662. *
  2663. * The device driver doesn't want to do DMA through kernel DMA API anymore.
  2664. * It must be called after iommu_device_use_default_domain().
  2665. */
  2666. void iommu_device_unuse_default_domain(struct device *dev)
  2667. {
  2668. /* Caller is the driver core during the post-probe path */
  2669. struct iommu_group *group = dev->iommu_group;
  2670. if (!group)
  2671. return;
  2672. mutex_lock(&group->mutex);
  2673. if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
  2674. group->owner_cnt--;
  2675. mutex_unlock(&group->mutex);
  2676. }
  2677. static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
  2678. {
  2679. struct iommu_domain *domain;
  2680. if (group->blocking_domain)
  2681. return 0;
  2682. domain = __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
  2683. if (IS_ERR(domain)) {
  2684. /*
  2685. * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
  2686. * create an empty domain instead.
  2687. */
  2688. domain = __iommu_group_domain_alloc(group,
  2689. IOMMU_DOMAIN_UNMANAGED);
  2690. if (IS_ERR(domain))
  2691. return PTR_ERR(domain);
  2692. }
  2693. group->blocking_domain = domain;
  2694. return 0;
  2695. }
  2696. static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
  2697. {
  2698. int ret;
  2699. if ((group->domain && group->domain != group->default_domain) ||
  2700. !xa_empty(&group->pasid_array))
  2701. return -EBUSY;
  2702. ret = __iommu_group_alloc_blocking_domain(group);
  2703. if (ret)
  2704. return ret;
  2705. ret = __iommu_group_set_domain(group, group->blocking_domain);
  2706. if (ret)
  2707. return ret;
  2708. group->owner = owner;
  2709. group->owner_cnt++;
  2710. return 0;
  2711. }
  2712. /**
  2713. * iommu_group_claim_dma_owner() - Set DMA ownership of a group
  2714. * @group: The group.
  2715. * @owner: Caller specified pointer. Used for exclusive ownership.
  2716. *
  2717. * This is to support backward compatibility for vfio which manages the dma
  2718. * ownership in iommu_group level. New invocations on this interface should be
  2719. * prohibited. Only a single owner may exist for a group.
  2720. */
  2721. int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
  2722. {
  2723. int ret = 0;
  2724. if (WARN_ON(!owner))
  2725. return -EINVAL;
  2726. mutex_lock(&group->mutex);
  2727. if (group->owner_cnt) {
  2728. ret = -EPERM;
  2729. goto unlock_out;
  2730. }
  2731. ret = __iommu_take_dma_ownership(group, owner);
  2732. unlock_out:
  2733. mutex_unlock(&group->mutex);
  2734. return ret;
  2735. }
  2736. EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
  2737. /**
  2738. * iommu_device_claim_dma_owner() - Set DMA ownership of a device
  2739. * @dev: The device.
  2740. * @owner: Caller specified pointer. Used for exclusive ownership.
  2741. *
  2742. * Claim the DMA ownership of a device. Multiple devices in the same group may
  2743. * concurrently claim ownership if they present the same owner value. Returns 0
  2744. * on success and error code on failure
  2745. */
  2746. int iommu_device_claim_dma_owner(struct device *dev, void *owner)
  2747. {
  2748. /* Caller must be a probed driver on dev */
  2749. struct iommu_group *group = dev->iommu_group;
  2750. int ret = 0;
  2751. if (WARN_ON(!owner))
  2752. return -EINVAL;
  2753. if (!group)
  2754. return -ENODEV;
  2755. mutex_lock(&group->mutex);
  2756. if (group->owner_cnt) {
  2757. if (group->owner != owner) {
  2758. ret = -EPERM;
  2759. goto unlock_out;
  2760. }
  2761. group->owner_cnt++;
  2762. goto unlock_out;
  2763. }
  2764. ret = __iommu_take_dma_ownership(group, owner);
  2765. unlock_out:
  2766. mutex_unlock(&group->mutex);
  2767. return ret;
  2768. }
  2769. EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
  2770. static void __iommu_release_dma_ownership(struct iommu_group *group)
  2771. {
  2772. if (WARN_ON(!group->owner_cnt || !group->owner ||
  2773. !xa_empty(&group->pasid_array)))
  2774. return;
  2775. group->owner_cnt = 0;
  2776. group->owner = NULL;
  2777. __iommu_group_set_domain_nofail(group, group->default_domain);
  2778. }
  2779. /**
  2780. * iommu_group_release_dma_owner() - Release DMA ownership of a group
  2781. * @group: The group
  2782. *
  2783. * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
  2784. */
  2785. void iommu_group_release_dma_owner(struct iommu_group *group)
  2786. {
  2787. mutex_lock(&group->mutex);
  2788. __iommu_release_dma_ownership(group);
  2789. mutex_unlock(&group->mutex);
  2790. }
  2791. EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
  2792. /**
  2793. * iommu_device_release_dma_owner() - Release DMA ownership of a device
  2794. * @dev: The device.
  2795. *
  2796. * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
  2797. */
  2798. void iommu_device_release_dma_owner(struct device *dev)
  2799. {
  2800. /* Caller must be a probed driver on dev */
  2801. struct iommu_group *group = dev->iommu_group;
  2802. mutex_lock(&group->mutex);
  2803. if (group->owner_cnt > 1)
  2804. group->owner_cnt--;
  2805. else
  2806. __iommu_release_dma_ownership(group);
  2807. mutex_unlock(&group->mutex);
  2808. }
  2809. EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
  2810. /**
  2811. * iommu_group_dma_owner_claimed() - Query group dma ownership status
  2812. * @group: The group.
  2813. *
  2814. * This provides status query on a given group. It is racy and only for
  2815. * non-binding status reporting.
  2816. */
  2817. bool iommu_group_dma_owner_claimed(struct iommu_group *group)
  2818. {
  2819. unsigned int user;
  2820. mutex_lock(&group->mutex);
  2821. user = group->owner_cnt;
  2822. mutex_unlock(&group->mutex);
  2823. return user;
  2824. }
  2825. EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
  2826. static int __iommu_set_group_pasid(struct iommu_domain *domain,
  2827. struct iommu_group *group, ioasid_t pasid)
  2828. {
  2829. struct group_device *device, *last_gdev;
  2830. int ret;
  2831. for_each_group_device(group, device) {
  2832. ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
  2833. if (ret)
  2834. goto err_revert;
  2835. }
  2836. return 0;
  2837. err_revert:
  2838. last_gdev = device;
  2839. for_each_group_device(group, device) {
  2840. const struct iommu_ops *ops = dev_iommu_ops(device->dev);
  2841. if (device == last_gdev)
  2842. break;
  2843. ops->remove_dev_pasid(device->dev, pasid, domain);
  2844. }
  2845. return ret;
  2846. }
  2847. static void __iommu_remove_group_pasid(struct iommu_group *group,
  2848. ioasid_t pasid,
  2849. struct iommu_domain *domain)
  2850. {
  2851. struct group_device *device;
  2852. const struct iommu_ops *ops;
  2853. for_each_group_device(group, device) {
  2854. ops = dev_iommu_ops(device->dev);
  2855. ops->remove_dev_pasid(device->dev, pasid, domain);
  2856. }
  2857. }
  2858. /*
  2859. * iommu_attach_device_pasid() - Attach a domain to pasid of device
  2860. * @domain: the iommu domain.
  2861. * @dev: the attached device.
  2862. * @pasid: the pasid of the device.
  2863. * @handle: the attach handle.
  2864. *
  2865. * Return: 0 on success, or an error.
  2866. */
  2867. int iommu_attach_device_pasid(struct iommu_domain *domain,
  2868. struct device *dev, ioasid_t pasid,
  2869. struct iommu_attach_handle *handle)
  2870. {
  2871. /* Caller must be a probed driver on dev */
  2872. struct iommu_group *group = dev->iommu_group;
  2873. struct group_device *device;
  2874. int ret;
  2875. if (!domain->ops->set_dev_pasid)
  2876. return -EOPNOTSUPP;
  2877. if (!group)
  2878. return -ENODEV;
  2879. if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner ||
  2880. pasid == IOMMU_NO_PASID)
  2881. return -EINVAL;
  2882. mutex_lock(&group->mutex);
  2883. for_each_group_device(group, device) {
  2884. if (pasid >= device->dev->iommu->max_pasids) {
  2885. ret = -EINVAL;
  2886. goto out_unlock;
  2887. }
  2888. }
  2889. if (handle)
  2890. handle->domain = domain;
  2891. ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL);
  2892. if (ret)
  2893. goto out_unlock;
  2894. ret = __iommu_set_group_pasid(domain, group, pasid);
  2895. if (ret)
  2896. xa_erase(&group->pasid_array, pasid);
  2897. out_unlock:
  2898. mutex_unlock(&group->mutex);
  2899. return ret;
  2900. }
  2901. EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
  2902. /*
  2903. * iommu_detach_device_pasid() - Detach the domain from pasid of device
  2904. * @domain: the iommu domain.
  2905. * @dev: the attached device.
  2906. * @pasid: the pasid of the device.
  2907. *
  2908. * The @domain must have been attached to @pasid of the @dev with
  2909. * iommu_attach_device_pasid().
  2910. */
  2911. void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
  2912. ioasid_t pasid)
  2913. {
  2914. /* Caller must be a probed driver on dev */
  2915. struct iommu_group *group = dev->iommu_group;
  2916. mutex_lock(&group->mutex);
  2917. __iommu_remove_group_pasid(group, pasid, domain);
  2918. xa_erase(&group->pasid_array, pasid);
  2919. mutex_unlock(&group->mutex);
  2920. }
  2921. EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
  2922. ioasid_t iommu_alloc_global_pasid(struct device *dev)
  2923. {
  2924. int ret;
  2925. /* max_pasids == 0 means that the device does not support PASID */
  2926. if (!dev->iommu->max_pasids)
  2927. return IOMMU_PASID_INVALID;
  2928. /*
  2929. * max_pasids is set up by vendor driver based on number of PASID bits
  2930. * supported but the IDA allocation is inclusive.
  2931. */
  2932. ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
  2933. dev->iommu->max_pasids - 1, GFP_KERNEL);
  2934. return ret < 0 ? IOMMU_PASID_INVALID : ret;
  2935. }
  2936. EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
  2937. void iommu_free_global_pasid(ioasid_t pasid)
  2938. {
  2939. if (WARN_ON(pasid == IOMMU_PASID_INVALID))
  2940. return;
  2941. ida_free(&iommu_global_pasid_ida, pasid);
  2942. }
  2943. EXPORT_SYMBOL_GPL(iommu_free_global_pasid);
  2944. /**
  2945. * iommu_attach_handle_get - Return the attach handle
  2946. * @group: the iommu group that domain was attached to
  2947. * @pasid: the pasid within the group
  2948. * @type: matched domain type, 0 for any match
  2949. *
  2950. * Return handle or ERR_PTR(-ENOENT) on none, ERR_PTR(-EBUSY) on mismatch.
  2951. *
  2952. * Return the attach handle to the caller. The life cycle of an iommu attach
  2953. * handle is from the time when the domain is attached to the time when the
  2954. * domain is detached. Callers are required to synchronize the call of
  2955. * iommu_attach_handle_get() with domain attachment and detachment. The attach
  2956. * handle can only be used during its life cycle.
  2957. */
  2958. struct iommu_attach_handle *
  2959. iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type)
  2960. {
  2961. struct iommu_attach_handle *handle;
  2962. xa_lock(&group->pasid_array);
  2963. handle = xa_load(&group->pasid_array, pasid);
  2964. if (!handle)
  2965. handle = ERR_PTR(-ENOENT);
  2966. else if (type && handle->domain->type != type)
  2967. handle = ERR_PTR(-EBUSY);
  2968. xa_unlock(&group->pasid_array);
  2969. return handle;
  2970. }
  2971. EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL);
  2972. /**
  2973. * iommu_attach_group_handle - Attach an IOMMU domain to an IOMMU group
  2974. * @domain: IOMMU domain to attach
  2975. * @group: IOMMU group that will be attached
  2976. * @handle: attach handle
  2977. *
  2978. * Returns 0 on success and error code on failure.
  2979. *
  2980. * This is a variant of iommu_attach_group(). It allows the caller to provide
  2981. * an attach handle and use it when the domain is attached. This is currently
  2982. * used by IOMMUFD to deliver the I/O page faults.
  2983. */
  2984. int iommu_attach_group_handle(struct iommu_domain *domain,
  2985. struct iommu_group *group,
  2986. struct iommu_attach_handle *handle)
  2987. {
  2988. int ret;
  2989. if (handle)
  2990. handle->domain = domain;
  2991. mutex_lock(&group->mutex);
  2992. ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
  2993. if (ret)
  2994. goto err_unlock;
  2995. ret = __iommu_attach_group(domain, group);
  2996. if (ret)
  2997. goto err_erase;
  2998. mutex_unlock(&group->mutex);
  2999. return 0;
  3000. err_erase:
  3001. xa_erase(&group->pasid_array, IOMMU_NO_PASID);
  3002. err_unlock:
  3003. mutex_unlock(&group->mutex);
  3004. return ret;
  3005. }
  3006. EXPORT_SYMBOL_NS_GPL(iommu_attach_group_handle, IOMMUFD_INTERNAL);
  3007. /**
  3008. * iommu_detach_group_handle - Detach an IOMMU domain from an IOMMU group
  3009. * @domain: IOMMU domain to attach
  3010. * @group: IOMMU group that will be attached
  3011. *
  3012. * Detach the specified IOMMU domain from the specified IOMMU group.
  3013. * It must be used in conjunction with iommu_attach_group_handle().
  3014. */
  3015. void iommu_detach_group_handle(struct iommu_domain *domain,
  3016. struct iommu_group *group)
  3017. {
  3018. mutex_lock(&group->mutex);
  3019. __iommu_group_set_core_domain(group);
  3020. xa_erase(&group->pasid_array, IOMMU_NO_PASID);
  3021. mutex_unlock(&group->mutex);
  3022. }
  3023. EXPORT_SYMBOL_NS_GPL(iommu_detach_group_handle, IOMMUFD_INTERNAL);
  3024. /**
  3025. * iommu_replace_group_handle - replace the domain that a group is attached to
  3026. * @group: IOMMU group that will be attached to the new domain
  3027. * @new_domain: new IOMMU domain to replace with
  3028. * @handle: attach handle
  3029. *
  3030. * This is a variant of iommu_group_replace_domain(). It allows the caller to
  3031. * provide an attach handle for the new domain and use it when the domain is
  3032. * attached.
  3033. */
  3034. int iommu_replace_group_handle(struct iommu_group *group,
  3035. struct iommu_domain *new_domain,
  3036. struct iommu_attach_handle *handle)
  3037. {
  3038. void *curr;
  3039. int ret;
  3040. if (!new_domain)
  3041. return -EINVAL;
  3042. mutex_lock(&group->mutex);
  3043. if (handle) {
  3044. ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL);
  3045. if (ret)
  3046. goto err_unlock;
  3047. handle->domain = new_domain;
  3048. }
  3049. ret = __iommu_group_set_domain(group, new_domain);
  3050. if (ret)
  3051. goto err_release;
  3052. curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
  3053. WARN_ON(xa_is_err(curr));
  3054. mutex_unlock(&group->mutex);
  3055. return 0;
  3056. err_release:
  3057. xa_release(&group->pasid_array, IOMMU_NO_PASID);
  3058. err_unlock:
  3059. mutex_unlock(&group->mutex);
  3060. return ret;
  3061. }
  3062. EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, IOMMUFD_INTERNAL);