text A618 SOURCE PART_3_BLOCK_07 6 views

	int reset_counter; /* Track how many GPU core resets have occurred */

	struct kthread_worker *events_worker;



	/* Number of active contexts seen globally for this device */

	int active_context_count;

	struct kobject gpu_sysfs_kobj;

	unsigned int l3_freq[3];

	unsigned int num_l3_pwrlevels;

	/* store current L3 vote to determine if we should change our vote */

	unsigned int cur_l3_pwrlevel;

	/** @globals: List of global memory objects */

	struct list_head globals;

	/** @globlal_map: bitmap for global memory allocations */

	unsigned long *global_map;

	/* @qdss_desc: Memory descriptor for the QDSS region if applicable */

	struct kgsl_memdesc *qdss_desc;

	/* @qtimer_desc: Memory descriptor for the QDSS region if applicable */

	struct kgsl_memdesc *qtimer_desc;

	/** @event_groups: List of event groups for this device */

	struct list_head event_groups;

	/** @event_groups_lock: A R/W lock for the events group list */

	rwlock_t event_groups_lock;

	/** @speed_bin: Speed bin for the GPU device if applicable */

	u32 speed_bin;

	/** @debug_bus_bin: Debug bus bin for the GPU device if applicable */

	u32 debug_bus_bin;

	/** @soc_code: Identifier containing product and feature code */

	u32 soc_code;

	/** @gmu_fault: Set when a gmu or rgmu fault is encountered */

	bool gmu_fault;

	/** @regmap: GPU register map */

	struct kgsl_regmap regmap;

	/** @timelines: Iterator for assigning IDs to timelines */

	struct idr timelines;

	/** @timelines_lock: Spinlock to protect the timelines idr */

	spinlock_t timelines_lock;

	/** @fence_trace_array: A local trace array for fence debugging */

	struct trace_array *fence_trace_array;

	/** @l3_vote: Enable/Disable l3 voting */

	bool l3_vote;

	/** @pdev_loaded: Flag to test if platform driver is probed */

	bool pdev_loaded;

	/** @nh: Pointer to head of the SRCU notifier chain */

	struct srcu_notifier_head nh;

	/** @bcl_data_kobj: Kobj for bcl_data sysfs node */

	struct kobject bcl_data_kobj;

	/** @work_period_timer: Timer to capture application GPU work stats */

	struct timer_list work_period_timer;

	/** @work_period_lock: Lock to protect application GPU work periods */

	spinlock_t work_period_lock;

	/** @work_period_ws: Work struct to emulate application GPU work events */

	struct work_struct work_period_ws;

	/** @flags: Flags for gpu_period stats */

	unsigned long flags;

	struct {

		u64 begin;

		u64 end;

	} gpu_period;

	/** @idle_jiffies: Latest idle jiffies */

	unsigned long idle_jiffies;

	/** @dump_all_ibs: Whether to dump all ibs in snapshot */

	bool dump_all_ibs;

	/** @freq_limiter_irq_clear: reset controller to clear freq limiter irq */

	struct reset_control *freq_limiter_irq_clear;

	/** @freq_limiter_intr_num: The interrupt number for freq limiter */

	int freq_limiter_intr_num;

	/** @cx_host_irq_num: Interrupt number for cx_host_irq */

	int cx_host_irq_num;

	/**

	 * @max_syncobj_hw_fence_count: Maximum number of hardware fences that are allowed in a sync

	 * object

	 */

	u32 max_syncobj_hw_fence_count;

	/** @file_mutex: Mutex to protect device open and close operations */

	struct mutex file_mutex;

	/** @host_based_dcvs: Set when KGSL is in charge of DCVS */

	bool host_based_dcvs;

};



#define KGSL_MMU_DEVICE(_mmu) \

	container_of((_mmu), struct kgsl_device, mmu)



/**

 * enum bits for struct kgsl_context.priv

 * @KGSL_CONTEXT_PRIV_SUBMITTED - The context has submitted commands to gpu.

 * @KGSL_CONTEXT_PRIV_DETACHED  - The context has been destroyed by userspace

 *	and is no longer using the gpu.

 * @KGSL_CONTEXT_PRIV_INVALID - The context has been destroyed by the kernel

 *	because it caused a GPU fault.

 * @KGSL_CONTEXT_PRIV_PAGEFAULT - The context has caused a page fault.

 * @KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC - this value and higher values are

 *	reserved for devices specific use.

 * @KGSL_CONTEXT_PRIV_INVALID_DRAIN_HW_FENCE - this context got invalidated

 * and needs its hardware fences drained after device reset

 */

enum kgsl_context_priv {

	KGSL_CONTEXT_PRIV_SUBMITTED = 0,

	KGSL_CONTEXT_PRIV_DETACHED,

	KGSL_CONTEXT_PRIV_INVALID,

	KGSL_CONTEXT_PRIV_PAGEFAULT,

	KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC = 16,

	KGSL_CONTEXT_PRIV_INVALID_DRAIN_HW_FENCE,

};



struct kgsl_process_private;



#define KGSL_MAX_FAULT_ENTRIES 40



/* Maintain faults observed within threshold time (in milliseconds) */

#define KGSL_MAX_FAULT_TIME_THRESHOLD 5000



/**

 * struct kgsl_fault_node - GPU fault descriptor

 * @node: List node for list of faults

 * @type: Type of fault

 * @priv: Pointer to type specific fault

 * @time: Time when fault was observed

 */

struct kgsl_fault_node {

	struct list_head node;

	u32 type;

	void *priv;

	ktime_t time;

};



/**

 * struct kgsl_context - The context fields that are valid for a user defined

 * context

 * @refcount: kref object for reference counting the context

 * @id: integer identifier for the context

 * @priority; The context's priority to submit commands to GPU

 * @tid: task that created this context.

 * @dev_priv: pointer to the owning device instance

 * @proc_priv: pointer to process private, the process that allocated the

 * context

 * @priv: in-kernel context flags, use KGSL_CONTEXT_* values

 * @reset_status: status indication whether a gpu reset occurred and whether

 * this context was responsible for causing it

 * @timeline: sync timeline used to create fences that can be signaled when a

 * sync_pt timestamp expires

 * @events: A kgsl_event_group for this context - contains the list of GPU

 * events

 * @flags: flags from userspace controlling the behavior of this context

 * @pwr_constraint: power constraint from userspace for this context

 * @fault_count: number of times gpu hanged in last _context_throttle_time ms

 * @fault_time: time of the first gpu hang in last _context_throttle_time ms

 * @user_ctxt_record: memory descriptor used by CP to save/restore VPC data

 * across preemption

 * @total_fault_count: number of times gpu faulted in this context

 * @last_faulted_cmd_ts: last faulted command batch timestamp

 * @gmu_registered: whether context is registered with gmu or not

 */

struct kgsl_context {

	struct kref refcount;

	uint32_t id;

	uint32_t priority;

	pid_t tid;

	struct kgsl_device_private *dev_priv;

	struct kgsl_process_private *proc_priv;

	unsigned long priv;

	struct kgsl_device *device;

	unsigned int reset_status;

	struct kgsl_sync_timeline *ktimeline;

	struct kgsl_event_group events;

	unsigned int flags;

	struct kgsl_pwr_constraint pwr_constraint;

	struct kgsl_pwr_constraint l3_pwr_constraint;

	unsigned int fault_count;

	ktime_t fault_time;

	struct kgsl_mem_entry *user_ctxt_record;

	unsigned int total_fault_count;

	unsigned int last_faulted_cmd_ts;

	bool gmu_registered;

	/**

	 * @gmu_dispatch_queue: dispatch queue id to which this context will be

	 * submitted

	 */

	u32 gmu_dispatch_queue;

	/** @faults: List of @kgsl_fault_node to store fault information */

	struct list_head faults;

	/** @fault_lock: Mutex to protect faults */

	struct mutex fault_lock;

	/** @deferred_destroy_ws: Work struct used to destroy context in a deferred manner */

	struct work_struct deferred_destroy_ws;

};



#define _context_comm(_c) \

	(((_c) && (_c)->proc_priv) ? (_c)->proc_priv->comm : "unknown")



/*

 * Print log messages with the context process name/pid:

 * [...] kgsl kgsl-3d0: kgsl-api-test[22182]:

 */



#define pr_context(_d, _c, fmt, args...) \

		dev_err((_d)->dev, "%s[%d]: " fmt, \

		_context_comm((_c)), \

		pid_nr((_c)->proc_priv->pid), ##args)



/**

 * struct kgsl_dcvs_profile_private - Private structure for a KGSL DCVS profile

 * @gmu_registered: True if DCVS profile is registered with GMU

 * @user_profile_registered: True if user DCVS IOCTL profile is received

 * @md: Memory descriptor for the DCVS profile region

 * @profile_mutex: Mutex lock to protect kgsl_dcvs_profile_private

 */

struct kgsl_dcvs_profile_private {

	bool gmu_registered;

	bool user_profile_registered;

	struct kgsl_memdesc md;

	struct mutex profile_mutex;

};



/**

 * struct kgsl_process_private -  Private structure for a KGSL process (across

 * all devices)

 * @priv: Internal flags, use KGSL_PROCESS_* values

 * @pid: Identification structure for the task owner of the process

 * @comm: task name of the process

 * @mm: mm_struct of the opening process, used to prevent other mm mmap

 * @mem_lock: Spinlock to protect the process memory lists

 * @refcount: kref object for reference counting the process

 * @idr: Iterator for assigning IDs to memory allocations

 * @pagetable: Pointer to the pagetable owned by this process

 * @kobj: Pointer to a kobj for the sysfs directory for this process

 * @debug_root: Pointer to the debugfs root for this process

 * @stats: Memory allocation statistics for this process

 * @gpumem_mapped: KGSL memory mapped in the process address space

 * @syncsource_idr: sync sources created by this process

 * @syncsource_lock: Spinlock to protect the syncsource idr

 * @fd_count: Counter for the number of FDs for this process

 * @ctxt_count: Count for the number of contexts for this process

 * @ctxt_count_lock: Spinlock to protect ctxt_count

 * @frame_count: Count for the number of frames processed

 */

struct kgsl_process_private {

	unsigned long priv;

	struct pid *pid;

	char comm[TASK_COMM_LEN];

	struct mm_struct *mm;

	spinlock_t mem_lock;

	struct kref refcount;

	struct idr mem_idr;

	struct kgsl_pagetable *pagetable;

	struct list_head list;

	struct list_head reclaim_list;

	struct kobject kobj;

	struct dentry *debug_root;

	struct {

		atomic64_t cur;

		uint64_t max;

	} stats[KGSL_MEM_ENTRY_MAX];

	atomic64_t gpumem_mapped;

	struct idr syncsource_idr;

	spinlock_t syncsource_lock;

	int fd_count;

	atomic_t ctxt_count;

	spinlock_t ctxt_count_lock;

	atomic64_t frame_count;

	/**

	 * @state: state consisting KGSL_PROC_STATE and KGSL_PROC_PINNED_STATE

	 */

	unsigned long state;

	/**

	 * @unpinned_page_count: The number of pages unpinned for reclaim

	 */

	atomic_t unpinned_page_count;

	/**

	 * @fg_work: Work struct to schedule foreground work

	 */

	struct work_struct fg_work;

	/**

	 * @reclaim_lock: Mutex lock to protect KGSL_PROC_PINNED_STATE

	 */

	struct mutex reclaim_lock;

	/** @period: Stats for GPU utilization */

	struct gpu_work_period *period;

	/**

	 * @cmd_count: The number of cmds that are active for the process

	 */

	atomic_t cmd_count;

	/**

	 * @kobj_memtype: Pointer to a kobj for memtype sysfs directory for this

	 * process

	 */

	struct kobject kobj_memtype;

	/**

	 * @private_mutex: Mutex lock to protect kgsl_process_private

	 */

	struct mutex private_mutex;

	/**

	 * @cmdline: Cmdline string of the process

	 */

	char *cmdline;

	/** @fault_count: Count of GPU faults from this process */

	u32 fault_count;

	/** @pf_count: Total count of pagefaults from this process */

	u32 pf_count;

	/** @pf_type_counts: Count of pagefaults of each type from this process */

	u32 pf_type_counts[KGSL_IOMMU_PAGEFAULT_TYPES];

	/** @profile: Container for the DCVS profile */

	struct kgsl_dcvs_profile_private profile;

};



struct kgsl_device_private {

	struct kgsl_device *device;

	struct kgsl_process_private *process_priv;

};



/**

 * struct kgsl_snapshot - details for a specific snapshot instance

 * @ib1base: Active IB1 base address at the time of fault

 * @ib2base: Active IB2 base address at the time of fault

 * @ib3base: Active IB3 base address at the time of fault

 * @ib1size: Number of DWORDS pending in IB1 at the time of fault

 * @ib2size: Number of DWORDS pending in IB2 at the time of fault

 * @ib3size: Number of DWORDS pending in IB3 at the time of fault

 * @ib1dumped: Active IB1 dump status to sansphot binary

 * @ib2dumped: Active IB2 dump status to sansphot binary

 * @start: Pointer to the start of the static snapshot region

 * @size: Size of the current snapshot instance

 * @ptr: Pointer to the next block of memory to write to during snapshotting

 * @remain: Bytes left in the snapshot region

 * @timestamp: Timestamp of the snapshot instance (in seconds since boot)

 * @mempool: Pointer to the memory pool for storing memory objects

 * @mempool_size: Size of the memory pool

 * @obj_list: List of frozen GPU buffers that are waiting to be dumped.

 * @cp_list: List of IB's to be dumped.

 * @work: worker to dump the frozen memory

 * @dump_gate: completion gate signaled by worker when it is finished.

 * @process: the process that caused the hang, if known.

 * @sysfs_read: Count of current reads via sysfs

 * @first_read: True until the snapshot read is started

 * @recovered: True if GPU was recovered after previous snapshot

 */

struct kgsl_snapshot {

	u64 ib1base;

	u64 ib2base;

	u64 ib3base;

	u32 ib1size;

	u32 ib2size;

	u32 ib3size;

	bool ib1dumped;

	bool ib2dumped;

	u64 ib1base_lpac;

	u64 ib2base_lpac;

	u32 ib1size_lpac;

	u32 ib2size_lpac;

	bool ib1dumped_lpac;

	bool ib2dumped_lpac;

	u8 *start;

	size_t size;

	u8 *ptr;

	size_t remain;

	unsigned long timestamp;

	u8 *mempool;

	size_t mempool_size;

	struct list_head obj_list;

	struct list_head cp_list;

	struct work_struct work;

	struct completion dump_gate;

	struct kgsl_process_private *process;

	struct kgsl_process_private *process_lpac;

	unsigned int sysfs_read;

	bool first_read;

	bool recovered;

	struct kgsl_device *device;

};



/**

 * struct kgsl_snapshot_object  - GPU memory in the snapshot

 * @gpuaddr: The GPU address identified during snapshot

 * @size: The buffer size identified during snapshot

 * @offset: offset from start of the allocated kgsl_mem_entry

 * @type: SNAPSHOT_OBJ_TYPE_* identifier.

 * @entry: the reference counted memory entry for this buffer

 * @node: node for kgsl_snapshot.obj_list

 */

struct kgsl_snapshot_object {

	uint64_t gpuaddr;

	uint64_t size;

	uint64_t offset;

	int type;

	struct kgsl_mem_entry *entry;

	struct list_head node;

};



struct kgsl_device *kgsl_get_device(int dev_idx);



static inline void kgsl_regread(struct kgsl_device *device,

				unsigned int offsetwords,

				unsigned int *value)

{

	*value = kgsl_regmap_read(&device->regmap, offsetwords);

}



static inline void kgsl_regread64(struct kgsl_device *device,

				  u32 offsetwords_lo, u32 offsetwords_hi,

				  u64 *value)

{

	u32 val_lo = 0, val_hi = 0;



	val_lo = kgsl_regmap_read(&device->regmap, offsetwords_lo);

	val_hi = kgsl_regmap_read(&device->regmap, offsetwords_hi);



	*value = (((u64)val_hi << 32) | val_lo);

}



static inline void kgsl_regwrite(struct kgsl_device *device,

				 unsigned int offsetwords,

				 unsigned int value)

{

	kgsl_regmap_write(&device->regmap, value, offsetwords);

}



static inline void kgsl_regrmw(struct kgsl_device *device,

		unsigned int offsetwords,

		unsigned int mask, unsigned int bits)

{

	kgsl_regmap_rmw(&device->regmap, offsetwords, mask, bits);

}



static inline bool kgsl_state_is_awake(struct kgsl_device *device)

{

	return (device->state == KGSL_STATE_ACTIVE ||

		device->state == KGSL_STATE_AWARE);

}



/**

 * kgsl_start_idle_timer - Start the idle timer

 * @device: A KGSL device handle

 *

 * Start the idle timer to expire in 'interval_timeout' milliseconds

 */

static inline void kgsl_start_idle_timer(struct kgsl_device *device)

{

	device->idle_jiffies = jiffies +

		msecs_to_jiffies(atomic64_read(&device->pwrctrl.interval_timeout));

	mod_timer(&device->idle_timer, device->idle_jiffies);

}



int kgsl_readtimestamp(struct kgsl_device *device, void *priv,

		enum kgsl_timestamp_type type, unsigned int *timestamp);



bool kgsl_check_timestamp(struct kgsl_device *device,

		struct kgsl_context *context, unsigned int timestamp);



int kgsl_device_platform_probe(struct kgsl_device *device);



void kgsl_device_platform_remove(struct kgsl_device *device);



const char *kgsl_pwrstate_to_str(unsigned int state);



/**

 * kgsl_device_snapshot_probe - add resources for the device GPU snapshot

 * @device: The device to initialize

 * @size: The size of the static region to allocate

 *

 * Allocate memory for a GPU snapshot for the specified device,

 * and create the sysfs files to manage it

 */

void kgsl_device_snapshot_probe(struct kgsl_device *device, u32 size);



void kgsl_device_snapshot(struct kgsl_device *device,

			struct kgsl_context *context, struct kgsl_context *context_lpac,

			bool gmu_fault);

void kgsl_device_snapshot_close(struct kgsl_device *device);



void kgsl_events_init(void);

void kgsl_events_exit(void);



/**

 * kgsl_device_events_probe - Set up events for the KGSL device

 * @device: A KGSL GPU device handle

 *

 * Set up the list and lock for GPU events for this device

 */

void kgsl_device_events_probe(struct kgsl_device *device);



/**

 * kgsl_device_events_remove - Remove all event groups from the KGSL device

 * @device: A KGSL GPU device handle

 *

 * Remove all of the GPU event groups from the device and warn if any of them

 * still have events pending

 */

void kgsl_device_events_remove(struct kgsl_device *device);



void kgsl_context_detach(struct kgsl_context *context);



/**

 * kgsl_del_event_group - Remove a GPU event group from a device

 * @device: A KGSL GPU device handle

 * @group: Event group to be removed

 *

 * Remove the specified group from the list of event groups on @device.

 */

void kgsl_del_event_group(struct kgsl_device *device,

		struct kgsl_event_group *group);



/**

 * kgsl_add_event_group - Add a new GPU event group

 * @device: A KGSL GPU device handle

 * @group: Pointer to the new group to add to the list

 * @context: Context that owns the group (or NULL for global)

 * @readtimestamp: Function pointer to the readtimestamp function to call when

 * processing events

 * @priv: Priv member to pass to the readtimestamp function

 * @fmt: The format string to use to build the event name

 * @...: Arguments for the format string

 */

void kgsl_add_event_group(struct kgsl_device *device,

		struct kgsl_event_group *group,

		struct kgsl_context *context, readtimestamp_func readtimestamp,

		void *priv, const char *fmt, ...);



void kgsl_cancel_events_timestamp(struct kgsl_device *device,

		struct kgsl_event_group *group, unsigned int timestamp);

void kgsl_cancel_events(struct kgsl_device *device,

		struct kgsl_event_group *group);

void kgsl_cancel_event(struct kgsl_device *device,

		struct kgsl_event_group *group, unsigned int timestamp,

		kgsl_event_func func, void *priv);

bool kgsl_event_pending(struct kgsl_device *device,

		struct kgsl_event_group *group, unsigned int timestamp,

		kgsl_event_func func, void *priv);

int kgsl_add_event(struct kgsl_device *device, struct kgsl_event_group *group,

		unsigned int timestamp, kgsl_event_func func, void *priv);

void kgsl_process_event_group(struct kgsl_device *device,

	struct kgsl_event_group *group);

void kgsl_flush_event_group(struct kgsl_device *device,

		struct kgsl_event_group *group);

void kgsl_process_event_groups(struct kgsl_device *device);



void kgsl_context_destroy(struct kref *kref);



int kgsl_context_init(struct kgsl_device_private *dev_priv,

		struct kgsl_context *context);



void kgsl_context_dump(struct kgsl_context *context);



int kgsl_memfree_find_entry(pid_t ptname, uint64_t *gpuaddr,

	uint64_t *size, uint64_t *flags, pid_t *pid);



long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);



long kgsl_ioctl_copy_in(unsigned int kernel_cmd, unsigned int user_cmd,

		unsigned long arg, unsigned char *ptr);



long kgsl_ioctl_copy_out(unsigned int kernel_cmd, unsigned int user_cmd,

		unsigned long arg, unsigned char *ptr);



/**

 * kgsl_context_type - Return a symbolic string for the context type

 * @type: Context type

 *

 * Return: Symbolic string representing the context type

 */

const char *kgsl_context_type(int type);



/**

 * kgsl_context_put() - Release context reference count

 * @context: Pointer to the KGSL context to be released

 *

 * Reduce the reference count on a KGSL context and destroy it if it is no

 * longer needed

 */

static inline void

kgsl_context_put(struct kgsl_context *context)

{

	if (context)

		kref_put(&context->refcount, kgsl_context_destroy);

}



/*

 * kgsl_context_put_deferred() - Puts refcount and triggers deferred

 * context destroy when refcount is the last refcount.

 * @context: context to put

 *

 * Use this to put a context from within atomic context

 */

static inline void kgsl_context_put_deferred(struct kgsl_context *context)

{

	if (context)

		kref_put(&context->refcount, kgsl_context_destroy_deferred);

}



/**

 * kgsl_context_detached() - check if a context is detached

 * @context: the context

 *

 * Check if a context has been destroyed by userspace and is only waiting

 * for reference counts to go away. This check is used to weed out

 * contexts that shouldn't use the gpu so NULL is considered detached.

 */

static inline bool kgsl_context_detached(struct kgsl_context *context)

{

	return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_DETACHED,

						&context->priv));

}



/**

 * kgsl_context_invalid() - check if a context is invalid

 * @context: the context

 *

 * Check if a context has been invalidated by the kernel and may no

 * longer use the GPU.

 */

static inline bool kgsl_context_invalid(struct kgsl_context *context)

{

	return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_INVALID,

						&context->priv));

}



/** kgsl_context_is_bad - Check if a context is detached or invalid

 * @context: Pointer to a KGSL context handle

 *

 * Return: True if the context has been detached or is invalid

 */

static inline bool kgsl_context_is_bad(struct kgsl_context *context)

{

	return (kgsl_context_detached(context) ||

		kgsl_context_invalid(context));

}



/** kgsl_check_context_state - Check if a context is bad or invalid

 *  @context: Pointer to a KGSL context handle

 *

 * Return: True if the context has been marked bad or invalid

 */

static inline int kgsl_check_context_state(struct kgsl_context *context)

{

	if (kgsl_context_invalid(context))

		return -EDEADLK;



	if (kgsl_context_detached(context))

		return -ENOENT;



	return 0;

}



/**

 * kgsl_context_get() - get a pointer to a KGSL context

 * @device: Pointer to the KGSL device that owns the context

 * @id: Context ID

 *

 * Find the context associated with the given ID number, increase the reference

 * count on it and return it.  The caller must make sure that this call is

 * paired with a kgsl_context_put.  This function is for internal use because it

 * doesn't validate the ownership of the context with the calling process - use

 * kgsl_context_get_owner for that

 */

static inline struct kgsl_context *kgsl_context_get(struct kgsl_device *device,

		uint32_t id)

{

	int result = 0;

	struct kgsl_context *context = NULL;



	read_lock(&device->context_lock);



	context = idr_find(&device->context_idr, id);



	/* Don't return a context that has been detached */

	if (kgsl_context_detached(context))

		context = NULL;

	else

		result = kref_get_unless_zero(&context->refcount);



	read_unlock(&device->context_lock);



	if (!result)

		return NULL;

	return context;

}



/**

 * _kgsl_context_get() - lightweight function to just increment the ref count

 * @context: Pointer to the KGSL context

 *

 * Get a reference to the specified KGSL context structure. This is a

 * lightweight way to just increase the refcount on a known context rather than

 * walking through kgsl_context_get and searching the iterator

 */

static inline int _kgsl_context_get(struct kgsl_context *context)

{

	int ret = 0;



	if (context)

		ret = kref_get_unless_zero(&context->refcount);



	return ret;

}



/**

 * kgsl_context_get_owner() - get a pointer to a KGSL context in a specific

 * process

 * @dev_priv: Pointer to the process struct

 * @id: Context ID to return

 *

 * Find the context associated with the given ID number, increase the reference

 * count on it and return it.  The caller must make sure that this call is

 * paired with a kgsl_context_put. This function validates that the context id

 * given is owned by the dev_priv instancet that is passed in.  See

 * kgsl_context_get for the internal version that doesn't do the check

 */

static inline struct kgsl_context *kgsl_context_get_owner(

		struct kgsl_device_private *dev_priv, uint32_t id)

{

	struct kgsl_context *context;



	context = kgsl_context_get(dev_priv->device, id);



	/* Verify that the context belongs to current calling fd. */

	if (context != NULL && context->dev_priv != dev_priv) {

		kgsl_context_put(context);

		return NULL;

	}



	return context;

}



/**

 * kgsl_process_private_get() - increment the refcount on a

 * kgsl_process_private struct

 * @process: Pointer to the KGSL process_private

 *

 * Returns 0 if the structure is invalid and a reference count could not be

 * obtained, nonzero otherwise.

 */

static inline int kgsl_process_private_get(struct kgsl_process_private *process)

{

	if (process != NULL)

		return kref_get_unless_zero(&process->refcount);

	return 0;

}



void kgsl_process_private_put(struct kgsl_process_private *private);





struct kgsl_process_private *kgsl_process_private_find(pid_t pid);



/*

 * A helper macro to print out "not enough memory functions" - this

 * makes it easy to standardize the messages as well as cut down on

 * the number of strings in the binary

 */

#define SNAPSHOT_ERR_NOMEM(_d, _s) \

	dev_err_ratelimited((_d)->dev, \

	"snapshot: not enough snapshot memory for section %s\n", (_s))



/**

 * struct kgsl_snapshot_registers - list of registers to snapshot

 * @regs: Pointer to an array of register ranges

 * @count: Number of entries in the array

 */

struct kgsl_snapshot_registers {

	const unsigned int *regs;

	unsigned int count;

};



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

		size_t remain, void *priv);



void kgsl_snapshot_indexed_registers(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot, unsigned int index,

	unsigned int data, unsigned int start, unsigned int count);



/**

 * kgsl_snapshot_indexed_registers_v2 - Add a set of indexed registers to the

 * snapshot

 * @device: Pointer to the KGSL device being snapshotted

 * @snapshot: Snapshot instance

 * @index: Offset for the index register

 * @data: Offset for the data register

 * @start: Index to start reading

 * @count: Number of entries to read

 * @pipe_id: Pipe ID to be dumped

 * @slice_id: Slice ID to be dumped

 *

 * Dump the values from an indexed register group into the snapshot

 */

void kgsl_snapshot_indexed_registers_v2(struct kgsl_device *device,

	struct kgsl_snapshot *snapshot, u32 index, u32 data,

	u32 start, u32 count, u32 pipe_id, u32 slice_id);



int kgsl_snapshot_get_object(struct kgsl_snapshot *snapshot,

	struct kgsl_process_private *process, uint64_t gpuaddr,

	uint64_t size, unsigned int type);



int kgsl_snapshot_have_object(struct kgsl_snapshot *snapshot,

	struct kgsl_process_private *process,

	uint64_t gpuaddr, uint64_t size);



struct adreno_ib_object_list;



int kgsl_snapshot_add_ib_obj_list(struct kgsl_snapshot *snapshot,

	struct adreno_ib_object_list *ib_obj_list);



void kgsl_snapshot_add_section(struct kgsl_device *device, u16 id,

	struct kgsl_snapshot *snapshot,

	size_t (*func)(struct kgsl_device *, u8 *, size_t, void *),

	void *priv);



/**

 * kgsl_of_property_read_ddrtype - Get property from devicetree based on

 * the type of DDR.

 * @node: Devicetree node

 * @base: prefix string of the property

 * @ptr:  Pointer to store the value of the property

 *

 * First look up the devicetree property based on the prefix string and DDR

 * type. If property is not specified per DDR type, then look for the property

 * based on prefix string only.

 *

 * Return: 0 on success or error code on failure.

 */

int kgsl_of_property_read_ddrtype(struct device_node *node, const char *base,

		u32 *ptr);



/**

 * kgsl_query_property_list - Get a list of valid properties

 * @device: A KGSL device handle

 * @list: Pointer to a list of u32s

 * @count: Number of items in @list

 *

 * Populate a list with the IDs for supported properties. If @list is NULL,

 * just return the number of properties available, otherwise fill up to @count

 * items in the list with property identifiers.

 *

 * Returns the number of total properties if @list is NULL or the number of

 * properties copied to @list.

 */

int kgsl_query_property_list(struct kgsl_device *device, u32 *list, u32 count);



static inline bool kgsl_mmu_has_feature(struct kgsl_device *device,

		enum kgsl_mmu_feature feature)

{

	return test_bit(feature, &device->mmu.features);

}



static inline void kgsl_mmu_set_feature(struct kgsl_device *device,

	      enum kgsl_mmu_feature feature)

{

	set_bit(feature, &device->mmu.features);

}



/**

 * kgsl_add_fault - Add fault information for a context

 * @context: Pointer to the KGSL context

 * @type: type of fault info

 * @priv: Pointer to type specific fault info

 *

 * Return: 0 on success or error code on failure.

 */

int kgsl_add_fault(struct kgsl_context *context, u32 type, void *priv);



/**

 * kgsl_free_faults - Free fault information for a context

 * @context: Pointer to the KGSL context

 */

void kgsl_free_faults(struct kgsl_context *context);



/**

 * kgsl_trace_gpu_mem_total - Overall gpu memory usage tracking which includes

 * process allocations, imported dmabufs and kgsl globals

 * @device: A KGSL device handle

 * @delta: delta of total mapped memory size

 */

#ifdef CONFIG_TRACE_GPU_MEM

static inline void kgsl_trace_gpu_mem_total(struct kgsl_device *device,

						s64 delta)

{

	u64 total_size;



	total_size = atomic64_add_return(delta, &device->total_mapped);

	trace_gpu_mem_total(0, 0, total_size);

}

#else

static inline void kgsl_trace_gpu_mem_total(struct kgsl_device *device,

						s64 delta) {}

#endif



/*

 * kgsl_context_is_lpac() - Checks if context is LPAC

 * @context: KGSL context to check

 *

 * Function returns true if context is LPAC else false

 */

static inline bool kgsl_context_is_lpac(struct kgsl_context *context)

{

	if (context == NULL)

		return false;



	return (context->flags & KGSL_CONTEXT_LPAC) ? true : false;

}



#endif  /* __KGSL_DEVICE_H */





==========================================================================================
FILE: kgsl_gmu_core.c
LINES: 1421 | BYTES: 39394
==========================================================================================

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

/*

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

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

 */



#include <dt-bindings/power/qcom-rpmpd.h>

#include <linux/of.h>

#include <linux/io.h>

#include <linux/of_platform.h>

#include <linux/pm_opp.h>



#include "adreno.h"

#include "adreno_trace.h"

#include "kgsl_device.h"

#include "kgsl_gmu_core.h"

#include "kgsl_power_trace.h"

#include "kgsl_sync.h"

#include "kgsl_trace.h"



static const struct of_device_id gmu_match_table[] = {

	{ .compatible = "qcom,gpu-gmu", .data = &a6xx_gmu_driver },

	{ .compatible = "qcom,gpu-rgmu", .data = &a6xx_rgmu_driver },

	{ .compatible = "qcom,gen7-gmu", .data = &gen7_gmu_driver },

	{ .compatible = "qcom,gen8-gmu", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-623.0", .data = &a6xx_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-663.0", .data = &a6xx_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-635.0", .data = &a6xx_gmu_driver },

	{ .compatible = "qcom,adreno-rgmu", .data = &a6xx_rgmu_driver },

	{ .compatible = "qcom,adreno-gmu-x185.1", .data = &gen7_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-x145.0", .data = &gen7_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-x285.1", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-x285.0", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-x290.1", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-722.0", .data = &gen7_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-753.1", .data = &gen7_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-830.1", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-840.1", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-845.0", .data = &gen8_gmu_driver },

	{ .compatible = "qcom,adreno-gmu-850.1", .data = &gen8_gmu_driver },

	{},

};



void __init gmu_core_register(void)

{

	const struct of_device_id *match;

	struct device_node *node;



	node = of_find_matching_node_and_match(NULL, gmu_match_table,

		&match);

	if (!node)

		return;



	platform_driver_register((struct platform_driver *) match->data);

	of_node_put(node);

}



void gmu_core_unregister(void)

{

	const struct of_device_id *match;

	struct device_node *node;



	node = of_find_matching_node_and_match(NULL, gmu_match_table,

		&match);

	if (!node)

		return;



	platform_driver_unregister((struct platform_driver *) match->data);

	of_node_put(node);

}



bool gmu_core_isenabled(struct kgsl_device *device)

{

	return test_bit(GMU_ENABLED, &device->gmu_core.flags);

}



bool gmu_core_gpmu_isenabled(struct kgsl_device *device)

{

	return (device->gmu_core.dev_ops != NULL);

}



bool gmu_core_scales_bandwidth(struct kgsl_device *device)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->scales_bandwidth)

		return ops->scales_bandwidth(device);



	return false;

}



int gmu_core_dev_acd_set(struct kgsl_device *device, bool val)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->acd_set)

		return ops->acd_set(device, val);



	return -EINVAL;

}



void gmu_core_regread(struct kgsl_device *device, unsigned int offsetwords,

		unsigned int *value)

{

	u32 val = kgsl_regmap_read(&device->regmap, offsetwords);

	*value  = val;

}



void gmu_core_regwrite(struct kgsl_device *device, unsigned int offsetwords,

		unsigned int value)

{

	kgsl_regmap_write(&device->regmap, value, offsetwords);

}



void gmu_core_blkwrite(struct kgsl_device *device, unsigned int offsetwords,

		const void *buffer, size_t size)

{

	kgsl_regmap_bulk_write(&device->regmap, offsetwords,

		buffer, size >> 2);

}



void gmu_core_regrmw(struct kgsl_device *device,

		unsigned int offsetwords,

		unsigned int mask, unsigned int bits)

{

	kgsl_regmap_rmw(&device->regmap, offsetwords, mask, bits);

}



int gmu_core_dev_oob_set(struct kgsl_device *device, enum oob_request req)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->oob_set)

		return ops->oob_set(device, req);



	return 0;

}



void gmu_core_dev_oob_clear(struct kgsl_device *device, enum oob_request req)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->oob_clear)

		ops->oob_clear(device, req);

}



void gmu_core_dev_cooperative_reset(struct kgsl_device *device)

{



	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->cooperative_reset)

		ops->cooperative_reset(device);

}



int gmu_core_dev_ifpc_isenabled(struct kgsl_device *device)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->ifpc_isenabled)

		return ops->ifpc_isenabled(device);



	return 0;

}



int gmu_core_dev_ifpc_store(struct kgsl_device *device, unsigned int val)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->ifpc_store)

		return ops->ifpc_store(device, val);



	return -EINVAL;

}



int gmu_core_dev_wait_for_active_transition(struct kgsl_device *device)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	if (ops && ops->wait_for_active_transition)

		return ops->wait_for_active_transition(device);



	return 0;

}



void gmu_core_fault_snapshot(struct kgsl_device *device,

			enum gmu_fault_panic_policy gf_policy)

{

	const struct gmu_dev_ops *ops = GMU_DEVICE_OPS(device);



	/* Send NMI first to halt GMU and capture the state close to the point of failure */

	if (ops && ops->send_nmi)

		ops->send_nmi(device, false, gf_policy);



	kgsl_device_snapshot(device, NULL, NULL, true);

}



int gmu_core_timed_poll_check(struct kgsl_device *device,

		unsigned int offset, unsigned int expected_ret,

		unsigned int timeout_ms, unsigned int mask)

{

	u32 val;



	return kgsl_regmap_read_poll_timeout(&device->regmap, offset,

		val, (val & mask) == expected_ret, 100, timeout_ms * 1000);

}



int gmu_core_map_memdesc(struct iommu_domain *domain, struct kgsl_memdesc *memdesc,

		u64 gmuaddr, int attrs)

{

	ssize_t mapped;



	if (!memdesc->pages) {

		mapped = kgsl_mmu_map_sg(domain, gmuaddr, memdesc->sgt->sgl,

			memdesc->sgt->nents, attrs);

	} else {

		struct sg_table sgt = { 0 };

		int ret;



		ret = sg_alloc_table_from_pages(&sgt, memdesc->pages,

			memdesc->page_count, 0, memdesc->size, GFP_KERNEL);



		if (ret)

			return ret;



		mapped = kgsl_mmu_map_sg(domain, gmuaddr, sgt.sgl, sgt.nents, attrs);

		sg_free_table(&sgt);

	}



	if (!mapped)

		mapped = -ENOMEM;



	return (mapped < 0) ? mapped : 0;

}



struct kgsl_memdesc *gmu_core_find_memdesc(struct kgsl_device *device, u32 addr, u32 size)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	int i;



	for (i = 0; i < gmu->global_entries; i++) {

		struct kgsl_memdesc *md = &gmu->gmu_globals[i];



		if ((addr >= md->gmuaddr) &&

				(((addr + size) <= (md->gmuaddr + md->size))))

			return md;

	}



	return NULL;

}



int gmu_core_find_vma_block(struct kgsl_device *device, u32 addr, u32 size)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	int i;



	for (i = 0; i < gmu->num_vmas; i++) {

		struct gmu_vma_entry *vma = &gmu->vma[i];



		if ((addr >= vma->start) &&

			((addr + size) <= (vma->start + vma->size)))

			return i;

	}



	return -ENOENT;

}



void gmu_core_free_globals(struct kgsl_device *device)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	int i;



	for (i = 0; i < gmu->global_entries && i < ARRAY_SIZE(gmu->gmu_globals); i++) {

		struct kgsl_memdesc *md = &gmu->gmu_globals[i];



		if (!md->gmuaddr)

			continue;



		iommu_unmap(gmu->domain, md->gmuaddr, md->size);



		if (TEST_FLAG(KGSL_MEMDESC_SYSMEM, &md->priv))

			kgsl_sharedmem_free(md);



		memset(md, 0, sizeof(*md));

	}



	if (gmu->domain) {

		kgsl_detach_iommu_group(gmu->domain, gmu->group);

		iommu_domain_free(gmu->domain);

		gmu->domain = NULL;

	}



	gmu->global_entries = 0;

}



static struct gmu_vma_node *find_va(struct gmu_vma_entry *vma, u32 addr, u32 size)

{

	struct rb_node *node = vma->vma_root.rb_node;



	while (node != NULL) {

		struct gmu_vma_node *data = rb_entry(node, struct gmu_vma_node, node);



		if (addr + size <= data->va)

			node = node->rb_left;

		else if (addr >= data->va + data->size)

			node = node->rb_right;

		else

			return data;

	}

	return NULL;

}



/* Return true if VMA supports dynamic allocations */

static bool vma_is_dynamic(struct kgsl_device *device, int vma_id)

{

	/* Dynamic allocations are done in the GMU_NONCACHED_KERNEL space */

	return (vma_id == GMU_NONCACHED_KERNEL) && (!adreno_is_a6xx(ADRENO_DEVICE(device)));

}



static int insert_va(struct gmu_vma_entry *vma, u32 addr, u32 size)

{

	struct rb_node **node, *parent = NULL;

	struct gmu_vma_node *new = kzalloc(sizeof(*new), GFP_NOWAIT);



	if (new == NULL)

		return -ENOMEM;



	new->va = addr;

	new->size = size;



	node = &vma->vma_root.rb_node;

	while (*node != NULL) {

		struct gmu_vma_node *this;



		parent = *node;

		this = rb_entry(parent, struct gmu_vma_node, node);



		if (addr + size <= this->va)

			node = &parent->rb_left;

		else if (addr >= this->va + this->size)

			node = &parent->rb_right;

		else {

			kfree(new);

			return -EEXIST;

		}

	}



	/* Add new node and rebalance tree */

	rb_link_node(&new->node, parent, node);

	rb_insert_color(&new->node, &vma->vma_root);



	return 0;

}



static u32 find_unmapped_va(struct gmu_vma_entry *vma, u32 size, u32 va_align)

{

	struct rb_node *node = rb_first(&vma->vma_root);

	u32 cur = vma->start;

	bool found = false;



	cur = ALIGN(cur, va_align);



	while (node) {

		struct gmu_vma_node *data = rb_entry(node, struct gmu_vma_node, node);



		if (cur + size <= data->va) {

			found = true;

			break;

		}



		cur = ALIGN(data->va + data->size, va_align);

		node = rb_next(node);

	}



	/* Do we have space after the last node? */

	if (!found && (cur + size <= vma->start + vma->size))

		found = true;

	return found ? cur : 0;

}



static int _map_gmu_dynamic(struct kgsl_device *device, struct kgsl_memdesc *md,

	u32 addr, u32 vma_id, int attrs, u32 align)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	struct device *gmu_pdev_dev = GMU_PDEV_DEV(device);

	struct gmu_vma_entry *vma = &gmu->vma[vma_id];

	struct gmu_vma_node *vma_node = NULL;

	int ret;

	u32 size = ALIGN(md->size, hfi_get_gmu_sz_alignment(align));



	spin_lock(&vma->lock);

	if (!addr) {

		/*

		 * We will end up with a hole (GMU VA range not backed by physical mapping) if

		 * the aligned size is greater than the size of the physical mapping

		 */

		addr = find_unmapped_va(vma, size, hfi_get_gmu_va_alignment(align));

		if (addr == 0) {

			spin_unlock(&vma->lock);

			dev_err(gmu_pdev_dev,

				"Insufficient VA space size: %x\n", size);

			return -ENOMEM;

		}

	}



	ret = insert_va(vma, addr, size);

	spin_unlock(&vma->lock);

	if (ret < 0) {

		dev_err(gmu_pdev_dev,

			"Could not insert va: %x size %x\n", addr, size);

		return ret;

	}



	ret = gmu_core_map_memdesc(gmu->domain, md, addr, attrs);

	if (!ret) {

		md->gmuaddr = addr;

		return 0;

	}



	/* Failed to map to GMU */

	dev_err(gmu_pdev_dev,

		"Unable to map GMU kernel block: addr:0x%08x size:0x%llx :%d\n",

		addr, md->size, ret);



	spin_lock(&vma->lock);

	vma_node = find_va(vma, md->gmuaddr, size);

	if (vma_node)

		rb_erase(&vma_node->node, &vma->vma_root);

	spin_unlock(&vma->lock);

	kfree(vma_node);



	return ret;

}



static int _map_gmu_static(struct kgsl_device *device, struct kgsl_memdesc *md,

	u32 addr, u32 vma_id, int attrs, u32 align)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	struct gmu_vma_entry *vma = &gmu->vma[vma_id];

	int ret;

	u32 size = ALIGN(md->size, hfi_get_gmu_sz_alignment(align));



	if (!addr)

		addr = ALIGN(vma->next_va, hfi_get_gmu_va_alignment(align));



	ret = gmu_core_map_memdesc(device->gmu_core.domain, md, addr, attrs);

	if (ret) {

		dev_err(GMU_PDEV_DEV(device),

			"Unable to map GMU kernel block: addr:0x%08x size:0x%llx :%d\n",

			addr, md->size, ret);

		return ret;

	}

	md->gmuaddr = addr;

	/*

	 * We will end up with a hole (GMU VA range not backed by physical mapping) if the aligned

	 * size is greater than the size of the physical mapping

	 */

	vma->next_va = md->gmuaddr + size;

	return 0;

}



int gmu_core_reserve_gmuaddr(struct kgsl_device *device, struct kgsl_memdesc *md, u32 vma_id,

	u32 align)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	struct gmu_vma_entry *vma = &gmu->vma[vma_id];

	u32 addr, size = ALIGN(md->size, hfi_get_gmu_sz_alignment(align));



	if (vma_is_dynamic(device, vma_id)) {

		spin_lock(&vma->lock);

		addr = find_unmapped_va(vma, size, hfi_get_gmu_va_alignment(align));

		spin_unlock(&vma->lock);

		if (addr == 0)

			goto error;

	} else {

		addr = ALIGN(vma->next_va, hfi_get_gmu_va_alignment(align));

		if ((addr + size) >= (vma->start + vma->size))

			goto error;

		vma->next_va = addr + size;

	}



	md->gmuaddr = addr;



	return 0;



error:

	dev_err_ratelimited(GMU_PDEV_DEV(device),

		"Insufficient VA space size: %x in vma:%u\n", size, vma_id);

	return -ENOMEM;

}



int gmu_core_map_gmu(struct kgsl_device *device, struct kgsl_memdesc *md,

	u32 addr, u32 vma_id, int attrs, u32 align)

{

	return vma_is_dynamic(device, vma_id) ?

			_map_gmu_dynamic(device, md, addr, vma_id, attrs, align) :

			_map_gmu_static(device, md, addr, vma_id, attrs, align);

}



int gmu_core_get_attrs(u32 flags)

{

	int attrs = IOMMU_READ;



	if (flags & HFI_MEMFLAG_GMU_PRIV)

		attrs |= IOMMU_PRIV;



	if (flags & HFI_MEMFLAG_GMU_WRITEABLE)

		attrs |= IOMMU_WRITE;



	return attrs;

}



int gmu_core_import_buffer(struct kgsl_device *device, struct hfi_mem_alloc_entry *entry)

{

	struct hfi_mem_alloc_desc *desc = &entry->desc;

	u32 attrs = gmu_core_get_attrs(desc->flags);

	u32 vma_id = (desc->flags & HFI_MEMFLAG_GMU_CACHEABLE) ? GMU_CACHE : GMU_NONCACHED_KERNEL;



	/*

	 * GMU Tx/Rx queues are mapped as I/O-coherent on both SOCCP and CPU,

	 * mark the buffer I/O-coherent on GMU side as well to prevent stale

	 * data and immediate updates in DDR.

	 */

	if (desc->mem_kind == HFI_MEMKIND_HW_FENCE &&

		kgsl_mmu_has_feature(device, KGSL_MMU_IO_COHERENT)) {

		entry->md->flags |= KGSL_MEMFLAGS_IOCOHERENT;

		attrs |= IOMMU_CACHE;

	}



	return gmu_core_map_gmu(device, entry->md, 0, vma_id, attrs, desc->align);

}



struct kgsl_memdesc *gmu_core_reserve_kernel_block(struct kgsl_device *device,

	u32 addr, u32 size, u32 vma_id, u32 align)

{

	int ret;

	u64 memflags = 0;

	struct kgsl_memdesc *md;

	struct gmu_core_device *gmu = &device->gmu_core;

	int attrs = IOMMU_READ | IOMMU_WRITE | IOMMU_PRIV;

	struct adreno_device *adreno_dev = ADRENO_DEVICE(device);



	if (gmu->global_entries == ARRAY_SIZE(gmu->gmu_globals))

		return ERR_PTR(-ENOMEM);



	md = &gmu->gmu_globals[gmu->global_entries];



	/*

	 * Apply WB cache policy to fetch GMU FW successfully for A622.

	 * A622 has specific cache coherency requirements that can lead to data

	 * inconsistency if not using writeback cache policy.

	 */

	if (adreno_is_a622(adreno_dev))

		memflags = FIELD_PREP(KGSL_CACHEMODE_MASK, KGSL_CACHEMODE_WRITEBACK);



	ret = kgsl_allocate_kernel(device, md, size, memflags, KGSL_MEMDESC_SYSMEM);

	if (ret) {

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

		return ERR_PTR(-ENOMEM);

	}



	ret = gmu_core_map_gmu(device, md, addr, vma_id, attrs, align);

	if (ret) {

		kgsl_sharedmem_free(md);

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

		return ERR_PTR(ret);

	}



	gmu->global_entries++;



	return md;

}



struct kgsl_memdesc *gmu_core_reserve_kernel_block_fixed(struct kgsl_device *device,

	u32 addr, u32 size, u32 vma_id, const char *resource, int attrs, u32 align)

{

	int ret;

	struct kgsl_memdesc *md;

	struct gmu_core_device *gmu = &device->gmu_core;



	if (gmu->global_entries == ARRAY_SIZE(gmu->gmu_globals))

		return ERR_PTR(-ENOMEM);



	md = &gmu->gmu_globals[gmu->global_entries];



	ret = kgsl_memdesc_init_fixed(device, GMU_PDEV(device), resource, md);

	if (ret)

		return ERR_PTR(ret);



	ret = gmu_core_map_gmu(device, md, addr, vma_id, attrs, align);



	sg_free_table(md->sgt);

	kfree(md->sgt);

	md->sgt = NULL;



	if (!ret) {

		gmu->global_entries++;

	} else {

		dev_err(GMU_PDEV_DEV(device),

			"Unable to map GMU kernel block: addr:0x%08x size:0x%llx :%d\n",

			addr, md->size, ret);

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

		md = ERR_PTR(ret);

	}

	return md;

}



int gmu_core_alloc_kernel_block(struct kgsl_device *device,

	struct kgsl_memdesc *md, u32 size, u32 vma_id, int attrs)

{

	int ret;



	ret = kgsl_allocate_kernel(device, md, size, 0, KGSL_MEMDESC_SYSMEM);

	if (ret)

		return ret;



	ret = gmu_core_map_gmu(device, md, 0, vma_id, attrs, 0);

	if (ret)

		kgsl_sharedmem_free(md);



	return ret;

}



void gmu_core_free_block(struct kgsl_device *device, struct kgsl_memdesc *md)

{

	struct gmu_core_device *gmu = &device->gmu_core;

	int vma_id = gmu_core_find_vma_block(device, md->gmuaddr, md->size);

	struct gmu_vma_entry *vma;

	struct gmu_vma_node *vma_node;



	if ((vma_id < 0) || !vma_is_dynamic(device, vma_id))

		return;



	vma = &gmu->vma[vma_id];



	/*

	 * Do not remove the vma node if we failed to unmap the entire buffer. This is because the

	 * iommu driver considers remapping an already mapped iova as fatal.

	 */

	if (md->size != iommu_unmap(device->gmu_core.domain, md->gmuaddr, md->size))

		goto free;



	spin_lock(&vma->lock);

	vma_node = find_va(vma, md->gmuaddr, md->size);

	if (vma_node)

		rb_erase(&vma_node->node, &vma->vma_root);

	spin_unlock(&vma->lock);

	kfree(vma_node);

free:

	kgsl_sharedmem_free(md);

}



int gmu_core_process_prealloc(struct kgsl_device *device, struct gmu_block_header *blk)

{

	struct kgsl_memdesc *md;

	int id = gmu_core_find_vma_block(device, blk->addr, blk->value);



	if (id < 0) {

		dev_err(GMU_PDEV_DEV(device),

			"Invalid prealloc block addr: 0x%x value:%d\n",

			blk->addr, blk->value);

		return id;

	}



	/* Nothing to do for TCM blocks or user uncached */

	if (id == GMU_ITCM || id == GMU_DTCM || id == GMU_NONCACHED_USER)