2 * Copyright (c) 2015, NVIDIA Corporation.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/host1x.h>
11 #include <linux/iommu.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/pmc.h>
36 struct tegra_drm_client client
;
37 struct host1x_channel
*channel
;
38 struct iommu_domain
*domain
;
42 /* Platform configuration */
43 const struct vic_config
*config
;
46 static inline struct vic
*to_vic(struct tegra_drm_client
*client
)
48 return container_of(client
, struct vic
, client
);
51 static void vic_writel(struct vic
*vic
, u32 value
, unsigned int offset
)
53 writel(value
, vic
->regs
+ offset
);
56 static int vic_runtime_resume(struct device
*dev
)
58 struct vic
*vic
= dev_get_drvdata(dev
);
60 return clk_prepare_enable(vic
->clk
);
63 static int vic_runtime_suspend(struct device
*dev
)
65 struct vic
*vic
= dev_get_drvdata(dev
);
67 clk_disable_unprepare(vic
->clk
);
74 static int vic_boot(struct vic
*vic
)
76 u32 fce_ucode_size
, fce_bin_data_offset
;
83 /* setup clockgating registers */
84 vic_writel(vic
, CG_IDLE_CG_DLY_CNT(4) |
87 NV_PVIC_MISC_PRI_VIC_CG
);
89 err
= falcon_boot(&vic
->falcon
);
93 hdr
= vic
->falcon
.firmware
.vaddr
;
94 fce_bin_data_offset
= *(u32
*)(hdr
+ VIC_UCODE_FCE_DATA_OFFSET
);
95 hdr
= vic
->falcon
.firmware
.vaddr
+
96 *(u32
*)(hdr
+ VIC_UCODE_FCE_HEADER_OFFSET
);
97 fce_ucode_size
= *(u32
*)(hdr
+ FCE_UCODE_SIZE_OFFSET
);
99 falcon_execute_method(&vic
->falcon
, VIC_SET_APPLICATION_ID
, 1);
100 falcon_execute_method(&vic
->falcon
, VIC_SET_FCE_UCODE_SIZE
,
102 falcon_execute_method(&vic
->falcon
, VIC_SET_FCE_UCODE_OFFSET
,
103 (vic
->falcon
.firmware
.paddr
+ fce_bin_data_offset
)
106 err
= falcon_wait_idle(&vic
->falcon
);
109 "failed to set application ID and FCE base\n");
118 static void *vic_falcon_alloc(struct falcon
*falcon
, size_t size
,
121 struct tegra_drm
*tegra
= falcon
->data
;
123 return tegra_drm_alloc(tegra
, size
, iova
);
126 static void vic_falcon_free(struct falcon
*falcon
, size_t size
,
127 dma_addr_t iova
, void *va
)
129 struct tegra_drm
*tegra
= falcon
->data
;
131 return tegra_drm_free(tegra
, size
, va
, iova
);
134 static const struct falcon_ops vic_falcon_ops
= {
135 .alloc
= vic_falcon_alloc
,
136 .free
= vic_falcon_free
139 static int vic_init(struct host1x_client
*client
)
141 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
142 struct iommu_group
*group
= iommu_group_get(client
->dev
);
143 struct drm_device
*dev
= dev_get_drvdata(client
->parent
);
144 struct tegra_drm
*tegra
= dev
->dev_private
;
145 struct vic
*vic
= to_vic(drm
);
148 if (group
&& tegra
->domain
) {
149 err
= iommu_attach_group(tegra
->domain
, group
);
151 dev_err(vic
->dev
, "failed to attach to domain: %d\n",
156 vic
->domain
= tegra
->domain
;
159 if (!vic
->falcon
.data
) {
160 vic
->falcon
.data
= tegra
;
161 err
= falcon_load_firmware(&vic
->falcon
);
166 vic
->channel
= host1x_channel_request(client
->dev
);
172 client
->syncpts
[0] = host1x_syncpt_request(client
, 0);
173 if (!client
->syncpts
[0]) {
178 err
= tegra_drm_register_client(tegra
, drm
);
185 host1x_syncpt_free(client
->syncpts
[0]);
187 host1x_channel_put(vic
->channel
);
189 if (group
&& tegra
->domain
)
190 iommu_detach_group(tegra
->domain
, group
);
195 static int vic_exit(struct host1x_client
*client
)
197 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
198 struct iommu_group
*group
= iommu_group_get(client
->dev
);
199 struct drm_device
*dev
= dev_get_drvdata(client
->parent
);
200 struct tegra_drm
*tegra
= dev
->dev_private
;
201 struct vic
*vic
= to_vic(drm
);
204 err
= tegra_drm_unregister_client(tegra
, drm
);
208 host1x_syncpt_free(client
->syncpts
[0]);
209 host1x_channel_put(vic
->channel
);
212 iommu_detach_group(vic
->domain
, group
);
219 static const struct host1x_client_ops vic_client_ops
= {
224 static int vic_open_channel(struct tegra_drm_client
*client
,
225 struct tegra_drm_context
*context
)
227 struct vic
*vic
= to_vic(client
);
230 err
= pm_runtime_get_sync(vic
->dev
);
236 pm_runtime_put(vic
->dev
);
240 context
->channel
= host1x_channel_get(vic
->channel
);
241 if (!context
->channel
) {
242 pm_runtime_put(vic
->dev
);
249 static void vic_close_channel(struct tegra_drm_context
*context
)
251 struct vic
*vic
= to_vic(context
->client
);
253 host1x_channel_put(context
->channel
);
255 pm_runtime_put(vic
->dev
);
258 static const struct tegra_drm_client_ops vic_ops
= {
259 .open_channel
= vic_open_channel
,
260 .close_channel
= vic_close_channel
,
261 .submit
= tegra_drm_submit
,
264 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
266 static const struct vic_config vic_t124_config
= {
267 .firmware
= NVIDIA_TEGRA_124_VIC_FIRMWARE
,
271 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
273 static const struct vic_config vic_t210_config
= {
274 .firmware
= NVIDIA_TEGRA_210_VIC_FIRMWARE
,
278 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
280 static const struct vic_config vic_t186_config
= {
281 .firmware
= NVIDIA_TEGRA_186_VIC_FIRMWARE
,
285 static const struct of_device_id vic_match
[] = {
286 { .compatible
= "nvidia,tegra124-vic", .data
= &vic_t124_config
},
287 { .compatible
= "nvidia,tegra210-vic", .data
= &vic_t210_config
},
288 { .compatible
= "nvidia,tegra186-vic", .data
= &vic_t186_config
},
292 static int vic_probe(struct platform_device
*pdev
)
294 struct device
*dev
= &pdev
->dev
;
295 struct host1x_syncpt
**syncpts
;
296 struct resource
*regs
;
300 vic
= devm_kzalloc(dev
, sizeof(*vic
), GFP_KERNEL
);
304 vic
->config
= of_device_get_match_data(dev
);
306 syncpts
= devm_kzalloc(dev
, sizeof(*syncpts
), GFP_KERNEL
);
310 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
312 dev_err(&pdev
->dev
, "failed to get registers\n");
316 vic
->regs
= devm_ioremap_resource(dev
, regs
);
317 if (IS_ERR(vic
->regs
))
318 return PTR_ERR(vic
->regs
);
320 vic
->clk
= devm_clk_get(dev
, NULL
);
321 if (IS_ERR(vic
->clk
)) {
322 dev_err(&pdev
->dev
, "failed to get clock\n");
323 return PTR_ERR(vic
->clk
);
326 vic
->falcon
.dev
= dev
;
327 vic
->falcon
.regs
= vic
->regs
;
328 vic
->falcon
.ops
= &vic_falcon_ops
;
330 err
= falcon_init(&vic
->falcon
);
334 err
= falcon_read_firmware(&vic
->falcon
, vic
->config
->firmware
);
338 platform_set_drvdata(pdev
, vic
);
340 INIT_LIST_HEAD(&vic
->client
.base
.list
);
341 vic
->client
.base
.ops
= &vic_client_ops
;
342 vic
->client
.base
.dev
= dev
;
343 vic
->client
.base
.class = HOST1X_CLASS_VIC
;
344 vic
->client
.base
.syncpts
= syncpts
;
345 vic
->client
.base
.num_syncpts
= 1;
348 INIT_LIST_HEAD(&vic
->client
.list
);
349 vic
->client
.version
= vic
->config
->version
;
350 vic
->client
.ops
= &vic_ops
;
352 err
= host1x_client_register(&vic
->client
.base
);
354 dev_err(dev
, "failed to register host1x client: %d\n", err
);
355 platform_set_drvdata(pdev
, NULL
);
359 pm_runtime_enable(&pdev
->dev
);
360 if (!pm_runtime_enabled(&pdev
->dev
)) {
361 err
= vic_runtime_resume(&pdev
->dev
);
363 goto unregister_client
;
369 host1x_client_unregister(&vic
->client
.base
);
371 falcon_exit(&vic
->falcon
);
376 static int vic_remove(struct platform_device
*pdev
)
378 struct vic
*vic
= platform_get_drvdata(pdev
);
381 err
= host1x_client_unregister(&vic
->client
.base
);
383 dev_err(&pdev
->dev
, "failed to unregister host1x client: %d\n",
388 if (pm_runtime_enabled(&pdev
->dev
))
389 pm_runtime_disable(&pdev
->dev
);
391 vic_runtime_suspend(&pdev
->dev
);
393 falcon_exit(&vic
->falcon
);
398 static const struct dev_pm_ops vic_pm_ops
= {
399 SET_RUNTIME_PM_OPS(vic_runtime_suspend
, vic_runtime_resume
, NULL
)
402 struct platform_driver tegra_vic_driver
= {
405 .of_match_table
= vic_match
,
409 .remove
= vic_remove
,
412 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
413 MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE
);
415 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
416 MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE
);
418 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
419 MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE
);