Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup ClassicCache
0007  */
0008 
0009 /*
0010  * Copyright (c) 2014 embedded brains GmbH & Co. KG
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037 
0038 #include <rtems.h>
0039 #include <rtems/malloc.h>
0040 #include <rtems/score/apimutex.h>
0041 #include <rtems/score/heapimpl.h>
0042 #include <rtems/score/sysstate.h>
0043 
0044 static Heap_Control cache_coherent_heap_instance;
0045 
0046 static Heap_Control *cache_coherent_heap;
0047 
0048 void *rtems_cache_coherent_allocate(
0049   size_t size,
0050   uintptr_t alignment,
0051   uintptr_t boundary
0052 )
0053 {
0054   void *ptr;
0055   Heap_Control *heap;
0056 
0057   _RTEMS_Lock_allocator();
0058 
0059   heap = cache_coherent_heap;
0060   if ( heap == NULL ) {
0061     heap = RTEMS_Malloc_Heap;
0062   }
0063 
0064   ptr = _Heap_Allocate_aligned_with_boundary(
0065     heap,
0066     size,
0067     alignment,
0068     boundary
0069   );
0070 
0071   _RTEMS_Unlock_allocator();
0072 
0073   return ptr;
0074 }
0075 
0076 void rtems_cache_coherent_free( void *ptr )
0077 {
0078   Heap_Control *heap;
0079 
0080   _RTEMS_Lock_allocator();
0081 
0082   heap = cache_coherent_heap;
0083   if ( heap != NULL ) {
0084     if ( _Heap_Free( heap, ptr ) ) {
0085       heap = NULL;
0086     } else {
0087       heap = RTEMS_Malloc_Heap;
0088     }
0089   } else {
0090     heap = RTEMS_Malloc_Heap;
0091   }
0092 
0093   if ( heap != NULL ) {
0094     _Heap_Free( heap, ptr );
0095   }
0096 
0097   _RTEMS_Unlock_allocator();
0098 }
0099 
0100 static rtems_status_code add_area(
0101   void *area_begin,
0102   uintptr_t area_size
0103 )
0104 {
0105   Heap_Control *heap = cache_coherent_heap;
0106 
0107   if ( heap == NULL ) {
0108     heap = &cache_coherent_heap_instance;
0109 
0110     if ( _Heap_Initialize( heap, area_begin, area_size, 0 ) == 0 ) {
0111       return RTEMS_UNSATISFIED;
0112     }
0113     cache_coherent_heap = heap;
0114   } else {
0115     if (_Heap_Extend( heap, area_begin, area_size, 0 ) == 0) {
0116       return RTEMS_UNSATISFIED;
0117     }
0118   }
0119   return RTEMS_SUCCESSFUL;
0120 }
0121 
0122 rtems_status_code rtems_cache_coherent_add_area(
0123   void *area_begin,
0124   uintptr_t area_size
0125 )
0126 {
0127   rtems_status_code sc;
0128   bool needs_locking = _System_state_Is_up( _System_state_Get());
0129 
0130   if ( needs_locking ) {
0131     _RTEMS_Lock_allocator();
0132   }
0133 
0134   sc = add_area( area_begin, area_size );
0135 
0136   if ( needs_locking ) {
0137     _RTEMS_Unlock_allocator();
0138   }
0139 
0140   return sc;
0141 }