summaryrefslogtreecommitdiff
path: root/usr/src/cmd/picl/plugins/sun4u/grover/memcfg/piclmemcfg.c
blob: 0277cb3c4a961547f92bc8bbfa0982b282eac87b (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
/*
 * 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) 2000-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 * This plugin creates memory configuration nodes and properties in the
 * PICL tree for Grover/Grover+ platform.
 *
 * Subtree of memory-controller in the physical aspect.
 * memory-controller --- memory-module
 * However, there is no memory controller node on Grover. Thus we need to
 * create it under platform.
 *
 * Subtree of memory in the logical aspect.
 * memory --- memory-segment
 * Add property _memory-module_ at memory-segment referring to the
 * memory-module since memory-segment equals to memory-module on Grover.
 *
 * Undo strategy:
 * Create all nodes and properties, or none if it fails in physical and
 * logical memory tree respectively. It keeps on creating logical
 * memory tree although it falis on physical logical tree, but no link to
 * memory module.
 *
 * NOTE:
 * It depends on PICL devtree plugin.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <alloca.h>
#include <syslog.h>
#include <string.h>
#include <libintl.h>
#include <picl.h>
#include <picltree.h>
#include <sys/types.h>
#include <sys/obpdefs.h>
#include "piclmemcfg.h"
#include "memcfg_impl.h"

static	void	piclmemcfg_register(void);
static	void	piclmemcfg_init(void);
static	void	piclmemcfg_fini(void);

#pragma	init(piclmemcfg_register)

static picld_plugin_reg_t  my_reg_info = {
	PICLD_PLUGIN_VERSION_1,
	PICLD_PLUGIN_NON_CRITICAL,
	"SUNW_piclmemcfg",
	piclmemcfg_init,
	piclmemcfg_fini
};

/*
 * Create logical memory tree
 * memory --- memory-segment
 */
static int
create_logical_tree(picl_nodehdl_t memh, mmodinfo_t *mmodinfo)
{
	picl_nodehdl_t		msegh;
	picl_nodehdl_t		*memsegh;
	ptree_propinfo_t	propinfo;
	uint32_t		ifactor = INTERLEAVEFACTOR;
	int			i;
	int			err = PICL_SUCCESS;

	if ((memsegh = alloca(sizeof (picl_nodehdl_t) * TOTAL_MEM_SLOTS)) ==
	    NULL)
		return (PICL_FAILURE);

	for (i = 0; i < TOTAL_MEM_SLOTS; i++) {
		/*
		 * It means no segment for the slot if size is zero
		 */
		if (mmodinfo[i].size == 0) {
			memsegh[i] = 0;
			continue;
		}

		/*
		 * Create memory-segment node under memory
		 */
		err = ptree_create_and_add_node(memh, PICL_NAME_MEMORY_SEGMENT,
		    PICL_CLASS_MEMORY_SEGMENT, &msegh);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * For undo easily later
		 */
		memsegh[i] = msegh;

		/*
		 * Add property, Size to memory-segment node
		 */
		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
		    sizeof (mmodinfo[i].size), PICL_PROP_SIZE, NULL, NULL);
		if (err != PICL_SUCCESS)
			break;

		err = ptree_create_and_add_prop(msegh, &propinfo,
		    &mmodinfo[i].size, NULL);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * Add property, BaseAddress to memory-segment node
		 */
		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
		    sizeof (mmodinfo[i].base), PICL_PROP_BASEADDRESS, NULL,
		    NULL);
		if (err != PICL_SUCCESS)
			break;

		err = ptree_create_and_add_prop(msegh, &propinfo,
		    &mmodinfo[i].base, NULL);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * Add property, InterleaveFactor to memory-segment node
		 */
		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (ifactor),
		    PICL_PROP_INTERLEAVE_FACTOR, NULL, NULL);
		if (err != PICL_SUCCESS)
			break;

		err = ptree_create_and_add_prop(msegh, &propinfo, &ifactor,
		    NULL);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * Add reference property to the memory module if memory
		 * module node handle is not NULL.
		 */
		if (mmodinfo[i].memmodh == 0)
			continue;

		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
		    PICL_REFPROP_MEMORY_MODULE, NULL, NULL);
		if (err != PICL_SUCCESS)
			break;

		err = ptree_create_and_add_prop(msegh, &propinfo,
		    &mmodinfo[i].memmodh, NULL);
		if (err != PICL_SUCCESS)
			break;
	}

	if (err != PICL_SUCCESS) {
		/*
		 * Undo in the logical memory tree
		 */
		for (i = 0; i < TOTAL_MEM_SLOTS; i++) {
			if (memsegh[i] == 0)
				continue;

			(void) ptree_delete_node(memsegh[i]);
			(void) ptree_destroy_node(memsegh[i]);
		}
	}

	return (err);
}

/*
 * Create physical memory tree
 * memory-controller --- memory-module
 */
static int
create_physical_tree(picl_nodehdl_t plfh, mmodinfo_t *mmodinfo)
{
	picl_nodehdl_t		mch, memmodh;
	ptree_propinfo_t	propinfo;
	int			i;
	int			err = PICL_SUCCESS;
	uint32_t		id;

	/*
	 * Create memory-controller node under platform
	 */
	err = ptree_create_and_add_node(plfh, PICL_NAME_MEMORY_CONTROLLER,
	    PICL_CLASS_MEMORY_CONTROLLER, &mch);
	if (err != PICL_SUCCESS)
		return (err);

	/*
	 * Create memory-module nodes and properties
	 * Get all memory modules with dimm
	 */
	for (i = 0; i < TOTAL_MEM_SLOTS; i++) {
		/*
		 * It means no dimm on the slot if size is zero
		 */
		if (mmodinfo[i].size == 0)
			continue;

		/* Create memory-module node under memory-controller */
		err = ptree_create_and_add_node(mch, PICL_NAME_MEMORY_MODULE,
		    PICL_CLASS_MEMORY_MODULE, &memmodh);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * Update memory module node handle at mmodinfo
		 */
		mmodinfo[i].memmodh = memmodh;

		/*
		 * Add property, Size to memory-module node
		 */
		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
		    sizeof (mmodinfo[i].size), PICL_PROP_SIZE, NULL, NULL);
		if (err != PICL_SUCCESS)
			break;

		err = ptree_create_and_add_prop(memmodh, &propinfo,
		    &mmodinfo[i].size, NULL);
		if (err != PICL_SUCCESS)
			break;

		/*
		 * Add property, ID to memory-module node
		 */
		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
		    PICL_PTYPE_INT, PICL_READ, sizeof (id), PICL_PROP_ID,
		    NULL, NULL);
		if (err != PICL_SUCCESS)
			break;

		id = i;
		err = ptree_create_and_add_prop(memmodh, &propinfo, &id, NULL);
		if (err != PICL_SUCCESS)
			break;
	}

	if (err != PICL_SUCCESS) {
		/*
		 * Clear out the saved memory module node handle so that
		 * logical memory tree won't link to memory module.
		 */
		for (i = 0; i < TOTAL_MEM_SLOTS; i++)
			mmodinfo[i].memmodh = 0;

		/*
		 * Undo in the physical memory tree
		 */
		(void) ptree_delete_node(mch);
		(void) ptree_destroy_node(mch);
	}

	return (err);
}

/*
 * Get the memory module and memory segment information from
 * property reg of memory node.
 *
 * mmodinfo will be updated. Also, the pointers to mseginfo and
 * the number of segments will be passed to the caller.
 */
static int
get_reg_info(picl_nodehdl_t plfh, picl_nodehdl_t memh,
    mmodinfo_t *mmodinfo)
{
	picl_prophdl_t		proph;
	ptree_propinfo_t	pinfo;
	regspec_t		*memspec;
	int			i, err;
	int			pval;
	int			nregspec;


	/*
	 * Check if the #size-cells of the platform node is 2
	 */
	err = ptree_get_propval_by_name(plfh, OBP_PROP_SIZE_CELLS, &pval,
	    sizeof (pval));

	if (err == PICL_PROPNOTFOUND)
		pval = SUPPORTED_NUM_CELL_SIZE;
	else if (err != PICL_SUCCESS)
		return (err);

	/*
	 * don't know to handle other vals
	 */
	if (pval != SUPPORTED_NUM_CELL_SIZE)
		return (PICL_FAILURE);

	/*
	 * Get property reg of memory node
	 */
	err = ptree_get_prop_by_name(memh, OBP_REG, &proph);
	if (err != PICL_SUCCESS)
		return (err);

	err = ptree_get_propinfo(proph, &pinfo);
	if (err != PICL_SUCCESS)
		return (err);

	if ((memspec = alloca(pinfo.piclinfo.size)) == NULL)
		return (PICL_FAILURE);

	nregspec = pinfo.piclinfo.size / sizeof (*memspec);

	if ((nregspec == 0) || (nregspec > TOTAL_MEM_SLOTS))
		return (PICL_FAILURE);

	err = ptree_get_propval(proph, memspec, pinfo.piclinfo.size);
	if (err != PICL_SUCCESS)
		return (err);


	for (i = 0; i < nregspec; i++) {

		mmodinfo[i].base = memspec[i].physaddr;
		mmodinfo[i].size = memspec[i].size;

	}

	return (PICL_SUCCESS);
}

/*
 * executed as part of .init when the plugin is dlopen()ed
 */
static void
piclmemcfg_register(void)
{
	(void) picld_plugin_register(&my_reg_info);
}

/*
 * Init entry point of the plugin
 * Creates the PICL nodes and properties in the physical and logical aspects.
 */
static void
piclmemcfg_init(void)
{
	picl_nodehdl_t		plfh, memh;
	mmodinfo_t		mmodinfo[TOTAL_MEM_SLOTS];

	/*
	 * Get platform node
	 */
	if ((ptree_get_node_by_path(PLATFORM_PATH, &plfh)) != PICL_SUCCESS) {
		syslog(LOG_ERR, EM_INIT_FAILED);
		return;
	}

	/*
	 * Find the memory node
	 */
	if ((ptree_get_node_by_path(MEMORY_PATH, &memh)) != PICL_SUCCESS) {
		syslog(LOG_ERR, EM_INIT_FAILED);
		return;
	}

	/*
	 * Initialize the mmodinfo and get segment information from reg
	 */
	(void) memset(mmodinfo, 0, sizeof (mmodinfo));

	if ((get_reg_info(plfh, memh, mmodinfo)) != PICL_SUCCESS) {
		syslog(LOG_ERR, EM_INIT_FAILED);
		return;
	}

	/*
	 * Create subtree of memory-controller in the physical aspect.
	 * memory-controller --- memory-module
	 */
	if ((create_physical_tree(plfh, mmodinfo)) != PICL_SUCCESS)
		syslog(LOG_ERR, EM_PHYSIC_MEM_TREE_FAILED);

	/*
	 * Create subtree of memory in the logical aspect.
	 * memory --- memory-segment
	 */
	if ((create_logical_tree(memh, mmodinfo)) != PICL_SUCCESS)
		syslog(LOG_ERR, EM_LOGIC_MEM_TREE_FAILED);
}

/*
 * fini entry point of the plugin
 */
static void
piclmemcfg_fini(void)
{
}