1 /*
2  * Copyright (c) 2020 LK Trusty Authors. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #pragma once
25 
26 #include <kernel/vm_obj.h>
27 #include <sys/types.h>
28 
29 struct vmm_obj;
30 struct obj_ref;
31 
32 /**
33  * struct phys_mem_obj - Memory object for physical memory.
34  * @vmm_obj:        VMM object.
35  * @paddr:          Physical address where region starts.
36  *                  Should be a multiple of PAGE_SIZE.
37  * @size:           Number of bytes in region.
38  *                  Should be a multiple of PAGE_SIZE.
39  * @arch_mmu_flags: Memory type and required permission flags.
40  * @destroy_fn:     Destructor function.
41  */
42 struct phys_mem_obj {
43     struct vmm_obj vmm_obj;
44     paddr_t paddr;
45     size_t size;
46     uint arch_mmu_flags;
47     void (*destroy_fn)(struct phys_mem_obj*);
48 };
49 
50 /**
51  * phys_mem_obj_dynamic_initialize - Initialize dynamically-allocated
52  *                                   struct phys_mem_obj that is accompanied
53  *                                   by a destructor function.
54  * @obj:            Object to initialize.
55  * @ref:            Initial reference.
56  * @paddr:          Physical address where region starts.
57  *                  Should be a multiple of PAGE_SIZE.
58  * @size:           Number of bytes in region.
59  *                  Should be a multiple of PAGE_SIZE.
60  * @arch_mmu_flags: Memory type and required permission flags.
61  * @destroy_fn:     Destructor function. Must not be %NULL.
62  */
63 void phys_mem_obj_dynamic_initialize(struct phys_mem_obj* obj,
64                                      struct obj_ref* ref,
65                                      paddr_t paddr,
66                                      size_t size,
67                                      uint arch_mmu_flags,
68                                      void (*destroy_fn)(struct phys_mem_obj*));
69 
70 /**
71  * phys_mem_obj_initialize - Initialize struct phys_mem_obj.
72  * @obj:            Object to initialize.
73  * @ref:            Initial reference.
74  * @paddr:          Physical address where region starts.
75  *                  Should be a multiple of PAGE_SIZE.
76  * @size:           Number of bytes in region.
77  *                  Should be a multiple of PAGE_SIZE.
78  * @arch_mmu_flags: Memory type and required permission flags.
79  */
80 void phys_mem_obj_initialize(struct phys_mem_obj* obj,
81                              struct obj_ref* ref,
82                              paddr_t paddr,
83                              size_t size,
84                              uint arch_mmu_flags);
85