arm: apple: nvme: Add SART support and RTKit buffer management

The NVMe firmware in the macOS 13 beta blocks or crashes with u-boot's
current minimal RTKit implementation. It does not provide buffers for
the firmware's buffer requests. The ANS2 firmware included in macOS 11
and 12 tolerates this. The firmware included in the first macOS 13 beta
requires buffers for the crashlog and ioreport endpoints to function.

In the case of the NVMe the buffers are physical memory. Access to
physical memory is guarded by what Apple calls SART.
Import m1n1's SART driver (exclusively used for the NVMe controller).
Implement buffer management helpers for RTKit. These are generic since
other devices (none in u-boot so far) require different handling.

Signed-off-by: Janne Grunau <j@jannau.net>
Reviewed-by: Mark Kettenis <kettenis@openbsd.org>
Tested-by: Mark Kettenis <kettenis@openbsd.org>
diff --git a/drivers/nvme/nvme_apple.c b/drivers/nvme/nvme_apple.c
index d9d491c..819b748 100644
--- a/drivers/nvme/nvme_apple.c
+++ b/drivers/nvme/nvme_apple.c
@@ -12,6 +12,7 @@
 
 #include <asm/io.h>
 #include <asm/arch/rtkit.h>
+#include <asm/arch/sart.h>
 #include <linux/iopoll.h>
 
 /* ASC registers */
@@ -66,6 +67,8 @@
 	void *asc;		/* ASC registers */
 	struct reset_ctl_bulk resets; /* ASC reset */
 	struct mbox_chan chan;
+	struct apple_sart *sart;
+	struct apple_rtkit *rtk;
 	struct ans_nvmmu_tcb *tcbs[NVME_Q_NUM]; /* Submission queue TCBs */
 	u32 __iomem *q_db[NVME_Q_NUM]; /* Submission queue doorbell */
 };
@@ -143,11 +146,51 @@
 	nvmeq->sq_tail = tail;
 }
 
+static int nvme_shmem_setup(void *cookie, struct apple_rtkit_buffer *buf)
+{
+	struct apple_nvme_priv *priv = (struct apple_nvme_priv *)cookie;
+
+	if (!buf || buf->dva || !buf->size)
+		return -1;
+
+	buf->buffer = memalign(SZ_16K, ALIGN(buf->size, SZ_16K));
+	if (!buf->buffer)
+		return -ENOMEM;
+
+	if (!sart_add_allowed_region(priv->sart, buf->buffer, buf->size)) {
+		free(buf->buffer);
+		buf->buffer = NULL;
+		buf->size = 0;
+		return -1;
+	}
+
+	buf->dva = (u64)buf->buffer;
+
+	return 0;
+}
+
+static void nvme_shmem_destroy(void *cookie, struct apple_rtkit_buffer *buf)
+{
+	struct apple_nvme_priv *priv = (struct apple_nvme_priv *)cookie;
+
+	if (!buf)
+		return;
+
+	if (buf->buffer) {
+		sart_remove_allowed_region(priv->sart, buf->buffer, buf->size);
+		free(buf->buffer);
+		buf->buffer = NULL;
+		buf->size = 0;
+		buf->dva = 0;
+	}
+}
+
 static int apple_nvme_probe(struct udevice *dev)
 {
 	struct apple_nvme_priv *priv = dev_get_priv(dev);
 	fdt_addr_t addr;
-	u32 ctrl, stat;
+	ofnode of_sart;
+	u32 ctrl, stat, phandle;
 	int ret;
 
 	priv->base = dev_read_addr_ptr(dev);
@@ -167,12 +210,27 @@
 	if (ret < 0)
 		return ret;
 
+	ret = dev_read_u32(dev, "apple,sart", &phandle);
+	if (ret < 0)
+		return ret;
+
+	of_sart = ofnode_get_by_phandle(phandle);
+	priv->sart = sart_init(of_sart);
+	if (!priv->sart)
+		return -EINVAL;
+
 	ctrl = readl(priv->asc + REG_CPU_CTRL);
 	writel(ctrl | REG_CPU_CTRL_RUN, priv->asc + REG_CPU_CTRL);
 
-	ret = apple_rtkit_init(&priv->chan);
-	if (ret < 0)
+	priv->rtk = apple_rtkit_init(&priv->chan, priv, nvme_shmem_setup, nvme_shmem_destroy);
+	if (!priv->rtk)
+		return -ENOMEM;
+
+	ret = apple_rtkit_boot(priv->rtk);
+	if (ret < 0) {
+		printf("%s: NVMe apple_rtkit_boot returned: %d\n", __func__, ret);
 		return ret;
+	}
 
 	ret = readl_poll_sleep_timeout(priv->base + ANS_BOOT_STATUS, stat,
 				       (stat == ANS_BOOT_STATUS_OK), 100,
@@ -206,11 +264,17 @@
 
 	nvme_shutdown(dev);
 
-	apple_rtkit_shutdown(&priv->chan, APPLE_RTKIT_PWR_STATE_SLEEP);
+	apple_rtkit_shutdown(priv->rtk, APPLE_RTKIT_PWR_STATE_SLEEP);
 
 	ctrl = readl(priv->asc + REG_CPU_CTRL);
 	writel(ctrl & ~REG_CPU_CTRL_RUN, priv->asc + REG_CPU_CTRL);
 
+	apple_rtkit_free(priv->rtk);
+	priv->rtk = NULL;
+
+	sart_free(priv->sart);
+	priv->sart = NULL;
+
 	reset_assert_bulk(&priv->resets);
 	reset_deassert_bulk(&priv->resets);