pci.c 19 KB

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