exynos-iommu.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. if (sysmmu_block(data)) {
  443. if (data->version >= MAKE_MMU_VER(5, 0))
  444. __sysmmu_tlb_invalidate(data);
  445. else
  446. __sysmmu_tlb_invalidate_entry(data, iova, 1);
  447. sysmmu_unblock(data);
  448. }
  449. clk_disable(data->clk_master);
  450. }
  451. spin_unlock_irqrestore(&data->lock, flags);
  452. }
  453. static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
  454. sysmmu_iova_t iova, size_t size)
  455. {
  456. unsigned long flags;
  457. spin_lock_irqsave(&data->lock, flags);
  458. if (is_sysmmu_active(data)) {
  459. unsigned int num_inv = 1;
  460. clk_enable(data->clk_master);
  461. /*
  462. * L2TLB invalidation required
  463. * 4KB page: 1 invalidation
  464. * 64KB page: 16 invalidations
  465. * 1MB page: 64 invalidations
  466. * because it is set-associative TLB
  467. * with 8-way and 64 sets.
  468. * 1MB page can be cached in one of all sets.
  469. * 64KB page can be one of 16 consecutive sets.
  470. */
  471. if (MMU_MAJ_VER(data->version) == 2)
  472. num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
  473. if (sysmmu_block(data)) {
  474. __sysmmu_tlb_invalidate_entry(data, iova, num_inv);
  475. sysmmu_unblock(data);
  476. }
  477. clk_disable(data->clk_master);
  478. } else {
  479. dev_dbg(data->master,
  480. "disabled. Skipping TLB invalidation @ %#x\n", iova);
  481. }
  482. spin_unlock_irqrestore(&data->lock, flags);
  483. }
  484. static struct iommu_ops exynos_iommu_ops;
  485. static int __init exynos_sysmmu_probe(struct platform_device *pdev)
  486. {
  487. int irq, ret;
  488. struct device *dev = &pdev->dev;
  489. struct sysmmu_drvdata *data;
  490. struct resource *res;
  491. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  492. if (!data)
  493. return -ENOMEM;
  494. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  495. data->sfrbase = devm_ioremap_resource(dev, res);
  496. if (IS_ERR(data->sfrbase))
  497. return PTR_ERR(data->sfrbase);
  498. irq = platform_get_irq(pdev, 0);
  499. if (irq <= 0) {
  500. dev_err(dev, "Unable to find IRQ resource\n");
  501. return irq;
  502. }
  503. ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
  504. dev_name(dev), data);
  505. if (ret) {
  506. dev_err(dev, "Unabled to register handler of irq %d\n", irq);
  507. return ret;
  508. }
  509. data->clk = devm_clk_get(dev, "sysmmu");
  510. if (PTR_ERR(data->clk) == -ENOENT)
  511. data->clk = NULL;
  512. else if (IS_ERR(data->clk))
  513. return PTR_ERR(data->clk);
  514. data->aclk = devm_clk_get(dev, "aclk");
  515. if (PTR_ERR(data->aclk) == -ENOENT)
  516. data->aclk = NULL;
  517. else if (IS_ERR(data->aclk))
  518. return PTR_ERR(data->aclk);
  519. data->pclk = devm_clk_get(dev, "pclk");
  520. if (PTR_ERR(data->pclk) == -ENOENT)
  521. data->pclk = NULL;
  522. else if (IS_ERR(data->pclk))
  523. return PTR_ERR(data->pclk);
  524. if (!data->clk && (!data->aclk || !data->pclk)) {
  525. dev_err(dev, "Failed to get device clock(s)!\n");
  526. return -ENOSYS;
  527. }
  528. data->clk_master = devm_clk_get(dev, "master");
  529. if (PTR_ERR(data->clk_master) == -ENOENT)
  530. data->clk_master = NULL;
  531. else if (IS_ERR(data->clk_master))
  532. return PTR_ERR(data->clk_master);
  533. data->sysmmu = dev;
  534. spin_lock_init(&data->lock);
  535. platform_set_drvdata(pdev, data);
  536. __sysmmu_get_version(data);
  537. if (PG_ENT_SHIFT < 0) {
  538. if (MMU_MAJ_VER(data->version) < 5)
  539. PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
  540. else
  541. PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
  542. }
  543. pm_runtime_enable(dev);
  544. of_iommu_set_ops(dev->of_node, &exynos_iommu_ops);
  545. return 0;
  546. }
  547. #ifdef CONFIG_PM_SLEEP
  548. static int exynos_sysmmu_suspend(struct device *dev)
  549. {
  550. struct sysmmu_drvdata *data = dev_get_drvdata(dev);
  551. dev_dbg(dev, "suspend\n");
  552. if (is_sysmmu_active(data)) {
  553. __sysmmu_disable_nocount(data);
  554. pm_runtime_put(dev);
  555. }
  556. return 0;
  557. }
  558. static int exynos_sysmmu_resume(struct device *dev)
  559. {
  560. struct sysmmu_drvdata *data = dev_get_drvdata(dev);
  561. dev_dbg(dev, "resume\n");
  562. if (is_sysmmu_active(data)) {
  563. pm_runtime_get_sync(dev);
  564. __sysmmu_enable_nocount(data);
  565. }
  566. return 0;
  567. }
  568. #endif
  569. static const struct dev_pm_ops sysmmu_pm_ops = {
  570. SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume)
  571. };
  572. static const struct of_device_id sysmmu_of_match[] __initconst = {
  573. { .compatible = "samsung,exynos-sysmmu", },
  574. { },
  575. };
  576. static struct platform_driver exynos_sysmmu_driver __refdata = {
  577. .probe = exynos_sysmmu_probe,
  578. .driver = {
  579. .name = "exynos-sysmmu",
  580. .of_match_table = sysmmu_of_match,
  581. .pm = &sysmmu_pm_ops,
  582. .suppress_bind_attrs = true,
  583. }
  584. };
  585. static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
  586. {
  587. dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
  588. DMA_TO_DEVICE);
  589. *ent = cpu_to_le32(val);
  590. dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
  591. DMA_TO_DEVICE);
  592. }
  593. static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
  594. {
  595. struct exynos_iommu_domain *domain;
  596. dma_addr_t handle;
  597. int i;
  598. /* Check if correct PTE offsets are initialized */
  599. BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
  600. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  601. if (!domain)
  602. return NULL;
  603. if (type == IOMMU_DOMAIN_DMA) {
  604. if (iommu_get_dma_cookie(&domain->domain) != 0)
  605. goto err_pgtable;
  606. } else if (type != IOMMU_DOMAIN_UNMANAGED) {
  607. goto err_pgtable;
  608. }
  609. domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
  610. if (!domain->pgtable)
  611. goto err_dma_cookie;
  612. domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
  613. if (!domain->lv2entcnt)
  614. goto err_counter;
  615. /* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
  616. for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
  617. domain->pgtable[i + 0] = ZERO_LV2LINK;
  618. domain->pgtable[i + 1] = ZERO_LV2LINK;
  619. domain->pgtable[i + 2] = ZERO_LV2LINK;
  620. domain->pgtable[i + 3] = ZERO_LV2LINK;
  621. domain->pgtable[i + 4] = ZERO_LV2LINK;
  622. domain->pgtable[i + 5] = ZERO_LV2LINK;
  623. domain->pgtable[i + 6] = ZERO_LV2LINK;
  624. domain->pgtable[i + 7] = ZERO_LV2LINK;
  625. }
  626. handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
  627. DMA_TO_DEVICE);
  628. /* For mapping page table entries we rely on dma == phys */
  629. BUG_ON(handle != virt_to_phys(domain->pgtable));
  630. spin_lock_init(&domain->lock);
  631. spin_lock_init(&domain->pgtablelock);
  632. INIT_LIST_HEAD(&domain->clients);
  633. domain->domain.geometry.aperture_start = 0;
  634. domain->domain.geometry.aperture_end = ~0UL;
  635. domain->domain.geometry.force_aperture = true;
  636. return &domain->domain;
  637. err_counter:
  638. free_pages((unsigned long)domain->pgtable, 2);
  639. err_dma_cookie:
  640. if (type == IOMMU_DOMAIN_DMA)
  641. iommu_put_dma_cookie(&domain->domain);
  642. err_pgtable:
  643. kfree(domain);
  644. return NULL;
  645. }
  646. static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
  647. {
  648. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  649. struct sysmmu_drvdata *data, *next;
  650. unsigned long flags;
  651. int i;
  652. WARN_ON(!list_empty(&domain->clients));
  653. spin_lock_irqsave(&domain->lock, flags);
  654. list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
  655. if (__sysmmu_disable(data))
  656. data->master = NULL;
  657. list_del_init(&data->domain_node);
  658. }
  659. spin_unlock_irqrestore(&domain->lock, flags);
  660. if (iommu_domain->type == IOMMU_DOMAIN_DMA)
  661. iommu_put_dma_cookie(iommu_domain);
  662. dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
  663. DMA_TO_DEVICE);
  664. for (i = 0; i < NUM_LV1ENTRIES; i++)
  665. if (lv1ent_page(domain->pgtable + i)) {
  666. phys_addr_t base = lv2table_base(domain->pgtable + i);
  667. dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
  668. DMA_TO_DEVICE);
  669. kmem_cache_free(lv2table_kmem_cache,
  670. phys_to_virt(base));
  671. }
  672. free_pages((unsigned long)domain->pgtable, 2);
  673. free_pages((unsigned long)domain->lv2entcnt, 1);
  674. kfree(domain);
  675. }
  676. static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
  677. struct device *dev)
  678. {
  679. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  680. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  681. phys_addr_t pagetable = virt_to_phys(domain->pgtable);
  682. struct sysmmu_drvdata *data, *next;
  683. unsigned long flags;
  684. bool found = false;
  685. if (!has_sysmmu(dev) || owner->domain != iommu_domain)
  686. return;
  687. spin_lock_irqsave(&domain->lock, flags);
  688. list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
  689. if (data->master == dev) {
  690. if (__sysmmu_disable(data)) {
  691. data->master = NULL;
  692. list_del_init(&data->domain_node);
  693. }
  694. pm_runtime_put(data->sysmmu);
  695. found = true;
  696. }
  697. }
  698. spin_unlock_irqrestore(&domain->lock, flags);
  699. owner->domain = NULL;
  700. if (found)
  701. dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
  702. __func__, &pagetable);
  703. else
  704. dev_err(dev, "%s: No IOMMU is attached\n", __func__);
  705. }
  706. static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
  707. struct device *dev)
  708. {
  709. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  710. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  711. struct sysmmu_drvdata *data;
  712. phys_addr_t pagetable = virt_to_phys(domain->pgtable);
  713. unsigned long flags;
  714. int ret = -ENODEV;
  715. if (!has_sysmmu(dev))
  716. return -ENODEV;
  717. if (owner->domain)
  718. exynos_iommu_detach_device(owner->domain, dev);
  719. list_for_each_entry(data, &owner->controllers, owner_node) {
  720. pm_runtime_get_sync(data->sysmmu);
  721. ret = __sysmmu_enable(data, pagetable, domain);
  722. if (ret >= 0) {
  723. data->master = dev;
  724. spin_lock_irqsave(&domain->lock, flags);
  725. list_add_tail(&data->domain_node, &domain->clients);
  726. spin_unlock_irqrestore(&domain->lock, flags);
  727. }
  728. }
  729. if (ret < 0) {
  730. dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
  731. __func__, &pagetable);
  732. return ret;
  733. }
  734. owner->domain = iommu_domain;
  735. dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
  736. __func__, &pagetable, (ret == 0) ? "" : ", again");
  737. return ret;
  738. }
  739. static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
  740. sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
  741. {
  742. if (lv1ent_section(sent)) {
  743. WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
  744. return ERR_PTR(-EADDRINUSE);
  745. }
  746. if (lv1ent_fault(sent)) {
  747. sysmmu_pte_t *pent;
  748. bool need_flush_flpd_cache = lv1ent_zero(sent);
  749. pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
  750. BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
  751. if (!pent)
  752. return ERR_PTR(-ENOMEM);
  753. update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
  754. kmemleak_ignore(pent);
  755. *pgcounter = NUM_LV2ENTRIES;
  756. dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE);
  757. /*
  758. * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
  759. * FLPD cache may cache the address of zero_l2_table. This
  760. * function replaces the zero_l2_table with new L2 page table
  761. * to write valid mappings.
  762. * Accessing the valid area may cause page fault since FLPD
  763. * cache may still cache zero_l2_table for the valid area
  764. * instead of new L2 page table that has the mapping
  765. * information of the valid area.
  766. * Thus any replacement of zero_l2_table with other valid L2
  767. * page table must involve FLPD cache invalidation for System
  768. * MMU v3.3.
  769. * FLPD cache invalidation is performed with TLB invalidation
  770. * by VPN without blocking. It is safe to invalidate TLB without
  771. * blocking because the target address of TLB invalidation is
  772. * not currently mapped.
  773. */
  774. if (need_flush_flpd_cache) {
  775. struct sysmmu_drvdata *data;
  776. spin_lock(&domain->lock);
  777. list_for_each_entry(data, &domain->clients, domain_node)
  778. sysmmu_tlb_invalidate_flpdcache(data, iova);
  779. spin_unlock(&domain->lock);
  780. }
  781. }
  782. return page_entry(sent, iova);
  783. }
  784. static int lv1set_section(struct exynos_iommu_domain *domain,
  785. sysmmu_pte_t *sent, sysmmu_iova_t iova,
  786. phys_addr_t paddr, short *pgcnt)
  787. {
  788. if (lv1ent_section(sent)) {
  789. WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
  790. iova);
  791. return -EADDRINUSE;
  792. }
  793. if (lv1ent_page(sent)) {
  794. if (*pgcnt != NUM_LV2ENTRIES) {
  795. WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
  796. iova);
  797. return -EADDRINUSE;
  798. }
  799. kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
  800. *pgcnt = 0;
  801. }
  802. update_pte(sent, mk_lv1ent_sect(paddr));
  803. spin_lock(&domain->lock);
  804. if (lv1ent_page_zero(sent)) {
  805. struct sysmmu_drvdata *data;
  806. /*
  807. * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
  808. * entry by speculative prefetch of SLPD which has no mapping.
  809. */
  810. list_for_each_entry(data, &domain->clients, domain_node)
  811. sysmmu_tlb_invalidate_flpdcache(data, iova);
  812. }
  813. spin_unlock(&domain->lock);
  814. return 0;
  815. }
  816. static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
  817. short *pgcnt)
  818. {
  819. if (size == SPAGE_SIZE) {
  820. if (WARN_ON(!lv2ent_fault(pent)))
  821. return -EADDRINUSE;
  822. update_pte(pent, mk_lv2ent_spage(paddr));
  823. *pgcnt -= 1;
  824. } else { /* size == LPAGE_SIZE */
  825. int i;
  826. dma_addr_t pent_base = virt_to_phys(pent);
  827. dma_sync_single_for_cpu(dma_dev, pent_base,
  828. sizeof(*pent) * SPAGES_PER_LPAGE,
  829. DMA_TO_DEVICE);
  830. for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
  831. if (WARN_ON(!lv2ent_fault(pent))) {
  832. if (i > 0)
  833. memset(pent - i, 0, sizeof(*pent) * i);
  834. return -EADDRINUSE;
  835. }
  836. *pent = mk_lv2ent_lpage(paddr);
  837. }
  838. dma_sync_single_for_device(dma_dev, pent_base,
  839. sizeof(*pent) * SPAGES_PER_LPAGE,
  840. DMA_TO_DEVICE);
  841. *pgcnt -= SPAGES_PER_LPAGE;
  842. }
  843. return 0;
  844. }
  845. /*
  846. * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
  847. *
  848. * System MMU v3.x has advanced logic to improve address translation
  849. * performance with caching more page table entries by a page table walk.
  850. * However, the logic has a bug that while caching faulty page table entries,
  851. * System MMU reports page fault if the cached fault entry is hit even though
  852. * the fault entry is updated to a valid entry after the entry is cached.
  853. * To prevent caching faulty page table entries which may be updated to valid
  854. * entries later, the virtual memory manager should care about the workaround
  855. * for the problem. The following describes the workaround.
  856. *
  857. * Any two consecutive I/O virtual address regions must have a hole of 128KiB
  858. * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
  859. *
  860. * Precisely, any start address of I/O virtual region must be aligned with
  861. * the following sizes for System MMU v3.1 and v3.2.
  862. * System MMU v3.1: 128KiB
  863. * System MMU v3.2: 256KiB
  864. *
  865. * Because System MMU v3.3 caches page table entries more aggressively, it needs
  866. * more workarounds.
  867. * - Any two consecutive I/O virtual regions must have a hole of size larger
  868. * than or equal to 128KiB.
  869. * - Start address of an I/O virtual region must be aligned by 128KiB.
  870. */
  871. static int exynos_iommu_map(struct iommu_domain *iommu_domain,
  872. unsigned long l_iova, phys_addr_t paddr, size_t size,
  873. int prot)
  874. {
  875. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  876. sysmmu_pte_t *entry;
  877. sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
  878. unsigned long flags;
  879. int ret = -ENOMEM;
  880. BUG_ON(domain->pgtable == NULL);
  881. spin_lock_irqsave(&domain->pgtablelock, flags);
  882. entry = section_entry(domain->pgtable, iova);
  883. if (size == SECT_SIZE) {
  884. ret = lv1set_section(domain, entry, iova, paddr,
  885. &domain->lv2entcnt[lv1ent_offset(iova)]);
  886. } else {
  887. sysmmu_pte_t *pent;
  888. pent = alloc_lv2entry(domain, entry, iova,
  889. &domain->lv2entcnt[lv1ent_offset(iova)]);
  890. if (IS_ERR(pent))
  891. ret = PTR_ERR(pent);
  892. else
  893. ret = lv2set_page(pent, paddr, size,
  894. &domain->lv2entcnt[lv1ent_offset(iova)]);
  895. }
  896. if (ret)
  897. pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
  898. __func__, ret, size, iova);
  899. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  900. return ret;
  901. }
  902. static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
  903. sysmmu_iova_t iova, size_t size)
  904. {
  905. struct sysmmu_drvdata *data;
  906. unsigned long flags;
  907. spin_lock_irqsave(&domain->lock, flags);
  908. list_for_each_entry(data, &domain->clients, domain_node)
  909. sysmmu_tlb_invalidate_entry(data, iova, size);
  910. spin_unlock_irqrestore(&domain->lock, flags);
  911. }
  912. static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
  913. unsigned long l_iova, size_t size)
  914. {
  915. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  916. sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
  917. sysmmu_pte_t *ent;
  918. size_t err_pgsize;
  919. unsigned long flags;
  920. BUG_ON(domain->pgtable == NULL);
  921. spin_lock_irqsave(&domain->pgtablelock, flags);
  922. ent = section_entry(domain->pgtable, iova);
  923. if (lv1ent_section(ent)) {
  924. if (WARN_ON(size < SECT_SIZE)) {
  925. err_pgsize = SECT_SIZE;
  926. goto err;
  927. }
  928. /* workaround for h/w bug in System MMU v3.3 */
  929. update_pte(ent, ZERO_LV2LINK);
  930. size = SECT_SIZE;
  931. goto done;
  932. }
  933. if (unlikely(lv1ent_fault(ent))) {
  934. if (size > SECT_SIZE)
  935. size = SECT_SIZE;
  936. goto done;
  937. }
  938. /* lv1ent_page(sent) == true here */
  939. ent = page_entry(ent, iova);
  940. if (unlikely(lv2ent_fault(ent))) {
  941. size = SPAGE_SIZE;
  942. goto done;
  943. }
  944. if (lv2ent_small(ent)) {
  945. update_pte(ent, 0);
  946. size = SPAGE_SIZE;
  947. domain->lv2entcnt[lv1ent_offset(iova)] += 1;
  948. goto done;
  949. }
  950. /* lv1ent_large(ent) == true here */
  951. if (WARN_ON(size < LPAGE_SIZE)) {
  952. err_pgsize = LPAGE_SIZE;
  953. goto err;
  954. }
  955. dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
  956. sizeof(*ent) * SPAGES_PER_LPAGE,
  957. DMA_TO_DEVICE);
  958. memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
  959. dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
  960. sizeof(*ent) * SPAGES_PER_LPAGE,
  961. DMA_TO_DEVICE);
  962. size = LPAGE_SIZE;
  963. domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
  964. done:
  965. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  966. exynos_iommu_tlb_invalidate_entry(domain, iova, size);
  967. return size;
  968. err:
  969. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  970. pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
  971. __func__, size, iova, err_pgsize);
  972. return 0;
  973. }
  974. static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
  975. dma_addr_t iova)
  976. {
  977. struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
  978. sysmmu_pte_t *entry;
  979. unsigned long flags;
  980. phys_addr_t phys = 0;
  981. spin_lock_irqsave(&domain->pgtablelock, flags);
  982. entry = section_entry(domain->pgtable, iova);
  983. if (lv1ent_section(entry)) {
  984. phys = section_phys(entry) + section_offs(iova);
  985. } else if (lv1ent_page(entry)) {
  986. entry = page_entry(entry, iova);
  987. if (lv2ent_large(entry))
  988. phys = lpage_phys(entry) + lpage_offs(iova);
  989. else if (lv2ent_small(entry))
  990. phys = spage_phys(entry) + spage_offs(iova);
  991. }
  992. spin_unlock_irqrestore(&domain->pgtablelock, flags);
  993. return phys;
  994. }
  995. static struct iommu_group *get_device_iommu_group(struct device *dev)
  996. {
  997. struct iommu_group *group;
  998. group = iommu_group_get(dev);
  999. if (!group)
  1000. group = iommu_group_alloc();
  1001. return group;
  1002. }
  1003. static int exynos_iommu_add_device(struct device *dev)
  1004. {
  1005. struct iommu_group *group;
  1006. if (!has_sysmmu(dev))
  1007. return -ENODEV;
  1008. group = iommu_group_get_for_dev(dev);
  1009. if (IS_ERR(group))
  1010. return PTR_ERR(group);
  1011. iommu_group_put(group);
  1012. return 0;
  1013. }
  1014. static void exynos_iommu_remove_device(struct device *dev)
  1015. {
  1016. if (!has_sysmmu(dev))
  1017. return;
  1018. iommu_group_remove_device(dev);
  1019. }
  1020. static int exynos_iommu_of_xlate(struct device *dev,
  1021. struct of_phandle_args *spec)
  1022. {
  1023. struct exynos_iommu_owner *owner = dev->archdata.iommu;
  1024. struct platform_device *sysmmu = of_find_device_by_node(spec->np);
  1025. struct sysmmu_drvdata *data;
  1026. if (!sysmmu)
  1027. return -ENODEV;
  1028. data = platform_get_drvdata(sysmmu);
  1029. if (!data)
  1030. return -ENODEV;
  1031. if (!owner) {
  1032. owner = kzalloc(sizeof(*owner), GFP_KERNEL);
  1033. if (!owner)
  1034. return -ENOMEM;
  1035. INIT_LIST_HEAD(&owner->controllers);
  1036. dev->archdata.iommu = owner;
  1037. }
  1038. list_add_tail(&data->owner_node, &owner->controllers);
  1039. return 0;
  1040. }
  1041. static struct iommu_ops exynos_iommu_ops = {
  1042. .domain_alloc = exynos_iommu_domain_alloc,
  1043. .domain_free = exynos_iommu_domain_free,
  1044. .attach_dev = exynos_iommu_attach_device,
  1045. .detach_dev = exynos_iommu_detach_device,
  1046. .map = exynos_iommu_map,
  1047. .unmap = exynos_iommu_unmap,
  1048. .map_sg = default_iommu_map_sg,
  1049. .iova_to_phys = exynos_iommu_iova_to_phys,
  1050. .device_group = get_device_iommu_group,
  1051. .add_device = exynos_iommu_add_device,
  1052. .remove_device = exynos_iommu_remove_device,
  1053. .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
  1054. .of_xlate = exynos_iommu_of_xlate,
  1055. };
  1056. static bool init_done;
  1057. static int __init exynos_iommu_init(void)
  1058. {
  1059. int ret;
  1060. lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
  1061. LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
  1062. if (!lv2table_kmem_cache) {
  1063. pr_err("%s: Failed to create kmem cache\n", __func__);
  1064. return -ENOMEM;
  1065. }
  1066. ret = platform_driver_register(&exynos_sysmmu_driver);
  1067. if (ret) {
  1068. pr_err("%s: Failed to register driver\n", __func__);
  1069. goto err_reg_driver;
  1070. }
  1071. zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
  1072. if (zero_lv2_table == NULL) {
  1073. pr_err("%s: Failed to allocate zero level2 page table\n",
  1074. __func__);
  1075. ret = -ENOMEM;
  1076. goto err_zero_lv2;
  1077. }
  1078. ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
  1079. if (ret) {
  1080. pr_err("%s: Failed to register exynos-iommu driver.\n",
  1081. __func__);
  1082. goto err_set_iommu;
  1083. }
  1084. init_done = true;
  1085. return 0;
  1086. err_set_iommu:
  1087. kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
  1088. err_zero_lv2:
  1089. platform_driver_unregister(&exynos_sysmmu_driver);
  1090. err_reg_driver:
  1091. kmem_cache_destroy(lv2table_kmem_cache);
  1092. return ret;
  1093. }
  1094. static int __init exynos_iommu_of_setup(struct device_node *np)
  1095. {
  1096. struct platform_device *pdev;
  1097. if (!init_done)
  1098. exynos_iommu_init();
  1099. pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
  1100. if (!pdev)
  1101. return -ENODEV;
  1102. /*
  1103. * use the first registered sysmmu device for performing
  1104. * dma mapping operations on iommu page tables (cpu cache flush)
  1105. */
  1106. if (!dma_dev)
  1107. dma_dev = &pdev->dev;
  1108. return 0;
  1109. }
  1110. IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu",
  1111. exynos_iommu_of_setup);