exynos-iommu.c 42 KB

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