vic.c 14 KB

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