exynos-iommu.c 39 KB

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