Skip to content

Conversation

@bisingha-xilinx
Copy link
Contributor

Added Multiple CMA Region Support for Buffer Allocation

Enables buffer object allocation from multiple CMA memory regions with automatic fallback, improving memory management on embedded platforms. It ads better memory utilization across multiple reserved cma regions, robust allocation with automatic fallback and its backward compatible

Changes

  • Implements CMA region selection via flags parameter (bits [7:0] encode region index 0-15)
  • Adds fallback logic: requested region → other available regions → system default CMA
  • Initializes CMA regions from device tree memory-region properties
  • Uses proper device lifecycle management (device_initialize(), put_device())

Tested

  • Verified user-specified allocation
  • automatic fallback
  • backward compatibility.
  • basic XRT hw unit tests for ve2

Copilot AI review requested due to automatic review settings November 26, 2025 10:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for allocating buffer objects from multiple CMA (Contiguous Memory Allocator) regions in the ve2 driver, improving memory management flexibility on embedded platforms. The implementation enables user-specified CMA region selection with automatic fallback to other regions and backward compatibility with existing code.

Key changes:

  • Implements CMA region selection through the flags parameter where bits [7:0] encode the region index (0-15)
  • Adds automatic fallback logic: requested region → other available regions → system default CMA
  • Initializes CMA regions from device tree memory-region properties with proper device lifecycle management

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/shim_ve2/xdna_bo.cpp Passes flags parameter to buffer object creation ioctl
src/include/uapi/drm_local/amdxdna_accel.h Updates documentation to define flags field bit layout for CMA region selection
src/driver/amdxdna/ve2_of.c Implements CMA region initialization and cleanup from device tree
src/driver/amdxdna/amdxdna_gem.c Adds allocation logic with region selection and fallback mechanism
src/driver/amdxdna/amdxdna_drm.h Defines structures and constants for CMA region management
src/driver/amdxdna/amdxdna_cma_buf.h Updates function signature to accept device pointer and adds region index extraction
src/driver/amdxdna/amdxdna_cma_buf.c Changes allocation functions to use device pointer instead of drm_device and implements index extraction

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* Valid indexes: 0-15 (validated at call site against MAX_MEM_REGIONS)
* Invalid indexes (>=16): handled as fallback to default CMA
*/
return ((flags) & 0xFF);
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary parentheses around flags make the code less readable. Simplify to return flags & 0xFF;

Suggested change
return ((flags) & 0xFF);
return flags & 0xFF;

Copilot uses AI. Check for mistakes.
XDNA_DBG(xdna,
"Failed to set name for cma region %d\n", i);
put_device(child_dev);
continue;
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On dev_set_name() failure, the device is freed but ve2_cma_mem_region_remove() is not called to clean up any previously initialized regions before returning. This leaves partially initialized state. Consider calling ve2_cma_mem_region_remove(xdna) before continuing or ensure proper cleanup on any failure path.

Suggested change
continue;
ve2_cma_mem_region_remove(xdna);
return ret;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The continue statement implements intentional fallback behavior. If one CMA region
fails to initialize, the driver tries remaining regions. The put_device() call
properly cleans up the failed region. Only if all regions fail the function
returns -EINVAL (line 170).

Comment on lines +156 to +162
ret = of_reserved_mem_device_init_by_idx(child_dev,
pdev->dev.of_node, i);
if (ret) {
XDNA_DBG(xdna,
"Failed to init reserved cma region %d\n", i);
put_device(child_dev);
continue;
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the dev_set_name() failure case, when of_reserved_mem_device_init_by_idx() fails, the code continues without cleaning up previously initialized regions. If all regions fail to initialize after some succeed, those successful regions remain allocated until the final check at line 170. Consider more explicit cleanup or ensure the final check properly handles partial initialization.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial initialization is the intended behavior. Successfully initialized regions are
tracked in xdna->cma_mem_regions[] and used for buffer allocation with automatic
fallback. Only if zero regions initialized the function returns -EINVAL. This
design provides resilience - the system works even if some CMA regions are unavailable.
All regions are properly cleaned up during device removal via ve2_cma_mem_region_remove().

Signed-off-by: Bikash Singha <[email protected]>
@NishadSaraf
Copy link
Member

retest this please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants