blob: 044f34770b9685633532bc716020e2cbe51a6ae0 [file] [log] [blame]
Mark A. Greerb2c5f612006-09-19 14:05:08 +10001/*
2 * Copyright (C) Paul Mackerras 1997.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9#include <stdarg.h>
10#include <stddef.h>
11#include "types.h"
12#include "elf.h"
13#include "string.h"
14#include "stdio.h"
15#include "page.h"
16#include "ops.h"
17
18typedef void *ihandle;
19typedef void *phandle;
20
21extern char _end[];
22
23/* Value picked to match that used by yaboot */
24#define PROG_START 0x01400000 /* only used on 64-bit systems */
25#define RAM_END (512<<20) /* Fixme: use OF */
26#define ONE_MB 0x100000
27
28int (*prom) (void *);
29
30
31static unsigned long claim_base;
32
33static int call_prom(const char *service, int nargs, int nret, ...)
34{
35 int i;
36 struct prom_args {
37 const char *service;
38 int nargs;
39 int nret;
40 unsigned int args[12];
41 } args;
42 va_list list;
43
44 args.service = service;
45 args.nargs = nargs;
46 args.nret = nret;
47
48 va_start(list, nret);
49 for (i = 0; i < nargs; i++)
50 args.args[i] = va_arg(list, unsigned int);
51 va_end(list);
52
53 for (i = 0; i < nret; i++)
54 args.args[nargs+i] = 0;
55
56 if (prom(&args) < 0)
57 return -1;
58
59 return (nret > 0)? args.args[nargs]: 0;
60}
61
62static int call_prom_ret(const char *service, int nargs, int nret,
63 unsigned int *rets, ...)
64{
65 int i;
66 struct prom_args {
67 const char *service;
68 int nargs;
69 int nret;
70 unsigned int args[12];
71 } args;
72 va_list list;
73
74 args.service = service;
75 args.nargs = nargs;
76 args.nret = nret;
77
78 va_start(list, rets);
79 for (i = 0; i < nargs; i++)
80 args.args[i] = va_arg(list, unsigned int);
81 va_end(list);
82
83 for (i = 0; i < nret; i++)
84 args.args[nargs+i] = 0;
85
86 if (prom(&args) < 0)
87 return -1;
88
89 if (rets != (void *) 0)
90 for (i = 1; i < nret; ++i)
91 rets[i-1] = args.args[nargs+i];
92
93 return (nret > 0)? args.args[nargs]: 0;
94}
95
96/*
97 * Older OF's require that when claiming a specific range of addresses,
98 * we claim the physical space in the /memory node and the virtual
99 * space in the chosen mmu node, and then do a map operation to
100 * map virtual to physical.
101 */
102static int need_map = -1;
103static ihandle chosen_mmu;
104static phandle memory;
105
106/* returns true if s2 is a prefix of s1 */
107static int string_match(const char *s1, const char *s2)
108{
109 for (; *s2; ++s2)
110 if (*s1++ != *s2)
111 return 0;
112 return 1;
113}
114
115static int check_of_version(void)
116{
117 phandle oprom, chosen;
118 char version[64];
119
120 oprom = finddevice("/openprom");
121 if (oprom == (phandle) -1)
122 return 0;
123 if (getprop(oprom, "model", version, sizeof(version)) <= 0)
124 return 0;
125 version[sizeof(version)-1] = 0;
126 printf("OF version = '%s'\r\n", version);
127 if (!string_match(version, "Open Firmware, 1.")
128 && !string_match(version, "FirmWorks,3."))
129 return 0;
130 chosen = finddevice("/chosen");
131 if (chosen == (phandle) -1) {
132 chosen = finddevice("/chosen@0");
133 if (chosen == (phandle) -1) {
134 printf("no chosen\n");
135 return 0;
136 }
137 }
138 if (getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
139 printf("no mmu\n");
140 return 0;
141 }
142 memory = (ihandle) call_prom("open", 1, 1, "/memory");
143 if (memory == (ihandle) -1) {
144 memory = (ihandle) call_prom("open", 1, 1, "/memory@0");
145 if (memory == (ihandle) -1) {
146 printf("no memory node\n");
147 return 0;
148 }
149 }
150 printf("old OF detected\r\n");
151 return 1;
152}
153
154static void *claim(unsigned long virt, unsigned long size, unsigned long align)
155{
156 int ret;
157 unsigned int result;
158
159 if (need_map < 0)
160 need_map = check_of_version();
161 if (align || !need_map)
162 return (void *) call_prom("claim", 3, 1, virt, size, align);
163
164 ret = call_prom_ret("call-method", 5, 2, &result, "claim", memory,
165 align, size, virt);
166 if (ret != 0 || result == -1)
167 return (void *) -1;
168 ret = call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
169 align, size, virt);
170 /* 0x12 == coherent + read/write */
171 ret = call_prom("call-method", 6, 1, "map", chosen_mmu,
172 0x12, size, virt, virt);
173 return (void *) virt;
174}
175
176static void *of_try_claim(u32 size)
177{
178 unsigned long addr = 0;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000179
Benjamin Herrenschmidtc998de12006-10-05 14:18:46 +1000180 if (claim_base == 0)
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000181 claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000182
183 for(; claim_base < RAM_END; claim_base += ONE_MB) {
184#ifdef DEBUG
185 printf(" trying: 0x%08lx\n\r", claim_base);
186#endif
187 addr = (unsigned long)claim(claim_base, size, 0);
188 if ((void *)addr != (void *)-1)
189 break;
190 }
191 if (addr == 0)
192 return NULL;
193 claim_base = PAGE_ALIGN(claim_base + size);
194 return (void *)addr;
195}
196
197static void of_image_hdr(const void *hdr)
198{
199 const Elf64_Ehdr *elf64 = hdr;
200
201 if (elf64->e_ident[EI_CLASS] == ELFCLASS64) {
202 /*
203 * Maintain a "magic" minimum address. This keeps some older
204 * firmware platforms running.
205 */
206 if (claim_base < PROG_START)
207 claim_base = PROG_START;
208 }
209}
210
David Gibson79c85412007-03-05 14:24:52 +1100211static void *of_vmlinux_alloc(unsigned long size)
212{
213 void *p = malloc(size);
214
215 if (!p) {
216 printf("Can't allocate memory for kernel image!\n\r");
217 exit();
218 }
219 return p;
220}
221
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000222static void of_exit(void)
223{
224 call_prom("exit", 0, 0);
225}
226
227/*
228 * OF device tree routines
229 */
230static void *of_finddevice(const char *name)
231{
232 return (phandle) call_prom("finddevice", 1, 1, name);
233}
234
235static int of_getprop(const void *phandle, const char *name, void *buf,
236 const int buflen)
237{
238 return call_prom("getprop", 4, 1, phandle, name, buf, buflen);
239}
240
241static int of_setprop(const void *phandle, const char *name, const void *buf,
242 const int buflen)
243{
244 return call_prom("setprop", 4, 1, phandle, name, buf, buflen);
245}
246
247/*
248 * OF console routines
249 */
250static void *of_stdout_handle;
251
252static int of_console_open(void)
253{
254 void *devp;
255
256 if (((devp = finddevice("/chosen")) != NULL)
257 && (getprop(devp, "stdout", &of_stdout_handle,
258 sizeof(of_stdout_handle))
259 == sizeof(of_stdout_handle)))
260 return 0;
261
262 return -1;
263}
264
265static void of_console_write(char *buf, int len)
266{
267 call_prom("write", 3, 1, of_stdout_handle, buf, len);
268}
269
Mark A. Greerc8885542006-10-16 13:49:27 -0700270int platform_init(void *promptr, char *dt_blob_start, char *dt_blob_end)
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000271{
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000272 platform_ops.image_hdr = of_image_hdr;
273 platform_ops.malloc = of_try_claim;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000274 platform_ops.exit = of_exit;
David Gibson79c85412007-03-05 14:24:52 +1100275 platform_ops.vmlinux_alloc = of_vmlinux_alloc;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000276
277 dt_ops.finddevice = of_finddevice;
278 dt_ops.getprop = of_getprop;
279 dt_ops.setprop = of_setprop;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000280
281 console_ops.open = of_console_open;
282 console_ops.write = of_console_write;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000283
284 prom = (int (*)(void *))promptr;
285 return 0;
286}