rockchip-iommu.c 36 KB

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