Skip to content

Add Draft version and AI generated docs for details. - #7

Draft
Toasterson wants to merge 9 commits into
masterfrom
feat/upstream-sync-2026
Draft

Add Draft version and AI generated docs for details.#7
Toasterson wants to merge 9 commits into
masterfrom
feat/upstream-sync-2026

Conversation

@Toasterson

Copy link
Copy Markdown

To gain more insight about the gfx-drm code I tasked Claude Code to compare the DRM versions and find out how it got ported back in the day so we can plan to repeat that comparing to modern illumos source code and linux DRM.

The Finding: We did the same shim based porting approach as FreeBSD and other OSes

The result was so detailed and in my opinion good that I decided to let it directly generate these 2000 lines of shim code and detailed documentation. Since this project is out of illumo-gate tree it's a good testing bed to see if this will make some booting ISO's over easter. DRM source file update will be with a seperate commit using the upstream-sync.sh script.

Signed-off-by: Till Wegmueller <toasterson@gmail.com>
@Toasterson Toasterson self-assigned this Mar 31, 2026
New shim headers for modern DRM API compatibility:

- drm_sun_completion.h: struct completion, wait_event/wake_up macros
- drm_sun_print.h: drm_dbg/drm_err per-device logging, WARN_ON_ONCE
- drm_sun_managed.h: drmm_kzalloc/kcalloc/kfree stubs
- drm_sun_debugfs.h: no-op stubs (illumos has no debugfs)
- drm_sun_gem_modern.h: drm_gem_object_funcs, get/put, DRIVER_RENDER

Extended drm_linux.h with cpu_to_le32/64, atomic64_t, rwsem->krwlock_t,
kvmalloc, container_of, ERR_PTR, READ_ONCE/WRITE_ONCE.

Extended drm_sun_workqueue with schedule_work/flush_work backed by a
module-global system taskq with per-work_struct completion tracking.
Port the core virtio-gpu driver transport from Linux 7.0 to illumos,
translating Linux's virtqueue_add_sgs scatter-gather model to the
illumos virtio_chain_alloc/append/submit chain API.

New UAPI headers:
- virtgpu_drm.h: ioctl numbers and structures (__u32 -> uint32_t)
- virtio_gpu_hw.h: virtio-gpu protocol definitions (__le32 -> uint32_t)

Driver private header (virtgpu_drv.h):
- All structures adapted for illumos: virtio_t, virtio_queue_t,
  kmutex_t, kmem_cache_t, wait_queue_head_t, boolean_t
- drm_gem_shmem_object with ddi_dma backing (Phase 2 populates)
- Minimal sg_table/scatterlist for DMA cookie iteration

Transport layer (virtgpu_vq.c):
- vbuf pool via kmem_cache + virtio_dma_alloc per-vbuf
- Command submission: bcopy to DMA VA, chain append with PA
- Completion: virtio_queue_poll -> virtio_chain_data -> bcopy resp
- 2D commands: create_resource, unref, set_scanout, resource_flush,
  transfer_to_host_2d, attach_backing, detach_backing
- 3D/blob commands stubbed for later phases

Device init (virtgpu_kms.c):
- virtio_init -> virtio_init_features -> virtio_queue_alloc x2
  -> virtio_init_complete lifecycle
- Feature negotiation via virtio_feature_present
- Config reads via virtio_dev_get32
- Interrupt handlers registered via virtio_queue_alloc

Fence driver (virtgpu_fence.c):
- Uses drm_sun_dma_fence shim for fence alloc/emit/signal
Fence list iteration (virtgpu_fence.c):
- Implement virtio_gpu_fence_event_process using gfx-drm's
  list_for_each_entry_safe macro from drm_linux_list.h
- Signal target fence and all earlier fences in the same context
- Fix list_add_tail call to use 3-arg gfx-drm list API

Config change handler (virtgpu_kms.c):
- Implement virtio_gpu_config_changed_work_func reading events_read
  from config space and dispatching display info queries
- Write events_clear via virtio_dev_put32 to ack events
- Fix list init from illumos list_create to INIT_LIST_HEAD

Data payload DMA (virtgpu_vq.c):
- Allocate virtio_dma_t for data payloads at submit time
- Copy caller data into DMA VA, append PA to chain
- Add data_dma field to vbuffer struct for clean ownership

Capset retrieval (virtgpu_vq.c):
- Implement capset_info callback parsing response fields into
  vgdev->capsets[i] using resp_cb_data for index passing
- Implement cmd_get_capset with response buffer allocation,
  DMA-backed capset cache, and list management

Display info callback:
- Parse resp_display_info into vgdev->outputs[i].info
- Log scanout dimensions for debugging

Context resource commands:
- Implement cmd_context_attach_resource and detach_resource
  with proper virtio_gpu_ctrl_hdr population

Struct fixes (virtgpu_drv.h):
- Move virtio_gpu_output before virtio_gpu_device
- Add outputs[VIRTIO_GPU_MAX_SCANOUTS] array to device struct
- Add data_dma field to virtio_gpu_vbuffer
Phase 2 - GEM memory management:
- drm_sun_gem_shmem.h/.c: DMA attribute definitions for GEM backing
- virtgpu_object.c: Resource ID allocation, object creation using
  ddi_dma_alloc_handle + ddi_dma_mem_alloc for DMA-capable pages,
  scatter-gather table built from ddi_dma_cookie_iter
- virtgpu_gem.c: dumb_create, object arrays with flexible member,
  multi-object reservation locking via dma_resv, deferred free via
  workqueue, array lifecycle management

Phase 3 - Driver shell and ioctls:
- virtgpu_sunmod.c: Full DDI entry point following gfx-drm pattern
  with _init/_fini/_info, attach/detach/quiesce lifecycle. Attach
  sequence: virtio_init -> virtio_init_features -> virtio_gpu_init
  -> ddi_create_minor_node for render node
- virtgpu_ioctl.c: 11 ioctl handlers using gfx-drm DRM_IOCTL_DEF
  format with ddi_copyin/copyout. Implemented: MAP, GETPARAM,
  RESOURCE_CREATE, RESOURCE_INFO, TRANSFER_TO_HOST (2D),
  WAIT, GET_CAPS. Stubbed: EXECBUFFER, TRANSFER_FROM_HOST,
  CREATE_BLOB, CONTEXT_INIT
- virtgpu_submit.c: execbuffer stub returning ENOSYS
- virtgpu_debugfs.c: no-op (illumos has no debugfs)
- virtgpu_display.c: modeset_init/fini stubs for headless mode

Phase 4 - Build system:
- Makefile.mod: VIRTIO_GPU_OBJS list (10 object files)
- intel/virtio_gpu/Makefile: Build rules with -Nmisc/drm -Nmisc/virtio
- virtio_gpu.conf: PCI binding for pci1af4,1050

Also includes Phase 2-3 updates to existing files:
- virtgpu_drv.h: Added data_dma field, moved output struct,
  added outputs array to device
- virtgpu_vq.c: Data payload DMA via vbuf->data_dma
- virtgpu_kms.c: Config change handler, INIT_LIST_HEAD usage
- virtgpu_fence.c: list_for_each_entry_safe iteration
libdrm header package (system-header-header-drm.mf):
- Add virtgpu_drm.h to installed DRM headers

libdrm library package (x11-library-libdrm.mf):
- Uncomment virtgpu_drm.h (was already referenced but disabled)

DRM driver package (driver-graphics-drm.mf):
- Add virtio_gpu kernel driver module and conf file
- Add driver binding for PCI ID pci1af4,1050 (virtio-gpu modern)

These changes allow Mesa's virgl driver to find the virtgpu_drm.h
UAPI header at build time, and ensure the kernel driver is properly
packaged and bound to virtio-gpu PCI devices in QEMU/KVM VMs.
Rather than porting the full Linux drm_atomic.c + drm_atomic_helper.c
(~4,500 lines), provide a minimal atomic shim (drm_sun_atomic.h) that
satisfies virtio-gpu's simple display needs using the existing 3.14
DRM core infrastructure.

drm_sun_atomic.h:
- Minimal atomic state structs (crtc_state, plane_state, connector_state)
- State management functions as no-ops (reset, duplicate, destroy)
- drm_crtc_init_with_planes -> wraps drm_crtc_init
- drmm_mode_config_init -> wraps drm_mode_config_init
- drm_simple_encoder_init -> wraps drm_encoder_init
- drm_connector_attach_encoder -> wraps drm_mode_connector_attach_encoder
- GEM framebuffer helpers (create_handle, destroy)
- EDID stubs

virtgpu_display.c (full implementation replacing stub):
- CRTC with mode_set_nofb sending SET_SCANOUT to host
- Virtual connector with get_modes returning CVT modes from
  host-reported display dimensions
- Virtual encoder (pass-through)
- Connector detect using host display_info.enabled
- Mode config with min/max resolution bounds
- Per-scanout output initialization
- VBlank init for timing

virtgpu_drv.h:
- Add drm_crtc, drm_connector, drm_encoder to virtio_gpu_output
- Add drm_crtc_to_virtio_gpu_output container_of macro

This uses the gfx-drm 3.14 non-atomic path (drm_crtc_helper_set_config)
for mode setting, which is simpler and works for virtio-gpu's needs.
Virtio framework API fixes:
- virtio_init() takes 1 arg (dip only), not 3
- virtio_init_features() takes 3 args (vio, features, allow_unknown)
- virtio_features_present (plural), not virtio_feature_present
- virtio_dma_va() takes 2 args (dma, offset), not 1
- virtio_dma_attr_sgl doesn't exist; define our own virtgpu_dma_attr

DRM 3.14 core API fixes:
- drm_connector_register/unregister don't exist; add no-op stubs
  in drm_sun_atomic.h (illumos has no sysfs)
- drm_set_preferred_mode doesn't exist; add inline implementation
  that iterates probed_modes list
- mode_set_nofb doesn't exist in 3.14 crtc_helper_funcs; use
  mode_set(crtc, mode, adjusted_mode, x, y, old_fb) instead
- Add required dpms/prepare/commit/mode_fixup callbacks for CRTC
  and encoder helper funcs (3.14 requires these)
- drm_encoder_init requires non-NULL funcs; provide minimal
  virtgpu_simple_encoder_funcs with drm_encoder_cleanup
- fb->obj[0] doesn't exist in 3.14 drm_framebuffer; remove
  GEM framebuffer helpers that reference it

Ioctl table fix:
- Use [DRM_IOCTL_NR(ioctl) - DRM_COMMAND_BASE] for 0-based
  indexing matching i915 I915_IOCTL_DEF pattern
- Change drm_ioctl_desc_t to struct drm_ioctl_desc

Header organization:
- Move VIRTGPU_WANTED_FEATURES to virtgpu_drv.h (shared by
  kms.c and sunmod.c)
- Add virtgpu_dma_attr extern declaration to virtgpu_drv.h
docs/virtio-gpu/:
  All planning and analysis documents for the DRM re-port project
  including virtio-gpu implementation plan, API estimates, driver
  comparison, dma_fence design, and deliverables spec.

vm/:
  QEMU/libvirt VM configs for testing virtio-gpu driver:
  - virtgpu-dev.kdl: Refraction-forger spec for OI dev image with
    gcc-10, system headers, onbld, SSH, serial console, 20GB disk
  - start-vm.sh: QEMU launch script with virtio-gpu-pci + SSH fwd
  - virtgpu-test.xml: Libvirt domain definition
  - user-data/meta-data: Cloud-init config (root:openindiana)

.claude/CLAUDE.md:
  Session context for continuing work on another machine.
  Documents all phases, architecture decisions, build/test
  instructions, and file inventory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant