firmware: arm_scpi: Revert updates made during v4.15 merge window

Revert "Merge tag 'scpi-updates-4.15' of git://git.kernel.org/pub/scm/linux/kernel/git/sudeep.holla/linux into next/drivers"

Paraphrased from email from Kevin Hilman:

Revert ARM SCPI changes since v4.14.

Untested changes caused regressions in SCPI and CPUfreq/DVFS failures
on most Amlogic SoCs. Changes reverted for v4.15 so they can be better
reviewed and tested.

These ARM SCPI changes caused SCPI regressions resulting in CPUfreq
failures on most Amlogic SoCs (found by kernelci.org.)

Unfortunately, this was not caught in linux-next due to other bugs/panics
on these platforms masking this problem so we've only found it since
we've fixed the other issues.

Since we're already in the -rc cycle, I'd prefer to revert to a known
working state (that of v4.14) rather than finding/reverting a subset,
which would just lead to another untested state.

These changes can then have some time to be better reviewed and tested
and resubmitted for v4.16.

Kevin Hilman has tested this revert on the affected Amlogic SoCs and
verified that we're back to the previous (working) condition.

This reverts commit 6710acf2596a29f7351e8165d981645f403e0025, reversing
changes made to 4b367f2e8854da34d14bd154ff4432fb49f69b36.

Reported-by: Kevin Hilman <khilman@baylibre.com>
Signed-off-by: Olof Johansson <olof@lixom.net>

+127 -85
+127 -85
drivers/firmware/arm_scpi.c
··· 28 28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 29 29 30 30 #include <linux/bitmap.h> 31 - #include <linux/bitfield.h> 32 31 #include <linux/device.h> 33 32 #include <linux/err.h> 34 33 #include <linux/export.h> ··· 72 73 73 74 #define MAX_DVFS_DOMAINS 8 74 75 #define MAX_DVFS_OPPS 16 76 + #define DVFS_LATENCY(hdr) (le32_to_cpu(hdr) >> 16) 77 + #define DVFS_OPP_COUNT(hdr) ((le32_to_cpu(hdr) >> 8) & 0xff) 75 78 76 - #define PROTO_REV_MAJOR_MASK GENMASK(31, 16) 77 - #define PROTO_REV_MINOR_MASK GENMASK(15, 0) 79 + #define PROTOCOL_REV_MINOR_BITS 16 80 + #define PROTOCOL_REV_MINOR_MASK ((1U << PROTOCOL_REV_MINOR_BITS) - 1) 81 + #define PROTOCOL_REV_MAJOR(x) ((x) >> PROTOCOL_REV_MINOR_BITS) 82 + #define PROTOCOL_REV_MINOR(x) ((x) & PROTOCOL_REV_MINOR_MASK) 78 83 79 - #define FW_REV_MAJOR_MASK GENMASK(31, 24) 80 - #define FW_REV_MINOR_MASK GENMASK(23, 16) 81 - #define FW_REV_PATCH_MASK GENMASK(15, 0) 84 + #define FW_REV_MAJOR_BITS 24 85 + #define FW_REV_MINOR_BITS 16 86 + #define FW_REV_PATCH_MASK ((1U << FW_REV_MINOR_BITS) - 1) 87 + #define FW_REV_MINOR_MASK ((1U << FW_REV_MAJOR_BITS) - 1) 88 + #define FW_REV_MAJOR(x) ((x) >> FW_REV_MAJOR_BITS) 89 + #define FW_REV_MINOR(x) (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS) 90 + #define FW_REV_PATCH(x) ((x) & FW_REV_PATCH_MASK) 82 91 83 92 #define MAX_RX_TIMEOUT (msecs_to_jiffies(30)) 84 93 ··· 311 304 u8 name[20]; 312 305 } __packed; 313 306 307 + struct clk_get_value { 308 + __le32 rate; 309 + } __packed; 310 + 314 311 struct clk_set_value { 315 312 __le16 id; 316 313 __le16 reserved; ··· 328 317 } __packed; 329 318 330 319 struct dvfs_info { 331 - u8 domain; 332 - u8 opp_count; 333 - __le16 latency; 320 + __le32 header; 334 321 struct { 335 322 __le32 freq; 336 323 __le32 m_volt; ··· 350 341 u8 trigger_type; 351 342 char name[20]; 352 343 }; 344 + 345 + struct sensor_value { 346 + __le32 lo_val; 347 + __le32 hi_val; 348 + } __packed; 353 349 354 350 struct dev_pstate_set { 355 351 __le16 dev_id; ··· 419 405 unsigned int len; 420 406 421 407 if (scpi_info->is_legacy) { 422 - struct legacy_scpi_shared_mem __iomem *mem = 423 - ch->rx_payload; 408 + struct legacy_scpi_shared_mem *mem = ch->rx_payload; 424 409 425 410 /* RX Length is not replied by the legacy Firmware */ 426 411 len = match->rx_len; 427 412 428 - match->status = ioread32(&mem->status); 413 + match->status = le32_to_cpu(mem->status); 429 414 memcpy_fromio(match->rx_buf, mem->payload, len); 430 415 } else { 431 - struct scpi_shared_mem __iomem *mem = ch->rx_payload; 416 + struct scpi_shared_mem *mem = ch->rx_payload; 432 417 433 418 len = min(match->rx_len, CMD_SIZE(cmd)); 434 419 435 - match->status = ioread32(&mem->status); 420 + match->status = le32_to_cpu(mem->status); 436 421 memcpy_fromio(match->rx_buf, mem->payload, len); 437 422 } 438 423 ··· 445 432 static void scpi_handle_remote_msg(struct mbox_client *c, void *msg) 446 433 { 447 434 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl); 448 - struct scpi_shared_mem __iomem *mem = ch->rx_payload; 435 + struct scpi_shared_mem *mem = ch->rx_payload; 449 436 u32 cmd = 0; 450 437 451 438 if (!scpi_info->is_legacy) 452 - cmd = ioread32(&mem->command); 439 + cmd = le32_to_cpu(mem->command); 453 440 454 441 scpi_process_cmd(ch, cmd); 455 442 } ··· 459 446 unsigned long flags; 460 447 struct scpi_xfer *t = msg; 461 448 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl); 462 - struct scpi_shared_mem __iomem *mem = ch->tx_payload; 449 + struct scpi_shared_mem *mem = (struct scpi_shared_mem *)ch->tx_payload; 463 450 464 451 if (t->tx_buf) { 465 452 if (scpi_info->is_legacy) ··· 478 465 } 479 466 480 467 if (!scpi_info->is_legacy) 481 - iowrite32(t->cmd, &mem->command); 468 + mem->command = cpu_to_le32(t->cmd); 482 469 } 483 470 484 471 static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch) ··· 583 570 static unsigned long scpi_clk_get_val(u16 clk_id) 584 571 { 585 572 int ret; 586 - __le32 rate; 573 + struct clk_get_value clk; 587 574 __le16 le_clk_id = cpu_to_le16(clk_id); 588 575 589 576 ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id, 590 - sizeof(le_clk_id), &rate, sizeof(rate)); 577 + sizeof(le_clk_id), &clk, sizeof(clk)); 591 578 592 - return ret ? ret : le32_to_cpu(rate); 579 + return ret ? ret : le32_to_cpu(clk.rate); 593 580 } 594 581 595 582 static int scpi_clk_set_val(u16 clk_id, unsigned long rate) ··· 645 632 646 633 static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain) 647 634 { 648 - if (domain >= MAX_DVFS_DOMAINS) 649 - return ERR_PTR(-EINVAL); 650 - 651 - return scpi_info->dvfs[domain] ?: ERR_PTR(-EINVAL); 652 - } 653 - 654 - static int scpi_dvfs_populate_info(struct device *dev, u8 domain) 655 - { 656 635 struct scpi_dvfs_info *info; 657 636 struct scpi_opp *opp; 658 637 struct dvfs_info buf; 659 638 int ret, i; 660 639 640 + if (domain >= MAX_DVFS_DOMAINS) 641 + return ERR_PTR(-EINVAL); 642 + 643 + if (scpi_info->dvfs[domain]) /* data already populated */ 644 + return scpi_info->dvfs[domain]; 645 + 661 646 ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain), 662 647 &buf, sizeof(buf)); 663 648 if (ret) 664 - return ret; 649 + return ERR_PTR(ret); 665 650 666 - info = devm_kmalloc(dev, sizeof(*info), GFP_KERNEL); 651 + info = kmalloc(sizeof(*info), GFP_KERNEL); 667 652 if (!info) 668 - return -ENOMEM; 653 + return ERR_PTR(-ENOMEM); 669 654 670 - info->count = buf.opp_count; 671 - info->latency = le16_to_cpu(buf.latency) * 1000; /* uS to nS */ 655 + info->count = DVFS_OPP_COUNT(buf.header); 656 + info->latency = DVFS_LATENCY(buf.header) * 1000; /* uS to nS */ 672 657 673 - info->opps = devm_kcalloc(dev, info->count, sizeof(*opp), GFP_KERNEL); 674 - if (!info->opps) 675 - return -ENOMEM; 658 + info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL); 659 + if (!info->opps) { 660 + kfree(info); 661 + return ERR_PTR(-ENOMEM); 662 + } 676 663 677 664 for (i = 0, opp = info->opps; i < info->count; i++, opp++) { 678 665 opp->freq = le32_to_cpu(buf.opps[i].freq); ··· 682 669 sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL); 683 670 684 671 scpi_info->dvfs[domain] = info; 685 - return 0; 686 - } 687 - 688 - static void scpi_dvfs_populate(struct device *dev) 689 - { 690 - int domain; 691 - 692 - for (domain = 0; domain < MAX_DVFS_DOMAINS; domain++) 693 - scpi_dvfs_populate_info(dev, domain); 672 + return info; 694 673 } 695 674 696 675 static int scpi_dev_domain_id(struct device *dev) ··· 712 707 713 708 if (IS_ERR(info)) 714 709 return PTR_ERR(info); 710 + 711 + if (!info->latency) 712 + return 0; 715 713 716 714 return info->latency; 717 715 } ··· 776 768 static int scpi_sensor_get_value(u16 sensor, u64 *val) 777 769 { 778 770 __le16 id = cpu_to_le16(sensor); 779 - __le64 value; 771 + struct sensor_value buf; 780 772 int ret; 781 773 782 774 ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id), 783 - &value, sizeof(value)); 775 + &buf, sizeof(buf)); 784 776 if (ret) 785 777 return ret; 786 778 787 779 if (scpi_info->is_legacy) 788 - /* only 32-bits supported, upper 32 bits can be junk */ 789 - *val = le32_to_cpup((__le32 *)&value); 780 + /* only 32-bits supported, hi_val can be junk */ 781 + *val = le32_to_cpu(buf.lo_val); 790 782 else 791 - *val = le64_to_cpu(value); 783 + *val = (u64)le32_to_cpu(buf.hi_val) << 32 | 784 + le32_to_cpu(buf.lo_val); 792 785 793 786 return 0; 794 787 } ··· 862 853 static ssize_t protocol_version_show(struct device *dev, 863 854 struct device_attribute *attr, char *buf) 864 855 { 865 - return sprintf(buf, "%lu.%lu\n", 866 - FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version), 867 - FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version)); 856 + struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev); 857 + 858 + return sprintf(buf, "%d.%d\n", 859 + PROTOCOL_REV_MAJOR(scpi_info->protocol_version), 860 + PROTOCOL_REV_MINOR(scpi_info->protocol_version)); 868 861 } 869 862 static DEVICE_ATTR_RO(protocol_version); 870 863 871 864 static ssize_t firmware_version_show(struct device *dev, 872 865 struct device_attribute *attr, char *buf) 873 866 { 874 - return sprintf(buf, "%lu.%lu.%lu\n", 875 - FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version), 876 - FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version), 877 - FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version)); 867 + struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev); 868 + 869 + return sprintf(buf, "%d.%d.%d\n", 870 + FW_REV_MAJOR(scpi_info->firmware_version), 871 + FW_REV_MINOR(scpi_info->firmware_version), 872 + FW_REV_PATCH(scpi_info->firmware_version)); 878 873 } 879 874 static DEVICE_ATTR_RO(firmware_version); 880 875 ··· 889 876 }; 890 877 ATTRIBUTE_GROUPS(versions); 891 878 892 - static void scpi_free_channels(void *data) 879 + static void 880 + scpi_free_channels(struct device *dev, struct scpi_chan *pchan, int count) 893 881 { 894 - struct scpi_drvinfo *info = data; 895 882 int i; 896 883 897 - for (i = 0; i < info->num_chans; i++) 898 - mbox_free_channel(info->channels[i].chan); 884 + for (i = 0; i < count && pchan->chan; i++, pchan++) { 885 + mbox_free_channel(pchan->chan); 886 + devm_kfree(dev, pchan->xfers); 887 + devm_iounmap(dev, pchan->rx_payload); 888 + } 889 + } 890 + 891 + static int scpi_remove(struct platform_device *pdev) 892 + { 893 + int i; 894 + struct device *dev = &pdev->dev; 895 + struct scpi_drvinfo *info = platform_get_drvdata(pdev); 896 + 897 + scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */ 898 + 899 + of_platform_depopulate(dev); 900 + sysfs_remove_groups(&dev->kobj, versions_groups); 901 + scpi_free_channels(dev, info->channels, info->num_chans); 902 + platform_set_drvdata(pdev, NULL); 903 + 904 + for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) { 905 + kfree(info->dvfs[i]->opps); 906 + kfree(info->dvfs[i]); 907 + } 908 + devm_kfree(dev, info->channels); 909 + devm_kfree(dev, info); 910 + 911 + return 0; 899 912 } 900 913 901 914 #define MAX_SCPI_XFERS 10 ··· 952 913 { 953 914 int count, idx, ret; 954 915 struct resource res; 916 + struct scpi_chan *scpi_chan; 955 917 struct device *dev = &pdev->dev; 956 918 struct device_node *np = dev->of_node; 957 919 ··· 969 929 return -ENODEV; 970 930 } 971 931 972 - scpi_info->channels = devm_kcalloc(dev, count, sizeof(struct scpi_chan), 973 - GFP_KERNEL); 974 - if (!scpi_info->channels) 932 + scpi_chan = devm_kcalloc(dev, count, sizeof(*scpi_chan), GFP_KERNEL); 933 + if (!scpi_chan) 975 934 return -ENOMEM; 976 935 977 - ret = devm_add_action(dev, scpi_free_channels, scpi_info); 978 - if (ret) 979 - return ret; 980 - 981 - for (; scpi_info->num_chans < count; scpi_info->num_chans++) { 936 + for (idx = 0; idx < count; idx++) { 982 937 resource_size_t size; 983 - int idx = scpi_info->num_chans; 984 - struct scpi_chan *pchan = scpi_info->channels + idx; 938 + struct scpi_chan *pchan = scpi_chan + idx; 985 939 struct mbox_client *cl = &pchan->cl; 986 940 struct device_node *shmem = of_parse_phandle(np, "shmem", idx); 987 941 ··· 983 949 of_node_put(shmem); 984 950 if (ret) { 985 951 dev_err(dev, "failed to get SCPI payload mem resource\n"); 986 - return ret; 952 + goto err; 987 953 } 988 954 989 955 size = resource_size(&res); 990 956 pchan->rx_payload = devm_ioremap(dev, res.start, size); 991 957 if (!pchan->rx_payload) { 992 958 dev_err(dev, "failed to ioremap SCPI payload\n"); 993 - return -EADDRNOTAVAIL; 959 + ret = -EADDRNOTAVAIL; 960 + goto err; 994 961 } 995 962 pchan->tx_payload = pchan->rx_payload + (size >> 1); 996 963 ··· 1017 982 dev_err(dev, "failed to get channel%d err %d\n", 1018 983 idx, ret); 1019 984 } 985 + err: 986 + scpi_free_channels(dev, scpi_chan, idx); 987 + scpi_info = NULL; 1020 988 return ret; 1021 989 } 1022 990 991 + scpi_info->channels = scpi_chan; 992 + scpi_info->num_chans = count; 1023 993 scpi_info->commands = scpi_std_commands; 1024 - scpi_info->scpi_ops = &scpi_ops; 994 + 995 + platform_set_drvdata(pdev, scpi_info); 1025 996 1026 997 if (scpi_info->is_legacy) { 1027 998 /* Replace with legacy variants */ ··· 1043 1002 ret = scpi_init_versions(scpi_info); 1044 1003 if (ret) { 1045 1004 dev_err(dev, "incorrect or no SCP firmware found\n"); 1005 + scpi_remove(pdev); 1046 1006 return ret; 1047 1007 } 1048 1008 1049 - scpi_dvfs_populate(dev); 1009 + _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n", 1010 + PROTOCOL_REV_MAJOR(scpi_info->protocol_version), 1011 + PROTOCOL_REV_MINOR(scpi_info->protocol_version), 1012 + FW_REV_MAJOR(scpi_info->firmware_version), 1013 + FW_REV_MINOR(scpi_info->firmware_version), 1014 + FW_REV_PATCH(scpi_info->firmware_version)); 1015 + scpi_info->scpi_ops = &scpi_ops; 1050 1016 1051 - _dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n", 1052 - FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version), 1053 - FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version), 1054 - FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version), 1055 - FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version), 1056 - FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version)); 1057 - 1058 - ret = devm_device_add_groups(dev, versions_groups); 1017 + ret = sysfs_create_groups(&dev->kobj, versions_groups); 1059 1018 if (ret) 1060 1019 dev_err(dev, "unable to create sysfs version group\n"); 1061 1020 1062 - return devm_of_platform_populate(dev); 1021 + return of_platform_populate(dev->of_node, NULL, NULL, dev); 1063 1022 } 1064 1023 1065 1024 static const struct of_device_id scpi_of_match[] = { ··· 1076 1035 .of_match_table = scpi_of_match, 1077 1036 }, 1078 1037 .probe = scpi_probe, 1038 + .remove = scpi_remove, 1079 1039 }; 1080 1040 module_platform_driver(scpi_driver); 1081 1041