Lines Matching refs:bitmap
14 * struct iova_bitmap_map - A bitmap representing an IOVA range
16 * Main data structure for tracking mapped user pages of bitmap data.
24 * The user does not need to exact location of the bits in the bitmap.
26 * records the IOVA *range* in the bitmap by setting the corresponding
29 * The bitmap is an array of u64 whereas each bit represents an IOVA of
30 * range of (1 << pgshift). Thus formula for the bitmap data to be set is:
47 /* pinned pages representing the bitmap data */
52 * struct iova_bitmap - The IOVA bitmap object
54 * Main data structure for iterating over the bitmap data.
57 * It uses a windowing scheme and pins the bitmap in relatively
60 * The bitmap object uses one base page to store all the pinned pages
61 * pointers related to the bitmap. For sizeof(struct page*) == 8 it stores
63 * 2M of bitmap data is pinned at a time. If the iova_bitmap page size is
72 * An example of the APIs on how to use/iterate over the IOVA bitmap:
74 * bitmap = iova_bitmap_alloc(iova, length, page_size, data);
75 * if (IS_ERR(bitmap))
76 * return PTR_ERR(bitmap);
78 * ret = iova_bitmap_for_each(bitmap, arg, dirty_reporter_fn);
80 * iova_bitmap_free(bitmap);
87 * iova_bitmap_set(bitmap, iova, iova_length);
90 * which u64 word of the bitmap is mapped, up to @mapped_total_index.
94 * The IOVA bitmap is usually located on what tracks DMA mapped ranges or
96 * bitmap.
99 /* IOVA range representing the currently mapped bitmap data */
102 /* userspace address of the bitmap */
103 u8 __user *bitmap;
111 /* base IOVA of the whole bitmap */
114 /* length of the IOVA range for the whole bitmap */
119 * Converts a relative IOVA to a bitmap index.
120 * This function provides the index into the u64 array (bitmap::bitmap)
122 * Relative IOVA means relative to the bitmap::mapped base IOVA
127 static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
130 unsigned long pgsize = 1 << bitmap->mapped.pgshift;
132 return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
136 * Converts a bitmap index to a *relative* IOVA.
138 static unsigned long iova_bitmap_index_to_offset(struct iova_bitmap *bitmap,
141 unsigned long pgshift = bitmap->mapped.pgshift;
143 return (index * BITS_PER_TYPE(*bitmap->bitmap)) << pgshift;
149 static unsigned long iova_bitmap_mapped_iova(struct iova_bitmap *bitmap)
151 unsigned long skip = bitmap->mapped_base_index;
153 return bitmap->iova + iova_bitmap_index_to_offset(bitmap, skip);
157 * Pins the bitmap user pages for the current range window.
158 * This is internal to IOVA bitmap and called when advancing the
159 * index (@mapped_base_index) or allocating the bitmap.
161 static int iova_bitmap_get(struct iova_bitmap *bitmap)
163 struct iova_bitmap_map *mapped = &bitmap->mapped;
174 npages = DIV_ROUND_UP((bitmap->mapped_total_index -
175 bitmap->mapped_base_index) *
176 sizeof(*bitmap->bitmap), PAGE_SIZE);
180 * with bitmap u64 word index.
182 addr = bitmap->bitmap + bitmap->mapped_base_index;
186 * This is, for example, on x86 means 2M of bitmap data max.
198 mapped->iova = iova_bitmap_mapped_iova(bitmap);
202 * This handles the case where the bitmap is not PAGE_SIZE
210 * Unpins the bitmap user pages and clears @npages
212 * the index or freeing the bitmap.
214 static void iova_bitmap_put(struct iova_bitmap *bitmap)
216 struct iova_bitmap_map *mapped = &bitmap->mapped;
225 * iova_bitmap_alloc() - Allocates an IOVA bitmap object
228 * @page_size: Page size of the IOVA bitmap. It defines what each bit
230 * @data: Userspace address of the bitmap
242 struct iova_bitmap *bitmap;
245 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
246 if (!bitmap)
249 mapped = &bitmap->mapped;
251 bitmap->bitmap = (u8 __user *)data;
252 bitmap->mapped_total_index =
253 iova_bitmap_offset_to_index(bitmap, length - 1) + 1;
254 bitmap->iova = iova;
255 bitmap->length = length;
263 rc = iova_bitmap_get(bitmap);
266 return bitmap;
269 iova_bitmap_free(bitmap);
274 * iova_bitmap_free() - Frees an IOVA bitmap object
275 * @bitmap: IOVA bitmap to free
280 void iova_bitmap_free(struct iova_bitmap *bitmap)
282 struct iova_bitmap_map *mapped = &bitmap->mapped;
284 iova_bitmap_put(bitmap);
291 kfree(bitmap);
295 * Returns the remaining bitmap indexes from mapped_total_index to process for
296 * the currently pinned bitmap pages.
298 static unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap)
302 bytes = (bitmap->mapped.npages << PAGE_SHIFT) - bitmap->mapped.pgoff;
304 remaining = bitmap->mapped_total_index - bitmap->mapped_base_index;
306 DIV_ROUND_UP(bytes, sizeof(*bitmap->bitmap)));
314 static unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap)
316 unsigned long max_iova = bitmap->iova + bitmap->length - 1;
317 unsigned long iova = iova_bitmap_mapped_iova(bitmap);
322 * when converted to IOVA gives us a max length that the bitmap
324 * only cover the IOVA range in @bitmap::iova .. @bitmap::length.
326 remaining = iova_bitmap_index_to_offset(bitmap,
327 iova_bitmap_mapped_remaining(bitmap));
338 static bool iova_bitmap_done(struct iova_bitmap *bitmap)
340 return bitmap->mapped_base_index >= bitmap->mapped_total_index;
345 * pages and pins the next set of bitmap pages.
348 static int iova_bitmap_advance(struct iova_bitmap *bitmap)
350 unsigned long iova = iova_bitmap_mapped_length(bitmap) - 1;
351 unsigned long count = iova_bitmap_offset_to_index(bitmap, iova) + 1;
353 bitmap->mapped_base_index += count;
355 iova_bitmap_put(bitmap);
356 if (iova_bitmap_done(bitmap))
359 /* When advancing the index we pin the next set of bitmap pages */
360 return iova_bitmap_get(bitmap);
364 * iova_bitmap_for_each() - Iterates over the bitmap
365 * @bitmap: IOVA bitmap to iterate
369 * Helper function to iterate over bitmap data representing a portion of IOVA
371 * mapped bitmap user pages into IOVA ranges to process.
376 int iova_bitmap_for_each(struct iova_bitmap *bitmap, void *opaque,
381 for (; !iova_bitmap_done(bitmap) && !ret;
382 ret = iova_bitmap_advance(bitmap)) {
383 ret = fn(bitmap, iova_bitmap_mapped_iova(bitmap),
384 iova_bitmap_mapped_length(bitmap), opaque);
393 * iova_bitmap_set() - Records an IOVA range in bitmap
394 * @bitmap: IOVA bitmap
399 * the user bitmap.
402 void iova_bitmap_set(struct iova_bitmap *bitmap,
405 struct iova_bitmap_map *mapped = &bitmap->mapped;