rockchip-iommu.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
  460. dma_addr_t iova)
  461. {
  462. u32 *page_table, *dte_addr;
  463. u32 dte;
  464. phys_addr_t pt_phys;
  465. assert_spin_locked(&rk_domain->dt_lock);
  466. dte_addr = &rk_domain->dt[rk_iova_dte_index(iova)];
  467. dte = *dte_addr;
  468. if (rk_dte_is_pt_valid(dte))
  469. goto done;
  470. page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
  471. if (!page_table)
  472. return ERR_PTR(-ENOMEM);
  473. dte = rk_mk_dte(page_table);
  474. *dte_addr = dte;
  475. rk_table_flush(page_table, NUM_PT_ENTRIES);
  476. rk_table_flush(dte_addr, 1);
  477. /*
  478. * Zap the first iova of newly allocated page table so iommu evicts
  479. * old cached value of new dte from the iotlb.
  480. */
  481. rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
  482. done:
  483. pt_phys = rk_dte_pt_address(dte);
  484. return (u32 *)phys_to_virt(pt_phys);
  485. }
  486. static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
  487. u32 *pte_addr, dma_addr_t iova, size_t size)
  488. {
  489. unsigned int pte_count;
  490. unsigned int pte_total = size / SPAGE_SIZE;
  491. assert_spin_locked(&rk_domain->dt_lock);
  492. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  493. u32 pte = pte_addr[pte_count];
  494. if (!rk_pte_is_page_valid(pte))
  495. break;
  496. pte_addr[pte_count] = rk_mk_pte_invalid(pte);
  497. }
  498. rk_table_flush(pte_addr, pte_count);
  499. return pte_count * SPAGE_SIZE;
  500. }
  501. static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
  502. dma_addr_t iova, phys_addr_t paddr, size_t size,
  503. int prot)
  504. {
  505. unsigned int pte_count;
  506. unsigned int pte_total = size / SPAGE_SIZE;
  507. phys_addr_t page_phys;
  508. assert_spin_locked(&rk_domain->dt_lock);
  509. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  510. u32 pte = pte_addr[pte_count];
  511. if (rk_pte_is_page_valid(pte))
  512. goto unwind;
  513. pte_addr[pte_count] = rk_mk_pte(paddr, prot);
  514. paddr += SPAGE_SIZE;
  515. }
  516. rk_table_flush(pte_addr, pte_count);
  517. return 0;
  518. unwind:
  519. /* Unmap the range of iovas that we just mapped */
  520. rk_iommu_unmap_iova(rk_domain, pte_addr, iova, pte_count * SPAGE_SIZE);
  521. iova += pte_count * SPAGE_SIZE;
  522. page_phys = rk_pte_page_address(pte_addr[pte_count]);
  523. pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
  524. &iova, &page_phys, &paddr, prot);
  525. return -EADDRINUSE;
  526. }
  527. static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
  528. phys_addr_t paddr, size_t size, int prot)
  529. {
  530. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  531. unsigned long flags;
  532. dma_addr_t iova = (dma_addr_t)_iova;
  533. u32 *page_table, *pte_addr;
  534. int ret;
  535. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  536. /*
  537. * pgsize_bitmap specifies iova sizes that fit in one page table
  538. * (1024 4-KiB pages = 4 MiB).
  539. * So, size will always be 4096 <= size <= 4194304.
  540. * Since iommu_map() guarantees that both iova and size will be
  541. * aligned, we will always only be mapping from a single dte here.
  542. */
  543. page_table = rk_dte_get_page_table(rk_domain, iova);
  544. if (IS_ERR(page_table)) {
  545. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  546. return PTR_ERR(page_table);
  547. }
  548. pte_addr = &page_table[rk_iova_pte_index(iova)];
  549. ret = rk_iommu_map_iova(rk_domain, pte_addr, iova, paddr, size, prot);
  550. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  551. return ret;
  552. }
  553. static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
  554. size_t size)
  555. {
  556. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  557. unsigned long flags;
  558. dma_addr_t iova = (dma_addr_t)_iova;
  559. phys_addr_t pt_phys;
  560. u32 dte;
  561. u32 *pte_addr;
  562. size_t unmap_size;
  563. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  564. /*
  565. * pgsize_bitmap specifies iova sizes that fit in one page table
  566. * (1024 4-KiB pages = 4 MiB).
  567. * So, size will always be 4096 <= size <= 4194304.
  568. * Since iommu_unmap() guarantees that both iova and size will be
  569. * aligned, we will always only be unmapping from a single dte here.
  570. */
  571. dte = rk_domain->dt[rk_iova_dte_index(iova)];
  572. /* Just return 0 if iova is unmapped */
  573. if (!rk_dte_is_pt_valid(dte)) {
  574. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  575. return 0;
  576. }
  577. pt_phys = rk_dte_pt_address(dte);
  578. pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
  579. unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, iova, size);
  580. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  581. /* Shootdown iotlb entries for iova range that was just unmapped */
  582. rk_iommu_zap_iova(rk_domain, iova, unmap_size);
  583. return unmap_size;
  584. }
  585. static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
  586. {
  587. struct iommu_group *group;
  588. struct device *iommu_dev;
  589. struct rk_iommu *rk_iommu;
  590. group = iommu_group_get(dev);
  591. if (!group)
  592. return NULL;
  593. iommu_dev = iommu_group_get_iommudata(group);
  594. rk_iommu = dev_get_drvdata(iommu_dev);
  595. iommu_group_put(group);
  596. return rk_iommu;
  597. }
  598. static int rk_iommu_attach_device(struct iommu_domain *domain,
  599. struct device *dev)
  600. {
  601. struct rk_iommu *iommu;
  602. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  603. unsigned long flags;
  604. int ret;
  605. phys_addr_t dte_addr;
  606. /*
  607. * Allow 'virtual devices' (e.g., drm) to attach to domain.
  608. * Such a device does not belong to an iommu group.
  609. */
  610. iommu = rk_iommu_from_dev(dev);
  611. if (!iommu)
  612. return 0;
  613. ret = rk_iommu_enable_stall(iommu);
  614. if (ret)
  615. return ret;
  616. ret = rk_iommu_force_reset(iommu);
  617. if (ret)
  618. return ret;
  619. iommu->domain = domain;
  620. ret = devm_request_irq(dev, iommu->irq, rk_iommu_irq,
  621. IRQF_SHARED, dev_name(dev), iommu);
  622. if (ret)
  623. return ret;
  624. dte_addr = virt_to_phys(rk_domain->dt);
  625. rk_iommu_write(iommu, RK_MMU_DTE_ADDR, dte_addr);
  626. rk_iommu_command(iommu, RK_MMU_CMD_ZAP_CACHE);
  627. rk_iommu_write(iommu, RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
  628. ret = rk_iommu_enable_paging(iommu);
  629. if (ret)
  630. return ret;
  631. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  632. list_add_tail(&iommu->node, &rk_domain->iommus);
  633. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  634. dev_info(dev, "Attached to iommu domain\n");
  635. rk_iommu_disable_stall(iommu);
  636. return 0;
  637. }
  638. static void rk_iommu_detach_device(struct iommu_domain *domain,
  639. struct device *dev)
  640. {
  641. struct rk_iommu *iommu;
  642. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  643. unsigned long flags;
  644. /* Allow 'virtual devices' (eg drm) to detach from domain */
  645. iommu = rk_iommu_from_dev(dev);
  646. if (!iommu)
  647. return;
  648. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  649. list_del_init(&iommu->node);
  650. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  651. /* Ignore error while disabling, just keep going */
  652. rk_iommu_enable_stall(iommu);
  653. rk_iommu_disable_paging(iommu);
  654. rk_iommu_write(iommu, RK_MMU_INT_MASK, 0);
  655. rk_iommu_write(iommu, RK_MMU_DTE_ADDR, 0);
  656. rk_iommu_disable_stall(iommu);
  657. devm_free_irq(dev, iommu->irq, iommu);
  658. iommu->domain = NULL;
  659. dev_info(dev, "Detached from iommu domain\n");
  660. }
  661. static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
  662. {
  663. struct rk_iommu_domain *rk_domain;
  664. if (type != IOMMU_DOMAIN_UNMANAGED)
  665. return NULL;
  666. rk_domain = kzalloc(sizeof(*rk_domain), GFP_KERNEL);
  667. if (!rk_domain)
  668. return NULL;
  669. /*
  670. * rk32xx iommus use a 2 level pagetable.
  671. * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
  672. * Allocate one 4 KiB page for each table.
  673. */
  674. rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
  675. if (!rk_domain->dt)
  676. goto err_dt;
  677. rk_table_flush(rk_domain->dt, NUM_DT_ENTRIES);
  678. spin_lock_init(&rk_domain->iommus_lock);
  679. spin_lock_init(&rk_domain->dt_lock);
  680. INIT_LIST_HEAD(&rk_domain->iommus);
  681. return &rk_domain->domain;
  682. err_dt:
  683. kfree(rk_domain);
  684. return NULL;
  685. }
  686. static void rk_iommu_domain_free(struct iommu_domain *domain)
  687. {
  688. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  689. int i;
  690. WARN_ON(!list_empty(&rk_domain->iommus));
  691. for (i = 0; i < NUM_DT_ENTRIES; i++) {
  692. u32 dte = rk_domain->dt[i];
  693. if (rk_dte_is_pt_valid(dte)) {
  694. phys_addr_t pt_phys = rk_dte_pt_address(dte);
  695. u32 *page_table = phys_to_virt(pt_phys);
  696. free_page((unsigned long)page_table);
  697. }
  698. }
  699. free_page((unsigned long)rk_domain->dt);
  700. kfree(rk_domain);
  701. }
  702. static bool rk_iommu_is_dev_iommu_master(struct device *dev)
  703. {
  704. struct device_node *np = dev->of_node;
  705. int ret;
  706. /*
  707. * An iommu master has an iommus property containing a list of phandles
  708. * to iommu nodes, each with an #iommu-cells property with value 0.
  709. */
  710. ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells");
  711. return (ret > 0);
  712. }
  713. static int rk_iommu_group_set_iommudata(struct iommu_group *group,
  714. struct device *dev)
  715. {
  716. struct device_node *np = dev->of_node;
  717. struct platform_device *pd;
  718. int ret;
  719. struct of_phandle_args args;
  720. /*
  721. * An iommu master has an iommus property containing a list of phandles
  722. * to iommu nodes, each with an #iommu-cells property with value 0.
  723. */
  724. ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0,
  725. &args);
  726. if (ret) {
  727. dev_err(dev, "of_parse_phandle_with_args(%s) => %d\n",
  728. np->full_name, ret);
  729. return ret;
  730. }
  731. if (args.args_count != 0) {
  732. dev_err(dev, "incorrect number of iommu params found for %s (found %d, expected 0)\n",
  733. args.np->full_name, args.args_count);
  734. return -EINVAL;
  735. }
  736. pd = of_find_device_by_node(args.np);
  737. of_node_put(args.np);
  738. if (!pd) {
  739. dev_err(dev, "iommu %s not found\n", args.np->full_name);
  740. return -EPROBE_DEFER;
  741. }
  742. /* TODO(djkurtz): handle multiple slave iommus for a single master */
  743. iommu_group_set_iommudata(group, &pd->dev, NULL);
  744. return 0;
  745. }
  746. static int rk_iommu_add_device(struct device *dev)
  747. {
  748. struct iommu_group *group;
  749. int ret;
  750. if (!rk_iommu_is_dev_iommu_master(dev))
  751. return -ENODEV;
  752. group = iommu_group_get(dev);
  753. if (!group) {
  754. group = iommu_group_alloc();
  755. if (IS_ERR(group)) {
  756. dev_err(dev, "Failed to allocate IOMMU group\n");
  757. return PTR_ERR(group);
  758. }
  759. }
  760. ret = iommu_group_add_device(group, dev);
  761. if (ret)
  762. goto err_put_group;
  763. ret = rk_iommu_group_set_iommudata(group, dev);
  764. if (ret)
  765. goto err_remove_device;
  766. iommu_group_put(group);
  767. return 0;
  768. err_remove_device:
  769. iommu_group_remove_device(dev);
  770. err_put_group:
  771. iommu_group_put(group);
  772. return ret;
  773. }
  774. static void rk_iommu_remove_device(struct device *dev)
  775. {
  776. if (!rk_iommu_is_dev_iommu_master(dev))
  777. return;
  778. iommu_group_remove_device(dev);
  779. }
  780. static const struct iommu_ops rk_iommu_ops = {
  781. .domain_alloc = rk_iommu_domain_alloc,
  782. .domain_free = rk_iommu_domain_free,
  783. .attach_dev = rk_iommu_attach_device,
  784. .detach_dev = rk_iommu_detach_device,
  785. .map = rk_iommu_map,
  786. .unmap = rk_iommu_unmap,
  787. .add_device = rk_iommu_add_device,
  788. .remove_device = rk_iommu_remove_device,
  789. .iova_to_phys = rk_iommu_iova_to_phys,
  790. .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
  791. };
  792. static int rk_iommu_probe(struct platform_device *pdev)
  793. {
  794. struct device *dev = &pdev->dev;
  795. struct rk_iommu *iommu;
  796. struct resource *res;
  797. iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
  798. if (!iommu)
  799. return -ENOMEM;
  800. platform_set_drvdata(pdev, iommu);
  801. iommu->dev = dev;
  802. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  803. iommu->base = devm_ioremap_resource(&pdev->dev, res);
  804. if (IS_ERR(iommu->base))
  805. return PTR_ERR(iommu->base);
  806. iommu->irq = platform_get_irq(pdev, 0);
  807. if (iommu->irq < 0) {
  808. dev_err(dev, "Failed to get IRQ, %d\n", iommu->irq);
  809. return -ENXIO;
  810. }
  811. return 0;
  812. }
  813. static int rk_iommu_remove(struct platform_device *pdev)
  814. {
  815. return 0;
  816. }
  817. static const struct of_device_id rk_iommu_dt_ids[] = {
  818. { .compatible = "rockchip,iommu" },
  819. { /* sentinel */ }
  820. };
  821. MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);
  822. static struct platform_driver rk_iommu_driver = {
  823. .probe = rk_iommu_probe,
  824. .remove = rk_iommu_remove,
  825. .driver = {
  826. .name = "rk_iommu",
  827. .of_match_table = rk_iommu_dt_ids,
  828. },
  829. };
  830. static int __init rk_iommu_init(void)
  831. {
  832. struct device_node *np;
  833. int ret;
  834. np = of_find_matching_node(NULL, rk_iommu_dt_ids);
  835. if (!np)
  836. return 0;
  837. of_node_put(np);
  838. ret = bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
  839. if (ret)
  840. return ret;
  841. return platform_driver_register(&rk_iommu_driver);
  842. }
  843. static void __exit rk_iommu_exit(void)
  844. {
  845. platform_driver_unregister(&rk_iommu_driver);
  846. }
  847. subsys_initcall(rk_iommu_init);
  848. module_exit(rk_iommu_exit);
  849. MODULE_DESCRIPTION("IOMMU API for Rockchip");
  850. MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
  851. MODULE_ALIAS("platform:rockchip-iommu");
  852. MODULE_LICENSE("GPL v2");