bonito.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* bonito.c - PCI bonito interface. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/pci.h>
  20. #include <grub/misc.h>
  21. static grub_uint32_t base_win[GRUB_MACHINE_PCI_NUM_WIN];
  22. static const grub_size_t sizes_win[GRUB_MACHINE_PCI_NUM_WIN] =
  23. {GRUB_MACHINE_PCI_WIN1_SIZE, GRUB_MACHINE_PCI_WIN_SIZE,
  24. GRUB_MACHINE_PCI_WIN_SIZE};
  25. /* Usage counters. */
  26. static int usage_win[GRUB_MACHINE_PCI_NUM_WIN];
  27. static grub_addr_t addr_win[GRUB_MACHINE_PCI_NUM_WIN] =
  28. {GRUB_MACHINE_PCI_WIN1_ADDR, GRUB_MACHINE_PCI_WIN2_ADDR,
  29. GRUB_MACHINE_PCI_WIN3_ADDR};
  30. grub_bonito_type_t grub_bonito_type;
  31. static volatile void *
  32. config_addr (grub_pci_address_t addr)
  33. {
  34. if (grub_bonito_type == GRUB_BONITO_2F)
  35. {
  36. GRUB_MACHINE_PCI_CONF_CTRL_REG_2F = 1 << ((addr >> 11) & 0xf);
  37. return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_2F
  38. | (addr & 0x07ff));
  39. }
  40. else
  41. {
  42. if (addr >> 16)
  43. return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A_EXT | addr);
  44. else
  45. return (volatile void *) (GRUB_MACHINE_PCI_CONFSPACE_3A | addr);
  46. }
  47. }
  48. grub_uint32_t
  49. grub_pci_read (grub_pci_address_t addr)
  50. {
  51. return *(volatile grub_uint32_t *) config_addr (addr);
  52. }
  53. grub_uint16_t
  54. grub_pci_read_word (grub_pci_address_t addr)
  55. {
  56. return *(volatile grub_uint16_t *) config_addr (addr);
  57. }
  58. grub_uint8_t
  59. grub_pci_read_byte (grub_pci_address_t addr)
  60. {
  61. return *(volatile grub_uint8_t *) config_addr (addr);
  62. }
  63. void
  64. grub_pci_write (grub_pci_address_t addr, grub_uint32_t data)
  65. {
  66. *(volatile grub_uint32_t *) config_addr (addr) = data;
  67. }
  68. void
  69. grub_pci_write_word (grub_pci_address_t addr, grub_uint16_t data)
  70. {
  71. *(volatile grub_uint16_t *) config_addr (addr) = data;
  72. }
  73. void
  74. grub_pci_write_byte (grub_pci_address_t addr, grub_uint8_t data)
  75. {
  76. *(volatile grub_uint8_t *) config_addr (addr) = data;
  77. }
  78. static inline void
  79. write_bases_2f (void)
  80. {
  81. int i;
  82. grub_uint32_t reg = 0;
  83. for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
  84. reg |= (((base_win[i] >> GRUB_MACHINE_PCI_WIN_SHIFT)
  85. & GRUB_MACHINE_PCI_WIN_MASK)
  86. << (i * GRUB_MACHINE_PCI_WIN_MASK_SIZE));
  87. GRUB_MACHINE_PCI_IO_CTRL_REG_2F = reg;
  88. }
  89. volatile void *
  90. grub_pci_device_map_range (grub_pci_device_t dev __attribute__ ((unused)),
  91. grub_addr_t base, grub_size_t size)
  92. {
  93. if (grub_bonito_type == GRUB_BONITO_2F)
  94. {
  95. int i;
  96. grub_addr_t newbase;
  97. /* First try already used registers. */
  98. for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
  99. if (usage_win[i] && base_win[i] <= base
  100. && base_win[i] + sizes_win[i] > base + size)
  101. {
  102. usage_win[i]++;
  103. return (void *)
  104. (addr_win[i] | (base & GRUB_MACHINE_PCI_WIN_OFFSET_MASK));
  105. }
  106. /* Map new register. */
  107. newbase = base & ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK;
  108. for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
  109. if (!usage_win[i] && newbase <= base
  110. && newbase + sizes_win[i] > base + size)
  111. {
  112. usage_win[i]++;
  113. base_win[i] = newbase;
  114. write_bases_2f ();
  115. return (void *)
  116. (addr_win[i] | (base & GRUB_MACHINE_PCI_WIN_OFFSET_MASK));
  117. }
  118. grub_fatal ("Out of PCI windows.");
  119. }
  120. else
  121. {
  122. int region = 0;
  123. if (base >= 0x10000000
  124. && base + size <= 0x18000000)
  125. region = 1;
  126. if (base >= 0x1c000000
  127. && base + size <= 0x1f000000)
  128. region = 2;
  129. if (region == 0)
  130. grub_fatal ("Attempt to map out of regions");
  131. return (void *) (0xa0000000 | base);
  132. }
  133. }
  134. void *
  135. grub_pci_device_map_range_cached (grub_pci_device_t dev,
  136. grub_addr_t base, grub_size_t size)
  137. {
  138. return (void *) (((grub_addr_t) grub_pci_device_map_range (dev, base, size))
  139. & ~0x20000000);
  140. }
  141. void
  142. grub_pci_device_unmap_range (grub_pci_device_t dev __attribute__ ((unused)),
  143. volatile void *mem,
  144. grub_size_t size __attribute__ ((unused)))
  145. {
  146. if (grub_bonito_type == GRUB_BONITO_2F)
  147. {
  148. int i;
  149. for (i = 0; i < GRUB_MACHINE_PCI_NUM_WIN; i++)
  150. if (usage_win[i] && addr_win[i]
  151. == (((grub_addr_t) mem | 0x20000000)
  152. & ~GRUB_MACHINE_PCI_WIN_OFFSET_MASK))
  153. {
  154. usage_win[i]--;
  155. return;
  156. }
  157. grub_fatal ("Tried to unmap not mapped region");
  158. }
  159. }