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> |
Matthew Garrett | eb627e1 | 2019-08-19 17:17:47 -0700 | [diff] [blame] | 10 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/syscalls.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 12 | #include <linux/uaccess.h> |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 13 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 15 | SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn, |
| 16 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
| 18 | struct pci_dev *dev; |
| 19 | u8 byte; |
| 20 | u16 word; |
| 21 | u32 dword; |
Krzysztof Wilczyński | d6b1715 | 2021-07-29 23:37:55 +0000 | [diff] [blame] | 22 | int err, cfg_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Krzysztof Wilczyński | a8bd29b | 2021-07-29 23:37:54 +0000 | [diff] [blame] | 24 | err = -EPERM; |
| 25 | dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | if (!capable(CAP_SYS_ADMIN)) |
Krzysztof Wilczyński | a8bd29b | 2021-07-29 23:37:54 +0000 | [diff] [blame] | 27 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | err = -ENODEV; |
Sinan Kaya | 39c9465 | 2017-12-19 00:37:53 -0500 | [diff] [blame] | 30 | dev = pci_get_domain_bus_and_slot(0, bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | if (!dev) |
| 32 | goto error; |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | switch (len) { |
| 35 | case 1: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 36 | cfg_ret = pci_user_read_config_byte(dev, off, &byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | break; |
| 38 | case 2: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 39 | cfg_ret = pci_user_read_config_word(dev, off, &word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | break; |
| 41 | case 4: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 42 | cfg_ret = pci_user_read_config_dword(dev, off, &dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | break; |
| 44 | default: |
| 45 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | goto error; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | err = -EIO; |
Heiner Kallweit | ef9e400 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 50 | if (cfg_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | goto error; |
| 52 | |
| 53 | switch (len) { |
| 54 | case 1: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 55 | err = put_user(byte, (u8 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | break; |
| 57 | case 2: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 58 | err = put_user(word, (u16 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | break; |
| 60 | case 4: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 61 | err = put_user(dword, (u32 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 63 | } |
| 64 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return err; |
| 66 | |
| 67 | error: |
| 68 | /* ??? XFree86 doesn't even check the return value. They |
| 69 | just look for 0xffffffff in the output, since that's what |
| 70 | they get instead of a machine check on x86. */ |
| 71 | switch (len) { |
| 72 | case 1: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 73 | put_user(-1, (u8 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | break; |
| 75 | case 2: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 76 | put_user(-1, (u16 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | break; |
| 78 | case 4: |
Bjorn Helgaas | 32e2a3f | 2021-01-25 11:57:38 -0600 | [diff] [blame] | 79 | put_user(-1, (u32 __user *)buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 81 | } |
| 82 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 86 | SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn, |
| 87 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct pci_dev *dev; |
| 90 | u8 byte; |
| 91 | u16 word; |
| 92 | u32 dword; |
| 93 | int err = 0; |
| 94 | |
Matthew Garrett | eb627e1 | 2019-08-19 17:17:47 -0700 | [diff] [blame] | 95 | if (!capable(CAP_SYS_ADMIN) || |
| 96 | security_locked_down(LOCKDOWN_PCI_ACCESS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return -EPERM; |
| 98 | |
Sinan Kaya | 39c9465 | 2017-12-19 00:37:53 -0500 | [diff] [blame] | 99 | dev = pci_get_domain_bus_and_slot(0, bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (!dev) |
| 101 | return -ENODEV; |
| 102 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 103 | switch (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | case 1: |
| 105 | err = get_user(byte, (u8 __user *)buf); |
| 106 | if (err) |
| 107 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 108 | err = pci_user_write_config_byte(dev, off, byte); |
Heiner Kallweit | ef9e400 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 109 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | err = -EIO; |
| 111 | break; |
| 112 | |
| 113 | case 2: |
| 114 | err = get_user(word, (u16 __user *)buf); |
| 115 | if (err) |
| 116 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 117 | err = pci_user_write_config_word(dev, off, word); |
Heiner Kallweit | ef9e400 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 118 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | err = -EIO; |
| 120 | break; |
| 121 | |
| 122 | case 4: |
| 123 | err = get_user(dword, (u32 __user *)buf); |
| 124 | if (err) |
| 125 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 126 | err = pci_user_write_config_dword(dev, off, dword); |
Heiner Kallweit | ef9e400 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 127 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | err = -EIO; |
| 129 | break; |
| 130 | |
| 131 | default: |
| 132 | err = -EINVAL; |
| 133 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 134 | } |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 135 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return err; |
| 137 | } |