xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /*
2  * Copyright 2022 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef AMDGPU_XCP_H
25 #define AMDGPU_XCP_H
26 
27 #include <linux/pci.h>
28 #include <linux/xarray.h>
29 
30 #include "amdgpu_ctx.h"
31 
32 #define MAX_XCP 8
33 
34 #define AMDGPU_XCP_MODE_NONE -1
35 #define AMDGPU_XCP_MODE_TRANS -2
36 
37 #define AMDGPU_XCP_FL_NONE 0
38 #define AMDGPU_XCP_FL_LOCKED (1 << 0)
39 
40 #define AMDGPU_XCP_NO_PARTITION (~0)
41 
42 #define AMDGPU_XCP_OPS_KFD	(1 << 0)
43 
44 struct amdgpu_fpriv;
45 
46 enum AMDGPU_XCP_IP_BLOCK {
47 	AMDGPU_XCP_GFXHUB,
48 	AMDGPU_XCP_GFX,
49 	AMDGPU_XCP_SDMA,
50 	AMDGPU_XCP_VCN,
51 	AMDGPU_XCP_MAX_BLOCKS
52 };
53 
54 enum AMDGPU_XCP_STATE {
55 	AMDGPU_XCP_PREPARE_SUSPEND,
56 	AMDGPU_XCP_SUSPEND,
57 	AMDGPU_XCP_PREPARE_RESUME,
58 	AMDGPU_XCP_RESUME,
59 };
60 
61 enum amdgpu_xcp_res_id {
62 	AMDGPU_XCP_RES_XCC,
63 	AMDGPU_XCP_RES_DMA,
64 	AMDGPU_XCP_RES_DEC,
65 	AMDGPU_XCP_RES_JPEG,
66 	AMDGPU_XCP_RES_MAX,
67 };
68 
69 struct amdgpu_xcp_res_details {
70 	enum amdgpu_xcp_res_id id;
71 	u8 num_inst;
72 	u8 num_shared;
73 	struct kobject kobj;
74 };
75 
76 struct amdgpu_xcp_cfg {
77 	u8 mode;
78 	struct amdgpu_xcp_res_details xcp_res[AMDGPU_XCP_RES_MAX];
79 	u8 num_res;
80 	struct amdgpu_xcp_mgr *xcp_mgr;
81 	struct kobject kobj;
82 	u16 compatible_nps_modes;
83 };
84 
85 struct amdgpu_xcp_ip_funcs {
86 	int (*prepare_suspend)(void *handle, uint32_t inst_mask);
87 	int (*suspend)(void *handle, uint32_t inst_mask);
88 	int (*prepare_resume)(void *handle, uint32_t inst_mask);
89 	int (*resume)(void *handle, uint32_t inst_mask);
90 };
91 
92 struct amdgpu_xcp_ip {
93 	struct amdgpu_xcp_ip_funcs *ip_funcs;
94 	uint32_t inst_mask;
95 
96 	enum AMDGPU_XCP_IP_BLOCK ip_id;
97 	bool valid;
98 };
99 
100 struct amdgpu_xcp {
101 	struct amdgpu_xcp_ip ip[AMDGPU_XCP_MAX_BLOCKS];
102 
103 	uint8_t id;
104 	uint8_t mem_id;
105 	bool valid;
106 	atomic_t	ref_cnt;
107 	struct drm_device *ddev;
108 	struct drm_device *rdev;
109 	struct drm_device *pdev;
110 	struct drm_driver *driver;
111 	struct drm_vma_offset_manager *vma_offset_manager;
112 	struct amdgpu_sched	gpu_sched[AMDGPU_HW_IP_NUM][AMDGPU_RING_PRIO_MAX];
113 	struct amdgpu_xcp_mgr *xcp_mgr;
114 	struct kobject kobj;
115 };
116 
117 struct amdgpu_xcp_mgr {
118 	struct amdgpu_device *adev;
119 	struct mutex xcp_lock;
120 	struct amdgpu_xcp_mgr_funcs *funcs;
121 
122 	struct amdgpu_xcp xcp[MAX_XCP];
123 	uint8_t num_xcps;
124 	int8_t mode;
125 
126 	 /* Used to determine KFD memory size limits per XCP */
127 	unsigned int num_xcp_per_mem_partition;
128 	struct amdgpu_xcp_cfg *xcp_cfg;
129 	uint32_t supp_xcp_modes;
130 	uint32_t avail_xcp_modes;
131 };
132 
133 struct amdgpu_xcp_mgr_funcs {
134 	int (*switch_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr, int mode,
135 				     int *num_xcps);
136 	int (*query_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr);
137 	int (*get_ip_details)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
138 			      enum AMDGPU_XCP_IP_BLOCK ip_id,
139 			      struct amdgpu_xcp_ip *ip);
140 	int (*get_xcp_mem_id)(struct amdgpu_xcp_mgr *xcp_mgr,
141 			      struct amdgpu_xcp *xcp, uint8_t *mem_id);
142 	int (*get_xcp_res_info)(struct amdgpu_xcp_mgr *xcp_mgr,
143 				int mode,
144 				struct amdgpu_xcp_cfg *xcp_cfg);
145 	int (*prepare_suspend)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
146 	int (*suspend)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
147 	int (*prepare_resume)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
148 	int (*resume)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
149 };
150 
151 int amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
152 int amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
153 int amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
154 int amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id);
155 
156 int amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode,
157 			int init_xcps, struct amdgpu_xcp_mgr_funcs *xcp_funcs);
158 int amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode);
159 int amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags);
160 int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode);
161 int amdgpu_xcp_restore_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr);
162 int amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr,
163 			     enum AMDGPU_XCP_IP_BLOCK ip, int instance);
164 
165 int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,
166 				enum AMDGPU_XCP_IP_BLOCK ip,
167 				uint32_t *inst_mask);
168 
169 int amdgpu_xcp_dev_register(struct amdgpu_device *adev,
170 				const struct pci_device_id *ent);
171 void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev);
172 int amdgpu_xcp_open_device(struct amdgpu_device *adev,
173 			   struct amdgpu_fpriv *fpriv,
174 			   struct drm_file *file_priv);
175 void amdgpu_xcp_release_sched(struct amdgpu_device *adev,
176 			      struct amdgpu_ctx_entity *entity);
177 int amdgpu_xcp_select_scheds(struct amdgpu_device *adev,
178 			     u32 hw_ip, u32 hw_prio,
179 			     struct amdgpu_fpriv *fpriv,
180 			     unsigned int *num_scheds,
181 			     struct drm_gpu_scheduler ***scheds);
182 void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr);
183 int amdgpu_xcp_update_partition_sched_list(struct amdgpu_device *adev);
184 int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags);
185 int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags);
186 void amdgpu_xcp_sysfs_init(struct amdgpu_device *adev);
187 void amdgpu_xcp_sysfs_fini(struct amdgpu_device *adev);
188 
amdgpu_xcp_get_num_xcp(struct amdgpu_xcp_mgr * xcp_mgr)189 static inline int amdgpu_xcp_get_num_xcp(struct amdgpu_xcp_mgr *xcp_mgr)
190 {
191 	if (!xcp_mgr)
192 		return 1;
193 	else
194 		return xcp_mgr->num_xcps;
195 }
196 
197 static inline struct amdgpu_xcp *
amdgpu_get_next_xcp(struct amdgpu_xcp_mgr * xcp_mgr,int * from)198 amdgpu_get_next_xcp(struct amdgpu_xcp_mgr *xcp_mgr, int *from)
199 {
200 	if (!xcp_mgr)
201 		return NULL;
202 
203 	while (*from < MAX_XCP) {
204 		if (xcp_mgr->xcp[*from].valid)
205 			return &xcp_mgr->xcp[*from];
206 		++(*from);
207 	}
208 
209 	return NULL;
210 }
211 
212 #define for_each_xcp(xcp_mgr, xcp, i)                            \
213 	for (i = 0, xcp = amdgpu_get_next_xcp(xcp_mgr, &i); xcp; \
214 	     ++i, xcp = amdgpu_get_next_xcp(xcp_mgr, &i))
215 
216 #endif
217