PageRenderTime 73ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/char/agp/hp-agp.c

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