pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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/pci-acpi.h>
  18. #include <linux/init.h>
  19. #include <linux/ioport.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/export.h>
  24. #include <asm/machvec.h>
  25. #include <asm/page.h>
  26. #include <asm/io.h>
  27. #include <asm/sal.h>
  28. #include <asm/smp.h>
  29. #include <asm/irq.h>
  30. #include <asm/hw_irq.h>
  31. /*
  32. * Low-level SAL-based PCI configuration access functions. Note that SAL
  33. * calls are already serialized (via sal_lock), so we don't need another
  34. * synchronization mechanism here.
  35. */
  36. #define PCI_SAL_ADDRESS(seg, bus, devfn, reg) \
  37. (((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))
  38. /* SAL 3.2 adds support for extended config space. */
  39. #define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg) \
  40. (((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))
  41. int raw_pci_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 if (sal_revision >= SAL_VERSION_CODE(3,2)) {
  52. addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
  53. mode = 1;
  54. } else {
  55. return -EINVAL;
  56. }
  57. result = ia64_sal_pci_config_read(addr, mode, len, &data);
  58. if (result != 0)
  59. return -EINVAL;
  60. *value = (u32) data;
  61. return 0;
  62. }
  63. int raw_pci_write(unsigned int seg, unsigned int bus, unsigned int devfn,
  64. int reg, int len, u32 value)
  65. {
  66. u64 addr;
  67. int mode, result;
  68. if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
  69. return -EINVAL;
  70. if ((seg | reg) <= 255) {
  71. addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
  72. mode = 0;
  73. } else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
  74. addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
  75. mode = 1;
  76. } else {
  77. return -EINVAL;
  78. }
  79. result = ia64_sal_pci_config_write(addr, mode, len, value);
  80. if (result != 0)
  81. return -EINVAL;
  82. return 0;
  83. }
  84. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  85. int size, u32 *value)
  86. {
  87. return raw_pci_read(pci_domain_nr(bus), bus->number,
  88. devfn, where, size, value);
  89. }
  90. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  91. int size, u32 value)
  92. {
  93. return raw_pci_write(pci_domain_nr(bus), bus->number,
  94. devfn, where, size, value);
  95. }
  96. struct pci_ops pci_root_ops = {
  97. .read = pci_read,
  98. .write = pci_write,
  99. };
  100. struct pci_root_info {
  101. struct acpi_pci_root_info common;
  102. struct pci_controller controller;
  103. struct list_head io_resources;
  104. };
  105. static unsigned int new_space(u64 phys_base, int sparse)
  106. {
  107. u64 mmio_base;
  108. int i;
  109. if (phys_base == 0)
  110. return 0; /* legacy I/O port space */
  111. mmio_base = (u64) ioremap(phys_base, 0);
  112. for (i = 0; i < num_io_spaces; i++)
  113. if (io_space[i].mmio_base == mmio_base &&
  114. io_space[i].sparse == sparse)
  115. return i;
  116. if (num_io_spaces == MAX_IO_SPACES) {
  117. pr_err("PCI: Too many IO port spaces "
  118. "(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
  119. return ~0;
  120. }
  121. i = num_io_spaces++;
  122. io_space[i].mmio_base = mmio_base;
  123. io_space[i].sparse = sparse;
  124. return i;
  125. }
  126. static int add_io_space(struct device *dev, struct pci_root_info *info,
  127. struct resource_entry *entry)
  128. {
  129. struct resource_entry *iospace;
  130. struct resource *resource, *res = entry->res;
  131. char *name;
  132. unsigned long base, min, max, base_port;
  133. unsigned int sparse = 0, space_nr, len;
  134. len = strlen(info->common.name) + 32;
  135. iospace = resource_list_create_entry(NULL, len);
  136. if (!iospace) {
  137. dev_err(dev, "PCI: No memory for %s I/O port space\n",
  138. info->common.name);
  139. return -ENOMEM;
  140. }
  141. if (res->flags & IORESOURCE_IO_SPARSE)
  142. sparse = 1;
  143. space_nr = new_space(entry->offset, sparse);
  144. if (space_nr == ~0)
  145. goto free_resource;
  146. name = (char *)(iospace + 1);
  147. min = res->start - entry->offset;
  148. max = res->end - entry->offset;
  149. base = __pa(io_space[space_nr].mmio_base);
  150. base_port = IO_SPACE_BASE(space_nr);
  151. snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->common.name,
  152. base_port + min, base_port + max);
  153. /*
  154. * The SDM guarantees the legacy 0-64K space is sparse, but if the
  155. * mapping is done by the processor (not the bridge), ACPI may not
  156. * mark it as sparse.
  157. */
  158. if (space_nr == 0)
  159. sparse = 1;
  160. resource = iospace->res;
  161. resource->name = name;
  162. resource->flags = IORESOURCE_MEM;
  163. resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
  164. resource->end = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
  165. if (insert_resource(&iomem_resource, resource)) {
  166. dev_err(dev,
  167. "can't allocate host bridge io space resource %pR\n",
  168. resource);
  169. goto free_resource;
  170. }
  171. entry->offset = base_port;
  172. res->start = min + base_port;
  173. res->end = max + base_port;
  174. resource_list_add_tail(iospace, &info->io_resources);
  175. return 0;
  176. free_resource:
  177. resource_list_free_entry(iospace);
  178. return -ENOSPC;
  179. }
  180. /*
  181. * An IO port or MMIO resource assigned to a PCI host bridge may be
  182. * consumed by the host bridge itself or available to its child
  183. * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
  184. * to tell whether the resource is consumed by the host bridge itself,
  185. * but firmware hasn't used that bit consistently, so we can't rely on it.
  186. *
  187. * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
  188. * to be available to child bus/devices except one special case:
  189. * IO port [0xCF8-0xCFF] is consumed by the host bridge itself
  190. * to access PCI configuration space.
  191. *
  192. * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
  193. */
  194. static bool resource_is_pcicfg_ioport(struct resource *res)
  195. {
  196. return (res->flags & IORESOURCE_IO) &&
  197. res->start == 0xCF8 && res->end == 0xCFF;
  198. }
  199. static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
  200. {
  201. struct device *dev = &ci->bridge->dev;
  202. struct pci_root_info *info;
  203. struct resource *res;
  204. struct resource_entry *entry, *tmp;
  205. int status;
  206. status = acpi_pci_probe_root_resources(ci);
  207. if (status > 0) {
  208. info = container_of(ci, struct pci_root_info, common);
  209. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  210. res = entry->res;
  211. if (res->flags & IORESOURCE_MEM) {
  212. /*
  213. * HP's firmware has a hack to work around a
  214. * Windows bug. Ignore these tiny memory ranges.
  215. */
  216. if (resource_size(res) <= 16) {
  217. resource_list_del(entry);
  218. insert_resource(&iomem_resource,
  219. entry->res);
  220. resource_list_add_tail(entry,
  221. &info->io_resources);
  222. }
  223. } else if (res->flags & IORESOURCE_IO) {
  224. if (resource_is_pcicfg_ioport(entry->res))
  225. resource_list_destroy_entry(entry);
  226. else if (add_io_space(dev, info, entry))
  227. resource_list_destroy_entry(entry);
  228. }
  229. }
  230. }
  231. return status;
  232. }
  233. static void pci_acpi_root_release_info(struct acpi_pci_root_info *ci)
  234. {
  235. struct pci_root_info *info;
  236. struct resource_entry *entry, *tmp;
  237. info = container_of(ci, struct pci_root_info, common);
  238. resource_list_for_each_entry_safe(entry, tmp, &info->io_resources) {
  239. release_resource(entry->res);
  240. resource_list_destroy_entry(entry);
  241. }
  242. kfree(info);
  243. }
  244. static struct acpi_pci_root_ops pci_acpi_root_ops = {
  245. .pci_ops = &pci_root_ops,
  246. .release_info = pci_acpi_root_release_info,
  247. .prepare_resources = pci_acpi_root_prepare_resources,
  248. };
  249. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  250. {
  251. struct acpi_device *device = root->device;
  252. struct pci_root_info *info;
  253. info = kzalloc(sizeof(*info), GFP_KERNEL);
  254. if (!info) {
  255. dev_err(&device->dev,
  256. "pci_bus %04x:%02x: ignored (out of memory)\n",
  257. root->segment, (int)root->secondary.start);
  258. return NULL;
  259. }
  260. info->controller.segment = root->segment;
  261. info->controller.companion = device;
  262. info->controller.node = acpi_get_node(device->handle);
  263. INIT_LIST_HEAD(&info->io_resources);
  264. return acpi_pci_root_create(root, &pci_acpi_root_ops,
  265. &info->common, &info->controller);
  266. }
  267. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  268. {
  269. /*
  270. * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
  271. * here, pci_create_root_bus() has been called by someone else and
  272. * sysdata is likely to be different from what we expect. Let it go in
  273. * that case.
  274. */
  275. if (!bridge->dev.parent) {
  276. struct pci_controller *controller = bridge->bus->sysdata;
  277. ACPI_COMPANION_SET(&bridge->dev, controller->companion);
  278. }
  279. return 0;
  280. }
  281. void pcibios_fixup_device_resources(struct pci_dev *dev)
  282. {
  283. int idx;
  284. if (!dev->bus)
  285. return;
  286. for (idx = 0; idx < PCI_BRIDGE_RESOURCES; idx++) {
  287. struct resource *r = &dev->resource[idx];
  288. if (!r->flags || r->parent || !r->start)
  289. continue;
  290. pci_claim_resource(dev, idx);
  291. }
  292. }
  293. EXPORT_SYMBOL_GPL(pcibios_fixup_device_resources);
  294. static void pcibios_fixup_bridge_resources(struct pci_dev *dev)
  295. {
  296. int idx;
  297. if (!dev->bus)
  298. return;
  299. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  300. struct resource *r = &dev->resource[idx];
  301. if (!r->flags || r->parent || !r->start)
  302. continue;
  303. pci_claim_bridge_resource(dev, idx);
  304. }
  305. }
  306. /*
  307. * Called after each bus is probed, but before its children are examined.
  308. */
  309. void pcibios_fixup_bus(struct pci_bus *b)
  310. {
  311. struct pci_dev *dev;
  312. if (b->self) {
  313. pci_read_bridge_bases(b);
  314. pcibios_fixup_bridge_resources(b->self);
  315. }
  316. list_for_each_entry(dev, &b->devices, bus_list)
  317. pcibios_fixup_device_resources(dev);
  318. platform_pci_fixup_bus(b);
  319. }
  320. void pcibios_add_bus(struct pci_bus *bus)
  321. {
  322. acpi_pci_add_bus(bus);
  323. }
  324. void pcibios_remove_bus(struct pci_bus *bus)
  325. {
  326. acpi_pci_remove_bus(bus);
  327. }
  328. void pcibios_set_master (struct pci_dev *dev)
  329. {
  330. /* No special bus mastering setup handling */
  331. }
  332. int
  333. pcibios_enable_device (struct pci_dev *dev, int mask)
  334. {
  335. int ret;
  336. ret = pci_enable_resources(dev, mask);
  337. if (ret < 0)
  338. return ret;
  339. if (!dev->msi_enabled)
  340. return acpi_pci_irq_enable(dev);
  341. return 0;
  342. }
  343. void
  344. pcibios_disable_device (struct pci_dev *dev)
  345. {
  346. BUG_ON(atomic_read(&dev->enable_cnt));
  347. if (!dev->msi_enabled)
  348. acpi_pci_irq_disable(dev);
  349. }
  350. resource_size_t
  351. pcibios_align_resource (void *data, const struct resource *res,
  352. resource_size_t size, resource_size_t align)
  353. {
  354. return res->start;
  355. }
  356. int
  357. pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
  358. enum pci_mmap_state mmap_state, int write_combine)
  359. {
  360. unsigned long size = vma->vm_end - vma->vm_start;
  361. pgprot_t prot;
  362. /*
  363. * I/O space cannot be accessed via normal processor loads and
  364. * stores on this platform.
  365. */
  366. if (mmap_state == pci_mmap_io)
  367. /*
  368. * XXX we could relax this for I/O spaces for which ACPI
  369. * indicates that the space is 1-to-1 mapped. But at the
  370. * moment, we don't support multiple PCI address spaces and
  371. * the legacy I/O space is not 1-to-1 mapped, so this is moot.
  372. */
  373. return -EINVAL;
  374. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  375. return -EINVAL;
  376. prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
  377. vma->vm_page_prot);
  378. /*
  379. * If the user requested WC, the kernel uses UC or WC for this region,
  380. * and the chipset supports WC, we can use WC. Otherwise, we have to
  381. * use the same attribute the kernel uses.
  382. */
  383. if (write_combine &&
  384. ((pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_UC ||
  385. (pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_WC) &&
  386. efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
  387. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  388. else
  389. vma->vm_page_prot = prot;
  390. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  391. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  392. return -EAGAIN;
  393. return 0;
  394. }
  395. /**
  396. * ia64_pci_get_legacy_mem - generic legacy mem routine
  397. * @bus: bus to get legacy memory base address for
  398. *
  399. * Find the base of legacy memory for @bus. This is typically the first
  400. * megabyte of bus address space for @bus or is simply 0 on platforms whose
  401. * chipsets support legacy I/O and memory routing. Returns the base address
  402. * or an error pointer if an error occurred.
  403. *
  404. * This is the ia64 generic version of this routine. Other platforms
  405. * are free to override it with a machine vector.
  406. */
  407. char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
  408. {
  409. return (char *)__IA64_UNCACHED_OFFSET;
  410. }
  411. /**
  412. * pci_mmap_legacy_page_range - map legacy memory space to userland
  413. * @bus: bus whose legacy space we're mapping
  414. * @vma: vma passed in by mmap
  415. *
  416. * Map legacy memory space for this device back to userspace using a machine
  417. * vector to get the base address.
  418. */
  419. int
  420. pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
  421. enum pci_mmap_state mmap_state)
  422. {
  423. unsigned long size = vma->vm_end - vma->vm_start;
  424. pgprot_t prot;
  425. char *addr;
  426. /* We only support mmap'ing of legacy memory space */
  427. if (mmap_state != pci_mmap_mem)
  428. return -ENOSYS;
  429. /*
  430. * Avoid attribute aliasing. See Documentation/ia64/aliasing.txt
  431. * for more details.
  432. */
  433. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  434. return -EINVAL;
  435. prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
  436. vma->vm_page_prot);
  437. addr = pci_get_legacy_mem(bus);
  438. if (IS_ERR(addr))
  439. return PTR_ERR(addr);
  440. vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
  441. vma->vm_page_prot = prot;
  442. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  443. size, vma->vm_page_prot))
  444. return -EAGAIN;
  445. return 0;
  446. }
  447. /**
  448. * ia64_pci_legacy_read - read from legacy I/O space
  449. * @bus: bus to read
  450. * @port: legacy port value
  451. * @val: caller allocated storage for returned value
  452. * @size: number of bytes to read
  453. *
  454. * Simply reads @size bytes from @port and puts the result in @val.
  455. *
  456. * Again, this (and the write routine) are generic versions that can be
  457. * overridden by the platform. This is necessary on platforms that don't
  458. * support legacy I/O routing or that hard fail on legacy I/O timeouts.
  459. */
  460. int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
  461. {
  462. int ret = size;
  463. switch (size) {
  464. case 1:
  465. *val = inb(port);
  466. break;
  467. case 2:
  468. *val = inw(port);
  469. break;
  470. case 4:
  471. *val = inl(port);
  472. break;
  473. default:
  474. ret = -EINVAL;
  475. break;
  476. }
  477. return ret;
  478. }
  479. /**
  480. * ia64_pci_legacy_write - perform a legacy I/O write
  481. * @bus: bus pointer
  482. * @port: port to write
  483. * @val: value to write
  484. * @size: number of bytes to write from @val
  485. *
  486. * Simply writes @size bytes of @val to @port.
  487. */
  488. int ia64_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
  489. {
  490. int ret = size;
  491. switch (size) {
  492. case 1:
  493. outb(val, port);
  494. break;
  495. case 2:
  496. outw(val, port);
  497. break;
  498. case 4:
  499. outl(val, port);
  500. break;
  501. default:
  502. ret = -EINVAL;
  503. break;
  504. }
  505. return ret;
  506. }
  507. /**
  508. * set_pci_cacheline_size - determine cacheline size for PCI devices
  509. *
  510. * We want to use the line-size of the outer-most cache. We assume
  511. * that this line-size is the same for all CPUs.
  512. *
  513. * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
  514. */
  515. static void __init set_pci_dfl_cacheline_size(void)
  516. {
  517. unsigned long levels, unique_caches;
  518. long status;
  519. pal_cache_config_info_t cci;
  520. status = ia64_pal_cache_summary(&levels, &unique_caches);
  521. if (status != 0) {
  522. pr_err("%s: ia64_pal_cache_summary() failed "
  523. "(status=%ld)\n", __func__, status);
  524. return;
  525. }
  526. status = ia64_pal_cache_config_info(levels - 1,
  527. /* cache_type (data_or_unified)= */ 2, &cci);
  528. if (status != 0) {
  529. pr_err("%s: ia64_pal_cache_config_info() failed "
  530. "(status=%ld)\n", __func__, status);
  531. return;
  532. }
  533. pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
  534. }
  535. u64 ia64_dma_get_required_mask(struct device *dev)
  536. {
  537. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  538. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  539. u64 mask;
  540. if (!high_totalram) {
  541. /* convert to mask just covering totalram */
  542. low_totalram = (1 << (fls(low_totalram) - 1));
  543. low_totalram += low_totalram - 1;
  544. mask = low_totalram;
  545. } else {
  546. high_totalram = (1 << (fls(high_totalram) - 1));
  547. high_totalram += high_totalram - 1;
  548. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  549. }
  550. return mask;
  551. }
  552. EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
  553. u64 dma_get_required_mask(struct device *dev)
  554. {
  555. return platform_dma_get_required_mask(dev);
  556. }
  557. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  558. static int __init pcibios_init(void)
  559. {
  560. set_pci_dfl_cacheline_size();
  561. return 0;
  562. }
  563. subsys_initcall(pcibios_init);