rockchip-iommu.c 27 KB

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