added some suse-specific patches to the kernel.
[linux-flexiantxendom0-3.2.10.git] / drivers / char / agp / hp-agp.c
1 /*
2  * HP AGPGART routines. 
3  */
4
5 #include <linux/module.h>
6 #include <linux/pci.h>
7 #include <linux/init.h>
8 #include <linux/agp_backend.h>
9 #include "agp.h"
10
11 #ifndef log2
12 #define log2(x)         ffz(~(x))
13 #endif
14
15 #define HP_ZX1_IOVA_BASE        GB(1UL)
16 #define HP_ZX1_IOVA_SIZE        GB(1UL)
17 #define HP_ZX1_GART_SIZE        (HP_ZX1_IOVA_SIZE / 2)
18 #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
19
20 #define HP_ZX1_PDIR_VALID_BIT   0x8000000000000000UL
21 #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
22
23 static struct aper_size_info_fixed hp_zx1_sizes[] =
24 {
25         {0, 0, 0},              /* filled in by hp_zx1_fetch_size() */
26 };
27
28 static struct gatt_mask hp_zx1_masks[] =
29 {
30         {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
31 };
32
33 static struct _hp_private {
34         struct pci_dev *ioc;
35         volatile u8 *registers;
36         u64 *io_pdir;           // PDIR for entire IOVA
37         u64 *gatt;              // PDIR just for GART (subset of above)
38         u64 gatt_entries;
39         u64 iova_base;
40         u64 gart_base;
41         u64 gart_size;
42         u64 io_pdir_size;
43         int io_pdir_owner;      // do we own it, or share it with sba_iommu?
44         int io_page_size;
45         int io_tlb_shift;
46         int io_tlb_ps;          // IOC ps config
47         int io_pages_per_kpage;
48 } hp_private;
49
50 static int __init hp_zx1_ioc_shared(void)
51 {
52         struct _hp_private *hp = &hp_private;
53
54         printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
55
56         /*
57          * IOC already configured by sba_iommu module; just use
58          * its setup.  We assume:
59          *      - IOVA space is 1Gb in size
60          *      - first 512Mb is IOMMU, second 512Mb is GART
61          */
62         hp->io_tlb_ps = INREG64(hp->registers, HP_ZX1_TCNFG);
63         switch (hp->io_tlb_ps) {
64                 case 0: hp->io_tlb_shift = 12; break;
65                 case 1: hp->io_tlb_shift = 13; break;
66                 case 2: hp->io_tlb_shift = 14; break;
67                 case 3: hp->io_tlb_shift = 16; break;
68                 default:
69                         printk(KERN_ERR PFX "Invalid IOTLB page size "
70                                "configuration 0x%x\n", hp->io_tlb_ps);
71                         hp->gatt = 0;
72                         hp->gatt_entries = 0;
73                         return -ENODEV;
74         }
75         hp->io_page_size = 1 << hp->io_tlb_shift;
76         hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
77
78         hp->iova_base = INREG64(hp->registers, HP_ZX1_IBASE) & ~0x1;
79         hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
80
81         hp->gart_size = HP_ZX1_GART_SIZE;
82         hp->gatt_entries = hp->gart_size / hp->io_page_size;
83
84         hp->io_pdir = phys_to_virt(INREG64(hp->registers, HP_ZX1_PDIR_BASE));
85         hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
86
87         if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
88                 hp->gatt = 0;
89                 hp->gatt_entries = 0;
90                 printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
91                        "GART disabled\n");
92                 return -ENODEV;
93         }
94
95         return 0;
96 }
97
98 static int __init hp_zx1_ioc_owner(u8 ioc_rev)
99 {
100         struct _hp_private *hp = &hp_private;
101
102         printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
103
104         /*
105          * Select an IOV page size no larger than system page size.
106          */
107         if (PAGE_SIZE >= KB(64)) {
108                 hp->io_tlb_shift = 16;
109                 hp->io_tlb_ps = 3;
110         } else if (PAGE_SIZE >= KB(16)) {
111                 hp->io_tlb_shift = 14;
112                 hp->io_tlb_ps = 2;
113         } else if (PAGE_SIZE >= KB(8)) {
114                 hp->io_tlb_shift = 13;
115                 hp->io_tlb_ps = 1;
116         } else {
117                 hp->io_tlb_shift = 12;
118                 hp->io_tlb_ps = 0;
119         }
120         hp->io_page_size = 1 << hp->io_tlb_shift;
121         hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
122
123         hp->iova_base = HP_ZX1_IOVA_BASE;
124         hp->gart_size = HP_ZX1_GART_SIZE;
125         hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
126
127         hp->gatt_entries = hp->gart_size / hp->io_page_size;
128         hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
129
130         return 0;
131 }
132
133 static int __init hp_zx1_ioc_init(void)
134 {
135         struct _hp_private *hp = &hp_private;
136         struct pci_dev *ioc;
137         int i;
138         u8 ioc_rev;
139
140         ioc = pci_find_device(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_ZX1_IOC, NULL);
141         if (!ioc) {
142                 printk(KERN_ERR PFX "Detected HP ZX1 AGP bridge but no IOC\n");
143                 return -ENODEV;
144         }
145         hp->ioc = ioc;
146
147         pci_read_config_byte(ioc, PCI_REVISION_ID, &ioc_rev);
148
149         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
150                 if (pci_resource_flags(ioc, i) == IORESOURCE_MEM) {
151                         hp->registers = (u8 *) ioremap(pci_resource_start(ioc, i),
152                                                     pci_resource_len(ioc, i));
153                         break;
154                 }
155         }
156         if (!hp->registers) {
157                 printk(KERN_ERR PFX "Detected HP ZX1 AGP bridge but no CSRs\n");
158                 return -ENODEV;
159         }
160
161         /*
162          * If the IOTLB is currently disabled, we can take it over.
163          * Otherwise, we have to share with sba_iommu.
164          */
165         hp->io_pdir_owner = (INREG64(hp->registers, HP_ZX1_IBASE) & 0x1) == 0;
166
167         if (hp->io_pdir_owner)
168                 return hp_zx1_ioc_owner(ioc_rev);
169
170         return hp_zx1_ioc_shared();
171 }
172
173 static int hp_zx1_fetch_size(void)
174 {
175         int size;
176
177         size = hp_private.gart_size / MB(1);
178         hp_zx1_sizes[0].size = size;
179         agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
180         return size;
181 }
182
183 static int hp_zx1_configure(void)
184 {
185         struct _hp_private *hp = &hp_private;
186
187         agp_bridge->gart_bus_addr = hp->gart_base;
188         agp_bridge->capndx = pci_find_capability(agp_bridge->dev, PCI_CAP_ID_AGP);
189         pci_read_config_dword(agp_bridge->dev,
190                 agp_bridge->capndx + PCI_AGP_STATUS, &agp_bridge->mode);
191
192         if (hp->io_pdir_owner) {
193                 OUTREG64(hp->registers, HP_ZX1_PDIR_BASE,
194                         virt_to_phys(hp->io_pdir));
195                 OUTREG64(hp->registers, HP_ZX1_TCNFG, hp->io_tlb_ps);
196                 OUTREG64(hp->registers, HP_ZX1_IMASK, ~(HP_ZX1_IOVA_SIZE - 1));
197                 OUTREG64(hp->registers, HP_ZX1_IBASE, hp->iova_base | 0x1);
198                 OUTREG64(hp->registers, HP_ZX1_PCOM,
199                         hp->iova_base | log2(HP_ZX1_IOVA_SIZE));
200                 INREG64(hp->registers, HP_ZX1_PCOM);
201         }
202
203         return 0;
204 }
205
206 static void hp_zx1_cleanup(void)
207 {
208         struct _hp_private *hp = &hp_private;
209
210         if (hp->io_pdir_owner)
211                 OUTREG64(hp->registers, HP_ZX1_IBASE, 0);
212         iounmap((void *) hp->registers);
213 }
214
215 static void hp_zx1_tlbflush(agp_memory * mem)
216 {
217         struct _hp_private *hp = &hp_private;
218
219         OUTREG64(hp->registers, HP_ZX1_PCOM, hp->gart_base | log2(hp->gart_size));
220         INREG64(hp->registers, HP_ZX1_PCOM);
221 }
222
223 static int hp_zx1_create_gatt_table(void)
224 {
225         struct _hp_private *hp = &hp_private;
226         int i;
227
228         if (hp->io_pdir_owner) {
229                 hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
230                                                 get_order(hp->io_pdir_size));
231                 if (!hp->io_pdir) {
232                         printk(KERN_ERR PFX "Couldn't allocate contiguous "
233                                 "memory for I/O PDIR\n");
234                         hp->gatt = 0;
235                         hp->gatt_entries = 0;
236                         return -ENOMEM;
237                 }
238                 memset(hp->io_pdir, 0, hp->io_pdir_size);
239
240                 hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
241         }
242
243         for (i = 0; i < hp->gatt_entries; i++) {
244                 hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
245         }
246
247         return 0;
248 }
249
250 static int hp_zx1_free_gatt_table(void)
251 {
252         struct _hp_private *hp = &hp_private;
253
254         if (hp->io_pdir_owner)
255                 free_pages((unsigned long) hp->io_pdir,
256                             get_order(hp->io_pdir_size));
257         else
258                 hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
259         return 0;
260 }
261
262 static int hp_zx1_insert_memory(agp_memory * mem, off_t pg_start, int type)
263 {
264         struct _hp_private *hp = &hp_private;
265         int i, k;
266         off_t j, io_pg_start;
267         int io_pg_count;
268
269         if (type != 0 || mem->type != 0) {
270                 return -EINVAL;
271         }
272
273         io_pg_start = hp->io_pages_per_kpage * pg_start;
274         io_pg_count = hp->io_pages_per_kpage * mem->page_count;
275         if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
276                 return -EINVAL;
277         }
278
279         j = io_pg_start;
280         while (j < (io_pg_start + io_pg_count)) {
281                 if (hp->gatt[j]) {
282                         return -EBUSY;
283                 }
284                 j++;
285         }
286
287         if (mem->is_flushed == FALSE) {
288                 CACHE_FLUSH();
289                 mem->is_flushed = TRUE;
290         }
291
292         for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
293                 unsigned long paddr;
294
295                 paddr = mem->memory[i];
296                 for (k = 0;
297                      k < hp->io_pages_per_kpage;
298                      k++, j++, paddr += hp->io_page_size) {
299                         hp->gatt[j] = agp_bridge->mask_memory(paddr, type);
300                 }
301         }
302
303         agp_bridge->tlb_flush(mem);
304         return 0;
305 }
306
307 static int hp_zx1_remove_memory(agp_memory * mem, off_t pg_start, int type)
308 {
309         struct _hp_private *hp = &hp_private;
310         int i, io_pg_start, io_pg_count;
311
312         if (type != 0 || mem->type != 0) {
313                 return -EINVAL;
314         }
315
316         io_pg_start = hp->io_pages_per_kpage * pg_start;
317         io_pg_count = hp->io_pages_per_kpage * mem->page_count;
318         for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
319                 hp->gatt[i] = agp_bridge->scratch_page;
320         }
321
322         agp_bridge->tlb_flush(mem);
323         return 0;
324 }
325
326 static unsigned long hp_zx1_mask_memory(unsigned long addr, int type)
327 {
328         return HP_ZX1_PDIR_VALID_BIT | addr;
329 }
330
331 static int __init hp_zx1_setup (struct pci_dev *pdev __attribute__((unused)))
332 {
333         agp_bridge->masks = hp_zx1_masks;
334         agp_bridge->dev_private_data = NULL;
335         agp_bridge->size_type = FIXED_APER_SIZE;
336         agp_bridge->needs_scratch_page = FALSE;
337         agp_bridge->configure = hp_zx1_configure;
338         agp_bridge->fetch_size = hp_zx1_fetch_size;
339         agp_bridge->cleanup = hp_zx1_cleanup;
340         agp_bridge->tlb_flush = hp_zx1_tlbflush;
341         agp_bridge->mask_memory = hp_zx1_mask_memory;
342         agp_bridge->agp_enable = agp_generic_enable;
343         agp_bridge->cache_flush = global_cache_flush;
344         agp_bridge->create_gatt_table = hp_zx1_create_gatt_table;
345         agp_bridge->free_gatt_table = hp_zx1_free_gatt_table;
346         agp_bridge->insert_memory = hp_zx1_insert_memory;
347         agp_bridge->remove_memory = hp_zx1_remove_memory;
348         agp_bridge->alloc_by_type = agp_generic_alloc_by_type;
349         agp_bridge->free_by_type = agp_generic_free_by_type;
350         agp_bridge->agp_alloc_page = agp_generic_alloc_page;
351         agp_bridge->agp_destroy_page = agp_generic_destroy_page;
352         agp_bridge->cant_use_aperture = 1;
353         return hp_zx1_ioc_init();
354 }
355
356 static int __init agp_find_supported_device(struct pci_dev *dev)
357 {
358         agp_bridge->dev = dev;
359
360         /* ZX1 LBAs can be either PCI or AGP bridges */
361         if (pci_find_capability(dev, PCI_CAP_ID_AGP)) {
362                 printk(KERN_INFO PFX "Detected HP ZX1 AGP chipset at %s\n",
363                         dev->slot_name);
364                 agp_bridge->type = HP_ZX1;
365                 agp_bridge->dev = dev;
366                 return hp_zx1_setup(dev);
367         }
368         return -ENODEV;
369 }
370
371 static struct agp_driver hp_agp_driver = {
372         .owner = THIS_MODULE,
373 };
374
375 static int __init agp_hp_probe (struct pci_dev *dev, const struct pci_device_id *ent)
376 {
377         if (agp_find_supported_device(dev) == 0) {
378                 hp_agp_driver.dev = dev;
379                 agp_register_driver(&hp_agp_driver);
380                 return 0;
381         }
382         return -ENODEV;
383 }
384
385 static struct pci_device_id agp_hp_pci_table[] __initdata = {
386         {
387         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
388         .class_mask     = ~0,
389         .vendor         = PCI_VENDOR_ID_HP,
390         .device         = PCI_DEVICE_ID_HP_ZX1_LBA,
391         .subvendor      = PCI_ANY_ID,
392         .subdevice      = PCI_ANY_ID,
393         },
394         { }
395 };
396
397 MODULE_DEVICE_TABLE(pci, agp_hp_pci_table);
398
399 static struct __initdata pci_driver agp_hp_pci_driver = {
400         .name           = "agpgart-hp",
401         .id_table       = agp_hp_pci_table,
402         .probe          = agp_hp_probe,
403 };
404
405 static int __init agp_hp_init(void)
406 {
407         int ret_val;
408
409         ret_val = pci_module_init(&agp_hp_pci_driver);
410         if (ret_val)
411                 agp_bridge->type = NOT_SUPPORTED;
412
413         return ret_val;
414 }
415
416 static void __exit agp_hp_cleanup(void)
417 {
418         agp_unregister_driver(&hp_agp_driver);
419         pci_unregister_driver(&agp_hp_pci_driver);
420 }
421
422 module_init(agp_hp_init);
423 module_exit(agp_hp_cleanup);
424
425 MODULE_LICENSE("GPL and additional rights");