Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Ggml Hexagon htp driver

From Leeroopedia


Implementation Metadata
File Name src/ggml-hexagon/htp-drv.cpp
Repository ggml-org/ggml
Lines 418
Language C++
Domain Tags Hardware_Abstraction, DSP_Computing, Driver_Interface
Status Active
Last Updated 2025-05-15 12:00 GMT
Knowledge Sources ggml-org/ggml repository

Overview

htp-drv.cpp is the Hexagon driver interface that dynamically loads and wraps the FastRPC/dspqueue runtime libraries at startup. By dynamically loading the Hexagon runtime, the backend can be compiled on systems without the Hexagon SDK installed and gracefully handle missing DSP hardware at runtime.

Description

The file defines function pointer types for all Hexagon SDK APIs and initializes them to nullptr. The htpdrv_init function dynamically loads shared libraries (libcdsprpc.so on Android/Linux, cdsprpc.dll on Windows) using the cross-platform libdl.h abstraction, then resolves each function symbol via dl_get_sym. Thin wrapper functions forward calls through the resolved function pointers.

The driver wraps the following API categories:

  • RPC memory management -- rpcmem_alloc, rpcmem_alloc2, rpcmem_free, rpcmem_to_fd
  • DSP queue communication -- dspqueue_create, dspqueue_close, dspqueue_export, dspqueue_write, dspqueue_read
  • Remote handle management -- remote_handle64_open, remote_handle64_invoke, remote_handle64_close
  • FastRPC memory mapping -- fastrpc_mmap, fastrpc_munmap
  • Session/handle control -- remote_handle_control, remote_handle64_control, remote_session_control

Usage

The driver is initialized internally by the Hexagon backend during startup. It is not intended for direct external use.

// Internal usage within ggml-hexagon.cpp
htpdrv_init();
// After init, all rpcmem_*, dspqueue_*, remote_* functions are available
void * buf = rpcmem_alloc(heapid, flags, size);

Code Reference

Source Location

Repository File Lines
ggml-org/ggml src/ggml-hexagon/htp-drv.cpp 418

Key Signatures

// Function pointer types
typedef void * (*rpcmem_alloc_pfn_t)(int heapid, uint32_t flags, int size);
typedef void * (*rpcmem_alloc2_pfn_t)(int heapid, uint32_t flags, size_t size);
typedef void   (*rpcmem_free_pfn_t)(void * po);
typedef int    (*rpcmem_to_fd_pfn_t)(void * po);

typedef AEEResult (*dspqueue_create_pfn_t)(int domain, uint32_t flags,
    uint32_t req_queue_size, uint32_t resp_queue_size,
    dspqueue_callback_t packet_callback, dspqueue_callback_t error_callback,
    void * callback_context, dspqueue_t * queue);

// Thin wrappers
void * rpcmem_alloc(int heapid, uint32_t flags, int size);
void   rpcmem_free(void * po);

I/O Contract

Inputs

  • Shared library paths -- Platform-specific library names (libcdsprpc.so or cdsprpc.dll)
  • Symbol names -- FastRPC API function names resolved via dl_get_sym

Outputs

  • Initialized function pointers -- All rpcmem_*, dspqueue_*, and remote_* function pointers populated
  • Error status -- Returns error if libraries cannot be loaded or symbols cannot be resolved

Usage Examples

Internal driver initialization:

// Called once during Hexagon backend startup
int err = htpdrv_init();
if (err != 0) {
    // Hexagon SDK runtime not available
    return;
}

// Now all DSP communication functions are available
dspqueue_t queue;
AEEResult result = dspqueue_create(domain, flags,
    req_queue_size, resp_queue_size,
    callback, error_callback, ctx, &queue);

Related Pages

Implements Principle

Related Implementations

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment