162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2022, Oracle and/or its affiliates. 462306a36Sopenharmony_ci * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#include <linux/iova_bitmap.h> 762306a36Sopenharmony_ci#include <linux/mm.h> 862306a36Sopenharmony_ci#include <linux/slab.h> 962306a36Sopenharmony_ci#include <linux/highmem.h> 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#define BITS_PER_PAGE (PAGE_SIZE * BITS_PER_BYTE) 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* 1462306a36Sopenharmony_ci * struct iova_bitmap_map - A bitmap representing an IOVA range 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * Main data structure for tracking mapped user pages of bitmap data. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * For example, for something recording dirty IOVAs, it will be provided a 1962306a36Sopenharmony_ci * struct iova_bitmap structure, as a general structure for iterating the 2062306a36Sopenharmony_ci * total IOVA range. The struct iova_bitmap_map, though, represents the 2162306a36Sopenharmony_ci * subset of said IOVA space that is pinned by its parent structure (struct 2262306a36Sopenharmony_ci * iova_bitmap). 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * The user does not need to exact location of the bits in the bitmap. 2562306a36Sopenharmony_ci * From user perspective the only API available is iova_bitmap_set() which 2662306a36Sopenharmony_ci * records the IOVA *range* in the bitmap by setting the corresponding 2762306a36Sopenharmony_ci * bits. 2862306a36Sopenharmony_ci * 2962306a36Sopenharmony_ci * The bitmap is an array of u64 whereas each bit represents an IOVA of 3062306a36Sopenharmony_ci * range of (1 << pgshift). Thus formula for the bitmap data to be set is: 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * data[(iova / page_size) / 64] & (1ULL << (iova % 64)) 3362306a36Sopenharmony_ci */ 3462306a36Sopenharmony_cistruct iova_bitmap_map { 3562306a36Sopenharmony_ci /* base IOVA representing bit 0 of the first page */ 3662306a36Sopenharmony_ci unsigned long iova; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci /* page size order that each bit granules to */ 3962306a36Sopenharmony_ci unsigned long pgshift; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci /* page offset of the first user page pinned */ 4262306a36Sopenharmony_ci unsigned long pgoff; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci /* number of pages pinned */ 4562306a36Sopenharmony_ci unsigned long npages; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci /* pinned pages representing the bitmap data */ 4862306a36Sopenharmony_ci struct page **pages; 4962306a36Sopenharmony_ci}; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci/* 5262306a36Sopenharmony_ci * struct iova_bitmap - The IOVA bitmap object 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * Main data structure for iterating over the bitmap data. 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * Abstracts the pinning work and iterates in IOVA ranges. 5762306a36Sopenharmony_ci * It uses a windowing scheme and pins the bitmap in relatively 5862306a36Sopenharmony_ci * big ranges e.g. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * The bitmap object uses one base page to store all the pinned pages 6162306a36Sopenharmony_ci * pointers related to the bitmap. For sizeof(struct page*) == 8 it stores 6262306a36Sopenharmony_ci * 512 struct page pointers which, if the base page size is 4K, it means 6362306a36Sopenharmony_ci * 2M of bitmap data is pinned at a time. If the iova_bitmap page size is 6462306a36Sopenharmony_ci * also 4K then the range window to iterate is 64G. 6562306a36Sopenharmony_ci * 6662306a36Sopenharmony_ci * For example iterating on a total IOVA range of 4G..128G, it will walk 6762306a36Sopenharmony_ci * through this set of ranges: 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * 4G - 68G-1 (64G) 7062306a36Sopenharmony_ci * 68G - 128G-1 (64G) 7162306a36Sopenharmony_ci * 7262306a36Sopenharmony_ci * An example of the APIs on how to use/iterate over the IOVA bitmap: 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * bitmap = iova_bitmap_alloc(iova, length, page_size, data); 7562306a36Sopenharmony_ci * if (IS_ERR(bitmap)) 7662306a36Sopenharmony_ci * return PTR_ERR(bitmap); 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * ret = iova_bitmap_for_each(bitmap, arg, dirty_reporter_fn); 7962306a36Sopenharmony_ci * 8062306a36Sopenharmony_ci * iova_bitmap_free(bitmap); 8162306a36Sopenharmony_ci * 8262306a36Sopenharmony_ci * Each iteration of the @dirty_reporter_fn is called with a unique @iova 8362306a36Sopenharmony_ci * and @length argument, indicating the current range available through the 8462306a36Sopenharmony_ci * iova_bitmap. The @dirty_reporter_fn uses iova_bitmap_set() to mark dirty 8562306a36Sopenharmony_ci * areas (@iova_length) within that provided range, as following: 8662306a36Sopenharmony_ci * 8762306a36Sopenharmony_ci * iova_bitmap_set(bitmap, iova, iova_length); 8862306a36Sopenharmony_ci * 8962306a36Sopenharmony_ci * The internals of the object uses an index @mapped_base_index that indexes 9062306a36Sopenharmony_ci * which u64 word of the bitmap is mapped, up to @mapped_total_index. 9162306a36Sopenharmony_ci * Those keep being incremented until @mapped_total_index is reached while 9262306a36Sopenharmony_ci * mapping up to PAGE_SIZE / sizeof(struct page*) maximum of pages. 9362306a36Sopenharmony_ci * 9462306a36Sopenharmony_ci * The IOVA bitmap is usually located on what tracks DMA mapped ranges or 9562306a36Sopenharmony_ci * some form of IOVA range tracking that co-relates to the user passed 9662306a36Sopenharmony_ci * bitmap. 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_cistruct iova_bitmap { 9962306a36Sopenharmony_ci /* IOVA range representing the currently mapped bitmap data */ 10062306a36Sopenharmony_ci struct iova_bitmap_map mapped; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci /* userspace address of the bitmap */ 10362306a36Sopenharmony_ci u8 __user *bitmap; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci /* u64 index that @mapped points to */ 10662306a36Sopenharmony_ci unsigned long mapped_base_index; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci /* how many u64 can we walk in total */ 10962306a36Sopenharmony_ci unsigned long mapped_total_index; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci /* base IOVA of the whole bitmap */ 11262306a36Sopenharmony_ci unsigned long iova; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci /* length of the IOVA range for the whole bitmap */ 11562306a36Sopenharmony_ci size_t length; 11662306a36Sopenharmony_ci}; 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci/* 11962306a36Sopenharmony_ci * Converts a relative IOVA to a bitmap index. 12062306a36Sopenharmony_ci * This function provides the index into the u64 array (bitmap::bitmap) 12162306a36Sopenharmony_ci * for a given IOVA offset. 12262306a36Sopenharmony_ci * Relative IOVA means relative to the bitmap::mapped base IOVA 12362306a36Sopenharmony_ci * (stored in mapped::iova). All computations in this file are done using 12462306a36Sopenharmony_ci * relative IOVAs and thus avoid an extra subtraction against mapped::iova. 12562306a36Sopenharmony_ci * The user API iova_bitmap_set() always uses a regular absolute IOVAs. 12662306a36Sopenharmony_ci */ 12762306a36Sopenharmony_cistatic unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap, 12862306a36Sopenharmony_ci unsigned long iova) 12962306a36Sopenharmony_ci{ 13062306a36Sopenharmony_ci unsigned long pgsize = 1 << bitmap->mapped.pgshift; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize); 13362306a36Sopenharmony_ci} 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci/* 13662306a36Sopenharmony_ci * Converts a bitmap index to a *relative* IOVA. 13762306a36Sopenharmony_ci */ 13862306a36Sopenharmony_cistatic unsigned long iova_bitmap_index_to_offset(struct iova_bitmap *bitmap, 13962306a36Sopenharmony_ci unsigned long index) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci unsigned long pgshift = bitmap->mapped.pgshift; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci return (index * BITS_PER_TYPE(*bitmap->bitmap)) << pgshift; 14462306a36Sopenharmony_ci} 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci/* 14762306a36Sopenharmony_ci * Returns the base IOVA of the mapped range. 14862306a36Sopenharmony_ci */ 14962306a36Sopenharmony_cistatic unsigned long iova_bitmap_mapped_iova(struct iova_bitmap *bitmap) 15062306a36Sopenharmony_ci{ 15162306a36Sopenharmony_ci unsigned long skip = bitmap->mapped_base_index; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci return bitmap->iova + iova_bitmap_index_to_offset(bitmap, skip); 15462306a36Sopenharmony_ci} 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci/* 15762306a36Sopenharmony_ci * Pins the bitmap user pages for the current range window. 15862306a36Sopenharmony_ci * This is internal to IOVA bitmap and called when advancing the 15962306a36Sopenharmony_ci * index (@mapped_base_index) or allocating the bitmap. 16062306a36Sopenharmony_ci */ 16162306a36Sopenharmony_cistatic int iova_bitmap_get(struct iova_bitmap *bitmap) 16262306a36Sopenharmony_ci{ 16362306a36Sopenharmony_ci struct iova_bitmap_map *mapped = &bitmap->mapped; 16462306a36Sopenharmony_ci unsigned long npages; 16562306a36Sopenharmony_ci u8 __user *addr; 16662306a36Sopenharmony_ci long ret; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * @mapped_base_index is the index of the currently mapped u64 words 17062306a36Sopenharmony_ci * that we have access. Anything before @mapped_base_index is not 17162306a36Sopenharmony_ci * mapped. The range @mapped_base_index .. @mapped_total_index-1 is 17262306a36Sopenharmony_ci * mapped but capped at a maximum number of pages. 17362306a36Sopenharmony_ci */ 17462306a36Sopenharmony_ci npages = DIV_ROUND_UP((bitmap->mapped_total_index - 17562306a36Sopenharmony_ci bitmap->mapped_base_index) * 17662306a36Sopenharmony_ci sizeof(*bitmap->bitmap), PAGE_SIZE); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci /* 17962306a36Sopenharmony_ci * Bitmap address to be pinned is calculated via pointer arithmetic 18062306a36Sopenharmony_ci * with bitmap u64 word index. 18162306a36Sopenharmony_ci */ 18262306a36Sopenharmony_ci addr = bitmap->bitmap + bitmap->mapped_base_index; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci /* 18562306a36Sopenharmony_ci * We always cap at max number of 'struct page' a base page can fit. 18662306a36Sopenharmony_ci * This is, for example, on x86 means 2M of bitmap data max. 18762306a36Sopenharmony_ci */ 18862306a36Sopenharmony_ci npages = min(npages + !!offset_in_page(addr), 18962306a36Sopenharmony_ci PAGE_SIZE / sizeof(struct page *)); 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci ret = pin_user_pages_fast((unsigned long)addr, npages, 19262306a36Sopenharmony_ci FOLL_WRITE, mapped->pages); 19362306a36Sopenharmony_ci if (ret <= 0) 19462306a36Sopenharmony_ci return -EFAULT; 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci mapped->npages = (unsigned long)ret; 19762306a36Sopenharmony_ci /* Base IOVA where @pages point to i.e. bit 0 of the first page */ 19862306a36Sopenharmony_ci mapped->iova = iova_bitmap_mapped_iova(bitmap); 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci /* 20162306a36Sopenharmony_ci * offset of the page where pinned pages bit 0 is located. 20262306a36Sopenharmony_ci * This handles the case where the bitmap is not PAGE_SIZE 20362306a36Sopenharmony_ci * aligned. 20462306a36Sopenharmony_ci */ 20562306a36Sopenharmony_ci mapped->pgoff = offset_in_page(addr); 20662306a36Sopenharmony_ci return 0; 20762306a36Sopenharmony_ci} 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci/* 21062306a36Sopenharmony_ci * Unpins the bitmap user pages and clears @npages 21162306a36Sopenharmony_ci * (un)pinning is abstracted from API user and it's done when advancing 21262306a36Sopenharmony_ci * the index or freeing the bitmap. 21362306a36Sopenharmony_ci */ 21462306a36Sopenharmony_cistatic void iova_bitmap_put(struct iova_bitmap *bitmap) 21562306a36Sopenharmony_ci{ 21662306a36Sopenharmony_ci struct iova_bitmap_map *mapped = &bitmap->mapped; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci if (mapped->npages) { 21962306a36Sopenharmony_ci unpin_user_pages(mapped->pages, mapped->npages); 22062306a36Sopenharmony_ci mapped->npages = 0; 22162306a36Sopenharmony_ci } 22262306a36Sopenharmony_ci} 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci/** 22562306a36Sopenharmony_ci * iova_bitmap_alloc() - Allocates an IOVA bitmap object 22662306a36Sopenharmony_ci * @iova: Start address of the IOVA range 22762306a36Sopenharmony_ci * @length: Length of the IOVA range 22862306a36Sopenharmony_ci * @page_size: Page size of the IOVA bitmap. It defines what each bit 22962306a36Sopenharmony_ci * granularity represents 23062306a36Sopenharmony_ci * @data: Userspace address of the bitmap 23162306a36Sopenharmony_ci * 23262306a36Sopenharmony_ci * Allocates an IOVA object and initializes all its fields including the 23362306a36Sopenharmony_ci * first user pages of @data. 23462306a36Sopenharmony_ci * 23562306a36Sopenharmony_ci * Return: A pointer to a newly allocated struct iova_bitmap 23662306a36Sopenharmony_ci * or ERR_PTR() on error. 23762306a36Sopenharmony_ci */ 23862306a36Sopenharmony_cistruct iova_bitmap *iova_bitmap_alloc(unsigned long iova, size_t length, 23962306a36Sopenharmony_ci unsigned long page_size, u64 __user *data) 24062306a36Sopenharmony_ci{ 24162306a36Sopenharmony_ci struct iova_bitmap_map *mapped; 24262306a36Sopenharmony_ci struct iova_bitmap *bitmap; 24362306a36Sopenharmony_ci int rc; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); 24662306a36Sopenharmony_ci if (!bitmap) 24762306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci mapped = &bitmap->mapped; 25062306a36Sopenharmony_ci mapped->pgshift = __ffs(page_size); 25162306a36Sopenharmony_ci bitmap->bitmap = (u8 __user *)data; 25262306a36Sopenharmony_ci bitmap->mapped_total_index = 25362306a36Sopenharmony_ci iova_bitmap_offset_to_index(bitmap, length - 1) + 1; 25462306a36Sopenharmony_ci bitmap->iova = iova; 25562306a36Sopenharmony_ci bitmap->length = length; 25662306a36Sopenharmony_ci mapped->iova = iova; 25762306a36Sopenharmony_ci mapped->pages = (struct page **)__get_free_page(GFP_KERNEL); 25862306a36Sopenharmony_ci if (!mapped->pages) { 25962306a36Sopenharmony_ci rc = -ENOMEM; 26062306a36Sopenharmony_ci goto err; 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci rc = iova_bitmap_get(bitmap); 26462306a36Sopenharmony_ci if (rc) 26562306a36Sopenharmony_ci goto err; 26662306a36Sopenharmony_ci return bitmap; 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cierr: 26962306a36Sopenharmony_ci iova_bitmap_free(bitmap); 27062306a36Sopenharmony_ci return ERR_PTR(rc); 27162306a36Sopenharmony_ci} 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci/** 27462306a36Sopenharmony_ci * iova_bitmap_free() - Frees an IOVA bitmap object 27562306a36Sopenharmony_ci * @bitmap: IOVA bitmap to free 27662306a36Sopenharmony_ci * 27762306a36Sopenharmony_ci * It unpins and releases pages array memory and clears any leftover 27862306a36Sopenharmony_ci * state. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_civoid iova_bitmap_free(struct iova_bitmap *bitmap) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci struct iova_bitmap_map *mapped = &bitmap->mapped; 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci iova_bitmap_put(bitmap); 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci if (mapped->pages) { 28762306a36Sopenharmony_ci free_page((unsigned long)mapped->pages); 28862306a36Sopenharmony_ci mapped->pages = NULL; 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci kfree(bitmap); 29262306a36Sopenharmony_ci} 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci/* 29562306a36Sopenharmony_ci * Returns the remaining bitmap indexes from mapped_total_index to process for 29662306a36Sopenharmony_ci * the currently pinned bitmap pages. 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_cistatic unsigned long iova_bitmap_mapped_remaining(struct iova_bitmap *bitmap) 29962306a36Sopenharmony_ci{ 30062306a36Sopenharmony_ci unsigned long remaining, bytes; 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci bytes = (bitmap->mapped.npages << PAGE_SHIFT) - bitmap->mapped.pgoff; 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ci remaining = bitmap->mapped_total_index - bitmap->mapped_base_index; 30562306a36Sopenharmony_ci remaining = min_t(unsigned long, remaining, 30662306a36Sopenharmony_ci DIV_ROUND_UP(bytes, sizeof(*bitmap->bitmap))); 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci return remaining; 30962306a36Sopenharmony_ci} 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci/* 31262306a36Sopenharmony_ci * Returns the length of the mapped IOVA range. 31362306a36Sopenharmony_ci */ 31462306a36Sopenharmony_cistatic unsigned long iova_bitmap_mapped_length(struct iova_bitmap *bitmap) 31562306a36Sopenharmony_ci{ 31662306a36Sopenharmony_ci unsigned long max_iova = bitmap->iova + bitmap->length - 1; 31762306a36Sopenharmony_ci unsigned long iova = iova_bitmap_mapped_iova(bitmap); 31862306a36Sopenharmony_ci unsigned long remaining; 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci /* 32162306a36Sopenharmony_ci * iova_bitmap_mapped_remaining() returns a number of indexes which 32262306a36Sopenharmony_ci * when converted to IOVA gives us a max length that the bitmap 32362306a36Sopenharmony_ci * pinned data can cover. Afterwards, that is capped to 32462306a36Sopenharmony_ci * only cover the IOVA range in @bitmap::iova .. @bitmap::length. 32562306a36Sopenharmony_ci */ 32662306a36Sopenharmony_ci remaining = iova_bitmap_index_to_offset(bitmap, 32762306a36Sopenharmony_ci iova_bitmap_mapped_remaining(bitmap)); 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci if (iova + remaining - 1 > max_iova) 33062306a36Sopenharmony_ci remaining -= ((iova + remaining - 1) - max_iova); 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci return remaining; 33362306a36Sopenharmony_ci} 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci/* 33662306a36Sopenharmony_ci * Returns true if there's not more data to iterate. 33762306a36Sopenharmony_ci */ 33862306a36Sopenharmony_cistatic bool iova_bitmap_done(struct iova_bitmap *bitmap) 33962306a36Sopenharmony_ci{ 34062306a36Sopenharmony_ci return bitmap->mapped_base_index >= bitmap->mapped_total_index; 34162306a36Sopenharmony_ci} 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci/* 34462306a36Sopenharmony_ci * Advances to the next range, releases the current pinned 34562306a36Sopenharmony_ci * pages and pins the next set of bitmap pages. 34662306a36Sopenharmony_ci * Returns 0 on success or otherwise errno. 34762306a36Sopenharmony_ci */ 34862306a36Sopenharmony_cistatic int iova_bitmap_advance(struct iova_bitmap *bitmap) 34962306a36Sopenharmony_ci{ 35062306a36Sopenharmony_ci unsigned long iova = iova_bitmap_mapped_length(bitmap) - 1; 35162306a36Sopenharmony_ci unsigned long count = iova_bitmap_offset_to_index(bitmap, iova) + 1; 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci bitmap->mapped_base_index += count; 35462306a36Sopenharmony_ci 35562306a36Sopenharmony_ci iova_bitmap_put(bitmap); 35662306a36Sopenharmony_ci if (iova_bitmap_done(bitmap)) 35762306a36Sopenharmony_ci return 0; 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci /* When advancing the index we pin the next set of bitmap pages */ 36062306a36Sopenharmony_ci return iova_bitmap_get(bitmap); 36162306a36Sopenharmony_ci} 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_ci/** 36462306a36Sopenharmony_ci * iova_bitmap_for_each() - Iterates over the bitmap 36562306a36Sopenharmony_ci * @bitmap: IOVA bitmap to iterate 36662306a36Sopenharmony_ci * @opaque: Additional argument to pass to the callback 36762306a36Sopenharmony_ci * @fn: Function that gets called for each IOVA range 36862306a36Sopenharmony_ci * 36962306a36Sopenharmony_ci * Helper function to iterate over bitmap data representing a portion of IOVA 37062306a36Sopenharmony_ci * space. It hides the complexity of iterating bitmaps and translating the 37162306a36Sopenharmony_ci * mapped bitmap user pages into IOVA ranges to process. 37262306a36Sopenharmony_ci * 37362306a36Sopenharmony_ci * Return: 0 on success, and an error on failure either upon 37462306a36Sopenharmony_ci * iteration or when the callback returns an error. 37562306a36Sopenharmony_ci */ 37662306a36Sopenharmony_ciint iova_bitmap_for_each(struct iova_bitmap *bitmap, void *opaque, 37762306a36Sopenharmony_ci iova_bitmap_fn_t fn) 37862306a36Sopenharmony_ci{ 37962306a36Sopenharmony_ci int ret = 0; 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci for (; !iova_bitmap_done(bitmap) && !ret; 38262306a36Sopenharmony_ci ret = iova_bitmap_advance(bitmap)) { 38362306a36Sopenharmony_ci ret = fn(bitmap, iova_bitmap_mapped_iova(bitmap), 38462306a36Sopenharmony_ci iova_bitmap_mapped_length(bitmap), opaque); 38562306a36Sopenharmony_ci if (ret) 38662306a36Sopenharmony_ci break; 38762306a36Sopenharmony_ci } 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_ci return ret; 39062306a36Sopenharmony_ci} 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci/** 39362306a36Sopenharmony_ci * iova_bitmap_set() - Records an IOVA range in bitmap 39462306a36Sopenharmony_ci * @bitmap: IOVA bitmap 39562306a36Sopenharmony_ci * @iova: IOVA to start 39662306a36Sopenharmony_ci * @length: IOVA range length 39762306a36Sopenharmony_ci * 39862306a36Sopenharmony_ci * Set the bits corresponding to the range [iova .. iova+length-1] in 39962306a36Sopenharmony_ci * the user bitmap. 40062306a36Sopenharmony_ci * 40162306a36Sopenharmony_ci */ 40262306a36Sopenharmony_civoid iova_bitmap_set(struct iova_bitmap *bitmap, 40362306a36Sopenharmony_ci unsigned long iova, size_t length) 40462306a36Sopenharmony_ci{ 40562306a36Sopenharmony_ci struct iova_bitmap_map *mapped = &bitmap->mapped; 40662306a36Sopenharmony_ci unsigned long cur_bit = ((iova - mapped->iova) >> 40762306a36Sopenharmony_ci mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE; 40862306a36Sopenharmony_ci unsigned long last_bit = (((iova + length - 1) - mapped->iova) >> 40962306a36Sopenharmony_ci mapped->pgshift) + mapped->pgoff * BITS_PER_BYTE; 41062306a36Sopenharmony_ci unsigned long last_page_idx = mapped->npages - 1; 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci do { 41362306a36Sopenharmony_ci unsigned int page_idx = cur_bit / BITS_PER_PAGE; 41462306a36Sopenharmony_ci unsigned int offset = cur_bit % BITS_PER_PAGE; 41562306a36Sopenharmony_ci unsigned int nbits = min(BITS_PER_PAGE - offset, 41662306a36Sopenharmony_ci last_bit - cur_bit + 1); 41762306a36Sopenharmony_ci void *kaddr; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci if (unlikely(page_idx > last_page_idx)) 42062306a36Sopenharmony_ci break; 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ci kaddr = kmap_local_page(mapped->pages[page_idx]); 42362306a36Sopenharmony_ci bitmap_set(kaddr, offset, nbits); 42462306a36Sopenharmony_ci kunmap_local(kaddr); 42562306a36Sopenharmony_ci cur_bit += nbits; 42662306a36Sopenharmony_ci } while (cur_bit <= last_bit); 42762306a36Sopenharmony_ci} 42862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(iova_bitmap_set); 429