1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Networks (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 Networks 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  NETWORKS 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 * @file
43 *
44 * Support library for the hardware Free Pool Allocator.
45 *
46 * <hr>$Revision: 49448 $<hr>
47 *
48 */
49
50#include "cvmx.h"
51#include "cvmx-fpa.h"
52#include "cvmx-ipd.h"
53
54/**
55 * Current state of all the pools. Use access functions
56 * instead of using it directly.
57 */
58CVMX_SHARED cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS];
59
60
61/**
62 * Setup a FPA pool to control a new block of memory. The
63 * buffer pointer must be a physical address.
64 *
65 * @param pool       Pool to initialize
66 *                   0 <= pool < 8
67 * @param name       Constant character string to name this pool.
68 *                   String is not copied.
69 * @param buffer     Pointer to the block of memory to use. This must be
70 *                   accessable by all processors and external hardware.
71 * @param block_size Size for each block controlled by the FPA
72 * @param num_blocks Number of blocks
73 *
74 * @return 0 on Success,
75 *         -1 on failure
76 */
77int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer,
78                         uint64_t block_size, uint64_t num_blocks)
79{
80    char *ptr;
81    if (!buffer)
82    {
83        cvmx_dprintf("ERROR: cvmx_fpa_setup_pool: NULL buffer pointer!\n");
84        return -1;
85    }
86    if (pool >= CVMX_FPA_NUM_POOLS)
87    {
88        cvmx_dprintf("ERROR: cvmx_fpa_setup_pool: Illegal pool!\n");
89        return -1;
90    }
91
92    if (block_size < CVMX_FPA_MIN_BLOCK_SIZE)
93    {
94        cvmx_dprintf("ERROR: cvmx_fpa_setup_pool: Block size too small.\n");
95        return -1;
96    }
97
98    if (((unsigned long)buffer & (CVMX_FPA_ALIGNMENT-1)) != 0)
99    {
100        cvmx_dprintf("ERROR: cvmx_fpa_setup_pool: Buffer not aligned properly.\n");
101        return -1;
102    }
103
104    cvmx_fpa_pool_info[pool].name = name;
105    cvmx_fpa_pool_info[pool].size = block_size;
106    cvmx_fpa_pool_info[pool].starting_element_count = num_blocks;
107    cvmx_fpa_pool_info[pool].base = buffer;
108
109    ptr = (char*)buffer;
110    while (num_blocks--)
111    {
112        cvmx_fpa_free(ptr, pool, 0);
113        ptr += block_size;
114    }
115    return 0;
116}
117
118/**
119 * Shutdown a Memory pool and validate that it had all of
120 * the buffers originally placed in it. This should only be
121 * called by one processor after all hardware has finished
122 * using the pool. Most like you will want to have called
123 * cvmx_helper_shutdown_packet_io_global() before this
124 * function to make sure all FPA buffers are out of the packet
125 * IO hardware.
126 *
127 * @param pool   Pool to shutdown
128 *
129 * @return Zero on success
130 *         - Positive is count of missing buffers
131 *         - Negative is too many buffers or corrupted pointers
132 */
133uint64_t cvmx_fpa_shutdown_pool(uint64_t pool)
134{
135    int errors = 0;
136    int count  = 0;
137    int expected_count = cvmx_fpa_pool_info[pool].starting_element_count;
138    uint64_t base   = cvmx_ptr_to_phys(cvmx_fpa_pool_info[pool].base);
139    uint64_t finish = base + cvmx_fpa_pool_info[pool].size * expected_count;
140
141    count = 0;
142    while (1)
143    {
144        uint64_t address;
145        void *ptr = cvmx_fpa_alloc(pool);
146        if (!ptr)
147            break;
148
149        address = cvmx_ptr_to_phys(ptr);
150        if ((address >= base) && (address < finish) &&
151            (((address - base) % cvmx_fpa_pool_info[pool].size) == 0))
152        {
153            count++;
154        }
155        else
156        {
157            cvmx_dprintf("ERROR: cvmx_fpa_shutdown_pool: Illegal address 0x%llx in pool %s(%d)\n",
158                   (unsigned long long)address, cvmx_fpa_pool_info[pool].name, (int)pool);
159            errors++;
160        }
161    }
162
163    if (count < expected_count)
164    {
165        cvmx_dprintf("ERROR: cvmx_fpa_shutdown_pool: Pool %s(%d) missing %d buffers\n",
166               cvmx_fpa_pool_info[pool].name, (int)pool, expected_count - count);
167    }
168    else if (count > expected_count)
169    {
170        cvmx_dprintf("ERROR: cvmx_fpa_shutdown_pool: Pool %s(%d) had %d duplicate buffers\n",
171               cvmx_fpa_pool_info[pool].name, (int)pool, count - expected_count);
172    }
173
174    if (errors)
175    {
176        cvmx_dprintf("ERROR: cvmx_fpa_shutdown_pool: Pool %s(%d) started at 0x%llx, ended at 0x%llx, with a step of 0x%x\n",
177               cvmx_fpa_pool_info[pool].name, (int)pool, (unsigned long long)base, (unsigned long long)finish, (int)cvmx_fpa_pool_info[pool].size);
178        return -errors;
179    }
180    else
181        return expected_count - count;
182}
183
184uint64_t cvmx_fpa_get_block_size(uint64_t pool)
185{
186    switch (pool)
187    {
188        case 0:
189	    return CVMX_FPA_POOL_0_SIZE;
190        case 1:
191	    return CVMX_FPA_POOL_1_SIZE;
192        case 2:
193	    return CVMX_FPA_POOL_2_SIZE;
194        case 3:
195	    return CVMX_FPA_POOL_3_SIZE;
196        case 4:
197	    return CVMX_FPA_POOL_4_SIZE;
198        case 5:
199	    return CVMX_FPA_POOL_5_SIZE;
200        case 6:
201	    return CVMX_FPA_POOL_6_SIZE;
202        case 7:
203	    return CVMX_FPA_POOL_7_SIZE;
204        default:
205	    return 0;
206    }
207}
208