Skip to content

Commit 3eb7d96

Browse files
drm/ttm: flip over the range manager to self allocated nodes
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 <[email protected]> Reviewed-by: Matthew Auld <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent bfa3357 commit 3eb7d96

File tree

10 files changed

+111
-50
lines changed

10 files changed

+111
-50
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <drm/ttm/ttm_bo_api.h>
4646
#include <drm/ttm/ttm_bo_driver.h>
4747
#include <drm/ttm/ttm_placement.h>
48+
#include <drm/ttm/ttm_range_manager.h>
4849

4950
#include <drm/amdgpu_drm.h>
5051

drivers/gpu/drm/drm_gem_vram_helper.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <drm/drm_prime.h>
1818
#include <drm/drm_simple_kms_helper.h>
1919

20+
#include <drm/ttm/ttm_range_manager.h>
21+
2022
static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
2123

2224
/**

drivers/gpu/drm/nouveau/nouveau_ttm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <linux/limits.h>
2727
#include <linux/swiotlb.h>
2828

29+
#include <drm/ttm/ttm_range_manager.h>
30+
2931
#include "nouveau_drv.h"
3032
#include "nouveau_gem.h"
3133
#include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <drm/ttm/ttm_bo_api.h>
3333
#include <drm/ttm/ttm_bo_driver.h>
3434
#include <drm/ttm/ttm_placement.h>
35+
#include <drm/ttm/ttm_range_manager.h>
3536

3637
#include "qxl_drv.h"
3738
#include "qxl_object.h"

drivers/gpu/drm/radeon/radeon_ttm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <drm/ttm/ttm_bo_api.h>
4646
#include <drm/ttm/ttm_bo_driver.h>
4747
#include <drm/ttm/ttm_placement.h>
48+
#include <drm/ttm/ttm_range_manager.h>
4849

4950
#include "radeon_reg.h"
5051
#include "radeon.h"

drivers/gpu/drm/ttm/ttm_range_manager.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
* Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
3030
*/
3131

32-
#include <drm/ttm/ttm_bo_driver.h>
32+
#include <drm/ttm/ttm_device.h>
3333
#include <drm/ttm/ttm_placement.h>
34+
#include <drm/ttm/ttm_range_manager.h>
35+
#include <drm/ttm/ttm_bo_api.h>
3436
#include <drm/drm_mm.h>
3537
#include <linux/slab.h>
3638
#include <linux/spinlock.h>
37-
#include <linux/module.h>
3839

3940
/*
4041
* 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,
6061
struct ttm_resource *mem)
6162
{
6263
struct ttm_range_manager *rman = to_range_manager(man);
64+
struct ttm_range_mgr_node *node;
6365
struct drm_mm *mm = &rman->mm;
64-
struct drm_mm_node *node;
6566
enum drm_mm_insert_mode mode;
6667
unsigned long lpfn;
6768
int ret;
@@ -70,25 +71,27 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
7071
if (!lpfn)
7172
lpfn = man->size;
7273

73-
node = kzalloc(sizeof(*node), GFP_KERNEL);
74+
node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);
7475
if (!node)
7576
return -ENOMEM;
7677

7778
mode = DRM_MM_INSERT_BEST;
7879
if (place->flags & TTM_PL_FLAG_TOPDOWN)
7980
mode = DRM_MM_INSERT_HIGH;
8081

82+
ttm_resource_init(bo, place, &node->base);
83+
8184
spin_lock(&rman->lock);
82-
ret = drm_mm_insert_node_in_range(mm, node, mem->num_pages,
83-
bo->page_alignment, 0,
85+
ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
86+
mem->num_pages, bo->page_alignment, 0,
8487
place->fpfn, lpfn, mode);
8588
spin_unlock(&rman->lock);
8689

8790
if (unlikely(ret)) {
8891
kfree(node);
8992
} else {
90-
mem->mm_node = node;
91-
mem->start = node->start;
93+
mem->mm_node = &node->mm_nodes[0];
94+
mem->start = node->mm_nodes[0].start;
9295
}
9396

9497
return ret;
@@ -98,15 +101,19 @@ static void ttm_range_man_free(struct ttm_resource_manager *man,
98101
struct ttm_resource *mem)
99102
{
100103
struct ttm_range_manager *rman = to_range_manager(man);
104+
struct ttm_range_mgr_node *node;
101105

102-
if (mem->mm_node) {
103-
spin_lock(&rman->lock);
104-
drm_mm_remove_node(mem->mm_node);
105-
spin_unlock(&rman->lock);
106+
if (!mem->mm_node)
107+
return;
106108

107-
kfree(mem->mm_node);
108-
mem->mm_node = NULL;
109-
}
109+
node = to_ttm_range_mgr_node(mem);
110+
111+
spin_lock(&rman->lock);
112+
drm_mm_remove_node(&node->mm_nodes[0]);
113+
spin_unlock(&rman->lock);
114+
115+
kfree(node);
116+
mem->mm_node = NULL;
110117
}
111118

112119
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 = {
125132
.debug = ttm_range_man_debug
126133
};
127134

135+
/**
136+
* ttm_range_man_init
137+
*
138+
* @bdev: ttm device
139+
* @type: memory manager type
140+
* @use_tt: if the memory manager uses tt
141+
* @p_size: size of area to be managed in pages.
142+
*
143+
* Initialise a generic range manager for the selected memory type.
144+
* The range manager is installed for this device in the type slot.
145+
*/
128146
int ttm_range_man_init(struct ttm_device *bdev,
129147
unsigned type, bool use_tt,
130148
unsigned long p_size)
@@ -152,6 +170,14 @@ int ttm_range_man_init(struct ttm_device *bdev,
152170
}
153171
EXPORT_SYMBOL(ttm_range_man_init);
154172

173+
/**
174+
* ttm_range_man_fini
175+
*
176+
* @bdev: ttm device
177+
* @type: memory manager type
178+
*
179+
* Remove the generic range manager from a slot and tear it down.
180+
*/
155181
int ttm_range_man_fini(struct ttm_device *bdev,
156182
unsigned type)
157183
{

drivers/gpu/drm/ttm/ttm_resource.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@
2525
#include <drm/ttm/ttm_resource.h>
2626
#include <drm/ttm/ttm_bo_driver.h>
2727

28+
void ttm_resource_init(struct ttm_buffer_object *bo,
29+
const struct ttm_place *place,
30+
struct ttm_resource *res)
31+
{
32+
res->mm_node = NULL;
33+
res->start = 0;
34+
res->num_pages = PFN_UP(bo->base.size);
35+
res->mem_type = place->mem_type;
36+
res->placement = place->flags;
37+
res->bus.addr = NULL;
38+
res->bus.offset = 0;
39+
res->bus.is_iomem = false;
40+
res->bus.caching = ttm_cached;
41+
}
42+
EXPORT_SYMBOL(ttm_resource_init);
43+
2844
int ttm_resource_alloc(struct ttm_buffer_object *bo,
2945
const struct ttm_place *place,
3046
struct ttm_resource **res_ptr)
@@ -38,15 +54,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
3854
if (!res)
3955
return -ENOMEM;
4056

41-
res->mm_node = NULL;
42-
res->start = 0;
43-
res->num_pages = PFN_UP(bo->base.size);
44-
res->mem_type = place->mem_type;
45-
res->placement = place->flags;
46-
res->bus.addr = NULL;
47-
res->bus.offset = 0;
48-
res->bus.is_iomem = false;
49-
res->bus.caching = ttm_cached;
57+
ttm_resource_init(bo, place, res);
5058
r = man->func->alloc(man, bo, place, res);
5159
if (r) {
5260
kfree(res);

include/drm/ttm/ttm_bo_driver.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,30 +304,4 @@ int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
304304
*/
305305
void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
306306

307-
/**
308-
* ttm_range_man_init
309-
*
310-
* @bdev: ttm device
311-
* @type: memory manager type
312-
* @use_tt: if the memory manager uses tt
313-
* @p_size: size of area to be managed in pages.
314-
*
315-
* Initialise a generic range manager for the selected memory type.
316-
* The range manager is installed for this device in the type slot.
317-
*/
318-
int ttm_range_man_init(struct ttm_device *bdev,
319-
unsigned type, bool use_tt,
320-
unsigned long p_size);
321-
322-
/**
323-
* ttm_range_man_fini
324-
*
325-
* @bdev: ttm device
326-
* @type: memory manager type
327-
*
328-
* Remove the generic range manager from a slot and tear it down.
329-
*/
330-
int ttm_range_man_fini(struct ttm_device *bdev,
331-
unsigned type);
332-
333307
#endif

include/drm/ttm/ttm_range_manager.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2+
3+
#ifndef _TTM_RANGE_MANAGER_H_
4+
#define _TTM_RANGE_MANAGER_H_
5+
6+
#include <drm/ttm/ttm_resource.h>
7+
#include <drm/drm_mm.h>
8+
9+
/**
10+
* struct ttm_range_mgr_node
11+
*
12+
* @base: base clase we extend
13+
* @mm_nodes: MM nodes, usually 1
14+
*
15+
* Extending the ttm_resource object to manage an address space allocation with
16+
* one or more drm_mm_nodes.
17+
*/
18+
struct ttm_range_mgr_node {
19+
struct ttm_resource base;
20+
struct drm_mm_node mm_nodes[];
21+
};
22+
23+
/**
24+
* to_ttm_range_mgr_node
25+
*
26+
* @res: the resource to upcast
27+
*
28+
* Upcast the ttm_resource object into a ttm_range_mgr_node object.
29+
*/
30+
static inline struct ttm_range_mgr_node *
31+
to_ttm_range_mgr_node(struct ttm_resource *res)
32+
{
33+
return container_of(res->mm_node, struct ttm_range_mgr_node,
34+
mm_nodes[0]);
35+
}
36+
37+
int ttm_range_man_init(struct ttm_device *bdev,
38+
unsigned type, bool use_tt,
39+
unsigned long p_size);
40+
int ttm_range_man_fini(struct ttm_device *bdev,
41+
unsigned type);
42+
43+
#endif

include/drm/ttm/ttm_resource.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
223223
man->move = NULL;
224224
}
225225

226+
void ttm_resource_init(struct ttm_buffer_object *bo,
227+
const struct ttm_place *place,
228+
struct ttm_resource *res);
226229
int ttm_resource_alloc(struct ttm_buffer_object *bo,
227230
const struct ttm_place *place,
228231
struct ttm_resource **res);

0 commit comments

Comments
 (0)