xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_ctx.h (revision f005ef32267c16bdb134f0e9fa4477dbe07c263a)
1c349dbc7Sjsg /*
2c349dbc7Sjsg  * Copyright 2018 Advanced Micro Devices, Inc.
3c349dbc7Sjsg  *
4c349dbc7Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5c349dbc7Sjsg  * copy of this software and associated documentation files (the "Software"),
6c349dbc7Sjsg  * to deal in the Software without restriction, including without limitation
7c349dbc7Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c349dbc7Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9c349dbc7Sjsg  * Software is furnished to do so, subject to the following conditions:
10c349dbc7Sjsg  *
11c349dbc7Sjsg  * The above copyright notice and this permission notice shall be included in
12c349dbc7Sjsg  * all copies or substantial portions of the Software.
13c349dbc7Sjsg  *
14c349dbc7Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c349dbc7Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c349dbc7Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17c349dbc7Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c349dbc7Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c349dbc7Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c349dbc7Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
21c349dbc7Sjsg  *
22c349dbc7Sjsg  */
23c349dbc7Sjsg #ifndef __AMDGPU_CTX_H__
24c349dbc7Sjsg #define __AMDGPU_CTX_H__
25c349dbc7Sjsg 
261bb76ff1Sjsg #include <linux/ktime.h>
271bb76ff1Sjsg #include <linux/types.h>
281bb76ff1Sjsg 
29c349dbc7Sjsg #include "amdgpu_ring.h"
30c349dbc7Sjsg 
31c349dbc7Sjsg struct drm_device;
32c349dbc7Sjsg struct drm_file;
33c349dbc7Sjsg struct amdgpu_fpriv;
341bb76ff1Sjsg struct amdgpu_ctx_mgr;
35c349dbc7Sjsg 
36c349dbc7Sjsg #define AMDGPU_MAX_ENTITY_NUM 4
37c349dbc7Sjsg 
38c349dbc7Sjsg struct amdgpu_ctx_entity {
391bb76ff1Sjsg 	uint32_t		hw_ip;
40c349dbc7Sjsg 	uint64_t		sequence;
41c349dbc7Sjsg 	struct drm_sched_entity	entity;
42c349dbc7Sjsg 	struct dma_fence	*fences[];
43c349dbc7Sjsg };
44c349dbc7Sjsg 
45c349dbc7Sjsg struct amdgpu_ctx {
46c349dbc7Sjsg 	struct kref			refcount;
471bb76ff1Sjsg 	struct amdgpu_ctx_mgr		*mgr;
48c349dbc7Sjsg 	unsigned			reset_counter;
49c349dbc7Sjsg 	unsigned			reset_counter_query;
50*f005ef32Sjsg 	uint64_t			generation;
51c349dbc7Sjsg 	spinlock_t			ring_lock;
52c349dbc7Sjsg 	struct amdgpu_ctx_entity	*entities[AMDGPU_HW_IP_NUM][AMDGPU_MAX_ENTITY_NUM];
53c349dbc7Sjsg 	bool				preamble_presented;
541bb76ff1Sjsg 	int32_t				init_priority;
551bb76ff1Sjsg 	int32_t				override_priority;
56c349dbc7Sjsg 	atomic_t			guilty;
57c349dbc7Sjsg 	unsigned long			ras_counter_ce;
58c349dbc7Sjsg 	unsigned long			ras_counter_ue;
591bb76ff1Sjsg 	uint32_t			stable_pstate;
60*f005ef32Sjsg 	struct amdgpu_ctx_mgr		*ctx_mgr;
61c349dbc7Sjsg };
62c349dbc7Sjsg 
63c349dbc7Sjsg struct amdgpu_ctx_mgr {
64c349dbc7Sjsg 	struct amdgpu_device	*adev;
65c349dbc7Sjsg 	struct rwlock		lock;
66c349dbc7Sjsg 	/* protected by lock */
67c349dbc7Sjsg 	struct idr		ctx_handles;
681bb76ff1Sjsg 	atomic64_t		time_spend[AMDGPU_HW_IP_NUM];
69c349dbc7Sjsg };
70c349dbc7Sjsg 
71c349dbc7Sjsg extern const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM];
72c349dbc7Sjsg 
73c349dbc7Sjsg struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id);
74c349dbc7Sjsg int amdgpu_ctx_put(struct amdgpu_ctx *ctx);
75c349dbc7Sjsg 
76c349dbc7Sjsg int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
77c349dbc7Sjsg 			  u32 ring, struct drm_sched_entity **entity);
781bb76ff1Sjsg uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
79c349dbc7Sjsg 			      struct drm_sched_entity *entity,
801bb76ff1Sjsg 			      struct dma_fence *fence);
81c349dbc7Sjsg struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
82c349dbc7Sjsg 				       struct drm_sched_entity *entity,
83c349dbc7Sjsg 				       uint64_t seq);
841bb76ff1Sjsg bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio);
851bb76ff1Sjsg void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx, int32_t ctx_prio);
86c349dbc7Sjsg 
87c349dbc7Sjsg int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
88c349dbc7Sjsg 		     struct drm_file *filp);
89c349dbc7Sjsg 
90c349dbc7Sjsg int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
91c349dbc7Sjsg 			       struct drm_sched_entity *entity);
92c349dbc7Sjsg 
931bb76ff1Sjsg void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
941bb76ff1Sjsg 			 struct amdgpu_device *adev);
95c349dbc7Sjsg void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
96c349dbc7Sjsg long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
97c349dbc7Sjsg void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
981bb76ff1Sjsg void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
991bb76ff1Sjsg 			  ktime_t usage[AMDGPU_HW_IP_NUM]);
1001bb76ff1Sjsg 
101c349dbc7Sjsg #endif
102