18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2018 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#ifndef __AMDGPU_CTX_H__
248c2ecf20Sopenharmony_ci#define __AMDGPU_CTX_H__
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "amdgpu_ring.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct drm_device;
298c2ecf20Sopenharmony_cistruct drm_file;
308c2ecf20Sopenharmony_cistruct amdgpu_fpriv;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define AMDGPU_MAX_ENTITY_NUM 4
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct amdgpu_ctx_entity {
358c2ecf20Sopenharmony_ci	uint64_t		sequence;
368c2ecf20Sopenharmony_ci	struct drm_sched_entity	entity;
378c2ecf20Sopenharmony_ci	struct dma_fence	*fences[];
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistruct amdgpu_ctx {
418c2ecf20Sopenharmony_ci	struct kref			refcount;
428c2ecf20Sopenharmony_ci	struct amdgpu_device		*adev;
438c2ecf20Sopenharmony_ci	unsigned			reset_counter;
448c2ecf20Sopenharmony_ci	unsigned			reset_counter_query;
458c2ecf20Sopenharmony_ci	uint32_t			vram_lost_counter;
468c2ecf20Sopenharmony_ci	spinlock_t			ring_lock;
478c2ecf20Sopenharmony_ci	struct amdgpu_ctx_entity	*entities[AMDGPU_HW_IP_NUM][AMDGPU_MAX_ENTITY_NUM];
488c2ecf20Sopenharmony_ci	bool				preamble_presented;
498c2ecf20Sopenharmony_ci	enum drm_sched_priority		init_priority;
508c2ecf20Sopenharmony_ci	enum drm_sched_priority		override_priority;
518c2ecf20Sopenharmony_ci	struct mutex			lock;
528c2ecf20Sopenharmony_ci	atomic_t			guilty;
538c2ecf20Sopenharmony_ci	unsigned long			ras_counter_ce;
548c2ecf20Sopenharmony_ci	unsigned long			ras_counter_ue;
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct amdgpu_ctx_mgr {
588c2ecf20Sopenharmony_ci	struct amdgpu_device	*adev;
598c2ecf20Sopenharmony_ci	struct mutex		lock;
608c2ecf20Sopenharmony_ci	/* protected by lock */
618c2ecf20Sopenharmony_ci	struct idr		ctx_handles;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciextern const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM];
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistruct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id);
678c2ecf20Sopenharmony_ciint amdgpu_ctx_put(struct amdgpu_ctx *ctx);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciint amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
708c2ecf20Sopenharmony_ci			  u32 ring, struct drm_sched_entity **entity);
718c2ecf20Sopenharmony_civoid amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
728c2ecf20Sopenharmony_ci			  struct drm_sched_entity *entity,
738c2ecf20Sopenharmony_ci			  struct dma_fence *fence, uint64_t *seq);
748c2ecf20Sopenharmony_cistruct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
758c2ecf20Sopenharmony_ci				       struct drm_sched_entity *entity,
768c2ecf20Sopenharmony_ci				       uint64_t seq);
778c2ecf20Sopenharmony_civoid amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
788c2ecf20Sopenharmony_ci				  enum drm_sched_priority priority);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciint amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
818c2ecf20Sopenharmony_ci		     struct drm_file *filp);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
848c2ecf20Sopenharmony_ci			       struct drm_sched_entity *entity);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_civoid amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
878c2ecf20Sopenharmony_civoid amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
888c2ecf20Sopenharmony_cilong amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
898c2ecf20Sopenharmony_civoid amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#endif
92