parisc-agp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HP Quicksilver AGP GART routines
  4. *
  5. * Copyright (c) 2006, Kyle McMartin <kyle@parisc-linux.org>
  6. *
  7. * Based on drivers/char/agpgart/hp-agp.c which is
  8. * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
  9. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/init.h>
  14. #include <linux/klist.h>
  15. #include <linux/agp_backend.h>
  16. #include <linux/log2.h>
  17. #include <linux/slab.h>
  18. #include <asm/parisc-device.h>
  19. #include <asm/ropes.h>
  20. #include "agp.h"
  21. #define DRVNAME "quicksilver"
  22. #define DRVPFX DRVNAME ": "
  23. #define AGP8X_MODE_BIT 3
  24. #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
  25. static unsigned long
  26. parisc_agp_mask_memory(struct agp_bridge_data *bridge, dma_addr_t addr,
  27. int type);
  28. static struct _parisc_agp_info {
  29. void __iomem *ioc_regs;
  30. void __iomem *lba_regs;
  31. int lba_cap_offset;
  32. __le64 *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. /* force fdc ops to be visible to IOMMU */
  73. asm_io_sync();
  74. writeq(info->gart_base | ilog2(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] = cpu_to_le64(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 != mem->type ||
  102. agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
  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] = cpu_to_le64(
  127. parisc_agp_mask_memory(agp_bridge,
  128. paddr, type));
  129. asm_io_fdc(&info->gatt[j]);
  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 != mem->type ||
  141. agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
  142. return -EINVAL;
  143. }
  144. io_pg_start = info->io_pages_per_kpage * pg_start;
  145. io_pg_count = info->io_pages_per_kpage * mem->page_count;
  146. for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
  147. info->gatt[i] = cpu_to_le64(agp_bridge->scratch_page);
  148. }
  149. agp_bridge->driver->tlb_flush(mem);
  150. return 0;
  151. }
  152. static unsigned long
  153. parisc_agp_mask_memory(struct agp_bridge_data *bridge, dma_addr_t addr,
  154. int type)
  155. {
  156. unsigned ci; /* coherent index */
  157. dma_addr_t pa;
  158. pa = addr & IOVP_MASK;
  159. asm("lci 0(%1), %0" : "=r" (ci) : "r" (phys_to_virt(pa)));
  160. pa |= (ci >> PAGE_SHIFT) & 0xff;/* move CI (8 bits) into lowest byte */
  161. pa |= SBA_PDIR_VALID_BIT; /* set "valid" bit */
  162. /* return native (big-endian) PDIR entry */
  163. return pa;
  164. }
  165. static void
  166. parisc_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  167. {
  168. struct _parisc_agp_info *info = &parisc_agp_info;
  169. u32 command;
  170. command = readl(info->lba_regs + info->lba_cap_offset + PCI_AGP_STATUS);
  171. command = agp_collect_device_status(bridge, mode, command);
  172. command |= 0x00000100;
  173. writel(command, info->lba_regs + info->lba_cap_offset + PCI_AGP_COMMAND);
  174. agp_device_command(command, (mode & AGP8X_MODE) != 0);
  175. }
  176. static const struct agp_bridge_driver parisc_agp_driver = {
  177. .owner = THIS_MODULE,
  178. .size_type = FIXED_APER_SIZE,
  179. .configure = parisc_agp_configure,
  180. .fetch_size = parisc_agp_fetch_size,
  181. .tlb_flush = parisc_agp_tlbflush,
  182. .mask_memory = parisc_agp_mask_memory,
  183. .masks = parisc_agp_masks,
  184. .agp_enable = parisc_agp_enable,
  185. .cache_flush = global_cache_flush,
  186. .create_gatt_table = parisc_agp_create_gatt_table,
  187. .free_gatt_table = parisc_agp_free_gatt_table,
  188. .insert_memory = parisc_agp_insert_memory,
  189. .remove_memory = parisc_agp_remove_memory,
  190. .alloc_by_type = agp_generic_alloc_by_type,
  191. .free_by_type = agp_generic_free_by_type,
  192. .agp_alloc_page = agp_generic_alloc_page,
  193. .agp_alloc_pages = agp_generic_alloc_pages,
  194. .agp_destroy_page = agp_generic_destroy_page,
  195. .agp_destroy_pages = agp_generic_destroy_pages,
  196. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  197. .cant_use_aperture = true,
  198. };
  199. static int __init
  200. agp_ioc_init(void __iomem *ioc_regs)
  201. {
  202. struct _parisc_agp_info *info = &parisc_agp_info;
  203. u64 iova_base, io_tlb_ps;
  204. __le64 *io_pdir;
  205. int io_tlb_shift;
  206. printk(KERN_INFO DRVPFX "IO PDIR shared with sba_iommu\n");
  207. info->ioc_regs = ioc_regs;
  208. io_tlb_ps = readq(info->ioc_regs+IOC_TCNFG);
  209. switch (io_tlb_ps) {
  210. case 0: io_tlb_shift = 12; break;
  211. case 1: io_tlb_shift = 13; break;
  212. case 2: io_tlb_shift = 14; break;
  213. case 3: io_tlb_shift = 16; break;
  214. default:
  215. printk(KERN_ERR DRVPFX "Invalid IOTLB page size "
  216. "configuration 0x%llx\n", io_tlb_ps);
  217. info->gatt = NULL;
  218. info->gatt_entries = 0;
  219. return -ENODEV;
  220. }
  221. info->io_page_size = 1 << io_tlb_shift;
  222. info->io_pages_per_kpage = PAGE_SIZE / info->io_page_size;
  223. iova_base = readq(info->ioc_regs+IOC_IBASE) & ~0x1;
  224. info->gart_base = iova_base + PLUTO_IOVA_SIZE - PLUTO_GART_SIZE;
  225. info->gart_size = PLUTO_GART_SIZE;
  226. info->gatt_entries = info->gart_size / info->io_page_size;
  227. io_pdir = phys_to_virt(readq(info->ioc_regs+IOC_PDIR_BASE));
  228. info->gatt = &io_pdir[(PLUTO_IOVA_SIZE/2) >> PAGE_SHIFT];
  229. if (info->gatt[0] != SBA_AGPGART_COOKIE) {
  230. info->gatt = NULL;
  231. info->gatt_entries = 0;
  232. printk(KERN_ERR DRVPFX "No reserved IO PDIR entry found; "
  233. "GART disabled\n");
  234. return -ENODEV;
  235. }
  236. return 0;
  237. }
  238. static int __init
  239. lba_find_capability(int cap)
  240. {
  241. struct _parisc_agp_info *info = &parisc_agp_info;
  242. u16 status;
  243. u8 pos, id;
  244. int ttl = 48;
  245. status = readw(info->lba_regs + PCI_STATUS);
  246. if (!(status & PCI_STATUS_CAP_LIST))
  247. return 0;
  248. pos = readb(info->lba_regs + PCI_CAPABILITY_LIST);
  249. while (ttl-- && pos >= 0x40) {
  250. pos &= ~3;
  251. id = readb(info->lba_regs + pos + PCI_CAP_LIST_ID);
  252. if (id == 0xff)
  253. break;
  254. if (id == cap)
  255. return pos;
  256. pos = readb(info->lba_regs + pos + PCI_CAP_LIST_NEXT);
  257. }
  258. return 0;
  259. }
  260. static int __init
  261. agp_lba_init(void __iomem *lba_hpa)
  262. {
  263. struct _parisc_agp_info *info = &parisc_agp_info;
  264. int cap;
  265. info->lba_regs = lba_hpa;
  266. info->lba_cap_offset = lba_find_capability(PCI_CAP_ID_AGP);
  267. cap = readl(lba_hpa + info->lba_cap_offset) & 0xff;
  268. if (cap != PCI_CAP_ID_AGP) {
  269. printk(KERN_ERR DRVPFX "Invalid capability ID 0x%02x at 0x%x\n",
  270. cap, info->lba_cap_offset);
  271. return -ENODEV;
  272. }
  273. return 0;
  274. }
  275. static int __init
  276. parisc_agp_setup(void __iomem *ioc_hpa, void __iomem *lba_hpa)
  277. {
  278. struct pci_dev *fake_bridge_dev = NULL;
  279. struct agp_bridge_data *bridge;
  280. int error = 0;
  281. fake_bridge_dev = pci_alloc_dev(NULL);
  282. if (!fake_bridge_dev) {
  283. error = -ENOMEM;
  284. goto fail;
  285. }
  286. error = agp_ioc_init(ioc_hpa);
  287. if (error)
  288. goto fail;
  289. error = agp_lba_init(lba_hpa);
  290. if (error)
  291. goto fail;
  292. bridge = agp_alloc_bridge();
  293. if (!bridge) {
  294. error = -ENOMEM;
  295. goto fail;
  296. }
  297. bridge->driver = &parisc_agp_driver;
  298. fake_bridge_dev->vendor = PCI_VENDOR_ID_HP;
  299. fake_bridge_dev->device = PCI_DEVICE_ID_HP_PCIX_LBA;
  300. bridge->dev = fake_bridge_dev;
  301. error = agp_add_bridge(bridge);
  302. if (error)
  303. goto fail;
  304. return 0;
  305. fail:
  306. kfree(fake_bridge_dev);
  307. return error;
  308. }
  309. static int __init
  310. find_quicksilver(struct device *dev, void *data)
  311. {
  312. struct parisc_device **lba = data;
  313. struct parisc_device *padev = to_parisc_device(dev);
  314. if (IS_QUICKSILVER(padev))
  315. *lba = padev;
  316. return 0;
  317. }
  318. static int __init
  319. parisc_agp_init(void)
  320. {
  321. int err = -1;
  322. struct parisc_device *sba = NULL, *lba = NULL;
  323. struct lba_device *lbadev = NULL;
  324. if (!sba_list)
  325. goto out;
  326. /* Find our parent Pluto */
  327. sba = sba_list->dev;
  328. if (!IS_PLUTO(sba)) {
  329. printk(KERN_INFO DRVPFX "No Pluto found, so no AGPGART for you.\n");
  330. goto out;
  331. }
  332. /* Now search our Pluto for our precious AGP device... */
  333. device_for_each_child(&sba->dev, &lba, find_quicksilver);
  334. if (!lba) {
  335. printk(KERN_INFO DRVPFX "No AGP devices found.\n");
  336. goto out;
  337. }
  338. lbadev = parisc_get_drvdata(lba);
  339. /* w00t, let's go find our cookies... */
  340. parisc_agp_setup(sba_list->ioc[0].ioc_hpa, lbadev->hba.base_addr);
  341. return 0;
  342. out:
  343. return err;
  344. }
  345. module_init(parisc_agp_init);
  346. MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
  347. MODULE_DESCRIPTION("HP Quicksilver AGP GART routines");
  348. MODULE_LICENSE("GPL");