parisc-agp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HP Quicksilver AGP GART routines
  4. *
  5. * Copyright (c) 2006, Kyle McMartin <[email protected]>
  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 <[email protected]>
  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. 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. /* 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] = (unsigned long)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] =
  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] = 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 cpu_to_le64(pa);
  163. }
  164. static void
  165. parisc_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  166. {
  167. struct _parisc_agp_info *info = &parisc_agp_info;
  168. u32 command;
  169. command = readl(info->lba_regs + info->lba_cap_offset + PCI_AGP_STATUS);
  170. command = agp_collect_device_status(bridge, mode, command);
  171. command |= 0x00000100;
  172. writel(command, info->lba_regs + info->lba_cap_offset + PCI_AGP_COMMAND);
  173. agp_device_command(command, (mode & AGP8X_MODE) != 0);
  174. }
  175. static const struct agp_bridge_driver parisc_agp_driver = {
  176. .owner = THIS_MODULE,
  177. .size_type = FIXED_APER_SIZE,
  178. .configure = parisc_agp_configure,
  179. .fetch_size = parisc_agp_fetch_size,
  180. .tlb_flush = parisc_agp_tlbflush,
  181. .mask_memory = parisc_agp_mask_memory,
  182. .masks = parisc_agp_masks,
  183. .agp_enable = parisc_agp_enable,
  184. .cache_flush = global_cache_flush,
  185. .create_gatt_table = parisc_agp_create_gatt_table,
  186. .free_gatt_table = parisc_agp_free_gatt_table,
  187. .insert_memory = parisc_agp_insert_memory,
  188. .remove_memory = parisc_agp_remove_memory,
  189. .alloc_by_type = agp_generic_alloc_by_type,
  190. .free_by_type = agp_generic_free_by_type,
  191. .agp_alloc_page = agp_generic_alloc_page,
  192. .agp_alloc_pages = agp_generic_alloc_pages,
  193. .agp_destroy_page = agp_generic_destroy_page,
  194. .agp_destroy_pages = agp_generic_destroy_pages,
  195. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  196. .cant_use_aperture = true,
  197. };
  198. static int __init
  199. agp_ioc_init(void __iomem *ioc_regs)
  200. {
  201. struct _parisc_agp_info *info = &parisc_agp_info;
  202. u64 iova_base, *io_pdir, io_tlb_ps;
  203. int io_tlb_shift;
  204. printk(KERN_INFO DRVPFX "IO PDIR shared with sba_iommu\n");
  205. info->ioc_regs = ioc_regs;
  206. io_tlb_ps = readq(info->ioc_regs+IOC_TCNFG);
  207. switch (io_tlb_ps) {
  208. case 0: io_tlb_shift = 12; break;
  209. case 1: io_tlb_shift = 13; break;
  210. case 2: io_tlb_shift = 14; break;
  211. case 3: io_tlb_shift = 16; break;
  212. default:
  213. printk(KERN_ERR DRVPFX "Invalid IOTLB page size "
  214. "configuration 0x%llx\n", io_tlb_ps);
  215. info->gatt = NULL;
  216. info->gatt_entries = 0;
  217. return -ENODEV;
  218. }
  219. info->io_page_size = 1 << io_tlb_shift;
  220. info->io_pages_per_kpage = PAGE_SIZE / info->io_page_size;
  221. iova_base = readq(info->ioc_regs+IOC_IBASE) & ~0x1;
  222. info->gart_base = iova_base + PLUTO_IOVA_SIZE - PLUTO_GART_SIZE;
  223. info->gart_size = PLUTO_GART_SIZE;
  224. info->gatt_entries = info->gart_size / info->io_page_size;
  225. io_pdir = phys_to_virt(readq(info->ioc_regs+IOC_PDIR_BASE));
  226. info->gatt = &io_pdir[(PLUTO_IOVA_SIZE/2) >> PAGE_SHIFT];
  227. if (info->gatt[0] != SBA_AGPGART_COOKIE) {
  228. info->gatt = NULL;
  229. info->gatt_entries = 0;
  230. printk(KERN_ERR DRVPFX "No reserved IO PDIR entry found; "
  231. "GART disabled\n");
  232. return -ENODEV;
  233. }
  234. return 0;
  235. }
  236. static int __init
  237. lba_find_capability(int cap)
  238. {
  239. struct _parisc_agp_info *info = &parisc_agp_info;
  240. u16 status;
  241. u8 pos, id;
  242. int ttl = 48;
  243. status = readw(info->lba_regs + PCI_STATUS);
  244. if (!(status & PCI_STATUS_CAP_LIST))
  245. return 0;
  246. pos = readb(info->lba_regs + PCI_CAPABILITY_LIST);
  247. while (ttl-- && pos >= 0x40) {
  248. pos &= ~3;
  249. id = readb(info->lba_regs + pos + PCI_CAP_LIST_ID);
  250. if (id == 0xff)
  251. break;
  252. if (id == cap)
  253. return pos;
  254. pos = readb(info->lba_regs + pos + PCI_CAP_LIST_NEXT);
  255. }
  256. return 0;
  257. }
  258. static int __init
  259. agp_lba_init(void __iomem *lba_hpa)
  260. {
  261. struct _parisc_agp_info *info = &parisc_agp_info;
  262. int cap;
  263. info->lba_regs = lba_hpa;
  264. info->lba_cap_offset = lba_find_capability(PCI_CAP_ID_AGP);
  265. cap = readl(lba_hpa + info->lba_cap_offset) & 0xff;
  266. if (cap != PCI_CAP_ID_AGP) {
  267. printk(KERN_ERR DRVPFX "Invalid capability ID 0x%02x at 0x%x\n",
  268. cap, info->lba_cap_offset);
  269. return -ENODEV;
  270. }
  271. return 0;
  272. }
  273. static int __init
  274. parisc_agp_setup(void __iomem *ioc_hpa, void __iomem *lba_hpa)
  275. {
  276. struct pci_dev *fake_bridge_dev = NULL;
  277. struct agp_bridge_data *bridge;
  278. int error = 0;
  279. fake_bridge_dev = pci_alloc_dev(NULL);
  280. if (!fake_bridge_dev) {
  281. error = -ENOMEM;
  282. goto fail;
  283. }
  284. error = agp_ioc_init(ioc_hpa);
  285. if (error)
  286. goto fail;
  287. error = agp_lba_init(lba_hpa);
  288. if (error)
  289. goto fail;
  290. bridge = agp_alloc_bridge();
  291. if (!bridge) {
  292. error = -ENOMEM;
  293. goto fail;
  294. }
  295. bridge->driver = &parisc_agp_driver;
  296. fake_bridge_dev->vendor = PCI_VENDOR_ID_HP;
  297. fake_bridge_dev->device = PCI_DEVICE_ID_HP_PCIX_LBA;
  298. bridge->dev = fake_bridge_dev;
  299. error = agp_add_bridge(bridge);
  300. if (error)
  301. goto fail;
  302. return 0;
  303. fail:
  304. kfree(fake_bridge_dev);
  305. return error;
  306. }
  307. static int __init
  308. find_quicksilver(struct device *dev, void *data)
  309. {
  310. struct parisc_device **lba = data;
  311. struct parisc_device *padev = to_parisc_device(dev);
  312. if (IS_QUICKSILVER(padev))
  313. *lba = padev;
  314. return 0;
  315. }
  316. static int __init
  317. parisc_agp_init(void)
  318. {
  319. int err = -1;
  320. struct parisc_device *sba = NULL, *lba = NULL;
  321. struct lba_device *lbadev = NULL;
  322. if (!sba_list)
  323. goto out;
  324. /* Find our parent Pluto */
  325. sba = sba_list->dev;
  326. if (!IS_PLUTO(sba)) {
  327. printk(KERN_INFO DRVPFX "No Pluto found, so no AGPGART for you.\n");
  328. goto out;
  329. }
  330. /* Now search our Pluto for our precious AGP device... */
  331. device_for_each_child(&sba->dev, &lba, find_quicksilver);
  332. if (!lba) {
  333. printk(KERN_INFO DRVPFX "No AGP devices found.\n");
  334. goto out;
  335. }
  336. lbadev = parisc_get_drvdata(lba);
  337. /* w00t, let's go find our cookies... */
  338. parisc_agp_setup(sba_list->ioc[0].ioc_hpa, lbadev->hba.base_addr);
  339. return 0;
  340. out:
  341. return err;
  342. }
  343. module_init(parisc_agp_init);
  344. MODULE_AUTHOR("Kyle McMartin <[email protected]>");
  345. MODULE_LICENSE("GPL");