Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 322d4017 authored by Chris Lew's avatar Chris Lew
Browse files

soc: qcom: smp2p: Add snapshot of smp2p driver



This snapshot is taken as of msm-4.14 'commit <67942dbf2187>
("Merge "net: netfilter: IRC DCC for private clients"")'.

Change-Id: I631058fc6aed60ab7de77fc24d04c154f3d0ea2b
Signed-off-by: default avatarChris Lew <clew@codeaurora.org>
parent 53c6cac7
Loading
Loading
Loading
Loading
+143 −41
Original line number Diff line number Diff line
/*
 * Copyright (c) 2015, Sony Mobile Communications AB.
 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
 * Copyright (c) 2012-2013, 2018 The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
@@ -27,6 +27,8 @@
#include <linux/soc/qcom/smem_state.h>
#include <linux/spinlock.h>

#include <linux/ipc_logging.h>

/*
 * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
 * of a single 32-bit value between two processors.  Each value has a single
@@ -48,8 +50,12 @@
#define SMP2P_MAX_ENTRY_NAME 16

#define SMP2P_FEATURE_SSR_ACK 0x1
#define SMP2P_FLAGS_RESTART_DONE_BIT 0
#define SMP2P_FLAGS_RESTART_ACK_BIT 1

#define SMP2P_MAGIC 0x504d5324
#define SMP2P_VERSION	1
#define SMP2P_FEATURES	SMP2P_FEATURE_SSR_ACK

/**
 * struct smp2p_smem_item - in memory communication structure
@@ -105,6 +111,7 @@ struct smp2p_entry {

	struct irq_domain *domain;
	DECLARE_BITMAP(irq_enabled, 32);
	DECLARE_BITMAP(irq_pending, 32);
	DECLARE_BITMAP(irq_rising, 32);
	DECLARE_BITMAP(irq_falling, 32);

@@ -142,9 +149,14 @@ struct qcom_smp2p {

	unsigned valid_entries;

	bool ssr_ack_enabled;
	bool ssr_ack;
	bool open;

	unsigned local_pid;
	unsigned remote_pid;

	int irq;
	struct regmap *ipc_regmap;
	int ipc_offset;
	int ipc_bit;
@@ -156,6 +168,14 @@ struct qcom_smp2p {
	struct list_head outbound;
};

static void *ilc;
#define SMP2P_LOG_PAGE_CNT 2
#define SMP2P_INFO(x, ...)	\
do {	\
	if (ilc) \
		ipc_log_string(ilc, "[%s]: "x, __func__, ##__VA_ARGS__); \
} while (0)

static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
{
	/* Make sure any updated data is written before the kick */
@@ -169,41 +189,66 @@ static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
	}
}

/**
 * qcom_smp2p_intr() - interrupt handler for incoming notifications
 * @irq:	unused
 * @data:	smp2p driver context
 *
 * Handle notifications from the remote side to handle newly allocated entries
 * or any changes to the state bits of existing entries.
 */
static irqreturn_t qcom_smp2p_intr(int irq, void *data)
static bool qcom_smp2p_check_ssr(struct qcom_smp2p *smp2p)
{
	struct smp2p_smem_item *in;
	struct smp2p_entry *entry;
	struct qcom_smp2p *smp2p = data;
	unsigned smem_id = smp2p->smem_items[SMP2P_INBOUND];
	unsigned pid = smp2p->remote_pid;
	size_t size;
	int irq_pin;
	u32 status;
	char buf[SMP2P_MAX_ENTRY_NAME];
	struct smp2p_smem_item *in = smp2p->in;
	bool restart;

	if (!smp2p->ssr_ack_enabled)
		return false;

	restart = in->flags & BIT(SMP2P_FLAGS_RESTART_DONE_BIT);
	if (restart == smp2p->ssr_ack)
		return false;

	SMP2P_INFO("%d: SSR DETECTED\n", smp2p->remote_pid);
	return true;
}

static void qcom_smp2p_do_ssr_ack(struct qcom_smp2p *smp2p)
{
	struct smp2p_smem_item *out = smp2p->out;
	u32 ack;
	u32 val;
	int i;

	in = smp2p->in;
	smp2p->ssr_ack = ack = !smp2p->ssr_ack;
	ack = ack << SMP2P_FLAGS_RESTART_ACK_BIT;

	/* Acquire smem item, if not already found */
	if (!in) {
		in = qcom_smem_get(pid, smem_id, &size);
		if (IS_ERR(in)) {
			dev_err(smp2p->dev,
				"Unable to acquire remote smp2p item\n");
			return IRQ_HANDLED;
	val = out->flags & ~BIT(SMP2P_FLAGS_RESTART_ACK_BIT);
	val |= ack;
	out->flags = val;

	qcom_smp2p_kick(smp2p);
}

		smp2p->in = in;
static void qcom_smp2p_negotiate(struct qcom_smp2p *smp2p)
{
	struct smp2p_smem_item *out = smp2p->out;
	struct smp2p_smem_item *in = smp2p->in;
	u32 features;

	if (in->version == out->version) {
		features = in->features & out->features;
		out->features = features;

		if (features & SMP2P_FEATURE_SSR_ACK)
			smp2p->ssr_ack_enabled = true;

		smp2p->open = true;
		SMP2P_INFO("%d: state=open ssr_ack=%d\n", smp2p->remote_pid,
			   smp2p->ssr_ack_enabled);
	}
}

static void qcom_smp2p_notify_in(struct qcom_smp2p *smp2p)
{
	struct smp2p_smem_item *in = smp2p->in;
	struct smp2p_entry *entry;
	unsigned long status;
	int irq_pin;
	char buf[SMP2P_MAX_ENTRY_NAME];
	u32 val;
	int i;

	/* Match newly created entries */
	for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
@@ -227,21 +272,71 @@ static irqreturn_t qcom_smp2p_intr(int irq, void *data)

		status = val ^ entry->last_value;
		entry->last_value = val;
		status |= *entry->irq_pending;

		/* No changes of this entry? */
		if (!status)
			continue;

		for_each_set_bit(i, entry->irq_enabled, 32) {
			if (!(status & BIT(i)))
				continue;
		SMP2P_INFO("%d: %s: status:%0x val:%0x\n",
			   smp2p->remote_pid, entry->name, status, val);

		for_each_set_bit(i, &status, 32) {
			if ((val & BIT(i) && test_bit(i, entry->irq_rising)) ||
			    (!(val & BIT(i)) && test_bit(i, entry->irq_falling))) {
				irq_pin = irq_find_mapping(entry->domain, i);
				handle_nested_irq(irq_pin);

				if (test_bit(i, entry->irq_enabled))
					clear_bit(i, entry->irq_pending);
				else
					set_bit(i, entry->irq_pending);
			}
		}
	}
}

/**
 * qcom_smp2p_intr() - interrupt handler for incoming notifications
 * @irq:	unused
 * @data:	smp2p driver context
 *
 * Handle notifications from the remote side to handle newly allocated entries
 * or any changes to the state bits of existing entries.
 */
static irqreturn_t qcom_smp2p_intr(int irq, void *data)
{
	struct smp2p_smem_item *in;
	struct qcom_smp2p *smp2p = data;
	unsigned int smem_id = smp2p->smem_items[SMP2P_INBOUND];
	unsigned int pid = smp2p->remote_pid;
	size_t size;

	in = smp2p->in;

	/* Acquire smem item, if not already found */
	if (!in) {
		in = qcom_smem_get(pid, smem_id, &size);
		if (IS_ERR(in)) {
			dev_err(smp2p->dev,
				"Unable to acquire remote smp2p item\n");
			return IRQ_HANDLED;
		}

		smp2p->in = in;
	}

	if (!smp2p->open)
		qcom_smp2p_negotiate(smp2p);

	if (smp2p->open) {
		bool do_restart;

		do_restart = qcom_smp2p_check_ssr(smp2p);
		qcom_smp2p_notify_in(smp2p);

		if (do_restart)
			qcom_smp2p_do_ssr_ack(smp2p);
	}

	return IRQ_HANDLED;
@@ -301,6 +396,8 @@ static int smp2p_irq_map(struct irq_domain *d,
	irq_set_chip_data(irq, entry);
	irq_set_nested_thread(irq, 1);
	irq_set_noprobe(irq);
	irq_set_parent(irq, entry->smp2p->irq);
	irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);

	return 0;
}
@@ -335,6 +432,8 @@ static int smp2p_update_bits(void *data, u32 mask, u32 value)
	val |= value;
	writel(val, entry->value);
	spin_unlock(&entry->lock);
	SMP2P_INFO("%d: %s: orig:0x%0x new:0x%0x\n",
		   entry->smp2p->remote_pid, entry->name, orig, val);

	if (val != orig)
		qcom_smp2p_kick(entry->smp2p);
@@ -398,13 +497,14 @@ static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
	out->remote_pid = smp2p->remote_pid;
	out->total_entries = SMP2P_MAX_ENTRY;
	out->valid_entries = 0;
	out->features = SMP2P_FEATURES;

	/*
	 * Make sure the rest of the header is written before we validate the
	 * item by writing a valid version number.
	 */
	wmb();
	out->version = 1;
	out->version = SMP2P_VERSION;

	qcom_smp2p_kick(smp2p);

@@ -452,9 +552,11 @@ static int qcom_smp2p_probe(struct platform_device *pdev)
	struct device_node *node;
	struct qcom_smp2p *smp2p;
	const char *key;
	int irq;
	int ret;

	if (!ilc)
		ilc = ipc_log_context_create(SMP2P_LOG_PAGE_CNT, "smp2p", 0);

	smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
	if (!smp2p)
		return -ENOMEM;
@@ -481,10 +583,10 @@ static int qcom_smp2p_probe(struct platform_device *pdev)
	if (ret)
		goto report_read_failure;

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
	smp2p->irq = platform_get_irq(pdev, 0);
	if (smp2p->irq < 0) {
		dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
		return irq;
		return smp2p->irq;
	}

	smp2p->mbox_client.dev = &pdev->dev;
@@ -537,15 +639,15 @@ static int qcom_smp2p_probe(struct platform_device *pdev)
	/* Kick the outgoing edge after allocating entries */
	qcom_smp2p_kick(smp2p);

	ret = devm_request_threaded_irq(&pdev->dev, irq,
	ret = devm_request_threaded_irq(&pdev->dev, smp2p->irq,
					NULL, qcom_smp2p_intr,
					IRQF_ONESHOT,
					IRQF_NO_SUSPEND | IRQF_ONESHOT,
					"smp2p", (void *)smp2p);
	if (ret) {
		dev_err(&pdev->dev, "failed to request interrupt\n");
		goto unwind_interfaces;
	}

	enable_irq_wake(smp2p->irq);

	return 0;