2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
7 #include "i915_scatterlist.h"
9 #include "i915_buddy.h"
10 #include "i915_ttm_buddy_manager.h"
12 #include <drm/drm_mm.h>
14 #include <linux/slab.h>
16 bool i915_sg_trim(struct sg_table *orig_st)
18 struct sg_table new_st;
19 struct scatterlist *sg, *new_sg;
22 if (orig_st->nents == orig_st->orig_nents)
25 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
29 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
30 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
31 sg_dma_address(new_sg) = sg_dma_address(sg);
32 sg_dma_len(new_sg) = sg_dma_len(sg);
34 new_sg = sg_next(new_sg);
36 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
38 sg_free_table(orig_st);
44 static void i915_refct_sgt_release(struct kref *ref)
46 struct i915_refct_sgt *rsgt =
47 container_of(ref, typeof(*rsgt), kref);
49 sg_free_table(&rsgt->table);
53 static const struct i915_refct_sgt_ops rsgt_ops = {
54 .release = i915_refct_sgt_release
58 * i915_refct_sgt_init - Initialize a struct i915_refct_sgt with default ops
59 * @rsgt: The struct i915_refct_sgt to initialize.
60 * size: The size of the underlying memory buffer.
62 void i915_refct_sgt_init(struct i915_refct_sgt *rsgt, size_t size)
64 __i915_refct_sgt_init(rsgt, size, &rsgt_ops);
68 * i915_rsgt_from_mm_node - Create a refcounted sg_table from a struct
70 * @node: The drm_mm_node.
71 * @region_start: An offset to add to the dma addresses of the sg list.
73 * Create a struct sg_table, initializing it from a struct drm_mm_node,
74 * taking a maximum segment length into account, splitting into segments
77 * Return: A pointer to a kmalloced struct i915_refct_sgt on success, negative
78 * error code cast to an error pointer on failure.
80 struct i915_refct_sgt *i915_rsgt_from_mm_node(const struct drm_mm_node *node,
83 const u64 max_segment = SZ_1G; /* Do we have a limit on this? */
84 u64 segment_pages = max_segment >> PAGE_SHIFT;
85 u64 block_size, offset, prev_end;
86 struct i915_refct_sgt *rsgt;
88 struct scatterlist *sg;
90 rsgt = kmalloc(sizeof(*rsgt), GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
94 i915_refct_sgt_init(rsgt, node->size << PAGE_SHIFT);
96 if (sg_alloc_table(st, DIV_ROUND_UP(node->size, segment_pages),
98 i915_refct_sgt_put(rsgt);
99 return ERR_PTR(-ENOMEM);
104 prev_end = (resource_size_t)-1;
105 block_size = node->size << PAGE_SHIFT;
106 offset = node->start << PAGE_SHIFT;
111 if (offset != prev_end || sg->length >= max_segment) {
115 sg_dma_address(sg) = region_start + offset;
121 len = min(block_size, max_segment - sg->length);
123 sg_dma_len(sg) += len;
138 * i915_rsgt_from_buddy_resource - Create a refcounted sg_table from a struct
139 * i915_buddy_block list
140 * @res: The struct i915_ttm_buddy_resource.
141 * @region_start: An offset to add to the dma addresses of the sg list.
143 * Create a struct sg_table, initializing it from struct i915_buddy_block list,
144 * taking a maximum segment length into account, splitting into segments
147 * Return: A pointer to a kmalloced struct i915_refct_sgts on success, negative
148 * error code cast to an error pointer on failure.
150 struct i915_refct_sgt *i915_rsgt_from_buddy_resource(struct ttm_resource *res,
153 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
154 const u64 size = res->num_pages << PAGE_SHIFT;
155 const u64 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
156 struct i915_buddy_mm *mm = bman_res->mm;
157 struct list_head *blocks = &bman_res->blocks;
158 struct i915_buddy_block *block;
159 struct i915_refct_sgt *rsgt;
160 struct scatterlist *sg;
162 resource_size_t prev_end;
164 GEM_BUG_ON(list_empty(blocks));
166 rsgt = kmalloc(sizeof(*rsgt), GFP_KERNEL);
168 return ERR_PTR(-ENOMEM);
170 i915_refct_sgt_init(rsgt, size);
172 if (sg_alloc_table(st, res->num_pages, GFP_KERNEL)) {
173 i915_refct_sgt_put(rsgt);
174 return ERR_PTR(-ENOMEM);
179 prev_end = (resource_size_t)-1;
181 list_for_each_entry(block, blocks, link) {
182 u64 block_size, offset;
184 block_size = min_t(u64, size, i915_buddy_block_size(mm, block));
185 offset = i915_buddy_block_offset(block);
190 if (offset != prev_end || sg->length >= max_segment) {
194 sg_dma_address(sg) = region_start + offset;
200 len = min(block_size, max_segment - sg->length);
202 sg_dma_len(sg) += len;
217 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
218 #include "selftests/scatterlist.c"