drm/xe/sysctrl: Add system controller component for Xe3p dGPU platforms
Subsystems (2)
v4 cover drm/xe/sysctrl: Add system controller component for Xe3p dGPU platforms
From: Anoop Vijay <[email protected]>
This patch series introduces System Controller (sysctrl) component for Intel Xe3p dGPU platforms.
This component provides the foundational infrastructure for communication with the System Controller firmware using MKHI protocol over a mailbox interface.
Key features introduced:
- Detection and initialization of System Controller interface on Xe3p dGPU platforms
- Mailbox communication with System Controller firmware
- Fragmented message transfer for large command payloads
This implementation establishes the base for future System Controller feature enablement and firmware command handling.
---
v2: (Umesh)
- Fix commit message to 75 chars per line
- Address CI.Hooks and checkpatch warnings
- Add sc_to_xe() helper for cleaner conversions
- Alphabetical order for headers
- Simplify write/read frame logic using memcpy
- Drop redundant frame length checks
- Use xe_mmio_rmw32() for register updates
- Align FIELD_PREP macros
- Improve error handling and timeout messages
- Handle phase_bit revert on write-frame failure
- Update rdata_len on partial receive before return
- Remove unnecessary headers
v3: (Matt, Riana, Umesh)
- Split patch into logical series
- Rename SC_* macros to SYSCTRL_* for consistency
- Move MKHI_* defines to .c file
- Pass xe directly to helpers
- Place devm_add_action_or_reset() after set_sysctrl_region()
- Add kernel-doc for xe_sysctrl_mailbox_init()
- Add short documentation for System Controller in xe_sysctrl.c
- Avoid hardcoded sizes
- Move structs to _types.h
v4: (Matt, Mike)
- Code style improvements
- Add domain-specific MMIO accessor
- Change phase_bit to bool type
- Add input validation and buffer overflow protection
---
Anoop Vijay (6):
drm/xe/sysctrl: Add System Controller register definitions drm/xe/sysctrl: Add System Controller types and structures drm/xe/sysctrl: Add System Controller mailbox implementation drm/xe/sysctrl: Add System Controller initialization drm/xe/sysctrl: Integrate System Controller into device drm/xe/sysctrl: Enable System Controller for Xe3p
drivers/gpu/drm/xe/Makefile | 2 + drivers/gpu/drm/xe/regs/xe_sysctrl_regs.h | 44 ++ drivers/gpu/drm/xe/xe_device.c | 5 + drivers/gpu/drm/xe/xe_device_types.h | 6 + drivers/gpu/drm/xe/xe_pci.c | 2 + drivers/gpu/drm/xe/xe_pci_types.h | 1 + drivers/gpu/drm/xe/xe_sysctrl.c | 80 ++++ drivers/gpu/drm/xe/xe_sysctrl.h | 13 + drivers/gpu/drm/xe/xe_sysctrl_mailbox.c | 445 ++++++++++++++++++ drivers/gpu/drm/xe/xe_sysctrl_mailbox.h | 35 ++ drivers/gpu/drm/xe/xe_sysctrl_mailbox_types.h | 36 ++ drivers/gpu/drm/xe/xe_sysctrl_types.h | 33 ++ 12 files changed, 702 insertions(+) create mode 100644 drivers/gpu/drm/xe/regs/xe_sysctrl_regs.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl.c create mode 100644 drivers/gpu/drm/xe/xe_sysctrl.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox.c create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox_types.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_types.h
Signature · 1 line
2.43.0
PATCH v4 1/6 drm/xe/sysctrl: Add System Controller register definitions
From: Anoop Vijay <[email protected]>
Add register definitions for System Controller mailbox interface on Xe3p dGPU platforms.
---
v4: (Matt)
- Use lowercase hex values
- Align macro definitions to column 49
- Change to relative register offsets
--- drivers/gpu/drm/xe/regs/xe_sysctrl_regs.h | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 drivers/gpu/drm/xe/regs/xe_sysctrl_regs.h
drivers/gpu/drm/xe/regs/xe_sysctrl_regs.h new file +44 -0
@@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2026 Intel Corporation +*/ + +#ifndef _XE_SYSCTRL_REGS_H_ +#define _XE_SYSCTRL_REGS_H_ + +#include "xe_regs.h" + +#define SYSCTRL_BASE_OFFSET 0xdb000 +#define SYSCTRL_BASE (SOC_BASE + SYSCTRL_BASE_OFFSET) +#define SYSCTRL_MAILBOX_INDEX 0x03 +#define SYSCTRL_BAR_LENGTH 0x1000 + +#define SYSCTRL_MB_CTRL XE_REG(0x10) +#define SYSCTRL_MB_CTRL_RUN_BUSY REG_BIT(31) +#define SYSCTRL_MB_CTRL_IRQ REG_BIT(30) +#define SYSCTRL_MB_CTRL_RUN_BUSY_OUT REG_BIT(29) +#define SYSCTRL_MB_CTRL_PARAM3_MASK REG_GENMASK(28, 24) +#define SYSCTRL_MB_CTRL_PARAM2_MASK REG_GENMASK(23, 16) +#define SYSCTRL_MB_CTRL_PARAM1_MASK REG_GENMASK(15, 8) +#define SYSCTRL_MB_CTRL_COMMAND_MASK REG_GENMASK(7, 0) + +#define SYSCTRL_MB_DATA0 XE_REG(0x14) +#define SYSCTRL_MB_DATA1 XE_REG(0x18) +#define SYSCTRL_MB_DATA2 XE_REG(0x1C) +#define SYSCTRL_MB_DATA3 XE_REG(0x20) + +#define MKHI_FRAME_PHASE REG_BIT(24) +#define MKHI_FRAME_CURRENT_MASK REG_GENMASK(21, 16) +#define MKHI_FRAME_TOTAL_MASK REG_GENMASK(13, 8) +#define MKHI_FRAME_COMMAND_MASK REG_GENMASK(7, 0) + +#define SYSCTRL_MB_FRAME_SIZE 16 +#define SYSCTRL_MB_MAX_FRAMES 64 +#define SYSCTRL_MB_MAX_MESSAGE_SIZE (SYSCTRL_MB_FRAME_SIZE * SYSCTRL_MB_MAX_FRAMES) +#define SYSCTRL_MKHI_COMMAND 5 + +#define SYSCTRL_MB_DEFAULT_TIMEOUT_MS 500 +#define SYSCTRL_MB_RETRY_TIMEOUT_MS 20 +#define SYSCTRL_MB_POLL_INTERVAL_US 100 + +#endif /* _XE_SYSCTRL_REGS_H_ */
Signature · 1 line
2.43.0
PATCH v4 2/6 drm/xe/sysctrl: Add System Controller types and structures
In reply to Anoop, VijayFrom: Anoop Vijay <[email protected]>
Add type definitions and header declarations for System Controller mailbox communication protocol.
---
v4: (Matt, Mike)
- Add domain-specific MMIO accessor
- Change phase_bit type from u32 to bool
--- drivers/gpu/drm/xe/xe_sysctrl_mailbox.h | 35 ++++++++++++++++++ drivers/gpu/drm/xe/xe_sysctrl_mailbox_types.h | 36 +++++++++++++++++++ drivers/gpu/drm/xe/xe_sysctrl_types.h | 33 +++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox_types.h create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_types.h
drivers/gpu/drm/xe/xe_sysctrl_mailbox.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2026 Intel Corporation +*/ + +#ifndef __XE_SYSCTRL_MAILBOX_H__ +#define __XE_SYSCTRL_MAILBOX_H__ + +#include <linux/bitfield.h> +#include <linux/types.h> + +struct xe_sysctrl ; +struct xe_device ; +struct xe_sysctrl_mailbox_command ; + +#define APP_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define APP_HDR_COMMAND_MASK GENMASK(15, 8) +#define APP_HDR_VERSION_MASK GENMASK(23, 16) +#define APP_HDR_RESERVED_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_APP_HDR_GROUP_ID (hdr) \ + FIELD_GET(APP_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_APP_HDR_COMMAND (hdr) \ + FIELD_GET(APP_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_APP_HDR_VERSION (hdr) \ + FIELD_GET(APP_HDR_VERSION_MASK, le32_to_cpu((hdr)->data)) + +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc); +int xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len); + +#endif /* __XE_SYSCTRL_MAILBOX_H__ */
drivers/gpu/drm/xe/xe_sysctrl_mailbox_types.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2026 Intel Corporation +*/ + +#ifndef __XE_SYSCTRL_MAILBOX_TYPES_H__ +#define __XE_SYSCTRL_MAILBOX_TYPES_H__ + +#include <linux/types.h> + +struct xe_sysctrl_mailbox_mkhi_msg_hdr { +__le32 data; +} __packed; + +struct xe_sysctrl_mailbox_app_msg_hdr { +__le32 data; +} __packed; + +struct xe_sysctrl_mailbox_command { +/** @header: Application message header containing command information */ +struct xe_sysctrl_mailbox_app_msg_hdr header; + +/** @data_in: Pointer to input payload data (can be NULL if no input data) */ +void *data_in; + +/** @data_in_len: Size of input payload in bytes (0 if no input data) */ +size_t data_in_len; + +/** @data_out: Pointer to output buffer for response data (can be NULL if no response) */ +void *data_out; + +/** @data_out_len: Size of output buffer in bytes (0 if no response expected) */ +size_t data_out_len; +}; + +#endif /* __XE_SYSCTRL_MAILBOX_TYPES_H__ */
drivers/gpu/drm/xe/xe_sysctrl_types.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2026 Intel Corporation +*/ + +#ifndef _XE_SYSCTRL_TYPES_H_ +#define _XE_SYSCTRL_TYPES_H_ + +#include <linux/mutex.h> +#include <linux/types.h> + +struct xe_mmio ; + +/** +* struct xe_sysctrl - System Controller driver context +*/ +struct xe_sysctrl { +/** @mmio: MMIO region for system control registers */ +struct xe_mmio *mmio; + +/** @cmd_lock: Mutex protecting mailbox command operations */ +struct mutex cmd_lock; + +/** +* @phase_bit: MKHI message boundary phase toggle bit +* +* Phase bit alternates between 0 and 1 for consecutive +* messages to help distinguish message boundaries. +*/ +bool phase_bit; +}; + +#endif /* _XE_SYSCTRL_TYPES_H_ */
Signature · 1 line
2.43.0
PATCH v4 3/6 drm/xe/sysctrl: Add System Controller mailbox implementation
In reply to Anoop, VijayFrom: Anoop Vijay <[email protected]>
Implement the System Controller mailbox communication layer:
- Low-level frame I/O operations
- Mailbox busy/ready state polling
- Command preparation and fragmentation
- Multi-frame send/receive with phase bit handling
- MKHI protocol header validation
- Public API for sending commands to System Controller
This provides the complete mailbox infrastructure for communicating with System Controller firmware on Xe3p dGPU platforms.
---
v4: (Matt, Mike)
- Refactor MMIO access to use domain-specific accessor
- Add input validation and buffer overflow protection
- Add bounds checking for multi-frame operations
- Fix potential NULL pointer dereference
--- drivers/gpu/drm/xe/xe_sysctrl_mailbox.c | 445 ++++++++++++++++++++++++ 1 file changed, 445 insertions(+) create mode 100644 drivers/gpu/drm/xe/xe_sysctrl_mailbox.c
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +445 -0
@@ -0,0 +1,445 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <linux/bitfield.h> +#include <linux/container_of.h> +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_device_types.h" +#include "xe_mmio.h" +#include "xe_pm.h" +#include "xe_printk.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_mailbox_types.h" +#include "xe_sysctrl_types.h" + +#define MKHI_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define MKHI_HDR_COMMAND_MASK GENMASK(14, 8) +#define MKHI_HDR_IS_RESPONSE BIT(15) +#define MKHI_HDR_RESERVED_MASK GENMASK(23, 16) +#define MKHI_HDR_RESULT_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_MKHI_HDR_GROUP_ID (hdr) \ + FIELD_GET(MKHI_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_COMMAND (hdr) \ + FIELD_GET(MKHI_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (hdr) \ + FIELD_GET(MKHI_HDR_IS_RESPONSE, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_RESULT (hdr) \ + FIELD_GET(MKHI_HDR_RESULT_MASK, le32_to_cpu((hdr)->data)) + +static struct xe_device *sc_to_xe (struct xe_sysctrl *sc) +{ + return container_of(sc, struct xe_device, sc); +} + +static bool xe_sysctrl_mailbox_wait_bit_clear (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32_not (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static bool xe_sysctrl_mailbox_wait_bit_set (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32 (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static int xe_sysctrl_mailbox_write_frame (struct xe_sysctrl *sc,const void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +memcpy (val, frame, len); + +for (i =0 ; i < dw; i++) +xe_mmio_write32 (sc->mmio, regs[i], val[i]); + +return 0 ; +} + +static int xe_sysctrl_mailbox_read_frame (struct xe_sysctrl *sc,void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +for (i =0 ; i < dw; i++) + val[i] =xe_mmio_read32 (sc->mmio, regs[i]); + +memcpy (frame, val, len); + +return 0 ; +} + +static void xe_sysctrl_mailbox_clear_response (struct xe_sysctrl *sc) +{ +xe_mmio_rmw32 (sc->mmio,SYSCTRL_MB_CTRL ,SYSCTRL_MB_CTRL_RUN_BUSY_OUT ,0 ); +} + +static int xe_sysctrl_mailbox_prepare_command (struct xe_device *xe, +u8 group_id,u8 command, +const void *data_in,size_t data_in_len, +u8 **mbox_cmd,size_t *cmd_size) +{ +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t size; +u8 *buffer; + +if (data_in_len >SYSCTRL_MB_MAX_MESSAGE_SIZE -sizeof (*mkhi_hdr)) { +xe_err (xe,"sysctrl: Input data too large: %zu bytes\n" , data_in_len); +return -EINVAL ; + } + + size =sizeof (*mkhi_hdr) + data_in_len; + + buffer =kmalloc (size,GFP_KERNEL ); +if (!buffer) +return -ENOMEM ; + + mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)buffer; + mkhi_hdr->data =cpu_to_le32 (FIELD_PREP (MKHI_HDR_GROUP_ID_MASK , group_id) | +FIELD_PREP (MKHI_HDR_COMMAND_MASK , command &0x7F ) | +FIELD_PREP (MKHI_HDR_IS_RESPONSE ,0 ) | +FIELD_PREP (MKHI_HDR_RESERVED_MASK ,0 ) | +FIELD_PREP (MKHI_HDR_RESULT_MASK ,0 )); + +if (data_in && data_in_len) +memcpy (buffer +sizeof (*mkhi_hdr), data_in, data_in_len); + + *mbox_cmd = buffer; + *cmd_size = size; + +return 0 ; +} + +static int xe_sysctrl_mailbox_send_frames (struct xe_sysctrl *sc, +const u8 *mbox_cmd, +size_t cmd_size,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +u32 ctrl_reg, total_frames, frame; +size_t bytes_sent, frame_size; + + total_frames =DIV_ROUND_UP (cmd_size,SYSCTRL_MB_FRAME_SIZE ); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Mailbox busy\n" ); +return -EBUSY ; + } + + sc->phase_bit ^=1 ; + bytes_sent =0 ; + +for (frame =0 ; frame < total_frames; frame++) { + frame_size =min (cmd_size - bytes_sent, (size_t )SYSCTRL_MB_FRAME_SIZE ); + +if (xe_sysctrl_mailbox_write_frame (sc, mbox_cmd + bytes_sent, frame_size)) { +xe_err (xe,"sysctrl: Failed to write frame %u\n" , frame); + sc->phase_bit ^=1 ; +return -EIO ; + } + + ctrl_reg =SYSCTRL_MB_CTRL_RUN_BUSY | +FIELD_PREP (MKHI_FRAME_CURRENT_MASK , frame) | +FIELD_PREP (MKHI_FRAME_TOTAL_MASK , total_frames -1 ) | +FIELD_PREP (MKHI_FRAME_COMMAND_MASK ,SYSCTRL_MKHI_COMMAND ) | + (sc->phase_bit ?MKHI_FRAME_PHASE :0 ); + +xe_mmio_write32 (sc->mmio,SYSCTRL_MB_CTRL , ctrl_reg); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Frame %u acknowledgment timeout\n" , frame); + sc->phase_bit ^=1 ; +return -ETIMEDOUT ; + } + + bytes_sent += frame_size; + } + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_first_frame (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *out, +size_t out_size, +size_t frame_size, +size_t *payload_bytes) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *resp_hdr; +u32 frame_data[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)]; +size_t hdr_size =sizeof (*resp_hdr); +size_t payload_size; +int ret; + +if (frame_size < hdr_size) { +xe_err (xe,"sysctrl: Frame size %zu too small\n" , frame_size); +return -EPROTO ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, frame_data, frame_size); +if (ret) +return ret; + + resp_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)frame_data; + +if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (resp_hdr) || +XE_SYSCTRL_MKHI_HDR_GROUP_ID (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_GROUP_ID (req) || +XE_SYSCTRL_MKHI_HDR_COMMAND (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_COMMAND (req)) { +xe_err (xe,"SC: Response header mismatch\n" ); +return -EPROTO ; + } + +if (XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr) !=0 ) { +xe_err (xe,"SC: Firmware error: 0x%02lx\n" , +XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr)); +return -EIO ; + } + + payload_size = frame_size - hdr_size; + +if (payload_size > out_size) { +xe_err (xe,"sysctrl: Payload %zu bytes exceeds buffer %zu bytes\n" , + payload_size, out_size); +return -ENOSPC ; + } + +if (payload_size >0 ) +memcpy (out, (u8 *)frame_data + hdr_size, payload_size); + + *payload_bytes = payload_size; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_frame (struct xe_sysctrl *sc, +void *out,size_t frame_size, +unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +int ret; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame timeout\n" ); +return -ETIMEDOUT ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, out, frame_size); +if (ret) +return ret; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_receive_frames (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t hdr_size =sizeof (*mkhi_hdr); +u32 ctrl_reg, total_frames, frame; +size_t received =0 ; +u8 *out = data_out; +size_t frame_size; +int ret =0 ; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame 0 timeout\n" ); +return -ETIMEDOUT ; + } + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + total_frames =FIELD_GET (MKHI_FRAME_TOTAL_MASK , ctrl_reg) +1 ; + +if (total_frames ==1 ) + frame_size =min (hdr_size + data_out_len, (size_t )SYSCTRL_MB_FRAME_SIZE ); +else + frame_size =SYSCTRL_MB_FRAME_SIZE ; + + ret =xe_sysctrl_mailbox_process_first_frame (sc, req, out, data_out_len, + frame_size, &received); +if (ret) +return ret; + + out += received; + +for (frame =1 ; frame < total_frames; frame++) { +size_t remaining; + +if (received >= data_out_len) { +xe_err (xe,"sysctrl: Received %zu bytes exceeds buffer %zu bytes\n" , + received, data_out_len); +return -ENOSPC ; + } + + remaining = data_out_len - received; + frame_size =min_t (size_t, remaining,SYSCTRL_MB_FRAME_SIZE ); + + ret =xe_sysctrl_mailbox_process_frame (sc, out, frame_size, timeout_ms); +if (ret) +break ; + + received += frame_size; + out += frame_size; + } + + *rdata_len = received; + +return ret; +} + +static int xe_sysctrl_mailbox_send_command (struct xe_sysctrl *sc, +const u8 *mbox_cmd,size_t cmd_size, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t received; +int ret; + + ret =xe_sysctrl_mailbox_send_frames (sc, mbox_cmd, cmd_size, timeout_ms); +if (ret) +return ret; + +if (!data_out || !rdata_len) +return 0 ; + + mkhi_hdr = (const struct xe_sysctrl_mailbox_mkhi_msg_hdr *)mbox_cmd; + + ret =xe_sysctrl_mailbox_receive_frames (sc, mkhi_hdr, data_out, data_out_len, + &received, timeout_ms); +if (ret) +return ret; + + *rdata_len = received; + +return 0 ; +} + +/** +* xe_sysctrl_mailbox_init - Initialize System Controller mailbox interface +* @sc: System controller structure +* +* Initialize system controller mailbox interface for communication. +*/ +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc) +{ +u32 ctrl_reg; + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + sc->phase_bit = (ctrl_reg &MKHI_FRAME_PHASE ) ?1 :0 ; +} + +/** +* xe_sysctrl_send_command - Send command to System Controller via mailbox +* @xe: XE device instance +* @cmd: Pointer to xe_sysctrl_mailbox_command structure +* @rdata_len: Pointer to store actual response data size (can be NULL) +* +* Send a command to the System Controller using MKHI protocol. Handles +* command preparation, fragmentation, transmission, and response reception. +* +* Return: 0 on success, negative error code on failure +*/ +int xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len) +{ +struct xe_sysctrl *sc; +u8 group_id, command_code; +u8 *mbox_cmd =NULL ; +size_t cmd_size =0 ; +int ret =0 ; + +if (!xe) { +pr_err ("sysctrl: Invalid device handle\n" ); +return -EINVAL ; + } + +if (!xe->info.has_sysctrl) +return -ENODEV ; + + sc = &xe->sc; + +if (!cmd) { +xe_err (xe,"sysctrl: Invalid command buffer\n" ); +return -EINVAL ; + } + + group_id =XE_SYSCTRL_APP_HDR_GROUP_ID (&cmd->header); + command_code =XE_SYSCTRL_APP_HDR_COMMAND (&cmd->header); + +if (!cmd->data_in && cmd->data_in_len) { +xe_err (xe,"sysctrl: Invalid input parameters\n" ); +return -EINVAL ; + } + +if (!cmd->data_out && cmd->data_out_len) { +xe_err (xe,"sysctrl: Invalid output parameters\n" ); +return -EINVAL ; + } + +might_sleep (); + + ret =xe_sysctrl_mailbox_prepare_command (xe, group_id, command_code, + cmd->data_in, cmd->data_in_len, + &mbox_cmd, &cmd_size); +if (ret) { +xe_err (xe,"sysctrl: Failed to prepare command: %d\n" , ret); +return ret; + } + +guard (xe_pm_runtime)(xe); + +guard (mutex)(&sc->cmd_lock); + + ret =xe_sysctrl_mailbox_send_command (sc, mbox_cmd, cmd_size, + cmd->data_out, cmd->data_out_len, rdata_len, +SYSCTRL_MB_DEFAULT_TIMEOUT_MS ); +if (ret) +xe_err (xe,"sysctrl: Mailbox command failed: %d\n" , ret); + +kfree (mbox_cmd); + +return ret; +}
Signature · 1 line
2.43.0
Re: [PATCH v4 3/6] drm/xe/sysctrl: Add System Controller mailbox implementation
Quoted 207 lines · Vijay
From: Anoop Vijay <[email protected]>
Implement the System Controller mailbox communication layer:
- Low-level frame I/O operations
- Mailbox busy/ready state polling
- Command preparation and fragmentation
- Multi-frame send/receive with phase bit handling
- MKHI protocol header validation
- Public API for sending commands to System ControllerThis provides the complete mailbox infrastructure for communicating with System Controller firmware on Xe3p dGPU platforms.
Signed-off-by Anoop Vijay <[email protected]>---
v4: (Matt, Mike)
- Refactor MMIO access to use domain-specific accessor
- Add input validation and buffer overflow protection
- Add bounds checking for multi-frame operations
- Fix potential NULL pointer dereference
---drivers/gpu/drm/xe/xe_sysctrl_mailbox.c| 445 ++++++++++++++++++++++++1 file changed, 445 insertions(+)create mode 100644
drivers/gpu/drm/xe/xe_sysctrl_mailbox.cdrivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +175
-0@@ -0,0 +1,445 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <linux/bitfield.h> +#include <linux/container_of.h> +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_device_types.h" +#include "xe_mmio.h" +#include "xe_pm.h" +#include "xe_printk.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_mailbox_types.h" +#include "xe_sysctrl_types.h" + +#define MKHI_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define MKHI_HDR_COMMAND_MASK GENMASK(14, 8) +#define MKHI_HDR_IS_RESPONSE BIT(15) +#define MKHI_HDR_RESERVED_MASK GENMASK(23, 16) +#define MKHI_HDR_RESULT_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_MKHI_HDR_GROUP_ID (hdr) \ + FIELD_GET(MKHI_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_COMMAND (hdr) \ + FIELD_GET(MKHI_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (hdr) \ + FIELD_GET(MKHI_HDR_IS_RESPONSE, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_RESULT (hdr) \ + FIELD_GET(MKHI_HDR_RESULT_MASK, le32_to_cpu((hdr)->data)) + +static struct xe_device *sc_to_xe (struct xe_sysctrl *sc) +{ + return container_of(sc, struct xe_device, sc); +} + +static bool xe_sysctrl_mailbox_wait_bit_clear (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32_not (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static bool xe_sysctrl_mailbox_wait_bit_set (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32 (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static int xe_sysctrl_mailbox_write_frame (struct xe_sysctrl *sc,const void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +memcpy (val, frame, len); + +for (i =0 ; i < dw; i++) +xe_mmio_write32 (sc->mmio, regs[i], val[i]); + +return 0 ; +} + +static int xe_sysctrl_mailbox_read_frame (struct xe_sysctrl *sc,void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +for (i =0 ; i < dw; i++) + val[i] =xe_mmio_read32 (sc->mmio, regs[i]); + +memcpy (frame, val, len); + +return 0 ; +} + +static void xe_sysctrl_mailbox_clear_response (struct xe_sysctrl *sc) +{ +xe_mmio_rmw32 (sc->mmio,SYSCTRL_MB_CTRL ,SYSCTRL_MB_CTRL_RUN_BUSY_OUT ,0 ); +} + +static int xe_sysctrl_mailbox_prepare_command (struct xe_device *xe, +u8 group_id,u8 command, +const void *data_in,size_t data_in_len, +u8 **mbox_cmd,size_t *cmd_size) +{ +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t size; +u8 *buffer; + +if (data_in_len >SYSCTRL_MB_MAX_MESSAGE_SIZE -sizeof (*mkhi_hdr)) { +xe_err (xe,"sysctrl: Input data too large: %zu bytes\n" , data_in_len); +return -EINVAL ; + } + + size =sizeof (*mkhi_hdr) + data_in_len; + + buffer =kmalloc (size,GFP_KERNEL ); +if (!buffer) +return -ENOMEM ; + + mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)buffer; + mkhi_hdr->data =cpu_to_le32 (FIELD_PREP (MKHI_HDR_GROUP_ID_MASK , group_id) | +FIELD_PREP (MKHI_HDR_COMMAND_MASK , command &0x7F ) | +FIELD_PREP (MKHI_HDR_IS_RESPONSE ,0 ) | +FIELD_PREP (MKHI_HDR_RESERVED_MASK ,0 ) | +FIELD_PREP (MKHI_HDR_RESULT_MASK ,0 )); + +if (data_in && data_in_len) +memcpy (buffer +sizeof (*mkhi_hdr), data_in, data_in_len); + + *mbox_cmd = buffer; + *cmd_size = size; + +return 0 ; +} + +static int xe_sysctrl_mailbox_send_frames (struct xe_sysctrl *sc, +const u8 *mbox_cmd, +size_t cmd_size,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +u32 ctrl_reg, total_frames, frame; +size_t bytes_sent, frame_size; + + total_frames =DIV_ROUND_UP (cmd_size,SYSCTRL_MB_FRAME_SIZE ); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Mailbox busy\n" ); +return -EBUSY ; + } + + sc->phase_bit ^=1 ; + bytes_sent =0 ; + +for (frame =0 ; frame < total_frames; frame++) { + frame_size =min (cmd_size - bytes_sent, (size_t )SYSCTRL_MB_FRAME_SIZE ); + +if (xe_sysctrl_mailbox_write_frame (sc, mbox_cmd + bytes_sent, frame_size)) { +xe_err (xe,"sysctrl: Failed to write frame %u\n" , frame); + sc->phase_bit ^=1 ; +return -EIO ; + } + + ctrl_reg = SYSCTRL_MB_CTRL_RUN_BUSY |
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 185
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+FIELD_PREP (MKHI_FRAME_CURRENT_MASK , frame) | +FIELD_PREP (MKHI_FRAME_TOTAL_MASK , total_frames -1 ) | +FIELD_PREP (MKHI_FRAME_COMMAND_MASK ,SYSCTRL_MKHI_COMMAND ) | + (sc->phase_bit ?MKHI_FRAME_PHASE :0 ); + +xe_mmio_write32 (sc->mmio,SYSCTRL_MB_CTRL , ctrl_reg); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Frame %u acknowledgment timeout\n" , frame); + sc->phase_bit ^=1 ;
At this point, you have already written to phase bit into the ctrl_reg. How do you know that the FW did not already interpret it? Maybe something you want to check with FW on whether you should reset the phase here.
Quoted 14 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +14
-0+return -ETIMEDOUT ; + } + + bytes_sent += frame_size; + } + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_first_frame(struct xe_sysctrl *sc, + const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, + void *out, + size_t out_size, + size_t frame_size,
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 209
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+ size_t *payload_bytes) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *resp_hdr; +u32 frame_data[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)]; +size_t hdr_size =sizeof (*resp_hdr); +size_t payload_size; +int ret; + +if (frame_size < hdr_size) {
This will never happen because frame_size passed into this function is hdr_size + data_out_len OR SYSCTRL_MB_FRAME_SIZE. Even if data_out_len is 0, this condition will never hit. Hence redundant.
Quoted 16 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +16
-0+xe_err (xe,"sysctrl: Frame size %zu too small\n" , frame_size); +return -EPROTO ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, frame_data, frame_size); +if (ret) +return ret; + + resp_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)frame_data; + +if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (resp_hdr) || +XE_SYSCTRL_MKHI_HDR_GROUP_ID (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_GROUP_ID (req) || +XE_SYSCTRL_MKHI_HDR_COMMAND (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_COMMAND (req)) { +xe_err (xe,"SC: Response header mismatch\n" ); +return -EPROTO ; + }
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 235
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+ +if (XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr) !=0 ) { +xe_err (xe,"SC: Firmware error: 0x%02lx\n" , +XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr)); +return -EIO ; + } + + payload_size = frame_size - hdr_size; + +if (payload_size > out_size) {
Same here. You are still operating on parameters passed into this function and neither payload_size nor out_size is being modified here in this function, so based on what you are passing into it, I don't see this condition ever entering. payload_size will be equal to out_size.
Regards,
Umesh
Quoted 213 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +210
-0+xe_err (xe,"sysctrl: Payload %zu bytes exceeds buffer %zu bytes\n" , + payload_size, out_size); +return -ENOSPC ; + } + +if (payload_size >0 ) +memcpy (out, (u8 *)frame_data + hdr_size, payload_size); + + *payload_bytes = payload_size; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_frame (struct xe_sysctrl *sc, +void *out,size_t frame_size, +unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +int ret; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame timeout\n" ); +return -ETIMEDOUT ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, out, frame_size); +if (ret) +return ret; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_receive_frames (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t hdr_size =sizeof (*mkhi_hdr); +u32 ctrl_reg, total_frames, frame; +size_t received =0 ; +u8 *out = data_out; +size_t frame_size; +int ret =0 ; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame 0 timeout\n" ); +return -ETIMEDOUT ; + } + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + total_frames =FIELD_GET (MKHI_FRAME_TOTAL_MASK , ctrl_reg) +1 ; + +if (total_frames ==1 ) + frame_size =min (hdr_size + data_out_len, (size_t )SYSCTRL_MB_FRAME_SIZE ); +else + frame_size =SYSCTRL_MB_FRAME_SIZE ; + + ret =xe_sysctrl_mailbox_process_first_frame (sc, req, out, data_out_len, + frame_size, &received); +if (ret) +return ret; + + out += received; + +for (frame =1 ; frame < total_frames; frame++) { +size_t remaining; + +if (received >= data_out_len) { +xe_err (xe,"sysctrl: Received %zu bytes exceeds buffer %zu bytes\n" , + received, data_out_len); +return -ENOSPC ; + } + + remaining = data_out_len - received; + frame_size =min_t (size_t, remaining,SYSCTRL_MB_FRAME_SIZE ); + + ret =xe_sysctrl_mailbox_process_frame (sc, out, frame_size, timeout_ms); +if (ret) +break ; + + received += frame_size; + out += frame_size; + } + + *rdata_len = received; + +return ret; +} + +static int xe_sysctrl_mailbox_send_command (struct xe_sysctrl *sc, +const u8 *mbox_cmd,size_t cmd_size, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t received; +int ret; + + ret =xe_sysctrl_mailbox_send_frames (sc, mbox_cmd, cmd_size, timeout_ms); +if (ret) +return ret; + +if (!data_out || !rdata_len) +return 0 ; + + mkhi_hdr = (const struct xe_sysctrl_mailbox_mkhi_msg_hdr *)mbox_cmd; + + ret =xe_sysctrl_mailbox_receive_frames (sc, mkhi_hdr, data_out, data_out_len, + &received, timeout_ms); +if (ret) +return ret; + + *rdata_len = received; + +return 0 ; +} + +/** +* xe_sysctrl_mailbox_init - Initialize System Controller mailbox interface +* @sc: System controller structure +* +* Initialize system controller mailbox interface for communication. +*/ +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc) +{ +u32 ctrl_reg; + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + sc->phase_bit = (ctrl_reg &MKHI_FRAME_PHASE ) ?1 :0 ; +} + +/** +* xe_sysctrl_send_command - Send command to System Controller via mailbox +* @xe: XE device instance +* @cmd: Pointer to xe_sysctrl_mailbox_command structure +* @rdata_len: Pointer to store actual response data size (can be NULL) +* +* Send a command to the System Controller using MKHI protocol. Handles +* command preparation, fragmentation, transmission, and response reception. +* +* Return: 0 on success, negative error code on failure +*/ +int xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len) +{ +struct xe_sysctrl *sc; +u8 group_id, command_code; +u8 *mbox_cmd =NULL ; +size_t cmd_size =0 ; +int ret =0 ; + +if (!xe) { +pr_err ("sysctrl: Invalid device handle\n" ); +return -EINVAL ; + } + +if (!xe->info.has_sysctrl) +return -ENODEV ; + + sc = &xe->sc; + +if (!cmd) { +xe_err (xe,"sysctrl: Invalid command buffer\n" ); +return -EINVAL ; + } + + group_id =XE_SYSCTRL_APP_HDR_GROUP_ID (&cmd->header); + command_code =XE_SYSCTRL_APP_HDR_COMMAND (&cmd->header); + +if (!cmd->data_in && cmd->data_in_len) { +xe_err (xe,"sysctrl: Invalid input parameters\n" ); +return -EINVAL ; + } + +if (!cmd->data_out && cmd->data_out_len) { +xe_err (xe,"sysctrl: Invalid output parameters\n" ); +return -EINVAL ; + } + +might_sleep (); + + ret =xe_sysctrl_mailbox_prepare_command (xe, group_id, command_code, + cmd->data_in, cmd->data_in_len, + &mbox_cmd, &cmd_size); +if (ret) { +xe_err (xe,"sysctrl: Failed to prepare command: %d\n" , ret); +return ret; + } + +guard (xe_pm_runtime)(xe); + +guard (mutex)(&sc->cmd_lock); + + ret =xe_sysctrl_mailbox_send_command (sc, mbox_cmd, cmd_size, + cmd->data_out, cmd->data_out_len, rdata_len, +SYSCTRL_MB_DEFAULT_TIMEOUT_MS ); +if (ret) +xe_err (xe,"sysctrl: Mailbox command failed: %d\n" , ret); + +kfree (mbox_cmd); + +return ret; +}Signature · 1 line
2.43.0
Re: [PATCH v4 3/6] drm/xe/sysctrl: Add System Controller mailbox implementation
In reply to Anoop, VijayHi Anoop
Quoted 245 lines · Vijay
From: Anoop Vijay <[email protected]>
Implement the System Controller mailbox communication layer:
- Low-level frame I/O operations
- Mailbox busy/ready state polling
- Command preparation and fragmentation
- Multi-frame send/receive with phase bit handling
- MKHI protocol header validation
- Public API for sending commands to System ControllerThis provides the complete mailbox infrastructure for communicating with System Controller firmware on Xe3p dGPU platforms.
Signed-off-by Anoop Vijay <[email protected]>---
v4: (Matt, Mike)
- Refactor MMIO access to use domain-specific accessor
- Add input validation and buffer overflow protection
- Add bounds checking for multi-frame operations
- Fix potential NULL pointer dereference--- drivers/gpu/drm/xe/xe_sysctrl_mailbox.c | 445 ++++++++++++++++++++++++ 1 file changed, 445 insertions(+)create mode 100644
drivers/gpu/drm/xe/xe_sysctrl_mailbox.cdrivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +213
-0@@ -0,0 +1,445 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <linux/bitfield.h> +#include <linux/container_of.h> +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_device_types.h" +#include "xe_mmio.h" +#include "xe_pm.h" +#include "xe_printk.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_mailbox_types.h" +#include "xe_sysctrl_types.h" + +#define MKHI_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define MKHI_HDR_COMMAND_MASK GENMASK(14, 8) +#define MKHI_HDR_IS_RESPONSE BIT(15) +#define MKHI_HDR_RESERVED_MASK GENMASK(23, 16) +#define MKHI_HDR_RESULT_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_MKHI_HDR_GROUP_ID (hdr) \ + FIELD_GET(MKHI_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_COMMAND (hdr) \ + FIELD_GET(MKHI_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (hdr) \ + FIELD_GET(MKHI_HDR_IS_RESPONSE, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_RESULT (hdr) \ + FIELD_GET(MKHI_HDR_RESULT_MASK, le32_to_cpu((hdr)->data)) + +static struct xe_device *sc_to_xe (struct xe_sysctrl *sc) +{ + return container_of(sc, struct xe_device, sc); +} + +static bool xe_sysctrl_mailbox_wait_bit_clear (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32_not (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static bool xe_sysctrl_mailbox_wait_bit_set (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32 (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static int xe_sysctrl_mailbox_write_frame (struct xe_sysctrl *sc,const void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +memcpy (val, frame, len); + +for (i =0 ; i < dw; i++) +xe_mmio_write32 (sc->mmio, regs[i], val[i]); + +return 0 ; +} + +static int xe_sysctrl_mailbox_read_frame (struct xe_sysctrl *sc,void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +for (i =0 ; i < dw; i++) + val[i] =xe_mmio_read32 (sc->mmio, regs[i]); + +memcpy (frame, val, len); + +return 0 ; +} + +static void xe_sysctrl_mailbox_clear_response (struct xe_sysctrl *sc) +{ +xe_mmio_rmw32 (sc->mmio,SYSCTRL_MB_CTRL ,SYSCTRL_MB_CTRL_RUN_BUSY_OUT ,0 ); +} + +static int xe_sysctrl_mailbox_prepare_command (struct xe_device *xe, +u8 group_id,u8 command, +const void *data_in,size_t data_in_len, +u8 **mbox_cmd,size_t *cmd_size) +{ +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t size; +u8 *buffer; + +if (data_in_len >SYSCTRL_MB_MAX_MESSAGE_SIZE -sizeof (*mkhi_hdr)) { +xe_err (xe,"sysctrl: Input data too large: %zu bytes\n" , data_in_len); +return -EINVAL ; + } + + size =sizeof (*mkhi_hdr) + data_in_len; + + buffer =kmalloc (size,GFP_KERNEL ); +if (!buffer) +return -ENOMEM ; + + mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)buffer; + mkhi_hdr->data =cpu_to_le32 (FIELD_PREP (MKHI_HDR_GROUP_ID_MASK , group_id) | +FIELD_PREP (MKHI_HDR_COMMAND_MASK , command &0x7F ) | +FIELD_PREP (MKHI_HDR_IS_RESPONSE ,0 ) | +FIELD_PREP (MKHI_HDR_RESERVED_MASK ,0 ) | +FIELD_PREP (MKHI_HDR_RESULT_MASK ,0 )); + +if (data_in && data_in_len) +memcpy (buffer +sizeof (*mkhi_hdr), data_in, data_in_len); + + *mbox_cmd = buffer; + *cmd_size = size; + +return 0 ; +} + +static int xe_sysctrl_mailbox_send_frames (struct xe_sysctrl *sc, +const u8 *mbox_cmd, +size_t cmd_size,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +u32 ctrl_reg, total_frames, frame; +size_t bytes_sent, frame_size; + + total_frames =DIV_ROUND_UP (cmd_size,SYSCTRL_MB_FRAME_SIZE ); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Mailbox busy\n" ); +return -EBUSY ; + } + + sc->phase_bit ^=1 ; + bytes_sent =0 ; + +for (frame =0 ; frame < total_frames; frame++) { + frame_size =min (cmd_size - bytes_sent, (size_t )SYSCTRL_MB_FRAME_SIZE ); + +if (xe_sysctrl_mailbox_write_frame (sc, mbox_cmd + bytes_sent, frame_size)) { +xe_err (xe,"sysctrl: Failed to write frame %u\n" , frame); + sc->phase_bit ^=1 ; +return -EIO ; + } + + ctrl_reg =SYSCTRL_MB_CTRL_RUN_BUSY | +FIELD_PREP (MKHI_FRAME_CURRENT_MASK , frame) | +FIELD_PREP (MKHI_FRAME_TOTAL_MASK , total_frames -1 ) | +FIELD_PREP (MKHI_FRAME_COMMAND_MASK ,SYSCTRL_MKHI_COMMAND ) | + (sc->phase_bit ?MKHI_FRAME_PHASE :0 ); + +xe_mmio_write32 (sc->mmio,SYSCTRL_MB_CTRL , ctrl_reg); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Frame %u acknowledgment timeout\n" , frame); + sc->phase_bit ^=1 ; +return -ETIMEDOUT ; + } + + bytes_sent += frame_size; + } + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_first_frame (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *out, +size_t out_size, +size_t frame_size, +size_t *payload_bytes) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *resp_hdr; +u32 frame_data[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)]; +size_t hdr_size =sizeof (*resp_hdr); +size_t payload_size; +int ret; + +if (frame_size < hdr_size) { +xe_err (xe,"sysctrl: Frame size %zu too small\n" , frame_size); +return -EPROTO ; + } +
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 223
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+ ret =xe_sysctrl_mailbox_read_frame (sc, frame_data, frame_size); +if (ret) +return ret; + + resp_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)frame_data; + +if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (resp_hdr) || +XE_SYSCTRL_MKHI_HDR_GROUP_ID (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_GROUP_ID (req) || +XE_SYSCTRL_MKHI_HDR_COMMAND (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_COMMAND (req)) { +xe_err (xe,"SC: Response header mismatch\n" );
Can you use either sysctrl or SC not both for consistency
Quoted 59 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +59
-0+return -EPROTO ; + } + +if (XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr) !=0 ) { +xe_err (xe,"SC: Firmware error: 0x%02lx\n" , +XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr)); +return -EIO ; + } + + payload_size = frame_size - hdr_size; + +if (payload_size > out_size) { +xe_err (xe,"sysctrl: Payload %zu bytes exceeds buffer %zu bytes\n" , + payload_size, out_size); +return -ENOSPC ; + } + +if (payload_size >0 ) +memcpy (out, (u8 *)frame_data + hdr_size, payload_size); + + *payload_bytes = payload_size; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_frame (struct xe_sysctrl *sc, +void *out,size_t frame_size, +unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +int ret; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame timeout\n" ); +return -ETIMEDOUT ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, out, frame_size); +if (ret) +return ret; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_receive_frames (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t hdr_size =sizeof (*mkhi_hdr); +u32 ctrl_reg, total_frames, frame; +size_t received =0 ; +u8 *out = data_out;
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 292
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+size_t frame_size; +int ret =0 ; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame 0 timeout\n" ); +return -ETIMEDOUT ; + } + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + total_frames =FIELD_GET (MKHI_FRAME_TOTAL_MASK , ctrl_reg) +1 ;
Why +1?
Thanks
Riana
Quoted 153 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +153
-0+ +if (total_frames ==1 ) + frame_size =min (hdr_size + data_out_len, (size_t )SYSCTRL_MB_FRAME_SIZE ); +else + frame_size =SYSCTRL_MB_FRAME_SIZE ; + + ret =xe_sysctrl_mailbox_process_first_frame (sc, req, out, data_out_len, + frame_size, &received); +if (ret) +return ret; + + out += received; + +for (frame =1 ; frame < total_frames; frame++) { +size_t remaining; + +if (received >= data_out_len) { +xe_err (xe,"sysctrl: Received %zu bytes exceeds buffer %zu bytes\n" , + received, data_out_len); +return -ENOSPC ; + } + + remaining = data_out_len - received; + frame_size =min_t (size_t, remaining,SYSCTRL_MB_FRAME_SIZE ); + + ret =xe_sysctrl_mailbox_process_frame (sc, out, frame_size, timeout_ms); +if (ret) +break ; + + received += frame_size; + out += frame_size; + } + + *rdata_len = received; + +return ret; +} + +static int xe_sysctrl_mailbox_send_command (struct xe_sysctrl *sc, +const u8 *mbox_cmd,size_t cmd_size, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t received; +int ret; + + ret =xe_sysctrl_mailbox_send_frames (sc, mbox_cmd, cmd_size, timeout_ms); +if (ret) +return ret; + +if (!data_out || !rdata_len) +return 0 ; + + mkhi_hdr = (const struct xe_sysctrl_mailbox_mkhi_msg_hdr *)mbox_cmd; + + ret =xe_sysctrl_mailbox_receive_frames (sc, mkhi_hdr, data_out, data_out_len, + &received, timeout_ms); +if (ret) +return ret; + + *rdata_len = received; + +return 0 ; +} + +/** +* xe_sysctrl_mailbox_init - Initialize System Controller mailbox interface +* @sc: System controller structure +* +* Initialize system controller mailbox interface for communication. +*/ +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc) +{ +u32 ctrl_reg; + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + sc->phase_bit = (ctrl_reg &MKHI_FRAME_PHASE ) ?1 :0 ; +} + +/** +* xe_sysctrl_send_command - Send command to System Controller via mailbox +* @xe: XE device instance +* @cmd: Pointer to xe_sysctrl_mailbox_command structure +* @rdata_len: Pointer to store actual response data size (can be NULL) +* +* Send a command to the System Controller using MKHI protocol. Handles +* command preparation, fragmentation, transmission, and response reception. +* +* Return: 0 on success, negative error code on failure +*/ +int xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len) +{ +struct xe_sysctrl *sc; +u8 group_id, command_code; +u8 *mbox_cmd =NULL ; +size_t cmd_size =0 ; +int ret =0 ; + +if (!xe) { +pr_err ("sysctrl: Invalid device handle\n" ); +return -EINVAL ; + } + +if (!xe->info.has_sysctrl) +return -ENODEV ; + + sc = &xe->sc; + +if (!cmd) { +xe_err (xe,"sysctrl: Invalid command buffer\n" ); +return -EINVAL ; + } + + group_id =XE_SYSCTRL_APP_HDR_GROUP_ID (&cmd->header); + command_code =XE_SYSCTRL_APP_HDR_COMMAND (&cmd->header); + +if (!cmd->data_in && cmd->data_in_len) { +xe_err (xe,"sysctrl: Invalid input parameters\n" ); +return -EINVAL ; + } + +if (!cmd->data_out && cmd->data_out_len) { +xe_err (xe,"sysctrl: Invalid output parameters\n" ); +return -EINVAL ; + } + +might_sleep (); + + ret =xe_sysctrl_mailbox_prepare_command (xe, group_id, command_code, + cmd->data_in, cmd->data_in_len, + &mbox_cmd, &cmd_size); +if (ret) { +xe_err (xe,"sysctrl: Failed to prepare command: %d\n" , ret); +return ret; + } + +guard (xe_pm_runtime)(xe); + +guard (mutex)(&sc->cmd_lock); + + ret =xe_sysctrl_mailbox_send_command (sc, mbox_cmd, cmd_size, + cmd->data_out, cmd->data_out_len, rdata_len, +SYSCTRL_MB_DEFAULT_TIMEOUT_MS ); +if (ret) +xe_err (xe,"sysctrl: Mailbox command failed: %d\n" , ret); + +kfree (mbox_cmd); + +return ret; +}
Re: [PATCH v4 3/6] drm/xe/sysctrl: Add System Controller mailbox implementation
In reply to Anoop, VijayQuoted 351 lines · Vijay
From: Anoop Vijay <[email protected]>
Implement the System Controller mailbox communication layer:
- Low-level frame I/O operations
- Mailbox busy/ready state polling
- Command preparation and fragmentation
- Multi-frame send/receive with phase bit handling
- MKHI protocol header validation
- Public API for sending commands to System ControllerThis provides the complete mailbox infrastructure for communicating with System Controller firmware on Xe3p dGPU platforms.
Signed-off-by Anoop Vijay <[email protected]>---
v4: (Matt, Mike)
- Refactor MMIO access to use domain-specific accessor
- Add input validation and buffer overflow protection
- Add bounds checking for multi-frame operations
- Fix potential NULL pointer dereference
---drivers/gpu/drm/xe/xe_sysctrl_mailbox.c| 445 ++++++++++++++++++++++++1 file changed, 445 insertions(+)create mode 100644
drivers/gpu/drm/xe/xe_sysctrl_mailbox.cdrivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +319
-0@@ -0,0 +1,445 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <linux/bitfield.h> +#include <linux/container_of.h> +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_device_types.h" +#include "xe_mmio.h" +#include "xe_pm.h" +#include "xe_printk.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_mailbox_types.h" +#include "xe_sysctrl_types.h" + +#define MKHI_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define MKHI_HDR_COMMAND_MASK GENMASK(14, 8) +#define MKHI_HDR_IS_RESPONSE BIT(15) +#define MKHI_HDR_RESERVED_MASK GENMASK(23, 16) +#define MKHI_HDR_RESULT_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_MKHI_HDR_GROUP_ID (hdr) \ + FIELD_GET(MKHI_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_COMMAND (hdr) \ + FIELD_GET(MKHI_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (hdr) \ + FIELD_GET(MKHI_HDR_IS_RESPONSE, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_RESULT (hdr) \ + FIELD_GET(MKHI_HDR_RESULT_MASK, le32_to_cpu((hdr)->data)) + +static struct xe_device *sc_to_xe (struct xe_sysctrl *sc) +{ + return container_of(sc, struct xe_device, sc); +} + +static bool xe_sysctrl_mailbox_wait_bit_clear (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32_not (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static bool xe_sysctrl_mailbox_wait_bit_set (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32 (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static int xe_sysctrl_mailbox_write_frame (struct xe_sysctrl *sc,const void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +memcpy (val, frame, len); + +for (i =0 ; i < dw; i++) +xe_mmio_write32 (sc->mmio, regs[i], val[i]); + +return 0 ; +} + +static int xe_sysctrl_mailbox_read_frame (struct xe_sysctrl *sc,void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +for (i =0 ; i < dw; i++) + val[i] =xe_mmio_read32 (sc->mmio, regs[i]); + +memcpy (frame, val, len); + +return 0 ; +} + +static void xe_sysctrl_mailbox_clear_response (struct xe_sysctrl *sc) +{ +xe_mmio_rmw32 (sc->mmio,SYSCTRL_MB_CTRL ,SYSCTRL_MB_CTRL_RUN_BUSY_OUT ,0 ); +} + +static int xe_sysctrl_mailbox_prepare_command (struct xe_device *xe, +u8 group_id,u8 command, +const void *data_in,size_t data_in_len, +u8 **mbox_cmd,size_t *cmd_size) +{ +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t size; +u8 *buffer; + +if (data_in_len >SYSCTRL_MB_MAX_MESSAGE_SIZE -sizeof (*mkhi_hdr)) { +xe_err (xe,"sysctrl: Input data too large: %zu bytes\n" , data_in_len); +return -EINVAL ; + } + + size =sizeof (*mkhi_hdr) + data_in_len; + + buffer =kmalloc (size,GFP_KERNEL ); +if (!buffer) +return -ENOMEM ; + + mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)buffer; + mkhi_hdr->data =cpu_to_le32 (FIELD_PREP (MKHI_HDR_GROUP_ID_MASK , group_id) | +FIELD_PREP (MKHI_HDR_COMMAND_MASK , command &0x7F ) | +FIELD_PREP (MKHI_HDR_IS_RESPONSE ,0 ) | +FIELD_PREP (MKHI_HDR_RESERVED_MASK ,0 ) | +FIELD_PREP (MKHI_HDR_RESULT_MASK ,0 )); + +if (data_in && data_in_len) +memcpy (buffer +sizeof (*mkhi_hdr), data_in, data_in_len); + + *mbox_cmd = buffer; + *cmd_size = size; + +return 0 ; +} + +static int xe_sysctrl_mailbox_send_frames (struct xe_sysctrl *sc, +const u8 *mbox_cmd, +size_t cmd_size,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +u32 ctrl_reg, total_frames, frame; +size_t bytes_sent, frame_size; + + total_frames =DIV_ROUND_UP (cmd_size,SYSCTRL_MB_FRAME_SIZE ); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Mailbox busy\n" ); +return -EBUSY ; + } + + sc->phase_bit ^=1 ; + bytes_sent =0 ; + +for (frame =0 ; frame < total_frames; frame++) { + frame_size =min (cmd_size - bytes_sent, (size_t )SYSCTRL_MB_FRAME_SIZE ); + +if (xe_sysctrl_mailbox_write_frame (sc, mbox_cmd + bytes_sent, frame_size)) { +xe_err (xe,"sysctrl: Failed to write frame %u\n" , frame); + sc->phase_bit ^=1 ; +return -EIO ; + } + + ctrl_reg =SYSCTRL_MB_CTRL_RUN_BUSY | +FIELD_PREP (MKHI_FRAME_CURRENT_MASK , frame) | +FIELD_PREP (MKHI_FRAME_TOTAL_MASK , total_frames -1 ) | +FIELD_PREP (MKHI_FRAME_COMMAND_MASK ,SYSCTRL_MKHI_COMMAND ) | + (sc->phase_bit ?MKHI_FRAME_PHASE :0 ); + +xe_mmio_write32 (sc->mmio,SYSCTRL_MB_CTRL , ctrl_reg); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Frame %u acknowledgment timeout\n" , frame); + sc->phase_bit ^=1 ; +return -ETIMEDOUT ; + } + + bytes_sent += frame_size; + } + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_first_frame (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *out, +size_t out_size, +size_t frame_size, +size_t *payload_bytes) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *resp_hdr; +u32 frame_data[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)]; +size_t hdr_size =sizeof (*resp_hdr); +size_t payload_size; +int ret; + +if (frame_size < hdr_size) { +xe_err (xe,"sysctrl: Frame size %zu too small\n" , frame_size); +return -EPROTO ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, frame_data, frame_size); +if (ret) +return ret; + + resp_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)frame_data; + +if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (resp_hdr) || +XE_SYSCTRL_MKHI_HDR_GROUP_ID (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_GROUP_ID (req) || +XE_SYSCTRL_MKHI_HDR_COMMAND (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_COMMAND (req)) { +xe_err (xe,"SC: Response header mismatch\n" ); +return -EPROTO ; + } + +if (XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr) !=0 ) { +xe_err (xe,"SC: Firmware error: 0x%02lx\n" , +XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr)); +return -EIO ; + } + + payload_size = frame_size - hdr_size; + +if (payload_size > out_size) { +xe_err (xe,"sysctrl: Payload %zu bytes exceeds buffer %zu bytes\n" , + payload_size, out_size); +return -ENOSPC ; + } + +if (payload_size >0 ) +memcpy (out, (u8 *)frame_data + hdr_size, payload_size); + + *payload_bytes = payload_size; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_frame (struct xe_sysctrl *sc, +void *out,size_t frame_size, +unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +int ret; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame timeout\n" ); +return -ETIMEDOUT ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, out, frame_size); +if (ret) +return ret; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_receive_frames (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t hdr_size =sizeof (*mkhi_hdr); +u32 ctrl_reg, total_frames, frame; +size_t received =0 ; +u8 *out = data_out; +size_t frame_size; +int ret =0 ; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame 0 timeout\n" ); +return -ETIMEDOUT ; + } + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + total_frames =FIELD_GET (MKHI_FRAME_TOTAL_MASK , ctrl_reg) +1 ; + +if (total_frames ==1 ) + frame_size =min (hdr_size + data_out_len, (size_t )SYSCTRL_MB_FRAME_SIZE ); +else + frame_size =SYSCTRL_MB_FRAME_SIZE ; + + ret =xe_sysctrl_mailbox_process_first_frame (sc, req, out, data_out_len, + frame_size, &received); +if (ret) +return ret; + + out += received; + +for (frame =1 ; frame < total_frames; frame++) { +size_t remaining; + +if (received >= data_out_len) { +xe_err (xe,"sysctrl: Received %zu bytes exceeds buffer %zu bytes\n" , + received, data_out_len); +return -ENOSPC ; + } + + remaining = data_out_len - received; + frame_size =min_t (size_t, remaining,SYSCTRL_MB_FRAME_SIZE ); + + ret =xe_sysctrl_mailbox_process_frame (sc, out, frame_size, timeout_ms); +if (ret)
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 329
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+break ; + + received += frame_size; + out += frame_size; + } + + *rdata_len = received; + +return ret; +}
[optional]
I lost track of a comment to simplify the receive handling to make it more readable. I have pasted a draft version here. Check it out if possible, if not, you could refactor in future.
This reads the ctrl_reg each time and relies on the current_frame value to stop reading further frames. Note that this will drain the entire message from FW and then validate the header. Also note that this reads the message to a temporary buffer and then copies it over to the user buffer, so it will not copy partial message to user buffer if the header checks fail.
static int xe_sysctrl_mailbox_process_frame(struct xe_sysctrl *sc, void *out,
size_t frame_size, bool *done)
{
u32 curr_frame, total_frames, ctrl_reg;
struct xe_device *xe = sc_to_xe(sc);
int ret;
if (!xe_sysctrl_mailbox_wait_bit_set(sc, SYSCTRL_MB_CTRL_RUN_BUSY_OUT)) {
xe_err(xe, "sysctrl: Response frame timeout\n");
return -ETIMEDOUT;
}
ctrl_reg = xe_mmio_read32(sc->mmio, SYSCTRL_MB_CTRL);
total_frames = FIELD_GET(MKHI_FRAME_TOTAL_MASK, ctrl_reg);
curr_frame = FIELD_GET(MKHI_FRAME_CURRENT_MASK, ctrl_reg);
ret = xe_sysctrl_mailbox_read_frame(sc, out, frame_size);
if (ret)
return ret;
xe_sysctrl_mailbox_clear_response(sc);
if (curr_frame == total_frames)
*done = true;
return 0;
}static int xe_sysctrl_mailbox_receive_frames(struct xe_sysctrl *sc,
const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, void *data_out, size_t data_out_len, size_t *rdata_len, unsigned int timeout_ms)
{
struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr;
struct xe_device *xe = sc_to_xe(sc);
size_t frame_size, remain;
u32 curr_frame, total_frames, ctrl_reg;
u8 *buffer, *out;
int ret = 0;
bool done = false;
remain = sizeof(*mkhi_hdr) + data_out_len;
buffer = kzalloc(remain, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
out = buffer;
while (!done && remain) {
frame_size = min(remain, (size_t)SYSCTRL_MB_FRAME_SIZE);
ret = xe_sysctrl_mailbox_process_frame(sc, out, frame_size,
timeout_ms, &done);
if (ret)
return ret;
remain -= frame_size;
out += frame_size;
}
mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *) buffer;
if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE(mkhi_hdr) ||
XE_SYSCTRL_MKHI_HDR_GROUP_ID(mkhi_hdr) != XE_SYSCTRL_MKHI_HDR_GROUP_ID(req) ||
XE_SYSCTRL_MKHI_HDR_COMMAND(mkhi_hdr) != XE_SYSCTRL_MKHI_HDR_COMMAND(req)) {
xe_err(xe, "SC: Response header mismatch\n");
return -EPROTO;
}
if (XE_SYSCTRL_MKHI_HDR_RESULT(mkhi_hdr) != 0) {
xe_err(xe, "SC: Firmware error: 0x%02lx\n", XE_SYSCTRL_MKHI_HDR_RESULT(mkhi_hdr));
return -EIO;
}
memcpy(data_out, mkhi_hdr + 1, data_out_len);
*rdata_len = received;
return ret;
}Quoted 99 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +99
-0+ +static int xe_sysctrl_mailbox_send_command (struct xe_sysctrl *sc, +const u8 *mbox_cmd,size_t cmd_size, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) +{ +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t received; +int ret; + + ret =xe_sysctrl_mailbox_send_frames (sc, mbox_cmd, cmd_size, timeout_ms); +if (ret) +return ret; + +if (!data_out || !rdata_len) +return 0 ; + + mkhi_hdr = (const struct xe_sysctrl_mailbox_mkhi_msg_hdr *)mbox_cmd; + + ret =xe_sysctrl_mailbox_receive_frames (sc, mkhi_hdr, data_out, data_out_len, + &received, timeout_ms); +if (ret) +return ret; + + *rdata_len = received; + +return 0 ; +} + +/** +* xe_sysctrl_mailbox_init - Initialize System Controller mailbox interface +* @sc: System controller structure +* +* Initialize system controller mailbox interface for communication. +*/ +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc) +{ +u32 ctrl_reg; + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + sc->phase_bit = (ctrl_reg &MKHI_FRAME_PHASE ) ?1 :0 ; +} + +/** +* xe_sysctrl_send_command - Send command to System Controller via mailbox +* @xe: XE device instance +* @cmd: Pointer to xe_sysctrl_mailbox_command structure +* @rdata_len: Pointer to store actual response data size (can be NULL) +* +* Send a command to the System Controller using MKHI protocol. Handles +* command preparation, fragmentation, transmission, and response reception. +* +* Return: 0 on success, negative error code on failure +*/ +int xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len) +{ +struct xe_sysctrl *sc; +u8 group_id, command_code; +u8 *mbox_cmd =NULL ; +size_t cmd_size =0 ; +int ret =0 ; + +if (!xe) { +pr_err ("sysctrl: Invalid device handle\n" ); +return -EINVAL ; + } + +if (!xe->info.has_sysctrl) +return -ENODEV ; + + sc = &xe->sc; + +if (!cmd) { +xe_err (xe,"sysctrl: Invalid command buffer\n" ); +return -EINVAL ; + } + + group_id =XE_SYSCTRL_APP_HDR_GROUP_ID (&cmd->header); + command_code =XE_SYSCTRL_APP_HDR_COMMAND (&cmd->header); + +if (!cmd->data_in && cmd->data_in_len) { +xe_err (xe,"sysctrl: Invalid input parameters\n" ); +return -EINVAL ; + } + +if (!cmd->data_out && cmd->data_out_len) { +xe_err (xe,"sysctrl: Invalid output parameters\n" ); +return -EINVAL ; + } + +might_sleep (); + + ret =xe_sysctrl_mailbox_prepare_command (xe, group_id, command_code, + cmd->data_in, cmd->data_in_len, + &mbox_cmd, &cmd_size); +if (ret) { +xe_err (xe,"sysctrl: Failed to prepare command: %d\n" , ret);
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 438
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+return ret; + } + +guard (xe_pm_runtime)(xe); + +guard (mutex)(&sc->cmd_lock); + + ret =xe_sysctrl_mailbox_send_command (sc, mbox_cmd, cmd_size, + cmd->data_out, cmd->data_out_len, rdata_len, +SYSCTRL_MB_DEFAULT_TIMEOUT_MS );
Since the timeout is a macro you are defining locally, can you move this to the innermost function that uses it. That way you will not need to pass it to each function call in the stack.
Thanks,
Umesh
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +7
-0+if (ret) +xe_err (xe,"sysctrl: Mailbox command failed: %d\n" , ret); + +kfree (mbox_cmd); + +return ret; +}Signature · 1 line
2.43.0
RE: [PATCH v4 3/6] drm/xe/sysctrl: Add System Controller mailbox implementation
Hi Anoop,
-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Umesh Nerlige Ramappa
Sent: Thursday, January 22, 2026 4:39 PM
To: Vijay, Anoop C <[email protected]>
Cc: [email protected]; Nilawar, Badal <[email protected]>; Vivi, Rodrigo <[email protected]>; Iddamsetty, Aravind <[email protected]>; Tauro, Riana <[email protected]>; Gupta, Anshuman <[email protected]>; Roper, Matthew D <[email protected]>; Ruhl, Michael J <[email protected]>; Luse, Paul E <[email protected]>; V, Mohamed Mansoor <[email protected]>; Nasim, Kam <[email protected]>
Subject: Re: [PATCH v4 3/6] drm/xe/sysctrl: Add System Controller mailbox implementation
Quoted 79 lines · Vijay
From: Anoop Vijay <[email protected]>
Implement the System Controller mailbox communication layer:
- Low-level frame I/O operations
- Mailbox busy/ready state polling
- Command preparation and fragmentation
- Multi-frame send/receive with phase bit handling
- MKHI protocol header validation
- Public API for sending commands to System ControllerThis provides the complete mailbox infrastructure for communicating with System Controller firmware on Xe3p dGPU platforms.
Signed-off-by Anoop Vijay <[email protected]>---
v4: (Matt, Mike)
- Refactor MMIO access to use domain-specific accessor
- Add input validation and buffer overflow protection
- Add bounds checking for multi-frame operations
- Fix potential NULL pointer dereference
---drivers/gpu/drm/xe/xe_sysctrl_mailbox.c| 445 ++++++++++++++++++++++++1 file changed, 445 insertions(+)create mode 100644
drivers/gpu/drm/xe/xe_sysctrl_mailbox.cdrivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +47
-0@@ -0,0 +1,445 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <linux/bitfield.h> +#include <linux/container_of.h> +#include <linux/errno.h> +#include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_device_types.h" +#include "xe_mmio.h" +#include "xe_pm.h" +#include "xe_printk.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_mailbox_types.h" +#include "xe_sysctrl_types.h" + +#define MKHI_HDR_GROUP_ID_MASK GENMASK(7, 0) +#define MKHI_HDR_COMMAND_MASK GENMASK(14, 8) +#define MKHI_HDR_IS_RESPONSE BIT(15) +#define MKHI_HDR_RESERVED_MASK GENMASK(23, 16) +#define MKHI_HDR_RESULT_MASK GENMASK(31, 24) + +#define XE_SYSCTRL_MKHI_HDR_GROUP_ID (hdr) \ + FIELD_GET(MKHI_HDR_GROUP_ID_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_COMMAND (hdr) \ + FIELD_GET(MKHI_HDR_COMMAND_MASK, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (hdr) \ + FIELD_GET(MKHI_HDR_IS_RESPONSE, le32_to_cpu((hdr)->data)) + +#define XE_SYSCTRL_MKHI_HDR_RESULT (hdr) \ + FIELD_GET(MKHI_HDR_RESULT_MASK, le32_to_cpu((hdr)->data)) + +static struct xe_device *sc_to_xe (struct xe_sysctrl *sc) { + return container_of(sc, struct xe_device, sc); } + +static bool xe_sysctrl_mailbox_wait_bit_clear(struct xe_sysctrl *sc, u32 bit_mask,
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 57
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+ unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32_not (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} +
We can use interrupt-driven support and fallback to polling for non-IRQ scenario, right?
Thanks,
-/Mallesh
Quoted 257 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +257
-0+static bool xe_sysctrl_mailbox_wait_bit_set (struct xe_sysctrl *sc,u32 bit_mask, +unsigned int timeout_ms) +{ +int ret; + + ret =xe_mmio_wait32 (sc->mmio,SYSCTRL_MB_CTRL , bit_mask, bit_mask, + timeout_ms *1000 ,NULL , false); + +return ret ==0 ; +} + +static int xe_sysctrl_mailbox_write_frame (struct xe_sysctrl *sc,const void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +memcpy (val, frame, len); + +for (i =0 ; i < dw; i++) +xe_mmio_write32 (sc->mmio, regs[i], val[i]); + +return 0 ; +} + +static int xe_sysctrl_mailbox_read_frame (struct xe_sysctrl *sc,void *frame, +size_t len) +{ +static const struct xe_reg regs[] = { +SYSCTRL_MB_DATA0 ,SYSCTRL_MB_DATA1 ,SYSCTRL_MB_DATA2 ,SYSCTRL_MB_DATA3 + }; +u32 val[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)] = {0 }; +u32 dw =DIV_ROUND_UP (len,sizeof (u32)); +u32 i; + +for (i =0 ; i < dw; i++) + val[i] =xe_mmio_read32 (sc->mmio, regs[i]); + +memcpy (frame, val, len); + +return 0 ; +} + +static void xe_sysctrl_mailbox_clear_response (struct xe_sysctrl *sc) { +xe_mmio_rmw32 (sc->mmio,SYSCTRL_MB_CTRL , +SYSCTRL_MB_CTRL_RUN_BUSY_OUT ,0 ); } + +static int xe_sysctrl_mailbox_prepare_command (struct xe_device *xe, +u8 group_id,u8 command, +const void *data_in,size_t data_in_len, +u8 **mbox_cmd,size_t *cmd_size) { +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t size; +u8 *buffer; + +if (data_in_len >SYSCTRL_MB_MAX_MESSAGE_SIZE -sizeof (*mkhi_hdr)) { +xe_err (xe,"sysctrl: Input data too large: %zu bytes\n" , data_in_len); +return -EINVAL ; + } + + size =sizeof (*mkhi_hdr) + data_in_len; + + buffer =kmalloc (size,GFP_KERNEL ); +if (!buffer) +return -ENOMEM ; + + mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)buffer; + mkhi_hdr->data =cpu_to_le32 (FIELD_PREP (MKHI_HDR_GROUP_ID_MASK , group_id) | +FIELD_PREP (MKHI_HDR_COMMAND_MASK , command &0x7F ) | +FIELD_PREP (MKHI_HDR_IS_RESPONSE ,0 ) | +FIELD_PREP (MKHI_HDR_RESERVED_MASK ,0 ) | +FIELD_PREP (MKHI_HDR_RESULT_MASK ,0 )); + +if (data_in && data_in_len) +memcpy (buffer +sizeof (*mkhi_hdr), data_in, data_in_len); + + *mbox_cmd = buffer; + *cmd_size = size; + +return 0 ; +} + +static int xe_sysctrl_mailbox_send_frames (struct xe_sysctrl *sc, +const u8 *mbox_cmd, +size_t cmd_size,unsigned int timeout_ms) { +struct xe_device *xe =sc_to_xe (sc); +u32 ctrl_reg, total_frames, frame; +size_t bytes_sent, frame_size; + + total_frames =DIV_ROUND_UP (cmd_size,SYSCTRL_MB_FRAME_SIZE ); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Mailbox busy\n" ); +return -EBUSY ; + } + + sc->phase_bit ^=1 ; + bytes_sent =0 ; + +for (frame =0 ; frame < total_frames; frame++) { + frame_size =min (cmd_size - bytes_sent, +(size_t )SYSCTRL_MB_FRAME_SIZE ); + +if (xe_sysctrl_mailbox_write_frame (sc, mbox_cmd + bytes_sent, frame_size)) { +xe_err (xe,"sysctrl: Failed to write frame %u\n" , frame); + sc->phase_bit ^=1 ; +return -EIO ; + } + + ctrl_reg =SYSCTRL_MB_CTRL_RUN_BUSY | +FIELD_PREP (MKHI_FRAME_CURRENT_MASK , frame) | +FIELD_PREP (MKHI_FRAME_TOTAL_MASK , total_frames -1 ) | +FIELD_PREP (MKHI_FRAME_COMMAND_MASK ,SYSCTRL_MKHI_COMMAND ) | + (sc->phase_bit ?MKHI_FRAME_PHASE :0 ); + +xe_mmio_write32 (sc->mmio,SYSCTRL_MB_CTRL , ctrl_reg); + +if (!xe_sysctrl_mailbox_wait_bit_clear (sc,SYSCTRL_MB_CTRL_RUN_BUSY , timeout_ms)) { +xe_err (xe,"sysctrl: Frame %u acknowledgment timeout\n" , frame); + sc->phase_bit ^=1 ; +return -ETIMEDOUT ; + } + + bytes_sent += frame_size; + } + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_first_frame (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *out, +size_t out_size, +size_t frame_size, +size_t *payload_bytes) +{ +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *resp_hdr; +u32 frame_data[SYSCTRL_MB_FRAME_SIZE /sizeof (u32)]; +size_t hdr_size =sizeof (*resp_hdr); +size_t payload_size; +int ret; + +if (frame_size < hdr_size) { +xe_err (xe,"sysctrl: Frame size %zu too small\n" , frame_size); +return -EPROTO ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, frame_data, frame_size); +if (ret) +return ret; + + resp_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *)frame_data; + +if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE (resp_hdr) || +XE_SYSCTRL_MKHI_HDR_GROUP_ID (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_GROUP_ID (req) || +XE_SYSCTRL_MKHI_HDR_COMMAND (resp_hdr) !=XE_SYSCTRL_MKHI_HDR_COMMAND (req)) { +xe_err (xe,"SC: Response header mismatch\n" ); +return -EPROTO ; + } + +if (XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr) !=0 ) { +xe_err (xe,"SC: Firmware error: 0x%02lx\n" , +XE_SYSCTRL_MKHI_HDR_RESULT (resp_hdr)); +return -EIO ; + } + + payload_size = frame_size - hdr_size; + +if (payload_size > out_size) { +xe_err (xe,"sysctrl: Payload %zu bytes exceeds buffer %zu bytes\n" , + payload_size, out_size); +return -ENOSPC ; + } + +if (payload_size >0 ) +memcpy (out, (u8 *)frame_data + hdr_size, payload_size); + + *payload_bytes = payload_size; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_process_frame (struct xe_sysctrl *sc, +void *out,size_t frame_size, +unsigned int timeout_ms) +{ +struct xe_device *xe =sc_to_xe (sc); +int ret; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame timeout\n" ); +return -ETIMEDOUT ; + } + + ret =xe_sysctrl_mailbox_read_frame (sc, out, frame_size); +if (ret) +return ret; + +xe_sysctrl_mailbox_clear_response (sc); + +return 0 ; +} + +static int xe_sysctrl_mailbox_receive_frames (struct xe_sysctrl *sc, +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) { +struct xe_device *xe =sc_to_xe (sc); +struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t hdr_size =sizeof (*mkhi_hdr); +u32 ctrl_reg, total_frames, frame; +size_t received =0 ; +u8 *out = data_out; +size_t frame_size; +int ret =0 ; + +if (!xe_sysctrl_mailbox_wait_bit_set (sc,SYSCTRL_MB_CTRL_RUN_BUSY_OUT , timeout_ms)) { +xe_err (xe,"sysctrl: Response frame 0 timeout\n" ); +return -ETIMEDOUT ; + } + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + total_frames =FIELD_GET (MKHI_FRAME_TOTAL_MASK , ctrl_reg) +1 ; + +if (total_frames ==1 ) + frame_size =min (hdr_size + data_out_len, (size_t )SYSCTRL_MB_FRAME_SIZE ); +else + frame_size =SYSCTRL_MB_FRAME_SIZE ; + + ret =xe_sysctrl_mailbox_process_first_frame (sc, req, out, data_out_len, + frame_size, &received); +if (ret) +return ret; + + out += received; + +for (frame =1 ; frame < total_frames; frame++) { +size_t remaining; + +if (received >= data_out_len) { +xe_err (xe,"sysctrl: Received %zu bytes exceeds buffer %zu bytes\n" , + received, data_out_len); +return -ENOSPC ; + } + + remaining = data_out_len - received; + frame_size =min_t (size_t, remaining,SYSCTRL_MB_FRAME_SIZE ); + + ret =xe_sysctrl_mailbox_process_frame (sc, out, frame_size, timeout_ms); +if (ret)
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 329
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+break ; + + received += frame_size; + out += frame_size; + } + + *rdata_len = received; + +return ret; +}
[optional]
I lost track of a comment to simplify the receive handling to make it more readable. I have pasted a draft version here. Check it out if possible, if not, you could refactor in future.
This reads the ctrl_reg each time and relies on the current_frame value to stop reading further frames. Note that this will drain the entire message from FW and then validate the header. Also note that this reads the message to a temporary buffer and then copies it over to the user buffer, so it will not copy partial message to user buffer if the header checks fail.
static int xe_sysctrl_mailbox_process_frame(struct xe_sysctrl *sc, void *out,
size_t frame_size, bool *done) {
u32 curr_frame, total_frames, ctrl_reg;
struct xe_device *xe = sc_to_xe(sc);
int ret;
if (!xe_sysctrl_mailbox_wait_bit_set(sc, SYSCTRL_MB_CTRL_RUN_BUSY_OUT)) {
xe_err(xe, "sysctrl: Response frame timeout\n");
return -ETIMEDOUT;
}
ctrl_reg = xe_mmio_read32(sc->mmio, SYSCTRL_MB_CTRL);
total_frames = FIELD_GET(MKHI_FRAME_TOTAL_MASK, ctrl_reg);
curr_frame = FIELD_GET(MKHI_FRAME_CURRENT_MASK, ctrl_reg);
ret = xe_sysctrl_mailbox_read_frame(sc, out, frame_size);
if (ret)
return ret;
xe_sysctrl_mailbox_clear_response(sc);
if (curr_frame == total_frames)
*done = true;
return 0;
}static int xe_sysctrl_mailbox_receive_frames(struct xe_sysctrl *sc,
const struct xe_sysctrl_mailbox_mkhi_msg_hdr *req,
void *data_out, size_t data_out_len,
size_t *rdata_len, unsigned int timeout_ms) {
struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr;
struct xe_device *xe = sc_to_xe(sc);
size_t frame_size, remain;
u32 curr_frame, total_frames, ctrl_reg;
u8 *buffer, *out;
int ret = 0;
bool done = false;
remain = sizeof(*mkhi_hdr) + data_out_len;
buffer = kzalloc(remain, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
out = buffer;
while (!done && remain) {
frame_size = min(remain, (size_t)SYSCTRL_MB_FRAME_SIZE);
ret = xe_sysctrl_mailbox_process_frame(sc, out, frame_size,
timeout_ms, &done);
if (ret)
return ret;
remain -= frame_size;
out += frame_size;
}
mkhi_hdr = (struct xe_sysctrl_mailbox_mkhi_msg_hdr *) buffer;
if (!XE_SYSCTRL_MKHI_HDR_IS_RESPONSE(mkhi_hdr) ||
XE_SYSCTRL_MKHI_HDR_GROUP_ID(mkhi_hdr) != XE_SYSCTRL_MKHI_HDR_GROUP_ID(req) ||
XE_SYSCTRL_MKHI_HDR_COMMAND(mkhi_hdr) != XE_SYSCTRL_MKHI_HDR_COMMAND(req)) {
xe_err(xe, "SC: Response header mismatch\n");
return -EPROTO;
}
if (XE_SYSCTRL_MKHI_HDR_RESULT(mkhi_hdr) != 0) {
xe_err(xe, "SC: Firmware error: 0x%02lx\n", XE_SYSCTRL_MKHI_HDR_RESULT(mkhi_hdr));
return -EIO;
}
memcpy(data_out, mkhi_hdr + 1, data_out_len);
*rdata_len = received;
return ret;
}Quoted 99 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +99
-0+ +static int xe_sysctrl_mailbox_send_command (struct xe_sysctrl *sc, +const u8 *mbox_cmd,size_t cmd_size, +void *data_out,size_t data_out_len, +size_t *rdata_len,unsigned int timeout_ms) { +const struct xe_sysctrl_mailbox_mkhi_msg_hdr *mkhi_hdr; +size_t received; +int ret; + + ret =xe_sysctrl_mailbox_send_frames (sc, mbox_cmd, cmd_size, timeout_ms); +if (ret) +return ret; + +if (!data_out || !rdata_len) +return 0 ; + + mkhi_hdr = (const struct xe_sysctrl_mailbox_mkhi_msg_hdr *)mbox_cmd; + + ret =xe_sysctrl_mailbox_receive_frames (sc, mkhi_hdr, data_out, data_out_len, + &received, timeout_ms); +if (ret) +return ret; + + *rdata_len = received; + +return 0 ; +} + +/** +* xe_sysctrl_mailbox_init - Initialize System Controller mailbox +interface +* @sc: System controller structure +* +* Initialize system controller mailbox interface for communication. +*/ +void xe_sysctrl_mailbox_init (struct xe_sysctrl *sc) { +u32 ctrl_reg; + + ctrl_reg =xe_mmio_read32 (sc->mmio,SYSCTRL_MB_CTRL ); + sc->phase_bit = (ctrl_reg &MKHI_FRAME_PHASE ) ?1 :0 ; } + +/** +* xe_sysctrl_send_command - Send command to System Controller via +mailbox +* @xe: XE device instance +* @cmd: Pointer to xe_sysctrl_mailbox_command structure +* @rdata_len: Pointer to store actual response data size (can be +NULL) +* +* Send a command to the System Controller using MKHI protocol. +Handles +* command preparation, fragmentation, transmission, and response reception. +* +* Return: 0 on success, negative error code on failure */ int +xe_sysctrl_send_command (struct xe_device *xe, +struct xe_sysctrl_mailbox_command *cmd, +size_t *rdata_len) +{ +struct xe_sysctrl *sc; +u8 group_id, command_code; +u8 *mbox_cmd =NULL ; +size_t cmd_size =0 ; +int ret =0 ; + +if (!xe) { +pr_err ("sysctrl: Invalid device handle\n" ); +return -EINVAL ; + } + +if (!xe->info.has_sysctrl) +return -ENODEV ; + + sc = &xe->sc; + +if (!cmd) { +xe_err (xe,"sysctrl: Invalid command buffer\n" ); +return -EINVAL ; + } + + group_id =XE_SYSCTRL_APP_HDR_GROUP_ID (&cmd->header); + command_code =XE_SYSCTRL_APP_HDR_COMMAND (&cmd->header); + +if (!cmd->data_in && cmd->data_in_len) { +xe_err (xe,"sysctrl: Invalid input parameters\n" ); +return -EINVAL ; + } + +if (!cmd->data_out && cmd->data_out_len) { +xe_err (xe,"sysctrl: Invalid output parameters\n" ); +return -EINVAL ; + } + +might_sleep (); + + ret =xe_sysctrl_mailbox_prepare_command (xe, group_id, command_code, + cmd->data_in, cmd->data_in_len, + &mbox_cmd, &cmd_size); +if (ret) { +xe_err (xe,"sysctrl: Failed to prepare command: %d\n" , ret);
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c line 438
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +10
-0+return ret; + } + +guard (xe_pm_runtime)(xe); + +guard (mutex)(&sc->cmd_lock); + + ret =xe_sysctrl_mailbox_send_command (sc, mbox_cmd, cmd_size, + cmd->data_out, cmd->data_out_len, rdata_len, +SYSCTRL_MB_DEFAULT_TIMEOUT_MS );
Since the timeout is a macro you are defining locally, can you move this to the innermost function that uses it. That way you will not need to pass it to each function call in the stack.
Thanks,
Umesh
Quoted 10 lines
drivers/gpu/drm/xe/xe_sysctrl_mailbox.c new file +7
-1+if (ret) +xe_err (xe,"sysctrl: Mailbox command failed: %d\n" , ret); + +kfree (mbox_cmd); + +return ret; +} --2.43.0
PATCH v4 4/6 drm/xe/sysctrl: Add System Controller initialization
In reply to Anoop, VijayFrom: Anoop Vijay <[email protected]>
Add top-level System Controller initialization and cleanup:
- SOC remapper region configuration
- Mailbox initialization
- Resource cleanup on driver unload
---
v4: (Matt)
- Add MMIO allocation and initialization
--- drivers/gpu/drm/xe/xe_sysctrl.c | 80 +++++++++++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_sysctrl.h | 13 ++++++ 2 files changed, 93 insertions(+) create mode 100644 drivers/gpu/drm/xe/xe_sysctrl.c create mode 100644 drivers/gpu/drm/xe/xe_sysctrl.h
drivers/gpu/drm/xe/xe_sysctrl.c new file +80 -0
@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: MIT +/* +* Copyright © 2026 Intel Corporation +*/ + +#include <drm/drm_managed.h> +#include <linux/device.h> +#include <linux/mutex.h> + +#include "regs/xe_sysctrl_regs.h" +#include "xe_device.h" +#include "xe_mmio.h" +#include "xe_printk.h" +#include "xe_soc_remapper.h" +#include "xe_sysctrl.h" +#include "xe_sysctrl_mailbox.h" +#include "xe_sysctrl_types.h" + +/** +* DOC: System Controller (sysctrl) +* +* The System Controller (sysctrl) is an embedded microcontroller in Intel GPUs +* responsible for managing various low-level platform functions. Communication +* between the driver and the System Controller occurs via a mailbox interface, +* enabling the exchange of commands and responses. +* +* This module provides initialization routines and helper functions to interact +* with the System Controller through the mailbox. +*/ + +static void xe_sysctrl_fini (void *arg) +{ +struct xe_device *xe = arg; + + xe->soc_remapper.set_sysctrl_region (xe,0 ); +} + +/** +* xe_sysctrl_init - Initialize System Controller subsystem +* @xe: xe device instance +* +* Entry point for System Controller initialization, called from xe_device_probe. +* This function checks platform support and initializes the system controller. +* +* Return: 0 on success, error code on failure +*/ +int xe_sysctrl_init (struct xe_device *xe) +{ +struct xe_tile *tile =xe_device_get_root_tile (xe); +struct xe_sysctrl *sc = &xe->sc; +int ret; + +if (!xe->info.has_sysctrl) +return 0 ; + +if (!xe->soc_remapper.set_sysctrl_region) +return -ENODEV ; + + xe->soc_remapper.set_sysctrl_region (xe,SYSCTRL_MAILBOX_INDEX ); + + ret =devm_add_action_or_reset (xe->drm.dev, xe_sysctrl_fini, xe); +if (ret) +return ret; + + sc->mmio =devm_kzalloc (xe->drm.dev,sizeof (*sc->mmio),GFP_KERNEL ); +if (!sc->mmio) +return -ENOMEM ; + +xe_mmio_init (sc->mmio, tile, tile->mmio.regs, tile->mmio.regs_size); + sc->mmio->adj_offset =SYSCTRL_BASE ; + sc->mmio->adj_limit =U32_MAX ; + + ret =drmm_mutex_init (&xe->drm, &sc->cmd_lock); +if (ret) +return ret; + +xe_sysctrl_mailbox_init (sc); + +return 0 ; +}
drivers/gpu/drm/xe/xe_sysctrl.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: MIT */ +/* +* Copyright © 2026 Intel Corporation +*/ + +#ifndef _XE_SYSCTRL_H_ +#define _XE_SYSCTRL_H_ + +struct xe_device ; + +int xe_sysctrl_init (struct xe_device *xe); + +#endif /* _XE_SYSCTRL_H_ */
Signature · 1 line
2.43.0
PATCH v4 5/6 drm/xe/sysctrl: Integrate System Controller into device
In reply to Anoop, VijayFrom: Anoop Vijay <[email protected]>
Add System Controller to the xe_device structure and wire up initialization during device probe.
--- drivers/gpu/drm/xe/xe_device.c | 5 +++++ drivers/gpu/drm/xe/xe_device_types.h | 6 ++++++ 2 files changed, 11 insertions(+)
drivers/gpu/drm/xe/xe_device.c +5 -0
@@ -66,6 +66,7 @@#include "xe_survivability_mode.h" #include "xe_sriov.h" #include "xe_svm.h" +#include "xe_sysctrl.h" #include "xe_tile.h" #include "xe_ttm_stolen_mgr.h" #include "xe_ttm_sys_mgr.h"
@@ -1027,6 +1028,10 @@ int xe_device_probe(struct xe_device *xe)if (err) goto err_unregister_display; + err =xe_sysctrl_init (xe); +if (err) + goto err_unregister_display; + err =xe_device_sysfs_init (xe);if (err) goto err_unregister_display;
drivers/gpu/drm/xe/xe_device_types.h +6 -0
@@ -29,6 +29,7 @@#include "xe_sriov_vf_ccs_types.h" #include "xe_step_types.h" #include "xe_survivability_mode_types.h" +#include "xe_sysctrl_types.h" #include "xe_tile_sriov_vf_types.h" #include "xe_validation.h"
@@ -340,6 +341,8 @@ struct xe_device { u8 has_soc_remapper_telem:1;/** @info.has_sriov: Supports SR-IOV */ u8 has_sriov:1; +/** @info.has_sysctrl: Supports System Controller */ + u8 has_sysctrl:1;/** @info.has_usm: Device has unified shared memory support */ u8 has_usm:1;/** @info.has_64bit_timestamp: Device supports 64-bit timestamps */
@@ -606,6 +609,9 @@ struct xe_device {/** @heci_gsc: graphics security controller */ struct xe_heci_gsc heci_gsc; +/** @sc: System Controller */ +struct xe_sysctrl sc; +/** @nvm: discrete graphics non-volatile memory */ struct intel_dg_nvm_dev *nvm;
Signature · 1 line
2.43.0
PATCH v4 6/6 drm/xe/sysctrl: Enable System Controller for Xe3p
In reply to Anoop, VijayFrom: Anoop Vijay <[email protected]>
Add PCI device detection for System Controller capability and update build system to include System Controller components.
--- drivers/gpu/drm/xe/Makefile | 2 ++ drivers/gpu/drm/xe/xe_pci.c | 2 ++ drivers/gpu/drm/xe/xe_pci_types.h | 1 + 3 files changed, 5 insertions(+)
drivers/gpu/drm/xe/Makefile +2 -0
@@ -121,6 +121,8 @@ xe-y += xe_bb.o \ xe_step.o \ xe_survivability_mode.o \ xe_sync.o \ + xe_sysctrl.o \ + xe_sysctrl_mailbox.o \ xe_tile.o \ xe_tile_sysfs.o \ xe_tlb_inval.o \
drivers/gpu/drm/xe/xe_pci.c +2 -0
@@ -426,6 +426,7 @@ static const struct xe_device_desc cri_desc = { .has_soc_remapper_sysctrl = true, .has_soc_remapper_telem = true, .has_sriov = true, + .has_sysctrl = true, .max_gt_per_tile = 2, .require_force_probe = true, .va_bits = 57,
@@ -701,6 +702,7 @@ static int xe_info_init_early(struct xe_device *xe, xe->info.has_soc_remapper_telem = desc->has_soc_remapper_telem; xe->info.has_sriov =xe_configfs_primary_gt_allowed (to_pci_dev (xe->drm.dev)) && desc->has_sriov; + xe->info.has_sysctrl = desc->has_sysctrl; xe->info.has_mem_copy_instr = desc->has_mem_copy_instr; xe->info.skip_guc_pc = desc->skip_guc_pc; xe->info.skip_mtcfg = desc->skip_mtcfg;
drivers/gpu/drm/xe/xe_pci_types.h +1 -0
@@ -56,6 +56,7 @@ struct xe_device_desc { u8 has_soc_remapper_sysctrl:1; u8 has_soc_remapper_telem:1; u8 has_sriov:1; + u8 has_sysctrl:1; u8 needs_scratch:1; u8 skip_guc_pc:1; u8 skip_mtcfg:1;
Signature · 1 line
2.43.0