diff mbox

[V7,6/7] LIBIO: Support the dynamically logical PIO registration of ACPI host I/O

Message ID 1489372963-9000-7-git-send-email-yuanzhichang@hisilicon.com
State Not Applicable
Headers show

Commit Message

Zhichang Yuan March 13, 2017, 2:42 a.m. UTC
For those hosts which access I/O based on the host/bus local I/O addresses,
their I/O range must be registered and translated as unique logical PIO before
the ACPI enumeration on the devices under the hosts. Otherwise, there is no
available I/O resources allocated for those devices.
This patch implements the interfaces in LIBIO to perform the host local I/O
translation and set the logical IO mapped as ACPI I/O resources.

Signed-off-by: zhichang.yuan <yuanzhichang@hisilicon.com>
Signed-off-by: Gabriele Paoloni <gabriele.paoloni@huawei.com>
---
 include/linux/libio.h |   4 +
 lib/libio.c           | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 228 insertions(+)

Comments

kernel test robot March 14, 2017, 4:27 a.m. UTC | #1
Hi zhichang.yuan,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.11-rc2 next-20170310]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/zhichang-yuan/LPC-legacy-ISA-I-O-support/20170314-114635
config: i386-tinyconfig (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

   In file included from arch/x86/boot/compressed/misc.h:24:0,
                    from arch/x86/boot/compressed/cmdline.c:1:
>> arch/x86/boot/compressed/../ctype.h:4:19: error: redefinition of 'isdigit'
    static inline int isdigit(int ch)
                      ^~~~~~~
   In file included from include/acpi/platform/aclinux.h:82:0,
                    from include/acpi/platform/acenv.h:186,
                    from include/acpi/acpi.h:56,
                    from include/linux/acpi.h:33,
                    from include/linux/libio.h:23,
                    from include/linux/io.h:27,
                    from arch/x86/boot/compressed/misc.h:17,
                    from arch/x86/boot/compressed/cmdline.c:1:
   include/linux/ctype.h:25:19: note: previous definition of 'isdigit' was here
    static inline int isdigit(int c)
                      ^~~~~~~
>> arch/x86/boot/compressed/../ctype.h:9:32: error: expected ')' before 'ch'
    static inline int isxdigit(int ch)
                                   ^
   include/linux/ctype.h:20:50: note: in definition of macro '__ismask'
    #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
                                                     ^
   arch/x86/boot/compressed/../ctype.h:9:19: note: in expansion of macro 'isxdigit'
    static inline int isxdigit(int ch)
                      ^~~~~~~~
   include/linux/ctype.h:20:52: error: expected expression before ']' token
    #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
                                                       ^
   include/linux/ctype.h:36:23: note: in expansion of macro '__ismask'
    #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
                          ^~~~~~~~
   arch/x86/boot/compressed/../ctype.h:9:19: note: in expansion of macro 'isxdigit'
    static inline int isxdigit(int ch)
                      ^~~~~~~~
   include/linux/ctype.h:36:44: error: expected ')' before '!=' token
    #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
                                               ^
   arch/x86/boot/compressed/../ctype.h:9:19: note: in expansion of macro 'isxdigit'
    static inline int isxdigit(int ch)
                      ^~~~~~~~

vim +/isdigit +4 arch/x86/boot/compressed/../ctype.h

60b217a0 arch/x86/boot/ctype.h   Alexander Kuleshov 2015-01-03   1  #ifndef BOOT_CTYPE_H
60b217a0 arch/x86/boot/ctype.h   Alexander Kuleshov 2015-01-03   2  #define BOOT_CTYPE_H
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02   3  
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02  @4  static inline int isdigit(int ch)
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02   5  {
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02   6  	return (ch >= '0') && (ch <= '9');
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02   7  }
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02   8  
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02  @9  static inline int isxdigit(int ch)
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02  10  {
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02  11  	if (isdigit(ch))
f4ed2877 arch/x86/boot/isdigit.h Yinghai Lu         2010-08-02  12  		return true;

:::::: The code at line 4 was first introduced by commit
:::::: f4ed2877b16e8146427306aea8819adac5c88374 x86, setup: reorganize the early console setup

:::::: TO: Yinghai Lu <yinghai@kernel.org>
:::::: CC: H. Peter Anvin <hpa@linux.intel.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot March 14, 2017, 5:10 a.m. UTC | #2
Hi zhichang.yuan,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.11-rc2 next-20170310]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/zhichang-yuan/LPC-legacy-ISA-I-O-support/20170314-114635
config: blackfin-allmodconfig (attached as .config)
compiler: bfin-uclinux-gcc (GCC) 6.2.0
reproduce:
        wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=blackfin 

All error/warnings (new ones prefixed by >>):

   include/linux/ratelimit.h: In function 'ratelimit_state_exit':
>> arch/blackfin/kernel/module.c:7:40: error: 'mod' undeclared (first use in this function)
    #define pr_fmt(fmt) "module %s: " fmt, mod->name
                                           ^
   include/linux/printk.h:303:22: note: in expansion of macro 'pr_fmt'
     printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
                         ^~~~~~
   include/linux/printk.h:304:17: note: in expansion of macro 'pr_warning'
    #define pr_warn pr_warning
                    ^~~~~~~~~~
>> include/linux/ratelimit.h:61:3: note: in expansion of macro 'pr_warn'
      pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
      ^~~~~~~
   arch/blackfin/kernel/module.c:7:40: note: each undeclared identifier is reported only once for each function it appears in
    #define pr_fmt(fmt) "module %s: " fmt, mod->name
                                           ^
   include/linux/printk.h:303:22: note: in expansion of macro 'pr_fmt'
     printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
                         ^~~~~~
   include/linux/printk.h:304:17: note: in expansion of macro 'pr_warning'
    #define pr_warn pr_warning
                    ^~~~~~~~~~
>> include/linux/ratelimit.h:61:3: note: in expansion of macro 'pr_warn'
      pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
      ^~~~~~~

vim +/mod +7 arch/blackfin/kernel/module.c

1394f032 Bryan Wu    2007-05-06   1  /*
96f1050d Robin Getz  2009-09-24   2   * Copyright 2004-2009 Analog Devices Inc.
1394f032 Bryan Wu    2007-05-06   3   *
96f1050d Robin Getz  2009-09-24   4   * Licensed under the GPL-2 or later
1394f032 Bryan Wu    2007-05-06   5   */
1394f032 Bryan Wu    2007-05-06   6  
b75a9e6b Joe Perches 2010-10-20  @7  #define pr_fmt(fmt) "module %s: " fmt, mod->name
1394f032 Bryan Wu    2007-05-06   8  
1394f032 Bryan Wu    2007-05-06   9  #include <linux/moduleloader.h>
1394f032 Bryan Wu    2007-05-06  10  #include <linux/elf.h>
1394f032 Bryan Wu    2007-05-06  11  #include <linux/vmalloc.h>
1394f032 Bryan Wu    2007-05-06  12  #include <linux/fs.h>
1394f032 Bryan Wu    2007-05-06  13  #include <linux/string.h>
1394f032 Bryan Wu    2007-05-06  14  #include <linux/kernel.h>
1394f032 Bryan Wu    2007-05-06  15  #include <asm/dma.h>

:::::: The code at line 7 was first introduced by commit
:::::: b75a9e6bfba5c7343391ac1bacd44d6652dde8da Blackfin: use more standard pr_fmt in the module loader

:::::: TO: Joe Perches <joe@perches.com>
:::::: CC: Mike Frysinger <vapier@gentoo.org>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox

Patch

diff --git a/include/linux/libio.h b/include/linux/libio.h
index 91038aa..95c5f3e 100644
--- a/include/linux/libio.h
+++ b/include/linux/libio.h
@@ -20,6 +20,7 @@ 
 
 #ifdef __KERNEL__
 
+#include <linux/acpi.h>
 #include <linux/fwnode.h>
 
 /* This is compatible to PCI MMIO. */
@@ -90,5 +91,8 @@  static inline unsigned long libio_translate_hwaddr(struct fwnode_handle *fwnode,
 
 extern unsigned long libio_translate_cpuaddr(resource_size_t hw_addr);
 
+extern int acpi_set_libio_resource(struct acpi_device *adev,
+			struct acpi_device *host);
+
 #endif /* __KERNEL__ */
 #endif /* __LINUX_LIBIO_H */
diff --git a/lib/libio.c b/lib/libio.c
index e42f50b..b60ec9c 100644
--- a/lib/libio.c
+++ b/lib/libio.c
@@ -242,6 +242,230 @@  resource_size_t libio_to_hwaddr(unsigned long pio)
 	return -1;
 }
 
+#ifdef	CONFIG_ACPI
+static inline bool acpi_libio_supported_resource(struct acpi_resource *res)
+{
+	switch (res->type) {
+	case ACPI_RESOURCE_TYPE_ADDRESS32:
+	case ACPI_RESOURCE_TYPE_ADDRESS64:
+		return true;
+	}
+	return false;
+}
+
+static acpi_status acpi_count_libiores(struct acpi_resource *res,
+					   void *data)
+{
+	int *res_cnt = data;
+
+	if (acpi_libio_supported_resource(res) &&
+		!acpi_dev_filter_resource_type(res, IORESOURCE_IO))
+		(*res_cnt)++;
+
+	return AE_OK;
+}
+
+static acpi_status acpi_read_one_libiores(struct acpi_resource *res,
+		void *data)
+{
+	struct acpi_resource **resource = data;
+
+	if (acpi_libio_supported_resource(res) &&
+		!acpi_dev_filter_resource_type(res, IORESOURCE_IO)) {
+		memcpy((*resource), res, sizeof(struct acpi_resource));
+		(*resource)->length = sizeof(struct acpi_resource);
+		(*resource)->type = res->type;
+		(*resource)++;
+	}
+
+	return AE_OK;
+}
+
+static acpi_status
+acpi_build_libiores_template(struct acpi_device *adev,
+			struct acpi_buffer *buffer)
+{
+	acpi_handle handle = adev->handle;
+	struct acpi_resource *resource;
+	acpi_status status;
+	int res_cnt = 0;
+
+	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
+				     acpi_count_libiores, &res_cnt);
+	if (ACPI_FAILURE(status) || !res_cnt) {
+		dev_err(&adev->dev, "can't evaluate _CRS: %d\n", status);
+		return -EINVAL;
+	}
+
+	buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
+	buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
+	if (!buffer->pointer)
+		return -ENOMEM;
+
+	resource = (struct acpi_resource *)buffer->pointer;
+	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
+				     acpi_read_one_libiores, &resource);
+	if (ACPI_FAILURE(status)) {
+		kfree(buffer->pointer);
+		dev_err(&adev->dev, "can't evaluate _CRS: %d\n", status);
+		return -EINVAL;
+	}
+
+	resource->type = ACPI_RESOURCE_TYPE_END_TAG;
+	resource->length = sizeof(struct acpi_resource);
+
+	return 0;
+}
+
+static int acpi_translate_libiores(struct acpi_device *adev,
+		struct acpi_device *host, struct acpi_buffer *buffer)
+{
+	int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
+	struct acpi_resource *resource = buffer->pointer;
+	struct acpi_resource_address64 addr;
+	unsigned long sys_port;
+	struct device *dev = &adev->dev;
+
+	/* only one I/O resource now */
+	if (res_cnt != 1) {
+		dev_err(dev, "encode %d resources whose type is(%d)!\n",
+			res_cnt, resource->type);
+		return -EINVAL;
+	}
+
+	if (ACPI_FAILURE(acpi_resource_to_address64(resource, &addr))) {
+		dev_err(dev, "convert acpi resource(%d) as addr64 FAIL!\n",
+			resource->type);
+		return -EFAULT;
+	}
+
+	/* For indirect-IO, addr length must be fixed. (>0, 0/1, 0/1)(0,0,0) */
+	if (addr.min_address_fixed != addr.max_address_fixed) {
+		dev_warn(dev, "variable I/O resource is invalid!\n");
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "CRS IO: len=0x%llx [0x%llx - 0x%llx]\n",
+			addr.address.address_length, addr.address.minimum,
+			addr.address.maximum);
+	sys_port = libio_translate_hwaddr(&host->fwnode, addr.address.minimum);
+	if (sys_port == -1) {
+		dev_err(dev, "translate bus-addr(0x%llx) fail!\n",
+			addr.address.minimum);
+		return -EFAULT;
+	}
+
+	switch (resource->type) {
+	case ACPI_RESOURCE_TYPE_ADDRESS32:
+	{
+		struct acpi_resource_address32 *out_res;
+
+		out_res = &resource->data.address32;
+		if (!addr.address.address_length)
+			addr.address.address_length = out_res->address.maximum -
+				out_res->address.minimum + 1;
+		out_res->address.minimum = sys_port;
+		out_res->address.maximum = sys_port +
+				addr.address.address_length - 1;
+		out_res->address.address_length = addr.address.address_length;
+
+		dev_info(dev, "_SRS 32IO: [0x%x - 0x%x] len = 0x%x\n",
+			out_res->address.minimum,
+			out_res->address.maximum,
+			out_res->address.address_length);
+
+		break;
+	}
+
+	case ACPI_RESOURCE_TYPE_ADDRESS64:
+	{
+		struct acpi_resource_address64 *out_res;
+
+		out_res = &resource->data.address64;
+		if (!addr.address.address_length)
+			addr.address.address_length = out_res->address.maximum -
+				out_res->address.minimum + 1;
+		out_res->address.minimum = sys_port;
+		out_res->address.maximum = sys_port +
+				addr.address.address_length - 1;
+		out_res->address.address_length = addr.address.address_length;
+
+		dev_info(dev, "_SRS 64IO: [0x%llx - 0x%llx] len = 0x%llx\n",
+			out_res->address.minimum,
+			out_res->address.maximum,
+			out_res->address.address_length);
+
+		break;
+	}
+
+	default:
+		return -EINVAL;
+
+	}
+
+	return 0;
+}
+
+/*
+ * update/set the current I/O resource of the designated device node.
+ * after this calling, the enumeration can be started as the I/O resource
+ * had been translated to logicial I/O from bus-local I/O.
+ *
+ * @adev: the device node to be updated the I/O resource;
+ * @host: the device node where 'adev' is attached, which can be not
+ *	the parent of 'adev';
+ *
+ * return 0 when successful, negative is for failure.
+ */
+int acpi_set_libio_resource(struct acpi_device *adev,
+		struct acpi_device *host)
+{
+	struct device *dev = &adev->dev;
+	struct acpi_buffer buffer;
+	acpi_status status;
+	int ret;
+
+	if (!host)
+		return -EINVAL;
+
+	/* check the device state */
+	if (!adev->status.present) {
+		dev_info(dev, "ACPI: device is not present!\n");
+		return 0;
+	}
+	/* whether the child had been enumerated? */
+	if (acpi_device_enumerated(adev)) {
+		dev_info(dev, "ACPI: had been enumerated!\n");
+		return 0;
+	}
+
+	/* read the _CRS and convert as acpi_buffer */
+	status = acpi_build_libiores_template(adev, &buffer);
+	if (ACPI_FAILURE(status)) {
+		dev_warn(dev, "Failure evaluating %s\n", METHOD_NAME__CRS);
+		return -ENODEV;
+	}
+
+	/* translate the I/O resources */
+	ret = acpi_translate_libiores(adev, host, &buffer);
+	if (ret) {
+		kfree(buffer.pointer);
+		dev_err(dev, "Translate I/O range FAIL!\n");
+		return ret;
+	}
+
+	/* set current resource... */
+	status = acpi_set_current_resources(adev->handle, &buffer);
+	kfree(buffer.pointer);
+	if (ACPI_FAILURE(status)) {
+		dev_err(dev, "Error evaluating _SRS (0x%x)\n", status);
+		ret = -EIO;
+	}
+
+	return ret;
+}
+#endif
+
 #ifdef PCI_IOBASE
 static struct libio_range *find_io_range(unsigned long pio)
 {