exynos-iommu.c 38 KB

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