dev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Tegra host1x driver
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/io.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of.h>
  25. #include <linux/slab.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/host1x.h>
  28. #undef CREATE_TRACE_POINTS
  29. #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
  30. #include <asm/dma-iommu.h>
  31. #endif
  32. #include "bus.h"
  33. #include "channel.h"
  34. #include "debug.h"
  35. #include "dev.h"
  36. #include "intr.h"
  37. #include "hw/host1x01.h"
  38. #include "hw/host1x02.h"
  39. #include "hw/host1x04.h"
  40. #include "hw/host1x05.h"
  41. #include "hw/host1x06.h"
  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. void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
  61. {
  62. writel(v, ch->regs + r);
  63. }
  64. u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
  65. {
  66. return readl(ch->regs + r);
  67. }
  68. static const struct host1x_info host1x01_info = {
  69. .nb_channels = 8,
  70. .nb_pts = 32,
  71. .nb_mlocks = 16,
  72. .nb_bases = 8,
  73. .init = host1x01_init,
  74. .sync_offset = 0x3000,
  75. .dma_mask = DMA_BIT_MASK(32),
  76. };
  77. static const struct host1x_info host1x02_info = {
  78. .nb_channels = 9,
  79. .nb_pts = 32,
  80. .nb_mlocks = 16,
  81. .nb_bases = 12,
  82. .init = host1x02_init,
  83. .sync_offset = 0x3000,
  84. .dma_mask = DMA_BIT_MASK(32),
  85. };
  86. static const struct host1x_info host1x04_info = {
  87. .nb_channels = 12,
  88. .nb_pts = 192,
  89. .nb_mlocks = 16,
  90. .nb_bases = 64,
  91. .init = host1x04_init,
  92. .sync_offset = 0x2100,
  93. .dma_mask = DMA_BIT_MASK(34),
  94. };
  95. static const struct host1x_info host1x05_info = {
  96. .nb_channels = 14,
  97. .nb_pts = 192,
  98. .nb_mlocks = 16,
  99. .nb_bases = 64,
  100. .init = host1x05_init,
  101. .sync_offset = 0x2100,
  102. .dma_mask = DMA_BIT_MASK(34),
  103. };
  104. static const struct host1x_info host1x06_info = {
  105. .nb_channels = 63,
  106. .nb_pts = 576,
  107. .nb_mlocks = 24,
  108. .nb_bases = 16,
  109. .init = host1x06_init,
  110. .sync_offset = 0x0,
  111. .dma_mask = DMA_BIT_MASK(34),
  112. .has_hypervisor = true,
  113. };
  114. static const struct of_device_id host1x_of_match[] = {
  115. { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
  116. { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
  117. { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
  118. { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
  119. { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
  120. { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(of, host1x_of_match);
  124. static int host1x_probe(struct platform_device *pdev)
  125. {
  126. struct host1x *host;
  127. struct resource *regs, *hv_regs = NULL;
  128. int syncpt_irq;
  129. int err;
  130. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  131. if (!host)
  132. return -ENOMEM;
  133. host->info = of_device_get_match_data(&pdev->dev);
  134. if (host->info->has_hypervisor) {
  135. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vm");
  136. if (!regs) {
  137. dev_err(&pdev->dev, "failed to get vm registers\n");
  138. return -ENXIO;
  139. }
  140. hv_regs = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  141. "hypervisor");
  142. if (!hv_regs) {
  143. dev_err(&pdev->dev,
  144. "failed to get hypervisor registers\n");
  145. return -ENXIO;
  146. }
  147. } else {
  148. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  149. if (!regs) {
  150. dev_err(&pdev->dev, "failed to get registers\n");
  151. return -ENXIO;
  152. }
  153. }
  154. syncpt_irq = platform_get_irq(pdev, 0);
  155. if (syncpt_irq < 0) {
  156. dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
  157. return syncpt_irq;
  158. }
  159. mutex_init(&host->devices_lock);
  160. INIT_LIST_HEAD(&host->devices);
  161. INIT_LIST_HEAD(&host->list);
  162. host->dev = &pdev->dev;
  163. /* set common host1x device data */
  164. platform_set_drvdata(pdev, host);
  165. host->regs = devm_ioremap_resource(&pdev->dev, regs);
  166. if (IS_ERR(host->regs))
  167. return PTR_ERR(host->regs);
  168. if (host->info->has_hypervisor) {
  169. host->hv_regs = devm_ioremap_resource(&pdev->dev, hv_regs);
  170. if (IS_ERR(host->hv_regs))
  171. return PTR_ERR(host->hv_regs);
  172. }
  173. dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
  174. if (host->info->init) {
  175. err = host->info->init(host);
  176. if (err)
  177. return err;
  178. }
  179. host->clk = devm_clk_get(&pdev->dev, NULL);
  180. if (IS_ERR(host->clk)) {
  181. dev_err(&pdev->dev, "failed to get clock\n");
  182. err = PTR_ERR(host->clk);
  183. return err;
  184. }
  185. host->rst = devm_reset_control_get(&pdev->dev, "host1x");
  186. if (IS_ERR(host->rst)) {
  187. err = PTR_ERR(host->rst);
  188. dev_err(&pdev->dev, "failed to get reset: %d\n", err);
  189. return err;
  190. }
  191. #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
  192. if (host->dev->archdata.mapping) {
  193. struct dma_iommu_mapping *mapping =
  194. to_dma_iommu_mapping(host->dev);
  195. arm_iommu_detach_device(host->dev);
  196. arm_iommu_release_mapping(mapping);
  197. }
  198. #endif
  199. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
  200. goto skip_iommu;
  201. host->group = iommu_group_get(&pdev->dev);
  202. if (host->group) {
  203. struct iommu_domain_geometry *geometry;
  204. unsigned long order;
  205. err = iova_cache_get();
  206. if (err < 0)
  207. goto put_group;
  208. host->domain = iommu_domain_alloc(&platform_bus_type);
  209. if (!host->domain) {
  210. err = -ENOMEM;
  211. goto put_cache;
  212. }
  213. err = iommu_attach_group(host->domain, host->group);
  214. if (err) {
  215. if (err == -ENODEV) {
  216. iommu_domain_free(host->domain);
  217. host->domain = NULL;
  218. iova_cache_put();
  219. iommu_group_put(host->group);
  220. host->group = NULL;
  221. goto skip_iommu;
  222. }
  223. goto fail_free_domain;
  224. }
  225. geometry = &host->domain->geometry;
  226. order = __ffs(host->domain->pgsize_bitmap);
  227. init_iova_domain(&host->iova, 1UL << order,
  228. geometry->aperture_start >> order);
  229. host->iova_end = geometry->aperture_end;
  230. }
  231. skip_iommu:
  232. err = host1x_channel_list_init(&host->channel_list,
  233. host->info->nb_channels);
  234. if (err) {
  235. dev_err(&pdev->dev, "failed to initialize channel list\n");
  236. goto fail_detach_device;
  237. }
  238. err = clk_prepare_enable(host->clk);
  239. if (err < 0) {
  240. dev_err(&pdev->dev, "failed to enable clock\n");
  241. goto fail_free_channels;
  242. }
  243. err = reset_control_deassert(host->rst);
  244. if (err < 0) {
  245. dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
  246. goto fail_unprepare_disable;
  247. }
  248. err = host1x_syncpt_init(host);
  249. if (err) {
  250. dev_err(&pdev->dev, "failed to initialize syncpts\n");
  251. goto fail_reset_assert;
  252. }
  253. err = host1x_intr_init(host, syncpt_irq);
  254. if (err) {
  255. dev_err(&pdev->dev, "failed to initialize interrupts\n");
  256. goto fail_deinit_syncpt;
  257. }
  258. host1x_debug_init(host);
  259. err = host1x_register(host);
  260. if (err < 0)
  261. goto fail_deinit_intr;
  262. return 0;
  263. fail_deinit_intr:
  264. host1x_intr_deinit(host);
  265. fail_deinit_syncpt:
  266. host1x_syncpt_deinit(host);
  267. fail_reset_assert:
  268. reset_control_assert(host->rst);
  269. fail_unprepare_disable:
  270. clk_disable_unprepare(host->clk);
  271. fail_free_channels:
  272. host1x_channel_list_free(&host->channel_list);
  273. fail_detach_device:
  274. if (host->group && host->domain) {
  275. put_iova_domain(&host->iova);
  276. iommu_detach_group(host->domain, host->group);
  277. }
  278. fail_free_domain:
  279. if (host->domain)
  280. iommu_domain_free(host->domain);
  281. put_cache:
  282. if (host->group)
  283. iova_cache_put();
  284. put_group:
  285. iommu_group_put(host->group);
  286. return err;
  287. }
  288. static int host1x_remove(struct platform_device *pdev)
  289. {
  290. struct host1x *host = platform_get_drvdata(pdev);
  291. host1x_unregister(host);
  292. host1x_intr_deinit(host);
  293. host1x_syncpt_deinit(host);
  294. reset_control_assert(host->rst);
  295. clk_disable_unprepare(host->clk);
  296. if (host->domain) {
  297. put_iova_domain(&host->iova);
  298. iommu_detach_group(host->domain, host->group);
  299. iommu_domain_free(host->domain);
  300. iova_cache_put();
  301. iommu_group_put(host->group);
  302. }
  303. return 0;
  304. }
  305. static struct platform_driver tegra_host1x_driver = {
  306. .driver = {
  307. .name = "tegra-host1x",
  308. .of_match_table = host1x_of_match,
  309. },
  310. .probe = host1x_probe,
  311. .remove = host1x_remove,
  312. };
  313. static struct platform_driver * const drivers[] = {
  314. &tegra_host1x_driver,
  315. &tegra_mipi_driver,
  316. };
  317. static int __init tegra_host1x_init(void)
  318. {
  319. int err;
  320. err = bus_register(&host1x_bus_type);
  321. if (err < 0)
  322. return err;
  323. err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  324. if (err < 0)
  325. bus_unregister(&host1x_bus_type);
  326. return err;
  327. }
  328. module_init(tegra_host1x_init);
  329. static void __exit tegra_host1x_exit(void)
  330. {
  331. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  332. bus_unregister(&host1x_bus_type);
  333. }
  334. module_exit(tegra_host1x_exit);
  335. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  336. MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
  337. MODULE_DESCRIPTION("Host1x driver for Tegra products");
  338. MODULE_LICENSE("GPL");