amd-k7-agp.c 15 KB

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