Debian Codegraph @

src/vendorcode/cavium/bdk/libdram/libdram.c

raw symbols SCIP
1 /***********************license start***********************************
2 * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). 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
24 * control laws, including the U.S. Export Administration Act and its
25 * associated regulations, and may be subject to export or import
26 * regulations in other 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
31 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
32 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
33 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
34 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
35 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
36 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK
37 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40 #include <bdk.h>
41 #include <libbdk-arch/bdk-csrs-mio_fus.h>
42 #include <libbdk-dram/bdk-dram-config.h>
43 #include <libbdk-hal/bdk-config.h>
44 #include <libbdk-hal/bdk-gpio.h>
45 #include <libbdk-hal/bdk-l2c.h>
46 #include <libbdk-hal/bdk-utils.h>
47 #include <libbdk-os/bdk-init.h>
48 #include <libdram/libdram-config.h>
49 #include "dram-internal.h"
50 #include <stddef.h> /* for NULL */
51 #include <lame_string.h> /* for strtol() and friends */
52
53
54 /* This global variable is accessed through dram_is_verbose() to determine
55 the verbosity level. Use that function instead of setting it directly */
56 dram_verbosity_t dram_verbosity = VBL_OFF; /* init this here so we could set a non-zero default */
57
58 static uint32_t measured_ddr_hertz[BDK_NUMA_MAX_NODES];
59
60 /* The various DRAM configs in the libdram/configs directory need space
61 to store the DRAM config. Since only one config is ever in active use
62 at a time, store the configs in __libdram_global_cfg. In a multi-node
63 setup, independent calls to get the DRAM config will load first node 0's
64 config, then node 1's */
65 dram_config_t __libdram_global_cfg;
66
67 static void bdk_dram_clear_mem(bdk_node_t node)
68 {
69 uint64_t mbytes = bdk_dram_get_size_mbytes(node);
70 uint64_t skip = (node == bdk_numa_master()) ? bdk_dram_get_top_of_bdk() : 0;
71 uint64_t len = (mbytes << 20) - skip;
72
73 BDK_TRACE(DRAM, "N%d: Clearing DRAM\n", node);
74 if (skip)
75 {
76 /* All memory below skip may contain valid data, so we can't clear
77 it. We still need to make sure all cache lines in this area are
78 fully dirty so that ECC bits will be updated on store. A single
79 write to the cache line isn't good enough because partial LMC
80 writes may be enabled */
81 ddr_print("N%d: Rewriting DRAM: start 0 length 0x%llx\n", node, skip);
82 volatile uint64_t *ptr = bdk_phys_to_ptr(bdk_numa_get_address(node, 0));
83 uint64_t *end = bdk_phys_to_ptr(bdk_numa_get_address(node, skip));
84 while (ptr < end)
85 {
86 *ptr = *ptr;
87 ptr++;
88 }
89 }
90 ddr_print("N%d: Clearing DRAM: start 0x%llx length 0x%llx\n", node, skip, len);
91 bdk_zero_memory(bdk_phys_to_ptr(bdk_numa_get_address(node, skip)), len);
92 BDK_TRACE(DRAM, "N%d: DRAM clear complete\n", node);
93 }
94
95 static void bdk_dram_clear_ecc(bdk_node_t node)
96 {
97 /* Clear any DRAM errors set during init */
98 BDK_TRACE(DRAM, "N%d: Clearing LMC ECC errors\n", node);
99 int num_lmc = __bdk_dram_get_num_lmc(node);
100 for (int lmc = 0; lmc < num_lmc; lmc++) {
101 DRAM_CSR_WRITE(node, BDK_LMCX_INT(lmc), BDK_CSR_READ(node, BDK_LMCX_INT(lmc)));
102 }
103 }
104
105 static void bdk_dram_enable_ecc_reporting(bdk_node_t node)
106 {
107 /* Enable LMC ECC error HW reporting */
108 int num_lmc = __bdk_dram_get_num_lmc(node);
109
110 BDK_TRACE(DRAM, "N%d: Enable LMC ECC error reporting\n", node);
111
112 for (int lmc = 0; lmc < num_lmc; lmc++) {
113
114 // NOTE: this must be done for pass 2.x
115 // enable ECC interrupts to allow ECC error info in LMCX_INT
116 if (! CAVIUM_IS_MODEL(CAVIUM_CN88XX_PASS1_X)) { // added 81xx and 83xx
117 DRAM_CSR_WRITE(node, BDK_LMCX_INT_ENA_W1S(lmc), -1ULL);
118 BDK_CSR_INIT(lmc_int_ena_w1s, node, BDK_LMCX_INT_ENA_W1S(lmc));
119 ddr_print("N%d.LMC%d: %-36s : 0x%08llx\n",
120 node, lmc, "LMC_INT_ENA_W1S", lmc_int_ena_w1s.u);
121 }
122 }
123 }
124
125 static void bdk_dram_disable_ecc_reporting(bdk_node_t node)
126 {
127 /* Disable LMC ECC error HW reporting */
128 int num_lmc = __bdk_dram_get_num_lmc(node);
129
130 BDK_TRACE(DRAM, "N%d: Disable LMC ECC error reporting\n", node);
131
132 for (int lmc = 0; lmc < num_lmc; lmc++) {
133
134 // NOTE: this must be done for pass 2.x
135 // disable ECC interrupts to prevent ECC error info in LMCX_INT
136 if (! CAVIUM_IS_MODEL(CAVIUM_CN88XX_PASS1_X)) { // added 81xx and 83xx
137 DRAM_CSR_WRITE(node, BDK_LMCX_INT_ENA_W1C(lmc), -1ULL);
138 BDK_CSR_INIT(lmc_int_ena_w1c, node, BDK_LMCX_INT_ENA_W1C(lmc));
139 ddr_print("N%d.LMC%d: %-36s : 0x%08llx\n",
140 node, lmc, "LMC_INT_ENA_W1C", lmc_int_ena_w1c.u);
141 }
142 }
143 }
144
145 // this routine simply makes the calls to the tuning routines and returns any errors
146 static int bdk_libdram_tune_node(int node)
147 {
148 int errs, tot_errs;
149 int do_dllro_hw = CONFIG(CAVIUM_BDK_DDR_TUNE_HW_OFFSETS);
150 int do_dllwo = CONFIG(CAVIUM_BDK_DDR_TUNE_WRITE_OFFSETS);
151 int do_eccdll = CONFIG(CAVIUM_BDK_DDR_TUNE_ECC_ENABLE);
152 BDK_CSR_INIT(lmc_config, node, BDK_LMCX_CONFIG(0)); // FIXME: probe LMC0
153 do_eccdll = (lmc_config.s.ecc_ena != 0); // change to ON if ECC enabled
154
155 // Automatically tune the data byte DLL read offsets
156 // always done by default, but allow use of HW-assist
157 // NOTE: HW-assist will also tune the ECC byte
158
159 BDK_TRACE(DRAM, "N%d: Starting DLL Read Offset Tuning for LMCs\n", node);
160 if (!do_dllro_hw || (lmc_config.s.mode32b != 0)) {
161 errs = perform_dll_offset_tuning(node, 2, /* tune */1);
162 } else {
163 errs = perform_HW_dll_offset_tuning(node, /* read */2, 0x0A/* all bytelanes */);
164 }
165 BDK_TRACE(DRAM, "N%d: Finished DLL Read Offset Tuning for LMCs, %d errors)\n",
166 node, errs);
167 tot_errs = errs;
168
169 // disabled by default for now, does not seem to be needed?
170 // Automatically tune the data byte DLL write offsets
171 // allow override of default setting
172 if (do_dllwo) {
173 BDK_TRACE(DRAM, "N%d: Starting DLL Write Offset Tuning for LMCs\n", node);
174 errs = perform_dll_offset_tuning(node, /* write */1, /* tune */1);
175 BDK_TRACE(DRAM, "N%d: Finished DLL Write Offset Tuning for LMCs, %d errors)\n",
176 node, errs);
177 tot_errs += errs;
178 }
179
180 // disabled by default for now, does not seem to be needed much?
181 // Automatically tune the ECC byte DLL read offsets
182 // FIXME? allow override of the filtering
183 // FIXME? allow programmatic override, not via envvar?
184 if (do_eccdll && !do_dllro_hw && (lmc_config.s.mode32b == 0)) { // do not do HW-assist twice for ECC
185 BDK_TRACE(DRAM, "N%d: Starting ECC DLL Read Offset Tuning for LMCs\n", node);
186 errs = perform_HW_dll_offset_tuning(node, 2, 8/* ECC bytelane */);
187 BDK_TRACE(DRAM, "N%d: Finished ECC DLL Read Offset Tuning for LMCs, %d errors\n",
188 node, errs);
189 tot_errs += errs;
190 }
191
192 return tot_errs;
193 }
194
195 // this routine makes the calls to the tuning routines when criteria are met
196 // intended to be called for automated tuning, to apply filtering...
197
198 #define IS_DDR4 1
199 #define IS_DDR3 0
200 #define IS_RDIMM 1
201 #define IS_UDIMM 0
202 #define IS_1SLOT 1
203 #define IS_2SLOT 0
204
205 // FIXME: DDR3 is not tuned
206 static const uint32_t ddr_speed_filter[2][2][2] = {
207 [IS_DDR4] = {
208 [IS_RDIMM] = {
209 [IS_1SLOT] = 940,
210 [IS_2SLOT] = 800
211 },
212 [IS_UDIMM] = {
213 [IS_1SLOT] = 1050,
214 [IS_2SLOT] = 940
215 },
216 },
217 [IS_DDR3] = {
218 [IS_RDIMM] = {
219 [IS_1SLOT] = 0, // disabled
220 [IS_2SLOT] = 0 // disabled
221 },
222 [IS_UDIMM] = {
223 [IS_1SLOT] = 0, // disabled
224 [IS_2SLOT] = 0 // disabled
225 }
226 }
227 };
228
229 static int bdk_libdram_maybe_tune_node(int node)
230 {
231 const char *str;
232
233 // FIXME: allow an override here so that all configs can be tuned or none
234 // If the envvar is defined, always either force it or avoid it accordingly
235 if ((str = getenv("ddr_tune_all_configs")) != NULL) {
236 int tune_it = !!strtoul(str, NULL, 0);
237 printf("N%d: DRAM auto-tuning %s.\n", node, (tune_it) ? "forced" : "avoided");
238 return (tune_it) ? bdk_libdram_tune_node(node) : 0;
239 }
240
241 // filter the tuning calls here...
242 // determine if we should/can run automatically for this configuration
243 //
244 // FIXME: tune only when the configuration indicates it will help:
245 // DDR type, RDIMM or UDIMM, 1-slot or 2-slot, and speed
246 //
247 uint32_t ddr_speed = divide_nint(libdram_get_freq_from_pll(node, 0), 1000000); // sample LMC0
248 BDK_CSR_INIT(lmc_config, node, BDK_LMCX_CONFIG(0)); // sample LMC0
249
250 int is_ddr4 = !!__bdk_dram_is_ddr4(node, 0);
251 int is_rdimm = !!__bdk_dram_is_rdimm(node, 0);
252 int is_1slot = !!(lmc_config.s.init_status < 4); // HACK, should do better
253 int do_tune = 0;
254
255 uint32_t ddr_min_speed = ddr_speed_filter[is_ddr4][is_rdimm][is_1slot];
256 do_tune = (ddr_min_speed && (ddr_speed > ddr_min_speed));
257
258 ddr_print("N%d: DDR%d %cDIMM %d-slot at %d MHz %s eligible for auto-tuning.\n",
259 node, (is_ddr4)?4:3, (is_rdimm)?'R':'U', (is_1slot)?1:2,
260 ddr_speed, (do_tune)?"is":"is not");
261
262 // call the tuning routines, done filtering...
263 return ((do_tune) ? bdk_libdram_tune_node(node) : 0);
264 }
265
266 /**
267 * This is the main DRAM init function. Users of libdram should call this function,
268 * avoiding the other internal function. As a rule, functions starting with
269 * "libdram_*" are part of the external API and should be used.
270 *
271 * @param node Node to initialize. This may not be the same node as the one running the code
272 * @param dram_config
273 * DRAM configuration to use
274 * @param ddr_clock_override
275 * If non-zeo, this overrides the DRAM clock speed in the config structure. This
276 * allows quickly testing of different DRAM speeds without modifying the basic
277 * config. If zero, the DRAM speed in the config is used.
278 *
279 * @return Amount of memory in MB. Zero or negative is a failure.
280 */
281 int libdram_config(int node, const dram_config_t *dram_config, int ddr_clock_override)
282 {
283 /* Boards may need to mux the TWSI connection between THUNDERX and the BMC.
284 This allows the BMC to monitor DIMM temeratures and health */
285 int gpio_select = bdk_config_get_int(BDK_CONFIG_DRAM_CONFIG_GPIO);
286 if (gpio_select != -1)
287 bdk_gpio_initialize(bdk_numa_master(), gpio_select, 1, 1);
288
289 /* Read all the SPDs and store them in the device tree. They are needed by
290 later software to populate SMBIOS information */
291 for (int lmc = 0; lmc < 4; lmc++)
292 for (int dimm = 0; dimm < DDR_CFG_T_MAX_DIMMS; dimm++)
293 read_entire_spd(node, (dram_config_t *)dram_config, lmc, dimm);
294
295 const ddr_configuration_t *ddr_config = dram_config->config;
296 int ddr_clock_hertz = (ddr_clock_override) ? ddr_clock_override : dram_config->ddr_clock_hertz;
297 if (ddr_clock_hertz == 0) // 0 == AUTO
298 {
299 ddr_clock_hertz = dram_get_default_spd_speed(node, ddr_config);
300 if (ddr_clock_hertz < 0) {
301 printf("N%d: DRAM init: AUTO clock ILLEGAL configuration\n", node);
302 return -1;
303 }
304 }
305 int errs;
306
307 // At this point, we only know the desired clock rate (ddr_clock_hertz).
308 // We do not know whether we are configuring RDIMMs.
309 // We also do not yet know if 100MHz alternate refclk is actually available.
310 // so, if we are being asked for 2133MT/s or better, we still need to do:
311 // 1. probe for RDIMMs (if not, 50MHz refclk is good enough)
312 // 2. determine if 100MHz refclk is there, and switch to it before starting any configuration
313 //
314 // NOTES:
315 // 1. dclk_alt_refclk_sel need only be set on LMC0 (see above disabled code)
316 // 2. I think we need to first probe to see if we need it, and configure it then if dictated use
317 // 3. then go on to configure at the selected refclk
318 int ddr_refclk_hertz = bdk_clock_get_rate(node, BDK_CLOCK_MAIN_REF);
319 int alt_refclk = bdk_config_get_int(BDK_CONFIG_DDR_ALT_REFCLK, node);
320
321 char *str = getenv("ddr_100mhz_refclk");
322 if (str) { // if the envvar was found, force it to that setting
323 int do_100mhz = !!strtoul(str, NULL, 0);
324 alt_refclk = (do_100mhz) ? 100 : 50;
325 }
326
327 dram_verbosity = bdk_config_get_int(BDK_CONFIG_DRAM_VERBOSE);
328
329 // Here we check for fuses that limit the number of LMCs we can configure,
330 // but only on 83XX and 88XX...
331 int lmc_limit = 4;
332 if (CAVIUM_IS_MODEL(CAVIUM_CN88XX) || CAVIUM_IS_MODEL(CAVIUM_CN83XX)) {
333 BDK_CSR_INIT(mio_fus_dat2, node, BDK_MIO_FUS_DAT2);
334 if (mio_fus_dat2.s.lmc_half) {
335 lmc_limit = (CAVIUM_IS_MODEL(CAVIUM_CN88XX)) ? 2 : 1; // limit LMCs to half present
336 error_print("Only %d LMC(s)s supported for this Thunder model\n", lmc_limit);
337 }
338 }
339
340 /* We need to calculate the interface mask based on the provided SPD
341 addresses/contents */
342 uint32_t interface_mask = 0;
343 for (int i = 0; i < lmc_limit; i++)
344 {
345 // We need to check only DIMM 0 of each LMC for possible presence of the LMC.
346 // This trusts that the board database is correctly configured.
347 // Empty DIMM slots in present LMCs will be detected later.
348 if (ddr_config[i].dimm_config_table[0].spd_addr ||
349 ddr_config[i].dimm_config_table[0].spd_ptr)
350 interface_mask |= 1 << i;
351
352 // we know whether alternate refclk is always wanted
353 // we also know already if we want 2133 MT/s
354 // if alt refclk not always wanted, then probe DDR and DIMM type
355 // if DDR4 and RDIMMs, then set desired refclk to 100MHz, otherwise to default (50MHz)
356 // depend on ddr_initialize() to do the refclk selection and validation
357 if (i == 0) { // only check for LMC0
358 if (alt_refclk) { // if alternate refclk was specified, let it override everything
359 ddr_refclk_hertz = alt_refclk * 1000000;
360 ddr_print("N%d: DRAM init: %d MHz refclk is REQUESTED ALWAYS\n", node, alt_refclk);
361 } else if (ddr_clock_hertz > 1000000000) { // if more than 2000 MT/s
362 int ddr_type = get_ddr_type(node, &ddr_config[0].dimm_config_table[0]);
363 int spd_dimm_type = get_dimm_module_type(node, &ddr_config[0].dimm_config_table[0], ddr_type);
364 // is DDR4 and RDIMM just to be sure
365 if ((ddr_type == DDR4_DRAM) &&
366 ((spd_dimm_type == 1) || (spd_dimm_type == 5) || (spd_dimm_type == 8))) {
367 ddr_refclk_hertz = 100000000; // yes, we require 100MHz refclk, so set it
368 ddr_print("N%d: DRAM init: 100 MHz refclk is REQUIRED\n", node);
369 }
370 } // if (ddr_clock_hertz > 1000000000)
371 } // if (i == 0)
372 }
373
374 BDK_TRACE(DRAM, "N%d: DRAM init started (hertz=%d, refclk=%d, config=%p)\n",
375 node, ddr_clock_hertz, ddr_refclk_hertz, dram_config);
376 debug_print("N%d: DRAM init started (hertz=%d, refclk=%d, config=%p)\n",
377 node, ddr_clock_hertz, ddr_refclk_hertz, dram_config);
378
379 BDK_TRACE(DRAM, "N%d: Calling DRAM init\n", node);
380 measured_ddr_hertz[node] = 0;
381 int mbytes = octeon_ddr_initialize(node,
382 bdk_clock_get_rate(node, BDK_CLOCK_RCLK),
383 ddr_clock_hertz,
384 ddr_refclk_hertz,
385 interface_mask,
386 ddr_config,
387 &measured_ddr_hertz[node],
388 0,
389 0,
390 0);
391 BDK_TRACE(DRAM, "N%d: DRAM init returned %d, measured %u Hz\n",
392 node, mbytes, measured_ddr_hertz[node]);
393
394 // do not tune or mess with memory if there was an init problem...
395 if (mbytes > 0) {
396
397 bdk_dram_disable_ecc_reporting(node);
398
399 // call the tuning routines, with filtering...
400 BDK_TRACE(DRAM, "N%d: Calling DRAM tuning\n", node);
401 errs = bdk_libdram_maybe_tune_node(node);
402 BDK_TRACE(DRAM, "N%d: DRAM tuning returned %d errors\n",
403 node, errs);
404
405 // finally, clear memory and any left-over ECC errors
406 bdk_dram_clear_mem(node);
407 bdk_dram_clear_ecc(node);
408
409 bdk_dram_enable_ecc_reporting(node);
410 }
411
412 /* Boards may need to mux the TWSI connection between THUNDERX and the BMC.
413 This allows the BMC to monitor DIMM temeratures and health */
414 if (gpio_select != -1)
415 bdk_gpio_initialize(bdk_numa_master(), gpio_select, 1, 0);
416
417 return mbytes;
418 }
419
420 /**
421 * This is the main DRAM tuning function. Users of libdram should call this function,
422 * avoiding the other internal function. As a rule, functions starting with
423 * "libdram_*" are part of the external API and should be used.
424 *
425 * @param node Node to tune. This may not be the same node as the one running the code
426 *
427 * @return Success or Fail
428 */
429 int libdram_tune(int node)
430 {
431 int tot_errs;
432 int l2c_is_locked = bdk_l2c_is_locked(node);
433
434 dram_verbosity = bdk_config_get_int(BDK_CONFIG_DRAM_VERBOSE);
435
436 // the only way this entry point should be called is from a MENU item,
437 // so, enable any non-running cores on this node, and leave them
438 // running at the end...
439 ddr_print("N%d: %s: Starting cores (mask was 0x%llx)\n",
440 node, __func__, bdk_get_running_coremask(node));
441 bdk_init_cores(node, ~0ULL);
442
443 // must test for L2C locked here, cannot go on with it unlocked
444 // FIXME: but we only need to worry about Node 0???
445 if (node == 0) {
446 if (!l2c_is_locked) { // is unlocked, must lock it now
447 ddr_print("N%d: %s: L2C was unlocked - locking it now\n", node, __func__);
448 // FIXME: this should be common-ized; it currently matches bdk_init()...
449 bdk_l2c_lock_mem_region(node, 0, bdk_l2c_get_cache_size_bytes(node) * 3 / 4);
450 } else {
451 ddr_print("N%d: %s: L2C was already locked - continuing\n", node, __func__);
452 }
453 } else {
454 ddr_print("N%d: %s: non-zero node, not worrying about L2C lock status\n", node, __func__);
455 }
456
457 // call the tuning routines, no filtering...
458 tot_errs = bdk_libdram_tune_node(node);
459
460 // FIXME: only for node 0, unlock L2C if it was unlocked before...
461 if (node == 0) {
462 if (!l2c_is_locked) { // it was Node 0 and unlocked, must re-unlock it now
463 ddr_print("N%d: Node 0 L2C was unlocked before - unlocking it now\n", node);
464 // FIXME: this should be common-ized; it currently matches bdk_init()...
465 bdk_l2c_unlock_mem_region(node, 0, bdk_l2c_get_cache_size_bytes(node) * 3 / 4);
466 } else {
467 ddr_print("N%d: %s: L2C was already locked - leaving it locked\n", node, __func__);
468 }
469 } else {
470 ddr_print("N%d: %s: non-zero node, not worrying about L2C lock status\n", node, __func__);
471 }
472
473 // make sure to clear memory and any ECC errs when done...
474 bdk_dram_clear_mem(node);
475 bdk_dram_clear_ecc(node);
476
477 return tot_errs;
478 }
479
480 /**
481 * This is the main function for DRAM margining of Write Voltage.
482 * Users of libdram should call this function,
483 * avoiding the other internal function. As a rule, functions starting with
484 * "libdram_*" are part of the external API and should be used.
485 *
486 * @param node Node to test. This may not be the same node as the one running the code
487 *
488 * @return Success or Fail
489 */
490 static
491 int libdram_margin_write_voltage(int node)
492 {
493 int tot_errs;
494
495 // call the margining routine
496 tot_errs = perform_margin_write_voltage(node);
497
498 // make sure to clear memory and any ECC errs when done...
499 bdk_dram_clear_mem(node);
500 bdk_dram_clear_ecc(node);
501
502 return tot_errs;
503 }
504
505 /**
506 * This is the main function for DRAM margining of Read Voltage.
507 * Users of libdram should call this function,
508 * avoiding the other internal function. As a rule, functions starting with
509 * "libdram_*" are part of the external API and should be used.
510 *
511 * @param node Node to test. This may not be the same node as the one running the code
512 *
513 * @return Success or Fail
514 */
515 static
516 int libdram_margin_read_voltage(int node)
517 {
518 int tot_errs;
519
520 // call the margining routine
521 tot_errs = perform_margin_read_voltage(node);
522
523 // make sure to clear memory and any ECC errs when done...
524 bdk_dram_clear_mem(node);
525 bdk_dram_clear_ecc(node);
526
527 return tot_errs;
528 }
529
530 /**
531 * This is the main function for DRAM margining of Write Timing.
532 * Users of libdram should call this function,
533 * avoiding the other internal function. As a rule, functions starting with
534 * "libdram_*" are part of the external API and should be used.
535 *
536 * @param node Node to test. This may not be the same node as the one running the code
537 *
538 * @return Success or Fail
539 */
540 static
541 int libdram_margin_write_timing(int node)
542 {
543 int tot_errs;
544
545 // call the tuning routine, tell it we are margining not tuning...
546 tot_errs = perform_dll_offset_tuning(node, /* write offsets */1, /* margin */0);
547
548 // make sure to clear memory and any ECC errs when done...
549 bdk_dram_clear_mem(node);
550 bdk_dram_clear_ecc(node);
551
552 return tot_errs;
553 }
554
555 /**
556 * This is the main function for DRAM margining of Read Timing.
557 * Users of libdram should call this function,
558 * avoiding the other internal function. As a rule, functions starting with
559 * "libdram_*" are part of the external API and should be used.
560 *
561 * @param node Node to test. This may not be the same node as the one running the code
562 *
563 * @return Success or Fail
564 */
565 static
566 int libdram_margin_read_timing(int node)
567 {
568 int tot_errs;
569
570 // call the tuning routine, tell it we are margining not tuning...
571 tot_errs = perform_dll_offset_tuning(node, /* read offsets */2, /* margin */0);
572
573 // make sure to clear memory and any ECC errs when done...
574 bdk_dram_clear_mem(node);
575 bdk_dram_clear_ecc(node);
576
577 return tot_errs;
578 }
579
580 /**
581 * This is the main function for all DRAM margining.
582 * Users of libdram should call this function,
583 * avoiding the other internal function. As a rule, functions starting with
584 * "libdram_*" are part of the external API and should be used.
585 *
586 * @param node Node to test. This may not be the same node as the one running the code
587 *
588 * @return Success or Fail
589 */
590 int libdram_margin(int node)
591 {
592 int ret_rt, ret_wt, ret_rv, ret_wv;
593 const char *risk[2] = { "Low Risk", "Needs Review" };
594 int l2c_is_locked = bdk_l2c_is_locked(node);
595
596 // for now, no margining on 81xx, until we can reduce the dynamic runtime size...
597 if (CAVIUM_IS_MODEL(CAVIUM_CN81XX)) {
598 printf("Sorry, margining is not available on 81xx yet...\n");
599 return 0;
600 }
601
602 dram_verbosity = bdk_config_get_int(BDK_CONFIG_DRAM_VERBOSE);
603
604 // the only way this entry point should be called is from a MENU item,
605 // so, enable any non-running cores on this node, and leave them
606 // running at the end...
607 ddr_print("N%d: %s: Starting cores (mask was 0x%llx)\n",
608 node, __func__, bdk_get_running_coremask(node));
609 bdk_init_cores(node, ~0ULL);
610
611 // must test for L2C locked here, cannot go on with it unlocked
612 // FIXME: but we only need to worry about Node 0???
613 if (node == 0) {
614 if (!l2c_is_locked) { // is unlocked, must lock it now
615 ddr_print("N%d: %s: L2C was unlocked - locking it now\n", node, __func__);
616 // FIXME: this should be common-ized; it currently matches bdk_init()...
617 bdk_l2c_lock_mem_region(node, 0, bdk_l2c_get_cache_size_bytes(node) * 3 / 4);
618 } else {
619 ddr_print("N%d: %s: L2C was already locked - continuing\n", node, __func__);
620 }
621 } else {
622 ddr_print("N%d: %s: non-zero node, not worrying about L2C lock status\n", node, __func__);
623 }
624
625 debug_print("N%d: Starting DRAM Margin ALL\n", node);
626 ret_rt = libdram_margin_read_timing(node);
627 ret_wt = libdram_margin_write_timing(node);
628 ret_rv = libdram_margin_read_voltage(node);
629 ret_wv = libdram_margin_write_voltage(node);
630 debug_print("N%d: DRAM Margin ALL finished\n", node);
631
632 /*
633 >>> Summary from DDR Margining tool:
634 >>> N0: Read Timing Margin : Low Risk
635 >>> N0: Write Timing Margin : Low Risk
636 >>> N0: Read Voltage Margin : Low Risk
637 >>> N0: Write Voltage Margin : Low Risk
638 */
639 printf(" \n");
640 printf("-------------------------------------\n");
641 printf(" \n");
642 printf("Summary from DDR Margining tool\n");
643 printf("N%d: Read Timing Margin : %s\n", node, risk[!!ret_rt]);
644 printf("N%d: Write Timing Margin : %s\n", node, risk[!!ret_wt]);
645
646 // these may not have been done due to DDR3 and/or THUNDER pass 1.x
647 // FIXME? would it be better to print an appropriate message here?
648 if (ret_rv != -1) printf("N%d: Read Voltage Margin : %s\n", node, risk[!!ret_rv]);
649 if (ret_wv != -1) printf("N%d: Write Voltage Margin : %s\n", node, risk[!!ret_wv]);
650
651 printf(" \n");
652 printf("-------------------------------------\n");
653 printf(" \n");
654
655 // FIXME: only for node 0, unlock L2C if it was unlocked before...
656 if (node == 0) {
657 if (!l2c_is_locked) { // it was Node 0 and unlocked, must re-unlock it now
658 ddr_print("N%d: Node 0 L2C was unlocked before - unlocking it now\n", node);
659 // FIXME: this should be common-ized; it currently matches bdk_init()...
660 bdk_l2c_unlock_mem_region(node, 0, bdk_l2c_get_cache_size_bytes(node) * 3 / 4);
661 } else {
662 ddr_print("N%d: %s: L2C was already locked - leaving it locked\n", node, __func__);
663 }
664 } else {
665 ddr_print("N%d: %s: non-zero node, not worrying about L2C lock status\n", node, __func__);
666 }
667
668 return 0;
669 }
670
671 /**
672 * Get the measured DRAM frequency after a call to libdram_config
673 *
674 * @param node Node to get frequency for
675 *
676 * @return Frequency in Hz
677 */
678 uint32_t libdram_get_freq(int node)
679 {
680 return measured_ddr_hertz[node];
681 }
682
683 /**
684 * Get the measured DRAM frequency from the DDR_PLL_CTL CSR
685 *
686 * @param node Node to get frequency for
687 *
688 * @return Frequency in Hz
689 */
690 uint32_t libdram_get_freq_from_pll(int node, int lmc)
691 {
692 static const uint8_t _en[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12};
693 BDK_CSR_INIT(c, node, BDK_LMCX_DDR_PLL_CTL(0));
694 // we check the alternate refclk select bit in LMC0 to indicate 100MHz use
695 // assumption: the alternate refclk is setup for 100MHz
696 uint64_t ddr_ref_hertz = (c.s.dclk_alt_refclk_sel) ? 100000000 : bdk_clock_get_rate(node, BDK_CLOCK_MAIN_REF);
697 uint64_t en = _en[c.cn83xx.ddr_ps_en];
698 uint64_t calculated_ddr_hertz = ddr_ref_hertz * (c.cn83xx.clkf + 1) / ((c.cn83xx.clkr + 1) * en);
699 return calculated_ddr_hertz;
700 }
701
702 #ifndef DRAM_CSR_WRITE_INLINE
703 void dram_csr_write(bdk_node_t node, const char *csr_name, bdk_csr_type_t type, int busnum, int size, uint64_t address, uint64_t value)
704 {
705 VB_PRT(VBL_CSRS, "N%d: DDR Config %s[%016llx] => %016llx\n", node, csr_name, address, value);
706 bdk_csr_write(node, type, busnum, size, address, value);
707 }
708 #endif