pci.c 19.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * pci.c - Low-Level PCI Access in IA-64
 *
 * Derived from bios32.c of i386 tree.
 *
 * (c) Copyright 2002, 2005 Hewlett-Packard Development Company, L.P.
 *	David Mosberger-Tang <davidm@hpl.hp.com>
 *	Bjorn Helgaas <bjorn.helgaas@hp.com>
 * Copyright (C) 2004 Silicon Graphics, Inc.
 *
 * Note: Above list of copyright holders is incomplete...
 */

#include <linux/acpi.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/pci.h>
18
#include <linux/pci-acpi.h>
L
Linus Torvalds 已提交
19 20 21 22
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
23
#include <linux/bootmem.h>
24
#include <linux/export.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include <asm/machvec.h>
#include <asm/page.h>
#include <asm/io.h>
#include <asm/sal.h>
#include <asm/smp.h>
#include <asm/irq.h>
#include <asm/hw_irq.h>

/*
 * Low-level SAL-based PCI configuration access functions. Note that SAL
 * calls are already serialized (via sal_lock), so we don't need another
 * synchronization mechanism here.
 */

#define PCI_SAL_ADDRESS(seg, bus, devfn, reg)		\
	(((u64) seg << 24) | (bus << 16) | (devfn << 8) | (reg))

/* SAL 3.2 adds support for extended config space. */

#define PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg)	\
	(((u64) seg << 28) | (bus << 20) | (devfn << 12) | (reg))

48
int raw_pci_read(unsigned int seg, unsigned int bus, unsigned int devfn,
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59
	      int reg, int len, u32 *value)
{
	u64 addr, data = 0;
	int mode, result;

	if (!value || (seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
		return -EINVAL;

	if ((seg | reg) <= 255) {
		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
		mode = 0;
60
	} else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
L
Linus Torvalds 已提交
61 62
		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
		mode = 1;
63 64
	} else {
		return -EINVAL;
L
Linus Torvalds 已提交
65
	}
66

L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74
	result = ia64_sal_pci_config_read(addr, mode, len, &data);
	if (result != 0)
		return -EINVAL;

	*value = (u32) data;
	return 0;
}

75
int raw_pci_write(unsigned int seg, unsigned int bus, unsigned int devfn,
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86
	       int reg, int len, u32 value)
{
	u64 addr;
	int mode, result;

	if ((seg > 65535) || (bus > 255) || (devfn > 255) || (reg > 4095))
		return -EINVAL;

	if ((seg | reg) <= 255) {
		addr = PCI_SAL_ADDRESS(seg, bus, devfn, reg);
		mode = 0;
87
	} else if (sal_revision >= SAL_VERSION_CODE(3,2)) {
L
Linus Torvalds 已提交
88 89
		addr = PCI_SAL_EXT_ADDRESS(seg, bus, devfn, reg);
		mode = 1;
90 91
	} else {
		return -EINVAL;
L
Linus Torvalds 已提交
92 93 94 95 96 97 98
	}
	result = ia64_sal_pci_config_write(addr, mode, len, value);
	if (result != 0)
		return -EINVAL;
	return 0;
}

99 100
static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
							int size, u32 *value)
L
Linus Torvalds 已提交
101
{
102
	return raw_pci_read(pci_domain_nr(bus), bus->number,
L
Linus Torvalds 已提交
103 104 105
				 devfn, where, size, value);
}

106 107
static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
							int size, u32 value)
L
Linus Torvalds 已提交
108
{
109
	return raw_pci_write(pci_domain_nr(bus), bus->number,
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119
				  devfn, where, size, value);
}

struct pci_ops pci_root_ops = {
	.read = pci_read,
	.write = pci_write,
};

/* Called by ACPI when it finds a new root bus.  */

120
static struct pci_controller *alloc_pci_controller(int seg)
L
Linus Torvalds 已提交
121 122 123
{
	struct pci_controller *controller;

124
	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
L
Linus Torvalds 已提交
125 126 127 128
	if (!controller)
		return NULL;

	controller->segment = seg;
129
	controller->node = -1;
L
Linus Torvalds 已提交
130 131 132
	return controller;
}

133
struct pci_root_info {
134
	struct acpi_device *bridge;
135
	struct pci_controller *controller;
136
	struct list_head resources;
137 138 139
	struct resource *res;
	resource_size_t *res_offset;
	unsigned int res_num;
140
	struct list_head io_resources;
141 142 143 144 145
	char *name;
};

static unsigned int
new_space (u64 phys_base, int sparse)
L
Linus Torvalds 已提交
146
{
147
	u64 mmio_base;
L
Linus Torvalds 已提交
148 149
	int i;

150 151
	if (phys_base == 0)
		return 0;	/* legacy I/O port space */
L
Linus Torvalds 已提交
152

153
	mmio_base = (u64) ioremap(phys_base, 0);
L
Linus Torvalds 已提交
154
	for (i = 0; i < num_io_spaces; i++)
155
		if (io_space[i].mmio_base == mmio_base &&
L
Linus Torvalds 已提交
156
		    io_space[i].sparse == sparse)
157
			return i;
L
Linus Torvalds 已提交
158 159

	if (num_io_spaces == MAX_IO_SPACES) {
160
		pr_err("PCI: Too many IO port spaces "
161
			"(MAX_IO_SPACES=%lu)\n", MAX_IO_SPACES);
L
Linus Torvalds 已提交
162 163 164 165
		return ~0;
	}

	i = num_io_spaces++;
166
	io_space[i].mmio_base = mmio_base;
L
Linus Torvalds 已提交
167 168
	io_space[i].sparse = sparse;

169 170 171
	return i;
}

172 173
static u64 add_io_space(struct pci_root_info *info,
			struct acpi_resource_address64 *addr)
174
{
175
	struct iospace_resource *iospace;
176 177
	struct resource *resource;
	char *name;
178
	unsigned long base, min, max, base_port;
179 180
	unsigned int sparse = 0, space_nr, len;

181 182 183
	len = strlen(info->name) + 32;
	iospace = kzalloc(sizeof(*iospace) + len, GFP_KERNEL);
	if (!iospace) {
184 185 186
		dev_err(&info->bridge->dev,
				"PCI: No memory for %s I/O port space\n",
				info->name);
187 188 189
		goto out;
	}

190
	name = (char *)(iospace + 1);
191

B
Bob Moore 已提交
192
	min = addr->minimum;
193
	max = min + addr->address_length - 1;
B
Bob Moore 已提交
194
	if (addr->info.io.translation_type == ACPI_SPARSE_TRANSLATION)
195 196
		sparse = 1;

B
Bob Moore 已提交
197
	space_nr = new_space(addr->translation_offset, sparse);
198
	if (space_nr == ~0)
199
		goto free_resource;
200 201 202 203 204 205 206 207 208 209 210 211 212 213

	base = __pa(io_space[space_nr].mmio_base);
	base_port = IO_SPACE_BASE(space_nr);
	snprintf(name, len, "%s I/O Ports %08lx-%08lx", info->name,
		base_port + min, base_port + max);

	/*
	 * The SDM guarantees the legacy 0-64K space is sparse, but if the
	 * mapping is done by the processor (not the bridge), ACPI may not
	 * mark it as sparse.
	 */
	if (space_nr == 0)
		sparse = 1;

214
	resource = &iospace->res;
215 216 217 218
	resource->name  = name;
	resource->flags = IORESOURCE_MEM;
	resource->start = base + (sparse ? IO_SPACE_SPARSE_ENCODING(min) : min);
	resource->end   = base + (sparse ? IO_SPACE_SPARSE_ENCODING(max) : max);
219 220 221 222 223 224
	if (insert_resource(&iomem_resource, resource)) {
		dev_err(&info->bridge->dev,
				"can't allocate host bridge io space resource  %pR\n",
				resource);
		goto free_resource;
	}
225

226
	list_add_tail(&iospace->list, &info->io_resources);
227 228 229
	return base_port;

free_resource:
230
	kfree(iospace);
231 232
out:
	return ~0;
L
Linus Torvalds 已提交
233 234
}

235 236
static acpi_status resource_to_window(struct acpi_resource *resource,
				      struct acpi_resource_address64 *addr)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
	acpi_status status;

	/*
	 * We're only interested in _CRS descriptors that are
	 *	- address space descriptors for memory or I/O space
	 *	- non-zero size
	 *	- producers, i.e., the address space is routed downstream,
	 *	  not consumed by the bridge itself
	 */
	status = acpi_resource_to_address64(resource, addr);
	if (ACPI_SUCCESS(status) &&
	    (addr->resource_type == ACPI_MEMORY_RANGE ||
	     addr->resource_type == ACPI_IO_RANGE) &&
	    addr->address_length &&
	    addr->producer_consumer == ACPI_PRODUCER)
		return AE_OK;

	return AE_ERROR;
}

258
static acpi_status count_window(struct acpi_resource *resource, void *data)
L
Linus Torvalds 已提交
259 260 261 262 263
{
	unsigned int *windows = (unsigned int *) data;
	struct acpi_resource_address64 addr;
	acpi_status status;

264
	status = resource_to_window(resource, &addr);
L
Linus Torvalds 已提交
265
	if (ACPI_SUCCESS(status))
266
		(*windows)++;
L
Linus Torvalds 已提交
267 268 269 270

	return AE_OK;
}

271
static acpi_status add_window(struct acpi_resource *res, void *data)
L
Linus Torvalds 已提交
272 273
{
	struct pci_root_info *info = data;
274
	struct resource *resource;
L
Linus Torvalds 已提交
275 276 277 278 279
	struct acpi_resource_address64 addr;
	acpi_status status;
	unsigned long flags, offset = 0;
	struct resource *root;

280 281
	/* Return AE_OK for non-window resources to keep scanning for more */
	status = resource_to_window(res, &addr);
L
Linus Torvalds 已提交
282 283 284 285 286 287
	if (!ACPI_SUCCESS(status))
		return AE_OK;

	if (addr.resource_type == ACPI_MEMORY_RANGE) {
		flags = IORESOURCE_MEM;
		root = &iomem_resource;
B
Bob Moore 已提交
288
		offset = addr.translation_offset;
L
Linus Torvalds 已提交
289 290 291
	} else if (addr.resource_type == ACPI_IO_RANGE) {
		flags = IORESOURCE_IO;
		root = &ioport_resource;
292
		offset = add_io_space(info, &addr);
L
Linus Torvalds 已提交
293 294 295 296 297
		if (offset == ~0)
			return AE_OK;
	} else
		return AE_OK;

298 299 300 301 302 303
	resource = &info->res[info->res_num];
	resource->name = info->name;
	resource->flags = flags;
	resource->start = addr.minimum + offset;
	resource->end = resource->start + addr.address_length - 1;
	info->res_offset[info->res_num] = offset;
L
Linus Torvalds 已提交
304

305
	if (insert_resource(root, resource)) {
306 307
		dev_err(&info->bridge->dev,
			"can't allocate host bridge window %pR\n",
308
			resource);
309 310
	} else {
		if (offset)
311
			dev_info(&info->bridge->dev, "host bridge window %pR "
312
				 "(PCI address [%#llx-%#llx])\n",
313 314 315
				 resource,
				 resource->start - offset,
				 resource->end - offset);
316 317
		else
			dev_info(&info->bridge->dev,
318
				 "host bridge window %pR\n", resource);
L
Linus Torvalds 已提交
319
	}
320 321
	/* HP's firmware has a hack to work around a Windows bug.
	 * Ignore these tiny memory ranges */
322 323 324 325
	if (!((resource->flags & IORESOURCE_MEM) &&
	      (resource->end - resource->start < 16)))
		pci_add_resource_offset(&info->resources, resource,
					info->res_offset[info->res_num]);
L
Linus Torvalds 已提交
326

327
	info->res_num++;
328
	return AE_OK;
L
Linus Torvalds 已提交
329 330
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
static void free_pci_root_info_res(struct pci_root_info *info)
{
	struct iospace_resource *iospace, *tmp;

	list_for_each_entry_safe(iospace, tmp, &info->io_resources, list)
		kfree(iospace);

	kfree(info->name);
	kfree(info->res);
	info->res = NULL;
	kfree(info->res_offset);
	info->res_offset = NULL;
	info->res_num = 0;
	kfree(info->controller);
	info->controller = NULL;
}

static void __release_pci_root_info(struct pci_root_info *info)
{
	int i;
	struct resource *res;
	struct iospace_resource *iospace;

	list_for_each_entry(iospace, &info->io_resources, list)
		release_resource(&iospace->res);

	for (i = 0; i < info->res_num; i++) {
		res = &info->res[i];

		if (!res->parent)
			continue;

		if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
			continue;

		release_resource(res);
	}

	free_pci_root_info_res(info);
	kfree(info);
}

373 374 375 376 377 378 379
static void release_pci_root_info(struct pci_host_bridge *bridge)
{
	struct pci_root_info *info = bridge->release_data;

	__release_pci_root_info(info);
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static int
probe_pci_root_info(struct pci_root_info *info, struct acpi_device *device,
		int busnum, int domain)
{
	char *name;

	name = kmalloc(16, GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	sprintf(name, "PCI Bus %04x:%02x", domain, busnum);
	info->bridge = device;
	info->name = name;

	acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_window,
			&info->res_num);
	if (info->res_num) {
		info->res =
			kzalloc_node(sizeof(*info->res) * info->res_num,
				     GFP_KERNEL, info->controller->node);
		if (!info->res) {
			kfree(name);
			return -ENOMEM;
		}

		info->res_offset =
			kzalloc_node(sizeof(*info->res_offset) * info->res_num,
					GFP_KERNEL, info->controller->node);
		if (!info->res_offset) {
			kfree(name);
			kfree(info->res);
			info->res = NULL;
			return -ENOMEM;
		}

		info->res_num = 0;
		acpi_walk_resources(device->handle, METHOD_NAME__CRS,
			add_window, info);
	} else
		kfree(name);

	return 0;
}

424
struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
L
Linus Torvalds 已提交
425
{
426 427 428
	struct acpi_device *device = root->device;
	int domain = root->segment;
	int bus = root->secondary.start;
L
Linus Torvalds 已提交
429
	struct pci_controller *controller;
430 431
	struct pci_root_info *info = NULL;
	int busnum = root->secondary.start;
L
Linus Torvalds 已提交
432
	struct pci_bus *pbus;
433
	int pxm, ret;
L
Linus Torvalds 已提交
434 435 436

	controller = alloc_pci_controller(domain);
	if (!controller)
437
		return NULL;
L
Linus Torvalds 已提交
438

439
	controller->companion = device;
L
Linus Torvalds 已提交
440

441
	pxm = acpi_get_pxm(device->handle);
442 443
#ifdef CONFIG_NUMA
	if (pxm >= 0)
444
		controller->node = pxm_to_node(pxm);
445 446
#endif

447 448
	info = kzalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
449
		dev_err(&device->dev,
450
				"pci_bus %04x:%02x: ignored (out of memory)\n",
451 452 453
				domain, busnum);
		kfree(controller);
		return NULL;
454 455
	}

456
	info->controller = controller;
457
	INIT_LIST_HEAD(&info->io_resources);
458
	INIT_LIST_HEAD(&info->resources);
L
Linus Torvalds 已提交
459

460 461 462 463 464
	ret = probe_pci_root_info(info, device, busnum, domain);
	if (ret) {
		kfree(info->controller);
		kfree(info);
		return NULL;
L
Luck, Tony 已提交
465
	}
466 467
	/* insert busn resource at first */
	pci_add_resource(&info->resources, &root->secondary);
468 469 470 471 472 473
	/*
	 * See arch/x86/pci/acpi.c.
	 * The desired pci bus might already be scanned in a quirk. We
	 * should handle the case here, but it appears that IA64 hasn't
	 * such quirk. So we just ignore the case now.
	 */
474
	pbus = pci_create_root_bus(NULL, bus, &pci_root_ops, controller,
475
				   &info->resources);
476
	if (!pbus) {
477
		pci_free_resource_list(&info->resources);
478
		__release_pci_root_info(info);
479
		return NULL;
480
	}
L
Linus Torvalds 已提交
481

482 483
	pci_set_host_bridge_release(to_pci_host_bridge(pbus->bridge),
			release_pci_root_info, info);
484
	pci_scan_child_bus(pbus);
L
Linus Torvalds 已提交
485 486 487
	return pbus;
}

488 489 490 491
int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
{
	struct pci_controller *controller = bridge->bus->sysdata;

492
	ACPI_COMPANION_SET(&bridge->dev, controller->companion);
493 494 495
	return 0;
}

496
static int is_valid_resource(struct pci_dev *dev, int idx)
497 498
{
	unsigned int i, type_mask = IORESOURCE_IO | IORESOURCE_MEM;
499
	struct resource *devr = &dev->resource[idx], *busr;
500 501 502 503

	if (!dev->bus)
		return 0;

504
	pci_bus_for_each_resource(dev->bus, busr, i) {
505 506 507 508 509 510 511 512 513
		if (!busr || ((busr->flags ^ devr->flags) & type_mask))
			continue;
		if ((devr->start) && (devr->start >= busr->start) &&
				(devr->end <= busr->end))
			return 1;
	}
	return 0;
}

514
static void pcibios_fixup_resources(struct pci_dev *dev, int start, int limit)
L
Linus Torvalds 已提交
515 516 517
{
	int i;

518
	for (i = start; i < limit; i++) {
L
Linus Torvalds 已提交
519 520
		if (!dev->resource[i].flags)
			continue;
521 522
		if ((is_valid_resource(dev, i)))
			pci_claim_resource(dev, i);
L
Linus Torvalds 已提交
523 524 525
	}
}

526
void pcibios_fixup_device_resources(struct pci_dev *dev)
527 528 529
{
	pcibios_fixup_resources(dev, 0, PCI_BRIDGE_RESOURCES);
}
J
John Keller 已提交
530
EXPORT_SYMBOL_GPL(pcibios_fixup_device_resources);
531

532
static void pcibios_fixup_bridge_resources(struct pci_dev *dev)
533 534 535 536
{
	pcibios_fixup_resources(dev, PCI_BRIDGE_RESOURCES, PCI_NUM_RESOURCES);
}

L
Linus Torvalds 已提交
537 538 539
/*
 *  Called after each bus is probed, but before its children are examined.
 */
540
void pcibios_fixup_bus(struct pci_bus *b)
L
Linus Torvalds 已提交
541 542 543
{
	struct pci_dev *dev;

544 545
	if (b->self) {
		pci_read_bridge_bases(b);
546
		pcibios_fixup_bridge_resources(b->self);
547
	}
L
Linus Torvalds 已提交
548 549
	list_for_each_entry(dev, &b->devices, bus_list)
		pcibios_fixup_device_resources(dev);
J
John Keller 已提交
550
	platform_pci_fixup_bus(b);
L
Linus Torvalds 已提交
551 552
}

553 554 555 556 557 558 559 560 561 562
void pcibios_add_bus(struct pci_bus *bus)
{
	acpi_pci_add_bus(bus);
}

void pcibios_remove_bus(struct pci_bus *bus)
{
	acpi_pci_remove_bus(bus);
}

563 564 565 566 567
void pcibios_set_master (struct pci_dev *dev)
{
	/* No special bus mastering setup handling */
}

L
Linus Torvalds 已提交
568 569 570 571 572
int
pcibios_enable_device (struct pci_dev *dev, int mask)
{
	int ret;

573
	ret = pci_enable_resources(dev, mask);
L
Linus Torvalds 已提交
574 575 576
	if (ret < 0)
		return ret;

577 578 579
	if (!dev->msi_enabled)
		return acpi_pci_irq_enable(dev);
	return 0;
L
Linus Torvalds 已提交
580 581 582 583 584
}

void
pcibios_disable_device (struct pci_dev *dev)
{
585
	BUG_ON(atomic_read(&dev->enable_cnt));
586 587
	if (!dev->msi_enabled)
		acpi_pci_irq_disable(dev);
L
Linus Torvalds 已提交
588 589
}

590
resource_size_t
591
pcibios_align_resource (void *data, const struct resource *res,
592
		        resource_size_t size, resource_size_t align)
L
Linus Torvalds 已提交
593
{
594
	return res->start;
L
Linus Torvalds 已提交
595 596 597 598 599 600
}

int
pci_mmap_page_range (struct pci_dev *dev, struct vm_area_struct *vma,
		     enum pci_mmap_state mmap_state, int write_combine)
{
601 602 603
	unsigned long size = vma->vm_end - vma->vm_start;
	pgprot_t prot;

L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616
	/*
	 * I/O space cannot be accessed via normal processor loads and
	 * stores on this platform.
	 */
	if (mmap_state == pci_mmap_io)
		/*
		 * XXX we could relax this for I/O spaces for which ACPI
		 * indicates that the space is 1-to-1 mapped.  But at the
		 * moment, we don't support multiple PCI address spaces and
		 * the legacy I/O space is not 1-to-1 mapped, so this is moot.
		 */
		return -EINVAL;

617 618 619 620 621 622
	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
		return -EINVAL;

	prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
				    vma->vm_page_prot);

L
Linus Torvalds 已提交
623
	/*
624 625 626
	 * If the user requested WC, the kernel uses UC or WC for this region,
	 * and the chipset supports WC, we can use WC. Otherwise, we have to
	 * use the same attribute the kernel uses.
L
Linus Torvalds 已提交
627
	 */
628 629 630 631
	if (write_combine &&
	    ((pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_UC ||
	     (pgprot_val(prot) & _PAGE_MA_MASK) == _PAGE_MA_WC) &&
	    efi_range_is_wc(vma->vm_start, vma->vm_end - vma->vm_start))
L
Linus Torvalds 已提交
632 633
		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
	else
634
		vma->vm_page_prot = prot;
L
Linus Torvalds 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
			     vma->vm_end - vma->vm_start, vma->vm_page_prot))
		return -EAGAIN;

	return 0;
}

/**
 * ia64_pci_get_legacy_mem - generic legacy mem routine
 * @bus: bus to get legacy memory base address for
 *
 * Find the base of legacy memory for @bus.  This is typically the first
 * megabyte of bus address space for @bus or is simply 0 on platforms whose
 * chipsets support legacy I/O and memory routing.  Returns the base address
 * or an error pointer if an error occurred.
 *
 * This is the ia64 generic version of this routine.  Other platforms
 * are free to override it with a machine vector.
 */
char *ia64_pci_get_legacy_mem(struct pci_bus *bus)
{
	return (char *)__IA64_UNCACHED_OFFSET;
}

/**
 * pci_mmap_legacy_page_range - map legacy memory space to userland
 * @bus: bus whose legacy space we're mapping
 * @vma: vma passed in by mmap
 *
 * Map legacy memory space for this device back to userspace using a machine
 * vector to get the base address.
 */
int
669 670
pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
			   enum pci_mmap_state mmap_state)
L
Linus Torvalds 已提交
671
{
672 673
	unsigned long size = vma->vm_end - vma->vm_start;
	pgprot_t prot;
L
Linus Torvalds 已提交
674 675
	char *addr;

676 677 678 679
	/* We only support mmap'ing of legacy memory space */
	if (mmap_state != pci_mmap_mem)
		return -ENOSYS;

680 681 682 683
	/*
	 * Avoid attribute aliasing.  See Documentation/ia64/aliasing.txt
	 * for more details.
	 */
684
	if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
685 686 687 688
		return -EINVAL;
	prot = phys_mem_access_prot(NULL, vma->vm_pgoff, size,
				    vma->vm_page_prot);

L
Linus Torvalds 已提交
689 690 691 692 693
	addr = pci_get_legacy_mem(bus);
	if (IS_ERR(addr))
		return PTR_ERR(addr);

	vma->vm_pgoff += (unsigned long)addr >> PAGE_SHIFT;
694
	vma->vm_page_prot = prot;
L
Linus Torvalds 已提交
695 696

	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
697
			    size, vma->vm_page_prot))
L
Linus Torvalds 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		return -EAGAIN;

	return 0;
}

/**
 * ia64_pci_legacy_read - read from legacy I/O space
 * @bus: bus to read
 * @port: legacy port value
 * @val: caller allocated storage for returned value
 * @size: number of bytes to read
 *
 * Simply reads @size bytes from @port and puts the result in @val.
 *
 * Again, this (and the write routine) are generic versions that can be
 * overridden by the platform.  This is necessary on platforms that don't
 * support legacy I/O routing or that hard fail on legacy I/O timeouts.
 */
int ia64_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
{
	int ret = size;

	switch (size) {
	case 1:
		*val = inb(port);
		break;
	case 2:
		*val = inw(port);
		break;
	case 4:
		*val = inl(port);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

/**
 * ia64_pci_legacy_write - perform a legacy I/O write
 * @bus: bus pointer
 * @port: port to write
 * @val: value to write
 * @size: number of bytes to write from @val
 *
 * Simply writes @size bytes of @val to @port.
 */
747
int ia64_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
L
Linus Torvalds 已提交
748
{
749
	int ret = size;
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

	switch (size) {
	case 1:
		outb(val, port);
		break;
	case 2:
		outw(val, port);
		break;
	case 4:
		outl(val, port);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

/**
770
 * set_pci_cacheline_size - determine cacheline size for PCI devices
L
Linus Torvalds 已提交
771 772 773 774 775 776
 *
 * We want to use the line-size of the outer-most cache.  We assume
 * that this line-size is the same for all CPUs.
 *
 * Code mostly taken from arch/ia64/kernel/palinfo.c:cache_info().
 */
777
static void __init set_pci_dfl_cacheline_size(void)
L
Linus Torvalds 已提交
778
{
779 780
	unsigned long levels, unique_caches;
	long status;
L
Linus Torvalds 已提交
781 782 783 784
	pal_cache_config_info_t cci;

	status = ia64_pal_cache_summary(&levels, &unique_caches);
	if (status != 0) {
785
		pr_err("%s: ia64_pal_cache_summary() failed "
786
			"(status=%ld)\n", __func__, status);
787
		return;
L
Linus Torvalds 已提交
788 789
	}

790 791
	status = ia64_pal_cache_config_info(levels - 1,
				/* cache_type (data_or_unified)= */ 2, &cci);
L
Linus Torvalds 已提交
792
	if (status != 0) {
793
		pr_err("%s: ia64_pal_cache_config_info() failed "
794
			"(status=%ld)\n", __func__, status);
795
		return;
L
Linus Torvalds 已提交
796
	}
797
	pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
L
Linus Torvalds 已提交
798 799
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
u64 ia64_dma_get_required_mask(struct device *dev)
{
	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
	u64 mask;

	if (!high_totalram) {
		/* convert to mask just covering totalram */
		low_totalram = (1 << (fls(low_totalram) - 1));
		low_totalram += low_totalram - 1;
		mask = low_totalram;
	} else {
		high_totalram = (1 << (fls(high_totalram) - 1));
		high_totalram += high_totalram - 1;
		mask = (((u64)high_totalram) << 32) + 0xffffffff;
	}
	return mask;
}
EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);

u64 dma_get_required_mask(struct device *dev)
{
	return platform_dma_get_required_mask(dev);
}
EXPORT_SYMBOL_GPL(dma_get_required_mask);

826 827
static int __init pcibios_init(void)
{
828
	set_pci_dfl_cacheline_size();
829
	return 0;
L
Linus Torvalds 已提交
830
}
831 832

subsys_initcall(pcibios_init);