dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x driver
  4. *
  5. * Copyright (c) 2010-2013, NVIDIA Corporation.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/io.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/slab.h>
  18. #include <soc/tegra/common.h>
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/host1x.h>
  21. #undef CREATE_TRACE_POINTS
  22. #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
  23. #include <asm/dma-iommu.h>
  24. #endif
  25. #include "bus.h"
  26. #include "channel.h"
  27. #include "context.h"
  28. #include "debug.h"
  29. #include "dev.h"
  30. #include "intr.h"
  31. #include "hw/host1x01.h"
  32. #include "hw/host1x02.h"
  33. #include "hw/host1x04.h"
  34. #include "hw/host1x05.h"
  35. #include "hw/host1x06.h"
  36. #include "hw/host1x07.h"
  37. #include "hw/host1x08.h"
  38. void host1x_common_writel(struct host1x *host1x, u32 v, u32 r)
  39. {
  40. writel(v, host1x->common_regs + r);
  41. }
  42. void host1x_hypervisor_writel(struct host1x *host1x, u32 v, u32 r)
  43. {
  44. writel(v, host1x->hv_regs + r);
  45. }
  46. u32 host1x_hypervisor_readl(struct host1x *host1x, u32 r)
  47. {
  48. return readl(host1x->hv_regs + r);
  49. }
  50. void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
  51. {
  52. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  53. writel(v, sync_regs + r);
  54. }
  55. u32 host1x_sync_readl(struct host1x *host1x, u32 r)
  56. {
  57. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  58. return readl(sync_regs + r);
  59. }
  60. #ifdef CONFIG_64BIT
  61. u64 host1x_sync_readq(struct host1x *host1x, u32 r)
  62. {
  63. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  64. return readq(sync_regs + r);
  65. }
  66. #endif
  67. void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
  68. {
  69. writel(v, ch->regs + r);
  70. }
  71. u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
  72. {
  73. return readl(ch->regs + r);
  74. }
  75. static const struct host1x_info host1x01_info = {
  76. .nb_channels = 8,
  77. .nb_pts = 32,
  78. .nb_mlocks = 16,
  79. .nb_bases = 8,
  80. .init = host1x01_init,
  81. .sync_offset = 0x3000,
  82. .dma_mask = DMA_BIT_MASK(32),
  83. .has_wide_gather = false,
  84. .has_hypervisor = false,
  85. .num_sid_entries = 0,
  86. .sid_table = NULL,
  87. .reserve_vblank_syncpts = true,
  88. };
  89. static const struct host1x_info host1x02_info = {
  90. .nb_channels = 9,
  91. .nb_pts = 32,
  92. .nb_mlocks = 16,
  93. .nb_bases = 12,
  94. .init = host1x02_init,
  95. .sync_offset = 0x3000,
  96. .dma_mask = DMA_BIT_MASK(32),
  97. .has_wide_gather = false,
  98. .has_hypervisor = false,
  99. .num_sid_entries = 0,
  100. .sid_table = NULL,
  101. .reserve_vblank_syncpts = true,
  102. };
  103. static const struct host1x_info host1x04_info = {
  104. .nb_channels = 12,
  105. .nb_pts = 192,
  106. .nb_mlocks = 16,
  107. .nb_bases = 64,
  108. .init = host1x04_init,
  109. .sync_offset = 0x2100,
  110. .dma_mask = DMA_BIT_MASK(34),
  111. .has_wide_gather = false,
  112. .has_hypervisor = false,
  113. .num_sid_entries = 0,
  114. .sid_table = NULL,
  115. .reserve_vblank_syncpts = false,
  116. };
  117. static const struct host1x_info host1x05_info = {
  118. .nb_channels = 14,
  119. .nb_pts = 192,
  120. .nb_mlocks = 16,
  121. .nb_bases = 64,
  122. .init = host1x05_init,
  123. .sync_offset = 0x2100,
  124. .dma_mask = DMA_BIT_MASK(34),
  125. .has_wide_gather = false,
  126. .has_hypervisor = false,
  127. .num_sid_entries = 0,
  128. .sid_table = NULL,
  129. .reserve_vblank_syncpts = false,
  130. };
  131. static const struct host1x_sid_entry tegra186_sid_table[] = {
  132. { /* SE1 */ .base = 0x1ac8, .offset = 0x90, .limit = 0x90 },
  133. { /* SE2 */ .base = 0x1ad0, .offset = 0x90, .limit = 0x90 },
  134. { /* SE3 */ .base = 0x1ad8, .offset = 0x90, .limit = 0x90 },
  135. { /* SE4 */ .base = 0x1ae0, .offset = 0x90, .limit = 0x90 },
  136. { /* ISP */ .base = 0x1ae8, .offset = 0x50, .limit = 0x50 },
  137. { /* VIC */ .base = 0x1af0, .offset = 0x30, .limit = 0x34 },
  138. { /* NVENC */ .base = 0x1af8, .offset = 0x30, .limit = 0x34 },
  139. { /* NVDEC */ .base = 0x1b00, .offset = 0x30, .limit = 0x34 },
  140. { /* NVJPG */ .base = 0x1b08, .offset = 0x30, .limit = 0x34 },
  141. { /* TSEC */ .base = 0x1b10, .offset = 0x30, .limit = 0x34 },
  142. { /* TSECB */ .base = 0x1b18, .offset = 0x30, .limit = 0x34 },
  143. { /* VI 0 */ .base = 0x1b80, .offset = 0x10000, .limit = 0x10000 },
  144. { /* VI 1 */ .base = 0x1b88, .offset = 0x20000, .limit = 0x20000 },
  145. { /* VI 2 */ .base = 0x1b90, .offset = 0x30000, .limit = 0x30000 },
  146. { /* VI 3 */ .base = 0x1b98, .offset = 0x40000, .limit = 0x40000 },
  147. { /* VI 4 */ .base = 0x1ba0, .offset = 0x50000, .limit = 0x50000 },
  148. { /* VI 5 */ .base = 0x1ba8, .offset = 0x60000, .limit = 0x60000 },
  149. { /* VI 6 */ .base = 0x1bb0, .offset = 0x70000, .limit = 0x70000 },
  150. { /* VI 7 */ .base = 0x1bb8, .offset = 0x80000, .limit = 0x80000 },
  151. { /* VI 8 */ .base = 0x1bc0, .offset = 0x90000, .limit = 0x90000 },
  152. { /* VI 9 */ .base = 0x1bc8, .offset = 0xa0000, .limit = 0xa0000 },
  153. { /* VI 10 */ .base = 0x1bd0, .offset = 0xb0000, .limit = 0xb0000 },
  154. { /* VI 11 */ .base = 0x1bd8, .offset = 0xc0000, .limit = 0xc0000 },
  155. };
  156. static const struct host1x_info host1x06_info = {
  157. .nb_channels = 63,
  158. .nb_pts = 576,
  159. .nb_mlocks = 24,
  160. .nb_bases = 16,
  161. .init = host1x06_init,
  162. .sync_offset = 0x0,
  163. .dma_mask = DMA_BIT_MASK(40),
  164. .has_wide_gather = true,
  165. .has_hypervisor = true,
  166. .num_sid_entries = ARRAY_SIZE(tegra186_sid_table),
  167. .sid_table = tegra186_sid_table,
  168. .reserve_vblank_syncpts = false,
  169. .skip_reset_assert = true,
  170. };
  171. static const struct host1x_sid_entry tegra194_sid_table[] = {
  172. { /* SE1 */ .base = 0x1ac8, .offset = 0x90, .limit = 0x90 },
  173. { /* SE2 */ .base = 0x1ad0, .offset = 0x90, .limit = 0x90 },
  174. { /* SE3 */ .base = 0x1ad8, .offset = 0x90, .limit = 0x90 },
  175. { /* SE4 */ .base = 0x1ae0, .offset = 0x90, .limit = 0x90 },
  176. { /* ISP */ .base = 0x1ae8, .offset = 0x800, .limit = 0x800 },
  177. { /* VIC */ .base = 0x1af0, .offset = 0x30, .limit = 0x34 },
  178. { /* NVENC */ .base = 0x1af8, .offset = 0x30, .limit = 0x34 },
  179. { /* NVDEC */ .base = 0x1b00, .offset = 0x30, .limit = 0x34 },
  180. { /* NVJPG */ .base = 0x1b08, .offset = 0x30, .limit = 0x34 },
  181. { /* TSEC */ .base = 0x1b10, .offset = 0x30, .limit = 0x34 },
  182. { /* TSECB */ .base = 0x1b18, .offset = 0x30, .limit = 0x34 },
  183. { /* VI */ .base = 0x1b80, .offset = 0x800, .limit = 0x800 },
  184. { /* VI_THI */ .base = 0x1b88, .offset = 0x30, .limit = 0x34 },
  185. { /* ISP_THI */ .base = 0x1b90, .offset = 0x30, .limit = 0x34 },
  186. { /* PVA0_CLUSTER */ .base = 0x1b98, .offset = 0x0, .limit = 0x0 },
  187. { /* PVA0_CLUSTER */ .base = 0x1ba0, .offset = 0x0, .limit = 0x0 },
  188. { /* NVDLA0 */ .base = 0x1ba8, .offset = 0x30, .limit = 0x34 },
  189. { /* NVDLA1 */ .base = 0x1bb0, .offset = 0x30, .limit = 0x34 },
  190. { /* NVENC1 */ .base = 0x1bb8, .offset = 0x30, .limit = 0x34 },
  191. { /* NVDEC1 */ .base = 0x1bc0, .offset = 0x30, .limit = 0x34 },
  192. };
  193. static const struct host1x_info host1x07_info = {
  194. .nb_channels = 63,
  195. .nb_pts = 704,
  196. .nb_mlocks = 32,
  197. .nb_bases = 0,
  198. .init = host1x07_init,
  199. .sync_offset = 0x0,
  200. .dma_mask = DMA_BIT_MASK(40),
  201. .has_wide_gather = true,
  202. .has_hypervisor = true,
  203. .num_sid_entries = ARRAY_SIZE(tegra194_sid_table),
  204. .sid_table = tegra194_sid_table,
  205. .reserve_vblank_syncpts = false,
  206. };
  207. /*
  208. * Tegra234 has two stream ID protection tables, one for setting stream IDs
  209. * through the channel path via SETSTREAMID, and one for setting them via
  210. * MMIO. We program each engine's data stream ID in the channel path table
  211. * and firmware stream ID in the MMIO path table.
  212. */
  213. static const struct host1x_sid_entry tegra234_sid_table[] = {
  214. { /* SE1 MMIO */ .base = 0x1650, .offset = 0x90, .limit = 0x90 },
  215. { /* SE1 ch */ .base = 0x1730, .offset = 0x90, .limit = 0x90 },
  216. { /* SE2 MMIO */ .base = 0x1658, .offset = 0x90, .limit = 0x90 },
  217. { /* SE2 ch */ .base = 0x1738, .offset = 0x90, .limit = 0x90 },
  218. { /* SE4 MMIO */ .base = 0x1660, .offset = 0x90, .limit = 0x90 },
  219. { /* SE4 ch */ .base = 0x1740, .offset = 0x90, .limit = 0x90 },
  220. { /* ISP MMIO */ .base = 0x1680, .offset = 0x800, .limit = 0x800 },
  221. { /* VIC MMIO */ .base = 0x1688, .offset = 0x34, .limit = 0x34 },
  222. { /* VIC ch */ .base = 0x17b8, .offset = 0x30, .limit = 0x30 },
  223. { /* NVENC MMIO */ .base = 0x1690, .offset = 0x34, .limit = 0x34 },
  224. { /* NVENC ch */ .base = 0x17c0, .offset = 0x30, .limit = 0x30 },
  225. { /* NVDEC MMIO */ .base = 0x1698, .offset = 0x34, .limit = 0x34 },
  226. { /* NVDEC ch */ .base = 0x17c8, .offset = 0x30, .limit = 0x30 },
  227. { /* NVJPG MMIO */ .base = 0x16a0, .offset = 0x34, .limit = 0x34 },
  228. { /* NVJPG ch */ .base = 0x17d0, .offset = 0x30, .limit = 0x30 },
  229. { /* TSEC MMIO */ .base = 0x16a8, .offset = 0x30, .limit = 0x34 },
  230. { /* NVJPG1 MMIO */ .base = 0x16b0, .offset = 0x34, .limit = 0x34 },
  231. { /* NVJPG1 ch */ .base = 0x17a8, .offset = 0x30, .limit = 0x30 },
  232. { /* VI MMIO */ .base = 0x16b8, .offset = 0x800, .limit = 0x800 },
  233. { /* VI_THI MMIO */ .base = 0x16c0, .offset = 0x30, .limit = 0x34 },
  234. { /* ISP_THI MMIO */ .base = 0x16c8, .offset = 0x30, .limit = 0x34 },
  235. { /* NVDLA MMIO */ .base = 0x16d8, .offset = 0x30, .limit = 0x34 },
  236. { /* NVDLA ch */ .base = 0x17e0, .offset = 0x30, .limit = 0x34 },
  237. { /* NVDLA1 MMIO */ .base = 0x16e0, .offset = 0x30, .limit = 0x34 },
  238. { /* NVDLA1 ch */ .base = 0x17e8, .offset = 0x30, .limit = 0x34 },
  239. { /* OFA MMIO */ .base = 0x16e8, .offset = 0x34, .limit = 0x34 },
  240. { /* OFA ch */ .base = 0x1768, .offset = 0x30, .limit = 0x30 },
  241. { /* VI2 MMIO */ .base = 0x16f0, .offset = 0x800, .limit = 0x800 },
  242. { /* VI2_THI MMIO */ .base = 0x16f8, .offset = 0x30, .limit = 0x34 },
  243. };
  244. static const struct host1x_info host1x08_info = {
  245. .nb_channels = 63,
  246. .nb_pts = 1024,
  247. .nb_mlocks = 24,
  248. .nb_bases = 0,
  249. .init = host1x08_init,
  250. .sync_offset = 0x0,
  251. .dma_mask = DMA_BIT_MASK(40),
  252. .has_wide_gather = true,
  253. .has_hypervisor = true,
  254. .has_common = true,
  255. .num_sid_entries = ARRAY_SIZE(tegra234_sid_table),
  256. .sid_table = tegra234_sid_table,
  257. .streamid_vm_table = { 0x1004, 128 },
  258. .classid_vm_table = { 0x1404, 25 },
  259. .mmio_vm_table = { 0x1504, 25 },
  260. .reserve_vblank_syncpts = false,
  261. };
  262. static const struct of_device_id host1x_of_match[] = {
  263. { .compatible = "nvidia,tegra234-host1x", .data = &host1x08_info, },
  264. { .compatible = "nvidia,tegra194-host1x", .data = &host1x07_info, },
  265. { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
  266. { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
  267. { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
  268. { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
  269. { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
  270. { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
  271. { },
  272. };
  273. MODULE_DEVICE_TABLE(of, host1x_of_match);
  274. static void host1x_setup_virtualization_tables(struct host1x *host)
  275. {
  276. const struct host1x_info *info = host->info;
  277. unsigned int i;
  278. if (!info->has_hypervisor)
  279. return;
  280. for (i = 0; i < info->num_sid_entries; i++) {
  281. const struct host1x_sid_entry *entry = &info->sid_table[i];
  282. host1x_hypervisor_writel(host, entry->offset, entry->base);
  283. host1x_hypervisor_writel(host, entry->limit, entry->base + 4);
  284. }
  285. for (i = 0; i < info->streamid_vm_table.count; i++) {
  286. /* Allow access to all stream IDs to all VMs. */
  287. host1x_hypervisor_writel(host, 0xff, info->streamid_vm_table.base + 4 * i);
  288. }
  289. for (i = 0; i < info->classid_vm_table.count; i++) {
  290. /* Allow access to all classes to all VMs. */
  291. host1x_hypervisor_writel(host, 0xff, info->classid_vm_table.base + 4 * i);
  292. }
  293. for (i = 0; i < info->mmio_vm_table.count; i++) {
  294. /* Use VM1 (that's us) as originator VMID for engine MMIO accesses. */
  295. host1x_hypervisor_writel(host, 0x1, info->mmio_vm_table.base + 4 * i);
  296. }
  297. }
  298. static bool host1x_wants_iommu(struct host1x *host1x)
  299. {
  300. /* Our IOMMU usage policy doesn't currently play well with GART */
  301. if (of_machine_is_compatible("nvidia,tegra20"))
  302. return false;
  303. /*
  304. * If we support addressing a maximum of 32 bits of physical memory
  305. * and if the host1x firewall is enabled, there's no need to enable
  306. * IOMMU support. This can happen for example on Tegra20, Tegra30
  307. * and Tegra114.
  308. *
  309. * Tegra124 and later can address up to 34 bits of physical memory and
  310. * many platforms come equipped with more than 2 GiB of system memory,
  311. * which requires crossing the 4 GiB boundary. But there's a catch: on
  312. * SoCs before Tegra186 (i.e. Tegra124 and Tegra210), the host1x can
  313. * only address up to 32 bits of memory in GATHER opcodes, which means
  314. * that command buffers need to either be in the first 2 GiB of system
  315. * memory (which could quickly lead to memory exhaustion), or command
  316. * buffers need to be treated differently from other buffers (which is
  317. * not possible with the current ABI).
  318. *
  319. * A third option is to use the IOMMU in these cases to make sure all
  320. * buffers will be mapped into a 32-bit IOVA space that host1x can
  321. * address. This allows all of the system memory to be used and works
  322. * within the limitations of the host1x on these SoCs.
  323. *
  324. * In summary, default to enable IOMMU on Tegra124 and later. For any
  325. * of the earlier SoCs, only use the IOMMU for additional safety when
  326. * the host1x firewall is disabled.
  327. */
  328. if (host1x->info->dma_mask <= DMA_BIT_MASK(32)) {
  329. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
  330. return false;
  331. }
  332. return true;
  333. }
  334. /*
  335. * Returns ERR_PTR on failure, NULL if the translation is IDENTITY, otherwise a
  336. * valid paging domain.
  337. */
  338. static struct iommu_domain *host1x_iommu_attach(struct host1x *host)
  339. {
  340. struct iommu_domain *domain = iommu_get_domain_for_dev(host->dev);
  341. int err;
  342. #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
  343. if (host->dev->archdata.mapping) {
  344. struct dma_iommu_mapping *mapping =
  345. to_dma_iommu_mapping(host->dev);
  346. arm_iommu_detach_device(host->dev);
  347. arm_iommu_release_mapping(mapping);
  348. domain = iommu_get_domain_for_dev(host->dev);
  349. }
  350. #endif
  351. /*
  352. * We may not always want to enable IOMMU support (for example if the
  353. * host1x firewall is already enabled and we don't support addressing
  354. * more than 32 bits of physical memory), so check for that first.
  355. *
  356. * Similarly, if host1x is already attached to an IOMMU (via the DMA
  357. * API), don't try to attach again.
  358. */
  359. if (domain && domain->type == IOMMU_DOMAIN_IDENTITY)
  360. domain = NULL;
  361. if (!host1x_wants_iommu(host) || domain)
  362. return domain;
  363. host->group = iommu_group_get(host->dev);
  364. if (host->group) {
  365. struct iommu_domain_geometry *geometry;
  366. dma_addr_t start, end;
  367. unsigned long order;
  368. err = iova_cache_get();
  369. if (err < 0)
  370. goto put_group;
  371. host->domain = iommu_paging_domain_alloc(host->dev);
  372. if (IS_ERR(host->domain)) {
  373. err = PTR_ERR(host->domain);
  374. host->domain = NULL;
  375. goto put_cache;
  376. }
  377. err = iommu_attach_group(host->domain, host->group);
  378. if (err) {
  379. if (err == -ENODEV)
  380. err = 0;
  381. goto free_domain;
  382. }
  383. geometry = &host->domain->geometry;
  384. start = geometry->aperture_start & host->info->dma_mask;
  385. end = geometry->aperture_end & host->info->dma_mask;
  386. order = __ffs(host->domain->pgsize_bitmap);
  387. init_iova_domain(&host->iova, 1UL << order, start >> order);
  388. host->iova_end = end;
  389. domain = host->domain;
  390. }
  391. return domain;
  392. free_domain:
  393. iommu_domain_free(host->domain);
  394. host->domain = NULL;
  395. put_cache:
  396. iova_cache_put();
  397. put_group:
  398. iommu_group_put(host->group);
  399. host->group = NULL;
  400. return ERR_PTR(err);
  401. }
  402. static int host1x_iommu_init(struct host1x *host)
  403. {
  404. u64 mask = host->info->dma_mask;
  405. struct iommu_domain *domain;
  406. int err;
  407. domain = host1x_iommu_attach(host);
  408. if (IS_ERR(domain)) {
  409. err = PTR_ERR(domain);
  410. dev_err(host->dev, "failed to attach to IOMMU: %d\n", err);
  411. return err;
  412. }
  413. /*
  414. * If we're not behind an IOMMU make sure we don't get push buffers
  415. * that are allocated outside of the range addressable by the GATHER
  416. * opcode.
  417. *
  418. * Newer generations of Tegra (Tegra186 and later) support a wide
  419. * variant of the GATHER opcode that allows addressing more bits.
  420. */
  421. if (!domain && !host->info->has_wide_gather)
  422. mask = DMA_BIT_MASK(32);
  423. err = dma_coerce_mask_and_coherent(host->dev, mask);
  424. if (err < 0) {
  425. dev_err(host->dev, "failed to set DMA mask: %d\n", err);
  426. return err;
  427. }
  428. return 0;
  429. }
  430. static void host1x_iommu_exit(struct host1x *host)
  431. {
  432. if (host->domain) {
  433. put_iova_domain(&host->iova);
  434. iommu_detach_group(host->domain, host->group);
  435. iommu_domain_free(host->domain);
  436. host->domain = NULL;
  437. iova_cache_put();
  438. iommu_group_put(host->group);
  439. host->group = NULL;
  440. }
  441. }
  442. static int host1x_get_resets(struct host1x *host)
  443. {
  444. int err;
  445. host->resets[0].id = "mc";
  446. host->resets[1].id = "host1x";
  447. host->nresets = ARRAY_SIZE(host->resets);
  448. err = devm_reset_control_bulk_get_optional_exclusive_released(
  449. host->dev, host->nresets, host->resets);
  450. if (err) {
  451. dev_err(host->dev, "failed to get reset: %d\n", err);
  452. return err;
  453. }
  454. return 0;
  455. }
  456. static int host1x_probe(struct platform_device *pdev)
  457. {
  458. struct host1x *host;
  459. int err, i;
  460. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  461. if (!host)
  462. return -ENOMEM;
  463. host->info = of_device_get_match_data(&pdev->dev);
  464. if (host->info->has_hypervisor) {
  465. host->regs = devm_platform_ioremap_resource_byname(pdev, "vm");
  466. if (IS_ERR(host->regs))
  467. return PTR_ERR(host->regs);
  468. host->hv_regs = devm_platform_ioremap_resource_byname(pdev, "hypervisor");
  469. if (IS_ERR(host->hv_regs))
  470. return PTR_ERR(host->hv_regs);
  471. if (host->info->has_common) {
  472. host->common_regs = devm_platform_ioremap_resource_byname(pdev, "common");
  473. if (IS_ERR(host->common_regs))
  474. return PTR_ERR(host->common_regs);
  475. }
  476. } else {
  477. host->regs = devm_platform_ioremap_resource(pdev, 0);
  478. if (IS_ERR(host->regs))
  479. return PTR_ERR(host->regs);
  480. }
  481. for (i = 0; i < ARRAY_SIZE(host->syncpt_irqs); i++) {
  482. char irq_name[] = "syncptX";
  483. sprintf(irq_name, "syncpt%d", i);
  484. err = platform_get_irq_byname_optional(pdev, irq_name);
  485. if (err == -ENXIO)
  486. break;
  487. if (err < 0)
  488. return err;
  489. host->syncpt_irqs[i] = err;
  490. }
  491. host->num_syncpt_irqs = i;
  492. /* Device tree without irq names */
  493. if (i == 0) {
  494. host->syncpt_irqs[0] = platform_get_irq(pdev, 0);
  495. if (host->syncpt_irqs[0] < 0)
  496. return host->syncpt_irqs[0];
  497. host->num_syncpt_irqs = 1;
  498. }
  499. mutex_init(&host->devices_lock);
  500. INIT_LIST_HEAD(&host->devices);
  501. INIT_LIST_HEAD(&host->list);
  502. host->dev = &pdev->dev;
  503. /* set common host1x device data */
  504. platform_set_drvdata(pdev, host);
  505. host->dev->dma_parms = &host->dma_parms;
  506. dma_set_max_seg_size(host->dev, UINT_MAX);
  507. if (host->info->init) {
  508. err = host->info->init(host);
  509. if (err)
  510. return err;
  511. }
  512. host->clk = devm_clk_get(&pdev->dev, NULL);
  513. if (IS_ERR(host->clk))
  514. return dev_err_probe(&pdev->dev, PTR_ERR(host->clk), "failed to get clock\n");
  515. err = host1x_get_resets(host);
  516. if (err)
  517. return err;
  518. host1x_bo_cache_init(&host->cache);
  519. err = host1x_iommu_init(host);
  520. if (err < 0) {
  521. dev_err(&pdev->dev, "failed to setup IOMMU: %d\n", err);
  522. goto destroy_cache;
  523. }
  524. err = host1x_channel_list_init(&host->channel_list,
  525. host->info->nb_channels);
  526. if (err) {
  527. dev_err(&pdev->dev, "failed to initialize channel list\n");
  528. goto iommu_exit;
  529. }
  530. err = host1x_memory_context_list_init(host);
  531. if (err) {
  532. dev_err(&pdev->dev, "failed to initialize context list\n");
  533. goto free_channels;
  534. }
  535. err = host1x_syncpt_init(host);
  536. if (err) {
  537. dev_err(&pdev->dev, "failed to initialize syncpts\n");
  538. goto free_contexts;
  539. }
  540. mutex_init(&host->intr_mutex);
  541. pm_runtime_enable(&pdev->dev);
  542. err = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
  543. if (err)
  544. goto pm_disable;
  545. /* the driver's code isn't ready yet for the dynamic RPM */
  546. err = pm_runtime_resume_and_get(&pdev->dev);
  547. if (err)
  548. goto pm_disable;
  549. err = host1x_intr_init(host);
  550. if (err) {
  551. dev_err(&pdev->dev, "failed to initialize interrupts\n");
  552. goto pm_put;
  553. }
  554. host1x_debug_init(host);
  555. err = host1x_register(host);
  556. if (err < 0)
  557. goto deinit_debugfs;
  558. err = devm_of_platform_populate(&pdev->dev);
  559. if (err < 0)
  560. goto unregister;
  561. return 0;
  562. unregister:
  563. host1x_unregister(host);
  564. deinit_debugfs:
  565. host1x_debug_deinit(host);
  566. host1x_intr_deinit(host);
  567. pm_put:
  568. pm_runtime_put_sync_suspend(&pdev->dev);
  569. pm_disable:
  570. pm_runtime_disable(&pdev->dev);
  571. host1x_syncpt_deinit(host);
  572. free_contexts:
  573. host1x_memory_context_list_free(&host->context_list);
  574. free_channels:
  575. host1x_channel_list_free(&host->channel_list);
  576. iommu_exit:
  577. host1x_iommu_exit(host);
  578. destroy_cache:
  579. host1x_bo_cache_destroy(&host->cache);
  580. return err;
  581. }
  582. static void host1x_remove(struct platform_device *pdev)
  583. {
  584. struct host1x *host = platform_get_drvdata(pdev);
  585. host1x_unregister(host);
  586. host1x_debug_deinit(host);
  587. pm_runtime_force_suspend(&pdev->dev);
  588. host1x_intr_deinit(host);
  589. host1x_syncpt_deinit(host);
  590. host1x_memory_context_list_free(&host->context_list);
  591. host1x_channel_list_free(&host->channel_list);
  592. host1x_iommu_exit(host);
  593. host1x_bo_cache_destroy(&host->cache);
  594. }
  595. static int __maybe_unused host1x_runtime_suspend(struct device *dev)
  596. {
  597. struct host1x *host = dev_get_drvdata(dev);
  598. int err;
  599. host1x_channel_stop_all(host);
  600. host1x_intr_stop(host);
  601. host1x_syncpt_save(host);
  602. if (!host->info->skip_reset_assert) {
  603. err = reset_control_bulk_assert(host->nresets, host->resets);
  604. if (err) {
  605. dev_err(dev, "failed to assert reset: %d\n", err);
  606. goto resume_host1x;
  607. }
  608. usleep_range(1000, 2000);
  609. }
  610. clk_disable_unprepare(host->clk);
  611. reset_control_bulk_release(host->nresets, host->resets);
  612. return 0;
  613. resume_host1x:
  614. host1x_setup_virtualization_tables(host);
  615. host1x_syncpt_restore(host);
  616. host1x_intr_start(host);
  617. return err;
  618. }
  619. static int __maybe_unused host1x_runtime_resume(struct device *dev)
  620. {
  621. struct host1x *host = dev_get_drvdata(dev);
  622. int err;
  623. err = reset_control_bulk_acquire(host->nresets, host->resets);
  624. if (err) {
  625. dev_err(dev, "failed to acquire reset: %d\n", err);
  626. return err;
  627. }
  628. err = clk_prepare_enable(host->clk);
  629. if (err) {
  630. dev_err(dev, "failed to enable clock: %d\n", err);
  631. goto release_reset;
  632. }
  633. err = reset_control_bulk_deassert(host->nresets, host->resets);
  634. if (err < 0) {
  635. dev_err(dev, "failed to deassert reset: %d\n", err);
  636. goto disable_clk;
  637. }
  638. host1x_setup_virtualization_tables(host);
  639. host1x_syncpt_restore(host);
  640. host1x_intr_start(host);
  641. return 0;
  642. disable_clk:
  643. clk_disable_unprepare(host->clk);
  644. release_reset:
  645. reset_control_bulk_release(host->nresets, host->resets);
  646. return err;
  647. }
  648. static const struct dev_pm_ops host1x_pm_ops = {
  649. SET_RUNTIME_PM_OPS(host1x_runtime_suspend, host1x_runtime_resume,
  650. NULL)
  651. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  652. };
  653. static struct platform_driver tegra_host1x_driver = {
  654. .driver = {
  655. .name = "tegra-host1x",
  656. .of_match_table = host1x_of_match,
  657. .pm = &host1x_pm_ops,
  658. },
  659. .probe = host1x_probe,
  660. .remove = host1x_remove,
  661. };
  662. static struct platform_driver * const drivers[] = {
  663. &tegra_host1x_driver,
  664. &tegra_mipi_driver,
  665. };
  666. static int __init tegra_host1x_init(void)
  667. {
  668. int err;
  669. err = bus_register(&host1x_bus_type);
  670. if (err < 0)
  671. return err;
  672. err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  673. if (err < 0)
  674. bus_unregister(&host1x_bus_type);
  675. return err;
  676. }
  677. module_init(tegra_host1x_init);
  678. static void __exit tegra_host1x_exit(void)
  679. {
  680. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  681. bus_unregister(&host1x_bus_type);
  682. }
  683. module_exit(tegra_host1x_exit);
  684. /**
  685. * host1x_get_dma_mask() - query the supported DMA mask for host1x
  686. * @host1x: host1x instance
  687. *
  688. * Note that this returns the supported DMA mask for host1x, which can be
  689. * different from the applicable DMA mask under certain circumstances.
  690. */
  691. u64 host1x_get_dma_mask(struct host1x *host1x)
  692. {
  693. return host1x->info->dma_mask;
  694. }
  695. EXPORT_SYMBOL(host1x_get_dma_mask);
  696. MODULE_SOFTDEP("post: tegra-drm");
  697. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  698. MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
  699. MODULE_DESCRIPTION("Host1x driver for Tegra products");
  700. MODULE_LICENSE("GPL");