amd64-agp.c 20 KB

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