18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci**  System Bus Adapter (SBA) I/O MMU manager
48c2ecf20Sopenharmony_ci**
58c2ecf20Sopenharmony_ci**	(c) Copyright 2000-2004 Grant Grundler <grundler @ parisc-linux x org>
68c2ecf20Sopenharmony_ci**	(c) Copyright 2004 Naresh Kumar Inna <knaresh at india x hp x com>
78c2ecf20Sopenharmony_ci**	(c) Copyright 2000-2004 Hewlett-Packard Company
88c2ecf20Sopenharmony_ci**
98c2ecf20Sopenharmony_ci**	Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)
108c2ecf20Sopenharmony_ci**
118c2ecf20Sopenharmony_ci**
128c2ecf20Sopenharmony_ci**
138c2ecf20Sopenharmony_ci** This module initializes the IOC (I/O Controller) found on B1000/C3000/
148c2ecf20Sopenharmony_ci** J5000/J7000/N-class/L-class machines and their successors.
158c2ecf20Sopenharmony_ci**
168c2ecf20Sopenharmony_ci** FIXME: add DMA hint support programming in both sba and lba modules.
178c2ecf20Sopenharmony_ci*/
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/types.h>
208c2ecf20Sopenharmony_ci#include <linux/kernel.h>
218c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <linux/init.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/mm.h>
268c2ecf20Sopenharmony_ci#include <linux/string.h>
278c2ecf20Sopenharmony_ci#include <linux/pci.h>
288c2ecf20Sopenharmony_ci#include <linux/dma-map-ops.h>
298c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
308c2ecf20Sopenharmony_ci#include <linux/iommu-helper.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
338c2ecf20Sopenharmony_ci#include <asm/io.h>
348c2ecf20Sopenharmony_ci#include <asm/dma.h>		/* for DMA_CHUNK_SIZE */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <asm/hardware.h>	/* for register_parisc_driver() stuff */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
398c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
408c2ecf20Sopenharmony_ci#include <linux/module.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#include <asm/ropes.h>
438c2ecf20Sopenharmony_ci#include <asm/mckinley.h>	/* for proc_mckinley_root */
448c2ecf20Sopenharmony_ci#include <asm/runway.h>		/* for proc_runway_root */
458c2ecf20Sopenharmony_ci#include <asm/page.h>		/* for PAGE0 */
468c2ecf20Sopenharmony_ci#include <asm/pdc.h>		/* for PDC_MODEL_* */
478c2ecf20Sopenharmony_ci#include <asm/pdcpat.h>		/* for is_pdc_pat() */
488c2ecf20Sopenharmony_ci#include <asm/parisc-device.h>
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#include "iommu.h"
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define MODULE_NAME "SBA"
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci** The number of debug flags is a clue - this code is fragile.
568c2ecf20Sopenharmony_ci** Don't even think about messing with it unless you have
578c2ecf20Sopenharmony_ci** plenty of 710's to sacrifice to the computer gods. :^)
588c2ecf20Sopenharmony_ci*/
598c2ecf20Sopenharmony_ci#undef DEBUG_SBA_INIT
608c2ecf20Sopenharmony_ci#undef DEBUG_SBA_RUN
618c2ecf20Sopenharmony_ci#undef DEBUG_SBA_RUN_SG
628c2ecf20Sopenharmony_ci#undef DEBUG_SBA_RESOURCE
638c2ecf20Sopenharmony_ci#undef ASSERT_PDIR_SANITY
648c2ecf20Sopenharmony_ci#undef DEBUG_LARGE_SG_ENTRIES
658c2ecf20Sopenharmony_ci#undef DEBUG_DMB_TRAP
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_INIT
688c2ecf20Sopenharmony_ci#define DBG_INIT(x...)	printk(x)
698c2ecf20Sopenharmony_ci#else
708c2ecf20Sopenharmony_ci#define DBG_INIT(x...)
718c2ecf20Sopenharmony_ci#endif
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_RUN
748c2ecf20Sopenharmony_ci#define DBG_RUN(x...)	printk(x)
758c2ecf20Sopenharmony_ci#else
768c2ecf20Sopenharmony_ci#define DBG_RUN(x...)
778c2ecf20Sopenharmony_ci#endif
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_RUN_SG
808c2ecf20Sopenharmony_ci#define DBG_RUN_SG(x...)	printk(x)
818c2ecf20Sopenharmony_ci#else
828c2ecf20Sopenharmony_ci#define DBG_RUN_SG(x...)
838c2ecf20Sopenharmony_ci#endif
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_RESOURCE
878c2ecf20Sopenharmony_ci#define DBG_RES(x...)	printk(x)
888c2ecf20Sopenharmony_ci#else
898c2ecf20Sopenharmony_ci#define DBG_RES(x...)
908c2ecf20Sopenharmony_ci#endif
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define SBA_INLINE	__inline__
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define DEFAULT_DMA_HINT_REG	0
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct sba_device *sba_list;
978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sba_list);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic unsigned long ioc_needs_fdc = 0;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* global count of IOMMUs in the system */
1028c2ecf20Sopenharmony_cistatic unsigned int global_ioc_cnt = 0;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* PA8700 (Piranha 2.2) bug workaround */
1058c2ecf20Sopenharmony_cistatic unsigned long piranha_bad_128k = 0;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* Looks nice and keeps the compiler happy */
1088c2ecf20Sopenharmony_ci#define SBA_DEV(d) ((struct sba_device *) (d))
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#ifdef CONFIG_AGP_PARISC
1118c2ecf20Sopenharmony_ci#define SBA_AGP_SUPPORT
1128c2ecf20Sopenharmony_ci#endif /*CONFIG_AGP_PARISC*/
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#ifdef SBA_AGP_SUPPORT
1158c2ecf20Sopenharmony_cistatic int sba_reserve_agpgart = 1;
1168c2ecf20Sopenharmony_cimodule_param(sba_reserve_agpgart, int, 0444);
1178c2ecf20Sopenharmony_ciMODULE_PARM_DESC(sba_reserve_agpgart, "Reserve half of IO pdir as AGPGART");
1188c2ecf20Sopenharmony_ci#endif
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/************************************
1228c2ecf20Sopenharmony_ci** SBA register read and write support
1238c2ecf20Sopenharmony_ci**
1248c2ecf20Sopenharmony_ci** BE WARNED: register writes are posted.
1258c2ecf20Sopenharmony_ci**  (ie follow writes which must reach HW with a read)
1268c2ecf20Sopenharmony_ci**
1278c2ecf20Sopenharmony_ci** Superdome (in particular, REO) allows only 64-bit CSR accesses.
1288c2ecf20Sopenharmony_ci*/
1298c2ecf20Sopenharmony_ci#define READ_REG32(addr)	readl(addr)
1308c2ecf20Sopenharmony_ci#define READ_REG64(addr)	readq(addr)
1318c2ecf20Sopenharmony_ci#define WRITE_REG32(val, addr)	writel((val), (addr))
1328c2ecf20Sopenharmony_ci#define WRITE_REG64(val, addr)	writeq((val), (addr))
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
1358c2ecf20Sopenharmony_ci#define READ_REG(addr)		READ_REG64(addr)
1368c2ecf20Sopenharmony_ci#define WRITE_REG(value, addr)	WRITE_REG64(value, addr)
1378c2ecf20Sopenharmony_ci#else
1388c2ecf20Sopenharmony_ci#define READ_REG(addr)		READ_REG32(addr)
1398c2ecf20Sopenharmony_ci#define WRITE_REG(value, addr)	WRITE_REG32(value, addr)
1408c2ecf20Sopenharmony_ci#endif
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_INIT
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* NOTE: When CONFIG_64BIT isn't defined, READ_REG64() is two 32-bit reads */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/**
1478c2ecf20Sopenharmony_ci * sba_dump_ranges - debugging only - print ranges assigned to this IOA
1488c2ecf20Sopenharmony_ci * @hpa: base address of the sba
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * Print the MMIO and IO Port address ranges forwarded by an Astro/Ike/RIO
1518c2ecf20Sopenharmony_ci * IO Adapter (aka Bus Converter).
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_cistatic void
1548c2ecf20Sopenharmony_cisba_dump_ranges(void __iomem *hpa)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	DBG_INIT("SBA at 0x%p\n", hpa);
1578c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIST_BASE   : %Lx\n", READ_REG64(hpa+IOS_DIST_BASE));
1588c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIST_MASK   : %Lx\n", READ_REG64(hpa+IOS_DIST_MASK));
1598c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIST_ROUTE  : %Lx\n", READ_REG64(hpa+IOS_DIST_ROUTE));
1608c2ecf20Sopenharmony_ci	DBG_INIT("\n");
1618c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIRECT_BASE : %Lx\n", READ_REG64(hpa+IOS_DIRECT_BASE));
1628c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIRECT_MASK : %Lx\n", READ_REG64(hpa+IOS_DIRECT_MASK));
1638c2ecf20Sopenharmony_ci	DBG_INIT("IOS_DIRECT_ROUTE: %Lx\n", READ_REG64(hpa+IOS_DIRECT_ROUTE));
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/**
1678c2ecf20Sopenharmony_ci * sba_dump_tlb - debugging only - print IOMMU operating parameters
1688c2ecf20Sopenharmony_ci * @hpa: base address of the IOMMU
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * Print the size/location of the IO MMU PDIR.
1718c2ecf20Sopenharmony_ci */
1728c2ecf20Sopenharmony_cistatic void sba_dump_tlb(void __iomem *hpa)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	DBG_INIT("IO TLB at 0x%p\n", hpa);
1758c2ecf20Sopenharmony_ci	DBG_INIT("IOC_IBASE    : 0x%Lx\n", READ_REG64(hpa+IOC_IBASE));
1768c2ecf20Sopenharmony_ci	DBG_INIT("IOC_IMASK    : 0x%Lx\n", READ_REG64(hpa+IOC_IMASK));
1778c2ecf20Sopenharmony_ci	DBG_INIT("IOC_TCNFG    : 0x%Lx\n", READ_REG64(hpa+IOC_TCNFG));
1788c2ecf20Sopenharmony_ci	DBG_INIT("IOC_PDIR_BASE: 0x%Lx\n", READ_REG64(hpa+IOC_PDIR_BASE));
1798c2ecf20Sopenharmony_ci	DBG_INIT("\n");
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci#else
1828c2ecf20Sopenharmony_ci#define sba_dump_ranges(x)
1838c2ecf20Sopenharmony_ci#define sba_dump_tlb(x)
1848c2ecf20Sopenharmony_ci#endif	/* DEBUG_SBA_INIT */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/**
1908c2ecf20Sopenharmony_ci * sba_dump_pdir_entry - debugging only - print one IOMMU PDIR entry
1918c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
1928c2ecf20Sopenharmony_ci * @msg: text to print ont the output line.
1938c2ecf20Sopenharmony_ci * @pide: pdir index.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * Print one entry of the IO MMU PDIR in human readable form.
1968c2ecf20Sopenharmony_ci */
1978c2ecf20Sopenharmony_cistatic void
1988c2ecf20Sopenharmony_cisba_dump_pdir_entry(struct ioc *ioc, char *msg, uint pide)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	/* start printing from lowest pde in rval */
2018c2ecf20Sopenharmony_ci	u64 *ptr = &(ioc->pdir_base[pide & (~0U * BITS_PER_LONG)]);
2028c2ecf20Sopenharmony_ci	unsigned long *rptr = (unsigned long *) &(ioc->res_map[(pide >>3) & ~(sizeof(unsigned long) - 1)]);
2038c2ecf20Sopenharmony_ci	uint rcnt;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "SBA: %s rp %p bit %d rval 0x%lx\n",
2068c2ecf20Sopenharmony_ci		 msg,
2078c2ecf20Sopenharmony_ci		 rptr, pide & (BITS_PER_LONG - 1), *rptr);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	rcnt = 0;
2108c2ecf20Sopenharmony_ci	while (rcnt < BITS_PER_LONG) {
2118c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "%s %2d %p %016Lx\n",
2128c2ecf20Sopenharmony_ci			(rcnt == (pide & (BITS_PER_LONG - 1)))
2138c2ecf20Sopenharmony_ci				? "    -->" : "       ",
2148c2ecf20Sopenharmony_ci			rcnt, ptr, *ptr );
2158c2ecf20Sopenharmony_ci		rcnt++;
2168c2ecf20Sopenharmony_ci		ptr++;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s", msg);
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/**
2238c2ecf20Sopenharmony_ci * sba_check_pdir - debugging only - consistency checker
2248c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
2258c2ecf20Sopenharmony_ci * @msg: text to print ont the output line.
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * Verify the resource map and pdir state is consistent
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_cistatic int
2308c2ecf20Sopenharmony_cisba_check_pdir(struct ioc *ioc, char *msg)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	u32 *rptr_end = (u32 *) &(ioc->res_map[ioc->res_size]);
2338c2ecf20Sopenharmony_ci	u32 *rptr = (u32 *) ioc->res_map;	/* resource map ptr */
2348c2ecf20Sopenharmony_ci	u64 *pptr = ioc->pdir_base;	/* pdir ptr */
2358c2ecf20Sopenharmony_ci	uint pide = 0;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	while (rptr < rptr_end) {
2388c2ecf20Sopenharmony_ci		u32 rval = *rptr;
2398c2ecf20Sopenharmony_ci		int rcnt = 32;	/* number of bits we might check */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		while (rcnt) {
2428c2ecf20Sopenharmony_ci			/* Get last byte and highest bit from that */
2438c2ecf20Sopenharmony_ci			u32 pde = ((u32) (((char *)pptr)[7])) << 24;
2448c2ecf20Sopenharmony_ci			if ((rval ^ pde) & 0x80000000)
2458c2ecf20Sopenharmony_ci			{
2468c2ecf20Sopenharmony_ci				/*
2478c2ecf20Sopenharmony_ci				** BUMMER!  -- res_map != pdir --
2488c2ecf20Sopenharmony_ci				** Dump rval and matching pdir entries
2498c2ecf20Sopenharmony_ci				*/
2508c2ecf20Sopenharmony_ci				sba_dump_pdir_entry(ioc, msg, pide);
2518c2ecf20Sopenharmony_ci				return(1);
2528c2ecf20Sopenharmony_ci			}
2538c2ecf20Sopenharmony_ci			rcnt--;
2548c2ecf20Sopenharmony_ci			rval <<= 1;	/* try the next bit */
2558c2ecf20Sopenharmony_ci			pptr++;
2568c2ecf20Sopenharmony_ci			pide++;
2578c2ecf20Sopenharmony_ci		}
2588c2ecf20Sopenharmony_ci		rptr++;	/* look at next word of res_map */
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci	/* It'd be nice if we always got here :^) */
2618c2ecf20Sopenharmony_ci	return 0;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci/**
2668c2ecf20Sopenharmony_ci * sba_dump_sg - debugging only - print Scatter-Gather list
2678c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
2688c2ecf20Sopenharmony_ci * @startsg: head of the SG list
2698c2ecf20Sopenharmony_ci * @nents: number of entries in SG list
2708c2ecf20Sopenharmony_ci *
2718c2ecf20Sopenharmony_ci * print the SG list so we can verify it's correct by hand.
2728c2ecf20Sopenharmony_ci */
2738c2ecf20Sopenharmony_cistatic void
2748c2ecf20Sopenharmony_cisba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	while (nents-- > 0) {
2778c2ecf20Sopenharmony_ci		printk(KERN_DEBUG " %d : %08lx/%05x %p/%05x\n",
2788c2ecf20Sopenharmony_ci				nents,
2798c2ecf20Sopenharmony_ci				(unsigned long) sg_dma_address(startsg),
2808c2ecf20Sopenharmony_ci				sg_dma_len(startsg),
2818c2ecf20Sopenharmony_ci				sg_virt(startsg), startsg->length);
2828c2ecf20Sopenharmony_ci		startsg++;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#endif /* ASSERT_PDIR_SANITY */
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/**************************************************************
2928c2ecf20Sopenharmony_ci*
2938c2ecf20Sopenharmony_ci*   I/O Pdir Resource Management
2948c2ecf20Sopenharmony_ci*
2958c2ecf20Sopenharmony_ci*   Bits set in the resource map are in use.
2968c2ecf20Sopenharmony_ci*   Each bit can represent a number of pages.
2978c2ecf20Sopenharmony_ci*   LSbs represent lower addresses (IOVA's).
2988c2ecf20Sopenharmony_ci*
2998c2ecf20Sopenharmony_ci***************************************************************/
3008c2ecf20Sopenharmony_ci#define PAGES_PER_RANGE 1	/* could increase this to 4 or 8 if needed */
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci/* Convert from IOVP to IOVA and vice versa. */
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci#ifdef ZX1_SUPPORT
3058c2ecf20Sopenharmony_ci/* Pluto (aka ZX1) boxes need to set or clear the ibase bits appropriately */
3068c2ecf20Sopenharmony_ci#define SBA_IOVA(ioc,iovp,offset,hint_reg) ((ioc->ibase) | (iovp) | (offset))
3078c2ecf20Sopenharmony_ci#define SBA_IOVP(ioc,iova) ((iova) & (ioc)->iovp_mask)
3088c2ecf20Sopenharmony_ci#else
3098c2ecf20Sopenharmony_ci/* only support Astro and ancestors. Saves a few cycles in key places */
3108c2ecf20Sopenharmony_ci#define SBA_IOVA(ioc,iovp,offset,hint_reg) ((iovp) | (offset))
3118c2ecf20Sopenharmony_ci#define SBA_IOVP(ioc,iova) (iova)
3128c2ecf20Sopenharmony_ci#endif
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci#define PDIR_INDEX(iovp)   ((iovp)>>IOVP_SHIFT)
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci#define RESMAP_MASK(n)    (~0UL << (BITS_PER_LONG - (n)))
3178c2ecf20Sopenharmony_ci#define RESMAP_IDX_MASK   (sizeof(unsigned long) - 1)
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic unsigned long ptr_to_pide(struct ioc *ioc, unsigned long *res_ptr,
3208c2ecf20Sopenharmony_ci				 unsigned int bitshiftcnt)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	return (((unsigned long)res_ptr - (unsigned long)ioc->res_map) << 3)
3238c2ecf20Sopenharmony_ci		+ bitshiftcnt;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci/**
3278c2ecf20Sopenharmony_ci * sba_search_bitmap - find free space in IO PDIR resource bitmap
3288c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
3298c2ecf20Sopenharmony_ci * @bits_wanted: number of entries we need.
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * Find consecutive free bits in resource bitmap.
3328c2ecf20Sopenharmony_ci * Each bit represents one entry in the IO Pdir.
3338c2ecf20Sopenharmony_ci * Cool perf optimization: search for log2(size) bits at a time.
3348c2ecf20Sopenharmony_ci */
3358c2ecf20Sopenharmony_cistatic SBA_INLINE unsigned long
3368c2ecf20Sopenharmony_cisba_search_bitmap(struct ioc *ioc, struct device *dev,
3378c2ecf20Sopenharmony_ci		  unsigned long bits_wanted)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	unsigned long *res_ptr = ioc->res_hint;
3408c2ecf20Sopenharmony_ci	unsigned long *res_end = (unsigned long *) &(ioc->res_map[ioc->res_size]);
3418c2ecf20Sopenharmony_ci	unsigned long pide = ~0UL, tpide;
3428c2ecf20Sopenharmony_ci	unsigned long boundary_size;
3438c2ecf20Sopenharmony_ci	unsigned long shift;
3448c2ecf20Sopenharmony_ci	int ret;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	boundary_size = dma_get_seg_boundary_nr_pages(dev, IOVP_SHIFT);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci#if defined(ZX1_SUPPORT)
3498c2ecf20Sopenharmony_ci	BUG_ON(ioc->ibase & ~IOVP_MASK);
3508c2ecf20Sopenharmony_ci	shift = ioc->ibase >> IOVP_SHIFT;
3518c2ecf20Sopenharmony_ci#else
3528c2ecf20Sopenharmony_ci	shift = 0;
3538c2ecf20Sopenharmony_ci#endif
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (bits_wanted > (BITS_PER_LONG/2)) {
3568c2ecf20Sopenharmony_ci		/* Search word at a time - no mask needed */
3578c2ecf20Sopenharmony_ci		for(; res_ptr < res_end; ++res_ptr) {
3588c2ecf20Sopenharmony_ci			tpide = ptr_to_pide(ioc, res_ptr, 0);
3598c2ecf20Sopenharmony_ci			ret = iommu_is_span_boundary(tpide, bits_wanted,
3608c2ecf20Sopenharmony_ci						     shift,
3618c2ecf20Sopenharmony_ci						     boundary_size);
3628c2ecf20Sopenharmony_ci			if ((*res_ptr == 0) && !ret) {
3638c2ecf20Sopenharmony_ci				*res_ptr = RESMAP_MASK(bits_wanted);
3648c2ecf20Sopenharmony_ci				pide = tpide;
3658c2ecf20Sopenharmony_ci				break;
3668c2ecf20Sopenharmony_ci			}
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci		/* point to the next word on next pass */
3698c2ecf20Sopenharmony_ci		res_ptr++;
3708c2ecf20Sopenharmony_ci		ioc->res_bitshift = 0;
3718c2ecf20Sopenharmony_ci	} else {
3728c2ecf20Sopenharmony_ci		/*
3738c2ecf20Sopenharmony_ci		** Search the resource bit map on well-aligned values.
3748c2ecf20Sopenharmony_ci		** "o" is the alignment.
3758c2ecf20Sopenharmony_ci		** We need the alignment to invalidate I/O TLB using
3768c2ecf20Sopenharmony_ci		** SBA HW features in the unmap path.
3778c2ecf20Sopenharmony_ci		*/
3788c2ecf20Sopenharmony_ci		unsigned long o = 1 << get_order(bits_wanted << PAGE_SHIFT);
3798c2ecf20Sopenharmony_ci		uint bitshiftcnt = ALIGN(ioc->res_bitshift, o);
3808c2ecf20Sopenharmony_ci		unsigned long mask;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		if (bitshiftcnt >= BITS_PER_LONG) {
3838c2ecf20Sopenharmony_ci			bitshiftcnt = 0;
3848c2ecf20Sopenharmony_ci			res_ptr++;
3858c2ecf20Sopenharmony_ci		}
3868c2ecf20Sopenharmony_ci		mask = RESMAP_MASK(bits_wanted) >> bitshiftcnt;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		DBG_RES("%s() o %ld %p", __func__, o, res_ptr);
3898c2ecf20Sopenharmony_ci		while(res_ptr < res_end)
3908c2ecf20Sopenharmony_ci		{
3918c2ecf20Sopenharmony_ci			DBG_RES("    %p %lx %lx\n", res_ptr, mask, *res_ptr);
3928c2ecf20Sopenharmony_ci			WARN_ON(mask == 0);
3938c2ecf20Sopenharmony_ci			tpide = ptr_to_pide(ioc, res_ptr, bitshiftcnt);
3948c2ecf20Sopenharmony_ci			ret = iommu_is_span_boundary(tpide, bits_wanted,
3958c2ecf20Sopenharmony_ci						     shift,
3968c2ecf20Sopenharmony_ci						     boundary_size);
3978c2ecf20Sopenharmony_ci			if ((((*res_ptr) & mask) == 0) && !ret) {
3988c2ecf20Sopenharmony_ci				*res_ptr |= mask;     /* mark resources busy! */
3998c2ecf20Sopenharmony_ci				pide = tpide;
4008c2ecf20Sopenharmony_ci				break;
4018c2ecf20Sopenharmony_ci			}
4028c2ecf20Sopenharmony_ci			mask >>= o;
4038c2ecf20Sopenharmony_ci			bitshiftcnt += o;
4048c2ecf20Sopenharmony_ci			if (mask == 0) {
4058c2ecf20Sopenharmony_ci				mask = RESMAP_MASK(bits_wanted);
4068c2ecf20Sopenharmony_ci				bitshiftcnt=0;
4078c2ecf20Sopenharmony_ci				res_ptr++;
4088c2ecf20Sopenharmony_ci			}
4098c2ecf20Sopenharmony_ci		}
4108c2ecf20Sopenharmony_ci		/* look in the same word on the next pass */
4118c2ecf20Sopenharmony_ci		ioc->res_bitshift = bitshiftcnt + bits_wanted;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* wrapped ? */
4158c2ecf20Sopenharmony_ci	if (res_end <= res_ptr) {
4168c2ecf20Sopenharmony_ci		ioc->res_hint = (unsigned long *) ioc->res_map;
4178c2ecf20Sopenharmony_ci		ioc->res_bitshift = 0;
4188c2ecf20Sopenharmony_ci	} else {
4198c2ecf20Sopenharmony_ci		ioc->res_hint = res_ptr;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci	return (pide);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci/**
4268c2ecf20Sopenharmony_ci * sba_alloc_range - find free bits and mark them in IO PDIR resource bitmap
4278c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
4288c2ecf20Sopenharmony_ci * @size: number of bytes to create a mapping for
4298c2ecf20Sopenharmony_ci *
4308c2ecf20Sopenharmony_ci * Given a size, find consecutive unmarked and then mark those bits in the
4318c2ecf20Sopenharmony_ci * resource bit map.
4328c2ecf20Sopenharmony_ci */
4338c2ecf20Sopenharmony_cistatic int
4348c2ecf20Sopenharmony_cisba_alloc_range(struct ioc *ioc, struct device *dev, size_t size)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	unsigned int pages_needed = size >> IOVP_SHIFT;
4378c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
4388c2ecf20Sopenharmony_ci	unsigned long cr_start = mfctl(16);
4398c2ecf20Sopenharmony_ci#endif
4408c2ecf20Sopenharmony_ci	unsigned long pide;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	pide = sba_search_bitmap(ioc, dev, pages_needed);
4438c2ecf20Sopenharmony_ci	if (pide >= (ioc->res_size << 3)) {
4448c2ecf20Sopenharmony_ci		pide = sba_search_bitmap(ioc, dev, pages_needed);
4458c2ecf20Sopenharmony_ci		if (pide >= (ioc->res_size << 3))
4468c2ecf20Sopenharmony_ci			panic("%s: I/O MMU @ %p is out of mapping resources\n",
4478c2ecf20Sopenharmony_ci			      __FILE__, ioc->ioc_hpa);
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
4518c2ecf20Sopenharmony_ci	/* verify the first enable bit is clear */
4528c2ecf20Sopenharmony_ci	if(0x00 != ((u8 *) ioc->pdir_base)[pide*sizeof(u64) + 7]) {
4538c2ecf20Sopenharmony_ci		sba_dump_pdir_entry(ioc, "sba_search_bitmap() botched it?", pide);
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci#endif
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	DBG_RES("%s(%x) %d -> %lx hint %x/%x\n",
4588c2ecf20Sopenharmony_ci		__func__, size, pages_needed, pide,
4598c2ecf20Sopenharmony_ci		(uint) ((unsigned long) ioc->res_hint - (unsigned long) ioc->res_map),
4608c2ecf20Sopenharmony_ci		ioc->res_bitshift );
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
4638c2ecf20Sopenharmony_ci	{
4648c2ecf20Sopenharmony_ci		unsigned long cr_end = mfctl(16);
4658c2ecf20Sopenharmony_ci		unsigned long tmp = cr_end - cr_start;
4668c2ecf20Sopenharmony_ci		/* check for roll over */
4678c2ecf20Sopenharmony_ci		cr_start = (cr_end < cr_start) ?  -(tmp) : (tmp);
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci	ioc->avg_search[ioc->avg_idx++] = cr_start;
4708c2ecf20Sopenharmony_ci	ioc->avg_idx &= SBA_SEARCH_SAMPLE - 1;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	ioc->used_pages += pages_needed;
4738c2ecf20Sopenharmony_ci#endif
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	return (pide);
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci/**
4808c2ecf20Sopenharmony_ci * sba_free_range - unmark bits in IO PDIR resource bitmap
4818c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
4828c2ecf20Sopenharmony_ci * @iova: IO virtual address which was previously allocated.
4838c2ecf20Sopenharmony_ci * @size: number of bytes to create a mapping for
4848c2ecf20Sopenharmony_ci *
4858c2ecf20Sopenharmony_ci * clear bits in the ioc's resource map
4868c2ecf20Sopenharmony_ci */
4878c2ecf20Sopenharmony_cistatic SBA_INLINE void
4888c2ecf20Sopenharmony_cisba_free_range(struct ioc *ioc, dma_addr_t iova, size_t size)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	unsigned long iovp = SBA_IOVP(ioc, iova);
4918c2ecf20Sopenharmony_ci	unsigned int pide = PDIR_INDEX(iovp);
4928c2ecf20Sopenharmony_ci	unsigned int ridx = pide >> 3;	/* convert bit to byte address */
4938c2ecf20Sopenharmony_ci	unsigned long *res_ptr = (unsigned long *) &((ioc)->res_map[ridx & ~RESMAP_IDX_MASK]);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	int bits_not_wanted = size >> IOVP_SHIFT;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	/* 3-bits "bit" address plus 2 (or 3) bits for "byte" == bit in word */
4988c2ecf20Sopenharmony_ci	unsigned long m = RESMAP_MASK(bits_not_wanted) >> (pide & (BITS_PER_LONG - 1));
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	DBG_RES("%s( ,%x,%x) %x/%lx %x %p %lx\n",
5018c2ecf20Sopenharmony_ci		__func__, (uint) iova, size,
5028c2ecf20Sopenharmony_ci		bits_not_wanted, m, pide, res_ptr, *res_ptr);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
5058c2ecf20Sopenharmony_ci	ioc->used_pages -= bits_not_wanted;
5068c2ecf20Sopenharmony_ci#endif
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	*res_ptr &= ~m;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/**************************************************************
5138c2ecf20Sopenharmony_ci*
5148c2ecf20Sopenharmony_ci*   "Dynamic DMA Mapping" support (aka "Coherent I/O")
5158c2ecf20Sopenharmony_ci*
5168c2ecf20Sopenharmony_ci***************************************************************/
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci#ifdef SBA_HINT_SUPPORT
5198c2ecf20Sopenharmony_ci#define SBA_DMA_HINT(ioc, val) ((val) << (ioc)->hint_shift_pdir)
5208c2ecf20Sopenharmony_ci#endif
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_citypedef unsigned long space_t;
5238c2ecf20Sopenharmony_ci#define KERNEL_SPACE 0
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/**
5268c2ecf20Sopenharmony_ci * sba_io_pdir_entry - fill in one IO PDIR entry
5278c2ecf20Sopenharmony_ci * @pdir_ptr:  pointer to IO PDIR entry
5288c2ecf20Sopenharmony_ci * @sid: process Space ID - currently only support KERNEL_SPACE
5298c2ecf20Sopenharmony_ci * @vba: Virtual CPU address of buffer to map
5308c2ecf20Sopenharmony_ci * @hint: DMA hint set to use for this mapping
5318c2ecf20Sopenharmony_ci *
5328c2ecf20Sopenharmony_ci * SBA Mapping Routine
5338c2ecf20Sopenharmony_ci *
5348c2ecf20Sopenharmony_ci * Given a virtual address (vba, arg2) and space id, (sid, arg1)
5358c2ecf20Sopenharmony_ci * sba_io_pdir_entry() loads the I/O PDIR entry pointed to by
5368c2ecf20Sopenharmony_ci * pdir_ptr (arg0).
5378c2ecf20Sopenharmony_ci * Using the bass-ackwards HP bit numbering, Each IO Pdir entry
5388c2ecf20Sopenharmony_ci * for Astro/Ike looks like:
5398c2ecf20Sopenharmony_ci *
5408c2ecf20Sopenharmony_ci *
5418c2ecf20Sopenharmony_ci *  0                    19                                 51   55       63
5428c2ecf20Sopenharmony_ci * +-+---------------------+----------------------------------+----+--------+
5438c2ecf20Sopenharmony_ci * |V|        U            |            PPN[43:12]            | U  |   VI   |
5448c2ecf20Sopenharmony_ci * +-+---------------------+----------------------------------+----+--------+
5458c2ecf20Sopenharmony_ci *
5468c2ecf20Sopenharmony_ci * Pluto is basically identical, supports fewer physical address bits:
5478c2ecf20Sopenharmony_ci *
5488c2ecf20Sopenharmony_ci *  0                       23                              51   55       63
5498c2ecf20Sopenharmony_ci * +-+------------------------+-------------------------------+----+--------+
5508c2ecf20Sopenharmony_ci * |V|        U               |         PPN[39:12]            | U  |   VI   |
5518c2ecf20Sopenharmony_ci * +-+------------------------+-------------------------------+----+--------+
5528c2ecf20Sopenharmony_ci *
5538c2ecf20Sopenharmony_ci *  V  == Valid Bit  (Most Significant Bit is bit 0)
5548c2ecf20Sopenharmony_ci *  U  == Unused
5558c2ecf20Sopenharmony_ci * PPN == Physical Page Number
5568c2ecf20Sopenharmony_ci * VI  == Virtual Index (aka Coherent Index)
5578c2ecf20Sopenharmony_ci *
5588c2ecf20Sopenharmony_ci * LPA instruction output is put into PPN field.
5598c2ecf20Sopenharmony_ci * LCI (Load Coherence Index) instruction provides the "VI" bits.
5608c2ecf20Sopenharmony_ci *
5618c2ecf20Sopenharmony_ci * We pre-swap the bytes since PCX-W is Big Endian and the
5628c2ecf20Sopenharmony_ci * IOMMU uses little endian for the pdir.
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic void SBA_INLINE
5668c2ecf20Sopenharmony_cisba_io_pdir_entry(u64 *pdir_ptr, space_t sid, unsigned long vba,
5678c2ecf20Sopenharmony_ci		  unsigned long hint)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	u64 pa; /* physical address */
5708c2ecf20Sopenharmony_ci	register unsigned ci; /* coherent index */
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	pa = lpa(vba);
5738c2ecf20Sopenharmony_ci	pa &= IOVP_MASK;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	asm("lci 0(%1), %0" : "=r" (ci) : "r" (vba));
5768c2ecf20Sopenharmony_ci	pa |= (ci >> PAGE_SHIFT) & 0xff;  /* move CI (8 bits) into lowest byte */
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	pa |= SBA_PDIR_VALID_BIT;	/* set "valid" bit */
5798c2ecf20Sopenharmony_ci	*pdir_ptr = cpu_to_le64(pa);	/* swap and store into I/O Pdir */
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	/*
5828c2ecf20Sopenharmony_ci	 * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
5838c2ecf20Sopenharmony_ci	 * (bit #61, big endian), we have to flush and sync every time
5848c2ecf20Sopenharmony_ci	 * IO-PDIR is changed in Ike/Astro.
5858c2ecf20Sopenharmony_ci	 */
5868c2ecf20Sopenharmony_ci	asm_io_fdc(pdir_ptr);
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/**
5918c2ecf20Sopenharmony_ci * sba_mark_invalid - invalidate one or more IO PDIR entries
5928c2ecf20Sopenharmony_ci * @ioc: IO MMU structure which owns the pdir we are interested in.
5938c2ecf20Sopenharmony_ci * @iova:  IO Virtual Address mapped earlier
5948c2ecf20Sopenharmony_ci * @byte_cnt:  number of bytes this mapping covers.
5958c2ecf20Sopenharmony_ci *
5968c2ecf20Sopenharmony_ci * Marking the IO PDIR entry(ies) as Invalid and invalidate
5978c2ecf20Sopenharmony_ci * corresponding IO TLB entry. The Ike PCOM (Purge Command Register)
5988c2ecf20Sopenharmony_ci * is to purge stale entries in the IO TLB when unmapping entries.
5998c2ecf20Sopenharmony_ci *
6008c2ecf20Sopenharmony_ci * The PCOM register supports purging of multiple pages, with a minium
6018c2ecf20Sopenharmony_ci * of 1 page and a maximum of 2GB. Hardware requires the address be
6028c2ecf20Sopenharmony_ci * aligned to the size of the range being purged. The size of the range
6038c2ecf20Sopenharmony_ci * must be a power of 2. The "Cool perf optimization" in the
6048c2ecf20Sopenharmony_ci * allocation routine helps keep that true.
6058c2ecf20Sopenharmony_ci */
6068c2ecf20Sopenharmony_cistatic SBA_INLINE void
6078c2ecf20Sopenharmony_cisba_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	u32 iovp = (u32) SBA_IOVP(ioc,iova);
6108c2ecf20Sopenharmony_ci	u64 *pdir_ptr = &ioc->pdir_base[PDIR_INDEX(iovp)];
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
6138c2ecf20Sopenharmony_ci	/* Assert first pdir entry is set.
6148c2ecf20Sopenharmony_ci	**
6158c2ecf20Sopenharmony_ci	** Even though this is a big-endian machine, the entries
6168c2ecf20Sopenharmony_ci	** in the iopdir are little endian. That's why we look at
6178c2ecf20Sopenharmony_ci	** the byte at +7 instead of at +0.
6188c2ecf20Sopenharmony_ci	*/
6198c2ecf20Sopenharmony_ci	if (0x80 != (((u8 *) pdir_ptr)[7])) {
6208c2ecf20Sopenharmony_ci		sba_dump_pdir_entry(ioc,"sba_mark_invalid()", PDIR_INDEX(iovp));
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci#endif
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (byte_cnt > IOVP_SIZE)
6258c2ecf20Sopenharmony_ci	{
6268c2ecf20Sopenharmony_ci#if 0
6278c2ecf20Sopenharmony_ci		unsigned long entries_per_cacheline = ioc_needs_fdc ?
6288c2ecf20Sopenharmony_ci				L1_CACHE_ALIGN(((unsigned long) pdir_ptr))
6298c2ecf20Sopenharmony_ci					- (unsigned long) pdir_ptr;
6308c2ecf20Sopenharmony_ci				: 262144;
6318c2ecf20Sopenharmony_ci#endif
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci		/* set "size" field for PCOM */
6348c2ecf20Sopenharmony_ci		iovp |= get_order(byte_cnt) + PAGE_SHIFT;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci		do {
6378c2ecf20Sopenharmony_ci			/* clear I/O Pdir entry "valid" bit first */
6388c2ecf20Sopenharmony_ci			((u8 *) pdir_ptr)[7] = 0;
6398c2ecf20Sopenharmony_ci			asm_io_fdc(pdir_ptr);
6408c2ecf20Sopenharmony_ci			if (ioc_needs_fdc) {
6418c2ecf20Sopenharmony_ci#if 0
6428c2ecf20Sopenharmony_ci				entries_per_cacheline = L1_CACHE_SHIFT - 3;
6438c2ecf20Sopenharmony_ci#endif
6448c2ecf20Sopenharmony_ci			}
6458c2ecf20Sopenharmony_ci			pdir_ptr++;
6468c2ecf20Sopenharmony_ci			byte_cnt -= IOVP_SIZE;
6478c2ecf20Sopenharmony_ci		} while (byte_cnt > IOVP_SIZE);
6488c2ecf20Sopenharmony_ci	} else
6498c2ecf20Sopenharmony_ci		iovp |= IOVP_SHIFT;     /* set "size" field for PCOM */
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	/*
6528c2ecf20Sopenharmony_ci	** clear I/O PDIR entry "valid" bit.
6538c2ecf20Sopenharmony_ci	** We have to R/M/W the cacheline regardless how much of the
6548c2ecf20Sopenharmony_ci	** pdir entry that we clobber.
6558c2ecf20Sopenharmony_ci	** The rest of the entry would be useful for debugging if we
6568c2ecf20Sopenharmony_ci	** could dump core on HPMC.
6578c2ecf20Sopenharmony_ci	*/
6588c2ecf20Sopenharmony_ci	((u8 *) pdir_ptr)[7] = 0;
6598c2ecf20Sopenharmony_ci	asm_io_fdc(pdir_ptr);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	WRITE_REG( SBA_IOVA(ioc, iovp, 0, 0), ioc->ioc_hpa+IOC_PCOM);
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci/**
6658c2ecf20Sopenharmony_ci * sba_dma_supported - PCI driver can query DMA support
6668c2ecf20Sopenharmony_ci * @dev: instance of PCI owned by the driver that's asking
6678c2ecf20Sopenharmony_ci * @mask:  number of address bits this PCI device can handle
6688c2ecf20Sopenharmony_ci *
6698c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
6708c2ecf20Sopenharmony_ci */
6718c2ecf20Sopenharmony_cistatic int sba_dma_supported( struct device *dev, u64 mask)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	struct ioc *ioc;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	if (dev == NULL) {
6768c2ecf20Sopenharmony_ci		printk(KERN_ERR MODULE_NAME ": EISA/ISA/et al not supported\n");
6778c2ecf20Sopenharmony_ci		BUG();
6788c2ecf20Sopenharmony_ci		return(0);
6798c2ecf20Sopenharmony_ci	}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	ioc = GET_IOC(dev);
6828c2ecf20Sopenharmony_ci	if (!ioc)
6838c2ecf20Sopenharmony_ci		return 0;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	/*
6868c2ecf20Sopenharmony_ci	 * check if mask is >= than the current max IO Virt Address
6878c2ecf20Sopenharmony_ci	 * The max IO Virt address will *always* < 30 bits.
6888c2ecf20Sopenharmony_ci	 */
6898c2ecf20Sopenharmony_ci	return((int)(mask >= (ioc->ibase - 1 +
6908c2ecf20Sopenharmony_ci			(ioc->pdir_size / sizeof(u64) * IOVP_SIZE) )));
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci/**
6958c2ecf20Sopenharmony_ci * sba_map_single - map one buffer and return IOVA for DMA
6968c2ecf20Sopenharmony_ci * @dev: instance of PCI owned by the driver that's asking.
6978c2ecf20Sopenharmony_ci * @addr:  driver buffer to map.
6988c2ecf20Sopenharmony_ci * @size:  number of bytes to map in driver buffer.
6998c2ecf20Sopenharmony_ci * @direction:  R/W or both.
7008c2ecf20Sopenharmony_ci *
7018c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
7028c2ecf20Sopenharmony_ci */
7038c2ecf20Sopenharmony_cistatic dma_addr_t
7048c2ecf20Sopenharmony_cisba_map_single(struct device *dev, void *addr, size_t size,
7058c2ecf20Sopenharmony_ci	       enum dma_data_direction direction)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct ioc *ioc;
7088c2ecf20Sopenharmony_ci	unsigned long flags;
7098c2ecf20Sopenharmony_ci	dma_addr_t iovp;
7108c2ecf20Sopenharmony_ci	dma_addr_t offset;
7118c2ecf20Sopenharmony_ci	u64 *pdir_start;
7128c2ecf20Sopenharmony_ci	int pide;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	ioc = GET_IOC(dev);
7158c2ecf20Sopenharmony_ci	if (!ioc)
7168c2ecf20Sopenharmony_ci		return DMA_MAPPING_ERROR;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	/* save offset bits */
7198c2ecf20Sopenharmony_ci	offset = ((dma_addr_t) (long) addr) & ~IOVP_MASK;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	/* round up to nearest IOVP_SIZE */
7228c2ecf20Sopenharmony_ci	size = (size + offset + ~IOVP_MASK) & IOVP_MASK;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ioc->res_lock, flags);
7258c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
7268c2ecf20Sopenharmony_ci	sba_check_pdir(ioc,"Check before sba_map_single()");
7278c2ecf20Sopenharmony_ci#endif
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
7308c2ecf20Sopenharmony_ci	ioc->msingle_calls++;
7318c2ecf20Sopenharmony_ci	ioc->msingle_pages += size >> IOVP_SHIFT;
7328c2ecf20Sopenharmony_ci#endif
7338c2ecf20Sopenharmony_ci	pide = sba_alloc_range(ioc, dev, size);
7348c2ecf20Sopenharmony_ci	iovp = (dma_addr_t) pide << IOVP_SHIFT;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	DBG_RUN("%s() 0x%p -> 0x%lx\n",
7378c2ecf20Sopenharmony_ci		__func__, addr, (long) iovp | offset);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	pdir_start = &(ioc->pdir_base[pide]);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	while (size > 0) {
7428c2ecf20Sopenharmony_ci		sba_io_pdir_entry(pdir_start, KERNEL_SPACE, (unsigned long) addr, 0);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		DBG_RUN("	pdir 0x%p %02x%02x%02x%02x%02x%02x%02x%02x\n",
7458c2ecf20Sopenharmony_ci			pdir_start,
7468c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[7]),
7478c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[6]),
7488c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[5]),
7498c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[4]),
7508c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[3]),
7518c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[2]),
7528c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[1]),
7538c2ecf20Sopenharmony_ci			(u8) (((u8 *) pdir_start)[0])
7548c2ecf20Sopenharmony_ci			);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		addr += IOVP_SIZE;
7578c2ecf20Sopenharmony_ci		size -= IOVP_SIZE;
7588c2ecf20Sopenharmony_ci		pdir_start++;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* force FDC ops in io_pdir_entry() to be visible to IOMMU */
7628c2ecf20Sopenharmony_ci	asm_io_sync();
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
7658c2ecf20Sopenharmony_ci	sba_check_pdir(ioc,"Check after sba_map_single()");
7668c2ecf20Sopenharmony_ci#endif
7678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ioc->res_lock, flags);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	/* form complete address */
7708c2ecf20Sopenharmony_ci	return SBA_IOVA(ioc, iovp, offset, DEFAULT_DMA_HINT_REG);
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic dma_addr_t
7758c2ecf20Sopenharmony_cisba_map_page(struct device *dev, struct page *page, unsigned long offset,
7768c2ecf20Sopenharmony_ci		size_t size, enum dma_data_direction direction,
7778c2ecf20Sopenharmony_ci		unsigned long attrs)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	return sba_map_single(dev, page_address(page) + offset, size,
7808c2ecf20Sopenharmony_ci			direction);
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci/**
7858c2ecf20Sopenharmony_ci * sba_unmap_page - unmap one IOVA and free resources
7868c2ecf20Sopenharmony_ci * @dev: instance of PCI owned by the driver that's asking.
7878c2ecf20Sopenharmony_ci * @iova:  IOVA of driver buffer previously mapped.
7888c2ecf20Sopenharmony_ci * @size:  number of bytes mapped in driver buffer.
7898c2ecf20Sopenharmony_ci * @direction:  R/W or both.
7908c2ecf20Sopenharmony_ci *
7918c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
7928c2ecf20Sopenharmony_ci */
7938c2ecf20Sopenharmony_cistatic void
7948c2ecf20Sopenharmony_cisba_unmap_page(struct device *dev, dma_addr_t iova, size_t size,
7958c2ecf20Sopenharmony_ci		enum dma_data_direction direction, unsigned long attrs)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	struct ioc *ioc;
7988c2ecf20Sopenharmony_ci#if DELAYED_RESOURCE_CNT > 0
7998c2ecf20Sopenharmony_ci	struct sba_dma_pair *d;
8008c2ecf20Sopenharmony_ci#endif
8018c2ecf20Sopenharmony_ci	unsigned long flags;
8028c2ecf20Sopenharmony_ci	dma_addr_t offset;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	DBG_RUN("%s() iovp 0x%lx/%x\n", __func__, (long) iova, size);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	ioc = GET_IOC(dev);
8078c2ecf20Sopenharmony_ci	if (!ioc) {
8088c2ecf20Sopenharmony_ci		WARN_ON(!ioc);
8098c2ecf20Sopenharmony_ci		return;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci	offset = iova & ~IOVP_MASK;
8128c2ecf20Sopenharmony_ci	iova ^= offset;        /* clear offset bits */
8138c2ecf20Sopenharmony_ci	size += offset;
8148c2ecf20Sopenharmony_ci	size = ALIGN(size, IOVP_SIZE);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ioc->res_lock, flags);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
8198c2ecf20Sopenharmony_ci	ioc->usingle_calls++;
8208c2ecf20Sopenharmony_ci	ioc->usingle_pages += size >> IOVP_SHIFT;
8218c2ecf20Sopenharmony_ci#endif
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	sba_mark_invalid(ioc, iova, size);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci#if DELAYED_RESOURCE_CNT > 0
8268c2ecf20Sopenharmony_ci	/* Delaying when we re-use a IO Pdir entry reduces the number
8278c2ecf20Sopenharmony_ci	 * of MMIO reads needed to flush writes to the PCOM register.
8288c2ecf20Sopenharmony_ci	 */
8298c2ecf20Sopenharmony_ci	d = &(ioc->saved[ioc->saved_cnt]);
8308c2ecf20Sopenharmony_ci	d->iova = iova;
8318c2ecf20Sopenharmony_ci	d->size = size;
8328c2ecf20Sopenharmony_ci	if (++(ioc->saved_cnt) >= DELAYED_RESOURCE_CNT) {
8338c2ecf20Sopenharmony_ci		int cnt = ioc->saved_cnt;
8348c2ecf20Sopenharmony_ci		while (cnt--) {
8358c2ecf20Sopenharmony_ci			sba_free_range(ioc, d->iova, d->size);
8368c2ecf20Sopenharmony_ci			d--;
8378c2ecf20Sopenharmony_ci		}
8388c2ecf20Sopenharmony_ci		ioc->saved_cnt = 0;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci		READ_REG(ioc->ioc_hpa+IOC_PCOM);	/* flush purges */
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci#else /* DELAYED_RESOURCE_CNT == 0 */
8438c2ecf20Sopenharmony_ci	sba_free_range(ioc, iova, size);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	/* If fdc's were issued, force fdc's to be visible now */
8468c2ecf20Sopenharmony_ci	asm_io_sync();
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	READ_REG(ioc->ioc_hpa+IOC_PCOM);	/* flush purges */
8498c2ecf20Sopenharmony_ci#endif /* DELAYED_RESOURCE_CNT == 0 */
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ioc->res_lock, flags);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	/* XXX REVISIT for 2.5 Linux - need syncdma for zero-copy support.
8548c2ecf20Sopenharmony_ci	** For Astro based systems this isn't a big deal WRT performance.
8558c2ecf20Sopenharmony_ci	** As long as 2.4 kernels copyin/copyout data from/to userspace,
8568c2ecf20Sopenharmony_ci	** we don't need the syncdma. The issue here is I/O MMU cachelines
8578c2ecf20Sopenharmony_ci	** are *not* coherent in all cases.  May be hwrev dependent.
8588c2ecf20Sopenharmony_ci	** Need to investigate more.
8598c2ecf20Sopenharmony_ci	asm volatile("syncdma");
8608c2ecf20Sopenharmony_ci	*/
8618c2ecf20Sopenharmony_ci}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci/**
8658c2ecf20Sopenharmony_ci * sba_alloc - allocate/map shared mem for DMA
8668c2ecf20Sopenharmony_ci * @hwdev: instance of PCI owned by the driver that's asking.
8678c2ecf20Sopenharmony_ci * @size:  number of bytes mapped in driver buffer.
8688c2ecf20Sopenharmony_ci * @dma_handle:  IOVA of new buffer.
8698c2ecf20Sopenharmony_ci *
8708c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
8718c2ecf20Sopenharmony_ci */
8728c2ecf20Sopenharmony_cistatic void *sba_alloc(struct device *hwdev, size_t size, dma_addr_t *dma_handle,
8738c2ecf20Sopenharmony_ci		gfp_t gfp, unsigned long attrs)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	void *ret;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	if (!hwdev) {
8788c2ecf20Sopenharmony_ci		/* only support PCI */
8798c2ecf20Sopenharmony_ci		*dma_handle = 0;
8808c2ecf20Sopenharmony_ci		return NULL;
8818c2ecf20Sopenharmony_ci	}
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci        ret = (void *) __get_free_pages(gfp, get_order(size));
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (ret) {
8868c2ecf20Sopenharmony_ci		memset(ret, 0, size);
8878c2ecf20Sopenharmony_ci		*dma_handle = sba_map_single(hwdev, ret, size, 0);
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return ret;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci/**
8958c2ecf20Sopenharmony_ci * sba_free - free/unmap shared mem for DMA
8968c2ecf20Sopenharmony_ci * @hwdev: instance of PCI owned by the driver that's asking.
8978c2ecf20Sopenharmony_ci * @size:  number of bytes mapped in driver buffer.
8988c2ecf20Sopenharmony_ci * @vaddr:  virtual address IOVA of "consistent" buffer.
8998c2ecf20Sopenharmony_ci * @dma_handler:  IO virtual address of "consistent" buffer.
9008c2ecf20Sopenharmony_ci *
9018c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
9028c2ecf20Sopenharmony_ci */
9038c2ecf20Sopenharmony_cistatic void
9048c2ecf20Sopenharmony_cisba_free(struct device *hwdev, size_t size, void *vaddr,
9058c2ecf20Sopenharmony_ci		    dma_addr_t dma_handle, unsigned long attrs)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	sba_unmap_page(hwdev, dma_handle, size, 0, 0);
9088c2ecf20Sopenharmony_ci	free_pages((unsigned long) vaddr, get_order(size));
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci/*
9138c2ecf20Sopenharmony_ci** Since 0 is a valid pdir_base index value, can't use that
9148c2ecf20Sopenharmony_ci** to determine if a value is valid or not. Use a flag to indicate
9158c2ecf20Sopenharmony_ci** the SG list entry contains a valid pdir index.
9168c2ecf20Sopenharmony_ci*/
9178c2ecf20Sopenharmony_ci#define PIDE_FLAG 0x80000000UL
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
9208c2ecf20Sopenharmony_ci#define IOMMU_MAP_STATS
9218c2ecf20Sopenharmony_ci#endif
9228c2ecf20Sopenharmony_ci#include "iommu-helpers.h"
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci#ifdef DEBUG_LARGE_SG_ENTRIES
9258c2ecf20Sopenharmony_ciint dump_run_sg = 0;
9268c2ecf20Sopenharmony_ci#endif
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci/**
9308c2ecf20Sopenharmony_ci * sba_map_sg - map Scatter/Gather list
9318c2ecf20Sopenharmony_ci * @dev: instance of PCI owned by the driver that's asking.
9328c2ecf20Sopenharmony_ci * @sglist:  array of buffer/length pairs
9338c2ecf20Sopenharmony_ci * @nents:  number of entries in list
9348c2ecf20Sopenharmony_ci * @direction:  R/W or both.
9358c2ecf20Sopenharmony_ci *
9368c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
9378c2ecf20Sopenharmony_ci */
9388c2ecf20Sopenharmony_cistatic int
9398c2ecf20Sopenharmony_cisba_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
9408c2ecf20Sopenharmony_ci	   enum dma_data_direction direction, unsigned long attrs)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	struct ioc *ioc;
9438c2ecf20Sopenharmony_ci	int coalesced, filled = 0;
9448c2ecf20Sopenharmony_ci	unsigned long flags;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	DBG_RUN_SG("%s() START %d entries\n", __func__, nents);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	ioc = GET_IOC(dev);
9498c2ecf20Sopenharmony_ci	if (!ioc)
9508c2ecf20Sopenharmony_ci		return 0;
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	/* Fast path single entry scatterlists. */
9538c2ecf20Sopenharmony_ci	if (nents == 1) {
9548c2ecf20Sopenharmony_ci		sg_dma_address(sglist) = sba_map_single(dev, sg_virt(sglist),
9558c2ecf20Sopenharmony_ci						sglist->length, direction);
9568c2ecf20Sopenharmony_ci		sg_dma_len(sglist)     = sglist->length;
9578c2ecf20Sopenharmony_ci		return 1;
9588c2ecf20Sopenharmony_ci	}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ioc->res_lock, flags);
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
9638c2ecf20Sopenharmony_ci	if (sba_check_pdir(ioc,"Check before sba_map_sg()"))
9648c2ecf20Sopenharmony_ci	{
9658c2ecf20Sopenharmony_ci		sba_dump_sg(ioc, sglist, nents);
9668c2ecf20Sopenharmony_ci		panic("Check before sba_map_sg()");
9678c2ecf20Sopenharmony_ci	}
9688c2ecf20Sopenharmony_ci#endif
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
9718c2ecf20Sopenharmony_ci	ioc->msg_calls++;
9728c2ecf20Sopenharmony_ci#endif
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	/*
9758c2ecf20Sopenharmony_ci	** First coalesce the chunks and allocate I/O pdir space
9768c2ecf20Sopenharmony_ci	**
9778c2ecf20Sopenharmony_ci	** If this is one DMA stream, we can properly map using the
9788c2ecf20Sopenharmony_ci	** correct virtual address associated with each DMA page.
9798c2ecf20Sopenharmony_ci	** w/o this association, we wouldn't have coherent DMA!
9808c2ecf20Sopenharmony_ci	** Access to the virtual address is what forces a two pass algorithm.
9818c2ecf20Sopenharmony_ci	*/
9828c2ecf20Sopenharmony_ci	coalesced = iommu_coalesce_chunks(ioc, dev, sglist, nents, sba_alloc_range);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	/*
9858c2ecf20Sopenharmony_ci	** Program the I/O Pdir
9868c2ecf20Sopenharmony_ci	**
9878c2ecf20Sopenharmony_ci	** map the virtual addresses to the I/O Pdir
9888c2ecf20Sopenharmony_ci	** o dma_address will contain the pdir index
9898c2ecf20Sopenharmony_ci	** o dma_len will contain the number of bytes to map
9908c2ecf20Sopenharmony_ci	** o address contains the virtual address.
9918c2ecf20Sopenharmony_ci	*/
9928c2ecf20Sopenharmony_ci	filled = iommu_fill_pdir(ioc, sglist, nents, 0, sba_io_pdir_entry);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	/* force FDC ops in io_pdir_entry() to be visible to IOMMU */
9958c2ecf20Sopenharmony_ci	asm_io_sync();
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
9988c2ecf20Sopenharmony_ci	if (sba_check_pdir(ioc,"Check after sba_map_sg()"))
9998c2ecf20Sopenharmony_ci	{
10008c2ecf20Sopenharmony_ci		sba_dump_sg(ioc, sglist, nents);
10018c2ecf20Sopenharmony_ci		panic("Check after sba_map_sg()\n");
10028c2ecf20Sopenharmony_ci	}
10038c2ecf20Sopenharmony_ci#endif
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ioc->res_lock, flags);
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	DBG_RUN_SG("%s() DONE %d mappings\n", __func__, filled);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	return filled;
10108c2ecf20Sopenharmony_ci}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci/**
10148c2ecf20Sopenharmony_ci * sba_unmap_sg - unmap Scatter/Gather list
10158c2ecf20Sopenharmony_ci * @dev: instance of PCI owned by the driver that's asking.
10168c2ecf20Sopenharmony_ci * @sglist:  array of buffer/length pairs
10178c2ecf20Sopenharmony_ci * @nents:  number of entries in list
10188c2ecf20Sopenharmony_ci * @direction:  R/W or both.
10198c2ecf20Sopenharmony_ci *
10208c2ecf20Sopenharmony_ci * See Documentation/core-api/dma-api-howto.rst
10218c2ecf20Sopenharmony_ci */
10228c2ecf20Sopenharmony_cistatic void
10238c2ecf20Sopenharmony_cisba_unmap_sg(struct device *dev, struct scatterlist *sglist, int nents,
10248c2ecf20Sopenharmony_ci	     enum dma_data_direction direction, unsigned long attrs)
10258c2ecf20Sopenharmony_ci{
10268c2ecf20Sopenharmony_ci	struct ioc *ioc;
10278c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
10288c2ecf20Sopenharmony_ci	unsigned long flags;
10298c2ecf20Sopenharmony_ci#endif
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	DBG_RUN_SG("%s() START %d entries,  %p,%x\n",
10328c2ecf20Sopenharmony_ci		__func__, nents, sg_virt(sglist), sglist->length);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	ioc = GET_IOC(dev);
10358c2ecf20Sopenharmony_ci	if (!ioc) {
10368c2ecf20Sopenharmony_ci		WARN_ON(!ioc);
10378c2ecf20Sopenharmony_ci		return;
10388c2ecf20Sopenharmony_ci	}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
10418c2ecf20Sopenharmony_ci	ioc->usg_calls++;
10428c2ecf20Sopenharmony_ci#endif
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
10458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ioc->res_lock, flags);
10468c2ecf20Sopenharmony_ci	sba_check_pdir(ioc,"Check before sba_unmap_sg()");
10478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ioc->res_lock, flags);
10488c2ecf20Sopenharmony_ci#endif
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	while (nents && sg_dma_len(sglist)) {
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci		sba_unmap_page(dev, sg_dma_address(sglist), sg_dma_len(sglist),
10538c2ecf20Sopenharmony_ci				direction, 0);
10548c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
10558c2ecf20Sopenharmony_ci		ioc->usg_pages += ((sg_dma_address(sglist) & ~IOVP_MASK) + sg_dma_len(sglist) + IOVP_SIZE - 1) >> PAGE_SHIFT;
10568c2ecf20Sopenharmony_ci		ioc->usingle_calls--;	/* kluge since call is unmap_sg() */
10578c2ecf20Sopenharmony_ci#endif
10588c2ecf20Sopenharmony_ci		++sglist;
10598c2ecf20Sopenharmony_ci		nents--;
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	DBG_RUN_SG("%s() DONE (nents %d)\n", __func__,  nents);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
10658c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ioc->res_lock, flags);
10668c2ecf20Sopenharmony_ci	sba_check_pdir(ioc,"Check after sba_unmap_sg()");
10678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ioc->res_lock, flags);
10688c2ecf20Sopenharmony_ci#endif
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic const struct dma_map_ops sba_ops = {
10738c2ecf20Sopenharmony_ci	.dma_supported =	sba_dma_supported,
10748c2ecf20Sopenharmony_ci	.alloc =		sba_alloc,
10758c2ecf20Sopenharmony_ci	.free =			sba_free,
10768c2ecf20Sopenharmony_ci	.map_page =		sba_map_page,
10778c2ecf20Sopenharmony_ci	.unmap_page =		sba_unmap_page,
10788c2ecf20Sopenharmony_ci	.map_sg =		sba_map_sg,
10798c2ecf20Sopenharmony_ci	.unmap_sg =		sba_unmap_sg,
10808c2ecf20Sopenharmony_ci	.get_sgtable =		dma_common_get_sgtable,
10818c2ecf20Sopenharmony_ci	.alloc_pages =		dma_common_alloc_pages,
10828c2ecf20Sopenharmony_ci	.free_pages =		dma_common_free_pages,
10838c2ecf20Sopenharmony_ci};
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci/**************************************************************************
10878c2ecf20Sopenharmony_ci**
10888c2ecf20Sopenharmony_ci**   SBA PAT PDC support
10898c2ecf20Sopenharmony_ci**
10908c2ecf20Sopenharmony_ci**   o call pdc_pat_cell_module()
10918c2ecf20Sopenharmony_ci**   o store ranges in PCI "resource" structures
10928c2ecf20Sopenharmony_ci**
10938c2ecf20Sopenharmony_ci**************************************************************************/
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_cistatic void
10968c2ecf20Sopenharmony_cisba_get_pat_resources(struct sba_device *sba_dev)
10978c2ecf20Sopenharmony_ci{
10988c2ecf20Sopenharmony_ci#if 0
10998c2ecf20Sopenharmony_ci/*
11008c2ecf20Sopenharmony_ci** TODO/REVISIT/FIXME: support for directed ranges requires calls to
11018c2ecf20Sopenharmony_ci**      PAT PDC to program the SBA/LBA directed range registers...this
11028c2ecf20Sopenharmony_ci**      burden may fall on the LBA code since it directly supports the
11038c2ecf20Sopenharmony_ci**      PCI subsystem. It's not clear yet. - ggg
11048c2ecf20Sopenharmony_ci*/
11058c2ecf20Sopenharmony_ciPAT_MOD(mod)->mod_info.mod_pages   = PAT_GET_MOD_PAGES(temp);
11068c2ecf20Sopenharmony_ci	FIXME : ???
11078c2ecf20Sopenharmony_ciPAT_MOD(mod)->mod_info.dvi         = PAT_GET_DVI(temp);
11088c2ecf20Sopenharmony_ci	Tells where the dvi bits are located in the address.
11098c2ecf20Sopenharmony_ciPAT_MOD(mod)->mod_info.ioc         = PAT_GET_IOC(temp);
11108c2ecf20Sopenharmony_ci	FIXME : ???
11118c2ecf20Sopenharmony_ci#endif
11128c2ecf20Sopenharmony_ci}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci/**************************************************************
11168c2ecf20Sopenharmony_ci*
11178c2ecf20Sopenharmony_ci*   Initialization and claim
11188c2ecf20Sopenharmony_ci*
11198c2ecf20Sopenharmony_ci***************************************************************/
11208c2ecf20Sopenharmony_ci#define PIRANHA_ADDR_MASK	0x00160000UL /* bit 17,18,20 */
11218c2ecf20Sopenharmony_ci#define PIRANHA_ADDR_VAL	0x00060000UL /* bit 17,18 on */
11228c2ecf20Sopenharmony_cistatic void *
11238c2ecf20Sopenharmony_cisba_alloc_pdir(unsigned int pdir_size)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci        unsigned long pdir_base;
11268c2ecf20Sopenharmony_ci	unsigned long pdir_order = get_order(pdir_size);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	pdir_base = __get_free_pages(GFP_KERNEL, pdir_order);
11298c2ecf20Sopenharmony_ci	if (NULL == (void *) pdir_base)	{
11308c2ecf20Sopenharmony_ci		panic("%s() could not allocate I/O Page Table\n",
11318c2ecf20Sopenharmony_ci			__func__);
11328c2ecf20Sopenharmony_ci	}
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	/* If this is not PA8700 (PCX-W2)
11358c2ecf20Sopenharmony_ci	**	OR newer than ver 2.2
11368c2ecf20Sopenharmony_ci	**	OR in a system that doesn't need VINDEX bits from SBA,
11378c2ecf20Sopenharmony_ci	**
11388c2ecf20Sopenharmony_ci	** then we aren't exposed to the HW bug.
11398c2ecf20Sopenharmony_ci	*/
11408c2ecf20Sopenharmony_ci	if ( ((boot_cpu_data.pdc.cpuid >> 5) & 0x7f) != 0x13
11418c2ecf20Sopenharmony_ci			|| (boot_cpu_data.pdc.versions > 0x202)
11428c2ecf20Sopenharmony_ci			|| (boot_cpu_data.pdc.capabilities & 0x08L) )
11438c2ecf20Sopenharmony_ci		return (void *) pdir_base;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	/*
11468c2ecf20Sopenharmony_ci	 * PA8700 (PCX-W2, aka piranha) silent data corruption fix
11478c2ecf20Sopenharmony_ci	 *
11488c2ecf20Sopenharmony_ci	 * An interaction between PA8700 CPU (Ver 2.2 or older) and
11498c2ecf20Sopenharmony_ci	 * Ike/Astro can cause silent data corruption. This is only
11508c2ecf20Sopenharmony_ci	 * a problem if the I/O PDIR is located in memory such that
11518c2ecf20Sopenharmony_ci	 * (little-endian)  bits 17 and 18 are on and bit 20 is off.
11528c2ecf20Sopenharmony_ci	 *
11538c2ecf20Sopenharmony_ci	 * Since the max IO Pdir size is 2MB, by cleverly allocating the
11548c2ecf20Sopenharmony_ci	 * right physical address, we can either avoid (IOPDIR <= 1MB)
11558c2ecf20Sopenharmony_ci	 * or minimize (2MB IO Pdir) the problem if we restrict the
11568c2ecf20Sopenharmony_ci	 * IO Pdir to a maximum size of 2MB-128K (1902K).
11578c2ecf20Sopenharmony_ci	 *
11588c2ecf20Sopenharmony_ci	 * Because we always allocate 2^N sized IO pdirs, either of the
11598c2ecf20Sopenharmony_ci	 * "bad" regions will be the last 128K if at all. That's easy
11608c2ecf20Sopenharmony_ci	 * to test for.
11618c2ecf20Sopenharmony_ci	 *
11628c2ecf20Sopenharmony_ci	 */
11638c2ecf20Sopenharmony_ci	if (pdir_order <= (19-12)) {
11648c2ecf20Sopenharmony_ci		if (((virt_to_phys(pdir_base)+pdir_size-1) & PIRANHA_ADDR_MASK) == PIRANHA_ADDR_VAL) {
11658c2ecf20Sopenharmony_ci			/* allocate a new one on 512k alignment */
11668c2ecf20Sopenharmony_ci			unsigned long new_pdir = __get_free_pages(GFP_KERNEL, (19-12));
11678c2ecf20Sopenharmony_ci			/* release original */
11688c2ecf20Sopenharmony_ci			free_pages(pdir_base, pdir_order);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci			pdir_base = new_pdir;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci			/* release excess */
11738c2ecf20Sopenharmony_ci			while (pdir_order < (19-12)) {
11748c2ecf20Sopenharmony_ci				new_pdir += pdir_size;
11758c2ecf20Sopenharmony_ci				free_pages(new_pdir, pdir_order);
11768c2ecf20Sopenharmony_ci				pdir_order +=1;
11778c2ecf20Sopenharmony_ci				pdir_size <<=1;
11788c2ecf20Sopenharmony_ci			}
11798c2ecf20Sopenharmony_ci		}
11808c2ecf20Sopenharmony_ci	} else {
11818c2ecf20Sopenharmony_ci		/*
11828c2ecf20Sopenharmony_ci		** 1MB or 2MB Pdir
11838c2ecf20Sopenharmony_ci		** Needs to be aligned on an "odd" 1MB boundary.
11848c2ecf20Sopenharmony_ci		*/
11858c2ecf20Sopenharmony_ci		unsigned long new_pdir = __get_free_pages(GFP_KERNEL, pdir_order+1); /* 2 or 4MB */
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci		/* release original */
11888c2ecf20Sopenharmony_ci		free_pages( pdir_base, pdir_order);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci		/* release first 1MB */
11918c2ecf20Sopenharmony_ci		free_pages(new_pdir, 20-12);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci		pdir_base = new_pdir + 1024*1024;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci		if (pdir_order > (20-12)) {
11968c2ecf20Sopenharmony_ci			/*
11978c2ecf20Sopenharmony_ci			** 2MB Pdir.
11988c2ecf20Sopenharmony_ci			**
11998c2ecf20Sopenharmony_ci			** Flag tells init_bitmap() to mark bad 128k as used
12008c2ecf20Sopenharmony_ci			** and to reduce the size by 128k.
12018c2ecf20Sopenharmony_ci			*/
12028c2ecf20Sopenharmony_ci			piranha_bad_128k = 1;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci			new_pdir += 3*1024*1024;
12058c2ecf20Sopenharmony_ci			/* release last 1MB */
12068c2ecf20Sopenharmony_ci			free_pages(new_pdir, 20-12);
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci			/* release unusable 128KB */
12098c2ecf20Sopenharmony_ci			free_pages(new_pdir - 128*1024 , 17-12);
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci			pdir_size -= 128*1024;
12128c2ecf20Sopenharmony_ci		}
12138c2ecf20Sopenharmony_ci	}
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	memset((void *) pdir_base, 0, pdir_size);
12168c2ecf20Sopenharmony_ci	return (void *) pdir_base;
12178c2ecf20Sopenharmony_ci}
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_cistruct ibase_data_struct {
12208c2ecf20Sopenharmony_ci	struct ioc *ioc;
12218c2ecf20Sopenharmony_ci	int ioc_num;
12228c2ecf20Sopenharmony_ci};
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_cistatic int setup_ibase_imask_callback(struct device *dev, void *data)
12258c2ecf20Sopenharmony_ci{
12268c2ecf20Sopenharmony_ci	/* lba_set_iregs() is in drivers/parisc/lba_pci.c */
12278c2ecf20Sopenharmony_ci        extern void lba_set_iregs(struct parisc_device *, u32, u32);
12288c2ecf20Sopenharmony_ci	struct parisc_device *lba = to_parisc_device(dev);
12298c2ecf20Sopenharmony_ci	struct ibase_data_struct *ibd = data;
12308c2ecf20Sopenharmony_ci	int rope_num = (lba->hpa.start >> 13) & 0xf;
12318c2ecf20Sopenharmony_ci	if (rope_num >> 3 == ibd->ioc_num)
12328c2ecf20Sopenharmony_ci		lba_set_iregs(lba, ibd->ioc->ibase, ibd->ioc->imask);
12338c2ecf20Sopenharmony_ci	return 0;
12348c2ecf20Sopenharmony_ci}
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci/* setup Mercury or Elroy IBASE/IMASK registers. */
12378c2ecf20Sopenharmony_cistatic void
12388c2ecf20Sopenharmony_cisetup_ibase_imask(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	struct ibase_data_struct ibase_data = {
12418c2ecf20Sopenharmony_ci		.ioc		= ioc,
12428c2ecf20Sopenharmony_ci		.ioc_num	= ioc_num,
12438c2ecf20Sopenharmony_ci	};
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	device_for_each_child(&sba->dev, &ibase_data,
12468c2ecf20Sopenharmony_ci			      setup_ibase_imask_callback);
12478c2ecf20Sopenharmony_ci}
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci#ifdef SBA_AGP_SUPPORT
12508c2ecf20Sopenharmony_cistatic int
12518c2ecf20Sopenharmony_cisba_ioc_find_quicksilver(struct device *dev, void *data)
12528c2ecf20Sopenharmony_ci{
12538c2ecf20Sopenharmony_ci	int *agp_found = data;
12548c2ecf20Sopenharmony_ci	struct parisc_device *lba = to_parisc_device(dev);
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	if (IS_QUICKSILVER(lba))
12578c2ecf20Sopenharmony_ci		*agp_found = 1;
12588c2ecf20Sopenharmony_ci	return 0;
12598c2ecf20Sopenharmony_ci}
12608c2ecf20Sopenharmony_ci#endif
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_cistatic void
12638c2ecf20Sopenharmony_cisba_ioc_init_pluto(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
12648c2ecf20Sopenharmony_ci{
12658c2ecf20Sopenharmony_ci	u32 iova_space_mask;
12668c2ecf20Sopenharmony_ci	u32 iova_space_size;
12678c2ecf20Sopenharmony_ci	int iov_order, tcnfg;
12688c2ecf20Sopenharmony_ci#ifdef SBA_AGP_SUPPORT
12698c2ecf20Sopenharmony_ci	int agp_found = 0;
12708c2ecf20Sopenharmony_ci#endif
12718c2ecf20Sopenharmony_ci	/*
12728c2ecf20Sopenharmony_ci	** Firmware programs the base and size of a "safe IOVA space"
12738c2ecf20Sopenharmony_ci	** (one that doesn't overlap memory or LMMIO space) in the
12748c2ecf20Sopenharmony_ci	** IBASE and IMASK registers.
12758c2ecf20Sopenharmony_ci	*/
12768c2ecf20Sopenharmony_ci	ioc->ibase = READ_REG(ioc->ioc_hpa + IOC_IBASE) & ~0x1fffffULL;
12778c2ecf20Sopenharmony_ci	iova_space_size = ~(READ_REG(ioc->ioc_hpa + IOC_IMASK) & 0xFFFFFFFFUL) + 1;
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	if ((ioc->ibase < 0xfed00000UL) && ((ioc->ibase + iova_space_size) > 0xfee00000UL)) {
12808c2ecf20Sopenharmony_ci		printk("WARNING: IOV space overlaps local config and interrupt message, truncating\n");
12818c2ecf20Sopenharmony_ci		iova_space_size /= 2;
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	/*
12858c2ecf20Sopenharmony_ci	** iov_order is always based on a 1GB IOVA space since we want to
12868c2ecf20Sopenharmony_ci	** turn on the other half for AGP GART.
12878c2ecf20Sopenharmony_ci	*/
12888c2ecf20Sopenharmony_ci	iov_order = get_order(iova_space_size >> (IOVP_SHIFT - PAGE_SHIFT));
12898c2ecf20Sopenharmony_ci	ioc->pdir_size = (iova_space_size / IOVP_SIZE) * sizeof(u64);
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	DBG_INIT("%s() hpa 0x%p IOV %dMB (%d bits)\n",
12928c2ecf20Sopenharmony_ci		__func__, ioc->ioc_hpa, iova_space_size >> 20,
12938c2ecf20Sopenharmony_ci		iov_order + PAGE_SHIFT);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	ioc->pdir_base = (void *) __get_free_pages(GFP_KERNEL,
12968c2ecf20Sopenharmony_ci						   get_order(ioc->pdir_size));
12978c2ecf20Sopenharmony_ci	if (!ioc->pdir_base)
12988c2ecf20Sopenharmony_ci		panic("Couldn't allocate I/O Page Table\n");
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	memset(ioc->pdir_base, 0, ioc->pdir_size);
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	DBG_INIT("%s() pdir %p size %x\n",
13038c2ecf20Sopenharmony_ci			__func__, ioc->pdir_base, ioc->pdir_size);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci#ifdef SBA_HINT_SUPPORT
13068c2ecf20Sopenharmony_ci	ioc->hint_shift_pdir = iov_order + PAGE_SHIFT;
13078c2ecf20Sopenharmony_ci	ioc->hint_mask_pdir = ~(0x3 << (iov_order + PAGE_SHIFT));
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	DBG_INIT("	hint_shift_pdir %x hint_mask_pdir %lx\n",
13108c2ecf20Sopenharmony_ci		ioc->hint_shift_pdir, ioc->hint_mask_pdir);
13118c2ecf20Sopenharmony_ci#endif
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	WARN_ON((((unsigned long) ioc->pdir_base) & PAGE_MASK) != (unsigned long) ioc->pdir_base);
13148c2ecf20Sopenharmony_ci	WRITE_REG(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci	/* build IMASK for IOC and Elroy */
13178c2ecf20Sopenharmony_ci	iova_space_mask =  0xffffffff;
13188c2ecf20Sopenharmony_ci	iova_space_mask <<= (iov_order + PAGE_SHIFT);
13198c2ecf20Sopenharmony_ci	ioc->imask = iova_space_mask;
13208c2ecf20Sopenharmony_ci#ifdef ZX1_SUPPORT
13218c2ecf20Sopenharmony_ci	ioc->iovp_mask = ~(iova_space_mask + PAGE_SIZE - 1);
13228c2ecf20Sopenharmony_ci#endif
13238c2ecf20Sopenharmony_ci	sba_dump_tlb(ioc->ioc_hpa);
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	setup_ibase_imask(sba, ioc, ioc_num);
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	WRITE_REG(ioc->imask, ioc->ioc_hpa + IOC_IMASK);
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
13308c2ecf20Sopenharmony_ci	/*
13318c2ecf20Sopenharmony_ci	** Setting the upper bits makes checking for bypass addresses
13328c2ecf20Sopenharmony_ci	** a little faster later on.
13338c2ecf20Sopenharmony_ci	*/
13348c2ecf20Sopenharmony_ci	ioc->imask |= 0xFFFFFFFF00000000UL;
13358c2ecf20Sopenharmony_ci#endif
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	/* Set I/O PDIR Page size to system page size */
13388c2ecf20Sopenharmony_ci	switch (PAGE_SHIFT) {
13398c2ecf20Sopenharmony_ci		case 12: tcnfg = 0; break;	/*  4K */
13408c2ecf20Sopenharmony_ci		case 13: tcnfg = 1; break;	/*  8K */
13418c2ecf20Sopenharmony_ci		case 14: tcnfg = 2; break;	/* 16K */
13428c2ecf20Sopenharmony_ci		case 16: tcnfg = 3; break;	/* 64K */
13438c2ecf20Sopenharmony_ci		default:
13448c2ecf20Sopenharmony_ci			panic(__FILE__ "Unsupported system page size %d",
13458c2ecf20Sopenharmony_ci				1 << PAGE_SHIFT);
13468c2ecf20Sopenharmony_ci			break;
13478c2ecf20Sopenharmony_ci	}
13488c2ecf20Sopenharmony_ci	WRITE_REG(tcnfg, ioc->ioc_hpa + IOC_TCNFG);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	/*
13518c2ecf20Sopenharmony_ci	** Program the IOC's ibase and enable IOVA translation
13528c2ecf20Sopenharmony_ci	** Bit zero == enable bit.
13538c2ecf20Sopenharmony_ci	*/
13548c2ecf20Sopenharmony_ci	WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa + IOC_IBASE);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	/*
13578c2ecf20Sopenharmony_ci	** Clear I/O TLB of any possible entries.
13588c2ecf20Sopenharmony_ci	** (Yes. This is a bit paranoid...but so what)
13598c2ecf20Sopenharmony_ci	*/
13608c2ecf20Sopenharmony_ci	WRITE_REG(ioc->ibase | 31, ioc->ioc_hpa + IOC_PCOM);
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci#ifdef SBA_AGP_SUPPORT
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	/*
13658c2ecf20Sopenharmony_ci	** If an AGP device is present, only use half of the IOV space
13668c2ecf20Sopenharmony_ci	** for PCI DMA.  Unfortunately we can't know ahead of time
13678c2ecf20Sopenharmony_ci	** whether GART support will actually be used, for now we
13688c2ecf20Sopenharmony_ci	** can just key on any AGP device found in the system.
13698c2ecf20Sopenharmony_ci	** We program the next pdir index after we stop w/ a key for
13708c2ecf20Sopenharmony_ci	** the GART code to handshake on.
13718c2ecf20Sopenharmony_ci	*/
13728c2ecf20Sopenharmony_ci	device_for_each_child(&sba->dev, &agp_found, sba_ioc_find_quicksilver);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	if (agp_found && sba_reserve_agpgart) {
13758c2ecf20Sopenharmony_ci		printk(KERN_INFO "%s: reserving %dMb of IOVA space for agpgart\n",
13768c2ecf20Sopenharmony_ci		       __func__, (iova_space_size/2) >> 20);
13778c2ecf20Sopenharmony_ci		ioc->pdir_size /= 2;
13788c2ecf20Sopenharmony_ci		ioc->pdir_base[PDIR_INDEX(iova_space_size/2)] = SBA_AGPGART_COOKIE;
13798c2ecf20Sopenharmony_ci	}
13808c2ecf20Sopenharmony_ci#endif /*SBA_AGP_SUPPORT*/
13818c2ecf20Sopenharmony_ci}
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_cistatic void
13848c2ecf20Sopenharmony_cisba_ioc_init(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
13858c2ecf20Sopenharmony_ci{
13868c2ecf20Sopenharmony_ci	u32 iova_space_size, iova_space_mask;
13878c2ecf20Sopenharmony_ci	unsigned int pdir_size, iov_order, tcnfg;
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	/*
13908c2ecf20Sopenharmony_ci	** Determine IOVA Space size from memory size.
13918c2ecf20Sopenharmony_ci	**
13928c2ecf20Sopenharmony_ci	** Ideally, PCI drivers would register the maximum number
13938c2ecf20Sopenharmony_ci	** of DMA they can have outstanding for each device they
13948c2ecf20Sopenharmony_ci	** own.  Next best thing would be to guess how much DMA
13958c2ecf20Sopenharmony_ci	** can be outstanding based on PCI Class/sub-class. Both
13968c2ecf20Sopenharmony_ci	** methods still require some "extra" to support PCI
13978c2ecf20Sopenharmony_ci	** Hot-Plug/Removal of PCI cards. (aka PCI OLARD).
13988c2ecf20Sopenharmony_ci	**
13998c2ecf20Sopenharmony_ci	** While we have 32-bits "IOVA" space, top two 2 bits are used
14008c2ecf20Sopenharmony_ci	** for DMA hints - ergo only 30 bits max.
14018c2ecf20Sopenharmony_ci	*/
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	iova_space_size = (u32) (totalram_pages()/global_ioc_cnt);
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	/* limit IOVA space size to 1MB-1GB */
14068c2ecf20Sopenharmony_ci	if (iova_space_size < (1 << (20 - PAGE_SHIFT))) {
14078c2ecf20Sopenharmony_ci		iova_space_size = 1 << (20 - PAGE_SHIFT);
14088c2ecf20Sopenharmony_ci	}
14098c2ecf20Sopenharmony_ci	else if (iova_space_size > (1 << (30 - PAGE_SHIFT))) {
14108c2ecf20Sopenharmony_ci		iova_space_size = 1 << (30 - PAGE_SHIFT);
14118c2ecf20Sopenharmony_ci	}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	/*
14148c2ecf20Sopenharmony_ci	** iova space must be log2() in size.
14158c2ecf20Sopenharmony_ci	** thus, pdir/res_map will also be log2().
14168c2ecf20Sopenharmony_ci	** PIRANHA BUG: Exception is when IO Pdir is 2MB (gets reduced)
14178c2ecf20Sopenharmony_ci	*/
14188c2ecf20Sopenharmony_ci	iov_order = get_order(iova_space_size << PAGE_SHIFT);
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	/* iova_space_size is now bytes, not pages */
14218c2ecf20Sopenharmony_ci	iova_space_size = 1 << (iov_order + PAGE_SHIFT);
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	ioc->pdir_size = pdir_size = (iova_space_size/IOVP_SIZE) * sizeof(u64);
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	DBG_INIT("%s() hpa 0x%lx mem %ldMB IOV %dMB (%d bits)\n",
14268c2ecf20Sopenharmony_ci			__func__,
14278c2ecf20Sopenharmony_ci			ioc->ioc_hpa,
14288c2ecf20Sopenharmony_ci			(unsigned long) totalram_pages() >> (20 - PAGE_SHIFT),
14298c2ecf20Sopenharmony_ci			iova_space_size>>20,
14308c2ecf20Sopenharmony_ci			iov_order + PAGE_SHIFT);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	ioc->pdir_base = sba_alloc_pdir(pdir_size);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	DBG_INIT("%s() pdir %p size %x\n",
14358c2ecf20Sopenharmony_ci			__func__, ioc->pdir_base, pdir_size);
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci#ifdef SBA_HINT_SUPPORT
14388c2ecf20Sopenharmony_ci	/* FIXME : DMA HINTs not used */
14398c2ecf20Sopenharmony_ci	ioc->hint_shift_pdir = iov_order + PAGE_SHIFT;
14408c2ecf20Sopenharmony_ci	ioc->hint_mask_pdir = ~(0x3 << (iov_order + PAGE_SHIFT));
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	DBG_INIT("	hint_shift_pdir %x hint_mask_pdir %lx\n",
14438c2ecf20Sopenharmony_ci			ioc->hint_shift_pdir, ioc->hint_mask_pdir);
14448c2ecf20Sopenharmony_ci#endif
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	WRITE_REG64(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	/* build IMASK for IOC and Elroy */
14498c2ecf20Sopenharmony_ci	iova_space_mask =  0xffffffff;
14508c2ecf20Sopenharmony_ci	iova_space_mask <<= (iov_order + PAGE_SHIFT);
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	/*
14538c2ecf20Sopenharmony_ci	** On C3000 w/512MB mem, HP-UX 10.20 reports:
14548c2ecf20Sopenharmony_ci	**     ibase=0, imask=0xFE000000, size=0x2000000.
14558c2ecf20Sopenharmony_ci	*/
14568c2ecf20Sopenharmony_ci	ioc->ibase = 0;
14578c2ecf20Sopenharmony_ci	ioc->imask = iova_space_mask;	/* save it */
14588c2ecf20Sopenharmony_ci#ifdef ZX1_SUPPORT
14598c2ecf20Sopenharmony_ci	ioc->iovp_mask = ~(iova_space_mask + PAGE_SIZE - 1);
14608c2ecf20Sopenharmony_ci#endif
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	DBG_INIT("%s() IOV base 0x%lx mask 0x%0lx\n",
14638c2ecf20Sopenharmony_ci		__func__, ioc->ibase, ioc->imask);
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	/*
14668c2ecf20Sopenharmony_ci	** FIXME: Hint registers are programmed with default hint
14678c2ecf20Sopenharmony_ci	** values during boot, so hints should be sane even if we
14688c2ecf20Sopenharmony_ci	** can't reprogram them the way drivers want.
14698c2ecf20Sopenharmony_ci	*/
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	setup_ibase_imask(sba, ioc, ioc_num);
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	/*
14748c2ecf20Sopenharmony_ci	** Program the IOC's ibase and enable IOVA translation
14758c2ecf20Sopenharmony_ci	*/
14768c2ecf20Sopenharmony_ci	WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa+IOC_IBASE);
14778c2ecf20Sopenharmony_ci	WRITE_REG(ioc->imask, ioc->ioc_hpa+IOC_IMASK);
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	/* Set I/O PDIR Page size to system page size */
14808c2ecf20Sopenharmony_ci	switch (PAGE_SHIFT) {
14818c2ecf20Sopenharmony_ci		case 12: tcnfg = 0; break;	/*  4K */
14828c2ecf20Sopenharmony_ci		case 13: tcnfg = 1; break;	/*  8K */
14838c2ecf20Sopenharmony_ci		case 14: tcnfg = 2; break;	/* 16K */
14848c2ecf20Sopenharmony_ci		case 16: tcnfg = 3; break;	/* 64K */
14858c2ecf20Sopenharmony_ci		default:
14868c2ecf20Sopenharmony_ci			panic(__FILE__ "Unsupported system page size %d",
14878c2ecf20Sopenharmony_ci				1 << PAGE_SHIFT);
14888c2ecf20Sopenharmony_ci			break;
14898c2ecf20Sopenharmony_ci	}
14908c2ecf20Sopenharmony_ci	/* Set I/O PDIR Page size to PAGE_SIZE (4k/16k/...) */
14918c2ecf20Sopenharmony_ci	WRITE_REG(tcnfg, ioc->ioc_hpa+IOC_TCNFG);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	/*
14948c2ecf20Sopenharmony_ci	** Clear I/O TLB of any possible entries.
14958c2ecf20Sopenharmony_ci	** (Yes. This is a bit paranoid...but so what)
14968c2ecf20Sopenharmony_ci	*/
14978c2ecf20Sopenharmony_ci	WRITE_REG(0 | 31, ioc->ioc_hpa+IOC_PCOM);
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	ioc->ibase = 0; /* used by SBA_IOVA and related macros */
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	DBG_INIT("%s() DONE\n", __func__);
15028c2ecf20Sopenharmony_ci}
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci/**************************************************************************
15078c2ecf20Sopenharmony_ci**
15088c2ecf20Sopenharmony_ci**   SBA initialization code (HW and SW)
15098c2ecf20Sopenharmony_ci**
15108c2ecf20Sopenharmony_ci**   o identify SBA chip itself
15118c2ecf20Sopenharmony_ci**   o initialize SBA chip modes (HardFail)
15128c2ecf20Sopenharmony_ci**   o initialize SBA chip modes (HardFail)
15138c2ecf20Sopenharmony_ci**   o FIXME: initialize DMA hints for reasonable defaults
15148c2ecf20Sopenharmony_ci**
15158c2ecf20Sopenharmony_ci**************************************************************************/
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_cistatic void __iomem *ioc_remap(struct sba_device *sba_dev, unsigned int offset)
15188c2ecf20Sopenharmony_ci{
15198c2ecf20Sopenharmony_ci	return ioremap(sba_dev->dev->hpa.start + offset, SBA_FUNC_SIZE);
15208c2ecf20Sopenharmony_ci}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_cistatic void sba_hw_init(struct sba_device *sba_dev)
15238c2ecf20Sopenharmony_ci{
15248c2ecf20Sopenharmony_ci	int i;
15258c2ecf20Sopenharmony_ci	int num_ioc;
15268c2ecf20Sopenharmony_ci	u64 ioc_ctl;
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	if (!is_pdc_pat()) {
15298c2ecf20Sopenharmony_ci		/* Shutdown the USB controller on Astro-based workstations.
15308c2ecf20Sopenharmony_ci		** Once we reprogram the IOMMU, the next DMA performed by
15318c2ecf20Sopenharmony_ci		** USB will HPMC the box. USB is only enabled if a
15328c2ecf20Sopenharmony_ci		** keyboard is present and found.
15338c2ecf20Sopenharmony_ci		**
15348c2ecf20Sopenharmony_ci		** With serial console, j6k v5.0 firmware says:
15358c2ecf20Sopenharmony_ci		**   mem_kbd hpa 0xfee003f8 sba 0x0 pad 0x0 cl_class 0x7
15368c2ecf20Sopenharmony_ci		**
15378c2ecf20Sopenharmony_ci		** FIXME: Using GFX+USB console at power up but direct
15388c2ecf20Sopenharmony_ci		**	linux to serial console is still broken.
15398c2ecf20Sopenharmony_ci		**	USB could generate DMA so we must reset USB.
15408c2ecf20Sopenharmony_ci		**	The proper sequence would be:
15418c2ecf20Sopenharmony_ci		**	o block console output
15428c2ecf20Sopenharmony_ci		**	o reset USB device
15438c2ecf20Sopenharmony_ci		**	o reprogram serial port
15448c2ecf20Sopenharmony_ci		**	o unblock console output
15458c2ecf20Sopenharmony_ci		*/
15468c2ecf20Sopenharmony_ci		if (PAGE0->mem_kbd.cl_class == CL_KEYBD) {
15478c2ecf20Sopenharmony_ci			pdc_io_reset_devices();
15488c2ecf20Sopenharmony_ci		}
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	}
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci#if 0
15548c2ecf20Sopenharmony_ciprintk("sba_hw_init(): mem_boot 0x%x 0x%x 0x%x 0x%x\n", PAGE0->mem_boot.hpa,
15558c2ecf20Sopenharmony_ci	PAGE0->mem_boot.spa, PAGE0->mem_boot.pad, PAGE0->mem_boot.cl_class);
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	/*
15588c2ecf20Sopenharmony_ci	** Need to deal with DMA from LAN.
15598c2ecf20Sopenharmony_ci	**	Maybe use page zero boot device as a handle to talk
15608c2ecf20Sopenharmony_ci	**	to PDC about which device to shutdown.
15618c2ecf20Sopenharmony_ci	**
15628c2ecf20Sopenharmony_ci	** Netbooting, j6k v5.0 firmware says:
15638c2ecf20Sopenharmony_ci	** 	mem_boot hpa 0xf4008000 sba 0x0 pad 0x0 cl_class 0x1002
15648c2ecf20Sopenharmony_ci	** ARGH! invalid class.
15658c2ecf20Sopenharmony_ci	*/
15668c2ecf20Sopenharmony_ci	if ((PAGE0->mem_boot.cl_class != CL_RANDOM)
15678c2ecf20Sopenharmony_ci		&& (PAGE0->mem_boot.cl_class != CL_SEQU)) {
15688c2ecf20Sopenharmony_ci			pdc_io_reset();
15698c2ecf20Sopenharmony_ci	}
15708c2ecf20Sopenharmony_ci#endif
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (!IS_PLUTO(sba_dev->dev)) {
15738c2ecf20Sopenharmony_ci		ioc_ctl = READ_REG(sba_dev->sba_hpa+IOC_CTRL);
15748c2ecf20Sopenharmony_ci		DBG_INIT("%s() hpa 0x%lx ioc_ctl 0x%Lx ->",
15758c2ecf20Sopenharmony_ci			__func__, sba_dev->sba_hpa, ioc_ctl);
15768c2ecf20Sopenharmony_ci		ioc_ctl &= ~(IOC_CTRL_RM | IOC_CTRL_NC | IOC_CTRL_CE);
15778c2ecf20Sopenharmony_ci		ioc_ctl |= IOC_CTRL_DD | IOC_CTRL_D4 | IOC_CTRL_TC;
15788c2ecf20Sopenharmony_ci			/* j6700 v1.6 firmware sets 0x294f */
15798c2ecf20Sopenharmony_ci			/* A500 firmware sets 0x4d */
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci		WRITE_REG(ioc_ctl, sba_dev->sba_hpa+IOC_CTRL);
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_INIT
15848c2ecf20Sopenharmony_ci		ioc_ctl = READ_REG64(sba_dev->sba_hpa+IOC_CTRL);
15858c2ecf20Sopenharmony_ci		DBG_INIT(" 0x%Lx\n", ioc_ctl);
15868c2ecf20Sopenharmony_ci#endif
15878c2ecf20Sopenharmony_ci	} /* if !PLUTO */
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	if (IS_ASTRO(sba_dev->dev)) {
15908c2ecf20Sopenharmony_ci		int err;
15918c2ecf20Sopenharmony_ci		sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, ASTRO_IOC_OFFSET);
15928c2ecf20Sopenharmony_ci		num_ioc = 1;
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci		sba_dev->chip_resv.name = "Astro Intr Ack";
15958c2ecf20Sopenharmony_ci		sba_dev->chip_resv.start = PCI_F_EXTEND | 0xfef00000UL;
15968c2ecf20Sopenharmony_ci		sba_dev->chip_resv.end   = PCI_F_EXTEND | (0xff000000UL - 1) ;
15978c2ecf20Sopenharmony_ci		err = request_resource(&iomem_resource, &(sba_dev->chip_resv));
15988c2ecf20Sopenharmony_ci		BUG_ON(err < 0);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	} else if (IS_PLUTO(sba_dev->dev)) {
16018c2ecf20Sopenharmony_ci		int err;
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci		sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, PLUTO_IOC_OFFSET);
16048c2ecf20Sopenharmony_ci		num_ioc = 1;
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci		sba_dev->chip_resv.name = "Pluto Intr/PIOP/VGA";
16078c2ecf20Sopenharmony_ci		sba_dev->chip_resv.start = PCI_F_EXTEND | 0xfee00000UL;
16088c2ecf20Sopenharmony_ci		sba_dev->chip_resv.end   = PCI_F_EXTEND | (0xff200000UL - 1);
16098c2ecf20Sopenharmony_ci		err = request_resource(&iomem_resource, &(sba_dev->chip_resv));
16108c2ecf20Sopenharmony_ci		WARN_ON(err < 0);
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci		sba_dev->iommu_resv.name = "IOVA Space";
16138c2ecf20Sopenharmony_ci		sba_dev->iommu_resv.start = 0x40000000UL;
16148c2ecf20Sopenharmony_ci		sba_dev->iommu_resv.end   = 0x50000000UL - 1;
16158c2ecf20Sopenharmony_ci		err = request_resource(&iomem_resource, &(sba_dev->iommu_resv));
16168c2ecf20Sopenharmony_ci		WARN_ON(err < 0);
16178c2ecf20Sopenharmony_ci	} else {
16188c2ecf20Sopenharmony_ci		/* IKE, REO */
16198c2ecf20Sopenharmony_ci		sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, IKE_IOC_OFFSET(0));
16208c2ecf20Sopenharmony_ci		sba_dev->ioc[1].ioc_hpa = ioc_remap(sba_dev, IKE_IOC_OFFSET(1));
16218c2ecf20Sopenharmony_ci		num_ioc = 2;
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci		/* TODO - LOOKUP Ike/Stretch chipset mem map */
16248c2ecf20Sopenharmony_ci	}
16258c2ecf20Sopenharmony_ci	/* XXX: What about Reo Grande? */
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	sba_dev->num_ioc = num_ioc;
16288c2ecf20Sopenharmony_ci	for (i = 0; i < num_ioc; i++) {
16298c2ecf20Sopenharmony_ci		void __iomem *ioc_hpa = sba_dev->ioc[i].ioc_hpa;
16308c2ecf20Sopenharmony_ci		unsigned int j;
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci		for (j=0; j < sizeof(u64) * ROPES_PER_IOC; j+=sizeof(u64)) {
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci			/*
16358c2ecf20Sopenharmony_ci			 * Clear ROPE(N)_CONFIG AO bit.
16368c2ecf20Sopenharmony_ci			 * Disables "NT Ordering" (~= !"Relaxed Ordering")
16378c2ecf20Sopenharmony_ci			 * Overrides bit 1 in DMA Hint Sets.
16388c2ecf20Sopenharmony_ci			 * Improves netperf UDP_STREAM by ~10% for bcm5701.
16398c2ecf20Sopenharmony_ci			 */
16408c2ecf20Sopenharmony_ci			if (IS_PLUTO(sba_dev->dev)) {
16418c2ecf20Sopenharmony_ci				void __iomem *rope_cfg;
16428c2ecf20Sopenharmony_ci				unsigned long cfg_val;
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci				rope_cfg = ioc_hpa + IOC_ROPE0_CFG + j;
16458c2ecf20Sopenharmony_ci				cfg_val = READ_REG(rope_cfg);
16468c2ecf20Sopenharmony_ci				cfg_val &= ~IOC_ROPE_AO;
16478c2ecf20Sopenharmony_ci				WRITE_REG(cfg_val, rope_cfg);
16488c2ecf20Sopenharmony_ci			}
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci			/*
16518c2ecf20Sopenharmony_ci			** Make sure the box crashes on rope errors.
16528c2ecf20Sopenharmony_ci			*/
16538c2ecf20Sopenharmony_ci			WRITE_REG(HF_ENABLE, ioc_hpa + ROPE0_CTL + j);
16548c2ecf20Sopenharmony_ci		}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci		/* flush out the last writes */
16578c2ecf20Sopenharmony_ci		READ_REG(sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci		DBG_INIT("	ioc[%d] ROPE_CFG 0x%Lx  ROPE_DBG 0x%Lx\n",
16608c2ecf20Sopenharmony_ci				i,
16618c2ecf20Sopenharmony_ci				READ_REG(sba_dev->ioc[i].ioc_hpa + 0x40),
16628c2ecf20Sopenharmony_ci				READ_REG(sba_dev->ioc[i].ioc_hpa + 0x50)
16638c2ecf20Sopenharmony_ci			);
16648c2ecf20Sopenharmony_ci		DBG_INIT("	STATUS_CONTROL 0x%Lx  FLUSH_CTRL 0x%Lx\n",
16658c2ecf20Sopenharmony_ci				READ_REG(sba_dev->ioc[i].ioc_hpa + 0x108),
16668c2ecf20Sopenharmony_ci				READ_REG(sba_dev->ioc[i].ioc_hpa + 0x400)
16678c2ecf20Sopenharmony_ci			);
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci		if (IS_PLUTO(sba_dev->dev)) {
16708c2ecf20Sopenharmony_ci			sba_ioc_init_pluto(sba_dev->dev, &(sba_dev->ioc[i]), i);
16718c2ecf20Sopenharmony_ci		} else {
16728c2ecf20Sopenharmony_ci			sba_ioc_init(sba_dev->dev, &(sba_dev->ioc[i]), i);
16738c2ecf20Sopenharmony_ci		}
16748c2ecf20Sopenharmony_ci	}
16758c2ecf20Sopenharmony_ci}
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_cistatic void
16788c2ecf20Sopenharmony_cisba_common_init(struct sba_device *sba_dev)
16798c2ecf20Sopenharmony_ci{
16808c2ecf20Sopenharmony_ci	int i;
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ci	/* add this one to the head of the list (order doesn't matter)
16838c2ecf20Sopenharmony_ci	** This will be useful for debugging - especially if we get coredumps
16848c2ecf20Sopenharmony_ci	*/
16858c2ecf20Sopenharmony_ci	sba_dev->next = sba_list;
16868c2ecf20Sopenharmony_ci	sba_list = sba_dev;
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	for(i=0; i< sba_dev->num_ioc; i++) {
16898c2ecf20Sopenharmony_ci		int res_size;
16908c2ecf20Sopenharmony_ci#ifdef DEBUG_DMB_TRAP
16918c2ecf20Sopenharmony_ci		extern void iterate_pages(unsigned long , unsigned long ,
16928c2ecf20Sopenharmony_ci					  void (*)(pte_t * , unsigned long),
16938c2ecf20Sopenharmony_ci					  unsigned long );
16948c2ecf20Sopenharmony_ci		void set_data_memory_break(pte_t * , unsigned long);
16958c2ecf20Sopenharmony_ci#endif
16968c2ecf20Sopenharmony_ci		/* resource map size dictated by pdir_size */
16978c2ecf20Sopenharmony_ci		res_size = sba_dev->ioc[i].pdir_size/sizeof(u64); /* entries */
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci		/* Second part of PIRANHA BUG */
17008c2ecf20Sopenharmony_ci		if (piranha_bad_128k) {
17018c2ecf20Sopenharmony_ci			res_size -= (128*1024)/sizeof(u64);
17028c2ecf20Sopenharmony_ci		}
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci		res_size >>= 3;  /* convert bit count to byte count */
17058c2ecf20Sopenharmony_ci		DBG_INIT("%s() res_size 0x%x\n",
17068c2ecf20Sopenharmony_ci			__func__, res_size);
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci		sba_dev->ioc[i].res_size = res_size;
17098c2ecf20Sopenharmony_ci		sba_dev->ioc[i].res_map = (char *) __get_free_pages(GFP_KERNEL, get_order(res_size));
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci#ifdef DEBUG_DMB_TRAP
17128c2ecf20Sopenharmony_ci		iterate_pages( sba_dev->ioc[i].res_map, res_size,
17138c2ecf20Sopenharmony_ci				set_data_memory_break, 0);
17148c2ecf20Sopenharmony_ci#endif
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci		if (NULL == sba_dev->ioc[i].res_map)
17178c2ecf20Sopenharmony_ci		{
17188c2ecf20Sopenharmony_ci			panic("%s:%s() could not allocate resource map\n",
17198c2ecf20Sopenharmony_ci			      __FILE__, __func__ );
17208c2ecf20Sopenharmony_ci		}
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ci		memset(sba_dev->ioc[i].res_map, 0, res_size);
17238c2ecf20Sopenharmony_ci		/* next available IOVP - circular search */
17248c2ecf20Sopenharmony_ci		sba_dev->ioc[i].res_hint = (unsigned long *)
17258c2ecf20Sopenharmony_ci				&(sba_dev->ioc[i].res_map[L1_CACHE_BYTES]);
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci#ifdef ASSERT_PDIR_SANITY
17288c2ecf20Sopenharmony_ci		/* Mark first bit busy - ie no IOVA 0 */
17298c2ecf20Sopenharmony_ci		sba_dev->ioc[i].res_map[0] = 0x80;
17308c2ecf20Sopenharmony_ci		sba_dev->ioc[i].pdir_base[0] = 0xeeffc0addbba0080ULL;
17318c2ecf20Sopenharmony_ci#endif
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci		/* Third (and last) part of PIRANHA BUG */
17348c2ecf20Sopenharmony_ci		if (piranha_bad_128k) {
17358c2ecf20Sopenharmony_ci			/* region from +1408K to +1536 is un-usable. */
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci			int idx_start = (1408*1024/sizeof(u64)) >> 3;
17388c2ecf20Sopenharmony_ci			int idx_end   = (1536*1024/sizeof(u64)) >> 3;
17398c2ecf20Sopenharmony_ci			long *p_start = (long *) &(sba_dev->ioc[i].res_map[idx_start]);
17408c2ecf20Sopenharmony_ci			long *p_end   = (long *) &(sba_dev->ioc[i].res_map[idx_end]);
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci			/* mark that part of the io pdir busy */
17438c2ecf20Sopenharmony_ci			while (p_start < p_end)
17448c2ecf20Sopenharmony_ci				*p_start++ = -1;
17458c2ecf20Sopenharmony_ci
17468c2ecf20Sopenharmony_ci		}
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci#ifdef DEBUG_DMB_TRAP
17498c2ecf20Sopenharmony_ci		iterate_pages( sba_dev->ioc[i].res_map, res_size,
17508c2ecf20Sopenharmony_ci				set_data_memory_break, 0);
17518c2ecf20Sopenharmony_ci		iterate_pages( sba_dev->ioc[i].pdir_base, sba_dev->ioc[i].pdir_size,
17528c2ecf20Sopenharmony_ci				set_data_memory_break, 0);
17538c2ecf20Sopenharmony_ci#endif
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci		DBG_INIT("%s() %d res_map %x %p\n",
17568c2ecf20Sopenharmony_ci			__func__, i, res_size, sba_dev->ioc[i].res_map);
17578c2ecf20Sopenharmony_ci	}
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	spin_lock_init(&sba_dev->sba_lock);
17608c2ecf20Sopenharmony_ci	ioc_needs_fdc = boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC;
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci#ifdef DEBUG_SBA_INIT
17638c2ecf20Sopenharmony_ci	/*
17648c2ecf20Sopenharmony_ci	 * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
17658c2ecf20Sopenharmony_ci	 * (bit #61, big endian), we have to flush and sync every time
17668c2ecf20Sopenharmony_ci	 * IO-PDIR is changed in Ike/Astro.
17678c2ecf20Sopenharmony_ci	 */
17688c2ecf20Sopenharmony_ci	if (ioc_needs_fdc) {
17698c2ecf20Sopenharmony_ci		printk(KERN_INFO MODULE_NAME " FDC/SYNC required.\n");
17708c2ecf20Sopenharmony_ci	} else {
17718c2ecf20Sopenharmony_ci		printk(KERN_INFO MODULE_NAME " IOC has cache coherent PDIR.\n");
17728c2ecf20Sopenharmony_ci	}
17738c2ecf20Sopenharmony_ci#endif
17748c2ecf20Sopenharmony_ci}
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
17778c2ecf20Sopenharmony_cistatic int sba_proc_info(struct seq_file *m, void *p)
17788c2ecf20Sopenharmony_ci{
17798c2ecf20Sopenharmony_ci	struct sba_device *sba_dev = sba_list;
17808c2ecf20Sopenharmony_ci	struct ioc *ioc = &sba_dev->ioc[0];	/* FIXME: Multi-IOC support! */
17818c2ecf20Sopenharmony_ci	int total_pages = (int) (ioc->res_size << 3); /* 8 bits per byte */
17828c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
17838c2ecf20Sopenharmony_ci	unsigned long avg = 0, min, max;
17848c2ecf20Sopenharmony_ci#endif
17858c2ecf20Sopenharmony_ci	int i;
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci	seq_printf(m, "%s rev %d.%d\n",
17888c2ecf20Sopenharmony_ci		   sba_dev->name,
17898c2ecf20Sopenharmony_ci		   (sba_dev->hw_rev & 0x7) + 1,
17908c2ecf20Sopenharmony_ci		   (sba_dev->hw_rev & 0x18) >> 3);
17918c2ecf20Sopenharmony_ci	seq_printf(m, "IO PDIR size    : %d bytes (%d entries)\n",
17928c2ecf20Sopenharmony_ci		   (int)((ioc->res_size << 3) * sizeof(u64)), /* 8 bits/byte */
17938c2ecf20Sopenharmony_ci		   total_pages);
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_ci	seq_printf(m, "Resource bitmap : %d bytes (%d pages)\n",
17968c2ecf20Sopenharmony_ci		   ioc->res_size, ioc->res_size << 3);   /* 8 bits per byte */
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	seq_printf(m, "LMMIO_BASE/MASK/ROUTE %08x %08x %08x\n",
17998c2ecf20Sopenharmony_ci		   READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_BASE),
18008c2ecf20Sopenharmony_ci		   READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_MASK),
18018c2ecf20Sopenharmony_ci		   READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_ROUTE));
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci	for (i=0; i<4; i++)
18048c2ecf20Sopenharmony_ci		seq_printf(m, "DIR%d_BASE/MASK/ROUTE %08x %08x %08x\n",
18058c2ecf20Sopenharmony_ci			   i,
18068c2ecf20Sopenharmony_ci			   READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_BASE  + i*0x18),
18078c2ecf20Sopenharmony_ci			   READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_MASK  + i*0x18),
18088c2ecf20Sopenharmony_ci			   READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_ROUTE + i*0x18));
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_ci#ifdef SBA_COLLECT_STATS
18118c2ecf20Sopenharmony_ci	seq_printf(m, "IO PDIR entries : %ld free  %ld used (%d%%)\n",
18128c2ecf20Sopenharmony_ci		   total_pages - ioc->used_pages, ioc->used_pages,
18138c2ecf20Sopenharmony_ci		   (int)(ioc->used_pages * 100 / total_pages));
18148c2ecf20Sopenharmony_ci
18158c2ecf20Sopenharmony_ci	min = max = ioc->avg_search[0];
18168c2ecf20Sopenharmony_ci	for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
18178c2ecf20Sopenharmony_ci		avg += ioc->avg_search[i];
18188c2ecf20Sopenharmony_ci		if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
18198c2ecf20Sopenharmony_ci		if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
18208c2ecf20Sopenharmony_ci	}
18218c2ecf20Sopenharmony_ci	avg /= SBA_SEARCH_SAMPLE;
18228c2ecf20Sopenharmony_ci	seq_printf(m, "  Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)\n",
18238c2ecf20Sopenharmony_ci		   min, avg, max);
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	seq_printf(m, "pci_map_single(): %12ld calls  %12ld pages (avg %d/1000)\n",
18268c2ecf20Sopenharmony_ci		   ioc->msingle_calls, ioc->msingle_pages,
18278c2ecf20Sopenharmony_ci		   (int)((ioc->msingle_pages * 1000)/ioc->msingle_calls));
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci	/* KLUGE - unmap_sg calls unmap_single for each mapped page */
18308c2ecf20Sopenharmony_ci	min = ioc->usingle_calls;
18318c2ecf20Sopenharmony_ci	max = ioc->usingle_pages - ioc->usg_pages;
18328c2ecf20Sopenharmony_ci	seq_printf(m, "pci_unmap_single: %12ld calls  %12ld pages (avg %d/1000)\n",
18338c2ecf20Sopenharmony_ci		   min, max, (int)((max * 1000)/min));
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci	seq_printf(m, "pci_map_sg()    : %12ld calls  %12ld pages (avg %d/1000)\n",
18368c2ecf20Sopenharmony_ci		   ioc->msg_calls, ioc->msg_pages,
18378c2ecf20Sopenharmony_ci		   (int)((ioc->msg_pages * 1000)/ioc->msg_calls));
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_ci	seq_printf(m, "pci_unmap_sg()  : %12ld calls  %12ld pages (avg %d/1000)\n",
18408c2ecf20Sopenharmony_ci		   ioc->usg_calls, ioc->usg_pages,
18418c2ecf20Sopenharmony_ci		   (int)((ioc->usg_pages * 1000)/ioc->usg_calls));
18428c2ecf20Sopenharmony_ci#endif
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	return 0;
18458c2ecf20Sopenharmony_ci}
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_cistatic int
18488c2ecf20Sopenharmony_cisba_proc_bitmap_info(struct seq_file *m, void *p)
18498c2ecf20Sopenharmony_ci{
18508c2ecf20Sopenharmony_ci	struct sba_device *sba_dev = sba_list;
18518c2ecf20Sopenharmony_ci	struct ioc *ioc = &sba_dev->ioc[0];	/* FIXME: Multi-IOC support! */
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	seq_hex_dump(m, "   ", DUMP_PREFIX_NONE, 32, 4, ioc->res_map,
18548c2ecf20Sopenharmony_ci		     ioc->res_size, false);
18558c2ecf20Sopenharmony_ci	seq_putc(m, '\n');
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	return 0;
18588c2ecf20Sopenharmony_ci}
18598c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_cistatic const struct parisc_device_id sba_tbl[] __initconst = {
18628c2ecf20Sopenharmony_ci	{ HPHW_IOA, HVERSION_REV_ANY_ID, ASTRO_RUNWAY_PORT, 0xb },
18638c2ecf20Sopenharmony_ci	{ HPHW_BCPORT, HVERSION_REV_ANY_ID, IKE_MERCED_PORT, 0xc },
18648c2ecf20Sopenharmony_ci	{ HPHW_BCPORT, HVERSION_REV_ANY_ID, REO_MERCED_PORT, 0xc },
18658c2ecf20Sopenharmony_ci	{ HPHW_BCPORT, HVERSION_REV_ANY_ID, REOG_MERCED_PORT, 0xc },
18668c2ecf20Sopenharmony_ci	{ HPHW_IOA, HVERSION_REV_ANY_ID, PLUTO_MCKINLEY_PORT, 0xc },
18678c2ecf20Sopenharmony_ci	{ 0, }
18688c2ecf20Sopenharmony_ci};
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_cistatic int sba_driver_callback(struct parisc_device *);
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_cistatic struct parisc_driver sba_driver __refdata = {
18738c2ecf20Sopenharmony_ci	.name =		MODULE_NAME,
18748c2ecf20Sopenharmony_ci	.id_table =	sba_tbl,
18758c2ecf20Sopenharmony_ci	.probe =	sba_driver_callback,
18768c2ecf20Sopenharmony_ci};
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci/*
18798c2ecf20Sopenharmony_ci** Determine if sba should claim this chip (return 0) or not (return 1).
18808c2ecf20Sopenharmony_ci** If so, initialize the chip and tell other partners in crime they
18818c2ecf20Sopenharmony_ci** have work to do.
18828c2ecf20Sopenharmony_ci*/
18838c2ecf20Sopenharmony_cistatic int __init sba_driver_callback(struct parisc_device *dev)
18848c2ecf20Sopenharmony_ci{
18858c2ecf20Sopenharmony_ci	struct sba_device *sba_dev;
18868c2ecf20Sopenharmony_ci	u32 func_class;
18878c2ecf20Sopenharmony_ci	int i;
18888c2ecf20Sopenharmony_ci	char *version;
18898c2ecf20Sopenharmony_ci	void __iomem *sba_addr = ioremap(dev->hpa.start, SBA_FUNC_SIZE);
18908c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
18918c2ecf20Sopenharmony_ci	struct proc_dir_entry *root;
18928c2ecf20Sopenharmony_ci#endif
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci	sba_dump_ranges(sba_addr);
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	/* Read HW Rev First */
18978c2ecf20Sopenharmony_ci	func_class = READ_REG(sba_addr + SBA_FCLASS);
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci	if (IS_ASTRO(dev)) {
19008c2ecf20Sopenharmony_ci		unsigned long fclass;
19018c2ecf20Sopenharmony_ci		static char astro_rev[]="Astro ?.?";
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci		/* Astro is broken...Read HW Rev First */
19048c2ecf20Sopenharmony_ci		fclass = READ_REG(sba_addr);
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci		astro_rev[6] = '1' + (char) (fclass & 0x7);
19078c2ecf20Sopenharmony_ci		astro_rev[8] = '0' + (char) ((fclass & 0x18) >> 3);
19088c2ecf20Sopenharmony_ci		version = astro_rev;
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	} else if (IS_IKE(dev)) {
19118c2ecf20Sopenharmony_ci		static char ike_rev[] = "Ike rev ?";
19128c2ecf20Sopenharmony_ci		ike_rev[8] = '0' + (char) (func_class & 0xff);
19138c2ecf20Sopenharmony_ci		version = ike_rev;
19148c2ecf20Sopenharmony_ci	} else if (IS_PLUTO(dev)) {
19158c2ecf20Sopenharmony_ci		static char pluto_rev[]="Pluto ?.?";
19168c2ecf20Sopenharmony_ci		pluto_rev[6] = '0' + (char) ((func_class & 0xf0) >> 4);
19178c2ecf20Sopenharmony_ci		pluto_rev[8] = '0' + (char) (func_class & 0x0f);
19188c2ecf20Sopenharmony_ci		version = pluto_rev;
19198c2ecf20Sopenharmony_ci	} else {
19208c2ecf20Sopenharmony_ci		static char reo_rev[] = "REO rev ?";
19218c2ecf20Sopenharmony_ci		reo_rev[8] = '0' + (char) (func_class & 0xff);
19228c2ecf20Sopenharmony_ci		version = reo_rev;
19238c2ecf20Sopenharmony_ci	}
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci	if (!global_ioc_cnt) {
19268c2ecf20Sopenharmony_ci		global_ioc_cnt = count_parisc_driver(&sba_driver);
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci		/* Astro and Pluto have one IOC per SBA */
19298c2ecf20Sopenharmony_ci		if ((!IS_ASTRO(dev)) || (!IS_PLUTO(dev)))
19308c2ecf20Sopenharmony_ci			global_ioc_cnt *= 2;
19318c2ecf20Sopenharmony_ci	}
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci	printk(KERN_INFO "%s found %s at 0x%llx\n",
19348c2ecf20Sopenharmony_ci		MODULE_NAME, version, (unsigned long long)dev->hpa.start);
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci	sba_dev = kzalloc(sizeof(struct sba_device), GFP_KERNEL);
19378c2ecf20Sopenharmony_ci	if (!sba_dev) {
19388c2ecf20Sopenharmony_ci		printk(KERN_ERR MODULE_NAME " - couldn't alloc sba_device\n");
19398c2ecf20Sopenharmony_ci		return -ENOMEM;
19408c2ecf20Sopenharmony_ci	}
19418c2ecf20Sopenharmony_ci
19428c2ecf20Sopenharmony_ci	parisc_set_drvdata(dev, sba_dev);
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci	for(i=0; i<MAX_IOC; i++)
19458c2ecf20Sopenharmony_ci		spin_lock_init(&(sba_dev->ioc[i].res_lock));
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	sba_dev->dev = dev;
19488c2ecf20Sopenharmony_ci	sba_dev->hw_rev = func_class;
19498c2ecf20Sopenharmony_ci	sba_dev->name = dev->name;
19508c2ecf20Sopenharmony_ci	sba_dev->sba_hpa = sba_addr;
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci	sba_get_pat_resources(sba_dev);
19538c2ecf20Sopenharmony_ci	sba_hw_init(sba_dev);
19548c2ecf20Sopenharmony_ci	sba_common_init(sba_dev);
19558c2ecf20Sopenharmony_ci
19568c2ecf20Sopenharmony_ci	hppa_dma_ops = &sba_ops;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
19598c2ecf20Sopenharmony_ci	switch (dev->id.hversion) {
19608c2ecf20Sopenharmony_ci	case PLUTO_MCKINLEY_PORT:
19618c2ecf20Sopenharmony_ci		root = proc_mckinley_root;
19628c2ecf20Sopenharmony_ci		break;
19638c2ecf20Sopenharmony_ci	case ASTRO_RUNWAY_PORT:
19648c2ecf20Sopenharmony_ci	case IKE_MERCED_PORT:
19658c2ecf20Sopenharmony_ci	default:
19668c2ecf20Sopenharmony_ci		root = proc_runway_root;
19678c2ecf20Sopenharmony_ci		break;
19688c2ecf20Sopenharmony_ci	}
19698c2ecf20Sopenharmony_ci
19708c2ecf20Sopenharmony_ci	proc_create_single("sba_iommu", 0, root, sba_proc_info);
19718c2ecf20Sopenharmony_ci	proc_create_single("sba_iommu-bitmap", 0, root, sba_proc_bitmap_info);
19728c2ecf20Sopenharmony_ci#endif
19738c2ecf20Sopenharmony_ci	return 0;
19748c2ecf20Sopenharmony_ci}
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci/*
19778c2ecf20Sopenharmony_ci** One time initialization to let the world know the SBA was found.
19788c2ecf20Sopenharmony_ci** This is the only routine which is NOT static.
19798c2ecf20Sopenharmony_ci** Must be called exactly once before pci_init().
19808c2ecf20Sopenharmony_ci*/
19818c2ecf20Sopenharmony_civoid __init sba_init(void)
19828c2ecf20Sopenharmony_ci{
19838c2ecf20Sopenharmony_ci	register_parisc_driver(&sba_driver);
19848c2ecf20Sopenharmony_ci}
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_ci/**
19888c2ecf20Sopenharmony_ci * sba_get_iommu - Assign the iommu pointer for the pci bus controller.
19898c2ecf20Sopenharmony_ci * @dev: The parisc device.
19908c2ecf20Sopenharmony_ci *
19918c2ecf20Sopenharmony_ci * Returns the appropriate IOMMU data for the given parisc PCI controller.
19928c2ecf20Sopenharmony_ci * This is cached and used later for PCI DMA Mapping.
19938c2ecf20Sopenharmony_ci */
19948c2ecf20Sopenharmony_civoid * sba_get_iommu(struct parisc_device *pci_hba)
19958c2ecf20Sopenharmony_ci{
19968c2ecf20Sopenharmony_ci	struct parisc_device *sba_dev = parisc_parent(pci_hba);
19978c2ecf20Sopenharmony_ci	struct sba_device *sba = dev_get_drvdata(&sba_dev->dev);
19988c2ecf20Sopenharmony_ci	char t = sba_dev->id.hw_type;
19998c2ecf20Sopenharmony_ci	int iocnum = (pci_hba->hw_path >> 3);	/* rope # */
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci	WARN_ON((t != HPHW_IOA) && (t != HPHW_BCPORT));
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	return &(sba->ioc[iocnum]);
20048c2ecf20Sopenharmony_ci}
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci/**
20088c2ecf20Sopenharmony_ci * sba_directed_lmmio - return first directed LMMIO range routed to rope
20098c2ecf20Sopenharmony_ci * @pa_dev: The parisc device.
20108c2ecf20Sopenharmony_ci * @r: resource PCI host controller wants start/end fields assigned.
20118c2ecf20Sopenharmony_ci *
20128c2ecf20Sopenharmony_ci * For the given parisc PCI controller, determine if any direct ranges
20138c2ecf20Sopenharmony_ci * are routed down the corresponding rope.
20148c2ecf20Sopenharmony_ci */
20158c2ecf20Sopenharmony_civoid sba_directed_lmmio(struct parisc_device *pci_hba, struct resource *r)
20168c2ecf20Sopenharmony_ci{
20178c2ecf20Sopenharmony_ci	struct parisc_device *sba_dev = parisc_parent(pci_hba);
20188c2ecf20Sopenharmony_ci	struct sba_device *sba = dev_get_drvdata(&sba_dev->dev);
20198c2ecf20Sopenharmony_ci	char t = sba_dev->id.hw_type;
20208c2ecf20Sopenharmony_ci	int i;
20218c2ecf20Sopenharmony_ci	int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1));  /* rope # */
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci	BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci	r->start = r->end = 0;
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	/* Astro has 4 directed ranges. Not sure about Ike/Pluto/et al */
20288c2ecf20Sopenharmony_ci	for (i=0; i<4; i++) {
20298c2ecf20Sopenharmony_ci		int base, size;
20308c2ecf20Sopenharmony_ci		void __iomem *reg = sba->sba_hpa + i*0x18;
20318c2ecf20Sopenharmony_ci
20328c2ecf20Sopenharmony_ci		base = READ_REG32(reg + LMMIO_DIRECT0_BASE);
20338c2ecf20Sopenharmony_ci		if ((base & 1) == 0)
20348c2ecf20Sopenharmony_ci			continue;	/* not enabled */
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci		size = READ_REG32(reg + LMMIO_DIRECT0_ROUTE);
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_ci		if ((size & (ROPES_PER_IOC-1)) != rope)
20398c2ecf20Sopenharmony_ci			continue;	/* directed down different rope */
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci		r->start = (base & ~1UL) | PCI_F_EXTEND;
20428c2ecf20Sopenharmony_ci		size = ~ READ_REG32(reg + LMMIO_DIRECT0_MASK);
20438c2ecf20Sopenharmony_ci		r->end = r->start + size;
20448c2ecf20Sopenharmony_ci		r->flags = IORESOURCE_MEM;
20458c2ecf20Sopenharmony_ci	}
20468c2ecf20Sopenharmony_ci}
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_ci
20498c2ecf20Sopenharmony_ci/**
20508c2ecf20Sopenharmony_ci * sba_distributed_lmmio - return portion of distributed LMMIO range
20518c2ecf20Sopenharmony_ci * @pa_dev: The parisc device.
20528c2ecf20Sopenharmony_ci * @r: resource PCI host controller wants start/end fields assigned.
20538c2ecf20Sopenharmony_ci *
20548c2ecf20Sopenharmony_ci * For the given parisc PCI controller, return portion of distributed LMMIO
20558c2ecf20Sopenharmony_ci * range. The distributed LMMIO is always present and it's just a question
20568c2ecf20Sopenharmony_ci * of the base address and size of the range.
20578c2ecf20Sopenharmony_ci */
20588c2ecf20Sopenharmony_civoid sba_distributed_lmmio(struct parisc_device *pci_hba, struct resource *r )
20598c2ecf20Sopenharmony_ci{
20608c2ecf20Sopenharmony_ci	struct parisc_device *sba_dev = parisc_parent(pci_hba);
20618c2ecf20Sopenharmony_ci	struct sba_device *sba = dev_get_drvdata(&sba_dev->dev);
20628c2ecf20Sopenharmony_ci	char t = sba_dev->id.hw_type;
20638c2ecf20Sopenharmony_ci	int base, size;
20648c2ecf20Sopenharmony_ci	int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1));  /* rope # */
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_ci	BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci	r->start = r->end = 0;
20698c2ecf20Sopenharmony_ci
20708c2ecf20Sopenharmony_ci	base = READ_REG32(sba->sba_hpa + LMMIO_DIST_BASE);
20718c2ecf20Sopenharmony_ci	if ((base & 1) == 0) {
20728c2ecf20Sopenharmony_ci		BUG();	/* Gah! Distr Range wasn't enabled! */
20738c2ecf20Sopenharmony_ci		return;
20748c2ecf20Sopenharmony_ci	}
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	r->start = (base & ~1UL) | PCI_F_EXTEND;
20778c2ecf20Sopenharmony_ci
20788c2ecf20Sopenharmony_ci	size = (~READ_REG32(sba->sba_hpa + LMMIO_DIST_MASK)) / ROPES_PER_IOC;
20798c2ecf20Sopenharmony_ci	r->start += rope * (size + 1);	/* adjust base for this rope */
20808c2ecf20Sopenharmony_ci	r->end = r->start + size;
20818c2ecf20Sopenharmony_ci	r->flags = IORESOURCE_MEM;
20828c2ecf20Sopenharmony_ci}
2083