xref: /openbmc/linux/arch/parisc/kernel/alternative.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Alternative live-patching for parisc.
4  *    Copyright (C) 2018 Helge Deller <deller@gmx.de>
5  *
6  */
7 
8 #include <asm/processor.h>
9 #include <asm/sections.h>
10 #include <asm/alternative.h>
11 
12 #include <linux/module.h>
13 
14 static int no_alternatives;
15 static int __init setup_no_alternatives(char *str)
16 {
17 	no_alternatives = 1;
18 	return 1;
19 }
20 __setup("no-alternatives", setup_no_alternatives);
21 
22 void __init_or_module apply_alternatives(struct alt_instr *start,
23 		 struct alt_instr *end, const char *module_name)
24 {
25 	struct alt_instr *entry;
26 	int index = 0, applied = 0;
27 	int num_cpus = num_online_cpus();
28 
29 	for (entry = start; entry < end; entry++, index++) {
30 
31 		u32 *from, len, cond, replacement;
32 
33 		from = (u32 *)((ulong)&entry->orig_offset + entry->orig_offset);
34 		len = entry->len;
35 		cond = entry->cond;
36 		replacement = entry->replacement;
37 
38 		WARN_ON(!cond);
39 
40 		if (cond != ALT_COND_ALWAYS && no_alternatives)
41 			continue;
42 
43 		pr_debug("Check %d: Cond 0x%x, Replace %02d instructions @ 0x%px with 0x%08x\n",
44 			index, cond, len, from, replacement);
45 
46 		if ((cond & ALT_COND_NO_SMP) && (num_cpus != 1))
47 			continue;
48 		if ((cond & ALT_COND_NO_DCACHE) && (cache_info.dc_size != 0))
49 			continue;
50 		if ((cond & ALT_COND_NO_ICACHE) && (cache_info.ic_size != 0))
51 			continue;
52 
53 		/*
54 		 * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit
55 		 * set (bit #61, big endian), we have to flush and sync every
56 		 * time IO-PDIR is changed in Ike/Astro.
57 		 */
58 		if ((cond & ALT_COND_NO_IOC_FDC) &&
59 			((boot_cpu_data.cpu_type <= pcxw_) ||
60 			 (boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC)))
61 			continue;
62 
63 		/* Want to replace pdtlb by a pdtlb,l instruction? */
64 		if (replacement == INSN_PxTLB) {
65 			replacement = *from;
66 			if (boot_cpu_data.cpu_type >= pcxu) /* >= pa2.0 ? */
67 				replacement |= (1 << 10); /* set el bit */
68 		}
69 
70 		/*
71 		 * Replace instruction with NOPs?
72 		 * For long distance insert a branch instruction instead.
73 		 */
74 		if (replacement == INSN_NOP && len > 1)
75 			replacement = 0xe8000002 + (len-2)*8; /* "b,n .+8" */
76 
77 		pr_debug("Do    %d: Cond 0x%x, Replace %02d instructions @ 0x%px with 0x%08x\n",
78 			index, cond, len, from, replacement);
79 
80 		/* Replace instruction */
81 		*from = replacement;
82 		applied++;
83 	}
84 
85 	pr_info("%s%salternatives: applied %d out of %d patches\n",
86 		module_name ? : "", module_name ? " " : "",
87 		applied, index);
88 }
89 
90 
91 void __init apply_alternatives_all(void)
92 {
93 	set_kernel_text_rw(1);
94 
95 	apply_alternatives((struct alt_instr *) &__alt_instructions,
96 		(struct alt_instr *) &__alt_instructions_end, NULL);
97 
98 	set_kernel_text_rw(0);
99 }
100