numachip.c 2.9 KB

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