amd64-agp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright 2001-2003 SuSE Labs.
  3. * Distributed under the GNU public license, v2.
  4. *
  5. * This is a GART driver for the AMD Opteron/Athlon64 on-CPU northbridge.
  6. * It also includes support for the AMD 8151 AGP bridge,
  7. * although it doesn't actually do much, as all the real
  8. * work is done in the northbridge(s).
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/init.h>
  13. #include <linux/agp_backend.h>
  14. #include <linux/mmzone.h>
  15. #include <asm/page.h> /* PAGE_SIZE */
  16. #include <asm/e820.h>
  17. #include <asm/k8.h>
  18. #include "agp.h"
  19. /* PTE bits. */
  20. #define GPTE_VALID 1
  21. #define GPTE_COHERENT 2
  22. /* Aperture control register bits. */
  23. #define GARTEN (1<<0)
  24. #define DISGARTCPU (1<<4)
  25. #define DISGARTIO (1<<5)
  26. /* GART cache control register bits. */
  27. #define INVGART (1<<0)
  28. #define GARTPTEERR (1<<1)
  29. /* K8 On-cpu GART registers */
  30. #define AMD64_GARTAPERTURECTL 0x90
  31. #define AMD64_GARTAPERTUREBASE 0x94
  32. #define AMD64_GARTTABLEBASE 0x98
  33. #define AMD64_GARTCACHECTL 0x9c
  34. #define AMD64_GARTEN (1<<0)
  35. /* NVIDIA K8 registers */
  36. #define NVIDIA_X86_64_0_APBASE 0x10
  37. #define NVIDIA_X86_64_1_APBASE1 0x50
  38. #define NVIDIA_X86_64_1_APLIMIT1 0x54
  39. #define NVIDIA_X86_64_1_APSIZE 0xa8
  40. #define NVIDIA_X86_64_1_APBASE2 0xd8
  41. #define NVIDIA_X86_64_1_APLIMIT2 0xdc
  42. /* ULi K8 registers */
  43. #define ULI_X86_64_BASE_ADDR 0x10
  44. #define ULI_X86_64_HTT_FEA_REG 0x50
  45. #define ULI_X86_64_ENU_SCR_REG 0x54
  46. static struct resource *aperture_resource;
  47. static int __initdata agp_try_unsupported = 1;
  48. static void amd64_tlbflush(struct agp_memory *temp)
  49. {
  50. k8_flush_garts();
  51. }
  52. static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
  53. {
  54. int i, j, num_entries;
  55. long long tmp;
  56. int mask_type;
  57. struct agp_bridge_data *bridge = mem->bridge;
  58. u32 pte;
  59. num_entries = agp_num_entries();
  60. if (type != mem->type)
  61. return -EINVAL;
  62. mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
  63. if (mask_type != 0)
  64. return -EINVAL;
  65. /* Make sure we can fit the range in the gatt table. */
  66. /* FIXME: could wrap */
  67. if (((unsigned long)pg_start + mem->page_count) > num_entries)
  68. return -EINVAL;
  69. j = pg_start;
  70. /* gatt table should be empty. */
  71. while (j < (pg_start + mem->page_count)) {
  72. if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j)))
  73. return -EBUSY;
  74. j++;
  75. }
  76. if (mem->is_flushed == FALSE) {
  77. global_cache_flush();
  78. mem->is_flushed = TRUE;
  79. }
  80. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  81. tmp = agp_bridge->driver->mask_memory(agp_bridge,
  82. mem->memory[i], mask_type);
  83. BUG_ON(tmp & 0xffffff0000000ffcULL);
  84. pte = (tmp & 0x000000ff00000000ULL) >> 28;
  85. pte |=(tmp & 0x00000000fffff000ULL);
  86. pte |= GPTE_VALID | GPTE_COHERENT;
  87. writel(pte, agp_bridge->gatt_table+j);
  88. readl(agp_bridge->gatt_table+j); /* PCI Posting. */
  89. }
  90. amd64_tlbflush(mem);
  91. return 0;
  92. }
  93. /*
  94. * This hack alters the order element according
  95. * to the size of a long. It sucks. I totally disown this, even
  96. * though it does appear to work for the most part.
  97. */
  98. static struct aper_size_info_32 amd64_aperture_sizes[7] =
  99. {
  100. {32, 8192, 3+(sizeof(long)/8), 0 },
  101. {64, 16384, 4+(sizeof(long)/8), 1<<1 },
  102. {128, 32768, 5+(sizeof(long)/8), 1<<2 },
  103. {256, 65536, 6+(sizeof(long)/8), 1<<1 | 1<<2 },
  104. {512, 131072, 7+(sizeof(long)/8), 1<<3 },
  105. {1024, 262144, 8+(sizeof(long)/8), 1<<1 | 1<<3},
  106. {2048, 524288, 9+(sizeof(long)/8), 1<<2 | 1<<3}
  107. };
  108. /*
  109. * Get the current Aperture size from the x86-64.
  110. * Note, that there may be multiple x86-64's, but we just return
  111. * the value from the first one we find. The set_size functions
  112. * keep the rest coherent anyway. Or at least should do.
  113. */
  114. static int amd64_fetch_size(void)
  115. {
  116. struct pci_dev *dev;
  117. int i;
  118. u32 temp;
  119. struct aper_size_info_32 *values;
  120. dev = k8_northbridges[0];
  121. if (dev==NULL)
  122. return 0;
  123. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &temp);
  124. temp = (temp & 0xe);
  125. values = A_SIZE_32(amd64_aperture_sizes);
  126. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  127. if (temp == values[i].size_value) {
  128. agp_bridge->previous_size =
  129. agp_bridge->current_size = (void *) (values + i);
  130. agp_bridge->aperture_size_idx = i;
  131. return values[i].size;
  132. }
  133. }
  134. return 0;
  135. }
  136. /*
  137. * In a multiprocessor x86-64 system, this function gets
  138. * called once for each CPU.
  139. */
  140. static u64 amd64_configure (struct pci_dev *hammer, u64 gatt_table)
  141. {
  142. u64 aperturebase;
  143. u32 tmp;
  144. u64 addr, aper_base;
  145. /* Address to map to */
  146. pci_read_config_dword (hammer, AMD64_GARTAPERTUREBASE, &tmp);
  147. aperturebase = tmp << 25;
  148. aper_base = (aperturebase & PCI_BASE_ADDRESS_MEM_MASK);
  149. /* address of the mappings table */
  150. addr = (u64) gatt_table;
  151. addr >>= 12;
  152. tmp = (u32) addr<<4;
  153. tmp &= ~0xf;
  154. pci_write_config_dword (hammer, AMD64_GARTTABLEBASE, tmp);
  155. /* Enable GART translation for this hammer. */
  156. pci_read_config_dword(hammer, AMD64_GARTAPERTURECTL, &tmp);
  157. tmp |= GARTEN;
  158. tmp &= ~(DISGARTCPU | DISGARTIO);
  159. pci_write_config_dword(hammer, AMD64_GARTAPERTURECTL, tmp);
  160. return aper_base;
  161. }
  162. static const struct aper_size_info_32 amd_8151_sizes[7] =
  163. {
  164. {2048, 524288, 9, 0x00000000 }, /* 0 0 0 0 0 0 */
  165. {1024, 262144, 8, 0x00000400 }, /* 1 0 0 0 0 0 */
  166. {512, 131072, 7, 0x00000600 }, /* 1 1 0 0 0 0 */
  167. {256, 65536, 6, 0x00000700 }, /* 1 1 1 0 0 0 */
  168. {128, 32768, 5, 0x00000720 }, /* 1 1 1 1 0 0 */
  169. {64, 16384, 4, 0x00000730 }, /* 1 1 1 1 1 0 */
  170. {32, 8192, 3, 0x00000738 } /* 1 1 1 1 1 1 */
  171. };
  172. static int amd_8151_configure(void)
  173. {
  174. unsigned long gatt_bus = virt_to_gart(agp_bridge->gatt_table_real);
  175. int i;
  176. /* Configure AGP regs in each x86-64 host bridge. */
  177. for (i = 0; i < num_k8_northbridges; i++) {
  178. agp_bridge->gart_bus_addr =
  179. amd64_configure(k8_northbridges[i], gatt_bus);
  180. }
  181. k8_flush_garts();
  182. return 0;
  183. }
  184. static void amd64_cleanup(void)
  185. {
  186. u32 tmp;
  187. int i;
  188. for (i = 0; i < num_k8_northbridges; i++) {
  189. struct pci_dev *dev = k8_northbridges[i];
  190. /* disable gart translation */
  191. pci_read_config_dword (dev, AMD64_GARTAPERTURECTL, &tmp);
  192. tmp &= ~AMD64_GARTEN;
  193. pci_write_config_dword (dev, AMD64_GARTAPERTURECTL, tmp);
  194. }
  195. }
  196. static const struct agp_bridge_driver amd_8151_driver = {
  197. .owner = THIS_MODULE,
  198. .aperture_sizes = amd_8151_sizes,
  199. .size_type = U32_APER_SIZE,
  200. .num_aperture_sizes = 7,
  201. .configure = amd_8151_configure,
  202. .fetch_size = amd64_fetch_size,
  203. .cleanup = amd64_cleanup,
  204. .tlb_flush = amd64_tlbflush,
  205. .mask_memory = agp_generic_mask_memory,
  206. .masks = NULL,
  207. .agp_enable = agp_generic_enable,
  208. .cache_flush = global_cache_flush,
  209. .create_gatt_table = agp_generic_create_gatt_table,
  210. .free_gatt_table = agp_generic_free_gatt_table,
  211. .insert_memory = amd64_insert_memory,
  212. .remove_memory = agp_generic_remove_memory,
  213. .alloc_by_type = agp_generic_alloc_by_type,
  214. .free_by_type = agp_generic_free_by_type,
  215. .agp_alloc_page = agp_generic_alloc_page,
  216. .agp_destroy_page = agp_generic_destroy_page,
  217. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  218. };
  219. /* Some basic sanity checks for the aperture. */
  220. static int __devinit aperture_valid(u64 aper, u32 size)
  221. {
  222. if (aper == 0) {
  223. printk(KERN_ERR PFX "No aperture\n");
  224. return 0;
  225. }
  226. if (size < 32*1024*1024) {
  227. printk(KERN_ERR PFX "Aperture too small (%d MB)\n", size>>20);
  228. return 0;
  229. }
  230. if ((u64)aper + size > 0x100000000ULL) {
  231. printk(KERN_ERR PFX "Aperture out of bounds\n");
  232. return 0;
  233. }
  234. if (e820_any_mapped(aper, aper + size, E820_RAM)) {
  235. printk(KERN_ERR PFX "Aperture pointing to RAM\n");
  236. return 0;
  237. }
  238. /* Request the Aperture. This catches cases when someone else
  239. already put a mapping in there - happens with some very broken BIOS
  240. Maybe better to use pci_assign_resource/pci_enable_device instead
  241. trusting the bridges? */
  242. if (!aperture_resource &&
  243. !(aperture_resource = request_mem_region(aper, size, "aperture"))) {
  244. printk(KERN_ERR PFX "Aperture conflicts with PCI mapping.\n");
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. /*
  250. * W*s centric BIOS sometimes only set up the aperture in the AGP
  251. * bridge, not the northbridge. On AMD64 this is handled early
  252. * in aperture.c, but when IOMMU is not enabled or we run
  253. * on a 32bit kernel this needs to be redone.
  254. * Unfortunately it is impossible to fix the aperture here because it's too late
  255. * to allocate that much memory. But at least error out cleanly instead of
  256. * crashing.
  257. */
  258. static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
  259. u16 cap)
  260. {
  261. u32 aper_low, aper_hi;
  262. u64 aper, nb_aper;
  263. int order = 0;
  264. u32 nb_order, nb_base;
  265. u16 apsize;
  266. pci_read_config_dword(nb, 0x90, &nb_order);
  267. nb_order = (nb_order >> 1) & 7;
  268. pci_read_config_dword(nb, 0x94, &nb_base);
  269. nb_aper = nb_base << 25;
  270. if (aperture_valid(nb_aper, (32*1024*1024)<<nb_order)) {
  271. return 0;
  272. }
  273. /* Northbridge seems to contain crap. Try the AGP bridge. */
  274. pci_read_config_word(agp, cap+0x14, &apsize);
  275. if (apsize == 0xffff)
  276. return -1;
  277. apsize &= 0xfff;
  278. /* Some BIOS use weird encodings not in the AGPv3 table. */
  279. if (apsize & 0xff)
  280. apsize |= 0xf00;
  281. order = 7 - hweight16(apsize);
  282. pci_read_config_dword(agp, 0x10, &aper_low);
  283. pci_read_config_dword(agp, 0x14, &aper_hi);
  284. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  285. printk(KERN_INFO PFX "Aperture from AGP @ %Lx size %u MB\n", aper, 32 << order);
  286. if (order < 0 || !aperture_valid(aper, (32*1024*1024)<<order))
  287. return -1;
  288. pci_write_config_dword(nb, 0x90, order << 1);
  289. pci_write_config_dword(nb, 0x94, aper >> 25);
  290. return 0;
  291. }
  292. static __devinit int cache_nbs (struct pci_dev *pdev, u32 cap_ptr)
  293. {
  294. int i;
  295. if (cache_k8_northbridges() < 0)
  296. return -ENODEV;
  297. i = 0;
  298. for (i = 0; i < num_k8_northbridges; i++) {
  299. struct pci_dev *dev = k8_northbridges[i];
  300. if (fix_northbridge(dev, pdev, cap_ptr) < 0) {
  301. printk(KERN_ERR PFX "No usable aperture found.\n");
  302. #ifdef __x86_64__
  303. /* should port this to i386 */
  304. printk(KERN_ERR PFX "Consider rebooting with iommu=memaper=2 to get a good aperture.\n");
  305. #endif
  306. return -1;
  307. }
  308. }
  309. return 0;
  310. }
  311. /* Handle AMD 8151 quirks */
  312. static void __devinit amd8151_init(struct pci_dev *pdev, struct agp_bridge_data *bridge)
  313. {
  314. char *revstring;
  315. switch (pdev->revision) {
  316. case 0x01: revstring="A0"; break;
  317. case 0x02: revstring="A1"; break;
  318. case 0x11: revstring="B0"; break;
  319. case 0x12: revstring="B1"; break;
  320. case 0x13: revstring="B2"; break;
  321. case 0x14: revstring="B3"; break;
  322. default: revstring="??"; break;
  323. }
  324. printk (KERN_INFO PFX "Detected AMD 8151 AGP Bridge rev %s\n", revstring);
  325. /*
  326. * Work around errata.
  327. * Chips before B2 stepping incorrectly reporting v3.5
  328. */
  329. if (pdev->revision < 0x13) {
  330. printk (KERN_INFO PFX "Correcting AGP revision (reports 3.5, is really 3.0)\n");
  331. bridge->major_version = 3;
  332. bridge->minor_version = 0;
  333. }
  334. }
  335. static const struct aper_size_info_32 uli_sizes[7] =
  336. {
  337. {256, 65536, 6, 10},
  338. {128, 32768, 5, 9},
  339. {64, 16384, 4, 8},
  340. {32, 8192, 3, 7},
  341. {16, 4096, 2, 6},
  342. {8, 2048, 1, 4},
  343. {4, 1024, 0, 3}
  344. };
  345. static int __devinit uli_agp_init(struct pci_dev *pdev)
  346. {
  347. u32 httfea,baseaddr,enuscr;
  348. struct pci_dev *dev1;
  349. int i;
  350. unsigned size = amd64_fetch_size();
  351. printk(KERN_INFO "Setting up ULi AGP.\n");
  352. dev1 = pci_get_slot (pdev->bus,PCI_DEVFN(0,0));
  353. if (dev1 == NULL) {
  354. printk(KERN_INFO PFX "Detected a ULi chipset, "
  355. "but could not fine the secondary device.\n");
  356. return -ENODEV;
  357. }
  358. for (i = 0; i < ARRAY_SIZE(uli_sizes); i++)
  359. if (uli_sizes[i].size == size)
  360. break;
  361. if (i == ARRAY_SIZE(uli_sizes)) {
  362. printk(KERN_INFO PFX "No ULi size found for %d\n", size);
  363. return -ENODEV;
  364. }
  365. /* shadow x86-64 registers into ULi registers */
  366. pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &httfea);
  367. /* if x86-64 aperture base is beyond 4G, exit here */
  368. if ((httfea & 0x7fff) >> (32 - 25))
  369. return -ENODEV;
  370. httfea = (httfea& 0x7fff) << 25;
  371. pci_read_config_dword(pdev, ULI_X86_64_BASE_ADDR, &baseaddr);
  372. baseaddr&= ~PCI_BASE_ADDRESS_MEM_MASK;
  373. baseaddr|= httfea;
  374. pci_write_config_dword(pdev, ULI_X86_64_BASE_ADDR, baseaddr);
  375. enuscr= httfea+ (size * 1024 * 1024) - 1;
  376. pci_write_config_dword(dev1, ULI_X86_64_HTT_FEA_REG, httfea);
  377. pci_write_config_dword(dev1, ULI_X86_64_ENU_SCR_REG, enuscr);
  378. pci_dev_put(dev1);
  379. return 0;
  380. }
  381. static const struct aper_size_info_32 nforce3_sizes[5] =
  382. {
  383. {512, 131072, 7, 0x00000000 },
  384. {256, 65536, 6, 0x00000008 },
  385. {128, 32768, 5, 0x0000000C },
  386. {64, 16384, 4, 0x0000000E },
  387. {32, 8192, 3, 0x0000000F }
  388. };
  389. /* Handle shadow device of the Nvidia NForce3 */
  390. /* CHECK-ME original 2.4 version set up some IORRs. Check if that is needed. */
  391. static int nforce3_agp_init(struct pci_dev *pdev)
  392. {
  393. u32 tmp, apbase, apbar, aplimit;
  394. struct pci_dev *dev1;
  395. int i;
  396. unsigned size = amd64_fetch_size();
  397. printk(KERN_INFO PFX "Setting up Nforce3 AGP.\n");
  398. dev1 = pci_get_slot(pdev->bus, PCI_DEVFN(11, 0));
  399. if (dev1 == NULL) {
  400. printk(KERN_INFO PFX "agpgart: Detected an NVIDIA "
  401. "nForce3 chipset, but could not find "
  402. "the secondary device.\n");
  403. return -ENODEV;
  404. }
  405. for (i = 0; i < ARRAY_SIZE(nforce3_sizes); i++)
  406. if (nforce3_sizes[i].size == size)
  407. break;
  408. if (i == ARRAY_SIZE(nforce3_sizes)) {
  409. printk(KERN_INFO PFX "No NForce3 size found for %d\n", size);
  410. return -ENODEV;
  411. }
  412. pci_read_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, &tmp);
  413. tmp &= ~(0xf);
  414. tmp |= nforce3_sizes[i].size_value;
  415. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, tmp);
  416. /* shadow x86-64 registers into NVIDIA registers */
  417. pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &apbase);
  418. /* if x86-64 aperture base is beyond 4G, exit here */
  419. if ( (apbase & 0x7fff) >> (32 - 25) ) {
  420. printk(KERN_INFO PFX "aperture base > 4G\n");
  421. return -ENODEV;
  422. }
  423. apbase = (apbase & 0x7fff) << 25;
  424. pci_read_config_dword(pdev, NVIDIA_X86_64_0_APBASE, &apbar);
  425. apbar &= ~PCI_BASE_ADDRESS_MEM_MASK;
  426. apbar |= apbase;
  427. pci_write_config_dword(pdev, NVIDIA_X86_64_0_APBASE, apbar);
  428. aplimit = apbase + (size * 1024 * 1024) - 1;
  429. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE1, apbase);
  430. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT1, aplimit);
  431. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE2, apbase);
  432. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT2, aplimit);
  433. pci_dev_put(dev1);
  434. return 0;
  435. }
  436. static int __devinit agp_amd64_probe(struct pci_dev *pdev,
  437. const struct pci_device_id *ent)
  438. {
  439. struct agp_bridge_data *bridge;
  440. u8 cap_ptr;
  441. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  442. if (!cap_ptr)
  443. return -ENODEV;
  444. /* Could check for AGPv3 here */
  445. bridge = agp_alloc_bridge();
  446. if (!bridge)
  447. return -ENOMEM;
  448. if (pdev->vendor == PCI_VENDOR_ID_AMD &&
  449. pdev->device == PCI_DEVICE_ID_AMD_8151_0) {
  450. amd8151_init(pdev, bridge);
  451. } else {
  452. printk(KERN_INFO PFX "Detected AGP bridge %x\n", pdev->devfn);
  453. }
  454. bridge->driver = &amd_8151_driver;
  455. bridge->dev = pdev;
  456. bridge->capndx = cap_ptr;
  457. /* Fill in the mode register */
  458. pci_read_config_dword(pdev, bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
  459. if (cache_nbs(pdev, cap_ptr) == -1) {
  460. agp_put_bridge(bridge);
  461. return -ENODEV;
  462. }
  463. if (pdev->vendor == PCI_VENDOR_ID_NVIDIA) {
  464. int ret = nforce3_agp_init(pdev);
  465. if (ret) {
  466. agp_put_bridge(bridge);
  467. return ret;
  468. }
  469. }
  470. if (pdev->vendor == PCI_VENDOR_ID_AL) {
  471. int ret = uli_agp_init(pdev);
  472. if (ret) {
  473. agp_put_bridge(bridge);
  474. return ret;
  475. }
  476. }
  477. pci_set_drvdata(pdev, bridge);
  478. return agp_add_bridge(bridge);
  479. }
  480. static void __devexit agp_amd64_remove(struct pci_dev *pdev)
  481. {
  482. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  483. release_mem_region(virt_to_gart(bridge->gatt_table_real),
  484. amd64_aperture_sizes[bridge->aperture_size_idx].size);
  485. agp_remove_bridge(bridge);
  486. agp_put_bridge(bridge);
  487. }
  488. #ifdef CONFIG_PM
  489. static int agp_amd64_suspend(struct pci_dev *pdev, pm_message_t state)
  490. {
  491. pci_save_state(pdev);
  492. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  493. return 0;
  494. }
  495. static int agp_amd64_resume(struct pci_dev *pdev)
  496. {
  497. pci_set_power_state(pdev, PCI_D0);
  498. pci_restore_state(pdev);
  499. if (pdev->vendor == PCI_VENDOR_ID_NVIDIA)
  500. nforce3_agp_init(pdev);
  501. return amd_8151_configure();
  502. }
  503. #endif /* CONFIG_PM */
  504. static struct pci_device_id agp_amd64_pci_table[] = {
  505. {
  506. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  507. .class_mask = ~0,
  508. .vendor = PCI_VENDOR_ID_AMD,
  509. .device = PCI_DEVICE_ID_AMD_8151_0,
  510. .subvendor = PCI_ANY_ID,
  511. .subdevice = PCI_ANY_ID,
  512. },
  513. /* ULi M1689 */
  514. {
  515. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  516. .class_mask = ~0,
  517. .vendor = PCI_VENDOR_ID_AL,
  518. .device = PCI_DEVICE_ID_AL_M1689,
  519. .subvendor = PCI_ANY_ID,
  520. .subdevice = PCI_ANY_ID,
  521. },
  522. /* VIA K8T800Pro */
  523. {
  524. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  525. .class_mask = ~0,
  526. .vendor = PCI_VENDOR_ID_VIA,
  527. .device = PCI_DEVICE_ID_VIA_K8T800PRO_0,
  528. .subvendor = PCI_ANY_ID,
  529. .subdevice = PCI_ANY_ID,
  530. },
  531. /* VIA K8T800 */
  532. {
  533. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  534. .class_mask = ~0,
  535. .vendor = PCI_VENDOR_ID_VIA,
  536. .device = PCI_DEVICE_ID_VIA_8385_0,
  537. .subvendor = PCI_ANY_ID,
  538. .subdevice = PCI_ANY_ID,
  539. },
  540. /* VIA K8M800 / K8N800 */
  541. {
  542. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  543. .class_mask = ~0,
  544. .vendor = PCI_VENDOR_ID_VIA,
  545. .device = PCI_DEVICE_ID_VIA_8380_0,
  546. .subvendor = PCI_ANY_ID,
  547. .subdevice = PCI_ANY_ID,
  548. },
  549. /* VIA K8M890 / K8N890 */
  550. {
  551. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  552. .class_mask = ~0,
  553. .vendor = PCI_VENDOR_ID_VIA,
  554. .device = PCI_DEVICE_ID_VIA_VT3336,
  555. .subvendor = PCI_ANY_ID,
  556. .subdevice = PCI_ANY_ID,
  557. },
  558. /* VIA K8T890 */
  559. {
  560. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  561. .class_mask = ~0,
  562. .vendor = PCI_VENDOR_ID_VIA,
  563. .device = PCI_DEVICE_ID_VIA_3238_0,
  564. .subvendor = PCI_ANY_ID,
  565. .subdevice = PCI_ANY_ID,
  566. },
  567. /* VIA K8T800/K8M800/K8N800 */
  568. {
  569. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  570. .class_mask = ~0,
  571. .vendor = PCI_VENDOR_ID_VIA,
  572. .device = PCI_DEVICE_ID_VIA_838X_1,
  573. .subvendor = PCI_ANY_ID,
  574. .subdevice = PCI_ANY_ID,
  575. },
  576. /* NForce3 */
  577. {
  578. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  579. .class_mask = ~0,
  580. .vendor = PCI_VENDOR_ID_NVIDIA,
  581. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3,
  582. .subvendor = PCI_ANY_ID,
  583. .subdevice = PCI_ANY_ID,
  584. },
  585. {
  586. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  587. .class_mask = ~0,
  588. .vendor = PCI_VENDOR_ID_NVIDIA,
  589. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3S,
  590. .subvendor = PCI_ANY_ID,
  591. .subdevice = PCI_ANY_ID,
  592. },
  593. /* SIS 755 */
  594. {
  595. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  596. .class_mask = ~0,
  597. .vendor = PCI_VENDOR_ID_SI,
  598. .device = PCI_DEVICE_ID_SI_755,
  599. .subvendor = PCI_ANY_ID,
  600. .subdevice = PCI_ANY_ID,
  601. },
  602. /* SIS 760 */
  603. {
  604. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  605. .class_mask = ~0,
  606. .vendor = PCI_VENDOR_ID_SI,
  607. .device = PCI_DEVICE_ID_SI_760,
  608. .subvendor = PCI_ANY_ID,
  609. .subdevice = PCI_ANY_ID,
  610. },
  611. /* ALI/ULI M1695 */
  612. {
  613. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  614. .class_mask = ~0,
  615. .vendor = PCI_VENDOR_ID_AL,
  616. .device = 0x1695,
  617. .subvendor = PCI_ANY_ID,
  618. .subdevice = PCI_ANY_ID,
  619. },
  620. { }
  621. };
  622. MODULE_DEVICE_TABLE(pci, agp_amd64_pci_table);
  623. static struct pci_driver agp_amd64_pci_driver = {
  624. .name = "agpgart-amd64",
  625. .id_table = agp_amd64_pci_table,
  626. .probe = agp_amd64_probe,
  627. .remove = agp_amd64_remove,
  628. #ifdef CONFIG_PM
  629. .suspend = agp_amd64_suspend,
  630. .resume = agp_amd64_resume,
  631. #endif
  632. };
  633. /* Not static due to IOMMU code calling it early. */
  634. int __init agp_amd64_init(void)
  635. {
  636. int err = 0;
  637. if (agp_off)
  638. return -EINVAL;
  639. if (pci_register_driver(&agp_amd64_pci_driver) < 0) {
  640. struct pci_dev *dev;
  641. if (!agp_try_unsupported && !agp_try_unsupported_boot) {
  642. printk(KERN_INFO PFX "No supported AGP bridge found.\n");
  643. #ifdef MODULE
  644. printk(KERN_INFO PFX "You can try agp_try_unsupported=1\n");
  645. #else
  646. printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n");
  647. #endif
  648. return -ENODEV;
  649. }
  650. /* First check that we have at least one AMD64 NB */
  651. if (!pci_dev_present(k8_nb_ids))
  652. return -ENODEV;
  653. /* Look for any AGP bridge */
  654. dev = NULL;
  655. err = -ENODEV;
  656. for_each_pci_dev(dev) {
  657. if (!pci_find_capability(dev, PCI_CAP_ID_AGP))
  658. continue;
  659. /* Only one bridge supported right now */
  660. if (agp_amd64_probe(dev, NULL) == 0) {
  661. err = 0;
  662. break;
  663. }
  664. }
  665. }
  666. return err;
  667. }
  668. static void __exit agp_amd64_cleanup(void)
  669. {
  670. if (aperture_resource)
  671. release_resource(aperture_resource);
  672. pci_unregister_driver(&agp_amd64_pci_driver);
  673. }
  674. /* On AMD64 the PCI driver needs to initialize this driver early
  675. for the IOMMU, so it has to be called via a backdoor. */
  676. #ifndef CONFIG_IOMMU
  677. module_init(agp_amd64_init);
  678. module_exit(agp_amd64_cleanup);
  679. #endif
  680. MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>, Andi Kleen");
  681. module_param(agp_try_unsupported, bool, 0);
  682. MODULE_LICENSE("GPL");