parisc-agp.c 10 KB

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