pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. /* Called by ACPI when it finds a new root bus. */
  104. static struct pci_controller * __devinit
  105. alloc_pci_controller (int seg)
  106. {
  107. struct pci_controller *controller;
  108. controller = kmalloc(sizeof(*controller), GFP_KERNEL);
  109. if (!controller)
  110. return NULL;
  111. memset(controller, 0, sizeof(*controller));
  112. controller->segment = seg;
  113. controller->node = -1;
  114. return controller;
  115. }
  116. struct pci_root_info {
  117. struct pci_controller *controller;
  118. char *name;
  119. };
  120. static unsigned int
  121. new_space (u64 phys_base, int sparse)
  122. {
  123. u64 mmio_base;
  124. int i;
  125. if (phys_base == 0)
  126. return 0; /* legacy I/O port space */
  127. mmio_base = (u64) ioremap(phys_base, 0);
  128. for (i = 0; i < num_io_spaces; i++)
  129. if (io_space[i].mmio_base == mmio_base &&
  130. io_space[i].sparse == sparse)
  131. return i;
  132. if (num_io_spaces == MAX_IO_SPACES) {
  133. printk(KERN_ERR "PCI: Too many IO port spaces "
  134. "(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
  135. return ~0;
  136. }
  137. i = num_io_spaces++;
  138. io_space[i].mmio_base = mmio_base;
  139. io_space[i].sparse = sparse;
  140. return i;
  141. }
  142. static u64 __devinit
  143. add_io_space (struct pci_root_info *info, struct acpi_resource_address64 *addr)
  144. {
  145. struct resource *resource;
  146. char *name;
  147. u64 base, min, max, base_port;
  148. unsigned int sparse = 0, space_nr, len;
  149. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  150. if (!resource) {
  151. printk(KERN_ERR "PCI: No memory for %s I/O port space\n",
  152. info->name);
  153. goto out;
  154. }
  155. len = strlen(info->name) + 32;
  156. name = kzalloc(len, GFP_KERNEL);
  157. if (!name) {
  158. printk(KERN_ERR "PCI: No memory for %s I/O port space name\n",
  159. info->name);
  160. goto free_resource;
  161. }
  162. min = addr->minimum;
  163. max = min + addr->address_length - 1;
  164. if (addr->info.io.translation_type == ACPI_SPARSE_TRANSLATION)
  165. sparse = 1;
  166. space_nr = new_space(addr->translation_offset, sparse);
  167. if (space_nr == ~0)
  168. goto free_name;
  169. base = __pa(io_space[space_nr].mmio_base);
  170. base_port = IO_SPACE_BASE(space_nr);
  171. snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->name,
  172. base_port + min, base_port + max);
  173. /*
  174. * The SDM guarantees the legacy 0-64K space is sparse, but if the
  175. * mapping is done by the processor (not the bridge), ACPI may not
  176. * mark it as sparse.
  177. */
  178. if (space_nr == 0)
  179. sparse = 1;
  180. resource->name = name;
  181. resource->flags = IORESOURCE_MEM;
  182. resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
  183. resource->end = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
  184. insert_resource(&iomem_resource, resource);
  185. return base_port;
  186. free_name:
  187. kfree(name);
  188. free_resource:
  189. kfree(resource);
  190. out:
  191. return ~0;
  192. }
  193. static acpi_status __devinit resource_to_window(struct acpi_resource *resource,
  194. struct acpi_resource_address64 *addr)
  195. {
  196. acpi_status status;
  197. /*
  198. * We're only interested in _CRS descriptors that are
  199. * - address space descriptors for memory or I/O space
  200. * - non-zero size
  201. * - producers, i.e., the address space is routed downstream,
  202. * not consumed by the bridge itself
  203. */
  204. status = acpi_resource_to_address64(resource, addr);
  205. if (ACPI_SUCCESS(status) &&
  206. (addr->resource_type == ACPI_MEMORY_RANGE ||
  207. addr->resource_type == ACPI_IO_RANGE) &&
  208. addr->address_length &&
  209. addr->producer_consumer == ACPI_PRODUCER)
  210. return AE_OK;
  211. return AE_ERROR;
  212. }
  213. static acpi_status __devinit
  214. count_window (struct acpi_resource *resource, void *data)
  215. {
  216. unsigned int *windows = (unsigned int *) data;
  217. struct acpi_resource_address64 addr;
  218. acpi_status status;
  219. status = resource_to_window(resource, &addr);
  220. if (ACPI_SUCCESS(status))
  221. (*windows)++;
  222. return AE_OK;
  223. }
  224. static __devinit acpi_status add_window(struct acpi_resource *res, void *data)
  225. {
  226. struct pci_root_info *info = data;
  227. struct pci_window *window;
  228. struct acpi_resource_address64 addr;
  229. acpi_status status;
  230. unsigned long flags, offset = 0;
  231. struct resource *root;
  232. /* Return AE_OK for non-window resources to keep scanning for more */
  233. status = resource_to_window(res, &addr);
  234. if (!ACPI_SUCCESS(status))
  235. return AE_OK;
  236. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  237. flags = IORESOURCE_MEM;
  238. root = &iomem_resource;
  239. offset = addr.translation_offset;
  240. } else if (addr.resource_type == ACPI_IO_RANGE) {
  241. flags = IORESOURCE_IO;
  242. root = &ioport_resource;
  243. offset = add_io_space(info, &addr);
  244. if (offset == ~0)
  245. return AE_OK;
  246. } else
  247. return AE_OK;
  248. window = &info->controller->window[info->controller->windows++];
  249. window->resource.name = info->name;
  250. window->resource.flags = flags;
  251. window->resource.start = addr.minimum + offset;
  252. window->resource.end = window->resource.start + addr.address_length - 1;
  253. window->resource.child = NULL;
  254. window->offset = offset;
  255. if (insert_resource(root, &window->resource)) {
  256. printk(KERN_ERR "alloc 0x%lx-0x%lx from %s for %s failed\n",
  257. window->resource.start, window->resource.end,
  258. root->name, info->name);
  259. }
  260. return AE_OK;
  261. }
  262. static void __devinit
  263. pcibios_setup_root_windows(struct pci_bus *bus, struct pci_controller *ctrl)
  264. {
  265. int i, j;
  266. j = 0;
  267. for (i = 0; i < ctrl->windows; i++) {
  268. struct resource *res = &ctrl->window[i].resource;
  269. /* HP's firmware has a hack to work around a Windows bug.
  270. * Ignore these tiny memory ranges */
  271. if ((res->flags & IORESOURCE_MEM) &&
  272. (res->end - res->start < 16))
  273. continue;
  274. if (j >= PCI_BUS_NUM_RESOURCES) {
  275. printk("Ignoring range [%lx-%lx] (%lx)\n", res->start,
  276. res->end, res->flags);
  277. continue;
  278. }
  279. bus->resource[j++] = res;
  280. }
  281. }
  282. struct pci_bus * __devinit
  283. pci_acpi_scan_root(struct acpi_device *device, int domain, int bus)
  284. {
  285. struct pci_root_info info;
  286. struct pci_controller *controller;
  287. unsigned int windows = 0;
  288. struct pci_bus *pbus;
  289. char *name;
  290. int pxm;
  291. controller = alloc_pci_controller(domain);
  292. if (!controller)
  293. goto out1;
  294. controller->acpi_handle = device->handle;
  295. pxm = acpi_get_pxm(controller->acpi_handle);
  296. #ifdef CONFIG_NUMA
  297. if (pxm >= 0)
  298. controller->node = pxm_to_nid_map[pxm];
  299. #endif
  300. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
  301. &windows);
  302. controller->window = kmalloc_node(sizeof(*controller->window) * windows,
  303. GFP_KERNEL, controller->node);
  304. if (!controller->window)
  305. goto out2;
  306. name = kmalloc(16, GFP_KERNEL);
  307. if (!name)
  308. goto out3;
  309. sprintf(name, "PCI Bus %04x:%02x", domain, bus);
  310. info.controller = controller;
  311. info.name = name;
  312. acpi_walk_resources(device->handle, METHOD_NAME__CRS, add_window,
  313. &info);
  314. pbus = pci_scan_bus_parented(NULL, bus, &pci_root_ops, controller);
  315. if (pbus)
  316. pcibios_setup_root_windows(pbus, controller);
  317. return pbus;
  318. out3:
  319. kfree(controller->window);
  320. out2:
  321. kfree(controller);
  322. out1:
  323. return NULL;
  324. }
  325. void pcibios_resource_to_bus(struct pci_dev *dev,
  326. struct pci_bus_region *region, struct resource *res)
  327. {
  328. struct pci_controller *controller = PCI_CONTROLLER(dev);
  329. unsigned long offset = 0;
  330. int i;
  331. for (i = 0; i < controller->windows; i++) {
  332. struct pci_window *window = &controller->window[i];
  333. if (!(window->resource.flags & res->flags))
  334. continue;
  335. if (window->resource.start > res->start)
  336. continue;
  337. if (window->resource.end < res->end)
  338. continue;
  339. offset = window->offset;
  340. break;
  341. }
  342. region->start = res->start - offset;
  343. region->end = res->end - offset;
  344. }
  345. EXPORT_SYMBOL(pcibios_resource_to_bus);
  346. void pcibios_bus_to_resource(struct pci_dev *dev,
  347. struct resource *res, struct pci_bus_region *region)
  348. {
  349. struct pci_controller *controller = PCI_CONTROLLER(dev);
  350. unsigned long offset = 0;
  351. int i;
  352. for (i = 0; i < controller->windows; i++) {
  353. struct pci_window *window = &controller->window[i];
  354. if (!(window->resource.flags & res->flags))
  355. continue;
  356. if (window->resource.start - window->offset > region->start)
  357. continue;
  358. if (window->resource.end - window->offset < region->end)
  359. continue;
  360. offset = window->offset;
  361. break;
  362. }
  363. res->start = region->start + offset;
  364. res->end = region->end + offset;
  365. }
  366. EXPORT_SYMBOL(pcibios_bus_to_resource);
  367. static int __devinit is_valid_resource(struct pci_dev *dev, int idx)
  368. {
  369. unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  370. struct resource *devr = &dev->resource[idx];
  371. if (!dev->bus)
  372. return 0;
  373. for (i=0; i<PCI_BUS_NUM_RESOURCES; i++) {
  374. struct resource *busr = dev->bus->resource[i];
  375. if (!busr || ((busr->flags ^ devr->flags) & type_mask))
  376. continue;
  377. if ((devr->start) && (devr->start >= busr->start) &&
  378. (devr->end <= busr->end))
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. static void __devinit
  384. pcibios_fixup_resources(struct pci_dev *dev, int start, int limit)
  385. {
  386. struct pci_bus_region region;
  387. int i;
  388. for (i = start; i < limit; i++) {
  389. if (!dev->resource[i].flags)
  390. continue;
  391. region.start = dev->resource[i].start;
  392. region.end = dev->resource[i].end;
  393. pcibios_bus_to_resource(dev, &dev->resource[i], &region);
  394. if ((is_valid_resource(dev, i)))
  395. pci_claim_resource(dev, i);
  396. }
  397. }
  398. static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
  399. {
  400. pcibios_fixup_resources(dev, 0, PCI_BRIDGE_RESOURCES);
  401. }
  402. static void __devinit pcibios_fixup_bridge_resources(struct pci_dev *dev)
  403. {
  404. pcibios_fixup_resources(dev, PCI_BRIDGE_RESOURCES, PCI_NUM_RESOURCES);
  405. }
  406. /*
  407. * Called after each bus is probed, but before its children are examined.
  408. */
  409. void __devinit
  410. pcibios_fixup_bus (struct pci_bus *b)
  411. {
  412. struct pci_dev *dev;
  413. if (b->self) {
  414. pci_read_bridge_bases(b);
  415. pcibios_fixup_bridge_resources(b->self);
  416. }
  417. list_for_each_entry(dev, &b->devices, bus_list)
  418. pcibios_fixup_device_resources(dev);
  419. return;
  420. }
  421. void __devinit
  422. pcibios_update_irq (struct pci_dev *dev, int irq)
  423. {
  424. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  425. /* ??? FIXME -- record old value for shutdown. */
  426. }
  427. static inline int
  428. pcibios_enable_resources (struct pci_dev *dev, int mask)
  429. {
  430. u16 cmd, old_cmd;
  431. int idx;
  432. struct resource *r;
  433. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  434. if (!dev)
  435. return -EINVAL;
  436. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  437. old_cmd = cmd;
  438. for (idx=0; idx<PCI_NUM_RESOURCES; idx++) {
  439. /* Only set up the desired resources. */
  440. if (!(mask & (1 << idx)))
  441. continue;
  442. r = &dev->resource[idx];
  443. if (!(r->flags & type_mask))
  444. continue;
  445. if ((idx == PCI_ROM_RESOURCE) &&
  446. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  447. continue;
  448. if (!r->start && r->end) {
  449. printk(KERN_ERR
  450. "PCI: Device %s not available because of resource collisions\n",
  451. pci_name(dev));
  452. return -EINVAL;
  453. }
  454. if (r->flags & IORESOURCE_IO)
  455. cmd |= PCI_COMMAND_IO;
  456. if (r->flags & IORESOURCE_MEM)
  457. cmd |= PCI_COMMAND_MEMORY;
  458. }
  459. if (cmd != old_cmd) {
  460. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  461. pci_write_config_word(dev, PCI_COMMAND, cmd);
  462. }
  463. return 0;
  464. }
  465. int
  466. pcibios_enable_device (struct pci_dev *dev, int mask)
  467. {
  468. int ret;
  469. ret = pcibios_enable_resources(dev, mask);
  470. if (ret < 0)
  471. return ret;
  472. return acpi_pci_irq_enable(dev);
  473. }
  474. void
  475. pcibios_disable_device (struct pci_dev *dev)
  476. {
  477. acpi_pci_irq_disable(dev);
  478. }
  479. void
  480. pcibios_align_resource (void *data, struct resource *res,
  481. unsigned long size, unsigned long align)
  482. {
  483. }
  484. /*
  485. * PCI BIOS setup, always defaults to SAL interface
  486. */
  487. char * __init
  488. pcibios_setup (char *str)
  489. {
  490. return NULL;
  491. }
  492. int
  493. pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
  494. enum pci_mmap_state mmap_state, int write_combine)
  495. {
  496. /*
  497. * I/O space cannot be accessed via normal processor loads and
  498. * stores on this platform.
  499. */
  500. if (mmap_state == pci_mmap_io)
  501. /*
  502. * XXX we could relax this for I/O spaces for which ACPI
  503. * indicates that the space is 1-to-1 mapped. But at the
  504. * moment, we don't support multiple PCI address spaces and
  505. * the legacy I/O space is not 1-to-1 mapped, so this is moot.
  506. */
  507. return -EINVAL;
  508. /*
  509. * Leave vm_pgoff as-is, the PCI space address is the physical
  510. * address on this platform.
  511. */
  512. vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
  513. if (write_combine && efi_range_is_wc(vma->vm_start,
  514. vma->vm_end - vma->vm_start))
  515. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  516. else
  517. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  518. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  519. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  520. return -EAGAIN;
  521. return 0;
  522. }
  523. /**
  524. * ia64_pci_get_legacy_mem - generic legacy mem routine
  525. * @bus: bus to get legacy memory base address for
  526. *
  527. * Find the base of legacy memory for @bus. This is typically the first
  528. * megabyte of bus address space for @bus or is simply 0 on platforms whose
  529. * chipsets support legacy I/O and memory routing. Returns the base address
  530. * or an error pointer if an error occurred.
  531. *
  532. * This is the ia64 generic version of this routine. Other platforms
  533. * are free to override it with a machine vector.
  534. */
  535. char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
  536. {
  537. return (char *)__IA64_UNCACHED_OFFSET;
  538. }
  539. /**
  540. * pci_mmap_legacy_page_range - map legacy memory space to userland
  541. * @bus: bus whose legacy space we're mapping
  542. * @vma: vma passed in by mmap
  543. *
  544. * Map legacy memory space for this device back to userspace using a machine
  545. * vector to get the base address.
  546. */
  547. int
  548. pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma)
  549. {
  550. char *addr;
  551. addr = pci_get_legacy_mem(bus);
  552. if (IS_ERR(addr))
  553. return PTR_ERR(addr);
  554. vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
  555. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  556. vma->vm_flags |= (VM_SHM | VM_RESERVED | VM_IO);
  557. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  558. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  559. return -EAGAIN;
  560. return 0;
  561. }
  562. /**
  563. * ia64_pci_legacy_read - read from legacy I/O space
  564. * @bus: bus to read
  565. * @port: legacy port value
  566. * @val: caller allocated storage for returned value
  567. * @size: number of bytes to read
  568. *
  569. * Simply reads @size bytes from @port and puts the result in @val.
  570. *
  571. * Again, this (and the write routine) are generic versions that can be
  572. * overridden by the platform. This is necessary on platforms that don't
  573. * support legacy I/O routing or that hard fail on legacy I/O timeouts.
  574. */
  575. int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  576. {
  577. int ret = size;
  578. switch (size) {
  579. case 1:
  580. *val = inb(port);
  581. break;
  582. case 2:
  583. *val = inw(port);
  584. break;
  585. case 4:
  586. *val = inl(port);
  587. break;
  588. default:
  589. ret = -EINVAL;
  590. break;
  591. }
  592. return ret;
  593. }
  594. /**
  595. * ia64_pci_legacy_write - perform a legacy I/O write
  596. * @bus: bus pointer
  597. * @port: port to write
  598. * @val: value to write
  599. * @size: number of bytes to write from @val
  600. *
  601. * Simply writes @size bytes of @val to @port.
  602. */
  603. int ia64_pci_legacy_write(struct pci_dev *bus, u16 port, u32 val, u8 size)
  604. {
  605. int ret = size;
  606. switch (size) {
  607. case 1:
  608. outb(val, port);
  609. break;
  610. case 2:
  611. outw(val, port);
  612. break;
  613. case 4:
  614. outl(val, port);
  615. break;
  616. default:
  617. ret = -EINVAL;
  618. break;
  619. }
  620. return ret;
  621. }
  622. /**
  623. * pci_cacheline_size - determine cacheline size for PCI devices
  624. * @dev: void
  625. *
  626. * We want to use the line-size of the outer-most cache. We assume
  627. * that this line-size is the same for all CPUs.
  628. *
  629. * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
  630. *
  631. * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
  632. */
  633. static unsigned long
  634. pci_cacheline_size (void)
  635. {
  636. u64 levels, unique_caches;
  637. s64 status;
  638. pal_cache_config_info_t cci;
  639. static u8 cacheline_size;
  640. if (cacheline_size)
  641. return cacheline_size;
  642. status = ia64_pal_cache_summary(&levels, &unique_caches);
  643. if (status != 0) {
  644. printk(KERN_ERR "%s: ia64_pal_cache_summary() failed (status=%ld)\n",
  645. __FUNCTION__, status);
  646. return SMP_CACHE_BYTES;
  647. }
  648. status = ia64_pal_cache_config_info(levels - 1, /* cache_type (data_or_unified)= */ 2,
  649. &cci);
  650. if (status != 0) {
  651. printk(KERN_ERR "%s: ia64_pal_cache_config_info() failed (status=%ld)\n",
  652. __FUNCTION__, status);
  653. return SMP_CACHE_BYTES;
  654. }
  655. cacheline_size = 1 << cci.pcci_line_size;
  656. return cacheline_size;
  657. }
  658. /**
  659. * pcibios_prep_mwi - helper function for drivers/pci/pci.c:pci_set_mwi()
  660. * @dev: the PCI device for which MWI is enabled
  661. *
  662. * For ia64, we can get the cacheline sizes from PAL.
  663. *
  664. * RETURNS: An appropriate -ERRNO error value on eror, or zero for success.
  665. */
  666. int
  667. pcibios_prep_mwi (struct pci_dev *dev)
  668. {
  669. unsigned long desired_linesize, current_linesize;
  670. int rc = 0;
  671. u8 pci_linesize;
  672. desired_linesize = pci_cacheline_size();
  673. pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &pci_linesize);
  674. current_linesize = 4 * pci_linesize;
  675. if (desired_linesize != current_linesize) {
  676. printk(KERN_WARNING "PCI: slot %s has incorrect PCI cache line size of %lu bytes,",
  677. pci_name(dev), current_linesize);
  678. if (current_linesize > desired_linesize) {
  679. printk(" expected %lu bytes instead\n", desired_linesize);
  680. rc = -EINVAL;
  681. } else {
  682. printk(" correcting to %lu\n", desired_linesize);
  683. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, desired_linesize / 4);
  684. }
  685. }
  686. return rc;
  687. }
  688. int pci_vector_resources(int last, int nr_released)
  689. {
  690. int count = nr_released;
  691. count += (IA64_LAST_DEVICE_VECTOR - last);
  692. return count;
  693. }