]> git.proxmox.com Git - grub2.git/blob - grub-core/bus/bonito.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / bus / bonito.c
1 /* bonito.c - PCI bonito interface. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <grub/pci.h>
21 #include <grub/misc.h>
22
23 static grub_uint32_t base_win[GRUB_MACHINE_PCI_NUM_WIN];
24 static const grub_size_t sizes_win[GRUB_MACHINE_PCI_NUM_WIN] =
25 {GRUB_MACHINE_PCI_WIN1_SIZE, GRUB_MACHINE_PCI_WIN_SIZE,
26 GRUB_MACHINE_PCI_WIN_SIZE};
27 /* Usage counters. */
28 static int usage_win[GRUB_MACHINE_PCI_NUM_WIN];
29 static grub_addr_t addr_win[GRUB_MACHINE_PCI_NUM_WIN] =
30 {GRUB_MACHINE_PCI_WIN1_ADDR, GRUB_MACHINE_PCI_WIN2_ADDR,
31 GRUB_MACHINE_PCI_WIN3_ADDR};
32
33 grub_bonito_type_t grub_bonito_type;
34
35 static volatile void *
36 config_addr (grub_pci_address_t addr)
37 {
38 if (grub_bonito_type == GRUB_BONITO_2F)
39 {
40 GRUB_MACHINE_PCI_CONF_CTRL_REG_2F = 1 << ((addr >> 11) & 0xf);
41 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_2F
42 | (addr & 0x07ff));
43 }
44 else
45 {
46
47 if (addr >> 16)
48 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A_EXT | addr);
49 else
50 return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A | addr);
51 }
52 }
53
54 grub_uint32_t
55 grub_pci_read (grub_pci_address_t addr)
56 {
57 return *(volatile grub_uint32_t *) config_addr (addr);
58 }
59
60 grub_uint16_t
61 grub_pci_read_word (grub_pci_address_t addr)
62 {
63 return *(volatile grub_uint16_t *) config_addr (addr);
64 }
65
66 grub_uint8_t
67 grub_pci_read_byte (grub_pci_address_t addr)
68 {
69 return *(volatile grub_uint8_t *) config_addr (addr);
70 }
71
72 void
73 grub_pci_write (grub_pci_address_t addr, grub_uint32_t data)
74 {
75 *(volatile grub_uint32_t *) config_addr (addr) = data;
76 }
77
78 void
79 grub_pci_write_word (grub_pci_address_t addr, grub_uint16_t data)
80 {
81 *(volatile grub_uint16_t *) config_addr (addr) = data;
82 }
83
84 void
85 grub_pci_write_byte (grub_pci_address_t addr, grub_uint8_t data)
86 {
87 *(volatile grub_uint8_t *) config_addr (addr) = data;
88 }
89
90
91 static inline void
92 write_bases_2f (void)
93 {
94 int i;
95 grub_uint32_t reg = 0;
96 for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
97 reg |= (((base_win[i] >> GRUB_MACHINE_PCI_WIN_SHIFT)
98 & GRUB_MACHINE_PCI_WIN_MASK)
99 << (i * GRUB_MACHINE_PCI_WIN_MASK_SIZE));
100 GRUB_MACHINE_PCI_IO_CTRL_REG_2F = reg;
101 }
102
103 volatile void *
104 grub_pci_device_map_range (grub_pci_device_t dev __attribute__ ((unused)),
105 grub_addr_t base, grub_size_t size)
106 {
107 if (grub_bonito_type == GRUB_BONITO_2F)
108 {
109 int i;
110 grub_addr_t newbase;
111
112 /* First try already used registers. */
113 for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
114 if (usage_win[i] && base_win[i] <= base
115 && base_win[i] + sizes_win[i] > base + size)
116 {
117 usage_win[i]++;
118 return (void *)
119 (addr_win[i] | (base & GRUB_MACHINE_PCI_WIN_OFFSET_MASK));
120 }
121 /* Map new register. */
122 newbase = base & ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK;
123 for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
124 if (!usage_win[i] && newbase <= base
125 && newbase + sizes_win[i] > base + size)
126 {
127 usage_win[i]++;
128 base_win[i] = newbase;
129 write_bases_2f ();
130 return (void *)
131 (addr_win[i] | (base & GRUB_MACHINE_PCI_WIN_OFFSET_MASK));
132 }
133 grub_fatal ("Out of PCI windows.");
134 }
135 else
136 {
137 int region = 0;
138 if (base >= 0x10000000
139 && base + size <= 0x18000000)
140 region = 1;
141 if (base >= 0x1c000000
142 && base + size <= 0x1f000000)
143 region = 2;
144 if (region == 0)
145 grub_fatal ("Attempt to map out of regions");
146 return (void *) (0xa0000000 | base);
147 }
148 }
149
150 void *
151 grub_pci_device_map_range_cached (grub_pci_device_t dev,
152 grub_addr_t base, grub_size_t size)
153 {
154 return (void *) (((grub_addr_t) grub_pci_device_map_range (dev, base, size))
155 & ~0x20000000);
156 }
157
158 void
159 grub_pci_device_unmap_range (grub_pci_device_t dev __attribute__ ((unused)),
160 volatile void *mem,
161 grub_size_t size __attribute__ ((unused)))
162 {
163 if (grub_bonito_type == GRUB_BONITO_2F)
164 {
165 int i;
166 for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
167 if (usage_win[i] && addr_win[i]
168 == (((grub_addr_t) mem | 0x20000000)
169 & ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK))
170 {
171 usage_win[i]--;
172 return;
173 }
174 grub_fatal ("Tried to unmap not mapped region");
175 }
176 }