HID: convert to dev_* prints
[linux-2.6/next.git] / drivers / char / agp / parisc-agp.c
blobf2492ecf082410da192b140d9a0615624ca79f3b
1 /*
2 * HP Quicksilver AGP GART routines
4 * Copyright (c) 2006, Kyle McMartin <kyle@parisc-linux.org>
6 * Based on drivers/char/agpgart/hp-agp.c which is
7 * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
8 * Bjorn Helgaas <bjorn.helgaas@hp.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/klist.h>
20 #include <linux/agp_backend.h>
21 #include <linux/log2.h>
23 #include <asm-parisc/parisc-device.h>
24 #include <asm-parisc/ropes.h>
26 #include "agp.h"
28 #define DRVNAME "quicksilver"
29 #define DRVPFX DRVNAME ": "
31 #define AGP8X_MODE_BIT 3
32 #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
34 static struct _parisc_agp_info {
35 void __iomem *ioc_regs;
36 void __iomem *lba_regs;
38 int lba_cap_offset;
40 u64 *gatt;
41 u64 gatt_entries;
43 u64 gart_base;
44 u64 gart_size;
46 int io_page_size;
47 int io_pages_per_kpage;
48 } parisc_agp_info;
50 static struct gatt_mask parisc_agp_masks[] =
53 .mask = SBA_PDIR_VALID_BIT,
54 .type = 0
58 static struct aper_size_info_fixed parisc_agp_sizes[] =
60 {0, 0, 0}, /* filled in by parisc_agp_fetch_size() */
63 static int
64 parisc_agp_fetch_size(void)
66 int size;
68 size = parisc_agp_info.gart_size / MB(1);
69 parisc_agp_sizes[0].size = size;
70 agp_bridge->current_size = (void *) &parisc_agp_sizes[0];
72 return size;
75 static int
76 parisc_agp_configure(void)
78 struct _parisc_agp_info *info = &parisc_agp_info;
80 agp_bridge->gart_bus_addr = info->gart_base;
81 agp_bridge->capndx = info->lba_cap_offset;
82 agp_bridge->mode = readl(info->lba_regs+info->lba_cap_offset+PCI_AGP_STATUS);
84 return 0;
87 static void
88 parisc_agp_tlbflush(struct agp_memory *mem)
90 struct _parisc_agp_info *info = &parisc_agp_info;
92 writeq(info->gart_base | ilog2(info->gart_size), info->ioc_regs+IOC_PCOM);
93 readq(info->ioc_regs+IOC_PCOM); /* flush */
96 static int
97 parisc_agp_create_gatt_table(struct agp_bridge_data *bridge)
99 struct _parisc_agp_info *info = &parisc_agp_info;
100 int i;
102 for (i = 0; i < info->gatt_entries; i++) {
103 info->gatt[i] = (unsigned long)agp_bridge->scratch_page;
106 return 0;
109 static int
110 parisc_agp_free_gatt_table(struct agp_bridge_data *bridge)
112 struct _parisc_agp_info *info = &parisc_agp_info;
114 info->gatt[0] = SBA_AGPGART_COOKIE;
116 return 0;
119 static int
120 parisc_agp_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
122 struct _parisc_agp_info *info = &parisc_agp_info;
123 int i, k;
124 off_t j, io_pg_start;
125 int io_pg_count;
127 if (type != 0 || mem->type != 0) {
128 return -EINVAL;
131 io_pg_start = info->io_pages_per_kpage * pg_start;
132 io_pg_count = info->io_pages_per_kpage * mem->page_count;
133 if ((io_pg_start + io_pg_count) > info->gatt_entries) {
134 return -EINVAL;
137 j = io_pg_start;
138 while (j < (io_pg_start + io_pg_count)) {
139 if (info->gatt[j])
140 return -EBUSY;
141 j++;
144 if (!mem->is_flushed) {
145 global_cache_flush();
146 mem->is_flushed = true;
149 for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
150 unsigned long paddr;
152 paddr = mem->memory[i];
153 for (k = 0;
154 k < info->io_pages_per_kpage;
155 k++, j++, paddr += info->io_page_size) {
156 info->gatt[j] =
157 agp_bridge->driver->mask_memory(agp_bridge,
158 paddr, type);
162 agp_bridge->driver->tlb_flush(mem);
164 return 0;
167 static int
168 parisc_agp_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
170 struct _parisc_agp_info *info = &parisc_agp_info;
171 int i, io_pg_start, io_pg_count;
173 if (type != 0 || mem->type != 0) {
174 return -EINVAL;
177 io_pg_start = info->io_pages_per_kpage * pg_start;
178 io_pg_count = info->io_pages_per_kpage * mem->page_count;
179 for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
180 info->gatt[i] = agp_bridge->scratch_page;
183 agp_bridge->driver->tlb_flush(mem);
184 return 0;
187 static unsigned long
188 parisc_agp_mask_memory(struct agp_bridge_data *bridge,
189 unsigned long addr, int type)
191 return SBA_PDIR_VALID_BIT | addr;
194 static void
195 parisc_agp_enable(struct agp_bridge_data *bridge, u32 mode)
197 struct _parisc_agp_info *info = &parisc_agp_info;
198 u32 command;
200 command = readl(info->lba_regs + info->lba_cap_offset + PCI_AGP_STATUS);
202 command = agp_collect_device_status(bridge, mode, command);
203 command |= 0x00000100;
205 writel(command, info->lba_regs + info->lba_cap_offset + PCI_AGP_COMMAND);
207 agp_device_command(command, (mode & AGP8X_MODE) != 0);
210 static const struct agp_bridge_driver parisc_agp_driver = {
211 .owner = THIS_MODULE,
212 .size_type = FIXED_APER_SIZE,
213 .configure = parisc_agp_configure,
214 .fetch_size = parisc_agp_fetch_size,
215 .tlb_flush = parisc_agp_tlbflush,
216 .mask_memory = parisc_agp_mask_memory,
217 .masks = parisc_agp_masks,
218 .agp_enable = parisc_agp_enable,
219 .cache_flush = global_cache_flush,
220 .create_gatt_table = parisc_agp_create_gatt_table,
221 .free_gatt_table = parisc_agp_free_gatt_table,
222 .insert_memory = parisc_agp_insert_memory,
223 .remove_memory = parisc_agp_remove_memory,
224 .alloc_by_type = agp_generic_alloc_by_type,
225 .free_by_type = agp_generic_free_by_type,
226 .agp_alloc_page = agp_generic_alloc_page,
227 .agp_alloc_pages = agp_generic_alloc_pages,
228 .agp_destroy_page = agp_generic_destroy_page,
229 .agp_destroy_pages = agp_generic_destroy_pages,
230 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
231 .cant_use_aperture = true,
234 static int __init
235 agp_ioc_init(void __iomem *ioc_regs)
237 struct _parisc_agp_info *info = &parisc_agp_info;
238 u64 iova_base, *io_pdir, io_tlb_ps;
239 int io_tlb_shift;
241 printk(KERN_INFO DRVPFX "IO PDIR shared with sba_iommu\n");
243 info->ioc_regs = ioc_regs;
245 io_tlb_ps = readq(info->ioc_regs+IOC_TCNFG);
246 switch (io_tlb_ps) {
247 case 0: io_tlb_shift = 12; break;
248 case 1: io_tlb_shift = 13; break;
249 case 2: io_tlb_shift = 14; break;
250 case 3: io_tlb_shift = 16; break;
251 default:
252 printk(KERN_ERR DRVPFX "Invalid IOTLB page size "
253 "configuration 0x%llx\n", io_tlb_ps);
254 info->gatt = NULL;
255 info->gatt_entries = 0;
256 return -ENODEV;
258 info->io_page_size = 1 << io_tlb_shift;
259 info->io_pages_per_kpage = PAGE_SIZE / info->io_page_size;
261 iova_base = readq(info->ioc_regs+IOC_IBASE) & ~0x1;
262 info->gart_base = iova_base + PLUTO_IOVA_SIZE - PLUTO_GART_SIZE;
264 info->gart_size = PLUTO_GART_SIZE;
265 info->gatt_entries = info->gart_size / info->io_page_size;
267 io_pdir = phys_to_virt(readq(info->ioc_regs+IOC_PDIR_BASE));
268 info->gatt = &io_pdir[(PLUTO_IOVA_SIZE/2) >> PAGE_SHIFT];
270 if (info->gatt[0] != SBA_AGPGART_COOKIE) {
271 info->gatt = NULL;
272 info->gatt_entries = 0;
273 printk(KERN_ERR DRVPFX "No reserved IO PDIR entry found; "
274 "GART disabled\n");
275 return -ENODEV;
278 return 0;
281 static int
282 lba_find_capability(int cap)
284 struct _parisc_agp_info *info = &parisc_agp_info;
285 u16 status;
286 u8 pos, id;
287 int ttl = 48;
289 status = readw(info->lba_regs + PCI_STATUS);
290 if (!(status & PCI_STATUS_CAP_LIST))
291 return 0;
292 pos = readb(info->lba_regs + PCI_CAPABILITY_LIST);
293 while (ttl-- && pos >= 0x40) {
294 pos &= ~3;
295 id = readb(info->lba_regs + pos + PCI_CAP_LIST_ID);
296 if (id == 0xff)
297 break;
298 if (id == cap)
299 return pos;
300 pos = readb(info->lba_regs + pos + PCI_CAP_LIST_NEXT);
302 return 0;
305 static int __init
306 agp_lba_init(void __iomem *lba_hpa)
308 struct _parisc_agp_info *info = &parisc_agp_info;
309 int cap;
311 info->lba_regs = lba_hpa;
312 info->lba_cap_offset = lba_find_capability(PCI_CAP_ID_AGP);
314 cap = readl(lba_hpa + info->lba_cap_offset) & 0xff;
315 if (cap != PCI_CAP_ID_AGP) {
316 printk(KERN_ERR DRVPFX "Invalid capability ID 0x%02x at 0x%x\n",
317 cap, info->lba_cap_offset);
318 return -ENODEV;
321 return 0;
324 static int __init
325 parisc_agp_setup(void __iomem *ioc_hpa, void __iomem *lba_hpa)
327 struct pci_dev *fake_bridge_dev = NULL;
328 struct agp_bridge_data *bridge;
329 int error = 0;
331 fake_bridge_dev = alloc_pci_dev();
332 if (!fake_bridge_dev) {
333 error = -ENOMEM;
334 goto fail;
337 error = agp_ioc_init(ioc_hpa);
338 if (error)
339 goto fail;
341 error = agp_lba_init(lba_hpa);
342 if (error)
343 goto fail;
345 bridge = agp_alloc_bridge();
346 if (!bridge) {
347 error = -ENOMEM;
348 goto fail;
350 bridge->driver = &parisc_agp_driver;
352 fake_bridge_dev->vendor = PCI_VENDOR_ID_HP;
353 fake_bridge_dev->device = PCI_DEVICE_ID_HP_PCIX_LBA;
354 bridge->dev = fake_bridge_dev;
356 error = agp_add_bridge(bridge);
358 fail:
359 return error;
362 static struct device *next_device(struct klist_iter *i) {
363 struct klist_node * n = klist_next(i);
364 return n ? container_of(n, struct device, knode_parent) : NULL;
367 static int
368 parisc_agp_init(void)
370 extern struct sba_device *sba_list;
372 int err = -1;
373 struct parisc_device *sba = NULL, *lba = NULL;
374 struct lba_device *lbadev = NULL;
375 struct device *dev = NULL;
376 struct klist_iter i;
378 if (!sba_list)
379 goto out;
381 /* Find our parent Pluto */
382 sba = sba_list->dev;
383 if (!IS_PLUTO(sba)) {
384 printk(KERN_INFO DRVPFX "No Pluto found, so no AGPGART for you.\n");
385 goto out;
388 /* Now search our Pluto for our precious AGP device... */
389 klist_iter_init(&sba->dev.klist_children, &i);
390 while ((dev = next_device(&i))) {
391 struct parisc_device *padev = to_parisc_device(dev);
392 if (IS_QUICKSILVER(padev))
393 lba = padev;
395 klist_iter_exit(&i);
397 if (!lba) {
398 printk(KERN_INFO DRVPFX "No AGP devices found.\n");
399 goto out;
402 lbadev = parisc_get_drvdata(lba);
404 /* w00t, let's go find our cookies... */
405 parisc_agp_setup(sba_list->ioc[0].ioc_hpa, lbadev->hba.base_addr);
407 return 0;
409 out:
410 return err;
413 module_init(parisc_agp_init);
415 MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
416 MODULE_LICENSE("GPL");