rockchip-iommu.c 28 KB

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