system-bus.c 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *  PS3 system bus driver.
 *
 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
 *  Copyright 2006 Sony 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; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>

#include <asm/udbg.h>
#include <asm/lv1call.h>
29
#include <asm/firmware.h>
30
#include <asm/iommu.h>
31

32 33
#include "platform.h"

G
Geoff Levand 已提交
34
static struct device ps3_system_bus = {
35
	.init_name = "ps3_system",
G
Geoff Levand 已提交
36 37 38 39 40 41 42 43 44 45
};

/* FIXME: need device usage counters! */
struct {
	struct mutex mutex;
	int sb_11; /* usb 0 */
	int sb_12; /* usb 0 */
	int gpu;
} static usage_hack;

46 47
static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
			 u64 dev_id)
G
Geoff Levand 已提交
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 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
{
	return dev->bus_id == bus_id && dev->dev_id == dev_id;
}

static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
{
	int result;

	BUG_ON(!dev->bus_id);
	mutex_lock(&usage_hack.mutex);

	if (ps3_is_device(dev, 1, 1)) {
		usage_hack.sb_11++;
		if (usage_hack.sb_11 > 1) {
			result = 0;
			goto done;
		}
	}

	if (ps3_is_device(dev, 1, 2)) {
		usage_hack.sb_12++;
		if (usage_hack.sb_12 > 1) {
			result = 0;
			goto done;
		}
	}

	result = lv1_open_device(dev->bus_id, dev->dev_id, 0);

	if (result) {
		pr_debug("%s:%d: lv1_open_device failed: %s\n", __func__,
			__LINE__, ps3_result(result));
			result = -EPERM;
	}

done:
	mutex_unlock(&usage_hack.mutex);
	return result;
}

static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
{
	int result;

	BUG_ON(!dev->bus_id);
	mutex_lock(&usage_hack.mutex);

	if (ps3_is_device(dev, 1, 1)) {
		usage_hack.sb_11--;
		if (usage_hack.sb_11) {
			result = 0;
			goto done;
		}
	}

	if (ps3_is_device(dev, 1, 2)) {
		usage_hack.sb_12--;
		if (usage_hack.sb_12) {
			result = 0;
			goto done;
		}
	}

	result = lv1_close_device(dev->bus_id, dev->dev_id);
	BUG_ON(result);

done:
	mutex_unlock(&usage_hack.mutex);
	return result;
}

static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
{
	int result;

	mutex_lock(&usage_hack.mutex);

	usage_hack.gpu++;
	if (usage_hack.gpu > 1) {
		result = 0;
		goto done;
	}

	result = lv1_gpu_open(0);

	if (result) {
		pr_debug("%s:%d: lv1_gpu_open failed: %s\n", __func__,
			__LINE__, ps3_result(result));
			result = -EPERM;
	}

done:
	mutex_unlock(&usage_hack.mutex);
	return result;
}

static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
{
	int result;

	mutex_lock(&usage_hack.mutex);

	usage_hack.gpu--;
	if (usage_hack.gpu) {
		result = 0;
		goto done;
	}

	result = lv1_gpu_close();
	BUG_ON(result);

done:
	mutex_unlock(&usage_hack.mutex);
	return result;
}

int ps3_open_hv_device(struct ps3_system_bus_device *dev)
{
	BUG_ON(!dev);
	pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);

	switch (dev->match_id) {
	case PS3_MATCH_ID_EHCI:
	case PS3_MATCH_ID_OHCI:
	case PS3_MATCH_ID_GELIC:
	case PS3_MATCH_ID_STOR_DISK:
	case PS3_MATCH_ID_STOR_ROM:
	case PS3_MATCH_ID_STOR_FLASH:
		return ps3_open_hv_device_sb(dev);

	case PS3_MATCH_ID_SOUND:
179
	case PS3_MATCH_ID_GPU:
G
Geoff Levand 已提交
180 181 182 183 184 185
		return ps3_open_hv_device_gpu(dev);

	case PS3_MATCH_ID_AV_SETTINGS:
	case PS3_MATCH_ID_SYSTEM_MANAGER:
		pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
			__LINE__, dev->match_id);
186
		pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
187
			dev->bus_id);
G
Geoff Levand 已提交
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
		BUG();
		return -EINVAL;

	default:
		break;
	}

	pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
		dev->match_id);
	BUG();
	return -ENODEV;
}
EXPORT_SYMBOL_GPL(ps3_open_hv_device);

int ps3_close_hv_device(struct ps3_system_bus_device *dev)
{
	BUG_ON(!dev);
	pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);

	switch (dev->match_id) {
	case PS3_MATCH_ID_EHCI:
	case PS3_MATCH_ID_OHCI:
	case PS3_MATCH_ID_GELIC:
	case PS3_MATCH_ID_STOR_DISK:
	case PS3_MATCH_ID_STOR_ROM:
	case PS3_MATCH_ID_STOR_FLASH:
		return ps3_close_hv_device_sb(dev);

	case PS3_MATCH_ID_SOUND:
217
	case PS3_MATCH_ID_GPU:
G
Geoff Levand 已提交
218 219 220 221 222 223
		return ps3_close_hv_device_gpu(dev);

	case PS3_MATCH_ID_AV_SETTINGS:
	case PS3_MATCH_ID_SYSTEM_MANAGER:
		pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
			__LINE__, dev->match_id);
224
		pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
225
			dev->bus_id);
G
Geoff Levand 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
		BUG();
		return -EINVAL;

	default:
		break;
	}

	pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
		dev->match_id);
	BUG();
	return -ENODEV;
}
EXPORT_SYMBOL_GPL(ps3_close_hv_device);

240 241 242 243
#define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
static void _dump_mmio_region(const struct ps3_mmio_region* r,
	const char* func, int line)
{
244
	pr_debug("%s:%d: dev       %llu:%llu\n", func, line, r->dev->bus_id,
G
Geoff Levand 已提交
245
		r->dev->dev_id);
246 247 248 249 250
	pr_debug("%s:%d: bus_addr  %lxh\n", func, line, r->bus_addr);
	pr_debug("%s:%d: len       %lxh\n", func, line, r->len);
	pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
}

G
Geoff Levand 已提交
251
static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
252 253
{
	int result;
254
	u64 lpar_addr;
255

G
Geoff Levand 已提交
256
	result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
257 258
		r->bus_addr, r->len, r->page_size, &lpar_addr);
	r->lpar_addr = lpar_addr;
259 260 261 262

	if (result) {
		pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));
263
		r->lpar_addr = 0;
264 265 266 267 268
	}

	dump_mmio_region(r);
	return result;
}
G
Geoff Levand 已提交
269 270 271 272 273 274 275 276 277 278 279

static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
{
	/* device specific; do nothing currently */
	return 0;
}

int ps3_mmio_region_create(struct ps3_mmio_region *r)
{
	return r->mmio_ops->create(r);
}
A
Al Viro 已提交
280
EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
281

G
Geoff Levand 已提交
282
static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
283 284 285
{
	int result;

G
Geoff Levand 已提交
286 287
	dump_mmio_region(r);
	result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
288
		r->lpar_addr);
289 290 291 292 293

	if (result)
		pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
			__func__, __LINE__, ps3_result(result));

294
	r->lpar_addr = 0;
295 296
	return result;
}
G
Geoff Levand 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309

static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
{
	/* device specific; do nothing currently */
	return 0;
}


int ps3_free_mmio_region(struct ps3_mmio_region *r)
{
	return r->mmio_ops->free(r);
}

A
Al Viro 已提交
310
EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
311

G
Geoff Levand 已提交
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
static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
	.create = ps3_sb_mmio_region_create,
	.free = ps3_sb_free_mmio_region
};

static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
	.create = ps3_ioc0_mmio_region_create,
	.free = ps3_ioc0_free_mmio_region
};

int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
	struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
	enum ps3_mmio_page_size page_size)
{
	r->dev = dev;
	r->bus_addr = bus_addr;
	r->len = len;
	r->page_size = page_size;
	switch (dev->dev_type) {
	case PS3_DEVICE_TYPE_SB:
		r->mmio_ops = &ps3_mmio_sb_region_ops;
		break;
	case PS3_DEVICE_TYPE_IOC0:
		r->mmio_ops = &ps3_mmio_ioc0_region_ops;
		break;
	default:
		BUG();
		return -EINVAL;
	}
	return 0;
}
EXPORT_SYMBOL_GPL(ps3_mmio_region_init);

345 346 347 348
static int ps3_system_bus_match(struct device *_dev,
	struct device_driver *_drv)
{
	int result;
G
Geoff Levand 已提交
349 350
	struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
351

352 353 354 355 356
	if (!dev->match_sub_id)
		result = dev->match_id == drv->match_id;
	else
		result = dev->match_sub_id == drv->match_sub_id &&
			dev->match_id == drv->match_id;
357

358
	if (result)
359 360
		pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
			__func__, __LINE__,
361
			dev->match_id, dev->match_sub_id, dev_name(&dev->core),
362
			drv->match_id, drv->match_sub_id, drv->core.name);
363
	else
364 365
		pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
			__func__, __LINE__,
366
			dev->match_id, dev->match_sub_id, dev_name(&dev->core),
367 368
			drv->match_id, drv->match_sub_id, drv->core.name);

369 370 371 372 373
	return result;
}

static int ps3_system_bus_probe(struct device *_dev)
{
G
Geoff Levand 已提交
374 375 376
	int result = 0;
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
	struct ps3_system_bus_driver *drv;
377

G
Geoff Levand 已提交
378
	BUG_ON(!dev);
G
Greg Kroah-Hartman 已提交
379
	dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
380

G
Geoff Levand 已提交
381
	drv = ps3_system_bus_dev_to_system_bus_drv(dev);
382 383 384 385 386
	BUG_ON(!drv);

	if (drv->probe)
		result = drv->probe(dev);
	else
387
		pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
388
			dev_name(&dev->core));
389

390
	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
391 392 393 394 395
	return result;
}

static int ps3_system_bus_remove(struct device *_dev)
{
G
Geoff Levand 已提交
396 397 398 399 400
	int result = 0;
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
	struct ps3_system_bus_driver *drv;

	BUG_ON(!dev);
G
Greg Kroah-Hartman 已提交
401
	dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
G
Geoff Levand 已提交
402 403 404

	drv = ps3_system_bus_dev_to_system_bus_drv(dev);
	BUG_ON(!drv);
405 406

	if (drv->remove)
G
Geoff Levand 已提交
407
		result = drv->remove(dev);
408
	else
G
Geoff Levand 已提交
409 410 411
		dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
			__func__, __LINE__, drv->core.name);

412
	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
G
Geoff Levand 已提交
413 414
	return result;
}
415

G
Geoff Levand 已提交
416 417 418 419
static void ps3_system_bus_shutdown(struct device *_dev)
{
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
	struct ps3_system_bus_driver *drv;
420

G
Geoff Levand 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	BUG_ON(!dev);

	dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
		dev->match_id);

	if (!dev->core.driver) {
		dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
			__LINE__);
		return;
	}

	drv = ps3_system_bus_dev_to_system_bus_drv(dev);

	BUG_ON(!drv);

	dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
437
		dev_name(&dev->core), drv->core.name);
G
Geoff Levand 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451

	if (drv->shutdown)
		drv->shutdown(dev);
	else if (drv->remove) {
		dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
			__func__, __LINE__, drv->core.name);
		drv->remove(dev);
	} else {
		dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
			__func__, __LINE__, drv->core.name);
		BUG();
	}

	dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
452 453
}

454
static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env)
455 456 457
{
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);

458 459
	if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
			   dev->match_sub_id))
460 461 462 463
		return -ENOMEM;
	return 0;
}

464 465 466 467
static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
	char *buf)
{
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
468 469
	int len = snprintf(buf, PAGE_SIZE, "ps3:%d:%d\n", dev->match_id,
			   dev->match_sub_id);
470 471 472 473 474 475 476 477 478

	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
}

static struct device_attribute ps3_system_bus_dev_attrs[] = {
	__ATTR_RO(modalias),
	__ATTR_NULL,
};

479
struct bus_type ps3_system_bus_type = {
480
	.name = "ps3_system_bus",
481
	.match = ps3_system_bus_match,
482
	.uevent = ps3_system_bus_uevent,
483 484
	.probe = ps3_system_bus_probe,
	.remove = ps3_system_bus_remove,
G
Geoff Levand 已提交
485
	.shutdown = ps3_system_bus_shutdown,
486
	.dev_attrs = ps3_system_bus_dev_attrs,
487 488
};

G
Geoff Levand 已提交
489
static int __init ps3_system_bus_init(void)
490 491 492
{
	int result;

493
	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
494
		return -ENODEV;
495

G
Geoff Levand 已提交
496 497 498 499 500 501 502
	pr_debug(" -> %s:%d\n", __func__, __LINE__);

	mutex_init(&usage_hack.mutex);

	result = device_register(&ps3_system_bus);
	BUG_ON(result);

503 504
	result = bus_register(&ps3_system_bus_type);
	BUG_ON(result);
G
Geoff Levand 已提交
505 506

	pr_debug(" <- %s:%d\n", __func__, __LINE__);
507 508 509 510 511 512 513 514 515 516
	return result;
}

core_initcall(ps3_system_bus_init);

/* Allocates a contiguous real buffer and creates mappings over it.
 * Returns the virtual address of the buffer and sets dma_handle
 * to the dma address (mapping) of the first page.
 */
static void * ps3_alloc_coherent(struct device *_dev, size_t size,
G
Geoff Levand 已提交
517
				      dma_addr_t *dma_handle, gfp_t flag)
518 519
{
	int result;
G
Geoff Levand 已提交
520
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
521 522 523 524 525 526 527 528 529 530 531 532
	unsigned long virt_addr;

	flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
	flag |= __GFP_ZERO;

	virt_addr = __get_free_pages(flag, get_order(size));

	if (!virt_addr) {
		pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
		goto clean_none;
	}

G
Geoff Levand 已提交
533
	result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
534 535
			     CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
			     CBE_IOPTE_SO_RW | CBE_IOPTE_M);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	if (result) {
		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
			__func__, __LINE__, result);
		BUG_ON("check region type");
		goto clean_alloc;
	}

	return (void*)virt_addr;

clean_alloc:
	free_pages(virt_addr, get_order(size));
clean_none:
	dma_handle = NULL;
	return NULL;
}

static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
	dma_addr_t dma_handle)
{
G
Geoff Levand 已提交
556
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
557 558 559 560 561 562

	ps3_dma_unmap(dev->d_region, dma_handle, size);
	free_pages((unsigned long)vaddr, get_order(size));
}

/* Creates TCEs for a user provided buffer.  The user buffer must be
563 564 565
 * contiguous real kernel storage (not vmalloc).  The address passed here
 * comprises a page address and offset into that page. The dma_addr_t
 * returned will point to the same byte within the page as was passed in.
566 567
 */

568 569 570
static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
	unsigned long offset, size_t size, enum dma_data_direction direction,
	struct dma_attrs *attrs)
571
{
G
Geoff Levand 已提交
572
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
573
	int result;
574
	dma_addr_t bus_addr;
575
	void *ptr = page_address(page) + offset;
576 577

	result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
G
Geoff Levand 已提交
578
			     &bus_addr,
579 580
			     CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
			     CBE_IOPTE_SO_RW | CBE_IOPTE_M);
581 582 583 584 585 586 587 588 589

	if (result) {
		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
			__func__, __LINE__, result);
	}

	return bus_addr;
}

590 591 592 593
static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
				    unsigned long offset, size_t size,
				    enum dma_data_direction direction,
				    struct dma_attrs *attrs)
G
Geoff Levand 已提交
594 595 596
{
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
	int result;
597
	dma_addr_t bus_addr;
G
Geoff Levand 已提交
598
	u64 iopte_flag;
599
	void *ptr = page_address(page) + offset;
G
Geoff Levand 已提交
600

601
	iopte_flag = CBE_IOPTE_M;
G
Geoff Levand 已提交
602 603
	switch (direction) {
	case DMA_BIDIRECTIONAL:
604
		iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
G
Geoff Levand 已提交
605 606
		break;
	case DMA_TO_DEVICE:
607
		iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
G
Geoff Levand 已提交
608 609
		break;
	case DMA_FROM_DEVICE:
610
		iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
G
Geoff Levand 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		break;
	default:
		/* not happned */
		BUG();
	};
	result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
			     &bus_addr, iopte_flag);

	if (result) {
		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
			__func__, __LINE__, result);
	}
	return bus_addr;
}

626
static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
627
	size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
628
{
G
Geoff Levand 已提交
629
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
630 631 632 633 634 635 636 637 638 639
	int result;

	result = ps3_dma_unmap(dev->d_region, dma_addr, size);

	if (result) {
		pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
			__func__, __LINE__, result);
	}
}

J
Jens Axboe 已提交
640
static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
641
	int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
642 643 644
{
#if defined(CONFIG_PS3_DYNAMIC_DMA)
	BUG_ON("do");
645 646
	return -EPERM;
#else
G
Geoff Levand 已提交
647
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
J
Jens Axboe 已提交
648
	struct scatterlist *sg;
649 650
	int i;

J
Jens Axboe 已提交
651
	for_each_sg(sgl, sg, nents, i) {
J
Jens Axboe 已提交
652 653
		int result = ps3_dma_map(dev->d_region, sg_phys(sg),
					sg->length, &sg->dma_address, 0);
654 655 656 657 658 659 660 661 662 663 664

		if (result) {
			pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
				__func__, __LINE__, result);
			return -EINVAL;
		}

		sg->dma_length = sg->length;
	}

	return nents;
665 666 667
#endif
}

G
Geoff Levand 已提交
668 669
static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
			   int nents,
670 671
			   enum dma_data_direction direction,
			   struct dma_attrs *attrs)
G
Geoff Levand 已提交
672 673 674 675 676 677
{
	BUG();
	return 0;
}

static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
678
	int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
679 680 681 682 683 684
{
#if defined(CONFIG_PS3_DYNAMIC_DMA)
	BUG_ON("do");
#endif
}

G
Geoff Levand 已提交
685
static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
686 687
			    int nents, enum dma_data_direction direction,
			    struct dma_attrs *attrs)
G
Geoff Levand 已提交
688 689 690 691
{
	BUG();
}

692 693
static int ps3_dma_supported(struct device *_dev, u64 mask)
{
694
	return mask >= DMA_BIT_MASK(32);
695 696
}

G
Geoff Levand 已提交
697 698 699 700 701
static struct dma_mapping_ops ps3_sb_dma_ops = {
	.alloc_coherent = ps3_alloc_coherent,
	.free_coherent = ps3_free_coherent,
	.map_sg = ps3_sb_map_sg,
	.unmap_sg = ps3_sb_unmap_sg,
702 703 704
	.dma_supported = ps3_dma_supported,
	.map_page = ps3_sb_map_page,
	.unmap_page = ps3_unmap_page,
G
Geoff Levand 已提交
705 706 707
};

static struct dma_mapping_ops ps3_ioc0_dma_ops = {
708 709
	.alloc_coherent = ps3_alloc_coherent,
	.free_coherent = ps3_free_coherent,
G
Geoff Levand 已提交
710 711
	.map_sg = ps3_ioc0_map_sg,
	.unmap_sg = ps3_ioc0_unmap_sg,
712 713 714
	.dma_supported = ps3_dma_supported,
	.map_page = ps3_ioc0_map_page,
	.unmap_page = ps3_unmap_page,
715 716 717 718 719 720 721 722
};

/**
 * ps3_system_bus_release_device - remove a device from the system bus
 */

static void ps3_system_bus_release_device(struct device *_dev)
{
G
Geoff Levand 已提交
723
	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
724 725 726 727 728 729 730 731 732 733 734 735 736 737
	kfree(dev);
}

/**
 * ps3_system_bus_device_register - add a device to the system bus
 *
 * ps3_system_bus_device_register() expects the dev object to be allocated
 * dynamically by the caller.  The system bus takes ownership of the dev
 * object and frees the object in ps3_system_bus_release_device().
 */

int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
{
	int result;
G
Geoff Levand 已提交
738 739 740
	static unsigned int dev_ioc0_count;
	static unsigned int dev_sb_count;
	static unsigned int dev_vuart_count;
741
	static unsigned int dev_lpm_count;
742

G
Geoff Levand 已提交
743 744
	if (!dev->core.parent)
		dev->core.parent = &ps3_system_bus;
745 746 747
	dev->core.bus = &ps3_system_bus_type;
	dev->core.release = ps3_system_bus_release_device;

G
Geoff Levand 已提交
748 749 750
	switch (dev->dev_type) {
	case PS3_DEVICE_TYPE_IOC0:
		dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops;
751
		dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
G
Geoff Levand 已提交
752 753 754
		break;
	case PS3_DEVICE_TYPE_SB:
		dev->core.archdata.dma_ops = &ps3_sb_dma_ops;
755
		dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
G
Geoff Levand 已提交
756 757 758

		break;
	case PS3_DEVICE_TYPE_VUART:
759
		dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
G
Geoff Levand 已提交
760
		break;
761
	case PS3_DEVICE_TYPE_LPM:
762
		dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
763
		break;
G
Geoff Levand 已提交
764 765 766 767
	default:
		BUG();
	};

768
	dev->core.archdata.of_node = NULL;
B
Becky Bruce 已提交
769
	set_dev_node(&dev->core, 0);
770

771
	pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
772 773 774 775 776 777 778 779 780 781 782

	result = device_register(&dev->core);
	return result;
}

EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);

int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
{
	int result;

G
Geoff Levand 已提交
783 784 785 786 787
	pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);

	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
		return -ENODEV;

788 789 790
	drv->core.bus = &ps3_system_bus_type;

	result = driver_register(&drv->core);
G
Geoff Levand 已提交
791
	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
792 793 794 795 796 797 798
	return result;
}

EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);

void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
{
G
Geoff Levand 已提交
799
	pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
800
	driver_unregister(&drv->core);
G
Geoff Levand 已提交
801
	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
802 803 804
}

EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);