exynos-iommu.c 37 KB

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