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 41*bf0297acSMikko Perttunen bool can_use_context; 42*bf0297acSMikko Perttunen 430ae797a8SArto Merilainen /* Platform configuration */ 440ae797a8SArto Merilainen const struct vic_config *config; 450ae797a8SArto Merilainen }; 460ae797a8SArto Merilainen 470ae797a8SArto Merilainen static inline struct vic *to_vic(struct tegra_drm_client *client) 480ae797a8SArto Merilainen { 490ae797a8SArto Merilainen return container_of(client, struct vic, client); 500ae797a8SArto Merilainen } 510ae797a8SArto Merilainen 520ae797a8SArto Merilainen static void vic_writel(struct vic *vic, u32 value, unsigned int offset) 530ae797a8SArto Merilainen { 540ae797a8SArto Merilainen writel(value, vic->regs + offset); 550ae797a8SArto Merilainen } 560ae797a8SArto Merilainen 570ae797a8SArto Merilainen static int vic_boot(struct vic *vic) 580ae797a8SArto Merilainen { 59dd631e8aSThierry Reding #ifdef CONFIG_IOMMU_API 60dd631e8aSThierry Reding struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev); 61dd631e8aSThierry Reding #endif 620ae797a8SArto Merilainen u32 fce_ucode_size, fce_bin_data_offset; 630ae797a8SArto Merilainen void *hdr; 640ae797a8SArto Merilainen int err = 0; 650ae797a8SArto Merilainen 66509869a2SAnders Roxell #ifdef CONFIG_IOMMU_API 67dd631e8aSThierry Reding if (vic->config->supports_sid && spec) { 68f3779cb1SThierry Reding u32 value; 69f3779cb1SThierry Reding 70f3779cb1SThierry Reding value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) | 71f3779cb1SThierry Reding TRANSCFG_ATT(0, TRANSCFG_SID_HW); 72f3779cb1SThierry Reding vic_writel(vic, value, VIC_TFBIF_TRANSCFG); 73f3779cb1SThierry Reding 74dd631e8aSThierry Reding if (spec->num_ids > 0) { 75f3779cb1SThierry Reding value = spec->ids[0] & 0xffff; 76f3779cb1SThierry Reding 7759e520a6SMikko Perttunen /* 7859e520a6SMikko Perttunen * STREAMID0 is used for input/output buffers. 7959e520a6SMikko Perttunen * Initialize it to SID_VIC in case context isolation 8059e520a6SMikko Perttunen * is not enabled, and SID_VIC is used for both firmware 8159e520a6SMikko Perttunen * and data buffers. 8259e520a6SMikko Perttunen * 8359e520a6SMikko Perttunen * If context isolation is enabled, it will be 8459e520a6SMikko Perttunen * overridden by the SETSTREAMID opcode as part of 8559e520a6SMikko Perttunen * each job. 8659e520a6SMikko Perttunen */ 87f3779cb1SThierry Reding vic_writel(vic, value, VIC_THI_STREAMID0); 8859e520a6SMikko Perttunen 8959e520a6SMikko Perttunen /* STREAMID1 is used for firmware loading. */ 90f3779cb1SThierry Reding vic_writel(vic, value, VIC_THI_STREAMID1); 91f3779cb1SThierry Reding } 92f3779cb1SThierry Reding } 93509869a2SAnders Roxell #endif 94f3779cb1SThierry Reding 950ae797a8SArto Merilainen /* setup clockgating registers */ 960ae797a8SArto Merilainen vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) | 970ae797a8SArto Merilainen CG_IDLE_CG_EN | 980ae797a8SArto Merilainen CG_WAKEUP_DLY_CNT(4), 990ae797a8SArto Merilainen NV_PVIC_MISC_PRI_VIC_CG); 1000ae797a8SArto Merilainen 1010ae797a8SArto Merilainen err = falcon_boot(&vic->falcon); 1020ae797a8SArto Merilainen if (err < 0) 1030ae797a8SArto Merilainen return err; 1040ae797a8SArto Merilainen 105d972d624SThierry Reding hdr = vic->falcon.firmware.virt; 1060ae797a8SArto Merilainen fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET); 10758ef3aebSMikko Perttunen 10858ef3aebSMikko Perttunen /* Old VIC firmware needs kernel help with setting up FCE microcode. */ 10958ef3aebSMikko Perttunen if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) { 110d972d624SThierry Reding hdr = vic->falcon.firmware.virt + 1110ae797a8SArto Merilainen *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET); 1120ae797a8SArto Merilainen fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET); 1130ae797a8SArto Merilainen 1140ae797a8SArto Merilainen falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE, 1150ae797a8SArto Merilainen fce_ucode_size); 11658ef3aebSMikko Perttunen falcon_execute_method( 11758ef3aebSMikko Perttunen &vic->falcon, VIC_SET_FCE_UCODE_OFFSET, 11858ef3aebSMikko Perttunen (vic->falcon.firmware.iova + fce_bin_data_offset) >> 8); 11958ef3aebSMikko Perttunen } 1200ae797a8SArto Merilainen 1210ae797a8SArto Merilainen err = falcon_wait_idle(&vic->falcon); 1220ae797a8SArto Merilainen if (err < 0) { 1230ae797a8SArto Merilainen dev_err(vic->dev, 1240ae797a8SArto Merilainen "failed to set application ID and FCE base\n"); 1250ae797a8SArto Merilainen return err; 1260ae797a8SArto Merilainen } 1270ae797a8SArto Merilainen 1280ae797a8SArto Merilainen return 0; 1290ae797a8SArto Merilainen } 1300ae797a8SArto Merilainen 1310ae797a8SArto Merilainen static int vic_init(struct host1x_client *client) 1320ae797a8SArto Merilainen { 1330ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client); 134608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host); 1350ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private; 1360ae797a8SArto Merilainen struct vic *vic = to_vic(drm); 1370ae797a8SArto Merilainen int err; 1380ae797a8SArto Merilainen 1397edd7961SThierry Reding err = host1x_client_iommu_attach(client); 140a8817489SThierry Reding if (err < 0 && err != -ENODEV) { 1417baa943eSThierry Reding dev_err(vic->dev, "failed to attach to domain: %d\n", err); 1420ae797a8SArto Merilainen return err; 1430ae797a8SArto Merilainen } 1440ae797a8SArto Merilainen 145caccddcfSThierry Reding vic->channel = host1x_channel_request(client); 1460ae797a8SArto Merilainen if (!vic->channel) { 1470ae797a8SArto Merilainen err = -ENOMEM; 148bc8828bdSThierry Reding goto detach; 1490ae797a8SArto Merilainen } 1500ae797a8SArto Merilainen 151617dd7ccSThierry Reding client->syncpts[0] = host1x_syncpt_request(client, 0); 1520ae797a8SArto Merilainen if (!client->syncpts[0]) { 1530ae797a8SArto Merilainen err = -ENOMEM; 1540ae797a8SArto Merilainen goto free_channel; 1550ae797a8SArto Merilainen } 1560ae797a8SArto Merilainen 1571e15f5b9SDmitry Osipenko pm_runtime_enable(client->dev); 1581e15f5b9SDmitry Osipenko pm_runtime_use_autosuspend(client->dev); 1591e15f5b9SDmitry Osipenko pm_runtime_set_autosuspend_delay(client->dev, 500); 1601e15f5b9SDmitry Osipenko 1610ae797a8SArto Merilainen err = tegra_drm_register_client(tegra, drm); 1620ae797a8SArto Merilainen if (err < 0) 1631e15f5b9SDmitry Osipenko goto disable_rpm; 1640ae797a8SArto Merilainen 16547b15779SThierry Reding /* 16647b15779SThierry Reding * Inherit the DMA parameters (such as maximum segment size) from the 167608f43adSThierry Reding * parent host1x device. 16847b15779SThierry Reding */ 169608f43adSThierry Reding client->dev->dma_parms = client->host->dma_parms; 17047b15779SThierry Reding 1710ae797a8SArto Merilainen return 0; 1720ae797a8SArto Merilainen 1731e15f5b9SDmitry Osipenko disable_rpm: 1741e15f5b9SDmitry Osipenko pm_runtime_dont_use_autosuspend(client->dev); 1751e15f5b9SDmitry Osipenko pm_runtime_force_suspend(client->dev); 1761e15f5b9SDmitry Osipenko 1772aed4f5aSMikko Perttunen host1x_syncpt_put(client->syncpts[0]); 1780ae797a8SArto Merilainen free_channel: 1798474b025SMikko Perttunen host1x_channel_put(vic->channel); 180bc8828bdSThierry Reding detach: 181aacdf198SThierry Reding host1x_client_iommu_detach(client); 1820ae797a8SArto Merilainen 1830ae797a8SArto Merilainen return err; 1840ae797a8SArto Merilainen } 1850ae797a8SArto Merilainen 1860ae797a8SArto Merilainen static int vic_exit(struct host1x_client *client) 1870ae797a8SArto Merilainen { 1880ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client); 189608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host); 1900ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private; 1910ae797a8SArto Merilainen struct vic *vic = to_vic(drm); 1920ae797a8SArto Merilainen int err; 1930ae797a8SArto Merilainen 19447b15779SThierry Reding /* avoid a dangling pointer just in case this disappears */ 19547b15779SThierry Reding client->dev->dma_parms = NULL; 19647b15779SThierry Reding 1970ae797a8SArto Merilainen err = tegra_drm_unregister_client(tegra, drm); 1980ae797a8SArto Merilainen if (err < 0) 1990ae797a8SArto Merilainen return err; 2000ae797a8SArto Merilainen 2011e15f5b9SDmitry Osipenko pm_runtime_dont_use_autosuspend(client->dev); 2021e15f5b9SDmitry Osipenko pm_runtime_force_suspend(client->dev); 2031e15f5b9SDmitry Osipenko 2042aed4f5aSMikko Perttunen host1x_syncpt_put(client->syncpts[0]); 2058474b025SMikko Perttunen host1x_channel_put(vic->channel); 206aacdf198SThierry Reding host1x_client_iommu_detach(client); 2070ae797a8SArto Merilainen 2081e15f5b9SDmitry Osipenko vic->channel = NULL; 2091e15f5b9SDmitry Osipenko 210d972d624SThierry Reding if (client->group) { 211d972d624SThierry Reding dma_unmap_single(vic->dev, vic->falcon.firmware.phys, 212d972d624SThierry Reding vic->falcon.firmware.size, DMA_TO_DEVICE); 21320e7dce2SThierry Reding tegra_drm_free(tegra, vic->falcon.firmware.size, 214d972d624SThierry Reding vic->falcon.firmware.virt, 215d972d624SThierry Reding vic->falcon.firmware.iova); 216d972d624SThierry Reding } else { 21720e7dce2SThierry Reding dma_free_coherent(vic->dev, vic->falcon.firmware.size, 218d972d624SThierry Reding vic->falcon.firmware.virt, 219d972d624SThierry Reding vic->falcon.firmware.iova); 220d972d624SThierry Reding } 22120e7dce2SThierry Reding 2220ae797a8SArto Merilainen return 0; 2230ae797a8SArto Merilainen } 2240ae797a8SArto Merilainen 2250ae797a8SArto Merilainen static const struct host1x_client_ops vic_client_ops = { 2260ae797a8SArto Merilainen .init = vic_init, 2270ae797a8SArto Merilainen .exit = vic_exit, 2280ae797a8SArto Merilainen }; 2290ae797a8SArto Merilainen 23077a0b09dSThierry Reding static int vic_load_firmware(struct vic *vic) 23177a0b09dSThierry Reding { 23220e7dce2SThierry Reding struct host1x_client *client = &vic->client.base; 23320e7dce2SThierry Reding struct tegra_drm *tegra = vic->client.drm; 234*bf0297acSMikko Perttunen static DEFINE_MUTEX(lock); 235*bf0297acSMikko Perttunen u32 fce_bin_data_offset; 236d972d624SThierry Reding dma_addr_t iova; 23720e7dce2SThierry Reding size_t size; 23820e7dce2SThierry Reding void *virt; 23977a0b09dSThierry Reding int err; 24077a0b09dSThierry Reding 241*bf0297acSMikko Perttunen mutex_lock(&lock); 242*bf0297acSMikko Perttunen 243*bf0297acSMikko Perttunen if (vic->falcon.firmware.virt) { 244*bf0297acSMikko Perttunen err = 0; 245*bf0297acSMikko Perttunen goto unlock; 246*bf0297acSMikko Perttunen } 24777a0b09dSThierry Reding 24877a0b09dSThierry Reding err = falcon_read_firmware(&vic->falcon, vic->config->firmware); 24977a0b09dSThierry Reding if (err < 0) 250*bf0297acSMikko Perttunen goto unlock; 25120e7dce2SThierry Reding 25220e7dce2SThierry Reding size = vic->falcon.firmware.size; 25320e7dce2SThierry Reding 25420e7dce2SThierry Reding if (!client->group) { 255d972d624SThierry Reding virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL); 256*bf0297acSMikko Perttunen if (!virt) { 257*bf0297acSMikko Perttunen err = -ENOMEM; 258*bf0297acSMikko Perttunen goto unlock; 259*bf0297acSMikko Perttunen } 26020e7dce2SThierry Reding } else { 261d972d624SThierry Reding virt = tegra_drm_alloc(tegra, size, &iova); 262*bf0297acSMikko Perttunen if (IS_ERR(virt)) { 263*bf0297acSMikko Perttunen err = PTR_ERR(virt); 264*bf0297acSMikko Perttunen goto unlock; 265*bf0297acSMikko Perttunen } 26620e7dce2SThierry Reding } 26720e7dce2SThierry Reding 268d972d624SThierry Reding vic->falcon.firmware.virt = virt; 269d972d624SThierry Reding vic->falcon.firmware.iova = iova; 27077a0b09dSThierry Reding 27177a0b09dSThierry Reding err = falcon_load_firmware(&vic->falcon); 27277a0b09dSThierry Reding if (err < 0) 27377a0b09dSThierry Reding goto cleanup; 27477a0b09dSThierry Reding 27520e7dce2SThierry Reding /* 27620e7dce2SThierry Reding * In this case we have received an IOVA from the shared domain, so we 27720e7dce2SThierry Reding * need to make sure to get the physical address so that the DMA API 27820e7dce2SThierry Reding * knows what memory pages to flush the cache for. 27920e7dce2SThierry Reding */ 28020e7dce2SThierry Reding if (client->group) { 281d972d624SThierry Reding dma_addr_t phys; 282d972d624SThierry Reding 28320e7dce2SThierry Reding phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE); 28420e7dce2SThierry Reding 28520e7dce2SThierry Reding err = dma_mapping_error(vic->dev, phys); 28620e7dce2SThierry Reding if (err < 0) 28720e7dce2SThierry Reding goto cleanup; 28820e7dce2SThierry Reding 289d972d624SThierry Reding vic->falcon.firmware.phys = phys; 29020e7dce2SThierry Reding } 29120e7dce2SThierry Reding 292*bf0297acSMikko Perttunen /* 293*bf0297acSMikko Perttunen * Check if firmware is new enough to not require mapping firmware 294*bf0297acSMikko Perttunen * to data buffer domains. 295*bf0297acSMikko Perttunen */ 296*bf0297acSMikko Perttunen fce_bin_data_offset = *(u32 *)(virt + VIC_UCODE_FCE_DATA_OFFSET); 297*bf0297acSMikko Perttunen 298*bf0297acSMikko Perttunen if (!vic->config->supports_sid) { 299*bf0297acSMikko Perttunen vic->can_use_context = false; 300*bf0297acSMikko Perttunen } else if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) { 301*bf0297acSMikko Perttunen /* 302*bf0297acSMikko Perttunen * Firmware will access FCE through STREAMID0, so context 303*bf0297acSMikko Perttunen * isolation cannot be used. 304*bf0297acSMikko Perttunen */ 305*bf0297acSMikko Perttunen vic->can_use_context = false; 306*bf0297acSMikko Perttunen dev_warn_once(vic->dev, "context isolation disabled due to old firmware\n"); 307*bf0297acSMikko Perttunen } else { 308*bf0297acSMikko Perttunen vic->can_use_context = true; 309*bf0297acSMikko Perttunen } 310*bf0297acSMikko Perttunen 311*bf0297acSMikko Perttunen unlock: 312*bf0297acSMikko Perttunen mutex_unlock(&lock); 313*bf0297acSMikko Perttunen return err; 31477a0b09dSThierry Reding 31577a0b09dSThierry Reding cleanup: 31620e7dce2SThierry Reding if (!client->group) 317d972d624SThierry Reding dma_free_coherent(vic->dev, size, virt, iova); 31820e7dce2SThierry Reding else 319d972d624SThierry Reding tegra_drm_free(tegra, size, virt, iova); 32020e7dce2SThierry Reding 321*bf0297acSMikko Perttunen mutex_unlock(&lock); 32277a0b09dSThierry Reding return err; 32377a0b09dSThierry Reding } 32477a0b09dSThierry Reding 32599166123SMikko Perttunen 32699166123SMikko Perttunen static int vic_runtime_resume(struct device *dev) 32799166123SMikko Perttunen { 32899166123SMikko Perttunen struct vic *vic = dev_get_drvdata(dev); 32999166123SMikko Perttunen int err; 33099166123SMikko Perttunen 33199166123SMikko Perttunen err = clk_prepare_enable(vic->clk); 33299166123SMikko Perttunen if (err < 0) 33399166123SMikko Perttunen return err; 33499166123SMikko Perttunen 33599166123SMikko Perttunen usleep_range(10, 20); 33699166123SMikko Perttunen 33799166123SMikko Perttunen err = reset_control_deassert(vic->rst); 33899166123SMikko Perttunen if (err < 0) 33999166123SMikko Perttunen goto disable; 34099166123SMikko Perttunen 34199166123SMikko Perttunen usleep_range(10, 20); 34299166123SMikko Perttunen 34399166123SMikko Perttunen err = vic_load_firmware(vic); 34499166123SMikko Perttunen if (err < 0) 34599166123SMikko Perttunen goto assert; 34699166123SMikko Perttunen 34799166123SMikko Perttunen err = vic_boot(vic); 34899166123SMikko Perttunen if (err < 0) 34999166123SMikko Perttunen goto assert; 35099166123SMikko Perttunen 35199166123SMikko Perttunen return 0; 35299166123SMikko Perttunen 35399166123SMikko Perttunen assert: 35499166123SMikko Perttunen reset_control_assert(vic->rst); 35599166123SMikko Perttunen disable: 35699166123SMikko Perttunen clk_disable_unprepare(vic->clk); 35799166123SMikko Perttunen return err; 35899166123SMikko Perttunen } 35999166123SMikko Perttunen 36099166123SMikko Perttunen static int vic_runtime_suspend(struct device *dev) 36199166123SMikko Perttunen { 36299166123SMikko Perttunen struct vic *vic = dev_get_drvdata(dev); 36399166123SMikko Perttunen int err; 36499166123SMikko Perttunen 3651e15f5b9SDmitry Osipenko host1x_channel_stop(vic->channel); 3661e15f5b9SDmitry Osipenko 36799166123SMikko Perttunen err = reset_control_assert(vic->rst); 36899166123SMikko Perttunen if (err < 0) 36999166123SMikko Perttunen return err; 37099166123SMikko Perttunen 37199166123SMikko Perttunen usleep_range(2000, 4000); 37299166123SMikko Perttunen 37399166123SMikko Perttunen clk_disable_unprepare(vic->clk); 37499166123SMikko Perttunen 37599166123SMikko Perttunen return 0; 37699166123SMikko Perttunen } 37799166123SMikko Perttunen 3780ae797a8SArto Merilainen static int vic_open_channel(struct tegra_drm_client *client, 3790ae797a8SArto Merilainen struct tegra_drm_context *context) 3800ae797a8SArto Merilainen { 3810ae797a8SArto Merilainen struct vic *vic = to_vic(client); 3820ae797a8SArto Merilainen 3830ae797a8SArto Merilainen context->channel = host1x_channel_get(vic->channel); 38458ed47adSDmitry Osipenko if (!context->channel) 38599166123SMikko Perttunen return -ENOMEM; 3860ae797a8SArto Merilainen 3870ae797a8SArto Merilainen return 0; 3880ae797a8SArto Merilainen } 3890ae797a8SArto Merilainen 3900ae797a8SArto Merilainen static void vic_close_channel(struct tegra_drm_context *context) 3910ae797a8SArto Merilainen { 3920ae797a8SArto Merilainen host1x_channel_put(context->channel); 3930ae797a8SArto Merilainen } 3940ae797a8SArto Merilainen 395*bf0297acSMikko Perttunen static int vic_can_use_memory_ctx(struct tegra_drm_client *client, bool *supported) 396*bf0297acSMikko Perttunen { 397*bf0297acSMikko Perttunen struct vic *vic = to_vic(client); 398*bf0297acSMikko Perttunen int err; 399*bf0297acSMikko Perttunen 400*bf0297acSMikko Perttunen /* This doesn't access HW so it's safe to call without powering up. */ 401*bf0297acSMikko Perttunen err = vic_load_firmware(vic); 402*bf0297acSMikko Perttunen if (err < 0) 403*bf0297acSMikko Perttunen return err; 404*bf0297acSMikko Perttunen 405*bf0297acSMikko Perttunen *supported = vic->can_use_context; 406*bf0297acSMikko Perttunen 407*bf0297acSMikko Perttunen return 0; 408*bf0297acSMikko Perttunen } 409*bf0297acSMikko Perttunen 4100ae797a8SArto Merilainen static const struct tegra_drm_client_ops vic_ops = { 4110ae797a8SArto Merilainen .open_channel = vic_open_channel, 4120ae797a8SArto Merilainen .close_channel = vic_close_channel, 4130ae797a8SArto Merilainen .submit = tegra_drm_submit, 414*bf0297acSMikko Perttunen .get_streamid_offset = tegra_drm_get_streamid_offset_thi, 415*bf0297acSMikko Perttunen .can_use_memory_ctx = vic_can_use_memory_ctx, 4160ae797a8SArto Merilainen }; 4170ae797a8SArto Merilainen 418788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin" 419788ff4b6SNicolas Chauvet 4200ae797a8SArto Merilainen static const struct vic_config vic_t124_config = { 421788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE, 422acae8a9dSThierry Reding .version = 0x40, 423f3779cb1SThierry Reding .supports_sid = false, 4240ae797a8SArto Merilainen }; 4250ae797a8SArto Merilainen 426788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin" 427788ff4b6SNicolas Chauvet 4280ae797a8SArto Merilainen static const struct vic_config vic_t210_config = { 429788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE, 430acae8a9dSThierry Reding .version = 0x21, 431f3779cb1SThierry Reding .supports_sid = false, 4320ae797a8SArto Merilainen }; 4330ae797a8SArto Merilainen 4346e44b9adSMikko Perttunen #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin" 4356e44b9adSMikko Perttunen 4366e44b9adSMikko Perttunen static const struct vic_config vic_t186_config = { 4376e44b9adSMikko Perttunen .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE, 438acae8a9dSThierry Reding .version = 0x18, 439f3779cb1SThierry Reding .supports_sid = true, 4406e44b9adSMikko Perttunen }; 4416e44b9adSMikko Perttunen 442d6b9bc02SThierry Reding #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin" 443d6b9bc02SThierry Reding 444d6b9bc02SThierry Reding static const struct vic_config vic_t194_config = { 445d6b9bc02SThierry Reding .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE, 446d6b9bc02SThierry Reding .version = 0x19, 447f3779cb1SThierry Reding .supports_sid = true, 448d6b9bc02SThierry Reding }; 449d6b9bc02SThierry Reding 45082d73874SThierry Reding static const struct of_device_id tegra_vic_of_match[] = { 4510ae797a8SArto Merilainen { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config }, 4520ae797a8SArto Merilainen { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config }, 4536e44b9adSMikko Perttunen { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config }, 454d6b9bc02SThierry Reding { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config }, 4550ae797a8SArto Merilainen { }, 4560ae797a8SArto Merilainen }; 45782d73874SThierry Reding MODULE_DEVICE_TABLE(of, tegra_vic_of_match); 4580ae797a8SArto Merilainen 4590ae797a8SArto Merilainen static int vic_probe(struct platform_device *pdev) 4600ae797a8SArto Merilainen { 4610ae797a8SArto Merilainen struct device *dev = &pdev->dev; 4620ae797a8SArto Merilainen struct host1x_syncpt **syncpts; 4630ae797a8SArto Merilainen struct resource *regs; 4640ae797a8SArto Merilainen struct vic *vic; 4650ae797a8SArto Merilainen int err; 4660ae797a8SArto Merilainen 467d5ad0e3dSThierry Reding /* inherit DMA mask from host1x parent */ 468d5ad0e3dSThierry Reding err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask); 469d5ad0e3dSThierry Reding if (err < 0) { 470d5ad0e3dSThierry Reding dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err); 471d5ad0e3dSThierry Reding return err; 472d5ad0e3dSThierry Reding } 473d5ad0e3dSThierry Reding 4740ae797a8SArto Merilainen vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL); 4750ae797a8SArto Merilainen if (!vic) 4760ae797a8SArto Merilainen return -ENOMEM; 4770ae797a8SArto Merilainen 478829ce7a6SThierry Reding vic->config = of_device_get_match_data(dev); 479829ce7a6SThierry Reding 4800ae797a8SArto Merilainen syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL); 4810ae797a8SArto Merilainen if (!syncpts) 4820ae797a8SArto Merilainen return -ENOMEM; 4830ae797a8SArto Merilainen 4840ae797a8SArto Merilainen regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4850ae797a8SArto Merilainen if (!regs) { 4860ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to get registers\n"); 4870ae797a8SArto Merilainen return -ENXIO; 4880ae797a8SArto Merilainen } 4890ae797a8SArto Merilainen 4900ae797a8SArto Merilainen vic->regs = devm_ioremap_resource(dev, regs); 4910ae797a8SArto Merilainen if (IS_ERR(vic->regs)) 4920ae797a8SArto Merilainen return PTR_ERR(vic->regs); 4930ae797a8SArto Merilainen 4940ae797a8SArto Merilainen vic->clk = devm_clk_get(dev, NULL); 4950ae797a8SArto Merilainen if (IS_ERR(vic->clk)) { 4960ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to get clock\n"); 4970ae797a8SArto Merilainen return PTR_ERR(vic->clk); 4980ae797a8SArto Merilainen } 4990ae797a8SArto Merilainen 500e97a951fSMikko Perttunen err = clk_set_rate(vic->clk, ULONG_MAX); 501e97a951fSMikko Perttunen if (err < 0) { 502e97a951fSMikko Perttunen dev_err(&pdev->dev, "failed to set clock rate\n"); 503e97a951fSMikko Perttunen return err; 504e97a951fSMikko Perttunen } 505e97a951fSMikko Perttunen 5060dc34e19SThierry Reding if (!dev->pm_domain) { 5070dc34e19SThierry Reding vic->rst = devm_reset_control_get(dev, "vic"); 5080dc34e19SThierry Reding if (IS_ERR(vic->rst)) { 5090dc34e19SThierry Reding dev_err(&pdev->dev, "failed to get reset\n"); 5100dc34e19SThierry Reding return PTR_ERR(vic->rst); 5110dc34e19SThierry Reding } 5120dc34e19SThierry Reding } 5130dc34e19SThierry Reding 5140ae797a8SArto Merilainen vic->falcon.dev = dev; 5150ae797a8SArto Merilainen vic->falcon.regs = vic->regs; 5160ae797a8SArto Merilainen 5170ae797a8SArto Merilainen err = falcon_init(&vic->falcon); 5180ae797a8SArto Merilainen if (err < 0) 5190ae797a8SArto Merilainen return err; 5200ae797a8SArto Merilainen 5210ae797a8SArto Merilainen platform_set_drvdata(pdev, vic); 5220ae797a8SArto Merilainen 5230ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.base.list); 5240ae797a8SArto Merilainen vic->client.base.ops = &vic_client_ops; 5250ae797a8SArto Merilainen vic->client.base.dev = dev; 5260ae797a8SArto Merilainen vic->client.base.class = HOST1X_CLASS_VIC; 5270ae797a8SArto Merilainen vic->client.base.syncpts = syncpts; 5280ae797a8SArto Merilainen vic->client.base.num_syncpts = 1; 5290ae797a8SArto Merilainen vic->dev = dev; 5300ae797a8SArto Merilainen 5310ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.list); 532acae8a9dSThierry Reding vic->client.version = vic->config->version; 5330ae797a8SArto Merilainen vic->client.ops = &vic_ops; 5340ae797a8SArto Merilainen 5350ae797a8SArto Merilainen err = host1x_client_register(&vic->client.base); 5360ae797a8SArto Merilainen if (err < 0) { 5370ae797a8SArto Merilainen dev_err(dev, "failed to register host1x client: %d\n", err); 5380ae797a8SArto Merilainen goto exit_falcon; 5390ae797a8SArto Merilainen } 5400ae797a8SArto Merilainen 5410ae797a8SArto Merilainen return 0; 5420ae797a8SArto Merilainen 5430ae797a8SArto Merilainen exit_falcon: 5440ae797a8SArto Merilainen falcon_exit(&vic->falcon); 5450ae797a8SArto Merilainen 5460ae797a8SArto Merilainen return err; 5470ae797a8SArto Merilainen } 5480ae797a8SArto Merilainen 5490ae797a8SArto Merilainen static int vic_remove(struct platform_device *pdev) 5500ae797a8SArto Merilainen { 5510ae797a8SArto Merilainen struct vic *vic = platform_get_drvdata(pdev); 5520ae797a8SArto Merilainen int err; 5530ae797a8SArto Merilainen 5540ae797a8SArto Merilainen err = host1x_client_unregister(&vic->client.base); 5550ae797a8SArto Merilainen if (err < 0) { 5560ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", 5570ae797a8SArto Merilainen err); 5580ae797a8SArto Merilainen return err; 5590ae797a8SArto Merilainen } 5600ae797a8SArto Merilainen 5610ae797a8SArto Merilainen falcon_exit(&vic->falcon); 5620ae797a8SArto Merilainen 5630ae797a8SArto Merilainen return 0; 5640ae797a8SArto Merilainen } 5650ae797a8SArto Merilainen 5660ae797a8SArto Merilainen static const struct dev_pm_ops vic_pm_ops = { 56742457494SArnd Bergmann RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL) 56842457494SArnd Bergmann SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) 5690ae797a8SArto Merilainen }; 5700ae797a8SArto Merilainen 5710ae797a8SArto Merilainen struct platform_driver tegra_vic_driver = { 5720ae797a8SArto Merilainen .driver = { 5730ae797a8SArto Merilainen .name = "tegra-vic", 57482d73874SThierry Reding .of_match_table = tegra_vic_of_match, 5750ae797a8SArto Merilainen .pm = &vic_pm_ops 5760ae797a8SArto Merilainen }, 5770ae797a8SArto Merilainen .probe = vic_probe, 5780ae797a8SArto Merilainen .remove = vic_remove, 5790ae797a8SArto Merilainen }; 580788ff4b6SNicolas Chauvet 581788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC) 582788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE); 583788ff4b6SNicolas Chauvet #endif 584788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 585788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE); 586788ff4b6SNicolas Chauvet #endif 5876e44b9adSMikko Perttunen #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) 5886e44b9adSMikko Perttunen MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE); 5896e44b9adSMikko Perttunen #endif 590d6b9bc02SThierry Reding #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) 591d6b9bc02SThierry Reding MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE); 592d6b9bc02SThierry Reding #endif 593