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>
18 #include <asm/acpi-ext.h>
23 #define log2(x) ffz(~(x))
26 #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
28 /* HP ZX1 IOC registers */
29 #define HP_ZX1_IBASE 0x300
30 #define HP_ZX1_IMASK 0x308
31 #define HP_ZX1_PCOM 0x310
32 #define HP_ZX1_TCNFG 0x318
33 #define HP_ZX1_PDIR_BASE 0x320
35 #define HP_ZX1_IOVA_BASE GB(1UL)
36 #define HP_ZX1_IOVA_SIZE GB(1UL)
37 #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
38 #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
40 #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
41 #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
43 #define AGP8X_MODE_BIT 3
44 #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
46 /* AGP bridge need not be PCI device, but DRM thinks it is. */
47 static struct pci_dev fake_bridge_dev
;
49 static int hp_zx1_gart_found
;
51 static struct aper_size_info_fixed hp_zx1_sizes
[] =
53 {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
56 static struct gatt_mask hp_zx1_masks
[] =
58 {.mask
= HP_ZX1_PDIR_VALID_BIT
, .type
= 0}
61 static struct _hp_private
{
62 volatile u8 __iomem
*ioc_regs
;
63 volatile u8 __iomem
*lba_regs
;
65 u64
*io_pdir
; // PDIR for entire IOVA
66 u64
*gatt
; // PDIR just for GART (subset of above)
72 int io_pdir_owner
; // do we own it, or share it with sba_iommu?
75 int io_tlb_ps
; // IOC ps config
76 int io_pages_per_kpage
;
79 static int __init
hp_zx1_ioc_shared(void)
81 struct _hp_private
*hp
= &hp_private
;
83 printk(KERN_INFO PFX
"HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
86 * IOC already configured by sba_iommu module; just use
87 * its setup. We assume:
88 * - IOVA space is 1Gb in size
89 * - first 512Mb is IOMMU, second 512Mb is GART
91 hp
->io_tlb_ps
= INREG64(hp
->ioc_regs
, HP_ZX1_TCNFG
);
92 switch (hp
->io_tlb_ps
) {
93 case 0: hp
->io_tlb_shift
= 12; break;
94 case 1: hp
->io_tlb_shift
= 13; break;
95 case 2: hp
->io_tlb_shift
= 14; break;
96 case 3: hp
->io_tlb_shift
= 16; break;
98 printk(KERN_ERR PFX
"Invalid IOTLB page size "
99 "configuration 0x%x\n", hp
->io_tlb_ps
);
101 hp
->gatt_entries
= 0;
104 hp
->io_page_size
= 1 << hp
->io_tlb_shift
;
105 hp
->io_pages_per_kpage
= PAGE_SIZE
/ hp
->io_page_size
;
107 hp
->iova_base
= INREG64(hp
->ioc_regs
, HP_ZX1_IBASE
) & ~0x1;
108 hp
->gart_base
= hp
->iova_base
+ HP_ZX1_IOVA_SIZE
- HP_ZX1_GART_SIZE
;
110 hp
->gart_size
= HP_ZX1_GART_SIZE
;
111 hp
->gatt_entries
= hp
->gart_size
/ hp
->io_page_size
;
113 hp
->io_pdir
= phys_to_virt(INREG64(hp
->ioc_regs
, HP_ZX1_PDIR_BASE
));
114 hp
->gatt
= &hp
->io_pdir
[HP_ZX1_IOVA_TO_PDIR(hp
->gart_base
)];
116 if (hp
->gatt
[0] != HP_ZX1_SBA_IOMMU_COOKIE
) {
117 /* Normal case when no AGP device in system */
119 hp
->gatt_entries
= 0;
120 printk(KERN_ERR PFX
"No reserved IO PDIR entry found; "
129 hp_zx1_ioc_owner (void)
131 struct _hp_private
*hp
= &hp_private
;
133 printk(KERN_INFO PFX
"HP ZX1 IOC: IOPDIR dedicated to GART\n");
136 * Select an IOV page size no larger than system page size.
138 if (PAGE_SIZE
>= KB(64)) {
139 hp
->io_tlb_shift
= 16;
141 } else if (PAGE_SIZE
>= KB(16)) {
142 hp
->io_tlb_shift
= 14;
144 } else if (PAGE_SIZE
>= KB(8)) {
145 hp
->io_tlb_shift
= 13;
148 hp
->io_tlb_shift
= 12;
151 hp
->io_page_size
= 1 << hp
->io_tlb_shift
;
152 hp
->io_pages_per_kpage
= PAGE_SIZE
/ hp
->io_page_size
;
154 hp
->iova_base
= HP_ZX1_IOVA_BASE
;
155 hp
->gart_size
= HP_ZX1_GART_SIZE
;
156 hp
->gart_base
= hp
->iova_base
+ HP_ZX1_IOVA_SIZE
- hp
->gart_size
;
158 hp
->gatt_entries
= hp
->gart_size
/ hp
->io_page_size
;
159 hp
->io_pdir_size
= (HP_ZX1_IOVA_SIZE
/ hp
->io_page_size
) * sizeof(u64
);
165 hp_zx1_ioc_init (u64 hpa
)
167 struct _hp_private
*hp
= &hp_private
;
169 hp
->ioc_regs
= ioremap(hpa
, 1024);
174 * If the IOTLB is currently disabled, we can take it over.
175 * Otherwise, we have to share with sba_iommu.
177 hp
->io_pdir_owner
= (INREG64(hp
->ioc_regs
, HP_ZX1_IBASE
) & 0x1) == 0;
179 if (hp
->io_pdir_owner
)
180 return hp_zx1_ioc_owner();
182 return hp_zx1_ioc_shared();
186 hp_zx1_lba_find_capability (volatile u8 __iomem
*hpa
, int cap
)
192 status
= INREG16(hpa
, PCI_STATUS
);
193 if (!(status
& PCI_STATUS_CAP_LIST
))
195 pos
= INREG8(hpa
, PCI_CAPABILITY_LIST
);
196 while (ttl
-- && pos
>= 0x40) {
198 id
= INREG8(hpa
, pos
+ PCI_CAP_LIST_ID
);
203 pos
= INREG8(hpa
, pos
+ PCI_CAP_LIST_NEXT
);
209 hp_zx1_lba_init (u64 hpa
)
211 struct _hp_private
*hp
= &hp_private
;
214 hp
->lba_regs
= ioremap(hpa
, 256);
218 hp
->lba_cap_offset
= hp_zx1_lba_find_capability(hp
->lba_regs
, PCI_CAP_ID_AGP
);
220 cap
= INREG32(hp
->lba_regs
, hp
->lba_cap_offset
) & 0xff;
221 if (cap
!= PCI_CAP_ID_AGP
) {
222 printk(KERN_ERR PFX
"Invalid capability ID 0x%02x at 0x%x\n",
223 cap
, hp
->lba_cap_offset
);
231 hp_zx1_fetch_size(void)
235 size
= hp_private
.gart_size
/ MB(1);
236 hp_zx1_sizes
[0].size
= size
;
237 agp_bridge
->current_size
= (void *) &hp_zx1_sizes
[0];
242 hp_zx1_configure (void)
244 struct _hp_private
*hp
= &hp_private
;
246 agp_bridge
->gart_bus_addr
= hp
->gart_base
;
247 agp_bridge
->capndx
= hp
->lba_cap_offset
;
248 agp_bridge
->mode
= INREG32(hp
->lba_regs
, hp
->lba_cap_offset
+ PCI_AGP_STATUS
);
250 if (hp
->io_pdir_owner
) {
251 OUTREG64(hp
->ioc_regs
, HP_ZX1_PDIR_BASE
, virt_to_phys(hp
->io_pdir
));
252 OUTREG64(hp
->ioc_regs
, HP_ZX1_TCNFG
, hp
->io_tlb_ps
);
253 OUTREG64(hp
->ioc_regs
, HP_ZX1_IMASK
, ~(HP_ZX1_IOVA_SIZE
- 1));
254 OUTREG64(hp
->ioc_regs
, HP_ZX1_IBASE
, hp
->iova_base
| 0x1);
255 OUTREG64(hp
->ioc_regs
, HP_ZX1_PCOM
, hp
->iova_base
| log2(HP_ZX1_IOVA_SIZE
));
256 INREG64(hp
->ioc_regs
, HP_ZX1_PCOM
);
263 hp_zx1_cleanup (void)
265 struct _hp_private
*hp
= &hp_private
;
268 if (hp
->io_pdir_owner
)
269 OUTREG64(hp
->ioc_regs
, HP_ZX1_IBASE
, 0);
270 iounmap(hp
->ioc_regs
);
273 iounmap(hp
->lba_regs
);
277 hp_zx1_tlbflush (struct agp_memory
*mem
)
279 struct _hp_private
*hp
= &hp_private
;
281 OUTREG64(hp
->ioc_regs
, HP_ZX1_PCOM
, hp
->gart_base
| log2(hp
->gart_size
));
282 INREG64(hp
->ioc_regs
, HP_ZX1_PCOM
);
286 hp_zx1_create_gatt_table (void)
288 struct _hp_private
*hp
= &hp_private
;
291 if (hp
->io_pdir_owner
) {
292 hp
->io_pdir
= (u64
*) __get_free_pages(GFP_KERNEL
,
293 get_order(hp
->io_pdir_size
));
295 printk(KERN_ERR PFX
"Couldn't allocate contiguous "
296 "memory for I/O PDIR\n");
298 hp
->gatt_entries
= 0;
301 memset(hp
->io_pdir
, 0, hp
->io_pdir_size
);
303 hp
->gatt
= &hp
->io_pdir
[HP_ZX1_IOVA_TO_PDIR(hp
->gart_base
)];
306 for (i
= 0; i
< hp
->gatt_entries
; i
++) {
307 hp
->gatt
[i
] = (unsigned long) agp_bridge
->scratch_page
;
314 hp_zx1_free_gatt_table (void)
316 struct _hp_private
*hp
= &hp_private
;
318 if (hp
->io_pdir_owner
)
319 free_pages((unsigned long) hp
->io_pdir
,
320 get_order(hp
->io_pdir_size
));
322 hp
->gatt
[0] = HP_ZX1_SBA_IOMMU_COOKIE
;
327 hp_zx1_insert_memory (struct agp_memory
*mem
, off_t pg_start
, int type
)
329 struct _hp_private
*hp
= &hp_private
;
331 off_t j
, io_pg_start
;
334 if (type
!= 0 || mem
->type
!= 0) {
338 io_pg_start
= hp
->io_pages_per_kpage
* pg_start
;
339 io_pg_count
= hp
->io_pages_per_kpage
* mem
->page_count
;
340 if ((io_pg_start
+ io_pg_count
) > hp
->gatt_entries
) {
345 while (j
< (io_pg_start
+ io_pg_count
)) {
352 if (mem
->is_flushed
== FALSE
) {
353 global_cache_flush();
354 mem
->is_flushed
= TRUE
;
357 for (i
= 0, j
= io_pg_start
; i
< mem
->page_count
; i
++) {
360 paddr
= mem
->memory
[i
];
362 k
< hp
->io_pages_per_kpage
;
363 k
++, j
++, paddr
+= hp
->io_page_size
) {
364 hp
->gatt
[j
] = agp_bridge
->driver
->mask_memory(paddr
, type
);
368 agp_bridge
->driver
->tlb_flush(mem
);
373 hp_zx1_remove_memory (struct agp_memory
*mem
, off_t pg_start
, int type
)
375 struct _hp_private
*hp
= &hp_private
;
376 int i
, io_pg_start
, io_pg_count
;
378 if (type
!= 0 || mem
->type
!= 0) {
382 io_pg_start
= hp
->io_pages_per_kpage
* pg_start
;
383 io_pg_count
= hp
->io_pages_per_kpage
* mem
->page_count
;
384 for (i
= io_pg_start
; i
< io_pg_count
+ io_pg_start
; i
++) {
385 hp
->gatt
[i
] = agp_bridge
->scratch_page
;
388 agp_bridge
->driver
->tlb_flush(mem
);
393 hp_zx1_mask_memory (unsigned long addr
, int type
)
395 return HP_ZX1_PDIR_VALID_BIT
| addr
;
399 hp_zx1_enable (u32 mode
)
401 struct _hp_private
*hp
= &hp_private
;
404 command
= INREG32(hp
->lba_regs
, hp
->lba_cap_offset
+ PCI_AGP_STATUS
);
406 command
= agp_collect_device_status(mode
, command
);
407 command
|= 0x00000100;
409 OUTREG32(hp
->lba_regs
, hp
->lba_cap_offset
+ PCI_AGP_COMMAND
, command
);
411 agp_device_command(command
, (mode
& AGP8X_MODE
) != 0);
414 struct agp_bridge_driver hp_zx1_driver
= {
415 .owner
= THIS_MODULE
,
416 .size_type
= FIXED_APER_SIZE
,
417 .configure
= hp_zx1_configure
,
418 .fetch_size
= hp_zx1_fetch_size
,
419 .cleanup
= hp_zx1_cleanup
,
420 .tlb_flush
= hp_zx1_tlbflush
,
421 .mask_memory
= hp_zx1_mask_memory
,
422 .masks
= hp_zx1_masks
,
423 .agp_enable
= hp_zx1_enable
,
424 .cache_flush
= global_cache_flush
,
425 .create_gatt_table
= hp_zx1_create_gatt_table
,
426 .free_gatt_table
= hp_zx1_free_gatt_table
,
427 .insert_memory
= hp_zx1_insert_memory
,
428 .remove_memory
= hp_zx1_remove_memory
,
429 .alloc_by_type
= agp_generic_alloc_by_type
,
430 .free_by_type
= agp_generic_free_by_type
,
431 .agp_alloc_page
= agp_generic_alloc_page
,
432 .agp_destroy_page
= agp_generic_destroy_page
,
433 .cant_use_aperture
= 1,
437 hp_zx1_setup (u64 ioc_hpa
, u64 lba_hpa
)
439 struct agp_bridge_data
*bridge
;
442 error
= hp_zx1_ioc_init(ioc_hpa
);
446 error
= hp_zx1_lba_init(lba_hpa
);
450 bridge
= agp_alloc_bridge();
455 bridge
->driver
= &hp_zx1_driver
;
457 fake_bridge_dev
.vendor
= PCI_VENDOR_ID_HP
;
458 fake_bridge_dev
.device
= PCI_DEVICE_ID_HP_PCIX_LBA
;
459 bridge
->dev
= &fake_bridge_dev
;
461 error
= agp_add_bridge(bridge
);
468 static acpi_status __init
469 zx1_gart_probe (acpi_handle obj
, u32 depth
, void *context
, void **ret
)
471 acpi_handle handle
, parent
;
473 struct acpi_buffer buffer
;
474 struct acpi_device_info
*info
;
475 u64 lba_hpa
, sba_hpa
, length
;
478 status
= hp_acpi_csr_space(obj
, &lba_hpa
, &length
);
479 if (ACPI_FAILURE(status
))
480 return AE_OK
; /* keep looking for another bridge */
482 /* Look for an enclosing IOC scope and find its CSR space */
485 buffer
.length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
486 status
= acpi_get_object_info(handle
, &buffer
);
487 if (ACPI_SUCCESS(status
)) {
488 /* TBD check _CID also */
489 info
= buffer
.pointer
;
490 info
->hardware_id
.value
[sizeof(info
->hardware_id
)-1] = '\0';
491 match
= (strcmp(info
->hardware_id
.value
, "HWP0001") == 0);
494 status
= hp_acpi_csr_space(handle
, &sba_hpa
, &length
);
495 if (ACPI_SUCCESS(status
))
498 printk(KERN_ERR PFX
"Detected HP ZX1 "
499 "AGP LBA but no IOC.\n");
505 status
= acpi_get_parent(handle
, &parent
);
507 } while (ACPI_SUCCESS(status
));
509 if (hp_zx1_setup(sba_hpa
+ HP_ZX1_IOC_OFFSET
, lba_hpa
))
512 printk(KERN_INFO PFX
"Detected HP ZX1 %s AGP chipset (ioc=%lx, lba=%lx)\n",
513 (char *) context
, sba_hpa
+ HP_ZX1_IOC_OFFSET
, lba_hpa
);
515 hp_zx1_gart_found
= 1;
516 return AE_CTRL_TERMINATE
; /* we only support one bridge; quit looking */
523 acpi_get_devices("HWP0003", zx1_gart_probe
, "HWP0003", NULL
);
524 if (hp_zx1_gart_found
)
527 acpi_get_devices("HWP0007", zx1_gart_probe
, "HWP0007", NULL
);
528 if (hp_zx1_gart_found
)
535 agp_hp_cleanup (void)
539 module_init(agp_hp_init
);
540 module_exit(agp_hp_cleanup
);
542 MODULE_LICENSE("GPL and additional rights");