Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/gasket/gasket_page_table.c
- This file was added.
| /* | |||||
| * Copyright (c) 2026 Abdelkader Boudih <freebsd@seuros.com> | |||||
| * | |||||
| * SPDX-License-Identifier: BSD-2-Clause | |||||
| */ | |||||
| #include <sys/systm.h> | |||||
| #include <sys/kernel.h> | |||||
| #include <sys/lock.h> | |||||
| #include <sys/malloc.h> | |||||
| #include <sys/memdesc.h> | |||||
| #include <sys/sx.h> | |||||
| #include <sys/sysctl.h> | |||||
| #include <vm/vm.h> | |||||
| #include <vm/pmap.h> | |||||
| #include <vm/vm_map.h> | |||||
| #include <vm/vm_page.h> | |||||
| #include <dev/gasket/gasket.h> | |||||
| #include <dev/gasket/gasket_page_table.h> | |||||
| #define GASKET_PAGE_SHIFT 12 | |||||
| #define GASKET_EXTENDED_LVL0_SHIFT 21 | |||||
| #define GASKET_EXTENDED_LVL0_WIDTH 13 | |||||
| #define GASKET_SUBTABLE_ENTRIES 512 | |||||
| #define GASKET_PTE_VALID 0x1 | |||||
| CTASSERT(PAGE_SIZE == (1 << GASKET_PAGE_SHIFT)); | |||||
| CTASSERT(GASKET_SUBTABLE_ENTRIES * sizeof(uint64_t) == PAGE_SIZE); | |||||
| static void gasket_garbage_collect_locked(struct gasket_page_table *pt); | |||||
| static MALLOC_DEFINE(M_GASKET_PT, "gasket_pt", "Gasket device page tables"); | |||||
| static SYSCTL_NODE(_hw, OID_AUTO, gasket, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, | |||||
| "Gasket accelerator framework"); | |||||
| static u_int gasket_max_wired_pages = 131072; | |||||
| SYSCTL_UINT(_hw_gasket, OID_AUTO, max_wired_pages, CTLFLAG_RWTUN, | |||||
| &gasket_max_wired_pages, 0, | |||||
| "Maximum pages one device page table may have mapped"); | |||||
| struct gasket_mapping { | |||||
| vm_page_t gm_page; | |||||
| bus_dmamap_t gm_dma_map; | |||||
| bus_addr_t gm_dma_addr; | |||||
| int gm_prot; | |||||
| enum gasket_map_kind gm_kind; | |||||
| bool gm_mapped; | |||||
| }; | |||||
| struct gasket_subtable { | |||||
| uint64_t *gst_slots; | |||||
| bus_dmamap_t gst_dma_map; | |||||
| bus_addr_t gst_dma_addr; | |||||
| struct gasket_mapping gst_entries[GASKET_SUBTABLE_ENTRIES]; | |||||
| }; | |||||
| struct gasket_l0_entry { | |||||
| struct gasket_mapping gle_simple; | |||||
| struct gasket_subtable *gle_subtable; | |||||
| }; | |||||
| struct gasket_page_table { | |||||
| device_t gpt_dev; | |||||
| struct resource *gpt_csr; | |||||
| struct gasket_page_table_config gpt_cfg; | |||||
| struct gasket_l0_entry *gpt_entries; | |||||
| bus_dma_tag_t gpt_dma_tag; | |||||
| struct sx gpt_lock; | |||||
| uint64_t gpt_extended_flag; | |||||
| u_int gpt_num_simple; | |||||
| u_int gpt_num_extended; | |||||
| u_int gpt_num_active; | |||||
| const vm_paddr_t *gpt_coherent_pages; | |||||
| u_int gpt_coherent_npages; | |||||
| bus_addr_t gpt_coherent_bus; | |||||
| }; | |||||
| struct gasket_dma_load { | |||||
| bus_addr_t gdl_addr; | |||||
| int gdl_error; | |||||
| }; | |||||
| static void | |||||
| gasket_dma_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) | |||||
| { | |||||
| struct gasket_dma_load *load; | |||||
| load = arg; | |||||
| if (error != 0) { | |||||
| load->gdl_error = error; | |||||
| return; | |||||
| } | |||||
| if (nsegs != 1) { | |||||
| load->gdl_error = EFBIG; | |||||
| return; | |||||
| } | |||||
| load->gdl_addr = segs[0].ds_addr; | |||||
| load->gdl_error = 0; | |||||
| } | |||||
| static int | |||||
| gasket_dma_load_page(struct gasket_page_table *pt, bus_dmamap_t map, | |||||
| vm_page_t page, bus_addr_t *addrp) | |||||
| { | |||||
| struct gasket_dma_load load; | |||||
| struct memdesc mem; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| load.gdl_addr = 0; | |||||
| load.gdl_error = ENOMEM; | |||||
| /* memdesc_vmpages() would keep a pointer into this stack frame. */ | |||||
| mem = memdesc_paddr(VM_PAGE_TO_PHYS(page), PAGE_SIZE); | |||||
| error = bus_dmamap_load_mem(pt->gpt_dma_tag, map, &mem, | |||||
| gasket_dma_load_cb, &load, BUS_DMA_NOWAIT); | |||||
| if (error != 0) | |||||
| return (ENOMEM); | |||||
| if (load.gdl_error != 0) { | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, map); | |||||
| return (ENOMEM); | |||||
| } | |||||
| if ((load.gdl_addr & PAGE_MASK) != 0) { | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, map); | |||||
| return (EFAULT); | |||||
| } | |||||
| *addrp = load.gdl_addr; | |||||
| return (0); | |||||
| } | |||||
| /* A page the host writes is one the device reads, and the other way round. */ | |||||
| static bus_dmasync_op_t | |||||
| gasket_dma_sync_op(int prot, bus_dmasync_op_t write, bus_dmasync_op_t read) | |||||
| { | |||||
| bus_dmasync_op_t op; | |||||
| op = 0; | |||||
| if ((prot & VM_PROT_READ) != 0) | |||||
| op |= write; | |||||
| if ((prot & VM_PROT_WRITE) != 0) | |||||
| op |= read; | |||||
| return (op); | |||||
| } | |||||
| #define gasket_dma_pre_sync(prot) \ | |||||
| gasket_dma_sync_op((prot), BUS_DMASYNC_PREWRITE, BUS_DMASYNC_PREREAD) | |||||
| #define gasket_dma_post_sync(prot) \ | |||||
| gasket_dma_sync_op((prot), BUS_DMASYNC_POSTWRITE, BUS_DMASYNC_POSTREAD) | |||||
| static int | |||||
| gasket_mapping_load_locked(struct gasket_page_table *pt, | |||||
| struct gasket_mapping *mapping, vm_page_t page, bus_addr_t bus, int prot, | |||||
| enum gasket_map_kind kind) | |||||
| { | |||||
| bus_dmasync_op_t op; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| KASSERT(!mapping->gm_mapped, ("gasket: mapping already in use")); | |||||
| KASSERT(kind != GASKET_MAP_NONE, ("gasket: mapping with no kind")); | |||||
| if (kind == GASKET_MAP_COHERENT) { | |||||
| mapping->gm_page = NULL; | |||||
| mapping->gm_dma_addr = bus; | |||||
| mapping->gm_prot = prot; | |||||
| mapping->gm_kind = kind; | |||||
| return (0); | |||||
| } | |||||
| error = bus_dmamap_create(pt->gpt_dma_tag, 0, | |||||
| &mapping->gm_dma_map); | |||||
| if (error != 0) | |||||
| return (ENOMEM); | |||||
| error = gasket_dma_load_page(pt, mapping->gm_dma_map, page, | |||||
| &mapping->gm_dma_addr); | |||||
| if (error != 0) { | |||||
| bus_dmamap_destroy(pt->gpt_dma_tag, mapping->gm_dma_map); | |||||
| memset(mapping, 0, sizeof(*mapping)); | |||||
| return (error); | |||||
| } | |||||
| op = gasket_dma_pre_sync(prot); | |||||
| if (op != 0) | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, mapping->gm_dma_map, op); | |||||
| mapping->gm_page = page; | |||||
| mapping->gm_prot = prot; | |||||
| mapping->gm_kind = kind; | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| gasket_mapping_discard_locked(struct gasket_page_table *pt, | |||||
| struct gasket_mapping *mapping) | |||||
| { | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| KASSERT(!mapping->gm_mapped, | |||||
| ("gasket: discarding a published mapping")); | |||||
| if (mapping->gm_kind == GASKET_MAP_HOST) { | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, mapping->gm_dma_map); | |||||
| bus_dmamap_destroy(pt->gpt_dma_tag, mapping->gm_dma_map); | |||||
| } | |||||
| memset(mapping, 0, sizeof(*mapping)); | |||||
| } | |||||
| static void | |||||
| gasket_mapping_unload_locked(struct gasket_page_table *pt, | |||||
| struct gasket_mapping *mapping) | |||||
| { | |||||
| vm_page_t page; | |||||
| bus_dmasync_op_t op; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| if (!mapping->gm_mapped) | |||||
| return; | |||||
| if (mapping->gm_kind == GASKET_MAP_COHERENT) { | |||||
| KASSERT(pt->gpt_num_active != 0, | |||||
| ("gasket: active page count underflow")); | |||||
| pt->gpt_num_active--; | |||||
| memset(mapping, 0, sizeof(*mapping)); | |||||
| return; | |||||
| } | |||||
| op = gasket_dma_post_sync(mapping->gm_prot); | |||||
| if (op != 0) | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, mapping->gm_dma_map, op); | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, mapping->gm_dma_map); | |||||
| bus_dmamap_destroy(pt->gpt_dma_tag, mapping->gm_dma_map); | |||||
| /* Dirtied at acquisition; busying it here trips an assertion. */ | |||||
| page = mapping->gm_page; | |||||
| vm_page_unhold_pages(&page, 1); | |||||
| KASSERT(pt->gpt_num_active != 0, | |||||
| ("gasket: active page count underflow")); | |||||
| pt->gpt_num_active--; | |||||
| memset(mapping, 0, sizeof(*mapping)); | |||||
| } | |||||
| static bus_size_t | |||||
| gasket_l0_slot_offset(struct gasket_page_table *pt, u_int slot) | |||||
| { | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| return (pt->gpt_cfg.gpt_base_reg + (bus_size_t)slot * sizeof(uint64_t)); | |||||
| } | |||||
| static bool | |||||
| gasket_mapping_range_free_locked(struct gasket_page_table *pt, | |||||
| struct gasket_mapping *entries, u_int nentries) | |||||
| { | |||||
| u_int i; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| for (i = 0; i < nentries; i++) { | |||||
| if (entries[i].gm_mapped) | |||||
| return (false); | |||||
| } | |||||
| return (true); | |||||
| } | |||||
| static bool | |||||
| gasket_simple_range_free_locked(struct gasket_page_table *pt, u_int first, | |||||
| u_int nentries) | |||||
| { | |||||
| u_int i; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| for (i = 0; i < nentries; i++) { | |||||
| if (pt->gpt_entries[first + i].gle_simple.gm_mapped) | |||||
| return (false); | |||||
| } | |||||
| return (true); | |||||
| } | |||||
| static int | |||||
| gasket_alloc_subtable_locked(struct gasket_page_table *pt, u_int lvl0) | |||||
| { | |||||
| struct gasket_dma_load load; | |||||
| struct gasket_l0_entry *l0e; | |||||
| struct gasket_subtable *subtable; | |||||
| bus_size_t slot; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| KASSERT(lvl0 < pt->gpt_num_extended, | |||||
| ("gasket: extended level 0 index out of range")); | |||||
| l0e = &pt->gpt_entries[pt->gpt_num_simple + lvl0]; | |||||
| if (l0e->gle_subtable != NULL) | |||||
| return (0); | |||||
| subtable = malloc(sizeof(*subtable), M_GASKET_PT, M_WAITOK | M_ZERO); | |||||
| error = bus_dmamem_alloc(pt->gpt_dma_tag, | |||||
| (void **)&subtable->gst_slots, | |||||
| BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, | |||||
| &subtable->gst_dma_map); | |||||
| if (error != 0) { | |||||
| free(subtable, M_GASKET_PT); | |||||
| return (ENOMEM); | |||||
| } | |||||
| load.gdl_addr = 0; | |||||
| load.gdl_error = ENOMEM; | |||||
| error = bus_dmamap_load(pt->gpt_dma_tag, subtable->gst_dma_map, | |||||
| subtable->gst_slots, PAGE_SIZE, gasket_dma_load_cb, &load, | |||||
| BUS_DMA_NOWAIT); | |||||
| if (error != 0 || load.gdl_error != 0 || | |||||
| (load.gdl_addr & PAGE_MASK) != 0) { | |||||
| if (error == 0) | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, | |||||
| subtable->gst_dma_map); | |||||
| bus_dmamem_free(pt->gpt_dma_tag, subtable->gst_slots, | |||||
| subtable->gst_dma_map); | |||||
| free(subtable, M_GASKET_PT); | |||||
| return (ENOMEM); | |||||
| } | |||||
| subtable->gst_dma_addr = load.gdl_addr; | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, subtable->gst_dma_map, | |||||
| BUS_DMASYNC_PREWRITE); | |||||
| slot = gasket_l0_slot_offset(pt, pt->gpt_num_simple + lvl0); | |||||
| gasket_wr8(pt->gpt_csr, slot, | |||||
| subtable->gst_dma_addr | GASKET_PTE_VALID); | |||||
| l0e->gle_subtable = subtable; | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| gasket_free_subtable_locked(struct gasket_page_table *pt, u_int lvl0) | |||||
| { | |||||
| struct gasket_l0_entry *l0e; | |||||
| struct gasket_subtable *subtable; | |||||
| bus_size_t slot; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| KASSERT(lvl0 < pt->gpt_num_extended, | |||||
| ("gasket: extended level 0 index out of range")); | |||||
| l0e = &pt->gpt_entries[pt->gpt_num_simple + lvl0]; | |||||
| subtable = l0e->gle_subtable; | |||||
| if (subtable == NULL) | |||||
| return; | |||||
| KASSERT(gasket_mapping_range_free_locked(pt, subtable->gst_entries, | |||||
| GASKET_SUBTABLE_ENTRIES), | |||||
| ("gasket: freeing active subtable")); | |||||
| /* Posted write: read back before the subtable page is freed. */ | |||||
| slot = gasket_l0_slot_offset(pt, pt->gpt_num_simple + lvl0); | |||||
| gasket_wr8(pt->gpt_csr, slot, 0); | |||||
| (void)gasket_rd8(pt->gpt_csr, slot); | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, subtable->gst_dma_map, | |||||
| BUS_DMASYNC_POSTWRITE); | |||||
| bus_dmamap_unload(pt->gpt_dma_tag, subtable->gst_dma_map); | |||||
| bus_dmamem_free(pt->gpt_dma_tag, subtable->gst_slots, | |||||
| subtable->gst_dma_map); | |||||
| free(subtable, M_GASKET_PT); | |||||
| l0e->gle_subtable = NULL; | |||||
| } | |||||
| static int | |||||
| gasket_validate_dev_range_locked(struct gasket_page_table *pt, | |||||
| uint64_t dev_addr, u_int npages, bool *extendedp, u_int *indexp) | |||||
| { | |||||
| uint64_t global, limit, raw; | |||||
| u_int lvl0; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| if ((dev_addr & PAGE_MASK) != 0) | |||||
| return (EINVAL); | |||||
| if ((dev_addr & pt->gpt_extended_flag) == 0) { | |||||
| global = dev_addr >> GASKET_PAGE_SHIFT; | |||||
| if (global >= pt->gpt_num_simple || | |||||
| npages > pt->gpt_num_simple - global) | |||||
| return (EINVAL); | |||||
| *extendedp = false; | |||||
| *indexp = (u_int)global; | |||||
| return (0); | |||||
| } | |||||
| raw = dev_addr & ~pt->gpt_extended_flag; | |||||
| global = raw >> GASKET_PAGE_SHIFT; | |||||
| limit = (uint64_t)pt->gpt_num_extended * | |||||
| GASKET_SUBTABLE_ENTRIES; | |||||
| lvl0 = (u_int)(global / GASKET_SUBTABLE_ENTRIES); | |||||
| if ((raw & PAGE_MASK) != 0 || | |||||
| raw >> (GASKET_EXTENDED_LVL0_WIDTH + | |||||
| GASKET_EXTENDED_LVL0_SHIFT) != 0 || | |||||
| lvl0 >= pt->gpt_num_extended || global >= limit || | |||||
| npages > limit - global) | |||||
| return (EINVAL); | |||||
| *extendedp = true; | |||||
| *indexp = (u_int)global; | |||||
| return (0); | |||||
| } | |||||
| static int | |||||
| gasket_prepare_extended_locked(struct gasket_page_table *pt, u_int first, | |||||
| u_int npages) | |||||
| { | |||||
| struct gasket_subtable *subtable; | |||||
| u_int len, lvl0, lvl1, remain; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| remain = npages; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| if (subtable != NULL && | |||||
| !gasket_mapping_range_free_locked(pt, | |||||
| &subtable->gst_entries[lvl1], len)) | |||||
| return (EBUSY); | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| remain = npages; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| error = gasket_alloc_subtable_locked(pt, lvl0); | |||||
| if (error != 0) | |||||
| return (error); | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| return (0); | |||||
| } | |||||
| static int | |||||
| gasket_map_simple_locked(struct gasket_page_table *pt, u_int first, | |||||
| vm_page_t *pages, bus_addr_t *bus, u_int npages, int prot, | |||||
| enum gasket_map_kind kind, u_int *nmappedp) | |||||
| { | |||||
| struct gasket_mapping *mapping; | |||||
| uint64_t pte; | |||||
| u_int i; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| for (i = 0; i < npages; i++) { | |||||
| mapping = &pt->gpt_entries[first + i].gle_simple; | |||||
| error = gasket_mapping_load_locked(pt, mapping, | |||||
| pages != NULL ? pages[i] : NULL, bus[i], prot, kind); | |||||
| if (error != 0) { | |||||
| while (i-- != 0) | |||||
| gasket_mapping_discard_locked(pt, | |||||
| &pt->gpt_entries[first + i].gle_simple); | |||||
| *nmappedp = 0; | |||||
| return (error); | |||||
| } | |||||
| } | |||||
| for (i = 0; i < npages; i++) { | |||||
| mapping = &pt->gpt_entries[first + i].gle_simple; | |||||
| pte = mapping->gm_dma_addr | GASKET_PTE_VALID; | |||||
| gasket_wr8(pt->gpt_csr, | |||||
| gasket_l0_slot_offset(pt, first + i), pte); | |||||
| mapping->gm_mapped = true; | |||||
| pt->gpt_num_active++; | |||||
| if (pages != NULL) | |||||
| pages[i] = NULL; | |||||
| } | |||||
| *nmappedp = npages; | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| gasket_discard_extended_locked(struct gasket_page_table *pt, u_int first, | |||||
| u_int nloaded) | |||||
| { | |||||
| struct gasket_subtable *subtable; | |||||
| u_int i, len, lvl0, lvl1, remain; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| remain = nloaded; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| KASSERT(subtable != NULL, ("gasket: missing subtable")); | |||||
| for (i = 0; i < len; i++) | |||||
| gasket_mapping_discard_locked(pt, | |||||
| &subtable->gst_entries[lvl1 + i]); | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| } | |||||
| static int | |||||
| gasket_map_extended_locked(struct gasket_page_table *pt, u_int first, | |||||
| vm_page_t *pages, bus_addr_t *bus, u_int npages, int prot, | |||||
| enum gasket_map_kind kind, u_int *nmappedp) | |||||
| { | |||||
| struct gasket_mapping *mapping; | |||||
| struct gasket_subtable *subtable; | |||||
| uint64_t pte; | |||||
| u_int done, i, len, lvl0, lvl1, remain; | |||||
| int error; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| done = 0; | |||||
| remain = npages; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| KASSERT(subtable != NULL, ("gasket: missing subtable")); | |||||
| for (i = 0; i < len; i++) { | |||||
| mapping = &subtable->gst_entries[lvl1 + i]; | |||||
| error = gasket_mapping_load_locked(pt, mapping, | |||||
| pages != NULL ? pages[done] : NULL, bus[done], prot, | |||||
| kind); | |||||
| if (error != 0) { | |||||
| gasket_discard_extended_locked(pt, first, done); | |||||
| *nmappedp = 0; | |||||
| return (error); | |||||
| } | |||||
| done++; | |||||
| } | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| done = 0; | |||||
| remain = npages; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| for (i = 0; i < len; i++) { | |||||
| mapping = &subtable->gst_entries[lvl1 + i]; | |||||
| pte = mapping->gm_dma_addr | GASKET_PTE_VALID; | |||||
| subtable->gst_slots[lvl1 + i] = pte; | |||||
| mapping->gm_mapped = true; | |||||
| pt->gpt_num_active++; | |||||
| if (pages != NULL) | |||||
| pages[done] = NULL; | |||||
| done++; | |||||
| } | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, subtable->gst_dma_map, | |||||
| BUS_DMASYNC_PREWRITE); | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| *nmappedp = npages; | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| gasket_unmap_simple_locked(struct gasket_page_table *pt, u_int first, | |||||
| u_int npages) | |||||
| { | |||||
| struct gasket_mapping *mapping; | |||||
| u_int i; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| /* MMIO writes are posted: flush before releasing the pages. */ | |||||
| for (i = 0; i < npages; i++) { | |||||
| gasket_wr8(pt->gpt_csr, | |||||
| gasket_l0_slot_offset(pt, first + i), 0); | |||||
| } | |||||
| if (npages != 0) | |||||
| (void)gasket_rd8(pt->gpt_csr, gasket_l0_slot_offset(pt, first)); | |||||
| for (i = 0; i < npages; i++) { | |||||
| mapping = &pt->gpt_entries[first + i].gle_simple; | |||||
| gasket_mapping_unload_locked(pt, mapping); | |||||
| } | |||||
| } | |||||
| static void | |||||
| gasket_unmap_extended_locked(struct gasket_page_table *pt, u_int first, | |||||
| u_int npages) | |||||
| { | |||||
| struct gasket_subtable *subtable; | |||||
| u_int i, len, lvl0, lvl1, remain; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| remain = npages; | |||||
| lvl0 = first / GASKET_SUBTABLE_ENTRIES; | |||||
| lvl1 = first % GASKET_SUBTABLE_ENTRIES; | |||||
| while (remain != 0) { | |||||
| len = MIN(remain, GASKET_SUBTABLE_ENTRIES - lvl1); | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| if (subtable != NULL) { | |||||
| for (i = 0; i < len; i++) | |||||
| subtable->gst_slots[lvl1 + i] = 0; | |||||
| bus_dmamap_sync(pt->gpt_dma_tag, | |||||
| subtable->gst_dma_map, BUS_DMASYNC_PREWRITE); | |||||
| (void)gasket_rd8(pt->gpt_csr, gasket_l0_slot_offset(pt, | |||||
| pt->gpt_num_simple + lvl0)); | |||||
| for (i = 0; i < len; i++) | |||||
| gasket_mapping_unload_locked(pt, | |||||
| &subtable->gst_entries[lvl1 + i]); | |||||
| } | |||||
| remain -= len; | |||||
| lvl0++; | |||||
| lvl1 = 0; | |||||
| } | |||||
| } | |||||
| static void | |||||
| gasket_unmap_all_locked(struct gasket_page_table *pt) | |||||
| { | |||||
| u_int i; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| gasket_unmap_simple_locked(pt, 0, pt->gpt_num_simple); | |||||
| for (i = 0; i < pt->gpt_num_extended; i++) { | |||||
| if (pt->gpt_entries[pt->gpt_num_simple + | |||||
| i].gle_subtable != NULL) | |||||
| gasket_unmap_extended_locked(pt, | |||||
| i * GASKET_SUBTABLE_ENTRIES, | |||||
| GASKET_SUBTABLE_ENTRIES); | |||||
| } | |||||
| } | |||||
| int | |||||
| gasket_page_table_init(device_t dev, struct resource *csr, | |||||
| const struct gasket_page_table_config *cfg, | |||||
| struct gasket_page_table **ptp) | |||||
| { | |||||
| struct gasket_page_table *pt; | |||||
| bus_size_t csr_size, table_size; | |||||
| u_int i; | |||||
| int error; | |||||
| if (ptp == NULL) | |||||
| return (EINVAL); | |||||
| *ptp = NULL; | |||||
| if (dev == NULL || csr == NULL || cfg == NULL || | |||||
| cfg->gpt_total_entries == 0 || | |||||
| !powerof2(cfg->gpt_total_entries) || | |||||
| cfg->gpt_total_entries > (1U << GASKET_EXTENDED_LVL0_WIDTH) || | |||||
| cfg->gpt_extended_bit < GASKET_EXTENDED_LVL0_WIDTH + | |||||
| GASKET_EXTENDED_LVL0_SHIFT || | |||||
| cfg->gpt_extended_bit >= 64) | |||||
| return (EINVAL); | |||||
| csr_size = rman_get_size(csr); | |||||
| table_size = (bus_size_t)cfg->gpt_total_entries * sizeof(uint64_t); | |||||
| if (cfg->gpt_base_reg > csr_size || | |||||
| table_size > csr_size - cfg->gpt_base_reg || | |||||
| cfg->gpt_extended_reg > csr_size || | |||||
| sizeof(uint64_t) > csr_size - cfg->gpt_extended_reg) | |||||
| return (EINVAL); | |||||
| pt = malloc(sizeof(*pt), M_GASKET_PT, M_WAITOK | M_ZERO); | |||||
| pt->gpt_entries = mallocarray(cfg->gpt_total_entries, | |||||
| sizeof(*pt->gpt_entries), M_GASKET_PT, M_WAITOK | M_ZERO); | |||||
| pt->gpt_dev = dev; | |||||
| pt->gpt_csr = csr; | |||||
| pt->gpt_cfg = *cfg; | |||||
| pt->gpt_extended_flag = 1ULL << cfg->gpt_extended_bit; | |||||
| pt->gpt_num_simple = cfg->gpt_total_entries; | |||||
| sx_init(&pt->gpt_lock, "gasket page table"); | |||||
| error = bus_dma_tag_create(bus_get_dma_tag(dev), PAGE_SIZE, 0, | |||||
| BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, | |||||
| PAGE_SIZE, BUS_DMA_ALLOCNOW, NULL, NULL, &pt->gpt_dma_tag); | |||||
| if (error != 0) { | |||||
| sx_destroy(&pt->gpt_lock); | |||||
| free(pt->gpt_entries, M_GASKET_PT); | |||||
| free(pt, M_GASKET_PT); | |||||
| return (ENOMEM); | |||||
| } | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| for (i = 0; i < cfg->gpt_total_entries; i++) | |||||
| gasket_wr8(csr, gasket_l0_slot_offset(pt, i), 0); | |||||
| gasket_wr8(csr, cfg->gpt_extended_reg, cfg->gpt_total_entries); | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| *ptp = pt; | |||||
| return (0); | |||||
| } | |||||
| void | |||||
| gasket_page_table_free(struct gasket_page_table *pt) | |||||
| { | |||||
| u_int i; | |||||
| if (pt == NULL) | |||||
| return; | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| gasket_unmap_all_locked(pt); | |||||
| for (i = 0; i < pt->gpt_num_extended; i++) | |||||
| gasket_free_subtable_locked(pt, i); | |||||
| gasket_wr8(pt->gpt_csr, pt->gpt_cfg.gpt_extended_reg, | |||||
| pt->gpt_cfg.gpt_total_entries); | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| bus_dma_tag_destroy(pt->gpt_dma_tag); | |||||
| sx_destroy(&pt->gpt_lock); | |||||
| free(pt->gpt_entries, M_GASKET_PT); | |||||
| free(pt, M_GASKET_PT); | |||||
| } | |||||
| int | |||||
| gasket_page_table_map(struct gasket_page_table *pt, vm_offset_t host_addr, | |||||
| uint64_t dev_addr, size_t size, int prot, enum gasket_map_kind kind, | |||||
| vm_ooffset_t coh_off, vm_page_t *pages) | |||||
| { | |||||
| bus_addr_t *bus; | |||||
| vm_ooffset_t limit; | |||||
| bool coherent, extended; | |||||
| u_int first, i, nmapped, npages; | |||||
| int error; | |||||
| if (kind != GASKET_MAP_HOST && kind != GASKET_MAP_COHERENT) | |||||
| return (EINVAL); | |||||
| if (kind == GASKET_MAP_HOST && pages == NULL) | |||||
| return (EINVAL); | |||||
| #ifdef KMSAN | |||||
| /* | |||||
| * kmsan_bus_dmamap_sync() panics on anything but virtual address and | |||||
| * mbuf descriptors, and a page mapped for a device is described | |||||
| * physically. | |||||
| */ | |||||
| if (kind == GASKET_MAP_HOST) | |||||
| return (EOPNOTSUPP); | |||||
| #endif | |||||
| if (pt != NULL && size / PAGE_SIZE > gasket_page_table_max_pages(pt)) | |||||
| return (EINVAL); | |||||
| if (pt == NULL || size == 0 || (size & PAGE_MASK) != 0 || | |||||
| (host_addr & PAGE_MASK) != 0 || host_addr + size < host_addr || | |||||
| (prot & ~(VM_PROT_READ | VM_PROT_WRITE)) != 0 || | |||||
| (prot & (VM_PROT_READ | VM_PROT_WRITE)) == 0) | |||||
| return (EINVAL); | |||||
| npages = (u_int)(size >> GASKET_PAGE_SHIFT); | |||||
| if ((size >> GASKET_PAGE_SHIFT) != npages) | |||||
| return (EINVAL); | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| if (pt->gpt_num_active + npages > gasket_max_wired_pages || | |||||
| pt->gpt_num_active + npages < pt->gpt_num_active) { | |||||
| error = ENOMEM; | |||||
| goto out; | |||||
| } | |||||
| error = gasket_validate_dev_range_locked(pt, dev_addr, npages, | |||||
| &extended, &first); | |||||
| if (error != 0) | |||||
| goto out; | |||||
| if (extended) { | |||||
| error = gasket_prepare_extended_locked(pt, first, npages); | |||||
| if (error != 0) { | |||||
| gasket_garbage_collect_locked(pt); | |||||
| goto out; | |||||
| } | |||||
| } else if (!gasket_simple_range_free_locked(pt, first, npages)) { | |||||
| error = EBUSY; | |||||
| goto out; | |||||
| } | |||||
| bus = mallocarray(npages, sizeof(*bus), M_GASKET_PT, | |||||
| M_WAITOK | M_ZERO); | |||||
| coherent = kind == GASKET_MAP_COHERENT; | |||||
| if (coherent) { | |||||
| limit = (vm_ooffset_t)pt->gpt_coherent_npages * PAGE_SIZE; | |||||
| if (pt->gpt_coherent_pages == NULL || coh_off > limit || | |||||
| (vm_ooffset_t)size > limit - coh_off) { | |||||
| error = EINVAL; | |||||
| goto out_free; | |||||
| } | |||||
| for (i = 0; i < npages; i++) | |||||
| bus[i] = pt->gpt_coherent_bus + (bus_addr_t)coh_off + | |||||
| (bus_addr_t)i * PAGE_SIZE; | |||||
| } | |||||
| nmapped = 0; | |||||
| if (extended) | |||||
| error = gasket_map_extended_locked(pt, first, pages, bus, | |||||
| npages, prot, kind, &nmapped); | |||||
| else | |||||
| error = gasket_map_simple_locked(pt, first, pages, bus, | |||||
| npages, prot, kind, &nmapped); | |||||
| if (error != 0) { | |||||
| if (extended) | |||||
| gasket_unmap_extended_locked(pt, first, nmapped); | |||||
| else | |||||
| gasket_unmap_simple_locked(pt, first, nmapped); | |||||
| } | |||||
| out_free: | |||||
| if (error != 0 && extended) | |||||
| gasket_garbage_collect_locked(pt); | |||||
| free(bus, M_GASKET_PT); | |||||
| out: | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| return (error); | |||||
| } | |||||
| int | |||||
| gasket_page_table_unmap(struct gasket_page_table *pt, uint64_t dev_addr, | |||||
| size_t size) | |||||
| { | |||||
| bool extended; | |||||
| u_int first, npages; | |||||
| int error; | |||||
| if (pt == NULL || size == 0 || (size & PAGE_MASK) != 0) | |||||
| return (EINVAL); | |||||
| npages = (u_int)(size >> GASKET_PAGE_SHIFT); | |||||
| if ((size >> GASKET_PAGE_SHIFT) != npages) | |||||
| return (EINVAL); | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| error = gasket_validate_dev_range_locked(pt, dev_addr, npages, | |||||
| &extended, &first); | |||||
| if (error == 0) { | |||||
| if (extended) | |||||
| gasket_unmap_extended_locked(pt, first, npages); | |||||
| else | |||||
| gasket_unmap_simple_locked(pt, first, npages); | |||||
| } | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| return (error); | |||||
| } | |||||
| void | |||||
| gasket_page_table_unmap_all(struct gasket_page_table *pt) | |||||
| { | |||||
| if (pt == NULL) | |||||
| return; | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| gasket_unmap_all_locked(pt); | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| } | |||||
| void | |||||
| gasket_page_table_set_coherent(struct gasket_page_table *pt, | |||||
| const vm_paddr_t *pages, u_int npages, bus_addr_t bus) | |||||
| { | |||||
| if (pt == NULL) | |||||
| return; | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| pt->gpt_coherent_pages = pages; | |||||
| pt->gpt_coherent_npages = npages; | |||||
| pt->gpt_coherent_bus = bus; | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| } | |||||
| void | |||||
| gasket_page_table_unmap_coherent(struct gasket_page_table *pt) | |||||
| { | |||||
| struct gasket_subtable *subtable; | |||||
| u_int i, j; | |||||
| if (pt == NULL) | |||||
| return; | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| for (i = 0; i < pt->gpt_num_simple; i++) { | |||||
| if (pt->gpt_entries[i].gle_simple.gm_mapped && | |||||
| pt->gpt_entries[i].gle_simple.gm_kind == | |||||
| GASKET_MAP_COHERENT) | |||||
| gasket_unmap_simple_locked(pt, i, 1); | |||||
| } | |||||
| for (i = 0; i < pt->gpt_num_extended; i++) { | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + i].gle_subtable; | |||||
| if (subtable == NULL) | |||||
| continue; | |||||
| for (j = 0; j < GASKET_SUBTABLE_ENTRIES; j++) { | |||||
| if (subtable->gst_entries[j].gm_mapped && | |||||
| subtable->gst_entries[j].gm_kind == | |||||
| GASKET_MAP_COHERENT) | |||||
| gasket_unmap_extended_locked(pt, | |||||
| i * GASKET_SUBTABLE_ENTRIES + j, 1); | |||||
| } | |||||
| } | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| } | |||||
| static void | |||||
| gasket_garbage_collect_locked(struct gasket_page_table *pt) | |||||
| { | |||||
| struct gasket_subtable *subtable; | |||||
| u_int lvl0; | |||||
| sx_assert(&pt->gpt_lock, SA_XLOCKED); | |||||
| for (lvl0 = 0; lvl0 < pt->gpt_num_extended; lvl0++) { | |||||
| subtable = pt->gpt_entries[pt->gpt_num_simple + | |||||
| lvl0].gle_subtable; | |||||
| if (subtable == NULL) | |||||
| continue; | |||||
| if (gasket_mapping_range_free_locked(pt, subtable->gst_entries, | |||||
| GASKET_SUBTABLE_ENTRIES)) | |||||
| gasket_free_subtable_locked(pt, lvl0); | |||||
| } | |||||
| } | |||||
| int | |||||
| gasket_page_table_partition(struct gasket_page_table *pt, u_int num_simple) | |||||
| { | |||||
| u_int i, start; | |||||
| if (pt == NULL) | |||||
| return (EINVAL); | |||||
| sx_xlock(&pt->gpt_lock); | |||||
| if (num_simple > pt->gpt_cfg.gpt_total_entries) { | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| gasket_garbage_collect_locked(pt); | |||||
| start = MIN(pt->gpt_num_simple, num_simple); | |||||
| for (i = start; i < pt->gpt_cfg.gpt_total_entries; i++) { | |||||
| if (pt->gpt_entries[i].gle_simple.gm_mapped || | |||||
| pt->gpt_entries[i].gle_subtable != NULL) { | |||||
| device_printf(pt->gpt_dev, | |||||
| "page table entry %u is still in use\n", i); | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| } | |||||
| pt->gpt_num_simple = num_simple; | |||||
| pt->gpt_num_extended = pt->gpt_cfg.gpt_total_entries - num_simple; | |||||
| gasket_wr8(pt->gpt_csr, pt->gpt_cfg.gpt_extended_reg, num_simple); | |||||
| sx_xunlock(&pt->gpt_lock); | |||||
| return (0); | |||||
| } | |||||
| u_int | |||||
| gasket_page_table_num_entries(struct gasket_page_table *pt) | |||||
| { | |||||
| if (pt == NULL) | |||||
| return (0); | |||||
| return (pt->gpt_num_simple + pt->gpt_num_extended); | |||||
| } | |||||
| u_int | |||||
| gasket_page_table_num_simple_entries(struct gasket_page_table *pt) | |||||
| { | |||||
| u_int n; | |||||
| if (pt == NULL) | |||||
| return (0); | |||||
| sx_slock(&pt->gpt_lock); | |||||
| n = pt->gpt_num_simple; | |||||
| sx_sunlock(&pt->gpt_lock); | |||||
| return (n); | |||||
| } | |||||
| u_int | |||||
| gasket_page_table_max_pages(struct gasket_page_table *pt) | |||||
| { | |||||
| u_int n; | |||||
| if (pt == NULL) | |||||
| return (0); | |||||
| sx_slock(&pt->gpt_lock); | |||||
| n = pt->gpt_num_simple + | |||||
| pt->gpt_num_extended * GASKET_SUBTABLE_ENTRIES; | |||||
| sx_sunlock(&pt->gpt_lock); | |||||
| return (n); | |||||
| } | |||||
| u_int | |||||
| gasket_page_table_num_active_pages(struct gasket_page_table *pt) | |||||
| { | |||||
| u_int nactive; | |||||
| if (pt == NULL) | |||||
| return (0); | |||||
| sx_slock(&pt->gpt_lock); | |||||
| nactive = pt->gpt_num_active; | |||||
| sx_sunlock(&pt->gpt_lock); | |||||
| return (nactive); | |||||
| } | |||||