1e3d60c9bSAdrian Frost /*
2e3d60c9bSAdrian Frost  * CDDL HEADER START
3e3d60c9bSAdrian Frost  *
4e3d60c9bSAdrian Frost  * The contents of this file are subject to the terms of the
5e3d60c9bSAdrian Frost  * Common Development and Distribution License (the "License").
6e3d60c9bSAdrian Frost  * You may not use this file except in compliance with the License.
7e3d60c9bSAdrian Frost  *
8e3d60c9bSAdrian Frost  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e3d60c9bSAdrian Frost  * or http://www.opensolaris.org/os/licensing.
10e3d60c9bSAdrian Frost  * See the License for the specific language governing permissions
11e3d60c9bSAdrian Frost  * and limitations under the License.
12e3d60c9bSAdrian Frost  *
13e3d60c9bSAdrian Frost  * When distributing Covered Code, include this CDDL HEADER in each
14e3d60c9bSAdrian Frost  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e3d60c9bSAdrian Frost  * If applicable, add the following below this CDDL HEADER, with the
16e3d60c9bSAdrian Frost  * fields enclosed by brackets "[]" replaced with your own identifying
17e3d60c9bSAdrian Frost  * information: Portions Copyright [yyyy] [name of copyright owner]
18e3d60c9bSAdrian Frost  *
19e3d60c9bSAdrian Frost  * CDDL HEADER END
20e3d60c9bSAdrian Frost  */
21e3d60c9bSAdrian Frost 
22e3d60c9bSAdrian Frost /*
23e8ee2240SAdrian Frost  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24e3d60c9bSAdrian Frost  * Use is subject to license terms.
25e3d60c9bSAdrian Frost  */
26e3d60c9bSAdrian Frost 
27*598f111bSJohn Levon /*
28*598f111bSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
29*598f111bSJohn Levon  */
30*598f111bSJohn Levon 
31e3d60c9bSAdrian Frost #include <sys/types.h>
32e3d60c9bSAdrian Frost #include <sys/time.h>
33e3d60c9bSAdrian Frost #include <sys/nvpair.h>
34e3d60c9bSAdrian Frost #include <sys/cmn_err.h>
35e3d60c9bSAdrian Frost #include <sys/cred.h>
36e3d60c9bSAdrian Frost #include <sys/open.h>
37e3d60c9bSAdrian Frost #include <sys/ddi.h>
38e3d60c9bSAdrian Frost #include <sys/sunddi.h>
39e3d60c9bSAdrian Frost #include <sys/conf.h>
40e3d60c9bSAdrian Frost #include <sys/modctl.h>
41e3d60c9bSAdrian Frost #include <sys/cyclic.h>
42e3d60c9bSAdrian Frost #include <sys/errorq.h>
43e3d60c9bSAdrian Frost #include <sys/stat.h>
44e3d60c9bSAdrian Frost #include <sys/cpuvar.h>
45e3d60c9bSAdrian Frost #include <sys/mc_intel.h>
46e3d60c9bSAdrian Frost #include <sys/mc.h>
47e3d60c9bSAdrian Frost #include <sys/fm/protocol.h>
48e3d60c9bSAdrian Frost #include "nhm_log.h"
49e3d60c9bSAdrian Frost #include "intel_nhm.h"
50e3d60c9bSAdrian Frost 
51e3d60c9bSAdrian Frost extern nvlist_t *inhm_mc_nvl[MAX_CPU_NODES];
52e3d60c9bSAdrian Frost extern char closed_page;
53e3d60c9bSAdrian Frost extern char ecc_enabled;
54e3d60c9bSAdrian Frost extern char lockstep[MAX_CPU_NODES];
55e3d60c9bSAdrian Frost extern char mirror_mode[MAX_CPU_NODES];
56e3d60c9bSAdrian Frost extern char spare_channel[MAX_CPU_NODES];
57e3d60c9bSAdrian Frost 
58e3d60c9bSAdrian Frost static void
inhm_vrank(nvlist_t * vrank,int num,uint64_t dimm_base,uint64_t limit,uint32_t sinterleave,uint32_t cinterleave,uint32_t rinterleave,uint32_t sway,uint32_t cway,uint32_t rway)59e3d60c9bSAdrian Frost inhm_vrank(nvlist_t *vrank, int num, uint64_t dimm_base, uint64_t limit,
60e3d60c9bSAdrian Frost     uint32_t sinterleave, uint32_t cinterleave, uint32_t rinterleave,
61e3d60c9bSAdrian Frost     uint32_t sway, uint32_t cway, uint32_t rway)
62e3d60c9bSAdrian Frost {
63e3d60c9bSAdrian Frost 	char buf[128];
64e3d60c9bSAdrian Frost 
65e3d60c9bSAdrian Frost 	(void) snprintf(buf, sizeof (buf), "dimm-rank-base-%d", num);
66e3d60c9bSAdrian Frost 	(void) nvlist_add_uint64(vrank, buf, dimm_base);
67e3d60c9bSAdrian Frost 	(void) snprintf(buf, sizeof (buf), "dimm-rank-limit-%d", num);
68e3d60c9bSAdrian Frost 	(void) nvlist_add_uint64(vrank, buf, dimm_base + limit);
69e3d60c9bSAdrian Frost 	if (sinterleave > 1) {
70e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf), "dimm-socket-interleave-%d",
71e3d60c9bSAdrian Frost 		    num);
72e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, sinterleave);
73e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf),
74e3d60c9bSAdrian Frost 		    "dimm-socket-interleave-way-%d", num);
75e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, sway);
76e3d60c9bSAdrian Frost 	}
77e3d60c9bSAdrian Frost 	if (cinterleave > 1) {
78e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf), "dimm-channel-interleave-%d",
79e3d60c9bSAdrian Frost 		    num);
80e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, cinterleave);
81e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf),
82e3d60c9bSAdrian Frost 		    "dimm-channel-interleave-way-%d", num);
83e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, cway);
84e3d60c9bSAdrian Frost 	}
85e3d60c9bSAdrian Frost 	if (rinterleave > 1) {
86e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf), "dimm-rank-interleave-%d",
87e3d60c9bSAdrian Frost 		    num);
88e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, rinterleave);
89e3d60c9bSAdrian Frost 		(void) snprintf(buf, sizeof (buf),
90e3d60c9bSAdrian Frost 		    "dimm-rank-interleave-way-%d", num);
91e3d60c9bSAdrian Frost 		(void) nvlist_add_uint32(vrank, buf, rway);
92e3d60c9bSAdrian Frost 	}
93e3d60c9bSAdrian Frost }
94e3d60c9bSAdrian Frost 
95e3d60c9bSAdrian Frost static void
inhm_rank(nvlist_t * newdimm,nhm_dimm_t * nhm_dimm,uint32_t node,uint8_t channel,uint32_t dimm,uint64_t rank_size)96e3d60c9bSAdrian Frost inhm_rank(nvlist_t *newdimm, nhm_dimm_t *nhm_dimm, uint32_t node,
97e3d60c9bSAdrian Frost     uint8_t channel, uint32_t dimm, uint64_t rank_size)
98e3d60c9bSAdrian Frost {
99e3d60c9bSAdrian Frost 	nvlist_t **newrank;
100e3d60c9bSAdrian Frost 	int num;
101e3d60c9bSAdrian Frost 	int i;
102e3d60c9bSAdrian Frost 	uint64_t dimm_base;
103e3d60c9bSAdrian Frost 	uint64_t vrank_sz;
104e3d60c9bSAdrian Frost 	uint64_t rank_addr;
105e3d60c9bSAdrian Frost 	uint64_t pa;
106e3d60c9bSAdrian Frost 	uint32_t sinterleave, cinterleave, rinterleave;
107e3d60c9bSAdrian Frost 	uint32_t sway, cway, rway;
108e3d60c9bSAdrian Frost 
109e3d60c9bSAdrian Frost 	newrank = kmem_zalloc(sizeof (nvlist_t *) * nhm_dimm->nranks, KM_SLEEP);
110e3d60c9bSAdrian Frost 	for (i = 0; i < nhm_dimm->nranks; i++) {
111e3d60c9bSAdrian Frost 		(void) nvlist_alloc(&newrank[i], NV_UNIQUE_NAME, KM_SLEEP);
112e3d60c9bSAdrian Frost 		rank_addr = 0;
113e3d60c9bSAdrian Frost 		num = 0;
114e3d60c9bSAdrian Frost 		while (rank_addr < rank_size) {
115e3d60c9bSAdrian Frost 			pa = dimm_to_addr(node, channel, dimm * 4 + i,
116e3d60c9bSAdrian Frost 			    rank_addr, &dimm_base, &vrank_sz, &sinterleave,
117e3d60c9bSAdrian Frost 			    &cinterleave, &rinterleave, &sway, &cway, &rway);
118e8ee2240SAdrian Frost 			if (pa == -1 || vrank_sz == 0)
119e3d60c9bSAdrian Frost 				break;
120e3d60c9bSAdrian Frost 			inhm_vrank(newrank[i], num, dimm_base,
121e3d60c9bSAdrian Frost 			    vrank_sz * sinterleave * cinterleave * rinterleave,
122e3d60c9bSAdrian Frost 			    sinterleave, cinterleave, rinterleave, sway, cway,
123e3d60c9bSAdrian Frost 			    rway);
124e3d60c9bSAdrian Frost 			rank_addr += vrank_sz;
125e3d60c9bSAdrian Frost 			num++;
126e3d60c9bSAdrian Frost 		}
127e3d60c9bSAdrian Frost 
128e3d60c9bSAdrian Frost 	}
129e3d60c9bSAdrian Frost 	(void) nvlist_add_nvlist_array(newdimm, MCINTEL_NVLIST_RANKS, newrank,
130e3d60c9bSAdrian Frost 	    nhm_dimm->nranks);
131e3d60c9bSAdrian Frost 	for (i = 0; i < nhm_dimm->nranks; i++)
132e3d60c9bSAdrian Frost 		nvlist_free(newrank[i]);
133e3d60c9bSAdrian Frost 	kmem_free(newrank, sizeof (nvlist_t *) * nhm_dimm->nranks);
134e3d60c9bSAdrian Frost }
135e3d60c9bSAdrian Frost 
136e3d60c9bSAdrian Frost static nvlist_t *
inhm_dimm(nhm_dimm_t * nhm_dimm,uint32_t node,uint8_t channel,uint32_t dimm)137e3d60c9bSAdrian Frost inhm_dimm(nhm_dimm_t *nhm_dimm, uint32_t node, uint8_t channel, uint32_t dimm)
138e3d60c9bSAdrian Frost {
139e3d60c9bSAdrian Frost 	nvlist_t *newdimm;
140e3d60c9bSAdrian Frost 	uint8_t t;
141e3d60c9bSAdrian Frost 	char sbuf[65];
142e3d60c9bSAdrian Frost 
143e3d60c9bSAdrian Frost 	(void) nvlist_alloc(&newdimm, NV_UNIQUE_NAME, KM_SLEEP);
144e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "dimm-number", dimm);
145e3d60c9bSAdrian Frost 
146e3d60c9bSAdrian Frost 	if (nhm_dimm->dimm_size >= 1024*1024*1024) {
147e3d60c9bSAdrian Frost 		(void) snprintf(sbuf, sizeof (sbuf), "%dG",
148e3d60c9bSAdrian Frost 		    (int)(nhm_dimm->dimm_size / (1024*1024*1024)));
149e3d60c9bSAdrian Frost 	} else {
150e3d60c9bSAdrian Frost 		(void) snprintf(sbuf, sizeof (sbuf), "%dM",
151e3d60c9bSAdrian Frost 		    (int)(nhm_dimm->dimm_size / (1024*1024)));
152e3d60c9bSAdrian Frost 	}
153e3d60c9bSAdrian Frost 	(void) nvlist_add_string(newdimm, "dimm-size", sbuf);
154e3d60c9bSAdrian Frost 	(void) nvlist_add_uint64(newdimm, "size", nhm_dimm->dimm_size);
155e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "nbanks", (uint32_t)nhm_dimm->nbanks);
156e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "ncolumn",
157e3d60c9bSAdrian Frost 	    (uint32_t)nhm_dimm->ncolumn);
158e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "nrow", (uint32_t)nhm_dimm->nrow);
159e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "width", (uint32_t)nhm_dimm->width);
160e3d60c9bSAdrian Frost 	(void) nvlist_add_uint32(newdimm, "ranks", (uint32_t)nhm_dimm->nranks);
161e3d60c9bSAdrian Frost 	inhm_rank(newdimm, nhm_dimm, node, channel, dimm,
162e3d60c9bSAdrian Frost 	    nhm_dimm->dimm_size / nhm_dimm->nranks);
163*598f111bSJohn Levon 	if (nhm_dimm->manufacturer[0]) {
164e3d60c9bSAdrian Frost 		t = sizeof (nhm_dimm->manufacturer);
165e3d60c9bSAdrian Frost 		(void) strncpy(sbuf, nhm_dimm->manufacturer, t);
166e3d60c9bSAdrian Frost 		sbuf[t] = 0;
167e3d60c9bSAdrian Frost 		(void) nvlist_add_string(newdimm, "manufacturer", sbuf);
168e3d60c9bSAdrian Frost 	}
169*598f111bSJohn Levon 	if (nhm_dimm->serial_number[0]) {
170e3d60c9bSAdrian Frost 		t = sizeof (nhm_dimm->serial_number);
171e3d60c9bSAdrian Frost 		(void) strncpy(sbuf, nhm_dimm->serial_number, t);
172e3d60c9bSAdrian Frost 		sbuf[t] = 0;
173e3d60c9bSAdrian Frost 		(void) nvlist_add_string(newdimm, FM_FMRI_HC_SERIAL_ID, sbuf);
174e3d60c9bSAdrian Frost 	}
175*598f111bSJohn Levon 	if (nhm_dimm->part_number[0]) {
176e3d60c9bSAdrian Frost 		t = sizeof (nhm_dimm->part_number);
177e3d60c9bSAdrian Frost 		(void) strncpy(sbuf, nhm_dimm->part_number, t);
178e3d60c9bSAdrian Frost 		sbuf[t] = 0;
179e3d60c9bSAdrian Frost 		(void) nvlist_add_string(newdimm, FM_FMRI_HC_PART, sbuf);
180e3d60c9bSAdrian Frost 	}
181*598f111bSJohn Levon 	if (nhm_dimm->revision[0]) {
182e3d60c9bSAdrian Frost 		t = sizeof (nhm_dimm->revision);
183e3d60c9bSAdrian Frost 		(void) strncpy(sbuf, nhm_dimm->revision, t);
184e3d60c9bSAdrian Frost 		sbuf[t] = 0;
185e3d60c9bSAdrian Frost 		(void) nvlist_add_string(newdimm, FM_FMRI_HC_REVISION, sbuf);
186e3d60c9bSAdrian Frost 	}
187e3d60c9bSAdrian Frost 	t = sizeof (nhm_dimm->label);
188e3d60c9bSAdrian Frost 	(void) strncpy(sbuf, nhm_dimm->label, t);
189e3d60c9bSAdrian Frost 	sbuf[t] = 0;
190e3d60c9bSAdrian Frost 	(void) nvlist_add_string(newdimm, FM_FAULT_FRU_LABEL, sbuf);
191e3d60c9bSAdrian Frost 	return (newdimm);
192e3d60c9bSAdrian Frost }
193e3d60c9bSAdrian Frost 
194e3d60c9bSAdrian Frost static void
inhm_dimmlist(uint32_t node,nvlist_t * nvl)195e3d60c9bSAdrian Frost inhm_dimmlist(uint32_t node, nvlist_t *nvl)
196e3d60c9bSAdrian Frost {
197e3d60c9bSAdrian Frost 	nvlist_t **dimmlist;
198e3d60c9bSAdrian Frost 	nvlist_t **newchannel;
199e3d60c9bSAdrian Frost 	int nchannels = CHANNELS_PER_MEMORY_CONTROLLER;
200e3d60c9bSAdrian Frost 	int nd;
201e3d60c9bSAdrian Frost 	uint8_t i, j;
202e3d60c9bSAdrian Frost 	nhm_dimm_t **dimmpp;
203e3d60c9bSAdrian Frost 	nhm_dimm_t *dimmp;
204e3d60c9bSAdrian Frost 
205e3d60c9bSAdrian Frost 	dimmlist =  kmem_zalloc(sizeof (nvlist_t *) * MAX_DIMMS_PER_CHANNEL,
206e3d60c9bSAdrian Frost 	    KM_SLEEP);
207e3d60c9bSAdrian Frost 	newchannel = kmem_zalloc(sizeof (nvlist_t *) * nchannels, KM_SLEEP);
208e3d60c9bSAdrian Frost 	dimmpp = &nhm_dimms[node * CHANNELS_PER_MEMORY_CONTROLLER *
209e3d60c9bSAdrian Frost 	    MAX_DIMMS_PER_CHANNEL];
210e3d60c9bSAdrian Frost 	(void) nvlist_add_string(nvl, "memory-policy",
211e3d60c9bSAdrian Frost 	    closed_page ? "closed-page" : "open-page");
212e3d60c9bSAdrian Frost 	(void) nvlist_add_string(nvl, "memory-ecc",
213e3d60c9bSAdrian Frost 	    ecc_enabled ? lockstep[node] ? "x8" : "x4" : "no");
214e3d60c9bSAdrian Frost 	for (i = 0; i < nchannels; i++) {
215e3d60c9bSAdrian Frost 		(void) nvlist_alloc(&newchannel[i], NV_UNIQUE_NAME, KM_SLEEP);
216e3d60c9bSAdrian Frost 		(void) nvlist_add_string(newchannel[i], "channel-mode",
217e3d60c9bSAdrian Frost 		    CHANNEL_DISABLED(MC_STATUS_RD(node), i) ? "disabled" :
218e3d60c9bSAdrian Frost 		    i != 2 && lockstep[node] ? "lockstep" :
219e3d60c9bSAdrian Frost 		    i != 2 && mirror_mode[node] ?
220e3d60c9bSAdrian Frost 		    REDUNDANCY_LOSS(MC_RAS_STATUS_RD(node)) ?
221e3d60c9bSAdrian Frost 		    "redundancy-loss" : "mirror" :
222e3d60c9bSAdrian Frost 		    i == 2 && spare_channel[node] &&
223e3d60c9bSAdrian Frost 		    !REDUNDANCY_LOSS(MC_RAS_STATUS_RD(node)) ? "spare" :
224e3d60c9bSAdrian Frost 		    "independent");
225e3d60c9bSAdrian Frost 		nd = 0;
226e3d60c9bSAdrian Frost 		for (j = 0; j < MAX_DIMMS_PER_CHANNEL; j++) {
227e3d60c9bSAdrian Frost 			dimmp = *dimmpp;
228e3d60c9bSAdrian Frost 			if (dimmp != NULL) {
229e3d60c9bSAdrian Frost 				dimmlist[nd] = inhm_dimm(dimmp, node, i,
230e3d60c9bSAdrian Frost 				    (uint32_t)j);
231e3d60c9bSAdrian Frost 				nd++;
232e3d60c9bSAdrian Frost 			}
233e3d60c9bSAdrian Frost 			dimmpp++;
234e3d60c9bSAdrian Frost 		}
235e3d60c9bSAdrian Frost 		if (nd) {
236e3d60c9bSAdrian Frost 			(void) nvlist_add_nvlist_array(newchannel[i],
237e3d60c9bSAdrian Frost 			    "memory-dimms", dimmlist, nd);
238e3d60c9bSAdrian Frost 			for (j = 0; j < nd; j++)
239e3d60c9bSAdrian Frost 				nvlist_free(dimmlist[j]);
240e3d60c9bSAdrian Frost 		}
241e3d60c9bSAdrian Frost 	}
242e3d60c9bSAdrian Frost 	(void) nvlist_add_nvlist_array(nvl, MCINTEL_NVLIST_MC, newchannel,
243e3d60c9bSAdrian Frost 	    nchannels);
244e3d60c9bSAdrian Frost 	for (i = 0; i < nchannels; i++)
245e3d60c9bSAdrian Frost 		nvlist_free(newchannel[i]);
246e3d60c9bSAdrian Frost 	kmem_free(dimmlist, sizeof (nvlist_t *) * MAX_DIMMS_PER_CHANNEL);
247e3d60c9bSAdrian Frost 	kmem_free(newchannel, sizeof (nvlist_t *) * nchannels);
248e3d60c9bSAdrian Frost }
249e3d60c9bSAdrian Frost 
250e3d60c9bSAdrian Frost char *
inhm_mc_name()251e3d60c9bSAdrian Frost inhm_mc_name()
252e3d60c9bSAdrian Frost {
253ee9ef9e5SAdrian Frost 	return (NHM_INTERCONNECT);
254e3d60c9bSAdrian Frost }
255e3d60c9bSAdrian Frost 
256e3d60c9bSAdrian Frost void
inhm_create_nvl(int chip)257e3d60c9bSAdrian Frost inhm_create_nvl(int chip)
258e3d60c9bSAdrian Frost {
259e3d60c9bSAdrian Frost 	nvlist_t *nvl;
260e3d60c9bSAdrian Frost 
261e3d60c9bSAdrian Frost 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
262e3d60c9bSAdrian Frost 	(void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_VERSTR,
263e3d60c9bSAdrian Frost 	    MCINTEL_NVLIST_VERS);
264e3d60c9bSAdrian Frost 	(void) nvlist_add_string(nvl, MCINTEL_NVLIST_MEM, inhm_mc_name());
265e3d60c9bSAdrian Frost 	(void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_NMEM, 1);
266e3d60c9bSAdrian Frost 	(void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_NRANKS, 4);
267e3d60c9bSAdrian Frost 	inhm_dimmlist(chip, nvl);
268e3d60c9bSAdrian Frost 
269aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(inhm_mc_nvl[chip]);
270e3d60c9bSAdrian Frost 	inhm_mc_nvl[chip] = nvl;
271e3d60c9bSAdrian Frost }