Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
lijuang | 7b1b742 | 2017-12-27 17:49:38 +0800 | [diff] [blame] | 5 | * Copyright (c) 2014-2015, 2017, The Linux Foundation. All rights reserved. |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 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> |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 35 | #include <board.h> |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 36 | |
| 37 | #include "smem.h" |
| 38 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 39 | static struct smem *smem; |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 40 | |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 41 | const char *hw_platform[] = { |
| 42 | [HW_PLATFORM_UNKNOWN] = "Unknown", |
| 43 | [HW_PLATFORM_SURF] = "Surf", |
| 44 | [HW_PLATFORM_FFA] = "FFA", |
| 45 | [HW_PLATFORM_FLUID] = "Fluid", |
| 46 | [HW_PLATFORM_SVLTE] = "SVLTE", |
| 47 | [HW_PLATFORM_MTP_MDM] = "MDM_MTP_NO_DISPLAY", |
| 48 | [HW_PLATFORM_MTP] = "MTP", |
| 49 | [HW_PLATFORM_RCM] = "RCM", |
| 50 | [HW_PLATFORM_LIQUID] = "Liquid", |
| 51 | [HW_PLATFORM_DRAGON] = "Dragon", |
| 52 | [HW_PLATFORM_QRD] = "QRD", |
Zhu Bin | 576c064 | 2016-11-17 18:18:02 +0800 | [diff] [blame] | 53 | [HW_PLATFORM_IPC] = "IPC", |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 54 | [HW_PLATFORM_HRD] = "HRD", |
| 55 | [HW_PLATFORM_DTV] = "DTV", |
| 56 | [HW_PLATFORM_STP] = "STP", |
| 57 | [HW_PLATFORM_SBC] = "SBC", |
Mayank Grover | 03b48ed | 2018-05-17 14:48:29 +0530 | [diff] [blame] | 58 | [HW_PLATFORM_ATP] = "ATP", |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 61 | /* DYNAMIC SMEM REGION feature enables LK to dynamically |
| 62 | * read the SMEM addr info from TCSR register or IMEM location. |
| 63 | * The first word read, if indicates a MAGIC number, then |
| 64 | * Dynamic SMEM is assumed to be enabled. Read the remaining |
| 65 | * SMEM info for SMEM Size and Phy_addr from the other bytes. |
| 66 | */ |
| 67 | |
| 68 | #if DYNAMIC_SMEM |
| 69 | uint32_t smem_get_base_addr() |
| 70 | { |
| 71 | struct smem_addr_info *smem_info = NULL; |
| 72 | |
| 73 | smem_info = (struct smem_addr_info *) SMEM_TARG_INFO_ADDR; |
| 74 | if(smem_info && (smem_info->identifier == SMEM_TARGET_INFO_IDENTIFIER)) |
| 75 | return smem_info->phy_addr; |
| 76 | else |
| 77 | return MSM_SHARED_BASE; |
| 78 | } |
| 79 | #endif |
| 80 | |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 81 | /* 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] | 82 | unsigned smem_read_alloc_entry(smem_mem_type_t type, void *buf, int len) |
| 83 | { |
| 84 | struct smem_alloc_info *ainfo; |
| 85 | unsigned *dest = buf; |
| 86 | unsigned src; |
| 87 | unsigned size; |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 88 | uint32_t smem_addr = 0; |
| 89 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 90 | #if DYNAMIC_SMEM |
| 91 | smem_addr = smem_get_base_addr(); |
| 92 | #else |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 93 | smem_addr = platform_get_smem_base_addr(); |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 94 | #endif |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 95 | |
| 96 | smem = (struct smem *)smem_addr; |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 97 | |
| 98 | if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0)) |
| 99 | return 1; |
| 100 | |
| 101 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
| 102 | return 1; |
| 103 | |
| 104 | /* TODO: Use smem spinlocks */ |
| 105 | ainfo = &smem->alloc_info[type]; |
| 106 | if (readl(&ainfo->allocated) == 0) |
| 107 | return 1; |
| 108 | |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 109 | size = readl(&ainfo->size); |
| 110 | |
Sridhar Parasuram | d63ea9c | 2014-10-22 12:39:11 -0700 | [diff] [blame] | 111 | if (size < (unsigned)((len + 7) & ~0x00000007)) |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 112 | return 1; |
| 113 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 114 | src = smem_addr + readl(&ainfo->offset); |
Ajay Dudani | c78b36f | 2010-07-08 19:34:06 -0700 | [diff] [blame] | 115 | for (; len > 0; src += 4, len -= 4) |
Dima Zavin | a404bce | 2009-01-26 12:32:22 -0800 | [diff] [blame] | 116 | *(dest++) = readl(src); |
| 117 | |
| 118 | return 0; |
| 119 | } |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 120 | |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 121 | /* Return a pointer to smem_item with size */ |
| 122 | void* smem_get_alloc_entry(smem_mem_type_t type, uint32_t* size) |
| 123 | { |
| 124 | struct smem_alloc_info *ainfo = NULL; |
| 125 | uint32_t smem_addr = 0; |
| 126 | uint32_t base_ext = 0; |
| 127 | uint32_t offset = 0; |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 128 | void *ret = NULL; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 129 | |
| 130 | #if DYNAMIC_SMEM |
| 131 | smem_addr = smem_get_base_addr(); |
| 132 | #else |
| 133 | smem_addr = platform_get_smem_base_addr(); |
| 134 | #endif |
| 135 | smem = (struct smem *)smem_addr; |
| 136 | |
| 137 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 138 | return ret; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 139 | |
| 140 | ainfo = &smem->alloc_info[type]; |
| 141 | if (readl(&ainfo->allocated) == 0) |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 142 | return ret; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 143 | |
| 144 | *size = readl(&ainfo->size); |
| 145 | base_ext = readl(&ainfo->base_ext); |
| 146 | offset = readl(&ainfo->offset); |
| 147 | |
| 148 | if(base_ext) |
| 149 | { |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 150 | ret = (void*)base_ext + offset; |
Sundarajan Srinivasan | 28106e3 | 2014-07-24 16:39:58 -0700 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | { |
| 154 | ret = (void*) smem_addr + offset; |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | unsigned |
| 161 | smem_read_alloc_entry_offset(smem_mem_type_t type, void *buf, int len, |
| 162 | int offset) |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 163 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 164 | struct smem_alloc_info *ainfo; |
| 165 | unsigned *dest = buf; |
| 166 | unsigned src; |
| 167 | unsigned size = len; |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 168 | uint32_t smem_addr = 0; |
| 169 | |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 170 | #if DYNAMIC_SMEM |
| 171 | smem_addr = smem_get_base_addr(); |
| 172 | #else |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 173 | smem_addr = platform_get_smem_base_addr(); |
Channagoud Kadabi | 9e57488 | 2014-06-24 16:15:23 -0700 | [diff] [blame] | 174 | #endif |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 175 | |
| 176 | smem = (struct smem *)smem_addr; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 177 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 178 | if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0)) |
| 179 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 180 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 181 | if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE) |
| 182 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 183 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 184 | ainfo = &smem->alloc_info[type]; |
| 185 | if (readl(&ainfo->allocated) == 0) |
| 186 | return 1; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 187 | |
Sundarajan Srinivasan | 79e5f51 | 2014-03-28 16:43:06 -0700 | [diff] [blame] | 188 | src = smem_addr + readl(&ainfo->offset) + offset; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 189 | for (; size > 0; src += 4, size -= 4) |
| 190 | *(dest++) = readl(src); |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 191 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 192 | return 0; |
Chandan Uddaraju | 1434a60 | 2010-03-08 17:13:38 -0800 | [diff] [blame] | 193 | } |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 194 | |
| 195 | size_t smem_get_hw_platform_name(void *buf, uint32 buf_size) |
| 196 | { |
| 197 | uint32 hw_id; |
| 198 | |
| 199 | if (buf == NULL) { |
| 200 | dprintf(CRITICAL, "ERROR: buf is NULL\n"); |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | hw_id = board_hardware_id(); |
lijuang | 7b1b742 | 2017-12-27 17:49:38 +0800 | [diff] [blame] | 205 | if (hw_id >= ARRAY_SIZE(hw_platform) || hw_platform[hw_id] == '\0') |
| 206 | return 1; |
| 207 | |
lijuang | d117505 | 2015-08-13 16:41:12 +0800 | [diff] [blame] | 208 | if (buf_size < strlen(hw_platform[hw_id]) + 1) |
| 209 | return 1; |
| 210 | |
| 211 | return snprintf(buf, strlen(hw_platform[hw_id]) + 1, |
| 212 | "%s\n", hw_platform[hw_id]); |
| 213 | } |