access.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/pci.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/ioport.h>
  6. #include <linux/wait.h>
  7. #include "pci.h"
  8. /*
  9. * This interrupt-safe spinlock protects all accesses to PCI
  10. * configuration space.
  11. */
  12. DEFINE_RAW_SPINLOCK(pci_lock);
  13. /*
  14. * Wrappers for all PCI configuration access functions. They just check
  15. * alignment, do locking and call the low-level functions pointed to
  16. * by pci_dev->ops.
  17. */
  18. #define PCI_byte_BAD 0
  19. #define PCI_word_BAD (pos & 1)
  20. #define PCI_dword_BAD (pos & 3)
  21. #ifdef CONFIG_PCI_LOCKLESS_CONFIG
  22. # define pci_lock_config(f) do { (void)(f); } while (0)
  23. # define pci_unlock_config(f) do { (void)(f); } while (0)
  24. #else
  25. # define pci_lock_config(f) raw_spin_lock_irqsave(&pci_lock, f)
  26. # define pci_unlock_config(f) raw_spin_unlock_irqrestore(&pci_lock, f)
  27. #endif
  28. #define PCI_OP_READ(size, type, len) \
  29. int noinline pci_bus_read_config_##size \
  30. (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
  31. { \
  32. int res; \
  33. unsigned long flags; \
  34. u32 data = 0; \
  35. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  36. pci_lock_config(flags); \
  37. res = bus->ops->read(bus, devfn, pos, len, &data); \
  38. if (res) \
  39. PCI_SET_ERROR_RESPONSE(value); \
  40. else \
  41. *value = (type)data; \
  42. pci_unlock_config(flags); \
  43. return res; \
  44. }
  45. #define PCI_OP_WRITE(size, type, len) \
  46. int noinline pci_bus_write_config_##size \
  47. (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
  48. { \
  49. int res; \
  50. unsigned long flags; \
  51. if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
  52. pci_lock_config(flags); \
  53. res = bus->ops->write(bus, devfn, pos, len, value); \
  54. pci_unlock_config(flags); \
  55. return res; \
  56. }
  57. PCI_OP_READ(byte, u8, 1)
  58. PCI_OP_READ(word, u16, 2)
  59. PCI_OP_READ(dword, u32, 4)
  60. PCI_OP_WRITE(byte, u8, 1)
  61. PCI_OP_WRITE(word, u16, 2)
  62. PCI_OP_WRITE(dword, u32, 4)
  63. EXPORT_SYMBOL(pci_bus_read_config_byte);
  64. EXPORT_SYMBOL(pci_bus_read_config_word);
  65. EXPORT_SYMBOL(pci_bus_read_config_dword);
  66. EXPORT_SYMBOL(pci_bus_write_config_byte);
  67. EXPORT_SYMBOL(pci_bus_write_config_word);
  68. EXPORT_SYMBOL(pci_bus_write_config_dword);
  69. int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn,
  70. int where, int size, u32 *val)
  71. {
  72. void __iomem *addr;
  73. addr = bus->ops->map_bus(bus, devfn, where);
  74. if (!addr)
  75. return PCIBIOS_DEVICE_NOT_FOUND;
  76. if (size == 1)
  77. *val = readb(addr);
  78. else if (size == 2)
  79. *val = readw(addr);
  80. else
  81. *val = readl(addr);
  82. return PCIBIOS_SUCCESSFUL;
  83. }
  84. EXPORT_SYMBOL_GPL(pci_generic_config_read);
  85. int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn,
  86. int where, int size, u32 val)
  87. {
  88. void __iomem *addr;
  89. addr = bus->ops->map_bus(bus, devfn, where);
  90. if (!addr)
  91. return PCIBIOS_DEVICE_NOT_FOUND;
  92. if (size == 1)
  93. writeb(val, addr);
  94. else if (size == 2)
  95. writew(val, addr);
  96. else
  97. writel(val, addr);
  98. return PCIBIOS_SUCCESSFUL;
  99. }
  100. EXPORT_SYMBOL_GPL(pci_generic_config_write);
  101. int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn,
  102. int where, int size, u32 *val)
  103. {
  104. void __iomem *addr;
  105. addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
  106. if (!addr)
  107. return PCIBIOS_DEVICE_NOT_FOUND;
  108. *val = readl(addr);
  109. if (size <= 2)
  110. *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  111. return PCIBIOS_SUCCESSFUL;
  112. }
  113. EXPORT_SYMBOL_GPL(pci_generic_config_read32);
  114. int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
  115. int where, int size, u32 val)
  116. {
  117. void __iomem *addr;
  118. u32 mask, tmp;
  119. addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
  120. if (!addr)
  121. return PCIBIOS_DEVICE_NOT_FOUND;
  122. if (size == 4) {
  123. writel(val, addr);
  124. return PCIBIOS_SUCCESSFUL;
  125. }
  126. /*
  127. * In general, hardware that supports only 32-bit writes on PCI is
  128. * not spec-compliant. For example, software may perform a 16-bit
  129. * write. If the hardware only supports 32-bit accesses, we must
  130. * do a 32-bit read, merge in the 16 bits we intend to write,
  131. * followed by a 32-bit write. If the 16 bits we *don't* intend to
  132. * write happen to have any RW1C (write-one-to-clear) bits set, we
  133. * just inadvertently cleared something we shouldn't have.
  134. */
  135. if (!bus->unsafe_warn) {
  136. dev_warn(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n",
  137. size, pci_domain_nr(bus), bus->number,
  138. PCI_SLOT(devfn), PCI_FUNC(devfn), where);
  139. bus->unsafe_warn = 1;
  140. }
  141. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  142. tmp = readl(addr) & mask;
  143. tmp |= val << ((where & 0x3) * 8);
  144. writel(tmp, addr);
  145. return PCIBIOS_SUCCESSFUL;
  146. }
  147. EXPORT_SYMBOL_GPL(pci_generic_config_write32);
  148. /**
  149. * pci_bus_set_ops - Set raw operations of pci bus
  150. * @bus: pci bus struct
  151. * @ops: new raw operations
  152. *
  153. * Return previous raw operations
  154. */
  155. struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
  156. {
  157. struct pci_ops *old_ops;
  158. unsigned long flags;
  159. raw_spin_lock_irqsave(&pci_lock, flags);
  160. old_ops = bus->ops;
  161. bus->ops = ops;
  162. raw_spin_unlock_irqrestore(&pci_lock, flags);
  163. return old_ops;
  164. }
  165. EXPORT_SYMBOL(pci_bus_set_ops);
  166. /*
  167. * The following routines are to prevent the user from accessing PCI config
  168. * space when it's unsafe to do so. Some devices require this during BIST and
  169. * we're required to prevent it during D-state transitions.
  170. *
  171. * We have a bit per device to indicate it's blocked and a global wait queue
  172. * for callers to sleep on until devices are unblocked.
  173. */
  174. static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
  175. static noinline void pci_wait_cfg(struct pci_dev *dev)
  176. __must_hold(&pci_lock)
  177. {
  178. do {
  179. raw_spin_unlock_irq(&pci_lock);
  180. wait_event(pci_cfg_wait, !dev->block_cfg_access);
  181. raw_spin_lock_irq(&pci_lock);
  182. } while (dev->block_cfg_access);
  183. }
  184. /* Returns 0 on success, negative values indicate error. */
  185. #define PCI_USER_READ_CONFIG(size, type) \
  186. int pci_user_read_config_##size \
  187. (struct pci_dev *dev, int pos, type *val) \
  188. { \
  189. int ret = PCIBIOS_SUCCESSFUL; \
  190. u32 data = -1; \
  191. if (PCI_##size##_BAD) \
  192. return -EINVAL; \
  193. raw_spin_lock_irq(&pci_lock); \
  194. if (unlikely(dev->block_cfg_access)) \
  195. pci_wait_cfg(dev); \
  196. ret = dev->bus->ops->read(dev->bus, dev->devfn, \
  197. pos, sizeof(type), &data); \
  198. raw_spin_unlock_irq(&pci_lock); \
  199. if (ret) \
  200. PCI_SET_ERROR_RESPONSE(val); \
  201. else \
  202. *val = (type)data; \
  203. return pcibios_err_to_errno(ret); \
  204. } \
  205. EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
  206. /* Returns 0 on success, negative values indicate error. */
  207. #define PCI_USER_WRITE_CONFIG(size, type) \
  208. int pci_user_write_config_##size \
  209. (struct pci_dev *dev, int pos, type val) \
  210. { \
  211. int ret = PCIBIOS_SUCCESSFUL; \
  212. if (PCI_##size##_BAD) \
  213. return -EINVAL; \
  214. raw_spin_lock_irq(&pci_lock); \
  215. if (unlikely(dev->block_cfg_access)) \
  216. pci_wait_cfg(dev); \
  217. ret = dev->bus->ops->write(dev->bus, dev->devfn, \
  218. pos, sizeof(type), val); \
  219. raw_spin_unlock_irq(&pci_lock); \
  220. return pcibios_err_to_errno(ret); \
  221. } \
  222. EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
  223. PCI_USER_READ_CONFIG(byte, u8)
  224. PCI_USER_READ_CONFIG(word, u16)
  225. PCI_USER_READ_CONFIG(dword, u32)
  226. PCI_USER_WRITE_CONFIG(byte, u8)
  227. PCI_USER_WRITE_CONFIG(word, u16)
  228. PCI_USER_WRITE_CONFIG(dword, u32)
  229. /**
  230. * pci_cfg_access_lock - Lock PCI config reads/writes
  231. * @dev: pci device struct
  232. *
  233. * When access is locked, any userspace reads or writes to config
  234. * space and concurrent lock requests will sleep until access is
  235. * allowed via pci_cfg_access_unlock() again.
  236. */
  237. void pci_cfg_access_lock(struct pci_dev *dev)
  238. {
  239. might_sleep();
  240. raw_spin_lock_irq(&pci_lock);
  241. if (dev->block_cfg_access)
  242. pci_wait_cfg(dev);
  243. dev->block_cfg_access = 1;
  244. raw_spin_unlock_irq(&pci_lock);
  245. }
  246. EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
  247. /**
  248. * pci_cfg_access_trylock - try to lock PCI config reads/writes
  249. * @dev: pci device struct
  250. *
  251. * Same as pci_cfg_access_lock, but will return 0 if access is
  252. * already locked, 1 otherwise. This function can be used from
  253. * atomic contexts.
  254. */
  255. bool pci_cfg_access_trylock(struct pci_dev *dev)
  256. {
  257. unsigned long flags;
  258. bool locked = true;
  259. raw_spin_lock_irqsave(&pci_lock, flags);
  260. if (dev->block_cfg_access)
  261. locked = false;
  262. else
  263. dev->block_cfg_access = 1;
  264. raw_spin_unlock_irqrestore(&pci_lock, flags);
  265. return locked;
  266. }
  267. EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
  268. /**
  269. * pci_cfg_access_unlock - Unlock PCI config reads/writes
  270. * @dev: pci device struct
  271. *
  272. * This function allows PCI config accesses to resume.
  273. */
  274. void pci_cfg_access_unlock(struct pci_dev *dev)
  275. {
  276. unsigned long flags;
  277. raw_spin_lock_irqsave(&pci_lock, flags);
  278. /*
  279. * This indicates a problem in the caller, but we don't need
  280. * to kill them, unlike a double-block above.
  281. */
  282. WARN_ON(!dev->block_cfg_access);
  283. dev->block_cfg_access = 0;
  284. raw_spin_unlock_irqrestore(&pci_lock, flags);
  285. wake_up_all(&pci_cfg_wait);
  286. }
  287. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  288. static inline int pcie_cap_version(const struct pci_dev *dev)
  289. {
  290. return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
  291. }
  292. bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  293. {
  294. int type = pci_pcie_type(dev);
  295. return type == PCI_EXP_TYPE_ENDPOINT ||
  296. type == PCI_EXP_TYPE_LEG_END ||
  297. type == PCI_EXP_TYPE_ROOT_PORT ||
  298. type == PCI_EXP_TYPE_UPSTREAM ||
  299. type == PCI_EXP_TYPE_DOWNSTREAM ||
  300. type == PCI_EXP_TYPE_PCI_BRIDGE ||
  301. type == PCI_EXP_TYPE_PCIE_BRIDGE;
  302. }
  303. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  304. {
  305. return pcie_downstream_port(dev) &&
  306. pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
  307. }
  308. bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  309. {
  310. int type = pci_pcie_type(dev);
  311. return type == PCI_EXP_TYPE_ROOT_PORT ||
  312. type == PCI_EXP_TYPE_RC_EC;
  313. }
  314. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  315. {
  316. if (!pci_is_pcie(dev))
  317. return false;
  318. switch (pos) {
  319. case PCI_EXP_FLAGS:
  320. return true;
  321. case PCI_EXP_DEVCAP:
  322. case PCI_EXP_DEVCTL:
  323. case PCI_EXP_DEVSTA:
  324. return true;
  325. case PCI_EXP_LNKCAP:
  326. case PCI_EXP_LNKCTL:
  327. case PCI_EXP_LNKSTA:
  328. return pcie_cap_has_lnkctl(dev);
  329. case PCI_EXP_SLTCAP:
  330. case PCI_EXP_SLTCTL:
  331. case PCI_EXP_SLTSTA:
  332. return pcie_cap_has_sltctl(dev);
  333. case PCI_EXP_RTCTL:
  334. case PCI_EXP_RTCAP:
  335. case PCI_EXP_RTSTA:
  336. return pcie_cap_has_rtctl(dev);
  337. case PCI_EXP_DEVCAP2:
  338. case PCI_EXP_DEVCTL2:
  339. case PCI_EXP_LNKCAP2:
  340. case PCI_EXP_LNKCTL2:
  341. case PCI_EXP_LNKSTA2:
  342. return pcie_cap_version(dev) > 1;
  343. default:
  344. return false;
  345. }
  346. }
  347. /*
  348. * Note that these accessor functions are only for the "PCI Express
  349. * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
  350. * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  351. */
  352. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  353. {
  354. int ret;
  355. *val = 0;
  356. if (pos & 1)
  357. return PCIBIOS_BAD_REGISTER_NUMBER;
  358. if (pcie_capability_reg_implemented(dev, pos)) {
  359. ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  360. /*
  361. * Reset *val to 0 if pci_read_config_word() fails; it may
  362. * have been written as 0xFFFF (PCI_ERROR_RESPONSE) if the
  363. * config read failed on PCI.
  364. */
  365. if (ret)
  366. *val = 0;
  367. return ret;
  368. }
  369. /*
  370. * For Functions that do not implement the Slot Capabilities,
  371. * Slot Status, and Slot Control registers, these spaces must
  372. * be hardwired to 0b, with the exception of the Presence Detect
  373. * State bit in the Slot Status register of Downstream Ports,
  374. * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
  375. */
  376. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  377. pos == PCI_EXP_SLTSTA)
  378. *val = PCI_EXP_SLTSTA_PDS;
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(pcie_capability_read_word);
  382. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  383. {
  384. int ret;
  385. *val = 0;
  386. if (pos & 3)
  387. return PCIBIOS_BAD_REGISTER_NUMBER;
  388. if (pcie_capability_reg_implemented(dev, pos)) {
  389. ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  390. /*
  391. * Reset *val to 0 if pci_read_config_dword() fails; it may
  392. * have been written as 0xFFFFFFFF (PCI_ERROR_RESPONSE) if
  393. * the config read failed on PCI.
  394. */
  395. if (ret)
  396. *val = 0;
  397. return ret;
  398. }
  399. if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
  400. pos == PCI_EXP_SLTSTA)
  401. *val = PCI_EXP_SLTSTA_PDS;
  402. return 0;
  403. }
  404. EXPORT_SYMBOL(pcie_capability_read_dword);
  405. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  406. {
  407. if (pos & 1)
  408. return PCIBIOS_BAD_REGISTER_NUMBER;
  409. if (!pcie_capability_reg_implemented(dev, pos))
  410. return 0;
  411. return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  412. }
  413. EXPORT_SYMBOL(pcie_capability_write_word);
  414. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  415. {
  416. if (pos & 3)
  417. return PCIBIOS_BAD_REGISTER_NUMBER;
  418. if (!pcie_capability_reg_implemented(dev, pos))
  419. return 0;
  420. return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  421. }
  422. EXPORT_SYMBOL(pcie_capability_write_dword);
  423. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  424. u16 clear, u16 set)
  425. {
  426. int ret;
  427. u16 val;
  428. ret = pcie_capability_read_word(dev, pos, &val);
  429. if (!ret) {
  430. val &= ~clear;
  431. val |= set;
  432. ret = pcie_capability_write_word(dev, pos, val);
  433. }
  434. return ret;
  435. }
  436. EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
  437. int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
  438. u32 clear, u32 set)
  439. {
  440. int ret;
  441. u32 val;
  442. ret = pcie_capability_read_dword(dev, pos, &val);
  443. if (!ret) {
  444. val &= ~clear;
  445. val |= set;
  446. ret = pcie_capability_write_dword(dev, pos, val);
  447. }
  448. return ret;
  449. }
  450. EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);
  451. int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val)
  452. {
  453. if (pci_dev_is_disconnected(dev)) {
  454. PCI_SET_ERROR_RESPONSE(val);
  455. return PCIBIOS_DEVICE_NOT_FOUND;
  456. }
  457. return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val);
  458. }
  459. EXPORT_SYMBOL(pci_read_config_byte);
  460. int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val)
  461. {
  462. if (pci_dev_is_disconnected(dev)) {
  463. PCI_SET_ERROR_RESPONSE(val);
  464. return PCIBIOS_DEVICE_NOT_FOUND;
  465. }
  466. return pci_bus_read_config_word(dev->bus, dev->devfn, where, val);
  467. }
  468. EXPORT_SYMBOL(pci_read_config_word);
  469. int pci_read_config_dword(const struct pci_dev *dev, int where,
  470. u32 *val)
  471. {
  472. if (pci_dev_is_disconnected(dev)) {
  473. PCI_SET_ERROR_RESPONSE(val);
  474. return PCIBIOS_DEVICE_NOT_FOUND;
  475. }
  476. return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val);
  477. }
  478. EXPORT_SYMBOL(pci_read_config_dword);
  479. int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val)
  480. {
  481. if (pci_dev_is_disconnected(dev))
  482. return PCIBIOS_DEVICE_NOT_FOUND;
  483. return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val);
  484. }
  485. EXPORT_SYMBOL(pci_write_config_byte);
  486. int pci_write_config_word(const struct pci_dev *dev, int where, u16 val)
  487. {
  488. if (pci_dev_is_disconnected(dev))
  489. return PCIBIOS_DEVICE_NOT_FOUND;
  490. return pci_bus_write_config_word(dev->bus, dev->devfn, where, val);
  491. }
  492. EXPORT_SYMBOL(pci_write_config_word);
  493. int pci_write_config_dword(const struct pci_dev *dev, int where,
  494. u32 val)
  495. {
  496. if (pci_dev_is_disconnected(dev))
  497. return PCIBIOS_DEVICE_NOT_FOUND;
  498. return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val);
  499. }
  500. EXPORT_SYMBOL(pci_write_config_dword);