xref: /openbmc/linux/arch/um/kernel/physmem.c (revision 3848d470)
10d1fb0a4SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
36d536e4bSJeff Dike  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
673395a00SAl Viro #include <linux/module.h>
7ddf63983SMike Rapoport #include <linux/memblock.h>
873395a00SAl Viro #include <linux/mm.h>
973395a00SAl Viro #include <linux/pfn.h>
1073395a00SAl Viro #include <asm/page.h>
11d5f20be7SNicolas Iooss #include <asm/sections.h>
1273395a00SAl Viro #include <as-layout.h>
1373395a00SAl Viro #include <init.h>
1473395a00SAl Viro #include <kern.h>
1573395a00SAl Viro #include <mem_user.h>
1673395a00SAl Viro #include <os.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds static int physmem_fd = -1;
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /* Changed during early boot */
211da177e4SLinus Torvalds unsigned long high_physmem;
2273395a00SAl Viro EXPORT_SYMBOL(high_physmem);
231da177e4SLinus Torvalds 
24ae173816SJeff Dike extern unsigned long long physmem_size;
251da177e4SLinus Torvalds 
mem_total_pages(unsigned long physmem,unsigned long iomem,unsigned long highmem)269e6a57d2SHonggang Li void __init mem_total_pages(unsigned long physmem, unsigned long iomem,
2797a1fcbbSJeff Dike 		     unsigned long highmem)
281da177e4SLinus Torvalds {
299e6a57d2SHonggang Li 	unsigned long phys_pages, highmem_pages;
309e6a57d2SHonggang Li 	unsigned long iomem_pages, total_pages;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 	phys_pages    = physmem >> PAGE_SHIFT;
331da177e4SLinus Torvalds 	iomem_pages   = iomem   >> PAGE_SHIFT;
341da177e4SLinus Torvalds 	highmem_pages = highmem >> PAGE_SHIFT;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	total_pages   = phys_pages + iomem_pages + highmem_pages;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	max_mapnr = total_pages;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
map_memory(unsigned long virt,unsigned long phys,unsigned long len,int r,int w,int x)411da177e4SLinus Torvalds void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
421da177e4SLinus Torvalds 		int r, int w, int x)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	__u64 offset;
451da177e4SLinus Torvalds 	int fd, err;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 	fd = phys_mapping(phys, &offset);
481da177e4SLinus Torvalds 	err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
491da177e4SLinus Torvalds 	if (err) {
501da177e4SLinus Torvalds 		if (err == -ENOMEM)
51ba180fd4SJeff Dike 			printk(KERN_ERR "try increasing the host's "
521da177e4SLinus Torvalds 			       "/proc/sys/vm/max_map_count to <physical "
531da177e4SLinus Torvalds 			       "memory size>/4096\n");
541da177e4SLinus Torvalds 		panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
551da177e4SLinus Torvalds 		      "err = %d\n", virt, fd, offset, len, r, w, x, err);
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59fe205bddSThomas Meyer /**
60fe205bddSThomas Meyer  * setup_physmem() - Setup physical memory for UML
61fe205bddSThomas Meyer  * @start:	Start address of the physical kernel memory,
62fe205bddSThomas Meyer  *		i.e start address of the executable image.
63fe205bddSThomas Meyer  * @reserve_end:	end address of the physical kernel memory.
64fe205bddSThomas Meyer  * @len:	Length of total physical memory that should be mapped/made
65fe205bddSThomas Meyer  *		available, in bytes.
66fe205bddSThomas Meyer  * @highmem:	Number of highmem bytes that should be mapped/made available.
67fe205bddSThomas Meyer  *
68fe205bddSThomas Meyer  * Creates an unlinked temporary file of size (len + highmem) and memory maps
69fe205bddSThomas Meyer  * it on the last executable image address (uml_reserved).
70fe205bddSThomas Meyer  *
71fe205bddSThomas Meyer  * The offset is needed as the length of the total physical memory
72fe205bddSThomas Meyer  * (len + highmem) includes the size of the memory used be the executable image,
73fe205bddSThomas Meyer  * but the mapped-to address is the last address of the executable image
74fe205bddSThomas Meyer  * (uml_reserved == end address of executable image).
75fe205bddSThomas Meyer  *
76fe205bddSThomas Meyer  * The memory mapped memory of the temporary file is used as backing memory
77fe205bddSThomas Meyer  * of all user space processes/kernel tasks.
78fe205bddSThomas Meyer  */
setup_physmem(unsigned long start,unsigned long reserve_end,unsigned long len,unsigned long long highmem)7997a1fcbbSJeff Dike void __init setup_physmem(unsigned long start, unsigned long reserve_end,
80ae173816SJeff Dike 			  unsigned long len, unsigned long long highmem)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	unsigned long reserve = reserve_end - start;
83ddf63983SMike Rapoport 	long map_size = len - reserve;
84fe205bddSThomas Meyer 	int err;
85fe205bddSThomas Meyer 
86fe205bddSThomas Meyer 	if(map_size <= 0) {
870936d4f3SMasami Hiramatsu 		os_warn("Too few physical memory! Needed=%lu, given=%lu\n",
88ddf63983SMike Rapoport 			reserve, len);
89fe205bddSThomas Meyer 		exit(1);
90fe205bddSThomas Meyer 	}
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	physmem_fd = create_mem_file(len + highmem);
931da177e4SLinus Torvalds 
94ddf63983SMike Rapoport 	err = os_map_memory((void *) reserve_end, physmem_fd, reserve,
95fe205bddSThomas Meyer 			    map_size, 1, 1, 1);
961da177e4SLinus Torvalds 	if (err < 0) {
970936d4f3SMasami Hiramatsu 		os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p "
98fe205bddSThomas Meyer 			"failed - errno = %d\n", map_size,
99be6ec5b1SMike Rapoport 			(void *) reserve_end, err);
1001da177e4SLinus Torvalds 		exit(1);
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 
103ba180fd4SJeff Dike 	/*
104ba180fd4SJeff Dike 	 * Special kludge - This page will be mapped in to userspace processes
105d67b569fSJeff Dike 	 * from physmem_fd, so it needs to be written out there.
106d67b569fSJeff Dike 	 */
10705eacfd0SNicolas Iooss 	os_seek_file(physmem_fd, __pa(__syscall_stub_start));
10805eacfd0SNicolas Iooss 	os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE);
1090565103dSAnton Ivanov 	os_fsync_file(physmem_fd);
110d67b569fSJeff Dike 
111ddf63983SMike Rapoport 	memblock_add(__pa(start), len + highmem);
112ddf63983SMike Rapoport 	memblock_reserve(__pa(start), reserve);
113ddf63983SMike Rapoport 
114ddf63983SMike Rapoport 	min_low_pfn = PFN_UP(__pa(reserve_end));
115ddf63983SMike Rapoport 	max_low_pfn = min_low_pfn + (map_size >> PAGE_SHIFT);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
phys_mapping(unsigned long phys,unsigned long long * offset_out)1180a7675aaSJeff Dike int phys_mapping(unsigned long phys, unsigned long long *offset_out)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int fd = -1;
1211da177e4SLinus Torvalds 
12216dd07bcSJeff Dike 	if (phys < physmem_size) {
1231da177e4SLinus Torvalds 		fd = physmem_fd;
1241da177e4SLinus Torvalds 		*offset_out = phys;
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds 	else if (phys < __pa(end_iomem)) {
1271da177e4SLinus Torvalds 		struct iomem_region *region = iomem_regions;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 		while (region != NULL) {
1301da177e4SLinus Torvalds 			if ((phys >= region->phys) &&
1311da177e4SLinus Torvalds 			    (phys < region->phys + region->size)) {
1321da177e4SLinus Torvalds 				fd = region->fd;
1331da177e4SLinus Torvalds 				*offset_out = phys - region->phys;
1341da177e4SLinus Torvalds 				break;
1351da177e4SLinus Torvalds 			}
1361da177e4SLinus Torvalds 			region = region->next;
1371da177e4SLinus Torvalds 		}
1381da177e4SLinus Torvalds 	}
1391da177e4SLinus Torvalds 	else if (phys < __pa(end_iomem) + highmem) {
1401da177e4SLinus Torvalds 		fd = physmem_fd;
1411da177e4SLinus Torvalds 		*offset_out = phys - iomem_size;
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 
14460678bbcSJeff Dike 	return fd;
1451da177e4SLinus Torvalds }
1465d38f324SErel Geron EXPORT_SYMBOL(phys_mapping);
1471da177e4SLinus Torvalds 
uml_mem_setup(char * line,int * add)1481da177e4SLinus Torvalds static int __init uml_mem_setup(char *line, int *add)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	char *retptr;
1511da177e4SLinus Torvalds 	physmem_size = memparse(line,&retptr);
1521da177e4SLinus Torvalds 	return 0;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds __uml_setup("mem=", uml_mem_setup,
1551da177e4SLinus Torvalds "mem=<Amount of desired ram>\n"
1561da177e4SLinus Torvalds "    This controls how much \"physical\" memory the kernel allocates\n"
1571da177e4SLinus Torvalds "    for the system. The size is specified as a number followed by\n"
1581da177e4SLinus Torvalds "    one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
1591da177e4SLinus Torvalds "    This is not related to the amount of memory in the host.  It can\n"
1601da177e4SLinus Torvalds "    be more, and the excess, if it's ever used, will just be swapped out.\n"
1611da177e4SLinus Torvalds "	Example: mem=64M\n\n"
1621da177e4SLinus Torvalds );
1631da177e4SLinus Torvalds 
16494c282d7SJeff Dike extern int __init parse_iomem(char *str, int *add);
16594c282d7SJeff Dike 
16694c282d7SJeff Dike __uml_setup("iomem=", parse_iomem,
16794c282d7SJeff Dike "iomem=<name>,<file>\n"
16894c282d7SJeff Dike "    Configure <file> as an IO memory region named <name>.\n\n"
16994c282d7SJeff Dike );
17094c282d7SJeff Dike 
17194c282d7SJeff Dike /*
172*3848d470SJason Wang  * This list is constructed in parse_iomem and addresses filled in
17394c282d7SJeff Dike  * setup_iomem, both of which run during early boot.  Afterwards, it's
17494c282d7SJeff Dike  * unchanged.
17594c282d7SJeff Dike  */
17680e39311SJeff Dike struct iomem_region *iomem_regions;
17794c282d7SJeff Dike 
17880e39311SJeff Dike /* Initialized in parse_iomem and unchanged thereafter */
17980e39311SJeff Dike int iomem_size;
18094c282d7SJeff Dike 
find_iomem(char * driver,unsigned long * len_out)1811da177e4SLinus Torvalds unsigned long find_iomem(char *driver, unsigned long *len_out)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct iomem_region *region = iomem_regions;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	while (region != NULL) {
1861da177e4SLinus Torvalds 		if (!strcmp(region->driver, driver)) {
1871da177e4SLinus Torvalds 			*len_out = region->size;
18860678bbcSJeff Dike 			return region->virt;
1891da177e4SLinus Torvalds 		}
190c39e50b4SVictor V. Vengerov 
191c39e50b4SVictor V. Vengerov 		region = region->next;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
19460678bbcSJeff Dike 	return 0;
1951da177e4SLinus Torvalds }
19673395a00SAl Viro EXPORT_SYMBOL(find_iomem);
1971da177e4SLinus Torvalds 
setup_iomem(void)19899764fa4SWANG Cong static int setup_iomem(void)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct iomem_region *region = iomem_regions;
2011da177e4SLinus Torvalds 	unsigned long iomem_start = high_physmem + PAGE_SIZE;
2021da177e4SLinus Torvalds 	int err;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	while (region != NULL) {
2051da177e4SLinus Torvalds 		err = os_map_memory((void *) iomem_start, region->fd, 0,
2061da177e4SLinus Torvalds 				    region->size, 1, 1, 0);
2071da177e4SLinus Torvalds 		if (err)
208ba180fd4SJeff Dike 			printk(KERN_ERR "Mapping iomem region for driver '%s' "
209ba180fd4SJeff Dike 			       "failed, errno = %d\n", region->driver, -err);
2101da177e4SLinus Torvalds 		else {
2111da177e4SLinus Torvalds 			region->virt = iomem_start;
2121da177e4SLinus Torvalds 			region->phys = __pa(region->virt);
2131da177e4SLinus Torvalds 		}
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 		iomem_start += region->size + PAGE_SIZE;
2161da177e4SLinus Torvalds 		region = region->next;
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
21960678bbcSJeff Dike 	return 0;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds __initcall(setup_iomem);
223