4 * @brief OS-independent IO/Mem implementation
8 * IXP400 SW Release version 2.0
10 * -- Copyright Notice --
13 * Copyright 2001-2005, Intel Corporation.
14 * All rights reserved.
17 * SPDX-License-Identifier: BSD-3-Clause
19 * -- End of Copyright Notice --
22 /* Access to the global mem map is only allowed in this file */
27 #define SEARCH_PHYSICAL_ADDRESS (1)
28 #define SEARCH_VIRTUAL_ADDRESS (2)
31 * Searches for map using one of the following criteria:
33 * - enough room to include a zone starting with the physical "requestedAddress" of size "size" (for mapping)
34 * - includes the virtual "requestedAddress" in its virtual address space (already mapped, for unmapping)
37 * Returns a pointer to the map or NULL if a suitable map is not found.
39 PRIVATE IxOsalMemoryMap *
40 ixOsalMemMapFind (UINT32 requestedAddress,
41 UINT32 size, UINT32 searchCriteria, UINT32 requestedEndianType)
45 UINT32 numMapElements = ARRAY_SIZE(ixOsalGlobalMemoryMap);
47 for (mapIndex = 0; mapIndex < numMapElements; mapIndex++)
49 IxOsalMemoryMap *map = &ixOsalGlobalMemoryMap[mapIndex];
51 if (searchCriteria == SEARCH_PHYSICAL_ADDRESS
52 && requestedAddress >= map->physicalAddress
53 && (requestedAddress + size) <= (map->physicalAddress + map->size)
54 && (map->mapEndianType & requestedEndianType) != 0)
58 else if (searchCriteria == SEARCH_VIRTUAL_ADDRESS
59 && requestedAddress >= map->virtualAddress
60 && requestedAddress <= (map->virtualAddress + map->size)
61 && (map->mapEndianType & requestedEndianType) != 0)
65 else if (searchCriteria == SEARCH_PHYSICAL_ADDRESS)
67 ixOsalLog (IX_OSAL_LOG_LVL_DEBUG3,
68 IX_OSAL_LOG_DEV_STDOUT,
69 "Osal: Checking [phys addr 0x%x:size 0x%x:endianType %d]\n",
70 map->physicalAddress, map->size, map->mapEndianType, 0, 0, 0);
81 * This function maps an I/O mapped physical memory zone of the given size
82 * into a virtual memory zone accessible by the caller and returns a cookie -
83 * the start address of the virtual memory zone.
84 * IX_OSAL_MMAP_PHYS_TO_VIRT should NOT therefore be used on the returned
86 * The memory zone is to be unmapped using ixOsalMemUnmap once the caller has
87 * finished using this zone (e.g. on driver unload) using the cookie as
89 * The IX_OSAL_READ/WRITE_LONG/SHORT macros should be used to read and write
90 * the mapped memory, adding the necessary offsets to the address cookie.
92 * Note: this function is not to be used directly. Use IX_OSAL_MEM_MAP
96 ixOsalIoMemMap (UINT32 requestedAddress,
97 UINT32 size, IxOsalMapEndianessType requestedEndianType)
101 ixOsalLog (IX_OSAL_LOG_LVL_DEBUG3,
102 IX_OSAL_LOG_DEV_STDOUT,
103 "OSAL: Mapping [addr 0x%x:size 0x%x:endianType %d]\n",
104 requestedAddress, size, requestedEndianType, 0, 0, 0);
106 if (requestedEndianType == IX_OSAL_LE)
108 ixOsalLog (IX_OSAL_LOG_LVL_ERROR,
109 IX_OSAL_LOG_DEV_STDOUT,
110 "ixOsalIoMemMap: Please specify component coherency mode to use MEM functions \n",
114 map = ixOsalMemMapFind (requestedAddress,
115 size, SEARCH_PHYSICAL_ADDRESS, requestedEndianType);
118 UINT32 offset = requestedAddress - map->physicalAddress;
120 ixOsalLog (IX_OSAL_LOG_LVL_DEBUG3,
121 IX_OSAL_LOG_DEV_STDOUT, "OSAL: Found map [", 0, 0, 0, 0, 0, 0);
122 ixOsalLog (IX_OSAL_LOG_LVL_DEBUG3,
123 IX_OSAL_LOG_DEV_STDOUT, map->name, 0, 0, 0, 0, 0, 0);
124 ixOsalLog (IX_OSAL_LOG_LVL_DEBUG3,
125 IX_OSAL_LOG_DEV_STDOUT,
126 ":addr 0x%x: virt 0x%x:size 0x%x:ref %d:endianType %d]\n",
127 map->physicalAddress, map->virtualAddress,
128 map->size, map->refCount, map->mapEndianType, 0);
130 if (map->type == IX_OSAL_DYNAMIC_MAP && map->virtualAddress == 0)
132 if (map->mapFunction != NULL)
134 map->mapFunction (map);
136 if (map->virtualAddress == 0)
141 ixOsalLog (IX_OSAL_LOG_LVL_FATAL,
142 IX_OSAL_LOG_DEV_STDERR,
143 "OSAL: Remap failed - [addr 0x%x:size 0x%x:endianType %d]\n",
144 requestedAddress, size, requestedEndianType, 0, 0, 0);
151 * error, no map function for a dynamic map
153 ixOsalLog (IX_OSAL_LOG_LVL_FATAL,
154 IX_OSAL_LOG_DEV_STDERR,
155 "OSAL: No map function for a dynamic map - "
156 "[addr 0x%x:size 0x%x:endianType %d]\n",
157 requestedAddress, size, requestedEndianType, 0, 0, 0);
164 * increment reference count
168 return (void *) (map->virtualAddress + offset);
172 * requested address is not described in the global memory map
174 ixOsalLog (IX_OSAL_LOG_LVL_FATAL,
175 IX_OSAL_LOG_DEV_STDERR,
176 "OSAL: No mapping found - [addr 0x%x:size 0x%x:endianType %d]\n",
177 requestedAddress, size, requestedEndianType, 0, 0, 0);
182 * This function unmaps a previously mapped I/O memory zone using
183 * the cookie obtained in the mapping operation. The memory zone in question
184 * becomes unavailable to the caller once unmapped and the cookie should be
187 * This function cannot fail if the given parameter is correct and does not
190 * Note: this function is not to be used directly. Use IX_OSAL_MEM_UNMAP
194 ixOsalIoMemUnmap (UINT32 requestedAddress, UINT32 endianType)
196 IxOsalMemoryMap *map;
198 if (endianType == IX_OSAL_LE)
200 ixOsalLog (IX_OSAL_LOG_LVL_ERROR,
201 IX_OSAL_LOG_DEV_STDOUT,
202 "ixOsalIoMemUnmap: Please specify component coherency mode to use MEM functions \n",
207 if (requestedAddress == 0)
210 * invalid virtual address
216 ixOsalMemMapFind (requestedAddress, 0, SEARCH_VIRTUAL_ADDRESS,
221 if (map->refCount > 0)
224 * decrement reference count
228 if (map->refCount == 0)
231 * no longer used, deallocate
233 if (map->type == IX_OSAL_DYNAMIC_MAP
234 && map->unmapFunction != NULL)
236 map->unmapFunction (map);
243 ixOsalLog (IX_OSAL_LOG_LVL_WARNING,
244 IX_OSAL_LOG_DEV_STDERR,
245 "OSAL: ixOsServMemUnmap didn't find the requested map "
246 "[virt addr 0x%x: endianType %d], ignoring call\n",
247 requestedAddress, endianType, 0, 0, 0, 0);
252 * This function Converts a virtual address into a physical
253 * address, including the dynamically mapped memory.
255 * Parameters virtAddr - virtual address to convert
256 * Return value: corresponding physical address, or NULL
257 * if there is no physical address addressable
258 * by the given virtual address
259 * OS: VxWorks, Linux, WinCE, QNX, eCos
264 ixOsalIoMemVirtToPhys (UINT32 virtualAddress, UINT32 requestedCoherency)
266 IxOsalMemoryMap *map =
267 ixOsalMemMapFind (virtualAddress, 0, SEARCH_VIRTUAL_ADDRESS,
272 return map->physicalAddress + virtualAddress - map->virtualAddress;
276 return (UINT32) IX_OSAL_MMU_VIRT_TO_PHYS (virtualAddress);
281 * This function Converts a virtual address into a physical
282 * address, including the dynamically mapped memory.
284 * Parameters virtAddr - virtual address to convert
285 * Return value: corresponding physical address, or NULL
286 * if there is no physical address addressable
287 * by the given virtual address
288 * OS: VxWorks, Linux, WinCE, QNX, eCos
293 ixOsalIoMemPhysToVirt (UINT32 physicalAddress, UINT32 requestedCoherency)
295 IxOsalMemoryMap *map =
296 ixOsalMemMapFind (physicalAddress, 0, SEARCH_PHYSICAL_ADDRESS,
301 return map->virtualAddress + physicalAddress - map->physicalAddress;
305 return (UINT32) IX_OSAL_MMU_PHYS_TO_VIRT (physicalAddress);