2 * Serverworks AGPGART routines.
5 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/jiffies.h>
11 #include <linux/agp_backend.h>
12 #include <asm/set_memory.h>
15 #define SVWRKS_COMMAND 0x04
16 #define SVWRKS_APSIZE 0x10
17 #define SVWRKS_MMBASE 0x14
18 #define SVWRKS_CACHING 0x4b
19 #define SVWRKS_AGP_ENABLE 0x60
20 #define SVWRKS_FEATURE 0x68
22 #define SVWRKS_SIZE_MASK 0xfe000000
24 /* Memory mapped registers */
25 #define SVWRKS_GART_CACHE 0x02
26 #define SVWRKS_GATTBASE 0x04
27 #define SVWRKS_TLBFLUSH 0x10
28 #define SVWRKS_POSTFLUSH 0x14
29 #define SVWRKS_DIRFLUSH 0x0c
32 struct serverworks_page_map
{
34 unsigned long __iomem
*remapped
;
37 static struct _serverworks_private
{
38 struct pci_dev
*svrwrks_dev
; /* device one */
39 volatile u8 __iomem
*registers
;
40 struct serverworks_page_map
**gatt_pages
;
42 struct serverworks_page_map scratch_dir
;
46 } serverworks_private
;
48 static int serverworks_create_page_map(struct serverworks_page_map
*page_map
)
52 page_map
->real
= (unsigned long *) __get_free_page(GFP_KERNEL
);
53 if (page_map
->real
== NULL
) {
57 set_memory_uc((unsigned long)page_map
->real
, 1);
58 page_map
->remapped
= page_map
->real
;
60 for (i
= 0; i
< PAGE_SIZE
/ sizeof(unsigned long); i
++)
61 writel(agp_bridge
->scratch_page
, page_map
->remapped
+i
);
62 /* Red Pen: Everyone else does pci posting flush here */
67 static void serverworks_free_page_map(struct serverworks_page_map
*page_map
)
69 set_memory_wb((unsigned long)page_map
->real
, 1);
70 free_page((unsigned long) page_map
->real
);
73 static void serverworks_free_gatt_pages(void)
76 struct serverworks_page_map
**tables
;
77 struct serverworks_page_map
*entry
;
79 tables
= serverworks_private
.gatt_pages
;
80 for (i
= 0; i
< serverworks_private
.num_tables
; i
++) {
83 if (entry
->real
!= NULL
) {
84 serverworks_free_page_map(entry
);
92 static int serverworks_create_gatt_pages(int nr_tables
)
94 struct serverworks_page_map
**tables
;
95 struct serverworks_page_map
*entry
;
99 tables
= kzalloc((nr_tables
+ 1) * sizeof(struct serverworks_page_map
*),
104 for (i
= 0; i
< nr_tables
; i
++) {
105 entry
= kzalloc(sizeof(struct serverworks_page_map
), GFP_KERNEL
);
111 retval
= serverworks_create_page_map(entry
);
112 if (retval
!= 0) break;
114 serverworks_private
.num_tables
= nr_tables
;
115 serverworks_private
.gatt_pages
= tables
;
117 if (retval
!= 0) serverworks_free_gatt_pages();
122 #define SVRWRKS_GET_GATT(addr) (serverworks_private.gatt_pages[\
123 GET_PAGE_DIR_IDX(addr)]->remapped)
125 #ifndef GET_PAGE_DIR_OFF
126 #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
129 #ifndef GET_PAGE_DIR_IDX
130 #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
131 GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
135 #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
138 static int serverworks_create_gatt_table(struct agp_bridge_data
*bridge
)
140 struct aper_size_info_lvl2
*value
;
141 struct serverworks_page_map page_dir
;
146 value
= A_SIZE_LVL2(agp_bridge
->current_size
);
147 retval
= serverworks_create_page_map(&page_dir
);
151 retval
= serverworks_create_page_map(&serverworks_private
.scratch_dir
);
153 serverworks_free_page_map(&page_dir
);
156 /* Create a fake scratch directory */
157 for (i
= 0; i
< 1024; i
++) {
158 writel(agp_bridge
->scratch_page
, serverworks_private
.scratch_dir
.remapped
+i
);
159 writel(virt_to_phys(serverworks_private
.scratch_dir
.real
) | 1, page_dir
.remapped
+i
);
162 retval
= serverworks_create_gatt_pages(value
->num_entries
/ 1024);
164 serverworks_free_page_map(&page_dir
);
165 serverworks_free_page_map(&serverworks_private
.scratch_dir
);
169 agp_bridge
->gatt_table_real
= (u32
*)page_dir
.real
;
170 agp_bridge
->gatt_table
= (u32 __iomem
*)page_dir
.remapped
;
171 agp_bridge
->gatt_bus_addr
= virt_to_phys(page_dir
.real
);
173 /* Get the address for the gart region.
174 * This is a bus address even on the alpha, b/c its
175 * used to program the agp master not the cpu
178 pci_read_config_dword(agp_bridge
->dev
,serverworks_private
.gart_addr_ofs
,&temp
);
179 agp_bridge
->gart_bus_addr
= (temp
& PCI_BASE_ADDRESS_MEM_MASK
);
181 /* Calculate the agp offset */
182 for (i
= 0; i
< value
->num_entries
/ 1024; i
++)
183 writel(virt_to_phys(serverworks_private
.gatt_pages
[i
]->real
)|1, page_dir
.remapped
+i
);
188 static int serverworks_free_gatt_table(struct agp_bridge_data
*bridge
)
190 struct serverworks_page_map page_dir
;
192 page_dir
.real
= (unsigned long *)agp_bridge
->gatt_table_real
;
193 page_dir
.remapped
= (unsigned long __iomem
*)agp_bridge
->gatt_table
;
195 serverworks_free_gatt_pages();
196 serverworks_free_page_map(&page_dir
);
197 serverworks_free_page_map(&serverworks_private
.scratch_dir
);
201 static int serverworks_fetch_size(void)
206 struct aper_size_info_lvl2
*values
;
208 values
= A_SIZE_LVL2(agp_bridge
->driver
->aperture_sizes
);
209 pci_read_config_dword(agp_bridge
->dev
,serverworks_private
.gart_addr_ofs
,&temp
);
210 pci_write_config_dword(agp_bridge
->dev
,serverworks_private
.gart_addr_ofs
,
212 pci_read_config_dword(agp_bridge
->dev
,serverworks_private
.gart_addr_ofs
,&temp2
);
213 pci_write_config_dword(agp_bridge
->dev
,serverworks_private
.gart_addr_ofs
,temp
);
214 temp2
&= SVWRKS_SIZE_MASK
;
216 for (i
= 0; i
< agp_bridge
->driver
->num_aperture_sizes
; i
++) {
217 if (temp2
== values
[i
].size_value
) {
218 agp_bridge
->previous_size
=
219 agp_bridge
->current_size
= (void *) (values
+ i
);
221 agp_bridge
->aperture_size_idx
= i
;
222 return values
[i
].size
;
230 * This routine could be implemented by taking the addresses
231 * written to the GATT, and flushing them individually. However
232 * currently it just flushes the whole table. Which is probably
233 * more efficient, since agp_memory blocks can be a large number of
236 static void serverworks_tlbflush(struct agp_memory
*temp
)
238 unsigned long timeout
;
240 writeb(1, serverworks_private
.registers
+SVWRKS_POSTFLUSH
);
241 timeout
= jiffies
+ 3*HZ
;
242 while (readb(serverworks_private
.registers
+SVWRKS_POSTFLUSH
) == 1) {
244 if (time_after(jiffies
, timeout
)) {
245 dev_err(&serverworks_private
.svrwrks_dev
->dev
,
246 "TLB post flush took more than 3 seconds\n");
251 writel(1, serverworks_private
.registers
+SVWRKS_DIRFLUSH
);
252 timeout
= jiffies
+ 3*HZ
;
253 while (readl(serverworks_private
.registers
+SVWRKS_DIRFLUSH
) == 1) {
255 if (time_after(jiffies
, timeout
)) {
256 dev_err(&serverworks_private
.svrwrks_dev
->dev
,
257 "TLB Dir flush took more than 3 seconds\n");
263 static int serverworks_configure(void)
265 struct aper_size_info_lvl2
*current_size
;
270 current_size
= A_SIZE_LVL2(agp_bridge
->current_size
);
272 /* Get the memory mapped registers */
273 pci_read_config_dword(agp_bridge
->dev
, serverworks_private
.mm_addr_ofs
, &temp
);
274 temp
= (temp
& PCI_BASE_ADDRESS_MEM_MASK
);
275 serverworks_private
.registers
= (volatile u8 __iomem
*) ioremap(temp
, 4096);
276 if (!serverworks_private
.registers
) {
277 dev_err(&agp_bridge
->dev
->dev
, "can't ioremap(%#x)\n", temp
);
281 writeb(0xA, serverworks_private
.registers
+SVWRKS_GART_CACHE
);
282 readb(serverworks_private
.registers
+SVWRKS_GART_CACHE
); /* PCI Posting. */
284 writel(agp_bridge
->gatt_bus_addr
, serverworks_private
.registers
+SVWRKS_GATTBASE
);
285 readl(serverworks_private
.registers
+SVWRKS_GATTBASE
); /* PCI Posting. */
287 cap_reg
= readw(serverworks_private
.registers
+SVWRKS_COMMAND
);
290 writew(cap_reg
, serverworks_private
.registers
+SVWRKS_COMMAND
);
291 readw(serverworks_private
.registers
+SVWRKS_COMMAND
);
293 pci_read_config_byte(serverworks_private
.svrwrks_dev
,SVWRKS_AGP_ENABLE
, &enable_reg
);
294 enable_reg
|= 0x1; /* Agp Enable bit */
295 pci_write_config_byte(serverworks_private
.svrwrks_dev
,SVWRKS_AGP_ENABLE
, enable_reg
);
296 serverworks_tlbflush(NULL
);
298 agp_bridge
->capndx
= pci_find_capability(serverworks_private
.svrwrks_dev
, PCI_CAP_ID_AGP
);
300 /* Fill in the mode register */
301 pci_read_config_dword(serverworks_private
.svrwrks_dev
,
302 agp_bridge
->capndx
+PCI_AGP_STATUS
, &agp_bridge
->mode
);
304 pci_read_config_byte(agp_bridge
->dev
, SVWRKS_CACHING
, &enable_reg
);
306 pci_write_config_byte(agp_bridge
->dev
, SVWRKS_CACHING
, enable_reg
);
308 pci_read_config_byte(agp_bridge
->dev
, SVWRKS_FEATURE
, &enable_reg
);
309 enable_reg
|= (1<<6);
310 pci_write_config_byte(agp_bridge
->dev
,SVWRKS_FEATURE
, enable_reg
);
315 static void serverworks_cleanup(void)
317 iounmap((void __iomem
*) serverworks_private
.registers
);
320 static int serverworks_insert_memory(struct agp_memory
*mem
,
321 off_t pg_start
, int type
)
323 int i
, j
, num_entries
;
324 unsigned long __iomem
*cur_gatt
;
327 num_entries
= A_SIZE_LVL2(agp_bridge
->current_size
)->num_entries
;
329 if (type
!= 0 || mem
->type
!= 0) {
332 if ((pg_start
+ mem
->page_count
) > num_entries
) {
337 while (j
< (pg_start
+ mem
->page_count
)) {
338 addr
= (j
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
339 cur_gatt
= SVRWRKS_GET_GATT(addr
);
340 if (!PGE_EMPTY(agp_bridge
, readl(cur_gatt
+GET_GATT_OFF(addr
))))
345 if (!mem
->is_flushed
) {
346 global_cache_flush();
347 mem
->is_flushed
= true;
350 for (i
= 0, j
= pg_start
; i
< mem
->page_count
; i
++, j
++) {
351 addr
= (j
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
352 cur_gatt
= SVRWRKS_GET_GATT(addr
);
353 writel(agp_bridge
->driver
->mask_memory(agp_bridge
,
354 page_to_phys(mem
->pages
[i
]), mem
->type
),
355 cur_gatt
+GET_GATT_OFF(addr
));
357 serverworks_tlbflush(mem
);
361 static int serverworks_remove_memory(struct agp_memory
*mem
, off_t pg_start
,
365 unsigned long __iomem
*cur_gatt
;
368 if (type
!= 0 || mem
->type
!= 0) {
372 global_cache_flush();
373 serverworks_tlbflush(mem
);
375 for (i
= pg_start
; i
< (mem
->page_count
+ pg_start
); i
++) {
376 addr
= (i
* PAGE_SIZE
) + agp_bridge
->gart_bus_addr
;
377 cur_gatt
= SVRWRKS_GET_GATT(addr
);
378 writel(agp_bridge
->scratch_page
, cur_gatt
+GET_GATT_OFF(addr
));
381 serverworks_tlbflush(mem
);
385 static const struct gatt_mask serverworks_masks
[] =
387 {.mask
= 1, .type
= 0}
390 static const struct aper_size_info_lvl2 serverworks_sizes
[7] =
392 {2048, 524288, 0x80000000},
393 {1024, 262144, 0xc0000000},
394 {512, 131072, 0xe0000000},
395 {256, 65536, 0xf0000000},
396 {128, 32768, 0xf8000000},
397 {64, 16384, 0xfc000000},
398 {32, 8192, 0xfe000000}
401 static void serverworks_agp_enable(struct agp_bridge_data
*bridge
, u32 mode
)
405 pci_read_config_dword(serverworks_private
.svrwrks_dev
,
406 bridge
->capndx
+ PCI_AGP_STATUS
,
409 command
= agp_collect_device_status(bridge
, mode
, command
);
411 command
&= ~0x10; /* disable FW */
416 pci_write_config_dword(serverworks_private
.svrwrks_dev
,
417 bridge
->capndx
+ PCI_AGP_COMMAND
,
420 agp_device_command(command
, false);
423 static const struct agp_bridge_driver sworks_driver
= {
424 .owner
= THIS_MODULE
,
425 .aperture_sizes
= serverworks_sizes
,
426 .size_type
= LVL2_APER_SIZE
,
427 .num_aperture_sizes
= 7,
428 .configure
= serverworks_configure
,
429 .fetch_size
= serverworks_fetch_size
,
430 .cleanup
= serverworks_cleanup
,
431 .tlb_flush
= serverworks_tlbflush
,
432 .mask_memory
= agp_generic_mask_memory
,
433 .masks
= serverworks_masks
,
434 .agp_enable
= serverworks_agp_enable
,
435 .cache_flush
= global_cache_flush
,
436 .create_gatt_table
= serverworks_create_gatt_table
,
437 .free_gatt_table
= serverworks_free_gatt_table
,
438 .insert_memory
= serverworks_insert_memory
,
439 .remove_memory
= serverworks_remove_memory
,
440 .alloc_by_type
= agp_generic_alloc_by_type
,
441 .free_by_type
= agp_generic_free_by_type
,
442 .agp_alloc_page
= agp_generic_alloc_page
,
443 .agp_alloc_pages
= agp_generic_alloc_pages
,
444 .agp_destroy_page
= agp_generic_destroy_page
,
445 .agp_destroy_pages
= agp_generic_destroy_pages
,
446 .agp_type_to_mask_type
= agp_generic_type_to_mask_type
,
449 static int agp_serverworks_probe(struct pci_dev
*pdev
,
450 const struct pci_device_id
*ent
)
452 struct agp_bridge_data
*bridge
;
453 struct pci_dev
*bridge_dev
;
457 cap_ptr
= pci_find_capability(pdev
, PCI_CAP_ID_AGP
);
459 switch (pdev
->device
) {
461 dev_err(&pdev
->dev
, "ServerWorks CNB20HE is unsupported due to lack of documentation\n");
464 case PCI_DEVICE_ID_SERVERWORKS_HE
:
465 case PCI_DEVICE_ID_SERVERWORKS_LE
:
471 dev_err(&pdev
->dev
, "unsupported Serverworks chipset "
472 "[%04x/%04x]\n", pdev
->vendor
, pdev
->device
);
476 /* Everything is on func 1 here so we are hardcoding function one */
477 bridge_dev
= pci_get_bus_and_slot((unsigned int)pdev
->bus
->number
,
480 dev_info(&pdev
->dev
, "can't find secondary device\n");
484 serverworks_private
.svrwrks_dev
= bridge_dev
;
485 serverworks_private
.gart_addr_ofs
= 0x10;
487 pci_read_config_dword(pdev
, SVWRKS_APSIZE
, &temp
);
488 if (temp
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
489 pci_read_config_dword(pdev
, SVWRKS_APSIZE
+ 4, &temp2
);
491 dev_info(&pdev
->dev
, "64 bit aperture address, "
492 "but top bits are not zero; disabling AGP\n");
495 serverworks_private
.mm_addr_ofs
= 0x18;
497 serverworks_private
.mm_addr_ofs
= 0x14;
499 pci_read_config_dword(pdev
, serverworks_private
.mm_addr_ofs
, &temp
);
500 if (temp
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
501 pci_read_config_dword(pdev
,
502 serverworks_private
.mm_addr_ofs
+ 4, &temp2
);
504 dev_info(&pdev
->dev
, "64 bit MMIO address, but top "
505 "bits are not zero; disabling AGP\n");
510 bridge
= agp_alloc_bridge();
514 bridge
->driver
= &sworks_driver
;
515 bridge
->dev_private_data
= &serverworks_private
,
516 bridge
->dev
= pci_dev_get(pdev
);
518 pci_set_drvdata(pdev
, bridge
);
519 return agp_add_bridge(bridge
);
522 static void agp_serverworks_remove(struct pci_dev
*pdev
)
524 struct agp_bridge_data
*bridge
= pci_get_drvdata(pdev
);
526 pci_dev_put(bridge
->dev
);
527 agp_remove_bridge(bridge
);
528 agp_put_bridge(bridge
);
529 pci_dev_put(serverworks_private
.svrwrks_dev
);
530 serverworks_private
.svrwrks_dev
= NULL
;
533 static struct pci_device_id agp_serverworks_pci_table
[] = {
535 .class = (PCI_CLASS_BRIDGE_HOST
<< 8),
537 .vendor
= PCI_VENDOR_ID_SERVERWORKS
,
538 .device
= PCI_ANY_ID
,
539 .subvendor
= PCI_ANY_ID
,
540 .subdevice
= PCI_ANY_ID
,
545 MODULE_DEVICE_TABLE(pci
, agp_serverworks_pci_table
);
547 static struct pci_driver agp_serverworks_pci_driver
= {
548 .name
= "agpgart-serverworks",
549 .id_table
= agp_serverworks_pci_table
,
550 .probe
= agp_serverworks_probe
,
551 .remove
= agp_serverworks_remove
,
554 static int __init
agp_serverworks_init(void)
558 return pci_register_driver(&agp_serverworks_pci_driver
);
561 static void __exit
agp_serverworks_cleanup(void)
563 pci_unregister_driver(&agp_serverworks_pci_driver
);
566 module_init(agp_serverworks_init
);
567 module_exit(agp_serverworks_cleanup
);
569 MODULE_LICENSE("GPL and additional rights");