Path: blob/master/arch/ia64/sn/pci/pcibr/pcibr_ate.c
15133 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights reserved.6*/78#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>1314int pcibr_invalidate_ate; /* by default don't invalidate ATE on free */1516/*17* mark_ate: Mark the ate as either free or inuse.18*/19static void mark_ate(struct ate_resource *ate_resource, int start, int number,20u64 value)21{22u64 *ate = ate_resource->ate;23int index;24int length = 0;2526for (index = start; length < number; index++, length++)27ate[index] = value;28}2930/*31* find_free_ate: Find the first free ate index starting from the given32* index for the desired consecutive count.33*/34static int find_free_ate(struct ate_resource *ate_resource, int start,35int count)36{37u64 *ate = ate_resource->ate;38int index;39int start_free;4041for (index = start; index < ate_resource->num_ate;) {42if (!ate[index]) {43int i;44int free;45free = 0;46start_free = index; /* Found start free ate */47for (i = start_free; i < ate_resource->num_ate; i++) {48if (!ate[i]) { /* This is free */49if (++free == count)50return start_free;51} else {52index = i + 1;53break;54}55}56if (i >= ate_resource->num_ate)57return -1;58} else59index++; /* Try next ate */60}6162return -1;63}6465/*66* free_ate_resource: Free the requested number of ATEs.67*/68static inline void free_ate_resource(struct ate_resource *ate_resource,69int start)70{71mark_ate(ate_resource, start, ate_resource->ate[start], 0);72if ((ate_resource->lowest_free_index > start) ||73(ate_resource->lowest_free_index < 0))74ate_resource->lowest_free_index = start;75}7677/*78* alloc_ate_resource: Allocate the requested number of ATEs.79*/80static inline int alloc_ate_resource(struct ate_resource *ate_resource,81int ate_needed)82{83int start_index;8485/*86* Check for ate exhaustion.87*/88if (ate_resource->lowest_free_index < 0)89return -1;9091/*92* Find the required number of free consecutive ates.93*/94start_index =95find_free_ate(ate_resource, ate_resource->lowest_free_index,96ate_needed);97if (start_index >= 0)98mark_ate(ate_resource, start_index, ate_needed, ate_needed);99100ate_resource->lowest_free_index =101find_free_ate(ate_resource, ate_resource->lowest_free_index, 1);102103return start_index;104}105106/*107* Allocate "count" contiguous Bridge Address Translation Entries108* on the specified bridge to be used for PCI to XTALK mappings.109* Indices in rm map range from 1..num_entries. Indices returned110* to caller range from 0..num_entries-1.111*112* Return the start index on success, -1 on failure.113*/114int pcibr_ate_alloc(struct pcibus_info *pcibus_info, int count)115{116int status;117unsigned long flags;118119spin_lock_irqsave(&pcibus_info->pbi_lock, flags);120status = alloc_ate_resource(&pcibus_info->pbi_int_ate_resource, count);121spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);122123return status;124}125126/*127* Setup an Address Translation Entry as specified. Use either the Bridge128* internal maps or the external map RAM, as appropriate.129*/130static inline u64 __iomem *pcibr_ate_addr(struct pcibus_info *pcibus_info,131int ate_index)132{133if (ate_index < pcibus_info->pbi_int_ate_size) {134return pcireg_int_ate_addr(pcibus_info, ate_index);135}136panic("pcibr_ate_addr: invalid ate_index 0x%x", ate_index);137}138139/*140* Update the ate.141*/142void inline143ate_write(struct pcibus_info *pcibus_info, int ate_index, int count,144volatile u64 ate)145{146while (count-- > 0) {147if (ate_index < pcibus_info->pbi_int_ate_size) {148pcireg_int_ate_set(pcibus_info, ate_index, ate);149} else {150panic("ate_write: invalid ate_index 0x%x", ate_index);151}152ate_index++;153ate += IOPGSIZE;154}155156pcireg_tflush_get(pcibus_info); /* wait until Bridge PIO complete */157}158159void pcibr_ate_free(struct pcibus_info *pcibus_info, int index)160{161162volatile u64 ate;163int count;164unsigned long flags;165166if (pcibr_invalidate_ate) {167/* For debugging purposes, clear the valid bit in the ATE */168ate = *pcibr_ate_addr(pcibus_info, index);169count = pcibus_info->pbi_int_ate_resource.ate[index];170ate_write(pcibus_info, index, count, (ate & ~PCI32_ATE_V));171}172173spin_lock_irqsave(&pcibus_info->pbi_lock, flags);174free_ate_resource(&pcibus_info->pbi_int_ate_resource, index);175spin_unlock_irqrestore(&pcibus_info->pbi_lock, flags);176}177178179