exynos-iommu.c 39 KB

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