efficeon-agp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Transmeta's Efficeon AGPGART driver.
  3. *
  4. * Based upon a diff by Linus around November '02.
  5. *
  6. * Ported to the 2.6 kernel by Carlos Puchol <cpglinux@puchol.com>
  7. * and H. Peter Anvin <hpa@transmeta.com>.
  8. */
  9. /*
  10. * NOTE-cpg-040217:
  11. *
  12. * - when compiled as a module, after loading the module,
  13. * it will refuse to unload, indicating it is in use,
  14. * when it is not.
  15. * - no s3 (suspend to ram) testing.
  16. * - tested on the efficeon integrated nothbridge for tens
  17. * of iterations of starting x and glxgears.
  18. * - tested with radeon 9000 and radeon mobility m9 cards
  19. * - tested with c3/c4 enabled (with the mobility m9 card)
  20. */
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/agp_backend.h>
  25. #include <linux/gfp.h>
  26. #include <linux/page-flags.h>
  27. #include <linux/mm.h>
  28. #include "agp.h"
  29. #include "intel-agp.h"
  30. /*
  31. * The real differences to the generic AGP code is
  32. * in the GART mappings - a two-level setup with the
  33. * first level being an on-chip 64-entry table.
  34. *
  35. * The page array is filled through the ATTPAGE register
  36. * (Aperture Translation Table Page Register) at 0xB8. Bits:
  37. * 31:20: physical page address
  38. * 11:9: Page Attribute Table Index (PATI)
  39. * must match the PAT index for the
  40. * mapped pages (the 2nd level page table pages
  41. * themselves should be just regular WB-cacheable,
  42. * so this is normally zero.)
  43. * 8: Present
  44. * 7:6: reserved, write as zero
  45. * 5:0: GATT directory index: which 1st-level entry
  46. *
  47. * The Efficeon AGP spec requires pages to be WB-cacheable
  48. * but to be explicitly CLFLUSH'd after any changes.
  49. */
  50. #define EFFICEON_ATTPAGE 0xb8
  51. #define EFFICEON_L1_SIZE 64 /* Number of PDE pages */
  52. #define EFFICEON_PATI (0 << 9)
  53. #define EFFICEON_PRESENT (1 << 8)
  54. static struct _efficeon_private {
  55. unsigned long l1_table[EFFICEON_L1_SIZE];
  56. } efficeon_private;
  57. static const struct gatt_mask efficeon_generic_masks[] =
  58. {
  59. {.mask = 0x00000001, .type = 0}
  60. };
  61. /* This function does the same thing as mask_memory() for this chipset... */
  62. static inline unsigned long efficeon_mask_memory(struct page *page)
  63. {
  64. unsigned long addr = page_to_phys(page);
  65. return addr | 0x00000001;
  66. }
  67. static const struct aper_size_info_lvl2 efficeon_generic_sizes[4] =
  68. {
  69. {256, 65536, 0},
  70. {128, 32768, 32},
  71. {64, 16384, 48},
  72. {32, 8192, 56}
  73. };
  74. /*
  75. * Control interfaces are largely identical to
  76. * the legacy Intel 440BX..
  77. */
  78. static int efficeon_fetch_size(void)
  79. {
  80. int i;
  81. u16 temp;
  82. struct aper_size_info_lvl2 *values;
  83. pci_read_config_word(agp_bridge->dev, INTEL_APSIZE, &temp);
  84. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  85. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  86. if (temp == values[i].size_value) {
  87. agp_bridge->previous_size =
  88. agp_bridge->current_size = (void *) (values + i);
  89. agp_bridge->aperture_size_idx = i;
  90. return values[i].size;
  91. }
  92. }
  93. return 0;
  94. }
  95. static void efficeon_tlbflush(struct agp_memory * mem)
  96. {
  97. printk(KERN_DEBUG PFX "efficeon_tlbflush()\n");
  98. pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, 0x2200);
  99. pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, 0x2280);
  100. }
  101. static void efficeon_cleanup(void)
  102. {
  103. u16 temp;
  104. struct aper_size_info_lvl2 *previous_size;
  105. printk(KERN_DEBUG PFX "efficeon_cleanup()\n");
  106. previous_size = A_SIZE_LVL2(agp_bridge->previous_size);
  107. pci_read_config_word(agp_bridge->dev, INTEL_NBXCFG, &temp);
  108. pci_write_config_word(agp_bridge->dev, INTEL_NBXCFG, temp & ~(1 << 9));
  109. pci_write_config_word(agp_bridge->dev, INTEL_APSIZE,
  110. previous_size->size_value);
  111. }
  112. static int efficeon_configure(void)
  113. {
  114. u32 temp;
  115. u16 temp2;
  116. struct aper_size_info_lvl2 *current_size;
  117. printk(KERN_DEBUG PFX "efficeon_configure()\n");
  118. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  119. /* aperture size */
  120. pci_write_config_word(agp_bridge->dev, INTEL_APSIZE,
  121. current_size->size_value);
  122. /* address to map to */
  123. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  124. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  125. /* agpctrl */
  126. pci_write_config_dword(agp_bridge->dev, INTEL_AGPCTRL, 0x2280);
  127. /* paccfg/nbxcfg */
  128. pci_read_config_word(agp_bridge->dev, INTEL_NBXCFG, &temp2);
  129. pci_write_config_word(agp_bridge->dev, INTEL_NBXCFG,
  130. (temp2 & ~(1 << 10)) | (1 << 9) | (1 << 11));
  131. /* clear any possible error conditions */
  132. pci_write_config_byte(agp_bridge->dev, INTEL_ERRSTS + 1, 7);
  133. return 0;
  134. }
  135. static int efficeon_free_gatt_table(struct agp_bridge_data *bridge)
  136. {
  137. int index, freed = 0;
  138. for (index = 0; index < EFFICEON_L1_SIZE; index++) {
  139. unsigned long page = efficeon_private.l1_table[index];
  140. if (page) {
  141. efficeon_private.l1_table[index] = 0;
  142. ClearPageReserved(virt_to_page((char *)page));
  143. free_page(page);
  144. freed++;
  145. }
  146. printk(KERN_DEBUG PFX "efficeon_free_gatt_table(%p, %02x, %08x)\n",
  147. agp_bridge->dev, EFFICEON_ATTPAGE, index);
  148. pci_write_config_dword(agp_bridge->dev,
  149. EFFICEON_ATTPAGE, index);
  150. }
  151. printk(KERN_DEBUG PFX "efficeon_free_gatt_table() freed %d pages\n", freed);
  152. return 0;
  153. }
  154. /*
  155. * Since we don't need contiguous memory we just try
  156. * to get the gatt table once
  157. */
  158. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  159. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  160. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  161. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  162. #undef GET_GATT
  163. #define GET_GATT(addr) (efficeon_private.gatt_pages[\
  164. GET_PAGE_DIR_IDX(addr)]->remapped)
  165. static int efficeon_create_gatt_table(struct agp_bridge_data *bridge)
  166. {
  167. int index;
  168. const int pati = EFFICEON_PATI;
  169. const int present = EFFICEON_PRESENT;
  170. const int clflush_chunk = ((cpuid_ebx(1) >> 8) & 0xff) << 3;
  171. int num_entries, l1_pages;
  172. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  173. printk(KERN_DEBUG PFX "efficeon_create_gatt_table(%d)\n", num_entries);
  174. /* There are 2^10 PTE pages per PDE page */
  175. BUG_ON(num_entries & 0x3ff);
  176. l1_pages = num_entries >> 10;
  177. for (index = 0 ; index < l1_pages ; index++) {
  178. int offset;
  179. unsigned long page;
  180. unsigned long value;
  181. page = efficeon_private.l1_table[index];
  182. BUG_ON(page);
  183. page = get_zeroed_page(GFP_KERNEL);
  184. if (!page) {
  185. efficeon_free_gatt_table(agp_bridge);
  186. return -ENOMEM;
  187. }
  188. SetPageReserved(virt_to_page((char *)page));
  189. for (offset = 0; offset < PAGE_SIZE; offset += clflush_chunk)
  190. clflush((char *)page+offset);
  191. efficeon_private.l1_table[index] = page;
  192. value = virt_to_phys((unsigned long *)page) | pati | present | index;
  193. pci_write_config_dword(agp_bridge->dev,
  194. EFFICEON_ATTPAGE, value);
  195. }
  196. return 0;
  197. }
  198. static int efficeon_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
  199. {
  200. int i, count = mem->page_count, num_entries;
  201. unsigned int *page, *last_page;
  202. const int clflush_chunk = ((cpuid_ebx(1) >> 8) & 0xff) << 3;
  203. const unsigned long clflush_mask = ~(clflush_chunk-1);
  204. printk(KERN_DEBUG PFX "efficeon_insert_memory(%lx, %d)\n", pg_start, count);
  205. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  206. if ((pg_start + mem->page_count) > num_entries)
  207. return -EINVAL;
  208. if (type != 0 || mem->type != 0)
  209. return -EINVAL;
  210. if (!mem->is_flushed) {
  211. global_cache_flush();
  212. mem->is_flushed = true;
  213. }
  214. last_page = NULL;
  215. for (i = 0; i < count; i++) {
  216. int index = pg_start + i;
  217. unsigned long insert = efficeon_mask_memory(mem->pages[i]);
  218. page = (unsigned int *) efficeon_private.l1_table[index >> 10];
  219. if (!page)
  220. continue;
  221. page += (index & 0x3ff);
  222. *page = insert;
  223. /* clflush is slow, so don't clflush until we have to */
  224. if (last_page &&
  225. (((unsigned long)page^(unsigned long)last_page) &
  226. clflush_mask))
  227. clflush(last_page);
  228. last_page = page;
  229. }
  230. if ( last_page )
  231. clflush(last_page);
  232. agp_bridge->driver->tlb_flush(mem);
  233. return 0;
  234. }
  235. static int efficeon_remove_memory(struct agp_memory * mem, off_t pg_start, int type)
  236. {
  237. int i, count = mem->page_count, num_entries;
  238. printk(KERN_DEBUG PFX "efficeon_remove_memory(%lx, %d)\n", pg_start, count);
  239. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  240. if ((pg_start + mem->page_count) > num_entries)
  241. return -EINVAL;
  242. if (type != 0 || mem->type != 0)
  243. return -EINVAL;
  244. for (i = 0; i < count; i++) {
  245. int index = pg_start + i;
  246. unsigned int *page = (unsigned int *) efficeon_private.l1_table[index >> 10];
  247. if (!page)
  248. continue;
  249. page += (index & 0x3ff);
  250. *page = 0;
  251. }
  252. agp_bridge->driver->tlb_flush(mem);
  253. return 0;
  254. }
  255. static const struct agp_bridge_driver efficeon_driver = {
  256. .owner = THIS_MODULE,
  257. .aperture_sizes = efficeon_generic_sizes,
  258. .size_type = LVL2_APER_SIZE,
  259. .num_aperture_sizes = 4,
  260. .configure = efficeon_configure,
  261. .fetch_size = efficeon_fetch_size,
  262. .cleanup = efficeon_cleanup,
  263. .tlb_flush = efficeon_tlbflush,
  264. .mask_memory = agp_generic_mask_memory,
  265. .masks = efficeon_generic_masks,
  266. .agp_enable = agp_generic_enable,
  267. .cache_flush = global_cache_flush,
  268. // Efficeon-specific GATT table setup / populate / teardown
  269. .create_gatt_table = efficeon_create_gatt_table,
  270. .free_gatt_table = efficeon_free_gatt_table,
  271. .insert_memory = efficeon_insert_memory,
  272. .remove_memory = efficeon_remove_memory,
  273. .cant_use_aperture = false, // true might be faster?
  274. // Generic
  275. .alloc_by_type = agp_generic_alloc_by_type,
  276. .free_by_type = agp_generic_free_by_type,
  277. .agp_alloc_page = agp_generic_alloc_page,
  278. .agp_alloc_pages = agp_generic_alloc_pages,
  279. .agp_destroy_page = agp_generic_destroy_page,
  280. .agp_destroy_pages = agp_generic_destroy_pages,
  281. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  282. };
  283. static int __devinit agp_efficeon_probe(struct pci_dev *pdev,
  284. const struct pci_device_id *ent)
  285. {
  286. struct agp_bridge_data *bridge;
  287. u8 cap_ptr;
  288. struct resource *r;
  289. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  290. if (!cap_ptr)
  291. return -ENODEV;
  292. /* Probe for Efficeon controller */
  293. if (pdev->device != PCI_DEVICE_ID_EFFICEON) {
  294. printk(KERN_ERR PFX "Unsupported Efficeon chipset (device id: %04x)\n",
  295. pdev->device);
  296. return -ENODEV;
  297. }
  298. printk(KERN_INFO PFX "Detected Transmeta Efficeon TM8000 series chipset\n");
  299. bridge = agp_alloc_bridge();
  300. if (!bridge)
  301. return -ENOMEM;
  302. bridge->driver = &efficeon_driver;
  303. bridge->dev = pdev;
  304. bridge->capndx = cap_ptr;
  305. /*
  306. * If the device has not been properly setup, the following will catch
  307. * the problem and should stop the system from crashing.
  308. * 20030610 - hamish@zot.org
  309. */
  310. if (pci_enable_device(pdev)) {
  311. printk(KERN_ERR PFX "Unable to Enable PCI device\n");
  312. agp_put_bridge(bridge);
  313. return -ENODEV;
  314. }
  315. /*
  316. * The following fixes the case where the BIOS has "forgotten" to
  317. * provide an address range for the GART.
  318. * 20030610 - hamish@zot.org
  319. */
  320. r = &pdev->resource[0];
  321. if (!r->start && r->end) {
  322. if (pci_assign_resource(pdev, 0)) {
  323. printk(KERN_ERR PFX "could not assign resource 0\n");
  324. agp_put_bridge(bridge);
  325. return -ENODEV;
  326. }
  327. }
  328. /* Fill in the mode register */
  329. if (cap_ptr) {
  330. pci_read_config_dword(pdev,
  331. bridge->capndx+PCI_AGP_STATUS,
  332. &bridge->mode);
  333. }
  334. pci_set_drvdata(pdev, bridge);
  335. return agp_add_bridge(bridge);
  336. }
  337. static void __devexit agp_efficeon_remove(struct pci_dev *pdev)
  338. {
  339. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  340. agp_remove_bridge(bridge);
  341. agp_put_bridge(bridge);
  342. }
  343. #ifdef CONFIG_PM
  344. static int agp_efficeon_suspend(struct pci_dev *dev, pm_message_t state)
  345. {
  346. return 0;
  347. }
  348. static int agp_efficeon_resume(struct pci_dev *pdev)
  349. {
  350. printk(KERN_DEBUG PFX "agp_efficeon_resume()\n");
  351. return efficeon_configure();
  352. }
  353. #endif
  354. static struct pci_device_id agp_efficeon_pci_table[] = {
  355. {
  356. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  357. .class_mask = ~0,
  358. .vendor = PCI_VENDOR_ID_TRANSMETA,
  359. .device = PCI_ANY_ID,
  360. .subvendor = PCI_ANY_ID,
  361. .subdevice = PCI_ANY_ID,
  362. },
  363. { }
  364. };
  365. MODULE_DEVICE_TABLE(pci, agp_efficeon_pci_table);
  366. static struct pci_driver agp_efficeon_pci_driver = {
  367. .name = "agpgart-efficeon",
  368. .id_table = agp_efficeon_pci_table,
  369. .probe = agp_efficeon_probe,
  370. .remove = agp_efficeon_remove,
  371. #ifdef CONFIG_PM
  372. .suspend = agp_efficeon_suspend,
  373. .resume = agp_efficeon_resume,
  374. #endif
  375. };
  376. static int __init agp_efficeon_init(void)
  377. {
  378. static int agp_initialised=0;
  379. if (agp_off)
  380. return -EINVAL;
  381. if (agp_initialised == 1)
  382. return 0;
  383. agp_initialised=1;
  384. return pci_register_driver(&agp_efficeon_pci_driver);
  385. }
  386. static void __exit agp_efficeon_cleanup(void)
  387. {
  388. pci_unregister_driver(&agp_efficeon_pci_driver);
  389. }
  390. module_init(agp_efficeon_init);
  391. module_exit(agp_efficeon_cleanup);
  392. MODULE_AUTHOR("Carlos Puchol <cpglinux@puchol.com>");
  393. MODULE_LICENSE("GPL and additional rights");