summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/os/cms.c
blob: 0433aeabfdf8c847134a685355121a872cef586b (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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */
/*
 * Copyright (c) 2010, Intel Corporation.
 * All rights reserved.
 */

/*
 * Copyright (c) 2018, Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/cpu_module_ms_impl.h>
#include <sys/cpuvar.h>
#include <sys/ksynch.h>
#include <sys/modctl.h>
#include <sys/x86_archext.h>
#include <sys/systm.h>
#include <sys/cmn_err.h>
#include <sys/param.h>
#include <sys/reboot.h>

/*
 * Set to prevent model-specific support from initialising.
 */
int cms_no_model_specific = 0;

/*
 * Subdirectory (relative to the module search path) in which we will
 * look for model-specific modules.
 */
#define	CPUMOD_MS_SUBDIR	"cpu"

/*
 * Cpu model-specific modules have filenames beginning with the following.
 */
#define	CPUMOD_MS_PREFIX	"cpu_ms"

#define	HDL2CMS(hdl)		cms_hdl_getcms(hdl)

#define	CMS_OPS(cms)		(cms)->cms_ops
#define	CMS_OP_PRESENT(cms, op)	((cms) && CMS_OPS(cms)->op != NULL)

struct cms_cpuid {
	const char *vendor;
	uint_t family;
	uint_t model;
	uint_t stepping;
};

#define	CMS_MATCH_VENDOR	0	/* Just match on vendor */
#define	CMS_MATCH_FAMILY	1	/* Match down to family */
#define	CMS_MATCH_MODEL		2	/* Match down to model */
#define	CMS_MATCH_STEPPING	3	/* Match down to stepping */

/*
 * Structure used to keep track of modules we have loaded.
 */
typedef struct cms {
	struct cms *cms_next;
	struct cms *cms_prev;
	const cms_ops_t *cms_ops;
	struct modctl *cms_modp;
	uint_t cms_refcnt;
} cms_t;

static cms_t *cms_list;
static kmutex_t cms_load_lock;

/*
 * We stash a cms_t and associated private data via cmi_hdl_setspecific.
 */
struct cms_ctl {
	cms_t *cs_cms;
	void *cs_cmsdata;
};

static cms_t *
cms_hdl_getcms(cmi_hdl_t hdl)
{
	struct cms_ctl *cdp = cmi_hdl_getspecific(hdl);

	return (cdp != NULL ? cdp->cs_cms : NULL);
}

void *
cms_hdl_getcmsdata(cmi_hdl_t hdl)
{
	struct cms_ctl *cdp = cmi_hdl_getspecific(hdl);

	return (cdp != NULL ? cdp->cs_cmsdata : NULL);
}

static void
cms_link(cms_t *cms)
{
	ASSERT(MUTEX_HELD(&cms_load_lock));

	cms->cms_prev = NULL;
	cms->cms_next = cms_list;
	if (cms_list != NULL)
		cms_list->cms_prev = cms;
	cms_list = cms;
}

static void
cms_unlink(cms_t *cms)
{
	ASSERT(MUTEX_HELD(&cms_load_lock));
	ASSERT(cms->cms_refcnt == 0);

	if (cms->cms_prev != NULL)
		cms->cms_prev->cms_next = cms->cms_next;

	if (cms->cms_next != NULL)
		cms->cms_next->cms_prev = cms->cms_prev;

	if (cms_list == cms)
		cms_list = cms->cms_next;
}

/*
 * Hold the module in memory.  We call to CPU modules without using the
 * stubs mechanism, so these modules must be manually held in memory.
 * The mod_ref acts as if another loaded module has a dependency on us.
 */
static void
cms_hold(cms_t *cms)
{
	ASSERT(MUTEX_HELD(&cms_load_lock));

	mutex_enter(&mod_lock);
	cms->cms_modp->mod_ref++;
	mutex_exit(&mod_lock);
	cms->cms_refcnt++;
}

static void
cms_rele(cms_t *cms)
{
	ASSERT(MUTEX_HELD(&cms_load_lock));

	mutex_enter(&mod_lock);
	cms->cms_modp->mod_ref--;
	mutex_exit(&mod_lock);

	if (--cms->cms_refcnt == 0) {
		cms_unlink(cms);
		kmem_free(cms, sizeof (cms_t));
	}
}

static cms_ops_t *
cms_getops(modctl_t *modp)
{
	cms_ops_t *ops;

	if ((ops = (cms_ops_t *)modlookup_by_modctl(modp, "_cms_ops")) ==
	    NULL) {
		cmn_err(CE_WARN, "cpu_ms module '%s' is invalid: no _cms_ops "
		    "found", modp->mod_modname);
		return (NULL);
	}

	if (ops->cms_init == NULL) {
		cmn_err(CE_WARN, "cpu_ms module '%s' is invalid: no cms_init "
		    "entry point", modp->mod_modname);
		return (NULL);
	}

	return (ops);
}

static cms_t *
cms_load_modctl(modctl_t *modp)
{
	cms_ops_t *ops;
	uintptr_t ver;
	cms_t *cms;
	cms_api_ver_t apiver;

	ASSERT(MUTEX_HELD(&cms_load_lock));

	for (cms = cms_list; cms != NULL; cms = cms->cms_next) {
		if (cms->cms_modp == modp)
			return (cms);
	}

	if ((ver = modlookup_by_modctl(modp, "_cms_api_version")) == NULL) {
		cmn_err(CE_WARN, "cpu model-specific module '%s' is invalid:  "
		    "no _cms_api_version", modp->mod_modname);
		return (NULL);
	} else {
		apiver = *((cms_api_ver_t *)ver);
		if (!CMS_API_VERSION_CHKMAGIC(apiver)) {
			cmn_err(CE_WARN, "cpu model-specific module '%s' is "
			    "invalid: _cms_api_version 0x%x has bad magic",
			    modp->mod_modname, apiver);
			return (NULL);
		}
	}

	if (apiver != CMS_API_VERSION) {
		cmn_err(CE_WARN, "cpu model-specific module '%s' has API "
		    "version %d, kernel requires API version %d",
		    modp->mod_modname, CMS_API_VERSION_TOPRINT(apiver),
		    CMS_API_VERSION_TOPRINT(CMS_API_VERSION));
		return (NULL);
	}

	if ((ops = cms_getops(modp)) == NULL)
		return (NULL);

	cms = kmem_zalloc(sizeof (cms_t), KM_SLEEP);
	cms->cms_ops = ops;
	cms->cms_modp = modp;

	cms_link(cms);

	return (cms);
}

static int
cms_cpu_match(cmi_hdl_t hdl1, cmi_hdl_t hdl2, int match)
{
	if (match >= CMS_MATCH_VENDOR &&
	    cmi_hdl_vendor(hdl1) != cmi_hdl_vendor(hdl2))
		return (0);

	if (match >= CMS_MATCH_FAMILY &&
	    cmi_hdl_family(hdl1) != cmi_hdl_family(hdl2))
		return (0);

	if (match >= CMS_MATCH_MODEL &&
	    cmi_hdl_model(hdl1) != cmi_hdl_model(hdl2))
		return (0);

	if (match >= CMS_MATCH_STEPPING &&
	    cmi_hdl_stepping(hdl1) != cmi_hdl_stepping(hdl2))
		return (0);

	return (1);
}

static int
cms_search_list_cb(cmi_hdl_t whdl, void *arg1, void *arg2, void *arg3)
{
	cmi_hdl_t thdl = (cmi_hdl_t)arg1;
	int match = *((int *)arg2);
	cmi_hdl_t *rsltp = (cmi_hdl_t *)arg3;

	if (cms_cpu_match(thdl, whdl, match)) {
		cmi_hdl_hold(whdl);	/* short-term hold */
		*rsltp = whdl;
		return (CMI_HDL_WALK_DONE);
	} else {
		return (CMI_HDL_WALK_NEXT);
	}
}

/*
 * Look to see if we've already got a module loaded for a CPU just
 * like this one.  If we do, then we'll re-use it.
 */
static cms_t *
cms_search_list(cmi_hdl_t hdl, int match)
{
	cmi_hdl_t dhdl = NULL;
	cms_t *cms = NULL;

	ASSERT(MUTEX_HELD(&cms_load_lock));

	cmi_hdl_walk(cms_search_list_cb, (void *)hdl, (void *)&match, &dhdl);
	if (dhdl) {
		cms = HDL2CMS(dhdl);
		cmi_hdl_rele(dhdl);	/* held in cms_search_list_cb */
	}

	return (cms);
}

/*
 * Try to find or load a module that offers model-specific support for
 * this vendor/family/model/stepping combination.  When attempting to load
 * a module we look in CPUMOD_MS_SUBDIR first for a match on
 * vendor/family/model/stepping, then on vendor/family/model (ignoring
 * stepping), then on vendor/family (ignoring model and stepping), then
 * on vendor alone.
 */
static cms_t *
cms_load_module(cmi_hdl_t hdl, int match, int *chosenp)
{
	modctl_t *modp;
	cms_t *cms;
	int modid;
	uint_t s[3];

	ASSERT(MUTEX_HELD(&cms_load_lock));
	ASSERT(match == CMS_MATCH_STEPPING || match == CMS_MATCH_MODEL ||
	    match == CMS_MATCH_FAMILY || match == CMS_MATCH_VENDOR);

	s[0] = cmi_hdl_family(hdl);
	s[1] = cmi_hdl_model(hdl);
	s[2] = cmi_hdl_stepping(hdl);

	/*
	 * Have we already loaded a module for a cpu with the same
	 * vendor/family/model/stepping?
	 */
	if ((cms = cms_search_list(hdl, match)) != NULL) {
		cms_hold(cms);
		return (cms);
	}

	modid = modload_qualified(CPUMOD_MS_SUBDIR, CPUMOD_MS_PREFIX,
	    cmi_hdl_vendorstr(hdl), ".", s, match, chosenp);

	if (modid == -1)
		return (NULL);

	modp = mod_hold_by_id(modid);
	cms = cms_load_modctl(modp);
	if (cms)
		cms_hold(cms);
	mod_release_mod(modp);

	return (cms);
}

static cms_t *
cms_load_specific(cmi_hdl_t hdl, void **datap)
{
	cms_t *cms;
	int err;
	int i;

	ASSERT(MUTEX_HELD(&cms_load_lock));

	for (i = CMS_MATCH_STEPPING; i >= CMS_MATCH_VENDOR; i--) {
		int suffixlevel;

		if ((cms = cms_load_module(hdl, i, &suffixlevel)) == NULL)
			return (NULL);

		/*
		 * A module has loaded and has a _cms_ops structure, and the
		 * module has been held for this instance.  Call the cms_init
		 * entry point - we expect success (0) or ENOTSUP.
		 */
		if ((err = cms->cms_ops->cms_init(hdl, datap)) == 0) {
			if (boothowto & RB_VERBOSE) {
				printf("initialized model-specific "
				    "module '%s' on chip %d core %d "
				    "strand %d\n",
				    cms->cms_modp->mod_modname,
				    cmi_hdl_chipid(hdl), cmi_hdl_coreid(hdl),
				    cmi_hdl_strandid(hdl));
			}
			return (cms);
		} else if (err != ENOTSUP) {
			cmn_err(CE_WARN, "failed to init model-specific "
			    "module '%s' on chip %d core %d strand %d: err=%d",
			    cms->cms_modp->mod_modname,
			    cmi_hdl_chipid(hdl), cmi_hdl_coreid(hdl),
			    cmi_hdl_strandid(hdl), err);
		}

		/*
		 * The module failed or declined to init, so release
		 * it and potentially change i to be equal to the number
		 * of suffices actually used in the last module path.
		 */
		cms_rele(cms);
		i = suffixlevel;
	}

	return (NULL);
}

void
cms_init(cmi_hdl_t hdl)
{
	cms_t *cms;
	void *data;

	if (cms_no_model_specific != 0)
		return;

	mutex_enter(&cms_load_lock);

	if ((cms = cms_load_specific(hdl, &data)) != NULL) {
		struct cms_ctl *cdp;

		ASSERT(cmi_hdl_getspecific(hdl) == NULL);

		cdp = kmem_alloc(sizeof (*cdp), KM_SLEEP);
		cdp->cs_cms = cms;
		cdp->cs_cmsdata = data;
		cmi_hdl_setspecific(hdl, cdp);
	}

	mutex_exit(&cms_load_lock);
}

void
cms_fini(cmi_hdl_t hdl)
{
	cms_t *cms = HDL2CMS(hdl);
	struct cms_ctl *cdp;

	if (CMS_OP_PRESENT(cms, cms_fini))
		CMS_OPS(cms)->cms_fini(hdl);

	mutex_enter(&cms_load_lock);
	cdp = (struct cms_ctl *)cmi_hdl_getspecific(hdl);
	if (cdp != NULL) {
		if (cdp->cs_cms != NULL)
			cms_rele(cdp->cs_cms);
		kmem_free(cdp, sizeof (*cdp));
	}
	mutex_exit(&cms_load_lock);
}

boolean_t
cms_present(cmi_hdl_t hdl)
{
	return (HDL2CMS(hdl) != NULL ? B_TRUE : B_FALSE);
}

void
cms_post_startup(cmi_hdl_t hdl)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_post_startup))
		CMS_OPS(cms)->cms_post_startup(hdl);
}

void
cms_post_mpstartup(cmi_hdl_t hdl)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_post_mpstartup))
		CMS_OPS(cms)->cms_post_mpstartup(hdl);
}

size_t
cms_logout_size(cmi_hdl_t hdl)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_logout_size))
		return (0);

	return (CMS_OPS(cms)->cms_logout_size(hdl));
}

uint64_t
cms_mcgctl_val(cmi_hdl_t hdl, int nbanks, uint64_t def)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_mcgctl_val))
		return (def);

	return (CMS_OPS(cms)->cms_mcgctl_val(hdl, nbanks, def));
}

boolean_t
cms_bankctl_skipinit(cmi_hdl_t hdl, int banknum)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_bankctl_skipinit))
		return (B_FALSE);

	return (CMS_OPS(cms)->cms_bankctl_skipinit(hdl, banknum));
}

uint64_t
cms_bankctl_val(cmi_hdl_t hdl, int banknum, uint64_t def)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_bankctl_val))
		return (def);

	return (CMS_OPS(cms)->cms_bankctl_val(hdl, banknum, def));
}

boolean_t
cms_bankstatus_skipinit(cmi_hdl_t hdl, int banknum)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_bankstatus_skipinit))
		return (B_FALSE);

	return (CMS_OPS(cms)->cms_bankstatus_skipinit(hdl, banknum));
}

uint64_t
cms_bankstatus_val(cmi_hdl_t hdl, int banknum, uint64_t def)
{
	cms_t *cms = HDL2CMS(hdl);

	if (!CMS_OP_PRESENT(cms, cms_bankstatus_val))
		return (def);

	return (CMS_OPS(cms)->cms_bankstatus_val(hdl, banknum, def));
}

void
cms_mca_init(cmi_hdl_t hdl, int nbanks)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_mca_init))
		CMS_OPS(cms)->cms_mca_init(hdl, nbanks);
}

uint64_t
cms_poll_ownermask(cmi_hdl_t hdl, hrtime_t poll_interval)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_poll_ownermask))
		return (CMS_OPS(cms)->cms_poll_ownermask(hdl, poll_interval));
	else
		return (-1ULL);		/* poll all banks by default */
}

void
cms_bank_logout(cmi_hdl_t hdl, int banknum, uint64_t status, uint64_t addr,
    uint64_t misc, void *mslogout)
{
	cms_t *cms = HDL2CMS(hdl);

	if (mslogout != NULL && CMS_OP_PRESENT(cms, cms_bank_logout))
		CMS_OPS(cms)->cms_bank_logout(hdl, banknum, status, addr,
		    misc, mslogout);
}

cms_errno_t
cms_msrinject(cmi_hdl_t hdl, uint_t msr, uint64_t val)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_msrinject))
		return (CMS_OPS(cms)->cms_msrinject(hdl, msr, val));
	else
		return (CMSERR_NOTSUP);
}

uint32_t
cms_error_action(cmi_hdl_t hdl, int ismc, int banknum, uint64_t status,
    uint64_t addr, uint64_t misc, void *mslogout)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_error_action))
		return (CMS_OPS(cms)->cms_error_action(hdl, ismc, banknum,
		    status, addr, misc, mslogout));
	else
		return (0);
}

cms_cookie_t
cms_disp_match(cmi_hdl_t hdl, int ismc, int banknum, uint64_t status,
    uint64_t addr, uint64_t misc, void *mslogout)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_disp_match))
		return (CMS_OPS(cms)->cms_disp_match(hdl, ismc, banknum,
		    status, addr, misc, mslogout));
	else
		return (NULL);

}

void
cms_ereport_class(cmi_hdl_t hdl, cms_cookie_t mscookie, const char **cpuclsp,
    const char **leafclsp)
{
	cms_t *cms = HDL2CMS(hdl);

	if (cpuclsp == NULL || leafclsp == NULL)
		return;

	*cpuclsp = *leafclsp = NULL;
	if (CMS_OP_PRESENT(cms, cms_ereport_class)) {
		CMS_OPS(cms)->cms_ereport_class(hdl, mscookie, cpuclsp,
		    leafclsp);
	}
}

nvlist_t *
cms_ereport_detector(cmi_hdl_t hdl, int bankno, cms_cookie_t mscookie,
    nv_alloc_t *nva)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_ereport_detector))
		return (CMS_OPS(cms)->cms_ereport_detector(hdl, bankno,
		    mscookie, nva));
	else
		return (NULL);

}

boolean_t
cms_ereport_includestack(cmi_hdl_t hdl, cms_cookie_t mscookie)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_ereport_includestack)) {
		return (CMS_OPS(cms)->cms_ereport_includestack(hdl, mscookie));
	} else {
		return (B_FALSE);
	}
}

void
cms_ereport_add_logout(cmi_hdl_t hdl, nvlist_t *nvl, nv_alloc_t *nva,
    int banknum, uint64_t status, uint64_t addr, uint64_t misc, void *mslogout,
    cms_cookie_t mscookie)
{
	cms_t *cms = HDL2CMS(hdl);

	if (CMS_OP_PRESENT(cms, cms_ereport_add_logout))
		CMS_OPS(cms)->cms_ereport_add_logout(hdl, nvl, nva, banknum,
		    status, addr, misc, mslogout, mscookie);

}