18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * ATi AGPGART routines.
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/types.h>
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/pci.h>
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/string.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/agp_backend.h>
128c2ecf20Sopenharmony_ci#include <asm/agp.h>
138c2ecf20Sopenharmony_ci#include <asm/set_memory.h>
148c2ecf20Sopenharmony_ci#include "agp.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define ATI_GART_MMBASE_BAR	1
178c2ecf20Sopenharmony_ci#define ATI_RS100_APSIZE	0xac
188c2ecf20Sopenharmony_ci#define ATI_RS100_IG_AGPMODE	0xb0
198c2ecf20Sopenharmony_ci#define ATI_RS300_APSIZE	0xf8
208c2ecf20Sopenharmony_ci#define ATI_RS300_IG_AGPMODE	0xfc
218c2ecf20Sopenharmony_ci#define ATI_GART_FEATURE_ID		0x00
228c2ecf20Sopenharmony_ci#define ATI_GART_BASE			0x04
238c2ecf20Sopenharmony_ci#define ATI_GART_CACHE_SZBASE		0x08
248c2ecf20Sopenharmony_ci#define ATI_GART_CACHE_CNTRL		0x0c
258c2ecf20Sopenharmony_ci#define ATI_GART_CACHE_ENTRY_CNTRL	0x10
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const struct aper_size_info_lvl2 ati_generic_sizes[7] =
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	{2048, 524288, 0x0000000c},
318c2ecf20Sopenharmony_ci	{1024, 262144, 0x0000000a},
328c2ecf20Sopenharmony_ci	{512, 131072, 0x00000008},
338c2ecf20Sopenharmony_ci	{256, 65536, 0x00000006},
348c2ecf20Sopenharmony_ci	{128, 32768, 0x00000004},
358c2ecf20Sopenharmony_ci	{64, 16384, 0x00000002},
368c2ecf20Sopenharmony_ci	{32, 8192, 0x00000000}
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic struct gatt_mask ati_generic_masks[] =
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	{ .mask = 1, .type = 0}
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct ati_page_map {
468c2ecf20Sopenharmony_ci	unsigned long *real;
478c2ecf20Sopenharmony_ci	unsigned long __iomem *remapped;
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic struct _ati_generic_private {
518c2ecf20Sopenharmony_ci	volatile u8 __iomem *registers;
528c2ecf20Sopenharmony_ci	struct ati_page_map **gatt_pages;
538c2ecf20Sopenharmony_ci	int num_tables;
548c2ecf20Sopenharmony_ci} ati_generic_private;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int ati_create_page_map(struct ati_page_map *page_map)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	int i, err = 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
618c2ecf20Sopenharmony_ci	if (page_map->real == NULL)
628c2ecf20Sopenharmony_ci		return -ENOMEM;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	set_memory_uc((unsigned long)page_map->real, 1);
658c2ecf20Sopenharmony_ci	err = map_page_into_agp(virt_to_page(page_map->real));
668c2ecf20Sopenharmony_ci	page_map->remapped = page_map->real;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
698c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, page_map->remapped+i);
708c2ecf20Sopenharmony_ci		readl(page_map->remapped+i);	/* PCI Posting. */
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic void ati_free_page_map(struct ati_page_map *page_map)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	unmap_page_from_agp(virt_to_page(page_map->real));
808c2ecf20Sopenharmony_ci	set_memory_wb((unsigned long)page_map->real, 1);
818c2ecf20Sopenharmony_ci	free_page((unsigned long) page_map->real);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void ati_free_gatt_pages(void)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	int i;
888c2ecf20Sopenharmony_ci	struct ati_page_map **tables;
898c2ecf20Sopenharmony_ci	struct ati_page_map *entry;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	tables = ati_generic_private.gatt_pages;
928c2ecf20Sopenharmony_ci	for (i = 0; i < ati_generic_private.num_tables; i++) {
938c2ecf20Sopenharmony_ci		entry = tables[i];
948c2ecf20Sopenharmony_ci		if (entry != NULL) {
958c2ecf20Sopenharmony_ci			if (entry->real != NULL)
968c2ecf20Sopenharmony_ci				ati_free_page_map(entry);
978c2ecf20Sopenharmony_ci			kfree(entry);
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci	kfree(tables);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int ati_create_gatt_pages(int nr_tables)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct ati_page_map **tables;
1078c2ecf20Sopenharmony_ci	struct ati_page_map *entry;
1088c2ecf20Sopenharmony_ci	int retval = 0;
1098c2ecf20Sopenharmony_ci	int i;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	tables = kcalloc(nr_tables + 1, sizeof(struct ati_page_map *),
1128c2ecf20Sopenharmony_ci			 GFP_KERNEL);
1138c2ecf20Sopenharmony_ci	if (tables == NULL)
1148c2ecf20Sopenharmony_ci		return -ENOMEM;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	for (i = 0; i < nr_tables; i++) {
1178c2ecf20Sopenharmony_ci		entry = kzalloc(sizeof(struct ati_page_map), GFP_KERNEL);
1188c2ecf20Sopenharmony_ci		tables[i] = entry;
1198c2ecf20Sopenharmony_ci		if (entry == NULL) {
1208c2ecf20Sopenharmony_ci			retval = -ENOMEM;
1218c2ecf20Sopenharmony_ci			break;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci		retval = ati_create_page_map(entry);
1248c2ecf20Sopenharmony_ci		if (retval != 0)
1258c2ecf20Sopenharmony_ci			break;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	ati_generic_private.num_tables = i;
1288c2ecf20Sopenharmony_ci	ati_generic_private.gatt_pages = tables;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (retval != 0)
1318c2ecf20Sopenharmony_ci		ati_free_gatt_pages();
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return retval;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic int is_r200(void)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	if ((agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS100) ||
1398c2ecf20Sopenharmony_ci	    (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200) ||
1408c2ecf20Sopenharmony_ci	    (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS200_B) ||
1418c2ecf20Sopenharmony_ci	    (agp_bridge->dev->device == PCI_DEVICE_ID_ATI_RS250))
1428c2ecf20Sopenharmony_ci		return 1;
1438c2ecf20Sopenharmony_ci	return 0;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int ati_fetch_size(void)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int i;
1498c2ecf20Sopenharmony_ci	u32 temp;
1508c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *values;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (is_r200())
1538c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
1548c2ecf20Sopenharmony_ci	else
1558c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	temp = (temp & 0x0000000e);
1588c2ecf20Sopenharmony_ci	values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
1598c2ecf20Sopenharmony_ci	for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
1608c2ecf20Sopenharmony_ci		if (temp == values[i].size_value) {
1618c2ecf20Sopenharmony_ci			agp_bridge->previous_size =
1628c2ecf20Sopenharmony_ci			    agp_bridge->current_size = (void *) (values + i);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci			agp_bridge->aperture_size_idx = i;
1658c2ecf20Sopenharmony_ci			return values[i].size;
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void ati_tlbflush(struct agp_memory * mem)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	writel(1, ati_generic_private.registers+ATI_GART_CACHE_CNTRL);
1758c2ecf20Sopenharmony_ci	readl(ati_generic_private.registers+ATI_GART_CACHE_CNTRL);	/* PCI Posting. */
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic void ati_cleanup(void)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *previous_size;
1818c2ecf20Sopenharmony_ci	u32 temp;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Write back the previous size and disable gart translation */
1868c2ecf20Sopenharmony_ci	if (is_r200()) {
1878c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
1888c2ecf20Sopenharmony_ci		temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
1898c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
1908c2ecf20Sopenharmony_ci	} else {
1918c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
1928c2ecf20Sopenharmony_ci		temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
1938c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	iounmap((volatile u8 __iomem *)ati_generic_private.registers);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int ati_configure(void)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	phys_addr_t reg;
2028c2ecf20Sopenharmony_ci	u32 temp;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* Get the memory mapped registers */
2058c2ecf20Sopenharmony_ci	reg = pci_resource_start(agp_bridge->dev, ATI_GART_MMBASE_BAR);
2068c2ecf20Sopenharmony_ci	ati_generic_private.registers = (volatile u8 __iomem *) ioremap(reg, 4096);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (!ati_generic_private.registers)
2098c2ecf20Sopenharmony_ci		return -ENOMEM;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (is_r200())
2128c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS100_IG_AGPMODE, 0x20000);
2138c2ecf20Sopenharmony_ci	else
2148c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS300_IG_AGPMODE, 0x20000);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* address to map to */
2178c2ecf20Sopenharmony_ci	/*
2188c2ecf20Sopenharmony_ci	agp_bridge.gart_bus_addr = pci_bus_address(agp_bridge.dev,
2198c2ecf20Sopenharmony_ci						   AGP_APERTURE_BAR);
2208c2ecf20Sopenharmony_ci	printk(KERN_INFO PFX "IGP320 gart_bus_addr: %x\n", agp_bridge.gart_bus_addr);
2218c2ecf20Sopenharmony_ci	*/
2228c2ecf20Sopenharmony_ci	writel(0x60000, ati_generic_private.registers+ATI_GART_FEATURE_ID);
2238c2ecf20Sopenharmony_ci	readl(ati_generic_private.registers+ATI_GART_FEATURE_ID);	/* PCI Posting.*/
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* SIGNALED_SYSTEM_ERROR @ NB_STATUS */
2268c2ecf20Sopenharmony_ci	pci_read_config_dword(agp_bridge->dev, PCI_COMMAND, &temp);
2278c2ecf20Sopenharmony_ci	pci_write_config_dword(agp_bridge->dev, PCI_COMMAND, temp | (1<<14));
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Write out the address of the gatt table */
2308c2ecf20Sopenharmony_ci	writel(agp_bridge->gatt_bus_addr, ati_generic_private.registers+ATI_GART_BASE);
2318c2ecf20Sopenharmony_ci	readl(ati_generic_private.registers+ATI_GART_BASE);	/* PCI Posting. */
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
2388c2ecf20Sopenharmony_cistatic int agp_ati_suspend(struct pci_dev *dev, pm_message_t state)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	pci_save_state(dev);
2418c2ecf20Sopenharmony_ci	pci_set_power_state(dev, PCI_D3hot);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int agp_ati_resume(struct pci_dev *dev)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	pci_set_power_state(dev, PCI_D0);
2498c2ecf20Sopenharmony_ci	pci_restore_state(dev);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return ati_configure();
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci#endif
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/*
2568c2ecf20Sopenharmony_ci *Since we don't need contiguous memory we just try
2578c2ecf20Sopenharmony_ci * to get the gatt table once
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci#define GET_PAGE_DIR_OFF(addr) (addr >> 22)
2618c2ecf20Sopenharmony_ci#define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
2628c2ecf20Sopenharmony_ci	GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
2638c2ecf20Sopenharmony_ci#define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
2648c2ecf20Sopenharmony_ci#undef  GET_GATT
2658c2ecf20Sopenharmony_ci#define GET_GATT(addr) (ati_generic_private.gatt_pages[\
2668c2ecf20Sopenharmony_ci	GET_PAGE_DIR_IDX(addr)]->remapped)
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic int ati_insert_memory(struct agp_memory * mem,
2698c2ecf20Sopenharmony_ci			     off_t pg_start, int type)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int i, j, num_entries;
2728c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
2738c2ecf20Sopenharmony_ci	unsigned long addr;
2748c2ecf20Sopenharmony_ci	int mask_type;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
2798c2ecf20Sopenharmony_ci	if (mask_type != 0 || type != mem->type)
2808c2ecf20Sopenharmony_ci		return -EINVAL;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (mem->page_count == 0)
2838c2ecf20Sopenharmony_ci		return 0;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if ((pg_start + mem->page_count) > num_entries)
2868c2ecf20Sopenharmony_ci		return -EINVAL;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	j = pg_start;
2898c2ecf20Sopenharmony_ci	while (j < (pg_start + mem->page_count)) {
2908c2ecf20Sopenharmony_ci		addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
2918c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
2928c2ecf20Sopenharmony_ci		if (!PGE_EMPTY(agp_bridge,readl(cur_gatt+GET_GATT_OFF(addr))))
2938c2ecf20Sopenharmony_ci			return -EBUSY;
2948c2ecf20Sopenharmony_ci		j++;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (!mem->is_flushed) {
2988c2ecf20Sopenharmony_ci		/*CACHE_FLUSH(); */
2998c2ecf20Sopenharmony_ci		global_cache_flush();
3008c2ecf20Sopenharmony_ci		mem->is_flushed = true;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
3048c2ecf20Sopenharmony_ci		addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
3058c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
3068c2ecf20Sopenharmony_ci		writel(agp_bridge->driver->mask_memory(agp_bridge,
3078c2ecf20Sopenharmony_ci						       page_to_phys(mem->pages[i]),
3088c2ecf20Sopenharmony_ci						       mem->type),
3098c2ecf20Sopenharmony_ci		       cur_gatt+GET_GATT_OFF(addr));
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci	readl(GET_GATT(agp_bridge->gart_bus_addr)); /* PCI posting */
3128c2ecf20Sopenharmony_ci	agp_bridge->driver->tlb_flush(mem);
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int ati_remove_memory(struct agp_memory * mem, off_t pg_start,
3178c2ecf20Sopenharmony_ci			     int type)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	int i;
3208c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
3218c2ecf20Sopenharmony_ci	unsigned long addr;
3228c2ecf20Sopenharmony_ci	int mask_type;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	mask_type = agp_generic_type_to_mask_type(mem->bridge, type);
3258c2ecf20Sopenharmony_ci	if (mask_type != 0 || type != mem->type)
3268c2ecf20Sopenharmony_ci		return -EINVAL;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	if (mem->page_count == 0)
3298c2ecf20Sopenharmony_ci		return 0;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	for (i = pg_start; i < (mem->page_count + pg_start); i++) {
3328c2ecf20Sopenharmony_ci		addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
3338c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
3348c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	readl(GET_GATT(agp_bridge->gart_bus_addr)); /* PCI posting */
3388c2ecf20Sopenharmony_ci	agp_bridge->driver->tlb_flush(mem);
3398c2ecf20Sopenharmony_ci	return 0;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic int ati_create_gatt_table(struct agp_bridge_data *bridge)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *value;
3458c2ecf20Sopenharmony_ci	struct ati_page_map page_dir;
3468c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
3478c2ecf20Sopenharmony_ci	unsigned long addr;
3488c2ecf20Sopenharmony_ci	int retval;
3498c2ecf20Sopenharmony_ci	u32 temp;
3508c2ecf20Sopenharmony_ci	int i;
3518c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *current_size;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	value = A_SIZE_LVL2(agp_bridge->current_size);
3548c2ecf20Sopenharmony_ci	retval = ati_create_page_map(&page_dir);
3558c2ecf20Sopenharmony_ci	if (retval != 0)
3568c2ecf20Sopenharmony_ci		return retval;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	retval = ati_create_gatt_pages(value->num_entries / 1024);
3598c2ecf20Sopenharmony_ci	if (retval != 0) {
3608c2ecf20Sopenharmony_ci		ati_free_page_map(&page_dir);
3618c2ecf20Sopenharmony_ci		return retval;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	agp_bridge->gatt_table_real = (u32 *)page_dir.real;
3658c2ecf20Sopenharmony_ci	agp_bridge->gatt_table = (u32 __iomem *) page_dir.remapped;
3668c2ecf20Sopenharmony_ci	agp_bridge->gatt_bus_addr = virt_to_phys(page_dir.real);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* Write out the size register */
3698c2ecf20Sopenharmony_ci	current_size = A_SIZE_LVL2(agp_bridge->current_size);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (is_r200()) {
3728c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
3738c2ecf20Sopenharmony_ci		temp = (((temp & ~(0x0000000e)) | current_size->size_value)
3748c2ecf20Sopenharmony_ci			| 0x00000001);
3758c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, temp);
3768c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS100_APSIZE, &temp);
3778c2ecf20Sopenharmony_ci	} else {
3788c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
3798c2ecf20Sopenharmony_ci		temp = (((temp & ~(0x0000000e)) | current_size->size_value)
3808c2ecf20Sopenharmony_ci			| 0x00000001);
3818c2ecf20Sopenharmony_ci		pci_write_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, temp);
3828c2ecf20Sopenharmony_ci		pci_read_config_dword(agp_bridge->dev, ATI_RS300_APSIZE, &temp);
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/*
3868c2ecf20Sopenharmony_ci	 * Get the address for the gart region.
3878c2ecf20Sopenharmony_ci	 * This is a bus address even on the alpha, b/c its
3888c2ecf20Sopenharmony_ci	 * used to program the agp master not the cpu
3898c2ecf20Sopenharmony_ci	 */
3908c2ecf20Sopenharmony_ci	addr = pci_bus_address(agp_bridge->dev, AGP_APERTURE_BAR);
3918c2ecf20Sopenharmony_ci	agp_bridge->gart_bus_addr = addr;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	/* Calculate the agp offset */
3948c2ecf20Sopenharmony_ci	for (i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
3958c2ecf20Sopenharmony_ci		writel(virt_to_phys(ati_generic_private.gatt_pages[i]->real) | 1,
3968c2ecf20Sopenharmony_ci			page_dir.remapped+GET_PAGE_DIR_OFF(addr));
3978c2ecf20Sopenharmony_ci		readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr));	/* PCI Posting. */
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	for (i = 0; i < value->num_entries; i++) {
4018c2ecf20Sopenharmony_ci		addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
4028c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
4038c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	return 0;
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic int ati_free_gatt_table(struct agp_bridge_data *bridge)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct ati_page_map page_dir;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
4148c2ecf20Sopenharmony_ci	page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	ati_free_gatt_pages();
4178c2ecf20Sopenharmony_ci	ati_free_page_map(&page_dir);
4188c2ecf20Sopenharmony_ci	return 0;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic const struct agp_bridge_driver ati_generic_bridge = {
4228c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
4238c2ecf20Sopenharmony_ci	.aperture_sizes		= ati_generic_sizes,
4248c2ecf20Sopenharmony_ci	.size_type		= LVL2_APER_SIZE,
4258c2ecf20Sopenharmony_ci	.num_aperture_sizes	= 7,
4268c2ecf20Sopenharmony_ci	.needs_scratch_page	= true,
4278c2ecf20Sopenharmony_ci	.configure		= ati_configure,
4288c2ecf20Sopenharmony_ci	.fetch_size		= ati_fetch_size,
4298c2ecf20Sopenharmony_ci	.cleanup		= ati_cleanup,
4308c2ecf20Sopenharmony_ci	.tlb_flush		= ati_tlbflush,
4318c2ecf20Sopenharmony_ci	.mask_memory		= agp_generic_mask_memory,
4328c2ecf20Sopenharmony_ci	.masks			= ati_generic_masks,
4338c2ecf20Sopenharmony_ci	.agp_enable		= agp_generic_enable,
4348c2ecf20Sopenharmony_ci	.cache_flush		= global_cache_flush,
4358c2ecf20Sopenharmony_ci	.create_gatt_table	= ati_create_gatt_table,
4368c2ecf20Sopenharmony_ci	.free_gatt_table	= ati_free_gatt_table,
4378c2ecf20Sopenharmony_ci	.insert_memory		= ati_insert_memory,
4388c2ecf20Sopenharmony_ci	.remove_memory		= ati_remove_memory,
4398c2ecf20Sopenharmony_ci	.alloc_by_type		= agp_generic_alloc_by_type,
4408c2ecf20Sopenharmony_ci	.free_by_type		= agp_generic_free_by_type,
4418c2ecf20Sopenharmony_ci	.agp_alloc_page		= agp_generic_alloc_page,
4428c2ecf20Sopenharmony_ci	.agp_alloc_pages	= agp_generic_alloc_pages,
4438c2ecf20Sopenharmony_ci	.agp_destroy_page	= agp_generic_destroy_page,
4448c2ecf20Sopenharmony_ci	.agp_destroy_pages	= agp_generic_destroy_pages,
4458c2ecf20Sopenharmony_ci	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
4468c2ecf20Sopenharmony_ci};
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic struct agp_device_ids ati_agp_device_ids[] =
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	{
4528c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS100,
4538c2ecf20Sopenharmony_ci		.chipset_name	= "IGP320/M",
4548c2ecf20Sopenharmony_ci	},
4558c2ecf20Sopenharmony_ci	{
4568c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS200,
4578c2ecf20Sopenharmony_ci		.chipset_name	= "IGP330/340/345/350/M",
4588c2ecf20Sopenharmony_ci	},
4598c2ecf20Sopenharmony_ci	{
4608c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS200_B,
4618c2ecf20Sopenharmony_ci		.chipset_name	= "IGP345M",
4628c2ecf20Sopenharmony_ci	},
4638c2ecf20Sopenharmony_ci	{
4648c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS250,
4658c2ecf20Sopenharmony_ci		.chipset_name	= "IGP7000/M",
4668c2ecf20Sopenharmony_ci	},
4678c2ecf20Sopenharmony_ci	{
4688c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS300_100,
4698c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9100/M",
4708c2ecf20Sopenharmony_ci	},
4718c2ecf20Sopenharmony_ci	{
4728c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS300_133,
4738c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9100/M",
4748c2ecf20Sopenharmony_ci	},
4758c2ecf20Sopenharmony_ci	{
4768c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS300_166,
4778c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9100/M",
4788c2ecf20Sopenharmony_ci	},
4798c2ecf20Sopenharmony_ci	{
4808c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS300_200,
4818c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9100/M",
4828c2ecf20Sopenharmony_ci	},
4838c2ecf20Sopenharmony_ci	{
4848c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS350_133,
4858c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9000/M",
4868c2ecf20Sopenharmony_ci	},
4878c2ecf20Sopenharmony_ci	{
4888c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_ATI_RS350_200,
4898c2ecf20Sopenharmony_ci		.chipset_name	= "IGP9100/M",
4908c2ecf20Sopenharmony_ci	},
4918c2ecf20Sopenharmony_ci	{ }, /* dummy final entry, always present */
4928c2ecf20Sopenharmony_ci};
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic int agp_ati_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct agp_device_ids *devs = ati_agp_device_ids;
4978c2ecf20Sopenharmony_ci	struct agp_bridge_data *bridge;
4988c2ecf20Sopenharmony_ci	u8 cap_ptr;
4998c2ecf20Sopenharmony_ci	int j;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
5028c2ecf20Sopenharmony_ci	if (!cap_ptr)
5038c2ecf20Sopenharmony_ci		return -ENODEV;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	/* probe for known chipsets */
5068c2ecf20Sopenharmony_ci	for (j = 0; devs[j].chipset_name; j++) {
5078c2ecf20Sopenharmony_ci		if (pdev->device == devs[j].device_id)
5088c2ecf20Sopenharmony_ci			goto found;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "unsupported Ati chipset [%04x/%04x])\n",
5128c2ecf20Sopenharmony_ci		pdev->vendor, pdev->device);
5138c2ecf20Sopenharmony_ci	return -ENODEV;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cifound:
5168c2ecf20Sopenharmony_ci	bridge = agp_alloc_bridge();
5178c2ecf20Sopenharmony_ci	if (!bridge)
5188c2ecf20Sopenharmony_ci		return -ENOMEM;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	bridge->dev = pdev;
5218c2ecf20Sopenharmony_ci	bridge->capndx = cap_ptr;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	bridge->driver = &ati_generic_bridge;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "Ati %s chipset\n", devs[j].chipset_name);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* Fill in the mode register */
5288c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev,
5298c2ecf20Sopenharmony_ci			bridge->capndx+PCI_AGP_STATUS,
5308c2ecf20Sopenharmony_ci			&bridge->mode);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, bridge);
5338c2ecf20Sopenharmony_ci	return agp_add_bridge(bridge);
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic void agp_ati_remove(struct pci_dev *pdev)
5378c2ecf20Sopenharmony_ci{
5388c2ecf20Sopenharmony_ci	struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	agp_remove_bridge(bridge);
5418c2ecf20Sopenharmony_ci	agp_put_bridge(bridge);
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic const struct pci_device_id agp_ati_pci_table[] = {
5458c2ecf20Sopenharmony_ci	{
5468c2ecf20Sopenharmony_ci	.class		= (PCI_CLASS_BRIDGE_HOST << 8),
5478c2ecf20Sopenharmony_ci	.class_mask	= ~0,
5488c2ecf20Sopenharmony_ci	.vendor		= PCI_VENDOR_ID_ATI,
5498c2ecf20Sopenharmony_ci	.device		= PCI_ANY_ID,
5508c2ecf20Sopenharmony_ci	.subvendor	= PCI_ANY_ID,
5518c2ecf20Sopenharmony_ci	.subdevice	= PCI_ANY_ID,
5528c2ecf20Sopenharmony_ci	},
5538c2ecf20Sopenharmony_ci	{ }
5548c2ecf20Sopenharmony_ci};
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, agp_ati_pci_table);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic struct pci_driver agp_ati_pci_driver = {
5598c2ecf20Sopenharmony_ci	.name		= "agpgart-ati",
5608c2ecf20Sopenharmony_ci	.id_table	= agp_ati_pci_table,
5618c2ecf20Sopenharmony_ci	.probe		= agp_ati_probe,
5628c2ecf20Sopenharmony_ci	.remove		= agp_ati_remove,
5638c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
5648c2ecf20Sopenharmony_ci	.suspend	= agp_ati_suspend,
5658c2ecf20Sopenharmony_ci	.resume		= agp_ati_resume,
5668c2ecf20Sopenharmony_ci#endif
5678c2ecf20Sopenharmony_ci};
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic int __init agp_ati_init(void)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	if (agp_off)
5728c2ecf20Sopenharmony_ci		return -EINVAL;
5738c2ecf20Sopenharmony_ci	return pci_register_driver(&agp_ati_pci_driver);
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic void __exit agp_ati_cleanup(void)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	pci_unregister_driver(&agp_ati_pci_driver);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cimodule_init(agp_ati_init);
5828c2ecf20Sopenharmony_cimodule_exit(agp_ati_cleanup);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dave Jones");
5858c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL and additional rights");
5868c2ecf20Sopenharmony_ci
587