rockchip-iommu.c 34 KB

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