pcibr_ate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <asm/sn/sn_sal.h>
  10. #include <asm/sn/pcibr_provider.h>
  11. #include <asm/sn/pcibus_provider_defs.h>
  12. #include <asm/sn/pcidev.h>
  13. int pcibr_invalidate_ate; /* by default don't invalidate ATE on free */
  14. /*
  15. * mark_ate: Mark the ate as either free or inuse.
  16. */
  17. static void mark_ate(struct ate_resource *ate_resource, int start, int number,
  18. u64 value)
  19. {
  20. u64 *ate = ate_resource->ate;
  21. int index;
  22. int length = 0;
  23. for (index = start; length < number; index++, length++)
  24. ate[index] = value;
  25. }
  26. /*
  27. * find_free_ate: Find the first free ate index starting from the given
  28. * index for the desired consequtive count.
  29. */
  30. static int find_free_ate(struct ate_resource *ate_resource, int start,
  31. int count)
  32. {
  33. u64 *ate = ate_resource->ate;
  34. int index;
  35. int start_free;
  36. for (index = start; index < ate_resource->num_ate;) {
  37. if (!ate[index]) {
  38. int i;
  39. int free;
  40. free = 0;
  41. start_free = index; /* Found start free ate */
  42. for (i = start_free; i < ate_resource->num_ate; i++) {
  43. if (!ate[i]) { /* This is free */
  44. if (++free == count)
  45. return start_free;
  46. } else {
  47. index = i + 1;
  48. break;
  49. }
  50. }
  51. } else
  52. index++; /* Try next ate */
  53. }
  54. return -1;
  55. }
  56. /*
  57. * free_ate_resource: Free the requested number of ATEs.
  58. */
  59. static inline void free_ate_resource(struct ate_resource *ate_resource,
  60. int start)
  61. {
  62. mark_ate(ate_resource, start, ate_resource->ate[start], 0);
  63. if ((ate_resource->lowest_free_index > start) ||
  64. (ate_resource->lowest_free_index < 0))
  65. ate_resource->lowest_free_index = start;
  66. }
  67. /*
  68. * alloc_ate_resource: Allocate the requested number of ATEs.
  69. */
  70. static inline int alloc_ate_resource(struct ate_resource *ate_resource,
  71. int ate_needed)
  72. {
  73. int start_index;
  74. /*
  75. * Check for ate exhaustion.
  76. */
  77. if (ate_resource->lowest_free_index < 0)
  78. return -1;
  79. /*
  80. * Find the required number of free consequtive ates.
  81. */
  82. start_index =
  83. find_free_ate(ate_resource, ate_resource->lowest_free_index,
  84. ate_needed);
  85. if (start_index >= 0)
  86. mark_ate(ate_resource, start_index, ate_needed, ate_needed);
  87. ate_resource->lowest_free_index =
  88. find_free_ate(ate_resource, ate_resource->lowest_free_index, 1);
  89. return start_index;
  90. }
  91. /*
  92. * Allocate "count" contiguous Bridge Address Translation Entries
  93. * on the specified bridge to be used for PCI to XTALK mappings.
  94. * Indices in rm map range from 1..num_entries. Indicies returned
  95. * to caller range from 0..num_entries-1.
  96. *
  97. * Return the start index on success, -1 on failure.
  98. */
  99. int pcibr_ate_alloc(struct pcibus_info *pcibus_info, int count)
  100. {
  101. int status;
  102. unsigned long flags;
  103. spin_lock_irqsave(&pcibus_info->pbi_lock, flags);
  104. status = alloc_ate_resource(&pcibus_info->pbi_int_ate_resource, count);
  105. spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);
  106. return status;
  107. }
  108. /*
  109. * Setup an Address Translation Entry as specified. Use either the Bridge
  110. * internal maps or the external map RAM, as appropriate.
  111. */
  112. static inline u64 __iomem *pcibr_ate_addr(struct pcibus_info *pcibus_info,
  113. int ate_index)
  114. {
  115. if (ate_index < pcibus_info->pbi_int_ate_size) {
  116. return pcireg_int_ate_addr(pcibus_info, ate_index);
  117. }
  118. panic("pcibr_ate_addr: invalid ate_index 0x%x", ate_index);
  119. }
  120. /*
  121. * Update the ate.
  122. */
  123. void inline
  124. ate_write(struct pcibus_info *pcibus_info, int ate_index, int count,
  125. volatile u64 ate)
  126. {
  127. while (count-- > 0) {
  128. if (ate_index < pcibus_info->pbi_int_ate_size) {
  129. pcireg_int_ate_set(pcibus_info, ate_index, ate);
  130. } else {
  131. panic("ate_write: invalid ate_index 0x%x", ate_index);
  132. }
  133. ate_index++;
  134. ate += IOPGSIZE;
  135. }
  136. pcireg_tflush_get(pcibus_info); /* wait until Bridge PIO complete */
  137. }
  138. void pcibr_ate_free(struct pcibus_info *pcibus_info, int index)
  139. {
  140. volatile u64 ate;
  141. int count;
  142. unsigned long flags;
  143. if (pcibr_invalidate_ate) {
  144. /* For debugging purposes, clear the valid bit in the ATE */
  145. ate = *pcibr_ate_addr(pcibus_info, index);
  146. count = pcibus_info->pbi_int_ate_resource.ate[index];
  147. ate_write(pcibus_info, index, count, (ate & ~PCI32_ATE_V));
  148. }
  149. spin_lock_irqsave(&pcibus_info->pbi_lock, flags);
  150. free_ate_resource(&pcibus_info->pbi_int_ate_resource, index);
  151. spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);
  152. }