rockchip-iommu.c 33 KB

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