exynos-iommu.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. */
  6. #ifdef CONFIG_EXYNOS_IOMMU_DEBUG
  7. #define DEBUG
  8. #endif
  9. #include <linux/clk.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/iommu.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/list.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include "iommu-pages.h"
  23. typedef u32 sysmmu_iova_t;
  24. typedef u32 sysmmu_pte_t;
  25. static struct iommu_domain exynos_identity_domain;
  26. /* We do not consider super section mapping (16MB) */
  27. #define SECT_ORDER 20
  28. #define LPAGE_ORDER 16
  29. #define SPAGE_ORDER 12
  30. #define SECT_SIZE (1 << SECT_ORDER)
  31. #define LPAGE_SIZE (1 << LPAGE_ORDER)
  32. #define SPAGE_SIZE (1 << SPAGE_ORDER)
  33. #define SECT_MASK (~(SECT_SIZE - 1))
  34. #define LPAGE_MASK (~(LPAGE_SIZE - 1))
  35. #define SPAGE_MASK (~(SPAGE_SIZE - 1))
  36. #define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
  37. ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
  38. #define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
  39. #define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
  40. #define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
  41. ((*(sent) & 3) == 1))
  42. #define lv1ent_section(sent) ((*(sent) & 3) == 2)
  43. #define lv2ent_fault(pent) ((*(pent) & 3) == 0)
  44. #define lv2ent_small(pent) ((*(pent) & 2) == 2)
  45. #define lv2ent_large(pent) ((*(pent) & 3) == 1)
  46. /*
  47. * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
  48. * v5.0 introduced support for 36bit physical address space by shifting
  49. * all page entry values by 4 bits.
  50. * All SYSMMU controllers in the system support the address spaces of the same
  51. * size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
  52. * value (0 or 4).
  53. */
  54. static short PG_ENT_SHIFT = -1;
  55. #define SYSMMU_PG_ENT_SHIFT 0
  56. #define SYSMMU_V5_PG_ENT_SHIFT 4
  57. static const sysmmu_pte_t *LV1_PROT;
  58. static const sysmmu_pte_t SYSMMU_LV1_PROT[] = {
  59. ((0 << 15) | (0 << 10)), /* no access */
  60. ((1 << 15) | (1 << 10)), /* IOMMU_READ only */
  61. ((0 << 15) | (1 << 10)), /* IOMMU_WRITE not supported, use read/write */
  62. ((0 << 15) | (1 << 10)), /* IOMMU_READ | IOMMU_WRITE */
  63. };
  64. static const sysmmu_pte_t SYSMMU_V5_LV1_PROT[] = {
  65. (0 << 4), /* no access */
  66. (1 << 4), /* IOMMU_READ only */
  67. (2 << 4), /* IOMMU_WRITE only */
  68. (3 << 4), /* IOMMU_READ | IOMMU_WRITE */
  69. };
  70. static const sysmmu_pte_t *LV2_PROT;
  71. static const sysmmu_pte_t SYSMMU_LV2_PROT[] = {
  72. ((0 << 9) | (0 << 4)), /* no access */
  73. ((1 << 9) | (1 << 4)), /* IOMMU_READ only */
  74. ((0 << 9) | (1 << 4)), /* IOMMU_WRITE not supported, use read/write */
  75. ((0 << 9) | (1 << 4)), /* IOMMU_READ | IOMMU_WRITE */
  76. };
  77. static const sysmmu_pte_t SYSMMU_V5_LV2_PROT[] = {
  78. (0 << 2), /* no access */
  79. (1 << 2), /* IOMMU_READ only */
  80. (2 << 2), /* IOMMU_WRITE only */
  81. (3 << 2), /* IOMMU_READ | IOMMU_WRITE */
  82. };
  83. #define SYSMMU_SUPPORTED_PROT_BITS (IOMMU_READ | IOMMU_WRITE)
  84. #define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
  85. #define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
  86. #define section_offs(iova) (iova & (SECT_SIZE - 1))
  87. #define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
  88. #define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
  89. #define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
  90. #define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
  91. #define NUM_LV1ENTRIES 4096
  92. #define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
  93. static u32 lv1ent_offset(sysmmu_iova_t iova)
  94. {
  95. return iova >> SECT_ORDER;
  96. }
  97. static u32 lv2ent_offset(sysmmu_iova_t iova)
  98. {
  99. return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
  100. }
  101. #define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
  102. #define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
  103. #define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
  104. #define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
  105. #define mk_lv1ent_sect(pa, prot) ((pa >> PG_ENT_SHIFT) | LV1_PROT[prot] | 2)
  106. #define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
  107. #define mk_lv2ent_lpage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 1)
  108. #define mk_lv2ent_spage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 2)
  109. #define CTRL_ENABLE 0x5
  110. #define CTRL_BLOCK 0x7
  111. #define CTRL_DISABLE 0x0
  112. #define CFG_LRU 0x1
  113. #define CFG_EAP (1 << 2)
  114. #define CFG_QOS(n) ((n & 0xF) << 7)
  115. #define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
  116. #define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
  117. #define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
  118. #define CTRL_VM_ENABLE BIT(0)
  119. #define CTRL_VM_FAULT_MODE_STALL BIT(3)
  120. #define CAPA0_CAPA1_EXIST BIT(11)
  121. #define CAPA1_VCR_ENABLED BIT(14)
  122. /* common registers */
  123. #define REG_MMU_CTRL 0x000
  124. #define REG_MMU_CFG 0x004
  125. #define REG_MMU_STATUS 0x008
  126. #define REG_MMU_VERSION 0x034
  127. #define MMU_MAJ_VER(val) ((val) >> 7)
  128. #define MMU_MIN_VER(val) ((val) & 0x7F)
  129. #define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
  130. #define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
  131. /* v1.x - v3.x registers */
  132. #define REG_PAGE_FAULT_ADDR 0x024
  133. #define REG_AW_FAULT_ADDR 0x028
  134. #define REG_AR_FAULT_ADDR 0x02C
  135. #define REG_DEFAULT_SLAVE_ADDR 0x030
  136. /* v5.x registers */
  137. #define REG_V5_FAULT_AR_VA 0x070
  138. #define REG_V5_FAULT_AW_VA 0x080
  139. /* v7.x registers */
  140. #define REG_V7_CAPA0 0x870
  141. #define REG_V7_CAPA1 0x874
  142. #define REG_V7_CTRL_VM 0x8000
  143. #define has_sysmmu(dev) (dev_iommu_priv_get(dev) != NULL)
  144. static struct device *dma_dev;
  145. static struct kmem_cache *lv2table_kmem_cache;
  146. static sysmmu_pte_t *zero_lv2_table;
  147. #define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
  148. static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
  149. {
  150. return pgtable + lv1ent_offset(iova);
  151. }
  152. static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
  153. {
  154. return (sysmmu_pte_t *)phys_to_virt(
  155. lv2table_base(sent)) + lv2ent_offset(iova);
  156. }
  157. struct sysmmu_fault {
  158. sysmmu_iova_t addr; /* IOVA address that caused fault */
  159. const char *name; /* human readable fault name */
  160. unsigned int type; /* fault type for report_iommu_fault() */
  161. };
  162. struct sysmmu_v1_fault_info {
  163. unsigned short addr_reg; /* register to read IOVA fault address */
  164. const char *name; /* human readable fault name */
  165. unsigned int type; /* fault type for report_iommu_fault */
  166. };
  167. static const struct sysmmu_v1_fault_info sysmmu_v1_faults[] = {
  168. { REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
  169. { REG_AR_FAULT_ADDR, "MULTI-HIT", IOMMU_FAULT_READ },
  170. { REG_AW_FAULT_ADDR, "MULTI-HIT", IOMMU_FAULT_WRITE },
  171. { REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
  172. { REG_AR_FAULT_ADDR, "SECURITY PROTECTION", IOMMU_FAULT_READ },
  173. { REG_AR_FAULT_ADDR, "ACCESS PROTECTION", IOMMU_FAULT_READ },
  174. { REG_AW_FAULT_ADDR, "SECURITY PROTECTION", IOMMU_FAULT_WRITE },
  175. { REG_AW_FAULT_ADDR, "ACCESS PROTECTION", IOMMU_FAULT_WRITE },
  176. };
  177. /* SysMMU v5 has the same faults for AR (0..4 bits) and AW (16..20 bits) */
  178. static const char * const sysmmu_v5_fault_names[] = {
  179. "PTW",
  180. "PAGE",
  181. "MULTI-HIT",
  182. "ACCESS PROTECTION",
  183. "SECURITY PROTECTION"
  184. };
  185. static const char * const sysmmu_v7_fault_names[] = {
  186. "PTW",
  187. "PAGE",
  188. "ACCESS PROTECTION",
  189. "RESERVED"
  190. };
  191. /*
  192. * This structure is attached to dev->iommu->priv of the master device
  193. * on device add, contains a list of SYSMMU controllers defined by device tree,
  194. * which are bound to given master device. It is usually referenced by 'owner'
  195. * pointer.
  196. */
  197. struct exynos_iommu_owner {
  198. struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
  199. struct iommu_domain *domain; /* domain this device is attached */
  200. struct mutex rpm_lock; /* for runtime pm of all sysmmus */
  201. };
  202. /*
  203. * This structure exynos specific generalization of struct iommu_domain.
  204. * It contains list of SYSMMU controllers from all master devices, which has
  205. * been attached to this domain and page tables of IO address space defined by
  206. * it. It is usually referenced by 'domain' pointer.
  207. */
  208. struct exynos_iommu_domain {
  209. struct list_head clients; /* list of sysmmu_drvdata.domain_node */
  210. sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
  211. short *lv2entcnt; /* free lv2 entry counter for each section */
  212. spinlock_t lock; /* lock for modyfying list of clients */
  213. spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
  214. struct iommu_domain domain; /* generic domain data structure */
  215. };
  216. struct sysmmu_drvdata;
  217. /*
  218. * SysMMU version specific data. Contains offsets for the registers which can
  219. * be found in different SysMMU variants, but have different offset values.
  220. * Also contains version specific callbacks to abstract the hardware.
  221. */
  222. struct sysmmu_variant {
  223. u32 pt_base; /* page table base address (physical) */
  224. u32 flush_all; /* invalidate all TLB entries */
  225. u32 flush_entry; /* invalidate specific TLB entry */
  226. u32 flush_range; /* invalidate TLB entries in specified range */
  227. u32 flush_start; /* start address of range invalidation */
  228. u32 flush_end; /* end address of range invalidation */
  229. u32 int_status; /* interrupt status information */
  230. u32 int_clear; /* clear the interrupt */
  231. u32 fault_va; /* IOVA address that caused fault */
  232. u32 fault_info; /* fault transaction info */
  233. int (*get_fault_info)(struct sysmmu_drvdata *data, unsigned int itype,
  234. struct sysmmu_fault *fault);
  235. };
  236. /*
  237. * This structure hold all data of a single SYSMMU controller, this includes
  238. * hw resources like registers and clocks, pointers and list nodes to connect
  239. * it to all other structures, internal state and parameters read from device
  240. * tree. It is usually referenced by 'data' pointer.
  241. */
  242. struct sysmmu_drvdata {
  243. struct device *sysmmu; /* SYSMMU controller device */
  244. struct device *master; /* master device (owner) */
  245. struct device_link *link; /* runtime PM link to master */
  246. void __iomem *sfrbase; /* our registers */
  247. struct clk *clk; /* SYSMMU's clock */
  248. struct clk *aclk; /* SYSMMU's aclk clock */
  249. struct clk *pclk; /* SYSMMU's pclk clock */
  250. struct clk *clk_master; /* master's device clock */
  251. spinlock_t lock; /* lock for modyfying state */
  252. bool active; /* current status */
  253. struct exynos_iommu_domain *domain; /* domain we belong to */
  254. struct list_head domain_node; /* node for domain clients list */
  255. struct list_head owner_node; /* node for owner controllers list */
  256. phys_addr_t pgtable; /* assigned page table structure */
  257. unsigned int version; /* our version */
  258. struct iommu_device iommu; /* IOMMU core handle */
  259. const struct sysmmu_variant *variant; /* version specific data */
  260. /* v7 fields */
  261. bool has_vcr; /* virtual machine control register */
  262. };
  263. #define SYSMMU_REG(data, reg) ((data)->sfrbase + (data)->variant->reg)
  264. static int exynos_sysmmu_v1_get_fault_info(struct sysmmu_drvdata *data,
  265. unsigned int itype,
  266. struct sysmmu_fault *fault)
  267. {
  268. const struct sysmmu_v1_fault_info *finfo;
  269. if (itype >= ARRAY_SIZE(sysmmu_v1_faults))
  270. return -ENXIO;
  271. finfo = &sysmmu_v1_faults[itype];
  272. fault->addr = readl(data->sfrbase + finfo->addr_reg);
  273. fault->name = finfo->name;
  274. fault->type = finfo->type;
  275. return 0;
  276. }
  277. static int exynos_sysmmu_v5_get_fault_info(struct sysmmu_drvdata *data,
  278. unsigned int itype,
  279. struct sysmmu_fault *fault)
  280. {
  281. unsigned int addr_reg;
  282. if (itype < ARRAY_SIZE(sysmmu_v5_fault_names)) {
  283. fault->type = IOMMU_FAULT_READ;
  284. addr_reg = REG_V5_FAULT_AR_VA;
  285. } else if (itype >= 16 && itype <= 20) {
  286. fault->type = IOMMU_FAULT_WRITE;
  287. addr_reg = REG_V5_FAULT_AW_VA;
  288. itype -= 16;
  289. } else {
  290. return -ENXIO;
  291. }
  292. fault->name = sysmmu_v5_fault_names[itype];
  293. fault->addr = readl(data->sfrbase + addr_reg);
  294. return 0;
  295. }
  296. static int exynos_sysmmu_v7_get_fault_info(struct sysmmu_drvdata *data,
  297. unsigned int itype,
  298. struct sysmmu_fault *fault)
  299. {
  300. u32 info = readl(SYSMMU_REG(data, fault_info));
  301. fault->addr = readl(SYSMMU_REG(data, fault_va));
  302. fault->name = sysmmu_v7_fault_names[itype % 4];
  303. fault->type = (info & BIT(20)) ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  304. return 0;
  305. }
  306. /* SysMMU v1..v3 */
  307. static const struct sysmmu_variant sysmmu_v1_variant = {
  308. .flush_all = 0x0c,
  309. .flush_entry = 0x10,
  310. .pt_base = 0x14,
  311. .int_status = 0x18,
  312. .int_clear = 0x1c,
  313. .get_fault_info = exynos_sysmmu_v1_get_fault_info,
  314. };
  315. /* SysMMU v5 */
  316. static const struct sysmmu_variant sysmmu_v5_variant = {
  317. .pt_base = 0x0c,
  318. .flush_all = 0x10,
  319. .flush_entry = 0x14,
  320. .flush_range = 0x18,
  321. .flush_start = 0x20,
  322. .flush_end = 0x24,
  323. .int_status = 0x60,
  324. .int_clear = 0x64,
  325. .get_fault_info = exynos_sysmmu_v5_get_fault_info,
  326. };
  327. /* SysMMU v7: non-VM capable register layout */
  328. static const struct sysmmu_variant sysmmu_v7_variant = {
  329. .pt_base = 0x0c,
  330. .flush_all = 0x10,
  331. .flush_entry = 0x14,
  332. .flush_range = 0x18,
  333. .flush_start = 0x20,
  334. .flush_end = 0x24,
  335. .int_status = 0x60,
  336. .int_clear = 0x64,
  337. .fault_va = 0x70,
  338. .fault_info = 0x78,
  339. .get_fault_info = exynos_sysmmu_v7_get_fault_info,
  340. };
  341. /* SysMMU v7: VM capable register layout */
  342. static const struct sysmmu_variant sysmmu_v7_vm_variant = {
  343. .pt_base = 0x800c,
  344. .flush_all = 0x8010,
  345. .flush_entry = 0x8014,
  346. .flush_range = 0x8018,
  347. .flush_start = 0x8020,
  348. .flush_end = 0x8024,
  349. .int_status = 0x60,
  350. .int_clear = 0x64,
  351. .fault_va = 0x1000,
  352. .fault_info = 0x1004,
  353. .get_fault_info = exynos_sysmmu_v7_get_fault_info,
  354. };
  355. static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
  356. {
  357. return container_of(dom, struct exynos_iommu_domain, domain);
  358. }
  359. static void sysmmu_unblock(struct sysmmu_drvdata *data)
  360. {
  361. writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
  362. }
  363. static bool sysmmu_block(struct sysmmu_drvdata *data)
  364. {
  365. int i = 120;
  366. writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
  367. while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
  368. --i;
  369. if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
  370. sysmmu_unblock(data);
  371. return false;
  372. }
  373. return true;
  374. }
  375. static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
  376. {
  377. writel(0x1, SYSMMU_REG(data, flush_all));
  378. }
  379. static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
  380. sysmmu_iova_t iova, unsigned int num_inv)
  381. {
  382. unsigned int i;
  383. if (MMU_MAJ_VER(data->version) < 5 || num_inv == 1) {
  384. for (i = 0; i < num_inv; i++) {
  385. writel((iova & SPAGE_MASK) | 1,
  386. SYSMMU_REG(data, flush_entry));
  387. iova += SPAGE_SIZE;
  388. }
  389. } else {
  390. writel(iova & SPAGE_MASK, SYSMMU_REG(data, flush_start));
  391. writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,
  392. SYSMMU_REG(data, flush_end));
  393. writel(0x1, SYSMMU_REG(data, flush_range));
  394. }
  395. }
  396. static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
  397. {
  398. u32 pt_base;
  399. if (MMU_MAJ_VER(data->version) < 5)
  400. pt_base = pgd;
  401. else
  402. pt_base = pgd >> SPAGE_ORDER;
  403. writel(pt_base, SYSMMU_REG(data, pt_base));
  404. __sysmmu_tlb_invalidate(data);
  405. }
  406. static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
  407. {
  408. BUG_ON(clk_prepare_enable(data->clk_master));
  409. BUG_ON(clk_prepare_enable(data->clk));
  410. BUG_ON(clk_prepare_enable(data->pclk));
  411. BUG_ON(clk_prepare_enable(data->aclk));
  412. }
  413. static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
  414. {
  415. clk_disable_unprepare(data->aclk);
  416. clk_disable_unprepare(data->pclk);
  417. clk_disable_unprepare(data->clk);
  418. clk_disable_unprepare(data->clk_master);
  419. }
  420. static bool __sysmmu_has_capa1(struct sysmmu_drvdata *data)
  421. {
  422. u32 capa0 = readl(data->sfrbase + REG_V7_CAPA0);
  423. return capa0 & CAPA0_CAPA1_EXIST;
  424. }
  425. static void __sysmmu_get_vcr(struct sysmmu_drvdata *data)
  426. {
  427. u32 capa1 = readl(data->sfrbase + REG_V7_CAPA1);
  428. data->has_vcr = capa1 & CAPA1_VCR_ENABLED;
  429. }
  430. static void __sysmmu_get_version(struct sysmmu_drvdata *data)
  431. {
  432. u32 ver;
  433. __sysmmu_enable_clocks(data);
  434. ver = readl(data->sfrbase + REG_MMU_VERSION);
  435. /* controllers on some SoCs don't report proper version */
  436. if (ver == 0x80000001u)
  437. data->version = MAKE_MMU_VER(1, 0);
  438. else
  439. data->version = MMU_RAW_VER(ver);
  440. dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
  441. MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));
  442. if (MMU_MAJ_VER(data->version) < 5) {
  443. data->variant = &sysmmu_v1_variant;
  444. } else if (MMU_MAJ_VER(data->version) < 7) {
  445. data->variant = &sysmmu_v5_variant;
  446. } else {
  447. if (__sysmmu_has_capa1(data))
  448. __sysmmu_get_vcr(data);
  449. if (data->has_vcr)
  450. data->variant = &sysmmu_v7_vm_variant;
  451. else
  452. data->variant = &sysmmu_v7_variant;
  453. }
  454. __sysmmu_disable_clocks(data);
  455. }
  456. static void show_fault_information(struct sysmmu_drvdata *data,
  457. const struct sysmmu_fault *fault)
  458. {
  459. sysmmu_pte_t *ent;
  460. dev_err(data->sysmmu, "%s: [%s] %s FAULT occurred at %#x\n",
  461. dev_name(data->master),
  462. fault->type == IOMMU_FAULT_READ ? "READ" : "WRITE",
  463. fault->name, fault->addr);
  464. dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable);
  465. ent = section_entry(phys_to_virt(data->pgtable), fault->addr);
  466. dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
  467. if (lv1ent_page(ent)) {
  468. ent = page_entry(ent, fault->addr);
  469. dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
  470. }
  471. }
  472. static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
  473. {
  474. struct sysmmu_drvdata *data = dev_id;
  475. unsigned int itype;
  476. struct sysmmu_fault fault;
  477. int ret = -ENOSYS;
  478. WARN_ON(!data->active);
  479. spin_lock(&data->lock);
  480. clk_enable(data->clk_master);
  481. itype = __ffs(readl(SYSMMU_REG(data, int_status)));
  482. ret = data->variant->get_fault_info(data, itype, &fault);
  483. if (ret) {
  484. dev_err(data->sysmmu, "Unhandled interrupt bit %u\n", itype);
  485. goto out;
  486. }
  487. show_fault_information(data, &fault);
  488. if (data->domain) {
  489. ret = report_iommu_fault(&data->domain->domain, data->master,
  490. fault.addr, fault.type);
  491. }
  492. if (ret)
  493. panic("Unrecoverable System MMU Fault!");
  494. out:
  495. writel(1 << itype, SYSMMU_REG(data, int_clear));
  496. /* SysMMU is in blocked state when interrupt occurred */
  497. sysmmu_unblock(data);
  498. clk_disable(data->clk_master);
  499. spin_unlock(&data->lock);
  500. return IRQ_HANDLED;
  501. }
  502. static void __sysmmu_disable(struct sysmmu_drvdata *data)
  503. {
  504. unsigned long flags;
  505. clk_enable(data->clk_master);
  506. spin_lock_irqsave(&data->lock, flags);
  507. writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
  508. writel(0, data->sfrbase + REG_MMU_CFG);
  509. data->active = false;
  510. spin_unlock_irqrestore(&data->lock, flags);
  511. __sysmmu_disable_clocks(data);
  512. }
  513. static void __sysmmu_init_config(struct sysmmu_drvdata *data)
  514. {
  515. unsigned int cfg;
  516. if (data->version <= MAKE_MMU_VER(3, 1))
  517. cfg = CFG_LRU | CFG_QOS(15);
  518. else if (data->version <= MAKE_MMU_VER(3, 2))
  519. cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
  520. else
  521. cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
  522. cfg |= CFG_EAP; /* enable access protection bits check */
  523. writel(cfg, data->sfrbase + REG_MMU_CFG);
  524. }
  525. static void __sysmmu_enable_vid(struct sysmmu_drvdata *data)
  526. {
  527. u32 ctrl;
  528. if (MMU_MAJ_VER(data->version) < 7 || !data->has_vcr)
  529. return;
  530. ctrl = readl(data->sfrbase + REG_V7_CTRL_VM);
  531. ctrl |= CTRL_VM_ENABLE | CTRL_VM_FAULT_MODE_STALL;
  532. writel(ctrl, data->sfrbase + REG_V7_CTRL_VM);
  533. }
  534. static void __sysmmu_enable(struct sysmmu_drvdata *data)
  535. {
  536. unsigned long flags;
  537. __sysmmu_enable_clocks(data);
  538. spin_lock_irqsave(&data->lock, flags);
  539. writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
  540. __sysmmu_init_config(data);
  541. __sysmmu_set_ptbase(data, data->pgtable);
  542. __sysmmu_enable_vid(data);
  543. writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
  544. data->active = true;
  545. spin_unlock_irqrestore(&data->lock, flags);
  546. /*
  547. * SYSMMU driver keeps master's clock enabled only for the short
  548. * time, while accessing the registers. For performing address
  549. * translation during DMA transaction it relies on the client
  550. * driver to enable it.
  551. */
  552. clk_disable(data->clk_master);
  553. }
  554. static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
  555. sysmmu_iova_t iova)
  556. {
  557. unsigned long flags;
  558. spin_lock_irqsave(&data->lock, flags);
  559. if (data->active && data->version >= MAKE_MMU_VER(3, 3)) {
  560. clk_enable(data->clk_master);
  561. if (sysmmu_block(data)) {
  562. if (data->version >= MAKE_MMU_VER(5, 0))
  563. __sysmmu_tlb_invalidate(data);
  564. else
  565. __sysmmu_tlb_invalidate_entry(data, iova, 1);
  566. sysmmu_unblock(data);
  567. }
  568. clk_disable(data->clk_master);
  569. }
  570. spin_unlock_irqrestore(&data->lock, flags);
  571. }
  572. static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
  573. sysmmu_iova_t iova, size_t size)
  574. {
  575. unsigned long flags;
  576. spin_lock_irqsave(&data->lock, flags);
  577. if (data->active) {
  578. unsigned int num_inv = 1;
  579. clk_enable(data->clk_master);
  580. /*
  581. * L2TLB invalidation required
  582. * 4KB page: 1 invalidation
  583. * 64KB page: 16 invalidations
  584. * 1MB page: 64 invalidations
  585. * because it is set-associative TLB
  586. * with 8-way and 64 sets.
  587. * 1MB page can be cached in one of all sets.
  588. * 64KB page can be one of 16 consecutive sets.
  589. */
  590. if (MMU_MAJ_VER(data->version) == 2)
  591. num_inv = min_t(unsigned int, size / SPAGE_SIZE, 64);
  592. if (sysmmu_block(data)) {
  593. __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
  594. sysmmu_unblock(data);
  595. }
  596. clk_disable(data->clk_master);
  597. }
  598. spin_unlock_irqrestore(&data->lock, flags);
  599. }
  600. static const struct iommu_ops exynos_iommu_ops;
  601. static int exynos_sysmmu_probe(struct platform_device *pdev)
  602. {
  603. int irq, ret;
  604. struct device *dev = &pdev->dev;
  605. struct sysmmu_drvdata *data;
  606. struct resource *res;
  607. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  608. if (!data)
  609. return -ENOMEM;
  610. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  611. data->sfrbase = devm_ioremap_resource(dev, res);
  612. if (IS_ERR(data->sfrbase))
  613. return PTR_ERR(data->sfrbase);
  614. irq = platform_get_irq(pdev, 0);
  615. if (irq <= 0)
  616. return irq;
  617. ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
  618. dev_name(dev), data);
  619. if (ret) {
  620. dev_err(dev, "Unabled to register handler of irq %d\n", irq);
  621. return ret;
  622. }
  623. data->clk = devm_clk_get_optional(dev, "sysmmu");
  624. if (IS_ERR(data->clk))
  625. return PTR_ERR(data->clk);
  626. data->aclk = devm_clk_get_optional(dev, "aclk");
  627. if (IS_ERR(data->aclk))
  628. return PTR_ERR(data->aclk);
  629. data->pclk = devm_clk_get_optional(dev, "pclk");
  630. if (IS_ERR(data->pclk))
  631. return PTR_ERR(data->pclk);
  632. if (!data->clk && (!data->aclk || !data->pclk)) {
  633. dev_err(dev, "Failed to get device clock(s)!\n");
  634. return -ENOSYS;
  635. }
  636. data->clk_master = devm_clk_get_optional(dev, "master");
  637. if (IS_ERR(data->clk_master))
  638. return PTR_ERR(data->clk_master);
  639. data->sysmmu = dev;
  640. spin_lock_init(&data->lock);
  641. __sysmmu_get_version(data);
  642. ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
  643. dev_name(data->sysmmu));
  644. if (ret)
  645. return ret;
  646. platform_set_drvdata(pdev, data);
  647. if (PG_ENT_SHIFT < 0) {
  648. if (MMU_MAJ_VER(data->version) < 5) {
  649. PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
  650. LV1_PROT = SYSMMU_LV1_PROT;
  651. LV2_PROT = SYSMMU_LV2_PROT;
  652. } else {
  653. PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
  654. LV1_PROT = SYSMMU_V5_LV1_PROT;
  655. LV2_PROT = SYSMMU_V5_LV2_PROT;
  656. }
  657. }
  658. if (MMU_MAJ_VER(data->version) >= 5) {
  659. ret = dma_set_mask(dev, DMA_BIT_MASK(36));
  660. if (ret) {
  661. dev_err(dev, "Unable to set DMA mask: %d\n", ret);
  662. goto err_dma_set_mask;
  663. }
  664. }
  665. /*
  666. * use the first registered sysmmu device for performing
  667. * dma mapping operations on iommu page tables (cpu cache flush)
  668. */
  669. if (!dma_dev)
  670. dma_dev = &pdev->dev;
  671. pm_runtime_enable(dev);
  672. ret = iommu_device_register(&data->iommu, &exynos_iommu_ops, dev);
  673. if (ret)
  674. goto err_dma_set_mask;
  675. return 0;
  676. err_dma_set_mask:
  677. iommu_device_sysfs_remove(&data->iommu);
  678. return ret;
  679. }
  680. static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)
  681. {
  682. struct sysmmu_drvdata *data = dev_get_drvdata(dev);
  683. struct device *master = data->master;
  684. if (master) {
  685. struct exynos_iommu_owner *owner = dev_iommu_priv_get(master);
  686. mutex_lock(&owner->rpm_lock);
  687. if (&data->domain->domain != &exynos_identity_domain) {
  688. dev_dbg(data->sysmmu, "saving state\n");
  689. __sysmmu_disable(data);
  690. }
  691. mutex_unlock(&owner->rpm_lock);
  692. }
  693. return 0;
  694. }
  695. static int __maybe_unused exynos_sysmmu_resume(struct device *dev)
  696. {
  697. struct sysmmu_drvdata *data = dev_get_drvdata(dev);
  698. struct device *master = data->master;
  699. if (master) {
  700. struct exynos_iommu_owner *owner = dev_iommu_priv_get(master);
  701. mutex_lock(&owner->rpm_lock);
  702. if (&data->domain->domain != &exynos_identity_domain) {
  703. dev_dbg(data->sysmmu, "restoring state\n");
  704. __sysmmu_enable(data);
  705. }
  706. mutex_unlock(&owner->rpm_lock);
  707. }
  708. return 0;
  709. }
  710. static const struct dev_pm_ops sysmmu_pm_ops = {
  711. SET_RUNTIME_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume, NULL)
  712. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  713. pm_runtime_force_resume)
  714. };
  715. static const struct of_device_id sysmmu_of_match[] = {
  716. { .compatible = "samsung,exynos-sysmmu", },
  717. { },
  718. };
  719. static struct platform_driver exynos_sysmmu_driver __refdata = {
  720. .probe = exynos_sysmmu_probe,
  721. .driver = {
  722. .name = "exynos-sysmmu",
  723. .of_match_table = sysmmu_of_match,
  724. .pm = &sysmmu_pm_ops,
  725. .suppress_bind_attrs = true,
  726. }
  727. };
  728. static inline void exynos_iommu_set_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
  729. {
  730. dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
  731. DMA_TO_DEVICE);
  732. *ent = cpu_to_le32(val);
  733. dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
  734. DMA_TO_DEVICE);
  735. }
  736. static struct iommu_domain *exynos_iommu_domain_alloc_paging(struct device *dev)
  737. {
  738. struct exynos_iommu_domain *domain;
  739. dma_addr_t handle;
  740. int i;
  741. /* Check if correct PTE offsets are initialized */
  742. BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
  743. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  744. if (!domain)
  745. return NULL;
  746. domain->pgtable = iommu_alloc_pages(GFP_KERNEL, 2);
  747. if (!domain->pgtable)
  748. goto err_pgtable;
  749. domain->lv2entcnt = iommu_alloc_pages(GFP_KERNEL, 1);
  750. if (!domain->lv2entcnt)
  751. goto err_counter;
  752. /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
  753. for (i = 0; i < NUM_LV1ENTRIES; i++)
  754. domain->pgtable[i] = ZERO_LV2LINK;
  755. handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
  756. DMA_TO_DEVICE);
  757. /* For mapping page table entries we rely on dma == phys */
  758. BUG_ON(handle != virt_to_phys(domain->pgtable));
  759. if (dma_mapping_error(dma_dev, handle))
  760. goto err_lv2ent;
  761. spin_lock_init(&domain->lock);
  762. spin_lock_init(&domain->pgtablelock);
  763. INIT_LIST_HEAD(&domain->clients);
  764. domain->domain.geometry.aperture_start = 0;
  765. domain->domain.geometry.aperture_end = ~0UL;
  766. domain->domain.geometry.force_aperture = true;
  767. return &domain->domain;
  768. err_lv2ent:
  769. iommu_free_pages(domain->lv2entcnt, 1);
  770. err_counter:
  771. iommu_free_pages(domain->pgtable, 2);
  772. err_pgtable:
  773. kfree(domain);
  774. return NULL;
  775. }
  776. static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
  777. {
  778. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  779. struct sysmmu_drvdata *data, *next;
  780. unsigned long flags;
  781. int i;
  782. WARN_ON(!list_empty(&domain->clients));
  783. spin_lock_irqsave(&domain->lock, flags);
  784. list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
  785. spin_lock(&data->lock);
  786. __sysmmu_disable(data);
  787. data->pgtable = 0;
  788. data->domain = NULL;
  789. list_del_init(&data->domain_node);
  790. spin_unlock(&data->lock);
  791. }
  792. spin_unlock_irqrestore(&domain->lock, flags);
  793. dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
  794. DMA_TO_DEVICE);
  795. for (i = 0; i < NUM_LV1ENTRIES; i++)
  796. if (lv1ent_page(domain->pgtable + i)) {
  797. phys_addr_t base = lv2table_base(domain->pgtable + i);
  798. dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
  799. DMA_TO_DEVICE);
  800. kmem_cache_free(lv2table_kmem_cache,
  801. phys_to_virt(base));
  802. }
  803. iommu_free_pages(domain->pgtable, 2);
  804. iommu_free_pages(domain->lv2entcnt, 1);
  805. kfree(domain);
  806. }
  807. static int exynos_iommu_identity_attach(struct iommu_domain *identity_domain,
  808. struct device *dev)
  809. {
  810. struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
  811. struct exynos_iommu_domain *domain;
  812. phys_addr_t pagetable;
  813. struct sysmmu_drvdata *data, *next;
  814. unsigned long flags;
  815. if (owner->domain == identity_domain)
  816. return 0;
  817. domain = to_exynos_domain(owner->domain);
  818. pagetable = virt_to_phys(domain->pgtable);
  819. mutex_lock(&owner->rpm_lock);
  820. list_for_each_entry(data, &owner->controllers, owner_node) {
  821. pm_runtime_get_noresume(data->sysmmu);
  822. if (pm_runtime_active(data->sysmmu))
  823. __sysmmu_disable(data);
  824. pm_runtime_put(data->sysmmu);
  825. }
  826. spin_lock_irqsave(&domain->lock, flags);
  827. list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
  828. spin_lock(&data->lock);
  829. data->pgtable = 0;
  830. data->domain = NULL;
  831. list_del_init(&data->domain_node);
  832. spin_unlock(&data->lock);
  833. }
  834. owner->domain = identity_domain;
  835. spin_unlock_irqrestore(&domain->lock, flags);
  836. mutex_unlock(&owner->rpm_lock);
  837. dev_dbg(dev, "%s: Restored IOMMU to IDENTITY from pgtable %pa\n",
  838. __func__, &pagetable);
  839. return 0;
  840. }
  841. static struct iommu_domain_ops exynos_identity_ops = {
  842. .attach_dev = exynos_iommu_identity_attach,
  843. };
  844. static struct iommu_domain exynos_identity_domain = {
  845. .type = IOMMU_DOMAIN_IDENTITY,
  846. .ops = &exynos_identity_ops,
  847. };
  848. static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
  849. struct device *dev)
  850. {
  851. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  852. struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
  853. struct sysmmu_drvdata *data;
  854. phys_addr_t pagetable = virt_to_phys(domain->pgtable);
  855. unsigned long flags;
  856. int err;
  857. err = exynos_iommu_identity_attach(&exynos_identity_domain, dev);
  858. if (err)
  859. return err;
  860. mutex_lock(&owner->rpm_lock);
  861. spin_lock_irqsave(&domain->lock, flags);
  862. list_for_each_entry(data, &owner->controllers, owner_node) {
  863. spin_lock(&data->lock);
  864. data->pgtable = pagetable;
  865. data->domain = domain;
  866. list_add_tail(&data->domain_node, &domain->clients);
  867. spin_unlock(&data->lock);
  868. }
  869. owner->domain = iommu_domain;
  870. spin_unlock_irqrestore(&domain->lock, flags);
  871. list_for_each_entry(data, &owner->controllers, owner_node) {
  872. pm_runtime_get_noresume(data->sysmmu);
  873. if (pm_runtime_active(data->sysmmu))
  874. __sysmmu_enable(data);
  875. pm_runtime_put(data->sysmmu);
  876. }
  877. mutex_unlock(&owner->rpm_lock);
  878. dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa\n", __func__,
  879. &pagetable);
  880. return 0;
  881. }
  882. static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
  883. sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
  884. {
  885. if (lv1ent_section(sent)) {
  886. WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
  887. return ERR_PTR(-EADDRINUSE);
  888. }
  889. if (lv1ent_fault(sent)) {
  890. dma_addr_t handle;
  891. sysmmu_pte_t *pent;
  892. bool need_flush_flpd_cache = lv1ent_zero(sent);
  893. pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
  894. BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
  895. if (!pent)
  896. return ERR_PTR(-ENOMEM);
  897. exynos_iommu_set_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
  898. kmemleak_ignore(pent);
  899. *pgcounter = NUM_LV2ENTRIES;
  900. handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE,
  901. DMA_TO_DEVICE);
  902. if (dma_mapping_error(dma_dev, handle)) {
  903. kmem_cache_free(lv2table_kmem_cache, pent);
  904. return ERR_PTR(-EADDRINUSE);
  905. }
  906. /*
  907. * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
  908. * FLPD cache may cache the address of zero_l2_table. This
  909. * function replaces the zero_l2_table with new L2 page table
  910. * to write valid mappings.
  911. * Accessing the valid area may cause page fault since FLPD
  912. * cache may still cache zero_l2_table for the valid area
  913. * instead of new L2 page table that has the mapping
  914. * information of the valid area.
  915. * Thus any replacement of zero_l2_table with other valid L2
  916. * page table must involve FLPD cache invalidation for System
  917. * MMU v3.3.
  918. * FLPD cache invalidation is performed with TLB invalidation
  919. * by VPN without blocking. It is safe to invalidate TLB without
  920. * blocking because the target address of TLB invalidation is
  921. * not currently mapped.
  922. */
  923. if (need_flush_flpd_cache) {
  924. struct sysmmu_drvdata *data;
  925. spin_lock(&domain->lock);
  926. list_for_each_entry(data, &domain->clients, domain_node)
  927. sysmmu_tlb_invalidate_flpdcache(data, iova);
  928. spin_unlock(&domain->lock);
  929. }
  930. }
  931. return page_entry(sent, iova);
  932. }
  933. static int lv1set_section(struct exynos_iommu_domain *domain,
  934. sysmmu_pte_t *sent, sysmmu_iova_t iova,
  935. phys_addr_t paddr, int prot, short *pgcnt)
  936. {
  937. if (lv1ent_section(sent)) {
  938. WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
  939. iova);
  940. return -EADDRINUSE;
  941. }
  942. if (lv1ent_page(sent)) {
  943. if (*pgcnt != NUM_LV2ENTRIES) {
  944. WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
  945. iova);
  946. return -EADDRINUSE;
  947. }
  948. kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
  949. *pgcnt = 0;
  950. }
  951. exynos_iommu_set_pte(sent, mk_lv1ent_sect(paddr, prot));
  952. spin_lock(&domain->lock);
  953. if (lv1ent_page_zero(sent)) {
  954. struct sysmmu_drvdata *data;
  955. /*
  956. * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
  957. * entry by speculative prefetch of SLPD which has no mapping.
  958. */
  959. list_for_each_entry(data, &domain->clients, domain_node)
  960. sysmmu_tlb_invalidate_flpdcache(data, iova);
  961. }
  962. spin_unlock(&domain->lock);
  963. return 0;
  964. }
  965. static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
  966. int prot, short *pgcnt)
  967. {
  968. if (size == SPAGE_SIZE) {
  969. if (WARN_ON(!lv2ent_fault(pent)))
  970. return -EADDRINUSE;
  971. exynos_iommu_set_pte(pent, mk_lv2ent_spage(paddr, prot));
  972. *pgcnt -= 1;
  973. } else { /* size == LPAGE_SIZE */
  974. int i;
  975. dma_addr_t pent_base = virt_to_phys(pent);
  976. dma_sync_single_for_cpu(dma_dev, pent_base,
  977. sizeof(*pent) * SPAGES_PER_LPAGE,
  978. DMA_TO_DEVICE);
  979. for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
  980. if (WARN_ON(!lv2ent_fault(pent))) {
  981. if (i > 0)
  982. memset(pent - i, 0, sizeof(*pent) * i);
  983. return -EADDRINUSE;
  984. }
  985. *pent = mk_lv2ent_lpage(paddr, prot);
  986. }
  987. dma_sync_single_for_device(dma_dev, pent_base,
  988. sizeof(*pent) * SPAGES_PER_LPAGE,
  989. DMA_TO_DEVICE);
  990. *pgcnt -= SPAGES_PER_LPAGE;
  991. }
  992. return 0;
  993. }
  994. /*
  995. * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
  996. *
  997. * System MMU v3.x has advanced logic to improve address translation
  998. * performance with caching more page table entries by a page table walk.
  999. * However, the logic has a bug that while caching faulty page table entries,
  1000. * System MMU reports page fault if the cached fault entry is hit even though
  1001. * the fault entry is updated to a valid entry after the entry is cached.
  1002. * To prevent caching faulty page table entries which may be updated to valid
  1003. * entries later, the virtual memory manager should care about the workaround
  1004. * for the problem. The following describes the workaround.
  1005. *
  1006. * Any two consecutive I/O virtual address regions must have a hole of 128KiB
  1007. * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
  1008. *
  1009. * Precisely, any start address of I/O virtual region must be aligned with
  1010. * the following sizes for System MMU v3.1 and v3.2.
  1011. * System MMU v3.1: 128KiB
  1012. * System MMU v3.2: 256KiB
  1013. *
  1014. * Because System MMU v3.3 caches page table entries more aggressively, it needs
  1015. * more workarounds.
  1016. * - Any two consecutive I/O virtual regions must have a hole of size larger
  1017. * than or equal to 128KiB.
  1018. * - Start address of an I/O virtual region must be aligned by 128KiB.
  1019. */
  1020. static int exynos_iommu_map(struct iommu_domain *iommu_domain,
  1021. unsigned long l_iova, phys_addr_t paddr, size_t size,
  1022. size_t count, int prot, gfp_t gfp, size_t *mapped)
  1023. {
  1024. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  1025. sysmmu_pte_t *entry;
  1026. sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
  1027. unsigned long flags;
  1028. int ret = -ENOMEM;
  1029. BUG_ON(domain->pgtable == NULL);
  1030. prot &= SYSMMU_SUPPORTED_PROT_BITS;
  1031. spin_lock_irqsave(&domain->pgtablelock, flags);
  1032. entry = section_entry(domain->pgtable, iova);
  1033. if (size == SECT_SIZE) {
  1034. ret = lv1set_section(domain, entry, iova, paddr, prot,
  1035. &domain->lv2entcnt[lv1ent_offset(iova)]);
  1036. } else {
  1037. sysmmu_pte_t *pent;
  1038. pent = alloc_lv2entry(domain, entry, iova,
  1039. &domain->lv2entcnt[lv1ent_offset(iova)]);
  1040. if (IS_ERR(pent))
  1041. ret = PTR_ERR(pent);
  1042. else
  1043. ret = lv2set_page(pent, paddr, size, prot,
  1044. &domain->lv2entcnt[lv1ent_offset(iova)]);
  1045. }
  1046. if (ret)
  1047. pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
  1048. __func__, ret, size, iova);
  1049. else
  1050. *mapped = size;
  1051. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  1052. return ret;
  1053. }
  1054. static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
  1055. sysmmu_iova_t iova, size_t size)
  1056. {
  1057. struct sysmmu_drvdata *data;
  1058. unsigned long flags;
  1059. spin_lock_irqsave(&domain->lock, flags);
  1060. list_for_each_entry(data, &domain->clients, domain_node)
  1061. sysmmu_tlb_invalidate_entry(data, iova, size);
  1062. spin_unlock_irqrestore(&domain->lock, flags);
  1063. }
  1064. static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
  1065. unsigned long l_iova, size_t size, size_t count,
  1066. struct iommu_iotlb_gather *gather)
  1067. {
  1068. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  1069. sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
  1070. sysmmu_pte_t *ent;
  1071. size_t err_pgsize;
  1072. unsigned long flags;
  1073. BUG_ON(domain->pgtable == NULL);
  1074. spin_lock_irqsave(&domain->pgtablelock, flags);
  1075. ent = section_entry(domain->pgtable, iova);
  1076. if (lv1ent_section(ent)) {
  1077. if (WARN_ON(size < SECT_SIZE)) {
  1078. err_pgsize = SECT_SIZE;
  1079. goto err;
  1080. }
  1081. /* workaround for h/w bug in System MMU v3.3 */
  1082. exynos_iommu_set_pte(ent, ZERO_LV2LINK);
  1083. size = SECT_SIZE;
  1084. goto done;
  1085. }
  1086. if (unlikely(lv1ent_fault(ent))) {
  1087. if (size > SECT_SIZE)
  1088. size = SECT_SIZE;
  1089. goto done;
  1090. }
  1091. /* lv1ent_page(sent) == true here */
  1092. ent = page_entry(ent, iova);
  1093. if (unlikely(lv2ent_fault(ent))) {
  1094. size = SPAGE_SIZE;
  1095. goto done;
  1096. }
  1097. if (lv2ent_small(ent)) {
  1098. exynos_iommu_set_pte(ent, 0);
  1099. size = SPAGE_SIZE;
  1100. domain->lv2entcnt[lv1ent_offset(iova)] += 1;
  1101. goto done;
  1102. }
  1103. /* lv1ent_large(ent) == true here */
  1104. if (WARN_ON(size < LPAGE_SIZE)) {
  1105. err_pgsize = LPAGE_SIZE;
  1106. goto err;
  1107. }
  1108. dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
  1109. sizeof(*ent) * SPAGES_PER_LPAGE,
  1110. DMA_TO_DEVICE);
  1111. memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
  1112. dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
  1113. sizeof(*ent) * SPAGES_PER_LPAGE,
  1114. DMA_TO_DEVICE);
  1115. size = LPAGE_SIZE;
  1116. domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
  1117. done:
  1118. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  1119. exynos_iommu_tlb_invalidate_entry(domain, iova, size);
  1120. return size;
  1121. err:
  1122. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  1123. pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
  1124. __func__, size, iova, err_pgsize);
  1125. return 0;
  1126. }
  1127. static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
  1128. dma_addr_t iova)
  1129. {
  1130. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  1131. sysmmu_pte_t *entry;
  1132. unsigned long flags;
  1133. phys_addr_t phys = 0;
  1134. spin_lock_irqsave(&domain->pgtablelock, flags);
  1135. entry = section_entry(domain->pgtable, iova);
  1136. if (lv1ent_section(entry)) {
  1137. phys = section_phys(entry) + section_offs(iova);
  1138. } else if (lv1ent_page(entry)) {
  1139. entry = page_entry(entry, iova);
  1140. if (lv2ent_large(entry))
  1141. phys = lpage_phys(entry) + lpage_offs(iova);
  1142. else if (lv2ent_small(entry))
  1143. phys = spage_phys(entry) + spage_offs(iova);
  1144. }
  1145. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  1146. return phys;
  1147. }
  1148. static struct iommu_device *exynos_iommu_probe_device(struct device *dev)
  1149. {
  1150. struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
  1151. struct sysmmu_drvdata *data;
  1152. if (!has_sysmmu(dev))
  1153. return ERR_PTR(-ENODEV);
  1154. list_for_each_entry(data, &owner->controllers, owner_node) {
  1155. /*
  1156. * SYSMMU will be runtime activated via device link
  1157. * (dependency) to its master device, so there are no
  1158. * direct calls to pm_runtime_get/put in this driver.
  1159. */
  1160. data->link = device_link_add(dev, data->sysmmu,
  1161. DL_FLAG_STATELESS |
  1162. DL_FLAG_PM_RUNTIME);
  1163. }
  1164. /* There is always at least one entry, see exynos_iommu_of_xlate() */
  1165. data = list_first_entry(&owner->controllers,
  1166. struct sysmmu_drvdata, owner_node);
  1167. return &data->iommu;
  1168. }
  1169. static void exynos_iommu_release_device(struct device *dev)
  1170. {
  1171. struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
  1172. struct sysmmu_drvdata *data;
  1173. WARN_ON(exynos_iommu_identity_attach(&exynos_identity_domain, dev));
  1174. list_for_each_entry(data, &owner->controllers, owner_node)
  1175. device_link_del(data->link);
  1176. }
  1177. static int exynos_iommu_of_xlate(struct device *dev,
  1178. const struct of_phandle_args *spec)
  1179. {
  1180. struct platform_device *sysmmu = of_find_device_by_node(spec->np);
  1181. struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
  1182. struct sysmmu_drvdata *data, *entry;
  1183. if (!sysmmu)
  1184. return -ENODEV;
  1185. data = platform_get_drvdata(sysmmu);
  1186. if (!data) {
  1187. put_device(&sysmmu->dev);
  1188. return -ENODEV;
  1189. }
  1190. if (!owner) {
  1191. owner = kzalloc(sizeof(*owner), GFP_KERNEL);
  1192. if (!owner) {
  1193. put_device(&sysmmu->dev);
  1194. return -ENOMEM;
  1195. }
  1196. INIT_LIST_HEAD(&owner->controllers);
  1197. mutex_init(&owner->rpm_lock);
  1198. owner->domain = &exynos_identity_domain;
  1199. dev_iommu_priv_set(dev, owner);
  1200. }
  1201. list_for_each_entry(entry, &owner->controllers, owner_node)
  1202. if (entry == data)
  1203. return 0;
  1204. list_add_tail(&data->owner_node, &owner->controllers);
  1205. data->master = dev;
  1206. return 0;
  1207. }
  1208. static const struct iommu_ops exynos_iommu_ops = {
  1209. .identity_domain = &exynos_identity_domain,
  1210. .domain_alloc_paging = exynos_iommu_domain_alloc_paging,
  1211. .device_group = generic_device_group,
  1212. .probe_device = exynos_iommu_probe_device,
  1213. .release_device = exynos_iommu_release_device,
  1214. .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
  1215. .of_xlate = exynos_iommu_of_xlate,
  1216. .default_domain_ops = &(const struct iommu_domain_ops) {
  1217. .attach_dev = exynos_iommu_attach_device,
  1218. .map_pages = exynos_iommu_map,
  1219. .unmap_pages = exynos_iommu_unmap,
  1220. .iova_to_phys = exynos_iommu_iova_to_phys,
  1221. .free = exynos_iommu_domain_free,
  1222. }
  1223. };
  1224. static int __init exynos_iommu_init(void)
  1225. {
  1226. struct device_node *np;
  1227. int ret;
  1228. np = of_find_matching_node(NULL, sysmmu_of_match);
  1229. if (!np)
  1230. return 0;
  1231. of_node_put(np);
  1232. lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
  1233. LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
  1234. if (!lv2table_kmem_cache) {
  1235. pr_err("%s: Failed to create kmem cache\n", __func__);
  1236. return -ENOMEM;
  1237. }
  1238. zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
  1239. if (zero_lv2_table == NULL) {
  1240. pr_err("%s: Failed to allocate zero level2 page table\n",
  1241. __func__);
  1242. ret = -ENOMEM;
  1243. goto err_zero_lv2;
  1244. }
  1245. ret = platform_driver_register(&exynos_sysmmu_driver);
  1246. if (ret) {
  1247. pr_err("%s: Failed to register driver\n", __func__);
  1248. goto err_reg_driver;
  1249. }
  1250. return 0;
  1251. err_reg_driver:
  1252. kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
  1253. err_zero_lv2:
  1254. kmem_cache_destroy(lv2table_kmem_cache);
  1255. return ret;
  1256. }
  1257. core_initcall(exynos_iommu_init);