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