of.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  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. typedef void *ihandle;
  18. typedef void *phandle;
  19. extern char _end[];
  20. /* Value picked to match that used by yaboot */
  21. #define PROG_START 0x01400000 /* only used on 64-bit systems */
  22. #define RAM_END (512<<20) /* Fixme: use OF */
  23. #define ONE_MB 0x100000
  24. int (*prom) (void *);
  25. static unsigned long claim_base;
  26. static int call_prom(const char *service, int nargs, int nret, ...)
  27. {
  28. int i;
  29. struct prom_args {
  30. const char *service;
  31. int nargs;
  32. int nret;
  33. unsigned int args[12];
  34. } args;
  35. va_list list;
  36. args.service = service;
  37. args.nargs = nargs;
  38. args.nret = nret;
  39. va_start(list, nret);
  40. for (i = 0; i < nargs; i++)
  41. args.args[i] = va_arg(list, unsigned int);
  42. va_end(list);
  43. for (i = 0; i < nret; i++)
  44. args.args[nargs+i] = 0;
  45. if (prom(&args) < 0)
  46. return -1;
  47. return (nret > 0)? args.args[nargs]: 0;
  48. }
  49. static int call_prom_ret(const char *service, int nargs, int nret,
  50. unsigned int *rets, ...)
  51. {
  52. int i;
  53. struct prom_args {
  54. const char *service;
  55. int nargs;
  56. int nret;
  57. unsigned int args[12];
  58. } args;
  59. va_list list;
  60. args.service = service;
  61. args.nargs = nargs;
  62. args.nret = nret;
  63. va_start(list, rets);
  64. for (i = 0; i < nargs; i++)
  65. args.args[i] = va_arg(list, unsigned int);
  66. va_end(list);
  67. for (i = 0; i < nret; i++)
  68. args.args[nargs+i] = 0;
  69. if (prom(&args) < 0)
  70. return -1;
  71. if (rets != (void *) 0)
  72. for (i = 1; i < nret; ++i)
  73. rets[i-1] = args.args[nargs+i];
  74. return (nret > 0)? args.args[nargs]: 0;
  75. }
  76. /*
  77. * Older OF's require that when claiming a specific range of addresses,
  78. * we claim the physical space in the /memory node and the virtual
  79. * space in the chosen mmu node, and then do a map operation to
  80. * map virtual to physical.
  81. */
  82. static int need_map = -1;
  83. static ihandle chosen_mmu;
  84. static phandle memory;
  85. /* returns true if s2 is a prefix of s1 */
  86. static int string_match(const char *s1, const char *s2)
  87. {
  88. for (; *s2; ++s2)
  89. if (*s1++ != *s2)
  90. return 0;
  91. return 1;
  92. }
  93. static int check_of_version(void)
  94. {
  95. phandle oprom, chosen;
  96. char version[64];
  97. oprom = finddevice("/openprom");
  98. if (oprom == (phandle) -1)
  99. return 0;
  100. if (getprop(oprom, "model", version, sizeof(version)) <= 0)
  101. return 0;
  102. version[sizeof(version)-1] = 0;
  103. printf("OF version = '%s'\r\n", version);
  104. if (!string_match(version, "Open Firmware, 1.")
  105. && !string_match(version, "FirmWorks,3."))
  106. return 0;
  107. chosen = finddevice("/chosen");
  108. if (chosen == (phandle) -1) {
  109. chosen = finddevice("/chosen@0");
  110. if (chosen == (phandle) -1) {
  111. printf("no chosen\n");
  112. return 0;
  113. }
  114. }
  115. if (getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
  116. printf("no mmu\n");
  117. return 0;
  118. }
  119. memory = (ihandle) call_prom("open", 1, 1, "/memory");
  120. if (memory == (ihandle) -1) {
  121. memory = (ihandle) call_prom("open", 1, 1, "/memory@0");
  122. if (memory == (ihandle) -1) {
  123. printf("no memory node\n");
  124. return 0;
  125. }
  126. }
  127. printf("old OF detected\r\n");
  128. return 1;
  129. }
  130. static void *claim(unsigned long virt, unsigned long size, unsigned long align)
  131. {
  132. int ret;
  133. unsigned int result;
  134. if (need_map < 0)
  135. need_map = check_of_version();
  136. if (align || !need_map)
  137. return (void *) call_prom("claim", 3, 1, virt, size, align);
  138. ret = call_prom_ret("call-method", 5, 2, &result, "claim", memory,
  139. align, size, virt);
  140. if (ret != 0 || result == -1)
  141. return (void *) -1;
  142. ret = call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
  143. align, size, virt);
  144. /* 0x12 == coherent + read/write */
  145. ret = call_prom("call-method", 6, 1, "map", chosen_mmu,
  146. 0x12, size, virt, virt);
  147. return (void *) virt;
  148. }
  149. static void *of_try_claim(u32 size)
  150. {
  151. unsigned long addr = 0;
  152. if (claim_base == 0)
  153. claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
  154. for(; claim_base < RAM_END; claim_base += ONE_MB) {
  155. #ifdef DEBUG
  156. printf(" trying: 0x%08lx\n\r", claim_base);
  157. #endif
  158. addr = (unsigned long)claim(claim_base, size, 0);
  159. if ((void *)addr != (void *)-1)
  160. break;
  161. }
  162. if (addr == 0)
  163. return NULL;
  164. claim_base = PAGE_ALIGN(claim_base + size);
  165. return (void *)addr;
  166. }
  167. static void of_image_hdr(const void *hdr)
  168. {
  169. const Elf64_Ehdr *elf64 = hdr;
  170. if (elf64->e_ident[EI_CLASS] == ELFCLASS64) {
  171. /*
  172. * Maintain a "magic" minimum address. This keeps some older
  173. * firmware platforms running.
  174. */
  175. if (claim_base < PROG_START)
  176. claim_base = PROG_START;
  177. }
  178. }
  179. static void of_exit(void)
  180. {
  181. call_prom("exit", 0, 0);
  182. }
  183. /*
  184. * OF device tree routines
  185. */
  186. static void *of_finddevice(const char *name)
  187. {
  188. return (phandle) call_prom("finddevice", 1, 1, name);
  189. }
  190. static int of_getprop(const void *phandle, const char *name, void *buf,
  191. const int buflen)
  192. {
  193. return call_prom("getprop", 4, 1, phandle, name, buf, buflen);
  194. }
  195. static int of_setprop(const void *phandle, const char *name, const void *buf,
  196. const int buflen)
  197. {
  198. return call_prom("setprop", 4, 1, phandle, name, buf, buflen);
  199. }
  200. /*
  201. * OF console routines
  202. */
  203. static void *of_stdout_handle;
  204. static int of_console_open(void)
  205. {
  206. void *devp;
  207. if (((devp = finddevice("/chosen")) != NULL)
  208. && (getprop(devp, "stdout", &of_stdout_handle,
  209. sizeof(of_stdout_handle))
  210. == sizeof(of_stdout_handle)))
  211. return 0;
  212. return -1;
  213. }
  214. static void of_console_write(char *buf, int len)
  215. {
  216. call_prom("write", 3, 1, of_stdout_handle, buf, len);
  217. }
  218. int platform_init(void *promptr)
  219. {
  220. platform_ops.fixups = NULL;
  221. platform_ops.image_hdr = of_image_hdr;
  222. platform_ops.malloc = of_try_claim;
  223. platform_ops.free = NULL;
  224. platform_ops.exit = of_exit;
  225. dt_ops.finddevice = of_finddevice;
  226. dt_ops.getprop = of_getprop;
  227. dt_ops.setprop = of_setprop;
  228. dt_ops.translate_addr = NULL;
  229. console_ops.open = of_console_open;
  230. console_ops.write = of_console_write;
  231. console_ops.edit_cmdline = NULL;
  232. console_ops.close = NULL;
  233. console_ops.data = NULL;
  234. prom = (int (*)(void *))promptr;
  235. return 0;
  236. }