Commit 0092908d authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jason Gunthorpe

mm: factor out a devm_request_free_mem_region helper

Keep the physical address allocation that hmm_add_device does with the
rest of the resource code, and allow future reuse of it without the hmm
wrapper.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarJason Gunthorpe <jgg@mellanox.com>
Reviewed-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
Reviewed-by: default avatarDan Williams <dan.j.williams@intel.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent 69262215
...@@ -285,6 +285,8 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2) ...@@ -285,6 +285,8 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
return (r1->start <= r2->end && r1->end >= r2->start); return (r1->start <= r2->end && r1->end >= r2->start);
} }
struct resource *devm_request_free_mem_region(struct device *dev,
struct resource *base, unsigned long size);
#endif /* __ASSEMBLY__ */ #endif /* __ASSEMBLY__ */
#endif /* _LINUX_IOPORT_H */ #endif /* _LINUX_IOPORT_H */
...@@ -1628,6 +1628,45 @@ void resource_list_free(struct list_head *head) ...@@ -1628,6 +1628,45 @@ void resource_list_free(struct list_head *head)
} }
EXPORT_SYMBOL(resource_list_free); EXPORT_SYMBOL(resource_list_free);
#ifdef CONFIG_DEVICE_PRIVATE
/**
* devm_request_free_mem_region - find free region for device private memory
*
* @dev: device struct to bind the resource to
* @size: size in bytes of the device memory to add
* @base: resource tree to look in
*
* This function tries to find an empty range of physical address big enough to
* contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
* memory, which in turn allocates struct pages.
*/
struct resource *devm_request_free_mem_region(struct device *dev,
struct resource *base, unsigned long size)
{
resource_size_t end, addr;
struct resource *res;
size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
addr = end - size + 1UL;
for (; addr > size && addr >= base->start; addr -= size) {
if (region_intersects(addr, size, 0, IORES_DESC_NONE) !=
REGION_DISJOINT)
continue;
res = devm_request_mem_region(dev, addr, size, dev_name(dev));
if (!res)
return ERR_PTR(-ENOMEM);
res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
return res;
}
return ERR_PTR(-ERANGE);
}
EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
#endif /* CONFIG_DEVICE_PRIVATE */
static int __init strict_iomem(char *str) static int __init strict_iomem(char *str)
{ {
if (strstr(str, "relaxed")) if (strstr(str, "relaxed"))
......
...@@ -25,8 +25,6 @@ ...@@ -25,8 +25,6 @@
#include <linux/mmu_notifier.h> #include <linux/mmu_notifier.h>
#include <linux/memory_hotplug.h> #include <linux/memory_hotplug.h>
#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
#if IS_ENABLED(CONFIG_HMM_MIRROR) #if IS_ENABLED(CONFIG_HMM_MIRROR)
static const struct mmu_notifier_ops hmm_mmu_notifier_ops; static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
...@@ -1408,7 +1406,6 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, ...@@ -1408,7 +1406,6 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
unsigned long size) unsigned long size)
{ {
struct hmm_devmem *devmem; struct hmm_devmem *devmem;
resource_size_t addr;
void *result; void *result;
int ret; int ret;
...@@ -1430,32 +1427,10 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, ...@@ -1430,32 +1427,10 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
if (ret) if (ret)
return ERR_PTR(ret); return ERR_PTR(ret);
size = ALIGN(size, PA_SECTION_SIZE); devmem->resource = devm_request_free_mem_region(device, &iomem_resource,
addr = min((unsigned long)iomem_resource.end, size);
(1UL << MAX_PHYSMEM_BITS) - 1); if (IS_ERR(devmem->resource))
addr = addr - size + 1UL; return ERR_CAST(devmem->resource);
/*
* FIXME add a new helper to quickly walk resource tree and find free
* range
*
* FIXME what about ioport_resource resource ?
*/
for (; addr > size && addr >= iomem_resource.start; addr -= size) {
ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
if (ret != REGION_DISJOINT)
continue;
devmem->resource = devm_request_mem_region(device, addr, size,
dev_name(device));
if (!devmem->resource)
return ERR_PTR(-ENOMEM);
break;
}
if (!devmem->resource)
return ERR_PTR(-ERANGE);
devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
devmem->pfn_last = devmem->pfn_first + devmem->pfn_last = devmem->pfn_first +
(resource_size(devmem->resource) >> PAGE_SHIFT); (resource_size(devmem->resource) >> PAGE_SHIFT);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment