1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/export.h>
4 #include <linux/spinlock_types.h>
5 #include <linux/init.h>
6 #include <linux/pgtable.h>
10 #include <asm/cpufeature.h>
11 #include <asm/special_insns.h>
12 #include <asm/olpc_ofw.h>
14 /* address of OFW callback interface; will be NULL if OFW isn't found */
15 static int (*olpc_ofw_cif
)(int *);
17 /* page dir entry containing OFW's pgdir table; filled in by head_32.S */
18 u32 olpc_ofw_pgd __initdata
;
20 static DEFINE_SPINLOCK(ofw_lock
);
24 void __init
setup_olpc_ofw_pgd(void)
26 pgd_t
*base
, *ofw_pde
;
32 base
= early_ioremap(olpc_ofw_pgd
, sizeof(olpc_ofw_pgd
) * PTRS_PER_PGD
);
34 printk(KERN_ERR
"failed to remap OFW's pgd - disabling OFW!\n");
38 ofw_pde
= &base
[OLPC_OFW_PDE_NR
];
40 /* install OFW's PDE permanently into the kernel's pgtable */
41 set_pgd(&swapper_pg_dir
[OLPC_OFW_PDE_NR
], *ofw_pde
);
42 /* implicit optimization barrier here due to uninline function return */
44 early_iounmap(base
, sizeof(olpc_ofw_pgd
) * PTRS_PER_PGD
);
47 int __olpc_ofw(const char *name
, int nr_args
, const void **args
, int nr_res
,
50 int ofw_args
[MAXARGS
+ 3];
54 BUG_ON(nr_args
+ nr_res
> MAXARGS
);
59 ofw_args
[0] = (int)name
;
60 ofw_args
[1] = nr_args
;
64 for (i
= 0; i
< nr_args
; i
++, p
++)
68 spin_lock_irqsave(&ofw_lock
, flags
);
69 ret
= olpc_ofw_cif(ofw_args
);
70 spin_unlock_irqrestore(&ofw_lock
, flags
);
73 for (i
= 0; i
< nr_res
; i
++, p
++)
74 *((int *)res
[i
]) = *p
;
79 EXPORT_SYMBOL_GPL(__olpc_ofw
);
81 bool olpc_ofw_present(void)
83 return olpc_ofw_cif
!= NULL
;
85 EXPORT_SYMBOL_GPL(olpc_ofw_present
);
87 /* OFW cif _should_ be above this address */
88 #define OFW_MIN 0xff000000
90 /* OFW starts on a 1MB boundary */
91 #define OFW_BOUND (1<<20)
93 void __init
olpc_ofw_detect(void)
95 struct olpc_ofw_header
*hdr
= &boot_params
.olpc_ofw_header
;
98 /* ensure OFW booted us by checking for "OFW " string */
99 if (hdr
->ofw_magic
!= OLPC_OFW_SIG
)
102 olpc_ofw_cif
= (int (*)(int *))hdr
->cif_handler
;
104 if ((unsigned long)olpc_ofw_cif
< OFW_MIN
) {
105 printk(KERN_ERR
"OFW detected, but cif has invalid address 0x%lx - disabling.\n",
106 (unsigned long)olpc_ofw_cif
);
111 /* determine where OFW starts in memory */
112 start
= round_down((unsigned long)olpc_ofw_cif
, OFW_BOUND
);
113 printk(KERN_INFO
"OFW detected in memory, cif @ 0x%lx (reserving top %ldMB)\n",
114 (unsigned long)olpc_ofw_cif
, (-start
) >> 20);
115 reserve_top_address(-start
);
118 bool __init
olpc_ofw_is_installed(void)
120 return olpc_ofw_cif
!= NULL
;