arm-smmu.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. /*
  2. * IOMMU API for ARM architected SMMU implementations.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Copyright (C) 2013 ARM Limited
  18. *
  19. * Author: Will Deacon <will.deacon@arm.com>
  20. *
  21. * This driver currently supports:
  22. * - SMMUv1 and v2 implementations
  23. * - Stream-matching and stream-indexing
  24. * - v7/v8 long-descriptor format
  25. * - Non-secure access to the SMMU
  26. * - Context fault reporting
  27. */
  28. #define pr_fmt(fmt) "arm-smmu: " fmt
  29. #include <linux/delay.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/err.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/iommu.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/pci.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/amba/bus.h>
  43. #include "io-pgtable.h"
  44. /* Maximum number of stream IDs assigned to a single device */
  45. #define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
  46. /* Maximum number of context banks per SMMU */
  47. #define ARM_SMMU_MAX_CBS 128
  48. /* Maximum number of mapping groups per SMMU */
  49. #define ARM_SMMU_MAX_SMRS 128
  50. /* SMMU global address space */
  51. #define ARM_SMMU_GR0(smmu) ((smmu)->base)
  52. #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
  53. /*
  54. * SMMU global address space with conditional offset to access secure
  55. * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
  56. * nsGFSYNR0: 0x450)
  57. */
  58. #define ARM_SMMU_GR0_NS(smmu) \
  59. ((smmu)->base + \
  60. ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
  61. ? 0x400 : 0))
  62. /* Configuration registers */
  63. #define ARM_SMMU_GR0_sCR0 0x0
  64. #define sCR0_CLIENTPD (1 << 0)
  65. #define sCR0_GFRE (1 << 1)
  66. #define sCR0_GFIE (1 << 2)
  67. #define sCR0_GCFGFRE (1 << 4)
  68. #define sCR0_GCFGFIE (1 << 5)
  69. #define sCR0_USFCFG (1 << 10)
  70. #define sCR0_VMIDPNE (1 << 11)
  71. #define sCR0_PTM (1 << 12)
  72. #define sCR0_FB (1 << 13)
  73. #define sCR0_BSU_SHIFT 14
  74. #define sCR0_BSU_MASK 0x3
  75. /* Identification registers */
  76. #define ARM_SMMU_GR0_ID0 0x20
  77. #define ARM_SMMU_GR0_ID1 0x24
  78. #define ARM_SMMU_GR0_ID2 0x28
  79. #define ARM_SMMU_GR0_ID3 0x2c
  80. #define ARM_SMMU_GR0_ID4 0x30
  81. #define ARM_SMMU_GR0_ID5 0x34
  82. #define ARM_SMMU_GR0_ID6 0x38
  83. #define ARM_SMMU_GR0_ID7 0x3c
  84. #define ARM_SMMU_GR0_sGFSR 0x48
  85. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  86. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  87. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  88. #define ID0_S1TS (1 << 30)
  89. #define ID0_S2TS (1 << 29)
  90. #define ID0_NTS (1 << 28)
  91. #define ID0_SMS (1 << 27)
  92. #define ID0_ATOSNS (1 << 26)
  93. #define ID0_CTTW (1 << 14)
  94. #define ID0_NUMIRPT_SHIFT 16
  95. #define ID0_NUMIRPT_MASK 0xff
  96. #define ID0_NUMSIDB_SHIFT 9
  97. #define ID0_NUMSIDB_MASK 0xf
  98. #define ID0_NUMSMRG_SHIFT 0
  99. #define ID0_NUMSMRG_MASK 0xff
  100. #define ID1_PAGESIZE (1 << 31)
  101. #define ID1_NUMPAGENDXB_SHIFT 28
  102. #define ID1_NUMPAGENDXB_MASK 7
  103. #define ID1_NUMS2CB_SHIFT 16
  104. #define ID1_NUMS2CB_MASK 0xff
  105. #define ID1_NUMCB_SHIFT 0
  106. #define ID1_NUMCB_MASK 0xff
  107. #define ID2_OAS_SHIFT 4
  108. #define ID2_OAS_MASK 0xf
  109. #define ID2_IAS_SHIFT 0
  110. #define ID2_IAS_MASK 0xf
  111. #define ID2_UBS_SHIFT 8
  112. #define ID2_UBS_MASK 0xf
  113. #define ID2_PTFS_4K (1 << 12)
  114. #define ID2_PTFS_16K (1 << 13)
  115. #define ID2_PTFS_64K (1 << 14)
  116. /* Global TLB invalidation */
  117. #define ARM_SMMU_GR0_TLBIVMID 0x64
  118. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  119. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  120. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  121. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  122. #define sTLBGSTATUS_GSACTIVE (1 << 0)
  123. #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
  124. /* Stream mapping registers */
  125. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  126. #define SMR_VALID (1 << 31)
  127. #define SMR_MASK_SHIFT 16
  128. #define SMR_MASK_MASK 0x7fff
  129. #define SMR_ID_SHIFT 0
  130. #define SMR_ID_MASK 0x7fff
  131. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  132. #define S2CR_CBNDX_SHIFT 0
  133. #define S2CR_CBNDX_MASK 0xff
  134. #define S2CR_TYPE_SHIFT 16
  135. #define S2CR_TYPE_MASK 0x3
  136. #define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
  137. #define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
  138. #define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
  139. /* Context bank attribute registers */
  140. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  141. #define CBAR_VMID_SHIFT 0
  142. #define CBAR_VMID_MASK 0xff
  143. #define CBAR_S1_BPSHCFG_SHIFT 8
  144. #define CBAR_S1_BPSHCFG_MASK 3
  145. #define CBAR_S1_BPSHCFG_NSH 3
  146. #define CBAR_S1_MEMATTR_SHIFT 12
  147. #define CBAR_S1_MEMATTR_MASK 0xf
  148. #define CBAR_S1_MEMATTR_WB 0xf
  149. #define CBAR_TYPE_SHIFT 16
  150. #define CBAR_TYPE_MASK 0x3
  151. #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
  152. #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
  153. #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
  154. #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
  155. #define CBAR_IRPTNDX_SHIFT 24
  156. #define CBAR_IRPTNDX_MASK 0xff
  157. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  158. #define CBA2R_RW64_32BIT (0 << 0)
  159. #define CBA2R_RW64_64BIT (1 << 0)
  160. /* Translation context bank */
  161. #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
  162. #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
  163. #define ARM_SMMU_CB_SCTLR 0x0
  164. #define ARM_SMMU_CB_RESUME 0x8
  165. #define ARM_SMMU_CB_TTBCR2 0x10
  166. #define ARM_SMMU_CB_TTBR0_LO 0x20
  167. #define ARM_SMMU_CB_TTBR0_HI 0x24
  168. #define ARM_SMMU_CB_TTBR1_LO 0x28
  169. #define ARM_SMMU_CB_TTBR1_HI 0x2c
  170. #define ARM_SMMU_CB_TTBCR 0x30
  171. #define ARM_SMMU_CB_S1_MAIR0 0x38
  172. #define ARM_SMMU_CB_S1_MAIR1 0x3c
  173. #define ARM_SMMU_CB_PAR_LO 0x50
  174. #define ARM_SMMU_CB_PAR_HI 0x54
  175. #define ARM_SMMU_CB_FSR 0x58
  176. #define ARM_SMMU_CB_FAR_LO 0x60
  177. #define ARM_SMMU_CB_FAR_HI 0x64
  178. #define ARM_SMMU_CB_FSYNR0 0x68
  179. #define ARM_SMMU_CB_S1_TLBIVA 0x600
  180. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  181. #define ARM_SMMU_CB_S1_TLBIVAL 0x620
  182. #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
  183. #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
  184. #define ARM_SMMU_CB_ATS1PR 0x800
  185. #define ARM_SMMU_CB_ATSR 0x8f0
  186. #define SCTLR_S1_ASIDPNE (1 << 12)
  187. #define SCTLR_CFCFG (1 << 7)
  188. #define SCTLR_CFIE (1 << 6)
  189. #define SCTLR_CFRE (1 << 5)
  190. #define SCTLR_E (1 << 4)
  191. #define SCTLR_AFE (1 << 2)
  192. #define SCTLR_TRE (1 << 1)
  193. #define SCTLR_M (1 << 0)
  194. #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
  195. #define CB_PAR_F (1 << 0)
  196. #define ATSR_ACTIVE (1 << 0)
  197. #define RESUME_RETRY (0 << 0)
  198. #define RESUME_TERMINATE (1 << 0)
  199. #define TTBCR2_SEP_SHIFT 15
  200. #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
  201. #define TTBRn_HI_ASID_SHIFT 16
  202. #define FSR_MULTI (1 << 31)
  203. #define FSR_SS (1 << 30)
  204. #define FSR_UUT (1 << 8)
  205. #define FSR_ASF (1 << 7)
  206. #define FSR_TLBLKF (1 << 6)
  207. #define FSR_TLBMCF (1 << 5)
  208. #define FSR_EF (1 << 4)
  209. #define FSR_PF (1 << 3)
  210. #define FSR_AFF (1 << 2)
  211. #define FSR_TF (1 << 1)
  212. #define FSR_IGN (FSR_AFF | FSR_ASF | \
  213. FSR_TLBMCF | FSR_TLBLKF)
  214. #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
  215. FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
  216. #define FSYNR0_WNR (1 << 4)
  217. static int force_stage;
  218. module_param_named(force_stage, force_stage, int, S_IRUGO);
  219. MODULE_PARM_DESC(force_stage,
  220. "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
  221. enum arm_smmu_arch_version {
  222. ARM_SMMU_V1 = 1,
  223. ARM_SMMU_V2,
  224. };
  225. struct arm_smmu_smr {
  226. u8 idx;
  227. u16 mask;
  228. u16 id;
  229. };
  230. struct arm_smmu_master_cfg {
  231. int num_streamids;
  232. u16 streamids[MAX_MASTER_STREAMIDS];
  233. struct arm_smmu_smr *smrs;
  234. };
  235. struct arm_smmu_master {
  236. struct device_node *of_node;
  237. struct rb_node node;
  238. struct arm_smmu_master_cfg cfg;
  239. };
  240. struct arm_smmu_device {
  241. struct device *dev;
  242. void __iomem *base;
  243. unsigned long size;
  244. unsigned long pgshift;
  245. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  246. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  247. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  248. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  249. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  250. #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
  251. u32 features;
  252. #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
  253. u32 options;
  254. enum arm_smmu_arch_version version;
  255. u32 num_context_banks;
  256. u32 num_s2_context_banks;
  257. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  258. atomic_t irptndx;
  259. u32 num_mapping_groups;
  260. DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
  261. unsigned long va_size;
  262. unsigned long ipa_size;
  263. unsigned long pa_size;
  264. u32 num_global_irqs;
  265. u32 num_context_irqs;
  266. unsigned int *irqs;
  267. struct list_head list;
  268. struct rb_root masters;
  269. };
  270. struct arm_smmu_cfg {
  271. u8 cbndx;
  272. u8 irptndx;
  273. u32 cbar;
  274. };
  275. #define INVALID_IRPTNDX 0xff
  276. #define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
  277. #define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
  278. enum arm_smmu_domain_stage {
  279. ARM_SMMU_DOMAIN_S1 = 0,
  280. ARM_SMMU_DOMAIN_S2,
  281. ARM_SMMU_DOMAIN_NESTED,
  282. };
  283. struct arm_smmu_domain {
  284. struct arm_smmu_device *smmu;
  285. struct io_pgtable_ops *pgtbl_ops;
  286. spinlock_t pgtbl_lock;
  287. struct arm_smmu_cfg cfg;
  288. enum arm_smmu_domain_stage stage;
  289. struct mutex init_mutex; /* Protects smmu pointer */
  290. struct iommu_domain domain;
  291. };
  292. static struct iommu_ops arm_smmu_ops;
  293. static DEFINE_SPINLOCK(arm_smmu_devices_lock);
  294. static LIST_HEAD(arm_smmu_devices);
  295. struct arm_smmu_option_prop {
  296. u32 opt;
  297. const char *prop;
  298. };
  299. static struct arm_smmu_option_prop arm_smmu_options[] = {
  300. { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
  301. { 0, NULL},
  302. };
  303. static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
  304. {
  305. return container_of(dom, struct arm_smmu_domain, domain);
  306. }
  307. static void parse_driver_options(struct arm_smmu_device *smmu)
  308. {
  309. int i = 0;
  310. do {
  311. if (of_property_read_bool(smmu->dev->of_node,
  312. arm_smmu_options[i].prop)) {
  313. smmu->options |= arm_smmu_options[i].opt;
  314. dev_notice(smmu->dev, "option %s\n",
  315. arm_smmu_options[i].prop);
  316. }
  317. } while (arm_smmu_options[++i].opt);
  318. }
  319. static struct device_node *dev_get_dev_node(struct device *dev)
  320. {
  321. if (dev_is_pci(dev)) {
  322. struct pci_bus *bus = to_pci_dev(dev)->bus;
  323. while (!pci_is_root_bus(bus))
  324. bus = bus->parent;
  325. return bus->bridge->parent->of_node;
  326. }
  327. return dev->of_node;
  328. }
  329. static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
  330. struct device_node *dev_node)
  331. {
  332. struct rb_node *node = smmu->masters.rb_node;
  333. while (node) {
  334. struct arm_smmu_master *master;
  335. master = container_of(node, struct arm_smmu_master, node);
  336. if (dev_node < master->of_node)
  337. node = node->rb_left;
  338. else if (dev_node > master->of_node)
  339. node = node->rb_right;
  340. else
  341. return master;
  342. }
  343. return NULL;
  344. }
  345. static struct arm_smmu_master_cfg *
  346. find_smmu_master_cfg(struct device *dev)
  347. {
  348. struct arm_smmu_master_cfg *cfg = NULL;
  349. struct iommu_group *group = iommu_group_get(dev);
  350. if (group) {
  351. cfg = iommu_group_get_iommudata(group);
  352. iommu_group_put(group);
  353. }
  354. return cfg;
  355. }
  356. static int insert_smmu_master(struct arm_smmu_device *smmu,
  357. struct arm_smmu_master *master)
  358. {
  359. struct rb_node **new, *parent;
  360. new = &smmu->masters.rb_node;
  361. parent = NULL;
  362. while (*new) {
  363. struct arm_smmu_master *this
  364. = container_of(*new, struct arm_smmu_master, node);
  365. parent = *new;
  366. if (master->of_node < this->of_node)
  367. new = &((*new)->rb_left);
  368. else if (master->of_node > this->of_node)
  369. new = &((*new)->rb_right);
  370. else
  371. return -EEXIST;
  372. }
  373. rb_link_node(&master->node, parent, new);
  374. rb_insert_color(&master->node, &smmu->masters);
  375. return 0;
  376. }
  377. static int register_smmu_master(struct arm_smmu_device *smmu,
  378. struct device *dev,
  379. struct of_phandle_args *masterspec)
  380. {
  381. int i;
  382. struct arm_smmu_master *master;
  383. master = find_smmu_master(smmu, masterspec->np);
  384. if (master) {
  385. dev_err(dev,
  386. "rejecting multiple registrations for master device %s\n",
  387. masterspec->np->name);
  388. return -EBUSY;
  389. }
  390. if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
  391. dev_err(dev,
  392. "reached maximum number (%d) of stream IDs for master device %s\n",
  393. MAX_MASTER_STREAMIDS, masterspec->np->name);
  394. return -ENOSPC;
  395. }
  396. master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
  397. if (!master)
  398. return -ENOMEM;
  399. master->of_node = masterspec->np;
  400. master->cfg.num_streamids = masterspec->args_count;
  401. for (i = 0; i < master->cfg.num_streamids; ++i) {
  402. u16 streamid = masterspec->args[i];
  403. if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
  404. (streamid >= smmu->num_mapping_groups)) {
  405. dev_err(dev,
  406. "stream ID for master device %s greater than maximum allowed (%d)\n",
  407. masterspec->np->name, smmu->num_mapping_groups);
  408. return -ERANGE;
  409. }
  410. master->cfg.streamids[i] = streamid;
  411. }
  412. return insert_smmu_master(smmu, master);
  413. }
  414. static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
  415. {
  416. struct arm_smmu_device *smmu;
  417. struct arm_smmu_master *master = NULL;
  418. struct device_node *dev_node = dev_get_dev_node(dev);
  419. spin_lock(&arm_smmu_devices_lock);
  420. list_for_each_entry(smmu, &arm_smmu_devices, list) {
  421. master = find_smmu_master(smmu, dev_node);
  422. if (master)
  423. break;
  424. }
  425. spin_unlock(&arm_smmu_devices_lock);
  426. return master ? smmu : NULL;
  427. }
  428. static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  429. {
  430. int idx;
  431. do {
  432. idx = find_next_zero_bit(map, end, start);
  433. if (idx == end)
  434. return -ENOSPC;
  435. } while (test_and_set_bit(idx, map));
  436. return idx;
  437. }
  438. static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
  439. {
  440. clear_bit(idx, map);
  441. }
  442. /* Wait for any pending TLB invalidations to complete */
  443. static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  444. {
  445. int count = 0;
  446. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  447. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
  448. while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
  449. & sTLBGSTATUS_GSACTIVE) {
  450. cpu_relax();
  451. if (++count == TLB_LOOP_TIMEOUT) {
  452. dev_err_ratelimited(smmu->dev,
  453. "TLB sync timed out -- SMMU may be deadlocked\n");
  454. return;
  455. }
  456. udelay(1);
  457. }
  458. }
  459. static void arm_smmu_tlb_sync(void *cookie)
  460. {
  461. struct arm_smmu_domain *smmu_domain = cookie;
  462. __arm_smmu_tlb_sync(smmu_domain->smmu);
  463. }
  464. static void arm_smmu_tlb_inv_context(void *cookie)
  465. {
  466. struct arm_smmu_domain *smmu_domain = cookie;
  467. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  468. struct arm_smmu_device *smmu = smmu_domain->smmu;
  469. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  470. void __iomem *base;
  471. if (stage1) {
  472. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  473. writel_relaxed(ARM_SMMU_CB_ASID(cfg),
  474. base + ARM_SMMU_CB_S1_TLBIASID);
  475. } else {
  476. base = ARM_SMMU_GR0(smmu);
  477. writel_relaxed(ARM_SMMU_CB_VMID(cfg),
  478. base + ARM_SMMU_GR0_TLBIVMID);
  479. }
  480. __arm_smmu_tlb_sync(smmu);
  481. }
  482. static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  483. bool leaf, void *cookie)
  484. {
  485. struct arm_smmu_domain *smmu_domain = cookie;
  486. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  487. struct arm_smmu_device *smmu = smmu_domain->smmu;
  488. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  489. void __iomem *reg;
  490. if (stage1) {
  491. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  492. reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
  493. if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
  494. iova &= ~12UL;
  495. iova |= ARM_SMMU_CB_ASID(cfg);
  496. writel_relaxed(iova, reg);
  497. #ifdef CONFIG_64BIT
  498. } else {
  499. iova >>= 12;
  500. iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
  501. writeq_relaxed(iova, reg);
  502. #endif
  503. }
  504. #ifdef CONFIG_64BIT
  505. } else if (smmu->version == ARM_SMMU_V2) {
  506. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  507. reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
  508. ARM_SMMU_CB_S2_TLBIIPAS2;
  509. writeq_relaxed(iova >> 12, reg);
  510. #endif
  511. } else {
  512. reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
  513. writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
  514. }
  515. }
  516. static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)
  517. {
  518. struct arm_smmu_domain *smmu_domain = cookie;
  519. struct arm_smmu_device *smmu = smmu_domain->smmu;
  520. unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
  521. /* Ensure new page tables are visible to the hardware walker */
  522. if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
  523. dsb(ishst);
  524. } else {
  525. /*
  526. * If the SMMU can't walk tables in the CPU caches, treat them
  527. * like non-coherent DMA since we need to flush the new entries
  528. * all the way out to memory. There's no possibility of
  529. * recursion here as the SMMU table walker will not be wired
  530. * through another SMMU.
  531. */
  532. dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
  533. DMA_TO_DEVICE);
  534. }
  535. }
  536. static struct iommu_gather_ops arm_smmu_gather_ops = {
  537. .tlb_flush_all = arm_smmu_tlb_inv_context,
  538. .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
  539. .tlb_sync = arm_smmu_tlb_sync,
  540. .flush_pgtable = arm_smmu_flush_pgtable,
  541. };
  542. static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  543. {
  544. int flags, ret;
  545. u32 fsr, far, fsynr, resume;
  546. unsigned long iova;
  547. struct iommu_domain *domain = dev;
  548. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  549. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  550. struct arm_smmu_device *smmu = smmu_domain->smmu;
  551. void __iomem *cb_base;
  552. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  553. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  554. if (!(fsr & FSR_FAULT))
  555. return IRQ_NONE;
  556. if (fsr & FSR_IGN)
  557. dev_err_ratelimited(smmu->dev,
  558. "Unexpected context fault (fsr 0x%x)\n",
  559. fsr);
  560. fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
  561. flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  562. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
  563. iova = far;
  564. #ifdef CONFIG_64BIT
  565. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
  566. iova |= ((unsigned long)far << 32);
  567. #endif
  568. if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
  569. ret = IRQ_HANDLED;
  570. resume = RESUME_RETRY;
  571. } else {
  572. dev_err_ratelimited(smmu->dev,
  573. "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
  574. iova, fsynr, cfg->cbndx);
  575. ret = IRQ_NONE;
  576. resume = RESUME_TERMINATE;
  577. }
  578. /* Clear the faulting FSR */
  579. writel(fsr, cb_base + ARM_SMMU_CB_FSR);
  580. /* Retry or terminate any stalled transactions */
  581. if (fsr & FSR_SS)
  582. writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
  583. return ret;
  584. }
  585. static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
  586. {
  587. u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
  588. struct arm_smmu_device *smmu = dev;
  589. void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
  590. gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  591. gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
  592. gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
  593. gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
  594. if (!gfsr)
  595. return IRQ_NONE;
  596. dev_err_ratelimited(smmu->dev,
  597. "Unexpected global fault, this could be serious\n");
  598. dev_err_ratelimited(smmu->dev,
  599. "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
  600. gfsr, gfsynr0, gfsynr1, gfsynr2);
  601. writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
  602. return IRQ_HANDLED;
  603. }
  604. static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
  605. struct io_pgtable_cfg *pgtbl_cfg)
  606. {
  607. u32 reg;
  608. bool stage1;
  609. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  610. struct arm_smmu_device *smmu = smmu_domain->smmu;
  611. void __iomem *cb_base, *gr0_base, *gr1_base;
  612. gr0_base = ARM_SMMU_GR0(smmu);
  613. gr1_base = ARM_SMMU_GR1(smmu);
  614. stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  615. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  616. if (smmu->version > ARM_SMMU_V1) {
  617. /*
  618. * CBA2R.
  619. * *Must* be initialised before CBAR thanks to VMID16
  620. * architectural oversight affected some implementations.
  621. */
  622. #ifdef CONFIG_64BIT
  623. reg = CBA2R_RW64_64BIT;
  624. #else
  625. reg = CBA2R_RW64_32BIT;
  626. #endif
  627. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
  628. }
  629. /* CBAR */
  630. reg = cfg->cbar;
  631. if (smmu->version == ARM_SMMU_V1)
  632. reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
  633. /*
  634. * Use the weakest shareability/memory types, so they are
  635. * overridden by the ttbcr/pte.
  636. */
  637. if (stage1) {
  638. reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
  639. (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
  640. } else {
  641. reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
  642. }
  643. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
  644. /* TTBRs */
  645. if (stage1) {
  646. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
  647. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
  648. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
  649. reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
  650. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
  651. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
  652. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
  653. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
  654. reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
  655. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
  656. } else {
  657. reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
  658. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
  659. reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
  660. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
  661. }
  662. /* TTBCR */
  663. if (stage1) {
  664. reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
  665. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  666. if (smmu->version > ARM_SMMU_V1) {
  667. reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
  668. reg |= TTBCR2_SEP_UPSTREAM;
  669. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
  670. }
  671. } else {
  672. reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
  673. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  674. }
  675. /* MAIRs (stage-1 only) */
  676. if (stage1) {
  677. reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
  678. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
  679. reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
  680. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
  681. }
  682. /* SCTLR */
  683. reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
  684. if (stage1)
  685. reg |= SCTLR_S1_ASIDPNE;
  686. #ifdef __BIG_ENDIAN
  687. reg |= SCTLR_E;
  688. #endif
  689. writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
  690. }
  691. static int arm_smmu_init_domain_context(struct iommu_domain *domain,
  692. struct arm_smmu_device *smmu)
  693. {
  694. int irq, start, ret = 0;
  695. unsigned long ias, oas;
  696. struct io_pgtable_ops *pgtbl_ops;
  697. struct io_pgtable_cfg pgtbl_cfg;
  698. enum io_pgtable_fmt fmt;
  699. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  700. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  701. mutex_lock(&smmu_domain->init_mutex);
  702. if (smmu_domain->smmu)
  703. goto out_unlock;
  704. /*
  705. * Mapping the requested stage onto what we support is surprisingly
  706. * complicated, mainly because the spec allows S1+S2 SMMUs without
  707. * support for nested translation. That means we end up with the
  708. * following table:
  709. *
  710. * Requested Supported Actual
  711. * S1 N S1
  712. * S1 S1+S2 S1
  713. * S1 S2 S2
  714. * S1 S1 S1
  715. * N N N
  716. * N S1+S2 S2
  717. * N S2 S2
  718. * N S1 S1
  719. *
  720. * Note that you can't actually request stage-2 mappings.
  721. */
  722. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
  723. smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
  724. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
  725. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  726. switch (smmu_domain->stage) {
  727. case ARM_SMMU_DOMAIN_S1:
  728. cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  729. start = smmu->num_s2_context_banks;
  730. ias = smmu->va_size;
  731. oas = smmu->ipa_size;
  732. if (IS_ENABLED(CONFIG_64BIT))
  733. fmt = ARM_64_LPAE_S1;
  734. else
  735. fmt = ARM_32_LPAE_S1;
  736. break;
  737. case ARM_SMMU_DOMAIN_NESTED:
  738. /*
  739. * We will likely want to change this if/when KVM gets
  740. * involved.
  741. */
  742. case ARM_SMMU_DOMAIN_S2:
  743. cfg->cbar = CBAR_TYPE_S2_TRANS;
  744. start = 0;
  745. ias = smmu->ipa_size;
  746. oas = smmu->pa_size;
  747. if (IS_ENABLED(CONFIG_64BIT))
  748. fmt = ARM_64_LPAE_S2;
  749. else
  750. fmt = ARM_32_LPAE_S2;
  751. break;
  752. default:
  753. ret = -EINVAL;
  754. goto out_unlock;
  755. }
  756. ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
  757. smmu->num_context_banks);
  758. if (IS_ERR_VALUE(ret))
  759. goto out_unlock;
  760. cfg->cbndx = ret;
  761. if (smmu->version == ARM_SMMU_V1) {
  762. cfg->irptndx = atomic_inc_return(&smmu->irptndx);
  763. cfg->irptndx %= smmu->num_context_irqs;
  764. } else {
  765. cfg->irptndx = cfg->cbndx;
  766. }
  767. pgtbl_cfg = (struct io_pgtable_cfg) {
  768. .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
  769. .ias = ias,
  770. .oas = oas,
  771. .tlb = &arm_smmu_gather_ops,
  772. };
  773. smmu_domain->smmu = smmu;
  774. pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
  775. if (!pgtbl_ops) {
  776. ret = -ENOMEM;
  777. goto out_clear_smmu;
  778. }
  779. /* Update our support page sizes to reflect the page table format */
  780. arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
  781. /* Initialise the context bank with our page table cfg */
  782. arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
  783. /*
  784. * Request context fault interrupt. Do this last to avoid the
  785. * handler seeing a half-initialised domain state.
  786. */
  787. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  788. ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
  789. "arm-smmu-context-fault", domain);
  790. if (IS_ERR_VALUE(ret)) {
  791. dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
  792. cfg->irptndx, irq);
  793. cfg->irptndx = INVALID_IRPTNDX;
  794. }
  795. mutex_unlock(&smmu_domain->init_mutex);
  796. /* Publish page table ops for map/unmap */
  797. smmu_domain->pgtbl_ops = pgtbl_ops;
  798. return 0;
  799. out_clear_smmu:
  800. smmu_domain->smmu = NULL;
  801. out_unlock:
  802. mutex_unlock(&smmu_domain->init_mutex);
  803. return ret;
  804. }
  805. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
  806. {
  807. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  808. struct arm_smmu_device *smmu = smmu_domain->smmu;
  809. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  810. void __iomem *cb_base;
  811. int irq;
  812. if (!smmu)
  813. return;
  814. /*
  815. * Disable the context bank and free the page tables before freeing
  816. * it.
  817. */
  818. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  819. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  820. if (cfg->irptndx != INVALID_IRPTNDX) {
  821. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  822. free_irq(irq, domain);
  823. }
  824. if (smmu_domain->pgtbl_ops)
  825. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  826. __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
  827. }
  828. static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
  829. {
  830. struct arm_smmu_domain *smmu_domain;
  831. if (type != IOMMU_DOMAIN_UNMANAGED)
  832. return NULL;
  833. /*
  834. * Allocate the domain and initialise some of its data structures.
  835. * We can't really do anything meaningful until we've added a
  836. * master.
  837. */
  838. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  839. if (!smmu_domain)
  840. return NULL;
  841. mutex_init(&smmu_domain->init_mutex);
  842. spin_lock_init(&smmu_domain->pgtbl_lock);
  843. return &smmu_domain->domain;
  844. }
  845. static void arm_smmu_domain_free(struct iommu_domain *domain)
  846. {
  847. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  848. /*
  849. * Free the domain resources. We assume that all devices have
  850. * already been detached.
  851. */
  852. arm_smmu_destroy_domain_context(domain);
  853. kfree(smmu_domain);
  854. }
  855. static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
  856. struct arm_smmu_master_cfg *cfg)
  857. {
  858. int i;
  859. struct arm_smmu_smr *smrs;
  860. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  861. if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
  862. return 0;
  863. if (cfg->smrs)
  864. return -EEXIST;
  865. smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
  866. if (!smrs) {
  867. dev_err(smmu->dev, "failed to allocate %d SMRs\n",
  868. cfg->num_streamids);
  869. return -ENOMEM;
  870. }
  871. /* Allocate the SMRs on the SMMU */
  872. for (i = 0; i < cfg->num_streamids; ++i) {
  873. int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
  874. smmu->num_mapping_groups);
  875. if (IS_ERR_VALUE(idx)) {
  876. dev_err(smmu->dev, "failed to allocate free SMR\n");
  877. goto err_free_smrs;
  878. }
  879. smrs[i] = (struct arm_smmu_smr) {
  880. .idx = idx,
  881. .mask = 0, /* We don't currently share SMRs */
  882. .id = cfg->streamids[i],
  883. };
  884. }
  885. /* It worked! Now, poke the actual hardware */
  886. for (i = 0; i < cfg->num_streamids; ++i) {
  887. u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
  888. smrs[i].mask << SMR_MASK_SHIFT;
  889. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
  890. }
  891. cfg->smrs = smrs;
  892. return 0;
  893. err_free_smrs:
  894. while (--i >= 0)
  895. __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
  896. kfree(smrs);
  897. return -ENOSPC;
  898. }
  899. static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
  900. struct arm_smmu_master_cfg *cfg)
  901. {
  902. int i;
  903. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  904. struct arm_smmu_smr *smrs = cfg->smrs;
  905. if (!smrs)
  906. return;
  907. /* Invalidate the SMRs before freeing back to the allocator */
  908. for (i = 0; i < cfg->num_streamids; ++i) {
  909. u8 idx = smrs[i].idx;
  910. writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
  911. __arm_smmu_free_bitmap(smmu->smr_map, idx);
  912. }
  913. cfg->smrs = NULL;
  914. kfree(smrs);
  915. }
  916. static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
  917. struct arm_smmu_master_cfg *cfg)
  918. {
  919. int i, ret;
  920. struct arm_smmu_device *smmu = smmu_domain->smmu;
  921. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  922. /* Devices in an IOMMU group may already be configured */
  923. ret = arm_smmu_master_configure_smrs(smmu, cfg);
  924. if (ret)
  925. return ret == -EEXIST ? 0 : ret;
  926. for (i = 0; i < cfg->num_streamids; ++i) {
  927. u32 idx, s2cr;
  928. idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
  929. s2cr = S2CR_TYPE_TRANS |
  930. (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
  931. writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
  932. }
  933. return 0;
  934. }
  935. static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
  936. struct arm_smmu_master_cfg *cfg)
  937. {
  938. int i;
  939. struct arm_smmu_device *smmu = smmu_domain->smmu;
  940. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  941. /* An IOMMU group is torn down by the first device to be removed */
  942. if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
  943. return;
  944. /*
  945. * We *must* clear the S2CR first, because freeing the SMR means
  946. * that it can be re-allocated immediately.
  947. */
  948. for (i = 0; i < cfg->num_streamids; ++i) {
  949. u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
  950. writel_relaxed(S2CR_TYPE_BYPASS,
  951. gr0_base + ARM_SMMU_GR0_S2CR(idx));
  952. }
  953. arm_smmu_master_free_smrs(smmu, cfg);
  954. }
  955. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  956. {
  957. int ret;
  958. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  959. struct arm_smmu_device *smmu;
  960. struct arm_smmu_master_cfg *cfg;
  961. smmu = find_smmu_for_device(dev);
  962. if (!smmu) {
  963. dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
  964. return -ENXIO;
  965. }
  966. if (dev->archdata.iommu) {
  967. dev_err(dev, "already attached to IOMMU domain\n");
  968. return -EEXIST;
  969. }
  970. /* Ensure that the domain is finalised */
  971. ret = arm_smmu_init_domain_context(domain, smmu);
  972. if (IS_ERR_VALUE(ret))
  973. return ret;
  974. /*
  975. * Sanity check the domain. We don't support domains across
  976. * different SMMUs.
  977. */
  978. if (smmu_domain->smmu != smmu) {
  979. dev_err(dev,
  980. "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
  981. dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
  982. return -EINVAL;
  983. }
  984. /* Looks ok, so add the device to the domain */
  985. cfg = find_smmu_master_cfg(dev);
  986. if (!cfg)
  987. return -ENODEV;
  988. ret = arm_smmu_domain_add_master(smmu_domain, cfg);
  989. if (!ret)
  990. dev->archdata.iommu = domain;
  991. return ret;
  992. }
  993. static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
  994. {
  995. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  996. struct arm_smmu_master_cfg *cfg;
  997. cfg = find_smmu_master_cfg(dev);
  998. if (!cfg)
  999. return;
  1000. dev->archdata.iommu = NULL;
  1001. arm_smmu_domain_remove_master(smmu_domain, cfg);
  1002. }
  1003. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1004. phys_addr_t paddr, size_t size, int prot)
  1005. {
  1006. int ret;
  1007. unsigned long flags;
  1008. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1009. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1010. if (!ops)
  1011. return -ENODEV;
  1012. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1013. ret = ops->map(ops, iova, paddr, size, prot);
  1014. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1015. return ret;
  1016. }
  1017. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  1018. size_t size)
  1019. {
  1020. size_t ret;
  1021. unsigned long flags;
  1022. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1023. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1024. if (!ops)
  1025. return 0;
  1026. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1027. ret = ops->unmap(ops, iova, size);
  1028. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1029. return ret;
  1030. }
  1031. static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  1032. dma_addr_t iova)
  1033. {
  1034. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1035. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1036. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1037. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1038. struct device *dev = smmu->dev;
  1039. void __iomem *cb_base;
  1040. u32 tmp;
  1041. u64 phys;
  1042. unsigned long va;
  1043. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1044. /* ATS1 registers can only be written atomically */
  1045. va = iova & ~0xfffUL;
  1046. #ifdef CONFIG_64BIT
  1047. if (smmu->version == ARM_SMMU_V2)
  1048. writeq_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
  1049. else
  1050. #endif
  1051. writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
  1052. if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
  1053. !(tmp & ATSR_ACTIVE), 5, 50)) {
  1054. dev_err(dev,
  1055. "iova to phys timed out on 0x%pad. Falling back to software table walk.\n",
  1056. &iova);
  1057. return ops->iova_to_phys(ops, iova);
  1058. }
  1059. phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
  1060. phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
  1061. if (phys & CB_PAR_F) {
  1062. dev_err(dev, "translation fault!\n");
  1063. dev_err(dev, "PAR = 0x%llx\n", phys);
  1064. return 0;
  1065. }
  1066. return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
  1067. }
  1068. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  1069. dma_addr_t iova)
  1070. {
  1071. phys_addr_t ret;
  1072. unsigned long flags;
  1073. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1074. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1075. if (!ops)
  1076. return 0;
  1077. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1078. if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
  1079. smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1080. ret = arm_smmu_iova_to_phys_hard(domain, iova);
  1081. } else {
  1082. ret = ops->iova_to_phys(ops, iova);
  1083. }
  1084. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1085. return ret;
  1086. }
  1087. static bool arm_smmu_capable(enum iommu_cap cap)
  1088. {
  1089. switch (cap) {
  1090. case IOMMU_CAP_CACHE_COHERENCY:
  1091. /*
  1092. * Return true here as the SMMU can always send out coherent
  1093. * requests.
  1094. */
  1095. return true;
  1096. case IOMMU_CAP_INTR_REMAP:
  1097. return true; /* MSIs are just memory writes */
  1098. case IOMMU_CAP_NOEXEC:
  1099. return true;
  1100. default:
  1101. return false;
  1102. }
  1103. }
  1104. static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
  1105. {
  1106. *((u16 *)data) = alias;
  1107. return 0; /* Continue walking */
  1108. }
  1109. static void __arm_smmu_release_pci_iommudata(void *data)
  1110. {
  1111. kfree(data);
  1112. }
  1113. static int arm_smmu_add_pci_device(struct pci_dev *pdev)
  1114. {
  1115. int i, ret;
  1116. u16 sid;
  1117. struct iommu_group *group;
  1118. struct arm_smmu_master_cfg *cfg;
  1119. group = iommu_group_get_for_dev(&pdev->dev);
  1120. if (IS_ERR(group))
  1121. return PTR_ERR(group);
  1122. cfg = iommu_group_get_iommudata(group);
  1123. if (!cfg) {
  1124. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1125. if (!cfg) {
  1126. ret = -ENOMEM;
  1127. goto out_put_group;
  1128. }
  1129. iommu_group_set_iommudata(group, cfg,
  1130. __arm_smmu_release_pci_iommudata);
  1131. }
  1132. if (cfg->num_streamids >= MAX_MASTER_STREAMIDS) {
  1133. ret = -ENOSPC;
  1134. goto out_put_group;
  1135. }
  1136. /*
  1137. * Assume Stream ID == Requester ID for now.
  1138. * We need a way to describe the ID mappings in FDT.
  1139. */
  1140. pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
  1141. for (i = 0; i < cfg->num_streamids; ++i)
  1142. if (cfg->streamids[i] == sid)
  1143. break;
  1144. /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
  1145. if (i == cfg->num_streamids)
  1146. cfg->streamids[cfg->num_streamids++] = sid;
  1147. return 0;
  1148. out_put_group:
  1149. iommu_group_put(group);
  1150. return ret;
  1151. }
  1152. static int arm_smmu_add_platform_device(struct device *dev)
  1153. {
  1154. struct iommu_group *group;
  1155. struct arm_smmu_master *master;
  1156. struct arm_smmu_device *smmu = find_smmu_for_device(dev);
  1157. if (!smmu)
  1158. return -ENODEV;
  1159. master = find_smmu_master(smmu, dev->of_node);
  1160. if (!master)
  1161. return -ENODEV;
  1162. /* No automatic group creation for platform devices */
  1163. group = iommu_group_alloc();
  1164. if (IS_ERR(group))
  1165. return PTR_ERR(group);
  1166. iommu_group_set_iommudata(group, &master->cfg, NULL);
  1167. return iommu_group_add_device(group, dev);
  1168. }
  1169. static int arm_smmu_add_device(struct device *dev)
  1170. {
  1171. if (dev_is_pci(dev))
  1172. return arm_smmu_add_pci_device(to_pci_dev(dev));
  1173. return arm_smmu_add_platform_device(dev);
  1174. }
  1175. static void arm_smmu_remove_device(struct device *dev)
  1176. {
  1177. iommu_group_remove_device(dev);
  1178. }
  1179. static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
  1180. enum iommu_attr attr, void *data)
  1181. {
  1182. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1183. switch (attr) {
  1184. case DOMAIN_ATTR_NESTING:
  1185. *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
  1186. return 0;
  1187. default:
  1188. return -ENODEV;
  1189. }
  1190. }
  1191. static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
  1192. enum iommu_attr attr, void *data)
  1193. {
  1194. int ret = 0;
  1195. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1196. mutex_lock(&smmu_domain->init_mutex);
  1197. switch (attr) {
  1198. case DOMAIN_ATTR_NESTING:
  1199. if (smmu_domain->smmu) {
  1200. ret = -EPERM;
  1201. goto out_unlock;
  1202. }
  1203. if (*(int *)data)
  1204. smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
  1205. else
  1206. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1207. break;
  1208. default:
  1209. ret = -ENODEV;
  1210. }
  1211. out_unlock:
  1212. mutex_unlock(&smmu_domain->init_mutex);
  1213. return ret;
  1214. }
  1215. static struct iommu_ops arm_smmu_ops = {
  1216. .capable = arm_smmu_capable,
  1217. .domain_alloc = arm_smmu_domain_alloc,
  1218. .domain_free = arm_smmu_domain_free,
  1219. .attach_dev = arm_smmu_attach_dev,
  1220. .detach_dev = arm_smmu_detach_dev,
  1221. .map = arm_smmu_map,
  1222. .unmap = arm_smmu_unmap,
  1223. .map_sg = default_iommu_map_sg,
  1224. .iova_to_phys = arm_smmu_iova_to_phys,
  1225. .add_device = arm_smmu_add_device,
  1226. .remove_device = arm_smmu_remove_device,
  1227. .domain_get_attr = arm_smmu_domain_get_attr,
  1228. .domain_set_attr = arm_smmu_domain_set_attr,
  1229. .pgsize_bitmap = -1UL, /* Restricted during device attach */
  1230. };
  1231. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  1232. {
  1233. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1234. void __iomem *cb_base;
  1235. int i = 0;
  1236. u32 reg;
  1237. /* clear global FSR */
  1238. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1239. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1240. /* Mark all SMRn as invalid and all S2CRn as bypass */
  1241. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  1242. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
  1243. writel_relaxed(S2CR_TYPE_BYPASS,
  1244. gr0_base + ARM_SMMU_GR0_S2CR(i));
  1245. }
  1246. /* Make sure all context banks are disabled and clear CB_FSR */
  1247. for (i = 0; i < smmu->num_context_banks; ++i) {
  1248. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
  1249. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  1250. writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
  1251. }
  1252. /* Invalidate the TLB, just in case */
  1253. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  1254. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  1255. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1256. /* Enable fault reporting */
  1257. reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  1258. /* Disable TLB broadcasting. */
  1259. reg |= (sCR0_VMIDPNE | sCR0_PTM);
  1260. /* Enable client access, but bypass when no mapping is found */
  1261. reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
  1262. /* Disable forced broadcasting */
  1263. reg &= ~sCR0_FB;
  1264. /* Don't upgrade barriers */
  1265. reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  1266. /* Push the button */
  1267. __arm_smmu_tlb_sync(smmu);
  1268. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1269. }
  1270. static int arm_smmu_id_size_to_bits(int size)
  1271. {
  1272. switch (size) {
  1273. case 0:
  1274. return 32;
  1275. case 1:
  1276. return 36;
  1277. case 2:
  1278. return 40;
  1279. case 3:
  1280. return 42;
  1281. case 4:
  1282. return 44;
  1283. case 5:
  1284. default:
  1285. return 48;
  1286. }
  1287. }
  1288. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  1289. {
  1290. unsigned long size;
  1291. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1292. u32 id;
  1293. dev_notice(smmu->dev, "probing hardware configuration...\n");
  1294. dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
  1295. /* ID0 */
  1296. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  1297. /* Restrict available stages based on module parameter */
  1298. if (force_stage == 1)
  1299. id &= ~(ID0_S2TS | ID0_NTS);
  1300. else if (force_stage == 2)
  1301. id &= ~(ID0_S1TS | ID0_NTS);
  1302. if (id & ID0_S1TS) {
  1303. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  1304. dev_notice(smmu->dev, "\tstage 1 translation\n");
  1305. }
  1306. if (id & ID0_S2TS) {
  1307. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  1308. dev_notice(smmu->dev, "\tstage 2 translation\n");
  1309. }
  1310. if (id & ID0_NTS) {
  1311. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  1312. dev_notice(smmu->dev, "\tnested translation\n");
  1313. }
  1314. if (!(smmu->features &
  1315. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
  1316. dev_err(smmu->dev, "\tno translation support!\n");
  1317. return -ENODEV;
  1318. }
  1319. if ((id & ID0_S1TS) && ((smmu->version == 1) || !(id & ID0_ATOSNS))) {
  1320. smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
  1321. dev_notice(smmu->dev, "\taddress translation ops\n");
  1322. }
  1323. if (id & ID0_CTTW) {
  1324. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  1325. dev_notice(smmu->dev, "\tcoherent table walk\n");
  1326. }
  1327. if (id & ID0_SMS) {
  1328. u32 smr, sid, mask;
  1329. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  1330. smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
  1331. ID0_NUMSMRG_MASK;
  1332. if (smmu->num_mapping_groups == 0) {
  1333. dev_err(smmu->dev,
  1334. "stream-matching supported, but no SMRs present!\n");
  1335. return -ENODEV;
  1336. }
  1337. smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
  1338. smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
  1339. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1340. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1341. mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
  1342. sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
  1343. if ((mask & sid) != sid) {
  1344. dev_err(smmu->dev,
  1345. "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
  1346. mask, sid);
  1347. return -ENODEV;
  1348. }
  1349. dev_notice(smmu->dev,
  1350. "\tstream matching with %u register groups, mask 0x%x",
  1351. smmu->num_mapping_groups, mask);
  1352. } else {
  1353. smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
  1354. ID0_NUMSIDB_MASK;
  1355. }
  1356. /* ID1 */
  1357. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  1358. smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
  1359. /* Check for size mismatch of SMMU address space from mapped region */
  1360. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  1361. size *= 2 << smmu->pgshift;
  1362. if (smmu->size != size)
  1363. dev_warn(smmu->dev,
  1364. "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
  1365. size, smmu->size);
  1366. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
  1367. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  1368. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  1369. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  1370. return -ENODEV;
  1371. }
  1372. dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  1373. smmu->num_context_banks, smmu->num_s2_context_banks);
  1374. /* ID2 */
  1375. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  1376. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  1377. smmu->ipa_size = size;
  1378. /* The output mask is also applied for bypass */
  1379. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  1380. smmu->pa_size = size;
  1381. /*
  1382. * What the page table walker can address actually depends on which
  1383. * descriptor format is in use, but since a) we don't know that yet,
  1384. * and b) it can vary per context bank, this will have to do...
  1385. */
  1386. if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
  1387. dev_warn(smmu->dev,
  1388. "failed to set DMA mask for table walker\n");
  1389. if (smmu->version == ARM_SMMU_V1) {
  1390. smmu->va_size = smmu->ipa_size;
  1391. size = SZ_4K | SZ_2M | SZ_1G;
  1392. } else {
  1393. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  1394. smmu->va_size = arm_smmu_id_size_to_bits(size);
  1395. #ifndef CONFIG_64BIT
  1396. smmu->va_size = min(32UL, smmu->va_size);
  1397. #endif
  1398. size = 0;
  1399. if (id & ID2_PTFS_4K)
  1400. size |= SZ_4K | SZ_2M | SZ_1G;
  1401. if (id & ID2_PTFS_16K)
  1402. size |= SZ_16K | SZ_32M;
  1403. if (id & ID2_PTFS_64K)
  1404. size |= SZ_64K | SZ_512M;
  1405. }
  1406. arm_smmu_ops.pgsize_bitmap &= size;
  1407. dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
  1408. if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
  1409. dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
  1410. smmu->va_size, smmu->ipa_size);
  1411. if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
  1412. dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
  1413. smmu->ipa_size, smmu->pa_size);
  1414. return 0;
  1415. }
  1416. static const struct of_device_id arm_smmu_of_match[] = {
  1417. { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
  1418. { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
  1419. { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
  1420. { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
  1421. { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
  1422. { },
  1423. };
  1424. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  1425. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  1426. {
  1427. const struct of_device_id *of_id;
  1428. struct resource *res;
  1429. struct arm_smmu_device *smmu;
  1430. struct device *dev = &pdev->dev;
  1431. struct rb_node *node;
  1432. struct of_phandle_args masterspec;
  1433. int num_irqs, i, err;
  1434. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  1435. if (!smmu) {
  1436. dev_err(dev, "failed to allocate arm_smmu_device\n");
  1437. return -ENOMEM;
  1438. }
  1439. smmu->dev = dev;
  1440. of_id = of_match_node(arm_smmu_of_match, dev->of_node);
  1441. smmu->version = (enum arm_smmu_arch_version)of_id->data;
  1442. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1443. smmu->base = devm_ioremap_resource(dev, res);
  1444. if (IS_ERR(smmu->base))
  1445. return PTR_ERR(smmu->base);
  1446. smmu->size = resource_size(res);
  1447. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  1448. &smmu->num_global_irqs)) {
  1449. dev_err(dev, "missing #global-interrupts property\n");
  1450. return -ENODEV;
  1451. }
  1452. num_irqs = 0;
  1453. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  1454. num_irqs++;
  1455. if (num_irqs > smmu->num_global_irqs)
  1456. smmu->num_context_irqs++;
  1457. }
  1458. if (!smmu->num_context_irqs) {
  1459. dev_err(dev, "found %d interrupts but expected at least %d\n",
  1460. num_irqs, smmu->num_global_irqs + 1);
  1461. return -ENODEV;
  1462. }
  1463. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  1464. GFP_KERNEL);
  1465. if (!smmu->irqs) {
  1466. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  1467. return -ENOMEM;
  1468. }
  1469. for (i = 0; i < num_irqs; ++i) {
  1470. int irq = platform_get_irq(pdev, i);
  1471. if (irq < 0) {
  1472. dev_err(dev, "failed to get irq index %d\n", i);
  1473. return -ENODEV;
  1474. }
  1475. smmu->irqs[i] = irq;
  1476. }
  1477. err = arm_smmu_device_cfg_probe(smmu);
  1478. if (err)
  1479. return err;
  1480. i = 0;
  1481. smmu->masters = RB_ROOT;
  1482. while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
  1483. "#stream-id-cells", i,
  1484. &masterspec)) {
  1485. err = register_smmu_master(smmu, dev, &masterspec);
  1486. if (err) {
  1487. dev_err(dev, "failed to add master %s\n",
  1488. masterspec.np->name);
  1489. goto out_put_masters;
  1490. }
  1491. i++;
  1492. }
  1493. dev_notice(dev, "registered %d master devices\n", i);
  1494. parse_driver_options(smmu);
  1495. if (smmu->version > ARM_SMMU_V1 &&
  1496. smmu->num_context_banks != smmu->num_context_irqs) {
  1497. dev_err(dev,
  1498. "found only %d context interrupt(s) but %d required\n",
  1499. smmu->num_context_irqs, smmu->num_context_banks);
  1500. err = -ENODEV;
  1501. goto out_put_masters;
  1502. }
  1503. for (i = 0; i < smmu->num_global_irqs; ++i) {
  1504. err = request_irq(smmu->irqs[i],
  1505. arm_smmu_global_fault,
  1506. IRQF_SHARED,
  1507. "arm-smmu global fault",
  1508. smmu);
  1509. if (err) {
  1510. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  1511. i, smmu->irqs[i]);
  1512. goto out_free_irqs;
  1513. }
  1514. }
  1515. INIT_LIST_HEAD(&smmu->list);
  1516. spin_lock(&arm_smmu_devices_lock);
  1517. list_add(&smmu->list, &arm_smmu_devices);
  1518. spin_unlock(&arm_smmu_devices_lock);
  1519. arm_smmu_device_reset(smmu);
  1520. return 0;
  1521. out_free_irqs:
  1522. while (i--)
  1523. free_irq(smmu->irqs[i], smmu);
  1524. out_put_masters:
  1525. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1526. struct arm_smmu_master *master
  1527. = container_of(node, struct arm_smmu_master, node);
  1528. of_node_put(master->of_node);
  1529. }
  1530. return err;
  1531. }
  1532. static int arm_smmu_device_remove(struct platform_device *pdev)
  1533. {
  1534. int i;
  1535. struct device *dev = &pdev->dev;
  1536. struct arm_smmu_device *curr, *smmu = NULL;
  1537. struct rb_node *node;
  1538. spin_lock(&arm_smmu_devices_lock);
  1539. list_for_each_entry(curr, &arm_smmu_devices, list) {
  1540. if (curr->dev == dev) {
  1541. smmu = curr;
  1542. list_del(&smmu->list);
  1543. break;
  1544. }
  1545. }
  1546. spin_unlock(&arm_smmu_devices_lock);
  1547. if (!smmu)
  1548. return -ENODEV;
  1549. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1550. struct arm_smmu_master *master
  1551. = container_of(node, struct arm_smmu_master, node);
  1552. of_node_put(master->of_node);
  1553. }
  1554. if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
  1555. dev_err(dev, "removing device with active domains!\n");
  1556. for (i = 0; i < smmu->num_global_irqs; ++i)
  1557. free_irq(smmu->irqs[i], smmu);
  1558. /* Turn the thing off */
  1559. writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1560. return 0;
  1561. }
  1562. static struct platform_driver arm_smmu_driver = {
  1563. .driver = {
  1564. .name = "arm-smmu",
  1565. .of_match_table = of_match_ptr(arm_smmu_of_match),
  1566. },
  1567. .probe = arm_smmu_device_dt_probe,
  1568. .remove = arm_smmu_device_remove,
  1569. };
  1570. static int __init arm_smmu_init(void)
  1571. {
  1572. struct device_node *np;
  1573. int ret;
  1574. /*
  1575. * Play nice with systems that don't have an ARM SMMU by checking that
  1576. * an ARM SMMU exists in the system before proceeding with the driver
  1577. * and IOMMU bus operation registration.
  1578. */
  1579. np = of_find_matching_node(NULL, arm_smmu_of_match);
  1580. if (!np)
  1581. return 0;
  1582. of_node_put(np);
  1583. ret = platform_driver_register(&arm_smmu_driver);
  1584. if (ret)
  1585. return ret;
  1586. /* Oh, for a proper bus abstraction */
  1587. if (!iommu_present(&platform_bus_type))
  1588. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  1589. #ifdef CONFIG_ARM_AMBA
  1590. if (!iommu_present(&amba_bustype))
  1591. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  1592. #endif
  1593. #ifdef CONFIG_PCI
  1594. if (!iommu_present(&pci_bus_type))
  1595. bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  1596. #endif
  1597. return 0;
  1598. }
  1599. static void __exit arm_smmu_exit(void)
  1600. {
  1601. return platform_driver_unregister(&arm_smmu_driver);
  1602. }
  1603. subsys_initcall(arm_smmu_init);
  1604. module_exit(arm_smmu_exit);
  1605. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  1606. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1607. MODULE_LICENSE("GPL v2");