Developer World
Spresense SDK Library v2.2.0-87f81d9
MemManager.h
1 /****************************************************************************
2  * modules/include/memutils/memory_manager/MemManager.h
3  *
4  * Copyright 2018 Sony Semiconductor Solutions Corporation
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17  * the names of its contributors may be used to endorse or promote
18  * products derived from this software without specific prior written
19  * permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************/
35 
41 #ifndef MEMMANAGER_H_INCLUDED
42 #define MEMMANAGER_H_INCLUDED
43 
55 #include <string.h> /* memset */
58 
63 namespace MemMgrLite {
64 
65 /*
66  * Address Converters for MemHandle.
67  */
68 static inline void* translatePoolAddrToVa(PoolAddr addr)
69 {
70  return reinterpret_cast<void*>(addr);
71 }
72 
73 static inline void* translateVaToPa(void* addr)
74 {
75  /* If the address is in the virtual address converter support area
76  * (0x00000000 to 0x000FFFFF), convert it to a physical address
77  */
78 
79  if (addr < reinterpret_cast<void*>(0x00100000))
80  {
81  uint32_t tileId;
82  uint32_t tileVal;
83  uint32_t cpuId;
84  uint32_t reg;
85  uint32_t pa;
86  uint32_t va;
87 
88  va = (uint32_t)addr;
89  tileId = (va >> 16) & 0xf;
90  cpuId = *(volatile uint32_t *)((0x4c000000 | 0x02002000) + 0x40);
91  reg = (0x02012000 + 0x04) + (0x04 * (tileId / 2)) + ((cpuId - 2) * 0x20);
92  pa = *(volatile uint32_t *)(reg);
93  tileVal = ((pa >> ((tileId & 0x1) * 16)) & 0x01ff) << 16;
94 
95  return (void *)(0x0c000000 | tileVal | (va & 0xffff));
96  }
97 
98  return addr;
99 }
100 
107 class MemHandle : public MemHandleBase {
108 public:
109  MemHandle() : MemHandleBase() {}
110 #ifdef USE_MEMMGR_SEG_DELETER
111  MemHandle(PoolId id, size_t size, bool use_deleter = false) :
112  MemHandleBase(id, size, use_deleter) {}
113 #else
114  MemHandle(PoolId id, size_t size) : MemHandleBase(id, size) {}
115  MemHandle(uint8_t id, size_t size) : MemHandleBase(id, size) {}
116 #endif
117  MemHandle(const MemHandle& mh) : MemHandleBase(mh) {}
118 
122  void* getVa() const { return translatePoolAddrToVa(getAddr()); }
123 
127  void* getPa() const { return translateVaToPa(getVa()); }
128 
133  void fill(unsigned char c) { memset(getVa(), c, getSize()); }
134 
135 #ifdef USE_MEMMGR_DEBUG_OUTPUT
136  void printInfo(bool newline = true) const {
137  printf("PoolId=%3d, SegNo=%3d, Flags=%02x", getPoolId(), getSegNo(), getFlags());
138  if (isAvail()) {
139  printf(", VA=%08x(%08x), Size=%08x, SegRefCnt=%3d",
140  getVa(), getAddr(), getSize(), getRefCnt());
141  }
142  if (newline) printf("\n");
143  }
144 #endif /* USE_MEMMGR_DEBUG_OUTPUT */
145 
146 }; /* class MemHandle */
147 
148 S_ASSERT(sizeof(MemHandle) == 4);
149 
150 } /* namespace MemMgrLite */
151 
160 #endif /* MEMMANAGER_H_INCLUDED */
void * getPa() const
Definition: MemManager.h:127
Types definitions for "Memory Manager".
Definition: MemMgrTypes.h:88
Memory Handler Class for "Memory Manager Lite". This is only wrapper class for convert project-specif...
Definition: MemManager.h:107
Memory Manager Lite Memory Handle Base class API.
namespace for "Memory Manager".
void * getVa() const
Definition: MemManager.h:122
uint16_t size
Definition: video.h:86
Memory Handler Base Class for Memory Handler Base Class. This class`s methods can called only from Me...
Definition: MemHandleBase.h:66
void fill(unsigned char c)
Definition: MemManager.h:133