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
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/cpupart.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/kstat.h>
#include <sys/processor.h>
#include <sys/disp.h>
#include <sys/group.h>
#include <sys/pg.h>
/*
* Processor groups
*
* With the introduction of Chip Multi-Threaded (CMT) processor architectures,
* it is no longer necessarily true that a given physical processor module
* will present itself as a single schedulable entity (cpu_t). Rather, each
* chip and/or processor core may present itself as one or more "logical" CPUs.
*
* The logical CPUs presented may share physical components such as caches,
* data pipes, execution pipelines, FPUs, etc. It is advantageous to have the
* kernel be aware of the relationships existing between logical CPUs so that
* the appropriate optmizations may be employed.
*
* The processor group abstraction represents a set of logical CPUs that
* generally share some sort of physical or characteristic relationship.
*
* In the case of a physical sharing relationship, the CPUs in the group may
* share a pipeline, cache or floating point unit. In the case of a logical
* relationship, a PG may represent the set of CPUs in a processor set, or the
* set of CPUs running at a particular clock speed.
*
* The generic processor group structure, pg_t, contains the elements generic
* to a group of CPUs. Depending on the nature of the CPU relationship
* (LOGICAL or PHYSICAL), a pointer to a pg may be recast to a "view" of that
* PG where more specific data is represented.
*
* As an example, a PG representing a PHYSICAL relationship, may be recast to
* a pghw_t, where data further describing the hardware sharing relationship
* is maintained. See pghw.c and pghw.h for details on physical PGs.
*
* At this time a more specialized casting of a PG representing a LOGICAL
* relationship has not been implemented, but the architecture allows for this
* in the future.
*
* Processor Group Classes
*
* Processor group consumers may wish to maintain and associate specific
* data with the PGs they create. For this reason, a mechanism for creating
* class specific PGs exists. Classes may overload the default functions for
* creating, destroying, and associating CPUs with PGs, and may also register
* class specific callbacks to be invoked when the CPU related system
* configuration changes. Class specific data is stored/associated with
* PGs by incorporating the pg_t (or pghw_t, as appropriate), as the first
* element of a class specific PG object. In memory, such a structure may look
* like:
*
* ----------------------- - - -
* | common | | | | <--(pg_t *)
* ----------------------- | | -
* | HW specific | | | <-----(pghw_t *)
* ----------------------- | -
* | class specific | | <-------(pg_cmt_t *)
* ----------------------- -
*
* Access to the PG class specific data can be had by casting a pointer to
* it's class specific view.
*/
static pg_t *pg_alloc_default(pg_class_t);
static void pg_free_default(pg_t *);
/*
* Bootstrap CPU specific PG data
* See pg_cpu_bootstrap()
*/
static cpu_pg_t bootstrap_pg_data;
/*
* Bitset of allocated PG ids (they are sequential)
* and the next free id in the set.
*/
static bitset_t pg_id_set;
static pgid_t pg_id_next = 0;
/*
* Default and externed PG ops vectors
*/
static struct pg_ops pg_ops_default = {
pg_alloc_default, /* alloc */
pg_free_default, /* free */
NULL, /* cpu_init */
NULL, /* cpu_fini */
NULL, /* cpu_active */
NULL, /* cpu_inactive */
NULL, /* cpupart_in */
NULL, /* cpupart_out */
NULL, /* cpupart_move */
NULL, /* cpu_belongs */
};
/*
* Class specific PG allocation callbacks
*/
#define PG_ALLOC(class) \
(pg_classes[class].pgc_ops->alloc ? \
pg_classes[class].pgc_ops->alloc() : \
pg_classes[pg_default_cid].pgc_ops->alloc())
#define PG_FREE(pg) \
((pg)->pg_class->pgc_ops->free ? \
(pg)->pg_class->pgc_ops->free(pg) : \
pg_classes[pg_default_cid].pgc_ops->free(pg)) \
/*
* Class specific membership test callback
*/
#define PG_CPU_BELONGS(pg, cp) \
((pg)->pg_class->pgc_ops->cpu_belongs ? \
(pg)->pg_class->pgc_ops->cpu_belongs(pg, cp) : 0) \
/*
* CPU configuration callbacks
*/
#define PG_CPU_INIT(class, cp) \
{ \
if (pg_classes[class].pgc_ops->cpu_init) \
pg_classes[class].pgc_ops->cpu_init(cp); \
}
#define PG_CPU_FINI(class, cp) \
{ \
if (pg_classes[class].pgc_ops->cpu_fini) \
pg_classes[class].pgc_ops->cpu_fini(cp); \
}
#define PG_CPU_ACTIVE(class, cp) \
{ \
if (pg_classes[class].pgc_ops->cpu_active) \
pg_classes[class].pgc_ops->cpu_active(cp); \
}
#define PG_CPU_INACTIVE(class, cp) \
{ \
if (pg_classes[class].pgc_ops->cpu_inactive) \
pg_classes[class].pgc_ops->cpu_inactive(cp); \
}
/*
* CPU / cpupart configuration callbacks
*/
#define PG_CPUPART_IN(class, cp, pp) \
{ \
if (pg_classes[class].pgc_ops->cpupart_in) \
pg_classes[class].pgc_ops->cpupart_in(cp, pp); \
}
#define PG_CPUPART_OUT(class, cp, pp) \
{ \
if (pg_classes[class].pgc_ops->cpupart_out) \
pg_classes[class].pgc_ops->cpupart_out(cp, pp); \
}
#define PG_CPUPART_MOVE(class, cp, old, new) \
{ \
if (pg_classes[class].pgc_ops->cpupart_move) \
pg_classes[class].pgc_ops->cpupart_move(cp, old, new); \
}
static pg_class_t *pg_classes;
static int pg_nclasses;
static pg_cid_t pg_default_cid;
/*
* Initialze common PG subsystem. Perform CPU 0 initialization
*/
void
pg_init(void)
{
pg_default_cid =
pg_class_register("default", &pg_ops_default, PGR_LOGICAL);
}
/*
* Perform CPU 0 initialization
*/
void
pg_cpu0_init(void)
{
extern void pghw_physid_create();
/*
* Create the physical ID cache for the boot CPU
*/
pghw_physid_create(CPU);
/*
* pg_cpu_* require that cpu_lock be held
*/
mutex_enter(&cpu_lock);
pg_cpu_init(CPU);
pg_cpupart_in(CPU, &cp_default);
pg_cpu_active(CPU);
mutex_exit(&cpu_lock);
}
/*
* Invoked when topology for CPU0 changes
* post pg_cpu0_init().
*
* Currently happens as a result of null_proc_lpa
* on Starcat.
*/
void
pg_cpu0_reinit(void)
{
mutex_enter(&cpu_lock);
pg_cpu_inactive(CPU);
pg_cpupart_out(CPU, &cp_default);
pg_cpu_fini(CPU);
pg_cpu_init(CPU);
pg_cpupart_in(CPU, &cp_default);
pg_cpu_active(CPU);
mutex_exit(&cpu_lock);
}
/*
* Register a new PG class
*/
pg_cid_t
pg_class_register(char *name, struct pg_ops *ops, pg_relation_t relation)
{
pg_class_t *newclass;
pg_class_t *classes_old;
id_t cid;
mutex_enter(&cpu_lock);
/*
* Allocate a new pg_class_t in the pg_classes array
*/
if (pg_nclasses == 0) {
pg_classes = kmem_zalloc(sizeof (pg_class_t), KM_SLEEP);
} else {
classes_old = pg_classes;
pg_classes =
kmem_zalloc(sizeof (pg_class_t) * (pg_nclasses + 1),
KM_SLEEP);
(void) kcopy(classes_old, pg_classes,
sizeof (pg_class_t) * pg_nclasses);
kmem_free(classes_old, sizeof (pg_class_t) * pg_nclasses);
}
cid = pg_nclasses++;
newclass = &pg_classes[cid];
(void) strncpy(newclass->pgc_name, name, PG_CLASS_NAME_MAX);
newclass->pgc_id = cid;
newclass->pgc_ops = ops;
newclass->pgc_relation = relation;
mutex_exit(&cpu_lock);
return (cid);
}
/*
* Try to find an existing pg in set in which to place cp.
* Returns the pg if found, and NULL otherwise.
* In the event that the CPU could belong to multiple
* PGs in the set, the first matching PG will be returned.
*/
pg_t *
pg_cpu_find_pg(cpu_t *cp, group_t *set)
{
pg_t *pg;
group_iter_t i;
group_iter_init(&i);
while ((pg = group_iterate(set, &i)) != NULL) {
/*
* Ask the class if the CPU belongs here
*/
if (PG_CPU_BELONGS(pg, cp))
return (pg);
}
return (NULL);
}
/*
* Iterate over the CPUs in a PG after initializing
* the iterator with PG_CPU_ITR_INIT()
*/
cpu_t *
pg_cpu_next(pg_cpu_itr_t *itr)
{
cpu_t *cpu;
pg_t *pg = itr->pg;
cpu = group_iterate(&pg->pg_cpus, &itr->position);
return (cpu);
}
/*
* Create a PG of a given class.
* This routine may block.
*/
pg_t *
pg_create(pg_cid_t cid)
{
pg_t *pg;
pgid_t id;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Call the class specific PG allocation routine
*/
pg = PG_ALLOC(cid);
pg->pg_class = &pg_classes[cid];
pg->pg_relation = pg->pg_class->pgc_relation;
/*
* Find the next free sequential pg id
*/
do {
if (pg_id_next >= bitset_capacity(&pg_id_set))
bitset_resize(&pg_id_set, pg_id_next + 1);
id = pg_id_next++;
} while (bitset_in_set(&pg_id_set, id));
pg->pg_id = id;
bitset_add(&pg_id_set, pg->pg_id);
/*
* Create the PG's CPU group
*/
group_create(&pg->pg_cpus);
return (pg);
}
/*
* Destroy a PG.
* This routine may block.
*/
void
pg_destroy(pg_t *pg)
{
ASSERT(MUTEX_HELD(&cpu_lock));
group_destroy(&pg->pg_cpus);
/*
* Unassign the pg_id
*/
if (pg_id_next > pg->pg_id)
pg_id_next = pg->pg_id;
bitset_del(&pg_id_set, pg->pg_id);
/*
* Invoke the class specific de-allocation routine
*/
PG_FREE(pg);
}
/*
* Add the CPU "cp" to processor group "pg"
* This routine may block.
*/
void
pg_cpu_add(pg_t *pg, cpu_t *cp)
{
int err;
ASSERT(MUTEX_HELD(&cpu_lock));
/* This adds the CPU to the PG's CPU group */
err = group_add(&pg->pg_cpus, cp, GRP_RESIZE);
ASSERT(err == 0);
/* This adds the PG to the CPUs PG group */
ASSERT(cp->cpu_pg != &bootstrap_pg_data);
err = group_add(&cp->cpu_pg->pgs, pg, GRP_RESIZE);
ASSERT(err == 0);
}
/*
* Remove "cp" from "pg".
* This routine may block.
*/
void
pg_cpu_delete(pg_t *pg, cpu_t *cp)
{
int err;
ASSERT(MUTEX_HELD(&cpu_lock));
/* Remove the CPU from the PG */
err = group_remove(&pg->pg_cpus, cp, GRP_RESIZE);
ASSERT(err == 0);
/* Remove the PG from the CPU's PG group */
ASSERT(cp->cpu_pg != &bootstrap_pg_data);
err = group_remove(&cp->cpu_pg->pgs, pg, GRP_RESIZE);
ASSERT(err == 0);
}
/*
* Allocate a CPU's PG data. This hangs off struct cpu at cpu_pg
*/
static cpu_pg_t *
pg_cpu_data_alloc(void)
{
cpu_pg_t *pgd;
pgd = kmem_zalloc(sizeof (cpu_pg_t), KM_SLEEP);
group_create(&pgd->pgs);
group_create(&pgd->cmt_pgs);
return (pgd);
}
/*
* Free the CPU's PG data.
*/
static void
pg_cpu_data_free(cpu_pg_t *pgd)
{
group_destroy(&pgd->pgs);
group_destroy(&pgd->cmt_pgs);
kmem_free(pgd, sizeof (cpu_pg_t));
}
/*
* A new CPU is coming into the system, either via booting or DR.
* Allocate it's PG data, and notify all registered classes about
* the new CPU.
*
* This routine may block.
*/
void
pg_cpu_init(cpu_t *cp)
{
pg_cid_t i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Allocate and size the per CPU pg data
*/
cp->cpu_pg = pg_cpu_data_alloc();
/*
* Notify all registered classes about the new CPU
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPU_INIT(i, cp);
}
/*
* This CPU is being deleted from the system. Notify the classes
* and free up the CPU's PG data.
*/
void
pg_cpu_fini(cpu_t *cp)
{
pg_cid_t i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* This can happen if the CPU coming into the system
* failed to power on.
*/
if (cp->cpu_pg == NULL ||
cp->cpu_pg == &bootstrap_pg_data)
return;
for (i = 0; i < pg_nclasses; i++)
PG_CPU_FINI(i, cp);
pg_cpu_data_free(cp->cpu_pg);
cp->cpu_pg = NULL;
}
/*
* This CPU is becoming active (online)
* This routine may not block as it is called from paused CPUs
* context.
*/
void
pg_cpu_active(cpu_t *cp)
{
pg_cid_t i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Notify all registered classes about the new CPU
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPU_ACTIVE(i, cp);
}
/*
* This CPU is going inactive (offline)
* This routine may not block, as it is called from paused
* CPUs context.
*/
void
pg_cpu_inactive(cpu_t *cp)
{
pg_cid_t i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Notify all registered classes about the new CPU
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPU_INACTIVE(i, cp);
}
/*
* Invoked when the CPU is about to move into the partition
* This routine may block.
*/
void
pg_cpupart_in(cpu_t *cp, cpupart_t *pp)
{
int i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Notify all registered classes that the
* CPU is about to enter the CPU partition
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPUPART_IN(i, cp, pp);
}
/*
* Invoked when the CPU is about to move out of the partition
* This routine may block.
*/
/*ARGSUSED*/
void
pg_cpupart_out(cpu_t *cp, cpupart_t *pp)
{
int i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Notify all registered classes that the
* CPU is about to leave the CPU partition
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPUPART_OUT(i, cp, pp);
}
/*
* Invoked when the CPU is *moving* partitions.
*
* This routine may not block, as it is called from paused CPUs
* context.
*/
void
pg_cpupart_move(cpu_t *cp, cpupart_t *oldpp, cpupart_t *newpp)
{
int i;
ASSERT(MUTEX_HELD(&cpu_lock));
/*
* Notify all registered classes that the
* CPU is about to leave the CPU partition
*/
for (i = 0; i < pg_nclasses; i++)
PG_CPUPART_MOVE(i, cp, oldpp, newpp);
}
/*
* Provide the specified CPU a bootstrap pg
* This is needed to allow sane behaviour if any PG consuming
* code needs to deal with a partially initialized CPU
*/
void
pg_cpu_bootstrap(cpu_t *cp)
{
cp->cpu_pg = &bootstrap_pg_data;
}
/*ARGSUSED*/
static pg_t *
pg_alloc_default(pg_class_t class)
{
return (kmem_zalloc(sizeof (pg_t), KM_SLEEP));
}
/*ARGSUSED*/
static void
pg_free_default(struct pg *pg)
{
kmem_free(pg, sizeof (pg_t));
}
|