numachip.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Numascale NumaConnect-specific PCI code
  4. *
  5. * Copyright (C) 2012 Numascale AS. All rights reserved.
  6. *
  7. * Send feedback to <[email protected]>
  8. *
  9. * PCI accessor functions derived from mmconfig_64.c
  10. *
  11. */
  12. #include <linux/pci.h>
  13. #include <asm/pci_x86.h>
  14. #include <asm/numachip/numachip.h>
  15. static u8 limit __read_mostly;
  16. static inline char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
  17. {
  18. struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
  19. if (cfg && cfg->virt)
  20. return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
  21. return NULL;
  22. }
  23. static int pci_mmcfg_read_numachip(unsigned int seg, unsigned int bus,
  24. unsigned int devfn, int reg, int len, u32 *value)
  25. {
  26. char __iomem *addr;
  27. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  28. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
  29. err: *value = -1;
  30. return -EINVAL;
  31. }
  32. /* Ensure AMD Northbridges don't decode reads to other devices */
  33. if (unlikely(bus == 0 && devfn >= limit)) {
  34. *value = -1;
  35. return 0;
  36. }
  37. rcu_read_lock();
  38. addr = pci_dev_base(seg, bus, devfn);
  39. if (!addr) {
  40. rcu_read_unlock();
  41. goto err;
  42. }
  43. switch (len) {
  44. case 1:
  45. *value = mmio_config_readb(addr + reg);
  46. break;
  47. case 2:
  48. *value = mmio_config_readw(addr + reg);
  49. break;
  50. case 4:
  51. *value = mmio_config_readl(addr + reg);
  52. break;
  53. }
  54. rcu_read_unlock();
  55. return 0;
  56. }
  57. static int pci_mmcfg_write_numachip(unsigned int seg, unsigned int bus,
  58. unsigned int devfn, int reg, int len, u32 value)
  59. {
  60. char __iomem *addr;
  61. /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
  62. if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
  63. return -EINVAL;
  64. /* Ensure AMD Northbridges don't decode writes to other devices */
  65. if (unlikely(bus == 0 && devfn >= limit))
  66. return 0;
  67. rcu_read_lock();
  68. addr = pci_dev_base(seg, bus, devfn);
  69. if (!addr) {
  70. rcu_read_unlock();
  71. return -EINVAL;
  72. }
  73. switch (len) {
  74. case 1:
  75. mmio_config_writeb(addr + reg, value);
  76. break;
  77. case 2:
  78. mmio_config_writew(addr + reg, value);
  79. break;
  80. case 4:
  81. mmio_config_writel(addr + reg, value);
  82. break;
  83. }
  84. rcu_read_unlock();
  85. return 0;
  86. }
  87. static const struct pci_raw_ops pci_mmcfg_numachip = {
  88. .read = pci_mmcfg_read_numachip,
  89. .write = pci_mmcfg_write_numachip,
  90. };
  91. int __init pci_numachip_init(void)
  92. {
  93. int ret = 0;
  94. u32 val;
  95. /* For remote I/O, restrict bus 0 access to the actual number of AMD
  96. Northbridges, which starts at device number 0x18 */
  97. ret = raw_pci_read(0, 0, PCI_DEVFN(0x18, 0), 0x60, sizeof(val), &val);
  98. if (ret)
  99. goto out;
  100. /* HyperTransport fabric size in bits 6:4 */
  101. limit = PCI_DEVFN(0x18 + ((val >> 4) & 7) + 1, 0);
  102. /* Use NumaChip PCI accessors for non-extended and extended access */
  103. raw_pci_ops = raw_pci_ext_ops = &pci_mmcfg_numachip;
  104. out:
  105. return ret;
  106. }