1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20ae797a8SArto Merilainen /*
30ae797a8SArto Merilainen * Copyright (c) 2015, NVIDIA Corporation.
40ae797a8SArto Merilainen */
50ae797a8SArto Merilainen
60ae797a8SArto Merilainen #include <linux/clk.h>
7eb1df694SSam Ravnborg #include <linux/delay.h>
85566174cSRobin Murphy #include <linux/dma-mapping.h>
90ae797a8SArto Merilainen #include <linux/host1x.h>
100ae797a8SArto Merilainen #include <linux/iommu.h>
110ae797a8SArto Merilainen #include <linux/module.h>
120ae797a8SArto Merilainen #include <linux/of.h>
130ae797a8SArto Merilainen #include <linux/platform_device.h>
140ae797a8SArto Merilainen #include <linux/pm_runtime.h>
150ae797a8SArto Merilainen #include <linux/reset.h>
160ae797a8SArto Merilainen
170ae797a8SArto Merilainen #include <soc/tegra/pmc.h>
180ae797a8SArto Merilainen
190ae797a8SArto Merilainen #include "drm.h"
200ae797a8SArto Merilainen #include "falcon.h"
210ae797a8SArto Merilainen #include "vic.h"
220ae797a8SArto Merilainen
230ae797a8SArto Merilainen struct vic_config {
240ae797a8SArto Merilainen const char *firmware;
25acae8a9dSThierry Reding unsigned int version;
26f3779cb1SThierry Reding bool supports_sid;
270ae797a8SArto Merilainen };
280ae797a8SArto Merilainen
290ae797a8SArto Merilainen struct vic {
300ae797a8SArto Merilainen struct falcon falcon;
310ae797a8SArto Merilainen
320ae797a8SArto Merilainen void __iomem *regs;
330ae797a8SArto Merilainen struct tegra_drm_client client;
340ae797a8SArto Merilainen struct host1x_channel *channel;
350ae797a8SArto Merilainen struct device *dev;
360ae797a8SArto Merilainen struct clk *clk;
370dc34e19SThierry Reding struct reset_control *rst;
380ae797a8SArto Merilainen
39bf0297acSMikko Perttunen bool can_use_context;
40bf0297acSMikko Perttunen
410ae797a8SArto Merilainen /* Platform configuration */
420ae797a8SArto Merilainen const struct vic_config *config;
430ae797a8SArto Merilainen };
440ae797a8SArto Merilainen
to_vic(struct tegra_drm_client * client)450ae797a8SArto Merilainen static inline struct vic *to_vic(struct tegra_drm_client *client)
460ae797a8SArto Merilainen {
470ae797a8SArto Merilainen return container_of(client, struct vic, client);
480ae797a8SArto Merilainen }
490ae797a8SArto Merilainen
vic_writel(struct vic * vic,u32 value,unsigned int offset)500ae797a8SArto Merilainen static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
510ae797a8SArto Merilainen {
520ae797a8SArto Merilainen writel(value, vic->regs + offset);
530ae797a8SArto Merilainen }
540ae797a8SArto Merilainen
vic_boot(struct vic * vic)550ae797a8SArto Merilainen static int vic_boot(struct vic *vic)
560ae797a8SArto Merilainen {
57b50ad38dSThierry Reding u32 fce_ucode_size, fce_bin_data_offset, stream_id;
580ae797a8SArto Merilainen void *hdr;
590ae797a8SArto Merilainen int err = 0;
600ae797a8SArto Merilainen
61b50ad38dSThierry Reding if (vic->config->supports_sid && tegra_dev_iommu_get_stream_id(vic->dev, &stream_id)) {
62f3779cb1SThierry Reding u32 value;
63f3779cb1SThierry Reding
64f3779cb1SThierry Reding value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
65f3779cb1SThierry Reding TRANSCFG_ATT(0, TRANSCFG_SID_HW);
66f3779cb1SThierry Reding vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
67f3779cb1SThierry Reding
6859e520a6SMikko Perttunen /*
69b50ad38dSThierry Reding * STREAMID0 is used for input/output buffers. Initialize it to SID_VIC in case
70b50ad38dSThierry Reding * context isolation is not enabled, and SID_VIC is used for both firmware and
71b50ad38dSThierry Reding * data buffers.
7259e520a6SMikko Perttunen *
73b50ad38dSThierry Reding * If context isolation is enabled, it will be overridden by the SETSTREAMID
74b50ad38dSThierry Reding * opcode as part of each job.
7559e520a6SMikko Perttunen */
76b50ad38dSThierry Reding vic_writel(vic, stream_id, VIC_THI_STREAMID0);
7759e520a6SMikko Perttunen
7859e520a6SMikko Perttunen /* STREAMID1 is used for firmware loading. */
79b50ad38dSThierry Reding vic_writel(vic, stream_id, VIC_THI_STREAMID1);
80f3779cb1SThierry Reding }
81f3779cb1SThierry Reding
820ae797a8SArto Merilainen /* setup clockgating registers */
830ae797a8SArto Merilainen vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
840ae797a8SArto Merilainen CG_IDLE_CG_EN |
850ae797a8SArto Merilainen CG_WAKEUP_DLY_CNT(4),
860ae797a8SArto Merilainen NV_PVIC_MISC_PRI_VIC_CG);
870ae797a8SArto Merilainen
880ae797a8SArto Merilainen err = falcon_boot(&vic->falcon);
890ae797a8SArto Merilainen if (err < 0)
900ae797a8SArto Merilainen return err;
910ae797a8SArto Merilainen
92d972d624SThierry Reding hdr = vic->falcon.firmware.virt;
930ae797a8SArto Merilainen fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
9458ef3aebSMikko Perttunen
9558ef3aebSMikko Perttunen /* Old VIC firmware needs kernel help with setting up FCE microcode. */
9658ef3aebSMikko Perttunen if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
97d972d624SThierry Reding hdr = vic->falcon.firmware.virt +
980ae797a8SArto Merilainen *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
990ae797a8SArto Merilainen fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
1000ae797a8SArto Merilainen
1010ae797a8SArto Merilainen falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
1020ae797a8SArto Merilainen fce_ucode_size);
10358ef3aebSMikko Perttunen falcon_execute_method(
10458ef3aebSMikko Perttunen &vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
10558ef3aebSMikko Perttunen (vic->falcon.firmware.iova + fce_bin_data_offset) >> 8);
10658ef3aebSMikko Perttunen }
1070ae797a8SArto Merilainen
1080ae797a8SArto Merilainen err = falcon_wait_idle(&vic->falcon);
1090ae797a8SArto Merilainen if (err < 0) {
1100ae797a8SArto Merilainen dev_err(vic->dev,
1110ae797a8SArto Merilainen "failed to set application ID and FCE base\n");
1120ae797a8SArto Merilainen return err;
1130ae797a8SArto Merilainen }
1140ae797a8SArto Merilainen
1150ae797a8SArto Merilainen return 0;
1160ae797a8SArto Merilainen }
1170ae797a8SArto Merilainen
vic_init(struct host1x_client * client)1180ae797a8SArto Merilainen static int vic_init(struct host1x_client *client)
1190ae797a8SArto Merilainen {
1200ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client);
121608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host);
1220ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private;
1230ae797a8SArto Merilainen struct vic *vic = to_vic(drm);
1240ae797a8SArto Merilainen int err;
1250ae797a8SArto Merilainen
1267edd7961SThierry Reding err = host1x_client_iommu_attach(client);
127a8817489SThierry Reding if (err < 0 && err != -ENODEV) {
1287baa943eSThierry Reding dev_err(vic->dev, "failed to attach to domain: %d\n", err);
1290ae797a8SArto Merilainen return err;
1300ae797a8SArto Merilainen }
1310ae797a8SArto Merilainen
132caccddcfSThierry Reding vic->channel = host1x_channel_request(client);
1330ae797a8SArto Merilainen if (!vic->channel) {
1340ae797a8SArto Merilainen err = -ENOMEM;
135bc8828bdSThierry Reding goto detach;
1360ae797a8SArto Merilainen }
1370ae797a8SArto Merilainen
138617dd7ccSThierry Reding client->syncpts[0] = host1x_syncpt_request(client, 0);
1390ae797a8SArto Merilainen if (!client->syncpts[0]) {
1400ae797a8SArto Merilainen err = -ENOMEM;
1410ae797a8SArto Merilainen goto free_channel;
1420ae797a8SArto Merilainen }
1430ae797a8SArto Merilainen
1440ae797a8SArto Merilainen err = tegra_drm_register_client(tegra, drm);
1450ae797a8SArto Merilainen if (err < 0)
146*62fa0a98SMikko Perttunen goto free_syncpt;
1470ae797a8SArto Merilainen
14847b15779SThierry Reding /*
14947b15779SThierry Reding * Inherit the DMA parameters (such as maximum segment size) from the
150608f43adSThierry Reding * parent host1x device.
15147b15779SThierry Reding */
152608f43adSThierry Reding client->dev->dma_parms = client->host->dma_parms;
15347b15779SThierry Reding
1540ae797a8SArto Merilainen return 0;
1550ae797a8SArto Merilainen
156*62fa0a98SMikko Perttunen free_syncpt:
1572aed4f5aSMikko Perttunen host1x_syncpt_put(client->syncpts[0]);
1580ae797a8SArto Merilainen free_channel:
1598474b025SMikko Perttunen host1x_channel_put(vic->channel);
160bc8828bdSThierry Reding detach:
161aacdf198SThierry Reding host1x_client_iommu_detach(client);
1620ae797a8SArto Merilainen
1630ae797a8SArto Merilainen return err;
1640ae797a8SArto Merilainen }
1650ae797a8SArto Merilainen
vic_exit(struct host1x_client * client)1660ae797a8SArto Merilainen static int vic_exit(struct host1x_client *client)
1670ae797a8SArto Merilainen {
1680ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client);
169608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host);
1700ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private;
1710ae797a8SArto Merilainen struct vic *vic = to_vic(drm);
1720ae797a8SArto Merilainen int err;
1730ae797a8SArto Merilainen
17447b15779SThierry Reding /* avoid a dangling pointer just in case this disappears */
17547b15779SThierry Reding client->dev->dma_parms = NULL;
17647b15779SThierry Reding
1770ae797a8SArto Merilainen err = tegra_drm_unregister_client(tegra, drm);
1780ae797a8SArto Merilainen if (err < 0)
1790ae797a8SArto Merilainen return err;
1800ae797a8SArto Merilainen
1811e15f5b9SDmitry Osipenko pm_runtime_dont_use_autosuspend(client->dev);
1821e15f5b9SDmitry Osipenko pm_runtime_force_suspend(client->dev);
1831e15f5b9SDmitry Osipenko
1842aed4f5aSMikko Perttunen host1x_syncpt_put(client->syncpts[0]);
1858474b025SMikko Perttunen host1x_channel_put(vic->channel);
186aacdf198SThierry Reding host1x_client_iommu_detach(client);
1870ae797a8SArto Merilainen
1881e15f5b9SDmitry Osipenko vic->channel = NULL;
1891e15f5b9SDmitry Osipenko
190d972d624SThierry Reding if (client->group) {
191d972d624SThierry Reding dma_unmap_single(vic->dev, vic->falcon.firmware.phys,
192d972d624SThierry Reding vic->falcon.firmware.size, DMA_TO_DEVICE);
19320e7dce2SThierry Reding tegra_drm_free(tegra, vic->falcon.firmware.size,
194d972d624SThierry Reding vic->falcon.firmware.virt,
195d972d624SThierry Reding vic->falcon.firmware.iova);
196d972d624SThierry Reding } else {
19720e7dce2SThierry Reding dma_free_coherent(vic->dev, vic->falcon.firmware.size,
198d972d624SThierry Reding vic->falcon.firmware.virt,
199d972d624SThierry Reding vic->falcon.firmware.iova);
200d972d624SThierry Reding }
20120e7dce2SThierry Reding
2020ae797a8SArto Merilainen return 0;
2030ae797a8SArto Merilainen }
2040ae797a8SArto Merilainen
2050ae797a8SArto Merilainen static const struct host1x_client_ops vic_client_ops = {
2060ae797a8SArto Merilainen .init = vic_init,
2070ae797a8SArto Merilainen .exit = vic_exit,
2080ae797a8SArto Merilainen };
2090ae797a8SArto Merilainen
vic_load_firmware(struct vic * vic)21077a0b09dSThierry Reding static int vic_load_firmware(struct vic *vic)
21177a0b09dSThierry Reding {
21220e7dce2SThierry Reding struct host1x_client *client = &vic->client.base;
21320e7dce2SThierry Reding struct tegra_drm *tegra = vic->client.drm;
214bf0297acSMikko Perttunen static DEFINE_MUTEX(lock);
215bf0297acSMikko Perttunen u32 fce_bin_data_offset;
216d972d624SThierry Reding dma_addr_t iova;
21720e7dce2SThierry Reding size_t size;
21820e7dce2SThierry Reding void *virt;
21977a0b09dSThierry Reding int err;
22077a0b09dSThierry Reding
221bf0297acSMikko Perttunen mutex_lock(&lock);
222bf0297acSMikko Perttunen
223bf0297acSMikko Perttunen if (vic->falcon.firmware.virt) {
224bf0297acSMikko Perttunen err = 0;
225bf0297acSMikko Perttunen goto unlock;
226bf0297acSMikko Perttunen }
22777a0b09dSThierry Reding
22877a0b09dSThierry Reding err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
22977a0b09dSThierry Reding if (err < 0)
230bf0297acSMikko Perttunen goto unlock;
23120e7dce2SThierry Reding
23220e7dce2SThierry Reding size = vic->falcon.firmware.size;
23320e7dce2SThierry Reding
23420e7dce2SThierry Reding if (!client->group) {
235d972d624SThierry Reding virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL);
236bf0297acSMikko Perttunen if (!virt) {
237bf0297acSMikko Perttunen err = -ENOMEM;
238bf0297acSMikko Perttunen goto unlock;
239bf0297acSMikko Perttunen }
24020e7dce2SThierry Reding } else {
241d972d624SThierry Reding virt = tegra_drm_alloc(tegra, size, &iova);
242bf0297acSMikko Perttunen if (IS_ERR(virt)) {
243bf0297acSMikko Perttunen err = PTR_ERR(virt);
244bf0297acSMikko Perttunen goto unlock;
245bf0297acSMikko Perttunen }
24620e7dce2SThierry Reding }
24720e7dce2SThierry Reding
248d972d624SThierry Reding vic->falcon.firmware.virt = virt;
249d972d624SThierry Reding vic->falcon.firmware.iova = iova;
25077a0b09dSThierry Reding
25177a0b09dSThierry Reding err = falcon_load_firmware(&vic->falcon);
25277a0b09dSThierry Reding if (err < 0)
25377a0b09dSThierry Reding goto cleanup;
25477a0b09dSThierry Reding
25520e7dce2SThierry Reding /*
25620e7dce2SThierry Reding * In this case we have received an IOVA from the shared domain, so we
25720e7dce2SThierry Reding * need to make sure to get the physical address so that the DMA API
25820e7dce2SThierry Reding * knows what memory pages to flush the cache for.
25920e7dce2SThierry Reding */
26020e7dce2SThierry Reding if (client->group) {
261d972d624SThierry Reding dma_addr_t phys;
262d972d624SThierry Reding
26320e7dce2SThierry Reding phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE);
26420e7dce2SThierry Reding
26520e7dce2SThierry Reding err = dma_mapping_error(vic->dev, phys);
26620e7dce2SThierry Reding if (err < 0)
26720e7dce2SThierry Reding goto cleanup;
26820e7dce2SThierry Reding
269d972d624SThierry Reding vic->falcon.firmware.phys = phys;
27020e7dce2SThierry Reding }
27120e7dce2SThierry Reding
272bf0297acSMikko Perttunen /*
273bf0297acSMikko Perttunen * Check if firmware is new enough to not require mapping firmware
274bf0297acSMikko Perttunen * to data buffer domains.
275bf0297acSMikko Perttunen */
276bf0297acSMikko Perttunen fce_bin_data_offset = *(u32 *)(virt + VIC_UCODE_FCE_DATA_OFFSET);
277bf0297acSMikko Perttunen
278bf0297acSMikko Perttunen if (!vic->config->supports_sid) {
279bf0297acSMikko Perttunen vic->can_use_context = false;
280bf0297acSMikko Perttunen } else if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
281bf0297acSMikko Perttunen /*
282bf0297acSMikko Perttunen * Firmware will access FCE through STREAMID0, so context
283bf0297acSMikko Perttunen * isolation cannot be used.
284bf0297acSMikko Perttunen */
285bf0297acSMikko Perttunen vic->can_use_context = false;
286bf0297acSMikko Perttunen dev_warn_once(vic->dev, "context isolation disabled due to old firmware\n");
287bf0297acSMikko Perttunen } else {
288bf0297acSMikko Perttunen vic->can_use_context = true;
289bf0297acSMikko Perttunen }
290bf0297acSMikko Perttunen
291bf0297acSMikko Perttunen unlock:
292bf0297acSMikko Perttunen mutex_unlock(&lock);
293bf0297acSMikko Perttunen return err;
29477a0b09dSThierry Reding
29577a0b09dSThierry Reding cleanup:
29620e7dce2SThierry Reding if (!client->group)
297d972d624SThierry Reding dma_free_coherent(vic->dev, size, virt, iova);
29820e7dce2SThierry Reding else
299d972d624SThierry Reding tegra_drm_free(tegra, size, virt, iova);
30020e7dce2SThierry Reding
301bf0297acSMikko Perttunen mutex_unlock(&lock);
30277a0b09dSThierry Reding return err;
30377a0b09dSThierry Reding }
30477a0b09dSThierry Reding
30599166123SMikko Perttunen
vic_runtime_resume(struct device * dev)306b5d5288aSYueHaibing static int __maybe_unused vic_runtime_resume(struct device *dev)
30799166123SMikko Perttunen {
30899166123SMikko Perttunen struct vic *vic = dev_get_drvdata(dev);
30999166123SMikko Perttunen int err;
31099166123SMikko Perttunen
31199166123SMikko Perttunen err = clk_prepare_enable(vic->clk);
31299166123SMikko Perttunen if (err < 0)
31399166123SMikko Perttunen return err;
31499166123SMikko Perttunen
31599166123SMikko Perttunen usleep_range(10, 20);
31699166123SMikko Perttunen
31799166123SMikko Perttunen err = reset_control_deassert(vic->rst);
31899166123SMikko Perttunen if (err < 0)
31999166123SMikko Perttunen goto disable;
32099166123SMikko Perttunen
32199166123SMikko Perttunen usleep_range(10, 20);
32299166123SMikko Perttunen
32399166123SMikko Perttunen err = vic_load_firmware(vic);
32499166123SMikko Perttunen if (err < 0)
32599166123SMikko Perttunen goto assert;
32699166123SMikko Perttunen
32799166123SMikko Perttunen err = vic_boot(vic);
32899166123SMikko Perttunen if (err < 0)
32999166123SMikko Perttunen goto assert;
33099166123SMikko Perttunen
33199166123SMikko Perttunen return 0;
33299166123SMikko Perttunen
33399166123SMikko Perttunen assert:
33499166123SMikko Perttunen reset_control_assert(vic->rst);
33599166123SMikko Perttunen disable:
33699166123SMikko Perttunen clk_disable_unprepare(vic->clk);
33799166123SMikko Perttunen return err;
33899166123SMikko Perttunen }
33999166123SMikko Perttunen
vic_runtime_suspend(struct device * dev)340b5d5288aSYueHaibing static int __maybe_unused vic_runtime_suspend(struct device *dev)
34199166123SMikko Perttunen {
34299166123SMikko Perttunen struct vic *vic = dev_get_drvdata(dev);
34399166123SMikko Perttunen int err;
34499166123SMikko Perttunen
3451e15f5b9SDmitry Osipenko host1x_channel_stop(vic->channel);
3461e15f5b9SDmitry Osipenko
34799166123SMikko Perttunen err = reset_control_assert(vic->rst);
34899166123SMikko Perttunen if (err < 0)
34999166123SMikko Perttunen return err;
35099166123SMikko Perttunen
35199166123SMikko Perttunen usleep_range(2000, 4000);
35299166123SMikko Perttunen
35399166123SMikko Perttunen clk_disable_unprepare(vic->clk);
35499166123SMikko Perttunen
35599166123SMikko Perttunen return 0;
35699166123SMikko Perttunen }
35799166123SMikko Perttunen
vic_open_channel(struct tegra_drm_client * client,struct tegra_drm_context * context)3580ae797a8SArto Merilainen static int vic_open_channel(struct tegra_drm_client *client,
3590ae797a8SArto Merilainen struct tegra_drm_context *context)
3600ae797a8SArto Merilainen {
3610ae797a8SArto Merilainen struct vic *vic = to_vic(client);
3620ae797a8SArto Merilainen
3630ae797a8SArto Merilainen context->channel = host1x_channel_get(vic->channel);
36458ed47adSDmitry Osipenko if (!context->channel)
36599166123SMikko Perttunen return -ENOMEM;
3660ae797a8SArto Merilainen
3670ae797a8SArto Merilainen return 0;
3680ae797a8SArto Merilainen }
3690ae797a8SArto Merilainen
vic_close_channel(struct tegra_drm_context * context)3700ae797a8SArto Merilainen static void vic_close_channel(struct tegra_drm_context *context)
3710ae797a8SArto Merilainen {
3720ae797a8SArto Merilainen host1x_channel_put(context->channel);
3730ae797a8SArto Merilainen }
3740ae797a8SArto Merilainen
vic_can_use_memory_ctx(struct tegra_drm_client * client,bool * supported)375bf0297acSMikko Perttunen static int vic_can_use_memory_ctx(struct tegra_drm_client *client, bool *supported)
376bf0297acSMikko Perttunen {
377bf0297acSMikko Perttunen struct vic *vic = to_vic(client);
378bf0297acSMikko Perttunen int err;
379bf0297acSMikko Perttunen
380bf0297acSMikko Perttunen /* This doesn't access HW so it's safe to call without powering up. */
381bf0297acSMikko Perttunen err = vic_load_firmware(vic);
382bf0297acSMikko Perttunen if (err < 0)
383bf0297acSMikko Perttunen return err;
384bf0297acSMikko Perttunen
385bf0297acSMikko Perttunen *supported = vic->can_use_context;
386bf0297acSMikko Perttunen
387bf0297acSMikko Perttunen return 0;
388bf0297acSMikko Perttunen }
389bf0297acSMikko Perttunen
3900ae797a8SArto Merilainen static const struct tegra_drm_client_ops vic_ops = {
3910ae797a8SArto Merilainen .open_channel = vic_open_channel,
3920ae797a8SArto Merilainen .close_channel = vic_close_channel,
3930ae797a8SArto Merilainen .submit = tegra_drm_submit,
394bf0297acSMikko Perttunen .get_streamid_offset = tegra_drm_get_streamid_offset_thi,
395bf0297acSMikko Perttunen .can_use_memory_ctx = vic_can_use_memory_ctx,
3960ae797a8SArto Merilainen };
3970ae797a8SArto Merilainen
398788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
399788ff4b6SNicolas Chauvet
4000ae797a8SArto Merilainen static const struct vic_config vic_t124_config = {
401788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
402acae8a9dSThierry Reding .version = 0x40,
403f3779cb1SThierry Reding .supports_sid = false,
4040ae797a8SArto Merilainen };
4050ae797a8SArto Merilainen
406788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
407788ff4b6SNicolas Chauvet
4080ae797a8SArto Merilainen static const struct vic_config vic_t210_config = {
409788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
410acae8a9dSThierry Reding .version = 0x21,
411f3779cb1SThierry Reding .supports_sid = false,
4120ae797a8SArto Merilainen };
4130ae797a8SArto Merilainen
4146e44b9adSMikko Perttunen #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
4156e44b9adSMikko Perttunen
4166e44b9adSMikko Perttunen static const struct vic_config vic_t186_config = {
4176e44b9adSMikko Perttunen .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
418acae8a9dSThierry Reding .version = 0x18,
419f3779cb1SThierry Reding .supports_sid = true,
4206e44b9adSMikko Perttunen };
4216e44b9adSMikko Perttunen
422d6b9bc02SThierry Reding #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
423d6b9bc02SThierry Reding
424d6b9bc02SThierry Reding static const struct vic_config vic_t194_config = {
425d6b9bc02SThierry Reding .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
426d6b9bc02SThierry Reding .version = 0x19,
427f3779cb1SThierry Reding .supports_sid = true,
428d6b9bc02SThierry Reding };
429d6b9bc02SThierry Reding
4309550669cSMikko Perttunen #define NVIDIA_TEGRA_234_VIC_FIRMWARE "nvidia/tegra234/vic.bin"
4319550669cSMikko Perttunen
4329550669cSMikko Perttunen static const struct vic_config vic_t234_config = {
4339550669cSMikko Perttunen .firmware = NVIDIA_TEGRA_234_VIC_FIRMWARE,
4349550669cSMikko Perttunen .version = 0x23,
4359550669cSMikko Perttunen .supports_sid = true,
4369550669cSMikko Perttunen };
4379550669cSMikko Perttunen
43882d73874SThierry Reding static const struct of_device_id tegra_vic_of_match[] = {
4390ae797a8SArto Merilainen { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
4400ae797a8SArto Merilainen { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
4416e44b9adSMikko Perttunen { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
442d6b9bc02SThierry Reding { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
4439550669cSMikko Perttunen { .compatible = "nvidia,tegra234-vic", .data = &vic_t234_config },
4440ae797a8SArto Merilainen { },
4450ae797a8SArto Merilainen };
44682d73874SThierry Reding MODULE_DEVICE_TABLE(of, tegra_vic_of_match);
4470ae797a8SArto Merilainen
vic_probe(struct platform_device * pdev)4480ae797a8SArto Merilainen static int vic_probe(struct platform_device *pdev)
4490ae797a8SArto Merilainen {
4500ae797a8SArto Merilainen struct device *dev = &pdev->dev;
4510ae797a8SArto Merilainen struct host1x_syncpt **syncpts;
4520ae797a8SArto Merilainen struct vic *vic;
4530ae797a8SArto Merilainen int err;
4540ae797a8SArto Merilainen
455d5ad0e3dSThierry Reding /* inherit DMA mask from host1x parent */
456d5ad0e3dSThierry Reding err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
457d5ad0e3dSThierry Reding if (err < 0) {
458d5ad0e3dSThierry Reding dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
459d5ad0e3dSThierry Reding return err;
460d5ad0e3dSThierry Reding }
461d5ad0e3dSThierry Reding
4620ae797a8SArto Merilainen vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
4630ae797a8SArto Merilainen if (!vic)
4640ae797a8SArto Merilainen return -ENOMEM;
4650ae797a8SArto Merilainen
466829ce7a6SThierry Reding vic->config = of_device_get_match_data(dev);
467829ce7a6SThierry Reding
4680ae797a8SArto Merilainen syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
4690ae797a8SArto Merilainen if (!syncpts)
4700ae797a8SArto Merilainen return -ENOMEM;
4710ae797a8SArto Merilainen
472135f4c55SLv Ruyi vic->regs = devm_platform_ioremap_resource(pdev, 0);
4730ae797a8SArto Merilainen if (IS_ERR(vic->regs))
4740ae797a8SArto Merilainen return PTR_ERR(vic->regs);
4750ae797a8SArto Merilainen
4760ae797a8SArto Merilainen vic->clk = devm_clk_get(dev, NULL);
4770ae797a8SArto Merilainen if (IS_ERR(vic->clk)) {
4780ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to get clock\n");
4790ae797a8SArto Merilainen return PTR_ERR(vic->clk);
4800ae797a8SArto Merilainen }
4810ae797a8SArto Merilainen
482e97a951fSMikko Perttunen err = clk_set_rate(vic->clk, ULONG_MAX);
483e97a951fSMikko Perttunen if (err < 0) {
484e97a951fSMikko Perttunen dev_err(&pdev->dev, "failed to set clock rate\n");
485e97a951fSMikko Perttunen return err;
486e97a951fSMikko Perttunen }
487e97a951fSMikko Perttunen
4880dc34e19SThierry Reding if (!dev->pm_domain) {
4890dc34e19SThierry Reding vic->rst = devm_reset_control_get(dev, "vic");
4900dc34e19SThierry Reding if (IS_ERR(vic->rst)) {
4910dc34e19SThierry Reding dev_err(&pdev->dev, "failed to get reset\n");
4920dc34e19SThierry Reding return PTR_ERR(vic->rst);
4930dc34e19SThierry Reding }
4940dc34e19SThierry Reding }
4950dc34e19SThierry Reding
4960ae797a8SArto Merilainen vic->falcon.dev = dev;
4970ae797a8SArto Merilainen vic->falcon.regs = vic->regs;
4980ae797a8SArto Merilainen
4990ae797a8SArto Merilainen err = falcon_init(&vic->falcon);
5000ae797a8SArto Merilainen if (err < 0)
5010ae797a8SArto Merilainen return err;
5020ae797a8SArto Merilainen
5030ae797a8SArto Merilainen platform_set_drvdata(pdev, vic);
5040ae797a8SArto Merilainen
5050ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.base.list);
5060ae797a8SArto Merilainen vic->client.base.ops = &vic_client_ops;
5070ae797a8SArto Merilainen vic->client.base.dev = dev;
5080ae797a8SArto Merilainen vic->client.base.class = HOST1X_CLASS_VIC;
5090ae797a8SArto Merilainen vic->client.base.syncpts = syncpts;
5100ae797a8SArto Merilainen vic->client.base.num_syncpts = 1;
5110ae797a8SArto Merilainen vic->dev = dev;
5120ae797a8SArto Merilainen
5130ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.list);
514acae8a9dSThierry Reding vic->client.version = vic->config->version;
5150ae797a8SArto Merilainen vic->client.ops = &vic_ops;
5160ae797a8SArto Merilainen
5170ae797a8SArto Merilainen err = host1x_client_register(&vic->client.base);
5180ae797a8SArto Merilainen if (err < 0) {
5190ae797a8SArto Merilainen dev_err(dev, "failed to register host1x client: %d\n", err);
5200ae797a8SArto Merilainen goto exit_falcon;
5210ae797a8SArto Merilainen }
5220ae797a8SArto Merilainen
523*62fa0a98SMikko Perttunen pm_runtime_enable(dev);
524*62fa0a98SMikko Perttunen pm_runtime_use_autosuspend(dev);
525*62fa0a98SMikko Perttunen pm_runtime_set_autosuspend_delay(dev, 500);
526*62fa0a98SMikko Perttunen
5270ae797a8SArto Merilainen return 0;
5280ae797a8SArto Merilainen
5290ae797a8SArto Merilainen exit_falcon:
5300ae797a8SArto Merilainen falcon_exit(&vic->falcon);
5310ae797a8SArto Merilainen
5320ae797a8SArto Merilainen return err;
5330ae797a8SArto Merilainen }
5340ae797a8SArto Merilainen
vic_remove(struct platform_device * pdev)5359eb75fbfSUwe Kleine-König static void vic_remove(struct platform_device *pdev)
5360ae797a8SArto Merilainen {
5370ae797a8SArto Merilainen struct vic *vic = platform_get_drvdata(pdev);
5380ae797a8SArto Merilainen
539*62fa0a98SMikko Perttunen pm_runtime_disable(&pdev->dev);
5401d83d1a2SUwe Kleine-König host1x_client_unregister(&vic->client.base);
5410ae797a8SArto Merilainen falcon_exit(&vic->falcon);
5420ae797a8SArto Merilainen }
5430ae797a8SArto Merilainen
5440ae797a8SArto Merilainen static const struct dev_pm_ops vic_pm_ops = {
54542457494SArnd Bergmann RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
54642457494SArnd Bergmann SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
5470ae797a8SArto Merilainen };
5480ae797a8SArto Merilainen
5490ae797a8SArto Merilainen struct platform_driver tegra_vic_driver = {
5500ae797a8SArto Merilainen .driver = {
5510ae797a8SArto Merilainen .name = "tegra-vic",
55282d73874SThierry Reding .of_match_table = tegra_vic_of_match,
5530ae797a8SArto Merilainen .pm = &vic_pm_ops
5540ae797a8SArto Merilainen },
5550ae797a8SArto Merilainen .probe = vic_probe,
5569eb75fbfSUwe Kleine-König .remove_new = vic_remove,
5570ae797a8SArto Merilainen };
558788ff4b6SNicolas Chauvet
559788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
560788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
561788ff4b6SNicolas Chauvet #endif
562788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
563788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
564788ff4b6SNicolas Chauvet #endif
5656e44b9adSMikko Perttunen #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
5666e44b9adSMikko Perttunen MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
5676e44b9adSMikko Perttunen #endif
568d6b9bc02SThierry Reding #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
569d6b9bc02SThierry Reding MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);
570d6b9bc02SThierry Reding #endif
5719550669cSMikko Perttunen #if IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
5729550669cSMikko Perttunen MODULE_FIRMWARE(NVIDIA_TEGRA_234_VIC_FIRMWARE);
5739550669cSMikko Perttunen #endif
574