hp-agp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HP zx1 AGPGART routines.
  4. *
  5. * (c) Copyright 2002, 2003 Hewlett-Packard Development Company, L.P.
  6. * Bjorn Helgaas <[email protected]>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/init.h>
  12. #include <linux/agp_backend.h>
  13. #include <linux/log2.h>
  14. #include <linux/slab.h>
  15. #include <asm/acpi-ext.h>
  16. #include "agp.h"
  17. #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
  18. /* HP ZX1 IOC registers */
  19. #define HP_ZX1_IBASE 0x300
  20. #define HP_ZX1_IMASK 0x308
  21. #define HP_ZX1_PCOM 0x310
  22. #define HP_ZX1_TCNFG 0x318
  23. #define HP_ZX1_PDIR_BASE 0x320
  24. #define HP_ZX1_IOVA_BASE GB(1UL)
  25. #define HP_ZX1_IOVA_SIZE GB(1UL)
  26. #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
  27. #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
  28. #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
  29. #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
  30. #define AGP8X_MODE_BIT 3
  31. #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
  32. /* AGP bridge need not be PCI device, but DRM thinks it is. */
  33. static struct pci_dev fake_bridge_dev;
  34. static int hp_zx1_gart_found;
  35. static struct aper_size_info_fixed hp_zx1_sizes[] =
  36. {
  37. {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
  38. };
  39. static struct gatt_mask hp_zx1_masks[] =
  40. {
  41. {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
  42. };
  43. static struct _hp_private {
  44. volatile u8 __iomem *ioc_regs;
  45. volatile u8 __iomem *lba_regs;
  46. int lba_cap_offset;
  47. u64 *io_pdir; // PDIR for entire IOVA
  48. u64 *gatt; // PDIR just for GART (subset of above)
  49. u64 gatt_entries;
  50. u64 iova_base;
  51. u64 gart_base;
  52. u64 gart_size;
  53. u64 io_pdir_size;
  54. int io_pdir_owner; // do we own it, or share it with sba_iommu?
  55. int io_page_size;
  56. int io_tlb_shift;
  57. int io_tlb_ps; // IOC ps config
  58. int io_pages_per_kpage;
  59. } hp_private;
  60. static int __init hp_zx1_ioc_shared(void)
  61. {
  62. struct _hp_private *hp = &hp_private;
  63. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
  64. /*
  65. * IOC already configured by sba_iommu module; just use
  66. * its setup. We assume:
  67. * - IOVA space is 1Gb in size
  68. * - first 512Mb is IOMMU, second 512Mb is GART
  69. */
  70. hp->io_tlb_ps = readq(hp->ioc_regs+HP_ZX1_TCNFG);
  71. switch (hp->io_tlb_ps) {
  72. case 0: hp->io_tlb_shift = 12; break;
  73. case 1: hp->io_tlb_shift = 13; break;
  74. case 2: hp->io_tlb_shift = 14; break;
  75. case 3: hp->io_tlb_shift = 16; break;
  76. default:
  77. printk(KERN_ERR PFX "Invalid IOTLB page size "
  78. "configuration 0x%x\n", hp->io_tlb_ps);
  79. hp->gatt = NULL;
  80. hp->gatt_entries = 0;
  81. return -ENODEV;
  82. }
  83. hp->io_page_size = 1 << hp->io_tlb_shift;
  84. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  85. hp->iova_base = readq(hp->ioc_regs+HP_ZX1_IBASE) & ~0x1;
  86. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
  87. hp->gart_size = HP_ZX1_GART_SIZE;
  88. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  89. hp->io_pdir = phys_to_virt(readq(hp->ioc_regs+HP_ZX1_PDIR_BASE));
  90. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  91. if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
  92. /* Normal case when no AGP device in system */
  93. hp->gatt = NULL;
  94. hp->gatt_entries = 0;
  95. printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
  96. "GART disabled\n");
  97. return -ENODEV;
  98. }
  99. return 0;
  100. }
  101. static int __init
  102. hp_zx1_ioc_owner (void)
  103. {
  104. struct _hp_private *hp = &hp_private;
  105. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
  106. /*
  107. * Select an IOV page size no larger than system page size.
  108. */
  109. if (PAGE_SIZE >= KB(64)) {
  110. hp->io_tlb_shift = 16;
  111. hp->io_tlb_ps = 3;
  112. } else if (PAGE_SIZE >= KB(16)) {
  113. hp->io_tlb_shift = 14;
  114. hp->io_tlb_ps = 2;
  115. } else if (PAGE_SIZE >= KB(8)) {
  116. hp->io_tlb_shift = 13;
  117. hp->io_tlb_ps = 1;
  118. } else {
  119. hp->io_tlb_shift = 12;
  120. hp->io_tlb_ps = 0;
  121. }
  122. hp->io_page_size = 1 << hp->io_tlb_shift;
  123. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  124. hp->iova_base = HP_ZX1_IOVA_BASE;
  125. hp->gart_size = HP_ZX1_GART_SIZE;
  126. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
  127. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  128. hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
  129. return 0;
  130. }
  131. static int __init
  132. hp_zx1_ioc_init (u64 hpa)
  133. {
  134. struct _hp_private *hp = &hp_private;
  135. hp->ioc_regs = ioremap(hpa, 1024);
  136. if (!hp->ioc_regs)
  137. return -ENOMEM;
  138. /*
  139. * If the IOTLB is currently disabled, we can take it over.
  140. * Otherwise, we have to share with sba_iommu.
  141. */
  142. hp->io_pdir_owner = (readq(hp->ioc_regs+HP_ZX1_IBASE) & 0x1) == 0;
  143. if (hp->io_pdir_owner)
  144. return hp_zx1_ioc_owner();
  145. return hp_zx1_ioc_shared();
  146. }
  147. static int
  148. hp_zx1_lba_find_capability (volatile u8 __iomem *hpa, int cap)
  149. {
  150. u16 status;
  151. u8 pos, id;
  152. int ttl = 48;
  153. status = readw(hpa+PCI_STATUS);
  154. if (!(status & PCI_STATUS_CAP_LIST))
  155. return 0;
  156. pos = readb(hpa+PCI_CAPABILITY_LIST);
  157. while (ttl-- && pos >= 0x40) {
  158. pos &= ~3;
  159. id = readb(hpa+pos+PCI_CAP_LIST_ID);
  160. if (id == 0xff)
  161. break;
  162. if (id == cap)
  163. return pos;
  164. pos = readb(hpa+pos+PCI_CAP_LIST_NEXT);
  165. }
  166. return 0;
  167. }
  168. static int __init
  169. hp_zx1_lba_init (u64 hpa)
  170. {
  171. struct _hp_private *hp = &hp_private;
  172. int cap;
  173. hp->lba_regs = ioremap(hpa, 256);
  174. if (!hp->lba_regs)
  175. return -ENOMEM;
  176. hp->lba_cap_offset = hp_zx1_lba_find_capability(hp->lba_regs, PCI_CAP_ID_AGP);
  177. cap = readl(hp->lba_regs+hp->lba_cap_offset) & 0xff;
  178. if (cap != PCI_CAP_ID_AGP) {
  179. printk(KERN_ERR PFX "Invalid capability ID 0x%02x at 0x%x\n",
  180. cap, hp->lba_cap_offset);
  181. iounmap(hp->lba_regs);
  182. return -ENODEV;
  183. }
  184. return 0;
  185. }
  186. static int
  187. hp_zx1_fetch_size(void)
  188. {
  189. int size;
  190. size = hp_private.gart_size / MB(1);
  191. hp_zx1_sizes[0].size = size;
  192. agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
  193. return size;
  194. }
  195. static int
  196. hp_zx1_configure (void)
  197. {
  198. struct _hp_private *hp = &hp_private;
  199. agp_bridge->gart_bus_addr = hp->gart_base;
  200. agp_bridge->capndx = hp->lba_cap_offset;
  201. agp_bridge->mode = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
  202. if (hp->io_pdir_owner) {
  203. writel(virt_to_phys(hp->io_pdir), hp->ioc_regs+HP_ZX1_PDIR_BASE);
  204. readl(hp->ioc_regs+HP_ZX1_PDIR_BASE);
  205. writel(hp->io_tlb_ps, hp->ioc_regs+HP_ZX1_TCNFG);
  206. readl(hp->ioc_regs+HP_ZX1_TCNFG);
  207. writel((unsigned int)(~(HP_ZX1_IOVA_SIZE-1)), hp->ioc_regs+HP_ZX1_IMASK);
  208. readl(hp->ioc_regs+HP_ZX1_IMASK);
  209. writel(hp->iova_base|1, hp->ioc_regs+HP_ZX1_IBASE);
  210. readl(hp->ioc_regs+HP_ZX1_IBASE);
  211. writel(hp->iova_base|ilog2(HP_ZX1_IOVA_SIZE), hp->ioc_regs+HP_ZX1_PCOM);
  212. readl(hp->ioc_regs+HP_ZX1_PCOM);
  213. }
  214. return 0;
  215. }
  216. static void
  217. hp_zx1_cleanup (void)
  218. {
  219. struct _hp_private *hp = &hp_private;
  220. if (hp->ioc_regs) {
  221. if (hp->io_pdir_owner) {
  222. writeq(0, hp->ioc_regs+HP_ZX1_IBASE);
  223. readq(hp->ioc_regs+HP_ZX1_IBASE);
  224. }
  225. iounmap(hp->ioc_regs);
  226. }
  227. if (hp->lba_regs)
  228. iounmap(hp->lba_regs);
  229. }
  230. static void
  231. hp_zx1_tlbflush (struct agp_memory *mem)
  232. {
  233. struct _hp_private *hp = &hp_private;
  234. writeq(hp->gart_base | ilog2(hp->gart_size), hp->ioc_regs+HP_ZX1_PCOM);
  235. readq(hp->ioc_regs+HP_ZX1_PCOM);
  236. }
  237. static int
  238. hp_zx1_create_gatt_table (struct agp_bridge_data *bridge)
  239. {
  240. struct _hp_private *hp = &hp_private;
  241. int i;
  242. if (hp->io_pdir_owner) {
  243. hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
  244. get_order(hp->io_pdir_size));
  245. if (!hp->io_pdir) {
  246. printk(KERN_ERR PFX "Couldn't allocate contiguous "
  247. "memory for I/O PDIR\n");
  248. hp->gatt = NULL;
  249. hp->gatt_entries = 0;
  250. return -ENOMEM;
  251. }
  252. memset(hp->io_pdir, 0, hp->io_pdir_size);
  253. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  254. }
  255. for (i = 0; i < hp->gatt_entries; i++) {
  256. hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
  257. }
  258. return 0;
  259. }
  260. static int
  261. hp_zx1_free_gatt_table (struct agp_bridge_data *bridge)
  262. {
  263. struct _hp_private *hp = &hp_private;
  264. if (hp->io_pdir_owner)
  265. free_pages((unsigned long) hp->io_pdir,
  266. get_order(hp->io_pdir_size));
  267. else
  268. hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
  269. return 0;
  270. }
  271. static int
  272. hp_zx1_insert_memory (struct agp_memory *mem, off_t pg_start, int type)
  273. {
  274. struct _hp_private *hp = &hp_private;
  275. int i, k;
  276. off_t j, io_pg_start;
  277. int io_pg_count;
  278. if (type != mem->type ||
  279. agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
  280. return -EINVAL;
  281. }
  282. io_pg_start = hp->io_pages_per_kpage * pg_start;
  283. io_pg_count = hp->io_pages_per_kpage * mem->page_count;
  284. if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
  285. return -EINVAL;
  286. }
  287. j = io_pg_start;
  288. while (j < (io_pg_start + io_pg_count)) {
  289. if (hp->gatt[j]) {
  290. return -EBUSY;
  291. }
  292. j++;
  293. }
  294. if (!mem->is_flushed) {
  295. global_cache_flush();
  296. mem->is_flushed = true;
  297. }
  298. for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
  299. unsigned long paddr;
  300. paddr = page_to_phys(mem->pages[i]);
  301. for (k = 0;
  302. k < hp->io_pages_per_kpage;
  303. k++, j++, paddr += hp->io_page_size) {
  304. hp->gatt[j] = HP_ZX1_PDIR_VALID_BIT | paddr;
  305. }
  306. }
  307. agp_bridge->driver->tlb_flush(mem);
  308. return 0;
  309. }
  310. static int
  311. hp_zx1_remove_memory (struct agp_memory *mem, off_t pg_start, int type)
  312. {
  313. struct _hp_private *hp = &hp_private;
  314. int i, io_pg_start, io_pg_count;
  315. if (type != mem->type ||
  316. agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type)) {
  317. return -EINVAL;
  318. }
  319. io_pg_start = hp->io_pages_per_kpage * pg_start;
  320. io_pg_count = hp->io_pages_per_kpage * mem->page_count;
  321. for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
  322. hp->gatt[i] = agp_bridge->scratch_page;
  323. }
  324. agp_bridge->driver->tlb_flush(mem);
  325. return 0;
  326. }
  327. static unsigned long
  328. hp_zx1_mask_memory (struct agp_bridge_data *bridge, dma_addr_t addr, int type)
  329. {
  330. return HP_ZX1_PDIR_VALID_BIT | addr;
  331. }
  332. static void
  333. hp_zx1_enable (struct agp_bridge_data *bridge, u32 mode)
  334. {
  335. struct _hp_private *hp = &hp_private;
  336. u32 command;
  337. command = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
  338. command = agp_collect_device_status(bridge, mode, command);
  339. command |= 0x00000100;
  340. writel(command, hp->lba_regs+hp->lba_cap_offset+PCI_AGP_COMMAND);
  341. agp_device_command(command, (mode & AGP8X_MODE) != 0);
  342. }
  343. const struct agp_bridge_driver hp_zx1_driver = {
  344. .owner = THIS_MODULE,
  345. .size_type = FIXED_APER_SIZE,
  346. .configure = hp_zx1_configure,
  347. .fetch_size = hp_zx1_fetch_size,
  348. .cleanup = hp_zx1_cleanup,
  349. .tlb_flush = hp_zx1_tlbflush,
  350. .mask_memory = hp_zx1_mask_memory,
  351. .masks = hp_zx1_masks,
  352. .agp_enable = hp_zx1_enable,
  353. .cache_flush = global_cache_flush,
  354. .create_gatt_table = hp_zx1_create_gatt_table,
  355. .free_gatt_table = hp_zx1_free_gatt_table,
  356. .insert_memory = hp_zx1_insert_memory,
  357. .remove_memory = hp_zx1_remove_memory,
  358. .alloc_by_type = agp_generic_alloc_by_type,
  359. .free_by_type = agp_generic_free_by_type,
  360. .agp_alloc_page = agp_generic_alloc_page,
  361. .agp_alloc_pages = agp_generic_alloc_pages,
  362. .agp_destroy_page = agp_generic_destroy_page,
  363. .agp_destroy_pages = agp_generic_destroy_pages,
  364. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  365. .cant_use_aperture = true,
  366. };
  367. static int __init
  368. hp_zx1_setup (u64 ioc_hpa, u64 lba_hpa)
  369. {
  370. struct agp_bridge_data *bridge;
  371. int error = 0;
  372. error = hp_zx1_ioc_init(ioc_hpa);
  373. if (error)
  374. goto fail;
  375. error = hp_zx1_lba_init(lba_hpa);
  376. if (error)
  377. goto fail;
  378. bridge = agp_alloc_bridge();
  379. if (!bridge) {
  380. error = -ENOMEM;
  381. goto fail;
  382. }
  383. bridge->driver = &hp_zx1_driver;
  384. fake_bridge_dev.vendor = PCI_VENDOR_ID_HP;
  385. fake_bridge_dev.device = PCI_DEVICE_ID_HP_PCIX_LBA;
  386. bridge->dev = &fake_bridge_dev;
  387. error = agp_add_bridge(bridge);
  388. fail:
  389. if (error)
  390. hp_zx1_cleanup();
  391. return error;
  392. }
  393. static acpi_status __init
  394. zx1_gart_probe (acpi_handle obj, u32 depth, void *context, void **ret)
  395. {
  396. acpi_handle handle, parent;
  397. acpi_status status;
  398. struct acpi_device_info *info;
  399. u64 lba_hpa, sba_hpa, length;
  400. int match;
  401. status = hp_acpi_csr_space(obj, &lba_hpa, &length);
  402. if (ACPI_FAILURE(status))
  403. return AE_OK; /* keep looking for another bridge */
  404. /* Look for an enclosing IOC scope and find its CSR space */
  405. handle = obj;
  406. do {
  407. status = acpi_get_object_info(handle, &info);
  408. if (ACPI_SUCCESS(status) && (info->valid & ACPI_VALID_HID)) {
  409. /* TBD check _CID also */
  410. match = (strcmp(info->hardware_id.string, "HWP0001") == 0);
  411. kfree(info);
  412. if (match) {
  413. status = hp_acpi_csr_space(handle, &sba_hpa, &length);
  414. if (ACPI_SUCCESS(status))
  415. break;
  416. else {
  417. printk(KERN_ERR PFX "Detected HP ZX1 "
  418. "AGP LBA but no IOC.\n");
  419. return AE_OK;
  420. }
  421. }
  422. }
  423. status = acpi_get_parent(handle, &parent);
  424. handle = parent;
  425. } while (ACPI_SUCCESS(status));
  426. if (ACPI_FAILURE(status))
  427. return AE_OK; /* found no enclosing IOC */
  428. if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
  429. return AE_OK;
  430. printk(KERN_INFO PFX "Detected HP ZX1 %s AGP chipset "
  431. "(ioc=%llx, lba=%llx)\n", (char *)context,
  432. sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa);
  433. hp_zx1_gart_found = 1;
  434. return AE_CTRL_TERMINATE; /* we only support one bridge; quit looking */
  435. }
  436. static int __init
  437. agp_hp_init (void)
  438. {
  439. if (agp_off)
  440. return -EINVAL;
  441. acpi_get_devices("HWP0003", zx1_gart_probe, "HWP0003", NULL);
  442. if (hp_zx1_gart_found)
  443. return 0;
  444. acpi_get_devices("HWP0007", zx1_gart_probe, "HWP0007", NULL);
  445. if (hp_zx1_gart_found)
  446. return 0;
  447. return -ENODEV;
  448. }
  449. static void __exit
  450. agp_hp_cleanup (void)
  451. {
  452. }
  453. module_init(agp_hp_init);
  454. module_exit(agp_hp_cleanup);
  455. MODULE_LICENSE("GPL and additional rights");