Repository navigation
vpage: fix free tracking, release partial maps, enable vpages on small-L2 (NVL-S) #11303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
605d200
c6cab73
c0936fc
621fb80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| return ret; | ||
| } | ||
|
|
@@ -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; | ||
|
|
@@ -296,18 +312,40 @@ __cold static int vpage_init(void) | |
| end = l2_end - CONFIG_LIBRARY_REGION_SIZE; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
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) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.