amd64-agp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 = 1;
  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. int gart_iterator;
  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. int gart_iterator;
  186. /* Configure AGP regs in each x86-64 host bridge. */
  187. for_each_nb() {
  188. agp_bridge->gart_bus_addr =
  189. amd64_configure(hammers[gart_iterator],gatt_bus);
  190. }
  191. return 0;
  192. }
  193. static void amd64_cleanup(void)
  194. {
  195. u32 tmp;
  196. int gart_iterator;
  197. for_each_nb() {
  198. /* disable gart translation */
  199. pci_read_config_dword (hammers[gart_iterator], AMD64_GARTAPERTURECTL, &tmp);
  200. tmp &= ~AMD64_GARTEN;
  201. pci_write_config_dword (hammers[gart_iterator], AMD64_GARTAPERTURECTL, tmp);
  202. }
  203. }
  204. static struct agp_bridge_driver amd_8151_driver = {
  205. .owner = THIS_MODULE,
  206. .aperture_sizes = amd_8151_sizes,
  207. .size_type = U32_APER_SIZE,
  208. .num_aperture_sizes = 7,
  209. .configure = amd_8151_configure,
  210. .fetch_size = amd64_fetch_size,
  211. .cleanup = amd64_cleanup,
  212. .tlb_flush = amd64_tlbflush,
  213. .mask_memory = agp_generic_mask_memory,
  214. .masks = NULL,
  215. .agp_enable = agp_generic_enable,
  216. .cache_flush = global_cache_flush,
  217. .create_gatt_table = agp_generic_create_gatt_table,
  218. .free_gatt_table = agp_generic_free_gatt_table,
  219. .insert_memory = amd64_insert_memory,
  220. .remove_memory = agp_generic_remove_memory,
  221. .alloc_by_type = agp_generic_alloc_by_type,
  222. .free_by_type = agp_generic_free_by_type,
  223. .agp_alloc_page = agp_generic_alloc_page,
  224. .agp_destroy_page = agp_generic_destroy_page,
  225. };
  226. /* Some basic sanity checks for the aperture. */
  227. static int __devinit aperture_valid(u64 aper, u32 size)
  228. {
  229. u32 pfn, c;
  230. if (aper == 0) {
  231. printk(KERN_ERR PFX "No aperture\n");
  232. return 0;
  233. }
  234. if (size < 32*1024*1024) {
  235. printk(KERN_ERR PFX "Aperture too small (%d MB)\n", size>>20);
  236. return 0;
  237. }
  238. if (aper + size > 0xffffffff) {
  239. printk(KERN_ERR PFX "Aperture out of bounds\n");
  240. return 0;
  241. }
  242. pfn = aper >> PAGE_SHIFT;
  243. for (c = 0; c < size/PAGE_SIZE; c++) {
  244. if (!pfn_valid(pfn + c))
  245. break;
  246. if (!PageReserved(pfn_to_page(pfn + c))) {
  247. printk(KERN_ERR PFX "Aperture pointing to RAM\n");
  248. return 0;
  249. }
  250. }
  251. /* Request the Aperture. This catches cases when someone else
  252. already put a mapping in there - happens with some very broken BIOS
  253. Maybe better to use pci_assign_resource/pci_enable_device instead
  254. trusting the bridges? */
  255. if (!aperture_resource &&
  256. !(aperture_resource = request_mem_region(aper, size, "aperture"))) {
  257. printk(KERN_ERR PFX "Aperture conflicts with PCI mapping.\n");
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. /*
  263. * W*s centric BIOS sometimes only set up the aperture in the AGP
  264. * bridge, not the northbridge. On AMD64 this is handled early
  265. * in aperture.c, but when GART_IOMMU is not enabled or we run
  266. * on a 32bit kernel this needs to be redone.
  267. * Unfortunately it is impossible to fix the aperture here because it's too late
  268. * to allocate that much memory. But at least error out cleanly instead of
  269. * crashing.
  270. */
  271. static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
  272. u16 cap)
  273. {
  274. u32 aper_low, aper_hi;
  275. u64 aper, nb_aper;
  276. int order = 0;
  277. u32 nb_order, nb_base;
  278. u16 apsize;
  279. pci_read_config_dword(nb, 0x90, &nb_order);
  280. nb_order = (nb_order >> 1) & 7;
  281. pci_read_config_dword(nb, 0x94, &nb_base);
  282. nb_aper = nb_base << 25;
  283. if (aperture_valid(nb_aper, (32*1024*1024)<<nb_order)) {
  284. return 0;
  285. }
  286. /* Northbridge seems to contain crap. Try the AGP bridge. */
  287. pci_read_config_word(agp, cap+0x14, &apsize);
  288. if (apsize == 0xffff)
  289. return -1;
  290. apsize &= 0xfff;
  291. /* Some BIOS use weird encodings not in the AGPv3 table. */
  292. if (apsize & 0xff)
  293. apsize |= 0xf00;
  294. order = 7 - hweight16(apsize);
  295. pci_read_config_dword(agp, 0x10, &aper_low);
  296. pci_read_config_dword(agp, 0x14, &aper_hi);
  297. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  298. printk(KERN_INFO PFX "Aperture from AGP @ %Lx size %u MB\n", aper, 32 << order);
  299. if (order < 0 || !aperture_valid(aper, (32*1024*1024)<<order))
  300. return -1;
  301. pci_write_config_dword(nb, 0x90, order << 1);
  302. pci_write_config_dword(nb, 0x94, aper >> 25);
  303. return 0;
  304. }
  305. static __devinit int cache_nbs (struct pci_dev *pdev, u32 cap_ptr)
  306. {
  307. struct pci_dev *loop_dev = NULL;
  308. int i = 0;
  309. /* cache pci_devs of northbridges. */
  310. while ((loop_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1103, loop_dev))
  311. != NULL) {
  312. if (i == MAX_HAMMER_GARTS) {
  313. printk(KERN_ERR PFX "Too many northbridges for AGP\n");
  314. return -1;
  315. }
  316. if (fix_northbridge(loop_dev, pdev, cap_ptr) < 0) {
  317. printk(KERN_ERR PFX "No usable aperture found.\n");
  318. #ifdef __x86_64__
  319. /* should port this to i386 */
  320. printk(KERN_ERR PFX "Consider rebooting with iommu=memaper=2 to get a good aperture.\n");
  321. #endif
  322. return -1;
  323. }
  324. hammers[i++] = loop_dev;
  325. }
  326. nr_garts = i;
  327. return i == 0 ? -1 : 0;
  328. }
  329. /* Handle AMD 8151 quirks */
  330. static void __devinit amd8151_init(struct pci_dev *pdev, struct agp_bridge_data *bridge)
  331. {
  332. char *revstring;
  333. u8 rev_id;
  334. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  335. switch (rev_id) {
  336. case 0x01: revstring="A0"; break;
  337. case 0x02: revstring="A1"; break;
  338. case 0x11: revstring="B0"; break;
  339. case 0x12: revstring="B1"; break;
  340. case 0x13: revstring="B2"; break;
  341. case 0x14: revstring="B3"; break;
  342. default: revstring="??"; break;
  343. }
  344. printk (KERN_INFO PFX "Detected AMD 8151 AGP Bridge rev %s\n", revstring);
  345. /*
  346. * Work around errata.
  347. * Chips before B2 stepping incorrectly reporting v3.5
  348. */
  349. if (rev_id < 0x13) {
  350. printk (KERN_INFO PFX "Correcting AGP revision (reports 3.5, is really 3.0)\n");
  351. bridge->major_version = 3;
  352. bridge->minor_version = 0;
  353. }
  354. }
  355. static const struct aper_size_info_32 uli_sizes[7] =
  356. {
  357. {256, 65536, 6, 10},
  358. {128, 32768, 5, 9},
  359. {64, 16384, 4, 8},
  360. {32, 8192, 3, 7},
  361. {16, 4096, 2, 6},
  362. {8, 2048, 1, 4},
  363. {4, 1024, 0, 3}
  364. };
  365. static int __devinit uli_agp_init(struct pci_dev *pdev)
  366. {
  367. u32 httfea,baseaddr,enuscr;
  368. struct pci_dev *dev1;
  369. int i;
  370. unsigned size = amd64_fetch_size();
  371. printk(KERN_INFO "Setting up ULi AGP.\n");
  372. dev1 = pci_find_slot ((unsigned int)pdev->bus->number,PCI_DEVFN(0,0));
  373. if (dev1 == NULL) {
  374. printk(KERN_INFO PFX "Detected a ULi chipset, "
  375. "but could not fine the secondary device.\n");
  376. return -ENODEV;
  377. }
  378. for (i = 0; i < ARRAY_SIZE(uli_sizes); i++)
  379. if (uli_sizes[i].size == size)
  380. break;
  381. if (i == ARRAY_SIZE(uli_sizes)) {
  382. printk(KERN_INFO PFX "No ULi size found for %d\n", size);
  383. return -ENODEV;
  384. }
  385. /* shadow x86-64 registers into ULi registers */
  386. pci_read_config_dword (hammers[0], AMD64_GARTAPERTUREBASE, &httfea);
  387. /* if x86-64 aperture base is beyond 4G, exit here */
  388. if ((httfea & 0x7fff) >> (32 - 25))
  389. return -ENODEV;
  390. httfea = (httfea& 0x7fff) << 25;
  391. pci_read_config_dword(pdev, ULI_X86_64_BASE_ADDR, &baseaddr);
  392. baseaddr&= ~PCI_BASE_ADDRESS_MEM_MASK;
  393. baseaddr|= httfea;
  394. pci_write_config_dword(pdev, ULI_X86_64_BASE_ADDR, baseaddr);
  395. enuscr= httfea+ (size * 1024 * 1024) - 1;
  396. pci_write_config_dword(dev1, ULI_X86_64_HTT_FEA_REG, httfea);
  397. pci_write_config_dword(dev1, ULI_X86_64_ENU_SCR_REG, enuscr);
  398. return 0;
  399. }
  400. static const struct aper_size_info_32 nforce3_sizes[5] =
  401. {
  402. {512, 131072, 7, 0x00000000 },
  403. {256, 65536, 6, 0x00000008 },
  404. {128, 32768, 5, 0x0000000C },
  405. {64, 16384, 4, 0x0000000E },
  406. {32, 8192, 3, 0x0000000F }
  407. };
  408. /* Handle shadow device of the Nvidia NForce3 */
  409. /* CHECK-ME original 2.4 version set up some IORRs. Check if that is needed. */
  410. static int __devinit nforce3_agp_init(struct pci_dev *pdev)
  411. {
  412. u32 tmp, apbase, apbar, aplimit;
  413. struct pci_dev *dev1;
  414. int i;
  415. unsigned size = amd64_fetch_size();
  416. printk(KERN_INFO PFX "Setting up Nforce3 AGP.\n");
  417. dev1 = pci_find_slot((unsigned int)pdev->bus->number, PCI_DEVFN(11, 0));
  418. if (dev1 == NULL) {
  419. printk(KERN_INFO PFX "agpgart: Detected an NVIDIA "
  420. "nForce3 chipset, but could not find "
  421. "the secondary device.\n");
  422. return -ENODEV;
  423. }
  424. for (i = 0; i < ARRAY_SIZE(nforce3_sizes); i++)
  425. if (nforce3_sizes[i].size == size)
  426. break;
  427. if (i == ARRAY_SIZE(nforce3_sizes)) {
  428. printk(KERN_INFO PFX "No NForce3 size found for %d\n", size);
  429. return -ENODEV;
  430. }
  431. pci_read_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, &tmp);
  432. tmp &= ~(0xf);
  433. tmp |= nforce3_sizes[i].size_value;
  434. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, tmp);
  435. /* shadow x86-64 registers into NVIDIA registers */
  436. pci_read_config_dword (hammers[0], AMD64_GARTAPERTUREBASE, &apbase);
  437. /* if x86-64 aperture base is beyond 4G, exit here */
  438. if ( (apbase & 0x7fff) >> (32 - 25) ) {
  439. printk(KERN_INFO PFX "aperture base > 4G\n");
  440. return -ENODEV;
  441. }
  442. apbase = (apbase & 0x7fff) << 25;
  443. pci_read_config_dword(pdev, NVIDIA_X86_64_0_APBASE, &apbar);
  444. apbar &= ~PCI_BASE_ADDRESS_MEM_MASK;
  445. apbar |= apbase;
  446. pci_write_config_dword(pdev, NVIDIA_X86_64_0_APBASE, apbar);
  447. aplimit = apbase + (size * 1024 * 1024) - 1;
  448. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE1, apbase);
  449. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT1, aplimit);
  450. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE2, apbase);
  451. pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT2, aplimit);
  452. return 0;
  453. }
  454. static int __devinit agp_amd64_probe(struct pci_dev *pdev,
  455. const struct pci_device_id *ent)
  456. {
  457. struct agp_bridge_data *bridge;
  458. u8 cap_ptr;
  459. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  460. if (!cap_ptr)
  461. return -ENODEV;
  462. /* Could check for AGPv3 here */
  463. bridge = agp_alloc_bridge();
  464. if (!bridge)
  465. return -ENOMEM;
  466. if (pdev->vendor == PCI_VENDOR_ID_AMD &&
  467. pdev->device == PCI_DEVICE_ID_AMD_8151_0) {
  468. amd8151_init(pdev, bridge);
  469. } else {
  470. printk(KERN_INFO PFX "Detected AGP bridge %x\n", pdev->devfn);
  471. }
  472. bridge->driver = &amd_8151_driver;
  473. bridge->dev = pdev;
  474. bridge->capndx = cap_ptr;
  475. /* Fill in the mode register */
  476. pci_read_config_dword(pdev, bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
  477. if (cache_nbs(pdev, cap_ptr) == -1) {
  478. agp_put_bridge(bridge);
  479. return -ENODEV;
  480. }
  481. if (pdev->vendor == PCI_VENDOR_ID_NVIDIA) {
  482. int ret = nforce3_agp_init(pdev);
  483. if (ret) {
  484. agp_put_bridge(bridge);
  485. return ret;
  486. }
  487. }
  488. if (pdev->vendor == PCI_VENDOR_ID_AL) {
  489. int ret = uli_agp_init(pdev);
  490. if (ret) {
  491. agp_put_bridge(bridge);
  492. return ret;
  493. }
  494. }
  495. pci_set_drvdata(pdev, bridge);
  496. return agp_add_bridge(bridge);
  497. }
  498. static void __devexit agp_amd64_remove(struct pci_dev *pdev)
  499. {
  500. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  501. release_mem_region(virt_to_gart(bridge->gatt_table_real),
  502. amd64_aperture_sizes[bridge->aperture_size_idx].size);
  503. agp_remove_bridge(bridge);
  504. agp_put_bridge(bridge);
  505. }
  506. #ifdef CONFIG_PM
  507. static int agp_amd64_suspend(struct pci_dev *pdev, pm_message_t state)
  508. {
  509. pci_save_state(pdev);
  510. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  511. return 0;
  512. }
  513. static int agp_amd64_resume(struct pci_dev *pdev)
  514. {
  515. pci_set_power_state(pdev, PCI_D0);
  516. pci_restore_state(pdev);
  517. return amd_8151_configure();
  518. }
  519. #endif /* CONFIG_PM */
  520. static struct pci_device_id agp_amd64_pci_table[] = {
  521. {
  522. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  523. .class_mask = ~0,
  524. .vendor = PCI_VENDOR_ID_AMD,
  525. .device = PCI_DEVICE_ID_AMD_8151_0,
  526. .subvendor = PCI_ANY_ID,
  527. .subdevice = PCI_ANY_ID,
  528. },
  529. /* ULi M1689 */
  530. {
  531. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  532. .class_mask = ~0,
  533. .vendor = PCI_VENDOR_ID_AL,
  534. .device = PCI_DEVICE_ID_AL_M1689,
  535. .subvendor = PCI_ANY_ID,
  536. .subdevice = PCI_ANY_ID,
  537. },
  538. /* VIA K8T800Pro */
  539. {
  540. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  541. .class_mask = ~0,
  542. .vendor = PCI_VENDOR_ID_VIA,
  543. .device = PCI_DEVICE_ID_VIA_K8T800PRO_0,
  544. .subvendor = PCI_ANY_ID,
  545. .subdevice = PCI_ANY_ID,
  546. },
  547. /* VIA K8T800 */
  548. {
  549. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  550. .class_mask = ~0,
  551. .vendor = PCI_VENDOR_ID_VIA,
  552. .device = PCI_DEVICE_ID_VIA_8385_0,
  553. .subvendor = PCI_ANY_ID,
  554. .subdevice = PCI_ANY_ID,
  555. },
  556. /* VIA 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_8380_0,
  562. .subvendor = PCI_ANY_ID,
  563. .subdevice = PCI_ANY_ID,
  564. },
  565. /* VIA K8T890 */
  566. {
  567. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  568. .class_mask = ~0,
  569. .vendor = PCI_VENDOR_ID_VIA,
  570. .device = PCI_DEVICE_ID_VIA_3238_0,
  571. .subvendor = PCI_ANY_ID,
  572. .subdevice = PCI_ANY_ID,
  573. },
  574. /* VIA K8T800/K8M800/K8N800 */
  575. {
  576. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  577. .class_mask = ~0,
  578. .vendor = PCI_VENDOR_ID_VIA,
  579. .device = PCI_DEVICE_ID_VIA_838X_1,
  580. .subvendor = PCI_ANY_ID,
  581. .subdevice = PCI_ANY_ID,
  582. },
  583. /* NForce3 */
  584. {
  585. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  586. .class_mask = ~0,
  587. .vendor = PCI_VENDOR_ID_NVIDIA,
  588. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3,
  589. .subvendor = PCI_ANY_ID,
  590. .subdevice = PCI_ANY_ID,
  591. },
  592. {
  593. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  594. .class_mask = ~0,
  595. .vendor = PCI_VENDOR_ID_NVIDIA,
  596. .device = PCI_DEVICE_ID_NVIDIA_NFORCE3S,
  597. .subvendor = PCI_ANY_ID,
  598. .subdevice = PCI_ANY_ID,
  599. },
  600. /* SIS 755 */
  601. {
  602. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  603. .class_mask = ~0,
  604. .vendor = PCI_VENDOR_ID_SI,
  605. .device = PCI_DEVICE_ID_SI_755,
  606. .subvendor = PCI_ANY_ID,
  607. .subdevice = PCI_ANY_ID,
  608. },
  609. /* SIS 760 */
  610. {
  611. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  612. .class_mask = ~0,
  613. .vendor = PCI_VENDOR_ID_SI,
  614. .device = PCI_DEVICE_ID_SI_760,
  615. .subvendor = PCI_ANY_ID,
  616. .subdevice = PCI_ANY_ID,
  617. },
  618. /* ALI/ULI M1695 */
  619. {
  620. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  621. .class_mask = ~0,
  622. .vendor = PCI_VENDOR_ID_AL,
  623. .device = 0x1695,
  624. .subvendor = PCI_ANY_ID,
  625. .subdevice = PCI_ANY_ID,
  626. },
  627. { }
  628. };
  629. MODULE_DEVICE_TABLE(pci, agp_amd64_pci_table);
  630. static struct pci_driver agp_amd64_pci_driver = {
  631. .name = "agpgart-amd64",
  632. .id_table = agp_amd64_pci_table,
  633. .probe = agp_amd64_probe,
  634. .remove = agp_amd64_remove,
  635. #ifdef CONFIG_PM
  636. .suspend = agp_amd64_suspend,
  637. .resume = agp_amd64_resume,
  638. #endif
  639. };
  640. /* Not static due to IOMMU code calling it early. */
  641. int __init agp_amd64_init(void)
  642. {
  643. int err = 0;
  644. static struct pci_device_id amd64nb[] = {
  645. { PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x1103) },
  646. { },
  647. };
  648. if (agp_off)
  649. return -EINVAL;
  650. if (pci_register_driver(&agp_amd64_pci_driver) > 0) {
  651. struct pci_dev *dev;
  652. if (!agp_try_unsupported && !agp_try_unsupported_boot) {
  653. printk(KERN_INFO PFX "No supported AGP bridge found.\n");
  654. #ifdef MODULE
  655. printk(KERN_INFO PFX "You can try agp_try_unsupported=1\n");
  656. #else
  657. printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n");
  658. #endif
  659. return -ENODEV;
  660. }
  661. /* First check that we have at least one AMD64 NB */
  662. if (!pci_dev_present(amd64nb))
  663. return -ENODEV;
  664. /* Look for any AGP bridge */
  665. dev = NULL;
  666. err = -ENODEV;
  667. for_each_pci_dev(dev) {
  668. if (!pci_find_capability(dev, PCI_CAP_ID_AGP))
  669. continue;
  670. /* Only one bridge supported right now */
  671. if (agp_amd64_probe(dev, NULL) == 0) {
  672. err = 0;
  673. break;
  674. }
  675. }
  676. }
  677. return err;
  678. }
  679. static void __exit agp_amd64_cleanup(void)
  680. {
  681. if (aperture_resource)
  682. release_resource(aperture_resource);
  683. pci_unregister_driver(&agp_amd64_pci_driver);
  684. }
  685. /* On AMD64 the PCI driver needs to initialize this driver early
  686. for the IOMMU, so it has to be called via a backdoor. */
  687. #ifndef CONFIG_GART_IOMMU
  688. module_init(agp_amd64_init);
  689. module_exit(agp_amd64_cleanup);
  690. #endif
  691. MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>, Andi Kleen");
  692. module_param(agp_try_unsupported, bool, 0);
  693. MODULE_LICENSE("GPL");