amd64-agp.c 20 KB

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