2 * Intel MCH AGPGART routines.
5 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/agp_backend.h>
12 #define AGP_DCACHE_MEMORY 1
13 #define AGP_PHYS_MEMORY 2
15 static struct gatt_mask intel_i810_masks[] =
17 {.mask = I810_PTE_VALID, .type = 0},
18 {.mask = (I810_PTE_VALID | I810_PTE_LOCAL), .type = AGP_DCACHE_MEMORY},
19 {.mask = I810_PTE_VALID, .type = 0}
22 static void intel_i810_tlbflush(struct agp_memory *mem)
27 static void intel_i810_agp_enable(u32 mode)
34 * The i810/i830 requires a physical address to program its mouse
35 * pointer into hardware.
36 * However the Xserver still writes to it through the agp aperture.
38 static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
40 struct agp_memory *new;
46 addr = agp_bridge->driver->agp_alloc_page();
50 new = agp_create_memory(1);
54 new->memory[0] = agp_bridge->driver->mask_memory(virt_to_phys(addr), type);
56 new->num_scratch_pages = 1;
57 new->type = AGP_PHYS_MEMORY;
58 new->physical = new->memory[0];
62 static void intel_i810_free_by_type(struct agp_memory *curr)
64 agp_free_key(curr->key);
65 if(curr->type == AGP_PHYS_MEMORY) {
66 agp_bridge->driver->agp_destroy_page(phys_to_virt(curr->memory[0]));
72 static unsigned long intel_i810_mask_memory(unsigned long addr, int type)
74 /* Type checking must be done elsewhere */
75 return addr | agp_bridge->driver->masks[type].mask;
78 static struct aper_size_info_fixed intel_i830_sizes[] =
81 /* The 64M mode still requires a 128k gatt */
85 static struct _intel_i830_private {
86 struct pci_dev *i830_dev; /* device one */
87 volatile u8 *registers;
91 static void intel_i830_init_gtt_entries(void)
97 static const int ddt[4] = { 0, 16, 32, 64 };
99 pci_read_config_word(agp_bridge->dev,I830_GMCH_CTRL,&gmch_ctrl);
101 if (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
102 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
103 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
104 case I830_GMCH_GMS_STOLEN_512:
105 gtt_entries = KB(512) - KB(132);
107 case I830_GMCH_GMS_STOLEN_1024:
108 gtt_entries = MB(1) - KB(132);
110 case I830_GMCH_GMS_STOLEN_8192:
111 gtt_entries = MB(8) - KB(132);
113 case I830_GMCH_GMS_LOCAL:
114 rdct = INREG8(intel_i830_private.registers,
115 I830_RDRAM_CHANNEL_TYPE);
116 gtt_entries = (I830_RDRAM_ND(rdct) + 1) *
117 MB(ddt[I830_RDRAM_DDT(rdct)]);
125 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
126 case I855_GMCH_GMS_STOLEN_1M:
127 gtt_entries = MB(1) - KB(132);
129 case I855_GMCH_GMS_STOLEN_4M:
130 gtt_entries = MB(4) - KB(132);
132 case I855_GMCH_GMS_STOLEN_8M:
133 gtt_entries = MB(8) - KB(132);
135 case I855_GMCH_GMS_STOLEN_16M:
136 gtt_entries = MB(16) - KB(132);
138 case I855_GMCH_GMS_STOLEN_32M:
139 gtt_entries = MB(32) - KB(132);
147 printk(KERN_INFO PFX "Detected %dK %s memory.\n",
148 gtt_entries / KB(1), local ? "local" : "stolen");
151 "No pre-allocated video memory detected.\n");
152 gtt_entries /= KB(4);
154 intel_i830_private.gtt_entries = gtt_entries;
157 /* The intel i830 automatically initializes the agp aperture during POST.
158 * Use the memory already set aside for in the GTT.
160 static int intel_i830_create_gatt_table(void)
163 struct aper_size_info_fixed *size;
167 size = agp_bridge->current_size;
168 page_order = size->page_order;
169 num_entries = size->num_entries;
170 agp_bridge->gatt_table_real = NULL;
172 pci_read_config_dword(intel_i830_private.i830_dev,I810_MMADDR,&temp);
175 intel_i830_private.registers = (volatile u8 *) ioremap(temp,128 * 4096);
176 if (!intel_i830_private.registers)
179 temp = INREG32(intel_i830_private.registers,I810_PGETBL_CTL) & 0xfffff000;
180 global_cache_flush();
182 /* we have to call this as early as possible after the MMIO base address is known */
183 intel_i830_init_gtt_entries();
185 agp_bridge->gatt_table = NULL;
187 agp_bridge->gatt_bus_addr = temp;
192 /* Return the gatt table to a sane state. Use the top of stolen
193 * memory for the GTT.
195 static int intel_i830_free_gatt_table(void)
200 static int intel_i830_fetch_size(void)
203 struct aper_size_info_fixed *values;
205 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes);
207 if (agp_bridge->dev->device != PCI_DEVICE_ID_INTEL_82830_HB &&
208 agp_bridge->dev->device != PCI_DEVICE_ID_INTEL_82845G_HB) {
209 /* 855GM/852GM/865G has 128MB aperture size */
210 agp_bridge->previous_size = agp_bridge->current_size = (void *) values;
211 agp_bridge->aperture_size_idx = 0;
212 return(values[0].size);
215 pci_read_config_word(agp_bridge->dev,I830_GMCH_CTRL,&gmch_ctrl);
217 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_128M) {
218 agp_bridge->previous_size = agp_bridge->current_size = (void *) values;
219 agp_bridge->aperture_size_idx = 0;
220 return(values[0].size);
222 agp_bridge->previous_size = agp_bridge->current_size = (void *) values;
223 agp_bridge->aperture_size_idx = 1;
224 return(values[1].size);
230 static int intel_i830_configure(void)
232 struct aper_size_info_fixed *current_size;
237 current_size = A_SIZE_FIX(agp_bridge->current_size);
239 pci_read_config_dword(intel_i830_private.i830_dev,I810_GMADDR,&temp);
240 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
242 pci_read_config_word(agp_bridge->dev,I830_GMCH_CTRL,&gmch_ctrl);
243 gmch_ctrl |= I830_GMCH_ENABLED;
244 pci_write_config_word(agp_bridge->dev,I830_GMCH_CTRL,gmch_ctrl);
246 OUTREG32(intel_i830_private.registers,I810_PGETBL_CTL,agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED);
247 global_cache_flush();
249 if (agp_bridge->driver->needs_scratch_page)
250 for (i = intel_i830_private.gtt_entries; i < current_size->num_entries; i++)
251 OUTREG32(intel_i830_private.registers,I810_PTE_BASE + (i * 4),agp_bridge->scratch_page);
256 static void intel_i830_cleanup(void)
258 iounmap((void *) intel_i830_private.registers);
261 static int intel_i830_insert_entries(struct agp_memory *mem,off_t pg_start,
267 temp = agp_bridge->current_size;
268 num_entries = A_SIZE_FIX(temp)->num_entries;
270 if (pg_start < intel_i830_private.gtt_entries) {
271 printk (KERN_DEBUG PFX "pg_start == 0x%.8lx,intel_i830_private.gtt_entries == 0x%.8x\n",
272 pg_start,intel_i830_private.gtt_entries);
274 printk (KERN_INFO PFX "Trying to insert into local/stolen memory\n");
278 if ((pg_start + mem->page_count) > num_entries)
281 /* The i830 can't check the GTT for entries since its read only,
282 * depend on the caller to make the correct offset decisions.
285 if ((type != 0 && type != AGP_PHYS_MEMORY) ||
286 (mem->type != 0 && mem->type != AGP_PHYS_MEMORY))
289 global_cache_flush();
291 for (i = 0, j = pg_start; i < mem->page_count; i++, j++)
292 OUTREG32(intel_i830_private.registers,I810_PTE_BASE + (j * 4),
293 agp_bridge->driver->mask_memory(mem->memory[i], mem->type));
295 global_cache_flush();
297 agp_bridge->driver->tlb_flush(mem);
302 static int intel_i830_remove_entries(struct agp_memory *mem,off_t pg_start,
307 global_cache_flush();
309 if (pg_start < intel_i830_private.gtt_entries) {
310 printk (KERN_INFO PFX "Trying to disable local/stolen memory\n");
314 for (i = pg_start; i < (mem->page_count + pg_start); i++)
315 OUTREG32(intel_i830_private.registers,I810_PTE_BASE + (i * 4),agp_bridge->scratch_page);
317 global_cache_flush();
319 agp_bridge->driver->tlb_flush(mem);
324 static struct agp_memory *intel_i830_alloc_by_type(size_t pg_count,int type)
326 if (type == AGP_PHYS_MEMORY)
327 return(alloc_agpphysmem_i8xx(pg_count, type));
329 /* always return NULL for other allocation types for now */
333 static int intel_8xx_fetch_size(void)
337 struct aper_size_info_8 *values;
339 pci_read_config_byte(agp_bridge->dev, INTEL_APSIZE, &temp);
341 values = A_SIZE_8(agp_bridge->driver->aperture_sizes);
343 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
344 if (temp == values[i].size_value) {
345 agp_bridge->previous_size =
346 agp_bridge->current_size = (void *) (values + i);
347 agp_bridge->aperture_size_idx = i;
348 return values[i].size;
354 static void intel_8xx_tlbflush(struct agp_memory *mem)
357 pci_read_config_dword(agp_bridge->dev, INTEL_AGPCTRL, &temp);
358 pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, temp & ~(1 << 7));
359 pci_read_config_dword(agp_bridge->dev, INTEL_AGPCTRL, &temp);
360 pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, temp | (1 << 7));
363 static void intel_8xx_cleanup(void)
366 struct aper_size_info_8 *previous_size;
368 previous_size = A_SIZE_8(agp_bridge->previous_size);
369 pci_read_config_word(agp_bridge->dev, INTEL_NBXCFG, &temp);
370 pci_write_config_word(agp_bridge->dev, INTEL_NBXCFG, temp & ~(1 << 9));
371 pci_write_config_byte(agp_bridge->dev, INTEL_APSIZE, previous_size->size_value);
374 static int intel_845_configure(void)
378 struct aper_size_info_8 *current_size;
380 current_size = A_SIZE_8(agp_bridge->current_size);
383 pci_write_config_byte(agp_bridge->dev, INTEL_APSIZE, current_size->size_value);
385 /* address to map to */
386 pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
387 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
389 /* attbase - aperture base */
390 pci_write_config_dword(agp_bridge->dev, INTEL_ATTBASE, agp_bridge->gatt_bus_addr);
393 pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, 0x0000);
396 pci_read_config_byte(agp_bridge->dev, INTEL_I845_AGPM, &temp2);
397 pci_write_config_byte(agp_bridge->dev, INTEL_I845_AGPM, temp2 | (1 << 1));
398 /* clear any possible error conditions */
399 pci_write_config_word(agp_bridge->dev, INTEL_I845_ERRSTS, 0x001c);
405 static struct gatt_mask intel_generic_masks[] =
407 {.mask = 0x00000017, .type = 0}
410 static struct aper_size_info_8 intel_8xx_sizes[7] =
421 static struct agp_bridge_driver intel_830_driver = {
422 .owner = THIS_MODULE,
423 .aperture_sizes = intel_i830_sizes,
424 .size_type = FIXED_APER_SIZE,
425 .num_aperture_sizes = 2,
426 .needs_scratch_page = TRUE,
427 .configure = intel_i830_configure,
428 .fetch_size = intel_i830_fetch_size,
429 .cleanup = intel_i830_cleanup,
430 .tlb_flush = intel_i810_tlbflush,
431 .mask_memory = intel_i810_mask_memory,
432 .masks = intel_i810_masks,
433 .agp_enable = intel_i810_agp_enable,
434 .cache_flush = global_cache_flush,
435 .create_gatt_table = intel_i830_create_gatt_table,
436 .free_gatt_table = intel_i830_free_gatt_table,
437 .insert_memory = intel_i830_insert_entries,
438 .remove_memory = intel_i830_remove_entries,
439 .alloc_by_type = intel_i830_alloc_by_type,
440 .free_by_type = intel_i810_free_by_type,
441 .agp_alloc_page = agp_generic_alloc_page,
442 .agp_destroy_page = agp_generic_destroy_page,
445 static struct agp_bridge_driver intel_845_driver = {
446 .owner = THIS_MODULE,
447 .aperture_sizes = intel_8xx_sizes,
448 .size_type = U8_APER_SIZE,
449 .num_aperture_sizes = 7,
450 .configure = intel_845_configure,
451 .fetch_size = intel_8xx_fetch_size,
452 .cleanup = intel_8xx_cleanup,
453 .tlb_flush = intel_8xx_tlbflush,
454 .mask_memory = agp_generic_mask_memory,
455 .masks = intel_generic_masks,
456 .agp_enable = agp_generic_enable,
457 .cache_flush = global_cache_flush,
458 .create_gatt_table = agp_generic_create_gatt_table,
459 .free_gatt_table = agp_generic_free_gatt_table,
460 .insert_memory = agp_generic_insert_memory,
461 .remove_memory = agp_generic_remove_memory,
462 .alloc_by_type = agp_generic_alloc_by_type,
463 .free_by_type = agp_generic_free_by_type,
464 .agp_alloc_page = agp_generic_alloc_page,
465 .agp_destroy_page = agp_generic_destroy_page,
469 static int find_i830(u16 device)
471 struct pci_dev *i830_dev;
473 i830_dev = pci_find_device(PCI_VENDOR_ID_INTEL, device, NULL);
474 if (i830_dev && PCI_FUNC(i830_dev->devfn) != 0) {
475 i830_dev = pci_find_device(PCI_VENDOR_ID_INTEL,
482 intel_i830_private.i830_dev = i830_dev;
486 static int __devinit agp_intelmch_probe(struct pci_dev *pdev,
487 const struct pci_device_id *ent)
489 struct agp_bridge_data *bridge;
491 char *name = "(unknown)";
494 cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
498 bridge = agp_alloc_bridge();
502 switch (pdev->device) {
503 case PCI_DEVICE_ID_INTEL_82865_HB:
504 if (find_i830(PCI_DEVICE_ID_INTEL_82865_IG)) {
505 bridge->driver = &intel_830_driver;
507 bridge->driver = &intel_845_driver;
511 case PCI_DEVICE_ID_INTEL_82875_HB:
512 bridge->driver = &intel_845_driver;
517 printk(KERN_ERR PFX "Unsupported Intel chipset (device id: %04x)\n",
523 bridge->capndx = cap_ptr;
525 if (bridge->driver == &intel_830_driver)
526 bridge->dev_private_data = &intel_i830_private;
528 printk(KERN_INFO PFX "Detected an Intel %s Chipset.\n", name);
531 * The following fixes the case where the BIOS has "forgotten" to
532 * provide an address range for the GART.
533 * 20030610 - hamish@zot.org
535 r = &pdev->resource[0];
536 if (!r->start && r->end) {
537 if(pci_assign_resource(pdev, 0)) {
538 printk(KERN_ERR PFX "could not assign resource 0\n");
544 * If the device has not been properly setup, the following will catch
545 * the problem and should stop the system from crashing.
546 * 20030610 - hamish@zot.org
548 if (pci_enable_device(pdev)) {
549 printk(KERN_ERR PFX "Unable to Enable PCI device\n");
553 /* Fill in the mode register */
555 pci_read_config_dword(pdev,
556 bridge->capndx+PCI_AGP_STATUS,
560 pci_set_drvdata(pdev, bridge);
561 return agp_add_bridge(bridge);
564 static void __devexit agp_intelmch_remove(struct pci_dev *pdev)
566 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
568 agp_remove_bridge(bridge);
569 agp_put_bridge(bridge);
572 static int agp_intelmch_resume(struct pci_dev *pdev)
574 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
576 pci_restore_state(pdev, pdev->saved_config_space);
578 if (bridge->driver == &intel_845_driver)
579 intel_845_configure();
584 static struct pci_device_id agp_intelmch_pci_table[] = {
586 .class = (PCI_CLASS_BRIDGE_HOST << 8),
588 .vendor = PCI_VENDOR_ID_INTEL,
589 .device = PCI_DEVICE_ID_INTEL_82865_HB,
590 .subvendor = PCI_ANY_ID,
591 .subdevice = PCI_ANY_ID,
594 .class = (PCI_CLASS_BRIDGE_HOST << 8),
596 .vendor = PCI_VENDOR_ID_INTEL,
597 .device = PCI_DEVICE_ID_INTEL_82875_HB,
598 .subvendor = PCI_ANY_ID,
599 .subdevice = PCI_ANY_ID,
604 MODULE_DEVICE_TABLE(pci, agp_intelmch_pci_table);
606 static struct pci_driver agp_intelmch_pci_driver = {
607 .name = "agpgart-intel-mch",
608 .id_table = agp_intelmch_pci_table,
609 .probe = agp_intelmch_probe,
610 .remove = agp_intelmch_remove,
611 .resume = agp_intelmch_resume,
614 /* intel_agp_init() must not be declared static for explicit
615 early initialization to work (ie i810fb) */
616 int __init agp_intelmch_init(void)
618 static int agp_initialised=0;
620 if (agp_initialised == 1)
624 return pci_module_init(&agp_intelmch_pci_driver);
627 static void __exit agp_intelmch_cleanup(void)
629 pci_unregister_driver(&agp_intelmch_pci_driver);
632 module_init(agp_intelmch_init);
633 module_exit(agp_intelmch_cleanup);
635 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
636 MODULE_LICENSE("GPL and additional rights");