parisc-agp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * HP Quicksilver AGP GART routines
  3. *
  4. * Copyright (c) 2006, Kyle McMartin <kyle@parisc-linux.org>
  5. *
  6. * Based on drivers/char/agpgart/hp-agp.c which is
  7. * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
  8. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/klist.h>
  19. #include <linux/agp_backend.h>
  20. #include <asm-parisc/parisc-device.h>
  21. #include <asm-parisc/ropes.h>
  22. #include "agp.h"
  23. #define DRVNAME "quicksilver"
  24. #define DRVPFX DRVNAME ": "
  25. #ifndef log2
  26. #define log2(x) ffz(~(x))
  27. #endif
  28. #define AGP8X_MODE_BIT 3
  29. #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
  30. static struct _parisc_agp_info {
  31. void __iomem *ioc_regs;
  32. void __iomem *lba_regs;
  33. int lba_cap_offset;
  34. u64 *gatt;
  35. u64 gatt_entries;
  36. u64 gart_base;
  37. u64 gart_size;
  38. int io_page_size;
  39. int io_pages_per_kpage;
  40. } parisc_agp_info;
  41. static struct gatt_mask parisc_agp_masks[] =
  42. {
  43. {
  44. .mask = SBA_PDIR_VALID_BIT,
  45. .type = 0
  46. }
  47. };
  48. static struct aper_size_info_fixed parisc_agp_sizes[] =
  49. {
  50. {0, 0, 0}, /* filled in by parisc_agp_fetch_size() */
  51. };
  52. static int
  53. parisc_agp_fetch_size(void)
  54. {
  55. int size;
  56. size = parisc_agp_info.gart_size / MB(1);
  57. parisc_agp_sizes[0].size = size;
  58. agp_bridge->current_size = (void *) &parisc_agp_sizes[0];
  59. return size;
  60. }
  61. static int
  62. parisc_agp_configure(void)
  63. {
  64. struct _parisc_agp_info *info = &parisc_agp_info;
  65. agp_bridge->gart_bus_addr = info->gart_base;
  66. agp_bridge->capndx = info->lba_cap_offset;
  67. agp_bridge->mode = readl(info->lba_regs+info->lba_cap_offset+PCI_AGP_STATUS);
  68. return 0;
  69. }
  70. static void
  71. parisc_agp_tlbflush(struct agp_memory *mem)
  72. {
  73. struct _parisc_agp_info *info = &parisc_agp_info;
  74. writeq(info->gart_base | log2(info->gart_size), info->ioc_regs+IOC_PCOM);
  75. readq(info->ioc_regs+IOC_PCOM); /* flush */
  76. }
  77. static int
  78. parisc_agp_create_gatt_table(struct agp_bridge_data *bridge)
  79. {
  80. struct _parisc_agp_info *info = &parisc_agp_info;
  81. int i;
  82. for (i = 0; i < info->gatt_entries; i++) {
  83. info->gatt[i] = (unsigned long)agp_bridge->scratch_page;
  84. }
  85. return 0;
  86. }
  87. static int
  88. parisc_agp_free_gatt_table(struct agp_bridge_data *bridge)
  89. {
  90. struct _parisc_agp_info *info = &parisc_agp_info;
  91. info->gatt[0] = SBA_AGPGART_COOKIE;
  92. return 0;
  93. }
  94. static int
  95. parisc_agp_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
  96. {
  97. struct _parisc_agp_info *info = &parisc_agp_info;
  98. int i, k;
  99. off_t j, io_pg_start;
  100. int io_pg_count;
  101. if (type != 0 || mem->type != 0) {
  102. return -EINVAL;
  103. }
  104. io_pg_start = info->io_pages_per_kpage * pg_start;
  105. io_pg_count = info->io_pages_per_kpage * mem->page_count;
  106. if ((io_pg_start + io_pg_count) > info->gatt_entries) {
  107. return -EINVAL;
  108. }
  109. j = io_pg_start;
  110. while (j < (io_pg_start + io_pg_count)) {
  111. if (info->gatt[j])
  112. return -EBUSY;
  113. j++;
  114. }
  115. if (mem->is_flushed == FALSE) {
  116. global_cache_flush();
  117. mem->is_flushed = TRUE;
  118. }
  119. for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
  120. unsigned long paddr;
  121. paddr = mem->memory[i];
  122. for (k = 0;
  123. k < info->io_pages_per_kpage;
  124. k++, j++, paddr += info->io_page_size) {
  125. info->gatt[j] =
  126. agp_bridge->driver->mask_memory(agp_bridge,
  127. paddr, type);
  128. }
  129. }
  130. agp_bridge->driver->tlb_flush(mem);
  131. return 0;
  132. }
  133. static int
  134. parisc_agp_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  135. {
  136. struct _parisc_agp_info *info = &parisc_agp_info;
  137. int i, io_pg_start, io_pg_count;
  138. if (type != 0 || mem->type != 0) {
  139. return -EINVAL;
  140. }
  141. io_pg_start = info->io_pages_per_kpage * pg_start;
  142. io_pg_count = info->io_pages_per_kpage * mem->page_count;
  143. for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
  144. info->gatt[i] = agp_bridge->scratch_page;
  145. }
  146. agp_bridge->driver->tlb_flush(mem);
  147. return 0;
  148. }
  149. static unsigned long
  150. parisc_agp_mask_memory(struct agp_bridge_data *bridge,
  151. unsigned long addr, int type)
  152. {
  153. return SBA_PDIR_VALID_BIT | addr;
  154. }
  155. static void
  156. parisc_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  157. {
  158. struct _parisc_agp_info *info = &parisc_agp_info;
  159. u32 command;
  160. command = readl(info->lba_regs + info->lba_cap_offset + PCI_AGP_STATUS);
  161. command = agp_collect_device_status(bridge, mode, command);
  162. command |= 0x00000100;
  163. writel(command, info->lba_regs + info->lba_cap_offset + PCI_AGP_COMMAND);
  164. agp_device_command(command, (mode & AGP8X_MODE) != 0);
  165. }
  166. struct agp_bridge_driver parisc_agp_driver = {
  167. .owner = THIS_MODULE,
  168. .size_type = FIXED_APER_SIZE,
  169. .configure = parisc_agp_configure,
  170. .fetch_size = parisc_agp_fetch_size,
  171. .tlb_flush = parisc_agp_tlbflush,
  172. .mask_memory = parisc_agp_mask_memory,
  173. .masks = parisc_agp_masks,
  174. .agp_enable = parisc_agp_enable,
  175. .cache_flush = global_cache_flush,
  176. .create_gatt_table = parisc_agp_create_gatt_table,
  177. .free_gatt_table = parisc_agp_free_gatt_table,
  178. .insert_memory = parisc_agp_insert_memory,
  179. .remove_memory = parisc_agp_remove_memory,
  180. .alloc_by_type = agp_generic_alloc_by_type,
  181. .free_by_type = agp_generic_free_by_type,
  182. .agp_alloc_page = agp_generic_alloc_page,
  183. .agp_destroy_page = agp_generic_destroy_page,
  184. .cant_use_aperture = 1,
  185. };
  186. static int __init
  187. agp_ioc_init(void __iomem *ioc_regs)
  188. {
  189. struct _parisc_agp_info *info = &parisc_agp_info;
  190. u64 *iova_base, *io_pdir, io_tlb_ps;
  191. int io_tlb_shift;
  192. printk(KERN_INFO DRVPFX "IO PDIR shared with sba_iommu\n");
  193. info->ioc_regs = ioc_regs;
  194. io_tlb_ps = readq(info->ioc_regs+IOC_TCNFG);
  195. switch (io_tlb_ps) {
  196. case 0: io_tlb_shift = 12; break;
  197. case 1: io_tlb_shift = 13; break;
  198. case 2: io_tlb_shift = 14; break;
  199. case 3: io_tlb_shift = 16; break;
  200. default:
  201. printk(KERN_ERR DRVPFX "Invalid IOTLB page size "
  202. "configuration 0x%llx\n", io_tlb_ps);
  203. info->gatt = NULL;
  204. info->gatt_entries = 0;
  205. return -ENODEV;
  206. }
  207. info->io_page_size = 1 << io_tlb_shift;
  208. info->io_pages_per_kpage = PAGE_SIZE / info->io_page_size;
  209. iova_base = readq(info->ioc_regs+IOC_IBASE) & ~0x1;
  210. info->gart_base = iova_base + PLUTO_IOVA_SIZE - PLUTO_GART_SIZE;
  211. info->gart_size = PLUTO_GART_SIZE;
  212. info->gatt_entries = info->gart_size / info->io_page_size;
  213. io_pdir = phys_to_virt(readq(info->ioc_regs+IOC_PDIR_BASE));
  214. info->gatt = &io_pdir[(PLUTO_IOVA_SIZE/2) >> PAGE_SHIFT];
  215. if (info->gatt[0] != SBA_AGPGART_COOKIE) {
  216. info->gatt = NULL;
  217. info->gatt_entries = 0;
  218. printk(KERN_ERR DRVPFX "No reserved IO PDIR entry found; "
  219. "GART disabled\n");
  220. return -ENODEV;
  221. }
  222. return 0;
  223. }
  224. static int
  225. lba_find_capability(int cap)
  226. {
  227. struct _parisc_agp_info *info = &parisc_agp_info;
  228. u16 status;
  229. u8 pos, id;
  230. int ttl = 48;
  231. status = readw(info->lba_regs + PCI_STATUS);
  232. if (!(status & PCI_STATUS_CAP_LIST))
  233. return 0;
  234. pos = readb(info->lba_regs + PCI_CAPABILITY_LIST);
  235. while (ttl-- && pos >= 0x40) {
  236. pos &= ~3;
  237. id = readb(info->lba_regs + pos + PCI_CAP_LIST_ID);
  238. if (id == 0xff)
  239. break;
  240. if (id == cap)
  241. return pos;
  242. pos = readb(info->lba_regs + pos + PCI_CAP_LIST_NEXT);
  243. }
  244. return 0;
  245. }
  246. static int __init
  247. agp_lba_init(void __iomem *lba_hpa)
  248. {
  249. struct _parisc_agp_info *info = &parisc_agp_info;
  250. int cap;
  251. info->lba_regs = lba_hpa;
  252. info->lba_cap_offset = lba_find_capability(PCI_CAP_ID_AGP);
  253. cap = readl(lba_hpa + info->lba_cap_offset) & 0xff;
  254. if (cap != PCI_CAP_ID_AGP) {
  255. printk(KERN_ERR DRVPFX "Invalid capability ID 0x%02x at 0x%x\n",
  256. cap, info->lba_cap_offset);
  257. return -ENODEV;
  258. }
  259. return 0;
  260. }
  261. static int __init
  262. parisc_agp_setup(void __iomem *ioc_hpa, void __iomem *lba_hpa)
  263. {
  264. struct pci_dev *fake_bridge_dev = NULL;
  265. struct agp_bridge_data *bridge;
  266. int error = 0;
  267. fake_bridge_dev = kmalloc(sizeof (struct pci_dev), GFP_KERNEL);
  268. if (!fake_bridge_dev) {
  269. error = -ENOMEM;
  270. goto fail;
  271. }
  272. error = agp_ioc_init(ioc_hpa);
  273. if (error)
  274. goto fail;
  275. error = agp_lba_init(lba_hpa);
  276. if (error)
  277. goto fail;
  278. bridge = agp_alloc_bridge();
  279. if (!bridge) {
  280. error = -ENOMEM;
  281. goto fail;
  282. }
  283. bridge->driver = &parisc_agp_driver;
  284. fake_bridge_dev->vendor = PCI_VENDOR_ID_HP;
  285. fake_bridge_dev->device = PCI_DEVICE_ID_HP_PCIX_LBA;
  286. bridge->dev = fake_bridge_dev;
  287. error = agp_add_bridge(bridge);
  288. fail:
  289. return error;
  290. }
  291. static struct device *next_device(struct klist_iter *i) {
  292. struct klist_node * n = klist_next(i);
  293. return n ? container_of(n, struct device, knode_parent) : NULL;
  294. }
  295. static int
  296. parisc_agp_init(void)
  297. {
  298. extern struct sba_device *sba_list;
  299. int err = -1;
  300. struct parisc_device *sba = NULL, *lba = NULL;
  301. struct lba_device *lbadev = NULL;
  302. struct device *dev = NULL;
  303. struct klist_iter i;
  304. if (!sba_list)
  305. goto out;
  306. /* Find our parent Pluto */
  307. sba = sba_list->dev;
  308. if (!IS_PLUTO(sba)) {
  309. printk(KERN_INFO DRVPFX "No Pluto found, so no AGPGART for you.\n");
  310. goto out;
  311. }
  312. /* Now search our Pluto for our precious AGP device... */
  313. klist_iter_init(&sba->dev.klist_children, &i);
  314. while ((dev = next_device(&i))) {
  315. struct parisc_device *padev = to_parisc_device(dev);
  316. if (IS_QUICKSILVER(padev))
  317. lba = padev;
  318. }
  319. klist_iter_exit(&i);
  320. if (!lba) {
  321. printk(KERN_INFO DRVPFX "No AGP devices found.\n");
  322. goto out;
  323. }
  324. lbadev = parisc_get_drvdata(lba);
  325. /* w00t, let's go find our cookies... */
  326. parisc_agp_setup(sba_list->ioc[0].ioc_hpa, lbadev->hba.base_addr);
  327. return 0;
  328. out:
  329. return err;
  330. }
  331. module_init(parisc_agp_init);
  332. MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
  333. MODULE_LICENSE("GPL");