Path: blob/master/arch/ia64/sn/pci/tioce_provider.c
15126 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) 2003-2006 Silicon Graphics, Inc. All Rights Reserved.6*/78#include <linux/types.h>9#include <linux/interrupt.h>10#include <linux/slab.h>11#include <linux/pci.h>12#include <asm/sn/sn_sal.h>13#include <asm/sn/addrs.h>14#include <asm/sn/io.h>15#include <asm/sn/pcidev.h>16#include <asm/sn/pcibus_provider_defs.h>17#include <asm/sn/tioce_provider.h>1819/*20* 1/26/200621*22* WAR for SGI PV 944642. For revA TIOCE, need to use the following recipe23* (taken from the above PV) before and after accessing tioce internal MMR's24* to avoid tioce lockups.25*26* The recipe as taken from the PV:27*28* if(mmr address < 0x45000) {29* if(mmr address == 0 or 0x80)30* mmr wrt or read address 0xc031* else if(mmr address == 0x148 or 0x200)32* mmr wrt or read address 0x2833* else34* mmr wrt or read address 0x15835*36* do desired mmr access (rd or wrt)37*38* if(mmr address == 0x100)39* mmr wrt or read address 0x3840* mmr wrt or read address 0xb05041* } else42* do desired mmr access43*44* According to hw, we can use reads instead of writes to the above address45*46* Note this WAR can only to be used for accessing internal MMR's in the47* TIOCE Coretalk Address Range 0x0 - 0x07ff_ffff. This includes the48* "Local CE Registers and Memories" and "PCI Compatible Config Space" address49* spaces from table 2-1 of the "CE Programmer's Reference Overview" document.50*51* All registers defined in struct tioce will meet that criteria.52*/5354static void inline55tioce_mmr_war_pre(struct tioce_kernel *kern, void __iomem *mmr_addr)56{57u64 mmr_base;58u64 mmr_offset;5960if (kern->ce_common->ce_rev != TIOCE_REV_A)61return;6263mmr_base = kern->ce_common->ce_pcibus.bs_base;64mmr_offset = (unsigned long)mmr_addr - mmr_base;6566if (mmr_offset < 0x45000) {67u64 mmr_war_offset;6869if (mmr_offset == 0 || mmr_offset == 0x80)70mmr_war_offset = 0xc0;71else if (mmr_offset == 0x148 || mmr_offset == 0x200)72mmr_war_offset = 0x28;73else74mmr_war_offset = 0x158;7576readq_relaxed((void __iomem *)(mmr_base + mmr_war_offset));77}78}7980static void inline81tioce_mmr_war_post(struct tioce_kernel *kern, void __iomem *mmr_addr)82{83u64 mmr_base;84u64 mmr_offset;8586if (kern->ce_common->ce_rev != TIOCE_REV_A)87return;8889mmr_base = kern->ce_common->ce_pcibus.bs_base;90mmr_offset = (unsigned long)mmr_addr - mmr_base;9192if (mmr_offset < 0x45000) {93if (mmr_offset == 0x100)94readq_relaxed((void __iomem *)(mmr_base + 0x38));95readq_relaxed((void __iomem *)(mmr_base + 0xb050));96}97}9899/* load mmr contents into a variable */100#define tioce_mmr_load(kern, mmrp, varp) do {\101tioce_mmr_war_pre(kern, mmrp); \102*(varp) = readq_relaxed(mmrp); \103tioce_mmr_war_post(kern, mmrp); \104} while (0)105106/* store variable contents into mmr */107#define tioce_mmr_store(kern, mmrp, varp) do {\108tioce_mmr_war_pre(kern, mmrp); \109writeq(*varp, mmrp); \110tioce_mmr_war_post(kern, mmrp); \111} while (0)112113/* store immediate value into mmr */114#define tioce_mmr_storei(kern, mmrp, val) do {\115tioce_mmr_war_pre(kern, mmrp); \116writeq(val, mmrp); \117tioce_mmr_war_post(kern, mmrp); \118} while (0)119120/* set bits (immediate value) into mmr */121#define tioce_mmr_seti(kern, mmrp, bits) do {\122u64 tmp; \123tioce_mmr_load(kern, mmrp, &tmp); \124tmp |= (bits); \125tioce_mmr_store(kern, mmrp, &tmp); \126} while (0)127128/* clear bits (immediate value) into mmr */129#define tioce_mmr_clri(kern, mmrp, bits) do { \130u64 tmp; \131tioce_mmr_load(kern, mmrp, &tmp); \132tmp &= ~(bits); \133tioce_mmr_store(kern, mmrp, &tmp); \134} while (0)135136/**137* Bus address ranges for the 5 flavors of TIOCE DMA138*/139140#define TIOCE_D64_MIN 0x8000000000000000UL141#define TIOCE_D64_MAX 0xffffffffffffffffUL142#define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)143144#define TIOCE_D32_MIN 0x0000000080000000UL145#define TIOCE_D32_MAX 0x00000000ffffffffUL146#define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)147148#define TIOCE_M32_MIN 0x0000000000000000UL149#define TIOCE_M32_MAX 0x000000007fffffffUL150#define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)151152#define TIOCE_M40_MIN 0x0000004000000000UL153#define TIOCE_M40_MAX 0x0000007fffffffffUL154#define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)155156#define TIOCE_M40S_MIN 0x0000008000000000UL157#define TIOCE_M40S_MAX 0x000000ffffffffffUL158#define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)159160/*161* ATE manipulation macros.162*/163164#define ATE_PAGESHIFT(ps) (__ffs(ps))165#define ATE_PAGEMASK(ps) ((ps)-1)166167#define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))168#define ATE_NPAGES(start, len, pagesize) \169(ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)170171#define ATE_VALID(ate) ((ate) & (1UL << 63))172#define ATE_MAKE(addr, ps, msi) \173(((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63) | ((msi)?(1UL << 62):0))174175/*176* Flavors of ate-based mapping supported by tioce_alloc_map()177*/178179#define TIOCE_ATE_M32 1180#define TIOCE_ATE_M40 2181#define TIOCE_ATE_M40S 3182183#define KB(x) ((u64)(x) << 10)184#define MB(x) ((u64)(x) << 20)185#define GB(x) ((u64)(x) << 30)186187/**188* tioce_dma_d64 - create a DMA mapping using 64-bit direct mode189* @ct_addr: system coretalk address190*191* Map @ct_addr into 64-bit CE bus space. No device context is necessary192* and no CE mapping are consumed.193*194* Bits 53:0 come from the coretalk address. The remaining bits are set as195* follows:196*197* 63 - must be 1 to indicate d64 mode to CE hardware198* 62 - barrier bit ... controlled with tioce_dma_barrier()199* 61 - msi bit ... specified through dma_flags200* 60:54 - reserved, MBZ201*/202static u64203tioce_dma_d64(unsigned long ct_addr, int dma_flags)204{205u64 bus_addr;206207bus_addr = ct_addr | (1UL << 63);208if (dma_flags & SN_DMA_MSI)209bus_addr |= (1UL << 61);210211return bus_addr;212}213214/**215* pcidev_to_tioce - return misc ce related pointers given a pci_dev216* @pci_dev: pci device context217* @base: ptr to store struct tioce_mmr * for the CE holding this device218* @kernel: ptr to store struct tioce_kernel * for the CE holding this device219* @port: ptr to store the CE port number that this device is on220*221* Return pointers to various CE-related structures for the CE upstream of222* @pci_dev.223*/224static inline void225pcidev_to_tioce(struct pci_dev *pdev, struct tioce __iomem **base,226struct tioce_kernel **kernel, int *port)227{228struct pcidev_info *pcidev_info;229struct tioce_common *ce_common;230struct tioce_kernel *ce_kernel;231232pcidev_info = SN_PCIDEV_INFO(pdev);233ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;234ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private;235236if (base)237*base = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;238if (kernel)239*kernel = ce_kernel;240241/*242* we use port as a zero-based value internally, even though the243* documentation is 1-based.244*/245if (port)246*port =247(pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;248}249250/**251* tioce_alloc_map - Given a coretalk address, map it to pcie bus address252* space using one of the various ATE-based address modes.253* @ce_kern: tioce context254* @type: map mode to use255* @port: 0-based port that the requesting device is downstream of256* @ct_addr: the coretalk address to map257* @len: number of bytes to map258*259* Given the addressing type, set up various parameters that define the260* ATE pool to use. Search for a contiguous block of entries to cover the261* length, and if enough resources exist, fill in the ATEs and construct a262* tioce_dmamap struct to track the mapping.263*/264static u64265tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port,266u64 ct_addr, int len, int dma_flags)267{268int i;269int j;270int first;271int last;272int entries;273int nates;274u64 pagesize;275int msi_capable, msi_wanted;276u64 *ate_shadow;277u64 __iomem *ate_reg;278u64 addr;279struct tioce __iomem *ce_mmr;280u64 bus_base;281struct tioce_dmamap *map;282283ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;284285switch (type) {286case TIOCE_ATE_M32:287/*288* The first 64 entries of the ate3240 pool are dedicated to289* super-page (TIOCE_ATE_M40S) mode.290*/291first = 64;292entries = TIOCE_NUM_M3240_ATES - 64;293ate_shadow = ce_kern->ce_ate3240_shadow;294ate_reg = ce_mmr->ce_ure_ate3240;295pagesize = ce_kern->ce_ate3240_pagesize;296bus_base = TIOCE_M32_MIN;297msi_capable = 1;298break;299case TIOCE_ATE_M40:300first = 0;301entries = TIOCE_NUM_M40_ATES;302ate_shadow = ce_kern->ce_ate40_shadow;303ate_reg = ce_mmr->ce_ure_ate40;304pagesize = MB(64);305bus_base = TIOCE_M40_MIN;306msi_capable = 0;307break;308case TIOCE_ATE_M40S:309/*310* ate3240 entries 0-31 are dedicated to port1 super-page311* mappings. ate3240 entries 32-63 are dedicated to port2.312*/313first = port * 32;314entries = 32;315ate_shadow = ce_kern->ce_ate3240_shadow;316ate_reg = ce_mmr->ce_ure_ate3240;317pagesize = GB(16);318bus_base = TIOCE_M40S_MIN;319msi_capable = 0;320break;321default:322return 0;323}324325msi_wanted = dma_flags & SN_DMA_MSI;326if (msi_wanted && !msi_capable)327return 0;328329nates = ATE_NPAGES(ct_addr, len, pagesize);330if (nates > entries)331return 0;332333last = first + entries - nates;334for (i = first; i <= last; i++) {335if (ATE_VALID(ate_shadow[i]))336continue;337338for (j = i; j < i + nates; j++)339if (ATE_VALID(ate_shadow[j]))340break;341342if (j >= i + nates)343break;344}345346if (i > last)347return 0;348349map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC);350if (!map)351return 0;352353addr = ct_addr;354for (j = 0; j < nates; j++) {355u64 ate;356357ate = ATE_MAKE(addr, pagesize, msi_wanted);358ate_shadow[i + j] = ate;359tioce_mmr_storei(ce_kern, &ate_reg[i + j], ate);360addr += pagesize;361}362363map->refcnt = 1;364map->nbytes = nates * pagesize;365map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize);366map->pci_start = bus_base + (i * pagesize);367map->ate_hw = &ate_reg[i];368map->ate_shadow = &ate_shadow[i];369map->ate_count = nates;370371list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list);372373return (map->pci_start + (ct_addr - map->ct_start));374}375376/**377* tioce_dma_d32 - create a DMA mapping using 32-bit direct mode378* @pdev: linux pci_dev representing the function379* @paddr: system physical address380*381* Map @paddr into 32-bit bus space of the CE associated with @pcidev_info.382*/383static u64384tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr, int dma_flags)385{386int dma_ok;387int port;388struct tioce __iomem *ce_mmr;389struct tioce_kernel *ce_kern;390u64 ct_upper;391u64 ct_lower;392dma_addr_t bus_addr;393394if (dma_flags & SN_DMA_MSI)395return 0;396397ct_upper = ct_addr & ~0x3fffffffUL;398ct_lower = ct_addr & 0x3fffffffUL;399400pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);401402if (ce_kern->ce_port[port].dirmap_refcnt == 0) {403u64 tmp;404405ce_kern->ce_port[port].dirmap_shadow = ct_upper;406tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],407ct_upper);408tmp = ce_mmr->ce_ure_dir_map[port];409dma_ok = 1;410} else411dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper);412413if (dma_ok) {414ce_kern->ce_port[port].dirmap_refcnt++;415bus_addr = TIOCE_D32_MIN + ct_lower;416} else417bus_addr = 0;418419return bus_addr;420}421422/**423* tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude424* the barrier bit.425* @bus_addr: bus address to swizzle426*427* Given a TIOCE bus address, set the appropriate bit to indicate barrier428* attributes.429*/430static u64431tioce_dma_barrier(u64 bus_addr, int on)432{433u64 barrier_bit;434435/* barrier not supported in M40/M40S mode */436if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr))437return bus_addr;438439if (TIOCE_D64_ADDR(bus_addr))440barrier_bit = (1UL << 62);441else /* must be m32 or d32 */442barrier_bit = (1UL << 30);443444return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);445}446447/**448* tioce_dma_unmap - release CE mapping resources449* @pdev: linux pci_dev representing the function450* @bus_addr: bus address returned by an earlier tioce_dma_map451* @dir: mapping direction (unused)452*453* Locate mapping resources associated with @bus_addr and release them.454* For mappings created using the direct modes there are no resources455* to release.456*/457void458tioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir)459{460int i;461int port;462struct tioce_kernel *ce_kern;463struct tioce __iomem *ce_mmr;464unsigned long flags;465466bus_addr = tioce_dma_barrier(bus_addr, 0);467pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);468469/* nothing to do for D64 */470471if (TIOCE_D64_ADDR(bus_addr))472return;473474spin_lock_irqsave(&ce_kern->ce_lock, flags);475476if (TIOCE_D32_ADDR(bus_addr)) {477if (--ce_kern->ce_port[port].dirmap_refcnt == 0) {478ce_kern->ce_port[port].dirmap_shadow = 0;479tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],4800);481}482} else {483struct tioce_dmamap *map;484485list_for_each_entry(map, &ce_kern->ce_dmamap_list,486ce_dmamap_list) {487u64 last;488489last = map->pci_start + map->nbytes - 1;490if (bus_addr >= map->pci_start && bus_addr <= last)491break;492}493494if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) {495printk(KERN_WARNING496"%s: %s - no map found for bus_addr 0x%llx\n",497__func__, pci_name(pdev), bus_addr);498} else if (--map->refcnt == 0) {499for (i = 0; i < map->ate_count; i++) {500map->ate_shadow[i] = 0;501tioce_mmr_storei(ce_kern, &map->ate_hw[i], 0);502}503504list_del(&map->ce_dmamap_list);505kfree(map);506}507}508509spin_unlock_irqrestore(&ce_kern->ce_lock, flags);510}511512/**513* tioce_do_dma_map - map pages for PCI DMA514* @pdev: linux pci_dev representing the function515* @paddr: host physical address to map516* @byte_count: bytes to map517*518* This is the main wrapper for mapping host physical pages to CE PCI space.519* The mapping mode used is based on the device's dma_mask.520*/521static u64522tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count,523int barrier, int dma_flags)524{525unsigned long flags;526u64 ct_addr;527u64 mapaddr = 0;528struct tioce_kernel *ce_kern;529struct tioce_dmamap *map;530int port;531u64 dma_mask;532533dma_mask = (barrier) ? pdev->dev.coherent_dma_mask : pdev->dma_mask;534535/* cards must be able to address at least 31 bits */536if (dma_mask < 0x7fffffffUL)537return 0;538539if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)540ct_addr = PHYS_TO_TIODMA(paddr);541else542ct_addr = paddr;543544/*545* If the device can generate 64 bit addresses, create a D64 map.546*/547if (dma_mask == ~0UL) {548mapaddr = tioce_dma_d64(ct_addr, dma_flags);549if (mapaddr)550goto dma_map_done;551}552553pcidev_to_tioce(pdev, NULL, &ce_kern, &port);554555spin_lock_irqsave(&ce_kern->ce_lock, flags);556557/*558* D64 didn't work ... See if we have an existing map that covers559* this address range. Must account for devices dma_mask here since560* an existing map might have been done in a mode using more pci561* address bits than this device can support.562*/563list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) {564u64 last;565566last = map->ct_start + map->nbytes - 1;567if (ct_addr >= map->ct_start &&568ct_addr + byte_count - 1 <= last &&569map->pci_start <= dma_mask) {570map->refcnt++;571mapaddr = map->pci_start + (ct_addr - map->ct_start);572break;573}574}575576/*577* If we don't have a map yet, and the card can generate 40578* bit addresses, try the M40/M40S modes. Note these modes do not579* support a barrier bit, so if we need a consistent map these580* won't work.581*/582if (!mapaddr && !barrier && dma_mask >= 0xffffffffffUL) {583/*584* We have two options for 40-bit mappings: 16GB "super" ATEs585* and 64MB "regular" ATEs. We'll try both if needed for a586* given mapping but which one we try first depends on the587* size. For requests >64MB, prefer to use a super page with588* regular as the fallback. Otherwise, try in the reverse order.589*/590591if (byte_count > MB(64)) {592mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,593port, ct_addr, byte_count,594dma_flags);595if (!mapaddr)596mapaddr =597tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,598ct_addr, byte_count,599dma_flags);600} else {601mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,602ct_addr, byte_count,603dma_flags);604if (!mapaddr)605mapaddr =606tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,607port, ct_addr, byte_count,608dma_flags);609}610}611612/*613* 32-bit direct is the next mode to try614*/615if (!mapaddr && dma_mask >= 0xffffffffUL)616mapaddr = tioce_dma_d32(pdev, ct_addr, dma_flags);617618/*619* Last resort, try 32-bit ATE-based map.620*/621if (!mapaddr)622mapaddr =623tioce_alloc_map(ce_kern, TIOCE_ATE_M32, -1, ct_addr,624byte_count, dma_flags);625626spin_unlock_irqrestore(&ce_kern->ce_lock, flags);627628dma_map_done:629if (mapaddr && barrier)630mapaddr = tioce_dma_barrier(mapaddr, 1);631632return mapaddr;633}634635/**636* tioce_dma - standard pci dma map interface637* @pdev: pci device requesting the map638* @paddr: system physical address to map into pci space639* @byte_count: # bytes to map640*641* Simply call tioce_do_dma_map() to create a map with the barrier bit clear642* in the address.643*/644static u64645tioce_dma(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)646{647return tioce_do_dma_map(pdev, paddr, byte_count, 0, dma_flags);648}649650/**651* tioce_dma_consistent - consistent pci dma map interface652* @pdev: pci device requesting the map653* @paddr: system physical address to map into pci space654* @byte_count: # bytes to map655*656* Simply call tioce_do_dma_map() to create a map with the barrier bit set657* in the address.658*/659static u64660tioce_dma_consistent(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)661{662return tioce_do_dma_map(pdev, paddr, byte_count, 1, dma_flags);663}664665/**666* tioce_error_intr_handler - SGI TIO CE error interrupt handler667* @irq: unused668* @arg: pointer to tioce_common struct for the given CE669*670* Handle a CE error interrupt. Simply a wrapper around a SAL call which671* defers processing to the SGI prom.672*/673static irqreturn_t674tioce_error_intr_handler(int irq, void *arg)675{676struct tioce_common *soft = arg;677struct ia64_sal_retval ret_stuff;678ret_stuff.status = 0;679ret_stuff.v0 = 0;680681SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_ERROR_INTERRUPT,682soft->ce_pcibus.bs_persist_segment,683soft->ce_pcibus.bs_persist_busnum, 0, 0, 0, 0, 0);684685if (ret_stuff.v0)686panic("tioce_error_intr_handler: Fatal TIOCE error");687688return IRQ_HANDLED;689}690691/**692* tioce_reserve_m32 - reserve M32 ATEs for the indicated address range693* @tioce_kernel: TIOCE context to reserve ATEs for694* @base: starting bus address to reserve695* @limit: last bus address to reserve696*697* If base/limit falls within the range of bus space mapped through the698* M32 space, reserve the resources corresponding to the range.699*/700static void701tioce_reserve_m32(struct tioce_kernel *ce_kern, u64 base, u64 limit)702{703int ate_index, last_ate, ps;704struct tioce __iomem *ce_mmr;705706ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;707ps = ce_kern->ce_ate3240_pagesize;708ate_index = ATE_PAGE(base, ps);709last_ate = ate_index + ATE_NPAGES(base, limit-base+1, ps) - 1;710711if (ate_index < 64)712ate_index = 64;713714if (last_ate >= TIOCE_NUM_M3240_ATES)715last_ate = TIOCE_NUM_M3240_ATES - 1;716717while (ate_index <= last_ate) {718u64 ate;719720ate = ATE_MAKE(0xdeadbeef, ps, 0);721ce_kern->ce_ate3240_shadow[ate_index] = ate;722tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_ate3240[ate_index],723ate);724ate_index++;725}726}727728/**729* tioce_kern_init - init kernel structures related to a given TIOCE730* @tioce_common: ptr to a cached tioce_common struct that originated in prom731*/732static struct tioce_kernel *733tioce_kern_init(struct tioce_common *tioce_common)734{735int i;736int ps;737int dev;738u32 tmp;739unsigned int seg, bus;740struct tioce __iomem *tioce_mmr;741struct tioce_kernel *tioce_kern;742743tioce_kern = kzalloc(sizeof(struct tioce_kernel), GFP_KERNEL);744if (!tioce_kern) {745return NULL;746}747748tioce_kern->ce_common = tioce_common;749spin_lock_init(&tioce_kern->ce_lock);750INIT_LIST_HEAD(&tioce_kern->ce_dmamap_list);751tioce_common->ce_kernel_private = (u64) tioce_kern;752753/*754* Determine the secondary bus number of the port2 logical PPB.755* This is used to decide whether a given pci device resides on756* port1 or port2. Note: We don't have enough plumbing set up757* here to use pci_read_config_xxx() so use raw_pci_read().758*/759760seg = tioce_common->ce_pcibus.bs_persist_segment;761bus = tioce_common->ce_pcibus.bs_persist_busnum;762763raw_pci_read(seg, bus, PCI_DEVFN(2, 0), PCI_SECONDARY_BUS, 1,&tmp);764tioce_kern->ce_port1_secondary = (u8) tmp;765766/*767* Set PMU pagesize to the largest size available, and zero out768* the ATEs.769*/770771tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;772tioce_mmr_clri(tioce_kern, &tioce_mmr->ce_ure_page_map,773CE_URE_PAGESIZE_MASK);774tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_ure_page_map,775CE_URE_256K_PAGESIZE);776ps = tioce_kern->ce_ate3240_pagesize = KB(256);777778for (i = 0; i < TIOCE_NUM_M40_ATES; i++) {779tioce_kern->ce_ate40_shadow[i] = 0;780tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate40[i], 0);781}782783for (i = 0; i < TIOCE_NUM_M3240_ATES; i++) {784tioce_kern->ce_ate3240_shadow[i] = 0;785tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate3240[i], 0);786}787788/*789* Reserve ATEs corresponding to reserved address ranges. These790* include:791*792* Memory space covered by each PPB mem base/limit register793* Memory space covered by each PPB prefetch base/limit register794*795* These bus ranges are for pio (downstream) traffic only, and so796* cannot be used for DMA.797*/798799for (dev = 1; dev <= 2; dev++) {800u64 base, limit;801802/* mem base/limit */803804raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),805PCI_MEMORY_BASE, 2, &tmp);806base = (u64)tmp << 16;807808raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),809PCI_MEMORY_LIMIT, 2, &tmp);810limit = (u64)tmp << 16;811limit |= 0xfffffUL;812813if (base < limit)814tioce_reserve_m32(tioce_kern, base, limit);815816/*817* prefetch mem base/limit. The tioce ppb's have 64-bit818* decoders, so read the upper portions w/o checking the819* attributes.820*/821822raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),823PCI_PREF_MEMORY_BASE, 2, &tmp);824base = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;825826raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),827PCI_PREF_BASE_UPPER32, 4, &tmp);828base |= (u64)tmp << 32;829830raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),831PCI_PREF_MEMORY_LIMIT, 2, &tmp);832833limit = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;834limit |= 0xfffffUL;835836raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),837PCI_PREF_LIMIT_UPPER32, 4, &tmp);838limit |= (u64)tmp << 32;839840if ((base < limit) && TIOCE_M32_ADDR(base))841tioce_reserve_m32(tioce_kern, base, limit);842}843844return tioce_kern;845}846847/**848* tioce_force_interrupt - implement altix force_interrupt() backend for CE849* @sn_irq_info: sn asic irq that we need an interrupt generated for850*851* Given an sn_irq_info struct, set the proper bit in ce_adm_force_int to852* force a secondary interrupt to be generated. This is to work around an853* asic issue where there is a small window of opportunity for a legacy device854* interrupt to be lost.855*/856static void857tioce_force_interrupt(struct sn_irq_info *sn_irq_info)858{859struct pcidev_info *pcidev_info;860struct tioce_common *ce_common;861struct tioce_kernel *ce_kern;862struct tioce __iomem *ce_mmr;863u64 force_int_val;864865if (!sn_irq_info->irq_bridge)866return;867868if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_TIOCE)869return;870871pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;872if (!pcidev_info)873return;874875ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;876ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;877ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;878879/*880* TIOCE Rev A workaround (PV 945826), force an interrupt by writing881* the TIO_INTx register directly (1/26/2006)882*/883if (ce_common->ce_rev == TIOCE_REV_A) {884u64 int_bit_mask = (1ULL << sn_irq_info->irq_int_bit);885u64 status;886887tioce_mmr_load(ce_kern, &ce_mmr->ce_adm_int_status, &status);888if (status & int_bit_mask) {889u64 force_irq = (1 << 8) | sn_irq_info->irq_irq;890u64 ctalk = sn_irq_info->irq_xtalkaddr;891u64 nasid, offset;892893nasid = (ctalk & CTALK_NASID_MASK) >> CTALK_NASID_SHFT;894offset = (ctalk & CTALK_NODE_OFFSET);895HUB_S(TIO_IOSPACE_ADDR(nasid, offset), force_irq);896}897898return;899}900901/*902* irq_int_bit is originally set up by prom, and holds the interrupt903* bit shift (not mask) as defined by the bit definitions in the904* ce_adm_int mmr. These shifts are not the same for the905* ce_adm_force_int register, so do an explicit mapping here to make906* things clearer.907*/908909switch (sn_irq_info->irq_int_bit) {910case CE_ADM_INT_PCIE_PORT1_DEV_A_SHFT:911force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_A_SHFT;912break;913case CE_ADM_INT_PCIE_PORT1_DEV_B_SHFT:914force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_B_SHFT;915break;916case CE_ADM_INT_PCIE_PORT1_DEV_C_SHFT:917force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_C_SHFT;918break;919case CE_ADM_INT_PCIE_PORT1_DEV_D_SHFT:920force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_D_SHFT;921break;922case CE_ADM_INT_PCIE_PORT2_DEV_A_SHFT:923force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_A_SHFT;924break;925case CE_ADM_INT_PCIE_PORT2_DEV_B_SHFT:926force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_B_SHFT;927break;928case CE_ADM_INT_PCIE_PORT2_DEV_C_SHFT:929force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_C_SHFT;930break;931case CE_ADM_INT_PCIE_PORT2_DEV_D_SHFT:932force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_D_SHFT;933break;934default:935return;936}937tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_force_int, force_int_val);938}939940/**941* tioce_target_interrupt - implement set_irq_affinity for tioce resident942* functions. Note: only applies to line interrupts, not MSI's.943*944* @sn_irq_info: SN IRQ context945*946* Given an sn_irq_info, set the associated CE device's interrupt destination947* register. Since the interrupt destination registers are on a per-ce-slot948* basis, this will retarget line interrupts for all functions downstream of949* the slot.950*/951static void952tioce_target_interrupt(struct sn_irq_info *sn_irq_info)953{954struct pcidev_info *pcidev_info;955struct tioce_common *ce_common;956struct tioce_kernel *ce_kern;957struct tioce __iomem *ce_mmr;958int bit;959u64 vector;960961pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;962if (!pcidev_info)963return;964965ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;966ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;967ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;968969bit = sn_irq_info->irq_int_bit;970971tioce_mmr_seti(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));972vector = (u64)sn_irq_info->irq_irq << INTR_VECTOR_SHFT;973vector |= sn_irq_info->irq_xtalkaddr;974tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_int_dest[bit], vector);975tioce_mmr_clri(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));976977tioce_force_interrupt(sn_irq_info);978}979980/**981* tioce_bus_fixup - perform final PCI fixup for a TIO CE bus982* @prom_bussoft: Common prom/kernel struct representing the bus983*984* Replicates the tioce_common pointed to by @prom_bussoft in kernel985* space. Allocates and initializes a kernel-only area for a given CE,986* and sets up an irq for handling CE error interrupts.987*988* On successful setup, returns the kernel version of tioce_common back to989* the caller.990*/991static void *992tioce_bus_fixup(struct pcibus_bussoft *prom_bussoft, struct pci_controller *controller)993{994struct tioce_common *tioce_common;995struct tioce_kernel *tioce_kern;996struct tioce __iomem *tioce_mmr;997998/*999* Allocate kernel bus soft and copy from prom.1000*/10011002tioce_common = kzalloc(sizeof(struct tioce_common), GFP_KERNEL);1003if (!tioce_common)1004return NULL;10051006memcpy(tioce_common, prom_bussoft, sizeof(struct tioce_common));1007tioce_common->ce_pcibus.bs_base = (unsigned long)1008ioremap(REGION_OFFSET(tioce_common->ce_pcibus.bs_base),1009sizeof(struct tioce_common));10101011tioce_kern = tioce_kern_init(tioce_common);1012if (tioce_kern == NULL) {1013kfree(tioce_common);1014return NULL;1015}10161017/*1018* Clear out any transient errors before registering the error1019* interrupt handler.1020*/10211022tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;1023tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_int_status_alias, ~0ULL);1024tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_error_summary_alias,1025~0ULL);1026tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_dre_comp_err_addr, 0ULL);10271028if (request_irq(SGI_PCIASIC_ERROR,1029tioce_error_intr_handler,1030IRQF_SHARED, "TIOCE error", (void *)tioce_common))1031printk(KERN_WARNING1032"%s: Unable to get irq %d. "1033"Error interrupts won't be routed for "1034"TIOCE bus %04x:%02x\n",1035__func__, SGI_PCIASIC_ERROR,1036tioce_common->ce_pcibus.bs_persist_segment,1037tioce_common->ce_pcibus.bs_persist_busnum);10381039sn_set_err_irq_affinity(SGI_PCIASIC_ERROR);1040return tioce_common;1041}10421043static struct sn_pcibus_provider tioce_pci_interfaces = {1044.dma_map = tioce_dma,1045.dma_map_consistent = tioce_dma_consistent,1046.dma_unmap = tioce_dma_unmap,1047.bus_fixup = tioce_bus_fixup,1048.force_interrupt = tioce_force_interrupt,1049.target_interrupt = tioce_target_interrupt1050};10511052/**1053* tioce_init_provider - init SN PCI provider ops for TIO CE1054*/1055int1056tioce_init_provider(void)1057{1058sn_pci_provider[PCIIO_ASIC_TYPE_TIOCE] = &tioce_pci_interfaces;1059return 0;1060}106110621063