- Separate out show_stack changes into own patch.
[linux-flexiantxendom0-3.2.10.git] / drivers / char / agp / hp-agp.c
1 /*
2  * HP AGPGART routines.
3  *      Copyright (C) 2002-2003 Hewlett-Packard Co
4  *              Bjorn Helgaas <bjorn_helgaas@hp.com>
5  */
6
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>
12
13 #include <asm/acpi-ext.h>
14
15 #include "agp.h"
16
17 #ifndef log2
18 #define log2(x)         ffz(~(x))
19 #endif
20
21 #define HP_ZX1_IOC_OFFSET       0x1000  /* ACPI reports SBA, we want IOC */
22
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
29
30 /* HP ZX1 LBA registers */
31 #define HP_ZX1_AGP_STATUS       0x64
32 #define HP_ZX1_AGP_COMMAND      0x68
33
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
38
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)
41
42 /* AGP bridge need not be PCI device, but DRM thinks it is. */
43 static struct pci_dev fake_bridge_dev;
44
45 static struct aper_size_info_fixed hp_zx1_sizes[] =
46 {
47         {0, 0, 0},              /* filled in by hp_zx1_fetch_size() */
48 };
49
50 static struct gatt_mask hp_zx1_masks[] =
51 {
52         {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
53 };
54
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)
60         u64 gatt_entries;
61         u64 iova_base;
62         u64 gart_base;
63         u64 gart_size;
64         u64 io_pdir_size;
65         int io_pdir_owner;      // do we own it, or share it with sba_iommu?
66         int io_page_size;
67         int io_tlb_shift;
68         int io_tlb_ps;          // IOC ps config
69         int io_pages_per_kpage;
70 } hp_private;
71
72 static int __init
73 hp_zx1_ioc_shared (void)
74 {
75         struct _hp_private *hp = &hp_private;
76
77         printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
78
79         /*
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
84          */
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;
91                 default:
92                         printk(KERN_ERR PFX "Invalid IOTLB page size "
93                                "configuration 0x%x\n", hp->io_tlb_ps);
94                         hp->gatt = 0;
95                         hp->gatt_entries = 0;
96                         return -ENODEV;
97         }
98         hp->io_page_size = 1 << hp->io_tlb_shift;
99         hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
100
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;
103
104         hp->gart_size = HP_ZX1_GART_SIZE;
105         hp->gatt_entries = hp->gart_size / hp->io_page_size;
106
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)];
109
110         if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
111                 hp->gatt = 0;
112                 hp->gatt_entries = 0;
113                 printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
114                        "GART disabled\n");
115                 return -ENODEV;
116         }
117
118         return 0;
119 }
120
121 static int __init
122 hp_zx1_ioc_owner (void)
123 {
124         struct _hp_private *hp = &hp_private;
125
126         printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
127
128         /*
129          * Select an IOV page size no larger than system page size.
130          */
131         if (PAGE_SIZE >= KB(64)) {
132                 hp->io_tlb_shift = 16;
133                 hp->io_tlb_ps = 3;
134         } else if (PAGE_SIZE >= KB(16)) {
135                 hp->io_tlb_shift = 14;
136                 hp->io_tlb_ps = 2;
137         } else if (PAGE_SIZE >= KB(8)) {
138                 hp->io_tlb_shift = 13;
139                 hp->io_tlb_ps = 1;
140         } else {
141                 hp->io_tlb_shift = 12;
142                 hp->io_tlb_ps = 0;
143         }
144         hp->io_page_size = 1 << hp->io_tlb_shift;
145         hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
146
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;
150
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);
153
154         return 0;
155 }
156
157 static int __init
158 hp_zx1_ioc_init (u64 ioc_hpa, u64 lba_hpa)
159 {
160         struct _hp_private *hp = &hp_private;
161
162         hp->ioc_regs = ioremap(ioc_hpa, 1024);
163         hp->lba_regs = ioremap(lba_hpa, 256);
164
165         /*
166          * If the IOTLB is currently disabled, we can take it over.
167          * Otherwise, we have to share with sba_iommu.
168          */
169         hp->io_pdir_owner = (INREG64(hp->ioc_regs, HP_ZX1_IBASE) & 0x1) == 0;
170
171         if (hp->io_pdir_owner)
172                 return hp_zx1_ioc_owner();
173
174         return hp_zx1_ioc_shared();
175 }
176
177 static int
178 hp_zx1_fetch_size (void)
179 {
180         int size;
181
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];
185         return size;
186 }
187
188 static int
189 hp_zx1_configure (void)
190 {
191         struct _hp_private *hp = &hp_private;
192
193         agp_bridge->gart_bus_addr = hp->gart_base;
194         agp_bridge->mode = INREG32(hp->lba_regs, HP_ZX1_AGP_STATUS);
195
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);
205         }
206
207         return 0;
208 }
209
210 static void
211 hp_zx1_cleanup (void)
212 {
213         struct _hp_private *hp = &hp_private;
214
215         if (hp->io_pdir_owner)
216                 OUTREG64(hp->ioc_regs, HP_ZX1_IBASE, 0);
217         iounmap((void *) hp->ioc_regs);
218 }
219
220 static void
221 hp_zx1_tlbflush (agp_memory * mem)
222 {
223         struct _hp_private *hp = &hp_private;
224
225         OUTREG64(hp->ioc_regs, HP_ZX1_PCOM, hp->gart_base | log2(hp->gart_size));
226         INREG64(hp->ioc_regs, HP_ZX1_PCOM);
227 }
228
229 static int
230 hp_zx1_create_gatt_table (void)
231 {
232         struct _hp_private *hp = &hp_private;
233         int i;
234
235         if (hp->io_pdir_owner) {
236                 hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
237                                                 get_order(hp->io_pdir_size));
238                 if (!hp->io_pdir) {
239                         printk(KERN_ERR PFX "Couldn't allocate contiguous "
240                                 "memory for I/O PDIR\n");
241                         hp->gatt = 0;
242                         hp->gatt_entries = 0;
243                         return -ENOMEM;
244                 }
245                 memset(hp->io_pdir, 0, hp->io_pdir_size);
246
247                 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
248         }
249
250         for (i = 0; i < hp->gatt_entries; i++) {
251                 hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
252         }
253
254         return 0;
255 }
256
257 static int
258 hp_zx1_free_gatt_table (void)
259 {
260         struct _hp_private *hp = &hp_private;
261
262         if (hp->io_pdir_owner)
263                 free_pages((unsigned long) hp->io_pdir,
264                             get_order(hp->io_pdir_size));
265         else
266                 hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
267         return 0;
268 }
269
270 static int
271 hp_zx1_insert_memory (agp_memory * mem, off_t pg_start, int type)
272 {
273         struct _hp_private *hp = &hp_private;
274         int i, k;
275         off_t j, io_pg_start;
276         int io_pg_count;
277
278         if (type != 0 || mem->type != 0) {
279                 return -EINVAL;
280         }
281
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) {
285                 return -EINVAL;
286         }
287
288         j = io_pg_start;
289         while (j < (io_pg_start + io_pg_count)) {
290                 if (hp->gatt[j]) {
291                         return -EBUSY;
292                 }
293                 j++;
294         }
295
296         if (mem->is_flushed == FALSE) {
297                 CACHE_FLUSH();
298                 mem->is_flushed = TRUE;
299         }
300
301         for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
302                 unsigned long paddr;
303
304                 paddr = mem->memory[i];
305                 for (k = 0;
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);
309                 }
310         }
311
312         agp_bridge->tlb_flush(mem);
313         return 0;
314 }
315
316 static int
317 hp_zx1_remove_memory (agp_memory * mem, off_t pg_start, int type)
318 {
319         struct _hp_private *hp = &hp_private;
320         int i, io_pg_start, io_pg_count;
321
322         if (type != 0 || mem->type != 0) {
323                 return -EINVAL;
324         }
325
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;
330         }
331
332         agp_bridge->tlb_flush(mem);
333         return 0;
334 }
335
336 static unsigned long
337 hp_zx1_mask_memory(unsigned long addr, int type)
338 {
339         return HP_ZX1_PDIR_VALID_BIT | addr;
340 }
341
342 static void
343 hp_zx1_agp_enable (u32 mode)
344 {
345         struct _hp_private *hp = &hp_private;
346         u32 command;
347
348         command = INREG32(hp->lba_regs, HP_ZX1_AGP_STATUS);
349
350         command = agp_collect_device_status(mode, command);
351         command |= 0x00000100;
352
353         OUTREG32(hp->lba_regs, HP_ZX1_AGP_COMMAND, command);
354
355         agp_device_command(command, 0);
356 }
357
358 static int __init
359 hp_zx1_setup (u64 ioc_hpa, u64 lba_hpa)
360 {
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;
381
382         return hp_zx1_ioc_init(ioc_hpa, lba_hpa);
383 }
384
385 static acpi_status __init
386 zx1_gart_probe (acpi_handle obj, u32 depth, void *context, void **ret)
387 {
388         acpi_handle handle, parent;
389         acpi_status status;
390         struct acpi_device_info info;
391         u64 lba_hpa, sba_hpa, length;
392
393         status = hp_acpi_csr_space(obj, &lba_hpa, &length);
394         if (ACPI_FAILURE(status))
395                 return 1;
396
397         /* Look for an enclosing IOC scope and find its CSR space */
398         handle = obj;
399         do {
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))
407                                         break;
408                                 else {
409                                         printk(KERN_ERR PFX "Detected HP ZX1 "
410                                                "AGP LBA but no IOC.\n");
411                                         return status;
412                                 }
413                         }
414                 }
415
416                 status = acpi_get_parent(handle, &parent);
417                 handle = parent;
418         } while (ACPI_SUCCESS(status));
419
420         if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
421                 return 1;
422
423         fake_bridge_dev.vendor = PCI_VENDOR_ID_HP;
424         fake_bridge_dev.device = PCI_DEVICE_ID_HP_ZX1_LBA;
425
426         return 0;
427 }
428
429 static struct agp_driver hp_agp_driver = {
430         .owner = THIS_MODULE,
431 };
432
433 static int __init
434 agp_hp_init (void)
435 {
436         acpi_status status;
437
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");
442                 return -ENODEV;
443         }
444
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);
450
451         } else {
452                 return -ENODEV;
453         }
454 }
455
456 static void __exit
457 agp_hp_cleanup (void)
458 {
459         agp_unregister_driver(&hp_agp_driver);
460 }
461
462 module_init(agp_hp_init);
463 module_exit(agp_hp_cleanup);
464
465 MODULE_LICENSE("GPL and additional rights");