1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22a08ea69SGeoff Levand /*
32a08ea69SGeoff Levand  *  PS3 system bus driver.
42a08ea69SGeoff Levand  *
52a08ea69SGeoff Levand  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
62a08ea69SGeoff Levand  *  Copyright 2006 Sony Corp.
72a08ea69SGeoff Levand  */
82a08ea69SGeoff Levand 
92a08ea69SGeoff Levand #include <linux/kernel.h>
102a08ea69SGeoff Levand #include <linux/init.h>
114b16f8e2SPaul Gortmaker #include <linux/export.h>
120a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
132a08ea69SGeoff Levand #include <linux/err.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
152a08ea69SGeoff Levand 
162a08ea69SGeoff Levand #include <asm/udbg.h>
172a08ea69SGeoff Levand #include <asm/lv1call.h>
182a08ea69SGeoff Levand #include <asm/firmware.h>
199413c883SGeert Uytterhoeven #include <asm/cell-regs.h>
202a08ea69SGeoff Levand 
212a08ea69SGeoff Levand #include "platform.h"
222a08ea69SGeoff Levand 
236bb5cf10SGeoff Levand static struct device ps3_system_bus = {
24aab0d375SKay Sievers 	.init_name = "ps3_system",
256bb5cf10SGeoff Levand };
266bb5cf10SGeoff Levand 
276bb5cf10SGeoff Levand /* FIXME: need device usage counters! */
288e0f9735SMathieu Malaterre static struct {
296bb5cf10SGeoff Levand 	struct mutex mutex;
306bb5cf10SGeoff Levand 	int sb_11; /* usb 0 */
316bb5cf10SGeoff Levand 	int sb_12; /* usb 0 */
326bb5cf10SGeoff Levand 	int gpu;
338e0f9735SMathieu Malaterre } usage_hack;
346bb5cf10SGeoff Levand 
ps3_is_device(struct ps3_system_bus_device * dev,u64 bus_id,u64 dev_id)35034e0ab5SGeert Uytterhoeven static int ps3_is_device(struct ps3_system_bus_device *dev, u64 bus_id,
36034e0ab5SGeert Uytterhoeven 			 u64 dev_id)
376bb5cf10SGeoff Levand {
386bb5cf10SGeoff Levand 	return dev->bus_id == bus_id && dev->dev_id == dev_id;
396bb5cf10SGeoff Levand }
406bb5cf10SGeoff Levand 
ps3_open_hv_device_sb(struct ps3_system_bus_device * dev)416bb5cf10SGeoff Levand static int ps3_open_hv_device_sb(struct ps3_system_bus_device *dev)
426bb5cf10SGeoff Levand {
436bb5cf10SGeoff Levand 	int result;
446bb5cf10SGeoff Levand 
456bb5cf10SGeoff Levand 	BUG_ON(!dev->bus_id);
466bb5cf10SGeoff Levand 	mutex_lock(&usage_hack.mutex);
476bb5cf10SGeoff Levand 
486bb5cf10SGeoff Levand 	if (ps3_is_device(dev, 1, 1)) {
496bb5cf10SGeoff Levand 		usage_hack.sb_11++;
506bb5cf10SGeoff Levand 		if (usage_hack.sb_11 > 1) {
516bb5cf10SGeoff Levand 			result = 0;
526bb5cf10SGeoff Levand 			goto done;
536bb5cf10SGeoff Levand 		}
546bb5cf10SGeoff Levand 	}
556bb5cf10SGeoff Levand 
566bb5cf10SGeoff Levand 	if (ps3_is_device(dev, 1, 2)) {
576bb5cf10SGeoff Levand 		usage_hack.sb_12++;
586bb5cf10SGeoff Levand 		if (usage_hack.sb_12 > 1) {
596bb5cf10SGeoff Levand 			result = 0;
606bb5cf10SGeoff Levand 			goto done;
616bb5cf10SGeoff Levand 		}
626bb5cf10SGeoff Levand 	}
636bb5cf10SGeoff Levand 
646bb5cf10SGeoff Levand 	result = lv1_open_device(dev->bus_id, dev->dev_id, 0);
656bb5cf10SGeoff Levand 
666bb5cf10SGeoff Levand 	if (result) {
67472b440fSGeoff Levand 		pr_warn("%s:%d: lv1_open_device dev=%u.%u(%s) failed: %s\n",
68472b440fSGeoff Levand 			__func__, __LINE__, dev->match_id, dev->match_sub_id,
69472b440fSGeoff Levand 			dev_name(&dev->core), ps3_result(result));
706bb5cf10SGeoff Levand 		result = -EPERM;
716bb5cf10SGeoff Levand 	}
726bb5cf10SGeoff Levand 
736bb5cf10SGeoff Levand done:
746bb5cf10SGeoff Levand 	mutex_unlock(&usage_hack.mutex);
756bb5cf10SGeoff Levand 	return result;
766bb5cf10SGeoff Levand }
776bb5cf10SGeoff Levand 
ps3_close_hv_device_sb(struct ps3_system_bus_device * dev)786bb5cf10SGeoff Levand static int ps3_close_hv_device_sb(struct ps3_system_bus_device *dev)
796bb5cf10SGeoff Levand {
806bb5cf10SGeoff Levand 	int result;
816bb5cf10SGeoff Levand 
826bb5cf10SGeoff Levand 	BUG_ON(!dev->bus_id);
836bb5cf10SGeoff Levand 	mutex_lock(&usage_hack.mutex);
846bb5cf10SGeoff Levand 
856bb5cf10SGeoff Levand 	if (ps3_is_device(dev, 1, 1)) {
866bb5cf10SGeoff Levand 		usage_hack.sb_11--;
876bb5cf10SGeoff Levand 		if (usage_hack.sb_11) {
886bb5cf10SGeoff Levand 			result = 0;
896bb5cf10SGeoff Levand 			goto done;
906bb5cf10SGeoff Levand 		}
916bb5cf10SGeoff Levand 	}
926bb5cf10SGeoff Levand 
936bb5cf10SGeoff Levand 	if (ps3_is_device(dev, 1, 2)) {
946bb5cf10SGeoff Levand 		usage_hack.sb_12--;
956bb5cf10SGeoff Levand 		if (usage_hack.sb_12) {
966bb5cf10SGeoff Levand 			result = 0;
976bb5cf10SGeoff Levand 			goto done;
986bb5cf10SGeoff Levand 		}
996bb5cf10SGeoff Levand 	}
1006bb5cf10SGeoff Levand 
1016bb5cf10SGeoff Levand 	result = lv1_close_device(dev->bus_id, dev->dev_id);
1026bb5cf10SGeoff Levand 	BUG_ON(result);
1036bb5cf10SGeoff Levand 
1046bb5cf10SGeoff Levand done:
1056bb5cf10SGeoff Levand 	mutex_unlock(&usage_hack.mutex);
1066bb5cf10SGeoff Levand 	return result;
1076bb5cf10SGeoff Levand }
1086bb5cf10SGeoff Levand 
ps3_open_hv_device_gpu(struct ps3_system_bus_device * dev)1096bb5cf10SGeoff Levand static int ps3_open_hv_device_gpu(struct ps3_system_bus_device *dev)
1106bb5cf10SGeoff Levand {
1116bb5cf10SGeoff Levand 	int result;
1126bb5cf10SGeoff Levand 
1136bb5cf10SGeoff Levand 	mutex_lock(&usage_hack.mutex);
1146bb5cf10SGeoff Levand 
1156bb5cf10SGeoff Levand 	usage_hack.gpu++;
1166bb5cf10SGeoff Levand 	if (usage_hack.gpu > 1) {
1176bb5cf10SGeoff Levand 		result = 0;
1186bb5cf10SGeoff Levand 		goto done;
1196bb5cf10SGeoff Levand 	}
1206bb5cf10SGeoff Levand 
1216bb5cf10SGeoff Levand 	result = lv1_gpu_open(0);
1226bb5cf10SGeoff Levand 
1236bb5cf10SGeoff Levand 	if (result) {
124472b440fSGeoff Levand 		pr_warn("%s:%d: lv1_gpu_open failed: %s\n", __func__,
1256bb5cf10SGeoff Levand 			__LINE__, ps3_result(result));
1266bb5cf10SGeoff Levand 			result = -EPERM;
1276bb5cf10SGeoff Levand 	}
1286bb5cf10SGeoff Levand 
1296bb5cf10SGeoff Levand done:
1306bb5cf10SGeoff Levand 	mutex_unlock(&usage_hack.mutex);
1316bb5cf10SGeoff Levand 	return result;
1326bb5cf10SGeoff Levand }
1336bb5cf10SGeoff Levand 
ps3_close_hv_device_gpu(struct ps3_system_bus_device * dev)1346bb5cf10SGeoff Levand static int ps3_close_hv_device_gpu(struct ps3_system_bus_device *dev)
1356bb5cf10SGeoff Levand {
1366bb5cf10SGeoff Levand 	int result;
1376bb5cf10SGeoff Levand 
1386bb5cf10SGeoff Levand 	mutex_lock(&usage_hack.mutex);
1396bb5cf10SGeoff Levand 
1406bb5cf10SGeoff Levand 	usage_hack.gpu--;
1416bb5cf10SGeoff Levand 	if (usage_hack.gpu) {
1426bb5cf10SGeoff Levand 		result = 0;
1436bb5cf10SGeoff Levand 		goto done;
1446bb5cf10SGeoff Levand 	}
1456bb5cf10SGeoff Levand 
1466bb5cf10SGeoff Levand 	result = lv1_gpu_close();
1476bb5cf10SGeoff Levand 	BUG_ON(result);
1486bb5cf10SGeoff Levand 
1496bb5cf10SGeoff Levand done:
1506bb5cf10SGeoff Levand 	mutex_unlock(&usage_hack.mutex);
1516bb5cf10SGeoff Levand 	return result;
1526bb5cf10SGeoff Levand }
1536bb5cf10SGeoff Levand 
ps3_open_hv_device(struct ps3_system_bus_device * dev)1546bb5cf10SGeoff Levand int ps3_open_hv_device(struct ps3_system_bus_device *dev)
1556bb5cf10SGeoff Levand {
1566bb5cf10SGeoff Levand 	BUG_ON(!dev);
1576bb5cf10SGeoff Levand 	pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
1586bb5cf10SGeoff Levand 
1596bb5cf10SGeoff Levand 	switch (dev->match_id) {
1606bb5cf10SGeoff Levand 	case PS3_MATCH_ID_EHCI:
1616bb5cf10SGeoff Levand 	case PS3_MATCH_ID_OHCI:
1626bb5cf10SGeoff Levand 	case PS3_MATCH_ID_GELIC:
1636bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_DISK:
1646bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_ROM:
1656bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_FLASH:
1666bb5cf10SGeoff Levand 		return ps3_open_hv_device_sb(dev);
1676bb5cf10SGeoff Levand 
1686bb5cf10SGeoff Levand 	case PS3_MATCH_ID_SOUND:
16946d01492SGeert Uytterhoeven 	case PS3_MATCH_ID_GPU:
1706bb5cf10SGeoff Levand 		return ps3_open_hv_device_gpu(dev);
1716bb5cf10SGeoff Levand 
1726bb5cf10SGeoff Levand 	case PS3_MATCH_ID_AV_SETTINGS:
1736bb5cf10SGeoff Levand 	case PS3_MATCH_ID_SYSTEM_MANAGER:
1746bb5cf10SGeoff Levand 		pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
1756bb5cf10SGeoff Levand 			__LINE__, dev->match_id);
1765c949070SStephen Rothwell 		pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
177034e0ab5SGeert Uytterhoeven 			dev->bus_id);
1786bb5cf10SGeoff Levand 		BUG();
1796bb5cf10SGeoff Levand 		return -EINVAL;
1806bb5cf10SGeoff Levand 
1816bb5cf10SGeoff Levand 	default:
1826bb5cf10SGeoff Levand 		break;
1836bb5cf10SGeoff Levand 	}
1846bb5cf10SGeoff Levand 
1856bb5cf10SGeoff Levand 	pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
1866bb5cf10SGeoff Levand 		dev->match_id);
1876bb5cf10SGeoff Levand 	BUG();
1886bb5cf10SGeoff Levand 	return -ENODEV;
1896bb5cf10SGeoff Levand }
1906bb5cf10SGeoff Levand EXPORT_SYMBOL_GPL(ps3_open_hv_device);
1916bb5cf10SGeoff Levand 
ps3_close_hv_device(struct ps3_system_bus_device * dev)1926bb5cf10SGeoff Levand int ps3_close_hv_device(struct ps3_system_bus_device *dev)
1936bb5cf10SGeoff Levand {
1946bb5cf10SGeoff Levand 	BUG_ON(!dev);
1956bb5cf10SGeoff Levand 	pr_debug("%s:%d: match_id: %u\n", __func__, __LINE__, dev->match_id);
1966bb5cf10SGeoff Levand 
1976bb5cf10SGeoff Levand 	switch (dev->match_id) {
1986bb5cf10SGeoff Levand 	case PS3_MATCH_ID_EHCI:
1996bb5cf10SGeoff Levand 	case PS3_MATCH_ID_OHCI:
2006bb5cf10SGeoff Levand 	case PS3_MATCH_ID_GELIC:
2016bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_DISK:
2026bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_ROM:
2036bb5cf10SGeoff Levand 	case PS3_MATCH_ID_STOR_FLASH:
2046bb5cf10SGeoff Levand 		return ps3_close_hv_device_sb(dev);
2056bb5cf10SGeoff Levand 
2066bb5cf10SGeoff Levand 	case PS3_MATCH_ID_SOUND:
20746d01492SGeert Uytterhoeven 	case PS3_MATCH_ID_GPU:
2086bb5cf10SGeoff Levand 		return ps3_close_hv_device_gpu(dev);
2096bb5cf10SGeoff Levand 
2106bb5cf10SGeoff Levand 	case PS3_MATCH_ID_AV_SETTINGS:
2116bb5cf10SGeoff Levand 	case PS3_MATCH_ID_SYSTEM_MANAGER:
2126bb5cf10SGeoff Levand 		pr_debug("%s:%d: unsupported match_id: %u\n", __func__,
2136bb5cf10SGeoff Levand 			__LINE__, dev->match_id);
2145c949070SStephen Rothwell 		pr_debug("%s:%d: bus_id: %llu\n", __func__, __LINE__,
215034e0ab5SGeert Uytterhoeven 			dev->bus_id);
2166bb5cf10SGeoff Levand 		BUG();
2176bb5cf10SGeoff Levand 		return -EINVAL;
2186bb5cf10SGeoff Levand 
2196bb5cf10SGeoff Levand 	default:
2206bb5cf10SGeoff Levand 		break;
2216bb5cf10SGeoff Levand 	}
2226bb5cf10SGeoff Levand 
2236bb5cf10SGeoff Levand 	pr_debug("%s:%d: unknown match_id: %u\n", __func__, __LINE__,
2246bb5cf10SGeoff Levand 		dev->match_id);
2256bb5cf10SGeoff Levand 	BUG();
2266bb5cf10SGeoff Levand 	return -ENODEV;
2276bb5cf10SGeoff Levand }
2286bb5cf10SGeoff Levand EXPORT_SYMBOL_GPL(ps3_close_hv_device);
2296bb5cf10SGeoff Levand 
2302a08ea69SGeoff Levand #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
_dump_mmio_region(const struct ps3_mmio_region * r,const char * func,int line)2312a08ea69SGeoff Levand static void _dump_mmio_region(const struct ps3_mmio_region* r,
2322a08ea69SGeoff Levand 	const char* func, int line)
2332a08ea69SGeoff Levand {
2345c949070SStephen Rothwell 	pr_debug("%s:%d: dev       %llu:%llu\n", func, line, r->dev->bus_id,
2356bb5cf10SGeoff Levand 		r->dev->dev_id);
2362a08ea69SGeoff Levand 	pr_debug("%s:%d: bus_addr  %lxh\n", func, line, r->bus_addr);
2372a08ea69SGeoff Levand 	pr_debug("%s:%d: len       %lxh\n", func, line, r->len);
2382a08ea69SGeoff Levand 	pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
2392a08ea69SGeoff Levand }
2402a08ea69SGeoff Levand 
ps3_sb_mmio_region_create(struct ps3_mmio_region * r)2416bb5cf10SGeoff Levand static int ps3_sb_mmio_region_create(struct ps3_mmio_region *r)
2422a08ea69SGeoff Levand {
2432a08ea69SGeoff Levand 	int result;
244b17b3df1SStephen Rothwell 	u64 lpar_addr;
2452a08ea69SGeoff Levand 
2466bb5cf10SGeoff Levand 	result = lv1_map_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
247b17b3df1SStephen Rothwell 		r->bus_addr, r->len, r->page_size, &lpar_addr);
248b17b3df1SStephen Rothwell 	r->lpar_addr = lpar_addr;
2492a08ea69SGeoff Levand 
2502a08ea69SGeoff Levand 	if (result) {
2512a08ea69SGeoff Levand 		pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
2522a08ea69SGeoff Levand 			__func__, __LINE__, ps3_result(result));
2532a08ea69SGeoff Levand 		r->lpar_addr = 0;
2542a08ea69SGeoff Levand 	}
2552a08ea69SGeoff Levand 
2562a08ea69SGeoff Levand 	dump_mmio_region(r);
2572a08ea69SGeoff Levand 	return result;
2582a08ea69SGeoff Levand }
2596bb5cf10SGeoff Levand 
ps3_ioc0_mmio_region_create(struct ps3_mmio_region * r)2606bb5cf10SGeoff Levand static int ps3_ioc0_mmio_region_create(struct ps3_mmio_region *r)
2616bb5cf10SGeoff Levand {
2626bb5cf10SGeoff Levand 	/* device specific; do nothing currently */
2636bb5cf10SGeoff Levand 	return 0;
2646bb5cf10SGeoff Levand }
2656bb5cf10SGeoff Levand 
ps3_mmio_region_create(struct ps3_mmio_region * r)2666bb5cf10SGeoff Levand int ps3_mmio_region_create(struct ps3_mmio_region *r)
2676bb5cf10SGeoff Levand {
2686bb5cf10SGeoff Levand 	return r->mmio_ops->create(r);
2696bb5cf10SGeoff Levand }
2709288f5c3SAl Viro EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
2712a08ea69SGeoff Levand 
ps3_sb_free_mmio_region(struct ps3_mmio_region * r)2726bb5cf10SGeoff Levand static int ps3_sb_free_mmio_region(struct ps3_mmio_region *r)
2732a08ea69SGeoff Levand {
2742a08ea69SGeoff Levand 	int result;
2752a08ea69SGeoff Levand 
2766bb5cf10SGeoff Levand 	dump_mmio_region(r);
2776bb5cf10SGeoff Levand 	result = lv1_unmap_device_mmio_region(r->dev->bus_id, r->dev->dev_id,
2782a08ea69SGeoff Levand 		r->lpar_addr);
2792a08ea69SGeoff Levand 
2802a08ea69SGeoff Levand 	if (result)
2812a08ea69SGeoff Levand 		pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
2822a08ea69SGeoff Levand 			__func__, __LINE__, ps3_result(result));
2832a08ea69SGeoff Levand 
2842a08ea69SGeoff Levand 	r->lpar_addr = 0;
2852a08ea69SGeoff Levand 	return result;
2862a08ea69SGeoff Levand }
2876bb5cf10SGeoff Levand 
ps3_ioc0_free_mmio_region(struct ps3_mmio_region * r)2886bb5cf10SGeoff Levand static int ps3_ioc0_free_mmio_region(struct ps3_mmio_region *r)
2896bb5cf10SGeoff Levand {
2906bb5cf10SGeoff Levand 	/* device specific; do nothing currently */
2916bb5cf10SGeoff Levand 	return 0;
2926bb5cf10SGeoff Levand }
2936bb5cf10SGeoff Levand 
2946bb5cf10SGeoff Levand 
ps3_free_mmio_region(struct ps3_mmio_region * r)2956bb5cf10SGeoff Levand int ps3_free_mmio_region(struct ps3_mmio_region *r)
2966bb5cf10SGeoff Levand {
2976bb5cf10SGeoff Levand 	return r->mmio_ops->free(r);
2986bb5cf10SGeoff Levand }
2996bb5cf10SGeoff Levand 
3009288f5c3SAl Viro EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
3012a08ea69SGeoff Levand 
3026bb5cf10SGeoff Levand static const struct ps3_mmio_region_ops ps3_mmio_sb_region_ops = {
3036bb5cf10SGeoff Levand 	.create = ps3_sb_mmio_region_create,
3046bb5cf10SGeoff Levand 	.free = ps3_sb_free_mmio_region
3056bb5cf10SGeoff Levand };
3066bb5cf10SGeoff Levand 
3076bb5cf10SGeoff Levand static const struct ps3_mmio_region_ops ps3_mmio_ioc0_region_ops = {
3086bb5cf10SGeoff Levand 	.create = ps3_ioc0_mmio_region_create,
3096bb5cf10SGeoff Levand 	.free = ps3_ioc0_free_mmio_region
3106bb5cf10SGeoff Levand };
3116bb5cf10SGeoff Levand 
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)3126bb5cf10SGeoff Levand int ps3_mmio_region_init(struct ps3_system_bus_device *dev,
3136bb5cf10SGeoff Levand 	struct ps3_mmio_region *r, unsigned long bus_addr, unsigned long len,
3146bb5cf10SGeoff Levand 	enum ps3_mmio_page_size page_size)
3156bb5cf10SGeoff Levand {
3166bb5cf10SGeoff Levand 	r->dev = dev;
3176bb5cf10SGeoff Levand 	r->bus_addr = bus_addr;
3186bb5cf10SGeoff Levand 	r->len = len;
3196bb5cf10SGeoff Levand 	r->page_size = page_size;
3206bb5cf10SGeoff Levand 	switch (dev->dev_type) {
3216bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
3226bb5cf10SGeoff Levand 		r->mmio_ops = &ps3_mmio_sb_region_ops;
3236bb5cf10SGeoff Levand 		break;
3246bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
3256bb5cf10SGeoff Levand 		r->mmio_ops = &ps3_mmio_ioc0_region_ops;
3266bb5cf10SGeoff Levand 		break;
3276bb5cf10SGeoff Levand 	default:
3286bb5cf10SGeoff Levand 		BUG();
3296bb5cf10SGeoff Levand 		return -EINVAL;
3306bb5cf10SGeoff Levand 	}
3316bb5cf10SGeoff Levand 	return 0;
3326bb5cf10SGeoff Levand }
3336bb5cf10SGeoff Levand EXPORT_SYMBOL_GPL(ps3_mmio_region_init);
3346bb5cf10SGeoff Levand 
ps3_system_bus_match(struct device * _dev,struct device_driver * _drv)3352a08ea69SGeoff Levand static int ps3_system_bus_match(struct device *_dev,
3362a08ea69SGeoff Levand 	struct device_driver *_drv)
3372a08ea69SGeoff Levand {
3382a08ea69SGeoff Levand 	int result;
3396bb5cf10SGeoff Levand 	struct ps3_system_bus_driver *drv = ps3_drv_to_system_bus_drv(_drv);
3406bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
3412a08ea69SGeoff Levand 
342059e4938SMasakazu Mokuno 	if (!dev->match_sub_id)
3432a08ea69SGeoff Levand 		result = dev->match_id == drv->match_id;
344059e4938SMasakazu Mokuno 	else
345059e4938SMasakazu Mokuno 		result = dev->match_sub_id == drv->match_sub_id &&
346059e4938SMasakazu Mokuno 			dev->match_id == drv->match_id;
3472a08ea69SGeoff Levand 
34888b90c96SGeoff Levand 	if (result)
349059e4938SMasakazu Mokuno 		pr_info("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): match\n",
350059e4938SMasakazu Mokuno 			__func__, __LINE__,
351aab0d375SKay Sievers 			dev->match_id, dev->match_sub_id, dev_name(&dev->core),
352059e4938SMasakazu Mokuno 			drv->match_id, drv->match_sub_id, drv->core.name);
35388b90c96SGeoff Levand 	else
354059e4938SMasakazu Mokuno 		pr_debug("%s:%d: dev=%u.%u(%s), drv=%u.%u(%s): miss\n",
355059e4938SMasakazu Mokuno 			__func__, __LINE__,
356aab0d375SKay Sievers 			dev->match_id, dev->match_sub_id, dev_name(&dev->core),
357059e4938SMasakazu Mokuno 			drv->match_id, drv->match_sub_id, drv->core.name);
358059e4938SMasakazu Mokuno 
3592a08ea69SGeoff Levand 	return result;
3602a08ea69SGeoff Levand }
3612a08ea69SGeoff Levand 
ps3_system_bus_probe(struct device * _dev)3622a08ea69SGeoff Levand static int ps3_system_bus_probe(struct device *_dev)
3632a08ea69SGeoff Levand {
3646bb5cf10SGeoff Levand 	int result = 0;
3656bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
3666bb5cf10SGeoff Levand 	struct ps3_system_bus_driver *drv;
3672a08ea69SGeoff Levand 
3686bb5cf10SGeoff Levand 	BUG_ON(!dev);
36954ca5412SGreg Kroah-Hartman 	dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
3702a08ea69SGeoff Levand 
3716bb5cf10SGeoff Levand 	drv = ps3_system_bus_dev_to_system_bus_drv(dev);
3722a08ea69SGeoff Levand 	BUG_ON(!drv);
3732a08ea69SGeoff Levand 
3742a08ea69SGeoff Levand 	if (drv->probe)
3752a08ea69SGeoff Levand 		result = drv->probe(dev);
3762a08ea69SGeoff Levand 	else
37788b90c96SGeoff Levand 		pr_debug("%s:%d: %s no probe method\n", __func__, __LINE__,
378aab0d375SKay Sievers 			dev_name(&dev->core));
3792a08ea69SGeoff Levand 
380aab0d375SKay Sievers 	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
3812a08ea69SGeoff Levand 	return result;
3822a08ea69SGeoff Levand }
3832a08ea69SGeoff Levand 
ps3_system_bus_remove(struct device * _dev)384fc7a6209SUwe Kleine-König static void ps3_system_bus_remove(struct device *_dev)
3852a08ea69SGeoff Levand {
3866bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
3876bb5cf10SGeoff Levand 	struct ps3_system_bus_driver *drv;
3886bb5cf10SGeoff Levand 
3896bb5cf10SGeoff Levand 	BUG_ON(!dev);
39054ca5412SGreg Kroah-Hartman 	dev_dbg(_dev, "%s:%d\n", __func__, __LINE__);
3916bb5cf10SGeoff Levand 
3926bb5cf10SGeoff Levand 	drv = ps3_system_bus_dev_to_system_bus_drv(dev);
3936bb5cf10SGeoff Levand 	BUG_ON(!drv);
3942a08ea69SGeoff Levand 
3952a08ea69SGeoff Levand 	if (drv->remove)
3966d247e4dSUwe Kleine-König 		drv->remove(dev);
3972a08ea69SGeoff Levand 	else
3986bb5cf10SGeoff Levand 		dev_dbg(&dev->core, "%s:%d %s: no remove method\n",
3996bb5cf10SGeoff Levand 			__func__, __LINE__, drv->core.name);
4002a08ea69SGeoff Levand 
401aab0d375SKay Sievers 	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, dev_name(&dev->core));
4026bb5cf10SGeoff Levand }
4032a08ea69SGeoff Levand 
ps3_system_bus_shutdown(struct device * _dev)4046bb5cf10SGeoff Levand static void ps3_system_bus_shutdown(struct device *_dev)
4056bb5cf10SGeoff Levand {
4066bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
4076bb5cf10SGeoff Levand 	struct ps3_system_bus_driver *drv;
4086bb5cf10SGeoff Levand 
4096bb5cf10SGeoff Levand 	BUG_ON(!dev);
4106bb5cf10SGeoff Levand 
4116bb5cf10SGeoff Levand 	dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
4126bb5cf10SGeoff Levand 		dev->match_id);
4136bb5cf10SGeoff Levand 
4146bb5cf10SGeoff Levand 	if (!dev->core.driver) {
4156bb5cf10SGeoff Levand 		dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
4166bb5cf10SGeoff Levand 			__LINE__);
4176bb5cf10SGeoff Levand 		return;
4186bb5cf10SGeoff Levand 	}
4196bb5cf10SGeoff Levand 
4206bb5cf10SGeoff Levand 	drv = ps3_system_bus_dev_to_system_bus_drv(dev);
4216bb5cf10SGeoff Levand 
4226bb5cf10SGeoff Levand 	BUG_ON(!drv);
4236bb5cf10SGeoff Levand 
4246bb5cf10SGeoff Levand 	dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__,
425aab0d375SKay Sievers 		dev_name(&dev->core), drv->core.name);
4266bb5cf10SGeoff Levand 
4276bb5cf10SGeoff Levand 	if (drv->shutdown)
4286bb5cf10SGeoff Levand 		drv->shutdown(dev);
4296bb5cf10SGeoff Levand 	else if (drv->remove) {
4306bb5cf10SGeoff Levand 		dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n",
4316bb5cf10SGeoff Levand 			__func__, __LINE__, drv->core.name);
4326bb5cf10SGeoff Levand 		drv->remove(dev);
4336bb5cf10SGeoff Levand 	} else {
4346bb5cf10SGeoff Levand 		dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n",
4356bb5cf10SGeoff Levand 			__func__, __LINE__, drv->core.name);
4366bb5cf10SGeoff Levand 		BUG();
4376bb5cf10SGeoff Levand 	}
4386bb5cf10SGeoff Levand 
4396bb5cf10SGeoff Levand 	dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
4402a08ea69SGeoff Levand }
4412a08ea69SGeoff Levand 
ps3_system_bus_uevent(const struct device * _dev,struct kobj_uevent_env * env)442*b7810ea8SStephen Rothwell static int ps3_system_bus_uevent(const struct device *_dev, struct kobj_uevent_env *env)
443688b3378SDavid Woodhouse {
444688b3378SDavid Woodhouse 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
445688b3378SDavid Woodhouse 
44646d01492SGeert Uytterhoeven 	if (add_uevent_var(env, "MODALIAS=ps3:%d:%d", dev->match_id,
44746d01492SGeert Uytterhoeven 			   dev->match_sub_id))
448688b3378SDavid Woodhouse 		return -ENOMEM;
449688b3378SDavid Woodhouse 	return 0;
450688b3378SDavid Woodhouse }
451688b3378SDavid Woodhouse 
modalias_show(struct device * _dev,struct device_attribute * a,char * buf)4526758555dSDavid Woodhouse static ssize_t modalias_show(struct device *_dev, struct device_attribute *a,
4536758555dSDavid Woodhouse 	char *buf)
4546758555dSDavid Woodhouse {
4556758555dSDavid Woodhouse 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
45646d01492SGeert Uytterhoeven 	int len = snprintf(buf, PAGE_SIZE, "ps3:%d:%d\n", dev->match_id,
45746d01492SGeert Uytterhoeven 			   dev->match_sub_id);
4586758555dSDavid Woodhouse 
4596758555dSDavid Woodhouse 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
4606758555dSDavid Woodhouse }
461892533e1SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias);
4626758555dSDavid Woodhouse 
463892533e1SGreg Kroah-Hartman static struct attribute *ps3_system_bus_dev_attrs[] = {
464892533e1SGreg Kroah-Hartman 	&dev_attr_modalias.attr,
465892533e1SGreg Kroah-Hartman 	NULL,
4666758555dSDavid Woodhouse };
467892533e1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(ps3_system_bus_dev);
4686758555dSDavid Woodhouse 
469dea681c9SChristoph Hellwig static struct bus_type ps3_system_bus_type = {
4702a08ea69SGeoff Levand 	.name = "ps3_system_bus",
4712a08ea69SGeoff Levand 	.match = ps3_system_bus_match,
472688b3378SDavid Woodhouse 	.uevent = ps3_system_bus_uevent,
4732a08ea69SGeoff Levand 	.probe = ps3_system_bus_probe,
4742a08ea69SGeoff Levand 	.remove = ps3_system_bus_remove,
4756bb5cf10SGeoff Levand 	.shutdown = ps3_system_bus_shutdown,
476892533e1SGreg Kroah-Hartman 	.dev_groups = ps3_system_bus_dev_groups,
4772a08ea69SGeoff Levand };
4782a08ea69SGeoff Levand 
ps3_system_bus_init(void)4796bb5cf10SGeoff Levand static int __init ps3_system_bus_init(void)
4802a08ea69SGeoff Levand {
4812a08ea69SGeoff Levand 	int result;
4822a08ea69SGeoff Levand 
4832a08ea69SGeoff Levand 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
484ef596c69SGeert Uytterhoeven 		return -ENODEV;
4852a08ea69SGeoff Levand 
4866bb5cf10SGeoff Levand 	pr_debug(" -> %s:%d\n", __func__, __LINE__);
4876bb5cf10SGeoff Levand 
4886bb5cf10SGeoff Levand 	mutex_init(&usage_hack.mutex);
4896bb5cf10SGeoff Levand 
4906bb5cf10SGeoff Levand 	result = device_register(&ps3_system_bus);
4916bb5cf10SGeoff Levand 	BUG_ON(result);
4926bb5cf10SGeoff Levand 
4932a08ea69SGeoff Levand 	result = bus_register(&ps3_system_bus_type);
4942a08ea69SGeoff Levand 	BUG_ON(result);
4956bb5cf10SGeoff Levand 
4966bb5cf10SGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
4972a08ea69SGeoff Levand 	return result;
4982a08ea69SGeoff Levand }
4992a08ea69SGeoff Levand 
5002a08ea69SGeoff Levand core_initcall(ps3_system_bus_init);
5012a08ea69SGeoff Levand 
5022a08ea69SGeoff Levand /* Allocates a contiguous real buffer and creates mappings over it.
5032a08ea69SGeoff Levand  * Returns the virtual address of the buffer and sets dma_handle
5042a08ea69SGeoff Levand  * to the dma address (mapping) of the first page.
5052a08ea69SGeoff Levand  */
ps3_alloc_coherent(struct device * _dev,size_t size,dma_addr_t * dma_handle,gfp_t flag,unsigned long attrs)5062a08ea69SGeoff Levand static void * ps3_alloc_coherent(struct device *_dev, size_t size,
507bfbf7d61SAndrzej Pietrasiewicz 				 dma_addr_t *dma_handle, gfp_t flag,
50800085f1eSKrzysztof Kozlowski 				 unsigned long attrs)
5092a08ea69SGeoff Levand {
5102a08ea69SGeoff Levand 	int result;
5116bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
5122a08ea69SGeoff Levand 	unsigned long virt_addr;
5132a08ea69SGeoff Levand 
5142a08ea69SGeoff Levand 	flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
5152a08ea69SGeoff Levand 	flag |= __GFP_ZERO;
5162a08ea69SGeoff Levand 
5172a08ea69SGeoff Levand 	virt_addr = __get_free_pages(flag, get_order(size));
5182a08ea69SGeoff Levand 
5192a08ea69SGeoff Levand 	if (!virt_addr) {
5202a08ea69SGeoff Levand 		pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
5212a08ea69SGeoff Levand 		goto clean_none;
5222a08ea69SGeoff Levand 	}
5232a08ea69SGeoff Levand 
5246bb5cf10SGeoff Levand 	result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle,
5255c6fc8dbSGeert Uytterhoeven 			     CBE_IOPTE_PP_W | CBE_IOPTE_PP_R |
5265c6fc8dbSGeert Uytterhoeven 			     CBE_IOPTE_SO_RW | CBE_IOPTE_M);
5272a08ea69SGeoff Levand 
5282a08ea69SGeoff Levand 	if (result) {
5292a08ea69SGeoff Levand 		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
5302a08ea69SGeoff Levand 			__func__, __LINE__, result);
5312a08ea69SGeoff Levand 		BUG_ON("check region type");
5322a08ea69SGeoff Levand 		goto clean_alloc;
5332a08ea69SGeoff Levand 	}
5342a08ea69SGeoff Levand 
5352a08ea69SGeoff Levand 	return (void*)virt_addr;
5362a08ea69SGeoff Levand 
5372a08ea69SGeoff Levand clean_alloc:
5382a08ea69SGeoff Levand 	free_pages(virt_addr, get_order(size));
5392a08ea69SGeoff Levand clean_none:
5402a08ea69SGeoff Levand 	dma_handle = NULL;
5412a08ea69SGeoff Levand 	return NULL;
5422a08ea69SGeoff Levand }
5432a08ea69SGeoff Levand 
ps3_free_coherent(struct device * _dev,size_t size,void * vaddr,dma_addr_t dma_handle,unsigned long attrs)5442a08ea69SGeoff Levand static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
54500085f1eSKrzysztof Kozlowski 			      dma_addr_t dma_handle, unsigned long attrs)
5462a08ea69SGeoff Levand {
5476bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
5482a08ea69SGeoff Levand 
5492a08ea69SGeoff Levand 	ps3_dma_unmap(dev->d_region, dma_handle, size);
5502a08ea69SGeoff Levand 	free_pages((unsigned long)vaddr, get_order(size));
5512a08ea69SGeoff Levand }
5522a08ea69SGeoff Levand 
5532a08ea69SGeoff Levand /* Creates TCEs for a user provided buffer.  The user buffer must be
554f9226d57SMark Nelson  * contiguous real kernel storage (not vmalloc).  The address passed here
555f9226d57SMark Nelson  * comprises a page address and offset into that page. The dma_addr_t
556f9226d57SMark Nelson  * returned will point to the same byte within the page as was passed in.
5572a08ea69SGeoff Levand  */
5582a08ea69SGeoff Levand 
ps3_sb_map_page(struct device * _dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction,unsigned long attrs)559f9226d57SMark Nelson static dma_addr_t ps3_sb_map_page(struct device *_dev, struct page *page,
560f9226d57SMark Nelson 	unsigned long offset, size_t size, enum dma_data_direction direction,
56100085f1eSKrzysztof Kozlowski 	unsigned long attrs)
5622a08ea69SGeoff Levand {
5636bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
5642a08ea69SGeoff Levand 	int result;
565494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
566f9226d57SMark Nelson 	void *ptr = page_address(page) + offset;
5672a08ea69SGeoff Levand 
5682a08ea69SGeoff Levand 	result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
5696bb5cf10SGeoff Levand 			     &bus_addr,
5705c6fc8dbSGeert Uytterhoeven 			     CBE_IOPTE_PP_R | CBE_IOPTE_PP_W |
5715c6fc8dbSGeert Uytterhoeven 			     CBE_IOPTE_SO_RW | CBE_IOPTE_M);
5722a08ea69SGeoff Levand 
5732a08ea69SGeoff Levand 	if (result) {
5742a08ea69SGeoff Levand 		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
5752a08ea69SGeoff Levand 			__func__, __LINE__, result);
5762a08ea69SGeoff Levand 	}
5772a08ea69SGeoff Levand 
5782a08ea69SGeoff Levand 	return bus_addr;
5792a08ea69SGeoff Levand }
5802a08ea69SGeoff Levand 
ps3_ioc0_map_page(struct device * _dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction,unsigned long attrs)581f9226d57SMark Nelson static dma_addr_t ps3_ioc0_map_page(struct device *_dev, struct page *page,
582f9226d57SMark Nelson 				    unsigned long offset, size_t size,
5833affedc4SMark Nelson 				    enum dma_data_direction direction,
58400085f1eSKrzysztof Kozlowski 				    unsigned long attrs)
5856bb5cf10SGeoff Levand {
5866bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
5876bb5cf10SGeoff Levand 	int result;
588494fd07aSStephen Rothwell 	dma_addr_t bus_addr;
5896bb5cf10SGeoff Levand 	u64 iopte_flag;
590f9226d57SMark Nelson 	void *ptr = page_address(page) + offset;
5916bb5cf10SGeoff Levand 
5925c6fc8dbSGeert Uytterhoeven 	iopte_flag = CBE_IOPTE_M;
5936bb5cf10SGeoff Levand 	switch (direction) {
5946bb5cf10SGeoff Levand 	case DMA_BIDIRECTIONAL:
5955c6fc8dbSGeert Uytterhoeven 		iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
5966bb5cf10SGeoff Levand 		break;
5976bb5cf10SGeoff Levand 	case DMA_TO_DEVICE:
5985c6fc8dbSGeert Uytterhoeven 		iopte_flag |= CBE_IOPTE_PP_R | CBE_IOPTE_SO_R;
5996bb5cf10SGeoff Levand 		break;
6006bb5cf10SGeoff Levand 	case DMA_FROM_DEVICE:
6015c6fc8dbSGeert Uytterhoeven 		iopte_flag |= CBE_IOPTE_PP_W | CBE_IOPTE_SO_RW;
6026bb5cf10SGeoff Levand 		break;
6036bb5cf10SGeoff Levand 	default:
6041fd02f66SJulia Lawall 		/* not happened */
6056bb5cf10SGeoff Levand 		BUG();
6069f519606Sjing yangyang 	}
6076bb5cf10SGeoff Levand 	result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
6086bb5cf10SGeoff Levand 			     &bus_addr, iopte_flag);
6096bb5cf10SGeoff Levand 
6106bb5cf10SGeoff Levand 	if (result) {
6116bb5cf10SGeoff Levand 		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
6126bb5cf10SGeoff Levand 			__func__, __LINE__, result);
6136bb5cf10SGeoff Levand 	}
6146bb5cf10SGeoff Levand 	return bus_addr;
6156bb5cf10SGeoff Levand }
6166bb5cf10SGeoff Levand 
ps3_unmap_page(struct device * _dev,dma_addr_t dma_addr,size_t size,enum dma_data_direction direction,unsigned long attrs)617f9226d57SMark Nelson static void ps3_unmap_page(struct device *_dev, dma_addr_t dma_addr,
61800085f1eSKrzysztof Kozlowski 	size_t size, enum dma_data_direction direction, unsigned long attrs)
6192a08ea69SGeoff Levand {
6206bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
6212a08ea69SGeoff Levand 	int result;
6222a08ea69SGeoff Levand 
6232a08ea69SGeoff Levand 	result = ps3_dma_unmap(dev->d_region, dma_addr, size);
6242a08ea69SGeoff Levand 
6252a08ea69SGeoff Levand 	if (result) {
6262a08ea69SGeoff Levand 		pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
6272a08ea69SGeoff Levand 			__func__, __LINE__, result);
6282a08ea69SGeoff Levand 	}
6292a08ea69SGeoff Levand }
6302a08ea69SGeoff Levand 
ps3_sb_map_sg(struct device * _dev,struct scatterlist * sgl,int nents,enum dma_data_direction direction,unsigned long attrs)631d1ed455eSJens Axboe static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
63200085f1eSKrzysztof Kozlowski 	int nents, enum dma_data_direction direction, unsigned long attrs)
6332a08ea69SGeoff Levand {
6342a08ea69SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
6352a08ea69SGeoff Levand 	BUG_ON("do");
63635063bb2SGeoff Levand 	return -EPERM;
63735063bb2SGeoff Levand #else
6386bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
639d1ed455eSJens Axboe 	struct scatterlist *sg;
640435e0b2bSStephen Rothwell 	int i;
641435e0b2bSStephen Rothwell 
642d1ed455eSJens Axboe 	for_each_sg(sgl, sg, nents, i) {
64358b053e4SJens Axboe 		int result = ps3_dma_map(dev->d_region, sg_phys(sg),
64458b053e4SJens Axboe 					sg->length, &sg->dma_address, 0);
64535063bb2SGeoff Levand 
64635063bb2SGeoff Levand 		if (result) {
64735063bb2SGeoff Levand 			pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
64835063bb2SGeoff Levand 				__func__, __LINE__, result);
64935063bb2SGeoff Levand 			return -EINVAL;
65035063bb2SGeoff Levand 		}
65135063bb2SGeoff Levand 
65235063bb2SGeoff Levand 		sg->dma_length = sg->length;
65335063bb2SGeoff Levand 	}
65435063bb2SGeoff Levand 
65535063bb2SGeoff Levand 	return nents;
6562a08ea69SGeoff Levand #endif
6572a08ea69SGeoff Levand }
6582a08ea69SGeoff Levand 
ps3_ioc0_map_sg(struct device * _dev,struct scatterlist * sg,int nents,enum dma_data_direction direction,unsigned long attrs)6596bb5cf10SGeoff Levand static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg,
6606bb5cf10SGeoff Levand 			   int nents,
6613affedc4SMark Nelson 			   enum dma_data_direction direction,
66200085f1eSKrzysztof Kozlowski 			   unsigned long attrs)
6636bb5cf10SGeoff Levand {
6646bb5cf10SGeoff Levand 	BUG();
665c4e0e892SMartin Oliveira 	return -EINVAL;
6666bb5cf10SGeoff Levand }
6676bb5cf10SGeoff Levand 
ps3_sb_unmap_sg(struct device * _dev,struct scatterlist * sg,int nents,enum dma_data_direction direction,unsigned long attrs)6686bb5cf10SGeoff Levand static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
66900085f1eSKrzysztof Kozlowski 	int nents, enum dma_data_direction direction, unsigned long attrs)
6702a08ea69SGeoff Levand {
6712a08ea69SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA)
6722a08ea69SGeoff Levand 	BUG_ON("do");
6732a08ea69SGeoff Levand #endif
6742a08ea69SGeoff Levand }
6752a08ea69SGeoff Levand 
ps3_ioc0_unmap_sg(struct device * _dev,struct scatterlist * sg,int nents,enum dma_data_direction direction,unsigned long attrs)6766bb5cf10SGeoff Levand static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg,
6773affedc4SMark Nelson 			    int nents, enum dma_data_direction direction,
67800085f1eSKrzysztof Kozlowski 			    unsigned long attrs)
6796bb5cf10SGeoff Levand {
6806bb5cf10SGeoff Levand 	BUG();
6816bb5cf10SGeoff Levand }
6826bb5cf10SGeoff Levand 
ps3_dma_supported(struct device * _dev,u64 mask)6832a08ea69SGeoff Levand static int ps3_dma_supported(struct device *_dev, u64 mask)
6842a08ea69SGeoff Levand {
685284901a9SYang Hongyang 	return mask >= DMA_BIT_MASK(32);
6862a08ea69SGeoff Levand }
6872a08ea69SGeoff Levand 
6885299709dSBart Van Assche static const struct dma_map_ops ps3_sb_dma_ops = {
689bfbf7d61SAndrzej Pietrasiewicz 	.alloc = ps3_alloc_coherent,
690bfbf7d61SAndrzej Pietrasiewicz 	.free = ps3_free_coherent,
6916bb5cf10SGeoff Levand 	.map_sg = ps3_sb_map_sg,
6926bb5cf10SGeoff Levand 	.unmap_sg = ps3_sb_unmap_sg,
693f9226d57SMark Nelson 	.dma_supported = ps3_dma_supported,
694f9226d57SMark Nelson 	.map_page = ps3_sb_map_page,
695f9226d57SMark Nelson 	.unmap_page = ps3_unmap_page,
696f9f3232aSChristoph Hellwig 	.mmap = dma_common_mmap,
697f9f3232aSChristoph Hellwig 	.get_sgtable = dma_common_get_sgtable,
698efa70f2fSChristoph Hellwig 	.alloc_pages = dma_common_alloc_pages,
699efa70f2fSChristoph Hellwig 	.free_pages = dma_common_free_pages,
7006bb5cf10SGeoff Levand };
7016bb5cf10SGeoff Levand 
7025299709dSBart Van Assche static const struct dma_map_ops ps3_ioc0_dma_ops = {
703bfbf7d61SAndrzej Pietrasiewicz 	.alloc = ps3_alloc_coherent,
704bfbf7d61SAndrzej Pietrasiewicz 	.free = ps3_free_coherent,
7056bb5cf10SGeoff Levand 	.map_sg = ps3_ioc0_map_sg,
7066bb5cf10SGeoff Levand 	.unmap_sg = ps3_ioc0_unmap_sg,
707f9226d57SMark Nelson 	.dma_supported = ps3_dma_supported,
708f9226d57SMark Nelson 	.map_page = ps3_ioc0_map_page,
709f9226d57SMark Nelson 	.unmap_page = ps3_unmap_page,
710f9f3232aSChristoph Hellwig 	.mmap = dma_common_mmap,
711f9f3232aSChristoph Hellwig 	.get_sgtable = dma_common_get_sgtable,
712efa70f2fSChristoph Hellwig 	.alloc_pages = dma_common_alloc_pages,
713efa70f2fSChristoph Hellwig 	.free_pages = dma_common_free_pages,
7142a08ea69SGeoff Levand };
7152a08ea69SGeoff Levand 
7162a08ea69SGeoff Levand /**
7172a08ea69SGeoff Levand  * ps3_system_bus_release_device - remove a device from the system bus
7182a08ea69SGeoff Levand  */
7192a08ea69SGeoff Levand 
ps3_system_bus_release_device(struct device * _dev)7202a08ea69SGeoff Levand static void ps3_system_bus_release_device(struct device *_dev)
7212a08ea69SGeoff Levand {
7226bb5cf10SGeoff Levand 	struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
7232a08ea69SGeoff Levand 	kfree(dev);
7242a08ea69SGeoff Levand }
7252a08ea69SGeoff Levand 
7262a08ea69SGeoff Levand /**
7272a08ea69SGeoff Levand  * ps3_system_bus_device_register - add a device to the system bus
7282a08ea69SGeoff Levand  *
7292a08ea69SGeoff Levand  * ps3_system_bus_device_register() expects the dev object to be allocated
7302a08ea69SGeoff Levand  * dynamically by the caller.  The system bus takes ownership of the dev
7312a08ea69SGeoff Levand  * object and frees the object in ps3_system_bus_release_device().
7322a08ea69SGeoff Levand  */
7332a08ea69SGeoff Levand 
ps3_system_bus_device_register(struct ps3_system_bus_device * dev)7342a08ea69SGeoff Levand int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
7352a08ea69SGeoff Levand {
7362a08ea69SGeoff Levand 	int result;
7376bb5cf10SGeoff Levand 	static unsigned int dev_ioc0_count;
7386bb5cf10SGeoff Levand 	static unsigned int dev_sb_count;
7396bb5cf10SGeoff Levand 	static unsigned int dev_vuart_count;
740ed757002SGeoff Levand 	static unsigned int dev_lpm_count;
7412a08ea69SGeoff Levand 
7426bb5cf10SGeoff Levand 	if (!dev->core.parent)
7436bb5cf10SGeoff Levand 		dev->core.parent = &ps3_system_bus;
7442a08ea69SGeoff Levand 	dev->core.bus = &ps3_system_bus_type;
7452a08ea69SGeoff Levand 	dev->core.release = ps3_system_bus_release_device;
7462a08ea69SGeoff Levand 
7476bb5cf10SGeoff Levand 	switch (dev->dev_type) {
7486bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_IOC0:
7495657933dSBart Van Assche 		dev->core.dma_ops = &ps3_ioc0_dma_ops;
750aab0d375SKay Sievers 		dev_set_name(&dev->core, "ioc0_%02x", ++dev_ioc0_count);
7516bb5cf10SGeoff Levand 		break;
7526bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_SB:
7535657933dSBart Van Assche 		dev->core.dma_ops = &ps3_sb_dma_ops;
754aab0d375SKay Sievers 		dev_set_name(&dev->core, "sb_%02x", ++dev_sb_count);
7552a08ea69SGeoff Levand 
7566bb5cf10SGeoff Levand 		break;
7576bb5cf10SGeoff Levand 	case PS3_DEVICE_TYPE_VUART:
758aab0d375SKay Sievers 		dev_set_name(&dev->core, "vuart_%02x", ++dev_vuart_count);
7596bb5cf10SGeoff Levand 		break;
760ed757002SGeoff Levand 	case PS3_DEVICE_TYPE_LPM:
761aab0d375SKay Sievers 		dev_set_name(&dev->core, "lpm_%02x", ++dev_lpm_count);
762ed757002SGeoff Levand 		break;
7636bb5cf10SGeoff Levand 	default:
7646bb5cf10SGeoff Levand 		BUG();
7659f519606Sjing yangyang 	}
7666bb5cf10SGeoff Levand 
767d706c1b0SGrant Likely 	dev->core.of_node = NULL;
7688fae0353SBecky Bruce 	set_dev_node(&dev->core, 0);
7692a08ea69SGeoff Levand 
770aab0d375SKay Sievers 	pr_debug("%s:%d add %s\n", __func__, __LINE__, dev_name(&dev->core));
7712a08ea69SGeoff Levand 
7722a08ea69SGeoff Levand 	result = device_register(&dev->core);
7732a08ea69SGeoff Levand 	return result;
7742a08ea69SGeoff Levand }
7752a08ea69SGeoff Levand 
7762a08ea69SGeoff Levand EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
7772a08ea69SGeoff Levand 
ps3_system_bus_driver_register(struct ps3_system_bus_driver * drv)7782a08ea69SGeoff Levand int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
7792a08ea69SGeoff Levand {
7802a08ea69SGeoff Levand 	int result;
7812a08ea69SGeoff Levand 
7826bb5cf10SGeoff Levand 	pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
7836bb5cf10SGeoff Levand 
7846bb5cf10SGeoff Levand 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
7856bb5cf10SGeoff Levand 		return -ENODEV;
7866bb5cf10SGeoff Levand 
7872a08ea69SGeoff Levand 	drv->core.bus = &ps3_system_bus_type;
7882a08ea69SGeoff Levand 
7892a08ea69SGeoff Levand 	result = driver_register(&drv->core);
7906bb5cf10SGeoff Levand 	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
7912a08ea69SGeoff Levand 	return result;
7922a08ea69SGeoff Levand }
7932a08ea69SGeoff Levand 
7942a08ea69SGeoff Levand EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
7952a08ea69SGeoff Levand 
ps3_system_bus_driver_unregister(struct ps3_system_bus_driver * drv)7962a08ea69SGeoff Levand void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
7972a08ea69SGeoff Levand {
7986bb5cf10SGeoff Levand 	pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name);
7992a08ea69SGeoff Levand 	driver_unregister(&drv->core);
8006bb5cf10SGeoff Levand 	pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);
8012a08ea69SGeoff Levand }
8022a08ea69SGeoff Levand 
8032a08ea69SGeoff Levand EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);
804