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>
37 struct tegra_drm_client client;
38 struct host1x_channel *channel;
39 struct iommu_domain *domain;
42 struct reset_control *rst;
44 /* Platform configuration */
45 const struct vic_config *config;
48 static inline struct vic *to_vic(struct tegra_drm_client *client)
50 return container_of(client, struct vic, client);
53 static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
55 writel(value, vic->regs + offset);
58 static int vic_runtime_resume(struct device *dev)
60 struct vic *vic = dev_get_drvdata(dev);
63 err = clk_prepare_enable(vic->clk);
69 err = reset_control_deassert(vic->rst);
78 clk_disable_unprepare(vic->clk);
82 static int vic_runtime_suspend(struct device *dev)
84 struct vic *vic = dev_get_drvdata(dev);
87 err = reset_control_assert(vic->rst);
91 usleep_range(2000, 4000);
93 clk_disable_unprepare(vic->clk);
100 static int vic_boot(struct vic *vic)
102 u32 fce_ucode_size, fce_bin_data_offset;
109 if (vic->config->supports_sid) {
110 struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev);
113 value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
114 TRANSCFG_ATT(0, TRANSCFG_SID_HW);
115 vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
117 if (spec && spec->num_ids > 0) {
118 value = spec->ids[0] & 0xffff;
120 vic_writel(vic, value, VIC_THI_STREAMID0);
121 vic_writel(vic, value, VIC_THI_STREAMID1);
125 /* setup clockgating registers */
126 vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
128 CG_WAKEUP_DLY_CNT(4),
129 NV_PVIC_MISC_PRI_VIC_CG);
131 err = falcon_boot(&vic->falcon);
135 hdr = vic->falcon.firmware.vaddr;
136 fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
137 hdr = vic->falcon.firmware.vaddr +
138 *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
139 fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
141 falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
142 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
144 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
145 (vic->falcon.firmware.paddr + fce_bin_data_offset)
148 err = falcon_wait_idle(&vic->falcon);
151 "failed to set application ID and FCE base\n");
160 static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
163 struct tegra_drm *tegra = falcon->data;
165 return tegra_drm_alloc(tegra, size, iova);
168 static void vic_falcon_free(struct falcon *falcon, size_t size,
169 dma_addr_t iova, void *va)
171 struct tegra_drm *tegra = falcon->data;
173 return tegra_drm_free(tegra, size, va, iova);
176 static const struct falcon_ops vic_falcon_ops = {
177 .alloc = vic_falcon_alloc,
178 .free = vic_falcon_free
181 static int vic_init(struct host1x_client *client)
183 struct tegra_drm_client *drm = host1x_to_drm_client(client);
184 struct iommu_group *group = iommu_group_get(client->dev);
185 struct drm_device *dev = dev_get_drvdata(client->parent);
186 struct tegra_drm *tegra = dev->dev_private;
187 struct vic *vic = to_vic(drm);
190 if (group && tegra->domain) {
191 err = iommu_attach_group(tegra->domain, group);
193 dev_err(vic->dev, "failed to attach to domain: %d\n",
198 vic->domain = tegra->domain;
201 vic->channel = host1x_channel_request(client->dev);
207 client->syncpts[0] = host1x_syncpt_request(client, 0);
208 if (!client->syncpts[0]) {
213 err = tegra_drm_register_client(tegra, drm);
220 host1x_syncpt_free(client->syncpts[0]);
222 host1x_channel_put(vic->channel);
224 if (group && tegra->domain)
225 iommu_detach_group(tegra->domain, group);
230 static int vic_exit(struct host1x_client *client)
232 struct tegra_drm_client *drm = host1x_to_drm_client(client);
233 struct iommu_group *group = iommu_group_get(client->dev);
234 struct drm_device *dev = dev_get_drvdata(client->parent);
235 struct tegra_drm *tegra = dev->dev_private;
236 struct vic *vic = to_vic(drm);
239 err = tegra_drm_unregister_client(tegra, drm);
243 host1x_syncpt_free(client->syncpts[0]);
244 host1x_channel_put(vic->channel);
247 iommu_detach_group(vic->domain, group);
254 static const struct host1x_client_ops vic_client_ops = {
259 static int vic_load_firmware(struct vic *vic)
263 if (vic->falcon.data)
266 vic->falcon.data = vic->client.drm;
268 err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
272 err = falcon_load_firmware(&vic->falcon);
279 vic->falcon.data = NULL;
283 static int vic_open_channel(struct tegra_drm_client *client,
284 struct tegra_drm_context *context)
286 struct vic *vic = to_vic(client);
289 err = pm_runtime_get_sync(vic->dev);
293 err = vic_load_firmware(vic);
301 context->channel = host1x_channel_get(vic->channel);
302 if (!context->channel) {
310 pm_runtime_put(vic->dev);
314 static void vic_close_channel(struct tegra_drm_context *context)
316 struct vic *vic = to_vic(context->client);
318 host1x_channel_put(context->channel);
320 pm_runtime_put(vic->dev);
323 static const struct tegra_drm_client_ops vic_ops = {
324 .open_channel = vic_open_channel,
325 .close_channel = vic_close_channel,
326 .submit = tegra_drm_submit,
329 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
331 static const struct vic_config vic_t124_config = {
332 .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
334 .supports_sid = false,
337 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
339 static const struct vic_config vic_t210_config = {
340 .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
342 .supports_sid = false,
345 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
347 static const struct vic_config vic_t186_config = {
348 .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
350 .supports_sid = true,
353 #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
355 static const struct vic_config vic_t194_config = {
356 .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
358 .supports_sid = true,
361 static const struct of_device_id vic_match[] = {
362 { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
363 { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
364 { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
365 { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
369 static int vic_probe(struct platform_device *pdev)
371 struct device *dev = &pdev->dev;
372 struct host1x_syncpt **syncpts;
373 struct resource *regs;
377 vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
381 vic->config = of_device_get_match_data(dev);
383 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
387 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
389 dev_err(&pdev->dev, "failed to get registers\n");
393 vic->regs = devm_ioremap_resource(dev, regs);
394 if (IS_ERR(vic->regs))
395 return PTR_ERR(vic->regs);
397 vic->clk = devm_clk_get(dev, NULL);
398 if (IS_ERR(vic->clk)) {
399 dev_err(&pdev->dev, "failed to get clock\n");
400 return PTR_ERR(vic->clk);
403 if (!dev->pm_domain) {
404 vic->rst = devm_reset_control_get(dev, "vic");
405 if (IS_ERR(vic->rst)) {
406 dev_err(&pdev->dev, "failed to get reset\n");
407 return PTR_ERR(vic->rst);
411 vic->falcon.dev = dev;
412 vic->falcon.regs = vic->regs;
413 vic->falcon.ops = &vic_falcon_ops;
415 err = falcon_init(&vic->falcon);
419 platform_set_drvdata(pdev, vic);
421 INIT_LIST_HEAD(&vic->client.base.list);
422 vic->client.base.ops = &vic_client_ops;
423 vic->client.base.dev = dev;
424 vic->client.base.class = HOST1X_CLASS_VIC;
425 vic->client.base.syncpts = syncpts;
426 vic->client.base.num_syncpts = 1;
429 INIT_LIST_HEAD(&vic->client.list);
430 vic->client.version = vic->config->version;
431 vic->client.ops = &vic_ops;
433 err = host1x_client_register(&vic->client.base);
435 dev_err(dev, "failed to register host1x client: %d\n", err);
439 pm_runtime_enable(&pdev->dev);
440 if (!pm_runtime_enabled(&pdev->dev)) {
441 err = vic_runtime_resume(&pdev->dev);
443 goto unregister_client;
449 host1x_client_unregister(&vic->client.base);
451 falcon_exit(&vic->falcon);
456 static int vic_remove(struct platform_device *pdev)
458 struct vic *vic = platform_get_drvdata(pdev);
461 err = host1x_client_unregister(&vic->client.base);
463 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
468 if (pm_runtime_enabled(&pdev->dev))
469 pm_runtime_disable(&pdev->dev);
471 vic_runtime_suspend(&pdev->dev);
473 falcon_exit(&vic->falcon);
478 static const struct dev_pm_ops vic_pm_ops = {
479 SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
482 struct platform_driver tegra_vic_driver = {
485 .of_match_table = vic_match,
489 .remove = vic_remove,
492 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
493 MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
495 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
496 MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
498 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
499 MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
501 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
502 MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);