3 * Copyright (C) 2002-2003 Hewlett-Packard Co
4 * Bjorn Helgaas <bjorn_helgaas@hp.com>
7 #include <linux/acpi.h>
8 #include <linux/agp_backend.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
13 #include <asm/acpi-ext.h>
18 #define log2(x) ffz(~(x))
21 #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
23 /* HP ZX1 IOC registers */
24 #define HP_ZX1_IBASE 0x300
25 #define HP_ZX1_IMASK 0x308
26 #define HP_ZX1_PCOM 0x310
27 #define HP_ZX1_TCNFG 0x318
28 #define HP_ZX1_PDIR_BASE 0x320
30 /* HP ZX1 LBA registers */
31 #define HP_ZX1_AGP_STATUS 0x64
32 #define HP_ZX1_AGP_COMMAND 0x68
34 #define HP_ZX1_IOVA_BASE GB(1UL)
35 #define HP_ZX1_IOVA_SIZE GB(1UL)
36 #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
37 #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
39 #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
40 #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
42 /* AGP bridge need not be PCI device, but DRM thinks it is. */
43 static struct pci_dev fake_bridge_dev;
45 static struct aper_size_info_fixed hp_zx1_sizes[] =
47 {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
50 static struct gatt_mask hp_zx1_masks[] =
52 {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
55 static struct _hp_private {
56 volatile u8 *ioc_regs;
57 volatile u8 *lba_regs;
58 u64 *io_pdir; // PDIR for entire IOVA
59 u64 *gatt; // PDIR just for GART (subset of above)
65 int io_pdir_owner; // do we own it, or share it with sba_iommu?
68 int io_tlb_ps; // IOC ps config
69 int io_pages_per_kpage;
73 hp_zx1_ioc_shared (void)
75 struct _hp_private *hp = &hp_private;
77 printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
80 * IOC already configured by sba_iommu module; just use
81 * its setup. We assume:
82 * - IOVA space is 1Gb in size
83 * - first 512Mb is IOMMU, second 512Mb is GART
85 hp->io_tlb_ps = INREG64(hp->ioc_regs, HP_ZX1_TCNFG);
86 switch (hp->io_tlb_ps) {
87 case 0: hp->io_tlb_shift = 12; break;
88 case 1: hp->io_tlb_shift = 13; break;
89 case 2: hp->io_tlb_shift = 14; break;
90 case 3: hp->io_tlb_shift = 16; break;
92 printk(KERN_ERR PFX "Invalid IOTLB page size "
93 "configuration 0x%x\n", hp->io_tlb_ps);
98 hp->io_page_size = 1 << hp->io_tlb_shift;
99 hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
101 hp->iova_base = INREG64(hp->ioc_regs, HP_ZX1_IBASE) & ~0x1;
102 hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
104 hp->gart_size = HP_ZX1_GART_SIZE;
105 hp->gatt_entries = hp->gart_size / hp->io_page_size;
107 hp->io_pdir = phys_to_virt(INREG64(hp->ioc_regs, HP_ZX1_PDIR_BASE));
108 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
110 if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
112 hp->gatt_entries = 0;
113 printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
122 hp_zx1_ioc_owner (void)
124 struct _hp_private *hp = &hp_private;
126 printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
129 * Select an IOV page size no larger than system page size.
131 if (PAGE_SIZE >= KB(64)) {
132 hp->io_tlb_shift = 16;
134 } else if (PAGE_SIZE >= KB(16)) {
135 hp->io_tlb_shift = 14;
137 } else if (PAGE_SIZE >= KB(8)) {
138 hp->io_tlb_shift = 13;
141 hp->io_tlb_shift = 12;
144 hp->io_page_size = 1 << hp->io_tlb_shift;
145 hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
147 hp->iova_base = HP_ZX1_IOVA_BASE;
148 hp->gart_size = HP_ZX1_GART_SIZE;
149 hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
151 hp->gatt_entries = hp->gart_size / hp->io_page_size;
152 hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
158 hp_zx1_ioc_init (u64 ioc_hpa, u64 lba_hpa)
160 struct _hp_private *hp = &hp_private;
162 hp->ioc_regs = ioremap(ioc_hpa, 1024);
163 hp->lba_regs = ioremap(lba_hpa, 256);
166 * If the IOTLB is currently disabled, we can take it over.
167 * Otherwise, we have to share with sba_iommu.
169 hp->io_pdir_owner = (INREG64(hp->ioc_regs, HP_ZX1_IBASE) & 0x1) == 0;
171 if (hp->io_pdir_owner)
172 return hp_zx1_ioc_owner();
174 return hp_zx1_ioc_shared();
178 hp_zx1_fetch_size (void)
182 size = hp_private.gart_size / MB(1);
183 hp_zx1_sizes[0].size = size;
184 agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
189 hp_zx1_configure (void)
191 struct _hp_private *hp = &hp_private;
193 agp_bridge->gart_bus_addr = hp->gart_base;
194 agp_bridge->mode = INREG32(hp->lba_regs, HP_ZX1_AGP_STATUS);
196 if (hp->io_pdir_owner) {
197 OUTREG64(hp->ioc_regs, HP_ZX1_PDIR_BASE,
198 virt_to_phys(hp->io_pdir));
199 OUTREG64(hp->ioc_regs, HP_ZX1_TCNFG, hp->io_tlb_ps);
200 OUTREG64(hp->ioc_regs, HP_ZX1_IMASK, ~(HP_ZX1_IOVA_SIZE - 1));
201 OUTREG64(hp->ioc_regs, HP_ZX1_IBASE, hp->iova_base | 0x1);
202 OUTREG64(hp->ioc_regs, HP_ZX1_PCOM,
203 hp->iova_base | log2(HP_ZX1_IOVA_SIZE));
204 INREG64(hp->ioc_regs, HP_ZX1_PCOM);
211 hp_zx1_cleanup (void)
213 struct _hp_private *hp = &hp_private;
215 if (hp->io_pdir_owner)
216 OUTREG64(hp->ioc_regs, HP_ZX1_IBASE, 0);
217 iounmap((void *) hp->ioc_regs);
221 hp_zx1_tlbflush (agp_memory * mem)
223 struct _hp_private *hp = &hp_private;
225 OUTREG64(hp->ioc_regs, HP_ZX1_PCOM, hp->gart_base | log2(hp->gart_size));
226 INREG64(hp->ioc_regs, HP_ZX1_PCOM);
230 hp_zx1_create_gatt_table (void)
232 struct _hp_private *hp = &hp_private;
235 if (hp->io_pdir_owner) {
236 hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
237 get_order(hp->io_pdir_size));
239 printk(KERN_ERR PFX "Couldn't allocate contiguous "
240 "memory for I/O PDIR\n");
242 hp->gatt_entries = 0;
245 memset(hp->io_pdir, 0, hp->io_pdir_size);
247 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
250 for (i = 0; i < hp->gatt_entries; i++) {
251 hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
258 hp_zx1_free_gatt_table (void)
260 struct _hp_private *hp = &hp_private;
262 if (hp->io_pdir_owner)
263 free_pages((unsigned long) hp->io_pdir,
264 get_order(hp->io_pdir_size));
266 hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
271 hp_zx1_insert_memory (agp_memory * mem, off_t pg_start, int type)
273 struct _hp_private *hp = &hp_private;
275 off_t j, io_pg_start;
278 if (type != 0 || mem->type != 0) {
282 io_pg_start = hp->io_pages_per_kpage * pg_start;
283 io_pg_count = hp->io_pages_per_kpage * mem->page_count;
284 if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
289 while (j < (io_pg_start + io_pg_count)) {
296 if (mem->is_flushed == FALSE) {
298 mem->is_flushed = TRUE;
301 for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
304 paddr = mem->memory[i];
306 k < hp->io_pages_per_kpage;
307 k++, j++, paddr += hp->io_page_size) {
308 hp->gatt[j] = agp_bridge->mask_memory(paddr, type);
312 agp_bridge->tlb_flush(mem);
317 hp_zx1_remove_memory (agp_memory * mem, off_t pg_start, int type)
319 struct _hp_private *hp = &hp_private;
320 int i, io_pg_start, io_pg_count;
322 if (type != 0 || mem->type != 0) {
326 io_pg_start = hp->io_pages_per_kpage * pg_start;
327 io_pg_count = hp->io_pages_per_kpage * mem->page_count;
328 for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
329 hp->gatt[i] = agp_bridge->scratch_page;
332 agp_bridge->tlb_flush(mem);
337 hp_zx1_mask_memory(unsigned long addr, int type)
339 return HP_ZX1_PDIR_VALID_BIT | addr;
343 hp_zx1_agp_enable (u32 mode)
345 struct _hp_private *hp = &hp_private;
348 command = INREG32(hp->lba_regs, HP_ZX1_AGP_STATUS);
350 command = agp_collect_device_status(mode, command);
351 command |= 0x00000100;
353 OUTREG32(hp->lba_regs, HP_ZX1_AGP_COMMAND, command);
355 agp_device_command(command, 0);
359 hp_zx1_setup (u64 ioc_hpa, u64 lba_hpa)
361 agp_bridge->masks = hp_zx1_masks;
362 agp_bridge->dev_private_data = NULL;
363 agp_bridge->size_type = FIXED_APER_SIZE;
364 agp_bridge->needs_scratch_page = FALSE;
365 agp_bridge->configure = hp_zx1_configure;
366 agp_bridge->fetch_size = hp_zx1_fetch_size;
367 agp_bridge->cleanup = hp_zx1_cleanup;
368 agp_bridge->tlb_flush = hp_zx1_tlbflush;
369 agp_bridge->mask_memory = hp_zx1_mask_memory;
370 agp_bridge->agp_enable = hp_zx1_agp_enable;
371 agp_bridge->cache_flush = global_cache_flush;
372 agp_bridge->create_gatt_table = hp_zx1_create_gatt_table;
373 agp_bridge->free_gatt_table = hp_zx1_free_gatt_table;
374 agp_bridge->insert_memory = hp_zx1_insert_memory;
375 agp_bridge->remove_memory = hp_zx1_remove_memory;
376 agp_bridge->alloc_by_type = agp_generic_alloc_by_type;
377 agp_bridge->free_by_type = agp_generic_free_by_type;
378 agp_bridge->agp_alloc_page = agp_generic_alloc_page;
379 agp_bridge->agp_destroy_page = agp_generic_destroy_page;
380 agp_bridge->cant_use_aperture = 1;
382 return hp_zx1_ioc_init(ioc_hpa, lba_hpa);
385 static acpi_status __init
386 zx1_gart_probe (acpi_handle obj, u32 depth, void *context, void **ret)
388 acpi_handle handle, parent;
390 struct acpi_device_info info;
391 u64 lba_hpa, sba_hpa, length;
393 status = hp_acpi_csr_space(obj, &lba_hpa, &length);
394 if (ACPI_FAILURE(status))
397 /* Look for an enclosing IOC scope and find its CSR space */
400 status = acpi_get_object_info(handle, &info);
401 if (ACPI_SUCCESS(status)) {
402 /* TBD check _CID also */
403 info.hardware_id[sizeof(info.hardware_id)-1] = '\0';
404 if (!strcmp(info.hardware_id, "HWP0001")) {
405 status = hp_acpi_csr_space(handle, &sba_hpa, &length);
406 if (ACPI_SUCCESS(status))
409 printk(KERN_ERR PFX "Detected HP ZX1 "
410 "AGP LBA but no IOC.\n");
416 status = acpi_get_parent(handle, &parent);
418 } while (ACPI_SUCCESS(status));
420 if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
423 fake_bridge_dev.vendor = PCI_VENDOR_ID_HP;
424 fake_bridge_dev.device = PCI_DEVICE_ID_HP_ZX1_LBA;
429 static struct agp_driver hp_agp_driver = {
430 .owner = THIS_MODULE,
438 status = acpi_get_devices("HWP0003", zx1_gart_probe, "HWP0003 AGP LBA", NULL);
439 if (!(ACPI_SUCCESS(status))) {
440 agp_bridge->type = NOT_SUPPORTED;
441 printk(KERN_INFO PFX "Failed to initialize zx1 AGP.\n");
445 if (fake_bridge_dev.vendor && !agp_bridge->type) {
446 hp_agp_driver.dev = &fake_bridge_dev;
447 agp_bridge->type = HP_ZX1;
448 agp_bridge->dev = &fake_bridge_dev;
449 return agp_register_driver(&hp_agp_driver);
457 agp_hp_cleanup (void)
459 agp_unregister_driver(&hp_agp_driver);
462 module_init(agp_hp_init);
463 module_exit(agp_hp_cleanup);
465 MODULE_LICENSE("GPL and additional rights");