1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28654b1c2SThomas Gleixner #include <linux/kernel.h>
3cc3ae7b0SPaul Gortmaker #include <linux/export.h>
4cc3ae7b0SPaul Gortmaker #include <linux/spinlock_types.h>
58654b1c2SThomas Gleixner #include <linux/init.h>
6*65fddcfcSMike Rapoport #include <linux/pgtable.h>
78654b1c2SThomas Gleixner #include <asm/page.h>
88654b1c2SThomas Gleixner #include <asm/setup.h>
98654b1c2SThomas Gleixner #include <asm/io.h>
10cc3ae7b0SPaul Gortmaker #include <asm/cpufeature.h>
11cc3ae7b0SPaul Gortmaker #include <asm/special_insns.h>
128654b1c2SThomas Gleixner #include <asm/olpc_ofw.h>
138654b1c2SThomas Gleixner
148654b1c2SThomas Gleixner /* address of OFW callback interface; will be NULL if OFW isn't found */
158654b1c2SThomas Gleixner static int (*olpc_ofw_cif)(int *);
168654b1c2SThomas Gleixner
178654b1c2SThomas Gleixner /* page dir entry containing OFW's pgdir table; filled in by head_32.S */
188654b1c2SThomas Gleixner u32 olpc_ofw_pgd __initdata;
198654b1c2SThomas Gleixner
208654b1c2SThomas Gleixner static DEFINE_SPINLOCK(ofw_lock);
218654b1c2SThomas Gleixner
228654b1c2SThomas Gleixner #define MAXARGS 10
238654b1c2SThomas Gleixner
setup_olpc_ofw_pgd(void)248654b1c2SThomas Gleixner void __init setup_olpc_ofw_pgd(void)
258654b1c2SThomas Gleixner {
268654b1c2SThomas Gleixner pgd_t *base, *ofw_pde;
278654b1c2SThomas Gleixner
288654b1c2SThomas Gleixner if (!olpc_ofw_cif)
298654b1c2SThomas Gleixner return;
308654b1c2SThomas Gleixner
318654b1c2SThomas Gleixner /* fetch OFW's PDE */
328654b1c2SThomas Gleixner base = early_ioremap(olpc_ofw_pgd, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
338654b1c2SThomas Gleixner if (!base) {
348654b1c2SThomas Gleixner printk(KERN_ERR "failed to remap OFW's pgd - disabling OFW!\n");
358654b1c2SThomas Gleixner olpc_ofw_cif = NULL;
368654b1c2SThomas Gleixner return;
378654b1c2SThomas Gleixner }
388654b1c2SThomas Gleixner ofw_pde = &base[OLPC_OFW_PDE_NR];
398654b1c2SThomas Gleixner
408654b1c2SThomas Gleixner /* install OFW's PDE permanently into the kernel's pgtable */
418654b1c2SThomas Gleixner set_pgd(&swapper_pg_dir[OLPC_OFW_PDE_NR], *ofw_pde);
428654b1c2SThomas Gleixner /* implicit optimization barrier here due to uninline function return */
438654b1c2SThomas Gleixner
448654b1c2SThomas Gleixner early_iounmap(base, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
458654b1c2SThomas Gleixner }
468654b1c2SThomas Gleixner
__olpc_ofw(const char * name,int nr_args,const void ** args,int nr_res,void ** res)478654b1c2SThomas Gleixner int __olpc_ofw(const char *name, int nr_args, const void **args, int nr_res,
488654b1c2SThomas Gleixner void **res)
498654b1c2SThomas Gleixner {
508654b1c2SThomas Gleixner int ofw_args[MAXARGS + 3];
518654b1c2SThomas Gleixner unsigned long flags;
528654b1c2SThomas Gleixner int ret, i, *p;
538654b1c2SThomas Gleixner
548654b1c2SThomas Gleixner BUG_ON(nr_args + nr_res > MAXARGS);
558654b1c2SThomas Gleixner
568654b1c2SThomas Gleixner if (!olpc_ofw_cif)
578654b1c2SThomas Gleixner return -EIO;
588654b1c2SThomas Gleixner
598654b1c2SThomas Gleixner ofw_args[0] = (int)name;
608654b1c2SThomas Gleixner ofw_args[1] = nr_args;
618654b1c2SThomas Gleixner ofw_args[2] = nr_res;
628654b1c2SThomas Gleixner
638654b1c2SThomas Gleixner p = &ofw_args[3];
648654b1c2SThomas Gleixner for (i = 0; i < nr_args; i++, p++)
658654b1c2SThomas Gleixner *p = (int)args[i];
668654b1c2SThomas Gleixner
678654b1c2SThomas Gleixner /* call into ofw */
688654b1c2SThomas Gleixner spin_lock_irqsave(&ofw_lock, flags);
698654b1c2SThomas Gleixner ret = olpc_ofw_cif(ofw_args);
708654b1c2SThomas Gleixner spin_unlock_irqrestore(&ofw_lock, flags);
718654b1c2SThomas Gleixner
728654b1c2SThomas Gleixner if (!ret) {
738654b1c2SThomas Gleixner for (i = 0; i < nr_res; i++, p++)
748654b1c2SThomas Gleixner *((int *)res[i]) = *p;
758654b1c2SThomas Gleixner }
768654b1c2SThomas Gleixner
778654b1c2SThomas Gleixner return ret;
788654b1c2SThomas Gleixner }
798654b1c2SThomas Gleixner EXPORT_SYMBOL_GPL(__olpc_ofw);
808654b1c2SThomas Gleixner
olpc_ofw_present(void)818654b1c2SThomas Gleixner bool olpc_ofw_present(void)
828654b1c2SThomas Gleixner {
838654b1c2SThomas Gleixner return olpc_ofw_cif != NULL;
848654b1c2SThomas Gleixner }
858654b1c2SThomas Gleixner EXPORT_SYMBOL_GPL(olpc_ofw_present);
868654b1c2SThomas Gleixner
878654b1c2SThomas Gleixner /* OFW cif _should_ be above this address */
888654b1c2SThomas Gleixner #define OFW_MIN 0xff000000
898654b1c2SThomas Gleixner
908654b1c2SThomas Gleixner /* OFW starts on a 1MB boundary */
918654b1c2SThomas Gleixner #define OFW_BOUND (1<<20)
928654b1c2SThomas Gleixner
olpc_ofw_detect(void)938654b1c2SThomas Gleixner void __init olpc_ofw_detect(void)
948654b1c2SThomas Gleixner {
958654b1c2SThomas Gleixner struct olpc_ofw_header *hdr = &boot_params.olpc_ofw_header;
968654b1c2SThomas Gleixner unsigned long start;
978654b1c2SThomas Gleixner
988654b1c2SThomas Gleixner /* ensure OFW booted us by checking for "OFW " string */
998654b1c2SThomas Gleixner if (hdr->ofw_magic != OLPC_OFW_SIG)
1008654b1c2SThomas Gleixner return;
1018654b1c2SThomas Gleixner
1028654b1c2SThomas Gleixner olpc_ofw_cif = (int (*)(int *))hdr->cif_handler;
1038654b1c2SThomas Gleixner
1048654b1c2SThomas Gleixner if ((unsigned long)olpc_ofw_cif < OFW_MIN) {
1058654b1c2SThomas Gleixner printk(KERN_ERR "OFW detected, but cif has invalid address 0x%lx - disabling.\n",
1068654b1c2SThomas Gleixner (unsigned long)olpc_ofw_cif);
1078654b1c2SThomas Gleixner olpc_ofw_cif = NULL;
1088654b1c2SThomas Gleixner return;
1098654b1c2SThomas Gleixner }
1108654b1c2SThomas Gleixner
1118654b1c2SThomas Gleixner /* determine where OFW starts in memory */
1128654b1c2SThomas Gleixner start = round_down((unsigned long)olpc_ofw_cif, OFW_BOUND);
1138654b1c2SThomas Gleixner printk(KERN_INFO "OFW detected in memory, cif @ 0x%lx (reserving top %ldMB)\n",
1148654b1c2SThomas Gleixner (unsigned long)olpc_ofw_cif, (-start) >> 20);
1158654b1c2SThomas Gleixner reserve_top_address(-start);
1168654b1c2SThomas Gleixner }
117c10d1e26SAndres Salomon
olpc_ofw_is_installed(void)118c10d1e26SAndres Salomon bool __init olpc_ofw_is_installed(void)
119c10d1e26SAndres Salomon {
120c10d1e26SAndres Salomon return olpc_ofw_cif != NULL;
121c10d1e26SAndres Salomon }
122