amd-k7-agp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * AMD K7 AGPGART routines.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. #include <linux/init.h>
  7. #include <linux/agp_backend.h>
  8. #include <linux/gfp.h>
  9. #include <linux/page-flags.h>
  10. #include <linux/mm.h>
  11. #include "agp.h"
  12. #define AMD_MMBASE 0x14
  13. #define AMD_APSIZE 0xac
  14. #define AMD_MODECNTL 0xb0
  15. #define AMD_MODECNTL2 0xb2
  16. #define AMD_GARTENABLE 0x02 /* In mmio region (16-bit register) */
  17. #define AMD_ATTBASE 0x04 /* In mmio region (32-bit register) */
  18. #define AMD_TLBFLUSH 0x0c /* In mmio region (32-bit register) */
  19. #define AMD_CACHEENTRY 0x10 /* In mmio region (32-bit register) */
  20. static struct pci_device_id agp_amdk7_pci_table[];
  21. struct amd_page_map {
  22. unsigned long *real;
  23. unsigned long __iomem *remapped;
  24. };
  25. static struct _amd_irongate_private {
  26. volatile u8 __iomem *registers;
  27. struct amd_page_map **gatt_pages;
  28. int num_tables;
  29. } amd_irongate_private;
  30. static int amd_create_page_map(struct amd_page_map *page_map)
  31. {
  32. int i;
  33. page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
  34. if (page_map->real == NULL)
  35. return -ENOMEM;
  36. SetPageReserved(virt_to_page(page_map->real));
  37. global_cache_flush();
  38. page_map->remapped = ioremap_nocache(virt_to_gart(page_map->real),
  39. PAGE_SIZE);
  40. if (page_map->remapped == NULL) {
  41. ClearPageReserved(virt_to_page(page_map->real));
  42. free_page((unsigned long) page_map->real);
  43. page_map->real = NULL;
  44. return -ENOMEM;
  45. }
  46. global_cache_flush();
  47. for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
  48. writel(agp_bridge->scratch_page, page_map->remapped+i);
  49. readl(page_map->remapped+i); /* PCI Posting. */
  50. }
  51. return 0;
  52. }
  53. static void amd_free_page_map(struct amd_page_map *page_map)
  54. {
  55. iounmap(page_map->remapped);
  56. ClearPageReserved(virt_to_page(page_map->real));
  57. free_page((unsigned long) page_map->real);
  58. }
  59. static void amd_free_gatt_pages(void)
  60. {
  61. int i;
  62. struct amd_page_map **tables;
  63. struct amd_page_map *entry;
  64. tables = amd_irongate_private.gatt_pages;
  65. for (i = 0; i < amd_irongate_private.num_tables; i++) {
  66. entry = tables[i];
  67. if (entry != NULL) {
  68. if (entry->real != NULL)
  69. amd_free_page_map(entry);
  70. kfree(entry);
  71. }
  72. }
  73. kfree(tables);
  74. amd_irongate_private.gatt_pages = NULL;
  75. }
  76. static int amd_create_gatt_pages(int nr_tables)
  77. {
  78. struct amd_page_map **tables;
  79. struct amd_page_map *entry;
  80. int retval = 0;
  81. int i;
  82. tables = kmalloc((nr_tables + 1) * sizeof(struct amd_page_map *),
  83. GFP_KERNEL);
  84. if (tables == NULL)
  85. return -ENOMEM;
  86. memset (tables, 0, sizeof(struct amd_page_map *) * (nr_tables + 1));
  87. for (i = 0; i < nr_tables; i++) {
  88. entry = kmalloc(sizeof(struct amd_page_map), GFP_KERNEL);
  89. if (entry == NULL) {
  90. retval = -ENOMEM;
  91. break;
  92. }
  93. memset (entry, 0, sizeof(struct amd_page_map));
  94. tables[i] = entry;
  95. retval = amd_create_page_map(entry);
  96. if (retval != 0)
  97. break;
  98. }
  99. amd_irongate_private.num_tables = nr_tables;
  100. amd_irongate_private.gatt_pages = tables;
  101. if (retval != 0)
  102. amd_free_gatt_pages();
  103. return retval;
  104. }
  105. /* Since we don't need contigious memory we just try
  106. * to get the gatt table once
  107. */
  108. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  109. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  110. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  111. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  112. #define GET_GATT(addr) (amd_irongate_private.gatt_pages[\
  113. GET_PAGE_DIR_IDX(addr)]->remapped)
  114. static int amd_create_gatt_table(struct agp_bridge_data *bridge)
  115. {
  116. struct aper_size_info_lvl2 *value;
  117. struct amd_page_map page_dir;
  118. unsigned long addr;
  119. int retval;
  120. u32 temp;
  121. int i;
  122. value = A_SIZE_LVL2(agp_bridge->current_size);
  123. retval = amd_create_page_map(&page_dir);
  124. if (retval != 0)
  125. return retval;
  126. retval = amd_create_gatt_pages(value->num_entries / 1024);
  127. if (retval != 0) {
  128. amd_free_page_map(&page_dir);
  129. return retval;
  130. }
  131. agp_bridge->gatt_table_real = (u32 *)page_dir.real;
  132. agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
  133. agp_bridge->gatt_bus_addr = virt_to_gart(page_dir.real);
  134. /* Get the address for the gart region.
  135. * This is a bus address even on the alpha, b/c its
  136. * used to program the agp master not the cpu
  137. */
  138. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  139. addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  140. agp_bridge->gart_bus_addr = addr;
  141. /* Calculate the agp offset */
  142. for (i = 0; i < value->num_entries / 1024; i++, addr += 0x00400000) {
  143. writel(virt_to_gart(amd_irongate_private.gatt_pages[i]->real) | 1,
  144. page_dir.remapped+GET_PAGE_DIR_OFF(addr));
  145. readl(page_dir.remapped+GET_PAGE_DIR_OFF(addr)); /* PCI Posting. */
  146. }
  147. return 0;
  148. }
  149. static int amd_free_gatt_table(struct agp_bridge_data *bridge)
  150. {
  151. struct amd_page_map page_dir;
  152. page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
  153. page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
  154. amd_free_gatt_pages();
  155. amd_free_page_map(&page_dir);
  156. return 0;
  157. }
  158. static int amd_irongate_fetch_size(void)
  159. {
  160. int i;
  161. u32 temp;
  162. struct aper_size_info_lvl2 *values;
  163. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  164. temp = (temp & 0x0000000e);
  165. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  166. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  167. if (temp == values[i].size_value) {
  168. agp_bridge->previous_size =
  169. agp_bridge->current_size = (void *) (values + i);
  170. agp_bridge->aperture_size_idx = i;
  171. return values[i].size;
  172. }
  173. }
  174. return 0;
  175. }
  176. static int amd_irongate_configure(void)
  177. {
  178. struct aper_size_info_lvl2 *current_size;
  179. u32 temp;
  180. u16 enable_reg;
  181. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  182. /* Get the memory mapped registers */
  183. pci_read_config_dword(agp_bridge->dev, AMD_MMBASE, &temp);
  184. temp = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  185. amd_irongate_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
  186. /* Write out the address of the gatt table */
  187. writel(agp_bridge->gatt_bus_addr, amd_irongate_private.registers+AMD_ATTBASE);
  188. readl(amd_irongate_private.registers+AMD_ATTBASE); /* PCI Posting. */
  189. /* Write the Sync register */
  190. pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL, 0x80);
  191. /* Set indexing mode */
  192. pci_write_config_byte(agp_bridge->dev, AMD_MODECNTL2, 0x00);
  193. /* Write the enable register */
  194. enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
  195. enable_reg = (enable_reg | 0x0004);
  196. writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
  197. readw(amd_irongate_private.registers+AMD_GARTENABLE); /* PCI Posting. */
  198. /* Write out the size register */
  199. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  200. temp = (((temp & ~(0x0000000e)) | current_size->size_value) | 1);
  201. pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
  202. /* Flush the tlb */
  203. writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
  204. readl(amd_irongate_private.registers+AMD_TLBFLUSH); /* PCI Posting.*/
  205. return 0;
  206. }
  207. static void amd_irongate_cleanup(void)
  208. {
  209. struct aper_size_info_lvl2 *previous_size;
  210. u32 temp;
  211. u16 enable_reg;
  212. previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
  213. enable_reg = readw(amd_irongate_private.registers+AMD_GARTENABLE);
  214. enable_reg = (enable_reg & ~(0x0004));
  215. writew(enable_reg, amd_irongate_private.registers+AMD_GARTENABLE);
  216. readw(amd_irongate_private.registers+AMD_GARTENABLE); /* PCI Posting. */
  217. /* Write back the previous size and disable gart translation */
  218. pci_read_config_dword(agp_bridge->dev, AMD_APSIZE, &temp);
  219. temp = ((temp & ~(0x0000000f)) | previous_size->size_value);
  220. pci_write_config_dword(agp_bridge->dev, AMD_APSIZE, temp);
  221. iounmap((void __iomem *) amd_irongate_private.registers);
  222. }
  223. /*
  224. * This routine could be implemented by taking the addresses
  225. * written to the GATT, and flushing them individually. However
  226. * currently it just flushes the whole table. Which is probably
  227. * more efficent, since agp_memory blocks can be a large number of
  228. * entries.
  229. */
  230. static void amd_irongate_tlbflush(struct agp_memory *temp)
  231. {
  232. writel(1, amd_irongate_private.registers+AMD_TLBFLUSH);
  233. readl(amd_irongate_private.registers+AMD_TLBFLUSH); /* PCI Posting. */
  234. }
  235. static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
  236. {
  237. int i, j, num_entries;
  238. unsigned long __iomem *cur_gatt;
  239. unsigned long addr;
  240. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  241. if (type != 0 || mem->type != 0)
  242. return -EINVAL;
  243. if ((pg_start + mem->page_count) > num_entries)
  244. return -EINVAL;
  245. j = pg_start;
  246. while (j < (pg_start + mem->page_count)) {
  247. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  248. cur_gatt = GET_GATT(addr);
  249. if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
  250. return -EBUSY;
  251. j++;
  252. }
  253. if (mem->is_flushed == FALSE) {
  254. global_cache_flush();
  255. mem->is_flushed = TRUE;
  256. }
  257. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  258. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  259. cur_gatt = GET_GATT(addr);
  260. writel(agp_generic_mask_memory(agp_bridge,
  261. mem->memory[i], mem->type), cur_gatt+GET_GATT_OFF(addr));
  262. readl(cur_gatt+GET_GATT_OFF(addr)); /* PCI Posting. */
  263. }
  264. amd_irongate_tlbflush(mem);
  265. return 0;
  266. }
  267. static int amd_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  268. {
  269. int i;
  270. unsigned long __iomem *cur_gatt;
  271. unsigned long addr;
  272. if (type != 0 || mem->type != 0)
  273. return -EINVAL;
  274. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  275. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  276. cur_gatt = GET_GATT(addr);
  277. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  278. readl(cur_gatt+GET_GATT_OFF(addr)); /* PCI Posting. */
  279. }
  280. amd_irongate_tlbflush(mem);
  281. return 0;
  282. }
  283. static struct aper_size_info_lvl2 amd_irongate_sizes[7] =
  284. {
  285. {2048, 524288, 0x0000000c},
  286. {1024, 262144, 0x0000000a},
  287. {512, 131072, 0x00000008},
  288. {256, 65536, 0x00000006},
  289. {128, 32768, 0x00000004},
  290. {64, 16384, 0x00000002},
  291. {32, 8192, 0x00000000}
  292. };
  293. static struct gatt_mask amd_irongate_masks[] =
  294. {
  295. {.mask = 1, .type = 0}
  296. };
  297. static struct agp_bridge_driver amd_irongate_driver = {
  298. .owner = THIS_MODULE,
  299. .aperture_sizes = amd_irongate_sizes,
  300. .size_type = LVL2_APER_SIZE,
  301. .num_aperture_sizes = 7,
  302. .configure = amd_irongate_configure,
  303. .fetch_size = amd_irongate_fetch_size,
  304. .cleanup = amd_irongate_cleanup,
  305. .tlb_flush = amd_irongate_tlbflush,
  306. .mask_memory = agp_generic_mask_memory,
  307. .masks = amd_irongate_masks,
  308. .agp_enable = agp_generic_enable,
  309. .cache_flush = global_cache_flush,
  310. .create_gatt_table = amd_create_gatt_table,
  311. .free_gatt_table = amd_free_gatt_table,
  312. .insert_memory = amd_insert_memory,
  313. .remove_memory = amd_remove_memory,
  314. .alloc_by_type = agp_generic_alloc_by_type,
  315. .free_by_type = agp_generic_free_by_type,
  316. .agp_alloc_page = agp_generic_alloc_page,
  317. .agp_destroy_page = agp_generic_destroy_page,
  318. };
  319. static struct agp_device_ids amd_agp_device_ids[] __devinitdata =
  320. {
  321. {
  322. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_7006,
  323. .chipset_name = "Irongate",
  324. },
  325. {
  326. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_700E,
  327. .chipset_name = "761",
  328. },
  329. {
  330. .device_id = PCI_DEVICE_ID_AMD_FE_GATE_700C,
  331. .chipset_name = "760MP",
  332. },
  333. { }, /* dummy final entry, always present */
  334. };
  335. static int __devinit agp_amdk7_probe(struct pci_dev *pdev,
  336. const struct pci_device_id *ent)
  337. {
  338. struct agp_bridge_data *bridge;
  339. u8 cap_ptr;
  340. int j;
  341. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  342. if (!cap_ptr)
  343. return -ENODEV;
  344. j = ent - agp_amdk7_pci_table;
  345. printk(KERN_INFO PFX "Detected AMD %s chipset\n",
  346. amd_agp_device_ids[j].chipset_name);
  347. bridge = agp_alloc_bridge();
  348. if (!bridge)
  349. return -ENOMEM;
  350. bridge->driver = &amd_irongate_driver;
  351. bridge->dev_private_data = &amd_irongate_private,
  352. bridge->dev = pdev;
  353. bridge->capndx = cap_ptr;
  354. /* 751 Errata (22564_B-1.PDF)
  355. erratum 20: strobe glitch with Nvidia NV10 GeForce cards.
  356. system controller may experience noise due to strong drive strengths
  357. */
  358. if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_7006) {
  359. u8 cap_ptr=0;
  360. struct pci_dev *gfxcard=NULL;
  361. while (!cap_ptr) {
  362. gfxcard = pci_get_class(PCI_CLASS_DISPLAY_VGA<<8, gfxcard);
  363. if (!gfxcard) {
  364. printk (KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
  365. return -ENODEV;
  366. }
  367. cap_ptr = pci_find_capability(gfxcard, PCI_CAP_ID_AGP);
  368. if (!cap_ptr) {
  369. pci_dev_put(gfxcard);
  370. continue;
  371. }
  372. }
  373. /* With so many variants of NVidia cards, it's simpler just
  374. to blacklist them all, and then whitelist them as needed
  375. (if necessary at all). */
  376. if (gfxcard->vendor == PCI_VENDOR_ID_NVIDIA) {
  377. agp_bridge->flags |= AGP_ERRATA_1X;
  378. printk (KERN_INFO PFX "AMD 751 chipset with NVidia GeForce detected. Forcing to 1X due to errata.\n");
  379. }
  380. pci_dev_put(gfxcard);
  381. }
  382. /* 761 Errata (23613_F.pdf)
  383. * Revisions B0/B1 were a disaster.
  384. * erratum 44: SYSCLK/AGPCLK skew causes 2X failures -- Force mode to 1X
  385. * erratum 45: Timing problem prevents fast writes -- Disable fast write.
  386. * erratum 46: Setup violation on AGP SBA pins - Disable side band addressing.
  387. * With this lot disabled, we should prevent lockups. */
  388. if (agp_bridge->dev->device == PCI_DEVICE_ID_AMD_FE_GATE_700E) {
  389. u8 revision=0;
  390. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision);
  391. if (revision == 0x10 || revision == 0x11) {
  392. agp_bridge->flags = AGP_ERRATA_FASTWRITES;
  393. agp_bridge->flags |= AGP_ERRATA_SBA;
  394. agp_bridge->flags |= AGP_ERRATA_1X;
  395. printk (KERN_INFO PFX "AMD 761 chipset with errata detected - disabling AGP fast writes & SBA and forcing to 1X.\n");
  396. }
  397. }
  398. /* Fill in the mode register */
  399. pci_read_config_dword(pdev,
  400. bridge->capndx+PCI_AGP_STATUS,
  401. &bridge->mode);
  402. pci_set_drvdata(pdev, bridge);
  403. return agp_add_bridge(bridge);
  404. }
  405. static void __devexit agp_amdk7_remove(struct pci_dev *pdev)
  406. {
  407. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  408. agp_remove_bridge(bridge);
  409. agp_put_bridge(bridge);
  410. }
  411. /* must be the same order as name table above */
  412. static struct pci_device_id agp_amdk7_pci_table[] = {
  413. {
  414. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  415. .class_mask = ~0,
  416. .vendor = PCI_VENDOR_ID_AMD,
  417. .device = PCI_DEVICE_ID_AMD_FE_GATE_7006,
  418. .subvendor = PCI_ANY_ID,
  419. .subdevice = PCI_ANY_ID,
  420. },
  421. {
  422. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  423. .class_mask = ~0,
  424. .vendor = PCI_VENDOR_ID_AMD,
  425. .device = PCI_DEVICE_ID_AMD_FE_GATE_700E,
  426. .subvendor = PCI_ANY_ID,
  427. .subdevice = PCI_ANY_ID,
  428. },
  429. {
  430. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  431. .class_mask = ~0,
  432. .vendor = PCI_VENDOR_ID_AMD,
  433. .device = PCI_DEVICE_ID_AMD_FE_GATE_700C,
  434. .subvendor = PCI_ANY_ID,
  435. .subdevice = PCI_ANY_ID,
  436. },
  437. { }
  438. };
  439. MODULE_DEVICE_TABLE(pci, agp_amdk7_pci_table);
  440. static struct pci_driver agp_amdk7_pci_driver = {
  441. .name = "agpgart-amdk7",
  442. .id_table = agp_amdk7_pci_table,
  443. .probe = agp_amdk7_probe,
  444. .remove = agp_amdk7_remove,
  445. };
  446. static int __init agp_amdk7_init(void)
  447. {
  448. if (agp_off)
  449. return -EINVAL;
  450. return pci_register_driver(&agp_amdk7_pci_driver);
  451. }
  452. static void __exit agp_amdk7_cleanup(void)
  453. {
  454. pci_unregister_driver(&agp_amdk7_pci_driver);
  455. }
  456. module_init(agp_amdk7_init);
  457. module_exit(agp_amdk7_cleanup);
  458. MODULE_LICENSE("GPL and additional rights");