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"
11 #include "i915_ttm_buddy_manager.h"
13 #include "intel_region_ttm.h"
15 #include "gem/i915_gem_ttm.h" /* For the funcs/ops export only */
17 * DOC: TTM support structure
19 * The code in this file deals with setting up memory managers for TTM
20 * LMEM and MOCK regions and converting the output from
21 * the managers to struct sg_table, Basically providing the mapping from
22 * i915 GEM regions to TTM memory types and resource managers.
26 * intel_region_ttm_device_init - Initialize a TTM device
27 * @dev_priv: Pointer to an i915 device private structure.
29 * Return: 0 on success, negative error code on failure.
31 int intel_region_ttm_device_init(struct drm_i915_private *dev_priv)
33 struct drm_device *drm = &dev_priv->drm;
35 return ttm_device_init(&dev_priv->bdev, i915_ttm_driver(),
36 drm->dev, drm->anon_inode->i_mapping,
37 drm->vma_offset_manager, false, false);
41 * intel_region_ttm_device_fini - Finalize a TTM device
42 * @dev_priv: Pointer to an i915 device private structure.
44 void intel_region_ttm_device_fini(struct drm_i915_private *dev_priv)
46 ttm_device_fini(&dev_priv->bdev);
50 * Map the i915 memory regions to TTM memory types. We use the
51 * driver-private types for now, reserving TTM_PL_VRAM for stolen
52 * memory and TTM_PL_TT for GGTT use if decided to implement this.
54 int intel_region_to_ttm_type(const struct intel_memory_region *mem)
58 GEM_BUG_ON(mem->type != INTEL_MEMORY_LOCAL &&
59 mem->type != INTEL_MEMORY_MOCK &&
60 mem->type != INTEL_MEMORY_SYSTEM);
62 if (mem->type == INTEL_MEMORY_SYSTEM)
65 type = mem->instance + TTM_PL_PRIV;
66 GEM_BUG_ON(type >= TTM_NUM_MEM_TYPES);
72 * intel_region_ttm_init - Initialize a memory region for TTM.
73 * @mem: The region to initialize.
75 * This function initializes a suitable TTM resource manager for the
76 * region, and if it's a LMEM region type, attaches it to the TTM
77 * device. MOCK regions are NOT attached to the TTM device, since we don't
78 * have one for the mock selftests.
80 * Return: 0 on success, negative error code on failure.
82 int intel_region_ttm_init(struct intel_memory_region *mem)
84 struct ttm_device *bdev = &mem->i915->bdev;
85 int mem_type = intel_region_to_ttm_type(mem);
88 ret = i915_ttm_buddy_man_init(bdev, mem_type, false,
89 resource_size(&mem->region),
90 mem->min_page_size, PAGE_SIZE);
94 mem->region_private = ttm_manager_type(bdev, mem_type);
100 * intel_region_ttm_fini - Finalize a TTM region.
101 * @mem: The memory region
103 * This functions takes down the TTM resource manager associated with the
104 * memory region, and if it was registered with the TTM device,
105 * removes that registration.
107 void intel_region_ttm_fini(struct intel_memory_region *mem)
111 ret = i915_ttm_buddy_man_fini(&mem->i915->bdev,
112 intel_region_to_ttm_type(mem));
114 mem->region_private = NULL;
118 * intel_region_ttm_resource_to_st - Convert an opaque TTM resource manager resource
120 * @mem: The memory region.
121 * @res: The resource manager resource obtained from the TTM resource manager.
123 * The gem backends typically use sg-tables for operations on the underlying
124 * io_memory. So provide a way for the backends to translate the
125 * nodes they are handed from TTM to sg-tables.
127 * Return: A malloced sg_table on success, an error pointer on failure.
129 struct sg_table *intel_region_ttm_resource_to_st(struct intel_memory_region *mem,
130 struct ttm_resource *res)
132 if (mem->is_range_manager) {
133 struct ttm_range_mgr_node *range_node =
134 to_ttm_range_mgr_node(res);
136 return i915_sg_from_mm_node(&range_node->mm_nodes[0],
139 return i915_sg_from_buddy_resource(res, mem->region.start);
143 #ifdef CONFIG_DRM_I915_SELFTEST
145 * intel_region_ttm_resource_alloc - Allocate memory resources from a region
146 * @mem: The memory region,
147 * @size: The requested size in bytes
148 * @flags: Allocation flags
150 * This functionality is provided only for callers that need to allocate
151 * memory from standalone TTM range managers, without the TTM eviction
152 * functionality. Don't use if you are not completely sure that's the
153 * case. The returned opaque node can be converted to an sg_table using
154 * intel_region_ttm_resource_to_st(), and can be freed using
155 * intel_region_ttm_resource_free().
157 * Return: A valid pointer on success, an error pointer on failure.
159 struct ttm_resource *
160 intel_region_ttm_resource_alloc(struct intel_memory_region *mem,
161 resource_size_t size,
164 struct ttm_resource_manager *man = mem->region_private;
165 struct ttm_place place = {};
166 struct ttm_buffer_object mock_bo = {};
167 struct ttm_resource *res;
170 mock_bo.base.size = size;
173 ret = man->func->alloc(man, &mock_bo, &place, &res);
176 return ret ? ERR_PTR(ret) : res;
182 * intel_region_ttm_resource_free - Free a resource allocated from a resource manager
183 * @mem: The region the resource was allocated from.
184 * @res: The opaque resource representing an allocation.
186 void intel_region_ttm_resource_free(struct intel_memory_region *mem,
187 struct ttm_resource *res)
189 struct ttm_resource_manager *man = mem->region_private;
191 man->func->free(man, res);