pci.c 19 KB

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