]> git.baikalelectronics.ru Git - kernel.git/commitdiff
drm/ttm: flip over the range manager to self allocated nodes
authorChristian König <christian.koenig@amd.com>
Sat, 17 Apr 2021 16:48:36 +0000 (18:48 +0200)
committerChristian König <christian.koenig@amd.com>
Fri, 4 Jun 2021 13:16:45 +0000 (15:16 +0200)
Start with the range manager to make the resource object the base
class for the allocated nodes.

While at it cleanup a lot of the code around that.

Signed-off-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20210602100914.46246-2-christian.koenig@amd.com
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
drivers/gpu/drm/drm_gem_vram_helper.c
drivers/gpu/drm/nouveau/nouveau_ttm.c
drivers/gpu/drm/qxl/qxl_ttm.c
drivers/gpu/drm/radeon/radeon_ttm.c
drivers/gpu/drm/ttm/ttm_range_manager.c
drivers/gpu/drm/ttm/ttm_resource.c
include/drm/ttm/ttm_bo_driver.h
include/drm/ttm/ttm_range_manager.h [new file with mode: 0644]
include/drm/ttm/ttm_resource.h

index 69db8926165023d2fb30b76cc5ebbe8f69968ca4..df1f185faae9aaaaac77e85f685e6ed23e329396 100644 (file)
@@ -45,6 +45,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include <drm/amdgpu_drm.h>
 
index 83e7258c7f909b267ca58ec05f0d07b1332cb0c1..17a4c5d47b6a5b1f93f09c578f38c0a4ba0ac5c2 100644 (file)
@@ -17,6 +17,8 @@
 #include <drm/drm_prime.h>
 #include <drm/drm_simple_kms_helper.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
 
 /**
index 65430912ff7273d9dd754febe8bec87532e467a5..b08b8efeefbaf342f23dab52c096e5963b7468be 100644 (file)
@@ -26,6 +26,8 @@
 #include <linux/limits.h>
 #include <linux/swiotlb.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
 #include "nouveau_drv.h"
 #include "nouveau_gem.h"
 #include "nouveau_mem.h"
index 8aa87b8edb9ccbd2a3f90d92fac8a62cb71bc2cf..19fd39d9a00cb40fabf6c15cb138557aa0524496 100644 (file)
@@ -32,6 +32,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include "qxl_drv.h"
 #include "qxl_object.h"
index cdffa9b65108a2610e5153dad46309fbb4409994..ad2a5a791bbafdbbd2fdc1b0645665dbd6aaefe3 100644 (file)
@@ -45,6 +45,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include "radeon_reg.h"
 #include "radeon.h"
index b9d5da6e6a8106caed189164db4ed5c7413860ba..ce5d07ca384cc475d807f95f7c476f0b503be1a0 100644 (file)
  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  */
 
-#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>
 #include <drm/drm_mm.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/module.h>
 
 /*
  * Currently we use a spinlock for the lock, but a mutex *may* be
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
                               struct ttm_resource *mem)
 {
        struct ttm_range_manager *rman = to_range_manager(man);
+       struct ttm_range_mgr_node *node;
        struct drm_mm *mm = &rman->mm;
-       struct drm_mm_node *node;
        enum drm_mm_insert_mode mode;
        unsigned long lpfn;
        int ret;
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
        if (!lpfn)
                lpfn = man->size;
 
-       node = kzalloc(sizeof(*node), GFP_KERNEL);
+       node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);
        if (!node)
                return -ENOMEM;
 
@@ -78,17 +79,19 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
        if (place->flags & TTM_PL_FLAG_TOPDOWN)
                mode = DRM_MM_INSERT_HIGH;
 
+       ttm_resource_init(bo, place, &node->base);
+
        spin_lock(&rman->lock);
-       ret = drm_mm_insert_node_in_range(mm, node, mem->num_pages,
-                                         bo->page_alignment, 0,
+       ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
+                                         mem->num_pages, bo->page_alignment, 0,
                                          place->fpfn, lpfn, mode);
        spin_unlock(&rman->lock);
 
        if (unlikely(ret)) {
                kfree(node);
        } else {
-               mem->mm_node = node;
-               mem->start = node->start;
+               mem->mm_node = &node->mm_nodes[0];
+               mem->start = node->mm_nodes[0].start;
        }
 
        return ret;
@@ -98,15 +101,19 @@ static void ttm_range_man_free(struct ttm_resource_manager *man,
                               struct ttm_resource *mem)
 {
        struct ttm_range_manager *rman = to_range_manager(man);
+       struct ttm_range_mgr_node *node;
 
-       if (mem->mm_node) {
-               spin_lock(&rman->lock);
-               drm_mm_remove_node(mem->mm_node);
-               spin_unlock(&rman->lock);
+       if (!mem->mm_node)
+               return;
 
-               kfree(mem->mm_node);
-               mem->mm_node = NULL;
-       }
+       node = to_ttm_range_mgr_node(mem);
+
+       spin_lock(&rman->lock);
+       drm_mm_remove_node(&node->mm_nodes[0]);
+       spin_unlock(&rman->lock);
+
+       kfree(node);
+       mem->mm_node = NULL;
 }
 
 static void ttm_range_man_debug(struct ttm_resource_manager *man,
@@ -125,6 +132,17 @@ static const struct ttm_resource_manager_func ttm_range_manager_func = {
        .debug = ttm_range_man_debug
 };
 
+/**
+ * ttm_range_man_init
+ *
+ * @bdev: ttm device
+ * @type: memory manager type
+ * @use_tt: if the memory manager uses tt
+ * @p_size: size of area to be managed in pages.
+ *
+ * Initialise a generic range manager for the selected memory type.
+ * The range manager is installed for this device in the type slot.
+ */
 int ttm_range_man_init(struct ttm_device *bdev,
                       unsigned type, bool use_tt,
                       unsigned long p_size)
@@ -152,6 +170,14 @@ int ttm_range_man_init(struct ttm_device *bdev,
 }
 EXPORT_SYMBOL(ttm_range_man_init);
 
+/**
+ * ttm_range_man_fini
+ *
+ * @bdev: ttm device
+ * @type: memory manager type
+ *
+ * Remove the generic range manager from a slot and tear it down.
+ */
 int ttm_range_man_fini(struct ttm_device *bdev,
                       unsigned type)
 {
index 65451e1bc30332845f31f9b3555b4c7b761eeb96..2a51ace1761438dc873bf57c9a07b74ae4d21d04 100644 (file)
 #include <drm/ttm/ttm_resource.h>
 #include <drm/ttm/ttm_bo_driver.h>
 
+void ttm_resource_init(struct ttm_buffer_object *bo,
+                       const struct ttm_place *place,
+                       struct ttm_resource *res)
+{
+       res->mm_node = NULL;
+       res->start = 0;
+       res->num_pages = PFN_UP(bo->base.size);
+       res->mem_type = place->mem_type;
+       res->placement = place->flags;
+       res->bus.addr = NULL;
+       res->bus.offset = 0;
+       res->bus.is_iomem = false;
+       res->bus.caching = ttm_cached;
+}
+EXPORT_SYMBOL(ttm_resource_init);
+
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
                       const struct ttm_place *place,
                       struct ttm_resource **res_ptr)
@@ -38,15 +54,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
        if (!res)
                return -ENOMEM;
 
-       res->mm_node = NULL;
-       res->start = 0;
-       res->num_pages = PFN_UP(bo->base.size);
-       res->mem_type = place->mem_type;
-       res->placement = place->flags;
-       res->bus.addr = NULL;
-       res->bus.offset = 0;
-       res->bus.is_iomem = false;
-       res->bus.caching = ttm_cached;
+       ttm_resource_init(bo, place, res);
        r = man->func->alloc(man, bo, place, res);
        if (r) {
                kfree(res);
index ead0ef7136c8bfb70e52e06e0122321add75d41e..b266971c1974280926ade69a80ff745b6f416e80 100644 (file)
@@ -304,30 +304,4 @@ int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
  */
 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
 
-/**
- * ttm_range_man_init
- *
- * @bdev: ttm device
- * @type: memory manager type
- * @use_tt: if the memory manager uses tt
- * @p_size: size of area to be managed in pages.
- *
- * Initialise a generic range manager for the selected memory type.
- * The range manager is installed for this device in the type slot.
- */
-int ttm_range_man_init(struct ttm_device *bdev,
-                      unsigned type, bool use_tt,
-                      unsigned long p_size);
-
-/**
- * ttm_range_man_fini
- *
- * @bdev: ttm device
- * @type: memory manager type
- *
- * Remove the generic range manager from a slot and tear it down.
- */
-int ttm_range_man_fini(struct ttm_device *bdev,
-                      unsigned type);
-
 #endif
diff --git a/include/drm/ttm/ttm_range_manager.h b/include/drm/ttm/ttm_range_manager.h
new file mode 100644 (file)
index 0000000..983f452
--- /dev/null
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef _TTM_RANGE_MANAGER_H_
+#define _TTM_RANGE_MANAGER_H_
+
+#include <drm/ttm/ttm_resource.h>
+#include <drm/drm_mm.h>
+
+/**
+ * struct ttm_range_mgr_node
+ *
+ * @base: base clase we extend
+ * @mm_nodes: MM nodes, usually 1
+ *
+ * Extending the ttm_resource object to manage an address space allocation with
+ * one or more drm_mm_nodes.
+ */
+struct ttm_range_mgr_node {
+       struct ttm_resource base;
+       struct drm_mm_node mm_nodes[];
+};
+
+/**
+ * to_ttm_range_mgr_node
+ *
+ * @res: the resource to upcast
+ *
+ * Upcast the ttm_resource object into a ttm_range_mgr_node object.
+ */
+static inline struct ttm_range_mgr_node *
+to_ttm_range_mgr_node(struct ttm_resource *res)
+{
+       return container_of(res->mm_node, struct ttm_range_mgr_node,
+                           mm_nodes[0]);
+}
+
+int ttm_range_man_init(struct ttm_device *bdev,
+                      unsigned type, bool use_tt,
+                      unsigned long p_size);
+int ttm_range_man_fini(struct ttm_device *bdev,
+                      unsigned type);
+
+#endif
index c17c1a52070da8c0197bd0da7c01cce1fcc5acba..803e4875d7799311b1f25f4fc231607f3e3bc4e7 100644 (file)
@@ -223,6 +223,9 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
        man->move = NULL;
 }
 
+void ttm_resource_init(struct ttm_buffer_object *bo,
+                       const struct ttm_place *place,
+                       struct ttm_resource *res);
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
                       const struct ttm_place *place,
                       struct ttm_resource **res);