vic.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (c) 2015, NVIDIA Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/host1x.h>
  10. #include <linux/iommu.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/reset.h>
  18. #include <soc/tegra/pmc.h>
  19. #include "drm.h"
  20. #include "falcon.h"
  21. #include "vic.h"
  22. struct vic_config {
  23. const char *firmware;
  24. };
  25. struct vic {
  26. struct falcon falcon;
  27. bool booted;
  28. void __iomem *regs;
  29. struct tegra_drm_client client;
  30. struct host1x_channel *channel;
  31. struct iommu_domain *domain;
  32. struct device *dev;
  33. struct clk *clk;
  34. /* Platform configuration */
  35. const struct vic_config *config;
  36. };
  37. static inline struct vic *to_vic(struct tegra_drm_client *client)
  38. {
  39. return container_of(client, struct vic, client);
  40. }
  41. static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
  42. {
  43. writel(value, vic->regs + offset);
  44. }
  45. static int vic_runtime_resume(struct device *dev)
  46. {
  47. struct vic *vic = dev_get_drvdata(dev);
  48. return clk_prepare_enable(vic->clk);
  49. }
  50. static int vic_runtime_suspend(struct device *dev)
  51. {
  52. struct vic *vic = dev_get_drvdata(dev);
  53. clk_disable_unprepare(vic->clk);
  54. vic->booted = false;
  55. return 0;
  56. }
  57. static int vic_boot(struct vic *vic)
  58. {
  59. u32 fce_ucode_size, fce_bin_data_offset;
  60. void *hdr;
  61. int err = 0;
  62. if (vic->booted)
  63. return 0;
  64. /* setup clockgating registers */
  65. vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
  66. CG_IDLE_CG_EN |
  67. CG_WAKEUP_DLY_CNT(4),
  68. NV_PVIC_MISC_PRI_VIC_CG);
  69. err = falcon_boot(&vic->falcon);
  70. if (err < 0)
  71. return err;
  72. hdr = vic->falcon.firmware.vaddr;
  73. fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
  74. hdr = vic->falcon.firmware.vaddr +
  75. *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
  76. fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
  77. falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
  78. falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
  79. fce_ucode_size);
  80. falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
  81. (vic->falcon.firmware.paddr + fce_bin_data_offset)
  82. >> 8);
  83. err = falcon_wait_idle(&vic->falcon);
  84. if (err < 0) {
  85. dev_err(vic->dev,
  86. "failed to set application ID and FCE base\n");
  87. return err;
  88. }
  89. vic->booted = true;
  90. return 0;
  91. }
  92. static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
  93. dma_addr_t *iova)
  94. {
  95. struct tegra_drm *tegra = falcon->data;
  96. return tegra_drm_alloc(tegra, size, iova);
  97. }
  98. static void vic_falcon_free(struct falcon *falcon, size_t size,
  99. dma_addr_t iova, void *va)
  100. {
  101. struct tegra_drm *tegra = falcon->data;
  102. return tegra_drm_free(tegra, size, va, iova);
  103. }
  104. static const struct falcon_ops vic_falcon_ops = {
  105. .alloc = vic_falcon_alloc,
  106. .free = vic_falcon_free
  107. };
  108. static int vic_init(struct host1x_client *client)
  109. {
  110. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  111. struct iommu_group *group = iommu_group_get(client->dev);
  112. struct drm_device *dev = dev_get_drvdata(client->parent);
  113. struct tegra_drm *tegra = dev->dev_private;
  114. struct vic *vic = to_vic(drm);
  115. int err;
  116. if (group && tegra->domain) {
  117. err = iommu_attach_group(tegra->domain, group);
  118. if (err < 0) {
  119. dev_err(vic->dev, "failed to attach to domain: %d\n",
  120. err);
  121. return err;
  122. }
  123. vic->domain = tegra->domain;
  124. }
  125. if (!vic->falcon.data) {
  126. vic->falcon.data = tegra;
  127. err = falcon_load_firmware(&vic->falcon);
  128. if (err < 0)
  129. goto detach;
  130. }
  131. vic->channel = host1x_channel_request(client->dev);
  132. if (!vic->channel) {
  133. err = -ENOMEM;
  134. goto detach;
  135. }
  136. client->syncpts[0] = host1x_syncpt_request(client, 0);
  137. if (!client->syncpts[0]) {
  138. err = -ENOMEM;
  139. goto free_channel;
  140. }
  141. err = tegra_drm_register_client(tegra, drm);
  142. if (err < 0)
  143. goto free_syncpt;
  144. return 0;
  145. free_syncpt:
  146. host1x_syncpt_free(client->syncpts[0]);
  147. free_channel:
  148. host1x_channel_put(vic->channel);
  149. detach:
  150. if (group && tegra->domain)
  151. iommu_detach_group(tegra->domain, group);
  152. return err;
  153. }
  154. static int vic_exit(struct host1x_client *client)
  155. {
  156. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  157. struct iommu_group *group = iommu_group_get(client->dev);
  158. struct drm_device *dev = dev_get_drvdata(client->parent);
  159. struct tegra_drm *tegra = dev->dev_private;
  160. struct vic *vic = to_vic(drm);
  161. int err;
  162. err = tegra_drm_unregister_client(tegra, drm);
  163. if (err < 0)
  164. return err;
  165. host1x_syncpt_free(client->syncpts[0]);
  166. host1x_channel_put(vic->channel);
  167. if (vic->domain) {
  168. iommu_detach_group(vic->domain, group);
  169. vic->domain = NULL;
  170. }
  171. return 0;
  172. }
  173. static const struct host1x_client_ops vic_client_ops = {
  174. .init = vic_init,
  175. .exit = vic_exit,
  176. };
  177. static int vic_open_channel(struct tegra_drm_client *client,
  178. struct tegra_drm_context *context)
  179. {
  180. struct vic *vic = to_vic(client);
  181. int err;
  182. err = pm_runtime_get_sync(vic->dev);
  183. if (err < 0)
  184. return err;
  185. err = vic_boot(vic);
  186. if (err < 0) {
  187. pm_runtime_put(vic->dev);
  188. return err;
  189. }
  190. context->channel = host1x_channel_get(vic->channel);
  191. if (!context->channel) {
  192. pm_runtime_put(vic->dev);
  193. return -ENOMEM;
  194. }
  195. return 0;
  196. }
  197. static void vic_close_channel(struct tegra_drm_context *context)
  198. {
  199. struct vic *vic = to_vic(context->client);
  200. host1x_channel_put(context->channel);
  201. pm_runtime_put(vic->dev);
  202. }
  203. static const struct tegra_drm_client_ops vic_ops = {
  204. .open_channel = vic_open_channel,
  205. .close_channel = vic_close_channel,
  206. .submit = tegra_drm_submit,
  207. };
  208. #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
  209. static const struct vic_config vic_t124_config = {
  210. .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
  211. };
  212. #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
  213. static const struct vic_config vic_t210_config = {
  214. .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
  215. };
  216. #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
  217. static const struct vic_config vic_t186_config = {
  218. .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
  219. };
  220. static const struct of_device_id vic_match[] = {
  221. { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
  222. { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
  223. { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
  224. { },
  225. };
  226. static int vic_probe(struct platform_device *pdev)
  227. {
  228. struct device *dev = &pdev->dev;
  229. struct host1x_syncpt **syncpts;
  230. struct resource *regs;
  231. struct vic *vic;
  232. int err;
  233. vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
  234. if (!vic)
  235. return -ENOMEM;
  236. vic->config = of_device_get_match_data(dev);
  237. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  238. if (!syncpts)
  239. return -ENOMEM;
  240. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  241. if (!regs) {
  242. dev_err(&pdev->dev, "failed to get registers\n");
  243. return -ENXIO;
  244. }
  245. vic->regs = devm_ioremap_resource(dev, regs);
  246. if (IS_ERR(vic->regs))
  247. return PTR_ERR(vic->regs);
  248. vic->clk = devm_clk_get(dev, NULL);
  249. if (IS_ERR(vic->clk)) {
  250. dev_err(&pdev->dev, "failed to get clock\n");
  251. return PTR_ERR(vic->clk);
  252. }
  253. vic->falcon.dev = dev;
  254. vic->falcon.regs = vic->regs;
  255. vic->falcon.ops = &vic_falcon_ops;
  256. err = falcon_init(&vic->falcon);
  257. if (err < 0)
  258. return err;
  259. err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
  260. if (err < 0)
  261. goto exit_falcon;
  262. platform_set_drvdata(pdev, vic);
  263. INIT_LIST_HEAD(&vic->client.base.list);
  264. vic->client.base.ops = &vic_client_ops;
  265. vic->client.base.dev = dev;
  266. vic->client.base.class = HOST1X_CLASS_VIC;
  267. vic->client.base.syncpts = syncpts;
  268. vic->client.base.num_syncpts = 1;
  269. vic->dev = dev;
  270. INIT_LIST_HEAD(&vic->client.list);
  271. vic->client.ops = &vic_ops;
  272. err = host1x_client_register(&vic->client.base);
  273. if (err < 0) {
  274. dev_err(dev, "failed to register host1x client: %d\n", err);
  275. platform_set_drvdata(pdev, NULL);
  276. goto exit_falcon;
  277. }
  278. pm_runtime_enable(&pdev->dev);
  279. if (!pm_runtime_enabled(&pdev->dev)) {
  280. err = vic_runtime_resume(&pdev->dev);
  281. if (err < 0)
  282. goto unregister_client;
  283. }
  284. return 0;
  285. unregister_client:
  286. host1x_client_unregister(&vic->client.base);
  287. exit_falcon:
  288. falcon_exit(&vic->falcon);
  289. return err;
  290. }
  291. static int vic_remove(struct platform_device *pdev)
  292. {
  293. struct vic *vic = platform_get_drvdata(pdev);
  294. int err;
  295. err = host1x_client_unregister(&vic->client.base);
  296. if (err < 0) {
  297. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  298. err);
  299. return err;
  300. }
  301. if (pm_runtime_enabled(&pdev->dev))
  302. pm_runtime_disable(&pdev->dev);
  303. else
  304. vic_runtime_suspend(&pdev->dev);
  305. falcon_exit(&vic->falcon);
  306. return 0;
  307. }
  308. static const struct dev_pm_ops vic_pm_ops = {
  309. SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
  310. };
  311. struct platform_driver tegra_vic_driver = {
  312. .driver = {
  313. .name = "tegra-vic",
  314. .of_match_table = vic_match,
  315. .pm = &vic_pm_ops
  316. },
  317. .probe = vic_probe,
  318. .remove = vic_remove,
  319. };
  320. #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
  321. MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
  322. #endif
  323. #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
  324. MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
  325. #endif
  326. #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
  327. MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
  328. #endif