parisc-agp.c 10.0 KB

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