^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2017 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/err.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/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/of_reserved_mem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/qcom_scm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define QCOM_RMTFS_MEM_DEV_MAX (MINORMASK + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static dev_t qcom_rmtfs_mem_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct qcom_rmtfs_mem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct cdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) phys_addr_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) phys_addr_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int client_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int perms;
^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) static ssize_t qcom_rmtfs_mem_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static DEVICE_ATTR(phys_addr, 0444, qcom_rmtfs_mem_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static DEVICE_ATTR(size, 0444, qcom_rmtfs_mem_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static DEVICE_ATTR(client_id, 0444, qcom_rmtfs_mem_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static ssize_t qcom_rmtfs_mem_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct qcom_rmtfs_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (attr == &dev_attr_phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return sprintf(buf, "%pa\n", &rmtfs_mem->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (attr == &dev_attr_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return sprintf(buf, "%pa\n", &rmtfs_mem->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (attr == &dev_attr_client_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return sprintf(buf, "%d\n", rmtfs_mem->client_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct attribute *qcom_rmtfs_mem_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) &dev_attr_phys_addr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) &dev_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) &dev_attr_client_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ATTRIBUTE_GROUPS(qcom_rmtfs_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int qcom_rmtfs_mem_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct qcom_rmtfs_mem *rmtfs_mem = container_of(inode->i_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct qcom_rmtfs_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) get_device(&rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) filp->private_data = rmtfs_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static ssize_t qcom_rmtfs_mem_read(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) char __user *buf, size_t count, loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (*f_pos >= rmtfs_mem->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (*f_pos + count >= rmtfs_mem->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) count = rmtfs_mem->size - *f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (copy_to_user(buf, rmtfs_mem->base + *f_pos, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *f_pos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static ssize_t qcom_rmtfs_mem_write(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) const char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (*f_pos >= rmtfs_mem->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (*f_pos + count >= rmtfs_mem->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) count = rmtfs_mem->size - *f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (copy_from_user(rmtfs_mem->base + *f_pos, buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) *f_pos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int qcom_rmtfs_mem_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct qcom_rmtfs_mem *rmtfs_mem = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) put_device(&rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct class rmtfs_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .name = "rmtfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int qcom_rmtfs_mem_mmap(struct file *filep, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct qcom_rmtfs_mem *rmtfs_mem = filep->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (vma->vm_end - vma->vm_start > rmtfs_mem->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_dbg(&rmtfs_mem->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) "vm_end[%lu] - vm_start[%lu] [%lu] > mem->size[%pa]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) vma->vm_end, vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) (vma->vm_end - vma->vm_start), &rmtfs_mem->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return remap_pfn_range(vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) rmtfs_mem->addr >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) vma->vm_end - vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static const struct file_operations qcom_rmtfs_mem_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .open = qcom_rmtfs_mem_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .read = qcom_rmtfs_mem_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .write = qcom_rmtfs_mem_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .release = qcom_rmtfs_mem_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .llseek = default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .mmap = qcom_rmtfs_mem_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void qcom_rmtfs_mem_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct qcom_rmtfs_mem *rmtfs_mem = container_of(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct qcom_rmtfs_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) kfree(rmtfs_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int qcom_rmtfs_mem_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct device_node *node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct qcom_scm_vmperm perms[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct reserved_mem *rmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct qcom_rmtfs_mem *rmtfs_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) u32 client_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u32 vmid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) rmem = of_reserved_mem_lookup(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!rmem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_err(&pdev->dev, "failed to acquire memory region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = of_property_read_u32(node, "qcom,client-id", &client_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(&pdev->dev, "failed to parse \"qcom,client-id\"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) rmtfs_mem = kzalloc(sizeof(*rmtfs_mem), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!rmtfs_mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rmtfs_mem->addr = rmem->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) rmtfs_mem->client_id = client_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) rmtfs_mem->size = rmem->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) device_initialize(&rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) rmtfs_mem->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) rmtfs_mem->dev.groups = qcom_rmtfs_mem_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) rmtfs_mem->dev.release = qcom_rmtfs_mem_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) rmtfs_mem->base = devm_memremap(&rmtfs_mem->dev, rmtfs_mem->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) rmtfs_mem->size, MEMREMAP_WC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (IS_ERR(rmtfs_mem->base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_err(&pdev->dev, "failed to remap rmtfs_mem region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ret = PTR_ERR(rmtfs_mem->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) cdev_init(&rmtfs_mem->cdev, &qcom_rmtfs_mem_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) rmtfs_mem->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) rmtfs_mem->dev.id = client_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) rmtfs_mem->dev.class = &rmtfs_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dev_err(&pdev->dev, "failed to add cdev: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = of_property_read_u32(node, "qcom,vmid", &vmid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (ret < 0 && ret != -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev_err(&pdev->dev, "failed to parse qcom,vmid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) goto remove_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) } else if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (!qcom_scm_is_available()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto remove_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) perms[0].vmid = QCOM_SCM_VMID_HLOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) perms[0].perm = QCOM_SCM_PERM_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) perms[1].vmid = vmid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) perms[1].perm = QCOM_SCM_PERM_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) rmtfs_mem->perms = BIT(QCOM_SCM_VMID_HLOS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) ret = qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) &rmtfs_mem->perms, perms, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) dev_err(&pdev->dev, "assign memory failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto remove_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_set_drvdata(&pdev->dev, rmtfs_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) remove_cdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) put_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) put_device(&rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int qcom_rmtfs_mem_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct qcom_rmtfs_mem *rmtfs_mem = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct qcom_scm_vmperm perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (rmtfs_mem->perms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) perm.vmid = QCOM_SCM_VMID_HLOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) perm.perm = QCOM_SCM_PERM_RW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) qcom_scm_assign_mem(rmtfs_mem->addr, rmtfs_mem->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) &rmtfs_mem->perms, &perm, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) cdev_device_del(&rmtfs_mem->cdev, &rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) put_device(&rmtfs_mem->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static const struct of_device_id qcom_rmtfs_mem_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) { .compatible = "qcom,rmtfs-mem" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) MODULE_DEVICE_TABLE(of, qcom_rmtfs_mem_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static struct platform_driver qcom_rmtfs_mem_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .probe = qcom_rmtfs_mem_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .remove = qcom_rmtfs_mem_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .name = "qcom_rmtfs_mem",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .of_match_table = qcom_rmtfs_mem_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int __init qcom_rmtfs_mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ret = class_register(&rmtfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto unregister_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = platform_driver_register(&qcom_rmtfs_mem_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto unregister_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) unregister_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) unregister_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) class_unregister(&rmtfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) module_init(qcom_rmtfs_mem_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static void __exit qcom_rmtfs_mem_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) platform_driver_unregister(&qcom_rmtfs_mem_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) class_unregister(&rmtfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) module_exit(qcom_rmtfs_mem_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_AUTHOR("Linaro Ltd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) MODULE_DESCRIPTION("Qualcomm Remote Filesystem memory driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) MODULE_LICENSE("GPL v2");