amd64-agp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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/config.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/init.h>
  14. #include <linux/agp_backend.h>
  15. #include <linux/mmzone.h>
  16. #include <asm/page.h> /* PAGE_SIZE */
  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. u32 pte;
  57. num_entries = agp_num_entries();
  58. if (type != 0 || mem->type != 0)
  59. return -EINVAL;
  60. /* Make sure we can fit the range in the gatt table. */
  61. /* FIXME: could wrap */
  62. if (((unsigned long)pg_start + mem->page_count) > num_entries)
  63. return -EINVAL;
  64. j = pg_start;
  65. /* gatt table should be empty. */
  66. while (j < (pg_start + mem->page_count)) {
  67. if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j)))
  68. return -EBUSY;
  69. j++;
  70. }
  71. if (mem->is_flushed == FALSE) {
  72. global_cache_flush();
  73. mem->is_flushed = TRUE;
  74. }
  75. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  76. tmp = agp_bridge->driver->mask_memory(agp_bridge,
  77. mem->memory[i], mem->type);
  78. BUG_ON(tmp & 0xffffff0000000ffcULL);
  79. pte = (tmp & 0x000000ff00000000ULL) >> 28;
  80. pte |=(tmp & 0x00000000fffff000ULL);
  81. pte |= GPTE_VALID | GPTE_COHERENT;
  82. writel(pte, agp_bridge->gatt_table+j);
  83. readl(agp_bridge->gatt_table+j); /* PCI Posting. */
  84. }
  85. amd64_tlbflush(mem);
  86. return 0;
  87. }
  88. /*
  89. * This hack alters the order element according
  90. * to the size of a long. It sucks. I totally disown this, even
  91. * though it does appear to work for the most part.
  92. */
  93. static struct aper_size_info_32 amd64_aperture_sizes[7] =
  94. {
  95. {32, 8192, 3+(sizeof(long)/8), 0 },
  96. {64, 16384, 4+(sizeof(long)/8), 1<<1 },
  97. {128, 32768, 5+(sizeof(long)/8), 1<<2 },
  98. {256, 65536, 6+(sizeof(long)/8), 1<<1 | 1<<2 },
  99. {512, 131072, 7+(sizeof(long)/8), 1<<3 },
  100. {1024, 262144, 8+(sizeof(long)/8), 1<<1 | 1<<3},
  101. {2048, 524288, 9+(sizeof(long)/8), 1<<2 | 1<<3}
  102. };
  103. /*
  104. * Get the current Aperture size from the x86-64.
  105. * Note, that there may be multiple x86-64's, but we just return
  106. * the value from the first one we find. The set_size functions
  107. * keep the rest coherent anyway. Or at least should do.
  108. */
  109. static int amd64_fetch_size(void)
  110. {
  111. struct pci_dev *dev;
  112. int i;
  113. u32 temp;
  114. struct aper_size_info_32 *values;
  115. dev = k8_northbridges[0];
  116. if (dev==NULL)
  117. return 0;
  118. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &temp);
  119. temp = (temp & 0xe);
  120. values = A_SIZE_32(amd64_aperture_sizes);
  121. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  122. if (temp == values[i].size_value) {
  123. agp_bridge->previous_size =
  124. agp_bridge->current_size = (void *) (values + i);
  125. agp_bridge->aperture_size_idx = i;
  126. return values[i].size;
  127. }
  128. }
  129. return 0;
  130. }
  131. /*
  132. * In a multiprocessor x86-64 system, this function gets
  133. * called once for each CPU.
  134. */
  135. static u64 amd64_configure (struct pci_dev *hammer, u64 gatt_table)
  136. {
  137. u64 aperturebase;
  138. u32 tmp;
  139. u64 addr, aper_base;
  140. /* Address to map to */
  141. pci_read_config_dword (hammer, AMD64_GARTAPERTUREBASE, &tmp);
  142. aperturebase = tmp << 25;
  143. aper_base = (aperturebase & PCI_BASE_ADDRESS_MEM_MASK);
  144. /* address of the mappings table */
  145. addr = (u64) gatt_table;
  146. addr >>= 12;
  147. tmp = (u32) addr<<4;
  148. tmp &= ~0xf;
  149. pci_write_config_dword (hammer, AMD64_GARTTABLEBASE, tmp);
  150. /* Enable GART translation for this hammer. */
  151. pci_read_config_dword(hammer, AMD64_GARTAPERTURECTL, &tmp);
  152. tmp |= GARTEN;
  153. tmp &= ~(DISGARTCPU | DISGARTIO);
  154. pci_write_config_dword(hammer, AMD64_GARTAPERTURECTL, tmp);
  155. return aper_base;
  156. }
  157. static struct aper_size_info_32 amd_8151_sizes[7] =
  158. {
  159. {2048, 524288, 9, 0x00000000 }, /* 0 0 0 0 0 0 */
  160. {1024, 262144, 8, 0x00000400 }, /* 1 0 0 0 0 0 */
  161. {512, 131072, 7, 0x00000600 }, /* 1 1 0 0 0 0 */
  162. {256, 65536, 6, 0x00000700 }, /* 1 1 1 0 0 0 */
  163. {128, 32768, 5, 0x00000720 }, /* 1 1 1 1 0 0 */
  164. {64, 16384, 4, 0x00000730 }, /* 1 1 1 1 1 0 */
  165. {32, 8192, 3, 0x00000738 } /* 1 1 1 1 1 1 */
  166. };
  167. static int amd_8151_configure(void)
  168. {
  169. unsigned long gatt_bus = virt_to_gart(agp_bridge->gatt_table_real);
  170. int i;
  171. /* Configure AGP regs in each x86-64 host bridge. */
  172. for (i = 0; i < num_k8_northbridges; i++) {
  173. agp_bridge->gart_bus_addr =
  174. amd64_configure(k8_northbridges[i], gatt_bus);
  175. }
  176. k8_flush_garts();
  177. return 0;
  178. }
  179. static void amd64_cleanup(void)
  180. {
  181. u32 tmp;
  182. int i;
  183. for (i = 0; i < num_k8_northbridges; i++) {
  184. struct pci_dev *dev = k8_northbridges[i];
  185. /* disable gart translation */
  186. pci_read_config_dword (dev, AMD64_GARTAPERTURECTL, &tmp);
  187. tmp &= ~AMD64_GARTEN;
  188. pci_write_config_dword (dev, AMD64_GARTAPERTURECTL, tmp);
  189. }
  190. }
  191. static struct agp_bridge_driver amd_8151_driver = {
  192. .owner = THIS_MODULE,
  193. .aperture_sizes = amd_8151_sizes,
  194. .size_type = U32_APER_SIZE,
  195. .num_aperture_sizes = 7,
  196. .configure = amd_8151_configure,
  197. .fetch_size = amd64_fetch_size,
  198. .cleanup = amd64_cleanup,
  199. .tlb_flush = amd64_tlbflush,
  200. .mask_memory = agp_generic_mask_memory,
  201. .masks = NULL,
  202. .agp_enable = agp_generic_enable,
  203. .cache_flush = global_cache_flush,
  204. .create_gatt_table = agp_generic_create_gatt_table,
  205. .free_gatt_table = agp_generic_free_gatt_table,
  206. .insert_memory = amd64_insert_memory,
  207. .remove_memory = agp_generic_remove_memory,
  208. .alloc_by_type = agp_generic_alloc_by_type,
  209. .free_by_type = agp_generic_free_by_type,
  210. .agp_alloc_page = agp_generic_alloc_page,
  211. .agp_destroy_page = agp_generic_destroy_page,
  212. };
  213. /* Some basic sanity checks for the aperture. */
  214. static int __devinit aperture_valid(u64 aper, u32 size)
  215. {
  216. u32 pfn, c;
  217. if (aper == 0) {
  218. printk(KERN_ERR PFX "No aperture\n");
  219. return 0;
  220. }
  221. if (size < 32*1024*1024) {
  222. printk(KERN_ERR PFX "Aperture too small (%d MB)\n", size>>20);
  223. return 0;
  224. }
  225. if (aper + size > 0xffffffff) {
  226. printk(KERN_ERR PFX "Aperture out of bounds\n");
  227. return 0;
  228. }
  229. pfn = aper >> PAGE_SHIFT;
  230. for (c = 0; c < size/PAGE_SIZE; c++) {
  231. if (!pfn_valid(pfn + c))
  232. break;
  233. if (!PageReserved(pfn_to_page(pfn + c))) {
  234. printk(KERN_ERR PFX "Aperture pointing to RAM\n");
  235. return 0;
  236. }
  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. u8 rev_id;
  316. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  317. switch (rev_id) {
  318. case 0x01: revstring="A0"; break;
  319. case 0x02: revstring="A1"; break;
  320. case 0x11: revstring="B0"; break;
  321. case 0x12: revstring="B1"; break;
  322. case 0x13: revstring="B2"; break;
  323. case 0x14: revstring="B3"; break;
  324. default: revstring="??"; break;
  325. }
  326. printk (KERN_INFO PFX "Detected AMD 8151 AGP Bridge rev %s\n", revstring);
  327. /*
  328. * Work around errata.
  329. * Chips before B2 stepping incorrectly reporting v3.5
  330. */
  331. if (rev_id < 0x13) {
  332. printk (KERN_INFO PFX "Correcting AGP revision (reports 3.5, is really 3.0)\n");
  333. bridge->major_version = 3;
  334. bridge->minor_version = 0;
  335. }
  336. }
  337. static const struct aper_size_info_32 uli_sizes[7] =
  338. {
  339. {256, 65536, 6, 10},
  340. {128, 32768, 5, 9},
  341. {64, 16384, 4, 8},
  342. {32, 8192, 3, 7},
  343. {16, 4096, 2, 6},
  344. {8, 2048, 1, 4},
  345. {4, 1024, 0, 3}
  346. };
  347. static int __devinit uli_agp_init(struct pci_dev *pdev)
  348. {
  349. u32 httfea,baseaddr,enuscr;
  350. struct pci_dev *dev1;
  351. int i;
  352. unsigned size = amd64_fetch_size();
  353. printk(KERN_INFO "Setting up ULi AGP.\n");
  354. dev1 = pci_find_slot ((unsigned int)pdev->bus->number,PCI_DEVFN(0,0));
  355. if (dev1 == NULL) {
  356. printk(KERN_INFO PFX "Detected a ULi chipset, "
  357. "but could not fine the secondary device.\n");
  358. return -ENODEV;
  359. }
  360. for (i = 0; i < ARRAY_SIZE(uli_sizes); i++)
  361. if (uli_sizes[i].size == size)
  362. break;
  363. if (i == ARRAY_SIZE(uli_sizes)) {
  364. printk(KERN_INFO PFX "No ULi size found for %d\n", size);
  365. return -ENODEV;
  366. }
  367. /* shadow x86-64 registers into ULi registers */
  368. pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &httfea);
  369. /* if x86-64 aperture base is beyond 4G, exit here */
  370. if ((httfea & 0x7fff) >> (32 - 25))
  371. return -ENODEV;
  372. httfea = (httfea& 0x7fff) << 25;
  373. pci_read_config_dword(pdev, ULI_X86_64_BASE_ADDR, &baseaddr);
  374. baseaddr&= ~PCI_BASE_ADDRESS_MEM_MASK;
  375. baseaddr|= httfea;
  376. pci_write_config_dword(pdev, ULI_X86_64_BASE_ADDR, baseaddr);
  377. enuscr= httfea+ (size * 1024 * 1024) - 1;
  378. pci_write_config_dword(dev1, ULI_X86_64_HTT_FEA_REG, httfea);
  379. pci_write_config_dword(dev1, ULI_X86_64_ENU_SCR_REG, enuscr);
  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_find_slot((unsigned int)pdev->bus->number, 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. 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 K8T890 */
  550. {
  551. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  552. .class_mask = ~0,
  553. .vendor = PCI_VENDOR_ID_VIA,
  554. .device = PCI_DEVICE_ID_VIA_3238_0,
  555. .subvendor = PCI_ANY_ID,
  556. .subdevice = PCI_ANY_ID,
  557. },
  558. /* VIA K8T800/K8M800/K8N800 */
  559. {
  560. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  561. .class_mask = ~0,
  562. .vendor = PCI_VENDOR_ID_VIA,
  563. .device = PCI_DEVICE_ID_VIA_838X_1,
  564. .subvendor = PCI_ANY_ID,
  565. .subdevice = PCI_ANY_ID,
  566. },
  567. /* NForce3 */
  568. {
  569. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  570. .class_mask = ~0,
  571. .vendor = PCI_VENDOR_ID_NVIDIA,
  572. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3,
  573. .subvendor = PCI_ANY_ID,
  574. .subdevice = PCI_ANY_ID,
  575. },
  576. {
  577. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  578. .class_mask = ~0,
  579. .vendor = PCI_VENDOR_ID_NVIDIA,
  580. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3S,
  581. .subvendor = PCI_ANY_ID,
  582. .subdevice = PCI_ANY_ID,
  583. },
  584. /* SIS 755 */
  585. {
  586. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  587. .class_mask = ~0,
  588. .vendor = PCI_VENDOR_ID_SI,
  589. .device = PCI_DEVICE_ID_SI_755,
  590. .subvendor = PCI_ANY_ID,
  591. .subdevice = PCI_ANY_ID,
  592. },
  593. /* SIS 760 */
  594. {
  595. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  596. .class_mask = ~0,
  597. .vendor = PCI_VENDOR_ID_SI,
  598. .device = PCI_DEVICE_ID_SI_760,
  599. .subvendor = PCI_ANY_ID,
  600. .subdevice = PCI_ANY_ID,
  601. },
  602. /* ALI/ULI M1695 */
  603. {
  604. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  605. .class_mask = ~0,
  606. .vendor = PCI_VENDOR_ID_AL,
  607. .device = 0x1695,
  608. .subvendor = PCI_ANY_ID,
  609. .subdevice = PCI_ANY_ID,
  610. },
  611. { }
  612. };
  613. MODULE_DEVICE_TABLE(pci, agp_amd64_pci_table);
  614. static struct pci_driver agp_amd64_pci_driver = {
  615. .name = "agpgart-amd64",
  616. .id_table = agp_amd64_pci_table,
  617. .probe = agp_amd64_probe,
  618. .remove = agp_amd64_remove,
  619. #ifdef CONFIG_PM
  620. .suspend = agp_amd64_suspend,
  621. .resume = agp_amd64_resume,
  622. #endif
  623. };
  624. /* Not static due to IOMMU code calling it early. */
  625. int __init agp_amd64_init(void)
  626. {
  627. int err = 0;
  628. if (agp_off)
  629. return -EINVAL;
  630. if (pci_register_driver(&agp_amd64_pci_driver) > 0) {
  631. struct pci_dev *dev;
  632. if (!agp_try_unsupported && !agp_try_unsupported_boot) {
  633. printk(KERN_INFO PFX "No supported AGP bridge found.\n");
  634. #ifdef MODULE
  635. printk(KERN_INFO PFX "You can try agp_try_unsupported=1\n");
  636. #else
  637. printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n");
  638. #endif
  639. return -ENODEV;
  640. }
  641. /* First check that we have at least one AMD64 NB */
  642. if (!pci_dev_present(k8_nb_ids))
  643. return -ENODEV;
  644. /* Look for any AGP bridge */
  645. dev = NULL;
  646. err = -ENODEV;
  647. for_each_pci_dev(dev) {
  648. if (!pci_find_capability(dev, PCI_CAP_ID_AGP))
  649. continue;
  650. /* Only one bridge supported right now */
  651. if (agp_amd64_probe(dev, NULL) == 0) {
  652. err = 0;
  653. break;
  654. }
  655. }
  656. }
  657. return err;
  658. }
  659. static void __exit agp_amd64_cleanup(void)
  660. {
  661. if (aperture_resource)
  662. release_resource(aperture_resource);
  663. pci_unregister_driver(&agp_amd64_pci_driver);
  664. }
  665. /* On AMD64 the PCI driver needs to initialize this driver early
  666. for the IOMMU, so it has to be called via a backdoor. */
  667. #ifndef CONFIG_IOMMU
  668. module_init(agp_amd64_init);
  669. module_exit(agp_amd64_cleanup);
  670. #endif
  671. MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>, Andi Kleen");
  672. module_param(agp_try_unsupported, bool, 0);
  673. MODULE_LICENSE("GPL");