text A618 SOURCE PART_3_BLOCK_04 6 views


		ret = wait_for_completion_timeout(&hwsched->idle_gate,

				msecs_to_jiffies(ADRENO_IDLE_TIMEOUT));

		if (ret == 0) {

			ret = -ETIMEDOUT;

			WARN(1, "hwsched halt timeout\n");

		} else if (ret < 0) {

			dev_err(device->dev, "hwsched halt failed %d\n", ret);

		} else {

			ret = 0;

		}



		if (adreno_gpu_fault(adreno_dev))

			ret = -EDEADLK;

	}



	kgsl_mutex_lock(&device->mutex);



	/*

	 * This will allow the dispatcher to start submitting to

	 * hardware once device mutex is released

	 */

	adreno_put_gpu_halt(adreno_dev);



	return ret;

}



static int adreno_hwsched_idle(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	unsigned long wait = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	int ret;



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

		return -EDEADLK;



	if (!kgsl_state_is_awake(device))

		return 0;



	ret = hwsched_idle(adreno_dev);

	if (ret)

		return ret;



	do {

		if (adreno_gpu_fault(adreno_dev))

			return -EDEADLK;



		if (gpudev->hw_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))

		return -EDEADLK;



	if (gpudev->hw_isidle(adreno_dev))

		return 0;



	return -ETIMEDOUT;

}



void adreno_hwsched_register_hw_fence(struct adreno_device *adreno_dev)

{

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_HW_FENCE))

		return;



	if (test_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags))

		return;



	if (kgsl_hw_fence_init(device))

		return;



	/*

	 * We need to set up the memory descriptor with the physical address of the Tx/Rx Queues so

	 * that these buffers can be imported in to GMU VA space

	 */

	kgsl_memdesc_init(device, &hwsched->hw_fence.md, 0);

	kgsl_hw_fence_populate_md(device, &hwsched->hw_fence.md);



	ret = kgsl_memdesc_sg_dma(&hwsched->hw_fence.md, hwsched->hw_fence.md.physaddr,

		hwsched->hw_fence.md.size);

	if (ret) {

		dev_err(device->dev, "Failed to setup HW fences memdesc: %d\n",

			ret);

		kgsl_hw_fence_close(device);

		memset(&hwsched->hw_fence.md, 0x0, sizeof(hwsched->hw_fence.md));

		return;

	}



	hwsched->hw_fence_cache = KMEM_CACHE(adreno_hw_fence_entry, 0);



	set_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags);

}



int adreno_hwsched_wait_ack_completion(struct adreno_device *adreno_dev,

	struct device *dev, struct pending_cmd *ack,

	void (*process_msgq)(struct adreno_device *adreno_dev))

{

	int rc;

	/* Only allow a single log in a second */

	static DEFINE_RATELIMIT_STATE(_rs, HZ, 1);

	static u32 unprocessed, processed;

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u64 start, end;



	start = gpudev->read_alwayson(adreno_dev);

	rc = wait_for_completion_timeout(&ack->complete,

		msecs_to_jiffies(HFI_RSP_TIMEOUT));

	/*

	 * A non-zero return value means the completion is complete, whereas zero indicates

	 * timeout

	 */

	if (rc) {

		/*

		 * If an ack goes unprocessed, keep track of processed and unprocessed acks

		 * because we may not log each unprocessed ack due to ratelimiting

		 */

		if (unprocessed)

			processed++;

		return 0;

	}



	/*

	 * It is possible the ack came, but due to HLOS latencies in processing hfi interrupt

	 * and/or the f2h daemon, the ack isn't processed yet. Hence, process the msgq one last

	 * time.

	 */

	process_msgq(adreno_dev);

	end = gpudev->read_alwayson(adreno_dev);

	if (completion_done(&ack->complete)) {

		unprocessed++;

		if (__ratelimit(&_rs))

			dev_err(dev, "Ack unprocessed for id:%d sequence=%d count=%d/%d ticks=0x%llx/0x%llx\n",

				MSG_HDR_GET_ID(ack->sent_hdr), MSG_HDR_GET_SEQNUM(ack->sent_hdr),

				unprocessed, processed, start, end);

		return 0;

	}



	dev_err(dev, "Ack timeout for id:%d sequence=%d ticks=0x%llx/0x%llx\n",

		MSG_HDR_GET_ID(ack->sent_hdr), MSG_HDR_GET_SEQNUM(ack->sent_hdr), start, end);

	gmu_core_fault_snapshot(KGSL_DEVICE(adreno_dev), GMU_FAULT_WAIT_ACK_COMPLETION);

	return -ETIMEDOUT;

}



int adreno_hwsched_ctxt_unregister_wait_completion(

	struct adreno_device *adreno_dev,

	struct device *dev, struct kgsl_context *context, struct pending_cmd *ack,

	void (*process_msgq)(struct adreno_device *adreno_dev),

	struct hfi_unregister_ctxt_cmd *cmd)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	struct adreno_context *drawctxt = ADRENO_CONTEXT(context);

	int rb = adreno_get_level(context);

	int ret;

	bool fast = adreno_is_fast_context_destroy_enabled(adreno_dev);

	u64 start, end;

	char prefix[60];



	start = gpudev->read_alwayson(adreno_dev);

	kgsl_mutex_unlock(&device->mutex);



	ret = wait_for_completion_timeout(&ack->complete, msecs_to_jiffies(30 * 1000));



	kgsl_mutex_lock(&device->mutex);

	if (ret)

		return 0;



	/*

	 * It is possible the ack came, but due to HLOS latencies in processing hfi interrupt

	 * and/or the f2h daemon, the ack isn't processed yet. Hence, process the msgq one last

	 * time.

	 */

	process_msgq(adreno_dev);

	end = gpudev->read_alwayson(adreno_dev);



	if (completion_done(&ack->complete)) {

		snprintf(prefix, sizeof(prefix),

			"Ack unprocessed for %scontext unregister", fast ? "fast " : "");

		dev_err_ratelimited(dev,

			"%s type: %s rb: %d seq: %d ctx: %u ts: %u ticks=0x%llx/0x%llx\n",

			prefix, kgsl_context_type(drawctxt->type), rb,

			MSG_HDR_GET_SEQNUM(ack->sent_hdr), cmd->ctxt_id, cmd->ts, start, end);

		return 0;

	}



	snprintf(prefix, sizeof(prefix),

		"Ack timeout for %scontext unregister", fast ? "fast " : "");

	dev_err_ratelimited(dev,

		"%s type: %s rb: %d seq: %d ctx: %u ts: %u ticks=0x%llx/0x%llx\n",

		prefix, kgsl_context_type(drawctxt->type), rb,

		MSG_HDR_GET_SEQNUM(ack->sent_hdr), cmd->ctxt_id, cmd->ts, start, end);

	return -ETIMEDOUT;

}



u32 adreno_hwsched_parse_payload(struct payload_section *payload, u32 key)

{

	u32 i;



	/* Each key-value pair is 2 dwords */

	for (i = 0; i < payload->dwords; i += 2) {

		if (payload->data[i] == key)

			return payload->data[i + 1];

	}



	return 0;

}



u32 adreno_hwsched_get_payload_rb_key_legacy(struct adreno_device *adreno_dev, u32 rb_id, u32 key)

{

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

	u32 i = 0, payload_bytes;

	void *start;



	if (!cmd->hdr)

		return 0;



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

			offsetof(struct hfi_context_bad_cmd_legacy, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == PAYLOAD_RB) {

			u32 id = adreno_hwsched_parse_payload(payload, KEY_RB_ID);



			if (id == rb_id)

				return adreno_hwsched_parse_payload(payload, key);

		}



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

	}



	return 0;

}



u32 adreno_hwsched_get_payload_rb_key(struct adreno_device *adreno_dev, u32 rb_id, u32 key)

{

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

	u32 i = 0, payload_bytes;

	void *start;



	if (!cmd->hdr)

		return 0;



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

			offsetof(struct hfi_context_bad_cmd, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == PAYLOAD_RB) {

			u32 id = adreno_hwsched_parse_payload(payload, KEY_RB_ID);



			if (id == rb_id)

				return adreno_hwsched_parse_payload(payload, key);

		}



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

	}



	return 0;

}



void adreno_hwsched_log_remove_pending_hw_fences(struct adreno_device *adreno_dev,

	struct device *dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct adreno_hw_fence_entry entries[5];

	struct adreno_hw_fence_entry *entry, *next;

	struct kgsl_context *context;

	int id, i;

	u32 count = 0;



	read_lock(&device->context_lock);

	idr_for_each_entry(&device->context_idr, context, id) {

		struct adreno_context *drawctxt = ADRENO_CONTEXT(context);



		spin_lock(&drawctxt->lock);



		list_for_each_entry_safe(entry, next, &drawctxt->hw_fence_list, node) {

			if (count < ARRAY_SIZE(entries))

				memcpy(&entries[count], entry, sizeof(*entry));

			count++;

			adreno_hwsched_remove_hw_fence_entry(adreno_dev, entry);

		}



		spin_unlock(&drawctxt->lock);

	}

	read_unlock(&device->context_lock);



	if (!count)

		return;



	dev_err(dev, "%d hw fences may not be signaled. %s are:\n", count,

		count > 5 ? "First 5" : "They");



	for (i = 0; (i < count) && (i < ARRAY_SIZE(entries)); i++)

		dev_err(dev, "%d: ctx=%llu seqno=%llu\n", i, entries[i].cmd.ctxt_id,

			entries[i].cmd.ts);

}



static void adreno_hwsched_lookup_key_value(struct adreno_device *adreno_dev,

		u32 type, u32 key, u32 *ptr, u32 num_values)

{

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

	u32 i = 0, payload_bytes;

	void *start;



	if (!cmd->hdr)

		return;



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

			offsetof(struct hfi_context_bad_cmd, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		/* key-value pair is 'num_values + 1' dwords */

		if ((payload->type == type) && (payload->data[i] == key)) {

			u32 j = 1;



			while (num_values--) {

				ptr[j - 1] = payload->data[i + j];

				j++;

			}

			break;

		}



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

	}

}



bool adreno_hwsched_log_nonfatal_gpu_fault(struct adreno_device *adreno_dev,

		struct device *dev, u32 error)

{

	bool non_fatal = true;



	switch (error) {

	case GMU_CP_AHB_ERROR: {

		u32 err_details[2];



		adreno_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,

						KEY_CP_AHB_ERROR, err_details, 2);

		dev_crit_ratelimited(dev,

			"CP: AHB bus error, CP_RL_ERROR_DETAILS_0:0x%x CP_RL_ERROR_DETAILS_1:0x%x\n",

			err_details[0], err_details[1]);

		break;

	}

	case GMU_ATB_ASYNC_FIFO_OVERFLOW:

		dev_crit_ratelimited(dev, "RBBM: ATB ASYNC overflow\n");

		break;

	case GMU_RBBM_ATB_BUF_OVERFLOW:

		dev_crit_ratelimited(dev, "RBBM: ATB bus overflow\n");

		break;

	case GMU_UCHE_OOB_ACCESS:

		dev_crit_ratelimited(dev, "UCHE: Out of bounds access\n");

		break;

	case GMU_UCHE_TRAP_INTR:

		dev_crit_ratelimited(dev, "UCHE: Trap interrupt\n");

		break;

	case GMU_TSB_WRITE_ERROR: {

		u32 addr[2];



		adreno_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,

						KEY_TSB_WRITE_ERROR, addr, 2);

		dev_crit_ratelimited(dev, "TSB: Write error interrupt: Address: 0x%lx MID: %lu\n",

			FIELD_GET(GENMASK(16, 0), addr[1]) << 32 | addr[0],

			FIELD_GET(GENMASK(31, 23), addr[1]));

		break;

	}

	default:

		non_fatal = false;

		break;

	}



	return non_fatal;

}



int adreno_hwsched_poll_msg_queue_write_index(struct kgsl_memdesc *hfi_mem)

{

	struct hfi_queue_table *tbl = hfi_mem->hostptr;

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

	unsigned long timeout = jiffies + msecs_to_jiffies(HFI_RSP_TIMEOUT);



	while (time_before(jiffies, timeout)) {

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

			goto done;



		/* Wait for up to 100 us before trying again */

		usleep_range((POLL_SLEEP_US >> 2) + 1, POLL_SLEEP_US);

		cpu_relax();

	}



	/* Check if the write index has advanced */

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

		return -ETIMEDOUT;



done:

	/*

	 * This is to ensure that the queue is not read speculatively before the

	 * polling condition is evaluated.

	 */

	rmb();



	return 0;

}



/* This function can be called while holding the drawctxt spinlock */

void adreno_hwsched_remove_hw_fence_entry(struct adreno_device *adreno_dev,

	struct adreno_hw_fence_entry *entry)

{

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	struct adreno_context *drawctxt = entry->drawctxt;

	struct adreno_hwsched_hw_fence *hwf = &adreno_dev->hwsched.hw_fence;



	spin_lock(&hwf->lock);

	hwf->pending_count--;

	spin_unlock(&hwf->lock);

	drawctxt->hw_fence_count--;



	dma_fence_put(&entry->kfence->fence);

	list_del_init(&entry->node);

	kmem_cache_free(hwsched->hw_fence_cache, entry);

	kgsl_context_put_deferred(&drawctxt->base);

}



void adreno_hwsched_add_profile_events(struct adreno_device *adreno_dev,

	struct kgsl_drawobj_cmd *cmdobj, struct adreno_submit_time *time)

{

	unsigned long flags;

	u64 time_in_s;

	unsigned long time_in_ns;

	struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);

	struct kgsl_context *context = drawobj->context;

	struct submission_info info = {0};

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (!time)

		return;



	/*

	 * Here we are attempting to create a mapping between the

	 * GPU time domain (alwayson counter) and the CPU time domain

	 * (local_clock) by sampling both values as close together as

	 * possible. This is useful for many types of debugging and

	 * profiling. In order to make this mapping as accurate as

	 * possible, we must turn off interrupts to avoid running

	 * interrupt handlers between the two samples.

	 */



	local_irq_save(flags);



	/* Read always on registers */

	time->ticks = gpudev->read_alwayson(adreno_dev);



	/* Trace the GPU time to create a mapping to ftrace time */

	trace_adreno_cmdbatch_sync(context->id, context->priority,

		drawobj->timestamp, time->ticks);



	/* Get the kernel clock for time since boot */

	time->ktime = local_clock();



	/* Get the timeofday for the wall time (for the user) */

	ktime_get_real_ts64(&time->utime);



	local_irq_restore(flags);



	/* Return kernel clock time to the client if requested */

	time_in_s = time->ktime;

	time_in_ns = do_div(time_in_s, 1000000000);



	info.inflight = hwsched->inflight;

	info.rb_id = adreno_get_level(context);

	info.gmu_dispatch_queue = context->gmu_dispatch_queue;



	cmdobj->submit_ticks = time->ticks;



	msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_SUBMIT,

		pid_nr(context->proc_priv->pid),

		context->id, drawobj->timestamp,

		!!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));

	trace_adreno_cmdbatch_submitted(drawobj, &info, time->ticks,

		(unsigned long) time_in_s, time_in_ns / 1000, 0);



	log_kgsl_cmdbatch_submitted_event(context->id, drawobj->timestamp,

			context->priority, drawobj->flags);

}



int adreno_gmu_context_queue_write(struct adreno_device *adreno_dev,

	struct kgsl_memdesc *gmu_context_queue, u32 *msg, u32 size_bytes,

	struct kgsl_drawobj *drawobj, struct adreno_submit_time *time)

{

	struct gmu_context_queue_header *hdr = gmu_context_queue->hostptr;

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	u32 *queue = gmu_context_queue->hostptr + sizeof(*hdr);

	u32 i, empty_space, write_idx = hdr->write_index, read_idx = hdr->read_index;

	u32 size_dwords = size_bytes >> 2;

	u32 align_size = ALIGN(size_dwords, SZ_4);

	u32 id = MSG_HDR_GET_ID(*msg);

	struct kgsl_drawobj_cmd *cmdobj = NULL;



	empty_space = (write_idx >= read_idx) ?

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

			: (read_idx - write_idx);



	if (empty_space <= align_size)

		return -ENOSPC;



	if (!IS_ALIGNED(size_bytes, sizeof(u32)))

		return -EINVAL;



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

		queue[write_idx] = msg[i];

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

	}



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

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

		queue[write_idx] = 0xfafafafa;

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

	}



	/* Ensure packet is written out before proceeding */

	wmb();



	if (!drawobj)

		goto done;



	if (drawobj->type & SYNCOBJ_TYPE) {

		struct kgsl_drawobj_sync *syncobj = SYNCOBJ(drawobj);



		trace_adreno_syncobj_submitted(drawobj->context->id, drawobj->timestamp,

			syncobj->num_hw_fence, gpudev->read_alwayson(adreno_dev));

		goto done;

	}



	cmdobj = CMDOBJ(drawobj);



	adreno_hwsched_add_profile_events(adreno_dev, cmdobj, time);



	/*

	 * Put the profiling information in the user profiling buffer.

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

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

	 * packet before the profile data is written out.

	 */

	adreno_profile_submit_time(time);



done:

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



	hfi_update_write_idx(&hdr->write_index, write_idx);



	return 0;

}



int adreno_hwsched_drain_and_idle(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret = adreno_drain(device, msecs_to_jiffies(100));



	/* Make sure the dispatcher and hardware are idle */

	if (!ret)

		ret = adreno_hwsched_idle(adreno_dev);



	return ret;

}



static void _retire_inflight_hw_fences(struct adreno_device *adreno_dev,

	struct kgsl_context *context)

{

	struct adreno_context *drawctxt = ADRENO_CONTEXT(context);

	struct adreno_hw_fence_entry *entry, *tmp;



	if (!test_bit(ADRENO_HWSCHED_HW_FENCE, &adreno_dev->hwsched.flags))

		return;



	spin_lock(&drawctxt->lock);



	list_for_each_entry_safe(entry, tmp, &drawctxt->hw_fence_inflight_list, node) {

		struct gmu_context_queue_header *hdr = drawctxt->gmu_context_queue.hostptr;



		/*

		 * Since this list is sorted by timestamp, abort on the first fence that hasn't

		 * yet been sent to TxQueue

		 */

		if (timestamp_cmp((u32)entry->cmd.ts, hdr->out_fence_ts) > 0)

			break;



		adreno_hwsched_remove_hw_fence_entry(adreno_dev, entry);

	}

	spin_unlock(&drawctxt->lock);

}



void adreno_hwsched_log_profiling_info(struct adreno_device *adreno_dev, u32 *rcvd)

{

	struct hfi_ts_retire_cmd *cmd = (struct hfi_ts_retire_cmd *)rcvd;

	struct kgsl_context *context;

	struct retire_info info = {0};

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	context = kgsl_context_get(device, cmd->ctxt_id);

	if (context == NULL)

		return;



	info.timestamp = cmd->ts;

	info.rb_id = adreno_get_level(context);

	info.gmu_dispatch_queue = context->gmu_dispatch_queue;

	info.submitted_to_rb = cmd->submitted_to_rb;

	info.sop = cmd->sop;

	info.eop = cmd->eop;

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

		info.active = cmd->eop - cmd->sop;

	else

		info.active = cmd->active;

	info.retired_on_gmu = cmd->retired_on_gmu;



	/* protected GPU work must not be reported */

	if  (!(context->flags & KGSL_CONTEXT_SECURE))

		kgsl_work_period_update(device, context->proc_priv->period,

					     info.active);



	trace_adreno_cmdbatch_retired(context, &info, 0, 0, 0);



	log_kgsl_cmdbatch_retired_event(context->id, cmd->ts,

		context->priority, 0, cmd->sop, cmd->eop);



	_retire_inflight_hw_fences(adreno_dev, context);



	kgsl_context_put(context);

}



void *adreno_hwsched_get_rb_hostptr(struct adreno_device *adreno_dev,

	u64 gpuaddr, u32 size)

{

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	u64 offset;

	u32 i;



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

		struct kgsl_memdesc *md = hwsched->mem_alloc_table[i].md;



		if (kgsl_gpuaddr_in_memdesc(md, gpuaddr, size)) {

			offset = gpuaddr - md->gpuaddr;

			return md->hostptr + offset;

		}

	}



	return NULL;

}



void adreno_hwsched_reset_hfi_mem(struct adreno_device *adreno_dev)

{

	struct adreno_hwsched *hwsched = &adreno_dev->hwsched;

	struct kgsl_memdesc *md = NULL;

	u32 i;



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

		struct hfi_mem_alloc_desc *desc = &hwsched->mem_alloc_table[i].desc;



		if (desc->flags & HFI_MEMFLAG_HOST_INIT) {

			md = hwsched->mem_alloc_table[i].md;

			if (md->hostptr)

				memset(md->hostptr, 0x0, md->size);

		}

	}



	if (!(adreno_is_preemption_enabled(adreno_dev) &&

		ADRENO_FEATURE(adreno_dev, ADRENO_DEFER_GMEM_ALLOC)))

		return;



	/* No need to reset gmem portion of the preemption records */

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

		md = hwsched->preempt_rec[i];

		if (md && md->hostptr)

			memset(md->hostptr, 0x0, md->size);

	}

}





==========================================================================================
FILE: adreno_hwsched.h
LINES: 491 | BYTES: 17804
==========================================================================================

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

/*

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

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

 */



#ifndef _ADRENO_HWSCHED_H_

#define _ADRENO_HWSCHED_H_



#include "adreno_hfi.h"

#include "kgsl_sync.h"



/* This structure represents inflight command object */

struct cmd_list_obj {

	/** @drawobj: Handle to the draw object */

	struct kgsl_drawobj *drawobj;

	/** @node: List node to put it in the list of inflight commands */

	struct list_head node;

};



struct adreno_hwsched_hw_fence {

	/** @lock: Spinlock for managing hardware fences */

	spinlock_t lock;

	/**

	 * @unack_count: Number of hardware fences sent to GMU but haven't yet been ack'd

	 * by GMU

	 */

	u32 unack_count;

	/**

	 * @unack_wq: Waitqueue to wait on till number of unacked hardware fences drops to

	 * a desired threshold

	 */

	wait_queue_head_t unack_wq;

	/**

	 * @defer_drawctxt: Drawctxt to send hardware fences from as soon as unacked

	 * hardware fences drops to a desired threshold

	 */

	struct adreno_context *defer_drawctxt;

	/**

	 * @defer_ts: The timestamp of the hardware fence which got deferred

	 */

	u32 defer_ts;

	/**

	 * @flags: Flags to control the creation of new hardware fences

	 */

	unsigned long flags;

	/** @seqnum: Sequence number for hardware fence packet header */

	atomic_t seqnum;

	/** @soccp_rproc: rproc handle for soccp */

	struct rproc *soccp_rproc;

	/** @hw_fence_md: Kgsl memory descriptor for hardware fences queue */

	struct kgsl_memdesc md;

	/** @pending_count: Number of hardware fences that haven't yet been sent to Tx Queue */

	u32 pending_count;

};



/**

* struct adreno_hw_fence_entry - A structure to store hardware fence and the context

*/

struct adreno_hw_fence_entry {

	/** @cmd: H2F_MSG_HW_FENCE_INFO packet for this hardware fence */

	struct hfi_hw_fence_info cmd;

	/** @kfence: Pointer to the kgsl fence */

	struct kgsl_sync_fence *kfence;

	/** @drawctxt: Pointer to the context */

	struct adreno_context *drawctxt;

	/** @node: list node to add it to a list */

	struct list_head node;

	/** @reset_node: list node to add it to post reset list of hardware fences */

	struct list_head reset_node;

};



/**

 * struct adreno_hwsched_ops - Function table to hook hwscheduler things

 * to target specific routines

 */

struct adreno_hwsched_ops {

	/**

	 * @submit_drawobj - Target specific function to submit IBs to hardware

	 */

	int (*submit_drawobj)(struct adreno_device *adreno_dev,

		struct kgsl_drawobj *drawobj);

	/**

	 * @preempt_count - Target specific function to get preemption count

	 */

	u32 (*preempt_count)(struct adreno_device *adreno_dev);

	/**

	 * @preempt_info - Target specific function to get preemption information

	 */

	ssize_t (*preempt_info)(struct adreno_device *adreno_dev, char *buf);

	/**

	 * @create_hw_fence - Target specific function to create a hardware fence

	 */

	void (*create_hw_fence)(struct adreno_device *adreno_dev,

		struct kgsl_sync_fence *kfence);

	/**

	 * @set_dcvs_profile - Set dcvs profile for a process

	 */

	int (*set_dcvs_profile)(struct adreno_device *adreno_dev,

		struct kgsl_process_private *proc_priv);

};



enum gpu_reset_type {

	GMU_GPU_RESET_NONE,

	GMU_GPU_SOFT_RESET,

	GMU_GPU_HARD_RESET,

};



struct adreno_dcvs_tunable {

	/** @value: Stores the requested value for the tunable **/

	u32 value;

	/** @update: True if this value needs to be sent to GMU at slumber exit **/

	bool update;

};



/**

 * struct adreno_hwsched - Container for the hardware scheduler

 */

struct adreno_hwsched {

	/** @mem_alloc_table: Array of HFI memory allocation entries */

	struct hfi_mem_alloc_entry mem_alloc_table[32];

	/** @mem_alloc_entries: Number of entries in the memory allocation table */

	u32 mem_alloc_entries;

	/** @mutex: Mutex needed to run dispatcher function */

	struct mutex mutex;

	/** @flags: Container for the dispatcher internal flags */

	unsigned long flags;

	/** @inflight: Number of active submissions to the dispatch queues */

	u32 inflight;

	/** @jobs - Array of dispatch job lists for each priority level */

	struct llist_head jobs[16];

	/** @requeue - Array of lists for dispatch jobs that got requeued */

	struct llist_head requeue[16];

	/** @cmd_list: List of objects submitted to dispatch queues */

	struct list_head cmd_list;

	/** @hwsched_ops: Container for target specific hwscheduler ops */

	const struct adreno_hwsched_ops *hwsched_ops;

	/** @ctxt_bad: Container for the context bad hfi packet */

	void *ctxt_bad;

	/** @idle_gate: Gate to wait on for hwscheduler to idle */

	struct completion idle_gate;

	/** @big_cmdobj = Points to the big IB that is inflight */

	struct kgsl_drawobj_cmd *big_cmdobj;

	/** @recurring_cmdobj: Recurring commmand object sent to GMU */

	struct kgsl_drawobj_cmd *recurring_cmdobj;

	/** @lsr_timer: Timer struct to schedule lsr work */

	struct timer_list lsr_timer;

	/** @lsr_check_ws: Lsr work to update power stats */

	struct work_struct lsr_check_ws;

	/** @hw_fence: Container for the hw fences instance */

	struct kmem_cache *hw_fence_cache;

	/**

	 * @submission_seqnum: Sequence number for sending submissions to GMU context queues or

	 * dispatch queues

	 */

	atomic_t submission_seqnum;

	/** @global_ctxtq: Memory descriptor for global context queue */

	struct kgsl_memdesc global_ctxtq;

	/** @global_ctxt_gmu_registered: Whether global context is registered with gmu */

	bool global_ctxt_gmu_registered;

	/** @hw_fence: Container for hw fence related structures */

	struct adreno_hwsched_hw_fence hw_fence;

	/** @reset_type: GPU fault reset (hard/soft) type */

	enum gpu_reset_type reset_type;

	/** @preempt_rec: Memory descriptors for non-gmem part of preemption records */

	struct kgsl_memdesc *preempt_rec[KGSL_PRIORITY_MAX_RB_LEVELS];

	/**

	 * @preempt_rec_gmem: Memory descriptors for gmem part of preemption

	 * records. No gmem buffer needed for rb0 preemption record.

	 */

	struct kgsl_memdesc *preempt_rec_gmem[KGSL_PRIORITY_MAX_RB_LEVELS - 1];

	/**

	 * @secure_preempt_rec: Memory descriptors for non-gmem part of secure

	 * preemption records

	 */

	struct kgsl_memdesc *secure_preempt_rec[KGSL_PRIORITY_MAX_RB_LEVELS];

	/**

	 * @secure_preempt_rec_gmem: Memory descriptors for gmem part of secure

	 * preemption records. No gmem buffer needed for rb0 preemption record.

	 */

	struct kgsl_memdesc *secure_preempt_rec_gmem[KGSL_PRIORITY_MAX_RB_LEVELS - 1];

	/** @dcvs_param_update: True if dcvs params have to be sent to GMU at slumber exit */

	bool dcvs_param_update;

	/** @tunables_kobj: Kobj for dcvs tunables **/

	struct kobject tunables_kobj;

	/** @dcvs_kobj: Kobj for dcvs params **/

	struct kobject dcvs_kobj;

	/** @sysfs_dcvs_tunables : Tuning parameters for GMU based DCVS **/

	struct adreno_dcvs_tunable sysfs_dcvs_tunables[GPU_TUNING_KEY_MAX];

	/** @default_dcvs_tunables: Default value of GMU based DCVS tunables **/

	u32 default_dcvs_tunables[GPU_TUNING_KEY_MAX];

};



/*

 * This value is based on maximum number of IBs that can fit

 * in the ringbuffer.

 */

#define HWSCHED_MAX_IBS 2000



enum adreno_hwsched_flags {

	ADRENO_HWSCHED_POWER = 0,

	ADRENO_HWSCHED_ACTIVE,

	ADRENO_HWSCHED_CTX_BAD_LEGACY,

	ADRENO_HWSCHED_CONTEXT_QUEUE,

	ADRENO_HWSCHED_HW_FENCE,

	ADRENO_HWSCHED_FORCE_RETIRE_GMU,

	ADRENO_HWSCHED_GPU_SOFT_RESET,

};



/**

 * adreno_hwsched_process_mem_alloc - Process memory allocation for hwsched

 * @adreno_dev: Pointer to the adreno device

 * @mad: Pointer to the HFI memory allocation descriptor

 *

 * This function processes the memory allocation request for the hwsched.

 * It retrieves or allocates the necessary memory based on the provided

 * descriptor and updates the descriptor with the allocated memory addresses.

 *

 * Return: 0 on success, or a negative error code on failure.

 */

int adreno_hwsched_process_mem_alloc(struct adreno_device *adreno_dev,

	struct hfi_mem_alloc_desc *mad);



/**

 * adreno_hwsched_start() - activate the hwsched dispatcher

 * @adreno_dev: pointer to the adreno device

 *

 * Enable dispatcher thread to execute

 */

void adreno_hwsched_start(struct adreno_device *adreno_dev);

/**

 * adreno_hwsched_init() - Initialize the hwsched

 * @adreno_dev: pointer to the adreno device

 * @hwsched_ops: Pointer to target specific hwsched ops

 *

 * Set up the hwsched resources.

 * Return: 0 on success or negative on failure.

 */

int adreno_hwsched_init(struct adreno_device *adreno_dev,

	const struct adreno_hwsched_ops *hwsched_ops);



/**

 * adreno_hwsched_parse_fault_ib - Parse the faulty submission

 * @adreno_dev: pointer to the adreno device

 * @snapshot: Pointer to the snapshot structure

 *

 * Walk the list of active submissions to find the one that faulted and

 * parse it so that relevant command buffers can be added to the snapshot

 */

void adreno_hwsched_parse_fault_cmdobj(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot);



/**

 * adreno_hwsched_unregister_contexts - Reset context gmu_registered bit

 * @adreno_dev: pointer to the adreno device

 *

 * Walk the list of contexts and reset the gmu_registered for all

 * contexts

 */

void adreno_hwsched_unregister_contexts(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_drain_and_idle - Drain pending work and wait for dispatcher and

 * hardware to become idle

 * @adreno_dev: A handle to adreno device

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_hwsched_drain_and_idle(struct adreno_device *adreno_dev);



void adreno_hwsched_retire_cmdobj(struct adreno_hwsched *hwsched,

	struct kgsl_drawobj_cmd *cmdobj);



bool adreno_hwsched_context_queue_enabled(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_register_hw_fence - Register GPU as a hardware fence client

 * @adreno_dev: pointer to the adreno device

 *

 * Register with the hardware fence driver to be able to trigger and wait

 * for hardware fences. Also, set up the memory descriptor for mapping the

 * client queue to the GMU.

 */

void adreno_hwsched_register_hw_fence(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_deregister_hw_fence - Deregister GPU as a hardware fence client

 * @adreno_dev: pointer to the adreno device

 *

 * Deregister with the hardware fence driver and free up any resources allocated

 * as part of registering with the hardware fence driver

 */

void adreno_hwsched_deregister_hw_fence(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_replay - Resubmit inflight cmdbatches after gpu reset

 * @adreno_dev: pointer to the adreno device

 *

 * Resubmit all cmdbatches to GMU after device reset

 */

void adreno_hwsched_replay(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_parse_payload - Parse payload to look up a key

 * @payload: Pointer to a payload section

 * @key: The key who's value is to be looked up

 *

 * This function parses the payload data which is a sequence

 * of key-value pairs.

 *

 * Return: The value of the key or 0 if key is not found

 */

u32 adreno_hwsched_parse_payload(struct payload_section *payload, u32 key);



/**

 * adreno_hwsched_get_payload_rb_key - Retrieve the payload based on ring buffer key

 * @adreno_dev: Pointer to the adreno device

 * @rb_id: Ring buffer ID to search for

 * @key: Key to retrieve from the payload

 *

 * Return: The value of the key if found, otherwise 0.

 */

u32 adreno_hwsched_get_payload_rb_key(struct adreno_device *adreno_dev, u32 rb_id, u32 key);



/**

 * adreno_hwsched_get_payload_rb_key_legacy - Same as adreno_hwsched_get_payload_rb_key,

 * but for legacy

 * @adreno_dev: Pointer to the adreno device

 * @rb_id: Ring buffer ID to search for

 * @key: Key to retrieve from the payload

 *

 * Return: The value of the key if found, otherwise 0.

 */

u32 adreno_hwsched_get_payload_rb_key_legacy(struct adreno_device *adreno_dev, u32 rb_id, u32 key);



/**

 * adreno_hwsched_gpu_fault - Gets hwsched gpu fault info

 * @adreno_dev: pointer to the adreno device

 *

 * Returns zero for hwsched fault else non zero value

 */

u32 adreno_hwsched_gpu_fault(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_log_remove_pending_fences - Log and remove any pending hardware fences if soccp

 * vote failed

 * @adreno_dev: pointer to the adreno device

 * @dev: Pointer to the gmu pdev device

 */

void adreno_hwsched_log_remove_pending_hw_fences(struct adreno_device *adreno_dev,

	struct device *dev);



/**

 * adreno_hwsched_syncobj_kfence_put - Put back kfence context refcounts for this sync object

 * @syncobj: Pointer to the sync object

 *

 */

void adreno_hwsched_syncobj_kfence_put(struct kgsl_drawobj_sync *syncobj);



/**

 * adreno_hwsched_log_nonfatal_gpu_fault - Logs non fatal GPU error from context bad hfi packet

 * @adreno_dev: pointer to the adreno device

 * @dev: Pointer to the struct device for the GMU platform device

 * @error: Types of error that triggered from context bad HFI

 *

 * This function parses context bad hfi packet and logs error information.

 *

 * Return: True for non fatal error code else false.

 */

bool adreno_hwsched_log_nonfatal_gpu_fault(struct adreno_device *adreno_dev,

		struct device *dev, u32 error);



/**

 * adreno_hwsched_poll_msg_queue_write_index - Poll on write index of HFI message queue

 * @hfi_mem: Memory descriptor for HFI queue table

 *

 * Returns zero if write index advances or ETIMEDOUT if timed out polling

 */

int adreno_hwsched_poll_msg_queue_write_index(struct kgsl_memdesc *hfi_mem);



/**

 * adreno_hwsched_remove_hw_fence_entry - Remove hardware fence entry

 * @adreno_dev: pointer to the adreno device

 * @entry: Pointer to the hardware fence entry

 */

void adreno_hwsched_remove_hw_fence_entry(struct adreno_device *adreno_dev,

	struct adreno_hw_fence_entry *entry);



/**

 * adreno_gmu_context_queue_read - Read data from context queue

 * @drawctxt: Pointer to the adreno draw context

 * @output: Pointer to read the data into

 * @read_idx: Index to read the data from

 * @size: Number of dwords to read from the context queue

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_gmu_context_queue_read(struct adreno_context *drawctxt, u32 *output,

	u32 read_idx, u32 size);



/**

 * adreno_gmu_context_queue_write - Write data to context queue

 *

 * @adreno_dev: Pointer to adreno device structure

 * @gmu_context_queue: Pointer to the memory descriptor for context queue

 * @msg: Pointer to the message data to be written

 * @size_bytes: Size of the message data in bytes

 * @drawobj: Pointer to the draw object

 * @time: Pointer to the submission time information

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_gmu_context_queue_write(struct adreno_device *adreno_dev,

	struct kgsl_memdesc *gmu_context_queue, u32 *msg, u32 size_bytes,

	struct kgsl_drawobj *drawobj, struct adreno_submit_time *time);



/**

 * adreno_hwsched_add_profile_events - Add profiling events

 *

 * @adreno_dev: Pointer to the adreno device structure

 * @cmdobj: Pointer to the command object

 * @time: Pointer to the submission time information

 */

void adreno_hwsched_add_profile_events(struct adreno_device *adreno_dev,

	struct kgsl_drawobj_cmd *cmdobj, struct adreno_submit_time *time);



/**

 * adreno_hwsched_log_profiling_info - Log profiling information for a retired

 * command batch

 * @adreno_dev: Pointer to the adreno device structure

 * @rcvd: Pointer to the received HFI timestamp retire command

 */

void adreno_hwsched_log_profiling_info(struct adreno_device *adreno_dev, u32 *rcvd);



/**

 * adreno_hwsched_drawobj_replay - Check drawobj need to be replayed or not

 * @adreno_dev: Pointer to the adreno device structure

 * @drawobj: Pointer to the draw object

 *

 * Return true if drawobj needs to replayed, false otherwise

 */

bool adreno_hwsched_drawobj_replay(struct adreno_device *adreno_dev,

	struct kgsl_drawobj *drawobj);



/**

 * adreno_hwsched_get_rb_hostptr - Get the host pointer for a given GPU address

 * @adreno_dev: Pointer to the adreno device

 * @gpuaddr: GPU address to look up

 * @size: Size of the memory region

 *

 * This function retrieves the host pointer corresponding to a given GPU address.

 * It searches through the memory allocation table to find the matching memory

 * descriptor and calculates the host pointer offset.

 *

 * Return: Host pointer if found, or NULL if the GPU address is not found in the

 * memory allocation table.

 */

void *adreno_hwsched_get_rb_hostptr(struct adreno_device *adreno_dev,

	u64 gpuaddr, u32 size);



/**

 * adreno_hwsched_reset_hfi_mem - Reset HFI memory records

 * @adreno_dev: Pointer to the adreno device

 *

 * GMU expects hfi memory records to be clear during bootup. This function

 * iterates through the memory allocation table and resets entries with

 * HFI_MEMFLAG_HOST_INIT set.

 */

void adreno_hwsched_reset_hfi_mem(struct adreno_device *adreno_dev);



/**

 * adreno_hwsched_context_init - Function for context creation

 * @drawctxt: Pointer to the adreno context

 *

 * Allocate resources at the time of context creation

 *

 * Return: Zero on success or negative error on failure

 */

int adreno_hwsched_context_init(struct adreno_context *drawctxt);



/*

 * adreno_hwsched_retire_cmdlist_obj - helper function for cleaning up cmd_list_obj

 * @adreno_dev: Pointer to the adreno device

 * @obj: pointer to cmd_list_obj

 *

 * Helper function to remove node from cmd_list.

 *

 */

void adreno_hwsched_retire_cmdlist_obj(struct adreno_device *adreno_dev,

	struct cmd_list_obj *obj);

#endif





==========================================================================================
FILE: adreno_hwsched_snapshot.c
LINES: 319 | BYTES: 9815
==========================================================================================

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

/*

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

 */



#include "adreno.h"

#include "adreno_hwsched_snapshot.h"

#include "adreno_snapshot.h"



size_t adreno_hwsched_snapshot_rb(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv)

{

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

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

	struct kgsl_memdesc *rb = (struct kgsl_memdesc *)priv;



	if (remain < rb->size + sizeof(*header)) {

		SNAPSHOT_ERR_NOMEM(device, "RB");

		return 0;

	}



	header->start = 0;

	header->end = rb->size >> 2;

	header->rptr = 0;

	header->rbsize = rb->size >> 2;

	header->count = rb->size >> 2;

	header->timestamp_queued = 0;

	header->timestamp_retired = 0;

	header->gpuaddr = rb->gpuaddr;

	header->id = 0;



	memcpy(data, rb->hostptr, rb->size);



	return rb->size + sizeof(*header);

}



static u32 copy_gpu_global(void *out, void *in, u32 size)

{

	if (out && in) {

		memcpy(out, in, size);

		return size;

	}



	return 0;

}



static void adreno_hwsched_snapshot_rb_payload(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot, struct payload_section *payload)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_snapshot_section_header *section_header =

		(struct kgsl_snapshot_section_header *)snapshot->ptr;

	u8 *buf = snapshot->ptr + sizeof(*section_header);

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

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

	u32 size = adreno_hwsched_parse_payload(payload, KEY_RB_SIZEDWORDS) << 2;

	u64 lo, hi, gpuaddr;

	void *rb_hostptr = NULL;

	char str[16];



	lo = adreno_hwsched_parse_payload(payload, KEY_RB_GPUADDR_LO);

	hi = adreno_hwsched_parse_payload(payload, KEY_RB_GPUADDR_HI);

	gpuaddr = hi << 32 | lo;



	/* Sanity check to make sure there is enough for the header */

	if (snapshot->remain < sizeof(*section_header))

		goto err;



	rb_hostptr = adreno_hwsched_get_rb_hostptr(adreno_dev, gpuaddr, size);



	if (rb_hostptr == NULL)

		goto err;



	/* If the gpuaddress and size don't match any allocation, then abort */

	if (((snapshot->remain - sizeof(*section_header)) < (size + sizeof(*header))) ||

		!copy_gpu_global(data, rb_hostptr, size))

		goto err;



	if (device->dump_all_ibs) {

		u64 rbaddr, lpac_rbaddr;



		adreno_readreg64(adreno_dev, ADRENO_REG_CP_RB_BASE,

			ADRENO_REG_CP_RB_BASE_HI, &rbaddr);



		adreno_readreg64(adreno_dev, ADRENO_REG_CP_LPAC_RB_BASE,

			ADRENO_REG_CP_LPAC_RB_BASE_HI, &lpac_rbaddr);



		/* Parse all IBs from current RB */

		if ((rbaddr == gpuaddr) || (lpac_rbaddr == gpuaddr))

			adreno_snapshot_dump_all_ibs(device, rb_hostptr, snapshot);

	}



	header->start = 0;

	header->end = size >> 2;

	header->rptr = adreno_hwsched_parse_payload(payload, KEY_RB_RPTR);

	header->wptr = adreno_hwsched_parse_payload(payload, KEY_RB_WPTR);

	header->rbsize = size >> 2;

	header->count = size >> 2;

	header->timestamp_queued = adreno_hwsched_parse_payload(payload,

			KEY_RB_QUEUED_TS);

	header->timestamp_retired = adreno_hwsched_parse_payload(payload,

			KEY_RB_RETIRED_TS);

	header->gpuaddr = gpuaddr;

	header->id = adreno_hwsched_parse_payload(payload, KEY_RB_ID);



	section_header->magic = SNAPSHOT_SECTION_MAGIC;

	section_header->id = KGSL_SNAPSHOT_SECTION_RB_V2;

	section_header->size = size + sizeof(*header) + sizeof(*section_header);



	snapshot->ptr += section_header->size;

	snapshot->remain -= section_header->size;

	snapshot->size += section_header->size;



	return;

err:

	snprintf(str, sizeof(str), "RB addr:0x%llx", gpuaddr);

	SNAPSHOT_ERR_NOMEM(device, str);

}



bool adreno_hwsched_parse_payload_rb_legacy(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot)

{

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

	u32 i = 0, payload_bytes;

	void *start;

	bool ret = false;



	/* Skip if we didn't receive a context bad HFI */

	if (!cmd->hdr)

		return false;



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

			offsetof(struct hfi_context_bad_cmd_legacy, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == PAYLOAD_RB) {

			adreno_hwsched_snapshot_rb_payload(adreno_dev, snapshot, payload);

			ret = true;

		}



		i += sizeof(*payload) + (payload->dwords << 2);

	}



	return ret;

}



bool adreno_hwsched_parse_payload_rb(struct adreno_device *adreno_dev,

	struct kgsl_snapshot *snapshot)

{

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

	u32 i = 0, payload_bytes;

	void *start;

	bool ret = false;



	/* Skip if we didn't receive a context bad HFI */

	if (!cmd->hdr)

		return false;



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

			offsetof(struct hfi_context_bad_cmd, payload);



	start = &cmd->payload[0];



	while (i < payload_bytes) {

		struct payload_section *payload = start + i;



		if (payload->type == PAYLOAD_RB) {

			adreno_hwsched_snapshot_rb_payload(adreno_dev,

							snapshot, payload);

			ret = true;

		}



		i += sizeof(*payload) + (payload->dwords << 2);

	}



	return ret;

}



size_t adreno_hwsched_snapshot_aqe_buffer(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv)

{

	struct kgsl_memdesc *memdesc = priv;



	struct kgsl_snapshot_gpu_object_v2 *header =

		(struct kgsl_snapshot_gpu_object_v2 *)buf;



	u8 *ptr = buf + sizeof(*header);



	if (IS_ERR_OR_NULL(memdesc) || memdesc->size == 0)

		return 0;



	if (remain < (memdesc->size + sizeof(*header))) {

		SNAPSHOT_ERR_NOMEM(device, "AQE BUFFER");

		return 0;

	}



	header->size = memdesc->size >> 2;

	header->gpuaddr = memdesc->gpuaddr;

	header->ptbase = MMU_DEFAULT_TTBR0(device);

	header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;



	memcpy(ptr, memdesc->hostptr, memdesc->size);



	return memdesc->size + sizeof(*header);

}



static int snapshot_context_queue(int id, void *ptr, void *data)

{

	struct kgsl_snapshot *snapshot = data;

	struct kgsl_context *context = ptr;

	struct adreno_context *drawctxt = ADRENO_CONTEXT(context);

	struct gmu_mem_type_desc desc;



	if (!context->gmu_registered)

		return 0;



	desc.memdesc = &drawctxt->gmu_context_queue;

	desc.type = SNAPSHOT_GMU_MEM_CONTEXT_QUEUE;

	kgsl_snapshot_add_section(context->device,

		KGSL_SNAPSHOT_SECTION_GMU_MEMORY,

		snapshot, adreno_snapshot_gmu_mem, &desc);



	return 0;

}



void adreno_hwsched_snapshot_context_queue(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot)

{

	if (!adreno_hwsched_context_queue_enabled(ADRENO_DEVICE(device)))

		return;



	read_lock(&device->context_lock);

	idr_for_each(&device->context_idr, snapshot_context_queue, snapshot);

	read_unlock(&device->context_lock);

}



static void snapshot_preemption_record(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot, struct kgsl_memdesc *md, u64 offset, u64 size)

{

	struct kgsl_snapshot_section_header *section_header =

		(struct kgsl_snapshot_section_header *)snapshot->ptr;

	u8 *dest = snapshot->ptr + sizeof(*section_header);

	struct kgsl_snapshot_gpu_object_v2 *header =

		(struct kgsl_snapshot_gpu_object_v2 *)dest;

	u64 ctxt_record_size = min_t(u64, device->snapshot_ctxt_record_size, size);

	size_t section_size;



	if (WARN_RATELIMIT((ctxt_record_size > md->size) ||

		(offset > (md->size - ctxt_record_size)),

		"Invalid preemption context record size: md_size: 0x%llx, ctxt_record_size: 0x%llx\n",

		md->size, ctxt_record_size))

		return;



	section_size = sizeof(*section_header) + sizeof(*header) + ctxt_record_size;

	if (snapshot->remain < section_size) {

		SNAPSHOT_ERR_NOMEM(device, "PREEMPTION RECORD");

		return;

	}



	section_header->magic = SNAPSHOT_SECTION_MAGIC;

	section_header->id = KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2;

	section_header->size = section_size;



	header->size = ctxt_record_size >> 2;

	header->gpuaddr = md->gpuaddr + offset;

	header->ptbase =

		kgsl_mmu_pagetable_get_ttbr0(device->mmu.defaultpagetable);

	header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;



	dest += sizeof(*header);



	memcpy(dest, md->hostptr + offset, ctxt_record_size);



	snapshot->ptr += section_header->size;

	snapshot->remain -= section_header->size;

	snapshot->size += section_header->size;

}



void adreno_hwsched_snapshot_preemption_records(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot, struct kgsl_memdesc *md)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	u64 offset = 0, ctxt_record_size = md->size;

	u64 rb0_ctxt_record_size = PAGE_ALIGN(adreno_dev->total_ctxt_record_sz);

	u64 curr_gmem_size = adreno_gmem_size(adreno_dev);

	int i;



	if (ADRENO_FEATURE(adreno_dev, ADRENO_DEFER_GMEM_ALLOC)) {

		struct adreno_hwsched *hwsched = &adreno_dev->hwsched;



		for (i = 0; i < KGSL_PRIORITY_MAX_RB_LEVELS; i++)

			snapshot_preemption_record(device, snapshot,

				hwsched->preempt_rec[i], 0, hwsched->preempt_rec[i]->size);

		return;

	}



	/* Check whether GMU has removed GMEM size from RB0 context record */

	if (md->size == (rb0_ctxt_record_size * KGSL_PRIORITY_MAX_RB_LEVELS)) {

		do_div(ctxt_record_size, KGSL_PRIORITY_MAX_RB_LEVELS);

	} else {

		rb0_ctxt_record_size -= PAGE_ALIGN(curr_gmem_size);

		ctxt_record_size -= rb0_ctxt_record_size;

		do_div(ctxt_record_size, KGSL_PRIORITY_MAX_RB_LEVELS - 1);

	}



	snapshot_preemption_record(device, snapshot, md, offset, rb0_ctxt_record_size);

	offset += rb0_ctxt_record_size;



	/* All preemption records exist as a single mem alloc entry */

	for (i = 1; i < KGSL_PRIORITY_MAX_RB_LEVELS; i++) {

		snapshot_preemption_record(device, snapshot, md,

			offset, ctxt_record_size);

		offset += ctxt_record_size;

	}

}





==========================================================================================
FILE: adreno_rpmh.c
LINES: 276 | BYTES: 6714
==========================================================================================

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

/*

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

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

 */



#include <linux/types.h>

#include <soc/qcom/cmd-db.h>

#include <soc/qcom/tcs.h>



#include "adreno.h"

#include "adreno_rpmh.h"



int adreno_rpmh_arc_cmds(struct rpmh_arc_vals *arc, const char *res_id)

{

	size_t len = 0;

	bool dummy_res = true;



	arc->val = cmd_db_read_aux_data(res_id, &len);



	/*

	 * cmd_db_read_aux_data() gives us a zero-padded table of

	 * size len that contains the arc values. To determine the

	 * number of arc values, we loop through the table and count

	 * them until we get to the end of the buffer or hit the

	 * zero padding.

	 */

	for (arc->num = 1; arc->num < (len >> 1); arc->num++) {

		if (arc->val[arc->num] != 0)

			dummy_res = false;



		if (arc->val[arc->num - 1] != 0 && arc->val[arc->num] == 0)

			break;

	}



	/* Dummy resource entry in cmd_db with all zeros */

	if (dummy_res)

		return -ENODATA;



	return 0;

}



int adreno_rpmh_setup_volt_dependency_tbl(u32 *votes, struct rpmh_arc_vals *pri_rail,

		struct rpmh_arc_vals *sec_rail, u16 *vlvl, u32 num_entries)

{

	int i, j, k;

	uint16_t cur_vlvl;

	bool found_match;



	/*

	 * i tracks current KGSL GPU frequency table entry

	 * j tracks secondary rail voltage table entry

	 * k tracks primary rail voltage table entry

	 */

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

		found_match = false;



		/* Look for a primary rail voltage that matches a VLVL level */

		for (k = 0; k < pri_rail->num; k++) {

			if (pri_rail->val[k] >= vlvl[i]) {

				cur_vlvl = pri_rail->val[k];

				found_match = true;

				break;

			}

		}



		/* If we did not find a matching VLVL level then abort */

		if (!found_match)

			return -EINVAL;



		/*

		 * Look for a secondary rail index whose VLVL value

		 * is greater than or equal to the VLVL value of the

		 * corresponding index of the primary rail

		 */

		for (j = 0; j < sec_rail->num; j++) {

			if (sec_rail->val[j] >= cur_vlvl ||

					j + 1 == sec_rail->num)

				break;

		}



		if (j == sec_rail->num)

			j = 0;



		votes[i] = ARC_VOTE_SET(k, j, cur_vlvl);

	}



	return 0;

}



/* Generate a set of bandwidth votes for the list of BCMs */

static void tcs_cmd_data(struct bcm *bcms, int count, u32 ab, u32 ib, u32 *data,

		u32 perfmode_vote)

{

	int i;



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

		bool valid = true;

		bool commit = false;

		u64 avg, peak, x, y;



		if (i == count - 1 || bcms[i].vcd != bcms[i + 1].vcd)

========== PART_17.txt ==========
			commit = true;



		if (bcms[i].fixed) {

			if (!ab && !ib)

				data[i] = BCM_TCS_CMD(commit, false, 0x0, 0x0);

			else

				data[i] = BCM_TCS_CMD(commit, true, 0x0, perfmode_vote);

			continue;

		}



		/* Multiple the bandwidth by the width of the connection */

		avg = ((u64) ab) * bcms[i].width;



		/* And then divide by the total width */

		do_div(avg, bcms[i].buswidth);



		peak = ((u64) ib) * bcms[i].width;

		do_div(peak, bcms[i].buswidth);



		/* Input bandwidth value is in KBps */

		x = avg * 1000ULL;

		do_div(x, bcms[i].unit);



		/* Input bandwidth value is in KBps */

		y = peak * 1000ULL;

		do_div(y, bcms[i].unit);



		/*

		 * If a bandwidth value was specified but the calculation ends

		 * rounding down to zero, set a minimum level

		 */

		if (ab && x == 0)

			x = 1;



		if (ib && y == 0)

			y = 1;



		x = min_t(u64, x, BCM_TCS_CMD_VOTE_MASK);

		y = min_t(u64, y, BCM_TCS_CMD_VOTE_MASK);



		if (!x && !y)

			valid = false;



		data[i] = BCM_TCS_CMD(commit, valid, x, y);

	}

}



void adreno_rpmh_free_bw_votes(struct rpmh_bw_votes *votes)

{

	int i;



	if (!votes)

		return;



	for (i = 0; votes->cmds && i < votes->num_levels; i++)

		kfree(votes->cmds[i]);



	kfree(votes->cmds);

	kfree(votes->addrs);

	kfree(votes);

}



struct rpmh_bw_votes *adreno_rpmh_build_bw_votes(struct bcm *bcms, int bcm_count,

		u32 *levels, int levels_count, u32 perfmode_vote, u32 perfmode_lvl,

		bool gmu_ab)

{

	struct rpmh_bw_votes *votes;

	int i;



	votes = kzalloc(sizeof(*votes), GFP_KERNEL);

	if (!votes)

		return ERR_PTR(-ENOMEM);



	votes->addrs = kcalloc(bcm_count, sizeof(*votes->cmds), GFP_KERNEL);

	if (!votes->addrs) {

		adreno_rpmh_free_bw_votes(votes);

		return ERR_PTR(-ENOMEM);

	}



	votes->cmds = kcalloc(levels_count, sizeof(*votes->cmds), GFP_KERNEL);

	if (!votes->cmds) {

		adreno_rpmh_free_bw_votes(votes);

		return ERR_PTR(-ENOMEM);

	}



	votes->num_cmds = bcm_count;

	votes->num_levels = levels_count;



	/* Get the cmd-db information for each BCM */

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

		size_t l;

		const struct bcm_data *data;



		data = cmd_db_read_aux_data(bcms[i].name, &l);



		votes->addrs[i] = cmd_db_read_addr(bcms[i].name);



		bcms[i].unit = le32_to_cpu(data->unit);

		bcms[i].width = le16_to_cpu(data->width);

		bcms[i].vcd = data->vcd;

	}



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

		if (i == (bcm_count - 1) || bcms[i].vcd != bcms[i + 1].vcd)

			votes->wait_bitmask |= (1 << i);

	}



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

		votes->cmds[i] = kcalloc(bcm_count, sizeof(u32), GFP_KERNEL);

		if (!votes->cmds[i]) {

			adreno_rpmh_free_bw_votes(votes);

			return ERR_PTR(-ENOMEM);

		}



		tcs_cmd_data(bcms, bcm_count, gmu_ab ? levels[i] : 0x0,

			levels[i], votes->cmds[i],

			(i >= perfmode_lvl) ? perfmode_vote : 0x0);

	}



	return votes;

}



int adreno_rpmh_to_cx_hlvl(struct rpmh_arc_vals *cx_rail, u32 vlvl, u32 *hlvl)

{

	u32 i;



	/*

	 * This means that the Gx level doesn't have a dependency on Cx level.

	 * Return the same value to disable cx voting at GMU.

	 */

	if (vlvl == 0xffffffff) {

		*hlvl = vlvl;

		return 0;

	}



	for (i = 0; i < cx_rail->num; i++) {

		if (cx_rail->val[i] >= vlvl) {

			*hlvl = i;

			return 0;

		}

	}



	return -EINVAL;

}



void adreno_rpmh_build_bw_table_cmd(struct hfi_bwtable_cmd *cmd, struct rpmh_bw_votes *ddr,

		struct rpmh_bw_votes *cnoc)

{

	u32 i, j;



	cmd->bw_level_num = ddr->num_levels;

	cmd->ddr_cmds_num = ddr->num_cmds;

	cmd->ddr_wait_bitmask = ddr->wait_bitmask;



	for (i = 0; i < ddr->num_cmds; i++)

		cmd->ddr_cmd_addrs[i] = ddr->addrs[i];



	for (i = 0; i < ddr->num_levels; i++)

		for (j = 0; j < ddr->num_cmds; j++)

			cmd->ddr_cmd_data[i][j] = (u32) ddr->cmds[i][j];



	if (!cnoc)

		return;



	cmd->cnoc_cmds_num = cnoc->num_cmds;

		cmd->cnoc_wait_bitmask = cnoc->wait_bitmask;



	for (i = 0; i < cnoc->num_cmds; i++)

		cmd->cnoc_cmd_addrs[i] = cnoc->addrs[i];



	for (i = 0; i < cnoc->num_levels; i++)

		for (j = 0; j < cnoc->num_cmds; j++)

			cmd->cnoc_cmd_data[i][j] = (u32) cnoc->cmds[i][j];

}





==========================================================================================
FILE: adreno_rpmh.h
LINES: 122 | BYTES: 3800
==========================================================================================

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

/*

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

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

 */



#ifndef _ADRENO_GMU_RPMH_H

#define _ADRENO_GMU_RPMH_H



struct rpmh_arc_vals {

	u32 num;

	const u16 *val;

};



struct bcm {

	const char *name;

	u32 buswidth;

	u32 channels;

	u32 unit;

	u16 width;

	u8 vcd;

	bool fixed;

};



struct bcm_data {

	__le32 unit;

	__le16 width;

	u8 vcd;

	u8 reserved;

};



struct rpmh_bw_votes {

	u32 wait_bitmask;

	u32 num_cmds;

	u32 *addrs;

	u32 num_levels;

	u32 **cmds;

};



#define ARC_VOTE_SET(pri, sec, vlvl) \

	(FIELD_PREP(GENMASK(31, 16), vlvl) | \

	 FIELD_PREP(GENMASK(15, 8), sec) | \

	 FIELD_PREP(GENMASK(7, 0), pri))



/*

 * List of Bus Control Modules (BCMs) that need to be configured for the GPU

 * to access DDR. For each bus level we will generate a vote each BC

 */

static struct bcm adreno_ddr_bcms[] = {

	{ .name = "SH0", .buswidth = 16 },

	{ .name = "MC0", .buswidth = 4 },

	{ .name = "ACV", .fixed = true },

};



/* Same as above, but for the CNOC BCMs */

static struct bcm adreno_cnoc_bcms[] = {

	{ .name = "CN0", .buswidth = 4 },

};



/**

 * adreno_rpmh_arc_cmds - Read RPMh ARC values from command DB

 * @arc: Pointer to the rpmh_arc_vals structure

 * @res_id: Resource ID for which ARC values are requested

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_rpmh_arc_cmds(struct rpmh_arc_vals *arc, const char *res_id);



/**

 * adreno_rpmh_setup_volt_dependency_tbl - Set up voltage dependency table

 * @votes: Pointer to an array to store the resulting ARC votes

 * @pri_rail: Pointer to the primary rail ARC values

 * @sec_rail: Pointer to the secondary rail ARC values

 * @vlvl: Array of voltage levels corresponding to each frequency corner

 * @num_entries: Number of entries in the voltage dependency table

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_rpmh_setup_volt_dependency_tbl(u32 *votes, struct rpmh_arc_vals *pri_rail,

		struct rpmh_arc_vals *sec_rail, u16 *vlvl, u32 num_entries);



/**

 * adreno_rpmh_free_bw_votes - Free memory associated with RPMh bandwidth votes

 * @votes: Pointer to the rpmh_bw_votes structure

 */

void adreno_rpmh_free_bw_votes(struct rpmh_bw_votes *votes);



/**

 * adreno_rpmh_build_bw_votes - Build the votes table from the specified

 * bandwidth levels

 * @bcms: Array of BCM structures representing BCMs

 * @bcm_count: Number of BCMs in the array

 * @levels: Array of performance levels

 * @levels_count: Number of performance levels

 * @perfmode_vote: Performance mode vote

 * @perfmode_lvl: Initial performance level for performance mode vote

 * @gmu_ab: Indicate if GMU supports AB vote

 *

 * Return: Pointer to the rpmh_bw_votes structure on success, or an error pointer failure

 */

struct rpmh_bw_votes *adreno_rpmh_build_bw_votes(struct bcm *bcms, int bcm_count,

	u32 *levels, int levels_count, u32 perfmode_vote, u32 perfmode_lvl, bool gmu_ab);



/**

 * adreno_rpmh_to_cx_hlvl - Convert RPMh VLVL to CX HLVL level

 * @cx_rail: Pointer to the RPMh ARC values for the CX rail

 * @vlvl: Voltage level to convert

 * @hlvl: Pointer to store the resulting CX level

 *

 * Return: 0 on success or negative error on failure

 */

int adreno_rpmh_to_cx_hlvl(struct rpmh_arc_vals *cx_rail, u32 vlvl, u32 *hlvl);



/**

 * adreno_rpmh_build_bw_table_cmd - Build bandwidth table command

 * @cmd: Pointer to the hfi_bwtable_cmd structure

 * @ddr: Pointer to the DDR RPMh bandwidth votes

 * @cnoc: Pointer to the CNOC RPMh bandwidth votes

 */

void adreno_rpmh_build_bw_table_cmd(struct hfi_bwtable_cmd *cmd, struct rpmh_bw_votes *ddr,

		struct rpmh_bw_votes *cnoc);

#endif





==========================================================================================
FILE: adreno_snapshot.c
LINES: 1466 | BYTES: 44456
==========================================================================================

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

/*

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

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

 */



#include <linux/utsname.h>



#include "adreno.h"

#include "adreno_cp_parser.h"

#include "adreno_pm4types.h"

#include "adreno_snapshot.h"



/* Maintain a list of the objects we see during parsing */



#define SNAPSHOT_OBJ_BUFSIZE 64



/* Used to print error message if an IB has too many objects in it */

static int ib_max_objs;



struct snapshot_rb_params {

	struct kgsl_snapshot *snapshot;

	struct adreno_ringbuffer *rb;

};



/* Keep track of how many bytes are frozen after a snapshot and tell the user */

static size_t snapshot_frozen_objsize;



static struct kgsl_snapshot_object objbuf[SNAPSHOT_OBJ_BUFSIZE];



/* Pointer to the next open entry in the object list */

static unsigned int objbufptr;



static inline int adreno_rb_ctxtswitch(struct adreno_device *adreno_dev,

				   unsigned int *cmd)

{

	return cmd[0] == cp_packet(adreno_dev, CP_NOP, 1) &&

		cmd[1] == CONTEXT_TO_MEM_IDENTIFIER;

}



/* Push a new buffer object onto the list */

void kgsl_snapshot_push_object(struct kgsl_device *device,

		struct kgsl_process_private *process,

		uint64_t gpuaddr, uint64_t dwords)

{

	int index;

	struct kgsl_mem_entry *entry;



	if (process == NULL || gpuaddr == 0)

		return;



	/*

	 * Sometimes IBs can be reused in the same dump.  Because we parse from

	 * oldest to newest, if we come across an IB that has already been used,

	 * assume that it has been reused and update the list with the newest

	 * size.

	 */



	for (index = 0; index < objbufptr; index++) {

		if (objbuf[index].gpuaddr == gpuaddr &&

			objbuf[index].entry->priv == process) {

			/*

			 * Check if newly requested size is within the

			 * allocated range or not, otherwise continue

			 * with previous size.

			 */

			if (!kgsl_gpuaddr_in_memdesc(

				&objbuf[index].entry->memdesc,

				gpuaddr, dwords << 2)) {

				dev_err(device->dev,

					"snapshot: gpuaddr 0x%016llX size is less than requested\n",

					gpuaddr);

				return;

			}



			objbuf[index].size = max_t(uint64_t,

						objbuf[index].size,

						dwords << 2);

			return;

		}

	}



	if (objbufptr == SNAPSHOT_OBJ_BUFSIZE) {

		dev_err(device->dev, "snapshot: too many snapshot objects\n");

		return;

	}



	entry = kgsl_sharedmem_find(process, gpuaddr);

	if (entry == NULL) {

		dev_err(device->dev,

			"snapshot: Can't find entry for 0x%016llX\n", gpuaddr);

		return;

	}



	/*

	 * In some gpu fault scenarios incorrect dword size resulting to return

	 * without putting IB addrs into the list. Hence update IB dword size

	 * within memdesc size to have IB dump in snapshot.

	 */

	if ((gpuaddr + (dwords << 2)) > (entry->memdesc.gpuaddr + entry->memdesc.size))

		dwords = (entry->memdesc.size - (gpuaddr - entry->memdesc.gpuaddr)) >> 2;



	if (!kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, dwords << 2)) {

		dev_err(device->dev,

			"snapshot: Mem entry 0x%016llX is too small\n",

			gpuaddr);

		kgsl_mem_entry_put(entry);

		return;

	}



	/* Put it on the list of things to parse */

	objbuf[objbufptr].gpuaddr = gpuaddr;

	objbuf[objbufptr].size = dwords << 2;

	objbuf[objbufptr++].entry = entry;

}



/*

 * Returns index of the specified object is already on the list of buffers

 * to be dumped

 */



static int find_object(uint64_t gpuaddr, struct kgsl_process_private *process)

{

	int index;



	for (index = 0; index < objbufptr; index++) {

		if (objbuf[index].gpuaddr == gpuaddr &&

			objbuf[index].entry->priv == process)

			return index;

	}

	return -ENOENT;

}



/*

 * snapshot_freeze_obj_list() - Take a list of ib objects and freeze their

 * memory for snapshot

 * @snapshot: The snapshot data.

 * @process: The process to which the IB belongs

 * @ib_obj_list: List of the IB objects

 *

 * Returns 0 on success else error code

 */

static int snapshot_freeze_obj_list(struct kgsl_snapshot *snapshot,

		struct kgsl_process_private *process,

		struct adreno_ib_object_list *ib_obj_list)

{

	int ret = 0;

	struct adreno_ib_object *ib_objs;

	int i;



	for (i = 0; i < ib_obj_list->num_objs; i++) {

		int temp_ret;

		int index;

		int freeze = 1;



		ib_objs = &(ib_obj_list->obj_list[i]);

		/* Make sure this object is not going to be saved statically */

		for (index = 0; index < objbufptr; index++) {

			if ((objbuf[index].gpuaddr <= ib_objs->gpuaddr) &&

				((objbuf[index].gpuaddr +

				(objbuf[index].size)) >=

				(ib_objs->gpuaddr + ib_objs->size)) &&

				(objbuf[index].entry->priv == process)) {

				freeze = 0;

				CLEAR_FLAG(KGSL_MEMDESC_SKIP_RECLAIM,

					&(objbuf[index].entry->memdesc.priv));

				break;

			}

		}



		if (freeze) {

			temp_ret = kgsl_snapshot_get_object(snapshot,

					    process, ib_objs->gpuaddr,

					    ib_objs->size,

					    ib_objs->snapshot_obj_type);

			if (temp_ret < 0) {

				if (ret >= 0)

					ret = temp_ret;

			} else {

				snapshot_frozen_objsize += temp_ret;

			}

		}

	}

	return ret;

}



static inline bool adreno_ib_addr_overlap(u64 ibaddr, u64 gpuaddr, u64 dwords)

{

	u64 size = dwords << 2;



	if (size > (U64_MAX - gpuaddr))

		return false;



	return (ibaddr >= gpuaddr && ibaddr < (gpuaddr + size));

}



void adreno_parse_ib(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot,

		struct kgsl_process_private *process,

		uint64_t gpuaddr, uint64_t dwords)

{

	struct adreno_ib_object_list *ib_obj_list;



	/*

	 * Check the IB address - if it is either the last executed IB1

	 * then push it into the static blob otherwise put it in the dynamic

	 * list

	 */

	if (adreno_ib_addr_overlap(snapshot->ib1base, gpuaddr, dwords)) {

		/*

		 * During restore after preemption, ib1base in the register

		 * can be updated by CP. In such scenarios, to dump complete

		 * IB1 in snapshot, we should consider ib1base from ringbuffer.

		 */

		snapshot->ib1base = gpuaddr;

		snapshot->ib1size = dwords;

		kgsl_snapshot_push_object(device, process, gpuaddr, dwords);

		return;

	}



	if (kgsl_snapshot_have_object(snapshot, process,

					gpuaddr, dwords << 2))

		return;



	if (-E2BIG == adreno_ib_create_object_list(device, process,

				gpuaddr, dwords, snapshot->ib2base,

				&ib_obj_list))

		ib_max_objs = 1;



	if (ib_obj_list)

		kgsl_snapshot_add_ib_obj_list(snapshot, ib_obj_list);



}



void adreno_parse_ib_lpac(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot,

		struct kgsl_process_private *process,

		u64 gpuaddr, u64 dwords)

{

	struct adreno_ib_object_list *ib_obj_list;



	/*

	 * Check the IB address - if it is either the last executed IB1

	 * then push it into the static blob otherwise put it in the dynamic

	 * list

	 */

	if (adreno_ib_addr_overlap(snapshot->ib1base_lpac, gpuaddr, dwords)) {

		/*

		 * During restore after preemption, ib1base in the register

		 * can be updated by CP. In such scenarios, to dump complete

		 * IB1 in snapshot, we should consider ib1base from ringbuffer.

		 */

		snapshot->ib1base_lpac = gpuaddr;

		snapshot->ib1size_lpac = dwords;

		kgsl_snapshot_push_object(device, process, gpuaddr, dwords);

		return;

	}



	if (kgsl_snapshot_have_object(snapshot, process,

					gpuaddr, dwords << 2))

		return;



	if (-E2BIG == adreno_ib_create_object_list(device, process,

				gpuaddr, dwords, snapshot->ib2base_lpac,

				&ib_obj_list))

		ib_max_objs = 1;



	if (ib_obj_list)

		kgsl_snapshot_add_ib_obj_list(snapshot, ib_obj_list);



}



void adreno_snapshot_dump_all_ibs(struct kgsl_device *device,

			unsigned int *rbptr, struct kgsl_snapshot *snapshot)

{

	int index = 0;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct kgsl_iommu *iommu = KGSL_IOMMU(device);



	for (index = 0; index < KGSL_RB_DWORDS;) {



		if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {

			uint64_t ibaddr;

			uint64_t ibsize;



			ibaddr = rbptr[(index + 2) % KGSL_RB_DWORDS];

			ibaddr = ibaddr << 32 | rbptr[(index + 1) % KGSL_RB_DWORDS];

			ibsize = rbptr[(index + 3) % KGSL_RB_DWORDS];

			index += 4;



			/* Don't parse known global IBs */

			if (kgsl_gpuaddr_in_memdesc(iommu->setstate,

				ibaddr, ibsize))

				continue;



			if (kgsl_gpuaddr_in_memdesc(adreno_dev->pwron_fixup,

				ibaddr, ibsize))

				continue;



			adreno_parse_ib(device, snapshot, snapshot->process,

				ibaddr, ibsize);

		} else

			index = index + 1;

	}

}



/**

 * snapshot_rb_ibs() - Dump rb data and capture the IB's in the RB as well

 * @device: Pointer to a KGSL device

 * @rb: The RB to dump

 * @data: Pointer to memory where the RB data is to be dumped

 * @snapshot: Pointer to information about the current snapshot being taken

 */

static void snapshot_rb_ibs(struct kgsl_device *device,

		struct adreno_ringbuffer *rb,

		struct kgsl_snapshot *snapshot)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	unsigned int *rbptr, rptr = adreno_get_rptr(rb);

	int index, i;

	int parse_ibs = 0, ib_parse_start;



	/*

	 * During IB parse, vmalloc is called which can sleep and

	 * should not be called from atomic context. Since IBs are not

	 * dumped during atomic snapshot, there is no need to parse it.

	 */

	if (device->snapshot_atomic)

		return;



	rbptr = rb->buffer_desc->hostptr;

	/*

	 * KGSL tries to dump the active IB first. If it is not present, then

	 * only it dumps all the IBs. In few cases, non-active IBs may help to

	 * establish the flow and understand the hardware state better.

	 */

	if (device->dump_all_ibs) {

		adreno_snapshot_dump_all_ibs(device, rbptr, snapshot);

		return;

	}



	/*

	 * Figure out the window of ringbuffer data to dump.  First we need to

	 * find where the last processed IB ws submitted.  Start walking back

	 * from the rptr

	 */

	index = rptr;



	do {

		index--;



		if (index < 0) {

			index = KGSL_RB_DWORDS - 4;



			/* We wrapped without finding what we wanted */

			if (index < rb->wptr) {

				index = rb->wptr;

				break;

			}

		}



		if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {

			uint64_t ibaddr;

			uint64_t ibsize;



			ibaddr = rbptr[index + 2];

			ibaddr = ibaddr << 32 | rbptr[index + 1];

			ibsize = rbptr[index + 3];



			if (adreno_ib_addr_overlap(snapshot->ib1base,

					ibaddr, ibsize)) {

				/*

				 * During restore after preemption, ib1base in

				 * the register can be updated by CP. In such

				 * scenario, to dump complete IB1 in snapshot,

				 * we should consider ib1base from ringbuffer.

				 */

				snapshot->ib1base = ibaddr;

				snapshot->ib1size = ibsize;

				break;

			}

		}

	} while (index != rb->wptr);



	/*

	 * If the ib1 was not found, for example, if ib1base was restored

	 * incorrectly after preemption, then simply dump the entire

	 * ringbuffer along with all the IBs in the ringbuffer.

	 */



	if (index == rb->wptr) {

		adreno_snapshot_dump_all_ibs(device, rb->buffer_desc->hostptr, snapshot);

		return;

	}



	/*

	 * index points at the last submitted IB. We can only trust that the

	 * memory between the context switch and the hanging IB is valid, so

	 * the next step is to find the context switch before the submission

	 */



	while (index != rb->wptr) {

		index--;



		if (index < 0) {

			index = KGSL_RB_DWORDS - 2;



			/*

			 * Wrapped without finding the context switch. This is

			 * harmless - we should still have enough data to dump a

			 * valid state

			 */



			if (index < rb->wptr) {

				index = rb->wptr;

				break;

			}

		}



		/* Break if the current packet is a context switch identifier */

		if ((rbptr[index] == cp_packet(adreno_dev, CP_NOP, 1)) &&

			(rbptr[index + 1] == CONTEXT_TO_MEM_IDENTIFIER))

			break;

	}



	/*

	 * Index represents the start of the window of interest.  We will try

	 * to dump all buffers between here and the rptr

	 */



	ib_parse_start = index;



	/*

	 * Loop through the RB, looking for indirect buffers and MMU pagetable

	 * changes

	 */



	index = rb->wptr;

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

		/*

		 * Only parse IBs between the start and the rptr or the next

		 * context switch, whichever comes first

		 */



		if (parse_ibs == 0 && index == ib_parse_start)

			parse_ibs = 1;

		else if (index == rptr || adreno_rb_ctxtswitch(adreno_dev,

							&rbptr[index]))

			parse_ibs = 0;



		if (parse_ibs && adreno_cmd_is_ib(adreno_dev, rbptr[index])) {

			struct kgsl_iommu *iommu = KGSL_IOMMU(device);

			uint64_t ibaddr;

			uint64_t ibsize;



			ibaddr = rbptr[index + 2];

			ibaddr = ibaddr << 32 | rbptr[index + 1];

			ibsize = rbptr[index + 3];



			index = (index + 1) % KGSL_RB_DWORDS;



			/* Don't parse known global IBs */

			if (kgsl_gpuaddr_in_memdesc(iommu->setstate,

				ibaddr, ibsize))

				continue;



			if (kgsl_gpuaddr_in_memdesc(adreno_dev->pwron_fixup,

				ibaddr, ibsize))

				continue;



			adreno_parse_ib(device, snapshot, snapshot->process,

				ibaddr, ibsize);

		} else

			index = (index + 1) % KGSL_RB_DWORDS;

	}



}



/* Snapshot the ringbuffer memory */

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

	size_t remain, void *priv)

{

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

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

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct snapshot_rb_params *snap_rb_params = priv;

	struct kgsl_snapshot *snapshot = snap_rb_params->snapshot;

	struct adreno_ringbuffer *rb = snap_rb_params->rb;



	/*

	 * Dump the entire ringbuffer - the parser can choose how much of it to

	 * process

	 */



	if (remain < KGSL_RB_SIZE + sizeof(*header)) {

		dev_err(device->dev,

			"snapshot: Not enough memory for the rb section\n");

		return 0;

	}



	/* Write the sub-header for the section */

	header->start = 0;

	header->end = KGSL_RB_DWORDS;

	header->wptr = rb->wptr;

	header->rptr = adreno_get_rptr(rb);

	header->rbsize = KGSL_RB_DWORDS;

	header->count = KGSL_RB_DWORDS;

	adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_QUEUED,

					&header->timestamp_queued);

	adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_RETIRED,

					&header->timestamp_retired);

	header->gpuaddr = rb->buffer_desc->gpuaddr;

	header->id = rb->id;



	if (rb == adreno_dev->cur_rb)

		snapshot_rb_ibs(device, rb, snapshot);



	/* Just copy the ringbuffer, there are no active IBs */

	memcpy(data, rb->buffer_desc->hostptr, KGSL_RB_SIZE);



	/* Return the size of the section */

	return KGSL_RB_SIZE + sizeof(*header);

}



static int _count_mem_entries(int id, void *ptr, void *data)

{

	int *count = data;

	*count = *count + 1;

	return 0;

}



struct mem_entry {

	uint64_t gpuaddr;

	uint64_t size;

	unsigned int type;

} __packed;



static size_t snapshot_capture_mem_list(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv)

{

	struct kgsl_snapshot_mem_list_v2 *header =

		(struct kgsl_snapshot_mem_list_v2 *)buf;

	int id, index = 0, ret = 0, num_mem = 0;

	struct kgsl_process_private *process = priv;

	struct mem_entry *m = (struct mem_entry *)(buf + sizeof(*header));

	struct kgsl_mem_entry *entry;



	/* we need a process to search! */

	if (process == NULL)

		return 0;



	spin_lock(&process->mem_lock);



	/* We need to know the number of memory objects that the process has */

	idr_for_each(&process->mem_idr, _count_mem_entries, &num_mem);



	if (num_mem == 0)

		goto out;



	if (remain < ((num_mem * sizeof(struct mem_entry)) + sizeof(*header))) {

		dev_err(device->dev,

			"snapshot: Not enough memory for the mem list\n");

		goto out;

	}



	header->num_entries = num_mem;

	header->ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);



	/*

	 * Walk through the memory list and store the

	 * tuples(gpuaddr, size, memtype) in snapshot

	 */

	idr_for_each_entry(&process->mem_idr, entry, id) {

		m[index].gpuaddr = entry->memdesc.gpuaddr;

		m[index].size = entry->memdesc.size;

		m[index].type = kgsl_memdesc_get_memtype(&entry->memdesc);

		index++;

	}



	ret = sizeof(*header) + (num_mem * sizeof(struct mem_entry));

out:

	spin_unlock(&process->mem_lock);

	return ret;

}



static void kgsl_snapshot_add_active_ib_obj_list(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{

	struct adreno_ib_object_list *ib_obj_list;

	int index = -ENOENT;



	if (!snapshot->ib1dumped)

		index = find_object(snapshot->ib1base, snapshot->process);



	/* only do this for IB1 because the IB2's are part of IB1 objects */

	if ((index != -ENOENT) &&

			(snapshot->ib1base == objbuf[index].gpuaddr)) {

		if (-E2BIG == adreno_ib_create_object_list(device,

					objbuf[index].entry->priv,

					objbuf[index].gpuaddr,

					objbuf[index].size >> 2,

					snapshot->ib2base,

					&ib_obj_list))

			ib_max_objs = 1;

		if (ib_obj_list) {

			/* freeze the IB objects in the IB */

			snapshot_freeze_obj_list(snapshot,

					objbuf[index].entry->priv,

					ib_obj_list);

			adreno_ib_destroy_obj_list(ib_obj_list);

		}

	} else {

		/* Get the IB2 index from parsed object */

		index = find_object(snapshot->ib2base, snapshot->process);



		if (index != -ENOENT)

			adreno_parse_ib(device, snapshot, snapshot->process,

				snapshot->ib2base, objbuf[index].size >> 2);

	}

}



static void kgsl_snapshot_add_active_lpac_ib_obj_list(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{

	struct adreno_ib_object_list *ib_obj_list;

	int index = -ENOENT;



	if (!snapshot->ib1dumped_lpac)

		index = find_object(snapshot->ib1base_lpac, snapshot->process_lpac);



	/* only do this for IB1 because the IB2's are part of IB1 objects */

	if ((index != -ENOENT) &&

			(snapshot->ib1base_lpac == objbuf[index].gpuaddr)) {

		if (-E2BIG == adreno_ib_create_object_list(device,

					objbuf[index].entry->priv,

					objbuf[index].gpuaddr,

					objbuf[index].size >> 2,

					snapshot->ib2base_lpac,

					&ib_obj_list))

			ib_max_objs = 1;

		if (ib_obj_list) {

			/* freeze the IB objects in the IB */

			snapshot_freeze_obj_list(snapshot,

					objbuf[index].entry->priv,

					ib_obj_list);

			adreno_ib_destroy_obj_list(ib_obj_list);

		}

	} else {

		/* Get the IB2 index from parsed object */

		index = find_object(snapshot->ib2base_lpac, snapshot->process_lpac);



		if (index != -ENOENT)

			adreno_parse_ib_lpac(device, snapshot, snapshot->process_lpac,

				snapshot->ib2base_lpac, objbuf[index].size >> 2);

	}

}



/*

 * active_ib_is_parsed() - Checks if active ib is already parsed

 * @gpuaddr: Active IB base address at the time of fault

 * @size: Active IB size

 * @process: The process to which the IB belongs

 *

 * Function returns true if the active is already is parsed

 * else false

 */

static bool active_ib_is_parsed(uint64_t gpuaddr, uint64_t size,

		struct kgsl_process_private *process)

{

	int  index;

	/* go through the static list for gpuaddr is in list or not */

	for (index = 0; index < objbufptr; index++) {

		if ((objbuf[index].gpuaddr <= gpuaddr) &&

				((objbuf[index].gpuaddr +

				  (objbuf[index].size)) >=

				 (gpuaddr + size)) &&

				(objbuf[index].entry->priv == process))

			return true;

	}

	return false;

}

/* Snapshot the memory for an indirect buffer */

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

	size_t remain, void *priv)

{

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

	struct snapshot_ib_meta *metadata = priv;

	unsigned int *src;

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

	struct adreno_ib_object_list *ib_obj_list;

	struct kgsl_snapshot *snapshot;

	struct kgsl_snapshot_object *obj;

	struct kgsl_memdesc *memdesc;



	if (metadata == NULL || metadata->snapshot == NULL || metadata->obj == NULL) {

		dev_err(device->dev, "snapshot: bad metadata\n");

		return 0;

	}

	snapshot = metadata->snapshot;

	obj = metadata->obj;

	memdesc = &obj->entry->memdesc;



	/* If size is zero get it from the medesc size */

	if (!obj->size)

		obj->size = (memdesc->size - (obj->gpuaddr - memdesc->gpuaddr));



	if (remain < (obj->size + sizeof(*header))) {

		dev_err(device->dev, "snapshot: Not enough memory for the ib\n");

		return 0;

	}



	src = kgsl_gpuaddr_to_vaddr(memdesc, obj->gpuaddr);

	if (src == NULL) {

		dev_err(device->dev,

			     "snapshot: Unable to map GPU memory object 0x%016llX into the kernel\n",

			     obj->gpuaddr);

		return 0;

	}



	/* only do this for IB1 because the IB2's are part of IB1 objects */

	if (metadata->ib1base == obj->gpuaddr) {



		snapshot->ib1dumped = active_ib_is_parsed(obj->gpuaddr,

					obj->size, obj->entry->priv);

		if (-E2BIG == adreno_ib_create_object_list(device,

				obj->entry->priv,

				obj->gpuaddr, obj->size >> 2,

				snapshot->ib2base,

				&ib_obj_list))

			ib_max_objs = 1;

		if (ib_obj_list) {

			/* freeze the IB objects in the IB */

			snapshot_freeze_obj_list(snapshot,

						obj->entry->priv,

						ib_obj_list);

			adreno_ib_destroy_obj_list(ib_obj_list);

		}

	}





	if (metadata->ib2base == obj->gpuaddr)

		snapshot->ib2dumped = active_ib_is_parsed(obj->gpuaddr,

					obj->size, obj->entry->priv);



	if (metadata->ib1base_lpac == obj->gpuaddr) {



		snapshot->ib1dumped_lpac = active_ib_is_parsed(obj->gpuaddr,

					obj->size, obj->entry->priv);

		if (-E2BIG == adreno_ib_create_object_list(device,

				obj->entry->priv,

				obj->gpuaddr, obj->size >> 2,

				snapshot->ib2base_lpac,

				&ib_obj_list))

			ib_max_objs = 1;

		if (ib_obj_list) {

			/* freeze the IB objects in the IB */

			snapshot_freeze_obj_list(snapshot,

						obj->entry->priv,

						ib_obj_list);

			adreno_ib_destroy_obj_list(ib_obj_list);

		}

	}



	if (metadata->ib2base_lpac == obj->gpuaddr)

		snapshot->ib2dumped_lpac = active_ib_is_parsed(obj->gpuaddr,

					obj->size, obj->entry->priv);



	/* Write the sub-header for the section */

	header->gpuaddr = obj->gpuaddr;

	header->ptbase =

		kgsl_mmu_pagetable_get_ttbr0(obj->entry->priv->pagetable);

	header->size = obj->size >> 2;



	/* Write the contents of the ib */

	memcpy((void *)dst, (void *)src, (size_t) obj->size);

	/* Write the contents of the ib */



	return obj->size + sizeof(*header);

}



/* Dump another item on the current pending list */

static void dump_object(struct kgsl_device *device, int obj,

		struct kgsl_snapshot *snapshot)

{

	metadata.snapshot = snapshot;

	metadata.obj = &objbuf[obj];

	metadata.ib1base = snapshot->ib1base;

	metadata.ib1size = snapshot->ib1size;

	metadata.ib2base = snapshot->ib2base;

	metadata.ib2size = snapshot->ib2size;

	metadata.ib3base = snapshot->ib3base;

	metadata.ib3size = snapshot->ib3size;

	metadata.ib1base_lpac = snapshot->ib1base_lpac;

	metadata.ib1size_lpac = snapshot->ib1size_lpac;

	metadata.ib2base_lpac = snapshot->ib2base_lpac;

	metadata.ib2size_lpac = snapshot->ib2size_lpac;



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_IB_V2,

			snapshot, snapshot_ib, &metadata);

	if (objbuf[obj].entry) {

		kgsl_memdesc_unmap(&(objbuf[obj].entry->memdesc));

		kgsl_mem_entry_put(objbuf[obj].entry);

	}

}



/* setup_fault process - Find kgsl_process_private struct that caused the fault

 *

 * Find the faulting process based what the dispatcher thinks happened and

 * what the hardware is using for the current pagetable. The process struct

 * will be used to look up GPU addresses that are encountered while parsing

 * the GPU state.

 */

static struct kgsl_process_private *setup_fault_process(struct kgsl_device *device,

				struct kgsl_snapshot *snapshot,

				struct kgsl_context *context)

{

	u64 hw_ptbase, proc_ptbase;

	struct kgsl_process_private *process = context ? context->proc_priv : NULL;



	if (process != NULL && !kgsl_process_private_get(process))

		process = NULL;



	/* Get the physical address of the MMU pagetable */

	hw_ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu, context);



	/* if we have an input process, make sure the ptbases match */

	if (process) {

		int asid = kgsl_mmu_pagetable_get_asid(process->pagetable, context);



		proc_ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);

		if (asid >= 0)

			proc_ptbase |= FIELD_PREP(GENMASK_ULL(63, KGSL_IOMMU_ASID_START_BIT), asid);



		/* agreement! No need to check further */

		if (hw_ptbase == proc_ptbase)

			goto done;



		kgsl_process_private_put(process);

		process = NULL;

		dev_err(device->dev,

			"snapshot: ptbase mismatch hw %llx sw %llx\n",

			hw_ptbase, proc_ptbase);

	}



	/* try to find the right pagetable by walking the process list */

	if (kgsl_mmu_is_perprocess(&device->mmu)) {

		struct kgsl_process_private *tmp;



		read_lock(&kgsl_driver.proclist_lock);

		list_for_each_entry(tmp, &kgsl_driver.process_list, list) {

			u64 pt_ttbr0;



			pt_ttbr0 = kgsl_mmu_pagetable_get_ttbr0(tmp->pagetable);

			if ((pt_ttbr0 == MMU_SW_PT_BASE(hw_ptbase))

			    && kgsl_process_private_get(tmp)) {

				process = tmp;

				break;

			}

		}

		read_unlock(&kgsl_driver.proclist_lock);

	}

done:

	return process;

}



/* Snapshot a global memory buffer */

size_t adreno_snapshot_global(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv)

{

	struct kgsl_memdesc *memdesc = priv;



	struct kgsl_snapshot_gpu_object_v2 *header =

		(struct kgsl_snapshot_gpu_object_v2 *)buf;



	u8 *ptr = buf + sizeof(*header);



	if (IS_ERR_OR_NULL(memdesc) || memdesc->size == 0)

		return 0;



	if (remain < (memdesc->size + sizeof(*header))) {

		dev_err(device->dev,

			"snapshot: Not enough memory for the memdesc\n");

		return 0;

	}



	if (memdesc->hostptr == NULL) {

		dev_err(device->dev,

		"snapshot: no kernel mapping for global object 0x%016llX\n",

		memdesc->gpuaddr);

		return 0;

	}



	header->size = memdesc->size >> 2;

	header->gpuaddr = memdesc->gpuaddr;

	header->ptbase = MMU_DEFAULT_TTBR0(device);

	header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;



	memcpy(ptr, memdesc->hostptr, memdesc->size);



	return memdesc->size + sizeof(*header);

}



/* Snapshot IOMMU specific buffers */

static void adreno_snapshot_iommu(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct kgsl_iommu *iommu = KGSL_IOMMU(device);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

		snapshot, adreno_snapshot_global, iommu->setstate);



	if (adreno_is_preemption_enabled(adreno_dev))

		kgsl_snapshot_add_section(device,

			KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

			snapshot, adreno_snapshot_global, iommu->smmu_info);

}



static void adreno_snapshot_ringbuffer(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot, struct adreno_ringbuffer *rb)

{

	struct snapshot_rb_params params = {

		.snapshot = snapshot,

		.rb = rb,

	};



	if (rb == NULL)

		return;



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB_V2, snapshot,

		snapshot_rb, &params);

}



static void adreno_snapshot_os(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot, struct kgsl_context *guilty,

		struct kgsl_context *guilty_lpac)

{

	struct kgsl_snapshot_section_header *sect =

		(struct kgsl_snapshot_section_header *) snapshot->ptr;

	struct kgsl_snapshot_linux_v4 *header = (struct kgsl_snapshot_linux_v4 *)

		(snapshot->ptr + sizeof(*sect));

	struct kgsl_context *context;

	u32 remain;

	void *mem;

	int id;



	if (snapshot->remain < (sizeof(*sect) + sizeof(*header))) {

		SNAPSHOT_ERR_NOMEM(device, "OS");

		return;

	}



	header->osid = KGSL_SNAPSHOT_OS_LINUX_V4;



	strscpy(header->release, init_utsname()->release, sizeof(header->release));

	strscpy(header->version, init_utsname()->version, sizeof(header->version));



	header->seconds = ktime_get_real_seconds();

	header->power_flags = device->pwrctrl.power_flags;

	header->power_level = device->pwrctrl.active_pwrlevel;

	header->power_interval_timeout = atomic64_read(&device->pwrctrl.interval_timeout);

	header->grpclk = clk_get_rate(device->pwrctrl.grp_clks[0]);



	/* Get the current PT base */

	header->ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu, guilty);

	header->ptbase_lpac = kgsl_mmu_get_current_ttbr0(&device->mmu, guilty_lpac);

	header->ctxtcount = 0;



	/* If we know the guilty context then dump it */

	if (guilty) {

		header->current_context = guilty->id;

		header->pid = guilty->tid;

		strscpy(header->comm, guilty->proc_priv->comm,

			sizeof(header->comm));

	}



	if (guilty_lpac) {

		header->current_context_lpac = guilty_lpac->id;

		header->pid_lpac = guilty_lpac->tid;

		strscpy(header->comm_lpac, guilty_lpac->proc_priv->comm,

			sizeof(header->comm_lpac));

	}



	remain = snapshot->remain - sizeof(*sect) + sizeof(*header);

	mem = snapshot->ptr + sizeof(*sect) + sizeof(*header);



	read_lock(&device->context_lock);

	idr_for_each_entry(&device->context_idr, context, id) {

		struct kgsl_snapshot_linux_context_v2 *c = mem;



		if (remain < sizeof(*c))

			break;



		kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_QUEUED,

			&c->timestamp_queued);



		kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_CONSUMED,

			&c->timestamp_consumed);



		kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,

			&c->timestamp_retired);



		header->ctxtcount++;



		mem += sizeof(*c);

		remain -= sizeof(*c);

	}

	read_unlock(&device->context_lock);



	sect->magic = SNAPSHOT_SECTION_MAGIC;

	sect->id = KGSL_SNAPSHOT_SECTION_OS;

	sect->size = sizeof(*sect) + sizeof(*header) +

		header->ctxtcount * sizeof(struct kgsl_snapshot_linux_context_v2);



	snapshot->ptr += sect->size;

	snapshot->remain -= sect->size;

	snapshot->size += sect->size;

}



static void adreno_static_ib_dump(struct kgsl_device *device,

				struct kgsl_process_private *process, u64 ib1base,

				u32 ib1size, u64 ib2base, u32 ib2size,

				u64 ib3base, u32 ib3size)

{

	if (process == NULL)

		return;



	 /* Make sure that the last IB1 that was being executed is dumped.

	  * Since this was the last IB1 that was processed, we should have

	  * already added it to the list during the ringbuffer parse but we

	  * want to be double plus sure.

	  * The problem is that IB size from the register is the unprocessed size

	  * of the buffer not the original size, so if we didn't catch this

	  * buffer being directly used in the RB, then we might not be able to

	  * dump the whole thing. Try to dump the maximum possible size from the

	  * IB1 base address till the end of memdesc size so that we dont miss

	  * what we are interested in. Print a warning message so we can try to

	  * figure how often this really happens.

	  */



	if (ib1base && (-ENOENT == find_object(ib1base, process))) {

		struct kgsl_mem_entry *entry;

		u64 ibsize;



		entry = kgsl_sharedmem_find(process, ib1base);

		if (entry == NULL) {

			dev_err(device->dev,

				"Can't find a memory entry containing IB1BASE %16llx\n",

				ib1base);

		} else {

			ibsize = entry->memdesc.size -

				(ib1base - entry->memdesc.gpuaddr);

			kgsl_mem_entry_put(entry);



			kgsl_snapshot_push_object(device, process,

				ib1base, ibsize >> 2);

			dev_err(device->dev,

				"CP_IB1_BASE %16llx is not found in the ringbuffer. Dumping %llx dwords of the buffer\n",

				ib1base, ibsize >> 2);

		}

	}



	/*

	 * Add the last parsed IB2 and IB3 to the list. These should be found as we

	 * parse the objects below, but we try to add them to the list first, so

	 * they too can be parsed. Don't print an error message in this case - if

	 * the IB2 and IB3 are found during parsing, the list will be updated with

	 * the correct size.

	 */

	if (ib2base && (-ENOENT == find_object(ib2base, process)))

		kgsl_snapshot_push_object(device, process, ib2base, ib2size);



	if (ib3base && (-ENOENT == find_object(ib3base, process)))

		kgsl_snapshot_push_object(device, process, ib3base, ib3size);

}



/**

 * adreno_snapshot_sqe -  Dump the SQE firmware version and few dwords

 * @device: Pointer to the kgsl device

 * @buf: Pointer to the snapshot buffer

 * @remain: Remaining size in snapshot buffer

 * @priv: Private pointer to pass to the function

 *

 * Return: Number of bytes written to the snapshot buffer

 */

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

		size_t remain, void *priv)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

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

	void *data = buf + sizeof(*header);

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



	if (remain < DEBUG_SECTION_SZ(SQE_FW_SNAPSHOT_DWORDS)) {

		SNAPSHOT_ERR_NOMEM(device, "SQE VERSION DEBUG");

		return 0;

	}



	/* Dump the SQE firmware version and few dwords */

	header->type = SNAPSHOT_DEBUG_SQE_VERSION;

	header->size = SQE_FW_SNAPSHOT_DWORDS;

	memcpy(data, fw->memdesc->hostptr, (SQE_FW_SNAPSHOT_DWORDS * sizeof(u32)));



	return DEBUG_SECTION_SZ(SQE_FW_SNAPSHOT_DWORDS);

}



/* adreno_snapshot_aqe() - Dump AQE data in snapshot */

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

		size_t remain, void *priv)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

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

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

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



	/*

	 * AQE firmware memory is allocated in the hwsched path.

	 * In swsched path, the address is NULL, so add a check

	 * to avoid NULL pointer dereference.

	 */

	if ((!ADRENO_FEATURE(adreno_dev, ADRENO_AQE)) || (fw->memdesc == NULL))

		return 0;



	if (remain < DEBUG_SECTION_SZ(AQE_FW_SNAPSHOT_DWORDS)) {

		SNAPSHOT_ERR_NOMEM(device, "AQE VERSION DEBUG");

		return 0;

	}



	/* Dump the AQE firmware version and few dwords */

	header->type = SNAPSHOT_DEBUG_AQE_VERSION;

	header->size = AQE_FW_SNAPSHOT_DWORDS;

	memcpy(data, fw->memdesc->hostptr, AQE_FW_SNAPSHOT_DWORDS * sizeof(u32));



	return DEBUG_SECTION_SZ(AQE_FW_SNAPSHOT_DWORDS);

}



static void adreno_snapshot_firmware(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{



	/* Dump SQE firmware version and few dwords */

	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_DEBUG,

		snapshot, adreno_snapshot_sqe, NULL);



	/* Dump AQE firmware version */

	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_DEBUG,

		snapshot, adreno_snapshot_aqe, NULL);

}



/* adreno_snapshot - Snapshot the Adreno GPU state

 * @device - KGSL device to snapshot

 * @snapshot - Pointer to the snapshot instance

 * @context - context that caused the fault, if known by the driver

 * This is a hook function called by kgsl_snapshot to snapshot the

 * Adreno specific information for the GPU snapshot.  In turn, this function

 * calls the GPU specific snapshot function to get core specific information.

 */

void adreno_snapshot(struct kgsl_device *device, struct kgsl_snapshot *snapshot,

			struct kgsl_context *context, struct kgsl_context *context_lpac)

{

	unsigned int i;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);

	struct kgsl_snapshot_header *header = (struct kgsl_snapshot_header *)

		snapshot->ptr;



	/* Set up the master header */

	header->magic = SNAPSHOT_MAGIC;

	/* gpuid is deprecated so initialize it to an obviously wrong value */

	header->gpuid = UINT_MAX;

	header->chipid = adreno_dev->chipid;



	snapshot->ptr += sizeof(*header);

	snapshot->remain -= sizeof(*header);

	snapshot->size += sizeof(*header);



	/* Write the OS section */

	adreno_snapshot_os(device, snapshot, context, context_lpac);



	ib_max_objs = 0;

	/* Reset the list of objects */

	objbufptr = 0;



	snapshot_frozen_objsize = 0;



	snapshot->process = setup_fault_process(device, snapshot, context);

	if (context_lpac)

		snapshot->process_lpac = setup_fault_process(device, snapshot, context_lpac);



	/* Dump firmware version and few dwords */

	adreno_snapshot_firmware(device, snapshot);



	/* Add GPU specific sections - registers mainly, but other stuff too */

	if (gpudev->snapshot)

		gpudev->snapshot(adreno_dev, snapshot);



	snapshot->ib1dumped = false;

	snapshot->ib2dumped = false;

	snapshot->ib1dumped_lpac = false;

	snapshot->ib2dumped_lpac = false;



	adreno_snapshot_ringbuffer(device, snapshot, adreno_dev->cur_rb);



	/* Dump the prev ringbuffer */

	if (adreno_dev->prev_rb != adreno_dev->cur_rb)

		adreno_snapshot_ringbuffer(device, snapshot,

			adreno_dev->prev_rb);



	if ((adreno_dev->next_rb != adreno_dev->prev_rb) &&

		 (adreno_dev->next_rb != adreno_dev->cur_rb))

		adreno_snapshot_ringbuffer(device, snapshot,

			adreno_dev->next_rb);



	if (device->snapshot_atomic)

		return;



	/* Dump selected global buffers */

	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

			snapshot, adreno_snapshot_global, device->memstore);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

			snapshot, adreno_snapshot_global, device->scratch);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

			snapshot, adreno_snapshot_global,

			adreno_dev->pwron_fixup);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

			snapshot, adreno_snapshot_global,

			adreno_dev->profile_buffer);



	if (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_IOMMU)

		adreno_snapshot_iommu(device, snapshot);



	/*

	 * Add a section that lists (gpuaddr, size, memtype) tuples of the

	 * hanging process

	 */

	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_MEMLIST_V2,

			snapshot, snapshot_capture_mem_list, snapshot->process);



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_MEMLIST_V2,

			snapshot, snapshot_capture_mem_list, snapshot->process_lpac);



	adreno_static_ib_dump(device, snapshot->process,

		snapshot->ib1base, snapshot->ib1size, snapshot->ib2base, snapshot->ib2size,

		snapshot->ib3base, snapshot->ib3size);



	adreno_static_ib_dump(device, snapshot->process_lpac,

		snapshot->ib1base_lpac, snapshot->ib1size_lpac,

		snapshot->ib2base_lpac, snapshot->ib2size_lpac, 0, 0);

	/*

	 * Go through the list of found objects and dump each one.  As the IBs

	 * are parsed, more objects might be found, and objbufptr will increase

	 */

	for (i = 0; i < objbufptr; i++)

		dump_object(device, i, snapshot);



	/*

	 * Incase snapshot static blob is running out of memory, Add Active IB1

	 * and IB2 entries to obj_list so that active ib's can be dumped to

	 * snapshot dynamic blob.

	 */

	if (!snapshot->ib1dumped || !snapshot->ib2dumped)

		kgsl_snapshot_add_active_ib_obj_list(device, snapshot);



	if (ib_max_objs)

		dev_err(device->dev, "Max objects found in GC IB\n");



	if (!snapshot->ib1dumped_lpac || !snapshot->ib2dumped_lpac)

		kgsl_snapshot_add_active_lpac_ib_obj_list(device, snapshot);



	if (ib_max_objs)

		dev_err(device->dev, "Max objects found in LPAC IB\n");



	if (snapshot_frozen_objsize)

		dev_err(device->dev,

			"GPU snapshot froze %zdKb of GPU buffers\n",

			snapshot_frozen_objsize / 1024);



}



void adreno_snapshot_registers(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot,

		const unsigned int *regs, unsigned int count)

{

	struct kgsl_snapshot_registers r;



	r.regs = regs;

	r.count = count;



	kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_REGS, snapshot,

		kgsl_snapshot_dump_registers, &r);

}



int adreno_snapshot_regs_count(const u32 *ptr)

{

	unsigned int count = 0;

	unsigned int group_count;



	for ( ; ptr[0] != UINT_MAX; ptr += 2) {

		group_count = REG_COUNT(ptr);

		if (group_count == 1)

			count += group_count + 1;

		else

			count += group_count + 2;

	}

	return count;

}



/*

 * This is a new format for dumping the registers, where we dump just the first

 * address of the register along with the count of the contiguous registers

 * which we going to dump. This helps us save memory by not dumping the

 * address for each register

 */

size_t adreno_snapshot_registers_v2(struct kgsl_device *device, u8 *buf,

				size_t remain, void *priv)

{

	const u32 *ptr = (const u32 *)priv;

	unsigned int *data = (unsigned int *)buf;

	int count = 0, k;



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

	count = adreno_snapshot_regs_count(ptr);



	if (remain < (count * 4)) {

		SNAPSHOT_ERR_NOMEM(device, "REGISTERS");

		return 0;

	}



	for (ptr = (const u32 *)priv; ptr[0] != UINT_MAX; ptr += 2) {

		int cnt = REG_COUNT(ptr);



		if (cnt == 1)

			*data++ = BIT(31) | ptr[0];

		else {

			*data++ = ptr[0];

			*data++ = cnt;

		}

		for (k = ptr[0]; k <= ptr[1]; k++) {

			kgsl_regread(device, k, data);

			data++;

		}

	}



	/* Return the size of the section */

	return (count * 4);

}



static size_t snapshot_gmu_version(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv)

{

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

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

	struct kgsl_snapshot_gmu_version *ver = priv;



	if (remain < DEBUG_SECTION_SZ(1)) {

		SNAPSHOT_ERR_NOMEM(device, "GMU Version");

		return 0;

	}



	header->type = ver->type;

	header->size = 1;



	*data = ver->value;



	return DEBUG_SECTION_SZ(1);

}



void adreno_snapshot_gmu_versions(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{

	int i;

	struct gmu_core_device *gmu = &device->gmu_core;

	struct kgsl_snapshot_gmu_version gmu_vers[] = {

		{ .type = SNAPSHOT_DEBUG_GMU_CORE_VERSION,

			.value = gmu->ver.core, },

		{ .type = SNAPSHOT_DEBUG_GMU_CORE_DEV_VERSION,

			.value = gmu->ver.core_dev, },

		{ .type = SNAPSHOT_DEBUG_GMU_PWR_VERSION,

			.value = gmu->ver.pwr, },

		{ .type = SNAPSHOT_DEBUG_GMU_PWR_DEV_VERSION,

			.value = gmu->ver.pwr_dev, },

		{ .type = SNAPSHOT_DEBUG_GMU_HFI_VERSION,

			.value = gmu->ver.hfi, },

	};



	for (i = 0; i < ARRAY_SIZE(gmu_vers); i++)

		kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_DEBUG,

				snapshot, snapshot_gmu_version, &gmu_vers[i]);

}



size_t adreno_snapshot_gmu_mem(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv)

{

	struct kgsl_snapshot_gmu_mem *mem_hdr =

		(struct kgsl_snapshot_gmu_mem *)buf;

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

	struct gmu_mem_type_desc *desc = priv;



	if (priv == NULL || desc->memdesc->hostptr == NULL)

		return 0;



	if (remain < desc->memdesc->size + sizeof(*mem_hdr)) {

		dev_err(device->dev,

			"snapshot: Not enough memory for the gmu section %d\n",

			desc->type);

		return 0;

	}



	mem_hdr->type = desc->type;

	mem_hdr->hostaddr = 0;

	mem_hdr->gmuaddr = desc->memdesc->gmuaddr;

	mem_hdr->gpuaddr = 0;



	/* The hw fence queues are mapped as iomem in the kernel */

	if (desc->type == SNAPSHOT_GMU_MEM_HW_FENCE)

		memcpy_fromio(data, desc->memdesc->hostptr, desc->memdesc->size);

	else

		memcpy(data, desc->memdesc->hostptr, desc->memdesc->size);



	return desc->memdesc->size + sizeof(*mem_hdr);

}



/* Snapshot the preemption related buffers */

static size_t snapshot_preemption_record(struct kgsl_device *device,

	u8 *buf, size_t remain, void *priv)

{

	struct kgsl_memdesc *memdesc = priv;

	struct kgsl_snapshot_gpu_object_v2 *header =

		(struct kgsl_snapshot_gpu_object_v2 *)buf;

	u8 *ptr = buf + sizeof(*header);

	u64 ctxt_record_size = min_t(u64, device->snapshot_ctxt_record_size, memdesc->size);



	if (remain < (ctxt_record_size + sizeof(*header))) {

		SNAPSHOT_ERR_NOMEM(device, "PREEMPTION RECORD");

		return 0;

	}



	header->size = ctxt_record_size >> 2;

	header->gpuaddr = memdesc->gpuaddr;

	header->ptbase =

		kgsl_mmu_pagetable_get_ttbr0(device->mmu.defaultpagetable);

	header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;



	memcpy(ptr, memdesc->hostptr, ctxt_record_size);



	return ctxt_record_size + sizeof(*header);

}



void adreno_snapshot_preemption_record(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	struct adreno_ringbuffer *rb;

	u32 i;



	if (adreno_is_preemption_enabled(adreno_dev)) {

		FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {

			kgsl_snapshot_add_section(device,

				KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,

				snapshot, snapshot_preemption_record,

				rb->preemption_desc);

		}

	}

}





==========================================================================================
FILE: adreno_snapshot.h
LINES: 186 | BYTES: 6256
==========================================================================================

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

/*

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

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

 */

#ifndef __ADRENO_SNAPSHOT_H

#define __ADRENO_SNAPSHOT_H



#include "adreno_hwsched_snapshot.h"

#include "kgsl_snapshot.h"



/* Number of dwords to dump in snapshot for CP SQE */

#define SQE_FW_SNAPSHOT_DWORDS 5

/* Number of dwords to dump in snapshot for CP AQE */

#define AQE_FW_SNAPSHOT_DWORDS 6



#define CP_CRASH_DUMPER_TIMEOUT 500



#define SHADER_SECTION_SZ(_dwords) (((_dwords) * sizeof(unsigned int)) \

		+ sizeof(struct kgsl_snapshot_shader))



/* Macro to make it super easy to dump registers */

#define SNAPSHOT_REGISTERS(_d, _s, _r) \

	adreno_snapshot_registers((_d), (_s), \

		(unsigned int *) _r, ARRAY_SIZE(_r) /  2)



#define REG_COUNT(_ptr) ((_ptr[1] - _ptr[0]) + 1)



struct snapshot_ib_meta {

	struct kgsl_snapshot *snapshot;

	struct kgsl_snapshot_object *obj;

	u64 ib1base;

	u64 ib1size;

	u64 ib2base;

	u64 ib2size;

	u64 ib3base;

	u64 ib3size;

	u64 ib1base_lpac;

	u64 ib1size_lpac;

	u64 ib2base_lpac;

	u64 ib2size_lpac;

};



extern struct snapshot_ib_meta metadata;



void adreno_snapshot_registers(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot,

		const unsigned int *regs, unsigned int count);



/**

 * adreno_snapshot_regs_count - Helper function to calculate register and

 * header size

 * @ptr: Pointer to the register array

 *

 * Return: Number of registers in the array

 *

 * Helper function to count the total number of regsiters

 * in a given array plus the header space needed for each group.

 */

int adreno_snapshot_regs_count(const u32 *ptr);



/**

 * adreno_snapshot_registers_v2 - Dump a series of registers

 * @device: Pointer to the kgsl device

 * @buf: The snapshot buffer

 * @remain: The size remaining in the snapshot buffer

 * @priv: Pointer to the register array to be dumped

 *

 * Return: Number of bytes written to the snapshot

 *

 * This function dumps the registers in a way that we need to

 * only dump the start address and count for each pair of register

 * in the array. This helps us save some memory in snapshot.

 */

size_t adreno_snapshot_registers_v2(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv);



/**

 * adreno_snapshot_registers_v2 - Dump a series of CX_MISC registers

 * @device: Pointer to the kgsl device

 * @buf: The snapshot buffer

 * @remain: The size remaining in the snapshot buffer

 * @priv: Pointer to the CX_MISC register array to be dumped

 *

 * Return: Number of bytes written to the snapshot

 *

 * This function dumps the CX_MISC registers in a way that we need to

 * only dump the start address and count for each pair of register

 * in the array. This helps us save some memory in snapshot.

 */

size_t adreno_snapshot_cx_misc_registers(struct kgsl_device *device,

		u8 *buf, size_t remain, void *priv);



/**

 * adreno_parse_ib - Parse the given IB

 * @device: Pointer to the kgsl device

 * @snapshot: Pointer to the snapshot structure

 * @process: Process to which this IB belongs

 * @gpuaddr: Gpu address of the IB

 * @dwords: Size in dwords of the IB

 *

 * We want to store the last executed IB1 and IB2 in the static region to ensure

 * that we get at least some information out of the snapshot even if we can't

 * access the dynamic data from the sysfs file.  Push all other IBs on the

 * dynamic list

 */

void adreno_parse_ib(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot,

	struct kgsl_process_private *process,

	u64 gpuaddr, u64 dwords);



/**

 * adreno_parse_ib_lpac - Parse the given LPAC IB

 * @device: Pointer to the kgsl device

 * @snapshot: Pointer to the snapshot structure

 * @process: Process to which this LPAC IB belongs

 * @gpuaddr: Gpu address of the LPAC IB

 * @dwords: Size in dwords of the LPAC IB

 *

 * We want to store the last executed LPAC IB1 and IB2 in the static region to ensure

 * that we get at least some information out of the snapshot even if we can't

 * access the dynamic data from the sysfs file.  Push all other IBs on the

 * dynamic list

 */

void adreno_parse_ib_lpac(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot,

	struct kgsl_process_private *process,

	u64 gpuaddr, u64 dwords);



/**

 * adreno_snapshot_global - Add global buffer to snapshot

 * @device: Pointer to the kgsl device

 * @buf: Where the global buffer section is to be written

 * @remain: Remaining bytes in snapshot buffer

 * @priv: Opaque data

 *

 * Return: Number of bytes written to the snapshot buffer

 */

size_t adreno_snapshot_global(struct kgsl_device *device, u8 *buf,

	size_t remain, void *priv);



/**

 * adreno_snapshot_dump_all_ibs - To dump all ibs from ringbuffer

 * @device: Pointer to the kgsl device

 * @rbptr: Ringbuffer host pointer

 * @snapshot: Pointer to the snapshot structure

 *

 * Parse all IBs from the ringbuffer and add to IB dump list.

 */

void adreno_snapshot_dump_all_ibs(struct kgsl_device *device,

			unsigned int *rbptr,

			struct kgsl_snapshot *snapshot);



/**

 * adreno_snapshot_gmu_versions - To dump gmu versions info to snapshot buffer

 * @device: Pointer to the kgsl device

 * @snapshot: Pointer to the snapshot structure

 *

 * Dump gmu versions info to the snapshot buffer.

 */

void adreno_snapshot_gmu_versions(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot);



/**

 * kgsl_snapshot_gmu_mem - Snapshot a GMU memory descriptor

 * @device: Pointer to the kgsl device

 * @buf: Destination snapshot buffer

 * @remain: Remaining size of the snapshot buffer

 * @priv: Opaque handle

 *

 * Return: Number of bytes written to snapshot buffer

 */

size_t adreno_snapshot_gmu_mem(struct kgsl_device *device,

	u8 *buf, size_t remain, void *priv);



/**

 * adreno_snapshot_preemption_record - To dump preemption related buffers

 * @device: Pointer to the kgsl device

 * @snapshot: Pointer to the snapshot structure

 *

 * Snapshot the preemption related buffers .

 */

void adreno_snapshot_preemption_record(struct kgsl_device *device,

		struct kgsl_snapshot *snapshot);



#endif /*__ADRENO_SNAPSHOT_H */





==========================================================================================
FILE: adreno_sysfs.c
LINES: 759 | BYTES: 20671
==========================================================================================

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

/*

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

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

 */



#include <linux/sysfs.h>

#include <linux/version.h>

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

#include <linux/firmware/qcom/qcom_scm.h>

#else

#include <linux/qcom_scm.h>

#endif



#include "adreno.h"

#include "adreno_sysfs.h"

#include "adreno_trace.h"

#include "kgsl_pwrscale.h"

#include "kgsl_sysfs.h"



static ssize_t _gpu_model_show(struct kgsl_device *device, char *buf)

{

	return scnprintf(buf, PAGE_SIZE, adreno_get_gpu_model(device));

}



static ssize_t gpu_model_show(struct device *dev,

		struct device_attribute *attr, char *buf)

{

	struct kgsl_device *device = dev_get_drvdata(dev);



	return _gpu_model_show(device, buf);

}



static int _l3_vote_store(struct adreno_device *adreno_dev, bool val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (ADRENO_FEATURE(adreno_dev, ADRENO_L3_VOTE))

		device->l3_vote = val;



	return 0;

}



static bool _l3_vote_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	return device->l3_vote;

}



static int _ft_policy_store(struct adreno_device *adreno_dev,

		unsigned int val)

{

	adreno_dev->ft_policy = val & KGSL_FT_POLICY_MASK;

	return 0;

}



static unsigned int _ft_policy_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->ft_policy;

}



static int _ft_pagefault_policy_store(struct adreno_device *adreno_dev,

		unsigned int val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	int ret = 0;



	kgsl_mutex_lock(&device->mutex);

	val &= KGSL_FT_PAGEFAULT_MASK;



	if (device->state == KGSL_STATE_ACTIVE)

		ret = kgsl_mmu_set_pagefault_policy(&device->mmu,

			(unsigned long) val);



	if (ret == 0)

		device->mmu.pfpolicy = val;



	kgsl_mutex_unlock(&device->mutex);



	return 0;

}



static unsigned int _ft_pagefault_policy_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	return device->mmu.pfpolicy;

}



static int _rt_bus_hint_store(struct adreno_device *adreno_dev, u32 val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwrctrl = &device->pwrctrl;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_RT_HINT) ||

		val == pwrctrl->rt_bus_hint)

		return 0;



	if (val > pwrctrl->pwrlevels[0].bus_max)

		val = 0;



	adreno_power_cycle_u32(adreno_dev, &pwrctrl->rt_bus_hint, val);



	return 0;

}



static u32 _rt_bus_hint_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	return device->pwrctrl.rt_bus_hint;

}



static int _rt_pwrlevel_hint_store(struct adreno_device *adreno_dev, u32 val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct kgsl_pwrctrl *pwrctrl = &device->pwrctrl;



	if (!ADRENO_FEATURE(adreno_dev, ADRENO_RT_HINT) ||

		val == pwrctrl->rt_pwrlevel_hint)

		return 0;



	if (val > pwrctrl->num_pwrlevels - 1)

		val = INVALID_DCVS_IDX;



	adreno_power_cycle_u32(adreno_dev, &pwrctrl->rt_pwrlevel_hint, val);



	return 0;

}



static u32 _rt_pwrlevel_hint_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	return device->pwrctrl.rt_pwrlevel_hint;

}



/* Tuning values can be set to 0/1/2/3 */

#define DCVS_TUNING_MAX 3

#define DCVS_TUNING_EN_BIT BIT(5)



/*

 * GPU DCVS Tuning allows for small adjustments to the DCVS

 * algorithm. The default value for each tunable is 0. Setting

 * a higher tunable value will increase the aggressivenes

 * of the DCVS algorithm. Currently 0-3 are supported values

 * for each tunable, 3 being most aggressive.

 */



/* Mingap is the count of consecutive low requests before moving to lower DCVS levels. */

#define DCVS_TUNING_MINGAP 0

/* Penalty is the busy threshold for moving between levels. */

#define DCVS_TUNING_PENALTY 1

/* Numbusy is the backoff from mingap to transition power level more quickly. */

#define DCVS_TUNING_NUMBUSY 2



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

static inline int qcom_scm_kgsl_dcvs_tuning(u32 mingap, u32 penalty, u32 numbusy)

{

	return -EINVAL;

}



static int __dcvs_tuning_scm_entry(struct adreno_device *adreno_dev,

		u32 param, u32 val)

{

	int ret;

	u32 mingap = 0, penalty = 0, numbusy = 0;

	u32 *save;



	switch (param) {

	case DCVS_TUNING_MINGAP:

		mingap = DCVS_TUNING_EN_BIT | FIELD_PREP(GENMASK(4, 0), val);

		save = &adreno_dev->dcvs_tuning_mingap_lvl;

		break;

	case DCVS_TUNING_PENALTY:

		penalty = DCVS_TUNING_EN_BIT | FIELD_PREP(GENMASK(4, 0), val);

		save = &adreno_dev->dcvs_tuning_penalty_lvl;

		break;

	case DCVS_TUNING_NUMBUSY:

		numbusy = DCVS_TUNING_EN_BIT | FIELD_PREP(GENMASK(4, 0), val);

		save = &adreno_dev->dcvs_tuning_numbusy_lvl;

		break;

	default:

		return -EINVAL;

	}



	if (!mutex_trylock(&adreno_dev->dcvs_tuning_mutex))

		return -EDEADLK;



	ret = qcom_scm_kgsl_dcvs_tuning(mingap, penalty, numbusy);

	if (ret == 0) {

		*save = val;

		trace_adreno_dcvs_tuning(param,

				adreno_dev->dcvs_tuning_mingap_lvl,

				adreno_dev->dcvs_tuning_penalty_lvl,

				adreno_dev->dcvs_tuning_numbusy_lvl);

	}

	mutex_unlock(&adreno_dev->dcvs_tuning_mutex);



	return ret;

}

#else

static int __dcvs_tuning_scm_entry(struct adreno_device *adreno_dev, u32 param, u32 val)

{

	return -EOPNOTSUPP;

}

#endif



static int _dcvs_tuning_mingap_store(struct adreno_device *adreno_dev,

		unsigned int val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return -EOPNOTSUPP;



	if (val > DCVS_TUNING_MAX)

		return -EINVAL;



	return __dcvs_tuning_scm_entry(adreno_dev, DCVS_TUNING_MINGAP, val);

}



static u32 _dcvs_tuning_mingap_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return 0;



	return adreno_dev->dcvs_tuning_mingap_lvl;

}



static int _dcvs_tuning_penalty_store(struct adreno_device *adreno_dev,

		unsigned int val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return -EOPNOTSUPP;



	if (val > DCVS_TUNING_MAX)

		return -EINVAL;



	return __dcvs_tuning_scm_entry(adreno_dev, DCVS_TUNING_PENALTY, val);

}



static u32 _dcvs_tuning_penalty_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return 0;



	return adreno_dev->dcvs_tuning_penalty_lvl;

}



static int _dcvs_tuning_numbusy_store(struct adreno_device *adreno_dev,

		unsigned int val)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return -EOPNOTSUPP;



	if (val > DCVS_TUNING_MAX)

		return -EINVAL;



	return __dcvs_tuning_scm_entry(adreno_dev, DCVS_TUNING_NUMBUSY, val);

}



static u32 _dcvs_tuning_numbusy_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);



	if (!device->host_based_dcvs || !kgsl_is_msm_adreno_tz_governor(device))

		return 0;



	return adreno_dev->dcvs_tuning_numbusy_lvl;

}



static unsigned int _dcvs_mode_show(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	u32 mode;



	if (device->host_based_dcvs)

		mode = 0;

	else

		mode = 1;



	return mode;

}



static int _gpu_llc_slice_enable_store(struct adreno_device *adreno_dev,

		bool val)

{

	if (IS_ERR_OR_NULL(adreno_dev->gpu_llc_slice) ||

		(adreno_dev->gpu_llc_slice_enable == val))

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->gpu_llc_slice_enable, val);

}



static bool _gpu_llc_slice_enable_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->gpu_llc_slice_enable;

}



static int _gpuhtw_llc_slice_enable_store(struct adreno_device *adreno_dev,

		bool val)

{

	if (IS_ERR_OR_NULL(adreno_dev->gpuhtw_llc_slice) ||

		(adreno_dev->gpuhtw_llc_slice_enable == val))

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->gpuhtw_llc_slice_enable, val);

}



static bool _gpuhtw_llc_slice_enable_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->gpuhtw_llc_slice_enable;

}



static int _gpumv_llc_slice_enable_store(struct adreno_device *adreno_dev,

		bool val)

{

	if (!IS_ERR_OR_NULL(adreno_dev->gpumv_llc_slice))

		adreno_dev->gpumv_llc_slice_enable = val;

	return 0;

}



static bool _gpumv_llc_slice_enable_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->gpumv_llc_slice_enable;

}



static bool _ft_hang_intr_status_show(struct adreno_device *adreno_dev)

{

	/* Hang interrupt is always on on all targets */

	return true;

}



static int _hwcg_store(struct adreno_device *adreno_dev, bool val)

{

	if (adreno_dev->hwcg_enabled == val)

		return 0;



	/* UCHE_GBIF_GX_CONFIG register requires update of reglist */

	if (!adreno_is_gen8(adreno_dev) && !adreno_is_gen7_9_x(adreno_dev))

		adreno_dev->patch_reglist = false;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->hwcg_enabled,

		val);

}



static bool _hwcg_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->hwcg_enabled;

}



static int _throttling_store(struct adreno_device *adreno_dev, bool val)

{

	if (!adreno_is_a540(adreno_dev) ||

		adreno_dev->throttling_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev,

		&adreno_dev->throttling_enabled, val);

}



static bool _throttling_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->throttling_enabled;

}



static int _sptp_pc_store(struct adreno_device *adreno_dev, bool val)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_SPTP_PC) ||

		adreno_dev->sptp_pc_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->sptp_pc_enabled,

		val);

}



static bool _sptp_pc_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->sptp_pc_enabled;

}



static int _lm_store(struct adreno_device *adreno_dev, bool val)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_LM) ||

		adreno_dev->lm_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->lm_enabled,

		val);

}



static bool _lm_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->lm_enabled;

}



static int _ifpc_store(struct adreno_device *adreno_dev, bool val)

{

	return gmu_core_dev_ifpc_store(KGSL_DEVICE(adreno_dev), val);

}



static bool _ifpc_show(struct adreno_device *adreno_dev)

{

	return gmu_core_dev_ifpc_isenabled(KGSL_DEVICE(adreno_dev));

}



static int _touch_wake_store(struct adreno_device *adreno_dev, bool val)

{

	if (val)

		adreno_touch_wake(KGSL_DEVICE(adreno_dev));

	return 0;

}



static bool _touch_wake_show(struct adreno_device *adreno_dev)

{

	return false;

}



static unsigned int _ifpc_count_show(struct adreno_device *adreno_dev)

{

	const struct adreno_gpudev *gpudev  = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->power_feature_stats)

		gpudev->power_feature_stats(adreno_dev);



	return adreno_dev->ifpc_count;

}



static bool _acd_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->acd_enabled;

}



static int _acd_store(struct adreno_device *adreno_dev, bool val)

{

	return gmu_core_dev_acd_set(KGSL_DEVICE(adreno_dev), val);

}



static bool _gmu_ab_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->gmu_ab;

}



static int _gmu_ab_store(struct adreno_device *adreno_dev, bool val)

{

	if (!test_bit(ADRENO_DEVICE_GMU_AB, &adreno_dev->priv) ||

		(adreno_dev->gmu_ab == val))

		return 0;



	/* Power cycle the GPU for changes to take effect */

	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->gmu_ab, val);

}



static bool _bcl_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->bcl_enabled;

}



static int _bcl_store(struct adreno_device *adreno_dev, bool val)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_BCL) ||

				adreno_dev->bcl_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->bcl_enabled,

					val);

}



static bool _clx_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->clx_enabled;

}



static int _clx_store(struct adreno_device *adreno_dev, bool val)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_CLX) || adreno_dev->clx_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->clx_enabled, val);

}



static bool _dms_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->dms_enabled;

}



static int _dms_store(struct adreno_device *adreno_dev, bool val)

{

	if (!test_bit(ADRENO_DEVICE_DMS, &adreno_dev->priv) ||

		adreno_dev->dms_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->dms_enabled, val);

}



static bool _perfcounter_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->perfcounter;

}



static int _perfcounter_store(struct adreno_device *adreno_dev, bool val)

{

	if (adreno_dev->perfcounter == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->perfcounter, val);

}



static bool _lpac_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->lpac_enabled;

}



static int _lpac_store(struct adreno_device *adreno_dev, bool val)

{

	const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);



	if (gpudev->lpac_store)

		return gpudev->lpac_store(adreno_dev, val);

	else

		return -EINVAL;

}



static ssize_t gpufaults_show(struct device *dev,

		struct device_attribute *attr, char *buf)

{

	struct kgsl_device *device = dev_get_drvdata(dev);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	size_t count = 0;

	int i;



	read_lock(&adreno_dev->fault_stats_lock);

	for (i = 0; i < ARRAY_SIZE(adreno_dev->fault_counts); i++)

		count += scnprintf(buf + count, PAGE_SIZE - 2 - count, "%u ",

			adreno_dev->fault_counts[i]);

	read_unlock(&adreno_dev->fault_stats_lock);



	buf[count++] = '\n';

	return count;

}



static ssize_t gpufault_procs_show(struct device *dev,

		struct device_attribute *attr, char *buf)

{

	struct kgsl_device *device = dev_get_drvdata(dev);

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);

	size_t count = 0;

	int i;



	read_lock(&adreno_dev->fault_stats_lock);

	for (i = 0; i < ARRAY_SIZE(adreno_dev->fault_procs); i++) {

		struct adreno_fault_proc *proc = &adreno_dev->fault_procs[i];



		if (!proc->fault_count)

			break;



		count += scnprintf(buf + count, PAGE_SIZE - 1 - count, "%s %u\n",

			proc->comm, proc->fault_count);



		if (count >= PAGE_SIZE - 1)

			break;

	}

	read_unlock(&adreno_dev->fault_stats_lock);



	return count;

}



ssize_t adreno_sysfs_store_u32(struct device *dev,

		struct device_attribute *attr, const char *buf, size_t count)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_get_drvdata(dev));

	const struct adreno_sysfs_attribute_u32 *_attr =

		container_of(attr, struct adreno_sysfs_attribute_u32, attr);

	u32 val;

	int ret;



	ret = kstrtou32(buf, 0, &val);

	if (ret)

		return ret;



	ret = _attr->store(adreno_dev, val);

	if (ret)

		return ret;



	return count;

}



ssize_t adreno_sysfs_show_u32(struct device *dev,

		struct device_attribute *attr, char *buf)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_get_drvdata(dev));

	const struct adreno_sysfs_attribute_u32 *_attr =

		container_of(attr, struct adreno_sysfs_attribute_u32, attr);



	return scnprintf(buf, PAGE_SIZE, "0x%X\n", _attr->show(adreno_dev));

}



ssize_t adreno_sysfs_store_bool(struct device *dev,

		struct device_attribute *attr, const char *buf, size_t count)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_get_drvdata(dev));

	const struct adreno_sysfs_attribute_bool *_attr =

		container_of(attr, struct adreno_sysfs_attribute_bool, attr);

	bool val;

	int ret;



	ret = kstrtobool(buf, &val);

	if (ret)

		return ret;



	ret = _attr->store(adreno_dev, val);

	if (ret)

		return ret;



	return count;

}



ssize_t adreno_sysfs_show_bool(struct device *dev,

		struct device_attribute *attr, char *buf)

{

	struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_get_drvdata(dev));

	const struct adreno_sysfs_attribute_bool *_attr =

		container_of(attr, struct adreno_sysfs_attribute_bool, attr);



	return scnprintf(buf, PAGE_SIZE, "%d\n", _attr->show(adreno_dev));

}



static int _dcvs_profile_enabled_store(struct adreno_device *adreno_dev, bool val)

{

	if (!ADRENO_FEATURE(adreno_dev, ADRENO_DCVS_PROFILE) ||

			adreno_dev->dcvs_profile_enabled == val)

		return 0;



	return adreno_power_cycle_bool(adreno_dev, &adreno_dev->dcvs_profile_enabled, val);

}



static bool _dcvs_profile_enabled_show(struct adreno_device *adreno_dev)

{

	return adreno_dev->dcvs_profile_enabled;

}



static ADRENO_SYSFS_U32(ft_policy);

static ADRENO_SYSFS_U32(ft_pagefault_policy);

static ADRENO_SYSFS_U32(rt_bus_hint);

static ADRENO_SYSFS_U32(rt_pwrlevel_hint);

static ADRENO_SYSFS_RO_BOOL(ft_hang_intr_status);

static ADRENO_SYSFS_BOOL(gpu_llc_slice_enable);

static ADRENO_SYSFS_BOOL(gpuhtw_llc_slice_enable);

static ADRENO_SYSFS_BOOL(gpumv_llc_slice_enable);



static DEVICE_INT_ATTR(wake_nice, 0644, adreno_wake_nice);

static DEVICE_INT_ATTR(wake_timeout, 0644, adreno_wake_timeout);



static ADRENO_SYSFS_BOOL(sptp_pc);

static ADRENO_SYSFS_BOOL(lm);

static ADRENO_SYSFS_BOOL(hwcg);

static ADRENO_SYSFS_BOOL(throttling);

static ADRENO_SYSFS_BOOL(ifpc);

static ADRENO_SYSFS_RO_U32(ifpc_count);

static ADRENO_SYSFS_BOOL(acd);

static ADRENO_SYSFS_BOOL(bcl);

static ADRENO_SYSFS_BOOL(clx);

static ADRENO_SYSFS_BOOL(l3_vote);

static ADRENO_SYSFS_BOOL(perfcounter);

static ADRENO_SYSFS_BOOL(lpac);

static ADRENO_SYSFS_BOOL(dms);

static ADRENO_SYSFS_BOOL(touch_wake);

static ADRENO_SYSFS_BOOL(gmu_ab);

static ADRENO_SYSFS_BOOL(dcvs_profile_enabled);



static DEVICE_ATTR_RO(gpu_model);

static DEVICE_ATTR_RO(gpufaults);

static DEVICE_ATTR_RO(gpufault_procs);



static ADRENO_SYSFS_U32(dcvs_tuning_mingap);

static ADRENO_SYSFS_U32(dcvs_tuning_penalty);

static ADRENO_SYSFS_U32(dcvs_tuning_numbusy);

static ADRENO_SYSFS_RO_U32(dcvs_mode);



static const struct attribute *_attr_list[] = {

	&adreno_attr_ft_policy.attr.attr,

	&adreno_attr_ft_pagefault_policy.attr.attr,

	&adreno_attr_rt_bus_hint.attr.attr,

	&adreno_attr_rt_pwrlevel_hint.attr.attr,

	&adreno_attr_ft_hang_intr_status.attr.attr,

	&dev_attr_wake_nice.attr.attr,

	&dev_attr_wake_timeout.attr.attr,

	&adreno_attr_sptp_pc.attr.attr,

	&adreno_attr_lm.attr.attr,

	&adreno_attr_hwcg.attr.attr,

	&adreno_attr_throttling.attr.attr,

	&adreno_attr_gpu_llc_slice_enable.attr.attr,

	&adreno_attr_gpuhtw_llc_slice_enable.attr.attr,

	&adreno_attr_gpumv_llc_slice_enable.attr.attr,

	&adreno_attr_ifpc.attr.attr,

	&adreno_attr_ifpc_count.attr.attr,

	&adreno_attr_acd.attr.attr,

	&adreno_attr_bcl.attr.attr,

	&dev_attr_gpu_model.attr,

	&adreno_attr_l3_vote.attr.attr,

	&adreno_attr_perfcounter.attr.attr,

	&adreno_attr_lpac.attr.attr,

	&adreno_attr_dms.attr.attr,

	&adreno_attr_touch_wake.attr.attr,

	&adreno_attr_gmu_ab.attr.attr,

	&adreno_attr_clx.attr.attr,

	&dev_attr_gpufaults.attr,

	&dev_attr_gpufault_procs.attr,

	&adreno_attr_dcvs_tuning_mingap.attr.attr,

	&adreno_attr_dcvs_tuning_penalty.attr.attr,

	&adreno_attr_dcvs_tuning_numbusy.attr.attr,

	&adreno_attr_dcvs_mode.attr.attr,

	&adreno_attr_dcvs_profile_enabled.attr.attr,

	NULL,

};



static GPU_SYSFS_ATTR(gpu_model, 0444, _gpu_model_show, NULL);



/**

 * adreno_sysfs_init() - Initialize adreno sysfs files

 * @adreno_dev: Pointer to the adreno device

 *

 * Initialize many of the adreno specific sysfs files especially for fault

 * tolerance and power control

 */

int adreno_sysfs_init(struct adreno_device *adreno_dev)

{

	struct kgsl_device *device = KGSL_DEVICE(adreno_dev);

	struct device *gmu_dev;

	int ret;



	ret = sysfs_create_files(&device->dev->kobj, _attr_list);



	if (!ret) {

		/* Notify userspace */

		kobject_uevent(&device->dev->kobj, KOBJ_ADD);



		ret = sysfs_create_file(&device->gpu_sysfs_kobj,

			&gpu_sysfs_attr_gpu_model.attr);

	}



	/* Add a soft link for gmu device */

	if (gmu_core_isenabled(device)) {

		gmu_dev =  GMU_PDEV_DEV(device);

		WARN_ON(sysfs_create_link(&device->dev->kobj, &gmu_dev->kobj, "gmu"));

	}

	return ret;

}







==========================================================================================
FILE: adreno_trace.h
LINES: 1142 | BYTES: 34911
==========================================================================================

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

/*

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

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

 */



#if !defined(_ADRENO_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)

#define _ADRENO_TRACE_H



#undef TRACE_SYSTEM

#define TRACE_SYSTEM kgsl

#undef TRACE_INCLUDE_PATH

#define TRACE_INCLUDE_PATH .

#undef TRACE_INCLUDE_FILE

#define TRACE_INCLUDE_FILE adreno_trace



#include <linux/tracepoint.h>

#include "adreno_a5xx.h"

#include "adreno_gen7.h"

#include "adreno_gen8.h"

#include "adreno_hfi.h"



#if (KERNEL_VERSION(6, 10, 0) > LINUX_VERSION_CODE)

#define adreno_assign_str(dst, src) __assign_str(dst, src)

#else

#define adreno_assign_str(dst, src) __assign_str(dst)

#endif



#define ADRENO_FT_TYPES \

	{ BIT(KGSL_FT_OFF), "off" }, \

	{ BIT(KGSL_FT_REPLAY), "replay" }, \

	{ BIT(KGSL_FT_SKIPIB), "skipib" }, \

	{ BIT(KGSL_FT_SKIPFRAME), "skipframe" }, \

	{ BIT(KGSL_FT_DISABLE), "disable" }, \

	{ BIT(KGSL_FT_TEMP_DISABLE), "temp" }, \

	{ BIT(KGSL_FT_THROTTLE), "throttle"}, \

	{ BIT(KGSL_FT_SKIPCMD), "skipcmd" }



TRACE_EVENT(adreno_cmdbatch_queued,

	TP_PROTO(struct kgsl_drawobj *drawobj, unsigned int queued),

	TP_ARGS(drawobj, queued),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(unsigned int, queued)

		__field(unsigned int, flags)

		__field(unsigned int, prio)

	),

	TP_fast_assign(

		__entry->id = drawobj->context->id;

		__entry->timestamp = drawobj->timestamp;

		__entry->queued = queued;

		__entry->flags = drawobj->flags;

		__entry->prio = drawobj->context->priority;

	),

	TP_printk(

		"ctx=%u ctx_prio=%u ts=%u queued=%u flags=%s",

			__entry->id, __entry->prio,

			__entry->timestamp, __entry->queued,

			__entry->flags ? __print_flags(__entry->flags, "|",

						KGSL_DRAWOBJ_FLAGS) : "none"

	)

);



TRACE_EVENT(adreno_hw_fence_query,

	TP_PROTO(u64 context, u64 seqno, u32 flags, const char *name, const char *val),

	TP_ARGS(context, seqno, flags, name, val),

	TP_STRUCT__entry(

		__field(u64, context)

		__field(u64, seqno)

		__field(u32, flags)

		__string(fence_name, name)

		__string(val, val)

	),

	TP_fast_assign(

		__entry->context = context;

		__entry->seqno = seqno;

		__entry->flags = flags;

		adreno_assign_str(fence_name, name);

		adreno_assign_str(val, val);

	),

	TP_printk(

		"id=%lld seqno=%lld sw_status=%s name=%s val=%s",

			__entry->context, __entry->seqno,

			__entry->flags ? __print_flags(__entry->flags, "|",

				{ ADRENO_HW_FENCE_SW_STATUS_PENDING, "PENDING" },

				{ ADRENO_HW_FENCE_SW_STATUS_SIGNALED, "SIGNALED" }) : "none",

			__get_str(fence_name),

			__get_str(val))

);



TRACE_EVENT(adreno_input_hw_fence,

	TP_PROTO(u32 id, u64 context, u64 seqno, u64 flags, const char *name),

	TP_ARGS(id, context, seqno, flags, name),

	TP_STRUCT__entry(

		__field(u32, id)

		__field(u64, context)

		__field(u64, seqno)

		__field(u64, flags)

		__string(fence_name, name)

	),

	TP_fast_assign(

		__entry->id = id;

		__entry->context = context;

		__entry->seqno = seqno;

		__entry->flags = flags;

		adreno_assign_str(fence_name, name);

	),

	TP_printk(

		"ctx=%u id=%lld seqno=%lld flags=%s name=%s",

			__entry->id,  __entry->context, __entry->seqno,

			__entry->flags ? __print_flags(__entry->flags, "|",

				GMU_SYNCOBJ_FLAGS) : "none",

			__get_str(fence_name))

);



TRACE_EVENT(adreno_syncobj_query_reply,

	TP_PROTO(u32 id, u32 timestamp, uint64_t ticks),

	TP_ARGS(id, timestamp, ticks),

	TP_STRUCT__entry(

		__field(u32, id)

		__field(u32, timestamp)

		__field(uint64_t, ticks)

	),

	TP_fast_assign(

		__entry->id = id;

		__entry->timestamp = timestamp;

		__entry->ticks = ticks;

	),

	TP_printk(

		"ctx=%u ts=%u ticks=%lld",

			__entry->id, __entry->timestamp, __entry->ticks)

);



TRACE_EVENT(adreno_syncobj_query,

	TP_PROTO(u32 id, u32 timestamp, uint64_t ticks),

	TP_ARGS(id, timestamp, ticks),

	TP_STRUCT__entry(

		__field(u32, id)

		__field(u32, timestamp)

		__field(uint64_t, ticks)

	),

	TP_fast_assign(

		__entry->id = id;

		__entry->timestamp = timestamp;

		__entry->ticks = ticks;

	),

	TP_printk(

		"ctx=%u ts=%u ticks=%lld",

			__entry->id, __entry->timestamp, __entry->ticks)

);



TRACE_EVENT(adreno_syncobj_submitted,

	TP_PROTO(u32 id, u32 timestamp, u32 num_syncobj,

		uint64_t ticks),

	TP_ARGS(id, timestamp, num_syncobj, ticks),

	TP_STRUCT__entry(

		__field(u32, id)

		__field(u32, timestamp)

		__field(u32, num_syncobj)

		__field(uint64_t, ticks)

	),

	TP_fast_assign(

		__entry->id = id;

		__entry->timestamp = timestamp;

		__entry->num_syncobj = num_syncobj;

		__entry->ticks = ticks;

	),

	TP_printk(

		"ctx=%u ts=%u num_sync=%u ticks=%lld",

			__entry->id, __entry->timestamp, __entry->num_syncobj, __entry->ticks)

);



TRACE_EVENT(adreno_syncobj_retired,

	TP_PROTO(u32 id, u32 timestamp, u64 ticks),

	TP_ARGS(id, timestamp, ticks),

	TP_STRUCT__entry(

		__field(u32, id)

		__field(u32, timestamp)

		__field(u64, ticks)

	),

	TP_fast_assign(

		__entry->id = id;

		__entry->timestamp = timestamp;

		__entry->ticks = ticks;

	),

	TP_printk(

		"ctx=%u ts=%u ticks=%llu", __entry->id, __entry->timestamp, __entry->ticks)

);



TRACE_EVENT(adreno_cmdbatch_submitted,

	TP_PROTO(struct kgsl_drawobj *drawobj, struct submission_info *info,

		uint64_t ticks, unsigned long secs, unsigned long usecs,

		int q_inflight),

	TP_ARGS(drawobj, info, ticks, secs, usecs, q_inflight),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(int, inflight)

		__field(unsigned int, flags)

		__field(uint64_t, ticks)

		__field(unsigned long, secs)

		__field(unsigned long, usecs)

		__field(int, prio)

		__field(int, rb_id)

		__field(unsigned int, rptr)

		__field(unsigned int, wptr)

		__field(int, q_inflight)

		__field(int, dispatch_queue)

	),

	TP_fast_assign(

		__entry->id = drawobj->context->id;

		__entry->timestamp = drawobj->timestamp;

		__entry->inflight = info->inflight;

		__entry->flags = drawobj->flags;

		__entry->ticks = ticks;

		__entry->secs = secs;

		__entry->usecs = usecs;

		__entry->prio = drawobj->context->priority;

		__entry->rb_id = info->rb_id;

		__entry->rptr = info->rptr;

		__entry->wptr = info->wptr;

		__entry->q_inflight = q_inflight;

		__entry->dispatch_queue = info->gmu_dispatch_queue;

	),

	TP_printk(

		"ctx=%u ctx_prio=%d ts=%u inflight=%d flags=%s ticks=%lld time=%lu.%0lu rb_id=%d r/w=%x/%x, q_inflight=%d dq_id=%d",

			__entry->id, __entry->prio, __entry->timestamp,

			__entry->inflight,

			__entry->flags ? __print_flags(__entry->flags, "|",

				KGSL_DRAWOBJ_FLAGS) : "none",

			__entry->ticks, __entry->secs, __entry->usecs,

			__entry->rb_id, __entry->rptr, __entry->wptr,

			__entry->q_inflight, __entry->dispatch_queue

	)

);



TRACE_EVENT(adreno_cmdbatch_retired,

		TP_PROTO(struct kgsl_context *context, struct retire_info *info,

			unsigned int flags, int q_inflight,

			unsigned long fault_recovery),

	TP_ARGS(context, info, flags, q_inflight, fault_recovery),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(int, inflight)

		__field(unsigned int, recovery)

		__field(unsigned int, flags)

		__field(uint64_t, start)

		__field(uint64_t, retire)

		__field(int, prio)

		__field(int, rb_id)

		__field(unsigned int, rptr)

		__field(unsigned int, wptr)

		__field(int, q_inflight)

		__field(unsigned long, fault_recovery)

		__field(unsigned int, dispatch_queue)

		__field(uint64_t, submitted_to_rb)

		__field(uint64_t, retired_on_gmu)

		__field(uint64_t, active)

		),

	TP_fast_assign(

		__entry->id = context->id;

		__entry->timestamp = info->timestamp;

		__entry->inflight = info->inflight;

		__entry->recovery = fault_recovery;

		__entry->flags = flags;

		__entry->start = info->sop;

		__entry->retire = info->eop;

		__entry->prio = context->priority;

		__entry->rb_id = info->rb_id;

		__entry->rptr = info->rptr;

		__entry->wptr = info->wptr;

		__entry->q_inflight = q_inflight;

		__entry->dispatch_queue = info->gmu_dispatch_queue;

		__entry->submitted_to_rb = info->submitted_to_rb;

		__entry->retired_on_gmu = info->retired_on_gmu;

		__entry->active = info->active;

		),



	TP_printk(

		"ctx=%u ctx_prio=%d ts=%u inflight=%d recovery=%s flags=%s start=%llu retire=%llu rb_id=%d, r/w=%x/%x, q_inflight=%d, dq_id=%u, submitted_to_rb=%llu retired_on_gmu=%llu active=%llu",

			__entry->id, __entry->prio, __entry->timestamp,

			__entry->inflight,

			__entry->recovery ?

				__print_flags(__entry->fault_recovery, "|",

				ADRENO_FT_TYPES) : "none",

			__entry->flags ? __print_flags(__entry->flags, "|",

				KGSL_DRAWOBJ_FLAGS) : "none",

			__entry->start,

			__entry->retire,

			__entry->rb_id, __entry->rptr, __entry->wptr,

			__entry->q_inflight,

			__entry->dispatch_queue,

			__entry->submitted_to_rb, __entry->retired_on_gmu,

			__entry->active

	 )

);



TRACE_EVENT(gmu_ao_sync,

	TP_PROTO(u64 ticks),

	TP_ARGS(ticks),

	TP_STRUCT__entry(

		__field(u64, ticks)

	),

	TP_fast_assign(

		__entry->ticks = ticks;

	),

	TP_printk(

		"ticks=%llu", __entry->ticks

	)

);



TRACE_EVENT(gmu_event,

	TP_PROTO(u32 *event_info),

	TP_ARGS(event_info),

	TP_STRUCT__entry(

		__field(u32, event)

		__field(u32, ticks)

		__field(u32, data1)

		__field(u32, data2)

	),

	TP_fast_assign(

		__entry->event = event_info[0];

		__entry->ticks = event_info[1];

		__entry->data1 = event_info[2];

		__entry->data2 = event_info[3];

	),

	TP_printk(

		"event=%08u ticks=%08u data1=0x%08x data2=0x%08x",

		__entry->event, __entry->ticks, __entry->data1, __entry->data2

	)

);



TRACE_EVENT(adreno_cmdbatch_sync,

	TP_PROTO(unsigned int ctx_id, unsigned int ctx_prio,

		unsigned int timestamp,	uint64_t ticks),

	TP_ARGS(ctx_id, ctx_prio, timestamp, ticks),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(uint64_t, ticks)

		__field(int, prio)

	),

	TP_fast_assign(

		__entry->id = ctx_id;

		__entry->timestamp = timestamp;

		__entry->ticks = ticks;

		__entry->prio = ctx_prio;

	),

	TP_printk(

		"ctx=%u ctx_prio=%d ts=%u ticks=%lld",

			__entry->id, __entry->prio, __entry->timestamp,

			__entry->ticks

	)

);



TRACE_EVENT(adreno_cmdbatch_ready,

	TP_PROTO(unsigned int ctx_id, unsigned int ctx_prio,

		unsigned int timestamp, unsigned int requeue_cnt),

	TP_ARGS(ctx_id, ctx_prio, timestamp, requeue_cnt),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(int, prio)

		__field(unsigned int, timestamp)

		__field(unsigned int, requeue_cnt)

	),

	TP_fast_assign(

		__entry->id = ctx_id;

		__entry->prio = ctx_prio;

		__entry->timestamp = timestamp;

		__entry->requeue_cnt = requeue_cnt;

	),

	TP_printk(

		"ctx=%u ctx_prio=%d ts=%u requeue_cnt=%u",

			__entry->id, __entry->prio, __entry->timestamp,

			__entry->requeue_cnt

	)

);



TRACE_EVENT(adreno_cmdbatch_done,

	TP_PROTO(unsigned int ctx_id, unsigned int ctx_prio,

		unsigned int timestamp),

	TP_ARGS(ctx_id, ctx_prio, timestamp),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, prio)

		__field(unsigned int, timestamp)

	),

	TP_fast_assign(

		__entry->id = ctx_id;

		__entry->prio = ctx_prio;

		__entry->timestamp = timestamp;

	),

	TP_printk(

		"ctx=%u ctx_prio=%u ts=%u",

			__entry->id, __entry->prio, __entry->timestamp

	)

);



TRACE_EVENT(adreno_cmdbatch_fault,

	TP_PROTO(struct kgsl_drawobj_cmd *cmdobj, unsigned int fault),

	TP_ARGS(cmdobj, fault),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(unsigned int, fault)

	),

	TP_fast_assign(

		__entry->id = cmdobj->base.context->id;

		__entry->timestamp = cmdobj->base.timestamp;

		__entry->fault = fault;

	),

	TP_printk(

		"ctx=%u ts=%u type=%s",

			__entry->id, __entry->timestamp,

			__print_symbolic(__entry->fault,

				{ 0, "none" },

				{ ADRENO_SOFT_FAULT, "soft" },

				{ ADRENO_HARD_FAULT, "hard" },

				{ ADRENO_TIMEOUT_FAULT, "timeout" })

	)

);



TRACE_EVENT(adreno_cmdbatch_recovery,

	TP_PROTO(struct kgsl_drawobj_cmd *cmdobj, unsigned int action),

	TP_ARGS(cmdobj, action),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, timestamp)

		__field(unsigned int, action)

	),

	TP_fast_assign(

		__entry->id = cmdobj->base.context->id;

		__entry->timestamp = cmdobj->base.timestamp;

		__entry->action = action;

	),

	TP_printk(

		"ctx=%u ts=%u action=%s",

			__entry->id, __entry->timestamp,

			__print_symbolic(__entry->action, ADRENO_FT_TYPES)

	)

);



DECLARE_EVENT_CLASS(adreno_drawctxt_template,

	TP_PROTO(struct adreno_context *drawctxt),

	TP_ARGS(drawctxt),

	TP_STRUCT__entry(

		__field(unsigned int, id)

		__field(unsigned int, priority)

	),

	TP_fast_assign(

		__entry->id = drawctxt->base.id;

		__entry->priority = drawctxt->base.priority;

	),

	TP_printk("ctx=%u priority=%u", __entry->id, __entry->priority)

);



DEFINE_EVENT(adreno_drawctxt_template, adreno_drawctxt_sleep,

	TP_PROTO(struct adreno_context *drawctxt),

	TP_ARGS(drawctxt)

);



DEFINE_EVENT(adreno_drawctxt_template, adreno_drawctxt_wake,

	TP_PROTO(struct adreno_context *drawctxt),

	TP_ARGS(drawctxt)

);



DEFINE_EVENT(adreno_drawctxt_template, dispatch_queue_context,

	TP_PROTO(struct adreno_context *drawctxt),

	TP_ARGS(drawctxt)

);



DEFINE_EVENT(adreno_drawctxt_template, adreno_drawctxt_invalidate,

	TP_PROTO(struct adreno_context *drawctxt),

	TP_ARGS(drawctxt)

);



TRACE_EVENT(adreno_drawctxt_wait_start,

	TP_PROTO(unsigned int rb_id, unsigned int ctx_id, unsigned int ts),

	TP_ARGS(rb_id, ctx_id, ts),

	TP_STRUCT__entry(

		__field(unsigned int, rb_id)

		__field(unsigned int, ctx_id)

		__field(unsigned int, ts)

	),

	TP_fast_assign(

		__entry->rb_id = rb_id;

		__entry->ctx_id = ctx_id;

		__entry->ts = ts;

	),

	TP_printk(

		"rb=%u ctx=%u ts=%u",

			__entry->rb_id, __entry->ctx_id, __entry->ts

	)

);



TRACE_EVENT(adreno_drawctxt_wait_done,

	TP_PROTO(unsigned int rb_id, unsigned int ctx_id,

			unsigned int ts, int status),

	TP_ARGS(rb_id, ctx_id, ts, status),

	TP_STRUCT__entry(

		__field(unsigned int, rb_id)

		__field(unsigned int, ctx_id)

		__field(unsigned int, ts)

		__field(int, status)

	),

	TP_fast_assign(

		__entry->rb_id = rb_id;

		__entry->ctx_id = ctx_id;

		__entry->ts = ts;

		__entry->status = status;

	),

	TP_printk(

		"rb=%u ctx=%u ts=%u status=%d",

		__entry->rb_id, __entry->ctx_id, __entry->ts, __entry->status

	)

);



TRACE_EVENT(adreno_drawctxt_switch,

	TP_PROTO(struct adreno_ringbuffer *rb,

		struct adreno_context *newctx),

	TP_ARGS(rb, newctx),

	TP_STRUCT__entry(

		__field(int, rb_level)

		__field(unsigned int, oldctx)

		__field(unsigned int, newctx)

		__field(unsigned int, flags)

	),

	TP_fast_assign(

		__entry->rb_level = rb->id;

		__entry->oldctx = rb->drawctxt_active ?

			rb->drawctxt_active->base.id : 0;

		__entry->newctx = newctx ? newctx->base.id : 0;

	),

	TP_printk(

		"rb level=%d oldctx=%u newctx=%u",

		__entry->rb_level, __entry->oldctx, __entry->newctx

	)

);



TRACE_EVENT(adreno_gpu_fault,

	TP_PROTO(unsigned int ctx, unsigned int ts,

		unsigned int status, unsigned int rptr, unsigned int wptr,

		u64 ib1base, unsigned int ib1size,

		u64 ib2base, unsigned int ib2size, int rb_id),

	TP_ARGS(ctx, ts, status, rptr, wptr, ib1base, ib1size, ib2base,

		ib2size, rb_id),

	TP_STRUCT__entry(

		__field(unsigned int, ctx)

		__field(unsigned int, ts)

		__field(unsigned int, status)

		__field(unsigned int, rptr)

		__field(unsigned int, wptr)

		__field(u64, ib1base)

		__field(unsigned int, ib1size)

		__field(u64, ib2base)

		__field(unsigned int, ib2size)

		__field(int, rb_id)

	),

	TP_fast_assign(

		__entry->ctx = ctx;

		__entry->ts = ts;

		__entry->status = status;

		__entry->rptr = rptr;

		__entry->wptr = wptr;

		__entry->ib1base = ib1base;

		__entry->ib1size = ib1size;

		__entry->ib2base = ib2base;

		__entry->ib2size = ib2size;

		__entry->rb_id = rb_id;

	),

	TP_printk(

		"ctx=%d ts=%d rb_id=%d status=%X RB=%X/%X IB1=%llX/%X IB2=%llX/%X",

		__entry->ctx, __entry->ts, __entry->rb_id, __entry->status,

		__entry->wptr, __entry->rptr, __entry->ib1base,

		__entry->ib1size, __entry->ib2base, __entry->ib2size)

);



TRACE_EVENT(adreno_sp_tp,



	TP_PROTO(unsigned long ip),



	TP_ARGS(ip),



	TP_STRUCT__entry(

		__field(unsigned long, ip)

	),



	TP_fast_assign(

		__entry->ip = ip;

	),



	TP_printk(

		"func=%pS", (void *) __entry->ip

	)

);



/*

 * Tracepoint for a5xx irq. Includes status info

 */

TRACE_EVENT(kgsl_a5xx_irq_status,



	TP_PROTO(struct adreno_device *adreno_dev, unsigned int status),



	TP_ARGS(adreno_dev, status),



	TP_STRUCT__entry(

		__string(device_name, adreno_dev->dev.name)

		__field(unsigned int, status)

	),



	TP_fast_assign(

		adreno_assign_str(device_name, adreno_dev->dev.name);

		__entry->status = status;

	),



	TP_printk(

		"d_name=%s status=%s",

		__get_str(device_name),

		__entry->status ? __print_flags(__entry->status, "|",

			{ BIT(A5XX_INT_RBBM_GPU_IDLE), "RBBM_GPU_IDLE" },

			{ BIT(A5XX_INT_RBBM_AHB_ERROR), "RBBM_AHB_ERR" },

			{ BIT(A5XX_INT_RBBM_TRANSFER_TIMEOUT),

				"RBBM_TRANSFER_TIMEOUT" },

			{ BIT(A5XX_INT_RBBM_ME_MS_TIMEOUT),

				"RBBM_ME_MS_TIMEOUT" },

			{ BIT(A5XX_INT_RBBM_PFP_MS_TIMEOUT),

				"RBBM_PFP_MS_TIMEOUT" },

			{ BIT(A5XX_INT_RBBM_ETS_MS_TIMEOUT),

				"RBBM_ETS_MS_TIMEOUT" },

			{ BIT(A5XX_INT_RBBM_ATB_ASYNC_OVERFLOW),

				"RBBM_ATB_ASYNC_OVERFLOW" },

			{ BIT(A5XX_INT_RBBM_GPC_ERROR), "RBBM_GPC_ERR" },

			{ BIT(A5XX_INT_CP_SW), "CP_SW" },

			{ BIT(A5XX_INT_CP_HW_ERROR), "CP_OPCODE_ERROR" },

			{ BIT(A5XX_INT_CP_CCU_FLUSH_DEPTH_TS),

				"CP_CCU_FLUSH_DEPTH_TS" },

			{ BIT(A5XX_INT_CP_CCU_FLUSH_COLOR_TS),

				"CP_CCU_FLUSH_COLOR_TS" },

			{ BIT(A5XX_INT_CP_CCU_RESOLVE_TS),

				"CP_CCU_RESOLVE_TS" },

			{ BIT(A5XX_INT_CP_IB2), "CP_IB2_INT" },

			{ BIT(A5XX_INT_CP_IB1), "CP_IB1_INT" },

			{ BIT(A5XX_INT_CP_RB), "CP_RB_INT" },

			{ BIT(A5XX_INT_CP_UNUSED_1), "CP_UNUSED_1" },

			{ BIT(A5XX_INT_CP_RB_DONE_TS), "CP_RB_DONE_TS" },

			{ BIT(A5XX_INT_CP_WT_DONE_TS), "CP_WT_DONE_TS" },

			{ BIT(A5XX_INT_UNKNOWN_1), "UNKNOWN_1" },

			{ BIT(A5XX_INT_CP_CACHE_FLUSH_TS),