oflib.c 4.8 KB

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