Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Export the firmware instance and label associated with a PCI device to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2010 Dell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * by Narendra K <Narendra_K@dell.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Jordan Hargrave <Jordan_Hargrave@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * PCI or PCI Express Device Under Operating Systems) defines an instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * number and string name. This code retrieves them and exports them to sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * If the system firmware does not provide the ACPI _DSM (Device Specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Method), then the SMBIOS type 41 instance number and string is exported to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * SMBIOS defines type 41 for onboard pci devices. This code retrieves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * the instance number and string from the type 41 record and exports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * it to sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Please see https://linux.dell.com/files/biosdevname/ for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/pci_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/nls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/pci-acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "pci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #ifdef CONFIG_DMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) enum smbios_attr_enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	SMBIOS_ATTR_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	SMBIOS_ATTR_LABEL_SHOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	SMBIOS_ATTR_INSTANCE_SHOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 					  enum smbios_attr_enum attribute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	const struct dmi_device *dmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct dmi_dev_onboard *donboard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int domain_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int devfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	domain_nr = pci_domain_nr(pdev->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	bus = pdev->bus->number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	devfn = pdev->devfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	dmi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				      NULL, dmi)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		donboard = dmi->device_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (donboard && donboard->segment == domain_nr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 				donboard->bus == bus &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 				donboard->devfn == devfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 					return scnprintf(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 							 "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 							 donboard->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 				else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 					return scnprintf(buf, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 							 "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 							 dmi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			return strlen(dmi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static umode_t smbios_instance_string_exist(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 					    struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct pci_dev *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	pdev = to_pci_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 					   S_IRUGO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static ssize_t smbioslabel_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct pci_dev *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	pdev = to_pci_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return find_smbios_instance_string(pdev, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 					   SMBIOS_ATTR_LABEL_SHOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static ssize_t smbiosinstance_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				   struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct pci_dev *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	pdev = to_pci_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return find_smbios_instance_string(pdev, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					   SMBIOS_ATTR_INSTANCE_SHOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct device_attribute smbios_attr_label = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	.attr = {.name = "label", .mode = 0444},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.show = smbioslabel_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct device_attribute smbios_attr_instance = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	.attr = {.name = "index", .mode = 0444},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.show = smbiosinstance_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct attribute *smbios_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	&smbios_attr_label.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	&smbios_attr_instance.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static const struct attribute_group smbios_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	.attrs = smbios_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	.is_visible = smbios_instance_string_exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int pci_create_smbiosname_file(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void pci_remove_smbiosname_file(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline int pci_create_smbiosname_file(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline void pci_remove_smbiosname_file(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) enum acpi_attr_enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ACPI_ATTR_LABEL_SHOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ACPI_ATTR_INDEX_SHOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			      obj->buffer.length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			      UTF16_LITTLE_ENDIAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			      buf, PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	buf[len] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int dsm_get_label(struct device *dev, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			 enum acpi_attr_enum attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	acpi_handle handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	union acpi_object *obj, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int len = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	handle = ACPI_HANDLE(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				DSM_PCI_DEVICE_NAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	tmp = obj->package.elements;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	    tmp[0].type == ACPI_TYPE_INTEGER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	    (tmp[1].type == ACPI_TYPE_STRING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	     tmp[1].type == ACPI_TYPE_BUFFER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		 * The second string element is optional even when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		 * this _DSM is implemented; when not implemented,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		 * this entry must return a null string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		if (attr == ACPI_ATTR_INDEX_SHOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		} else if (attr == ACPI_ATTR_LABEL_SHOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			if (tmp[1].type == ACPI_TYPE_STRING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				scnprintf(buf, PAGE_SIZE, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 					  tmp[1].string.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			else if (tmp[1].type == ACPI_TYPE_BUFFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				dsm_label_utf16s_to_utf8s(tmp + 1, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		len = strlen(buf) > 0 ? strlen(buf) : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	ACPI_FREE(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static bool device_has_dsm(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	acpi_handle handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	handle = ACPI_HANDLE(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return !!acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				1 << DSM_PCI_DEVICE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static umode_t acpi_index_string_exist(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				       struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (device_has_dsm(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static ssize_t acpilabel_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			      struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static ssize_t acpiindex_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			      struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static struct device_attribute acpi_attr_label = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.attr = {.name = "label", .mode = 0444},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.show = acpilabel_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static struct device_attribute acpi_attr_index = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.attr = {.name = "acpi_index", .mode = 0444},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	.show = acpiindex_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static struct attribute *acpi_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	&acpi_attr_label.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	&acpi_attr_index.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static const struct attribute_group acpi_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	.attrs = acpi_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.is_visible = acpi_index_string_exist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int pci_create_acpi_index_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static inline bool device_has_dsm(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) void pci_create_firmware_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (device_has_dsm(&pdev->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		pci_create_acpi_index_label_files(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		pci_create_smbiosname_file(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) void pci_remove_firmware_label_files(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (device_has_dsm(&pdev->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		pci_remove_acpi_index_label_files(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		pci_remove_smbiosname_file(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }