Loading...
1/**************************************************************************
2 *
3 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
31#include <drm/ttm/ttm_module.h>
32#include <drm/ttm/ttm_bo_driver.h>
33#include <drm/ttm/ttm_placement.h>
34#include <drm/drm_mm.h>
35#include <linux/slab.h>
36#include <linux/spinlock.h>
37#include <linux/module.h>
38
39/**
40 * Currently we use a spinlock for the lock, but a mutex *may* be
41 * more appropriate to reduce scheduling latency if the range manager
42 * ends up with very fragmented allocation patterns.
43 */
44
45struct ttm_range_manager {
46 struct drm_mm mm;
47 spinlock_t lock;
48};
49
50static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
51 struct ttm_buffer_object *bo,
52 const struct ttm_place *place,
53 struct ttm_mem_reg *mem)
54{
55 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
56 struct drm_mm *mm = &rman->mm;
57 struct drm_mm_node *node = NULL;
58 enum drm_mm_search_flags sflags = DRM_MM_SEARCH_BEST;
59 enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT;
60 unsigned long lpfn;
61 int ret;
62
63 lpfn = place->lpfn;
64 if (!lpfn)
65 lpfn = man->size;
66
67 node = kzalloc(sizeof(*node), GFP_KERNEL);
68 if (!node)
69 return -ENOMEM;
70
71 if (place->flags & TTM_PL_FLAG_TOPDOWN) {
72 sflags = DRM_MM_SEARCH_BELOW;
73 aflags = DRM_MM_CREATE_TOP;
74 }
75
76 spin_lock(&rman->lock);
77 ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages,
78 mem->page_alignment, 0,
79 place->fpfn, lpfn,
80 sflags, aflags);
81 spin_unlock(&rman->lock);
82
83 if (unlikely(ret)) {
84 kfree(node);
85 } else {
86 mem->mm_node = node;
87 mem->start = node->start;
88 }
89
90 return 0;
91}
92
93static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
94 struct ttm_mem_reg *mem)
95{
96 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
97
98 if (mem->mm_node) {
99 spin_lock(&rman->lock);
100 drm_mm_remove_node(mem->mm_node);
101 spin_unlock(&rman->lock);
102
103 kfree(mem->mm_node);
104 mem->mm_node = NULL;
105 }
106}
107
108static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
109 unsigned long p_size)
110{
111 struct ttm_range_manager *rman;
112
113 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
114 if (!rman)
115 return -ENOMEM;
116
117 drm_mm_init(&rman->mm, 0, p_size);
118 spin_lock_init(&rman->lock);
119 man->priv = rman;
120 return 0;
121}
122
123static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
124{
125 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
126 struct drm_mm *mm = &rman->mm;
127
128 spin_lock(&rman->lock);
129 if (drm_mm_clean(mm)) {
130 drm_mm_takedown(mm);
131 spin_unlock(&rman->lock);
132 kfree(rman);
133 man->priv = NULL;
134 return 0;
135 }
136 spin_unlock(&rman->lock);
137 return -EBUSY;
138}
139
140static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
141 const char *prefix)
142{
143 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
144
145 spin_lock(&rman->lock);
146 drm_mm_debug_table(&rman->mm, prefix);
147 spin_unlock(&rman->lock);
148}
149
150const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
151 ttm_bo_man_init,
152 ttm_bo_man_takedown,
153 ttm_bo_man_get_node,
154 ttm_bo_man_put_node,
155 ttm_bo_man_debug
156};
157EXPORT_SYMBOL(ttm_bo_manager_func);
1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2/**************************************************************************
3 *
4 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28/*
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 */
31
32#include <drm/ttm/ttm_module.h>
33#include <drm/ttm/ttm_bo_driver.h>
34#include <drm/ttm/ttm_placement.h>
35#include <drm/drm_mm.h>
36#include <linux/slab.h>
37#include <linux/spinlock.h>
38#include <linux/module.h>
39
40/**
41 * Currently we use a spinlock for the lock, but a mutex *may* be
42 * more appropriate to reduce scheduling latency if the range manager
43 * ends up with very fragmented allocation patterns.
44 */
45
46struct ttm_range_manager {
47 struct drm_mm mm;
48 spinlock_t lock;
49};
50
51static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
52 struct ttm_buffer_object *bo,
53 const struct ttm_place *place,
54 struct ttm_mem_reg *mem)
55{
56 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
57 struct drm_mm *mm = &rman->mm;
58 struct drm_mm_node *node;
59 enum drm_mm_insert_mode mode;
60 unsigned long lpfn;
61 int ret;
62
63 lpfn = place->lpfn;
64 if (!lpfn)
65 lpfn = man->size;
66
67 node = kzalloc(sizeof(*node), GFP_KERNEL);
68 if (!node)
69 return -ENOMEM;
70
71 mode = DRM_MM_INSERT_BEST;
72 if (place->flags & TTM_PL_FLAG_TOPDOWN)
73 mode = DRM_MM_INSERT_HIGH;
74
75 spin_lock(&rman->lock);
76 ret = drm_mm_insert_node_in_range(mm, node,
77 mem->num_pages,
78 mem->page_alignment, 0,
79 place->fpfn, lpfn, mode);
80 spin_unlock(&rman->lock);
81
82 if (unlikely(ret)) {
83 kfree(node);
84 } else {
85 mem->mm_node = node;
86 mem->start = node->start;
87 }
88
89 return 0;
90}
91
92static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
93 struct ttm_mem_reg *mem)
94{
95 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
96
97 if (mem->mm_node) {
98 spin_lock(&rman->lock);
99 drm_mm_remove_node(mem->mm_node);
100 spin_unlock(&rman->lock);
101
102 kfree(mem->mm_node);
103 mem->mm_node = NULL;
104 }
105}
106
107static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
108 unsigned long p_size)
109{
110 struct ttm_range_manager *rman;
111
112 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
113 if (!rman)
114 return -ENOMEM;
115
116 drm_mm_init(&rman->mm, 0, p_size);
117 spin_lock_init(&rman->lock);
118 man->priv = rman;
119 return 0;
120}
121
122static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
123{
124 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
125 struct drm_mm *mm = &rman->mm;
126
127 spin_lock(&rman->lock);
128 if (drm_mm_clean(mm)) {
129 drm_mm_takedown(mm);
130 spin_unlock(&rman->lock);
131 kfree(rman);
132 man->priv = NULL;
133 return 0;
134 }
135 spin_unlock(&rman->lock);
136 return -EBUSY;
137}
138
139static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
140 struct drm_printer *printer)
141{
142 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
143
144 spin_lock(&rman->lock);
145 drm_mm_print(&rman->mm, printer);
146 spin_unlock(&rman->lock);
147}
148
149const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
150 .init = ttm_bo_man_init,
151 .takedown = ttm_bo_man_takedown,
152 .get_node = ttm_bo_man_get_node,
153 .put_node = ttm_bo_man_put_node,
154 .debug = ttm_bo_man_debug
155};
156EXPORT_SYMBOL(ttm_bo_manager_func);