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