init.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013 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/kernel.h>
  19. #include <grub/mm.h>
  20. #include <grub/machine/time.h>
  21. #include <grub/machine/memory.h>
  22. #include <grub/machine/console.h>
  23. #include <grub/offsets.h>
  24. #include <grub/types.h>
  25. #include <grub/err.h>
  26. #include <grub/dl.h>
  27. #include <grub/misc.h>
  28. #include <grub/loader.h>
  29. #include <grub/env.h>
  30. #include <grub/cache.h>
  31. #include <grub/time.h>
  32. #include <grub/symbol.h>
  33. #include <grub/cpu/io.h>
  34. #include <grub/cpu/floppy.h>
  35. #include <grub/cpu/tsc.h>
  36. #include <grub/machine/kernel.h>
  37. #include <grub/pci.h>
  38. extern grub_uint8_t _start[];
  39. extern grub_uint8_t _end[];
  40. extern grub_uint8_t _edata[];
  41. void __attribute__ ((noreturn))
  42. grub_exit (void)
  43. {
  44. /* We can't use grub_fatal() in this function. This would create an infinite
  45. loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
  46. while (1)
  47. grub_cpu_idle ();
  48. }
  49. grub_addr_t grub_modbase;
  50. /* Helper for grub_machine_init. */
  51. static int
  52. heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
  53. void *data __attribute__ ((unused)))
  54. {
  55. grub_uint64_t begin = addr, end = addr + size;
  56. #if GRUB_CPU_SIZEOF_VOID_P == 4
  57. /* Restrict ourselves to 32-bit memory space. */
  58. if (begin > GRUB_ULONG_MAX)
  59. return 0;
  60. if (end > GRUB_ULONG_MAX)
  61. end = GRUB_ULONG_MAX;
  62. #endif
  63. if (type != GRUB_MEMORY_AVAILABLE)
  64. return 0;
  65. /* Avoid the lower memory. */
  66. if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
  67. begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;
  68. if (end <= begin)
  69. return 0;
  70. grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
  71. return 0;
  72. }
  73. struct resource
  74. {
  75. grub_pci_device_t dev;
  76. grub_size_t size;
  77. unsigned type:4;
  78. unsigned bar:3;
  79. };
  80. struct iterator_ctx
  81. {
  82. struct resource *resources;
  83. grub_size_t nresources;
  84. };
  85. /* We don't support bridges, so can't have more than 32 devices. */
  86. #define MAX_DEVICES 32
  87. static int
  88. find_resources (grub_pci_device_t dev,
  89. grub_pci_id_t pciid __attribute__ ((unused)),
  90. void *data)
  91. {
  92. struct iterator_ctx *ctx = data;
  93. int bar;
  94. if (ctx->nresources >= MAX_DEVICES * 6)
  95. return 1;
  96. for (bar = 0; bar < 6; bar++)
  97. {
  98. grub_pci_address_t addr;
  99. grub_uint32_t ones, zeros, mask;
  100. struct resource *res;
  101. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
  102. + 4 * bar);
  103. grub_pci_write (addr, 0xffffffff);
  104. grub_pci_read (addr);
  105. ones = grub_pci_read (addr);
  106. grub_pci_write (addr, 0);
  107. grub_pci_read (addr);
  108. zeros = grub_pci_read (addr);
  109. if (ones == zeros)
  110. continue;
  111. res = &ctx->resources[ctx->nresources++];
  112. if ((zeros & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
  113. mask = GRUB_PCI_ADDR_SPACE_MASK;
  114. else
  115. mask = (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK | GRUB_PCI_ADDR_MEM_PREFETCH);
  116. res->type = ones & mask;
  117. res->dev = dev;
  118. res->bar = bar;
  119. res->size = (~((zeros ^ ones)) | mask) + 1;
  120. if ((zeros & (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK))
  121. == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
  122. bar++;
  123. }
  124. return 0;
  125. }
  126. static int
  127. enable_cards (grub_pci_device_t dev,
  128. grub_pci_id_t pciid __attribute__ ((unused)),
  129. void *data __attribute__ ((unused)))
  130. {
  131. grub_uint16_t cmd = 0;
  132. grub_pci_address_t addr;
  133. grub_uint32_t class;
  134. int bar;
  135. for (bar = 0; bar < 6; bar++)
  136. {
  137. grub_uint32_t val;
  138. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
  139. + 4 * bar);
  140. val = grub_pci_read (addr);
  141. if (!val)
  142. continue;
  143. if ((val & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
  144. cmd |= GRUB_PCI_COMMAND_IO_ENABLED;
  145. else
  146. cmd |= GRUB_PCI_COMMAND_MEM_ENABLED;
  147. }
  148. class = (grub_pci_read (addr) >> 16) & 0xffff;
  149. if (class == GRUB_PCI_CLASS_DISPLAY_VGA)
  150. cmd |= GRUB_PCI_COMMAND_IO_ENABLED
  151. | GRUB_PCI_COMMAND_MEM_ENABLED;
  152. if (class == GRUB_PCI_CLASS_SERIAL_USB)
  153. return 0;
  154. addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  155. grub_pci_write (addr, cmd);
  156. return 0;
  157. }
  158. static void
  159. grub_pci_assign_addresses (void)
  160. {
  161. struct iterator_ctx ctx;
  162. {
  163. struct resource resources[MAX_DEVICES * 6];
  164. int done;
  165. unsigned i;
  166. ctx.nresources = 0;
  167. ctx.resources = resources;
  168. grub_uint32_t memptr = 0xf0000000;
  169. grub_uint16_t ioptr = 0x1000;
  170. grub_pci_iterate (find_resources, &ctx);
  171. /* FIXME: do we need a better sort here? */
  172. do
  173. {
  174. done = 0;
  175. for (i = 0; i + 1 < ctx.nresources; i++)
  176. if (resources[i].size < resources[i+1].size)
  177. {
  178. struct resource t;
  179. t = resources[i];
  180. resources[i] = resources[i+1];
  181. resources[i+1] = t;
  182. done = 1;
  183. }
  184. }
  185. while (done);
  186. for (i = 0; i < ctx.nresources; i++)
  187. {
  188. grub_pci_address_t addr;
  189. addr = grub_pci_make_address (resources[i].dev,
  190. GRUB_PCI_REG_ADDRESS_REG0
  191. + 4 * resources[i].bar);
  192. if ((resources[i].type & GRUB_PCI_ADDR_SPACE_MASK)
  193. == GRUB_PCI_ADDR_SPACE_IO)
  194. {
  195. grub_pci_write (addr, ioptr | resources[i].type);
  196. ioptr += resources[i].size;
  197. }
  198. else
  199. {
  200. grub_pci_write (addr, memptr | resources[i].type);
  201. memptr += resources[i].size;
  202. if ((resources[i].type & (GRUB_PCI_ADDR_MEM_TYPE_MASK
  203. | GRUB_PCI_ADDR_SPACE_MASK))
  204. == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
  205. {
  206. addr = grub_pci_make_address (resources[i].dev,
  207. GRUB_PCI_REG_ADDRESS_REG0
  208. + 4 * resources[i].bar + 4);
  209. grub_pci_write (addr, 0);
  210. }
  211. }
  212. }
  213. grub_pci_iterate (enable_cards, NULL);
  214. }
  215. }
  216. void
  217. grub_machine_init (void)
  218. {
  219. grub_modbase = grub_core_entry_addr + (_edata - _start);
  220. grub_pci_assign_addresses ();
  221. grub_qemu_init_cirrus ();
  222. grub_vga_text_init ();
  223. grub_machine_mmap_init ();
  224. grub_machine_mmap_iterate (heap_init, NULL);
  225. grub_tsc_init ();
  226. }
  227. void
  228. grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
  229. char **path __attribute__ ((unused)))
  230. {
  231. }
  232. void
  233. grub_machine_fini (int flags)
  234. {
  235. if (flags & GRUB_LOADER_FLAG_NORETURN)
  236. grub_vga_text_fini ();
  237. grub_stop_floppy ();
  238. }