xref: /linux/drivers/gpu/drm/tegra/vic.c (revision 58ed47adcabb4ce13a60f56230074805957822fa)
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/of_device.h>
140ae797a8SArto Merilainen #include <linux/of_platform.h>
150ae797a8SArto Merilainen #include <linux/platform_device.h>
160ae797a8SArto Merilainen #include <linux/pm_runtime.h>
170ae797a8SArto Merilainen #include <linux/reset.h>
180ae797a8SArto Merilainen 
190ae797a8SArto Merilainen #include <soc/tegra/pmc.h>
200ae797a8SArto Merilainen 
210ae797a8SArto Merilainen #include "drm.h"
220ae797a8SArto Merilainen #include "falcon.h"
230ae797a8SArto Merilainen #include "vic.h"
240ae797a8SArto Merilainen 
250ae797a8SArto Merilainen struct vic_config {
260ae797a8SArto Merilainen 	const char *firmware;
27acae8a9dSThierry Reding 	unsigned int version;
28f3779cb1SThierry Reding 	bool supports_sid;
290ae797a8SArto Merilainen };
300ae797a8SArto Merilainen 
310ae797a8SArto Merilainen struct vic {
320ae797a8SArto Merilainen 	struct falcon falcon;
330ae797a8SArto Merilainen 
340ae797a8SArto Merilainen 	void __iomem *regs;
350ae797a8SArto Merilainen 	struct tegra_drm_client client;
360ae797a8SArto Merilainen 	struct host1x_channel *channel;
370ae797a8SArto Merilainen 	struct device *dev;
380ae797a8SArto Merilainen 	struct clk *clk;
390dc34e19SThierry Reding 	struct reset_control *rst;
400ae797a8SArto Merilainen 
410ae797a8SArto Merilainen 	/* Platform configuration */
420ae797a8SArto Merilainen 	const struct vic_config *config;
430ae797a8SArto Merilainen };
440ae797a8SArto Merilainen 
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 
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 
550ae797a8SArto Merilainen static int vic_boot(struct vic *vic)
560ae797a8SArto Merilainen {
57dd631e8aSThierry Reding #ifdef CONFIG_IOMMU_API
58dd631e8aSThierry Reding 	struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev);
59dd631e8aSThierry Reding #endif
600ae797a8SArto Merilainen 	u32 fce_ucode_size, fce_bin_data_offset;
610ae797a8SArto Merilainen 	void *hdr;
620ae797a8SArto Merilainen 	int err = 0;
630ae797a8SArto Merilainen 
64509869a2SAnders Roxell #ifdef CONFIG_IOMMU_API
65dd631e8aSThierry Reding 	if (vic->config->supports_sid && spec) {
66f3779cb1SThierry Reding 		u32 value;
67f3779cb1SThierry Reding 
68f3779cb1SThierry Reding 		value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) |
69f3779cb1SThierry Reding 			TRANSCFG_ATT(0, TRANSCFG_SID_HW);
70f3779cb1SThierry Reding 		vic_writel(vic, value, VIC_TFBIF_TRANSCFG);
71f3779cb1SThierry Reding 
72dd631e8aSThierry Reding 		if (spec->num_ids > 0) {
73f3779cb1SThierry Reding 			value = spec->ids[0] & 0xffff;
74f3779cb1SThierry Reding 
7559e520a6SMikko Perttunen 			/*
7659e520a6SMikko Perttunen 			 * STREAMID0 is used for input/output buffers.
7759e520a6SMikko Perttunen 			 * Initialize it to SID_VIC in case context isolation
7859e520a6SMikko Perttunen 			 * is not enabled, and SID_VIC is used for both firmware
7959e520a6SMikko Perttunen 			 * and data buffers.
8059e520a6SMikko Perttunen 			 *
8159e520a6SMikko Perttunen 			 * If context isolation is enabled, it will be
8259e520a6SMikko Perttunen 			 * overridden by the SETSTREAMID opcode as part of
8359e520a6SMikko Perttunen 			 * each job.
8459e520a6SMikko Perttunen 			 */
85f3779cb1SThierry Reding 			vic_writel(vic, value, VIC_THI_STREAMID0);
8659e520a6SMikko Perttunen 
8759e520a6SMikko Perttunen 			/* STREAMID1 is used for firmware loading. */
88f3779cb1SThierry Reding 			vic_writel(vic, value, VIC_THI_STREAMID1);
89f3779cb1SThierry Reding 		}
90f3779cb1SThierry Reding 	}
91509869a2SAnders Roxell #endif
92f3779cb1SThierry Reding 
930ae797a8SArto Merilainen 	/* setup clockgating registers */
940ae797a8SArto Merilainen 	vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
950ae797a8SArto Merilainen 			CG_IDLE_CG_EN |
960ae797a8SArto Merilainen 			CG_WAKEUP_DLY_CNT(4),
970ae797a8SArto Merilainen 		   NV_PVIC_MISC_PRI_VIC_CG);
980ae797a8SArto Merilainen 
990ae797a8SArto Merilainen 	err = falcon_boot(&vic->falcon);
1000ae797a8SArto Merilainen 	if (err < 0)
1010ae797a8SArto Merilainen 		return err;
1020ae797a8SArto Merilainen 
103d972d624SThierry Reding 	hdr = vic->falcon.firmware.virt;
1040ae797a8SArto Merilainen 	fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
10558ef3aebSMikko Perttunen 
10658ef3aebSMikko Perttunen 	/* Old VIC firmware needs kernel help with setting up FCE microcode. */
10758ef3aebSMikko Perttunen 	if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) {
108d972d624SThierry Reding 		hdr = vic->falcon.firmware.virt +
1090ae797a8SArto Merilainen 			*(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
1100ae797a8SArto Merilainen 		fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
1110ae797a8SArto Merilainen 
1120ae797a8SArto Merilainen 		falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
1130ae797a8SArto Merilainen 				      fce_ucode_size);
11458ef3aebSMikko Perttunen 		falcon_execute_method(
11558ef3aebSMikko Perttunen 			&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
11658ef3aebSMikko Perttunen 			(vic->falcon.firmware.iova + fce_bin_data_offset) >> 8);
11758ef3aebSMikko Perttunen 	}
1180ae797a8SArto Merilainen 
1190ae797a8SArto Merilainen 	err = falcon_wait_idle(&vic->falcon);
1200ae797a8SArto Merilainen 	if (err < 0) {
1210ae797a8SArto Merilainen 		dev_err(vic->dev,
1220ae797a8SArto Merilainen 			"failed to set application ID and FCE base\n");
1230ae797a8SArto Merilainen 		return err;
1240ae797a8SArto Merilainen 	}
1250ae797a8SArto Merilainen 
1260ae797a8SArto Merilainen 	return 0;
1270ae797a8SArto Merilainen }
1280ae797a8SArto Merilainen 
1290ae797a8SArto Merilainen static int vic_init(struct host1x_client *client)
1300ae797a8SArto Merilainen {
1310ae797a8SArto Merilainen 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
132608f43adSThierry Reding 	struct drm_device *dev = dev_get_drvdata(client->host);
1330ae797a8SArto Merilainen 	struct tegra_drm *tegra = dev->dev_private;
1340ae797a8SArto Merilainen 	struct vic *vic = to_vic(drm);
1350ae797a8SArto Merilainen 	int err;
1360ae797a8SArto Merilainen 
1377edd7961SThierry Reding 	err = host1x_client_iommu_attach(client);
138a8817489SThierry Reding 	if (err < 0 && err != -ENODEV) {
1397baa943eSThierry Reding 		dev_err(vic->dev, "failed to attach to domain: %d\n", err);
1400ae797a8SArto Merilainen 		return err;
1410ae797a8SArto Merilainen 	}
1420ae797a8SArto Merilainen 
143caccddcfSThierry Reding 	vic->channel = host1x_channel_request(client);
1440ae797a8SArto Merilainen 	if (!vic->channel) {
1450ae797a8SArto Merilainen 		err = -ENOMEM;
146bc8828bdSThierry Reding 		goto detach;
1470ae797a8SArto Merilainen 	}
1480ae797a8SArto Merilainen 
149617dd7ccSThierry Reding 	client->syncpts[0] = host1x_syncpt_request(client, 0);
1500ae797a8SArto Merilainen 	if (!client->syncpts[0]) {
1510ae797a8SArto Merilainen 		err = -ENOMEM;
1520ae797a8SArto Merilainen 		goto free_channel;
1530ae797a8SArto Merilainen 	}
1540ae797a8SArto Merilainen 
1551e15f5b9SDmitry Osipenko 	pm_runtime_enable(client->dev);
1561e15f5b9SDmitry Osipenko 	pm_runtime_use_autosuspend(client->dev);
1571e15f5b9SDmitry Osipenko 	pm_runtime_set_autosuspend_delay(client->dev, 500);
1581e15f5b9SDmitry Osipenko 
1590ae797a8SArto Merilainen 	err = tegra_drm_register_client(tegra, drm);
1600ae797a8SArto Merilainen 	if (err < 0)
1611e15f5b9SDmitry Osipenko 		goto disable_rpm;
1620ae797a8SArto Merilainen 
16347b15779SThierry Reding 	/*
16447b15779SThierry Reding 	 * Inherit the DMA parameters (such as maximum segment size) from the
165608f43adSThierry Reding 	 * parent host1x device.
16647b15779SThierry Reding 	 */
167608f43adSThierry Reding 	client->dev->dma_parms = client->host->dma_parms;
16847b15779SThierry Reding 
1690ae797a8SArto Merilainen 	return 0;
1700ae797a8SArto Merilainen 
1711e15f5b9SDmitry Osipenko disable_rpm:
1721e15f5b9SDmitry Osipenko 	pm_runtime_dont_use_autosuspend(client->dev);
1731e15f5b9SDmitry Osipenko 	pm_runtime_force_suspend(client->dev);
1741e15f5b9SDmitry Osipenko 
1752aed4f5aSMikko Perttunen 	host1x_syncpt_put(client->syncpts[0]);
1760ae797a8SArto Merilainen free_channel:
1778474b025SMikko Perttunen 	host1x_channel_put(vic->channel);
178bc8828bdSThierry Reding detach:
179aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
1800ae797a8SArto Merilainen 
1810ae797a8SArto Merilainen 	return err;
1820ae797a8SArto Merilainen }
1830ae797a8SArto Merilainen 
1840ae797a8SArto Merilainen static int vic_exit(struct host1x_client *client)
1850ae797a8SArto Merilainen {
1860ae797a8SArto Merilainen 	struct tegra_drm_client *drm = host1x_to_drm_client(client);
187608f43adSThierry Reding 	struct drm_device *dev = dev_get_drvdata(client->host);
1880ae797a8SArto Merilainen 	struct tegra_drm *tegra = dev->dev_private;
1890ae797a8SArto Merilainen 	struct vic *vic = to_vic(drm);
1900ae797a8SArto Merilainen 	int err;
1910ae797a8SArto Merilainen 
19247b15779SThierry Reding 	/* avoid a dangling pointer just in case this disappears */
19347b15779SThierry Reding 	client->dev->dma_parms = NULL;
19447b15779SThierry Reding 
1950ae797a8SArto Merilainen 	err = tegra_drm_unregister_client(tegra, drm);
1960ae797a8SArto Merilainen 	if (err < 0)
1970ae797a8SArto Merilainen 		return err;
1980ae797a8SArto Merilainen 
1991e15f5b9SDmitry Osipenko 	pm_runtime_dont_use_autosuspend(client->dev);
2001e15f5b9SDmitry Osipenko 	pm_runtime_force_suspend(client->dev);
2011e15f5b9SDmitry Osipenko 
2022aed4f5aSMikko Perttunen 	host1x_syncpt_put(client->syncpts[0]);
2038474b025SMikko Perttunen 	host1x_channel_put(vic->channel);
204aacdf198SThierry Reding 	host1x_client_iommu_detach(client);
2050ae797a8SArto Merilainen 
2061e15f5b9SDmitry Osipenko 	vic->channel = NULL;
2071e15f5b9SDmitry Osipenko 
208d972d624SThierry Reding 	if (client->group) {
209d972d624SThierry Reding 		dma_unmap_single(vic->dev, vic->falcon.firmware.phys,
210d972d624SThierry Reding 				 vic->falcon.firmware.size, DMA_TO_DEVICE);
21120e7dce2SThierry Reding 		tegra_drm_free(tegra, vic->falcon.firmware.size,
212d972d624SThierry Reding 			       vic->falcon.firmware.virt,
213d972d624SThierry Reding 			       vic->falcon.firmware.iova);
214d972d624SThierry Reding 	} else {
21520e7dce2SThierry Reding 		dma_free_coherent(vic->dev, vic->falcon.firmware.size,
216d972d624SThierry Reding 				  vic->falcon.firmware.virt,
217d972d624SThierry Reding 				  vic->falcon.firmware.iova);
218d972d624SThierry Reding 	}
21920e7dce2SThierry Reding 
2200ae797a8SArto Merilainen 	return 0;
2210ae797a8SArto Merilainen }
2220ae797a8SArto Merilainen 
2230ae797a8SArto Merilainen static const struct host1x_client_ops vic_client_ops = {
2240ae797a8SArto Merilainen 	.init = vic_init,
2250ae797a8SArto Merilainen 	.exit = vic_exit,
2260ae797a8SArto Merilainen };
2270ae797a8SArto Merilainen 
22877a0b09dSThierry Reding static int vic_load_firmware(struct vic *vic)
22977a0b09dSThierry Reding {
23020e7dce2SThierry Reding 	struct host1x_client *client = &vic->client.base;
23120e7dce2SThierry Reding 	struct tegra_drm *tegra = vic->client.drm;
232d972d624SThierry Reding 	dma_addr_t iova;
23320e7dce2SThierry Reding 	size_t size;
23420e7dce2SThierry Reding 	void *virt;
23577a0b09dSThierry Reding 	int err;
23677a0b09dSThierry Reding 
237d972d624SThierry Reding 	if (vic->falcon.firmware.virt)
23877a0b09dSThierry Reding 		return 0;
23977a0b09dSThierry Reding 
24077a0b09dSThierry Reding 	err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
24177a0b09dSThierry Reding 	if (err < 0)
24220e7dce2SThierry Reding 		return err;
24320e7dce2SThierry Reding 
24420e7dce2SThierry Reding 	size = vic->falcon.firmware.size;
24520e7dce2SThierry Reding 
24620e7dce2SThierry Reding 	if (!client->group) {
247d972d624SThierry Reding 		virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL);
2485566174cSRobin Murphy 		if (!virt)
2495566174cSRobin Murphy 			return -ENOMEM;
25020e7dce2SThierry Reding 	} else {
251d972d624SThierry Reding 		virt = tegra_drm_alloc(tegra, size, &iova);
252d53830eeSThierry Reding 		if (IS_ERR(virt))
253d53830eeSThierry Reding 			return PTR_ERR(virt);
25420e7dce2SThierry Reding 	}
25520e7dce2SThierry Reding 
256d972d624SThierry Reding 	vic->falcon.firmware.virt = virt;
257d972d624SThierry Reding 	vic->falcon.firmware.iova = iova;
25877a0b09dSThierry Reding 
25977a0b09dSThierry Reding 	err = falcon_load_firmware(&vic->falcon);
26077a0b09dSThierry Reding 	if (err < 0)
26177a0b09dSThierry Reding 		goto cleanup;
26277a0b09dSThierry Reding 
26320e7dce2SThierry Reding 	/*
26420e7dce2SThierry Reding 	 * In this case we have received an IOVA from the shared domain, so we
26520e7dce2SThierry Reding 	 * need to make sure to get the physical address so that the DMA API
26620e7dce2SThierry Reding 	 * knows what memory pages to flush the cache for.
26720e7dce2SThierry Reding 	 */
26820e7dce2SThierry Reding 	if (client->group) {
269d972d624SThierry Reding 		dma_addr_t phys;
270d972d624SThierry Reding 
27120e7dce2SThierry Reding 		phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE);
27220e7dce2SThierry Reding 
27320e7dce2SThierry Reding 		err = dma_mapping_error(vic->dev, phys);
27420e7dce2SThierry Reding 		if (err < 0)
27520e7dce2SThierry Reding 			goto cleanup;
27620e7dce2SThierry Reding 
277d972d624SThierry Reding 		vic->falcon.firmware.phys = phys;
27820e7dce2SThierry Reding 	}
27920e7dce2SThierry Reding 
28077a0b09dSThierry Reding 	return 0;
28177a0b09dSThierry Reding 
28277a0b09dSThierry Reding cleanup:
28320e7dce2SThierry Reding 	if (!client->group)
284d972d624SThierry Reding 		dma_free_coherent(vic->dev, size, virt, iova);
28520e7dce2SThierry Reding 	else
286d972d624SThierry Reding 		tegra_drm_free(tegra, size, virt, iova);
28720e7dce2SThierry Reding 
28877a0b09dSThierry Reding 	return err;
28977a0b09dSThierry Reding }
29077a0b09dSThierry Reding 
29199166123SMikko Perttunen 
29299166123SMikko Perttunen static int vic_runtime_resume(struct device *dev)
29399166123SMikko Perttunen {
29499166123SMikko Perttunen 	struct vic *vic = dev_get_drvdata(dev);
29599166123SMikko Perttunen 	int err;
29699166123SMikko Perttunen 
29799166123SMikko Perttunen 	err = clk_prepare_enable(vic->clk);
29899166123SMikko Perttunen 	if (err < 0)
29999166123SMikko Perttunen 		return err;
30099166123SMikko Perttunen 
30199166123SMikko Perttunen 	usleep_range(10, 20);
30299166123SMikko Perttunen 
30399166123SMikko Perttunen 	err = reset_control_deassert(vic->rst);
30499166123SMikko Perttunen 	if (err < 0)
30599166123SMikko Perttunen 		goto disable;
30699166123SMikko Perttunen 
30799166123SMikko Perttunen 	usleep_range(10, 20);
30899166123SMikko Perttunen 
30999166123SMikko Perttunen 	err = vic_load_firmware(vic);
31099166123SMikko Perttunen 	if (err < 0)
31199166123SMikko Perttunen 		goto assert;
31299166123SMikko Perttunen 
31399166123SMikko Perttunen 	err = vic_boot(vic);
31499166123SMikko Perttunen 	if (err < 0)
31599166123SMikko Perttunen 		goto assert;
31699166123SMikko Perttunen 
31799166123SMikko Perttunen 	return 0;
31899166123SMikko Perttunen 
31999166123SMikko Perttunen assert:
32099166123SMikko Perttunen 	reset_control_assert(vic->rst);
32199166123SMikko Perttunen disable:
32299166123SMikko Perttunen 	clk_disable_unprepare(vic->clk);
32399166123SMikko Perttunen 	return err;
32499166123SMikko Perttunen }
32599166123SMikko Perttunen 
32699166123SMikko Perttunen static int vic_runtime_suspend(struct device *dev)
32799166123SMikko Perttunen {
32899166123SMikko Perttunen 	struct vic *vic = dev_get_drvdata(dev);
32999166123SMikko Perttunen 	int err;
33099166123SMikko Perttunen 
3311e15f5b9SDmitry Osipenko 	host1x_channel_stop(vic->channel);
3321e15f5b9SDmitry Osipenko 
33399166123SMikko Perttunen 	err = reset_control_assert(vic->rst);
33499166123SMikko Perttunen 	if (err < 0)
33599166123SMikko Perttunen 		return err;
33699166123SMikko Perttunen 
33799166123SMikko Perttunen 	usleep_range(2000, 4000);
33899166123SMikko Perttunen 
33999166123SMikko Perttunen 	clk_disable_unprepare(vic->clk);
34099166123SMikko Perttunen 
34199166123SMikko Perttunen 	return 0;
34299166123SMikko Perttunen }
34399166123SMikko Perttunen 
3440ae797a8SArto Merilainen static int vic_open_channel(struct tegra_drm_client *client,
3450ae797a8SArto Merilainen 			    struct tegra_drm_context *context)
3460ae797a8SArto Merilainen {
3470ae797a8SArto Merilainen 	struct vic *vic = to_vic(client);
3480ae797a8SArto Merilainen 
3490ae797a8SArto Merilainen 	context->channel = host1x_channel_get(vic->channel);
350*58ed47adSDmitry Osipenko 	if (!context->channel)
35199166123SMikko Perttunen 		return -ENOMEM;
3520ae797a8SArto Merilainen 
3530ae797a8SArto Merilainen 	return 0;
3540ae797a8SArto Merilainen }
3550ae797a8SArto Merilainen 
3560ae797a8SArto Merilainen static void vic_close_channel(struct tegra_drm_context *context)
3570ae797a8SArto Merilainen {
3580ae797a8SArto Merilainen 	host1x_channel_put(context->channel);
3590ae797a8SArto Merilainen }
3600ae797a8SArto Merilainen 
3610ae797a8SArto Merilainen static const struct tegra_drm_client_ops vic_ops = {
3620ae797a8SArto Merilainen 	.open_channel = vic_open_channel,
3630ae797a8SArto Merilainen 	.close_channel = vic_close_channel,
3640ae797a8SArto Merilainen 	.submit = tegra_drm_submit,
3650ae797a8SArto Merilainen };
3660ae797a8SArto Merilainen 
367788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
368788ff4b6SNicolas Chauvet 
3690ae797a8SArto Merilainen static const struct vic_config vic_t124_config = {
370788ff4b6SNicolas Chauvet 	.firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
371acae8a9dSThierry Reding 	.version = 0x40,
372f3779cb1SThierry Reding 	.supports_sid = false,
3730ae797a8SArto Merilainen };
3740ae797a8SArto Merilainen 
375788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
376788ff4b6SNicolas Chauvet 
3770ae797a8SArto Merilainen static const struct vic_config vic_t210_config = {
378788ff4b6SNicolas Chauvet 	.firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
379acae8a9dSThierry Reding 	.version = 0x21,
380f3779cb1SThierry Reding 	.supports_sid = false,
3810ae797a8SArto Merilainen };
3820ae797a8SArto Merilainen 
3836e44b9adSMikko Perttunen #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
3846e44b9adSMikko Perttunen 
3856e44b9adSMikko Perttunen static const struct vic_config vic_t186_config = {
3866e44b9adSMikko Perttunen 	.firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
387acae8a9dSThierry Reding 	.version = 0x18,
388f3779cb1SThierry Reding 	.supports_sid = true,
3896e44b9adSMikko Perttunen };
3906e44b9adSMikko Perttunen 
391d6b9bc02SThierry Reding #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
392d6b9bc02SThierry Reding 
393d6b9bc02SThierry Reding static const struct vic_config vic_t194_config = {
394d6b9bc02SThierry Reding 	.firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
395d6b9bc02SThierry Reding 	.version = 0x19,
396f3779cb1SThierry Reding 	.supports_sid = true,
397d6b9bc02SThierry Reding };
398d6b9bc02SThierry Reding 
39982d73874SThierry Reding static const struct of_device_id tegra_vic_of_match[] = {
4000ae797a8SArto Merilainen 	{ .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
4010ae797a8SArto Merilainen 	{ .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
4026e44b9adSMikko Perttunen 	{ .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
403d6b9bc02SThierry Reding 	{ .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
4040ae797a8SArto Merilainen 	{ },
4050ae797a8SArto Merilainen };
40682d73874SThierry Reding MODULE_DEVICE_TABLE(of, tegra_vic_of_match);
4070ae797a8SArto Merilainen 
4080ae797a8SArto Merilainen static int vic_probe(struct platform_device *pdev)
4090ae797a8SArto Merilainen {
4100ae797a8SArto Merilainen 	struct device *dev = &pdev->dev;
4110ae797a8SArto Merilainen 	struct host1x_syncpt **syncpts;
4120ae797a8SArto Merilainen 	struct resource *regs;
4130ae797a8SArto Merilainen 	struct vic *vic;
4140ae797a8SArto Merilainen 	int err;
4150ae797a8SArto Merilainen 
416d5ad0e3dSThierry Reding 	/* inherit DMA mask from host1x parent */
417d5ad0e3dSThierry Reding 	err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask);
418d5ad0e3dSThierry Reding 	if (err < 0) {
419d5ad0e3dSThierry Reding 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
420d5ad0e3dSThierry Reding 		return err;
421d5ad0e3dSThierry Reding 	}
422d5ad0e3dSThierry Reding 
4230ae797a8SArto Merilainen 	vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
4240ae797a8SArto Merilainen 	if (!vic)
4250ae797a8SArto Merilainen 		return -ENOMEM;
4260ae797a8SArto Merilainen 
427829ce7a6SThierry Reding 	vic->config = of_device_get_match_data(dev);
428829ce7a6SThierry Reding 
4290ae797a8SArto Merilainen 	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
4300ae797a8SArto Merilainen 	if (!syncpts)
4310ae797a8SArto Merilainen 		return -ENOMEM;
4320ae797a8SArto Merilainen 
4330ae797a8SArto Merilainen 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4340ae797a8SArto Merilainen 	if (!regs) {
4350ae797a8SArto Merilainen 		dev_err(&pdev->dev, "failed to get registers\n");
4360ae797a8SArto Merilainen 		return -ENXIO;
4370ae797a8SArto Merilainen 	}
4380ae797a8SArto Merilainen 
4390ae797a8SArto Merilainen 	vic->regs = devm_ioremap_resource(dev, regs);
4400ae797a8SArto Merilainen 	if (IS_ERR(vic->regs))
4410ae797a8SArto Merilainen 		return PTR_ERR(vic->regs);
4420ae797a8SArto Merilainen 
4430ae797a8SArto Merilainen 	vic->clk = devm_clk_get(dev, NULL);
4440ae797a8SArto Merilainen 	if (IS_ERR(vic->clk)) {
4450ae797a8SArto Merilainen 		dev_err(&pdev->dev, "failed to get clock\n");
4460ae797a8SArto Merilainen 		return PTR_ERR(vic->clk);
4470ae797a8SArto Merilainen 	}
4480ae797a8SArto Merilainen 
449e97a951fSMikko Perttunen 	err = clk_set_rate(vic->clk, ULONG_MAX);
450e97a951fSMikko Perttunen 	if (err < 0) {
451e97a951fSMikko Perttunen 		dev_err(&pdev->dev, "failed to set clock rate\n");
452e97a951fSMikko Perttunen 		return err;
453e97a951fSMikko Perttunen 	}
454e97a951fSMikko Perttunen 
4550dc34e19SThierry Reding 	if (!dev->pm_domain) {
4560dc34e19SThierry Reding 		vic->rst = devm_reset_control_get(dev, "vic");
4570dc34e19SThierry Reding 		if (IS_ERR(vic->rst)) {
4580dc34e19SThierry Reding 			dev_err(&pdev->dev, "failed to get reset\n");
4590dc34e19SThierry Reding 			return PTR_ERR(vic->rst);
4600dc34e19SThierry Reding 		}
4610dc34e19SThierry Reding 	}
4620dc34e19SThierry Reding 
4630ae797a8SArto Merilainen 	vic->falcon.dev = dev;
4640ae797a8SArto Merilainen 	vic->falcon.regs = vic->regs;
4650ae797a8SArto Merilainen 
4660ae797a8SArto Merilainen 	err = falcon_init(&vic->falcon);
4670ae797a8SArto Merilainen 	if (err < 0)
4680ae797a8SArto Merilainen 		return err;
4690ae797a8SArto Merilainen 
4700ae797a8SArto Merilainen 	platform_set_drvdata(pdev, vic);
4710ae797a8SArto Merilainen 
4720ae797a8SArto Merilainen 	INIT_LIST_HEAD(&vic->client.base.list);
4730ae797a8SArto Merilainen 	vic->client.base.ops = &vic_client_ops;
4740ae797a8SArto Merilainen 	vic->client.base.dev = dev;
4750ae797a8SArto Merilainen 	vic->client.base.class = HOST1X_CLASS_VIC;
4760ae797a8SArto Merilainen 	vic->client.base.syncpts = syncpts;
4770ae797a8SArto Merilainen 	vic->client.base.num_syncpts = 1;
4780ae797a8SArto Merilainen 	vic->dev = dev;
4790ae797a8SArto Merilainen 
4800ae797a8SArto Merilainen 	INIT_LIST_HEAD(&vic->client.list);
481acae8a9dSThierry Reding 	vic->client.version = vic->config->version;
4820ae797a8SArto Merilainen 	vic->client.ops = &vic_ops;
4830ae797a8SArto Merilainen 
4840ae797a8SArto Merilainen 	err = host1x_client_register(&vic->client.base);
4850ae797a8SArto Merilainen 	if (err < 0) {
4860ae797a8SArto Merilainen 		dev_err(dev, "failed to register host1x client: %d\n", err);
4870ae797a8SArto Merilainen 		goto exit_falcon;
4880ae797a8SArto Merilainen 	}
4890ae797a8SArto Merilainen 
4900ae797a8SArto Merilainen 	return 0;
4910ae797a8SArto Merilainen 
4920ae797a8SArto Merilainen exit_falcon:
4930ae797a8SArto Merilainen 	falcon_exit(&vic->falcon);
4940ae797a8SArto Merilainen 
4950ae797a8SArto Merilainen 	return err;
4960ae797a8SArto Merilainen }
4970ae797a8SArto Merilainen 
4980ae797a8SArto Merilainen static int vic_remove(struct platform_device *pdev)
4990ae797a8SArto Merilainen {
5000ae797a8SArto Merilainen 	struct vic *vic = platform_get_drvdata(pdev);
5010ae797a8SArto Merilainen 	int err;
5020ae797a8SArto Merilainen 
5030ae797a8SArto Merilainen 	err = host1x_client_unregister(&vic->client.base);
5040ae797a8SArto Merilainen 	if (err < 0) {
5050ae797a8SArto Merilainen 		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
5060ae797a8SArto Merilainen 			err);
5070ae797a8SArto Merilainen 		return err;
5080ae797a8SArto Merilainen 	}
5090ae797a8SArto Merilainen 
5100ae797a8SArto Merilainen 	falcon_exit(&vic->falcon);
5110ae797a8SArto Merilainen 
5120ae797a8SArto Merilainen 	return 0;
5130ae797a8SArto Merilainen }
5140ae797a8SArto Merilainen 
5150ae797a8SArto Merilainen static const struct dev_pm_ops vic_pm_ops = {
5160ae797a8SArto Merilainen 	SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
5171e15f5b9SDmitry Osipenko 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
5181e15f5b9SDmitry Osipenko 				pm_runtime_force_resume)
5190ae797a8SArto Merilainen };
5200ae797a8SArto Merilainen 
5210ae797a8SArto Merilainen struct platform_driver tegra_vic_driver = {
5220ae797a8SArto Merilainen 	.driver = {
5230ae797a8SArto Merilainen 		.name = "tegra-vic",
52482d73874SThierry Reding 		.of_match_table = tegra_vic_of_match,
5250ae797a8SArto Merilainen 		.pm = &vic_pm_ops
5260ae797a8SArto Merilainen 	},
5270ae797a8SArto Merilainen 	.probe = vic_probe,
5280ae797a8SArto Merilainen 	.remove = vic_remove,
5290ae797a8SArto Merilainen };
530788ff4b6SNicolas Chauvet 
531788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
532788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
533788ff4b6SNicolas Chauvet #endif
534788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
535788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
536788ff4b6SNicolas Chauvet #endif
5376e44b9adSMikko Perttunen #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
5386e44b9adSMikko Perttunen MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
5396e44b9adSMikko Perttunen #endif
540d6b9bc02SThierry Reding #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
541d6b9bc02SThierry Reding MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);
542d6b9bc02SThierry Reding #endif
543