powerpc: Only save/restore SDR1 if in hypervisor mode
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / powerpc / kernel / ibmebus.c
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-mapping.h>
44 #include <linux/interrupt.h>
45 #include <linux/of.h>
46 #include <linux/slab.h>
47 #include <linux/stat.h>
48 #include <linux/of_platform.h>
49 #include <asm/ibmebus.h>
50
51 static struct device ibmebus_bus_device = { /* fake "parent" device */
52         .init_name = "ibmebus",
53 };
54
55 struct bus_type ibmebus_bus_type;
56
57 /* These devices will automatically be added to the bus during init */
58 static struct of_device_id __initdata ibmebus_matches[] = {
59         { .compatible = "IBM,lhca" },
60         { .compatible = "IBM,lhea" },
61         {},
62 };
63
64 static void *ibmebus_alloc_coherent(struct device *dev,
65                                     size_t size,
66                                     dma_addr_t *dma_handle,
67                                     gfp_t flag,
68                                     struct dma_attrs *attrs)
69 {
70         void *mem;
71
72         mem = kmalloc(size, flag);
73         *dma_handle = (dma_addr_t)mem;
74
75         return mem;
76 }
77
78 static void ibmebus_free_coherent(struct device *dev,
79                                   size_t size, void *vaddr,
80                                   dma_addr_t dma_handle,
81                                   struct dma_attrs *attrs)
82 {
83         kfree(vaddr);
84 }
85
86 static dma_addr_t ibmebus_map_page(struct device *dev,
87                                    struct page *page,
88                                    unsigned long offset,
89                                    size_t size,
90                                    enum dma_data_direction direction,
91                                    struct dma_attrs *attrs)
92 {
93         return (dma_addr_t)(page_address(page) + offset);
94 }
95
96 static void ibmebus_unmap_page(struct device *dev,
97                                dma_addr_t dma_addr,
98                                size_t size,
99                                enum dma_data_direction direction,
100                                struct dma_attrs *attrs)
101 {
102         return;
103 }
104
105 static int ibmebus_map_sg(struct device *dev,
106                           struct scatterlist *sgl,
107                           int nents, enum dma_data_direction direction,
108                           struct dma_attrs *attrs)
109 {
110         struct scatterlist *sg;
111         int i;
112
113         for_each_sg(sgl, sg, nents, i) {
114                 sg->dma_address = (dma_addr_t) sg_virt(sg);
115                 sg->dma_length = sg->length;
116         }
117
118         return nents;
119 }
120
121 static void ibmebus_unmap_sg(struct device *dev,
122                              struct scatterlist *sg,
123                              int nents, enum dma_data_direction direction,
124                              struct dma_attrs *attrs)
125 {
126         return;
127 }
128
129 static int ibmebus_dma_supported(struct device *dev, u64 mask)
130 {
131         return mask == DMA_BIT_MASK(64);
132 }
133
134 static u64 ibmebus_dma_get_required_mask(struct device *dev)
135 {
136         return DMA_BIT_MASK(64);
137 }
138
139 static struct dma_map_ops ibmebus_dma_ops = {
140         .alloc              = ibmebus_alloc_coherent,
141         .free               = ibmebus_free_coherent,
142         .map_sg             = ibmebus_map_sg,
143         .unmap_sg           = ibmebus_unmap_sg,
144         .dma_supported      = ibmebus_dma_supported,
145         .get_required_mask  = ibmebus_dma_get_required_mask,
146         .map_page           = ibmebus_map_page,
147         .unmap_page         = ibmebus_unmap_page,
148 };
149
150 static int ibmebus_match_path(struct device *dev, void *data)
151 {
152         struct device_node *dn = to_platform_device(dev)->dev.of_node;
153         return (dn->full_name &&
154                 (strcasecmp((char *)data, dn->full_name) == 0));
155 }
156
157 static int ibmebus_match_node(struct device *dev, void *data)
158 {
159         return to_platform_device(dev)->dev.of_node == data;
160 }
161
162 static int ibmebus_create_device(struct device_node *dn)
163 {
164         struct platform_device *dev;
165         int ret;
166
167         dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
168         if (!dev)
169                 return -ENOMEM;
170
171         dev->dev.bus = &ibmebus_bus_type;
172         dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
173
174         ret = of_device_add(dev);
175         if (ret)
176                 platform_device_put(dev);
177         return ret;
178 }
179
180 static int ibmebus_create_devices(const struct of_device_id *matches)
181 {
182         struct device_node *root, *child;
183         int ret = 0;
184
185         root = of_find_node_by_path("/");
186
187         for_each_child_of_node(root, child) {
188                 if (!of_match_node(matches, child))
189                         continue;
190
191                 if (bus_find_device(&ibmebus_bus_type, NULL, child,
192                                     ibmebus_match_node))
193                         continue;
194
195                 ret = ibmebus_create_device(child);
196                 if (ret) {
197                         printk(KERN_ERR "%s: failed to create device (%i)",
198                                __func__, ret);
199                         of_node_put(child);
200                         break;
201                 }
202         }
203
204         of_node_put(root);
205         return ret;
206 }
207
208 int ibmebus_register_driver(struct platform_driver *drv)
209 {
210         /* If the driver uses devices that ibmebus doesn't know, add them */
211         ibmebus_create_devices(drv->driver.of_match_table);
212
213         drv->driver.bus = &ibmebus_bus_type;
214         return driver_register(&drv->driver);
215 }
216 EXPORT_SYMBOL(ibmebus_register_driver);
217
218 void ibmebus_unregister_driver(struct platform_driver *drv)
219 {
220         driver_unregister(&drv->driver);
221 }
222 EXPORT_SYMBOL(ibmebus_unregister_driver);
223
224 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
225                         unsigned long irq_flags, const char *devname,
226                         void *dev_id)
227 {
228         unsigned int irq = irq_create_mapping(NULL, ist);
229
230         if (irq == NO_IRQ)
231                 return -EINVAL;
232
233         return request_irq(irq, handler, irq_flags, devname, dev_id);
234 }
235 EXPORT_SYMBOL(ibmebus_request_irq);
236
237 void ibmebus_free_irq(u32 ist, void *dev_id)
238 {
239         unsigned int irq = irq_find_mapping(NULL, ist);
240
241         free_irq(irq, dev_id);
242         irq_dispose_mapping(irq);
243 }
244 EXPORT_SYMBOL(ibmebus_free_irq);
245
246 static char *ibmebus_chomp(const char *in, size_t count)
247 {
248         char *out = kmalloc(count + 1, GFP_KERNEL);
249
250         if (!out)
251                 return NULL;
252
253         memcpy(out, in, count);
254         out[count] = '\0';
255         if (out[count - 1] == '\n')
256                 out[count - 1] = '\0';
257
258         return out;
259 }
260
261 static ssize_t ibmebus_store_probe(struct bus_type *bus,
262                                    const char *buf, size_t count)
263 {
264         struct device_node *dn = NULL;
265         char *path;
266         ssize_t rc = 0;
267
268         path = ibmebus_chomp(buf, count);
269         if (!path)
270                 return -ENOMEM;
271
272         if (bus_find_device(&ibmebus_bus_type, NULL, path,
273                             ibmebus_match_path)) {
274                 printk(KERN_WARNING "%s: %s has already been probed\n",
275                        __func__, path);
276                 rc = -EEXIST;
277                 goto out;
278         }
279
280         if ((dn = of_find_node_by_path(path))) {
281                 rc = ibmebus_create_device(dn);
282                 of_node_put(dn);
283         } else {
284                 printk(KERN_WARNING "%s: no such device node: %s\n",
285                        __func__, path);
286                 rc = -ENODEV;
287         }
288
289 out:
290         kfree(path);
291         if (rc)
292                 return rc;
293         return count;
294 }
295
296 static ssize_t ibmebus_store_remove(struct bus_type *bus,
297                                     const char *buf, size_t count)
298 {
299         struct device *dev;
300         char *path;
301
302         path = ibmebus_chomp(buf, count);
303         if (!path)
304                 return -ENOMEM;
305
306         if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
307                                    ibmebus_match_path))) {
308                 of_device_unregister(to_platform_device(dev));
309
310                 kfree(path);
311                 return count;
312         } else {
313                 printk(KERN_WARNING "%s: %s not on the bus\n",
314                        __func__, path);
315
316                 kfree(path);
317                 return -ENODEV;
318         }
319 }
320
321
322 static struct bus_attribute ibmebus_bus_attrs[] = {
323         __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
324         __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
325         __ATTR_NULL
326 };
327
328 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
329 {
330         const struct of_device_id *matches = drv->of_match_table;
331
332         if (!matches)
333                 return 0;
334
335         return of_match_device(matches, dev) != NULL;
336 }
337
338 static int ibmebus_bus_device_probe(struct device *dev)
339 {
340         int error = -ENODEV;
341         struct platform_driver *drv;
342         struct platform_device *of_dev;
343
344         drv = to_platform_driver(dev->driver);
345         of_dev = to_platform_device(dev);
346
347         if (!drv->probe)
348                 return error;
349
350         of_dev_get(of_dev);
351
352         if (of_driver_match_device(dev, dev->driver))
353                 error = drv->probe(of_dev);
354         if (error)
355                 of_dev_put(of_dev);
356
357         return error;
358 }
359
360 static int ibmebus_bus_device_remove(struct device *dev)
361 {
362         struct platform_device *of_dev = to_platform_device(dev);
363         struct platform_driver *drv = to_platform_driver(dev->driver);
364
365         if (dev->driver && drv->remove)
366                 drv->remove(of_dev);
367         return 0;
368 }
369
370 static void ibmebus_bus_device_shutdown(struct device *dev)
371 {
372         struct platform_device *of_dev = to_platform_device(dev);
373         struct platform_driver *drv = to_platform_driver(dev->driver);
374
375         if (dev->driver && drv->shutdown)
376                 drv->shutdown(of_dev);
377 }
378
379 /*
380  * ibmebus_bus_device_attrs
381  */
382 static ssize_t devspec_show(struct device *dev,
383                                 struct device_attribute *attr, char *buf)
384 {
385         struct platform_device *ofdev;
386
387         ofdev = to_platform_device(dev);
388         return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
389 }
390
391 static ssize_t name_show(struct device *dev,
392                                 struct device_attribute *attr, char *buf)
393 {
394         struct platform_device *ofdev;
395
396         ofdev = to_platform_device(dev);
397         return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
398 }
399
400 static ssize_t modalias_show(struct device *dev,
401                                 struct device_attribute *attr, char *buf)
402 {
403         ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
404         buf[len] = '\n';
405         buf[len+1] = 0;
406         return len+1;
407 }
408
409 struct device_attribute ibmebus_bus_device_attrs[] = {
410         __ATTR_RO(devspec),
411         __ATTR_RO(name),
412         __ATTR_RO(modalias),
413         __ATTR_NULL
414 };
415
416 #ifdef CONFIG_PM_SLEEP
417 static int ibmebus_bus_legacy_suspend(struct device *dev, pm_message_t mesg)
418 {
419         struct platform_device *of_dev = to_platform_device(dev);
420         struct platform_driver *drv = to_platform_driver(dev->driver);
421         int ret = 0;
422
423         if (dev->driver && drv->suspend)
424                 ret = drv->suspend(of_dev, mesg);
425         return ret;
426 }
427
428 static int ibmebus_bus_legacy_resume(struct device *dev)
429 {
430         struct platform_device *of_dev = to_platform_device(dev);
431         struct platform_driver *drv = to_platform_driver(dev->driver);
432         int ret = 0;
433
434         if (dev->driver && drv->resume)
435                 ret = drv->resume(of_dev);
436         return ret;
437 }
438
439 static int ibmebus_bus_pm_prepare(struct device *dev)
440 {
441         struct device_driver *drv = dev->driver;
442         int ret = 0;
443
444         if (drv && drv->pm && drv->pm->prepare)
445                 ret = drv->pm->prepare(dev);
446
447         return ret;
448 }
449
450 static void ibmebus_bus_pm_complete(struct device *dev)
451 {
452         struct device_driver *drv = dev->driver;
453
454         if (drv && drv->pm && drv->pm->complete)
455                 drv->pm->complete(dev);
456 }
457
458 #ifdef CONFIG_SUSPEND
459
460 static int ibmebus_bus_pm_suspend(struct device *dev)
461 {
462         struct device_driver *drv = dev->driver;
463         int ret = 0;
464
465         if (!drv)
466                 return 0;
467
468         if (drv->pm) {
469                 if (drv->pm->suspend)
470                         ret = drv->pm->suspend(dev);
471         } else {
472                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_SUSPEND);
473         }
474
475         return ret;
476 }
477
478 static int ibmebus_bus_pm_suspend_noirq(struct device *dev)
479 {
480         struct device_driver *drv = dev->driver;
481         int ret = 0;
482
483         if (!drv)
484                 return 0;
485
486         if (drv->pm) {
487                 if (drv->pm->suspend_noirq)
488                         ret = drv->pm->suspend_noirq(dev);
489         }
490
491         return ret;
492 }
493
494 static int ibmebus_bus_pm_resume(struct device *dev)
495 {
496         struct device_driver *drv = dev->driver;
497         int ret = 0;
498
499         if (!drv)
500                 return 0;
501
502         if (drv->pm) {
503                 if (drv->pm->resume)
504                         ret = drv->pm->resume(dev);
505         } else {
506                 ret = ibmebus_bus_legacy_resume(dev);
507         }
508
509         return ret;
510 }
511
512 static int ibmebus_bus_pm_resume_noirq(struct device *dev)
513 {
514         struct device_driver *drv = dev->driver;
515         int ret = 0;
516
517         if (!drv)
518                 return 0;
519
520         if (drv->pm) {
521                 if (drv->pm->resume_noirq)
522                         ret = drv->pm->resume_noirq(dev);
523         }
524
525         return ret;
526 }
527
528 #else /* !CONFIG_SUSPEND */
529
530 #define ibmebus_bus_pm_suspend          NULL
531 #define ibmebus_bus_pm_resume           NULL
532 #define ibmebus_bus_pm_suspend_noirq    NULL
533 #define ibmebus_bus_pm_resume_noirq     NULL
534
535 #endif /* !CONFIG_SUSPEND */
536
537 #ifdef CONFIG_HIBERNATE_CALLBACKS
538
539 static int ibmebus_bus_pm_freeze(struct device *dev)
540 {
541         struct device_driver *drv = dev->driver;
542         int ret = 0;
543
544         if (!drv)
545                 return 0;
546
547         if (drv->pm) {
548                 if (drv->pm->freeze)
549                         ret = drv->pm->freeze(dev);
550         } else {
551                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_FREEZE);
552         }
553
554         return ret;
555 }
556
557 static int ibmebus_bus_pm_freeze_noirq(struct device *dev)
558 {
559         struct device_driver *drv = dev->driver;
560         int ret = 0;
561
562         if (!drv)
563                 return 0;
564
565         if (drv->pm) {
566                 if (drv->pm->freeze_noirq)
567                         ret = drv->pm->freeze_noirq(dev);
568         }
569
570         return ret;
571 }
572
573 static int ibmebus_bus_pm_thaw(struct device *dev)
574 {
575         struct device_driver *drv = dev->driver;
576         int ret = 0;
577
578         if (!drv)
579                 return 0;
580
581         if (drv->pm) {
582                 if (drv->pm->thaw)
583                         ret = drv->pm->thaw(dev);
584         } else {
585                 ret = ibmebus_bus_legacy_resume(dev);
586         }
587
588         return ret;
589 }
590
591 static int ibmebus_bus_pm_thaw_noirq(struct device *dev)
592 {
593         struct device_driver *drv = dev->driver;
594         int ret = 0;
595
596         if (!drv)
597                 return 0;
598
599         if (drv->pm) {
600                 if (drv->pm->thaw_noirq)
601                         ret = drv->pm->thaw_noirq(dev);
602         }
603
604         return ret;
605 }
606
607 static int ibmebus_bus_pm_poweroff(struct device *dev)
608 {
609         struct device_driver *drv = dev->driver;
610         int ret = 0;
611
612         if (!drv)
613                 return 0;
614
615         if (drv->pm) {
616                 if (drv->pm->poweroff)
617                         ret = drv->pm->poweroff(dev);
618         } else {
619                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_HIBERNATE);
620         }
621
622         return ret;
623 }
624
625 static int ibmebus_bus_pm_poweroff_noirq(struct device *dev)
626 {
627         struct device_driver *drv = dev->driver;
628         int ret = 0;
629
630         if (!drv)
631                 return 0;
632
633         if (drv->pm) {
634                 if (drv->pm->poweroff_noirq)
635                         ret = drv->pm->poweroff_noirq(dev);
636         }
637
638         return ret;
639 }
640
641 static int ibmebus_bus_pm_restore(struct device *dev)
642 {
643         struct device_driver *drv = dev->driver;
644         int ret = 0;
645
646         if (!drv)
647                 return 0;
648
649         if (drv->pm) {
650                 if (drv->pm->restore)
651                         ret = drv->pm->restore(dev);
652         } else {
653                 ret = ibmebus_bus_legacy_resume(dev);
654         }
655
656         return ret;
657 }
658
659 static int ibmebus_bus_pm_restore_noirq(struct device *dev)
660 {
661         struct device_driver *drv = dev->driver;
662         int ret = 0;
663
664         if (!drv)
665                 return 0;
666
667         if (drv->pm) {
668                 if (drv->pm->restore_noirq)
669                         ret = drv->pm->restore_noirq(dev);
670         }
671
672         return ret;
673 }
674
675 #else /* !CONFIG_HIBERNATE_CALLBACKS */
676
677 #define ibmebus_bus_pm_freeze           NULL
678 #define ibmebus_bus_pm_thaw             NULL
679 #define ibmebus_bus_pm_poweroff         NULL
680 #define ibmebus_bus_pm_restore          NULL
681 #define ibmebus_bus_pm_freeze_noirq     NULL
682 #define ibmebus_bus_pm_thaw_noirq               NULL
683 #define ibmebus_bus_pm_poweroff_noirq   NULL
684 #define ibmebus_bus_pm_restore_noirq    NULL
685
686 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
687
688 static struct dev_pm_ops ibmebus_bus_dev_pm_ops = {
689         .prepare = ibmebus_bus_pm_prepare,
690         .complete = ibmebus_bus_pm_complete,
691         .suspend = ibmebus_bus_pm_suspend,
692         .resume = ibmebus_bus_pm_resume,
693         .freeze = ibmebus_bus_pm_freeze,
694         .thaw = ibmebus_bus_pm_thaw,
695         .poweroff = ibmebus_bus_pm_poweroff,
696         .restore = ibmebus_bus_pm_restore,
697         .suspend_noirq = ibmebus_bus_pm_suspend_noirq,
698         .resume_noirq = ibmebus_bus_pm_resume_noirq,
699         .freeze_noirq = ibmebus_bus_pm_freeze_noirq,
700         .thaw_noirq = ibmebus_bus_pm_thaw_noirq,
701         .poweroff_noirq = ibmebus_bus_pm_poweroff_noirq,
702         .restore_noirq = ibmebus_bus_pm_restore_noirq,
703 };
704
705 #define IBMEBUS_BUS_PM_OPS_PTR  (&ibmebus_bus_dev_pm_ops)
706
707 #else /* !CONFIG_PM_SLEEP */
708
709 #define IBMEBUS_BUS_PM_OPS_PTR  NULL
710
711 #endif /* !CONFIG_PM_SLEEP */
712
713 struct bus_type ibmebus_bus_type = {
714         .name      = "ibmebus",
715         .uevent    = of_device_uevent_modalias,
716         .bus_attrs = ibmebus_bus_attrs,
717         .match     = ibmebus_bus_bus_match,
718         .probe     = ibmebus_bus_device_probe,
719         .remove    = ibmebus_bus_device_remove,
720         .shutdown  = ibmebus_bus_device_shutdown,
721         .dev_attrs = ibmebus_bus_device_attrs,
722         .pm        = IBMEBUS_BUS_PM_OPS_PTR,
723 };
724 EXPORT_SYMBOL(ibmebus_bus_type);
725
726 static int __init ibmebus_bus_init(void)
727 {
728         int err;
729
730         printk(KERN_INFO "IBM eBus Device Driver\n");
731
732         err = bus_register(&ibmebus_bus_type);
733         if (err) {
734                 printk(KERN_ERR "%s: failed to register IBM eBus.\n",
735                        __func__);
736                 return err;
737         }
738
739         err = device_register(&ibmebus_bus_device);
740         if (err) {
741                 printk(KERN_WARNING "%s: device_register returned %i\n",
742                        __func__, err);
743                 bus_unregister(&ibmebus_bus_type);
744
745                 return err;
746         }
747
748         err = ibmebus_create_devices(ibmebus_matches);
749         if (err) {
750                 device_unregister(&ibmebus_bus_device);
751                 bus_unregister(&ibmebus_bus_type);
752                 return err;
753         }
754
755         return 0;
756 }
757 postcore_initcall(ibmebus_bus_init);