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