Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 593669c2 authored by Jiang Liu's avatar Jiang Liu Committed by Rafael J. Wysocki
Browse files

x86/PCI/ACPI: Use common ACPI resource interfaces to simplify implementation



Use common ACPI resource discovery interfaces to simplify PCI host bridge
resource enumeration.

Signed-off-by: default avatarJiang Liu <jiang.liu@linux.intel.com>
Acked-by: default avatarBjorn Helgaas <bhelgaas@google.com>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 812dbd99
Loading
Loading
Loading
Loading
+91 −204
Original line number Original line Diff line number Diff line
@@ -10,9 +10,6 @@
struct pci_root_info {
struct pci_root_info {
	struct acpi_device *bridge;
	struct acpi_device *bridge;
	char name[16];
	char name[16];
	unsigned int res_num;
	struct resource *res;
	resource_size_t *res_offset;
	struct pci_sysdata sd;
	struct pci_sysdata sd;
#ifdef	CONFIG_PCI_MMCONFIG
#ifdef	CONFIG_PCI_MMCONFIG
	bool mcfg_added;
	bool mcfg_added;
@@ -218,132 +215,41 @@ static void teardown_mcfg_map(struct pci_root_info *info)
}
}
#endif
#endif


static acpi_status resource_to_addr(struct acpi_resource *resource,
static void validate_resources(struct device *dev, struct list_head *crs_res,
				    struct acpi_resource_address64 *addr)
			       unsigned long type)
{
	acpi_status status;
	struct acpi_resource_memory24 *memory24;
	struct acpi_resource_memory32 *memory32;
	struct acpi_resource_fixed_memory32 *fixed_memory32;

	memset(addr, 0, sizeof(*addr));
	switch (resource->type) {
	case ACPI_RESOURCE_TYPE_MEMORY24:
		memory24 = &resource->data.memory24;
		addr->resource_type = ACPI_MEMORY_RANGE;
		addr->address.minimum = memory24->minimum;
		addr->address.address_length = memory24->address_length;
		addr->address.maximum = addr->address.minimum + addr->address.address_length - 1;
		return AE_OK;
	case ACPI_RESOURCE_TYPE_MEMORY32:
		memory32 = &resource->data.memory32;
		addr->resource_type = ACPI_MEMORY_RANGE;
		addr->address.minimum = memory32->minimum;
		addr->address.address_length = memory32->address_length;
		addr->address.maximum = addr->address.minimum + addr->address.address_length - 1;
		return AE_OK;
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
		fixed_memory32 = &resource->data.fixed_memory32;
		addr->resource_type = ACPI_MEMORY_RANGE;
		addr->address.minimum = fixed_memory32->address;
		addr->address.address_length = fixed_memory32->address_length;
		addr->address.maximum = addr->address.minimum + addr->address.address_length - 1;
		return AE_OK;
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
		status = acpi_resource_to_address64(resource, addr);
		if (ACPI_SUCCESS(status) &&
		    (addr->resource_type == ACPI_MEMORY_RANGE ||
		    addr->resource_type == ACPI_IO_RANGE) &&
		    addr->address.address_length > 0) {
			return AE_OK;
		}
		break;
	}
	return AE_ERROR;
}

static acpi_status count_resource(struct acpi_resource *acpi_res, void *data)
{
{
	struct pci_root_info *info = data;
	LIST_HEAD(list);
	struct acpi_resource_address64 addr;
	struct resource *res1, *res2, *root = NULL;
	acpi_status status;
	struct resource_entry *tmp, *entry, *entry2;


	status = resource_to_addr(acpi_res, &addr);
	BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
	if (ACPI_SUCCESS(status))
	root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
		info->res_num++;
	return AE_OK;
}


static acpi_status setup_resource(struct acpi_resource *acpi_res, void *data)
	list_splice_init(crs_res, &list);
{
	resource_list_for_each_entry_safe(entry, tmp, &list) {
	struct pci_root_info *info = data;
		bool free = false;
	struct resource *res;
		resource_size_t end;
	struct acpi_resource_address64 addr;
	acpi_status status;
	unsigned long flags;
	u64 start, orig_end, end, res_end;

	status = resource_to_addr(acpi_res, &addr);
	if (!ACPI_SUCCESS(status))
		return AE_OK;

	if (addr.resource_type == ACPI_MEMORY_RANGE) {
		flags = IORESOURCE_MEM;
		if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
			flags |= IORESOURCE_PREFETCH;
		res_end = (u64)iomem_resource.end;
	} else if (addr.resource_type == ACPI_IO_RANGE) {
		flags = IORESOURCE_IO;
		res_end = (u64)ioport_resource.end;
	} else
		return AE_OK;

	start = addr.address.minimum + addr.address.translation_offset;
	orig_end = end = addr.address.maximum + addr.address.translation_offset;

	/* Exclude non-addressable range or non-addressable portion of range */
	end = min(end, res_end);
	if (end <= start) {
		dev_info(&info->bridge->dev,
			"host bridge window [%#llx-%#llx] "
			"(ignored, not CPU addressable)\n", start, orig_end);
		return AE_OK;
	} else if (orig_end != end) {
		dev_info(&info->bridge->dev,
			"host bridge window [%#llx-%#llx] "
			"([%#llx-%#llx] ignored, not CPU addressable)\n", 
			start, orig_end, end + 1, orig_end);
	}

	res = &info->res[info->res_num];
	res->name = info->name;
	res->flags = flags;
	res->start = start;
	res->end = end;
	info->res_offset[info->res_num] = addr.address.translation_offset;
	info->res_num++;

	if (!pci_use_crs)
		dev_printk(KERN_DEBUG, &info->bridge->dev,
			   "host bridge window %pR (ignored)\n", res);


	return AE_OK;
		res1 = entry->res;
}

static void coalesce_windows(struct pci_root_info *info, unsigned long type)
{
	int i, j;
	struct resource *res1, *res2;

	for (i = 0; i < info->res_num; i++) {
		res1 = &info->res[i];
		if (!(res1->flags & type))
		if (!(res1->flags & type))
			continue;
			goto next;


		for (j = i + 1; j < info->res_num; j++) {
		/* Exclude non-addressable range or non-addressable portion */
			res2 = &info->res[j];
		end = min(res1->end, root->end);
		if (end <= res1->start) {
			dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
				 res1);
			free = true;
			goto next;
		} else if (res1->end != end) {
			dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
				 res1, (unsigned long long)end + 1,
				 (unsigned long long)res1->end);
			res1->end = end;
		}

		resource_list_for_each_entry(entry2, crs_res) {
			res2 = entry2->res;
			if (!(res2->flags & type))
			if (!(res2->flags & type))
				continue;
				continue;


@@ -355,118 +261,92 @@ static void coalesce_windows(struct pci_root_info *info, unsigned long type)
			if (resource_overlaps(res1, res2)) {
			if (resource_overlaps(res1, res2)) {
				res2->start = min(res1->start, res2->start);
				res2->start = min(res1->start, res2->start);
				res2->end = max(res1->end, res2->end);
				res2->end = max(res1->end, res2->end);
				dev_info(&info->bridge->dev,
				dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
					 "host bridge window expanded to %pR; %pR ignored\n",
					 res2, res1);
					 res2, res1);
				res1->flags = 0;
				free = true;
				goto next;
			}
			}
		}
		}

next:
		resource_list_del(entry);
		if (free)
			resource_list_free_entry(entry);
		else
			resource_list_add_tail(entry, crs_res);
	}
	}
}
}


static void add_resources(struct pci_root_info *info,
static void add_resources(struct pci_root_info *info,
			  struct list_head *resources)
			  struct list_head *resources,
			  struct list_head *crs_res)
{
{
	int i;
	struct resource_entry *entry, *tmp;
	struct resource *res, *root, *conflict;
	struct resource *res, *conflict, *root = NULL;


	coalesce_windows(info, IORESOURCE_MEM);
	validate_resources(&info->bridge->dev, crs_res, IORESOURCE_MEM);
	coalesce_windows(info, IORESOURCE_IO);
	validate_resources(&info->bridge->dev, crs_res, IORESOURCE_IO);

	for (i = 0; i < info->res_num; i++) {
		res = &info->res[i];


	resource_list_for_each_entry_safe(entry, tmp, crs_res) {
		res = entry->res;
		if (res->flags & IORESOURCE_MEM)
		if (res->flags & IORESOURCE_MEM)
			root = &iomem_resource;
			root = &iomem_resource;
		else if (res->flags & IORESOURCE_IO)
		else if (res->flags & IORESOURCE_IO)
			root = &ioport_resource;
			root = &ioport_resource;
		else
		else
			continue;
			BUG_ON(res);


		conflict = insert_resource_conflict(root, res);
		conflict = insert_resource_conflict(root, res);
		if (conflict)
		if (conflict) {
			dev_info(&info->bridge->dev,
			dev_info(&info->bridge->dev,
				 "ignoring host bridge window %pR (conflicts with %s %pR)\n",
				 "ignoring host bridge window %pR (conflicts with %s %pR)\n",
				 res, conflict->name, conflict);
				 res, conflict->name, conflict);
		else
			resource_list_destroy_entry(entry);
			pci_add_resource_offset(resources, res,
					info->res_offset[i]);
		}
		}
	}
	}


static void free_pci_root_info_res(struct pci_root_info *info)
	list_splice_tail(crs_res, resources);
{
	kfree(info->res);
	info->res = NULL;
	kfree(info->res_offset);
	info->res_offset = NULL;
	info->res_num = 0;
}
}


static void __release_pci_root_info(struct pci_root_info *info)
static void release_pci_root_info(struct pci_host_bridge *bridge)
{
{
	int i;
	struct resource *res;
	struct resource *res;
	struct resource_entry *entry;
	struct pci_root_info *info = bridge->release_data;


	for (i = 0; i < info->res_num; i++) {
	resource_list_for_each_entry(entry, &bridge->windows) {
		res = &info->res[i];
		res = entry->res;

		if (res->parent &&
		if (!res->parent)
		    (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
			continue;

		if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
			continue;

			release_resource(res);
			release_resource(res);
	}
	}


	free_pci_root_info_res(info);

	teardown_mcfg_map(info);
	teardown_mcfg_map(info);

	kfree(info);
	kfree(info);
}
}


static void release_pci_root_info(struct pci_host_bridge *bridge)
{
	struct pci_root_info *info = bridge->release_data;

	__release_pci_root_info(info);
}

static void probe_pci_root_info(struct pci_root_info *info,
static void probe_pci_root_info(struct pci_root_info *info,
				struct acpi_device *device,
				struct acpi_device *device,
				int busnum, int domain)
				int busnum, int domain,
				struct list_head *list)
{
{
	size_t size;
	int ret;
	struct resource_entry *entry;


	sprintf(info->name, "PCI Bus %04x:%02x", domain, busnum);
	sprintf(info->name, "PCI Bus %04x:%02x", domain, busnum);
	info->bridge = device;
	info->bridge = device;

	ret = acpi_dev_get_resources(device, list,
	info->res_num = 0;
				     acpi_dev_filter_resource_type_cb,
	acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
				     (void *)(IORESOURCE_IO | IORESOURCE_MEM));
				info);
	if (ret < 0)
	if (!info->res_num)
		dev_warn(&device->dev,
		return;
			 "failed to parse _CRS method, error code %d\n", ret);

	else if (ret == 0)
	size = sizeof(*info->res) * info->res_num;
		dev_dbg(&device->dev,
	info->res = kzalloc_node(size, GFP_KERNEL, info->sd.node);
			"no IO and memory resources present in _CRS\n");
	if (!info->res) {
	else
		info->res_num = 0;
		resource_list_for_each_entry(entry, list)
		return;
			entry->res->name = info->name;
	}

	size = sizeof(*info->res_offset) * info->res_num;
	info->res_num = 0;
	info->res_offset = kzalloc_node(size, GFP_KERNEL, info->sd.node);
	if (!info->res_offset) {
		kfree(info->res);
		info->res = NULL;
		return;
	}

	acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
				info);
}
}


struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
@@ -475,6 +355,8 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
	struct pci_root_info *info;
	struct pci_root_info *info;
	int domain = root->segment;
	int domain = root->segment;
	int busnum = root->secondary.start;
	int busnum = root->secondary.start;
	struct resource_entry *res_entry;
	LIST_HEAD(crs_res);
	LIST_HEAD(resources);
	LIST_HEAD(resources);
	struct pci_bus *bus;
	struct pci_bus *bus;
	struct pci_sysdata *sd;
	struct pci_sysdata *sd;
@@ -522,18 +404,22 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
		memcpy(bus->sysdata, sd, sizeof(*sd));
		memcpy(bus->sysdata, sd, sizeof(*sd));
		kfree(info);
		kfree(info);
	} else {
	} else {
		probe_pci_root_info(info, device, busnum, domain);

		/* insert busn res at first */
		/* insert busn res at first */
		pci_add_resource(&resources,  &root->secondary);
		pci_add_resource(&resources,  &root->secondary);

		/*
		/*
		 * _CRS with no apertures is normal, so only fall back to
		 * _CRS with no apertures is normal, so only fall back to
		 * defaults or native bridge info if we're ignoring _CRS.
		 * defaults or native bridge info if we're ignoring _CRS.
		 */
		 */
		if (pci_use_crs)
		probe_pci_root_info(info, device, busnum, domain, &crs_res);
			add_resources(info, &resources);
		if (pci_use_crs) {
		else {
			add_resources(info, &resources, &crs_res);
			free_pci_root_info_res(info);
		} else {
			resource_list_for_each_entry(res_entry, &crs_res)
				dev_printk(KERN_DEBUG, &device->dev,
					   "host bridge window %pR (ignored)\n",
					   res_entry->res);
			resource_list_free(&crs_res);
			x86_pci_root_bus_resources(busnum, &resources);
			x86_pci_root_bus_resources(busnum, &resources);
		}
		}


@@ -548,8 +434,9 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
				to_pci_host_bridge(bus->bridge),
				to_pci_host_bridge(bus->bridge),
				release_pci_root_info, info);
				release_pci_root_info, info);
		} else {
		} else {
			pci_free_resource_list(&resources);
			resource_list_free(&resources);
			__release_pci_root_info(info);
			teardown_mcfg_map(info);
			kfree(info);
		}
		}
	}
	}