1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2022, NVIDIA Corporation.
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/host1x.h>
10 #include <linux/iommu.h>
11 #include <linux/iopoll.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
18 #include <soc/tegra/mc.h>
25 #define NVDEC_FALCON_DEBUGINFO 0x1094
26 #define NVDEC_TFBIF_TRANSCFG 0x2c44
33 bool has_extra_clocks;
40 struct tegra_drm_client client;
41 struct host1x_channel *channel;
43 struct clk_bulk_data clks[3];
44 unsigned int num_clks;
45 struct reset_control *reset;
47 /* Platform configuration */
48 const struct nvdec_config *config;
50 /* RISC-V specific data */
51 struct tegra_drm_riscv riscv;
52 phys_addr_t carveout_base;
55 static inline struct nvdec *to_nvdec(struct tegra_drm_client *client)
57 return container_of(client, struct nvdec, client);
60 static inline void nvdec_writel(struct nvdec *nvdec, u32 value,
63 writel(value, nvdec->regs + offset);
66 static int nvdec_boot_falcon(struct nvdec *nvdec)
71 if (nvdec->config->supports_sid && tegra_dev_iommu_get_stream_id(nvdec->dev, &stream_id)) {
74 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) | TRANSCFG_ATT(0, TRANSCFG_SID_HW);
75 nvdec_writel(nvdec, value, NVDEC_TFBIF_TRANSCFG);
77 nvdec_writel(nvdec, stream_id, VIC_THI_STREAMID0);
78 nvdec_writel(nvdec, stream_id, VIC_THI_STREAMID1);
81 err = falcon_boot(&nvdec->falcon);
85 err = falcon_wait_idle(&nvdec->falcon);
87 dev_err(nvdec->dev, "falcon boot timed out\n");
94 static int nvdec_wait_debuginfo(struct nvdec *nvdec, const char *phase)
99 err = readl_poll_timeout(nvdec->regs + NVDEC_FALCON_DEBUGINFO, val, val == 0x0, 10, 100000);
101 dev_err(nvdec->dev, "failed to boot %s, debuginfo=0x%x\n", phase, val);
108 static int nvdec_boot_riscv(struct nvdec *nvdec)
112 err = reset_control_acquire(nvdec->reset);
116 nvdec_writel(nvdec, 0xabcd1234, NVDEC_FALCON_DEBUGINFO);
118 err = tegra_drm_riscv_boot_bootrom(&nvdec->riscv, nvdec->carveout_base, 1,
119 &nvdec->riscv.bl_desc);
121 dev_err(nvdec->dev, "failed to execute bootloader\n");
125 err = nvdec_wait_debuginfo(nvdec, "bootloader");
129 err = reset_control_reset(nvdec->reset);
133 nvdec_writel(nvdec, 0xabcd1234, NVDEC_FALCON_DEBUGINFO);
135 err = tegra_drm_riscv_boot_bootrom(&nvdec->riscv, nvdec->carveout_base, 1,
136 &nvdec->riscv.os_desc);
138 dev_err(nvdec->dev, "failed to execute firmware\n");
142 err = nvdec_wait_debuginfo(nvdec, "firmware");
147 reset_control_release(nvdec->reset);
152 static int nvdec_init(struct host1x_client *client)
154 struct tegra_drm_client *drm = host1x_to_drm_client(client);
155 struct drm_device *dev = dev_get_drvdata(client->host);
156 struct tegra_drm *tegra = dev->dev_private;
157 struct nvdec *nvdec = to_nvdec(drm);
160 err = host1x_client_iommu_attach(client);
161 if (err < 0 && err != -ENODEV) {
162 dev_err(nvdec->dev, "failed to attach to domain: %d\n", err);
166 nvdec->channel = host1x_channel_request(client);
167 if (!nvdec->channel) {
172 client->syncpts[0] = host1x_syncpt_request(client, 0);
173 if (!client->syncpts[0]) {
178 err = tegra_drm_register_client(tegra, drm);
183 * Inherit the DMA parameters (such as maximum segment size) from the
184 * parent host1x device.
186 client->dev->dma_parms = client->host->dma_parms;
191 host1x_syncpt_put(client->syncpts[0]);
193 host1x_channel_put(nvdec->channel);
195 host1x_client_iommu_detach(client);
200 static int nvdec_exit(struct host1x_client *client)
202 struct tegra_drm_client *drm = host1x_to_drm_client(client);
203 struct drm_device *dev = dev_get_drvdata(client->host);
204 struct tegra_drm *tegra = dev->dev_private;
205 struct nvdec *nvdec = to_nvdec(drm);
208 /* avoid a dangling pointer just in case this disappears */
209 client->dev->dma_parms = NULL;
211 err = tegra_drm_unregister_client(tegra, drm);
215 pm_runtime_dont_use_autosuspend(client->dev);
216 pm_runtime_force_suspend(client->dev);
218 host1x_syncpt_put(client->syncpts[0]);
219 host1x_channel_put(nvdec->channel);
220 host1x_client_iommu_detach(client);
222 nvdec->channel = NULL;
225 dma_unmap_single(nvdec->dev, nvdec->falcon.firmware.phys,
226 nvdec->falcon.firmware.size, DMA_TO_DEVICE);
227 tegra_drm_free(tegra, nvdec->falcon.firmware.size,
228 nvdec->falcon.firmware.virt,
229 nvdec->falcon.firmware.iova);
231 dma_free_coherent(nvdec->dev, nvdec->falcon.firmware.size,
232 nvdec->falcon.firmware.virt,
233 nvdec->falcon.firmware.iova);
239 static const struct host1x_client_ops nvdec_client_ops = {
244 static int nvdec_load_falcon_firmware(struct nvdec *nvdec)
246 struct host1x_client *client = &nvdec->client.base;
247 struct tegra_drm *tegra = nvdec->client.drm;
253 if (nvdec->falcon.firmware.virt)
256 err = falcon_read_firmware(&nvdec->falcon, nvdec->config->firmware);
260 size = nvdec->falcon.firmware.size;
262 if (!client->group) {
263 virt = dma_alloc_coherent(nvdec->dev, size, &iova, GFP_KERNEL);
265 err = dma_mapping_error(nvdec->dev, iova);
269 virt = tegra_drm_alloc(tegra, size, &iova);
271 return PTR_ERR(virt);
274 nvdec->falcon.firmware.virt = virt;
275 nvdec->falcon.firmware.iova = iova;
277 err = falcon_load_firmware(&nvdec->falcon);
282 * In this case we have received an IOVA from the shared domain, so we
283 * need to make sure to get the physical address so that the DMA API
284 * knows what memory pages to flush the cache for.
289 phys = dma_map_single(nvdec->dev, virt, size, DMA_TO_DEVICE);
291 err = dma_mapping_error(nvdec->dev, phys);
295 nvdec->falcon.firmware.phys = phys;
302 dma_free_coherent(nvdec->dev, size, virt, iova);
304 tegra_drm_free(tegra, size, virt, iova);
309 static __maybe_unused int nvdec_runtime_resume(struct device *dev)
311 struct nvdec *nvdec = dev_get_drvdata(dev);
314 err = clk_bulk_prepare_enable(nvdec->num_clks, nvdec->clks);
318 usleep_range(10, 20);
320 if (nvdec->config->has_riscv) {
321 err = nvdec_boot_riscv(nvdec);
325 err = nvdec_load_falcon_firmware(nvdec);
329 err = nvdec_boot_falcon(nvdec);
337 clk_bulk_disable_unprepare(nvdec->num_clks, nvdec->clks);
341 static __maybe_unused int nvdec_runtime_suspend(struct device *dev)
343 struct nvdec *nvdec = dev_get_drvdata(dev);
345 host1x_channel_stop(nvdec->channel);
347 clk_bulk_disable_unprepare(nvdec->num_clks, nvdec->clks);
352 static int nvdec_open_channel(struct tegra_drm_client *client,
353 struct tegra_drm_context *context)
355 struct nvdec *nvdec = to_nvdec(client);
357 context->channel = host1x_channel_get(nvdec->channel);
358 if (!context->channel)
364 static void nvdec_close_channel(struct tegra_drm_context *context)
366 host1x_channel_put(context->channel);
369 static int nvdec_can_use_memory_ctx(struct tegra_drm_client *client, bool *supported)
376 static const struct tegra_drm_client_ops nvdec_ops = {
377 .open_channel = nvdec_open_channel,
378 .close_channel = nvdec_close_channel,
379 .submit = tegra_drm_submit,
380 .get_streamid_offset = tegra_drm_get_streamid_offset_thi,
381 .can_use_memory_ctx = nvdec_can_use_memory_ctx,
384 #define NVIDIA_TEGRA_210_NVDEC_FIRMWARE "nvidia/tegra210/nvdec.bin"
386 static const struct nvdec_config nvdec_t210_config = {
387 .firmware = NVIDIA_TEGRA_210_NVDEC_FIRMWARE,
389 .supports_sid = false,
392 #define NVIDIA_TEGRA_186_NVDEC_FIRMWARE "nvidia/tegra186/nvdec.bin"
394 static const struct nvdec_config nvdec_t186_config = {
395 .firmware = NVIDIA_TEGRA_186_NVDEC_FIRMWARE,
397 .supports_sid = true,
400 #define NVIDIA_TEGRA_194_NVDEC_FIRMWARE "nvidia/tegra194/nvdec.bin"
402 static const struct nvdec_config nvdec_t194_config = {
403 .firmware = NVIDIA_TEGRA_194_NVDEC_FIRMWARE,
405 .supports_sid = true,
408 static const struct nvdec_config nvdec_t234_config = {
410 .supports_sid = true,
412 .has_extra_clocks = true,
415 static const struct of_device_id tegra_nvdec_of_match[] = {
416 { .compatible = "nvidia,tegra210-nvdec", .data = &nvdec_t210_config },
417 { .compatible = "nvidia,tegra186-nvdec", .data = &nvdec_t186_config },
418 { .compatible = "nvidia,tegra194-nvdec", .data = &nvdec_t194_config },
419 { .compatible = "nvidia,tegra234-nvdec", .data = &nvdec_t234_config },
422 MODULE_DEVICE_TABLE(of, tegra_nvdec_of_match);
424 static int nvdec_probe(struct platform_device *pdev)
426 struct device *dev = &pdev->dev;
427 struct host1x_syncpt **syncpts;
432 /* inherit DMA mask from host1x parent */
433 err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
435 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
439 nvdec = devm_kzalloc(dev, sizeof(*nvdec), GFP_KERNEL);
443 nvdec->config = of_device_get_match_data(dev);
445 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
449 nvdec->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
450 if (IS_ERR(nvdec->regs))
451 return PTR_ERR(nvdec->regs);
453 nvdec->clks[0].id = "nvdec";
456 if (nvdec->config->has_extra_clocks) {
458 nvdec->clks[1].id = "fuse";
459 nvdec->clks[2].id = "tsec_pka";
462 err = devm_clk_bulk_get(dev, nvdec->num_clks, nvdec->clks);
464 dev_err(&pdev->dev, "failed to get clock(s)\n");
468 err = clk_set_rate(nvdec->clks[0].clk, ULONG_MAX);
470 dev_err(&pdev->dev, "failed to set clock rate\n");
474 err = of_property_read_u32(dev->of_node, "nvidia,host1x-class", &host_class);
476 host_class = HOST1X_CLASS_NVDEC;
478 if (nvdec->config->has_riscv) {
481 mc = devm_tegra_memory_controller_get(dev);
483 dev_err_probe(dev, PTR_ERR(mc),
484 "failed to get memory controller handle\n");
488 err = tegra_mc_get_carveout_info(mc, 1, &nvdec->carveout_base, NULL);
490 dev_err(dev, "failed to get carveout info: %d\n", err);
494 nvdec->reset = devm_reset_control_get_exclusive_released(dev, "nvdec");
495 if (IS_ERR(nvdec->reset)) {
496 dev_err_probe(dev, PTR_ERR(nvdec->reset), "failed to get reset\n");
497 return PTR_ERR(nvdec->reset);
500 nvdec->riscv.dev = dev;
501 nvdec->riscv.regs = nvdec->regs;
503 err = tegra_drm_riscv_read_descriptors(&nvdec->riscv);
507 nvdec->falcon.dev = dev;
508 nvdec->falcon.regs = nvdec->regs;
510 err = falcon_init(&nvdec->falcon);
515 platform_set_drvdata(pdev, nvdec);
517 INIT_LIST_HEAD(&nvdec->client.base.list);
518 nvdec->client.base.ops = &nvdec_client_ops;
519 nvdec->client.base.dev = dev;
520 nvdec->client.base.class = host_class;
521 nvdec->client.base.syncpts = syncpts;
522 nvdec->client.base.num_syncpts = 1;
525 INIT_LIST_HEAD(&nvdec->client.list);
526 nvdec->client.version = nvdec->config->version;
527 nvdec->client.ops = &nvdec_ops;
529 err = host1x_client_register(&nvdec->client.base);
531 dev_err(dev, "failed to register host1x client: %d\n", err);
535 pm_runtime_enable(dev);
536 pm_runtime_use_autosuspend(dev);
537 pm_runtime_set_autosuspend_delay(dev, 500);
542 falcon_exit(&nvdec->falcon);
547 static void nvdec_remove(struct platform_device *pdev)
549 struct nvdec *nvdec = platform_get_drvdata(pdev);
551 pm_runtime_disable(&pdev->dev);
552 host1x_client_unregister(&nvdec->client.base);
553 falcon_exit(&nvdec->falcon);
556 static const struct dev_pm_ops nvdec_pm_ops = {
557 SET_RUNTIME_PM_OPS(nvdec_runtime_suspend, nvdec_runtime_resume, NULL)
558 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
559 pm_runtime_force_resume)
562 struct platform_driver tegra_nvdec_driver = {
564 .name = "tegra-nvdec",
565 .of_match_table = tegra_nvdec_of_match,
568 .probe = nvdec_probe,
569 .remove_new = nvdec_remove,
572 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
573 MODULE_FIRMWARE(NVIDIA_TEGRA_210_NVDEC_FIRMWARE);
575 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
576 MODULE_FIRMWARE(NVIDIA_TEGRA_186_NVDEC_FIRMWARE);
578 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
579 MODULE_FIRMWARE(NVIDIA_TEGRA_194_NVDEC_FIRMWARE);