Add reserved regions and helper functions to memory manager.

Signed-off-by: Lijo Lazar <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 58 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 31 +++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 714fd8d12ca5..7f04e53983b5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -1677,6 +1677,64 @@ static struct ttm_device_funcs amdgpu_bo_driver = {
        .access_memory = &amdgpu_ttm_access_memory,
 };
 
+void amdgpu_ttm_init_vram_resv(struct amdgpu_device *adev,
+                               enum amdgpu_resv_region_id id,
+                               uint64_t offset, uint64_t size,
+                               bool needs_cpu_map)
+{
+       struct amdgpu_vram_resv *resv;
+
+       if (id >= AMDGPU_RESV_MAX)
+               return;
+
+       resv = &adev->mman.resv_region[id];
+       resv->offset = offset;
+       resv->size = size;
+       resv->needs_cpu_map = needs_cpu_map;
+}
+
+int amdgpu_ttm_reserve_vram(struct amdgpu_device *adev,
+                           enum amdgpu_resv_region_id id)
+{
+       struct amdgpu_vram_resv *resv;
+       int ret;
+
+       if (id >= AMDGPU_RESV_MAX)
+               return -EINVAL;
+
+       resv = &adev->mman.resv_region[id];
+       if (!resv->size)
+               return 0;
+
+       ret = amdgpu_bo_create_kernel_at(adev, resv->offset, resv->size,
+                                        &resv->bo,
+                                        resv->needs_cpu_map ? &resv->cpu_addr 
: NULL);
+       if (ret) {
+               dev_dbg(adev->dev, "reserve vram failed: id=%d offset=0x%llx 
size=0x%llx ret=%d\n",
+                       id, resv->offset, resv->size, ret);
+               memset(resv, 0, sizeof(*resv));
+       }
+
+       return ret;
+}
+
+void amdgpu_ttm_unreserve_vram(struct amdgpu_device *adev,
+                              enum amdgpu_resv_region_id id)
+{
+       struct amdgpu_vram_resv *resv;
+
+       if (id >= AMDGPU_RESV_MAX)
+               return;
+
+       resv = &adev->mman.resv_region[id];
+       if (!resv->bo)
+               return;
+
+       amdgpu_bo_free_kernel(&resv->bo, NULL,
+                             resv->needs_cpu_map ? &resv->cpu_addr : NULL);
+       memset(resv, 0, sizeof(*resv));
+}
+
 /*
  * Firmware Reservation functions
  */
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
index bf101215757e..b73f65a4bc0d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
@@ -59,6 +59,26 @@ struct amdgpu_ttm_buffer_entity {
        u64                     gart_window_offs[2];
 };
 
+enum amdgpu_resv_region_id {
+       AMDGPU_RESV_STOLEN_VGA,
+       AMDGPU_RESV_STOLEN_EXTENDED,
+       AMDGPU_RESV_STOLEN_RESERVED,
+       AMDGPU_RESV_FW,
+       AMDGPU_RESV_FW_EXTEND,
+       AMDGPU_RESV_FW_VRAM_USAGE,
+       AMDGPU_RESV_DRV_VRAM_USAGE,
+       AMDGPU_RESV_MEM_TRAIN,
+       AMDGPU_RESV_MAX
+};
+
+struct amdgpu_vram_resv {
+       uint64_t                offset;
+       uint64_t                size;
+       struct amdgpu_bo        *bo;
+       void                    *cpu_addr;
+       bool                    needs_cpu_map;
+};
+
 struct amdgpu_mman {
        struct ttm_device               bdev;
        struct ttm_pool                 *ttm_pools;
@@ -105,6 +125,8 @@ struct amdgpu_mman {
        struct amdgpu_bo        *drv_vram_usage_reserved_bo;
        void            *drv_vram_usage_va;
 
+       struct amdgpu_vram_resv         resv_region[AMDGPU_RESV_MAX];
+
        /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
        struct amdgpu_bo        *sdma_access_bo;
        void                    *sdma_access_ptr;
@@ -171,6 +193,15 @@ void amdgpu_vram_mgr_clear_reset_blocks(struct 
amdgpu_device *adev);
 bool amdgpu_res_cpu_visible(struct amdgpu_device *adev,
                            struct ttm_resource *res);
 
+void amdgpu_ttm_init_vram_resv(struct amdgpu_device *adev,
+                               enum amdgpu_resv_region_id id,
+                               uint64_t offset, uint64_t size,
+                               bool needs_cpu_map);
+int amdgpu_ttm_reserve_vram(struct amdgpu_device *adev,
+                           enum amdgpu_resv_region_id id);
+void amdgpu_ttm_unreserve_vram(struct amdgpu_device *adev,
+                              enum amdgpu_resv_region_id id);
+
 int amdgpu_ttm_init(struct amdgpu_device *adev);
 void amdgpu_ttm_fini(struct amdgpu_device *adev);
 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,
-- 
2.49.0

Reply via email to