exynos-iommu.c 39 KB

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