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/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
20 #include <soc/tegra/mc.h>
27 #define NVDEC_FALCON_DEBUGINFO 0x1094
28 #define NVDEC_TFBIF_TRANSCFG 0x2c44
35 bool has_extra_clocks;
42 struct tegra_drm_client client;
43 struct host1x_channel *channel;
45 struct clk_bulk_data clks[3];
46 unsigned int num_clks;
47 struct reset_control *reset;
49 /* Platform configuration */
50 const struct nvdec_config *config;
52 /* RISC-V specific data */
53 struct tegra_drm_riscv riscv;
54 phys_addr_t carveout_base;
57 static inline struct nvdec *to_nvdec(struct tegra_drm_client *client)
59 return container_of(client, struct nvdec, client);
62 static inline void nvdec_writel(struct nvdec *nvdec, u32 value,
65 writel(value, nvdec->regs + offset);
68 static int nvdec_boot_falcon(struct nvdec *nvdec)
73 if (nvdec->config->supports_sid && tegra_dev_iommu_get_stream_id(nvdec->dev, &stream_id)) {
76 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) | TRANSCFG_ATT(0, TRANSCFG_SID_HW);
77 nvdec_writel(nvdec, value, NVDEC_TFBIF_TRANSCFG);
79 nvdec_writel(nvdec, stream_id, VIC_THI_STREAMID0);
80 nvdec_writel(nvdec, stream_id, VIC_THI_STREAMID1);
83 err = falcon_boot(&nvdec->falcon);
87 err = falcon_wait_idle(&nvdec->falcon);
89 dev_err(nvdec->dev, "falcon boot timed out\n");
96 static int nvdec_wait_debuginfo(struct nvdec *nvdec, const char *phase)
101 err = readl_poll_timeout(nvdec->regs + NVDEC_FALCON_DEBUGINFO, val, val == 0x0, 10, 100000);
103 dev_err(nvdec->dev, "failed to boot %s, debuginfo=0x%x\n", phase, val);
110 static int nvdec_boot_riscv(struct nvdec *nvdec)
114 err = reset_control_acquire(nvdec->reset);
118 nvdec_writel(nvdec, 0xabcd1234, NVDEC_FALCON_DEBUGINFO);
120 err = tegra_drm_riscv_boot_bootrom(&nvdec->riscv, nvdec->carveout_base, 1,
121 &nvdec->riscv.bl_desc);
123 dev_err(nvdec->dev, "failed to execute bootloader\n");
127 err = nvdec_wait_debuginfo(nvdec, "bootloader");
131 err = reset_control_reset(nvdec->reset);
135 nvdec_writel(nvdec, 0xabcd1234, NVDEC_FALCON_DEBUGINFO);
137 err = tegra_drm_riscv_boot_bootrom(&nvdec->riscv, nvdec->carveout_base, 1,
138 &nvdec->riscv.os_desc);
140 dev_err(nvdec->dev, "failed to execute firmware\n");
144 err = nvdec_wait_debuginfo(nvdec, "firmware");
149 reset_control_release(nvdec->reset);
154 static int nvdec_init(struct host1x_client *client)
156 struct tegra_drm_client *drm = host1x_to_drm_client(client);
157 struct drm_device *dev = dev_get_drvdata(client->host);
158 struct tegra_drm *tegra = dev->dev_private;
159 struct nvdec *nvdec = to_nvdec(drm);
162 err = host1x_client_iommu_attach(client);
163 if (err < 0 && err != -ENODEV) {
164 dev_err(nvdec->dev, "failed to attach to domain: %d\n", err);
168 nvdec->channel = host1x_channel_request(client);
169 if (!nvdec->channel) {
174 client->syncpts[0] = host1x_syncpt_request(client, 0);
175 if (!client->syncpts[0]) {
180 pm_runtime_enable(client->dev);
181 pm_runtime_use_autosuspend(client->dev);
182 pm_runtime_set_autosuspend_delay(client->dev, 500);
184 err = tegra_drm_register_client(tegra, drm);
189 * Inherit the DMA parameters (such as maximum segment size) from the
190 * parent host1x device.
192 client->dev->dma_parms = client->host->dma_parms;
197 pm_runtime_dont_use_autosuspend(client->dev);
198 pm_runtime_force_suspend(client->dev);
200 host1x_syncpt_put(client->syncpts[0]);
202 host1x_channel_put(nvdec->channel);
204 host1x_client_iommu_detach(client);
209 static int nvdec_exit(struct host1x_client *client)
211 struct tegra_drm_client *drm = host1x_to_drm_client(client);
212 struct drm_device *dev = dev_get_drvdata(client->host);
213 struct tegra_drm *tegra = dev->dev_private;
214 struct nvdec *nvdec = to_nvdec(drm);
217 /* avoid a dangling pointer just in case this disappears */
218 client->dev->dma_parms = NULL;
220 err = tegra_drm_unregister_client(tegra, drm);
224 pm_runtime_dont_use_autosuspend(client->dev);
225 pm_runtime_force_suspend(client->dev);
227 host1x_syncpt_put(client->syncpts[0]);
228 host1x_channel_put(nvdec->channel);
229 host1x_client_iommu_detach(client);
231 nvdec->channel = NULL;
234 dma_unmap_single(nvdec->dev, nvdec->falcon.firmware.phys,
235 nvdec->falcon.firmware.size, DMA_TO_DEVICE);
236 tegra_drm_free(tegra, nvdec->falcon.firmware.size,
237 nvdec->falcon.firmware.virt,
238 nvdec->falcon.firmware.iova);
240 dma_free_coherent(nvdec->dev, nvdec->falcon.firmware.size,
241 nvdec->falcon.firmware.virt,
242 nvdec->falcon.firmware.iova);
248 static const struct host1x_client_ops nvdec_client_ops = {
253 static int nvdec_load_falcon_firmware(struct nvdec *nvdec)
255 struct host1x_client *client = &nvdec->client.base;
256 struct tegra_drm *tegra = nvdec->client.drm;
262 if (nvdec->falcon.firmware.virt)
265 err = falcon_read_firmware(&nvdec->falcon, nvdec->config->firmware);
269 size = nvdec->falcon.firmware.size;
271 if (!client->group) {
272 virt = dma_alloc_coherent(nvdec->dev, size, &iova, GFP_KERNEL);
274 err = dma_mapping_error(nvdec->dev, iova);
278 virt = tegra_drm_alloc(tegra, size, &iova);
281 nvdec->falcon.firmware.virt = virt;
282 nvdec->falcon.firmware.iova = iova;
284 err = falcon_load_firmware(&nvdec->falcon);
289 * In this case we have received an IOVA from the shared domain, so we
290 * need to make sure to get the physical address so that the DMA API
291 * knows what memory pages to flush the cache for.
296 phys = dma_map_single(nvdec->dev, virt, size, DMA_TO_DEVICE);
298 err = dma_mapping_error(nvdec->dev, phys);
302 nvdec->falcon.firmware.phys = phys;
309 dma_free_coherent(nvdec->dev, size, virt, iova);
311 tegra_drm_free(tegra, size, virt, iova);
316 static __maybe_unused int nvdec_runtime_resume(struct device *dev)
318 struct nvdec *nvdec = dev_get_drvdata(dev);
321 err = clk_bulk_prepare_enable(nvdec->num_clks, nvdec->clks);
325 usleep_range(10, 20);
327 if (nvdec->config->has_riscv) {
328 err = nvdec_boot_riscv(nvdec);
332 err = nvdec_load_falcon_firmware(nvdec);
336 err = nvdec_boot_falcon(nvdec);
344 clk_bulk_disable_unprepare(nvdec->num_clks, nvdec->clks);
348 static __maybe_unused int nvdec_runtime_suspend(struct device *dev)
350 struct nvdec *nvdec = dev_get_drvdata(dev);
352 host1x_channel_stop(nvdec->channel);
354 clk_bulk_disable_unprepare(nvdec->num_clks, nvdec->clks);
359 static int nvdec_open_channel(struct tegra_drm_client *client,
360 struct tegra_drm_context *context)
362 struct nvdec *nvdec = to_nvdec(client);
364 context->channel = host1x_channel_get(nvdec->channel);
365 if (!context->channel)
371 static void nvdec_close_channel(struct tegra_drm_context *context)
373 host1x_channel_put(context->channel);
376 static int nvdec_can_use_memory_ctx(struct tegra_drm_client *client, bool *supported)
383 static const struct tegra_drm_client_ops nvdec_ops = {
384 .open_channel = nvdec_open_channel,
385 .close_channel = nvdec_close_channel,
386 .submit = tegra_drm_submit,
387 .get_streamid_offset = tegra_drm_get_streamid_offset_thi,
388 .can_use_memory_ctx = nvdec_can_use_memory_ctx,
391 #define NVIDIA_TEGRA_210_NVDEC_FIRMWARE "nvidia/tegra210/nvdec.bin"
393 static const struct nvdec_config nvdec_t210_config = {
394 .firmware = NVIDIA_TEGRA_210_NVDEC_FIRMWARE,
396 .supports_sid = false,
399 #define NVIDIA_TEGRA_186_NVDEC_FIRMWARE "nvidia/tegra186/nvdec.bin"
401 static const struct nvdec_config nvdec_t186_config = {
402 .firmware = NVIDIA_TEGRA_186_NVDEC_FIRMWARE,
404 .supports_sid = true,
407 #define NVIDIA_TEGRA_194_NVDEC_FIRMWARE "nvidia/tegra194/nvdec.bin"
409 static const struct nvdec_config nvdec_t194_config = {
410 .firmware = NVIDIA_TEGRA_194_NVDEC_FIRMWARE,
412 .supports_sid = true,
415 static const struct nvdec_config nvdec_t234_config = {
417 .supports_sid = true,
419 .has_extra_clocks = true,
422 static const struct of_device_id tegra_nvdec_of_match[] = {
423 { .compatible = "nvidia,tegra210-nvdec", .data = &nvdec_t210_config },
424 { .compatible = "nvidia,tegra186-nvdec", .data = &nvdec_t186_config },
425 { .compatible = "nvidia,tegra194-nvdec", .data = &nvdec_t194_config },
426 { .compatible = "nvidia,tegra234-nvdec", .data = &nvdec_t234_config },
429 MODULE_DEVICE_TABLE(of, tegra_nvdec_of_match);
431 static int nvdec_probe(struct platform_device *pdev)
433 struct device *dev = &pdev->dev;
434 struct host1x_syncpt **syncpts;
439 /* inherit DMA mask from host1x parent */
440 err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
442 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
446 nvdec = devm_kzalloc(dev, sizeof(*nvdec), GFP_KERNEL);
450 nvdec->config = of_device_get_match_data(dev);
452 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
456 nvdec->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
457 if (IS_ERR(nvdec->regs))
458 return PTR_ERR(nvdec->regs);
460 nvdec->clks[0].id = "nvdec";
463 if (nvdec->config->has_extra_clocks) {
465 nvdec->clks[1].id = "fuse";
466 nvdec->clks[2].id = "tsec_pka";
469 err = devm_clk_bulk_get(dev, nvdec->num_clks, nvdec->clks);
471 dev_err(&pdev->dev, "failed to get clock(s)\n");
475 err = clk_set_rate(nvdec->clks[0].clk, ULONG_MAX);
477 dev_err(&pdev->dev, "failed to set clock rate\n");
481 err = of_property_read_u32(dev->of_node, "nvidia,host1x-class", &host_class);
483 host_class = HOST1X_CLASS_NVDEC;
485 if (nvdec->config->has_riscv) {
488 mc = devm_tegra_memory_controller_get(dev);
490 dev_err_probe(dev, PTR_ERR(mc),
491 "failed to get memory controller handle\n");
495 err = tegra_mc_get_carveout_info(mc, 1, &nvdec->carveout_base, NULL);
497 dev_err(dev, "failed to get carveout info: %d\n", err);
501 nvdec->reset = devm_reset_control_get_exclusive_released(dev, "nvdec");
502 if (IS_ERR(nvdec->reset)) {
503 dev_err_probe(dev, PTR_ERR(nvdec->reset), "failed to get reset\n");
504 return PTR_ERR(nvdec->reset);
507 nvdec->riscv.dev = dev;
508 nvdec->riscv.regs = nvdec->regs;
510 err = tegra_drm_riscv_read_descriptors(&nvdec->riscv);
514 nvdec->falcon.dev = dev;
515 nvdec->falcon.regs = nvdec->regs;
517 err = falcon_init(&nvdec->falcon);
522 platform_set_drvdata(pdev, nvdec);
524 INIT_LIST_HEAD(&nvdec->client.base.list);
525 nvdec->client.base.ops = &nvdec_client_ops;
526 nvdec->client.base.dev = dev;
527 nvdec->client.base.class = host_class;
528 nvdec->client.base.syncpts = syncpts;
529 nvdec->client.base.num_syncpts = 1;
532 INIT_LIST_HEAD(&nvdec->client.list);
533 nvdec->client.version = nvdec->config->version;
534 nvdec->client.ops = &nvdec_ops;
536 err = host1x_client_register(&nvdec->client.base);
538 dev_err(dev, "failed to register host1x client: %d\n", err);
545 falcon_exit(&nvdec->falcon);
550 static void nvdec_remove(struct platform_device *pdev)
552 struct nvdec *nvdec = platform_get_drvdata(pdev);
554 host1x_client_unregister(&nvdec->client.base);
556 falcon_exit(&nvdec->falcon);
559 static const struct dev_pm_ops nvdec_pm_ops = {
560 SET_RUNTIME_PM_OPS(nvdec_runtime_suspend, nvdec_runtime_resume, NULL)
561 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
562 pm_runtime_force_resume)
565 struct platform_driver tegra_nvdec_driver = {
567 .name = "tegra-nvdec",
568 .of_match_table = tegra_nvdec_of_match,
571 .probe = nvdec_probe,
572 .remove_new = nvdec_remove,
575 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
576 MODULE_FIRMWARE(NVIDIA_TEGRA_210_NVDEC_FIRMWARE);
578 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
579 MODULE_FIRMWARE(NVIDIA_TEGRA_186_NVDEC_FIRMWARE);
581 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
582 MODULE_FIRMWARE(NVIDIA_TEGRA_194_NVDEC_FIRMWARE);