summaryrefslogtreecommitdiff
path: root/usr/src/lib/libprtdiag/common/memory.c
blob: 575f94e956806b5ed968d31f37ba1b47f7f57043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 * Copyright (c) 2020 Peter Tribble.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <kvm.h>
#include <varargs.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/openpromio.h>
#include <kstat.h>
#include <libintl.h>
#include <syslog.h>
#include <sys/dkio.h>
#include <sys/sbd_ioctl.h>
#include <sys/sbdp_mem.h>
#include <sys/serengeti.h>
#include <sys/mc.h>
#include "pdevinfo.h"
#include "display.h"
#include "pdevinfo_sun4u.h"
#include "display_sun4u.h"
#include "libprtdiag.h"

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif

#define	KBYTE	1024
#define	MBYTE	(KBYTE * KBYTE)

#define	MEM_UK_SIZE_MASK	0x3FF

/*
 * Global variables.
 */
static memory_bank_t	*bank_head;
static memory_bank_t	*bank_tail;
static memory_seg_t	*seg_head;

/*
 * Local functions.
 */
static void add_bank_node(uint64_t mc_decode, int portid, char *bank_status);
static void add_seg_node(void);
static memory_seg_t *match_seg(uint64_t);


/*
 * Used for US-I and US-II systems
 */
/*ARGSUSED0*/
void
display_memorysize(Sys_tree *tree, struct system_kstat_data *kstats,
	struct mem_total *memory_total)
{
	log_printf(dgettext(TEXT_DOMAIN, "Memory size: "), 0);

	if (sysconf(_SC_PAGESIZE) == -1 || sysconf(_SC_PHYS_PAGES) == -1)
		log_printf(dgettext(TEXT_DOMAIN, "unable to determine\n"), 0);
	else {
		uint64_t	mem_size;

		mem_size =
		    (uint64_t)sysconf(_SC_PAGESIZE) * \
			(uint64_t)sysconf(_SC_PHYS_PAGES);

		if (mem_size >= MBYTE)
			log_printf(dgettext(TEXT_DOMAIN, "%d Megabytes\n"),
				(int)((mem_size+MBYTE-1) / MBYTE), 0);
		else
			log_printf(dgettext(TEXT_DOMAIN, "%d Kilobytes\n"),
				(int)((mem_size+KBYTE-1) / KBYTE), 0);
	}
}

/*ARGSUSED0*/
void
display_memoryconf(Sys_tree *tree)
{
	/*
	 * This function is intentionally blank
	 */
}

/*
 * The following functions are for use by any US-III based systems.
 * All they need to do is to call get_us3_mem_regs()
 * and then display_us3_banks(). Each platform then needs to decide how
 * to format this data by over-riding the generic function
 * print_us3_memory_line().
 */
int
get_us3_mem_regs(Board_node *bnode)
{
	Prom_node	*pnode;
	int		portid;
	uint64_t	*ma_reg_arr;
	uint64_t	madr[NUM_MBANKS_PER_MC];
	void		*bank_status_array;
	char		*bank_status;
	int		i, status_offset;

	for (pnode = dev_find_node(bnode->nodes, "memory-controller");
		pnode != NULL;
		pnode = dev_next_node(pnode, "memory-controller")) {

		/* Get portid of this mc from libdevinfo. */
		portid = (*(int *)get_prop_val(find_prop(pnode, "portid")));

		/* read the logical_bank_ma_regs property for this mc node. */
		ma_reg_arr = (uint64_t *)get_prop_val(
				find_prop(pnode, MEM_CFG_PROP_NAME));

		/*
		 * There are situations where a memory-controller node
		 * will not have the logical_bank_ma_regs property and
		 * we need to allow for these cases. They include:
		 *	- Excalibur/Littleneck systems that only
		 *	  support memory on one of their CPUs.
		 *	- Systems that support DR where a cpu board
		 *	  can be unconfigured but still connected.
		 * It is up to the caller of this function to ensure
		 * that the bank_head and seg_head pointers are not
		 * NULL after processing all memory-controllers in the
		 * system. This would indicate a situation where no
		 * memory-controllers in the system have a logical_bank_ma_regs
		 * property which should never happen.
		 */
		if (ma_reg_arr == NULL)
			continue;

		/*
		 * The first NUM_MBANKS_PER_MC of uint64_t's in the
		 * logical_bank_ma_regs property are the madr values.
		 */
		for (i = 0; i < NUM_MBANKS_PER_MC; i++) {
			madr[i] = *ma_reg_arr++;
		}

		/*
		 * Get the bank_status property for this mem controller from
		 * OBP. This contains the bank-status for each logical bank.
		 */
		bank_status_array = (void *)get_prop_val(
				find_prop(pnode, "bank-status"));
		status_offset = 0;

		/*
		 * process each logical bank
		 */
		for (i = 0; i < NUM_MBANKS_PER_MC; i++) {
			/*
			 * Get the bank-status string for this bank
			 * from the bank_status_array we just retrieved
			 * from OBP. If the prop was not found, we
			 * malloc a bank_status and set it to "no_status".
			 */
			if (bank_status_array) {
				bank_status = ((char *)bank_status_array +
				    status_offset);

				/* Move offset to next bank_status string */
				status_offset += (strlen(bank_status) + 1);
			} else {
				bank_status = malloc(strlen("no_status"));
				strcpy(bank_status, "no_status");
			}

			/*
			 * create a bank_node for this bank
			 * and add it to the list.
			 */
			add_bank_node(madr[i], portid, bank_status);

			/*
			 * find the segment to which this bank
			 * belongs. If it doesn't already exist
			 * then create it. If it exists, add to it.
			 */
			add_seg_node();
		}
	}
	return (0);
}

static void
add_bank_node(uint64_t mc_decode, int portid, char *bank_status)
{
	static int	id = 0;
	memory_bank_t	*new, *bank;
	uint32_t	ifactor = MC_INTLV(mc_decode);
	uint64_t	seg_size;

	if ((new = malloc(sizeof (memory_bank_t))) == NULL) {
		perror("malloc");
		exit(1);
	}

	new->portid = portid;
	new->id = id++;
	new->valid = (mc_decode >> 63);
	new->uk = MC_UK(mc_decode);
	new->um = MC_UM(mc_decode);
	new->lk = MC_LK(mc_decode);
	new->lm = MC_LM(mc_decode);

	seg_size = ((((uint64_t)new->uk & MEM_UK_SIZE_MASK) + 1) << 26);
	new->bank_size = seg_size / ifactor;
	new->bank_status = bank_status;

	new->next = NULL;
	new->seg_next = NULL;

	/* Handle the first bank found */
	if (bank_head == NULL) {
		bank_head = new;
		bank_tail = new;
		return;
	}

	/* find last bank in list */
	bank = bank_head;
	while (bank->next)
		bank = bank->next;

	/* insert this bank into the list */
	bank->next = new;
	bank_tail = new;
}

void
display_us3_banks(void)
{
	uint64_t	base, bank_size;
	uint32_t	intlv;
	memory_bank_t	*bank, *tmp_bank;
	memory_seg_t	*seg;
	int		 mcid;
	uint64_t	dimm_size;
	uint64_t	total_bank_size = 0;
	uint64_t	total_sys_mem;
	static uint64_t	bank0_size, bank1_size, bank2_size, bank3_size;

	if ((bank_head == NULL) || (seg_head == NULL)) {
		log_printf("\nCannot find any memory bank/segment info.\n");
		return;
	}

	for (bank = bank_head; bank; bank = bank->next) {
		/*
		 * Interleave factor is determined from the
		 * lk bits in the Mem Addr Decode register.
		 *
		 * The Base Address of the memory segment in which this
		 * bank belongs is determined from the um abd uk bits
		 * of the Mem Addr Decode register.
		 *
		 * See section 9.1.5 of Cheetah Programmer's reference
		 * manual.
		 */
		intlv 		= ((bank->lk ^ 0xF) + 1);
		base 		= bank->um & ~(bank->uk);

		mcid 		= SG_PORTID_TO_SAFARI_ID(bank->portid);

		/* If bank is not valid, set size to zero incase it's garbage */
		if (bank->valid)
			bank_size = ((bank->bank_size) / MBYTE);
		else
			bank_size = 0;

		/*
		 * Keep track of all banks found so we can check later
		 * that this value matches the total memory in the
		 * system using the pagesize and number of pages.
		 */
		total_bank_size	+= bank_size;

		/* Find the matching segment for this bank. */
		seg = match_seg(base);

		/*
		 * Find the Dimm size by adding banks 0 + 2 and divide by 4
		 * and then adding banks 1 + 3 and divide by 4. We divide
		 * by 2 if one of the logical banks size is zero.
		 */
		switch ((bank->id) % 4) {
		case 0:
			/* have bank0_size, need bank2_size */
			bank0_size = bank_size;
			bank2_size = 0;

			tmp_bank = bank->next;
			while (tmp_bank) {
				if (tmp_bank->valid == 0) {
					tmp_bank = tmp_bank->next;
					continue;
				}
				/* Is next bank on the same mc ? */
				if (mcid != SG_PORTID_TO_SAFARI_ID(
				    tmp_bank->portid)) {
					break;
				}
				if ((tmp_bank->id) % 4 == 2) {
					bank2_size =
					    (tmp_bank->bank_size / MBYTE);
					break;
				}
				tmp_bank = tmp_bank->next;
			}
			if (bank2_size)
				dimm_size = (bank0_size + bank2_size) / 4;
			else
				dimm_size = bank0_size / 2;
			break;
		case 1:
			/* have bank1_size, need bank3_size */
			bank1_size = bank_size;
			bank3_size = 0;

			tmp_bank = bank->next;
			while (tmp_bank) {
				if (tmp_bank->valid == 0) {
					tmp_bank = tmp_bank->next;
					continue;
				}
				/* Is next bank on the same mc ? */
				if (mcid != SG_PORTID_TO_SAFARI_ID(
				    tmp_bank->portid)) {
					break;
				}
				if ((tmp_bank->id) % 4 == 3) {
					bank3_size =
					    (tmp_bank->bank_size / MBYTE);
					break;
				}
				tmp_bank = tmp_bank->next;
			}
			if (bank3_size)
				dimm_size = (bank1_size + bank3_size) / 4;
			else
				dimm_size = bank1_size / 2;
			break;
		case 2:
			/* have bank0_size and bank2_size */
			bank2_size = bank_size;
			if (bank0_size)
				dimm_size = (bank0_size + bank2_size) / 4;
			else
				dimm_size = bank2_size / 2;
			break;
		case 3:
			/* have bank1_size and bank3_size */
			bank3_size = bank_size;
			if (bank1_size)
				dimm_size = (bank1_size + bank3_size) / 4;
			else
				dimm_size = bank3_size / 4;
			break;
		}

		if (bank->valid == 0)
			continue;

		/*
		 * Call platform specific code for formatting memory
		 * information.
		 */
		print_us3_memory_line(bank->portid, bank->id, bank_size,
		    bank->bank_status, dimm_size, intlv, seg->id);
	}

	printf("\n");

	/*
	 * Sanity check to ensure that the total amount of system
	 * memory matches the total number of memory banks that
	 * we find here. Scream if there is a mis-match.
	 */
	total_sys_mem = (((uint64_t)sysconf(_SC_PAGESIZE) * \
		(uint64_t)sysconf(_SC_PHYS_PAGES)) / MBYTE);

	if (total_bank_size != total_sys_mem) {
		log_printf(dgettext(TEXT_DOMAIN,
		    "\nError: total bank size [%lldMB] does not match total "
			"system memory [%lldMB]\n"), total_bank_size,
				total_sys_mem, 0);
	}

}

static void
add_seg_node(void)
{
	uint64_t	base;
	memory_seg_t	*new;
	static int	id = 0;
	memory_bank_t	*bank = bank_tail;

	if (bank->valid != 1)
		return;

	base = bank->um & ~(bank->uk);

	if ((new = match_seg(base)) == NULL) {
		/*
		 * This bank is part of a new segment, so create
		 * a struct for it and added to the list of segments
		 */
		if ((new = malloc(sizeof (memory_seg_t))) == NULL) {
			perror("malloc");
			exit(1);
		}
		new->id = id++;
		new->base = base;
		new->size = (((uint64_t)bank->uk +1) << 26);
		new->intlv = ((bank->lk ^ 0xF) + 1);

		/*
		 * add to the seg list
		 */
		new->next = seg_head;
		seg_head = new;
	}

	new->nbanks++;
	/*
	 * add bank into segs bank list.  Note we add at the head
	 */
	bank->seg_next = new->banks;
	new->banks = bank;
}

static memory_seg_t *
match_seg(uint64_t base)
{
	memory_seg_t	*cur_seg;

	for (cur_seg = seg_head; cur_seg; cur_seg = cur_seg->next) {
		if (cur_seg-> base == base)
			break;
	}
	return (cur_seg);
}

/*ARGSUSED0*/
void
print_us3_memory_line(int portid, int bank_id, uint64_t bank_size,
    char *bank_status, uint64_t dimm_size, uint32_t intlv, int seg_id)
{
	log_printf(dgettext(TEXT_DOMAIN,
	    "\n No print_us3_memory_line() function specified for"
	    " this platform\n"), 0);
}

int
display_us3_failed_banks(int system_failed)
{
	memory_bank_t	*bank;
	int		found_failed_bank = 0;

	if ((bank_head == NULL) || (seg_head == NULL)) {
		log_printf("\nCannot find any memory bank/segment info.\n");
		return (1);
	}

	for (bank = bank_head; bank; bank = bank->next) {
		/*
		 * check to see if the bank is invalid and also
		 * check if the bank_status is unpopulated.  Unpopulated
		 * means the bank is empty.
		 */

		if ((bank->valid == 0) &&
		    (strcmp(bank->bank_status, "unpopulated"))) {
			if (!system_failed && !found_failed_bank) {
				found_failed_bank = TRUE;
				log_printf("\n", 0);
				log_printf(dgettext(TEXT_DOMAIN,
				"Failed Field Replaceable Units (FRU) in "
				    "System:\n"), 0);
				log_printf("=========================="
				    "====================\n", 0);
			}
			/*
			 * Call platform specific code for formatting memory
			 * information.
			 */
			print_us3_failed_memory_line(bank->portid, bank->id,
			    bank->bank_status);
		}
	}
	if (found_failed_bank)
		return (1);
	else
		return (0);
}

/*ARGSUSED0*/
void
print_us3_failed_memory_line(int portid, int bank_id, char *bank_status)
{
	log_printf(dgettext(TEXT_DOMAIN,
	    "\n No print_us3_failed_memory_line() function specified for"
	    " this platform\n"), 0);
}