18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * AMD K7 AGPGART routines.
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/pci.h>
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/agp_backend.h>
98c2ecf20Sopenharmony_ci#include <linux/page-flags.h>
108c2ecf20Sopenharmony_ci#include <linux/mm.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <asm/set_memory.h>
138c2ecf20Sopenharmony_ci#include "agp.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define AMD_MMBASE_BAR	1
168c2ecf20Sopenharmony_ci#define AMD_APSIZE	0xac
178c2ecf20Sopenharmony_ci#define AMD_MODECNTL	0xb0
188c2ecf20Sopenharmony_ci#define AMD_MODECNTL2	0xb2
198c2ecf20Sopenharmony_ci#define AMD_GARTENABLE	0x02	/* In mmio region (16-bit register) */
208c2ecf20Sopenharmony_ci#define AMD_ATTBASE	0x04	/* In mmio region (32-bit register) */
218c2ecf20Sopenharmony_ci#define AMD_TLBFLUSH	0x0c	/* In mmio region (32-bit register) */
228c2ecf20Sopenharmony_ci#define AMD_CACHEENTRY	0x10	/* In mmio region (32-bit register) */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic const struct pci_device_id agp_amdk7_pci_table[];
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct amd_page_map {
278c2ecf20Sopenharmony_ci	unsigned long *real;
288c2ecf20Sopenharmony_ci	unsigned long __iomem *remapped;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic struct _amd_irongate_private {
328c2ecf20Sopenharmony_ci	volatile u8 __iomem *registers;
338c2ecf20Sopenharmony_ci	struct amd_page_map **gatt_pages;
348c2ecf20Sopenharmony_ci	int num_tables;
358c2ecf20Sopenharmony_ci} amd_irongate_private;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int amd_create_page_map(struct amd_page_map *page_map)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	int i;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
428c2ecf20Sopenharmony_ci	if (page_map->real == NULL)
438c2ecf20Sopenharmony_ci		return -ENOMEM;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	set_memory_uc((unsigned long)page_map->real, 1);
468c2ecf20Sopenharmony_ci	page_map->remapped = page_map->real;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
498c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, page_map->remapped+i);
508c2ecf20Sopenharmony_ci		readl(page_map->remapped+i);	/* PCI Posting. */
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void amd_free_page_map(struct amd_page_map *page_map)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	set_memory_wb((unsigned long)page_map->real, 1);
598c2ecf20Sopenharmony_ci	free_page((unsigned long) page_map->real);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void amd_free_gatt_pages(void)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	int i;
658c2ecf20Sopenharmony_ci	struct amd_page_map **tables;
668c2ecf20Sopenharmony_ci	struct amd_page_map *entry;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	tables = amd_irongate_private.gatt_pages;
698c2ecf20Sopenharmony_ci	for (i = 0; i < amd_irongate_private.num_tables; i++) {
708c2ecf20Sopenharmony_ci		entry = tables[i];
718c2ecf20Sopenharmony_ci		if (entry != NULL) {
728c2ecf20Sopenharmony_ci			if (entry->real != NULL)
738c2ecf20Sopenharmony_ci				amd_free_page_map(entry);
748c2ecf20Sopenharmony_ci			kfree(entry);
758c2ecf20Sopenharmony_ci		}
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci	kfree(tables);
788c2ecf20Sopenharmony_ci	amd_irongate_private.gatt_pages = NULL;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic int amd_create_gatt_pages(int nr_tables)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct amd_page_map **tables;
848c2ecf20Sopenharmony_ci	struct amd_page_map *entry;
858c2ecf20Sopenharmony_ci	int retval = 0;
868c2ecf20Sopenharmony_ci	int i;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	tables = kcalloc(nr_tables + 1, sizeof(struct amd_page_map *),
898c2ecf20Sopenharmony_ci			 GFP_KERNEL);
908c2ecf20Sopenharmony_ci	if (tables == NULL)
918c2ecf20Sopenharmony_ci		return -ENOMEM;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	for (i = 0; i < nr_tables; i++) {
948c2ecf20Sopenharmony_ci		entry = kzalloc(sizeof(struct amd_page_map), GFP_KERNEL);
958c2ecf20Sopenharmony_ci		tables[i] = entry;
968c2ecf20Sopenharmony_ci		if (entry == NULL) {
978c2ecf20Sopenharmony_ci			retval = -ENOMEM;
988c2ecf20Sopenharmony_ci			break;
998c2ecf20Sopenharmony_ci		}
1008c2ecf20Sopenharmony_ci		retval = amd_create_page_map(entry);
1018c2ecf20Sopenharmony_ci		if (retval != 0)
1028c2ecf20Sopenharmony_ci			break;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci	amd_irongate_private.num_tables = i;
1058c2ecf20Sopenharmony_ci	amd_irongate_private.gatt_pages = tables;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (retval != 0)
1088c2ecf20Sopenharmony_ci		amd_free_gatt_pages();
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return retval;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* Since we don't need contiguous memory we just try
1148c2ecf20Sopenharmony_ci * to get the gatt table once
1158c2ecf20Sopenharmony_ci */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define GET_PAGE_DIR_OFF(addr) (addr >> 22)
1188c2ecf20Sopenharmony_ci#define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
1198c2ecf20Sopenharmony_ci	GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
1208c2ecf20Sopenharmony_ci#define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
1218c2ecf20Sopenharmony_ci#define GET_GATT(addr) (amd_irongate_private.gatt_pages[\
1228c2ecf20Sopenharmony_ci	GET_PAGE_DIR_IDX(addr)]->remapped)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic int amd_create_gatt_table(struct agp_bridge_data *bridge)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *value;
1278c2ecf20Sopenharmony_ci	struct amd_page_map page_dir;
1288c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
1298c2ecf20Sopenharmony_ci	unsigned long addr;
1308c2ecf20Sopenharmony_ci	int retval;
1318c2ecf20Sopenharmony_ci	int i;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	value = A_SIZE_LVL2(agp_bridge->current_size);
1348c2ecf20Sopenharmony_ci	retval = amd_create_page_map(&page_dir);
1358c2ecf20Sopenharmony_ci	if (retval != 0)
1368c2ecf20Sopenharmony_ci		return retval;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	retval = amd_create_gatt_pages(value->num_entries / 1024);
1398c2ecf20Sopenharmony_ci	if (retval != 0) {
1408c2ecf20Sopenharmony_ci		amd_free_page_map(&page_dir);
1418c2ecf20Sopenharmony_ci		return retval;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	agp_bridge->gatt_table_real = (u32 *)page_dir.real;
1458c2ecf20Sopenharmony_ci	agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
1468c2ecf20Sopenharmony_ci	agp_bridge->gatt_bus_addr = virt_to_phys(page_dir.real);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* Get the address for the gart region.
1498c2ecf20Sopenharmony_ci	 * This is a bus address even on the alpha, b/c its
1508c2ecf20Sopenharmony_ci	 * used to program the agp master not the cpu
1518c2ecf20Sopenharmony_ci	 */
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	addr = pci_bus_address(agp_bridge->dev, AGP_APERTURE_BAR);
1548c2ecf20Sopenharmony_ci	agp_bridge->gart_bus_addr = addr;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Calculate the agp offset */
1578c2ecf20Sopenharmony_ci	for (i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
1588c2ecf20Sopenharmony_ci		writel(virt_to_phys(amd_irongate_private.gatt_pages[i]->real) | 1,
1598c2ecf20Sopenharmony_ci			page_dir.remapped+GET_PAGE_DIR_OFF(addr));
1608c2ecf20Sopenharmony_ci		readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr));	/* PCI Posting. */
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	for (i = 0; i < value->num_entries; i++) {
1648c2ecf20Sopenharmony_ci		addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
1658c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
1668c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
1678c2ecf20Sopenharmony_ci		readl(cur_gatt+GET_GATT_OFF(addr));	/* PCI Posting. */
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int amd_free_gatt_table(struct agp_bridge_data *bridge)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct amd_page_map page_dir;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
1788c2ecf20Sopenharmony_ci	page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	amd_free_gatt_pages();
1818c2ecf20Sopenharmony_ci	amd_free_page_map(&page_dir);
1828c2ecf20Sopenharmony_ci	return 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int amd_irongate_fetch_size(void)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	int i;
1888c2ecf20Sopenharmony_ci	u32 temp;
1898c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *values;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
1928c2ecf20Sopenharmony_ci	temp = (temp & 0x0000000e);
1938c2ecf20Sopenharmony_ci	values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
1948c2ecf20Sopenharmony_ci	for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
1958c2ecf20Sopenharmony_ci		if (temp == values[i].size_value) {
1968c2ecf20Sopenharmony_ci			agp_bridge->previous_size =
1978c2ecf20Sopenharmony_ci			    agp_bridge->current_size = (void *) (values + i);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci			agp_bridge->aperture_size_idx = i;
2008c2ecf20Sopenharmony_ci			return values[i].size;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	return 0;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic int amd_irongate_configure(void)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *current_size;
2108c2ecf20Sopenharmony_ci	phys_addr_t reg;
2118c2ecf20Sopenharmony_ci	u32 temp;
2128c2ecf20Sopenharmony_ci	u16 enable_reg;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	current_size = A_SIZE_LVL2(agp_bridge->current_size);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (!amd_irongate_private.registers) {
2178c2ecf20Sopenharmony_ci		/* Get the memory mapped registers */
2188c2ecf20Sopenharmony_ci		reg = pci_resource_start(agp_bridge->dev, AMD_MMBASE_BAR);
2198c2ecf20Sopenharmony_ci		amd_irongate_private.registers = (volatile u8 __iomem *) ioremap(reg, 4096);
2208c2ecf20Sopenharmony_ci		if (!amd_irongate_private.registers)
2218c2ecf20Sopenharmony_ci			return -ENOMEM;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* Write out the address of the gatt table */
2258c2ecf20Sopenharmony_ci	writel(agp_bridge->gatt_bus_addr, amd_irongate_private.registers+AMD_ATTBASE);
2268c2ecf20Sopenharmony_ci	readl(amd_irongate_private.registers+AMD_ATTBASE);	/* PCI Posting. */
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Write the Sync register */
2298c2ecf20Sopenharmony_ci	pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL, 0x80);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Set indexing mode */
2328c2ecf20Sopenharmony_ci	pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL2, 0x00);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	/* Write the enable register */
2358c2ecf20Sopenharmony_ci	enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
2368c2ecf20Sopenharmony_ci	enable_reg = (enable_reg | 0x0004);
2378c2ecf20Sopenharmony_ci	writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
2388c2ecf20Sopenharmony_ci	readw(amd_irongate_private.registers+AMD_GARTENABLE);	/* PCI Posting. */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* Write out the size register */
2418c2ecf20Sopenharmony_ci	pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
2428c2ecf20Sopenharmony_ci	temp = (((temp & ~(0x0000000e)) | current_size->size_value) | 1);
2438c2ecf20Sopenharmony_ci	pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* Flush the tlb */
2468c2ecf20Sopenharmony_ci	writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
2478c2ecf20Sopenharmony_ci	readl(amd_irongate_private.registers+AMD_TLBFLUSH);	/* PCI Posting.*/
2488c2ecf20Sopenharmony_ci	return 0;
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic void amd_irongate_cleanup(void)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct aper_size_info_lvl2 *previous_size;
2548c2ecf20Sopenharmony_ci	u32 temp;
2558c2ecf20Sopenharmony_ci	u16 enable_reg;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
2608c2ecf20Sopenharmony_ci	enable_reg = (enable_reg & ~(0x0004));
2618c2ecf20Sopenharmony_ci	writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
2628c2ecf20Sopenharmony_ci	readw(amd_irongate_private.registers+AMD_GARTENABLE);	/* PCI Posting. */
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/* Write back the previous size and disable gart translation */
2658c2ecf20Sopenharmony_ci	pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
2668c2ecf20Sopenharmony_ci	temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
2678c2ecf20Sopenharmony_ci	pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
2688c2ecf20Sopenharmony_ci	iounmap((void __iomem *) amd_irongate_private.registers);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci/*
2728c2ecf20Sopenharmony_ci * This routine could be implemented by taking the addresses
2738c2ecf20Sopenharmony_ci * written to the GATT, and flushing them individually.  However
2748c2ecf20Sopenharmony_ci * currently it just flushes the whole table.  Which is probably
2758c2ecf20Sopenharmony_ci * more efficient, since agp_memory blocks can be a large number of
2768c2ecf20Sopenharmony_ci * entries.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic void amd_irongate_tlbflush(struct agp_memory *temp)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
2828c2ecf20Sopenharmony_ci	readl(amd_irongate_private.registers+AMD_TLBFLUSH);	/* PCI Posting. */
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	int i, j, num_entries;
2888c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
2898c2ecf20Sopenharmony_ci	unsigned long addr;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (type != mem->type ||
2948c2ecf20Sopenharmony_ci	    agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type))
2958c2ecf20Sopenharmony_ci		return -EINVAL;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if ((pg_start + mem->page_count) > num_entries)
2988c2ecf20Sopenharmony_ci		return -EINVAL;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	j = pg_start;
3018c2ecf20Sopenharmony_ci	while (j < (pg_start + mem->page_count)) {
3028c2ecf20Sopenharmony_ci		addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
3038c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
3048c2ecf20Sopenharmony_ci		if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
3058c2ecf20Sopenharmony_ci			return -EBUSY;
3068c2ecf20Sopenharmony_ci		j++;
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (!mem->is_flushed) {
3108c2ecf20Sopenharmony_ci		global_cache_flush();
3118c2ecf20Sopenharmony_ci		mem->is_flushed = true;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
3158c2ecf20Sopenharmony_ci		addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
3168c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
3178c2ecf20Sopenharmony_ci		writel(agp_generic_mask_memory(agp_bridge,
3188c2ecf20Sopenharmony_ci					       page_to_phys(mem->pages[i]),
3198c2ecf20Sopenharmony_ci					       mem->type),
3208c2ecf20Sopenharmony_ci		       cur_gatt+GET_GATT_OFF(addr));
3218c2ecf20Sopenharmony_ci		readl(cur_gatt+GET_GATT_OFF(addr));	/* PCI Posting. */
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci	amd_irongate_tlbflush(mem);
3248c2ecf20Sopenharmony_ci	return 0;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int amd_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	int i;
3308c2ecf20Sopenharmony_ci	unsigned long __iomem *cur_gatt;
3318c2ecf20Sopenharmony_ci	unsigned long addr;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (type != mem->type ||
3348c2ecf20Sopenharmony_ci	    agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type))
3358c2ecf20Sopenharmony_ci		return -EINVAL;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	for (i = pg_start; i < (mem->page_count + pg_start); i++) {
3388c2ecf20Sopenharmony_ci		addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
3398c2ecf20Sopenharmony_ci		cur_gatt = GET_GATT(addr);
3408c2ecf20Sopenharmony_ci		writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
3418c2ecf20Sopenharmony_ci		readl(cur_gatt+GET_GATT_OFF(addr));	/* PCI Posting. */
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	amd_irongate_tlbflush(mem);
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic const struct aper_size_info_lvl2 amd_irongate_sizes[7] =
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	{2048, 524288, 0x0000000c},
3518c2ecf20Sopenharmony_ci	{1024, 262144, 0x0000000a},
3528c2ecf20Sopenharmony_ci	{512, 131072, 0x00000008},
3538c2ecf20Sopenharmony_ci	{256, 65536, 0x00000006},
3548c2ecf20Sopenharmony_ci	{128, 32768, 0x00000004},
3558c2ecf20Sopenharmony_ci	{64, 16384, 0x00000002},
3568c2ecf20Sopenharmony_ci	{32, 8192, 0x00000000}
3578c2ecf20Sopenharmony_ci};
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic const struct gatt_mask amd_irongate_masks[] =
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	{.mask = 1, .type = 0}
3628c2ecf20Sopenharmony_ci};
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic const struct agp_bridge_driver amd_irongate_driver = {
3658c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
3668c2ecf20Sopenharmony_ci	.aperture_sizes		= amd_irongate_sizes,
3678c2ecf20Sopenharmony_ci	.size_type		= LVL2_APER_SIZE,
3688c2ecf20Sopenharmony_ci	.num_aperture_sizes	= 7,
3698c2ecf20Sopenharmony_ci	.needs_scratch_page	= true,
3708c2ecf20Sopenharmony_ci	.configure		= amd_irongate_configure,
3718c2ecf20Sopenharmony_ci	.fetch_size		= amd_irongate_fetch_size,
3728c2ecf20Sopenharmony_ci	.cleanup		= amd_irongate_cleanup,
3738c2ecf20Sopenharmony_ci	.tlb_flush		= amd_irongate_tlbflush,
3748c2ecf20Sopenharmony_ci	.mask_memory		= agp_generic_mask_memory,
3758c2ecf20Sopenharmony_ci	.masks			= amd_irongate_masks,
3768c2ecf20Sopenharmony_ci	.agp_enable		= agp_generic_enable,
3778c2ecf20Sopenharmony_ci	.cache_flush		= global_cache_flush,
3788c2ecf20Sopenharmony_ci	.create_gatt_table	= amd_create_gatt_table,
3798c2ecf20Sopenharmony_ci	.free_gatt_table	= amd_free_gatt_table,
3808c2ecf20Sopenharmony_ci	.insert_memory		= amd_insert_memory,
3818c2ecf20Sopenharmony_ci	.remove_memory		= amd_remove_memory,
3828c2ecf20Sopenharmony_ci	.alloc_by_type		= agp_generic_alloc_by_type,
3838c2ecf20Sopenharmony_ci	.free_by_type		= agp_generic_free_by_type,
3848c2ecf20Sopenharmony_ci	.agp_alloc_page		= agp_generic_alloc_page,
3858c2ecf20Sopenharmony_ci	.agp_alloc_pages	= agp_generic_alloc_pages,
3868c2ecf20Sopenharmony_ci	.agp_destroy_page	= agp_generic_destroy_page,
3878c2ecf20Sopenharmony_ci	.agp_destroy_pages	= agp_generic_destroy_pages,
3888c2ecf20Sopenharmony_ci	.agp_type_to_mask_type  = agp_generic_type_to_mask_type,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic struct agp_device_ids amd_agp_device_ids[] =
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	{
3948c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_AMD_FE_GATE_7006,
3958c2ecf20Sopenharmony_ci		.chipset_name	= "Irongate",
3968c2ecf20Sopenharmony_ci	},
3978c2ecf20Sopenharmony_ci	{
3988c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_AMD_FE_GATE_700E,
3998c2ecf20Sopenharmony_ci		.chipset_name	= "761",
4008c2ecf20Sopenharmony_ci	},
4018c2ecf20Sopenharmony_ci	{
4028c2ecf20Sopenharmony_ci		.device_id	= PCI_DEVICE_ID_AMD_FE_GATE_700C,
4038c2ecf20Sopenharmony_ci		.chipset_name	= "760MP",
4048c2ecf20Sopenharmony_ci	},
4058c2ecf20Sopenharmony_ci	{ }, /* dummy final entry, always present */
4068c2ecf20Sopenharmony_ci};
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic int agp_amdk7_probe(struct pci_dev *pdev,
4098c2ecf20Sopenharmony_ci			   const struct pci_device_id *ent)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct agp_bridge_data *bridge;
4128c2ecf20Sopenharmony_ci	u8 cap_ptr;
4138c2ecf20Sopenharmony_ci	int j;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
4168c2ecf20Sopenharmony_ci	if (!cap_ptr)
4178c2ecf20Sopenharmony_ci		return -ENODEV;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	j = ent - agp_amdk7_pci_table;
4208c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "AMD %s chipset\n",
4218c2ecf20Sopenharmony_ci		 amd_agp_device_ids[j].chipset_name);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	bridge = agp_alloc_bridge();
4248c2ecf20Sopenharmony_ci	if (!bridge)
4258c2ecf20Sopenharmony_ci		return -ENOMEM;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	bridge->driver = &amd_irongate_driver;
4288c2ecf20Sopenharmony_ci	bridge->dev_private_data = &amd_irongate_private;
4298c2ecf20Sopenharmony_ci	bridge->dev = pdev;
4308c2ecf20Sopenharmony_ci	bridge->capndx = cap_ptr;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* 751 Errata (22564_B-1.PDF)
4338c2ecf20Sopenharmony_ci	   erratum 20: strobe glitch with Nvidia NV10 GeForce cards.
4348c2ecf20Sopenharmony_ci	   system controller may experience noise due to strong drive strengths
4358c2ecf20Sopenharmony_ci	 */
4368c2ecf20Sopenharmony_ci	if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_7006) {
4378c2ecf20Sopenharmony_ci		struct pci_dev *gfxcard=NULL;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		cap_ptr = 0;
4408c2ecf20Sopenharmony_ci		while (!cap_ptr) {
4418c2ecf20Sopenharmony_ci			gfxcard = pci_get_class(PCI_CLASS_DISPLAY_VGA<<8, gfxcard);
4428c2ecf20Sopenharmony_ci			if (!gfxcard) {
4438c2ecf20Sopenharmony_ci				dev_info(&pdev->dev, "no AGP VGA controller\n");
4448c2ecf20Sopenharmony_ci				return -ENODEV;
4458c2ecf20Sopenharmony_ci			}
4468c2ecf20Sopenharmony_ci			cap_ptr = pci_find_capability(gfxcard, PCI_CAP_ID_AGP);
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		/* With so many variants of NVidia cards, it's simpler just
4508c2ecf20Sopenharmony_ci		   to blacklist them all, and then whitelist them as needed
4518c2ecf20Sopenharmony_ci		   (if necessary at all). */
4528c2ecf20Sopenharmony_ci		if (gfxcard->vendor == PCI_VENDOR_ID_NVIDIA) {
4538c2ecf20Sopenharmony_ci			agp_bridge->flags |= AGP_ERRATA_1X;
4548c2ecf20Sopenharmony_ci			dev_info(&pdev->dev, "AMD 751 chipset with NVidia GeForce; forcing 1X due to errata\n");
4558c2ecf20Sopenharmony_ci		}
4568c2ecf20Sopenharmony_ci		pci_dev_put(gfxcard);
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* 761 Errata (23613_F.pdf)
4608c2ecf20Sopenharmony_ci	 * Revisions B0/B1 were a disaster.
4618c2ecf20Sopenharmony_ci	 * erratum 44: SYSCLK/AGPCLK skew causes 2X failures -- Force mode to 1X
4628c2ecf20Sopenharmony_ci	 * erratum 45: Timing problem prevents fast writes -- Disable fast write.
4638c2ecf20Sopenharmony_ci	 * erratum 46: Setup violation on AGP SBA pins - Disable side band addressing.
4648c2ecf20Sopenharmony_ci	 * With this lot disabled, we should prevent lockups. */
4658c2ecf20Sopenharmony_ci	if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_700E) {
4668c2ecf20Sopenharmony_ci		if (pdev->revision == 0x10 || pdev->revision == 0x11) {
4678c2ecf20Sopenharmony_ci			agp_bridge->flags = AGP_ERRATA_FASTWRITES;
4688c2ecf20Sopenharmony_ci			agp_bridge->flags |= AGP_ERRATA_SBA;
4698c2ecf20Sopenharmony_ci			agp_bridge->flags |= AGP_ERRATA_1X;
4708c2ecf20Sopenharmony_ci			dev_info(&pdev->dev, "AMD 761 chipset with errata; disabling AGP fast writes & SBA and forcing to 1X\n");
4718c2ecf20Sopenharmony_ci		}
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* Fill in the mode register */
4758c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev,
4768c2ecf20Sopenharmony_ci			bridge->capndx+PCI_AGP_STATUS,
4778c2ecf20Sopenharmony_ci			&bridge->mode);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, bridge);
4808c2ecf20Sopenharmony_ci	return agp_add_bridge(bridge);
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic void agp_amdk7_remove(struct pci_dev *pdev)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	agp_remove_bridge(bridge);
4888c2ecf20Sopenharmony_ci	agp_put_bridge(bridge);
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int agp_amdk7_suspend(struct pci_dev *pdev, pm_message_t state)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	pci_save_state(pdev);
4968c2ecf20Sopenharmony_ci	pci_set_power_state(pdev, pci_choose_state(pdev, state));
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	return 0;
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic int agp_amdk7_resume(struct pci_dev *pdev)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	pci_set_power_state(pdev, PCI_D0);
5048c2ecf20Sopenharmony_ci	pci_restore_state(pdev);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	return amd_irongate_driver.configure();
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci/* must be the same order as name table above */
5128c2ecf20Sopenharmony_cistatic const struct pci_device_id agp_amdk7_pci_table[] = {
5138c2ecf20Sopenharmony_ci	{
5148c2ecf20Sopenharmony_ci	.class		= (PCI_CLASS_BRIDGE_HOST << 8),
5158c2ecf20Sopenharmony_ci	.class_mask	= ~0,
5168c2ecf20Sopenharmony_ci	.vendor		= PCI_VENDOR_ID_AMD,
5178c2ecf20Sopenharmony_ci	.device		= PCI_DEVICE_ID_AMD_FE_GATE_7006,
5188c2ecf20Sopenharmony_ci	.subvendor	= PCI_ANY_ID,
5198c2ecf20Sopenharmony_ci	.subdevice	= PCI_ANY_ID,
5208c2ecf20Sopenharmony_ci	},
5218c2ecf20Sopenharmony_ci	{
5228c2ecf20Sopenharmony_ci	.class		= (PCI_CLASS_BRIDGE_HOST << 8),
5238c2ecf20Sopenharmony_ci	.class_mask	= ~0,
5248c2ecf20Sopenharmony_ci	.vendor		= PCI_VENDOR_ID_AMD,
5258c2ecf20Sopenharmony_ci	.device		= PCI_DEVICE_ID_AMD_FE_GATE_700E,
5268c2ecf20Sopenharmony_ci	.subvendor	= PCI_ANY_ID,
5278c2ecf20Sopenharmony_ci	.subdevice	= PCI_ANY_ID,
5288c2ecf20Sopenharmony_ci	},
5298c2ecf20Sopenharmony_ci	{
5308c2ecf20Sopenharmony_ci	.class		= (PCI_CLASS_BRIDGE_HOST << 8),
5318c2ecf20Sopenharmony_ci	.class_mask	= ~0,
5328c2ecf20Sopenharmony_ci	.vendor		= PCI_VENDOR_ID_AMD,
5338c2ecf20Sopenharmony_ci	.device		= PCI_DEVICE_ID_AMD_FE_GATE_700C,
5348c2ecf20Sopenharmony_ci	.subvendor	= PCI_ANY_ID,
5358c2ecf20Sopenharmony_ci	.subdevice	= PCI_ANY_ID,
5368c2ecf20Sopenharmony_ci	},
5378c2ecf20Sopenharmony_ci	{ }
5388c2ecf20Sopenharmony_ci};
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, agp_amdk7_pci_table);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic struct pci_driver agp_amdk7_pci_driver = {
5438c2ecf20Sopenharmony_ci	.name		= "agpgart-amdk7",
5448c2ecf20Sopenharmony_ci	.id_table	= agp_amdk7_pci_table,
5458c2ecf20Sopenharmony_ci	.probe		= agp_amdk7_probe,
5468c2ecf20Sopenharmony_ci	.remove		= agp_amdk7_remove,
5478c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
5488c2ecf20Sopenharmony_ci	.suspend	= agp_amdk7_suspend,
5498c2ecf20Sopenharmony_ci	.resume		= agp_amdk7_resume,
5508c2ecf20Sopenharmony_ci#endif
5518c2ecf20Sopenharmony_ci};
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic int __init agp_amdk7_init(void)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	if (agp_off)
5568c2ecf20Sopenharmony_ci		return -EINVAL;
5578c2ecf20Sopenharmony_ci	return pci_register_driver(&agp_amdk7_pci_driver);
5588c2ecf20Sopenharmony_ci}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic void __exit agp_amdk7_cleanup(void)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	pci_unregister_driver(&agp_amdk7_pci_driver);
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cimodule_init(agp_amdk7_init);
5668c2ecf20Sopenharmony_cimodule_exit(agp_amdk7_cleanup);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL and additional rights");
569