summaryrefslogtreecommitdiffstats
path: root/grub-core/bus/pci.c
blob: b388ce5c8384ebe9dd2ae04e28a747f7984e4613 (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
/* pci.c - Generic PCI interfaces.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2007,2009  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/dl.h>
#include <grub/pci.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/mm_private.h>
#include <grub/cache.h>

GRUB_MOD_LICENSE ("GPLv3+");

/* FIXME: correctly support 64-bit architectures.  */
/* #if GRUB_TARGET_SIZEOF_VOID_P == 4 */
struct grub_pci_dma_chunk *
grub_memalign_dma32 (grub_size_t align, grub_size_t size)
{
  void *ret;
  if (align < 64)
    align = 64;
  size = ALIGN_UP (size, align);
  ret = grub_memalign (align, size);
#if GRUB_CPU_SIZEOF_VOID_P == 8
  if ((grub_addr_t) ret >> 32)
    {
      /* Shouldn't happend since the only platform in this case is
	 x86_64-efi and it skips any regions > 4GiB because
	 of EFI bugs anyway.  */
      grub_error (GRUB_ERR_BUG, "allocation outside 32-bit range");
      return 0;
    }
#endif
  if (!ret)
    return 0;
  grub_arch_sync_dma_caches (ret, size);
  return ret;
}

/* FIXME: evil.  */
void
grub_dma_free (struct grub_pci_dma_chunk *ch)
{
  grub_size_t size = (((struct grub_mm_header *) ch) - 1)->size * GRUB_MM_ALIGN;
  grub_arch_sync_dma_caches (ch, size);
  grub_free (ch);
}
/* #endif */

#ifdef GRUB_MACHINE_MIPS_LOONGSON
volatile void *
grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
{
  return (void *) ((((grub_uint32_t) ch) & 0x1fffffff) | 0xa0000000);
}

grub_uint32_t
grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
{
  return (((grub_uint32_t) ch) & 0x1fffffff) | 0x80000000;
}
#else

volatile void *
grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
{
  return (void *) ch;
}

grub_uint32_t
grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
{
  return (grub_uint32_t) (grub_addr_t) ch;
}

#endif

grub_pci_address_t
grub_pci_make_address (grub_pci_device_t dev, int reg)
{
  return (1 << 31) | (dev.bus << 16) | (dev.device << 11)
    | (dev.function << 8) | reg;
}

void
grub_pci_iterate (grub_pci_iteratefunc_t hook, void *hook_data)
{
  grub_pci_device_t dev;
  grub_pci_address_t addr;
  grub_pci_id_t id;
  grub_uint32_t hdr;

  for (dev.bus = 0; dev.bus < GRUB_PCI_NUM_BUS; dev.bus++)
    {
      for (dev.device = 0; dev.device < GRUB_PCI_NUM_DEVICES; dev.device++)
	{
	  for (dev.function = 0; dev.function < 8; dev.function++)
	    {
	      addr = grub_pci_make_address (dev, GRUB_PCI_REG_PCI_ID);
	      id = grub_pci_read (addr);

	      /* Check if there is a device present.  */
	      if (id >> 16 == 0xFFFF)
		{
		  if (dev.function == 0)
		    /* Devices are required to implement function 0, so if
		       it's missing then there is no device here.  */
		    break;
		  else
		    continue;
		}

	      if (hook (dev, id, hook_data))
		return;

	      /* Probe only func = 0 if the device if not multifunction */
	      if (dev.function == 0)
		{
		  addr = grub_pci_make_address (dev, GRUB_PCI_REG_CACHELINE);
		  hdr = grub_pci_read (addr);
		  if (!(hdr & 0x800000))
		    break;
		}
	    }
	}
    }
}

grub_uint8_t
grub_pci_find_capability (grub_pci_device_t dev, grub_uint8_t cap)
{
  grub_uint8_t pos = 0x34;
  int ttl = 48;

  while (ttl--)
    {
      grub_uint8_t id;
      grub_pci_address_t addr;

      addr = grub_pci_make_address (dev, pos);
      pos = grub_pci_read_byte (addr);
      if (pos < 0x40)
	break;

      pos &= ~3;

      addr = grub_pci_make_address (dev, pos);      
      id = grub_pci_read_byte (addr);

      if (id == 0xff)
	break;
      
      if (id == cap)
	return pos;
      pos++;
    }
  return 0;
}