Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 8 Feb 2023 18:26:25 -0400
From:      Mitchell Horne <mhorne@freebsd.org>
To:        Jessica Clarke <jrtc27@freebsd.org>
Cc:        "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org>
Subject:   Re: git: e6cf1a0826c9 - main - physmem: add ram0 pseudo-driver
Message-ID:  <5cfc2de7-4b08-283b-b9fc-c990f236ff3e@freebsd.org>
In-Reply-To: <505D72C8-1F67-46C8-A6EF-39392841FBF5@freebsd.org>
References:  <202302082052.318KqOE6052473@gitrepo.freebsd.org> <26339898-C02C-42D4-916A-2B6AD2562B02@freebsd.org> <d2156648-6181-6721-12e3-dae5ae15e49c@freebsd.org> <505D72C8-1F67-46C8-A6EF-39392841FBF5@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2/8/23 17:13, Jessica Clarke wrote:
> On 8 Feb 2023, at 21:06, Mitchell Horne <mhorne@freebsd.org> wrote:
>>
>> On 2/8/23 16:55, Jessica Clarke wrote:
>>> On 8 Feb 2023, at 20:52, Mitchell Horne <mhorne@FreeBSD.org> wrote:
>>>>
>>>> The branch main has been updated by mhorne:
>>>>
>>>> URL: https://cgit.FreeBSD.org/src/commit/?id=e6cf1a0826c9d7f229e41224ec7b783501636528
>>>>
>>>> commit e6cf1a0826c9d7f229e41224ec7b783501636528
>>>> Author:     Mitchell Horne <mhorne@FreeBSD.org>
>>>> AuthorDate: 2021-05-27 14:27:40 +0000
>>>> Commit:     Mitchell Horne <mhorne@FreeBSD.org>
>>>> CommitDate: 2023-02-08 20:50:46 +0000
>>>>
>>>>     physmem: add ram0 pseudo-driver
>>>>
>>>>     Its purpose is to reserve all I/O space belonging to physical memory
>>> If it’s RAM then it’s not I/O, and if it’s I/O then it’s not RAM?
>>> Jess
>>
>> Yes, they are distinct. And yet, they share an address space. So this is to assist in the SYS_RES_MEMORY resource bookkeeping. Did I misuse some terminology, or what is your actual question?
> 
> The commit message just doesn’t make sense to me, it switches between
> talking about I/O space and memory (same applies to the comments in the
> code). Is this reserving I/O parts of the address space so
> SYS_RES_MEMORY doesn’t include them (which doesn’t *belong* to physical
> memory, just resides in the same address space), reserving
> FDT/ACPI-reserved actual RAM memory regions so SYS_RES_MEMORY doesn’t
> include them (which isn’t I/O space), or both? Currently it reads to me
> as describing part of one and part of the other, but neither fully.
> 
> Jess

Okay sure, perhaps the term I am searching for but missing is "physical 
address space". We know that some regions of physical address space 
correspond to RAM/memory/"physical memory". Other portions of the 
address space correspond to memory-mapped I/O regions belonging to one 
or more devices.

Device drivers allocate SYS_RES_MEMORY resources using e.g. 
bus_alloc_resource(). They do this to declare ownership of the 
particular range of physical address space that they will use for I/O. 
If it's not RF_SHAREABLE, then it can't be handed out again.

Memory/RAM is handed out by a different set of APIs, and therefore never 
touches SYS_RES_MEMORY resources. However, it is true that the 
(tangible) physical memory in the system maps to a set of regions in the 
(abstract) physical address space. This is the sense in which I used the 
word "belong".

The purpose of the change is to reserve, from the system-wide 
SYS_RES_MEMORY rman, those portions of the physical address space that 
correspond to real physical memory/RAM.

> 
>>>>     from nexus, preventing it from being handed out by bus_alloc_resource()
>>>>     to callers such as xenpv_alloc_physmem(), which looks for the first
>>>>     available free range it can get. This mimics the existing pseudo-driver
>>>>     on x86.
>>>>
>>>>     If needed, the device can be disabled with hint.ram.0.disabled="1" in
>>>>     /boot/device.hints.
>>>>
>>>>     Reviewed by:    imp
>>>>     MFC after:      1 month
>>>>     Differential Revision:  https://reviews.freebsd.org/D32343
>>>> ---
>>>> sys/kern/subr_physmem.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++-
>>>> 1 file changed, 99 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/sys/kern/subr_physmem.c b/sys/kern/subr_physmem.c
>>>> index 498ad2440f40..bb6af5a580aa 100644
>>>> --- a/sys/kern/subr_physmem.c
>>>> +++ b/sys/kern/subr_physmem.c
>>>> @@ -40,7 +40,9 @@ __FBSDID("$FreeBSD$");
>>>>
>>>> #include <sys/param.h>
>>>> #include <sys/systm.h>
>>>> +#include <sys/bus.h>
>>>> #include <sys/kernel.h>
>>>> +#include <sys/module.h>
>>>> #include <sys/physmem.h>
>>>>
>>>> #ifdef _KERNEL
>>>> @@ -49,7 +51,9 @@ __FBSDID("$FreeBSD$");
>>>> #include <vm/vm_page.h>
>>>> #include <vm/vm_phys.h>
>>>> #include <vm/vm_dumpset.h>
>>>> +
>>>> #include <machine/md_var.h>
>>>> +#include <machine/resource.h>
>>>> #else
>>>> #include <stdarg.h>
>>>> #include <stdio.h>
>>>> @@ -524,7 +528,6 @@ physmem_init_kernel_globals(void)
>>>> 		panic("No memory entries in phys_avail");
>>>> 	Maxmem = atop(phys_avail[nextidx - 1]);
>>>> }
>>>> -#endif
>>>>
>>>> #ifdef DDB
>>>> #include <ddb/ddb.h>
>>>> @@ -536,3 +539,98 @@ DB_SHOW_COMMAND_FLAGS(physmem, db_show_physmem, DB_CMD_MEMSAFE)
>>>> }
>>>>
>>>> #endif /* DDB */
>>>> +
>>>> +/*
>>>> + * ram pseudo driver - this reserves I/O space resources corresponding to physical
>>>> + * memory regions.
>>>> + */
>>>> +
>>>> +static void
>>>> +ram_identify(driver_t *driver, device_t parent)
>>>> +{
>>>> +
>>>> +	if (resource_disabled("ram", 0))
>>>> +		return;
>>>> +	if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
>>>> +		panic("ram_identify");
>>>> +}
>>>> +
>>>> +static int
>>>> +ram_probe(device_t dev)
>>>> +{
>>>> +
>>>> +	device_quiet(dev);
>>>> +	device_set_desc(dev, "System RAM");
>>>> +	return (BUS_PROBE_SPECIFIC);
>>>> +}
>>>> +
>>>> +static int
>>>> +ram_attach(device_t dev)
>>>> +{
>>>> +	vm_paddr_t avail_list[PHYS_AVAIL_COUNT];
>>>> +	rman_res_t start, end;
>>>> +	struct region *hwp;
>>>> +	int rid, i;
>>>> +
>>>> +	rid = 0;
>>>> +
>>>> +	/* Get the avail list. */
>>>> +	bzero(avail_list, sizeof(avail_list));
>>>> +	regions_to_avail(avail_list, EXFLAG_NOALLOC | EXFLAG_NODUMP,
>>>> +	    PHYS_AVAIL_COUNT, 0, NULL, NULL);
>>>> +
>>>> +	/* Reserve all memory regions. */
>>>> +	for (i = 0; avail_list[i + 1] != 0; i += 2) {
>>>> +		start = avail_list[i];
>>>> +		end = avail_list[i + 1];
>>>> +
>>>> +		if (bootverbose)
>>>> +			device_printf(dev,
>>>> +			    "reserving memory region:   %jx-%jx\n",
>>>> +			    (uintmax_t)start, (uintmax_t)end);
>>>> +
>>>> +		if (bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, end,
>>>> +		    end - start, 0) == NULL)
>>>> +			panic("ram_attach: resource %d failed to attach", rid);
>>>> +		rid++;
>>>> +	}
>>>> +
>>>> +	/* Now, reserve the excluded memory regions. */
>>>> +	for (i = 0, hwp = exregions; i < excnt; i++, hwp++) {
>>>> +		start = hwp->addr;
>>>> +		end = hwp->addr + hwp->size;
>>>> +
>>>> +		if (bootverbose)
>>>> +			device_printf(dev,
>>>> +			    "reserving excluded region: %jx-%jx\n",
>>>> +			    (uintmax_t)start, (uintmax_t)(end - 1));
>>>> +
>>>> +		/*
>>>> +		 * Best-effort attempt to reserve the range. This may fail, as
>>>> +		 * sometimes the excluded ranges provided by the device tree
>>>> +		 * will cover or overlap some I/O range.
>>>> +		 */
>>>> +		if (bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, end,
>>>> +		    end - start, 0) == NULL) {
>>>> +			if (bootverbose)
>>>> +				device_printf(dev, "failed to reserve region\n");
>>>> +			continue;
>>>> +		}
>>>> +		rid++;
>>>> +	}
>>>> +
>>>> +	return (0);
>>>> +}
>>>> +
>>>> +static device_method_t ram_methods[] = {
>>>> +	/* Device interface */
>>>> +	DEVMETHOD(device_identify,	ram_identify),
>>>> +	DEVMETHOD(device_probe,		ram_probe),
>>>> +	DEVMETHOD(device_attach,	ram_attach),
>>>> +
>>>> +	DEVMETHOD_END
>>>> +};
>>>> +
>>>> +DEFINE_CLASS_0(ram, ram_driver, ram_methods, /* no softc */ 1);
>>>> +DRIVER_MODULE(ram, nexus, ram_driver, 0, 0);
>>>> +#endif /* _KERNEL */
> 




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5cfc2de7-4b08-283b-b9fc-c990f236ff3e>