text A618 SOURCE PART_1_BLOCK_05 5 views



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



	return ret;

}



static int a6xx_first_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	if (test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags)) {

		if (!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

			return a6xx_boot(adreno_dev);



		return 0;

	}



	KGSL_BOOT_MARKER("ADRENO Init");



	ret = a6xx_ringbuffer_init(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_microcode_read(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_init(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_gmu_init(adreno_dev);

	if (ret)

		return ret;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	ret = a6xx_gmu_first_boot(adreno_dev);

	if (ret)

		return ret;



	a6xx_fusa_init(adreno_dev);



	ret = a6xx_gpu_boot(adreno_dev);

	if (ret)

		return ret;



	adreno_get_bus_counters(adreno_dev);



	adreno_dev->cooperative_reset = ADRENO_FEATURE(adreno_dev,

						 ADRENO_COOP_RESET);



	adreno_create_profile_buffer(adreno_dev);



	set_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags);

	set_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	/*

	 * BCL needs respective Central Broadcast register to

	 * be programed from TZ. This programing happens only

	 * when zap shader firmware load is successful. Zap firmware

	 * load can fail in boot up path hence enable BCL only after we

	 * successfully complete first boot to ensure that Central

	 * Broadcast register was programed before enabling BCL.

	 */

	if (ADRENO_FEATURE(adreno_dev, ADRENO_BCL))

		adreno_dev->bcl_enabled = true;



	/*

	 * There is a possible deadlock scenario during kgsl firmware reading

	 * (firmware_request_nowarn) and devfreq update calls. During first boot, kgsl

	 * device mutex is held and then firmware_request_nowarn is called for reading

	 * firmware. firmware_request_nowarn internally takes dev_pm_qos_mtx lock.

	 * Whereas in case of devfreq update calls triggered by thermal/bcl or

	 * devfreq sysfs, it first takes the same dev_pm_qos_mtx lock and then

	 * tries to take kgsl device mutex as part of get_dev_status/target

	 * calls. This results in deadlock when both thread are unable to acquire

	 * the mutex held by other thread. Enable devfreq updates now as we are

	 * done reading all firmware files.

	 */

	device->pwrscale.devfreq_enabled = true;



	device->pwrctrl.last_stat_updated = ktime_get();



	set_bit(ADRENO_DEVICE_FIRST_BOOT_DONE, &adreno_dev->priv);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



	KGSL_BOOT_MARKER("ADRENO Ready");



	return 0;

}



static int a630_vbif_halt(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	kgsl_regwrite(device, A6XX_VBIF_XIN_HALT_CTRL0,

		A6XX_VBIF_XIN_HALT_CTRL0_MASK);

	ret = adreno_wait_for_halt_ack(device,

			A6XX_VBIF_XIN_HALT_CTRL1,

			A6XX_VBIF_XIN_HALT_CTRL0_MASK);

	kgsl_regwrite(device, A6XX_VBIF_XIN_HALT_CTRL0, 0);



	return ret;

}



static int a6xx_power_off(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	WARN_ON(!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags));



	adreno_check_idle(device);



	kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);



	ret = a6xx_gmu_oob_set(device, oob_gpu);

	if (ret) {

		a6xx_gmu_oob_clear(device, oob_gpu);

		goto no_gx_power;

	}



	if (a6xx_irq_pending(adreno_dev)) {

		a6xx_gmu_oob_clear(device, oob_gpu);

		return -EBUSY;

	}



	kgsl_pwrscale_update_stats(device);



	/* Save active coresight registers if applicable */

	adreno_coresight_stop(adreno_dev);



	/* Save physical performance counter values before GPU power down*/

	adreno_perfcounter_save(adreno_dev);



	/*

	 * Clear GX halt on non-gbif targets. For targets with GBIF,

	 * GX halt is handled by the GMU FW.

	 */

	if (adreno_is_a630(adreno_dev))

		a630_vbif_halt(adreno_dev);



	adreno_irqctrl(adreno_dev, 0);



	a6xx_gmu_oob_clear(device, oob_gpu);



no_gx_power:

	kgsl_pwrctrl_irq(device, false);



	a6xx_gmu_power_off(adreno_dev);



	adreno_set_active_ctxs_null(adreno_dev);



	adreno_dispatcher_stop(adreno_dev);



	adreno_ringbuffer_stop(adreno_dev);



	adreno_llcc_slice_deactivate(adreno_dev);



	clear_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	kgsl_delete_timer_sync(&device->idle_timer);



	kgsl_pwrscale_sleep(device);



	kgsl_pwrctrl_clear_l3_vote(device);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);



	return ret;

}



static void gmu_idle_check(struct work_struct *work)

{

	struct kgsl_device *device = container_of(work,

					struct kgsl_device, idle_check_ws);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	kgsl_mutex_lock(&device->mutex);



	if (test_bit(GMU_DISABLE_SLUMBER, &device->gmu_core.flags))

		goto done;



	if (atomic_read(&device->active_cnt) || time_is_after_jiffies(device->idle_jiffies)) {

		kgsl_pwrscale_update(device);

		goto done;

	}



	if (!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		goto done;



	spin_lock(&device->submit_lock);



	if (device->submit_now) {

		spin_unlock(&device->submit_lock);

		kgsl_pwrscale_update(device);

		kgsl_start_idle_timer(device);

		goto done;

	}



	device->skip_inline_submit = true;

	spin_unlock(&device->submit_lock);



	ret = a6xx_power_off(adreno_dev);

	if (ret == -EBUSY) {

		kgsl_pwrscale_update(device);

		kgsl_start_idle_timer(device);

	}



done:

	kgsl_mutex_unlock(&device->mutex);

}



static int a6xx_gmu_first_open(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	/*

	 * Do the one time settings that need to happen when we

	 * attempt to boot the gpu the very first time

	 */

	ret = a6xx_first_boot(adreno_dev);

	if (ret)

		return ret;



	/*

	 * A client that does a first_open but never closes the device

	 * may prevent us from going back to SLUMBER. So trigger the idle

	 * check by incrementing the active count and immediately releasing it.

	 */

	atomic_inc(&device->active_cnt);

	adreno_active_count_put(adreno_dev);



	return 0;

}



static int a6xx_gmu_last_close(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		return a6xx_power_off(adreno_dev);



	return 0;

}



static int a6xx_gmu_active_count_get(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret = 0;



	if (WARN_ON(!kgsl_mutex_is_locked(&device->mutex)))

		return -EINVAL;



	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags))

		return -EINVAL;



	if ((atomic_read(&device->active_cnt) == 0) &&

		!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		ret = a6xx_boot(adreno_dev);



	if (ret == 0)

		atomic_inc(&device->active_cnt);



	trace_kgsl_active_count(device,

		(unsigned long) __builtin_return_address(0));



	return ret;

}



static int a6xx_gmu_pm_suspend(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags))

		return 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND);



	ret = adreno_drain_and_idle(device);

	if (ret)

		goto err;



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		a6xx_power_off(adreno_dev);



	set_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags);



	adreno_get_gpu_halt(adreno_dev);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);



	return 0;

err:

	adreno_dispatcher_start(device);

	return ret;

}



static void a6xx_gmu_pm_resume(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	if (WARN(!test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags),

		"resume invoked without a suspend\n"))

		return;



	adreno_put_gpu_halt(adreno_dev);



	adreno_dispatcher_start(device);



	clear_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags);

}



static void a6xx_gmu_touch_wakeup(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	/*

	 * Do not wake up a suspended device or until the first boot sequence

	 * has been completed.

	 */

	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags) ||

		!test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags))

		return;



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		goto done;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	ret = a6xx_gmu_boot(adreno_dev);

	if (ret)

		return;



	ret = a6xx_gpu_boot(adreno_dev);

	if (ret)

		return;



	kgsl_pwrscale_wake(device);



	set_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	device->pwrctrl.last_stat_updated = ktime_get();



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



done:

	/*

	 * When waking up from a touch event we want to stay active long enough

	 * for the user to send a draw command.  The default idle timer timeout

	 * is shorter than we want so go ahead and push the idle timer out

	 * further for this special case

	 */

	mod_timer(&device->idle_timer, jiffies +

			msecs_to_jiffies(adreno_wake_timeout));



}



const struct adreno_power_ops a6xx_gmu_power_ops = {

	.first_open = a6xx_gmu_first_open,

	.last_close = a6xx_gmu_last_close,

	.active_count_get = a6xx_gmu_active_count_get,

	.pm_suspend = a6xx_gmu_pm_suspend,

	.pm_resume = a6xx_gmu_pm_resume,

	.touch_wakeup = a6xx_gmu_touch_wakeup,

	.gpu_clock_set = a6xx_gmu_clock_set,

	.gpu_bus_set = a6xx_gmu_bus_set,

};



const struct adreno_power_ops a630_gmu_power_ops = {

	.first_open = a6xx_gmu_first_open,

	.last_close = a6xx_gmu_last_close,

	.active_count_get = a6xx_gmu_active_count_get,

	.pm_suspend = a6xx_gmu_pm_suspend,

	.pm_resume = a6xx_gmu_pm_resume,

	.touch_wakeup = a6xx_gmu_touch_wakeup,

	.gpu_clock_set = a6xx_gmu_clock_set,

};



int a6xx_gmu_device_probe(struct platform_device *pdev,

	u32 chipid, const struct adreno_gpu_core *gpucore)

{

	struct adreno_device *adreno_dev;

	struct kgsl_device *device;

	struct a6xx_device *a6xx_dev;

	int ret;



	a6xx_dev = devm_kzalloc(&pdev->dev, sizeof(*a6xx_dev),

			GFP_KERNEL);

	if (!a6xx_dev)

		return -ENOMEM;



	adreno_dev = &a6xx_dev->adreno_dev;



	adreno_dev->irq_mask = A6XX_INT_MASK;



	ret = a6xx_probe_common(pdev, adreno_dev, chipid, gpucore);

	if (ret)

		return ret;



	ret = adreno_dispatcher_init(adreno_dev);

	if (ret)

		return ret;



	device = KGSL_DEVICE(adreno_dev);



	INIT_WORK(&device->idle_check_ws, gmu_idle_check);



	timer_setup(&device->idle_timer, gmu_idle_timer, 0);



	return 0;

}



int a6xx_gmu_reset(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	a6xx_disable_gpu_irq(adreno_dev);



	a6xx_gmu_irq_disable(adreno_dev);



	a6xx_hfi_stop(adreno_dev);



	/* Hard reset the gmu and gpu */

	a6xx_gmu_suspend(adreno_dev, true);



	a6xx_reset_preempt_records(adreno_dev);



	adreno_llcc_slice_deactivate(adreno_dev);



	clear_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	/* Attempt to reboot the gmu and gpu */

	return a6xx_boot(adreno_dev);

}



int a6xx_gmu_hfi_probe(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hfi *hfi = &gmu->hfi;

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	/* Try with "hfi" irq first. Use Fallback legacy name "kgsl_hfi_irq" if not found. */

	hfi->irq = kgsl_request_irq(GMU_PDEV(device), "hfi", "kgsl_hfi_irq", -EINVAL,

		a6xx_hfi_irq_handler, KGSL_DEVICE(adreno_dev));



	return hfi->irq < 0 ? hfi->irq : 0;

}



int a6xx_gmu_add_to_minidump(struct adreno_device *adreno_dev)

{

	struct a6xx_device *a6xx_dev = container_of(adreno_dev,

					struct a6xx_device, adreno_dev);

	int ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_A6XX_DEVICE,

			(void *)(a6xx_dev), sizeof(struct a6xx_device));

	if (ret)

		return ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_GMU_LOG_ENTRY,

			a6xx_dev->gmu.gmu_log->hostptr, a6xx_dev->gmu.gmu_log->size);

	if (ret)

		return ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_HFIMEM_ENTRY,

			a6xx_dev->gmu.hfi.hfi_mem->hostptr, a6xx_dev->gmu.hfi.hfi_mem->size);

	if (ret)

		return ret;



	if (adreno_is_a630(adreno_dev) || adreno_is_a615_family(adreno_dev))

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_GMU_DUMPMEM_ENTRY,

				a6xx_dev->gmu.dump_mem->hostptr, a6xx_dev->gmu.dump_mem->size);



	return ret;

}



static int a6xx_gmu_bind(struct device *dev, struct device *master, void *data)

{

	struct kgsl_device *device = dev_get_drvdata(master);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	const struct a6xx_gpudev *a6xx_gpudev = to_a6xx_gpudev(gpudev);

	int ret;



	ret = a6xx_gmu_probe(device, to_platform_device(dev));

	if (ret)

		return ret;



	if (a6xx_gpudev->hfi_probe) {

		ret = a6xx_gpudev->hfi_probe(adreno_dev);



		if (ret) {

			a6xx_gmu_remove(device);

			return ret;

		}

	}



	return 0;

}



static void a6xx_gmu_unbind(struct device *dev, struct device *master,

		void *data)

{

	struct kgsl_device *device = dev_get_drvdata(master);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	const struct a6xx_gpudev *a6xx_gpudev = to_a6xx_gpudev(gpudev);



	if (a6xx_gpudev->hfi_remove)

		a6xx_gpudev->hfi_remove(adreno_dev);



	a6xx_gmu_remove(device);

}



static const struct component_ops a6xx_gmu_component_ops = {

	.bind = a6xx_gmu_bind,

	.unbind = a6xx_gmu_unbind,

};



static int a6xx_gmu_probe_dev(struct platform_device *pdev)

{

	/*

	 * Let us say there are two devices. One with "qcom,adreno-gmu" compatible

	 * and one with "qcom,gpu-gmu" compatible. In this case probe only the

	 * device with legacy compatible string and return error for the device

	 * with "qcom,adreno-gmu".

	 */

	if (of_device_is_compatible(pdev->dev.of_node, "qcom,adreno-gmu") &&

		kgsl_is_compatible_node_available("qcom,gpu-gmu"))

		return -ENODEV;



	return component_add(&pdev->dev, &a6xx_gmu_component_ops);

}



#if (KERNEL_VERSION(6, 10, 0) <= LINUX_VERSION_CODE)

static void a6xx_gmu_remove_dev(struct platform_device *pdev)

{

	component_del(&pdev->dev, &a6xx_gmu_component_ops);

}

#else

static int a6xx_gmu_remove_dev(struct platform_device *pdev)

{

	component_del(&pdev->dev, &a6xx_gmu_component_ops);

	return 0;

}

#endif



static const struct of_device_id a6xx_gmu_match_table[] = {

	{ .compatible = "qcom,gpu-gmu" },

	{ .compatible = "qcom,adreno-gmu" },

	{ },

};



struct platform_driver a6xx_gmu_driver = {

	.probe = a6xx_gmu_probe_dev,

	.remove = a6xx_gmu_remove_dev,

	.driver = {

		.name = "adreno-a6xx-gmu",

		.of_match_table = a6xx_gmu_match_table,

	},

};





==========================================================================================
FILE: adreno_a6xx_gmu.h
LINES: 370 | BYTES: 11409
==========================================================================================

/* SPDX-License-Identifier: GPL-2.0-only */

/*

 * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */

#ifndef __ADRENO_A6XX_GMU_H

#define __ADRENO_A6XX_GMU_H



#include <linux/mailbox_client.h>

#include <linux/soc/qcom/qcom_aoss.h>



#include "adreno_a6xx_hfi.h"

#include "kgsl_gmu_core.h"



/**

 * struct a6xx_gmu_device - GMU device structure

 * @irq: GMU interrupt number

 * @fw_image: GMU FW image

 * @dump_mem: pointer to GMU debug dump memory

 * @gmu_log: gmu event log memory

 * @hfi: HFI controller

 * @idle_level: Minimal GPU idle power level

 * @mailbox: Messages to AOP for ACD enable/disable go through this

 * @log_wptr_retention: Store the log wptr offset on slumber

 */

struct a6xx_gmu_device {

	int irq;

	const struct firmware *fw_image;

	struct kgsl_memdesc *dump_mem;

	struct kgsl_memdesc *gmu_log;

	struct a6xx_hfi hfi;

	unsigned int idle_level;

	/** @qmp: Pointer to qmp struct used for AOP messages*/

	struct qmp *qmp;

	struct kgsl_mailbox mailbox;

	bool preallocations;

	unsigned int log_wptr_retention;

	/** @cm3_fault: whether gmu received a cm3 fault interrupt */

	atomic_t cm3_fault;

	/**

	 * @itcm_shadow: Copy of the itcm block in firmware binary used for

	 * snapshot

	 */

	void *itcm_shadow;

	/** @flags: Internal gmu flags */

	unsigned long flags;

	/** @rscc_virt: Pointer where RSCC block is mapped */

	void __iomem *rscc_virt;

	/** @log_stream_enable: GMU log streaming enable. Disabled by default */

	bool log_stream_enable;

	/** @log_group_mask: Allows overriding default GMU log group mask */

	u32 log_group_mask;

	struct kobject log_kobj;

	/** @num_oob_perfcntr: Number of active oob_perfcntr requests */

	u32 num_oob_perfcntr;

	/** @pdc_cfg_base: Base address of PDC cfg registers */

	void __iomem *pdc_cfg_base;

	/** @pdc_seq_base: Base address of PDC seq registers */

	void __iomem *pdc_seq_base;

	/** @stats_enable: GMU stats feature enable */

	bool stats_enable;

	/** @stats_mask: GMU performance countables to enable */

	u32 stats_mask;

	/** @stats_interval: GMU performance counters sampling interval */

	u32 stats_interval;

	/** @stats_kobj: kernel object for GMU stats directory in sysfs */

	struct kobject stats_kobj;

};



/* Helper function to get to a6xx gmu device from adreno device */

struct a6xx_gmu_device *to_a6xx_gmu(struct adreno_device *adreno_dev);



/* Helper function to get to adreno device from a6xx gmu device */

struct adreno_device *a6xx_gmu_to_adreno(struct a6xx_gmu_device *gmu);



/**

 * a6xx_build_rpmh_tables - Build the rpmh tables

 * @adreno_dev: Pointer to the adreno device

 *

 * This function creates the gpu dcvs and bw tables

 *

 * Return: 0 on success and negative error on failure

 */

int a6xx_build_rpmh_tables(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_gx_is_on - Check if GX is on

 * @adreno_dev: Pointer to the adreno device

 *

 * This function reads pwr status registers to check if GX

 * is on or off

 */

bool a6xx_gmu_gx_is_on(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_device_snapshot - A6XX GMU snapshot function

 * @device: Device being snapshotted

 * @snapshot: Pointer to the snapshot instance

 *

 * This is where all of the A6XX GMU specific bits and pieces are grabbed

 * into the snapshot memory

 */

void a6xx_gmu_device_snapshot(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot);



/**

 * a6xx_gmu_device_probe - A6XX GMU snapshot function

 * @pdev: Pointer to the platform device

 * @chipid: Chipid of the target

 * @gpucore: Pointer to the gpucore

 *

 * The target specific probe function for gmu based a6xx targets.

 */

int a6xx_gmu_device_probe(struct platform_device *pdev,

	u32 chipid, const struct adreno_gpu_core *gpucore);



/**

 * a6xx_gmu_reset - Reset and restart the gmu

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_reset(struct adreno_device *adreno_dev);



/**

 * a6xx_enable_gpu_irq - Enable gpu interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_enable_gpu_irq(struct adreno_device *adreno_dev);



/**

 * a6xx_disable_gpu_irq - Disable gpu interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_disable_gpu_irq(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_snapshot- Take snapshot for gmu targets

 * @adreno_dev: Pointer to the adreno device

 * @snapshot: Pointer to the snapshot structure

 *

 * Send an NMI to gmu if we hit a gmu fault. Then take gmu

 * snapshot and carry on with rest of the a6xx snapshot

 */

void a6xx_gmu_snapshot(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot);



/**

 * a6xx_gmu_probe - Probe a6xx gmu resources

 * @device: Pointer to the kgsl device

 * @pdev: Pointer to the gmu platform device

 *

 * Probe the gmu and hfi resources

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_probe(struct kgsl_device *device,

	struct platform_device *pdev);



/**

 * a6xx_gmu_parse_fw - Parse the gmu fw binary

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_parse_fw(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_memory_init - Allocate gmu memory

 * @adreno_dev: Pointer to the adreno device

 *

 * Allocates the gmu log buffer and others if ndeeded.

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_memory_init(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_aop_send_acd_state - Enable or disable acd feature in aop

 * @gmu: Pointer to the a6xx gmu device

 * @flag: Boolean to enable or disable acd in aop

 *

 * This function enables or disables gpu acd feature using mailbox or qmp

 */

void a6xx_gmu_aop_send_acd_state(struct a6xx_gmu_device *gmu, bool flag);



/**

 * a6xx_gmu_disable_gdsc - Disable gmu gdsc

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_gmu_disable_gdsc(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_load_fw - Load gmu firmware

 * @adreno_dev: Pointer to the adreno device

 *

 * Loads the gmu firmware binary into TCMs and memory

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_load_fw(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_device_start - Bring gmu out of reset

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_device_start(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_hfi_start - Indicate hfi start to gmu

 * @device: Pointer to the kgsl device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_hfi_start(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_itcm_shadow - Create itcm shadow copy for snapshot

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_itcm_shadow(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_register_config - gmu register configuration

 * @adreno_dev: Pointer to the adreno device

 *

 * Program gmu regsiters based on features

 */

void a6xx_gmu_register_config(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_version_info - Get gmu firmware version

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_gmu_version_info(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_irq_enable - Enable gmu interrupts

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_gmu_irq_enable(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_irq_disable - Disaable gmu interrupts

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_gmu_irq_disable(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_suspend - Hard reset the gpu and gmu

 * @adreno_dev: Pointer to the adreno device

 * @force: Set to true to enforce a hard reset

 *

 * In case we hit a gmu fault, hard reset the gpu and gmu

 * to recover from the fault

 */

void a6xx_gmu_suspend(struct adreno_device *adreno_dev, bool force);



/**

 * a6xx_gmu_oob_set - send gmu oob request

 * @device: Pointer to the kgsl device

 * @req: Type of oob request as defined in enum oob_request

 *

 * Request gmu to keep gpu powered up till the oob is cleared

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_oob_set(struct kgsl_device *device, enum oob_request oob);



/**

 * a6xx_gmu_oob_clear - clear an asserted oob request

 * @device: Pointer to the kgsl device

 * @req: Type of oob request as defined in enum oob_request

 *

 * Clear a previously requested oob so that gmu can power

 * collapse the gpu

 */

void a6xx_gmu_oob_clear(struct kgsl_device *device, enum oob_request oob);



/**

 * a6xx_gmu_wait_for_lowest_idle - wait for gmu to complete ifpc

 * @adreno_dev: Pointer to the adreno device

 *

 * If ifpc is enabled, wait for gmu to put gpu into ifpc.

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_wait_for_lowest_idle(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_wait_for_idle - Wait for gmu to become idle

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_gmu_wait_for_idle(struct adreno_device *adreno_dev);



/**

 * a6xx_rscc_sleep_sequence - Trigger rscc sleep sequence

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_rscc_sleep_sequence(struct adreno_device *adreno_dev);



/**

 * a6xx_rscc_wakeup_sequence - Trigger rscc wakeup sequence

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_rscc_wakeup_sequence(struct adreno_device *adreno_dev);



/**

 * a6xx_halt_gbif - Halt CX and GX requests in GBIF

 * @adreno_dev: Pointer to the adreno device

 *

 * Clear any pending GX or CX transactions in GBIF and

 * deassert GBIF halt

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_halt_gbif(struct adreno_device *adreno_dev);



/**

 * a6xx_load_pdc_ucode - Load and enable pdc sequence

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_load_pdc_ucode(struct adreno_device *adreno_dev);



/**

 * a6xx_load_rsc_ucode - Load rscc sequence

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_load_rsc_ucode(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_remove - Clean up gmu probed resources

 * @device: Pointer to the kgsl device

 */

void a6xx_gmu_remove(struct kgsl_device *device);



/**

 * a6xx_gmu_handle_watchdog - Handle watchdog interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void a6xx_gmu_handle_watchdog(struct adreno_device *adreno_dev);



/**

 * a6xx_gmu_send_nmi - Send NMI to GMU

 * @device: Pointer to the kgsl device

 * @gf_policy: GMU fault panic setting policy

 * @force: Boolean to forcefully send NMI irrespective of GMU state

 */

void a6xx_gmu_send_nmi(struct kgsl_device *device, bool force,

		       enum gmu_fault_panic_policy gf_policy);



/**

 * a6xx_gmu_add_to_minidump - Register a6xx_device with va minidump

 * @adreno_dev: Pointer to the adreno device

 */

int a6xx_gmu_add_to_minidump(struct adreno_device *adreno_dev);



#endif





==========================================================================================
FILE: adreno_a6xx_gmu_snapshot.c
LINES: 392 | BYTES: 15451
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include "a6xx_reg.h"

#include "adreno.h"

#include "adreno_a6xx.h"

#include "adreno_a6xx_gmu.h"

#include "adreno_snapshot.h"

#include "kgsl_device.h"

#include "kgsl_gmu_core.h"



static const unsigned int a6xx_gmu_gx_registers[] = {

	/* GMU GX */

	0x1A800, 0x1A800, 0x1A810, 0x1A813, 0x1A816, 0x1A816, 0x1A818, 0x1A81B,

	0x1A81E, 0x1A81E, 0x1A820, 0x1A823, 0x1A826, 0x1A826, 0x1A828, 0x1A82B,

	0x1A82E, 0x1A82E, 0x1A830, 0x1A833, 0x1A836, 0x1A836, 0x1A838, 0x1A83B,

	0x1A83E, 0x1A83E, 0x1A840, 0x1A843, 0x1A846, 0x1A846, 0x1A880, 0x1A884,

	0x1A900, 0x1A92B, 0x1A940, 0x1A940,

};



static const unsigned int a6xx_gmu_tcm_registers[] = {

	/* ITCM */

	0x1B400, 0x1C3FF,

	/* DTCM */

	0x1C400, 0x1D3FF,

};



static const unsigned int a6xx_gmu_registers[] = {

	/* GMU CX */

	0x1F400, 0x1F407, 0x1F410, 0x1F412, 0x1F500, 0x1F500, 0x1F507, 0x1F50A,

	0x1F800, 0x1F804, 0x1F807, 0x1F808, 0x1F80B, 0x1F80C, 0x1F80F, 0x1F81C,

	0x1F824, 0x1F82A, 0x1F82D, 0x1F830, 0x1F840, 0x1F853, 0x1F887, 0x1F889,

	0x1F8A0, 0x1F8A2, 0x1F8A4, 0x1F8AF, 0x1F8C0, 0x1F8C3, 0x1F8D0, 0x1F8D0,

	0x1F8E4, 0x1F8E4, 0x1F8E8, 0x1F8EC, 0x1F900, 0x1F903, 0x1F940, 0x1F940,

	0x1F942, 0x1F944, 0x1F94C, 0x1F94D, 0x1F94F, 0x1F951, 0x1F954, 0x1F954,

	0x1F957, 0x1F958, 0x1F95D, 0x1F95D, 0x1F962, 0x1F962, 0x1F964, 0x1F965,

	0x1F980, 0x1F986, 0x1F990, 0x1F99E, 0x1F9C0, 0x1F9C0, 0x1F9C5, 0x1F9CC,

	0x1F9E0, 0x1F9E2, 0x1F9F0, 0x1F9F0, 0x1FA00, 0x1FA01,

	/* GMU AO */

	0x23B00, 0x23B16,

};



static const unsigned int a660_gmu_registers[] = {

	/* GMU CX */

	0x1F408, 0x1F40D, 0x1F40F, 0x1F40F, 0x1F50B, 0x1F50B, 0x1F860, 0x1F860,

	0x1F870, 0x1F877, 0x1F8C4, 0x1F8C4, 0x1F8F0, 0x1F8F1, 0x1F948, 0x1F94A,

	0x1F966, 0x1F96B, 0x1F970, 0x1F970, 0x1F972, 0x1F979, 0x1F9CD, 0x1F9D4,

	0x1FA02, 0x1FA03, 0x20000, 0x20001, 0x20004, 0x20004, 0x20008, 0x20012,

	0x20018, 0x20018,

	/* GMU AO LPAC */

	0x23B30, 0x23B30,

};



static const unsigned int a6xx_gmu_gpucc_registers[] = {

	/* GPU CC */

	0x24000, 0x24012, 0x24040, 0x24052, 0x24400, 0x24404, 0x24407, 0x2440B,

	0x24415, 0x2441C, 0x2441E, 0x2442D, 0x2443C, 0x2443D, 0x2443F, 0x24440,

	0x24442, 0x24449, 0x24458, 0x2445A, 0x24540, 0x2455E, 0x24800, 0x24802,

	0x24C00, 0x24C02, 0x25400, 0x25402, 0x25800, 0x25802, 0x25C00, 0x25C02,

	0x26000, 0x26002,

	/* GPU CC ACD */

	0x26400, 0x26416, 0x26420, 0x26427,

};



static const unsigned int a662_gmu_gpucc_registers[] = {

	/* GPU CC */

	0x24000, 0x2400e, 0x24400, 0x2440e, 0x24800, 0x24805, 0x24c00, 0x24cff,

	0x25800, 0x25804, 0x25c00, 0x25c04, 0x26000, 0x26004, 0x26400, 0x26405,

	0x26414, 0x2641d, 0x2642a, 0x26430, 0x26432, 0x26432, 0x26441, 0x26455,

	0x26466, 0x26468, 0x26478, 0x2647a, 0x26489, 0x2648a, 0x2649c, 0x2649e,

	0x264a0, 0x264a3, 0x264b3, 0x264b5, 0x264c5, 0x264c7, 0x264d6, 0x264d8,

	0x264e8, 0x264e9, 0x264f9, 0x264fc, 0x2650b, 0x2650c, 0x2651c, 0x2651e,

	0x26540, 0x26570, 0x26600, 0x26616, 0x26620, 0x2662d,

};



static const unsigned int a663_gmu_gpucc_registers[] = {

	/* GPU CC */

	0x24000, 0x2400e, 0x24400, 0x2440e, 0x25800, 0x25804, 0x25c00, 0x25c04,

	0x26000, 0x26004, 0x26400, 0x26405, 0x26414, 0x2641d, 0x2642a, 0x26430,

	0x26432, 0x26432, 0x26441, 0x26455, 0x26466, 0x26468, 0x26478, 0x2647a,

	0x26489, 0x2648a, 0x2649c, 0x2649e, 0x264a0, 0x264a3, 0x264b3, 0x264b5,

	0x264c5, 0x264c7, 0x264d6, 0x264d8, 0x264e8, 0x264e9, 0x264f9, 0x264fc,

	0x2650b, 0x2650c, 0x2651c, 0x2651e, 0x26540, 0x26570, 0x26600, 0x26616,

	0x26620, 0x2662d,

};



static const unsigned int a630_rscc_snapshot_registers[] = {

	0x23400, 0x23434, 0x23436, 0x23436, 0x23480, 0x23484, 0x23489, 0x2348C,

	0x23491, 0x23494, 0x23499, 0x2349C, 0x234A1, 0x234A4, 0x234A9, 0x234AC,

	0x23500, 0x23502, 0x23504, 0x23507, 0x23514, 0x23519, 0x23524, 0x2352B,

	0x23580, 0x23597, 0x23740, 0x23741, 0x23744, 0x23747, 0x2374C, 0x23787,

	0x237EC, 0x237EF, 0x237F4, 0x2382F, 0x23894, 0x23897, 0x2389C, 0x238D7,

	0x2393C, 0x2393F, 0x23944, 0x2397F,

};



static const unsigned int a6xx_rscc_snapshot_registers[] = {

	0x23400, 0x23434, 0x23436, 0x23436, 0x23440, 0x23440, 0x23480, 0x23484,

	0x23489, 0x2348C, 0x23491, 0x23494, 0x23499, 0x2349C, 0x234A1, 0x234A4,

	0x234A9, 0x234AC, 0x23500, 0x23502, 0x23504, 0x23507, 0x23514, 0x23519,

	0x23524, 0x2352B, 0x23580, 0x23597, 0x23740, 0x23741, 0x23744, 0x23747,

	0x2374C, 0x23787, 0x237EC, 0x237EF, 0x237F4, 0x2382F, 0x23894, 0x23897,

	0x2389C, 0x238D7, 0x2393C, 0x2393F, 0x23944, 0x2397F,

};



static const unsigned int a650_rscc_registers[] = {

	0x38000, 0x38034, 0x38036, 0x38036, 0x38040, 0x38042, 0x38080, 0x38084,

	0x38089, 0x3808C, 0x38091, 0x38094, 0x38099, 0x3809C, 0x380A1, 0x380A4,

	0x380A9, 0x380AC, 0x38100, 0x38102, 0x38104, 0x38107, 0x38114, 0x38119,

	0x38124, 0x3812E, 0x38180, 0x38197, 0x38340, 0x38341, 0x38344, 0x38347,

	0x3834C, 0x3834F, 0x38351, 0x38354, 0x38356, 0x38359, 0x3835B, 0x3835E,

	0x38360, 0x38363, 0x38365, 0x38368, 0x3836A, 0x3836D, 0x3836F, 0x38372,

	0x383EC, 0x383EF, 0x383F4, 0x383F7, 0x383F9, 0x383FC, 0x383FE, 0x38401,

	0x38403, 0x38406, 0x38408, 0x3840B, 0x3840D, 0x38410, 0x38412, 0x38415,

	0x38417, 0x3841A, 0x38494, 0x38497, 0x3849C, 0x3849F, 0x384A1, 0x384A4,

	0x384A6, 0x384A9, 0x384AB, 0x384AE, 0x384B0, 0x384B3, 0x384B5, 0x384B8,

	0x384BA, 0x384BD, 0x384BF, 0x384C2, 0x3853C, 0x3853F, 0x38544, 0x38547,

	0x38549, 0x3854C, 0x3854E, 0x38551, 0x38553, 0x38556, 0x38558, 0x3855B,

	0x3855D, 0x38560, 0x38562, 0x38565, 0x38567, 0x3856A, 0x385E4, 0x385E7,

	0x385EC, 0x385EF, 0x385F1, 0x385F4, 0x385F6, 0x385F9, 0x385FB, 0x385FE,

	0x38600, 0x38603, 0x38605, 0x38608, 0x3860A, 0x3860D, 0x3860F, 0x38612,

	0x3868C, 0x3868F, 0x38694, 0x38697, 0x38699, 0x3869C, 0x3869E, 0x386A1,

	0x386A3, 0x386A6, 0x386A8, 0x386AB, 0x386AD, 0x386B0, 0x386B2, 0x386B5,

	0x386B7, 0x386BA, 0x38734, 0x38737, 0x3873C, 0x3873F, 0x38741, 0x38744,

	0x38746, 0x38749, 0x3874B, 0x3874E, 0x38750, 0x38753, 0x38755, 0x38758,

	0x3875A, 0x3875D, 0x3875F, 0x38762, 0x387DC, 0x387DF, 0x387E4, 0x387E7,

	0x387E9, 0x387EC, 0x387EE, 0x387F1, 0x387F3, 0x387F6, 0x387F8, 0x387FB,

	0x387FD, 0x38800, 0x38802, 0x38805, 0x38807, 0x3880A, 0x38884, 0x38887,

	0x3888C, 0x3888F, 0x38891, 0x38894, 0x38896, 0x38899, 0x3889B, 0x3889E,

	0x388A0, 0x388A3, 0x388A5, 0x388A8, 0x388AA, 0x388AD, 0x388AF, 0x388B2,

	0x3892C, 0x3892F, 0x38934, 0x38937, 0x38939, 0x3893C, 0x3893E, 0x38941,

	0x38943, 0x38946, 0x38948, 0x3894B, 0x3894D, 0x38950, 0x38952, 0x38955,

	0x38957, 0x3895A, 0x38B50, 0x38B51, 0x38B53, 0x38B55, 0x38B5A, 0x38B5A,

	0x38B5F, 0x38B5F, 0x38B64, 0x38B64, 0x38B69, 0x38B69, 0x38B6E, 0x38B6E,

	0x38B73, 0x38B73, 0x38BF8, 0x38BF8, 0x38BFD, 0x38BFD, 0x38C02, 0x38C02,

	0x38C07, 0x38C07, 0x38C0C, 0x38C0C, 0x38C11, 0x38C11, 0x38C16, 0x38C16,

	0x38C1B, 0x38C1B, 0x38CA0, 0x38CA0, 0x38CA5, 0x38CA5, 0x38CAA, 0x38CAA,

	0x38CAF, 0x38CAF, 0x38CB4, 0x38CB4, 0x38CB9, 0x38CB9, 0x38CBE, 0x38CBE,

	0x38CC3, 0x38CC3, 0x38D48, 0x38D48, 0x38D4D, 0x38D4D, 0x38D52, 0x38D52,

	0x38D57, 0x38D57, 0x38D5C, 0x38D5C, 0x38D61, 0x38D61, 0x38D66, 0x38D66,

	0x38D6B, 0x38D6B, 0x38DF0, 0x38DF0, 0x38DF5, 0x38DF5, 0x38DFA, 0x38DFA,

	0x38DFF, 0x38DFF, 0x38E04, 0x38E04, 0x38E09, 0x38E09, 0x38E0E, 0x38E0E,

	0x38E13, 0x38E13, 0x38E98, 0x38E98, 0x38E9D, 0x38E9D, 0x38EA2, 0x38EA2,

	0x38EA7, 0x38EA7, 0x38EAC, 0x38EAC, 0x38EB1, 0x38EB1, 0x38EB6, 0x38EB6,

	0x38EBB, 0x38EBB, 0x38F40, 0x38F40, 0x38F45, 0x38F45, 0x38F4A, 0x38F4A,

	0x38F4F, 0x38F4F, 0x38F54, 0x38F54, 0x38F59, 0x38F59, 0x38F5E, 0x38F5E,

	0x38F63, 0x38F63, 0x38FE8, 0x38FE8, 0x38FED, 0x38FED, 0x38FF2, 0x38FF2,

	0x38FF7, 0x38FF7, 0x38FFC, 0x38FFC, 0x39001, 0x39001, 0x39006, 0x39006,

	0x3900B, 0x3900B, 0x39090, 0x39090, 0x39095, 0x39095, 0x3909A, 0x3909A,

	0x3909F, 0x3909F, 0x390A4, 0x390A4, 0x390A9, 0x390A9, 0x390AE, 0x390AE,

	0x390B3, 0x390B3, 0x39138, 0x39138, 0x3913D, 0x3913D, 0x39142, 0x39142,

	0x39147, 0x39147, 0x3914C, 0x3914C, 0x39151, 0x39151, 0x39156, 0x39156,

	0x3915B, 0x3915B,

};



static size_t a6xx_gmu_snapshot_dtcm(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv)

{

	struct gmu_core_device *gmu_core = &device->gmu_core;

	struct kgsl_snapshot_gmu_mem *mem_hdr =

		(struct kgsl_snapshot_gmu_mem *)buf;

	u32 *data = (u32 *)(buf + sizeof(*mem_hdr));

	u32 i;



	if (remain < gmu_core->vma[GMU_DTCM].size + sizeof(*mem_hdr)) {

		SNAPSHOT_ERR_NOMEM(device, "GMU DTCM Memory");

		return 0;

	}



	mem_hdr->type = SNAPSHOT_GMU_MEM_BIN_BLOCK;

	mem_hdr->hostaddr = 0;

	mem_hdr->gmuaddr = gmu_core->vma[GMU_DTCM].start;

	mem_hdr->gpuaddr = 0;



	/* FIXME: use a bulk read? */

	for (i = 0; i < (gmu_core->vma[GMU_DTCM].size >> 2); i++)

		gmu_core_regread(device, A6XX_GMU_CM3_DTCM_START + i, data++);



	return gmu_core->vma[GMU_DTCM].size + sizeof(*mem_hdr);

}



static size_t a6xx_gmu_snapshot_itcm(struct kgsl_device *device,

	u8 *buf, size_t remain, void *priv)

{

	struct gmu_core_device *gmu_core = &device->gmu_core;

	struct kgsl_snapshot_gmu_mem *mem_hdr =

			(struct kgsl_snapshot_gmu_mem *)buf;

	void *dest = buf + sizeof(*mem_hdr);

	struct a6xx_gmu_device *gmu = (struct a6xx_gmu_device *)priv;



	if (!gmu->itcm_shadow) {

		dev_err(GMU_PDEV_DEV(device), "ITCM not captured\n");

		return 0;

	}



	if (remain < gmu_core->vma[GMU_ITCM].size + sizeof(*mem_hdr)) {

		SNAPSHOT_ERR_NOMEM(device, "GMU ITCM Memory");

		return 0;

	}



	mem_hdr->type = SNAPSHOT_GMU_MEM_BIN_BLOCK;

	mem_hdr->hostaddr = 0;

	mem_hdr->gmuaddr = gmu_core->vma[GMU_ITCM].start;

	mem_hdr->gpuaddr = 0;



	memcpy(dest, gmu->itcm_shadow, gmu_core->vma[GMU_ITCM].size);



	return gmu_core->vma[GMU_ITCM].size + sizeof(*mem_hdr);

}



static void a6xx_gmu_snapshot_memories(struct kgsl_device *device,

	struct a6xx_gmu_device *gmu, struct kgsl_snapshot *snapshot)

{

	struct gmu_mem_type_desc desc;

	struct kgsl_memdesc *md;

	struct gmu_core_device *gmu_core = &device->gmu_core;

	int i;



	for (i = 0; i < ARRAY_SIZE(gmu_core->gmu_globals); i++) {



		md = &gmu_core->gmu_globals[i];

		if (!md->size)

			continue;



		desc.memdesc = md;

		if (md == gmu->hfi.hfi_mem)

			desc.type = SNAPSHOT_GMU_MEM_HFI;

		else if (md == gmu->gmu_log)

			desc.type = SNAPSHOT_GMU_MEM_LOG;

		else if (md == gmu->dump_mem)

			desc.type = SNAPSHOT_GMU_MEM_DEBUG;

		else if (md == gmu_core->vrb)

			desc.type = SNAPSHOT_GMU_MEM_VRB;

		else if (md == gmu_core->trace.md)

			desc.type = SNAPSHOT_GMU_MEM_TRACE;

		else

			desc.type = SNAPSHOT_GMU_MEM_BIN_BLOCK;



		kgsl_snapshot_add_section(device,

			KGSL_SNAPSHOT_SECTION_GMU_MEMORY,

			snapshot, adreno_snapshot_gmu_mem, &desc);

	}

}



#define RSCC_OFFSET_DWORDS 0x38000



static size_t a6xx_snapshot_rscc_registers(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv)

{

	struct kgsl_snapshot_regs *header = (struct kgsl_snapshot_regs *)buf;

	struct kgsl_snapshot_registers *regs = priv;

	unsigned int *data = (unsigned int *)(buf + sizeof(*header));

	int count = 0, j, k;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	/* Figure out how many registers we are going to dump */

	for (j = 0; j < regs->count; j++) {

		int start = regs->regs[j * 2];

		int end = regs->regs[j * 2 + 1];



		count += (end - start + 1);

	}



	if (remain < (count * 8) + sizeof(*header)) {

		SNAPSHOT_ERR_NOMEM(device, "RSCC REGISTERS");

		return 0;

	}



	for (j = 0; j < regs->count; j++) {

		unsigned int start = regs->regs[j * 2];

		unsigned int end = regs->regs[j * 2 + 1];



		for (k = start; k <= end; k++) {

			unsigned int val;



			val = __raw_readl(gmu->rscc_virt +

				((k - RSCC_OFFSET_DWORDS) << 2));

			*data++ = k;

			*data++ = val;

		}

	}



	header->count = count;



	/* Return the size of the section */

	return (count * 8) + sizeof(*header);

}



static void snapshot_rscc_registers(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	/* RSCC registers are on cx */

	if (adreno_is_a650_family(adreno_dev)) {

		struct kgsl_snapshot_registers r;



		r.regs = a650_rscc_registers;

		r.count = ARRAY_SIZE(a650_rscc_registers) / 2;



		kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_REGS,

			snapshot, a6xx_snapshot_rscc_registers, &r);

	} else if (adreno_is_a615_family(adreno_dev) ||

			adreno_is_a630(adreno_dev)) {

		adreno_snapshot_registers(device, snapshot,

			a630_rscc_snapshot_registers,

			ARRAY_SIZE(a630_rscc_snapshot_registers) / 2);

	} else if (adreno_is_a640(adreno_dev) || adreno_is_a680(adreno_dev)) {

		adreno_snapshot_registers(device, snapshot,

			a6xx_rscc_snapshot_registers,

			ARRAY_SIZE(a6xx_rscc_snapshot_registers) / 2);

	}

}



/*

 * a6xx_gmu_device_snapshot() - A6XX GMU snapshot function

 * @device: Device being snapshotted

 * @snapshot: Pointer to the snapshot instance

 *

 * This is where all of the A6XX GMU specific bits and pieces are grabbed

 * into the snapshot memory

 */

void a6xx_gmu_device_snapshot(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GMU_MEMORY,

		snapshot, a6xx_gmu_snapshot_itcm, gmu);



	adreno_snapshot_gmu_versions(device, snapshot);



	a6xx_gmu_snapshot_memories(device, gmu, snapshot);



	/* Snapshot tcms as registers for legacy targets */

	if (adreno_is_a630(adreno_dev) ||

			adreno_is_a615_family(adreno_dev))

		adreno_snapshot_registers(device, snapshot,

				a6xx_gmu_tcm_registers,

				ARRAY_SIZE(a6xx_gmu_tcm_registers) / 2);



	adreno_snapshot_registers(device, snapshot, a6xx_gmu_registers,

					ARRAY_SIZE(a6xx_gmu_registers) / 2);



	if (adreno_is_a662(adreno_dev) || adreno_is_a621(adreno_dev) ||

		adreno_is_a622(adreno_dev))

		adreno_snapshot_registers(device, snapshot,

			a662_gmu_gpucc_registers,

			ARRAY_SIZE(a662_gmu_gpucc_registers) / 2);

	else if (adreno_is_a663(adreno_dev) || adreno_is_a623(adreno_dev))

		adreno_snapshot_registers(device, snapshot,

			a663_gmu_gpucc_registers,

			ARRAY_SIZE(a663_gmu_gpucc_registers) / 2);

	else

		adreno_snapshot_registers(device, snapshot,

			a6xx_gmu_gpucc_registers,

			ARRAY_SIZE(a6xx_gmu_gpucc_registers) / 2);



	/* Snapshot A660 specific GMU registers */

	if (adreno_is_a660(adreno_dev))

		adreno_snapshot_registers(device, snapshot, a660_gmu_registers,

					ARRAY_SIZE(a660_gmu_registers) / 2);



	snapshot_rscc_registers(adreno_dev, snapshot);



	if (!a6xx_gmu_gx_is_on(adreno_dev))

		goto dtcm;



	/* Set fence to ALLOW mode so registers can be read */

	kgsl_regwrite(device, A6XX_GMU_AO_AHB_FENCE_CTRL, 0);

	/* Make sure the previous write posted before reading */

	wmb();



	adreno_snapshot_registers(device, snapshot,

			a6xx_gmu_gx_registers,

			ARRAY_SIZE(a6xx_gmu_gx_registers) / 2);



	/* A stalled SMMU can lead to NoC timeouts when host accesses DTCM */

	if (adreno_smmu_is_stalled(adreno_dev)) {

		dev_err(GMU_PDEV_DEV(device),

			"Not dumping dtcm because SMMU is stalled\n");

		return;

	}



dtcm:

	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GMU_MEMORY,

		snapshot, a6xx_gmu_snapshot_dtcm, gmu);

}





==========================================================================================
FILE: adreno_a6xx_hfi.c
LINES: 791 | BYTES: 21775
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include <linux/delay.h>

#include <linux/nvmem-consumer.h>



#include "adreno.h"

#include "adreno_a6xx.h"

#include "adreno_a6xx_hfi.h"

#include "kgsl_device.h"

#include "kgsl_trace.h"



/* Below section is for all structures related to HFI queues */

#define HFI_QUEUE_MAX HFI_QUEUE_DEFAULT_CNT



/* Total header sizes + queue sizes + 16 for alignment */

#define HFIMEM_SIZE (sizeof(struct hfi_queue_table) + 16 + \

		(HFI_QUEUE_SIZE * HFI_QUEUE_MAX))



struct a6xx_hfi *to_a6xx_hfi(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	return &gmu->hfi;

}



/* Size in below functions are in unit of dwords */

int a6xx_hfi_queue_read(struct a6xx_gmu_device *gmu, uint32_t queue_idx,

		unsigned int *output, unsigned int max_size)

{

	struct kgsl_device *device = KGSL_DEVICE(a6xx_gmu_to_adreno(gmu));

	struct kgsl_memdesc *mem_addr = gmu->hfi.hfi_mem;

	struct hfi_queue_table *tbl = mem_addr->hostptr;

	struct hfi_queue_header *hdr = &tbl->qhdr[queue_idx];

	uint32_t *queue;

	uint32_t msg_hdr;

	uint32_t i, read;

	uint32_t size;

	int result = 0;



	if (hdr->status == HFI_QUEUE_STATUS_DISABLED)

		return -EINVAL;



	if (hdr->read_index == hdr->write_index)

		return -ENODATA;



	/* Clear the output data before populating */

	memset(output, 0, max_size);



	queue = HOST_QUEUE_START_ADDR(mem_addr, queue_idx);

	msg_hdr = queue[hdr->read_index];

	size = MSG_HDR_GET_SIZE(msg_hdr);



	if (size > (max_size >> 2)) {

		dev_err(GMU_PDEV_DEV(device),

		"HFI message too big: hdr:0x%x rd idx=%d\n",

			msg_hdr, hdr->read_index);

		result = -EMSGSIZE;

		goto done;

	}



	read = hdr->read_index;



	if (read < hdr->queue_size) {

		for (i = 0; i < size && i < (max_size >> 2); i++) {

			output[i] = queue[read];

			read = (read + 1)%hdr->queue_size;

		}

		result = size;

	} else {

		/* In case FW messed up */

		dev_err(GMU_PDEV_DEV(device),

			"Read index %d greater than queue size %d\n",

			hdr->read_index, hdr->queue_size);

		result = -ENODATA;

	}



	if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) >= 2)

		read = ALIGN(read, SZ_4) % hdr->queue_size;



	/* For acks, trace the packet for which this ack was sent */

	if (MSG_HDR_GET_TYPE(msg_hdr) == HFI_MSG_ACK)

		trace_kgsl_hfi_receive(MSG_HDR_GET_ID(output[1]),

			MSG_HDR_GET_SIZE(output[1]),

			MSG_HDR_GET_SEQNUM(output[1]));

	else

		trace_kgsl_hfi_receive(MSG_HDR_GET_ID(msg_hdr),

			MSG_HDR_GET_SIZE(msg_hdr), MSG_HDR_GET_SEQNUM(msg_hdr));



	hfi_update_read_idx(hdr, read);



done:

	return result;

}



/* Size in below functions are in unit of dwords */

int a6xx_hfi_queue_write(struct adreno_device *adreno_dev, uint32_t queue_idx,

		uint32_t *msg, u32 size_bytes)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct hfi_queue_table *tbl = gmu->hfi.hfi_mem->hostptr;

	struct hfi_queue_header *hdr = &tbl->qhdr[queue_idx];

	uint32_t *queue;

	uint32_t i, write_idx, read_idx, empty_space;

	uint32_t size_dwords = size_bytes >> 2;

	u32 align_size = ALIGN(size_dwords, SZ_4);

	uint32_t id = MSG_HDR_GET_ID(*msg);



	if (hdr->status == HFI_QUEUE_STATUS_DISABLED || !IS_ALIGNED(size_bytes, sizeof(u32)))

		return -EINVAL;



	queue = HOST_QUEUE_START_ADDR(gmu->hfi.hfi_mem, queue_idx);



	write_idx = hdr->write_index;

	read_idx = hdr->read_index;



	empty_space = (write_idx >= read_idx) ?

			(hdr->queue_size - (write_idx - read_idx))

			: (read_idx - write_idx);



	if (empty_space <= align_size)

		return -ENOSPC;



	for (i = 0; i < size_dwords; i++) {

		queue[write_idx] = msg[i];

		write_idx = (write_idx + 1) % hdr->queue_size;

	}



	/* Cookify any non used data at the end of the write buffer */

	if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) >= 2) {

		for (; i < align_size; i++) {

			queue[write_idx] = 0xFAFAFAFA;

			write_idx = (write_idx + 1) % hdr->queue_size;

		}

	}



	trace_kgsl_hfi_send(id, size_dwords, MSG_HDR_GET_SEQNUM(*msg));



	hfi_update_write_idx(&hdr->write_index, write_idx);



	return 0;

}



int a6xx_hfi_cmdq_write(struct adreno_device *adreno_dev, u32 *msg, u32 size_bytes)

{

	int ret;



	ret = a6xx_hfi_queue_write(adreno_dev, HFI_CMD_ID, msg, size_bytes);



	/*

	 * Memory barrier to make sure packet and write index are written before

	 * an interrupt is raised

	 */

	wmb();



	/* Send interrupt to GMU to receive the message */

	if (!ret)

		gmu_core_regwrite(KGSL_DEVICE(adreno_dev),

			A6XX_GMU_HOST2GMU_INTR_SET,

			0x1);



	return ret;

}



/* Sizes of the queue and message are in unit of dwords */

static void init_queues(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_memdesc *mem_addr = gmu->hfi.hfi_mem;

	int i;

	struct hfi_queue_table *tbl;

	struct hfi_queue_header *hdr;

	struct {

		unsigned int idx;

		unsigned int pri;

		unsigned int status;

	} queue[HFI_QUEUE_MAX] = {

		{ HFI_CMD_IDX, HFI_CMD_PRI, HFI_QUEUE_STATUS_ENABLED },

		{ HFI_MSG_IDX, HFI_MSG_PRI, HFI_QUEUE_STATUS_ENABLED },

		{ HFI_DBG_IDX, HFI_DBG_PRI, HFI_QUEUE_STATUS_ENABLED },

	};



	/*

	 * Overwrite the queue IDs for A630, A615 and A616 as they use

	 * legacy firmware. Legacy firmware has different queue IDs for

	 * message, debug and dispatch queues (dispatch queues aren't used

	 * on these targets so the queue idx value update is not needed).

	 */

	if (adreno_is_a630(adreno_dev) || adreno_is_a615_family(adreno_dev)) {

		queue[HFI_MSG_ID].idx = HFI_MSG_IDX_LEGACY;

		queue[HFI_DBG_ID].idx = HFI_DBG_IDX_LEGACY;

	}



	/* Fill Table Header */

	tbl = mem_addr->hostptr;

	tbl->qtbl_hdr.version = 0;

	tbl->qtbl_hdr.size = sizeof(struct hfi_queue_table) >> 2;

	tbl->qtbl_hdr.qhdr0_offset = sizeof(struct hfi_queue_table_header) >> 2;

	tbl->qtbl_hdr.qhdr_size = sizeof(struct hfi_queue_header) >> 2;

	tbl->qtbl_hdr.num_q = HFI_QUEUE_MAX;

	tbl->qtbl_hdr.num_active_q = HFI_QUEUE_MAX;



	memset(&tbl->qhdr[0], 0, sizeof(tbl->qhdr));



	/* Fill Individual Queue Headers */

	for (i = 0; i < HFI_QUEUE_MAX; i++) {

		hdr = &tbl->qhdr[i];

		hdr->start_addr = GMU_QUEUE_START_ADDR(mem_addr->gmuaddr, i);

		hdr->type = QUEUE_HDR_TYPE(queue[i].idx, queue[i].pri, 0,  0);

		hdr->status = queue[i].status;

		hdr->queue_size = HFI_QUEUE_SIZE >> 2; /* convert to dwords */

	}

}



int a6xx_hfi_init(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hfi *hfi = &gmu->hfi;



	/* Allocates & maps memory for HFI */

	if (IS_ERR_OR_NULL(hfi->hfi_mem)) {

		hfi->hfi_mem = gmu_core_reserve_kernel_block(KGSL_DEVICE(adreno_dev), 0,

			HFIMEM_SIZE, GMU_NONCACHED_KERNEL, 0);

		if (!IS_ERR(hfi->hfi_mem))

			init_queues(adreno_dev);

	}



	return PTR_ERR_OR_ZERO(hfi->hfi_mem);

}



int a6xx_receive_ack_cmd(struct a6xx_gmu_device *gmu, void *rcvd,

	struct pending_cmd *ret_cmd)

{

	struct adreno_device *adreno_dev = a6xx_gmu_to_adreno(gmu);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	uint32_t *ack = rcvd;

	uint32_t hdr = ack[0];

	uint32_t req_hdr = ack[1];



	if (ret_cmd == NULL)

		return -EINVAL;



	if (CMP_HFI_ACK_HDR(ret_cmd->sent_hdr, req_hdr)) {

		memcpy(&ret_cmd->results, ack, MSG_HDR_GET_SIZE(hdr) << 2);

		return 0;

	}



	/* Didn't find the sender, list the waiter */

	dev_err_ratelimited(GMU_PDEV_DEV(device),

		"HFI ACK: Cannot find sender for 0x%8.8x Waiter: 0x%8.8x\n",

		req_hdr, ret_cmd->sent_hdr);



	gmu_core_fault_snapshot(device, GMU_FAULT_HFI_RECIVE_ACK);



	return -ENODEV;

}



static int a6xx_hfi_send_cmd_wait_inline(struct adreno_device *adreno_dev,

	void *data, u32 size_bytes, struct pending_cmd *ret_cmd)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int rc;

	uint32_t *cmd = data;

	struct a6xx_hfi *hfi = &gmu->hfi;

	unsigned int seqnum = atomic_inc_return(&hfi->seqnum);



	*cmd = MSG_HDR_SET_SEQNUM_SIZE(*cmd, seqnum, size_bytes >> 2);

	if (ret_cmd == NULL)

		return a6xx_hfi_cmdq_write(adreno_dev, cmd, size_bytes);



	ret_cmd->sent_hdr = cmd[0];



	rc = a6xx_hfi_cmdq_write(adreno_dev, cmd, size_bytes);

	if (rc)

		return rc;



	rc = gmu_core_timed_poll_check(device, A6XX_GMU_GMU2HOST_INTR_INFO,

			HFI_IRQ_MSGQ_MASK, HFI_RSP_TIMEOUT, HFI_IRQ_MSGQ_MASK);



	if (rc) {

		dev_err(GMU_PDEV_DEV(device),

		"Timed out waiting on ack for 0x%8.8x (id %d, sequence %d)\n",

		cmd[0], MSG_HDR_GET_ID(*cmd), MSG_HDR_GET_SEQNUM(*cmd));

		gmu_core_fault_snapshot(device, GMU_FAULT_SEND_CMD_WAIT_INLINE);

		return rc;

	}



	/* Clear the interrupt */

	gmu_core_regwrite(device, A6XX_GMU_GMU2HOST_INTR_CLR,

		HFI_IRQ_MSGQ_MASK);



	rc = a6xx_hfi_process_queue(gmu, HFI_MSG_ID, ret_cmd);



	return rc;

}



int a6xx_hfi_send_generic_req(struct adreno_device *adreno_dev, void *cmd, u32 size_bytes)

{

	struct pending_cmd ret_cmd;

	int rc;



	memset(&ret_cmd, 0, sizeof(ret_cmd));



	rc = a6xx_hfi_send_cmd_wait_inline(adreno_dev, cmd, size_bytes, &ret_cmd);

	if (rc)

		return rc;



	if (ret_cmd.results[2]) {

		struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



		dev_err(GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev)),

				"HFI ACK failure: Req=0x%8.8X, Result=0x%8.8X\n",

				ret_cmd.results[1],

				ret_cmd.results[2]);

		gmu_core_fault_snapshot(device, GMU_FAULT_HFI_SEND_GENERIC_REQ);

		return -EINVAL;

	}



	return 0;

}



static int a6xx_hfi_send_gmu_init(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_gmu_init_cmd cmd = {

		.seg_id = 0,

		.dbg_buffer_addr = (unsigned int) gmu->dump_mem->gmuaddr,

		.dbg_buffer_size = (unsigned int) gmu->dump_mem->size,

		.boot_state = 0x1,

	};

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_INIT);

	if (ret)

		return ret;



	return a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

}



static int a6xx_hfi_get_fw_version(struct adreno_device *adreno_dev,

		uint32_t expected_ver, uint32_t *ver)

{

	struct hfi_fw_version_cmd cmd = {

		.supported_ver = expected_ver,

	};

	int rc;

	struct pending_cmd ret_cmd;



	rc = CMD_MSG_HDR(cmd, H2F_MSG_FW_VER);

	if (rc)

		return rc;



	memset(&ret_cmd, 0, sizeof(ret_cmd));



	rc = a6xx_hfi_send_cmd_wait_inline(adreno_dev, &cmd, sizeof(cmd), &ret_cmd);

	if (rc)

		return rc;



	rc = ret_cmd.results[2];

	if (!rc)

		*ver = ret_cmd.results[3];

	else

		dev_err(GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev)),

			"gmu get fw ver failed with error=%d\n", rc);



	return rc;

}



int a6xx_hfi_send_core_fw_start(struct adreno_device *adreno_dev)

{

	struct hfi_core_fw_start_cmd cmd = {

		.handle = 0x0,

	};

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_CORE_FW_START);

	if (ret)

		return ret;



	return a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

}



int a6xx_hfi_send_feature_ctrl(struct adreno_device *adreno_dev,

	uint32_t feature, uint32_t enable, uint32_t data)

{

	struct hfi_feature_ctrl_cmd cmd = {

		.feature = feature,

		.enable = enable,

		.data = data,

	};

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_FEATURE_CTRL);

	if (ret)

		return ret;



	ret = a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

	if (ret)

		dev_err(GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev)), "Unable to %s feature %s (%d)\n",

			enable ? "enable" : "disable", hfi_feature_to_string(feature), feature);

	return ret;

}



int a6xx_hfi_send_set_value(struct adreno_device *adreno_dev,

		u32 type, u32 subtype, u32 data)

{

	struct hfi_set_value_cmd cmd = {

		.type = type,

		.subtype = subtype,

		.data = data,

	};

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_SET_VALUE);

	if (ret)

		return ret;



	ret = a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

	if (ret)

		dev_err(GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev)),

			"Unable to set HFI Value %d, %d to %d, error = %d\n",

			type, subtype, data, ret);

	return ret;

}



static int a6xx_hfi_send_dcvstbl_v1(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_dcvstable_cmd *table = &gmu->hfi.dcvs_table;

	struct hfi_dcvstable_v1_cmd cmd = {

		.gpu_level_num = table->gpu_level_num,

		.gmu_level_num = table->gmu_level_num,

	};

	int i, ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_PERF_TBL);

	if (ret)

		return ret;



	for (i = 0; i < table->gpu_level_num; i++) {

		cmd.gx_votes[i].vote = table->gx_votes[i].vote;

		cmd.gx_votes[i].freq = table->gx_votes[i].freq;

	}



	cmd.cx_votes[0].vote = table->cx_votes[0].vote;

	cmd.cx_votes[0].freq = table->cx_votes[0].freq;

	cmd.cx_votes[1].vote = table->cx_votes[1].vote;

	cmd.cx_votes[1].freq = table->cx_votes[1].freq;



	return a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

}



static int a6xx_hfi_send_test(struct adreno_device *adreno_dev)

{

	struct hfi_test_cmd cmd;

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_TEST);

	if (ret)

		return ret;



	cmd.data = 0;



	return a6xx_hfi_send_generic_req(adreno_dev, &cmd, sizeof(cmd));

}



void adreno_a6xx_receive_err_req(struct a6xx_gmu_device *gmu, void *rcvd)

{

	struct kgsl_device *device = KGSL_DEVICE(a6xx_gmu_to_adreno(gmu));

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(ADRENO_DEVICE(device));

	u64 ticks = gpudev->read_alwayson(ADRENO_DEVICE(device));

	struct hfi_err_cmd *cmd = rcvd;



	dev_err(GMU_PDEV_DEV(device), "HFI Error Received: %d %d %.16s\n",

			((cmd->error_code >> 16) & 0xFFFF),

			(cmd->error_code & 0xFFFF),

			(char *) cmd->data);



	KGSL_GMU_CORE_FORCE_PANIC(device->gmu_core.gf_panic,

			GMU_PDEV(device), ticks, GMU_FAULT_F2H_MSG_ERR);

}



void adreno_a6xx_receive_debug_req(struct a6xx_gmu_device *gmu, void *rcvd)

{

	struct kgsl_device *device = KGSL_DEVICE(a6xx_gmu_to_adreno(gmu));

	struct hfi_debug_cmd *cmd = rcvd;



	dev_dbg(GMU_PDEV_DEV(device), "HFI Debug Received: %d %d %d\n",

			cmd->type, cmd->timestamp, cmd->data);

}



static void a6xx_hfi_v1_receiver(struct a6xx_gmu_device *gmu, uint32_t *rcvd,

	struct pending_cmd *ret_cmd)

{

	struct kgsl_device *device = KGSL_DEVICE(a6xx_gmu_to_adreno(gmu));

	/* V1 ACK Handler */

	if (MSG_HDR_GET_TYPE(rcvd[0]) == HFI_V1_MSG_ACK) {

		a6xx_receive_ack_cmd(gmu, rcvd, ret_cmd);

		return;

	}



	/* V1 Request Handler */

	switch (MSG_HDR_GET_ID(rcvd[0])) {

	case F2H_MSG_ERR: /* No Reply */

		adreno_a6xx_receive_err_req(gmu, rcvd);

		break;

	case F2H_MSG_DEBUG: /* No Reply */

		adreno_a6xx_receive_debug_req(gmu, rcvd);

		break;

	default: /* No Reply */

		dev_err(GMU_PDEV_DEV(device),

				"HFI V1 request %d not supported\n",

				MSG_HDR_GET_ID(rcvd[0]));

		break;

	}

}



int a6xx_hfi_process_queue(struct a6xx_gmu_device *gmu,

		uint32_t queue_idx, struct pending_cmd *ret_cmd)

{

	struct kgsl_device *device = KGSL_DEVICE(a6xx_gmu_to_adreno(gmu));

	uint32_t rcvd[MAX_RCVD_SIZE];



	while (a6xx_hfi_queue_read(gmu, queue_idx, rcvd, sizeof(rcvd)) > 0) {

		/* Special case if we're v1 */

		if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) < 2) {

			a6xx_hfi_v1_receiver(gmu, rcvd, ret_cmd);

			continue;

		}



		/* V2 ACK Handler */

		if (MSG_HDR_GET_TYPE(rcvd[0]) == HFI_MSG_ACK) {

			int ret = a6xx_receive_ack_cmd(gmu, rcvd, ret_cmd);



			if (ret)

				return ret;

			continue;

		}



		/* V2 Request Handler */

		switch (MSG_HDR_GET_ID(rcvd[0])) {

		case F2H_MSG_ERR: /* No Reply */

			adreno_a6xx_receive_err_req(gmu, rcvd);

			break;

		case F2H_MSG_DEBUG: /* No Reply */

			adreno_a6xx_receive_debug_req(gmu, rcvd);

			break;

		default: /* No Reply */

			dev_err(GMU_PDEV_DEV(device),

				"HFI request %d not supported\n",

				MSG_HDR_GET_ID(rcvd[0]));

			break;

		}

	}



	return 0;

}



static int a6xx_hfi_verify_fw_version(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(device);

	int result;

	unsigned int ver, major, minor;



	/* GMU version is already known, so don't waste time finding again */

	if (device->gmu_core.ver.core != 0)

		return 0;



	major = a6xx_core->gmu_major;

	minor = a6xx_core->gmu_minor;



	result = a6xx_hfi_get_fw_version(adreno_dev, GMU_VERSION(major, minor, 0),

			&ver);

	if (result) {

		dev_err_once(gmu_pdev_dev,

				"Failed to get FW version via HFI\n");

		return result;

	}



	/* For now, warn once. Could return error later if needed */

	if (major != GMU_VER_MAJOR(ver))

		dev_err_once(gmu_pdev_dev,

				"FW Major Error: Wanted %d, got %d\n",

				major, GMU_VER_MAJOR(ver));



	if (minor > GMU_VER_MINOR(ver))

		dev_err_once(gmu_pdev_dev,

				"FW Minor Error: Wanted < %d, got %d\n",

				GMU_VER_MINOR(ver), minor);



	/* Save the gmu version information */

	device->gmu_core.ver.core = ver;



	return 0;

}



int a6xx_hfi_send_bcl_feature_ctrl(struct adreno_device *adreno_dev)

{

	int ret;



	if (!adreno_dev->bcl_enabled)

		return 0;



	ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_BCL, 1, 0);



	return ret;

}



int a6xx_hfi_send_lm_feature_ctrl(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct hfi_set_value_cmd req;

	u32 slope = 0;

	int ret;



	if (!adreno_dev->lm_enabled)

		return 0;



	memset(&req, 0, sizeof(req));



	nvmem_cell_read_u32(&device->pdev->dev, "isense_slope", &slope);



	ret = CMD_MSG_HDR(req, H2F_MSG_SET_VALUE);

	if (ret)

		return ret;



	req.type = HFI_VALUE_LM_CS0;

	req.subtype = 0;

	req.data = slope;



	ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_LM, 1,

			device->pwrctrl.throttle_mask);



	if (!ret)

		ret = a6xx_hfi_send_generic_req(adreno_dev, &req, sizeof(req));



	return ret;

}



int a6xx_hfi_send_acd_feature_ctrl(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret = 0;



	if (adreno_dev->acd_enabled) {

		ret = a6xx_hfi_send_generic_req(adreno_dev,

			&gmu->hfi.acd_table, sizeof(gmu->hfi.acd_table));

		if (!ret)

			ret = a6xx_hfi_send_feature_ctrl(adreno_dev,

				HFI_FEATURE_ACD, 1, 0);

	}



	return ret;

}



static void reset_hfi_queues(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_memdesc *mem_addr = gmu->hfi.hfi_mem;

	struct hfi_queue_table *tbl = mem_addr->hostptr;

	struct hfi_queue_header *hdr;

	unsigned int i;



	/* Flush HFI queues */

	for (i = 0; i < HFI_QUEUE_MAX; i++) {

		hdr = &tbl->qhdr[i];



		if (hdr->status == HFI_QUEUE_STATUS_DISABLED)

			continue;



		hdr->read_index = hdr->write_index;

	}

}



int a6xx_hfi_start(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int result;



	reset_hfi_queues(adreno_dev);



	/* This is legacy HFI message for A630 and A615 family firmware */

	if (adreno_is_a630(adreno_dev) || adreno_is_a615_family(adreno_dev)) {

		result = a6xx_hfi_send_gmu_init(adreno_dev);

		if (result)

			goto err;

	}



	result = a6xx_hfi_verify_fw_version(adreno_dev);

	if (result)

		goto err;



	if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) < 2)

		result = a6xx_hfi_send_dcvstbl_v1(adreno_dev);

	else

		result = a6xx_hfi_send_generic_req(adreno_dev,

			&gmu->hfi.dcvs_table, sizeof(gmu->hfi.dcvs_table));

	if (result)

		goto err;



	result = a6xx_hfi_send_generic_req(adreno_dev, &gmu->hfi.bw_table,

			sizeof(gmu->hfi.bw_table));

	if (result)

		goto err;



	/*

	 * If quirk is enabled send H2F_MSG_TEST and tell the GMU

	 * we are sending no more HFIs until the next boot otherwise

	 * send H2F_MSG_CORE_FW_START and features for A640 devices

	 */

	if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) >= 2) {

		result = a6xx_hfi_send_acd_feature_ctrl(adreno_dev);

		if (result)

			goto err;



		result = a6xx_hfi_send_lm_feature_ctrl(adreno_dev);

		if (result)

			goto err;



		result = a6xx_hfi_send_bcl_feature_ctrl(adreno_dev);

		if (result)

			goto err;



		result = a6xx_hfi_send_core_fw_start(adreno_dev);

		if (result)

			goto err;

	} else {

		if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_HFI_USE_REG)) {

			result = a6xx_hfi_send_test(adreno_dev);

			if (result)

				goto err;

		}

	}



	set_bit(GMU_PRIV_HFI_STARTED, &gmu->flags);



	result = kgsl_pwrctrl_setup_default_votes(device);



err:

	if (result)

		a6xx_hfi_stop(adreno_dev);



	return result;



}



void a6xx_hfi_stop(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	kgsl_pwrctrl_axi(device, false);



	clear_bit(GMU_PRIV_HFI_STARTED, &gmu->flags);

}



/* HFI interrupt handler */

irqreturn_t a6xx_hfi_irq_handler(int irq, void *data)

{

	struct kgsl_device *device = data;

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(ADRENO_DEVICE(device));

	unsigned int status = 0;



	gmu_core_regread(device, A6XX_GMU_GMU2HOST_INTR_INFO, &status);

	gmu_core_regwrite(device, A6XX_GMU_GMU2HOST_INTR_CLR, HFI_IRQ_MASK);



	if (status & HFI_IRQ_DBGQ_MASK)

		a6xx_hfi_process_queue(gmu, HFI_DBG_ID, NULL);

	if (status & HFI_IRQ_CM3_FAULT_MASK) {

		dev_err_ratelimited(GMU_PDEV_DEV(device),

				"GMU CM3 fault interrupt received\n");

		atomic_set(&gmu->cm3_fault, 1);



		/* make sure other CPUs see the update */

		smp_wmb();

	}

	if (status & ~HFI_IRQ_MASK)

		dev_err_ratelimited(GMU_PDEV_DEV(device),

				"Unhandled HFI interrupts 0x%lx\n",

				status & ~HFI_IRQ_MASK);



	return IRQ_HANDLED;

}





==========================================================================================
FILE: adreno_a6xx_hfi.h
LINES: 188 | BYTES: 6238
==========================================================================================

/* SPDX-License-Identifier: GPL-2.0-only */

/*

 * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.

 */

#ifndef __ADRENO_A6XX_HFI_H

#define __ADRENO_A6XX_HFI_H



#include "adreno_hfi.h"



/**

 * struct a6xx_hfi - HFI control structure

 * @seqnum: atomic counter that is incremented for each message sent. The

 *	value of the counter is used as sequence number for HFI message

 * @bw_table: HFI BW table buffer

 * @acd_table: HFI table for ACD data

 */

struct a6xx_hfi {

	/** @irq: HFI interrupt line */

	int irq;

	atomic_t seqnum;

	/** @hfi_mem: Memory descriptor for the hfi memory */

	struct kgsl_memdesc *hfi_mem;

	struct hfi_bwtable_cmd bw_table;

	struct hfi_acd_table_cmd acd_table;

	/** @dcvs_table: HFI table for gpu dcvs levels */

	struct hfi_dcvstable_cmd dcvs_table;

};



struct a6xx_gmu_device;



/* a6xx_hfi_irq_handler - IRQ handler for HFI interripts */

irqreturn_t a6xx_hfi_irq_handler(int irq, void *data);



/**

 * a6xx_hfi_start - Send the various HFIs during device boot up

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_start(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_start - Send the various HFIs during device boot up

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

void a6xx_hfi_stop(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_init - Initialize hfi resources

 * @adreno_dev: Pointer to the adreno device

 *

 * This function allocates and sets up hfi queues

 * when a process creates the very first kgsl instance

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_init(struct adreno_device *adreno_dev);



/* Helper function to get to a6xx hfi struct from adreno device */

struct a6xx_hfi *to_a6xx_hfi(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_queue_write - Write a command to hfi queue

 * @adreno_dev: Pointer to the adreno device

 * @queue_idx: destination queue id

 * @msg: Data to be written to the queue

 * @size_bytes: Size of the command in bytes

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_queue_write(struct adreno_device *adreno_dev, u32 queue_idx,

	u32 *msg, u32 size_bytes);



/**

 * a6xx_hfi_queue_read - Read data from hfi queue

 * @gmu: Pointer to the a6xx gmu device

 * @queue_idx: queue id to read from

 * @output: Pointer to read the data into

 * @max_size: Number of bytes to read from the queue

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_queue_read(struct a6xx_gmu_device *gmu, u32 queue_idx,

	u32 *output, u32 max_size);



/**

 * a6xx_receive_ack_cmd - Process ack type packets

 * @gmu: Pointer to the a6xx gmu device

 * @rcvd: Pointer to the data read from hfi queue

 * @ret_cmd: Container for the hfi packet for which this ack is received

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_receive_ack_cmd(struct a6xx_gmu_device *gmu, void *rcvd,

	struct pending_cmd *ret_cmd);



/**

 * a6xx_hfi_send_feature_ctrl - Enable gmu feature via hfi

 * @adreno_dev: Pointer to the adreno device

 * @feature: feature to be enabled or disabled

 * enable: Set 1 to enable or 0 to disable a feature

 * @data: payload for the send feature hfi packet

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_feature_ctrl(struct adreno_device *adreno_dev,

	u32 feature, u32 enable, u32 data);



/**

 * a6xx_hfi_send_set_value - Send gmu set_values via hfi

 * @adreno_dev: Pointer to the adreno device

 * @type: GMU set_value type

 * @subtype: GMU set_value subtype

 * @data: Value to set

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_set_value(struct adreno_device *adreno_dev,

		u32 type, u32 subtype, u32 data);



/**

 * a6xx_hfi_send_core_fw_start - Send the core fw start hfi

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_core_fw_start(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_send_acd_feature_ctrl - Send the acd table and acd feature

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_acd_feature_ctrl(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_send_lm_feature_ctrl -  Send the lm feature hfi packet

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_lm_feature_ctrl(struct adreno_device *adreno_dev);



/**

 * a6xx_hfi_send_generic_req -  Send a generic hfi packet

 * @adreno_dev: Pointer to the adreno device

 * @cmd: Pointer to the hfi packet header and data

 * @size_bytes: Size of the command in bytes

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_generic_req(struct adreno_device *adreno_dev, void *cmd, u32 size_bytes);



/**

 * a6xx_hfi_send_bcl_feature_ctrl -  Send the bcl feature hfi packet

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_send_bcl_feature_ctrl(struct adreno_device *adreno_dev);



/*

 * a6xx_hfi_process_queue - Check hfi queue for messages from gmu

 * @gmu: Pointer to the a6xx gmu device

 * @queue_idx: queue id to be processed

 * @ret_cmd: Container for data needed for waiting for the ack

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_process_queue(struct a6xx_gmu_device *gmu,

	u32 queue_idx, struct pending_cmd *ret_cmd);



/**

 * a6xx_hfi_cmdq_write - Write a command to command queue

 * @adreno_dev: Pointer to the adreno device

 * @msg: Data to be written to the queue

 * @size_bytes: Size of the command in bytes

 *

 * Return: 0 on success or negative error on failure

 */

int a6xx_hfi_cmdq_write(struct adreno_device *adreno_dev, u32 *msg, u32 size_bytes);

void adreno_a6xx_receive_err_req(struct a6xx_gmu_device *gmu, void *rcvd);

void adreno_a6xx_receive_debug_req(struct a6xx_gmu_device *gmu, void *rcvd);

#endif





==========================================================================================
FILE: adreno_a6xx_hwsched.c
LINES: 1115 | BYTES: 29751
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include <linux/interconnect.h>



#include "adreno.h"

#include "adreno_a6xx.h"

#include "adreno_a6xx_hwsched.h"

#include "adreno_hfi.h"

#include "adreno_snapshot.h"

#include "kgsl_bus.h"

#include "kgsl_device.h"

#include "kgsl_gmu_core.h"

#include "kgsl_trace.h"

#include "kgsl_util.h"



void a6xx_hwsched_snapshot(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	u32 i;

	bool skip_memkind_rb = false;

	bool parse_payload;



	a6xx_gmu_snapshot(adreno_dev, snapshot);



	adreno_hwsched_parse_fault_cmdobj(adreno_dev, snapshot);



	/*

	 * First try to dump ringbuffers using context bad HFI payloads

	 * because they have all the ringbuffer parameters. If ringbuffer

	 * payloads are not present, fall back to dumping ringbuffers

	 * based on MEMKIND_RB

	 */

	if (GMU_VER_MINOR(device->gmu_core.ver.hfi) < 2)

		parse_payload = adreno_hwsched_parse_payload_rb_legacy(adreno_dev, snapshot);

	else

		parse_payload = adreno_hwsched_parse_payload_rb(adreno_dev, snapshot);



	if (parse_payload)

		skip_memkind_rb = true;



	for (i = 0; i < hwsched->mem_alloc_entries; i++) {

		struct hfi_mem_alloc_entry *entry = &hwsched->mem_alloc_table[i];



		if (entry->desc.mem_kind == HFI_MEMKIND_RB && !skip_memkind_rb)

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_RB_V2,

				snapshot, adreno_hwsched_snapshot_rb,

				entry->md);



		if (entry->desc.mem_kind == HFI_MEMKIND_SCRATCH)

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

				snapshot, adreno_snapshot_global,

				entry->md);



		if (entry->desc.mem_kind == HFI_MEMKIND_PROFILE)

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

				snapshot, adreno_snapshot_global,

				entry->md);



		if (entry->desc.mem_kind == HFI_MEMKIND_CSW_SMMU_INFO)

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

				snapshot, adreno_snapshot_global,

				entry->md);



		if (entry->desc.mem_kind == HFI_MEMKIND_CSW_PRIV_NON_SECURE)

			adreno_hwsched_snapshot_preemption_records(device, snapshot,

				entry->md);



		if (entry->desc.mem_kind == HFI_MEMKIND_PREEMPT_SCRATCH)

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

				snapshot, adreno_snapshot_global,

				entry->md);

	}

}



static void a6xx_hwsched_gmu_suspend(struct adreno_device *adreno_dev, bool force)

{

	a6xx_gmu_suspend(adreno_dev, force);

	adreno_hwsched_reset_hfi_mem(adreno_dev);

}



static int a6xx_hwsched_gmu_first_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int level, ret = 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_AWARE);



	a6xx_gmu_aop_send_acd_state(gmu, adreno_dev->acd_enabled);



	ret = kgsl_pwrctrl_enable_cx_gdsc(device);

	if (ret)

		return ret;



	ret = gmu_core_enable_clks(device, device->gmu_core.num_freqs - 1);

	if (ret)

		goto gdsc_off;



	ret = a6xx_gmu_load_fw(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = a6xx_gmu_itcm_shadow(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	if (!test_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags)) {

		ret = a6xx_load_pdc_ucode(adreno_dev);

		if (ret)

			goto clks_gdsc_off;



		a6xx_load_rsc_ucode(adreno_dev);

		set_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags);

	}



	a6xx_gmu_register_config(adreno_dev);



	a6xx_gmu_version_info(adreno_dev);



	if (GMU_VER_MINOR(device->gmu_core.ver.hfi) < 2)

		set_bit(ADRENO_HWSCHED_CTX_BAD_LEGACY, &adreno_dev->hwsched.flags);



	a6xx_gmu_irq_enable(adreno_dev);



	/* Vote for minimal DDR BW for GMU to init */

	level = pwr->pwrlevels[pwr->default_pwrlevel].bus_min;



	icc_set_bw(pwr->icc_path, 0, kBps_to_icc(pwr->ddr_table[level]));



	/* Clear any hwsched faults that might have been left over */

	adreno_clear_gpu_fault(adreno_dev);



	ret = a6xx_gmu_device_start(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hwsched_hfi_start(adreno_dev);

	if (ret)

		goto err;



	/* Switch to min GMU clock */

	ret = gmu_core_clock_set_rate(device, 0);

	if (ret) {

		a6xx_hwsched_hfi_stop(adreno_dev);

		goto err;

	}



	icc_set_bw(pwr->icc_path, 0, 0);



	device->gmu_fault = false;



	kgsl_pwrctrl_set_state(device, KGSL_STATE_AWARE);



	return 0;



err:

	a6xx_gmu_irq_disable(adreno_dev);



	if (device->gmu_fault) {

		a6xx_hwsched_gmu_suspend(adreno_dev, false);

		return ret;

	}



	adreno_hwsched_reset_hfi_mem(adreno_dev);



clks_gdsc_off:

	gmu_core_disable_clks(device);



gdsc_off:

	a6xx_gmu_disable_gdsc(adreno_dev);



	gmu_core_rdpm_cx_freq_update(device, 0);



	return ret;

}



static int a6xx_hwsched_gmu_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret = 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_AWARE);



	ret = kgsl_pwrctrl_enable_cx_gdsc(device);

	if (ret)

		return ret;



	ret = gmu_core_enable_clks(device, device->gmu_core.num_freqs - 1);

	if (ret)

		goto gdsc_off;



	ret = a6xx_rscc_wakeup_sequence(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = a6xx_gmu_load_fw(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	a6xx_gmu_register_config(adreno_dev);



	a6xx_gmu_irq_enable(adreno_dev);



	/* Clear any hwsched faults that might have been left over */

	adreno_clear_gpu_fault(adreno_dev);



	ret = a6xx_gmu_device_start(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hwsched_hfi_start(adreno_dev);

	if (ret)

		goto err;



	/* Switch to min GMU clock */

	ret = gmu_core_clock_set_rate(device, 0);

	if (ret) {

		a6xx_hwsched_hfi_stop(adreno_dev);

		goto err;

	}



	device->gmu_fault = false;



	kgsl_pwrctrl_set_state(device, KGSL_STATE_AWARE);



	return 0;

err:

	a6xx_gmu_irq_disable(adreno_dev);



	if (device->gmu_fault) {

		a6xx_hwsched_gmu_suspend(adreno_dev, false);

		return ret;

	}



	adreno_hwsched_reset_hfi_mem(adreno_dev);



clks_gdsc_off:

	gmu_core_disable_clks(device);



gdsc_off:

	a6xx_gmu_disable_gdsc(adreno_dev);



	gmu_core_rdpm_cx_freq_update(device, 0);



	return ret;

}



static int a6xx_hwsched_notify_slumber(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_prep_slumber_cmd req;

	int ret;



	ret = CMD_MSG_HDR(req, H2F_MSG_PREPARE_SLUMBER);

	if (ret)

		return ret;



	req.freq = gmu->hfi.dcvs_table.gpu_level_num -

			pwr->default_pwrlevel - 1;

	req.bw = pwr->pwrlevels[pwr->default_pwrlevel].bus_freq;



	/* Disable the power counter so that the GMU is not busy */

	gmu_core_regwrite(device, A6XX_GMU_CX_GMU_POWER_COUNTER_ENABLE, 0);



	ret = a6xx_hfi_send_cmd_async(adreno_dev, &req, sizeof(req));



	/*

	 * GEMNOC can enter power collapse state during GPU power down sequence.

	 * This could abort CX GDSC collapse. Assert Qactive to avoid this.

	 */

	if ((adreno_is_a662(adreno_dev) || adreno_is_a621(adreno_dev) ||

			adreno_is_a622(adreno_dev) || adreno_is_a642l(adreno_dev)))

		gmu_core_regwrite(device, A6XX_GPU_GMU_CX_GMU_CX_FALNEXT_INTF, 0x1);



	return ret;

}

static int a6xx_hwsched_gmu_power_off(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret = 0;



	if (device->gmu_fault)

		goto error;



	/* Wait for the lowest idle level we requested */

	ret = a6xx_gmu_wait_for_lowest_idle(adreno_dev);

	if (ret)

		goto error;



	ret = a6xx_hwsched_notify_slumber(adreno_dev);

	if (ret)

		goto error;



	ret = a6xx_gmu_wait_for_idle(adreno_dev);

	if (ret)

		goto error;



	ret = a6xx_rscc_sleep_sequence(adreno_dev);



	gmu_core_rdpm_mx_freq_update(device, 0);



	/* Now that we are done with GMU and GPU, Clear the GBIF */

	ret = a6xx_halt_gbif(adreno_dev);

	/* De-assert the halts */

	kgsl_regwrite(device, A6XX_GBIF_HALT, 0x0);



	a6xx_gmu_irq_disable(adreno_dev);



	a6xx_hwsched_hfi_stop(adreno_dev);



	gmu_core_disable_clks(device);



	a6xx_gmu_disable_gdsc(adreno_dev);



	gmu_core_rdpm_cx_freq_update(device, 0);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_NONE);



	adreno_hwsched_reset_hfi_mem(adreno_dev);



	return ret;



error:

	a6xx_gmu_irq_disable(adreno_dev);

	a6xx_hwsched_hfi_stop(adreno_dev);

	a6xx_hwsched_gmu_suspend(adreno_dev, false);



	return ret;

}



static int a6xx_hwsched_gpu_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	ret = kgsl_mmu_start(device);

	if (ret)

		goto err;



	ret = a6xx_gmu_oob_set(device, oob_gpu);

	if (ret)

		goto err;



	/* Clear the busy_data stats - we're starting over from scratch */

	memset(&adreno_dev->busy_data, 0, sizeof(adreno_dev->busy_data));



	/* Restore performance counter registers with saved values */

	adreno_perfcounter_restore(adreno_dev);



	a6xx_start(adreno_dev);



	/* Re-initialize the coresight registers if applicable */

	adreno_coresight_start(adreno_dev);



	adreno_perfcounter_start(adreno_dev);



	/* Clear FSR here in case it is set from a previous pagefault */

	kgsl_mmu_clear_fsr(&device->mmu);



	a6xx_enable_gpu_irq(adreno_dev);



	ret = a6xx_hwsched_cp_init(adreno_dev);

	if (ret) {

		a6xx_disable_gpu_irq(adreno_dev);

		adreno_llcc_slice_deactivate(adreno_dev);

		goto err;

	}



	/*

	 * At this point it is safe to assume that we recovered. Setting

	 * this field allows us to take a new snapshot for the next failure

	 * if we are prioritizing the first unrecoverable snapshot.

	 */

	if (device->snapshot)

		device->snapshot->recovered = true;



	device->reset_counter++;

err:

	a6xx_gmu_oob_clear(device, oob_gpu);



	if (ret)

		a6xx_hwsched_gmu_power_off(adreno_dev);



	return ret;

}



static void hwsched_idle_timer(struct timer_list *t)

{

	struct kgsl_device *device = container_of(t, struct kgsl_device,

					idle_timer);



	kgsl_schedule_work(&device->idle_check_ws);

}



static int a6xx_hwsched_gmu_init(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	ret = a6xx_gmu_parse_fw(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_gmu_memory_init(adreno_dev);

	if (ret)

		return ret;



	ret = gmu_core_hwsched_memory_init(device);

	if (ret)

		return ret;



	return a6xx_hwsched_hfi_init(adreno_dev);

}



static void a6xx_hwsched_touch_wakeup(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	/*

	 * Do not wake up a suspended device or until the first boot sequence

	 * has been completed.

	 */

	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags) ||

		!test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags))

		return;



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		goto done;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	ret = a6xx_hwsched_gmu_boot(adreno_dev);

	if (ret)

		return;



	ret = a6xx_hwsched_gpu_boot(adreno_dev);

	if (ret)

		return;



	kgsl_pwrscale_wake(device);



	set_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	device->pwrctrl.last_stat_updated = ktime_get();



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



done:

	/*

	 * When waking up from a touch event we want to stay active long enough

	 * for the user to send a draw command.  The default idle timer timeout

	 * is shorter than we want so go ahead and push the idle timer out

	 * further for this special case

	 */

	mod_timer(&device->idle_timer, jiffies +

		msecs_to_jiffies(adreno_wake_timeout));

}



static int a6xx_hwsched_boot(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	bool bcl_state = adreno_dev->bcl_enabled;

	int ret;



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		return 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	adreno_hwsched_start(adreno_dev);



	if (IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION) &&

		!test_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags)) {

		/*

		 * During hibernation entry ZAP was unloaded and

		 * CBCAST BCL register is in reset state.

		 * Set bcl_enabled to false to skip KMD's HFI request

		 * to GMU for BCL feature, send BCL feature request to

		 * GMU after ZAP load at GPU boot. This ensures that

		 * Central Broadcast register was programmed before

		 * enabling BCL.

		 */

		adreno_dev->bcl_enabled = false;

		ret = a6xx_hwsched_gmu_first_boot(adreno_dev);

	} else {

		ret = a6xx_hwsched_gmu_boot(adreno_dev);

	}

	if (ret)

		return ret;



	ret = a6xx_hwsched_gpu_boot(adreno_dev);

	if (ret)

		return ret;



	kgsl_start_idle_timer(device);



	kgsl_pwrscale_wake(device);



	set_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	if (IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION))

		adreno_dev->bcl_enabled = bcl_state;



	device->pwrctrl.last_stat_updated = ktime_get();



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



	return ret;

}



static int a6xx_hwsched_first_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	if (test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags))

		return a6xx_hwsched_boot(adreno_dev);



	if (adreno_preemption_feature_set(adreno_dev))

		set_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);



	adreno_hwsched_start(adreno_dev);



	ret = a6xx_microcode_read(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_init(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_hwsched_gmu_init(adreno_dev);

	if (ret)

		return ret;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	ret = a6xx_hwsched_gmu_first_boot(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_hwsched_gpu_boot(adreno_dev);

	if (ret)

		return ret;



	adreno_get_bus_counters(adreno_dev);



	adreno_dev->cooperative_reset = ADRENO_FEATURE(adreno_dev,

						 ADRENO_COOP_RESET);



	set_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags);

	set_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	/*

	 * BCL needs respective Central Broadcast register to

	 * be programed from TZ. This programing happens only

	 * when zap shader firmware load is successful. Zap firmware

	 * load can fail in boot up path hence enable BCL only after we

	 * successfully complete first boot to ensure that Central

	 * Broadcast register was programed before enabling BCL.

	 */

	if (ADRENO_FEATURE(adreno_dev, ADRENO_BCL))

		adreno_dev->bcl_enabled = true;



	/*

	 * There is a possible deadlock scenario during kgsl firmware reading

	 * (firmware_request_nowarn) and devfreq update calls. During first boot, kgsl

	 * device mutex is held and then firmware_request_nowarn is called for reading

	 * firmware. firmware_request_nowarn internally takes dev_pm_qos_mtx lock.

	 * Whereas in case of devfreq update calls triggered by thermal/bcl or

	 * devfreq sysfs, it first takes the same dev_pm_qos_mtx lock and then

	 * tries to take kgsl device mutex as part of get_dev_status/target

	 * calls. This results in deadlock when both thread are unable to acquire

	 * the mutex held by other thread. Enable devfreq updates now as we are

	 * done reading all firmware files.

	 */

	device->pwrscale.devfreq_enabled = true;



	device->pwrctrl.last_stat_updated = ktime_get();



	set_bit(ADRENO_DEVICE_FIRST_BOOT_DONE, &adreno_dev->priv);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);



	return 0;

}



static int a6xx_hwsched_power_off(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	if (!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		return 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);



	ret = a6xx_gmu_oob_set(device, oob_gpu);

	if (ret) {

		a6xx_gmu_oob_clear(device, oob_gpu);

		goto no_gx_power;

	}



	kgsl_pwrscale_update_stats(device);



	/* Save active coresight registers if applicable */

	adreno_coresight_stop(adreno_dev);



	/* Save physical performance counter values before GPU power down*/

	adreno_perfcounter_save(adreno_dev);



	adreno_irqctrl(adreno_dev, 0);



	a6xx_gmu_oob_clear(device, oob_gpu);



no_gx_power:

	kgsl_pwrctrl_irq(device, false);



	a6xx_hwsched_gmu_power_off(adreno_dev);



	adreno_hwsched_unregister_contexts(adreno_dev);



	adreno_llcc_slice_deactivate(adreno_dev);



	clear_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	kgsl_delete_timer_sync(&device->idle_timer);



	kgsl_pwrscale_sleep(device);



	kgsl_pwrctrl_clear_l3_vote(device);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);



	return ret;

}



static void hwsched_idle_check(struct work_struct *work)

{

	struct kgsl_device *device = container_of(work,

					struct kgsl_device, idle_check_ws);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	kgsl_mutex_lock(&device->mutex);



	if (test_bit(GMU_DISABLE_SLUMBER, &device->gmu_core.flags))

		goto done;



	if (atomic_read(&device->active_cnt) || time_is_after_jiffies(device->idle_jiffies)) {

		kgsl_pwrscale_update(device);

		goto done;

	}



	spin_lock(&device->submit_lock);

	if (device->submit_now) {

		spin_unlock(&device->submit_lock);

		kgsl_pwrscale_update(device);

		kgsl_start_idle_timer(device);

		goto done;

	}



	device->skip_inline_submit = true;

	spin_unlock(&device->submit_lock);



	if (!gpudev->hw_isidle(adreno_dev)) {

		dev_err(device->dev, "GPU isn't idle before SLUMBER\n");

		gmu_core_fault_snapshot(device, GMU_FAULT_PANIC_NONE);

	}



	a6xx_hwsched_power_off(adreno_dev);



done:

	kgsl_mutex_unlock(&device->mutex);

}



static int a6xx_hwsched_first_open(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	/*

	 * Do the one time settings that need to happen when we

	 * attempt to boot the gpu the very first time

	 */

	ret = a6xx_hwsched_first_boot(adreno_dev);

	if (ret)

		return ret;



	/*

	 * A client that does a first_open but never closes the device

	 * may prevent us from going back to SLUMBER. So trigger the idle

	 * check by incrementing the active count and immediately releasing it.

	 */

	atomic_inc(&device->active_cnt);

	adreno_active_count_put(adreno_dev);



	return 0;

}



static int a6xx_hwsched_active_count_get(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret = 0;



	if (WARN_ON(!kgsl_mutex_is_locked(&device->mutex)))

		return -EINVAL;



	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags))

		return -EINVAL;



	if ((atomic_read(&device->active_cnt) == 0))

		ret = a6xx_hwsched_boot(adreno_dev);



	if (ret == 0)

		atomic_inc(&device->active_cnt);



	trace_kgsl_active_count(device,

		(unsigned long) __builtin_return_address(0));



	return ret;

}



static int a6xx_hwsched_dcvs_set(struct adreno_device *adreno_dev,

		int gpu_pwrlevel, int bus_level)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_dcvstable_cmd *table = &gmu->hfi.dcvs_table;

	struct hfi_gx_bw_perf_vote_cmd req = {

		.ack_type = DCVS_ACK_BLOCK,

		.freq = INVALID_DCVS_IDX,

		.bw = INVALID_DCVS_IDX,

	};

	int ret;



	if (!test_bit(GMU_PRIV_HFI_STARTED, &gmu->flags))

		return 0;



	/* Do not set to XO and lower GPU clock vote from GMU */

	if ((gpu_pwrlevel != INVALID_DCVS_IDX) &&

			(gpu_pwrlevel >= table->gpu_level_num - 1)) {

		dev_err(GMU_PDEV_DEV(device), "Invalid gpu dcvs request: %d\n",

			gpu_pwrlevel);

		return -EINVAL;

	}



	if (gpu_pwrlevel < table->gpu_level_num - 1)

		req.freq = table->gpu_level_num - gpu_pwrlevel - 1;



	if (bus_level < pwr->ddr_table_count && bus_level > 0)

		req.bw = bus_level;



	/* GMU will vote for slumber levels through the sleep sequence */

	if ((req.freq == INVALID_DCVS_IDX) && (req.bw == INVALID_DCVS_IDX))

		return 0;



	ret = CMD_MSG_HDR(req, H2F_MSG_GX_BW_PERF_VOTE);

	if (ret)

		return ret;



	ret = a6xx_hfi_send_cmd_async(adreno_dev, &req, sizeof(req));



	if (ret) {

		dev_err_ratelimited(GMU_PDEV_DEV(device),

			"Failed to set GPU perf idx %u, bw idx %u\n",

			req.freq, req.bw);



		/*

		 * If this was a dcvs request along side an active gpu, request

		 * dispatcher based reset and recovery.

		 */

		if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

			adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);

	}



	if (req.freq != INVALID_DCVS_IDX)

		gmu_core_rdpm_mx_freq_update(device, table->gx_votes[req.freq].freq);



	return ret;

}



static int a6xx_hwsched_clock_set(struct adreno_device *adreno_dev,

	u32 pwrlevel)

{

	return a6xx_hwsched_dcvs_set(adreno_dev, pwrlevel, INVALID_DCVS_IDX);

}



static int a6xx_hwsched_bus_set(struct adreno_device *adreno_dev, int buslevel,

	u32 ab)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	int ret = 0;



	kgsl_icc_set_tag(pwr, buslevel);



	if (buslevel != pwr->cur_buslevel) {

		ret = a6xx_hwsched_dcvs_set(adreno_dev, INVALID_DCVS_IDX,

				buslevel);

		if (ret)

			return ret;



		gmu_core_scale_gmu_frequency(device, buslevel);



		pwr->cur_buslevel = buslevel;

	}



	if (ab != pwr->cur_ab) {

		icc_set_bw(pwr->icc_path, MBps_to_icc(ab), 0);

		pwr->cur_ab = ab;

	}



	trace_kgsl_buslevel(device, pwr->active_pwrlevel, pwr->cur_buslevel, pwr->cur_ab, 0);

	return ret;

}



static int a6xx_hwsched_pm_suspend(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret, active_count;



	if (test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags))

		return 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND);



	ret = adreno_hwsched_drain_and_idle(adreno_dev);

	if (ret)

		goto err;



	active_count = atomic_read(&device->active_cnt);



	if (active_count > 0) {

		ret = -ETIMEDOUT;

		dev_err_ratelimited(GMU_PDEV_DEV(device),

			"Aborting suspend because of active count:%d\n",

			active_count);

		goto err;

	}



	a6xx_hwsched_power_off(adreno_dev);



	adreno_get_gpu_halt(adreno_dev);



	set_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);



	return 0;



err:

	adreno_hwsched_start(adreno_dev);



	return ret;

}



void a6xx_hwsched_handle_watchdog(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 mask;



	/* Temporarily mask the watchdog interrupt to prevent a storm */

	gmu_core_regread(device, A6XX_GMU_AO_HOST_INTERRUPT_MASK,

		&mask);

	gmu_core_regwrite(device, A6XX_GMU_AO_HOST_INTERRUPT_MASK,

			(mask | GMU_INT_WDOG_BITE));



	a6xx_gmu_send_nmi(device, false, GMU_FAULT_PANIC_NONE);



	dev_err_ratelimited(GMU_PDEV_DEV(device),

			"GMU watchdog expired interrupt received\n");



	adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);

}



static void a6xx_hwsched_pm_resume(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);



	if (WARN(!test_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags),

		"resume invoked without a suspend\n"))

		return;



	adreno_put_gpu_halt(adreno_dev);



	adreno_hwsched_start(adreno_dev);



	clear_bit(GMU_PRIV_PM_SUSPEND, &gmu->flags);

}



static void a6xx_hwsched_drain_ctxt_unregister(struct adreno_device *adreno_dev)

{

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct pending_cmd *cmd = NULL;



	read_lock(&hfi->msglock);



	list_for_each_entry(cmd, &hfi->msglist, node) {

		if (MSG_HDR_GET_ID(cmd->sent_hdr) == H2F_MSG_UNREGISTER_CONTEXT)

			complete(&cmd->complete);

	}



	read_unlock(&hfi->msglock);

}



int a6xx_hwsched_reset_replay(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	int ret;



	/*

	 * Any pending context unregister packets will be lost

	 * since we hard reset the GMU. This means any threads waiting

	 * for context unregister hfi ack will timeout. Wake them

	 * to avoid false positive ack timeout messages later.

	 */

	a6xx_hwsched_drain_ctxt_unregister(adreno_dev);



	adreno_hwsched_unregister_contexts(adreno_dev);



	if (!test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		return 0;



	a6xx_disable_gpu_irq(adreno_dev);



	a6xx_gmu_irq_disable(adreno_dev);



	a6xx_hwsched_hfi_stop(adreno_dev);



	a6xx_hwsched_gmu_suspend(adreno_dev, true);



	adreno_llcc_slice_deactivate(adreno_dev);



	clear_bit(GMU_PRIV_GPU_STARTED, &gmu->flags);



	ret = a6xx_hwsched_boot(adreno_dev);



	if (!ret)

		adreno_hwsched_replay(adreno_dev);



	BUG_ON(ret);



	return ret;

}



const struct adreno_power_ops a6xx_hwsched_power_ops = {

	.first_open = a6xx_hwsched_first_open,

	.last_close = a6xx_hwsched_power_off,

	.active_count_get = a6xx_hwsched_active_count_get,

	.touch_wakeup = a6xx_hwsched_touch_wakeup,

	.pm_suspend = a6xx_hwsched_pm_suspend,

	.pm_resume = a6xx_hwsched_pm_resume,

	.gpu_clock_set = a6xx_hwsched_clock_set,

	.gpu_bus_set = a6xx_hwsched_bus_set,

};



const struct adreno_hwsched_ops a6xx_hwsched_ops = {

	.submit_drawobj = a6xx_hwsched_submit_drawobj,

	.preempt_count = a6xx_hwsched_preempt_count_get,

};



int a6xx_hwsched_probe(struct platform_device *pdev,

	u32 chipid, const struct adreno_gpu_core *gpucore)

{

	struct adreno_device *adreno_dev;

	struct kgsl_device *device;

	struct a6xx_hwsched_device *a6xx_hwsched_dev;

	int ret;



	a6xx_hwsched_dev = devm_kzalloc(&pdev->dev, sizeof(*a6xx_hwsched_dev),

				GFP_KERNEL);

	if (!a6xx_hwsched_dev)

		return -ENOMEM;



	adreno_dev = &a6xx_hwsched_dev->a6xx_dev.adreno_dev;



	adreno_dev->hwsched_enabled = true;



	adreno_dev->irq_mask = A6XX_HWSCHED_INT_MASK;



	ret = a6xx_probe_common(pdev, adreno_dev, chipid, gpucore);

	if (ret)

		return ret;



	device = KGSL_DEVICE(adreno_dev);



	INIT_WORK(&device->idle_check_ws, hwsched_idle_check);



	timer_setup(&device->idle_timer, hwsched_idle_timer, 0);



	return adreno_hwsched_init(adreno_dev, &a6xx_hwsched_ops);

}



int a6xx_hwsched_add_to_minidump(struct adreno_device *adreno_dev)

{

	struct a6xx_device *a6xx_dev = container_of(adreno_dev,

					struct a6xx_device, adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gmu_core_device *gmu_core = &device->gmu_core;

	struct a6xx_hwsched_device *a6xx_hwsched = container_of(a6xx_dev,

					struct a6xx_hwsched_device, a6xx_dev);

	struct a6xx_hwsched_hfi *hw_hfi = &a6xx_hwsched->hwsched_hfi;

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	int ret, i;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_HWSCHED_DEVICE,

			(void *)(a6xx_hwsched), sizeof(struct a6xx_hwsched_device));

	if (ret)

		return ret;



	if (!IS_ERR_OR_NULL(a6xx_dev->gmu.gmu_log)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					KGSL_GMU_LOG_ENTRY,

					a6xx_dev->gmu.gmu_log->hostptr,

					a6xx_dev->gmu.gmu_log->size);

		if (ret)

			return ret;

	}



	if (!IS_ERR_OR_NULL(a6xx_dev->gmu.hfi.hfi_mem)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					KGSL_HFIMEM_ENTRY,

					a6xx_dev->gmu.hfi.hfi_mem->hostptr,

					a6xx_dev->gmu.hfi.hfi_mem->size);

		if (ret)

			return ret;

	}



	if (!IS_ERR_OR_NULL(gmu_core->vrb)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					KGSL_GMU_VRB_ENTRY,

					gmu_core->vrb->hostptr,

					gmu_core->vrb->size);

		if (ret)

			return ret;

	}



	if (!IS_ERR_OR_NULL(a6xx_dev->gmu.dump_mem)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					KGSL_GMU_DUMPMEM_ENTRY,

					a6xx_dev->gmu.dump_mem->hostptr,

					a6xx_dev->gmu.dump_mem->size);

		if (ret)

			return ret;

	}



	if (!IS_ERR_OR_NULL(gmu_core->trace.md)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					KGSL_GMU_TRACE_ENTRY,

					gmu_core->trace.md->hostptr,

					gmu_core->trace.md->size);

		if (ret)

			return ret;

	}



	/* Dump HFI hwsched global mem alloc entries */

	for (i = 0; i < hwsched->mem_alloc_entries; i++) {

		struct hfi_mem_alloc_entry *entry = &hwsched->mem_alloc_table[i];

		char hfi_minidump_str[MAX_VA_MINIDUMP_STR_LEN] = {0};

		char name[MAX_VA_MINIDUMP_STR_LEN];

		u32 rb_id = 0;



		if (!hfi_get_minidump_string(entry->desc.mem_kind,

					     &hfi_minidump_str[0],

					     sizeof(hfi_minidump_str), &rb_id)) {

			snprintf(name, sizeof(name), "kgsl_global_%s", hfi_minidump_str);

			ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

						      name,

						      entry->md->hostptr,

						      entry->md->size);

			if (ret)

				return ret;

		}

	}



	if (!IS_ERR_OR_NULL(hw_hfi->big_ib)) {

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					      KGSL_HFI_BIG_IB_ENTRY,

					      hw_hfi->big_ib->hostptr,

					      hw_hfi->big_ib->size);

		if (ret)

			return ret;

	}



	if (!IS_ERR_OR_NULL(hw_hfi->big_ib_recurring))

		ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev,

					      KGSL_HFI_BIG_IB_REC_ENTRY,

					      hw_hfi->big_ib_recurring->hostptr,

					      hw_hfi->big_ib_recurring->size);



	return ret;

}





==========================================================================================
FILE: adreno_a6xx_hwsched_hfi.c
LINES: 1785 | BYTES: 51247
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include <linux/iommu.h>



#include "adreno.h"

#include "adreno_a6xx.h"

#include "adreno_a6xx_hwsched.h"

#include "adreno_hfi.h"

#include "adreno_perfcounter.h"

#include "adreno_pm4types.h"

#include "adreno_trace.h"

#include "kgsl_device.h"

#include "kgsl_eventlog.h"

#include "kgsl_gmu_core.h"

#include "kgsl_pwrctrl.h"

#include "kgsl_trace.h"

#include "kgsl_util.h"



#define HFI_QUEUE_MAX (HFI_QUEUE_DEFAULT_CNT + HFI_QUEUE_DISPATCH_MAX_CNT)



#define DEFINE_QHDR(gmuaddr, id, prio) \

	{\

		.status = 1, \

		.start_addr = GMU_QUEUE_START_ADDR(gmuaddr, id), \

		.type = QUEUE_HDR_TYPE(id, prio, 0, 0), \

		.queue_size = SZ_4K >> 2, \

		.msg_size = 0, \

		.unused0 = 0, \

		.unused1 = 0, \

		.unused2 = 0, \

		.unused3 = 0, \

		.unused4 = 0, \

		.read_index = 0, \

		.write_index = 0, \

}



static struct dq_info {

	/** @max_dq: Maximum number of dispatch queues per RB level */

	u32 max_dq;

	/** @base_dq_id: Base dqid for level */

	u32 base_dq_id;

	/** @offset: Next dqid to use for roundrobin context assignment */

	u32 offset;

} a6xx_hfi_dqs[KGSL_PRIORITY_MAX_RB_LEVELS] = {

	{ 4, 0, }, /* RB0 */

	{ 4, 4, }, /* RB1 */

	{ 3, 8, }, /* RB2 */

	{ 3, 11, }, /* RB3 */

};



static int a6xx_hfi_dispatch_queue_write(struct adreno_device *adreno_dev, uint32_t queue_idx,

	uint32_t *msg, u32 size_bytes, struct kgsl_drawobj_cmd *cmdobj,

	struct adreno_submit_time *time);



struct a6xx_hwsched_hfi *to_a6xx_hwsched_hfi(

	struct adreno_device *adreno_dev)

{

	struct a6xx_device *a6xx_dev = container_of(adreno_dev,

					struct a6xx_device, adreno_dev);

	struct a6xx_hwsched_device *a6xx_hwsched = container_of(a6xx_dev,

					struct a6xx_hwsched_device, a6xx_dev);



	return &a6xx_hwsched->hwsched_hfi;

}



static void add_waiter(struct a6xx_hwsched_hfi *hfi, u32 hdr,

	struct pending_cmd *ack)

{

	memset(ack, 0x0, sizeof(*ack));



	init_completion(&ack->complete);

	write_lock_irq(&hfi->msglock);

	list_add_tail(&ack->node, &hfi->msglist);

	write_unlock_irq(&hfi->msglock);



	ack->sent_hdr = hdr;

}



static void del_waiter(struct a6xx_hwsched_hfi *hfi, struct pending_cmd *ack)

{

	write_lock_irq(&hfi->msglock);

	list_del(&ack->node);

	write_unlock_irq(&hfi->msglock);

}



static void a6xx_receive_ack_async(struct adreno_device *adreno_dev, void *rcvd)

{

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));

	struct pending_cmd *cmd = NULL;

	u32 waiters[64], num_waiters = 0, i;

	u32 *ack = rcvd;

	u32 hdr = ack[0];

	u32 req_hdr = ack[1];

	u32 size_bytes = MSG_HDR_GET_SIZE(hdr) << 2;



	if (size_bytes > sizeof(cmd->results))

		dev_err_ratelimited(gmu_pdev_dev,

			"Ack result too big: %d Truncating to: %ld\n",

			size_bytes, sizeof(cmd->results));



	read_lock(&hfi->msglock);



	list_for_each_entry(cmd, &hfi->msglist, node) {

		if (CMP_HFI_ACK_HDR(cmd->sent_hdr, req_hdr)) {

			memcpy(cmd->results, ack,

				min_t(u32, size_bytes,

					sizeof(cmd->results)));

			complete(&cmd->complete);

			read_unlock(&hfi->msglock);

			return;

		}



		if (num_waiters < ARRAY_SIZE(waiters))

			waiters[num_waiters++] = cmd->sent_hdr;

	}



	read_unlock(&hfi->msglock);



	/* Didn't find the sender, list the waiter */

	dev_err_ratelimited(gmu_pdev_dev,

		"Unexpectedly got id %d seqnum %d. Total waiters: %d Top %d Waiters:\n",

		MSG_HDR_GET_ID(req_hdr), MSG_HDR_GET_SEQNUM(req_hdr),

		num_waiters, min_t(u32, num_waiters, 5));



	for (i = 0; i < num_waiters && i < 5; i++)

		dev_err_ratelimited(gmu_pdev_dev,

			" id %d seqnum %d\n",

			MSG_HDR_GET_ID(waiters[i]),

			MSG_HDR_GET_SEQNUM(waiters[i]));

}



/* Look up a particular key's value for a given type of payload */

static u32 a6xx_hwsched_lookup_key_value_legacy(struct adreno_device *adreno_dev,

	u32 type, u32 key)

{

	struct hfi_context_bad_cmd_legacy *cmd = adreno_dev->hwsched.ctxt_bad;

	u32 i = 0, payload_bytes;

	void *start;



	if (!cmd->hdr)

		return 0;



	payload_bytes = (MSG_HDR_GET_SIZE(cmd->hdr) << 2) -

			offsetof(struct hfi_context_bad_cmd_legacy, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == type)

			return adreno_hwsched_parse_payload(payload, key);



		i += struct_size(payload, data, payload->dwords);

	}



	return 0;

}



static void log_gpu_fault_legacy(struct adreno_device *adreno_dev)

{

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));

	struct hfi_context_bad_cmd_legacy *cmd = adreno_dev->hwsched.ctxt_bad;



	switch (cmd->error) {

	case GMU_GPU_HW_HANG:

		dev_crit_ratelimited(gmu_pdev_dev, "MISC: GPU hang detected\n");

		break;

	case GMU_GPU_SW_HANG:

		dev_crit_ratelimited(gmu_pdev_dev, "gpu timeout ctx %u ts %u\n",

			cmd->ctxt_id, cmd->ts);

		break;

	case GMU_CP_OPCODE_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev,

			"CP opcode error interrupt | opcode=0x%8.8x\n",

			a6xx_hwsched_lookup_key_value_legacy(adreno_dev, PAYLOAD_FAULT_REGS,

			KEY_CP_OPCODE_ERROR));

		break;

	case GMU_CP_PROTECTED_ERROR: {

		u32 status = a6xx_hwsched_lookup_key_value_legacy(adreno_dev, PAYLOAD_FAULT_REGS,

				KEY_CP_PROTECTED_ERROR);



		dev_crit_ratelimited(gmu_pdev_dev,

			"CP | Protected mode error | %s | addr=0x%5.5x | status=0x%8.8x\n",

			status & (1 << 20) ? "READ" : "WRITE",

			status & 0x3FFFF, status);

		}

		break;

	case GMU_CP_ILLEGAL_INST_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "CP Illegal instruction error\n");

		break;

	case GMU_CP_UCODE_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "CP ucode error interrupt\n");

		break;

	case GMU_CP_HW_FAULT_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev,

			"CP | Ringbuffer HW fault | status=0x%8.8x\n",

			a6xx_hwsched_lookup_key_value_legacy(adreno_dev, PAYLOAD_FAULT_REGS,

				KEY_CP_HW_FAULT));

		break;

	case GMU_GPU_PREEMPT_TIMEOUT: {

		u32 cur, next, cur_rptr, cur_wptr, next_rptr, next_wptr;



		cur = a6xx_hwsched_lookup_key_value_legacy(adreno_dev,

			PAYLOAD_PREEMPT_TIMEOUT, KEY_PREEMPT_TIMEOUT_CUR_RB_ID);

		next = a6xx_hwsched_lookup_key_value_legacy(adreno_dev,

			PAYLOAD_PREEMPT_TIMEOUT,

			KEY_PREEMPT_TIMEOUT_NEXT_RB_ID);

		cur_rptr = adreno_hwsched_get_payload_rb_key_legacy(adreno_dev, cur, KEY_RB_RPTR);

		cur_wptr = adreno_hwsched_get_payload_rb_key_legacy(adreno_dev, cur, KEY_RB_WPTR);

		next_rptr = adreno_hwsched_get_payload_rb_key_legacy(adreno_dev, next, KEY_RB_RPTR);

		next_wptr = adreno_hwsched_get_payload_rb_key_legacy(adreno_dev, next, KEY_RB_WPTR);



		dev_crit_ratelimited(gmu_pdev_dev,

			"Preemption Fault: cur=%d R/W=0x%x/0x%x, next=%d R/W=0x%x/0x%x\n",

			cur, cur_rptr, cur_wptr, next, next_rptr, next_wptr);

		}

		break;

	case GMU_CP_GPC_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "RBBM: GPC error\n");

		break;

	default:

		dev_crit_ratelimited(gmu_pdev_dev, "Unknown GPU fault: %u\n",

			cmd->error);

		break;

	}

}



/* Look up a particular key's value for a given type of payload */

static u32 a6xx_hwsched_lookup_key_value(struct adreno_device *adreno_dev,

	u32 type, u32 key)

{

	struct hfi_context_bad_cmd *cmd = adreno_dev->hwsched.ctxt_bad;

	u32 i = 0, payload_bytes;

	void *start;



	if (!cmd->hdr)

		return 0;



	payload_bytes = (MSG_HDR_GET_SIZE(cmd->hdr) << 2) -

			offsetof(struct hfi_context_bad_cmd, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == type)

			return adreno_hwsched_parse_payload(payload, key);



		i += struct_size(payload, data, payload->dwords);

	}



	return 0;

}



static void log_gpu_fault(struct adreno_device *adreno_dev)

{

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));

	struct hfi_context_bad_cmd *cmd = adreno_dev->hwsched.ctxt_bad;



	switch (cmd->error) {

	case GMU_GPU_HW_HANG:

		dev_crit_ratelimited(gmu_pdev_dev, "MISC: GPU hang detected\n");

		break;

	case GMU_GPU_SW_HANG:

		dev_crit_ratelimited(gmu_pdev_dev, "gpu timeout ctx %u ts %u\n",

			cmd->gc.ctxt_id, cmd->gc.ts);

		break;

	case GMU_CP_OPCODE_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev,

			"CP opcode error interrupt | opcode=0x%8.8x\n",

			a6xx_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,

			KEY_CP_OPCODE_ERROR));

		break;

	case GMU_CP_PROTECTED_ERROR: {

		u32 status = a6xx_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,

				KEY_CP_PROTECTED_ERROR);



		dev_crit_ratelimited(gmu_pdev_dev,

			"CP | Protected mode error | %s | addr=0x%5.5x | status=0x%8.8x\n",

			status & (1 << 20) ? "READ" : "WRITE",

			status & 0x3FFFF, status);

		}

		break;

	case GMU_CP_ILLEGAL_INST_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "CP Illegal instruction error\n");

		break;

	case GMU_CP_UCODE_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "CP ucode error interrupt\n");

		break;

	case GMU_CP_HW_FAULT_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev,

			"CP | Ringbuffer HW fault | status=0x%8.8x\n",

			a6xx_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,

				KEY_CP_HW_FAULT));

		break;

	case GMU_GPU_PREEMPT_TIMEOUT: {

		u32 cur, next, cur_rptr, cur_wptr, next_rptr, next_wptr;



		cur = a6xx_hwsched_lookup_key_value(adreno_dev,

			PAYLOAD_PREEMPT_TIMEOUT, KEY_PREEMPT_TIMEOUT_CUR_RB_ID);

		next = a6xx_hwsched_lookup_key_value(adreno_dev,

			PAYLOAD_PREEMPT_TIMEOUT,

			KEY_PREEMPT_TIMEOUT_NEXT_RB_ID);

		cur_rptr = adreno_hwsched_get_payload_rb_key(adreno_dev, cur, KEY_RB_RPTR);

		cur_wptr = adreno_hwsched_get_payload_rb_key(adreno_dev, cur, KEY_RB_WPTR);

		next_rptr = adreno_hwsched_get_payload_rb_key(adreno_dev, next, KEY_RB_RPTR);

		next_wptr = adreno_hwsched_get_payload_rb_key(adreno_dev, next, KEY_RB_WPTR);



		dev_crit_ratelimited(gmu_pdev_dev,

			"Preemption Fault: cur=%d R/W=0x%x/0x%x, next=%d R/W=0x%x/0x%x\n",

			cur, cur_rptr, cur_wptr, next, next_rptr, next_wptr);

		}

		break;

	case GMU_CP_GPC_ERROR:

		dev_crit_ratelimited(gmu_pdev_dev, "RBBM: GPC error\n");

		break;

	default:

		dev_crit_ratelimited(gmu_pdev_dev, "Unknown GPU fault: %u\n",

			cmd->error);

		break;

	}

}



static void process_ctx_bad(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (GMU_VER_MINOR(device->gmu_core.ver.hfi) < 2)

		log_gpu_fault_legacy(adreno_dev);

	else

		log_gpu_fault(adreno_dev);



	adreno_scheduler_fault(adreno_dev, ADRENO_HARD_FAULT);

}



static u32 peek_next_header(struct a6xx_gmu_device *gmu, uint32_t queue_idx)

{

	struct kgsl_memdesc *mem_addr = gmu->hfi.hfi_mem;

	struct hfi_queue_table *tbl = mem_addr->hostptr;

	struct hfi_queue_header *hdr = &tbl->qhdr[queue_idx];

	u32 *queue;



	if (hdr->status == HFI_QUEUE_STATUS_DISABLED)

		return 0;



	if (hdr->read_index == hdr->write_index)

		return 0;



	queue = HOST_QUEUE_START_ADDR(mem_addr, queue_idx);



	return queue[hdr->read_index];

}



static void a6xx_hwsched_process_msgq(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hwsched_hfi *hw_hfi = to_a6xx_hwsched_hfi(adreno_dev);

	u32 rcvd[MAX_RCVD_SIZE], next_hdr;



	if (!(hw_hfi->irq_mask & HFI_IRQ_MSGQ_MASK))

		return;



	mutex_lock(&hw_hfi->msgq_mutex);



	for (;;) {

		next_hdr = peek_next_header(gmu, HFI_MSG_ID);



		if (!next_hdr)

			break;



		if (MSG_HDR_GET_ID(next_hdr) == F2H_MSG_CONTEXT_BAD) {

			a6xx_hfi_queue_read(gmu, HFI_MSG_ID,

				(u32 *)adreno_dev->hwsched.ctxt_bad,

				HFI_MAX_MSG_SIZE);

			process_ctx_bad(adreno_dev);

			continue;

		}



		a6xx_hfi_queue_read(gmu, HFI_MSG_ID, rcvd, sizeof(rcvd));



		/*

		 * We are assuming that there is only one outstanding ack

		 * because hfi sending thread waits for completion while

		 * holding the device mutex

		 */

		if (MSG_HDR_GET_TYPE(rcvd[0]) == HFI_MSG_ACK) {

			a6xx_receive_ack_async(adreno_dev, rcvd);

		} else if (MSG_HDR_GET_ID(rcvd[0]) == F2H_MSG_TS_RETIRE) {

			adreno_scheduler_queue(adreno_dev);

			adreno_hwsched_log_profiling_info(adreno_dev, rcvd);

		} else if (MSG_HDR_GET_ID(rcvd[0]) == F2H_MSG_GMU_CNTR_RELEASE) {

			struct hfi_gmu_cntr_release_cmd *cmd =

				(struct hfi_gmu_cntr_release_cmd *) rcvd;



			adreno_perfcounter_put(adreno_dev,

				cmd->group_id, cmd->countable, PERFCOUNTER_FLAG_KERNEL);

		}

	}

	mutex_unlock(&hw_hfi->msgq_mutex);

}



static void process_log_block(struct adreno_device *adreno_dev, void *data)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_log_block *cmd = data;

	u32 *log_event = gmu->gmu_log->hostptr;

	u32 start, end;



	start = cmd->start_index;

	end = cmd->stop_index;



	log_event += start * 4;

	while (start != end) {

		trace_gmu_event(log_event);

		log_event += 4;

		start++;

	}

}



static void a6xx_hwsched_process_dbgq(struct adreno_device *adreno_dev, bool limited)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	u32 rcvd[MAX_RCVD_SIZE];

	bool recovery = false;



	while (a6xx_hfi_queue_read(gmu, HFI_DBG_ID, rcvd, sizeof(rcvd)) > 0) {



		if (MSG_HDR_GET_ID(rcvd[0]) == F2H_MSG_ERR) {

			adreno_a6xx_receive_err_req(gmu, rcvd);

			recovery = true;

			break;

		}



		if (MSG_HDR_GET_ID(rcvd[0]) == F2H_MSG_DEBUG)

			adreno_a6xx_receive_debug_req(gmu, rcvd);



		if (MSG_HDR_GET_ID(rcvd[0]) == F2H_MSG_LOG_BLOCK)

			process_log_block(adreno_dev, rcvd);



		/* Process one debug queue message and return to not delay msgq processing */

		if (limited)

			break;

	}



	if (!recovery)

		return;



	adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);

}



/* HFI interrupt handler */

static irqreturn_t a6xx_hwsched_hfi_handler(int irq, void *data)

{

	struct adreno_device *adreno_dev = data;

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 status = 0;



	/*

	 * A6XX_GMU_GMU2HOST_INTR_INFO may have bits set not specified in hfi->irq_mask.

	 * Read and clear only those irq bits that we are processing here.

	 */

	gmu_core_regread(device, A6XX_GMU_GMU2HOST_INTR_INFO, &status);

	gmu_core_regwrite(device, A6XX_GMU_GMU2HOST_INTR_CLR, status & hfi->irq_mask);



	/*

	 * If interrupts are not enabled on the HFI message queue,

	 * the inline message processing loop will process it,

	 * else, process it here.

	 */

	if (!(hfi->irq_mask & HFI_IRQ_MSGQ_MASK))

		status &= ~HFI_IRQ_MSGQ_MASK;



	if (status & (HFI_IRQ_MSGQ_MASK | HFI_IRQ_DBGQ_MASK)) {

		wake_up_interruptible(&hfi->f2h_wq);

		adreno_scheduler_queue(adreno_dev);

	}

	if (status & HFI_IRQ_CM3_FAULT_MASK) {

		atomic_set(&gmu->cm3_fault, 1);



		/* make sure other CPUs see the update */

		smp_wmb();



		dev_err_ratelimited(GMU_PDEV_DEV(device),

				"GMU CM3 fault interrupt received\n");



		adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);

	}



	/* Ignore OOB bits */

	status &= GENMASK(31 - (oob_max - 1), 0);



	if (status & ~hfi->irq_mask)

		dev_err_ratelimited(GMU_PDEV_DEV(device),

			"Unhandled HFI interrupts 0x%x\n",

			status & ~hfi->irq_mask);



	return IRQ_HANDLED;

}



#define HFI_IRQ_MSGQ_MASK BIT(0)



static int check_ack_failure(struct adreno_device *adreno_dev,

	struct pending_cmd *ack)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u64 ticks = gpudev->read_alwayson(adreno_dev);



	if (ack->results[2] != 0xffffffff)

		return 0;



	dev_err(GMU_PDEV_DEV(device),

		"ACK error: sender id %d seqnum %d\n",

		MSG_HDR_GET_ID(ack->sent_hdr),

		MSG_HDR_GET_SEQNUM(ack->sent_hdr));



	KGSL_GMU_CORE_FORCE_PANIC(device->gmu_core.gf_panic,

				GMU_PDEV(device), ticks, GMU_FAULT_HFI_ACK);

	return -EINVAL;

}



int a6xx_hfi_send_cmd_async(struct adreno_device *adreno_dev, void *data, u32 size_bytes)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 *cmd = data;

	u32 seqnum;

	int rc;

	struct pending_cmd pending_ack;



	seqnum = atomic_inc_return(&gmu->hfi.seqnum);

	*cmd = MSG_HDR_SET_SEQNUM_SIZE(*cmd, seqnum, size_bytes >> 2);



	add_waiter(hfi, *cmd, &pending_ack);



	rc = a6xx_hfi_cmdq_write(adreno_dev, cmd, size_bytes);

	if (rc)

		goto done;



	rc = adreno_hwsched_wait_ack_completion(adreno_dev,

		GMU_PDEV_DEV(device), &pending_ack, a6xx_hwsched_process_msgq);

	if (rc)

		goto done;



	rc = check_ack_failure(adreno_dev, &pending_ack);



done:

	del_waiter(hfi, &pending_ack);



	return rc;

}



static void init_queues(struct a6xx_hfi *hfi)

{

	u32 gmuaddr = hfi->hfi_mem->gmuaddr;

	struct hfi_queue_table hfi_table = {

		.qtbl_hdr = {

			.version = 0,

			.size = sizeof(struct hfi_queue_table) >> 2,

			.qhdr0_offset =

				sizeof(struct hfi_queue_table_header) >> 2,

			.qhdr_size = sizeof(struct hfi_queue_header) >> 2,

			.num_q = HFI_QUEUE_MAX,

			.num_active_q = HFI_QUEUE_MAX,

		},

		.qhdr = {

			DEFINE_QHDR(gmuaddr, HFI_CMD_ID, 0),

			DEFINE_QHDR(gmuaddr, HFI_MSG_ID, 0),

			DEFINE_QHDR(gmuaddr, HFI_DBG_ID, 0),

			/* 4 DQs for RB priority 0 */

			DEFINE_QHDR(gmuaddr, 3, 0),

			DEFINE_QHDR(gmuaddr, 4, 0),

			DEFINE_QHDR(gmuaddr, 5, 0),

			DEFINE_QHDR(gmuaddr, 6, 0),

			/* 4 DQs for RB priority 1 */

			DEFINE_QHDR(gmuaddr, 7, 1),

			DEFINE_QHDR(gmuaddr, 8, 1),

			DEFINE_QHDR(gmuaddr, 9, 1),

			DEFINE_QHDR(gmuaddr, 10, 1),

			/* 3 DQs for RB priority 2 */

			DEFINE_QHDR(gmuaddr, 11, 2),

			DEFINE_QHDR(gmuaddr, 12, 2),

			DEFINE_QHDR(gmuaddr, 13, 2),

			/* 3 DQs for RB priority 3 */

			DEFINE_QHDR(gmuaddr, 14, 3),

			DEFINE_QHDR(gmuaddr, 15, 3),

			DEFINE_QHDR(gmuaddr, 16, 3),

		},

	};



	memcpy(hfi->hfi_mem->hostptr, &hfi_table, sizeof(hfi_table));

}



/* Total header sizes + queue sizes + 16 for alignment */

#define HFIMEM_SIZE (sizeof(struct hfi_queue_table) + 16 + \

	(SZ_4K * HFI_QUEUE_MAX))



static int hfi_f2h_main(void *arg);



int a6xx_hwsched_hfi_init(struct adreno_device *adreno_dev)

{

	struct a6xx_hwsched_hfi *hw_hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct a6xx_hfi *hfi = to_a6xx_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (IS_ERR_OR_NULL(hw_hfi->big_ib)) {

		hw_hfi->big_ib = gmu_core_reserve_kernel_block(device,

				0,

				HWSCHED_MAX_IBS * sizeof(struct hfi_issue_ib),

				GMU_NONCACHED_KERNEL, 0);

		if (IS_ERR(hw_hfi->big_ib))

			return PTR_ERR(hw_hfi->big_ib);

	}



	if (ADRENO_FEATURE(adreno_dev, ADRENO_LSR) &&

			IS_ERR_OR_NULL(hw_hfi->big_ib_recurring)) {

		hw_hfi->big_ib_recurring = gmu_core_reserve_kernel_block(

				device, 0,

				HWSCHED_MAX_IBS * sizeof(struct hfi_issue_ib),

				GMU_NONCACHED_KERNEL, 0);

		if (IS_ERR(hw_hfi->big_ib_recurring))

			return PTR_ERR(hw_hfi->big_ib_recurring);

	}



	if (IS_ERR_OR_NULL(hfi->hfi_mem)) {

		hfi->hfi_mem = gmu_core_reserve_kernel_block(device,

				0, HFIMEM_SIZE, GMU_NONCACHED_KERNEL, 0);

		if (IS_ERR(hfi->hfi_mem))

			return PTR_ERR(hfi->hfi_mem);

		init_queues(hfi);

	}



	if (IS_ERR_OR_NULL(hw_hfi->f2h_task))

		hw_hfi->f2h_task = kthread_run(hfi_f2h_main, adreno_dev, "gmu_f2h");



	return PTR_ERR_OR_ZERO(hw_hfi->f2h_task);

}



static int mem_alloc_reply(struct adreno_device *adreno_dev, void *rcvd)

{

	struct hfi_mem_alloc_desc desc = {0};

	struct hfi_mem_alloc_reply_cmd out = {0};

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	u32 seqnum;

	int ret;



	hfi_get_mem_alloc_desc(rcvd, &desc);



	ret = adreno_hwsched_process_mem_alloc(adreno_dev, &desc);

	if (ret)

		return ret;



	memcpy(&out.desc, &desc, sizeof(out.desc));



	out.hdr = ACK_MSG_HDR(F2H_MSG_MEM_ALLOC);

	seqnum = atomic_inc_return(&gmu->hfi.seqnum);

	out.hdr = MSG_HDR_SET_SEQNUM_SIZE(out.hdr, seqnum, sizeof(out) >> 2);

	out.req_hdr = *(u32 *)rcvd;



	return a6xx_hfi_cmdq_write(adreno_dev, (u32 *)&out, sizeof(out));

}



static int gmu_cntr_register_reply(struct adreno_device *adreno_dev, void *rcvd)

{

	struct hfi_gmu_cntr_register_cmd *in = (struct hfi_gmu_cntr_register_cmd *)rcvd;

	struct hfi_gmu_cntr_register_reply_cmd out = {0};

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	u32 lo = 0, hi = 0, seqnum;



	/*

	 * Failure to allocate counter is not fatal. Sending lo = 0, hi = 0

	 * indicates to GMU that counter allocation failed.

	 */

	adreno_perfcounter_get(adreno_dev,

		in->group_id, in->countable, &lo, &hi, PERFCOUNTER_FLAG_KERNEL);



	out.hdr = ACK_MSG_HDR(F2H_MSG_GMU_CNTR_REGISTER);

	seqnum = atomic_inc_return(&gmu->hfi.seqnum);

	out.hdr = MSG_HDR_SET_SEQNUM_SIZE(out.hdr, seqnum, sizeof(out) >> 2);

	out.req_hdr = in->hdr;

	out.group_id = in->group_id;

	out.countable = in->countable;

	/* Fill in byte offset of counter */

	out.cntr_lo = lo << 2;

	out.cntr_hi = hi << 2;



	return a6xx_hfi_cmdq_write(adreno_dev, (u32 *)&out, sizeof(out));

}



static int send_start_msg(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(device);

	u32 seqnum;

	int rc;

	struct hfi_start_cmd cmd;

	u32 rcvd[MAX_RCVD_SIZE];

	struct pending_cmd pending_ack = {0};



	rc = CMD_MSG_HDR(cmd, H2F_MSG_START);

	if (rc)

		return rc;



	seqnum = atomic_inc_return(&gmu->hfi.seqnum);

	cmd.hdr = MSG_HDR_SET_SEQNUM_SIZE(cmd.hdr, seqnum, sizeof(cmd) >> 2);



	pending_ack.sent_hdr = cmd.hdr;



	rc = a6xx_hfi_cmdq_write(adreno_dev, (u32 *)&cmd, sizeof(cmd));

	if (rc)

		return rc;



poll:

	rc = adreno_hwsched_poll_msg_queue_write_index(gmu->hfi.hfi_mem);



	if (rc) {

		dev_err(gmu_pdev_dev,

			"Timed out processing MSG_START seqnum: %d\n",

			seqnum);

		gmu_core_fault_snapshot(device, GMU_FAULT_H2F_MSG_START);

		goto done;

	}



	rc = a6xx_hfi_queue_read(gmu, HFI_MSG_ID, rcvd, sizeof(rcvd));

	if (rc <= 0) {

		dev_err(gmu_pdev_dev,

			"MSG_START: payload error: %d\n",

			rc);

		gmu_core_fault_snapshot(device, GMU_FAULT_H2F_MSG_START);

		goto done;

	}



	switch (MSG_HDR_GET_ID(rcvd[0])) {

	case F2H_MSG_MEM_ALLOC:

		rc = mem_alloc_reply(adreno_dev, rcvd);

		break;

	case F2H_MSG_GMU_CNTR_REGISTER:

		rc = gmu_cntr_register_reply(adreno_dev, rcvd);

		break;

	default:

		if (MSG_HDR_GET_TYPE(rcvd[0]) == HFI_MSG_ACK) {

			rc = a6xx_receive_ack_cmd(gmu, rcvd, &pending_ack);

			/* Check ack failure if we received an expected ack */

			if (!rc)

				rc = check_ack_failure(adreno_dev, &pending_ack);

			goto done;

		} else {

			dev_err(gmu_pdev_dev,

				"MSG_START: unexpected response id:%d, type:%d\n",

				MSG_HDR_GET_ID(rcvd[0]),

				MSG_HDR_GET_TYPE(rcvd[0]));

			gmu_core_fault_snapshot(device, GMU_FAULT_H2F_MSG_START);

			rc = -EINVAL;

			goto done;

		}

	}



	if (!rc)

		goto poll;



done:

	/* Clear the interrupt */

	gmu_core_regwrite(device, A6XX_GMU_GMU2HOST_INTR_CLR, HFI_IRQ_MSGQ_MASK);

	/*

	 * Add a write barrier to post the interrupt clear so that we dont have a

	 * pending interrupt.

	 */

	wmb();

	return rc;

}



static void reset_hfi_queues(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_queue_table *tbl = gmu->hfi.hfi_mem->hostptr;

	u32 i;



	/* Flush HFI queues */

	for (i = 0; i < HFI_QUEUE_MAX; i++) {

		struct hfi_queue_header *hdr = &tbl->qhdr[i];



		if (hdr->status == HFI_QUEUE_STATUS_DISABLED)

			continue;



		hdr->read_index = hdr->write_index;

	}

}



void a6xx_hwsched_hfi_stop(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);



	hfi->irq_mask &= ~HFI_IRQ_MSGQ_MASK;



	/*

	 * In some corner cases, it is possible that GMU put TS_RETIRE

	 * on the msgq after we have turned off gmu interrupts. Hence,

	 * drain the queue one last time before we reset HFI queues.

	 */

	a6xx_hwsched_process_msgq(adreno_dev);



	/* Drain the debug queue before we reset HFI queues */

	a6xx_hwsched_process_dbgq(adreno_dev, false);



	kgsl_pwrctrl_axi(KGSL_DEVICE(adreno_dev), false);



	clear_bit(GMU_PRIV_HFI_STARTED, &gmu->flags);

}



static void enable_async_hfi(struct adreno_device *adreno_dev)

{

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);



	hfi->irq_mask |= HFI_IRQ_MSGQ_MASK;



	gmu_core_regwrite(KGSL_DEVICE(adreno_dev), A6XX_GMU_GMU2HOST_INTR_MASK,

		(u32)~hfi->irq_mask);

}



static int enable_preemption(struct adreno_device *adreno_dev)

{

	u32 data;

	int ret;



	if (!adreno_is_preemption_enabled(adreno_dev))

		return 0;



	/*

	 * Bits [0:1] contains the preemption level

	 * Bit 2 is to enable/disable gmem save/restore

	 * Bit 3 is to enable/disable skipsaverestore

	 */

	data = FIELD_PREP(GENMASK(1, 0), adreno_dev->preempt.preempt_level) |

			FIELD_PREP(BIT(2), adreno_dev->preempt.usesgmem) |

			FIELD_PREP(BIT(3), adreno_dev->preempt.skipsaverestore);



	ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_PREEMPTION, 1,

			data);

	if (ret)

		return ret;



	/*

	 * Bits[3:0] contain the preemption timeout enable bit per ringbuffer

	 * Bits[31:4] contain the timeout in ms

	 */

	return a6xx_hfi_send_set_value(adreno_dev, HFI_VALUE_BIN_TIME, 1,

			FIELD_PREP(GENMASK(31, 4), ADRENO_PREEMPT_TIMEOUT) |

			FIELD_PREP(GENMASK(3, 0), 0xf));

}



static int enable_gmu_stats(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	u32 data;



	if (!gmu->stats_enable)

		return 0;



	/*

	 * Bits [23:0] contains the countables mask

	 * Bits [31:24] is the sampling interval

	 */

	data = FIELD_PREP(GENMASK(23, 0), gmu->stats_mask) |

		FIELD_PREP(GENMASK(31, 24), gmu->stats_interval);



	return a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_GMU_STATS, 1, data);

}



static int a6xx_hfi_send_perfcounter_feature_ctrl(struct adreno_device *adreno_dev)

{

	/*

	 * Perfcounter retention is disabled by default in GMU firmware.

	 * In case perfcounter retention behaviour is overwritten by sysfs

	 * setting dynmaically, send this HFI feature with 'enable = 0' to

	 * disable this feature in GMU firmware.

	 */

	if (adreno_dev->perfcounter)

		return a6xx_hfi_send_feature_ctrl(adreno_dev,

				HFI_FEATURE_PERF_NORETAIN, 0, 0);



	return 0;

}



int a6xx_hwsched_hfi_start(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	reset_hfi_queues(adreno_dev);



	ret = a6xx_gmu_hfi_start(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hfi_send_generic_req(adreno_dev, &gmu->hfi.dcvs_table,

		sizeof(gmu->hfi.dcvs_table));

	if (ret)

		goto err;



	ret = a6xx_hfi_send_generic_req(adreno_dev, &gmu->hfi.bw_table, sizeof(gmu->hfi.bw_table));

	if (ret)

		goto err;



	ret = a6xx_hfi_send_acd_feature_ctrl(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hfi_send_lm_feature_ctrl(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hfi_send_bcl_feature_ctrl(adreno_dev);

	if (ret)

		goto err;



	ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_HWSCHED, 1, 0);

	if (ret)

		goto err;



	ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_KPROF, 1, 0);

	if (ret)

		goto err;



	if (ADRENO_FEATURE(adreno_dev, ADRENO_LSR)) {

		ret = a6xx_hfi_send_feature_ctrl(adreno_dev, HFI_FEATURE_LSR,

				1, 0);

		if (ret)

			goto err;

	}



	ret = a6xx_hfi_send_perfcounter_feature_ctrl(adreno_dev);

	if (ret)

		goto err;



	/* Enable the long ib timeout detection */

	if (adreno_long_ib_detect(adreno_dev)) {

		ret = a6xx_hfi_send_feature_ctrl(adreno_dev,

			HFI_FEATURE_BAIL_OUT_TIMER, 1, 0);

		if (ret)

			goto err;

	}



	enable_gmu_stats(adreno_dev);



	if (gmu->log_stream_enable)

		a6xx_hfi_send_set_value(adreno_dev,

			HFI_VALUE_LOG_STREAM_ENABLE, 0, 1);



	if (gmu->log_group_mask)

		a6xx_hfi_send_set_value(adreno_dev, HFI_VALUE_LOG_GROUP, 0, gmu->log_group_mask);



	ret = a6xx_hfi_send_core_fw_start(adreno_dev);

	if (ret)

		goto err;



	ret = enable_preemption(adreno_dev);

	if (ret)

		goto err;



	ret = send_start_msg(adreno_dev);

	if (ret)

		goto err;



	enable_async_hfi(adreno_dev);



	set_bit(GMU_PRIV_HFI_STARTED, &gmu->flags);



	ret = kgsl_pwrctrl_setup_default_votes(device);



err:

	if (ret)

		a6xx_hwsched_hfi_stop(adreno_dev);



	return ret;

}



static int submit_raw_cmds(struct adreno_device *adreno_dev, void *cmds, u32 size_bytes,

	const char *str)

{

	int ret;



	ret = a6xx_hfi_send_cmd_async(adreno_dev, cmds, size_bytes);

	if (ret)

		return ret;



	ret = gmu_core_timed_poll_check(KGSL_DEVICE(adreno_dev),

			A6XX_GPU_GMU_AO_GPU_CX_BUSY_STATUS, 0, 200, BIT(23));

	if (ret)

		a6xx_spin_idle_debug(adreno_dev, str);



	return ret;

}



static int cp_init(struct adreno_device *adreno_dev)

{

	u32 cmds[A6XX_CP_INIT_DWORDS + 1];



	cmds[0] = CREATE_MSG_HDR(H2F_MSG_ISSUE_CMD_RAW, HFI_MSG_CMD);



	a6xx_cp_init_cmds(adreno_dev, &cmds[1]);



	return submit_raw_cmds(adreno_dev, cmds, sizeof(cmds),

			"CP initialization failed to idle\n");

}



static int send_switch_to_unsecure(struct adreno_device *adreno_dev)

{

	u32 cmds[3];



	cmds[0] = CREATE_MSG_HDR(H2F_MSG_ISSUE_CMD_RAW, HFI_MSG_CMD);



	cmds[1] = cp_type7_packet(CP_SET_SECURE_MODE, 1);

	cmds[2] = 0;



	return  submit_raw_cmds(adreno_dev, cmds, sizeof(cmds),

			"Switch to unsecure failed to idle\n");

}



int a6xx_hwsched_cp_init(struct adreno_device *adreno_dev)

{

	const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);

	struct adreno_firmware *fw = ADRENO_FW(adreno_dev, ADRENO_FW_SQE);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	/* Program the ucode base for CP */

	kgsl_regwrite(device, A6XX_CP_SQE_INSTR_BASE_LO,

		lower_32_bits(fw->memdesc->gpuaddr));

	kgsl_regwrite(device, A6XX_CP_SQE_INSTR_BASE_HI,

		upper_32_bits(fw->memdesc->gpuaddr));



	ret = cp_init(adreno_dev);

	if (ret)

		return ret;



	ret = adreno_zap_shader_load(adreno_dev, a6xx_core->zap_name);

	if (ret)

		return ret;



	if (!adreno_dev->zap_loaded)

		kgsl_regwrite(KGSL_DEVICE(adreno_dev),

			A6XX_RBBM_SECVID_TRUST_CNTL, 0x0);

	else

		ret = send_switch_to_unsecure(adreno_dev);



	return ret;

}



static int register_global_ctxt(struct adreno_device *adreno_dev)

{

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct hfi_register_ctxt_cmd rcmd = {0};

	struct hfi_context_pointers_cmd pcmd = {0};

	int ret;



	if (hwsched->global_ctxt_gmu_registered)

		return 0;



	ret = CMD_MSG_HDR(rcmd, H2F_MSG_REGISTER_CONTEXT);

	if (ret)

		return ret;



	rcmd.ctxt_id = KGSL_GLOBAL_CTXT_ID;

	rcmd.flags = (KGSL_CONTEXT_PRIORITY_HIGH << KGSL_CONTEXT_PRIORITY_SHIFT);



	ret = a6xx_hfi_send_cmd_async(adreno_dev, &rcmd, sizeof(rcmd));

	if (ret)

		return ret;



	ret = CMD_MSG_HDR(pcmd, H2F_MSG_CONTEXT_POINTERS);

	if (ret)

		return ret;



	pcmd.ctxt_id = KGSL_GLOBAL_CTXT_ID;

	pcmd.sop_addr = MEMSTORE_ID_GPU_ADDR(device, KGSL_GLOBAL_CTXT_ID, soptimestamp);

	pcmd.eop_addr = MEMSTORE_ID_GPU_ADDR(device, KGSL_GLOBAL_CTXT_ID, eoptimestamp);



	ret = a6xx_hfi_send_cmd_async(adreno_dev, &pcmd, sizeof(pcmd));

	if (!ret)

		hwsched->global_ctxt_gmu_registered = true;



	return ret;

}



#define HFI_DSP_IRQ_BASE 2



#define DISPQ_IRQ_BIT(_idx) BIT((_idx) + HFI_DSP_IRQ_BASE)



static int submit_global_ctxt_cmd(struct adreno_device *adreno_dev, u64 gpuaddr, u32 size)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct {

		struct hfi_submit_cmd submit_cmd;

		struct hfi_issue_ib issue_ib;

	} cmd = {0};

	u32 seqnum, cmd_size = sizeof(cmd);

	static u32 ts;

	int ret = 0;



	cmd.submit_cmd.ctxt_id = KGSL_GLOBAL_CTXT_ID;

	cmd.submit_cmd.ts = ++ts;

	cmd.submit_cmd.numibs = 1;



	cmd.issue_ib.addr = gpuaddr;

	cmd.issue_ib.size = size;



	seqnum = atomic_inc_return(&adreno_dev->hwsched.submission_seqnum);

	cmd.submit_cmd.hdr = CREATE_MSG_HDR(H2F_MSG_ISSUE_CMD, HFI_MSG_CMD);

	cmd.submit_cmd.hdr = MSG_HDR_SET_SEQNUM_SIZE(cmd.submit_cmd.hdr, seqnum, cmd_size >> 2);



	ret = a6xx_hfi_dispatch_queue_write(adreno_dev, HFI_DSP_ID_0,

			(u32 *)&cmd, cmd_size, NULL, NULL);

	/* Send interrupt to GMU to receive the message */

	if (!ret)

		gmu_core_regwrite(device, A6XX_GMU_HOST2GMU_INTR_SET, DISPQ_IRQ_BIT(0));



	return ret;

}



int a6xx_hwsched_counter_inline_enable(struct adreno_device *adreno_dev,

		const struct adreno_perfcount_group *group,

		u32 counter, u32 countable)

{

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_perfcount_register *reg = &group->regs[counter];

	u32 val, *cmds, count = 0;

	int ret;



	ret = register_global_ctxt(adreno_dev);

	if (ret)

		goto err;



	ret = adreno_allocate_global(device, &hfi->perfctr_scratch,

		PAGE_SIZE, 0, KGSL_MEMFLAGS_GPUREADONLY, 0, "perfctr_scratch");

	if (ret)

		goto err;



	if (group->flags & ADRENO_PERFCOUNTER_GROUP_RESTORE)

		a6xx_perfcounter_update(adreno_dev, reg, false);



	cmds = hfi->perfctr_scratch->hostptr;



	cmds[count++] = cp_type7_packet(CP_WAIT_FOR_IDLE, 0);

	cmds[count++] = cp_type4_packet(reg->select, 1);

	cmds[count++] = countable;



	ret = submit_global_ctxt_cmd(adreno_dev, hfi->perfctr_scratch->gpuaddr, count << 2);

	if (ret)

		goto err;



	/* Wait till the register is programmed with the countable */

	ret = kgsl_regmap_read_poll_timeout(&device->regmap, reg->select, val,

				val == countable, 100, ADRENO_IDLE_TIMEOUT * USEC_PER_MSEC);

	if (!ret) {

		reg->value = 0;

		return ret;

	}



err:

	dev_err(device->dev, "Perfcounter %s/%u/%u start via commands failed\n",

			group->name, counter, countable);

	return ret;

}



static bool is_queue_empty(struct adreno_device *adreno_dev, u32 queue_idx)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct kgsl_memdesc *mem_addr = gmu->hfi.hfi_mem;

	struct hfi_queue_table *tbl = mem_addr->hostptr;

	struct hfi_queue_header *hdr = &tbl->qhdr[queue_idx];



	if (hdr->status == HFI_QUEUE_STATUS_DISABLED)

		return true;



	if (hdr->read_index == hdr->write_index)

		return true;



	return false;

}



static int hfi_f2h_main(void *arg)

{

	struct adreno_device *adreno_dev = arg;

	struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	while (!kthread_should_stop()) {

		wait_event_interruptible(hfi->f2h_wq, kthread_should_stop() ||

			/* If msgq irq is enabled and msgq has messages to process */

			(((hfi->irq_mask & HFI_IRQ_MSGQ_MASK) &&

			!is_queue_empty(adreno_dev, HFI_MSG_ID)) ||

			/* Trace buffer has messages to process */

			!gmu_core_is_trace_empty(device->gmu_core.trace.md->hostptr) ||

			/* Dbgq has messages to process */

			!is_queue_empty(adreno_dev, HFI_DBG_ID)));



		if (kthread_should_stop())

			break;



		a6xx_hwsched_process_msgq(adreno_dev);

		gmu_core_process_trace_data(device,

				GMU_PDEV_DEV(device), &device->gmu_core.trace);

		a6xx_hwsched_process_dbgq(adreno_dev, true);

	}



	return 0;

}



int a6xx_hwsched_hfi_probe(struct adreno_device *adreno_dev)

{

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct a6xx_hwsched_hfi *hw_hfi = to_a6xx_hwsched_hfi(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	/* Try with "hfi" irq first. Use Fallback legacy name "kgsl_hfi_irq" if not found. */

	gmu->hfi.irq = kgsl_request_irq(GMU_PDEV(device), "hfi", "kgsl_hfi_irq", -EINVAL,

		a6xx_hwsched_hfi_handler, adreno_dev);



	if (gmu->hfi.irq < 0)

		return gmu->hfi.irq;



	hw_hfi->irq_mask = HFI_IRQ_MASK;



	rwlock_init(&hw_hfi->msglock);



	INIT_LIST_HEAD(&hw_hfi->msglist);



	init_waitqueue_head(&hw_hfi->f2h_wq);



	mutex_init(&hw_hfi->msgq_mutex);



	return 0;

}



void a6xx_hwsched_hfi_remove(struct adreno_device *adreno_dev)

{

	struct a6xx_hwsched_hfi *hw_hfi = to_a6xx_hwsched_hfi(adreno_dev);



	if (hw_hfi->f2h_task)

		kthread_stop(hw_hfi->f2h_task);

}



static u32 get_next_dq(u32 priority)

{

	struct dq_info *info = &a6xx_hfi_dqs[priority];

	u32 next = info->base_dq_id + info->offset;



	info->offset = (info->offset + 1) % info->max_dq;



	return next;

}



static u32 get_dq_id(struct kgsl_context *context)

{

	u32 level = adreno_get_level(context);



	return get_next_dq(level);

}



static int send_context_register(struct adreno_device *adreno_dev,

	struct kgsl_context *context)

{

	struct hfi_register_ctxt_cmd cmd;

	struct kgsl_pagetable *pt = context->proc_priv->pagetable;

	int ret, asid = kgsl_mmu_pagetable_get_asid(pt, context);



	if (asid < 0)

		return asid;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_REGISTER_CONTEXT);

	if (ret)

		return ret;



	cmd.ctxt_id = context->id;

	cmd.flags = HFI_CTXT_FLAG_NOTIFY | context->flags;



	/*

	 * HLOS SMMU driver programs context bank to look up ASID from TTBR0 during a page

	 * table walk. So the TLB entries are tagged with the ASID from TTBR0. TLBIASID

	 * invalidates TLB entries whose ASID matches the value that was written to the

	 * CBn_TLBIASID register. Set ASID along with PT address.

	 */

	cmd.pt_addr = kgsl_mmu_pagetable_get_ttbr0(pt) |

		FIELD_PREP(GENMASK_ULL(63, KGSL_IOMMU_ASID_START_BIT), asid);

	cmd.ctxt_idr = pid_nr(context->proc_priv->pid);

	cmd.ctxt_bank = kgsl_mmu_pagetable_get_context_bank(pt, context);



	return a6xx_hfi_send_cmd_async(adreno_dev, &cmd, sizeof(cmd));

}



static int send_context_pointers(struct adreno_device *adreno_dev,

	struct kgsl_context *context)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct hfi_context_pointers_cmd cmd = {0};

	int ret;



	ret = CMD_MSG_HDR(cmd, H2F_MSG_CONTEXT_POINTERS);

	if (ret)

		return ret;



	cmd.ctxt_id = context->id;

	cmd.sop_addr = MEMSTORE_ID_GPU_ADDR(device, context->id, soptimestamp);

	cmd.eop_addr = MEMSTORE_ID_GPU_ADDR(device, context->id, eoptimestamp);

	if (context->user_ctxt_record)

		cmd.user_ctxt_record_addr =

			context->user_ctxt_record->memdesc.gpuaddr;



	return a6xx_hfi_send_cmd_async(adreno_dev, &cmd, sizeof(cmd));

}



static int hfi_context_register(struct adreno_device *adreno_dev,

	struct kgsl_context *context)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	if (context->gmu_registered)

		return 0;



	ret = send_context_register(adreno_dev, context);

	if (ret) {

		dev_err(GMU_PDEV_DEV(device),

			"Unable to register context %u: %d\n",

			context->id, ret);



		if (device->gmu_fault)

			adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);



		return ret;

	}



	ret = send_context_pointers(adreno_dev, context);

	if (ret) {

		dev_err(GMU_PDEV_DEV(device),

			"Unable to register context %u pointers: %d\n",

			context->id, ret);



		if (device->gmu_fault)

			adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);



		return ret;

	}



	context->gmu_registered = true;

	context->gmu_dispatch_queue = get_dq_id(context);



	return 0;

}



static void populate_ibs(struct adreno_device *adreno_dev,

	struct hfi_submit_cmd *cmd, struct kgsl_drawobj_cmd *cmdobj)

{

	struct hfi_issue_ib *issue_ib;

	struct kgsl_memobj_node *ib;



	if (cmdobj->numibs > HWSCHED_MAX_DISPATCH_NUMIBS) {

		struct a6xx_hwsched_hfi *hfi = to_a6xx_hwsched_hfi(adreno_dev);

		struct kgsl_memdesc *big_ib;



		if (test_bit(CMDOBJ_RECURRING_START, &cmdobj->priv))

			big_ib = hfi->big_ib_recurring;

		else

			big_ib = hfi->big_ib;

		/*

		 * The dispatcher ensures that there is only one big IB inflight

		 */

		cmd->big_ib_gmu_va = big_ib->gmuaddr;

		cmd->flags |= CMDBATCH_INDIRECT;

		issue_ib = big_ib->hostptr;

	} else {

		issue_ib = (struct hfi_issue_ib *)&cmd[1];

	}



	list_for_each_entry(ib, &cmdobj->cmdlist, node) {

		issue_ib->addr = ib->gpuaddr;

		issue_ib->size = ib->size;

		issue_ib++;

	}



	cmd->numibs = cmdobj->numibs;

}



/* Size in below functions are in unit of dwords */

static int a6xx_hfi_dispatch_queue_write(struct adreno_device *adreno_dev, uint32_t queue_idx,

	uint32_t *msg, u32 size_bytes, struct kgsl_drawobj_cmd *cmdobj,

	struct adreno_submit_time *time)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct a6xx_gmu_device *gmu = to_a6xx_gmu(adreno_dev);

	struct hfi_queue_table *tbl = gmu->hfi.hfi_mem->hostptr;

	struct hfi_queue_header *hdr = &tbl->qhdr[queue_idx];

	uint32_t *queue;

	uint32_t i, write, empty_space;

	uint32_t size_dwords = size_bytes >> 2;

	u32 align_size = ALIGN(size_dwords, SZ_4);

	uint32_t id = MSG_HDR_GET_ID(*msg);



	if (hdr->status == HFI_QUEUE_STATUS_DISABLED || !IS_ALIGNED(size_bytes, sizeof(u32)))

		return -EINVAL;



	queue = HOST_QUEUE_START_ADDR(gmu->hfi.hfi_mem, queue_idx);



	empty_space = (hdr->write_index >= hdr->read_index) ?

			(hdr->queue_size - (hdr->write_index - hdr->read_index))

			: (hdr->read_index - hdr->write_index);



	if (empty_space <= align_size)

		return -ENOSPC;



	write = hdr->write_index;



	for (i = 0; i < size_dwords; i++) {

		queue[write] = msg[i];

		write = (write + 1) % hdr->queue_size;

	}



	/* Cookify any non used data at the end of the write buffer */

	if (GMU_VER_MAJOR(device->gmu_core.ver.hfi) >= 2) {

		for (; i < align_size; i++) {

			queue[write] = 0xFAFAFAFA;

			write = (write + 1) % hdr->queue_size;

		}

	}



	/* Ensure packet is written out before proceeding */

	wmb();



	if (!cmdobj)

		goto done;



	adreno_hwsched_add_profile_events(adreno_dev, cmdobj, time);



	/*

	 * Put the profiling information in the user profiling buffer.

	 * The hfi_update_write_idx below has a wmb() before the actual

	 * write index update to ensure that the GMU does not see the

	 * packet before the profile data is written out.

	 */

	adreno_profile_submit_time(time);



done:

	trace_kgsl_hfi_send(id, size_dwords, MSG_HDR_GET_SEQNUM(*msg));



	hfi_update_write_idx(&hdr->write_index, write);



	return 0;

}



int a6xx_hwsched_submit_drawobj(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj)

{

	int ret = 0;

	u32 cmd_sizebytes, seqnum;

	struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);

	struct hfi_submit_cmd *cmd;

	struct adreno_submit_time time = {0};

	static void *cmdbuf;

	struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);



	if (cmdbuf == NULL) {

		struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



		cmdbuf = devm_kzalloc(&device->pdev->dev, HFI_MAX_MSG_SIZE,

			GFP_KERNEL);

		if (!cmdbuf)

			return -ENOMEM;

	}



	ret = hfi_context_register(adreno_dev, drawobj->context);

========== PART_06.txt ==========
	if (ret)

		return ret;



	/* Add a *issue_ib struct for each IB */

	if (cmdobj->numibs > HWSCHED_MAX_DISPATCH_NUMIBS ||

		test_bit(CMDOBJ_SKIP, &cmdobj->priv))

		cmd_sizebytes = sizeof(*cmd);

	else

		cmd_sizebytes = sizeof(*cmd) +

			(sizeof(struct hfi_issue_ib) * cmdobj->numibs);



	if (WARN_ON(cmd_sizebytes > HFI_MAX_MSG_SIZE))

		return -EMSGSIZE;



	memset(cmdbuf, 0x0, cmd_sizebytes);



	cmd = cmdbuf;



	cmd->ctxt_id = drawobj->context->id;

	cmd->flags = HFI_CTXT_FLAG_NOTIFY;

	if (drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME)

		cmd->flags |= CMDBATCH_EOF;



	cmd->ts = drawobj->timestamp;



	if (test_bit(CMDOBJ_SKIP, &cmdobj->priv))

		goto skipib;



	populate_ibs(adreno_dev, cmd, cmdobj);



	if ((drawobj->flags & KGSL_DRAWOBJ_PROFILING) &&

		cmdobj->profiling_buf_entry) {



		time.drawobj = drawobj;



		cmd->profile_gpuaddr_lo =

			lower_32_bits(cmdobj->profiling_buffer_gpuaddr);

		cmd->profile_gpuaddr_hi =

			upper_32_bits(cmdobj->profiling_buffer_gpuaddr);



		/* Indicate to GMU to do user profiling for this submission */

		cmd->flags |= CMDBATCH_PROFILING;

	}



skipib:

	adreno_drawobj_set_constraint(KGSL_DEVICE(adreno_dev), drawobj);



	seqnum = atomic_inc_return(&adreno_dev->hwsched.submission_seqnum);

	cmd->hdr = CREATE_MSG_HDR(H2F_MSG_ISSUE_CMD, HFI_MSG_CMD);

	cmd->hdr = MSG_HDR_SET_SEQNUM_SIZE(cmd->hdr, seqnum, cmd_sizebytes >> 2);



	ret = a6xx_hfi_dispatch_queue_write(adreno_dev,

		HFI_DSP_ID_0 + drawobj->context->gmu_dispatch_queue,

		(u32 *)cmd, cmd_sizebytes, cmdobj, &time);