pci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * pci.c - Low-Level PCI Access in IA-64
  3. *
  4. * Derived from bios32.c of i386 tree.
  5. *
  6. * (c) Copyright 2002, 2005 Hewlett-Packard Development Company, L.P.
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  9. * Copyright (C) 2004 Silicon Graphics, Inc.
  10. *
  11. * Note: Above list of copyright holders is incomplete...
  12. */
  13. #include <linux/config.h>
  14. #include <linux/acpi.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/pci.h>
  18. #include <linux/init.h>
  19. #include <linux/ioport.h>
  20. #include <linux/slab.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/machvec.h>
  24. #include <asm/page.h>
  25. #include <asm/system.h>
  26. #include <asm/io.h>
  27. #include <asm/sal.h>
  28. #include <asm/smp.h>
  29. #include <asm/irq.h>
  30. #include <asm/hw_irq.h>
  31. /*
  32. * Low-level SAL-based PCI configuration access functions. Note that SAL
  33. * calls are already serialized (via sal_lock), so we don't need another
  34. * synchronization mechanism here.
  35. */
  36. #define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
  37. (((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
  38. /* SAL 3.2 adds support for extended config space. */
  39. #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg) \
  40. (((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
  41. static int
  42. pci_sal_read (unsigned int seg, unsigned int bus, unsigned int devfn,
  43. int reg, int len, u32 *value)
  44. {
  45. u64 addr, data = 0;
  46. int mode, result;
  47. if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
  48. return -EINVAL;
  49. if ((seg | reg) <= 255) {
  50. addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
  51. mode = 0;
  52. } else {
  53. addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
  54. mode = 1;
  55. }
  56. result = ia64_sal_pci_config_read(addr, mode, len, &data);
  57. if (result != 0)
  58. return -EINVAL;
  59. *value = (u32) data;
  60. return 0;
  61. }
  62. static int
  63. pci_sal_write (unsigned int seg, unsigned int bus, unsigned int devfn,
  64. int reg, int len, u32 value)
  65. {
  66. u64 addr;
  67. int mode, result;
  68. if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
  69. return -EINVAL;
  70. if ((seg | reg) <= 255) {
  71. addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
  72. mode = 0;
  73. } else {
  74. addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
  75. mode = 1;
  76. }
  77. result = ia64_sal_pci_config_write(addr, mode, len, value);
  78. if (result != 0)
  79. return -EINVAL;
  80. return 0;
  81. }
  82. static struct pci_raw_ops pci_sal_ops = {
  83. .read = pci_sal_read,
  84. .write = pci_sal_write
  85. };
  86. struct pci_raw_ops *raw_pci_ops = &pci_sal_ops;
  87. static int
  88. pci_read (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  89. {
  90. return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
  91. devfn, where, size, value);
  92. }
  93. static int
  94. pci_write (struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  95. {
  96. return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
  97. devfn, where, size, value);
  98. }
  99. struct pci_ops pci_root_ops = {
  100. .read = pci_read,
  101. .write = pci_write,
  102. };
  103. #ifdef CONFIG_NUMA
  104. extern acpi_status acpi_map_iosapic(acpi_handle, u32, void *, void **);
  105. static void acpi_map_iosapics(void)
  106. {
  107. acpi_get_devices(NULL, acpi_map_iosapic, NULL, NULL);
  108. }
  109. #else
  110. static void acpi_map_iosapics(void)
  111. {
  112. return;
  113. }
  114. #endif /* CONFIG_NUMA */
  115. static int __init
  116. pci_acpi_init (void)
  117. {
  118. acpi_map_iosapics();
  119. return 0;
  120. }
  121. subsys_initcall(pci_acpi_init);
  122. /* Called by ACPI when it finds a new root bus. */
  123. static struct pci_controller * __devinit
  124. alloc_pci_controller (int seg)
  125. {
  126. struct pci_controller *controller;
  127. controller = kmalloc(sizeof(*controller), GFP_KERNEL);
  128. if (!controller)
  129. return NULL;
  130. memset(controller, 0, sizeof(*controller));
  131. controller->segment = seg;
  132. controller->node = -1;
  133. return controller;
  134. }
  135. static u64 __devinit
  136. add_io_space (struct acpi_resource_address64 *addr)
  137. {
  138. u64 offset;
  139. int sparse = 0;
  140. int i;
  141. if (addr->address_translation_offset == 0)
  142. return IO_SPACE_BASE(0); /* part of legacy IO space */
  143. if (addr->attribute.io.translation_attribute == ACPI_SPARSE_TRANSLATION)
  144. sparse = 1;
  145. offset = (u64) ioremap(addr->address_translation_offset, 0);
  146. for (i = 0; i < num_io_spaces; i++)
  147. if (io_space[i].mmio_base == offset &&
  148. io_space[i].sparse == sparse)
  149. return IO_SPACE_BASE(i);
  150. if (num_io_spaces == MAX_IO_SPACES) {
  151. printk("Too many IO port spaces\n");
  152. return ~0;
  153. }
  154. i = num_io_spaces++;
  155. io_space[i].mmio_base = offset;
  156. io_space[i].sparse = sparse;
  157. return IO_SPACE_BASE(i);
  158. }
  159. static acpi_status __devinit resource_to_window(struct acpi_resource *resource,
  160. struct acpi_resource_address64 *addr)
  161. {
  162. acpi_status status;
  163. /*
  164. * We're only interested in _CRS descriptors that are
  165. * - address space descriptors for memory or I/O space
  166. * - non-zero size
  167. * - producers, i.e., the address space is routed downstream,
  168. * not consumed by the bridge itself
  169. */
  170. status = acpi_resource_to_address64(resource, addr);
  171. if (ACPI_SUCCESS(status) &&
  172. (addr->resource_type == ACPI_MEMORY_RANGE ||
  173. addr->resource_type == ACPI_IO_RANGE) &&
  174. addr->address_length &&
  175. addr->producer_consumer == ACPI_PRODUCER)
  176. return AE_OK;
  177. return AE_ERROR;
  178. }
  179. static acpi_status __devinit
  180. count_window (struct acpi_resource *resource, void *data)
  181. {
  182. unsigned int *windows = (unsigned int *) data;
  183. struct acpi_resource_address64 addr;
  184. acpi_status status;
  185. status = resource_to_window(resource, &addr);
  186. if (ACPI_SUCCESS(status))
  187. (*windows)++;
  188. return AE_OK;
  189. }
  190. struct pci_root_info {
  191. struct pci_controller *controller;
  192. char *name;
  193. };
  194. static __devinit acpi_status add_window(struct acpi_resource *res, void *data)
  195. {
  196. struct pci_root_info *info = data;
  197. struct pci_window *window;
  198. struct acpi_resource_address64 addr;
  199. acpi_status status;
  200. unsigned long flags, offset = 0;
  201. struct resource *root;
  202. /* Return AE_OK for non-window resources to keep scanning for more */
  203. status = resource_to_window(res, &addr);
  204. if (!ACPI_SUCCESS(status))
  205. return AE_OK;
  206. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  207. flags = IORESOURCE_MEM;
  208. root = &iomem_resource;
  209. offset = addr.address_translation_offset;
  210. } else if (addr.resource_type == ACPI_IO_RANGE) {
  211. flags = IORESOURCE_IO;
  212. root = &ioport_resource;
  213. offset = add_io_space(&addr);
  214. if (offset == ~0)
  215. return AE_OK;
  216. } else
  217. return AE_OK;
  218. window = &info->controller->window[info->controller->windows++];
  219. window->resource.name = info->name;
  220. window->resource.flags = flags;
  221. window->resource.start = addr.min_address_range + offset;
  222. window->resource.end = addr.max_address_range + offset;
  223. window->resource.child = NULL;
  224. window->offset = offset;
  225. if (insert_resource(root, &window->resource)) {
  226. printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
  227. window->resource.start, window->resource.end,
  228. root->name, info->name);
  229. }
  230. return AE_OK;
  231. }
  232. static void __devinit
  233. pcibios_setup_root_windows(struct pci_bus *bus, struct pci_controller *ctrl)
  234. {
  235. int i, j;
  236. j = 0;
  237. for (i = 0; i < ctrl->windows; i++) {
  238. struct resource *res = &ctrl->window[i].resource;
  239. /* HP's firmware has a hack to work around a Windows bug.
  240. * Ignore these tiny memory ranges */
  241. if ((res->flags & IORESOURCE_MEM) &&
  242. (res->end - res->start < 16))
  243. continue;
  244. if (j >= PCI_BUS_NUM_RESOURCES) {
  245. printk("Ignoring range [%lx-%lx] (%lx)\n", res->start,
  246. res->end, res->flags);
  247. continue;
  248. }
  249. bus->resource[j++] = res;
  250. }
  251. }
  252. struct pci_bus * __devinit
  253. pci_acpi_scan_root(struct acpi_device *device, int domain, int bus)
  254. {
  255. struct pci_root_info info;
  256. struct pci_controller *controller;
  257. unsigned int windows = 0;
  258. struct pci_bus *pbus;
  259. char *name;
  260. int pxm;
  261. controller = alloc_pci_controller(domain);
  262. if (!controller)
  263. goto out1;
  264. controller->acpi_handle = device->handle;
  265. pxm = acpi_get_pxm(controller->acpi_handle);
  266. #ifdef CONFIG_NUMA
  267. if (pxm >= 0)
  268. controller->node = pxm_to_nid_map[pxm];
  269. #endif
  270. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
  271. &windows);
  272. controller->window = kmalloc_node(sizeof(*controller->window) * windows,
  273. GFP_KERNEL, controller->node);
  274. if (!controller->window)
  275. goto out2;
  276. name = kmalloc(16, GFP_KERNEL);
  277. if (!name)
  278. goto out3;
  279. sprintf(name, "PCI Bus %04x:%02x", domain, bus);
  280. info.controller = controller;
  281. info.name = name;
  282. acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window,
  283. &info);
  284. pbus = pci_scan_bus_parented(NULL, bus, &pci_root_ops, controller);
  285. if (pbus)
  286. pcibios_setup_root_windows(pbus, controller);
  287. return pbus;
  288. out3:
  289. kfree(controller->window);
  290. out2:
  291. kfree(controller);
  292. out1:
  293. return NULL;
  294. }
  295. void pcibios_resource_to_bus(struct pci_dev *dev,
  296. struct pci_bus_region *region, struct resource *res)
  297. {
  298. struct pci_controller *controller = PCI_CONTROLLER(dev);
  299. unsigned long offset = 0;
  300. int i;
  301. for (i = 0; i < controller->windows; i++) {
  302. struct pci_window *window = &controller->window[i];
  303. if (!(window->resource.flags & res->flags))
  304. continue;
  305. if (window->resource.start > res->start)
  306. continue;
  307. if (window->resource.end < res->end)
  308. continue;
  309. offset = window->offset;
  310. break;
  311. }
  312. region->start = res->start - offset;
  313. region->end = res->end - offset;
  314. }
  315. EXPORT_SYMBOL(pcibios_resource_to_bus);
  316. void pcibios_bus_to_resource(struct pci_dev *dev,
  317. struct resource *res, struct pci_bus_region *region)
  318. {
  319. struct pci_controller *controller = PCI_CONTROLLER(dev);
  320. unsigned long offset = 0;
  321. int i;
  322. for (i = 0; i < controller->windows; i++) {
  323. struct pci_window *window = &controller->window[i];
  324. if (!(window->resource.flags & res->flags))
  325. continue;
  326. if (window->resource.start - window->offset > region->start)
  327. continue;
  328. if (window->resource.end - window->offset < region->end)
  329. continue;
  330. offset = window->offset;
  331. break;
  332. }
  333. res->start = region->start + offset;
  334. res->end = region->end + offset;
  335. }
  336. EXPORT_SYMBOL(pcibios_bus_to_resource);
  337. static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
  338. {
  339. unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  340. struct resource *devr = &dev->resource[idx];
  341. if (!dev->bus)
  342. return 0;
  343. for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
  344. struct resource *busr = dev->bus->resource[i];
  345. if (!busr || ((busr->flags ^ devr->flags) & type_mask))
  346. continue;
  347. if ((devr->start) && (devr->start >= busr->start) &&
  348. (devr->end <= busr->end))
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
  354. {
  355. struct pci_bus_region region;
  356. int i;
  357. int limit = (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) ? \
  358. PCI_BRIDGE_RESOURCES : PCI_NUM_RESOURCES;
  359. for (i = 0; i < limit; i++) {
  360. if (!dev->resource[i].flags)
  361. continue;
  362. region.start = dev->resource[i].start;
  363. region.end = dev->resource[i].end;
  364. pcibios_bus_to_resource(dev, &dev->resource[i], &region);
  365. if ((is_valid_resource(dev, i)))
  366. pci_claim_resource(dev, i);
  367. }
  368. }
  369. /*
  370. * Called after each bus is probed, but before its children are examined.
  371. */
  372. void __devinit
  373. pcibios_fixup_bus (struct pci_bus *b)
  374. {
  375. struct pci_dev *dev;
  376. if (b->self) {
  377. pci_read_bridge_bases(b);
  378. pcibios_fixup_device_resources(b->self);
  379. }
  380. list_for_each_entry(dev, &b->devices, bus_list)
  381. pcibios_fixup_device_resources(dev);
  382. return;
  383. }
  384. void __devinit
  385. pcibios_update_irq (struct pci_dev *dev, int irq)
  386. {
  387. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  388. /* ??? FIXME -- record old value for shutdown. */
  389. }
  390. static inline int
  391. pcibios_enable_resources (struct pci_dev *dev, int mask)
  392. {
  393. u16 cmd, old_cmd;
  394. int idx;
  395. struct resource *r;
  396. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  397. if (!dev)
  398. return -EINVAL;
  399. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  400. old_cmd = cmd;
  401. for (idx=0; idx<PCI_NUM_RESOURCES; idx++) {
  402. /* Only set up the desired resources. */
  403. if (!(mask & (1 << idx)))
  404. continue;
  405. r = &dev->resource[idx];
  406. if (!(r->flags & type_mask))
  407. continue;
  408. if ((idx == PCI_ROM_RESOURCE) &&
  409. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  410. continue;
  411. if (!r->start && r->end) {
  412. printk(KERN_ERR
  413. "PCI: Device %s not available because of resource collisions\n",
  414. pci_name(dev));
  415. return -EINVAL;
  416. }
  417. if (r->flags & IORESOURCE_IO)
  418. cmd |= PCI_COMMAND_IO;
  419. if (r->flags & IORESOURCE_MEM)
  420. cmd |= PCI_COMMAND_MEMORY;
  421. }
  422. if (cmd != old_cmd) {
  423. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  424. pci_write_config_word(dev, PCI_COMMAND, cmd);
  425. }
  426. return 0;
  427. }
  428. int
  429. pcibios_enable_device (struct pci_dev *dev, int mask)
  430. {
  431. int ret;
  432. ret = pcibios_enable_resources(dev, mask);
  433. if (ret < 0)
  434. return ret;
  435. return acpi_pci_irq_enable(dev);
  436. }
  437. void
  438. pcibios_disable_device (struct pci_dev *dev)
  439. {
  440. acpi_pci_irq_disable(dev);
  441. }
  442. void
  443. pcibios_align_resource (void *data, struct resource *res,
  444. unsigned long size, unsigned long align)
  445. {
  446. }
  447. /*
  448. * PCI BIOS setup, always defaults to SAL interface
  449. */
  450. char * __init
  451. pcibios_setup (char *str)
  452. {
  453. return NULL;
  454. }
  455. int
  456. pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
  457. enum pci_mmap_state mmap_state, int write_combine)
  458. {
  459. /*
  460. * I/O space cannot be accessed via normal processor loads and
  461. * stores on this platform.
  462. */
  463. if (mmap_state == pci_mmap_io)
  464. /*
  465. * XXX we could relax this for I/O spaces for which ACPI
  466. * indicates that the space is 1-to-1 mapped. But at the
  467. * moment, we don't support multiple PCI address spaces and
  468. * the legacy I/O space is not 1-to-1 mapped, so this is moot.
  469. */
  470. return -EINVAL;
  471. /*
  472. * Leave vm_pgoff as-is, the PCI space address is the physical
  473. * address on this platform.
  474. */
  475. vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
  476. if (write_combine && efi_range_is_wc(vma->vm_start,
  477. vma->vm_end - vma->vm_start))
  478. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  479. else
  480. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  481. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  482. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  483. return -EAGAIN;
  484. return 0;
  485. }
  486. /**
  487. * ia64_pci_get_legacy_mem - generic legacy mem routine
  488. * @bus: bus to get legacy memory base address for
  489. *
  490. * Find the base of legacy memory for @bus. This is typically the first
  491. * megabyte of bus address space for @bus or is simply 0 on platforms whose
  492. * chipsets support legacy I/O and memory routing. Returns the base address
  493. * or an error pointer if an error occurred.
  494. *
  495. * This is the ia64 generic version of this routine. Other platforms
  496. * are free to override it with a machine vector.
  497. */
  498. char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
  499. {
  500. return (char *)__IA64_UNCACHED_OFFSET;
  501. }
  502. /**
  503. * pci_mmap_legacy_page_range - map legacy memory space to userland
  504. * @bus: bus whose legacy space we're mapping
  505. * @vma: vma passed in by mmap
  506. *
  507. * Map legacy memory space for this device back to userspace using a machine
  508. * vector to get the base address.
  509. */
  510. int
  511. pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma)
  512. {
  513. char *addr;
  514. addr = pci_get_legacy_mem(bus);
  515. if (IS_ERR(addr))
  516. return PTR_ERR(addr);
  517. vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
  518. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  519. vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
  520. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  521. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  522. return -EAGAIN;
  523. return 0;
  524. }
  525. /**
  526. * ia64_pci_legacy_read - read from legacy I/O space
  527. * @bus: bus to read
  528. * @port: legacy port value
  529. * @val: caller allocated storage for returned value
  530. * @size: number of bytes to read
  531. *
  532. * Simply reads @size bytes from @port and puts the result in @val.
  533. *
  534. * Again, this (and the write routine) are generic versions that can be
  535. * overridden by the platform. This is necessary on platforms that don't
  536. * support legacy I/O routing or that hard fail on legacy I/O timeouts.
  537. */
  538. int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  539. {
  540. int ret = size;
  541. switch (size) {
  542. case 1:
  543. *val = inb(port);
  544. break;
  545. case 2:
  546. *val = inw(port);
  547. break;
  548. case 4:
  549. *val = inl(port);
  550. break;
  551. default:
  552. ret = -EINVAL;
  553. break;
  554. }
  555. return ret;
  556. }
  557. /**
  558. * ia64_pci_legacy_write - perform a legacy I/O write
  559. * @bus: bus pointer
  560. * @port: port to write
  561. * @val: value to write
  562. * @size: number of bytes to write from @val
  563. *
  564. * Simply writes @size bytes of @val to @port.
  565. */
  566. int ia64_pci_legacy_write(struct pci_dev *bus, u16 port, u32 val, u8 size)
  567. {
  568. int ret = 0;
  569. switch (size) {
  570. case 1:
  571. outb(val, port);
  572. break;
  573. case 2:
  574. outw(val, port);
  575. break;
  576. case 4:
  577. outl(val, port);
  578. break;
  579. default:
  580. ret = -EINVAL;
  581. break;
  582. }
  583. return ret;
  584. }
  585. /**
  586. * pci_cacheline_size - determine cacheline size for PCI devices
  587. * @dev: void
  588. *
  589. * We want to use the line-size of the outer-most cache. We assume
  590. * that this line-size is the same for all CPUs.
  591. *
  592. * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
  593. *
  594. * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
  595. */
  596. static unsigned long
  597. pci_cacheline_size (void)
  598. {
  599. u64 levels, unique_caches;
  600. s64 status;
  601. pal_cache_config_info_t cci;
  602. static u8 cacheline_size;
  603. if (cacheline_size)
  604. return cacheline_size;
  605. status = ia64_pal_cache_summary(&levels, &unique_caches);
  606. if (status != 0) {
  607. printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
  608. __FUNCTION__, status);
  609. return SMP_CACHE_BYTES;
  610. }
  611. status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
  612. &cci);
  613. if (status != 0) {
  614. printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
  615. __FUNCTION__, status);
  616. return SMP_CACHE_BYTES;
  617. }
  618. cacheline_size = 1 << cci.pcci_line_size;
  619. return cacheline_size;
  620. }
  621. /**
  622. * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
  623. * @dev: the PCI device for which MWI is enabled
  624. *
  625. * For ia64, we can get the cacheline sizes from PAL.
  626. *
  627. * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
  628. */
  629. int
  630. pcibios_prep_mwi (struct pci_dev *dev)
  631. {
  632. unsigned long desired_linesize, current_linesize;
  633. int rc = 0;
  634. u8 pci_linesize;
  635. desired_linesize = pci_cacheline_size();
  636. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
  637. current_linesize = 4 * pci_linesize;
  638. if (desired_linesize != current_linesize) {
  639. printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
  640. pci_name(dev), current_linesize);
  641. if (current_linesize > desired_linesize) {
  642. printk(" expected %lu bytes instead\n", desired_linesize);
  643. rc = -EINVAL;
  644. } else {
  645. printk(" correcting to %lu\n", desired_linesize);
  646. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
  647. }
  648. }
  649. return rc;
  650. }
  651. int pci_vector_resources(int last, int nr_released)
  652. {
  653. int count = nr_released;
  654. count += (IA64_LAST_DEVICE_VECTOR - last);
  655. return count;
  656. }