blob: 4bf9fdaee359fa6c8faded1ee8199a5a126e9505 [file] [log] [blame]
Dima Zavina404bce2009-01-26 12:32:22 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -07005 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
6 *
Dima Zavina404bce2009-01-26 12:32:22 -08007 * 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 Srinivasan79e5f512014-03-28 16:43:06 -070038static struct smem *smem;
Dima Zavina404bce2009-01-26 12:32:22 -080039
Channagoud Kadabi9e574882014-06-24 16:15:23 -070040/* 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
48uint32_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 Dudanic78b36f2010-07-08 19:34:06 -070060/* buf MUST be 4byte aligned, and len MUST be a multiple of 8. */
Dima Zavina404bce2009-01-26 12:32:22 -080061unsigned 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 Srinivasan79e5f512014-03-28 16:43:06 -070067 uint32_t smem_addr = 0;
68
Channagoud Kadabi9e574882014-06-24 16:15:23 -070069#if DYNAMIC_SMEM
70 smem_addr = smem_get_base_addr();
71#else
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -070072 smem_addr = platform_get_smem_base_addr();
Channagoud Kadabi9e574882014-06-24 16:15:23 -070073#endif
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -070074
75 smem = (struct smem *)smem_addr;
Dima Zavina404bce2009-01-26 12:32:22 -080076
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 Dudanic78b36f2010-07-08 19:34:06 -070088 size = readl(&ainfo->size);
89
90 if (size != (unsigned)((len + 7) & ~0x00000007))
Dima Zavina404bce2009-01-26 12:32:22 -080091 return 1;
92
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -070093 src = smem_addr + readl(&ainfo->offset);
Ajay Dudanic78b36f2010-07-08 19:34:06 -070094 for (; len > 0; src += 4, len -= 4)
Dima Zavina404bce2009-01-26 12:32:22 -080095 *(dest++) = readl(src);
96
97 return 0;
98}
Chandan Uddaraju1434a602010-03-08 17:13:38 -080099
Ajay Dudanib01e5062011-12-03 23:23:42 -0800100unsigned
101smem_read_alloc_entry_offset(smem_mem_type_t type, void *buf, int len,
102 int offset)
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800103{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800104 struct smem_alloc_info *ainfo;
105 unsigned *dest = buf;
106 unsigned src;
107 unsigned size = len;
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -0700108 uint32_t smem_addr = 0;
109
Channagoud Kadabi9e574882014-06-24 16:15:23 -0700110#if DYNAMIC_SMEM
111 smem_addr = smem_get_base_addr();
112#else
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -0700113 smem_addr = platform_get_smem_base_addr();
Channagoud Kadabi9e574882014-06-24 16:15:23 -0700114#endif
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -0700115
116 smem = (struct smem *)smem_addr;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800117
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0))
119 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800120
Ajay Dudanib01e5062011-12-03 23:23:42 -0800121 if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE)
122 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800123
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124 ainfo = &smem->alloc_info[type];
125 if (readl(&ainfo->allocated) == 0)
126 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800127
Sundarajan Srinivasan79e5f512014-03-28 16:43:06 -0700128 src = smem_addr + readl(&ainfo->offset) + offset;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129 for (; size > 0; src += 4, size -= 4)
130 *(dest++) = readl(src);
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800131
Ajay Dudanib01e5062011-12-03 23:23:42 -0800132 return 0;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800133}