Skip to content

Commit 78ab675

Browse files
Gregory Pricedavejiang
authored andcommitted
cxl: docs/allocation/dax
Small example of accessing CXL memory capacity via DAX device Signed-off-by: Gregory Price <gourry@gourry.net> Link: https://patch.msgid.link/20250512162134.3596150-14-gourry@gourry.net Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 641fdea commit 78ab675

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
===========
4+
DAX Devices
5+
===========
6+
CXL capacity exposed as a DAX device can be accessed directly via mmap.
7+
Users may wish to use this interface mechanism to write their own userland
8+
CXL allocator, or to managed shared or persistent memory regions across multiple
9+
hosts.
10+
11+
If the capacity is shared across hosts or persistent, appropriate flushing
12+
mechanisms must be employed unless the region supports Snoop Back-Invalidate.
13+
14+
Note that mappings must be aligned (size and base) to the dax device's base
15+
alignment, which is typically 2MB - but maybe be configured larger.
16+
17+
::
18+
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
#include <stdint.h>
22+
#include <sys/mman.h>
23+
#include <fcntl.h>
24+
#include <unistd.h>
25+
26+
#define DEVICE_PATH "/dev/dax0.0" // Replace DAX device path
27+
#define DEVICE_SIZE (4ULL * 1024 * 1024 * 1024) // 4GB
28+
29+
int main() {
30+
int fd;
31+
void* mapped_addr;
32+
33+
/* Open the DAX device */
34+
fd = open(DEVICE_PATH, O_RDWR);
35+
if (fd < 0) {
36+
perror("open");
37+
return -1;
38+
}
39+
40+
/* Map the device into memory */
41+
mapped_addr = mmap(NULL, DEVICE_SIZE, PROT_READ | PROT_WRITE,
42+
MAP_SHARED, fd, 0);
43+
if (mapped_addr == MAP_FAILED) {
44+
perror("mmap");
45+
close(fd);
46+
return -1;
47+
}
48+
49+
printf("Mapped address: %p\n", mapped_addr);
50+
51+
/* You can now access the device through the mapped address */
52+
uint64_t* ptr = (uint64_t*)mapped_addr;
53+
*ptr = 0x1234567890abcdef; // Write a value to the device
54+
printf("Value at address %p: 0x%016llx\n", ptr, *ptr);
55+
56+
/* Clean up */
57+
munmap(mapped_addr, DEVICE_SIZE);
58+
close(fd);
59+
return 0;
60+
}

Documentation/driver-api/cxl/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@ that have impacts on each other. The docs here break up configurations steps.
4040
linux/memory-hotplug
4141
linux/access-coordinates
4242

43+
.. toctree::
44+
:maxdepth: 2
45+
:caption: Memory Allocation
46+
47+
allocation/dax
4348

4449
.. only:: subproject and html

0 commit comments

Comments
 (0)