efficeon-agp.c 12 KB

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