xref: /openbmc/qemu/block/block-ram-registrar.c (revision ffe98631)
1 /*
2  * BlockBackend RAM Registrar
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 
7 #include "qemu/osdep.h"
8 #include "sysemu/block-backend.h"
9 #include "sysemu/block-ram-registrar.h"
10 #include "qapi/error.h"
11 
12 static void ram_block_added(RAMBlockNotifier *n, void *host, size_t size,
13                             size_t max_size)
14 {
15     BlockRAMRegistrar *r = container_of(n, BlockRAMRegistrar, notifier);
16     Error *err = NULL;
17 
18     if (!r->ok) {
19         return; /* don't try again if we've already failed */
20     }
21 
22     if (!blk_register_buf(r->blk, host, max_size, &err)) {
23         error_report_err(err);
24         ram_block_notifier_remove(&r->notifier);
25         r->ok = false;
26     }
27 }
28 
29 static void ram_block_removed(RAMBlockNotifier *n, void *host, size_t size,
30                               size_t max_size)
31 {
32     BlockRAMRegistrar *r = container_of(n, BlockRAMRegistrar, notifier);
33     blk_unregister_buf(r->blk, host, max_size);
34 }
35 
36 void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk)
37 {
38     r->blk = blk;
39     r->notifier = (RAMBlockNotifier){
40         .ram_block_added = ram_block_added,
41         .ram_block_removed = ram_block_removed,
42 
43         /*
44          * .ram_block_resized() is not necessary because we use the max_size
45          * value that does not change across resize.
46          */
47     };
48     r->ok = true;
49 
50     ram_block_notifier_add(&r->notifier);
51 }
52 
53 void blk_ram_registrar_destroy(BlockRAMRegistrar *r)
54 {
55     if (r->ok) {
56         ram_block_notifier_remove(&r->notifier);
57     }
58 }
59