Index: head/sys/dev/nvdimm/nvdimm.c =================================================================== --- head/sys/dev/nvdimm/nvdimm.c (revision 343627) +++ head/sys/dev/nvdimm/nvdimm.c (revision 343628) @@ -1,364 +1,409 @@ /*- * Copyright (c) 2017 The FreeBSD Foundation * All rights reserved. * Copyright (c) 2018, 2019 Intel Corporation * * This software was developed by Konstantin Belousov * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_acpi.h" #include "opt_ddb.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #define _COMPONENT ACPI_OEM ACPI_MODULE_NAME("NVDIMM") static devclass_t nvdimm_devclass; static devclass_t nvdimm_root_devclass; MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory"); struct nvdimm_dev * nvdimm_find_by_handle(nfit_handle_t nv_handle) { struct nvdimm_dev *res; device_t *dimms; int i, error, num_dimms; res = NULL; error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms); if (error != 0) return (NULL); for (i = 0; i < num_dimms; i++) { if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) { res = device_get_softc(dimms[i]); break; } } free(dimms, M_TEMP); return (res); } static int nvdimm_parse_flush_addr(void *nfitsubtbl, void *arg) { ACPI_NFIT_FLUSH_ADDRESS *nfitflshaddr; struct nvdimm_dev *nv; int i; nfitflshaddr = nfitsubtbl; nv = arg; if (nfitflshaddr->DeviceHandle != nv->nv_handle) return (0); MPASS(nv->nv_flush_addr == NULL && nv->nv_flush_addr_cnt == 0); nv->nv_flush_addr = mallocarray(nfitflshaddr->HintCount, sizeof(uint64_t *), M_NVDIMM, M_WAITOK); for (i = 0; i < nfitflshaddr->HintCount; i++) nv->nv_flush_addr[i] = (uint64_t *)nfitflshaddr->HintAddress[i]; nv->nv_flush_addr_cnt = nfitflshaddr->HintCount; return (0); } int nvdimm_iterate_nfit(ACPI_TABLE_NFIT *nfitbl, enum AcpiNfitType type, int (*cb)(void *, void *), void *arg) { ACPI_NFIT_HEADER *nfithdr; ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr; ACPI_NFIT_MEMORY_MAP *nfitmap; ACPI_NFIT_INTERLEAVE *nfitintrl; ACPI_NFIT_SMBIOS *nfitsmbios; ACPI_NFIT_CONTROL_REGION *nfitctlreg; ACPI_NFIT_DATA_REGION *nfitdtreg; ACPI_NFIT_FLUSH_ADDRESS *nfitflshaddr; char *ptr; int error; error = 0; for (ptr = (char *)(nfitbl + 1); ptr < (char *)nfitbl + nfitbl->Header.Length; ptr += nfithdr->Length) { nfithdr = (ACPI_NFIT_HEADER *)ptr; if (nfithdr->Type != type) continue; switch (nfithdr->Type) { case ACPI_NFIT_TYPE_SYSTEM_ADDRESS: nfitaddr = __containerof(nfithdr, ACPI_NFIT_SYSTEM_ADDRESS, Header); error = cb(nfitaddr, arg); break; case ACPI_NFIT_TYPE_MEMORY_MAP: nfitmap = __containerof(nfithdr, ACPI_NFIT_MEMORY_MAP, Header); error = cb(nfitmap, arg); break; case ACPI_NFIT_TYPE_INTERLEAVE: nfitintrl = __containerof(nfithdr, ACPI_NFIT_INTERLEAVE, Header); error = cb(nfitintrl, arg); break; case ACPI_NFIT_TYPE_SMBIOS: nfitsmbios = __containerof(nfithdr, ACPI_NFIT_SMBIOS, Header); error = cb(nfitsmbios, arg); break; case ACPI_NFIT_TYPE_CONTROL_REGION: nfitctlreg = __containerof(nfithdr, ACPI_NFIT_CONTROL_REGION, Header); error = cb(nfitctlreg, arg); break; case ACPI_NFIT_TYPE_DATA_REGION: nfitdtreg = __containerof(nfithdr, ACPI_NFIT_DATA_REGION, Header); error = cb(nfitdtreg, arg); break; case ACPI_NFIT_TYPE_FLUSH_ADDRESS: nfitflshaddr = __containerof(nfithdr, ACPI_NFIT_FLUSH_ADDRESS, Header); error = cb(nfitflshaddr, arg); break; case ACPI_NFIT_TYPE_RESERVED: default: if (bootverbose) printf("NFIT subtype %d unknown\n", nfithdr->Type); error = 0; break; } if (error != 0) break; } return (error); } static int nvdimm_probe(device_t dev) { return (BUS_PROBE_NOWILDCARD); } static int nvdimm_attach(device_t dev) { struct nvdimm_dev *nv; ACPI_TABLE_NFIT *nfitbl; ACPI_HANDLE handle; ACPI_STATUS status; nv = device_get_softc(dev); handle = nvdimm_root_get_acpi_handle(dev); if (handle == NULL) return (EINVAL); nv->nv_dev = dev; nv->nv_handle = nvdimm_root_get_device_handle(dev); status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl); if (ACPI_FAILURE(status)) { if (bootverbose) device_printf(dev, "cannot get NFIT\n"); return (ENXIO); } nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_FLUSH_ADDRESS, nvdimm_parse_flush_addr, nv); AcpiPutTable(&nfitbl->Header); return (0); } static int nvdimm_detach(device_t dev) { struct nvdimm_dev *nv; nv = device_get_softc(dev); free(nv->nv_flush_addr, M_NVDIMM); return (0); } static int nvdimm_suspend(device_t dev) { return (0); } static int nvdimm_resume(device_t dev) { return (0); } +static int +nvdimm_root_create_spa(void *nfitsubtbl, void *arg) +{ + enum SPA_mapping_type spa_type; + ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr; + struct SPA_mapping *spa; + struct nvdimm_root_dev *dev; + int error; + + nfitaddr = nfitsubtbl; + dev = arg; + spa_type = nvdimm_spa_type_from_uuid( + (struct uuid *)nfitaddr->RangeGuid); + if (spa_type == SPA_TYPE_UNKNOWN) + return (0); + spa = malloc(sizeof(struct SPA_mapping), M_NVDIMM, M_WAITOK | M_ZERO); + error = nvdimm_spa_init(spa, nfitaddr, spa_type); + if (error != 0) { + nvdimm_spa_fini(spa); + free(spa, M_NVDIMM); + } + SLIST_INSERT_HEAD(&dev->spas, spa, link); + return (0); +} + static ACPI_STATUS nvdimm_root_create_dev(ACPI_HANDLE handle, UINT32 nesting_level, void *context, void **return_value) { ACPI_STATUS status; ACPI_DEVICE_INFO *device_info; device_t parent, child; uintptr_t *ivars; parent = context; child = BUS_ADD_CHILD(parent, 100, "nvdimm", -1); if (child == NULL) { device_printf(parent, "failed to create nvdimm\n"); return_ACPI_STATUS(AE_ERROR); } status = AcpiGetObjectInfo(handle, &device_info); if (ACPI_FAILURE(status)) { device_printf(parent, "failed to get nvdimm device info\n"); return_ACPI_STATUS(AE_ERROR); } ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX - 1, sizeof(uintptr_t), M_NVDIMM, M_ZERO | M_WAITOK); device_set_ivars(child, ivars); nvdimm_root_set_acpi_handle(child, handle); nvdimm_root_set_device_handle(child, device_info->Address); return_ACPI_STATUS(AE_OK); } static char *nvdimm_root_id[] = {"ACPI0012", NULL}; static int nvdimm_root_probe(device_t dev) { int rv; if (acpi_disabled("nvdimm")) return (ENXIO); rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL); if (rv <= 0) device_set_desc(dev, "ACPI NVDIMM root device"); return (rv); } static int nvdimm_root_attach(device_t dev) { ACPI_HANDLE handle; ACPI_STATUS status; + ACPI_TABLE_NFIT *nfitbl; int error; handle = acpi_get_handle(dev); status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1, nvdimm_root_create_dev, NULL, dev, NULL); if (ACPI_FAILURE(status)) device_printf(dev, "failed adding children\n"); error = bus_generic_attach(dev); + if (error != 0) + return (error); + status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl); + if (ACPI_FAILURE(status)) { + device_printf(dev, "cannot get NFIT\n"); + return (ENXIO); + } + error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS, + nvdimm_root_create_spa, device_get_softc(dev)); + AcpiPutTable(&nfitbl->Header); return (error); } static int nvdimm_root_detach(device_t dev) { + struct nvdimm_root_dev *root; + struct SPA_mapping *spa, *next; device_t *children; int i, error, num_children; + root = device_get_softc(dev); + SLIST_FOREACH_SAFE(spa, &root->spas, link, next) { + nvdimm_spa_fini(spa); + SLIST_REMOVE_HEAD(&root->spas, link); + free(spa, M_NVDIMM); + } error = bus_generic_detach(dev); if (error != 0) return (error); error = device_get_children(dev, &children, &num_children); if (error != 0) return (error); for (i = 0; i < num_children; i++) free(device_get_ivars(children[i]), M_NVDIMM); free(children, M_TEMP); error = device_delete_children(dev); return (error); } static int nvdimm_root_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) { if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX) return (ENOENT); *result = ((uintptr_t *)device_get_ivars(child))[index]; return (0); } static int nvdimm_root_write_ivar(device_t dev, device_t child, int index, uintptr_t value) { if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX) return (ENOENT); ((uintptr_t *)device_get_ivars(child))[index] = value; return (0); } static device_method_t nvdimm_methods[] = { DEVMETHOD(device_probe, nvdimm_probe), DEVMETHOD(device_attach, nvdimm_attach), DEVMETHOD(device_detach, nvdimm_detach), DEVMETHOD(device_suspend, nvdimm_suspend), DEVMETHOD(device_resume, nvdimm_resume), DEVMETHOD_END }; static driver_t nvdimm_driver = { "nvdimm", nvdimm_methods, sizeof(struct nvdimm_dev), }; static device_method_t nvdimm_root_methods[] = { DEVMETHOD(device_probe, nvdimm_root_probe), DEVMETHOD(device_attach, nvdimm_root_attach), DEVMETHOD(device_detach, nvdimm_root_detach), DEVMETHOD(bus_add_child, bus_generic_add_child), DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar), DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar), DEVMETHOD_END }; static driver_t nvdimm_root_driver = { "nvdimm_root", nvdimm_root_methods, + sizeof(struct nvdimm_root_dev), }; DRIVER_MODULE(nvdimm_root, acpi, nvdimm_root_driver, nvdimm_root_devclass, NULL, NULL); DRIVER_MODULE(nvdimm, nvdimm_root, nvdimm_driver, nvdimm_devclass, NULL, NULL); MODULE_DEPEND(nvdimm, acpi, 1, 1, 1); Index: head/sys/dev/nvdimm/nvdimm_spa.c =================================================================== --- head/sys/dev/nvdimm/nvdimm_spa.c (revision 343627) +++ head/sys/dev/nvdimm/nvdimm_spa.c (revision 343628) @@ -1,649 +1,548 @@ /*- * Copyright (c) 2017, 2018 The FreeBSD Foundation * All rights reserved. * Copyright (c) 2018, 2019 Intel Corporation * * This software was developed by Konstantin Belousov * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_acpi.h" #include "opt_ddb.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define UUID_INITIALIZER_VOLATILE_MEMORY \ {0x7305944f,0xfdda,0x44e3,0xb1,0x6c,{0x3f,0x22,0xd2,0x52,0xe5,0xd0}} #define UUID_INITIALIZER_PERSISTENT_MEMORY \ {0x66f0d379,0xb4f3,0x4074,0xac,0x43,{0x0d,0x33,0x18,0xb7,0x8c,0xdb}} #define UUID_INITIALIZER_CONTROL_REGION \ {0x92f701f6,0x13b4,0x405d,0x91,0x0b,{0x29,0x93,0x67,0xe8,0x23,0x4c}} #define UUID_INITIALIZER_DATA_REGION \ {0x91af0530,0x5d86,0x470e,0xa6,0xb0,{0x0a,0x2d,0xb9,0x40,0x82,0x49}} #define UUID_INITIALIZER_VOLATILE_VIRTUAL_DISK \ {0x77ab535a,0x45fc,0x624b,0x55,0x60,{0xf7,0xb2,0x81,0xd1,0xf9,0x6e}} #define UUID_INITIALIZER_VOLATILE_VIRTUAL_CD \ {0x3d5abd30,0x4175,0x87ce,0x6d,0x64,{0xd2,0xad,0xe5,0x23,0xc4,0xbb}} #define UUID_INITIALIZER_PERSISTENT_VIRTUAL_DISK \ {0x5cea02c9,0x4d07,0x69d3,0x26,0x9f,{0x44,0x96,0xfb,0xe0,0x96,0xf9}} #define UUID_INITIALIZER_PERSISTENT_VIRTUAL_CD \ {0x08018188,0x42cd,0xbb48,0x10,0x0f,{0x53,0x87,0xd5,0x3d,0xed,0x3d}} -struct SPA_mapping *spa_mappings; -int spa_mappings_cnt; - -static int -nvdimm_spa_count(void *nfitsubtbl __unused, void *arg) -{ - int *cnt; - - cnt = arg; - (*cnt)++; - return (0); -} - static struct nvdimm_SPA_uuid_list_elm { const char *u_name; struct uuid u_id; const bool u_usr_acc; } nvdimm_SPA_uuid_list[] = { [SPA_TYPE_VOLATILE_MEMORY] = { .u_name = "VOLA MEM ", .u_id = UUID_INITIALIZER_VOLATILE_MEMORY, .u_usr_acc = true, }, [SPA_TYPE_PERSISTENT_MEMORY] = { .u_name = "PERS MEM", .u_id = UUID_INITIALIZER_PERSISTENT_MEMORY, .u_usr_acc = true, }, [SPA_TYPE_CONTROL_REGION] = { .u_name = "CTRL RG ", .u_id = UUID_INITIALIZER_CONTROL_REGION, .u_usr_acc = false, }, [SPA_TYPE_DATA_REGION] = { .u_name = "DATA RG ", .u_id = UUID_INITIALIZER_DATA_REGION, .u_usr_acc = true, }, [SPA_TYPE_VOLATILE_VIRTUAL_DISK] = { .u_name = "VIRT DSK", .u_id = UUID_INITIALIZER_VOLATILE_VIRTUAL_DISK, .u_usr_acc = true, }, [SPA_TYPE_VOLATILE_VIRTUAL_CD] = { .u_name = "VIRT CD ", .u_id = UUID_INITIALIZER_VOLATILE_VIRTUAL_CD, .u_usr_acc = true, }, [SPA_TYPE_PERSISTENT_VIRTUAL_DISK] = { .u_name = "PV DSK ", .u_id = UUID_INITIALIZER_PERSISTENT_VIRTUAL_DISK, .u_usr_acc = true, }, [SPA_TYPE_PERSISTENT_VIRTUAL_CD] = { .u_name = "PV CD ", .u_id = UUID_INITIALIZER_PERSISTENT_VIRTUAL_CD, .u_usr_acc = true, }, }; enum SPA_mapping_type nvdimm_spa_type_from_uuid(struct uuid *uuid) { int j; for (j = 0; j < nitems(nvdimm_SPA_uuid_list); j++) { if (uuidcmp(uuid, &nvdimm_SPA_uuid_list[j].u_id) != 0) continue; return (j); } return (SPA_TYPE_UNKNOWN); } static vm_memattr_t nvdimm_spa_memattr(struct SPA_mapping *spa) { vm_memattr_t mode; if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WB) != 0) mode = VM_MEMATTR_WRITE_BACK; else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WT) != 0) mode = VM_MEMATTR_WRITE_THROUGH; else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WC) != 0) mode = VM_MEMATTR_WRITE_COMBINING; else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WP) != 0) mode = VM_MEMATTR_WRITE_PROTECTED; else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_UC) != 0) mode = VM_MEMATTR_UNCACHEABLE; else { if (bootverbose) printf("SPA%d mapping attr unsupported\n", spa->spa_nfit_idx); mode = VM_MEMATTR_UNCACHEABLE; } return (mode); } static int nvdimm_spa_uio(struct SPA_mapping *spa, struct uio *uio) { struct vm_page m, *ma; off_t off; vm_memattr_t mattr; int error, n; error = 0; if (spa->spa_kva == NULL) { mattr = nvdimm_spa_memattr(spa); vm_page_initfake(&m, 0, mattr); ma = &m; while (uio->uio_resid > 0) { if (uio->uio_offset >= spa->spa_len) break; off = spa->spa_phys_base + uio->uio_offset; vm_page_updatefake(&m, trunc_page(off), mattr); n = PAGE_SIZE; if (n > uio->uio_resid) n = uio->uio_resid; error = uiomove_fromphys(&ma, off & PAGE_MASK, n, uio); if (error != 0) break; } } else { while (uio->uio_resid > 0) { if (uio->uio_offset >= spa->spa_len) break; n = INT_MAX; if (n > uio->uio_resid) n = uio->uio_resid; if (uio->uio_offset + n > spa->spa_len) n = spa->spa_len - uio->uio_offset; error = uiomove((char *)spa->spa_kva + uio->uio_offset, n, uio); if (error != 0) break; } } return (error); } static int nvdimm_spa_rw(struct cdev *dev, struct uio *uio, int ioflag) { return (nvdimm_spa_uio(dev->si_drv1, uio)); } static int nvdimm_spa_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) { struct SPA_mapping *spa; int error; spa = dev->si_drv1; error = 0; switch (cmd) { case DIOCGSECTORSIZE: *(u_int *)data = DEV_BSIZE; break; case DIOCGMEDIASIZE: *(off_t *)data = spa->spa_len; break; default: error = ENOTTY; break; } return (error); } static int nvdimm_spa_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size, vm_object_t *objp, int nprot) { struct SPA_mapping *spa; spa = dev->si_drv1; if (spa->spa_obj == NULL) return (ENXIO); if (*offset >= spa->spa_len || *offset + size < *offset || *offset + size > spa->spa_len) return (EINVAL); vm_object_reference(spa->spa_obj); *objp = spa->spa_obj; return (0); } static struct cdevsw spa_cdevsw = { .d_version = D_VERSION, .d_flags = D_DISK, .d_name = "nvdimm_spa", .d_read = nvdimm_spa_rw, .d_write = nvdimm_spa_rw, .d_ioctl = nvdimm_spa_ioctl, .d_mmap_single = nvdimm_spa_mmap_single, }; static void nvdimm_spa_g_all_unmapped(struct SPA_mapping *spa, struct bio *bp, int rw) { struct vm_page maa[bp->bio_ma_n]; vm_page_t ma[bp->bio_ma_n]; vm_memattr_t mattr; int i; mattr = nvdimm_spa_memattr(spa); for (i = 0; i < nitems(ma); i++) { maa[i].flags = 0; vm_page_initfake(&maa[i], spa->spa_phys_base + trunc_page(bp->bio_offset) + PAGE_SIZE * i, mattr); ma[i] = &maa[i]; } if (rw == BIO_READ) pmap_copy_pages(ma, bp->bio_offset & PAGE_MASK, bp->bio_ma, bp->bio_ma_offset, bp->bio_length); else pmap_copy_pages(bp->bio_ma, bp->bio_ma_offset, ma, bp->bio_offset & PAGE_MASK, bp->bio_length); } static void nvdimm_spa_g_thread(void *arg) { struct SPA_mapping *spa; struct bio *bp; struct uio auio; struct iovec aiovec; int error; spa = arg; for (;;) { mtx_lock(&spa->spa_g_mtx); for (;;) { bp = bioq_takefirst(&spa->spa_g_queue); if (bp != NULL) break; msleep(&spa->spa_g_queue, &spa->spa_g_mtx, PRIBIO, "spa_g", 0); if (!spa->spa_g_proc_run) { spa->spa_g_proc_exiting = true; wakeup(&spa->spa_g_queue); mtx_unlock(&spa->spa_g_mtx); kproc_exit(0); } continue; } mtx_unlock(&spa->spa_g_mtx); if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE && bp->bio_cmd != BIO_FLUSH) { error = EOPNOTSUPP; goto completed; } error = 0; if (bp->bio_cmd == BIO_FLUSH) { if (spa->spa_kva != NULL) { pmap_large_map_wb(spa->spa_kva, spa->spa_len); } else { pmap_flush_cache_phys_range( (vm_paddr_t)spa->spa_phys_base, (vm_paddr_t)spa->spa_phys_base + spa->spa_len, nvdimm_spa_memattr(spa)); } /* * XXX flush IMC */ goto completed; } if ((bp->bio_flags & BIO_UNMAPPED) != 0) { if (spa->spa_kva != NULL) { aiovec.iov_base = (char *)spa->spa_kva + bp->bio_offset; aiovec.iov_len = bp->bio_length; auio.uio_iov = &aiovec; auio.uio_iovcnt = 1; auio.uio_resid = bp->bio_length; auio.uio_offset = bp->bio_offset; auio.uio_segflg = UIO_SYSSPACE; auio.uio_rw = bp->bio_cmd == BIO_READ ? UIO_WRITE : UIO_READ; auio.uio_td = curthread; error = uiomove_fromphys(bp->bio_ma, bp->bio_ma_offset, bp->bio_length, &auio); bp->bio_resid = auio.uio_resid; } else { nvdimm_spa_g_all_unmapped(spa, bp, bp->bio_cmd); bp->bio_resid = bp->bio_length; error = 0; } } else { aiovec.iov_base = bp->bio_data; aiovec.iov_len = bp->bio_length; auio.uio_iov = &aiovec; auio.uio_iovcnt = 1; auio.uio_resid = bp->bio_length; auio.uio_offset = bp->bio_offset; auio.uio_segflg = UIO_SYSSPACE; auio.uio_rw = bp->bio_cmd == BIO_READ ? UIO_READ : UIO_WRITE; auio.uio_td = curthread; error = nvdimm_spa_uio(spa, &auio); bp->bio_resid = auio.uio_resid; } bp->bio_bcount = bp->bio_length; devstat_end_transaction_bio(spa->spa_g_devstat, bp); completed: bp->bio_completed = bp->bio_length; g_io_deliver(bp, error); } } static void nvdimm_spa_g_start(struct bio *bp) { struct SPA_mapping *spa; spa = bp->bio_to->geom->softc; if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { mtx_lock(&spa->spa_g_stat_mtx); devstat_start_transaction_bio(spa->spa_g_devstat, bp); mtx_unlock(&spa->spa_g_stat_mtx); } mtx_lock(&spa->spa_g_mtx); bioq_disksort(&spa->spa_g_queue, bp); wakeup(&spa->spa_g_queue); mtx_unlock(&spa->spa_g_mtx); } static int nvdimm_spa_g_access(struct g_provider *pp, int r, int w, int e) { return (0); } -static g_init_t nvdimm_spa_g_init; -static g_fini_t nvdimm_spa_g_fini; - struct g_class nvdimm_spa_g_class = { .name = "SPA", .version = G_VERSION, .start = nvdimm_spa_g_start, .access = nvdimm_spa_g_access, - .init = nvdimm_spa_g_init, - .fini = nvdimm_spa_g_fini, }; DECLARE_GEOM_CLASS(nvdimm_spa_g_class, g_spa); -static int -nvdimm_spa_init_one(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr, - int spa_type) +int +nvdimm_spa_init(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr, + enum SPA_mapping_type spa_type) { struct make_dev_args mda; struct sglist *spa_sg; int error, error1; spa->spa_type = spa_type; spa->spa_domain = ((nfitaddr->Flags & ACPI_NFIT_PROXIMITY_VALID) != 0) ? nfitaddr->ProximityDomain : -1; spa->spa_nfit_idx = nfitaddr->RangeIndex; spa->spa_phys_base = nfitaddr->Address; spa->spa_len = nfitaddr->Length; spa->spa_efi_mem_flags = nfitaddr->MemoryMapping; if (bootverbose) { printf("NVDIMM SPA%d base %#016jx len %#016jx %s fl %#jx\n", spa->spa_nfit_idx, (uintmax_t)spa->spa_phys_base, (uintmax_t)spa->spa_len, nvdimm_SPA_uuid_list[spa_type].u_name, spa->spa_efi_mem_flags); } if (!nvdimm_SPA_uuid_list[spa_type].u_usr_acc) return (0); error1 = pmap_large_map(spa->spa_phys_base, spa->spa_len, &spa->spa_kva, nvdimm_spa_memattr(spa)); if (error1 != 0) { printf("NVDIMM SPA%d cannot map into KVA, error %d\n", spa->spa_nfit_idx, error1); spa->spa_kva = NULL; } spa_sg = sglist_alloc(1, M_WAITOK); error = sglist_append_phys(spa_sg, spa->spa_phys_base, spa->spa_len); if (error == 0) { spa->spa_obj = vm_pager_allocate(OBJT_SG, spa_sg, spa->spa_len, VM_PROT_ALL, 0, NULL); if (spa->spa_obj == NULL) { printf("NVDIMM SPA%d failed to alloc vm object", spa->spa_nfit_idx); sglist_free(spa_sg); } } else { printf("NVDIMM SPA%d failed to init sglist, error %d", spa->spa_nfit_idx, error); sglist_free(spa_sg); } make_dev_args_init(&mda); mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; mda.mda_devsw = &spa_cdevsw; mda.mda_cr = NULL; mda.mda_uid = UID_ROOT; mda.mda_gid = GID_OPERATOR; mda.mda_mode = 0660; mda.mda_si_drv1 = spa; error = make_dev_s(&mda, &spa->spa_dev, "nvdimm_spa%d", spa->spa_nfit_idx); if (error != 0) { printf("NVDIMM SPA%d cannot create devfs node, error %d\n", spa->spa_nfit_idx, error); if (error1 == 0) error1 = error; } bioq_init(&spa->spa_g_queue); mtx_init(&spa->spa_g_mtx, "spag", NULL, MTX_DEF); mtx_init(&spa->spa_g_stat_mtx, "spagst", NULL, MTX_DEF); spa->spa_g_proc_run = true; spa->spa_g_proc_exiting = false; error = kproc_create(nvdimm_spa_g_thread, spa, &spa->spa_g_proc, 0, 0, "g_spa%d", spa->spa_nfit_idx); if (error != 0) { printf("NVDIMM SPA%d cannot create geom worker, error %d\n", spa->spa_nfit_idx, error); if (error1 == 0) error1 = error; } else { - g_topology_assert(); + g_topology_lock(); spa->spa_g = g_new_geomf(&nvdimm_spa_g_class, "spa%d", spa->spa_nfit_idx); spa->spa_g->softc = spa; spa->spa_p = g_new_providerf(spa->spa_g, "spa%d", spa->spa_nfit_idx); spa->spa_p->mediasize = spa->spa_len; spa->spa_p->sectorsize = DEV_BSIZE; spa->spa_p->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE | G_PF_ACCEPT_UNMAPPED; g_error_provider(spa->spa_p, 0); spa->spa_g_devstat = devstat_new_entry("spa", spa->spa_nfit_idx, DEV_BSIZE, DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); + g_topology_unlock(); } return (error1); } -static void -nvdimm_spa_fini_one(struct SPA_mapping *spa) +void +nvdimm_spa_fini(struct SPA_mapping *spa) { mtx_lock(&spa->spa_g_mtx); spa->spa_g_proc_run = false; wakeup(&spa->spa_g_queue); while (!spa->spa_g_proc_exiting) msleep(&spa->spa_g_queue, &spa->spa_g_mtx, PRIBIO, "spa_e", 0); mtx_unlock(&spa->spa_g_mtx); if (spa->spa_g != NULL) { g_topology_lock(); g_wither_geom(spa->spa_g, ENXIO); g_topology_unlock(); spa->spa_g = NULL; spa->spa_p = NULL; } if (spa->spa_g_devstat != NULL) { devstat_remove_entry(spa->spa_g_devstat); spa->spa_g_devstat = NULL; } if (spa->spa_dev != NULL) { destroy_dev(spa->spa_dev); spa->spa_dev = NULL; } vm_object_deallocate(spa->spa_obj); if (spa->spa_kva != NULL) { pmap_large_unmap(spa->spa_kva, spa->spa_len); spa->spa_kva = NULL; } mtx_destroy(&spa->spa_g_mtx); mtx_destroy(&spa->spa_g_stat_mtx); -} - -static int -nvdimm_spa_parse(void *nfitsubtbl, void *arg) -{ - ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr; - struct SPA_mapping *spa; - enum SPA_mapping_type spa_type; - int error, *i; - - i = arg; - spa = &spa_mappings[(*i)++]; - nfitaddr = nfitsubtbl; - spa_type = nvdimm_spa_type_from_uuid( - (struct uuid *)&nfitaddr->RangeGuid); - if (spa_type == SPA_TYPE_UNKNOWN) { - printf("Unknown SPA UUID %d ", nfitaddr->RangeIndex); - printf_uuid((struct uuid *)&nfitaddr->RangeGuid); - printf("\n"); - return (0); - } - error = nvdimm_spa_init_one(spa, nfitaddr, spa_type); - if (error != 0) - nvdimm_spa_fini_one(spa); - return (0); -} - -static int -nvdimm_spa_init1(ACPI_TABLE_NFIT *nfitbl) -{ - int error, i; - - error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS, - nvdimm_spa_count, &spa_mappings_cnt); - if (error != 0) - return (error); - spa_mappings = malloc(sizeof(struct SPA_mapping) * spa_mappings_cnt, - M_NVDIMM, M_WAITOK | M_ZERO); - i = 0; - error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS, - nvdimm_spa_parse, &i); - if (error != 0) { - free(spa_mappings, M_NVDIMM); - spa_mappings = NULL; - return (error); - } - return (0); -} - -static void -nvdimm_spa_g_init(struct g_class *mp __unused) -{ - ACPI_TABLE_NFIT *nfitbl; - ACPI_STATUS status; - int error; - - spa_mappings_cnt = 0; - spa_mappings = NULL; - if (acpi_disabled("nvdimm")) - return; - status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl); - if (ACPI_FAILURE(status)) { - if (bootverbose) - printf("nvdimm_spa_g_init: cannot find NFIT\n"); - return; - } - error = nvdimm_spa_init1(nfitbl); - if (error != 0) - printf("nvdimm_spa_g_init: error %d\n", error); - AcpiPutTable(&nfitbl->Header); -} - -static void -nvdimm_spa_g_fini(struct g_class *mp __unused) -{ - int i; - - if (spa_mappings == NULL) - return; - for (i = 0; i < spa_mappings_cnt; i++) - nvdimm_spa_fini_one(&spa_mappings[i]); - free(spa_mappings, M_NVDIMM); - spa_mappings = NULL; - spa_mappings_cnt = 0; } Index: head/sys/dev/nvdimm/nvdimm_var.h =================================================================== --- head/sys/dev/nvdimm/nvdimm_var.h (revision 343627) +++ head/sys/dev/nvdimm/nvdimm_var.h (revision 343628) @@ -1,97 +1,102 @@ /*- * Copyright (c) 2017 The FreeBSD Foundation * All rights reserved. * Copyright (c) 2018, 2019 Intel Corporation * * This software was developed by Konstantin Belousov * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef __DEV_NVDIMM_VAR_H__ #define __DEV_NVDIMM_VAR_H__ typedef uint32_t nfit_handle_t; enum nvdimm_root_ivar { NVDIMM_ROOT_IVAR_ACPI_HANDLE, NVDIMM_ROOT_IVAR_DEVICE_HANDLE, NVDIMM_ROOT_IVAR_MAX, }; __BUS_ACCESSOR(nvdimm_root, acpi_handle, NVDIMM_ROOT, ACPI_HANDLE, ACPI_HANDLE) __BUS_ACCESSOR(nvdimm_root, device_handle, NVDIMM_ROOT, DEVICE_HANDLE, nfit_handle_t) +struct nvdimm_root_dev { + SLIST_HEAD(, SPA_mapping) spas; +}; + struct nvdimm_dev { device_t nv_dev; nfit_handle_t nv_handle; uint64_t **nv_flush_addr; int nv_flush_addr_cnt; }; enum SPA_mapping_type { SPA_TYPE_VOLATILE_MEMORY = 0, SPA_TYPE_PERSISTENT_MEMORY = 1, SPA_TYPE_CONTROL_REGION = 2, SPA_TYPE_DATA_REGION = 3, SPA_TYPE_VOLATILE_VIRTUAL_DISK = 4, SPA_TYPE_VOLATILE_VIRTUAL_CD = 5, SPA_TYPE_PERSISTENT_VIRTUAL_DISK= 6, SPA_TYPE_PERSISTENT_VIRTUAL_CD = 7, SPA_TYPE_UNKNOWN = 127, }; struct SPA_mapping { + SLIST_ENTRY(SPA_mapping) link; enum SPA_mapping_type spa_type; int spa_domain; int spa_nfit_idx; uint64_t spa_phys_base; uint64_t spa_len; uint64_t spa_efi_mem_flags; void *spa_kva; struct cdev *spa_dev; struct g_geom *spa_g; struct g_provider *spa_p; struct bio_queue_head spa_g_queue; struct mtx spa_g_mtx; struct mtx spa_g_stat_mtx; struct devstat *spa_g_devstat; struct proc *spa_g_proc; struct vm_object *spa_obj; bool spa_g_proc_run; bool spa_g_proc_exiting; }; -extern struct SPA_mapping *spa_mappings; -extern int spa_mappings_cnt; - MALLOC_DECLARE(M_NVDIMM); enum SPA_mapping_type nvdimm_spa_type_from_uuid(struct uuid *); struct nvdimm_dev *nvdimm_find_by_handle(nfit_handle_t nv_handle); int nvdimm_iterate_nfit(ACPI_TABLE_NFIT *nfitbl, enum AcpiNfitType type, int (*cb)(void *, void *), void *arg); +int nvdimm_spa_init(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr, + enum SPA_mapping_type spa_type); +void nvdimm_spa_fini(struct SPA_mapping *spa); #endif /* __DEV_NVDIMM_VAR_H__ */