exynos-iommu.c 36 KB

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