hc
2024-09-20 a36159eec6ca17402b0e146b86efaf76568dc353
kernel/arch/powerpc/platforms/pseries/vio.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * IBM PowerPC Virtual I/O Infrastructure Support.
34 *
....@@ -7,11 +8,6 @@
78 * Hollis Blanchard <hollisb@us.ibm.com>
89 * Stephen Rothwell
910 * Robert Jennings <rcjenn@us.ibm.com>
10
- *
11
- * This program is free software; you can redistribute it and/or
12
- * modify it under the terms of the GNU General Public License
13
- * as published by the Free Software Foundation; either version
14
- * 2 of the License, or (at your option) any later version.
1511 */
1612
1713 #include <linux/cpu.h>
....@@ -24,7 +20,7 @@
2420 #include <linux/console.h>
2521 #include <linux/export.h>
2622 #include <linux/mm.h>
27
-#include <linux/dma-mapping.h>
23
+#include <linux/dma-map-ops.h>
2824 #include <linux/kobject.h>
2925
3026 #include <asm/iommu.h>
....@@ -35,6 +31,7 @@
3531 #include <asm/tce.h>
3632 #include <asm/page.h>
3733 #include <asm/hvcall.h>
34
+#include <asm/machdep.h>
3835
3936 static struct vio_dev vio_bus_device = { /* fake "parent" device */
4037 .name = "vio",
....@@ -492,7 +489,9 @@
492489 return NULL;
493490 }
494491
495
- ret = dma_iommu_ops.alloc(dev, size, dma_handle, flag, attrs);
492
+ ret = iommu_alloc_coherent(dev, get_iommu_table_base(dev), size,
493
+ dma_handle, dev->coherent_dma_mask, flag,
494
+ dev_to_node(dev));
496495 if (unlikely(ret == NULL)) {
497496 vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
498497 atomic_inc(&viodev->cmo.allocs_failed);
....@@ -507,8 +506,7 @@
507506 {
508507 struct vio_dev *viodev = to_vio_dev(dev);
509508
510
- dma_iommu_ops.free(dev, size, vaddr, dma_handle, attrs);
511
-
509
+ iommu_free_coherent(get_iommu_table_base(dev), size, vaddr, dma_handle);
512510 vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
513511 }
514512
....@@ -518,22 +516,22 @@
518516 unsigned long attrs)
519517 {
520518 struct vio_dev *viodev = to_vio_dev(dev);
521
- struct iommu_table *tbl;
522
- dma_addr_t ret = IOMMU_MAPPING_ERROR;
519
+ struct iommu_table *tbl = get_iommu_table_base(dev);
520
+ dma_addr_t ret = DMA_MAPPING_ERROR;
523521
524
- tbl = get_iommu_table_base(dev);
525
- if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)))) {
526
- atomic_inc(&viodev->cmo.allocs_failed);
527
- return ret;
528
- }
529
-
530
- ret = dma_iommu_ops.map_page(dev, page, offset, size, direction, attrs);
531
- if (unlikely(dma_mapping_error(dev, ret))) {
532
- vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
533
- atomic_inc(&viodev->cmo.allocs_failed);
534
- }
535
-
522
+ if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl))))
523
+ goto out_fail;
524
+ ret = iommu_map_page(dev, tbl, page, offset, size, dma_get_mask(dev),
525
+ direction, attrs);
526
+ if (unlikely(ret == DMA_MAPPING_ERROR))
527
+ goto out_deallocate;
536528 return ret;
529
+
530
+out_deallocate:
531
+ vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
532
+out_fail:
533
+ atomic_inc(&viodev->cmo.allocs_failed);
534
+ return DMA_MAPPING_ERROR;
537535 }
538536
539537 static void vio_dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
....@@ -542,11 +540,9 @@
542540 unsigned long attrs)
543541 {
544542 struct vio_dev *viodev = to_vio_dev(dev);
545
- struct iommu_table *tbl;
543
+ struct iommu_table *tbl = get_iommu_table_base(dev);
546544
547
- tbl = get_iommu_table_base(dev);
548
- dma_iommu_ops.unmap_page(dev, dma_handle, size, direction, attrs);
549
-
545
+ iommu_unmap_page(tbl, dma_handle, size, direction, attrs);
550546 vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
551547 }
552548
....@@ -555,34 +551,32 @@
555551 unsigned long attrs)
556552 {
557553 struct vio_dev *viodev = to_vio_dev(dev);
558
- struct iommu_table *tbl;
554
+ struct iommu_table *tbl = get_iommu_table_base(dev);
559555 struct scatterlist *sgl;
560556 int ret, count;
561557 size_t alloc_size = 0;
562558
563
- tbl = get_iommu_table_base(dev);
564559 for_each_sg(sglist, sgl, nelems, count)
565560 alloc_size += roundup(sgl->length, IOMMU_PAGE_SIZE(tbl));
566561
567
- if (vio_cmo_alloc(viodev, alloc_size)) {
568
- atomic_inc(&viodev->cmo.allocs_failed);
569
- return 0;
570
- }
571
-
572
- ret = dma_iommu_ops.map_sg(dev, sglist, nelems, direction, attrs);
573
-
574
- if (unlikely(!ret)) {
575
- vio_cmo_dealloc(viodev, alloc_size);
576
- atomic_inc(&viodev->cmo.allocs_failed);
577
- return ret;
578
- }
562
+ if (vio_cmo_alloc(viodev, alloc_size))
563
+ goto out_fail;
564
+ ret = ppc_iommu_map_sg(dev, tbl, sglist, nelems, dma_get_mask(dev),
565
+ direction, attrs);
566
+ if (unlikely(!ret))
567
+ goto out_deallocate;
579568
580569 for_each_sg(sglist, sgl, ret, count)
581570 alloc_size -= roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
582571 if (alloc_size)
583572 vio_cmo_dealloc(viodev, alloc_size);
584
-
585573 return ret;
574
+
575
+out_deallocate:
576
+ vio_cmo_dealloc(viodev, alloc_size);
577
+out_fail:
578
+ atomic_inc(&viodev->cmo.allocs_failed);
579
+ return 0;
586580 }
587581
588582 static void vio_dma_iommu_unmap_sg(struct device *dev,
....@@ -591,41 +585,31 @@
591585 unsigned long attrs)
592586 {
593587 struct vio_dev *viodev = to_vio_dev(dev);
594
- struct iommu_table *tbl;
588
+ struct iommu_table *tbl = get_iommu_table_base(dev);
595589 struct scatterlist *sgl;
596590 size_t alloc_size = 0;
597591 int count;
598592
599
- tbl = get_iommu_table_base(dev);
600593 for_each_sg(sglist, sgl, nelems, count)
601594 alloc_size += roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
602595
603
- dma_iommu_ops.unmap_sg(dev, sglist, nelems, direction, attrs);
604
-
596
+ ppc_iommu_unmap_sg(tbl, sglist, nelems, direction, attrs);
605597 vio_cmo_dealloc(viodev, alloc_size);
606
-}
607
-
608
-static int vio_dma_iommu_dma_supported(struct device *dev, u64 mask)
609
-{
610
- return dma_iommu_ops.dma_supported(dev, mask);
611
-}
612
-
613
-static u64 vio_dma_get_required_mask(struct device *dev)
614
-{
615
- return dma_iommu_ops.get_required_mask(dev);
616598 }
617599
618600 static const struct dma_map_ops vio_dma_mapping_ops = {
619601 .alloc = vio_dma_iommu_alloc_coherent,
620602 .free = vio_dma_iommu_free_coherent,
621
- .mmap = dma_nommu_mmap_coherent,
622603 .map_sg = vio_dma_iommu_map_sg,
623604 .unmap_sg = vio_dma_iommu_unmap_sg,
624605 .map_page = vio_dma_iommu_map_page,
625606 .unmap_page = vio_dma_iommu_unmap_page,
626
- .dma_supported = vio_dma_iommu_dma_supported,
627
- .get_required_mask = vio_dma_get_required_mask,
628
- .mapping_error = dma_iommu_mapping_error,
607
+ .dma_supported = dma_iommu_dma_supported,
608
+ .get_required_mask = dma_iommu_get_required_mask,
609
+ .mmap = dma_common_mmap,
610
+ .get_sgtable = dma_common_get_sgtable,
611
+ .alloc_pages = dma_common_alloc_pages,
612
+ .free_pages = dma_common_free_pages,
629613 };
630614
631615 /**
....@@ -1214,7 +1198,7 @@
12141198 else
12151199 tbl->it_ops = &iommu_table_pseries_ops;
12161200
1217
- return iommu_init_table(tbl, -1);
1201
+ return iommu_init_table(tbl, -1, 0, 0);
12181202 }
12191203
12201204 /**
....@@ -1302,6 +1286,10 @@
13021286 int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
13031287 const char *mod_name)
13041288 {
1289
+ // vio_bus_type is only initialised for pseries
1290
+ if (!machine_is(pseries))
1291
+ return -ENODEV;
1292
+
13051293 pr_debug("%s: driver %s registering\n", __func__, viodrv->name);
13061294
13071295 /* fill in 'struct driver' fields */
....@@ -1351,7 +1339,6 @@
13511339 struct device_node *parent_node;
13521340 const __be32 *prop;
13531341 enum vio_dev_family family;
1354
- const char *of_node_name = of_node->name ? of_node->name : "<unknown>";
13551342
13561343 /*
13571344 * Determine if this node is a under the /vdevice node or under the
....@@ -1359,29 +1346,29 @@
13591346 */
13601347 parent_node = of_get_parent(of_node);
13611348 if (parent_node) {
1362
- if (!strcmp(parent_node->type, "ibm,platform-facilities"))
1349
+ if (of_node_is_type(parent_node, "ibm,platform-facilities"))
13631350 family = PFO;
1364
- else if (!strcmp(parent_node->type, "vdevice"))
1351
+ else if (of_node_is_type(parent_node, "vdevice"))
13651352 family = VDEVICE;
13661353 else {
1367
- pr_warn("%s: parent(%pOF) of %s not recognized.\n",
1354
+ pr_warn("%s: parent(%pOF) of %pOFn not recognized.\n",
13681355 __func__,
13691356 parent_node,
1370
- of_node_name);
1357
+ of_node);
13711358 of_node_put(parent_node);
13721359 return NULL;
13731360 }
13741361 of_node_put(parent_node);
13751362 } else {
1376
- pr_warn("%s: could not determine the parent of node %s.\n",
1377
- __func__, of_node_name);
1363
+ pr_warn("%s: could not determine the parent of node %pOFn.\n",
1364
+ __func__, of_node);
13781365 return NULL;
13791366 }
13801367
13811368 if (family == PFO) {
13821369 if (of_get_property(of_node, "interrupt-controller", NULL)) {
1383
- pr_debug("%s: Skipping the interrupt controller %s.\n",
1384
- __func__, of_node_name);
1370
+ pr_debug("%s: Skipping the interrupt controller %pOFn.\n",
1371
+ __func__, of_node);
13851372 return NULL;
13861373 }
13871374 }
....@@ -1398,18 +1385,17 @@
13981385 if (viodev->family == VDEVICE) {
13991386 unsigned int unit_address;
14001387
1401
- if (of_node->type != NULL)
1402
- viodev->type = of_node->type;
1403
- else {
1404
- pr_warn("%s: node %s is missing the 'device_type' "
1405
- "property.\n", __func__, of_node_name);
1388
+ viodev->type = of_node_get_device_type(of_node);
1389
+ if (!viodev->type) {
1390
+ pr_warn("%s: node %pOFn is missing the 'device_type' "
1391
+ "property.\n", __func__, of_node);
14061392 goto out;
14071393 }
14081394
14091395 prop = of_get_property(of_node, "reg", NULL);
14101396 if (prop == NULL) {
1411
- pr_warn("%s: node %s missing 'reg'\n",
1412
- __func__, of_node_name);
1397
+ pr_warn("%s: node %pOFn missing 'reg'\n",
1398
+ __func__, of_node);
14131399 goto out;
14141400 }
14151401 unit_address = of_read_number(prop, 1);
....@@ -1424,8 +1410,8 @@
14241410 if (prop != NULL)
14251411 viodev->resource_id = of_read_number(prop, 1);
14261412
1427
- dev_set_name(&viodev->dev, "%s", of_node_name);
1428
- viodev->type = of_node_name;
1413
+ dev_set_name(&viodev->dev, "%pOFn", of_node);
1414
+ viodev->type = dev_name(&viodev->dev);
14291415 viodev->irq = 0;
14301416 }
14311417
....@@ -1534,7 +1520,7 @@
15341520
15351521 return 0;
15361522 }
1537
-postcore_initcall(vio_bus_init);
1523
+machine_postcore_initcall(pseries, vio_bus_init);
15381524
15391525 static int __init vio_device_init(void)
15401526 {
....@@ -1543,7 +1529,7 @@
15431529
15441530 return 0;
15451531 }
1546
-device_initcall(vio_device_init);
1532
+machine_device_initcall(pseries, vio_device_init);
15471533
15481534 static ssize_t name_show(struct device *dev,
15491535 struct device_attribute *attr, char *buf)
....@@ -1649,7 +1635,6 @@
16491635 }
16501636 EXPORT_SYMBOL(vio_get_attribute);
16511637
1652
-#ifdef CONFIG_PPC_PSERIES
16531638 /* vio_find_name() - internal because only vio.c knows how we formatted the
16541639 * kobject name
16551640 */
....@@ -1675,32 +1660,30 @@
16751660 {
16761661 char kobj_name[20];
16771662 struct device_node *vnode_parent;
1678
- const char *dev_type;
16791663
16801664 vnode_parent = of_get_parent(vnode);
16811665 if (!vnode_parent)
16821666 return NULL;
16831667
1684
- dev_type = of_get_property(vnode_parent, "device_type", NULL);
1685
- of_node_put(vnode_parent);
1686
- if (!dev_type)
1687
- return NULL;
1688
-
16891668 /* construct the kobject name from the device node */
1690
- if (!strcmp(dev_type, "vdevice")) {
1669
+ if (of_node_is_type(vnode_parent, "vdevice")) {
16911670 const __be32 *prop;
16921671
16931672 prop = of_get_property(vnode, "reg", NULL);
16941673 if (!prop)
1695
- return NULL;
1674
+ goto out;
16961675 snprintf(kobj_name, sizeof(kobj_name), "%x",
16971676 (uint32_t)of_read_number(prop, 1));
1698
- } else if (!strcmp(dev_type, "ibm,platform-facilities"))
1699
- snprintf(kobj_name, sizeof(kobj_name), "%s", vnode->name);
1677
+ } else if (of_node_is_type(vnode_parent, "ibm,platform-facilities"))
1678
+ snprintf(kobj_name, sizeof(kobj_name), "%pOFn", vnode);
17001679 else
1701
- return NULL;
1680
+ goto out;
17021681
1682
+ of_node_put(vnode_parent);
17031683 return vio_find_name(kobj_name);
1684
+out:
1685
+ of_node_put(vnode_parent);
1686
+ return NULL;
17041687 }
17051688 EXPORT_SYMBOL(vio_find_node);
17061689
....@@ -1721,4 +1704,10 @@
17211704 return rc;
17221705 }
17231706 EXPORT_SYMBOL(vio_disable_interrupts);
1724
-#endif /* CONFIG_PPC_PSERIES */
1707
+
1708
+static int __init vio_init(void)
1709
+{
1710
+ dma_debug_add_bus(&vio_bus_type);
1711
+ return 0;
1712
+}
1713
+machine_fs_initcall(pseries, vio_init);