162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright 2022 Advanced Micro Devices, Inc. 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation 762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 1262306a36Sopenharmony_ci * all copies or substantial portions of the Software. 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#ifndef AMDGPU_XCP_H 2562306a36Sopenharmony_ci#define AMDGPU_XCP_H 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include <linux/pci.h> 2862306a36Sopenharmony_ci#include <linux/xarray.h> 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#include "amdgpu_ctx.h" 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#define MAX_XCP 8 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#define AMDGPU_XCP_MODE_NONE -1 3562306a36Sopenharmony_ci#define AMDGPU_XCP_MODE_TRANS -2 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#define AMDGPU_XCP_FL_NONE 0 3862306a36Sopenharmony_ci#define AMDGPU_XCP_FL_LOCKED (1 << 0) 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#define AMDGPU_XCP_NO_PARTITION (~0) 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistruct amdgpu_fpriv; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_cienum AMDGPU_XCP_IP_BLOCK { 4562306a36Sopenharmony_ci AMDGPU_XCP_GFXHUB, 4662306a36Sopenharmony_ci AMDGPU_XCP_GFX, 4762306a36Sopenharmony_ci AMDGPU_XCP_SDMA, 4862306a36Sopenharmony_ci AMDGPU_XCP_VCN, 4962306a36Sopenharmony_ci AMDGPU_XCP_MAX_BLOCKS 5062306a36Sopenharmony_ci}; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_cienum AMDGPU_XCP_STATE { 5362306a36Sopenharmony_ci AMDGPU_XCP_PREPARE_SUSPEND, 5462306a36Sopenharmony_ci AMDGPU_XCP_SUSPEND, 5562306a36Sopenharmony_ci AMDGPU_XCP_PREPARE_RESUME, 5662306a36Sopenharmony_ci AMDGPU_XCP_RESUME, 5762306a36Sopenharmony_ci}; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistruct amdgpu_xcp_ip_funcs { 6062306a36Sopenharmony_ci int (*prepare_suspend)(void *handle, uint32_t inst_mask); 6162306a36Sopenharmony_ci int (*suspend)(void *handle, uint32_t inst_mask); 6262306a36Sopenharmony_ci int (*prepare_resume)(void *handle, uint32_t inst_mask); 6362306a36Sopenharmony_ci int (*resume)(void *handle, uint32_t inst_mask); 6462306a36Sopenharmony_ci}; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistruct amdgpu_xcp_ip { 6762306a36Sopenharmony_ci struct amdgpu_xcp_ip_funcs *ip_funcs; 6862306a36Sopenharmony_ci uint32_t inst_mask; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci enum AMDGPU_XCP_IP_BLOCK ip_id; 7162306a36Sopenharmony_ci bool valid; 7262306a36Sopenharmony_ci}; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_cistruct amdgpu_xcp { 7562306a36Sopenharmony_ci struct amdgpu_xcp_ip ip[AMDGPU_XCP_MAX_BLOCKS]; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci uint8_t id; 7862306a36Sopenharmony_ci uint8_t mem_id; 7962306a36Sopenharmony_ci bool valid; 8062306a36Sopenharmony_ci atomic_t ref_cnt; 8162306a36Sopenharmony_ci struct drm_device *ddev; 8262306a36Sopenharmony_ci struct drm_device *rdev; 8362306a36Sopenharmony_ci struct drm_device *pdev; 8462306a36Sopenharmony_ci struct drm_driver *driver; 8562306a36Sopenharmony_ci struct drm_vma_offset_manager *vma_offset_manager; 8662306a36Sopenharmony_ci struct amdgpu_sched gpu_sched[AMDGPU_HW_IP_NUM][AMDGPU_RING_PRIO_MAX]; 8762306a36Sopenharmony_ci}; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cistruct amdgpu_xcp_mgr { 9062306a36Sopenharmony_ci struct amdgpu_device *adev; 9162306a36Sopenharmony_ci struct mutex xcp_lock; 9262306a36Sopenharmony_ci struct amdgpu_xcp_mgr_funcs *funcs; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci struct amdgpu_xcp xcp[MAX_XCP]; 9562306a36Sopenharmony_ci uint8_t num_xcps; 9662306a36Sopenharmony_ci int8_t mode; 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci /* Used to determine KFD memory size limits per XCP */ 9962306a36Sopenharmony_ci unsigned int num_xcp_per_mem_partition; 10062306a36Sopenharmony_ci}; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_cistruct amdgpu_xcp_mgr_funcs { 10362306a36Sopenharmony_ci int (*switch_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr, int mode, 10462306a36Sopenharmony_ci int *num_xcps); 10562306a36Sopenharmony_ci int (*query_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr); 10662306a36Sopenharmony_ci int (*get_ip_details)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id, 10762306a36Sopenharmony_ci enum AMDGPU_XCP_IP_BLOCK ip_id, 10862306a36Sopenharmony_ci struct amdgpu_xcp_ip *ip); 10962306a36Sopenharmony_ci int (*get_xcp_mem_id)(struct amdgpu_xcp_mgr *xcp_mgr, 11062306a36Sopenharmony_ci struct amdgpu_xcp *xcp, uint8_t *mem_id); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci int (*prepare_suspend)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 11362306a36Sopenharmony_ci int (*suspend)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 11462306a36Sopenharmony_ci int (*prepare_resume)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 11562306a36Sopenharmony_ci int (*resume)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 11662306a36Sopenharmony_ci int (*select_scheds)(struct amdgpu_device *adev, 11762306a36Sopenharmony_ci u32 hw_ip, u32 hw_prio, struct amdgpu_fpriv *fpriv, 11862306a36Sopenharmony_ci unsigned int *num_scheds, struct drm_gpu_scheduler ***scheds); 11962306a36Sopenharmony_ci int (*update_partition_sched_list)(struct amdgpu_device *adev); 12062306a36Sopenharmony_ci}; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciint amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 12362306a36Sopenharmony_ciint amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 12462306a36Sopenharmony_ciint amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 12562306a36Sopenharmony_ciint amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id); 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ciint amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode, 12862306a36Sopenharmony_ci int init_xcps, struct amdgpu_xcp_mgr_funcs *xcp_funcs); 12962306a36Sopenharmony_ciint amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode); 13062306a36Sopenharmony_ciint amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags); 13162306a36Sopenharmony_ciint amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode); 13262306a36Sopenharmony_ciint amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr, 13362306a36Sopenharmony_ci enum AMDGPU_XCP_IP_BLOCK ip, int instance); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciint amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp, 13662306a36Sopenharmony_ci enum AMDGPU_XCP_IP_BLOCK ip, 13762306a36Sopenharmony_ci uint32_t *inst_mask); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ciint amdgpu_xcp_dev_register(struct amdgpu_device *adev, 14062306a36Sopenharmony_ci const struct pci_device_id *ent); 14162306a36Sopenharmony_civoid amdgpu_xcp_dev_unplug(struct amdgpu_device *adev); 14262306a36Sopenharmony_ciint amdgpu_xcp_open_device(struct amdgpu_device *adev, 14362306a36Sopenharmony_ci struct amdgpu_fpriv *fpriv, 14462306a36Sopenharmony_ci struct drm_file *file_priv); 14562306a36Sopenharmony_civoid amdgpu_xcp_release_sched(struct amdgpu_device *adev, 14662306a36Sopenharmony_ci struct amdgpu_ctx_entity *entity); 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci#define amdgpu_xcp_select_scheds(adev, e, c, d, x, y) \ 14962306a36Sopenharmony_ci ((adev)->xcp_mgr && (adev)->xcp_mgr->funcs && \ 15062306a36Sopenharmony_ci (adev)->xcp_mgr->funcs->select_scheds ? \ 15162306a36Sopenharmony_ci (adev)->xcp_mgr->funcs->select_scheds((adev), (e), (c), (d), (x), (y)) : -ENOENT) 15262306a36Sopenharmony_ci#define amdgpu_xcp_update_partition_sched_list(adev) \ 15362306a36Sopenharmony_ci ((adev)->xcp_mgr && (adev)->xcp_mgr->funcs && \ 15462306a36Sopenharmony_ci (adev)->xcp_mgr->funcs->update_partition_sched_list ? \ 15562306a36Sopenharmony_ci (adev)->xcp_mgr->funcs->update_partition_sched_list(adev) : 0) 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_cistatic inline int amdgpu_xcp_get_num_xcp(struct amdgpu_xcp_mgr *xcp_mgr) 15862306a36Sopenharmony_ci{ 15962306a36Sopenharmony_ci if (!xcp_mgr) 16062306a36Sopenharmony_ci return 1; 16162306a36Sopenharmony_ci else 16262306a36Sopenharmony_ci return xcp_mgr->num_xcps; 16362306a36Sopenharmony_ci} 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_cistatic inline struct amdgpu_xcp * 16662306a36Sopenharmony_ciamdgpu_get_next_xcp(struct amdgpu_xcp_mgr *xcp_mgr, int *from) 16762306a36Sopenharmony_ci{ 16862306a36Sopenharmony_ci if (!xcp_mgr) 16962306a36Sopenharmony_ci return NULL; 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci while (*from < MAX_XCP) { 17262306a36Sopenharmony_ci if (xcp_mgr->xcp[*from].valid) 17362306a36Sopenharmony_ci return &xcp_mgr->xcp[*from]; 17462306a36Sopenharmony_ci ++(*from); 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci return NULL; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci#define for_each_xcp(xcp_mgr, xcp, i) \ 18162306a36Sopenharmony_ci for (i = 0, xcp = amdgpu_get_next_xcp(xcp_mgr, &i); xcp; \ 18262306a36Sopenharmony_ci xcp = amdgpu_get_next_xcp(xcp_mgr, &i)) 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci#endif 185