Merge branch 'for-3.18/drivers' of git://git.kernel.dk/linux-block
[cascardo/linux.git] / drivers / hwmon / k10temp.c
index f7b46f6..1e7bdcd 100644 (file)
@@ -33,6 +33,9 @@ static bool force;
 module_param(force, bool, 0444);
 MODULE_PARM_DESC(force, "force loading on processors with erratum 319");
 
+/* Provide lock for writing to NB_SMU_IND_ADDR */
+static DEFINE_MUTEX(nb_smu_ind_mutex);
+
 /* CPUID function 0x80000001, ebx */
 #define CPUID_PKGTYPE_MASK     0xf0000000
 #define CPUID_PKGTYPE_F                0x00000000
@@ -51,13 +54,38 @@ MODULE_PARM_DESC(force, "force loading on processors with erratum 319");
 #define REG_NORTHBRIDGE_CAPABILITIES   0xe8
 #define  NB_CAP_HTC                    0x00000400
 
+/*
+ * For F15h M60h, functionality of REG_REPORTED_TEMPERATURE
+ * has been moved to D0F0xBC_xD820_0CA4 [Reported Temperature
+ * Control]
+ */
+#define F15H_M60H_REPORTED_TEMP_CTRL_OFFSET    0xd8200ca4
+#define PCI_DEVICE_ID_AMD_15H_M60H_NB_F3       0x1573
+
+static void amd_nb_smu_index_read(struct pci_dev *pdev, unsigned int devfn,
+                                 int offset, u32 *val)
+{
+       mutex_lock(&nb_smu_ind_mutex);
+       pci_bus_write_config_dword(pdev->bus, devfn,
+                                  0xb8, offset);
+       pci_bus_read_config_dword(pdev->bus, devfn,
+                                 0xbc, val);
+       mutex_unlock(&nb_smu_ind_mutex);
+}
+
 static ssize_t show_temp(struct device *dev,
                         struct device_attribute *attr, char *buf)
 {
        u32 regval;
-
-       pci_read_config_dword(to_pci_dev(dev),
-                             REG_REPORTED_TEMPERATURE, &regval);
+       struct pci_dev *pdev = dev_get_drvdata(dev);
+
+       if (boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model == 0x60) {
+               amd_nb_smu_index_read(pdev, PCI_DEVFN(0, 0),
+                                     F15H_M60H_REPORTED_TEMP_CTRL_OFFSET,
+                                     &regval);
+       } else {
+               pci_read_config_dword(pdev, REG_REPORTED_TEMPERATURE, &regval);
+       }
        return sprintf(buf, "%u\n", (regval >> 21) * 125);
 }
 
@@ -75,7 +103,7 @@ static ssize_t show_temp_crit(struct device *dev,
        u32 regval;
        int value;
 
-       pci_read_config_dword(to_pci_dev(dev),
+       pci_read_config_dword(dev_get_drvdata(dev),
                              REG_HARDWARE_THERMAL_CONTROL, &regval);
        value = ((regval >> 16) & 0x7f) * 500 + 52000;
        if (show_hyst)
@@ -83,17 +111,43 @@ static ssize_t show_temp_crit(struct device *dev,
        return sprintf(buf, "%d\n", value);
 }
 
-static ssize_t show_name(struct device *dev,
-                        struct device_attribute *attr, char *buf)
-{
-       return sprintf(buf, "k10temp\n");
-}
-
 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
 static DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL);
 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_crit, NULL, 1);
-static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static umode_t k10temp_is_visible(struct kobject *kobj,
+                                 struct attribute *attr, int index)
+{
+       struct device *dev = container_of(kobj, struct device, kobj);
+       struct pci_dev *pdev = dev_get_drvdata(dev);
+
+       if (index >= 2) {
+               u32 reg_caps, reg_htc;
+
+               pci_read_config_dword(pdev, REG_NORTHBRIDGE_CAPABILITIES,
+                                     &reg_caps);
+               pci_read_config_dword(pdev, REG_HARDWARE_THERMAL_CONTROL,
+                                     &reg_htc);
+               if (!(reg_caps & NB_CAP_HTC) || !(reg_htc & HTC_ENABLE))
+                       return 0;
+       }
+       return attr->mode;
+}
+
+static struct attribute *k10temp_attrs[] = {
+       &dev_attr_temp1_input.attr,
+       &dev_attr_temp1_max.attr,
+       &sensor_dev_attr_temp1_crit.dev_attr.attr,
+       &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
+       NULL
+};
+
+static const struct attribute_group k10temp_group = {
+       .attrs = k10temp_attrs,
+       .is_visible = k10temp_is_visible,
+};
+__ATTRIBUTE_GROUPS(k10temp);
 
 static bool has_erratum_319(struct pci_dev *pdev)
 {
@@ -132,76 +186,23 @@ static bool has_erratum_319(struct pci_dev *pdev)
 static int k10temp_probe(struct pci_dev *pdev,
                                   const struct pci_device_id *id)
 {
-       struct device *hwmon_dev;
-       u32 reg_caps, reg_htc;
        int unreliable = has_erratum_319(pdev);
-       int err;
-
-       if (unreliable && !force) {
-               dev_err(&pdev->dev,
-                       "unreliable CPU thermal sensor; monitoring disabled\n");
-               err = -ENODEV;
-               goto exit;
-       }
-
-       err = device_create_file(&pdev->dev, &dev_attr_temp1_input);
-       if (err)
-               goto exit;
-       err = device_create_file(&pdev->dev, &dev_attr_temp1_max);
-       if (err)
-               goto exit_remove;
-
-       pci_read_config_dword(pdev, REG_NORTHBRIDGE_CAPABILITIES, &reg_caps);
-       pci_read_config_dword(pdev, REG_HARDWARE_THERMAL_CONTROL, &reg_htc);
-       if ((reg_caps & NB_CAP_HTC) && (reg_htc & HTC_ENABLE)) {
-               err = device_create_file(&pdev->dev,
-                               &sensor_dev_attr_temp1_crit.dev_attr);
-               if (err)
-                       goto exit_remove;
-               err = device_create_file(&pdev->dev,
-                               &sensor_dev_attr_temp1_crit_hyst.dev_attr);
-               if (err)
-                       goto exit_remove;
-       }
-
-       err = device_create_file(&pdev->dev, &dev_attr_name);
-       if (err)
-               goto exit_remove;
-
-       hwmon_dev = hwmon_device_register(&pdev->dev);
-       if (IS_ERR(hwmon_dev)) {
-               err = PTR_ERR(hwmon_dev);
-               goto exit_remove;
-       }
-       pci_set_drvdata(pdev, hwmon_dev);
+       struct device *dev = &pdev->dev;
+       struct device *hwmon_dev;
 
-       if (unreliable && force)
-               dev_warn(&pdev->dev,
+       if (unreliable) {
+               if (!force) {
+                       dev_err(dev,
+                               "unreliable CPU thermal sensor; monitoring disabled\n");
+                       return -ENODEV;
+               }
+               dev_warn(dev,
                         "unreliable CPU thermal sensor; check erratum 319\n");
-       return 0;
-
-exit_remove:
-       device_remove_file(&pdev->dev, &dev_attr_name);
-       device_remove_file(&pdev->dev, &dev_attr_temp1_input);
-       device_remove_file(&pdev->dev, &dev_attr_temp1_max);
-       device_remove_file(&pdev->dev,
-                          &sensor_dev_attr_temp1_crit.dev_attr);
-       device_remove_file(&pdev->dev,
-                          &sensor_dev_attr_temp1_crit_hyst.dev_attr);
-exit:
-       return err;
-}
+       }
 
-static void k10temp_remove(struct pci_dev *pdev)
-{
-       hwmon_device_unregister(pci_get_drvdata(pdev));
-       device_remove_file(&pdev->dev, &dev_attr_name);
-       device_remove_file(&pdev->dev, &dev_attr_temp1_input);
-       device_remove_file(&pdev->dev, &dev_attr_temp1_max);
-       device_remove_file(&pdev->dev,
-                          &sensor_dev_attr_temp1_crit.dev_attr);
-       device_remove_file(&pdev->dev,
-                          &sensor_dev_attr_temp1_crit_hyst.dev_attr);
+       hwmon_dev = devm_hwmon_device_register_with_groups(dev, "k10temp", pdev,
+                                                          k10temp_groups);
+       return PTR_ERR_OR_ZERO(hwmon_dev);
 }
 
 static const struct pci_device_id k10temp_id_table[] = {
@@ -211,6 +212,7 @@ static const struct pci_device_id k10temp_id_table[] = {
        { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F3) },
        { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M10H_F3) },
        { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F3) },
+       { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F3) },
        { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F3) },
        { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F3) },
        {}
@@ -221,7 +223,6 @@ static struct pci_driver k10temp_driver = {
        .name = "k10temp",
        .id_table = k10temp_id_table,
        .probe = k10temp_probe,
-       .remove = k10temp_remove,
 };
 
 module_pci_driver(k10temp_driver);