1/*
2 *  PS3 system bus driver.
3 *
4 *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5 *  Copyright 2006 Sony Corp.
6 *
7 *  This program is free software; you can redistribute it and/or modify
8 *  it under the terms of the GNU General Public License as published by
9 *  the Free Software Foundation; version 2 of the License.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/dma-mapping.h>
25#include <linux/err.h>
26
27#include <asm/udbg.h>
28#include <asm/lv1call.h>
29#include <asm/firmware.h>
30
31#include "platform.h"
32
33#define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
34static void _dump_mmio_region(const struct ps3_mmio_region* r,
35	const char* func, int line)
36{
37	pr_debug("%s:%d: dev       %u:%u\n", func, line, r->did.bus_id,
38		r->did.dev_id);
39	pr_debug("%s:%d: bus_addr  %lxh\n", func, line, r->bus_addr);
40	pr_debug("%s:%d: len       %lxh\n", func, line, r->len);
41	pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
42}
43
44int ps3_mmio_region_create(struct ps3_mmio_region *r)
45{
46	int result;
47
48	result = lv1_map_device_mmio_region(r->did.bus_id, r->did.dev_id,
49		r->bus_addr, r->len, r->page_size, &r->lpar_addr);
50
51	if (result) {
52		pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
53			__func__, __LINE__, ps3_result(result));
54		r->lpar_addr = 0;
55	}
56
57	dump_mmio_region(r);
58	return result;
59}
60EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
61
62int ps3_free_mmio_region(struct ps3_mmio_region *r)
63{
64	int result;
65
66	result = lv1_unmap_device_mmio_region(r->did.bus_id, r->did.dev_id,
67		r->lpar_addr);
68
69	if (result)
70		pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
71			__func__, __LINE__, ps3_result(result));
72
73	r->lpar_addr = 0;
74	return result;
75}
76EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
77
78static int ps3_system_bus_match(struct device *_dev,
79	struct device_driver *_drv)
80{
81	int result;
82	struct ps3_system_bus_driver *drv = to_ps3_system_bus_driver(_drv);
83	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
84
85	result = dev->match_id == drv->match_id;
86
87	pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__,
88		dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name,
89		(result ? "match" : "miss"));
90	return result;
91}
92
93static int ps3_system_bus_probe(struct device *_dev)
94{
95	int result;
96	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
97	struct ps3_system_bus_driver *drv =
98		to_ps3_system_bus_driver(_dev->driver);
99
100	result = lv1_open_device(dev->did.bus_id, dev->did.dev_id, 0);
101
102	if (result) {
103		pr_debug("%s:%d: lv1_open_device failed (%d)\n",
104			__func__, __LINE__, result);
105		result = -EACCES;
106		goto clean_none;
107	}
108
109	if (dev->d_region->did.bus_id) {
110		result = ps3_dma_region_create(dev->d_region);
111
112		if (result) {
113			pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n",
114				__func__, __LINE__, result);
115			BUG_ON("check region type");
116			result = -EINVAL;
117			goto clean_device;
118		}
119	}
120
121	BUG_ON(!drv);
122
123	if (drv->probe)
124		result = drv->probe(dev);
125	else
126		pr_info("%s:%d: %s no probe method\n", __func__, __LINE__,
127			dev->core.bus_id);
128
129	if (result) {
130		pr_debug("%s:%d: drv->probe failed\n", __func__, __LINE__);
131		goto clean_dma;
132	}
133
134	return result;
135
136clean_dma:
137	ps3_dma_region_free(dev->d_region);
138clean_device:
139	lv1_close_device(dev->did.bus_id, dev->did.dev_id);
140clean_none:
141	return result;
142}
143
144static int ps3_system_bus_remove(struct device *_dev)
145{
146	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
147	struct ps3_system_bus_driver *drv =
148		to_ps3_system_bus_driver(_dev->driver);
149
150	if (drv->remove)
151		drv->remove(dev);
152	else
153		pr_info("%s:%d: %s no remove method\n", __func__, __LINE__,
154			dev->core.bus_id);
155
156	ps3_dma_region_free(dev->d_region);
157	ps3_free_mmio_region(dev->m_region);
158	lv1_close_device(dev->did.bus_id, dev->did.dev_id);
159
160	return 0;
161}
162
163struct bus_type ps3_system_bus_type = {
164	.name = "ps3_system_bus",
165	.match = ps3_system_bus_match,
166	.probe = ps3_system_bus_probe,
167	.remove = ps3_system_bus_remove,
168};
169
170int __init ps3_system_bus_init(void)
171{
172	int result;
173
174	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
175		return -ENODEV;
176
177	result = bus_register(&ps3_system_bus_type);
178	BUG_ON(result);
179	return result;
180}
181
182core_initcall(ps3_system_bus_init);
183
184/* Allocates a contiguous real buffer and creates mappings over it.
185 * Returns the virtual address of the buffer and sets dma_handle
186 * to the dma address (mapping) of the first page.
187 */
188
189static void * ps3_alloc_coherent(struct device *_dev, size_t size,
190	dma_addr_t *dma_handle, gfp_t flag)
191{
192	int result;
193	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
194	unsigned long virt_addr;
195
196	BUG_ON(!dev->d_region->bus_addr);
197
198	flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
199	flag |= __GFP_ZERO;
200
201	virt_addr = __get_free_pages(flag, get_order(size));
202
203	if (!virt_addr) {
204		pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
205		goto clean_none;
206	}
207
208	result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle);
209
210	if (result) {
211		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
212			__func__, __LINE__, result);
213		BUG_ON("check region type");
214		goto clean_alloc;
215	}
216
217	return (void*)virt_addr;
218
219clean_alloc:
220	free_pages(virt_addr, get_order(size));
221clean_none:
222	dma_handle = NULL;
223	return NULL;
224}
225
226static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
227	dma_addr_t dma_handle)
228{
229	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
230
231	ps3_dma_unmap(dev->d_region, dma_handle, size);
232	free_pages((unsigned long)vaddr, get_order(size));
233}
234
235/* Creates TCEs for a user provided buffer.  The user buffer must be
236 * contiguous real kernel storage (not vmalloc).  The address of the buffer
237 * passed here is the kernel (virtual) address of the buffer.  The buffer
238 * need not be page aligned, the dma_addr_t returned will point to the same
239 * byte within the page as vaddr.
240 */
241
242static dma_addr_t ps3_map_single(struct device *_dev, void *ptr, size_t size,
243	enum dma_data_direction direction)
244{
245	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
246	int result;
247	unsigned long bus_addr;
248
249	result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
250		&bus_addr);
251
252	if (result) {
253		pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
254			__func__, __LINE__, result);
255	}
256
257	return bus_addr;
258}
259
260static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
261	size_t size, enum dma_data_direction direction)
262{
263	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
264	int result;
265
266	result = ps3_dma_unmap(dev->d_region, dma_addr, size);
267
268	if (result) {
269		pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
270			__func__, __LINE__, result);
271	}
272}
273
274static int ps3_map_sg(struct device *_dev, struct scatterlist *sg, int nents,
275	enum dma_data_direction direction)
276{
277#if defined(CONFIG_PS3_DYNAMIC_DMA)
278	BUG_ON("do");
279	return -EPERM;
280#else
281	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
282	int i;
283
284	for (i = 0; i < nents; i++, sg++) {
285		int result = ps3_dma_map(dev->d_region,
286			page_to_phys(sg->page) + sg->offset, sg->length,
287			&sg->dma_address);
288
289		if (result) {
290			pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
291				__func__, __LINE__, result);
292			return -EINVAL;
293		}
294
295		sg->dma_length = sg->length;
296	}
297
298	return nents;
299#endif
300}
301
302static void ps3_unmap_sg(struct device *_dev, struct scatterlist *sg,
303	int nents, enum dma_data_direction direction)
304{
305#if defined(CONFIG_PS3_DYNAMIC_DMA)
306	BUG_ON("do");
307#endif
308}
309
310static int ps3_dma_supported(struct device *_dev, u64 mask)
311{
312	return mask >= DMA_32BIT_MASK;
313}
314
315static struct dma_mapping_ops ps3_dma_ops = {
316	.alloc_coherent = ps3_alloc_coherent,
317	.free_coherent = ps3_free_coherent,
318	.map_single = ps3_map_single,
319	.unmap_single = ps3_unmap_single,
320	.map_sg = ps3_map_sg,
321	.unmap_sg = ps3_unmap_sg,
322	.dma_supported = ps3_dma_supported
323};
324
325/**
326 * ps3_system_bus_release_device - remove a device from the system bus
327 */
328
329static void ps3_system_bus_release_device(struct device *_dev)
330{
331	struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
332	kfree(dev);
333}
334
335/**
336 * ps3_system_bus_device_register - add a device to the system bus
337 *
338 * ps3_system_bus_device_register() expects the dev object to be allocated
339 * dynamically by the caller.  The system bus takes ownership of the dev
340 * object and frees the object in ps3_system_bus_release_device().
341 */
342
343int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
344{
345	int result;
346	static unsigned int dev_count = 1;
347
348	dev->core.parent = NULL;
349	dev->core.bus = &ps3_system_bus_type;
350	dev->core.release = ps3_system_bus_release_device;
351
352	dev->core.archdata.of_node = NULL;
353	dev->core.archdata.dma_ops = &ps3_dma_ops;
354	dev->core.archdata.numa_node = 0;
355
356	snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x",
357		dev_count++);
358
359	pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id);
360
361	result = device_register(&dev->core);
362	return result;
363}
364
365EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
366
367int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
368{
369	int result;
370
371	drv->core.bus = &ps3_system_bus_type;
372
373	result = driver_register(&drv->core);
374	return result;
375}
376
377EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
378
379void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
380{
381	driver_unregister(&drv->core);
382}
383
384EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);
385