1 /* Copyright 2013-2017 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * TODO: Index array by PIR to be able to catch them easily
19  * from assembly such as machine checks etc...
20  */
21 #include <skiboot.h>
22 #include <cpu.h>
23 #include <device.h>
24 #include <mem_region.h>
25 #include <opal.h>
26 #include <stack.h>
27 #include <trace.h>
28 #include <affinity.h>
29 #include <chip.h>
30 #include <timebase.h>
31 #include <interrupts.h>
32 #include <ccan/str/str.h>
33 #include <ccan/container_of/container_of.h>
34 #include <xscom.h>
35 
36 /* The cpu_threads array is static and indexed by PIR in
37  * order to speed up lookup from asm entry points
38  */
39 struct cpu_stack {
40 	union {
41 		uint8_t	stack[STACK_SIZE];
42 		struct cpu_thread cpu;
43 	};
44 } __align(STACK_SIZE);
45 
46 static struct cpu_stack *cpu_stacks = (struct cpu_stack *)CPU_STACKS_BASE;
47 unsigned int cpu_thread_count;
48 unsigned int cpu_max_pir;
49 struct cpu_thread *boot_cpu;
50 static struct lock reinit_lock = LOCK_UNLOCKED;
51 static bool hile_supported;
52 static bool radix_supported;
53 static unsigned long hid0_hile;
54 static unsigned long hid0_attn;
55 static bool sreset_enabled;
56 static bool ipi_enabled;
57 static bool pm_enabled;
58 static bool current_hile_mode;
59 static bool current_radix_mode;
60 
61 unsigned long cpu_secondary_start __force_data = 0;
62 
63 struct cpu_job {
64 	struct list_node	link;
65 	void			(*func)(void *data);
66 	void			*data;
67 	const char		*name;
68 	bool			complete;
69 	bool		        no_return;
70 };
71 
72 /* attribute const as cpu_stacks is constant. */
cpu_stack_bottom(unsigned int pir)73 unsigned long __attrconst cpu_stack_bottom(unsigned int pir)
74 {
75 	return ((unsigned long)&cpu_stacks[pir]) +
76 		sizeof(struct cpu_thread) + STACK_SAFETY_GAP;
77 }
78 
cpu_stack_top(unsigned int pir)79 unsigned long __attrconst cpu_stack_top(unsigned int pir)
80 {
81 	/* This is the top of the MC stack which is above the normal
82 	 * stack, which means a SP between cpu_stack_bottom() and
83 	 * cpu_stack_top() can either be a normal stack pointer or
84 	 * a Machine Check stack pointer
85 	 */
86 	return ((unsigned long)&cpu_stacks[pir]) +
87 		NORMAL_STACK_SIZE - STACK_TOP_GAP;
88 }
89 
cpu_wake(struct cpu_thread * cpu)90 static void cpu_wake(struct cpu_thread *cpu)
91 {
92 	/* Is it idle ? If not, no need to wake */
93 	sync();
94 	if (!cpu->in_idle)
95 		return;
96 
97 	if (proc_gen == proc_gen_p8 || proc_gen == proc_gen_p7) {
98 		/* Poke IPI */
99 		icp_kick_cpu(cpu);
100 	} else if (proc_gen == proc_gen_p9) {
101 		p9_dbell_send(cpu->pir);
102 	}
103 }
104 
cpu_find_job_target(void)105 static struct cpu_thread *cpu_find_job_target(void)
106 {
107 	struct cpu_thread *cpu, *best, *me = this_cpu();
108 	uint32_t best_count;
109 
110 	/* We try to find a target to run a job. We need to avoid
111 	 * a CPU that has a "no return" job on its queue as it might
112 	 * never be able to process anything.
113 	 *
114 	 * Additionally we don't check the list but the job count
115 	 * on the target CPUs, since that is decremented *after*
116 	 * a job has been completed.
117 	 */
118 
119 
120 	/* First we scan all available primary threads
121 	 */
122 	for_each_available_cpu(cpu) {
123 		if (cpu == me || !cpu_is_thread0(cpu) || cpu->job_has_no_return)
124 			continue;
125 		if (cpu->job_count)
126 			continue;
127 		lock(&cpu->job_lock);
128 		if (!cpu->job_count)
129 			return cpu;
130 		unlock(&cpu->job_lock);
131 	}
132 
133 	/* Now try again with secondary threads included and keep
134 	 * track of the one with the less jobs queued up. This is
135 	 * done in a racy way, but it's just an optimization in case
136 	 * we are overcommitted on jobs. Could could also just pick
137 	 * a random one...
138 	 */
139 	best = NULL;
140 	best_count = -1u;
141 	for_each_available_cpu(cpu) {
142 		if (cpu == me || cpu->job_has_no_return)
143 			continue;
144 		if (!best || cpu->job_count < best_count) {
145 			best = cpu;
146 			best_count = cpu->job_count;
147 		}
148 		if (cpu->job_count)
149 			continue;
150 		lock(&cpu->job_lock);
151 		if (!cpu->job_count)
152 			return cpu;
153 		unlock(&cpu->job_lock);
154 	}
155 
156 	/* We haven't found anybody, do we have a bestie ? */
157 	if (best) {
158 		lock(&best->job_lock);
159 		return best;
160 	}
161 
162 	/* Go away */
163 	return NULL;
164 }
165 
__cpu_queue_job(struct cpu_thread * cpu,const char * name,void (* func)(void * data),void * data,bool no_return)166 struct cpu_job *__cpu_queue_job(struct cpu_thread *cpu,
167 				const char *name,
168 				void (*func)(void *data), void *data,
169 				bool no_return)
170 {
171 	struct cpu_job *job;
172 
173 #ifdef DEBUG_SERIALIZE_CPU_JOBS
174 	if (cpu == NULL)
175 		cpu = this_cpu();
176 #endif
177 
178 	if (cpu && !cpu_is_available(cpu)) {
179 		prerror("CPU: Tried to queue job on unavailable CPU 0x%04x\n",
180 			cpu->pir);
181 		return NULL;
182 	}
183 
184 	job = zalloc(sizeof(struct cpu_job));
185 	if (!job)
186 		return NULL;
187 	job->func = func;
188 	job->data = data;
189 	job->name = name;
190 	job->complete = false;
191 	job->no_return = no_return;
192 
193 	/* Pick a candidate. Returns with target queue locked */
194 	if (cpu == NULL)
195 		cpu = cpu_find_job_target();
196 	else if (cpu != this_cpu())
197 		lock(&cpu->job_lock);
198 	else
199 		cpu = NULL;
200 
201 	/* Can't be scheduled, run it now */
202 	if (cpu == NULL) {
203 		func(data);
204 		job->complete = true;
205 		return job;
206 	}
207 
208 	/* That's bad, the job will never run */
209 	if (cpu->job_has_no_return) {
210 		prlog(PR_WARNING, "WARNING ! Job %s scheduled on CPU 0x%x"
211 		      " which has a no-return job on its queue !\n",
212 		      job->name, cpu->pir);
213 		backtrace();
214 	}
215 	list_add_tail(&cpu->job_queue, &job->link);
216 	if (no_return)
217 		cpu->job_has_no_return = true;
218 	else
219 		cpu->job_count++;
220 	if (pm_enabled)
221 		cpu_wake(cpu);
222 	unlock(&cpu->job_lock);
223 
224 	return job;
225 }
226 
cpu_poll_job(struct cpu_job * job)227 bool cpu_poll_job(struct cpu_job *job)
228 {
229 	lwsync();
230 	return job->complete;
231 }
232 
cpu_wait_job(struct cpu_job * job,bool free_it)233 void cpu_wait_job(struct cpu_job *job, bool free_it)
234 {
235 	unsigned long time_waited = 0;
236 
237 	if (!job)
238 		return;
239 
240 	while (!job->complete) {
241 		/* This will call OPAL pollers for us */
242 		time_wait_ms(10);
243 		time_waited += 10;
244 		lwsync();
245 	}
246 	lwsync();
247 
248 	if (time_waited > msecs_to_tb(1000))
249 		prlog(PR_DEBUG, "cpu_wait_job(%s) for %lu\n",
250 		      job->name, tb_to_msecs(time_waited));
251 
252 	if (free_it)
253 		free(job);
254 }
255 
cpu_check_jobs(struct cpu_thread * cpu)256 bool cpu_check_jobs(struct cpu_thread *cpu)
257 {
258 	return !list_empty_nocheck(&cpu->job_queue);
259 }
260 
cpu_process_jobs(void)261 void cpu_process_jobs(void)
262 {
263 	struct cpu_thread *cpu = this_cpu();
264 	struct cpu_job *job = NULL;
265 	void (*func)(void *);
266 	void *data;
267 
268 	sync();
269 	if (!cpu_check_jobs(cpu))
270 		return;
271 
272 	lock(&cpu->job_lock);
273 	while (true) {
274 		bool no_return;
275 
276 		job = list_pop(&cpu->job_queue, struct cpu_job, link);
277 		if (!job)
278 			break;
279 
280 		func = job->func;
281 		data = job->data;
282 		no_return = job->no_return;
283 		unlock(&cpu->job_lock);
284 		prlog(PR_TRACE, "running job %s on %x\n", job->name, cpu->pir);
285 		if (no_return)
286 			free(job);
287 		func(data);
288 		lock(&cpu->job_lock);
289 		if (!no_return) {
290 			cpu->job_count--;
291 			lwsync();
292 			job->complete = true;
293 		}
294 	}
295 	unlock(&cpu->job_lock);
296 }
297 
298 enum cpu_wake_cause {
299 	cpu_wake_on_job,
300 	cpu_wake_on_dec,
301 };
302 
cpu_idle_p8(enum cpu_wake_cause wake_on)303 static void cpu_idle_p8(enum cpu_wake_cause wake_on)
304 {
305 	uint64_t lpcr = mfspr(SPR_LPCR) & ~SPR_LPCR_P8_PECE;
306 	struct cpu_thread *cpu = this_cpu();
307 
308 	if (!pm_enabled) {
309 		prlog_once(PR_DEBUG, "cpu_idle_p8 called pm disabled\n");
310 		return;
311 	}
312 
313 	/* Clean up ICP, be ready for IPIs */
314 	icp_prep_for_pm();
315 
316 	/* Synchronize with wakers */
317 	if (wake_on == cpu_wake_on_job) {
318 		/* Mark ourselves in idle so other CPUs know to send an IPI */
319 		cpu->in_idle = true;
320 		sync();
321 
322 		/* Check for jobs again */
323 		if (cpu_check_jobs(cpu) || !pm_enabled)
324 			goto skip_sleep;
325 
326 		/* Setup wakup cause in LPCR: EE (for IPI) */
327 		lpcr |= SPR_LPCR_P8_PECE2;
328 		mtspr(SPR_LPCR, lpcr);
329 
330 	} else {
331 		/* Mark outselves sleeping so cpu_set_pm_enable knows to
332 		 * send an IPI
333 		 */
334 		cpu->in_sleep = true;
335 		sync();
336 
337 		/* Check if PM got disabled */
338 		if (!pm_enabled)
339 			goto skip_sleep;
340 
341 		/* EE and DEC */
342 		lpcr |= SPR_LPCR_P8_PECE2 | SPR_LPCR_P8_PECE3;
343 		mtspr(SPR_LPCR, lpcr);
344 	}
345 
346 	/* Enter nap */
347 	enter_p8_pm_state(false);
348 
349 skip_sleep:
350 	/* Restore */
351 	sync();
352 	cpu->in_idle = false;
353 	cpu->in_sleep = false;
354 	reset_cpu_icp();
355 }
356 
cpu_idle_p9(enum cpu_wake_cause wake_on)357 static void cpu_idle_p9(enum cpu_wake_cause wake_on)
358 {
359 	uint64_t lpcr = mfspr(SPR_LPCR) & ~SPR_LPCR_P9_PECE;
360 	uint64_t psscr;
361 	struct cpu_thread *cpu = this_cpu();
362 
363 	if (!pm_enabled) {
364 		prlog_once(PR_DEBUG, "cpu_idle_p9 called pm disabled\n");
365 		return;
366 	}
367 
368 	msgclr(); /* flush pending messages */
369 
370 	/* Synchronize with wakers */
371 	if (wake_on == cpu_wake_on_job) {
372 		/* Mark ourselves in idle so other CPUs know to send an IPI */
373 		cpu->in_idle = true;
374 		sync();
375 
376 		/* Check for jobs again */
377 		if (cpu_check_jobs(cpu) || !pm_enabled)
378 			goto skip_sleep;
379 
380 		/* HV DBELL for IPI */
381 		lpcr |= SPR_LPCR_P9_PECEL1;
382 	} else {
383 		/* Mark outselves sleeping so cpu_set_pm_enable knows to
384 		 * send an IPI
385 		 */
386 		cpu->in_sleep = true;
387 		sync();
388 
389 		/* Check if PM got disabled */
390 		if (!pm_enabled)
391 			goto skip_sleep;
392 
393 		/* HV DBELL and DEC */
394 		lpcr |= SPR_LPCR_P9_PECEL1 | SPR_LPCR_P9_PECEL3;
395 		mtspr(SPR_LPCR, lpcr);
396 	}
397 
398 	mtspr(SPR_LPCR, lpcr);
399 
400 	if (sreset_enabled) {
401 		/* stop with EC=1 (sreset) and ESL=1 (enable thread switch). */
402 		/* PSSCR SD=0 ESL=1 EC=1 PSSL=0 TR=3 MTL=0 RL=3 */
403 		psscr = PPC_BIT(42) | PPC_BIT(43) |
404 			PPC_BITMASK(54, 55) | PPC_BITMASK(62,63);
405 		enter_p9_pm_state(psscr);
406 	} else {
407 		/* stop with EC=0 (resumes) which does not require sreset. */
408 		/* PSSCR SD=0 ESL=0 EC=0 PSSL=0 TR=3 MTL=0 RL=3 */
409 		psscr = PPC_BITMASK(54, 55) | PPC_BITMASK(62,63);
410 		enter_p9_pm_lite_state(psscr);
411 	}
412 
413 skip_sleep:
414 	/* Restore */
415 	cpu->in_idle = false;
416 	cpu->in_sleep = false;
417 	p9_dbell_receive();
418 }
419 
cpu_idle_pm(enum cpu_wake_cause wake_on)420 static void cpu_idle_pm(enum cpu_wake_cause wake_on)
421 {
422 	switch(proc_gen) {
423 	case proc_gen_p8:
424 		cpu_idle_p8(wake_on);
425 		break;
426 	case proc_gen_p9:
427 		cpu_idle_p9(wake_on);
428 		break;
429 	default:
430 		prlog_once(PR_DEBUG, "cpu_idle_pm called with bad processor type\n");
431 		break;
432 	}
433 }
434 
cpu_idle_job(void)435 void cpu_idle_job(void)
436 {
437 	if (pm_enabled) {
438 		cpu_idle_pm(cpu_wake_on_job);
439 	} else {
440 		struct cpu_thread *cpu = this_cpu();
441 
442 		smt_lowest();
443 		/* Check for jobs again */
444 		while (!cpu_check_jobs(cpu)) {
445 			if (pm_enabled)
446 				break;
447 			barrier();
448 		}
449 		smt_medium();
450 	}
451 }
452 
cpu_idle_delay(unsigned long delay)453 void cpu_idle_delay(unsigned long delay)
454 {
455 	unsigned long now = mftb();
456 	unsigned long end = now + delay;
457 	unsigned long min_pm = usecs_to_tb(10);
458 
459 	if (pm_enabled && delay > min_pm) {
460 pm:
461 		for (;;) {
462 			if (delay >= 0x7fffffff)
463 				delay = 0x7fffffff;
464 			mtspr(SPR_DEC, delay);
465 
466 			cpu_idle_pm(cpu_wake_on_dec);
467 
468 			now = mftb();
469 			if (tb_compare(now, end) == TB_AAFTERB)
470 				break;
471 			delay = end - now;
472 			if (!(pm_enabled && delay > min_pm))
473 				goto no_pm;
474 		}
475 	} else {
476 no_pm:
477 		smt_lowest();
478 		for (;;) {
479 			now = mftb();
480 			if (tb_compare(now, end) == TB_AAFTERB)
481 				break;
482 			delay = end - now;
483 			if (pm_enabled && delay > min_pm) {
484 				smt_medium();
485 				goto pm;
486 			}
487 		}
488 		smt_medium();
489 	}
490 }
491 
cpu_pm_disable(void)492 static void cpu_pm_disable(void)
493 {
494 	struct cpu_thread *cpu;
495 
496 	pm_enabled = false;
497 	sync();
498 
499 	if (proc_gen == proc_gen_p8) {
500 		for_each_available_cpu(cpu) {
501 			while (cpu->in_sleep || cpu->in_idle) {
502 				icp_kick_cpu(cpu);
503 				cpu_relax();
504 			}
505 		}
506 	} else if (proc_gen == proc_gen_p9) {
507 		for_each_available_cpu(cpu) {
508 			if (cpu->in_sleep || cpu->in_idle)
509 				p9_dbell_send(cpu->pir);
510 		}
511 
512 		smt_lowest();
513 		for_each_available_cpu(cpu) {
514 			while (cpu->in_sleep || cpu->in_idle)
515 				barrier();
516 		}
517 		smt_medium();
518 	}
519 }
520 
cpu_set_sreset_enable(bool enabled)521 void cpu_set_sreset_enable(bool enabled)
522 {
523 	if (sreset_enabled == enabled)
524 		return;
525 
526 	if (proc_gen == proc_gen_p8) {
527 		/* Public P8 Mambo has broken NAP */
528 		if (chip_quirk(QUIRK_MAMBO_CALLOUTS))
529 			return;
530 
531 		sreset_enabled = enabled;
532 		sync();
533 
534 		if (!enabled) {
535 			cpu_pm_disable();
536 		} else {
537 			if (ipi_enabled)
538 				pm_enabled = true;
539 		}
540 
541 	} else if (proc_gen == proc_gen_p9) {
542 		/* Don't use sreset idle on DD1 (has a number of bugs) */
543 		uint32_t version = mfspr(SPR_PVR);
544 		if (is_power9n(version) && (PVR_VERS_MAJ(version) == 1))
545 			return;
546 
547 		sreset_enabled = enabled;
548 		sync();
549 		/*
550 		 * Kick everybody out of PM so they can adjust the PM
551 		 * mode they are using (EC=0/1).
552 		 */
553 		cpu_pm_disable();
554 		if (ipi_enabled)
555 			pm_enabled = true;
556 	}
557 }
558 
cpu_set_ipi_enable(bool enabled)559 void cpu_set_ipi_enable(bool enabled)
560 {
561 	if (ipi_enabled == enabled)
562 		return;
563 
564 	if (proc_gen == proc_gen_p8) {
565 		ipi_enabled = enabled;
566 		sync();
567 		if (!enabled) {
568 			cpu_pm_disable();
569 		} else {
570 			if (sreset_enabled)
571 				pm_enabled = true;
572 		}
573 
574 	} else if (proc_gen == proc_gen_p9) {
575 		/* Don't use doorbell on DD1 (requires darn for msgsync) */
576 		uint32_t version = mfspr(SPR_PVR);
577 		if (is_power9n(version) && (PVR_VERS_MAJ(version) == 1))
578 			return;
579 
580 		ipi_enabled = enabled;
581 		sync();
582 		if (!enabled)
583 			cpu_pm_disable();
584 		else
585 			pm_enabled = true;
586 	}
587 }
588 
cpu_process_local_jobs(void)589 void cpu_process_local_jobs(void)
590 {
591 	struct cpu_thread *cpu = first_available_cpu();
592 
593 	while (cpu) {
594 		if (cpu != this_cpu())
595 			return;
596 
597 		cpu = next_available_cpu(cpu);
598 	}
599 
600 	if (!cpu)
601 		cpu = first_available_cpu();
602 
603 	/* No CPU to run on, just run synchro */
604 	if (cpu == this_cpu()) {
605 		prlog_once(PR_DEBUG, "Processing jobs synchronously\n");
606 		cpu_process_jobs();
607 		opal_run_pollers();
608 	}
609 }
610 
611 
get_cpu_node(u32 pir)612 struct dt_node *get_cpu_node(u32 pir)
613 {
614 	struct cpu_thread *t = find_cpu_by_pir(pir);
615 
616 	return t ? t->node : NULL;
617 }
618 
619 /* This only covers primary, active cpus */
find_cpu_by_chip_id(u32 chip_id)620 struct cpu_thread *find_cpu_by_chip_id(u32 chip_id)
621 {
622 	struct cpu_thread *t;
623 
624 	for_each_available_cpu(t) {
625 		if (t->is_secondary)
626 			continue;
627 		if (t->chip_id == chip_id)
628 			return t;
629 	}
630 	return NULL;
631 }
632 
find_cpu_by_node(struct dt_node * cpu)633 struct cpu_thread *find_cpu_by_node(struct dt_node *cpu)
634 {
635 	struct cpu_thread *t;
636 
637 	for_each_available_cpu(t) {
638 		if (t->node == cpu)
639 			return t;
640 	}
641 	return NULL;
642 }
643 
find_cpu_by_pir(u32 pir)644 struct cpu_thread *find_cpu_by_pir(u32 pir)
645 {
646 	if (pir > cpu_max_pir)
647 		return NULL;
648 	return &cpu_stacks[pir].cpu;
649 }
650 
find_cpu_by_server(u32 server_no)651 struct cpu_thread *find_cpu_by_server(u32 server_no)
652 {
653 	struct cpu_thread *t;
654 
655 	for_each_cpu(t) {
656 		if (t->server_no == server_no)
657 			return t;
658 	}
659 	return NULL;
660 }
661 
next_cpu(struct cpu_thread * cpu)662 struct cpu_thread *next_cpu(struct cpu_thread *cpu)
663 {
664 	struct cpu_stack *s = container_of(cpu, struct cpu_stack, cpu);
665 	unsigned int index;
666 
667 	if (cpu == NULL)
668 		index = 0;
669 	else
670 		index = s - cpu_stacks + 1;
671 	for (; index <= cpu_max_pir; index++) {
672 		cpu = &cpu_stacks[index].cpu;
673 		if (cpu->state != cpu_state_no_cpu)
674 			return cpu;
675 	}
676 	return NULL;
677 }
678 
first_cpu(void)679 struct cpu_thread *first_cpu(void)
680 {
681 	return next_cpu(NULL);
682 }
683 
next_available_cpu(struct cpu_thread * cpu)684 struct cpu_thread *next_available_cpu(struct cpu_thread *cpu)
685 {
686 	do {
687 		cpu = next_cpu(cpu);
688 	} while(cpu && !cpu_is_available(cpu));
689 
690 	return cpu;
691 }
692 
first_available_cpu(void)693 struct cpu_thread *first_available_cpu(void)
694 {
695 	return next_available_cpu(NULL);
696 }
697 
next_present_cpu(struct cpu_thread * cpu)698 struct cpu_thread *next_present_cpu(struct cpu_thread *cpu)
699 {
700 	do {
701 		cpu = next_cpu(cpu);
702 	} while(cpu && !cpu_is_present(cpu));
703 
704 	return cpu;
705 }
706 
first_present_cpu(void)707 struct cpu_thread *first_present_cpu(void)
708 {
709 	return next_present_cpu(NULL);
710 }
711 
get_available_nr_cores_in_chip(u32 chip_id)712 u8 get_available_nr_cores_in_chip(u32 chip_id)
713 {
714 	struct cpu_thread *core;
715 	u8 nr_cores = 0;
716 
717 	for_each_available_core_in_chip(core, chip_id)
718 		nr_cores++;
719 
720 	return nr_cores;
721 }
722 
next_available_core_in_chip(struct cpu_thread * core,u32 chip_id)723 struct cpu_thread *next_available_core_in_chip(struct cpu_thread *core,
724 					       u32 chip_id)
725 {
726 	do {
727 		core = next_cpu(core);
728 	} while(core && (!cpu_is_available(core) ||
729 			 core->chip_id != chip_id ||
730 			 core->is_secondary));
731 	return core;
732 }
733 
first_available_core_in_chip(u32 chip_id)734 struct cpu_thread *first_available_core_in_chip(u32 chip_id)
735 {
736 	return next_available_core_in_chip(NULL, chip_id);
737 }
738 
cpu_get_core_index(struct cpu_thread * cpu)739 uint32_t cpu_get_core_index(struct cpu_thread *cpu)
740 {
741 	return pir_to_core_id(cpu->pir);
742 }
743 
cpu_remove_node(const struct cpu_thread * t)744 void cpu_remove_node(const struct cpu_thread *t)
745 {
746 	struct dt_node *i;
747 
748 	/* Find this cpu node */
749 	dt_for_each_node(dt_root, i) {
750 		const struct dt_property *p;
751 
752 		if (!dt_has_node_property(i, "device_type", "cpu"))
753 			continue;
754 		p = dt_find_property(i, "ibm,pir");
755 		if (!p)
756 			continue;
757 		if (dt_property_get_cell(p, 0) == t->pir) {
758 			dt_free(i);
759 			return;
760 		}
761 	}
762 	prerror("CPU: Could not find cpu node %i to remove!\n", t->pir);
763 	abort();
764 }
765 
cpu_disable_all_threads(struct cpu_thread * cpu)766 void cpu_disable_all_threads(struct cpu_thread *cpu)
767 {
768 	unsigned int i;
769 	struct dt_property *p;
770 
771 	for (i = 0; i <= cpu_max_pir; i++) {
772 		struct cpu_thread *t = &cpu_stacks[i].cpu;
773 
774 		if (t->primary == cpu->primary)
775 			t->state = cpu_state_disabled;
776 
777 	}
778 
779 	/* Mark this core as bad so that Linux kernel don't use this CPU. */
780 	prlog(PR_DEBUG, "CPU: Mark CPU bad (PIR 0x%04x)...\n", cpu->pir);
781 	p = __dt_find_property(cpu->node, "status");
782 	if (p)
783 		dt_del_property(cpu->node, p);
784 
785 	dt_add_property_string(cpu->node, "status", "bad");
786 
787 	/* XXX Do something to actually stop the core */
788 }
789 
init_cpu_thread(struct cpu_thread * t,enum cpu_thread_state state,unsigned int pir)790 static void init_cpu_thread(struct cpu_thread *t,
791 			    enum cpu_thread_state state,
792 			    unsigned int pir)
793 {
794 	init_lock(&t->dctl_lock);
795 	init_lock(&t->job_lock);
796 	list_head_init(&t->job_queue);
797 	t->state = state;
798 	t->pir = pir;
799 #ifdef STACK_CHECK_ENABLED
800 	t->stack_bot_mark = LONG_MAX;
801 #endif
802 	assert(pir == container_of(t, struct cpu_stack, cpu) - cpu_stacks);
803 }
804 
enable_attn(void)805 static void enable_attn(void)
806 {
807 	unsigned long hid0;
808 
809 	hid0 = mfspr(SPR_HID0);
810 	hid0 |= hid0_attn;
811 	set_hid0(hid0);
812 }
813 
disable_attn(void)814 static void disable_attn(void)
815 {
816 	unsigned long hid0;
817 
818 	hid0 = mfspr(SPR_HID0);
819 	hid0 &= ~hid0_attn;
820 	set_hid0(hid0);
821 }
822 
823 extern void __trigger_attn(void);
trigger_attn(void)824 void trigger_attn(void)
825 {
826 	enable_attn();
827 	__trigger_attn();
828 }
829 
init_hid(void)830 static void init_hid(void)
831 {
832 	/* attn is enabled even when HV=0, so make sure it's off */
833 	disable_attn();
834 }
835 
pre_init_boot_cpu(void)836 void __nomcount pre_init_boot_cpu(void)
837 {
838 	struct cpu_thread *cpu = this_cpu();
839 
840 	memset(cpu, 0, sizeof(struct cpu_thread));
841 }
842 
init_boot_cpu(void)843 void init_boot_cpu(void)
844 {
845 	unsigned int i, pir, pvr;
846 
847 	pir = mfspr(SPR_PIR);
848 	pvr = mfspr(SPR_PVR);
849 
850 	/* Get CPU family and other flags based on PVR */
851 	switch(PVR_TYPE(pvr)) {
852 	case PVR_TYPE_P7:
853 	case PVR_TYPE_P7P:
854 		proc_gen = proc_gen_p7;
855 		break;
856 	case PVR_TYPE_P8E:
857 	case PVR_TYPE_P8:
858 		proc_gen = proc_gen_p8;
859 		hile_supported = PVR_VERS_MAJ(mfspr(SPR_PVR)) >= 2;
860 		hid0_hile = SPR_HID0_POWER8_HILE;
861 		hid0_attn = SPR_HID0_POWER8_ENABLE_ATTN;
862 		break;
863 	case PVR_TYPE_P8NVL:
864 		proc_gen = proc_gen_p8;
865 		hile_supported = true;
866 		hid0_hile = SPR_HID0_POWER8_HILE;
867 		hid0_attn = SPR_HID0_POWER8_ENABLE_ATTN;
868 		break;
869 	case PVR_TYPE_P9:
870 		proc_gen = proc_gen_p9;
871 		hile_supported = true;
872 		radix_supported = true;
873 		hid0_hile = SPR_HID0_POWER9_HILE;
874 		hid0_attn = SPR_HID0_POWER9_ENABLE_ATTN;
875 		break;
876 	default:
877 		proc_gen = proc_gen_unknown;
878 	}
879 
880 	/* Get a CPU thread count and an initial max PIR based on family */
881 	switch(proc_gen) {
882 	case proc_gen_p7:
883 		cpu_thread_count = 4;
884 		cpu_max_pir = SPR_PIR_P7_MASK;
885 		prlog(PR_INFO, "CPU: P7 generation processor"
886 		      " (max %d threads/core)\n", cpu_thread_count);
887 		break;
888 	case proc_gen_p8:
889 		cpu_thread_count = 8;
890 		cpu_max_pir = SPR_PIR_P8_MASK;
891 		prlog(PR_INFO, "CPU: P8 generation processor"
892 		      " (max %d threads/core)\n", cpu_thread_count);
893 		break;
894 	case proc_gen_p9:
895 		cpu_thread_count = 4;
896 		cpu_max_pir = SPR_PIR_P9_MASK;
897 		prlog(PR_INFO, "CPU: P9 generation processor"
898 		      " (max %d threads/core)\n", cpu_thread_count);
899 		break;
900 	default:
901 		prerror("CPU: Unknown PVR, assuming 1 thread\n");
902 		cpu_thread_count = 1;
903 		cpu_max_pir = mfspr(SPR_PIR);
904 	}
905 
906 	prlog(PR_DEBUG, "CPU: Boot CPU PIR is 0x%04x PVR is 0x%08x\n",
907 	      pir, pvr);
908 	prlog(PR_DEBUG, "CPU: Initial max PIR set to 0x%x\n", cpu_max_pir);
909 
910 	/*
911 	 * Adjust top of RAM to include CPU stacks. While we *could* have
912 	 * less RAM than this... during early boot, it's enough of a check
913 	 * until we start parsing device tree / hdat and find out for sure
914 	 */
915 	top_of_ram += (cpu_max_pir + 1) * STACK_SIZE;
916 
917 	/* Clear the CPU structs */
918 	for (i = 0; i <= cpu_max_pir; i++)
919 		memset(&cpu_stacks[i].cpu, 0, sizeof(struct cpu_thread));
920 
921 	/* Setup boot CPU state */
922 	boot_cpu = &cpu_stacks[pir].cpu;
923 	init_cpu_thread(boot_cpu, cpu_state_active, pir);
924 	init_boot_tracebuf(boot_cpu);
925 	assert(this_cpu() == boot_cpu);
926 	init_hid();
927 }
928 
enable_large_dec(bool on)929 static void enable_large_dec(bool on)
930 {
931 	u64 lpcr = mfspr(SPR_LPCR);
932 
933 	if (on)
934 		lpcr |= SPR_LPCR_P9_LD;
935 	else
936 		lpcr &= ~SPR_LPCR_P9_LD;
937 
938 	mtspr(SPR_LPCR, lpcr);
939 }
940 
941 #define HIGH_BIT (1ull << 63)
942 
find_dec_bits(void)943 static int find_dec_bits(void)
944 {
945 	int bits = 65; /* we always decrement once */
946 	u64 mask = ~0ull;
947 
948 	if (proc_gen < proc_gen_p9)
949 		return 32;
950 
951 	/* The ISA doesn't specify the width of the decrementer register so we
952 	 * need to discover it. When in large mode (LPCR.LD = 1) reads from the
953 	 * DEC SPR are sign extended to 64 bits and writes are truncated to the
954 	 * physical register width. We can use this behaviour to detect the
955 	 * width by starting from an all 1s value and left shifting until we
956 	 * read a value from the DEC with it's high bit cleared.
957 	 */
958 
959 	enable_large_dec(true);
960 
961 	do {
962 		bits--;
963 		mask = mask >> 1;
964 		mtspr(SPR_DEC, mask);
965 	} while (mfspr(SPR_DEC) & HIGH_BIT);
966 
967 	enable_large_dec(false);
968 
969 	prlog(PR_DEBUG, "CPU: decrementer bits %d\n", bits);
970 	return bits;
971 }
972 
init_all_cpus(void)973 void init_all_cpus(void)
974 {
975 	struct dt_node *cpus, *cpu;
976 	unsigned int thread, new_max_pir = 0;
977 	int dec_bits = find_dec_bits();
978 
979 	cpus = dt_find_by_path(dt_root, "/cpus");
980 	assert(cpus);
981 
982 	/* Iterate all CPUs in the device-tree */
983 	dt_for_each_child(cpus, cpu) {
984 		unsigned int pir, server_no, chip_id;
985 		enum cpu_thread_state state;
986 		const struct dt_property *p;
987 		struct cpu_thread *t, *pt;
988 
989 		/* Skip cache nodes */
990 		if (strcmp(dt_prop_get(cpu, "device_type"), "cpu"))
991 			continue;
992 
993 		server_no = dt_prop_get_u32(cpu, "reg");
994 
995 		/* If PIR property is absent, assume it's the same as the
996 		 * server number
997 		 */
998 		pir = dt_prop_get_u32_def(cpu, "ibm,pir", server_no);
999 
1000 		/* We should always have an ibm,chip-id property */
1001 		chip_id = dt_get_chip_id(cpu);
1002 
1003 		/* Only use operational CPUs */
1004 		if (!strcmp(dt_prop_get(cpu, "status"), "okay"))
1005 			state = cpu_state_present;
1006 		else
1007 			state = cpu_state_unavailable;
1008 
1009 		prlog(PR_INFO, "CPU: CPU from DT PIR=0x%04x Server#=0x%x"
1010 		      " State=%d\n", pir, server_no, state);
1011 
1012 		/* Setup thread 0 */
1013 		assert(pir <= cpu_max_pir);
1014 		t = pt = &cpu_stacks[pir].cpu;
1015 		if (t != boot_cpu) {
1016 			init_cpu_thread(t, state, pir);
1017 			/* Each cpu gets its own later in init_trace_buffers */
1018 			t->trace = boot_cpu->trace;
1019 		}
1020 		t->server_no = server_no;
1021 		t->primary = t;
1022 		t->node = cpu;
1023 		t->chip_id = chip_id;
1024 		t->icp_regs = NULL; /* Will be set later */
1025 		t->core_hmi_state = 0;
1026 		t->core_hmi_state_ptr = &t->core_hmi_state;
1027 		t->thread_mask = 1;
1028 
1029 		/* Add associativity properties */
1030 		add_core_associativity(t);
1031 
1032 		/* Add the decrementer width property */
1033 		dt_add_property_cells(cpu, "ibm,dec-bits", dec_bits);
1034 
1035 		/* Adjust max PIR */
1036 		if (new_max_pir < (pir + cpu_thread_count - 1))
1037 			new_max_pir = pir + cpu_thread_count - 1;
1038 
1039 		/* Iterate threads */
1040 		p = dt_find_property(cpu, "ibm,ppc-interrupt-server#s");
1041 		if (!p)
1042 			continue;
1043 		for (thread = 1; thread < (p->len / 4); thread++) {
1044 			prlog(PR_TRACE, "CPU:   secondary thread %d found\n",
1045 			      thread);
1046 			t = &cpu_stacks[pir + thread].cpu;
1047 			init_cpu_thread(t, state, pir + thread);
1048 			t->trace = boot_cpu->trace;
1049 			t->server_no = ((const u32 *)p->prop)[thread];
1050 			t->is_secondary = true;
1051 			t->primary = pt;
1052 			t->node = cpu;
1053 			t->chip_id = chip_id;
1054 			t->core_hmi_state_ptr = &pt->core_hmi_state;
1055 			t->thread_mask = 1 << thread;
1056 		}
1057 		prlog(PR_INFO, "CPU:  %d secondary threads\n", thread);
1058 	}
1059 	cpu_max_pir = new_max_pir;
1060 	prlog(PR_DEBUG, "CPU: New max PIR set to 0x%x\n", new_max_pir);
1061 	adjust_cpu_stacks_alloc();
1062 }
1063 
cpu_bringup(void)1064 void cpu_bringup(void)
1065 {
1066 	struct cpu_thread *t;
1067 	uint32_t count = 0;
1068 
1069 	prlog(PR_INFO, "CPU: Setting up secondary CPU state\n");
1070 
1071 	op_display(OP_LOG, OP_MOD_CPU, 0x0000);
1072 
1073 	/* Tell everybody to chime in ! */
1074 	prlog(PR_INFO, "CPU: Calling in all processors...\n");
1075 	cpu_secondary_start = 1;
1076 	sync();
1077 
1078 	op_display(OP_LOG, OP_MOD_CPU, 0x0002);
1079 
1080 	for_each_cpu(t) {
1081 		if (t->state != cpu_state_present &&
1082 		    t->state != cpu_state_active)
1083 			continue;
1084 
1085 		/* Add a callin timeout ?  If so, call cpu_remove_node(t). */
1086 		while (t->state != cpu_state_active) {
1087 			smt_lowest();
1088 			sync();
1089 		}
1090 		smt_medium();
1091 		count++;
1092 	}
1093 
1094 	prlog(PR_NOTICE, "CPU: All %d processors called in...\n", count);
1095 
1096 	op_display(OP_LOG, OP_MOD_CPU, 0x0003);
1097 }
1098 
cpu_callin(struct cpu_thread * cpu)1099 void cpu_callin(struct cpu_thread *cpu)
1100 {
1101 	cpu->state = cpu_state_active;
1102 	cpu->job_has_no_return = false;
1103 
1104 	init_hid();
1105 }
1106 
opal_start_thread_job(void * data)1107 static void opal_start_thread_job(void *data)
1108 {
1109 	cpu_give_self_os();
1110 
1111 	/* We do not return, so let's mark the job as
1112 	 * complete
1113 	 */
1114 	start_kernel_secondary((uint64_t)data);
1115 }
1116 
opal_start_cpu_thread(uint64_t server_no,uint64_t start_address)1117 static int64_t opal_start_cpu_thread(uint64_t server_no, uint64_t start_address)
1118 {
1119 	struct cpu_thread *cpu;
1120 	struct cpu_job *job;
1121 
1122 	if (!opal_addr_valid((void *)start_address))
1123 		return OPAL_PARAMETER;
1124 
1125 	cpu = find_cpu_by_server(server_no);
1126 	if (!cpu) {
1127 		prerror("OPAL: Start invalid CPU 0x%04llx !\n", server_no);
1128 		return OPAL_PARAMETER;
1129 	}
1130 	prlog(PR_DEBUG, "OPAL: Start CPU 0x%04llx (PIR 0x%04x) -> 0x%016llx\n",
1131 	       server_no, cpu->pir, start_address);
1132 
1133 	lock(&reinit_lock);
1134 	if (!cpu_is_available(cpu)) {
1135 		unlock(&reinit_lock);
1136 		prerror("OPAL: CPU not active in OPAL !\n");
1137 		return OPAL_WRONG_STATE;
1138 	}
1139 	if (cpu->in_reinit) {
1140 		unlock(&reinit_lock);
1141 		prerror("OPAL: CPU being reinitialized !\n");
1142 		return OPAL_WRONG_STATE;
1143 	}
1144 	job = __cpu_queue_job(cpu, "start_thread",
1145 			      opal_start_thread_job, (void *)start_address,
1146 			      true);
1147 	unlock(&reinit_lock);
1148 	if (!job) {
1149 		prerror("OPAL: Failed to create CPU start job !\n");
1150 		return OPAL_INTERNAL_ERROR;
1151 	}
1152 	return OPAL_SUCCESS;
1153 }
1154 opal_call(OPAL_START_CPU, opal_start_cpu_thread, 2);
1155 
opal_query_cpu_status(uint64_t server_no,uint8_t * thread_status)1156 static int64_t opal_query_cpu_status(uint64_t server_no, uint8_t *thread_status)
1157 {
1158 	struct cpu_thread *cpu;
1159 
1160 	if (!opal_addr_valid(thread_status))
1161 		return OPAL_PARAMETER;
1162 
1163 	cpu = find_cpu_by_server(server_no);
1164 	if (!cpu) {
1165 		prerror("OPAL: Query invalid CPU 0x%04llx !\n", server_no);
1166 		return OPAL_PARAMETER;
1167 	}
1168 	if (!cpu_is_available(cpu) && cpu->state != cpu_state_os) {
1169 		prerror("OPAL: CPU not active in OPAL nor OS !\n");
1170 		return OPAL_PARAMETER;
1171 	}
1172 	switch(cpu->state) {
1173 	case cpu_state_os:
1174 		*thread_status = OPAL_THREAD_STARTED;
1175 		break;
1176 	case cpu_state_active:
1177 		/* Active in skiboot -> inactive in OS */
1178 		*thread_status = OPAL_THREAD_INACTIVE;
1179 		break;
1180 	default:
1181 		*thread_status = OPAL_THREAD_UNAVAILABLE;
1182 	}
1183 
1184 	return OPAL_SUCCESS;
1185 }
1186 opal_call(OPAL_QUERY_CPU_STATUS, opal_query_cpu_status, 2);
1187 
opal_return_cpu(void)1188 static int64_t opal_return_cpu(void)
1189 {
1190 	prlog(PR_DEBUG, "OPAL: Returning CPU 0x%04x\n", this_cpu()->pir);
1191 
1192 	__secondary_cpu_entry();
1193 
1194 	return OPAL_HARDWARE; /* Should not happen */
1195 }
1196 opal_call(OPAL_RETURN_CPU, opal_return_cpu, 0);
1197 
1198 struct hid0_change_req {
1199 	uint64_t clr_bits;
1200 	uint64_t set_bits;
1201 };
1202 
cpu_change_hid0(void * __req)1203 static void cpu_change_hid0(void *__req)
1204 {
1205 	struct hid0_change_req *req = __req;
1206 	unsigned long hid0, new_hid0;
1207 
1208 	hid0 = new_hid0 = mfspr(SPR_HID0);
1209 	new_hid0 &= ~req->clr_bits;
1210 	new_hid0 |= req->set_bits;
1211 	prlog(PR_DEBUG, "CPU: [%08x] HID0 change 0x%016lx -> 0x%016lx\n",
1212 		this_cpu()->pir, hid0, new_hid0);
1213 	set_hid0(new_hid0);
1214 }
1215 
cpu_change_all_hid0(struct hid0_change_req * req)1216 static int64_t cpu_change_all_hid0(struct hid0_change_req *req)
1217 {
1218 	struct cpu_thread *cpu;
1219 
1220 	for_each_available_cpu(cpu) {
1221 		if (!cpu_is_thread0(cpu))
1222 			continue;
1223 		if (cpu == this_cpu()) {
1224 			cpu_change_hid0(req);
1225 			continue;
1226 		}
1227 		cpu_wait_job(cpu_queue_job(cpu, "cpu_change_hid0",
1228 			cpu_change_hid0, req), true);
1229 	}
1230 	return OPAL_SUCCESS;
1231 }
1232 
cpu_set_radix_mode(void)1233 void cpu_set_radix_mode(void)
1234 {
1235 	struct hid0_change_req req;
1236 
1237 	if (!radix_supported)
1238 		return;
1239 
1240 	req.clr_bits = 0;
1241 	req.set_bits = SPR_HID0_POWER9_RADIX;
1242 	cleanup_global_tlb();
1243 	current_radix_mode = true;
1244 	cpu_change_all_hid0(&req);
1245 }
1246 
cpu_cleanup_one(void * param __unused)1247 static void cpu_cleanup_one(void *param __unused)
1248 {
1249 	mtspr(SPR_AMR, 0);
1250 	mtspr(SPR_IAMR, 0);
1251 }
1252 
cpu_cleanup_all(void)1253 static int64_t cpu_cleanup_all(void)
1254 {
1255 	struct cpu_thread *cpu;
1256 
1257 	for_each_available_cpu(cpu) {
1258 		if (cpu == this_cpu()) {
1259 			cpu_cleanup_one(NULL);
1260 			continue;
1261 		}
1262 		cpu_wait_job(cpu_queue_job(cpu, "cpu_cleanup",
1263 					   cpu_cleanup_one, NULL), true);
1264 	}
1265 	return OPAL_SUCCESS;
1266 }
1267 
cpu_fast_reboot_complete(void)1268 void cpu_fast_reboot_complete(void)
1269 {
1270 	/* Fast reboot will have cleared HID0:HILE */
1271 	current_hile_mode = false;
1272 
1273 	/* On P9, restore radix mode */
1274 	cpu_set_radix_mode();
1275 }
1276 
opal_reinit_cpus(uint64_t flags)1277 static int64_t opal_reinit_cpus(uint64_t flags)
1278 {
1279 	struct hid0_change_req req = { 0, 0 };
1280 	struct cpu_thread *cpu;
1281 	int64_t rc = OPAL_SUCCESS;
1282 	int i;
1283 
1284 	prlog(PR_DEBUG, "OPAL: CPU re-init with flags: 0x%llx\n", flags);
1285 
1286 	if (flags & OPAL_REINIT_CPUS_HILE_LE)
1287 		prlog(PR_NOTICE, "OPAL: Switch to little-endian OS\n");
1288 	else if (flags & OPAL_REINIT_CPUS_HILE_BE)
1289 		prlog(PR_NOTICE, "OPAL: Switch to big-endian OS\n");
1290 
1291  again:
1292 	lock(&reinit_lock);
1293 
1294 	for (cpu = first_cpu(); cpu; cpu = next_cpu(cpu)) {
1295 		if (cpu == this_cpu() || cpu->in_reinit)
1296 			continue;
1297 		if (cpu->state == cpu_state_os) {
1298 			unlock(&reinit_lock);
1299 			/*
1300 			 * That might be a race with return CPU during kexec
1301 			 * where we are still, wait a bit and try again
1302 			 */
1303 			for (i = 0; (i < 1000) &&
1304 				     (cpu->state == cpu_state_os); i++) {
1305 				time_wait_ms(1);
1306 			}
1307 			if (cpu->state == cpu_state_os) {
1308 				prerror("OPAL: CPU 0x%x not in OPAL !\n", cpu->pir);
1309 				return OPAL_WRONG_STATE;
1310 			}
1311 			goto again;
1312 		}
1313 		cpu->in_reinit = true;
1314 	}
1315 	/*
1316 	 * Now we need to mark ourselves "active" or we'll be skipped
1317 	 * by the various "for_each_active_..." calls done by slw_reinit()
1318 	 */
1319 	this_cpu()->state = cpu_state_active;
1320 	this_cpu()->in_reinit = true;
1321 	unlock(&reinit_lock);
1322 
1323 	/*
1324 	 * This cleans up a few things left over by Linux
1325 	 * that can cause problems in cases such as radix->hash
1326 	 * transitions. Ideally Linux should do it but doing it
1327 	 * here works around existing broken kernels.
1328 	 */
1329 	cpu_cleanup_all();
1330 
1331 	/* If HILE change via HID0 is supported ... */
1332 	if (hile_supported &&
1333 	    (flags & (OPAL_REINIT_CPUS_HILE_BE |
1334 		      OPAL_REINIT_CPUS_HILE_LE))) {
1335 		bool hile = !!(flags & OPAL_REINIT_CPUS_HILE_LE);
1336 
1337 		flags &= ~(OPAL_REINIT_CPUS_HILE_BE | OPAL_REINIT_CPUS_HILE_LE);
1338 		if (hile != current_hile_mode) {
1339 			if (hile)
1340 				req.set_bits |= hid0_hile;
1341 			else
1342 				req.clr_bits |= hid0_hile;
1343 			current_hile_mode = hile;
1344 		}
1345 	}
1346 
1347 	/* If MMU mode change is supported */
1348 	if (radix_supported &&
1349 	    (flags & (OPAL_REINIT_CPUS_MMU_HASH |
1350 		      OPAL_REINIT_CPUS_MMU_RADIX))) {
1351 		bool radix = !!(flags & OPAL_REINIT_CPUS_MMU_RADIX);
1352 
1353 		flags &= ~(OPAL_REINIT_CPUS_MMU_HASH |
1354 			   OPAL_REINIT_CPUS_MMU_RADIX);
1355 		if (radix != current_radix_mode) {
1356 			if (radix)
1357 				req.set_bits |= SPR_HID0_POWER9_RADIX;
1358 			else
1359 				req.clr_bits |= SPR_HID0_POWER9_RADIX;
1360 
1361 			current_radix_mode = radix;
1362 		}
1363 	}
1364 
1365 	/* Cleanup the TLB. We do that unconditionally, this works
1366 	 * around issues where OSes fail to invalidate the PWC in Radix
1367 	 * mode for example. This only works on P9 and later, but we
1368 	 * also know we don't have a problem with Linux cleanups on
1369 	 * P8 so this isn't a problem. If we wanted to cleanup the
1370 	 * TLB on P8 as well, we'd have to use jobs to do it locally
1371 	 * on each CPU.
1372 	 */
1373 	 cleanup_global_tlb();
1374 
1375 	 /* Apply HID bits changes if any */
1376 	if (req.set_bits || req.clr_bits)
1377 		cpu_change_all_hid0(&req);
1378 
1379 	/* If we have a P7, error out for LE switch, do nothing for BE */
1380 	if (proc_gen < proc_gen_p8) {
1381 		if (flags & OPAL_REINIT_CPUS_HILE_LE)
1382 			rc = OPAL_UNSUPPORTED;
1383 		flags &= ~(OPAL_REINIT_CPUS_HILE_BE | OPAL_REINIT_CPUS_HILE_LE);
1384 	}
1385 
1386 	if (flags & OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED) {
1387 		flags &= ~OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED;
1388 
1389 		/*
1390 		 * Pending a hostboot change we can't determine the status of
1391 		 * this, so it always fails.
1392 		 */
1393 		rc = OPAL_UNSUPPORTED;
1394 	}
1395 
1396 	/* Handle P8 DD1 SLW reinit */
1397 	if (flags != 0 && proc_gen == proc_gen_p8 && !hile_supported)
1398 		rc = slw_reinit(flags);
1399 	else if (flags != 0)
1400 		rc = OPAL_UNSUPPORTED;
1401 
1402 	/* And undo the above */
1403 	lock(&reinit_lock);
1404 	this_cpu()->state = cpu_state_os;
1405 	for (cpu = first_cpu(); cpu; cpu = next_cpu(cpu))
1406 		cpu->in_reinit = false;
1407 	unlock(&reinit_lock);
1408 
1409 	return rc;
1410 }
1411 opal_call(OPAL_REINIT_CPUS, opal_reinit_cpus, 1);
1412 
1413 #define NMMU_XLAT_CTL_PTCR 0xb
nmmu_set_ptcr(uint64_t chip_id,struct dt_node * node,uint64_t ptcr)1414 static int64_t nmmu_set_ptcr(uint64_t chip_id, struct dt_node *node, uint64_t ptcr)
1415 {
1416 	uint32_t nmmu_base_addr;
1417 
1418 	nmmu_base_addr = dt_get_address(node, 0, NULL);
1419 	return xscom_write(chip_id, nmmu_base_addr + NMMU_XLAT_CTL_PTCR, ptcr);
1420 }
1421 
1422 /*
1423  * Setup the the Nest MMU PTCR register for all chips in the system or
1424  * the specified chip id.
1425  *
1426  * The PTCR value may be overwritten so long as all users have been
1427  * quiesced. If it is set to an invalid memory address the system will
1428  * checkstop if anything attempts to use it.
1429  *
1430  * Returns OPAL_UNSUPPORTED if no nest mmu was found.
1431  */
opal_nmmu_set_ptcr(uint64_t chip_id,uint64_t ptcr)1432 static int64_t opal_nmmu_set_ptcr(uint64_t chip_id, uint64_t ptcr)
1433 {
1434 	struct dt_node *node;
1435 	int64_t rc = OPAL_UNSUPPORTED;
1436 
1437 	if (chip_id == -1ULL)
1438 		dt_for_each_compatible(dt_root, node, "ibm,power9-nest-mmu") {
1439 			chip_id = dt_get_chip_id(node);
1440 			if ((rc = nmmu_set_ptcr(chip_id, node, ptcr)))
1441 				return rc;
1442 		}
1443 	else
1444 		dt_for_each_compatible_on_chip(dt_root, node, "ibm,power9-nest-mmu", chip_id)
1445 			if ((rc = nmmu_set_ptcr(chip_id, node, ptcr)))
1446 				return rc;
1447 
1448 	return rc;
1449 }
1450 opal_call(OPAL_NMMU_SET_PTCR, opal_nmmu_set_ptcr, 2);
1451