exynos-iommu.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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 int exynos_iommu_add_device(struct device *dev)
  1027. {
  1028. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  1029. struct sysmmu_drvdata *data;
  1030. struct iommu_group *group;
  1031. if (!has_sysmmu(dev))
  1032. return -ENODEV;
  1033. group = iommu_group_get_for_dev(dev);
  1034. if (IS_ERR(group))
  1035. return PTR_ERR(group);
  1036. list_for_each_entry(data, &owner->controllers, owner_node) {
  1037. /*
  1038. * SYSMMU will be runtime activated via device link
  1039. * (dependency) to its master device, so there are no
  1040. * direct calls to pm_runtime_get/put in this driver.
  1041. */
  1042. data->link = device_link_add(dev, data->sysmmu,
  1043. DL_FLAG_PM_RUNTIME);
  1044. }
  1045. iommu_group_put(group);
  1046. return 0;
  1047. }
  1048. static void exynos_iommu_remove_device(struct device *dev)
  1049. {
  1050. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  1051. struct sysmmu_drvdata *data;
  1052. if (!has_sysmmu(dev))
  1053. return;
  1054. if (owner->domain) {
  1055. struct iommu_group *group = iommu_group_get(dev);
  1056. if (group) {
  1057. WARN_ON(owner->domain !=
  1058. iommu_group_default_domain(group));
  1059. exynos_iommu_detach_device(owner->domain, dev);
  1060. iommu_group_put(group);
  1061. }
  1062. }
  1063. iommu_group_remove_device(dev);
  1064. list_for_each_entry(data, &owner->controllers, owner_node)
  1065. device_link_del(data->link);
  1066. }
  1067. static int exynos_iommu_of_xlate(struct device *dev,
  1068. struct of_phandle_args *spec)
  1069. {
  1070. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  1071. struct platform_device *sysmmu = of_find_device_by_node(spec->np);
  1072. struct sysmmu_drvdata *data, *entry;
  1073. if (!sysmmu)
  1074. return -ENODEV;
  1075. data = platform_get_drvdata(sysmmu);
  1076. if (!data)
  1077. return -ENODEV;
  1078. if (!owner) {
  1079. owner = kzalloc(sizeof(*owner), GFP_KERNEL);
  1080. if (!owner)
  1081. return -ENOMEM;
  1082. INIT_LIST_HEAD(&owner->controllers);
  1083. mutex_init(&owner->rpm_lock);
  1084. dev->archdata.iommu = owner;
  1085. }
  1086. list_for_each_entry(entry, &owner->controllers, owner_node)
  1087. if (entry == data)
  1088. return 0;
  1089. list_add_tail(&data->owner_node, &owner->controllers);
  1090. data->master = dev;
  1091. return 0;
  1092. }
  1093. static const struct iommu_ops exynos_iommu_ops = {
  1094. .domain_alloc = exynos_iommu_domain_alloc,
  1095. .domain_free = exynos_iommu_domain_free,
  1096. .attach_dev = exynos_iommu_attach_device,
  1097. .detach_dev = exynos_iommu_detach_device,
  1098. .map = exynos_iommu_map,
  1099. .unmap = exynos_iommu_unmap,
  1100. .iova_to_phys = exynos_iommu_iova_to_phys,
  1101. .device_group = generic_device_group,
  1102. .add_device = exynos_iommu_add_device,
  1103. .remove_device = exynos_iommu_remove_device,
  1104. .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
  1105. .of_xlate = exynos_iommu_of_xlate,
  1106. };
  1107. static int __init exynos_iommu_init(void)
  1108. {
  1109. struct device_node *np;
  1110. int ret;
  1111. np = of_find_matching_node(NULL, sysmmu_of_match);
  1112. if (!np)
  1113. return 0;
  1114. of_node_put(np);
  1115. lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
  1116. LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
  1117. if (!lv2table_kmem_cache) {
  1118. pr_err("%s: Failed to create kmem cache\n", __func__);
  1119. return -ENOMEM;
  1120. }
  1121. ret = platform_driver_register(&exynos_sysmmu_driver);
  1122. if (ret) {
  1123. pr_err("%s: Failed to register driver\n", __func__);
  1124. goto err_reg_driver;
  1125. }
  1126. zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
  1127. if (zero_lv2_table == NULL) {
  1128. pr_err("%s: Failed to allocate zero level2 page table\n",
  1129. __func__);
  1130. ret = -ENOMEM;
  1131. goto err_zero_lv2;
  1132. }
  1133. ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
  1134. if (ret) {
  1135. pr_err("%s: Failed to register exynos-iommu driver.\n",
  1136. __func__);
  1137. goto err_set_iommu;
  1138. }
  1139. return 0;
  1140. err_set_iommu:
  1141. kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
  1142. err_zero_lv2:
  1143. platform_driver_unregister(&exynos_sysmmu_driver);
  1144. err_reg_driver:
  1145. kmem_cache_destroy(lv2table_kmem_cache);
  1146. return ret;
  1147. }
  1148. core_initcall(exynos_iommu_init);