2 * HP zx1 AGPGART routines.
4 * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/acpi.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/init.h>
16 #include <linux/agp_backend.h>
17 #include <linux/log2.h>
19 #include <asm/acpi-ext.h>
23 #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
25 /* HP ZX1 IOC registers */
26 #define HP_ZX1_IBASE 0x300
27 #define HP_ZX1_IMASK 0x308
28 #define HP_ZX1_PCOM 0x310
29 #define HP_ZX1_TCNFG 0x318
30 #define HP_ZX1_PDIR_BASE 0x320
32 #define HP_ZX1_IOVA_BASE GB(1UL)
33 #define HP_ZX1_IOVA_SIZE GB(1UL)
34 #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
35 #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
37 #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
38 #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
40 #define AGP8X_MODE_BIT 3
41 #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
43 /* AGP bridge need not be PCI device, but DRM thinks it is. */
44 static struct pci_dev fake_bridge_dev;
46 static int hp_zx1_gart_found;
48 static struct aper_size_info_fixed hp_zx1_sizes[] =
50 {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
53 static struct gatt_mask hp_zx1_masks[] =
55 {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
58 static struct _hp_private {
59 volatile u8 __iomem *ioc_regs;
60 volatile u8 __iomem *lba_regs;
62 u64 *io_pdir; // PDIR for entire IOVA
63 u64 *gatt; // PDIR just for GART (subset of above)
69 int io_pdir_owner; // do we own it, or share it with sba_iommu?
72 int io_tlb_ps; // IOC ps config
73 int io_pages_per_kpage;
76 static int __init hp_zx1_ioc_shared(void)
78 struct _hp_private *hp = &hp_private;
80 printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
83 * IOC already configured by sba_iommu module; just use
84 * its setup. We assume:
85 * - IOVA space is 1Gb in size
86 * - first 512Mb is IOMMU, second 512Mb is GART
88 hp->io_tlb_ps = readq(hp->ioc_regs+HP_ZX1_TCNFG);
89 switch (hp->io_tlb_ps) {
90 case 0: hp->io_tlb_shift = 12; break;
91 case 1: hp->io_tlb_shift = 13; break;
92 case 2: hp->io_tlb_shift = 14; break;
93 case 3: hp->io_tlb_shift = 16; break;
95 printk(KERN_ERR PFX "Invalid IOTLB page size "
96 "configuration 0x%x\n", hp->io_tlb_ps);
101 hp->io_page_size = 1 << hp->io_tlb_shift;
102 hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
104 hp->iova_base = readq(hp->ioc_regs+HP_ZX1_IBASE) & ~0x1;
105 hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
107 hp->gart_size = HP_ZX1_GART_SIZE;
108 hp->gatt_entries = hp->gart_size / hp->io_page_size;
110 hp->io_pdir = gart_to_virt(readq(hp->ioc_regs+HP_ZX1_PDIR_BASE));
111 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
113 if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
114 /* Normal case when no AGP device in system */
116 hp->gatt_entries = 0;
117 printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
126 hp_zx1_ioc_owner (void)
128 struct _hp_private *hp = &hp_private;
130 printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
133 * Select an IOV page size no larger than system page size.
135 if (PAGE_SIZE >= KB(64)) {
136 hp->io_tlb_shift = 16;
138 } else if (PAGE_SIZE >= KB(16)) {
139 hp->io_tlb_shift = 14;
141 } else if (PAGE_SIZE >= KB(8)) {
142 hp->io_tlb_shift = 13;
145 hp->io_tlb_shift = 12;
148 hp->io_page_size = 1 << hp->io_tlb_shift;
149 hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
151 hp->iova_base = HP_ZX1_IOVA_BASE;
152 hp->gart_size = HP_ZX1_GART_SIZE;
153 hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
155 hp->gatt_entries = hp->gart_size / hp->io_page_size;
156 hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
162 hp_zx1_ioc_init (u64 hpa)
164 struct _hp_private *hp = &hp_private;
166 hp->ioc_regs = ioremap(hpa, 1024);
171 * If the IOTLB is currently disabled, we can take it over.
172 * Otherwise, we have to share with sba_iommu.
174 hp->io_pdir_owner = (readq(hp->ioc_regs+HP_ZX1_IBASE) & 0x1) == 0;
176 if (hp->io_pdir_owner)
177 return hp_zx1_ioc_owner();
179 return hp_zx1_ioc_shared();
183 hp_zx1_lba_find_capability (volatile u8 __iomem *hpa, int cap)
189 status = readw(hpa+PCI_STATUS);
190 if (!(status & PCI_STATUS_CAP_LIST))
192 pos = readb(hpa+PCI_CAPABILITY_LIST);
193 while (ttl-- && pos >= 0x40) {
195 id = readb(hpa+pos+PCI_CAP_LIST_ID);
200 pos = readb(hpa+pos+PCI_CAP_LIST_NEXT);
206 hp_zx1_lba_init (u64 hpa)
208 struct _hp_private *hp = &hp_private;
211 hp->lba_regs = ioremap(hpa, 256);
215 hp->lba_cap_offset = hp_zx1_lba_find_capability(hp->lba_regs, PCI_CAP_ID_AGP);
217 cap = readl(hp->lba_regs+hp->lba_cap_offset) & 0xff;
218 if (cap != PCI_CAP_ID_AGP) {
219 printk(KERN_ERR PFX "Invalid capability ID 0x%02x at 0x%x\n",
220 cap, hp->lba_cap_offset);
221 iounmap(hp->lba_regs);
229 hp_zx1_fetch_size(void)
233 size = hp_private.gart_size / MB(1);
234 hp_zx1_sizes[0].size = size;
235 agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
240 hp_zx1_configure (void)
242 struct _hp_private *hp = &hp_private;
244 agp_bridge->gart_bus_addr = hp->gart_base;
245 agp_bridge->capndx = hp->lba_cap_offset;
246 agp_bridge->mode = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
248 if (hp->io_pdir_owner) {
249 writel(virt_to_gart(hp->io_pdir), hp->ioc_regs+HP_ZX1_PDIR_BASE);
250 readl(hp->ioc_regs+HP_ZX1_PDIR_BASE);
251 writel(hp->io_tlb_ps, hp->ioc_regs+HP_ZX1_TCNFG);
252 readl(hp->ioc_regs+HP_ZX1_TCNFG);
253 writel((unsigned int)(~(HP_ZX1_IOVA_SIZE-1)), hp->ioc_regs+HP_ZX1_IMASK);
254 readl(hp->ioc_regs+HP_ZX1_IMASK);
255 writel(hp->iova_base|1, hp->ioc_regs+HP_ZX1_IBASE);
256 readl(hp->ioc_regs+HP_ZX1_IBASE);
257 writel(hp->iova_base|ilog2(HP_ZX1_IOVA_SIZE), hp->ioc_regs+HP_ZX1_PCOM);
258 readl(hp->ioc_regs+HP_ZX1_PCOM);
265 hp_zx1_cleanup (void)
267 struct _hp_private *hp = &hp_private;
270 if (hp->io_pdir_owner) {
271 writeq(0, hp->ioc_regs+HP_ZX1_IBASE);
272 readq(hp->ioc_regs+HP_ZX1_IBASE);
274 iounmap(hp->ioc_regs);
277 iounmap(hp->lba_regs);
281 hp_zx1_tlbflush (struct agp_memory *mem)
283 struct _hp_private *hp = &hp_private;
285 writeq(hp->gart_base | ilog2(hp->gart_size), hp->ioc_regs+HP_ZX1_PCOM);
286 readq(hp->ioc_regs+HP_ZX1_PCOM);
290 hp_zx1_create_gatt_table (struct agp_bridge_data *bridge)
292 struct _hp_private *hp = &hp_private;
295 if (hp->io_pdir_owner) {
296 hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
297 get_order(hp->io_pdir_size));
299 printk(KERN_ERR PFX "Couldn't allocate contiguous "
300 "memory for I/O PDIR\n");
302 hp->gatt_entries = 0;
305 memset(hp->io_pdir, 0, hp->io_pdir_size);
307 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
310 for (i = 0; i < hp->gatt_entries; i++) {
311 hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
318 hp_zx1_free_gatt_table (struct agp_bridge_data *bridge)
320 struct _hp_private *hp = &hp_private;
322 if (hp->io_pdir_owner)
323 free_pages((unsigned long) hp->io_pdir,
324 get_order(hp->io_pdir_size));
326 hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
331 hp_zx1_insert_memory (struct agp_memory *mem, off_t pg_start, int type)
333 struct _hp_private *hp = &hp_private;
335 off_t j, io_pg_start;
338 if (type != 0 || mem->type != 0) {
342 io_pg_start = hp->io_pages_per_kpage * pg_start;
343 io_pg_count = hp->io_pages_per_kpage * mem->page_count;
344 if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
349 while (j < (io_pg_start + io_pg_count)) {
356 if (!mem->is_flushed) {
357 global_cache_flush();
358 mem->is_flushed = true;
361 for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
364 paddr = mem->memory[i];
366 k < hp->io_pages_per_kpage;
367 k++, j++, paddr += hp->io_page_size) {
369 agp_bridge->driver->mask_memory(agp_bridge,
374 agp_bridge->driver->tlb_flush(mem);
379 hp_zx1_remove_memory (struct agp_memory *mem, off_t pg_start, int type)
381 struct _hp_private *hp = &hp_private;
382 int i, io_pg_start, io_pg_count;
384 if (type != 0 || mem->type != 0) {
388 io_pg_start = hp->io_pages_per_kpage * pg_start;
389 io_pg_count = hp->io_pages_per_kpage * mem->page_count;
390 for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
391 hp->gatt[i] = agp_bridge->scratch_page;
394 agp_bridge->driver->tlb_flush(mem);
399 hp_zx1_mask_memory (struct agp_bridge_data *bridge,
400 unsigned long addr, int type)
402 return HP_ZX1_PDIR_VALID_BIT | addr;
406 hp_zx1_enable (struct agp_bridge_data *bridge, u32 mode)
408 struct _hp_private *hp = &hp_private;
411 command = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
412 command = agp_collect_device_status(bridge, mode, command);
413 command |= 0x00000100;
415 writel(command, hp->lba_regs+hp->lba_cap_offset+PCI_AGP_COMMAND);
417 agp_device_command(command, (mode & AGP8X_MODE) != 0);
420 const struct agp_bridge_driver hp_zx1_driver = {
421 .owner = THIS_MODULE,
422 .size_type = FIXED_APER_SIZE,
423 .configure = hp_zx1_configure,
424 .fetch_size = hp_zx1_fetch_size,
425 .cleanup = hp_zx1_cleanup,
426 .tlb_flush = hp_zx1_tlbflush,
427 .mask_memory = hp_zx1_mask_memory,
428 .masks = hp_zx1_masks,
429 .agp_enable = hp_zx1_enable,
430 .cache_flush = global_cache_flush,
431 .create_gatt_table = hp_zx1_create_gatt_table,
432 .free_gatt_table = hp_zx1_free_gatt_table,
433 .insert_memory = hp_zx1_insert_memory,
434 .remove_memory = hp_zx1_remove_memory,
435 .alloc_by_type = agp_generic_alloc_by_type,
436 .free_by_type = agp_generic_free_by_type,
437 .agp_alloc_page = agp_generic_alloc_page,
438 .agp_alloc_pages = agp_generic_alloc_pages,
439 .agp_destroy_page = agp_generic_destroy_page,
440 .agp_destroy_pages = agp_generic_destroy_pages,
441 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
442 .cant_use_aperture = true,
446 hp_zx1_setup (u64 ioc_hpa, u64 lba_hpa)
448 struct agp_bridge_data *bridge;
451 error = hp_zx1_ioc_init(ioc_hpa);
455 error = hp_zx1_lba_init(lba_hpa);
459 bridge = agp_alloc_bridge();
464 bridge->driver = &hp_zx1_driver;
466 fake_bridge_dev.vendor = PCI_VENDOR_ID_HP;
467 fake_bridge_dev.device = PCI_DEVICE_ID_HP_PCIX_LBA;
468 bridge->dev = &fake_bridge_dev;
470 error = agp_add_bridge(bridge);
477 static acpi_status __init
478 zx1_gart_probe (acpi_handle obj, u32 depth, void *context, void **ret)
480 acpi_handle handle, parent;
482 struct acpi_buffer buffer;
483 struct acpi_device_info *info;
484 u64 lba_hpa, sba_hpa, length;
487 status = hp_acpi_csr_space(obj, &lba_hpa, &length);
488 if (ACPI_FAILURE(status))
489 return AE_OK; /* keep looking for another bridge */
491 /* Look for an enclosing IOC scope and find its CSR space */
494 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
495 status = acpi_get_object_info(handle, &buffer);
496 if (ACPI_SUCCESS(status)) {
497 /* TBD check _CID also */
498 info = buffer.pointer;
499 info->hardware_id.value[sizeof(info->hardware_id)-1] = '\0';
500 match = (strcmp(info->hardware_id.value, "HWP0001") == 0);
503 status = hp_acpi_csr_space(handle, &sba_hpa, &length);
504 if (ACPI_SUCCESS(status))
507 printk(KERN_ERR PFX "Detected HP ZX1 "
508 "AGP LBA but no IOC.\n");
514 status = acpi_get_parent(handle, &parent);
516 } while (ACPI_SUCCESS(status));
518 if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
521 printk(KERN_INFO PFX "Detected HP ZX1 %s AGP chipset (ioc=%lx, lba=%lx)\n",
522 (char *) context, sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa);
524 hp_zx1_gart_found = 1;
525 return AE_CTRL_TERMINATE; /* we only support one bridge; quit looking */
534 acpi_get_devices("HWP0003", zx1_gart_probe, "HWP0003", NULL);
535 if (hp_zx1_gart_found)
538 acpi_get_devices("HWP0007", zx1_gart_probe, "HWP0007", NULL);
539 if (hp_zx1_gart_found)
546 agp_hp_cleanup (void)
550 module_init(agp_hp_init);
551 module_exit(agp_hp_cleanup);
553 MODULE_LICENSE("GPL and additional rights");