vio.c 48.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * IBM PowerPC Virtual I/O Infrastructure Support.
 *
4
 *    Copyright (c) 2003,2008 IBM Corp.
L
Linus Torvalds 已提交
5 6 7
 *     Dave Engebretsen engebret@us.ibm.com
 *     Santiago Leon santil@us.ibm.com
 *     Hollis Blanchard <hollisb@us.ibm.com>
8
 *     Stephen Rothwell
9
 *     Robert Jennings <rcjenn@us.ibm.com>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16
 *
 *      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.
 */

17
#include <linux/cpu.h>
18
#include <linux/types.h>
19
#include <linux/delay.h>
20
#include <linux/stat.h>
21
#include <linux/device.h>
L
Linus Torvalds 已提交
22
#include <linux/init.h>
23
#include <linux/slab.h>
L
Linus Torvalds 已提交
24
#include <linux/console.h>
25
#include <linux/export.h>
L
Linus Torvalds 已提交
26 27
#include <linux/mm.h>
#include <linux/dma-mapping.h>
28 29
#include <linux/kobject.h>

L
Linus Torvalds 已提交
30 31 32
#include <asm/iommu.h>
#include <asm/dma.h>
#include <asm/vio.h>
33
#include <asm/prom.h>
34
#include <asm/firmware.h>
35 36 37 38 39
#include <asm/tce.h>
#include <asm/page.h>
#include <asm/hvcall.h>

static struct vio_dev vio_bus_device  = { /* fake "parent" device */
40
	.name = "vio",
41
	.type = "",
42
	.dev.init_name = "vio",
43
	.dev.bus = &vio_bus_type,
L
Linus Torvalds 已提交
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 77 78 79 80 81 82 83 84 85 86 87 88 89
#ifdef CONFIG_PPC_SMLPAR
/**
 * vio_cmo_pool - A pool of IO memory for CMO use
 *
 * @size: The size of the pool in bytes
 * @free: The amount of free memory in the pool
 */
struct vio_cmo_pool {
	size_t size;
	size_t free;
};

/* How many ms to delay queued balance work */
#define VIO_CMO_BALANCE_DELAY 100

/* Portion out IO memory to CMO devices by this chunk size */
#define VIO_CMO_BALANCE_CHUNK 131072

/**
 * vio_cmo_dev_entry - A device that is CMO-enabled and requires entitlement
 *
 * @vio_dev: struct vio_dev pointer
 * @list: pointer to other devices on bus that are being tracked
 */
struct vio_cmo_dev_entry {
	struct vio_dev *viodev;
	struct list_head list;
};

/**
 * vio_cmo - VIO bus accounting structure for CMO entitlement
 *
 * @lock: spinlock for entire structure
 * @balance_q: work queue for balancing system entitlement
 * @device_list: list of CMO-enabled devices requiring entitlement
 * @entitled: total system entitlement in bytes
 * @reserve: pool of memory from which devices reserve entitlement, incl. spare
 * @excess: pool of excess entitlement not needed for device reserves or spare
 * @spare: IO memory for device hotplug functionality
 * @min: minimum necessary for system operation
 * @desired: desired memory for system operation
 * @curr: bytes currently allocated
 * @high: high water mark for IO data usage
 */
90
static struct vio_cmo {
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 159 160 161 162 163 164 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	spinlock_t lock;
	struct delayed_work balance_q;
	struct list_head device_list;
	size_t entitled;
	struct vio_cmo_pool reserve;
	struct vio_cmo_pool excess;
	size_t spare;
	size_t min;
	size_t desired;
	size_t curr;
	size_t high;
} vio_cmo;

/**
 * vio_cmo_OF_devices - Count the number of OF devices that have DMA windows
 */
static int vio_cmo_num_OF_devs(void)
{
	struct device_node *node_vroot;
	int count = 0;

	/*
	 * Count the number of vdevice entries with an
	 * ibm,my-dma-window OF property
	 */
	node_vroot = of_find_node_by_name(NULL, "vdevice");
	if (node_vroot) {
		struct device_node *of_node;
		struct property *prop;

		for_each_child_of_node(node_vroot, of_node) {
			prop = of_find_property(of_node, "ibm,my-dma-window",
			                       NULL);
			if (prop)
				count++;
		}
	}
	of_node_put(node_vroot);
	return count;
}

/**
 * vio_cmo_alloc - allocate IO memory for CMO-enable devices
 *
 * @viodev: VIO device requesting IO memory
 * @size: size of allocation requested
 *
 * Allocations come from memory reserved for the devices and any excess
 * IO memory available to all devices.  The spare pool used to service
 * hotplug must be equal to %VIO_CMO_MIN_ENT for the excess pool to be
 * made available.
 *
 * Return codes:
 *  0 for successful allocation and -ENOMEM for a failure
 */
static inline int vio_cmo_alloc(struct vio_dev *viodev, size_t size)
{
	unsigned long flags;
	size_t reserve_free = 0;
	size_t excess_free = 0;
	int ret = -ENOMEM;

	spin_lock_irqsave(&vio_cmo.lock, flags);

	/* Determine the amount of free entitlement available in reserve */
	if (viodev->cmo.entitled > viodev->cmo.allocated)
		reserve_free = viodev->cmo.entitled - viodev->cmo.allocated;

	/* If spare is not fulfilled, the excess pool can not be used. */
	if (vio_cmo.spare >= VIO_CMO_MIN_ENT)
		excess_free = vio_cmo.excess.free;

	/* The request can be satisfied */
	if ((reserve_free + excess_free) >= size) {
		vio_cmo.curr += size;
		if (vio_cmo.curr > vio_cmo.high)
			vio_cmo.high = vio_cmo.curr;
		viodev->cmo.allocated += size;
		size -= min(reserve_free, size);
		vio_cmo.excess.free -= size;
		ret = 0;
	}

	spin_unlock_irqrestore(&vio_cmo.lock, flags);
	return ret;
}

/**
 * vio_cmo_dealloc - deallocate IO memory from CMO-enable devices
 * @viodev: VIO device freeing IO memory
 * @size: size of deallocation
 *
 * IO memory is freed by the device back to the correct memory pools.
 * The spare pool is replenished first from either memory pool, then
 * the reserve pool is used to reduce device entitlement, the excess
 * pool is used to increase the reserve pool toward the desired entitlement
 * target, and then the remaining memory is returned to the pools.
 *
 */
static inline void vio_cmo_dealloc(struct vio_dev *viodev, size_t size)
{
	unsigned long flags;
	size_t spare_needed = 0;
	size_t excess_freed = 0;
	size_t reserve_freed = size;
	size_t tmp;
	int balance = 0;

	spin_lock_irqsave(&vio_cmo.lock, flags);
	vio_cmo.curr -= size;

	/* Amount of memory freed from the excess pool */
	if (viodev->cmo.allocated > viodev->cmo.entitled) {
		excess_freed = min(reserve_freed, (viodev->cmo.allocated -
		                                   viodev->cmo.entitled));
		reserve_freed -= excess_freed;
	}

	/* Remove allocation from device */
	viodev->cmo.allocated -= (reserve_freed + excess_freed);

	/* Spare is a subset of the reserve pool, replenish it first. */
	spare_needed = VIO_CMO_MIN_ENT - vio_cmo.spare;

	/*
	 * Replenish the spare in the reserve pool from the excess pool.
	 * This moves entitlement into the reserve pool.
	 */
	if (spare_needed && excess_freed) {
		tmp = min(excess_freed, spare_needed);
		vio_cmo.excess.size -= tmp;
		vio_cmo.reserve.size += tmp;
		vio_cmo.spare += tmp;
		excess_freed -= tmp;
		spare_needed -= tmp;
		balance = 1;
	}

	/*
	 * Replenish the spare in the reserve pool from the reserve pool.
	 * This removes entitlement from the device down to VIO_CMO_MIN_ENT,
	 * if needed, and gives it to the spare pool. The amount of used
	 * memory in this pool does not change.
	 */
	if (spare_needed && reserve_freed) {
236
		tmp = min3(spare_needed, reserve_freed, (viodev->cmo.entitled - VIO_CMO_MIN_ENT));
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 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

		vio_cmo.spare += tmp;
		viodev->cmo.entitled -= tmp;
		reserve_freed -= tmp;
		spare_needed -= tmp;
		balance = 1;
	}

	/*
	 * Increase the reserve pool until the desired allocation is met.
	 * Move an allocation freed from the excess pool into the reserve
	 * pool and schedule a balance operation.
	 */
	if (excess_freed && (vio_cmo.desired > vio_cmo.reserve.size)) {
		tmp = min(excess_freed, (vio_cmo.desired - vio_cmo.reserve.size));

		vio_cmo.excess.size -= tmp;
		vio_cmo.reserve.size += tmp;
		excess_freed -= tmp;
		balance = 1;
	}

	/* Return memory from the excess pool to that pool */
	if (excess_freed)
		vio_cmo.excess.free += excess_freed;

	if (balance)
		schedule_delayed_work(&vio_cmo.balance_q, VIO_CMO_BALANCE_DELAY);
	spin_unlock_irqrestore(&vio_cmo.lock, flags);
}

/**
 * vio_cmo_entitlement_update - Manage system entitlement changes
 *
 * @new_entitlement: new system entitlement to attempt to accommodate
 *
 * Increases in entitlement will be used to fulfill the spare entitlement
 * and the rest is given to the excess pool.  Decreases, if they are
 * possible, come from the excess pool and from unused device entitlement
 *
 * Returns: 0 on success, -ENOMEM when change can not be made
 */
int vio_cmo_entitlement_update(size_t new_entitlement)
{
	struct vio_dev *viodev;
	struct vio_cmo_dev_entry *dev_ent;
	unsigned long flags;
	size_t avail, delta, tmp;

	spin_lock_irqsave(&vio_cmo.lock, flags);

	/* Entitlement increases */
	if (new_entitlement > vio_cmo.entitled) {
		delta = new_entitlement - vio_cmo.entitled;

		/* Fulfill spare allocation */
		if (vio_cmo.spare < VIO_CMO_MIN_ENT) {
			tmp = min(delta, (VIO_CMO_MIN_ENT - vio_cmo.spare));
			vio_cmo.spare += tmp;
			vio_cmo.reserve.size += tmp;
			delta -= tmp;
		}

		/* Remaining new allocation goes to the excess pool */
		vio_cmo.entitled += delta;
		vio_cmo.excess.size += delta;
		vio_cmo.excess.free += delta;

		goto out;
	}

	/* Entitlement decreases */
	delta = vio_cmo.entitled - new_entitlement;
	avail = vio_cmo.excess.free;

	/*
	 * Need to check how much unused entitlement each device can
	 * sacrifice to fulfill entitlement change.
	 */
	list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
		if (avail >= delta)
			break;

		viodev = dev_ent->viodev;
		if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
		    (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
				avail += viodev->cmo.entitled -
				         max_t(size_t, viodev->cmo.allocated,
				               VIO_CMO_MIN_ENT);
	}

	if (delta <= avail) {
		vio_cmo.entitled -= delta;

		/* Take entitlement from the excess pool first */
		tmp = min(vio_cmo.excess.free, delta);
		vio_cmo.excess.size -= tmp;
		vio_cmo.excess.free -= tmp;
		delta -= tmp;

		/*
		 * Remove all but VIO_CMO_MIN_ENT bytes from devices
		 * until entitlement change is served
		 */
		list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
			if (!delta)
				break;

			viodev = dev_ent->viodev;
			tmp = 0;
			if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
			    (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
				tmp = viodev->cmo.entitled -
				      max_t(size_t, viodev->cmo.allocated,
				            VIO_CMO_MIN_ENT);
			viodev->cmo.entitled -= min(tmp, delta);
			delta -= min(tmp, delta);
		}
	} else {
		spin_unlock_irqrestore(&vio_cmo.lock, flags);
		return -ENOMEM;
	}

out:
	schedule_delayed_work(&vio_cmo.balance_q, 0);
	spin_unlock_irqrestore(&vio_cmo.lock, flags);
	return 0;
}

/**
 * vio_cmo_balance - Balance entitlement among devices
 *
 * @work: work queue structure for this operation
 *
 * Any system entitlement above the minimum needed for devices, or
 * already allocated to devices, can be distributed to the devices.
 * The list of devices is iterated through to recalculate the desired
 * entitlement level and to determine how much entitlement above the
 * minimum entitlement is allocated to devices.
 *
 * Small chunks of the available entitlement are given to devices until
 * their requirements are fulfilled or there is no entitlement left to give.
 * Upon completion sizes of the reserve and excess pools are calculated.
 *
 * The system minimum entitlement level is also recalculated here.
 * Entitlement will be reserved for devices even after vio_bus_remove to
 * accommodate reloading the driver.  The OF tree is walked to count the
 * number of devices present and this will remove entitlement for devices
 * that have actually left the system after having vio_bus_remove called.
 */
static void vio_cmo_balance(struct work_struct *work)
{
	struct vio_cmo *cmo;
	struct vio_dev *viodev;
	struct vio_cmo_dev_entry *dev_ent;
	unsigned long flags;
	size_t avail = 0, level, chunk, need;
	int devcount = 0, fulfilled;

	cmo = container_of(work, struct vio_cmo, balance_q.work);

	spin_lock_irqsave(&vio_cmo.lock, flags);

	/* Calculate minimum entitlement and fulfill spare */
	cmo->min = vio_cmo_num_OF_devs() * VIO_CMO_MIN_ENT;
	BUG_ON(cmo->min > cmo->entitled);
	cmo->spare = min_t(size_t, VIO_CMO_MIN_ENT, (cmo->entitled - cmo->min));
	cmo->min += cmo->spare;
	cmo->desired = cmo->min;

	/*
	 * Determine how much entitlement is available and reset device
	 * entitlements
	 */
	avail = cmo->entitled - cmo->spare;
	list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
		viodev = dev_ent->viodev;
		devcount++;
		viodev->cmo.entitled = VIO_CMO_MIN_ENT;
		cmo->desired += (viodev->cmo.desired - VIO_CMO_MIN_ENT);
		avail -= max_t(size_t, viodev->cmo.allocated, VIO_CMO_MIN_ENT);
	}

	/*
	 * Having provided each device with the minimum entitlement, loop
	 * over the devices portioning out the remaining entitlement
	 * until there is nothing left.
	 */
	level = VIO_CMO_MIN_ENT;
	while (avail) {
		fulfilled = 0;
		list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
			viodev = dev_ent->viodev;

			if (viodev->cmo.desired <= level) {
				fulfilled++;
				continue;
			}

			/*
			 * Give the device up to VIO_CMO_BALANCE_CHUNK
			 * bytes of entitlement, but do not exceed the
			 * desired level of entitlement for the device.
			 */
			chunk = min_t(size_t, avail, VIO_CMO_BALANCE_CHUNK);
			chunk = min(chunk, (viodev->cmo.desired -
			                    viodev->cmo.entitled));
			viodev->cmo.entitled += chunk;

			/*
			 * If the memory for this entitlement increase was
			 * already allocated to the device it does not come
			 * from the available pool being portioned out.
			 */
			need = max(viodev->cmo.allocated, viodev->cmo.entitled)-
			       max(viodev->cmo.allocated, level);
			avail -= need;

		}
		if (fulfilled == devcount)
			break;
		level += VIO_CMO_BALANCE_CHUNK;
	}

	/* Calculate new reserve and excess pool sizes */
	cmo->reserve.size = cmo->min;
	cmo->excess.free = 0;
	cmo->excess.size = 0;
	need = 0;
	list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
		viodev = dev_ent->viodev;
		/* Calculated reserve size above the minimum entitlement */
		if (viodev->cmo.entitled)
			cmo->reserve.size += (viodev->cmo.entitled -
			                      VIO_CMO_MIN_ENT);
		/* Calculated used excess entitlement */
		if (viodev->cmo.allocated > viodev->cmo.entitled)
			need += viodev->cmo.allocated - viodev->cmo.entitled;
	}
	cmo->excess.size = cmo->entitled - cmo->reserve.size;
	cmo->excess.free = cmo->excess.size - need;

479
	cancel_delayed_work(to_delayed_work(work));
480 481 482 483
	spin_unlock_irqrestore(&vio_cmo.lock, flags);
}

static void *vio_dma_iommu_alloc_coherent(struct device *dev, size_t size,
484
					  dma_addr_t *dma_handle, gfp_t flag,
485
					  unsigned long attrs)
486 487 488 489
{
	struct vio_dev *viodev = to_vio_dev(dev);
	void *ret;

490
	if (vio_cmo_alloc(viodev, roundup(size, PAGE_SIZE))) {
491 492 493 494
		atomic_inc(&viodev->cmo.allocs_failed);
		return NULL;
	}

495
	ret = dma_iommu_ops.alloc(dev, size, dma_handle, flag, attrs);
496
	if (unlikely(ret == NULL)) {
497
		vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
498 499 500 501 502 503 504
		atomic_inc(&viodev->cmo.allocs_failed);
	}

	return ret;
}

static void vio_dma_iommu_free_coherent(struct device *dev, size_t size,
505
					void *vaddr, dma_addr_t dma_handle,
506
					unsigned long attrs)
507 508 509
{
	struct vio_dev *viodev = to_vio_dev(dev);

510
	dma_iommu_ops.free(dev, size, vaddr, dma_handle, attrs);
511

512
	vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
513 514
}

515 516 517
static dma_addr_t vio_dma_iommu_map_page(struct device *dev, struct page *page,
                                         unsigned long offset, size_t size,
                                         enum dma_data_direction direction,
518
                                         unsigned long attrs)
519 520
{
	struct vio_dev *viodev = to_vio_dev(dev);
521
	struct iommu_table *tbl;
522
	dma_addr_t ret = IOMMU_MAPPING_ERROR;
523

524 525
	tbl = get_iommu_table_base(dev);
	if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)))) {
526 527 528 529
		atomic_inc(&viodev->cmo.allocs_failed);
		return ret;
	}

530
	ret = dma_iommu_ops.map_page(dev, page, offset, size, direction, attrs);
531
	if (unlikely(dma_mapping_error(dev, ret))) {
532
		vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
533 534 535 536 537 538
		atomic_inc(&viodev->cmo.allocs_failed);
	}

	return ret;
}

539 540 541
static void vio_dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
				     size_t size,
				     enum dma_data_direction direction,
542
				     unsigned long attrs)
543 544
{
	struct vio_dev *viodev = to_vio_dev(dev);
545
	struct iommu_table *tbl;
546

547
	tbl = get_iommu_table_base(dev);
548
	dma_iommu_ops.unmap_page(dev, dma_handle, size, direction, attrs);
549

550
	vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
551 552 553 554
}

static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
                                int nelems, enum dma_data_direction direction,
555
                                unsigned long attrs)
556 557
{
	struct vio_dev *viodev = to_vio_dev(dev);
558
	struct iommu_table *tbl;
559
	struct scatterlist *sgl;
A
Akinobu Mita 已提交
560
	int ret, count;
561 562
	size_t alloc_size = 0;

563
	tbl = get_iommu_table_base(dev);
A
Akinobu Mita 已提交
564
	for_each_sg(sglist, sgl, nelems, count)
565
		alloc_size += roundup(sgl->length, IOMMU_PAGE_SIZE(tbl));
566 567 568 569 570 571 572 573 574 575 576

	if (vio_cmo_alloc(viodev, alloc_size)) {
		atomic_inc(&viodev->cmo.allocs_failed);
		return 0;
	}

	ret = dma_iommu_ops.map_sg(dev, sglist, nelems, direction, attrs);

	if (unlikely(!ret)) {
		vio_cmo_dealloc(viodev, alloc_size);
		atomic_inc(&viodev->cmo.allocs_failed);
577
		return ret;
578 579
	}

A
Akinobu Mita 已提交
580
	for_each_sg(sglist, sgl, ret, count)
581
		alloc_size -= roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
582 583 584 585 586 587 588 589 590
	if (alloc_size)
		vio_cmo_dealloc(viodev, alloc_size);

	return ret;
}

static void vio_dma_iommu_unmap_sg(struct device *dev,
		struct scatterlist *sglist, int nelems,
		enum dma_data_direction direction,
591
		unsigned long attrs)
592 593
{
	struct vio_dev *viodev = to_vio_dev(dev);
594
	struct iommu_table *tbl;
595 596
	struct scatterlist *sgl;
	size_t alloc_size = 0;
A
Akinobu Mita 已提交
597
	int count;
598

599
	tbl = get_iommu_table_base(dev);
A
Akinobu Mita 已提交
600
	for_each_sg(sglist, sgl, nelems, count)
601
		alloc_size += roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
602 603 604 605 606 607

	dma_iommu_ops.unmap_sg(dev, sglist, nelems, direction, attrs);

	vio_cmo_dealloc(viodev, alloc_size);
}

608 609 610 611 612
static int vio_dma_iommu_dma_supported(struct device *dev, u64 mask)
{
        return dma_iommu_ops.dma_supported(dev, mask);
}

613 614 615 616 617
static u64 vio_dma_get_required_mask(struct device *dev)
{
        return dma_iommu_ops.get_required_mask(dev);
}

618
static const struct dma_map_ops vio_dma_mapping_ops = {
619 620
	.alloc             = vio_dma_iommu_alloc_coherent,
	.free              = vio_dma_iommu_free_coherent,
621
	.mmap		   = dma_direct_mmap_coherent,
622 623 624 625 626
	.map_sg            = vio_dma_iommu_map_sg,
	.unmap_sg          = vio_dma_iommu_unmap_sg,
	.map_page          = vio_dma_iommu_map_page,
	.unmap_page        = vio_dma_iommu_unmap_page,
	.dma_supported     = vio_dma_iommu_dma_supported,
627
	.get_required_mask = vio_dma_get_required_mask,
628
	.mapping_error	   = dma_iommu_mapping_error,
629 630 631 632 633 634
};

/**
 * vio_cmo_set_dev_desired - Set desired entitlement for a device
 *
 * @viodev: struct vio_dev for device to alter
W
Wanpeng Li 已提交
635
 * @desired: new desired entitlement level in bytes
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
 *
 * For use by devices to request a change to their entitlement at runtime or
 * through sysfs.  The desired entitlement level is changed and a balancing
 * of system resources is scheduled to run in the future.
 */
void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired)
{
	unsigned long flags;
	struct vio_cmo_dev_entry *dev_ent;
	int found = 0;

	if (!firmware_has_feature(FW_FEATURE_CMO))
		return;

	spin_lock_irqsave(&vio_cmo.lock, flags);
	if (desired < VIO_CMO_MIN_ENT)
		desired = VIO_CMO_MIN_ENT;

	/*
	 * Changes will not be made for devices not in the device list.
	 * If it is not in the device list, then no driver is loaded
	 * for the device and it can not receive entitlement.
	 */
	list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
		if (viodev == dev_ent->viodev) {
			found = 1;
			break;
		}
664 665
	if (!found) {
		spin_unlock_irqrestore(&vio_cmo.lock, flags);
666
		return;
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 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

	/* Increase/decrease in desired device entitlement */
	if (desired >= viodev->cmo.desired) {
		/* Just bump the bus and device values prior to a balance*/
		vio_cmo.desired += desired - viodev->cmo.desired;
		viodev->cmo.desired = desired;
	} else {
		/* Decrease bus and device values for desired entitlement */
		vio_cmo.desired -= viodev->cmo.desired - desired;
		viodev->cmo.desired = desired;
		/*
		 * If less entitlement is desired than current entitlement, move
		 * any reserve memory in the change region to the excess pool.
		 */
		if (viodev->cmo.entitled > desired) {
			vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
			vio_cmo.excess.size += viodev->cmo.entitled - desired;
			/*
			 * If entitlement moving from the reserve pool to the
			 * excess pool is currently unused, add to the excess
			 * free counter.
			 */
			if (viodev->cmo.allocated < viodev->cmo.entitled)
				vio_cmo.excess.free += viodev->cmo.entitled -
				                       max(viodev->cmo.allocated, desired);
			viodev->cmo.entitled = desired;
		}
	}
	schedule_delayed_work(&vio_cmo.balance_q, 0);
	spin_unlock_irqrestore(&vio_cmo.lock, flags);
}

/**
 * vio_cmo_bus_probe - Handle CMO specific bus probe activities
 *
 * @viodev - Pointer to struct vio_dev for device
 *
 * Determine the devices IO memory entitlement needs, attempting
 * to satisfy the system minimum entitlement at first and scheduling
 * a balance operation to take care of the rest at a later time.
 *
 * Returns: 0 on success, -EINVAL when device doesn't support CMO, and
 *          -ENOMEM when entitlement is not available for device or
 *          device entry.
 *
 */
static int vio_cmo_bus_probe(struct vio_dev *viodev)
{
	struct vio_cmo_dev_entry *dev_ent;
	struct device *dev = &viodev->dev;
718
	struct iommu_table *tbl;
719 720 721
	struct vio_driver *viodrv = to_vio_driver(dev->driver);
	unsigned long flags;
	size_t size;
722 723
	bool dma_capable = false;

724 725
	tbl = get_iommu_table_base(dev);

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	/* A device requires entitlement if it has a DMA window property */
	switch (viodev->family) {
	case VDEVICE:
		if (of_get_property(viodev->dev.of_node,
					"ibm,my-dma-window", NULL))
			dma_capable = true;
		break;
	case PFO:
		dma_capable = false;
		break;
	default:
		dev_warn(dev, "unknown device family: %d\n", viodev->family);
		BUG();
		break;
	}
741

742 743
	/* Configure entitlement for the device. */
	if (dma_capable) {
744 745 746 747 748 749 750
		/* Check that the driver is CMO enabled and get desired DMA */
		if (!viodrv->get_desired_dma) {
			dev_err(dev, "%s: device driver does not support CMO\n",
			        __func__);
			return -EINVAL;
		}

751
		viodev->cmo.desired =
752
			IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev), tbl);
753 754 755 756 757 758 759 760 761 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 839 840 841 842 843 844 845 846 847 848 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
		if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
			viodev->cmo.desired = VIO_CMO_MIN_ENT;
		size = VIO_CMO_MIN_ENT;

		dev_ent = kmalloc(sizeof(struct vio_cmo_dev_entry),
		                  GFP_KERNEL);
		if (!dev_ent)
			return -ENOMEM;

		dev_ent->viodev = viodev;
		spin_lock_irqsave(&vio_cmo.lock, flags);
		list_add(&dev_ent->list, &vio_cmo.device_list);
	} else {
		viodev->cmo.desired = 0;
		size = 0;
		spin_lock_irqsave(&vio_cmo.lock, flags);
	}

	/*
	 * If the needs for vio_cmo.min have not changed since they
	 * were last set, the number of devices in the OF tree has
	 * been constant and the IO memory for this is already in
	 * the reserve pool.
	 */
	if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
	                    VIO_CMO_MIN_ENT)) {
		/* Updated desired entitlement if device requires it */
		if (size)
			vio_cmo.desired += (viodev->cmo.desired -
		                        VIO_CMO_MIN_ENT);
	} else {
		size_t tmp;

		tmp = vio_cmo.spare + vio_cmo.excess.free;
		if (tmp < size) {
			dev_err(dev, "%s: insufficient free "
			        "entitlement to add device. "
			        "Need %lu, have %lu\n", __func__,
				size, (vio_cmo.spare + tmp));
			spin_unlock_irqrestore(&vio_cmo.lock, flags);
			return -ENOMEM;
		}

		/* Use excess pool first to fulfill request */
		tmp = min(size, vio_cmo.excess.free);
		vio_cmo.excess.free -= tmp;
		vio_cmo.excess.size -= tmp;
		vio_cmo.reserve.size += tmp;

		/* Use spare if excess pool was insufficient */
		vio_cmo.spare -= size - tmp;

		/* Update bus accounting */
		vio_cmo.min += size;
		vio_cmo.desired += viodev->cmo.desired;
	}
	spin_unlock_irqrestore(&vio_cmo.lock, flags);
	return 0;
}

/**
 * vio_cmo_bus_remove - Handle CMO specific bus removal activities
 *
 * @viodev - Pointer to struct vio_dev for device
 *
 * Remove the device from the cmo device list.  The minimum entitlement
 * will be reserved for the device as long as it is in the system.  The
 * rest of the entitlement the device had been allocated will be returned
 * to the system.
 */
static void vio_cmo_bus_remove(struct vio_dev *viodev)
{
	struct vio_cmo_dev_entry *dev_ent;
	unsigned long flags;
	size_t tmp;

	spin_lock_irqsave(&vio_cmo.lock, flags);
	if (viodev->cmo.allocated) {
		dev_err(&viodev->dev, "%s: device had %lu bytes of IO "
		        "allocated after remove operation.\n",
		        __func__, viodev->cmo.allocated);
		BUG();
	}

	/*
	 * Remove the device from the device list being maintained for
	 * CMO enabled devices.
	 */
	list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
		if (viodev == dev_ent->viodev) {
			list_del(&dev_ent->list);
			kfree(dev_ent);
			break;
		}

	/*
	 * Devices may not require any entitlement and they do not need
	 * to be processed.  Otherwise, return the device's entitlement
	 * back to the pools.
	 */
	if (viodev->cmo.entitled) {
		/*
		 * This device has not yet left the OF tree, it's
		 * minimum entitlement remains in vio_cmo.min and
		 * vio_cmo.desired
		 */
		vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);

		/*
		 * Save min allocation for device in reserve as long
		 * as it exists in OF tree as determined by later
		 * balance operation
		 */
		viodev->cmo.entitled -= VIO_CMO_MIN_ENT;

		/* Replenish spare from freed reserve pool */
		if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
			tmp = min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
			                                 vio_cmo.spare));
			vio_cmo.spare += tmp;
			viodev->cmo.entitled -= tmp;
		}

		/* Remaining reserve goes to excess pool */
		vio_cmo.excess.size += viodev->cmo.entitled;
		vio_cmo.excess.free += viodev->cmo.entitled;
		vio_cmo.reserve.size -= viodev->cmo.entitled;

		/*
		 * Until the device is removed it will keep a
		 * minimum entitlement; this will guarantee that
		 * a module unload/load will result in a success.
		 */
		viodev->cmo.entitled = VIO_CMO_MIN_ENT;
		viodev->cmo.desired = VIO_CMO_MIN_ENT;
		atomic_set(&viodev->cmo.allocs_failed, 0);
	}

	spin_unlock_irqrestore(&vio_cmo.lock, flags);
}

static void vio_cmo_set_dma_ops(struct vio_dev *viodev)
{
896
	set_dma_ops(&viodev->dev, &vio_dma_mapping_ops);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
}

/**
 * vio_cmo_bus_init - CMO entitlement initialization at bus init time
 *
 * Set up the reserve and excess entitlement pools based on available
 * system entitlement and the number of devices in the OF tree that
 * require entitlement in the reserve pool.
 */
static void vio_cmo_bus_init(void)
{
	struct hvcall_mpp_data mpp_data;
	int err;

	memset(&vio_cmo, 0, sizeof(struct vio_cmo));
	spin_lock_init(&vio_cmo.lock);
	INIT_LIST_HEAD(&vio_cmo.device_list);
	INIT_DELAYED_WORK(&vio_cmo.balance_q, vio_cmo_balance);

	/* Get current system entitlement */
	err = h_get_mpp(&mpp_data);

	/*
	 * On failure, continue with entitlement set to 0, will panic()
	 * later when spare is reserved.
	 */
	if (err != H_SUCCESS) {
		printk(KERN_ERR "%s: unable to determine system IO "\
		       "entitlement. (%d)\n", __func__, err);
		vio_cmo.entitled = 0;
	} else {
		vio_cmo.entitled = mpp_data.entitled_mem;
	}

	/* Set reservation and check against entitlement */
	vio_cmo.spare = VIO_CMO_MIN_ENT;
	vio_cmo.reserve.size = vio_cmo.spare;
	vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
	                         VIO_CMO_MIN_ENT);
	if (vio_cmo.reserve.size > vio_cmo.entitled) {
		printk(KERN_ERR "%s: insufficient system entitlement\n",
		       __func__);
		panic("%s: Insufficient system entitlement", __func__);
	}

	/* Set the remaining accounting variables */
	vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
	vio_cmo.excess.free = vio_cmo.excess.size;
	vio_cmo.min = vio_cmo.reserve.size;
	vio_cmo.desired = vio_cmo.reserve.size;
}

/* sysfs device functions and data structures for CMO */

#define viodev_cmo_rd_attr(name)                                        \
952
static ssize_t cmo_##name##_show(struct device *dev,                    \
953 954 955 956 957 958
                                        struct device_attribute *attr,  \
                                         char *buf)                     \
{                                                                       \
	return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name);        \
}

959
static ssize_t cmo_allocs_failed_show(struct device *dev,
960 961 962 963 964 965
		struct device_attribute *attr, char *buf)
{
	struct vio_dev *viodev = to_vio_dev(dev);
	return sprintf(buf, "%d\n", atomic_read(&viodev->cmo.allocs_failed));
}

966
static ssize_t cmo_allocs_failed_store(struct device *dev,
967 968 969 970 971 972 973
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct vio_dev *viodev = to_vio_dev(dev);
	atomic_set(&viodev->cmo.allocs_failed, 0);
	return count;
}

974
static ssize_t cmo_desired_store(struct device *dev,
975 976 977 978 979 980
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct vio_dev *viodev = to_vio_dev(dev);
	size_t new_desired;
	int ret;

981
	ret = kstrtoul(buf, 10, &new_desired);
982 983 984 985 986 987 988 989 990 991 992 993 994
	if (ret)
		return ret;

	vio_cmo_set_dev_desired(viodev, new_desired);
	return count;
}

viodev_cmo_rd_attr(desired);
viodev_cmo_rd_attr(entitled);
viodev_cmo_rd_attr(allocated);

static ssize_t name_show(struct device *, struct device_attribute *, char *);
static ssize_t devspec_show(struct device *, struct device_attribute *, char *);
995 996
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
			     char *buf);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

static struct device_attribute dev_attr_name;
static struct device_attribute dev_attr_devspec;
static struct device_attribute dev_attr_modalias;

static DEVICE_ATTR_RO(cmo_entitled);
static DEVICE_ATTR_RO(cmo_allocated);
static DEVICE_ATTR_RW(cmo_desired);
static DEVICE_ATTR_RW(cmo_allocs_failed);

static struct attribute *vio_cmo_dev_attrs[] = {
	&dev_attr_name.attr,
	&dev_attr_devspec.attr,
	&dev_attr_modalias.attr,
	&dev_attr_cmo_entitled.attr,
	&dev_attr_cmo_allocated.attr,
	&dev_attr_cmo_desired.attr,
	&dev_attr_cmo_allocs_failed.attr,
	NULL,
1016
};
1017
ATTRIBUTE_GROUPS(vio_cmo_dev);
1018 1019 1020 1021

/* sysfs bus functions and data structures for CMO */

#define viobus_cmo_rd_attr(name)                                        \
1022
static ssize_t cmo_bus_##name##_show(struct bus_type *bt, char *buf)    \
1023 1024
{                                                                       \
	return sprintf(buf, "%lu\n", vio_cmo.name);                     \
1025
}                                                                       \
1026 1027
static struct bus_attribute bus_attr_cmo_bus_##name =			\
	__ATTR(cmo_##name, S_IRUGO, cmo_bus_##name##_show, NULL)
1028 1029 1030

#define viobus_cmo_pool_rd_attr(name, var)                              \
static ssize_t                                                          \
1031
cmo_##name##_##var##_show(struct bus_type *bt, char *buf)               \
1032 1033
{                                                                       \
	return sprintf(buf, "%lu\n", vio_cmo.name.var);                 \
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
}                                                                       \
static BUS_ATTR_RO(cmo_##name##_##var)

viobus_cmo_rd_attr(entitled);
viobus_cmo_rd_attr(spare);
viobus_cmo_rd_attr(min);
viobus_cmo_rd_attr(desired);
viobus_cmo_rd_attr(curr);
viobus_cmo_pool_rd_attr(reserve, size);
viobus_cmo_pool_rd_attr(excess, size);
viobus_cmo_pool_rd_attr(excess, free);

static ssize_t cmo_high_show(struct bus_type *bt, char *buf)
{
	return sprintf(buf, "%lu\n", vio_cmo.high);
1049 1050
}

1051 1052
static ssize_t cmo_high_store(struct bus_type *bt, const char *buf,
			      size_t count)
1053 1054 1055 1056 1057 1058 1059 1060 1061
{
	unsigned long flags;

	spin_lock_irqsave(&vio_cmo.lock, flags);
	vio_cmo.high = vio_cmo.curr;
	spin_unlock_irqrestore(&vio_cmo.lock, flags);

	return count;
}
1062 1063 1064
static BUS_ATTR_RW(cmo_high);

static struct attribute *vio_bus_attrs[] = {
1065 1066 1067 1068 1069
	&bus_attr_cmo_bus_entitled.attr,
	&bus_attr_cmo_bus_spare.attr,
	&bus_attr_cmo_bus_min.attr,
	&bus_attr_cmo_bus_desired.attr,
	&bus_attr_cmo_bus_curr.attr,
1070 1071 1072 1073 1074
	&bus_attr_cmo_high.attr,
	&bus_attr_cmo_reserve_size.attr,
	&bus_attr_cmo_excess_size.attr,
	&bus_attr_cmo_excess_free.attr,
	NULL,
1075
};
1076
ATTRIBUTE_GROUPS(vio_bus);
1077 1078 1079

static void vio_cmo_sysfs_init(void)
{
1080
	vio_bus_type.dev_groups = vio_cmo_dev_groups;
1081
	vio_bus_type.bus_groups = vio_bus_groups;
1082 1083 1084 1085 1086 1087 1088
}
#else /* CONFIG_PPC_SMLPAR */
int vio_cmo_entitlement_update(size_t new_entitlement) { return 0; }
void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired) {}
static int vio_cmo_bus_probe(struct vio_dev *viodev) { return 0; }
static void vio_cmo_bus_remove(struct vio_dev *viodev) {}
static void vio_cmo_set_dma_ops(struct vio_dev *viodev) {}
N
Nathan Lynch 已提交
1089 1090
static void vio_cmo_bus_init(void) {}
static void vio_cmo_sysfs_init(void) { }
1091 1092 1093 1094
#endif /* CONFIG_PPC_SMLPAR */
EXPORT_SYMBOL(vio_cmo_entitlement_update);
EXPORT_SYMBOL(vio_cmo_set_dev_desired);

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

/*
 * Platform Facilities Option (PFO) support
 */

/**
 * vio_h_cop_sync - Perform a synchronous PFO co-processor operation
 *
 * @vdev - Pointer to a struct vio_dev for device
 * @op - Pointer to a struct vio_pfo_op for the operation parameters
 *
 * Calls the hypervisor to synchronously perform the PFO operation
 * described in @op.  In the case of a busy response from the hypervisor,
 * the operation will be re-submitted indefinitely unless a non-zero timeout
 * is specified or an error occurs. The timeout places a limit on when to
 * stop re-submitting a operation, the total time can be exceeded if an
 * operation is in progress.
 *
 * If op->hcall_ret is not NULL, this will be set to the return from the
 * last h_cop_op call or it will be 0 if an error not involving the h_call
 * was encountered.
 *
 * Returns:
 *	0 on success,
 *	-EINVAL if the h_call fails due to an invalid parameter,
 *	-E2BIG if the h_call can not be performed synchronously,
 *	-EBUSY if a timeout is specified and has elapsed,
 *	-EACCES if the memory area for data/status has been rescinded, or
 *	-EPERM if a hardware fault has been indicated
 */
int vio_h_cop_sync(struct vio_dev *vdev, struct vio_pfo_op *op)
{
	struct device *dev = &vdev->dev;
	unsigned long deadline = 0;
	long hret = 0;
	int ret = 0;

	if (op->timeout)
		deadline = jiffies + msecs_to_jiffies(op->timeout);

	while (true) {
		hret = plpar_hcall_norets(H_COP, op->flags,
				vdev->resource_id,
				op->in, op->inlen, op->out,
				op->outlen, op->csbcpb);

		if (hret == H_SUCCESS ||
		    (hret != H_NOT_ENOUGH_RESOURCES &&
		     hret != H_BUSY && hret != H_RESOURCE) ||
		    (op->timeout && time_after(deadline, jiffies)))
			break;

		dev_dbg(dev, "%s: hcall ret(%ld), retrying.\n", __func__, hret);
	}

	switch (hret) {
	case H_SUCCESS:
		ret = 0;
		break;
	case H_OP_MODE:
	case H_TOO_BIG:
		ret = -E2BIG;
		break;
	case H_RESCINDED:
		ret = -EACCES;
		break;
	case H_HARDWARE:
		ret = -EPERM;
		break;
	case H_NOT_ENOUGH_RESOURCES:
	case H_RESOURCE:
	case H_BUSY:
		ret = -EBUSY;
		break;
	default:
		ret = -EINVAL;
		break;
	}

	if (ret)
		dev_dbg(dev, "%s: Sync h_cop_op failure (ret:%d) (hret:%ld)\n",
				__func__, ret, hret);

	op->hcall_err = hret;
	return ret;
}
EXPORT_SYMBOL(vio_h_cop_sync);

1183 1184
static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
{
1185
	const __be32 *dma_window;
1186 1187 1188
	struct iommu_table *tbl;
	unsigned long offset, size;

1189
	dma_window = of_get_property(dev->dev.of_node,
1190 1191 1192
				  "ibm,my-dma-window", NULL);
	if (!dma_window)
		return NULL;
1193

1194
	tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
1195 1196
	if (tbl == NULL)
		return NULL;
1197

1198
	of_parse_dma_window(dev->dev.of_node, dma_window,
1199
			    &tbl->it_index, &offset, &size);
1200

1201
	/* TCE table size - measured in tce entries */
1202 1203
	tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
	tbl->it_size = size >> tbl->it_page_shift;
1204
	/* offset for VIO should always be 0 */
1205
	tbl->it_offset = offset >> tbl->it_page_shift;
1206 1207
	tbl->it_busno = 0;
	tbl->it_type = TCE_VB;
1208
	tbl->it_blocksize = 16;
1209

1210 1211 1212 1213 1214
	if (firmware_has_feature(FW_FEATURE_LPAR))
		tbl->it_ops = &iommu_table_lpar_multi_ops;
	else
		tbl->it_ops = &iommu_table_pseries_ops;

1215
	return iommu_init_table(tbl, -1);
1216
}
L
Linus Torvalds 已提交
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/**
 * vio_match_device: - Tell if a VIO device has a matching
 *			VIO device id structure.
 * @ids:	array of VIO device id structures to search in
 * @dev:	the VIO device structure to match against
 *
 * Used by a driver to check whether a VIO device present in the
 * system is in its list of supported devices. Returns the matching
 * vio_device_id structure or NULL if there is no match.
 */
static const struct vio_device_id *vio_match_device(
		const struct vio_device_id *ids, const struct vio_dev *dev)
{
	while (ids->type[0] != '\0') {
1232
		if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
1233
		    of_device_is_compatible(dev->dev.of_node,
1234
					 ids->compat))
1235 1236 1237 1238 1239 1240
			return ids;
		ids++;
	}
	return NULL;
}

1241 1242
/*
 * Convert from struct device to struct vio_dev and pass to driver.
L
Linus Torvalds 已提交
1243
 * dev->driver has already been set by generic code because vio_bus_match
1244 1245
 * succeeded.
 */
L
Linus Torvalds 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
static int vio_bus_probe(struct device *dev)
{
	struct vio_dev *viodev = to_vio_dev(dev);
	struct vio_driver *viodrv = to_vio_driver(dev->driver);
	const struct vio_device_id *id;
	int error = -ENODEV;

	if (!viodrv->probe)
		return error;

	id = vio_match_device(viodrv->id_table, viodev);
1257 1258 1259 1260 1261 1262 1263
	if (id) {
		memset(&viodev->cmo, 0, sizeof(viodev->cmo));
		if (firmware_has_feature(FW_FEATURE_CMO)) {
			error = vio_cmo_bus_probe(viodev);
			if (error)
				return error;
		}
L
Linus Torvalds 已提交
1264
		error = viodrv->probe(viodev, id);
1265
		if (error && firmware_has_feature(FW_FEATURE_CMO))
1266 1267
			vio_cmo_bus_remove(viodev);
	}
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276

	return error;
}

/* convert from struct device to struct vio_dev and pass to driver. */
static int vio_bus_remove(struct device *dev)
{
	struct vio_dev *viodev = to_vio_dev(dev);
	struct vio_driver *viodrv = to_vio_driver(dev->driver);
1277 1278 1279 1280 1281 1282 1283 1284
	struct device *devptr;
	int ret = 1;

	/*
	 * Hold a reference to the device after the remove function is called
	 * to allow for CMO accounting cleanup for the device.
	 */
	devptr = get_device(dev);
L
Linus Torvalds 已提交
1285

1286
	if (viodrv->remove)
1287
		ret = viodrv->remove(viodev);
L
Linus Torvalds 已提交
1288

1289 1290 1291 1292 1293
	if (!ret && firmware_has_feature(FW_FEATURE_CMO))
		vio_cmo_bus_remove(viodev);

	put_device(devptr);
	return ret;
L
Linus Torvalds 已提交
1294 1295 1296 1297
}

/**
 * vio_register_driver: - Register a new vio driver
W
Wanpeng Li 已提交
1298
 * @viodrv:	The vio_driver structure to be registered.
L
Linus Torvalds 已提交
1299
 */
1300 1301
int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
			  const char *mod_name)
L
Linus Torvalds 已提交
1302
{
1303
	pr_debug("%s: driver %s registering\n", __func__, viodrv->name);
L
Linus Torvalds 已提交
1304 1305

	/* fill in 'struct driver' fields */
1306 1307
	viodrv->driver.name = viodrv->name;
	viodrv->driver.pm = viodrv->pm;
L
Linus Torvalds 已提交
1308
	viodrv->driver.bus = &vio_bus_type;
1309 1310
	viodrv->driver.owner = owner;
	viodrv->driver.mod_name = mod_name;
L
Linus Torvalds 已提交
1311 1312 1313

	return driver_register(&viodrv->driver);
}
1314
EXPORT_SYMBOL(__vio_register_driver);
L
Linus Torvalds 已提交
1315 1316 1317

/**
 * vio_unregister_driver - Remove registration of vio driver.
W
Wanpeng Li 已提交
1318
 * @viodrv:	The vio_driver struct to be removed form registration
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324 1325
 */
void vio_unregister_driver(struct vio_driver *viodrv)
{
	driver_unregister(&viodrv->driver);
}
EXPORT_SYMBOL(vio_unregister_driver);

1326
/* vio_dev refcount hit 0 */
1327
static void vio_dev_release(struct device *dev)
1328
{
1329 1330
	struct iommu_table *tbl = get_iommu_table_base(dev);

S
Stephen Rothwell 已提交
1331
	if (tbl)
1332
		iommu_tce_table_put(tbl);
1333
	of_node_put(dev->of_node);
1334 1335 1336
	kfree(to_vio_dev(dev));
}

L
Linus Torvalds 已提交
1337
/**
1338 1339
 * vio_register_device_node: - Register a new vio device.
 * @of_node:	The OF node for this device.
L
Linus Torvalds 已提交
1340
 *
1341
 * Creates and initializes a vio_dev structure from the data in
1342
 * of_node and adds it to the list of virtual devices.
1343 1344
 * Returns a pointer to the created vio_dev or NULL if node has
 * NULL device_type or compatible fields.
L
Linus Torvalds 已提交
1345
 */
1346
struct vio_dev *vio_register_device_node(struct device_node *of_node)
L
Linus Torvalds 已提交
1347
{
1348
	struct vio_dev *viodev;
1349
	struct device_node *parent_node;
1350
	const __be32 *prop;
1351 1352
	enum vio_dev_family family;
	const char *of_node_name = of_node->name ? of_node->name : "<unknown>";
1353

1354 1355 1356 1357 1358 1359
	/*
	 * Determine if this node is a under the /vdevice node or under the
	 * /ibm,platform-facilities node.  This decides the device's family.
	 */
	parent_node = of_get_parent(of_node);
	if (parent_node) {
1360
		if (!strcmp(parent_node->type, "ibm,platform-facilities"))
1361
			family = PFO;
1362
		else if (!strcmp(parent_node->type, "vdevice"))
1363 1364
			family = VDEVICE;
		else {
1365
			pr_warn("%s: parent(%pOF) of %s not recognized.\n",
1366
					__func__,
1367
					parent_node,
1368 1369 1370 1371 1372 1373 1374 1375
					of_node_name);
			of_node_put(parent_node);
			return NULL;
		}
		of_node_put(parent_node);
	} else {
		pr_warn("%s: could not determine the parent of node %s.\n",
				__func__, of_node_name);
1376
		return NULL;
L
Linus Torvalds 已提交
1377
	}
1378

1379 1380 1381 1382 1383 1384
	if (family == PFO) {
		if (of_get_property(of_node, "interrupt-controller", NULL)) {
			pr_debug("%s: Skipping the interrupt controller %s.\n",
					__func__, of_node_name);
			return NULL;
		}
1385 1386 1387 1388
	}

	/* allocate a vio_dev for this node */
	viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
1389 1390
	if (viodev == NULL) {
		pr_warn("%s: allocation failure for VIO device.\n", __func__);
1391
		return NULL;
1392
	}
1393

1394 1395 1396
	/* we need the 'device_type' property, in order to match with drivers */
	viodev->family = family;
	if (viodev->family == VDEVICE) {
1397 1398
		unsigned int unit_address;

1399 1400 1401 1402 1403 1404 1405 1406
		if (of_node->type != NULL)
			viodev->type = of_node->type;
		else {
			pr_warn("%s: node %s is missing the 'device_type' "
					"property.\n", __func__, of_node_name);
			goto out;
		}

1407 1408
		prop = of_get_property(of_node, "reg", NULL);
		if (prop == NULL) {
1409 1410 1411 1412
			pr_warn("%s: node %s missing 'reg'\n",
					__func__, of_node_name);
			goto out;
		}
1413 1414
		unit_address = of_read_number(prop, 1);
		dev_set_name(&viodev->dev, "%x", unit_address);
1415
		viodev->irq = irq_of_parse_and_map(of_node, 0);
1416
		viodev->unit_address = unit_address;
1417 1418 1419 1420
	} else {
		/* PFO devices need their resource_id for submitting COP_OPs
		 * This is an optional field for devices, but is required when
		 * performing synchronous ops */
1421 1422 1423
		prop = of_get_property(of_node, "ibm,resource-id", NULL);
		if (prop != NULL)
			viodev->resource_id = of_read_number(prop, 1);
1424 1425 1426 1427 1428

		dev_set_name(&viodev->dev, "%s", of_node_name);
		viodev->type = of_node_name;
		viodev->irq = 0;
	}
1429 1430

	viodev->name = of_node->name;
1431
	viodev->dev.of_node = of_node_get(of_node);
1432

B
Becky Bruce 已提交
1433
	set_dev_node(&viodev->dev, of_node_to_nid(of_node));
1434 1435 1436 1437 1438

	/* init generic 'struct device' fields: */
	viodev->dev.parent = &vio_bus_device.dev;
	viodev->dev.bus = &vio_bus_type;
	viodev->dev.release = vio_dev_release;
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450

	if (of_get_property(viodev->dev.of_node, "ibm,my-dma-window", NULL)) {
		if (firmware_has_feature(FW_FEATURE_CMO))
			vio_cmo_set_dma_ops(viodev);
		else
			set_dma_ops(&viodev->dev, &dma_iommu_ops);

		set_iommu_table_base(&viodev->dev,
				     vio_build_iommu_table(viodev));

		/* needed to ensure proper operation of coherent allocations
		 * later, in case driver doesn't set it explicitly */
1451 1452
		viodev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
		viodev->dev.dma_mask = &viodev->dev.coherent_dma_mask;
1453
	}
1454 1455

	/* register with generic device framework */
1456 1457
	if (device_register(&viodev->dev)) {
		printk(KERN_ERR "%s: failed to register device %s\n",
1458
				__func__, dev_name(&viodev->dev));
1459
		put_device(&viodev->dev);
1460 1461 1462 1463
		return NULL;
	}

	return viodev;
1464 1465 1466 1467 1468

out:	/* Use this exit point for any return prior to device_register */
	kfree(viodev);

	return NULL;
L
Linus Torvalds 已提交
1469
}
1470
EXPORT_SYMBOL(vio_register_device_node);
L
Linus Torvalds 已提交
1471

1472 1473 1474 1475 1476 1477 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
/*
 * vio_bus_scan_for_devices - Scan OF and register each child device
 * @root_name - OF node name for the root of the subtree to search.
 *		This must be non-NULL
 *
 * Starting from the root node provide, register the device node for
 * each child beneath the root.
 */
static void vio_bus_scan_register_devices(char *root_name)
{
	struct device_node *node_root, *node_child;

	if (!root_name)
		return;

	node_root = of_find_node_by_name(NULL, root_name);
	if (node_root) {

		/*
		 * Create struct vio_devices for each virtual device in
		 * the device tree. Drivers will associate with them later.
		 */
		node_child = of_get_next_child(node_root, NULL);
		while (node_child) {
			vio_register_device_node(node_child);
			node_child = of_get_next_child(node_root, node_child);
		}
		of_node_put(node_root);
	}
}

L
Linus Torvalds 已提交
1503 1504 1505
/**
 * vio_bus_init: - Initialize the virtual IO bus
 */
1506
static int __init vio_bus_init(void)
L
Linus Torvalds 已提交
1507 1508 1509
{
	int err;

1510 1511 1512
	if (firmware_has_feature(FW_FEATURE_CMO))
		vio_cmo_sysfs_init();

L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518
	err = bus_register(&vio_bus_type);
	if (err) {
		printk(KERN_ERR "failed to register VIO bus\n");
		return err;
	}

1519 1520
	/*
	 * The fake parent of all vio devices, just to give us
1521 1522
	 * a nice directory
	 */
1523
	err = device_register(&vio_bus_device.dev);
L
Linus Torvalds 已提交
1524
	if (err) {
1525
		printk(KERN_WARNING "%s: device_register returned %i\n",
1526
				__func__, err);
L
Linus Torvalds 已提交
1527 1528 1529
		return err;
	}

1530 1531 1532
	if (firmware_has_feature(FW_FEATURE_CMO))
		vio_cmo_bus_init();

1533 1534 1535 1536 1537 1538
	return 0;
}
postcore_initcall(vio_bus_init);

static int __init vio_device_init(void)
{
1539 1540
	vio_bus_scan_register_devices("vdevice");
	vio_bus_scan_register_devices("ibm,platform-facilities");
1541

1542 1543
	return 0;
}
1544
device_initcall(vio_device_init);
L
Linus Torvalds 已提交
1545

1546
static ssize_t name_show(struct device *dev,
1547
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1548 1549 1550
{
	return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
}
1551
static DEVICE_ATTR_RO(name);
1552 1553 1554 1555

static ssize_t devspec_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
1556
	struct device_node *of_node = dev->of_node;
1557

1558
	return sprintf(buf, "%pOF\n", of_node);
1559
}
1560
static DEVICE_ATTR_RO(devspec);
1561

1562 1563 1564 1565 1566 1567 1568
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
	const struct vio_dev *vio_dev = to_vio_dev(dev);
	struct device_node *dn;
	const char *cp;

1569
	dn = dev->of_node;
1570
	if (!dn) {
1571
		strcpy(buf, "\n");
1572 1573
		return strlen(buf);
	}
1574
	cp = of_get_property(dn, "compatible", NULL);
1575
	if (!cp) {
1576
		strcpy(buf, "\n");
1577 1578
		return strlen(buf);
	}
1579 1580 1581

	return sprintf(buf, "vio:T%sS%s\n", vio_dev->type, cp);
}
1582
static DEVICE_ATTR_RO(modalias);
1583

1584 1585 1586 1587 1588
static struct attribute *vio_dev_attrs[] = {
	&dev_attr_name.attr,
	&dev_attr_devspec.attr,
	&dev_attr_modalias.attr,
	NULL,
1589
};
1590
ATTRIBUTE_GROUPS(vio_dev);
L
Linus Torvalds 已提交
1591

1592
void vio_unregister_device(struct vio_dev *viodev)
L
Linus Torvalds 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
{
	device_unregister(&viodev->dev);
}
EXPORT_SYMBOL(vio_unregister_device);

static int vio_bus_match(struct device *dev, struct device_driver *drv)
{
	const struct vio_dev *vio_dev = to_vio_dev(dev);
	struct vio_driver *vio_drv = to_vio_driver(drv);
	const struct vio_device_id *ids = vio_drv->id_table;

1604
	return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
L
Linus Torvalds 已提交
1605 1606
}

1607
static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
1608 1609
{
	const struct vio_dev *vio_dev = to_vio_dev(dev);
1610
	struct device_node *dn;
1611
	const char *cp;
1612

1613
	dn = dev->of_node;
1614
	if (!dn)
1615
		return -ENODEV;
1616
	cp = of_get_property(dn, "compatible", NULL);
1617 1618 1619
	if (!cp)
		return -ENODEV;

1620
	add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
1621 1622 1623
	return 0;
}

1624
struct bus_type vio_bus_type = {
L
Linus Torvalds 已提交
1625
	.name = "vio",
1626
	.dev_groups = vio_dev_groups,
1627
	.uevent = vio_hotplug,
L
Linus Torvalds 已提交
1628
	.match = vio_bus_match,
1629 1630
	.probe = vio_bus_probe,
	.remove = vio_bus_remove,
L
Linus Torvalds 已提交
1631
};
1632 1633 1634 1635 1636 1637 1638

/**
 * vio_get_attribute: - get attribute for virtual device
 * @vdev:	The vio device to get property.
 * @which:	The property/attribute to be extracted.
 * @length:	Pointer to length of returned data size (unused if NULL).
 *
1639
 * Calls prom.c's of_get_property() to return the value of the
1640 1641 1642 1643
 * attribute specified by @which
*/
const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
{
1644
	return of_get_property(vdev->dev.of_node, which, length);
1645 1646
}
EXPORT_SYMBOL(vio_get_attribute);
1647 1648 1649 1650 1651

#ifdef CONFIG_PPC_PSERIES
/* vio_find_name() - internal because only vio.c knows how we formatted the
 * kobject name
 */
1652
static struct vio_dev *vio_find_name(const char *name)
1653
{
1654
	struct device *found;
1655

1656
	found = bus_find_device_by_name(&vio_bus_type, NULL, name);
1657 1658 1659
	if (!found)
		return NULL;

1660
	return to_vio_dev(found);
1661 1662 1663 1664 1665
}

/**
 * vio_find_node - find an already-registered vio_dev
 * @vnode: device_node of the virtual device we're looking for
1666 1667 1668
 *
 * Takes a reference to the embedded struct device which needs to be dropped
 * after use.
1669 1670 1671
 */
struct vio_dev *vio_find_node(struct device_node *vnode)
{
1672
	char kobj_name[20];
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
	struct device_node *vnode_parent;
	const char *dev_type;

	vnode_parent = of_get_parent(vnode);
	if (!vnode_parent)
		return NULL;

	dev_type = of_get_property(vnode_parent, "device_type", NULL);
	of_node_put(vnode_parent);
	if (!dev_type)
		return NULL;
1684 1685

	/* construct the kobject name from the device node */
1686
	if (!strcmp(dev_type, "vdevice")) {
1687 1688 1689 1690
		const __be32 *prop;
		
		prop = of_get_property(vnode, "reg", NULL);
		if (!prop)
1691
			return NULL;
1692 1693
		snprintf(kobj_name, sizeof(kobj_name), "%x",
			 (uint32_t)of_read_number(prop, 1));
1694 1695 1696
	} else if (!strcmp(dev_type, "ibm,platform-facilities"))
		snprintf(kobj_name, sizeof(kobj_name), "%s", vnode->name);
	else
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
		return NULL;

	return vio_find_name(kobj_name);
}
EXPORT_SYMBOL(vio_find_node);

int vio_enable_interrupts(struct vio_dev *dev)
{
	int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
	if (rc != H_SUCCESS)
		printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
	return rc;
}
EXPORT_SYMBOL(vio_enable_interrupts);

int vio_disable_interrupts(struct vio_dev *dev)
{
	int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
	if (rc != H_SUCCESS)
		printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
	return rc;
}
EXPORT_SYMBOL(vio_disable_interrupts);
#endif /* CONFIG_PPC_PSERIES */