drm/xe: Add helper to extend CPU-mirrored VMA range for merge

Introduce xe_vm_find_cpu_addr_mirror_vma_range(), which computes an
extended range around a given range by including adjacent VMAs that are
CPU-address-mirrored and have default memory attributes. This helper is
useful for determining mergeable range without performing the actual merge.

v2
- Add assert
- Move unmap check to this patch

v3
- Decrease offset to check by SZ_4K to avoid wrong vma return in fast
  lookup path

v4
- *start should be >= SZ_4K (Matt)

Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Link: https://patch.msgid.link/20251125075628.1182481-2-himal.prasad.ghimiray@intel.com
Signed-off-by: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
This commit is contained in:
Himal Prasad Ghimiray
2025-11-25 13:26:24 +05:30
parent 07cf4b864f
commit 5b12958b1f
2 changed files with 44 additions and 0 deletions

View File

@@ -4383,6 +4383,47 @@ int xe_vm_alloc_madvise_vma(struct xe_vm *vm, uint64_t start, uint64_t range)
return xe_vm_alloc_vma(vm, &map_req, true);
}
static bool is_cpu_addr_vma_with_default_attr(struct xe_vma *vma)
{
return vma && xe_vma_is_cpu_addr_mirror(vma) &&
!xe_svm_has_mapping(xe_vma_vm(vma), xe_vma_start(vma), xe_vma_end(vma)) &&
xe_vma_has_default_mem_attrs(vma);
}
/**
* xe_vm_find_cpu_addr_mirror_vma_range - Extend a VMA range to include adjacent CPU-mirrored VMAs
* @vm: VM to search within
* @start: Input/output pointer to the starting address of the range
* @end: Input/output pointer to the end address of the range
*
* Given a range defined by @start and @range, this function checks the VMAs
* immediately before and after the range. If those neighboring VMAs are
* CPU-address-mirrored and have default memory attributes, the function
* updates @start and @range to include them. This extended range can then
* be used for merging or other operations that require a unified VMA.
*
* The function does not perform the merge itself; it only computes the
* mergeable boundaries.
*/
void xe_vm_find_cpu_addr_mirror_vma_range(struct xe_vm *vm, u64 *start, u64 *end)
{
struct xe_vma *prev, *next;
lockdep_assert_held(&vm->lock);
if (*start >= SZ_4K) {
prev = xe_vm_find_vma_by_addr(vm, *start - SZ_4K);
if (is_cpu_addr_vma_with_default_attr(prev))
*start = xe_vma_start(prev);
}
if (*end < vm->size) {
next = xe_vm_find_vma_by_addr(vm, *end + 1);
if (is_cpu_addr_vma_with_default_attr(next))
*end = xe_vma_end(next);
}
}
/**
* xe_vm_alloc_cpu_addr_mirror_vma - Allocate CPU addr mirror vma
* @vm: Pointer to the xe_vm structure