1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17
18 *   * Neither the name of Cavium Inc. nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22
23 * This Software, including technical data, may be subject to U.S. export  control
24 * laws, including the U.S. Export Administration Act and its  associated
25 * regulations, and may be subject to export or import  regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41
42
43
44
45
46/**
47 * @file
48 *
49 * This module provides system/board/application information obtained by the bootloader.
50 *
51 * <hr>$Revision: 70030 $<hr>
52 *
53 */
54
55#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
56#include <linux/module.h>
57
58#include <asm/octeon/cvmx.h>
59#include <asm/octeon/cvmx-spinlock.h>
60#include <asm/octeon/cvmx-sysinfo.h>
61#else
62#include "cvmx.h"
63#include "cvmx-spinlock.h"
64#include "cvmx-sysinfo.h"
65#endif
66
67
68/**
69 * This structure defines the private state maintained by sysinfo module.
70 *
71 */
72#if defined(CVMX_BUILD_FOR_UBOOT) && CONFIG_OCTEON_NAND_STAGE2
73/* For u-boot, put this in the text section so that we can use this in early
74** boot when running from ram(or L2 cache).  This is primarily used for NAND
75** access during NAND boot.   The 'data_in_text' section is merged with the
76** text section by the linker script to avoid an assembler warning. */
77static struct {
78
79    cvmx_sysinfo_t   sysinfo;      /**< system information */
80    cvmx_spinlock_t  lock;         /**< mutex spinlock */
81
82} state __attribute__ ((section (".data_in_text"))) = {
83    .lock = CVMX_SPINLOCK_UNLOCKED_INITIALIZER
84};
85#else
86CVMX_SHARED static struct {
87
88    struct cvmx_sysinfo   sysinfo;      /**< system information */
89    cvmx_spinlock_t  lock;         /**< mutex spinlock */
90
91} state = {
92    .lock = CVMX_SPINLOCK_UNLOCKED_INITIALIZER
93};
94#endif
95
96#ifdef CVMX_BUILD_FOR_LINUX_USER
97/* Global variable with the processor ID since we can't read it directly */
98CVMX_SHARED uint32_t cvmx_app_init_processor_id;
99#endif
100
101/* Global variables that define the min/max of the memory region set up for 32 bit userspace access */
102uint64_t linux_mem32_min = 0;
103uint64_t linux_mem32_max = 0;
104uint64_t linux_mem32_wired = 0;
105uint64_t linux_mem32_offset = 0;
106
107/**
108 * This function returns the application information as obtained
109 * by the bootloader.  This provides the core mask of the cores
110 * running the same application image, as well as the physical
111 * memory regions available to the core.
112 *
113 * @return  Pointer to the boot information structure
114 *
115 */
116struct cvmx_sysinfo *cvmx_sysinfo_get(void)
117{
118    return &(state.sysinfo);
119}
120
121void cvmx_sysinfo_add_self_to_core_mask(void)
122{
123    int core = cvmx_get_core_num();
124    uint32_t core_mask = 1 << core;
125
126    cvmx_spinlock_lock(&state.lock);
127    state.sysinfo.core_mask = state.sysinfo.core_mask | core_mask;
128    cvmx_spinlock_unlock(&state.lock);
129}
130
131void cvmx_sysinfo_remove_self_from_core_mask(void)
132{
133    int core = cvmx_get_core_num();
134    uint32_t core_mask = 1 << core;
135
136    cvmx_spinlock_lock(&state.lock);
137    state.sysinfo.core_mask = state.sysinfo.core_mask & ~core_mask;
138    cvmx_spinlock_unlock(&state.lock);
139}
140
141#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
142EXPORT_SYMBOL(cvmx_sysinfo_get);
143#endif
144
145
146/**
147 * This function is used in non-simple executive environments (such as Linux kernel, u-boot, etc.)
148 * to configure the minimal fields that are required to use
149 * simple executive files directly.
150 *
151 * Locking (if required) must be handled outside of this
152 * function
153 *
154 * @param phy_mem_desc_addr
155 *                   Address of the global physical memory descriptor (bootmem
156 *                   descriptor)
157 * @param board_type Octeon board type enumeration
158 *
159 * @param board_rev_major
160 *                   Board major revision
161 * @param board_rev_minor
162 *                   Board minor revision
163 * @param cpu_clock_hz
164 *                   CPU clock freqency in hertz
165 *
166 * @return 0: Failure
167 *         1: success
168 */
169int cvmx_sysinfo_minimal_initialize(uint64_t phy_mem_desc_addr, uint16_t board_type, uint8_t board_rev_major,
170                                    uint8_t board_rev_minor, uint32_t cpu_clock_hz)
171{
172
173
174    memset(&(state.sysinfo), 0x0, sizeof(state.sysinfo));
175    state.sysinfo.phy_mem_desc_addr = phy_mem_desc_addr;
176    state.sysinfo.board_type = board_type;
177    state.sysinfo.board_rev_major = board_rev_major;
178    state.sysinfo.board_rev_minor = board_rev_minor;
179    state.sysinfo.cpu_clock_hz = cpu_clock_hz;
180
181    return(1);
182}
183
184#ifdef CVMX_BUILD_FOR_LINUX_USER
185/**
186 * Initialize the sysinfo structure when running on
187 * Octeon under Linux userspace
188 */
189void cvmx_sysinfo_linux_userspace_initialize(void)
190{
191    cvmx_sysinfo_t *system_info = cvmx_sysinfo_get();
192    memset(system_info, 0, sizeof(cvmx_sysinfo_t));
193
194    system_info->core_mask = 0;
195    system_info->init_core = -1;
196
197    FILE *infile = fopen("/proc/octeon_info", "r");
198    if (infile == NULL)
199    {
200        perror("Error opening /proc/octeon_info");
201        exit(-1);
202    }
203
204    while (!feof(infile))
205    {
206        char buffer[80];
207        if (fgets(buffer, sizeof(buffer), infile))
208        {
209            const char *field = strtok(buffer, " ");
210            const char *valueS = strtok(NULL, " ");
211            if (field == NULL)
212                continue;
213            if (valueS == NULL)
214                continue;
215            unsigned long long value;
216            sscanf(valueS, "%lli", &value);
217
218            if (strcmp(field, "dram_size:") == 0)
219                system_info->system_dram_size = value << 20;
220            else if (strcmp(field, "phy_mem_desc_addr:") == 0)
221                system_info->phy_mem_desc_addr = value;
222            else if (strcmp(field, "eclock_hz:") == 0)
223                system_info->cpu_clock_hz = value;
224            else if (strcmp(field, "dclock_hz:") == 0)
225                system_info->dram_data_rate_hz = value * 2;
226            else if (strcmp(field, "board_type:") == 0)
227                system_info->board_type = value;
228            else if (strcmp(field, "board_rev_major:") == 0)
229                system_info->board_rev_major = value;
230            else if (strcmp(field, "board_rev_minor:") == 0)
231                system_info->board_rev_minor = value;
232            else if (strcmp(field, "board_serial_number:") == 0)
233                strncpy(system_info->board_serial_number, valueS, sizeof(system_info->board_serial_number));
234            else if (strcmp(field, "mac_addr_base:") == 0)
235            {
236                int i;
237                int m[6];
238                sscanf(valueS, "%02x:%02x:%02x:%02x:%02x:%02x", m+0, m+1, m+2, m+3, m+4, m+5);
239                for (i=0; i<6; i++)
240                    system_info->mac_addr_base[i] = m[i];
241            }
242            else if (strcmp(field, "mac_addr_count:") == 0)
243                system_info->mac_addr_count = value;
244            else if (strcmp(field, "fdt_addr:") == 0)
245                system_info->fdt_addr = UNMAPPED_PTR(value);
246            else if (strcmp(field, "32bit_shared_mem_base:") == 0)
247                linux_mem32_min = value;
248            else if (strcmp(field, "32bit_shared_mem_size:") == 0)
249                linux_mem32_max = linux_mem32_min + value - 1;
250            else if (strcmp(field, "processor_id:") == 0)
251                cvmx_app_init_processor_id = value;
252            else if (strcmp(field, "32bit_shared_mem_wired:") == 0)
253                linux_mem32_wired = value;
254        }
255    }
256
257    /*
258     * set up the feature map.
259     */
260    octeon_feature_init();
261
262    system_info->cpu_clock_hz = cvmx_clock_get_rate(CVMX_CLOCK_CORE);
263}
264#endif
265