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