162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2016, Intel Corporation.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#include "test/nfit_test.h"
662306a36Sopenharmony_ci#include <linux/mm.h>
762306a36Sopenharmony_ci#include "../../../drivers/dax/dax-private.h"
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciphys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff,
1062306a36Sopenharmony_ci		unsigned long size)
1162306a36Sopenharmony_ci{
1262306a36Sopenharmony_ci	int i;
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci	for (i = 0; i < dev_dax->nr_range; i++) {
1562306a36Sopenharmony_ci		struct dev_dax_range *dax_range = &dev_dax->ranges[i];
1662306a36Sopenharmony_ci		struct range *range = &dax_range->range;
1762306a36Sopenharmony_ci		unsigned long long pgoff_end;
1862306a36Sopenharmony_ci		phys_addr_t addr;
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci		pgoff_end = dax_range->pgoff + PHYS_PFN(range_len(range)) - 1;
2162306a36Sopenharmony_ci		if (pgoff < dax_range->pgoff || pgoff > pgoff_end)
2262306a36Sopenharmony_ci			continue;
2362306a36Sopenharmony_ci		addr = PFN_PHYS(pgoff - dax_range->pgoff) + range->start;
2462306a36Sopenharmony_ci		if (addr + size - 1 <= range->end) {
2562306a36Sopenharmony_ci			if (get_nfit_res(addr)) {
2662306a36Sopenharmony_ci				struct page *page;
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci				if (dev_dax->region->align > PAGE_SIZE)
2962306a36Sopenharmony_ci					return -1;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci				page = vmalloc_to_page((void *)addr);
3262306a36Sopenharmony_ci				return PFN_PHYS(page_to_pfn(page));
3362306a36Sopenharmony_ci			}
3462306a36Sopenharmony_ci			return addr;
3562306a36Sopenharmony_ci		}
3662306a36Sopenharmony_ci		break;
3762306a36Sopenharmony_ci	}
3862306a36Sopenharmony_ci	return -1;
3962306a36Sopenharmony_ci}
40