ipmi_si_mem_io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(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. io->io_cleanup = mem_cleanup;
  70. /*
  71. * Figure out the actual readb/readw/readl/etc routine to use based
  72. * upon the register size.
  73. */
  74. switch (io->regsize) {
  75. case 1:
  76. io->inputb = intf_mem_inb;
  77. io->outputb = intf_mem_outb;
  78. break;
  79. case 2:
  80. io->inputb = intf_mem_inw;
  81. io->outputb = intf_mem_outw;
  82. break;
  83. case 4:
  84. io->inputb = intf_mem_inl;
  85. io->outputb = intf_mem_outl;
  86. break;
  87. #ifdef readq
  88. case 8:
  89. io->inputb = mem_inq;
  90. io->outputb = mem_outq;
  91. break;
  92. #endif
  93. default:
  94. dev_warn(io->dev, "Invalid register size: %d\n",
  95. io->regsize);
  96. return -EINVAL;
  97. }
  98. /*
  99. * Some BIOSes reserve disjoint memory regions in their ACPI
  100. * tables. This causes problems when trying to request the
  101. * entire region. Therefore we must request each register
  102. * separately.
  103. */
  104. for (idx = 0; idx < io->io_size; idx++) {
  105. if (request_mem_region(addr + idx * io->regspacing,
  106. io->regsize, DEVICE_NAME) == NULL) {
  107. /* Undo allocations */
  108. mem_region_cleanup(io, idx);
  109. return -EIO;
  110. }
  111. }
  112. /*
  113. * Calculate the total amount of memory to claim. This is an
  114. * unusual looking calculation, but it avoids claiming any
  115. * more memory than it has to. It will claim everything
  116. * between the first address to the end of the last full
  117. * register.
  118. */
  119. mapsize = ((io->io_size * io->regspacing)
  120. - (io->regspacing - io->regsize));
  121. io->addr = ioremap(addr, mapsize);
  122. if (io->addr == NULL) {
  123. mem_region_cleanup(io, io->io_size);
  124. return -EIO;
  125. }
  126. return 0;
  127. }