pci.c 23.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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.
 */

#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/io.h>
19
#include <linux/msi.h>
20
#include <linux/iommu.h>
21 22 23 24 25 26

#include <asm/sections.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/pci-bridge.h>
#include <asm/machdep.h>
27
#include <asm/msi_bitmap.h>
28
#include <asm/ppc-pci.h>
29
#include <asm/pnv-pci.h>
30 31 32
#include <asm/opal.h>
#include <asm/iommu.h>
#include <asm/tce.h>
33
#include <asm/firmware.h>
34 35
#include <asm/eeh_event.h>
#include <asm/eeh.h>
36 37 38 39

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
int pnv_pci_get_slot_id(struct device_node *np, uint64_t *id)
{
	struct device_node *parent = np;
	u32 bdfn;
	u64 phbid;
	int ret;

	ret = of_property_read_u32(np, "reg", &bdfn);
	if (ret)
		return -ENXIO;

	bdfn = ((bdfn & 0x00ffff00) >> 8);
	while ((parent = of_get_parent(parent))) {
		if (!PCI_DN(parent)) {
			of_node_put(parent);
			break;
		}

		if (!of_device_is_compatible(parent, "ibm,ioda2-phb")) {
			of_node_put(parent);
			continue;
		}

		ret = of_property_read_u64(parent, "ibm,opal-phbid", &phbid);
		if (ret) {
			of_node_put(parent);
			return -ENXIO;
		}

		*id = PCI_SLOT_ID(phbid, bdfn);
		return 0;
	}

	return -ENODEV;
}
EXPORT_SYMBOL_GPL(pnv_pci_get_slot_id);

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
int pnv_pci_get_device_tree(uint32_t phandle, void *buf, uint64_t len)
{
	int64_t rc;

	if (!opal_check_token(OPAL_GET_DEVICE_TREE))
		return -ENXIO;

	rc = opal_get_device_tree(phandle, (uint64_t)buf, len);
	if (rc < OPAL_SUCCESS)
		return -EIO;

	return rc;
}
EXPORT_SYMBOL_GPL(pnv_pci_get_device_tree);

int pnv_pci_get_presence_state(uint64_t id, uint8_t *state)
{
	int64_t rc;

	if (!opal_check_token(OPAL_PCI_GET_PRESENCE_STATE))
		return -ENXIO;

	rc = opal_pci_get_presence_state(id, (uint64_t)state);
	if (rc != OPAL_SUCCESS)
		return -EIO;

	return 0;
}
EXPORT_SYMBOL_GPL(pnv_pci_get_presence_state);

int pnv_pci_get_power_state(uint64_t id, uint8_t *state)
{
	int64_t rc;

	if (!opal_check_token(OPAL_PCI_GET_POWER_STATE))
		return -ENXIO;

	rc = opal_pci_get_power_state(id, (uint64_t)state);
	if (rc != OPAL_SUCCESS)
		return -EIO;

	return 0;
}
EXPORT_SYMBOL_GPL(pnv_pci_get_power_state);

int pnv_pci_set_power_state(uint64_t id, uint8_t state, struct opal_msg *msg)
{
	struct opal_msg m;
	int token, ret;
	int64_t rc;

	if (!opal_check_token(OPAL_PCI_SET_POWER_STATE))
		return -ENXIO;

	token = opal_async_get_token_interruptible();
	if (unlikely(token < 0))
		return token;

	rc = opal_pci_set_power_state(token, id, (uint64_t)&state);
	if (rc == OPAL_SUCCESS) {
		ret = 0;
		goto exit;
	} else if (rc != OPAL_ASYNC_COMPLETION) {
		ret = -EIO;
		goto exit;
	}

	ret = opal_async_wait_response(token, &m);
	if (ret < 0)
		goto exit;

	if (msg) {
		ret = 1;
		memcpy(msg, &m, sizeof(m));
	}

exit:
	opal_async_release_token(token);
	return ret;
}
EXPORT_SYMBOL_GPL(pnv_pci_set_power_state);

159
#ifdef CONFIG_PCI_MSI
160
int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
161 162 163 164 165
{
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
	struct msi_desc *entry;
	struct msi_msg msg;
166 167
	int hwirq;
	unsigned int virq;
168 169
	int rc;

170 171 172
	if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
		return -ENODEV;

173
	if (pdev->no_64bit_msi && !phb->msi32_support)
174 175
		return -ENODEV;

176
	for_each_pci_msi_entry(entry, pdev) {
177 178 179 180 181
		if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
			pr_warn("%s: Supports only 64-bit MSIs\n",
				pci_name(pdev));
			return -ENXIO;
		}
182 183
		hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
		if (hwirq < 0) {
184 185 186 187
			pr_warn("%s: Failed to find a free MSI\n",
				pci_name(pdev));
			return -ENOSPC;
		}
188
		virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
189 190 191
		if (virq == NO_IRQ) {
			pr_warn("%s: Failed to map MSI to linux irq\n",
				pci_name(pdev));
192
			msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
193 194
			return -ENOMEM;
		}
195
		rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
196
				    virq, entry->msi_attrib.is_64, &msg);
197 198 199
		if (rc) {
			pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
			irq_dispose_mapping(virq);
200
			msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
201 202 203
			return rc;
		}
		irq_set_msi_desc(virq, entry);
204
		pci_write_msi_msg(virq, &msg);
205 206 207 208
	}
	return 0;
}

209
void pnv_teardown_msi_irqs(struct pci_dev *pdev)
210 211 212 213
{
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
	struct msi_desc *entry;
214
	irq_hw_number_t hwirq;
215 216 217 218

	if (WARN_ON(!phb))
		return;

219
	for_each_pci_msi_entry(entry, pdev) {
220 221
		if (entry->irq == NO_IRQ)
			continue;
222
		hwirq = virq_to_hw(entry->irq);
223 224
		irq_set_msi_desc(entry->irq, NULL);
		irq_dispose_mapping(entry->irq);
225
		msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, 1);
226 227 228
	}
}
#endif /* CONFIG_PCI_MSI */
229

230 231
static void pnv_pci_dump_p7ioc_diag_data(struct pci_controller *hose,
					 struct OpalIoPhbErrorCommon *common)
232
{
233
	struct OpalIoP7IOCPhbErrorData *data;
234 235
	int i;

236
	data = (struct OpalIoP7IOCPhbErrorData *)common;
237
	pr_info("P7IOC PHB#%d Diag-data (Version: %d)\n",
238
		hose->global_number, be32_to_cpu(common->version));
239

240
	if (data->brdgCtl)
241
		pr_info("brdgCtl:     %08x\n",
242
			be32_to_cpu(data->brdgCtl));
243 244
	if (data->portStatusReg || data->rootCmplxStatus ||
	    data->busAgentStatus)
245
		pr_info("UtlSts:      %08x %08x %08x\n",
246 247 248
			be32_to_cpu(data->portStatusReg),
			be32_to_cpu(data->rootCmplxStatus),
			be32_to_cpu(data->busAgentStatus));
249 250 251
	if (data->deviceStatus || data->slotStatus   ||
	    data->linkStatus   || data->devCmdStatus ||
	    data->devSecStatus)
252
		pr_info("RootSts:     %08x %08x %08x %08x %08x\n",
253 254 255 256 257
			be32_to_cpu(data->deviceStatus),
			be32_to_cpu(data->slotStatus),
			be32_to_cpu(data->linkStatus),
			be32_to_cpu(data->devCmdStatus),
			be32_to_cpu(data->devSecStatus));
258 259
	if (data->rootErrorStatus   || data->uncorrErrorStatus ||
	    data->corrErrorStatus)
260
		pr_info("RootErrSts:  %08x %08x %08x\n",
261 262 263
			be32_to_cpu(data->rootErrorStatus),
			be32_to_cpu(data->uncorrErrorStatus),
			be32_to_cpu(data->corrErrorStatus));
264 265
	if (data->tlpHdr1 || data->tlpHdr2 ||
	    data->tlpHdr3 || data->tlpHdr4)
266
		pr_info("RootErrLog:  %08x %08x %08x %08x\n",
267 268 269 270
			be32_to_cpu(data->tlpHdr1),
			be32_to_cpu(data->tlpHdr2),
			be32_to_cpu(data->tlpHdr3),
			be32_to_cpu(data->tlpHdr4));
271 272
	if (data->sourceId || data->errorClass ||
	    data->correlator)
273
		pr_info("RootErrLog1: %08x %016llx %016llx\n",
274 275 276
			be32_to_cpu(data->sourceId),
			be64_to_cpu(data->errorClass),
			be64_to_cpu(data->correlator));
277
	if (data->p7iocPlssr || data->p7iocCsr)
278
		pr_info("PhbSts:      %016llx %016llx\n",
279 280
			be64_to_cpu(data->p7iocPlssr),
			be64_to_cpu(data->p7iocCsr));
281 282
	if (data->lemFir)
		pr_info("Lem:         %016llx %016llx %016llx\n",
283 284 285
			be64_to_cpu(data->lemFir),
			be64_to_cpu(data->lemErrorMask),
			be64_to_cpu(data->lemWOF));
286 287
	if (data->phbErrorStatus)
		pr_info("PhbErr:      %016llx %016llx %016llx %016llx\n",
288 289 290 291
			be64_to_cpu(data->phbErrorStatus),
			be64_to_cpu(data->phbFirstErrorStatus),
			be64_to_cpu(data->phbErrorLog0),
			be64_to_cpu(data->phbErrorLog1));
292 293
	if (data->mmioErrorStatus)
		pr_info("OutErr:      %016llx %016llx %016llx %016llx\n",
294 295 296 297
			be64_to_cpu(data->mmioErrorStatus),
			be64_to_cpu(data->mmioFirstErrorStatus),
			be64_to_cpu(data->mmioErrorLog0),
			be64_to_cpu(data->mmioErrorLog1));
298 299
	if (data->dma0ErrorStatus)
		pr_info("InAErr:      %016llx %016llx %016llx %016llx\n",
300 301 302 303
			be64_to_cpu(data->dma0ErrorStatus),
			be64_to_cpu(data->dma0FirstErrorStatus),
			be64_to_cpu(data->dma0ErrorLog0),
			be64_to_cpu(data->dma0ErrorLog1));
304 305
	if (data->dma1ErrorStatus)
		pr_info("InBErr:      %016llx %016llx %016llx %016llx\n",
306 307 308 309
			be64_to_cpu(data->dma1ErrorStatus),
			be64_to_cpu(data->dma1FirstErrorStatus),
			be64_to_cpu(data->dma1ErrorLog0),
			be64_to_cpu(data->dma1ErrorLog1));
310 311 312 313 314

	for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
		if ((data->pestA[i] >> 63) == 0 &&
		    (data->pestB[i] >> 63) == 0)
			continue;
315

316
		pr_info("PE[%3d] A/B: %016llx %016llx\n",
317 318
			i, be64_to_cpu(data->pestA[i]),
			be64_to_cpu(data->pestB[i]));
319 320 321
	}
}

322 323
static void pnv_pci_dump_phb3_diag_data(struct pci_controller *hose,
					struct OpalIoPhbErrorCommon *common)
324
{
325 326 327 328
	struct OpalIoPhb3ErrorData *data;
	int i;

	data = (struct OpalIoPhb3ErrorData*)common;
329
	pr_info("PHB3 PHB#%d Diag-data (Version: %d)\n",
330
		hose->global_number, be32_to_cpu(common->version));
331
	if (data->brdgCtl)
332
		pr_info("brdgCtl:     %08x\n",
333
			be32_to_cpu(data->brdgCtl));
334 335
	if (data->portStatusReg || data->rootCmplxStatus ||
	    data->busAgentStatus)
336
		pr_info("UtlSts:      %08x %08x %08x\n",
337 338 339
			be32_to_cpu(data->portStatusReg),
			be32_to_cpu(data->rootCmplxStatus),
			be32_to_cpu(data->busAgentStatus));
340 341 342
	if (data->deviceStatus || data->slotStatus   ||
	    data->linkStatus   || data->devCmdStatus ||
	    data->devSecStatus)
343
		pr_info("RootSts:     %08x %08x %08x %08x %08x\n",
344 345 346 347 348
			be32_to_cpu(data->deviceStatus),
			be32_to_cpu(data->slotStatus),
			be32_to_cpu(data->linkStatus),
			be32_to_cpu(data->devCmdStatus),
			be32_to_cpu(data->devSecStatus));
349 350
	if (data->rootErrorStatus || data->uncorrErrorStatus ||
	    data->corrErrorStatus)
351
		pr_info("RootErrSts:  %08x %08x %08x\n",
352 353 354
			be32_to_cpu(data->rootErrorStatus),
			be32_to_cpu(data->uncorrErrorStatus),
			be32_to_cpu(data->corrErrorStatus));
355 356
	if (data->tlpHdr1 || data->tlpHdr2 ||
	    data->tlpHdr3 || data->tlpHdr4)
357
		pr_info("RootErrLog:  %08x %08x %08x %08x\n",
358 359 360 361
			be32_to_cpu(data->tlpHdr1),
			be32_to_cpu(data->tlpHdr2),
			be32_to_cpu(data->tlpHdr3),
			be32_to_cpu(data->tlpHdr4));
362 363
	if (data->sourceId || data->errorClass ||
	    data->correlator)
364
		pr_info("RootErrLog1: %08x %016llx %016llx\n",
365 366 367
			be32_to_cpu(data->sourceId),
			be64_to_cpu(data->errorClass),
			be64_to_cpu(data->correlator));
368 369
	if (data->nFir)
		pr_info("nFir:        %016llx %016llx %016llx\n",
370 371 372
			be64_to_cpu(data->nFir),
			be64_to_cpu(data->nFirMask),
			be64_to_cpu(data->nFirWOF));
373
	if (data->phbPlssr || data->phbCsr)
374
		pr_info("PhbSts:      %016llx %016llx\n",
375 376
			be64_to_cpu(data->phbPlssr),
			be64_to_cpu(data->phbCsr));
377 378
	if (data->lemFir)
		pr_info("Lem:         %016llx %016llx %016llx\n",
379 380 381
			be64_to_cpu(data->lemFir),
			be64_to_cpu(data->lemErrorMask),
			be64_to_cpu(data->lemWOF));
382 383
	if (data->phbErrorStatus)
		pr_info("PhbErr:      %016llx %016llx %016llx %016llx\n",
384 385 386 387
			be64_to_cpu(data->phbErrorStatus),
			be64_to_cpu(data->phbFirstErrorStatus),
			be64_to_cpu(data->phbErrorLog0),
			be64_to_cpu(data->phbErrorLog1));
388 389
	if (data->mmioErrorStatus)
		pr_info("OutErr:      %016llx %016llx %016llx %016llx\n",
390 391 392 393
			be64_to_cpu(data->mmioErrorStatus),
			be64_to_cpu(data->mmioFirstErrorStatus),
			be64_to_cpu(data->mmioErrorLog0),
			be64_to_cpu(data->mmioErrorLog1));
394 395
	if (data->dma0ErrorStatus)
		pr_info("InAErr:      %016llx %016llx %016llx %016llx\n",
396 397 398 399
			be64_to_cpu(data->dma0ErrorStatus),
			be64_to_cpu(data->dma0FirstErrorStatus),
			be64_to_cpu(data->dma0ErrorLog0),
			be64_to_cpu(data->dma0ErrorLog1));
400 401
	if (data->dma1ErrorStatus)
		pr_info("InBErr:      %016llx %016llx %016llx %016llx\n",
402 403 404 405
			be64_to_cpu(data->dma1ErrorStatus),
			be64_to_cpu(data->dma1FirstErrorStatus),
			be64_to_cpu(data->dma1ErrorLog0),
			be64_to_cpu(data->dma1ErrorLog1));
406 407

	for (i = 0; i < OPAL_PHB3_NUM_PEST_REGS; i++) {
408 409
		if ((be64_to_cpu(data->pestA[i]) >> 63) == 0 &&
		    (be64_to_cpu(data->pestB[i]) >> 63) == 0)
410 411
			continue;

412
		pr_info("PE[%3d] A/B: %016llx %016llx\n",
413 414
				i, be64_to_cpu(data->pestA[i]),
				be64_to_cpu(data->pestB[i]));
415 416 417 418 419 420 421 422 423 424 425 426
	}
}

void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
				unsigned char *log_buff)
{
	struct OpalIoPhbErrorCommon *common;

	if (!hose || !log_buff)
		return;

	common = (struct OpalIoPhbErrorCommon *)log_buff;
427
	switch (be32_to_cpu(common->ioType)) {
428 429 430 431 432
	case OPAL_PHB_ERROR_DATA_TYPE_P7IOC:
		pnv_pci_dump_p7ioc_diag_data(hose, common);
		break;
	case OPAL_PHB_ERROR_DATA_TYPE_PHB3:
		pnv_pci_dump_phb3_diag_data(hose, common);
433 434
		break;
	default:
435
		pr_warn("%s: Unrecognized ioType %d\n",
436
			__func__, be32_to_cpu(common->ioType));
437 438 439 440 441 442
	}
}

static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
{
	unsigned long flags, rc;
443
	int has_diag, ret = 0;
444 445 446

	spin_lock_irqsave(&phb->lock, flags);

447
	/* Fetch PHB diag-data */
448 449
	rc = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
					 PNV_PCI_DIAG_BUF_SIZE);
450 451
	has_diag = (rc == OPAL_SUCCESS);

452 453 454 455
	/* If PHB supports compound PE, to handle it */
	if (phb->unfreeze_pe) {
		ret = phb->unfreeze_pe(phb,
				       pe_no,
456
				       OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
457 458 459 460 461 462 463 464 465 466 467
	} else {
		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
					     pe_no,
					     OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
		if (rc) {
			pr_warn("%s: Failure %ld clearing frozen "
				"PHB#%x-PE#%x\n",
				__func__, rc, phb->hose->global_number,
				pe_no);
			ret = -EIO;
		}
468 469
	}

470 471 472 473 474 475 476 477 478
	/*
	 * For now, let's only display the diag buffer when we fail to clear
	 * the EEH status. We'll do more sensible things later when we have
	 * proper EEH support. We need to make sure we don't pollute ourselves
	 * with the normal errors generated when probing empty slots
	 */
	if (has_diag && ret)
		pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);

479 480 481
	spin_unlock_irqrestore(&phb->lock, flags);
}

482
static void pnv_pci_config_check_eeh(struct pci_dn *pdn)
483
{
484
	struct pnv_phb *phb = pdn->phb->private_data;
485
	u8	fstate;
486
	__be16	pcierr;
487
	unsigned int pe_no;
488
	s64	rc;
489

490 491 492
	/*
	 * Get the PE#. During the PCI probe stage, we might not
	 * setup that yet. So all ER errors should be mapped to
493
	 * reserved PE.
494
	 */
495
	pe_no = pdn->pe_number;
496
	if (pe_no == IODA_INVALID_PE) {
497
		pe_no = phb->ioda.reserved_pe_idx;
498
	}
499

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	/*
	 * Fetch frozen state. If the PHB support compound PE,
	 * we need handle that case.
	 */
	if (phb->get_pe_state) {
		fstate = phb->get_pe_state(phb, pe_no);
	} else {
		rc = opal_pci_eeh_freeze_status(phb->opal_id,
						pe_no,
						&fstate,
						&pcierr,
						NULL);
		if (rc) {
			pr_warn("%s: Failure %lld getting PHB#%x-PE#%x state\n",
				__func__, rc, phb->hose->global_number, pe_no);
			return;
		}
517
	}
518

519 520
	pr_devel(" -> EEH check, bdfn=%04x PE#%d fstate=%x\n",
		 (pdn->busno << 8) | (pdn->devfn), pe_no, fstate);
521 522 523 524 525 526 527 528 529 530 531 532

	/* Clear the frozen state if applicable */
	if (fstate == OPAL_EEH_STOPPED_MMIO_FREEZE ||
	    fstate == OPAL_EEH_STOPPED_DMA_FREEZE  ||
	    fstate == OPAL_EEH_STOPPED_MMIO_DMA_FREEZE) {
		/*
		 * If PHB supports compound PE, freeze it for
		 * consistency.
		 */
		if (phb->freeze_pe)
			phb->freeze_pe(phb, pe_no);

533
		pnv_pci_handle_eeh_config(phb, pe_no);
534
	}
535 536
}

537
int pnv_pci_cfg_read(struct pci_dn *pdn,
538
		     int where, int size, u32 *val)
539
{
540 541
	struct pnv_phb *phb = pdn->phb->private_data;
	u32 bdfn = (pdn->busno << 8) | pdn->devfn;
542 543 544 545 546 547 548 549 550 551
	s64 rc;

	switch (size) {
	case 1: {
		u8 v8;
		rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
		*val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
		break;
	}
	case 2: {
552
		__be16 v16;
553 554
		rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
						   &v16);
555
		*val = (rc == OPAL_SUCCESS) ? be16_to_cpu(v16) : 0xffff;
556 557 558
		break;
	}
	case 4: {
559
		__be32 v32;
560
		rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
561
		*val = (rc == OPAL_SUCCESS) ? be32_to_cpu(v32) : 0xffffffff;
562 563 564 565 566
		break;
	}
	default:
		return PCIBIOS_FUNC_NOT_SUPPORTED;
	}
567

568 569
	pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
		 __func__, pdn->busno, pdn->devfn, where, size, *val);
570 571 572
	return PCIBIOS_SUCCESSFUL;
}

573
int pnv_pci_cfg_write(struct pci_dn *pdn,
574
		      int where, int size, u32 val)
575
{
576 577
	struct pnv_phb *phb = pdn->phb->private_data;
	u32 bdfn = (pdn->busno << 8) | pdn->devfn;
578

579 580
	pr_devel("%s: bus: %x devfn: %x +%x/%x -> %08x\n",
		 __func__, pdn->busno, pdn->devfn, where, size, val);
581 582 583 584 585 586 587 588 589 590 591 592 593
	switch (size) {
	case 1:
		opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
		break;
	case 2:
		opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
		break;
	case 4:
		opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
		break;
	default:
		return PCIBIOS_FUNC_NOT_SUPPORTED;
	}
594

595 596 597 598
	return PCIBIOS_SUCCESSFUL;
}

#if CONFIG_EEH
599
static bool pnv_pci_cfg_check(struct pci_dn *pdn)
600 601
{
	struct eeh_dev *edev = NULL;
602
	struct pnv_phb *phb = pdn->phb->private_data;
603 604

	/* EEH not enabled ? */
605
	if (!(phb->flags & PNV_PHB_FLAG_EEH))
606
		return true;
607

608
	/* PE reset or device removed ? */
609
	edev = pdn->edev;
610 611
	if (edev) {
		if (edev->pe &&
612
		    (edev->pe->state & EEH_PE_CFG_BLOCKED))
613 614 615 616 617
			return false;

		if (edev->mode & EEH_DEV_REMOVED)
			return false;
	}
618 619 620 621

	return true;
}
#else
622
static inline pnv_pci_cfg_check(struct pci_dn *pdn)
623 624
{
	return true;
625
}
626
#endif /* CONFIG_EEH */
627

628 629 630 631 632
static int pnv_pci_read_config(struct pci_bus *bus,
			       unsigned int devfn,
			       int where, int size, u32 *val)
{
	struct pci_dn *pdn;
633 634
	struct pnv_phb *phb;
	int ret;
635

636
	*val = 0xFFFFFFFF;
637 638 639
	pdn = pci_get_pdn_by_devfn(bus, devfn);
	if (!pdn)
		return PCIBIOS_DEVICE_NOT_FOUND;
640

641
	if (!pnv_pci_cfg_check(pdn))
642 643
		return PCIBIOS_DEVICE_NOT_FOUND;

644 645 646
	ret = pnv_pci_cfg_read(pdn, where, size, val);
	phb = pdn->phb->private_data;
	if (phb->flags & PNV_PHB_FLAG_EEH && pdn->edev) {
647
		if (*val == EEH_IO_ERROR_VALUE(size) &&
648
		    eeh_dev_check_failure(pdn->edev))
649 650
                        return PCIBIOS_DEVICE_NOT_FOUND;
	} else {
651
		pnv_pci_config_check_eeh(pdn);
652
	}
653

654
	return ret;
655 656 657 658 659 660 661
}

static int pnv_pci_write_config(struct pci_bus *bus,
				unsigned int devfn,
				int where, int size, u32 val)
{
	struct pci_dn *pdn;
662 663
	struct pnv_phb *phb;
	int ret;
664

665 666 667
	pdn = pci_get_pdn_by_devfn(bus, devfn);
	if (!pdn)
		return PCIBIOS_DEVICE_NOT_FOUND;
668

669
	if (!pnv_pci_cfg_check(pdn))
670 671
		return PCIBIOS_DEVICE_NOT_FOUND;

672 673
	ret = pnv_pci_cfg_write(pdn, where, size, val);
	phb = pdn->phb->private_data;
674
	if (!(phb->flags & PNV_PHB_FLAG_EEH))
675
		pnv_pci_config_check_eeh(pdn);
676 677

	return ret;
678 679
}

680
struct pci_ops pnv_pci_ops = {
681
	.read  = pnv_pci_read_config,
682 683 684
	.write = pnv_pci_write_config,
};

685 686 687
static __be64 *pnv_tce(struct iommu_table *tbl, long idx)
{
	__be64 *tmp = ((__be64 *)tbl->it_base);
688 689 690 691 692 693 694 695 696 697 698 699 700
	int  level = tbl->it_indirect_levels;
	const long shift = ilog2(tbl->it_level_size);
	unsigned long mask = (tbl->it_level_size - 1) << (level * shift);

	while (level) {
		int n = (idx & mask) >> (level * shift);
		unsigned long tce = be64_to_cpu(tmp[n]);

		tmp = __va(tce & ~(TCE_PCI_READ | TCE_PCI_WRITE));
		idx &= ~mask;
		mask >>= shift;
		--level;
	}
701 702 703 704

	return tmp + idx;
}

705 706
int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
		unsigned long uaddr, enum dma_data_direction direction,
707
		unsigned long attrs)
708
{
709
	u64 proto_tce = iommu_direction_to_tce_perm(direction);
710 711
	u64 rpn = __pa(uaddr) >> tbl->it_page_shift;
	long i;
712

713 714 715
	if (proto_tce & TCE_PCI_WRITE)
		proto_tce |= TCE_PCI_READ;

716 717 718 719
	for (i = 0; i < npages; i++) {
		unsigned long newtce = proto_tce |
			((rpn + i) << tbl->it_page_shift);
		unsigned long idx = index - tbl->it_offset + i;
720

721 722
		*(pnv_tce(tbl, idx)) = cpu_to_be64(newtce);
	}
723 724 725 726

	return 0;
}

727 728 729 730 731 732 733 734 735 736
#ifdef CONFIG_IOMMU_API
int pnv_tce_xchg(struct iommu_table *tbl, long index,
		unsigned long *hpa, enum dma_data_direction *direction)
{
	u64 proto_tce = iommu_direction_to_tce_perm(*direction);
	unsigned long newtce = *hpa | proto_tce, oldtce;
	unsigned long idx = index - tbl->it_offset;

	BUG_ON(*hpa & ~IOMMU_PAGE_MASK(tbl));

737 738 739
	if (newtce & TCE_PCI_WRITE)
		newtce |= TCE_PCI_READ;

740 741
	oldtce = be64_to_cpu(xchg(pnv_tce(tbl, idx), cpu_to_be64(newtce)));
	*hpa = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
742 743 744 745 746 747
	*direction = iommu_tce_direction(oldtce);

	return 0;
}
#endif

748
void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
749
{
750
	long i;
751

752 753
	for (i = 0; i < npages; i++) {
		unsigned long idx = index - tbl->it_offset + i;
754

755 756
		*(pnv_tce(tbl, idx)) = cpu_to_be64(0);
	}
757 758
}

759
unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
760
{
761
	return *(pnv_tce(tbl, index - tbl->it_offset));
762 763
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
struct iommu_table *pnv_pci_table_alloc(int nid)
{
	struct iommu_table *tbl;

	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
	INIT_LIST_HEAD_RCU(&tbl->it_group_list);

	return tbl;
}

long pnv_pci_link_table_and_group(int node, int num,
		struct iommu_table *tbl,
		struct iommu_table_group *table_group)
{
	struct iommu_table_group_link *tgl = NULL;

	if (WARN_ON(!tbl || !table_group))
		return -EINVAL;

	tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
			node);
	if (!tgl)
		return -ENOMEM;

	tgl->table_group = table_group;
	list_add_rcu(&tgl->next, &tbl->it_group_list);

	table_group->tables[num] = tbl;

	return 0;
}

static void pnv_iommu_table_group_link_free(struct rcu_head *head)
{
	struct iommu_table_group_link *tgl = container_of(head,
			struct iommu_table_group_link, rcu);

	kfree(tgl);
}

void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
		struct iommu_table_group *table_group)
{
	long i;
	bool found;
	struct iommu_table_group_link *tgl;

	if (!tbl || !table_group)
		return;

	/* Remove link to a group from table's list of attached groups */
	found = false;
	list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
		if (tgl->table_group == table_group) {
			list_del_rcu(&tgl->next);
			call_rcu(&tgl->rcu, pnv_iommu_table_group_link_free);
			found = true;
			break;
		}
	}
	if (WARN_ON(!found))
		return;

	/* Clean a pointer to iommu_table in iommu_table_group::tables[] */
	found = false;
	for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
		if (table_group->tables[i] == tbl) {
			table_group->tables[i] = NULL;
			found = true;
			break;
		}
	}
	WARN_ON(!found);
}

839 840
void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
			       void *tce_mem, u64 tce_size,
841
			       u64 dma_offset, unsigned page_shift)
842 843 844
{
	tbl->it_blocksize = 16;
	tbl->it_base = (unsigned long)tce_mem;
845
	tbl->it_page_shift = page_shift;
846
	tbl->it_offset = dma_offset >> tbl->it_page_shift;
847 848 849 850 851 852
	tbl->it_index = 0;
	tbl->it_size = tce_size >> 3;
	tbl->it_busno = 0;
	tbl->it_type = TCE_PCI;
}

853
void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
854 855 856
{
	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
	struct pnv_phb *phb = hose->private_data;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
#ifdef CONFIG_PCI_IOV
	struct pnv_ioda_pe *pe;
	struct pci_dn *pdn;

	/* Fix the VF pdn PE number */
	if (pdev->is_virtfn) {
		pdn = pci_get_pdn(pdev);
		WARN_ON(pdn->pe_number != IODA_INVALID_PE);
		list_for_each_entry(pe, &phb->ioda.pe_list, list) {
			if (pe->rid == ((pdev->bus->number << 8) |
			    (pdev->devfn & 0xff))) {
				pdn->pe_number = pe->pe_number;
				pe->pdev = pdev;
				break;
			}
		}
	}
#endif /* CONFIG_PCI_IOV */
875 876 877 878 879

	if (phb && phb->dma_dev_setup)
		phb->dma_dev_setup(phb, pdev);
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
void pnv_pci_dma_bus_setup(struct pci_bus *bus)
{
	struct pci_controller *hose = bus->sysdata;
	struct pnv_phb *phb = hose->private_data;
	struct pnv_ioda_pe *pe;

	list_for_each_entry(pe, &phb->ioda.pe_list, list) {
		if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
			continue;

		if (!pe->pbus)
			continue;

		if (bus->number == ((pe->rid >> 8) & 0xFF)) {
			pe->pbus = bus;
			break;
		}
	}
}

900 901 902 903
void pnv_pci_shutdown(void)
{
	struct pci_controller *hose;

904 905 906
	list_for_each_entry(hose, &hose_list, list_node)
		if (hose->controller_ops.shutdown)
			hose->controller_ops.shutdown(hose);
907 908
}

G
Gavin Shan 已提交
909
/* Fixup wrong class code in p7ioc and p8 root complex */
910
static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
911 912 913 914 915
{
	dev->class = PCI_CLASS_BRIDGE_PCI << 8;
}
DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);

916 917 918 919
void __init pnv_pci_init(void)
{
	struct device_node *np;

920
	pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
921

922 923 924
	/* If we don't have OPAL, eg. in sim, just skip PCI probe */
	if (!firmware_has_feature(FW_FEATURE_OPAL))
		return;
925

926
	/* Look for IODA IO-Hubs. */
927 928 929
	for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
		pnv_pci_init_ioda_hub(np);
	}
930

931 932 933
	/* Look for ioda2 built-in PHB3's */
	for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
		pnv_pci_init_ioda2_phb(np);
934

935 936 937 938
	/* Look for ioda3 built-in PHB4's, we treat them as IODA2 */
	for_each_compatible_node(np, NULL, "ibm,ioda3-phb")
		pnv_pci_init_ioda2_phb(np);

939 940 941 942
	/* Look for NPU PHBs */
	for_each_compatible_node(np, NULL, "ibm,ioda2-npu-phb")
		pnv_pci_init_npu_phb(np);

943 944 945
	/* Configure IOMMU DMA hooks */
	set_pci_dma_ops(&dma_iommu_ops);
}
946

947
machine_subsys_initcall_sync(powernv, tce_iommu_bus_notifier_init);