1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, NVIDIA Corporation.
7 #include <linux/delay.h>
8 #include <linux/host1x.h>
9 #include <linux/iommu.h>
10 #include <linux/module.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>
18 #include <soc/tegra/pmc.h>
34 struct tegra_drm_client client;
35 struct host1x_channel *channel;
38 struct reset_control *rst;
40 /* Platform configuration */
41 const struct vic_config *config;
44 static inline struct vic *to_vic(struct tegra_drm_client *client)
46 return container_of(client, struct vic, client);
49 static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
51 writel(value, vic->regs + offset);
54 static int vic_boot(struct vic *vic)
56 #ifdef CONFIG_IOMMU_API
57 struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev);
59 u32 fce_ucode_size, fce_bin_data_offset;
63 #ifdef CONFIG_IOMMU_API
64 if (vic->config->supports_sid && spec) {
67 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
68 TRANSCFG_ATT(0, TRANSCFG_SID_HW);
69 vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
71 if (spec->num_ids > 0) {
72 value = spec->ids[0] & 0xffff;
75 * STREAMID0 is used for input/output buffers.
76 * Initialize it to SID_VIC in case context isolation
77 * is not enabled, and SID_VIC is used for both firmware
80 * If context isolation is enabled, it will be
81 * overridden by the SETSTREAMID opcode as part of
84 vic_writel(vic, value, VIC_THI_STREAMID0);
86 /* STREAMID1 is used for firmware loading. */
87 vic_writel(vic, value, VIC_THI_STREAMID1);
92 /* setup clockgating registers */
93 vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
96 NV_PVIC_MISC_PRI_VIC_CG);
98 err = falcon_boot(&vic->falcon);
102 hdr = vic->falcon.firmware.virt;
103 fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
105 /* Old VIC firmware needs kernel help with setting up FCE microcode. */
106 if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
107 hdr = vic->falcon.firmware.virt +
108 *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
109 fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
111 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
113 falcon_execute_method(
114 &vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
115 (vic->falcon.firmware.iova + fce_bin_data_offset) >> 8);
118 err = falcon_wait_idle(&vic->falcon);
121 "failed to set application ID and FCE base\n");
128 static int vic_init(struct host1x_client *client)
130 struct tegra_drm_client *drm = host1x_to_drm_client(client);
131 struct drm_device *dev = dev_get_drvdata(client->host);
132 struct tegra_drm *tegra = dev->dev_private;
133 struct vic *vic = to_vic(drm);
136 err = host1x_client_iommu_attach(client);
137 if (err < 0 && err != -ENODEV) {
138 dev_err(vic->dev, "failed to attach to domain: %d\n", err);
142 vic->channel = host1x_channel_request(client);
148 client->syncpts[0] = host1x_syncpt_request(client, 0);
149 if (!client->syncpts[0]) {
154 err = tegra_drm_register_client(tegra, drm);
159 * Inherit the DMA parameters (such as maximum segment size) from the
160 * parent host1x device.
162 client->dev->dma_parms = client->host->dma_parms;
167 host1x_syncpt_put(client->syncpts[0]);
169 host1x_channel_put(vic->channel);
171 host1x_client_iommu_detach(client);
176 static int vic_exit(struct host1x_client *client)
178 struct tegra_drm_client *drm = host1x_to_drm_client(client);
179 struct drm_device *dev = dev_get_drvdata(client->host);
180 struct tegra_drm *tegra = dev->dev_private;
181 struct vic *vic = to_vic(drm);
184 /* avoid a dangling pointer just in case this disappears */
185 client->dev->dma_parms = NULL;
187 err = tegra_drm_unregister_client(tegra, drm);
191 host1x_syncpt_put(client->syncpts[0]);
192 host1x_channel_put(vic->channel);
193 host1x_client_iommu_detach(client);
196 dma_unmap_single(vic->dev, vic->falcon.firmware.phys,
197 vic->falcon.firmware.size, DMA_TO_DEVICE);
198 tegra_drm_free(tegra, vic->falcon.firmware.size,
199 vic->falcon.firmware.virt,
200 vic->falcon.firmware.iova);
202 dma_free_coherent(vic->dev, vic->falcon.firmware.size,
203 vic->falcon.firmware.virt,
204 vic->falcon.firmware.iova);
210 static const struct host1x_client_ops vic_client_ops = {
215 static int vic_load_firmware(struct vic *vic)
217 struct host1x_client *client = &vic->client.base;
218 struct tegra_drm *tegra = vic->client.drm;
224 if (vic->falcon.firmware.virt)
227 err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
231 size = vic->falcon.firmware.size;
233 if (!client->group) {
234 virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL);
236 err = dma_mapping_error(vic->dev, iova);
240 virt = tegra_drm_alloc(tegra, size, &iova);
243 vic->falcon.firmware.virt = virt;
244 vic->falcon.firmware.iova = iova;
246 err = falcon_load_firmware(&vic->falcon);
251 * In this case we have received an IOVA from the shared domain, so we
252 * need to make sure to get the physical address so that the DMA API
253 * knows what memory pages to flush the cache for.
258 phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE);
260 err = dma_mapping_error(vic->dev, phys);
264 vic->falcon.firmware.phys = phys;
271 dma_free_coherent(vic->dev, size, virt, iova);
273 tegra_drm_free(tegra, size, virt, iova);
279 static int vic_runtime_resume(struct device *dev)
281 struct vic *vic = dev_get_drvdata(dev);
284 err = clk_prepare_enable(vic->clk);
288 usleep_range(10, 20);
290 err = reset_control_deassert(vic->rst);
294 usleep_range(10, 20);
296 err = vic_load_firmware(vic);
307 reset_control_assert(vic->rst);
309 clk_disable_unprepare(vic->clk);
313 static int vic_runtime_suspend(struct device *dev)
315 struct vic *vic = dev_get_drvdata(dev);
318 err = reset_control_assert(vic->rst);
322 usleep_range(2000, 4000);
324 clk_disable_unprepare(vic->clk);
329 static int vic_open_channel(struct tegra_drm_client *client,
330 struct tegra_drm_context *context)
332 struct vic *vic = to_vic(client);
335 err = pm_runtime_resume_and_get(vic->dev);
339 context->channel = host1x_channel_get(vic->channel);
340 if (!context->channel) {
341 pm_runtime_put(vic->dev);
348 static void vic_close_channel(struct tegra_drm_context *context)
350 struct vic *vic = to_vic(context->client);
352 host1x_channel_put(context->channel);
353 pm_runtime_put(vic->dev);
356 static const struct tegra_drm_client_ops vic_ops = {
357 .open_channel = vic_open_channel,
358 .close_channel = vic_close_channel,
359 .submit = tegra_drm_submit,
362 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
364 static const struct vic_config vic_t124_config = {
365 .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
367 .supports_sid = false,
370 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
372 static const struct vic_config vic_t210_config = {
373 .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
375 .supports_sid = false,
378 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
380 static const struct vic_config vic_t186_config = {
381 .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
383 .supports_sid = true,
386 #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
388 static const struct vic_config vic_t194_config = {
389 .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
391 .supports_sid = true,
394 static const struct of_device_id tegra_vic_of_match[] = {
395 { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
396 { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
397 { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
398 { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
401 MODULE_DEVICE_TABLE(of, tegra_vic_of_match);
403 static int vic_probe(struct platform_device *pdev)
405 struct device *dev = &pdev->dev;
406 struct host1x_syncpt **syncpts;
407 struct resource *regs;
411 /* inherit DMA mask from host1x parent */
412 err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
414 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
418 vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
422 vic->config = of_device_get_match_data(dev);
424 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
428 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
430 dev_err(&pdev->dev, "failed to get registers\n");
434 vic->regs = devm_ioremap_resource(dev, regs);
435 if (IS_ERR(vic->regs))
436 return PTR_ERR(vic->regs);
438 vic->clk = devm_clk_get(dev, NULL);
439 if (IS_ERR(vic->clk)) {
440 dev_err(&pdev->dev, "failed to get clock\n");
441 return PTR_ERR(vic->clk);
444 if (!dev->pm_domain) {
445 vic->rst = devm_reset_control_get(dev, "vic");
446 if (IS_ERR(vic->rst)) {
447 dev_err(&pdev->dev, "failed to get reset\n");
448 return PTR_ERR(vic->rst);
452 vic->falcon.dev = dev;
453 vic->falcon.regs = vic->regs;
455 err = falcon_init(&vic->falcon);
459 platform_set_drvdata(pdev, vic);
461 INIT_LIST_HEAD(&vic->client.base.list);
462 vic->client.base.ops = &vic_client_ops;
463 vic->client.base.dev = dev;
464 vic->client.base.class = HOST1X_CLASS_VIC;
465 vic->client.base.syncpts = syncpts;
466 vic->client.base.num_syncpts = 1;
469 INIT_LIST_HEAD(&vic->client.list);
470 vic->client.version = vic->config->version;
471 vic->client.ops = &vic_ops;
473 err = host1x_client_register(&vic->client.base);
475 dev_err(dev, "failed to register host1x client: %d\n", err);
479 pm_runtime_enable(&pdev->dev);
480 if (!pm_runtime_enabled(&pdev->dev)) {
481 err = vic_runtime_resume(&pdev->dev);
483 goto unregister_client;
489 host1x_client_unregister(&vic->client.base);
491 falcon_exit(&vic->falcon);
496 static int vic_remove(struct platform_device *pdev)
498 struct vic *vic = platform_get_drvdata(pdev);
501 err = host1x_client_unregister(&vic->client.base);
503 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
508 if (pm_runtime_enabled(&pdev->dev))
509 pm_runtime_disable(&pdev->dev);
511 vic_runtime_suspend(&pdev->dev);
513 falcon_exit(&vic->falcon);
518 static const struct dev_pm_ops vic_pm_ops = {
519 SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
522 struct platform_driver tegra_vic_driver = {
525 .of_match_table = tegra_vic_of_match,
529 .remove = vic_remove,
532 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
533 MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
535 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
536 MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
538 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
539 MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
541 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
542 MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);