drm/amdgpu: Correct get_xcp_mem_id calculation
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / amd / amdgpu / aqua_vanjaram_reg_init.c
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 #include "amdgpu.h"
24 #include "soc15.h"
25
26 #include "soc15_common.h"
27 #include "amdgpu_xcp.h"
28 #include "gfx_v9_4_3.h"
29 #include "gfxhub_v1_2.h"
30 #include "sdma_v4_4_2.h"
31
32 #define XCP_INST_MASK(num_inst, xcp_id)                                        \
33         (num_inst ? GENMASK(num_inst - 1, 0) << (xcp_id * num_inst) : 0)
34
35 #define AMDGPU_XCP_OPS_KFD      (1 << 0)
36
37 void aqua_vanjaram_doorbell_index_init(struct amdgpu_device *adev)
38 {
39         int i;
40
41         adev->doorbell_index.kiq = AMDGPU_DOORBELL_LAYOUT1_KIQ_START;
42
43         adev->doorbell_index.mec_ring0 = AMDGPU_DOORBELL_LAYOUT1_MEC_RING_START;
44
45         adev->doorbell_index.userqueue_start = AMDGPU_DOORBELL_LAYOUT1_USERQUEUE_START;
46         adev->doorbell_index.userqueue_end = AMDGPU_DOORBELL_LAYOUT1_USERQUEUE_END;
47         adev->doorbell_index.xcc_doorbell_range = AMDGPU_DOORBELL_LAYOUT1_XCC_RANGE;
48
49         adev->doorbell_index.sdma_doorbell_range = 20;
50         for (i = 0; i < adev->sdma.num_instances; i++)
51                 adev->doorbell_index.sdma_engine[i] =
52                         AMDGPU_DOORBELL_LAYOUT1_sDMA_ENGINE_START +
53                         i * (adev->doorbell_index.sdma_doorbell_range >> 1);
54
55         adev->doorbell_index.ih = AMDGPU_DOORBELL_LAYOUT1_IH;
56         adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_DOORBELL_LAYOUT1_VCN_START;
57
58         adev->doorbell_index.first_non_cp = AMDGPU_DOORBELL_LAYOUT1_FIRST_NON_CP;
59         adev->doorbell_index.last_non_cp = AMDGPU_DOORBELL_LAYOUT1_LAST_NON_CP;
60
61         adev->doorbell_index.max_assignment = AMDGPU_DOORBELL_LAYOUT1_MAX_ASSIGNMENT << 1;
62 }
63
64 static void aqua_vanjaram_set_xcp_id(struct amdgpu_device *adev,
65                              uint32_t inst_idx, struct amdgpu_ring *ring)
66 {
67         int xcp_id;
68         enum AMDGPU_XCP_IP_BLOCK ip_blk;
69         uint32_t inst_mask;
70
71         ring->xcp_id = ~0;
72         if (adev->xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
73                 return;
74
75         inst_mask = 1 << inst_idx;
76
77         switch (ring->funcs->type) {
78         case AMDGPU_HW_IP_GFX:
79         case AMDGPU_RING_TYPE_COMPUTE:
80         case AMDGPU_RING_TYPE_KIQ:
81                 ip_blk = AMDGPU_XCP_GFX;
82                 break;
83         case AMDGPU_RING_TYPE_SDMA:
84                 ip_blk = AMDGPU_XCP_SDMA;
85                 break;
86         case AMDGPU_RING_TYPE_VCN_ENC:
87         case AMDGPU_RING_TYPE_VCN_JPEG:
88                 ip_blk = AMDGPU_XCP_VCN;
89                 if (adev->xcp_mgr->mode == AMDGPU_CPX_PARTITION_MODE)
90                         inst_mask = 1 << (inst_idx * 2);
91                 break;
92         default:
93                 DRM_ERROR("Not support ring type %d!", ring->funcs->type);
94                 return;
95         }
96
97         for (xcp_id = 0; xcp_id < adev->xcp_mgr->num_xcps; xcp_id++) {
98                 if (adev->xcp_mgr->xcp[xcp_id].ip[ip_blk].inst_mask & inst_mask) {
99                         ring->xcp_id = xcp_id;
100                         break;
101                 }
102         }
103 }
104
105 static void aqua_vanjaram_xcp_gpu_sched_update(
106                 struct amdgpu_device *adev,
107                 struct amdgpu_ring *ring,
108                 unsigned int sel_xcp_id)
109 {
110         unsigned int *num_gpu_sched;
111
112         num_gpu_sched = &adev->xcp_mgr->xcp[sel_xcp_id]
113                         .gpu_sched[ring->funcs->type][ring->hw_prio].num_scheds;
114         adev->xcp_mgr->xcp[sel_xcp_id].gpu_sched[ring->funcs->type][ring->hw_prio]
115                         .sched[(*num_gpu_sched)++] = &ring->sched;
116         DRM_DEBUG("%s :[%d] gpu_sched[%d][%d] = %d", ring->name,
117                         sel_xcp_id, ring->funcs->type,
118                         ring->hw_prio, *num_gpu_sched);
119 }
120
121 static int aqua_vanjaram_xcp_sched_list_update(
122                 struct amdgpu_device *adev)
123 {
124         struct amdgpu_ring *ring;
125         int i;
126
127         for (i = 0; i < MAX_XCP; i++) {
128                 atomic_set(&adev->xcp_mgr->xcp[i].ref_cnt, 0);
129                 memset(adev->xcp_mgr->xcp[i].gpu_sched, 0, sizeof(adev->xcp_mgr->xcp->gpu_sched));
130         }
131
132         if (adev->xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
133                 return 0;
134
135         for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
136                 ring = adev->rings[i];
137                 if (!ring || !ring->sched.ready)
138                         continue;
139
140                 aqua_vanjaram_xcp_gpu_sched_update(adev, ring, ring->xcp_id);
141
142                 /* VCN is shared by two partitions under CPX MODE */
143                 if ((ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC ||
144                         ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG) &&
145                         adev->xcp_mgr->mode == AMDGPU_CPX_PARTITION_MODE)
146                         aqua_vanjaram_xcp_gpu_sched_update(adev, ring, ring->xcp_id + 1);
147         }
148
149         return 0;
150 }
151
152 static int aqua_vanjaram_update_partition_sched_list(struct amdgpu_device *adev)
153 {
154         int i;
155
156         for (i = 0; i < adev->num_rings; i++) {
157                 struct amdgpu_ring *ring = adev->rings[i];
158
159                 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE ||
160                         ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
161                         aqua_vanjaram_set_xcp_id(adev, ring->xcc_id, ring);
162                 else
163                         aqua_vanjaram_set_xcp_id(adev, ring->me, ring);
164         }
165
166         return aqua_vanjaram_xcp_sched_list_update(adev);
167 }
168
169 int aqua_vanjaram_select_scheds(
170                 struct amdgpu_device *adev,
171                 u32 hw_ip,
172                 u32 hw_prio,
173                 struct amdgpu_fpriv *fpriv,
174                 unsigned int *num_scheds,
175                 struct drm_gpu_scheduler ***scheds)
176 {
177         u32 sel_xcp_id;
178         int i;
179
180         if (fpriv->xcp_id == ~0) {
181                 u32 least_ref_cnt = ~0;
182
183                 fpriv->xcp_id = 0;
184                 for (i = 0; i < adev->xcp_mgr->num_xcps; i++) {
185                         u32 total_ref_cnt;
186
187                         total_ref_cnt = atomic_read(&adev->xcp_mgr->xcp[i].ref_cnt);
188                         if (total_ref_cnt < least_ref_cnt) {
189                                 fpriv->xcp_id = i;
190                                 least_ref_cnt = total_ref_cnt;
191                         }
192                 }
193         }
194         sel_xcp_id = fpriv->xcp_id;
195
196         if (adev->xcp_mgr->xcp[sel_xcp_id].gpu_sched[hw_ip][hw_prio].num_scheds) {
197                 *num_scheds = adev->xcp_mgr->xcp[fpriv->xcp_id].gpu_sched[hw_ip][hw_prio].num_scheds;
198                 *scheds = adev->xcp_mgr->xcp[fpriv->xcp_id].gpu_sched[hw_ip][hw_prio].sched;
199                 atomic_inc(&adev->xcp_mgr->xcp[sel_xcp_id].ref_cnt);
200                 DRM_DEBUG("Selected partition #%d", sel_xcp_id);
201         } else {
202                 DRM_ERROR("Failed to schedule partition #%d.", sel_xcp_id);
203                 return -ENOENT;
204         }
205
206         return 0;
207 }
208
209 static int8_t aqua_vanjaram_logical_to_dev_inst(struct amdgpu_device *adev,
210                                          enum amd_hw_ip_block_type block,
211                                          int8_t inst)
212 {
213         int8_t dev_inst;
214
215         switch (block) {
216         case GC_HWIP:
217         case SDMA0_HWIP:
218         /* Both JPEG and VCN as JPEG is only alias of VCN */
219         case VCN_HWIP:
220                 dev_inst = adev->ip_map.dev_inst[block][inst];
221                 break;
222         default:
223                 /* For rest of the IPs, no look up required.
224                  * Assume 'logical instance == physical instance' for all configs. */
225                 dev_inst = inst;
226                 break;
227         }
228
229         return dev_inst;
230 }
231
232 static void aqua_vanjaram_populate_ip_map(struct amdgpu_device *adev,
233                                           enum amd_hw_ip_block_type ip_block,
234                                           uint32_t inst_mask)
235 {
236         int l = 0, i;
237
238         while (inst_mask) {
239                 i = ffs(inst_mask) - 1;
240                 adev->ip_map.dev_inst[ip_block][l++] = i;
241                 inst_mask &= ~(1 << i);
242         }
243         for (; l < HWIP_MAX_INSTANCE; l++)
244                 adev->ip_map.dev_inst[ip_block][l] = -1;
245 }
246
247 void aqua_vanjaram_ip_map_init(struct amdgpu_device *adev)
248 {
249         u32 ip_map[][2] = {
250                 { GC_HWIP, adev->gfx.xcc_mask },
251                 { SDMA0_HWIP, adev->sdma.sdma_mask },
252                 { VCN_HWIP, adev->vcn.inst_mask },
253         };
254         int i;
255
256         for (i = 0; i < ARRAY_SIZE(ip_map); ++i)
257                 aqua_vanjaram_populate_ip_map(adev, ip_map[i][0], ip_map[i][1]);
258
259         adev->ip_map.logical_to_dev_inst = aqua_vanjaram_logical_to_dev_inst;
260 }
261
262 /* Fixed pattern for smn addressing on different AIDs:
263  *   bit[34]: indicate cross AID access
264  *   bit[33:32]: indicate target AID id
265  * AID id range is 0 ~ 3 as maximum AID number is 4.
266  */
267 u64 aqua_vanjaram_encode_ext_smn_addressing(int ext_id)
268 {
269         u64 ext_offset;
270
271         /* local routing and bit[34:32] will be zeros */
272         if (ext_id == 0)
273                 return 0;
274
275         /* Initiated from host, accessing to all non-zero aids are cross traffic */
276         ext_offset = ((u64)(ext_id & 0x3) << 32) | (1ULL << 34);
277
278         return ext_offset;
279 }
280
281 static int aqua_vanjaram_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr)
282 {
283         enum amdgpu_gfx_partition mode = AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE;
284         struct amdgpu_device *adev = xcp_mgr->adev;
285
286         if (adev->nbio.funcs->get_compute_partition_mode)
287                 mode = adev->nbio.funcs->get_compute_partition_mode(adev);
288
289         return mode;
290 }
291
292 int __aqua_vanjaram_get_xcc_per_xcp(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
293 {
294         int num_xcc, num_xcc_per_xcp = 0;
295
296         num_xcc = NUM_XCC(xcp_mgr->adev->gfx.xcc_mask);
297
298         switch (mode) {
299         case AMDGPU_SPX_PARTITION_MODE:
300                 num_xcc_per_xcp = num_xcc;
301                 break;
302         case AMDGPU_DPX_PARTITION_MODE:
303                 num_xcc_per_xcp = num_xcc / 2;
304                 break;
305         case AMDGPU_TPX_PARTITION_MODE:
306                 num_xcc_per_xcp = num_xcc / 3;
307                 break;
308         case AMDGPU_QPX_PARTITION_MODE:
309                 num_xcc_per_xcp = num_xcc / 4;
310                 break;
311         case AMDGPU_CPX_PARTITION_MODE:
312                 num_xcc_per_xcp = 1;
313                 break;
314         }
315
316         return num_xcc_per_xcp;
317 }
318
319 int __aqua_vanjaram_get_xcp_ip_info(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
320                                     enum AMDGPU_XCP_IP_BLOCK ip_id,
321                                     struct amdgpu_xcp_ip *ip)
322 {
323         struct amdgpu_device *adev = xcp_mgr->adev;
324         int num_xcc_xcp, num_sdma_xcp, num_vcn_xcp;
325         int num_sdma, num_vcn;
326
327         num_sdma = adev->sdma.num_instances;
328         num_vcn = adev->vcn.num_vcn_inst;
329
330         switch (xcp_mgr->mode) {
331         case AMDGPU_SPX_PARTITION_MODE:
332                 num_sdma_xcp = num_sdma;
333                 num_vcn_xcp = num_vcn;
334                 break;
335         case AMDGPU_DPX_PARTITION_MODE:
336                 num_sdma_xcp = num_sdma / 2;
337                 num_vcn_xcp = num_vcn / 2;
338                 break;
339         case AMDGPU_TPX_PARTITION_MODE:
340                 num_sdma_xcp = num_sdma / 3;
341                 num_vcn_xcp = num_vcn / 3;
342                 break;
343         case AMDGPU_QPX_PARTITION_MODE:
344                 num_sdma_xcp = num_sdma / 4;
345                 num_vcn_xcp = num_vcn / 4;
346                 break;
347         case AMDGPU_CPX_PARTITION_MODE:
348                 num_sdma_xcp = 2;
349                 num_vcn_xcp = num_vcn ? 1 : 0;
350                 break;
351         default:
352                 return -EINVAL;
353         }
354
355         num_xcc_xcp = adev->gfx.num_xcc_per_xcp;
356
357         switch (ip_id) {
358         case AMDGPU_XCP_GFXHUB:
359                 ip->inst_mask = XCP_INST_MASK(num_xcc_xcp, xcp_id);
360                 ip->ip_funcs = &gfxhub_v1_2_xcp_funcs;
361                 break;
362         case AMDGPU_XCP_GFX:
363                 ip->inst_mask = XCP_INST_MASK(num_xcc_xcp, xcp_id);
364                 ip->ip_funcs = &gfx_v9_4_3_xcp_funcs;
365                 break;
366         case AMDGPU_XCP_SDMA:
367                 ip->inst_mask = XCP_INST_MASK(num_sdma_xcp, xcp_id);
368                 ip->ip_funcs = &sdma_v4_4_2_xcp_funcs;
369                 break;
370         case AMDGPU_XCP_VCN:
371                 ip->inst_mask = XCP_INST_MASK(num_vcn_xcp, xcp_id);
372                 /* TODO : Assign IP funcs */
373                 break;
374         default:
375                 return -EINVAL;
376         }
377
378         ip->ip_id = ip_id;
379
380         return 0;
381 }
382
383 static enum amdgpu_gfx_partition
384 __aqua_vanjaram_get_auto_mode(struct amdgpu_xcp_mgr *xcp_mgr)
385 {
386         struct amdgpu_device *adev = xcp_mgr->adev;
387         int num_xcc;
388
389         num_xcc = NUM_XCC(xcp_mgr->adev->gfx.xcc_mask);
390
391         if (adev->gmc.num_mem_partitions == 1)
392                 return AMDGPU_SPX_PARTITION_MODE;
393
394         if (adev->gmc.num_mem_partitions == num_xcc)
395                 return AMDGPU_CPX_PARTITION_MODE;
396
397         if (adev->gmc.num_mem_partitions == num_xcc / 2)
398                 return (adev->flags & AMD_IS_APU) ? AMDGPU_TPX_PARTITION_MODE :
399                                                     AMDGPU_QPX_PARTITION_MODE;
400
401         if (adev->gmc.num_mem_partitions == 2 && !(adev->flags & AMD_IS_APU))
402                 return AMDGPU_DPX_PARTITION_MODE;
403
404         return AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE;
405 }
406
407 static bool __aqua_vanjaram_is_valid_mode(struct amdgpu_xcp_mgr *xcp_mgr,
408                                           enum amdgpu_gfx_partition mode)
409 {
410         struct amdgpu_device *adev = xcp_mgr->adev;
411         int num_xcc, num_xccs_per_xcp;
412
413         num_xcc = NUM_XCC(adev->gfx.xcc_mask);
414         switch (mode) {
415         case AMDGPU_SPX_PARTITION_MODE:
416                 return adev->gmc.num_mem_partitions == 1 && num_xcc > 0;
417         case AMDGPU_DPX_PARTITION_MODE:
418                 return adev->gmc.num_mem_partitions != 8 && (num_xcc % 4) == 0;
419         case AMDGPU_TPX_PARTITION_MODE:
420                 return (adev->gmc.num_mem_partitions == 1 ||
421                         adev->gmc.num_mem_partitions == 3) &&
422                        ((num_xcc % 3) == 0);
423         case AMDGPU_QPX_PARTITION_MODE:
424                 num_xccs_per_xcp = num_xcc / 4;
425                 return (adev->gmc.num_mem_partitions == 1 ||
426                         adev->gmc.num_mem_partitions == 4) &&
427                        (num_xccs_per_xcp >= 2);
428         case AMDGPU_CPX_PARTITION_MODE:
429                 return ((num_xcc > 1) &&
430                        (adev->gmc.num_mem_partitions == 1 || adev->gmc.num_mem_partitions == 4) &&
431                        (num_xcc % adev->gmc.num_mem_partitions) == 0);
432         default:
433                 return false;
434         }
435
436         return false;
437 }
438
439 static int __aqua_vanjaram_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
440 {
441         /* TODO:
442          * Stop user queues and threads, and make sure GPU is empty of work.
443          */
444
445         if (flags & AMDGPU_XCP_OPS_KFD)
446                 amdgpu_amdkfd_device_fini_sw(xcp_mgr->adev);
447
448         return 0;
449 }
450
451 static int __aqua_vanjaram_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
452 {
453         int ret = 0;
454
455         if (flags & AMDGPU_XCP_OPS_KFD) {
456                 amdgpu_amdkfd_device_probe(xcp_mgr->adev);
457                 amdgpu_amdkfd_device_init(xcp_mgr->adev);
458                 /* If KFD init failed, return failure */
459                 if (!xcp_mgr->adev->kfd.init_complete)
460                         ret = -EIO;
461         }
462
463         return ret;
464 }
465
466 static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
467                                                int mode, int *num_xcps)
468 {
469         int num_xcc_per_xcp, num_xcc, ret;
470         struct amdgpu_device *adev;
471         u32 flags = 0;
472
473         adev = xcp_mgr->adev;
474         num_xcc = NUM_XCC(adev->gfx.xcc_mask);
475
476         if (mode == AMDGPU_AUTO_COMPUTE_PARTITION_MODE) {
477                 mode = __aqua_vanjaram_get_auto_mode(xcp_mgr);
478         } else if (!__aqua_vanjaram_is_valid_mode(xcp_mgr, mode)) {
479                 dev_err(adev->dev,
480                         "Invalid compute partition mode requested, requested: %s, available memory partitions: %d",
481                         amdgpu_gfx_compute_mode_desc(mode), adev->gmc.num_mem_partitions);
482                 return -EINVAL;
483         }
484
485         if (adev->kfd.init_complete)
486                 flags |= AMDGPU_XCP_OPS_KFD;
487
488         if (flags & AMDGPU_XCP_OPS_KFD) {
489                 ret = amdgpu_amdkfd_check_and_lock_kfd(adev);
490                 if (ret)
491                         goto out;
492         }
493
494         ret = __aqua_vanjaram_pre_partition_switch(xcp_mgr, flags);
495         if (ret)
496                 goto unlock;
497
498         num_xcc_per_xcp = __aqua_vanjaram_get_xcc_per_xcp(xcp_mgr, mode);
499         if (adev->gfx.funcs->switch_partition_mode)
500                 adev->gfx.funcs->switch_partition_mode(xcp_mgr->adev,
501                                                        num_xcc_per_xcp);
502
503         if (adev->nbio.funcs->set_compute_partition_mode)
504                 adev->nbio.funcs->set_compute_partition_mode(adev, mode);
505
506         /* Init info about new xcps */
507         *num_xcps = num_xcc / num_xcc_per_xcp;
508         amdgpu_xcp_init(xcp_mgr, *num_xcps, mode);
509
510         ret = __aqua_vanjaram_post_partition_switch(xcp_mgr, flags);
511 unlock:
512         if (flags & AMDGPU_XCP_OPS_KFD)
513                 amdgpu_amdkfd_unlock_kfd(adev);
514 out:
515         return ret;
516 }
517
518 static int __aqua_vanjaram_get_xcp_mem_id(struct amdgpu_device *adev,
519                                           int xcc_id, uint8_t *mem_id)
520 {
521         /* memory/spatial modes validation check is already done */
522         *mem_id = xcc_id / adev->gfx.num_xcc_per_xcp;
523         *mem_id /= adev->xcp_mgr->num_xcp_per_mem_partition;
524
525         return 0;
526 }
527
528 static int aqua_vanjaram_get_xcp_mem_id(struct amdgpu_xcp_mgr *xcp_mgr,
529                                         struct amdgpu_xcp *xcp, uint8_t *mem_id)
530 {
531         struct amdgpu_numa_info numa_info;
532         struct amdgpu_device *adev;
533         uint32_t xcc_mask;
534         int r, i, xcc_id;
535
536         adev = xcp_mgr->adev;
537         /* TODO: BIOS is not returning the right info now
538          * Check on this later
539          */
540         /*
541         if (adev->gmc.gmc_funcs->query_mem_partition_mode)
542                 mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
543         */
544         if (adev->gmc.num_mem_partitions == 1) {
545                 /* Only one range */
546                 *mem_id = 0;
547                 return 0;
548         }
549
550         r = amdgpu_xcp_get_inst_details(xcp, AMDGPU_XCP_GFX, &xcc_mask);
551         if (r || !xcc_mask)
552                 return -EINVAL;
553
554         xcc_id = ffs(xcc_mask) - 1;
555         if (!adev->gmc.is_app_apu)
556                 return __aqua_vanjaram_get_xcp_mem_id(adev, xcc_id, mem_id);
557
558         r = amdgpu_acpi_get_mem_info(adev, xcc_id, &numa_info);
559
560         if (r)
561                 return r;
562
563         r = -EINVAL;
564         for (i = 0; i < adev->gmc.num_mem_partitions; ++i) {
565                 if (adev->gmc.mem_partitions[i].numa.node == numa_info.nid) {
566                         *mem_id = i;
567                         r = 0;
568                         break;
569                 }
570         }
571
572         return r;
573 }
574
575 int aqua_vanjaram_get_xcp_ip_details(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
576                                      enum AMDGPU_XCP_IP_BLOCK ip_id,
577                                      struct amdgpu_xcp_ip *ip)
578 {
579         if (!ip)
580                 return -EINVAL;
581
582         return __aqua_vanjaram_get_xcp_ip_info(xcp_mgr, xcp_id, ip_id, ip);
583 }
584
585 struct amdgpu_xcp_mgr_funcs aqua_vanjaram_xcp_funcs = {
586         .switch_partition_mode = &aqua_vanjaram_switch_partition_mode,
587         .query_partition_mode = &aqua_vanjaram_query_partition_mode,
588         .get_ip_details = &aqua_vanjaram_get_xcp_ip_details,
589         .get_xcp_mem_id = &aqua_vanjaram_get_xcp_mem_id,
590         .select_scheds = &aqua_vanjaram_select_scheds,
591         .update_partition_sched_list = &aqua_vanjaram_update_partition_sched_list
592 };
593
594 static int aqua_vanjaram_xcp_mgr_init(struct amdgpu_device *adev)
595 {
596         int ret;
597
598         ret = amdgpu_xcp_mgr_init(adev, AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE, 1,
599                                   &aqua_vanjaram_xcp_funcs);
600         if (ret)
601                 return ret;
602
603         /* TODO: Default memory node affinity init */
604
605         return ret;
606 }
607
608 int aqua_vanjaram_init_soc_config(struct amdgpu_device *adev)
609 {
610         u32 mask, inst_mask = adev->sdma.sdma_mask;
611         int ret, i;
612
613         /* generally 1 AID supports 4 instances */
614         adev->sdma.num_inst_per_aid = 4;
615         adev->sdma.num_instances = NUM_SDMA(adev->sdma.sdma_mask);
616
617         adev->aid_mask = i = 1;
618         inst_mask >>= adev->sdma.num_inst_per_aid;
619
620         for (mask = (1 << adev->sdma.num_inst_per_aid) - 1; inst_mask;
621              inst_mask >>= adev->sdma.num_inst_per_aid, ++i) {
622                 if ((inst_mask & mask) == mask)
623                         adev->aid_mask |= (1 << i);
624         }
625
626         /* Harvest config is not used for aqua vanjaram. VCN and JPEGs will be
627          * addressed based on logical instance ids.
628          */
629         adev->vcn.harvest_config = 0;
630         adev->vcn.num_inst_per_aid = 1;
631         adev->vcn.num_vcn_inst = hweight32(adev->vcn.inst_mask);
632         adev->jpeg.harvest_config = 0;
633         adev->jpeg.num_inst_per_aid = 1;
634         adev->jpeg.num_jpeg_inst = hweight32(adev->jpeg.inst_mask);
635
636         ret = aqua_vanjaram_xcp_mgr_init(adev);
637         if (ret)
638                 return ret;
639
640         aqua_vanjaram_ip_map_init(adev);
641
642         return 0;
643 }