rockchip-iommu.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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, err;
  439. err = pm_runtime_get_if_in_use(iommu->dev);
  440. if (WARN_ON_ONCE(err <= 0))
  441. return ret;
  442. if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks)))
  443. goto out;
  444. for (i = 0; i < iommu->num_mmu; i++) {
  445. int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
  446. if (int_status == 0)
  447. continue;
  448. ret = IRQ_HANDLED;
  449. iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
  450. if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
  451. int flags;
  452. status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
  453. flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ?
  454. IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  455. dev_err(iommu->dev, "Page fault at %pad of type %s\n",
  456. &iova,
  457. (flags == IOMMU_FAULT_WRITE) ? "write" : "read");
  458. log_iova(iommu, i, iova);
  459. /*
  460. * Report page fault to any installed handlers.
  461. * Ignore the return code, though, since we always zap cache
  462. * and clear the page fault anyway.
  463. */
  464. if (iommu->domain)
  465. report_iommu_fault(iommu->domain, iommu->dev, iova,
  466. flags);
  467. else
  468. dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
  469. rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
  470. rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
  471. }
  472. if (int_status & RK_MMU_IRQ_BUS_ERROR)
  473. dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
  474. if (int_status & ~RK_MMU_IRQ_MASK)
  475. dev_err(iommu->dev, "unexpected int_status: %#08x\n",
  476. int_status);
  477. rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
  478. }
  479. clk_bulk_disable(iommu->num_clocks, iommu->clocks);
  480. out:
  481. pm_runtime_put(iommu->dev);
  482. return ret;
  483. }
  484. static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain,
  485. dma_addr_t iova)
  486. {
  487. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  488. unsigned long flags;
  489. phys_addr_t pt_phys, phys = 0;
  490. u32 dte, pte;
  491. u32 *page_table;
  492. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  493. dte = rk_domain->dt[rk_iova_dte_index(iova)];
  494. if (!rk_dte_is_pt_valid(dte))
  495. goto out;
  496. pt_phys = rk_dte_pt_address(dte);
  497. page_table = (u32 *)phys_to_virt(pt_phys);
  498. pte = page_table[rk_iova_pte_index(iova)];
  499. if (!rk_pte_is_page_valid(pte))
  500. goto out;
  501. phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
  502. out:
  503. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  504. return phys;
  505. }
  506. static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain,
  507. dma_addr_t iova, size_t size)
  508. {
  509. struct list_head *pos;
  510. unsigned long flags;
  511. /* shootdown these iova from all iommus using this domain */
  512. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  513. list_for_each(pos, &rk_domain->iommus) {
  514. struct rk_iommu *iommu;
  515. int ret;
  516. iommu = list_entry(pos, struct rk_iommu, node);
  517. /* Only zap TLBs of IOMMUs that are powered on. */
  518. ret = pm_runtime_get_if_in_use(iommu->dev);
  519. if (WARN_ON_ONCE(ret < 0))
  520. continue;
  521. if (ret) {
  522. WARN_ON(clk_bulk_enable(iommu->num_clocks,
  523. iommu->clocks));
  524. rk_iommu_zap_lines(iommu, iova, size);
  525. clk_bulk_disable(iommu->num_clocks, iommu->clocks);
  526. pm_runtime_put(iommu->dev);
  527. }
  528. }
  529. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  530. }
  531. static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
  532. dma_addr_t iova, size_t size)
  533. {
  534. rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
  535. if (size > SPAGE_SIZE)
  536. rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE,
  537. SPAGE_SIZE);
  538. }
  539. static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
  540. dma_addr_t iova)
  541. {
  542. u32 *page_table, *dte_addr;
  543. u32 dte_index, dte;
  544. phys_addr_t pt_phys;
  545. dma_addr_t pt_dma;
  546. assert_spin_locked(&rk_domain->dt_lock);
  547. dte_index = rk_iova_dte_index(iova);
  548. dte_addr = &rk_domain->dt[dte_index];
  549. dte = *dte_addr;
  550. if (rk_dte_is_pt_valid(dte))
  551. goto done;
  552. page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
  553. if (!page_table)
  554. return ERR_PTR(-ENOMEM);
  555. pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
  556. if (dma_mapping_error(dma_dev, pt_dma)) {
  557. dev_err(dma_dev, "DMA mapping error while allocating page table\n");
  558. free_page((unsigned long)page_table);
  559. return ERR_PTR(-ENOMEM);
  560. }
  561. dte = rk_mk_dte(pt_dma);
  562. *dte_addr = dte;
  563. rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
  564. rk_table_flush(rk_domain,
  565. rk_domain->dt_dma + dte_index * sizeof(u32), 1);
  566. done:
  567. pt_phys = rk_dte_pt_address(dte);
  568. return (u32 *)phys_to_virt(pt_phys);
  569. }
  570. static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
  571. u32 *pte_addr, dma_addr_t pte_dma,
  572. size_t size)
  573. {
  574. unsigned int pte_count;
  575. unsigned int pte_total = size / SPAGE_SIZE;
  576. assert_spin_locked(&rk_domain->dt_lock);
  577. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  578. u32 pte = pte_addr[pte_count];
  579. if (!rk_pte_is_page_valid(pte))
  580. break;
  581. pte_addr[pte_count] = rk_mk_pte_invalid(pte);
  582. }
  583. rk_table_flush(rk_domain, pte_dma, pte_count);
  584. return pte_count * SPAGE_SIZE;
  585. }
  586. static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
  587. dma_addr_t pte_dma, dma_addr_t iova,
  588. phys_addr_t paddr, size_t size, int prot)
  589. {
  590. unsigned int pte_count;
  591. unsigned int pte_total = size / SPAGE_SIZE;
  592. phys_addr_t page_phys;
  593. assert_spin_locked(&rk_domain->dt_lock);
  594. for (pte_count = 0; pte_count < pte_total; pte_count++) {
  595. u32 pte = pte_addr[pte_count];
  596. if (rk_pte_is_page_valid(pte))
  597. goto unwind;
  598. pte_addr[pte_count] = rk_mk_pte(paddr, prot);
  599. paddr += SPAGE_SIZE;
  600. }
  601. rk_table_flush(rk_domain, pte_dma, pte_total);
  602. /*
  603. * Zap the first and last iova to evict from iotlb any previously
  604. * mapped cachelines holding stale values for its dte and pte.
  605. * We only zap the first and last iova, since only they could have
  606. * dte or pte shared with an existing mapping.
  607. */
  608. rk_iommu_zap_iova_first_last(rk_domain, iova, size);
  609. return 0;
  610. unwind:
  611. /* Unmap the range of iovas that we just mapped */
  612. rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma,
  613. pte_count * SPAGE_SIZE);
  614. iova += pte_count * SPAGE_SIZE;
  615. page_phys = rk_pte_page_address(pte_addr[pte_count]);
  616. pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
  617. &iova, &page_phys, &paddr, prot);
  618. return -EADDRINUSE;
  619. }
  620. static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
  621. phys_addr_t paddr, size_t size, int prot)
  622. {
  623. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  624. unsigned long flags;
  625. dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
  626. u32 *page_table, *pte_addr;
  627. u32 dte_index, pte_index;
  628. int ret;
  629. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  630. /*
  631. * pgsize_bitmap specifies iova sizes that fit in one page table
  632. * (1024 4-KiB pages = 4 MiB).
  633. * So, size will always be 4096 <= size <= 4194304.
  634. * Since iommu_map() guarantees that both iova and size will be
  635. * aligned, we will always only be mapping from a single dte here.
  636. */
  637. page_table = rk_dte_get_page_table(rk_domain, iova);
  638. if (IS_ERR(page_table)) {
  639. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  640. return PTR_ERR(page_table);
  641. }
  642. dte_index = rk_domain->dt[rk_iova_dte_index(iova)];
  643. pte_index = rk_iova_pte_index(iova);
  644. pte_addr = &page_table[pte_index];
  645. pte_dma = rk_dte_pt_address(dte_index) + pte_index * sizeof(u32);
  646. ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova,
  647. paddr, size, prot);
  648. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  649. return ret;
  650. }
  651. static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
  652. size_t size)
  653. {
  654. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  655. unsigned long flags;
  656. dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
  657. phys_addr_t pt_phys;
  658. u32 dte;
  659. u32 *pte_addr;
  660. size_t unmap_size;
  661. spin_lock_irqsave(&rk_domain->dt_lock, flags);
  662. /*
  663. * pgsize_bitmap specifies iova sizes that fit in one page table
  664. * (1024 4-KiB pages = 4 MiB).
  665. * So, size will always be 4096 <= size <= 4194304.
  666. * Since iommu_unmap() guarantees that both iova and size will be
  667. * aligned, we will always only be unmapping from a single dte here.
  668. */
  669. dte = rk_domain->dt[rk_iova_dte_index(iova)];
  670. /* Just return 0 if iova is unmapped */
  671. if (!rk_dte_is_pt_valid(dte)) {
  672. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  673. return 0;
  674. }
  675. pt_phys = rk_dte_pt_address(dte);
  676. pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
  677. pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
  678. unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
  679. spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
  680. /* Shootdown iotlb entries for iova range that was just unmapped */
  681. rk_iommu_zap_iova(rk_domain, iova, unmap_size);
  682. return unmap_size;
  683. }
  684. static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
  685. {
  686. struct rk_iommudata *data = dev->archdata.iommu;
  687. return data ? data->iommu : NULL;
  688. }
  689. /* Must be called with iommu powered on and attached */
  690. static void rk_iommu_disable(struct rk_iommu *iommu)
  691. {
  692. int i;
  693. /* Ignore error while disabling, just keep going */
  694. WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks));
  695. rk_iommu_enable_stall(iommu);
  696. rk_iommu_disable_paging(iommu);
  697. for (i = 0; i < iommu->num_mmu; i++) {
  698. rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
  699. rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
  700. }
  701. rk_iommu_disable_stall(iommu);
  702. clk_bulk_disable(iommu->num_clocks, iommu->clocks);
  703. }
  704. /* Must be called with iommu powered on and attached */
  705. static int rk_iommu_enable(struct rk_iommu *iommu)
  706. {
  707. struct iommu_domain *domain = iommu->domain;
  708. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  709. int ret, i;
  710. ret = clk_bulk_enable(iommu->num_clocks, iommu->clocks);
  711. if (ret)
  712. return ret;
  713. ret = rk_iommu_enable_stall(iommu);
  714. if (ret)
  715. goto out_disable_clocks;
  716. ret = rk_iommu_force_reset(iommu);
  717. if (ret)
  718. goto out_disable_stall;
  719. for (i = 0; i < iommu->num_mmu; i++) {
  720. rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR,
  721. rk_domain->dt_dma);
  722. rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
  723. rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
  724. }
  725. ret = rk_iommu_enable_paging(iommu);
  726. out_disable_stall:
  727. rk_iommu_disable_stall(iommu);
  728. out_disable_clocks:
  729. clk_bulk_disable(iommu->num_clocks, iommu->clocks);
  730. return ret;
  731. }
  732. static void rk_iommu_detach_device(struct iommu_domain *domain,
  733. struct device *dev)
  734. {
  735. struct rk_iommu *iommu;
  736. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  737. unsigned long flags;
  738. int ret;
  739. /* Allow 'virtual devices' (eg drm) to detach from domain */
  740. iommu = rk_iommu_from_dev(dev);
  741. if (!iommu)
  742. return;
  743. dev_dbg(dev, "Detaching from iommu domain\n");
  744. /* iommu already detached */
  745. if (iommu->domain != domain)
  746. return;
  747. iommu->domain = NULL;
  748. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  749. list_del_init(&iommu->node);
  750. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  751. ret = pm_runtime_get_if_in_use(iommu->dev);
  752. WARN_ON_ONCE(ret < 0);
  753. if (ret > 0) {
  754. rk_iommu_disable(iommu);
  755. pm_runtime_put(iommu->dev);
  756. }
  757. }
  758. static int rk_iommu_attach_device(struct iommu_domain *domain,
  759. struct device *dev)
  760. {
  761. struct rk_iommu *iommu;
  762. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  763. unsigned long flags;
  764. int ret;
  765. /*
  766. * Allow 'virtual devices' (e.g., drm) to attach to domain.
  767. * Such a device does not belong to an iommu group.
  768. */
  769. iommu = rk_iommu_from_dev(dev);
  770. if (!iommu)
  771. return 0;
  772. dev_dbg(dev, "Attaching to iommu domain\n");
  773. /* iommu already attached */
  774. if (iommu->domain == domain)
  775. return 0;
  776. if (iommu->domain)
  777. rk_iommu_detach_device(iommu->domain, dev);
  778. iommu->domain = domain;
  779. spin_lock_irqsave(&rk_domain->iommus_lock, flags);
  780. list_add_tail(&iommu->node, &rk_domain->iommus);
  781. spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
  782. ret = pm_runtime_get_if_in_use(iommu->dev);
  783. if (!ret || WARN_ON_ONCE(ret < 0))
  784. return 0;
  785. ret = rk_iommu_enable(iommu);
  786. if (ret)
  787. rk_iommu_detach_device(iommu->domain, dev);
  788. pm_runtime_put(iommu->dev);
  789. return ret;
  790. }
  791. static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
  792. {
  793. struct rk_iommu_domain *rk_domain;
  794. if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
  795. return NULL;
  796. if (!dma_dev)
  797. return NULL;
  798. rk_domain = kzalloc(sizeof(*rk_domain), GFP_KERNEL);
  799. if (!rk_domain)
  800. return NULL;
  801. if (type == IOMMU_DOMAIN_DMA &&
  802. iommu_get_dma_cookie(&rk_domain->domain))
  803. goto err_free_domain;
  804. /*
  805. * rk32xx iommus use a 2 level pagetable.
  806. * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
  807. * Allocate one 4 KiB page for each table.
  808. */
  809. rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
  810. if (!rk_domain->dt)
  811. goto err_put_cookie;
  812. rk_domain->dt_dma = dma_map_single(dma_dev, rk_domain->dt,
  813. SPAGE_SIZE, DMA_TO_DEVICE);
  814. if (dma_mapping_error(dma_dev, rk_domain->dt_dma)) {
  815. dev_err(dma_dev, "DMA map error for DT\n");
  816. goto err_free_dt;
  817. }
  818. rk_table_flush(rk_domain, rk_domain->dt_dma, NUM_DT_ENTRIES);
  819. spin_lock_init(&rk_domain->iommus_lock);
  820. spin_lock_init(&rk_domain->dt_lock);
  821. INIT_LIST_HEAD(&rk_domain->iommus);
  822. rk_domain->domain.geometry.aperture_start = 0;
  823. rk_domain->domain.geometry.aperture_end = DMA_BIT_MASK(32);
  824. rk_domain->domain.geometry.force_aperture = true;
  825. return &rk_domain->domain;
  826. err_free_dt:
  827. free_page((unsigned long)rk_domain->dt);
  828. err_put_cookie:
  829. if (type == IOMMU_DOMAIN_DMA)
  830. iommu_put_dma_cookie(&rk_domain->domain);
  831. err_free_domain:
  832. kfree(rk_domain);
  833. return NULL;
  834. }
  835. static void rk_iommu_domain_free(struct iommu_domain *domain)
  836. {
  837. struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
  838. int i;
  839. WARN_ON(!list_empty(&rk_domain->iommus));
  840. for (i = 0; i < NUM_DT_ENTRIES; i++) {
  841. u32 dte = rk_domain->dt[i];
  842. if (rk_dte_is_pt_valid(dte)) {
  843. phys_addr_t pt_phys = rk_dte_pt_address(dte);
  844. u32 *page_table = phys_to_virt(pt_phys);
  845. dma_unmap_single(dma_dev, pt_phys,
  846. SPAGE_SIZE, DMA_TO_DEVICE);
  847. free_page((unsigned long)page_table);
  848. }
  849. }
  850. dma_unmap_single(dma_dev, rk_domain->dt_dma,
  851. SPAGE_SIZE, DMA_TO_DEVICE);
  852. free_page((unsigned long)rk_domain->dt);
  853. if (domain->type == IOMMU_DOMAIN_DMA)
  854. iommu_put_dma_cookie(&rk_domain->domain);
  855. kfree(rk_domain);
  856. }
  857. static int rk_iommu_add_device(struct device *dev)
  858. {
  859. struct iommu_group *group;
  860. struct rk_iommu *iommu;
  861. struct rk_iommudata *data;
  862. data = dev->archdata.iommu;
  863. if (!data)
  864. return -ENODEV;
  865. iommu = rk_iommu_from_dev(dev);
  866. group = iommu_group_get_for_dev(dev);
  867. if (IS_ERR(group))
  868. return PTR_ERR(group);
  869. iommu_group_put(group);
  870. iommu_device_link(&iommu->iommu, dev);
  871. data->link = device_link_add(dev, iommu->dev, DL_FLAG_PM_RUNTIME);
  872. return 0;
  873. }
  874. static void rk_iommu_remove_device(struct device *dev)
  875. {
  876. struct rk_iommu *iommu;
  877. struct rk_iommudata *data = dev->archdata.iommu;
  878. iommu = rk_iommu_from_dev(dev);
  879. device_link_del(data->link);
  880. iommu_device_unlink(&iommu->iommu, dev);
  881. iommu_group_remove_device(dev);
  882. }
  883. static struct iommu_group *rk_iommu_device_group(struct device *dev)
  884. {
  885. struct rk_iommu *iommu;
  886. iommu = rk_iommu_from_dev(dev);
  887. return iommu_group_ref_get(iommu->group);
  888. }
  889. static int rk_iommu_of_xlate(struct device *dev,
  890. struct of_phandle_args *args)
  891. {
  892. struct platform_device *iommu_dev;
  893. struct rk_iommudata *data;
  894. data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL);
  895. if (!data)
  896. return -ENOMEM;
  897. iommu_dev = of_find_device_by_node(args->np);
  898. data->iommu = platform_get_drvdata(iommu_dev);
  899. dev->archdata.iommu = data;
  900. platform_device_put(iommu_dev);
  901. return 0;
  902. }
  903. static const struct iommu_ops rk_iommu_ops = {
  904. .domain_alloc = rk_iommu_domain_alloc,
  905. .domain_free = rk_iommu_domain_free,
  906. .attach_dev = rk_iommu_attach_device,
  907. .detach_dev = rk_iommu_detach_device,
  908. .map = rk_iommu_map,
  909. .unmap = rk_iommu_unmap,
  910. .add_device = rk_iommu_add_device,
  911. .remove_device = rk_iommu_remove_device,
  912. .iova_to_phys = rk_iommu_iova_to_phys,
  913. .device_group = rk_iommu_device_group,
  914. .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
  915. .of_xlate = rk_iommu_of_xlate,
  916. };
  917. static int rk_iommu_probe(struct platform_device *pdev)
  918. {
  919. struct device *dev = &pdev->dev;
  920. struct rk_iommu *iommu;
  921. struct resource *res;
  922. int num_res = pdev->num_resources;
  923. int err, i, irq;
  924. iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
  925. if (!iommu)
  926. return -ENOMEM;
  927. platform_set_drvdata(pdev, iommu);
  928. iommu->dev = dev;
  929. iommu->num_mmu = 0;
  930. iommu->bases = devm_kcalloc(dev, num_res, sizeof(*iommu->bases),
  931. GFP_KERNEL);
  932. if (!iommu->bases)
  933. return -ENOMEM;
  934. for (i = 0; i < num_res; i++) {
  935. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  936. if (!res)
  937. continue;
  938. iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
  939. if (IS_ERR(iommu->bases[i]))
  940. continue;
  941. iommu->num_mmu++;
  942. }
  943. if (iommu->num_mmu == 0)
  944. return PTR_ERR(iommu->bases[0]);
  945. iommu->reset_disabled = device_property_read_bool(dev,
  946. "rockchip,disable-mmu-reset");
  947. iommu->num_clocks = ARRAY_SIZE(rk_iommu_clocks);
  948. iommu->clocks = devm_kcalloc(iommu->dev, iommu->num_clocks,
  949. sizeof(*iommu->clocks), GFP_KERNEL);
  950. if (!iommu->clocks)
  951. return -ENOMEM;
  952. for (i = 0; i < iommu->num_clocks; ++i)
  953. iommu->clocks[i].id = rk_iommu_clocks[i];
  954. /*
  955. * iommu clocks should be present for all new devices and devicetrees
  956. * but there are older devicetrees without clocks out in the wild.
  957. * So clocks as optional for the time being.
  958. */
  959. err = devm_clk_bulk_get(iommu->dev, iommu->num_clocks, iommu->clocks);
  960. if (err == -ENOENT)
  961. iommu->num_clocks = 0;
  962. else if (err)
  963. return err;
  964. err = clk_bulk_prepare(iommu->num_clocks, iommu->clocks);
  965. if (err)
  966. return err;
  967. iommu->group = iommu_group_alloc();
  968. if (IS_ERR(iommu->group)) {
  969. err = PTR_ERR(iommu->group);
  970. goto err_unprepare_clocks;
  971. }
  972. err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev));
  973. if (err)
  974. goto err_put_group;
  975. iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops);
  976. iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode);
  977. err = iommu_device_register(&iommu->iommu);
  978. if (err)
  979. goto err_remove_sysfs;
  980. /*
  981. * Use the first registered IOMMU device for domain to use with DMA
  982. * API, since a domain might not physically correspond to a single
  983. * IOMMU device..
  984. */
  985. if (!dma_dev)
  986. dma_dev = &pdev->dev;
  987. bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
  988. pm_runtime_enable(dev);
  989. i = 0;
  990. while ((irq = platform_get_irq(pdev, i++)) != -ENXIO) {
  991. if (irq < 0)
  992. return irq;
  993. err = devm_request_irq(iommu->dev, irq, rk_iommu_irq,
  994. IRQF_SHARED, dev_name(dev), iommu);
  995. if (err) {
  996. pm_runtime_disable(dev);
  997. goto err_remove_sysfs;
  998. }
  999. }
  1000. return 0;
  1001. err_remove_sysfs:
  1002. iommu_device_sysfs_remove(&iommu->iommu);
  1003. err_put_group:
  1004. iommu_group_put(iommu->group);
  1005. err_unprepare_clocks:
  1006. clk_bulk_unprepare(iommu->num_clocks, iommu->clocks);
  1007. return err;
  1008. }
  1009. static void rk_iommu_shutdown(struct platform_device *pdev)
  1010. {
  1011. struct rk_iommu *iommu = platform_get_drvdata(pdev);
  1012. int i = 0, irq;
  1013. while ((irq = platform_get_irq(pdev, i++)) != -ENXIO)
  1014. devm_free_irq(iommu->dev, irq, iommu);
  1015. pm_runtime_force_suspend(&pdev->dev);
  1016. }
  1017. static int __maybe_unused rk_iommu_suspend(struct device *dev)
  1018. {
  1019. struct rk_iommu *iommu = dev_get_drvdata(dev);
  1020. if (!iommu->domain)
  1021. return 0;
  1022. rk_iommu_disable(iommu);
  1023. return 0;
  1024. }
  1025. static int __maybe_unused rk_iommu_resume(struct device *dev)
  1026. {
  1027. struct rk_iommu *iommu = dev_get_drvdata(dev);
  1028. if (!iommu->domain)
  1029. return 0;
  1030. return rk_iommu_enable(iommu);
  1031. }
  1032. static const struct dev_pm_ops rk_iommu_pm_ops = {
  1033. SET_RUNTIME_PM_OPS(rk_iommu_suspend, rk_iommu_resume, NULL)
  1034. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1035. pm_runtime_force_resume)
  1036. };
  1037. static const struct of_device_id rk_iommu_dt_ids[] = {
  1038. { .compatible = "rockchip,iommu" },
  1039. { /* sentinel */ }
  1040. };
  1041. MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);
  1042. static struct platform_driver rk_iommu_driver = {
  1043. .probe = rk_iommu_probe,
  1044. .shutdown = rk_iommu_shutdown,
  1045. .driver = {
  1046. .name = "rk_iommu",
  1047. .of_match_table = rk_iommu_dt_ids,
  1048. .pm = &rk_iommu_pm_ops,
  1049. .suppress_bind_attrs = true,
  1050. },
  1051. };
  1052. static int __init rk_iommu_init(void)
  1053. {
  1054. return platform_driver_register(&rk_iommu_driver);
  1055. }
  1056. subsys_initcall(rk_iommu_init);
  1057. MODULE_DESCRIPTION("IOMMU API for Rockchip");
  1058. MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
  1059. MODULE_ALIAS("platform:rockchip-iommu");
  1060. MODULE_LICENSE("GPL v2");