blob: 469ee438429581810fa602f2381ab304b4b47826 [file] [log] [blame]
Andres Salomonfd699c72010-06-18 17:46:53 -04001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/init.h>
4#include <asm/page.h>
5#include <asm/setup.h>
6#include <asm/io.h>
7#include <asm/pgtable.h>
8#include <asm/olpc_ofw.h>
9
10/* address of OFW callback interface; will be NULL if OFW isn't found */
11static int (*olpc_ofw_cif)(int *);
12
13/* page dir entry containing OFW's pgdir table; filled in by head_32.S */
14u32 olpc_ofw_pgd __initdata;
15
16static DEFINE_SPINLOCK(ofw_lock);
17
18#define MAXARGS 10
19
20void __init setup_olpc_ofw_pgd(void)
21{
22 pgd_t *base, *ofw_pde;
23
24 if (!olpc_ofw_cif)
25 return;
26
27 /* fetch OFW's PDE */
28 base = early_ioremap(olpc_ofw_pgd, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
29 if (!base) {
30 printk(KERN_ERR "failed to remap OFW's pgd - disabling OFW!\n");
31 olpc_ofw_cif = NULL;
32 return;
33 }
34 ofw_pde = &base[OLPC_OFW_PDE_NR];
35
36 /* install OFW's PDE permanently into the kernel's pgtable */
37 set_pgd(&swapper_pg_dir[OLPC_OFW_PDE_NR], *ofw_pde);
38 early_iounmap(base, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
39}
40
41int __olpc_ofw(const char *name, int nr_args, void **args, int nr_res,
42 void **res)
43{
44 int ofw_args[MAXARGS + 3];
45 unsigned long flags;
46 int ret, i, *p;
47
48 BUG_ON(nr_args + nr_res > MAXARGS);
49
50 if (!olpc_ofw_cif)
51 return -EIO;
52
53 ofw_args[0] = (int)name;
54 ofw_args[1] = nr_args;
55 ofw_args[2] = nr_res;
56
57 p = &ofw_args[3];
58 for (i = 0; i < nr_args; i++, p++)
59 *p = (int)args[i];
60
61 /* call into ofw */
62 spin_lock_irqsave(&ofw_lock, flags);
63 ret = olpc_ofw_cif(ofw_args);
64 spin_unlock_irqrestore(&ofw_lock, flags);
65
66 if (!ret) {
67 for (i = 0; i < nr_res; i++, p++)
68 *((int *)res[i]) = *p;
69 }
70
71 return ret;
72}
73EXPORT_SYMBOL_GPL(__olpc_ofw);
74
75/* OFW cif _should_ be above this address */
76#define OFW_MIN 0xff000000
77
78/* OFW starts on a 1MB boundary */
79#define OFW_BOUND (1<<20)
80
81void __init olpc_ofw_detect(void)
82{
83 struct olpc_ofw_header *hdr = &boot_params.olpc_ofw_header;
84 unsigned long start;
85
86 /* ensure OFW booted us by checking for "OFW " string */
87 if (hdr->ofw_magic != OLPC_OFW_SIG)
88 return;
89
90 olpc_ofw_cif = (int (*)(int *))hdr->cif_handler;
91
92 if ((unsigned long)olpc_ofw_cif < OFW_MIN) {
93 printk(KERN_ERR "OFW detected, but cif has invalid address 0x%lx - disabling.\n",
94 (unsigned long)olpc_ofw_cif);
95 olpc_ofw_cif = NULL;
96 return;
97 }
98
99 /* determine where OFW starts in memory */
100 start = round_down((unsigned long)olpc_ofw_cif, OFW_BOUND);
101 printk(KERN_INFO "OFW detected in memory, cif @ 0x%lx (reserving top %ldMB)\n",
102 (unsigned long)olpc_ofw_cif, (-start) >> 20);
103 reserve_top_address(-start);
104}