xref: /linux/arch/powerpc/platforms/pseries/ibmebus.c (revision 0c7c237b1c35011ef0b8d30c1d5c20bc6ae7b69b)
1 /*
2  * IBM PowerPC IBM eBus Infrastructure Support.
3  *
4  * Copyright (c) 2005 IBM Corporation
5  *  Joachim Fenkes <fenkes@de.ibm.com>
6  *  Heiko J Schick <schickhj@de.ibm.com>
7  *
8  * All rights reserved.
9  *
10  * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11  * BSD.
12  *
13  * OpenIB BSD License
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * Redistributions of source code must retain the above copyright notice, this
19  * list of conditions and the following disclaimer.
20  *
21  * Redistributions in binary form must reproduce the above copyright notice,
22  * this list of conditions and the following disclaimer in the documentation
23  * and/or other materials
24  * provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <linux/init.h>
40 #include <linux/export.h>
41 #include <linux/console.h>
42 #include <linux/kobject.h>
43 #include <linux/dma-map-ops.h>
44 #include <linux/interrupt.h>
45 #include <linux/irqdomain.h>
46 #include <linux/of.h>
47 #include <linux/slab.h>
48 #include <linux/stat.h>
49 #include <linux/of_platform.h>
50 #include <asm/ibmebus.h>
51 #include <asm/machdep.h>
52 
53 static struct device ibmebus_bus_device = { /* fake "parent" device */
54 	.init_name = "ibmebus",
55 };
56 
57 struct bus_type ibmebus_bus_type;
58 
59 /* These devices will automatically be added to the bus during init */
60 static const struct of_device_id ibmebus_matches[] __initconst = {
61 	{ .compatible = "IBM,lhca" },
62 	{ .compatible = "IBM,lhea" },
63 	{},
64 };
65 
66 static void *ibmebus_alloc_coherent(struct device *dev,
67 				    size_t size,
68 				    dma_addr_t *dma_handle,
69 				    gfp_t flag,
70 				    unsigned long attrs)
71 {
72 	void *mem;
73 
74 	mem = kmalloc(size, flag);
75 	*dma_handle = (dma_addr_t)mem;
76 
77 	return mem;
78 }
79 
80 static void ibmebus_free_coherent(struct device *dev,
81 				  size_t size, void *vaddr,
82 				  dma_addr_t dma_handle,
83 				  unsigned long attrs)
84 {
85 	kfree(vaddr);
86 }
87 
88 static dma_addr_t ibmebus_map_page(struct device *dev,
89 				   struct page *page,
90 				   unsigned long offset,
91 				   size_t size,
92 				   enum dma_data_direction direction,
93 				   unsigned long attrs)
94 {
95 	return (dma_addr_t)(page_address(page) + offset);
96 }
97 
98 static void ibmebus_unmap_page(struct device *dev,
99 			       dma_addr_t dma_addr,
100 			       size_t size,
101 			       enum dma_data_direction direction,
102 			       unsigned long attrs)
103 {
104 	return;
105 }
106 
107 static int ibmebus_map_sg(struct device *dev,
108 			  struct scatterlist *sgl,
109 			  int nents, enum dma_data_direction direction,
110 			  unsigned long attrs)
111 {
112 	struct scatterlist *sg;
113 	int i;
114 
115 	for_each_sg(sgl, sg, nents, i) {
116 		sg->dma_address = (dma_addr_t) sg_virt(sg);
117 		sg->dma_length = sg->length;
118 	}
119 
120 	return nents;
121 }
122 
123 static void ibmebus_unmap_sg(struct device *dev,
124 			     struct scatterlist *sg,
125 			     int nents, enum dma_data_direction direction,
126 			     unsigned long attrs)
127 {
128 	return;
129 }
130 
131 static int ibmebus_dma_supported(struct device *dev, u64 mask)
132 {
133 	return mask == DMA_BIT_MASK(64);
134 }
135 
136 static u64 ibmebus_dma_get_required_mask(struct device *dev)
137 {
138 	return DMA_BIT_MASK(64);
139 }
140 
141 static const struct dma_map_ops ibmebus_dma_ops = {
142 	.alloc              = ibmebus_alloc_coherent,
143 	.free               = ibmebus_free_coherent,
144 	.map_sg             = ibmebus_map_sg,
145 	.unmap_sg           = ibmebus_unmap_sg,
146 	.dma_supported      = ibmebus_dma_supported,
147 	.get_required_mask  = ibmebus_dma_get_required_mask,
148 	.map_page           = ibmebus_map_page,
149 	.unmap_page         = ibmebus_unmap_page,
150 };
151 
152 static int ibmebus_match_path(struct device *dev, const void *data)
153 {
154 	struct device_node *dn = to_platform_device(dev)->dev.of_node;
155 	struct device_node *tn = of_find_node_by_path(data);
156 
157 	of_node_put(tn);
158 
159 	return (tn == dn);
160 }
161 
162 static int ibmebus_match_node(struct device *dev, const void *data)
163 {
164 	return to_platform_device(dev)->dev.of_node == data;
165 }
166 
167 static int ibmebus_create_device(struct device_node *dn)
168 {
169 	struct platform_device *dev;
170 	int ret;
171 
172 	dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
173 	if (!dev)
174 		return -ENOMEM;
175 
176 	dev->dev.bus = &ibmebus_bus_type;
177 	dev->dev.dma_ops = &ibmebus_dma_ops;
178 
179 	ret = of_device_add(dev);
180 	if (ret)
181 		platform_device_put(dev);
182 	return ret;
183 }
184 
185 static int ibmebus_create_devices(const struct of_device_id *matches)
186 {
187 	struct device_node *root, *child;
188 	struct device *dev;
189 	int ret = 0;
190 
191 	root = of_find_node_by_path("/");
192 
193 	for_each_child_of_node(root, child) {
194 		if (!of_match_node(matches, child))
195 			continue;
196 
197 		dev = bus_find_device(&ibmebus_bus_type, NULL, child,
198 				      ibmebus_match_node);
199 		if (dev) {
200 			put_device(dev);
201 			continue;
202 		}
203 
204 		ret = ibmebus_create_device(child);
205 		if (ret) {
206 			printk(KERN_ERR "%s: failed to create device (%i)",
207 			       __func__, ret);
208 			of_node_put(child);
209 			break;
210 		}
211 	}
212 
213 	of_node_put(root);
214 	return ret;
215 }
216 
217 int ibmebus_register_driver(struct platform_driver *drv)
218 {
219 	/* If the driver uses devices that ibmebus doesn't know, add them */
220 	ibmebus_create_devices(drv->driver.of_match_table);
221 
222 	drv->driver.bus = &ibmebus_bus_type;
223 	return driver_register(&drv->driver);
224 }
225 EXPORT_SYMBOL(ibmebus_register_driver);
226 
227 void ibmebus_unregister_driver(struct platform_driver *drv)
228 {
229 	driver_unregister(&drv->driver);
230 }
231 EXPORT_SYMBOL(ibmebus_unregister_driver);
232 
233 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
234 			unsigned long irq_flags, const char *devname,
235 			void *dev_id)
236 {
237 	unsigned int irq = irq_create_mapping(NULL, ist);
238 
239 	if (!irq)
240 		return -EINVAL;
241 
242 	return request_irq(irq, handler, irq_flags, devname, dev_id);
243 }
244 EXPORT_SYMBOL(ibmebus_request_irq);
245 
246 void ibmebus_free_irq(u32 ist, void *dev_id)
247 {
248 	unsigned int irq = irq_find_mapping(NULL, ist);
249 
250 	free_irq(irq, dev_id);
251 	irq_dispose_mapping(irq);
252 }
253 EXPORT_SYMBOL(ibmebus_free_irq);
254 
255 static char *ibmebus_chomp(const char *in, size_t count)
256 {
257 	char *out = kmalloc(count + 1, GFP_KERNEL);
258 
259 	if (!out)
260 		return NULL;
261 
262 	memcpy(out, in, count);
263 	out[count] = '\0';
264 	if (out[count - 1] == '\n')
265 		out[count - 1] = '\0';
266 
267 	return out;
268 }
269 
270 static ssize_t probe_store(const struct bus_type *bus, const char *buf, size_t count)
271 {
272 	struct device_node *dn = NULL;
273 	struct device *dev;
274 	char *path;
275 	ssize_t rc = 0;
276 
277 	path = ibmebus_chomp(buf, count);
278 	if (!path)
279 		return -ENOMEM;
280 
281 	dev = bus_find_device(&ibmebus_bus_type, NULL, path,
282 			      ibmebus_match_path);
283 	if (dev) {
284 		put_device(dev);
285 		printk(KERN_WARNING "%s: %s has already been probed\n",
286 		       __func__, path);
287 		rc = -EEXIST;
288 		goto out;
289 	}
290 
291 	if ((dn = of_find_node_by_path(path))) {
292 		rc = ibmebus_create_device(dn);
293 		of_node_put(dn);
294 	} else {
295 		printk(KERN_WARNING "%s: no such device node: %s\n",
296 		       __func__, path);
297 		rc = -ENODEV;
298 	}
299 
300 out:
301 	kfree(path);
302 	if (rc)
303 		return rc;
304 	return count;
305 }
306 static BUS_ATTR_WO(probe);
307 
308 static ssize_t remove_store(const struct bus_type *bus, const char *buf, size_t count)
309 {
310 	struct device *dev;
311 	char *path;
312 
313 	path = ibmebus_chomp(buf, count);
314 	if (!path)
315 		return -ENOMEM;
316 
317 	if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
318 				   ibmebus_match_path))) {
319 		of_device_unregister(to_platform_device(dev));
320 		put_device(dev);
321 
322 		kfree(path);
323 		return count;
324 	} else {
325 		printk(KERN_WARNING "%s: %s not on the bus\n",
326 		       __func__, path);
327 
328 		kfree(path);
329 		return -ENODEV;
330 	}
331 }
332 static BUS_ATTR_WO(remove);
333 
334 static struct attribute *ibmbus_bus_attrs[] = {
335 	&bus_attr_probe.attr,
336 	&bus_attr_remove.attr,
337 	NULL,
338 };
339 ATTRIBUTE_GROUPS(ibmbus_bus);
340 
341 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
342 {
343 	const struct of_device_id *matches = drv->of_match_table;
344 
345 	if (!matches)
346 		return 0;
347 
348 	return of_match_device(matches, dev) != NULL;
349 }
350 
351 static int ibmebus_bus_device_probe(struct device *dev)
352 {
353 	int error = -ENODEV;
354 	struct platform_driver *drv;
355 	struct platform_device *of_dev;
356 
357 	drv = to_platform_driver(dev->driver);
358 	of_dev = to_platform_device(dev);
359 
360 	if (!drv->probe)
361 		return error;
362 
363 	get_device(dev);
364 
365 	if (of_driver_match_device(dev, dev->driver))
366 		error = drv->probe(of_dev);
367 	if (error)
368 		put_device(dev);
369 
370 	return error;
371 }
372 
373 static void ibmebus_bus_device_remove(struct device *dev)
374 {
375 	struct platform_device *of_dev = to_platform_device(dev);
376 	struct platform_driver *drv = to_platform_driver(dev->driver);
377 
378 	if (dev->driver && drv->remove)
379 		drv->remove(of_dev);
380 }
381 
382 static void ibmebus_bus_device_shutdown(struct device *dev)
383 {
384 	struct platform_device *of_dev = to_platform_device(dev);
385 	struct platform_driver *drv = to_platform_driver(dev->driver);
386 
387 	if (dev->driver && drv->shutdown)
388 		drv->shutdown(of_dev);
389 }
390 
391 /*
392  * ibmebus_bus_device_attrs
393  */
394 static ssize_t devspec_show(struct device *dev,
395 				struct device_attribute *attr, char *buf)
396 {
397 	struct platform_device *ofdev;
398 
399 	ofdev = to_platform_device(dev);
400 	return sprintf(buf, "%pOF\n", ofdev->dev.of_node);
401 }
402 static DEVICE_ATTR_RO(devspec);
403 
404 static ssize_t name_show(struct device *dev,
405 				struct device_attribute *attr, char *buf)
406 {
407 	struct platform_device *ofdev;
408 
409 	ofdev = to_platform_device(dev);
410 	return sprintf(buf, "%pOFn\n", ofdev->dev.of_node);
411 }
412 static DEVICE_ATTR_RO(name);
413 
414 static ssize_t modalias_show(struct device *dev,
415 				struct device_attribute *attr, char *buf)
416 {
417 	return of_device_modalias(dev, buf, PAGE_SIZE);
418 }
419 static DEVICE_ATTR_RO(modalias);
420 
421 static struct attribute *ibmebus_bus_device_attrs[] = {
422 	&dev_attr_devspec.attr,
423 	&dev_attr_name.attr,
424 	&dev_attr_modalias.attr,
425 	NULL,
426 };
427 ATTRIBUTE_GROUPS(ibmebus_bus_device);
428 
429 static int ibmebus_bus_modalias(const struct device *dev, struct kobj_uevent_env *env)
430 {
431 	return of_device_uevent_modalias(dev, env);
432 }
433 
434 struct bus_type ibmebus_bus_type = {
435 	.name      = "ibmebus",
436 	.uevent    = ibmebus_bus_modalias,
437 	.bus_groups = ibmbus_bus_groups,
438 	.match     = ibmebus_bus_bus_match,
439 	.probe     = ibmebus_bus_device_probe,
440 	.remove    = ibmebus_bus_device_remove,
441 	.shutdown  = ibmebus_bus_device_shutdown,
442 	.dev_groups = ibmebus_bus_device_groups,
443 };
444 EXPORT_SYMBOL(ibmebus_bus_type);
445 
446 static int __init ibmebus_bus_init(void)
447 {
448 	int err;
449 
450 	printk(KERN_INFO "IBM eBus Device Driver\n");
451 
452 	err = bus_register(&ibmebus_bus_type);
453 	if (err) {
454 		printk(KERN_ERR "%s: failed to register IBM eBus.\n",
455 		       __func__);
456 		return err;
457 	}
458 
459 	err = device_register(&ibmebus_bus_device);
460 	if (err) {
461 		printk(KERN_WARNING "%s: device_register returned %i\n",
462 		       __func__, err);
463 		bus_unregister(&ibmebus_bus_type);
464 
465 		return err;
466 	}
467 
468 	err = ibmebus_create_devices(ibmebus_matches);
469 	if (err) {
470 		device_unregister(&ibmebus_bus_device);
471 		bus_unregister(&ibmebus_bus_type);
472 		return err;
473 	}
474 
475 	return 0;
476 }
477 machine_postcore_initcall(pseries, ibmebus_bus_init);
478