1 /* $NetBSD: i915_scatterlist.c,v 1.3 2021/12/19 11:37:05 riastradh Exp $ */
2
3 /*
4 * SPDX-License-Identifier: MIT
5 *
6 * Copyright © 2016 Intel Corporation
7 */
8
9 #include <sys/cdefs.h>
10 __KERNEL_RCSID(0, "$NetBSD: i915_scatterlist.c,v 1.3 2021/12/19 11:37:05 riastradh Exp $");
11
12 #include "i915_scatterlist.h"
13
i915_sg_trim(struct sg_table * orig_st)14 bool i915_sg_trim(struct sg_table *orig_st)
15 {
16 #ifndef __NetBSD__
17 struct sg_table new_st;
18 struct scatterlist *sg, *new_sg;
19 unsigned int i;
20
21 if (orig_st->nents == orig_st->orig_nents)
22 return false;
23
24 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
25 return false;
26
27 new_sg = new_st.sgl;
28 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
29 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
30 sg_dma_address(new_sg) = sg_dma_address(sg);
31 sg_dma_len(new_sg) = sg_dma_len(sg);
32
33 new_sg = sg_next(new_sg);
34 }
35 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
36
37 sg_free_table(orig_st);
38
39 *orig_st = new_st;
40 #endif
41 return true;
42 }
43
44 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
45 #include "selftests/scatterlist.c"
46 #endif
47