Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions zephyr/lib/vpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ __cold static int vpages_alloc_and_map(unsigned int pages, void **ptr)
if (ret < 0) {
LOG_ERR("error: failed to map virtual region %p to physical region %p, error %d",
vaddr, vpage_ctx.virtual_region->addr, ret);

/*
* The driver maps page by page and leaves already mapped pages (and
* their physical pages) in place on failure, e.g. when physical L2 is
* exhausted. Release them so they do not leak.
*/
for (unsigned int i = 0; i < pages; i++) {
void *va = (uint8_t *)vaddr + i * CONFIG_MM_DRV_PAGE_SIZE;

if (!sys_mm_drv_page_phys_get(va, NULL))
sys_mm_drv_unmap_page(va);
}

sys_mem_blocks_free_contiguous(&vpage_ctx.vpage_blocks, vaddr, pages);
Comment thread
kv2019i marked this conversation as resolved.
Resolved
return ret;
}
Expand Down Expand Up @@ -231,12 +244,15 @@ static int vpages_free_and_unmap(uintptr_t *ptr)
return ret;
}

/* move the last element over the released one, clear the last element */
/*
* Keep the elements dense: move the last in-use element (num_elems_in_use - 1)
* over the released one and clear the last slot.
*/
vpage_ctx.num_elems_in_use--;
if (vpage_ctx.num_elems_in_use != elem_idx)
vpage_ctx.velems[elem_idx] = vpage_ctx.velems[vpage_ctx.num_elems_in_use];
vpage_ctx.velems[vpage_ctx.num_elems_in_use].pages = 0;
vpage_ctx.velems[vpage_ctx.num_elems_in_use].vpage = 0;
vpage_ctx.num_elems_in_use--;

/* success update the free pages */
vpage_ctx.free_pages += pages;
Expand Down Expand Up @@ -296,18 +312,40 @@ __cold static int vpage_init(void)
end = l2_end - CONFIG_LIBRARY_REGION_SIZE;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a blocker but I wonder if the vpages should go beyond l2_end in this case. IOW why limit vpages to physical L2 size in this case, if we already know Zephyr supports going beyond and we use it below for systems with smaller L2. IOW, shouldn't we size vpages based on a) physical L2 size and b) image size. vpages cannot be larger than a-b, but I don't see a need to limit otherwise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The only cap applied in the small-L2 branch is a) physical L2 minus b) the image size (L2 end - image end), i.e. exactly the bound you describe; beyond that the region extends up to the library base / end of virtual memory. I kept the first branch (everything fits in L2) as is to not change the layout of the other platforms. Happy to unify the two if you prefer, but that would move other platforms' vpage regions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My comment was specifically about the first branch. Given the code now "oversubscribes" vpage+libregion in case of a smaller L2, why not oversubscribe also in the generic case?

But agreed this is probably handled in a separate PR.

} else {
/*
* TODO: on demand mapping currently only supports
* cached mappings, so we need to limit vpages to
* physical L2 size for now until support is added to
* Zephyr mm_drv_intel_adsp_mtl_tlb.c and friends
* Small L2 (e.g. NVL-S): the virtual heap and library regions are only
* virtual address space backed on demand, so they need not fit in L2
* together with the vpages. Use the virtual space up to the library
* region (or the end of virtual memory) and cap it below by the
* physical L2 left after the image, as vpages can never use more.
* Using pages beyond physical L2 needs the Zephyr TLB driver to map
* the uncached alias too, see
* https://github.com/zephyrproject-rtos/zephyr/pull/122801
*/
LOG_WRN("no space left for vpages, start %#lx l2_end %#lx",
(unsigned long)start, (unsigned long)l2_end);
end = L2_VIRTUAL_SRAM_BASE + L2_VIRTUAL_SRAM_SIZE;
if (IS_ENABLED(CONFIG_LIBRARY_MANAGER) && CONFIG_LIBRARY_BASE_ADDRESS < end)
end = CONFIG_LIBRARY_BASE_ADDRESS;
}
Comment thread
kv2019i marked this conversation as resolved.
Resolved

/* the image or virtual heap may leave no room, don't let this underflow */
if (end < start + CONFIG_MM_DRV_PAGE_SIZE) {
LOG_WRN("no space left for vpages, start %#lx end %#lx",
(unsigned long)start, (unsigned long)end);
return -ENOMEM;
}

remaining_ram = end - start;
__ASSERT_NO_MSG(remaining_ram >= CONFIG_MM_DRV_PAGE_SIZE);
if (end > l2_end) {
uintptr_t image_end = adsp_mm_get_unused_l2_start_aligned();
size_t phys_left = image_end < l2_end ? l2_end - image_end : 0;

remaining_ram = MIN(remaining_ram, phys_left);
}

/* revalidate, the physical cap can leave less than a page */
if (remaining_ram < CONFIG_MM_DRV_PAGE_SIZE) {
LOG_WRN("no physical L2 left for vpages");
return -ENOMEM;
}

ret = adsp_add_virtual_memory_region(start, remaining_ram, VIRTUAL_REGION_VPAGES_ATTR);
if (ret)
Expand Down
Loading
Loading