hp-agp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 <asm/acpi-ext.h>
  17. #include "agp.h"
  18. #ifndef log2
  19. #define log2(x) ffz(~(x))
  20. #endif
  21. #define HP_ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
  22. /* HP ZX1 IOC registers */
  23. #define HP_ZX1_IBASE 0x300
  24. #define HP_ZX1_IMASK 0x308
  25. #define HP_ZX1_PCOM 0x310
  26. #define HP_ZX1_TCNFG 0x318
  27. #define HP_ZX1_PDIR_BASE 0x320
  28. #define HP_ZX1_IOVA_BASE GB(1UL)
  29. #define HP_ZX1_IOVA_SIZE GB(1UL)
  30. #define HP_ZX1_GART_SIZE (HP_ZX1_IOVA_SIZE / 2)
  31. #define HP_ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
  32. #define HP_ZX1_PDIR_VALID_BIT 0x8000000000000000UL
  33. #define HP_ZX1_IOVA_TO_PDIR(va) ((va - hp_private.iova_base) >> hp_private.io_tlb_shift)
  34. #define AGP8X_MODE_BIT 3
  35. #define AGP8X_MODE (1 << AGP8X_MODE_BIT)
  36. /* AGP bridge need not be PCI device, but DRM thinks it is. */
  37. static struct pci_dev fake_bridge_dev;
  38. static int hp_zx1_gart_found;
  39. static struct aper_size_info_fixed hp_zx1_sizes[] =
  40. {
  41. {0, 0, 0}, /* filled in by hp_zx1_fetch_size() */
  42. };
  43. static struct gatt_mask hp_zx1_masks[] =
  44. {
  45. {.mask = HP_ZX1_PDIR_VALID_BIT, .type = 0}
  46. };
  47. static struct _hp_private {
  48. volatile u8 __iomem *ioc_regs;
  49. volatile u8 __iomem *lba_regs;
  50. int lba_cap_offset;
  51. u64 *io_pdir; // PDIR for entire IOVA
  52. u64 *gatt; // PDIR just for GART (subset of above)
  53. u64 gatt_entries;
  54. u64 iova_base;
  55. u64 gart_base;
  56. u64 gart_size;
  57. u64 io_pdir_size;
  58. int io_pdir_owner; // do we own it, or share it with sba_iommu?
  59. int io_page_size;
  60. int io_tlb_shift;
  61. int io_tlb_ps; // IOC ps config
  62. int io_pages_per_kpage;
  63. } hp_private;
  64. static int __init hp_zx1_ioc_shared(void)
  65. {
  66. struct _hp_private *hp = &hp_private;
  67. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR shared with sba_iommu\n");
  68. /*
  69. * IOC already configured by sba_iommu module; just use
  70. * its setup. We assume:
  71. * - IOVA space is 1Gb in size
  72. * - first 512Mb is IOMMU, second 512Mb is GART
  73. */
  74. hp->io_tlb_ps = readq(hp->ioc_regs+HP_ZX1_TCNFG);
  75. switch (hp->io_tlb_ps) {
  76. case 0: hp->io_tlb_shift = 12; break;
  77. case 1: hp->io_tlb_shift = 13; break;
  78. case 2: hp->io_tlb_shift = 14; break;
  79. case 3: hp->io_tlb_shift = 16; break;
  80. default:
  81. printk(KERN_ERR PFX "Invalid IOTLB page size "
  82. "configuration 0x%x\n", hp->io_tlb_ps);
  83. hp->gatt = NULL;
  84. hp->gatt_entries = 0;
  85. return -ENODEV;
  86. }
  87. hp->io_page_size = 1 << hp->io_tlb_shift;
  88. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  89. hp->iova_base = readq(hp->ioc_regs+HP_ZX1_IBASE) & ~0x1;
  90. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - HP_ZX1_GART_SIZE;
  91. hp->gart_size = HP_ZX1_GART_SIZE;
  92. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  93. hp->io_pdir = gart_to_virt(readq(hp->ioc_regs+HP_ZX1_PDIR_BASE));
  94. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  95. if (hp->gatt[0] != HP_ZX1_SBA_IOMMU_COOKIE) {
  96. /* Normal case when no AGP device in system */
  97. hp->gatt = NULL;
  98. hp->gatt_entries = 0;
  99. printk(KERN_ERR PFX "No reserved IO PDIR entry found; "
  100. "GART disabled\n");
  101. return -ENODEV;
  102. }
  103. return 0;
  104. }
  105. static int __init
  106. hp_zx1_ioc_owner (void)
  107. {
  108. struct _hp_private *hp = &hp_private;
  109. printk(KERN_INFO PFX "HP ZX1 IOC: IOPDIR dedicated to GART\n");
  110. /*
  111. * Select an IOV page size no larger than system page size.
  112. */
  113. if (PAGE_SIZE >= KB(64)) {
  114. hp->io_tlb_shift = 16;
  115. hp->io_tlb_ps = 3;
  116. } else if (PAGE_SIZE >= KB(16)) {
  117. hp->io_tlb_shift = 14;
  118. hp->io_tlb_ps = 2;
  119. } else if (PAGE_SIZE >= KB(8)) {
  120. hp->io_tlb_shift = 13;
  121. hp->io_tlb_ps = 1;
  122. } else {
  123. hp->io_tlb_shift = 12;
  124. hp->io_tlb_ps = 0;
  125. }
  126. hp->io_page_size = 1 << hp->io_tlb_shift;
  127. hp->io_pages_per_kpage = PAGE_SIZE / hp->io_page_size;
  128. hp->iova_base = HP_ZX1_IOVA_BASE;
  129. hp->gart_size = HP_ZX1_GART_SIZE;
  130. hp->gart_base = hp->iova_base + HP_ZX1_IOVA_SIZE - hp->gart_size;
  131. hp->gatt_entries = hp->gart_size / hp->io_page_size;
  132. hp->io_pdir_size = (HP_ZX1_IOVA_SIZE / hp->io_page_size) * sizeof(u64);
  133. return 0;
  134. }
  135. static int __init
  136. hp_zx1_ioc_init (u64 hpa)
  137. {
  138. struct _hp_private *hp = &hp_private;
  139. hp->ioc_regs = ioremap(hpa, 1024);
  140. if (!hp->ioc_regs)
  141. return -ENOMEM;
  142. /*
  143. * If the IOTLB is currently disabled, we can take it over.
  144. * Otherwise, we have to share with sba_iommu.
  145. */
  146. hp->io_pdir_owner = (readq(hp->ioc_regs+HP_ZX1_IBASE) & 0x1) == 0;
  147. if (hp->io_pdir_owner)
  148. return hp_zx1_ioc_owner();
  149. return hp_zx1_ioc_shared();
  150. }
  151. static int
  152. hp_zx1_lba_find_capability (volatile u8 __iomem *hpa, int cap)
  153. {
  154. u16 status;
  155. u8 pos, id;
  156. int ttl = 48;
  157. status = readw(hpa+PCI_STATUS);
  158. if (!(status & PCI_STATUS_CAP_LIST))
  159. return 0;
  160. pos = readb(hpa+PCI_CAPABILITY_LIST);
  161. while (ttl-- && pos >= 0x40) {
  162. pos &= ~3;
  163. id = readb(hpa+pos+PCI_CAP_LIST_ID);
  164. if (id == 0xff)
  165. break;
  166. if (id == cap)
  167. return pos;
  168. pos = readb(hpa+pos+PCI_CAP_LIST_NEXT);
  169. }
  170. return 0;
  171. }
  172. static int __init
  173. hp_zx1_lba_init (u64 hpa)
  174. {
  175. struct _hp_private *hp = &hp_private;
  176. int cap;
  177. hp->lba_regs = ioremap(hpa, 256);
  178. if (!hp->lba_regs)
  179. return -ENOMEM;
  180. hp->lba_cap_offset = hp_zx1_lba_find_capability(hp->lba_regs, PCI_CAP_ID_AGP);
  181. cap = readl(hp->lba_regs+hp->lba_cap_offset) & 0xff;
  182. if (cap != PCI_CAP_ID_AGP) {
  183. printk(KERN_ERR PFX "Invalid capability ID 0x%02x at 0x%x\n",
  184. cap, hp->lba_cap_offset);
  185. iounmap(hp->lba_regs);
  186. return -ENODEV;
  187. }
  188. return 0;
  189. }
  190. static int
  191. hp_zx1_fetch_size(void)
  192. {
  193. int size;
  194. size = hp_private.gart_size / MB(1);
  195. hp_zx1_sizes[0].size = size;
  196. agp_bridge->current_size = (void *) &hp_zx1_sizes[0];
  197. return size;
  198. }
  199. static int
  200. hp_zx1_configure (void)
  201. {
  202. struct _hp_private *hp = &hp_private;
  203. agp_bridge->gart_bus_addr = hp->gart_base;
  204. agp_bridge->capndx = hp->lba_cap_offset;
  205. agp_bridge->mode = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
  206. if (hp->io_pdir_owner) {
  207. writel(virt_to_gart(hp->io_pdir), hp->ioc_regs+HP_ZX1_PDIR_BASE);
  208. readl(hp->ioc_regs+HP_ZX1_PDIR_BASE);
  209. writel(hp->io_tlb_ps, hp->ioc_regs+HP_ZX1_TCNFG);
  210. readl(hp->ioc_regs+HP_ZX1_TCNFG);
  211. writel((unsigned int)(~(HP_ZX1_IOVA_SIZE-1)), hp->ioc_regs+HP_ZX1_IMASK);
  212. readl(hp->ioc_regs+HP_ZX1_IMASK);
  213. writel(hp->iova_base|1, hp->ioc_regs+HP_ZX1_IBASE);
  214. readl(hp->ioc_regs+HP_ZX1_IBASE);
  215. writel(hp->iova_base|log2(HP_ZX1_IOVA_SIZE), hp->ioc_regs+HP_ZX1_PCOM);
  216. readl(hp->ioc_regs+HP_ZX1_PCOM);
  217. }
  218. return 0;
  219. }
  220. static void
  221. hp_zx1_cleanup (void)
  222. {
  223. struct _hp_private *hp = &hp_private;
  224. if (hp->ioc_regs) {
  225. if (hp->io_pdir_owner) {
  226. writeq(0, hp->ioc_regs+HP_ZX1_IBASE);
  227. readq(hp->ioc_regs+HP_ZX1_IBASE);
  228. }
  229. iounmap(hp->ioc_regs);
  230. }
  231. if (hp->lba_regs)
  232. iounmap(hp->lba_regs);
  233. }
  234. static void
  235. hp_zx1_tlbflush (struct agp_memory *mem)
  236. {
  237. struct _hp_private *hp = &hp_private;
  238. writeq(hp->gart_base | log2(hp->gart_size), hp->ioc_regs+HP_ZX1_PCOM);
  239. readq(hp->ioc_regs+HP_ZX1_PCOM);
  240. }
  241. static int
  242. hp_zx1_create_gatt_table (struct agp_bridge_data *bridge)
  243. {
  244. struct _hp_private *hp = &hp_private;
  245. int i;
  246. if (hp->io_pdir_owner) {
  247. hp->io_pdir = (u64 *) __get_free_pages(GFP_KERNEL,
  248. get_order(hp->io_pdir_size));
  249. if (!hp->io_pdir) {
  250. printk(KERN_ERR PFX "Couldn't allocate contiguous "
  251. "memory for I/O PDIR\n");
  252. hp->gatt = NULL;
  253. hp->gatt_entries = 0;
  254. return -ENOMEM;
  255. }
  256. memset(hp->io_pdir, 0, hp->io_pdir_size);
  257. hp->gatt = &hp->io_pdir[HP_ZX1_IOVA_TO_PDIR(hp->gart_base)];
  258. }
  259. for (i = 0; i < hp->gatt_entries; i++) {
  260. hp->gatt[i] = (unsigned long) agp_bridge->scratch_page;
  261. }
  262. return 0;
  263. }
  264. static int
  265. hp_zx1_free_gatt_table (struct agp_bridge_data *bridge)
  266. {
  267. struct _hp_private *hp = &hp_private;
  268. if (hp->io_pdir_owner)
  269. free_pages((unsigned long) hp->io_pdir,
  270. get_order(hp->io_pdir_size));
  271. else
  272. hp->gatt[0] = HP_ZX1_SBA_IOMMU_COOKIE;
  273. return 0;
  274. }
  275. static int
  276. hp_zx1_insert_memory (struct agp_memory *mem, off_t pg_start, int type)
  277. {
  278. struct _hp_private *hp = &hp_private;
  279. int i, k;
  280. off_t j, io_pg_start;
  281. int io_pg_count;
  282. if (type != 0 || mem->type != 0) {
  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 == FALSE) {
  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 = mem->memory[i];
  304. for (k = 0;
  305. k < hp->io_pages_per_kpage;
  306. k++, j++, paddr += hp->io_page_size) {
  307. hp->gatt[j] =
  308. agp_bridge->driver->mask_memory(agp_bridge,
  309. paddr, type);
  310. }
  311. }
  312. agp_bridge->driver->tlb_flush(mem);
  313. return 0;
  314. }
  315. static int
  316. hp_zx1_remove_memory (struct agp_memory *mem, off_t pg_start, int type)
  317. {
  318. struct _hp_private *hp = &hp_private;
  319. int i, io_pg_start, io_pg_count;
  320. if (type != 0 || mem->type != 0) {
  321. return -EINVAL;
  322. }
  323. io_pg_start = hp->io_pages_per_kpage * pg_start;
  324. io_pg_count = hp->io_pages_per_kpage * mem->page_count;
  325. for (i = io_pg_start; i < io_pg_count + io_pg_start; i++) {
  326. hp->gatt[i] = agp_bridge->scratch_page;
  327. }
  328. agp_bridge->driver->tlb_flush(mem);
  329. return 0;
  330. }
  331. static unsigned long
  332. hp_zx1_mask_memory (struct agp_bridge_data *bridge,
  333. unsigned long addr, int type)
  334. {
  335. return HP_ZX1_PDIR_VALID_BIT | addr;
  336. }
  337. static void
  338. hp_zx1_enable (struct agp_bridge_data *bridge, u32 mode)
  339. {
  340. struct _hp_private *hp = &hp_private;
  341. u32 command;
  342. command = readl(hp->lba_regs+hp->lba_cap_offset+PCI_AGP_STATUS);
  343. command = agp_collect_device_status(bridge, mode, command);
  344. command |= 0x00000100;
  345. writel(command, hp->lba_regs+hp->lba_cap_offset+PCI_AGP_COMMAND);
  346. agp_device_command(command, (mode & AGP8X_MODE) != 0);
  347. }
  348. const struct agp_bridge_driver hp_zx1_driver = {
  349. .owner = THIS_MODULE,
  350. .size_type = FIXED_APER_SIZE,
  351. .configure = hp_zx1_configure,
  352. .fetch_size = hp_zx1_fetch_size,
  353. .cleanup = hp_zx1_cleanup,
  354. .tlb_flush = hp_zx1_tlbflush,
  355. .mask_memory = hp_zx1_mask_memory,
  356. .masks = hp_zx1_masks,
  357. .agp_enable = hp_zx1_enable,
  358. .cache_flush = global_cache_flush,
  359. .create_gatt_table = hp_zx1_create_gatt_table,
  360. .free_gatt_table = hp_zx1_free_gatt_table,
  361. .insert_memory = hp_zx1_insert_memory,
  362. .remove_memory = hp_zx1_remove_memory,
  363. .alloc_by_type = agp_generic_alloc_by_type,
  364. .free_by_type = agp_generic_free_by_type,
  365. .agp_alloc_page = agp_generic_alloc_page,
  366. .agp_destroy_page = agp_generic_destroy_page,
  367. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  368. .cant_use_aperture = 1,
  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");