summaryrefslogtreecommitdiffstats
path: root/grub-core/kern/i386/qemu/init.c
blob: 271b6fbfabdb0356981971df94a472df850e615e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/kernel.h>
#include <grub/mm.h>
#include <grub/machine/time.h>
#include <grub/machine/memory.h>
#include <grub/machine/console.h>
#include <grub/offsets.h>
#include <grub/types.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/loader.h>
#include <grub/env.h>
#include <grub/cache.h>
#include <grub/time.h>
#include <grub/symbol.h>
#include <grub/cpu/io.h>
#include <grub/cpu/floppy.h>
#include <grub/cpu/tsc.h>
#include <grub/machine/kernel.h>
#include <grub/pci.h>

extern grub_uint8_t _start[];
extern grub_uint8_t _end[];
extern grub_uint8_t _edata[];

void  __attribute__ ((noreturn))
grub_exit (void)
{
  /* We can't use grub_fatal() in this function.  This would create an infinite
     loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit().  */
  while (1)
    grub_cpu_idle ();
}

grub_addr_t grub_modbase;

/* Helper for grub_machine_init.  */
static int
heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
	   void *data __attribute__ ((unused)))
{
  grub_uint64_t begin = addr, end = addr + size;

#if GRUB_CPU_SIZEOF_VOID_P == 4
  /* Restrict ourselves to 32-bit memory space.  */
  if (begin > GRUB_ULONG_MAX)
    return 0;
  if (end > GRUB_ULONG_MAX)
    end = GRUB_ULONG_MAX;
#endif

  if (type != GRUB_MEMORY_AVAILABLE)
    return 0;

  /* Avoid the lower memory.  */
  if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
    begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;

  if (end <= begin)
    return 0;

  grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));

  return 0;
}

struct resource
{
  grub_pci_device_t dev;
  grub_size_t size;
  unsigned type:4;
  unsigned bar:3;
};

struct iterator_ctx
{
  struct resource *resources;
  grub_size_t nresources;
};

/* We don't support bridges, so can't have more than 32 devices.  */
#define MAX_DEVICES 32

static int
find_resources (grub_pci_device_t dev,
		grub_pci_id_t pciid __attribute__ ((unused)),
		void *data)
{
  struct iterator_ctx *ctx = data;
  int bar;

  if (ctx->nresources >= MAX_DEVICES * 6)
    return 1;

  for (bar = 0; bar < 6; bar++)
    {
      grub_pci_address_t addr;
      grub_uint32_t ones, zeros, mask;
      struct resource *res;
      addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
				    + 4 * bar);
      grub_pci_write (addr, 0xffffffff);
      grub_pci_read (addr);
      ones = grub_pci_read (addr);
      grub_pci_write (addr, 0);
      grub_pci_read (addr);
      zeros = grub_pci_read (addr);
      if (ones == zeros)
	continue;
      res = &ctx->resources[ctx->nresources++];
      if ((zeros & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
	mask = GRUB_PCI_ADDR_SPACE_MASK;
      else
	mask = (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK | GRUB_PCI_ADDR_MEM_PREFETCH);

      res->type = ones & mask;
      res->dev = dev;
      res->bar = bar;
      res->size = (~((zeros ^ ones)) | mask) + 1;
      if ((zeros & (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK))
	  == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
	bar++;
    }
  return 0;
}

static int
enable_cards (grub_pci_device_t dev,
	      grub_pci_id_t pciid __attribute__ ((unused)),
	      void *data __attribute__ ((unused)))
{
  grub_uint16_t cmd = 0;
  grub_pci_address_t addr;
  grub_uint32_t class;
  int bar;

  for (bar = 0; bar < 6; bar++)
    {
      grub_uint32_t val;
      addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
				    + 4 * bar);
      val = grub_pci_read (addr);
      if (!val)
	continue;
      if ((val & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
	cmd |= GRUB_PCI_COMMAND_IO_ENABLED;
      else
	cmd |= GRUB_PCI_COMMAND_MEM_ENABLED;
    }

  class = (grub_pci_read (addr) >> 16) & 0xffff;

  if (class == GRUB_PCI_CLASS_SUBCLASS_VGA)
    cmd |= GRUB_PCI_COMMAND_IO_ENABLED
      | GRUB_PCI_COMMAND_MEM_ENABLED;

  if (class == GRUB_PCI_CLASS_SUBCLASS_USB)
    return 0;
  
  addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  grub_pci_write (addr, cmd);

  return 0;
}

static void
grub_pci_assign_addresses (void)
{
  struct iterator_ctx ctx;

  {
    struct resource resources[MAX_DEVICES * 6];
    int done;
    unsigned i;
    ctx.nresources = 0;
    ctx.resources = resources;
    grub_uint32_t memptr = 0xf0000000;
    grub_uint16_t ioptr = 0x1000;

    grub_pci_iterate (find_resources, &ctx);
    /* FIXME: do we need a better sort here?  */
    do
      {
	done = 0;
	for (i = 0; i + 1 < ctx.nresources; i++)
	  if (resources[i].size < resources[i+1].size)
	    {
	      struct resource t;
	      t = resources[i];
	      resources[i] = resources[i+1];
	      resources[i+1] = t;
	      done = 1;
	    }
      }
    while (done);

    for (i = 0; i < ctx.nresources; i++)
      {
	grub_pci_address_t addr;
	addr = grub_pci_make_address (resources[i].dev,
				      GRUB_PCI_REG_ADDRESS_REG0
				      + 4 * resources[i].bar);
	if ((resources[i].type & GRUB_PCI_ADDR_SPACE_MASK)
	    == GRUB_PCI_ADDR_SPACE_IO)
	  {
	    grub_pci_write (addr, ioptr | resources[i].type);
	    ioptr += resources[i].size;
	  }
	else
	  {
	    grub_pci_write (addr, memptr | resources[i].type);
	    memptr += resources[i].size;
	    if ((resources[i].type & (GRUB_PCI_ADDR_MEM_TYPE_MASK
				      | GRUB_PCI_ADDR_SPACE_MASK))
		== (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
	      {
		addr = grub_pci_make_address (resources[i].dev,
					      GRUB_PCI_REG_ADDRESS_REG0
					      + 4 * resources[i].bar + 4);
		grub_pci_write (addr, 0);
	      }
	  }	  
      }
    grub_pci_iterate (enable_cards, NULL);
  }
}

void
grub_machine_init (void)
{
  grub_modbase = grub_core_entry_addr + (_edata - _start);

  grub_pci_assign_addresses ();

  grub_qemu_init_cirrus ();

  grub_vga_text_init ();

  grub_machine_mmap_init ();
  grub_machine_mmap_iterate (heap_init, NULL);


  grub_tsc_init ();
}

void
grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
			       char **path __attribute__ ((unused)))
{
}

void
grub_machine_fini (int flags)
{
  if (flags & GRUB_LOADER_FLAG_NORETURN)
    grub_vga_text_fini ();
  grub_stop_floppy ();
}