rockchip-iommu.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. */
  6. #include <asm/cacheflush.h>
  7. #include <asm/pgtable.h>
  8. #include <linux/compiler.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/iommu.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/list.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. /** MMU register offsets */
  25. #define RK_MMU_DTE_ADDR 0x00 /* Directory table address */
  26. #define RK_MMU_STATUS 0x04
  27. #define RK_MMU_COMMAND 0x08
  28. #define RK_MMU_PAGE_FAULT_ADDR 0x0C /* IOVA of last page fault */
  29. #define RK_MMU_ZAP_ONE_LINE 0x10 /* Shootdown one IOTLB entry */
  30. #define RK_MMU_INT_RAWSTAT 0x14 /* IRQ status ignoring mask */
  31. #define RK_MMU_INT_CLEAR 0x18 /* Acknowledge and re-arm irq */
  32. #define RK_MMU_INT_MASK 0x1C /* IRQ enable */
  33. #define RK_MMU_INT_STATUS 0x20 /* IRQ status after masking */
  34. #define RK_MMU_AUTO_GATING 0x24
  35. #define DTE_ADDR_DUMMY 0xCAFEBABE
  36. #define FORCE_RESET_TIMEOUT 100 /* ms */
  37. /* RK_MMU_STATUS fields */
  38. #define RK_MMU_STATUS_PAGING_ENABLED BIT(0)
  39. #define RK_MMU_STATUS_PAGE_FAULT_ACTIVE BIT(1)
  40. #define RK_MMU_STATUS_STALL_ACTIVE BIT(2)
  41. #define RK_MMU_STATUS_IDLE BIT(3)
  42. #define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY BIT(4)
  43. #define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE BIT(5)
  44. #define RK_MMU_STATUS_STALL_NOT_ACTIVE BIT(31)
  45. /* RK_MMU_COMMAND command values */
  46. #define RK_MMU_CMD_ENABLE_PAGING 0 /* Enable memory translation */
  47. #define RK_MMU_CMD_DISABLE_PAGING 1 /* Disable memory translation */
  48. #define RK_MMU_CMD_ENABLE_STALL 2 /* Stall paging to allow other cmds */
  49. #define RK_MMU_CMD_DISABLE_STALL 3 /* Stop stall re-enables paging */
  50. #define RK_MMU_CMD_ZAP_CACHE 4 /* Shoot down entire IOTLB */
  51. #define RK_MMU_CMD_PAGE_FAULT_DONE 5 /* Clear page fault */
  52. #define RK_MMU_CMD_FORCE_RESET 6 /* Reset all registers */
  53. /* RK_MMU_INT_* register fields */
  54. #define RK_MMU_IRQ_PAGE_FAULT 0x01 /* page fault */
  55. #define RK_MMU_IRQ_BUS_ERROR 0x02 /* bus read error */
  56. #define RK_MMU_IRQ_MASK (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR)
  57. #define NUM_DT_ENTRIES 1024
  58. #define NUM_PT_ENTRIES 1024
  59. #define SPAGE_ORDER 12
  60. #define SPAGE_SIZE (1 << SPAGE_ORDER)
  61. /*
  62. * Support mapping any size that fits in one page table:
  63. * 4 KiB to 4 MiB
  64. */
  65. #define RK_IOMMU_PGSIZE_BITMAP 0x007ff000
  66. #define IOMMU_REG_POLL_COUNT_FAST 1000
  67. struct rk_iommu_domain {
  68. struct list_head iommus;
  69. u32 *dt; /* page directory table */
  70. spinlock_t iommus_lock; /* lock for iommus list */
  71. spinlock_t dt_lock; /* lock for modifying page directory table */
  72. struct iommu_domain domain;
  73. };
  74. struct rk_iommu {
  75. struct device *dev;
  76. void __iomem **bases;
  77. int num_mmu;
  78. int irq;
  79. struct list_head node; /* entry in rk_iommu_domain.iommus */
  80. struct iommu_domain *domain; /* domain to which iommu is attached */
  81. };
  82. static inline void rk_table_flush(u32 *va, unsigned int count)
  83. {
  84. phys_addr_t pa_start = virt_to_phys(va);
  85. phys_addr_t pa_end = virt_to_phys(va + count);
  86. size_t size = pa_end - pa_start;
  87. __cpuc_flush_dcache_area(va, size);
  88. outer_flush_range(pa_start, pa_end);
  89. }
  90. static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
  91. {
  92. return container_of(dom, struct rk_iommu_domain, domain);
  93. }
  94. /**
  95. * Inspired by _wait_for in intel_drv.h
  96. * This is NOT safe for use in interrupt context.
  97. *
  98. * Note that it's important that we check the condition again after having
  99. * timed out, since the timeout could be due to preemption or similar and
  100. * we've never had a chance to check the condition before the timeout.
  101. */
  102. #define rk_wait_for(COND, MS) ({ \
  103. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
  104. int ret__ = 0; \
  105. while (!(COND)) { \
  106. if (time_after(jiffies, timeout__)) { \
  107. ret__ = (COND) ? 0 : -ETIMEDOUT; \
  108. break; \
  109. } \
  110. usleep_range(50, 100); \
  111. } \
  112. ret__; \
  113. })
  114. /*
  115. * The Rockchip rk3288 iommu uses a 2-level page table.
  116. * The first level is the "Directory Table" (DT).
  117. * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing
  118. * to a "Page Table".
  119. * The second level is the 1024 Page Tables (PT).
  120. * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to
  121. * a 4 KB page of physical memory.
  122. *
  123. * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries).
  124. * Each iommu device has a MMU_DTE_ADDR register that contains the physical
  125. * address of the start of the DT page.
  126. *
  127. * The structure of the page table is as follows:
  128. *
  129. * DT
  130. * MMU_DTE_ADDR -> +-----+
  131. * | |
  132. * +-----+ PT
  133. * | DTE | -> +-----+
  134. * +-----+ | | Memory
  135. * | | +-----+ Page
  136. * | | | PTE | -> +-----+
  137. * +-----+ +-----+ | |
  138. * | | | |
  139. * | | | |
  140. * +-----+ | |
  141. * | |
  142. * | |
  143. * +-----+
  144. */
  145. /*
  146. * Each DTE has a PT address and a valid bit:
  147. * +---------------------+-----------+-+
  148. * | PT address | Reserved |V|
  149. * +---------------------+-----------+-+
  150. * 31:12 - PT address (PTs always starts on a 4 KB boundary)
  151. * 11: 1 - Reserved
  152. * 0 - 1 if PT @ PT address is valid
  153. */
  154. #define RK_DTE_PT_ADDRESS_MASK 0xfffff000
  155. #define RK_DTE_PT_VALID BIT(0)
  156. static inline phys_addr_t rk_dte_pt_address(u32 dte)
  157. {
  158. return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK;
  159. }
  160. static inline bool rk_dte_is_pt_valid(u32 dte)
  161. {
  162. return dte & RK_DTE_PT_VALID;
  163. }
  164. static u32 rk_mk_dte(u32 *pt)
  165. {
  166. phys_addr_t pt_phys = virt_to_phys(pt);
  167. return (pt_phys & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID;
  168. }
  169. /*
  170. * Each PTE has a Page address, some flags and a valid bit:
  171. * +---------------------+---+-------+-+
  172. * | Page address |Rsv| Flags |V|
  173. * +---------------------+---+-------+-+
  174. * 31:12 - Page address (Pages always start on a 4 KB boundary)
  175. * 11: 9 - Reserved
  176. * 8: 1 - Flags
  177. * 8 - Read allocate - allocate cache space on read misses
  178. * 7 - Read cache - enable cache & prefetch of data
  179. * 6 - Write buffer - enable delaying writes on their way to memory
  180. * 5 - Write allocate - allocate cache space on write misses
  181. * 4 - Write cache - different writes can be merged together
  182. * 3 - Override cache attributes
  183. * if 1, bits 4-8 control cache attributes
  184. * if 0, the system bus defaults are used
  185. * 2 - Writable
  186. * 1 - Readable
  187. * 0 - 1 if Page @ Page address is valid
  188. */
  189. #define RK_PTE_PAGE_ADDRESS_MASK 0xfffff000
  190. #define RK_PTE_PAGE_FLAGS_MASK 0x000001fe
  191. #define RK_PTE_PAGE_WRITABLE BIT(2)
  192. #define RK_PTE_PAGE_READABLE BIT(1)
  193. #define RK_PTE_PAGE_VALID BIT(0)
  194. static inline phys_addr_t rk_pte_page_address(u32 pte)
  195. {
  196. return (phys_addr_t)pte & RK_PTE_PAGE_ADDRESS_MASK;
  197. }
  198. static inline bool rk_pte_is_page_valid(u32 pte)
  199. {
  200. return pte & RK_PTE_PAGE_VALID;
  201. }
  202. /* TODO: set cache flags per prot IOMMU_CACHE */
  203. static u32 rk_mk_pte(phys_addr_t page, int prot)
  204. {
  205. u32 flags = 0;
  206. flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0;
  207. flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0;
  208. page &= RK_PTE_PAGE_ADDRESS_MASK;
  209. return page | flags | RK_PTE_PAGE_VALID;
  210. }
  211. static u32 rk_mk_pte_invalid(u32 pte)
  212. {
  213. return pte & ~RK_PTE_PAGE_VALID;
  214. }
  215. /*
  216. * rk3288 iova (IOMMU Virtual Address) format
  217. * 31 22.21 12.11 0
  218. * +-----------+-----------+-------------+
  219. * | DTE index | PTE index | Page offset |
  220. * +-----------+-----------+-------------+
  221. * 31:22 - DTE index - index of DTE in DT
  222. * 21:12 - PTE index - index of PTE in PT @ DTE.pt_address
  223. * 11: 0 - Page offset - offset into page @ PTE.page_address
  224. */
  225. #define RK_IOVA_DTE_MASK 0xffc00000
  226. #define RK_IOVA_DTE_SHIFT 22
  227. #define RK_IOVA_PTE_MASK 0x003ff000
  228. #define RK_IOVA_PTE_SHIFT 12
  229. #define RK_IOVA_PAGE_MASK 0x00000fff
  230. #define RK_IOVA_PAGE_SHIFT 0
  231. static u32 rk_iova_dte_index(dma_addr_t iova)
  232. {
  233. return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT;
  234. }
  235. static u32 rk_iova_pte_index(dma_addr_t iova)
  236. {
  237. return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT;
  238. }
  239. static u32 rk_iova_page_offset(dma_addr_t iova)
  240. {
  241. return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT;
  242. }
  243. static u32 rk_iommu_read(void __iomem *base, u32 offset)
  244. {
  245. return readl(base + offset);
  246. }
  247. static void rk_iommu_write(void __iomem *base, u32 offset, u32 value)
  248. {
  249. writel(value, base + offset);
  250. }
  251. static void rk_iommu_command(struct rk_iommu *iommu, u32 command)
  252. {
  253. int i;
  254. for (i = 0; i < iommu->num_mmu; i++)
  255. writel(command, iommu->bases[i] + RK_MMU_COMMAND);
  256. }
  257. static void rk_iommu_base_command(void __iomem *base, u32 command)
  258. {
  259. writel(command, base + RK_MMU_COMMAND);
  260. }
  261. static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova,
  262. size_t size)
  263. {
  264. int i;
  265. dma_addr_t iova_end = iova + size;
  266. /*
  267. * TODO(djkurtz): Figure out when it is more efficient to shootdown the
  268. * entire iotlb rather than iterate over individual iovas.
  269. */
  270. for (i = 0; i < iommu->num_mmu; i++)
  271. for (; iova < iova_end; iova += SPAGE_SIZE)
  272. rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova);
  273. }
  274. static bool rk_iommu_is_stall_active(struct rk_iommu *iommu)
  275. {
  276. bool active = true;
  277. int i;
  278. for (i = 0; i < iommu->num_mmu; i++)
  279. active &= rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
  280. RK_MMU_STATUS_STALL_ACTIVE;
  281. return active;
  282. }
  283. static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu)
  284. {
  285. bool enable = true;
  286. int i;
  287. for (i = 0; i < iommu->num_mmu; i++)
  288. enable &= rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
  289. RK_MMU_STATUS_PAGING_ENABLED;
  290. return enable;
  291. }
  292. static int rk_iommu_enable_stall(struct rk_iommu *iommu)
  293. {
  294. int ret, i;
  295. if (rk_iommu_is_stall_active(iommu))
  296. return 0;
  297. /* Stall can only be enabled if paging is enabled */
  298. if (!rk_iommu_is_paging_enabled(iommu))
  299. return 0;
  300. rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL);
  301. ret = rk_wait_for(rk_iommu_is_stall_active(iommu), 1);
  302. if (ret)
  303. for (i = 0; i < iommu->num_mmu; i++)
  304. dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n",
  305. rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
  306. return ret;
  307. }
  308. static int rk_iommu_disable_stall(struct rk_iommu *iommu)
  309. {
  310. int ret, i;
  311. if (!rk_iommu_is_stall_active(iommu))
  312. return 0;
  313. rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL);
  314. ret = rk_wait_for(!rk_iommu_is_stall_active(iommu), 1);
  315. if (ret)
  316. for (i = 0; i < iommu->num_mmu; i++)
  317. dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n",
  318. rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
  319. return ret;
  320. }
  321. static int rk_iommu_enable_paging(struct rk_iommu *iommu)
  322. {
  323. int ret, i;
  324. if (rk_iommu_is_paging_enabled(iommu))
  325. return 0;
  326. rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING);
  327. ret = rk_wait_for(rk_iommu_is_paging_enabled(iommu), 1);
  328. if (ret)
  329. for (i = 0; i < iommu->num_mmu; i++)
  330. dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n",
  331. rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
  332. return ret;
  333. }
  334. static int rk_iommu_disable_paging(struct rk_iommu *iommu)
  335. {
  336. int ret, i;
  337. if (!rk_iommu_is_paging_enabled(iommu))
  338. return 0;
  339. rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING);
  340. ret = rk_wait_for(!rk_iommu_is_paging_enabled(iommu), 1);
  341. if (ret)
  342. for (i = 0; i < iommu->num_mmu; i++)
  343. dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n",
  344. rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
  345. return ret;
  346. }
  347. static int rk_iommu_force_reset(struct rk_iommu *iommu)
  348. {
  349. int ret, i;
  350. u32 dte_addr;
  351. /*
  352. * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY
  353. * and verifying that upper 5 nybbles are read back.
  354. */
  355. for (i = 0; i < iommu->num_mmu; i++) {
  356. rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, DTE_ADDR_DUMMY);
  357. dte_addr = rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR);
  358. if (dte_addr != (DTE_ADDR_DUMMY & RK_DTE_PT_ADDRESS_MASK)) {
  359. dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n");
  360. return -EFAULT;
  361. }
  362. }
  363. rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET);
  364. for (i = 0; i < iommu->num_mmu; i++) {
  365. ret = rk_wait_for(rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0x00000000,
  366. FORCE_RESET_TIMEOUT);
  367. if (ret) {
  368. dev_err(iommu->dev, "FORCE_RESET command timed out\n");
  369. return ret;
  370. }
  371. }
  372. return 0;
  373. }
  374. static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova)
  375. {
  376. void __iomem *base = iommu->bases[index];
  377. u32 dte_index, pte_index, page_offset;
  378. u32 mmu_dte_addr;
  379. phys_addr_t mmu_dte_addr_phys, dte_addr_phys;
  380. u32 *dte_addr;
  381. u32 dte;
  382. phys_addr_t pte_addr_phys = 0;
  383. u32 *pte_addr = NULL;
  384. u32 pte = 0;
  385. phys_addr_t page_addr_phys = 0;
  386. u32 page_flags = 0;
  387. dte_index = rk_iova_dte_index(iova);
  388. pte_index = rk_iova_pte_index(iova);
  389. page_offset = rk_iova_page_offset(iova);
  390. mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR);
  391. mmu_dte_addr_phys = (phys_addr_t)mmu_dte_addr;
  392. dte_addr_phys = mmu_dte_addr_phys + (4 * dte_index);
  393. dte_addr = phys_to_virt(dte_addr_phys);
  394. dte = *dte_addr;
  395. if (!rk_dte_is_pt_valid(dte))
  396. goto print_it;
  397. pte_addr_phys = rk_dte_pt_address(dte) + (pte_index * 4);
  398. pte_addr = phys_to_virt(pte_addr_phys);
  399. pte = *pte_addr;
  400. if (!rk_pte_is_page_valid(pte))
  401. goto print_it;
  402. page_addr_phys = rk_pte_page_address(pte) + page_offset;
  403. page_flags = pte & RK_PTE_PAGE_FLAGS_MASK;
  404. print_it:
  405. dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n",
  406. &iova, dte_index, pte_index, page_offset);
  407. dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n",
  408. &mmu_dte_addr_phys, &dte_addr_phys, dte,
  409. rk_dte_is_pt_valid(dte), &pte_addr_phys, pte,
  410. rk_pte_is_page_valid(pte), &page_addr_phys, page_flags);
  411. }
  412. static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
  413. {
  414. struct rk_iommu *iommu = dev_id;
  415. u32 status;
  416. u32 int_status;
  417. dma_addr_t iova;
  418. irqreturn_t ret = IRQ_NONE;
  419. int i;
  420. for (i = 0; i < iommu->num_mmu; i++) {
  421. int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
  422. if (int_status == 0)
  423. continue;
  424. ret = IRQ_HANDLED;
  425. iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
  426. if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
  427. int flags;
  428. status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
  429. flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ?
  430. IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  431. dev_err(iommu->dev, "Page fault at %pad of type %s\n",
  432. &iova,
  433. (flags == IOMMU_FAULT_WRITE) ? "write" : "read");
  434. log_iova(iommu, i, iova);
  435. /*
  436. * Report page fault to any installed handlers.
  437. * Ignore the return code, though, since we always zap cache
  438. * and clear the page fault anyway.
  439. */
  440. if (iommu->domain)
  441. report_iommu_fault(iommu->domain, iommu->dev, iova,
  442. flags);
  443. else
  444. dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
  445. rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
  446. rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
  447. }
  448. if (int_status & RK_MMU_IRQ_BUS_ERROR)
  449. dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
  450. if (int_status & ~RK_MMU_IRQ_MASK)
  451. dev_err(iommu->dev, "unexpected int_status: %#08x\n",
  452. int_status);
  453. rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
  454. }
  455. return ret;
  456. }
  457. static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain,
  458. dma_addr_t iova)
  459. {
  460. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  461. unsigned long flags;
  462. phys_addr_t pt_phys, phys = 0;
  463. u32 dte, pte;
  464. u32 *page_table;
  465. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  466. dte = rk_domain->dt[rk_iova_dte_index(iova)];
  467. if (!rk_dte_is_pt_valid(dte))
  468. goto out;
  469. pt_phys = rk_dte_pt_address(dte);
  470. page_table = (u32 *)phys_to_virt(pt_phys);
  471. pte = page_table[rk_iova_pte_index(iova)];
  472. if (!rk_pte_is_page_valid(pte))
  473. goto out;
  474. phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
  475. out:
  476. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  477. return phys;
  478. }
  479. static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain,
  480. dma_addr_t iova, size_t size)
  481. {
  482. struct list_head *pos;
  483. unsigned long flags;
  484. /* shootdown these iova from all iommus using this domain */
  485. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  486. list_for_each(pos, &rk_domain->iommus) {
  487. struct rk_iommu *iommu;
  488. iommu = list_entry(pos, struct rk_iommu, node);
  489. rk_iommu_zap_lines(iommu, iova, size);
  490. }
  491. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  492. }
  493. static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
  494. dma_addr_t iova, size_t size)
  495. {
  496. rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
  497. if (size > SPAGE_SIZE)
  498. rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE,
  499. SPAGE_SIZE);
  500. }
  501. static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
  502. dma_addr_t iova)
  503. {
  504. u32 *page_table, *dte_addr;
  505. u32 dte;
  506. phys_addr_t pt_phys;
  507. assert_spin_locked(&rk_domain->dt_lock);
  508. dte_addr = &rk_domain->dt[rk_iova_dte_index(iova)];
  509. dte = *dte_addr;
  510. if (rk_dte_is_pt_valid(dte))
  511. goto done;
  512. page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
  513. if (!page_table)
  514. return ERR_PTR(-ENOMEM);
  515. dte = rk_mk_dte(page_table);
  516. *dte_addr = dte;
  517. rk_table_flush(page_table, NUM_PT_ENTRIES);
  518. rk_table_flush(dte_addr, 1);
  519. done:
  520. pt_phys = rk_dte_pt_address(dte);
  521. return (u32 *)phys_to_virt(pt_phys);
  522. }
  523. static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
  524. u32 *pte_addr, dma_addr_t iova, size_t size)
  525. {
  526. unsigned int pte_count;
  527. unsigned int pte_total = size / SPAGE_SIZE;
  528. assert_spin_locked(&rk_domain->dt_lock);
  529. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  530. u32 pte = pte_addr[pte_count];
  531. if (!rk_pte_is_page_valid(pte))
  532. break;
  533. pte_addr[pte_count] = rk_mk_pte_invalid(pte);
  534. }
  535. rk_table_flush(pte_addr, pte_count);
  536. return pte_count * SPAGE_SIZE;
  537. }
  538. static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
  539. dma_addr_t iova, phys_addr_t paddr, size_t size,
  540. int prot)
  541. {
  542. unsigned int pte_count;
  543. unsigned int pte_total = size / SPAGE_SIZE;
  544. phys_addr_t page_phys;
  545. assert_spin_locked(&rk_domain->dt_lock);
  546. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  547. u32 pte = pte_addr[pte_count];
  548. if (rk_pte_is_page_valid(pte))
  549. goto unwind;
  550. pte_addr[pte_count] = rk_mk_pte(paddr, prot);
  551. paddr += SPAGE_SIZE;
  552. }
  553. rk_table_flush(pte_addr, pte_count);
  554. /*
  555. * Zap the first and last iova to evict from iotlb any previously
  556. * mapped cachelines holding stale values for its dte and pte.
  557. * We only zap the first and last iova, since only they could have
  558. * dte or pte shared with an existing mapping.
  559. */
  560. rk_iommu_zap_iova_first_last(rk_domain, iova, size);
  561. return 0;
  562. unwind:
  563. /* Unmap the range of iovas that we just mapped */
  564. rk_iommu_unmap_iova(rk_domain, pte_addr, iova, pte_count * SPAGE_SIZE);
  565. iova += pte_count * SPAGE_SIZE;
  566. page_phys = rk_pte_page_address(pte_addr[pte_count]);
  567. pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
  568. &iova, &page_phys, &paddr, prot);
  569. return -EADDRINUSE;
  570. }
  571. static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
  572. phys_addr_t paddr, size_t size, int prot)
  573. {
  574. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  575. unsigned long flags;
  576. dma_addr_t iova = (dma_addr_t)_iova;
  577. u32 *page_table, *pte_addr;
  578. int ret;
  579. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  580. /*
  581. * pgsize_bitmap specifies iova sizes that fit in one page table
  582. * (1024 4-KiB pages = 4 MiB).
  583. * So, size will always be 4096 <= size <= 4194304.
  584. * Since iommu_map() guarantees that both iova and size will be
  585. * aligned, we will always only be mapping from a single dte here.
  586. */
  587. page_table = rk_dte_get_page_table(rk_domain, iova);
  588. if (IS_ERR(page_table)) {
  589. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  590. return PTR_ERR(page_table);
  591. }
  592. pte_addr = &page_table[rk_iova_pte_index(iova)];
  593. ret = rk_iommu_map_iova(rk_domain, pte_addr, iova, paddr, size, prot);
  594. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  595. return ret;
  596. }
  597. static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
  598. size_t size)
  599. {
  600. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  601. unsigned long flags;
  602. dma_addr_t iova = (dma_addr_t)_iova;
  603. phys_addr_t pt_phys;
  604. u32 dte;
  605. u32 *pte_addr;
  606. size_t unmap_size;
  607. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  608. /*
  609. * pgsize_bitmap specifies iova sizes that fit in one page table
  610. * (1024 4-KiB pages = 4 MiB).
  611. * So, size will always be 4096 <= size <= 4194304.
  612. * Since iommu_unmap() guarantees that both iova and size will be
  613. * aligned, we will always only be unmapping from a single dte here.
  614. */
  615. dte = rk_domain->dt[rk_iova_dte_index(iova)];
  616. /* Just return 0 if iova is unmapped */
  617. if (!rk_dte_is_pt_valid(dte)) {
  618. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  619. return 0;
  620. }
  621. pt_phys = rk_dte_pt_address(dte);
  622. pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
  623. unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, iova, size);
  624. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  625. /* Shootdown iotlb entries for iova range that was just unmapped */
  626. rk_iommu_zap_iova(rk_domain, iova, unmap_size);
  627. return unmap_size;
  628. }
  629. static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
  630. {
  631. struct iommu_group *group;
  632. struct device *iommu_dev;
  633. struct rk_iommu *rk_iommu;
  634. group = iommu_group_get(dev);
  635. if (!group)
  636. return NULL;
  637. iommu_dev = iommu_group_get_iommudata(group);
  638. rk_iommu = dev_get_drvdata(iommu_dev);
  639. iommu_group_put(group);
  640. return rk_iommu;
  641. }
  642. static int rk_iommu_attach_device(struct iommu_domain *domain,
  643. struct device *dev)
  644. {
  645. struct rk_iommu *iommu;
  646. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  647. unsigned long flags;
  648. int ret, i;
  649. phys_addr_t dte_addr;
  650. /*
  651. * Allow 'virtual devices' (e.g., drm) to attach to domain.
  652. * Such a device does not belong to an iommu group.
  653. */
  654. iommu = rk_iommu_from_dev(dev);
  655. if (!iommu)
  656. return 0;
  657. ret = rk_iommu_enable_stall(iommu);
  658. if (ret)
  659. return ret;
  660. ret = rk_iommu_force_reset(iommu);
  661. if (ret)
  662. return ret;
  663. iommu->domain = domain;
  664. ret = devm_request_irq(dev, iommu->irq, rk_iommu_irq,
  665. IRQF_SHARED, dev_name(dev), iommu);
  666. if (ret)
  667. return ret;
  668. dte_addr = virt_to_phys(rk_domain->dt);
  669. for (i = 0; i < iommu->num_mmu; i++) {
  670. rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, dte_addr);
  671. rk_iommu_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
  672. rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
  673. }
  674. ret = rk_iommu_enable_paging(iommu);
  675. if (ret)
  676. return ret;
  677. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  678. list_add_tail(&iommu->node, &rk_domain->iommus);
  679. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  680. dev_dbg(dev, "Attached to iommu domain\n");
  681. rk_iommu_disable_stall(iommu);
  682. return 0;
  683. }
  684. static void rk_iommu_detach_device(struct iommu_domain *domain,
  685. struct device *dev)
  686. {
  687. struct rk_iommu *iommu;
  688. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  689. unsigned long flags;
  690. int i;
  691. /* Allow 'virtual devices' (eg drm) to detach from domain */
  692. iommu = rk_iommu_from_dev(dev);
  693. if (!iommu)
  694. return;
  695. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  696. list_del_init(&iommu->node);
  697. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  698. /* Ignore error while disabling, just keep going */
  699. rk_iommu_enable_stall(iommu);
  700. rk_iommu_disable_paging(iommu);
  701. for (i = 0; i < iommu->num_mmu; i++) {
  702. rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
  703. rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
  704. }
  705. rk_iommu_disable_stall(iommu);
  706. devm_free_irq(dev, iommu->irq, iommu);
  707. iommu->domain = NULL;
  708. dev_dbg(dev, "Detached from iommu domain\n");
  709. }
  710. static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
  711. {
  712. struct rk_iommu_domain *rk_domain;
  713. if (type != IOMMU_DOMAIN_UNMANAGED)
  714. return NULL;
  715. rk_domain = kzalloc(sizeof(*rk_domain), GFP_KERNEL);
  716. if (!rk_domain)
  717. return NULL;
  718. /*
  719. * rk32xx iommus use a 2 level pagetable.
  720. * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
  721. * Allocate one 4 KiB page for each table.
  722. */
  723. rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
  724. if (!rk_domain->dt)
  725. goto err_dt;
  726. rk_table_flush(rk_domain->dt, NUM_DT_ENTRIES);
  727. spin_lock_init(&rk_domain->iommus_lock);
  728. spin_lock_init(&rk_domain->dt_lock);
  729. INIT_LIST_HEAD(&rk_domain->iommus);
  730. return &rk_domain->domain;
  731. err_dt:
  732. kfree(rk_domain);
  733. return NULL;
  734. }
  735. static void rk_iommu_domain_free(struct iommu_domain *domain)
  736. {
  737. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  738. int i;
  739. WARN_ON(!list_empty(&rk_domain->iommus));
  740. for (i = 0; i < NUM_DT_ENTRIES; i++) {
  741. u32 dte = rk_domain->dt[i];
  742. if (rk_dte_is_pt_valid(dte)) {
  743. phys_addr_t pt_phys = rk_dte_pt_address(dte);
  744. u32 *page_table = phys_to_virt(pt_phys);
  745. free_page((unsigned long)page_table);
  746. }
  747. }
  748. free_page((unsigned long)rk_domain->dt);
  749. kfree(rk_domain);
  750. }
  751. static bool rk_iommu_is_dev_iommu_master(struct device *dev)
  752. {
  753. struct device_node *np = dev->of_node;
  754. int ret;
  755. /*
  756. * An iommu master has an iommus property containing a list of phandles
  757. * to iommu nodes, each with an #iommu-cells property with value 0.
  758. */
  759. ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells");
  760. return (ret > 0);
  761. }
  762. static int rk_iommu_group_set_iommudata(struct iommu_group *group,
  763. struct device *dev)
  764. {
  765. struct device_node *np = dev->of_node;
  766. struct platform_device *pd;
  767. int ret;
  768. struct of_phandle_args args;
  769. /*
  770. * An iommu master has an iommus property containing a list of phandles
  771. * to iommu nodes, each with an #iommu-cells property with value 0.
  772. */
  773. ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0,
  774. &args);
  775. if (ret) {
  776. dev_err(dev, "of_parse_phandle_with_args(%s) => %d\n",
  777. np->full_name, ret);
  778. return ret;
  779. }
  780. if (args.args_count != 0) {
  781. dev_err(dev, "incorrect number of iommu params found for %s (found %d, expected 0)\n",
  782. args.np->full_name, args.args_count);
  783. return -EINVAL;
  784. }
  785. pd = of_find_device_by_node(args.np);
  786. of_node_put(args.np);
  787. if (!pd) {
  788. dev_err(dev, "iommu %s not found\n", args.np->full_name);
  789. return -EPROBE_DEFER;
  790. }
  791. /* TODO(djkurtz): handle multiple slave iommus for a single master */
  792. iommu_group_set_iommudata(group, &pd->dev, NULL);
  793. return 0;
  794. }
  795. static int rk_iommu_add_device(struct device *dev)
  796. {
  797. struct iommu_group *group;
  798. int ret;
  799. if (!rk_iommu_is_dev_iommu_master(dev))
  800. return -ENODEV;
  801. group = iommu_group_get(dev);
  802. if (!group) {
  803. group = iommu_group_alloc();
  804. if (IS_ERR(group)) {
  805. dev_err(dev, "Failed to allocate IOMMU group\n");
  806. return PTR_ERR(group);
  807. }
  808. }
  809. ret = iommu_group_add_device(group, dev);
  810. if (ret)
  811. goto err_put_group;
  812. ret = rk_iommu_group_set_iommudata(group, dev);
  813. if (ret)
  814. goto err_remove_device;
  815. iommu_group_put(group);
  816. return 0;
  817. err_remove_device:
  818. iommu_group_remove_device(dev);
  819. err_put_group:
  820. iommu_group_put(group);
  821. return ret;
  822. }
  823. static void rk_iommu_remove_device(struct device *dev)
  824. {
  825. if (!rk_iommu_is_dev_iommu_master(dev))
  826. return;
  827. iommu_group_remove_device(dev);
  828. }
  829. static const struct iommu_ops rk_iommu_ops = {
  830. .domain_alloc = rk_iommu_domain_alloc,
  831. .domain_free = rk_iommu_domain_free,
  832. .attach_dev = rk_iommu_attach_device,
  833. .detach_dev = rk_iommu_detach_device,
  834. .map = rk_iommu_map,
  835. .unmap = rk_iommu_unmap,
  836. .add_device = rk_iommu_add_device,
  837. .remove_device = rk_iommu_remove_device,
  838. .iova_to_phys = rk_iommu_iova_to_phys,
  839. .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
  840. };
  841. static int rk_iommu_probe(struct platform_device *pdev)
  842. {
  843. struct device *dev = &pdev->dev;
  844. struct rk_iommu *iommu;
  845. struct resource *res;
  846. int i;
  847. iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
  848. if (!iommu)
  849. return -ENOMEM;
  850. platform_set_drvdata(pdev, iommu);
  851. iommu->dev = dev;
  852. iommu->num_mmu = 0;
  853. iommu->bases = devm_kzalloc(dev, sizeof(*iommu->bases) * iommu->num_mmu,
  854. GFP_KERNEL);
  855. if (!iommu->bases)
  856. return -ENOMEM;
  857. for (i = 0; i < pdev->num_resources; i++) {
  858. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  859. iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
  860. if (IS_ERR(iommu->bases[i]))
  861. continue;
  862. iommu->num_mmu++;
  863. }
  864. if (iommu->num_mmu == 0)
  865. return PTR_ERR(iommu->bases[0]);
  866. iommu->irq = platform_get_irq(pdev, 0);
  867. if (iommu->irq < 0) {
  868. dev_err(dev, "Failed to get IRQ, %d\n", iommu->irq);
  869. return -ENXIO;
  870. }
  871. return 0;
  872. }
  873. static int rk_iommu_remove(struct platform_device *pdev)
  874. {
  875. return 0;
  876. }
  877. static const struct of_device_id rk_iommu_dt_ids[] = {
  878. { .compatible = "rockchip,iommu" },
  879. { /* sentinel */ }
  880. };
  881. MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);
  882. static struct platform_driver rk_iommu_driver = {
  883. .probe = rk_iommu_probe,
  884. .remove = rk_iommu_remove,
  885. .driver = {
  886. .name = "rk_iommu",
  887. .of_match_table = rk_iommu_dt_ids,
  888. },
  889. };
  890. static int __init rk_iommu_init(void)
  891. {
  892. struct device_node *np;
  893. int ret;
  894. np = of_find_matching_node(NULL, rk_iommu_dt_ids);
  895. if (!np)
  896. return 0;
  897. of_node_put(np);
  898. ret = bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
  899. if (ret)
  900. return ret;
  901. return platform_driver_register(&rk_iommu_driver);
  902. }
  903. static void __exit rk_iommu_exit(void)
  904. {
  905. platform_driver_unregister(&rk_iommu_driver);
  906. }
  907. subsys_initcall(rk_iommu_init);
  908. module_exit(rk_iommu_exit);
  909. MODULE_DESCRIPTION("IOMMU API for Rockchip");
  910. MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
  911. MODULE_ALIAS("platform:rockchip-iommu");
  912. MODULE_LICENSE("GPL v2");