rockchip-iommu.c 34 KB

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