1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2021, 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;
39 /* Platform configuration */
40 const struct nvdec_config *config;
43 static inline struct nvdec *to_nvdec(struct tegra_drm_client *client)
45 return container_of(client, struct nvdec, client);
48 static inline void nvdec_writel(struct nvdec *nvdec, u32 value,
51 writel(value, nvdec->regs + offset);
54 static int nvdec_boot(struct nvdec *nvdec)
56 #ifdef CONFIG_IOMMU_API
57 struct iommu_fwspec *spec = dev_iommu_fwspec_get(nvdec->dev);
61 #ifdef CONFIG_IOMMU_API
62 if (nvdec->config->supports_sid && spec) {
65 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) | TRANSCFG_ATT(0, TRANSCFG_SID_HW);
66 nvdec_writel(nvdec, value, VIC_TFBIF_TRANSCFG);
68 if (spec->num_ids > 0) {
69 value = spec->ids[0] & 0xffff;
71 nvdec_writel(nvdec, value, VIC_THI_STREAMID0);
72 nvdec_writel(nvdec, value, VIC_THI_STREAMID1);
77 err = falcon_boot(&nvdec->falcon);
81 err = falcon_wait_idle(&nvdec->falcon);
83 dev_err(nvdec->dev, "falcon boot timed out\n");
90 static int nvdec_init(struct host1x_client *client)
92 struct tegra_drm_client *drm = host1x_to_drm_client(client);
93 struct drm_device *dev = dev_get_drvdata(client->host);
94 struct tegra_drm *tegra = dev->dev_private;
95 struct nvdec *nvdec = to_nvdec(drm);
98 err = host1x_client_iommu_attach(client);
99 if (err < 0 && err != -ENODEV) {
100 dev_err(nvdec->dev, "failed to attach to domain: %d\n", err);
104 nvdec->channel = host1x_channel_request(client);
105 if (!nvdec->channel) {
110 client->syncpts[0] = host1x_syncpt_request(client, 0);
111 if (!client->syncpts[0]) {
116 pm_runtime_enable(client->dev);
117 pm_runtime_use_autosuspend(client->dev);
118 pm_runtime_set_autosuspend_delay(client->dev, 500);
120 err = tegra_drm_register_client(tegra, drm);
125 * Inherit the DMA parameters (such as maximum segment size) from the
126 * parent host1x device.
128 client->dev->dma_parms = client->host->dma_parms;
133 pm_runtime_dont_use_autosuspend(client->dev);
134 pm_runtime_force_suspend(client->dev);
136 host1x_syncpt_put(client->syncpts[0]);
138 host1x_channel_put(nvdec->channel);
140 host1x_client_iommu_detach(client);
145 static int nvdec_exit(struct host1x_client *client)
147 struct tegra_drm_client *drm = host1x_to_drm_client(client);
148 struct drm_device *dev = dev_get_drvdata(client->host);
149 struct tegra_drm *tegra = dev->dev_private;
150 struct nvdec *nvdec = to_nvdec(drm);
153 /* avoid a dangling pointer just in case this disappears */
154 client->dev->dma_parms = NULL;
156 err = tegra_drm_unregister_client(tegra, drm);
160 pm_runtime_dont_use_autosuspend(client->dev);
161 pm_runtime_force_suspend(client->dev);
163 host1x_syncpt_put(client->syncpts[0]);
164 host1x_channel_put(nvdec->channel);
165 host1x_client_iommu_detach(client);
167 nvdec->channel = NULL;
170 dma_unmap_single(nvdec->dev, nvdec->falcon.firmware.phys,
171 nvdec->falcon.firmware.size, DMA_TO_DEVICE);
172 tegra_drm_free(tegra, nvdec->falcon.firmware.size,
173 nvdec->falcon.firmware.virt,
174 nvdec->falcon.firmware.iova);
176 dma_free_coherent(nvdec->dev, nvdec->falcon.firmware.size,
177 nvdec->falcon.firmware.virt,
178 nvdec->falcon.firmware.iova);
184 static const struct host1x_client_ops nvdec_client_ops = {
189 static int nvdec_load_firmware(struct nvdec *nvdec)
191 struct host1x_client *client = &nvdec->client.base;
192 struct tegra_drm *tegra = nvdec->client.drm;
198 if (nvdec->falcon.firmware.virt)
201 err = falcon_read_firmware(&nvdec->falcon, nvdec->config->firmware);
205 size = nvdec->falcon.firmware.size;
207 if (!client->group) {
208 virt = dma_alloc_coherent(nvdec->dev, size, &iova, GFP_KERNEL);
210 err = dma_mapping_error(nvdec->dev, iova);
214 virt = tegra_drm_alloc(tegra, size, &iova);
217 nvdec->falcon.firmware.virt = virt;
218 nvdec->falcon.firmware.iova = iova;
220 err = falcon_load_firmware(&nvdec->falcon);
225 * In this case we have received an IOVA from the shared domain, so we
226 * need to make sure to get the physical address so that the DMA API
227 * knows what memory pages to flush the cache for.
232 phys = dma_map_single(nvdec->dev, virt, size, DMA_TO_DEVICE);
234 err = dma_mapping_error(nvdec->dev, phys);
238 nvdec->falcon.firmware.phys = phys;
245 dma_free_coherent(nvdec->dev, size, virt, iova);
247 tegra_drm_free(tegra, size, virt, iova);
253 static __maybe_unused int nvdec_runtime_resume(struct device *dev)
255 struct nvdec *nvdec = dev_get_drvdata(dev);
258 err = clk_prepare_enable(nvdec->clk);
262 usleep_range(10, 20);
264 err = nvdec_load_firmware(nvdec);
268 err = nvdec_boot(nvdec);
275 clk_disable_unprepare(nvdec->clk);
279 static __maybe_unused int nvdec_runtime_suspend(struct device *dev)
281 struct nvdec *nvdec = dev_get_drvdata(dev);
283 host1x_channel_stop(nvdec->channel);
285 clk_disable_unprepare(nvdec->clk);
290 static int nvdec_open_channel(struct tegra_drm_client *client,
291 struct tegra_drm_context *context)
293 struct nvdec *nvdec = to_nvdec(client);
295 context->channel = host1x_channel_get(nvdec->channel);
296 if (!context->channel)
302 static void nvdec_close_channel(struct tegra_drm_context *context)
304 host1x_channel_put(context->channel);
307 static const struct tegra_drm_client_ops nvdec_ops = {
308 .open_channel = nvdec_open_channel,
309 .close_channel = nvdec_close_channel,
310 .submit = tegra_drm_submit,
313 #define NVIDIA_TEGRA_210_NVDEC_FIRMWARE "/*(DEBLOBBED)*/"
315 static const struct nvdec_config nvdec_t210_config = {
316 .firmware = NVIDIA_TEGRA_210_NVDEC_FIRMWARE,
318 .supports_sid = false,
321 #define NVIDIA_TEGRA_186_NVDEC_FIRMWARE "/*(DEBLOBBED)*/"
323 static const struct nvdec_config nvdec_t186_config = {
324 .firmware = NVIDIA_TEGRA_186_NVDEC_FIRMWARE,
326 .supports_sid = true,
329 #define NVIDIA_TEGRA_194_NVDEC_FIRMWARE "/*(DEBLOBBED)*/"
331 static const struct nvdec_config nvdec_t194_config = {
332 .firmware = NVIDIA_TEGRA_194_NVDEC_FIRMWARE,
334 .supports_sid = true,
337 static const struct of_device_id tegra_nvdec_of_match[] = {
338 { .compatible = "nvidia,tegra210-nvdec", .data = &nvdec_t210_config },
339 { .compatible = "nvidia,tegra186-nvdec", .data = &nvdec_t186_config },
340 { .compatible = "nvidia,tegra194-nvdec", .data = &nvdec_t194_config },
343 MODULE_DEVICE_TABLE(of, tegra_nvdec_of_match);
345 static int nvdec_probe(struct platform_device *pdev)
347 struct device *dev = &pdev->dev;
348 struct host1x_syncpt **syncpts;
353 /* inherit DMA mask from host1x parent */
354 err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
356 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
360 nvdec = devm_kzalloc(dev, sizeof(*nvdec), GFP_KERNEL);
364 nvdec->config = of_device_get_match_data(dev);
366 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
370 nvdec->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
371 if (IS_ERR(nvdec->regs))
372 return PTR_ERR(nvdec->regs);
374 nvdec->clk = devm_clk_get(dev, NULL);
375 if (IS_ERR(nvdec->clk)) {
376 dev_err(&pdev->dev, "failed to get clock\n");
377 return PTR_ERR(nvdec->clk);
380 err = clk_set_rate(nvdec->clk, ULONG_MAX);
382 dev_err(&pdev->dev, "failed to set clock rate\n");
386 err = of_property_read_u32(dev->of_node, "nvidia,host1x-class", &host_class);
388 host_class = HOST1X_CLASS_NVDEC;
390 nvdec->falcon.dev = dev;
391 nvdec->falcon.regs = nvdec->regs;
393 err = falcon_init(&nvdec->falcon);
397 platform_set_drvdata(pdev, nvdec);
399 INIT_LIST_HEAD(&nvdec->client.base.list);
400 nvdec->client.base.ops = &nvdec_client_ops;
401 nvdec->client.base.dev = dev;
402 nvdec->client.base.class = host_class;
403 nvdec->client.base.syncpts = syncpts;
404 nvdec->client.base.num_syncpts = 1;
407 INIT_LIST_HEAD(&nvdec->client.list);
408 nvdec->client.version = nvdec->config->version;
409 nvdec->client.ops = &nvdec_ops;
411 err = host1x_client_register(&nvdec->client.base);
413 dev_err(dev, "failed to register host1x client: %d\n", err);
420 falcon_exit(&nvdec->falcon);
425 static int nvdec_remove(struct platform_device *pdev)
427 struct nvdec *nvdec = platform_get_drvdata(pdev);
430 err = host1x_client_unregister(&nvdec->client.base);
432 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
437 falcon_exit(&nvdec->falcon);
442 static const struct dev_pm_ops nvdec_pm_ops = {
443 SET_RUNTIME_PM_OPS(nvdec_runtime_suspend, nvdec_runtime_resume, NULL)
444 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
445 pm_runtime_force_resume)
448 struct platform_driver tegra_nvdec_driver = {
450 .name = "tegra-nvdec",
451 .of_match_table = tegra_nvdec_of_match,
454 .probe = nvdec_probe,
455 .remove = nvdec_remove,
458 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
461 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
464 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)