^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * An example software sink buffer for Intel TH MSU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019 Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/intel_th.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define MAX_SGTS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct msu_sink_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct sg_table **sgts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int nr_sgts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void *msu_sink_assign(struct device *dev, int *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct msu_sink_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) priv->sgts = kcalloc(MAX_SGTS, sizeof(void *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!priv->sgts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) priv->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *mode = MSC_MODE_MULTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void msu_sink_unassign(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct msu_sink_private *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) kfree(priv->sgts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* See also: msc.c: __msc_buffer_win_alloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int msu_sink_alloc_window(void *data, struct sg_table **sgt, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct msu_sink_private *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct scatterlist *sg_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (priv->nr_sgts == MAX_SGTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) nents = DIV_ROUND_UP(size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = sg_alloc_table(*sgt, nents, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) priv->sgts[priv->nr_sgts++] = *sgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) for_each_sg((*sgt)->sgl, sg_ptr, nents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) block = dma_alloc_coherent(priv->dev->parent->parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) PAGE_SIZE, &sg_dma_address(sg_ptr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sg_set_buf(sg_ptr, block, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* See also: msc.c: __msc_buffer_win_free() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void msu_sink_free_window(void *data, struct sg_table *sgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct msu_sink_private *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct scatterlist *sg_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for_each_sg(sgt->sgl, sg_ptr, sgt->nents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dma_free_coherent(priv->dev->parent->parent, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) sg_virt(sg_ptr), sg_dma_address(sg_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sg_free_table(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) priv->nr_sgts--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int msu_sink_ready(void *data, struct sg_table *sgt, size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct msu_sink_private *priv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) intel_th_msc_window_unlock(priv->dev, sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct msu_buffer sink_mbuf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .name = "sink",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .assign = msu_sink_assign,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .unassign = msu_sink_unassign,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .alloc_window = msu_sink_alloc_window,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .free_window = msu_sink_free_window,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .ready = msu_sink_ready,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) module_intel_th_msu_buffer(sink_mbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) MODULE_LICENSE("GPL v2");