]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
sandbox: Implement reference counting for address mapping
authorSimon Glass <sjg@chromium.org>
Sun, 1 Sep 2024 22:26:27 +0000 (16:26 -0600)
committerTom Rini <trini@konsulko.com>
Wed, 18 Sep 2024 19:01:00 +0000 (13:01 -0600)
An address may be mapped twice and unmapped twice. Delete the mapping
only when the last user unmaps it.

Fix a missing comment while here.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/cpu.c
arch/sandbox/include/asm/state.h

index 3e1c0dd583e978fa50e16623c99e29102024ef01..51ce40e7f088a39dbd66273d8c8fb675361c7e28 100644 (file)
@@ -111,6 +111,7 @@ void *phys_to_virt(phys_addr_t paddr)
                if (mentry->tag == paddr) {
                        log_debug("Used map from %lx to %p\n", (ulong)paddr,
                                  mentry->ptr);
+                       mentry->refcnt++;
                        return mentry->ptr;
                }
        }
@@ -200,10 +201,12 @@ void unmap_physmem(const void *ptr, unsigned long flags)
 
        mentry = find_tag(ptr);
        if (mentry) {
-               list_del(&mentry->sibling_node);
-               log_debug("Removed map from %p to %lx\n", ptr,
-                         (ulong)mentry->tag);
-               free(mentry);
+               if (!--mentry->refcnt) {
+                       list_del(&mentry->sibling_node);
+                       log_debug("Removed map from %p to %lx\n", ptr,
+                                 (ulong)mentry->tag);
+                       free(mentry);
+               }
        } else {
                log_warning("Address not mapped: %p\n", ptr);
        }
@@ -235,11 +238,14 @@ phys_addr_t map_to_sysmem(const void *ptr)
                }
                mentry->tag = state->next_tag++;
                mentry->ptr = (void *)ptr;
+               mentry->refcnt = 0;
                list_add_tail(&mentry->sibling_node, &state->mapmem_head);
                log_debug("Added map from %p to %lx\n", ptr,
                          (ulong)mentry->tag);
        }
 
+       mentry->refcnt++;
+
        /*
         * Return the tag as the address to use. A later call to map_sysmem()
         * will return ptr
index 6b50473ed41c25c67dca32776418499452195690..e7dc01759e8e93814eb88bd46707d829f9d5ac05 100644 (file)
@@ -53,10 +53,13 @@ struct sandbox_wdt_info {
  * be returned, just as it would for a normal sandbox address.
  *
  * @tag: Address tag (a value which U-Boot uses to refer to the address)
+ * @refcnt: Number of references to this tag
  * @ptr: Associated pointer for that tag
+ * @sibling_node: Next node
  */
 struct sandbox_mapmem_entry {
        ulong tag;
+       uint refcnt;
        void *ptr;
        struct list_head sibling_node;
 };