1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, NVIDIA Corporation.
7 #include <linux/host1x.h>
8 #include <linux/iommu.h>
9 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/reset.h>
17 #include <soc/tegra/pmc.h>
34 struct tegra_drm_client client;
35 struct host1x_channel *channel;
36 struct iommu_domain *domain;
39 struct reset_control *rst;
41 /* Platform configuration */
42 const struct vic_config *config;
45 static inline struct vic *to_vic(struct tegra_drm_client *client)
47 return container_of(client, struct vic, client);
50 static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
52 writel(value, vic->regs + offset);
55 static int vic_runtime_resume(struct device *dev)
57 struct vic *vic = dev_get_drvdata(dev);
60 err = clk_prepare_enable(vic->clk);
66 err = reset_control_deassert(vic->rst);
75 clk_disable_unprepare(vic->clk);
79 static int vic_runtime_suspend(struct device *dev)
81 struct vic *vic = dev_get_drvdata(dev);
84 err = reset_control_assert(vic->rst);
88 usleep_range(2000, 4000);
90 clk_disable_unprepare(vic->clk);
97 static int vic_boot(struct vic *vic)
99 u32 fce_ucode_size, fce_bin_data_offset;
106 #ifdef CONFIG_IOMMU_API
107 if (vic->config->supports_sid) {
108 struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev);
111 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
112 TRANSCFG_ATT(0, TRANSCFG_SID_HW);
113 vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
115 if (spec && spec->num_ids > 0) {
116 value = spec->ids[0] & 0xffff;
118 vic_writel(vic, value, VIC_THI_STREAMID0);
119 vic_writel(vic, value, VIC_THI_STREAMID1);
124 /* setup clockgating registers */
125 vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
127 CG_WAKEUP_DLY_CNT(4),
128 NV_PVIC_MISC_PRI_VIC_CG);
130 err = falcon_boot(&vic->falcon);
134 hdr = vic->falcon.firmware.vaddr;
135 fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
136 hdr = vic->falcon.firmware.vaddr +
137 *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
138 fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
140 falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
141 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
143 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
144 (vic->falcon.firmware.paddr + fce_bin_data_offset)
147 err = falcon_wait_idle(&vic->falcon);
150 "failed to set application ID and FCE base\n");
159 static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
162 struct tegra_drm *tegra = falcon->data;
164 return tegra_drm_alloc(tegra, size, iova);
167 static void vic_falcon_free(struct falcon *falcon, size_t size,
168 dma_addr_t iova, void *va)
170 struct tegra_drm *tegra = falcon->data;
172 return tegra_drm_free(tegra, size, va, iova);
175 static const struct falcon_ops vic_falcon_ops = {
176 .alloc = vic_falcon_alloc,
177 .free = vic_falcon_free
180 static int vic_init(struct host1x_client *client)
182 struct tegra_drm_client *drm = host1x_to_drm_client(client);
183 struct iommu_group *group = iommu_group_get(client->dev);
184 struct drm_device *dev = dev_get_drvdata(client->parent);
185 struct tegra_drm *tegra = dev->dev_private;
186 struct vic *vic = to_vic(drm);
189 if (group && tegra->domain) {
190 err = iommu_attach_group(tegra->domain, group);
192 dev_err(vic->dev, "failed to attach to domain: %d\n",
197 vic->domain = tegra->domain;
200 vic->channel = host1x_channel_request(client->dev);
206 client->syncpts[0] = host1x_syncpt_request(client, 0);
207 if (!client->syncpts[0]) {
212 err = tegra_drm_register_client(tegra, drm);
219 host1x_syncpt_free(client->syncpts[0]);
221 host1x_channel_put(vic->channel);
223 if (group && tegra->domain)
224 iommu_detach_group(tegra->domain, group);
229 static int vic_exit(struct host1x_client *client)
231 struct tegra_drm_client *drm = host1x_to_drm_client(client);
232 struct iommu_group *group = iommu_group_get(client->dev);
233 struct drm_device *dev = dev_get_drvdata(client->parent);
234 struct tegra_drm *tegra = dev->dev_private;
235 struct vic *vic = to_vic(drm);
238 err = tegra_drm_unregister_client(tegra, drm);
242 host1x_syncpt_free(client->syncpts[0]);
243 host1x_channel_put(vic->channel);
246 iommu_detach_group(vic->domain, group);
253 static const struct host1x_client_ops vic_client_ops = {
258 static int vic_load_firmware(struct vic *vic)
262 if (vic->falcon.data)
265 vic->falcon.data = vic->client.drm;
267 err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
271 err = falcon_load_firmware(&vic->falcon);
278 vic->falcon.data = NULL;
282 static int vic_open_channel(struct tegra_drm_client *client,
283 struct tegra_drm_context *context)
285 struct vic *vic = to_vic(client);
288 err = pm_runtime_get_sync(vic->dev);
292 err = vic_load_firmware(vic);
300 context->channel = host1x_channel_get(vic->channel);
301 if (!context->channel) {
309 pm_runtime_put(vic->dev);
313 static void vic_close_channel(struct tegra_drm_context *context)
315 struct vic *vic = to_vic(context->client);
317 host1x_channel_put(context->channel);
319 pm_runtime_put(vic->dev);
322 static const struct tegra_drm_client_ops vic_ops = {
323 .open_channel = vic_open_channel,
324 .close_channel = vic_close_channel,
325 .submit = tegra_drm_submit,
328 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "/*(DEBLOBBED)*/"
330 static const struct vic_config vic_t124_config = {
331 .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
333 .supports_sid = false,
336 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "/*(DEBLOBBED)*/"
338 static const struct vic_config vic_t210_config = {
339 .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
341 .supports_sid = false,
344 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "/*(DEBLOBBED)*/"
346 static const struct vic_config vic_t186_config = {
347 .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
349 .supports_sid = true,
352 #define NVIDIA_TEGRA_194_VIC_FIRMWARE "/*(DEBLOBBED)*/"
354 static const struct vic_config vic_t194_config = {
355 /*(DEBLOBBED)*/NVIDIA_TEGRA_194_VIC_FIRMWARE,
357 .supports_sid = true,
360 static const struct of_device_id vic_match[] = {
361 { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
362 { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
363 { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
364 { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
368 static int vic_probe(struct platform_device *pdev)
370 struct device *dev = &pdev->dev;
371 struct host1x_syncpt **syncpts;
372 struct resource *regs;
376 vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
380 vic->config = of_device_get_match_data(dev);
382 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
386 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
388 dev_err(&pdev->dev, "failed to get registers\n");
392 vic->regs = devm_ioremap_resource(dev, regs);
393 if (IS_ERR(vic->regs))
394 return PTR_ERR(vic->regs);
396 vic->clk = devm_clk_get(dev, NULL);
397 if (IS_ERR(vic->clk)) {
398 dev_err(&pdev->dev, "failed to get clock\n");
399 return PTR_ERR(vic->clk);
402 if (!dev->pm_domain) {
403 vic->rst = devm_reset_control_get(dev, "vic");
404 if (IS_ERR(vic->rst)) {
405 dev_err(&pdev->dev, "failed to get reset\n");
406 return PTR_ERR(vic->rst);
410 vic->falcon.dev = dev;
411 vic->falcon.regs = vic->regs;
412 vic->falcon.ops = &vic_falcon_ops;
414 err = falcon_init(&vic->falcon);
418 platform_set_drvdata(pdev, vic);
420 INIT_LIST_HEAD(&vic->client.base.list);
421 vic->client.base.ops = &vic_client_ops;
422 vic->client.base.dev = dev;
423 vic->client.base.class = HOST1X_CLASS_VIC;
424 vic->client.base.syncpts = syncpts;
425 vic->client.base.num_syncpts = 1;
428 INIT_LIST_HEAD(&vic->client.list);
429 vic->client.version = vic->config->version;
430 vic->client.ops = &vic_ops;
432 err = host1x_client_register(&vic->client.base);
434 dev_err(dev, "failed to register host1x client: %d\n", err);
438 pm_runtime_enable(&pdev->dev);
439 if (!pm_runtime_enabled(&pdev->dev)) {
440 err = vic_runtime_resume(&pdev->dev);
442 goto unregister_client;
448 host1x_client_unregister(&vic->client.base);
450 falcon_exit(&vic->falcon);
455 static int vic_remove(struct platform_device *pdev)
457 struct vic *vic = platform_get_drvdata(pdev);
460 err = host1x_client_unregister(&vic->client.base);
462 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
467 if (pm_runtime_enabled(&pdev->dev))
468 pm_runtime_disable(&pdev->dev);
470 vic_runtime_suspend(&pdev->dev);
472 falcon_exit(&vic->falcon);
477 static const struct dev_pm_ops vic_pm_ops = {
478 SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
481 struct platform_driver tegra_vic_driver = {
484 .of_match_table = vic_match,
488 .remove = vic_remove,
491 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
494 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
497 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
500 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)