Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * SPDX-License-Identifier: MIT
0003  *
0004  * Copyright © 2016 Intel Corporation
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  * Optimised SGL iterator for GEM objects
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  * __sg_next - return the next scatterlist entry in a list
0069  * @sg:     The current sg entry
0070  *
0071  * Description:
0072  *   If the entry is the last, return NULL; otherwise, step to the next
0073  *   element in the array (@sg@+1). If that's a chain pointer, follow it;
0074  *   otherwise just return the pointer to the current element.
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  * __for_each_sgt_daddr - iterate over the device addresses of the given sg_table
0083  * @__dp:   Device address (output)
0084  * @__iter: 'struct sgt_iter' (iterator state, internal)
0085  * @__sgt:  sg_table to iterate over (input)
0086  * @__step: step size
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  * for_each_sgt_page - iterate over the pages of the given sg_table
0096  * @__pp:   page pointer (output)
0097  * @__iter: 'struct sgt_iter' (iterator state, internal)
0098  * @__sgt:  sg_table to iterate over (input)
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  * i915_sg_dma_sizes - Record the dma segment sizes of a scatterlist
0109  * @sg: The scatterlist
0110  *
0111  * Return: An unsigned int with segment sizes logically or'ed together.
0112  * A caller can use this information to determine what hardware page table
0113  * entry sizes can be used to map the memory represented by the scatterlist.
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     /* swiotlb_max_segment_size can return 1 byte when it means one page. */
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  * struct i915_refct_sgt_ops - Operations structure for struct i915_refct_sgt
0149  */
0150 struct i915_refct_sgt_ops {
0151     /**
0152      * release() - Free the memory of the struct i915_refct_sgt
0153      * @ref: struct kref that is embedded in the struct i915_refct_sgt
0154      */
0155     void (*release)(struct kref *ref);
0156 };
0157 
0158 /**
0159  * struct i915_refct_sgt - A refcounted scatter-gather table
0160  * @kref: struct kref for refcounting
0161  * @table: struct sg_table holding the scatter-gather table itself. Note that
0162  * @table->sgl = NULL can be used to determine whether a scatter-gather table
0163  * is present or not.
0164  * @size: The size in bytes of the underlying memory buffer
0165  * @ops: The operations structure.
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  * i915_refct_sgt_put - Put a refcounted sg-table
0176  * @rsgt the struct i915_refct_sgt to put.
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  * i915_refct_sgt_get - Get a refcounted sg-table
0186  * @rsgt the struct i915_refct_sgt to get.
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  * __i915_refct_sgt_init - Initialize a refcounted sg-list with a custom
0197  * operations structure
0198  * @rsgt The struct i915_refct_sgt to initialize.
0199  * @size: Size in bytes of the underlying memory buffer.
0200  * @ops: A customized operations structure in case the refcounted sg-list
0201  * is embedded into another structure.
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