Path: blob/master/drivers/accel/amdxdna/amdxdna_mailbox.c
51892 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2022-2024, Advanced Micro Devices, Inc.3*/45#include <drm/drm_device.h>6#include <drm/drm_managed.h>7#include <linux/bitfield.h>8#include <linux/interrupt.h>9#include <linux/iopoll.h>10#include <linux/slab.h>11#include <linux/xarray.h>1213#define CREATE_TRACE_POINTS14#include <trace/events/amdxdna.h>1516#include "amdxdna_mailbox.h"1718#define MB_ERR(chann, fmt, args...) \19({ \20typeof(chann) _chann = chann; \21dev_err((_chann)->mb->dev, "xdna_mailbox.%d: "fmt, \22(_chann)->msix_irq, ##args); \23})24#define MB_DBG(chann, fmt, args...) \25({ \26typeof(chann) _chann = chann; \27dev_dbg((_chann)->mb->dev, "xdna_mailbox.%d: "fmt, \28(_chann)->msix_irq, ##args); \29})30#define MB_WARN_ONCE(chann, fmt, args...) \31({ \32typeof(chann) _chann = chann; \33dev_warn_once((_chann)->mb->dev, "xdna_mailbox.%d: "fmt, \34(_chann)->msix_irq, ##args); \35})3637#define MAGIC_VAL 0x1D000000U38#define MAGIC_VAL_MASK 0xFF00000039#define MAX_MSG_ID_ENTRIES 25640#define MSG_RX_TIMER 200 /* milliseconds */41#define MAILBOX_NAME "xdna_mailbox"4243enum channel_res_type {44CHAN_RES_X2I,45CHAN_RES_I2X,46CHAN_RES_NUM47};4849struct mailbox {50struct device *dev;51struct xdna_mailbox_res res;52};5354struct mailbox_channel {55struct mailbox *mb;56struct xdna_mailbox_chann_res res[CHAN_RES_NUM];57int msix_irq;58u32 iohub_int_addr;59struct xarray chan_xa;60u32 next_msgid;61u32 x2i_tail;6263/* Received msg related fields */64struct workqueue_struct *work_q;65struct work_struct rx_work;66u32 i2x_head;67bool bad_state;68};6970#define MSG_BODY_SZ GENMASK(10, 0)71#define MSG_PROTO_VER GENMASK(23, 16)72struct xdna_msg_header {73__u32 total_size;74__u32 sz_ver;75__u32 id;76__u32 opcode;77} __packed;7879static_assert(sizeof(struct xdna_msg_header) == 16);8081struct mailbox_pkg {82struct xdna_msg_header header;83__u32 payload[];84};8586/* The protocol version. */87#define MSG_PROTOCOL_VERSION 0x188/* The tombstone value. */89#define TOMBSTONE 0xDEADFACE9091struct mailbox_msg {92void *handle;93int (*notify_cb)(void *handle, void __iomem *data, size_t size);94size_t pkg_size; /* package size in bytes */95struct mailbox_pkg pkg;96};9798static void mailbox_reg_write(struct mailbox_channel *mb_chann, u32 mbox_reg, u32 data)99{100struct xdna_mailbox_res *mb_res = &mb_chann->mb->res;101void __iomem *ringbuf_addr = mb_res->mbox_base + mbox_reg;102103writel(data, ringbuf_addr);104}105106static u32 mailbox_reg_read(struct mailbox_channel *mb_chann, u32 mbox_reg)107{108struct xdna_mailbox_res *mb_res = &mb_chann->mb->res;109void __iomem *ringbuf_addr = mb_res->mbox_base + mbox_reg;110111return readl(ringbuf_addr);112}113114static inline void115mailbox_set_headptr(struct mailbox_channel *mb_chann, u32 headptr_val)116{117mailbox_reg_write(mb_chann, mb_chann->res[CHAN_RES_I2X].mb_head_ptr_reg, headptr_val);118mb_chann->i2x_head = headptr_val;119}120121static inline void122mailbox_set_tailptr(struct mailbox_channel *mb_chann, u32 tailptr_val)123{124mailbox_reg_write(mb_chann, mb_chann->res[CHAN_RES_X2I].mb_tail_ptr_reg, tailptr_val);125mb_chann->x2i_tail = tailptr_val;126}127128static inline u32129mailbox_get_headptr(struct mailbox_channel *mb_chann, enum channel_res_type type)130{131return mailbox_reg_read(mb_chann, mb_chann->res[type].mb_head_ptr_reg);132}133134static inline u32135mailbox_get_tailptr(struct mailbox_channel *mb_chann, enum channel_res_type type)136{137return mailbox_reg_read(mb_chann, mb_chann->res[type].mb_tail_ptr_reg);138}139140static inline u32141mailbox_get_ringbuf_size(struct mailbox_channel *mb_chann, enum channel_res_type type)142{143return mb_chann->res[type].rb_size;144}145146static inline int mailbox_validate_msgid(int msg_id)147{148return (msg_id & MAGIC_VAL_MASK) == MAGIC_VAL;149}150151static int mailbox_acquire_msgid(struct mailbox_channel *mb_chann, struct mailbox_msg *mb_msg)152{153u32 msg_id;154int ret;155156ret = xa_alloc_cyclic_irq(&mb_chann->chan_xa, &msg_id, mb_msg,157XA_LIMIT(0, MAX_MSG_ID_ENTRIES - 1),158&mb_chann->next_msgid, GFP_NOWAIT);159if (ret < 0)160return ret;161162/*163* Add MAGIC_VAL to the higher bits.164*/165msg_id |= MAGIC_VAL;166return msg_id;167}168169static void mailbox_release_msgid(struct mailbox_channel *mb_chann, int msg_id)170{171msg_id &= ~MAGIC_VAL_MASK;172xa_erase_irq(&mb_chann->chan_xa, msg_id);173}174175static void mailbox_release_msg(struct mailbox_channel *mb_chann,176struct mailbox_msg *mb_msg)177{178MB_DBG(mb_chann, "msg_id 0x%x msg opcode 0x%x",179mb_msg->pkg.header.id, mb_msg->pkg.header.opcode);180if (mb_msg->notify_cb)181mb_msg->notify_cb(mb_msg->handle, NULL, 0);182kfree(mb_msg);183}184185static int186mailbox_send_msg(struct mailbox_channel *mb_chann, struct mailbox_msg *mb_msg)187{188void __iomem *write_addr;189u32 ringbuf_size;190u32 head, tail;191u32 start_addr;192u32 tmp_tail;193int ret;194195head = mailbox_get_headptr(mb_chann, CHAN_RES_X2I);196tail = mb_chann->x2i_tail;197ringbuf_size = mailbox_get_ringbuf_size(mb_chann, CHAN_RES_X2I) - sizeof(u32);198start_addr = mb_chann->res[CHAN_RES_X2I].rb_start_addr;199tmp_tail = tail + mb_msg->pkg_size;200201202check_again:203if (tail >= head && tmp_tail > ringbuf_size) {204write_addr = mb_chann->mb->res.ringbuf_base + start_addr + tail;205writel(TOMBSTONE, write_addr);206207/* tombstone is set. Write from the start of the ringbuf */208tail = 0;209tmp_tail = tail + mb_msg->pkg_size;210}211212if (tail < head && tmp_tail >= head) {213ret = read_poll_timeout(mailbox_get_headptr, head,214tmp_tail < head || tail >= head,2151, 100, false, mb_chann, CHAN_RES_X2I);216if (ret)217return ret;218219if (tail >= head)220goto check_again;221}222223write_addr = mb_chann->mb->res.ringbuf_base + start_addr + tail;224memcpy_toio(write_addr, &mb_msg->pkg, mb_msg->pkg_size);225mailbox_set_tailptr(mb_chann, tail + mb_msg->pkg_size);226227trace_mbox_set_tail(MAILBOX_NAME, mb_chann->msix_irq,228mb_msg->pkg.header.opcode,229mb_msg->pkg.header.id);230231return 0;232}233234static int235mailbox_get_resp(struct mailbox_channel *mb_chann, struct xdna_msg_header *header,236void __iomem *data)237{238struct mailbox_msg *mb_msg;239int msg_id;240int ret = 0;241242msg_id = header->id;243if (!mailbox_validate_msgid(msg_id)) {244MB_ERR(mb_chann, "Bad message ID 0x%x", msg_id);245return -EINVAL;246}247248msg_id &= ~MAGIC_VAL_MASK;249mb_msg = xa_erase_irq(&mb_chann->chan_xa, msg_id);250if (!mb_msg) {251MB_ERR(mb_chann, "Cannot find msg 0x%x", msg_id);252return -EINVAL;253}254255MB_DBG(mb_chann, "opcode 0x%x size %d id 0x%x",256header->opcode, header->total_size, header->id);257if (mb_msg->notify_cb) {258ret = mb_msg->notify_cb(mb_msg->handle, data, header->total_size);259if (unlikely(ret))260MB_ERR(mb_chann, "Message callback ret %d", ret);261}262263kfree(mb_msg);264return ret;265}266267static int mailbox_get_msg(struct mailbox_channel *mb_chann)268{269struct xdna_msg_header header;270void __iomem *read_addr;271u32 msg_size, rest;272u32 ringbuf_size;273u32 head, tail;274u32 start_addr;275int ret;276277tail = mailbox_get_tailptr(mb_chann, CHAN_RES_I2X);278head = mb_chann->i2x_head;279ringbuf_size = mailbox_get_ringbuf_size(mb_chann, CHAN_RES_I2X);280start_addr = mb_chann->res[CHAN_RES_I2X].rb_start_addr;281282if (unlikely(tail > ringbuf_size || !IS_ALIGNED(tail, 4))) {283MB_WARN_ONCE(mb_chann, "Invalid tail 0x%x", tail);284return -EINVAL;285}286287/* ringbuf empty */288if (head == tail)289return -ENOENT;290291if (head == ringbuf_size)292head = 0;293294/* Peek size of the message or TOMBSTONE */295read_addr = mb_chann->mb->res.ringbuf_base + start_addr + head;296header.total_size = readl(read_addr);297/* size is TOMBSTONE, set next read from 0 */298if (header.total_size == TOMBSTONE) {299if (head < tail) {300MB_WARN_ONCE(mb_chann, "Tombstone, head 0x%x tail 0x%x",301head, tail);302return -EINVAL;303}304mailbox_set_headptr(mb_chann, 0);305return 0;306}307308if (unlikely(!header.total_size || !IS_ALIGNED(header.total_size, 4))) {309MB_WARN_ONCE(mb_chann, "Invalid total size 0x%x", header.total_size);310return -EINVAL;311}312msg_size = sizeof(header) + header.total_size;313314if (msg_size > ringbuf_size - head || msg_size > tail - head) {315MB_WARN_ONCE(mb_chann, "Invalid message size %d, tail %d, head %d",316msg_size, tail, head);317return -EINVAL;318}319320rest = sizeof(header) - sizeof(u32);321read_addr += sizeof(u32);322memcpy_fromio((u32 *)&header + 1, read_addr, rest);323read_addr += rest;324325ret = mailbox_get_resp(mb_chann, &header, read_addr);326327mailbox_set_headptr(mb_chann, head + msg_size);328/* After update head, it can equal to ringbuf_size. This is expected. */329trace_mbox_set_head(MAILBOX_NAME, mb_chann->msix_irq,330header.opcode, header.id);331332return ret;333}334335static irqreturn_t mailbox_irq_handler(int irq, void *p)336{337struct mailbox_channel *mb_chann = p;338339trace_mbox_irq_handle(MAILBOX_NAME, irq);340/* Schedule a rx_work to call the callback functions */341queue_work(mb_chann->work_q, &mb_chann->rx_work);342343return IRQ_HANDLED;344}345346static void mailbox_rx_worker(struct work_struct *rx_work)347{348struct mailbox_channel *mb_chann;349int ret;350351mb_chann = container_of(rx_work, struct mailbox_channel, rx_work);352353if (READ_ONCE(mb_chann->bad_state)) {354MB_ERR(mb_chann, "Channel in bad state, work aborted");355return;356}357358again:359mailbox_reg_write(mb_chann, mb_chann->iohub_int_addr, 0);360361while (1) {362/*363* If return is 0, keep consuming next message, until there is364* no messages or an error happened.365*/366ret = mailbox_get_msg(mb_chann);367if (ret == -ENOENT)368break;369370/* Other error means device doesn't look good, disable irq. */371if (unlikely(ret)) {372MB_ERR(mb_chann, "Unexpected ret %d, disable irq", ret);373WRITE_ONCE(mb_chann->bad_state, true);374return;375}376}377378/*379* The hardware will not generate interrupt if firmware creates a new380* response right after driver clears interrupt register. Check381* the interrupt register to make sure there is not any new response382* before exiting.383*/384if (mailbox_reg_read(mb_chann, mb_chann->iohub_int_addr))385goto again;386}387388int xdna_mailbox_send_msg(struct mailbox_channel *mb_chann,389const struct xdna_mailbox_msg *msg, u64 tx_timeout)390{391struct xdna_msg_header *header;392struct mailbox_msg *mb_msg;393size_t pkg_size;394int ret;395396pkg_size = sizeof(*header) + msg->send_size;397if (pkg_size > mailbox_get_ringbuf_size(mb_chann, CHAN_RES_X2I)) {398MB_ERR(mb_chann, "Message size larger than ringbuf size");399return -EINVAL;400}401402if (unlikely(!IS_ALIGNED(msg->send_size, 4))) {403MB_ERR(mb_chann, "Message must be 4 bytes align");404return -EINVAL;405}406407/* The fist word in payload can NOT be TOMBSTONE */408if (unlikely(((u32 *)msg->send_data)[0] == TOMBSTONE)) {409MB_ERR(mb_chann, "Tomb stone in data");410return -EINVAL;411}412413if (READ_ONCE(mb_chann->bad_state)) {414MB_ERR(mb_chann, "Channel in bad state");415return -EPIPE;416}417418mb_msg = kzalloc(sizeof(*mb_msg) + pkg_size, GFP_KERNEL);419if (!mb_msg)420return -ENOMEM;421422mb_msg->handle = msg->handle;423mb_msg->notify_cb = msg->notify_cb;424mb_msg->pkg_size = pkg_size;425426header = &mb_msg->pkg.header;427/*428* Hardware use total_size and size to split huge message.429* We do not support it here. Thus the values are the same.430*/431header->total_size = msg->send_size;432header->sz_ver = FIELD_PREP(MSG_BODY_SZ, msg->send_size) |433FIELD_PREP(MSG_PROTO_VER, MSG_PROTOCOL_VERSION);434header->opcode = msg->opcode;435memcpy(mb_msg->pkg.payload, msg->send_data, msg->send_size);436437ret = mailbox_acquire_msgid(mb_chann, mb_msg);438if (unlikely(ret < 0)) {439MB_ERR(mb_chann, "mailbox_acquire_msgid failed");440goto msg_id_failed;441}442header->id = ret;443444MB_DBG(mb_chann, "opcode 0x%x size %d id 0x%x",445header->opcode, header->total_size, header->id);446447ret = mailbox_send_msg(mb_chann, mb_msg);448if (ret) {449MB_DBG(mb_chann, "Error in mailbox send msg, ret %d", ret);450goto release_id;451}452453return 0;454455release_id:456mailbox_release_msgid(mb_chann, header->id);457msg_id_failed:458kfree(mb_msg);459return ret;460}461462struct mailbox_channel *463xdna_mailbox_create_channel(struct mailbox *mb,464const struct xdna_mailbox_chann_res *x2i,465const struct xdna_mailbox_chann_res *i2x,466u32 iohub_int_addr,467int mb_irq)468{469struct mailbox_channel *mb_chann;470int ret;471472if (!is_power_of_2(x2i->rb_size) || !is_power_of_2(i2x->rb_size)) {473pr_err("Ring buf size must be power of 2");474return NULL;475}476477mb_chann = kzalloc(sizeof(*mb_chann), GFP_KERNEL);478if (!mb_chann)479return NULL;480481mb_chann->mb = mb;482mb_chann->msix_irq = mb_irq;483mb_chann->iohub_int_addr = iohub_int_addr;484memcpy(&mb_chann->res[CHAN_RES_X2I], x2i, sizeof(*x2i));485memcpy(&mb_chann->res[CHAN_RES_I2X], i2x, sizeof(*i2x));486487xa_init_flags(&mb_chann->chan_xa, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);488mb_chann->x2i_tail = mailbox_get_tailptr(mb_chann, CHAN_RES_X2I);489mb_chann->i2x_head = mailbox_get_headptr(mb_chann, CHAN_RES_I2X);490491INIT_WORK(&mb_chann->rx_work, mailbox_rx_worker);492mb_chann->work_q = create_singlethread_workqueue(MAILBOX_NAME);493if (!mb_chann->work_q) {494MB_ERR(mb_chann, "Create workqueue failed");495goto free_and_out;496}497498/* Everything look good. Time to enable irq handler */499ret = request_irq(mb_irq, mailbox_irq_handler, 0, MAILBOX_NAME, mb_chann);500if (ret) {501MB_ERR(mb_chann, "Failed to request irq %d ret %d", mb_irq, ret);502goto destroy_wq;503}504505mb_chann->bad_state = false;506mailbox_reg_write(mb_chann, mb_chann->iohub_int_addr, 0);507508MB_DBG(mb_chann, "Mailbox channel created (irq: %d)", mb_chann->msix_irq);509return mb_chann;510511destroy_wq:512destroy_workqueue(mb_chann->work_q);513free_and_out:514kfree(mb_chann);515return NULL;516}517518int xdna_mailbox_destroy_channel(struct mailbox_channel *mb_chann)519{520struct mailbox_msg *mb_msg;521unsigned long msg_id;522523MB_DBG(mb_chann, "IRQ disabled and RX work cancelled");524free_irq(mb_chann->msix_irq, mb_chann);525destroy_workqueue(mb_chann->work_q);526/* We can clean up and release resources */527528xa_for_each(&mb_chann->chan_xa, msg_id, mb_msg)529mailbox_release_msg(mb_chann, mb_msg);530531xa_destroy(&mb_chann->chan_xa);532533MB_DBG(mb_chann, "Mailbox channel destroyed, irq: %d", mb_chann->msix_irq);534kfree(mb_chann);535return 0;536}537538void xdna_mailbox_stop_channel(struct mailbox_channel *mb_chann)539{540/* Disable an irq and wait. This might sleep. */541disable_irq(mb_chann->msix_irq);542543/* Cancel RX work and wait for it to finish */544cancel_work_sync(&mb_chann->rx_work);545MB_DBG(mb_chann, "IRQ disabled and RX work cancelled");546}547548struct mailbox *xdnam_mailbox_create(struct drm_device *ddev,549const struct xdna_mailbox_res *res)550{551struct mailbox *mb;552553mb = drmm_kzalloc(ddev, sizeof(*mb), GFP_KERNEL);554if (!mb)555return NULL;556mb->dev = ddev->dev;557558/* mailbox and ring buf base and size information */559memcpy(&mb->res, res, sizeof(*res));560561return mb;562}563564565