module Platform.Linux.Nvidia.PlanHostPeers import Data.Bytes import Platform.Linux.Nvidia.PlanHost import Runtime.NativePhysicalProgram import Std.Natural -- The cards of one process reading and writing one another's memory -- (docs/PRD-8XH100-ON-A-BUDGET.md F4), on the direct-RM lifecycle and -- PlanHost's sessions (one RM client, a session a card), without UVM: some -- RunPod hosts refuse /dev/nvidia-uvm (docs/PLAN-NANOCHAT-SPEEDRUN.md). -- -- 1. For every pair of sessions a < b on different cards (`card s` is -- session s's), an NV50_P2P object (class 0x503b, -- its parent the client: resource_list.h) joining their subdevices, -- the peer IDs RM's to choose (cl503b.h). With it RM programs the -- link -- NVLink through the NVSwitch on an HGX H100 -- and gives -- each GPU a peer ID for the other (p2p_api.c). -- 2. For every session s and every session t, t = s included: a virtual -- reservation in s's address space at `window t` (NV01_MEMORY_VIRTUAL, -- 0x70, as PlanHost's own buffers take) and t's buffer `index` mapped -- into it (NV_ESC_RM_MAP_MEMORY_DMA, NVOS46: hDevice s's, hMemory t's). -- A peer's map carries NVOS46_FLAGS_P2P_ENABLE_NOSLI with source -- subdevice 0 (nvos.h; dmaAllocMapping takes the peer ID from the -- memory's GPU: virt_mem_allocator_gm107.c); a session's own, or a -- session's on the same card (virtual ranks: one GPU, a device object -- a session, which RM refuses only on a vGPU: device.c), the plain map. -- -- So every card sees every card's buffer at the same address, `window t`: -- a kernel addresses rank t's copy of an element at window t plus the -- element's offset, on every rank alike (Realization.Nvidia.SM86. -- PeerCollectiveSM86). The sources were read at open-gpu-kernel-modules -- 580.95.05 (kept under /mnt/donto-data/donto-resources). -- NVOS46_FLAGS_P2P_ENABLE (21:20) = NOSLI (2); P2P_SUBDEVICE_ID (24:22) 0 def ppPeerFlag : Nat = 0x200000 -- a pair's P2P object, a reservation's handle def ppPairHandle = (lambda unrestricted a : Nat . (lambda unrestricted b : Nat . (naturalAdd 0xB2000000 (naturalAdd (naturalMultiply a 16) b)))) def ppWindowHandle = (lambda unrestricted session : Nat . (lambda unrestricted peer : Nat . (phSessionHandle session (naturalAdd 0xB3000000 peer)))) -- NV503B_ALLOC_PARAMETERS: hSubDevice (0), hPeerSubDevice (4), the masks, -- mailbox and flags zero; 72 bytes through 580, 88 from 590 (two fabric DMA -- infos added: cl503b.h at 590.44.01 and 595.45.04) def ppPairParamsSize = (lambda unrestricted driver : (family NvidiaDriverInterface) . (naturalSelect (naturalLess (nvidiaDriverInterfaceMajorOf driver) 590) 72 88)) -- the call blocks: the UVM DMA selector's (unused under direct RM), the -- parameters after its own, the statuses in RM slots 20 and 21 def ppBlock : Nat = phDMAObject def ppParams : Nat = phDMAObjectP def ppAllocStatus : Nat = (phRMStatusSlot 20) def ppMapStatus : Nat = (phRMStatusSlot 21) def ppPair = (lambda unrestricted abi : (family NvidiaPlanHostABI) . (lambda unrestricted a : Nat . (lambda unrestricted b : Nat . (phDriverChoice (phDrivers abi) (lambda unrestricted driver : (family NvidiaDriverInterface) . (lambda unrestricted tail : (family NativePhysicalCommands) . (phFill ppParams (phSet32 (phSet32 (phZeros (ppPairParamsSize driver)) 0 (phHSubdeviceS a)) 4 (phHSubdeviceS b)) (phAlloc b"alloc:p2p" 0 ppBlock 0 0x503b ppParams (ppPairParamsSize driver) (ppPairHandle a b) ppAllocStatus tail)))))))) -- a choice of two byte strings (flag nonzero: the first) def ppSelectBytes = (lambda unrestricted flag : Nat . (lambda unrestricted whenNonzero : Bytes . (lambda unrestricted whenZero : Bytes . (nat-eliminate (lambda unrestricted current : Nat . Bytes) whenZero (lambda unrestricted p : Nat . (lambda unrestricted ignored : Bytes . whenNonzero)) flag)))) -- NVOS46 into session s's reservation of `peer`'s window, as PlanHost's -- phMapDMA builds it (hClient copied from the root at run time) def ppMapWith = (lambda unrestricted abi : (family NvidiaPlanHostABI) . (lambda unrestricted sameCard : Nat . (lambda unrestricted session : Nat . (lambda unrestricted peer : Nat . (lambda unrestricted index : Nat . (lambda unrestricted gpu : Nat . (lambda unrestricted extent : Nat . (lambda unrestricted driver : (family NvidiaDriverInterface) . (lambda unrestricted tail : (family NativePhysicalCommands) . (let unrestricted status = (naturalAdd ppBlock (nvidiaDriverInterfaceNVOS46StatusOf driver)) in (let unrestricted flags = (naturalAdd (phDirectDMAFlags abi) (naturalSelect sameCard 0 ppPeerFlag)) in (let unrestricted handles = (phSet32 (phSet32 (phSet32 (phSet32 (phZeros (nvidiaDriverInterfaceNVOS46SizeOf driver)) 4 (phHDeviceS session)) 8 (ppWindowHandle session peer)) 12 (phHMemoryS peer index)) 32 flags) in (let unrestricted block = (phSet32 (phSet64 (phSet64 handles 24 extent) (nvidiaDriverInterfaceNVOS46DMAOffsetOf driver) gpu) (nvidiaDriverInterfaceNVOS46StatusOf driver) phStatusSentinel) in (phCopy ppBlock block (phCopyStateToState ppBlock (naturalAdd phRoot 8) 4 (phCall phSysIoctl (phArgs3 (phSlot phSlotControl) (phImm (nvidiaDriverInterfaceMapDMAIoctlOf driver)) (phState ppBlock)) b"" phDiscard (phCopyStateToState ppMapStatus status 4 (phCheckStateZero32 (ppSelectBytes sameCard b"dma-map-window" b"dma-map-peer") 0 status tail)))))))))))))))))) def ppMap = (lambda unrestricted abi : (family NvidiaPlanHostABI) . (lambda unrestricted sameCard : Nat . (lambda unrestricted session : Nat . (lambda unrestricted peer : Nat . (lambda unrestricted index : Nat . (lambda unrestricted gpu : Nat . (lambda unrestricted extent : Nat . (phDriverChoice (phDrivers abi) (ppMapWith abi sameCard session peer index gpu extent))))))))) -- session s's window of `peer`: the reservation, then the map def ppWindow = (lambda unrestricted abi : (family NvidiaPlanHostABI) . (lambda unrestricted sameCard : Nat . (lambda unrestricted session : Nat . (lambda unrestricted peer : Nat . (lambda unrestricted index : Nat . (lambda unrestricted gpu : Nat . (lambda unrestricted extent : Nat . (lambda unrestricted tail : (family NativePhysicalCommands) . (phFill ppParams (phSet32 (phSet64 (phSet64 (phZeros 24) 0 gpu) 8 (naturalSaturatingSubtract (naturalAdd gpu extent) 1)) 16 (phHVASpaceS session)) (phAlloc b"alloc:window" 0 ppBlock (phHDeviceS session) 0x70 ppParams 24 (ppWindowHandle session peer) ppAllocStatus (ppMap abi sameCard session peer index gpu extent tail))))))))))) -- Every pair joined, then every session's window of every session: -- `sessions` cards, buffer `index` of each (the same index in every -- session's layout) of `extent` bytes, peer t's window at `window t`. def ppPeers = (lambda unrestricted abi : (family NvidiaPlanHostABI) . (lambda unrestricted card : (pi unrestricted session : Nat . Nat) . (lambda unrestricted sessions : Nat . (lambda unrestricted index : Nat . (lambda unrestricted extent : Nat . (lambda unrestricted window : (pi unrestricted peer : Nat . Nat) . (lambda unrestricted tail : (family NativePhysicalCommands) . (let unrestricted pairs = (nat-eliminate (lambda unrestricted n : Nat . (pi unrestricted after : (family NativePhysicalCommands) . (family NativePhysicalCommands))) (lambda unrestricted after : (family NativePhysicalCommands) . after) (lambda unrestricted b : Nat . (lambda unrestricted earlier : (pi unrestricted after : (family NativePhysicalCommands) . (family NativePhysicalCommands)) . (lambda unrestricted after : (family NativePhysicalCommands) . (earlier (nat-eliminate (lambda unrestricted n : Nat . (family NativePhysicalCommands)) after (lambda unrestricted a : Nat . (lambda unrestricted later : (family NativePhysicalCommands) . (nat-eliminate (lambda unrestricted n : Nat . (family NativePhysicalCommands)) (ppPair abi a b later) (lambda unrestricted q : Nat . (lambda unrestricted ignored : (family NativePhysicalCommands) . later)) (naturalEqual (card a) (card b))))) b))))) sessions) in (let unrestricted windows = (nat-eliminate (lambda unrestricted n : Nat . (family NativePhysicalCommands)) tail (lambda unrestricted k : Nat . (lambda unrestricted later : (family NativePhysicalCommands) . (let unrestricted session = (naturalDivideUnchecked k sessions) in (let unrestricted peer = (naturalModuloUnchecked k sessions) in (ppWindow abi (naturalEqual (card session) (card peer)) session peer index (window peer) extent later))))) (naturalMultiply sessions sessions)) in (pairs windows))))))))))