dev.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include "bus.h"
  30. #include "channel.h"
  31. #include "debug.h"
  32. #include "dev.h"
  33. #include "intr.h"
  34. #include "hw/host1x01.h"
  35. #include "hw/host1x02.h"
  36. #include "hw/host1x04.h"
  37. #include "hw/host1x05.h"
  38. #include "hw/host1x06.h"
  39. void host1x_hypervisor_writel(struct host1x *host1x, u32 v, u32 r)
  40. {
  41. writel(v, host1x->hv_regs + r);
  42. }
  43. u32 host1x_hypervisor_readl(struct host1x *host1x, u32 r)
  44. {
  45. return readl(host1x->hv_regs + r);
  46. }
  47. void host1x_sync_writel(struct host1x *host1x, u32 v, u32 r)
  48. {
  49. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  50. writel(v, sync_regs + r);
  51. }
  52. u32 host1x_sync_readl(struct host1x *host1x, u32 r)
  53. {
  54. void __iomem *sync_regs = host1x->regs + host1x->info->sync_offset;
  55. return readl(sync_regs + r);
  56. }
  57. void host1x_ch_writel(struct host1x_channel *ch, u32 v, u32 r)
  58. {
  59. writel(v, ch->regs + r);
  60. }
  61. u32 host1x_ch_readl(struct host1x_channel *ch, u32 r)
  62. {
  63. return readl(ch->regs + r);
  64. }
  65. static const struct host1x_info host1x01_info = {
  66. .nb_channels = 8,
  67. .nb_pts = 32,
  68. .nb_mlocks = 16,
  69. .nb_bases = 8,
  70. .init = host1x01_init,
  71. .sync_offset = 0x3000,
  72. .dma_mask = DMA_BIT_MASK(32),
  73. };
  74. static const struct host1x_info host1x02_info = {
  75. .nb_channels = 9,
  76. .nb_pts = 32,
  77. .nb_mlocks = 16,
  78. .nb_bases = 12,
  79. .init = host1x02_init,
  80. .sync_offset = 0x3000,
  81. .dma_mask = DMA_BIT_MASK(32),
  82. };
  83. static const struct host1x_info host1x04_info = {
  84. .nb_channels = 12,
  85. .nb_pts = 192,
  86. .nb_mlocks = 16,
  87. .nb_bases = 64,
  88. .init = host1x04_init,
  89. .sync_offset = 0x2100,
  90. .dma_mask = DMA_BIT_MASK(34),
  91. };
  92. static const struct host1x_info host1x05_info = {
  93. .nb_channels = 14,
  94. .nb_pts = 192,
  95. .nb_mlocks = 16,
  96. .nb_bases = 64,
  97. .init = host1x05_init,
  98. .sync_offset = 0x2100,
  99. .dma_mask = DMA_BIT_MASK(34),
  100. };
  101. static const struct host1x_info host1x06_info = {
  102. .nb_channels = 63,
  103. .nb_pts = 576,
  104. .nb_mlocks = 24,
  105. .nb_bases = 16,
  106. .init = host1x06_init,
  107. .sync_offset = 0x0,
  108. .dma_mask = DMA_BIT_MASK(34),
  109. .has_hypervisor = true,
  110. };
  111. static const struct of_device_id host1x_of_match[] = {
  112. { .compatible = "nvidia,tegra186-host1x", .data = &host1x06_info, },
  113. { .compatible = "nvidia,tegra210-host1x", .data = &host1x05_info, },
  114. { .compatible = "nvidia,tegra124-host1x", .data = &host1x04_info, },
  115. { .compatible = "nvidia,tegra114-host1x", .data = &host1x02_info, },
  116. { .compatible = "nvidia,tegra30-host1x", .data = &host1x01_info, },
  117. { .compatible = "nvidia,tegra20-host1x", .data = &host1x01_info, },
  118. { },
  119. };
  120. MODULE_DEVICE_TABLE(of, host1x_of_match);
  121. static int host1x_probe(struct platform_device *pdev)
  122. {
  123. struct host1x *host;
  124. struct resource *regs, *hv_regs = NULL;
  125. int syncpt_irq;
  126. int err;
  127. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  128. if (!host)
  129. return -ENOMEM;
  130. host->info = of_device_get_match_data(&pdev->dev);
  131. if (host->info->has_hypervisor) {
  132. regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vm");
  133. if (!regs) {
  134. dev_err(&pdev->dev, "failed to get vm registers\n");
  135. return -ENXIO;
  136. }
  137. hv_regs = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  138. "hypervisor");
  139. if (!hv_regs) {
  140. dev_err(&pdev->dev,
  141. "failed to get hypervisor registers\n");
  142. return -ENXIO;
  143. }
  144. } else {
  145. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  146. if (!regs) {
  147. dev_err(&pdev->dev, "failed to get registers\n");
  148. return -ENXIO;
  149. }
  150. }
  151. syncpt_irq = platform_get_irq(pdev, 0);
  152. if (syncpt_irq < 0) {
  153. dev_err(&pdev->dev, "failed to get IRQ: %d\n", syncpt_irq);
  154. return syncpt_irq;
  155. }
  156. mutex_init(&host->devices_lock);
  157. INIT_LIST_HEAD(&host->devices);
  158. INIT_LIST_HEAD(&host->list);
  159. host->dev = &pdev->dev;
  160. /* set common host1x device data */
  161. platform_set_drvdata(pdev, host);
  162. host->regs = devm_ioremap_resource(&pdev->dev, regs);
  163. if (IS_ERR(host->regs))
  164. return PTR_ERR(host->regs);
  165. if (host->info->has_hypervisor) {
  166. host->hv_regs = devm_ioremap_resource(&pdev->dev, hv_regs);
  167. if (IS_ERR(host->hv_regs))
  168. return PTR_ERR(host->hv_regs);
  169. }
  170. dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
  171. if (host->info->init) {
  172. err = host->info->init(host);
  173. if (err)
  174. return err;
  175. }
  176. host->clk = devm_clk_get(&pdev->dev, NULL);
  177. if (IS_ERR(host->clk)) {
  178. dev_err(&pdev->dev, "failed to get clock\n");
  179. err = PTR_ERR(host->clk);
  180. return err;
  181. }
  182. host->rst = devm_reset_control_get(&pdev->dev, "host1x");
  183. if (IS_ERR(host->rst)) {
  184. err = PTR_ERR(host->rst);
  185. dev_err(&pdev->dev, "failed to get reset: %d\n", err);
  186. return err;
  187. }
  188. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
  189. goto skip_iommu;
  190. host->group = iommu_group_get(&pdev->dev);
  191. if (host->group) {
  192. struct iommu_domain_geometry *geometry;
  193. unsigned long order;
  194. err = iova_cache_get();
  195. if (err < 0)
  196. goto put_group;
  197. host->domain = iommu_domain_alloc(&platform_bus_type);
  198. if (!host->domain) {
  199. err = -ENOMEM;
  200. goto put_cache;
  201. }
  202. err = iommu_attach_group(host->domain, host->group);
  203. if (err) {
  204. if (err == -ENODEV) {
  205. iommu_domain_free(host->domain);
  206. host->domain = NULL;
  207. iova_cache_put();
  208. iommu_group_put(host->group);
  209. host->group = NULL;
  210. goto skip_iommu;
  211. }
  212. goto fail_free_domain;
  213. }
  214. geometry = &host->domain->geometry;
  215. order = __ffs(host->domain->pgsize_bitmap);
  216. init_iova_domain(&host->iova, 1UL << order,
  217. geometry->aperture_start >> order);
  218. host->iova_end = geometry->aperture_end;
  219. }
  220. skip_iommu:
  221. err = host1x_channel_list_init(&host->channel_list,
  222. host->info->nb_channels);
  223. if (err) {
  224. dev_err(&pdev->dev, "failed to initialize channel list\n");
  225. goto fail_detach_device;
  226. }
  227. err = clk_prepare_enable(host->clk);
  228. if (err < 0) {
  229. dev_err(&pdev->dev, "failed to enable clock\n");
  230. goto fail_free_channels;
  231. }
  232. err = reset_control_deassert(host->rst);
  233. if (err < 0) {
  234. dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
  235. goto fail_unprepare_disable;
  236. }
  237. err = host1x_syncpt_init(host);
  238. if (err) {
  239. dev_err(&pdev->dev, "failed to initialize syncpts\n");
  240. goto fail_reset_assert;
  241. }
  242. err = host1x_intr_init(host, syncpt_irq);
  243. if (err) {
  244. dev_err(&pdev->dev, "failed to initialize interrupts\n");
  245. goto fail_deinit_syncpt;
  246. }
  247. host1x_debug_init(host);
  248. err = host1x_register(host);
  249. if (err < 0)
  250. goto fail_deinit_intr;
  251. return 0;
  252. fail_deinit_intr:
  253. host1x_intr_deinit(host);
  254. fail_deinit_syncpt:
  255. host1x_syncpt_deinit(host);
  256. fail_reset_assert:
  257. reset_control_assert(host->rst);
  258. fail_unprepare_disable:
  259. clk_disable_unprepare(host->clk);
  260. fail_free_channels:
  261. host1x_channel_list_free(&host->channel_list);
  262. fail_detach_device:
  263. if (host->group && host->domain) {
  264. put_iova_domain(&host->iova);
  265. iommu_detach_group(host->domain, host->group);
  266. }
  267. fail_free_domain:
  268. if (host->domain)
  269. iommu_domain_free(host->domain);
  270. put_cache:
  271. if (host->group)
  272. iova_cache_put();
  273. put_group:
  274. iommu_group_put(host->group);
  275. return err;
  276. }
  277. static int host1x_remove(struct platform_device *pdev)
  278. {
  279. struct host1x *host = platform_get_drvdata(pdev);
  280. host1x_unregister(host);
  281. host1x_intr_deinit(host);
  282. host1x_syncpt_deinit(host);
  283. reset_control_assert(host->rst);
  284. clk_disable_unprepare(host->clk);
  285. if (host->domain) {
  286. put_iova_domain(&host->iova);
  287. iommu_detach_group(host->domain, host->group);
  288. iommu_domain_free(host->domain);
  289. iova_cache_put();
  290. iommu_group_put(host->group);
  291. }
  292. return 0;
  293. }
  294. static struct platform_driver tegra_host1x_driver = {
  295. .driver = {
  296. .name = "tegra-host1x",
  297. .of_match_table = host1x_of_match,
  298. },
  299. .probe = host1x_probe,
  300. .remove = host1x_remove,
  301. };
  302. static struct platform_driver * const drivers[] = {
  303. &tegra_host1x_driver,
  304. &tegra_mipi_driver,
  305. };
  306. static int __init tegra_host1x_init(void)
  307. {
  308. int err;
  309. err = bus_register(&host1x_bus_type);
  310. if (err < 0)
  311. return err;
  312. err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  313. if (err < 0)
  314. bus_unregister(&host1x_bus_type);
  315. return err;
  316. }
  317. module_init(tegra_host1x_init);
  318. static void __exit tegra_host1x_exit(void)
  319. {
  320. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  321. bus_unregister(&host1x_bus_type);
  322. }
  323. module_exit(tegra_host1x_exit);
  324. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  325. MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
  326. MODULE_DESCRIPTION("Host1x driver for Tegra products");
  327. MODULE_LICENSE("GPL");