Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/errno.h> |
| 9 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/syscalls.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 12 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 14 | SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn, |
| 15 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | { |
| 17 | struct pci_dev *dev; |
| 18 | u8 byte; |
| 19 | u16 word; |
| 20 | u32 dword; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 21 | long err; |
| 22 | long cfg_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | if (!capable(CAP_SYS_ADMIN)) |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 25 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | err = -ENODEV; |
Sinan Kaya | 39c9465 | 2017-12-19 00:37:53 -0500 | [diff] [blame] | 28 | dev = pci_get_domain_bus_and_slot(0, bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | if (!dev) |
| 30 | goto error; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | switch (len) { |
| 33 | case 1: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 34 | cfg_ret = pci_user_read_config_byte(dev, off, &byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | break; |
| 36 | case 2: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 37 | cfg_ret = pci_user_read_config_word(dev, off, &word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | break; |
| 39 | case 4: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 40 | cfg_ret = pci_user_read_config_dword(dev, off, &dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | break; |
| 42 | default: |
| 43 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | goto error; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 45 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 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 Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 61 | } |
| 62 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return err; |
| 64 | |
| 65 | error: |
| 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 Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 79 | } |
| 80 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return err; |
| 82 | } |
| 83 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 84 | SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn, |
| 85 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 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 Kaya | 39c9465 | 2017-12-19 00:37:53 -0500 | [diff] [blame] | 96 | dev = pci_get_domain_bus_and_slot(0, bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (!dev) |
| 98 | return -ENODEV; |
| 99 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 100 | switch (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | case 1: |
| 102 | err = get_user(byte, (u8 __user *)buf); |
| 103 | if (err) |
| 104 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 105 | err = pci_user_write_config_byte(dev, off, byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | 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 King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 114 | err = pci_user_write_config_word(dev, off, word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | 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 King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 123 | err = pci_user_write_config_dword(dev, off, dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (err != PCIBIOS_SUCCESSFUL) |
| 125 | err = -EIO; |
| 126 | break; |
| 127 | |
| 128 | default: |
| 129 | err = -EINVAL; |
| 130 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 131 | } |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 132 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return err; |
| 134 | } |