hp-agp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 <asm/acpi-ext.h>
  18. #include "agp.h"
  19. #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
  20. /* HP ZX1 IOC registers */
  21. #define HP_ZX1_IBASE 0x300
  22. #define HP_ZX1_IMASK 0x308
  23. #define HP_ZX1_PCOM 0x310
  24. #define HP_ZX1_TCNFG 0x318
  25. #define HP_ZX1_PDIR_BASE 0x320
  26. #define HP_ZX1_IOVA_BASE GB(1UL)
  27. #define HP_ZX1_IOVA_SIZE GB(1UL)
  28. #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
  29. #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
  30. #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
  31. #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
  32. #define AGP8X_MODE_BIT 3
  33. #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
  34. /* AGP bridge need not be PCI device, but DRM thinks it is. */
  35. static struct pci_dev fake_bridge_dev;
  36. static int hp_zx1_gart_found;
  37. static struct aper_size_info_fixed hp_zx1_sizes[] =
  38. {
  39. {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
  40. };
  41. static struct gatt_mask hp_zx1_masks[] =
  42. {
  43. {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
  44. };
  45. static struct _hp_private {
  46. volatile u8 __iomem *ioc_regs;
  47. volatile u8 __iomem *lba_regs;
  48. int lba_cap_offset;
  49. u64 *io_pdir; // PDIR for entire IOVA
  50. u64 *gatt; // PDIR just for GART (subset of above)
  51. u64 gatt_entries;
  52. u64 iova_base;
  53. u64 gart_base;
  54. u64 gart_size;
  55. u64 io_pdir_size;
  56. int io_pdir_owner; // do we own it, or share it with sba_iommu?
  57. int io_page_size;
  58. int io_tlb_shift;
  59. int io_tlb_ps; // IOC ps config
  60. int io_pages_per_kpage;
  61. } hp_private;
  62. static int __init hp_zx1_ioc_shared(void)
  63. {
  64. struct _hp_private *hp = &hp_private;
  65. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
  66. /*
  67. * IOC already configured by sba_iommu module; just use
  68. * its setup. We assume:
  69. * - IOVA space is 1Gb in size
  70. * - first 512Mb is IOMMU, second 512Mb is GART
  71. */
  72. hp->io_tlb_ps = readq(hp->ioc_regs+HP_ZX1_TCNFG);
  73. switch (hp->io_tlb_ps) {
  74. case 0: hp->io_tlb_shift = 12; break;
  75. case 1: hp->io_tlb_shift = 13; break;
  76. case 2: hp->io_tlb_shift = 14; break;
  77. case 3: hp->io_tlb_shift = 16; break;
  78. default:
  79. printk(KERN_ERR PFX "Invalid IOTLB page size "
  80. "configuration 0x%x\n", hp->io_tlb_ps);
  81. hp->gatt = NULL;
  82. hp->gatt_entries = 0;
  83. return -ENODEV;
  84. }
  85. hp->io_page_size = 1 << hp->io_tlb_shift;
  86. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  87. hp->iova_base = readq(hp->ioc_regs+HP_ZX1_IBASE) & ~0x1;
  88. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
  89. hp->gart_size = HP_ZX1_GART_SIZE;
  90. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  91. hp->io_pdir = gart_to_virt(readq(hp->ioc_regs+HP_ZX1_PDIR_BASE));
  92. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  93. if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
  94. /* Normal case when no AGP device in system */
  95. hp->gatt = NULL;
  96. hp->gatt_entries = 0;
  97. printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
  98. "GART disabled\n");
  99. return -ENODEV;
  100. }
  101. return 0;
  102. }
  103. static int __init
  104. hp_zx1_ioc_owner (void)
  105. {
  106. struct _hp_private *hp = &hp_private;
  107. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
  108. /*
  109. * Select an IOV page size no larger than system page size.
  110. */
  111. if (PAGE_SIZE >= KB(64)) {
  112. hp->io_tlb_shift = 16;
  113. hp->io_tlb_ps = 3;
  114. } else if (PAGE_SIZE >= KB(16)) {
  115. hp->io_tlb_shift = 14;
  116. hp->io_tlb_ps = 2;
  117. } else if (PAGE_SIZE >= KB(8)) {
  118. hp->io_tlb_shift = 13;
  119. hp->io_tlb_ps = 1;
  120. } else {
  121. hp->io_tlb_shift = 12;
  122. hp->io_tlb_ps = 0;
  123. }
  124. hp->io_page_size = 1 << hp->io_tlb_shift;
  125. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  126. hp->iova_base = HP_ZX1_IOVA_BASE;
  127. hp->gart_size = HP_ZX1_GART_SIZE;
  128. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
  129. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  130. hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
  131. return 0;
  132. }
  133. static int __init
  134. hp_zx1_ioc_init (u64 hpa)
  135. {
  136. struct _hp_private *hp = &hp_private;
  137. hp->ioc_regs = ioremap(hpa, 1024);
  138. if (!hp->ioc_regs)
  139. return -ENOMEM;
  140. /*
  141. * If the IOTLB is currently disabled, we can take it over.
  142. * Otherwise, we have to share with sba_iommu.
  143. */
  144. hp->io_pdir_owner = (readq(hp->ioc_regs+HP_ZX1_IBASE) & 0x1) == 0;
  145. if (hp->io_pdir_owner)
  146. return hp_zx1_ioc_owner();
  147. return hp_zx1_ioc_shared();
  148. }
  149. static int
  150. hp_zx1_lba_find_capability (volatile u8 __iomem *hpa, int cap)
  151. {
  152. u16 status;
  153. u8 pos, id;
  154. int ttl = 48;
  155. status = readw(hpa+PCI_STATUS);
  156. if (!(status & PCI_STATUS_CAP_LIST))
  157. return 0;
  158. pos = readb(hpa+PCI_CAPABILITY_LIST);
  159. while (ttl-- && pos >= 0x40) {
  160. pos &= ~3;
  161. id = readb(hpa+pos+PCI_CAP_LIST_ID);
  162. if (id == 0xff)
  163. break;
  164. if (id == cap)
  165. return pos;
  166. pos = readb(hpa+pos+PCI_CAP_LIST_NEXT);
  167. }
  168. return 0;
  169. }
  170. static int __init
  171. hp_zx1_lba_init (u64 hpa)
  172. {
  173. struct _hp_private *hp = &hp_private;
  174. int cap;
  175. hp->lba_regs = ioremap(hpa, 256);
  176. if (!hp->lba_regs)
  177. return -ENOMEM;
  178. hp->lba_cap_offset = hp_zx1_lba_find_capability(hp->lba_regs, PCI_CAP_ID_AGP);
  179. cap = readl(hp->lba_regs+hp->lba_cap_offset) & 0xff;
  180. if (cap != PCI_CAP_ID_AGP) {
  181. printk(KERN_ERR PFX "Invalid capability ID 0x%02x at 0x%x\n",
  182. cap, hp->lba_cap_offset);
  183. iounmap(hp->lba_regs);
  184. return -ENODEV;
  185. }
  186. return 0;
  187. }
  188. static int
  189. hp_zx1_fetch_size(void)
  190. {
  191. int size;
  192. size = hp_private.gart_size / MB(1);
  193. hp_zx1_sizes[0].size = size;
  194. agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
  195. return size;
  196. }
  197. static int
  198. hp_zx1_configure (void)
  199. {
  200. struct _hp_private *hp = &hp_private;
  201. agp_bridge->gart_bus_addr = hp->gart_base;
  202. agp_bridge->capndx = hp->lba_cap_offset;
  203. agp_bridge->mode = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
  204. if (hp->io_pdir_owner) {
  205. writel(virt_to_gart(hp->io_pdir), hp->ioc_regs+HP_ZX1_PDIR_BASE);
  206. readl(hp->ioc_regs+HP_ZX1_PDIR_BASE);
  207. writel(hp->io_tlb_ps, hp->ioc_regs+HP_ZX1_TCNFG);
  208. readl(hp->ioc_regs+HP_ZX1_TCNFG);
  209. writel((unsigned int)(~(HP_ZX1_IOVA_SIZE-1)), hp->ioc_regs+HP_ZX1_IMASK);
  210. readl(hp->ioc_regs+HP_ZX1_IMASK);
  211. writel(hp->iova_base|1, hp->ioc_regs+HP_ZX1_IBASE);
  212. readl(hp->ioc_regs+HP_ZX1_IBASE);
  213. writel(hp->iova_base|ilog2(HP_ZX1_IOVA_SIZE), hp->ioc_regs+HP_ZX1_PCOM);
  214. readl(hp->ioc_regs+HP_ZX1_PCOM);
  215. }
  216. return 0;
  217. }
  218. static void
  219. hp_zx1_cleanup (void)
  220. {
  221. struct _hp_private *hp = &hp_private;
  222. if (hp->ioc_regs) {
  223. if (hp->io_pdir_owner) {
  224. writeq(0, hp->ioc_regs+HP_ZX1_IBASE);
  225. readq(hp->ioc_regs+HP_ZX1_IBASE);
  226. }
  227. iounmap(hp->ioc_regs);
  228. }
  229. if (hp->lba_regs)
  230. iounmap(hp->lba_regs);
  231. }
  232. static void
  233. hp_zx1_tlbflush (struct agp_memory *mem)
  234. {
  235. struct _hp_private *hp = &hp_private;
  236. writeq(hp->gart_base | ilog2(hp->gart_size), hp->ioc_regs+HP_ZX1_PCOM);
  237. readq(hp->ioc_regs+HP_ZX1_PCOM);
  238. }
  239. static int
  240. hp_zx1_create_gatt_table (struct agp_bridge_data *bridge)
  241. {
  242. struct _hp_private *hp = &hp_private;
  243. int i;
  244. if (hp->io_pdir_owner) {
  245. hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
  246. get_order(hp->io_pdir_size));
  247. if (!hp->io_pdir) {
  248. printk(KERN_ERR PFX "Couldn't allocate contiguous "
  249. "memory for I/O PDIR\n");
  250. hp->gatt = NULL;
  251. hp->gatt_entries = 0;
  252. return -ENOMEM;
  253. }
  254. memset(hp->io_pdir, 0, hp->io_pdir_size);
  255. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  256. }
  257. for (i = 0; i < hp->gatt_entries; i++) {
  258. hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
  259. }
  260. return 0;
  261. }
  262. static int
  263. hp_zx1_free_gatt_table (struct agp_bridge_data *bridge)
  264. {
  265. struct _hp_private *hp = &hp_private;
  266. if (hp->io_pdir_owner)
  267. free_pages((unsigned long) hp->io_pdir,
  268. get_order(hp->io_pdir_size));
  269. else
  270. hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
  271. return 0;
  272. }
  273. static int
  274. hp_zx1_insert_memory (struct agp_memory *mem, off_t pg_start, int type)
  275. {
  276. struct _hp_private *hp = &hp_private;
  277. int i, k;
  278. off_t j, io_pg_start;
  279. int io_pg_count;
  280. if (type != 0 || mem->type != 0) {
  281. return -EINVAL;
  282. }
  283. io_pg_start = hp->io_pages_per_kpage * pg_start;
  284. io_pg_count = hp->io_pages_per_kpage * mem->page_count;
  285. if ((io_pg_start + io_pg_count) > hp->gatt_entries) {
  286. return -EINVAL;
  287. }
  288. j = io_pg_start;
  289. while (j < (io_pg_start + io_pg_count)) {
  290. if (hp->gatt[j]) {
  291. return -EBUSY;
  292. }
  293. j++;
  294. }
  295. if (!mem->is_flushed) {
  296. global_cache_flush();
  297. mem->is_flushed = true;
  298. }
  299. for (i = 0, j = io_pg_start; i < mem->page_count; i++) {
  300. unsigned long paddr;
  301. paddr = mem->memory[i];
  302. for (k = 0;
  303. k < hp->io_pages_per_kpage;
  304. k++, j++, paddr += hp->io_page_size) {
  305. hp->gatt[j] =
  306. agp_bridge->driver->mask_memory(agp_bridge,
  307. paddr, type);
  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 != 0 || mem->type != 0) {
  319. return -EINVAL;
  320. }
  321. io_pg_start = hp->io_pages_per_kpage * pg_start;
  322. io_pg_count = hp->io_pages_per_kpage * mem->page_count;
  323. for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
  324. hp->gatt[i] = agp_bridge->scratch_page;
  325. }
  326. agp_bridge->driver->tlb_flush(mem);
  327. return 0;
  328. }
  329. static unsigned long
  330. hp_zx1_mask_memory (struct agp_bridge_data *bridge,
  331. unsigned long 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_buffer buffer;
  402. struct acpi_device_info *info;
  403. u64 lba_hpa, sba_hpa, length;
  404. int match;
  405. status = hp_acpi_csr_space(obj, &lba_hpa, &length);
  406. if (ACPI_FAILURE(status))
  407. return AE_OK; /* keep looking for another bridge */
  408. /* Look for an enclosing IOC scope and find its CSR space */
  409. handle = obj;
  410. do {
  411. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  412. status = acpi_get_object_info(handle, &buffer);
  413. if (ACPI_SUCCESS(status)) {
  414. /* TBD check _CID also */
  415. info = buffer.pointer;
  416. info->hardware_id.value[sizeof(info->hardware_id)-1] = '\0';
  417. match = (strcmp(info->hardware_id.value, "HWP0001") == 0);
  418. kfree(info);
  419. if (match) {
  420. status = hp_acpi_csr_space(handle, &sba_hpa, &length);
  421. if (ACPI_SUCCESS(status))
  422. break;
  423. else {
  424. printk(KERN_ERR PFX "Detected HP ZX1 "
  425. "AGP LBA but no IOC.\n");
  426. return AE_OK;
  427. }
  428. }
  429. }
  430. status = acpi_get_parent(handle, &parent);
  431. handle = parent;
  432. } while (ACPI_SUCCESS(status));
  433. if (hp_zx1_setup(sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa))
  434. return AE_OK;
  435. printk(KERN_INFO PFX "Detected HP ZX1 %s AGP chipset (ioc=%lx, lba=%lx)\n",
  436. (char *) context, sba_hpa + HP_ZX1_IOC_OFFSET, lba_hpa);
  437. hp_zx1_gart_found = 1;
  438. return AE_CTRL_TERMINATE; /* we only support one bridge; quit looking */
  439. }
  440. static int __init
  441. agp_hp_init (void)
  442. {
  443. if (agp_off)
  444. return -EINVAL;
  445. acpi_get_devices("HWP0003", zx1_gart_probe, "HWP0003", NULL);
  446. if (hp_zx1_gart_found)
  447. return 0;
  448. acpi_get_devices("HWP0007", zx1_gart_probe, "HWP0007", NULL);
  449. if (hp_zx1_gart_found)
  450. return 0;
  451. return -ENODEV;
  452. }
  453. static void __exit
  454. agp_hp_cleanup (void)
  455. {
  456. }
  457. module_init(agp_hp_init);
  458. module_exit(agp_hp_cleanup);
  459. MODULE_LICENSE("GPL and additional rights");