18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HP zx1 AGPGART routines.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
68c2ecf20Sopenharmony_ci *	Bjorn Helgaas <bjorn.helgaas@hp.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/acpi.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/agp_backend.h>
148c2ecf20Sopenharmony_ci#include <linux/log2.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/acpi-ext.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "agp.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define HP_ZX1_IOC_OFFSET	0x1000  /* ACPI reports SBA, we want IOC */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* HP ZX1 IOC registers */
248c2ecf20Sopenharmony_ci#define HP_ZX1_IBASE		0x300
258c2ecf20Sopenharmony_ci#define HP_ZX1_IMASK		0x308
268c2ecf20Sopenharmony_ci#define HP_ZX1_PCOM		0x310
278c2ecf20Sopenharmony_ci#define HP_ZX1_TCNFG		0x318
288c2ecf20Sopenharmony_ci#define HP_ZX1_PDIR_BASE	0x320
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define HP_ZX1_IOVA_BASE	GB(1UL)
318c2ecf20Sopenharmony_ci#define HP_ZX1_IOVA_SIZE	GB(1UL)
328c2ecf20Sopenharmony_ci#define HP_ZX1_GART_SIZE	(HP_ZX1_IOVA_SIZE / 2)
338c2ecf20Sopenharmony_ci#define HP_ZX1_SBA_IOMMU_COOKIE	0x0000badbadc0ffeeUL
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define HP_ZX1_PDIR_VALID_BIT	0x8000000000000000UL
368c2ecf20Sopenharmony_ci#define HP_ZX1_IOVA_TO_PDIR(va)	((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define AGP8X_MODE_BIT		3
398c2ecf20Sopenharmony_ci#define AGP8X_MODE		(1 << AGP8X_MODE_BIT)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* AGP bridge need not be PCI device, but DRM thinks it is. */
428c2ecf20Sopenharmony_cistatic struct pci_dev fake_bridge_dev;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int hp_zx1_gart_found;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct aper_size_info_fixed hp_zx1_sizes[] =
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	{0, 0, 0},		/* filled in by hp_zx1_fetch_size() */
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct gatt_mask hp_zx1_masks[] =
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	{.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct _hp_private {
578c2ecf20Sopenharmony_ci	volatile u8 __iomem *ioc_regs;
588c2ecf20Sopenharmony_ci	volatile u8 __iomem *lba_regs;
598c2ecf20Sopenharmony_ci	int lba_cap_offset;
608c2ecf20Sopenharmony_ci	u64 *io_pdir;		// PDIR for entire IOVA
618c2ecf20Sopenharmony_ci	u64 *gatt;		// PDIR just for GART (subset of above)
628c2ecf20Sopenharmony_ci	u64 gatt_entries;
638c2ecf20Sopenharmony_ci	u64 iova_base;
648c2ecf20Sopenharmony_ci	u64 gart_base;
658c2ecf20Sopenharmony_ci	u64 gart_size;
668c2ecf20Sopenharmony_ci	u64 io_pdir_size;
678c2ecf20Sopenharmony_ci	int io_pdir_owner;	// do we own it, or share it with sba_iommu?
688c2ecf20Sopenharmony_ci	int io_page_size;
698c2ecf20Sopenharmony_ci	int io_tlb_shift;
708c2ecf20Sopenharmony_ci	int io_tlb_ps;		// IOC ps config
718c2ecf20Sopenharmony_ci	int io_pages_per_kpage;
728c2ecf20Sopenharmony_ci} hp_private;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int __init hp_zx1_ioc_shared(void)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * IOC already configured by sba_iommu module; just use
828c2ecf20Sopenharmony_ci	 * its setup.  We assume:
838c2ecf20Sopenharmony_ci	 *	- IOVA space is 1Gb in size
848c2ecf20Sopenharmony_ci	 *	- first 512Mb is IOMMU, second 512Mb is GART
858c2ecf20Sopenharmony_ci	 */
868c2ecf20Sopenharmony_ci	hp->io_tlb_ps = readq(hp->ioc_regs+HP_ZX1_TCNFG);
878c2ecf20Sopenharmony_ci	switch (hp->io_tlb_ps) {
888c2ecf20Sopenharmony_ci		case 0: hp->io_tlb_shift = 12; break;
898c2ecf20Sopenharmony_ci		case 1: hp->io_tlb_shift = 13; break;
908c2ecf20Sopenharmony_ci		case 2: hp->io_tlb_shift = 14; break;
918c2ecf20Sopenharmony_ci		case 3: hp->io_tlb_shift = 16; break;
928c2ecf20Sopenharmony_ci		default:
938c2ecf20Sopenharmony_ci			printk(KERN_ERR PFX "Invalid IOTLB page size "
948c2ecf20Sopenharmony_ci			       "configuration 0x%x\n", hp->io_tlb_ps);
958c2ecf20Sopenharmony_ci			hp->gatt = NULL;
968c2ecf20Sopenharmony_ci			hp->gatt_entries = 0;
978c2ecf20Sopenharmony_ci			return -ENODEV;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	hp->io_page_size = 1 << hp->io_tlb_shift;
1008c2ecf20Sopenharmony_ci	hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	hp->iova_base = readq(hp->ioc_regs+HP_ZX1_IBASE) & ~0x1;
1038c2ecf20Sopenharmony_ci	hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	hp->gart_size = HP_ZX1_GART_SIZE;
1068c2ecf20Sopenharmony_ci	hp->gatt_entries = hp->gart_size / hp->io_page_size;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	hp->io_pdir = phys_to_virt(readq(hp->ioc_regs+HP_ZX1_PDIR_BASE));
1098c2ecf20Sopenharmony_ci	hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
1128c2ecf20Sopenharmony_ci		/* Normal case when no AGP device in system */
1138c2ecf20Sopenharmony_ci		hp->gatt = NULL;
1148c2ecf20Sopenharmony_ci		hp->gatt_entries = 0;
1158c2ecf20Sopenharmony_ci		printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
1168c2ecf20Sopenharmony_ci		       "GART disabled\n");
1178c2ecf20Sopenharmony_ci		return -ENODEV;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int __init
1248c2ecf20Sopenharmony_cihp_zx1_ioc_owner (void)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Select an IOV page size no larger than system page size.
1328c2ecf20Sopenharmony_ci	 */
1338c2ecf20Sopenharmony_ci	if (PAGE_SIZE >= KB(64)) {
1348c2ecf20Sopenharmony_ci		hp->io_tlb_shift = 16;
1358c2ecf20Sopenharmony_ci		hp->io_tlb_ps = 3;
1368c2ecf20Sopenharmony_ci	} else if (PAGE_SIZE >= KB(16)) {
1378c2ecf20Sopenharmony_ci		hp->io_tlb_shift = 14;
1388c2ecf20Sopenharmony_ci		hp->io_tlb_ps = 2;
1398c2ecf20Sopenharmony_ci	} else if (PAGE_SIZE >= KB(8)) {
1408c2ecf20Sopenharmony_ci		hp->io_tlb_shift = 13;
1418c2ecf20Sopenharmony_ci		hp->io_tlb_ps = 1;
1428c2ecf20Sopenharmony_ci	} else {
1438c2ecf20Sopenharmony_ci		hp->io_tlb_shift = 12;
1448c2ecf20Sopenharmony_ci		hp->io_tlb_ps = 0;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci	hp->io_page_size = 1 << hp->io_tlb_shift;
1478c2ecf20Sopenharmony_ci	hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	hp->iova_base = HP_ZX1_IOVA_BASE;
1508c2ecf20Sopenharmony_ci	hp->gart_size = HP_ZX1_GART_SIZE;
1518c2ecf20Sopenharmony_ci	hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	hp->gatt_entries = hp->gart_size / hp->io_page_size;
1548c2ecf20Sopenharmony_ci	hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return 0;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic int __init
1608c2ecf20Sopenharmony_cihp_zx1_ioc_init (u64 hpa)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	hp->ioc_regs = ioremap(hpa, 1024);
1658c2ecf20Sopenharmony_ci	if (!hp->ioc_regs)
1668c2ecf20Sopenharmony_ci		return -ENOMEM;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/*
1698c2ecf20Sopenharmony_ci	 * If the IOTLB is currently disabled, we can take it over.
1708c2ecf20Sopenharmony_ci	 * Otherwise, we have to share with sba_iommu.
1718c2ecf20Sopenharmony_ci	 */
1728c2ecf20Sopenharmony_ci	hp->io_pdir_owner = (readq(hp->ioc_regs+HP_ZX1_IBASE) & 0x1) == 0;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	if (hp->io_pdir_owner)
1758c2ecf20Sopenharmony_ci		return hp_zx1_ioc_owner();
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	return hp_zx1_ioc_shared();
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic int
1818c2ecf20Sopenharmony_cihp_zx1_lba_find_capability (volatile u8 __iomem *hpa, int cap)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	u16 status;
1848c2ecf20Sopenharmony_ci	u8 pos, id;
1858c2ecf20Sopenharmony_ci	int ttl = 48;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	status = readw(hpa+PCI_STATUS);
1888c2ecf20Sopenharmony_ci	if (!(status & PCI_STATUS_CAP_LIST))
1898c2ecf20Sopenharmony_ci		return 0;
1908c2ecf20Sopenharmony_ci	pos = readb(hpa+PCI_CAPABILITY_LIST);
1918c2ecf20Sopenharmony_ci	while (ttl-- && pos >= 0x40) {
1928c2ecf20Sopenharmony_ci		pos &= ~3;
1938c2ecf20Sopenharmony_ci		id = readb(hpa+pos+PCI_CAP_LIST_ID);
1948c2ecf20Sopenharmony_ci		if (id == 0xff)
1958c2ecf20Sopenharmony_ci			break;
1968c2ecf20Sopenharmony_ci		if (id == cap)
1978c2ecf20Sopenharmony_ci			return pos;
1988c2ecf20Sopenharmony_ci		pos = readb(hpa+pos+PCI_CAP_LIST_NEXT);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic int __init
2048c2ecf20Sopenharmony_cihp_zx1_lba_init (u64 hpa)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
2078c2ecf20Sopenharmony_ci	int cap;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	hp->lba_regs = ioremap(hpa, 256);
2108c2ecf20Sopenharmony_ci	if (!hp->lba_regs)
2118c2ecf20Sopenharmony_ci		return -ENOMEM;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	hp->lba_cap_offset = hp_zx1_lba_find_capability(hp->lba_regs, PCI_CAP_ID_AGP);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	cap = readl(hp->lba_regs+hp->lba_cap_offset) & 0xff;
2168c2ecf20Sopenharmony_ci	if (cap != PCI_CAP_ID_AGP) {
2178c2ecf20Sopenharmony_ci		printk(KERN_ERR PFX "Invalid capability ID 0x%02x at 0x%x\n",
2188c2ecf20Sopenharmony_ci		       cap, hp->lba_cap_offset);
2198c2ecf20Sopenharmony_ci		iounmap(hp->lba_regs);
2208c2ecf20Sopenharmony_ci		return -ENODEV;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int
2278c2ecf20Sopenharmony_cihp_zx1_fetch_size(void)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	int size;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	size = hp_private.gart_size / MB(1);
2328c2ecf20Sopenharmony_ci	hp_zx1_sizes[0].size = size;
2338c2ecf20Sopenharmony_ci	agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
2348c2ecf20Sopenharmony_ci	return size;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic int
2388c2ecf20Sopenharmony_cihp_zx1_configure (void)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	agp_bridge->gart_bus_addr = hp->gart_base;
2438c2ecf20Sopenharmony_ci	agp_bridge->capndx = hp->lba_cap_offset;
2448c2ecf20Sopenharmony_ci	agp_bridge->mode = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (hp->io_pdir_owner) {
2478c2ecf20Sopenharmony_ci		writel(virt_to_phys(hp->io_pdir), hp->ioc_regs+HP_ZX1_PDIR_BASE);
2488c2ecf20Sopenharmony_ci		readl(hp->ioc_regs+HP_ZX1_PDIR_BASE);
2498c2ecf20Sopenharmony_ci		writel(hp->io_tlb_ps, hp->ioc_regs+HP_ZX1_TCNFG);
2508c2ecf20Sopenharmony_ci		readl(hp->ioc_regs+HP_ZX1_TCNFG);
2518c2ecf20Sopenharmony_ci		writel((unsigned int)(~(HP_ZX1_IOVA_SIZE-1)), hp->ioc_regs+HP_ZX1_IMASK);
2528c2ecf20Sopenharmony_ci		readl(hp->ioc_regs+HP_ZX1_IMASK);
2538c2ecf20Sopenharmony_ci		writel(hp->iova_base|1, hp->ioc_regs+HP_ZX1_IBASE);
2548c2ecf20Sopenharmony_ci		readl(hp->ioc_regs+HP_ZX1_IBASE);
2558c2ecf20Sopenharmony_ci		writel(hp->iova_base|ilog2(HP_ZX1_IOVA_SIZE), hp->ioc_regs+HP_ZX1_PCOM);
2568c2ecf20Sopenharmony_ci		readl(hp->ioc_regs+HP_ZX1_PCOM);
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	return 0;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic void
2638c2ecf20Sopenharmony_cihp_zx1_cleanup (void)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (hp->ioc_regs) {
2688c2ecf20Sopenharmony_ci		if (hp->io_pdir_owner) {
2698c2ecf20Sopenharmony_ci			writeq(0, hp->ioc_regs+HP_ZX1_IBASE);
2708c2ecf20Sopenharmony_ci			readq(hp->ioc_regs+HP_ZX1_IBASE);
2718c2ecf20Sopenharmony_ci		}
2728c2ecf20Sopenharmony_ci		iounmap(hp->ioc_regs);
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci	if (hp->lba_regs)
2758c2ecf20Sopenharmony_ci		iounmap(hp->lba_regs);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void
2798c2ecf20Sopenharmony_cihp_zx1_tlbflush (struct agp_memory *mem)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	writeq(hp->gart_base | ilog2(hp->gart_size), hp->ioc_regs+HP_ZX1_PCOM);
2848c2ecf20Sopenharmony_ci	readq(hp->ioc_regs+HP_ZX1_PCOM);
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int
2888c2ecf20Sopenharmony_cihp_zx1_create_gatt_table (struct agp_bridge_data *bridge)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
2918c2ecf20Sopenharmony_ci	int i;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (hp->io_pdir_owner) {
2948c2ecf20Sopenharmony_ci		hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
2958c2ecf20Sopenharmony_ci						get_order(hp->io_pdir_size));
2968c2ecf20Sopenharmony_ci		if (!hp->io_pdir) {
2978c2ecf20Sopenharmony_ci			printk(KERN_ERR PFX "Couldn't allocate contiguous "
2988c2ecf20Sopenharmony_ci				"memory for I/O PDIR\n");
2998c2ecf20Sopenharmony_ci			hp->gatt = NULL;
3008c2ecf20Sopenharmony_ci			hp->gatt_entries = 0;
3018c2ecf20Sopenharmony_ci			return -ENOMEM;
3028c2ecf20Sopenharmony_ci		}
3038c2ecf20Sopenharmony_ci		memset(hp->io_pdir, 0, hp->io_pdir_size);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	for (i = 0; i < hp->gatt_entries; i++) {
3098c2ecf20Sopenharmony_ci		hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return 0;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int
3168c2ecf20Sopenharmony_cihp_zx1_free_gatt_table (struct agp_bridge_data *bridge)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (hp->io_pdir_owner)
3218c2ecf20Sopenharmony_ci		free_pages((unsigned long) hp->io_pdir,
3228c2ecf20Sopenharmony_ci			    get_order(hp->io_pdir_size));
3238c2ecf20Sopenharmony_ci	else
3248c2ecf20Sopenharmony_ci		hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
3258c2ecf20Sopenharmony_ci	return 0;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int
3298c2ecf20Sopenharmony_cihp_zx1_insert_memory (struct agp_memory *mem, off_t pg_start, int type)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
3328c2ecf20Sopenharmony_ci	int i, k;
3338c2ecf20Sopenharmony_ci	off_t j, io_pg_start;
3348c2ecf20Sopenharmony_ci	int io_pg_count;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (type != mem->type ||
3378c2ecf20Sopenharmony_ci		agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
3388c2ecf20Sopenharmony_ci		return -EINVAL;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	io_pg_start = hp->io_pages_per_kpage * pg_start;
3428c2ecf20Sopenharmony_ci	io_pg_count = hp->io_pages_per_kpage * mem->page_count;
3438c2ecf20Sopenharmony_ci	if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
3448c2ecf20Sopenharmony_ci		return -EINVAL;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	j = io_pg_start;
3488c2ecf20Sopenharmony_ci	while (j < (io_pg_start + io_pg_count)) {
3498c2ecf20Sopenharmony_ci		if (hp->gatt[j]) {
3508c2ecf20Sopenharmony_ci			return -EBUSY;
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci		j++;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (!mem->is_flushed) {
3568c2ecf20Sopenharmony_ci		global_cache_flush();
3578c2ecf20Sopenharmony_ci		mem->is_flushed = true;
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
3618c2ecf20Sopenharmony_ci		unsigned long paddr;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		paddr = page_to_phys(mem->pages[i]);
3648c2ecf20Sopenharmony_ci		for (k = 0;
3658c2ecf20Sopenharmony_ci		     k < hp->io_pages_per_kpage;
3668c2ecf20Sopenharmony_ci		     k++, j++, paddr += hp->io_page_size) {
3678c2ecf20Sopenharmony_ci			hp->gatt[j] = HP_ZX1_PDIR_VALID_BIT | paddr;
3688c2ecf20Sopenharmony_ci		}
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	agp_bridge->driver->tlb_flush(mem);
3728c2ecf20Sopenharmony_ci	return 0;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int
3768c2ecf20Sopenharmony_cihp_zx1_remove_memory (struct agp_memory *mem, off_t pg_start, int type)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
3798c2ecf20Sopenharmony_ci	int i, io_pg_start, io_pg_count;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (type != mem->type ||
3828c2ecf20Sopenharmony_ci		agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
3838c2ecf20Sopenharmony_ci		return -EINVAL;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	io_pg_start = hp->io_pages_per_kpage * pg_start;
3878c2ecf20Sopenharmony_ci	io_pg_count = hp->io_pages_per_kpage * mem->page_count;
3888c2ecf20Sopenharmony_ci	for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
3898c2ecf20Sopenharmony_ci		hp->gatt[i] = agp_bridge->scratch_page;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	agp_bridge->driver->tlb_flush(mem);
3938c2ecf20Sopenharmony_ci	return 0;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic unsigned long
3978c2ecf20Sopenharmony_cihp_zx1_mask_memory (struct agp_bridge_data *bridge, dma_addr_t addr, int type)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	return HP_ZX1_PDIR_VALID_BIT | addr;
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic void
4038c2ecf20Sopenharmony_cihp_zx1_enable (struct agp_bridge_data *bridge, u32 mode)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct _hp_private *hp = &hp_private;
4068c2ecf20Sopenharmony_ci	u32 command;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	command = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
4098c2ecf20Sopenharmony_ci	command = agp_collect_device_status(bridge, mode, command);
4108c2ecf20Sopenharmony_ci	command |= 0x00000100;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	writel(command, hp->lba_regs+hp->lba_cap_offset+PCI_AGP_COMMAND);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	agp_device_command(command, (mode & AGP8X_MODE) != 0);
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciconst struct agp_bridge_driver hp_zx1_driver = {
4188c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
4198c2ecf20Sopenharmony_ci	.size_type		= FIXED_APER_SIZE,
4208c2ecf20Sopenharmony_ci	.configure		= hp_zx1_configure,
4218c2ecf20Sopenharmony_ci	.fetch_size		= hp_zx1_fetch_size,
4228c2ecf20Sopenharmony_ci	.cleanup		= hp_zx1_cleanup,
4238c2ecf20Sopenharmony_ci	.tlb_flush		= hp_zx1_tlbflush,
4248c2ecf20Sopenharmony_ci	.mask_memory		= hp_zx1_mask_memory,
4258c2ecf20Sopenharmony_ci	.masks			= hp_zx1_masks,
4268c2ecf20Sopenharmony_ci	.agp_enable		= hp_zx1_enable,
4278c2ecf20Sopenharmony_ci	.cache_flush		= global_cache_flush,
4288c2ecf20Sopenharmony_ci	.create_gatt_table	= hp_zx1_create_gatt_table,
4298c2ecf20Sopenharmony_ci	.free_gatt_table	= hp_zx1_free_gatt_table,
4308c2ecf20Sopenharmony_ci	.insert_memory		= hp_zx1_insert_memory,
4318c2ecf20Sopenharmony_ci	.remove_memory		= hp_zx1_remove_memory,
4328c2ecf20Sopenharmony_ci	.alloc_by_type		= agp_generic_alloc_by_type,
4338c2ecf20Sopenharmony_ci	.free_by_type		= agp_generic_free_by_type,
4348c2ecf20Sopenharmony_ci	.agp_alloc_page		= agp_generic_alloc_page,
4358c2ecf20Sopenharmony_ci	.agp_alloc_pages	= agp_generic_alloc_pages,
4368c2ecf20Sopenharmony_ci	.agp_destroy_page	= agp_generic_destroy_page,
4378c2ecf20Sopenharmony_ci	.agp_destroy_pages	= agp_generic_destroy_pages,
4388c2ecf20Sopenharmony_ci	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
4398c2ecf20Sopenharmony_ci	.cant_use_aperture	= true,
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic int __init
4438c2ecf20Sopenharmony_cihp_zx1_setup (u64 ioc_hpa, u64 lba_hpa)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct agp_bridge_data *bridge;
4468c2ecf20Sopenharmony_ci	int error = 0;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	error = hp_zx1_ioc_init(ioc_hpa);
4498c2ecf20Sopenharmony_ci	if (error)
4508c2ecf20Sopenharmony_ci		goto fail;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	error = hp_zx1_lba_init(lba_hpa);
4538c2ecf20Sopenharmony_ci	if (error)
4548c2ecf20Sopenharmony_ci		goto fail;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	bridge = agp_alloc_bridge();
4578c2ecf20Sopenharmony_ci	if (!bridge) {
4588c2ecf20Sopenharmony_ci		error = -ENOMEM;
4598c2ecf20Sopenharmony_ci		goto fail;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci	bridge->driver = &hp_zx1_driver;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	fake_bridge_dev.vendor = PCI_VENDOR_ID_HP;
4648c2ecf20Sopenharmony_ci	fake_bridge_dev.device = PCI_DEVICE_ID_HP_PCIX_LBA;
4658c2ecf20Sopenharmony_ci	bridge->dev = &fake_bridge_dev;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	error = agp_add_bridge(bridge);
4688c2ecf20Sopenharmony_ci  fail:
4698c2ecf20Sopenharmony_ci	if (error)
4708c2ecf20Sopenharmony_ci		hp_zx1_cleanup();
4718c2ecf20Sopenharmony_ci	return error;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic acpi_status __init
4758c2ecf20Sopenharmony_cizx1_gart_probe (acpi_handle obj, u32 depth, void *context, void **ret)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	acpi_handle handle, parent;
4788c2ecf20Sopenharmony_ci	acpi_status status;
4798c2ecf20Sopenharmony_ci	struct acpi_device_info *info;
4808c2ecf20Sopenharmony_ci	u64 lba_hpa, sba_hpa, length;
4818c2ecf20Sopenharmony_ci	int match;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	status = hp_acpi_csr_space(obj, &lba_hpa, &length);
4848c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(status))
4858c2ecf20Sopenharmony_ci		return AE_OK; /* keep looking for another bridge */
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/* Look for an enclosing IOC scope and find its CSR space */
4888c2ecf20Sopenharmony_ci	handle = obj;
4898c2ecf20Sopenharmony_ci	do {
4908c2ecf20Sopenharmony_ci		status = acpi_get_object_info(handle, &info);
4918c2ecf20Sopenharmony_ci		if (ACPI_SUCCESS(status) && (info->valid & ACPI_VALID_HID)) {
4928c2ecf20Sopenharmony_ci			/* TBD check _CID also */
4938c2ecf20Sopenharmony_ci			match = (strcmp(info->hardware_id.string, "HWP0001") == 0);
4948c2ecf20Sopenharmony_ci			kfree(info);
4958c2ecf20Sopenharmony_ci			if (match) {
4968c2ecf20Sopenharmony_ci				status = hp_acpi_csr_space(handle, &sba_hpa, &length);
4978c2ecf20Sopenharmony_ci				if (ACPI_SUCCESS(status))
4988c2ecf20Sopenharmony_ci					break;
4998c2ecf20Sopenharmony_ci				else {
5008c2ecf20Sopenharmony_ci					printk(KERN_ERR PFX "Detected HP ZX1 "
5018c2ecf20Sopenharmony_ci					       "AGP LBA but no IOC.\n");
5028c2ecf20Sopenharmony_ci					return AE_OK;
5038c2ecf20Sopenharmony_ci				}
5048c2ecf20Sopenharmony_ci			}
5058c2ecf20Sopenharmony_ci		}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci		status = acpi_get_parent(handle, &parent);
5088c2ecf20Sopenharmony_ci		handle = parent;
5098c2ecf20Sopenharmony_ci	} while (ACPI_SUCCESS(status));
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(status))
5128c2ecf20Sopenharmony_ci		return AE_OK;	/* found no enclosing IOC */
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
5158c2ecf20Sopenharmony_ci		return AE_OK;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	printk(KERN_INFO PFX "Detected HP ZX1 %s AGP chipset "
5188c2ecf20Sopenharmony_ci		"(ioc=%llx, lba=%llx)\n", (char *)context,
5198c2ecf20Sopenharmony_ci		sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	hp_zx1_gart_found = 1;
5228c2ecf20Sopenharmony_ci	return AE_CTRL_TERMINATE; /* we only support one bridge; quit looking */
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int __init
5268c2ecf20Sopenharmony_ciagp_hp_init (void)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	if (agp_off)
5298c2ecf20Sopenharmony_ci		return -EINVAL;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	acpi_get_devices("HWP0003", zx1_gart_probe, "HWP0003", NULL);
5328c2ecf20Sopenharmony_ci	if (hp_zx1_gart_found)
5338c2ecf20Sopenharmony_ci		return 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	acpi_get_devices("HWP0007", zx1_gart_probe, "HWP0007", NULL);
5368c2ecf20Sopenharmony_ci	if (hp_zx1_gart_found)
5378c2ecf20Sopenharmony_ci		return 0;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return -ENODEV;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic void __exit
5438c2ecf20Sopenharmony_ciagp_hp_cleanup (void)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cimodule_init(agp_hp_init);
5488c2ecf20Sopenharmony_cimodule_exit(agp_hp_cleanup);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL and additional rights");
551