]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/pci.c
core: Introduce generic PCI config space access functions
[jailhouse.git] / hypervisor / pci.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2014
5  *
6  * Authors:
7  *  Ivan Kolchin <ivan.kolchin@siemens.com>
8  *  Jan Kiszka <jan.kiszka@siemens.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  */
13
14 #include <jailhouse/acpi.h>
15 #include <jailhouse/mmio.h>
16 #include <jailhouse/pci.h>
17 #include <jailhouse/printk.h>
18 #include <jailhouse/utils.h>
19
20 #define PCI_CONFIG_HEADER_SIZE          0x40
21
22 struct acpi_mcfg_alloc {
23         u64 base_addr;
24         u16 segment_num;
25         u8 start_bus;
26         u8 end_bus;
27         u32 reserved;
28 } __attribute__((packed));
29
30 struct acpi_mcfg_table {
31         struct acpi_table_header header;
32         u8 reserved[8];
33         struct acpi_mcfg_alloc alloc_structs[];
34 } __attribute__((packed));
35
36 /* entry for PCI config space whitelist (granting access) */
37 struct pci_cfg_access {
38         u32 reg_num; /** Register number (4-byte aligned) */
39         u32 mask; /** Bit set: access allowed */
40 };
41
42 /* --- Whilelist for writing to PCI config space registers --- */
43 /* Type 1: Endpoints */
44 static const struct pci_cfg_access endpoint_write_access[] = {
45         { 0x04, 0xffffffff }, /* Command, Status */
46         { 0x0c, 0xff00ffff }, /* BIST, Latency Timer, Cacheline */
47         { 0x3c, 0x000000ff }, /* Int Line */
48 };
49 /* Type 2: Bridges */
50 static const struct pci_cfg_access bridge_write_access[] = {
51         { 0x04, 0xffffffff }, /* Command, Status */
52         { 0x0c, 0xff00ffff }, /* BIST, Latency Timer, Cacheline */
53         { 0x3c, 0xffff00ff }, /* Int Line, Bridge Control */
54 };
55
56 static void *pci_space;
57 static u64 pci_mmcfg_addr;
58 static u32 pci_mmcfg_size;
59 static u8 end_bus;
60
61 static void *pci_get_device_mmcfg_base(u16 bdf)
62 {
63         return pci_space + ((unsigned long)bdf << 12);
64 }
65
66 /**
67  * pci_read_config() - Read from PCI config space
68  * @bdf:        16-bit bus/device/function ID of target
69  * @address:    Config space access address
70  * @size:       Access size (1, 2 or 4 bytes)
71  *
72  * Return: read value
73  */
74 u32 pci_read_config(u16 bdf, u16 address, unsigned int size)
75 {
76         void *mmcfg_addr = pci_get_device_mmcfg_base(bdf) + address;
77
78         if (!pci_space || PCI_BUS(bdf) > end_bus)
79                 return arch_pci_read_config(bdf, address, size);
80
81         if (size == 1)
82                 return mmio_read8(mmcfg_addr);
83         else if (size == 2)
84                 return mmio_read16(mmcfg_addr);
85         else
86                 return mmio_read32(mmcfg_addr);
87 }
88
89 /**
90  * pci_write_config() - Write to PCI config space
91  * @bdf:        16-bit bus/device/function ID of target
92  * @address:    Config space access address
93  * @value:      Value to be written
94  * @size:       Access size (1, 2 or 4 bytes)
95  */
96 void pci_write_config(u16 bdf, u16 address, u32 value, unsigned int size)
97 {
98         void *mmcfg_addr = pci_get_device_mmcfg_base(bdf) + address;
99
100         if (!pci_space || PCI_BUS(bdf) > end_bus)
101                 return arch_pci_write_config(bdf, address, value, size);
102
103         if (size == 1)
104                 mmio_write8(mmcfg_addr, value);
105         else if (size == 2)
106                 mmio_write16(mmcfg_addr, value);
107         else
108                 mmio_write32(mmcfg_addr, value);
109 }
110
111 /**
112  * pci_get_assigned_device() - Look up device owned by a cell
113  * @cell:       Owning cell
114  * @bdf:        16-bit bus/device/function ID
115  *
116  * Return: Valid pointer - owns, NULL - doesn't own.
117  */
118 const struct jailhouse_pci_device *
119 pci_get_assigned_device(const struct cell *cell, u16 bdf)
120 {
121         const struct jailhouse_pci_device *device =
122                 jailhouse_cell_pci_devices(cell->config);
123         u32 n;
124
125         for (n = 0; n < cell->config->num_pci_devices; n++)
126                 if (device[n].bdf == bdf)
127                         return &device[n];
128
129         return NULL;
130 }
131
132 /**
133  * pci_find_capability() - Look up capability at given config space address
134  * @cell:       Device owning cell
135  * @device:     The device to be accessed
136  * @address:    Config space access address
137  *
138  * Return: Corresponding capability structure or NULL if none found.
139  */
140 static const struct jailhouse_pci_capability *
141 pci_find_capability(const struct cell *cell,
142                     const struct jailhouse_pci_device *device, u16 address)
143 {
144         const struct jailhouse_pci_capability *cap =
145                 jailhouse_cell_pci_caps(cell->config) + device->caps_start;
146         u32 n;
147
148         for (n = 0; n < device->num_caps; n++, cap++)
149                 if (cap->start <= address && cap->start + cap->len > address)
150                         return cap;
151
152         return NULL;
153 }
154
155 /**
156  * pci_cfg_read_moderate() - Moderate config space read access
157  * @cell:       Request issuing cell
158  * @device:     The device to be accessed; if NULL, access will be emulated,
159  *              returning a value of -1
160  * @address:    Config space address
161  * @size:       Access size (1, 2 or 4 bytes)
162  * @value:      Pointer to buffer to receive the emulated value if
163  *              PCI_ACCESS_EMULATE is returned
164  *
165  * Return: PCI_ACCESS_PERFORM or PCI_ACCESS_EMULATE.
166  */
167 enum pci_access
168 pci_cfg_read_moderate(const struct cell *cell,
169                       const struct jailhouse_pci_device *device, u16 address,
170                       unsigned int size, u32 *value)
171 {
172         const struct jailhouse_pci_capability *cap;
173
174         if (!device) {
175                 *value = -1;
176                 return PCI_ACCESS_EMULATE;
177         }
178
179         if (address < PCI_CONFIG_HEADER_SIZE)
180                 return PCI_ACCESS_PERFORM;
181
182         cap = pci_find_capability(cell, device, address);
183         if (!cap)
184                 return PCI_ACCESS_PERFORM;
185
186         // TODO: Emulate MSI/MSI-X etc.
187
188         return PCI_ACCESS_PERFORM;
189 }
190
191 /**
192  * pci_cfg_write_moderate() - Moderate config space write access
193  * @cell:       Request issuing cell
194  * @device:     The device to be accessed; if NULL, access will be rejected
195  * @address:    Config space address
196  * @size:       Access size (1, 2 or 4 bytes)
197  * @value:      Pointer to value to be written, initialized with cell value,
198  *              set to the to-be-written hardware value if PCI_ACCESS_EMULATE
199  *              is returned
200  *
201  * Return: PCI_ACCESS_REJECT, PCI_ACCESS_PERFORM or PCI_ACCESS_EMULATE.
202  */
203 enum pci_access
204 pci_cfg_write_moderate(const struct cell *cell,
205                        const struct jailhouse_pci_device *device, u16 address,
206                        unsigned int size, u32 *value)
207 {
208         const struct jailhouse_pci_capability *cap;
209         /* initialize list to work around wrong compiler warning */
210         const struct pci_cfg_access *list = NULL;
211         unsigned int n, bias_shift, len = 0;
212         u32 mask;
213
214         if (!device)
215                 return PCI_ACCESS_REJECT;
216
217         if (address < PCI_CONFIG_HEADER_SIZE) {
218                 if (device->type == JAILHOUSE_PCI_TYPE_DEVICE) {
219                         list = endpoint_write_access;
220                         len = ARRAY_SIZE(endpoint_write_access);
221                 } else if (device->type == JAILHOUSE_PCI_TYPE_BRIDGE) {
222                         list = bridge_write_access;
223                         len = ARRAY_SIZE(bridge_write_access);
224                 }
225
226                 bias_shift = (address & 0x003) * 8;
227                 mask = BYTE_MASK(size);
228
229                 for (n = 0; n < len; n++) {
230                         if (list[n].reg_num == (address & 0xffc) &&
231                             ((list[n].mask >> bias_shift) & mask) == mask)
232                                 return PCI_ACCESS_PERFORM;
233                 }
234
235                 return PCI_ACCESS_REJECT;
236         }
237
238         cap = pci_find_capability(cell, device, address);
239         if (!cap || !(cap->flags & JAILHOUSE_PCICAPS_WRITE))
240                 return PCI_ACCESS_REJECT;
241
242         return PCI_ACCESS_PERFORM;
243 }
244
245 /**
246  * pci_init() - Initialization of PCI module
247  *
248  * Return: 0 - success, error code - if error.
249  */
250 int pci_init(void)
251 {
252         struct acpi_mcfg_table *mcfg;
253
254         mcfg = (struct acpi_mcfg_table *)acpi_find_table("MCFG", NULL);
255         if (!mcfg)
256                 return 0;
257
258         if (mcfg->header.length !=
259             sizeof(struct acpi_mcfg_table) + sizeof(struct acpi_mcfg_alloc))
260                 return -EIO;
261
262         pci_mmcfg_addr = mcfg->alloc_structs[0].base_addr;
263         pci_mmcfg_size = (mcfg->alloc_structs[0].end_bus + 1) * 256 * 4096;
264         pci_space = page_alloc(&remap_pool, pci_mmcfg_size / PAGE_SIZE);
265         if (!pci_space)
266                 return -ENOMEM;
267
268         end_bus = mcfg->alloc_structs[0].end_bus;
269
270         return page_map_create(&hv_paging_structs,
271                                mcfg->alloc_structs[0].base_addr,
272                                pci_mmcfg_size, (unsigned long)pci_space,
273                                PAGE_DEFAULT_FLAGS | PAGE_FLAG_UNCACHED,
274                                PAGE_MAP_NON_COHERENT);
275 }
276
277 /**
278  * pci_mmio_access_handler() - Handler for MMIO-accesses to PCI config space
279  * @cell:       Request issuing cell
280  * @is_write:   True if write access
281  * @addr:       Address accessed
282  * @value:      Pointer to value for reading/writing
283  *
284  * Return: 1 if handled successfully, 0 if unhandled, -1 on access error
285  */
286 int pci_mmio_access_handler(const struct cell *cell, bool is_write,
287                             u64 addr, u32 *value)
288 {
289         u32 mmcfg_offset, val, reg_addr;
290         const struct jailhouse_pci_device *device;
291         enum pci_access access;
292
293         if (!pci_space || addr < pci_mmcfg_addr ||
294             addr >= (pci_mmcfg_addr + pci_mmcfg_size - 4))
295                 return 0;
296
297         mmcfg_offset = addr - pci_mmcfg_addr;
298         reg_addr = mmcfg_offset & 0xfff;
299         device = pci_get_assigned_device(cell, mmcfg_offset >> 12);
300
301         if (is_write) {
302                 val = *value;
303                 access = pci_cfg_write_moderate(cell, device, reg_addr, 4,
304                                                 &val);
305                 if (access == PCI_ACCESS_REJECT)
306                         goto invalid_access;
307                 mmio_write32(pci_space + mmcfg_offset, val);
308         } else {
309                 access = pci_cfg_read_moderate(cell, device, reg_addr, 4,
310                                                value);
311                 if (access == PCI_ACCESS_PERFORM)
312                         *value = mmio_read32(pci_space + mmcfg_offset);
313         }
314
315         return 1;
316
317 invalid_access:
318         panic_printk("FATAL: Invalid PCI MMCONFIG write, device %02x:%02x.%x, "
319                      "reg: %\n", PCI_BDF_PARAMS(mmcfg_offset >> 12), reg_addr);
320         return -1;
321
322 }