1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
5 #include <drm/ttm/ttm_bo_driver.h>
6 #include <drm/ttm/ttm_device.h>
7 #include <drm/ttm/ttm_range_manager.h>
10 #include "i915_scatterlist.h"
12 #include "intel_region_ttm.h"
15 * DOC: TTM support structure
17 * The code in this file deals with setting up memory managers for TTM
18 * LMEM and MOCK regions and converting the output from
19 * the managers to struct sg_table, Basically providing the mapping from
20 * i915 GEM regions to TTM memory types and resource managers.
23 /* A Zero-initialized driver for now. We don't have a TTM backend yet. */
24 static struct ttm_device_funcs i915_ttm_bo_driver;
27 * intel_region_ttm_device_init - Initialize a TTM device
28 * @dev_priv: Pointer to an i915 device private structure.
30 * Return: 0 on success, negative error code on failure.
32 int intel_region_ttm_device_init(struct drm_i915_private *dev_priv)
34 struct drm_device *drm = &dev_priv->drm;
36 return ttm_device_init(&dev_priv->bdev, &i915_ttm_bo_driver,
37 drm->dev, drm->anon_inode->i_mapping,
38 drm->vma_offset_manager, false, false);
42 * intel_region_ttm_device_fini - Finalize a TTM device
43 * @dev_priv: Pointer to an i915 device private structure.
45 void intel_region_ttm_device_fini(struct drm_i915_private *dev_priv)
47 ttm_device_fini(&dev_priv->bdev);
51 * Map the i915 memory regions to TTM memory types. We use the
52 * driver-private types for now, reserving TTM_PL_VRAM for stolen
53 * memory and TTM_PL_TT for GGTT use if decided to implement this.
55 static int intel_region_to_ttm_type(struct intel_memory_region *mem)
59 GEM_BUG_ON(mem->type != INTEL_MEMORY_LOCAL &&
60 mem->type != INTEL_MEMORY_MOCK);
62 type = mem->instance + TTM_PL_PRIV;
63 GEM_BUG_ON(type >= TTM_NUM_MEM_TYPES);
68 static struct ttm_resource *
69 intel_region_ttm_node_reserve(struct intel_memory_region *mem,
70 resource_size_t offset,
73 struct ttm_resource_manager *man = mem->region_private;
74 struct ttm_place place = {};
75 struct ttm_buffer_object mock_bo = {};
76 struct ttm_resource *res;
80 * Having to use a mock_bo is unfortunate but stems from some
81 * drivers having private managers that insist to know what the
82 * allocate memory is intended for, using it to send private
83 * data to the manager. Also recently the bo has been used to send
84 * alignment info to the manager. Assume that apart from the latter,
85 * none of the managers we use will ever access the buffer object
86 * members, hoping we can pass the alignment info in the
87 * struct ttm_place in the future.
90 place.fpfn = offset >> PAGE_SHIFT;
91 place.lpfn = place.fpfn + (size >> PAGE_SHIFT);
92 mock_bo.base.size = size;
93 ret = man->func->alloc(man, &mock_bo, &place, &res);
97 return ret ? ERR_PTR(ret) : res;
101 * intel_region_ttm_node_free - Free a node allocated from a resource manager
102 * @mem: The region the node was allocated from.
103 * @node: The opaque node representing an allocation.
105 void intel_region_ttm_node_free(struct intel_memory_region *mem,
106 struct ttm_resource *res)
108 struct ttm_resource_manager *man = mem->region_private;
110 man->func->free(man, res);
113 static const struct intel_memory_region_private_ops priv_ops = {
114 .reserve = intel_region_ttm_node_reserve,
115 .free = intel_region_ttm_node_free,
118 int intel_region_ttm_init(struct intel_memory_region *mem)
120 struct ttm_device *bdev = &mem->i915->bdev;
121 int mem_type = intel_region_to_ttm_type(mem);
124 ret = ttm_range_man_init(bdev, mem_type, false,
125 resource_size(&mem->region) >> PAGE_SHIFT);
129 mem->chunk_size = PAGE_SIZE;
131 get_order(rounddown_pow_of_two(resource_size(&mem->region)));
132 mem->is_range_manager = true;
133 mem->priv_ops = &priv_ops;
134 mem->region_private = ttm_manager_type(bdev, mem_type);
140 * intel_region_ttm_fini - Finalize a TTM region.
141 * @mem: The memory region
143 * This functions takes down the TTM resource manager associated with the
144 * memory region, and if it was registered with the TTM device,
145 * removes that registration.
147 void intel_region_ttm_fini(struct intel_memory_region *mem)
151 ret = ttm_range_man_fini(&mem->i915->bdev,
152 intel_region_to_ttm_type(mem));
154 mem->region_private = NULL;
158 * intel_region_ttm_node_to_st - Convert an opaque TTM resource manager node
160 * @mem: The memory region.
161 * @node: The resource manager node obtained from the TTM resource manager.
163 * The gem backends typically use sg-tables for operations on the underlying
164 * io_memory. So provide a way for the backends to translate the
165 * nodes they are handed from TTM to sg-tables.
167 * Return: A malloced sg_table on success, an error pointer on failure.
169 struct sg_table *intel_region_ttm_node_to_st(struct intel_memory_region *mem,
170 struct ttm_resource *res)
172 struct ttm_range_mgr_node *range_node =
173 container_of(res, typeof(*range_node), base);
175 GEM_WARN_ON(!mem->is_range_manager);
176 return i915_sg_from_mm_node(&range_node->mm_nodes[0],
181 * intel_region_ttm_node_alloc - Allocate memory resources from a region
182 * @mem: The memory region,
183 * @size: The requested size in bytes
184 * @flags: Allocation flags
186 * This functionality is provided only for callers that need to allocate
187 * memory from standalone TTM range managers, without the TTM eviction
188 * functionality. Don't use if you are not completely sure that's the
189 * case. The returned opaque node can be converted to an sg_table using
190 * intel_region_ttm_node_to_st(), and can be freed using
191 * intel_region_ttm_node_free().
193 * Return: A valid pointer on success, an error pointer on failure.
195 struct ttm_resource *
196 intel_region_ttm_node_alloc(struct intel_memory_region *mem,
197 resource_size_t size,
200 struct ttm_resource_manager *man = mem->region_private;
201 struct ttm_place place = {};
202 struct ttm_buffer_object mock_bo = {};
203 struct ttm_resource *res;
207 * We ignore the flags for now since we're using the range
208 * manager and contigous and min page size would be fulfilled
209 * by default if size is min page size aligned.
211 mock_bo.base.size = size;
213 if (mem->is_range_manager) {
215 mock_bo.page_alignment = SZ_1G >> PAGE_SHIFT;
216 else if (size >= SZ_2M)
217 mock_bo.page_alignment = SZ_2M >> PAGE_SHIFT;
218 else if (size >= SZ_64K)
219 mock_bo.page_alignment = SZ_64K >> PAGE_SHIFT;
222 ret = man->func->alloc(man, &mock_bo, &place, &res);
225 return ret ? ERR_PTR(ret) : res;