ipmi_si_mem_io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/io.h>
  3. #include "ipmi_si.h"
  4. static unsigned char intf_mem_inb(const struct si_sm_io *io,
  5. unsigned int offset)
  6. {
  7. return readb((io->addr)+(offset * io->regspacing));
  8. }
  9. static void intf_mem_outb(const struct si_sm_io *io, unsigned int offset,
  10. unsigned char b)
  11. {
  12. writeb(b, (io->addr)+(offset * io->regspacing));
  13. }
  14. static unsigned char intf_mem_inw(const struct si_sm_io *io,
  15. unsigned int offset)
  16. {
  17. return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
  18. & 0xff;
  19. }
  20. static void intf_mem_outw(const struct si_sm_io *io, unsigned int offset,
  21. unsigned char b)
  22. {
  23. writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
  24. }
  25. static unsigned char intf_mem_inl(const struct si_sm_io *io,
  26. unsigned int offset)
  27. {
  28. return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
  29. & 0xff;
  30. }
  31. static void intf_mem_outl(const struct si_sm_io *io, unsigned int offset,
  32. unsigned char b)
  33. {
  34. writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
  35. }
  36. #ifdef readq
  37. static unsigned char mem_inq(const struct si_sm_io *io, unsigned int offset)
  38. {
  39. return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
  40. & 0xff;
  41. }
  42. static void mem_outq(const struct si_sm_io *io, unsigned int offset,
  43. unsigned char b)
  44. {
  45. writeq((u64)b << io->regshift, (io->addr)+(offset * io->regspacing));
  46. }
  47. #endif
  48. static void mem_region_cleanup(struct si_sm_io *io, int num)
  49. {
  50. unsigned long addr = io->addr_data;
  51. int idx;
  52. for (idx = 0; idx < num; idx++)
  53. release_mem_region(addr + idx * io->regspacing,
  54. io->regsize);
  55. }
  56. static void mem_cleanup(struct si_sm_io *io)
  57. {
  58. if (io->addr) {
  59. iounmap(io->addr);
  60. mem_region_cleanup(io, io->io_size);
  61. }
  62. }
  63. int ipmi_si_mem_setup(struct si_sm_io *io)
  64. {
  65. unsigned long addr = io->addr_data;
  66. int mapsize, idx;
  67. if (!addr)
  68. return -ENODEV;
  69. /*
  70. * Figure out the actual readb/readw/readl/etc routine to use based
  71. * upon the register size.
  72. */
  73. switch (io->regsize) {
  74. case 1:
  75. io->inputb = intf_mem_inb;
  76. io->outputb = intf_mem_outb;
  77. break;
  78. case 2:
  79. io->inputb = intf_mem_inw;
  80. io->outputb = intf_mem_outw;
  81. break;
  82. case 4:
  83. io->inputb = intf_mem_inl;
  84. io->outputb = intf_mem_outl;
  85. break;
  86. #ifdef readq
  87. case 8:
  88. io->inputb = mem_inq;
  89. io->outputb = mem_outq;
  90. break;
  91. #endif
  92. default:
  93. dev_warn(io->dev, "Invalid register size: %d\n",
  94. io->regsize);
  95. return -EINVAL;
  96. }
  97. /*
  98. * Some BIOSes reserve disjoint memory regions in their ACPI
  99. * tables. This causes problems when trying to request the
  100. * entire region. Therefore we must request each register
  101. * separately.
  102. */
  103. for (idx = 0; idx < io->io_size; idx++) {
  104. if (request_mem_region(addr + idx * io->regspacing,
  105. io->regsize, DEVICE_NAME) == NULL) {
  106. /* Undo allocations */
  107. mem_region_cleanup(io, idx);
  108. return -EIO;
  109. }
  110. }
  111. /*
  112. * Calculate the total amount of memory to claim. This is an
  113. * unusual looking calculation, but it avoids claiming any
  114. * more memory than it has to. It will claim everything
  115. * between the first address to the end of the last full
  116. * register.
  117. */
  118. mapsize = ((io->io_size * io->regspacing)
  119. - (io->regspacing - io->regsize));
  120. io->addr = ioremap(addr, mapsize);
  121. if (io->addr == NULL) {
  122. mem_region_cleanup(io, io->io_size);
  123. return -EIO;
  124. }
  125. io->io_cleanup = mem_cleanup;
  126. return 0;
  127. }