efficeon-agp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <[email protected]>
  7. * and H. Peter Anvin <[email protected]>.
  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. 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. for (offset = 0; offset < PAGE_SIZE; offset += clflush_chunk)
  187. clflush((char *)page+offset);
  188. efficeon_private.l1_table[index] = page;
  189. value = virt_to_phys((unsigned long *)page) | pati | present | index;
  190. pci_write_config_dword(agp_bridge->dev,
  191. EFFICEON_ATTPAGE, value);
  192. }
  193. return 0;
  194. }
  195. static int efficeon_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
  196. {
  197. int i, count = mem->page_count, num_entries;
  198. unsigned int *page, *last_page;
  199. const int clflush_chunk = ((cpuid_ebx(1) >> 8) & 0xff) << 3;
  200. const unsigned long clflush_mask = ~(clflush_chunk-1);
  201. printk(KERN_DEBUG PFX "efficeon_insert_memory(%lx, %d)\n", pg_start, count);
  202. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  203. if ((pg_start + mem->page_count) > num_entries)
  204. return -EINVAL;
  205. if (type != 0 || mem->type != 0)
  206. return -EINVAL;
  207. if (!mem->is_flushed) {
  208. global_cache_flush();
  209. mem->is_flushed = true;
  210. }
  211. last_page = NULL;
  212. for (i = 0; i < count; i++) {
  213. int index = pg_start + i;
  214. unsigned long insert = efficeon_mask_memory(mem->pages[i]);
  215. page = (unsigned int *) efficeon_private.l1_table[index >> 10];
  216. if (!page)
  217. continue;
  218. page += (index & 0x3ff);
  219. *page = insert;
  220. /* clflush is slow, so don't clflush until we have to */
  221. if (last_page &&
  222. (((unsigned long)page^(unsigned long)last_page) &
  223. clflush_mask))
  224. clflush(last_page);
  225. last_page = page;
  226. }
  227. if ( last_page )
  228. clflush(last_page);
  229. agp_bridge->driver->tlb_flush(mem);
  230. return 0;
  231. }
  232. static int efficeon_remove_memory(struct agp_memory * mem, off_t pg_start, int type)
  233. {
  234. int i, count = mem->page_count, num_entries;
  235. printk(KERN_DEBUG PFX "efficeon_remove_memory(%lx, %d)\n", pg_start, count);
  236. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  237. if ((pg_start + mem->page_count) > num_entries)
  238. return -EINVAL;
  239. if (type != 0 || mem->type != 0)
  240. return -EINVAL;
  241. for (i = 0; i < count; i++) {
  242. int index = pg_start + i;
  243. unsigned int *page = (unsigned int *) efficeon_private.l1_table[index >> 10];
  244. if (!page)
  245. continue;
  246. page += (index & 0x3ff);
  247. *page = 0;
  248. }
  249. agp_bridge->driver->tlb_flush(mem);
  250. return 0;
  251. }
  252. static const struct agp_bridge_driver efficeon_driver = {
  253. .owner = THIS_MODULE,
  254. .aperture_sizes = efficeon_generic_sizes,
  255. .size_type = LVL2_APER_SIZE,
  256. .num_aperture_sizes = 4,
  257. .configure = efficeon_configure,
  258. .fetch_size = efficeon_fetch_size,
  259. .cleanup = efficeon_cleanup,
  260. .tlb_flush = efficeon_tlbflush,
  261. .mask_memory = agp_generic_mask_memory,
  262. .masks = efficeon_generic_masks,
  263. .agp_enable = agp_generic_enable,
  264. .cache_flush = global_cache_flush,
  265. // Efficeon-specific GATT table setup / populate / teardown
  266. .create_gatt_table = efficeon_create_gatt_table,
  267. .free_gatt_table = efficeon_free_gatt_table,
  268. .insert_memory = efficeon_insert_memory,
  269. .remove_memory = efficeon_remove_memory,
  270. .cant_use_aperture = false, // true might be faster?
  271. // Generic
  272. .alloc_by_type = agp_generic_alloc_by_type,
  273. .free_by_type = agp_generic_free_by_type,
  274. .agp_alloc_page = agp_generic_alloc_page,
  275. .agp_alloc_pages = agp_generic_alloc_pages,
  276. .agp_destroy_page = agp_generic_destroy_page,
  277. .agp_destroy_pages = agp_generic_destroy_pages,
  278. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  279. };
  280. static int agp_efficeon_probe(struct pci_dev *pdev,
  281. const struct pci_device_id *ent)
  282. {
  283. struct agp_bridge_data *bridge;
  284. u8 cap_ptr;
  285. struct resource *r;
  286. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  287. if (!cap_ptr)
  288. return -ENODEV;
  289. /* Probe for Efficeon controller */
  290. if (pdev->device != PCI_DEVICE_ID_EFFICEON) {
  291. printk(KERN_ERR PFX "Unsupported Efficeon chipset (device id: %04x)\n",
  292. pdev->device);
  293. return -ENODEV;
  294. }
  295. printk(KERN_INFO PFX "Detected Transmeta Efficeon TM8000 series chipset\n");
  296. bridge = agp_alloc_bridge();
  297. if (!bridge)
  298. return -ENOMEM;
  299. bridge->driver = &efficeon_driver;
  300. bridge->dev = pdev;
  301. bridge->capndx = cap_ptr;
  302. /*
  303. * If the device has not been properly setup, the following will catch
  304. * the problem and should stop the system from crashing.
  305. * 20030610 - [email protected]
  306. */
  307. if (pci_enable_device(pdev)) {
  308. printk(KERN_ERR PFX "Unable to Enable PCI device\n");
  309. agp_put_bridge(bridge);
  310. return -ENODEV;
  311. }
  312. /*
  313. * The following fixes the case where the BIOS has "forgotten" to
  314. * provide an address range for the GART.
  315. * 20030610 - [email protected]
  316. */
  317. r = &pdev->resource[0];
  318. if (!r->start && r->end) {
  319. if (pci_assign_resource(pdev, 0)) {
  320. printk(KERN_ERR PFX "could not assign resource 0\n");
  321. agp_put_bridge(bridge);
  322. return -ENODEV;
  323. }
  324. }
  325. /* Fill in the mode register */
  326. if (cap_ptr) {
  327. pci_read_config_dword(pdev,
  328. bridge->capndx+PCI_AGP_STATUS,
  329. &bridge->mode);
  330. }
  331. pci_set_drvdata(pdev, bridge);
  332. return agp_add_bridge(bridge);
  333. }
  334. static void agp_efficeon_remove(struct pci_dev *pdev)
  335. {
  336. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  337. agp_remove_bridge(bridge);
  338. agp_put_bridge(bridge);
  339. }
  340. #ifdef CONFIG_PM
  341. static int agp_efficeon_suspend(struct pci_dev *dev, pm_message_t state)
  342. {
  343. return 0;
  344. }
  345. static int agp_efficeon_resume(struct pci_dev *pdev)
  346. {
  347. printk(KERN_DEBUG PFX "agp_efficeon_resume()\n");
  348. return efficeon_configure();
  349. }
  350. #endif
  351. static const struct pci_device_id agp_efficeon_pci_table[] = {
  352. {
  353. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  354. .class_mask = ~0,
  355. .vendor = PCI_VENDOR_ID_TRANSMETA,
  356. .device = PCI_ANY_ID,
  357. .subvendor = PCI_ANY_ID,
  358. .subdevice = PCI_ANY_ID,
  359. },
  360. { }
  361. };
  362. MODULE_DEVICE_TABLE(pci, agp_efficeon_pci_table);
  363. static struct pci_driver agp_efficeon_pci_driver = {
  364. .name = "agpgart-efficeon",
  365. .id_table = agp_efficeon_pci_table,
  366. .probe = agp_efficeon_probe,
  367. .remove = agp_efficeon_remove,
  368. #ifdef CONFIG_PM
  369. .suspend = agp_efficeon_suspend,
  370. .resume = agp_efficeon_resume,
  371. #endif
  372. };
  373. static int __init agp_efficeon_init(void)
  374. {
  375. static int agp_initialised=0;
  376. if (agp_off)
  377. return -EINVAL;
  378. if (agp_initialised == 1)
  379. return 0;
  380. agp_initialised=1;
  381. return pci_register_driver(&agp_efficeon_pci_driver);
  382. }
  383. static void __exit agp_efficeon_cleanup(void)
  384. {
  385. pci_unregister_driver(&agp_efficeon_pci_driver);
  386. }
  387. module_init(agp_efficeon_init);
  388. module_exit(agp_efficeon_cleanup);
  389. MODULE_AUTHOR("Carlos Puchol <[email protected]>");
  390. MODULE_LICENSE("GPL and additional rights");