18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2017-2019 The Linux Foundation. All rights reserved. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/clk.h>
58c2ecf20Sopenharmony_ci#include <linux/interconnect.h>
68c2ecf20Sopenharmony_ci#include <linux/pm_domain.h>
78c2ecf20Sopenharmony_ci#include <linux/pm_opp.h>
88c2ecf20Sopenharmony_ci#include <soc/qcom/cmd-db.h>
98c2ecf20Sopenharmony_ci#include <drm/drm_gem.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "a6xx_gpu.h"
128c2ecf20Sopenharmony_ci#include "a6xx_gmu.xml.h"
138c2ecf20Sopenharmony_ci#include "msm_gem.h"
148c2ecf20Sopenharmony_ci#include "msm_gpu_trace.h"
158c2ecf20Sopenharmony_ci#include "msm_mmu.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic void a6xx_gmu_fault(struct a6xx_gmu *gmu)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
208c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
218c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &adreno_gpu->base;
228c2ecf20Sopenharmony_ci	struct drm_device *dev = gpu->dev;
238c2ecf20Sopenharmony_ci	struct msm_drm_private *priv = dev->dev_private;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	/* FIXME: add a banner here */
268c2ecf20Sopenharmony_ci	gmu->hung = true;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	/* Turn off the hangcheck timer while we are resetting */
298c2ecf20Sopenharmony_ci	del_timer(&gpu->hangcheck_timer);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	/* Queue the GPU handler because we need to treat this as a recovery */
328c2ecf20Sopenharmony_ci	queue_work(priv->wq, &gpu->recover_work);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic irqreturn_t a6xx_gmu_irq(int irq, void *data)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = data;
388c2ecf20Sopenharmony_ci	u32 status;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	status = gmu_read(gmu, REG_A6XX_GMU_AO_HOST_INTERRUPT_STATUS);
418c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AO_HOST_INTERRUPT_CLR, status);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (status & A6XX_GMU_AO_HOST_INTERRUPT_STATUS_WDOG_BITE) {
448c2ecf20Sopenharmony_ci		dev_err_ratelimited(gmu->dev, "GMU watchdog expired\n");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci		a6xx_gmu_fault(gmu);
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (status &  A6XX_GMU_AO_HOST_INTERRUPT_STATUS_HOST_AHB_BUS_ERROR)
508c2ecf20Sopenharmony_ci		dev_err_ratelimited(gmu->dev, "GMU AHB bus error\n");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (status & A6XX_GMU_AO_HOST_INTERRUPT_STATUS_FENCE_ERR)
538c2ecf20Sopenharmony_ci		dev_err_ratelimited(gmu->dev, "GMU fence error: 0x%x\n",
548c2ecf20Sopenharmony_ci			gmu_read(gmu, REG_A6XX_GMU_AHB_FENCE_STATUS));
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic irqreturn_t a6xx_hfi_irq(int irq, void *data)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = data;
628c2ecf20Sopenharmony_ci	u32 status;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	status = gmu_read(gmu, REG_A6XX_GMU_GMU2HOST_INTR_INFO);
658c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_CLR, status);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (status & A6XX_GMU_GMU2HOST_INTR_INFO_CM3_FAULT) {
688c2ecf20Sopenharmony_ci		dev_err_ratelimited(gmu->dev, "GMU firmware fault\n");
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		a6xx_gmu_fault(gmu);
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cibool a6xx_gmu_sptprac_is_on(struct a6xx_gmu *gmu)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	u32 val;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* This can be called from gpu state code so make sure GMU is valid */
818c2ecf20Sopenharmony_ci	if (!gmu->initialized)
828c2ecf20Sopenharmony_ci		return false;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	val = gmu_read(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return !(val &
878c2ecf20Sopenharmony_ci		(A6XX_GMU_SPTPRAC_PWR_CLK_STATUS_SPTPRAC_GDSC_POWER_OFF |
888c2ecf20Sopenharmony_ci		A6XX_GMU_SPTPRAC_PWR_CLK_STATUS_SP_CLOCK_OFF));
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Check to see if the GX rail is still powered */
928c2ecf20Sopenharmony_cibool a6xx_gmu_gx_is_on(struct a6xx_gmu *gmu)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	u32 val;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* This can be called from gpu state code so make sure GMU is valid */
978c2ecf20Sopenharmony_ci	if (!gmu->initialized)
988c2ecf20Sopenharmony_ci		return false;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	val = gmu_read(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	return !(val &
1038c2ecf20Sopenharmony_ci		(A6XX_GMU_SPTPRAC_PWR_CLK_STATUS_GX_HM_GDSC_POWER_OFF |
1048c2ecf20Sopenharmony_ci		A6XX_GMU_SPTPRAC_PWR_CLK_STATUS_GX_HM_CLK_OFF));
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_civoid a6xx_gmu_set_freq(struct msm_gpu *gpu, struct dev_pm_opp *opp)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
1108c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
1118c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
1128c2ecf20Sopenharmony_ci	u32 perf_index;
1138c2ecf20Sopenharmony_ci	unsigned long gpu_freq;
1148c2ecf20Sopenharmony_ci	int ret = 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	gpu_freq = dev_pm_opp_get_freq(opp);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (gpu_freq == gmu->freq)
1198c2ecf20Sopenharmony_ci		return;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	for (perf_index = 0; perf_index < gmu->nr_gpu_freqs - 1; perf_index++)
1228c2ecf20Sopenharmony_ci		if (gpu_freq == gmu->gpu_freqs[perf_index])
1238c2ecf20Sopenharmony_ci			break;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	gmu->current_perf_index = perf_index;
1268c2ecf20Sopenharmony_ci	gmu->freq = gmu->gpu_freqs[perf_index];
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	trace_msm_gmu_freq_change(gmu->freq, perf_index);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * This can get called from devfreq while the hardware is idle. Don't
1328c2ecf20Sopenharmony_ci	 * bring up the power if it isn't already active
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	if (pm_runtime_get_if_in_use(gmu->dev) == 0)
1358c2ecf20Sopenharmony_ci		return;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (!gmu->legacy) {
1388c2ecf20Sopenharmony_ci		a6xx_hfi_set_freq(gmu, perf_index);
1398c2ecf20Sopenharmony_ci		dev_pm_opp_set_bw(&gpu->pdev->dev, opp);
1408c2ecf20Sopenharmony_ci		pm_runtime_put(gmu->dev);
1418c2ecf20Sopenharmony_ci		return;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_DCVS_ACK_OPTION, 0);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_DCVS_PERF_SETTING,
1478c2ecf20Sopenharmony_ci			((3 & 0xf) << 28) | perf_index);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/*
1508c2ecf20Sopenharmony_ci	 * Send an invalid index as a vote for the bus bandwidth and let the
1518c2ecf20Sopenharmony_ci	 * firmware decide on the right vote
1528c2ecf20Sopenharmony_ci	 */
1538c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_DCVS_BW_SETTING, 0xff);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Set and clear the OOB for DCVS to trigger the GMU */
1568c2ecf20Sopenharmony_ci	a6xx_gmu_set_oob(gmu, GMU_OOB_DCVS_SET);
1578c2ecf20Sopenharmony_ci	a6xx_gmu_clear_oob(gmu, GMU_OOB_DCVS_SET);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	ret = gmu_read(gmu, REG_A6XX_GMU_DCVS_RETURN);
1608c2ecf20Sopenharmony_ci	if (ret)
1618c2ecf20Sopenharmony_ci		dev_err(gmu->dev, "GMU set GPU frequency error: %d\n", ret);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	dev_pm_opp_set_bw(&gpu->pdev->dev, opp);
1648c2ecf20Sopenharmony_ci	pm_runtime_put(gmu->dev);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciunsigned long a6xx_gmu_get_freq(struct msm_gpu *gpu)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
1708c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
1718c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return  gmu->freq;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic bool a6xx_gmu_check_idle_level(struct a6xx_gmu *gmu)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	u32 val;
1798c2ecf20Sopenharmony_ci	int local = gmu->idle_level;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* SPTP and IFPC both report as IFPC */
1828c2ecf20Sopenharmony_ci	if (gmu->idle_level == GMU_IDLE_STATE_SPTP)
1838c2ecf20Sopenharmony_ci		local = GMU_IDLE_STATE_IFPC;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	val = gmu_read(gmu, REG_A6XX_GPU_GMU_CX_GMU_RPMH_POWER_STATE);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (val == local) {
1888c2ecf20Sopenharmony_ci		if (gmu->idle_level != GMU_IDLE_STATE_IFPC ||
1898c2ecf20Sopenharmony_ci			!a6xx_gmu_gx_is_on(gmu))
1908c2ecf20Sopenharmony_ci			return true;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return false;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* Wait for the GMU to get to its most idle state */
1978c2ecf20Sopenharmony_ciint a6xx_gmu_wait_for_idle(struct a6xx_gmu *gmu)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	return spin_until(a6xx_gmu_check_idle_level(gmu));
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int a6xx_gmu_start(struct a6xx_gmu *gmu)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	int ret;
2058c2ecf20Sopenharmony_ci	u32 val;
2068c2ecf20Sopenharmony_ci	u32 mask, reset_val;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	val = gmu_read(gmu, REG_A6XX_GMU_CM3_DTCM_START + 0xff8);
2098c2ecf20Sopenharmony_ci	if (val <= 0x20010004) {
2108c2ecf20Sopenharmony_ci		mask = 0xffffffff;
2118c2ecf20Sopenharmony_ci		reset_val = 0xbabeface;
2128c2ecf20Sopenharmony_ci	} else {
2138c2ecf20Sopenharmony_ci		mask = 0x1ff;
2148c2ecf20Sopenharmony_ci		reset_val = 0x100;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CM3_SYSRESET, 1);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Set the log wptr index
2208c2ecf20Sopenharmony_ci	 * note: downstream saves the value in poweroff and restores it here
2218c2ecf20Sopenharmony_ci	 */
2228c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GPU_GMU_CX_GMU_PWR_COL_CP_RESP, 0);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CM3_SYSRESET, 0);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_CM3_FW_INIT_RESULT, val,
2278c2ecf20Sopenharmony_ci		(val & mask) == reset_val, 100, 10000);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (ret)
2308c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "GMU firmware initialization timed out\n");
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	return ret;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int a6xx_gmu_hfi_start(struct a6xx_gmu *gmu)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	u32 val;
2388c2ecf20Sopenharmony_ci	int ret;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_HFI_CTRL_INIT, 1);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_HFI_CTRL_STATUS, val,
2438c2ecf20Sopenharmony_ci		val & 1, 100, 10000);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "Unable to start the HFI queues\n");
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	return ret;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* Trigger a OOB (out of band) request to the GMU */
2518c2ecf20Sopenharmony_ciint a6xx_gmu_set_oob(struct a6xx_gmu *gmu, enum a6xx_gmu_oob_state state)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	int ret;
2548c2ecf20Sopenharmony_ci	u32 val;
2558c2ecf20Sopenharmony_ci	int request, ack;
2568c2ecf20Sopenharmony_ci	const char *name;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	switch (state) {
2598c2ecf20Sopenharmony_ci	case GMU_OOB_GPU_SET:
2608c2ecf20Sopenharmony_ci		if (gmu->legacy) {
2618c2ecf20Sopenharmony_ci			request = GMU_OOB_GPU_SET_REQUEST;
2628c2ecf20Sopenharmony_ci			ack = GMU_OOB_GPU_SET_ACK;
2638c2ecf20Sopenharmony_ci		} else {
2648c2ecf20Sopenharmony_ci			request = GMU_OOB_GPU_SET_REQUEST_NEW;
2658c2ecf20Sopenharmony_ci			ack = GMU_OOB_GPU_SET_ACK_NEW;
2668c2ecf20Sopenharmony_ci		}
2678c2ecf20Sopenharmony_ci		name = "GPU_SET";
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	case GMU_OOB_PERFCOUNTER_SET:
2708c2ecf20Sopenharmony_ci		if (gmu->legacy) {
2718c2ecf20Sopenharmony_ci			request = GMU_OOB_PERFCOUNTER_REQUEST;
2728c2ecf20Sopenharmony_ci			ack = GMU_OOB_PERFCOUNTER_ACK;
2738c2ecf20Sopenharmony_ci		} else {
2748c2ecf20Sopenharmony_ci			request = GMU_OOB_PERFCOUNTER_REQUEST_NEW;
2758c2ecf20Sopenharmony_ci			ack = GMU_OOB_PERFCOUNTER_ACK_NEW;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci		name = "PERFCOUNTER";
2788c2ecf20Sopenharmony_ci		break;
2798c2ecf20Sopenharmony_ci	case GMU_OOB_BOOT_SLUMBER:
2808c2ecf20Sopenharmony_ci		request = GMU_OOB_BOOT_SLUMBER_REQUEST;
2818c2ecf20Sopenharmony_ci		ack = GMU_OOB_BOOT_SLUMBER_ACK;
2828c2ecf20Sopenharmony_ci		name = "BOOT_SLUMBER";
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci	case GMU_OOB_DCVS_SET:
2858c2ecf20Sopenharmony_ci		request = GMU_OOB_DCVS_REQUEST;
2868c2ecf20Sopenharmony_ci		ack = GMU_OOB_DCVS_ACK;
2878c2ecf20Sopenharmony_ci		name = "GPU_DCVS";
2888c2ecf20Sopenharmony_ci		break;
2898c2ecf20Sopenharmony_ci	default:
2908c2ecf20Sopenharmony_ci		return -EINVAL;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* Trigger the equested OOB operation */
2948c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET, 1 << request);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Wait for the acknowledge interrupt */
2978c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_GMU2HOST_INTR_INFO, val,
2988c2ecf20Sopenharmony_ci		val & (1 << ack), 100, 10000);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if (ret)
3018c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev,
3028c2ecf20Sopenharmony_ci			"Timeout waiting for GMU OOB set %s: 0x%x\n",
3038c2ecf20Sopenharmony_ci				name,
3048c2ecf20Sopenharmony_ci				gmu_read(gmu, REG_A6XX_GMU_GMU2HOST_INTR_INFO));
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* Clear the acknowledge interrupt */
3078c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_CLR, 1 << ack);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	return ret;
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/* Clear a pending OOB state in the GMU */
3138c2ecf20Sopenharmony_civoid a6xx_gmu_clear_oob(struct a6xx_gmu *gmu, enum a6xx_gmu_oob_state state)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	if (!gmu->legacy) {
3168c2ecf20Sopenharmony_ci		if (state == GMU_OOB_GPU_SET) {
3178c2ecf20Sopenharmony_ci			gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3188c2ecf20Sopenharmony_ci				1 << GMU_OOB_GPU_SET_CLEAR_NEW);
3198c2ecf20Sopenharmony_ci		} else {
3208c2ecf20Sopenharmony_ci			WARN_ON(state != GMU_OOB_PERFCOUNTER_SET);
3218c2ecf20Sopenharmony_ci			gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3228c2ecf20Sopenharmony_ci				1 << GMU_OOB_PERFCOUNTER_CLEAR_NEW);
3238c2ecf20Sopenharmony_ci		}
3248c2ecf20Sopenharmony_ci		return;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	switch (state) {
3288c2ecf20Sopenharmony_ci	case GMU_OOB_GPU_SET:
3298c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3308c2ecf20Sopenharmony_ci			1 << GMU_OOB_GPU_SET_CLEAR);
3318c2ecf20Sopenharmony_ci		break;
3328c2ecf20Sopenharmony_ci	case GMU_OOB_PERFCOUNTER_SET:
3338c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3348c2ecf20Sopenharmony_ci			1 << GMU_OOB_PERFCOUNTER_CLEAR);
3358c2ecf20Sopenharmony_ci		break;
3368c2ecf20Sopenharmony_ci	case GMU_OOB_BOOT_SLUMBER:
3378c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3388c2ecf20Sopenharmony_ci			1 << GMU_OOB_BOOT_SLUMBER_CLEAR);
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	case GMU_OOB_DCVS_SET:
3418c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET,
3428c2ecf20Sopenharmony_ci			1 << GMU_OOB_DCVS_CLEAR);
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci/* Enable CPU control of SPTP power power collapse */
3488c2ecf20Sopenharmony_cistatic int a6xx_sptprac_enable(struct a6xx_gmu *gmu)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	int ret;
3518c2ecf20Sopenharmony_ci	u32 val;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (!gmu->legacy)
3548c2ecf20Sopenharmony_ci		return 0;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GX_SPTPRAC_POWER_CONTROL, 0x778000);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS, val,
3598c2ecf20Sopenharmony_ci		(val & 0x38) == 0x28, 1, 100);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (ret) {
3628c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "Unable to power on SPTPRAC: 0x%x\n",
3638c2ecf20Sopenharmony_ci			gmu_read(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS));
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/* Disable CPU control of SPTP power power collapse */
3708c2ecf20Sopenharmony_cistatic void a6xx_sptprac_disable(struct a6xx_gmu *gmu)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	u32 val;
3738c2ecf20Sopenharmony_ci	int ret;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (!gmu->legacy)
3768c2ecf20Sopenharmony_ci		return;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* Make sure retention is on */
3798c2ecf20Sopenharmony_ci	gmu_rmw(gmu, REG_A6XX_GPU_CC_GX_GDSCR, 0, (1 << 11));
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GX_SPTPRAC_POWER_CONTROL, 0x778001);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS, val,
3848c2ecf20Sopenharmony_ci		(val & 0x04), 100, 10000);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (ret)
3878c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "failed to power off SPTPRAC: 0x%x\n",
3888c2ecf20Sopenharmony_ci			gmu_read(gmu, REG_A6XX_GMU_SPTPRAC_PWR_CLK_STATUS));
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci/* Let the GMU know we are starting a boot sequence */
3928c2ecf20Sopenharmony_cistatic int a6xx_gmu_gfx_rail_on(struct a6xx_gmu *gmu)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	u32 vote;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* Let the GMU know we are getting ready for boot */
3978c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_BOOT_SLUMBER_OPTION, 0);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* Choose the "default" power level as the highest available */
4008c2ecf20Sopenharmony_ci	vote = gmu->gx_arc_votes[gmu->nr_gpu_freqs - 1];
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GX_VOTE_IDX, vote & 0xff);
4038c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_MX_VOTE_IDX, (vote >> 8) & 0xff);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* Let the GMU know the boot sequence has started */
4068c2ecf20Sopenharmony_ci	return a6xx_gmu_set_oob(gmu, GMU_OOB_BOOT_SLUMBER);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci/* Let the GMU know that we are about to go into slumber */
4108c2ecf20Sopenharmony_cistatic int a6xx_gmu_notify_slumber(struct a6xx_gmu *gmu)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	int ret;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Disable the power counter so the GMU isn't busy */
4158c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_ENABLE, 0);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* Disable SPTP_PC if the CPU is responsible for it */
4188c2ecf20Sopenharmony_ci	if (gmu->idle_level < GMU_IDLE_STATE_SPTP)
4198c2ecf20Sopenharmony_ci		a6xx_sptprac_disable(gmu);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (!gmu->legacy) {
4228c2ecf20Sopenharmony_ci		ret = a6xx_hfi_send_prep_slumber(gmu);
4238c2ecf20Sopenharmony_ci		goto out;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/* Tell the GMU to get ready to slumber */
4278c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_BOOT_SLUMBER_OPTION, 1);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	ret = a6xx_gmu_set_oob(gmu, GMU_OOB_BOOT_SLUMBER);
4308c2ecf20Sopenharmony_ci	a6xx_gmu_clear_oob(gmu, GMU_OOB_BOOT_SLUMBER);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	if (!ret) {
4338c2ecf20Sopenharmony_ci		/* Check to see if the GMU really did slumber */
4348c2ecf20Sopenharmony_ci		if (gmu_read(gmu, REG_A6XX_GPU_GMU_CX_GMU_RPMH_POWER_STATE)
4358c2ecf20Sopenharmony_ci			!= 0x0f) {
4368c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(gmu->dev, "The GMU did not go into slumber\n");
4378c2ecf20Sopenharmony_ci			ret = -ETIMEDOUT;
4388c2ecf20Sopenharmony_ci		}
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ciout:
4428c2ecf20Sopenharmony_ci	/* Put fence into allow mode */
4438c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AO_AHB_FENCE_CTRL, 0);
4448c2ecf20Sopenharmony_ci	return ret;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int a6xx_rpmh_start(struct a6xx_gmu *gmu)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	int ret;
4508c2ecf20Sopenharmony_ci	u32 val;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_RSCC_CONTROL_REQ, 1 << 1);
4538c2ecf20Sopenharmony_ci	/* Wait for the register to finish posting */
4548c2ecf20Sopenharmony_ci	wmb();
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_RSCC_CONTROL_ACK, val,
4578c2ecf20Sopenharmony_ci		val & (1 << 1), 100, 10000);
4588c2ecf20Sopenharmony_ci	if (ret) {
4598c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "Unable to power on the GPU RSC\n");
4608c2ecf20Sopenharmony_ci		return ret;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout_rscc(gmu, REG_A6XX_RSCC_SEQ_BUSY_DRV0, val,
4648c2ecf20Sopenharmony_ci		!val, 100, 10000);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (ret) {
4678c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "GPU RSC sequence stuck while waking up the GPU\n");
4688c2ecf20Sopenharmony_ci		return ret;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_RSCC_CONTROL_REQ, 0);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* Set up CX GMU counter 0 to count busy ticks */
4748c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_MASK, 0xff000000);
4758c2ecf20Sopenharmony_ci	gmu_rmw(gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_SELECT_0, 0xff, 0x20);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* Enable the power counter */
4788c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_ENABLE, 1);
4798c2ecf20Sopenharmony_ci	return 0;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic void a6xx_rpmh_stop(struct a6xx_gmu *gmu)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	int ret;
4858c2ecf20Sopenharmony_ci	u32 val;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_RSCC_CONTROL_REQ, 1);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	ret = gmu_poll_timeout_rscc(gmu, REG_A6XX_GPU_RSCC_RSC_STATUS0_DRV0,
4908c2ecf20Sopenharmony_ci		val, val & (1 << 16), 100, 10000);
4918c2ecf20Sopenharmony_ci	if (ret)
4928c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "Unable to power off the GPU RSC\n");
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_RSCC_CONTROL_REQ, 0);
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic inline void pdc_write(void __iomem *ptr, u32 offset, u32 value)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	return msm_writel(value, ptr + (offset << 2));
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic void __iomem *a6xx_gmu_get_mmio(struct platform_device *pdev,
5038c2ecf20Sopenharmony_ci		const char *name);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic void a6xx_gmu_rpmh_init(struct a6xx_gmu *gmu)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
5088c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
5098c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(gmu->dev);
5108c2ecf20Sopenharmony_ci	void __iomem *pdcptr = a6xx_gmu_get_mmio(pdev, "gmu_pdc");
5118c2ecf20Sopenharmony_ci	void __iomem *seqptr = a6xx_gmu_get_mmio(pdev, "gmu_pdc_seq");
5128c2ecf20Sopenharmony_ci	uint32_t pdc_address_offset;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if (!pdcptr || !seqptr)
5158c2ecf20Sopenharmony_ci		goto err;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (adreno_is_a618(adreno_gpu) || adreno_is_a640(adreno_gpu))
5188c2ecf20Sopenharmony_ci		pdc_address_offset = 0x30090;
5198c2ecf20Sopenharmony_ci	else if (adreno_is_a650(adreno_gpu))
5208c2ecf20Sopenharmony_ci		pdc_address_offset = 0x300a0;
5218c2ecf20Sopenharmony_ci	else
5228c2ecf20Sopenharmony_ci		pdc_address_offset = 0x30080;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/* Disable SDE clock gating */
5258c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_GPU_RSCC_RSC_STATUS0_DRV0, BIT(24));
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* Setup RSC PDC handshake for sleep and wakeup */
5288c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_PDC_SLAVE_ID_DRV0, 1);
5298c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_DATA, 0);
5308c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_ADDR, 0);
5318c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_DATA + 2, 0);
5328c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_ADDR + 2, 0);
5338c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_DATA + 4, 0x80000000);
5348c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_HIDDEN_TCS_CMD0_ADDR + 4, 0);
5358c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_OVERRIDE_START_ADDR, 0);
5368c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_PDC_SEQ_START_ADDR, 0x4520);
5378c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_PDC_MATCH_VALUE_LO, 0x4510);
5388c2ecf20Sopenharmony_ci	gmu_write_rscc(gmu, REG_A6XX_RSCC_PDC_MATCH_VALUE_HI, 0x4514);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	/* Load RSC sequencer uCode for sleep and wakeup */
5418c2ecf20Sopenharmony_ci	if (adreno_is_a650(adreno_gpu)) {
5428c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0, 0xeaaae5a0);
5438c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 1, 0xe1a1ebab);
5448c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 2, 0xa2e0a581);
5458c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 3, 0xecac82e2);
5468c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 4, 0x0020edad);
5478c2ecf20Sopenharmony_ci	} else {
5488c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0, 0xa7a506a0);
5498c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 1, 0xa1e6a6e7);
5508c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 2, 0xa2e081e1);
5518c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 3, 0xe9a982e2);
5528c2ecf20Sopenharmony_ci		gmu_write_rscc(gmu, REG_A6XX_RSCC_SEQ_MEM_0_DRV0 + 4, 0x0020e8a8);
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	/* Load PDC sequencer uCode for power up and power down sequence */
5568c2ecf20Sopenharmony_ci	pdc_write(seqptr, REG_A6XX_PDC_GPU_SEQ_MEM_0, 0xfebea1e1);
5578c2ecf20Sopenharmony_ci	pdc_write(seqptr, REG_A6XX_PDC_GPU_SEQ_MEM_0 + 1, 0xa5a4a3a2);
5588c2ecf20Sopenharmony_ci	pdc_write(seqptr, REG_A6XX_PDC_GPU_SEQ_MEM_0 + 2, 0x8382a6e0);
5598c2ecf20Sopenharmony_ci	pdc_write(seqptr, REG_A6XX_PDC_GPU_SEQ_MEM_0 + 3, 0xbce3e284);
5608c2ecf20Sopenharmony_ci	pdc_write(seqptr, REG_A6XX_PDC_GPU_SEQ_MEM_0 + 4, 0x002081fc);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* Set TCS commands used by PDC sequence for low power modes */
5638c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD_ENABLE_BANK, 7);
5648c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD_WAIT_FOR_CMPL_BANK, 0);
5658c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CONTROL, 0);
5668c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_MSGID, 0x10108);
5678c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_ADDR, 0x30010);
5688c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_DATA, 1);
5698c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_MSGID + 4, 0x10108);
5708c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_ADDR + 4, 0x30000);
5718c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_DATA + 4, 0x0);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_MSGID + 8, 0x10108);
5748c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_ADDR + 8, pdc_address_offset);
5758c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS1_CMD0_DATA + 8, 0x0);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD_ENABLE_BANK, 7);
5788c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD_WAIT_FOR_CMPL_BANK, 0);
5798c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CONTROL, 0);
5808c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_MSGID, 0x10108);
5818c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_ADDR, 0x30010);
5828c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_DATA, 2);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_MSGID + 4, 0x10108);
5858c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_ADDR + 4, 0x30000);
5868c2ecf20Sopenharmony_ci	if (adreno_is_a618(adreno_gpu) || adreno_is_a650(adreno_gpu))
5878c2ecf20Sopenharmony_ci		pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_DATA + 4, 0x2);
5888c2ecf20Sopenharmony_ci	else
5898c2ecf20Sopenharmony_ci		pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_DATA + 4, 0x3);
5908c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_MSGID + 8, 0x10108);
5918c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_ADDR + 8, pdc_address_offset);
5928c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_TCS3_CMD0_DATA + 8, 0x3);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* Setup GPU PDC */
5958c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_SEQ_START_ADDR, 0);
5968c2ecf20Sopenharmony_ci	pdc_write(pdcptr, REG_A6XX_PDC_GPU_ENABLE_PDC, 0x80000001);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	/* ensure no writes happen before the uCode is fully written */
5998c2ecf20Sopenharmony_ci	wmb();
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cierr:
6028c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(pdcptr))
6038c2ecf20Sopenharmony_ci		iounmap(pdcptr);
6048c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(seqptr))
6058c2ecf20Sopenharmony_ci		iounmap(seqptr);
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci/*
6098c2ecf20Sopenharmony_ci * The lowest 16 bits of this value are the number of XO clock cycles for main
6108c2ecf20Sopenharmony_ci * hysteresis which is set at 0x1680 cycles (300 us).  The higher 16 bits are
6118c2ecf20Sopenharmony_ci * for the shorter hysteresis that happens after main - this is 0xa (.5 us)
6128c2ecf20Sopenharmony_ci */
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci#define GMU_PWR_COL_HYST 0x000a1680
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci/* Set up the idle state for the GMU */
6178c2ecf20Sopenharmony_cistatic void a6xx_gmu_power_config(struct a6xx_gmu *gmu)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	/* Disable GMU WB/RB buffer */
6208c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_SYS_BUS_CONFIG, 0x1);
6218c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_ICACHE_CONFIG, 0x1);
6228c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_DCACHE_CONFIG, 0x1);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_PWR_COL_INTER_FRAME_CTRL, 0x9c40400);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	switch (gmu->idle_level) {
6278c2ecf20Sopenharmony_ci	case GMU_IDLE_STATE_IFPC:
6288c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_PWR_COL_INTER_FRAME_HYST,
6298c2ecf20Sopenharmony_ci			GMU_PWR_COL_HYST);
6308c2ecf20Sopenharmony_ci		gmu_rmw(gmu, REG_A6XX_GMU_PWR_COL_INTER_FRAME_CTRL, 0,
6318c2ecf20Sopenharmony_ci			A6XX_GMU_PWR_COL_INTER_FRAME_CTRL_IFPC_ENABLE |
6328c2ecf20Sopenharmony_ci			A6XX_GMU_PWR_COL_INTER_FRAME_CTRL_HM_POWER_COLLAPSE_ENABLE);
6338c2ecf20Sopenharmony_ci		fallthrough;
6348c2ecf20Sopenharmony_ci	case GMU_IDLE_STATE_SPTP:
6358c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_PWR_COL_SPTPRAC_HYST,
6368c2ecf20Sopenharmony_ci			GMU_PWR_COL_HYST);
6378c2ecf20Sopenharmony_ci		gmu_rmw(gmu, REG_A6XX_GMU_PWR_COL_INTER_FRAME_CTRL, 0,
6388c2ecf20Sopenharmony_ci			A6XX_GMU_PWR_COL_INTER_FRAME_CTRL_IFPC_ENABLE |
6398c2ecf20Sopenharmony_ci			A6XX_GMU_PWR_COL_INTER_FRAME_CTRL_SPTPRAC_POWER_CONTROL_ENABLE);
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	/* Enable RPMh GPU client */
6438c2ecf20Sopenharmony_ci	gmu_rmw(gmu, REG_A6XX_GMU_RPMH_CTRL, 0,
6448c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_RPMH_INTERFACE_ENABLE |
6458c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_LLC_VOTE_ENABLE |
6468c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_DDR_VOTE_ENABLE |
6478c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_MX_VOTE_ENABLE |
6488c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_CX_VOTE_ENABLE |
6498c2ecf20Sopenharmony_ci		A6XX_GMU_RPMH_CTRL_GFX_VOTE_ENABLE);
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistruct block_header {
6538c2ecf20Sopenharmony_ci	u32 addr;
6548c2ecf20Sopenharmony_ci	u32 size;
6558c2ecf20Sopenharmony_ci	u32 type;
6568c2ecf20Sopenharmony_ci	u32 value;
6578c2ecf20Sopenharmony_ci	u32 data[];
6588c2ecf20Sopenharmony_ci};
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci/* this should be a general kernel helper */
6618c2ecf20Sopenharmony_cistatic int in_range(u32 addr, u32 start, u32 size)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	return addr >= start && addr < start + size;
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic bool fw_block_mem(struct a6xx_gmu_bo *bo, const struct block_header *blk)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	if (!in_range(blk->addr, bo->iova, bo->size))
6698c2ecf20Sopenharmony_ci		return false;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	memcpy(bo->virt + blk->addr - bo->iova, blk->data, blk->size);
6728c2ecf20Sopenharmony_ci	return true;
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic int a6xx_gmu_fw_load(struct a6xx_gmu *gmu)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
6788c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
6798c2ecf20Sopenharmony_ci	const struct firmware *fw_image = adreno_gpu->fw[ADRENO_FW_GMU];
6808c2ecf20Sopenharmony_ci	const struct block_header *blk;
6818c2ecf20Sopenharmony_ci	u32 reg_offset;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	u32 itcm_base = 0x00000000;
6848c2ecf20Sopenharmony_ci	u32 dtcm_base = 0x00040000;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	if (adreno_is_a650(adreno_gpu))
6878c2ecf20Sopenharmony_ci		dtcm_base = 0x10004000;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	if (gmu->legacy) {
6908c2ecf20Sopenharmony_ci		/* Sanity check the size of the firmware that was loaded */
6918c2ecf20Sopenharmony_ci		if (fw_image->size > 0x8000) {
6928c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(gmu->dev,
6938c2ecf20Sopenharmony_ci				"GMU firmware is bigger than the available region\n");
6948c2ecf20Sopenharmony_ci			return -EINVAL;
6958c2ecf20Sopenharmony_ci		}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci		gmu_write_bulk(gmu, REG_A6XX_GMU_CM3_ITCM_START,
6988c2ecf20Sopenharmony_ci			       (u32*) fw_image->data, fw_image->size);
6998c2ecf20Sopenharmony_ci		return 0;
7008c2ecf20Sopenharmony_ci	}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	for (blk = (const struct block_header *) fw_image->data;
7048c2ecf20Sopenharmony_ci	     (const u8*) blk < fw_image->data + fw_image->size;
7058c2ecf20Sopenharmony_ci	     blk = (const struct block_header *) &blk->data[blk->size >> 2]) {
7068c2ecf20Sopenharmony_ci		if (blk->size == 0)
7078c2ecf20Sopenharmony_ci			continue;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		if (in_range(blk->addr, itcm_base, SZ_16K)) {
7108c2ecf20Sopenharmony_ci			reg_offset = (blk->addr - itcm_base) >> 2;
7118c2ecf20Sopenharmony_ci			gmu_write_bulk(gmu,
7128c2ecf20Sopenharmony_ci				REG_A6XX_GMU_CM3_ITCM_START + reg_offset,
7138c2ecf20Sopenharmony_ci				blk->data, blk->size);
7148c2ecf20Sopenharmony_ci		} else if (in_range(blk->addr, dtcm_base, SZ_16K)) {
7158c2ecf20Sopenharmony_ci			reg_offset = (blk->addr - dtcm_base) >> 2;
7168c2ecf20Sopenharmony_ci			gmu_write_bulk(gmu,
7178c2ecf20Sopenharmony_ci				REG_A6XX_GMU_CM3_DTCM_START + reg_offset,
7188c2ecf20Sopenharmony_ci				blk->data, blk->size);
7198c2ecf20Sopenharmony_ci		} else if (!fw_block_mem(&gmu->icache, blk) &&
7208c2ecf20Sopenharmony_ci			   !fw_block_mem(&gmu->dcache, blk) &&
7218c2ecf20Sopenharmony_ci			   !fw_block_mem(&gmu->dummy, blk)) {
7228c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(gmu->dev,
7238c2ecf20Sopenharmony_ci				"failed to match fw block (addr=%.8x size=%d data[0]=%.8x)\n",
7248c2ecf20Sopenharmony_ci				blk->addr, blk->size, blk->data[0]);
7258c2ecf20Sopenharmony_ci		}
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	return 0;
7298c2ecf20Sopenharmony_ci}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic int a6xx_gmu_fw_start(struct a6xx_gmu *gmu, unsigned int state)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	static bool rpmh_init;
7348c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
7358c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
7368c2ecf20Sopenharmony_ci	int ret;
7378c2ecf20Sopenharmony_ci	u32 chipid;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	if (adreno_is_a650(adreno_gpu))
7408c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GPU_GMU_CX_GMU_CX_FAL_INTF, 1);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	if (state == GMU_WARM_BOOT) {
7438c2ecf20Sopenharmony_ci		ret = a6xx_rpmh_start(gmu);
7448c2ecf20Sopenharmony_ci		if (ret)
7458c2ecf20Sopenharmony_ci			return ret;
7468c2ecf20Sopenharmony_ci	} else {
7478c2ecf20Sopenharmony_ci		if (WARN(!adreno_gpu->fw[ADRENO_FW_GMU],
7488c2ecf20Sopenharmony_ci			"GMU firmware is not loaded\n"))
7498c2ecf20Sopenharmony_ci			return -ENOENT;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		/* Turn on register retention */
7528c2ecf20Sopenharmony_ci		gmu_write(gmu, REG_A6XX_GMU_GENERAL_7, 1);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci		/* We only need to load the RPMh microcode once */
7558c2ecf20Sopenharmony_ci		if (!rpmh_init) {
7568c2ecf20Sopenharmony_ci			a6xx_gmu_rpmh_init(gmu);
7578c2ecf20Sopenharmony_ci			rpmh_init = true;
7588c2ecf20Sopenharmony_ci		} else {
7598c2ecf20Sopenharmony_ci			ret = a6xx_rpmh_start(gmu);
7608c2ecf20Sopenharmony_ci			if (ret)
7618c2ecf20Sopenharmony_ci				return ret;
7628c2ecf20Sopenharmony_ci		}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		ret = a6xx_gmu_fw_load(gmu);
7658c2ecf20Sopenharmony_ci		if (ret)
7668c2ecf20Sopenharmony_ci			return ret;
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CM3_FW_INIT_RESULT, 0);
7708c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_CM3_BOOT_CONFIG, 0x02);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	/* Write the iova of the HFI table */
7738c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_HFI_QTBL_ADDR, gmu->hfi.iova);
7748c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_HFI_QTBL_INFO, 1);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AHB_FENCE_RANGE_0,
7778c2ecf20Sopenharmony_ci		(1 << 31) | (0xa << 18) | (0xa0));
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	chipid = adreno_gpu->rev.core << 24;
7808c2ecf20Sopenharmony_ci	chipid |= adreno_gpu->rev.major << 16;
7818c2ecf20Sopenharmony_ci	chipid |= adreno_gpu->rev.minor << 12;
7828c2ecf20Sopenharmony_ci	chipid |= adreno_gpu->rev.patchid << 8;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_HFI_SFR_ADDR, chipid);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GPU_GMU_CX_GMU_PWR_COL_CP_MSG,
7878c2ecf20Sopenharmony_ci		  gmu->log.iova | (gmu->log.size / SZ_4K - 1));
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* Set up the lowest idle level on the GMU */
7908c2ecf20Sopenharmony_ci	a6xx_gmu_power_config(gmu);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	ret = a6xx_gmu_start(gmu);
7938c2ecf20Sopenharmony_ci	if (ret)
7948c2ecf20Sopenharmony_ci		return ret;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	if (gmu->legacy) {
7978c2ecf20Sopenharmony_ci		ret = a6xx_gmu_gfx_rail_on(gmu);
7988c2ecf20Sopenharmony_ci		if (ret)
7998c2ecf20Sopenharmony_ci			return ret;
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	/* Enable SPTP_PC if the CPU is responsible for it */
8038c2ecf20Sopenharmony_ci	if (gmu->idle_level < GMU_IDLE_STATE_SPTP) {
8048c2ecf20Sopenharmony_ci		ret = a6xx_sptprac_enable(gmu);
8058c2ecf20Sopenharmony_ci		if (ret)
8068c2ecf20Sopenharmony_ci			return ret;
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	ret = a6xx_gmu_hfi_start(gmu);
8108c2ecf20Sopenharmony_ci	if (ret)
8118c2ecf20Sopenharmony_ci		return ret;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	/* FIXME: Do we need this wmb() here? */
8148c2ecf20Sopenharmony_ci	wmb();
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return 0;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci#define A6XX_HFI_IRQ_MASK \
8208c2ecf20Sopenharmony_ci	(A6XX_GMU_GMU2HOST_INTR_INFO_CM3_FAULT)
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci#define A6XX_GMU_IRQ_MASK \
8238c2ecf20Sopenharmony_ci	(A6XX_GMU_AO_HOST_INTERRUPT_STATUS_WDOG_BITE | \
8248c2ecf20Sopenharmony_ci	 A6XX_GMU_AO_HOST_INTERRUPT_STATUS_HOST_AHB_BUS_ERROR | \
8258c2ecf20Sopenharmony_ci	 A6XX_GMU_AO_HOST_INTERRUPT_STATUS_FENCE_ERR)
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic void a6xx_gmu_irq_disable(struct a6xx_gmu *gmu)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	disable_irq(gmu->gmu_irq);
8308c2ecf20Sopenharmony_ci	disable_irq(gmu->hfi_irq);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AO_HOST_INTERRUPT_MASK, ~0);
8338c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_MASK, ~0);
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic void a6xx_gmu_rpmh_off(struct a6xx_gmu *gmu)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	u32 val;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	/* Make sure there are no outstanding RPMh votes */
8418c2ecf20Sopenharmony_ci	gmu_poll_timeout_rscc(gmu, REG_A6XX_RSCC_TCS0_DRV0_STATUS, val,
8428c2ecf20Sopenharmony_ci		(val & 1), 100, 10000);
8438c2ecf20Sopenharmony_ci	gmu_poll_timeout_rscc(gmu, REG_A6XX_RSCC_TCS1_DRV0_STATUS, val,
8448c2ecf20Sopenharmony_ci		(val & 1), 100, 10000);
8458c2ecf20Sopenharmony_ci	gmu_poll_timeout_rscc(gmu, REG_A6XX_RSCC_TCS2_DRV0_STATUS, val,
8468c2ecf20Sopenharmony_ci		(val & 1), 100, 10000);
8478c2ecf20Sopenharmony_ci	gmu_poll_timeout_rscc(gmu, REG_A6XX_RSCC_TCS3_DRV0_STATUS, val,
8488c2ecf20Sopenharmony_ci		(val & 1), 100, 1000);
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci/* Force the GMU off in case it isn't responsive */
8528c2ecf20Sopenharmony_cistatic void a6xx_gmu_force_off(struct a6xx_gmu *gmu)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	/* Flush all the queues */
8558c2ecf20Sopenharmony_ci	a6xx_hfi_stop(gmu);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	/* Stop the interrupts */
8588c2ecf20Sopenharmony_ci	a6xx_gmu_irq_disable(gmu);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	/* Force off SPTP in case the GMU is managing it */
8618c2ecf20Sopenharmony_ci	a6xx_sptprac_disable(gmu);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	/* Make sure there are no outstanding RPMh votes */
8648c2ecf20Sopenharmony_ci	a6xx_gmu_rpmh_off(gmu);
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_cistatic void a6xx_gmu_set_initial_freq(struct msm_gpu *gpu, struct a6xx_gmu *gmu)
8688c2ecf20Sopenharmony_ci{
8698c2ecf20Sopenharmony_ci	struct dev_pm_opp *gpu_opp;
8708c2ecf20Sopenharmony_ci	unsigned long gpu_freq = gmu->gpu_freqs[gmu->current_perf_index];
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	gpu_opp = dev_pm_opp_find_freq_exact(&gpu->pdev->dev, gpu_freq, true);
8738c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(gpu_opp))
8748c2ecf20Sopenharmony_ci		return;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	gmu->freq = 0; /* so a6xx_gmu_set_freq() doesn't exit early */
8778c2ecf20Sopenharmony_ci	a6xx_gmu_set_freq(gpu, gpu_opp);
8788c2ecf20Sopenharmony_ci	dev_pm_opp_put(gpu_opp);
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_cistatic void a6xx_gmu_set_initial_bw(struct msm_gpu *gpu, struct a6xx_gmu *gmu)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct dev_pm_opp *gpu_opp;
8848c2ecf20Sopenharmony_ci	unsigned long gpu_freq = gmu->gpu_freqs[gmu->current_perf_index];
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	gpu_opp = dev_pm_opp_find_freq_exact(&gpu->pdev->dev, gpu_freq, true);
8878c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(gpu_opp))
8888c2ecf20Sopenharmony_ci		return;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	dev_pm_opp_set_bw(&gpu->pdev->dev, gpu_opp);
8918c2ecf20Sopenharmony_ci	dev_pm_opp_put(gpu_opp);
8928c2ecf20Sopenharmony_ci}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ciint a6xx_gmu_resume(struct a6xx_gpu *a6xx_gpu)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
8978c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &adreno_gpu->base;
8988c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
8998c2ecf20Sopenharmony_ci	int status, ret;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	if (WARN(!gmu->initialized, "The GMU is not set up yet\n"))
9028c2ecf20Sopenharmony_ci		return 0;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	gmu->hung = false;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	/* Turn on the resources */
9078c2ecf20Sopenharmony_ci	pm_runtime_get_sync(gmu->dev);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	/*
9108c2ecf20Sopenharmony_ci	 * "enable" the GX power domain which won't actually do anything but it
9118c2ecf20Sopenharmony_ci	 * will make sure that the refcounting is correct in case we need to
9128c2ecf20Sopenharmony_ci	 * bring down the GX after a GMU failure
9138c2ecf20Sopenharmony_ci	 */
9148c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(gmu->gxpd))
9158c2ecf20Sopenharmony_ci		pm_runtime_get_sync(gmu->gxpd);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	/* Use a known rate to bring up the GMU */
9188c2ecf20Sopenharmony_ci	clk_set_rate(gmu->core_clk, 200000000);
9198c2ecf20Sopenharmony_ci	ret = clk_bulk_prepare_enable(gmu->nr_clocks, gmu->clocks);
9208c2ecf20Sopenharmony_ci	if (ret) {
9218c2ecf20Sopenharmony_ci		pm_runtime_put(gmu->gxpd);
9228c2ecf20Sopenharmony_ci		pm_runtime_put(gmu->dev);
9238c2ecf20Sopenharmony_ci		return ret;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	/* Set the bus quota to a reasonable value for boot */
9278c2ecf20Sopenharmony_ci	a6xx_gmu_set_initial_bw(gpu, gmu);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* Enable the GMU interrupt */
9308c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AO_HOST_INTERRUPT_CLR, ~0);
9318c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_AO_HOST_INTERRUPT_MASK, ~A6XX_GMU_IRQ_MASK);
9328c2ecf20Sopenharmony_ci	enable_irq(gmu->gmu_irq);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/* Check to see if we are doing a cold or warm boot */
9358c2ecf20Sopenharmony_ci	status = gmu_read(gmu, REG_A6XX_GMU_GENERAL_7) == 1 ?
9368c2ecf20Sopenharmony_ci		GMU_WARM_BOOT : GMU_COLD_BOOT;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	/*
9398c2ecf20Sopenharmony_ci	 * Warm boot path does not work on newer GPUs
9408c2ecf20Sopenharmony_ci	 * Presumably this is because icache/dcache regions must be restored
9418c2ecf20Sopenharmony_ci	 */
9428c2ecf20Sopenharmony_ci	if (!gmu->legacy)
9438c2ecf20Sopenharmony_ci		status = GMU_COLD_BOOT;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	ret = a6xx_gmu_fw_start(gmu, status);
9468c2ecf20Sopenharmony_ci	if (ret)
9478c2ecf20Sopenharmony_ci		goto out;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	ret = a6xx_hfi_start(gmu, status);
9508c2ecf20Sopenharmony_ci	if (ret)
9518c2ecf20Sopenharmony_ci		goto out;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	/*
9548c2ecf20Sopenharmony_ci	 * Turn on the GMU firmware fault interrupt after we know the boot
9558c2ecf20Sopenharmony_ci	 * sequence is successful
9568c2ecf20Sopenharmony_ci	 */
9578c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_CLR, ~0);
9588c2ecf20Sopenharmony_ci	gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_MASK, ~A6XX_HFI_IRQ_MASK);
9598c2ecf20Sopenharmony_ci	enable_irq(gmu->hfi_irq);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	/* Set the GPU to the current freq */
9628c2ecf20Sopenharmony_ci	a6xx_gmu_set_initial_freq(gpu, gmu);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ciout:
9658c2ecf20Sopenharmony_ci	/* On failure, shut down the GMU to leave it in a good state */
9668c2ecf20Sopenharmony_ci	if (ret) {
9678c2ecf20Sopenharmony_ci		disable_irq(gmu->gmu_irq);
9688c2ecf20Sopenharmony_ci		a6xx_rpmh_stop(gmu);
9698c2ecf20Sopenharmony_ci		pm_runtime_put(gmu->gxpd);
9708c2ecf20Sopenharmony_ci		pm_runtime_put(gmu->dev);
9718c2ecf20Sopenharmony_ci	}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	return ret;
9748c2ecf20Sopenharmony_ci}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_cibool a6xx_gmu_isidle(struct a6xx_gmu *gmu)
9778c2ecf20Sopenharmony_ci{
9788c2ecf20Sopenharmony_ci	u32 reg;
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	if (!gmu->initialized)
9818c2ecf20Sopenharmony_ci		return true;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	reg = gmu_read(gmu, REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	if (reg &  A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS_GPUBUSYIGNAHB)
9868c2ecf20Sopenharmony_ci		return false;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	return true;
9898c2ecf20Sopenharmony_ci}
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci#define GBIF_CLIENT_HALT_MASK             BIT(0)
9928c2ecf20Sopenharmony_ci#define GBIF_ARB_HALT_MASK                BIT(1)
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic void a6xx_bus_clear_pending_transactions(struct adreno_gpu *adreno_gpu)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &adreno_gpu->base;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	if (!a6xx_has_gbif(adreno_gpu)) {
9998c2ecf20Sopenharmony_ci		gpu_write(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL0, 0xf);
10008c2ecf20Sopenharmony_ci		spin_until((gpu_read(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL1) &
10018c2ecf20Sopenharmony_ci								0xf) == 0xf);
10028c2ecf20Sopenharmony_ci		gpu_write(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL0, 0);
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci		return;
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	/* Halt new client requests on GBIF */
10088c2ecf20Sopenharmony_ci	gpu_write(gpu, REG_A6XX_GBIF_HALT, GBIF_CLIENT_HALT_MASK);
10098c2ecf20Sopenharmony_ci	spin_until((gpu_read(gpu, REG_A6XX_GBIF_HALT_ACK) &
10108c2ecf20Sopenharmony_ci			(GBIF_CLIENT_HALT_MASK)) == GBIF_CLIENT_HALT_MASK);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	/* Halt all AXI requests on GBIF */
10138c2ecf20Sopenharmony_ci	gpu_write(gpu, REG_A6XX_GBIF_HALT, GBIF_ARB_HALT_MASK);
10148c2ecf20Sopenharmony_ci	spin_until((gpu_read(gpu,  REG_A6XX_GBIF_HALT_ACK) &
10158c2ecf20Sopenharmony_ci			(GBIF_ARB_HALT_MASK)) == GBIF_ARB_HALT_MASK);
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	/* The GBIF halt needs to be explicitly cleared */
10188c2ecf20Sopenharmony_ci	gpu_write(gpu, REG_A6XX_GBIF_HALT, 0x0);
10198c2ecf20Sopenharmony_ci}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci/* Gracefully try to shut down the GMU and by extension the GPU */
10228c2ecf20Sopenharmony_cistatic void a6xx_gmu_shutdown(struct a6xx_gmu *gmu)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
10258c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
10268c2ecf20Sopenharmony_ci	u32 val;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	/*
10298c2ecf20Sopenharmony_ci	 * The GMU may still be in slumber unless the GPU started so check and
10308c2ecf20Sopenharmony_ci	 * skip putting it back into slumber if so
10318c2ecf20Sopenharmony_ci	 */
10328c2ecf20Sopenharmony_ci	val = gmu_read(gmu, REG_A6XX_GPU_GMU_CX_GMU_RPMH_POWER_STATE);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	if (val != 0xf) {
10358c2ecf20Sopenharmony_ci		int ret = a6xx_gmu_wait_for_idle(gmu);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci		/* If the GMU isn't responding assume it is hung */
10388c2ecf20Sopenharmony_ci		if (ret) {
10398c2ecf20Sopenharmony_ci			a6xx_gmu_force_off(gmu);
10408c2ecf20Sopenharmony_ci			return;
10418c2ecf20Sopenharmony_ci		}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci		a6xx_bus_clear_pending_transactions(adreno_gpu);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci		/* tell the GMU we want to slumber */
10468c2ecf20Sopenharmony_ci		a6xx_gmu_notify_slumber(gmu);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci		ret = gmu_poll_timeout(gmu,
10498c2ecf20Sopenharmony_ci			REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS, val,
10508c2ecf20Sopenharmony_ci			!(val & A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS_GPUBUSYIGNAHB),
10518c2ecf20Sopenharmony_ci			100, 10000);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci		/*
10548c2ecf20Sopenharmony_ci		 * Let the user know we failed to slumber but don't worry too
10558c2ecf20Sopenharmony_ci		 * much because we are powering down anyway
10568c2ecf20Sopenharmony_ci		 */
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci		if (ret)
10598c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(gmu->dev,
10608c2ecf20Sopenharmony_ci				"Unable to slumber GMU: status = 0%x/0%x\n",
10618c2ecf20Sopenharmony_ci				gmu_read(gmu,
10628c2ecf20Sopenharmony_ci					REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS),
10638c2ecf20Sopenharmony_ci				gmu_read(gmu,
10648c2ecf20Sopenharmony_ci					REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS2));
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	/* Turn off HFI */
10688c2ecf20Sopenharmony_ci	a6xx_hfi_stop(gmu);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	/* Stop the interrupts and mask the hardware */
10718c2ecf20Sopenharmony_ci	a6xx_gmu_irq_disable(gmu);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	/* Tell RPMh to power off the GPU */
10748c2ecf20Sopenharmony_ci	a6xx_rpmh_stop(gmu);
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ciint a6xx_gmu_stop(struct a6xx_gpu *a6xx_gpu)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
10818c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &a6xx_gpu->base.base;
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	if (!pm_runtime_active(gmu->dev))
10848c2ecf20Sopenharmony_ci		return 0;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	/*
10878c2ecf20Sopenharmony_ci	 * Force the GMU off if we detected a hang, otherwise try to shut it
10888c2ecf20Sopenharmony_ci	 * down gracefully
10898c2ecf20Sopenharmony_ci	 */
10908c2ecf20Sopenharmony_ci	if (gmu->hung)
10918c2ecf20Sopenharmony_ci		a6xx_gmu_force_off(gmu);
10928c2ecf20Sopenharmony_ci	else
10938c2ecf20Sopenharmony_ci		a6xx_gmu_shutdown(gmu);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	/* Remove the bus vote */
10968c2ecf20Sopenharmony_ci	dev_pm_opp_set_bw(&gpu->pdev->dev, NULL);
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	/*
10998c2ecf20Sopenharmony_ci	 * Make sure the GX domain is off before turning off the GMU (CX)
11008c2ecf20Sopenharmony_ci	 * domain. Usually the GMU does this but only if the shutdown sequence
11018c2ecf20Sopenharmony_ci	 * was successful
11028c2ecf20Sopenharmony_ci	 */
11038c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(gmu->gxpd))
11048c2ecf20Sopenharmony_ci		pm_runtime_put_sync(gmu->gxpd);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	clk_bulk_disable_unprepare(gmu->nr_clocks, gmu->clocks);
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	pm_runtime_put_sync(gmu->dev);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	return 0;
11118c2ecf20Sopenharmony_ci}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_cistatic void a6xx_gmu_memory_free(struct a6xx_gmu *gmu)
11148c2ecf20Sopenharmony_ci{
11158c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->hfi.obj, gmu->aspace, false);
11168c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->debug.obj, gmu->aspace, false);
11178c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->icache.obj, gmu->aspace, false);
11188c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->dcache.obj, gmu->aspace, false);
11198c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->dummy.obj, gmu->aspace, false);
11208c2ecf20Sopenharmony_ci	msm_gem_kernel_put(gmu->log.obj, gmu->aspace, false);
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	gmu->aspace->mmu->funcs->detach(gmu->aspace->mmu);
11238c2ecf20Sopenharmony_ci	msm_gem_address_space_put(gmu->aspace);
11248c2ecf20Sopenharmony_ci}
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_cistatic int a6xx_gmu_memory_alloc(struct a6xx_gmu *gmu, struct a6xx_gmu_bo *bo,
11278c2ecf20Sopenharmony_ci		size_t size, u64 iova)
11288c2ecf20Sopenharmony_ci{
11298c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
11308c2ecf20Sopenharmony_ci	struct drm_device *dev = a6xx_gpu->base.base.dev;
11318c2ecf20Sopenharmony_ci	uint32_t flags = MSM_BO_WC;
11328c2ecf20Sopenharmony_ci	u64 range_start, range_end;
11338c2ecf20Sopenharmony_ci	int ret;
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	size = PAGE_ALIGN(size);
11368c2ecf20Sopenharmony_ci	if (!iova) {
11378c2ecf20Sopenharmony_ci		/* no fixed address - use GMU's uncached range */
11388c2ecf20Sopenharmony_ci		range_start = 0x60000000 + PAGE_SIZE; /* skip dummy page */
11398c2ecf20Sopenharmony_ci		range_end = 0x80000000;
11408c2ecf20Sopenharmony_ci	} else {
11418c2ecf20Sopenharmony_ci		/* range for fixed address */
11428c2ecf20Sopenharmony_ci		range_start = iova;
11438c2ecf20Sopenharmony_ci		range_end = iova + size;
11448c2ecf20Sopenharmony_ci		/* use IOMMU_PRIV for icache/dcache */
11458c2ecf20Sopenharmony_ci		flags |= MSM_BO_MAP_PRIV;
11468c2ecf20Sopenharmony_ci	}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	bo->obj = msm_gem_new(dev, size, flags);
11498c2ecf20Sopenharmony_ci	if (IS_ERR(bo->obj))
11508c2ecf20Sopenharmony_ci		return PTR_ERR(bo->obj);
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	ret = msm_gem_get_and_pin_iova_range(bo->obj, gmu->aspace, &bo->iova,
11538c2ecf20Sopenharmony_ci		range_start >> PAGE_SHIFT, range_end >> PAGE_SHIFT);
11548c2ecf20Sopenharmony_ci	if (ret) {
11558c2ecf20Sopenharmony_ci		drm_gem_object_put(bo->obj);
11568c2ecf20Sopenharmony_ci		return ret;
11578c2ecf20Sopenharmony_ci	}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	bo->virt = msm_gem_get_vaddr(bo->obj);
11608c2ecf20Sopenharmony_ci	bo->size = size;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	return 0;
11638c2ecf20Sopenharmony_ci}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_cistatic int a6xx_gmu_memory_probe(struct a6xx_gmu *gmu)
11668c2ecf20Sopenharmony_ci{
11678c2ecf20Sopenharmony_ci	struct iommu_domain *domain;
11688c2ecf20Sopenharmony_ci	struct msm_mmu *mmu;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	domain = iommu_domain_alloc(&platform_bus_type);
11718c2ecf20Sopenharmony_ci	if (!domain)
11728c2ecf20Sopenharmony_ci		return -ENODEV;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	mmu = msm_iommu_new(gmu->dev, domain);
11758c2ecf20Sopenharmony_ci	gmu->aspace = msm_gem_address_space_create(mmu, "gmu", 0x0, 0x80000000);
11768c2ecf20Sopenharmony_ci	if (IS_ERR(gmu->aspace)) {
11778c2ecf20Sopenharmony_ci		iommu_domain_free(domain);
11788c2ecf20Sopenharmony_ci		return PTR_ERR(gmu->aspace);
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	return 0;
11828c2ecf20Sopenharmony_ci}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci/* Return the 'arc-level' for the given frequency */
11858c2ecf20Sopenharmony_cistatic unsigned int a6xx_gmu_get_arc_level(struct device *dev,
11868c2ecf20Sopenharmony_ci					   unsigned long freq)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	struct dev_pm_opp *opp;
11898c2ecf20Sopenharmony_ci	unsigned int val;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	if (!freq)
11928c2ecf20Sopenharmony_ci		return 0;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	opp = dev_pm_opp_find_freq_exact(dev, freq, true);
11958c2ecf20Sopenharmony_ci	if (IS_ERR(opp))
11968c2ecf20Sopenharmony_ci		return 0;
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	val = dev_pm_opp_get_level(opp);
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	dev_pm_opp_put(opp);
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	return val;
12038c2ecf20Sopenharmony_ci}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_cistatic int a6xx_gmu_rpmh_arc_votes_init(struct device *dev, u32 *votes,
12068c2ecf20Sopenharmony_ci		unsigned long *freqs, int freqs_count, const char *id)
12078c2ecf20Sopenharmony_ci{
12088c2ecf20Sopenharmony_ci	int i, j;
12098c2ecf20Sopenharmony_ci	const u16 *pri, *sec;
12108c2ecf20Sopenharmony_ci	size_t pri_count, sec_count;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	pri = cmd_db_read_aux_data(id, &pri_count);
12138c2ecf20Sopenharmony_ci	if (IS_ERR(pri))
12148c2ecf20Sopenharmony_ci		return PTR_ERR(pri);
12158c2ecf20Sopenharmony_ci	/*
12168c2ecf20Sopenharmony_ci	 * The data comes back as an array of unsigned shorts so adjust the
12178c2ecf20Sopenharmony_ci	 * count accordingly
12188c2ecf20Sopenharmony_ci	 */
12198c2ecf20Sopenharmony_ci	pri_count >>= 1;
12208c2ecf20Sopenharmony_ci	if (!pri_count)
12218c2ecf20Sopenharmony_ci		return -EINVAL;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	sec = cmd_db_read_aux_data("mx.lvl", &sec_count);
12248c2ecf20Sopenharmony_ci	if (IS_ERR(sec))
12258c2ecf20Sopenharmony_ci		return PTR_ERR(sec);
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	sec_count >>= 1;
12288c2ecf20Sopenharmony_ci	if (!sec_count)
12298c2ecf20Sopenharmony_ci		return -EINVAL;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	/* Construct a vote for each frequency */
12328c2ecf20Sopenharmony_ci	for (i = 0; i < freqs_count; i++) {
12338c2ecf20Sopenharmony_ci		u8 pindex = 0, sindex = 0;
12348c2ecf20Sopenharmony_ci		unsigned int level = a6xx_gmu_get_arc_level(dev, freqs[i]);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci		/* Get the primary index that matches the arc level */
12378c2ecf20Sopenharmony_ci		for (j = 0; j < pri_count; j++) {
12388c2ecf20Sopenharmony_ci			if (pri[j] >= level) {
12398c2ecf20Sopenharmony_ci				pindex = j;
12408c2ecf20Sopenharmony_ci				break;
12418c2ecf20Sopenharmony_ci			}
12428c2ecf20Sopenharmony_ci		}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci		if (j == pri_count) {
12458c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(dev,
12468c2ecf20Sopenharmony_ci				      "Level %u not found in the RPMh list\n",
12478c2ecf20Sopenharmony_ci				      level);
12488c2ecf20Sopenharmony_ci			DRM_DEV_ERROR(dev, "Available levels:\n");
12498c2ecf20Sopenharmony_ci			for (j = 0; j < pri_count; j++)
12508c2ecf20Sopenharmony_ci				DRM_DEV_ERROR(dev, "  %u\n", pri[j]);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci			return -EINVAL;
12538c2ecf20Sopenharmony_ci		}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci		/*
12568c2ecf20Sopenharmony_ci		 * Look for a level in in the secondary list that matches. If
12578c2ecf20Sopenharmony_ci		 * nothing fits, use the maximum non zero vote
12588c2ecf20Sopenharmony_ci		 */
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci		for (j = 0; j < sec_count; j++) {
12618c2ecf20Sopenharmony_ci			if (sec[j] >= level) {
12628c2ecf20Sopenharmony_ci				sindex = j;
12638c2ecf20Sopenharmony_ci				break;
12648c2ecf20Sopenharmony_ci			} else if (sec[j]) {
12658c2ecf20Sopenharmony_ci				sindex = j;
12668c2ecf20Sopenharmony_ci			}
12678c2ecf20Sopenharmony_ci		}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci		/* Construct the vote */
12708c2ecf20Sopenharmony_ci		votes[i] = ((pri[pindex] & 0xffff) << 16) |
12718c2ecf20Sopenharmony_ci			(sindex << 8) | pindex;
12728c2ecf20Sopenharmony_ci	}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	return 0;
12758c2ecf20Sopenharmony_ci}
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci/*
12788c2ecf20Sopenharmony_ci * The GMU votes with the RPMh for itself and on behalf of the GPU but we need
12798c2ecf20Sopenharmony_ci * to construct the list of votes on the CPU and send it over. Query the RPMh
12808c2ecf20Sopenharmony_ci * voltage levels and build the votes
12818c2ecf20Sopenharmony_ci */
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_cistatic int a6xx_gmu_rpmh_votes_init(struct a6xx_gmu *gmu)
12848c2ecf20Sopenharmony_ci{
12858c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
12868c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
12878c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &adreno_gpu->base;
12888c2ecf20Sopenharmony_ci	int ret;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	/* Build the GX votes */
12918c2ecf20Sopenharmony_ci	ret = a6xx_gmu_rpmh_arc_votes_init(&gpu->pdev->dev, gmu->gx_arc_votes,
12928c2ecf20Sopenharmony_ci		gmu->gpu_freqs, gmu->nr_gpu_freqs, "gfx.lvl");
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci	/* Build the CX votes */
12958c2ecf20Sopenharmony_ci	ret |= a6xx_gmu_rpmh_arc_votes_init(gmu->dev, gmu->cx_arc_votes,
12968c2ecf20Sopenharmony_ci		gmu->gmu_freqs, gmu->nr_gmu_freqs, "cx.lvl");
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	return ret;
12998c2ecf20Sopenharmony_ci}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_cistatic int a6xx_gmu_build_freq_table(struct device *dev, unsigned long *freqs,
13028c2ecf20Sopenharmony_ci		u32 size)
13038c2ecf20Sopenharmony_ci{
13048c2ecf20Sopenharmony_ci	int count = dev_pm_opp_get_opp_count(dev);
13058c2ecf20Sopenharmony_ci	struct dev_pm_opp *opp;
13068c2ecf20Sopenharmony_ci	int i, index = 0;
13078c2ecf20Sopenharmony_ci	unsigned long freq = 1;
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	/*
13108c2ecf20Sopenharmony_ci	 * The OPP table doesn't contain the "off" frequency level so we need to
13118c2ecf20Sopenharmony_ci	 * add 1 to the table size to account for it
13128c2ecf20Sopenharmony_ci	 */
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	if (WARN(count + 1 > size,
13158c2ecf20Sopenharmony_ci		"The GMU frequency table is being truncated\n"))
13168c2ecf20Sopenharmony_ci		count = size - 1;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	/* Set the "off" frequency */
13198c2ecf20Sopenharmony_ci	freqs[index++] = 0;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
13228c2ecf20Sopenharmony_ci		opp = dev_pm_opp_find_freq_ceil(dev, &freq);
13238c2ecf20Sopenharmony_ci		if (IS_ERR(opp))
13248c2ecf20Sopenharmony_ci			break;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci		dev_pm_opp_put(opp);
13278c2ecf20Sopenharmony_ci		freqs[index++] = freq++;
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	return index;
13318c2ecf20Sopenharmony_ci}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_cistatic int a6xx_gmu_pwrlevels_probe(struct a6xx_gmu *gmu)
13348c2ecf20Sopenharmony_ci{
13358c2ecf20Sopenharmony_ci	struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
13368c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
13378c2ecf20Sopenharmony_ci	struct msm_gpu *gpu = &adreno_gpu->base;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	int ret = 0;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	/*
13428c2ecf20Sopenharmony_ci	 * The GMU handles its own frequency switching so build a list of
13438c2ecf20Sopenharmony_ci	 * available frequencies to send during initialization
13448c2ecf20Sopenharmony_ci	 */
13458c2ecf20Sopenharmony_ci	ret = dev_pm_opp_of_add_table(gmu->dev);
13468c2ecf20Sopenharmony_ci	if (ret) {
13478c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(gmu->dev, "Unable to set the OPP table for the GMU\n");
13488c2ecf20Sopenharmony_ci		return ret;
13498c2ecf20Sopenharmony_ci	}
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	gmu->nr_gmu_freqs = a6xx_gmu_build_freq_table(gmu->dev,
13528c2ecf20Sopenharmony_ci		gmu->gmu_freqs, ARRAY_SIZE(gmu->gmu_freqs));
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	/*
13558c2ecf20Sopenharmony_ci	 * The GMU also handles GPU frequency switching so build a list
13568c2ecf20Sopenharmony_ci	 * from the GPU OPP table
13578c2ecf20Sopenharmony_ci	 */
13588c2ecf20Sopenharmony_ci	gmu->nr_gpu_freqs = a6xx_gmu_build_freq_table(&gpu->pdev->dev,
13598c2ecf20Sopenharmony_ci		gmu->gpu_freqs, ARRAY_SIZE(gmu->gpu_freqs));
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	gmu->current_perf_index = gmu->nr_gpu_freqs - 1;
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	/* Build the list of RPMh votes that we'll send to the GMU */
13648c2ecf20Sopenharmony_ci	return a6xx_gmu_rpmh_votes_init(gmu);
13658c2ecf20Sopenharmony_ci}
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_cistatic int a6xx_gmu_clocks_probe(struct a6xx_gmu *gmu)
13688c2ecf20Sopenharmony_ci{
13698c2ecf20Sopenharmony_ci	int ret = devm_clk_bulk_get_all(gmu->dev, &gmu->clocks);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	if (ret < 1)
13728c2ecf20Sopenharmony_ci		return ret;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	gmu->nr_clocks = ret;
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	gmu->core_clk = msm_clk_bulk_get_clock(gmu->clocks,
13778c2ecf20Sopenharmony_ci		gmu->nr_clocks, "gmu");
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	return 0;
13808c2ecf20Sopenharmony_ci}
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_cistatic void __iomem *a6xx_gmu_get_mmio(struct platform_device *pdev,
13838c2ecf20Sopenharmony_ci		const char *name)
13848c2ecf20Sopenharmony_ci{
13858c2ecf20Sopenharmony_ci	void __iomem *ret;
13868c2ecf20Sopenharmony_ci	struct resource *res = platform_get_resource_byname(pdev,
13878c2ecf20Sopenharmony_ci			IORESOURCE_MEM, name);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	if (!res) {
13908c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(&pdev->dev, "Unable to find the %s registers\n", name);
13918c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	ret = ioremap(res->start, resource_size(res));
13958c2ecf20Sopenharmony_ci	if (!ret) {
13968c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(&pdev->dev, "Unable to map the %s registers\n", name);
13978c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
13988c2ecf20Sopenharmony_ci	}
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	return ret;
14018c2ecf20Sopenharmony_ci}
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_cistatic int a6xx_gmu_get_irq(struct a6xx_gmu *gmu, struct platform_device *pdev,
14048c2ecf20Sopenharmony_ci		const char *name, irq_handler_t handler)
14058c2ecf20Sopenharmony_ci{
14068c2ecf20Sopenharmony_ci	int irq, ret;
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	irq = platform_get_irq_byname(pdev, name);
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	ret = request_irq(irq, handler, IRQF_TRIGGER_HIGH, name, gmu);
14118c2ecf20Sopenharmony_ci	if (ret) {
14128c2ecf20Sopenharmony_ci		DRM_DEV_ERROR(&pdev->dev, "Unable to get interrupt %s %d\n",
14138c2ecf20Sopenharmony_ci			      name, ret);
14148c2ecf20Sopenharmony_ci		return ret;
14158c2ecf20Sopenharmony_ci	}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	disable_irq(irq);
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	return irq;
14208c2ecf20Sopenharmony_ci}
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_civoid a6xx_gmu_remove(struct a6xx_gpu *a6xx_gpu)
14238c2ecf20Sopenharmony_ci{
14248c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
14258c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(gmu->dev);
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	if (!gmu->initialized)
14288c2ecf20Sopenharmony_ci		return;
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	pm_runtime_force_suspend(gmu->dev);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(gmu->gxpd)) {
14338c2ecf20Sopenharmony_ci		pm_runtime_disable(gmu->gxpd);
14348c2ecf20Sopenharmony_ci		dev_pm_domain_detach(gmu->gxpd, false);
14358c2ecf20Sopenharmony_ci	}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	iounmap(gmu->mmio);
14388c2ecf20Sopenharmony_ci	if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "rscc"))
14398c2ecf20Sopenharmony_ci		iounmap(gmu->rscc);
14408c2ecf20Sopenharmony_ci	gmu->mmio = NULL;
14418c2ecf20Sopenharmony_ci	gmu->rscc = NULL;
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	a6xx_gmu_memory_free(gmu);
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	free_irq(gmu->gmu_irq, gmu);
14468c2ecf20Sopenharmony_ci	free_irq(gmu->hfi_irq, gmu);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	/* Drop reference taken in of_find_device_by_node */
14498c2ecf20Sopenharmony_ci	put_device(gmu->dev);
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	gmu->initialized = false;
14528c2ecf20Sopenharmony_ci}
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ciint a6xx_gmu_init(struct a6xx_gpu *a6xx_gpu, struct device_node *node)
14558c2ecf20Sopenharmony_ci{
14568c2ecf20Sopenharmony_ci	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
14578c2ecf20Sopenharmony_ci	struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
14588c2ecf20Sopenharmony_ci	struct platform_device *pdev = of_find_device_by_node(node);
14598c2ecf20Sopenharmony_ci	int ret;
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	if (!pdev)
14628c2ecf20Sopenharmony_ci		return -ENODEV;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	gmu->dev = &pdev->dev;
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci	of_dma_configure(gmu->dev, node, true);
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci	/* Fow now, don't do anything fancy until we get our feet under us */
14698c2ecf20Sopenharmony_ci	gmu->idle_level = GMU_IDLE_STATE_ACTIVE;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	pm_runtime_enable(gmu->dev);
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	/* Get the list of clocks */
14748c2ecf20Sopenharmony_ci	ret = a6xx_gmu_clocks_probe(gmu);
14758c2ecf20Sopenharmony_ci	if (ret)
14768c2ecf20Sopenharmony_ci		goto err_put_device;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	ret = a6xx_gmu_memory_probe(gmu);
14798c2ecf20Sopenharmony_ci	if (ret)
14808c2ecf20Sopenharmony_ci		goto err_put_device;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	/* Allocate memory for the GMU dummy page */
14838c2ecf20Sopenharmony_ci	ret = a6xx_gmu_memory_alloc(gmu, &gmu->dummy, SZ_4K, 0x60000000);
14848c2ecf20Sopenharmony_ci	if (ret)
14858c2ecf20Sopenharmony_ci		goto err_memory;
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	if (adreno_is_a650(adreno_gpu)) {
14888c2ecf20Sopenharmony_ci		ret = a6xx_gmu_memory_alloc(gmu, &gmu->icache,
14898c2ecf20Sopenharmony_ci			SZ_16M - SZ_16K, 0x04000);
14908c2ecf20Sopenharmony_ci		if (ret)
14918c2ecf20Sopenharmony_ci			goto err_memory;
14928c2ecf20Sopenharmony_ci	} else if (adreno_is_a640(adreno_gpu)) {
14938c2ecf20Sopenharmony_ci		ret = a6xx_gmu_memory_alloc(gmu, &gmu->icache,
14948c2ecf20Sopenharmony_ci			SZ_256K - SZ_16K, 0x04000);
14958c2ecf20Sopenharmony_ci		if (ret)
14968c2ecf20Sopenharmony_ci			goto err_memory;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci		ret = a6xx_gmu_memory_alloc(gmu, &gmu->dcache,
14998c2ecf20Sopenharmony_ci			SZ_256K - SZ_16K, 0x44000);
15008c2ecf20Sopenharmony_ci		if (ret)
15018c2ecf20Sopenharmony_ci			goto err_memory;
15028c2ecf20Sopenharmony_ci	} else {
15038c2ecf20Sopenharmony_ci		/* HFI v1, has sptprac */
15048c2ecf20Sopenharmony_ci		gmu->legacy = true;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci		/* Allocate memory for the GMU debug region */
15078c2ecf20Sopenharmony_ci		ret = a6xx_gmu_memory_alloc(gmu, &gmu->debug, SZ_16K, 0);
15088c2ecf20Sopenharmony_ci		if (ret)
15098c2ecf20Sopenharmony_ci			goto err_memory;
15108c2ecf20Sopenharmony_ci	}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	/* Allocate memory for for the HFI queues */
15138c2ecf20Sopenharmony_ci	ret = a6xx_gmu_memory_alloc(gmu, &gmu->hfi, SZ_16K, 0);
15148c2ecf20Sopenharmony_ci	if (ret)
15158c2ecf20Sopenharmony_ci		goto err_memory;
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci	/* Allocate memory for the GMU log region */
15188c2ecf20Sopenharmony_ci	ret = a6xx_gmu_memory_alloc(gmu, &gmu->log, SZ_4K, 0);
15198c2ecf20Sopenharmony_ci	if (ret)
15208c2ecf20Sopenharmony_ci		goto err_memory;
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci	/* Map the GMU registers */
15238c2ecf20Sopenharmony_ci	gmu->mmio = a6xx_gmu_get_mmio(pdev, "gmu");
15248c2ecf20Sopenharmony_ci	if (IS_ERR(gmu->mmio)) {
15258c2ecf20Sopenharmony_ci		ret = PTR_ERR(gmu->mmio);
15268c2ecf20Sopenharmony_ci		goto err_memory;
15278c2ecf20Sopenharmony_ci	}
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	if (adreno_is_a650(adreno_gpu)) {
15308c2ecf20Sopenharmony_ci		gmu->rscc = a6xx_gmu_get_mmio(pdev, "rscc");
15318c2ecf20Sopenharmony_ci		if (IS_ERR(gmu->rscc))
15328c2ecf20Sopenharmony_ci			goto err_mmio;
15338c2ecf20Sopenharmony_ci	} else {
15348c2ecf20Sopenharmony_ci		gmu->rscc = gmu->mmio + 0x23000;
15358c2ecf20Sopenharmony_ci	}
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	/* Get the HFI and GMU interrupts */
15388c2ecf20Sopenharmony_ci	gmu->hfi_irq = a6xx_gmu_get_irq(gmu, pdev, "hfi", a6xx_hfi_irq);
15398c2ecf20Sopenharmony_ci	gmu->gmu_irq = a6xx_gmu_get_irq(gmu, pdev, "gmu", a6xx_gmu_irq);
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	if (gmu->hfi_irq < 0 || gmu->gmu_irq < 0)
15428c2ecf20Sopenharmony_ci		goto err_mmio;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	/*
15458c2ecf20Sopenharmony_ci	 * Get a link to the GX power domain to reset the GPU in case of GMU
15468c2ecf20Sopenharmony_ci	 * crash
15478c2ecf20Sopenharmony_ci	 */
15488c2ecf20Sopenharmony_ci	gmu->gxpd = dev_pm_domain_attach_by_name(gmu->dev, "gx");
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	/* Get the power levels for the GMU and GPU */
15518c2ecf20Sopenharmony_ci	a6xx_gmu_pwrlevels_probe(gmu);
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	/* Set up the HFI queues */
15548c2ecf20Sopenharmony_ci	a6xx_hfi_init(gmu);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	gmu->initialized = true;
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	return 0;
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_cierr_mmio:
15618c2ecf20Sopenharmony_ci	iounmap(gmu->mmio);
15628c2ecf20Sopenharmony_ci	if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "rscc"))
15638c2ecf20Sopenharmony_ci		iounmap(gmu->rscc);
15648c2ecf20Sopenharmony_ci	free_irq(gmu->gmu_irq, gmu);
15658c2ecf20Sopenharmony_ci	free_irq(gmu->hfi_irq, gmu);
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	ret = -ENODEV;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_cierr_memory:
15708c2ecf20Sopenharmony_ci	a6xx_gmu_memory_free(gmu);
15718c2ecf20Sopenharmony_cierr_put_device:
15728c2ecf20Sopenharmony_ci	/* Drop reference taken in of_find_device_by_node */
15738c2ecf20Sopenharmony_ci	put_device(gmu->dev);
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	return ret;
15768c2ecf20Sopenharmony_ci}
1577