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