1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend - Functions for creating a virtual configuration space for
4 * exported PCI Devices.
5 * It's dangerous to allow PCI Driver Domains to change their
6 * device's resources (memory, i/o ports, interrupts). We need to
7 * restrict changes to certain PCI Configuration registers:
8 * BARs, INTERRUPT_PIN, most registers in the header...
10 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
13 #define dev_fmt(fmt) DRV_NAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/moduleparam.h>
17 #include <linux/pci.h>
19 #include "conf_space.h"
20 #include "conf_space_quirks.h"
22 bool xen_pcibk_permissive
;
23 module_param_named(permissive
, xen_pcibk_permissive
, bool, 0644);
25 /* This is where xen_pcibk_read_config_byte, xen_pcibk_read_config_word,
26 * xen_pcibk_write_config_word, and xen_pcibk_write_config_byte are created. */
27 #define DEFINE_PCI_CONFIG(op, size, type) \
28 int xen_pcibk_##op##_config_##size \
29 (struct pci_dev *dev, int offset, type value, void *data) \
31 return pci_##op##_config_##size(dev, offset, value); \
34 DEFINE_PCI_CONFIG(read
, byte
, u8
*)
35 DEFINE_PCI_CONFIG(read
, word
, u16
*)
36 DEFINE_PCI_CONFIG(read
, dword
, u32
*)
38 DEFINE_PCI_CONFIG(write
, byte
, u8
)
39 DEFINE_PCI_CONFIG(write
, word
, u16
)
40 DEFINE_PCI_CONFIG(write
, dword
, u32
)
42 static int conf_space_read(struct pci_dev
*dev
,
43 const struct config_field_entry
*entry
,
44 int offset
, u32
*value
)
47 const struct config_field
*field
= entry
->field
;
51 switch (field
->size
) {
54 ret
= field
->u
.b
.read(dev
, offset
, (u8
*) value
,
59 ret
= field
->u
.w
.read(dev
, offset
, (u16
*) value
,
64 ret
= field
->u
.dw
.read(dev
, offset
, value
, entry
->data
);
70 static int conf_space_write(struct pci_dev
*dev
,
71 const struct config_field_entry
*entry
,
72 int offset
, u32 value
)
75 const struct config_field
*field
= entry
->field
;
77 switch (field
->size
) {
80 ret
= field
->u
.b
.write(dev
, offset
, (u8
) value
,
85 ret
= field
->u
.w
.write(dev
, offset
, (u16
) value
,
89 if (field
->u
.dw
.write
)
90 ret
= field
->u
.dw
.write(dev
, offset
, value
,
97 static inline u32
get_mask(int size
)
107 static inline int valid_request(int offset
, int size
)
109 /* Validate request (no un-aligned requests) */
110 if ((size
== 1 || size
== 2 || size
== 4) && (offset
% size
) == 0)
115 static inline u32
merge_value(u32 val
, u32 new_val
, u32 new_val_mask
,
119 new_val_mask
<<= (offset
* 8);
120 new_val
<<= (offset
* 8);
122 new_val_mask
>>= (offset
* -8);
123 new_val
>>= (offset
* -8);
125 val
= (val
& ~new_val_mask
) | (new_val
& new_val_mask
);
130 static int xen_pcibios_err_to_errno(int err
)
133 case PCIBIOS_SUCCESSFUL
:
134 return XEN_PCI_ERR_success
;
135 case PCIBIOS_DEVICE_NOT_FOUND
:
136 return XEN_PCI_ERR_dev_not_found
;
137 case PCIBIOS_BAD_REGISTER_NUMBER
:
138 return XEN_PCI_ERR_invalid_offset
;
139 case PCIBIOS_FUNC_NOT_SUPPORTED
:
140 return XEN_PCI_ERR_not_implemented
;
141 case PCIBIOS_SET_FAILED
:
142 return XEN_PCI_ERR_access_denied
;
147 int xen_pcibk_config_read(struct pci_dev
*dev
, int offset
, int size
,
151 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
152 const struct config_field_entry
*cfg_entry
;
153 const struct config_field
*field
;
154 int field_start
, field_end
;
155 /* if read fails for any reason, return 0
156 * (as if device didn't respond) */
157 u32 value
= 0, tmp_val
;
159 dev_dbg(&dev
->dev
, "read %d bytes at 0x%x\n", size
, offset
);
161 if (!valid_request(offset
, size
)) {
162 err
= XEN_PCI_ERR_invalid_offset
;
166 /* Get the real value first, then modify as appropriate */
169 err
= pci_read_config_byte(dev
, offset
, (u8
*) &value
);
172 err
= pci_read_config_word(dev
, offset
, (u16
*) &value
);
175 err
= pci_read_config_dword(dev
, offset
, &value
);
179 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
180 field
= cfg_entry
->field
;
182 field_start
= OFFSET(cfg_entry
);
183 field_end
= OFFSET(cfg_entry
) + field
->size
;
185 if (offset
+ size
> field_start
&& field_end
> offset
) {
186 err
= conf_space_read(dev
, cfg_entry
, field_start
,
191 value
= merge_value(value
, tmp_val
,
192 get_mask(field
->size
),
193 field_start
- offset
);
198 dev_dbg(&dev
->dev
, "read %d bytes at 0x%x = %x\n", size
, offset
, value
);
201 return xen_pcibios_err_to_errno(err
);
204 int xen_pcibk_config_write(struct pci_dev
*dev
, int offset
, int size
, u32 value
)
206 int err
= 0, handled
= 0;
207 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
208 const struct config_field_entry
*cfg_entry
;
209 const struct config_field
*field
;
211 int field_start
, field_end
;
213 dev_dbg(&dev
->dev
, "write request %d bytes at 0x%x = %x\n",
214 size
, offset
, value
);
216 if (!valid_request(offset
, size
))
217 return XEN_PCI_ERR_invalid_offset
;
219 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
220 field
= cfg_entry
->field
;
222 field_start
= OFFSET(cfg_entry
);
223 field_end
= OFFSET(cfg_entry
) + field
->size
;
225 if (offset
+ size
> field_start
&& field_end
> offset
) {
226 err
= conf_space_read(dev
, cfg_entry
, field_start
,
231 tmp_val
= merge_value(tmp_val
, value
, get_mask(size
),
232 offset
- field_start
);
234 err
= conf_space_write(dev
, cfg_entry
, field_start
,
237 /* handled is set true here, but not every byte
238 * may have been written! Properly detecting if
239 * every byte is handled is unnecessary as the
240 * flag is used to detect devices that need
241 * special helpers to work correctly.
247 if (!handled
&& !err
) {
248 /* By default, anything not specificially handled above is
249 * read-only. The permissive flag changes this behavior so
250 * that anything not specifically handled above is writable.
251 * This means that some fields may still be read-only because
252 * they have entries in the config_field list that intercept
253 * the write and do nothing. */
254 if (dev_data
->permissive
|| xen_pcibk_permissive
) {
257 err
= pci_write_config_byte(dev
, offset
,
261 err
= pci_write_config_word(dev
, offset
,
265 err
= pci_write_config_dword(dev
, offset
,
269 } else if (!dev_data
->warned_on_write
) {
270 dev_data
->warned_on_write
= 1;
271 dev_warn(&dev
->dev
, "Driver tried to write to a "
272 "read-only configuration space field at offset"
273 " 0x%x, size %d. This may be harmless, but if "
274 "you have problems with your device:\n"
275 "1) see permissive attribute in sysfs\n"
276 "2) report problems to the xen-devel "
277 "mailing list along with details of your "
278 "device obtained from lspci.\n", offset
, size
);
282 return xen_pcibios_err_to_errno(err
);
285 int xen_pcibk_get_interrupt_type(struct pci_dev
*dev
)
292 * Do not trust dev->msi(x)_enabled here, as enabling could be done
293 * bypassing the pci_*msi* functions, by the qemu.
296 err
= pci_read_config_word(dev
,
297 dev
->msi_cap
+ PCI_MSI_FLAGS
,
301 if (val
& PCI_MSI_FLAGS_ENABLE
)
302 ret
|= INTERRUPT_TYPE_MSI
;
305 err
= pci_read_config_word(dev
,
306 dev
->msix_cap
+ PCI_MSIX_FLAGS
,
310 if (val
& PCI_MSIX_FLAGS_ENABLE
)
311 ret
|= INTERRUPT_TYPE_MSIX
;
315 * PCIe spec says device cannot use INTx if MSI/MSI-X is enabled,
316 * so check for INTx only when both are disabled.
319 err
= pci_read_config_word(dev
, PCI_COMMAND
, &val
);
322 if (!(val
& PCI_COMMAND_INTX_DISABLE
))
323 ret
|= INTERRUPT_TYPE_INTX
;
326 return ret
?: INTERRUPT_TYPE_NONE
;
329 void xen_pcibk_config_free_dyn_fields(struct pci_dev
*dev
)
331 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
332 struct config_field_entry
*cfg_entry
, *t
;
333 const struct config_field
*field
;
335 dev_dbg(&dev
->dev
, "free-ing dynamically allocated virtual "
336 "configuration space fields\n");
340 list_for_each_entry_safe(cfg_entry
, t
, &dev_data
->config_fields
, list
) {
341 field
= cfg_entry
->field
;
344 field
->clean((struct config_field
*)field
);
346 kfree(cfg_entry
->data
);
348 list_del(&cfg_entry
->list
);
355 void xen_pcibk_config_reset_dev(struct pci_dev
*dev
)
357 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
358 const struct config_field_entry
*cfg_entry
;
359 const struct config_field
*field
;
361 dev_dbg(&dev
->dev
, "resetting virtual configuration space\n");
365 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
366 field
= cfg_entry
->field
;
369 field
->reset(dev
, OFFSET(cfg_entry
), cfg_entry
->data
);
373 void xen_pcibk_config_free_dev(struct pci_dev
*dev
)
375 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
376 struct config_field_entry
*cfg_entry
, *t
;
377 const struct config_field
*field
;
379 dev_dbg(&dev
->dev
, "free-ing virtual configuration space fields\n");
383 list_for_each_entry_safe(cfg_entry
, t
, &dev_data
->config_fields
, list
) {
384 list_del(&cfg_entry
->list
);
386 field
= cfg_entry
->field
;
389 field
->release(dev
, OFFSET(cfg_entry
), cfg_entry
->data
);
395 int xen_pcibk_config_add_field_offset(struct pci_dev
*dev
,
396 const struct config_field
*field
,
397 unsigned int base_offset
)
400 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
401 struct config_field_entry
*cfg_entry
;
404 cfg_entry
= kmalloc(sizeof(*cfg_entry
), GFP_KERNEL
);
410 cfg_entry
->data
= NULL
;
411 cfg_entry
->field
= field
;
412 cfg_entry
->base_offset
= base_offset
;
414 /* silently ignore duplicate fields */
415 err
= xen_pcibk_field_is_dup(dev
, OFFSET(cfg_entry
));
420 tmp
= field
->init(dev
, OFFSET(cfg_entry
));
427 cfg_entry
->data
= tmp
;
430 dev_dbg(&dev
->dev
, "added config field at offset 0x%02x\n",
432 list_add_tail(&cfg_entry
->list
, &dev_data
->config_fields
);
441 /* This sets up the device's virtual configuration space to keep track of
442 * certain registers (like the base address registers (BARs) so that we can
443 * keep the client from manipulating them directly.
445 int xen_pcibk_config_init_dev(struct pci_dev
*dev
)
448 struct xen_pcibk_dev_data
*dev_data
= pci_get_drvdata(dev
);
450 dev_dbg(&dev
->dev
, "initializing virtual configuration space\n");
452 INIT_LIST_HEAD(&dev_data
->config_fields
);
454 err
= xen_pcibk_config_header_add_fields(dev
);
458 err
= xen_pcibk_config_capability_add_fields(dev
);
462 err
= xen_pcibk_config_quirks_init(dev
);
468 int xen_pcibk_config_init(void)
470 return xen_pcibk_config_capability_init();