9d105ecf5d7f1eb41e29a403fbb931a3a03e4129
[linux.git] /
1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #ifndef __MEMORY_ACCESS_H_INCLUDED__
16 #define __MEMORY_ACCESS_H_INCLUDED__
17
18 /*!
19  * \brief
20  * Define the public interface for virtual memory
21  * access functions. Access types are limited to
22  * those defined in <stdint.h>
23  *
24  * The address representation is private to the system
25  * and represented as "hrt_vaddress" rather than a
26  * pointer, as the memory allocation cannot be accessed
27  * by dereferencing but reaquires load and store access
28  * functions
29  *
30  * The page table selection or virtual memory context;
31  * The page table base index; Is implicit. This page
32  * table base index must be set by the implementation
33  * of the access function
34  *
35  * "store" is a transfer to the system
36  * "load" is a transfer from the system
37  *
38  * Allocation properties can be specified by setting
39  * attributes (see below) in case of multiple physical
40  * memories the memory ID is encoded on the attribute
41  *
42  * Allocations in the same physical memory, but in a
43  * different (set of) page tables can be shared through
44  * a page table information mapping function
45  */
46
47 #include <type_support.h>
48 #include "platform_support.h"   /* for __func__ */
49
50 /*
51  * User provided file that defines the (sub)system address types:
52  *      - hrt_vaddress  a type that can hold the (sub)system virtual address range
53  */
54 #include "system_types.h"
55
56 /*
57  * The MMU base address is a physical address, thus the same type is used
58  * as for the device base address
59  */
60 #include "device_access.h"
61
62 /*!
63  * \brief
64  * Bit masks for specialised allocation functions
65  * the default is "uncached", "not contiguous",
66  * "not page aligned" and "not cleared"
67  *
68  * Forcing alignment (usually) returns a pointer
69  * at an alignment boundary that is offset from
70  * the allocated pointer. Without storing this
71  * pointer/offset, we cannot free it. The memory
72  * manager is responsible for the bookkeeping, e.g.
73  * the allocation function creates a sentinel
74  * within the allocation referencable from the
75  * returned pointer/address.
76  */
77 #define MMGR_ATTRIBUTE_MASK                     0x000f
78 #define MMGR_ATTRIBUTE_CACHED           0x0001
79 #define MMGR_ATTRIBUTE_CONTIGUOUS       0x0002
80 #define MMGR_ATTRIBUTE_PAGEALIGN        0x0004
81 #define MMGR_ATTRIBUTE_CLEARED          0x0008
82 #define MMGR_ATTRIBUTE_UNUSED           0xfff0
83
84 /* #define MMGR_ATTRIBUTE_DEFAULT       (MMGR_ATTRIBUTE_CACHED) */
85 #define MMGR_ATTRIBUTE_DEFAULT  0
86
87 extern const hrt_vaddress       mmgr_NULL;
88 extern const hrt_vaddress       mmgr_EXCEPTION;
89
90 /*! Set the (sub)system virtual memory page table base address
91
92  \param base_addr[in]           The address where page table 0 is located
93
94  \Note: The base_addr is an absolute system address, thus it is not
95         relative to the DDR base address
96
97  \return none,
98  */
99 extern void mmgr_set_base_address(
100         const sys_address               base_addr);
101
102 /*! Get the (sub)system virtual memory page table base address
103
104  \return base_address,
105  */
106 /* unused */
107 extern sys_address mmgr_get_base_address(void);
108
109
110 /*! Set the (sub)system virtual memory page table base index
111
112  \param base_addr[in]           The index  where page table 0 is located
113
114  \Note: The base_index is the MSB section of an absolute system address,
115         the in-page address bits are discared. The base address is not
116         relative to the DDR base address
117
118  \return none,
119  */
120 /* unused */
121 extern void mmgr_set_base_index(
122         const hrt_data                  base_index);
123
124 /*! Get the (sub)system virtual memory page table base index
125
126  \return base_address,
127  */
128 /* unused */
129 extern hrt_data mmgr_get_base_index(void);
130
131 /*! Return the address of an allocation in memory
132
133  \param size[in]                        Size in bytes of the allocation
134  \param caller_func[in]         Caller function name
135  \param caller_line[in]         Caller function line number
136
137  \return vaddress
138  */
139 #define mmgr_malloc(__size) mmgr_malloc_ex(__size, __func__, __LINE__)
140 extern hrt_vaddress mmgr_malloc_ex(
141         const size_t                    size,
142         const char                              *caller_func,
143         int                                             caller_line);
144
145 /*! Return the address of a zero initialised allocation in memory
146
147  \param N[in]                   Horizontal dimension of array
148  \param size[in]                Vertical dimension of array  Total size is N*size
149  \param caller_func[in]         Caller function name
150  \param caller_line[in]         Caller function line number
151
152  \return vaddress
153  */
154 #define mmgr_calloc(__N, __size) mmgr_calloc_ex(__N, __size, __func__, __LINE__)
155 extern hrt_vaddress mmgr_calloc_ex(
156         const size_t                    N,
157         const size_t                    size,
158         const char                              *caller_func,
159         int                                             caller_line);
160
161 /*! Return the address of a reallocated allocation in memory
162
163  \param vaddr[in]               Address of an allocation
164  \param size[in]                Size in bytes of the allocation
165
166  \Note
167  All limitations and particularities of the C stdlib
168  realloc function apply
169
170  \return vaddress
171  */
172 /* unused */
173 extern hrt_vaddress mmgr_realloc(
174         hrt_vaddress                    vaddr,
175         const size_t                    size);
176
177 /*! Free the memory allocation identified by the address
178
179  \param vaddr[in]               Address of the allocation
180  \param caller_func[in]         Caller function name
181  \param caller_line[in]         Caller function line number
182
183  \return vaddress
184  */
185 #define mmgr_free(__vaddr) mmgr_free_ex(__vaddr, __func__, __LINE__)
186 extern void mmgr_free_ex(
187         hrt_vaddress                    vaddr,
188         const char                              *caller_func,
189         int                                             caller_line);
190
191 /*! Return the address of an allocation in memory
192
193  \param size[in]                Size in bytes of the allocation
194  \param attribute[in]           Bit vector specifying the properties
195                                 of the allocation including zero initialisation
196  \param caller_func[in]         Caller function name
197  \param caller_line[in]         Caller function line number
198
199  \return vaddress
200  */
201 #define mmgr_alloc_attr(__size, __attribute) mmgr_alloc_attr_ex(__size, __attribute, __func__, __LINE__)
202 extern hrt_vaddress mmgr_alloc_attr_ex(
203         const size_t                    size,
204         const uint16_t                  attribute,
205         const char                              *caller_func,
206         int                                             caller_line);
207
208 /*! Return the address of a reallocated allocation in memory
209
210  \param vaddr[in]               Address of an allocation
211  \param size[in]                Size in bytes of the allocation
212  \param attribute[in]           Bit vector specifying the properties
213                                 of the allocation
214 #endif
215
216  \Note
217         All limitations and particularities of the C stdlib
218         realloc function apply
219
220  \return vaddress
221  */
222 /* unused */
223 extern hrt_vaddress mmgr_realloc_attr(
224         hrt_vaddress                    vaddr,
225         const size_t                    size,
226         const uint16_t                  attribute);
227
228 /*! Return the address of a mapped existing allocation in memory
229
230  \param ptr[in]                 Pointer to an allocation in a different
231                                 virtual memory page table, but the same
232                                 physical memory
233  \param size[in]                Size of the memory of the pointer
234  \param attribute[in]           Bit vector specifying the properties
235                                 of the allocation
236  \param context                 Pointer of a context provided by
237                                 client/driver for additonal parameters
238                                 needed by the implementation
239  \Note
240         This interface is tentative, limited to the desired function
241         the actual interface may require furhter parameters
242
243  \return vaddress
244  */
245 extern hrt_vaddress mmgr_mmap(
246         const void *ptr,
247         const size_t size,
248         uint16_t attribute,
249         void *context);
250
251 /*! Zero initialise an allocation in memory
252
253  \param vaddr[in]               Address of an allocation
254  \param size[in]                Size in bytes of the area to be cleared
255  \param caller_func[in]         Caller function name
256  \param caller_line[in]         Caller function line number
257
258  \return none
259  */
260 #define mmgr_clear(__vaddr, __size) mmgr_clear_ex(__vaddr, __size, __func__, __LINE__)
261 extern void mmgr_clear_ex(
262         hrt_vaddress                    vaddr,
263         const size_t                    size,
264         const char                      *caller_func,
265         int                             caller_line);
266
267 /*! Set an allocation in memory to a value
268
269  \param vaddr[in]               Address of an allocation
270  \param data[in]                Value to set
271  \param size[in]                Size in bytes of the area to be set
272
273  \return none
274  */
275 /* unused */
276 extern void mmgr_set(
277         hrt_vaddress                    vaddr,
278         const uint8_t                   data,
279         const size_t                    size);
280
281 /*! Read an array of bytes from a virtual memory address
282
283  \param vaddr[in]               Address of an allocation
284  \param data[out]               pointer to the destination array
285  \param size[in]                number of bytes to read
286  \param caller_func[in]         Caller function name
287  \param caller_line[in]         Caller function line number
288
289  \return none
290  */
291 #define mmgr_load(__vaddr, __data, __size) mmgr_load_ex(__vaddr, __data, __size, __func__, __LINE__)
292 extern void mmgr_load_ex(
293         const hrt_vaddress              vaddr,
294         void                            *data,
295         const size_t                    size,
296         const char                      *caller_func,
297         int                             caller_line);
298
299 /*! Write an array of bytes to device registers or memory in the device
300
301  \param vaddr[in]               Address of an allocation
302  \param data[in]                pointer to the source array
303  \param size[in]                number of bytes to write
304  \param caller_func[in]         Caller function name
305  \param caller_line[in]         Caller function line number
306
307  \return none
308  */
309 #define mmgr_store(__vaddr, __data, __size) mmgr_store_ex(__vaddr, __data, __size, __func__, __LINE__)
310 extern void mmgr_store_ex(
311         const hrt_vaddress              vaddr,
312         const void                              *data,
313         const size_t                    size,
314         const char                              *caller_func,
315         int                                             caller_line);
316
317 #endif /* __MEMORY_ACCESS_H_INCLUDED__ */