parisc-agp.c 9.9 KB

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