exynos-iommu.c 39 KB

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