rockchip-iommu.c 37 KB

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