pcibr_ate.c 4.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
6
 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights reserved.
L
Linus Torvalds 已提交
7 8 9 10
 */

#include <linux/types.h>
#include <asm/sn/sn_sal.h>
11
#include <asm/sn/pcibr_provider.h>
12 13
#include <asm/sn/pcibus_provider_defs.h>
#include <asm/sn/pcidev.h>
L
Linus Torvalds 已提交
14

15
int pcibr_invalidate_ate;	/* by default don't invalidate ATE on free */
L
Linus Torvalds 已提交
16 17 18 19 20

/*
 * mark_ate: Mark the ate as either free or inuse.
 */
static void mark_ate(struct ate_resource *ate_resource, int start, int number,
21
		     u64 value)
L
Linus Torvalds 已提交
22
{
23
	u64 *ate = ate_resource->ate;
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32
	int index;
	int length = 0;

	for (index = start; length < number; index++, length++)
		ate[index] = value;
}

/*
 * find_free_ate:  Find the first free ate index starting from the given
S
Simon Arlott 已提交
33
 *		   index for the desired consecutive count.
L
Linus Torvalds 已提交
34 35 36 37
 */
static int find_free_ate(struct ate_resource *ate_resource, int start,
			 int count)
{
38
	u64 *ate = ate_resource->ate;
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	int index;
	int start_free;

	for (index = start; index < ate_resource->num_ate;) {
		if (!ate[index]) {
			int i;
			int free;
			free = 0;
			start_free = index;	/* Found start free ate */
			for (i = start_free; i < ate_resource->num_ate; i++) {
				if (!ate[i]) {	/* This is free */
					if (++free == count)
						return start_free;
				} else {
					index = i + 1;
					break;
				}
			}
57 58
			if (i >= ate_resource->num_ate)
				return -1;
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		} else
			index++;	/* Try next ate */
	}

	return -1;
}

/*
 * free_ate_resource:  Free the requested number of ATEs.
 */
static inline void free_ate_resource(struct ate_resource *ate_resource,
				     int start)
{
	mark_ate(ate_resource, start, ate_resource->ate[start], 0);
	if ((ate_resource->lowest_free_index > start) ||
	    (ate_resource->lowest_free_index < 0))
		ate_resource->lowest_free_index = start;
}

/*
 * alloc_ate_resource:  Allocate the requested number of ATEs.
 */
static inline int alloc_ate_resource(struct ate_resource *ate_resource,
				     int ate_needed)
{
	int start_index;

	/*
	 * Check for ate exhaustion.
	 */
	if (ate_resource->lowest_free_index < 0)
		return -1;

	/*
S
Simon Arlott 已提交
93
	 * Find the required number of free consecutive ates.
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	 */
	start_index =
	    find_free_ate(ate_resource, ate_resource->lowest_free_index,
			  ate_needed);
	if (start_index >= 0)
		mark_ate(ate_resource, start_index, ate_needed, ate_needed);

	ate_resource->lowest_free_index =
	    find_free_ate(ate_resource, ate_resource->lowest_free_index, 1);

	return start_index;
}

/*
 * Allocate "count" contiguous Bridge Address Translation Entries
 * on the specified bridge to be used for PCI to XTALK mappings.
S
Simon Arlott 已提交
110
 * Indices in rm map range from 1..num_entries.  Indices returned
L
Linus Torvalds 已提交
111 112 113 114 115 116
 * to caller range from 0..num_entries-1.
 *
 * Return the start index on success, -1 on failure.
 */
int pcibr_ate_alloc(struct pcibus_info *pcibus_info, int count)
{
117 118
	int status;
	unsigned long flags;
L
Linus Torvalds 已提交
119

120
	spin_lock_irqsave(&pcibus_info->pbi_lock, flags);
L
Linus Torvalds 已提交
121
	status = alloc_ate_resource(&pcibus_info->pbi_int_ate_resource, count);
122
	spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130

	return status;
}

/*
 * Setup an Address Translation Entry as specified.  Use either the Bridge
 * internal maps or the external map RAM, as appropriate.
 */
A
Al Viro 已提交
131
static inline u64 __iomem *pcibr_ate_addr(struct pcibus_info *pcibus_info,
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
				       int ate_index)
{
	if (ate_index < pcibus_info->pbi_int_ate_size) {
		return pcireg_int_ate_addr(pcibus_info, ate_index);
	}
	panic("pcibr_ate_addr: invalid ate_index 0x%x", ate_index);
}

/*
 * Update the ate.
 */
void inline
ate_write(struct pcibus_info *pcibus_info, int ate_index, int count,
145
	  volatile u64 ate)
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
	while (count-- > 0) {
		if (ate_index < pcibus_info->pbi_int_ate_size) {
			pcireg_int_ate_set(pcibus_info, ate_index, ate);
		} else {
			panic("ate_write: invalid ate_index 0x%x", ate_index);
		}
		ate_index++;
		ate += IOPGSIZE;
	}

	pcireg_tflush_get(pcibus_info);	/* wait until Bridge PIO complete */
}

void pcibr_ate_free(struct pcibus_info *pcibus_info, int index)
{

163
	volatile u64 ate;
L
Linus Torvalds 已提交
164
	int count;
165
	unsigned long flags;
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173

	if (pcibr_invalidate_ate) {
		/* For debugging purposes, clear the valid bit in the ATE */
		ate = *pcibr_ate_addr(pcibus_info, index);
		count = pcibus_info->pbi_int_ate_resource.ate[index];
		ate_write(pcibus_info, index, count, (ate & ~PCI32_ATE_V));
	}

174
	spin_lock_irqsave(&pcibus_info->pbi_lock, flags);
L
Linus Torvalds 已提交
175
	free_ate_resource(&pcibus_info->pbi_int_ate_resource, index);
176
	spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);
L
Linus Torvalds 已提交
177
}