pci.c 19 KB

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