linux devm_xxx介面相關的iomem_resource管理__linux

來源:互聯網
上載者:User
struct resource {
resource_size_t start;
resource_size_t end;
const char *name;
unsigned long flags;
struct resource *parent, *sibling, *child;

};

//resource資源樹,會先找sibling節點然後再找子節點,sibling代表的所有子節點的region加起來等於sibling的本身的region

struct resource iomem_resource = {
.name = "PCI mem",
.start = 0,
.end = -1,
.flags = IORESOURCE_MEM,
};
EXPORT_SYMBOL(iomem_resource);

可見iomem_resource 代表了整個系統的所有物理位址範圍

void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
{
resource_size_t size;
const char *name;
void __iomem *dest_ptr;


BUG_ON(!dev);


if (!res || resource_type(res) != IORESOURCE_MEM) {
dev_err(dev, "invalid resource\n");
return IOMEM_ERR_PTR(-EINVAL);
}


size = resource_size(res);
name = res->name ?: dev_name(dev);

//擷取一塊memory region
if (!devm_request_mem_region(dev, res->start, size, name)) {
dev_err(dev, "can't request region for resource %pR\n", res);
return IOMEM_ERR_PTR(-EBUSY);
}

//註冊mem region成功後會進行  ioremap
if (res->flags & IORESOURCE_CACHEABLE)
dest_ptr = devm_ioremap(dev, res->start, size);
else
dest_ptr = devm_ioremap_nocache(dev, res->start, size);


if (!dest_ptr) {
dev_err(dev, "ioremap failed for resource %pR\n", res);
devm_release_mem_region(dev, res->start, size);
dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
}


return dest_ptr;
}
EXPORT_SYMBOL(devm_ioremap_resource);


struct resource * __devm_request_region(struct device *dev,
struct resource *parent, resource_size_t start,
resource_size_t n, const char *name)
{
struct region_devres *dr = NULL;
struct resource *res;


dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
 GFP_KERNEL);//註冊該資源的釋放函數
if (!dr)
return NULL;

dr->parent = parent;
dr->start = start;
dr->n = n;

//註冊到iomem_resource樹中
res = __request_region(parent, start, n, name, 0);
if (res)
devres_add(dev, dr);
else
devres_free(dr);


return res;
}
EXPORT_SYMBOL(__devm_request_region);


struct resource * __request_region(struct resource *parent,
  resource_size_t start, resource_size_t n,
  const char *name, int flags)
{
DECLARE_WAITQUEUE(wait, current);
struct resource *res = alloc_resource(GFP_KERNEL);


if (!res)
return NULL;


res->name = name;
res->start = start;
res->end = start + n - 1;
res->flags = resource_type(parent);
res->flags |= IORESOURCE_BUSY | flags;


write_lock(&resource_lock);
//資源管理的讀寫鎖,多個讀可以任意進入保護區,同時只能有一個寫

for (;;) {
struct resource *conflict;


conflict = __request_resource(parent, res);
if (!conflict)//遍曆資源樹找到與該資源可用的節點,或者建立了一個新的資源節點
break;
if (conflict != parent) {
parent = conflict;
if (!(conflict->flags & IORESOURCE_BUSY))
continue;
}//如果一時拿不到資源的話會休眠,等待釋放函數wakeup 等待隊列muxed_resource_wait
if (conflict->flags & flags & IORESOURCE_MUXED) {
add_wait_queue(&muxed_resource_wait, &wait);
write_unlock(&resource_lock);
set_current_state(TASK_UNINTERRUPTIBLE);
schedule();
remove_wait_queue(&muxed_resource_wait, &wait);
write_lock(&resource_lock);
continue;
}
/* Uhhuh, that didn't work out.. */
free_resource(res);
res = NULL;
break;
}
write_unlock(&resource_lock);
return res;
}
EXPORT_SYMBOL(__request_region);

=============================================================================================

自己在使用devm_ioremap_resource介面的時候kernel ooops提示can not request region

追蹤是該介面是會去一個全域唯一的管理所有region的資源樹中尋找是否有滿足的region,如果已經有人佔用了這塊region會返回conflict 

換用介面devm_ioremap


void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
  unsigned long size)
{
void __iomem **ptr, *addr;


ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return NULL;

//可以比較明顯的看出這其中是沒有mem region的申請操作的
addr = ioremap(offset, size);
if (addr) {
*ptr = addr;
devres_add(dev, ptr);
} else
devres_free(ptr);


return addr;
}
EXPORT_SYMBOL(devm_ioremap);


 cat /proc/iomem 可以查看系統當前的通過devm_request_mem_region申請的mem region記憶體資源的佔用情況

mem  resource 的管理方式


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.