	 */

	kgsl_regread(device, GEN7_RBBM_SW_FUSE_INT_STATUS, &status);



	/*

	 * RBBM_INT_CLEAR_CMD will not clear SWFUSEVIOLATION interrupt. Hence

	 * do explicit swfuse irq clear.

	 */

	kgsl_regwrite(device, GEN7_RBBM_SW_FUSE_INT_MASK, 0);



	dev_crit_ratelimited(device->dev,

		"RBBM: SW Feature Fuse violation status=0x%8.8x\n", status);



	/* Trigger a fault in the dispatcher for LPAC and RAYTRACING violation */

	if (status & GENMASK(GEN7_RAYTRACING_SW_FUSE, GEN7_LPAC_SW_FUSE)) {

		adreno_irqctrl(adreno_dev, 0);

		adreno_scheduler_fault(adreno_dev, ADRENO_HARD_FAULT);

	}

}



static const struct adreno_irq_funcs gen7_irq_funcs[32] = {

	ADRENO_IRQ_CALLBACK(NULL), /* 0 - RBBM_GPU_IDLE */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 1 - RBBM_AHB_ERROR */

	ADRENO_IRQ_CALLBACK(NULL), /* 2 - UNUSED */

	ADRENO_IRQ_CALLBACK(NULL), /* 3 - UNUSED */

	ADRENO_IRQ_CALLBACK(NULL), /* 4 - CPIPCINT0 */

	ADRENO_IRQ_CALLBACK(NULL), /* 5 - CPIPCINT1 */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 6 - ATBASYNCOVERFLOW */

	ADRENO_IRQ_CALLBACK(gen7_gpc_err_int_callback), /* 7 - GPC_ERR */

	ADRENO_IRQ_CALLBACK(gen7_preemption_callback),/* 8 - CP_SW */

	ADRENO_IRQ_CALLBACK(gen7_cp_hw_err_callback), /* 9 - CP_HW_ERROR */

	ADRENO_IRQ_CALLBACK(NULL), /* 10 - CP_CCU_FLUSH_DEPTH_TS */

	ADRENO_IRQ_CALLBACK(NULL), /* 11 - CP_CCU_FLUSH_COLOR_TS */

	ADRENO_IRQ_CALLBACK(NULL), /* 12 - CP_CCU_RESOLVE_TS */

	ADRENO_IRQ_CALLBACK(NULL), /* 13 - UNUSED */

	ADRENO_IRQ_CALLBACK(NULL), /* 14 - UNUSED */

	ADRENO_IRQ_CALLBACK(adreno_cp_callback), /* 15 - CP_RB_INT */

	ADRENO_IRQ_CALLBACK(NULL), /* 16 - CP_RB_INT_LPAC*/

	ADRENO_IRQ_CALLBACK(NULL), /* 17 - CP_RB_DONE_TS */

	ADRENO_IRQ_CALLBACK(NULL), /* 18 - UNUSED */

	ADRENO_IRQ_CALLBACK(NULL), /* 19 - UNUSED */

	ADRENO_IRQ_CALLBACK(gen7_cp_callback), /* 20 - CP_CACHE_FLUSH_TS */

	ADRENO_IRQ_CALLBACK(NULL), /* 21 - CP_CACHE_TS_LPAC */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 22 - RBBM_ATB_BUS_OVERFLOW */

	ADRENO_IRQ_CALLBACK(adreno_hang_int_callback), /* 23 - MISHANGDETECT */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 24 - UCHE_OOB_ACCESS */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 25 - UCHE_TRAP_INTR */

	ADRENO_IRQ_CALLBACK(NULL), /* 26 - DEBBUS_INTR_0 */

	ADRENO_IRQ_CALLBACK(NULL), /* 27 - DEBBUS_INTR_1 */

	ADRENO_IRQ_CALLBACK(gen7_err_callback), /* 28 - TSBWRITEERROR */

	ADRENO_IRQ_CALLBACK(gen7_swfuse_violation_callback), /* 29 - SWFUSEVIOLATION */

	ADRENO_IRQ_CALLBACK(NULL), /* 30 - ISDB_CPU_IRQ */

	ADRENO_IRQ_CALLBACK(NULL), /* 31 - ISDB_UNDER_DEBUG */

};



/*

 * If the AHB fence is not in ALLOW mode when we receive an RBBM

 * interrupt, something went wrong. This means that we cannot proceed

 * since the IRQ status and clear registers are not accessible.

 * This is usually harmless because the GMU will abort power collapse

 * and change the fence back to ALLOW. Poll so that this can happen.

 */

static int gen7_irq_poll_fence(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u32 status, fence, fence_retries = 0;

	u64 a, b, c;



	a = gpudev->read_alwayson(adreno_dev);



	kgsl_regread(device, GEN7_GMU_AO_AHB_FENCE_CTRL, &fence);



	while (fence != 0) {

		b = gpudev->read_alwayson(adreno_dev);



		/* Wait for small time before trying again */

		udelay(1);

		kgsl_regread(device, GEN7_GMU_AO_AHB_FENCE_CTRL, &fence);



		if (fence_retries == 100 && fence != 0) {

			c = gpudev->read_alwayson(adreno_dev);



			kgsl_regread(device, GEN7_GMU_RBBM_INT_UNMASKED_STATUS,

				&status);



			dev_crit_ratelimited(device->dev,

				"status=0x%x Unmasked status=0x%x Mask=0x%x timestamps: %llx %llx %llx\n",

					status & adreno_dev->irq_mask, status,

					adreno_dev->irq_mask, a, b, c);

				return -ETIMEDOUT;

		}



		fence_retries++;

	}



	return 0;

}



static irqreturn_t gen7_hwsched_irq_handler(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	irqreturn_t ret = IRQ_NONE;

	u32 status;



	/*

	 * GPU can power down once the INT_0_STATUS is read below.

	 * But there still might be some register reads required so

	 * force the GMU/GPU into KEEPALIVE mode until done with the ISR.

	 */

	gen7_gpu_keepalive(adreno_dev, true);



	kgsl_regread(device, GEN7_RBBM_INT_0_STATUS, &status);



	kgsl_regwrite(device, GEN7_RBBM_INT_CLEAR_CMD, status);



	ret = adreno_irq_callbacks(adreno_dev, gen7_irq_funcs, status);



	trace_kgsl_gen7_irq_status(adreno_dev, status);



	/* If hard fault, then let snapshot turn off the keepalive */

	if (!(adreno_gpu_fault(adreno_dev) & ADRENO_HARD_FAULT))

		gen7_gpu_keepalive(adreno_dev, false);



	return ret;

}



static irqreturn_t gen7_irq_handler(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	irqreturn_t ret = IRQ_NONE;

	u32 status;



	/*

	 * GPU can power down once the INT_0_STATUS is read below.

	 * But there still might be some register reads required so

	 * force the GMU/GPU into KEEPALIVE mode until done with the ISR.

	 */

	gen7_gpu_keepalive(adreno_dev, true);



	if (gen7_irq_poll_fence(adreno_dev)) {

		adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT);

		goto done;

	}



	kgsl_regread(device, GEN7_RBBM_INT_0_STATUS, &status);



	kgsl_regwrite(device, GEN7_RBBM_INT_CLEAR_CMD, status);



	ret = adreno_irq_callbacks(adreno_dev, gen7_irq_funcs, status);



	trace_kgsl_gen7_irq_status(adreno_dev, status);



done:

	/* If hard fault, then let snapshot turn off the keepalive */

	if (!(adreno_gpu_fault(adreno_dev) & ADRENO_HARD_FAULT))

		gen7_gpu_keepalive(adreno_dev, false);



	return ret;

}



int gen7_probe_common(struct platform_device *pdev,

	struct adreno_device *adreno_dev, u32 chipid,

	const struct adreno_gpu_core *gpucore)

{

	const struct adreno_gpudev *gpudev = gpucore->gpudev;

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	const struct adreno_gen7_core *gen7_core = container_of(gpucore,

			struct adreno_gen7_core, base);

	int ret;



	adreno_dev->gpucore = gpucore;

	adreno_dev->chipid = chipid;



	adreno_reg_offset_init(gpudev->reg_offsets);



	adreno_dev->hwcg_enabled = true;

	adreno_dev->uche_client_pf = 1;



	kgsl_pwrscale_fast_bus_hint(gen7_core->fast_bus_hint);



	pwr->rt_bus_hint = gen7_core->rt_bus_hint;



	if (adreno_is_gen7_11_0(adreno_dev))

		pwr->cx_cfg_gdsc_offset = GEN7_11_0_GPU_CC_CX_CFG_GDSCR;

	else if (adreno_is_gen7_17_0(adreno_dev))

		pwr->cx_cfg_gdsc_offset = GEN7_17_0_GPU_CC_CX_CFG_GDSCR;

	else if (adreno_is_gen7_13_0(adreno_dev))

		pwr->cx_cfg_gdsc_offset = GEN7_13_0_GPU_CC_CX_CFG_GDSCR;

	else

		pwr->cx_cfg_gdsc_offset = GEN7_GPU_CC_CX_CFG_GDSCR;



	ret = adreno_device_probe(pdev, adreno_dev);

	if (ret)

		return ret;



	if (adreno_preemption_feature_set(adreno_dev)) {

		const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);



		adreno_dev->total_ctxt_record_sz = gen7_core->ctxt_record_size ?

			gen7_core->ctxt_record_size : GEN7_CP_CTXRECORD_SIZE_IN_BYTES;



		adreno_dev->preempt.preempt_level = gen7_core->preempt_level;

		adreno_dev->preempt.skipsaverestore = true;

		adreno_dev->preempt.usesgmem = true;

		set_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);

	}



	/* debugfs node for ACD calibration */

	debugfs_create_file("acd_calibrate", 0644, device->d_debugfs, device, &acd_cal_fops);



	gen7_coresight_init(adreno_dev);



	/* Dump additional AQE 16KB data on top of default 96KB(48(BR)+48(BV)) */

	device->snapshot_ctxt_record_size = ADRENO_FEATURE(adreno_dev, ADRENO_AQE) ?

					112 * SZ_1K : 96 * SZ_1K;



	return 0;

}



/* Register offset defines for Gen7, in order of enum adreno_regs */

static unsigned int gen7_register_offsets[ADRENO_REG_REGISTER_MAX] = {

	ADRENO_REG_DEFINE(ADRENO_REG_CP_RB_BASE, GEN7_CP_RB_BASE),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_RB_BASE_HI, GEN7_CP_RB_BASE_HI),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_LPAC_RB_BASE, GEN7_CP_LPAC_RB_BASE),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_LPAC_RB_BASE_HI, GEN7_CP_LPAC_RB_BASE_HI),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_RB_RPTR, GEN7_CP_RB_RPTR),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_RB_WPTR, GEN7_CP_RB_WPTR),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_ME_CNTL, GEN7_CP_SQE_CNTL),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB1_BASE, GEN7_CP_IB1_BASE),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB1_BASE_HI, GEN7_CP_IB1_BASE_HI),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB1_BUFSZ, GEN7_CP_IB1_REM_SIZE),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB2_BASE, GEN7_CP_IB2_BASE),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB2_BASE_HI, GEN7_CP_IB2_BASE_HI),

	ADRENO_REG_DEFINE(ADRENO_REG_CP_IB2_BUFSZ, GEN7_CP_IB2_REM_SIZE),

	ADRENO_REG_DEFINE(ADRENO_REG_RBBM_STATUS, GEN7_RBBM_STATUS),

	ADRENO_REG_DEFINE(ADRENO_REG_RBBM_STATUS3, GEN7_RBBM_STATUS3),

	ADRENO_REG_DEFINE(ADRENO_REG_RBBM_INT_0_MASK, GEN7_RBBM_INT_0_MASK),

	ADRENO_REG_DEFINE(ADRENO_REG_RBBM_SW_RESET_CMD, GEN7_RBBM_SW_RESET_CMD),

	ADRENO_REG_DEFINE(ADRENO_REG_GMU_AO_HOST_INTERRUPT_MASK,

			GEN7_GMU_AO_HOST_INTERRUPT_MASK),

	ADRENO_REG_DEFINE(ADRENO_REG_GMU_GMU2HOST_INTR_MASK,

			GEN7_GMU_GMU2HOST_INTR_MASK),

};





static u32 _get_pipeid(u32 groupid)

{

	switch (groupid) {

	case KGSL_PERFCOUNTER_GROUP_BV_PC:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_BV_VPC:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_BV_TSE:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_BV_RAS:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_BV_LRZ:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_BV_HLSQ:

		return PIPE_BV;

	case KGSL_PERFCOUNTER_GROUP_PC:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_VPC:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_TSE:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_RAS:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_LRZ:

		fallthrough;

	case KGSL_PERFCOUNTER_GROUP_HLSQ:

		return PIPE_BR;

	default:

		return PIPE_NONE;

	}

}



int gen7_perfcounter_remove(struct adreno_device *adreno_dev,

	struct adreno_perfcount_register *reg, u32 groupid)

{

	const struct adreno_perfcounters *counters = ADRENO_PERFCOUNTERS(adreno_dev);

	struct gen7_device *gen7_dev = container_of(adreno_dev, struct gen7_device, adreno_dev);

	const struct adreno_perfcount_group *group;

	void *ptr = adreno_dev->pwrup_reglist->hostptr;

	struct cpu_gpu_lock *lock = ptr;

	bool remove_counter = false;

	u32 offset = ((lock->ifpc_list_len + lock->preemption_list_len) * 2) +

			(gen7_dev->ext_pwrup_list_len * 3);

	int i, last_offset, second_last_offset;

	u32 *data = ptr + sizeof(*lock), pipe = FIELD_PREP(GENMASK(13, 12), _get_pipeid(groupid));

	u16 perfcntr_list_len = lock->dynamic_list_len - gen7_dev->ext_pwrup_list_len;



	if (!perfcntr_list_len)

		return -EINVAL;



	group = &(counters->groups[groupid]);



	if (!(group->flags & ADRENO_PERFCOUNTER_GROUP_RESTORE)) {

		if (perfcntr_list_len != 1)

			return 0;



		if (kgsl_hwlock(lock)) {

			kgsl_hwunlock(lock);

			return -EBUSY;

		}

		goto disable_perfcounter;

	}



	second_last_offset = offset + (lock->dynamic_list_len - 2) * 3;

	last_offset = second_last_offset + 3;



	/* Look for the perfcounter to remove in the list */

	for (i = 0; i < lock->dynamic_list_len - 1; i++) {

		if ((data[offset + 1] == reg->select) && (data[offset] == pipe)) {

			remove_counter = true;

			break;

		}

		offset += 3;

	}



	if (!remove_counter)

		return -ENOENT;



	if (kgsl_hwlock(lock)) {

		kgsl_hwunlock(lock);

		return -EBUSY;

	}



	/*

	 * If the entry is found, remove it from the list by overwriting with second last

	 * entry. Skip this if data at offset is already second last entry

	 */

	if (offset != second_last_offset)

		memcpy(&data[offset], &data[second_last_offset], 3 * sizeof(u32));



	/*

	 * Overwrite the second last entry with last entry as last entry always has to be

	 * GEN7_RBBM_PERFCTR_CNTL.

	 */

	memcpy(&data[second_last_offset], &data[last_offset], 3 * sizeof(u32));



	/* Clear the last entry */

	memset(&data[last_offset], 0, 3 * sizeof(u32));



	lock->dynamic_list_len--;



disable_perfcounter:

	/*

	 * If dynamic list length is 1 and no_restore_count is 0, then we can remove the

	 * only entry in the list, which is the GEN7_RBBM_PERFCTRL_CNTL.

	 */

	if (perfcntr_list_len == 1 && !adreno_dev->no_restore_count) {

		memset(&data[offset], 0, 3 * sizeof(u32));

		lock->dynamic_list_len = gen7_dev->ext_pwrup_list_len;

	}



	kgsl_hwunlock(lock);

	return 0;

}



static bool gen7_acquire_cp_semaphore(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 sem, i;



	for (i = 0; i < 10; i++) {

		kgsl_regwrite(device, GEN7_CP_SEMAPHORE_REG_0, BIT(8));



		/*

		 * Make sure the previous register write is posted before

		 * checking the CP sempahore status

		 */

		mb();



		kgsl_regread(device, GEN7_CP_SEMAPHORE_REG_0, &sem);

		if (sem)

			return true;



		udelay(10);

	}



	/* Check CP semaphore status one last time */

	kgsl_regread(device, GEN7_CP_SEMAPHORE_REG_0, &sem);



	if (!sem)

		return false;



	return true;

}



static void gen7_release_cp_semaphore(struct adreno_device *adreno_dev)

{

	kgsl_regwrite(KGSL_DEVICE(adreno_dev), GEN7_CP_SEMAPHORE_REG_0, 0);

}



int gen7_perfcounter_update(struct adreno_device *adreno_dev,

	struct adreno_perfcount_register *reg, bool update_reg, u32 pipe, unsigned long flags)

{

	struct gen7_device *gen7_dev = container_of(adreno_dev, struct gen7_device, adreno_dev);

	void *ptr = adreno_dev->pwrup_reglist->hostptr;

	struct cpu_gpu_lock *lock = ptr;

	u32 offset = ((lock->ifpc_list_len + lock->preemption_list_len) * 2) +

			(gen7_dev->ext_pwrup_list_len * 3);

	u32 *data = ptr + sizeof(*lock);

	u64 reglist_size_dwords = adreno_dev->pwrup_reglist->size / sizeof(u32);

	u64 lock_size_dwords = sizeof(*lock) / sizeof(u32);

	int i;

	u16 perfcntr_list_len = lock->dynamic_list_len - gen7_dev->ext_pwrup_list_len;

	bool select_reg_present = false;

	unsigned long irq_flags;

	int ret = 0;

	u32 pending_triplets = 1;



	if (!ADRENO_ACQUIRE_CP_SEMAPHORE(adreno_dev, irq_flags))

		return -EBUSY;



	if (flags & ADRENO_PERFCOUNTER_GROUP_RESTORE) {

		/* No of triplet to add if restoring: 1 main + 1 control otherwise: 1 control */

		pending_triplets++;

		for (i = 0; i < perfcntr_list_len; i++) {

			if ((data[offset + 1] == reg->select) && (data[offset] == pipe)) {

				select_reg_present = true;

				break;

			}



			if (data[offset + 1] == GEN7_RBBM_PERFCTR_CNTL)

				break;



			offset += 3;

		}

	} else if (perfcntr_list_len) {

		goto update;

	}



	/* Ensure there is enough space in the reglist buffer for new triplets */

	if ((!select_reg_present) && ((offset + (pending_triplets * 3) + lock_size_dwords) >=

		reglist_size_dwords)) {

		ret = -ENOSPC;

		goto err;

	}



	if (kgsl_hwlock(lock)) {

		kgsl_hwunlock(lock);

		ret = -EBUSY;

		goto err;

	}



	/*

	 * If the perfcounter select register is already present in reglist

	 * update it, otherwise append the <aperture, select register, value>

	 * triplet to the end of the list.

	 */

	if (select_reg_present) {

		data[offset + 2] = reg->countable;

		kgsl_hwunlock(lock);

		goto update;

	}



	/* Initialize the lock->dynamic_list_len to account for GEN7_RBBM_PERFCTR_CNTL */

	if (!perfcntr_list_len)

		lock->dynamic_list_len = gen7_dev->ext_pwrup_list_len + 1;



	/*

	 * For all targets GEN7_RBBM_PERFCTR_CNTL needs to be the last entry,

	 * so overwrite the existing GEN7_RBBM_PERFCTR_CNTL and add it back to

	 * the end.

	 */

	if (flags & ADRENO_PERFCOUNTER_GROUP_RESTORE) {

		data[offset++] = pipe;

		data[offset++] = reg->select;

		data[offset++] = reg->countable;

		lock->dynamic_list_len++;

	}



	data[offset++] = FIELD_PREP(GENMASK(13, 12), PIPE_NONE);

	data[offset++] = GEN7_RBBM_PERFCTR_CNTL;

	data[offset++] = 1;



	kgsl_hwunlock(lock);



update:

	if (update_reg)

		kgsl_regwrite(KGSL_DEVICE(adreno_dev), reg->select,

			reg->countable);

err:

	ADRENO_RELEASE_CP_SEMAPHORE(adreno_dev, irq_flags);

	return ret;

}



static u64 gen7_9_0_read_alwayson(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 lo = 0, hi = 0, tmp = 0;



	/* Always use the GMU AO counter when doing a AHB read */

	gmu_core_regread(device, GEN7_GMU_CX_AO_COUNTER_HI, &hi);

	gmu_core_regread(device, GEN7_GMU_CX_AO_COUNTER_LO, &lo);



	/* Check for overflow */

	gmu_core_regread(device, GEN7_GMU_CX_AO_COUNTER_HI, &tmp);



	if (hi != tmp) {

		gmu_core_regread(device, GEN7_GMU_CX_AO_COUNTER_LO,

				&lo);

		hi = tmp;

	}



	return (((u64) hi) << 32) | lo;

}



static u64 gen7_read_alwayson(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 lo = 0, hi = 0, tmp = 0;



	/* Always use the GMU AO counter when doing a AHB read */

	gmu_core_regread(device, GEN7_GMU_ALWAYS_ON_COUNTER_H, &hi);

	gmu_core_regread(device, GEN7_GMU_ALWAYS_ON_COUNTER_L, &lo);



	/* Check for overflow */

	gmu_core_regread(device, GEN7_GMU_ALWAYS_ON_COUNTER_H, &tmp);



	if (hi != tmp) {

		gmu_core_regread(device, GEN7_GMU_ALWAYS_ON_COUNTER_L,

				&lo);

		hi = tmp;

	}



	return (((u64) hi) << 32) | lo;

}



static int gen7_9_0_lpac_store(struct adreno_device *adreno_dev, bool enable)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_LPAC))

		return -EINVAL;



	if (!(adreno_dev->feature_fuse & BIT(GEN7_LPAC_SW_FUSE)) ||

		(adreno_dev->lpac_enabled == enable))

		return 0;



	/* Power down the GPU before changing the lpac setting */

	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->lpac_enabled,

				       enable);

}



static int gen7_lpac_store(struct adreno_device *adreno_dev, bool enable)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_LPAC))

		return -EINVAL;



	if (adreno_dev->lpac_enabled == enable)

		return 0;



	/* Power down the GPU before changing the lpac setting */

	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->lpac_enabled,

				       enable);

}



static void gen7_remove(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	/* Make sure timer is initialized, otherwise WARN_ON is generated */

	if (adreno_preemption_feature_set(adreno_dev) &&

	    (test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags)))

		kgsl_delete_timer(&adreno_dev->preempt.timer);

}



static void gen7_read_bus_stats(struct kgsl_device *device,

		struct kgsl_power_stats *stats,

		struct adreno_busy_data *busy)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	u64 ram_cycles, starved_ram;



	ram_cycles = counter_delta(device, adreno_dev->ram_cycles_lo,

		&busy->bif_ram_cycles);



	starved_ram = counter_delta(device, adreno_dev->starved_ram_lo,

		&busy->bif_starved_ram);



	ram_cycles += counter_delta(device,

		adreno_dev->ram_cycles_lo_ch1_read,

		&busy->bif_ram_cycles_read_ch1);



	ram_cycles += counter_delta(device,

		adreno_dev->ram_cycles_lo_ch0_write,

		&busy->bif_ram_cycles_write_ch0);



	ram_cycles += counter_delta(device,

		adreno_dev->ram_cycles_lo_ch1_write,

		&busy->bif_ram_cycles_write_ch1);



	starved_ram += counter_delta(device,

		adreno_dev->starved_ram_lo_ch1,

		&busy->bif_starved_ram_ch1);



	stats->ram_time = ram_cycles;

	stats->ram_wait = starved_ram;

}



static void gen7_power_feature_stats(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_busy_data *busy = &adreno_dev->busy_data;



	if (ADRENO_FEATURE(adreno_dev, ADRENO_IFPC)) {

		u32 ifpc = counter_delta(device,

			GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_4_L,

			&busy->num_ifpc);



		adreno_dev->ifpc_count += ifpc;

		if (ifpc > 0)

			trace_adreno_ifpc_count(adreno_dev->ifpc_count);

	}



	if (adreno_dev->bcl_enabled) {

		u32 a, b, c;



		a = counter_delta(device, GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_1_L,

			&busy->throttle_cycles[0]);



		b = counter_delta(device, GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_2_L,

			&busy->throttle_cycles[1]);



		c = counter_delta(device, GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_3_L,

			&busy->throttle_cycles[2]);



		if (a || b || c)

			trace_kgsl_bcl_clock_throttling(a, b, c);



		if (adreno_is_gen7_2_x_family(adreno_dev)) {

			u32 bcl_throttle = counter_delta(device,

				GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_5_L, &busy->bcl_throttle);

			/*

			 * This counts number of cycles throttled in XO cycles. Convert it to

			 * micro seconds by dividing by XO freq which is 19.2MHz.

			 */

			adreno_dev->bcl_throttle_time_us += ((bcl_throttle * 10) / 192);

		}

	}

}



static void gen7_power_stats(struct adreno_device *adreno_dev,

		struct kgsl_power_stats *stats)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_busy_data *busy = &adreno_dev->busy_data;

	u64 gpu_busy;



	/* Set the GPU busy counter for frequency scaling */

	gpu_busy = counter_delta(device, GEN7_GMU_CX_GMU_POWER_COUNTER_XOCLK_0_L,

			&busy->gpu_busy);



	stats->busy_time = gpu_busy * 10;

	do_div(stats->busy_time, 192);



	if (device->pwrctrl.bus_control)

		gen7_read_bus_stats(device, stats, busy);



	/* Read stats that are not associated with DCVS */

	gen7_power_feature_stats(adreno_dev);

}



static void gen7_set_isdb_breakpoint_registers(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct clk *clk;

	int ret;



	/* QDSS unavailable, suppress force panic */

	if (!IS_ENABLED(CONFIG_QCOM_KGSL_QDSS_STM))

		goto err;



	if (!device->set_isdb_breakpoint || device->ftbl->is_hwcg_on(device)

			|| device->qdss_gfx_virt == NULL || !device->force_panic)

		return;



	clk = clk_get(&device->pdev->dev, "apb_pclk");



	if (IS_ERR(clk)) {

		dev_err(device->dev, "Unable to get QDSS clock\n");

		goto err;

	}



	ret = clk_prepare_enable(clk);



	if (ret) {

		dev_err(device->dev, "QDSS Clock enable error: %d\n", ret);

		clk_put(clk);

		goto err;

	}



	/* Issue break command for eight SPs */

	isdb_write(device->qdss_gfx_virt, 0x0000);

	isdb_write(device->qdss_gfx_virt, 0x1000);

	isdb_write(device->qdss_gfx_virt, 0x2000);

	isdb_write(device->qdss_gfx_virt, 0x3000);

	isdb_write(device->qdss_gfx_virt, 0x4000);

	isdb_write(device->qdss_gfx_virt, 0x5000);

	isdb_write(device->qdss_gfx_virt, 0x6000);

	isdb_write(device->qdss_gfx_virt, 0x7000);



	/* gen7_2_x has additional SPs */

	if (adreno_is_gen7_2_x_family(adreno_dev)) {

		isdb_write(device->qdss_gfx_virt, 0x8000);

		isdb_write(device->qdss_gfx_virt, 0x9000);

		isdb_write(device->qdss_gfx_virt, 0xa000);

		isdb_write(device->qdss_gfx_virt, 0xb000);

	}



	clk_disable_unprepare(clk);

	clk_put(clk);



	return;



err:

	/* Do not force kernel panic if isdb writes did not go through */

	device->force_panic = false;

}



static void gen7_swfuse_irqctrl(struct adreno_device *adreno_dev, bool state)

{

	if (adreno_is_gen7_9_x(adreno_dev))

		kgsl_regwrite(KGSL_DEVICE(adreno_dev), GEN7_RBBM_SW_FUSE_INT_MASK,

			state ? GEN7_SW_FUSE_INT_MASK : 0);

}



static void gen7_lpac_fault_header(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj_lpac)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_context *drawctxt_lpac;

	u32 status = 0, lpac_rptr = 0, lpac_wptr = 0, lpac_ib1sz = 0, lpac_ib2sz = 0;

	u64 lpac_ib1base = 0, lpac_ib2base = 0;

	bool gx_on = adreno_gx_is_on(adreno_dev);



	drawctxt_lpac = ADRENO_CONTEXT(drawobj_lpac->context);

	drawobj_lpac->context->last_faulted_cmd_ts = drawobj_lpac->timestamp;

	drawobj_lpac->context->total_fault_count++;



	pr_context(device, drawobj_lpac->context,

		"LPAC ctx %d ctx_type %s ts %d dispatch_queue=%d\n",

		drawobj_lpac->context->id, kgsl_context_type(drawctxt_lpac->type),

		drawobj_lpac->timestamp, drawobj_lpac->context->gmu_dispatch_queue);



	pr_context(device, drawobj_lpac->context, "lpac cmdline: %s\n",

			drawctxt_lpac->base.proc_priv->cmdline);

	if (!gx_on)

		goto done;



	kgsl_regread(device, GEN7_RBBM_STATUS, &status);

	kgsl_regread(device, GEN7_CP_LPAC_RB_RPTR, &lpac_rptr);

	kgsl_regread(device, GEN7_CP_LPAC_RB_WPTR, &lpac_wptr);

	gen7_periph_regread64(device, GEN7_CP_PERIPH_IB1_BASE_LO(adreno_dev),

		GEN7_CP_PERIPH_IB1_BASE_HI(adreno_dev), &lpac_ib1base, PIPE_LPAC);

	gen7_periph_regread(device, GEN7_CP_PERIPH_IB1_OFFSET(adreno_dev), &lpac_ib1sz, PIPE_LPAC);

	gen7_periph_regread64(device, GEN7_CP_PERIPH_IB2_BASE_LO(adreno_dev),

		GEN7_CP_PERIPH_IB2_BASE_HI(adreno_dev), &lpac_ib2base, PIPE_LPAC);

	gen7_periph_regread(device, GEN7_CP_PERIPH_IB2_OFFSET(adreno_dev), &lpac_ib2sz, PIPE_LPAC);



	pr_context(device, drawobj_lpac->context,

		"LPAC: status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",

		status, lpac_rptr, lpac_wptr, lpac_ib1base,

		lpac_ib1sz, lpac_ib2base, lpac_ib2sz);



done:

	trace_adreno_gpu_fault(drawobj_lpac->context->id, drawobj_lpac->timestamp, status,

		lpac_rptr, lpac_wptr, lpac_ib1base, lpac_ib1sz, lpac_ib2base, lpac_ib2sz,

		adreno_get_level(drawobj_lpac->context));

}



static void gen7_fault_header(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_context *drawctxt;

	u32 status = 0, rptr = 0, wptr = 0, ib1sz = 0, ib2sz = 0;

	u64 ib1base = 0, ib2base = 0;

	u32 ctxt_id = 0, ts = 0;

	int rb_id = -1;

	bool gx_on = adreno_gx_is_on(adreno_dev);



	if (drawobj) {

		drawctxt = ADRENO_CONTEXT(drawobj->context);

		drawobj->context->last_faulted_cmd_ts = drawobj->timestamp;

		drawobj->context->total_fault_count++;

		ctxt_id = drawobj->context->id;

		ts = drawobj->timestamp;

		rb_id = adreno_get_level(drawobj->context);



		pr_context(device, drawobj->context,

			"ctx %u ctx_type %s ts %u policy %lX dispatch_queue=%d\n",

			drawobj->context->id, kgsl_context_type(drawctxt->type),

			drawobj->timestamp, CMDOBJ(drawobj)->fault_recovery,

			drawobj->context->gmu_dispatch_queue);



		pr_context(device, drawobj->context, "cmdline: %s\n",

			   drawctxt->base.proc_priv->cmdline);

	}



	if (!gx_on)

		goto done;



	kgsl_regread(device, GEN7_RBBM_STATUS, &status);

	kgsl_regread(device, GEN7_CP_RB_RPTR, &rptr);

	kgsl_regread(device, GEN7_CP_RB_WPTR, &wptr);

	gen7_periph_regread64(device, GEN7_CP_PERIPH_IB1_BASE_LO(adreno_dev),

			GEN7_CP_PERIPH_IB1_BASE_HI(adreno_dev), &ib1base, PIPE_BR);

	gen7_periph_regread(device, GEN7_CP_PERIPH_IB1_OFFSET(adreno_dev), &ib1sz, PIPE_BR);

	gen7_periph_regread64(device, GEN7_CP_PERIPH_IB2_BASE_LO(adreno_dev),

			GEN7_CP_PERIPH_IB2_BASE_HI(adreno_dev), &ib2base, PIPE_BR);

	gen7_periph_regread(device, GEN7_CP_PERIPH_IB2_OFFSET(adreno_dev), &ib2sz, PIPE_BR);



	dev_err(device->dev,

		"status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",

		status, rptr, wptr, ib1base, ib1sz, ib2base, ib2sz);

done:

	trace_adreno_gpu_fault(ctxt_id, ts, status,

		rptr, wptr, ib1base, ib1sz, ib2base, ib2sz, rb_id);

}



const struct gen7_gpudev adreno_gen7_9_0_hwsched_gpudev = {

	.base = {

		.reg_offsets = gen7_register_offsets,

		.probe = gen7_hwsched_probe,

		.snapshot = gen7_hwsched_snapshot,

		.irq_handler = gen7_hwsched_irq_handler,

		.iommu_fault_block = gen7_iommu_fault_block,

		.context_detach = gen7_hwsched_context_detach,

		.read_alwayson = gen7_9_0_read_alwayson,

		.reset = gen7_hwsched_reset_replay,

		.power_ops = &gen7_hwsched_power_ops,

		.power_stats = gen7_power_stats,

		.hw_isidle = gen7_hw_isidle,

		.add_to_va_minidump = gen7_hwsched_add_to_minidump,

		.gx_is_on = gen7_gmu_gx_is_on,

		.send_recurring_cmdobj = gen7_hwsched_send_recurring_cmdobj,

		.perfcounter_remove = gen7_perfcounter_remove,

		.set_isdb_breakpoint_registers = gen7_set_isdb_breakpoint_registers,

		.context_destroy = gen7_hwsched_context_destroy,

		.lpac_store = gen7_9_0_lpac_store,

		.get_uche_trap_base = gen7_get_uche_trap_base,

		.fault_header = gen7_fault_header,

		.lpac_fault_header = gen7_lpac_fault_header,

		.power_feature_stats = gen7_power_feature_stats,

		.acquire_cp_semaphore = gen7_acquire_cp_semaphore,

		.release_cp_semaphore = gen7_release_cp_semaphore,

	},

	.hfi_probe = gen7_hwsched_hfi_probe,

	.hfi_remove = gen7_hwsched_hfi_remove,

	.handle_watchdog = gen7_hwsched_handle_watchdog,

};



const struct gen7_gpudev adreno_gen7_hwsched_gpudev = {

	.base = {

		.reg_offsets = gen7_register_offsets,

		.probe = gen7_hwsched_probe,

		.snapshot = gen7_hwsched_snapshot,

		.irq_handler = gen7_hwsched_irq_handler,

		.iommu_fault_block = gen7_iommu_fault_block,

		.context_detach = gen7_hwsched_context_detach,

		.read_alwayson = gen7_read_alwayson,

		.reset = gen7_hwsched_reset_replay,

		.power_ops = &gen7_hwsched_power_ops,

		.power_stats = gen7_power_stats,

		.hw_isidle = gen7_hw_isidle,

		.add_to_va_minidump = gen7_hwsched_add_to_minidump,

		.gx_is_on = gen7_gmu_gx_is_on,

		.send_recurring_cmdobj = gen7_hwsched_send_recurring_cmdobj,

		.perfcounter_remove = gen7_perfcounter_remove,

		.set_isdb_breakpoint_registers = gen7_set_isdb_breakpoint_registers,

		.context_destroy = gen7_hwsched_context_destroy,

		.lpac_store = gen7_lpac_store,

		.get_uche_trap_base = gen7_get_uche_trap_base,

		.fault_header = gen7_fault_header,

		.lpac_fault_header = gen7_lpac_fault_header,

		.acquire_cp_semaphore = gen7_acquire_cp_semaphore,

		.release_cp_semaphore = gen7_release_cp_semaphore,

	},

	.hfi_probe = gen7_hwsched_hfi_probe,

	.hfi_remove = gen7_hwsched_hfi_remove,

	.handle_watchdog = gen7_hwsched_handle_watchdog,

};



const struct gen7_gpudev adreno_gen7_gmu_gpudev = {

	.base = {

		.reg_offsets = gen7_register_offsets,

		.probe = gen7_gmu_device_probe,

		.snapshot = gen7_gmu_snapshot,

		.irq_handler = gen7_irq_handler,

		.rb_start = gen7_rb_start,

		.gpu_keepalive = gen7_gpu_keepalive,

		.hw_isidle = gen7_hw_isidle,

		.iommu_fault_block = gen7_iommu_fault_block,

		.reset = gen7_gmu_reset,

		.preemption_schedule = gen7_preemption_schedule,

		.read_alwayson = gen7_read_alwayson,

		.power_ops = &gen7_gmu_power_ops,

		.remove = gen7_remove,

		.ringbuffer_submitcmd = gen7_ringbuffer_submitcmd,

		.power_stats = gen7_power_stats,

		.add_to_va_minidump = gen7_gmu_add_to_minidump,

		.gx_is_on = gen7_gmu_gx_is_on,

		.perfcounter_remove = gen7_perfcounter_remove,

		.set_isdb_breakpoint_registers = gen7_set_isdb_breakpoint_registers,

		.swfuse_irqctrl = gen7_swfuse_irqctrl,

		.get_uche_trap_base = gen7_get_uche_trap_base,

		.fault_header = gen7_fault_header,

		.acquire_cp_semaphore = gen7_acquire_cp_semaphore,

		.release_cp_semaphore = gen7_release_cp_semaphore,

	},

	.hfi_probe = gen7_gmu_hfi_probe,

	.handle_watchdog = gen7_gmu_handle_watchdog,

};





==========================================================================================
FILE: adreno_gen7.h
LINES: 542 | BYTES: 18551
==========================================================================================

/* SPDX-License-Identifier: GPL-2.0-only */

/*

 * Copyright (c) 2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#ifndef _ADRENO_GEN7_H_

#define _ADRENO_GEN7_H_



#include <linux/delay.h>



#include "gen7_reg.h"

#include "adreno_gen7_gmu.h"



/* Forward struct declaration */

struct gen7_snapshot_block_list;



extern const struct adreno_power_ops gen7_gmu_power_ops;

extern const struct adreno_power_ops gen7_hwsched_power_ops;

extern const struct adreno_perfcounters adreno_gen7_perfcounters;

extern const struct adreno_perfcounters adreno_gen7_2_0_perfcounters;

extern const struct adreno_perfcounters adreno_gen7_no_cb_perfcounters;

extern const struct adreno_perfcounters adreno_gen7_9_0_perfcounters;



struct gen7_gpudev {

	struct adreno_gpudev base;

	int (*hfi_probe)(struct adreno_device *adreno_dev);

	void (*hfi_remove)(struct adreno_device *adreno_dev);

	void (*handle_watchdog)(struct adreno_device *adreno_dev);

};



extern const struct gen7_gpudev adreno_gen7_gmu_gpudev;

extern const struct gen7_gpudev adreno_gen7_hwsched_gpudev;

extern const struct gen7_gpudev adreno_gen7_9_0_hwsched_gpudev;



/**

 * struct gen7_device - Container for the gen7_device

 */

struct gen7_device {

	/** @gmu: Container for the gen7 GMU device */

	struct gen7_gmu_device gmu;

	/** @adreno_dev: Container for the generic adreno device */

	struct adreno_device adreno_dev;

	/** @aperture: The last value that the host aperture register was programmed to */

	u32 aperture;

	/** @ext_pwrup_list_len: External pwrup reglist length */

	u16 ext_pwrup_list_len;

};



/**

 * struct gen7_pwrup_extlist - container for powerup external reglist

 */

struct gen7_pwrup_extlist {

	/** offset: Dword offset of the register to write */

	u32 offset;

	/** pipelines: pipelines to write */

	u32 pipelines;

};



/**

 * struct gen7_protected_regs - container for a protect register span

 */

struct gen7_protected_regs {

	/** @reg: Physical protected mode register to write to */

	u32 reg;

	/** @start: Dword offset of the starting register in the range */

	u32 start;

	/**

	 * @end: Dword offset of the ending register in the range

	 * (inclusive)

	 */

	u32 end;

	/**

	 * @noaccess: 1 if the register should not be accessible from

	 * userspace, 0 if it can be read (but not written)

	 */

	u32 noaccess;

};



/**

 * struct adreno_gen7_core - gen7 specific GPU core definitions

 */

struct adreno_gen7_core {

	/** @base: Container for the generic GPU definitions */

	struct adreno_gpu_core base;

	/** @gmu_fw_version: Minimum firmware version required to support this core */

	u32 gmu_fw_version;

	/** @sqefw_name: Name of the SQE microcode file */

	const char *sqefw_name;

	/** @aqefw_name: Name of the AQE microcode file */

	const char *aqefw_name;

	/** @gmufw_name: Name of the GMU firmware file */

	const char *gmufw_name;

	/** @gmufw_name: Name of the backup GMU firmware file */

	const char *gmufw_bak_name;

	/**

	 * @zap_name: Specifies the name of the CPZ zap file. This field is deprecated.

	 * Use the 'firmware-name' device tree property instead.

	 */

	const char *zap_name;

	/** @hwcg: List of registers and values to write for HWCG */

	const struct kgsl_regmap_list *hwcg;

	/** @hwcg_count: Number of registers in @hwcg */

	u32 hwcg_count;

	/** @ao_hwcg: List of registers and values to write for HWCG in AO block */

	const struct kgsl_regmap_list *ao_hwcg;

	/** @ao_hwcg_count: Number of registers in @ao_hwcg */

	u32 ao_hwcg_count;

	/** @gbif: List of registers and values to write for GBIF */

	const struct kgsl_regmap_list *gbif;

	/** @gbif_count: Number of registers in @gbif */

	u32 gbif_count;

	/** @hang_detect_cycles: Hang detect counter timeout value */

	u32 hang_detect_cycles;

	/** @protected_regs: Array of protected registers for the target */

	const struct gen7_protected_regs *protected_regs;

	/** @ctxt_record_size: Size of the preemption record in bytes */

	u64 ctxt_record_size;

	/** @gen7_snapshot_block_list: Device-specific blocks dumped in the snapshot */

	const struct gen7_snapshot_block_list *gen7_snapshot_block_list;

	/** @gmu_hub_clk_freq: Gmu hub interface clock frequency */

	u64 gmu_hub_clk_freq;

	/**

	 * @bcl_data: bit 0 contains response type for bcl alarms and bits 1:21 controls sid vals

	 * to configure throttle levels for bcl alarm levels 0-2. If sid vals are not set,

	 * gmu fw sets default throttle levels.

	 */

	u32 bcl_data;

	/** @preempt_level: Preemption level valid ranges [0 to 2] */

	u32 preempt_level;

	/** @qos_value: GPU qos value to set for each RB. */

	const u32 *qos_value;

	/**

	 * @acv_perfmode_ddr_freq: Vote perfmode when DDR frequency >= acv_perfmode_ddr_freq.

	 * If not specified, vote perfmode for highest DDR level only.

	 */

	u32 acv_perfmode_ddr_freq;

	/** @acv_perfmode_vote: ACV vote for GPU perfmode */

	u32 acv_perfmode_vote;

	/** @rt_bus_hint: IB level hint for real time clients i.e. RB-0 */

	const u32 rt_bus_hint;

	/** @fast_bus_hint: Whether or not to increase IB vote on high ddr stall */

	bool fast_bus_hint;

	/** @noc_timeout_us: GPU config NOC port timeout in usec */

	u32 noc_timeout_us;

	/**

	 * @gmu_chipid: If non-zero, the exact value programmed into

	 * GEN7_GMU_GENERAL_10 to tell the GMU firmware which chip it is

	 * running on.

	 */

	u32 gmu_chipid;

};



/**

 * struct gen7_cp_preemption_record - CP context record for

 * preemption.

 * @magic: (00) Value at this offset must be equal to

 * GEN7_CP_CTXRECORD_MAGIC_REF.

 * @info: (04) Type of record. Written non-zero (usually) by CP.

 * we must set to zero for all ringbuffers.

 * @errno: (08) Error code. Initialize this to GEN7_CP_CTXRECORD_ERROR_NONE.

 * CP will update to another value if a preemption error occurs.

 * @data: (12) DATA field in YIELD and SET_MARKER packets.

 * Written by CP when switching out. Not used on switch-in. Initialized to 0.

 * @cntl: (16) RB_CNTL, saved and restored by CP. We must initialize this.

 * @rptr: (20) RB_RPTR, saved and restored by CP. We must initialize this.

 * @wptr: (24) RB_WPTR, saved and restored by CP. We must initialize this.

 * @_pad28: (28) Reserved/padding.

 * @rptr_addr: (32) RB_RPTR_ADDR_LO|HI saved and restored. We must initialize.

 * rbase: (40) RB_BASE_LO|HI saved and restored.

 * counter: (48) Pointer to preemption counter.

 * @bv_rptr_addr: (56) BV_RB_RPTR_ADDR_LO|HI save and restored. We must initialize.

 */

struct gen7_cp_preemption_record {

	u32 magic;

	u32 info;

	u32 errno;

	u32 data;

	u32 cntl;

	u32 rptr;

	u32 wptr;

	u32 _pad28;

	u64 rptr_addr;

	u64 rbase;

	u64 counter;

	u64 bv_rptr_addr;

};



/**

 * struct gen7_cp_smmu_info - CP preemption SMMU info.

 * @magic: (00) The value at this offset must be equal to

 * GEN7_CP_SMMU_INFO_MAGIC_REF

 * @_pad4: (04) Reserved/padding

 * @ttbr0: (08) Base address of the page table for the * incoming context

 * @asid: (16) Address Space IDentifier (ASID) of the incoming context

 * @context_idr: (20) Context Identification Register value

 * @context_bank: (24) Which Context Bank in SMMU to update

 */

struct gen7_cp_smmu_info {

	u32 magic;

	u32 _pad4;

	u64 ttbr0;

	u32 asid;

	u32 context_idr;

	u32 context_bank;

};



#define GEN7_CP_SMMU_INFO_MAGIC_REF		0x241350d5UL



#define GEN7_CP_CTXRECORD_MAGIC_REF		0xae399d6eUL

/* Size of each CP preemption record */

#define GEN7_CP_CTXRECORD_SIZE_IN_BYTES		(4192 * 1024)

/* Size of the performance counter save/restore block (in bytes) */

#define GEN7_CP_PERFCOUNTER_SAVE_RESTORE_SIZE	(4 * 1024)



#define GEN7_CP_RB_CNTL_DEFAULT \

	(FIELD_PREP(GENMASK(7, 0), ilog2(KGSL_RB_DWORDS >> 1)) | \

	 FIELD_PREP(GENMASK(12, 8), ilog2(4)))



/* Size of the CP_INIT pm4 stream in dwords */

#define GEN7_CP_INIT_DWORDS 10



#define GEN7_INT_MASK \

	((1 << GEN7_INT_AHBERROR) |			\

	 (1 << GEN7_INT_ATBASYNCFIFOOVERFLOW) |		\

	 (1 << GEN7_INT_GPCERROR) |			\

	 (1 << GEN7_INT_SWINTERRUPT) |			\

	 (1 << GEN7_INT_HWERROR) |			\

	 (1 << GEN7_INT_PM4CPINTERRUPT) |		\

	 (1 << GEN7_INT_RB_DONE_TS) |			\

	 (1 << GEN7_INT_CACHE_CLEAN_TS) |		\

	 (1 << GEN7_INT_ATBBUSOVERFLOW) |		\

	 (1 << GEN7_INT_HANGDETECTINTERRUPT) |		\

	 (1 << GEN7_INT_OUTOFBOUNDACCESS) |		\

	 (1 << GEN7_INT_UCHETRAPINTERRUPT) |		\

	 (1 << GEN7_INT_TSBWRITEERROR) |		\

	 (1 << GEN7_INT_SWFUSEVIOLATION))



#define GEN7_HWSCHED_INT_MASK \

	((1 << GEN7_INT_AHBERROR) |			\

	 (1 << GEN7_INT_ATBASYNCFIFOOVERFLOW) |		\

	 (1 << GEN7_INT_ATBBUSOVERFLOW) |		\

	 (1 << GEN7_INT_OUTOFBOUNDACCESS) |		\

	 (1 << GEN7_INT_UCHETRAPINTERRUPT) |		\

	 (1 << GEN7_INT_TSBWRITEERROR))



/**

 * to_gen7_core - return the gen7 specific GPU core struct

 * @adreno_dev: An Adreno GPU device handle

 *

 * Returns:

 * A pointer to the gen7 specific GPU core struct

 */

static inline const struct adreno_gen7_core *

to_gen7_core(struct adreno_device *adreno_dev)

{

	const struct adreno_gpu_core *core = adreno_dev->gpucore;



	return container_of(core, struct adreno_gen7_core, base);

}



/* Preemption functions */

void gen7_preemption_trigger(struct adreno_device *adreno_dev, bool atomic);

void gen7_preemption_schedule(struct adreno_device *adreno_dev);

void gen7_preemption_start(struct adreno_device *adreno_dev);

int gen7_preemption_init(struct adreno_device *adreno_dev);



u32 gen7_preemption_post_ibsubmit(struct adreno_device *adreno_dev,

		unsigned int *cmds);

u32 gen7_preemption_pre_ibsubmit(struct adreno_device *adreno_dev,

		struct adreno_ringbuffer *rb, struct adreno_context *drawctxt,

		u32 *cmds);



void gen7_preemption_callback(struct adreno_device *adreno_dev, int bit);



void gen7_preemption_context_destroy(struct kgsl_context *context);



void gen7_preemption_prepare_postamble(struct adreno_device *adreno_dev);



void gen7_snapshot(struct adreno_device *adreno_dev,

		struct kgsl_snapshot *snapshot);

void gen7_crashdump_init(struct adreno_device *adreno_dev);



/**

 * gen7_snapshot_external_core_regs - Dump external registers into snapshot

 * @device: Pointer to KGSL device

 * @snapshot: Pointer to the snapshot

 *

 * Dump external core registers like GPUCC, CPR into GPU snapshot.

 */

void gen7_snapshot_external_core_regs(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot);



/**

 * gen7_start - Program gen7 registers

 * @adreno_dev: An Adreno GPU handle

 *

 * This function does all gen7 register programming every

 * time we boot the gpu

 *

 * Return: 0 on success or negative on failure

 */

int gen7_start(struct adreno_device *adreno_dev);



/**

 * gen7_init - Initialize gen7 resources

 * @adreno_dev: An Adreno GPU handle

 *

 * This function does gen7 specific one time initialization

 * and is invoked when the very first client opens a

 * kgsl instance

 *

 * Return: Zero on success and negative error on failure

 */

int gen7_init(struct adreno_device *adreno_dev);



/**

 * gen7_cx_timer_init - Initialize the CX timer on Gen7 devices

 * @adreno_dev: Pointer to the adreno device

 *

 * Synchronize the GPU CX timer (if we have one) with the CPU timer

 */

void gen7_cx_timer_init(struct adreno_device *adreno_dev);



/**

 * gen7_get_gpu_feature_info - Get hardware supported feature info

 * @adreno_dev: Pointer to the adreno device

 *

 * Get HW supported feature info and update sofware feature configuration

 */

void gen7_get_gpu_feature_info(struct adreno_device *adreno_dev);



/**

 * gen7_rb_start - Gen7 specific ringbuffer setup

 * @adreno_dev: An Adreno GPU handle

 *

 * This function does gen7 specific ringbuffer setup and

 * attempts to submit CP INIT and bring GPU out of secure mode

 *

 * Return: Zero on success and negative error on failure

 */

int gen7_rb_start(struct adreno_device *adreno_dev);



/**

 * gen7_microcode_read - Get the cp microcode from the filesystem

 * @adreno_dev: An Adreno GPU handle

 *

 * This function gets the firmware from filesystem and sets up

 * the micorocode global buffer

 *

 * Return: Zero on success and negative error on failure

 */

int gen7_microcode_read(struct adreno_device *adreno_dev);



/**

 * gen7_probe_common - Probe common gen7 resources

 * @pdev: Pointer to the platform device

 * @adreno_dev: Pointer to the adreno device

 * @chipid: Chipid of the target

 * @gpucore: Pointer to the gpucore strucure

 *

 * This function sets up the gen7 resources common across all

 * gen7 targets

 */

int gen7_probe_common(struct platform_device *pdev,

	struct adreno_device *adreno_dev, u32 chipid,

	const struct adreno_gpu_core *gpucore);



/**

 * gen7_spin_idle_debug - Debug logging used when gpu fails to idle

 * @adreno_dev: An Adreno GPU handle

 * @str: String describing the failure

 *

 * This function logs interesting registers and triggers a snapshot

 */

void gen7_spin_idle_debug(struct adreno_device *adreno_dev,

	const char *str);



/**

 * gen7_perfcounter_update - Update the IFPC perfcounter list

 * @adreno_dev: An Adreno GPU handle

 * @reg: Perfcounter reg struct to add/remove to the list

 * @update_reg: true if the perfcounter needs to be programmed by the CPU

 * @pipe: pipe id for CP aperture control

 * @flags: Flags set for requested perfcounter group

 *

 * Return: 0 on success or -EBUSY if the lock couldn't be taken

 */

int gen7_perfcounter_update(struct adreno_device *adreno_dev,

	struct adreno_perfcount_register *reg, bool update_reg, u32 pipe,

	unsigned long flags);



/*

 * gen7_ringbuffer_init - Initialize the ringbuffers

 * @adreno_dev: An Adreno GPU handle

 *

 * Initialize the ringbuffer(s) for a5xx.

 * Return: 0 on success or negative on failure

 */

int gen7_ringbuffer_init(struct adreno_device *adreno_dev);



/**

 * gen7_ringbuffer_submitcmd - Submit a user command to the ringbuffer

 * @adreno_dev: An Adreno GPU handle

 * @cmdobj: Pointer to a user command object

 * @flags: Internal submit flags

 * @time: Optional pointer to a adreno_submit_time container

 *

 * Return: 0 on success or negative on failure

 */

int gen7_ringbuffer_submitcmd(struct adreno_device *adreno_dev,

		struct kgsl_drawobj_cmd *cmdobj, u32 flags,

		struct adreno_submit_time *time);



/**

 * gen7_ringbuffer_submit - Submit a command to the ringbuffer

 * @rb: Ringbuffer pointer

 * @time: Optional pointer to a adreno_submit_time container

 *

 * Return: 0 on success or negative on failure

 */

int gen7_ringbuffer_submit(struct adreno_ringbuffer *rb,

		struct adreno_submit_time *time);



/**

 * gen7_fenced_write - Write to a fenced register

 * @adreno_dev: An Adreno GPU handle

 * @offset: Register offset

 * @value: Value to write

 * @mask: Expected FENCE_STATUS for successful write

 *

 * Return: 0 on success or negative on failure

 */

int gen7_fenced_write(struct adreno_device *adreno_dev, u32 offset,

		u32 value, u32 mask);



/**

 * gen77ringbuffer_addcmds - Wrap and submit commands to the ringbuffer

 * @adreno_dev: An Adreno GPU handle

 * @rb: Ringbuffer pointer

 * @drawctxt: Draw context submitting the commands

 * @flags: Submission flags

 * @in: Input buffer to write to ringbuffer

 * @dwords: Dword length of @in

 * @timestamp: Draw context timestamp for the submission

 * @time: Optional pointer to a adreno_submit_time container

 *

 * Return: 0 on success or negative on failure

 */

int gen7_ringbuffer_addcmds(struct adreno_device *adreno_dev,

		struct adreno_ringbuffer *rb, struct adreno_context *drawctxt,

		u32 flags, u32 *in, u32 dwords, u32 timestamp,

		struct adreno_submit_time *time);



/**

 * gen7_cp_init_cmds - Create the CP_INIT commands

 * @adreno_dev: An Adreno GPU handle

 * @cmd: Buffer to write the CP_INIT commands into

 */

void gen7_cp_init_cmds(struct adreno_device *adreno_dev, u32 *cmds);



/**

 * gen7_gmu_hfi_probe - Probe Gen7 HFI specific data

 * @adreno_dev: An Adreno GPU handle

 *

 * Return: 0 on success or negative on failure

 */

int gen7_gmu_hfi_probe(struct adreno_device *adreno_dev);



static inline const struct gen7_gpudev *

to_gen7_gpudev(const struct adreno_gpudev *gpudev)

{

	return container_of(gpudev, struct gen7_gpudev, base);

}



/**

 * gen7_reset_preempt_records - Reset the preemption buffers

 * @adreno_dev: Handle to the adreno device

 *

 * Reset the preemption records at the time of hard reset

 */

void gen7_reset_preempt_records(struct adreno_device *adreno_dev);



/**

 * gen7_enable_ahb_timeout_detection - Program AHB control registers

 * @adreno_dev: An Adreno GPU handle

 *

 * Program AHB control registers to enable AHB timeout detection.

 */

void gen7_enable_ahb_timeout_detection(struct adreno_device *adreno_dev);



/**

 * gen7_scm_gpu_init_cx_regs - Program gpu regs for feature support

 * @adreno_dev: Handle to the adreno device

 *

 * Program gpu regs for feature support. Scm call for the same

 * is added from kernel version 6.0 onwards.

 *

 * Return: 0 on success or negative on failure

 */

int gen7_scm_gpu_init_cx_regs(struct adreno_device *adreno_dev);



/**

 * gen7_host_aperture_set - Program CP aperture register

 * @adreno_dev: Handle to the adreno device

 * @pipe_id: Pipe for which the register is to be set

 *

 * This function programs CP aperture register.

 */

void gen7_host_aperture_set(struct adreno_device *adreno_dev, u32 pipe_id);



#ifdef CONFIG_QCOM_KGSL_CORESIGHT

void gen7_coresight_init(struct adreno_device *device);

#else

static inline void gen7_coresight_init(struct adreno_device *device) { }

#endif



/**

 * gen7_periph_regread64 - Read 64 bit peripheral register values

 * @device: Handle to the KGSL device

 * @offsetwords_lo: Lower 32 bit address to read

 * @offsetwords_hi: Higher 32 bit address to read

 * @value: The value of register at offsetwords

 * @pipe: Pipe for which the register is to be read

 *

 * This function reads the 64 bit value for peripheral registers

 */

void gen7_periph_regread64(struct kgsl_device *device,

	u32 offsetwords_lo, u32 offsetwords_hi, u64 *value, u32 pipe);



/**

 * gen7_periph_regread - Read 32 bit peripheral register values

 * @device: Handle to the KGSL device

 * @offsetwords: 32 bit address to read

 * @value: The value of register at offsetwords

 * @pipe: Pipe for which the register is to be read

 *

 * This function reads the 32 bit value for peripheral registers

 */

void gen7_periph_regread(struct kgsl_device *device, u32 offsetwords,

	u32 *value, u32 pipe);

#endif





==========================================================================================
FILE: adreno_gen7_gmu.c
LINES: 2970 | BYTES: 82972
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include <linux/component.h>

#include <linux/delay.h>

#include <linux/dma-map-ops.h>

#include <linux/firmware.h>

#include <linux/interconnect.h>

#include <linux/io.h>

#include <linux/kobject.h>

#include <linux/slab.h>

#include <linux/sysfs.h>

#include <linux/mailbox/qmp.h>

#include <linux/vmalloc.h>



#include "adreno.h"

#include "adreno_gen7.h"

#include "adreno_trace.h"

#include "kgsl_bus.h"

#include "kgsl_device.h"

#include "kgsl_gmu_core.h"

#include "kgsl_trace.h"

#include "kgsl_util.h"



static struct gmu_vma_entry gen7_gmu_vma[] = {

	[GMU_ITCM] = {

			.start = 0x00000000,

			.size = SZ_16K,

		},

	[GMU_CACHE] = {

			.start = SZ_16K,

			.size = (SZ_16M - SZ_16K),

			.next_va = SZ_16K,

		},

	[GMU_DTCM] = {

			.start = SZ_256M + SZ_16K,

			.size = SZ_16K,

		},

	[GMU_DCACHE] = {

			.start = 0x0,

			.size = 0x0,

		},

	[GMU_NONCACHED_KERNEL] = {

			.start = 0x60000000,

			.size = SZ_512M,

			.next_va = 0x60000000,

		},

	[GMU_NONCACHED_KERNEL_EXTENDED] = {

			.start = 0xc0000000,

			.size = SZ_512M,

			.next_va = 0xc0000000,

		},

};



/**

 * adreno_gmu_chipid_based() - Return true for targets where Chipid

 * should be passed to GMU instead of GPU REV

 * @adreno_dev: A pointer to the adreno_device

 */

static inline int adreno_gmu_chipid_based(struct adreno_device *adreno_dev)

{

	return adreno_is_gen7_0_0(adreno_dev) || adreno_is_gen7_0_1(adreno_dev) ||

		adreno_is_gen7_4_0(adreno_dev) || adreno_is_gen7_3_0(adreno_dev);

}



static ssize_t log_stream_enable_store(struct kobject *kobj,

	struct kobj_attribute *attr, const char *buf, size_t count)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, log_kobj);

	bool val;

	int ret;



	ret = kstrtobool(buf, &val);

	if (ret)

		return ret;



	gmu->log_stream_enable = val;

	gmu_core_mark_for_coldboot(KGSL_DEVICE(gen7_gmu_to_adreno(gmu)));

	return count;

}



static ssize_t log_stream_enable_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, log_kobj);



	return scnprintf(buf, PAGE_SIZE, "%d\n", gmu->log_stream_enable);

}



static ssize_t log_group_mask_store(struct kobject *kobj,

	struct kobj_attribute *attr, const char *buf, size_t count)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, log_kobj);

	u32 val;

	int ret;



	ret = kstrtou32(buf, 0, &val);

	if (ret)

		return ret;



	gmu->log_group_mask = val;

	gmu_core_mark_for_coldboot(KGSL_DEVICE(gen7_gmu_to_adreno(gmu)));

	return count;

}



static ssize_t log_group_mask_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, log_kobj);



	return scnprintf(buf, PAGE_SIZE, "%x\n", gmu->log_group_mask);

}



static struct kobj_attribute log_stream_enable_attr =

	__ATTR(log_stream_enable, 0644, log_stream_enable_show, log_stream_enable_store);



static struct kobj_attribute log_group_mask_attr =

	__ATTR(log_group_mask, 0644, log_group_mask_show, log_group_mask_store);



static struct attribute *log_attrs[] = {

	&log_stream_enable_attr.attr,

	&log_group_mask_attr.attr,

	NULL,

};

ATTRIBUTE_GROUPS(log);



static struct kobj_type log_kobj_type = {

	.sysfs_ops = &kobj_sysfs_ops,

	.default_groups = log_groups,

};



static ssize_t stats_enable_store(struct kobject *kobj,

	struct kobj_attribute *attr, const char *buf, size_t count)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);

	bool val;

	int ret;



	ret = kstrtobool(buf, &val);

	if (ret)

		return ret;



	gmu->stats_enable = val;

	gmu_core_mark_for_coldboot(KGSL_DEVICE(gen7_gmu_to_adreno(gmu)));

	return count;

}



static ssize_t stats_enable_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);



	return scnprintf(buf, PAGE_SIZE, "%d\n", gmu->stats_enable);

}



static ssize_t stats_mask_store(struct kobject *kobj,

	struct kobj_attribute *attr, const char *buf, size_t count)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);

	u32 val;

	int ret;



	ret = kstrtou32(buf, 0, &val);

	if (ret)

		return ret;



	gmu->stats_mask = val;

	gmu_core_mark_for_coldboot(KGSL_DEVICE(gen7_gmu_to_adreno(gmu)));

	return count;

}



static ssize_t stats_mask_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);



	return scnprintf(buf, PAGE_SIZE, "%x\n", gmu->stats_mask);

}



static ssize_t stats_interval_store(struct kobject *kobj,

	struct kobj_attribute *attr, const char *buf, size_t count)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);

	u32 val;

	int ret;



	ret = kstrtou32(buf, 0, &val);

	if (ret)

		return ret;



	gmu->stats_interval = val;

	gmu_core_mark_for_coldboot(KGSL_DEVICE(gen7_gmu_to_adreno(gmu)));

	return count;

}



static ssize_t stats_interval_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)

{

	struct gen7_gmu_device *gmu = container_of(kobj, struct gen7_gmu_device, stats_kobj);



	return scnprintf(buf, PAGE_SIZE, "%x\n", gmu->stats_interval);

}



static struct kobj_attribute stats_enable_attr =

	__ATTR(stats_enable, 0644, stats_enable_show, stats_enable_store);



static struct kobj_attribute stats_mask_attr =

	__ATTR(stats_mask, 0644, stats_mask_show, stats_mask_store);



static struct kobj_attribute stats_interval_attr =

	__ATTR(stats_interval, 0644, stats_interval_show, stats_interval_store);



static struct attribute *stats_attrs[] = {

	&stats_enable_attr.attr,

	&stats_mask_attr.attr,

	&stats_interval_attr.attr,

	NULL,

};

ATTRIBUTE_GROUPS(stats);



static struct kobj_type stats_kobj_type = {

	.sysfs_ops = &kobj_sysfs_ops,

	.default_groups = stats_groups,

};



static int gen7_timed_poll_check_rscc(struct gen7_gmu_device *gmu,

		unsigned int offset, unsigned int expected_ret,

		unsigned int timeout, unsigned int mask)

{

	u32 value;



	return readl_poll_timeout(gmu->rscc_virt + (offset << 2), value,

		(value & mask) == expected_ret, 100, timeout * 1000);

}



struct gen7_gmu_device *to_gen7_gmu(struct adreno_device *adreno_dev)

{

	struct gen7_device *gen7_dev = container_of(adreno_dev,

					struct gen7_device, adreno_dev);



	return &gen7_dev->gmu;

}



struct adreno_device *gen7_gmu_to_adreno(struct gen7_gmu_device *gmu)

{

	struct gen7_device *gen7_dev =

			container_of(gmu, struct gen7_device, gmu);



	return &gen7_dev->adreno_dev;

}



#define RSC_CMD_OFFSET 2

#define GEN7_PDC_ENABLE_REG_VALUE 0x80000001



static void _regwrite(void __iomem *regbase,

		unsigned int offsetwords, unsigned int value)

{

	void __iomem *reg;



	reg = regbase + (offsetwords << 2);

	__raw_writel(value, reg);

}



void gen7_load_rsc_ucode(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	void __iomem *rscc = gmu->rscc_virt;

	unsigned int seq_offset = GEN7_RSCC_SEQ_MEM_0_DRV0;



	/* Disable SDE clock gating */

	_regwrite(rscc, GEN7_GPU_RSCC_RSC_STATUS0_DRV0, BIT(24));



	/* Setup RSC PDC handshake for sleep and wakeup */

	_regwrite(rscc, GEN7_RSCC_PDC_SLAVE_ID_DRV0, 1);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_DATA, 0);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_ADDR, 0);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_DATA + RSC_CMD_OFFSET, 0);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_ADDR + RSC_CMD_OFFSET, 0);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_DATA + RSC_CMD_OFFSET * 2,

		adreno_is_gen7_2_x_family(adreno_dev) ?  0x80000021 : 0x80000000);

	_regwrite(rscc, GEN7_RSCC_HIDDEN_TCS_CMD0_ADDR + RSC_CMD_OFFSET * 2, 0);

	_regwrite(rscc, GEN7_RSCC_OVERRIDE_START_ADDR, 0);

	_regwrite(rscc, GEN7_RSCC_PDC_SEQ_START_ADDR, 0x4520);

	_regwrite(rscc, GEN7_RSCC_PDC_MATCH_VALUE_LO, 0x4510);

	_regwrite(rscc, GEN7_RSCC_PDC_MATCH_VALUE_HI, 0x4514);



	if (adreno_is_gen7_2_x_family(adreno_dev))

		seq_offset = GEN7_2_0_RSCC_SEQ_MEM_0_DRV0;



	/* Load RSC sequencer uCode for sleep and wakeup */

	_regwrite(rscc, seq_offset, 0xeaaae5a0);

	_regwrite(rscc, seq_offset + 1, 0xe1a1ebab);

	_regwrite(rscc, seq_offset + 2, 0xa2e0a581);

	_regwrite(rscc, seq_offset + 3, 0xecac82e2);

	_regwrite(rscc, seq_offset + 4, 0x0020edad);

}



int gen7_load_pdc_ucode(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct platform_device *gmu_pdev = GMU_PDEV(KGSL_DEVICE(adreno_dev));

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));



	if (gmu->pdc_cfg_base == NULL) {

		struct resource *res_cfg;



		res_cfg = platform_get_resource_byname(gmu_pdev,

			IORESOURCE_MEM, "gmu_pdc");



		if (res_cfg)

			gmu->pdc_cfg_base = devm_ioremap(gmu_pdev_dev,

				res_cfg->start, resource_size(res_cfg));



		if (!gmu->pdc_cfg_base) {

			dev_err(gmu_pdev_dev, "Failed to map PDC CFG\n");

			return -ENODEV;

		}

	}



	/* Setup GPU PDC */

	_regwrite(gmu->pdc_cfg_base, GEN7_PDC_GPU_SEQ_START_ADDR, 0);

	_regwrite(gmu->pdc_cfg_base, GEN7_PDC_GPU_ENABLE_PDC,

			GEN7_PDC_ENABLE_REG_VALUE);



	if (!IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION)) {

		devm_iounmap(gmu_pdev_dev, gmu->pdc_cfg_base);

		gmu->pdc_cfg_base = NULL;

	}



	return 0;

}



/* Configure and enable GMU low power mode */

static void gen7_gmu_power_config(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	/* Disable GMU WB/RB buffer and caches at boot */

	gmu_core_regwrite(device, GEN7_GMU_SYS_BUS_CONFIG, 0x1);

	gmu_core_regwrite(device, GEN7_GMU_ICACHE_CONFIG, 0x1);

	gmu_core_regwrite(device, GEN7_GMU_DCACHE_CONFIG, 0x1);

}



static void gmu_ao_sync_event(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	unsigned long flags;

	u64 ticks;



	/*

	 * Get the GMU always on ticks and log it in a trace message. This

	 * will be used to map GMU ticks to ftrace time. Do this in atomic

	 * context to ensure nothing happens between reading the always

	 * on ticks and doing the trace.

	 */



	local_irq_save(flags);



	ticks = gpudev->read_alwayson(adreno_dev);



	trace_gmu_ao_sync(ticks);



	local_irq_restore(flags);

}



int gen7_gmu_device_start(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	gmu_core_reset_trace_header(&device->gmu_core.trace);



	gmu_ao_sync_event(adreno_dev);



	/* Bring GMU out of reset */

	gmu_core_regwrite(device, GEN7_GMU_CM3_SYSRESET, 0);



	/* Make sure the write is posted before moving ahead */

	wmb();



	if (gmu_core_timed_poll_check(device, GEN7_GMU_CM3_FW_INIT_RESULT,

			BIT(8), 100, GENMASK(8, 0))) {

		dev_err(GMU_PDEV_DEV(device),

				"GMU failed to come out of reset\n");

		gmu_core_fault_snapshot(device, GMU_FAULT_DEVICE_START);

		return -ETIMEDOUT;

	}



	return 0;

}



/*

 * gen7_gmu_hfi_start() - Write registers and start HFI.

 * @device: Pointer to KGSL device

 */

int gen7_gmu_hfi_start(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	gmu_core_regwrite(device, GEN7_GMU_HFI_CTRL_INIT, 1);



	if (gmu_core_timed_poll_check(device, GEN7_GMU_HFI_CTRL_STATUS,

			BIT(0), 100, BIT(0))) {

		dev_err(GMU_PDEV_DEV(device), "GMU HFI init failed\n");

		gmu_core_fault_snapshot(device, GMU_FAULT_HFI_INIT);

		return -ETIMEDOUT;

	}



	return 0;

}



int gen7_rscc_wakeup_sequence(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	/* Skip wakeup sequence if we didn't do the sleep sequence */

	if (!test_bit(GMU_PRIV_RSCC_SLEEP_DONE, &gmu->flags))

		return 0;



	/* RSC wake sequence */

	gmu_core_regwrite(device, GEN7_GMU_RSCC_CONTROL_REQ, BIT(1));



	/* Write request before polling */

	wmb();



	if (gmu_core_timed_poll_check(device, GEN7_GMU_RSCC_CONTROL_ACK,

				BIT(1), 100, BIT(1))) {

		dev_err(GMU_PDEV_DEV(device), "Failed to do GPU RSC power on\n");

		return -ETIMEDOUT;

	}



	if (gen7_timed_poll_check_rscc(gmu, GEN7_RSCC_SEQ_BUSY_DRV0,

				0x0, 100, UINT_MAX)) {

		dev_err(GMU_PDEV_DEV(device),

			"GPU RSC sequence stuck in waking up GPU\n");

		return -ETIMEDOUT;

	}



	gmu_core_regwrite(device, GEN7_GMU_RSCC_CONTROL_REQ, 0);



	clear_bit(GMU_PRIV_RSCC_SLEEP_DONE, &gmu->flags);



	return 0;

}



int gen7_rscc_sleep_sequence(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	int ret;



	if (!test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags))

		return 0;



	if (test_bit(GMU_PRIV_RSCC_SLEEP_DONE, &gmu->flags))

		return 0;



	gmu_core_regwrite(device, GEN7_GMU_CM3_SYSRESET, 1);

	/* Make sure M3 is in reset before going on */

	wmb();



	gmu_core_regread(device, GEN7_GMU_GENERAL_9, &gmu->log_wptr_retention);



	gmu_core_regwrite(device, GEN7_GMU_RSCC_CONTROL_REQ, BIT(0));

	/* Make sure the request completes before continuing */

	wmb();



	ret = gen7_timed_poll_check_rscc(gmu, GEN7_GPU_RSCC_RSC_STATUS0_DRV0,

			BIT(16), 100, BIT(16));

	if (ret) {

		dev_err(GMU_PDEV_DEV(device), "GPU RSC power off fail\n");

		return -ETIMEDOUT;

	}



	gmu_core_regwrite(device, GEN7_GMU_RSCC_CONTROL_REQ, 0);



	set_bit(GMU_PRIV_RSCC_SLEEP_DONE, &gmu->flags);



	return 0;

}



static void load_tcm(struct adreno_device *adreno_dev, const u8 *src,

	u32 tcm_start, u32 base, const struct gmu_block_header *blk)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 tcm_offset = tcm_start + ((blk->addr - base)/sizeof(u32));



	kgsl_regmap_bulk_write(&device->regmap, tcm_offset, src,

		blk->size >> 2);

}



int gen7_gmu_load_fw(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	const u8 *fw = (const u8 *)gmu->fw_image->data;



	while (fw < gmu->fw_image->data + gmu->fw_image->size) {

		const struct gmu_block_header *blk =

					(const struct gmu_block_header *)fw;

		int id;



		fw += sizeof(*blk);



		/* Don't deal with zero size blocks */

		if (blk->size == 0)

			continue;



		id = gmu_core_find_vma_block(device, blk->addr, blk->size);



		if (id < 0) {

			dev_err(GMU_PDEV_DEV(device),

				"Unknown block in GMU FW addr:0x%x size:0x%x\n",

				blk->addr, blk->size);

			return -EINVAL;

		}



		if (id == GMU_ITCM) {

			load_tcm(adreno_dev, fw,

				GEN7_GMU_CM3_ITCM_START,

				device->gmu_core.vma[GMU_ITCM].start, blk);

		} else if (id == GMU_DTCM) {

			load_tcm(adreno_dev, fw,

				GEN7_GMU_CM3_DTCM_START,

				device->gmu_core.vma[GMU_DTCM].start, blk);

		} else {

			/* The firmware block for memory needs to be copied on first boot only */

			if (!test_bit(GMU_PRIV_FIRST_BOOT_DONE, &gmu->flags)) {

				struct kgsl_memdesc *md =

					gmu_core_find_memdesc(device, blk->addr, blk->size);



				if (!md) {

					dev_err(GMU_PDEV_DEV(device),

						"No backing memory for GMU FW block addr:0x%x size:0x%x\n",

						blk->addr, blk->size);

					return -EINVAL;

				}



				memcpy(md->hostptr + (blk->addr - md->gmuaddr), fw,

					blk->size);

			}

		}



		fw += blk->size;

	}



	/* Proceed only after the FW is written */

	wmb();

	return 0;

}



static const char *oob_to_str(enum oob_request req)

{

	switch (req) {

	case oob_gpu:

		return "oob_gpu";

	case oob_perfcntr:

		return "oob_perfcntr";

	case oob_boot_slumber:

		return "oob_boot_slumber";

	case oob_dcvs:

		return "oob_dcvs";

	default:

		return "unknown";

	}

}



static void trigger_reset_recovery(struct adreno_device *adreno_dev,

	enum oob_request req)

{

	/*

	 * Trigger recovery for perfcounter oob only since only

	 * perfcounter oob can happen alongside an actively rendering gpu.

	 */

	if (req != oob_perfcntr)

		return;



	adreno_scheduler_fault(adreno_dev, ADRENO_GMU_FAULT_SKIP_SNAPSHOT);

}



int gen7_gmu_oob_set(struct kgsl_device *device,

		enum oob_request req)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	int ret = 0;

	int set, check;



	if (req == oob_perfcntr && gmu->num_oob_perfcntr++)

		return 0;



	if (req >= oob_boot_slumber) {

		dev_err(GMU_PDEV_DEV(device),

			"Unsupported OOB request %s\n",

			oob_to_str(req));

		return -EINVAL;

	}



	set = BIT(30 - req * 2);

	check = BIT(31 - req);



	gmu_core_regwrite(device, GEN7_GMU_HOST2GMU_INTR_SET, set);



	if (gmu_core_timed_poll_check(device, GEN7_GMU_GMU2HOST_INTR_INFO, check,

				100, check)) {

		if (req == oob_perfcntr)

			gmu->num_oob_perfcntr--;

		ret = -ETIMEDOUT;

		WARN(1, "OOB request %s timed out\n", oob_to_str(req));

		gmu_core_fault_snapshot(device, GMU_FAULT_OOB_SET);

		trigger_reset_recovery(adreno_dev, req);

	}



	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_CLR, check);



	trace_kgsl_gmu_oob_set(set);

	return ret;

}



void gen7_gmu_oob_clear(struct kgsl_device *device,

		enum oob_request req)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	int clear = BIT(31 - req * 2);



	if (req == oob_perfcntr && --gmu->num_oob_perfcntr)

		return;



	if (req >= oob_boot_slumber) {

		dev_err(GMU_PDEV_DEV(device), "Unsupported OOB clear %s\n",

				oob_to_str(req));

		return;

	}



	gmu_core_regwrite(device, GEN7_GMU_HOST2GMU_INTR_SET, clear);

	trace_kgsl_gmu_oob_clear(clear);

}



void gen7_gmu_irq_enable(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct gen7_hfi *hfi = &gmu->hfi;



	/* Clear pending IRQs and Unmask needed IRQs */

	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_CLR, UINT_MAX);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_CLR, UINT_MAX);



	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_MASK,

			(unsigned int)~HFI_IRQ_MASK);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_MASK,

			(unsigned int)~GMU_AO_INT_MASK);



	/* Enable all IRQs on host */

	enable_irq(hfi->irq);

	enable_irq(gmu->irq);

}



void gen7_gmu_irq_disable(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct gen7_hfi *hfi = &gmu->hfi;



	/* Disable all IRQs on host */

	disable_irq(gmu->irq);

	disable_irq(hfi->irq);



	/* Mask all IRQs and clear pending IRQs */

	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_MASK, UINT_MAX);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_MASK, UINT_MAX);



	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_CLR, UINT_MAX);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_CLR, UINT_MAX);

}



static int gen7_gmu_hfi_start_msg(struct adreno_device *adreno_dev)

{

	struct hfi_start_cmd req;

	int ret;



	ret = CMD_MSG_HDR(req, H2F_MSG_START);

	if (ret)

		return ret;



	return gen7_hfi_send_generic_req(adreno_dev, &req, sizeof(req));

}



static u32 gen7_rscc_tcsm_drv0_status_reglist[] = {

	GEN7_RSCC_TCS0_DRV0_STATUS,

	GEN7_RSCC_TCS1_DRV0_STATUS,

	GEN7_RSCC_TCS2_DRV0_STATUS,

	GEN7_RSCC_TCS3_DRV0_STATUS,

	GEN7_RSCC_TCS4_DRV0_STATUS,

	GEN7_RSCC_TCS5_DRV0_STATUS,

	GEN7_RSCC_TCS6_DRV0_STATUS,

	GEN7_RSCC_TCS7_DRV0_STATUS,

	GEN7_RSCC_TCS8_DRV0_STATUS,

	GEN7_RSCC_TCS9_DRV0_STATUS,

};



static u32 gen7_2_0_rscc_tcsm_drv0_status_reglist[] = {

	GEN7_2_0_RSCC_TCS0_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS1_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS2_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS3_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS4_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS5_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS6_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS7_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS8_DRV0_STATUS,

	GEN7_2_0_RSCC_TCS9_DRV0_STATUS,

};



static int gen7_complete_rpmh_votes(struct gen7_gmu_device *gmu,

		u32 timeout)

{

	struct adreno_device *adreno_dev = gen7_gmu_to_adreno(gmu);

	int i, ret = 0;



	if (adreno_is_gen7_2_x_family(adreno_dev)) {

		for (i = 0; i < ARRAY_SIZE(gen7_2_0_rscc_tcsm_drv0_status_reglist); i++)

			ret |= gen7_timed_poll_check_rscc(gmu,

				gen7_2_0_rscc_tcsm_drv0_status_reglist[i], BIT(0), timeout,

				BIT(0));

	} else {

		for (i = 0; i < ARRAY_SIZE(gen7_rscc_tcsm_drv0_status_reglist); i++)

			ret |= gen7_timed_poll_check_rscc(gmu,

				gen7_rscc_tcsm_drv0_status_reglist[i], BIT(0), timeout,

				BIT(0));

	}



	if (ret)

		dev_err(GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev)),

				"RPMH votes timedout: %d\n", ret);



	return ret;

}



#define GX_GDSC_POWER_OFF	BIT(0)

#define GX_CLK_OFF		BIT(1)

#define is_on(val)		(!(val & (GX_GDSC_POWER_OFF | GX_CLK_OFF)))



bool gen7_gmu_gx_is_on(struct adreno_device *adreno_dev)

{

	unsigned int val;



	gmu_core_regread(KGSL_DEVICE(adreno_dev),

			GEN7_GMU_GFX_PWR_CLK_STATUS, &val);

	return is_on(val);

}



static bool gen7_gmu_rpmh_pwr_state_is_active(struct kgsl_device *device)

{

	u32 val;



	gmu_core_regread(device, GEN7_GPU_GMU_CX_GMU_RPMH_POWER_STATE, &val);

	return (val == GPU_HW_ACTIVE) ? true : false;

}



static const char *idle_level_name(int level)

{

	if (level == GPU_HW_ACTIVE)

		return "GPU_HW_ACTIVE";

	else if (level == GPU_HW_IFPC)

		return "GPU_HW_IFPC";



	return "(Unknown)";

}



int gen7_gmu_wait_for_lowest_idle(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u32 reg, reg1, reg2, reg3, reg4;

	unsigned long t;

	u64 ts1, ts2;



	ts1 = gpudev->read_alwayson(adreno_dev);



	t = jiffies + msecs_to_jiffies(100);

	do {

		gmu_core_regread(device,

			GEN7_GPU_GMU_CX_GMU_RPMH_POWER_STATE, &reg);

		gmu_core_regread(device, GEN7_GMU_GFX_PWR_CLK_STATUS, &reg1);



		/*

		 * Check that we are at lowest level. If lowest level is IFPC

		 * double check that GFX clock is off.

		 */

		if (gmu->idle_level == reg)

			if (!(gmu->idle_level == GPU_HW_IFPC && is_on(reg1)))

				return 0;



		/* Wait 100us to reduce unnecessary AHB bus traffic */

		usleep_range(10, 100);

	} while (!time_after(jiffies, t));



	/* Check one last time */

	gmu_core_regread(device, GEN7_GPU_GMU_CX_GMU_RPMH_POWER_STATE, &reg);

	gmu_core_regread(device, GEN7_GMU_GFX_PWR_CLK_STATUS, &reg1);



	/*

	 * Check that we are at lowest level. If lowest level is IFPC

	 * double check that GFX clock is off.

	 */

	if (gmu->idle_level == reg)

		if (!(gmu->idle_level == GPU_HW_IFPC && is_on(reg1)))

			return 0;



	ts2 = gpudev->read_alwayson(adreno_dev);



	/* Collect abort data to help with debugging */

	gmu_core_regread(device, GEN7_GPU_GMU_AO_GPU_CX_BUSY_STATUS, &reg2);

	gmu_core_regread(device, GEN7_GMU_RBBM_INT_UNMASKED_STATUS, &reg3);

	gmu_core_regread(device, GEN7_GMU_GMU_PWR_COL_KEEPALIVE, &reg4);



	dev_err(gmu_pdev_dev,

		"----------------------[ GMU error ]----------------------\n");

	dev_err(gmu_pdev_dev,

		"Timeout waiting for lowest idle level %s\n",

		idle_level_name(gmu->idle_level));

	dev_err(gmu_pdev_dev, "Start: %llx (absolute ticks)\n", ts1);

	dev_err(gmu_pdev_dev,

		"Poll: %llx (ticks relative to start)\n", ts2-ts1);

	dev_err(gmu_pdev_dev,

		"RPMH_POWER_STATE=%x GFX_PWR_CLK_STATUS=%x\n", reg, reg1);

	dev_err(gmu_pdev_dev, "CX_BUSY_STATUS=%x\n", reg2);

	dev_err(gmu_pdev_dev,

		"RBBM_INT_UNMASKED_STATUS=%x PWR_COL_KEEPALIVE=%x\n", reg3, reg4);



	/* Access GX registers only when GX is ON */

	if (is_on(reg1)) {

		kgsl_regread(device, GEN7_CP_STATUS_1, &reg2);

		kgsl_regread(device, GEN7_CP_CP2GMU_STATUS, &reg3);

		kgsl_regread(device, GEN7_CP_CONTEXT_SWITCH_CNTL, &reg4);



		dev_err(gmu_pdev_dev, "GEN7_CP_STATUS_1=%x\n", reg2);

		dev_err(gmu_pdev_dev,

			"CP2GMU_STATUS=%x CONTEXT_SWITCH_CNTL=%x\n", reg3, reg4);

	}



	WARN_ON(1);

	gmu_core_fault_snapshot(device, GMU_FAULT_WAIT_FOR_LOWEST_IDLE);

	return -ETIMEDOUT;

}



/* Bitmask for GPU idle status check */

#define CXGXCPUBUSYIGNAHB	BIT(30)

int gen7_gmu_wait_for_idle(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u32 status2;

	u64 ts1;



	ts1 = gpudev->read_alwayson(adreno_dev);

	if (gmu_core_timed_poll_check(device, GEN7_GPU_GMU_AO_GPU_CX_BUSY_STATUS,

			0, 100, CXGXCPUBUSYIGNAHB)) {

		gmu_core_regread(device,

				GEN7_GPU_GMU_AO_GPU_CX_BUSY_STATUS2, &status2);

		dev_err(GMU_PDEV_DEV(device),

				"GMU not idling: status2=0x%x %llx %llx\n",

				status2, ts1,

				gpudev->read_alwayson(adreno_dev));

		gmu_core_fault_snapshot(device, GMU_FAULT_WAIT_FOR_IDLE);

		return -ETIMEDOUT;

	}



	return 0;

}



int gen7_gmu_version_info(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gmu_core_device *gmu = &device->gmu_core;

	const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);



	/* GMU version info is at a fixed offset in the DTCM */

	gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + 0xff8,

			&gmu->ver.core);

	gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + 0xff9,

			&gmu->ver.core_dev);

	gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + 0xffa,

			&gmu->ver.pwr);

	gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + 0xffb,

			&gmu->ver.pwr_dev);

	gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + 0xffc,

			&gmu->ver.hfi);



	/* Check if gmu fw version on device is compatible with kgsl driver */

	if (gmu->ver.core < gen7_core->gmu_fw_version) {

		dev_err_once(GMU_PDEV_DEV(device),

			     "GMU FW version 0x%x error (expected 0x%x)\n",

			     gmu->ver.core, gen7_core->gmu_fw_version);

		return -EINVAL;

	}

	return 0;

}



int gen7_gmu_itcm_shadow(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	u32 i, *dest;



	if (gmu->itcm_shadow)

		return 0;



	gmu->itcm_shadow = vzalloc(device->gmu_core.vma[GMU_ITCM].size);

	if (!gmu->itcm_shadow)

		return -ENOMEM;



	dest = (u32 *)gmu->itcm_shadow;



	for (i = 0; i < (device->gmu_core.vma[GMU_ITCM].size >> 2); i++)

		gmu_core_regread(KGSL_DEVICE(adreno_dev),

			GEN7_GMU_CM3_ITCM_START + i, dest++);



	return 0;

}



void gen7_gmu_register_config(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);

	u32 val;



	/* Clear any previously set cm3 fault */

	atomic_set(&gmu->cm3_fault, 0);



	/* Vote veto for FAL10 */

	gmu_core_regwrite(device, GEN7_GPU_GMU_CX_GMU_CX_FALNEXT_INTF, 0x1);

	gmu_core_regwrite(device, GEN7_GPU_GMU_CX_GMU_CX_FAL_INTF, 0x1);



	/* Clear init result to make sure we are getting fresh value */

	gmu_core_regwrite(device, GEN7_GMU_CM3_FW_INIT_RESULT, 0);

	gmu_core_regwrite(device, GEN7_GMU_CM3_BOOT_CONFIG, 0x2);



	gmu_core_regwrite(device, GEN7_GMU_HFI_QTBL_ADDR,

			gmu->hfi.hfi_mem->gmuaddr);

	gmu_core_regwrite(device, GEN7_GMU_HFI_QTBL_INFO, 1);



	gmu_core_regwrite(device, GEN7_GMU_AHB_FENCE_RANGE_0, BIT(31) |

			FIELD_PREP(GENMASK(30, 18), 0x32) |

			FIELD_PREP(GENMASK(17, 0), GEN7_CP_CONTEXT_SWITCH_CNTL));



	/*

	 * Make sure that CM3 state is at reset value. Snapshot is changing

	 * NMI bit and if we boot up GMU with NMI bit set GMU will boot

	 * straight in to NMI handler without executing __main code

	 */

	gmu_core_regwrite(device, GEN7_GMU_CM3_CFG, 0x4052);



	/* Set up GBIF registers from the GPU core definition */

	kgsl_regmap_multi_write(&device->regmap, gen7_core->gbif,

		gen7_core->gbif_count);



	if (adreno_is_gen7_13_0(adreno_dev) || adreno_is_gen7_9_x(adreno_dev))

		kgsl_regrmw(device, GEN7_GBIF_CX_CONFIG, GENMASK(31, 29),

				FIELD_PREP(GENMASK(31, 29), 1));

	else if (adreno_is_gen7_14_0_family(adreno_dev))

		kgsl_regrmw(device, GEN7_GBIF_CX_CONFIG, GENMASK(31, 29),

				FIELD_PREP(GENMASK(31, 29), 2));



	/**

	 * We may have asserted gbif halt as part of reset sequence which may

	 * not get cleared if the gdsc was not reset. So clear it before

	 * attempting GMU boot.

	 */

	kgsl_regwrite(device, GEN7_GBIF_HALT, 0x0);



	/* Set vrb address before starting GMU */

	if (!IS_ERR_OR_NULL(device->gmu_core.vrb))

		gmu_core_regwrite(device, GEN7_GMU_GENERAL_11, device->gmu_core.vrb->gmuaddr);



	/* Set the log wptr index */

	gmu_core_regwrite(device, GEN7_GMU_GENERAL_9,

			gmu->log_wptr_retention);



	/* Pass chipid to GMU FW, must happen before starting GMU */

	if (gen7_core->gmu_chipid)

		val = gen7_core->gmu_chipid;

	else if (adreno_gmu_chipid_based(adreno_dev))

		val = ADRENO_GMU_CHIPID(adreno_dev->chipid);

	else

		val = ADRENO_GMU_REV(ADRENO_GPUREV(adreno_dev));

	gmu_core_regwrite(device, GEN7_GMU_GENERAL_10, val);



	/* Log size is encoded in (number of 4K units - 1) */

	val = (gmu->gmu_log->gmuaddr & GENMASK(31, 12)) |

		((GMU_LOG_SIZE/SZ_4K - 1) & GENMASK(7, 0));

	gmu_core_regwrite(device, GEN7_GMU_GENERAL_8, val);



	/* Configure power control and bring the GMU out of reset */

	gen7_gmu_power_config(adreno_dev);



	/*

	 * Enable BCL throttling -

	 * XOCLK1: countable: 0x13 (25% throttle)

	 * XOCLK2: countable: 0x17 (58% throttle)

	 * XOCLK3: countable: 0x19 (75% throttle)

	 * POWER_CONTROL_SELECT_0 controls counters 0 - 3, each selector

	 * is 8 bits wide.

	 */

	if (adreno_dev->bcl_enabled)

		gmu_core_regrmw(device, GEN7_GMU_CX_GMU_POWER_COUNTER_SELECT_0,

			0xffffff00, FIELD_PREP(GENMASK(31, 24), 0x19) |

			FIELD_PREP(GENMASK(23, 16), 0x17) |

			FIELD_PREP(GENMASK(15, 8), 0x13));

}



int gen7_gmu_parse_fw(struct adreno_device *adreno_dev)

{

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);

	struct gmu_block_header *blk;

	int ret, offset = 0;

	const char *gmufw_name = gen7_core->gmufw_name;



	/*

	 * If GMU fw already saved and verified, do nothing new.

	 * Skip only requesting firmware and allow preallocation to

	 * ensure in scenario where GMU request firmware succeeded

	 * but preallocation fails, we don't return early without

	 * successful preallocations on next open call.

	 */

	if (!gmu->fw_image) {



		if (gen7_core->gmufw_name == NULL)

			return -EINVAL;



		ret = adreno_request_firmware(&gmu->fw_image, gmufw_name,

				gmu_pdev_dev, false);

		if (ret) {

			if (gen7_core->gmufw_bak_name) {

				gmufw_name = gen7_core->gmufw_bak_name;

				ret = adreno_request_firmware(&gmu->fw_image, gmufw_name,

					gmu_pdev_dev, true);

			}



			if (ret)

				return ret;

		}

	}



	/*

	 * Zero payload fw blocks contain metadata and are

	 * guaranteed to precede fw load data. Parse the

	 * metadata blocks.

	 */

	while (offset < gmu->fw_image->size) {

		blk = (struct gmu_block_header *)&gmu->fw_image->data[offset];



		if (offset + sizeof(*blk) > gmu->fw_image->size) {

			dev_err(gmu_pdev_dev, "Invalid FW Block\n");

			return -EINVAL;

		}



		/* Done with zero length blocks so return */

		if (blk->size)

			break;



		offset += sizeof(*blk);



		if (blk->type == GMU_BLK_TYPE_PREALLOC_REQ ||

			blk->type == GMU_BLK_TYPE_PREALLOC_PERSIST_REQ) {

			ret = gmu_core_process_prealloc(KGSL_DEVICE(adreno_dev), blk);



			if (ret)

				return ret;

		}

	}



	return 0;

}



int gen7_gmu_memory_init(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	/* GMU master log */

	if (IS_ERR_OR_NULL(gmu->gmu_log))

		gmu->gmu_log = gmu_core_reserve_kernel_block(device, 0,

				GMU_LOG_SIZE, GMU_NONCACHED_KERNEL, 0);



	return PTR_ERR_OR_ZERO(gmu->gmu_log);

}



static int gen7_gmu_init(struct adreno_device *adreno_dev)

{

	int ret;



	ret = gen7_gmu_parse_fw(adreno_dev);

	if (ret)

		return ret;



	ret = gen7_gmu_memory_init(adreno_dev);

	if (ret)

		return ret;



	return gen7_hfi_init(adreno_dev);

}



static void _do_gbif_halt(struct kgsl_device *device, u32 reg, u32 ack_reg,

	u32 mask, const char *client)

{

	u32 ack;

	unsigned long t;



	kgsl_regwrite(device, reg, mask);



	t = jiffies + msecs_to_jiffies(100);

	do {

		kgsl_regread(device, ack_reg, &ack);

		if ((ack & mask) == mask)

			return;



		/*

		 * If we are attempting recovery in case of stall-on-fault

		 * then the halt sequence will not complete as long as SMMU

		 * is stalled.

		 */

		kgsl_mmu_pagefault_resume(&device->mmu, false);



		usleep_range(10, 100);

	} while (!time_after(jiffies, t));



	/* Check one last time */

	kgsl_mmu_pagefault_resume(&device->mmu, false);



	kgsl_regread(device, ack_reg, &ack);

	if ((ack & mask) == mask)

		return;



	dev_err(device->dev, "%s GBIF halt timed out\n", client);

}



static void gen7_gmu_pwrctrl_suspend(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	/* Disconnect GPU from BUS is not needed if CX GDSC goes off later */



	/*

	 * GEMNOC can enter power collapse state during GPU power down sequence.

	 * This could abort CX GDSC collapse. Assert Qactive to avoid this.

	 */

	gmu_core_regwrite(device, GEN7_GPU_GMU_CX_GMU_CX_FALNEXT_INTF, 0x1);



	/* Check no outstanding RPMh voting */

	gen7_complete_rpmh_votes(gmu, 1);



	/* Clear the WRITEDROPPED fields and set fence to allow mode */

	gmu_core_regwrite(device, GEN7_GMU_AHB_FENCE_STATUS_CLR, 0x7);

	gmu_core_regwrite(device, GEN7_GMU_AO_AHB_FENCE_CTRL, 0);



	/* Make sure above writes are committed before we proceed to recovery */

	wmb();



	gmu_core_regwrite(device, GEN7_GMU_CM3_SYSRESET, 1);



	/* Halt GX traffic */

	if (gen7_gmu_gx_is_on(adreno_dev))

		_do_gbif_halt(device, GEN7_RBBM_GBIF_HALT,

				GEN7_RBBM_GBIF_HALT_ACK,

				GEN7_GBIF_GX_HALT_MASK,

				"GX");



	/* Halt CX traffic */

	_do_gbif_halt(device, GEN7_GBIF_HALT, GEN7_GBIF_HALT_ACK,

			GEN7_GBIF_ARB_HALT_MASK, "CX");



	/*

	 * GX_CXO_CLK is needed to access RBBM_SW_RESET_CMD register. There are

	 * scenarios where the IFPC exit sequence is still in progress, and the

	 * above clock may not be enabled. This situation leads to unclocked

	 * access. Thus, trigger RBBM_SW_RESET_CMD only when GPU is fully active

	 * i.e., IFPC sequence is completed.

	 */

	if (gen7_gmu_gx_is_on(adreno_dev) && gen7_gmu_rpmh_pwr_state_is_active(device))

		kgsl_regwrite(device, GEN7_RBBM_SW_RESET_CMD, 0x1);



	/* Make sure above writes are posted before turning off power resources */

	wmb();



	/* Allow the software reset to complete */

	udelay(100);



	/*

	 * This is based on the assumption that GMU is the only one controlling

	 * the GX HS. This code path is the only client voting for GX from linux

	 * kernel.

	 */

	if (!gen7_gmu_gx_is_on(adreno_dev))

		return;



	/*

	 * Switch gx gdsc control from GMU to CPU force non-zero reference

	 * count in clk driver so next disable call will turn off the GDSC

	 */

	kgsl_pwrctrl_enable_gx_gdsc(device);

	kgsl_pwrctrl_disable_gx_gdsc(device);



	if (gen7_gmu_gx_is_on(adreno_dev))

		dev_err(GMU_PDEV_DEV(device), "gx is stuck on\n");

}



/*

 * gen7_gmu_notify_slumber() - initiate request to GMU to prepare to slumber

 * @device: Pointer to KGSL device

 */

static int gen7_gmu_notify_slumber(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	int bus_level = pwr->pwrlevels[pwr->default_pwrlevel].bus_freq;

	int perf_idx = gmu->dcvs_table.gpu_level_num -

			pwr->default_pwrlevel - 1;

	struct hfi_prep_slumber_cmd req = {

		.freq = perf_idx,

		.bw = bus_level,

	};

	int ret;



	req.bw |= adreno_gmu_bus_ab_quantize(adreno_dev, 0);



	/* Disable the power counter so that the GMU is not busy */

	gmu_core_regwrite(device, GEN7_GMU_CX_GMU_POWER_COUNTER_ENABLE, 0);



	ret = CMD_MSG_HDR(req, H2F_MSG_PREPARE_SLUMBER);

	if (ret)

		return ret;



	ret = gen7_hfi_send_generic_req(adreno_dev, &req, sizeof(req));



	/* Make sure the fence is in ALLOW mode */

	gmu_core_regwrite(device, GEN7_GMU_AO_AHB_FENCE_CTRL, 0);



	/*

	 * GEMNOC can enter power collapse state during GPU power down sequence.

	 * This could abort CX GDSC collapse. Assert Qactive to avoid this.

	 */

	gmu_core_regwrite(device, GEN7_GPU_GMU_CX_GMU_CX_FALNEXT_INTF, 0x1);



	return ret;

}



void gen7_gmu_suspend(struct adreno_device *adreno_dev, bool force)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!force && test_bit(ADRENO_DEVICE_RESET_RECOVERY, &adreno_dev->priv))

		return;



	gen7_gmu_pwrctrl_suspend(adreno_dev);



	gmu_core_disable_clks(device);



	kgsl_pwrctrl_disable_cx_gdsc(device);



	gmu_core_rdpm_cx_freq_update(device, 0);



	dev_err(GMU_PDEV_DEV(device), "Suspended GMU\n");



	kgsl_pwrctrl_set_state(device, KGSL_STATE_NONE);

}



static int gen7_gmu_dcvs_set(struct adreno_device *adreno_dev,

		int gpu_pwrlevel, int bus_level, u32 ab)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct gen7_dcvs_table *table = &gmu->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 = 0;



	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))

		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;



	req.bw |= adreno_gmu_bus_ab_quantize(adreno_dev, ab);



	/* GMU will vote for slumber levels through the sleep sequence */

	if ((req.freq == INVALID_DCVS_IDX) && (req.bw == INVALID_BW_VOTE))

		return 0;



	ret = CMD_MSG_HDR(req, H2F_MSG_GX_BW_PERF_VOTE);

	if (ret)

		return ret;



	ret = gen7_hfi_send_generic_req(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 |

				ADRENO_GMU_FAULT_SKIP_SNAPSHOT);

	}



	if (req.freq != INVALID_DCVS_IDX)

		gmu_core_rdpm_mx_freq_update(device, table->gx_votes[req.freq].freq);



	return ret;

}



static int gen7_gmu_clock_set(struct adreno_device *adreno_dev, u32 pwrlevel)

{

	return gen7_gmu_dcvs_set(adreno_dev, pwrlevel, INVALID_DCVS_IDX, INVALID_AB_VALUE);

}



static int gen7_gmu_ifpc_store(struct kgsl_device *device,

		unsigned int val)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	unsigned int requested_idle_level;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_IFPC))

		return -EINVAL;



	if (val)

		requested_idle_level = GPU_HW_IFPC;

	else

		requested_idle_level = GPU_HW_ACTIVE;



	if (gmu->idle_level == requested_idle_level)

		return 0;



	/* Power down the GPU before changing the idle level */

	return adreno_power_cycle_u32(adreno_dev, &gmu->idle_level,

		requested_idle_level);

}



static unsigned int gen7_gmu_ifpc_isenabled(struct kgsl_device *device)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(ADRENO_DEVICE(device));



	return gmu->idle_level == GPU_HW_IFPC;

}



/* Send an NMI to the GMU */

void gen7_gmu_send_nmi(struct kgsl_device *device, bool force,

		       enum gmu_fault_panic_policy gf_policy)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u64 ticks = gpudev->read_alwayson(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	u32 result;



	/*

	 * Do not send NMI if the SMMU is stalled because GMU will not be able

	 * to save cm3 state to DDR.

	 */

	if (gen7_gmu_gx_is_on(adreno_dev) && adreno_smmu_is_stalled(adreno_dev)) {

		dev_err(GMU_PDEV_DEV(device),

			"Skipping NMI because SMMU is stalled\n");

		goto done;

	}



	if (force)

		goto nmi;



	/*

	 * We should not send NMI if there was a CM3 fault reported because we

	 * don't want to overwrite the critical CM3 state captured by gmu before

	 * it sent the CM3 fault interrupt. Also don't send NMI if GMU reset is

	 * already active. We could have hit a GMU assert and NMI might have

	 * already been triggered.

	 */



	/* make sure we're reading the latest cm3_fault */

	smp_rmb();



	if (atomic_read(&gmu->cm3_fault))

		return;



	gmu_core_regread(device, GEN7_GMU_CM3_FW_INIT_RESULT, &result);



	if (result & 0xE00)

		return;



nmi:

	/* Mask so there's no interrupt caused by NMI */

	gmu_core_regwrite(device, GEN7_GMU_GMU2HOST_INTR_MASK, UINT_MAX);



	/* Make sure the interrupt is masked before causing it */

	wmb();



	/* This will cause the GMU to save it's internal state to ddr */

	gmu_core_regrmw(device, GEN7_GMU_CM3_CFG, BIT(9), BIT(9));



	/* Make sure the NMI is invoked before we proceed*/

	wmb();



	/* Wait for the NMI to be handled */

	udelay(200);

done:

	KGSL_GMU_CORE_FORCE_PANIC(device->gmu_core.gf_panic, GMU_PDEV(device), ticks, gf_policy);

}



static void gen7_gmu_cooperative_reset(struct kgsl_device *device)

{

	unsigned int result;



	gmu_core_regwrite(device, GEN7_GMU_CX_GMU_WDOG_CTRL, 0);

	gmu_core_regwrite(device, GEN7_GMU_HOST2GMU_INTR_SET, BIT(17));



	/*

	 * After triggering graceful death wait for snapshot ready

	 * indication from GMU.

	 */

	if (!gmu_core_timed_poll_check(device, GEN7_GMU_CM3_FW_INIT_RESULT,

				0x800, 2, 0x800))

		return;



	gmu_core_regread(device, GEN7_GMU_CM3_FW_INIT_RESULT, &result);

	dev_err(GMU_PDEV_DEV(device),

		"GMU cooperative reset timed out 0x%x\n", result);

	/*

	 * If we dont get a snapshot ready from GMU, trigger NMI

	 * and if we still timeout then we just continue with reset.

	 */

	gen7_gmu_send_nmi(device, true, GMU_FAULT_PANIC_NONE);



	gmu_core_regread(device, GEN7_GMU_CM3_FW_INIT_RESULT, &result);

	if ((result & 0x800) != 0x800)

		dev_err(GMU_PDEV_DEV(device),

			"GMU cooperative reset NMI timed out 0x%x\n", result);

}



static int gen7_gmu_wait_for_active_transition(struct kgsl_device *device)

{

	unsigned int reg;



	if (gmu_core_timed_poll_check(device, GEN7_GPU_GMU_CX_GMU_RPMH_POWER_STATE,

			GPU_HW_ACTIVE, 100, GENMASK(3, 0))) {

		gmu_core_regread(device, GEN7_GPU_GMU_CX_GMU_RPMH_POWER_STATE, &reg);

		dev_err(GMU_PDEV_DEV(device),

			"GMU failed to move to ACTIVE state, Current state: 0x%x\n",

			reg);



		return -ETIMEDOUT;

	}



	return 0;

}



static bool gen7_gmu_scales_bandwidth(struct kgsl_device *device)

{

	return true;

}



void gen7_gmu_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, GEN7_GMU_AO_HOST_INTERRUPT_MASK, &mask);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_MASK,

			(mask | GMU_INT_WDOG_BITE));



	gen7_gmu_send_nmi(device, false, GMU_FAULT_PANIC_NONE);



	dev_err_ratelimited(GMU_PDEV_DEV(device),

			"GMU watchdog expired interrupt received\n");

}



static irqreturn_t gen7_gmu_irq_handler(int irq, void *data)

{

	struct kgsl_device *device = data;

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(device);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct gen7_gpudev *gen7_gpudev =

		to_gen7_gpudev(ADRENO_GPU_DEVICE(adreno_dev));

	unsigned int status = 0;



	gmu_core_regread(device, GEN7_GMU_AO_HOST_INTERRUPT_STATUS, &status);

	gmu_core_regwrite(device, GEN7_GMU_AO_HOST_INTERRUPT_CLR, status);



	if (status & GMU_INT_HOST_AHB_BUS_ERR)

		dev_err_ratelimited(gmu_pdev_dev,

				"AHB bus error interrupt received\n");



	if (status & GMU_INT_WDOG_BITE)

		gen7_gpudev->handle_watchdog(adreno_dev);



	if (status & GMU_INT_FENCE_ERR) {

		unsigned int fence_status;



		gmu_core_regread(device, GEN7_GMU_AHB_FENCE_STATUS,

			&fence_status);

		dev_err_ratelimited(gmu_pdev_dev,

			"FENCE error interrupt received %x\n", fence_status);

	}



	if (status & ~GMU_AO_INT_MASK)

		dev_err_ratelimited(gmu_pdev_dev,

				"Unhandled GMU interrupts 0x%lx\n",

				status & ~GMU_AO_INT_MASK);



	return IRQ_HANDLED;

}



void gen7_gmu_aop_send_acd_state(struct gen7_gmu_device *gmu, bool flag)

{

	struct kgsl_device *device = KGSL_DEVICE(gen7_gmu_to_adreno(gmu));

	char msg_buf[36];

	u32 size;

	int ret;



	if (IS_ERR_OR_NULL(gmu->qmp) && IS_ERR_OR_NULL(gmu->mailbox.channel))

		return;



	size = scnprintf(msg_buf, sizeof(msg_buf),

			"{class: gpu, res: acd, val: %d}", flag);



	/* mailbox controller expects 4-byte aligned buffer */

	size = ALIGN((size + 1), SZ_4);



	if (gmu->qmp) {

		ret = qmp_send(gmu->qmp, msg_buf, size);

	} else {

		struct qmp_pkt msg;



		msg.size = size;

		msg.data = msg_buf;



		ret = mbox_send_message(gmu->mailbox.channel, &msg);

	}



	if (ret < 0)

		dev_err(GMU_PDEV_DEV(device),

			"AOP send message failed: %d\n", ret);

}



#if IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION)

static void gen7_gmu_force_first_boot(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	u32 val = 0;



	if (gmu->pdc_cfg_base) {

		kgsl_pwrctrl_enable_cx_gdsc(device);

		gmu_core_enable_clks(device, 0);



		val = __raw_readl(gmu->pdc_cfg_base + (GEN7_PDC_GPU_ENABLE_PDC << 2));



		/* Make sure we read val before disabling clks. */

		mb();



		gmu_core_disable_clks(device);

		kgsl_pwrctrl_disable_cx_gdsc(device);

		gmu_core_rdpm_cx_freq_update(device, 0);

	}



	if (val != GEN7_PDC_ENABLE_REG_VALUE) {

		clear_bit(GMU_PRIV_RSCC_SLEEP_DONE, &gmu->flags);

		clear_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags);

	}

}

#endif



static int gen7_gmu_first_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	int level, ret;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_AWARE);



	gen7_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, 0);

	if (ret)

		goto gdsc_off;



	/*

	 * Enable AHB timeout detection to catch any register access taking longer

	 * time before NOC timeout gets detected. Enable this logic before any

	 * register access which happens to be just after enabling clocks.

	 */

	gen7_enable_ahb_timeout_detection(adreno_dev);



	/* Initialize the CX timer */

	gen7_cx_timer_init(adreno_dev);



	ret = gen7_gmu_load_fw(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = gen7_gmu_version_info(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = gen7_gmu_itcm_shadow(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = gen7_scm_gpu_init_cx_regs(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	gen7_gmu_register_config(adreno_dev);



	gen7_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 GPU faults that might have been left over */

	adreno_clear_gpu_fault(adreno_dev);



	ret = gen7_gmu_device_start(adreno_dev);

	if (ret)

		goto err;



	if (!test_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags)) {

		ret = gen7_load_pdc_ucode(adreno_dev);

		if (ret)

			goto err;



		gen7_load_rsc_ucode(adreno_dev);

		set_bit(GMU_PRIV_PDC_RSC_LOADED, &gmu->flags);

	}



	ret = gen7_gmu_hfi_start(adreno_dev);

	if (ret)

		goto err;



	gen7_get_gpu_feature_info(adreno_dev);



	ret = gen7_hfi_start(adreno_dev);

	if (ret)

		goto err;



	if (adreno_dev->gmu_ab &&

		gen7_hfi_send_get_value(adreno_dev, HFI_VALUE_GMU_AB_VOTE, 0) == 1 &&

		!WARN_ONCE(!adreno_dev->gpucore->num_ddr_channels,

			"Number of DDR channel is not specified in gpu core")) {

		set_bit(ADRENO_DEVICE_GMU_AB, &adreno_dev->priv);

	} else {

		/* If gmu_ab feature flag is enabled but GMU doesn't support it, set it to false */

		adreno_dev->gmu_ab = false;

	}



	icc_set_bw(pwr->icc_path, 0, 0);



	device->gmu_fault = false;



	kgsl_pwrctrl_set_state(device, KGSL_STATE_AWARE);



	return 0;



err:

	gen7_gmu_irq_disable(adreno_dev);



	if (device->gmu_fault) {

		gen7_gmu_suspend(adreno_dev, false);

		return ret;

	}



clks_gdsc_off:

	gmu_core_disable_clks(device);



gdsc_off:

	kgsl_pwrctrl_disable_cx_gdsc(device);



	gmu_core_rdpm_cx_freq_update(device, 0);



	return ret;

}



static int gen7_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, 0);

	if (ret)

		goto gdsc_off;



	/*

	 * Enable AHB timeout detection to catch any register access taking longer

	 * time before NOC timeout gets detected. Enable this logic before any

	 * register access which happens to be just after enabling clocks.

	 */

	gen7_enable_ahb_timeout_detection(adreno_dev);



	/* Initialize the CX timer */

	gen7_cx_timer_init(adreno_dev);



	ret = gen7_rscc_wakeup_sequence(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	ret = gen7_gmu_load_fw(adreno_dev);

	if (ret)

		goto clks_gdsc_off;



	gen7_gmu_register_config(adreno_dev);



	gen7_gmu_irq_enable(adreno_dev);



	/* Clear any GPU faults that might have been left over */

	adreno_clear_gpu_fault(adreno_dev);



	ret = gen7_gmu_device_start(adreno_dev);

	if (ret)

		goto err;



	ret = gen7_gmu_hfi_start(adreno_dev);

	if (ret)

		goto err;



	ret = gen7_hfi_start(adreno_dev);

	if (ret)

		goto err;



	device->gmu_fault = false;



	kgsl_pwrctrl_set_state(device, KGSL_STATE_AWARE);



	return 0;



err:

	gen7_gmu_irq_disable(adreno_dev);



	if (device->gmu_fault) {

		gen7_gmu_suspend(adreno_dev, false);

		return ret;

	}



clks_gdsc_off:

	gmu_core_disable_clks(device);



gdsc_off:

	kgsl_pwrctrl_disable_cx_gdsc(device);



	gmu_core_rdpm_cx_freq_update(device, 0);



	return ret;

}



static void set_acd(struct adreno_device *adreno_dev, void *priv)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	adreno_dev->acd_enabled = *((bool *)priv);

	gen7_gmu_aop_send_acd_state(gmu, adreno_dev->acd_enabled);

}



static int gen7_gmu_acd_set(struct kgsl_device *device, bool val)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	if (!gmu->qmp && !gmu->mailbox.channel)

		return -EINVAL;



	/* Don't do any unneeded work if ACD is already in the correct state */

	if (adreno_dev->acd_enabled == val)

		return 0;



	/* Power cycle the GPU for changes to take effect */

	return adreno_power_cycle(adreno_dev, set_acd, &val);

}



#define BCL_RESP_TYPE_MASK   BIT(0)

#define BCL_SID0_MASK        GENMASK(7, 1)

#define BCL_SID1_MASK        GENMASK(14, 8)

#define BCL_SID2_MASK        GENMASK(21, 15)



static int gen7_bcl_sid_set(struct kgsl_device *device, u32 sid_id, u64 sid_val)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	u32 bcl_data, val = (u32) sid_val;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_BCL) ||

		!FIELD_GET(BCL_RESP_TYPE_MASK, adreno_dev->bcl_data))

		return -EINVAL;



	switch (sid_id) {

	case 0:

		adreno_dev->bcl_data &= ~BCL_SID0_MASK;

		bcl_data = adreno_dev->bcl_data | FIELD_PREP(BCL_SID0_MASK, val);

		break;

	case 1:

		adreno_dev->bcl_data &= ~BCL_SID1_MASK;

		bcl_data = adreno_dev->bcl_data | FIELD_PREP(BCL_SID1_MASK, val);

		break;

	case 2:

		adreno_dev->bcl_data &= ~BCL_SID2_MASK;

		bcl_data = adreno_dev->bcl_data | FIELD_PREP(BCL_SID2_MASK, val);

		break;

	default:

		return -EINVAL;

	}



	return adreno_power_cycle_u32(adreno_dev, &adreno_dev->bcl_data, bcl_data);

}



static u64 gen7_bcl_sid_get(struct kgsl_device *device, u32 sid_id)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_BCL) ||

		!FIELD_GET(BCL_RESP_TYPE_MASK, adreno_dev->bcl_data))

		return 0;



	switch (sid_id) {

========== PART_09.txt ==========
	case 0:

		return ((u64) FIELD_GET(BCL_SID0_MASK, adreno_dev->bcl_data));

	case 1:

		return ((u64) FIELD_GET(BCL_SID1_MASK, adreno_dev->bcl_data));

	case 2:

		return ((u64) FIELD_GET(BCL_SID2_MASK, adreno_dev->bcl_data));

	default:

		return 0;

	}

}



static const struct gmu_dev_ops gen7_gmudev = {

	.oob_set = gen7_gmu_oob_set,

	.oob_clear = gen7_gmu_oob_clear,

	.ifpc_store = gen7_gmu_ifpc_store,

	.ifpc_isenabled = gen7_gmu_ifpc_isenabled,

	.cooperative_reset = gen7_gmu_cooperative_reset,

	.wait_for_active_transition = gen7_gmu_wait_for_active_transition,

	.scales_bandwidth = gen7_gmu_scales_bandwidth,

	.acd_set = gen7_gmu_acd_set,

	.bcl_sid_set = gen7_bcl_sid_set,

	.bcl_sid_get = gen7_bcl_sid_get,

	.send_nmi = gen7_gmu_send_nmi,

#if IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION)

	.force_first_boot = gen7_gmu_force_first_boot,

#endif

};



static int gen7_gmu_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;



	/* Target gen7_9_x votes for perfmode through ACV. Skip icc path for same */

	if (!adreno_is_gen7_9_x(adreno_dev))

		kgsl_icc_set_tag(pwr, buslevel);



	if (buslevel == pwr->cur_buslevel)

		buslevel = INVALID_DCVS_IDX;



	if ((ab == pwr->cur_ab) || ((ab == 0) && (adreno_dev->gmu_ab)))

		ab = INVALID_AB_VALUE;



	if ((ab == INVALID_AB_VALUE) && (buslevel == INVALID_DCVS_IDX))

		return 0;



	ret = gen7_gmu_dcvs_set(adreno_dev, INVALID_DCVS_IDX,

			buslevel, ab);

	if (ret)

		return ret;



	if (buslevel != INVALID_DCVS_IDX)

		pwr->cur_buslevel = buslevel;



	if (ab != INVALID_AB_VALUE) {

		if (!adreno_dev->gmu_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 gen7_gmu_aop_messaging_init(struct adreno_device *adreno_dev,

		struct gen7_gmu_device *gmu)

{

	struct kgsl_mailbox *mailbox = &gmu->mailbox;

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(KGSL_DEVICE(adreno_dev));

	int ret;



	if (of_find_property(gmu_pdev_dev->of_node, "qcom,qmp", NULL)) {

		gmu->qmp = qmp_get(gmu_pdev_dev);

		ret = PTR_ERR_OR_ZERO(gmu->qmp);

		if (ret) {

			/* Clear the error pointer to ensure a clean state */

			gmu->qmp = NULL;

			return ret;

		}

		goto done;

	}



	mailbox->client.dev = gmu_pdev_dev;

	mailbox->client.tx_block = true;

	mailbox->client.tx_tout = 1000;

	mailbox->client.knows_txdone = false;



	mailbox->channel = mbox_request_channel(&mailbox->client, 0);

	ret = PTR_ERR_OR_ZERO(mailbox->channel);

	if (ret) {

		mailbox->channel = NULL;

		return ret;

	}



done:

	adreno_dev->acd_enabled = true;



	return 0;

}



static int gen7_gmu_acd_probe(struct kgsl_device *device,

		struct gen7_gmu_device *gmu, struct device_node *node)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct kgsl_pwrlevel *pwrlevel =

			&pwr->pwrlevels[pwr->num_pwrlevels - 1];

	struct hfi_acd_table_cmd *cmd = &gmu->hfi.acd_table;

	int ret, i, cmd_idx = 0;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_ACD))

		return 0;



	cmd->hdr = CREATE_MSG_HDR(H2F_MSG_ACD_TBL, HFI_MSG_CMD);



	cmd->version = 1;

	cmd->stride = 1;

	cmd->enable_by_level = 0;



	/*

	 * Iterate through each gpu power level and generate a mask for GMU

	 * firmware for ACD enabled levels and store the corresponding control

	 * register configurations to the acd_table structure.

	 */

	for (i = 0; i < pwr->num_pwrlevels; i++) {

		if (pwrlevel->acd_level) {

			cmd->enable_by_level |= (1 << (i + 1));

			cmd->data[cmd_idx++] = pwrlevel->acd_level;

		}

		pwrlevel--;

	}



	if (!cmd->enable_by_level)

		return 0;



	cmd->num_levels = cmd_idx;



	ret = gen7_gmu_aop_messaging_init(adreno_dev, gmu);



	if (ret)

		dev_err_probe(GMU_PDEV_DEV(device), ret,

			"AOP messaging init failed\n");



	/*

	 * Only fail probe for -EPROBE_DEFER; ignore other errors to avoid

	 * failing probe if ACD init fails.

	 */



	return ret == -EPROBE_DEFER ? ret : 0;

}



static int gen7_gmu_reg_probe(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	ret = kgsl_regmap_add_region(&device->regmap,

				GMU_PDEV(device), "gmu", -EINVAL, NULL, NULL);



	if (ret)

		dev_err(GMU_PDEV_DEV(device),

				"Unable to map the GMU registers\n");

	/*

	 * gmu_ao_blk_dec1 and gmu_ao_blk_dec2 are contiguous and contained within the gmu region

	 * mapped above. gmu_ao_blk_dec0 is not within the gmu region and is mapped separately.

	 */

	kgsl_regmap_add_region(&device->regmap, GMU_PDEV(device),

			"gmu_ao_blk_dec0", -EINVAL, NULL, NULL);



	return ret;

}



void gen7_gmu_remove(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	if (gmu->qmp)

		qmp_put(gmu->qmp);



	if (gmu->mailbox.channel)

		mbox_free_channel(gmu->mailbox.channel);



	adreno_dev->acd_enabled = false;



	if (gmu->fw_image)

		release_firmware(gmu->fw_image);



	gmu_core_free_globals(device);



	vfree(gmu->itcm_shadow);

	if (gmu->log_kobj.state_initialized)

		kobject_put(&gmu->log_kobj);

	if (gmu->stats_kobj.state_initialized)

		kobject_put(&gmu->stats_kobj);

}



/* Default IFPC timer (300usec) value */

#define GEN7_GMU_LONG_IFPC_HYST	FIELD_PREP(GENMASK(15, 0), 0x1680)



/* Minimum IFPC timer (200usec) allowed to override default value */

#define GEN7_GMU_LONG_IFPC_HYST_FLOOR	FIELD_PREP(GENMASK(15, 0), 0x0F00)



/* Offsets into the MX/CX mapped register regions */

#define GEN7_RDPM_MX_OFFSET 0xf00

#define GEN7_RDPM_CX_OFFSET 0xf14



int gen7_gmu_probe(struct kgsl_device *device,

		struct platform_device *pdev)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct device *dev = &pdev->dev;

	u64 freq = gen7_core->gmu_hub_clk_freq;

	struct resource *res;

	int ret, i;



	adreno_dev->gmu_hub_clk_freq = freq ? freq : 150000000;



	device->gmu_core.pdev = pdev;



	dma_coerce_mask_and_coherent(&device->gmu_core.pdev->dev, DMA_BIT_MASK(64));

	set_dma_ops(GMU_PDEV_DEV(device), NULL);



	/* In standard device tree bindings, rscc range is part of GMU pdev */

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rscc");

	if (!res)

		res = platform_get_resource_byname(device->pdev,

			IORESOURCE_MEM, "rscc");



	if (!res) {

		dev_err(GMU_PDEV_DEV(device), "Failed to get rscc resource\n");

		return -ENODEV;

	}



	gmu->rscc_virt = devm_ioremap(GMU_PDEV_DEV(device), res->start,

					resource_size(res));

	if (!gmu->rscc_virt) {

		dev_err(GMU_PDEV_DEV(device), "rscc ioremap failed\n");

		return -ENOMEM;

	}



	/* Setup any rdpm register ranges */

	gmu_core_rdpm_probe(device);



	device->gmu_core.rdpm_cx_offset = GEN7_RDPM_CX_OFFSET;

	device->gmu_core.rdpm_mx_offset = GEN7_RDPM_MX_OFFSET;



	/* Set up GMU gdscs */

	ret = kgsl_pwrctrl_probe_gdscs(device, pdev);

	if (ret)

		return ret;



	ret = gmu_core_clk_probe(device);

	if (ret)

		return ret;



	/* Set up GMU IOMMU and shared memory with GMU */

	ret = gmu_core_iommu_init(device);

	if (ret)

		goto error;



	device->gmu_core.vma = gen7_gmu_vma;

	device->gmu_core.num_vmas = ARRAY_SIZE(gen7_gmu_vma);



	for (i = 0; i < device->gmu_core.num_vmas; i++) {

		struct gmu_vma_entry *vma = &gen7_gmu_vma[i];



		vma->vma_root = RB_ROOT;

		spin_lock_init(&vma->lock);

	}



	/* Map and reserve GMU CSRs registers */

	ret = gen7_gmu_reg_probe(adreno_dev);

	if (ret)

		goto error;



	if (ADRENO_FEATURE(adreno_dev, ADRENO_GMU_AB))

		adreno_dev->gmu_ab = true;



	/* Populates RPMh configurations */

	ret = gen7_build_rpmh_tables(adreno_dev);

	if (ret)

		goto error;



	/* Set up GMU idle state */

	if (ADRENO_FEATURE(adreno_dev, ADRENO_IFPC)) {

		gmu->idle_level = GPU_HW_IFPC;

		adreno_dev->ifpc_hyst = GEN7_GMU_LONG_IFPC_HYST;

		adreno_dev->ifpc_hyst_floor = GEN7_GMU_LONG_IFPC_HYST_FLOOR;

	} else {

		gmu->idle_level = GPU_HW_ACTIVE;

	}



	ret = gen7_gmu_acd_probe(device, gmu, pdev->dev.of_node);

	if (ret)

		goto error;



	set_bit(GMU_ENABLED, &device->gmu_core.flags);



	device->gmu_core.dev_ops = &gen7_gmudev;



	/* Set default GMU attributes */

	gmu->log_stream_enable = false;

	gmu->log_group_mask = 0x3;



	/* Initialize to zero to detect trace packet loss */

	device->gmu_core.trace.seq_num = 0;



	/* Disabled by default */

	gmu->stats_enable = false;

	/* Set default to CM3 busy cycles countable */

	gmu->stats_mask = BIT(GEN7_GMU_CM3_BUSY_CYCLES);

	/* Interval is in 50 us units. Set default sampling frequency to 4x50 us */

	gmu->stats_interval = HFI_FEATURE_GMU_STATS_INTERVAL;



	/* GMU sysfs nodes setup */

	ret = kobject_init_and_add(&gmu->log_kobj, &log_kobj_type, &dev->kobj, "log");

	if (ret)

		dev_err(dev, "Failed to add log_kobj: %d\n", ret);



	ret = kobject_init_and_add(&gmu->stats_kobj, &stats_kobj_type, &dev->kobj, "stats");

	if (ret)

		dev_err(dev, "Failed to add stats_kobj: %d\n", ret);



	spin_lock_init(&gmu->hfi.cmdq_lock);



	gmu->irq = kgsl_request_irq(GMU_PDEV(device), "gmu", NULL, -EINVAL,

		gen7_gmu_irq_handler, device);



	if (gmu->irq >= 0)

		return 0;



	ret = gmu->irq;



error:

	gen7_gmu_remove(device);

	return ret;

}



int gen7_halt_gbif(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	/* Halt new client requests */

	kgsl_regwrite(device, GEN7_GBIF_HALT, GEN7_GBIF_CLIENT_HALT_MASK);

	ret = adreno_wait_for_halt_ack(device,

		GEN7_GBIF_HALT_ACK, GEN7_GBIF_CLIENT_HALT_MASK);



	/* Halt all AXI requests */

	kgsl_regwrite(device, GEN7_GBIF_HALT, GEN7_GBIF_ARB_HALT_MASK);

	ret = adreno_wait_for_halt_ack(device,

		GEN7_GBIF_HALT_ACK, GEN7_GBIF_ARB_HALT_MASK);



	/* De-assert the halts */

	kgsl_regwrite(device, GEN7_GBIF_HALT, 0x0);



	return ret;

}



static int gen7_gmu_power_off(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(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 = gen7_gmu_wait_for_lowest_idle(adreno_dev);

	if (ret)

		goto error;



	ret = gen7_complete_rpmh_votes(gmu, 2);

	if (ret)

		goto error;



	ret = gen7_gmu_notify_slumber(adreno_dev);

	if (ret)

		goto error;



	ret = gen7_gmu_wait_for_idle(adreno_dev);

	if (ret)

		goto error;



	ret = gen7_rscc_sleep_sequence(adreno_dev);

	if (ret)

		goto error;



	gmu_core_rdpm_mx_freq_update(device, 0);



	/* Now that we are done with GMU and GPU, Clear the GBIF */

	ret = gen7_halt_gbif(adreno_dev);

	if (ret)

		goto error;



	gen7_gmu_irq_disable(adreno_dev);



	gen7_hfi_stop(adreno_dev);



	gmu_core_disable_clks(device);



	kgsl_pwrctrl_disable_cx_gdsc(device);



	gmu_core_rdpm_cx_freq_update(device, 0);



	kgsl_pwrctrl_set_state(device, KGSL_STATE_NONE);



	return 0;



error:

	gen7_gmu_irq_disable(adreno_dev);

	gen7_hfi_stop(adreno_dev);

	gen7_gmu_suspend(adreno_dev, false);



	return ret;

}



void gen7_enable_gpu_irq(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	kgsl_pwrctrl_irq(device, true);



	adreno_irqctrl(adreno_dev, 1);

}



void gen7_disable_gpu_irq(struct adreno_device *adreno_dev)

{

	kgsl_pwrctrl_irq(KGSL_DEVICE(adreno_dev), false);



	if (gen7_gmu_gx_is_on(adreno_dev))

		adreno_irqctrl(adreno_dev, 0);

}



static int gen7_gpu_boot(struct adreno_device *adreno_dev)

{

	const struct adreno_gen7_core *gen7_core = to_gen7_core(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	adreno_set_active_ctxs_null(adreno_dev);



	ret = kgsl_mmu_start(device);

	if (ret)

		goto err;



	ret = gen7_gmu_oob_set(device, oob_gpu);

	if (ret)

		goto oob_clear;



	ret = gen7_gmu_hfi_start_msg(adreno_dev);

	if (ret)

		goto oob_clear;



	/* Clear the busy_data stats - we're starting over from scratch */

	memset(&adreno_dev->busy_data, 0, sizeof(adreno_dev->busy_data));



	gen7_start(adreno_dev);



	if (gen7_core->qos_value && adreno_is_preemption_enabled(adreno_dev))

		kgsl_regwrite(device, GEN7_RBBM_GBIF_CLIENT_QOS_CNTL,

			gen7_core->qos_value[adreno_dev->cur_rb->id]);



	/* 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);



	gen7_enable_gpu_irq(adreno_dev);



	ret = gen7_rb_start(adreno_dev);

	if (ret) {

		gen7_disable_gpu_irq(adreno_dev);

		adreno_llcc_slice_deactivate(adreno_dev);

		goto oob_clear;

	}



	/*

	 * 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;



	/* Start the dispatcher */

	adreno_dispatcher_start(device);



	device->reset_counter++;



	gen7_gmu_oob_clear(device, oob_gpu);



	return 0;



oob_clear:

	gen7_gmu_oob_clear(device, oob_gpu);



err:

	gen7_gmu_power_off(adreno_dev);



	return ret;

}



static void gmu_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 gen7_boot(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	bool bcl_state = adreno_dev->bcl_enabled;

	int ret;



	if (WARN_ON(test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags)))

		return 0;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	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 = gen7_gmu_first_boot(adreno_dev);

	} else {

		ret = gen7_gmu_boot(adreno_dev);

	}



	if (ret)

		return ret;



	ret = gen7_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 gen7_first_boot(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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 gen7_boot(adreno_dev);



		return 0;

	}



	ret = gen7_ringbuffer_init(adreno_dev);

	if (ret)

		return ret;



	ret = gen7_microcode_read(adreno_dev);

	if (ret)

		return ret;



	ret = gen7_init(adreno_dev);

	if (ret)

		return ret;



	ret = gen7_gmu_init(adreno_dev);

	if (ret)

		return ret;



	kgsl_pwrctrl_request_state(device, KGSL_STATE_ACTIVE);



	ret = gen7_gmu_first_boot(adreno_dev);

	if (ret)

		return ret;



	ret = gen7_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. For kernel version prior to 6.1, 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 bool gen7_irq_pending(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 status;



	kgsl_regread(device, GEN7_RBBM_INT_0_STATUS, &status);



	/* Return busy if a interrupt is pending */

	return ((status & adreno_dev->irq_mask) ||

		atomic_read(&adreno_dev->pending_irq_refcnt));

}



static int gen7_power_off(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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 = gen7_gmu_oob_set(device, oob_gpu);

	if (ret)

		goto no_gx_power;



	if (gen7_irq_pending(adreno_dev)) {

		gen7_gmu_oob_clear(device, oob_gpu);

		return -EBUSY;

	}



	kgsl_pwrscale_update_stats(device);



	/* Save active coresight registers if applicable */

	adreno_coresight_stop(adreno_dev);



	adreno_irqctrl(adreno_dev, 0);



no_gx_power:

	gen7_gmu_oob_clear(device, oob_gpu);



	kgsl_pwrctrl_irq(device, false);



	gen7_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);



	/*

	 * Reset the context records so that CP can start

	 * at the correct read pointer for BV thread after

	 * coming out of slumber.

	 */

	gen7_reset_preempt_records(adreno_dev);



	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 gen7_gmu_device *gmu = to_gen7_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 = gen7_power_off(adreno_dev);

	if (ret == -EBUSY) {

		kgsl_pwrscale_update(device);

		kgsl_start_idle_timer(device);

	}



done:

	kgsl_mutex_unlock(&device->mutex);

}



static int gen7_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 = gen7_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 gen7_gmu_last_close(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	if (test_bit(GMU_PRIV_GPU_STARTED, &gmu->flags))

		return gen7_power_off(adreno_dev);



	return 0;

}



static int gen7_gmu_active_count_get(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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 = gen7_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 gen7_gmu_pm_suspend(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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))

		gen7_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 gen7_gmu_pm_resume(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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 gen7_gmu_touch_wakeup(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct gen7_gmu_device *gmu = to_gen7_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 = gen7_gmu_boot(adreno_dev);

	if (ret)

		return;



	ret = gen7_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 gen7_gmu_power_ops = {

	.first_open = gen7_gmu_first_open,

	.last_close = gen7_gmu_last_close,

	.active_count_get = gen7_gmu_active_count_get,

	.pm_suspend = gen7_gmu_pm_suspend,

	.pm_resume = gen7_gmu_pm_resume,

	.touch_wakeup = gen7_gmu_touch_wakeup,

	.gpu_clock_set = gen7_gmu_clock_set,

	.gpu_bus_set = gen7_gmu_bus_set,

};



int gen7_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 gen7_device *gen7_dev;

	int ret;



	gen7_dev = devm_kzalloc(&pdev->dev, sizeof(*gen7_dev),

			GFP_KERNEL);

	if (!gen7_dev)

		return -ENOMEM;



	adreno_dev = &gen7_dev->adreno_dev;



	adreno_dev->irq_mask = GEN7_INT_MASK;



	ret = gen7_probe_common(pdev, adreno_dev, chipid, gpucore);

	if (ret)

		return ret;



	ret = adreno_dispatcher_init(adreno_dev);

	if (ret) {

		dev_err(&pdev->dev, "adreno dispatcher init failed ret %d\n", 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);



	if (ADRENO_FEATURE(adreno_dev, ADRENO_DMS)) {

		set_bit(ADRENO_DEVICE_DMS, &adreno_dev->priv);

		adreno_dev->dms_enabled = true;

	}



	return 0;

}



int gen7_gmu_reset(struct adreno_device *adreno_dev)

{

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	gen7_disable_gpu_irq(adreno_dev);



	gen7_gmu_irq_disable(adreno_dev);



	gen7_hfi_stop(adreno_dev);



	/* Hard reset the gmu and gpu */

	gen7_gmu_suspend(adreno_dev, true);



	gen7_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 gen7_boot(adreno_dev);

}



int gen7_gmu_hfi_probe(struct adreno_device *adreno_dev)

{

	struct platform_device *gmu_pdev = GMU_PDEV(KGSL_DEVICE(adreno_dev));

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	struct gen7_hfi *hfi = &gmu->hfi;



	hfi->irq = kgsl_request_irq(gmu_pdev, "hfi", NULL, -EINVAL,

		gen7_hfi_irq_handler, KGSL_DEVICE(adreno_dev));



	return hfi->irq < 0 ? hfi->irq : 0;

}



int gen7_gmu_add_to_minidump(struct adreno_device *adreno_dev)

{

	struct gen7_device *gen7_dev = container_of(adreno_dev,

					struct gen7_device, adreno_dev);

	int ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_GEN7_DEVICE,

			(void *)(gen7_dev), sizeof(struct gen7_device));

	if (ret)

		return ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_GMU_LOG_ENTRY,

			gen7_dev->gmu.gmu_log->hostptr, gen7_dev->gmu.gmu_log->size);

	if (ret)

		return ret;



	ret = kgsl_add_va_to_minidump(adreno_dev->dev.dev, KGSL_HFIMEM_ENTRY,

			gen7_dev->gmu.hfi.hfi_mem->hostptr, gen7_dev->gmu.hfi.hfi_mem->size);



	return ret;

}



static int gen7_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 gen7_gpudev *gen7_gpudev = to_gen7_gpudev(gpudev);

	int ret;



	ret = gen7_gmu_probe(device, to_platform_device(dev));

	if (ret)

		return ret;



	if (gen7_gpudev->hfi_probe) {

		ret = gen7_gpudev->hfi_probe(adreno_dev);



		if (ret) {

			gen7_gmu_remove(device);

			return ret;

		}

	}



	return 0;

}



static void gen7_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 gen7_gpudev *gen7_gpudev = to_gen7_gpudev(gpudev);



	if (gen7_gpudev->hfi_remove)

		gen7_gpudev->hfi_remove(adreno_dev);



	gen7_gmu_remove(device);

}



static const struct component_ops gen7_gmu_component_ops = {

	.bind = gen7_gmu_bind,

	.unbind = gen7_gmu_unbind,

};



static int gen7_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, &gen7_gmu_component_ops);

}



#if (KERNEL_VERSION(6, 10, 0) <= LINUX_VERSION_CODE)

static void gen7_gmu_remove_dev(struct platform_device *pdev)

{

	component_del(&pdev->dev, &gen7_gmu_component_ops);

}

#else

static int gen7_gmu_remove_dev(struct platform_device *pdev)

{

	component_del(&pdev->dev, &gen7_gmu_component_ops);

	return 0;

}

#endif



static const struct of_device_id gen7_gmu_match_table[] = {

	{ .compatible = "qcom,gen7-gmu" },

	{ .compatible = "qcom,adreno-gmu" },

	{ },

};



struct platform_driver gen7_gmu_driver = {

	.probe = gen7_gmu_probe_dev,

	.remove = gen7_gmu_remove_dev,

	.driver = {

		.name = "adreno-gen7-gmu",

		.of_match_table = gen7_gmu_match_table,

	},

};





==========================================================================================
FILE: adreno_gen7_gmu.h
LINES: 369 | BYTES: 11518
==========================================================================================

/* SPDX-License-Identifier: GPL-2.0-only */

/*

 * Copyright (c) 2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */

#ifndef __ADRENO_GEN7_GMU_H

#define __ADRENO_GEN7_GMU_H



#include <linux/mailbox_client.h>

#include <linux/soc/qcom/qcom_aoss.h>



#include "adreno_gen7_hfi.h"

#include "kgsl_gmu_core.h"



struct gen7_dcvs_table {

	u32 gpu_level_num;

	u32 gmu_level_num;

	struct opp_gx_desc gx_votes[MAX_GX_LEVELS];

	struct opp_desc cx_votes[MAX_CX_LEVELS_LEGACY];

};



/**

 * struct gen7_gmu_device - GMU device structure

 * @ver: GMU Version information

 * @irq: GMU interrupt number

 * @fw_image: GMU FW image

 * @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 gen7_gmu_device {

	int irq;

	const struct firmware *fw_image;

	struct kgsl_memdesc *gmu_log;

	/** @gmu_init_scratch: Memory to store the initial HFI messages */

	struct kgsl_memdesc *gmu_init_scratch;

	/** @gpu_boot_scratch: Memory to store the bootup HFI messages */

	struct kgsl_memdesc *gpu_boot_scratch;

	struct gen7_hfi hfi;

	unsigned int idle_level;

	/** @qmp: Pointer to qmp struct used for AOP messages*/

	struct qmp *qmp;

	struct kgsl_mailbox mailbox;

	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;

	/** @pdc_cfg_base: Base address of PDC cfg registers */

	void __iomem *pdc_cfg_base;

	/** @num_oob_perfcntr: Number of active oob_perfcntr requests */

	u32 num_oob_perfcntr;

	/** @acd_debug_val: DVM value to calibrate ACD for a level */

	u32 acd_debug_val;

	/** @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;

	/** @cp_init_hdr: raw command header for cp_init */

	u32 cp_init_hdr;

	/** @switch_to_unsec_hdr: raw command header for switch to unsecure packet */

	u32 switch_to_unsec_hdr;

	/** @dcvs_table: Table for gpu dcvs levels */

	struct gen7_dcvs_table dcvs_table;

};



/* Helper function to get to gen7 gmu device from adreno device */

struct gen7_gmu_device *to_gen7_gmu(struct adreno_device *adreno_dev);



/* Helper function to get to adreno device from gen7 gmu device */

struct adreno_device *gen7_gmu_to_adreno(struct gen7_gmu_device *gmu);



/**

 * gen7_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 gen7_build_rpmh_tables(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_gx_is_on(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_device_probe - GEN7 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 gen7 targets.

 */

int gen7_gmu_device_probe(struct platform_device *pdev,

		u32 chipid, const struct adreno_gpu_core *gpucore);



/**

 * gen7_gmu_reset - Reset and restart the gmu

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int gen7_gmu_reset(struct adreno_device *adreno_dev);



/**

 * gen7_enable_gpu_irq - Enable gpu interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_enable_gpu_irq(struct adreno_device *adreno_dev);



/**

 * gen7_disable_gpu_irq - Disable gpu interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_disable_gpu_irq(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7 snapshot

 */

void gen7_gmu_snapshot(struct adreno_device *adreno_dev,

		struct kgsl_snapshot *snapshot);



/**

 * gen7_gmu_probe - Probe gen7 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 gen7_gmu_probe(struct kgsl_device *device,

		struct platform_device *pdev);



/**

 * gen7_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 gen7_gmu_parse_fw(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_memory_init(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_aop_send_acd_state - Enable or disable acd feature in aop

 * @gmu: Pointer to the gen7 gmu device

 * @flag: Boolean to enable or disable acd in aop

 *

 * This function enables or disables gpu acd feature using mailbox or qmp

 */

void gen7_gmu_aop_send_acd_state(struct gen7_gmu_device *gmu, bool flag);



/**

 * gen7_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 gen7_gmu_load_fw(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_device_start(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_hfi_start - Indicate hfi start to gmu

 * @device: Pointer to the kgsl device

 *

 * Return: 0 on success or negative error on failure

 */

int gen7_gmu_hfi_start(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_itcm_shadow(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_register_config - gmu register configuration

 * @adreno_dev: Pointer to the adreno device

 *

 * Program gmu regsiters based on features

 */

void gen7_gmu_register_config(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_version_info - Get gmu firmware version

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int gen7_gmu_version_info(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_irq_enable - Enable gmu interrupts

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_gmu_irq_enable(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_irq_disable - Disaable gmu interrupts

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_gmu_irq_disable(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_suspend(struct adreno_device *adreno_dev, bool force);



/**

 * gen7_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 gen7_gmu_oob_set(struct kgsl_device *device, enum oob_request oob);



/**

 * gen7_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 gen7_gmu_oob_clear(struct kgsl_device *device, enum oob_request oob);



/**

 * gen7_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 gen7_gmu_wait_for_lowest_idle(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_wait_for_idle(struct adreno_device *adreno_dev);



/**

 * gen7_rscc_sleep_sequence - Trigger rscc sleep sequence

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int gen7_rscc_sleep_sequence(struct adreno_device *adreno_dev);



/**

 * gen7_rscc_wakeup_sequence - Trigger rscc wakeup sequence

 * @adreno_dev: Pointer to the adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int gen7_rscc_wakeup_sequence(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_halt_gbif(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_load_pdc_ucode(struct adreno_device *adreno_dev);



/**

 * gen7_load_rsc_ucode - Load rscc sequence

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_load_rsc_ucode(struct adreno_device *adreno_dev);



/**

 * gen7_gmu_remove - Clean up gmu probed resources

 * @device: Pointer to the kgsl device

 */

void gen7_gmu_remove(struct kgsl_device *device);



/**

 * gen7_gmu_handle_watchdog - Handle watchdog interrupt

 * @adreno_dev: Pointer to the adreno device

 */

void gen7_gmu_handle_watchdog(struct adreno_device *adreno_dev);



/**

 * gen7_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 gen7_gmu_send_nmi(struct kgsl_device *device, bool force,

		       enum gmu_fault_panic_policy gf_policy);



/**

 * gen7_gmu_add_to_minidump - Register gen7_device with va minidump

 * @adreno_dev: Pointer to the adreno device

 */

int gen7_gmu_add_to_minidump(struct adreno_device *adreno_dev);

#endif





==========================================================================================
FILE: adreno_gen7_gmu_snapshot.c
LINES: 250 | BYTES: 8164
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include "gen7_reg.h"

#include "adreno.h"

#include "adreno_gen7.h"

#include "adreno_gen7_gmu.h"

#include "adreno_snapshot.h"

#include "adreno_gen7_0_0_snapshot.h"

#include "adreno_gen7_2_0_snapshot.h"

#include "adreno_gen7_6_0_snapshot.h"

#include "kgsl_device.h"

#include "kgsl_gmu_core.h"



static size_t gen7_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;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	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;



	/*

	 * Read of GMU TCMs over side-band debug controller interface is

	 * supported on gen7_2_x family

	 */

	if (adreno_is_gen7_2_x_family(adreno_dev)) {

		/*

		 * region [20]: Dump ITCM/DTCM. Select 1 for DTCM.

		 * autoInc [31]: Autoincrement the address field after each

		 * access to TCM_DBG_DATA

		 */

		kgsl_regwrite(device, GEN7_CX_DBGC_TCM_DBG_ADDR, BIT(20) | BIT(31));



		for (i = 0; i < (gmu_core->vma[GMU_DTCM].size >> 2); i++)

			kgsl_regread(device, GEN7_CX_DBGC_TCM_DBG_DATA, data++);

	} else {

		for (i = 0; i < (gmu_core->vma[GMU_DTCM].size >> 2); i++)

			gmu_core_regread(device, GEN7_GMU_CM3_DTCM_START + i, data++);

	}



	return gmu_core->vma[GMU_DTCM].size + sizeof(*mem_hdr);

}



static size_t gen7_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 gen7_gmu_device *gmu = (struct gen7_gmu_device *)priv;



	if (!gmu->itcm_shadow) {

		dev_err(GMU_PDEV_DEV(device),

			"No memory allocated for ITCM shadow capture\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 gen7_gmu_snapshot_memories(struct kgsl_device *device,

	struct gen7_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->gmu_init_scratch) || (md == gmu->gpu_boot_scratch))

			desc.type = SNAPSHOT_GMU_MEM_WARMBOOT;

		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 0x14000



static size_t gen7_snapshot_rscc_registers(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv)

{

	const u32 *regs = priv;

	unsigned int *data = (unsigned int *)buf;

	int count = 0, k;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);



	/* Figure out how many registers we are going to dump */

	count = adreno_snapshot_regs_count(regs);



	if (remain < (count * 4)) {

		SNAPSHOT_ERR_NOMEM(device, "RSCC REGISTERS");

		return 0;

	}



	for (regs = priv; regs[0] != UINT_MAX; regs += 2) {

		unsigned int cnt = REG_COUNT(regs);



		if (cnt == 1) {

			*data++ = BIT(31) |  regs[0];

			*data++ =  __raw_readl(gmu->rscc_virt +

				((regs[0] - RSCC_OFFSET_DWORDS) << 2));

			continue;

		}

		*data++ = regs[0];

		*data++ = cnt;

		for (k = regs[0]; k <= regs[1]; k++)

			*data++ =  __raw_readl(gmu->rscc_virt +

				((k - RSCC_OFFSET_DWORDS) << 2));

	}



	/* Return the size of the section */

	return (count * 4);

}



/*

 * gen7_gmu_device_snapshot() - GEN7 GMU snapshot function

 * @device: Device being snapshotted

 * @snapshot: Pointer to the snapshot instance

 *

 * This is where all of the GEN7 GMU specific bits and pieces are grabbed

 * into the snapshot memory

 */

static void gen7_gmu_device_snapshot(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct gen7_gmu_device *gmu = to_gen7_gmu(adreno_dev);

	const struct adreno_gen7_core *gpucore = to_gen7_core(ADRENO_DEVICE(device));

	const struct gen7_snapshot_block_list *gen7_snapshot_block_list =

						gpucore->gen7_snapshot_block_list;

	const u32 *regs_ptr = (const u32 *)gen7_snapshot_block_list->gmu_ao_regs;



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GMU_MEMORY,

		snapshot, gen7_gmu_snapshot_itcm, gmu);



	adreno_snapshot_gmu_versions(device, snapshot);

