Path: blob/master/drivers/firmware/tegra/bpmp-tegra186.c
26428 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (c) 2018, NVIDIA CORPORATION.3*/45#include <linux/genalloc.h>6#include <linux/io.h>7#include <linux/mailbox_client.h>8#include <linux/of_reserved_mem.h>9#include <linux/platform_device.h>1011#include <soc/tegra/bpmp.h>12#include <soc/tegra/bpmp-abi.h>13#include <soc/tegra/ivc.h>1415#include "bpmp-private.h"1617struct tegra186_bpmp {18struct tegra_bpmp *parent;1920struct {21struct gen_pool *pool;22union {23void __iomem *sram;24void *dram;25};26dma_addr_t phys;27} tx, rx;2829struct {30struct mbox_client client;31struct mbox_chan *channel;32} mbox;33};3435static inline struct tegra_bpmp *36mbox_client_to_bpmp(struct mbox_client *client)37{38struct tegra186_bpmp *priv;3940priv = container_of(client, struct tegra186_bpmp, mbox.client);4142return priv->parent;43}4445static bool tegra186_bpmp_is_message_ready(struct tegra_bpmp_channel *channel)46{47int err;4849err = tegra_ivc_read_get_next_frame(channel->ivc, &channel->ib);50if (err) {51iosys_map_clear(&channel->ib);52return false;53}5455return true;56}5758static bool tegra186_bpmp_is_channel_free(struct tegra_bpmp_channel *channel)59{60int err;6162err = tegra_ivc_write_get_next_frame(channel->ivc, &channel->ob);63if (err) {64iosys_map_clear(&channel->ob);65return false;66}6768return true;69}7071static int tegra186_bpmp_ack_message(struct tegra_bpmp_channel *channel)72{73return tegra_ivc_read_advance(channel->ivc);74}7576static int tegra186_bpmp_post_message(struct tegra_bpmp_channel *channel)77{78return tegra_ivc_write_advance(channel->ivc);79}8081static int tegra186_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)82{83struct tegra186_bpmp *priv = bpmp->priv;84int err;8586err = mbox_send_message(priv->mbox.channel, NULL);87if (err < 0)88return err;8990mbox_client_txdone(priv->mbox.channel, 0);9192return 0;93}9495static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)96{97struct tegra_bpmp *bpmp = data;98struct tegra186_bpmp *priv = bpmp->priv;99100if (WARN_ON(priv->mbox.channel == NULL))101return;102103tegra186_bpmp_ring_doorbell(bpmp);104}105106static int tegra186_bpmp_channel_init(struct tegra_bpmp_channel *channel,107struct tegra_bpmp *bpmp,108unsigned int index)109{110struct tegra186_bpmp *priv = bpmp->priv;111size_t message_size, queue_size;112struct iosys_map rx, tx;113unsigned int offset;114int err;115116channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),117GFP_KERNEL);118if (!channel->ivc)119return -ENOMEM;120121message_size = tegra_ivc_align(MSG_MIN_SZ);122queue_size = tegra_ivc_total_queue_size(message_size);123offset = queue_size * index;124125if (priv->rx.pool) {126iosys_map_set_vaddr_iomem(&rx, priv->rx.sram + offset);127iosys_map_set_vaddr_iomem(&tx, priv->tx.sram + offset);128} else {129iosys_map_set_vaddr(&rx, priv->rx.dram + offset);130iosys_map_set_vaddr(&tx, priv->tx.dram + offset);131}132133err = tegra_ivc_init(channel->ivc, NULL, &rx, priv->rx.phys + offset, &tx,134priv->tx.phys + offset, 1, message_size, tegra186_bpmp_ivc_notify,135bpmp);136if (err < 0) {137dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",138index, err);139return err;140}141142init_completion(&channel->completion);143channel->bpmp = bpmp;144145return 0;146}147148static void tegra186_bpmp_channel_reset(struct tegra_bpmp_channel *channel)149{150/* reset the channel state */151tegra_ivc_reset(channel->ivc);152153/* sync the channel state with BPMP */154while (tegra_ivc_notified(channel->ivc))155;156}157158static void tegra186_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)159{160tegra_ivc_cleanup(channel->ivc);161}162163static void mbox_handle_rx(struct mbox_client *client, void *data)164{165struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);166167tegra_bpmp_handle_rx(bpmp);168}169170static void tegra186_bpmp_teardown_channels(struct tegra_bpmp *bpmp)171{172struct tegra186_bpmp *priv = bpmp->priv;173unsigned int i;174175for (i = 0; i < bpmp->threaded.count; i++) {176if (!bpmp->threaded_channels[i].bpmp)177continue;178179tegra186_bpmp_channel_cleanup(&bpmp->threaded_channels[i]);180}181182tegra186_bpmp_channel_cleanup(bpmp->rx_channel);183tegra186_bpmp_channel_cleanup(bpmp->tx_channel);184185if (priv->tx.pool) {186gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.sram, 4096);187gen_pool_free(priv->rx.pool, (unsigned long)priv->rx.sram, 4096);188}189}190191static int tegra186_bpmp_dram_init(struct tegra_bpmp *bpmp)192{193struct tegra186_bpmp *priv = bpmp->priv;194struct resource res;195size_t size;196int err;197198err = of_reserved_mem_region_to_resource(bpmp->dev->of_node, 0, &res);199if (err < 0) {200dev_warn(bpmp->dev, "failed to parse memory region: %d\n", err);201return err;202}203204size = resource_size(&res);205206if (size < SZ_8K) {207dev_warn(bpmp->dev, "DRAM region must be larger than 8 KiB\n");208return -EINVAL;209}210211priv->tx.phys = res.start;212priv->rx.phys = res.start + SZ_4K;213214priv->tx.dram = devm_memremap(bpmp->dev, priv->tx.phys, size,215MEMREMAP_WC);216if (IS_ERR(priv->tx.dram)) {217err = PTR_ERR(priv->tx.dram);218dev_warn(bpmp->dev, "failed to map DRAM region: %d\n", err);219return err;220}221222priv->rx.dram = priv->tx.dram + SZ_4K;223224return 0;225}226227static int tegra186_bpmp_sram_init(struct tegra_bpmp *bpmp)228{229struct tegra186_bpmp *priv = bpmp->priv;230int err;231232priv->tx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 0);233if (!priv->tx.pool) {234dev_err(bpmp->dev, "TX shmem pool not found\n");235return -EPROBE_DEFER;236}237238priv->tx.sram = (void __iomem *)gen_pool_dma_alloc(priv->tx.pool, 4096,239&priv->tx.phys);240if (!priv->tx.sram) {241dev_err(bpmp->dev, "failed to allocate from TX pool\n");242return -ENOMEM;243}244245priv->rx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 1);246if (!priv->rx.pool) {247dev_err(bpmp->dev, "RX shmem pool not found\n");248err = -EPROBE_DEFER;249goto free_tx;250}251252priv->rx.sram = (void __iomem *)gen_pool_dma_alloc(priv->rx.pool, 4096,253&priv->rx.phys);254if (!priv->rx.sram) {255dev_err(bpmp->dev, "failed to allocate from RX pool\n");256err = -ENOMEM;257goto free_tx;258}259260return 0;261262free_tx:263gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.sram, 4096);264265return err;266}267268static int tegra186_bpmp_setup_channels(struct tegra_bpmp *bpmp)269{270unsigned int i;271int err;272273err = tegra186_bpmp_dram_init(bpmp);274if (err == -ENODEV) {275err = tegra186_bpmp_sram_init(bpmp);276if (err < 0)277return err;278}279280err = tegra186_bpmp_channel_init(bpmp->tx_channel, bpmp,281bpmp->soc->channels.cpu_tx.offset);282if (err < 0)283return err;284285err = tegra186_bpmp_channel_init(bpmp->rx_channel, bpmp,286bpmp->soc->channels.cpu_rx.offset);287if (err < 0) {288tegra186_bpmp_channel_cleanup(bpmp->tx_channel);289return err;290}291292for (i = 0; i < bpmp->threaded.count; i++) {293unsigned int index = bpmp->soc->channels.thread.offset + i;294295err = tegra186_bpmp_channel_init(&bpmp->threaded_channels[i],296bpmp, index);297if (err < 0)298break;299}300301if (err < 0)302tegra186_bpmp_teardown_channels(bpmp);303304return err;305}306307static void tegra186_bpmp_reset_channels(struct tegra_bpmp *bpmp)308{309unsigned int i;310311/* reset message channels */312tegra186_bpmp_channel_reset(bpmp->tx_channel);313tegra186_bpmp_channel_reset(bpmp->rx_channel);314315for (i = 0; i < bpmp->threaded.count; i++)316tegra186_bpmp_channel_reset(&bpmp->threaded_channels[i]);317}318319static int tegra186_bpmp_init(struct tegra_bpmp *bpmp)320{321struct tegra186_bpmp *priv;322int err;323324priv = devm_kzalloc(bpmp->dev, sizeof(*priv), GFP_KERNEL);325if (!priv)326return -ENOMEM;327328priv->parent = bpmp;329bpmp->priv = priv;330331err = tegra186_bpmp_setup_channels(bpmp);332if (err < 0)333return err;334335/* mbox registration */336priv->mbox.client.dev = bpmp->dev;337priv->mbox.client.rx_callback = mbox_handle_rx;338priv->mbox.client.tx_block = false;339priv->mbox.client.knows_txdone = false;340341priv->mbox.channel = mbox_request_channel(&priv->mbox.client, 0);342if (IS_ERR(priv->mbox.channel)) {343err = PTR_ERR(priv->mbox.channel);344dev_err(bpmp->dev, "failed to get HSP mailbox: %d\n", err);345tegra186_bpmp_teardown_channels(bpmp);346return err;347}348349tegra186_bpmp_reset_channels(bpmp);350351return 0;352}353354static void tegra186_bpmp_deinit(struct tegra_bpmp *bpmp)355{356struct tegra186_bpmp *priv = bpmp->priv;357358mbox_free_channel(priv->mbox.channel);359360tegra186_bpmp_teardown_channels(bpmp);361}362363static int tegra186_bpmp_resume(struct tegra_bpmp *bpmp)364{365tegra186_bpmp_reset_channels(bpmp);366367return 0;368}369370const struct tegra_bpmp_ops tegra186_bpmp_ops = {371.init = tegra186_bpmp_init,372.deinit = tegra186_bpmp_deinit,373.is_response_ready = tegra186_bpmp_is_message_ready,374.is_request_ready = tegra186_bpmp_is_message_ready,375.ack_response = tegra186_bpmp_ack_message,376.ack_request = tegra186_bpmp_ack_message,377.is_response_channel_free = tegra186_bpmp_is_channel_free,378.is_request_channel_free = tegra186_bpmp_is_channel_free,379.post_response = tegra186_bpmp_post_message,380.post_request = tegra186_bpmp_post_message,381.ring_doorbell = tegra186_bpmp_ring_doorbell,382.resume = tegra186_bpmp_resume,383};384385386