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);
45 * i915_sg_from_mm_node - Create an sg_table from a struct drm_mm_node
46 * @node: The drm_mm_node.
47 * @region_start: An offset to add to the dma addresses of the sg list.
49 * Create a struct sg_table, initializing it from a struct drm_mm_node,
50 * taking a maximum segment length into account, splitting into segments
53 * Return: A pointer to a kmalloced struct sg_table on success, negative
54 * error code cast to an error pointer on failure.
56 struct sg_table *i915_sg_from_mm_node(const struct drm_mm_node *node,
59 const u64 max_segment = SZ_1G; /* Do we have a limit on this? */
60 u64 segment_pages = max_segment >> PAGE_SHIFT;
61 u64 block_size, offset, prev_end;
63 struct scatterlist *sg;
65 st = kmalloc(sizeof(*st), GFP_KERNEL);
67 return ERR_PTR(-ENOMEM);
69 if (sg_alloc_table(st, DIV_ROUND_UP(node->size, segment_pages),
72 return ERR_PTR(-ENOMEM);
77 prev_end = (resource_size_t)-1;
78 block_size = node->size << PAGE_SHIFT;
79 offset = node->start << PAGE_SHIFT;
84 if (offset != prev_end || sg->length >= max_segment) {
88 sg_dma_address(sg) = region_start + offset;
94 len = min(block_size, max_segment - sg->length);
96 sg_dma_len(sg) += len;
111 * i915_sg_from_buddy_resource - Create an sg_table from a struct
112 * i915_buddy_block list
113 * @res: The struct i915_ttm_buddy_resource.
114 * @region_start: An offset to add to the dma addresses of the sg list.
116 * Create a struct sg_table, initializing it from struct i915_buddy_block list,
117 * taking a maximum segment length into account, splitting into segments
120 * Return: A pointer to a kmalloced struct sg_table on success, negative
121 * error code cast to an error pointer on failure.
123 struct sg_table *i915_sg_from_buddy_resource(struct ttm_resource *res,
126 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
127 const u64 size = res->num_pages << PAGE_SHIFT;
128 const u64 max_segment = rounddown(UINT_MAX, PAGE_SIZE);
129 struct i915_buddy_mm *mm = bman_res->mm;
130 struct list_head *blocks = &bman_res->blocks;
131 struct i915_buddy_block *block;
132 struct scatterlist *sg;
134 resource_size_t prev_end;
136 GEM_BUG_ON(list_empty(blocks));
138 st = kmalloc(sizeof(*st), GFP_KERNEL);
140 return ERR_PTR(-ENOMEM);
142 if (sg_alloc_table(st, res->num_pages, GFP_KERNEL)) {
144 return ERR_PTR(-ENOMEM);
149 prev_end = (resource_size_t)-1;
151 list_for_each_entry(block, blocks, link) {
152 u64 block_size, offset;
154 block_size = min_t(u64, size, i915_buddy_block_size(mm, block));
155 offset = i915_buddy_block_offset(block);
160 if (offset != prev_end || sg->length >= max_segment) {
164 sg_dma_address(sg) = region_start + offset;
170 len = min(block_size, max_segment - sg->length);
172 sg_dma_len(sg) += len;
187 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
188 #include "selftests/scatterlist.c"