efficeon-agp.c 12 KB

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