exynos-iommu.c 38 KB

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