pci-ioda.c 88.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Support PCI/PCIe on PowerNV platforms
 *
 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12
#undef DEBUG
13 14 15

#include <linux/kernel.h>
#include <linux/pci.h>
16
#include <linux/crash_dump.h>
17
#include <linux/debugfs.h>
18 19 20 21 22 23 24
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bootmem.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/msi.h>
25
#include <linux/memblock.h>
26
#include <linux/iommu.h>
27
#include <linux/rculist.h>
28
#include <linux/sizes.h>
29 30 31 32 33 34

#include <asm/sections.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/pci-bridge.h>
#include <asm/machdep.h>
35
#include <asm/msi_bitmap.h>
36 37 38 39
#include <asm/ppc-pci.h>
#include <asm/opal.h>
#include <asm/iommu.h>
#include <asm/tce.h>
40
#include <asm/xics.h>
41
#include <asm/debug.h>
42
#include <asm/firmware.h>
43
#include <asm/pnv-pci.h>
44
#include <asm/mmzone.h>
45

46
#include <misc/cxl-base.h>
47 48 49 50

#include "powernv.h"
#include "pci.h"

51 52 53
/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
#define TCE32_TABLE_SIZE	((0x10000000 / 0x1000) * 8)

54 55 56
#define POWERNV_IOMMU_DEFAULT_LEVELS	1
#define POWERNV_IOMMU_MAX_LEVELS	5

57 58
static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);

59 60 61 62 63 64 65 66 67 68 69 70
static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
			    const char *fmt, ...)
{
	struct va_format vaf;
	va_list args;
	char pfix[32];

	va_start(args, fmt);

	vaf.fmt = fmt;
	vaf.va = &args;

71
	if (pe->flags & PNV_IODA_PE_DEV)
72
		strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
73
	else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
74 75
		sprintf(pfix, "%04x:%02x     ",
			pci_domain_nr(pe->pbus), pe->pbus->number);
76 77 78 79 80 81 82
#ifdef CONFIG_PCI_IOV
	else if (pe->flags & PNV_IODA_PE_VF)
		sprintf(pfix, "%04x:%02x:%2x.%d",
			pci_domain_nr(pe->parent_dev->bus),
			(pe->rid & 0xff00) >> 8,
			PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
#endif /* CONFIG_PCI_IOV*/
83 84 85 86 87 88

	printk("%spci %s: [PE# %.3d] %pV",
	       level, pfix, pe->pe_number, &vaf);

	va_end(args);
}
89

90 91 92 93 94 95
#define pe_err(pe, fmt, ...)					\
	pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
#define pe_warn(pe, fmt, ...)					\
	pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
#define pe_info(pe, fmt, ...)					\
	pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static bool pnv_iommu_bypass_disabled __read_mostly;

static int __init iommu_setup(char *str)
{
	if (!str)
		return -EINVAL;

	while (*str) {
		if (!strncmp(str, "nobypass", 8)) {
			pnv_iommu_bypass_disabled = true;
			pr_info("PowerNV: IOMMU bypass window disabled.\n");
			break;
		}
		str += strcspn(str, ",");
		if (*str == ',')
			str++;
	}

	return 0;
}
early_param("iommu", iommu_setup);

119 120 121 122 123 124
static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
{
	return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
		(IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
}

125 126 127 128 129 130 131 132
static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
{
	if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
		pr_warn("%s: Invalid PE %d on PHB#%x\n",
			__func__, pe_no, phb->hose->global_number);
		return;
	}

133 134 135
	if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
		pr_debug("%s: PE %d was reserved on PHB#%x\n",
			 __func__, pe_no, phb->hose->global_number);
136 137 138 139 140

	phb->ioda.pe_array[pe_no].phb = phb;
	phb->ioda.pe_array[pe_no].pe_number = pe_no;
}

141
static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
142 143 144 145 146 147 148 149 150 151
{
	unsigned long pe;

	do {
		pe = find_next_zero_bit(phb->ioda.pe_alloc,
					phb->ioda.total_pe, 0);
		if (pe >= phb->ioda.total_pe)
			return IODA_INVALID_PE;
	} while(test_and_set_bit(pe, phb->ioda.pe_alloc));

152
	phb->ioda.pe_array[pe].phb = phb;
153 154 155 156
	phb->ioda.pe_array[pe].pe_number = pe;
	return pe;
}

157
static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
158 159 160 161 162 163 164
{
	WARN_ON(phb->ioda.pe_array[pe].pdev);

	memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
	clear_bit(pe, phb->ioda.pe_alloc);
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* The default M64 BAR is shared by all PEs */
static int pnv_ioda2_init_m64(struct pnv_phb *phb)
{
	const char *desc;
	struct resource *r;
	s64 rc;

	/* Configure the default M64 BAR */
	rc = opal_pci_set_phb_mem_window(phb->opal_id,
					 OPAL_M64_WINDOW_TYPE,
					 phb->ioda.m64_bar_idx,
					 phb->ioda.m64_base,
					 0, /* unused */
					 phb->ioda.m64_size);
	if (rc != OPAL_SUCCESS) {
		desc = "configuring";
		goto fail;
	}

	/* Enable the default M64 BAR */
	rc = opal_pci_phb_mmio_enable(phb->opal_id,
				      OPAL_M64_WINDOW_TYPE,
				      phb->ioda.m64_bar_idx,
				      OPAL_ENABLE_M64_SPLIT);
	if (rc != OPAL_SUCCESS) {
		desc = "enabling";
		goto fail;
	}

	/* Mark the M64 BAR assigned */
	set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);

	/*
	 * Strip off the segment used by the reserved PE, which is
	 * expected to be 0 or last one of PE capabicity.
	 */
	r = &phb->hose->mem_resources[1];
	if (phb->ioda.reserved_pe == 0)
		r->start += phb->ioda.m64_segsize;
	else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
		r->end -= phb->ioda.m64_segsize;
	else
		pr_warn("  Cannot strip M64 segment for reserved PE#%d\n",
			phb->ioda.reserved_pe);

	return 0;

fail:
	pr_warn("  Failure %lld %s M64 BAR#%d\n",
		rc, desc, phb->ioda.m64_bar_idx);
	opal_pci_phb_mmio_enable(phb->opal_id,
				 OPAL_M64_WINDOW_TYPE,
				 phb->ioda.m64_bar_idx,
				 OPAL_DISABLE_M64);
	return -EIO;
}

222 223
static void pnv_ioda2_reserve_dev_m64_pe(struct pci_dev *pdev,
					 unsigned long *pe_bitmap)
224
{
225 226
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
227
	struct resource *r;
228 229 230 231 232 233 234 235 236
	resource_size_t base, sgsz, start, end;
	int segno, i;

	base = phb->ioda.m64_base;
	sgsz = phb->ioda.m64_segsize;
	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
		r = &pdev->resource[i];
		if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
			continue;
237

238 239 240 241 242 243 244
		start = _ALIGN_DOWN(r->start - base, sgsz);
		end = _ALIGN_UP(r->end - base, sgsz);
		for (segno = start / sgsz; segno < end / sgsz; segno++) {
			if (pe_bitmap)
				set_bit(segno, pe_bitmap);
			else
				pnv_ioda_reserve_pe(phb, segno);
245 246 247 248
		}
	}
}

249 250 251
static void pnv_ioda2_reserve_m64_pe(struct pci_bus *bus,
				     unsigned long *pe_bitmap,
				     bool all)
252 253
{
	struct pci_dev *pdev;
254 255 256 257 258 259 260 261 262 263

	list_for_each_entry(pdev, &bus->devices, bus_list) {
		pnv_ioda2_reserve_dev_m64_pe(pdev, pe_bitmap);

		if (all && pdev->subordinate)
			pnv_ioda2_reserve_m64_pe(pdev->subordinate,
						 pe_bitmap, all);
	}
}

264
static int pnv_ioda2_pick_m64_pe(struct pci_bus *bus, bool all)
265
{
266 267
	struct pci_controller *hose = pci_bus_to_host(bus);
	struct pnv_phb *phb = hose->private_data;
268 269
	struct pnv_ioda_pe *master_pe, *pe;
	unsigned long size, *pe_alloc;
270
	int i;
271 272 273 274 275 276 277 278 279 280 281 282 283 284

	/* Root bus shouldn't use M64 */
	if (pci_is_root_bus(bus))
		return IODA_INVALID_PE;

	/* Allocate bitmap */
	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
	pe_alloc = kzalloc(size, GFP_KERNEL);
	if (!pe_alloc) {
		pr_warn("%s: Out of memory !\n",
			__func__);
		return IODA_INVALID_PE;
	}

285 286
	/* Figure out reserved PE numbers by the PE */
	pnv_ioda2_reserve_m64_pe(bus, pe_alloc, all);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	/*
	 * the current bus might not own M64 window and that's all
	 * contributed by its child buses. For the case, we needn't
	 * pick M64 dependent PE#.
	 */
	if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
		kfree(pe_alloc);
		return IODA_INVALID_PE;
	}

	/*
	 * Figure out the master PE and put all slave PEs to master
	 * PE's list to form compound PE.
	 */
	master_pe = NULL;
	i = -1;
	while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
		phb->ioda.total_pe) {
		pe = &phb->ioda.pe_array[i];

		if (!master_pe) {
			pe->flags |= PNV_IODA_PE_MASTER;
			INIT_LIST_HEAD(&pe->slaves);
			master_pe = pe;
		} else {
			pe->flags |= PNV_IODA_PE_SLAVE;
			pe->master = master_pe;
			list_add_tail(&pe->list, &master_pe->slaves);
		}
	}

	kfree(pe_alloc);
	return master_pe->pe_number;
}

static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
{
	struct pci_controller *hose = phb->hose;
	struct device_node *dn = hose->dn;
	struct resource *res;
	const u32 *r;
	u64 pci_addr;

331 332 333 334 335 336
	/* FIXME: Support M64 for P7IOC */
	if (phb->type != PNV_PHB_IODA2) {
		pr_info("  Not support M64 window\n");
		return;
	}

337
	if (!firmware_has_feature(FW_FEATURE_OPAL)) {
338 339 340 341 342 343 344 345 346 347 348 349
		pr_info("  Firmware too old to support M64 window\n");
		return;
	}

	r = of_get_property(dn, "ibm,opal-m64-window", NULL);
	if (!r) {
		pr_info("  No <ibm,opal-m64-window> on %s\n",
			dn->full_name);
		return;
	}

	res = &hose->mem_resources[1];
350
	res->name = dn->full_name;
351 352 353 354 355 356 357 358 359 360
	res->start = of_translate_address(dn, r + 2);
	res->end = res->start + of_read_number(r + 4, 2) - 1;
	res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
	pci_addr = of_read_number(r, 2);
	hose->mem_offset[1] = res->start - pci_addr;

	phb->ioda.m64_size = resource_size(res);
	phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
	phb->ioda.m64_base = pci_addr;

361 362 363
	pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
			res->start, res->end, pci_addr);

364 365 366
	/* Use last M64 BAR to cover M64 window */
	phb->ioda.m64_bar_idx = 15;
	phb->init_m64 = pnv_ioda2_init_m64;
367
	phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
368 369 370
	phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
}

G
Gavin Shan 已提交
371 372 373 374 375 376 377 378 379
static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
{
	struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
	struct pnv_ioda_pe *slave;
	s64 rc;

	/* Fetch master PE */
	if (pe->flags & PNV_IODA_PE_SLAVE) {
		pe = pe->master;
380 381 382
		if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
			return;

G
Gavin Shan 已提交
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
		pe_no = pe->pe_number;
	}

	/* Freeze master PE */
	rc = opal_pci_eeh_freeze_set(phb->opal_id,
				     pe_no,
				     OPAL_EEH_ACTION_SET_FREEZE_ALL);
	if (rc != OPAL_SUCCESS) {
		pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
			__func__, rc, phb->hose->global_number, pe_no);
		return;
	}

	/* Freeze slave PEs */
	if (!(pe->flags & PNV_IODA_PE_MASTER))
		return;

	list_for_each_entry(slave, &pe->slaves, list) {
		rc = opal_pci_eeh_freeze_set(phb->opal_id,
					     slave->pe_number,
					     OPAL_EEH_ACTION_SET_FREEZE_ALL);
		if (rc != OPAL_SUCCESS)
			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
				__func__, rc, phb->hose->global_number,
				slave->pe_number);
	}
}

411
static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
G
Gavin Shan 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
	struct pnv_ioda_pe *pe, *slave;
	s64 rc;

	/* Find master PE */
	pe = &phb->ioda.pe_array[pe_no];
	if (pe->flags & PNV_IODA_PE_SLAVE) {
		pe = pe->master;
		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
		pe_no = pe->pe_number;
	}

	/* Clear frozen state for master PE */
	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
	if (rc != OPAL_SUCCESS) {
		pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
			__func__, rc, opt, phb->hose->global_number, pe_no);
		return -EIO;
	}

	if (!(pe->flags & PNV_IODA_PE_MASTER))
		return 0;

	/* Clear frozen state for slave PEs */
	list_for_each_entry(slave, &pe->slaves, list) {
		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
					     slave->pe_number,
					     opt);
		if (rc != OPAL_SUCCESS) {
			pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
				__func__, rc, opt, phb->hose->global_number,
				slave->pe_number);
			return -EIO;
		}
	}

	return 0;
}

static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
{
	struct pnv_ioda_pe *slave, *pe;
	u8 fstate, state;
	__be16 pcierr;
	s64 rc;

	/* Sanity check on PE number */
	if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
		return OPAL_EEH_STOPPED_PERM_UNAVAIL;

	/*
	 * Fetch the master PE and the PE instance might be
	 * not initialized yet.
	 */
	pe = &phb->ioda.pe_array[pe_no];
	if (pe->flags & PNV_IODA_PE_SLAVE) {
		pe = pe->master;
		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
		pe_no = pe->pe_number;
	}

	/* Check the master PE */
	rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
					&state, &pcierr, NULL);
	if (rc != OPAL_SUCCESS) {
		pr_warn("%s: Failure %lld getting "
			"PHB#%x-PE#%x state\n",
			__func__, rc,
			phb->hose->global_number, pe_no);
		return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
	}

	/* Check the slave PE */
	if (!(pe->flags & PNV_IODA_PE_MASTER))
		return state;

	list_for_each_entry(slave, &pe->slaves, list) {
		rc = opal_pci_eeh_freeze_status(phb->opal_id,
						slave->pe_number,
						&fstate,
						&pcierr,
						NULL);
		if (rc != OPAL_SUCCESS) {
			pr_warn("%s: Failure %lld getting "
				"PHB#%x-PE#%x state\n",
				__func__, rc,
				phb->hose->global_number, slave->pe_number);
			return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
		}

		/*
		 * Override the result based on the ascending
		 * priority.
		 */
		if (fstate > state)
			state = fstate;
	}

	return state;
}

513 514 515 516
/* Currently those 2 are only used when MSIs are enabled, this will change
 * but in the meantime, we need to protect them to avoid warnings
 */
#ifdef CONFIG_PCI_MSI
517
static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
518 519 520
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
521
	struct pci_dn *pdn = pci_get_pdn(dev);
522 523 524 525 526 527 528 529 530

	if (!pdn)
		return NULL;
	if (pdn->pe_number == IODA_INVALID_PE)
		return NULL;
	return &phb->ioda.pe_array[pdn->pe_number];
}
#endif /* CONFIG_PCI_MSI */

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
				  struct pnv_ioda_pe *parent,
				  struct pnv_ioda_pe *child,
				  bool is_add)
{
	const char *desc = is_add ? "adding" : "removing";
	uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
			      OPAL_REMOVE_PE_FROM_DOMAIN;
	struct pnv_ioda_pe *slave;
	long rc;

	/* Parent PE affects child PE */
	rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
				child->pe_number, op);
	if (rc != OPAL_SUCCESS) {
		pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
			rc, desc);
		return -ENXIO;
	}

	if (!(child->flags & PNV_IODA_PE_MASTER))
		return 0;

	/* Compound case: parent PE affects slave PEs */
	list_for_each_entry(slave, &child->slaves, list) {
		rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
					slave->pe_number, op);
		if (rc != OPAL_SUCCESS) {
			pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
				rc, desc);
			return -ENXIO;
		}
	}

	return 0;
}

static int pnv_ioda_set_peltv(struct pnv_phb *phb,
			      struct pnv_ioda_pe *pe,
			      bool is_add)
{
	struct pnv_ioda_pe *slave;
573
	struct pci_dev *pdev = NULL;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	int ret;

	/*
	 * Clear PE frozen state. If it's master PE, we need
	 * clear slave PE frozen state as well.
	 */
	if (is_add) {
		opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
					  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
		if (pe->flags & PNV_IODA_PE_MASTER) {
			list_for_each_entry(slave, &pe->slaves, list)
				opal_pci_eeh_freeze_clear(phb->opal_id,
							  slave->pe_number,
							  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
		}
	}

	/*
	 * Associate PE in PELT. We need add the PE into the
	 * corresponding PELT-V as well. Otherwise, the error
	 * originated from the PE might contribute to other
	 * PEs.
	 */
	ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
	if (ret)
		return ret;

	/* For compound PEs, any one affects all of them */
	if (pe->flags & PNV_IODA_PE_MASTER) {
		list_for_each_entry(slave, &pe->slaves, list) {
			ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
			if (ret)
				return ret;
		}
	}

	if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
		pdev = pe->pbus->self;
612
	else if (pe->flags & PNV_IODA_PE_DEV)
613
		pdev = pe->pdev->bus->self;
614 615
#ifdef CONFIG_PCI_IOV
	else if (pe->flags & PNV_IODA_PE_VF)
616
		pdev = pe->parent_dev;
617
#endif /* CONFIG_PCI_IOV */
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	while (pdev) {
		struct pci_dn *pdn = pci_get_pdn(pdev);
		struct pnv_ioda_pe *parent;

		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
			parent = &phb->ioda.pe_array[pdn->pe_number];
			ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
			if (ret)
				return ret;
		}

		pdev = pdev->bus->self;
	}

	return 0;
}

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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
#ifdef CONFIG_PCI_IOV
static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
{
	struct pci_dev *parent;
	uint8_t bcomp, dcomp, fcomp;
	int64_t rc;
	long rid_end, rid;

	/* Currently, we just deconfigure VF PE. Bus PE will always there.*/
	if (pe->pbus) {
		int count;

		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
		parent = pe->pbus->self;
		if (pe->flags & PNV_IODA_PE_BUS_ALL)
			count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
		else
			count = 1;

		switch(count) {
		case  1: bcomp = OpalPciBusAll;         break;
		case  2: bcomp = OpalPciBus7Bits;       break;
		case  4: bcomp = OpalPciBus6Bits;       break;
		case  8: bcomp = OpalPciBus5Bits;       break;
		case 16: bcomp = OpalPciBus4Bits;       break;
		case 32: bcomp = OpalPciBus3Bits;       break;
		default:
			dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
			        count);
			/* Do an exact match only */
			bcomp = OpalPciBusAll;
		}
		rid_end = pe->rid + (count << 8);
	} else {
		if (pe->flags & PNV_IODA_PE_VF)
			parent = pe->parent_dev;
		else
			parent = pe->pdev->bus->self;
		bcomp = OpalPciBusAll;
		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
		rid_end = pe->rid + 1;
	}

	/* Clear the reverse map */
	for (rid = pe->rid; rid < rid_end; rid++)
		phb->ioda.pe_rmap[rid] = 0;

	/* Release from all parents PELT-V */
	while (parent) {
		struct pci_dn *pdn = pci_get_pdn(parent);
		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
			rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
						pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
			/* XXX What to do in case of error ? */
		}
		parent = parent->bus->self;
	}

695
	opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
				  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);

	/* Disassociate PE in PELT */
	rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
				pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
	if (rc)
		pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
			     bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
	if (rc)
		pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);

	pe->pbus = NULL;
	pe->pdev = NULL;
	pe->parent_dev = NULL;

	return 0;
}
#endif /* CONFIG_PCI_IOV */

716
static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
717 718 719 720 721 722 723 724 725 726 727 728
{
	struct pci_dev *parent;
	uint8_t bcomp, dcomp, fcomp;
	long rc, rid_end, rid;

	/* Bus validation ? */
	if (pe->pbus) {
		int count;

		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
		parent = pe->pbus->self;
729 730 731 732 733
		if (pe->flags & PNV_IODA_PE_BUS_ALL)
			count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
		else
			count = 1;

734 735 736 737 738 739 740 741
		switch(count) {
		case  1: bcomp = OpalPciBusAll;		break;
		case  2: bcomp = OpalPciBus7Bits;	break;
		case  4: bcomp = OpalPciBus6Bits;	break;
		case  8: bcomp = OpalPciBus5Bits;	break;
		case 16: bcomp = OpalPciBus4Bits;	break;
		case 32: bcomp = OpalPciBus3Bits;	break;
		default:
742 743
			dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
			        count);
744 745 746 747 748
			/* Do an exact match only */
			bcomp = OpalPciBusAll;
		}
		rid_end = pe->rid + (count << 8);
	} else {
749 750 751 752 753 754
#ifdef CONFIG_PCI_IOV
		if (pe->flags & PNV_IODA_PE_VF)
			parent = pe->parent_dev;
		else
#endif /* CONFIG_PCI_IOV */
			parent = pe->pdev->bus->self;
755 756 757 758 759 760
		bcomp = OpalPciBusAll;
		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
		rid_end = pe->rid + 1;
	}

761 762 763 764 765 766
	/*
	 * Associate PE in PELT. We need add the PE into the
	 * corresponding PELT-V as well. Otherwise, the error
	 * originated from the PE might contribute to other
	 * PEs.
	 */
767 768 769 770 771 772
	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
			     bcomp, dcomp, fcomp, OPAL_MAP_PE);
	if (rc) {
		pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
		return -ENXIO;
	}
773

774 775 776 777 778 779
	/*
	 * Configure PELTV. NPUs don't have a PELTV table so skip
	 * configuration on them.
	 */
	if (phb->type != PNV_PHB_NPU)
		pnv_ioda_set_peltv(phb, pe, true);
780 781 782 783 784 785

	/* Setup reverse map */
	for (rid = pe->rid; rid < rid_end; rid++)
		phb->ioda.pe_rmap[rid] = pe->pe_number;

	/* Setup one MVTs on IODA1 */
786 787 788 789 790 791 792 793 794 795 796 797 798 799
	if (phb->type != PNV_PHB_IODA1) {
		pe->mve_number = 0;
		goto out;
	}

	pe->mve_number = pe->pe_number;
	rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
	if (rc != OPAL_SUCCESS) {
		pe_err(pe, "OPAL error %ld setting up MVE %d\n",
		       rc, pe->mve_number);
		pe->mve_number = -1;
	} else {
		rc = opal_pci_set_mve_enable(phb->opal_id,
					     pe->mve_number, OPAL_ENABLE_MVE);
800
		if (rc) {
801
			pe_err(pe, "OPAL error %ld enabling MVE %d\n",
802 803 804
			       rc, pe->mve_number);
			pe->mve_number = -1;
		}
805
	}
806

807
out:
808 809 810
	return 0;
}

811 812
static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
				       struct pnv_ioda_pe *pe)
813 814 815
{
	struct pnv_ioda_pe *lpe;

816
	list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
817
		if (lpe->dma_weight < pe->dma_weight) {
818
			list_add_tail(&pe->dma_link, &lpe->dma_link);
819 820 821
			return;
		}
	}
822
	list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
{
	/* This is quite simplistic. The "base" weight of a device
	 * is 10. 0 means no DMA is to be accounted for it.
	 */

	/* If it's a bridge, no DMA */
	if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
		return 0;

	/* Reduce the weight of slow USB controllers */
	if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
	    dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
	    dev->class == PCI_CLASS_SERIAL_USB_EHCI)
		return 3;

	/* Increase the weight of RAID (includes Obsidian) */
	if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
		return 15;

	/* Default */
	return 10;
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
#ifdef CONFIG_PCI_IOV
static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
{
	struct pci_dn *pdn = pci_get_pdn(dev);
	int i;
	struct resource *res, res2;
	resource_size_t size;
	u16 num_vfs;

	if (!dev->is_physfn)
		return -EINVAL;

	/*
	 * "offset" is in VFs.  The M64 windows are sized so that when they
	 * are segmented, each segment is the same size as the IOV BAR.
	 * Each segment is in a separate PE, and the high order bits of the
	 * address are the PE number.  Therefore, each VF's BAR is in a
	 * separate PE, and changing the IOV BAR start address changes the
	 * range of PEs the VFs are in.
	 */
	num_vfs = pdn->num_vfs;
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = &dev->resource[i + PCI_IOV_RESOURCES];
		if (!res->flags || !res->parent)
			continue;

		if (!pnv_pci_is_mem_pref_64(res->flags))
			continue;

		/*
		 * The actual IOV BAR range is determined by the start address
		 * and the actual size for num_vfs VFs BAR.  This check is to
		 * make sure that after shifting, the range will not overlap
		 * with another device.
		 */
		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
		res2.flags = res->flags;
		res2.start = res->start + (size * offset);
		res2.end = res2.start + (size * num_vfs) - 1;

		if (res2.end > res->end) {
			dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
				i, &res2, res, num_vfs, offset);
			return -EBUSY;
		}
	}

	/*
	 * After doing so, there would be a "hole" in the /proc/iomem when
	 * offset is a positive value. It looks like the device return some
	 * mmio back to the system, which actually no one could use it.
	 */
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = &dev->resource[i + PCI_IOV_RESOURCES];
		if (!res->flags || !res->parent)
			continue;

		if (!pnv_pci_is_mem_pref_64(res->flags))
			continue;

		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
		res2 = *res;
		res->start += size * offset;

913 914 915
		dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
			 i, &res2, res, (offset > 0) ? "En" : "Dis",
			 num_vfs, offset);
916 917 918 919 920 921
		pci_update_resource(dev, i + PCI_IOV_RESOURCES);
	}
	return 0;
}
#endif /* CONFIG_PCI_IOV */

922
static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
923 924 925
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
926
	struct pci_dn *pdn = pci_get_pdn(dev);
927 928 929 930 931 932 933 934 935 936 937
	struct pnv_ioda_pe *pe;
	int pe_num;

	if (!pdn) {
		pr_err("%s: Device tree node not associated properly\n",
			   pci_name(dev));
		return NULL;
	}
	if (pdn->pe_number != IODA_INVALID_PE)
		return NULL;

938
	pe_num = pnv_ioda_alloc_pe(phb);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
	if (pe_num == IODA_INVALID_PE) {
		pr_warning("%s: Not enough PE# available, disabling device\n",
			   pci_name(dev));
		return NULL;
	}

	/* NOTE: We get only one ref to the pci_dev for the pdn, not for the
	 * pointer in the PE data structure, both should be destroyed at the
	 * same time. However, this needs to be looked at more closely again
	 * once we actually start removing things (Hotplug, SR-IOV, ...)
	 *
	 * At some point we want to remove the PDN completely anyways
	 */
	pe = &phb->ioda.pe_array[pe_num];
	pci_dev_get(dev);
	pdn->pcidev = dev;
	pdn->pe_number = pe_num;
956
	pe->flags = PNV_IODA_PE_DEV;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	pe->pdev = dev;
	pe->pbus = NULL;
	pe->tce32_seg = -1;
	pe->mve_number = -1;
	pe->rid = dev->bus->number << 8 | pdn->devfn;

	pe_info(pe, "Associated device to PE\n");

	if (pnv_ioda_configure_pe(phb, pe)) {
		/* XXX What do we do here ? */
		if (pe_num)
			pnv_ioda_free_pe(phb, pe_num);
		pdn->pe_number = IODA_INVALID_PE;
		pe->pdev = NULL;
		pci_dev_put(dev);
		return NULL;
	}

	/* Assign a DMA weight to the device */
	pe->dma_weight = pnv_ioda_dma_weight(dev);
	if (pe->dma_weight != 0) {
		phb->ioda.dma_weight += pe->dma_weight;
		phb->ioda.dma_pe_count++;
	}

	/* Link the PE */
	pnv_ioda_link_pe_by_weight(phb, pe);

	return pe;
}

static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
{
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
993
		struct pci_dn *pdn = pci_get_pdn(dev);
994 995 996 997 998 999

		if (pdn == NULL) {
			pr_warn("%s: No device node associated with device !\n",
				pci_name(dev));
			continue;
		}
1000
		pdn->pcidev = dev;
1001 1002
		pdn->pe_number = pe->pe_number;
		pe->dma_weight += pnv_ioda_dma_weight(dev);
1003
		if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
1004 1005 1006 1007
			pnv_ioda_setup_same_PE(dev->subordinate, pe);
	}
}

1008 1009 1010 1011 1012 1013
/*
 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
 * single PCI bus. Another one that contains the primary PCI bus and its
 * subordinate PCI devices and buses. The second type of PE is normally
 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
 */
1014
static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
1015
{
1016
	struct pci_controller *hose = pci_bus_to_host(bus);
1017 1018
	struct pnv_phb *phb = hose->private_data;
	struct pnv_ioda_pe *pe;
1019 1020 1021 1022
	int pe_num = IODA_INVALID_PE;

	/* Check if PE is determined by M64 */
	if (phb->pick_m64_pe)
1023
		pe_num = phb->pick_m64_pe(bus, all);
1024 1025 1026 1027

	/* The PE number isn't pinned by M64 */
	if (pe_num == IODA_INVALID_PE)
		pe_num = pnv_ioda_alloc_pe(phb);
1028 1029

	if (pe_num == IODA_INVALID_PE) {
1030 1031
		pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
			__func__, pci_domain_nr(bus), bus->number);
1032 1033 1034 1035
		return;
	}

	pe = &phb->ioda.pe_array[pe_num];
1036
	pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
1037 1038 1039 1040
	pe->pbus = bus;
	pe->pdev = NULL;
	pe->tce32_seg = -1;
	pe->mve_number = -1;
1041
	pe->rid = bus->busn_res.start << 8;
1042 1043
	pe->dma_weight = 0;

1044 1045 1046 1047 1048 1049
	if (all)
		pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
			bus->busn_res.start, bus->busn_res.end, pe_num);
	else
		pe_info(pe, "Secondary bus %d associated with PE#%d\n",
			bus->busn_res.start, pe_num);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	if (pnv_ioda_configure_pe(phb, pe)) {
		/* XXX What do we do here ? */
		if (pe_num)
			pnv_ioda_free_pe(phb, pe_num);
		pe->pbus = NULL;
		return;
	}

	/* Associate it with all child devices */
	pnv_ioda_setup_same_PE(bus, pe);

1062 1063 1064
	/* Put PE to the list */
	list_add_tail(&pe->list, &phb->ioda.pe_list);

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	/* Account for one DMA PE if at least one DMA capable device exist
	 * below the bridge
	 */
	if (pe->dma_weight != 0) {
		phb->ioda.dma_weight += pe->dma_weight;
		phb->ioda.dma_pe_count++;
	}

	/* Link the PE */
	pnv_ioda_link_pe_by_weight(phb, pe);
}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
static void pnv_ioda_setup_dev_PEs(struct pci_bus *bus)
{
	struct pci_bus *child;
	struct pci_dev *pdev;

	list_for_each_entry(pdev, &bus->devices, bus_list)
		pnv_ioda_setup_dev_PE(pdev);

	list_for_each_entry(child, &bus->children, node)
		pnv_ioda_setup_dev_PEs(child);
}

1089
static void pnv_ioda_setup_PEs(struct pci_bus *bus)
1090 1091
{
	struct pci_dev *dev;
1092

1093
	pnv_ioda_setup_bus_PE(bus, false);
1094 1095

	list_for_each_entry(dev, &bus->devices, bus_list) {
1096 1097
		if (dev->subordinate) {
			if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
1098
				pnv_ioda_setup_bus_PE(dev->subordinate, true);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
			else
				pnv_ioda_setup_PEs(dev->subordinate);
		}
	}
}

/*
 * Configure PEs so that the downstream PCI buses and devices
 * could have their associated PE#. Unfortunately, we didn't
 * figure out the way to identify the PLX bridge yet. So we
 * simply put the PCI bus and the subordinate behind the root
 * port to PE# here. The game rule here is expected to be changed
 * as soon as we can detected PLX bridge correctly.
 */
1113
static void pnv_pci_ioda_setup_PEs(void)
1114 1115
{
	struct pci_controller *hose, *tmp;
1116
	struct pnv_phb *phb;
1117 1118

	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1119 1120 1121
		phb = hose->private_data;

		/* M64 layout might affect PE allocation */
1122
		if (phb->reserve_m64_pe)
1123
			phb->reserve_m64_pe(hose->bus, NULL, true);
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133
		/*
		 * On NPU PHB, we expect separate PEs for individual PCI
		 * functions. PCI bus dependent PEs are required for the
		 * remaining types of PHBs.
		 */
		if (phb->type == PNV_PHB_NPU)
			pnv_ioda_setup_dev_PEs(hose->bus);
		else
			pnv_ioda_setup_PEs(hose->bus);
1134 1135 1136
	}
}

G
Gavin Shan 已提交
1137
#ifdef CONFIG_PCI_IOV
1138 1139 1140 1141 1142 1143
static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pci_dn         *pdn;
1144
	int                    i, j;
1145 1146 1147 1148 1149 1150

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
	pdn = pci_get_pdn(pdev);

1151 1152 1153 1154 1155 1156 1157 1158 1159
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
		for (j = 0; j < M64_PER_IOV; j++) {
			if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
				continue;
			opal_pci_phb_mmio_enable(phb->opal_id,
				OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
			clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
			pdn->m64_wins[i][j] = IODA_INVALID_M64;
		}
1160 1161 1162 1163

	return 0;
}

1164
static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
1165 1166 1167 1168 1169 1170 1171
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pci_dn         *pdn;
	unsigned int           win;
	struct resource       *res;
1172
	int                    i, j;
1173
	int64_t                rc;
1174 1175 1176 1177 1178
	int                    total_vfs;
	resource_size_t        size, start;
	int                    pe_num;
	int                    vf_groups;
	int                    vf_per_group;
1179 1180 1181 1182 1183

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
	pdn = pci_get_pdn(pdev);
1184
	total_vfs = pci_sriov_get_totalvfs(pdev);
1185 1186 1187

	/* Initialize the m64_wins to IODA_INVALID_M64 */
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		for (j = 0; j < M64_PER_IOV; j++)
			pdn->m64_wins[i][j] = IODA_INVALID_M64;

	if (pdn->m64_per_iov == M64_PER_IOV) {
		vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
		vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
			roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
	} else {
		vf_groups = 1;
		vf_per_group = 1;
	}
1199 1200 1201 1202 1203 1204 1205 1206 1207

	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = &pdev->resource[i + PCI_IOV_RESOURCES];
		if (!res->flags || !res->parent)
			continue;

		if (!pnv_pci_is_mem_pref_64(res->flags))
			continue;

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
		for (j = 0; j < vf_groups; j++) {
			do {
				win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
						phb->ioda.m64_bar_idx + 1, 0);

				if (win >= phb->ioda.m64_bar_idx + 1)
					goto m64_failed;
			} while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));

			pdn->m64_wins[i][j] = win;

			if (pdn->m64_per_iov == M64_PER_IOV) {
				size = pci_iov_resource_size(pdev,
							PCI_IOV_RESOURCES + i);
				size = size * vf_per_group;
				start = res->start + size * j;
			} else {
				size = resource_size(res);
				start = res->start;
			}

			/* Map the M64 here */
			if (pdn->m64_per_iov == M64_PER_IOV) {
				pe_num = pdn->offset + j;
				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
						pe_num, OPAL_M64_WINDOW_TYPE,
						pdn->m64_wins[i][j], 0);
			}

			rc = opal_pci_set_phb_mem_window(phb->opal_id,
						 OPAL_M64_WINDOW_TYPE,
						 pdn->m64_wins[i][j],
						 start,
						 0, /* unused */
						 size);
1243 1244


1245 1246 1247 1248 1249
			if (rc != OPAL_SUCCESS) {
				dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
					win, rc);
				goto m64_failed;
			}
1250

1251 1252 1253 1254 1255 1256
			if (pdn->m64_per_iov == M64_PER_IOV)
				rc = opal_pci_phb_mmio_enable(phb->opal_id,
				     OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
			else
				rc = opal_pci_phb_mmio_enable(phb->opal_id,
				     OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1257

1258 1259 1260 1261 1262
			if (rc != OPAL_SUCCESS) {
				dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
					win, rc);
				goto m64_failed;
			}
1263 1264 1265 1266 1267 1268 1269 1270 1271
		}
	}
	return 0;

m64_failed:
	pnv_pci_vf_release_m64(pdev);
	return -EBUSY;
}

1272 1273 1274 1275
static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
		int num);
static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);

1276 1277 1278 1279 1280
static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
{
	struct iommu_table    *tbl;
	int64_t               rc;

1281
	tbl = pe->table_group.tables[0];
1282
	rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
1283 1284 1285
	if (rc)
		pe_warn(pe, "OPAL error %ld release DMA window\n", rc);

1286
	pnv_pci_ioda2_set_bypass(pe, false);
1287 1288 1289
	if (pe->table_group.group) {
		iommu_group_put(pe->table_group.group);
		BUG_ON(pe->table_group.group);
1290
	}
1291
	pnv_pci_ioda2_table_free_pages(tbl);
1292 1293 1294
	iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
}

1295
static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1296 1297 1298 1299 1300 1301
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pnv_ioda_pe    *pe, *pe_n;
	struct pci_dn         *pdn;
1302 1303
	u16                    vf_index;
	int64_t                rc;
1304 1305 1306 1307

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
1308
	pdn = pci_get_pdn(pdev);
1309 1310 1311 1312

	if (!pdev->is_physfn)
		return;

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
		int   vf_group;
		int   vf_per_group;
		int   vf_index1;

		vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;

		for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
			for (vf_index = vf_group * vf_per_group;
				vf_index < (vf_group + 1) * vf_per_group &&
				vf_index < num_vfs;
				vf_index++)
				for (vf_index1 = vf_group * vf_per_group;
					vf_index1 < (vf_group + 1) * vf_per_group &&
					vf_index1 < num_vfs;
					vf_index1++){

					rc = opal_pci_set_peltv(phb->opal_id,
						pdn->offset + vf_index,
						pdn->offset + vf_index1,
						OPAL_REMOVE_PE_FROM_DOMAIN);

					if (rc)
					    dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
						__func__,
						pdn->offset + vf_index1, rc);
				}
	}

1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
		if (pe->parent_dev != pdev)
			continue;

		pnv_pci_ioda2_release_dma_pe(pdev, pe);

		/* Remove from list */
		mutex_lock(&phb->ioda.pe_list_mutex);
		list_del(&pe->list);
		mutex_unlock(&phb->ioda.pe_list_mutex);

		pnv_ioda_deconfigure_pe(phb, pe);

		pnv_ioda_free_pe(phb, pe->pe_number);
	}
}

void pnv_pci_sriov_disable(struct pci_dev *pdev)
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pci_dn         *pdn;
	struct pci_sriov      *iov;
	u16 num_vfs;

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
	pdn = pci_get_pdn(pdev);
	iov = pdev->sriov;
	num_vfs = pdn->num_vfs;

	/* Release VF PEs */
1376
	pnv_ioda_release_vf_PE(pdev, num_vfs);
1377 1378

	if (phb->type == PNV_PHB_IODA2) {
1379 1380
		if (pdn->m64_per_iov == 1)
			pnv_pci_vf_resource_shift(pdev, -pdn->offset);
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

		/* Release M64 windows */
		pnv_pci_vf_release_m64(pdev);

		/* Release PE numbers */
		bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
		pdn->offset = 0;
	}
}

static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
				       struct pnv_ioda_pe *pe);
static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pnv_ioda_pe    *pe;
	int                    pe_num;
	u16                    vf_index;
	struct pci_dn         *pdn;
1402
	int64_t                rc;
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
	pdn = pci_get_pdn(pdev);

	if (!pdev->is_physfn)
		return;

	/* Reserve PE for each VF */
	for (vf_index = 0; vf_index < num_vfs; vf_index++) {
		pe_num = pdn->offset + vf_index;

		pe = &phb->ioda.pe_array[pe_num];
		pe->pe_number = pe_num;
		pe->phb = phb;
		pe->flags = PNV_IODA_PE_VF;
		pe->pbus = NULL;
		pe->parent_dev = pdev;
		pe->tce32_seg = -1;
		pe->mve_number = -1;
		pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
			   pci_iov_virtfn_devfn(pdev, vf_index);

		pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
			hose->global_number, pdev->bus->number,
			PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
			PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);

		if (pnv_ioda_configure_pe(phb, pe)) {
			/* XXX What do we do here ? */
			if (pe_num)
				pnv_ioda_free_pe(phb, pe_num);
			pe->pdev = NULL;
			continue;
		}

		/* Put PE to the list */
		mutex_lock(&phb->ioda.pe_list_mutex);
		list_add_tail(&pe->list, &phb->ioda.pe_list);
		mutex_unlock(&phb->ioda.pe_list_mutex);

		pnv_pci_ioda2_setup_dma_pe(phb, pe);
	}
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

	if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
		int   vf_group;
		int   vf_per_group;
		int   vf_index1;

		vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;

		for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
			for (vf_index = vf_group * vf_per_group;
			     vf_index < (vf_group + 1) * vf_per_group &&
			     vf_index < num_vfs;
			     vf_index++) {
				for (vf_index1 = vf_group * vf_per_group;
				     vf_index1 < (vf_group + 1) * vf_per_group &&
				     vf_index1 < num_vfs;
				     vf_index1++) {

					rc = opal_pci_set_peltv(phb->opal_id,
						pdn->offset + vf_index,
						pdn->offset + vf_index1,
						OPAL_ADD_PE_TO_DOMAIN);

					if (rc)
					    dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
						__func__,
						pdn->offset + vf_index1, rc);
				}
			}
		}
	}
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
}

int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
{
	struct pci_bus        *bus;
	struct pci_controller *hose;
	struct pnv_phb        *phb;
	struct pci_dn         *pdn;
	int                    ret;

	bus = pdev->bus;
	hose = pci_bus_to_host(bus);
	phb = hose->private_data;
	pdn = pci_get_pdn(pdev);

	if (phb->type == PNV_PHB_IODA2) {
		/* Calculate available PE for required VFs */
		mutex_lock(&phb->ioda.pe_alloc_mutex);
		pdn->offset = bitmap_find_next_zero_area(
			phb->ioda.pe_alloc, phb->ioda.total_pe,
			0, num_vfs, 0);
		if (pdn->offset >= phb->ioda.total_pe) {
			mutex_unlock(&phb->ioda.pe_alloc_mutex);
			dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
			pdn->offset = 0;
			return -EBUSY;
		}
		bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
		pdn->num_vfs = num_vfs;
		mutex_unlock(&phb->ioda.pe_alloc_mutex);

		/* Assign M64 window accordingly */
1510
		ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
		if (ret) {
			dev_info(&pdev->dev, "Not enough M64 window resources\n");
			goto m64_failed;
		}

		/*
		 * When using one M64 BAR to map one IOV BAR, we need to shift
		 * the IOV BAR according to the PE# allocated to the VFs.
		 * Otherwise, the PE# for the VF will conflict with others.
		 */
1521 1522 1523 1524 1525
		if (pdn->m64_per_iov == 1) {
			ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
			if (ret)
				goto m64_failed;
		}
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	}

	/* Setup VF PEs */
	pnv_ioda_setup_vf_PE(pdev, num_vfs);

	return 0;

m64_failed:
	bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
	pdn->offset = 0;

	return ret;
}

G
Gavin Shan 已提交
1540 1541
int pcibios_sriov_disable(struct pci_dev *pdev)
{
1542 1543
	pnv_pci_sriov_disable(pdev);

G
Gavin Shan 已提交
1544 1545 1546 1547 1548 1549 1550 1551 1552
	/* Release PCI data */
	remove_dev_pci_data(pdev);
	return 0;
}

int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
{
	/* Allocate PCI data */
	add_dev_pci_data(pdev);
1553 1554

	pnv_pci_sriov_enable(pdev, num_vfs);
G
Gavin Shan 已提交
1555 1556 1557 1558
	return 0;
}
#endif /* CONFIG_PCI_IOV */

1559
static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
1560
{
1561
	struct pci_dn *pdn = pci_get_pdn(pdev);
1562
	struct pnv_ioda_pe *pe;
1563

1564 1565 1566 1567 1568 1569 1570
	/*
	 * The function can be called while the PE#
	 * hasn't been assigned. Do nothing for the
	 * case.
	 */
	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
		return;
1571

1572
	pe = &phb->ioda.pe_array[pdn->pe_number];
1573
	WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
1574
	set_dma_offset(&pdev->dev, pe->tce_bypass_base);
1575
	set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
1576 1577 1578 1579 1580 1581
	/*
	 * Note: iommu_add_device() will fail here as
	 * for physical PE: the device is already added by now;
	 * for virtual PE: sysfs entries are not ready yet and
	 * tce_iommu_bus_notifier will add the device to a group later.
	 */
1582 1583
}

1584
static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
1585
{
1586 1587
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
1588 1589 1590 1591
	struct pci_dn *pdn = pci_get_pdn(pdev);
	struct pnv_ioda_pe *pe;
	uint64_t top;
	bool bypass = false;
1592 1593
	struct pci_dev *linked_npu_dev;
	int i;
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610

	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
		return -ENODEV;;

	pe = &phb->ioda.pe_array[pdn->pe_number];
	if (pe->tce_bypass_enabled) {
		top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
		bypass = (dma_mask >= top);
	}

	if (bypass) {
		dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
		set_dma_ops(&pdev->dev, &dma_direct_ops);
	} else {
		dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
		set_dma_ops(&pdev->dev, &dma_iommu_ops);
	}
1611
	*pdev->dev.dma_mask = dma_mask;
1612 1613 1614 1615 1616 1617 1618 1619 1620

	/* Update peer npu devices */
	if (pe->flags & PNV_IODA_PE_PEER)
		for (i = 0; pe->peers[i]; i++) {
			linked_npu_dev = pe->peers[i]->pdev;
			if (dma_get_mask(&linked_npu_dev->dev) != dma_mask)
				dma_set_mask(&linked_npu_dev->dev, dma_mask);
		}

1621 1622 1623
	return 0;
}

1624
static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
1625
{
1626 1627
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	struct pci_dn *pdn = pci_get_pdn(pdev);
	struct pnv_ioda_pe *pe;
	u64 end, mask;

	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
		return 0;

	pe = &phb->ioda.pe_array[pdn->pe_number];
	if (!pe->tce_bypass_enabled)
		return __dma_get_required_mask(&pdev->dev);


	end = pe->tce_bypass_base + memblock_end_of_DRAM();
	mask = 1ULL << (fls64(end) - 1);
	mask += mask - 1;

	return mask;
}

1647
static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1648
				   struct pci_bus *bus)
1649 1650 1651 1652
{
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
1653
		set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
1654
		set_dma_offset(&dev->dev, pe->tce_bypass_base);
1655
		iommu_add_device(&dev->dev);
1656

1657
		if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
1658
			pnv_ioda_setup_bus_dma(pe, dev->subordinate);
1659 1660 1661
	}
}

1662 1663
static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
		unsigned long index, unsigned long npages, bool rm)
1664
{
1665 1666 1667 1668
	struct iommu_table_group_link *tgl = list_first_entry_or_null(
			&tbl->it_group_list, struct iommu_table_group_link,
			next);
	struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1669
			struct pnv_ioda_pe, table_group);
1670
	__be64 __iomem *invalidate = rm ?
1671 1672
		(__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
		pe->phb->ioda.tce_inval_reg;
1673
	unsigned long start, end, inc;
1674
	const unsigned shift = tbl->it_page_shift;
1675

1676 1677 1678
	start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
	end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
			npages - 1);
1679 1680 1681

	/* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
	if (tbl->it_busno) {
1682 1683 1684
		start <<= shift;
		end <<= shift;
		inc = 128ull << shift;
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
		start |= tbl->it_busno;
		end |= tbl->it_busno;
	} else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
		/* p7ioc-style invalidation, 2 TCEs per write */
		start |= (1ull << 63);
		end |= (1ull << 63);
		inc = 16;
        } else {
		/* Default (older HW) */
                inc = 128;
	}

        end |= inc - 1;	/* round up end to be different than start */

        mb(); /* Ensure above stores are visible */
        while (start <= end) {
1701
		if (rm)
1702
			__raw_rm_writeq(cpu_to_be64(start), invalidate);
1703
		else
1704
			__raw_writeq(cpu_to_be64(start), invalidate);
1705 1706 1707 1708 1709 1710 1711 1712 1713
                start += inc;
        }

	/*
	 * The iommu layer will do another mb() for us on build()
	 * and we don't care on free()
	 */
}

1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
		long npages, unsigned long uaddr,
		enum dma_data_direction direction,
		struct dma_attrs *attrs)
{
	int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
			attrs);

	if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
		pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);

	return ret;
}

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
#ifdef CONFIG_IOMMU_API
static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
		unsigned long *hpa, enum dma_data_direction *direction)
{
	long ret = pnv_tce_xchg(tbl, index, hpa, direction);

	if (!ret && (tbl->it_type &
			(TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
		pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);

	return ret;
}
#endif

1742 1743 1744 1745 1746 1747 1748 1749 1750
static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
		long npages)
{
	pnv_tce_free(tbl, index, npages);

	if (tbl->it_type & TCE_PCI_SWINV_FREE)
		pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
}

1751
static struct iommu_table_ops pnv_ioda1_iommu_ops = {
1752
	.set = pnv_ioda1_tce_build,
1753 1754 1755
#ifdef CONFIG_IOMMU_API
	.exchange = pnv_ioda1_tce_xchg,
#endif
1756
	.clear = pnv_ioda1_tce_free,
1757 1758 1759
	.get = pnv_tce_get,
};

1760 1761 1762 1763 1764
static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
{
	/* 01xb - invalidate TCEs that match the specified PE# */
	unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
	struct pnv_phb *phb = pe->phb;
1765 1766
	struct pnv_ioda_pe *npe;
	int i;
1767 1768 1769 1770 1771 1772

	if (!phb->ioda.tce_inval_reg)
		return;

	mb(); /* Ensure above stores are visible */
	__raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1773 1774 1775 1776 1777 1778 1779 1780 1781

	if (pe->flags & PNV_IODA_PE_PEER)
		for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
			npe = pe->peers[i];
			if (!npe || npe->phb->type != PNV_PHB_NPU)
				continue;

			pnv_npu_tce_invalidate_entire(npe);
		}
1782 1783
}

1784 1785 1786
static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
		__be64 __iomem *invalidate, unsigned shift,
		unsigned long index, unsigned long npages)
1787 1788 1789 1790
{
	unsigned long start, end, inc;

	/* We'll invalidate DMA address in PE scope */
1791
	start = 0x2ull << 60;
1792
	start |= (pe_number & 0xFF);
1793 1794 1795
	end = start;

	/* Figure out the start, end and step */
1796 1797
	start |= (index << shift);
	end |= ((index + npages - 1) << shift);
1798
	inc = (0x1ull << shift);
1799 1800 1801
	mb();

	while (start <= end) {
1802
		if (rm)
1803
			__raw_rm_writeq(cpu_to_be64(start), invalidate);
1804
		else
1805
			__raw_writeq(cpu_to_be64(start), invalidate);
1806 1807 1808 1809
		start += inc;
	}
}

1810 1811 1812 1813 1814 1815
static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
		unsigned long index, unsigned long npages, bool rm)
{
	struct iommu_table_group_link *tgl;

	list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1816
		struct pnv_ioda_pe *npe;
1817 1818 1819 1820 1821
		struct pnv_ioda_pe *pe = container_of(tgl->table_group,
				struct pnv_ioda_pe, table_group);
		__be64 __iomem *invalidate = rm ?
			(__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
			pe->phb->ioda.tce_inval_reg;
1822
		int i;
1823 1824 1825 1826

		pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
			invalidate, tbl->it_page_shift,
			index, npages);
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837

		if (pe->flags & PNV_IODA_PE_PEER)
			/* Invalidate PEs using the same TCE table */
			for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
				npe = pe->peers[i];
				if (!npe || npe->phb->type != PNV_PHB_NPU)
					continue;

				pnv_npu_tce_invalidate(npe, tbl, index,
							npages, rm);
			}
1838 1839 1840
	}
}

1841 1842 1843 1844
static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
		long npages, unsigned long uaddr,
		enum dma_data_direction direction,
		struct dma_attrs *attrs)
1845
{
1846 1847
	int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
			attrs);
1848

1849 1850 1851 1852 1853 1854
	if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
		pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);

	return ret;
}

1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
#ifdef CONFIG_IOMMU_API
static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
		unsigned long *hpa, enum dma_data_direction *direction)
{
	long ret = pnv_tce_xchg(tbl, index, hpa, direction);

	if (!ret && (tbl->it_type &
			(TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
		pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);

	return ret;
}
#endif

1869 1870 1871 1872 1873 1874 1875
static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
		long npages)
{
	pnv_tce_free(tbl, index, npages);

	if (tbl->it_type & TCE_PCI_SWINV_FREE)
		pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1876 1877
}

1878 1879 1880 1881 1882 1883
static void pnv_ioda2_table_free(struct iommu_table *tbl)
{
	pnv_pci_ioda2_table_free_pages(tbl);
	iommu_free_table(tbl, "pnv");
}

1884
static struct iommu_table_ops pnv_ioda2_iommu_ops = {
1885
	.set = pnv_ioda2_tce_build,
1886 1887 1888
#ifdef CONFIG_IOMMU_API
	.exchange = pnv_ioda2_tce_xchg,
#endif
1889
	.clear = pnv_ioda2_tce_free,
1890
	.get = pnv_tce_get,
1891
	.free = pnv_ioda2_table_free,
1892 1893
};

1894 1895 1896
static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
				      struct pnv_ioda_pe *pe, unsigned int base,
				      unsigned int segs)
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
{

	struct page *tce_mem = NULL;
	struct iommu_table *tbl;
	unsigned int i;
	int64_t rc;
	void *addr;

	/* XXX FIXME: Handle 64-bit only DMA devices */
	/* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
	/* XXX FIXME: Allocate multi-level tables on PHB3 */

	/* We shouldn't already have a 32-bit DMA associated */
	if (WARN_ON(pe->tce32_seg >= 0))
		return;

1913
	tbl = pnv_pci_table_alloc(phb->hose->node);
1914 1915
	iommu_register_group(&pe->table_group, phb->hose->global_number,
			pe->pe_number);
1916
	pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
1917

1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
	/* Grab a 32-bit TCE table */
	pe->tce32_seg = base;
	pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
		(base << 28), ((base + segs) << 28) - 1);

	/* XXX Currently, we allocate one big contiguous table for the
	 * TCEs. We only really need one chunk per 256M of TCE space
	 * (ie per segment) but that's an optimization for later, it
	 * requires some added smarts with our get/put_tce implementation
	 */
	tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
				   get_order(TCE32_TABLE_SIZE * segs));
	if (!tce_mem) {
		pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
		goto fail;
	}
	addr = page_address(tce_mem);
	memset(addr, 0, TCE32_TABLE_SIZE * segs);

	/* Configure HW */
	for (i = 0; i < segs; i++) {
		rc = opal_pci_map_pe_dma_window(phb->opal_id,
					      pe->pe_number,
					      base + i, 1,
					      __pa(addr) + TCE32_TABLE_SIZE * i,
					      TCE32_TABLE_SIZE, 0x1000);
		if (rc) {
			pe_err(pe, " Failed to configure 32-bit TCE table,"
			       " err %ld\n", rc);
			goto fail;
		}
	}

	/* Setup linux iommu table */
	pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
1953
				  base << 28, IOMMU_PAGE_SHIFT_4K);
1954 1955

	/* OPAL variant of P7IOC SW invalidated TCEs */
1956
	if (phb->ioda.tce_inval_reg)
1957 1958 1959
		tbl->it_type |= (TCE_PCI_SWINV_CREATE |
				 TCE_PCI_SWINV_FREE   |
				 TCE_PCI_SWINV_PAIR);
1960

1961
	tbl->it_ops = &pnv_ioda1_iommu_ops;
1962 1963
	pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
	pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
1964 1965
	iommu_init_table(tbl, phb->hose->node);

1966
	if (pe->flags & PNV_IODA_PE_DEV) {
1967 1968 1969 1970 1971 1972 1973
		/*
		 * Setting table base here only for carrying iommu_group
		 * further down to let iommu_add_device() do the job.
		 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
		 */
		set_iommu_table_base(&pe->pdev->dev, tbl);
		iommu_add_device(&pe->pdev->dev);
1974
	} else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
1975
		pnv_ioda_setup_bus_dma(pe, pe->pbus);
1976

1977 1978 1979 1980 1981 1982 1983
	return;
 fail:
	/* XXX Failure: Try to fallback to 64-bit only ? */
	if (pe->tce32_seg >= 0)
		pe->tce32_seg = -1;
	if (tce_mem)
		__free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1984 1985 1986 1987
	if (tbl) {
		pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
		iommu_free_table(tbl, "pnv");
	}
1988 1989
}

1990 1991 1992 1993 1994 1995 1996
static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
		int num, struct iommu_table *tbl)
{
	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
			table_group);
	struct pnv_phb *phb = pe->phb;
	int64_t rc;
1997 1998
	const unsigned long size = tbl->it_indirect_levels ?
			tbl->it_level_size : tbl->it_size;
1999 2000 2001
	const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
	const __u64 win_size = tbl->it_size << tbl->it_page_shift;

2002
	pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
2003 2004 2005 2006 2007 2008 2009 2010 2011
			start_addr, start_addr + win_size - 1,
			IOMMU_PAGE_SIZE(tbl));

	/*
	 * Map TCE table through TVT. The TVE index is the PE number
	 * shifted by 1 bit for 32-bits DMA space.
	 */
	rc = opal_pci_map_pe_dma_window(phb->opal_id,
			pe->pe_number,
2012
			(pe->pe_number << 1) + num,
2013
			tbl->it_indirect_levels + 1,
2014
			__pa(tbl->it_base),
2015
			size << 3,
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
			IOMMU_PAGE_SIZE(tbl));
	if (rc) {
		pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
		return rc;
	}

	pnv_pci_link_table_and_group(phb->hose->node, num,
			tbl, &pe->table_group);
	pnv_pci_ioda2_tce_invalidate_entire(pe);

	return 0;
}

2029
static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
{
	uint16_t window_id = (pe->pe_number << 1 ) + 1;
	int64_t rc;

	pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
	if (enable) {
		phys_addr_t top = memblock_end_of_DRAM();

		top = roundup_pow_of_two(top);
		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
						     pe->pe_number,
						     window_id,
						     pe->tce_bypass_base,
						     top);
	} else {
		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
						     pe->pe_number,
						     window_id,
						     pe->tce_bypass_base,
						     0);
	}
	if (rc)
		pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
	else
		pe->tce_bypass_enabled = enable;
}

2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
		__u32 page_shift, __u64 window_size, __u32 levels,
		struct iommu_table *tbl);

static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
		int num, __u32 page_shift, __u64 window_size, __u32 levels,
		struct iommu_table **ptbl)
{
	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
			table_group);
	int nid = pe->phb->hose->node;
	__u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
	long ret;
	struct iommu_table *tbl;

	tbl = pnv_pci_table_alloc(nid);
	if (!tbl)
		return -ENOMEM;

	ret = pnv_pci_ioda2_table_alloc_pages(nid,
			bus_offset, page_shift, window_size,
			levels, tbl);
	if (ret) {
		iommu_free_table(tbl, "pnv");
		return ret;
	}

	tbl->it_ops = &pnv_ioda2_iommu_ops;
	if (pe->phb->ioda.tce_inval_reg)
		tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);

	*ptbl = tbl;

	return 0;
}

2093 2094 2095 2096 2097
static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
{
	struct iommu_table *tbl = NULL;
	long rc;

2098 2099 2100 2101 2102 2103 2104
	/*
	 * crashkernel= specifies the kdump kernel's maximum memory at
	 * some offset and there is no guaranteed the result is a power
	 * of 2, which will cause errors later.
	 */
	const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());

2105 2106 2107 2108 2109
	/*
	 * In memory constrained environments, e.g. kdump kernel, the
	 * DMA window can be larger than available memory, which will
	 * cause errors later.
	 */
2110
	const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
2111

2112 2113
	rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
			IOMMU_PAGE_SHIFT_4K,
2114
			window_size,
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
			POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
	if (rc) {
		pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
				rc);
		return rc;
	}

	iommu_init_table(tbl, pe->phb->hose->node);

	rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
	if (rc) {
		pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
				rc);
		pnv_ioda2_table_free(tbl);
		return rc;
	}

	if (!pnv_iommu_bypass_disabled)
		pnv_pci_ioda2_set_bypass(pe, true);

	/* OPAL variant of PHB3 invalidated TCEs */
	if (pe->phb->ioda.tce_inval_reg)
		tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);

	/*
	 * Setting table base here only for carrying iommu_group
	 * further down to let iommu_add_device() do the job.
	 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
	 */
	if (pe->flags & PNV_IODA_PE_DEV)
		set_iommu_table_base(&pe->pdev->dev, tbl);

	return 0;
}

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
		int num)
{
	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
			table_group);
	struct pnv_phb *phb = pe->phb;
	long ret;

	pe_info(pe, "Removing DMA window #%d\n", num);

	ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
			(pe->pe_number << 1) + num,
			0/* levels */, 0/* table address */,
			0/* table size */, 0/* page size */);
	if (ret)
		pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
	else
		pnv_pci_ioda2_tce_invalidate_entire(pe);

	pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);

	return ret;
}
#endif

2176
#ifdef CONFIG_IOMMU_API
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
		__u64 window_size, __u32 levels)
{
	unsigned long bytes = 0;
	const unsigned window_shift = ilog2(window_size);
	unsigned entries_shift = window_shift - page_shift;
	unsigned table_shift = entries_shift + 3;
	unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
	unsigned long direct_table_size;

	if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
			(window_size > memory_hotplug_max()) ||
			!is_power_of_2(window_size))
		return 0;

	/* Calculate a direct table size from window_size and levels */
	entries_shift = (entries_shift + levels - 1) / levels;
	table_shift = entries_shift + 3;
	table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
	direct_table_size =  1UL << table_shift;

	for ( ; levels; --levels) {
		bytes += _ALIGN_UP(tce_table_size, direct_table_size);

		tce_table_size /= direct_table_size;
		tce_table_size <<= 3;
		tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
	}

	return bytes;
}

2209
static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
2210
{
2211 2212
	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
						table_group);
2213 2214
	/* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
	struct iommu_table *tbl = pe->table_group.tables[0];
2215

2216
	pnv_pci_ioda2_set_bypass(pe, false);
2217 2218
	pnv_pci_ioda2_unset_window(&pe->table_group, 0);
	pnv_ioda2_table_free(tbl);
2219
}
2220

2221 2222 2223 2224 2225
static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
{
	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
						table_group);

2226
	pnv_pci_ioda2_setup_default_config(pe);
2227 2228
}

2229
static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
2230
	.get_table_size = pnv_pci_ioda2_get_table_size,
2231 2232 2233
	.create_table = pnv_pci_ioda2_create_table,
	.set_window = pnv_pci_ioda2_set_window,
	.unset_window = pnv_pci_ioda2_unset_window,
2234 2235 2236 2237 2238
	.take_ownership = pnv_ioda2_take_ownership,
	.release_ownership = pnv_ioda2_release_ownership,
};
#endif

2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
{
	const __be64 *swinvp;

	/* OPAL variant of PHB3 invalidated TCEs */
	swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
	if (!swinvp)
		return;

	phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
	phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
}

2252 2253
static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
		unsigned levels, unsigned long limit,
2254
		unsigned long *current_offset, unsigned long *total_allocated)
2255 2256
{
	struct page *tce_mem = NULL;
2257
	__be64 *addr, *tmp;
2258
	unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
2259 2260 2261
	unsigned long allocated = 1UL << (order + PAGE_SHIFT);
	unsigned entries = 1UL << (shift - 3);
	long i;
2262 2263 2264 2265 2266 2267 2268

	tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
	if (!tce_mem) {
		pr_err("Failed to allocate a TCE memory, order=%d\n", order);
		return NULL;
	}
	addr = page_address(tce_mem);
2269
	memset(addr, 0, allocated);
2270
	*total_allocated += allocated;
2271 2272 2273 2274 2275 2276 2277 2278 2279

	--levels;
	if (!levels) {
		*current_offset += allocated;
		return addr;
	}

	for (i = 0; i < entries; ++i) {
		tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
2280
				levels, limit, current_offset, total_allocated);
2281 2282 2283 2284 2285 2286 2287 2288 2289
		if (!tmp)
			break;

		addr[i] = cpu_to_be64(__pa(tmp) |
				TCE_PCI_READ | TCE_PCI_WRITE);

		if (*current_offset >= limit)
			break;
	}
2290 2291 2292 2293

	return addr;
}

2294 2295 2296
static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
		unsigned long size, unsigned level);

2297
static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2298 2299
		__u32 page_shift, __u64 window_size, __u32 levels,
		struct iommu_table *tbl)
2300
{
2301
	void *addr;
2302
	unsigned long offset = 0, level_shift, total_allocated = 0;
2303 2304 2305 2306 2307
	const unsigned window_shift = ilog2(window_size);
	unsigned entries_shift = window_shift - page_shift;
	unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
	const unsigned long tce_table_size = 1UL << table_shift;

2308 2309 2310
	if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
		return -EINVAL;

2311 2312 2313
	if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
		return -EINVAL;

2314 2315 2316 2317 2318
	/* Adjust direct table size from window_size and levels */
	entries_shift = (entries_shift + levels - 1) / levels;
	level_shift = entries_shift + 3;
	level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);

2319
	/* Allocate TCE table */
2320
	addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
2321
			levels, tce_table_size, &offset, &total_allocated);
2322 2323

	/* addr==NULL means that the first level allocation failed */
2324 2325 2326
	if (!addr)
		return -ENOMEM;

2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
	/*
	 * First level was allocated but some lower level failed as
	 * we did not allocate as much as we wanted,
	 * release partially allocated table.
	 */
	if (offset < tce_table_size) {
		pnv_pci_ioda2_table_do_free_pages(addr,
				1ULL << (level_shift - 3), levels - 1);
		return -ENOMEM;
	}

2338 2339 2340
	/* Setup linux iommu table */
	pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
			page_shift);
2341 2342
	tbl->it_level_size = 1ULL << (level_shift - 3);
	tbl->it_indirect_levels = levels - 1;
2343
	tbl->it_allocated_size = total_allocated;
2344 2345 2346 2347 2348 2349 2350

	pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
			window_size, tce_table_size, bus_offset);

	return 0;
}

2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
		unsigned long size, unsigned level)
{
	const unsigned long addr_ul = (unsigned long) addr &
			~(TCE_PCI_READ | TCE_PCI_WRITE);

	if (level) {
		long i;
		u64 *tmp = (u64 *) addr_ul;

		for (i = 0; i < size; ++i) {
			unsigned long hpa = be64_to_cpu(tmp[i]);

			if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
				continue;

			pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
					level - 1);
		}
	}

	free_pages(addr_ul, get_order(size << 3));
}

2375 2376
static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
{
2377 2378 2379
	const unsigned long size = tbl->it_indirect_levels ?
			tbl->it_level_size : tbl->it_size;

2380 2381 2382
	if (!tbl->it_size)
		return;

2383 2384
	pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
			tbl->it_indirect_levels);
2385 2386 2387 2388 2389
}

static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
				       struct pnv_ioda_pe *pe)
{
2390 2391 2392 2393 2394 2395
	int64_t rc;

	/* We shouldn't already have a 32-bit DMA associated */
	if (WARN_ON(pe->tce32_seg >= 0))
		return;

2396 2397 2398
	/* TVE #1 is selected by PCI address bit 59 */
	pe->tce_bypass_base = 1ull << 59;

2399 2400
	iommu_register_group(&pe->table_group, phb->hose->global_number,
			pe->pe_number);
2401

2402 2403 2404
	/* The PE will reserve all possible 32-bits space */
	pe->tce32_seg = 0;
	pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
2405
		phb->ioda.m32_pci_base);
2406

2407
	/* Setup linux iommu table */
2408 2409 2410 2411 2412 2413
	pe->table_group.tce32_start = 0;
	pe->table_group.tce32_size = phb->ioda.m32_pci_base;
	pe->table_group.max_dynamic_windows_supported =
			IOMMU_TABLE_GROUP_MAX_TABLES;
	pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
	pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
2414 2415 2416 2417
#ifdef CONFIG_IOMMU_API
	pe->table_group.ops = &pnv_pci_ioda2_ops;
#endif

2418
	rc = pnv_pci_ioda2_setup_default_config(pe);
2419
	if (rc) {
2420 2421 2422
		if (pe->tce32_seg >= 0)
			pe->tce32_seg = -1;
		return;
2423 2424
	}

2425
	if (pe->flags & PNV_IODA_PE_DEV)
2426
		iommu_add_device(&pe->pdev->dev);
2427
	else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
2428
		pnv_ioda_setup_bus_dma(pe, pe->pbus);
2429 2430
}

2431
static void pnv_ioda_setup_dma(struct pnv_phb *phb)
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
{
	struct pci_controller *hose = phb->hose;
	unsigned int residual, remaining, segs, tw, base;
	struct pnv_ioda_pe *pe;

	/* If we have more PE# than segments available, hand out one
	 * per PE until we run out and let the rest fail. If not,
	 * then we assign at least one segment per PE, plus more based
	 * on the amount of devices under that PE
	 */
	if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
		residual = 0;
	else
		residual = phb->ioda.tce32_count -
			phb->ioda.dma_pe_count;

	pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
		hose->global_number, phb->ioda.tce32_count);
	pr_info("PCI: %d PE# for a total weight of %d\n",
		phb->ioda.dma_pe_count, phb->ioda.dma_weight);

2453 2454
	pnv_pci_ioda_setup_opal_tce_kill(phb);

2455 2456 2457 2458 2459 2460 2461
	/* Walk our PE list and configure their DMA segments, hand them
	 * out one base segment plus any residual segments based on
	 * weight
	 */
	remaining = phb->ioda.tce32_count;
	tw = phb->ioda.dma_weight;
	base = 0;
2462
	list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
		if (!pe->dma_weight)
			continue;
		if (!remaining) {
			pe_warn(pe, "No DMA32 resources available\n");
			continue;
		}
		segs = 1;
		if (residual) {
			segs += ((pe->dma_weight * residual)  + (tw / 2)) / tw;
			if (segs > remaining)
				segs = remaining;
		}
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484

		/*
		 * For IODA2 compliant PHB3, we needn't care about the weight.
		 * The all available 32-bits DMA space will be assigned to
		 * the specific PE.
		 */
		if (phb->type == PNV_PHB_IODA1) {
			pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
				pe->dma_weight, segs);
			pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2485
		} else if (phb->type == PNV_PHB_IODA2) {
2486 2487 2488
			pe_info(pe, "Assign DMA32 space\n");
			segs = 0;
			pnv_pci_ioda2_setup_dma_pe(phb, pe);
2489 2490 2491 2492 2493 2494 2495
		} else if (phb->type == PNV_PHB_NPU) {
			/*
			 * We initialise the DMA space for an NPU PHB
			 * after setup of the PHB is complete as we
			 * point the NPU TVT to the the same location
			 * as the PHB3 TVT.
			 */
2496 2497
		}

2498 2499 2500 2501 2502 2503
		remaining -= segs;
		base += segs;
	}
}

#ifdef CONFIG_PCI_MSI
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
static void pnv_ioda2_msi_eoi(struct irq_data *d)
{
	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
	struct irq_chip *chip = irq_data_get_irq_chip(d);
	struct pnv_phb *phb = container_of(chip, struct pnv_phb,
					   ioda.irq_chip);
	int64_t rc;

	rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
	WARN_ON_ONCE(rc);

	icp_native_eoi(d);
}

2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540

static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
{
	struct irq_data *idata;
	struct irq_chip *ichip;

	if (phb->type != PNV_PHB_IODA2)
		return;

	if (!phb->ioda.irq_chip_init) {
		/*
		 * First time we setup an MSI IRQ, we need to setup the
		 * corresponding IRQ chip to route correctly.
		 */
		idata = irq_get_irq_data(virq);
		ichip = irq_data_get_irq_chip(idata);
		phb->ioda.irq_chip_init = 1;
		phb->ioda.irq_chip = *ichip;
		phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
	}
	irq_set_chip(virq, &phb->ioda.irq_chip);
}

2541 2542
#ifdef CONFIG_CXL_BASE

2543
struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
2544 2545 2546
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);

2547
	return of_node_get(hose->dn);
2548
}
2549
EXPORT_SYMBOL(pnv_pci_get_phb_node);
2550

R
Ryan Grimm 已提交
2551
int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	struct pnv_ioda_pe *pe;
	int rc;

	pe = pnv_ioda_get_pe(dev);
	if (!pe)
		return -ENODEV;

	pe_info(pe, "Switching PHB to CXL\n");

R
Ryan Grimm 已提交
2564
	rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
2565 2566 2567 2568 2569
	if (rc)
		dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);

	return rc;
}
R
Ryan Grimm 已提交
2570
EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691

/* Find PHB for cxl dev and allocate MSI hwirqs?
 * Returns the absolute hardware IRQ number
 */
int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);

	if (hwirq < 0) {
		dev_warn(&dev->dev, "Failed to find a free MSI\n");
		return -ENOSPC;
	}

	return phb->msi_base + hwirq;
}
EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);

void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;

	msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
}
EXPORT_SYMBOL(pnv_cxl_release_hwirqs);

void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
				  struct pci_dev *dev)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	int i, hwirq;

	for (i = 1; i < CXL_IRQ_RANGES; i++) {
		if (!irqs->range[i])
			continue;
		pr_devel("cxl release irq range 0x%x: offset: 0x%lx  limit: %ld\n",
			 i, irqs->offset[i],
			 irqs->range[i]);
		hwirq = irqs->offset[i] - phb->msi_base;
		msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
				       irqs->range[i]);
	}
}
EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);

int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
			       struct pci_dev *dev, int num)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	int i, hwirq, try;

	memset(irqs, 0, sizeof(struct cxl_irq_ranges));

	/* 0 is reserved for the multiplexed PSL DSI interrupt */
	for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
		try = num;
		while (try) {
			hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
			if (hwirq >= 0)
				break;
			try /= 2;
		}
		if (!try)
			goto fail;

		irqs->offset[i] = phb->msi_base + hwirq;
		irqs->range[i] = try;
		pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx  limit: %li\n",
			 i, irqs->offset[i], irqs->range[i]);
		num -= try;
	}
	if (num)
		goto fail;

	return 0;
fail:
	pnv_cxl_release_hwirq_ranges(irqs, dev);
	return -ENOSPC;
}
EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);

int pnv_cxl_get_irq_count(struct pci_dev *dev)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;

	return phb->msi_bmp.irq_count;
}
EXPORT_SYMBOL(pnv_cxl_get_irq_count);

int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
			   unsigned int virq)
{
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	unsigned int xive_num = hwirq - phb->msi_base;
	struct pnv_ioda_pe *pe;
	int rc;

	if (!(pe = pnv_ioda_get_pe(dev)))
		return -ENODEV;

	/* Assign XIVE to PE */
	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
	if (rc) {
		pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
			"hwirq 0x%x XIVE 0x%x PE\n",
			pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
		return -EIO;
	}
	set_msi_irq_chip(phb, virq);

	return 0;
}
EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
#endif

2692
static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
2693 2694
				  unsigned int hwirq, unsigned int virq,
				  unsigned int is_64, struct msi_msg *msg)
2695 2696 2697
{
	struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
	unsigned int xive_num = hwirq - phb->msi_base;
2698
	__be32 data;
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
	int rc;

	/* No PE assigned ? bail out ... no MSI for you ! */
	if (pe == NULL)
		return -ENXIO;

	/* Check if we have an MVE */
	if (pe->mve_number < 0)
		return -ENXIO;

2709
	/* Force 32-bit MSI on some broken devices */
2710
	if (dev->no_64bit_msi)
2711 2712
		is_64 = 0;

2713 2714 2715 2716 2717 2718 2719 2720 2721
	/* Assign XIVE to PE */
	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
	if (rc) {
		pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
			pci_name(dev), rc, xive_num);
		return -EIO;
	}

	if (is_64) {
2722 2723
		__be64 addr64;

2724 2725 2726 2727 2728 2729 2730
		rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
				     &addr64, &data);
		if (rc) {
			pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
				pci_name(dev), rc);
			return -EIO;
		}
2731 2732
		msg->address_hi = be64_to_cpu(addr64) >> 32;
		msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
2733
	} else {
2734 2735
		__be32 addr32;

2736 2737 2738 2739 2740 2741 2742 2743
		rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
				     &addr32, &data);
		if (rc) {
			pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
				pci_name(dev), rc);
			return -EIO;
		}
		msg->address_hi = 0;
2744
		msg->address_lo = be32_to_cpu(addr32);
2745
	}
2746
	msg->data = be32_to_cpu(data);
2747

2748
	set_msi_irq_chip(phb, virq);
2749

2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
	pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
		 " address=%x_%08x data=%x PE# %d\n",
		 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
		 msg->address_hi, msg->address_lo, data, pe->pe_number);

	return 0;
}

static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
{
2760
	unsigned int count;
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
	const __be32 *prop = of_get_property(phb->hose->dn,
					     "ibm,opal-msi-ranges", NULL);
	if (!prop) {
		/* BML Fallback */
		prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
	}
	if (!prop)
		return;

	phb->msi_base = be32_to_cpup(prop);
2771 2772
	count = be32_to_cpup(prop + 1);
	if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
2773 2774 2775 2776
		pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
		       phb->hose->global_number);
		return;
	}
2777

2778 2779 2780
	phb->msi_setup = pnv_pci_ioda_msi_setup;
	phb->msi32_support = 1;
	pr_info("  Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
2781
		count, phb->msi_base);
2782 2783 2784 2785 2786
}
#else
static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
#endif /* CONFIG_PCI_MSI */

2787 2788 2789 2790 2791 2792 2793 2794 2795
#ifdef CONFIG_PCI_IOV
static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
{
	struct pci_controller *hose;
	struct pnv_phb *phb;
	struct resource *res;
	int i;
	resource_size_t size;
	struct pci_dn *pdn;
2796
	int mul, total_vfs;
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806

	if (!pdev->is_physfn || pdev->is_added)
		return;

	hose = pci_bus_to_host(pdev->bus);
	phb = hose->private_data;

	pdn = pci_get_pdn(pdev);
	pdn->vfs_expanded = 0;

2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
	total_vfs = pci_sriov_get_totalvfs(pdev);
	pdn->m64_per_iov = 1;
	mul = phb->ioda.total_pe;

	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = &pdev->resource[i + PCI_IOV_RESOURCES];
		if (!res->flags || res->parent)
			continue;
		if (!pnv_pci_is_mem_pref_64(res->flags)) {
			dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
				 i, res);
			continue;
		}

		size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);

		/* bigger than 64M */
		if (size > (1 << 26)) {
			dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
				 i, res);
			pdn->m64_per_iov = M64_PER_IOV;
			mul = roundup_pow_of_two(total_vfs);
			break;
		}
	}

2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
		res = &pdev->resource[i + PCI_IOV_RESOURCES];
		if (!res->flags || res->parent)
			continue;
		if (!pnv_pci_is_mem_pref_64(res->flags)) {
			dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
				 i, res);
			continue;
		}

		dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
		size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2845
		res->end = res->start + size * mul - 1;
2846 2847
		dev_dbg(&pdev->dev, "                       %pR\n", res);
		dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
2848
			 i, res, mul);
2849
	}
2850
	pdn->vfs_expanded = mul;
2851 2852 2853
}
#endif /* CONFIG_PCI_IOV */

2854 2855 2856 2857 2858
/*
 * This function is supposed to be called on basis of PE from top
 * to bottom style. So the the I/O or MMIO segment assigned to
 * parent PE could be overrided by its child PEs if necessary.
 */
2859 2860
static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
				  struct pnv_ioda_pe *pe)
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
{
	struct pnv_phb *phb = hose->private_data;
	struct pci_bus_region region;
	struct resource *res;
	int i, index;
	int rc;

	/*
	 * NOTE: We only care PCI bus based PE for now. For PCI
	 * device based PE, for example SRIOV sensitive VF should
	 * be figured out later.
	 */
	BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));

	pci_bus_for_each_resource(pe->pbus, res, i) {
		if (!res || !res->flags ||
		    res->start > res->end)
			continue;

		if (res->flags & IORESOURCE_IO) {
			region.start = res->start - phb->ioda.io_pci_base;
			region.end   = res->end - phb->ioda.io_pci_base;
			index = region.start / phb->ioda.io_segsize;

			while (index < phb->ioda.total_pe &&
			       region.start <= region.end) {
				phb->ioda.io_segmap[index] = pe->pe_number;
				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
					pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
				if (rc != OPAL_SUCCESS) {
					pr_err("%s: OPAL error %d when mapping IO "
					       "segment #%d to PE#%d\n",
					       __func__, rc, index, pe->pe_number);
					break;
				}

				region.start += phb->ioda.io_segsize;
				index++;
			}
2900 2901
		} else if ((res->flags & IORESOURCE_MEM) &&
			   !pnv_pci_is_mem_pref_64(res->flags)) {
2902
			region.start = res->start -
2903
				       hose->mem_offset[0] -
2904 2905
				       phb->ioda.m32_pci_base;
			region.end   = res->end -
2906
				       hose->mem_offset[0] -
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
				       phb->ioda.m32_pci_base;
			index = region.start / phb->ioda.m32_segsize;

			while (index < phb->ioda.total_pe &&
			       region.start <= region.end) {
				phb->ioda.m32_segmap[index] = pe->pe_number;
				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
					pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
				if (rc != OPAL_SUCCESS) {
					pr_err("%s: OPAL error %d when mapping M32 "
					       "segment#%d to PE#%d",
					       __func__, rc, index, pe->pe_number);
					break;
				}

				region.start += phb->ioda.m32_segsize;
				index++;
			}
		}
	}
}

2929
static void pnv_pci_ioda_setup_seg(void)
2930 2931 2932 2933 2934 2935 2936
{
	struct pci_controller *tmp, *hose;
	struct pnv_phb *phb;
	struct pnv_ioda_pe *pe;

	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		phb = hose->private_data;
2937 2938 2939 2940 2941

		/* NPU PHB does not support IO or MMIO segmentation */
		if (phb->type == PNV_PHB_NPU)
			continue;

2942 2943 2944 2945 2946 2947
		list_for_each_entry(pe, &phb->ioda.pe_list, list) {
			pnv_ioda_setup_pe_seg(hose, pe);
		}
	}
}

2948
static void pnv_pci_ioda_setup_DMA(void)
2949 2950
{
	struct pci_controller *hose, *tmp;
2951
	struct pnv_phb *phb;
2952 2953 2954

	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		pnv_ioda_setup_dma(hose->private_data);
2955 2956 2957 2958

		/* Mark the PHB initialization done */
		phb = hose->private_data;
		phb->initialized = 1;
2959 2960 2961
	}
}

2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
static void pnv_pci_ioda_create_dbgfs(void)
{
#ifdef CONFIG_DEBUG_FS
	struct pci_controller *hose, *tmp;
	struct pnv_phb *phb;
	char name[16];

	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		phb = hose->private_data;

		sprintf(name, "PCI%04x", hose->global_number);
		phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
		if (!phb->dbgfs)
			pr_warning("%s: Error on creating debugfs on PHB#%x\n",
				__func__, hose->global_number);
	}
#endif /* CONFIG_DEBUG_FS */
}

2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
static void pnv_npu_ioda_fixup(void)
{
	bool enable_bypass;
	struct pci_controller *hose, *tmp;
	struct pnv_phb *phb;
	struct pnv_ioda_pe *pe;

	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
		phb = hose->private_data;
		if (phb->type != PNV_PHB_NPU)
			continue;

		list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
			enable_bypass = dma_get_mask(&pe->pdev->dev) ==
				DMA_BIT_MASK(64);
			pnv_npu_init_dma_pe(pe);
			pnv_npu_dma_set_bypass(pe, enable_bypass);
		}
	}
}

3002
static void pnv_pci_ioda_fixup(void)
3003 3004
{
	pnv_pci_ioda_setup_PEs();
3005
	pnv_pci_ioda_setup_seg();
3006
	pnv_pci_ioda_setup_DMA();
3007

3008 3009
	pnv_pci_ioda_create_dbgfs();

3010 3011
#ifdef CONFIG_EEH
	eeh_init();
M
Mike Qiu 已提交
3012
	eeh_addr_cache_build();
3013
#endif
3014 3015 3016

	/* Link NPU IODA tables to their PCI devices. */
	pnv_npu_ioda_fixup();
3017 3018
}

3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
/*
 * Returns the alignment for I/O or memory windows for P2P
 * bridges. That actually depends on how PEs are segmented.
 * For now, we return I/O or M32 segment size for PE sensitive
 * P2P bridges. Otherwise, the default values (4KiB for I/O,
 * 1MiB for memory) will be returned.
 *
 * The current PCI bus might be put into one PE, which was
 * create against the parent PCI bridge. For that case, we
 * needn't enlarge the alignment so that we can save some
 * resources.
 */
static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
						unsigned long type)
{
	struct pci_dev *bridge;
	struct pci_controller *hose = pci_bus_to_host(bus);
	struct pnv_phb *phb = hose->private_data;
	int num_pci_bridges = 0;

	bridge = bus->self;
	while (bridge) {
		if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
			num_pci_bridges++;
			if (num_pci_bridges >= 2)
				return 1;
		}

		bridge = bridge->bus->self;
	}

3050 3051 3052 3053
	/* We fail back to M32 if M64 isn't supported */
	if (phb->ioda.m64_segsize &&
	    pnv_pci_is_mem_pref_64(type))
		return phb->ioda.m64_segsize;
3054 3055 3056 3057 3058 3059
	if (type & IORESOURCE_MEM)
		return phb->ioda.m32_segsize;

	return phb->ioda.io_segsize;
}

3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
#ifdef CONFIG_PCI_IOV
static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
						      int resno)
{
	struct pci_dn *pdn = pci_get_pdn(pdev);
	resource_size_t align, iov_align;

	iov_align = resource_size(&pdev->resource[resno]);
	if (iov_align)
		return iov_align;

	align = pci_iov_resource_size(pdev, resno);
	if (pdn->vfs_expanded)
		return pdn->vfs_expanded * align;

	return align;
}
#endif /* CONFIG_PCI_IOV */

3079 3080 3081
/* Prevent enabling devices for which we couldn't properly
 * assign a PE
 */
3082
static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
3083
{
3084 3085 3086
	struct pci_controller *hose = pci_bus_to_host(dev->bus);
	struct pnv_phb *phb = hose->private_data;
	struct pci_dn *pdn;
3087

3088 3089 3090 3091 3092 3093
	/* The function is probably called while the PEs have
	 * not be created yet. For example, resource reassignment
	 * during PCI probe period. We just skip the check if
	 * PEs isn't ready.
	 */
	if (!phb->initialized)
3094
		return true;
3095

3096
	pdn = pci_get_pdn(dev);
3097
	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
3098
		return false;
3099

3100
	return true;
3101 3102 3103 3104 3105 3106 3107 3108
}

static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
			       u32 devfn)
{
	return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
}

3109
static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
3110
{
3111 3112
	struct pnv_phb *phb = hose->private_data;

3113
	opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
3114 3115 3116
		       OPAL_ASSERT_RESET);
}

3117 3118 3119 3120 3121 3122 3123 3124 3125
static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
       .dma_dev_setup = pnv_pci_dma_dev_setup,
#ifdef CONFIG_PCI_MSI
       .setup_msi_irqs = pnv_setup_msi_irqs,
       .teardown_msi_irqs = pnv_teardown_msi_irqs,
#endif
       .enable_device_hook = pnv_pci_enable_device_hook,
       .window_alignment = pnv_pci_window_alignment,
       .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3126
       .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3127
       .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3128
       .shutdown = pnv_pci_ioda_shutdown,
3129 3130
};

3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
	.dma_dev_setup = pnv_pci_dma_dev_setup,
#ifdef CONFIG_PCI_MSI
	.setup_msi_irqs = pnv_setup_msi_irqs,
	.teardown_msi_irqs = pnv_teardown_msi_irqs,
#endif
	.enable_device_hook = pnv_pci_enable_device_hook,
	.window_alignment = pnv_pci_window_alignment,
	.reset_secondary_bus = pnv_pci_reset_secondary_bus,
	.dma_set_mask = pnv_npu_dma_set_mask,
	.shutdown = pnv_pci_ioda_shutdown,
};

3144 3145
static void __init pnv_pci_init_ioda_phb(struct device_node *np,
					 u64 hub_id, int ioda_type)
3146 3147 3148
{
	struct pci_controller *hose;
	struct pnv_phb *phb;
3149
	unsigned long size, m32map_off, pemap_off, iomap_off = 0;
3150
	const __be64 *prop64;
3151
	const __be32 *prop32;
3152
	int len;
3153 3154 3155 3156
	u64 phb_id;
	void *aux;
	long rc;

3157
	pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
3158 3159 3160 3161 3162 3163 3164 3165 3166

	prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
	if (!prop64) {
		pr_err("  Missing \"ibm,opal-phbid\" property !\n");
		return;
	}
	phb_id = be64_to_cpup(prop64);
	pr_debug("  PHB-ID  : 0x%016llx\n", phb_id);

3167
	phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
3168 3169 3170 3171 3172

	/* Allocate PCI controller */
	phb->hose = hose = pcibios_alloc_controller(np);
	if (!phb->hose) {
		pr_err("  Can't allocate PCI controller for %s\n",
3173
		       np->full_name);
3174
		memblock_free(__pa(phb), sizeof(struct pnv_phb));
3175 3176 3177 3178
		return;
	}

	spin_lock_init(&phb->lock);
3179 3180
	prop32 = of_get_property(np, "bus-range", &len);
	if (prop32 && len == 8) {
3181 3182
		hose->first_busno = be32_to_cpu(prop32[0]);
		hose->last_busno = be32_to_cpu(prop32[1]);
3183 3184 3185 3186 3187
	} else {
		pr_warn("  Broken <bus-range> on %s\n", np->full_name);
		hose->first_busno = 0;
		hose->last_busno = 0xff;
	}
3188
	hose->private_data = phb;
3189
	phb->hub_id = hub_id;
3190
	phb->opal_id = phb_id;
G
Gavin Shan 已提交
3191
	phb->type = ioda_type;
3192
	mutex_init(&phb->ioda.pe_alloc_mutex);
3193

3194 3195 3196
	/* Detect specific models for error handling */
	if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
		phb->model = PNV_PHB_MODEL_P7IOC;
3197
	else if (of_device_is_compatible(np, "ibm,power8-pciex"))
G
Gavin Shan 已提交
3198
		phb->model = PNV_PHB_MODEL_PHB3;
3199 3200
	else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
		phb->model = PNV_PHB_MODEL_NPU;
3201 3202 3203
	else
		phb->model = PNV_PHB_MODEL_UNKNOWN;

G
Gavin Shan 已提交
3204
	/* Parse 32-bit and IO ranges (if any) */
3205
	pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
3206

G
Gavin Shan 已提交
3207
	/* Get registers */
3208 3209 3210 3211 3212
	phb->regs = of_iomap(np, 0);
	if (phb->regs == NULL)
		pr_err("  Failed to map registers !\n");

	/* Initialize more IODA stuff */
3213
	phb->ioda.total_pe = 1;
G
Gavin Shan 已提交
3214
	prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
3215
	if (prop32)
3216
		phb->ioda.total_pe = be32_to_cpup(prop32);
3217 3218 3219
	prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
	if (prop32)
		phb->ioda.reserved_pe = be32_to_cpup(prop32);
3220 3221 3222 3223

	/* Parse 64-bit MMIO range */
	pnv_ioda_parse_m64_window(phb);

3224
	phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
G
Gavin Shan 已提交
3225
	/* FW Has already off top 64k of M32 space (MSI space) */
3226 3227 3228
	phb->ioda.m32_size += 0x10000;

	phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
3229
	phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
3230 3231 3232 3233
	phb->ioda.io_size = hose->pci_io_size;
	phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
	phb->ioda.io_pci_base = 0; /* XXX calculate this ? */

3234
	/* Allocate aux data & arrays. We don't have IO ports on PHB3 */
3235 3236
	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
	m32map_off = size;
3237
	size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
3238 3239 3240 3241
	if (phb->type == PNV_PHB_IODA1) {
		iomap_off = size;
		size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
	}
3242 3243
	pemap_off = size;
	size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
3244
	aux = memblock_virt_alloc(size, 0);
3245 3246
	phb->ioda.pe_alloc = aux;
	phb->ioda.m32_segmap = aux + m32map_off;
3247 3248
	if (phb->type == PNV_PHB_IODA1)
		phb->ioda.io_segmap = aux + iomap_off;
3249
	phb->ioda.pe_array = aux + pemap_off;
3250
	set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
3251

3252
	INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
3253
	INIT_LIST_HEAD(&phb->ioda.pe_list);
3254
	mutex_init(&phb->ioda.pe_list_mutex);
3255 3256 3257 3258

	/* Calculate how many 32-bit TCE segments we have */
	phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;

G
Gavin Shan 已提交
3259
#if 0 /* We should really do that ... */
3260 3261 3262 3263 3264 3265 3266 3267
	rc = opal_pci_set_phb_mem_window(opal->phb_id,
					 window_type,
					 window_num,
					 starting_real_address,
					 starting_pci_address,
					 segment_size);
#endif

3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
	pr_info("  %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
		phb->ioda.total_pe, phb->ioda.reserved_pe,
		phb->ioda.m32_size, phb->ioda.m32_segsize);
	if (phb->ioda.m64_size)
		pr_info("                 M64: 0x%lx [segment=0x%lx]\n",
			phb->ioda.m64_size, phb->ioda.m64_segsize);
	if (phb->ioda.io_size)
		pr_info("                  IO: 0x%x [segment=0x%x]\n",
			phb->ioda.io_size, phb->ioda.io_segsize);

3278 3279

	phb->hose->ops = &pnv_pci_ops;
G
Gavin Shan 已提交
3280 3281 3282
	phb->get_pe_state = pnv_ioda_get_pe_state;
	phb->freeze_pe = pnv_ioda_freeze_pe;
	phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292

	/* Setup RID -> PE mapping function */
	phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;

	/* Setup TCEs */
	phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;

	/* Setup MSI support */
	pnv_pci_init_ioda_msis(phb);

3293 3294 3295 3296 3297 3298
	/*
	 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
	 * to let the PCI core do resource assignment. It's supposed
	 * that the PCI core will do correct I/O and MMIO alignment
	 * for the P2P bridge bars so that each PCI bus (excluding
	 * the child P2P bridges) can form individual PE.
3299
	 */
3300
	ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
3301 3302 3303 3304 3305

	if (phb->type == PNV_PHB_NPU)
		hose->controller_ops = pnv_npu_ioda_controller_ops;
	else
		hose->controller_ops = pnv_pci_ioda_controller_ops;
3306

3307 3308
#ifdef CONFIG_PCI_IOV
	ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
3309
	ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
3310 3311
#endif

3312
	pci_add_flags(PCI_REASSIGN_ALL_RSRC);
3313 3314

	/* Reset IODA tables to a clean state */
3315
	rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
3316
	if (rc)
3317
		pr_warning("  OPAL Error %ld performing IODA table reset !\n", rc);
3318 3319 3320 3321 3322 3323 3324 3325

	/* If we're running in kdump kerenl, the previous kerenl never
	 * shutdown PCI devices correctly. We already got IODA table
	 * cleaned out. So we have to issue PHB reset to stop all PCI
	 * transactions from previous kerenl.
	 */
	if (is_kdump_kernel()) {
		pr_info("  Issue PHB reset ...\n");
3326 3327
		pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
		pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
3328
	}
3329

3330 3331
	/* Remove M64 resource if we can't configure it successfully */
	if (!phb->init_m64 || phb->init_m64(phb))
3332
		hose->mem_resources[1].flags = 0;
G
Gavin Shan 已提交
3333 3334
}

3335
void __init pnv_pci_init_ioda2_phb(struct device_node *np)
G
Gavin Shan 已提交
3336
{
3337
	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
3338 3339
}

3340 3341 3342 3343 3344
void __init pnv_pci_init_npu_phb(struct device_node *np)
{
	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
}

3345 3346 3347
void __init pnv_pci_init_ioda_hub(struct device_node *np)
{
	struct device_node *phbn;
3348
	const __be64 *prop64;
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
	u64 hub_id;

	pr_info("Probing IODA IO-Hub %s\n", np->full_name);

	prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
	if (!prop64) {
		pr_err(" Missing \"ibm,opal-hubid\" property !\n");
		return;
	}
	hub_id = be64_to_cpup(prop64);
	pr_devel(" HUB-ID : 0x%016llx\n", hub_id);

	/* Count child PHBs */
	for_each_child_of_node(np, phbn) {
		/* Look for IODA1 PHBs */
		if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
3365
			pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
3366 3367
	}
}