Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /*
0003  * Copyright 2016-2021 Advanced Micro Devices, Inc.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in
0013  * all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0019  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0020  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0021  * OTHER DEALINGS IN THE SOFTWARE.
0022  *
0023  * Authors: Christian König, Felix Kuehling
0024  */
0025 
0026 #include "amdgpu.h"
0027 
0028 /**
0029  * DOC: mem_info_preempt_used
0030  *
0031  * The amdgpu driver provides a sysfs API for reporting current total amount of
0032  * used preemptible memory.
0033  * The file mem_info_preempt_used is used for this, and returns the current
0034  * used size of the preemptible block, in bytes
0035  */
0036 static ssize_t mem_info_preempt_used_show(struct device *dev,
0037                       struct device_attribute *attr,
0038                       char *buf)
0039 {
0040     struct drm_device *ddev = dev_get_drvdata(dev);
0041     struct amdgpu_device *adev = drm_to_adev(ddev);
0042     struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
0043 
0044     return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man));
0045 }
0046 
0047 static DEVICE_ATTR_RO(mem_info_preempt_used);
0048 
0049 /**
0050  * amdgpu_preempt_mgr_new - allocate a new node
0051  *
0052  * @man: TTM memory type manager
0053  * @tbo: TTM BO we need this range for
0054  * @place: placement flags and restrictions
0055  * @res: TTM memory object
0056  *
0057  * Dummy, just count the space used without allocating resources or any limit.
0058  */
0059 static int amdgpu_preempt_mgr_new(struct ttm_resource_manager *man,
0060                   struct ttm_buffer_object *tbo,
0061                   const struct ttm_place *place,
0062                   struct ttm_resource **res)
0063 {
0064     *res = kzalloc(sizeof(**res), GFP_KERNEL);
0065     if (!*res)
0066         return -ENOMEM;
0067 
0068     ttm_resource_init(tbo, place, *res);
0069     (*res)->start = AMDGPU_BO_INVALID_OFFSET;
0070     return 0;
0071 }
0072 
0073 /**
0074  * amdgpu_preempt_mgr_del - free ranges
0075  *
0076  * @man: TTM memory type manager
0077  * @res: TTM memory object
0078  *
0079  * Free the allocated GTT again.
0080  */
0081 static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
0082                    struct ttm_resource *res)
0083 {
0084     ttm_resource_fini(man, res);
0085     kfree(res);
0086 }
0087 
0088 static const struct ttm_resource_manager_func amdgpu_preempt_mgr_func = {
0089     .alloc = amdgpu_preempt_mgr_new,
0090     .free = amdgpu_preempt_mgr_del,
0091 };
0092 
0093 /**
0094  * amdgpu_preempt_mgr_init - init PREEMPT manager and DRM MM
0095  *
0096  * @adev: amdgpu_device pointer
0097  *
0098  * Allocate and initialize the GTT manager.
0099  */
0100 int amdgpu_preempt_mgr_init(struct amdgpu_device *adev)
0101 {
0102     struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
0103     int ret;
0104 
0105     man->use_tt = true;
0106     man->func = &amdgpu_preempt_mgr_func;
0107 
0108     ttm_resource_manager_init(man, &adev->mman.bdev, (1 << 30));
0109 
0110     ret = device_create_file(adev->dev, &dev_attr_mem_info_preempt_used);
0111     if (ret) {
0112         DRM_ERROR("Failed to create device file mem_info_preempt_used\n");
0113         return ret;
0114     }
0115 
0116     ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, man);
0117     ttm_resource_manager_set_used(man, true);
0118     return 0;
0119 }
0120 
0121 /**
0122  * amdgpu_preempt_mgr_fini - free and destroy GTT manager
0123  *
0124  * @adev: amdgpu_device pointer
0125  *
0126  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
0127  * allocated inside it.
0128  */
0129 void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev)
0130 {
0131     struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
0132     int ret;
0133 
0134     ttm_resource_manager_set_used(man, false);
0135 
0136     ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
0137     if (ret)
0138         return;
0139 
0140     device_remove_file(adev->dev, &dev_attr_mem_info_preempt_used);
0141 
0142     ttm_resource_manager_cleanup(man);
0143     ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, NULL);
0144 }