0001
0002
0003
0004
0005
0006
0007 #ifndef I915_SCATTERLIST_H
0008 #define I915_SCATTERLIST_H
0009
0010 #include <linux/pfn.h>
0011 #include <linux/scatterlist.h>
0012 #include <linux/swiotlb.h>
0013
0014 #include "i915_gem.h"
0015
0016 struct drm_mm_node;
0017 struct ttm_resource;
0018
0019
0020
0021
0022 static __always_inline struct sgt_iter {
0023 struct scatterlist *sgp;
0024 union {
0025 unsigned long pfn;
0026 dma_addr_t dma;
0027 };
0028 unsigned int curr;
0029 unsigned int max;
0030 } __sgt_iter(struct scatterlist *sgl, bool dma) {
0031 struct sgt_iter s = { .sgp = sgl };
0032
0033 if (dma && s.sgp && sg_dma_len(s.sgp) == 0) {
0034 s.sgp = NULL;
0035 } else if (s.sgp) {
0036 s.max = s.curr = s.sgp->offset;
0037 if (dma) {
0038 s.dma = sg_dma_address(s.sgp);
0039 s.max += sg_dma_len(s.sgp);
0040 } else {
0041 s.pfn = page_to_pfn(sg_page(s.sgp));
0042 s.max += s.sgp->length;
0043 }
0044 }
0045
0046 return s;
0047 }
0048
0049 static inline int __sg_page_count(const struct scatterlist *sg)
0050 {
0051 return sg->length >> PAGE_SHIFT;
0052 }
0053
0054 static inline int __sg_dma_page_count(const struct scatterlist *sg)
0055 {
0056 return sg_dma_len(sg) >> PAGE_SHIFT;
0057 }
0058
0059 static inline struct scatterlist *____sg_next(struct scatterlist *sg)
0060 {
0061 ++sg;
0062 if (unlikely(sg_is_chain(sg)))
0063 sg = sg_chain_ptr(sg);
0064 return sg;
0065 }
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 static inline struct scatterlist *__sg_next(struct scatterlist *sg)
0077 {
0078 return sg_is_last(sg) ? NULL : ____sg_next(sg);
0079 }
0080
0081
0082
0083
0084
0085
0086
0087
0088 #define __for_each_sgt_daddr(__dp, __iter, __sgt, __step) \
0089 for ((__iter) = __sgt_iter((__sgt)->sgl, true); \
0090 ((__dp) = (__iter).dma + (__iter).curr), (__iter).sgp; \
0091 (((__iter).curr += (__step)) >= (__iter).max) ? \
0092 (__iter) = __sgt_iter(__sg_next((__iter).sgp), true), 0 : 0)
0093
0094
0095
0096
0097
0098
0099
0100 #define for_each_sgt_page(__pp, __iter, __sgt) \
0101 for ((__iter) = __sgt_iter((__sgt)->sgl, false); \
0102 ((__pp) = (__iter).pfn == 0 ? NULL : \
0103 pfn_to_page((__iter).pfn + ((__iter).curr >> PAGE_SHIFT))); \
0104 (((__iter).curr += PAGE_SIZE) >= (__iter).max) ? \
0105 (__iter) = __sgt_iter(__sg_next((__iter).sgp), false), 0 : 0)
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 static inline unsigned int i915_sg_dma_sizes(struct scatterlist *sg)
0116 {
0117 unsigned int page_sizes;
0118
0119 page_sizes = 0;
0120 while (sg && sg_dma_len(sg)) {
0121 GEM_BUG_ON(sg->offset);
0122 GEM_BUG_ON(!IS_ALIGNED(sg_dma_len(sg), PAGE_SIZE));
0123 page_sizes |= sg_dma_len(sg);
0124 sg = __sg_next(sg);
0125 }
0126
0127 return page_sizes;
0128 }
0129
0130 static inline unsigned int i915_sg_segment_size(void)
0131 {
0132 unsigned int size = swiotlb_max_segment();
0133
0134 if (size == 0)
0135 size = UINT_MAX;
0136
0137 size = rounddown(size, PAGE_SIZE);
0138
0139 if (size < PAGE_SIZE)
0140 size = PAGE_SIZE;
0141
0142 return size;
0143 }
0144
0145 bool i915_sg_trim(struct sg_table *orig_st);
0146
0147
0148
0149
0150 struct i915_refct_sgt_ops {
0151
0152
0153
0154
0155 void (*release)(struct kref *ref);
0156 };
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 struct i915_refct_sgt {
0168 struct kref kref;
0169 struct sg_table table;
0170 size_t size;
0171 const struct i915_refct_sgt_ops *ops;
0172 };
0173
0174
0175
0176
0177
0178 static inline void i915_refct_sgt_put(struct i915_refct_sgt *rsgt)
0179 {
0180 if (rsgt)
0181 kref_put(&rsgt->kref, rsgt->ops->release);
0182 }
0183
0184
0185
0186
0187
0188 static inline struct i915_refct_sgt *
0189 i915_refct_sgt_get(struct i915_refct_sgt *rsgt)
0190 {
0191 kref_get(&rsgt->kref);
0192 return rsgt;
0193 }
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203 static inline void __i915_refct_sgt_init(struct i915_refct_sgt *rsgt,
0204 size_t size,
0205 const struct i915_refct_sgt_ops *ops)
0206 {
0207 kref_init(&rsgt->kref);
0208 rsgt->table.sgl = NULL;
0209 rsgt->size = size;
0210 rsgt->ops = ops;
0211 }
0212
0213 void i915_refct_sgt_init(struct i915_refct_sgt *rsgt, size_t size);
0214
0215 struct i915_refct_sgt *i915_rsgt_from_mm_node(const struct drm_mm_node *node,
0216 u64 region_start,
0217 u32 page_alignment);
0218
0219 struct i915_refct_sgt *i915_rsgt_from_buddy_resource(struct ttm_resource *res,
0220 u64 region_start,
0221 u32 page_alignment);
0222
0223 #endif