vic.c 9.2 KB

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