Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 5 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 6 | * |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * 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 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <debug.h> |
| 32 | #include <reg.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <platform/iomap.h> |
| 35 | |
| 36 | #include "smem.h" |
| 37 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 38 | static struct smem *smem; |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 39 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 40 | /* DYNAMIC SMEM REGION feature enables LK to dynamically |
| 41 | * read the SMEM addr info from TCSR register or IMEM location. |
| 42 | * The first word read, if indicates a MAGIC number, then |
| 43 | * Dynamic SMEM is assumed to be enabled. Read the remaining |
| 44 | * SMEM info for SMEM Size and Phy_addr from the other bytes. |
| 45 | */ |
| 46 | |
| 47 | #if DYNAMIC_SMEM |
| 48 | uint32_t smem_get_base_addr() |
| 49 | { |
| 50 | struct smem_addr_info *smem_info = NULL; |
| 51 | |
| 52 | smem_info = (struct smem_addr_info *) SMEM_TARG_INFO_ADDR; |
| 53 | if(smem_info && (smem_info->identifier == SMEM_TARGET_INFO_IDENTIFIER)) |
| 54 | return smem_info->phy_addr; |
| 55 | else |
| 56 | return MSM_SHARED_BASE; |
| 57 | } |
| 58 | #endif |
| 59 | |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 60 | /* buf MUST be 4byte aligned, and len MUST be a multiple of 8. */ |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 61 | unsigned smem_read_alloc_entry(smem_mem_type_t type, void *buf, int len) |
| 62 | { |
| 63 | struct smem_alloc_info *ainfo; |
| 64 | unsigned *dest = buf; |
| 65 | unsigned src; |
| 66 | unsigned size; |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 67 | uint32_t smem_addr = 0; |
| 68 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 69 | #if DYNAMIC_SMEM |
| 70 | smem_addr = smem_get_base_addr(); |
| 71 | #else |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 72 | smem_addr = platform_get_smem_base_addr(); |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 73 | #endif |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 74 | |
| 75 | smem = (struct smem *)smem_addr; |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 76 | |
| 77 | if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0)) |
| 78 | return 1; |
| 79 | |
| 80 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
| 81 | return 1; |
| 82 | |
| 83 | /* TODO: Use smem spinlocks */ |
| 84 | ainfo = &smem->alloc_info[type]; |
| 85 | if (readl(&ainfo->allocated) == 0) |
| 86 | return 1; |
| 87 | |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 88 | size = readl(&ainfo->size); |
| 89 | |
Sridhar Parasuram | d63ea9c | 2014-10-22 12:39:11 -0700 | [diff] [blame] | 90 | if (size < (unsigned)((len + 7) & ~0x00000007)) |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 91 | return 1; |
| 92 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 93 | src = smem_addr + readl(&ainfo->offset); |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 94 | for (; len > 0; src += 4, len -= 4) |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 95 | *(dest++) = readl(src); |
| 96 | |
| 97 | return 0; |
| 98 | } |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 99 | |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 100 | /* Return a pointer to smem_item with size */ |
| 101 | void* smem_get_alloc_entry(smem_mem_type_t type, uint32_t* size) |
| 102 | { |
| 103 | struct smem_alloc_info *ainfo = NULL; |
| 104 | uint32_t smem_addr = 0; |
| 105 | uint32_t base_ext = 0; |
| 106 | uint32_t offset = 0; |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 107 | void *ret = NULL; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 108 | |
| 109 | #if DYNAMIC_SMEM |
| 110 | smem_addr = smem_get_base_addr(); |
| 111 | #else |
| 112 | smem_addr = platform_get_smem_base_addr(); |
| 113 | #endif |
| 114 | smem = (struct smem *)smem_addr; |
| 115 | |
| 116 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 117 | return ret; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 118 | |
| 119 | ainfo = &smem->alloc_info[type]; |
| 120 | if (readl(&ainfo->allocated) == 0) |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 121 | return ret; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 122 | |
| 123 | *size = readl(&ainfo->size); |
| 124 | base_ext = readl(&ainfo->base_ext); |
| 125 | offset = readl(&ainfo->offset); |
| 126 | |
| 127 | if(base_ext) |
| 128 | { |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 129 | ret = (void*)base_ext + offset; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
| 133 | ret = (void*) smem_addr + offset; |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 139 | unsigned |
| 140 | smem_read_alloc_entry_offset(smem_mem_type_t type, void *buf, int len, |
| 141 | int offset) |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 142 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 143 | struct smem_alloc_info *ainfo; |
| 144 | unsigned *dest = buf; |
| 145 | unsigned src; |
| 146 | unsigned size = len; |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 147 | uint32_t smem_addr = 0; |
| 148 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 149 | #if DYNAMIC_SMEM |
| 150 | smem_addr = smem_get_base_addr(); |
| 151 | #else |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 152 | smem_addr = platform_get_smem_base_addr(); |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 153 | #endif |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 154 | |
| 155 | smem = (struct smem *)smem_addr; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 156 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 157 | if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0)) |
| 158 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 159 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
| 161 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 162 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 163 | ainfo = &smem->alloc_info[type]; |
| 164 | if (readl(&ainfo->allocated) == 0) |
| 165 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 166 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 167 | src = smem_addr + readl(&ainfo->offset) + offset; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 168 | for (; size > 0; src += 4, size -= 4) |
| 169 | *(dest++) = readl(src); |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 170 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 171 | return 0; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 172 | } |