amd64-agp.c 20 KB

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