pcibr_ate.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-2004 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 = 0; /* 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. uint64_t value)
  19. {
  20. uint64_t *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. uint64_t *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 = 0;
  102. uint64_t flag;
  103. flag = pcibr_lock(pcibus_info);
  104. status = alloc_ate_resource(&pcibus_info->pbi_int_ate_resource, count);
  105. if (status < 0) {
  106. /* Failed to allocate */
  107. pcibr_unlock(pcibus_info, flag);
  108. return -1;
  109. }
  110. pcibr_unlock(pcibus_info, flag);
  111. return status;
  112. }
  113. /*
  114. * Setup an Address Translation Entry as specified. Use either the Bridge
  115. * internal maps or the external map RAM, as appropriate.
  116. */
  117. static inline uint64_t *pcibr_ate_addr(struct pcibus_info *pcibus_info,
  118. int ate_index)
  119. {
  120. if (ate_index < pcibus_info->pbi_int_ate_size) {
  121. return pcireg_int_ate_addr(pcibus_info, ate_index);
  122. }
  123. panic("pcibr_ate_addr: invalid ate_index 0x%x", ate_index);
  124. }
  125. /*
  126. * Update the ate.
  127. */
  128. void inline
  129. ate_write(struct pcibus_info *pcibus_info, int ate_index, int count,
  130. volatile uint64_t ate)
  131. {
  132. while (count-- > 0) {
  133. if (ate_index < pcibus_info->pbi_int_ate_size) {
  134. pcireg_int_ate_set(pcibus_info, ate_index, ate);
  135. } else {
  136. panic("ate_write: invalid ate_index 0x%x", ate_index);
  137. }
  138. ate_index++;
  139. ate += IOPGSIZE;
  140. }
  141. pcireg_tflush_get(pcibus_info); /* wait until Bridge PIO complete */
  142. }
  143. void pcibr_ate_free(struct pcibus_info *pcibus_info, int index)
  144. {
  145. volatile uint64_t ate;
  146. int count;
  147. uint64_t flags;
  148. if (pcibr_invalidate_ate) {
  149. /* For debugging purposes, clear the valid bit in the ATE */
  150. ate = *pcibr_ate_addr(pcibus_info, index);
  151. count = pcibus_info->pbi_int_ate_resource.ate[index];
  152. ate_write(pcibus_info, index, count, (ate & ~PCI32_ATE_V));
  153. }
  154. flags = pcibr_lock(pcibus_info);
  155. free_ate_resource(&pcibus_info->pbi_int_ate_resource, index);
  156. pcibr_unlock(pcibus_info, flags);
  157. }