vic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, NVIDIA Corporation.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/host1x.h>
  9. #include <linux/iommu.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/reset.h>
  15. #include <soc/tegra/pmc.h>
  16. #include "drm.h"
  17. #include "falcon.h"
  18. #include "vic.h"
  19. struct vic_config {
  20. const char *firmware;
  21. unsigned int version;
  22. bool supports_sid;
  23. };
  24. struct vic {
  25. struct falcon falcon;
  26. void __iomem *regs;
  27. struct tegra_drm_client client;
  28. struct host1x_channel *channel;
  29. struct device *dev;
  30. struct clk *clk;
  31. struct reset_control *rst;
  32. bool can_use_context;
  33. /* Platform configuration */
  34. const struct vic_config *config;
  35. };
  36. static inline struct vic *to_vic(struct tegra_drm_client *client)
  37. {
  38. return container_of(client, struct vic, client);
  39. }
  40. static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
  41. {
  42. writel(value, vic->regs + offset);
  43. }
  44. static int vic_boot(struct vic *vic)
  45. {
  46. u32 fce_ucode_size, fce_bin_data_offset, stream_id;
  47. void *hdr;
  48. int err = 0;
  49. if (vic->config->supports_sid && tegra_dev_iommu_get_stream_id(vic->dev, &stream_id)) {
  50. u32 value;
  51. value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
  52. TRANSCFG_ATT(0, TRANSCFG_SID_HW);
  53. vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
  54. /*
  55. * STREAMID0 is used for input/output buffers. Initialize it to SID_VIC in case
  56. * context isolation is not enabled, and SID_VIC is used for both firmware and
  57. * data buffers.
  58. *
  59. * If context isolation is enabled, it will be overridden by the SETSTREAMID
  60. * opcode as part of each job.
  61. */
  62. vic_writel(vic, stream_id, VIC_THI_STREAMID0);
  63. /* STREAMID1 is used for firmware loading. */
  64. vic_writel(vic, stream_id, VIC_THI_STREAMID1);
  65. }
  66. /* setup clockgating registers */
  67. vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
  68. CG_IDLE_CG_EN |
  69. CG_WAKEUP_DLY_CNT(4),
  70. NV_PVIC_MISC_PRI_VIC_CG);
  71. err = falcon_boot(&vic->falcon);
  72. if (err < 0)
  73. return err;
  74. hdr = vic->falcon.firmware.virt;
  75. fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
  76. /* Old VIC firmware needs kernel help with setting up FCE microcode. */
  77. if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
  78. hdr = vic->falcon.firmware.virt +
  79. *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
  80. fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
  81. falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
  82. fce_ucode_size);
  83. falcon_execute_method(
  84. &vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
  85. (vic->falcon.firmware.iova + fce_bin_data_offset) >> 8);
  86. }
  87. err = falcon_wait_idle(&vic->falcon);
  88. if (err < 0) {
  89. dev_err(vic->dev,
  90. "failed to set application ID and FCE base\n");
  91. return err;
  92. }
  93. return 0;
  94. }
  95. static int vic_init(struct host1x_client *client)
  96. {
  97. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  98. struct drm_device *dev = dev_get_drvdata(client->host);
  99. struct tegra_drm *tegra = dev->dev_private;
  100. struct vic *vic = to_vic(drm);
  101. int err;
  102. err = host1x_client_iommu_attach(client);
  103. if (err < 0 && err != -ENODEV) {
  104. dev_err(vic->dev, "failed to attach to domain: %d\n", err);
  105. return err;
  106. }
  107. vic->channel = host1x_channel_request(client);
  108. if (!vic->channel) {
  109. err = -ENOMEM;
  110. goto detach;
  111. }
  112. client->syncpts[0] = host1x_syncpt_request(client, 0);
  113. if (!client->syncpts[0]) {
  114. err = -ENOMEM;
  115. goto free_channel;
  116. }
  117. err = tegra_drm_register_client(tegra, drm);
  118. if (err < 0)
  119. goto free_syncpt;
  120. /*
  121. * Inherit the DMA parameters (such as maximum segment size) from the
  122. * parent host1x device.
  123. */
  124. client->dev->dma_parms = client->host->dma_parms;
  125. return 0;
  126. free_syncpt:
  127. host1x_syncpt_put(client->syncpts[0]);
  128. free_channel:
  129. host1x_channel_put(vic->channel);
  130. detach:
  131. host1x_client_iommu_detach(client);
  132. return err;
  133. }
  134. static int vic_exit(struct host1x_client *client)
  135. {
  136. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  137. struct drm_device *dev = dev_get_drvdata(client->host);
  138. struct tegra_drm *tegra = dev->dev_private;
  139. struct vic *vic = to_vic(drm);
  140. int err;
  141. /* avoid a dangling pointer just in case this disappears */
  142. client->dev->dma_parms = NULL;
  143. err = tegra_drm_unregister_client(tegra, drm);
  144. if (err < 0)
  145. return err;
  146. pm_runtime_dont_use_autosuspend(client->dev);
  147. pm_runtime_force_suspend(client->dev);
  148. host1x_syncpt_put(client->syncpts[0]);
  149. host1x_channel_put(vic->channel);
  150. host1x_client_iommu_detach(client);
  151. vic->channel = NULL;
  152. if (client->group) {
  153. dma_unmap_single(vic->dev, vic->falcon.firmware.phys,
  154. vic->falcon.firmware.size, DMA_TO_DEVICE);
  155. tegra_drm_free(tegra, vic->falcon.firmware.size,
  156. vic->falcon.firmware.virt,
  157. vic->falcon.firmware.iova);
  158. } else {
  159. dma_free_coherent(vic->dev, vic->falcon.firmware.size,
  160. vic->falcon.firmware.virt,
  161. vic->falcon.firmware.iova);
  162. }
  163. return 0;
  164. }
  165. static const struct host1x_client_ops vic_client_ops = {
  166. .init = vic_init,
  167. .exit = vic_exit,
  168. };
  169. static int vic_load_firmware(struct vic *vic)
  170. {
  171. struct host1x_client *client = &vic->client.base;
  172. struct tegra_drm *tegra = vic->client.drm;
  173. static DEFINE_MUTEX(lock);
  174. u32 fce_bin_data_offset;
  175. dma_addr_t iova;
  176. size_t size;
  177. void *virt;
  178. int err;
  179. mutex_lock(&lock);
  180. if (vic->falcon.firmware.virt) {
  181. err = 0;
  182. goto unlock;
  183. }
  184. err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
  185. if (err < 0)
  186. goto unlock;
  187. size = vic->falcon.firmware.size;
  188. if (!client->group) {
  189. virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL);
  190. if (!virt) {
  191. err = -ENOMEM;
  192. goto unlock;
  193. }
  194. } else {
  195. virt = tegra_drm_alloc(tegra, size, &iova);
  196. if (IS_ERR(virt)) {
  197. err = PTR_ERR(virt);
  198. goto unlock;
  199. }
  200. }
  201. vic->falcon.firmware.virt = virt;
  202. vic->falcon.firmware.iova = iova;
  203. err = falcon_load_firmware(&vic->falcon);
  204. if (err < 0)
  205. goto cleanup;
  206. /*
  207. * In this case we have received an IOVA from the shared domain, so we
  208. * need to make sure to get the physical address so that the DMA API
  209. * knows what memory pages to flush the cache for.
  210. */
  211. if (client->group) {
  212. dma_addr_t phys;
  213. phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE);
  214. err = dma_mapping_error(vic->dev, phys);
  215. if (err < 0)
  216. goto cleanup;
  217. vic->falcon.firmware.phys = phys;
  218. }
  219. /*
  220. * Check if firmware is new enough to not require mapping firmware
  221. * to data buffer domains.
  222. */
  223. fce_bin_data_offset = *(u32 *)(virt + VIC_UCODE_FCE_DATA_OFFSET);
  224. if (!vic->config->supports_sid) {
  225. vic->can_use_context = false;
  226. } else if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
  227. /*
  228. * Firmware will access FCE through STREAMID0, so context
  229. * isolation cannot be used.
  230. */
  231. vic->can_use_context = false;
  232. dev_warn_once(vic->dev, "context isolation disabled due to old firmware\n");
  233. } else {
  234. vic->can_use_context = true;
  235. }
  236. unlock:
  237. mutex_unlock(&lock);
  238. return err;
  239. cleanup:
  240. if (!client->group)
  241. dma_free_coherent(vic->dev, size, virt, iova);
  242. else
  243. tegra_drm_free(tegra, size, virt, iova);
  244. mutex_unlock(&lock);
  245. return err;
  246. }
  247. static int __maybe_unused vic_runtime_resume(struct device *dev)
  248. {
  249. struct vic *vic = dev_get_drvdata(dev);
  250. int err;
  251. err = clk_prepare_enable(vic->clk);
  252. if (err < 0)
  253. return err;
  254. usleep_range(10, 20);
  255. err = reset_control_deassert(vic->rst);
  256. if (err < 0)
  257. goto disable;
  258. usleep_range(10, 20);
  259. err = vic_load_firmware(vic);
  260. if (err < 0)
  261. goto assert;
  262. err = vic_boot(vic);
  263. if (err < 0)
  264. goto assert;
  265. return 0;
  266. assert:
  267. reset_control_assert(vic->rst);
  268. disable:
  269. clk_disable_unprepare(vic->clk);
  270. return err;
  271. }
  272. static int __maybe_unused vic_runtime_suspend(struct device *dev)
  273. {
  274. struct vic *vic = dev_get_drvdata(dev);
  275. int err;
  276. host1x_channel_stop(vic->channel);
  277. err = reset_control_assert(vic->rst);
  278. if (err < 0)
  279. return err;
  280. usleep_range(2000, 4000);
  281. clk_disable_unprepare(vic->clk);
  282. return 0;
  283. }
  284. static int vic_open_channel(struct tegra_drm_client *client,
  285. struct tegra_drm_context *context)
  286. {
  287. struct vic *vic = to_vic(client);
  288. context->channel = host1x_channel_get(vic->channel);
  289. if (!context->channel)
  290. return -ENOMEM;
  291. return 0;
  292. }
  293. static void vic_close_channel(struct tegra_drm_context *context)
  294. {
  295. host1x_channel_put(context->channel);
  296. }
  297. static int vic_can_use_memory_ctx(struct tegra_drm_client *client, bool *supported)
  298. {
  299. struct vic *vic = to_vic(client);
  300. int err;
  301. /* This doesn't access HW so it's safe to call without powering up. */
  302. err = vic_load_firmware(vic);
  303. if (err < 0)
  304. return err;
  305. *supported = vic->can_use_context;
  306. return 0;
  307. }
  308. static const struct tegra_drm_client_ops vic_ops = {
  309. .open_channel = vic_open_channel,
  310. .close_channel = vic_close_channel,
  311. .submit = tegra_drm_submit,
  312. .get_streamid_offset = tegra_drm_get_streamid_offset_thi,
  313. .can_use_memory_ctx = vic_can_use_memory_ctx,
  314. };
  315. #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
  316. static const struct vic_config vic_t124_config = {
  317. .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
  318. .version = 0x40,
  319. .supports_sid = false,
  320. };
  321. #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
  322. static const struct vic_config vic_t210_config = {
  323. .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
  324. .version = 0x21,
  325. .supports_sid = false,
  326. };
  327. #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
  328. static const struct vic_config vic_t186_config = {
  329. .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
  330. .version = 0x18,
  331. .supports_sid = true,
  332. };
  333. #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
  334. static const struct vic_config vic_t194_config = {
  335. .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
  336. .version = 0x19,
  337. .supports_sid = true,
  338. };
  339. #define NVIDIA_TEGRA_234_VIC_FIRMWARE "nvidia/tegra234/vic.bin"
  340. static const struct vic_config vic_t234_config = {
  341. .firmware = NVIDIA_TEGRA_234_VIC_FIRMWARE,
  342. .version = 0x23,
  343. .supports_sid = true,
  344. };
  345. static const struct of_device_id tegra_vic_of_match[] = {
  346. { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
  347. { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
  348. { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
  349. { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
  350. { .compatible = "nvidia,tegra234-vic", .data = &vic_t234_config },
  351. { },
  352. };
  353. MODULE_DEVICE_TABLE(of, tegra_vic_of_match);
  354. static int vic_probe(struct platform_device *pdev)
  355. {
  356. struct device *dev = &pdev->dev;
  357. struct host1x_syncpt **syncpts;
  358. struct vic *vic;
  359. int err;
  360. /* inherit DMA mask from host1x parent */
  361. err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
  362. if (err < 0) {
  363. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  364. return err;
  365. }
  366. vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
  367. if (!vic)
  368. return -ENOMEM;
  369. vic->config = of_device_get_match_data(dev);
  370. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  371. if (!syncpts)
  372. return -ENOMEM;
  373. vic->regs = devm_platform_ioremap_resource(pdev, 0);
  374. if (IS_ERR(vic->regs))
  375. return PTR_ERR(vic->regs);
  376. vic->clk = devm_clk_get(dev, NULL);
  377. if (IS_ERR(vic->clk)) {
  378. dev_err(&pdev->dev, "failed to get clock\n");
  379. return PTR_ERR(vic->clk);
  380. }
  381. err = clk_set_rate(vic->clk, ULONG_MAX);
  382. if (err < 0) {
  383. dev_err(&pdev->dev, "failed to set clock rate\n");
  384. return err;
  385. }
  386. if (!dev->pm_domain) {
  387. vic->rst = devm_reset_control_get(dev, "vic");
  388. if (IS_ERR(vic->rst)) {
  389. dev_err(&pdev->dev, "failed to get reset\n");
  390. return PTR_ERR(vic->rst);
  391. }
  392. }
  393. vic->falcon.dev = dev;
  394. vic->falcon.regs = vic->regs;
  395. err = falcon_init(&vic->falcon);
  396. if (err < 0)
  397. return err;
  398. platform_set_drvdata(pdev, vic);
  399. INIT_LIST_HEAD(&vic->client.base.list);
  400. vic->client.base.ops = &vic_client_ops;
  401. vic->client.base.dev = dev;
  402. vic->client.base.class = HOST1X_CLASS_VIC;
  403. vic->client.base.syncpts = syncpts;
  404. vic->client.base.num_syncpts = 1;
  405. vic->dev = dev;
  406. INIT_LIST_HEAD(&vic->client.list);
  407. vic->client.version = vic->config->version;
  408. vic->client.ops = &vic_ops;
  409. err = host1x_client_register(&vic->client.base);
  410. if (err < 0) {
  411. dev_err(dev, "failed to register host1x client: %d\n", err);
  412. goto exit_falcon;
  413. }
  414. pm_runtime_enable(dev);
  415. pm_runtime_use_autosuspend(dev);
  416. pm_runtime_set_autosuspend_delay(dev, 500);
  417. return 0;
  418. exit_falcon:
  419. falcon_exit(&vic->falcon);
  420. return err;
  421. }
  422. static void vic_remove(struct platform_device *pdev)
  423. {
  424. struct vic *vic = platform_get_drvdata(pdev);
  425. pm_runtime_disable(&pdev->dev);
  426. host1x_client_unregister(&vic->client.base);
  427. falcon_exit(&vic->falcon);
  428. }
  429. static const struct dev_pm_ops vic_pm_ops = {
  430. RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
  431. SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
  432. };
  433. struct platform_driver tegra_vic_driver = {
  434. .driver = {
  435. .name = "tegra-vic",
  436. .of_match_table = tegra_vic_of_match,
  437. .pm = &vic_pm_ops
  438. },
  439. .probe = vic_probe,
  440. .remove_new = vic_remove,
  441. };
  442. #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
  443. MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
  444. #endif
  445. #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
  446. MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
  447. #endif
  448. #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
  449. MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
  450. #endif
  451. #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
  452. MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);
  453. #endif
  454. #if IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
  455. MODULE_FIRMWARE(NVIDIA_TEGRA_234_VIC_FIRMWARE);
  456. #endif