blob: d96626c614f5674b4de24b1c11bcb13a456a6ca5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * For architectures where we want to allow direct access to the PCI config
4 * stuff - it would probably be preferable on PCs too, but there people
5 * just do it by hand with the magic northbridge registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/syscalls.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
Brian Kinge04b0ea2005-09-27 01:21:55 -070012#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010014SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn,
15 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
17 struct pci_dev *dev;
18 u8 byte;
19 u16 word;
20 u32 dword;
Alan Coxe4585da2007-04-23 14:57:37 +010021 long err;
22 long cfg_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 if (!capable(CAP_SYS_ADMIN))
Alan Coxe4585da2007-04-23 14:57:37 +010025 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 err = -ENODEV;
Sinan Kaya39c94652017-12-19 00:37:53 -050028 dev = pci_get_domain_bus_and_slot(0, bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 if (!dev)
30 goto error;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 switch (len) {
33 case 1:
Brian Kinge04b0ea2005-09-27 01:21:55 -070034 cfg_ret = pci_user_read_config_byte(dev, off, &byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 break;
36 case 2:
Brian Kinge04b0ea2005-09-27 01:21:55 -070037 cfg_ret = pci_user_read_config_word(dev, off, &word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 break;
39 case 4:
Brian Kinge04b0ea2005-09-27 01:21:55 -070040 cfg_ret = pci_user_read_config_dword(dev, off, &dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
42 default:
43 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 goto error;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 err = -EIO;
48 if (cfg_ret != PCIBIOS_SUCCESSFUL)
49 goto error;
50
51 switch (len) {
52 case 1:
53 err = put_user(byte, (unsigned char __user *)buf);
54 break;
55 case 2:
56 err = put_user(word, (unsigned short __user *)buf);
57 break;
58 case 4:
59 err = put_user(dword, (unsigned int __user *)buf);
60 break;
Alan Coxe4585da2007-04-23 14:57:37 +010061 }
62 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return err;
64
65error:
66 /* ??? XFree86 doesn't even check the return value. They
67 just look for 0xffffffff in the output, since that's what
68 they get instead of a machine check on x86. */
69 switch (len) {
70 case 1:
71 put_user(-1, (unsigned char __user *)buf);
72 break;
73 case 2:
74 put_user(-1, (unsigned short __user *)buf);
75 break;
76 case 4:
77 put_user(-1, (unsigned int __user *)buf);
78 break;
Alan Coxe4585da2007-04-23 14:57:37 +010079 }
80 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return err;
82}
83
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010084SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
85 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct pci_dev *dev;
88 u8 byte;
89 u16 word;
90 u32 dword;
91 int err = 0;
92
93 if (!capable(CAP_SYS_ADMIN))
94 return -EPERM;
95
Sinan Kaya39c94652017-12-19 00:37:53 -050096 dev = pci_get_domain_bus_and_slot(0, bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (!dev)
98 return -ENODEV;
99
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400100 switch (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 case 1:
102 err = get_user(byte, (u8 __user *)buf);
103 if (err)
104 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700105 err = pci_user_write_config_byte(dev, off, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (err != PCIBIOS_SUCCESSFUL)
107 err = -EIO;
108 break;
109
110 case 2:
111 err = get_user(word, (u16 __user *)buf);
112 if (err)
113 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700114 err = pci_user_write_config_word(dev, off, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (err != PCIBIOS_SUCCESSFUL)
116 err = -EIO;
117 break;
118
119 case 4:
120 err = get_user(dword, (u32 __user *)buf);
121 if (err)
122 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700123 err = pci_user_write_config_dword(dev, off, dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (err != PCIBIOS_SUCCESSFUL)
125 err = -EIO;
126 break;
127
128 default:
129 err = -EINVAL;
130 break;
Alan Coxe4585da2007-04-23 14:57:37 +0100131 }
Alan Coxe4585da2007-04-23 14:57:37 +0100132 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return err;
134}