xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/i915_scatterlist.h (revision 39bff1e027336a9dd992c1e0b01318522b8b1a62)
1 /*	$NetBSD: i915_scatterlist.h,v 1.8 2021/12/19 11:37:05 riastradh Exp $	*/
2 
3 /*
4  * SPDX-License-Identifier: MIT
5  *
6  * Copyright © 2016 Intel Corporation
7  */
8 
9 #ifndef I915_SCATTERLIST_H
10 #define I915_SCATTERLIST_H
11 
12 #include <linux/pfn.h>
13 #include <linux/scatterlist.h>
14 #include <linux/swiotlb.h>
15 
16 #include "i915_gem.h"
17 
18 #ifdef __NetBSD__
19 
20 struct sgt_iter {
21 	unsigned i;
22 };
23 
24 #define	for_each_sgt_page(pp, iter, sgt)				      \
25 	for ((iter).i = 0;						      \
26 	     ((iter).i < (sgt)->sgl->sg_npgs				      \
27 		 ? (((pp) = (sgt)->sgl->sg_pgs[(iter).i]), 1)		      \
28 		 : 0);							      \
29 	     (iter).i++)
30 
31 static inline unsigned
i915_sg_page_sizes(struct scatterlist * sg)32 i915_sg_page_sizes(struct scatterlist *sg)
33 {
34 	unsigned i, page_sizes = 0;
35 
36 	for (i = 0; i < sg->sg_dmamap->dm_nsegs; i++)
37 		page_sizes |= sg->sg_dmamap->dm_segs[i].ds_len;
38 
39 	return page_sizes;
40 }
41 
42 static inline unsigned
i915_sg_segment_size(void)43 i915_sg_segment_size(void)
44 {
45 	return PAGE_SIZE;
46 }
47 
48 #else
49 
50 /*
51  * Optimised SGL iterator for GEM objects
52  */
53 static __always_inline struct sgt_iter {
54 	struct scatterlist *sgp;
55 	union {
56 		unsigned long pfn;
57 		dma_addr_t dma;
58 	};
59 	unsigned int curr;
60 	unsigned int max;
__sgt_iter(struct scatterlist * sgl,bool dma)61 } __sgt_iter(struct scatterlist *sgl, bool dma) {
62 	struct sgt_iter s = { .sgp = sgl };
63 
64 	if (s.sgp) {
65 		s.max = s.curr = s.sgp->offset;
66 		s.max += s.sgp->length;
67 		if (dma)
68 			s.dma = sg_dma_address(s.sgp);
69 		else
70 			s.pfn = page_to_pfn(sg_page(s.sgp));
71 	}
72 
73 	return s;
74 }
75 
__sg_page_count(const struct scatterlist * sg)76 static inline int __sg_page_count(const struct scatterlist *sg)
77 {
78 	return sg->length >> PAGE_SHIFT;
79 }
80 
____sg_next(struct scatterlist * sg)81 static inline struct scatterlist *____sg_next(struct scatterlist *sg)
82 {
83 	++sg;
84 	if (unlikely(sg_is_chain(sg)))
85 		sg = sg_chain_ptr(sg);
86 	return sg;
87 }
88 
89 /**
90  * __sg_next - return the next scatterlist entry in a list
91  * @sg:		The current sg entry
92  *
93  * Description:
94  *   If the entry is the last, return NULL; otherwise, step to the next
95  *   element in the array (@sg@+1). If that's a chain pointer, follow it;
96  *   otherwise just return the pointer to the current element.
97  **/
__sg_next(struct scatterlist * sg)98 static inline struct scatterlist *__sg_next(struct scatterlist *sg)
99 {
100 	return sg_is_last(sg) ? NULL : ____sg_next(sg);
101 }
102 
103 /**
104  * __for_each_sgt_daddr - iterate over the device addresses of the given sg_table
105  * @__dp:	Device address (output)
106  * @__iter:	'struct sgt_iter' (iterator state, internal)
107  * @__sgt:	sg_table to iterate over (input)
108  * @__step:	step size
109  */
110 #define __for_each_sgt_daddr(__dp, __iter, __sgt, __step)		\
111 	for ((__iter) = __sgt_iter((__sgt)->sgl, true);			\
112 	     ((__dp) = (__iter).dma + (__iter).curr), (__iter).sgp;	\
113 	     (((__iter).curr += (__step)) >= (__iter).max) ?		\
114 	     (__iter) = __sgt_iter(__sg_next((__iter).sgp), true), 0 : 0)
115 
116 /**
117  * for_each_sgt_page - iterate over the pages of the given sg_table
118  * @__pp:	page pointer (output)
119  * @__iter:	'struct sgt_iter' (iterator state, internal)
120  * @__sgt:	sg_table to iterate over (input)
121  */
122 #define for_each_sgt_page(__pp, __iter, __sgt)				\
123 	for ((__iter) = __sgt_iter((__sgt)->sgl, false);		\
124 	     ((__pp) = (__iter).pfn == 0 ? NULL :			\
125 	      pfn_to_page((__iter).pfn + ((__iter).curr >> PAGE_SHIFT))); \
126 	     (((__iter).curr += PAGE_SIZE) >= (__iter).max) ?		\
127 	     (__iter) = __sgt_iter(__sg_next((__iter).sgp), false), 0 : 0)
128 
i915_sg_page_sizes(struct scatterlist * sg)129 static inline unsigned int i915_sg_page_sizes(struct scatterlist *sg)
130 {
131 	unsigned int page_sizes;
132 
133 	page_sizes = 0;
134 	while (sg) {
135 		GEM_BUG_ON(sg->offset);
136 		GEM_BUG_ON(!IS_ALIGNED(sg->length, PAGE_SIZE));
137 		page_sizes |= sg->length;
138 		sg = __sg_next(sg);
139 	}
140 
141 	return page_sizes;
142 }
143 
i915_sg_segment_size(void)144 static inline unsigned int i915_sg_segment_size(void)
145 {
146 	unsigned int size = swiotlb_max_segment();
147 
148 	if (size == 0)
149 		return SCATTERLIST_MAX_SEGMENT;
150 
151 	size = rounddown(size, PAGE_SIZE);
152 	/* swiotlb_max_segment_size can return 1 byte when it means one page. */
153 	if (size < PAGE_SIZE)
154 		size = PAGE_SIZE;
155 
156 	return size;
157 }
158 
159 #endif
160 
161 bool i915_sg_trim(struct sg_table *orig_st);
162 
163 #endif
164