blob: dc1b1415a182025fbe452e2bac10dba13798f93f [file] [log] [blame]
Dima Zavina404bce2009-01-26 12:32:22 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Sundarajan Srinivasan66951e52014-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 Srinivasan66951e52014-03-28 16:43:06 -070038static struct smem *smem;
Dima Zavina404bce2009-01-26 12:32:22 -080039
Ajay Dudanic78b36f2010-07-08 19:34:06 -070040/* buf MUST be 4byte aligned, and len MUST be a multiple of 8. */
Dima Zavina404bce2009-01-26 12:32:22 -080041unsigned smem_read_alloc_entry(smem_mem_type_t type, void *buf, int len)
42{
43 struct smem_alloc_info *ainfo;
44 unsigned *dest = buf;
45 unsigned src;
46 unsigned size;
Sundarajan Srinivasan66951e52014-03-28 16:43:06 -070047 uint32_t smem_addr = 0;
48
49 smem_addr = platform_get_smem_base_addr();
50
51 smem = (struct smem *)smem_addr;
Dima Zavina404bce2009-01-26 12:32:22 -080052
53 if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0))
54 return 1;
55
56 if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE)
57 return 1;
58
59 /* TODO: Use smem spinlocks */
60 ainfo = &smem->alloc_info[type];
61 if (readl(&ainfo->allocated) == 0)
62 return 1;
63
Ajay Dudanic78b36f2010-07-08 19:34:06 -070064 size = readl(&ainfo->size);
65
66 if (size != (unsigned)((len + 7) & ~0x00000007))
Dima Zavina404bce2009-01-26 12:32:22 -080067 return 1;
68
Sundarajan Srinivasan66951e52014-03-28 16:43:06 -070069 src = smem_addr + readl(&ainfo->offset);
Ajay Dudanic78b36f2010-07-08 19:34:06 -070070 for (; len > 0; src += 4, len -= 4)
Dima Zavina404bce2009-01-26 12:32:22 -080071 *(dest++) = readl(src);
72
73 return 0;
74}
Chandan Uddaraju1434a602010-03-08 17:13:38 -080075
Ajay Dudanib01e5062011-12-03 23:23:42 -080076unsigned
77smem_read_alloc_entry_offset(smem_mem_type_t type, void *buf, int len,
78 int offset)
Chandan Uddaraju1434a602010-03-08 17:13:38 -080079{
Ajay Dudanib01e5062011-12-03 23:23:42 -080080 struct smem_alloc_info *ainfo;
81 unsigned *dest = buf;
82 unsigned src;
83 unsigned size = len;
Sundarajan Srinivasan66951e52014-03-28 16:43:06 -070084 uint32_t smem_addr = 0;
85
86 smem_addr = platform_get_smem_base_addr();
87
88 smem = (struct smem *)smem_addr;
Chandan Uddaraju1434a602010-03-08 17:13:38 -080089
Ajay Dudanib01e5062011-12-03 23:23:42 -080090 if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0))
91 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -080092
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE)
94 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -080095
Ajay Dudanib01e5062011-12-03 23:23:42 -080096 ainfo = &smem->alloc_info[type];
97 if (readl(&ainfo->allocated) == 0)
98 return 1;
Chandan Uddaraju1434a602010-03-08 17:13:38 -080099
Sundarajan Srinivasan66951e52014-03-28 16:43:06 -0700100 src = smem_addr + readl(&ainfo->offset) + offset;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800101 for (; size > 0; src += 4, size -= 4)
102 *(dest++) = readl(src);
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800103
Ajay Dudanib01e5062011-12-03 23:23:42 -0800104 return 0;
Chandan Uddaraju1434a602010-03-08 17:13:38 -0800105}