


static int adreno_setproperty(struct kgsl_device_private *dev_priv,

				unsigned int type,

				void __user *value,

				unsigned int sizebytes)

{

	int status = -EINVAL;

	struct kgsl_device *device = dev_priv->device;



	switch (type) {

	case KGSL_PROP_PWR_CONSTRAINT:

	case KGSL_PROP_L3_PWR_CONSTRAINT: {

			struct kgsl_device_constraint constraint;

			struct kgsl_context *context;



			if (sizebytes != sizeof(constraint))

				break;



			if (copy_from_user(&constraint, value,

				sizeof(constraint))) {

				status = -EFAULT;

				break;

			}



			context = kgsl_context_get_owner(dev_priv,

							constraint.context_id);



			if (context == NULL)

				break;



			status = adreno_set_constraint(device, context,

								&constraint);



			kgsl_context_put(context);

		}

		break;

	case KGSL_PROP_DCVS_PROFILE:

		status = adreno_set_dcvs_profile(dev_priv, value, sizebytes);

		break;

	default:

		status = adreno_default_setproperty(dev_priv, type, value, sizebytes);

		break;

	}



	return status;

}



/*

 * adreno_soft_reset -  Do a soft reset of the GPU hardware

 * @device: KGSL device to soft reset

 *

 * "soft reset" the GPU hardware - this is a fast path GPU reset

 * The GPU hardware is reset but we never pull power so we can skip

 * a lot of the standard adreno_stop/adreno_start sequence

 */

static int adreno_soft_reset(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	int ret;



	if (gpudev->clear_pending_transactions) {

		ret = gpudev->clear_pending_transactions(adreno_dev);

		if (ret)

			return ret;

	}



	kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);

	adreno_set_active_ctxs_null(adreno_dev);



	adreno_irqctrl(adreno_dev, 0);



	adreno_clear_gpu_fault(adreno_dev);

	/* since device is oficially off now clear start bit */

	clear_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);



	/* save physical performance counter values before GPU soft reset */

	adreno_perfcounter_save(adreno_dev);



	_soft_reset(adreno_dev);



	/* Clear the busy_data stats - we're starting over from scratch */

	adreno_dev->busy_data.gpu_busy = 0;

	adreno_dev->busy_data.bif_ram_cycles = 0;

	adreno_dev->busy_data.bif_ram_cycles_read_ch1 = 0;

	adreno_dev->busy_data.bif_ram_cycles_write_ch0 = 0;

	adreno_dev->busy_data.bif_ram_cycles_write_ch1 = 0;

	adreno_dev->busy_data.bif_starved_ram = 0;

	adreno_dev->busy_data.bif_starved_ram_ch1 = 0;



	/* Reinitialize the GPU */

	gpudev->start(adreno_dev);



	/* Re-initialize the coresight registers if applicable */

	adreno_coresight_start(adreno_dev);



	/* Enable IRQ */

	adreno_irqctrl(adreno_dev, 1);



	/* stop all ringbuffers to cancel RB events */

	adreno_ringbuffer_stop(adreno_dev);



	/* Start the ringbuffer(s) again */

	ret = gpudev->rb_start(adreno_dev);

	if (ret == 0) {

		device->reset_counter++;

		set_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);

	}



	/* Restore physical performance counter values after soft reset */

	adreno_perfcounter_restore(adreno_dev);



	if (ret)

		dev_err(device->dev, "Device soft reset failed: %d\n", ret);



	return ret;

}



bool adreno_isidle(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);

	struct adreno_ringbuffer *rb;

	int i;



	if (!kgsl_state_is_awake(KGSL_DEVICE(adreno_dev)))

		return true;



	/*

	 * wptr is updated when we add commands to ringbuffer, add a barrier

	 * to make sure updated wptr is compared to rptr

	 */

	smp_mb();



	/*

	 * ringbuffer is truly idle when all ringbuffers read and write

	 * pointers are equal

	 */



	FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {

		if (!adreno_rb_empty(rb))

			return false;

	}



	return gpudev->hw_isidle(adreno_dev);

}



/**

 * adreno_spin_idle() - Spin wait for the GPU to idle

 * @adreno_dev: Pointer to an adreno device

 * @timeout: milliseconds to wait before returning error

 *

 * Spin the CPU waiting for the RBBM status to return idle

 */

int adreno_spin_idle(struct adreno_device *adreno_dev, unsigned int timeout)

{

	unsigned long wait = jiffies + msecs_to_jiffies(timeout);



	do {

		/*

		 * If we fault, stop waiting and return an error. The dispatcher

		 * will clean up the fault from the work queue, but we need to

		 * make sure we don't block it by waiting for an idle that

		 * will never come.

		 */



		if (adreno_gpu_fault(adreno_dev) != 0)

			return -EDEADLK;



		if (adreno_isidle(adreno_dev))

			return 0;



	} while (time_before(jiffies, wait));



	/*

	 * Under rare conditions, preemption can cause the while loop to exit

	 * without checking if the gpu is idle. check one last time before we

	 * return failure.

	 */

	if (adreno_gpu_fault(adreno_dev) != 0)

		return -EDEADLK;



	if (adreno_isidle(adreno_dev))

		return 0;



	return -ETIMEDOUT;

}



/**

 * adreno_wait_idle() - wait for the GPU hardware to go idle

 * @device: Pointer to the KGSL device structure for the GPU

 *

 * Wait up to ADRENO_IDLE_TIMEOUT milliseconds for the GPU hardware to go quiet.

 * Caller must hold the device mutex, and must not hold the dispatcher mutex.

 */



int adreno_wait_idle(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	int ret;



	/*

	 * Make sure the device mutex is held so the dispatcher can't send any

	 * more commands to the hardware

	 */



	if (WARN_ON(!kgsl_mutex_is_locked(&device->mutex)))

		return -EDEADLK;



	/* Check if we are already idle before idling dispatcher */

	if (adreno_isidle(adreno_dev))

		return 0;

	/*

	 * Wait for dispatcher to finish completing commands

	 * already submitted

	 */

	ret = adreno_dispatcher_idle(adreno_dev);

	if (ret)

		return ret;



	return adreno_spin_idle(adreno_dev, ADRENO_IDLE_TIMEOUT);

}



int adreno_drain(struct kgsl_device *device, unsigned long wait_jiffies)

{

	int ret;



	/* Halt any new submissions */

	reinit_completion(&device->halt_gate);



	/**

	 * Wait for the dispatcher to retire everything by waiting

	 * for the active count to go to zero.

	 */

	ret = kgsl_active_count_wait(device, 0, wait_jiffies);

	if (ret)

		dev_err(device->dev, "Timed out waiting for the active count\n");



	return ret;

}



/* Caller must hold the device mutex. */

void adreno_check_idle(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	/* process any profiling results that are available */

	adreno_profile_process_results(adreno_dev);



	/*

	 * Make sure the device mutex is held so the dispatcher can't send any

	 * more commands to the hardware

	 */

	if (WARN_ON(!kgsl_mutex_is_locked(&device->mutex)))

		return;



	/* Make sure dispatcher is idle */

	if (!completion_done(&adreno_dev->dispatcher.idle_gate))

		dev_err_ratelimited(device->dev, "Dispatcher not idle before SLUMBER\n");



	/* Make sure the device is idle */

	if (!adreno_isidle(adreno_dev))

		dev_err_ratelimited(device->dev, "Device not idle before SLUMBER\n");

}



bool adreno_gx_is_on(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	return gpudev->gx_is_on(adreno_dev);

}



void adreno_profile_submit_time(struct adreno_submit_time *time)

{

	struct kgsl_drawobj *drawobj;

	struct kgsl_drawobj_cmd *cmdobj;

	struct kgsl_mem_entry *entry;

	struct kgsl_drawobj_profiling_buffer *profile_buffer;



	if (!time)

		return;



	drawobj = time->drawobj;

	if (drawobj == NULL)

		return;



	cmdobj = CMDOBJ(drawobj);

	entry = cmdobj->profiling_buf_entry;

	if (!entry)

		return;



	profile_buffer = kgsl_gpuaddr_to_vaddr(&entry->memdesc,

			cmdobj->profiling_buffer_gpuaddr);



	if (profile_buffer == NULL)

		return;



	/* Return kernel clock time to the client if requested */

	if (drawobj->flags & KGSL_DRAWOBJ_PROFILING_KTIME) {

		u64 secs = time->ktime;



		profile_buffer->wall_clock_ns =

			do_div(secs, NSEC_PER_SEC);

		profile_buffer->wall_clock_s = secs;

	} else {

		profile_buffer->wall_clock_s = time->utime.tv_sec;

		profile_buffer->wall_clock_ns = time->utime.tv_nsec;

	}



	profile_buffer->gpu_ticks_queued = time->ticks;



	kgsl_memdesc_unmap(&entry->memdesc);

}



/**

 * adreno_waittimestamp - sleep while waiting for the specified timestamp

 * @device - pointer to a KGSL device structure

 * @context - pointer to the active kgsl context

 * @timestamp - GPU timestamp to wait for

 * @msecs - amount of time to wait (in milliseconds)

 *

 * Wait up to 'msecs' milliseconds for the specified timestamp to expire.

 */

static int adreno_waittimestamp(struct kgsl_device *device,

		struct kgsl_context *context,

		unsigned int timestamp,

		unsigned int msecs)

{

	int ret;



	if (context == NULL) {

		/* If they are doing then complain once */

		dev_WARN_ONCE(device->dev, 1,

			"IOCTL_KGSL_DEVICE_WAITTIMESTAMP is deprecated\n");

		return -ENOTTY;

	}



	/* Return -ENOENT if the context has been detached */

	if (kgsl_context_detached(context))

		return -ENOENT;



	ret = adreno_drawctxt_wait(ADRENO_DEVICE(device), context,

		timestamp, msecs);



	/* If the context got invalidated then return a specific error */

	if (kgsl_context_invalid(context))

		ret = -EDEADLK;



	/*

	 * Return -EPROTO if the device has faulted since the last time we

	 * checked.  Userspace uses this as a marker for performing post

	 * fault activities

	 */



	if (!ret && test_and_clear_bit(ADRENO_CONTEXT_FAULT, &context->priv))

		ret = -EPROTO;



	return ret;

}



/**

 * __adreno_readtimestamp() - Reads the timestamp from memstore memory

 * @adreno_dev: Pointer to an adreno device

 * @index: Index into the memstore memory

 * @type: Type of timestamp to read

 * @timestamp: The out parameter where the timestamp is read

 */

static int __adreno_readtimestamp(struct adreno_device *adreno_dev, int index,

				int type, unsigned int *timestamp)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int status = 0;



	switch (type) {

	case KGSL_TIMESTAMP_CONSUMED:

		kgsl_sharedmem_readl(device->memstore, timestamp,

			KGSL_MEMSTORE_OFFSET(index, soptimestamp));

		break;

	case KGSL_TIMESTAMP_RETIRED:

		kgsl_sharedmem_readl(device->memstore, timestamp,

			KGSL_MEMSTORE_OFFSET(index, eoptimestamp));

		break;

	default:

		status = -EINVAL;

		*timestamp = 0;

		break;

	}

	return status;

}



/**

 * adreno_rb_readtimestamp(): Return the value of given type of timestamp

 * for a RB

 * @adreno_dev: adreno device whose timestamp values are being queried

 * @priv: The object being queried for a timestamp (expected to be a rb pointer)

 * @type: The type of timestamp (one of 3) to be read

 * @timestamp: Pointer to where the read timestamp is to be written to

 *

 * CONSUMED and RETIRED type timestamps are sorted by id and are constantly

 * updated by the GPU through shared memstore memory. QUEUED type timestamps

 * are read directly from context struct.



 * The function returns 0 on success and timestamp value at the *timestamp

 * address and returns -EINVAL on any read error/invalid type and timestamp = 0.

 */

int adreno_rb_readtimestamp(struct adreno_device *adreno_dev,

		void *priv, enum kgsl_timestamp_type type,

		unsigned int *timestamp)

{

	int status = 0;

	struct adreno_ringbuffer *rb = priv;



	if (type == KGSL_TIMESTAMP_QUEUED)

		*timestamp = rb->timestamp;

	else

		status = __adreno_readtimestamp(adreno_dev,

				rb->id + KGSL_MEMSTORE_MAX,

				type, timestamp);



	return status;

}



/**

 * adreno_readtimestamp(): Return the value of given type of timestamp

 * @device: GPU device whose timestamp values are being queried

 * @priv: The object being queried for a timestamp (expected to be a context)

 * @type: The type of timestamp (one of 3) to be read

 * @timestamp: Pointer to where the read timestamp is to be written to

 *

 * CONSUMED and RETIRED type timestamps are sorted by id and are constantly

 * updated by the GPU through shared memstore memory. QUEUED type timestamps

 * are read directly from context struct.



 * The function returns 0 on success and timestamp value at the *timestamp

 * address and returns -EINVAL on any read error/invalid type and timestamp = 0.

 */

static int adreno_readtimestamp(struct kgsl_device *device,

		void *priv, enum kgsl_timestamp_type type,

		unsigned int *timestamp)

{

	int status = 0;

	struct kgsl_context *context = priv;



	if (type == KGSL_TIMESTAMP_QUEUED) {

		struct adreno_context *ctxt = ADRENO_CONTEXT(context);



		*timestamp = ctxt->timestamp;

	} else

		status = __adreno_readtimestamp(ADRENO_DEVICE(device),

				context->id, type, timestamp);



	return status;

}



/**

 * adreno_device_private_create(): Allocate an adreno_device_private structure

 */

static struct kgsl_device_private *adreno_device_private_create(void)

{

	struct adreno_device_private *adreno_priv =

			kzalloc(sizeof(*adreno_priv), GFP_KERNEL);



	if (adreno_priv) {

		INIT_LIST_HEAD(&adreno_priv->perfcounter_list);

		return &adreno_priv->dev_priv;

	}

	return NULL;

}



/**

 * adreno_device_private_destroy(): Destroy an adreno_device_private structure

 * and release the perfcounters held by the kgsl fd.

 * @dev_priv: The kgsl device private structure

 */

static void adreno_device_private_destroy(struct kgsl_device_private *dev_priv)

{

	struct kgsl_device *device = dev_priv->device;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct adreno_device_private *adreno_priv =

		container_of(dev_priv, struct adreno_device_private,

		dev_priv);

	struct adreno_perfcounter_list_node *p, *tmp;



	kgsl_mutex_lock(&device->mutex);

	list_for_each_entry_safe(p, tmp, &adreno_priv->perfcounter_list, node) {

		adreno_perfcounter_put(adreno_dev, p->groupid,

					p->countable, PERFCOUNTER_FLAG_NONE);

		list_del(&p->node);

		kfree(p);

	}

	kgsl_mutex_unlock(&device->mutex);



	kfree(adreno_priv);

}



/**

 * adreno_power_stats() - Reads the counters needed for freq decisions

 * @device: Pointer to device whose counters are read

 * @stats: Pointer to stats set that needs updating

 * Power: The caller is expected to be in a clock enabled state as this

 * function does reg reads

 */

static void adreno_power_stats(struct kgsl_device *device,

				struct kgsl_power_stats *stats)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	return gpudev->power_stats(adreno_dev, stats);

}



static int adreno_regulator_enable(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->regulator_enable)

		return gpudev->regulator_enable(adreno_dev);



	return 0;

}



static bool adreno_is_hw_collapsible(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (!gpudev->is_hw_collapsible(adreno_dev))

		return false;



	if (gpudev->clear_pending_transactions(adreno_dev))

		return false;



	return true;

}



static void adreno_regulator_disable(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->regulator_disable)

		gpudev->regulator_disable(adreno_dev);

}



static void adreno_pwrlevel_change_settings(struct kgsl_device *device,

		unsigned int prelevel, unsigned int postlevel, bool post)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->pwrlevel_change_settings)

		gpudev->pwrlevel_change_settings(adreno_dev, prelevel,

					postlevel, post);

}



static bool adreno_is_hwcg_on(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	return adreno_dev->hwcg_enabled;

}



static int adreno_queue_cmds(struct kgsl_device_private *dev_priv,

	struct kgsl_context *context, struct kgsl_drawobj *drawobj[],

	u32 count, u32 *timestamp)

{

	struct kgsl_device *device = dev_priv->device;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	if (WARN_ON(!adreno_dev->dispatch_ops || !adreno_dev->dispatch_ops->queue_cmds))

		return -ENODEV;



	return adreno_dev->dispatch_ops->queue_cmds(dev_priv, context, drawobj,

		count, timestamp);

}



static inline bool _verify_ib(struct kgsl_device_private *dev_priv,

		struct kgsl_context *context, struct kgsl_memobj_node *ib)

{

	struct kgsl_device *device = dev_priv->device;

	struct kgsl_process_private *private = dev_priv->process_priv;



	/* The maximum allowable size for an IB in the CP is 0xFFFFF dwords */

	if (ib->size == 0 || ((ib->size >> 2) > 0xFFFFF)) {

		pr_context(device, context, "ctxt %u invalid ib size %lld\n",

			context->id, ib->size);

		return false;

	}



	/* Make sure that the address is in range and dword aligned */

	if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, ib->gpuaddr,

		ib->size) || !IS_ALIGNED(ib->gpuaddr, 4)) {

		pr_context(device, context, "ctxt %u invalid ib gpuaddr %llX\n",

			context->id, ib->gpuaddr);

		return false;

	}



	return true;

}



int adreno_verify_cmdobj(struct kgsl_device_private *dev_priv,

		struct kgsl_context *context, struct kgsl_drawobj *drawobj[],

		uint32_t count)

{

	struct kgsl_device *device = dev_priv->device;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct kgsl_memobj_node *ib;

	unsigned int i;



	for (i = 0; i < count; i++) {

		/* Verify the IBs before they get queued */

		if (drawobj[i]->type == CMDOBJ_TYPE) {

			struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj[i]);



			/* Only allow mALU submissions if hardware supports mALU */

			if ((drawobj[i]->flags & KGSL_DRAWOBJ_USES_MALU) &&

				!adreno_dev->malu_enabled) {

				dev_err_once(device->dev, "mALU workload isn't supported\n");

				return -EINVAL;

			}



			list_for_each_entry(ib, &cmdobj->cmdlist, node)

				if (!_verify_ib(dev_priv,

					&ADRENO_CONTEXT(context)->base, ib))

					return -EINVAL;



			/*

			 * Clear the wake on touch bit to indicate an IB has

			 * been submitted since the last time we set it.

			 * But only clear it when we have rendering commands.

			 */

			device->pwrctrl.wake_on_touch = false;

		}

	}



	return 0;

}



static int adreno_queue_recurring_cmd(struct kgsl_device_private *dev_priv,

	struct kgsl_context *context, struct kgsl_drawobj *drawobj)

{

	struct kgsl_device *device = dev_priv->device;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);

	struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);

	int ret;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_LSR))

		return -EOPNOTSUPP;



	if (!gpudev->send_recurring_cmdobj)

		return -ENODEV;



	ret = adreno_verify_cmdobj(dev_priv, context, &drawobj, 1);

	if (ret)

		return ret;



	kgsl_mutex_lock(&device->mutex);



	/* Only one recurring command allowed */

	if (hwsched->recurring_cmdobj) {

		kgsl_mutex_unlock(&device->mutex);

		return -EINVAL;

	}



	ret = kgsl_check_context_state(context);

	if (ret) {

		kgsl_mutex_unlock(&device->mutex);

		return ret;

	}



	set_bit(CMDOBJ_RECURRING_START, &cmdobj->priv);



	ret = gpudev->send_recurring_cmdobj(adreno_dev, cmdobj);

	kgsl_mutex_unlock(&device->mutex);



	if (!ret)

		srcu_notifier_call_chain(&device->nh, GPU_GMU_READY, NULL);



	return ret;

}



static int adreno_dequeue_recurring_cmd(struct kgsl_device *device,

	struct kgsl_context *context)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);

	struct kgsl_drawobj *recurring_drawobj;

	int ret;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_LSR))

		return -EOPNOTSUPP;



	if (!gpudev->send_recurring_cmdobj)

		return -ENODEV;



	kgsl_mutex_lock(&device->mutex);



	/* We can safely return here as recurring wokload is already untracked */

	if (hwsched->recurring_cmdobj == NULL) {

		kgsl_mutex_unlock(&device->mutex);

		return -EINVAL;

	}



	recurring_drawobj = DRAWOBJ(hwsched->recurring_cmdobj);



	/* Check if the recurring command is for same context or not*/

	if (recurring_drawobj->context != context) {

		kgsl_mutex_unlock(&device->mutex);

		return -EINVAL;

	}



	ret = kgsl_check_context_state(context);

	if (ret) {

		kgsl_mutex_unlock(&device->mutex);

		return ret;

	}



	clear_bit(CMDOBJ_RECURRING_START, &hwsched->recurring_cmdobj->priv);

	set_bit(CMDOBJ_RECURRING_STOP, &hwsched->recurring_cmdobj->priv);



	ret = gpudev->send_recurring_cmdobj(adreno_dev, hwsched->recurring_cmdobj);



	kgsl_mutex_unlock(&device->mutex);



	if (!ret)

		srcu_notifier_call_chain(&device->nh, GPU_GMU_STOP, NULL);



	return ret;

}



static void adreno_set_isdb_breakpoint_registers(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->set_isdb_breakpoint_registers)

		gpudev->set_isdb_breakpoint_registers(adreno_dev);

}



static void adreno_drawctxt_sched(struct kgsl_device *device,

		struct kgsl_context *context)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	if (WARN_ON(!adreno_dev->dispatch_ops || !adreno_dev->dispatch_ops->queue_context))

		return;



	adreno_dev->dispatch_ops->queue_context(adreno_dev,

		ADRENO_CONTEXT(context));

}



bool adreno_smmu_is_stalled(struct adreno_device *adreno_dev)

{

	u32 fault, val;



	/*

	 * RBBM_STATUS3:SMMU_STALLED_ON_FAULT (BIT 24) to tells if GPU

	 * encountered a pagefault. Gen8 page fault status checked from

	 * the software condition as RBBM_STATUS3 is not available.

	 */

	if (ADRENO_GPUREV(adreno_dev) < ADRENO_REV_GEN8_0_0) {

		adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS3, &val);

		return (val & BIT(24));

	}



	fault = adreno_gpu_fault(adreno_dev);



	return (fault & ADRENO_IOMMU_STALL_ON_PAGE_FAULT) ? true : false;

}



int adreno_power_cycle(struct adreno_device *adreno_dev,

	void (*callback)(struct adreno_device *adreno_dev, void *priv),

	void *priv)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_power_ops *ops = ADRENO_POWER_OPS(adreno_dev);

	int ret;



	kgsl_mutex_lock(&device->mutex);

	ret = ops->pm_suspend(adreno_dev);



	if (!ret) {

		callback(adreno_dev, priv);

		gmu_core_mark_for_coldboot(device);

		ops->pm_resume(adreno_dev);

	}



	kgsl_mutex_unlock(&device->mutex);



	return ret;

}



struct cycle_data {

	void *ptr;

	void *val;

};



static void cycle_set_bool(struct adreno_device *adreno_dev, void *priv)

{

	struct cycle_data *data = priv;



	*((bool *) data->ptr) = *((bool *) data->val);

}



int adreno_power_cycle_bool(struct adreno_device *adreno_dev,

	bool *flag, bool val)

{

	struct cycle_data data = { .ptr = flag, .val = &val };



	return adreno_power_cycle(adreno_dev, cycle_set_bool, &data);

}



static void cycle_set_u32(struct adreno_device *adreno_dev, void *priv)

{

	struct cycle_data *data = priv;



	*((u32 *) data->ptr) = *((u32 *) data->val);

}



int adreno_power_cycle_u32(struct adreno_device *adreno_dev,

	u32 *flag, u32 val)

{

	struct cycle_data data = { .ptr = flag, .val = &val };



	return adreno_power_cycle(adreno_dev, cycle_set_u32, &data);

}



static int adreno_set_opp(struct kgsl_device *device, struct kgsl_pwrlevel *level)

{

	struct device *dev = &device->pdev->dev;

	int ret;



	/*

	 * level->opp is initialized with a valid OPP pointer only when the driver

	 * is probed with standard DT bindings. Use this to distinguish standard

	 * vs non-standard kernels here.

	 *

	 * On non-standard kernels (level->opp == NULL), vote the core clock using

	 * clk_set_rate API. The downstream clock driver internally handles the

	 * required regulator voting.

	 *

	 * On standard kernels (level->opp != NULL), use dev_pm_opp_set_opp() API

	 * and OPP framework will take care of all required votes.

	 */

	if (!level->opp) {

		struct kgsl_pwrctrl *pwr = &device->pwrctrl;



		ret = clk_set_rate(pwr->grp_clks[0], level->gpu_freq);

		if (ret)

			dev_err(device->dev, "GPU clk freq set failure: %d\n", ret);



		return ret;

	}



	ret = dev_pm_opp_set_opp(dev, level->opp);

	if (ret)

		dev_err(device->dev, "GPU OPP configure failure: %d\n", ret);



	return ret;

}



static int adreno_gpu_clock_set(struct kgsl_device *device, u32 pwrlevel)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_power_ops *ops = ADRENO_POWER_OPS(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	struct kgsl_pwrlevel *level = &pwr->pwrlevels[pwrlevel];

	u32 prev_pwrlevel = pwr->previous_pwrlevel;

	int ret;



	if (ops->gpu_clock_set) {

		ret = ops->gpu_clock_set(adreno_dev, pwrlevel);

	} else

		ret = adreno_set_opp(device, level);





	if (ret)

		return ret;



	trace_kgsl_pwrlevel(device, pwrlevel, level->gpu_freq,

		prev_pwrlevel, pwr->pwrlevels[prev_pwrlevel].gpu_freq, 0);



	KGSL_TRACE_GPU_FREQ(level->gpu_freq/1000, 0, 0);

	return 0;

}



static int adreno_interconnect_bus_set(struct adreno_device *adreno_dev,

	int level, u32 ab)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;



	if ((level == pwr->cur_buslevel) && (ab == pwr->cur_ab))

		return 0;



	kgsl_icc_set_tag(pwr, level);

	pwr->cur_buslevel = level;

	pwr->cur_ab = ab;



	icc_set_bw(pwr->icc_path, MBps_to_icc(ab),

		kBps_to_icc(pwr->ddr_table[level]));



	trace_kgsl_buslevel(device, pwr->active_pwrlevel, level, ab, 0);



	return 0;

}



static int adreno_gpu_bus_set(struct kgsl_device *device, int level, u32 ab)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_power_ops *ops = ADRENO_POWER_OPS(adreno_dev);



	if (ops->gpu_bus_set)

		return ops->gpu_bus_set(adreno_dev, level, ab);



	return adreno_interconnect_bus_set(adreno_dev, level, ab);

}



u32 adreno_gmu_bus_ab_quantize(struct adreno_device *adreno_dev, u32 ab)

{

	u16 vote = 0;

	u32 max_bw, max_ab;

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;



	if (!adreno_dev->gmu_ab || (ab == INVALID_AB_VALUE))

		return (FIELD_PREP(GENMASK(31, 16), INVALID_AB_VALUE));



	/*

	 * max ddr bandwidth (kbps) = (Max bw in kbps per channel * number of channel)

	 * max ab (Mbps) = max ddr bandwidth (kbps) / 1000

	 */

	max_bw = pwr->ddr_table[pwr->ddr_table_count - 1] * adreno_dev->gpucore->num_ddr_channels;

	max_ab = max_bw / 1000;



	/*

	 * If requested AB is higher than theoretical max bandwidth, set AB vote as max

	 * allowable quantized AB value.

	 *

	 * Power FW supports a 16 bit AB BW level. We can quantize the entire vote-able BW

	 * range to a 16 bit space and the quantized value can be used to vote for AB though

	 * GMU. Quantization can be performed as below.

	 *

	 * quantized_vote = (ab vote (kbps) * 2^16) / max ddr bandwidth (kbps)

	 */

	if (ab >= max_ab)

		vote = MAX_AB_VALUE;

	else

		vote = (u16)(((u64)ab * 1000 * (1 << 16)) / max_bw);



	/*

	 * Vote will be calculated as 0 for smaller AB values.

	 * Set a minimum non-zero vote in such cases.

	 */

	if (ab && !vote)

		vote = 0x1;



	/*

	 * Set ab enable mask and valid AB vote. req.bw is 32 bit value 0xABABENIB

	 * and with this return we want to set the upper 16 bits and EN field specifies

	 * if the AB vote is valid or not.

	 */

	return (FIELD_PREP(GENMASK(31, 16), vote) | FIELD_PREP(GENMASK(15, 8), 1));

}



static void adreno_deassert_gbif_halt(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->deassert_gbif_halt)

		gpudev->deassert_gbif_halt(adreno_dev);

}



static void adreno_create_hw_fence(struct kgsl_device *device, struct kgsl_sync_fence *kfence)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	if (WARN_ON(!adreno_dev->dispatch_ops))

		return;



	if (adreno_dev->dispatch_ops->create_hw_fence)

		adreno_dev->dispatch_ops->create_hw_fence(adreno_dev, kfence);

}



u64 adreno_read_cx_timer(struct adreno_device *adreno_dev)

{

	/* Check if the CX timer is initialized */

	if (!test_bit(ADRENO_DEVICE_CX_TIMER_INITIALIZED, &adreno_dev->priv))

		return 0;



	/* Since the GPU CX and CPU timers are synchronized return the CPU timer */

	return arch_timer_read_counter();

}



static void add_proc_fault_list(struct adreno_device *adreno_dev,

			struct kgsl_process_private *proc_priv)

{

	int cur_idx = -1;

	int new_idx = -1;

	int i;



	/*

	 * Add the process to array of faulting processes. This array is sorted (processes with

	 * more faults are higher ranked).

	 */

	write_lock(&adreno_dev->fault_stats_lock);

	proc_priv->fault_count++;



	for (i = ARRAY_SIZE(adreno_dev->fault_procs) - 1; i >= 0; i--) {

		struct adreno_fault_proc *proc = &adreno_dev->fault_procs[i];



		/* Check whether this process is already tracked in the array */

		if (!strcmp(proc->comm, proc_priv->comm)) {

			proc_priv->fault_count = ++proc->fault_count;

			cur_idx = i;

			break;

		}



		/* Find left most entry with lower fault count than current process */

		if (proc->fault_count < proc_priv->fault_count)

			new_idx = i;

	}



	/*

	 * If the process is not currently tracked, we can place it at the left most index that has

	 * a fault count less than its fault count. If such an index does not exist, the array is

	 * already full.

	 */

	if (cur_idx < 0) {

		if (new_idx >= 0)

			goto write_new_idx;

		goto unlock;

	}



	/* Find the left most entry (if any) with a lower fault_count */

	for (new_idx = 0; new_idx < cur_idx; new_idx++) {

		if (adreno_dev->fault_procs[new_idx].fault_count < proc_priv->fault_count)

			break;

	}



	/* Check if the current process is already at its correct position */

	if (new_idx == cur_idx)

		goto unlock;



	/* Swap the two entries */

	adreno_dev->fault_procs[cur_idx].fault_count =

			adreno_dev->fault_procs[new_idx].fault_count;

	strscpy(adreno_dev->fault_procs[cur_idx].comm, adreno_dev->fault_procs[new_idx].comm,

			sizeof(adreno_dev->fault_procs[cur_idx].comm));



write_new_idx:

	adreno_dev->fault_procs[new_idx].fault_count = proc_priv->fault_count;

	strscpy(adreno_dev->fault_procs[new_idx].comm, proc_priv->comm,

		sizeof(adreno_dev->fault_procs[new_idx].comm));



unlock:

	write_unlock(&adreno_dev->fault_stats_lock);

}



void adreno_gpufault_stats(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj, struct kgsl_drawobj *drawobj_lpac, int fault)

{

	int i;

	struct kgsl_process_private *proc_priv = NULL, *proc_priv_lpac;



	write_lock(&adreno_dev->fault_stats_lock);

	for (i = 0; i < ARRAY_SIZE(adreno_dev->fault_counts); i++)

		if (fault & BIT(i))

			adreno_dev->fault_counts[i]++;

	write_unlock(&adreno_dev->fault_stats_lock);



	if (drawobj) {

		proc_priv = drawobj->context->proc_priv;

		add_proc_fault_list(adreno_dev, proc_priv);

	}

	if (drawobj_lpac) {

		proc_priv_lpac = drawobj_lpac->context->proc_priv;

		if (proc_priv_lpac != proc_priv)

			add_proc_fault_list(adreno_dev, proc_priv_lpac);

	}

}



static void adreno_set_thermal_index(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_power_ops *ops = ADRENO_POWER_OPS(adreno_dev);



	if (ops->set_thermal_index)

		ops->set_thermal_index(adreno_dev);

}



static int adreno_set_smmu_aperture(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpu_core *gpucore = adreno_dev->gpucore;

	int i;



	if (!gpucore->smmu_aperture_count || !device->smmu_aperture_virt)

		return -ENODEV;



	for (i = 0; i < gpucore->smmu_aperture_count; i++)

		writel_relaxed(gpucore->smmu_aperture[i].val, device->smmu_aperture_virt +

			(gpucore->smmu_aperture[i].offset << 2));



	/* Make sure all of the preceding writes have posted */

	wmb();



	return 0;

}



static bool adreno_is_first_boot_done(struct kgsl_device *device)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	return test_bit(ADRENO_DEVICE_FIRST_BOOT_DONE, &adreno_dev->priv);

}



static const struct kgsl_functable adreno_functable = {

	/* Mandatory functions */

	.check_idle = adreno_check_idle,

	.first_open = adreno_first_open,

	.start = adreno_start,

	.stop = adreno_stop,

	.last_close = adreno_last_close,

	.getproperty = adreno_getproperty,

	.getproperty_compat = adreno_getproperty_compat,

	.waittimestamp = adreno_waittimestamp,

	.readtimestamp = adreno_readtimestamp,

	.queue_cmds = adreno_queue_cmds,

	.ioctl = adreno_ioctl,

	.compat_ioctl = adreno_compat_ioctl,

	.power_stats = adreno_power_stats,

	.snapshot = adreno_snapshot,

	.drain_and_idle = adreno_drain_and_idle,

	.device_private_create = adreno_device_private_create,

	.device_private_destroy = adreno_device_private_destroy,

	/* Optional functions */

	.drawctxt_create = adreno_drawctxt_create,

	.drawctxt_detach = adreno_drawctxt_detach,

	.drawctxt_destroy = adreno_drawctxt_destroy,

	.drawctxt_dump = adreno_drawctxt_dump,

	.setproperty = adreno_setproperty,

	.setproperty_compat = adreno_setproperty_compat,

	.drawctxt_sched = adreno_drawctxt_sched,

	.resume = adreno_dispatcher_start,

	.regulator_enable = adreno_regulator_enable,

	.is_hw_collapsible = adreno_is_hw_collapsible,

	.regulator_disable = adreno_regulator_disable,

	.pwrlevel_change_settings = adreno_pwrlevel_change_settings,

	.query_property_list = adreno_query_property_list,

	.is_hwcg_on = adreno_is_hwcg_on,

	.gpu_clock_set = adreno_gpu_clock_set,

	.gpu_bus_set = adreno_gpu_bus_set,

	.deassert_gbif_halt = adreno_deassert_gbif_halt,

	.queue_recurring_cmd = adreno_queue_recurring_cmd,

	.dequeue_recurring_cmd = adreno_dequeue_recurring_cmd,

	.set_isdb_breakpoint_registers = adreno_set_isdb_breakpoint_registers,

	.create_hw_fence = adreno_create_hw_fence,

	.gmu_based_dcvs_pwr_ops = adreno_gmu_based_dcvs_pwr_ops,

	.set_thermal_index = adreno_set_thermal_index,

	.alloc_dcvs_profile_memory = adreno_alloc_dcvs_profile_memory,

	.is_first_boot_done = adreno_is_first_boot_done,

	.set_smmu_aperture = adreno_set_smmu_aperture,

};



static const struct component_master_ops adreno_ops = {

	.bind = adreno_bind,

	.unbind = adreno_unbind,

};



const struct adreno_power_ops adreno_power_operations = {

	.first_open = adreno_open,

	.last_close = adreno_close,

	.active_count_get = adreno_pwrctrl_active_count_get,

	.pm_suspend = adreno_suspend,

	.pm_resume = adreno_resume,

	.touch_wakeup = adreno_touch_wakeup,

};



static int _compare_of(struct device *dev, void *data)

{

	return (dev->of_node == data);

}



static void _release_of(struct device *dev, void *data)

{

	of_node_put(data);

}



static void adreno_add_components(struct device *dev,

		struct component_match **match, const struct of_device_id *matches)

{

	struct device_node *node;



	/*

	 * Add kgsl-smmu, context banks and gmu as components, if supported.

	 * Master bind (adreno_bind) will be called only once all added

	 * components are available.

	 */

	for_each_matching_node(node, matches) {

		if (!of_device_is_available(node))

			continue;



		component_match_add_release(dev, match, _release_of, _compare_of, node);

	}

}



static int adreno_probe(struct platform_device *pdev)

{

	struct component_match *match = NULL;

	const struct of_device_id *matches = adreno_component_match_legacy;



	/*

	 * Let us say there are two devices. One with "qcom,adreno" compatible

	 * and one with "qcom,kgsl-3d0" or "qcom,kgsl" compatible. In this case

	 * probe only the device with legacy compatible strings and return

	 * error for the device with "qcom,adreno". Also choose the match table

	 * accordingly.

	 */

	if (of_device_is_compatible(pdev->dev.of_node, "qcom,adreno")) {

		if (kgsl_is_compatible_node_available("qcom,kgsl") ||

				kgsl_is_compatible_node_available("qcom,kgsl-3d0"))

			return -ENODEV;

		matches = adreno_component_match;

	}



	/*

	 * With standard DT bindings, there is no component to match

	 * from adreno_component_match list for no GMU targets. Invoke

	 * adreno_bind directly in this case.

	 */

	if (is_gmu_wrapper_available())

		return adreno_bind(&pdev->dev);



	adreno_add_components(&pdev->dev, &match, matches);



	if (!match)

		return -ENODEV;



	return component_master_add_with_match(&pdev->dev,

			&adreno_ops, match);

}



#if (KERNEL_VERSION(6, 10, 0) <= LINUX_VERSION_CODE)

static void adreno_remove(struct platform_device *pdev)

{

	if (is_gmu_wrapper_available())

		adreno_unbind(&pdev->dev);

	else

		component_master_del(&pdev->dev, &adreno_ops);

}

#else

static int adreno_remove(struct platform_device *pdev)

{

	if (is_gmu_wrapper_available())

		adreno_unbind(&pdev->dev);

	else

		component_master_del(&pdev->dev, &adreno_ops);



	return 0;

}

#endif



#if IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION)

#if IS_ENABLED(CONFIG_QCOM_SECURE_BUFFER)

/*

 * Issue hyp_assign call to assign non-used internal/userspace secure

 * buffers to kernel.

 */

static int adreno_secure_pt_hibernate(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_process_private *process;

	struct kgsl_mem_entry *entry;

	struct kgsl_global_memdesc *md;

	struct kgsl_memdesc *memdesc;

	int ret, id;



	read_lock(&kgsl_driver.proclist_lock);

	list_for_each_entry(process, &kgsl_driver.process_list, list) {

		idr_for_each_entry(&process->mem_idr, entry, id) {

			memdesc = &entry->memdesc;

			if (!kgsl_memdesc_is_secured(memdesc) ||

				(memdesc->flags & KGSL_MEMFLAGS_USERMEM_ION) ||

				(TEST_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv)))

				continue;



			read_unlock(&kgsl_driver.proclist_lock);



			if (kgsl_unlock_sgt(memdesc->sgt))

				dev_err(device->dev, "kgsl_unlock_sgt failed\n");



			SET_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv);



			read_lock(&kgsl_driver.proclist_lock);

		}

	}

	read_unlock(&kgsl_driver.proclist_lock);



	list_for_each_entry(md, &device->globals, node) {

		memdesc = &md->memdesc;

		if (kgsl_memdesc_is_secured(memdesc) &&

			!(TEST_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv))) {

			ret = kgsl_unlock_sgt(memdesc->sgt);

			if (ret) {

				dev_err(device->dev, "kgsl_unlock_sgt failed ret %d\n", ret);

				goto fail;

			}

			SET_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv);

		}

	}



	return 0;



fail:

	list_for_each_entry(md, &device->globals, node) {

		memdesc = &md->memdesc;

		if (kgsl_memdesc_is_secured(memdesc) &&

			(TEST_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv))) {

			kgsl_lock_sgt(memdesc->sgt, memdesc->size);

			CLEAR_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv);

		}

	}



	return -EBUSY;

}



static int adreno_secure_pt_restore(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_process_private *process;

	struct kgsl_mem_entry *entry;

	struct kgsl_memdesc *memdesc;

	struct kgsl_global_memdesc *md;

	int ret, id;



	list_for_each_entry(md, &device->globals, node) {

		memdesc = &md->memdesc;

		if (kgsl_memdesc_is_secured(memdesc) &&

			(TEST_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv))) {

			ret = kgsl_lock_sgt(memdesc->sgt, memdesc->size);

			if (ret) {

				dev_err(device->dev, "kgsl_lock_sgt failed ret %d\n", ret);

				return ret;

			}

			CLEAR_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv);

		}

	}



	read_lock(&kgsl_driver.proclist_lock);

	list_for_each_entry(process, &kgsl_driver.process_list, list) {

		idr_for_each_entry(&process->mem_idr, entry, id) {

			memdesc = &entry->memdesc;

			if (!kgsl_memdesc_is_secured(memdesc) ||

				(memdesc->flags & KGSL_MEMFLAGS_USERMEM_ION) ||

				!(TEST_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv)))

				continue;



			read_unlock(&kgsl_driver.proclist_lock);



			ret = kgsl_lock_sgt(memdesc->sgt, memdesc->size);

			if (ret) {

				dev_err(device->dev, "kgsl_lock_sgt failed ret %d\n", ret);

				return ret;

			}

			CLEAR_FLAG(KGSL_MEMDESC_HYPASSIGNED_HLOS, &memdesc->priv);



			read_lock(&kgsl_driver.proclist_lock);

		}

	}

	read_unlock(&kgsl_driver.proclist_lock);



	return 0;

}

#else

static int adreno_secure_pt_hibernate(struct adreno_device *adreno_dev)

{

	return 0;

}



static int adreno_secure_pt_restore(struct adreno_device *adreno_dev)

{

	return 0;

}

#endif /* IS_ENABLED(CONFIG_QCOM_SECURE_BUFFER) */



static int adreno_hibernation_suspend(struct device *dev)

{

	struct kgsl_device *device = dev_get_drvdata(dev);

	struct adreno_device *adreno_dev;

	const struct adreno_power_ops *ops;

	int status;



	if (!device)

		return 0;



	adreno_dev = ADRENO_DEVICE(device);

	ops = ADRENO_POWER_OPS(adreno_dev);



	kgsl_mutex_lock(&device->mutex);



	status = ops->pm_suspend(adreno_dev);

	if (status)

		goto err;



	/*

	 * When the device enters in hibernation state, the CX will be collapsed causing

	 * the GPU CX timer to pause. Clear the ADRENO_DEVICE_CX_TIMER_INITIALIZED flag

	 * to ensure that the CX timer is reseeded during resume.

	 */

	clear_bit(ADRENO_DEVICE_CX_TIMER_INITIALIZED, &adreno_dev->priv);



	/*

	 * Unload zap shader during device hibernation and reload it

	 * during resume as there is possibility that TZ driver

	 * is not aware of the hibernation.

	 */

	adreno_zap_shader_unload(adreno_dev);

	status = adreno_secure_pt_hibernate(adreno_dev);



err:

	kgsl_mutex_unlock(&device->mutex);

	return status;

}



static int governor_reinit(struct devfreq *devfreq, const char *governor)

{

	if (governor && !strcmp(governor, "msm-adreno-tz"))

		return msm_adreno_tz_reinit(devfreq);



	return 0;

}



static int adreno_hibernation_resume(struct device *dev)

{

	struct kgsl_device *device = dev_get_drvdata(dev);

	struct kgsl_iommu *iommu;

	struct kgsl_pwrscale *pwrscale;

	struct adreno_device *adreno_dev;

	const struct adreno_power_ops *ops;

	int ret;



	if (!device)

		return 0;



	iommu = &device->mmu.iommu;

	pwrscale = &device->pwrscale;

	adreno_dev = ADRENO_DEVICE(device);

	ops = ADRENO_POWER_OPS(adreno_dev);



	kgsl_mutex_lock(&device->mutex);



	ret = adreno_secure_pt_restore(adreno_dev);

	if (ret)

		goto err;



	iommu->smmu_aperture_set = false;



	gmu_core_dev_force_first_boot(device);



	governor_reinit(pwrscale->devfreqptr, CONFIG_QCOM_ADRENO_DEFAULT_GOVERNOR);



	ops->pm_resume(adreno_dev);



err:

	kgsl_mutex_unlock(&device->mutex);

	return ret;

}



static const struct dev_pm_ops adreno_pm_ops = {

	.suspend  = adreno_pm_suspend,

	.resume = adreno_pm_resume,

	.freeze = adreno_hibernation_suspend,

	.thaw = adreno_hibernation_resume,

	.poweroff = adreno_hibernation_suspend,

	.restore = adreno_hibernation_resume,

};

#else

static const struct dev_pm_ops adreno_pm_ops = {

	SET_SYSTEM_SLEEP_PM_OPS(adreno_pm_suspend, adreno_pm_resume)

};

#endif /* IS_ENABLED(CONFIG_QCOM_KGSL_HIBERNATION) */



static struct platform_driver adreno_platform_driver = {

	.probe = adreno_probe,

	.remove = adreno_remove,

	.driver = {

		.name = "kgsl-3d",

		.pm = &adreno_pm_ops,

		.of_match_table = of_match_ptr(adreno_match_table),

	}

};



static int __init kgsl_3d_init(void)

{

	int ret;



	ret = kgsl_core_init();

	if (ret)

		return ret;



	ret = kgsl_mmu_init();

	if (ret) {

		kgsl_core_exit();

		return ret;

	}



	gmu_core_register();

	ret = platform_driver_register(&adreno_platform_driver);

	if (ret) {

		gmu_core_unregister();

		kgsl_mmu_exit();

		kgsl_core_exit();

	}



	return ret;

}



static void __exit kgsl_3d_exit(void)

{

	platform_driver_unregister(&adreno_platform_driver);

	gmu_core_unregister();

	kgsl_mmu_exit();

	kgsl_core_exit();

}



module_param_named(preempt_enable, adreno_preemption_enable, bool, 0600);

MODULE_PARM_DESC(preempt_enable, "Enable GPU HW Preemption");



module_param_named(gpu_sku_override, kgsl_gpu_sku_override, uint, 0600);

MODULE_PARM_DESC(gpu_sku_override, "Override SKU code identifier for GPU driver");



module_param_named(gpu_speed_bin_override, kgsl_gpu_speed_bin_override, uint, 0600);

MODULE_PARM_DESC(gpu_speed_bin_override, "Override GPU speed bin");



module_param_named(slice_mask_override, adreno_slice_mask_override, uint, 0600);

MODULE_PARM_DESC(slice_mask_override, "Override GPU slice mask");



module_init(kgsl_3d_init);

module_exit(kgsl_3d_exit);



MODULE_DESCRIPTION("3D Graphics driver");

MODULE_LICENSE("GPL v2");

MODULE_SOFTDEP("pre: arm_smmu nvmem_qfprom socinfo governor_msm_adreno_tz governor_gpubw_mon governor_msm_adreno_ro");

#if (KERNEL_VERSION(6, 13, 0) <= LINUX_VERSION_CODE)

MODULE_IMPORT_NS("DMA_BUF");

#elif (KERNEL_VERSION(5, 18, 0) <= LINUX_VERSION_CODE)

MODULE_IMPORT_NS(DMA_BUF);

#endif





==========================================================================================
FILE: adreno.h
LINES: 2429 | BYTES: 84887
==========================================================================================

/* SPDX-License-Identifier: GPL-2.0-only */

/*

 * Copyright (c) 2008-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */

#ifndef __ADRENO_H

#define __ADRENO_H



#include <linux/firmware.h>

#include <linux/iopoll.h>

#include <linux/of.h>

#include <linux/soc/qcom/llcc-qcom.h>

#include "adreno_coresight.h"

#include "adreno_dispatch.h"

#include "adreno_drawctxt.h"

#include "adreno_hwsched.h"

#include "adreno_perfcounter.h"

#include "adreno_profile.h"

#include "adreno_ringbuffer.h"

#include "kgsl_sharedmem.h"



/* Used to point CP to the SMMU record during preemption */

#define SET_PSEUDO_SMMU_INFO 0

/* Used to inform CP where to save preemption data at the time of switch out */

#define SET_PSEUDO_PRIV_NON_SECURE_SAVE_ADDR 1

/* Used to inform CP where to save secure preemption data at the time of switch out */

#define SET_PSEUDO_PRIV_SECURE_SAVE_ADDR 2

/* Used to inform CP where to save per context non-secure data at the time of switch out */

#define SET_PSEUDO_NON_PRIV_SAVE_ADDR 3

/* Used to inform CP where to save preemption counter data at the time of switch out */

#define SET_PSEUDO_COUNTER 4



/* Index to preemption scratch buffer to store current QOS value */

#define QOS_VALUE_IDX KGSL_PRIORITY_MAX_RB_LEVELS



/* Size of the user context record block (in bytes) */

#define ADRENO_CP_CTXRECORD_USER_RESTORE_SIZE (192 * SZ_1K)



/* ADRENO_DEVICE - Given a kgsl_device return the adreno device struct */

#define ADRENO_DEVICE(device) \

		container_of(device, struct adreno_device, dev)



/* KGSL_DEVICE - given an adreno_device, return the KGSL device struct */

#define KGSL_DEVICE(_dev) (&((_dev)->dev))



/* ADRENO_CONTEXT - Given a context return the adreno context struct */

#define ADRENO_CONTEXT(context) \

		container_of(context, struct adreno_context, base)



/* ADRENO_GPU_DEVICE - Given an adreno device return the GPU specific struct */

#define ADRENO_GPU_DEVICE(_a) ((_a)->gpucore->gpudev)



/*

 * ADRENO_POWER_OPS - Given an adreno device return the GPU specific power

 * ops

 */

#define ADRENO_POWER_OPS(_a) ((_a)->gpucore->gpudev->power_ops)



#define ADRENO_CHIPID_CORE(_id) FIELD_GET(GENMASK(31, 24), _id)

#define ADRENO_CHIPID_MAJOR(_id) FIELD_GET(GENMASK(23, 16), _id)

#define ADRENO_CHIPID_MINOR(_id) FIELD_GET(GENMASK(15, 8), _id)

#define ADRENO_CHIPID_PATCH(_id) FIELD_GET(GENMASK(7, 0), _id)



#define ADRENO_GMU_CHIPID(_id) \

	(FIELD_PREP(GENMASK(31, 24), ADRENO_CHIPID_CORE(_id)) | \

	 FIELD_PREP(GENMASK(23, 16), ADRENO_CHIPID_MAJOR(_id)) | \

	 FIELD_PREP(GENMASK(15, 12), ADRENO_CHIPID_MINOR(_id)) | \

	 FIELD_PREP(GENMASK(11, 8), ADRENO_CHIPID_PATCH(_id)))



#define ADRENO_REV_MAJOR(_rev) FIELD_GET(GENMASK(23, 16), _rev)

#define ADRENO_REV_MINOR(_rev) FIELD_GET(GENMASK(15, 8), _rev)

#define ADRENO_REV_PATCH(_rev) FIELD_GET(GENMASK(7, 0), _rev)



#define ADRENO_GMU_REV(_rev) \

	(FIELD_PREP(GENMASK(31, 24), ADRENO_REV_MAJOR(_rev)) | \

	 FIELD_PREP(GENMASK(23, 16), ADRENO_REV_MINOR(_rev)) | \

	 FIELD_PREP(GENMASK(15, 8), ADRENO_REV_PATCH(_rev)))



/* ADRENO_GPUREV - Return the GPU ID for the given adreno_device */

#define ADRENO_GPUREV(_a) ((_a)->gpucore->gpurev)



/*

 * Disable local interrupts and CPU preemption to avoid interruptions

 * while holding the CP semaphore; otherwise, it could stall the CP.

 * Make sure to call ADRENO_RELEASE_CP_SEMAPHORE after calling the

 * below macro to reenable CPU interrupts.

 */

#define ADRENO_ACQUIRE_CP_SEMAPHORE(_adreno_dev, _flags) \

	({ \

		bool ret = true; \

		if ((_adreno_dev)->gpucore->gpudev->acquire_cp_semaphore) { \

			local_irq_save(_flags); \

			preempt_disable(); \

			ret = (_adreno_dev)->gpucore->gpudev->acquire_cp_semaphore(_adreno_dev); \

			if (!ret) { \

				preempt_enable(); \

				local_irq_restore(_flags); \

				dev_err_ratelimited(KGSL_DEVICE(_adreno_dev)->dev, \

				"Timed out waiting to acquire CP semaphore: status=0x%08x\n", \

				ret); \

			} \

		} \

		ret; \

	})



#define ADRENO_RELEASE_CP_SEMAPHORE(_adreno_dev, _flags) \

	({ \

		do { \

			if ((_adreno_dev)->gpucore->gpudev->release_cp_semaphore) { \

				(_adreno_dev)->gpucore->gpudev->release_cp_semaphore(_adreno_dev); \

				preempt_enable(); \

				local_irq_restore(_flags); \

			} \

		} while (0);\

	})



/*

 * ADRENO_FEATURE - return true if the specified feature is supported by the GPU

 * core

 */

#define ADRENO_FEATURE(_dev, _bit) \

	(((_dev)->gpucore->features_standard && \

	of_device_is_compatible((_dev)->dev.pdev->dev.of_node, "qcom,adreno")) \

	? ((_dev)->gpucore->features_standard & (_bit)) \

	: ((_dev)->gpucore->features & (_bit)))



/**

 * ADRENO_QUIRK - return true if the specified quirk is required by the GPU

 */

#define ADRENO_QUIRK(_dev, _bit) \

	((_dev)->quirks & (_bit))



#define ADRENO_FW(a, f)		(&(a->fw[f]))



/* Adreno core features */

/* The core supports SP/TP hw controlled power collapse */

#define ADRENO_SPTP_PC BIT(0)

/* The GPU supports content protection */

#define ADRENO_CONTENT_PROTECTION BIT(1)

/* The GPU supports preemption */

#define ADRENO_PREEMPTION BIT(2)

/* The GPMU supports Limits Management */

#define ADRENO_LM BIT(3)

/* The GPU supports retention for cpz registers */

#define ADRENO_CPZ_RETENTION BIT(4)

/* The core has soft fault detection available */

#define ADRENO_SOFT_FAULT_DETECT BIT(5)

/* The GMU supports IFPC power management*/

#define ADRENO_IFPC BIT(6)

/* The core supports IO-coherent memory */

#define ADRENO_IOCOHERENT BIT(7)

/*

 * The GMU supports Adaptive Clock Distribution (ACD)

 * for droop mitigation

 */

#define ADRENO_ACD BIT(8)

/* Cooperative reset enabled GMU */

#define ADRENO_COOP_RESET BIT(9)

/* Indicates that the specific target is no longer supported */

#define ADRENO_DEPRECATED BIT(10)

/* The target supports ringbuffer level APRIV */

#define ADRENO_APRIV BIT(11)

/* The GMU supports Battery Current Limiting */

#define ADRENO_BCL BIT(12)

/* L3 voting is supported with L3 constraints */

#define ADRENO_L3_VOTE BIT(13)

/* LPAC is supported  */

#define ADRENO_LPAC BIT(14)

/* Late Stage Reprojection (LSR) enablment for GMU */

#define ADRENO_LSR BIT(15)

/* GMU and kernel supports hardware fences */

#define ADRENO_HW_FENCE BIT(16)

/* Dynamic Mode Switching supported on this target */

#define ADRENO_DMS BIT(17)

/* AQE supported on this target */

#define ADRENO_AQE BIT(18)

/* Warm Boot supported on this target */

#define ADRENO_GMU_WARMBOOT BIT(19)

/* The GPU supports CLX */

#define ADRENO_CLX BIT(20)

/* Enable GMU support for GMU based thermal mitigation */

#define ADRENO_GMU_THERMAL_MITIGATION BIT(21)

/* GMU Based DCVS */

#define ADRENO_GMU_BASED_DCVS BIT(22)

/* RT hint feature for RB0 workloads */

#define ADRENO_RT_HINT BIT(23)

/* Defer allocation of preemption record gmem memory when needed */

#define ADRENO_DEFER_GMEM_ALLOC BIT(24)

/* The GMU supports MINBW voting */

#define ADRENO_GMU_MINBW BIT(25)

/* Enable GMU Based DCVS profile */

#define ADRENO_DCVS_PROFILE BIT(26)

/* Enable TSENSE power savings during sleep */

#define ADRENO_TSENSE_DYNAMIC_PERIOD BIT(27)

/* Enable GMU Based AB voting */

#define ADRENO_GMU_AB BIT(28)

/* Enable GMU Fast Context Destroy optimization */

#define ADRENO_GMU_FAST_CONTEXT_DESTROY BIT(29)

/*

 * The target uses the GPU_CX_MISC_SW_FUSE_FREQ_LIMIT_STATUS register to

 * get combined (HW + SW) speedbin when no nvmem speed_bin cell exists.

 */

#define ADRENO_SOFTFUSE BIT_ULL(32)

/*

 * Adreno GPU quirks - control bits for various workarounds

 */



/* Set TWOPASSUSEWFI in PC_DBG_ECO_CNTL (5XX/6XX) */

#define ADRENO_QUIRK_TWO_PASS_USE_WFI BIT(0)

/* Submit critical packets at GPU wake up */

#define ADRENO_QUIRK_CRITICAL_PACKETS BIT(1)

/* Mask out RB1-3 activity signals from HW hang detection logic */

#define ADRENO_QUIRK_FAULT_DETECT_MASK BIT(2)

/* Disable RB sampler datapath clock gating optimization */

#define ADRENO_QUIRK_DISABLE_RB_DP2CLOCKGATING BIT(3)

/* Disable local memory(LM) feature to avoid corner case error */

#define ADRENO_QUIRK_DISABLE_LMLOADKILL BIT(4)

/* Allow HFI to use registers to send message to GMU */

#define ADRENO_QUIRK_HFI_USE_REG BIT(5)

/* Only set protected SECVID registers once */

#define ADRENO_QUIRK_SECVID_SET_ONCE BIT(6)

/*

 * Limit number of read and write transactions from

 * UCHE block to GBIF to avoid possible deadlock

 * between GBIF, SMMU and MEMNOC.

 */

#define ADRENO_QUIRK_LIMIT_UCHE_GBIF_RW BIT(8)

/* Do explicit mode control of cx gdsc */

#define ADRENO_QUIRK_CX_GDSC BIT(9)



/* Command identifiers */

#define CONTEXT_TO_MEM_IDENTIFIER	0x2EADBEEF

#define CMD_IDENTIFIER			0x2EEDFACE

#define CMD_INTERNAL_IDENTIFIER		0x2EEDD00D

#define START_IB_IDENTIFIER		0x2EADEABE

#define END_IB_IDENTIFIER		0x2ABEDEAD

#define START_PROFILE_IDENTIFIER	0x2DEFADE1

#define END_PROFILE_IDENTIFIER		0x2DEFADE2

#define PWRON_FIXUP_IDENTIFIER		0x2AFAFAFA



/* One cannot wait forever for the core to idle, so set an upper limit to the

 * amount of time to wait for the core to go idle

 */

#define ADRENO_IDLE_TIMEOUT (20 * 1000)



#define ADRENO_FW_PFP 0

#define ADRENO_FW_SQE 0

#define ADRENO_FW_PM4 1

#define ADRENO_FW_AQE 1



#define ADRENO_GPUREV_VALUE(_major, _minor, _patchid) (((_major & 0xFF) << 16) | \

						       ((_minor & 0xFF) << 8) | \

						       (_patchid & 0xFF))

enum adreno_gpurev {

	ADRENO_REV_UNKNOWN = 0,

	ADRENO_REV_A304 = 304,

	ADRENO_REV_A305 = 305,

	ADRENO_REV_A305C = 306,

	ADRENO_REV_A306 = 307,

	ADRENO_REV_A306A = 308,

	ADRENO_REV_A310 = 310,

	ADRENO_REV_A320 = 320,

	ADRENO_REV_A330 = 330,

	ADRENO_REV_A305B = 335,

	ADRENO_REV_A405 = 405,

	ADRENO_REV_A418 = 418,

	ADRENO_REV_A420 = 420,

	ADRENO_REV_A430 = 430,

	ADRENO_REV_A505 = 505,

	ADRENO_REV_A506 = 506,

	ADRENO_REV_A508 = 508,

	ADRENO_REV_A510 = 510,

	ADRENO_REV_A512 = 512,

	ADRENO_REV_A530 = 530,

	ADRENO_REV_A540 = 540,

	ADRENO_REV_A610 = 610,

	ADRENO_REV_A611 = 611,

	ADRENO_REV_A612 = 612,

	ADRENO_REV_A615 = 615,

	ADRENO_REV_A616 = 616,

	ADRENO_REV_A618 = 618,

	ADRENO_REV_A619 = 619,

	ADRENO_REV_A620 = 620,

	ADRENO_REV_A621 = 621,

	ADRENO_REV_A622 = 622,

	ADRENO_REV_A623 = 623,

	ADRENO_REV_A630 = 630,

	ADRENO_REV_A640 = 640,

	ADRENO_REV_A642 = 642,

	ADRENO_REV_A643 = 643,

	ADRENO_REV_A650 = 650,

	ADRENO_REV_A660 = 660,

	ADRENO_REV_A662 = 662,

	ADRENO_REV_A663 = 663,

	ADRENO_REV_A680 = 680,

	ADRENO_REV_A702 = 702,

	ADRENO_REV_A704 = 704,

	/*

	 * Version numbers may exceed 1 digit

	 * Bits 16-23: Major

	 * Bits 8-15: Minor

	 * Bits 0-7: Patch id

	 */

	ADRENO_REV_GEN6_3_26_0 = ADRENO_GPUREV_VALUE(3, 26, 0),

	ADRENO_REV_GEN7_0_0 = ADRENO_GPUREV_VALUE(7, 0, 0),

	ADRENO_REV_GEN7_0_1 = ADRENO_GPUREV_VALUE(7, 0, 1),

	ADRENO_REV_GEN7_2_0 = ADRENO_GPUREV_VALUE(7, 2, 0),

	ADRENO_REV_GEN7_2_1 = ADRENO_GPUREV_VALUE(7, 2, 1),

	ADRENO_REV_GEN7_3_0 = ADRENO_GPUREV_VALUE(7, 3, 0),

	ADRENO_REV_GEN7_4_0 = ADRENO_GPUREV_VALUE(7, 4, 0),

	ADRENO_REV_GEN7_5_0 = ADRENO_GPUREV_VALUE(7, 5, 0),

	ADRENO_REV_GEN7_6_0 = ADRENO_GPUREV_VALUE(7, 6, 0),

	ADRENO_REV_GEN7_9_0 = ADRENO_GPUREV_VALUE(7, 9, 0),

	ADRENO_REV_GEN7_9_1 = ADRENO_GPUREV_VALUE(7, 9, 1),

	ADRENO_REV_GEN7_13_0 = ADRENO_GPUREV_VALUE(7, 13, 0),

	ADRENO_REV_GEN7_14_0 = ADRENO_GPUREV_VALUE(7, 14, 0),

	ADRENO_REV_GEN7_11_0 = ADRENO_GPUREV_VALUE(7, 11, 0),

	ADRENO_REV_GEN7_15_0 = ADRENO_GPUREV_VALUE(7, 15, 0),

	ADRENO_REV_GEN7_17_0 = ADRENO_GPUREV_VALUE(7, 17, 0),

	ADRENO_REV_GEN8_0_0 = ADRENO_GPUREV_VALUE(8, 0, 0),

	ADRENO_REV_GEN8_0_1 = ADRENO_GPUREV_VALUE(8, 0, 1),

	ADRENO_REV_GEN8_1_0 = ADRENO_GPUREV_VALUE(8, 1, 0),

	ADRENO_REV_GEN8_2_0 = ADRENO_GPUREV_VALUE(8, 2, 0),

	ADRENO_REV_GEN8_2_1 = ADRENO_GPUREV_VALUE(8, 2, 1),

	ADRENO_REV_GEN8_3_0 = ADRENO_GPUREV_VALUE(8, 3, 0),

	ADRENO_REV_GEN8_4_0 = ADRENO_GPUREV_VALUE(8, 4, 0),

	ADRENO_REV_GEN8_5_0 = ADRENO_GPUREV_VALUE(8, 5, 0),

	ADRENO_REV_GEN8_6_0 = ADRENO_GPUREV_VALUE(8, 6, 0),

	ADRENO_REV_GEN8_8_0 = ADRENO_GPUREV_VALUE(8, 8, 0),

	ADRENO_REV_GEN8_9_0 = ADRENO_GPUREV_VALUE(8, 9, 0),

	ADRENO_REV_GEN8_10_0 = ADRENO_GPUREV_VALUE(8, 10, 0),

	ADRENO_REV_GEN8_11_0 = ADRENO_GPUREV_VALUE(8, 11, 0),

	ADRENO_REV_GEN8_11_1 = ADRENO_GPUREV_VALUE(8, 11, 1),

	ADRENO_REV_GEN8_14_0 = ADRENO_GPUREV_VALUE(8, 14, 0),

};



#define ADRENO_SOFT_FAULT BIT(0)

#define ADRENO_HARD_FAULT BIT(1)

#define ADRENO_TIMEOUT_FAULT BIT(2)

#define ADRENO_IOMMU_STALL_ON_PAGE_FAULT BIT(3)

#define ADRENO_PREEMPT_FAULT BIT(4)

#define ADRENO_GMU_FAULT BIT(5)

#define ADRENO_CTX_DETATCH_TIMEOUT_FAULT BIT(6)

#define ADRENO_GMU_FAULT_SKIP_SNAPSHOT BIT(7)

#define ADRENO_FAULT_TYPES 8



/**

 * Bit fields for GPU_CX_MISC_CX_AHB_*_CNTL registers

 * AHB_TXFRTIMEOUTRELEASE	[8:8]

 * AHB_TXFRTIMEOUTENABLE	[9:9]

 * AHB_RESPONDERROR		[11:11]

 * AHB_ERRORSTATUSENABLE	[12:12]

 */

#define ADRENO_AHB_CNTL_DEFAULT (BIT(12) | BIT(11) | BIT(9) | BIT(8))



enum adreno_pipe_type {

	PIPE_NONE = 0,

	PIPE_BR = 1,

	PIPE_BV = 2,

	PIPE_LPAC = 3,

	PIPE_AQE0 = 4,

	PIPE_AQE1 = 5,

	PIPE_DDE_BR = 6,

	PIPE_DDE_BV = 7,

};



/* number of throttle counters for DCVS adjustment */

#define ADRENO_GPMU_THROTTLE_COUNTERS 4



struct adreno_gpudev;



/* Time to allow preemption to complete (in ms) */

#define ADRENO_PREEMPT_TIMEOUT 10000



#define PREEMPT_SCRATCH_OFFSET(id) (id * sizeof(u64))



#define PREEMPT_SCRATCH_ADDR(dev, id) \

	((dev)->preempt.scratch->gpuaddr + PREEMPT_SCRATCH_OFFSET(id))



/**

 * enum adreno_preempt_states

 * ADRENO_PREEMPT_NONE: No preemption is scheduled

 * ADRENO_PREEMPT_START: The S/W has started

 * ADRENO_PREEMPT_TRIGGERED: A preeempt has been triggered in the HW

 * ADRENO_PREEMPT_FAULTED: The preempt timer has fired

 * ADRENO_PREEMPT_PENDING: The H/W has signaled preemption complete

 * ADRENO_PREEMPT_COMPLETE: Preemption could not be finished in the IRQ handler,

 * worker has been scheduled

 */

enum adreno_preempt_states {

	ADRENO_PREEMPT_NONE = 0,

	ADRENO_PREEMPT_START,

	ADRENO_PREEMPT_TRIGGERED,

	ADRENO_PREEMPT_FAULTED,

	ADRENO_PREEMPT_PENDING,

	ADRENO_PREEMPT_COMPLETE,

};



/**

 * struct adreno_protected_regs - container for a protect register span

 */

struct adreno_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_preemption

 * @state: The current state of preemption

 * @scratch: Per-target scratch memory for implementation specific functionality

 * @timer: A timer to make sure preemption doesn't stall

 * @work: A work struct for the preemption worker (for 5XX)

 * preempt_level: The level of preemption (for 6XX)

 * skipsaverestore: To skip saverestore during L1 preemption (for 6XX)

 * usesgmem: enable GMEM save/restore across preemption (for 6XX)

 * count: Track the number of preemptions triggered

 */

struct adreno_preemption {

	atomic_t state;

	struct kgsl_memdesc *scratch;

	struct timer_list timer;

	struct work_struct work;

	unsigned int preempt_level;

	bool skipsaverestore;

	bool usesgmem;

	unsigned int count;

	/* @postamble_len: Number of dwords in KMD postamble pm4 packet */

	u32 postamble_len;

	/*

	 * @postamble_bootup_len: Number of dwords in KMD postamble pm4 packet

	 * that needs to be sent before first submission to GPU.

	 * Note: Postambles are not preserved across slumber.

	 */

	u32 postamble_bootup_len;

};



struct adreno_busy_data {

	unsigned int gpu_busy;

	unsigned int bif_ram_cycles;

	unsigned int bif_ram_cycles_read_ch1;

	unsigned int bif_ram_cycles_write_ch0;

	unsigned int bif_ram_cycles_write_ch1;

	unsigned int bif_starved_ram;

	unsigned int bif_starved_ram_ch1;

	unsigned int num_ifpc;

	unsigned int throttle_cycles[ADRENO_GPMU_THROTTLE_COUNTERS];

	u32 bcl_throttle;

};



/**

 * struct adreno_firmware - Struct holding fw details

 * @fwvirt: Buffer which holds the ucode

 * @size: Size of ucode buffer

 * @version: Version of ucode

 * @memdesc: Memory descriptor which holds ucode buffer info

 */

struct adreno_firmware {

	unsigned int *fwvirt;

	size_t size;

	unsigned int version;

	struct kgsl_memdesc *memdesc;

};



/**

 * struct adreno_perfcounter_list_node - struct to store perfcounters

 * allocated by a process on a kgsl fd.

 * @groupid: groupid of the allocated perfcounter

 * @countable: countable assigned to the allocated perfcounter

 * @node: list node for perfcounter_list of a process

 */

struct adreno_perfcounter_list_node {

	unsigned int groupid;

	unsigned int countable;

	struct list_head node;

};



/**

 * struct adreno_device_private - Adreno private structure per fd

 * @dev_priv: the kgsl device private structure

 * @perfcounter_list: list of perfcounters used by the process

 */

struct adreno_device_private {

	struct kgsl_device_private dev_priv;

	struct list_head perfcounter_list;

};



/**

 * struct adreno_reglist_list - A container for list of registers and

 * number of registers in the list

 */

struct adreno_reglist_list {

	/** @reg: List of register **/

	const u32 *regs;

	/** @count: Number of registers in the list **/

	u32 count;

};



/**

 * struct adreno_power_ops - Container for target specific power up/down

 * sequences

 */

struct adreno_power_ops {

	/**

	 * @first_open: Target specific function triggered when first kgsl

	 * instance is opened

	 */

	int (*first_open)(struct adreno_device *adreno_dev);

	/**

	 * @last_close: Target specific function triggered when last kgsl

	 * instance is closed

	 */

	int (*last_close)(struct adreno_device *adreno_dev);

	/**

	 * @active_count_get: Target specific function to keep gpu from power

	 * collapsing

	 */

	int (*active_count_get)(struct adreno_device *adreno_dev);

	/** @pm_suspend: Target specific function to suspend the driver */

	int (*pm_suspend)(struct adreno_device *adreno_dev);

	/** @pm_resume: Target specific function to resume the driver */

	void (*pm_resume)(struct adreno_device *adreno_dev);

	/**

	 * @touch_wakeup: Target specific function to start gpu on touch event

	 */

	void (*touch_wakeup)(struct adreno_device *adreno_dev);

	/** @gpu_clock_set: Target specific function to set gpu frequency */

	int (*gpu_clock_set)(struct adreno_device *adreno_dev, u32 pwrlevel);

	/** @gpu_bus_set: Target specific function to set gpu bandwidth */

	int (*gpu_bus_set)(struct adreno_device *adreno_dev, int bus_level,

		u32 ab);

	/** @gmu_based_dcvs_pwr_ops: Function ops for GMU based DCVS power operations */

	void (*gmu_based_dcvs_pwr_ops)(struct adreno_device *adreno_dev, u32 arg,

			enum gpu_pwrlevel_op op);

	/** @set_thermal_index: Function ops for sending thermal constraint to GMU */

	void (*set_thermal_index)(struct adreno_device *adreno_dev);

};



struct adreno_ubwc_props {

	/** @mode: UBWC mode value */

	u32 mode;

	/** @mode2: Secondary UBWC mode (Gen8 only) */

	u32 mode2;

	/** @level2_swizzling_dis: Level 2 swizzling disable (Gen6 only) */

	u32 level2_swizzling_dis;

	/** @amsbc: AMSBC enable flag */

	bool amsbc;

	/** @rgb565_predicator: RGB565 predictor enable */

	bool rgb565_predicator;

	/** @yuvnotcomptofc: YUV not compressed to FC (Gen8 only) */

	bool yuvnotcomptofc;

	/** @fp16compoptdis: FP16 compression optimization disable (Gen8 only) */

	bool fp16compoptdis;

	/** @rgba8888_lossless: RGBA8888 lossless mode (Gen8 only) */

	bool rgba8888_lossless;

};



/**

 * struct adreno_gpu_core - A specific GPU core definition

 * @gpurev: Unique GPU revision identifier

 * @core: Match for the core version of the GPU

 * @major: Match for the major version of the GPU

 * @minor: Match for the minor version of the GPU

 * @patchid: Match for the patch revision of the GPU

 * @features: Common adreno features supported by this core

 * @gpudev: Pointer to the GPU family specific functions for this core

 * @uche_gmem_alignment: Alignment required for UCHE GMEM base

 * @gmem_size: Amount of binning memory (GMEM/OCMEM) to reserve for the core

 * @bus_width: Bytes transferred in 1 cycle

 */

struct adreno_gpu_core {

	enum adreno_gpurev gpurev;

	unsigned int core, major, minor, patchid;

	/**

	 * @compatible: If specified, use the compatible string to match the

	 * device

	 */

	const char *compatible;

	u64 features;

	/** @features_standard: Features only supported on standard kernel */

	u64 features_standard;

	const struct adreno_gpudev *gpudev;

	const struct adreno_perfcounters *perfcounters;

	u32 uche_gmem_alignment;

	size_t gmem_size;

	u32 bus_width;

	/** @snapshot_size: Size of the static snapshot region in bytes */

	u32 snapshot_size;

	/** @num_ddr_channels: Number of DDR channels */

	u32 num_ddr_channels;

	/**

	 * @chipid: Optional GPU chip ID for publically released chipsets; required when

	 * using standard devicetree bindings.

	 */

	u32 chipid;

	/**

	 * @speedbins: Optional table of fuse to speedbin mappings

	 *

	 * Consists of pairs of fuse, index mappings, terminated with

	 * {SHRT_MAX, 0} sentinel.

	 */

	struct kgsl_speedbin *speedbins;

	/** @ubwc_mode: Supported UBWC mode */

	u32 ubwc_mode;

	/** @mal: Minimum access length */

	u32 mal;

	/** @highest_bank_bit: The bit of the highest DDR bank */

	u32 highest_bank_bit;

	/** @gpu_model: Gpu model name */

	const char *gpu_model;

	/** @smmu_aperture: List of registers and values to write for SMMU aperture */

	const struct kgsl_regmap_list *smmu_aperture;

	/** @smmu_aperture_count: Number of registers in @smmu_aperture */

	u32 smmu_aperture_count;

};



/**

 * struct adreno_dispatch_ops - Common functions for dispatcher operations

 */

struct adreno_dispatch_ops {

	/* @close: Shut down the dispatcher */

	void (*close)(struct adreno_device *adreno_dev);

	/* @queue_cmds: Queue a command on the context */

	int (*queue_cmds)(struct kgsl_device_private *dev_priv,

		struct kgsl_context *context, struct kgsl_drawobj *drawobj[],

		u32 count, u32 *timestamp);

	/* @queue_context: Queue a context to be dispatched */

	void (*queue_context)(struct adreno_device *adreno_dev,

			struct adreno_context *drawctxt);

	int (*setup_context)(struct adreno_device *adreno_dev,

			struct adreno_context *drawctxt);

	/* @create_hw_fence: Create a hardware fence */

	void (*create_hw_fence)(struct adreno_device *adreno_dev, struct kgsl_sync_fence *kfence);

};



/**

 * struct adreno_fault_proc - Structure to hold data on faulting processes

 */

struct adreno_fault_proc {

	/** @comm: Task name of the faulting process */

	char comm[TASK_COMM_LEN];

	/** @fault_count: Count of the GPU faults from this process */

	u32 fault_count;

};



#define ADRENO_MAX_FAULTING_PROCS 10



/**

 * struct adreno_device - The mothership structure for all adreno related info

 * @dev: Reference to struct kgsl_device

 * @priv: Holds the private flags specific to the adreno_device

 * @chipid: Chip ID specific to the GPU

 * @gpucore: Pointer to the adreno_gpu_core structure

 * @gpmu_cmds_size: Length of gpmu cmd stream

 * @gpmu_cmds: gpmu cmd stream

 * @ringbuffers: Array of pointers to adreno_ringbuffers

 * @num_ringbuffers: Number of ringbuffers for the GPU

 * @cur_rb: Pointer to the current ringbuffer

 * @next_rb: Ringbuffer we are switching to during preemption

 * @prev_rb: Ringbuffer we are switching from during preemption

 * @ft_policy: Defines the fault tolerance policy

 * @long_ib_detect: Long IB detection availability

 * @cooperative_reset: Indicates if graceful death handshake is enabled

 * between GMU and GPU

 * @profile: Container for adreno profiler information

 * @dispatcher: Container for adreno GPU dispatcher

 * @pwron_fixup: Command buffer to run a post-power collapse shader workaround

 * @pwron_fixup_dwords: Number of dwords in the command buffer

 * @input_work: Work struct for turning on the GPU after a touch event

 * @busy_data: Struct holding GPU VBIF busy stats

 * @ram_cycles_lo: Number of DDR clock cycles for the monitor session (Only

 * DDR channel 0 read cycles in case of GBIF)

 * @ram_cycles_lo_ch1_read: Number of DDR channel 1 Read clock cycles for

 * the monitor session

 * @ram_cycles_lo_ch0_write: Number of DDR channel 0 Write clock cycles for

 * the monitor session

 * @ram_cycles_lo_ch1_write: Number of DDR channel 0 Write clock cycles for

 * the monitor session

 * @starved_ram_lo: Number of cycles VBIF/GBIF is stalled by DDR (Only channel 0

 * stall cycles in case of GBIF)

 * @starved_ram_lo_ch1: Number of cycles GBIF is stalled by DDR channel 1

 * @halt: Atomic variable to check whether the GPU is currently halted

 * @pending_irq_refcnt: Atomic variable to keep track of running IRQ handlers

 * @ctx_d_debugfs: Context debugfs node

 * @profile_buffer: Memdesc holding the drawobj profiling buffer

 * @profile_index: Index to store the start/stop ticks in the profiling

 * buffer

 * @pwrup_reglist: Memdesc holding the power up register list

 * which is used by CP during preemption and IFPC

 * @lm_sequence: Pointer to the start of the register write sequence for LM

 * @lm_size: The dword size of the LM sequence

 * @lm_limit: limiting value for LM

 * @lm_threshold_count: register value for counter for lm threshold breakin

 * @lm_threshold_cross: number of current peaks exceeding threshold

 * @ifpc_count: Number of times the GPU went into IFPC

 * @highest_bank_bit: Value of the highest bank bit

 * @gpmu_throttle_counters - counters for number of throttled clocks

 * @irq_storm_work: Worker to handle possible interrupt storms

 * @active_list: List to track active contexts

 * @active_list_lock: Lock to protect active_list

 * @gpu_llc_slice: GPU system cache slice descriptor

 * @gpu_llc_slice_enable: To enable the GPU system cache slice or not

 * @gpuhtw_llc_slice: GPU pagetables system cache slice descriptor

 * @gpuhtw_llc_slice_enable: To enable the GPUHTW system cache slice or not

 * @gpumv_llc_slice: GPU MV buffer system cache slice descriptor

 * @gpumv_llc_slice_enable: To enable GPUMV buffer system cache slice or not

 * @zap_loaded: Used to track if zap was successfully loaded or not

 */

struct adreno_device {

	struct kgsl_device dev;    /* Must be first field in this struct */

	unsigned long priv;

	unsigned int chipid;

	/** @uche_gmem_base: Base address of GMEM for UCHE access */

	u64 uche_gmem_base;

	const struct adreno_gpu_core *gpucore;

	struct adreno_firmware fw[2];

	size_t gpmu_cmds_size;

	unsigned int *gpmu_cmds;

	struct adreno_ringbuffer ringbuffers[KGSL_PRIORITY_MAX_RB_LEVELS];

	int num_ringbuffers;

	struct adreno_ringbuffer *cur_rb;

	struct adreno_ringbuffer *next_rb;

	struct adreno_ringbuffer *prev_rb;

	unsigned long ft_policy;

	bool long_ib_detect;

	bool cooperative_reset;

	struct adreno_profile profile;

	struct adreno_dispatcher dispatcher;

	struct kgsl_memdesc *pwron_fixup;

	unsigned int pwron_fixup_dwords;

	struct work_struct input_work;

	struct adreno_busy_data busy_data;

	unsigned int ram_cycles_lo;

	unsigned int ram_cycles_lo_ch1_read;

	unsigned int ram_cycles_lo_ch0_write;

	unsigned int ram_cycles_lo_ch1_write;

	unsigned int starved_ram_lo;

	unsigned int starved_ram_lo_ch1;

	atomic_t halt;

	atomic_t pending_irq_refcnt;

	struct dentry *ctx_d_debugfs;

	/** @lm_enabled: True if limits management is enabled for this target */

	bool lm_enabled;

	/** @acd_enabled: True if acd is enabled for this target */

	bool acd_enabled;

	/** @hwcg_enabled: True if hardware clock gating is enabled */

	bool hwcg_enabled;

	/** @throttling_enabled: True if LM throttling is enabled on a5xx */

	bool throttling_enabled;

	/** @sptp_pc_enabled: True if SPTP power collapse is enabled on a5xx */

	bool sptp_pc_enabled;

	/** @bcl_enabled: True if BCL is enabled */

	bool bcl_enabled;

	/** @clx_enabled: True if CLX is enabled */

	bool clx_enabled;

	/** @isense_reg_mapped: True if isense registers are mapped to regmap */

	bool isense_reg_mapped;

	/** @lpac_enabled: True if LPAC is enabled */

	bool lpac_enabled;

	/** @dms_enabled: True if DMS is enabled */

	bool dms_enabled;

	/** @minbw_enabled: True if minbw vote is enabled */

	bool minbw_enabled;

	/** @malu_enabled: True if mALU is enabled */

	bool malu_enabled;

	/** @preempt_override: True if command line param enables preemption */

	bool preempt_override;

	struct kgsl_memdesc *profile_buffer;

	unsigned int profile_index;

	struct kgsl_memdesc *pwrup_reglist;

	uint32_t *lm_sequence;

	uint32_t lm_size;

	struct adreno_preemption preempt;

	struct work_struct gpmu_work;

	uint32_t lm_leakage;

	uint32_t lm_limit;

	uint32_t lm_threshold_count;

	uint32_t lm_threshold_cross;

	uint32_t ifpc_count;



	unsigned int highest_bank_bit;

	/** @ubwc_mode: Supported UBWC mode */

	u32 ubwc_mode;

	/** @mal: Minimum access length */

	u32 mal;

	unsigned int quirks;



#ifdef CONFIG_QCOM_KGSL_CORESIGHT

	/** @gx_coresight:  A coresight instance for GX */

	struct adreno_coresight_device gx_coresight;

	/** @gx_coresight:  A coresight instance for CX */

	struct adreno_coresight_device cx_coresight;

	/** @funnel_gfx:  A coresight instance for gfx funnel */

	struct adreno_funnel_device funnel_gfx;

	/**

	 * @coresight_en_cnt: Retain mask if either coresight-gfx

	 * or coresight-gfx-cx is enabled; remove only when both

	 * are disabled

	 */

	u32 coresight_en_cnt;

#endif



	uint32_t gpmu_throttle_counters[ADRENO_GPMU_THROTTLE_COUNTERS];

	struct work_struct irq_storm_work;



	struct list_head active_list;

	spinlock_t active_list_lock;



	void *gpu_llc_slice;

	bool gpu_llc_slice_enable;

	void *gpuhtw_llc_slice;

	bool gpuhtw_llc_slice_enable;

	void *gpumv_llc_slice;

	bool gpumv_llc_slice_enable;

	unsigned int zap_loaded;

	/** @zap_enabled: True when zap-shader is present */

	bool zap_enabled;

	/**

	 * @critpkts: Memory descriptor for 5xx critical packets if applicable

	 */

	struct kgsl_memdesc *critpkts;

	/**

	 * @critpkts: Memory descriptor for 5xx secure critical packets

	 */

	struct kgsl_memdesc *critpkts_secure;

	/** @irq_mask: The current interrupt mask for the GPU device */

	u32 irq_mask;

	/* @dispatch_ops: A pointer to a set of adreno dispatch ops */

	const struct adreno_dispatch_ops *dispatch_ops;

	/** @hwsched: Container for the hardware dispatcher */

	struct adreno_hwsched hwsched;

	/*

	 * @perfcounter: Flag to clear perfcounters across contexts and

	 * controls perfcounter ioctl read

	 */

	bool perfcounter;

	/** @gmu_hub_clk_freq: Gmu hub interface clock frequency */

	u64 gmu_hub_clk_freq;

	/* @patch_reglist: If false power up register list needs to be patched */

	bool patch_reglist;

	/*

	 * @uche_client_pf: uche_client_pf client register configuration

	 * for pf debugging

	 */

	u32 uche_client_pf;

	/**

	 * @bcl_data: bit 0 contains response type for bcl alarms and bits 1:24 controls

	 * throttle level for bcl alarm levels 0-2. If not set, gmu fw sets default throttle levels.

	 */

	u32 bcl_data;

	/* @minbw_data: Min bw level to vote for when entering ifpc */

	u32 minbw_data;

	/*

	 * @bcl_debugfs_dir: Debugfs directory node for bcl related nodes

	 */

	struct dentry *bcl_debugfs_dir;

	/** @bcl_throttle_time_us: Total time in us spent in BCL throttling */

	u32 bcl_throttle_time_us;

	/* @preemption_debugfs_dir: Debugfs directory node for preemption related nodes */

	struct dentry *preemption_debugfs_dir;

	/* @hwsched_enabled: If true, hwsched is enabled */

	bool hwsched_enabled;

	/* @fastblend_enabled: True if fastblend feature is enabled */

	bool fastblend_enabled;

	/* @raytracing_enabled: True if raytracing feature is enabled */

	bool raytracing_enabled;

	/* @feature_fuse: feature fuse value read from HW */

	u32 feature_fuse;

	/** @gmu_ab: Track if GMU supports ab vote */

	bool gmu_ab;

	/** @ifpc_hyst: IFPC long hysteresis value */

	u32 ifpc_hyst;

	/** @ifpc_hyst_floor: IFPC long hysteresis floor value */

	u32 ifpc_hyst_floor;

	/*

	 * @no_restore_count: Keep track of perfcounter requests that don't have

	 * ADRENO_PERFCOUNTER_GROUP_RESTORE flag set

	 */

	u32 no_restore_count;

	/*

	 * @ahb_timeout_val: AHB transaction timeout value.

	 * If set, a timeout will occur in 2 ^ (ahb_timeout_val + 1) cycles.

	 */

	u32 ahb_timeout_val;

	/** @fault_counts: Keep track of GPU faults */

	u32 fault_counts[ADRENO_FAULT_TYPES];

	/**

	 * @fault_procs: Array to keep track of per process GPU fault count sorted by the number

	 * of GPU faults

	 */

	struct adreno_fault_proc fault_procs[ADRENO_MAX_FAULTING_PROCS];

	/** @fault_stats_lock: A R/W lock to protect GPU fault statistics */

	rwlock_t fault_stats_lock;

	/** @fault_recovery_mutex: Mutex taken during fault handling in the dispatcher */

	struct mutex fault_recovery_mutex;

	/** @pm_nb: Notifier block for defining a callback that gets called during system suspend */

	struct notifier_block pm_nb;

	/** @suspend_recovery_gate: Gate to wait on for system to come out of suspend */

	struct completion suspend_recovery_gate;

	/** @scheduler_worker: kthread worker for scheduling gpu commands */

	struct kthread_worker *scheduler_worker;

	/** @scheduler_work: work_struct to put the gpu command scheduler in a work queue */

	struct kthread_work scheduler_work;

	/** @scheduler_fault: Atomic to trigger scheduler based fault recovery */

	atomic_t scheduler_fault;

	/** @dcvs_tuning_mutex: Mutex taken during dcvs tuning */

	struct mutex dcvs_tuning_mutex;

	/** @dcvs_tuning_mingap_lvl: Current DCVS tuning level for mingap */

	u32 dcvs_tuning_mingap_lvl;

	/** @dcvs_tuning_penalty_lvl: Current DCVS tuning level for penalty */

	u32 dcvs_tuning_penalty_lvl;

	/** @dcvs_tuning_numbusy_lvl: Current DCVS tuning level for numbusy */

	u32 dcvs_tuning_numbusy_lvl;

	/** @total_ctxt_record_sz: Size of the total preemption record in bytes */

	u64 total_ctxt_record_sz;

	/** @dcvs_profile_enabled: True if DCVS profile is enabled */

	bool dcvs_profile_enabled;

	/** @aqe_ctxt_record_sz: Size of the AQE section in preemption record in bytes */

	u64 aqe_ctxt_record_sz;

	/** @ubwc_cfg: Pointer to hold struct qcom_ubwc_cfg_data fetched from

	 * qcom_ubwc_config_get_data() API

	 */

	void *ubwc_cfg_data;

	/** @adreno_ubwc_props: Container of all UBWC props required to program NC_MODE_CNTL

	 * registers

	 */

	struct adreno_ubwc_props ubwc_props;

	/** @multidraw_mode: Whether PC unroll is enabled for multidraw **/

	u32 multidraw_mode;

	/** @viz_flush_draw_count: Draw count to flush visibility stream **/

	u32 viz_flush_draw_count;

	/** @viz_flush_prim_count: Prim count to flush visibility stream **/

	u32 viz_flush_prim_count;

};



/* Time to wait for suspend recovery gate to complete */

#define ADRENO_SUSPEND_RECOVERY_GATE_TIMEOUT_MS 5000



/**

 * enum adreno_device_flags - Private flags for the adreno_device

 * @ADRENO_DEVICE_PWRON - Set during init after a power collapse

 * @ADRENO_DEVICE_PWRON_FIXUP - Set if the target requires the shader fixup

 * after power collapse

 * @ADRENO_DEVICE_STARTED - Set if the device start sequence is in progress

 * @ADRENO_DEVICE_FAULT - Set if the device is currently in fault (and shouldn't

 * send any more commands to the ringbuffer)

 * @ADRENO_DEVICE_DRAWOBJ_PROFILE - Set if the device supports drawobj

 * profiling via the ALWAYSON counter

 * @ADRENO_DEVICE_PREEMPTION - Turn on/off preemption

 * @ADRENO_DEVICE_SOFT_FAULT_DETECT - Set if soft fault detect is enabled

 * @ADRENO_DEVICE_GPMU_INITIALIZED - Set if GPMU firmware initialization succeed

 * @ADRENO_DEVICE_ISDB_ENABLED - Set if the Integrated Shader DeBugger is

 * attached and enabled

 * @ADRENO_DEVICE_CACHE_FLUSH_TS_SUSPENDED - Set if a CACHE_FLUSH_TS irq storm

 * is in progress

 */

enum adreno_device_flags {

	ADRENO_DEVICE_PWRON = 0,

	ADRENO_DEVICE_PWRON_FIXUP = 1,

	ADRENO_DEVICE_INITIALIZED = 2,

	ADRENO_DEVICE_STARTED = 5,

	ADRENO_DEVICE_FAULT = 6,

	ADRENO_DEVICE_DRAWOBJ_PROFILE = 7,

	ADRENO_DEVICE_GPU_REGULATOR_ENABLED = 8,

	ADRENO_DEVICE_PREEMPTION = 9,

	ADRENO_DEVICE_SOFT_FAULT_DETECT = 10,

	ADRENO_DEVICE_GPMU_INITIALIZED = 11,

	ADRENO_DEVICE_ISDB_ENABLED = 12,

	ADRENO_DEVICE_CACHE_FLUSH_TS_SUSPENDED = 13,

	/** @ADRENO_DEVICE_DMS: Set if DMS is enabled */

	ADRENO_DEVICE_DMS = 14,

	/** @ADRENO_DEVICE_GMU_AB: Set if AB vote via GMU is enabled */

	ADRENO_DEVICE_GMU_AB = 15,

	/** @ADRENO_DEVICE_CX_TIMER_INITIALIZED: Set if the CX timer is initialized */

	ADRENO_DEVICE_CX_TIMER_INITIALIZED = 17,

	/** @ADRENO_DEVICE_RESET_RECOVERY: Set if the ADRENO device under goes reset recovery */

	ADRENO_DEVICE_RESET_RECOVERY = 18,

	/** @ADRENO_DEVICE_FIRST_BOOT_DONE: Set if the ADRENO device first boot is done */

	ADRENO_DEVICE_FIRST_BOOT_DONE = 19,

	/** @ADRENO_DEVICE_FAST_CONTEXT_DESTROY: Set if fast context destroy is enabled on GMU */

	ADRENO_DEVICE_FAST_CONTEXT_DESTROY = 20,

	/** @ADRENO_DEVICE_ALLOW_MALU_WORKLOAD: mALU workload is supported by GMU and GPU */

	ADRENO_DEVICE_ALLOW_MALU_WORKLOAD = 21,

};



/**

 * struct adreno_drawobj_profile_entry - a single drawobj entry in the

 * kernel profiling buffer

 * @started: Number of GPU ticks at start of the drawobj

 * @retired: Number of GPU ticks at the end of the drawobj

 * @ctx_start: CP_ALWAYS_ON_CONTEXT tick at start of the drawobj

 * @ctx_end: CP_ALWAYS_ON_CONTEXT tick at end of the drawobj

 */

struct adreno_drawobj_profile_entry {

	uint64_t started;

	uint64_t retired;

	uint64_t ctx_start;

	uint64_t ctx_end;

};



#define ADRENO_DRAWOBJ_PROFILE_OFFSET(_index, _member) \

	 ((_index) * sizeof(struct adreno_drawobj_profile_entry) \

	  + offsetof(struct adreno_drawobj_profile_entry, _member))



/**

 * struct adreno_submit_time - utility structure to store the wall clock / GPU

 * ticks at command submit time

 * @ticks: GPU ticks at submit time (from the 19.2Mhz timer)

 * @ktime: local clock time (in nanoseconds)

 * @utime: Wall clock time

 * @drawobj: the object that we want to profile

 */

struct adreno_submit_time {

	u64 ticks;

	u64 ktime;

	struct timespec64 utime;

	struct kgsl_drawobj *drawobj;

};



/**

 * adreno_regs: List of registers that are used in kgsl driver for all

 * 3D devices. Each device type has different offset value for the same

 * register, so an array of register offsets are declared for every device

 * and are indexed by the enumeration values defined in this enum

 */

enum adreno_regs {

	ADRENO_REG_CP_RB_BASE,

	ADRENO_REG_CP_RB_BASE_HI,

	ADRENO_REG_CP_LPAC_RB_BASE,

	ADRENO_REG_CP_LPAC_RB_BASE_HI,

	ADRENO_REG_CP_RB_RPTR,

	ADRENO_REG_CP_RB_WPTR,

	ADRENO_REG_CP_ME_CNTL,

	ADRENO_REG_CP_RB_CNTL,

	ADRENO_REG_CP_IB1_BASE,

	ADRENO_REG_CP_IB1_BASE_HI,

	ADRENO_REG_CP_IB1_BUFSZ,

	ADRENO_REG_CP_IB2_BASE,

	ADRENO_REG_CP_IB2_BASE_HI,

	ADRENO_REG_CP_IB2_BUFSZ,

	ADRENO_REG_RBBM_STATUS,

	ADRENO_REG_RBBM_STATUS3,

	ADRENO_REG_RBBM_INT_0_MASK,

	ADRENO_REG_RBBM_SW_RESET_CMD,

	ADRENO_REG_GMU_AO_HOST_INTERRUPT_MASK,

	ADRENO_REG_GMU_GMU2HOST_INTR_MASK,

	ADRENO_REG_REGISTER_MAX,

};



#define ADRENO_REG_UNUSED	0xFFFFFFFF

#define ADRENO_REG_SKIP	0xFFFFFFFE

#define ADRENO_REG_DEFINE(_offset, _reg)[_offset] = _reg



struct adreno_irq_funcs {

	void (*func)(struct adreno_device *adreno_dev, int mask);

};

#define ADRENO_IRQ_CALLBACK(_c) { .func = _c }



/*

 * struct adreno_debugbus_block - Holds info about debug buses of a chip

 * @block_id: Bus identifier

 * @dwords: Number of dwords of data that this block holds

 */

struct adreno_debugbus_block {

	unsigned int block_id;

	unsigned int dwords;

};



struct adreno_gpudev {

	/*

	 * These registers are in a different location on different devices,

	 * so define them in the structure and use them as variables.

	 */

	unsigned int *const reg_offsets;



	/* GPU specific function hooks */

	int (*probe)(struct platform_device *pdev, u32 chipid,

		const struct adreno_gpu_core *gpucore);

	void (*snapshot)(struct adreno_device *adreno_dev,

				struct kgsl_snapshot *snapshot);

	irqreturn_t (*irq_handler)(struct adreno_device *adreno_dev);

	int (*init)(struct adreno_device *adreno_dev);

	void (*remove)(struct adreno_device *adreno_dev);

	int (*rb_start)(struct adreno_device *adreno_dev);

	int (*start)(struct adreno_device *adreno_dev);

	int (*regulator_enable)(struct adreno_device *adreno_dev);

	void (*regulator_disable)(struct adreno_device *adreno_dev);

	void (*pwrlevel_change_settings)(struct adreno_device *adreno_dev,

				unsigned int prelevel, unsigned int postlevel,

				bool post);

	void (*preemption_schedule)(struct adreno_device *adreno_dev);

	void (*context_detach)(struct adreno_context *drawctxt);

	void (*pre_reset)(struct adreno_device *adreno_dev);

	void (*gpu_keepalive)(struct adreno_device *adreno_dev,

			bool state);

	bool (*hw_isidle)(struct adreno_device *adreno_dev);

	const char *(*iommu_fault_block)(struct kgsl_device *device,

				unsigned int fsynr1);

	int (*reset)(struct adreno_device *adreno_dev);

	/** @soft_reset: Soft reset GPU on fault recovery */

	int (*soft_reset)(struct adreno_device *adreno_dev,

				struct kgsl_context *context, bool ctx_guilty);

	/** @read_alwayson: Return the current value of the alwayson counter */

	u64 (*read_alwayson)(struct adreno_device *adreno_dev);

	/**

	 * @power_ops: Target specific function pointers to power up/down the

	 * gpu

	 */

	const struct adreno_power_ops *power_ops;

	int (*clear_pending_transactions)(struct adreno_device *adreno_dev);

	void (*deassert_gbif_halt)(struct adreno_device *adreno_dev);

	int (*ringbuffer_submitcmd)(struct adreno_device *adreno_dev,

			struct kgsl_drawobj_cmd *cmdobj, u32 flags,

			struct adreno_submit_time *time);

	/**

	 * @is_hw_collapsible: Return true if the hardware can be collapsed.

	 * Only used by non GMU/RGMU targets

	 */

	bool (*is_hw_collapsible)(struct adreno_device *adreno_dev);

	/**

	 * @power_stats - Return the perfcounter statistics for DCVS

	 */

	void (*power_stats)(struct adreno_device *adreno_dev,

			struct kgsl_power_stats *stats);

	int (*add_to_va_minidump)(struct adreno_device *adreno_dev);

	/**

	 * @gx_is_on - Return true if both gfx clock and gxgdsc are enabled.

	 */

	bool (*gx_is_on)(struct adreno_device *adreno_dev);

	/**

	 * @send_recurring_cmdobj - Target specific function to send recurring IBs to GMU

	 */

	int (*send_recurring_cmdobj)(struct adreno_device *adreno_dev,

		struct kgsl_drawobj_cmd *cmdobj);

	/**

	 * @perfcounter_remove: Remove perfcounter from the power up list

	 */

	int (*perfcounter_remove)(struct adreno_device *adreno_dev,

			struct adreno_perfcount_register *reg, u32 groupid);

	/**

	 * @set_isdb_breakpoint_registers - Program isdb registers to issue break command

	 */

	void (*set_isdb_breakpoint_registers)(struct adreno_device *adreno_dev);

	/**

	 * @context_destroy: Target specific function called during context destruction

	 */

	void (*context_destroy)(struct adreno_device *adreno_dev, struct adreno_context *drawctxt);

	/**

	 * @swfuse_irqctrl: To enable/disable sw fuse violation interrupt

	 */

	void (*swfuse_irqctrl)(struct adreno_device *adreno_dev, bool state);

	/**

	 * @lpac_store: To enable/disable lpac at runtime

	 */

	int (*lpac_store)(struct adreno_device *adreno_dev, bool enable);

	/*

	 * @get_uche_trap_base: Return the UCHE_TRAP_BASE value

	 */

	u64 (*get_uche_trap_base)(void);

	/**

	 * @fault_header: Print fault header

	 */

	void (*fault_header)(struct adreno_device *adreno_dev, struct kgsl_drawobj *drawobj);

	/**

	 * @lpac_fault_header: Print LPAC fault header

	 */

	void (*lpac_fault_header)(struct adreno_device *adreno_dev, struct kgsl_drawobj *drawobj);

	/**

	 * @power_feature_stats: Sample feature related perfcounter stats

	 */

	void (*power_feature_stats)(struct adreno_device *adreno_dev);

	/**

	 * @acquire_cp_semaphore: Return true if CP semaphore is acquired, otherwise false

	 */

	bool (*acquire_cp_semaphore)(struct adreno_device *adreno_dev);

	/**

	 * @release_cp_semaphore: Release CP semaphore

	 */

	void (*release_cp_semaphore)(struct adreno_device *adreno_dev);

	/** @get_gmem_size: Return the GMEM size */

	u32 (*get_gmem_size)(struct adreno_device *adreno_dev);

	/**

	 * @read_speedbin: Read speedbin from a target-specific register.

	 * Only implemented by targets that set ADRENO_SOFTFUSE.

	 */

	void (*read_speedbin)(struct adreno_device *adreno_dev, u32 *speedbin);

};



/**

 * enum kgsl_ft_policy_bits - KGSL fault tolerance policy bits

 * @KGSL_FT_OFF: Disable fault detection (not used)

 * @KGSL_FT_REPLAY: Replay the faulting command

 * @KGSL_FT_SKIPIB: Skip the faulting indirect buffer

 * @KGSL_FT_SKIPFRAME: Skip the frame containing the faulting IB

 * @KGSL_FT_DISABLE: Tells the dispatcher to disable FT for the command obj

 * @KGSL_FT_TEMP_DISABLE: Disables FT for all commands

 * @KGSL_FT_THROTTLE: Disable the context if it faults too often

 * @KGSL_FT_SKIPCMD: Skip the command containing the faulting IB

 */

enum kgsl_ft_policy_bits {

	KGSL_FT_OFF = 0,

	KGSL_FT_REPLAY,

	KGSL_FT_SKIPIB,

	KGSL_FT_SKIPFRAME,

	KGSL_FT_DISABLE,

	KGSL_FT_TEMP_DISABLE,

	KGSL_FT_THROTTLE,

	KGSL_FT_SKIPCMD,

	/* KGSL_FT_MAX_BITS is used to calculate the mask */

	KGSL_FT_MAX_BITS,

	/* Internal bits - set during GFT */

	/* Skip the PM dump on replayed command obj's */

	KGSL_FT_SKIP_PMDUMP = 31,

};



#define KGSL_FT_POLICY_MASK GENMASK(KGSL_FT_MAX_BITS - 1, 0)



#define FOR_EACH_RINGBUFFER(_dev, _rb, _i)			\

	for ((_i) = 0, (_rb) = &((_dev)->ringbuffers[0]);	\

		(_i) < (_dev)->num_ringbuffers;			\

		(_i)++, (_rb)++)



extern const struct adreno_power_ops adreno_power_operations;



extern const struct adreno_gpudev adreno_a5xx_gpudev;

extern const struct adreno_gpudev adreno_a6xx_gpudev;

extern const struct adreno_gpudev adreno_a6xx_rgmu_gpudev;

extern const struct adreno_gpudev adreno_a619_holi_gpudev;

extern const struct adreno_gpudev adreno_a611_gpudev;



extern int adreno_wake_nice;

extern unsigned int adreno_wake_timeout;



int adreno_start(struct kgsl_device *device, int priority);

long adreno_ioctl(struct kgsl_device_private *dev_priv,

		unsigned int cmd, unsigned long arg);



long adreno_ioctl_helper(struct kgsl_device_private *dev_priv,

		unsigned int cmd, unsigned long arg,

		const struct kgsl_ioctl *cmds, int len);



int adreno_spin_idle(struct adreno_device *device, unsigned int timeout);

int adreno_wait_idle(struct kgsl_device *device);



int adreno_set_constraint(struct kgsl_device *device,

				struct kgsl_context *context,

				struct kgsl_device_constraint *constraint);



void adreno_snapshot(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot,

		struct kgsl_context *context, struct kgsl_context *context_lpac);



int adreno_reset(struct kgsl_device *device, int fault);



void adreno_fault_skipcmd_detached(struct adreno_device *adreno_dev,

					 struct adreno_context *drawctxt,

					 struct kgsl_drawobj *drawobj);



void adreno_hang_int_callback(struct adreno_device *adreno_dev, int bit);

void adreno_cp_callback(struct adreno_device *adreno_dev, int bit);



int adreno_sysfs_init(struct adreno_device *adreno_dev);



void adreno_irqctrl(struct adreno_device *adreno_dev, int state);



long adreno_ioctl_perfcounter_get(struct kgsl_device_private *dev_priv,

	unsigned int cmd, void *data);



long adreno_ioctl_perfcounter_put(struct kgsl_device_private *dev_priv,

	unsigned int cmd, void *data);



bool adreno_gx_is_on(struct adreno_device *adreno_dev);



u64 adreno_read_cx_timer(struct adreno_device *adreno_dev);



/**

 * adreno_active_count_get - Wrapper for target specific active count get

 * @adreno_dev: pointer to the adreno device

 *

 * Increase the active count for the KGSL device and execute slumber exit

 * sequence if this is the first reference. Code paths that need to touch the

 * hardware or wait for the hardware to complete an operation must hold an

 * active count reference until they are finished. The device mutex must be held

 * while calling this function.

 *

 * Return: 0 on success or negative error on failure to wake up the device

 */

int adreno_active_count_get(struct adreno_device *adreno_dev);



/**

 * adreno_active_count_put - Decrement the active count

 * @adreno_dev: pointer to the adreno device

 *

 * Decrease the active or the KGSL device and schedule the idle thread to

 * execute the slumber sequence if there are no remaining references. The

 * device mutex must be held while calling this function.

 */

void adreno_active_count_put(struct adreno_device *adreno_dev);



/**

 * adreno_populate_ctxt_record_size - Populate the context record size

 * @adreno_dev: Pointer to the adreno device structure

 *

 * Return: 0 on success, or an error code on failure.

 */

int adreno_populate_ctxt_record_size(struct adreno_device *adreno_dev);



#define ADRENO_TARGET(_name, _id) \

static inline int adreno_is_##_name(struct adreno_device *adreno_dev) \

{ \

	return (ADRENO_GPUREV(adreno_dev) == (_id)); \

}



static inline int adreno_is_a5xx(struct adreno_device *adreno_dev)

{

	return ADRENO_GPUREV(adreno_dev) >= 500 &&

			ADRENO_GPUREV(adreno_dev) < 600;

}



ADRENO_TARGET(a505, ADRENO_REV_A505)

ADRENO_TARGET(a506, ADRENO_REV_A506)

ADRENO_TARGET(a508, ADRENO_REV_A508)

ADRENO_TARGET(a510, ADRENO_REV_A510)

ADRENO_TARGET(a512, ADRENO_REV_A512)

ADRENO_TARGET(a530, ADRENO_REV_A530)

ADRENO_TARGET(a540, ADRENO_REV_A540)



static inline int adreno_is_a530v2(struct adreno_device *adreno_dev)

{

	return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A530) &&

		(ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 1);

}



static inline int adreno_is_a530v3(struct adreno_device *adreno_dev)

{

	return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A530) &&

		(ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 2);

}



static inline int adreno_is_a505_or_a506(struct adreno_device *adreno_dev)

{

	return ADRENO_GPUREV(adreno_dev) >= 505 &&

			ADRENO_GPUREV(adreno_dev) <= 506;

}



static inline int adreno_is_a6xx(struct adreno_device *adreno_dev)

{

	return (ADRENO_GPUREV(adreno_dev) >= 600 &&

			ADRENO_GPUREV(adreno_dev) <= 702) ||

			ADRENO_GPUREV(adreno_dev) == ADRENO_REV_GEN6_3_26_0;

}



static inline int adreno_is_a642l(struct adreno_device *adreno_dev)

{

	return (adreno_dev->gpucore->compatible &&

		!strcmp(adreno_dev->gpucore->compatible,

		"qcom,adreno-gpu-a642l"));

}



static inline int adreno_is_a660_shima(struct adreno_device *adreno_dev)

{

	return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A660) &&

		(adreno_dev->gpucore->compatible &&

		!strcmp(adreno_dev->gpucore->compatible,

		"qcom,adreno-gpu-a660-shima"));

}



ADRENO_TARGET(a610, ADRENO_REV_A610)

ADRENO_TARGET(a611, ADRENO_REV_A611)

ADRENO_TARGET(a612, ADRENO_REV_A612)

ADRENO_TARGET(a618, ADRENO_REV_A618)

ADRENO_TARGET(a619, ADRENO_REV_A619)

ADRENO_TARGET(a621, ADRENO_REV_A621)

ADRENO_TARGET(a622, ADRENO_REV_A622)

ADRENO_TARGET(a623, ADRENO_REV_A623)

ADRENO_TARGET(a630, ADRENO_REV_A630)

ADRENO_TARGET(a662, ADRENO_REV_A662)

ADRENO_TARGET(a640, ADRENO_REV_A640)

ADRENO_TARGET(a643, ADRENO_REV_A643)

ADRENO_TARGET(a650, ADRENO_REV_A650)

ADRENO_TARGET(a663, ADRENO_REV_A663)

ADRENO_TARGET(a680, ADRENO_REV_A680)

ADRENO_TARGET(gen6_3_26_0, ADRENO_REV_GEN6_3_26_0)

ADRENO_TARGET(a702, ADRENO_REV_A702)

ADRENO_TARGET(a704, ADRENO_REV_A704)



/* A642L and A643 is derived from A660 and shares same logic */

static inline int adreno_is_a660(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A660 || adreno_is_a642l(adreno_dev) ||

		rev == ADRENO_REV_A643 || rev == ADRENO_REV_A662);

}



/*

 * All the derived chipsets from A615 needs to be added to this

 * list such as A616, A618, A619 etc.

 */

static inline int adreno_is_a615_family(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A615 || rev == ADRENO_REV_A616 ||

			rev == ADRENO_REV_A618 || rev == ADRENO_REV_A619);

}



/*

 * Derived GPUs from A640 needs to be added to this list.

 * A640 and A680 belongs to this family.

 */

static inline int adreno_is_a640_family(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A640 || rev == ADRENO_REV_A680);

}



/*

 * Derived GPUs from A650 needs to be added to this list.

 * A650 is derived from A640 but register specs has been

 * changed hence do not belongs to A640 family. A620, A621,

 * A622, A642L, A643, A660, A663, A690 follows the register

 * specs of A650.

 *

 */

static inline int adreno_is_a650_family(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A650 || rev == ADRENO_REV_A620 ||

		rev == ADRENO_REV_A660 || adreno_is_a642l(adreno_dev) ||

		rev == ADRENO_REV_A643 || rev == ADRENO_REV_A662 ||

		rev == ADRENO_REV_A621 || rev == ADRENO_REV_A622 ||

		rev == ADRENO_REV_A663 || rev == ADRENO_REV_A623);

}



static inline int adreno_is_a619_holi(struct adreno_device *adreno_dev)

{

	return of_device_is_compatible(adreno_dev->dev.pdev->dev.of_node,

		"qcom,adreno-gpu-a619-holi");

}



static inline int adreno_is_a620(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A620 || rev == ADRENO_REV_A621 ||

		rev == ADRENO_REV_A622 || rev == ADRENO_REV_A623);

}



static inline int adreno_is_a610_family(struct adreno_device *adreno_dev)

{

	u32 rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A610 || rev == ADRENO_REV_A611);

}



static inline int adreno_is_a612_family(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A612 || rev == ADRENO_REV_GEN6_3_26_0);

}



/*

 * A704 is derived from A702 and shares the same register specs.

 * Derived GPUs from A702 need to be added to this list.

 */

static inline int adreno_is_a702_family(struct adreno_device *adreno_dev)

{

	u32 rev = ADRENO_GPUREV(adreno_dev);



	return (rev == ADRENO_REV_A702 || rev == ADRENO_REV_A704);

}



static inline int adreno_is_a640v2(struct adreno_device *adreno_dev)

{

	return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A640) &&

		(ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 1);

}



static inline int adreno_is_gen7(struct adreno_device *adreno_dev)

{

	return ADRENO_GPUREV(adreno_dev) >= 0x070000 &&

			ADRENO_GPUREV(adreno_dev) < 0x080000;

}



static inline int adreno_is_gen8(struct adreno_device *adreno_dev)

{

	return ADRENO_GPUREV(adreno_dev) >= 0x080000 &&

			ADRENO_GPUREV(adreno_dev) < 0x090000;

}



ADRENO_TARGET(gen7_0_0, ADRENO_REV_GEN7_0_0)

ADRENO_TARGET(gen7_0_1, ADRENO_REV_GEN7_0_1)

ADRENO_TARGET(gen7_2_0, ADRENO_REV_GEN7_2_0)

ADRENO_TARGET(gen7_2_1, ADRENO_REV_GEN7_2_1)

ADRENO_TARGET(gen7_3_0, ADRENO_REV_GEN7_3_0)

ADRENO_TARGET(gen7_4_0, ADRENO_REV_GEN7_4_0)

ADRENO_TARGET(gen7_5_0, ADRENO_REV_GEN7_5_0)

ADRENO_TARGET(gen7_6_0, ADRENO_REV_GEN7_6_0)

ADRENO_TARGET(gen7_9_0, ADRENO_REV_GEN7_9_0)

ADRENO_TARGET(gen7_9_1, ADRENO_REV_GEN7_9_1)

ADRENO_TARGET(gen7_13_0, ADRENO_REV_GEN7_13_0)

ADRENO_TARGET(gen7_14_0, ADRENO_REV_GEN7_14_0)

ADRENO_TARGET(gen7_11_0, ADRENO_REV_GEN7_11_0)

ADRENO_TARGET(gen7_15_0, ADRENO_REV_GEN7_15_0)

ADRENO_TARGET(gen7_17_0, ADRENO_REV_GEN7_17_0)

ADRENO_TARGET(gen8_0_0, ADRENO_REV_GEN8_0_0)

ADRENO_TARGET(gen8_0_1, ADRENO_REV_GEN8_0_1)

ADRENO_TARGET(gen8_1_0, ADRENO_REV_GEN8_1_0)

ADRENO_TARGET(gen8_2_0, ADRENO_REV_GEN8_2_0)

ADRENO_TARGET(gen8_2_1, ADRENO_REV_GEN8_2_1)

ADRENO_TARGET(gen8_3_0, ADRENO_REV_GEN8_3_0)

ADRENO_TARGET(gen8_4_0, ADRENO_REV_GEN8_4_0)

ADRENO_TARGET(gen8_5_0, ADRENO_REV_GEN8_5_0)

ADRENO_TARGET(gen8_6_0, ADRENO_REV_GEN8_6_0)

ADRENO_TARGET(gen8_8_0, ADRENO_REV_GEN8_8_0)

ADRENO_TARGET(gen8_9_0, ADRENO_REV_GEN8_9_0)

ADRENO_TARGET(gen8_10_0, ADRENO_REV_GEN8_10_0)

ADRENO_TARGET(gen8_11_0, ADRENO_REV_GEN8_11_0)

ADRENO_TARGET(gen8_11_1, ADRENO_REV_GEN8_11_1)

ADRENO_TARGET(gen8_14_0, ADRENO_REV_GEN8_14_0)



static inline int adreno_is_gen7_9_x(struct adreno_device *adreno_dev)

{

	return adreno_is_gen7_9_0(adreno_dev) || adreno_is_gen7_9_1(adreno_dev);

}



static inline int adreno_is_gen7_0_x_family(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 inline int adreno_is_gen7_14_0_family(struct adreno_device *adreno_dev)

{

	return adreno_is_gen7_14_0(adreno_dev) || adreno_is_gen7_17_0(adreno_dev);

}



static inline int adreno_is_gen7_2_x_family(struct adreno_device *adreno_dev)

{

	return adreno_is_gen7_2_0(adreno_dev) || adreno_is_gen7_2_1(adreno_dev) ||

		adreno_is_gen7_5_0(adreno_dev) || adreno_is_gen7_6_0(adreno_dev) ||

		adreno_is_gen7_9_x(adreno_dev) || adreno_is_gen7_14_0_family(adreno_dev) ||

		adreno_is_gen7_11_0(adreno_dev) || adreno_is_gen7_15_0(adreno_dev) ||

		adreno_is_gen7_13_0(adreno_dev);

}



static inline int adreno_is_gen8_11_x(struct adreno_device *adreno_dev)

{

	return adreno_is_gen8_11_0(adreno_dev) || adreno_is_gen8_11_1(adreno_dev) ||

		adreno_is_gen8_14_0(adreno_dev);

}



static inline int adreno_is_gen8_2_x(struct adreno_device *adreno_dev)

{

	return adreno_is_gen8_2_0(adreno_dev) || adreno_is_gen8_2_1(adreno_dev) ||

		adreno_is_gen8_5_0(adreno_dev) || adreno_is_gen8_9_0(adreno_dev);

}



static inline int adreno_is_gen8_0_x_family(struct adreno_device *adreno_dev)

{

	return adreno_is_gen8_0_0(adreno_dev) || adreno_is_gen8_0_1(adreno_dev) ||

		adreno_is_gen8_4_0(adreno_dev) || adreno_is_gen8_6_0(adreno_dev);

}



/* Gen7 targets which does not support concurrent binning */

static inline int adreno_is_gen7_no_cb_family(struct adreno_device *adreno_dev)

{

	return adreno_is_gen7_14_0_family(adreno_dev) || adreno_is_gen7_3_0(adreno_dev);

}



/*

 * adreno_checkreg_off() - Checks the validity of a register enum

 * @adreno_dev: Pointer to adreno device

 * @offset_name: The register enum that is checked

 */

static inline bool adreno_checkreg_off(struct adreno_device *adreno_dev,

					enum adreno_regs offset_name)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (offset_name >= ADRENO_REG_REGISTER_MAX ||

		gpudev->reg_offsets[offset_name] == ADRENO_REG_UNUSED)

		return false;



	/*

	 * GPU register programming is kept common as much as possible

	 * across the cores, Use ADRENO_REG_SKIP when certain register

	 * programming needs to be skipped for certain GPU cores.

	 * Example: Certain registers on a5xx like IB1_BASE are 64 bit.

	 * Common programming programs 64bit register but upper 32 bits

	 * are skipped in a5xx using ADRENO_REG_SKIP.

	 */

	if (gpudev->reg_offsets[offset_name] == ADRENO_REG_SKIP)

		return false;



	return true;

}



/*

 * adreno_readreg() - Read a register by getting its offset from the

 * offset array defined in gpudev node

 * @adreno_dev: Pointer to the adreno device

 * @offset_name: The register enum that is to be read

 * @val: Register value read is placed here

 */

static inline void adreno_readreg(struct adreno_device *adreno_dev,

				enum adreno_regs offset_name, unsigned int *val)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (adreno_checkreg_off(adreno_dev, offset_name))

		kgsl_regread(KGSL_DEVICE(adreno_dev),

				gpudev->reg_offsets[offset_name], val);

	else

		*val = 0;

}



/*

 * adreno_writereg() - Write a register by getting its offset from the

 * offset array defined in gpudev node

 * @adreno_dev: Pointer to the adreno device

 * @offset_name: The register enum that is to be written

 * @val: Value to write

 */

static inline void adreno_writereg(struct adreno_device *adreno_dev,

				enum adreno_regs offset_name, unsigned int val)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (adreno_checkreg_off(adreno_dev, offset_name))

		kgsl_regwrite(KGSL_DEVICE(adreno_dev),

				gpudev->reg_offsets[offset_name], val);

}



/*

 * adreno_getreg() - Returns the offset value of a register from the

 * register offset array in the gpudev node

 * @adreno_dev: Pointer to the adreno device

 * @offset_name: The register enum whore offset is returned

 */

static inline unsigned int adreno_getreg(struct adreno_device *adreno_dev,

				enum adreno_regs offset_name)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (!adreno_checkreg_off(adreno_dev, offset_name))

		return ADRENO_REG_REGISTER_MAX;

	return gpudev->reg_offsets[offset_name];

}



/*

 * adreno_write_gmureg() - Write a GMU register by getting its offset from the

 * offset array defined in gpudev node

 * @adreno_dev: Pointer to the adreno device

 * @offset_name: The register enum that is to be written

 * @val: Value to write

 */

static inline void adreno_write_gmureg(struct adreno_device *adreno_dev,

				enum adreno_regs offset_name, unsigned int val)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (adreno_checkreg_off(adreno_dev, offset_name))

		gmu_core_regwrite(KGSL_DEVICE(adreno_dev),

				gpudev->reg_offsets[offset_name], val);

}



/**

 * adreno_gpu_fault() - Return the current state of the GPU

 * @adreno_dev: A pointer to the adreno_device to query

 *

 * Return 0 if there is no fault or positive with the last type of fault that

 * occurred

 */

static inline unsigned int adreno_gpu_fault(struct adreno_device *adreno_dev)

{

	/* make sure we're reading the latest value */

	smp_rmb();

	return atomic_read(&adreno_dev->scheduler_fault);

}



/**

 * adreno_set_gpu_fault() - Set the current fault status of the GPU

 * @adreno_dev: A pointer to the adreno_device to set

 * @state: fault state to set

 *

 */

static inline void adreno_set_gpu_fault(struct adreno_device *adreno_dev,

	int state)

{

	/* only set the fault bit w/o overwriting other bits */

	atomic_or(state, &adreno_dev->scheduler_fault);



	/* make sure other CPUs see the update */

	smp_wmb();

}



/**

 * adreno_clear_gpu_fault() - Clear the GPU fault register

 * @adreno_dev: A pointer to an adreno_device structure

 *

 * Clear the GPU fault status for the adreno device

 */

static inline void adreno_clear_gpu_fault(struct adreno_device *adreno_dev)

{

	atomic_set(&adreno_dev->scheduler_fault, 0);



	/* make sure other CPUs see the update */

	smp_wmb();

}



/**

 * adreno_scheduler_queue - Queue the scheduler kthread

 * @adreno_dev: Adreno device handle

 */

static inline void adreno_scheduler_queue(struct adreno_device *adreno_dev)

{

	kthread_queue_work(adreno_dev->scheduler_worker, &adreno_dev->scheduler_work);

}



/**

 * adreno_scheduler_fault() - Set GPU fault and trigger fault recovery

 * @adreno_dev: A pointer to an adreno_device structure

 * @fault: Type of fault

 */

static inline void adreno_scheduler_fault(struct adreno_device *adreno_dev,

		u32 fault)

{

	adreno_set_gpu_fault(adreno_dev, fault);

	adreno_scheduler_queue(adreno_dev);

}



/**

 * adreno_gpu_halt() - Return the GPU halt refcount

 * @adreno_dev: A pointer to the adreno_device

 */

static inline int adreno_gpu_halt(struct adreno_device *adreno_dev)

{

	/* make sure we're reading the latest value */

	smp_rmb();

	return atomic_read(&adreno_dev->halt);

}





/**

 * adreno_clear_gpu_halt() - Clear the GPU halt refcount

 * @adreno_dev: A pointer to the adreno_device

 */

static inline void adreno_clear_gpu_halt(struct adreno_device *adreno_dev)

{

	atomic_set(&adreno_dev->halt, 0);



	/* make sure other CPUs see the update */

	smp_wmb();

}



/**

 * adreno_get_gpu_halt() - Increment GPU halt refcount

 * @adreno_dev: A pointer to the adreno_device

 */

static inline void adreno_get_gpu_halt(struct adreno_device *adreno_dev)

{

	atomic_inc(&adreno_dev->halt);

}



/**

 * adreno_put_gpu_halt() - Decrement GPU halt refcount

 * @adreno_dev: A pointer to the adreno_device

 */

static inline void adreno_put_gpu_halt(struct adreno_device *adreno_dev)

{

	/* Make sure the refcount is good */

	int ret = atomic_dec_if_positive(&adreno_dev->halt);



	WARN(ret < 0, "GPU halt refcount unbalanced\n");

}





#ifdef CONFIG_DEBUG_FS

void adreno_debugfs_init(struct adreno_device *adreno_dev);

void adreno_context_debugfs_init(struct adreno_device *adreno_dev,

				struct adreno_context *ctx);

#else

static inline void adreno_debugfs_init(struct adreno_device *adreno_dev) { }

static inline void adreno_context_debugfs_init(struct adreno_device *device,

						struct adreno_context *context)

{

	context->debug_root = NULL;

}

#endif



/**

 * adreno_compare_pm4_version() - Compare the PM4 microcode version

 * @adreno_dev: Pointer to the adreno_device struct

 * @version: Version number to compare again

 *

 * Compare the current version against the specified version and return -1 if

 * the current code is older, 0 if equal or 1 if newer.

 */

static inline int adreno_compare_pm4_version(struct adreno_device *adreno_dev,

	unsigned int version)

{

	if (adreno_dev->fw[ADRENO_FW_PM4].version == version)

		return 0;



	return (adreno_dev->fw[ADRENO_FW_PM4].version > version) ? 1 : -1;

}



/**

 * adreno_compare_pfp_version() - Compare the PFP microcode version

 * @adreno_dev: Pointer to the adreno_device struct

 * @version: Version number to compare against

 *

 * Compare the current version against the specified version and return -1 if

 * the current code is older, 0 if equal or 1 if newer.

 */

static inline int adreno_compare_pfp_version(struct adreno_device *adreno_dev,

	unsigned int version)

{

	if (adreno_dev->fw[ADRENO_FW_PFP].version == version)

		return 0;



	return (adreno_dev->fw[ADRENO_FW_PFP].version > version) ? 1 : -1;

}



/**

 * adreno_in_preempt_state() - Check if preemption state is equal to given state

 * @adreno_dev: Device whose preemption state is checked

 * @state: State to compare against

 */

static inline bool adreno_in_preempt_state(struct adreno_device *adreno_dev,

			enum adreno_preempt_states state)

{

	return atomic_read(&adreno_dev->preempt.state) == state;

}

/**

 * adreno_set_preempt_state() - Set the specified preemption state

 * @adreno_dev: Device to change preemption state

 * @state: State to set

 */

static inline void adreno_set_preempt_state(struct adreno_device *adreno_dev,

		enum adreno_preempt_states state)

{

	/*

	 * atomic_set doesn't use barriers, so we need to do it ourselves.  One

	 * before...

	 */

	smp_wmb();

	atomic_set(&adreno_dev->preempt.state, state);



	/* ... and one after */

	smp_wmb();

}



static inline bool adreno_is_preemption_enabled(

				struct adreno_device *adreno_dev)

{

	return test_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);

}



/**

 * adreno_is_fast_context_destroy_enabled() - Check whether the GMU fast context

 * destroy optimization is statically enabled and if the GMU supports the

 * capability.

 * @adreno_dev: Pointer to the adreno_device struct

 */

static inline bool adreno_is_fast_context_destroy_enabled(

				struct adreno_device *adreno_dev)

{

	return test_bit(ADRENO_DEVICE_FAST_CONTEXT_DESTROY, &adreno_dev->priv);

}





/**

 * adreno_preemption_feature_set() - Check whether adreno preemption feature is statically enabled

 * either via adreno feature bit, or via the cmdline override

 * @adreno_dev: Device whose preemption state is checked

 */

static inline bool adreno_preemption_feature_set(struct adreno_device *adreno_dev)

{

	return ADRENO_FEATURE(adreno_dev, ADRENO_PREEMPTION) || adreno_dev->preempt_override;

}



/*

 * adreno_compare_prio_level() - Compares 2 priority levels based on enum values

 * @p1: First priority level

 * @p2: Second priority level

 *

 * Returns greater than 0 if p1 is higher priority, 0 if levels are equal else

 * less than 0

 */

static inline int adreno_compare_prio_level(int p1, int p2)

{

	return p2 - p1;

}



void adreno_readreg64(struct adreno_device *adreno_dev,

		enum adreno_regs lo, enum adreno_regs hi, uint64_t *val);



void adreno_writereg64(struct adreno_device *adreno_dev,

		enum adreno_regs lo, enum adreno_regs hi, uint64_t val);



unsigned int adreno_get_rptr(struct adreno_ringbuffer *rb);



void adreno_touch_wake(struct kgsl_device *device);



static inline bool adreno_rb_empty(struct adreno_ringbuffer *rb)

{

	return (adreno_get_rptr(rb) == rb->wptr);

}



static inline bool adreno_long_ib_detect(struct adreno_device *adreno_dev)

{

	return adreno_dev->long_ib_detect &&

		!test_bit(ADRENO_DEVICE_ISDB_ENABLED, &adreno_dev->priv);

}



/**

 * adreno_support_64bit - Return true if the GPU supports 64 bit addressing

 * @adreno_dev: An Adreno GPU device handle

 *

 * Return: True if the device supports 64 bit addressing

 */

static inline bool adreno_support_64bit(struct adreno_device *adreno_dev)

{

	/*

	 * The IOMMU API takes a unsigned long for the iova so we can't support

	 * 64 bit addresses when the kernel is in 32 bit mode even if we wanted

	 * so we need to check that we are using a5xx or newer and that the

	 * unsigned long is big enough for our purposes.

	 */

	return (BITS_PER_LONG > 32 && ADRENO_GPUREV(adreno_dev) >= 500);

}



static inline void adreno_ringbuffer_set_pagetable(struct kgsl_device *device,

	struct adreno_ringbuffer *rb, struct kgsl_pagetable *pt)

{

	unsigned long flags;



	spin_lock_irqsave(&rb->preempt_lock, flags);



	kgsl_sharedmem_writel(device->scratch,

		SCRATCH_RB_OFFSET(rb->id, current_rb_ptname), pt->name);



	kgsl_sharedmem_writeq(device->scratch,

		SCRATCH_RB_OFFSET(rb->id, ttbr0),

		kgsl_mmu_pagetable_get_ttbr0(pt));



	kgsl_sharedmem_writel(device->scratch,

		SCRATCH_RB_OFFSET(rb->id, contextidr), 0);



	spin_unlock_irqrestore(&rb->preempt_lock, flags);

}



static inline u32 counter_delta(struct kgsl_device *device,

			unsigned int reg, unsigned int *counter)

{

	u32 val, ret = 0;



	if (!reg)

		return 0;



	kgsl_regread(device, reg, &val);



	if (*counter) {

		if (val >= *counter)

			ret = val - *counter;

		else

			ret = (UINT_MAX - *counter) + val;

	}



	*counter = val;

	return ret;

}



static inline int adreno_perfcntr_active_oob_get(

	struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret = adreno_active_count_get(adreno_dev);



	if (!ret) {

		ret = gmu_core_dev_oob_set(device, oob_perfcntr);

		if (ret)

			adreno_active_count_put(adreno_dev);

	}



	return ret;

}



static inline void adreno_perfcntr_active_oob_put(

	struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	gmu_core_dev_oob_clear(device, oob_perfcntr);

	adreno_active_count_put(adreno_dev);

}



/**

 * adreno_wait_for_halt_ack - wait for acknowlegement for a bus halt request

 * @ack_reg: register offset to wait for acknowledge

 * @mask: A mask value to wait for

 *

 * Return: 0 on success or -ETIMEDOUT if the request timed out

 */

static inline int adreno_wait_for_halt_ack(struct kgsl_device *device,

	int ack_reg, unsigned int mask)

{

	u32 val;

	int ret = kgsl_regmap_read_poll_timeout(&device->regmap, ack_reg,

		val, (val & mask) == mask, 100, 100 * 1000);



	if (ret)

		dev_err(device->dev,

			"GBIF/VBIF Halt ack timeout: reg=%08x mask=%08x status=%08x\n",

			ack_reg, mask, val);



	return ret;

}



/**

 * adreno_move_preempt_state - Update the preemption state

 * @adreno_dev: An Adreno GPU device handle

 * @old: The current state of the preemption

 * @new: The new state of the preemption

 *

 * Return: True if the state was updated or false if not

 */

static inline bool adreno_move_preempt_state(struct adreno_device *adreno_dev,

	enum adreno_preempt_states old, enum adreno_preempt_states new)

{

	return (atomic_cmpxchg(&adreno_dev->preempt.state, old, new) == old);

}



/**

 * adreno_reg_offset_init - Helper function to initialize reg_offsets

 * @reg_offsets: Pointer to an array of register offsets

 *

 * Helper function to setup register_offsets for a target. Go through

 * and set ADRENO_REG_UNUSED for all unused entries in the list.

 */

static inline void adreno_reg_offset_init(u32 *reg_offsets)

{

	int i;



	/*

	 * Initialize uninitialzed gpu registers, only needs to be done once.

	 * Make all offsets that are not initialized to ADRENO_REG_UNUSED

	 */

	for (i = 0; i < ADRENO_REG_REGISTER_MAX; i++) {

		if (!reg_offsets[i])

			reg_offsets[i] = ADRENO_REG_UNUSED;

	}

}



static inline u32 adreno_get_level(struct kgsl_context *context)

{

	u32 level;



	if (kgsl_context_is_lpac(context))

		return KGSL_LPAC_RB_ID;



	level = context->priority / KGSL_PRIORITY_MAX_RB_LEVELS;



	return min_t(u32, level, KGSL_PRIORITY_MAX_RB_LEVELS - 1);

}



/**

 * adreno_request_firmware - Helper function for requesting firmware

 * @fw: Pointer to firmware image

 * @name: Name of firmware file

 * @device: Device for which firmware is being loaded

 * @log_error: If true, logs an error message on failure

 *

 * Request for firmware with the name passed. If firmware is not

 * available at /lib/firmware/, try /lib/firmware/qcom path by

 * adding qcom/ prefix to the firmware name.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_request_firmware(const struct firmware **fw,

		const char *name, struct device *device, bool log_error);



/**

 * adreno_get_firwmare - Load firmware into a adreno_firmware struct

 * @adreno_dev: An Adreno GPU device handle

 * @fwfile: Firmware file to load

 * @firmware: A &struct adreno_firmware container for the firmware.

 *

 * Load the specified firmware file into the memdesc in &struct adreno_firmware

 * and get the size and version from the data.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_get_firmware(struct adreno_device *adreno_dev,

		const char *fwfile, struct adreno_firmware *firmware);

/**

 * adreno_zap_shader_load - Helper function for loading the zap shader

 * adreno_dev: A handle to an Adreno GPU device

 * name: Name of the zap shader to load

 *

 * A target indepedent helper function for loading the zap shader.

 *

 * Return: 0 on success or negative on failure.

 */

int adreno_zap_shader_load(struct adreno_device *adreno_dev,

		const char *name);



/**

 * adreno_irq_callbacks - Helper function to handle IRQ callbacks

 * @adreno_dev: Adreno GPU device handle

 * @funcs: List of callback functions

 * @status: Interrupt status

 *

 * Walk the bits in the interrupt status and call any applicable callbacks.

 * Return: IRQ_HANDLED if one or more interrupt callbacks were called.

 */

irqreturn_t adreno_irq_callbacks(struct adreno_device *adreno_dev,

		const struct adreno_irq_funcs *funcs, u32 status);





/**

 * adreno_device_probe - Generic adreno device probe function

 * @pdev: Pointer to the platform device

 * @adreno_dev: Adreno GPU device handle

 *

 * Do the generic setup for the Adreno device. Called from the target specific

 * probe functions.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_device_probe(struct platform_device *pdev,

		struct adreno_device *adreno_dev);



/**

 * adreno_power_cycle - Suspend and resume the device

 * @adreno_dev: Pointer to the adreno device

 * @callback: Function that needs to be executed

 * @priv: Argument to be passed to the callback

 *

 * Certain properties that can be set via sysfs need to power

 * cycle the device to take effect. This function suspends

 * the device, executes the callback, and resumes the device.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_power_cycle(struct adreno_device *adreno_dev,

	void (*callback)(struct adreno_device *adreno_dev, void *priv),

	void *priv);



/**

 * adreno_power_cycle_bool - Power cycle the device to change device setting

 * @adreno_dev: Pointer to the adreno device

 * @flag: Flag that needs to be set

 * @val: The value flag should be set to

 *

 * Certain properties that can be set via sysfs need to power cycle the device

 * to take effect. This function suspends the device, sets the flag, and

 * resumes the device.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_power_cycle_bool(struct adreno_device *adreno_dev,

	bool *flag, bool val);



/**

 * adreno_power_cycle_u32 - Power cycle the device to change device setting

 * @adreno_dev: Pointer to the adreno device

 * @flag: Flag that needs to be set

 * @val: The value flag should be set to

 *

 * Certain properties that can be set via sysfs need to power cycle the device

 * to take effect. This function suspends the device, sets the flag, and

 * resumes the device.

 *

 * Return: 0 on success or negative on failure

 */

int adreno_power_cycle_u32(struct adreno_device *adreno_dev,

	u32 *flag, u32 val);



/**

 * adreno_set_active_ctxs_null - Give up active context refcount

 * @adreno_dev: Adreno GPU device handle

 *

 * This puts back the reference for that last active context on

 * each ringbuffer when going in and out of slumber.

 */

void adreno_set_active_ctxs_null(struct adreno_device *adreno_dev);



/**

 * adreno_get_bus_counters - Allocate the bus dcvs counters

 * @adreno_dev: Adreno GPU device handle

 *

 * This function allocates the various gpu counters to measure

 * gpu bus usage for bus dcvs

 */

void adreno_get_bus_counters(struct adreno_device *adreno_dev);



/**

 * adreno_gmu_bus_ab_quantize - Calculate the AB vote that needs to be sent to GMU

 * @adreno_dev: Handle to the adreno device

 * @ab: ab request that needs to be scaled in MBps

 *

 * Returns the AB value that needs to be prefixed to bandwidth vote in kbps

 */

u32 adreno_gmu_bus_ab_quantize(struct adreno_device *adreno_dev, u32 ab);



/**

 * adreno_check_idle - Make sure device is idle

 * @device: Pointer to the kgsl device

 *

 * This function processes the profiling results and log an error if

 * device is not idle

 */

void adreno_check_idle(struct kgsl_device *device);



/*

 * adreno_profile_submit_time - Populate profiling buffer with timestamps

 * @time: Container for the statistics

 *

 * Populate the draw object user profiling buffer with the timestamps

 * recored in the adreno_submit_time structure at the time of draw object

 * submission.

 */

void adreno_profile_submit_time(struct adreno_submit_time *time);



void adreno_preemption_timer(struct timer_list *t);



/**

 * adreno_create_profile_buffer - Create a buffer to store profiling data

 * @adreno_dev: Adreno GPU device handle

 */

void adreno_create_profile_buffer(struct adreno_device *adreno_dev);



/**

 * adreno_isidle - return true if the hardware is idle

 * @adreno_dev: Adreno GPU device handle

 *

 * Return: True if the hardware is idle

 */

bool adreno_isidle(struct adreno_device *adreno_dev);



/**

 * adreno_allocate_global - Helper function to allocate a global GPU object

 * @device: A GPU device handle

 * @memdesc: Pointer to a &struct kgsl_memdesc pointer

 * @size: Size of the allocation in bytes

 * @padding: Amount of extra adding to add to the VA allocation

 * @flags: Control flags for the allocation

 * @priv: Internal flags for the allocation

 * @name: Name of the allocation (for the debugfs file)

 *

 * Allocate a global object if it hasn't already been alllocated and put it in

 * the pointer pointed to by @memdesc.

 * Return: 0 on success or negative on error

 */

static inline int adreno_allocate_global(struct kgsl_device *device,

		struct kgsl_memdesc **memdesc, u64 size, u32 padding, u64 flags,

		u32 priv, const char *name)

{

	if (!IS_ERR_OR_NULL(*memdesc))

		return 0;



	*memdesc = kgsl_allocate_global(device, size, padding, flags, priv, name);

	return PTR_ERR_OR_ZERO(*memdesc);

}



static inline void adreno_set_dispatch_ops(struct adreno_device *adreno_dev,

		const struct adreno_dispatch_ops *ops)

{

	adreno_dev->dispatch_ops = ops;

}



#ifdef CONFIG_QCOM_KGSL_FENCE_TRACE

/**

 * adreno_fence_trace_array_init - Initialize an always on trace array

 * @device: A GPU device handle

 *

 * Register an always-on trace array to for fence timeout debugging

 */

void adreno_fence_trace_array_init(struct kgsl_device *device);

#else

static inline void adreno_fence_trace_array_init(struct kgsl_device *device) {}

#endif



/*

 * adreno_drawobj_set_constraint - Set a power constraint

 * @device: Pointer to a KGSL device structure

 * @drawobj: Draw object for which constraint is to be set

 *

 * Set the power constraint if requested by this context

 */

void adreno_drawobj_set_constraint(struct kgsl_device *device,

		struct kgsl_drawobj *drawobj);



/**

 * adreno_get_gpu_model - Gets gpu model name from device tree (or) chipid

 * @device: A GPU device handle

 *

 * Return: GPU model name string

 */

const char *adreno_get_gpu_model(struct kgsl_device *device);



int adreno_verify_cmdobj(struct kgsl_device_private *dev_priv,

		struct kgsl_context *context, struct kgsl_drawobj *drawobj[],

		uint32_t count);



/**

 * adreno_smmu_is_stalled() - Check whether smmu is stalled or not

 * @device: Pointer to adreno device

 *

 * Return - True if smmu is stalled or false otherwise

 */

bool adreno_smmu_is_stalled(struct adreno_device *adreno_dev);



/**

 * adreno_get_ahb_timeout_val() - Get the ahb_timeout value

 * @adreno_dev: Adreno device handle

 * @noc_timeout_us: GPU config NOC timeout value in usec

 *

 * Return - AHB timeout value to be programmed in AHB CNTL registers

 */

u32 adreno_get_ahb_timeout_val(struct adreno_device *adreno_dev, u32 noc_timeout_us);



/**

 * adreno_llcc_slice_deactivate - Deactivate GPU and GPUHTW llcc slices

 * @adreno_dev: Adreno device handle

 */

static inline void adreno_llcc_slice_deactivate(struct adreno_device *adreno_dev)

{

	if (adreno_dev->gpu_llc_slice_enable && !IS_ERR_OR_NULL(adreno_dev->gpu_llc_slice))

		llcc_slice_deactivate(adreno_dev->gpu_llc_slice);



	if (adreno_dev->gpuhtw_llc_slice_enable && !IS_ERR_OR_NULL(adreno_dev->gpuhtw_llc_slice))

		llcc_slice_deactivate(adreno_dev->gpuhtw_llc_slice);



	if (adreno_dev->gpumv_llc_slice_enable && !IS_ERR_OR_NULL(adreno_dev->gpumv_llc_slice))

		llcc_slice_deactivate(adreno_dev->gpumv_llc_slice);

}



/**

 * adreno_gpufault_stats - Update GPU fault statistics

 * @adreno_dev: Adreno device handle

 * @drawobj: GC drawobj that caused the fault

 * @drawobj_lpac: LPAC drawobj that caused the fault

 * @fault: Fault type

 *

 * Update statistics about GPU faults

 */

void adreno_gpufault_stats(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj, struct kgsl_drawobj *drawobj_lpac, int fault);





/**

 * adreno_drain - Halt new submissions and drain pending work by waiting for active count to become

 * zero

 * @device: A handle to kgsl device

 * @wait_jiffies: jiffies to wait for active count to become zero

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_drain(struct kgsl_device *device, unsigned long wait_jiffies);



/**

 * adreno_irq_free - Free an interrupt allocated for GPU

 * @adreno_dev: Adreno device handle

 */

static inline void adreno_irq_free(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!(adreno_dev->irq_mask || device->pwrctrl.interrupt_num))

		return;



	devm_free_irq(&device->pdev->dev, device->pwrctrl.interrupt_num, device);

	adreno_dev->irq_mask = 0;

	device->pwrctrl.interrupt_num = 0;

}



/**

 * adreno_gpudev_reset - Adreno gpu device reset

 * @adreno_dev: Adreno device handle

 */

static inline int adreno_gpudev_reset(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	int ret = -ENODEV;



	if (gpudev->reset) {

		set_bit(ADRENO_DEVICE_RESET_RECOVERY, &adreno_dev->priv);

		ret = gpudev->reset(adreno_dev);

		clear_bit(ADRENO_DEVICE_RESET_RECOVERY, &adreno_dev->priv);

	}



	return ret;

}



/**

 * adreno_gmem_size - Returns the GMEM size

 * @adreno_dev Adreno device handle

 *

 * Return: Size of GMEM in bytes

 */

static inline u32 adreno_gmem_size(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->get_gmem_size)

		return gpudev->get_gmem_size(adreno_dev);



	return adreno_dev->gpucore->gmem_size;

}

#endif /*__ADRENO_H */





==========================================================================================
FILE: adreno_a6xx.c
LINES: 2534 | BYTES: 78615
==========================================================================================

// SPDX-License-Identifier: GPL-2.0-only

/*

 * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.

 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.

 */



#include <linux/clk/qcom.h>

#include <linux/clk-provider.h>

#include <linux/io.h>

#include <linux/of.h>

#include <linux/of_fdt.h>

#include <linux/of_device.h>

#include <soc/qcom/of_common.h>

#include <linux/version.h>

#if (KERNEL_VERSION(6, 17, 0) <= LINUX_VERSION_CODE)

#include <linux/soc/qcom/ubwc.h>

#endif



#include "adreno.h"

#include "adreno_a6xx.h"

#include "adreno_a6xx_hwsched.h"

#include "adreno_pm4types.h"

#include "adreno_trace.h"

#include "kgsl_gmu_core.h"

#include "kgsl_trace.h"

#include "kgsl_util.h"



/* IFPC & Preemption static powerup restore list */

static u32 a6xx_pwrup_reglist[] = {

	A6XX_VSC_ADDR_MODE_CNTL,

	A6XX_GRAS_ADDR_MODE_CNTL,

	A6XX_RB_ADDR_MODE_CNTL,

	A6XX_PC_ADDR_MODE_CNTL,

	A6XX_HLSQ_ADDR_MODE_CNTL,

	A6XX_VFD_ADDR_MODE_CNTL,

	A6XX_VPC_ADDR_MODE_CNTL,

	A6XX_UCHE_ADDR_MODE_CNTL,

	A6XX_SP_ADDR_MODE_CNTL,

	A6XX_TPL1_ADDR_MODE_CNTL,

	A6XX_UCHE_WRITE_RANGE_MAX_LO,

	A6XX_UCHE_WRITE_RANGE_MAX_HI,

	A6XX_UCHE_TRAP_BASE_LO,

	A6XX_UCHE_TRAP_BASE_HI,

	A6XX_UCHE_WRITE_THRU_BASE_LO,

	A6XX_UCHE_WRITE_THRU_BASE_HI,

	A6XX_UCHE_GMEM_RANGE_MIN_LO,

	A6XX_UCHE_GMEM_RANGE_MIN_HI,

	A6XX_UCHE_GMEM_RANGE_MAX_LO,

	A6XX_UCHE_GMEM_RANGE_MAX_HI,

	A6XX_UCHE_FILTER_CNTL,

	A6XX_UCHE_CACHE_WAYS,

	A6XX_UCHE_MODE_CNTL,

	A6XX_RB_NC_MODE_CNTL,

	A6XX_TPL1_NC_MODE_CNTL,

	A6XX_SP_NC_MODE_CNTL,

	A6XX_PC_DBG_ECO_CNTL,

	A6XX_RB_CONTEXT_SWITCH_GMEM_SAVE_RESTORE,

	A6XX_UCHE_GBIF_GX_CONFIG,

	A6XX_UCHE_CLIENT_PF,

};



/* IFPC only static powerup restore list */

static u32 a6xx_ifpc_pwrup_reglist[] = {

	A6XX_CP_CHICKEN_DBG,

	A6XX_CP_DBG_ECO_CNTL,

	A6XX_CP_PROTECT_CNTL,

	A6XX_CP_PROTECT_REG,

	A6XX_CP_PROTECT_REG+1,

	A6XX_CP_PROTECT_REG+2,

	A6XX_CP_PROTECT_REG+3,

	A6XX_CP_PROTECT_REG+4,

	A6XX_CP_PROTECT_REG+5,

	A6XX_CP_PROTECT_REG+6,

	A6XX_CP_PROTECT_REG+7,

	A6XX_CP_PROTECT_REG+8,

	A6XX_CP_PROTECT_REG+9,

	A6XX_CP_PROTECT_REG+10,

	A6XX_CP_PROTECT_REG+11,

	A6XX_CP_PROTECT_REG+12,

	A6XX_CP_PROTECT_REG+13,

	A6XX_CP_PROTECT_REG+14,

	A6XX_CP_PROTECT_REG+15,

	A6XX_CP_PROTECT_REG+16,

	A6XX_CP_PROTECT_REG+17,

	A6XX_CP_PROTECT_REG+18,

	A6XX_CP_PROTECT_REG+19,

	A6XX_CP_PROTECT_REG+20,

	A6XX_CP_PROTECT_REG+21,

	A6XX_CP_PROTECT_REG+22,

	A6XX_CP_PROTECT_REG+23,

	A6XX_CP_PROTECT_REG+24,

	A6XX_CP_PROTECT_REG+25,

	A6XX_CP_PROTECT_REG+26,

	A6XX_CP_PROTECT_REG+27,

	A6XX_CP_PROTECT_REG+28,

	A6XX_CP_PROTECT_REG+29,

	A6XX_CP_PROTECT_REG+30,

	A6XX_CP_PROTECT_REG+31,

	A6XX_CP_AHB_CNTL,

};



/* Applicable to a620, a621, a622, a642l, a643, a650 and a660 */

static u32 a650_ifpc_pwrup_reglist[] = {

	A6XX_CP_PROTECT_REG+32,

	A6XX_CP_PROTECT_REG+33,

	A6XX_CP_PROTECT_REG+34,

	A6XX_CP_PROTECT_REG+35,

	A6XX_CP_PROTECT_REG+36,

	A6XX_CP_PROTECT_REG+37,

	A6XX_CP_PROTECT_REG+38,

	A6XX_CP_PROTECT_REG+39,

	A6XX_CP_PROTECT_REG+40,

	A6XX_CP_PROTECT_REG+41,

	A6XX_CP_PROTECT_REG+42,

	A6XX_CP_PROTECT_REG+43,

	A6XX_CP_PROTECT_REG+44,

	A6XX_CP_PROTECT_REG+45,

	A6XX_CP_PROTECT_REG+46,

	A6XX_CP_PROTECT_REG+47,

};



/* Applicable to a620, a621, a622, a642l, a643, a650 and a660 */

static u32 a650_pwrup_reglist[] = {

	A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_0,

	A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_1,

	A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_2,

	A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_3,

	A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_4,

	A6XX_UCHE_CMDQ_CONFIG,

	A6XX_SP_DBG_ECO_CNTL,

};



static u32 a615_pwrup_reglist[] = {

	A6XX_UCHE_GBIF_GX_CONFIG,

};



int a6xx_fenced_write(struct adreno_device *adreno_dev, u32 offset,

		u32 value, u32 mask)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	unsigned int status, i;

	u64 ts1, ts2;



	kgsl_regwrite(device, offset, value);



	if (!gmu_core_isenabled(device))

		return 0;



	ts1 = a6xx_read_alwayson(adreno_dev);

	for (i = 0; i < GMU_CORE_LONG_WAKEUP_RETRY_LIMIT; i++) {

		/*

		 * Make sure the previous register write is posted before

		 * checking the fence status

		 */

		mb();



		kgsl_regread(device, A6XX_GMU_AHB_FENCE_STATUS, &status);



		/*

		 * If !writedropped0/1, then the write to fenced register

		 * was successful

		 */

		if (!(status & mask))

			break;



		/* Wait a small amount of time before trying again */

		udelay(GMU_CORE_WAKEUP_DELAY_US);



		/* Try to write the fenced register again */

		kgsl_regwrite(device, offset, value);

	}



	if (i < GMU_CORE_SHORT_WAKEUP_RETRY_LIMIT)

		return 0;



	if (i == GMU_CORE_LONG_WAKEUP_RETRY_LIMIT) {

		ts2 = a6xx_read_alwayson(adreno_dev);

		dev_err(adreno_dev->dev.dev,

			"Timed out waiting %d usecs to write fenced register 0x%x, timestamps: %llx %llx\n",

			i * GMU_CORE_WAKEUP_DELAY_US, offset, ts1, ts2);

		return -ETIMEDOUT;

	}



	dev_err(adreno_dev->dev.dev,

		"Waited %d usecs to write fenced register 0x%x\n",

		i * GMU_CORE_WAKEUP_DELAY_US, offset);



	return 0;

}



static void a6xx_calc_ubwc_config_legacy(struct adreno_device *adreno_dev)

{

	struct adreno_ubwc_props *ubwc_props = &adreno_dev->ubwc_props;



	switch (adreno_dev->ubwc_mode) {

	case KGSL_UBWC_1_0:

		ubwc_props->mode = 1;

		break;

	case KGSL_UBWC_2_0:

		ubwc_props->mode = 0;

		break;

	case KGSL_UBWC_3_0:

		ubwc_props->mode = 0;

		ubwc_props->amsbc = true;

		break;

	case KGSL_UBWC_4_0:

		ubwc_props->mode = 0;

		ubwc_props->rgb565_predicator = true;

		ubwc_props->amsbc = true;

		if (adreno_is_a663(adreno_dev))

			ubwc_props->level2_swizzling_dis = 1;

		break;

	default:

		break;

	}

}



#if (KERNEL_VERSION(7, 3, 0) <= LINUX_VERSION_CODE)

static void a6xx_calc_ubwc_config(struct adreno_device *adreno_dev)

{

	const struct qcom_ubwc_cfg_data *cfg =

		(const struct qcom_ubwc_cfg_data *)adreno_dev->ubwc_cfg_data;

	struct adreno_ubwc_props *ubwc_props = &adreno_dev->ubwc_props;



	if (!cfg)

		return a6xx_calc_ubwc_config_legacy(adreno_dev);



	ubwc_props->mode = qcom_ubwc_get_ubwc_mode(cfg);

	ubwc_props->amsbc = qcom_ubwc_enable_amsbc(cfg);

	ubwc_props->rgb565_predicator = cfg->ubwc_enc_version >= UBWC_4_0;

	ubwc_props->level2_swizzling_dis =

		!(qcom_ubwc_swizzle(cfg) & UBWC_SWIZZLE_ENABLE_LVL2);

}

#elif (KERNEL_VERSION(6, 17, 0) <= LINUX_VERSION_CODE)

static void a6xx_calc_ubwc_config(struct adreno_device *adreno_dev)

{

	struct qcom_ubwc_cfg_data *cfg = (struct qcom_ubwc_cfg_data *)adreno_dev->ubwc_cfg_data;

	struct adreno_ubwc_props *ubwc_props = &adreno_dev->ubwc_props;



	if (!cfg)

		return a6xx_calc_ubwc_config_legacy(adreno_dev);



	ubwc_props->mode = (cfg->ubwc_enc_version == UBWC_1_0) ? 1 : 0;

	ubwc_props->amsbc = cfg->ubwc_enc_version >= UBWC_3_0;

	ubwc_props->rgb565_predicator = cfg->ubwc_enc_version >= UBWC_4_0;

	ubwc_props->level2_swizzling_dis = !(cfg->ubwc_swizzle & UBWC_SWIZZLE_ENABLE_LVL2);

}

#else

static void a6xx_calc_ubwc_config(struct adreno_device *adreno_dev)

{

	a6xx_calc_ubwc_config_legacy(adreno_dev);

}

#endif



int a6xx_init(struct adreno_device *adreno_dev)

{

	adreno_dev->cooperative_reset = ADRENO_FEATURE(adreno_dev,

							ADRENO_COOP_RESET);



	a6xx_calc_ubwc_config(adreno_dev);



	a6xx_crashdump_init(adreno_dev);



	return adreno_allocate_global(KGSL_DEVICE(adreno_dev),

		&adreno_dev->pwrup_reglist,

		PAGE_SIZE, 0, 0, KGSL_MEMDESC_PRIVILEGED,

		"powerup_register_list");

}



static int a6xx_nogmu_init(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	ret = a6xx_ringbuffer_init(adreno_dev);

	if (ret)

		return ret;



	ret = a6xx_microcode_read(adreno_dev);

	if (ret)

		return ret;



	/* Try to map the GMU wrapper region if applicable */

	ret = kgsl_regmap_add_region(&device->regmap, device->pdev,

		"gmu_wrapper", -EINVAL, NULL, NULL);

	if (ret && ret != -ENODEV)

		dev_err(device->dev, "Couldn't map the GMU wrapper registers\n");



	adreno_create_profile_buffer(adreno_dev);



	return a6xx_init(adreno_dev);

}



static void a6xx_protect_init(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);

	const struct adreno_protected_regs *regs = a6xx_core->protected_regs;

	int i;



	/*

	 * Enable access protection to privileged registers, fault on an access

	 * protect violation and select the last span to protect from the start

	 * address all the way to the end of the register address space

	 */

	kgsl_regwrite(device, A6XX_CP_PROTECT_CNTL,

		(1 << 0) | (1 << 1) | (1 << 3));



	/* Program each register defined by the core definition */

	for (i = 0; regs[i].reg; i++) {

		u32 count;



		/*

		 * This is the offset of the end register as counted from the

		 * start, i.e. # of registers in the range - 1

		 */

		count = regs[i].end - regs[i].start;



		kgsl_regwrite(device, regs[i].reg,

			(regs[i].start & 0x3ffff) | ((count & 0x1fff) << 18) |

			(regs[i].noaccess << 31));

	}

}



static inline unsigned int

__get_rbbm_clock_cntl_on(struct adreno_device *adreno_dev)

{

	if (adreno_is_a630(adreno_dev))

		return 0x8AA8AA02;

	else if (adreno_is_a612_family(adreno_dev) || adreno_is_a610_family(adreno_dev))

		return 0xAAA8AA82;

	else if (adreno_is_a702_family(adreno_dev))

		return 0xAAAAAA82;

	else

		return 0x8AA8AA82;

}



static inline unsigned int

__get_gmu_ao_cgc_mode_cntl(struct adreno_device *adreno_dev)

{

	if (adreno_is_a612(adreno_dev))

		return 0x00000022;

	else if (adreno_is_a615_family(adreno_dev))

		return 0x00000222;

	/* a662 should be checked before a660 */

	else if (adreno_is_a662(adreno_dev) || adreno_is_a621(adreno_dev) ||

			adreno_is_a622(adreno_dev) || adreno_is_a623(adreno_dev))

		return 0x00020200;

	else if (adreno_is_a660(adreno_dev))

		return 0x00020000;

	else

		return 0x00020202;

}



static inline unsigned int

__get_gmu_ao_cgc_delay_cntl(struct adreno_device *adreno_dev)

{

	if (adreno_is_a612(adreno_dev))

		return 0x00000011;

	else if (adreno_is_a615_family(adreno_dev))

		return 0x00000111;

	else

		return 0x00010111;

}



static inline unsigned int

__get_gmu_ao_cgc_hyst_cntl(struct adreno_device *adreno_dev)

{

	if (adreno_is_a612(adreno_dev))

		return 0x00000055;

	else if (adreno_is_a615_family(adreno_dev))

		return 0x00000555;

	else

		return 0x00005555;

}



static unsigned int __get_gmu_wfi_config(struct adreno_device *adreno_dev)

{

	unsigned int rev = ADRENO_GPUREV(adreno_dev);



	if ((rev == ADRENO_REV_A620) || adreno_is_a640(adreno_dev) ||

		adreno_is_a650(adreno_dev))

		return 0x00000002;



	return 0x00000000;

}



static void set_holi_sptprac_clock(struct kgsl_device *device, bool enable)

{

	u32 val = 0;



	kgsl_regread(device, A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL, &val);

	val &= ~1;

	kgsl_regwrite(device, A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL,

			val | (enable ? 1 : 0));

}



static void a6xx_hwcg_set(struct adreno_device *adreno_dev, bool on)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);

	unsigned int value;

	int i;



	if (!adreno_dev->hwcg_enabled)

		on = false;



	if (gmu_core_isenabled(device)) {

		gmu_core_regwrite(device, A6XX_GPU_GMU_AO_GMU_CGC_MODE_CNTL,

			on ? __get_gmu_ao_cgc_mode_cntl(adreno_dev) : 0);

		gmu_core_regwrite(device, A6XX_GPU_GMU_AO_GMU_CGC_DELAY_CNTL,

			on ? __get_gmu_ao_cgc_delay_cntl(adreno_dev) : 0);

		gmu_core_regwrite(device, A6XX_GPU_GMU_AO_GMU_CGC_HYST_CNTL,

			on ? __get_gmu_ao_cgc_hyst_cntl(adreno_dev) : 0);

		gmu_core_regwrite(device, A6XX_GMU_CX_GMU_WFI_CONFIG,

			on ? __get_gmu_wfi_config(adreno_dev) : 0);

	}



	kgsl_regread(device, A6XX_RBBM_CLOCK_CNTL, &value);



	/*

	 * GBIF L2 CGC control is not part of the UCHE and is enabled by

	 * default. Hence modify the register when CGC disabled is

	 * requested.

	 * Note: The below programming will need modification in case

	 * of change in the register reset value in future.

	 */

	kgsl_regrmw(device, A6XX_UCHE_GBIF_GX_CONFIG, GENMASK(18, 16),

			FIELD_PREP(GENMASK(18, 16), on ? 2 : 0));



	/* Recommended to always disable GBIF_CX_CONFIG for gen6_3_26_0 */

	if (!on || adreno_is_gen6_3_26_0(adreno_dev))

		kgsl_regrmw(device, A6XX_GBIF_CX_CONFIG, GENMASK(18, 16),

				FIELD_PREP(GENMASK(18, 16), 0));



	if (value == __get_rbbm_clock_cntl_on(adreno_dev) && on)

		return;



	if (value == 0 && !on)

		return;



	/*

	 * Disable SP clock before programming HWCG registers.

	 * A612, A610 and A702 family GPU is not having the GX power domain.

	 * Hence skip GMU_GX registers for A612, A610 and A702 family.

	 */



	if (gmu_core_isenabled(device) && !adreno_is_a612_family(adreno_dev) &&

		!adreno_is_a610_family(adreno_dev) && !adreno_is_a702_family(adreno_dev))

		gmu_core_regrmw(device,

			A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL, 1, 0);

	else if (adreno_is_a619_holi(adreno_dev))

		set_holi_sptprac_clock(device, false);



	for (i = 0; i < a6xx_core->hwcg_count; i++)

		kgsl_regwrite(device, a6xx_core->hwcg[i].offset,

			on ? a6xx_core->hwcg[i].val : 0);



	/*

	 * Enable SP clock after programming HWCG registers.

	 * A612, A610 and A702 family GPU is not having the GX power domain.

	 * Hence skip GMU_GX registers for A612, A610 and A702 family.

	 */

	if (gmu_core_isenabled(device) && !adreno_is_a612_family(adreno_dev) &&

		!adreno_is_a610_family(adreno_dev) && !adreno_is_a702_family(adreno_dev))

		gmu_core_regrmw(device,

			A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL, 0, 1);

	else if (adreno_is_a619_holi(adreno_dev))

		set_holi_sptprac_clock(device, true);



	/* enable top level HWCG */

	kgsl_regwrite(device, A6XX_RBBM_CLOCK_CNTL,

		on ? __get_rbbm_clock_cntl_on(adreno_dev) : 0);

}



struct a6xx_reglist_list {

	u32 *regs;

	u32 count;

};



#define REGLIST(_a) \

	 ((struct a6xx_reglist_list) { .regs = _a, .count = ARRAY_SIZE(_a), })



static void a6xx_patch_pwrup_reglist(struct adreno_device *adreno_dev)

{

	struct a6xx_reglist_list reglist[4];

	void *ptr = adreno_dev->pwrup_reglist->hostptr;

	struct cpu_gpu_lock *lock = ptr;

	int items = 0, i, j;

	u32 *dest = ptr + sizeof(*lock);

	u16 list_offset = 0;



	/* Static IFPC-only registers */

	reglist[items] = REGLIST(a6xx_ifpc_pwrup_reglist);

	list_offset += reglist[items++].count * 2;



	if (adreno_is_a650_family(adreno_dev)) {

		reglist[items] = REGLIST(a650_ifpc_pwrup_reglist);

		list_offset += reglist[items++].count * 2;

	}



	/* Static IFPC + preemption registers */

	reglist[items++] = REGLIST(a6xx_pwrup_reglist);



	/* Add target specific registers */

	if (adreno_is_a615_family(adreno_dev))

		reglist[items++] = REGLIST(a615_pwrup_reglist);

	else if (adreno_is_a650_family(adreno_dev))

		reglist[items++] = REGLIST(a650_pwrup_reglist);



	/*

	 * For each entry in each of the lists, write the offset and the current

	 * register value into the GPU buffer

	 */

	for (i = 0; i < items; i++) {

		u32 *r = reglist[i].regs;



		for (j = 0; j < reglist[i].count; j++) {

			*dest++ = r[j];

			kgsl_regread(KGSL_DEVICE(adreno_dev), r[j], dest++);

		}



		lock->list_length += reglist[i].count * 2;



		if ((r == a6xx_ifpc_pwrup_reglist) || (r == a650_ifpc_pwrup_reglist)) {

			u32 cs_len = adreno_coresight_patch_pwrup_reglist(adreno_dev, dest);



			cs_len = cs_len * 2;

			lock->list_length += cs_len;

			list_offset += cs_len;

			dest += cs_len;

		}

	}



	if (adreno_is_a630(adreno_dev)) {

		*dest++ = A6XX_RBBM_VBIF_CLIENT_QOS_CNTL;

		kgsl_regread(KGSL_DEVICE(adreno_dev),

			A6XX_RBBM_VBIF_CLIENT_QOS_CNTL, dest++);

	} else {

		*dest++ = A6XX_RBBM_GBIF_CLIENT_QOS_CNTL;

		kgsl_regread(KGSL_DEVICE(adreno_dev),

			A6XX_RBBM_GBIF_CLIENT_QOS_CNTL, dest++);

	}



	lock->list_length += 2;



	*dest++ = A6XX_RBBM_PERFCTR_CNTL;

	*dest++ = 1;

	lock->list_length += 2;



	/*

	 * The overall register list is composed of

	 * 1. Static IFPC-only registers

	 * 2. Static IFPC + preemption registers

	 * 3. Dynamic IFPC + preemption registers (ex: perfcounter selects)

	 *

	 * The CP views the second and third entries as one dynamic list

	 * starting from list_offset. list_length should be the total dwords in

	 * all the lists and list_offset should be specified as the size in

	 * dwords of the first entry in the list.

	 */

	lock->list_offset = list_offset;

}





static void a6xx_llc_configure_gpu_scid(struct adreno_device *adreno_dev);

static void a6xx_llc_configure_gpuhtw_scid(struct adreno_device *adreno_dev);

static void a6xx_llc_configure_gpumv_scid(struct adreno_device *adreno_dev);

static void a6xx_llc_enable_overrides(struct adreno_device *adreno_dev);



static void a6xx_set_secvid(struct kgsl_device *device)

{

	static bool set;



	if (set || !device->mmu.secured)

		return;



	kgsl_regwrite(device, A6XX_RBBM_SECVID_TSB_CNTL, 0x0);

	kgsl_regwrite(device, A6XX_RBBM_SECVID_TSB_TRUSTED_BASE_LO,

		lower_32_bits(KGSL_IOMMU_SECURE_BASE32));

	kgsl_regwrite(device, A6XX_RBBM_SECVID_TSB_TRUSTED_BASE_HI,

		upper_32_bits(KGSL_IOMMU_SECURE_BASE32));

	kgsl_regwrite(device, A6XX_RBBM_SECVID_TSB_TRUSTED_SIZE,

		FIELD_PREP(GENMASK(31, 12),

		(KGSL_IOMMU_SECURE_SIZE(&device->mmu) / SZ_4K)));



	if (ADRENO_QUIRK(ADRENO_DEVICE(device), ADRENO_QUIRK_SECVID_SET_ONCE))

		set = true;

}



static void a6xx_deassert_gbif_halt(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	kgsl_regwrite(device, A6XX_GBIF_HALT, 0x0);



	if (adreno_is_a619_holi(adreno_dev))

		kgsl_regwrite(device, A6XX_RBBM_GPR0_CNTL, 0x0);

	else

		kgsl_regwrite(device, A6XX_RBBM_GBIF_HALT, 0x0);

}



bool a6xx_gx_is_on(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwr = &device->pwrctrl;

	bool gdsc_on, clk_on;



	clk_on = __clk_is_enabled(pwr->grp_clks[0]);



	if (pwr->gx_pd)

		gdsc_on = kgsl_genpd_is_enabled(pwr->gx_pd);

	else

		gdsc_on = regulator_is_enabled(pwr->gx_regulator);



	return (gdsc_on & clk_on);

}



/*

 * Some targets support marking certain transactions as always privileged which

 * allows us to mark more memory as privileged without having to explicitly set

 * the APRIV bit.  For those targets, choose the following transactions to be

 * privileged by default:

 * CDWRITE     [6:6] - Crashdumper writes

 * CDREAD      [5:5] - Crashdumper reads

 * RBRPWB      [3:3] - RPTR shadow writes

 * RBPRIVLEVEL [2:2] - Memory accesses from PM4 packets in the ringbuffer

 * RBFETCH     [1:1] - Ringbuffer reads

 */

#define A6XX_APRIV_DEFAULT \

	((1 << 6) | (1 << 5) | (1 << 3) | (1 << 2) | (1 << 1))



void a6xx_start(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);

	unsigned int hbb_hi = 0, hbb_lo = 0;

	unsigned int uavflagprd_inv;

	struct adreno_ubwc_props *ubwc_props = &adreno_dev->ubwc_props;

	u32 mal = adreno_dev->mal;



	/* Enable 64 bit addressing */

	kgsl_regwrite(device, A6XX_CP_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_VSC_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_GRAS_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_RB_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_PC_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_HLSQ_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_VFD_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_VPC_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_UCHE_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_SP_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_TPL1_ADDR_MODE_CNTL, 0x1);

	kgsl_regwrite(device, A6XX_RBBM_SECVID_TSB_ADDR_MODE_CNTL, 0x1);



	/* Set up VBIF registers from the GPU core definition */

	kgsl_regmap_multi_write(&device->regmap, a6xx_core->vbif,

		a6xx_core->vbif_count);



	if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_LIMIT_UCHE_GBIF_RW))

		kgsl_regwrite(device, A6XX_UCHE_GBIF_GX_CONFIG, 0x10200F9);



	/* Make all blocks contribute to the GPU BUSY perf counter */

	kgsl_regwrite(device, A6XX_RBBM_PERFCTR_GPU_BUSY_MASKED, 0xFFFFFFFF);



	/*

	 * Set UCHE_WRITE_THRU_BASE to the UCHE_TRAP_BASE effectively

	 * disabling L2 bypass

	 */

	kgsl_regwrite(device, A6XX_UCHE_WRITE_RANGE_MAX_LO, 0xffffffc0);

	kgsl_regwrite(device, A6XX_UCHE_WRITE_RANGE_MAX_HI, 0x0001ffff);

	kgsl_regwrite(device, A6XX_UCHE_TRAP_BASE_LO, 0xfffff000);

	kgsl_regwrite(device, A6XX_UCHE_TRAP_BASE_HI, 0x0001ffff);

	kgsl_regwrite(device, A6XX_UCHE_WRITE_THRU_BASE_LO, 0xfffff000);

	kgsl_regwrite(device, A6XX_UCHE_WRITE_THRU_BASE_HI, 0x0001ffff);



	/*

	 * Some A6xx targets no longer use a programmed UCHE GMEM base

	 * address, so only write the registers if this address is

	 * non-zero.

	 */

	if (adreno_dev->uche_gmem_base) {

		kgsl_regwrite(device, A6XX_UCHE_GMEM_RANGE_MIN_LO,

				adreno_dev->uche_gmem_base);

		kgsl_regwrite(device, A6XX_UCHE_GMEM_RANGE_MIN_HI, 0x0);

		kgsl_regwrite(device, A6XX_UCHE_GMEM_RANGE_MAX_LO,

				adreno_dev->uche_gmem_base +

				adreno_dev->gpucore->gmem_size - 1);

		kgsl_regwrite(device, A6XX_UCHE_GMEM_RANGE_MAX_HI, 0x0);

	}



	kgsl_regwrite(device, A6XX_UCHE_FILTER_CNTL, 0x804);

	kgsl_regwrite(device, A6XX_UCHE_CACHE_WAYS, 0x4);



	/* ROQ sizes are twice as big on a640/a680 than on a630 */

	if (adreno_is_a612_family(adreno_dev) || adreno_is_a610_family(adreno_dev) ||

			adreno_is_a702_family(adreno_dev)) {

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_2, 0x00800060);

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_1, 0x40201b16);

	} else if (ADRENO_GPUREV(adreno_dev) >= ADRENO_REV_A640) {

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_2, 0x02000140);

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_1, 0x8040362C);

	} else {

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_2, 0x010000C0);

		kgsl_regwrite(device, A6XX_CP_ROQ_THRESHOLDS_1, 0x8040362C);

	}



	if (adreno_is_a660(adreno_dev))

		kgsl_regwrite(device, A6XX_CP_LPAC_PROG_FIFO_SIZE, 0x00000020);



	if (adreno_is_a663(adreno_dev)) {

		kgsl_regwrite(device, A6XX_RBBM_GBIF_CLIENT_QOS_CNTL, 0x0);

		kgsl_regwrite(device, A6XX_RBBM_LPAC_GBIF_CLIENT_QOS_CNTL, 0x0);

		kgsl_regwrite(device, A6XX_CP_LPAC_PROG_FIFO_SIZE, 0x00000020);

	}



	if (adreno_is_a612_family(adreno_dev) || adreno_is_a610_family(adreno_dev)) {

		/* For A612, gen6_3_26_0 and A610 Mem pool size is reduced to 48 */

		kgsl_regwrite(device, A6XX_CP_MEM_POOL_SIZE, 48);

		kgsl_regwrite(device, A6XX_CP_MEM_POOL_DBG_ADDR, 47);

	} else if (adreno_is_a702_family(adreno_dev)) {

		kgsl_regwrite(device, A6XX_CP_MEM_POOL_SIZE, 64);

		kgsl_regwrite(device, A6XX_CP_MEM_POOL_DBG_ADDR, 63);

	} else {

		kgsl_regwrite(device, A6XX_CP_MEM_POOL_SIZE, 128);

	}



	/* Setting the primFifo thresholds values */

	kgsl_regwrite(device, A6XX_PC_DBG_ECO_CNTL,

		a6xx_core->prim_fifo_threshold);



	/* This enables ECO for an issue in SP instruction fetching */

	if (adreno_is_a642l(adreno_dev) || adreno_is_a643(adreno_dev))

		kgsl_regrmw(device, A6XX_SP_DBG_ECO_CNTL, 0x6,

			FIELD_PREP(GENMASK(2, 1), 0x3));



	/* Set the AHB default slave response to "ERROR" */

	kgsl_regwrite(device, A6XX_CP_AHB_CNTL, 0x1);



	/* Turn on performance counters */

	kgsl_regwrite(device, A6XX_RBBM_PERFCTR_CNTL, 0x1);



	/* Turn on the IFPC counter (countable 4 on XOCLK4) */

	if (gmu_core_isenabled(device))

		gmu_core_regrmw(device, A6XX_GMU_CX_GMU_POWER_COUNTER_SELECT_1,

			0xff, 0x4);



	/* Turn on GX_MEM retention */

	if (gmu_core_isenabled(device) && adreno_is_a612(adreno_dev)) {

		kgsl_regwrite(device, A6XX_RBBM_BLOCK_GX_RETENTION_CNTL, 0x7FB);

		/* For CP IPC interrupt */

		kgsl_regwrite(device, A6XX_RBBM_INT_2_MASK, 0x00000010);

	}



	/* macrotilingmode 0: 4 channels (default)

	 * overwrite to 1: 8 channels for supported gpus

	 */

	if (adreno_is_a680(adreno_dev) ||

			adreno_is_a663(adreno_dev) ||

			adreno_is_a704(adreno_dev))

		kgsl_regwrite(device, A6XX_RBBM_NC_MODE_CNTL, 1);



	if (adreno_dev->highest_bank_bit) {

		hbb_lo = (adreno_dev->highest_bank_bit - 13) & 3;

		hbb_hi = ((adreno_dev->highest_bank_bit - 13) >> 2) & 1;

	}



	mal = (mal == 64) ? 1 : 0;



	uavflagprd_inv = (adreno_is_a650_family(adreno_dev)) ? 2 : 0;



	kgsl_regwrite(device, A6XX_RB_NC_MODE_CNTL,

				(ubwc_props->level2_swizzling_dis << 12) |

				(ubwc_props->rgb565_predicator << 11) |

				(hbb_hi << 10) | (ubwc_props->amsbc << 4) | (mal << 3) |

				(hbb_lo << 1) | ubwc_props->mode);





	kgsl_regwrite(device, A6XX_TPL1_NC_MODE_CNTL,

				(ubwc_props->level2_swizzling_dis << 6) | (hbb_hi << 4) |

				(mal << 3) | (hbb_lo << 1) | ubwc_props->mode);



	kgsl_regwrite(device, A6XX_SP_NC_MODE_CNTL,

				(ubwc_props->level2_swizzling_dis << 12) | (hbb_hi << 10) |

				(mal << 3) | (uavflagprd_inv << 4) |

				(hbb_lo << 1) | ubwc_props->mode);



	kgsl_regwrite(device, A6XX_UCHE_MODE_CNTL, (mal << 23) |

		(hbb_lo << 21));



	kgsl_regwrite(device, A6XX_RBBM_INTERFACE_HANG_INT_CNTL,

				(1 << 30) | a6xx_core->hang_detect_cycles);



	kgsl_regwrite(device, A6XX_UCHE_CLIENT_PF, BIT(7) |

			FIELD_PREP(GENMASK(3, 0), adreno_dev->uche_client_pf));



	/* Set weights for bicubic filtering */

	if (adreno_is_a650_family(adreno_dev)) {

		kgsl_regwrite(device, A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_0, 0);

		kgsl_regwrite(device, A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_1,

			0x3FE05FF4);

		kgsl_regwrite(device, A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_2,

			0x3FA0EBEE);

		kgsl_regwrite(device, A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_3,

			0x3F5193ED);

		kgsl_regwrite(device, A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_4,

			0x3F0243F0);

	}



	/* Set TWOPASSUSEWFI in A6XX_PC_DBG_ECO_CNTL if requested */

	if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_TWO_PASS_USE_WFI))

		kgsl_regrmw(device, A6XX_PC_DBG_ECO_CNTL, 0, (1 << 8));



	/* Set the bit vccCacheSkipDis=1 to get rid of TSEskip logic */

	if (a6xx_core->disable_tseskip)

		kgsl_regrmw(device, A6XX_PC_DBG_ECO_CNTL, 0, (1 << 9));



	/* Set the bit in HLSQ Cluster for A702 family */

	if (adreno_is_a702_family(adreno_dev))

		kgsl_regwrite(device, A6XX_CP_CHICKEN_DBG, (1 << 24));



	/* Enable the GMEM save/restore feature for preemption */

	if (adreno_is_preemption_enabled(adreno_dev))

		kgsl_regwrite(device, A6XX_RB_CONTEXT_SWITCH_GMEM_SAVE_RESTORE,

			0x1);



	/*

	 * Enable GMU power counter 0 to count GPU busy. This is applicable to

	 * all a6xx targets

	 */

	kgsl_regwrite(device, A6XX_GPU_GMU_AO_GPU_CX_BUSY_MASK, 0xff000000);

	kgsl_regrmw(device, A6XX_GMU_CX_GMU_POWER_COUNTER_SELECT_0, 0xff, 0x20);

	kgsl_regwrite(device, A6XX_GMU_CX_GMU_POWER_COUNTER_ENABLE, 0x1);



	a6xx_protect_init(adreno_dev);

	/*

	 * We start LM here because we want all the following to be up

	 * 1. GX HS

	 * 2. SPTPRAC

	 * 3. HFI

	 * At this point, we are guaranteed all.

	 */



	/* Configure LLCC */

	a6xx_llc_configure_gpu_scid(adreno_dev);

	a6xx_llc_configure_gpuhtw_scid(adreno_dev);

	a6xx_llc_configure_gpumv_scid(adreno_dev);



	a6xx_llc_enable_overrides(adreno_dev);



	if (adreno_is_a662(adreno_dev))

		kgsl_regrmw(device, A6XX_GBIF_CX_CONFIG, 0x3c0,

			FIELD_PREP(GENMASK(7, 6), 0x1) |

			FIELD_PREP(GENMASK(9, 8), 0x1));



	if (adreno_is_a660(adreno_dev)) {

		kgsl_regwrite(device, A6XX_CP_CHICKEN_DBG, 0x1);

		kgsl_regwrite(device, A6XX_RBBM_GBIF_CLIENT_QOS_CNTL, 0x0);

	}



	/* Set dualQ + disable afull for A660 and A663 GPU but not for A642l and A643 */

	if ((adreno_is_a660(adreno_dev) || adreno_is_a663(adreno_dev)) &&

			!(adreno_is_a642l(adreno_dev) || adreno_is_a643(adreno_dev)))

		kgsl_regwrite(device, A6XX_UCHE_CMDQ_CONFIG, 0x66906);



	if (ADRENO_FEATURE(adreno_dev, ADRENO_APRIV))

		kgsl_regwrite(device, A6XX_CP_APRIV_CNTL, A6XX_APRIV_DEFAULT);



	a6xx_set_secvid(device);



	/*

	 * Enable hardware clock gating here to prevent any register access

	 * issue due to internal clock gating.

	 */

	a6xx_hwcg_set(adreno_dev, true);



	/*

	 * All registers must be written before this point so that we don't

	 * miss any register programming when we patch the power up register

	 * list.

	 */

	if (!adreno_dev->patch_reglist &&

		(adreno_dev->pwrup_reglist->gpuaddr != 0)) {

		a6xx_patch_pwrup_reglist(adreno_dev);

		adreno_dev->patch_reglist = true;

	}



	/*

	 * During adreno_stop, GBIF halt is asserted to ensure

	 * no further transaction can go through GPU before GPU

	 * headswitch is turned off.

	 *

	 * This halt is deasserted once headswitch goes off but

	 * incase headswitch doesn't goes off clear GBIF halt

	 * here to ensure GPU wake-up doesn't fail because of

	 * halted GPU transactions.

	 */

	a6xx_deassert_gbif_halt(adreno_dev);



}



/* This is the start point for non GMU/RGMU targets */

static int a6xx_nogmu_start(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	/*

	 * During adreno_stop() GBIF halt is asserted to ensure that

	 * no further transactions go through the GPU before the

	 * GPU headswitch is turned off.

	 *

	 * The halt is supposed to be deasserted when the headswitch goes off

	 * but clear it again during start to be sure

	 */

	kgsl_regwrite(device, A6XX_GBIF_HALT, 0x0);

	kgsl_regwrite(device, A6XX_RBBM_GBIF_HALT, 0x0);



	ret = kgsl_mmu_start(device);

	if (ret)

		return ret;



	adreno_get_bus_counters(adreno_dev);

	adreno_perfcounter_restore(adreno_dev);



	a6xx_start(adreno_dev);

	return 0;

}



/*

 * CP_INIT_MAX_CONTEXT bit tells if the multiple hardware contexts can

 * be used at once of if they should be serialized

 */

#define CP_INIT_MAX_CONTEXT BIT(0)



/* Enables register protection mode */

#define CP_INIT_ERROR_DETECTION_CONTROL BIT(1)



/* Header dump information */

#define CP_INIT_HEADER_DUMP BIT(2) /* Reserved */



/* Default Reset states enabled for PFP and ME */

#define CP_INIT_DEFAULT_RESET_STATE BIT(3)



/* Drawcall filter range */

#define CP_INIT_DRAWCALL_FILTER_RANGE BIT(4)



/* Ucode workaround masks */

#define CP_INIT_UCODE_WORKAROUND_MASK BIT(5)



/*

 * Operation mode mask

 *

 * This ordinal provides the option to disable the

 * save/restore of performance counters across preemption.

 */

#define CP_INIT_OPERATION_MODE_MASK BIT(6)



/* Register initialization list */

#define CP_INIT_REGISTER_INIT_LIST BIT(7)



/* Register initialization list with spinlock */

#define CP_INIT_REGISTER_INIT_LIST_WITH_SPINLOCK BIT(8)



#define CP_INIT_MASK (CP_INIT_MAX_CONTEXT | \

		CP_INIT_ERROR_DETECTION_CONTROL | \

		CP_INIT_HEADER_DUMP | \

		CP_INIT_DEFAULT_RESET_STATE | \

		CP_INIT_UCODE_WORKAROUND_MASK | \

		CP_INIT_OPERATION_MODE_MASK | \

		CP_INIT_REGISTER_INIT_LIST_WITH_SPINLOCK)



void a6xx_cp_init_cmds(struct adreno_device *adreno_dev, u32 *cmds)

{

	int i = 0;



	cmds[i++] = cp_type7_packet(CP_ME_INIT, A6XX_CP_INIT_DWORDS - 1);



	/* Enabled ordinal mask */

	cmds[i++] = CP_INIT_MASK;



	if (CP_INIT_MASK & CP_INIT_MAX_CONTEXT)

		cmds[i++] = 0x00000003;



	if (CP_INIT_MASK & CP_INIT_ERROR_DETECTION_CONTROL)

		cmds[i++] = 0x20000000;



	if (CP_INIT_MASK & CP_INIT_HEADER_DUMP) {

		/* Header dump address */

========== PART_04.txt ==========
		cmds[i++] = 0x00000000;

		/* Header dump enable and dump size */

		cmds[i++] = 0x00000000;

	}



	if (CP_INIT_MASK & CP_INIT_UCODE_WORKAROUND_MASK)

		cmds[i++] = 0x00000000;



	if (CP_INIT_MASK & CP_INIT_OPERATION_MODE_MASK)

		cmds[i++] = 0x00000002;



	if (CP_INIT_MASK & CP_INIT_REGISTER_INIT_LIST_WITH_SPINLOCK) {

		uint64_t gpuaddr = adreno_dev->pwrup_reglist->gpuaddr;



		cmds[i++] = lower_32_bits(gpuaddr);

		cmds[i++] = upper_32_bits(gpuaddr);

		cmds[i++] =  0;

	}

}
