blob: 3f8f7fa6b0ff0177d626a9be477cfade0fe70552 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Serverworks AGPGART routines.
3 */
4
5#include <linux/module.h>
6#include <linux/pci.h>
7#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -08008#include <linux/string.h>
9#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/agp_backend.h>
11#include "agp.h"
12
13#define SVWRKS_COMMAND 0x04
14#define SVWRKS_APSIZE 0x10
15#define SVWRKS_MMBASE 0x14
16#define SVWRKS_CACHING 0x4b
17#define SVWRKS_AGP_ENABLE 0x60
18#define SVWRKS_FEATURE 0x68
19
20#define SVWRKS_SIZE_MASK 0xfe000000
21
22/* Memory mapped registers */
23#define SVWRKS_GART_CACHE 0x02
24#define SVWRKS_GATTBASE 0x04
25#define SVWRKS_TLBFLUSH 0x10
26#define SVWRKS_POSTFLUSH 0x14
27#define SVWRKS_DIRFLUSH 0x0c
28
29
30struct serverworks_page_map {
31 unsigned long *real;
32 unsigned long __iomem *remapped;
33};
34
35static struct _serverworks_private {
36 struct pci_dev *svrwrks_dev; /* device one */
37 volatile u8 __iomem *registers;
38 struct serverworks_page_map **gatt_pages;
39 int num_tables;
40 struct serverworks_page_map scratch_dir;
41
42 int gart_addr_ofs;
43 int mm_addr_ofs;
44} serverworks_private;
45
46static int serverworks_create_page_map(struct serverworks_page_map *page_map)
47{
48 int i;
49
50 page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
51 if (page_map->real == NULL) {
52 return -ENOMEM;
53 }
54 SetPageReserved(virt_to_page(page_map->real));
55 global_cache_flush();
Keir Fraser07eee782005-03-30 13:17:04 -080056 page_map->remapped = ioremap_nocache(virt_to_gart(page_map->real),
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 PAGE_SIZE);
58 if (page_map->remapped == NULL) {
59 ClearPageReserved(virt_to_page(page_map->real));
60 free_page((unsigned long) page_map->real);
61 page_map->real = NULL;
62 return -ENOMEM;
63 }
64 global_cache_flush();
65
66 for(i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++)
67 writel(agp_bridge->scratch_page, page_map->remapped+i);
68
69 return 0;
70}
71
72static void serverworks_free_page_map(struct serverworks_page_map *page_map)
73{
74 iounmap(page_map->remapped);
75 ClearPageReserved(virt_to_page(page_map->real));
76 free_page((unsigned long) page_map->real);
77}
78
79static void serverworks_free_gatt_pages(void)
80{
81 int i;
82 struct serverworks_page_map **tables;
83 struct serverworks_page_map *entry;
84
85 tables = serverworks_private.gatt_pages;
86 for(i = 0; i < serverworks_private.num_tables; i++) {
87 entry = tables[i];
88 if (entry != NULL) {
89 if (entry->real != NULL) {
90 serverworks_free_page_map(entry);
91 }
92 kfree(entry);
93 }
94 }
95 kfree(tables);
96}
97
98static int serverworks_create_gatt_pages(int nr_tables)
99{
100 struct serverworks_page_map **tables;
101 struct serverworks_page_map *entry;
102 int retval = 0;
103 int i;
104
Dave Jones0ea27d92005-10-20 15:12:16 -0700105 tables = kzalloc((nr_tables + 1) * sizeof(struct serverworks_page_map *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 GFP_KERNEL);
Dave Jones0ea27d92005-10-20 15:12:16 -0700107 if (tables == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -ENOMEM;
Dave Jones0ea27d92005-10-20 15:12:16 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (i = 0; i < nr_tables; i++) {
Dave Jones0ea27d92005-10-20 15:12:16 -0700111 entry = kzalloc(sizeof(struct serverworks_page_map), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (entry == NULL) {
113 retval = -ENOMEM;
114 break;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 tables[i] = entry;
117 retval = serverworks_create_page_map(entry);
118 if (retval != 0) break;
119 }
120 serverworks_private.num_tables = nr_tables;
121 serverworks_private.gatt_pages = tables;
122
123 if (retval != 0) serverworks_free_gatt_pages();
124
125 return retval;
126}
127
128#define SVRWRKS_GET_GATT(addr) (serverworks_private.gatt_pages[\
129 GET_PAGE_DIR_IDX(addr)]->remapped)
130
131#ifndef GET_PAGE_DIR_OFF
132#define GET_PAGE_DIR_OFF(addr) (addr >> 22)
133#endif
134
135#ifndef GET_PAGE_DIR_IDX
136#define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
137 GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
138#endif
139
140#ifndef GET_GATT_OFF
141#define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
142#endif
143
144static int serverworks_create_gatt_table(struct agp_bridge_data *bridge)
145{
146 struct aper_size_info_lvl2 *value;
147 struct serverworks_page_map page_dir;
148 int retval;
149 u32 temp;
150 int i;
151
152 value = A_SIZE_LVL2(agp_bridge->current_size);
153 retval = serverworks_create_page_map(&page_dir);
154 if (retval != 0) {
155 return retval;
156 }
157 retval = serverworks_create_page_map(&serverworks_private.scratch_dir);
158 if (retval != 0) {
159 serverworks_free_page_map(&page_dir);
160 return retval;
161 }
162 /* Create a fake scratch directory */
163 for(i = 0; i < 1024; i++) {
164 writel(agp_bridge->scratch_page, serverworks_private.scratch_dir.remapped+i);
Keir Fraser07eee782005-03-30 13:17:04 -0800165 writel(virt_to_gart(serverworks_private.scratch_dir.real) | 1, page_dir.remapped+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
168 retval = serverworks_create_gatt_pages(value->num_entries / 1024);
169 if (retval != 0) {
170 serverworks_free_page_map(&page_dir);
171 serverworks_free_page_map(&serverworks_private.scratch_dir);
172 return retval;
173 }
174
175 agp_bridge->gatt_table_real = (u32 *)page_dir.real;
176 agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
Keir Fraser07eee782005-03-30 13:17:04 -0800177 agp_bridge->gatt_bus_addr = virt_to_gart(page_dir.real);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* Get the address for the gart region.
180 * This is a bus address even on the alpha, b/c its
181 * used to program the agp master not the cpu
182 */
183
184 pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
185 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
186
187 /* Calculate the agp offset */
188
189 for(i = 0; i < value->num_entries / 1024; i++)
Keir Fraser07eee782005-03-30 13:17:04 -0800190 writel(virt_to_gart(serverworks_private.gatt_pages[i]->real)|1, page_dir.remapped+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return 0;
193}
194
195static int serverworks_free_gatt_table(struct agp_bridge_data *bridge)
196{
197 struct serverworks_page_map page_dir;
198
199 page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
200 page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
201
202 serverworks_free_gatt_pages();
203 serverworks_free_page_map(&page_dir);
204 serverworks_free_page_map(&serverworks_private.scratch_dir);
205 return 0;
206}
207
208static int serverworks_fetch_size(void)
209{
210 int i;
211 u32 temp;
212 u32 temp2;
213 struct aper_size_info_lvl2 *values;
214
215 values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
216 pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
217 pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,
218 SVWRKS_SIZE_MASK);
219 pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp2);
220 pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,temp);
221 temp2 &= SVWRKS_SIZE_MASK;
222
223 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
224 if (temp2 == values[i].size_value) {
225 agp_bridge->previous_size =
226 agp_bridge->current_size = (void *) (values + i);
227
228 agp_bridge->aperture_size_idx = i;
229 return values[i].size;
230 }
231 }
232
233 return 0;
234}
235
236/*
237 * This routine could be implemented by taking the addresses
238 * written to the GATT, and flushing them individually. However
239 * currently it just flushes the whole table. Which is probably
240 * more efficent, since agp_memory blocks can be a large number of
241 * entries.
242 */
243static void serverworks_tlbflush(struct agp_memory *temp)
244{
Dave Jones0ff541d2005-09-23 15:59:37 -0700245 unsigned long timeout;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 writeb(1, serverworks_private.registers+SVWRKS_POSTFLUSH);
Dave Jones0ff541d2005-09-23 15:59:37 -0700248 timeout = jiffies + 3*HZ;
249 while (readb(serverworks_private.registers+SVWRKS_POSTFLUSH) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cpu_relax();
Dave Jones0ff541d2005-09-23 15:59:37 -0700251 if (time_after(jiffies, timeout)) {
252 printk(KERN_ERR PFX "TLB post flush took more than 3 seconds\n");
253 break;
254 }
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 writel(1, serverworks_private.registers+SVWRKS_DIRFLUSH);
Dave Jones0ff541d2005-09-23 15:59:37 -0700258 timeout = jiffies + 3*HZ;
259 while (readl(serverworks_private.registers+SVWRKS_DIRFLUSH) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 cpu_relax();
Dave Jones0ff541d2005-09-23 15:59:37 -0700261 if (time_after(jiffies, timeout)) {
262 printk(KERN_ERR PFX "TLB Dir flush took more than 3 seconds\n");
263 break;
264 }
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static int serverworks_configure(void)
269{
270 struct aper_size_info_lvl2 *current_size;
271 u32 temp;
272 u8 enable_reg;
273 u16 cap_reg;
274
275 current_size = A_SIZE_LVL2(agp_bridge->current_size);
276
277 /* Get the memory mapped registers */
278 pci_read_config_dword(agp_bridge->dev, serverworks_private.mm_addr_ofs, &temp);
279 temp = (temp & PCI_BASE_ADDRESS_MEM_MASK);
280 serverworks_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
281 if (!serverworks_private.registers) {
282 printk (KERN_ERR PFX "Unable to ioremap() memory.\n");
283 return -ENOMEM;
284 }
285
286 writeb(0xA, serverworks_private.registers+SVWRKS_GART_CACHE);
287 readb(serverworks_private.registers+SVWRKS_GART_CACHE); /* PCI Posting. */
288
289 writel(agp_bridge->gatt_bus_addr, serverworks_private.registers+SVWRKS_GATTBASE);
290 readl(serverworks_private.registers+SVWRKS_GATTBASE); /* PCI Posting. */
291
292 cap_reg = readw(serverworks_private.registers+SVWRKS_COMMAND);
293 cap_reg &= ~0x0007;
294 cap_reg |= 0x4;
295 writew(cap_reg, serverworks_private.registers+SVWRKS_COMMAND);
296 readw(serverworks_private.registers+SVWRKS_COMMAND);
297
298 pci_read_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, &enable_reg);
299 enable_reg |= 0x1; /* Agp Enable bit */
300 pci_write_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, enable_reg);
301 serverworks_tlbflush(NULL);
302
303 agp_bridge->capndx = pci_find_capability(serverworks_private.svrwrks_dev, PCI_CAP_ID_AGP);
304
305 /* Fill in the mode register */
306 pci_read_config_dword(serverworks_private.svrwrks_dev,
307 agp_bridge->capndx+PCI_AGP_STATUS, &agp_bridge->mode);
308
309 pci_read_config_byte(agp_bridge->dev, SVWRKS_CACHING, &enable_reg);
310 enable_reg &= ~0x3;
311 pci_write_config_byte(agp_bridge->dev, SVWRKS_CACHING, enable_reg);
312
313 pci_read_config_byte(agp_bridge->dev, SVWRKS_FEATURE, &enable_reg);
314 enable_reg |= (1<<6);
315 pci_write_config_byte(agp_bridge->dev,SVWRKS_FEATURE, enable_reg);
316
317 return 0;
318}
319
320static void serverworks_cleanup(void)
321{
322 iounmap((void __iomem *) serverworks_private.registers);
323}
324
325static int serverworks_insert_memory(struct agp_memory *mem,
326 off_t pg_start, int type)
327{
328 int i, j, num_entries;
329 unsigned long __iomem *cur_gatt;
330 unsigned long addr;
331
332 num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
333
334 if (type != 0 || mem->type != 0) {
335 return -EINVAL;
336 }
337 if ((pg_start + mem->page_count) > num_entries) {
338 return -EINVAL;
339 }
340
341 j = pg_start;
342 while (j < (pg_start + mem->page_count)) {
343 addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
344 cur_gatt = SVRWRKS_GET_GATT(addr);
345 if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
346 return -EBUSY;
347 j++;
348 }
349
350 if (mem->is_flushed == FALSE) {
351 global_cache_flush();
352 mem->is_flushed = TRUE;
353 }
354
355 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
356 addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
357 cur_gatt = SVRWRKS_GET_GATT(addr);
358 writel(agp_bridge->driver->mask_memory(agp_bridge, mem->memory[i], mem->type), cur_gatt+GET_GATT_OFF(addr));
359 }
360 serverworks_tlbflush(mem);
361 return 0;
362}
363
364static int serverworks_remove_memory(struct agp_memory *mem, off_t pg_start,
365 int type)
366{
367 int i;
368 unsigned long __iomem *cur_gatt;
369 unsigned long addr;
370
371 if (type != 0 || mem->type != 0) {
372 return -EINVAL;
373 }
374
375 global_cache_flush();
376 serverworks_tlbflush(mem);
377
378 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
379 addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
380 cur_gatt = SVRWRKS_GET_GATT(addr);
381 writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
382 }
383
384 serverworks_tlbflush(mem);
385 return 0;
386}
387
388static struct gatt_mask serverworks_masks[] =
389{
390 {.mask = 1, .type = 0}
391};
392
393static struct aper_size_info_lvl2 serverworks_sizes[7] =
394{
395 {2048, 524288, 0x80000000},
396 {1024, 262144, 0xc0000000},
397 {512, 131072, 0xe0000000},
398 {256, 65536, 0xf0000000},
399 {128, 32768, 0xf8000000},
400 {64, 16384, 0xfc000000},
401 {32, 8192, 0xfe000000}
402};
403
404static void serverworks_agp_enable(struct agp_bridge_data *bridge, u32 mode)
405{
406 u32 command;
407
408 pci_read_config_dword(serverworks_private.svrwrks_dev,
409 bridge->capndx + PCI_AGP_STATUS,
410 &command);
411
412 command = agp_collect_device_status(bridge, mode, command);
413
414 command &= ~0x10; /* disable FW */
415 command &= ~0x08;
416
417 command |= 0x100;
418
419 pci_write_config_dword(serverworks_private.svrwrks_dev,
420 bridge->capndx + PCI_AGP_COMMAND,
421 command);
422
423 agp_device_command(command, 0);
424}
425
Adrian Bunk408b6642005-05-01 08:59:29 -0700426static struct agp_bridge_driver sworks_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 .owner = THIS_MODULE,
428 .aperture_sizes = serverworks_sizes,
429 .size_type = LVL2_APER_SIZE,
430 .num_aperture_sizes = 7,
431 .configure = serverworks_configure,
432 .fetch_size = serverworks_fetch_size,
433 .cleanup = serverworks_cleanup,
434 .tlb_flush = serverworks_tlbflush,
435 .mask_memory = agp_generic_mask_memory,
436 .masks = serverworks_masks,
437 .agp_enable = serverworks_agp_enable,
438 .cache_flush = global_cache_flush,
439 .create_gatt_table = serverworks_create_gatt_table,
440 .free_gatt_table = serverworks_free_gatt_table,
441 .insert_memory = serverworks_insert_memory,
442 .remove_memory = serverworks_remove_memory,
443 .alloc_by_type = agp_generic_alloc_by_type,
444 .free_by_type = agp_generic_free_by_type,
445 .agp_alloc_page = agp_generic_alloc_page,
446 .agp_destroy_page = agp_generic_destroy_page,
447};
448
449static int __devinit agp_serverworks_probe(struct pci_dev *pdev,
450 const struct pci_device_id *ent)
451{
452 struct agp_bridge_data *bridge;
453 struct pci_dev *bridge_dev;
454 u32 temp, temp2;
455 u8 cap_ptr = 0;
456
457 /* Everything is on func 1 here so we are hardcoding function one */
458 bridge_dev = pci_find_slot((unsigned int)pdev->bus->number,
459 PCI_DEVFN(0, 1));
460 if (!bridge_dev) {
461 printk(KERN_INFO PFX "Detected a Serverworks chipset "
462 "but could not find the secondary device.\n");
463 return -ENODEV;
464 }
465
466 cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
467
468 switch (pdev->device) {
469 case 0x0006:
470 /* ServerWorks CNB20HE
471 Fail silently.*/
472 printk (KERN_ERR PFX "Detected ServerWorks CNB20HE chipset: No AGP present.\n");
473 return -ENODEV;
474
475 case PCI_DEVICE_ID_SERVERWORKS_HE:
476 case PCI_DEVICE_ID_SERVERWORKS_LE:
477 case 0x0007:
478 break;
479
480 default:
481 if (cap_ptr)
482 printk(KERN_ERR PFX "Unsupported Serverworks chipset "
483 "(device id: %04x)\n", pdev->device);
484 return -ENODEV;
485 }
486
487 serverworks_private.svrwrks_dev = bridge_dev;
488 serverworks_private.gart_addr_ofs = 0x10;
489
490 pci_read_config_dword(pdev, SVWRKS_APSIZE, &temp);
491 if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
492 pci_read_config_dword(pdev, SVWRKS_APSIZE + 4, &temp2);
493 if (temp2 != 0) {
494 printk(KERN_INFO PFX "Detected 64 bit aperture address, "
495 "but top bits are not zero. Disabling agp\n");
496 return -ENODEV;
497 }
498 serverworks_private.mm_addr_ofs = 0x18;
499 } else
500 serverworks_private.mm_addr_ofs = 0x14;
501
502 pci_read_config_dword(pdev, serverworks_private.mm_addr_ofs, &temp);
503 if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
504 pci_read_config_dword(pdev,
505 serverworks_private.mm_addr_ofs + 4, &temp2);
506 if (temp2 != 0) {
507 printk(KERN_INFO PFX "Detected 64 bit MMIO address, "
508 "but top bits are not zero. Disabling agp\n");
509 return -ENODEV;
510 }
511 }
512
513 bridge = agp_alloc_bridge();
514 if (!bridge)
515 return -ENOMEM;
516
517 bridge->driver = &sworks_driver;
518 bridge->dev_private_data = &serverworks_private,
519 bridge->dev = pdev;
520
521 pci_set_drvdata(pdev, bridge);
522 return agp_add_bridge(bridge);
523}
524
525static void __devexit agp_serverworks_remove(struct pci_dev *pdev)
526{
527 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
528
529 agp_remove_bridge(bridge);
530 agp_put_bridge(bridge);
531}
532
533static struct pci_device_id agp_serverworks_pci_table[] = {
534 {
535 .class = (PCI_CLASS_BRIDGE_HOST << 8),
536 .class_mask = ~0,
537 .vendor = PCI_VENDOR_ID_SERVERWORKS,
538 .device = PCI_ANY_ID,
539 .subvendor = PCI_ANY_ID,
540 .subdevice = PCI_ANY_ID,
541 },
542 { }
543};
544
545MODULE_DEVICE_TABLE(pci, agp_serverworks_pci_table);
546
547static struct pci_driver agp_serverworks_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .name = "agpgart-serverworks",
549 .id_table = agp_serverworks_pci_table,
550 .probe = agp_serverworks_probe,
551 .remove = agp_serverworks_remove,
552};
553
554static int __init agp_serverworks_init(void)
555{
556 if (agp_off)
557 return -EINVAL;
558 return pci_register_driver(&agp_serverworks_pci_driver);
559}
560
561static void __exit agp_serverworks_cleanup(void)
562{
563 pci_unregister_driver(&agp_serverworks_pci_driver);
564}
565
566module_init(agp_serverworks_init);
567module_exit(agp_serverworks_cleanup);
568
569MODULE_LICENSE("GPL and additional rights");
570