oflib.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <stddef.h>
  10. #include "types.h"
  11. #include "elf.h"
  12. #include "string.h"
  13. #include "stdio.h"
  14. #include "page.h"
  15. #include "ops.h"
  16. #include "of.h"
  17. typedef u32 prom_arg_t;
  18. /* The following structure is used to communicate with open firmware.
  19. * All arguments in and out are in big endian format. */
  20. struct prom_args {
  21. __be32 service; /* Address of service name string. */
  22. __be32 nargs; /* Number of input arguments. */
  23. __be32 nret; /* Number of output arguments. */
  24. __be32 args[10]; /* Input/output arguments. */
  25. };
  26. #ifdef __powerpc64__
  27. extern int prom(void *);
  28. #else
  29. static int (*prom) (void *);
  30. #endif
  31. void of_init(void *promptr)
  32. {
  33. #ifndef __powerpc64__
  34. prom = (int (*)(void *))promptr;
  35. #endif
  36. }
  37. #define ADDR(x) (u32)(unsigned long)(x)
  38. int of_call_prom(const char *service, int nargs, int nret, ...)
  39. {
  40. int i;
  41. struct prom_args args;
  42. va_list list;
  43. args.service = cpu_to_be32(ADDR(service));
  44. args.nargs = cpu_to_be32(nargs);
  45. args.nret = cpu_to_be32(nret);
  46. va_start(list, nret);
  47. for (i = 0; i < nargs; i++)
  48. args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
  49. va_end(list);
  50. for (i = 0; i < nret; i++)
  51. args.args[nargs+i] = 0;
  52. if (prom(&args) < 0)
  53. return PROM_ERROR;
  54. return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
  55. }
  56. static int of_call_prom_ret(const char *service, int nargs, int nret,
  57. prom_arg_t *rets, ...)
  58. {
  59. int i;
  60. struct prom_args args;
  61. va_list list;
  62. args.service = cpu_to_be32(ADDR(service));
  63. args.nargs = cpu_to_be32(nargs);
  64. args.nret = cpu_to_be32(nret);
  65. va_start(list, rets);
  66. for (i = 0; i < nargs; i++)
  67. args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
  68. va_end(list);
  69. for (i = 0; i < nret; i++)
  70. args.args[nargs+i] = 0;
  71. if (prom(&args) < 0)
  72. return PROM_ERROR;
  73. if (rets != NULL)
  74. for (i = 1; i < nret; ++i)
  75. rets[i-1] = be32_to_cpu(args.args[nargs+i]);
  76. return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
  77. }
  78. /* returns true if s2 is a prefix of s1 */
  79. static int string_match(const char *s1, const char *s2)
  80. {
  81. for (; *s2; ++s2)
  82. if (*s1++ != *s2)
  83. return 0;
  84. return 1;
  85. }
  86. /*
  87. * Older OF's require that when claiming a specific range of addresses,
  88. * we claim the physical space in the /memory node and the virtual
  89. * space in the chosen mmu node, and then do a map operation to
  90. * map virtual to physical.
  91. */
  92. static int need_map = -1;
  93. static ihandle chosen_mmu;
  94. static ihandle memory;
  95. static int check_of_version(void)
  96. {
  97. phandle oprom, chosen;
  98. char version[64];
  99. oprom = of_finddevice("/openprom");
  100. if (oprom == (phandle) -1)
  101. return 0;
  102. if (of_getprop(oprom, "model", version, sizeof(version)) <= 0)
  103. return 0;
  104. version[sizeof(version)-1] = 0;
  105. printf("OF version = '%s'\r\n", version);
  106. if (!string_match(version, "Open Firmware, 1.")
  107. && !string_match(version, "FirmWorks,3."))
  108. return 0;
  109. chosen = of_finddevice("/chosen");
  110. if (chosen == (phandle) -1) {
  111. chosen = of_finddevice("/chosen@0");
  112. if (chosen == (phandle) -1) {
  113. printf("no chosen\n");
  114. return 0;
  115. }
  116. }
  117. if (of_getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
  118. printf("no mmu\n");
  119. return 0;
  120. }
  121. memory = of_call_prom("open", 1, 1, "/memory");
  122. if (memory == PROM_ERROR) {
  123. memory = of_call_prom("open", 1, 1, "/memory@0");
  124. if (memory == PROM_ERROR) {
  125. printf("no memory node\n");
  126. return 0;
  127. }
  128. }
  129. printf("old OF detected\r\n");
  130. return 1;
  131. }
  132. unsigned int of_claim(unsigned long virt, unsigned long size,
  133. unsigned long align)
  134. {
  135. int ret;
  136. prom_arg_t result;
  137. if (need_map < 0)
  138. need_map = check_of_version();
  139. if (align || !need_map)
  140. return of_call_prom("claim", 3, 1, virt, size, align);
  141. ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", memory,
  142. align, size, virt);
  143. if (ret != 0 || result == -1)
  144. return -1;
  145. ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
  146. align, size, virt);
  147. /* 0x12 == coherent + read/write */
  148. ret = of_call_prom("call-method", 6, 1, "map", chosen_mmu,
  149. 0x12, size, virt, virt);
  150. return virt;
  151. }
  152. void *of_vmlinux_alloc(unsigned long size)
  153. {
  154. unsigned long start = (unsigned long)_start, end = (unsigned long)_end;
  155. unsigned long addr;
  156. void *p;
  157. /* With some older POWER4 firmware we need to claim the area the kernel
  158. * will reside in. Newer firmwares don't need this so we just ignore
  159. * the return value.
  160. */
  161. addr = (unsigned long) of_claim(start, end - start, 0);
  162. printf("Trying to claim from 0x%lx to 0x%lx (0x%lx) got %lx\r\n",
  163. start, end, end - start, addr);
  164. p = malloc(size);
  165. if (!p)
  166. fatal("Can't allocate memory for kernel image!\n\r");
  167. return p;
  168. }
  169. void of_exit(void)
  170. {
  171. of_call_prom("exit", 0, 0);
  172. }
  173. /*
  174. * OF device tree routines
  175. */
  176. void *of_finddevice(const char *name)
  177. {
  178. return (void *) (unsigned long) of_call_prom("finddevice", 1, 1, name);
  179. }
  180. int of_getprop(const void *phandle, const char *name, void *buf,
  181. const int buflen)
  182. {
  183. return of_call_prom("getprop", 4, 1, phandle, name, buf, buflen);
  184. }
  185. int of_setprop(const void *phandle, const char *name, const void *buf,
  186. const int buflen)
  187. {
  188. return of_call_prom("setprop", 4, 1, phandle, name, buf, buflen);
  189. }