fdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2016 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/fdtbus.h>
  19. #include <grub/fdt.h>
  20. #include <grub/term.h>
  21. static const void *dtb;
  22. static grub_size_t root_address_cells, root_size_cells;
  23. /* Pointer to this symbol signals invalid mapping. */
  24. char grub_fdtbus_invalid_mapping[1];
  25. struct grub_fdtbus_dev *devs;
  26. struct grub_fdtbus_driver *drivers;
  27. int
  28. grub_fdtbus_is_compatible (const char *compat_string,
  29. const struct grub_fdtbus_dev *dev)
  30. {
  31. grub_size_t compatible_size;
  32. const char *compatible = grub_fdt_get_prop (dtb, dev->node, "compatible",
  33. &compatible_size);
  34. if (!compatible)
  35. return 0;
  36. const char *compatible_end = compatible + compatible_size;
  37. while (compatible < compatible_end)
  38. {
  39. if (grub_strcmp (compat_string, compatible) == 0)
  40. return 1;
  41. compatible += grub_strlen (compatible) + 1;
  42. }
  43. return 0;
  44. }
  45. static void
  46. fdtbus_scan (struct grub_fdtbus_dev *parent)
  47. {
  48. int node;
  49. for (node = grub_fdt_first_node (dtb, parent ? parent->node : 0); node >= 0;
  50. node = grub_fdt_next_node (dtb, node))
  51. {
  52. struct grub_fdtbus_dev *dev;
  53. struct grub_fdtbus_driver *driver;
  54. dev = grub_zalloc (sizeof (*dev));
  55. if (!dev)
  56. {
  57. grub_print_error ();
  58. return;
  59. }
  60. dev->node = node;
  61. dev->next = devs;
  62. dev->parent = parent;
  63. devs = dev;
  64. FOR_LIST_ELEMENTS(driver, drivers)
  65. if (!dev->driver && grub_fdtbus_is_compatible (driver->compatible, dev))
  66. {
  67. grub_dprintf ("fdtbus", "Attaching %s\n", driver->compatible);
  68. if (driver->attach (dev) == GRUB_ERR_NONE)
  69. {
  70. grub_dprintf ("fdtbus", "Attached %s\n", driver->compatible);
  71. dev->driver = driver;
  72. break;
  73. }
  74. grub_print_error ();
  75. }
  76. fdtbus_scan (dev);
  77. }
  78. }
  79. void
  80. grub_fdtbus_register (struct grub_fdtbus_driver *driver)
  81. {
  82. struct grub_fdtbus_dev *dev;
  83. grub_dprintf ("fdtbus", "Registering %s\n", driver->compatible);
  84. grub_list_push (GRUB_AS_LIST_P (&drivers),
  85. GRUB_AS_LIST (driver));
  86. for (dev = devs; dev; dev = dev->next)
  87. if (!dev->driver && grub_fdtbus_is_compatible (driver->compatible, dev))
  88. {
  89. grub_dprintf ("fdtbus", "Attaching %s (%p)\n", driver->compatible, dev);
  90. if (driver->attach (dev) == GRUB_ERR_NONE)
  91. {
  92. grub_dprintf ("fdtbus", "Attached %s\n", driver->compatible);
  93. dev->driver = driver;
  94. }
  95. grub_print_error ();
  96. }
  97. }
  98. void
  99. grub_fdtbus_unregister (struct grub_fdtbus_driver *driver)
  100. {
  101. grub_list_remove (GRUB_AS_LIST (driver));
  102. struct grub_fdtbus_dev *dev;
  103. for (dev = devs; dev; dev = dev->next)
  104. if (dev->driver == driver)
  105. {
  106. if (driver->detach)
  107. driver->detach(dev);
  108. dev->driver = 0;
  109. }
  110. }
  111. void
  112. grub_fdtbus_init (const void *dtb_in, grub_size_t size)
  113. {
  114. if (!dtb_in || grub_fdt_check_header (dtb_in, size) < 0)
  115. grub_fatal ("invalid FDT");
  116. dtb = dtb_in;
  117. const grub_uint32_t *prop = grub_fdt_get_prop (dtb, 0, "#address-cells", 0);
  118. if (prop)
  119. root_address_cells = grub_be_to_cpu32 (*prop);
  120. else
  121. root_address_cells = 1;
  122. prop = grub_fdt_get_prop (dtb, 0, "#size-cells", 0);
  123. if (prop)
  124. root_size_cells = grub_be_to_cpu32 (*prop);
  125. else
  126. root_size_cells = 1;
  127. fdtbus_scan (0);
  128. }
  129. static int
  130. get_address_cells (const struct grub_fdtbus_dev *dev)
  131. {
  132. const grub_uint32_t *prop;
  133. if (!dev)
  134. return root_address_cells;
  135. prop = grub_fdt_get_prop (dtb, dev->node, "#address-cells", 0);
  136. if (prop)
  137. return grub_be_to_cpu32 (*prop);
  138. return 1;
  139. }
  140. static int
  141. get_size_cells (const struct grub_fdtbus_dev *dev)
  142. {
  143. const grub_uint32_t *prop;
  144. if (!dev)
  145. return root_size_cells;
  146. prop = grub_fdt_get_prop (dtb, dev->node, "#size-cells", 0);
  147. if (prop)
  148. return grub_be_to_cpu32 (*prop);
  149. return 1;
  150. }
  151. static grub_uint64_t
  152. get64 (const grub_uint32_t *reg, grub_size_t cells)
  153. {
  154. grub_uint64_t val = 0;
  155. if (cells >= 1)
  156. val = grub_be_to_cpu32 (reg[cells - 1]);
  157. if (cells >= 2)
  158. val |= ((grub_uint64_t) grub_be_to_cpu32 (reg[cells - 2])) << 32;
  159. return val;
  160. }
  161. static volatile void *
  162. translate (const struct grub_fdtbus_dev *dev, const grub_uint32_t *reg)
  163. {
  164. volatile void *ret;
  165. const grub_uint32_t *ranges;
  166. grub_size_t ranges_size, cells_per_mapping;
  167. grub_size_t parent_address_cells, child_address_cells, child_size_cells;
  168. grub_size_t nmappings, i;
  169. if (dev == 0)
  170. {
  171. grub_uint64_t val;
  172. val = get64 (reg, root_address_cells);
  173. if (sizeof (void *) == 4 && (val >> 32))
  174. return grub_fdtbus_invalid_mapping;
  175. return (void *) (grub_addr_t) val;
  176. }
  177. ranges = grub_fdt_get_prop (dtb, dev->node, "ranges", &ranges_size);
  178. if (!ranges)
  179. return grub_fdtbus_invalid_mapping;
  180. if (ranges_size == 0)
  181. return translate (dev->parent, reg);
  182. parent_address_cells = get_address_cells (dev->parent);
  183. child_address_cells = get_address_cells (dev);
  184. child_size_cells = get_size_cells (dev);
  185. cells_per_mapping = parent_address_cells + child_address_cells + child_size_cells;
  186. nmappings = ranges_size / 4 / cells_per_mapping;
  187. for (i = 0; i < nmappings; i++)
  188. {
  189. const grub_uint32_t *child_addr = &ranges[i * cells_per_mapping];
  190. const grub_uint32_t *parent_addr = child_addr + child_address_cells;
  191. grub_uint64_t child_size = get64 (parent_addr + parent_address_cells, child_size_cells);
  192. if (child_address_cells > 2 && grub_memcmp (reg, child_addr, (child_address_cells - 2) * 4) != 0)
  193. continue;
  194. if (get64 (reg, child_address_cells) < get64 (child_addr, child_address_cells))
  195. continue;
  196. grub_uint64_t offset = get64 (reg, child_address_cells) - get64 (child_addr, child_address_cells);
  197. if (offset >= child_size)
  198. continue;
  199. ret = translate (dev->parent, parent_addr);
  200. if (grub_fdtbus_is_mapping_valid (ret))
  201. ret = (volatile char *) ret + offset;
  202. return ret;
  203. }
  204. return grub_fdtbus_invalid_mapping;
  205. }
  206. volatile void *
  207. grub_fdtbus_map_reg (const struct grub_fdtbus_dev *dev, int regno, grub_size_t *size)
  208. {
  209. grub_size_t address_cells, size_cells;
  210. address_cells = get_address_cells (dev->parent);
  211. size_cells = get_size_cells (dev->parent);
  212. const grub_uint32_t *reg = grub_fdt_get_prop (dtb, dev->node, "reg", 0);
  213. if (size && size_cells)
  214. *size = reg[(address_cells + size_cells) * regno + address_cells];
  215. if (size && !size_cells)
  216. *size = 0;
  217. return translate (dev->parent, reg + (address_cells + size_cells) * regno);
  218. }
  219. const char *
  220. grub_fdtbus_get_name (const struct grub_fdtbus_dev *dev)
  221. {
  222. return grub_fdt_get_nodename (dtb, dev->node);
  223. }
  224. const void *
  225. grub_fdtbus_get_prop (const struct grub_fdtbus_dev *dev,
  226. const char *name,
  227. grub_uint32_t *len)
  228. {
  229. return grub_fdt_get_prop (dtb, dev->node, name, len);
  230. }
  231. const void *
  232. grub_fdtbus_get_fdt (void)
  233. {
  234. return dtb;
  235. }